v2 background task lifecycle: revive re-adoption after host restart (P0) + terminal-session GC (P2)

Author: Jiajun0413Created Oct 1, 2026Updated Oct 1, 2026

Root cause (verified, deterministic)

After a host restart, task_revive fails with Unknown or unowned background task even though the child session still exists in host storage. Three-layer gap:

  1. Board records are process-local. The persistence layer stores only tombstones, deletion epochs, and alias high-water marks; job records and the alias→taskID mapping are deliberately not restored (src/utils/background-job-persistence.ts header).
  2. The only restart-recovery path (transcript rehydration) is running-only. rehydrateHistoricalRunningTasks (src/hooks/task-session-manager/index.ts:68) skips any historical task part whose output is not running (:115-119) and dies with transcript compaction/revert. A child that reached a terminal state before the restart — or whose task part was trimmed — is never re-adopted.
  3. task_revive is the only task-control tool without a host-storage fallback. cancel_task verifies ownership via session.get(taskID).data.parentID (untrackedTaskReason in src/tools/cancel-task.ts); task_revive throws at backgroundJobBoard.resolve() miss (src/tools/task-revive.ts).

omoh/omo contrast: omo answers lifecycle with deletion (10-min cleanup delay / 30-min TTL → session.delete); omos answers with retain+reuse but the reuse leg breaks across restarts. The fix is to re-connect the reuse leg, not to adopt omo's deletion model.

P0 design: adopt-on-miss in task_revive

  • Trigger: only when board.resolve() misses, only for native session-ID input (aliases intentionally do not survive restarts).
  • Ownership proof: session.get(taskID).data.parentID === caller — the host-endorsed ledger. Mismatch keeps the current error.
  • Live-state gate (getRuntimeSessionStatusSnapshot): busy/retry → refuse adoption, point at task_status; idle → adopt; absent → refuse (that is a fresh-spawn case).
  • Adoption semantics: registerLaunch({preserveRun: true}) rebuilds the record (agent/description from session.get), then runtimeStatusReconciler.reconcile(), then enter the unchanged revive pipeline (relaunch lease, baseline capture, idle re-verification, queue delivery). No shortcuts around existing fences.
  • Fencing: generation resumes from the persisted epoch high-water (+1) so pre-restart generations can never resurrect; leases die with the process; the tombstone gate must allow live-session adoption while still blocking ghost relaunches of deleted runs (the main implementation risk — the two semantics must meet in one place: registerLaunch).
  • Observability: log + board render marks the record recovered.
  • Tests: host mocks for parentID-match / busy / 404 / other-parent's-child, tombstone interaction, concurrent adoption.
  • Docs: update "Post-restart recovery is partial and best-effort" (docs/background-orchestration.md) and pin the session.get parentID semantic to the audited host version.

Explicit non-goals: startup sweep re-adoption (the adopt-on-miss path is simpler and pay-per-use), automatic alias rebinding, full Board-record persistence, reusing the host's native job recovery (omos children are not in the host job registry).

P2 follow-ups (separate changes)

  1. Persist terminal result summaries alongside tombstones (write-once, zero hot-path cost) so revive of a completed-but-unacknowledged task can return the result instead of re-prompting.
  2. Terminal-session GC hooked into the existing eviction semantics (trimReusable/trimRetained → suppression recording): optionally session.remove the evicted child session, gated by backgroundJobs.pruneEvictedSessions (default false; conditions: terminal + no waiter + record actually evicted). Wall-clock TTL and polling deliberately rejected.
  3. Optional: persist a minimal open-taskID set (identity only) to survive orchestrator-context compaction losing IDs; only if ID-loss incidents are observed.

Context-economy items shipped separately in the companion PR (scoping gaps, revive guidance, prompt economy).

Review provenance: strategic verdict (oracle) + architecture verdict (council seat C) with every fact cross-checked against the working tree.

Source: alvinunreal/oh-my-opencode-slim