Skip to content

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

Open
CHLK wants to merge 2 commits intocode-yeongyu:devfrom
CHLK:fix/cli-run-premature-exit-with-background-tasks
Open

fix(cli-run): prevent premature exit when background tasks are active#3455
CHLK wants to merge 2 commits intocode-yeongyu:devfrom
CHLK:fix/cli-run-premature-exit-with-background-tasks

Conversation

@CHLK
Copy link
Copy Markdown

@CHLK CHLK commented Apr 15, 2026

Summary

  • Prevent opencode run from exiting prematurely when background tasks are active but no todos exist
  • Use the existing run-continuation-state marker system to signal active background tasks across the process boundary
  • Fix BG task status checks to include "pending" alongside "running" so queued tasks are not invisible

Problem

When running opencode run, the agent would exit immediately after dispatching background tasks because:

  1. Agent dispatches background task(s) → finishes response → session.idle fires
  2. checkCompletionConditions() sees no todos, no active children → returns true
  3. pollForCompletion() returns exit code 0 → process exits
  4. Background tasks are orphaned/aborted during shutdown

PR #217 partially addressed this but only when todos exist. The todo-continuation-enforcer returns early at idle-event.ts:110-114
when there are no todos, bypassing the background task check entirely.

Solution

File Change
src/features/run-continuation-state/types.ts Add "background-task" to ContinuationMarkerSource
src/features/background-agent/manager.ts Add updateBackgroundTaskMarker() — writes/clears marker on task lifecycle events
(launch, tryCompleteTask, cancelTask, failCrashedTask)
src/cli/run/continuation-state.ts Add hasActiveBackgroundTaskMarker field
src/cli/run/completion.ts Check background-task marker in checkCompletionConditions()
src/hooks/todo-continuation-enforcer/idle-event.ts Include "pending" in BG task status check
src/hooks/todo-continuation-enforcer/continuation-injection.ts Include "pending" in BG task status check

Testing

  • bun run typecheck — Pass
  • bun test — 171 relevant tests pass, 0 fail
  • Manual: opencode run with 1 background task — process waits for completion, exits normally
  • Manual: opencode run with 2 parallel background tasks — process waits for both, agent provides final summary

Closes #3452


Summary by cubic

Fixes premature exit in opencode run when background tasks are active. The CLI now waits for running or queued tasks before exiting. Closes #3452.

  • Bug Fixes
    • Added "background-task" continuation marker and hasActiveBackgroundTaskMarker; CLI blocks exit while active.
    • Background manager writes/clears the marker on task lifecycle events and all terminal paths (start errors, non-retryable session errors, stale prune) to prevent stale “active” markers.
    • Treats "pending" as active alongside "running" so queued tasks keep the process alive.

Written for commit dd95d59. Summary will update on new commits.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 15, 2026

All contributors have signed the CLA. Thank you! ✅
Posted by the CLA Assistant Lite bot.

@CHLK
Copy link
Copy Markdown
Author

CHLK commented Apr 15, 2026

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Apr 15, 2026
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

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 stale active continuation 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.

Comment thread src/features/background-agent/manager.ts
@CHLK CHLK force-pushed the fix/cli-run-premature-exit-with-background-tasks branch from f14ea35 to af6812a Compare April 16, 2026 01:00
CHLK added 2 commits April 16, 2026 11:59
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: CLI opencode run exits prematurely when background tasks are active but no todos exist

1 participant