Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(persistence): count a later write unless managed sync's submit_job wrote it, compacted or not; refuse a managed_sync_ capture kind
garrytanabd63d619ef4
diff --git a/src/core/persistence/page-mutations.ts b/src/core/persistence/page-mutations.tsindex ec9c30ca31..1ec79103d5 100644--- a/src/core/persistence/page-mutations.ts+++ b/src/core/persistence/page-mutations.ts@@ -362,6 +362,11 @@ export async function preparePageAdmission(ctx: OperationContext,'Drop kind, preview and backup_reference from put_page; reconciling a canonical file runs through gbrain sources reconcile on the brain host.');}}+ // #8765 review: a managed_sync_ kind marks managed sync's own rows, so no page write may carry one (a capture's event kind).+ if (typeof input.params.kind === 'string' && input.params.kind.startsWith('managed_sync_')) {+ throw new OperationError('invalid_params', `kind ${input.params.kind} is reserved for managed sync, so nothing was written.`,+ 'Use another kind, or drop kind.');+ }const bodyRefKind = (input.params.body_ref as { kind?: unknown } | null | undefined)?.kind;const staged = input.stagedBody === true && ['put_page', 'append_page'].includes(input.operation) && bodyRefKind === 'staging';const imported = input.managedFileImport === true && input.operation === 'put_page' && bodyRefKind === 'input_file';diff --git a/src/core/persistence/refresh-barrier.ts b/src/core/persistence/refresh-barrier.tsindex 53d370497d..38e778eecd 100644--- a/src/core/persistence/refresh-barrier.ts+++ b/src/core/persistence/refresh-barrier.ts@@ -59,12 +59,14 @@ const APPENDING_OPERATIONS = ['remember', 'add_timeline_entry'];* GST-1623: the refusal of deferred write `row` when a write to its page accepted after it has already committed (an older* consumer, which has no EARLIER_DEFERRED in its claim, claimed the later write first), so the deferred one, replayed now,* never lands on top of it. Managed sync's own rows are the import the write waited for and do not count; neither does a- * write that only appends.+ * write that only appends. A sync row is a `submit_job` with a managed_sync_ kind, or with an imported file once compaction+ * dropped its intent (#8765 review): a caller cannot make its own write look like one.*/async function laterWriteRefusal(engine: SqlEngine, row: WriteRequest): Promise<DeferredRefusal | null> {if (APPENDING_OPERATIONS.includes(row.operation)) return null;const [later] = await engine.executeRaw<{ request_id: string }>(`SELECT r.request_id::text AS request_id FROM persistence_requests r- WHERE r.source_id=$1 AND r.slug=$2 AND r.sequence>$3 AND r.state='committed' AND NOT ${SYNC_KIND('r')} ORDER BY r.sequence LIMIT 1`,+ WHERE r.source_id=$1 AND r.slug=$2 AND r.sequence>$3 AND r.state='committed' AND NOT (r.operation='submit_job'+ AND (${SYNC_KIND('r')} OR (r.intent IS NULL AND r.outcome->>'imported_file'='true'))) ORDER BY r.sequence LIMIT 1`,[row.source_id, row.slug, String(row.sequence)]);if (!later) return null;return { state: 'conflict', failure: { code: 'revision_conflict', message: `Request ${row.request_id} waited ${row.deferred_refresh_id ? `behind refresh ${row.deferred_refresh_id}` : 'for an import'}, and request ${later.request_id}, accepted after it, has already changed ${row.slug}, so nothing was applied and the later write was not overwritten. Read the page again and submit the change with a new request_id.` } };diff --git a/test/refresh-barrier-gst1623.test.ts b/test/refresh-barrier-gst1623.test.tsindex 69b8b0d859..438ea20761 100644--- a/test/refresh-barrier-gst1623.test.ts+++ b/test/refresh-barrier-gst1623.test.ts@@ -222,6 +222,37 @@ test('item 1: a deferred write never lands on top of a later write to its page texpect(await deferredReplayRefusal(f.engine, { ...imported, operation: 'remember' } as WriteRequest, [])).toBeNull();}), 180_000);+test('#8765 outside voice: a later write counts unless managed sync wrote it (submit_job), its receipt compacted or not; capture refuses a managed_sync_ kind', () => each(async f => {+ let deferred!: WriteRequest;+ const refresh = await duringRefresh(f, async () => {+ const out = await local(f, 'put_page', { slug: 'one', content: page('Alpha one', 'Deferred, accepted first.') });+ deferred = await requestRow(f, out.receipt.request_id);+ expect(deferred.state).toBe('deferred');+ await disposePersistenceConsumer(f.engine);+ });+ expect((await refresh).status).toBe('completed');+ const later = await requestRow(f, (await local(f, 'put_page', { slug: 'one', content: page('Alpha one', 'Accepted second.') })).receipt.request_id);+ expect(later.state).toBe('committed');+ const held = { ...deferred, deferred_refresh_id: null, deferred_import_from: '' } as WriteRequest;+ const refusal = () => deferredReplayRefusal(f.engine, held, []);+ // A foreground row whose stored intent names a managed_sync_ kind (a caller-chosen value) is still a later write.+ await f.engine.executeRaw(`UPDATE persistence_requests SET intent=intent||'{"kind":"managed_sync_import"}'::jsonb WHERE id=$1::uuid`, [later.id]);+ expect(await refusal()).toMatchObject({ state: 'conflict', failure: { code: 'revision_conflict' } });+ // Managed sync's own import is not, before or after receipt compaction drops its intent.+ await f.engine.executeRaw(`UPDATE persistence_requests SET operation='submit_job', outcome=COALESCE(outcome,'{}'::jsonb)||'{"imported_file":true}'::jsonb WHERE id=$1::uuid`, [later.id]);+ expect(await refusal()).toBeNull();+ await f.engine.executeRaw('UPDATE persistence_requests SET intent=NULL, compacted=true WHERE id=$1::uuid', [later.id]);+ expect(await refusal()).toBeNull();+ // A compacted foreground write still counts.+ await f.engine.executeRaw(`UPDATE persistence_requests SET operation='put_page', outcome=outcome-'imported_file' WHERE id=$1::uuid`, [later.id]);+ expect(await refusal()).toMatchObject({ state: 'conflict', failure: { code: 'revision_conflict' } });+ const before = (await f.engine.readPageSnapshot('two', { sourceId: f.alpha }))?.revision;+ const captured = await local(f, 'capture', { slug: 'two', content: 'An event.', kind: 'managed_sync_import' }).catch(error => error);+ expect(captured).toBeInstanceOf(OperationError);+ expect((captured as OperationError).code).toBe('invalid_params');+ expect((await f.engine.readPageSnapshot('two', { sourceId: f.alpha }))?.revision).toBe(before);+}), 180_000);+test('item 16: a write deferred behind a refresh is held for an import without its released refresh, unless the refresh changed its file', () => each(async f => {f.push('alpha/three.md', page('Alpha three', 'Before the refresh.'));expect((await refreshWorktree(f.engine, f.alpha)).status).toBe('completed');