fix(cli-run): prevent premature exit when background tasks are active#3455
Open
CHLK wants to merge 2 commits intocode-yeongyu:devfrom
Open
fix(cli-run): prevent premature exit when background tasks are active#3455CHLK wants to merge 2 commits intocode-yeongyu:devfrom
CHLK wants to merge 2 commits intocode-yeongyu:devfrom
Conversation
Contributor
|
All contributors have signed the CLA. Thank you! ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
There was a problem hiding this comment.
1 issue found across 6 files
Confidence score: 3/5
- There is a concrete regression risk in
src/features/background-agent/manager.ts: terminal error/interrupt paths may leave a staleactivecontinuation marker, which can block CLI run completion. - Given the issue is severity 7/10 with high confidence (8/10) and affects completion behavior, this is more than a minor edge case and adds meaningful merge risk.
- Pay close attention to
src/features/background-agent/manager.ts- ensure all terminal exit paths clear or reconcile the continuation marker consistently.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/features/background-agent/manager.ts">
<violation number="1" location="src/features/background-agent/manager.ts:375">
P1: The new background-task continuation marker is set on enqueue, but some terminal error/interrupt paths do not update it, which can leave a stale `active` marker and block CLI run completion.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
f14ea35 to
af6812a
Compare
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 code-yeongyu#3452
…anup Address review feedback: add updateBackgroundTaskMarker() calls to three additional terminal paths in BackgroundManager that were missing marker cleanup, which could leave stale "active" markers and prevent CLI run mode from exiting: - processKey() startTask error handler - handleEvent() session error (non-retryable) - pruneStaleTasksAndNotifications() stale task cleanup Closes code-yeongyu#3452
af6812a to
dd95d59
Compare
This was referenced Apr 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
opencode runfrom exiting prematurely when background tasks are active but no todos existrun-continuation-statemarker system to signal active background tasks across the process boundary"pending"alongside"running"so queued tasks are not invisibleProblem
When running
opencode run, the agent would exit immediately after dispatching background tasks because:session.idlefirescheckCompletionConditions()sees no todos, no active children → returnstruepollForCompletion()returns exit code 0 → process exitsPR #217 partially addressed this but only when todos exist. The
todo-continuation-enforcerreturns early atidle-event.ts:110-114when there are no todos, bypassing the background task check entirely.
Solution
src/features/run-continuation-state/types.ts"background-task"toContinuationMarkerSourcesrc/features/background-agent/manager.tsupdateBackgroundTaskMarker()— writes/clears marker on task lifecycle eventslaunch,tryCompleteTask,cancelTask,failCrashedTask)src/cli/run/continuation-state.tshasActiveBackgroundTaskMarkerfieldsrc/cli/run/completion.tscheckCompletionConditions()src/hooks/todo-continuation-enforcer/idle-event.ts"pending"in BG task status checksrc/hooks/todo-continuation-enforcer/continuation-injection.ts"pending"in BG task status checkTesting
bun run typecheck— Passbun test— 171 relevant tests pass, 0 failopencode runwith 1 background task — process waits for completion, exits normallyopencode runwith 2 parallel background tasks — process waits for both, agent provides final summaryCloses #3452
Summary by cubic
Fixes premature exit in
opencode runwhen background tasks are active. The CLI now waits for running or queued tasks before exiting. Closes #3452."background-task"continuation marker andhasActiveBackgroundTaskMarker; CLI blocks exit while active."pending"as active alongside"running"so queued tasks keep the process alive.Written for commit dd95d59. Summary will update on new commits.