Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Take the freeze's timer turn after the screen's filter-driver read, and count the entry's page reads in its stretch

garrytan85e9b2e57263
diff --git a/src/core/persistence/sync-run.ts b/src/core/persistence/sync-run.ts
index 81c2ff3d3d..d3ad21e4c2 100644
--- a/src/core/persistence/sync-run.ts
+++ b/src/core/persistence/sync-run.ts
@@ -365,15 +365,14 @@ async function freezeEntryOnce(engine: BrainEngine, cursor: Cursor, key: string,
if (!holdsOn) throw syncRunRefusal('page_identity_changed', 'Several pages claim the same imported origin.', retry, `Pages ${entry.originConflict.claimants.map(page => page.slug).join(', ')} of source ${cursor.sourceId} all record ${entry.path} as their origin (sync.holds=fail); ${sharedOriginExit(cursor.sourceId, entry.originConflict.claimants.map(page => page.slug))} Then sync again.`);
return { hold: originConflictHold(entry) };
}
+ // GBRA-133: once a stretch (from the entry's start, so its page reads count) has run its budget, the freeze takes a timer turn before and between its synchronous Git reads. A committed
+ // entry reads the pinned commit, or HEAD checked by hash against its first read (`headCommittedBytes`); a working-tree entry reads its file twice with no turn between. So work that runs at a turn changes nothing frozen, and the run is re-checked there.
+ const step = loopYield(), turn = async () => { if (!step.over()) return; await step.pause(); assertActive(); };
const [occupant, snapshot] = await Promise.all([alreadyImportedAtOrigin(engine, cursor, entry, originScope).catch(error => {
if (!holdsOn || !(error instanceof OperationError) || error.code !== 'page_identity_changed') throw error;
return { originFault: error };
}), engine.readPageSnapshot(entry.slug!, { sourceId: cursor.sourceId, includeDeleted: true })]);
assertActive();
- // GBRA-133: once a stretch has run its budget, the freeze takes a timer turn between its synchronous Git reads. A committed entry
- // reads the pinned commit, or HEAD checked by hash against its first read (`headCommittedBytes`); a working-tree entry reads its
- // file twice with no turn between. So work that runs at a turn changes nothing frozen, and the run is re-checked there.
- const step = loopYield(), turn = async () => { if (!step.over()) return; await step.pause(); assertActive(); };
if (occupant && 'originFault' in occupant) {
if (entry.action === 'delete' && snapshot?.page.deleted_at != null && !existsSync(join(cursor.root, entry.path))) {
const done = await idempotentDeletionProof(engine, { sourceId: cursor.sourceId, incarnation: cursor.incarnation, pageId: entry.pageId ?? null, revision: entry.revision ?? null, sourcePath: entry.sourcePath });
@@ -382,6 +381,7 @@ async function freezeEntryOnce(engine: BrainEngine, cursor: Cursor, key: string,
}
return { hold: await originFaultHold(engine, { sourceId: cursor.sourceId, incarnation: cursor.incarnation, entry, observed: snapshot, message: occupant.originFault.message }), overtaken: true };
}
+ await turn();
let bytes: Buffer | null = null;
// G3: a strict source's entry reads its commit alone (a deletion has no bytes); only a blob over the read bound takes the streamed path below.
const committed = entry.committedOnly === true && !entry.working ? entry.action === 'delete' ? null : pinnedBlob(cursor, entry.path) : undefined;
@@ -489,7 +489,7 @@ async function freezeEntryOnce(engine: BrainEngine, cursor: Cursor, key: string,
return { hold: fileDriftHold(entry, slug, pageId, frozenIntent, { sides: entry.drift, revision: snapshot.revision, proof, sharedOrigin: await sharedOriginSurvivor(engine, cursor.sourceId, entry.sourcePath), baseline: entry.driftBaseline ?? null }) };
}
if (screening && !image) {
- const hold = await screenFrozenImport(engine, { cursor, entry, slug, pageId, snapshot, content, rawHash, lineEndingOnly, blob, oversize, streamed: !!bodyRef,
+ const hold = await screenFrozenImport(engine, { cursor, entry, slug, pageId, snapshot, content, rawHash, lineEndingOnly, blob, oversize, streamed: !!bodyRef, turn,
retryCommand: checkpointRetryCommand(retry) }, screening);
assertActive();
if (hold) return { hold };
diff --git a/src/core/persistence/sync-screen.ts b/src/core/persistence/sync-screen.ts
index 2dd4e7967e..a6ef867210 100644
--- a/src/core/persistence/sync-screen.ts
+++ b/src/core/persistence/sync-screen.ts
@@ -335,6 +335,8 @@ export interface FrozenImportScreen {
retryCommand: string;
/** #6188 dry run: told the fixes when Tier 1 admits the file by rewriting a fence. */
onNormalized?: (fixes: FenceFix[]) => void;
+ /** GBRA-133: the freeze's timer turn, taken after the filter driver's Git read so the screen that follows starts a stretch of its own. */
+ turn?: () => Promise<void>;
}
/**
@@ -402,6 +404,7 @@ export async function screenFrozenImport(engine: BrainEngine, input: FrozenImpor
const filtered = filterOwnedRefusal(entry.path, content, entry.working || !gitRoot || !target || !entries ? null
: pinnedFilterDriver({ ...input.cursor, gitRoot, target, entries }, entry.path), input.cursor.sourceId, true);
if (filtered) return heldEntry(entry, slug, pageId, filtered, content, blob);
+ await input.turn?.();
// GBRA-133 S-39: text holding NUL cannot be stored or carried in the write intent; only this file is held. A slug conflict
// still names the hold (#6749): its message carries the path alone, and the slug-conflicts lane reports NULL_BYTES as still_invalid.
const nul = nullBytesRefusal(entry.path, content);
diff --git a/test/sync-freeze-turn-write-gbra133.test.ts b/test/sync-freeze-turn-write-gbra133.test.ts
index dc1be0b7e0..b5cf6f4cb5 100644
--- a/test/sync-freeze-turn-write-gbra133.test.ts
+++ b/test/sync-freeze-turn-write-gbra133.test.ts
@@ -7,7 +7,8 @@
*
* The fixture pins a first sync at the commit holding version one, then commits version two at the batch build, before
* the freeze: the working tree holds a commit past the pin, which the freeze imports as HEAD has it (#6340). Every timer
- * turn the freeze asks for is forced, and at the first one after the working-tree read the test commits version three.
+ * turn the freeze asks for is forced, and at the first one after the working-tree read the test commits version three. The
+ * last case commits it instead at the screen's turn after its filter-driver read, which the publication's byte check refuses.
*/
import { afterEach, describe, expect, spyOn, test } from 'bun:test';
import { createHash } from 'node:crypto';
@@ -18,6 +19,7 @@ import type { BrainEngine } from '../src/core/engine.ts';
import { installFaultHook } from '../src/core/persistence/fault-points.ts';
import * as syncDiscovery from '../src/core/persistence/sync-discovery.ts';
import * as syncPageFault from '../src/core/persistence/sync-page-fault.ts';
+import * as syncScreen from '../src/core/persistence/sync-screen.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
import * as syncYield from '../src/core/persistence/sync-yield.ts';
import { managedBrain } from './helpers/managed-brain.ts';
@@ -32,16 +34,17 @@ afterEach(() => installFaultHook(undefined));
const engines = [{ name: 'pglite', databaseUrl: undefined as string | undefined }, ...(process.env.DATABASE_URL ? [{ name: 'postgres', databaseUrl: process.env.DATABASE_URL }] : [])];
for (const { name, databaseUrl } of engines) describe(`GBRA-133: a write at a freeze's timer turn (${name})`, () => {
- /** Runs the first sync with every freeze turn forced; `interfere` commits version three at the first turn after the working-tree read. */
- const run = async (interfere: boolean) => {
+ /** Runs the first sync with every freeze turn forced; `interfere` commits version three at the first turn after the working-tree read
+ * (`freeze`), or at the screen's turn after its filter-driver read (`screen`). */
+ const run = async (interfere: false | 'freeze' | 'screen') => {
let result: { events: string[]; requests: { content: string | null; raw_hash: string | null; state: string }[]; page: string | null } | undefined;
await managedBrain(async ({ engine, root }) => {
const events: string[] = [];
- let readWorking = false, wrote = false;
+ let readWorking = false, readFilter = false, wrote = false;
installFaultHook(async point => {
if (point === 'sync:batch_entries' && !events.includes('v2')) { writeFileSync(join(root, 'a.md'), note('two')); commit(root, 'version two, past the pin'); events.push('v2'); }
});
- const { readSyncFile } = syncDiscovery, { headCommittedBytes } = syncPageFault, { loopYield } = syncYield;
+ const { readSyncFile } = syncDiscovery, { headCommittedBytes } = syncPageFault, { loopYield } = syncYield, { pinnedFilterDriver } = syncScreen;
const spies = [
spyOn(syncDiscovery, 'readSyncFile').mockImplementation(((root: string, path: string) => {
const bytes = readSyncFile(root, path);
@@ -52,10 +55,14 @@ for (const { name, databaseUrl } of engines) describe(`GBRA-133: a write at a fr
if (args[1] === 'a.md') events.push('head');
return headCommittedBytes(...args);
}) as typeof headCommittedBytes),
+ spyOn(syncScreen, 'pinnedFilterDriver').mockImplementation(((...args: Parameters<typeof pinnedFilterDriver>) => {
+ if (args[1] === 'a.md' && readWorking && !wrote) { readFilter = true; events.push('filter'); }
+ return pinnedFilterDriver(...args);
+ }) as typeof pinnedFilterDriver),
spyOn(syncYield, 'loopYield').mockImplementation(() => {
const step = loopYield();
return { ...step, over: () => true, pause: async () => {
- if (readWorking && !wrote) { events.push('pause'); if (interfere) { writeFileSync(join(root, 'a.md'), note('three')); commit(root, 'version three, at the freeze turn'); events.push('v3'); } wrote = true; }
+ if (readWorking && !wrote && (interfere !== 'screen' || readFilter)) { events.push('pause'); if (interfere) { writeFileSync(join(root, 'a.md'), note('three')); commit(root, 'version three, at the freeze turn'); events.push('v3'); } wrote = true; }
await step.pause();
} };
}),
@@ -79,7 +86,7 @@ for (const { name, databaseUrl } of engines) describe(`GBRA-133: a write at a fr
}, 120_000);
test('a commit of the same path at the turn keeps the pinned content, and only a later freeze imports the new bytes', async () => {
- const { events, requests, page } = await run(true);
+ const { events, requests, page } = await run('freeze');
expect(events.slice(0, 5)).toEqual(['v2', 'read', 'pause', 'v3', 'head']);
// The entry frozen across the turn carries the pinned version, never the bytes committed at the turn, and is not published.
expect(requests[0]).toMatchObject({ content: note('one'), raw_hash: sha256(note('two')) });
@@ -91,4 +98,19 @@ for (const { name, databaseUrl } of engines) describe(`GBRA-133: a write at a fr
expect(committed.at(-1)!.content).toBe(note('three'));
expect(page).toContain('version three');
}, 120_000);
+
+ test('a commit of the same path at the screen\'s turn after its filter-driver read is never published from the frozen request', async () => {
+ const { events, requests, page } = await run('screen');
+ // The write lands at the turn right after the screen's filter-driver read, once the freeze has read HEAD.
+ expect(events.indexOf('head')).toBeGreaterThan(events.indexOf('read'));
+ expect(events.indexOf('filter')).toBeGreaterThan(events.indexOf('head'));
+ expect(events.slice(events.indexOf('filter'), events.indexOf('filter') + 3)).toEqual(['filter', 'pause', 'v3']);
+ // The frozen request keeps the version the freeze read before the turn, with its own hash, and publication refuses it.
+ expect(requests[0]).toEqual({ content: note('two'), raw_hash: sha256(note('two')), state: 'conflict' });
+ const committed = requests.filter(request => request.state === 'committed');
+ expect(committed.length).toBeGreaterThan(0);
+ for (const request of committed) expect(request.raw_hash).toBe(sha256(request.content!));
+ expect(committed.at(-1)!.content).toBe(note('three'));
+ expect(page).toContain('version three');
+ }, 120_000);
});
diff --git a/test/sync-pass-batch-entries-6712.test.ts b/test/sync-pass-batch-entries-6712.test.ts
index c98c8d9fef..181095a9b9 100644
--- a/test/sync-pass-batch-entries-6712.test.ts
+++ b/test/sync-pass-batch-entries-6712.test.ts
@@ -129,7 +129,8 @@ describe('GBRA-133 PR 2b: a first sync builds its batch in linear time', () => {
// GBRA-133: the first freeze reads the file, its pinned blob and its pinned content one synchronous call after another
// (Git children for the pinned reads). Each read here costs FREEZE_READ_CPU_MS more of this thread's CPU, so a freeze
// that runs its reads as one stretch holds timers past the bound on any machine, and one that yields between them never
- // does. The filter driver and HEAD reads are wrapped too; this fixture (holds on, a clean checkout) reaches neither.
+ // does. The filter driver and HEAD reads are wrapped too; with holds on the filter driver is read by the screen after the freeze's
+ // last own turn, and this fixture (a clean checkout) never reaches the HEAD read.
const resolve = syncCore.resolveSlugForPath;
let slugs = 0, reads = 0;
const slugSpy = spyOn(syncCore, 'resolveSlugForPath').mockImplementation(((...args: Parameters<typeof resolve>) => {