Repository navigation
fix(core-ts): graph writes caught in a D1 move are settled, never lost or doubled (#324) - #347
Merged
Merged
Conversation
…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
force-pushed
the
claude/d1-handoff-writes-324
branch
from
November 29, 2026 10:37
118270e to
e3b8029
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).op_guardsrow exists, or a deleted node is goneConflictError, as for any concurrent changeThere 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.tsnow attach the aim each write already knows: id, new hash, and the revision it read.casBatchandcasRunsettle on that aim.runOpBatchsettles on itsop_guardsrows 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-dayop_guardssweep is an idempotent delete that the next operation repeats, so a transient failure there is skipped instead of failing the write.Coverage
Settled:
runUnique: content entries and pages without plans, glossary, site and taxonomy creates;OpBatchoperation (runOpBatch): entries with new terms or plans, pages with plans, taxonomy operations (merge, rename, retire, parent, …), redirects, the tags migration;sites.#run), including publish and rollback pointer flips with their search-index statements;snapshots.ts; applied when the row exists);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.
Tests
packages/core-ts/src/testing-handoff.ts(suite) andtest/handoff.test.tsrun on better-sqlite3 and Miniflare D1. AHandoffDriverfails the next write with D1's own error, either before it reaches the database or after it applied. Each case runs both ways:OpBatch), snapshot and publish, entry delete: each succeeds and applies exactly once;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
publishReleasedid not grow (the re-export goes throughnode-sql.ts).size-baseline.jsonis tightened for three entries that earlier PRs shrank.Checked locally
biome check --diagnostic-level=errorandpnpm size:check.tscof 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