fix(cli-run): prevent premature exit when background tasks are active

When using `opencode run`, the process exits prematurely if the main
agent dispatches background subtasks. The CLI completion checker relies
on `session.children()` + `session.status()` APIs which cannot see
tasks in "pending" state (no session created yet) or tasks whose
sessions are momentarily idle between operations.

This fix bridges BackgroundManager state to the CLI completion checker
using the existing run-continuation-state marker system:

- Add "background-task" continuation marker source
- BackgroundManager writes/clears markers on task lifecycle events
  (launch, cancel, complete, crash)
- CLI completion checker blocks exit when marker is active
- Fix todo-continuation-enforcer to also check "pending" task status

Closes #3452
This commit is contained in:
柯杨
2026-04-16 11:59:11 +08:00
parent c55e08c1da
commit aa90cff9cc
6 changed files with 44 additions and 3 deletions
+5
View File
@@ -20,6 +20,11 @@ export async function checkCompletionConditions(ctx: RunContext): Promise<boolea
return false
}
if (continuationState.hasActiveBackgroundTaskMarker) {
logWaiting(ctx, continuationState.activeHookMarkerReason ?? "background tasks are active")
return false
}
if (!await areAllChildrenIdle(ctx)) {
return false
}
+2
View File
@@ -16,6 +16,7 @@ export interface ContinuationState {
hasActiveRalphLoop: boolean
hasHookMarker: boolean
hasTodoHookMarker: boolean
hasActiveBackgroundTaskMarker: boolean
hasActiveHookMarker: boolean
activeHookMarkerReason: string | null
}
@@ -32,6 +33,7 @@ export async function getContinuationState(
hasActiveRalphLoop: hasActiveRalphLoopContinuation(directory, sessionID),
hasHookMarker: marker !== null,
hasTodoHookMarker: marker?.sources.todo !== undefined,
hasActiveBackgroundTaskMarker: marker?.sources["background-task"]?.state === "active",
hasActiveHookMarker: isContinuationMarkerActive(marker),
activeHookMarkerReason: getActiveContinuationMarkerReason(marker),
}