Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
#6712 PR 4b lane F: a folder moved with git mv stays a move of each page past the mass-delete valve (GBRA-133 rename storm)
garrytanf9785980c917
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.tsindex 6090eb5735..0905505fbb 100644--- a/src/core/behavior-change-notice.ts+++ b/src/core/behavior-change-notice.ts@@ -936,6 +936,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;{ since: '0.60.300.0', text: 'Doctor `managed_sync_blocked` (and get_brain_health `sync_blocked`) now count a managed sync pass that stopped with a recorded failure before finishing, in discovery (path `<discovery>`) or on one file, until a later sync of the source succeeds; before, such a source indexed nothing while the check said no run was blocked.' },{ since: '0.60.300.0', text: 'A managed sync no longer stops a whole source when a file it would skip as already imported (a no-op waiver) has meanwhile become a symlink, a FIFO or an unreadable file in the working tree: that entry is simply not skipped and goes on like any other, where an unreadable file is held `file_unreadable`.' },{ since: '0.60.300.0', text: 'A managed sync holds a committed file whose name is not valid UTF-8 (a Latin-1 name from an older system or an archive) under the new hold code `path_not_utf8`, naming the rename that fixes it, and the rest of the source syncs; before, the file was held with a raw Git error or stopped the source. Renaming the file to a UTF-8 name clears the hold. With `sync.holds=fail` the run stops with `path_not_utf8`.' },+ { since: '0.60.300.0', text: 'A managed sync of a folder of more than 20 pages moved with `git mv` keeps each page (a move, with its id, links and history): the mass-delete valve no longer counts a delete whose bytes arrive at a new path in the same round. Before, the valve withheld the old paths\' deletes in the first round, so every new path imported as a new page beside its old one (one `shares content_hash ... Indexing both` warning per file) and the old pages were deleted afterwards. A mass delete with nothing arriving is still withheld.' },];/** The newest disclosed change's release: the notice id moves only when a release adds rows. */diff --git a/src/core/persistence/sync-pass-apply.ts b/src/core/persistence/sync-pass-apply.tsindex a8206a8812..3954345f50 100644--- a/src/core/persistence/sync-pass-apply.ts+++ b/src/core/persistence/sync-pass-apply.ts@@ -214,9 +214,23 @@ async function readStoredMarker(exec: Pick<BrainEngine, 'executeRaw'>, level: Shreturn await readOriginDeletedMarker(exec, level.sourceId, level.incarnation, origin) as { kind?: string; blob_oids?: string[]; slug?: string } | null;}-/** Design §2.6: the round's deletes through #2828's valve; a trip withholds them all under one source-level record. */+/**+ * Design §2.6: the round's deletes through #2828's valve; a trip withholds them all under one source-level record.+ * GBRA-133 rename storm: a delete whose recorded bytes arrive at a new path in the same round is a rename (batchEntries pairs+ * it), not a deletion, so it is neither counted nor withheld; a whole folder moved with git mv stays a move of each page.+ */async function applyDeleteValve(engine: BrainEngine, level: ShadowLevel, input: PassApplyInput, state: PassApplyState): Promise<void> {- const deletes = await filterYielding(level.paths, entry => entry.result.action === 'delete' && !entry.result.settled && !entry.result.inFlight);+ const arriving = new Map<string, number>();+ for (const entry of await filterYielding(level.paths, entry => entry.result.action === 'import' && !entry.state.page && !!entry.state.level)) {+ arriving.set(entry.state.level!, (arriving.get(entry.state.level!) ?? 0) + 1);+ }+ const moving = (entry: ShadowPath) => {+ const oid = entry.state.record?.blobOids.find(blob => (arriving.get(blob) ?? 0) > 0);+ if (oid === undefined) return false;+ arriving.set(oid, arriving.get(oid)! - 1);+ return true;+ };+ const deletes = await filterYielding(level.paths, entry => entry.result.action === 'delete' && !entry.result.settled && !entry.result.inFlight && !moving(entry));// The valve plans over every live page only when the round deletes something (no delete never trips it).const plan = deletes.length ? await (async () => {const deleting = new Set(deletes.map(entry => entry.path));diff --git a/test/behavior-change-notice-6712-block.test.ts b/test/behavior-change-notice-6712-block.test.tsindex e4fba69793..c617337892 100644--- a/test/behavior-change-notice-6712-block.test.ts+++ b/test/behavior-change-notice-6712-block.test.ts@@ -12,7 +12,7 @@ import { join } from 'node:path';import { BEHAVIOR_CHANGES, compareReleases } from '../src/core/behavior-change-notice.ts';const MARKER = '// #6712 PR 4b (the level-triggered sync pass';-const ROWS = 32;+const ROWS = 33;const NOTICE_CAP = 2_000;/** The array literal's row lines in order, and where the block's rows start and how many there are. */diff --git a/test/sync-entry-faults-6712.test.ts b/test/sync-entry-faults-6712.test.tsindex bef4002c5d..8cefca16ec 100644--- a/test/sync-entry-faults-6712.test.ts+++ b/test/sync-entry-faults-6712.test.ts@@ -24,7 +24,10 @@* `.code`; it now reaches the caller and the failure record as itself (red with the `sync:batch_entries` seam alone).* - S-7: a committed file whose name is not valid UTF-8 (a Latin-1 byte) was held `page_write_failed` with a Git error (and* stopped the source before this lane); it is held `path_not_utf8` naming the rename, which clears the hold.- * The deliberate-refusal, systemic and changing-message guards pass before and after.+ * - Rename storm: a folder of more than 20 pages moved with git mv tripped the mass-delete valve, which withheld the+ * old paths' deletes in the first round, so each new path imported as a new page beside its old one ("shares+ * content_hash ... Indexing both") and the pages lost their ids; each is now a move that keeps its page.+ * The deliberate-refusal, systemic, changing-message and mass-delete guards pass before and after.** PGLite here; Postgres through test/e2e/sync-entry-faults-6712-postgres.test.ts.*/@@ -254,4 +257,43 @@ for (const backend of testBackends()) describe(`[${backend}] GBRA-133: one odd fwriteFileSync(latin1(root), note('notes/cafe')); commit(root, 'odd');await expect(performManagedSync(engine, OPTS)).rejects.toMatchObject({ code: 'path_not_utf8' });}), 240_000);+ const ids = async (engine: BrainEngine, prefix: string) => (await engine.executeRaw<{ id: number | string; slug: string }>(+ "SELECT id, slug FROM pages WHERE source_id='default' AND deleted_at IS NULL AND slug LIKE $1 ORDER BY slug", [`${prefix}/%`])).map(row => [Number(row.id), row.slug.slice(prefix.length + 1)]);+ const folder = (root: string, name: string, n: number, body = (i: number) => `# Note ${i}\n\nSynthetic body number ${i}.\n`) => {+ mkdirSync(join(root, name), { recursive: true });+ for (let i = 0; i < n; i++) writeFileSync(join(root, `${name}/n${i}.md`), body(i));+ };+ test('rename storm: a folder of 30 pages moved with git mv keeps every page and indexes nothing twice', () => brain(async ({ engine, root }) => {+ folder(root, 'bulk', 30); commit(root, 'bulk');+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ const before = await ids(engine, 'bulk');+ git(root, 'mv', 'bulk', 'moved'); commit(root, 'mv');+ const warned: string[] = [];+ const write = process.stderr.write.bind(process.stderr);+ process.stderr.write = ((chunk: string | Uint8Array, ...rest: never[]) => { warned.push(String(chunk)); return write(chunk, ...rest); }) as typeof process.stderr.write;+ const moved = await performManagedSync(engine, OPTS).finally(() => { process.stderr.write = write; });+ expect(moved).toMatchObject({ status: 'synced', renamed: 30, added: 0, deleted: 0 });+ expect(warned.filter(line => line.includes('shares content_hash'))).toEqual([]);+ expect(await ids(engine, 'moved')).toEqual(before);+ expect(await ids(engine, 'bulk')).toEqual([]);+ expect(await atHead(engine, root)).toBe(true);+ }), 240_000);++ test('rename storm guard: 30 pages deleted with nothing arriving is still a withheld mass delete', () => brain(async ({ engine, root }) => {+ folder(root, 'bulk', 30); commit(root, 'bulk');+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ git(root, 'rm', '-rq', 'bulk'); commit(root, 'rm');+ await performManagedSync(engine, OPTS);+ expect((await ids(engine, 'bulk')).length).toBe(30);+ const convergence = (await readSyncStatus(engine, 'default')).convergence;+ expect(convergence && 'deletes_withheld' in convergence ? convergence.deletes_withheld?.count : null).toBe(30);+ }), 240_000);++ test('rename storm guard: 30 pages deleted while 30 different files arrive are deletes and adds, never renames', () => brain(async ({ engine, root }) => {+ folder(root, 'bulk', 30); commit(root, 'bulk');+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ git(root, 'rm', '-rq', 'bulk'); folder(root, 'other', 30, i => `# Other ${i}\n\nA different synthetic body ${i}.\n`); commit(root, 'swap');+ expect(await performManagedSync(engine, OPTS)).toMatchObject({ renamed: 0, added: 30 });+ expect((await ids(engine, 'other')).length).toBe(30);+ }), 240_000);});