Skip to content

fix(core-ts): graph writes caught in a D1 move are settled, never lost or doubled (#324) - #347

Merged
Madraka merged 2 commits into
mainfrom
claude/d1-handoff-writes-324
Nov 29, 2026
Merged

Madraka merged 2 commits into
mainfrom
claude/d1-handoff-writes-324

Conversation

@Madraka

@Madraka Madraka commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Graph writes caught in a D1 move are now settled: none is lost or applied twice. The cluster failover test found the case (#290, PR #322).

Design (as specified)

On isTransientD1Error (now exported from @cloudhal/storage), a graph write reads its aim back once (packages/core-ts/src/cas-settle.ts).

Read-back Meaning What the writer does
The node has the write's new hash, or the operation's op_guards row exists, or a deleted node is gone applied reports what a successful batch reports
The node is still at the revision it read, or still absent for an insert lost runs the same statements once more; a second failure surfaces as it is
Anything else moved reports a CAS miss, so the caller raises its ConflictError, as for any concurrent change

There are no transactions (rule 4): one read after the error, and at most one retry. SQLite drivers never raise a transient error, so nothing changes on Node.

One helper at the write path. The node statement builders in node-sql.ts now attach the aim each write already knows: id, new hash, and the revision it read. casBatch and casRun settle on that aim. runOpBatch settles on its op_guards rows instead, which is stronger than one node's hash: batch() is atomic, so the row proves the whole multi-node operation applied. If those rows are absent, it compares the revisions it planned on. Its 30-day op_guards sweep is an idempotent delete that the next operation repeats, so a transient failure there is skipped instead of failing the write.

Coverage

Settled:

  • single-node CAS writes through runUnique: content entries and pages without plans, glossary, site and taxonomy creates;
  • term updates;
  • every OpBatch operation (runOpBatch): entries with new terms or plans, pages with plans, taxonomy operations (merge, rename, retire, parent, …), redirects, the tags migration;
  • site updates (sites.#run), including publish and rollback pointer flips with their search-index statements;
  • entry and page deletes. A deleted node counts as applied even if someone else deleted it, since the end state is the same;
  • snapshots (snapshots.ts; applied when the row exists);
  • scheduled release publishes (release-apply.ts, settled on the pointer flip's aim), so a release run is still published exactly once.

Not covered: these fail once, as before.

  • Non-graph rows: audit, Better Auth sessions and accounts, rate limits, approvals, schedule rows (runs, leases, heartbeats), comments, analytics.
  • Resumable bulk and background jobs: import/sync, list-value backfill, references rebuild, field-operation rewrites, tag-migration restores, media upload tickets.
  • The search refresh outside a publish batch, an idempotent rebuild.

Tests

  • packages/core-ts/src/testing-handoff.ts (suite) and test/handoff.test.ts run on better-sqlite3 and Miniflare D1. A HandoffDriver fails the next write with D1's own error, either before it reaches the database or after it applied. Each case runs both ways:
    • entry update, redirect create (OpBatch), snapshot and publish, entry delete: each succeeds and applies exactly once;
    • a lost write while another writer moved the node is a ConflictError, and so is a lost site update while someone else renamed the site.
  • scripts/failover-smoke.ts: both SIGTERM handoffs now also send draft edits of the starter post through a surviving instance (GET, then PUT, every 250 ms, through the whole handoff window). Not one may fail, and the last successful edit must be the one stored. The live post keeps its released title, so the read checks are unchanged.

Docs

  • RFC-0047 §7: "Writes during a handoff" (amended).
  • Self-host guide, "Stopping": content writes are settled; other writes may fail once.
  • Changeset: core-ts and storage, patch.
  • Size: store.ts, taxonomy.ts and publishRelease did not grow (the re-export goes through node-sql.ts). size-baseline.json is tightened for three entries that earlier PRs shrank.

Checked locally

  • biome check --diagnostic-level=error and pnpm size:check.
  • tsc of storage, core-ts, cloudflare, api and cli, plus the failover script.
  • vitest run test/handoff.test.ts: 20 passed, 10 on SQLite and 10 on Miniflare D1.

The full store suites, and the failover smoke with writes, run in CI.

Closes #324

🤖 Generated with Claude Code

Madraka and others added 2 commits November 29, 2026 13:36
…t or doubled (#324)

When D1's object moves (a graceful handoff in a self-host cluster, a deploy
or an eviction on Cloudflare), a write on its way gets a transient error and
may or may not have applied. Every graph write is a CAS on content hashes,
so after isTransientD1Error the writer reads its aim back (cas-settle.ts):

- the new hash is stored, or the operation's op_guards row exists: it
  applied; report what a successful batch reports;
- the node is still at the revision it read (or absent, for an insert): it
  was lost; run the same statements once more;
- anything else: a CAS miss, so the caller's ConflictError, as today.

The node statement builders attach that aim (id, new hash, revision read);
casBatch and casRun settle on it; runOpBatch settles on its op_guards rows
and the revisions it planned on, and skips a failed op_guards sweep (an
idempotent delete the next operation repeats). No transactions, one read and
at most one retry.

Covered: single-node CAS writes (runUnique: entries, pages, glossary, site
and taxonomy creates), term updates, every OpBatch operation (entries and
pages with plans, taxonomy operations, redirects, tags migration), site
updates and publish/rollback pointer flips, entry and page deletes,
snapshots, and scheduled release publishes. Not covered, failing once as
before: audit, auth, rate limits, approvals, schedule and comment rows, and
the resumable bulk jobs (import/sync, backfills, rebuilds, field operations,
upload tickets).

Tests: testing-handoff.ts drops the next write before or after it applies
on better-sqlite3 and Miniflare D1 (test/handoff.test.ts); the failover
smoke now sends draft edits through both SIGTERM handoffs and requires none
to fail and the last to be stored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
The PR's earlier tightening of createAdminWorker and its fetch to this
branch's sizes failed once main grew them by a line (#346): main's own
baseline allows that. Only createApi, which shrank, is tightened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
@Madraka
Madraka force-pushed the claude/d1-handoff-writes-324 branch from 118270e to e3b8029 Compare November 29, 2026 10:37
@Madraka
Madraka merged commit 36a2f21 into main Nov 29, 2026
12 checks passed
@Madraka
Madraka deleted the claude/d1-handoff-writes-324 branch November 29, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Writes during a D1 handoff fail once: retry CAS-anchored writes safely by reading the anchor back

1 participant