Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

extract_facts: lift the pending-legacy-row query into pendingLegacyRows (function-size ratchet); regenerate error-codes.md for fence_unrenderable

garrytanc3adda7eb128
diff --git a/docs/guides/error-codes.md b/docs/guides/error-codes.md
index af5b089d84..2998b2483b 100644
--- a/docs/guides/error-codes.md
+++ b/docs/guides/error-codes.md
@@ -966,6 +966,16 @@ More: [docs/guides/retrieval-feedback.md#feedback_disabled](../../docs/guides/re
More: [docs/guides/retrieval-feedback.md#feedback_not_authorized](../../docs/guides/retrieval-feedback.md#feedback_not_authorized)
+### fence_unrenderable
+
+<a id="fence_unrenderable"></a>
+
+| Meaning | Why | Next step | Who acts · consent | Verify | Exit | Retryable |
+|---|---|---|---|---|---|---|
+| A legacy fact row was not adopted into its page's facts fence because the fence codec reads the rendered row back as something other than the stored fact. | The fence trims a claim, folds CRLF to LF, reads `~~x~~` as a struck row and `<br>` as a line break; a claim the codec would change by more than whitespace cannot be written into the fence without changing what it says, so the row stays a legacy row (active and searchable, nothing is lost) and is counted by doctor fence_integrity as unrenderable_legacy_facts. The same code names a planned adoption whose rendered fence does not read back as its facts; that is a gbrain planning defect, not caller input. | Nothing to fix in the page: the rows stay active and searchable. Report the page slug and the reason class with the gbrain version; do not forget or expire a row to clear this count. Run: gbrain doctor --only fence_integrity --json | agent | `gbrain doctor --json` | 1 | no |
+
+More: [docs/guides/write-refusals.md#fence_unrenderable](../../docs/guides/write-refusals.md#fence_unrenderable)
+
### fetch_failed
<a id="fetch_failed"></a>
diff --git a/src/core/cycle/extract-facts.ts b/src/core/cycle/extract-facts.ts
index c41921ad3f..57be25c00f 100644
--- a/src/core/cycle/extract-facts.ts
+++ b/src/core/cycle/extract-facts.ts
@@ -446,6 +446,44 @@ function timelineHasGenuineFactsFenceMarker(timeline: string): boolean {
return false;
}
+/**
+ * Empty-fence guard (Codex R2-#7; #2484; #2646; per page, #6278): the active
+ * unfenced rows the fence step left on live pages of this source, per page.
+ * A row counts only when `row_num IS NULL`, its `entity_slug` resolves to a
+ * LIVE page in THIS run's source (#3526 source isolation), it is not
+ * soft-expired (#2646: `forget_fact` drains rows by soft-expiring them), the
+ * source has a `local_path` (#2763), and it is not an ontology observation
+ * (`dimension IS NULL`, #6264: those are never fenced, so they must not gate
+ * either). Rows without a page or checkout (#2484: the inline writer's
+ * slugify-floor / stub-guard-blocked slugs) are structurally unfenceable and
+ * never gate.
+ */
+async function pendingLegacyRows(engine: BrainEngine, sourceId: string): Promise<{ count: number; slugs: Set<string> }> {
+ const legacy = await engine.executeRaw<{ entity_slug: string; n: string }>(
+ `SELECT f.entity_slug, COUNT(*) AS n
+ FROM facts f
+ WHERE f.source_id = $1
+ AND f.row_num IS NULL
+ AND f.dimension IS NULL
+ AND f.entity_slug IS NOT NULL
+ AND f.expired_at IS NULL
+ AND EXISTS (
+ SELECT 1 FROM pages p
+ WHERE p.source_id = f.source_id
+ AND p.slug = f.entity_slug
+ AND p.deleted_at IS NULL
+ )
+ AND EXISTS (
+ SELECT 1 FROM sources s
+ WHERE s.id = f.source_id
+ AND s.local_path IS NOT NULL
+ )
+ GROUP BY f.entity_slug`,
+ [sourceId],
+ );
+ return { count: legacy.reduce((sum, row) => sum + parseInt(row.n ?? '0', 10), 0), slugs: new Set(legacy.map(row => row.entity_slug)) };
+}
+
/**
* Run the extract_facts phase against the current brain state. Returns
* an ExtractFactsResult envelope; status mapping (ok / warn / fail)
@@ -500,41 +538,10 @@ export async function runExtractFacts(
}
}
- // ── Empty-fence guard (Codex R2-#7; #2484; #2646; per page, #6278) ──
+ // ── Empty-fence guard (per page, #6278) ─────────────────────────
// Rows the fence step could not fence this run keep their own page out of
- // the destructive reconciliation pass; the other pages reconcile. A row
- // counts only when `row_num IS NULL`, its `entity_slug` resolves to a LIVE
- // page in THIS run's source (#3526 source isolation), it is not
- // soft-expired (#2646: `forget_fact` drains rows by soft-expiring them),
- // the source has a `local_path` (#2763), and it is not an ontology
- // observation (`dimension IS NULL`, #6264: those are never fenced, so they
- // must not gate either). Rows without a page or checkout (#2484: the
- // inline writer's slugify-floor / stub-guard-blocked slugs) are
- // structurally unfenceable and never gate.
- const legacy = await engine.executeRaw<{ entity_slug: string; n: string }>(
- `SELECT f.entity_slug, COUNT(*) AS n
- FROM facts f
- WHERE f.source_id = $1
- AND f.row_num IS NULL
- AND f.dimension IS NULL
- AND f.entity_slug IS NOT NULL
- AND f.expired_at IS NULL
- AND EXISTS (
- SELECT 1 FROM pages p
- WHERE p.source_id = f.source_id
- AND p.slug = f.entity_slug
- AND p.deleted_at IS NULL
- )
- AND EXISTS (
- SELECT 1 FROM sources s
- WHERE s.id = f.source_id
- AND s.local_path IS NOT NULL
- )
- GROUP BY f.entity_slug`,
- [sourceId],
- );
- const legacyCount = legacy.reduce((sum, row) => sum + parseInt(row.n ?? '0', 10), 0);
- const legacySlugs = new Set(legacy.map(row => row.entity_slug));
+ // the destructive reconciliation pass; the other pages reconcile.
+ const { count: legacyCount, slugs: legacySlugs } = await pendingLegacyRows(engine, sourceId);
result.legacyRowsPending = legacyCount;
if (legacyCount > 0) {
result.guardTriggered = true;