claude -p background tasks orphaned across invocations surface false "no completion record from previous session" events
Summary
When Claude Code is driven as a series of one-shot claude -p invocations — e.g. a wrapper that runs one claude -p per conversation turn and continues the conversation via --resume — background tasks started in one invocation (persistent Monitor tasks and background Bash / run_in_background) are bound to that invocation's process. When the process exits normally at the end of the turn, those tasks are orphaned. On the next claude -p invocation, the harness's task reconciliation emits, once per orphaned task, on every turn boundary:
No completion record was found for this background shell command from the previous session. It may have been stopped (via the UI, Monitor timeout, or agent teardown — these leave no transcript marker), or it may have been running when the previous Claude Code process exited. Check the output file for partial results before assuming it completed.
Environment
- Claude Code 2.1.195
- Usage pattern:
claude -p --output-format stream-json --include-partial-messages --resume <session-id>with--permission-mode bypassPermissions— one process per turn (a local chat-bridge wrapper) - Tasks affected: persistent
Monitortasks, backgroundBash(run_in_background)
Repro
- In a
claude -pinvocation, arm a persistentMonitor(or start a backgroundBashtask). - Let that invocation produce its result and exit.
- Start a new
claude -pfor the same conversation with--resume <id>. - Observe a "no completion record … from the previous session" event for each task armed in step 1.
- If the new invocation re-arms them, the cycle repeats on every turn.
Expected
A one-shot claude -p process that exits normally after producing its result should not surface its background tasks as anomalous "may have been running when the process exited" events in a subsequent process. Either:
- background tasks tied to a
-pprocess are reaped quietly when that process exits normally (no cross-invocation reconciliation event carried into--resume), or - the reconciliation distinguishes "parent
-pexited normally" (expected → silent) from "process crashed / was killed" (anomalous → surface).
Actual
Every orphaned task produces a teardown event in the next invocation, indistinguishable from a genuine crash/hang, creating recurring noise surfaced to the user.
Impact
- Recurring, alarming-looking noise on every turn for any wrapper using
claude -pper turn. - Misleading — implies a possible crash/hang when the process simply exited normally.
- Forces wrappers to disable background tasks (
Monitor, backgroundBash) entirely as a workaround.
Possible directions
- Treat normal exit of a
-pprocess as a clean reap point for its background tasks (no carry-forward into--resume). - Tag reconciliation events with the prior process's exit status; surface only on abnormal exit.
- Provide a flag to suppress cross-invocation background-task reconciliation for headless/
-puse.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