Skip to content
Closed
27 changes: 24 additions & 3 deletions src/features/background-agent/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,13 @@ export class BackgroundManager {
const sessionStatus = allStatuses[task.sessionID]

if (!sessionStatus) {
log("[background-agent] Session not found in status:", task.sessionID)
continue
// Note: Background sessions may not appear in session.status()
// Don't skip - fall through to message-based stability detection
log("[background-agent] Session not found in status, checking via messages:", task.sessionID)
// Removed: early continue that skipped completion logic
}

if (sessionStatus.type === "idle") {
if (sessionStatus?.type === "idle") {
const hasIncompleteTodos = await this.checkSessionTodos(task.sessionID)
if (hasIncompleteTodos) {
log("[background-agent] Task has incomplete todos via polling, waiting:", task.id)
Expand Down Expand Up @@ -504,6 +506,25 @@ export class BackgroundManager {
if (!task.progress) {
task.progress = { toolCalls: 0, lastUpdate: new Date() }
}

// Stability detection: if message count unchanged for 3 polls, consider complete
const currentMsgCount = messages.length
const progress = task.progress as { stableCount?: number; lastMsgCount?: number }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stableCount and lastMsgCount not in TaskProgress type - extend the type in types.ts to include these fields

if (progress.lastMsgCount === currentMsgCount && currentMsgCount > 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Task that completes instantly with 0 messages will never reach stability detection

Suggested change
if (progress.lastMsgCount === currentMsgCount && currentMsgCount > 0) {
if (progress.lastMsgCount === currentMsgCount) {

progress.stableCount = (progress.stableCount ?? 0) + 1
if (progress.stableCount >= 3) {
log("[background-agent] Task completed via stability detection:", task.id)
task.status = "completed"
task.completedAt = new Date()
this.markForNotification(task)
this.notifyParentSession(task)
continue
}
} else {
progress.stableCount = 0
}
progress.lastMsgCount = currentMsgCount

task.progress.toolCalls = toolCalls
task.progress.lastTool = lastTool
task.progress.lastUpdate = new Date()
Expand Down
Loading