Skip to content

CONTRACT: fix(ui-server): version a kept draft when an acceptance releases its snapshot (#3733) - #4068

Merged
schlessera merged 3 commits into
mainfrom
fix/3733-stale-snapshot-save
Nov 16, 2026
Merged

schlessera merged 3 commits into
mainfrom
fix/3733-stale-snapshot-save

Conversation

@schlessera

@schlessera schlessera commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Closes #3733

What changed

When an acceptance releases an outgoing snapshot and keeps the draft (a newer revision exists, or the draft changed chats), the draft now moves to a new revision. Before this, accept rewrote outgoing_snapshots in place and left the revision alone. A tab or device that read the draft before the acceptance could save its old list under the same If-Match, and nextSnapshots found no stored snapshot for the sent one, so it recreated it as a new dispatched snapshot. That phantom held one of the chat's ten slots and pointed its files back at outgoing. Now the stale save gets 409 DRAFT_CONFLICT with current, and current no longer lists the snapshot. The single-slot outgoing write follows the same rule, because it goes through the same If-Match.

Why this shape

The issue allowed two rules. This PR takes the revision bump instead of having nextSnapshots refuse a requestId found in session_draft_sends. That table can't answer the question: it records only first messages (!input.resumed), not sends into an existing chat, and those are the common snapshot case. It also keeps only the last eight sends per draft, while a chat can hold ten snapshots. The bump needs no new record, covers every acceptance path and gives the conflict the If-Match contract already promises. The consumed path and the ownership move already bumped the revision. The new keep() covers the remaining kept returns, and only when a snapshot was actually released.

The client already holds a draft's saves while one of its snapshot sends is out (#3758), so the sending tab never saves over its own accepted snapshot. Only a tab or device that was already behind gets the new 409. Today the client shows that 409 as an ordinary conflict choice. A silent reconcile when the only difference is a released snapshot is filed as #4061 (the client is out of scope here, per the issue).

Contract (additive within If-Match). A save after such an acceptance can newly get 409 DRAFT_CONFLICT. This is documented in docs/http-api.md#outgoing-snapshot-list-additive-0450-3588 and in the docs/integration-contract.md index entry, and it has a minor @schlessera/brain-ui-server changeset. The choice and the reasons are recorded in docs/decisions/chat-attachments.md under "What #3588 built". Two client comments and the client section of that record said the release happened "without a revision"; they are corrected. No client code changes.

Proof

bun run check:pr --base origin/main   # passed, receipt below
bun run test packages/ui-server/tests/draft-snapshots.test.ts packages/ui-server/tests/drafts-store.test.ts packages/ui-server/tests/drafts-http.test.ts packages/ui-server/tests/drafts-send.test.ts packages/ui-server/tests/attachment-ownership.test.ts packages/ui-react/tests/draft-snapshots.test.ts   # all pass

Failing first (new test a stale save cannot bring back a snapshot whose send the host accepted with the draft kept (#3733) in packages/ui-server/tests/draft-snapshots.test.ts, with store.ts at origin/main): it failed at refuse(...) with error: expected a DraftError. The stale save of the old list was accepted, which is what recreates the phantom.

Mutation: with keep()'s bump disabled (if (false && released && …)), the same test fails at the same point (expected a DraftError). Restored, it passes (11/11 in the file).

The test saves a dispatched snapshot with a file, saves a newer revision, and accepts the request with the draft kept. Then it saves the old list under the revision that tab holds. Afterwards:

  • the save is 409 DRAFT_CONFLICT, and its current is at a newer revision, lists only request-2 and keeps the newer text;
  • the single-slot outgoing write of the same request is also DRAFT_CONFLICT;
  • no snapshot names request-1, the chat's live count stays 1, the file has no outgoing ref and keeps its message ref;
  • a save from current.revision goes through.

attachment-ownership.test.ts asserted that a kept draft stays at revision 2 after the acceptance released its single-slot snapshot. That is the old behaviour, so the test now expects revision 3, with a comment explaining why.

Broader local run: every ui-server test file that mentions drafts or snapshots, outside the runtime tier (124 files): 2,026 pass and 85 fail. The 85 failures are in 13 files, none of them about drafts (hygiene-review, governance-policy, http-supported-mounts, private-index-cache, turn-scope, jobs-board, …). Run on an unmodified origin/main worktree at the merge base (86003b261), the same 13 files fail 87 times, and each file fails at least as often there, except one. That one is run-session.test.ts > an error result fails its turn…, which failed once under the load of the broad run and passes alone on this tree (16/16). Its wait is 200 one-millisecond ticks, so this is a timing budget, filed as #4066 with no milestone.

Collisions: bun scripts/collisions.ts --json --pr 4068 at head 40fb4f4b1. Two textual reports, both in docs: #3622 touches docs/http-api.md (its hunks are at lines 10–209 and 1809+, mine are at about 1180), and #4050 touches docs/integration-contract.md (its hunk is at line 20, mine at about 278). Neither overlaps these lines. Everything else is closure-only (the shared test graph), with no path collision on packages/ui-server/src/drafts/store.ts.

{"version":1,"kind":"brain-check-pr-receipt","head":"fdd25b495824b564526d43822d805b264641bec3","base":"origin/main","mode":"focused","startedAt":"2026-11-16T18:52:23.032Z","finishedAt":"2026-11-16T18:53:33.229Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":13057.064136},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":125.63348899999983},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":101.71474600000147},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":22.620653000001766},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":56873.580872000006}]}

Checklist

  • Focused local preflight/behavioral receipts and complete selected hosted proof passed; actual head/base/job checkout evidence is in Proof.
  • Acceptance criteria from the issue are all met, or the gap is named below.
  • The PR and completed/advanced issues have verified release milestones under docs/process/github.md.
  • A changeset is included, or this changes nothing a consumer can see.
  • No personal data anywhere in the diff — the leakage gate covers the whole tree.
  • The integration contract index or relevant component is updated in this same commit, or nothing in the machine surface moved.
  • Docs under docs/ reflect the change, or nothing documented moved.
  • A fail-closed guard added or changed answers the proportionate-containment checklist in this body, or no guard changed.
  • Provider history stays append-only (docs/decisions/provider-history.md): the provider-history guard passes, or nothing on a turn's path changed.

Anything left open

🤖 Generated with Claude Code

…eases its snapshot (#3733)

An acceptance that released a snapshot but kept the draft (a newer
revision exists, or the draft changed chats) rewrote outgoing_snapshots
without moving the revision. A tab or device that read the draft before
the acceptance could then save its old list under its If-Match, and the
sent snapshot came back as a new dispatched one: it held a slot of the
chat's ten and its files went back to outgoing.

The kept draft now moves to the next revision with the release, so that
save is 409 DRAFT_CONFLICT with current. The http-api snapshot rules and
the #3588 decision record state the rule and why it was chosen over
refusing requestIds found in session_draft_sends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera schlessera added this to the 0.45.0 milestone Nov 15, 2026
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. contract Touches the integration contract: CONTRACT: commit, contract doc updated in the same commit. labels Nov 15, 2026
This was referenced Nov 15, 2026
@schlessera
schlessera marked this pull request as ready for review November 16, 2026 19:34
@schlessera
schlessera merged commit 437ac87 into main Nov 16, 2026
50 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. contract Touches the integration contract: CONTRACT: commit, contract doc updated in the same commit. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ui-server: a stale draft save recreates an accepted outgoing snapshot

1 participant