Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(migrations): v0.32.2 verify skips a deleted page whose fenced rows are all expired (#6685)

time-attack3914913d5859
diff --git a/skills/migrations/v0.32.2.md b/skills/migrations/v0.32.2.md
index 872df5f93f..f8848fe0ff 100644
--- a/skills/migrations/v0.32.2.md
+++ b/skills/migrations/v0.32.2.md
@@ -29,7 +29,9 @@ restorable database backup as well as source files.
`gbrain apply-migrations --migration 0.32.2 --yes`.
2. The orchestrator checks the schema, fences eligible facts, then verifies
that each page's fence and its indexed facts carry the same row numbers
- (a duplicate active fence row may be indexed once). Only live legacy
+ (a duplicate active fence row may be indexed once; a page deleted after
+ all of its fenced rows expired is reported as `skipped_expired_no_page`,
+ not as drift, while a missing page with a live row still fails). Only live legacy
rows with an existing source-scoped page and canonical file are
eligible. Missing targets are reported as `skipped_no_page`; no stub is
created. Existing fence and database row numbers remain reserved.
diff --git a/src/commands/migrations/v0_32_2.ts b/src/commands/migrations/v0_32_2.ts
index 3b32426417..823bc8f6ed 100644
--- a/src/commands/migrations/v0_32_2.ts
+++ b/src/commands/migrations/v0_32_2.ts
@@ -241,8 +241,9 @@ async function phaseCVerify(
// (chat-log shape is the source of truth). Same cli: exclusion as
// extract_facts reconciliation. Counting them here fails every brain
// that ran extract-conversation-facts.
- const groups = await engine.executeRaw<{ source_id: string; source_markdown_slug: string; row_nums: number[] }>(
- `SELECT source_id, source_markdown_slug, array_agg(row_num ORDER BY row_num) AS row_nums
+ const groups = await engine.executeRaw<{ source_id: string; source_markdown_slug: string; row_nums: number[]; all_expired: boolean }>(
+ `SELECT source_id, source_markdown_slug, array_agg(row_num ORDER BY row_num) AS row_nums,
+ bool_and(expired_at IS NOT NULL) AS all_expired
FROM facts
WHERE row_num IS NOT NULL
AND COALESCE(source, '') NOT LIKE 'cli:%'
@@ -251,6 +252,10 @@ async function phaseCVerify(
const mismatches: string[] = [];
let pagesChecked = 0;
+ // #6685: a page deleted after all of its fenced rows expired leaves
+ // nothing to compare; like the backfill's skipped_no_page, it is not drift.
+ // A missing page that still has a live row is drift.
+ let skippedExpiredNoPage = 0;
// Managed brains publish the fence through the coordinator, which writes
// the canonical file in the same publication; the database copy is the
// one every managed page has, database-only pages included.
@@ -261,7 +266,8 @@ async function phaseCVerify(
if (managed) {
const snapshot = await engine.readPageSnapshot(g.source_markdown_slug, { sourceId: g.source_id });
if (!snapshot) {
- mismatches.push(`${g.source_markdown_slug} (page missing)`);
+ if (g.all_expired) skippedExpiredNoPage += 1;
+ else mismatches.push(`${g.source_markdown_slug} (page missing)`);
continue;
}
body = serializePageToMarkdown(snapshot.page, snapshot.tags);
@@ -270,7 +276,9 @@ async function phaseCVerify(
if (!localPath) continue;
const target = await resolvePageWriteTarget(engine, g.source_markdown_slug, g.source_id);
if (!target.ok || !existsSync(target.filePath)) {
- mismatches.push(`${g.source_markdown_slug} (file missing)`);
+ // Skip only an absent file; an unresolvable target is still drift.
+ if (target.ok && g.all_expired) skippedExpiredNoPage += 1;
+ else mismatches.push(`${g.source_markdown_slug} (file missing)`);
continue;
}
body = readFileSync(target.filePath, 'utf-8');
@@ -301,7 +309,8 @@ async function phaseCVerify(
detail: `${mismatches.length} pages drifted: ${mismatches.slice(0, 3).join(' | ')}${mismatches.length > 3 ? '...' : ''}`,
};
}
- return { name: 'verify', status: 'complete', detail: `pages_checked=${pagesChecked}` };
+ const skipped = skippedExpiredNoPage > 0 ? ` skipped_expired_no_page=${skippedExpiredNoPage}` : '';
+ return { name: 'verify', status: 'complete', detail: `pages_checked=${pagesChecked}${skipped}` };
} catch (e) {
return { name: 'verify', status: 'failed', detail: e instanceof Error ? e.message : String(e) };
}
diff --git a/test/migrations-v0_32_2-managed.test.ts b/test/migrations-v0_32_2-managed.test.ts
index 9b25620b3d..2382f00b3d 100644
--- a/test/migrations-v0_32_2-managed.test.ts
+++ b/test/migrations-v0_32_2-managed.test.ts
@@ -234,6 +234,23 @@ for (const backend of testBackends()) {
} });
}, 120_000);
+ // #6685: fenced rows left in the index after their page was deleted.
+ test.each([
+ ['all expired: skipped, not drift', [true, true], { status: 'complete', detail: 'pages_checked=0 skipped_expired_no_page=1' }],
+ ['one live row: still drift', [true, false], { status: 'failed', detail: '1 pages drifted: projects/gone-example (page missing)' }],
+ ])(`${backend}: verify on a deleted page's fenced rows, %s (#6685)`, async (_case, expired, expected) => {
+ await managedBrain(async ({ engine }) => {
+ expect(await __testing.phaseCVerify(engine, OPTS)).toMatchObject(expected);
+ }, { databaseUrl, setup: async ({ engine }) => {
+ for (const [index, isExpired] of expired.entries()) {
+ await engine.executeRaw(`INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, notability, valid_from, source, confidence,
+ row_num, source_markdown_slug, expired_at)
+ VALUES ('default', 'projects/gone-example', $1, 'fact', 'private', 'medium', now(), 'manual', 1.0, $2, 'projects/gone-example',
+ CASE WHEN $3::boolean THEN now() ELSE NULL END)`, [`Claim ${index + 1}`, index + 1, isExpired]);
+ }
+ } });
+ }, 120_000);
+
test(`${backend}: a duplicate active fence row reads in sync whichever writer indexed the page last (#5814)`, async () => {
await managedBrain(async ({ ctx, engine }) => {
const slug = 'people/dana-example';
diff --git a/test/migrations-v0_32_2.test.ts b/test/migrations-v0_32_2.test.ts
index b69f5e8c5c..c0e47e85ae 100644
--- a/test/migrations-v0_32_2.test.ts
+++ b/test/migrations-v0_32_2.test.ts
@@ -535,6 +535,38 @@ describe('phaseCVerify', () => {
expect(r.detail).toContain('pages_checked=1');
});
+ // #6685: rows that stay fenced in the index after their page was deleted.
+ // Every row expired: nothing is left to compare, so the page is skipped (like
+ // the backfill's skipped_no_page), not drift. A live row is still drift.
+ async function seedFencedRowOfDeletedPage(slug: string, rowNum: number, expired: boolean): Promise<void> {
+ await engine.executeRaw(
+ `INSERT INTO facts (source_id, entity_slug, fact, kind, visibility, notability,
+ valid_from, source, confidence, row_num, source_markdown_slug, expired_at)
+ VALUES ('default', $1, $2, 'fact', 'private', 'medium', now(), 'manual', 1.0, $3, $1,
+ CASE WHEN $4::boolean THEN now() ELSE NULL END)`,
+ [slug, `Claim ${rowNum}`, rowNum, expired],
+ );
+ }
+
+ test('skips a deleted page whose fenced rows are all expired (#6685)', async () => {
+ await seedLegacyFact({ entity_slug: 'people/alice', fact: 'F1' });
+ await __testing.phaseBFenceFacts(engine, OPTS);
+ await seedFencedRowOfDeletedPage('projects/gone-example', 1, true);
+ await seedFencedRowOfDeletedPage('projects/gone-example', 2, true);
+
+ expect(await __testing.phaseCVerify(engine, OPTS))
+ .toMatchObject({ status: 'complete', detail: 'pages_checked=1 skipped_expired_no_page=1' });
+ });
+
+ test('still reports a deleted page that keeps a live fenced row (#6685)', async () => {
+ await seedFencedRowOfDeletedPage('projects/gone-example', 1, true);
+ await seedFencedRowOfDeletedPage('projects/gone-example', 2, false);
+
+ const r = await __testing.phaseCVerify(engine, OPTS);
+ expect(r.status).toBe('failed');
+ expect(r.detail).toBe('1 pages drifted: projects/gone-example (file missing)');
+ });
+
// #5814: extract_facts indexes a duplicate ACTIVE fence row (same claim and
// source) once (#1781), so the fence legitimately holds one row more than
// the index. Verify must read that page as in sync, and still catch a fence