Skip to content

fix(conn-server): launch saves a new thread and its first message in one transaction - #262

Merged
nckrtl merged 24 commits into
mainfrom
fix/conn-server-atomic-launch
Nov 24, 2026
Merged

nckrtl merged 24 commits into
mainfrom
fix/conn-server-atomic-launch

Conversation

@nckrtl

@nckrtl nckrtl commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #260 (conn-server/unsettle-restore-anchor). The launch receipts use #260's receipt schema: migration 5 with owner_kind, owner_id, error and the sequence index, and no pruning. A receipt from before the upgrade has no owner and is refused on replay, so it never counts as a launch's first message. This PR adds no table, column or migration. Merge #260 first; this PR's base then moves to main.

A Conn Server launch wrote the new thread and its first message as two commands, so a dropped socket between them left an empty thread. Three gates then found replays and claims that could land on the wrong thread, project or checkout, or leave state behind. As in T3's ThreadLaunchService and orchestrator, each check now runs in the write that commits.

  • One write. thread.create carries its first message, so thread, message and run commit together. A reused thread takes its message in a command of its own.
  • Claimed checkout. The reuse claim records the launch's branch and checkout, as T3 does (ThreadLaunchService.ts:829). Every separate first message, including an older build's split launch, reads the checkout from its claim's own event and checks it in its own write. A rebind meanwhile makes it refuse.
  • Admission. A launch's own checks run in the command's write, before the decision: reusable thread, project identity, checkout and mode limit. As in T3 they record no receipt, so a refused launch id stays usable (T3 test: "does not let a failing same-command caller strand a concurrent durable launch").
  • Receipts. fix(conn-server): an undone settle keeps the thread's place in Active #260's owner columns decide replays. A replayed launch must be the id's own thread.create. Only an accepted message.dispatch of the thread, with its run.created, counts as a first message (T3: "Initial message was accepted without a durable run"). The :initial-message receipt is read only when the claim needs a separate first message or for a verified legacy send.
  • Cancellation and shutdown. A launch runs on a server-owned task. Shutdown refuses new launches, cancels worktree setup through the host, and gives admitted launches a 10 s grace to commit or clean up in process, as T3 closes its preparation scope. A worktree a hard crash leaves stays on disk, as with T3.
  • Worktree setup (conn-vcs). create_worktree reports its claim before git worktree add, and git runs in its own process group. A cancel during post-checkout reaps the hook's process tree and removes the checkout and its branch. A discard removes the branch only once the worktree is gone, and a failed setup says what stayed.
  • Provider. Admission passes a launch already accepted and reads settings off the runtime thread. A thread.create with its first message checks the provider in its commit.
  • Projection. The returned projection must be in the launch's project.
  • Delete. thread.delete honours onlyIfUnused under the thread lock.
  • Attachments. A refused or replayed message releases only the copies its own claim made.
  • Blocking I/O. The .gitmodules check runs off the runtime thread.
  • Test git. Test binaries stop git's repository search at the temp folder.

Depends on #261. A first turn committed just before the RPC is cancelled, but before effect.start(), stays starting. The effect outbox in #261 closes that.

Tests. Each test failed on the head the gate reviewed and passes here. They are in engine_tests.rs unless noted.

Round 5 (failed on 4611c3e630):

  • a_split_launch_replay_keeps_its_claimed_checkout
  • a_continuation_under_the_launch_id_is_no_launch
  • conn-vcs/tests/repos.rs a_cancel_during_post_checkout_removes_the_checkout_and_branch
  • conn-vcs/tests/repos.rs a_worktree_cleanup_cannot_remove_keeps_its_branch_and_says_so

Round 4 (failed on 78fec3f52e):

  • a_rebound_checkout_refuses_the_first_message
  • a_continuation_run_is_no_first_message
  • shutdown_cancels_a_stalled_worktree_setup
  • a_stray_message_id_does_not_change_admission

Round 3 (failed on a419171af3):

  • a_reused_thread_runs_in_the_launch_checkout
  • a_metadata_command_under_the_message_id_is_no_first_message
  • shutdown_waits_for_a_launch_that_owns_a_worktree
  • a_replayed_launch_releases_the_copies_its_claim_made
  • an_invalid_reuse_does_not_burn_its_launch_id
  • a_replay_returns_no_projection_from_another_project

