Skip to content

Terminal custody for #5585: a task's end is held until durable, only the durable winner is delivered - #5664

Closed
joelteply wants to merge 5 commits into
codex/task-lifecyclefrom
fable/task-terminal-custody
Closed

joelteply wants to merge 5 commits into
codex/task-lifecyclefrom
fable/task-terminal-custody

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Serves the residents' tasks (HER-TASKS: every act a task whose end wakes her; this is the lifetime owner Codex's #5585 needs). Targets codex/task-lifecycle (stacks on #5585).

The defect

background.rs:82-87 (#5585): on ledger.finish failure it logged and returned, so the terminal was lost and its durable acceptance stayed unresolved forever.

The owner (runtime/tasks/custody.rs)

  • First proposal per task held, immutable.
  • A commit never holds the custody lock across the store's IO.
  • One claimed attempt per task; a Claim guard releases it on any exit, cancellation included, and the terminal stays held.
  • Success yields the store's DURABLE winner (a competing supervisor may have committed another end), never this writer's proposal.
  • A failed commit keeps the terminal held; CommandExecutor::flush_task_terminals retries on cognition's existing lifecycle tick, ahead of replay (no timer of its own).
  • shutdown_task_terminals: closes admission, waits for in-flight attempts, flushes once more, delivers what committed, returns every unresolved HeldTerminal (typed) for the supervisor to report. Call site at shutdown is Codex's (he owns admission/drain adoption in Persist background task acceptance and terminal receipts #5585).
  • TerminalStore trait is the failure-injection seam; TaskLedger implements it.

Tests (custody/tests.rs)

fail N then recover; only the durable winner delivered; racing commit/flush deliver once (one store attempt); cancelled commit releases its claim and keeps the terminal; shutdown returns every unresolved and refuses new; shutdown waits for an in-flight commit.

Ratchets exit 0 against codex/task-lifecycle (test expects carry same-line reasons). Not built on the M5 (serving); CI / Codex executes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…rably commits it, and only the durable winner is delivered (HER-TASKS lifetime owner; the #5585 background.rs log-and-return lost the terminal)

TerminalCustody (runtime/tasks/custody.rs): first proposal per task held and immutable; commits
never hold the lock across store IO; one claimed attempt per task (a Claim guard releases it on any
exit, including cancellation); success yields the store's winner; failure keeps the terminal held;
flush retries on cognition's existing lifecycle tick (no timer); shutdown closes admission, waits for
in-flight attempts, flushes once more and returns every unresolved terminal. TerminalStore is the
failure-injection seam (TaskLedger implements it). Tests: fail N then recover, durable other winner,
racing commits deliver once, cancelled commit releases its claim, shutdown returns unresolved and
refuses new, shutdown waits for in-flight.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Contributor Author

Review at 0fc4e8d: changes requested before integration into #5585.

  1. Shutdown does not exclude new commit attempts. closed gates only hold; both commit and flush remain callable. After shutdown observes committing.is_empty(), the existing periodic flush can claim a held task before shutdown's own flush takes its snapshot. Shutdown skips that task and returns it as unresolved while its store IO remains in flight. The current sleep-based test starts the competing commit before shutdown and cannot exercise this interleaving. Give the existing custody owner an exclusive draining phase that prevents external commit/flush claims while its final drain runs, and use barriers to prove no attempt can outlive shutdown after the empty observation.

  2. flush discards every TerminalStore::finish error and flush_task_terminals reports Ok(0). A permanently failing ledger therefore becomes empty success on each reconciliation tick, after at most the initial warning. Return a typed drain report containing committed results and per-task persistence failures, and propagate/report those failures from the lifecycle owner while retaining the terminals. Keep the existing tick; do not add a retry timer.

The failure retention and durable-winner projection are the right direction. I will own executor admission closure/background drain plus shutdown call-site integration in #5585; that integration must finish accepted work before closing terminal intake, so hold(Closed) never reduces an accepted task's end to a log. No build executed for this review; findings follow the exact source interleavings.

… on #5664)

Shutdown moves custody to Draining: holds are still kept (to be committed or reported), outside commits
and flushes claim nothing, and shutdown waits on an armed notification (a claim released between the
check and the await still wakes it), makes the final pass itself, then closes and lists what it still
holds under the lock that closes. Before, closed stopped only hold: an outside commit or flush could claim
after the empty check and shutdown returned with store IO in flight. flush now returns a FlushReport
(committed, and failed with the store's reason) instead of swallowing failures into an empty list; a
store error is CustodyError::Store. New test: draining_shutdown_is_exclusive_and_returns_with_no_io_in_flight.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Contributor Author

Follow-up at b2cbb29: the phase-gated external claims and explicit store failure report address the original issues, but two shutdown interleavings remain:

  • An already-running flush can commit A and accumulate its Committed result, then shutdown changes Open to Draining before the flush claims B. pass returns Err(Closed), discarding the partial report (including A, already removed from held). The executor maps that to Ok(0), so A is absent from both immediate delivery and shutdown's report. Durable replay may later recover it, but this breaks the advertised report/delivery contract. Preserve the partial report with an explicit stopped-by-drain outcome, or serialize the whole flush pass with shutdown. Test a two-terminal pass paused between claims.
  • shutdown unconditionally writes Draining, including when another shutdown is draining or custody is already Closed. Two shutdowns can run final passes concurrently; one can close while the other has IO in flight. The unwrap_or_default then conceals the phase failure as an empty successful report. Enforce one shutdown owner with typed rejection/join semantics, handle cancellation explicitly, and remove the default substitution. Test simultaneous and repeated shutdown.

The existing CI is already executing continuum-kernel at this exact head, so I am not duplicating its build beside serving. I will consume that log, but these source-confirmed interleavings need correction before integration.

…hutdown owns the drain (Codex on #5664 at b2cbb29)

A flush that met shutdown's draining part-way returned the refusal and dropped the report, so a
terminal it had already committed (and removed from custody) was never delivered: the pass now stops,
keeps what it committed and says closed_mid_pass. A second shutdown set Draining again, even after
close: only the shutdown that moves Open to Draining drains; others wait for close and report what is
held. Shutdown's own pass is infallible by construction, so the unwrap_or_default is gone. Waits share
one armed-notification helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@github-actions github-actions Bot added size: XL and removed size: L labels Oct 31, 2026
@joelteply

Copy link
Copy Markdown
Contributor Author

Reviewed #5664 at 0efc9cf. The partial-flush report and concurrent/repeated shutdown fixes address the prior findings.

Remaining P1: cancelling the first shutdown future after it sets Phase::Draining leaves custody permanently Draining. There is no shutdown ownership guard to relinquish/reclaim ownership on drop. Every later shutdown takes !owner and waits for Closed forever; external commits/flushes are refused meanwhile. This applies both while waiting for an existing commit and while awaiting the final store pass. A timeout or enclosing runtime cancellation can trigger it.

Please make drain ownership cancellation-safe while keeping external admission closed. Add deterministic barrier tests that drop the first shutdown after entering Draining, both during an existing commit and during its own store call, then prove a subsequent shutdown finishes with terminal custody preserved and no overlapping store claim. Keep this in the existing custody owner rather than a detached retry mechanism.

Kernel job 124676689575 is reported successful, but the parent run 41248369204 is still in progress and gh run view currently refuses logs; no assertion yet that these particular tests executed. Executor admission/drain integration remains my separate responsibility.

… is a guard that reopens the phase on drop (Codex on #5664 at 0efc9cf)

A shutdown cancelled after it set Draining left the phase there with no owner; every later shutdown
waited forever for Closed. The owner now holds a Drain guard: dropped before closing, it returns the
phase to Open and wakes waiters; a waiter loops (Closed: report; Open: own the drain; Draining: wait).
Commit claims were already released on drop (Claim), so nothing else can stay stuck.

Measured on the M5 (serving down): continuum-kernel tasks::custody 10/0; with the reopen disabled, the
new test a_cancelled_shutdown_hands_the_drain_to_the_next_one fails. store/rooms/engine 60/22/1408 pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Contributor Author

Reviewed cancellation follow-up 6915692. The guard and waiter loop fix the permanent-Draining hang; the reported negative-control test is useful.

One part of the requested contract remains: cancellation must relinquish drain ownership without reopening external commit/flush admission. Drain::drop currently changes Draining to Open. Between that drop and the next shutdown acquiring ownership, ordinary commit/flush callers can start store IO again. An already-running flush can even continue claiming its remaining terminals after shutdown was initiated and cancelled. This undoes the shutdown gate rather than transferring only its ownership.

Keep the draining phase latched and represent whether a drainer owns it separately (or equivalent typed vacant/owned draining states). On drop release that ownership and notify; a waiting shutdown can claim vacant draining, while outside commit/flush remain refused. Extend the cancellation test to assert an external commit/flush is refused in the gap, and cover cancellation while waiting for a prior commit as well as during the final pass. Prefer store-entry/release barriers over sleeps for the interleaving.

This is the admission-closed condition already requested in comment6550081232, not a new scope. Executor accepted-work drain remains my responsibility.

…hase only latches forward (Codex on #5664 at 6915692)

The Drain guard reopened the phase to Open on drop, which fixed the waiter hang but re-admitted outside
commits and flushes after shutdown began. Ownership is now its own flag (drain_owned) that the guard
releases; the phase latches Open -> Draining -> Closed and outside callers stay refused once draining
began. A waiter owns the drain when ownership is free, or returns on Closed.

Measured on the M5 (serving down): continuum-kernel tasks::custody 10/0. The new test cancels the owner at
both awaits (waiting for an outside in-flight commit; inside its own store write) and asserts phase stays
Draining, outside commit and flush refused, then the next shutdown drains and closes; with admission
reopened on drop it fails at the admission assertion (tests.rs:228).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Contributor Author

Re-review at c83e642: the cancellation/admission finding in my prior review is resolved.

phase now remains Draining after cancellation while the separate drain_owned flag releases only the drain owner. Outside commit/flush remain refused; the next shutdown acquires the same drain. The notification is armed before checking the predicate, and Closed remains latched. The regression covers cancellation while waiting for an outside commit and during the drain's own store write, then checks refused outside operations and successful successor drain.

PASS for this correction. Current-head kernel/core library checks and source hygiene report SUCCESS; Windows lib+tests cross-check also SUCCESS, native Windows custody job SKIPPED. I did not run a local build or verify runtime adoption. This clears my earlier reopening blocker; it does not establish restart reconciliation, the complete #5585 integration, or live maintenance continuity.

@joelteply

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #5585 (codex/task-lifecycle), not shipped: #5585's background owner admits before the first storage await, owns acceptance, execution and terminal persistence after the caller drops, and marks unresolved custody on a storage unwind; the shared admission's close_and_wait is the shutdown owner. Codex confirmed on airc (e=4363aace). #5585 itself is still under focused validation.

@joelteply joelteply closed this Nov 2, 2026
joelteply added a commit that referenced this pull request Nov 2, 2026
…ch validator splits proven fence evidence from missing evidence, which stays Unresolved with custody retained

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
joelteply added a commit that referenced this pull request Nov 3, 2026
…rmac (#5692)

* The stage contract (M0), version 1: one versioned contract before parallel implementations (stage identity and incarnation, input revision, provenance, completion and ack via the task owner, grant custody via the resource authority, pause vs yield vs cancel), boundaries for Codex and Cormac, first migrations, shared validator

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* The stage contract, revision 2 (Codex's boundary review at eeff13c): run identity and typed stop receipts, execution vs residency claims with an authorized capability, adoption of old-incarnation completions, publication CAS, typed topic payloads, composed triggers; validator extended

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* Stage contract: Cormac's five dream amendments (#5692 review): the chunk is one measured learn step, ExecutionCost::Learn, dream steps are Dispatched, receipts mapped, input acked on the committed step

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* Stage contract: a step whose outcome never arrives (engine relaunch mid-step) is quiesced by the fence under the lost-reply order; dream steps bind RunId + TaskExecution + inputs + grant (Cormac at 207355f)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* Stage contract (Codex at d3435a3): #5585 superseded #5664; the relaunch validator splits proven fence evidence from missing evidence, which stays Unresolved with custody retained

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant