Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

lost-fence-rows: an empty section written after the loss is the answer

garrytan4d50820e12f4
diff --git a/docs/guides/repair.md b/docs/guides/repair.md
index febd1da4bd..7f0e3325e4 100644
--- a/docs/guides/repair.md
+++ b/docs/guides/repair.md
@@ -2075,6 +2075,20 @@ the whole section on purpose, or an edit may have lost it: ask which.
write retires every row on it. For facts it is:
```markdown
+## Facts
+
+<!--- gbrain:facts:begin -->
+
+| # | claim | kind | confidence | visibility | notability | valid_from | valid_until | source | context |
+|---|-------|------|------------|------------|------------|------------|-------------|--------|---------|
+<!--- gbrain:facts:end -->
+```
+
+A page with no file changes through `get_page` (`include_content: true`) and
+`put_page` with that revision as `expected_revision`; `edit_page` refuses to
+write fence markers. Neither outcome is a default, and nothing changes until the
+user chooses.
+
### Lost fence rows
A page rewrite that drops its `## Facts` or takes section is safe on its
@@ -2099,7 +2113,8 @@ claim is on the page now, and page history shows the row in a section that
then vanished before the retirement. A row you removed by editing a section
that stayed in place, a row retired by an explicit empty section, and a
forgotten fact are never listed, and neither is a row you re-added after the
-loss and then removed again. A section you rebuilt by hand without a row
+loss and then removed again, or any row of a page where you wrote an empty
+section after the loss. A section you rebuilt by hand without a row
leaves the same records as the bug, so read the preview with the user before
applying it. `gbrain repair generations --mode restore`
does not cover this case, because the retiring write published a fence that
@@ -2113,20 +2128,6 @@ fact is a new facts row at the trust tier the expired one had (the expired row
stays as history). It is explicit-only and nothing runs on upgrade: show the
user the preview before running the apply.
-## Facts
-
-<!--- gbrain:facts:begin -->
-
-| # | claim | kind | confidence | visibility | notability | valid_from | valid_until | source | context |
-|---|-------|------|------------|------------|------------|------------|-------------|--------|---------|
-<!--- gbrain:facts:end -->
-```
-
-A page with no file changes through `get_page` (`include_content: true`) and
-`put_page` with that revision as `expected_revision`; `edit_page` refuses to
-write fence markers. Neither outcome is a default, and nothing changes until the
-user chooses.
-
### Extractor facts
A managed write by a gbrain older than v0.60.11.0 to a conversation page
diff --git a/src/core/repair/lost-fence-rows.ts b/src/core/repair/lost-fence-rows.ts
index 4f805717e8..f97f2aa274 100644
--- a/src/core/repair/lost-fence-rows.ts
+++ b/src/core/repair/lost-fence-rows.ts
@@ -63,20 +63,26 @@ type History = Array<{ id: number | null; compiled_truth: string; timeline: stri
/**
* The newest body whose fence holds the claim (at any row number: a later re-add counts), when the body after it has
* no fence of that kind at all; `removed` when the body after it kept the fence without the claim (an edit, a forget
- * strike or a takes_remove reservation after any re-add), or when it is the page as it is now; null when no stored
+ * strike or a takes_remove reservation after any re-add), when any later body holds an explicit empty fence of that kind
+ * (the user's answer for every row the section ever held), or when it is the page as it is now; null when no stored
* version holds it.
*/
function lostIn(history: History, fence: 'facts' | 'takes', holds: (body: string) => ParsedFact | ParsedTake | undefined) {
let held: { version_id: number; cell: ParsedFact | ParsedTake } | null = null;
let next: 'absent' | 'fence' | null = null;
+ let emptied = false;
for (const body of history) {
- const cell = holds(`${body.compiled_truth}\n${body.timeline ?? ''}`);
+ const text = `${body.compiled_truth}\n${body.timeline ?? ''}`;
+ const cell = holds(text);
if (cell) {
if (body.id === null) return 'removed' as const;
- held = { version_id: body.id, cell }; next = null;
- } else if (held && next === null) next = hasFence(body, fence) ? 'fence' : 'absent';
+ held = { version_id: body.id, cell }; next = null; emptied = false;
+ } else if (held) {
+ if (next === null) next = hasFence(body, fence) ? 'fence' : 'absent';
+ if (hasFence(body, fence) && (fence === 'facts' ? parseFactsFence(text).facts.length : parseTakesFence(text).takes.length) === 0) emptied = true;
+ }
}
- return !held ? null : next === 'absent' ? held : 'removed' as const;
+ return !held ? null : next === 'absent' && !emptied ? held : 'removed' as const;
}
/** Every lost row of the selected sources, grouped by page with the numbers they take, and the count of each reason a row is not listed. */
diff --git a/test/repair-lost-fence-rows.test.ts b/test/repair-lost-fence-rows.test.ts
index ca035a07b4..3ac80ddc86 100644
--- a/test/repair-lost-fence-rows.test.ts
+++ b/test/repair-lost-fence-rows.test.ts
@@ -131,6 +131,13 @@ for (const backend of testBackends()) {
expect((await repair(engine)).affected).toBe(0);
}, opts), 180_000);
+ test('a lost row is not listed once the user wrote an explicit empty section after the loss', () => managedBrain(async ({ engine, ctx }) => {
+ await loseRows(ctx);
+ await put(ctx, PAGE(`Alice v4.\n\n${emptyFenceBlock('facts')}\n${emptyFenceBlock('takes')}`));
+ await put(ctx, PAGE(`Alice v5.\n\n${FACTS(ROW(3, B))}\n${TAKES([3, TB])}`));
+ expect((await repair(engine)).affected).toBe(0);
+ }, opts), 180_000);
+
test('an apply after the page changed restores nothing and asks for a new preview', () => managedBrain(async ({ engine, ctx }) => {
await loseRows(ctx);
const preview = await repair(engine);