Earlier rounds (failed on e583247679):

  • a_launch_message_checks_its_project_in_its_own_write
  • a_launch_id_another_thread_used_is_a_conflict
  • a_launch_dropped_during_its_claim_still_cleans_up
  • an_older_reuse_replays_after_its_provider_is_off
  • identical_reuse_retries_converge
  • a_reuse_refuses_a_thread_archived_before_its_claim
  • a_provider_turned_off_mid_launch_refuses_it
  • the_receipt_owner_is_a_key_lookup
  • a_receipt_without_an_owner_replays_for_no_thread
  • an_old_launch_id_replays_after_many_writes
  • a_refused_launch_keeps_attachments_it_did_not_claim
  • a_send_wins_against_a_conditional_delete
  • an_old_retry_after_an_undo_and_many_writes_changes_nothing
  • conn-files/tests/files.rs uploads_claims_and_deletes_attachments

Checks. cargo fmt --check passes. cargo clippy -D warnings passes for conn-engine, conn-files and conn-vcs. cargo test passes for the same crates (conn-engine 265).

Checkout cleanup (5da0078). A failed post-checkout hook now keeps the checkout and its files, as Git and the TypeScript server do; only a cancel removes it. A cancel while Git still populates the checkout unlocks the launch's own lock (a unique --reason) before it removes the checkout and branch. Tests: a_failed_post_checkout_hook_keeps_the_checkout_and_its_files, a_cancel_while_git_populates_the_checkout_removes_it.

Old Git unlock. The launch's unlock reads Git's registration files (locked, gitdir) instead of worktree list -z, which Git 2.34 refuses. Test: conn-vcs/tests/old_git.rs with a shim git.

Dropped createWorktree (b45a40a). The direct vcs.createWorktree RPC locks its checkout too. A client that disconnects during a slow post-checkout hook aborted the handler before the unlock, so the checkout stayed locked. create_worktree now holds a drop guard: a dropped call unlocks, removes the checkout and deletes its new branch on a background task, as the TypeScript server does on interrupt. Test: a_dropped_create_worktree_leaves_no_locked_checkout (fails on 27d1e9f with locked t3-create-…, passes now).

Follow-ups (not fixed here).

  • A retry of the same launch during a slow hook replaces the first cancel handle, so worktreeSetup.cancel returns false while the first hook runs; the TypeScript server reserves one preparation per command ID (engine.rs:1349, setup.rs:129).
  • In the contested-path race, cancel force-removes the planned path even without a matching ownership lock; the TypeScript server keeps the other creator's files (worktree.rs:463).

🤖 Generated with Claude Code

…e settle cleared

The engine ignored restoreUnsettledAt, so undoing a settle moved the
thread to the top of Active. It now sets unsettledAt to the given time,
to null for null, and refuses a time that does not parse, as T3's
Orchestrator does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:L labels Nov 23, 2026
…tries and wire times

- An archived thread refuses thread.unsettle before its anchor is read.
- The anchor parses as T3's DateTime.make: a time without a zone is UTC.
- A fresh command on a thread already active records thread.unsettled and
  keeps the anchor and updatedAt; a retry of the same command id records
  nothing. The shell row keeps its updatedAt, as T3's projection does.
- The event envelope carries providerInstanceId, from the updated thread.
- Wire times outside years 0000-9999 get toISOString's signed six-digit
  year, so T3's client decoder reads them.

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

- Command times (restoreUnsettledAt, snoozedUntil, visitedAt) parse as T3's
  DateTime.make: a port of V8's Date.parse, legacy forms included, with
  Effect's zone rule and the server's zone for local times. Checked against
  T3's own answers for 1683 strings in five zones (gen-date-cases.mjs).
- An archived thread refuses settle, auto-settle, unsettle, snooze,
  unsnooze, auto-settle set, pin, unpin and both reorders first, as T3.
- The thread's updatedAt follows T3's ProjectionStore: a thread event writes
  its payload's, every other event its own time, in the row and the stored
  thread (list generated from T3). Pin and settle keep updatedAt as T3 does.
- The event envelope names providerInstanceId wherever T3's does, mapped
  flow by flow; thread-cli-diff compares it with the T3 server.
