Skip to content

feat(conn-server): reconcile the effect outbox after a process loss, as T3 - #268

Merged
nckrtl merged 19 commits into
mainfrom
feat/conn-server-effect-outbox-reconcile
Nov 24, 2026
Merged

nckrtl merged 19 commits into
mainfrom
feat/conn-server-effect-outbox-reconcile

Conversation

@nckrtl

@nckrtl nckrtl commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #261. After a crash or restart, Conn Server cancelled every unsettled outbox row, as it lost in-memory effects before. T3 instead splits them by whether they can safely run twice, and decides from evidence whether the provider took a prompt.

Change: port T3's reconcileAfterProcessLoss and its recovery (ProviderRuntimeRecoveryService.reconcileProjection). All of it is in conn-engine, except the settings resolution in conn-server.

  • At start, process-bound rows are cancelled: turn start, steer, interrupt, abandon start, stop background work, respond, apply model selection (T3's PROCESS_BOUND_EFFECT_TYPES). restart.rs then decides again from the stored runs.
  • Replay-safe rows the old process was running go back to pending and run again, in place: session close, checkpoint capture, title, HTML attachment cleanup, thread closed, remote task changed, and rollback. A waiting run whose capture row is still queued leaves the capture to that row.
  • A rollback keeps its place in the lane. The journal moves its row on from its phase: provider_done resumes from the stored answer, and provider fails with ROLLBACK_INTERRUPTED_MESSAGE. An unsettled row whose journal entry is gone committed its result already, so it settles. refactor(conn-server): keep the rollback phase in its outbox row #269 moves the phase into the row.
  • Evidence of acceptance. A run's prompt counts as accepted only when its provider-turn.start row settled, which happens after Session::start_turn returned. T3 reads a provider turn of the run's attempt. Here a provider turn proves nothing either way: Codex gets turn/start before its reply is stored, and our Claude adapter reports its turn about 3.5 s before a cold SDK host has the prompt. A store without start rows (from before the outbox) falls back to T3's provider-turn rule.
  • An unaccepted run is not sent again on its own (T3's reconcileProjection):
    • A user's prompt waits at the head of the held queue, with its attempt, root node and turn item back to pending. The user sends it again.
    • Unattended work (a scheduled task, an MCP agent's prompt) is sent again, at most 3 times (MAX_RESENDS). It is never sent to an archived, deleted, settled or snoozed thread.
    • Anything else, preparing included, is cancelled.
  • continueThreadsAfterServerUpdate (audit 5.1): nothing read it before. conn-server resolves it per project, with an override winning either way, as T3's resolveProjectSettings does. It passes the result as EngineConfig::restart_policy, and the restart checks it before a continuation or an unattended resend.
  • A replayed capture wakes a parent once. The capture ends the run, and writes the parent's wake, only when it finds the run waiting.
  • The worker starts after the host. With EngineConfig::hold_effects, rows wait until the server calls start_effects, after its scheduled tasks, devices and /mcp are up. A replayed thread.closed(Deleted) then deletes the thread's schedules.
  • A dropped worker cannot settle a replayed row's new claim. feat(conn-server): commit effects with their command in an outbox #261's engine lease already fences it; a store test proves it.
  • An inert copy (recover_runs off) still cancels every unsettled row.
  • reconcile_after_process_loss checks ownership and writes in one immediate transaction, as feat(conn-server): commit effects with their command in an outbox #261's startup cancel does. A stale open gets NOT_OWNER and aborts. a_stale_open_cancels_nothing covers it.

Second gate fixes (BLOCK on bf696e8bc4), in conn-engine, plus the host in conn-server. Each test failed on the reviewed head first:

# Finding Fix Test (how it failed)
1 Codex confirmed the turn before the start row settled, so its prompt was held or sent again A provider turn of the attempt with a strong native ref counts as acceptance, next to a settled start row. Claude's weak cold-start turn still proves nothing. Table case RunningConfirmedTurn (the run was held instead of cancelled)
2 An interrupted or cancelled child with a checkpoint scope never woke its parent The first capture to commit writes the run's end for every terminal outcome and marks its capture row (progress.runEnded). A replayed row finds the mark. an_interrupted_child_with_a_checkpoint_wakes_its_parent_once (0 wakes)
3 A continuation cut off by a second crash was cancelled A run with restartContinuationOfRunId is sent again under the project policy and the chain limit. Table case Continuation (0 starts)
4 A resend ignored a stored Stop A resend is skipped when the run, or the running task that delegated its thread, has a run_interrupt_request, or when a request of the run is pending (T3's restartStopRequested). Table case McpStopped (sent again)
5 An opted-out project's queued prompt started Prompts queued behind a cut run are held before the continuation decision. an_opted_out_restart_holds_the_queued_prompt (B ran)
6 Hold and resend skipped the dead-session cleanup Sessions are stopped on every path, and the attempt and node reset for both kept-run outcomes. The table's held cases check the session (it stayed ready)
7 A replayed thread.closed(Deleted) settled before its schedule cleanup EngineHost::thread_closed returns a future that the effect awaits. The host awaits device cleanup and delete_for_thread. a_close_settles_after_its_host_cleanup. Proof: with the host future dropped instead of awaited, the row settled during the cleanup.

Third gate fixes (BLOCK on 4982214bec), in conn-engine, plus the host in conn-server. Each test failed before its fix:

# Finding Fix Test (how it failed)
1 A crashed continuation was resent forever Its claimed resends count toward the chain limit. a_continuation_cut_off_again_and_again_stops_at_the_chain_limit (round 4 still starting)
2 A Stop after acceptance, then a crash, still continued may_continue applies the run and delegating-task Stop checks, as the resend does. a_stopped_run_is_not_continued_after_a_restart (a continuation ran)
3 An unmarked #261 capture row woke the parent twice A terminal run whose checkpoint committed counts as already ended. an_unmarked_capture_row_replayed_wakes_the_parent_once (2 wakes)
4 An opted-out delegated child left its parent waiting Without a continuation, recovery commits the family completion (on_run_end). an_opted_out_child_ends_for_its_parent_at_a_restart (the task stayed running)
5 A resend returned before hold_queue Sessions are stopped and the queue held before every exit, the waiting capture included. a_resend_holds_the_prompt_queued_behind_it (B ran)
6 Terminal teardown ran on the Tokio thread The close runs it on spawn_blocking and awaits it. None: it is a threading change.
7 The legacy path took Claude's weak turn as acceptance Without start rows, only a confirmed (strong) turn counts. Table case LegacyEarlyTurn (not held)
8 The waiting exit skipped stop_sessions Sessions are stopped before the waiting branch. a_restart_finishes_a_run_left_waiting_for_its_checkpoint now checks the session (it stayed ready)
9 Cleanup errors were swallowed thread_closed returns its error. The close retries with T3's backoff, up to 5 attempts, then the worker settles the row failed with the error. Device release has a 30 s overall deadline. a_failed_close_cleanup_is_retried_then_recorded. Proof: with the error dropped, there was no retry and the row settled succeeded.

Fifth gate fixes (on 76c6734b49), in conn-engine, conn-devices and the host in conn-server. Each test failed on 76c6734b49 first:

# Finding Fix Test (how it failed)
3 P1: a replayed capture started a prompt queued behind an interrupted run, in a project that opted out At start, the queue of every thread with an unfinished run is held once, before any replayed effect can run. That includes threads whose only unfinished run is queued. restart::recover no longer holds it itself. a_replayed_capture_does_not_start_a_queued_prompt: A is interrupted with B queued, and the store is copied while A's capture is unsettled. After the restart, B was starting; now it stays queued and held.
4 P2: cleanup_thread dropped Devices::close errors, and a device timeout skipped deleting the thread's schedules cleanup_thread tries every session and returns the first error. The host runs the terminal close, the schedule delete and the device release each on its own, and returns their errors together, so the close effect retries. a_thread_cleanup_reports_a_device_it_could_not_close (conn-devices): a session on a reconnecting SSH host returned (); now it returns the "reconnecting" error and the session stays. The host's step order has no test seam: conn-server cannot make a device session.

Known deviations from T3

  • Acceptance comes from the start row, for the reason above.
  • Queued runs: now held at restart, as T3 holds them.

Tests. Each gate probe fails on 3934265f9a:

  • a_prompt_the_provider_never_accepted_is_held_resent_or_cancelled ports T3's "recovers $0 work after restart" table. It covers starting, running, running with an early provider turn, scheduled, MCP, MCP capped, MCP snoozed, MCP archived and user archived.
  • a_held_prompt_runs_when_the_user_sends_it and a_restart_holds_an_undelivered_message_for_the_user, which replaces the test that expected an automatic resend.
  • a_replayed_capture_wakes_the_parent_once, where the probe got 2 parent messages.
  • held_effects_wait_for_the_host.
  • a_restart_keeps_a_rollback_ahead_of_later_work. The probe left rollback 1 and detach 2; the restart produced detach 2, then rollback 3.
  • a_replaced_worker_cannot_settle_the_new_claim_of_a_replayed_row is a store test. It passes on the base too: feat(conn-server): commit effects with their command in an outbox #261's lease covers it.
  • Audit 5.1: a_project_override_decides_whether_a_restart_continues covers both directions, global off with the project on, and global on with the project off. The opt-out direction fails on the base. conn-server also has restart_policy_follows_the_project_override.
  • Kept: a replay-safe effect runs again once, and an inert copy cancels it. The store tests cover the split.

Checks (Mac):

tools/check.sh was not run locally: the disk was under 60 GB free. CI runs everything on Linux.

Rebase onto #260 (still being fixed; not rebased yet).

Follow-up: a restart after a settlement whose settle script had not started yet skips the script. This matches the TypeScript server, which does not persist settle cleanup.

🤖 Generated with Claude Code

nckrtl and others added 2 commits November 23, 2026 12:49
Effects now commit in the same transaction as their command, as T3's
EventSink and EffectOutbox do, and one worker claims and runs them
(T3's EffectWorker). A closed socket that aborts a handler after the
commit can no longer lose a committed provider start. The in-memory
effect lanes are gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The restart tests open a second engine on the store while the first
engine's tasks still run. The first engine's worker could still claim the
new engine's rows, such as the continuation's turn start, and write events
with its own stale sequence counter: "UNIQUE constraint failed:
events.seq" (1 in 30 runs on Linux, 9 in 120 under load).

Dropping the engine now stops its worker and prune loop. The claim checks
the flag while it holds the write lock, and the worker checks it again
before it runs what it claimed, so a claim in flight at the drop takes
nothing the next engine wrote.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…h on commit

Three gate findings on the effect outbox:

- A dropped or replaced engine still wrote: an effect already running
  committed into the next engine's store with a stale sequence. The
  engine now takes an engine_epoch lease at open, and every write checks
  the lease and the drop flag inside its own transaction (Tx::commit,
  claim, settle, prune). The drop flag at claim and before execution
  stays.
- One failed settlement write held the thread's lane until a restart.
  As T3's worker does, a settlement retries while the store is locked
  (100 ms), and any other failure settles the row failed instead, so the
  lane frees without running the effect again.
- A commit published its sequence only when its caller lived, so an
  aborted caller hid the change from connected clients. Tx::commit now
  sends it, for every command path, as T3's EventSink publishes after
  its transaction.

Also: pruning lets the store go between 500-row batches, and the lane
order test waits on the host's event instead of a 50 ms sleep.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nckrtl and others added 2 commits November 23, 2026 18:57
An outbox row outlives a server update. Fields a later build adds to
CleanupHtmlAttachments take #[serde(default)], so a row stored before
them still decodes (audit 6.7 adds one).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…runtime

Four gate findings on the outbox:

- A replaced engine could send a rollback twice: its phase update checked
  only the command id and took zero changed rows as success. The move
  from admitted to provider is now one write that checks the engine's
  lease and the expected phase, and the provider is contacted only when
  exactly one row moved. Otherwise the rollback stops and writes nothing.
- Overlapping opens: engine A's late startup cancel could cancel engine
  B's committed start. The ownership check and the cancel are one
  immediate transaction; a stale open gets NOT_OWNER and aborts.
- Two failed settlement writes left a row running. The settlement is
  kept and tried again with backoff (100 ms, doubling, 30 s at most) for
  as long as the engine owns the store; the effect ran once.
- The server opened the store, ran its migrations and recovery on the
  runtime thread. SqliteEngine::start opens on the blocking pool; the
  worker starts once recovery committed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nckrtl
nckrtl force-pushed the feat/conn-server-effect-outbox-reconcile branch from 3934265 to bf696e8 Compare November 23, 2026 19:05
@github-actions github-actions Bot added size:XXL and removed size:L labels Nov 23, 2026
…t, and settlement

Three gate findings (round 3):

- A rollback fenced after the provider answered still restored files
  and deleted refs. Ok(false) from the provider_done move now stops it,
  and the rollback checks ownership again after it takes the workspace
  lock, before it touches Git.
- A start the server gave up on still ran on the blocking pool and took
  the lease after a newer engine opened. start() marks the open
  abandoned when its future drops; take_ownership reads that flag under
  the lease's write lock and takes nothing.
- A settlement kept retrying after another engine took the store. After
  any failed write, both retry paths read the durable epoch and stop on
  a mismatch; the write keeps its epoch predicate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nckrtl
nckrtl force-pushed the feat/conn-server-effect-outbox-reconcile branch from bf696e8 to 4982214 Compare November 23, 2026 20:22
Four gate rounds found races between two engines on one store. In
production only one may exist: serve holds the state-directory lock,
and T3 is one process. SqliteEngine::open now makes that the engine's
own invariant: it takes an exclusive advisory lock on <database>.lock
before it reads, migrates or recovers the store (on the blocking pool
under start), and holds it until the engine drops. A second open, in
this process or another, gets STORE_IN_USE and touches nothing.

A child process forked elsewhere in the process keeps a copy of the
lock's file until its exec, so an open retries the lock for up to 2 s
before it reports the store in use.

The epoch fencing stays as defence in depth.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nckrtl
nckrtl force-pushed the feat/conn-server-effect-outbox-reconcile branch from 4982214 to 76c6734 Compare November 23, 2026 21:13
nckrtl and others added 6 commits November 23, 2026 22:47
… restore

A symlink to a live store took its own lock file, so a second engine opened
beside the first. The lock path now comes from the resolved database path, a
file not created yet through its folder and any symlink naming it.

A dropped engine released the lock while its rollback still restored files.
The rollback now upgrades the engine's lock before it touches the workspace
and holds it until files and refs are done; a failed upgrade fences it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…as T3

A restart now follows T3's reconcileAfterProcessLoss. Process-bound
effects (turn start, steer, Stop, answers, model changes) are cancelled,
and the restart decides again from the stored runs. Replay-safe effects
(capture, session close, cleanups, titles, host notices) run again. A
rollback row is still cancelled and enqueued again from pending_rollbacks;
the next change folds that journal into the row.

A running run with no provider turn for its attempt never got its prompt,
so the restart sends it again (T3's rewindUnacceptedRuns) instead of
asking the agent to continue.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Seven gate findings on the process-loss reconcile, and audit 5.1:

- A prompt a provider never confirmed was sent again on its own. Codex
  gets turn/start before its reply is stored, and our Claude adapter
  reports its turn before a cold SDK host has the prompt, so neither a
  missing nor a present provider-turn row proves anything. A start now
  counts as accepted only when its provider-turn.start row settled
  (start_turn returned); a store without start rows falls back to
  provider turns. As T3's reconcileProjection: a user's prompt waits at
  the head of the held queue, unattended work (scheduled, MCP) is sent
  again at most 3 times and never to an archived, deleted, settled or
  snoozed thread, and anything else is cancelled.
- A replayed checkpoint capture woke the parent again. The capture ends
  the run, and wakes its parent, only when it finds the run waiting.
- A replayed thread.closed ran before the server's scheduled tasks were
  ready. EngineConfig::hold_effects keeps the worker stopped until the
  server calls start_effects after its host services.
- A rollback row was cancelled and enqueued again at the lane's end, so
  later work overtook it. It now stays in place: the journal moves it on
  from its phase, and a row whose entry is gone (result committed)
  settles.
- A dropped worker settling a replayed row: already fenced by the
  engine lease (#261); a store test now proves it.
- Audit 5.1: nothing read continueThreadsAfterServerUpdate, so a restart
  continued cut turns after the user opted out. The server resolves it
  per project (an override wins either way, as T3's
  resolveProjectSettings) into EngineConfig::restart_policy, and the
  restart checks it before a continuation or an unattended resend.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…and Stops

Seven gate findings on the process-loss recovery:

- A strong provider turn of the run's attempt (Codex's turn/started,
  Claude's first usage) now counts as acceptance next to a settled start
  row. Claude's weak cold-start turn still proves nothing.
- A child with a checkpoint scope that was interrupted or cancelled got
  no parent wake: the capture only ended runs it found waiting. The
  first capture to commit now writes the run's end for every outcome and
  marks its row; a replay of that row finds the mark.
- A restart continuation cut off by a second crash was cancelled. It is
  now sent again under the project policy and the chain limit.
- A resend now skips a run with a stored Stop, on itself or on the task
  that delegated its thread, and a run with a waiting request.
- Queued prompts behind a cut run are held before the continuation
  decision, so an opted-out project no longer starts them.
- Dead sessions are stopped, and the attempt and node reset, on the
  hold and resend paths too.
- EngineHost::thread_closed returns a future the effect awaits, so a
  replayed delete finishes its schedule cleanup before it settles.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… families

Nine gate findings on the restart recovery:

- A continuation sent again keeps its run id; its claimed resends now
  count toward the chain limit, so crash after crash ends at 3.
- A Stop stored on the run or its delegating task now blocks the
  continuation too, not only the resend.
- A capture row from an older build has no mark: a terminal run whose
  checkpoint committed counts as already ended, so a replay wakes the
  parent once.
- A run recovery cancels without a continuation (an opted-out project)
  ends for its parent: the family completion commits then.
- Sessions are stopped and the queue held before every exit, the
  resend and the waiting capture included.
- A store without start rows takes only a provider-confirmed turn as
  acceptance, not Claude's weak cold-start turn.
- Terminal teardown runs on the blocking pool. thread_closed returns its
  error; the close tries again with T3's backoff (5 attempts) and then
  settles its row failed with that error. Device release has a 30 s
  deadline.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…reports device errors

A replayed checkpoint capture started a prompt queued behind an interrupted
run, in a project that opted out of restart continuation: only threads with
an active run had their queue held. The start now holds the queue of every
thread with an unfinished run, queued-only ones included, before any replayed
effect can run.

Devices::cleanup_thread dropped close errors, so a reconnecting SSH device
host reported success and the close was never retried. It now returns the
first failure after trying every session. The host's thread close runs the
terminal close, the scheduled-task delete and the device release each on its
own, so a device timeout no longer skips the schedules, and returns their
errors together.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nckrtl
nckrtl force-pushed the feat/conn-server-effect-outbox-reconcile branch from 76c6734 to 274edac Compare November 23, 2026 22:29
nckrtl and others added 5 commits November 24, 2026 01:10
…tions

A Claude background-task notification queued behind a run that waits for its
checkpoint lost its buffered output with the old process. Recovery now cancels
it before holding the rest of the queue, as ProviderRuntimeRecoveryService does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oval

A queued run never started, so it owns no runtime request. Cancelling a
buffered notification at restart cancelled the active run's pending approval
too, and recovery then continued that run without it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	apps/conn-server/crates/conn-engine/src/commands.rs
#	apps/conn-server/crates/conn-engine/src/engine.rs
#	apps/conn-server/crates/conn-engine/src/engine_tests.rs
#	apps/conn-server/crates/conn-engine/src/lib.rs
#	apps/conn-server/crates/conn-engine/src/projects.rs
…into HEAD

# Conflicts:
#	apps/conn-server/crates/conn-engine/src/engine.rs
#	apps/conn-server/crates/conn-engine/src/restart.rs
#	apps/conn-server/crates/conn-engine/src/store.rs
#	apps/conn-server/crates/conn-server/src/host.rs
#	apps/conn-server/crates/conn-server/src/serve.rs
#	apps/conn-server/crates/conn-server/src/settings.rs
…estart; old settle closes decode

- A settle close returns before its script runs, so a restart could requeue
  the close and start the script again. The engine now records, durably,
  which settlement's script started (settle_scripts), and run_settle_script
  starts nothing for a settlement it already recorded.
- A settle close stored by the earlier #268 build holds "reason": "settled".
  It now decodes as ThreadClose::SettledUnnamed: the host closes the
  thread's terminals for its current settlement and runs no script, since
  nothing shows whether it ran.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nckrtl
nckrtl changed the base branch from feat/conn-server-effect-outbox to main November 24, 2026 04:53
@nckrtl
nckrtl merged commit 1b0a0d7 into main Nov 24, 2026
44 checks passed
@nckrtl

nckrtl commented Nov 24, 2026

Copy link
Copy Markdown
Owner Author

Included in the merge train #327 (merged as ee7b1f9). Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XXL vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant