Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Conversion evidence matches a retyped page's file read either way, so a real retype adopts and a file set back to a database-only state stays both_moved (GST-2568)
garrytan260a388b715f
diff --git a/src/core/persistence/git-gap.ts b/src/core/persistence/git-gap.tsindex 8fd1f93e0b..85557746f4 100644--- a/src/core/persistence/git-gap.ts+++ b/src/core/persistence/git-gap.ts@@ -139,8 +139,9 @@ export function compareInlineExportFile(raw: Buffer, importPath: string, snapsho/*** A committed file parsed the way reconcile parses a canonical file, aligned with the stored page (the census contract's file side).- * GST-2568: `retypeStamp` false reads a file naming the type a retype replaced as written, which content evidence needs: it- * compares the file with the page's older versions, and those predate the retype's stamp.+ * GST-2568: `retypeStamp` false reads a file naming the type a retype replaced as written. Content evidence reads each blob+ * both ways, because a version archived before the retype matches the file as written and one archived after it matches+ * the file with the stamp kept.*/function canonicalFile(raw: Buffer, importPath: string, page: ExportSnapshot['page'], retypeStamp = true): { state: 'parsed'; canonical: ReturnType<typeof reconcileCanonical> }| { state: 'differs'; paths: string[] } | { state: 'unparseable'; reason: UnparseableReason } {@@ -489,6 +490,8 @@ export async function readDirectionEvidence(engine: BrainEngine, input: { sourceconst sizes = await readBlobSizes(root, oids) ?? new Map<string, number>();const readable = oids.flatMap(oid => { const size = sizes.get(oid); return size !== undefined && size <= SYNC_READ_BOUND ? [{ oid, size, mode: '100644' }] : []; });const fileDigests = new Map<string, string | null>();+ const sameState = new Map<string, Map<string, string>>();+ const stateOf = (id: string, value: string | null): string | null => { let at = value; while (at !== null && sameState.get(id)?.has(at)) at = sameState.get(id)!.get(at)!; return at; };for (let from = 0; from < readable.length;) {let to = from, bytes = 0;while (to < readable.length && (to === from || bytes + readable[to]!.size <= CAT_FILE_BATCH_BYTES)) bytes += readable[to++]!.size;@@ -500,8 +503,11 @@ export async function readDirectionEvidence(engine: BrainEngine, input: { sourceconst key = `${subject.id}:${blob.oid}`;if (fileDigests.has(key) || !blobs?.has(blob.oid)) continue;if (blob.oid !== subject.blob.oid && !older(subject).list.some(entry => entry.oid === blob.oid)) continue;- const file = canonicalFile(blobs.get(blob.oid)!, subject.importPath, page, false);- fileDigests.set(key, file.state === 'parsed' ? digest(file.canonical) : null);+ const reading = (retypeStamp: boolean) => { const file = canonicalFile(blobs.get(blob.oid)!, subject.importPath, page, retypeStamp); return file.state === 'parsed' ? digest(file.canonical) : null; };+ const written = reading(false), stamped = stateOf(subject.id, reading(true));+ fileDigests.set(key, written);+ const ownState = stateOf(subject.id, written);+ if (ownState !== null && stamped !== null && ownState !== stamped) sameState.set(subject.id, (sameState.get(subject.id) ?? new Map()).set(stamped, ownState));}}from = to;@@ -517,10 +523,11 @@ export async function readDirectionEvidence(engine: BrainEngine, input: { source}const snapshot = read.snapshots[i]!;const rows = byPage.get(subject.id) ?? [];- const versions = rows.flatMap(row => read.digests.has(String(row.id)) ? [{ id: Number(row.id), digest: read.digests.get(String(row.id))! }] : []);+ const state = (value: string | null) => stateOf(subject.id, value);+ const versions = rows.flatMap(row => read.digests.has(String(row.id)) ? [{ id: Number(row.id), digest: state(read.digests.get(String(row.id))!)! }] : []);const unread = read.gaps.has(subject.id) || rows.length >= EVIDENCE_DEPTH || versions.length < rows.length;- const decided = decideDirection(digest(reconcileCanonical(snapshot.page, snapshot.tags)), fileDigests.get(`${subject.id}:${subject.blob.oid}`) ?? null, versions,- list.map(entry => ({ oid: entry.oid, digest: fileDigests.get(`${subject.id}:${entry.oid}`) ?? null })), { versions: unread, blobs: capped });+ const decided = decideDirection(state(digest(reconcileCanonical(snapshot.page, snapshot.tags)))!, state(fileDigests.get(`${subject.id}:${subject.blob.oid}`) ?? null), versions,+ list.map(entry => ({ oid: entry.oid, digest: state(fileDigests.get(`${subject.id}:${entry.oid}`) ?? null) })), { versions: unread, blobs: capped });out.set(subject.id, { ...decided, versions: versions.length, blobs: list.length, committed_at });}}diff --git a/test/git-gap-direction-evidence-d4.test.ts b/test/git-gap-direction-evidence-d4.test.tsindex d6b587942f..5ea85a013e 100644--- a/test/git-gap-direction-evidence-d4.test.ts+++ b/test/git-gap-direction-evidence-d4.test.ts@@ -226,6 +226,36 @@ for (const backend of testBackends()) describe(`git-gap direction evidence (d4):expect(pages.find(page => page.slug === 'notes/g')).toMatchObject({ evidence: { kind: 'both_moved', basis: 'conflicting_history' } });}, 120000);+ test('GST-2568: a retyped page matches its file read either way, so a real retype adopts and a file set back under a stamped import is both_moved', async () => {+ const root = tempDir('git-gap-retype-');+ git(root, 'init', '-q');+ await engine.executeRaw("UPDATE sources SET local_path=$1 WHERE id='default'", [root]);+ const person = (slug: string, text: string) => engine.putPage(slug, { type: 'person', title: 'T', compiled_truth: text, timeline: '' });+ const retyped = (slug: string, text: string) => engine.putPage(slug, { type: 'concept', title: 'T', compiled_truth: text, timeline: '', frontmatter: { legacy_type: 'person' } });+ // schema-pack/retype.ts changes the row in place and archives no version.+ const retype = (slug: string) => engine.executeRaw("UPDATE pages SET type='concept', frontmatter = jsonb_set(frontmatter, ARRAY['legacy_type'], to_jsonb('person'::text), true) WHERE slug=$1", [slug]);+ const fileOf = (text: string) => '---\ntype: person\ntitle: T\n---\n\n' + text + '\n';+ for (const slug of ['people/retype-then-edit', 'people/archived-then-retyped']) { await person(slug, 'Body C0.'); put(root, slug + '.md', await rendered(slug)); }+ commitAll(root, 'first');+ // A real retype, then a database edit: the only archived version is the retyped page, which the file reads as with the stamp kept.+ await retype('people/retype-then-edit');+ await engine.createVersion('people/retype-then-edit'); await retyped('people/retype-then-edit', 'Edited in the database.');+ // The pre-retype state archived, then retyped and edited in one write: the file equals that version as written.+ await engine.createVersion('people/archived-then-retyped'); await retyped('people/archived-then-retyped', 'Edited in the database.');+ // A database-only state, a file commit sync imported with the stamp kept, then the file set back to the database-only state.+ await person('people/set-back', 'Body C0.');+ await engine.createVersion('people/set-back'); await person('people/set-back', 'Body C0x.');+ await retype('people/set-back');+ put(root, 'people/set-back.md', fileOf('Body C1.')); commitAll(root, 'file C1');+ await engine.createVersion('people/set-back'); await retyped('people/set-back', 'Body C1.');+ put(root, 'people/set-back.md', fileOf('Body C0.')); commitAll(root, 'file back to C0');+ await engine.createVersion('people/set-back'); await retyped('people/set-back', 'Body C2.');+ const pages: GitGapPage[] = [];+ await censusGitGap(engine, 'default', { sample: 0, evidence: true, collect: page => { pages.push(page); } });+ const kind = (slug: string) => pages.find(page => page.slug === slug)?.evidence?.kind;+ expect([kind('people/retype-then-edit'), kind('people/archived-then-retyped'), kind('people/set-back')]).toEqual(['adopt_db_moved', 'adopt_db_moved', 'both_moved']);+ }, 120000);+test('a version with no comparable content (a deletion, a segmented snapshot) counts as unread: no one-sided Git verdict', async () => {const root = await fixture();const [page] = await engine.executeRaw<{ id: string }>("SELECT id::text FROM pages WHERE slug='notes/file-moved'");