fix: prevent stale timeout from killing actively running background tasks
The stale detection was checking lastUpdate timestamps BEFORE consulting session.status(), causing tasks to be unfairly killed after 3 minutes even when the session was actively running (e.g., during long tool executions or extended thinking). Changes: - Reorder pollRunningTasks to fetch session.status() before stale check - Skip stale-kill entirely when session status is 'running' - Port no-lastUpdate handling from task-poller.ts into manager.ts (previously manager silently skipped tasks without lastUpdate) - Add sessionStatuses parameter to checkAndInterruptStaleTasks - Add 7 new test cases covering session-status-aware stale detection
This commit is contained in:
@@ -57,14 +57,17 @@ export function pruneStaleTasksAndNotifications(args: {
|
||||
}
|
||||
}
|
||||
|
||||
export type SessionStatusMap = Record<string, { type: string }>
|
||||
|
||||
export async function checkAndInterruptStaleTasks(args: {
|
||||
tasks: Iterable<BackgroundTask>
|
||||
client: OpencodeClient
|
||||
config: BackgroundTaskConfig | undefined
|
||||
concurrencyManager: ConcurrencyManager
|
||||
notifyParentSession: (task: BackgroundTask) => Promise<void>
|
||||
sessionStatuses?: SessionStatusMap
|
||||
}): Promise<void> {
|
||||
const { tasks, client, config, concurrencyManager, notifyParentSession } = args
|
||||
const { tasks, client, config, concurrencyManager, notifyParentSession, sessionStatuses } = args
|
||||
const staleTimeoutMs = config?.staleTimeoutMs ?? DEFAULT_STALE_TIMEOUT_MS
|
||||
const now = Date.now()
|
||||
|
||||
@@ -77,9 +80,11 @@ export async function checkAndInterruptStaleTasks(args: {
|
||||
const sessionID = task.sessionID
|
||||
if (!startedAt || !sessionID) continue
|
||||
|
||||
const sessionIsRunning = sessionStatuses?.[sessionID]?.type === "running"
|
||||
const runtime = now - startedAt.getTime()
|
||||
|
||||
if (!task.progress?.lastUpdate) {
|
||||
if (sessionIsRunning) continue
|
||||
if (runtime <= messageStalenessMs) continue
|
||||
|
||||
const staleMinutes = Math.round(runtime / 60000)
|
||||
@@ -103,6 +108,8 @@ export async function checkAndInterruptStaleTasks(args: {
|
||||
continue
|
||||
}
|
||||
|
||||
if (sessionIsRunning) continue
|
||||
|
||||
if (runtime < MIN_RUNTIME_BEFORE_STALE_MS) continue
|
||||
|
||||
const timeSinceLastUpdate = now - task.progress.lastUpdate.getTime()
|
||||
|
||||
Reference in New Issue
Block a user