Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(memory): number a remembered fence row past facts rows the fence does not hold (#6696 C9)
garrytanee8c574aaf1d
diff --git a/src/core/persistence/memory-prepare.ts b/src/core/persistence/memory-prepare.tsindex d5f19d5548..c37b284133 100644--- a/src/core/persistence/memory-prepare.ts+++ b/src/core/persistence/memory-prepare.ts@@ -148,10 +148,13 @@ export async function prepareMemoryMutation(engine: BrainEngine, row: WriteRequeconst target = await normalizeTargetFences(engine, { sourceId: row.source_id, slug: row.slug, kind: 'facts', page: snapshot.page });if (target.fixes.length) fencesNormalized = { fences_normalized: pageFencesNormalized({ sourceId: row.source_id, slug: row.slug, fixes: target.fixes,writer: row.principal_kind, path: snapshot.page.source_path ?? null, remote: row.authority.remote }) };+ // #6696 C9: a facts row the fence does not hold (extraction, a hand edit) still owns its row_num, so the new row numbers past both.+ const [indexed] = await engine.executeRaw<{ n: number }>('SELECT COALESCE(MAX(row_num),0)::int AS n FROM facts WHERE source_id=$1 AND source_markdown_slug=$2', [row.source_id, row.slug]);// #6385 R12: an entity fence that does not parse cleanly refuses typed instead of losing the rows it skipped.const appended = refuseUnparsedRewrite(target.page, row.slug, row.source_id, () => upsertFactRow(target.page.compiled_truth, {claim: input.fact, kind: input.kind, visibility: input.visibility, confidence: 1, notability: 'medium', validFrom: formatFenceDate(validFrom),- validUntil: validUntil ? formatFenceDate(validUntil) : undefined, source: fact.source, context }));+ validUntil: validUntil ? formatFenceDate(validUntil) : undefined, source: fact.source, context,+ rowNum: Math.max(indexed?.n ?? 0, ...parseFactsFence(target.page.compiled_truth).facts.map(f => f.rowNum)) + 1 }));rowNum = appended.rowNum;let body = appended.body;const old = decision.candidate;@@ -174,8 +177,11 @@ export async function prepareMemoryMutation(engine: BrainEngine, row: WriteRequeif (rowNum !== undefined) {const inserted = await tx.insertFacts([{ ...fact, row_num: rowNum, source_markdown_slug: row.slug }], { source_id: row.source_id }); // gbrain-allow-direct-insert: coordinator atomically publishes the prepared canonical fact fence and its new indexed rowif (inserted.ids.length !== 1) {+ const [occupant] = await tx.executeRaw<{ id: number }>('SELECT id FROM facts WHERE source_id=$1 AND source_markdown_slug=$2 AND row_num=$3', [row.source_id, row.slug, rowNum]);throw opError('storage_error', 'The new canonical fact row was not indexed.',- `The fact row for request ${row.request_id} in source ${row.source_id} was not indexed, so its transaction rolled back. Inspect the request before remembering again; if it repeats, run gbrain doctor --json and report it to the user.`,+ `The fact row for request ${row.request_id} in source ${row.source_id} was not indexed, so its transaction rolled back.`+ + (occupant ? ` Row ${rowNum} of ${row.slug} is already held by fact ${occupant.id}.` : '')+ + ' Inspect the request before remembering again; if it repeats, run gbrain doctor --json and report it to the user.',{ fix: receiptFix(row) });}id = inserted.ids[0];diff --git a/test/persistence-memory-mutations.test.ts b/test/persistence-memory-mutations.test.tsindex 00d6a77a0a..c011b4498a 100644--- a/test/persistence-memory-mutations.test.ts+++ b/test/persistence-memory-mutations.test.ts@@ -177,6 +177,26 @@ describe('journaled memory publication, both engines', () => {}}, 120_000);+ test('remember numbers its fence row past a facts row the fence does not hold', async () => {+ for (const engine of engines) {+ const slug = 'people/row-floor-example';+ const fenced = [1, 2].reduce(body => upsertFactRow(body, { claim: `Fenced claim ${body.length}`, kind: 'fact', visibility: 'world',+ confidence: 1, notability: 'medium' }).body, 'Biography');+ await setupPage(engine, slug, fenced);+ await engine.transaction(tx => withCoordinatedWrite(tx, [sourceId], () => tx.executeRaw(+ `INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, source, row_num, source_markdown_slug)+ VALUES ($1, $2, 'Extracted occupant claim', 'fact', 'world', 'extraction fixture', 3, $2)`, [sourceId, slug]), TEST_WRITE_ATTRIBUTION));+ const receipt = await submitRememberMutation(context(engine), { fact: 'Remembered past the occupant', provenance: 'test conversation',+ entity: slug, request_id: randomUUID() }, 30_000);+ expect(receipt).toMatchObject({ status: 'inserted', state: 'committed' });+ const rows = await engine.executeRaw<{ fact: string; row_num: number }>(+ 'SELECT fact, row_num FROM facts WHERE source_id=$1 AND source_markdown_slug=$2 ORDER BY row_num', [sourceId, slug]);+ expect(rows.map(r => [r.fact, Number(r.row_num)])).toEqual([['Extracted occupant claim', 3], ['Remembered past the occupant', 4]]);+ const fence = parseFactsFence((await engine.readPageSnapshot(slug, { sourceId }))!.page.compiled_truth).facts;+ expect(fence.map(f => f.rowNum)).toEqual([1, 2, 4]);+ }+ }, 60_000);+test('a remember prepared against a stale page re-prepares; every committed receipt is readable at acknowledgement', async () => {let held = 0;let gate = Promise.withResolvers<void>();