Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

facts: duplicate adoption key is claim, source and visibility; phantom pass still waits for a fully fenced source (#6278 C1/C4)

garrytan438608fc4c77
diff --git a/src/core/cycle/extract-facts.ts b/src/core/cycle/extract-facts.ts
index 57be25c00f..c7ec901bd2 100644
--- a/src/core/cycle/extract-facts.ts
+++ b/src/core/cycle/extract-facts.ts
@@ -552,7 +552,7 @@ export async function runExtractFacts(
: 'could not be fenced this run, so those pages keep their fact index and skip reconciliation while the other pages reconcile. ' +
'The FACTS_FENCE_FAILED warnings name each page and why; ' +
'a page whose canonical file does not exist on this host is fenced by the cycle on the host that holds the file; ' +
- 'a row the fence codec cannot render stays active and searchable (doctor fence_integrity lists it). ' +
+ 'a row the fence codec cannot render stays active and searchable (doctor fence_integrity lists it); the phantom-redirect pass waits until the source has no unfenced rows. ' +
'Individual rows can instead be drained via `forget_fact`.'),
);
// #3683: a guard-triggered run books its halt here for doctor
@@ -578,8 +578,11 @@ export async function runExtractFacts(
// Idempotency-by-construction: phantom predicate filters out `deleted_at
// IS NOT NULL` so a half-redirected page (soft-deleted, .md still on
// disk) won't be re-redirected.
+ // #6278: the pass moves rows by their fence position and would strand a
+ // legacy row (no position) on a deleted phantom, so it still waits for the
+ // whole source to be fenced; the reconcile walk below is per page.
let phantomResult: PhantomPassResult = emptyPhantomPassResult();
- if (opts.brainDir) {
+ if (opts.brainDir && legacyCount === 0) {
try {
phantomResult = await runPhantomRedirectPass(
engine,
diff --git a/src/core/facts/unfenced-facts.ts b/src/core/facts/unfenced-facts.ts
index 2163dbbedd..ec92eb30d4 100644
--- a/src/core/facts/unfenced-facts.ts
+++ b/src/core/facts/unfenced-facts.ts
@@ -32,8 +32,9 @@
* project, so it is written back into `facts.fact` with the row number (the
* canonical projection expires any adopted row whose text differs from its
* fence row). A row the codec would change further, or whose normalized
- * claim and source another active fence row or planned row already carries
- * (the reconciler indexes a duplicate once and would retire the second),
+ * claim, source and visibility another active fence row or planned row
+ * already carries (the reconciler indexes a duplicate once and would retire
+ * the second),
* stays a legacy row exactly as it is: never deleted or expired, recorded as
* an `UnadoptableFactRecord` (source, page, fact id, reason class; never the
* claim text) and named in `failed_pages` with the stable token
@@ -279,21 +280,23 @@ export async function planFence(engine: BrainEngine, sourceId: string, entitySlu
const assignments: Array<{ id: string; row_num: number; fact: string }> = [];
const rejected: UnadoptableFactRecord[] = [];
// The reconciler indexes active rows with one claim and source once (#1781)
- // and retires the later copy, so a claim the page already carries, or that
- // an earlier row of this group plans, is not adopted a second time.
- const dupKey = (claim: string, source: string | undefined) => `${claim}\u0000${source || FENCE_SOURCE_DEFAULT}`;
+ // and retires the later copy, so a claim the page already carries with the
+ // same source and visibility (the projection's identity), or that an
+ // earlier row of this group plans, is not adopted a second time. A private
+ // and a world row with one text are two facts and both adopt, as before.
+ const dupKey = (claim: string, source: string | undefined, visibility: string) => `${claim}\u0000${source || FENCE_SOURCE_DEFAULT}\u0000${visibility}`;
const planned = new Set<string>();
const ownedKeys = new Set(existingFence.facts.filter(f => {
const owner = occupied.get(f.rowNum);
return f.active && owner !== undefined && owner.fact === f.claim && (owner.source ?? '') === (f.source ?? '');
- }).map(f => dupKey(f.claim, f.source)));
+ }).map(f => dupKey(f.claim, f.source, f.visibility)));
for (const row of group) {
const oracle = adoptableClaim(row);
if ('rejected' in oracle) {
rejected.push({ source_id: sourceId, slug: entitySlug, fact_id: Number(row.id), reason: 'fence_unrenderable', class: oracle.rejected });
continue;
}
- const key = dupKey(oracle.claim, row.source);
+ const key = dupKey(oracle.claim, row.source, row.visibility);
if (planned.has(key) || ownedKeys.has(key)) {
rejected.push({ source_id: sourceId, slug: entitySlug, fact_id: Number(row.id), reason: 'duplicate_claim', class: 'duplicate_claim' });
continue;
diff --git a/test/extract-facts-unfenced-rows.test.ts b/test/extract-facts-unfenced-rows.test.ts
index 674a35bd95..c9abd0ce7e 100644
--- a/test/extract-facts-unfenced-rows.test.ts
+++ b/test/extract-facts-unfenced-rows.test.ts
@@ -245,7 +245,7 @@ for (const backend of testBackends()) {
expect(claims.some(c => c.includes('left Acme') || c.includes('uses tabs') || c === '')).toBe(false);
// No second fence-owned row for any adopted claim (the legacy duplicate keeps its text, unfenced).
expect(await managed.executeRaw(`SELECT id FROM facts WHERE fact = 'Alice example likes tea' AND row_num IS NOT NULL`)).toHaveLength(1);
- expect(await managed.executeRaw(`SELECT id FROM facts WHERE fact = 'Alice example likes tea' AND row_num IS NULL`)).toEqual([{ id: ids.duplicate }]);
+ expect((await managed.executeRaw<{ id: number }>(`SELECT id FROM facts WHERE fact = 'Alice example likes tea' AND row_num IS NULL`)).map(r => Number(r.id))).toEqual([ids.duplicate]);
// Another page of the source reconciled in the same run.
const [staleRow] = await managed.executeRaw<{ expired: boolean }>('SELECT expired_at IS NOT NULL AS expired FROM facts WHERE id = $1', [staleId]);
expect(staleRow.expired).toBe(true);