Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
GBRA-161: the crash-robot model accepts PR 2's fence_absent hold only while it holds, and a parsed fence must clear it
garrytan6b42356cc477
diff --git a/scripts/persistence/generator.ts b/scripts/persistence/generator.tsindex b0cc0a2cc4..ca2b0cccae 100644--- a/scripts/persistence/generator.ts+++ b/scripts/persistence/generator.ts@@ -3,7 +3,7 @@* sequence mixes the real write ops across two sources whose slugs overlap,* a local CLI principal and one authenticated remote agent per source, stale* revisions (competing writers), caller-bound replays and concurrent groups.- * The five irreducible cross-boundary sequences are always present+ * The irreducible cross-boundary sequences are always present* (`crossBoundarySequences`); random sequences add breadth around them.*/import { createHash } from 'node:crypto';@@ -49,7 +49,9 @@ class Builder {* by a stale publication; two sources with overlapping slugs; competing* writers on one page; an authority change while effects are pending* (writer revoked mid-sequence); sync and connector publish racing a direct- * write. Caller-bound replay runs beside them as its own sequence.+ * write; a full overwrite that drops a page's Facts and takes sections, then+ * the parsed fences coming back. Caller-bound replay runs beside them as its+ * own sequence.*/export function crossBoundarySequences(topology: GeneratorTopology, seed = 1): Schedule[] {const [a, b] = topology.sources;@@ -129,6 +131,18 @@ export function crossBoundarySequences(topology: GeneratorTopology, seed = 1): Ss.groups.push([again.id, racing.id]);out.push({ seed, ops: s.ops, groups: s.groups, label: 'sync_and_connector_race_direct_write' });}+ {+ const s = new Builder(seed, 'fd'); const slug = 'notes/alpha';+ const p = s.put('local', a, slug);+ s.next('remember', 'local', a, { fact: `Alpha holds ${s.marker()}`, entity: slug }, [p.id]);+ const take = s.next('takes_add', 'local', a, { slug, claim: `Alpha claim ${s.marker()}`, kind: 'take', holder: 'world' }, [p.id]);+ // A full overwrite without the Facts and takes sections holds both fences and keeps their rows (GBRA-161 PR 2, B-5);+ // republishing the bytes read after the take puts the parsed fences back, and the rows must match them again.+ const dropped = s.put('local', a, slug, '$current', [take.id]);+ s.next('takes_supersede', 'local', a, { slug, row_num: { $ref: take.id, field: 'row_num' }, claim: `Alpha revised ${s.marker()}` }, [dropped.id]);+ s.next('put_page', 'local', a, { slug, content: '$stale_content', stale_of: take.id, expected_revision: '$current' }, [dropped.id]);+ out.push({ seed, ops: s.ops, groups: s.groups, label: 'fence_dropped_then_restored' });+ }return out;}diff --git a/scripts/persistence/model.ts b/scripts/persistence/model.tsindex db196ebf9b..a8dbcc2755 100644--- a/scripts/persistence/model.ts+++ b/scripts/persistence/model.ts@@ -16,6 +16,7 @@ import { EFFECT_FAULT_POINTS, type FaultPoint } from '../../src/core/persistenceimport { operationsByName } from '../../src/core/operations.ts';import { parseFactsFence } from '../../src/core/facts-fence.ts';import { parseTakesFence } from '../../src/core/takes-fence.ts';+import { FACT_FENCE_KIND, TAKE_FENCE_KIND, fenceOwnerKey, readFenceBlocks } from '../../src/core/generations/fence.ts';import { isRegistryCode } from '../../src/core/error-catalogue.ts';import { CONNECTOR_SLUG, contextFor, retryingRead, type OpDescriptor, type OpKind, type OpObservation, type World } from './ops.ts';@@ -338,6 +339,30 @@ export class ReferenceModel {}}+ /** Each page fence's rows the last time they matched the fence, by page key and fence name. */+ private projected = new Map<string, string>();++ /**+ * A fence must list exactly its page's rows, with one exception (GBRA-161 PR 2, B-5): a write that drops the whole+ * section holds the fence `fence_absent` and keeps the rows. That page passes only while it has no fence block, the+ * hold is recorded, and its rows are still the ones it had when they last matched the fence. A parsed fence with the+ * hold still recorded fails, so a hold never outlives the fence that answers it.+ */+ private async checkFence(stage: string, row: { source_id: string; slug: string }, content: string, fence: 'facts' | 'takes', listed: string[], rows: string[]): Promise<void> {+ const k = `${key(row.source_id, row.slug)}\u0000${fence}`;+ const [hold] = await this.q<{ n: number }>(`SELECT count(*)::int AS n FROM derived_generations WHERE source_id=$1 AND kind=$2 AND owner_key=$3+ AND state='held' AND held_reason='fence_absent'`, [row.source_id, fence === 'facts' ? FACT_FENCE_KIND : TAKE_FENCE_KIND, fenceOwnerKey(row.slug)]);+ const held = hold.n > 0;+ const absent = readFenceBlocks({ compiled_truth: content }, fence).state === 'absent';+ if (JSON.stringify(listed) === JSON.stringify(rows)) {+ this.projected.set(k, JSON.stringify(rows));+ if (held && !absent) this.violate({ class: 'projection_drift', detail: `${stage}: ${row.source_id}/${row.slug} ${fence} fence parses but its fence_absent hold is still recorded` });+ return;+ }+ if (absent && held && this.projected.get(k) === JSON.stringify(rows)) return;+ this.violate({ class: 'projection_drift', detail: `${stage}: ${row.source_id}/${row.slug} ${fence} fence ${JSON.stringify(listed)} != rows ${JSON.stringify(rows)}${held ? ' (held fence_absent)' : ''}` });+ }+private async checkProjection(stage: string, live: { id: number; source_id: string; slug: string }[]): Promise<void> {for (const row of live) {const page = await this.readPage('local', row.source_id, row.slug);@@ -347,11 +372,11 @@ export class ReferenceModel {'SELECT row_num,claim,active FROM takes WHERE page_id=$1 ORDER BY row_num', [row.id]);const fence = takes.map(t => `${t.rowNum}:${t.claim}:${t.active}`).sort();const db = dbTakes.map(t => `${t.row_num}:${t.claim}:${t.active}`).sort();- if (JSON.stringify(fence) !== JSON.stringify(db)) this.violate({ class: 'projection_drift', detail: `${stage}: ${row.source_id}/${row.slug} takes fence ${JSON.stringify(fence)} != rows ${JSON.stringify(db)}` });+ await this.checkFence(stage, row, content, 'takes', fence, db);const liveFacts = parseFactsFence(content).facts.filter(f => !f.forgotten && f.validUntil === undefined).map(f => f.claim).sort();const dbFacts = (await this.q<{ fact: string }>(`SELECT fact FROM facts WHERE source_id=$1 AND source_markdown_slug=$2AND expired_at IS NULL AND superseded_by IS NULL AND (valid_until IS NULL OR valid_until > now())`, [row.source_id, row.slug])).map(f => f.fact).sort();- if (JSON.stringify(liveFacts) !== JSON.stringify(dbFacts)) this.violate({ class: 'projection_drift', detail: `${stage}: ${row.source_id}/${row.slug} facts fence ${JSON.stringify(liveFacts)} != rows ${JSON.stringify(dbFacts)}` });+ await this.checkFence(stage, row, content, 'facts', liveFacts, dbFacts);}const [orphans] = await this.q<{ takes: number; timeline: number }>(`SELECT(SELECT count(*)::int FROM takes t WHERE NOT EXISTS (SELECT 1 FROM pages p WHERE p.id=t.page_id)) AS takes,diff --git a/scripts/persistence/robot-driver.ts b/scripts/persistence/robot-driver.tsindex 1145bced4d..6dad276bae 100644--- a/scripts/persistence/robot-driver.ts+++ b/scripts/persistence/robot-driver.ts@@ -1,6 +1,6 @@/*** Driver for the crash robot phase of the persistence gate. For each- * schedule (the five cross-boundary sequences first, then seeded random+ * schedule (the cross-boundary sequences first, then seeded random* sequences until the time budget runs out) it runs a counting pass that* finds every seam the schedule reaches, then one process-separated run per* chosen crash point: a worker freezes at the seam, the driver SIGKILLs it,diff --git a/test/fixtures/crash-robot/fence-absent-hold-keeps-rows.json b/test/fixtures/crash-robot/fence-absent-hold-keeps-rows.jsonnew file mode 100644index 0000000000..ff5a1c0b8a--- /dev/null+++ b/test/fixtures/crash-robot/fence-absent-hold-keeps-rows.json@@ -0,0 +1,71 @@+{+ "description": "A full page overwrite without its Facts or takes section holds the fence fence_absent and keeps its rows (GBRA-161 PR 2, B-5). The reference model still required fence == rows, so every schedule that added a fact or take and then overwrote that page reported projection_drift (seeds 5108, 5109, 5113, 5115, 5116 from master 1d04e8ee). The model now accepts a mismatch only while the page has no fence block, the fence_absent hold is recorded and the rows are unchanged since they last matched; a parsed fence with the hold still recorded fails. fence_dropped_then_restored drops both sections, tries a supersede on the held page, then republishes the fenced bytes: the rows must match the restored fences and the hold must be gone.",+ "engine": "pglite",+ "reproduced_before_fix": "7/7 runs on master 1d04e8ee (4 to 15 projection_drift violations each); 0 on 61e52ec9, the commit before PR 2",+ "failing_runs": [+ {+ "schedule": "random-5108",+ "seed": 5108,+ "length": 24,+ "crashed": false,+ "violations": []+ },+ {+ "schedule": "random-5109",+ "seed": 5109,+ "length": 24,+ "crashed": false,+ "violations": []+ },+ {+ "schedule": "random-5113",+ "seed": 5113,+ "length": 24,+ "crashed": false,+ "violations": []+ },+ {+ "schedule": "random-5115",+ "seed": 5115,+ "length": 24,+ "crashed": false,+ "violations": []+ },+ {+ "schedule": "random-5116",+ "seed": 5116,+ "length": 24,+ "crashed": false,+ "violations": []+ },+ {+ "schedule": "random-5108",+ "seed": 5108,+ "length": 24,+ "fault": {+ "point": "effect:links:mid",+ "nth": 1+ },+ "crashed": true,+ "violations": []+ },+ {+ "schedule": "random-5109",+ "seed": 5109,+ "length": 24,+ "fault": {+ "point": "publication:before_publication",+ "nth": 2+ },+ "crashed": true,+ "violations": []+ },+ {+ "schedule": "fence_dropped_then_restored",+ "seed": 5105,+ "length": 6,+ "crashed": false,+ "violations": []+ }+ ]+}diff --git a/test/persistence-crash-robot.test.ts b/test/persistence-crash-robot.test.tsindex 33f1ed9030..0674f1bcfd 100644--- a/test/persistence-crash-robot.test.ts+++ b/test/persistence-crash-robot.test.ts@@ -63,7 +63,7 @@ describe('generator', () => {test('the cross-boundary sequences are always generated', () => {expect(crossBoundarySequences(ROBOT_TOPOLOGY).map(s => s.label)).toEqual(['withdrawal_then_stale_publication','overlapping_slugs_two_sources', 'competing_writers_one_page', 'caller_bound_replay', 'authority_change_pending_effects',- 'sync_and_connector_race_direct_write']);+ 'sync_and_connector_race_direct_write', 'fence_dropped_then_restored']);});test('a random schedule is a function of its seed and covers every write op', () => {expect(JSON.stringify(randomSchedule(ROBOT_TOPOLOGY, 9))).toBe(JSON.stringify(randomSchedule(ROBOT_TOPOLOGY, 9)));