feat(sql-fs): fence stale sandbox writers with durable epochs#161
Draft
Hazzng wants to merge 5 commits into
Draft
feat(sql-fs): fence stale sandbox writers with durable epochs#161Hazzng wants to merge 5 commits into
Hazzng wants to merge 5 commits into
Conversation
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.
Trek Voyage
c62365e4ef1cTicket: 131
Base ref:
mainCheckpoint commit:
5d3d3bd4ed1f11d475bdd61fcc18b28ddf4aaf14Repair rounds: 1
Change summary
Prevent stale script transactions from mutating a sandbox after its lifecycle epoch has advanced, including after deletion and recreation with the same ID. The change persists sandbox epochs in Postgres, pins an epoch when a script transaction opens, and conditionally advances that epoch inside each advisory-locked composite mutation so stale writes fail and roll back.
What changed & why
Walkthrough
On migration, each sandbox receives a live version and each sandbox ID gains durable epoch storage that survives deletion. Creating or recreating a sandbox takes the per-ID advisory lock and returns the allocated epoch; deleting it records a newer tombstone epoch. When SqlFs lazily opens a script transaction, it sets the RLS sandbox context, acquires the advisory lock, and pins the current epoch. Every composite mutation receives that pinned value: its SQL re-establishes the local context and lock, verifies the expected or transaction-local epoch against the live sandbox row, advances the version, and only then performs the filesystem change. If a stale transaction attempts its first mutation after a replacement has committed, the fenced CTE produces no eligible row, the mutation rejects, and the transaction rolls back without changing the replacement sandbox.
sequenceDiagram participant A as Stale scope participant DB as Postgres participant B as Live writer A->>DB: Read epoch zero B->>DB: Delete and recreate sandbox B->>DB: Write with live epoch DB-->>B: Commit and advance epoch A->>DB: Composite write with epoch zero DB->>DB: Lock and validate epoch DB-->>A: Reject with no rows A->>DB: Roll back transactionClaimed assertions
Passed assertions (evidence)
A1-migration-and-initial-epoch/workspace/artifacts/logs/cmd_6a2b2506-7a0c-44d5-a0e7-28f87967cd9d.outA3-zombie-commit-rejected/workspace/artifacts/logs/cmd_17e70f9f-17d8-4b48-a9a5-a6fc1b56ab61.outA4-rls-trusted-transaction-boundary/workspace/artifacts/logs/cmd_4c4b287c-1e2b-4df0-ab8b-87384c8403e2.outA6-lifecycle-nonreuse/workspace/artifacts/logs/cmd_1bc15bf7-2b1a-41d6-be7f-77f0045ffe40.outADV-447743e5sandbox://cmd/e7a250b8-59bc-408d-9531-2dc418b6d9faADV-d7e431e4/workspace/artifacts/logs/cmd_b8af0250-4eb6-42e2-b12e-ebbd9f317a18.outFiles changed
src/sql-fs/migrations/postgres/0007_fence_sandbox_epochs.sqlsrc/api/tests/integration/migrations.integration.test.tssrc/sql-fs/types.tssrc/sql-fs/dialects/postgres.tssrc/sql-fs/tests/sql-fs.composite.test.tssrc/sql-fs/tests/sql-fs.script-tx.test.tssrc/sql-fs/sql-fs.tssrc/sql-fs/tests/integration/fencing.integration.test.tsChecks executed
bash /workspace/state/setup.shpnpm exec vitest run src/sql-fs/tests/integration/fencing.integration.test.tspnpm exec biome check src/sql-fs/tests/integration/fencing.integration.test.tspnpm exec tsc --noEmitpnpm exec vitest run src/sql-fs/tests/integration/fencing.integration.test.ts -t "rejects a stale first mutation after a committed live append"git diff --checkKnown limitations
none
Live E2E smoke test (advisory)
Verdict:
error(advisory — never blocks the PR)live smoke could not run (infrastructure): docker compose up failed (exit 1): Network trek-smoke-296b8815-e9d3-435c-90ec-8a1d5d0e563e_default Creating
Network trek-smoke-296b8815-e9d3-435c-90ec-8a1d5d0e563e_default Created
Volume trek-smoke-296b8815-e9d3-435c-90ec-8a1d5d0e563e_sqlfs-pgdata Creating
Volume trek-smoke-296b8815-e9d3-435c-90ec-8a1d5d0e563e_sqlfs-pgdata Created
Container trek-smoke-296b8815-e9d3-435c-90ec-8a1d5d0e563e-redis-1 Creating
Container trek-smoke-296b8815-e9d3-435c
Unresolved non-blocking findings
none
Rollback
To revert this change:
git revertthe commits onvoyage/c62365e4ef1c, ordelete the branch — no merge has occurred (draft PR only).
Summary by cubic
Fences stale sandbox writers using durable epochs in Postgres so old script transactions cannot mutate a sandbox after delete/recreate or concurrent writes. Script scopes pin the current epoch, and all composite mutations verify and advance it atomically.
New Features
sandboxes.versionand retain per-ID tombstones insandbox_epochs; serialize create/delete with advisory locks and advance epochs across recreation.mkdir,rm,writeFile,mv) validate the expected epoch inside advisory-locked CTEs, increment on success, and roll back on mismatch.getSandboxEpoch,createSandboxreturns{ epoch }, anddeleteSandboxreturns the advanced epoch;SqlFstracks last-known epoch to detect stale scopes.Migration
0007_fence_sandbox_epochs.sql(sandboxes.version BIGINT NOT NULL DEFAULT 0;sandbox_epochstombstone table). Run migrations to adopt.Written for commit 5d3d3bd. Summary will update on new commits.