- Command receipts are kept for good and record refusals: a retry gets the
  recorded refusal, a receipt answers only for its own thread, and an old
  retry after an undo changes nothing (T3's receipt tests, ported).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nckrtl
nckrtl force-pushed the fix/conn-server-atomic-launch branch from e583247 to a419171 Compare November 23, 2026 18:54
@nckrtl
nckrtl changed the base branch from main to conn-server/unsettle-restore-anchor November 23, 2026 18:54
@github-actions github-actions Bot added size:XXL and removed size:XL labels Nov 23, 2026
nckrtl and others added 2 commits November 23, 2026 20:04
… and replay follow T3

- Receipts name their owner (owner_kind thread or project, owner_id), chosen
  by command type as T3's commandThreadId: a fork's and a merge-back's is
  the target thread. Migration 5 names older receipts' owners from their own
  events; one nothing proves is kept and refused, never run twice. A project
  receipt never answers for a thread.
- A replayed queue.resume or queued-run.start checks the queue again and
  answers the recorded sequence. A command that writes nothing answers its
  own thread's latest sequence. Thread setters record their event even when
  nothing changes (a re-pin keeps updatedAt).
- thread.auto-settle refuses a thread set active or changed after its
  snapshot, then settles with the archive guard first.
- thread.visit keeps the later visit, compared as times, and records
  thread.visit-recorded.
- The first message after a merge-back names its instance on the handoff and
  transfer events.
- The receipts' high-water mark reads an index. Opening the engine off the
  runtime thread is #261's.
- thread-cli-diff excuses only the expired question after its session's
  release, row by row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
thread.settle with an expectedLatestRunId that is not the thread's latest
run (every run by ordinal, queued ones too; or no run at all) is refused
with T3's "The thread received another run before settlement." instead of
returning quietly.

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

- Migration 5 names an older receipt's owner only from its own execution:
  the event at the receipt's sequence must carry its command id, so an older
  run of a reused id (main pruned receipts, not events) never decides it. A
  delegation's parent is the thread whose task row names the created child.
  The upgrade pages receipts and commits columns, owners and the version in
  one transaction.
- The queue start holds the queue when the run's provider is off for its
  project (T3's startNextQueuedRun), so a replayed queue.resume starts
  nothing then.
- A project write reads its receipt in its own transaction and records its
  project and command type: a thread command's id, or another project's or
  command's, is refused with T3's text and the receipt kept.
- thread.visit reads the stored read mark as a command time, so main's
  stored forms compare correctly.
- thread-cli-diff excuses the expired question only when it pairs with
  exactly one T3 question for the same native request, thread, run and
  request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nckrtl
nckrtl force-pushed the fix/conn-server-atomic-launch branch from 4611c3e to 2b0b43a Compare November 23, 2026 20:28
… queue start as T3

- Migration 5 only adds the receipt columns (one transaction). A receipt from
  before keeps no owner, so a retry across the upgrade is refused, never run
  twice. The evidence-based backfill and its tests are gone.
- A project command takes its id inside its write (insert-if-absent) and
  validates a receipt another write holds there; a receipt is never
  replaced. A refusal from planning is recorded under the project and T3's
  command type (project.update is project.meta.update) and replayed; folder
  and store failures stay retryable. A replay with no project row is T3's
  typed read error.
- The queue start resolves the run's effective selection (a message that
  follows the conversation takes the thread's) and checks it with T3's queue
  rule: only an explicit project override or a machine entry disables. The
  server reads it from the settings under the lock, without a copy.
- thread-cli-diff binds its one exception to the thread and run of the
  scenario's asked step, with the pending row before its terminal row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Moved from fix/conn-server-rejected-receipts (4bbd2ac, audit 1.6 and
1.7): after the store reopens, a refused id is refused again though its
precondition now holds, an accepted archive replays with its original
sequence after an undo and 4,400 later writes, and the same command under a
new id runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nckrtl added a commit that referenced this pull request Nov 23, 2026
…ls, overflow and message-less launches

- A thread a link below full access starts, and a delegate of one, runs no setup script
  (T3's linkOriginIsFenced, checked before the terminal opens).
- A cancel during the terminal's open waits for it, then closes that terminal.
- A delegate the engine would refuse is refused before its checkout and script.
- A sentinel the subscriber's overflowing queue dropped is read from the resync snapshot;
  an exited shell there ends the wait.
- A worktree launch with no first message ends its agent stage when it commits.
- Tests for shutdown and concurrent-retry cleanup of delegates, ignored until #262.

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

A launch wrote the new thread and its first message as two commands, so a
dropped socket between them left an empty thread. The gates then found
replays and claims that could land on the wrong thread, project or
checkout, or leave state behind. Each check now runs in the write that
commits, as T3's orchestrator does.

- thread.create carries its first message; thread, message and run commit
  together. A reused thread takes its message in a command of its own.
  Every separate first message checks the checkout its claim recorded,
  read from the claim's own event.
- A launch's own checks (reusable thread, project, checkout, mode limit)
  run at admission in the command's write. As in T3's ThreadLaunchService
  they record no receipt, so a refused launch id stays usable.
- Receipts (#260's owner columns) decide replays. A replayed launch must be
  the id's own thread.create; only an accepted message.dispatch of the
  thread with its run counts as a first message.
- A launch runs on a server-owned task. Shutdown refuses new launches,
  cancels worktree setup through the host, and gives admitted launches a
  10 s grace to commit or clean up in process, as T3 closes its
  preparation scope. A worktree a crash leaves stays on disk, as in T3.
- conn-vcs reports a worktree's claim before `git worktree add` and runs
  git in its own process group, so a cancel during `post-checkout` reaps
  the hook's process tree and removes the checkout and its branch. A
  discard removes the branch only once the worktree is gone, and says what
  stayed.
- Provider admission passes a launch already accepted and reads settings
  off the runtime thread; a thread.create with its first message checks
  the provider in its commit.
- The returned projection must be in the launch's project.
- thread.delete honours onlyIfUnused under the thread lock.
- A refused or replayed message releases only its own claim's copies.
- The .gitmodules check runs off the runtime thread.
- Test binaries stop git's repository search at the temp folder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nckrtl
nckrtl force-pushed the fix/conn-server-atomic-launch branch from 2b0b43a to 019fc24 Compare November 23, 2026 22:11
nckrtl and others added 3 commits November 23, 2026 23:40
…y, monograms count graphemes

- An unpinned scheduled dispatch sets followsThreadSelection, and the queue
  start recognises Conn's delegated-completion wakes by their id, so both run
  on the thread's current selection, as T3's followsCurrentConversation.
- project.create checks the folder before it plans, as T3's
  ProjectService.create: an offline folder another project holds is a
  retryable ProjectOperationError, not a recorded conflict.
- A monogram counts extended grapheme clusters, as T3's Intl.Segmenter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the thread, wakes reach their conversation

- thread.settle refuses a thread with a queued user message, as T3's
  settle; queued server, agent or scheduled messages do not block it.
- Starting a queued run on another selection moves the thread to it in the
  same transaction, as T3's startNextQueuedRun, so an unpinned scheduled run
  queued behind it starts on the same model.
- The runner recognises delegated-completion wakes with the queue's
  is_delegated_completion, so a fresh scheduled run's result reaches the
  conversation that delegated it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A settle refused a queued user message but left a queued scheduled, agent or
server run in place, so a later queue resume still started it. The settle
now cancels those runs before it writes thread.settled, as T3's settle
cancels its automatic cohort.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nckrtl and others added 10 commits November 24, 2026 00:52
…data matches ICU

Linux tzdata merges backzone local mean times (Amsterdam reads Brussels), so pre-1970 cases
differ from the TypeScript server's ICU values on the CI runner, not in this code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…heckout cleans up

A failing post-checkout hook made the launch force-remove the checkout and its
branch, losing the hook's files. Git and the TypeScript server keep it. The early
claim now serves only cancel cleanup; setup cleanup marks the checkout after
`git worktree add` succeeds.

A cancel while Git still populated the checkout left Git's `initializing` lock,
which `git worktree remove --force` refuses, so a partial checkout and its branch
stayed and blocked a retry. As in the TypeScript server, `worktree add` now locks
with a unique reason, and cancel cleanup unlocks the matching registration
before it removes it.

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

Git 2.34 (Ubuntu 22.04) takes `worktree add --lock --reason` but refuses
`worktree list -z`. The unlock read that list, so a checkout kept after a
failed hook stayed locked, and a cancel mid-checkout left the partial checkout
and its branch. The unlock now reads each registration's `locked` and `gitdir`
files, as the TypeScript server does, and unlocks only on an exact reason match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The direct vcs.createWorktree RPC locks the checkout during git worktree
add. When the client disconnects during a slow post-checkout hook, the
server aborts the handler and the unlock never ran, so the branch stayed
in use and force-remove refused the checkout.

create_worktree now holds a drop guard. If its future is dropped while
the checkout is claimed, a background task unlocks, removes the checkout
and deletes its new branch, as the TypeScript server does on interrupt.

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

# Conflicts:
#	apps/conn-server/crates/conn-engine/src/engine.rs
# Conflicts:
#	apps/conn-server/crates/conn-engine/src/runner.rs
#	apps/conn-server/crates/conn-engine/src/store.rs
…hor' into HEAD

# Conflicts:
#	apps/conn-server/crates/conn-engine/src/commands.rs
@nckrtl
nckrtl changed the base branch from conn-server/unsettle-restore-anchor to main November 24, 2026 04:15
# Conflicts:
#	apps/conn-server/crates/conn-engine/src/commands.rs
#	apps/conn-server/crates/conn-engine/src/store.rs
@nckrtl
nckrtl merged commit 0945361 into main Nov 24, 2026
47 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