v2 background task lifecycle: revive re-adoption after host restart (P0) + terminal-session GC (P2)
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:
- 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.tsheader). - The only restart-recovery path (transcript rehydration) is running-only.
rehydrateHistoricalRunningTasks(src/hooks/task-session-manager/index.ts:68) skips any historicaltaskpart whose output is notrunning(: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. task_reviveis the only task-control tool without a host-storage fallback.cancel_taskverifies ownership viasession.get(taskID).data.parentID(untrackedTaskReasoninsrc/tools/cancel-task.ts);task_revivethrows atbackgroundJobBoard.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 attask_status; idle → adopt; absent → refuse (that is a fresh-spawn case). - Adoption semantics:
registerLaunch({preserveRun: true})rebuilds the record (agent/description fromsession.get), thenruntimeStatusReconciler.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 thesession.getparentID 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)
- 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.
- Terminal-session GC hooked into the existing eviction semantics (
trimReusable/trimRetained→ suppression recording): optionallysession.removethe evicted child session, gated bybackgroundJobs.pruneEvictedSessions(default false; conditions: terminal + no waiter + record actually evicted). Wall-clock TTL and polling deliberately rejected. - 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