Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(persistence): a segmented file whose page moved since its snapshot refuses revision_conflict, so the write prepares again (GBRA-287 fix-check)
garrytan49eafc36515e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1b592dd810..3a084c7a14 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -27,7 +27,7 @@ A full sync's DB-only reconcile (#2426) now re-exports a page stored in segments- `src/core/cycle/derived-write-through.ts` (`writeDerivedFile`): a generated page stored in segments is skipped (`segmented_page`, `writePageThrough`'s `inlineOnly`), since the batch rollback compares file strings.- `src/commands/sync/full.ts`: the DB-only re-export no longer refuses segmented pages, and each failed re-export is returned in `reexport_failures` (slug, path, code, message, fix).- `src/core/page-body/body-tiling.ts` (`readTiledManifest`), `ingest.ts`, `read-range.ts`, `projection.ts`, `segment-rewrite.ts`: a live read names its generation first and checks that generation's rows against that generation's size; a live read whose generation a publish retired meanwhile reads the new live generation (up to 3 attempts), and is checked against the body_bytes read with the generation (`assertGenerationTiles`'s `bodyBytes`), never against a missing size.-- `src/core/export-snapshot.ts` (`segmentedExportMarkdown`), `page-prepare.ts` (`unreadPageFile`): the body is opened pinned to the snapshot's revision, so a publish after the snapshot refuses the file instead of mixing revisions.+- `src/core/export-snapshot.ts` (`segmentedExportMarkdown`), `page-prepare.ts` (`unreadPageFile`): the body is opened pinned to the snapshot's revision, so a publish after the snapshot refuses the file `revision_conflict` (the code a changed staging hash gives, so a tag write or restore prepares again or ends `conflict`) instead of mixing revisions.- `docs/guides/sync-unblock-runbook.md`: the two `freeze_*` fault-table rows.- Tests: `test/large-pages/managed-sync-segmented-freeze.test.ts` (the live-checkout freeze, with its inline twin, and a page whose rows don't tile held while the rest of the commit imports), `test/sync-reconcile-db-only-segmented.test.ts` (2 MiB and over-24 MiB re-exports byte-equal to `gbrain export`, one failing page listed while the run advances, a plain-directory source, a write inside a transaction keeping the file's mode, frontmatter overrides refused, a file over the scan cap left unbound, the derived skip), `test/segmented-fault-classes-gbra287.test.ts` (hold and receipt classes), `test/segmented-read-concurrent-publish.test.ts` (a forced publish between a reader's statements, a retired generation leaving only a shared prefix, a publish between a snapshot and its streamed file), `test/pathological/d15-sync-reexport-overwrite.test.ts` (a segmented DB-only page is re-exported, not held). Every case but the plain-directory one fails on master.diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.tsindex c98cdd7627..006dc43c2b 100644--- a/src/core/persistence/page-prepare.ts+++ b/src/core/persistence/page-prepare.ts@@ -31,7 +31,7 @@ import { ContentSanityBlockError } from '../content-sanity.ts';import { assertPageRevision, type PageSnapshot } from '../page-state/types.ts';import { GIT_HOLD_OP, gitHoldFingerprint, gitHoldFix, readGitHold } from './sync-holds.ts';import { hostOperatorFix, recordRoute } from './held-reads.ts';-import { heldFileMessage, sourceCheckoutMissingRefusal } from './verb-errors.ts';+import { heldFileMessage, PAGE_CHANGED_DURING_PREPARATION_MESSAGE, sourceCheckoutMissingRefusal } from './verb-errors.ts';import { isWriteTargetContained } from '../path-confine.ts';import { recordedPathFromFileUri, scannerSlugRootMode, scannerSourcePath } from '../write-through.ts';import { engineMutationPrecondition, parseMutationPrecondition } from './preconditions.ts';@@ -675,12 +675,18 @@ async function prepareUnreadRestore(engine: BrainEngine, row: WriteRequest, snap* serializePageToMarkdown's bytes for a page whose body was not read back, streamed from its segments by export's* serializer: measured here, produced again into the publication's staging file (large-file.ts), never concatenated.* A withdrawal mirror of a segmented page (effects.ts, effect-recovery.ts) renders its file the same way, with- * `between` (its claim renewal) run after each piece each time the file is produced.+ * `between` (its claim renewal) run after each piece each time the file is produced. The body is read at the snapshot's+ * revision; a page that moved since refuses `revision_conflict`, as a changed staging hash does, so the request prepares again.*/export async function unreadPageFile(engine: BrainEngine, snapshot: PageSnapshot, between?: () => Promise<void>): Promise<StreamedFileContent> {const head = serializePageToMarkdown({ ...withoutBody(snapshot.page), compiled_truth: '', timeline: '' }, snapshot.tags).slice(0, -1);const ledger = snapshotLedger(snapshot);- const produce = (exec: BrainEngine) => segmentedExportMarkdown(exec, snapshot.page, ledger, head, snapshot.revision);+ const produce = async function* (exec: BrainEngine) {+ try { yield* segmentedExportMarkdown(exec, snapshot.page, ledger, head, snapshot.revision); } catch (error) {+ if (!(error instanceof OperationError) || error.canonical !== 'revision_changed') throw error;+ throw new OperationError('revision_conflict', PAGE_CHANGED_DURING_PREPARATION_MESSAGE, 'Read its current revision and submit the updated intent with a new request_id.');+ }+ };return StreamedFileContent.measure(engine, !between ? produce : async function* (exec) { for await (const piece of produce(exec)) { yield piece; await between(); } });}diff --git a/test/segmented-read-concurrent-publish.test.ts b/test/segmented-read-concurrent-publish.test.tsindex f5cda77165..734a05c463 100644--- a/test/segmented-read-concurrent-publish.test.ts+++ b/test/segmented-read-concurrent-publish.test.ts@@ -147,6 +147,9 @@ describe('GBRA-287: segmented reads across a concurrent publish', () => {const snapshot = await engine.readPageSnapshot(SLUG, { sourceId: 'default' });await publish('b', 2 * 1024 * 1024, 'b', 'Live Example Renamed');const { unreadPageFile } = await import('../src/core/persistence/page-prepare.ts');- await expect(unreadPageFile(engine, snapshot!)).rejects.toThrow('changed after this read was authorized');+ const { mayReprepare } = await import('../src/core/persistence/semantic.ts');+ const refused = await unreadPageFile(engine, snapshot!).then(() => null, (error: { code?: string }) => error);+ expect(refused).toMatchObject({ code: 'revision_conflict' });+ expect(mayReprepare({ operation: 'add_tag', intent: {} } as never, refused!)).toBe(true);}, 120_000);});