Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(segments): markerCarry carries only a split marker, not a whole one
garrytan8f62592ab21b
diff --git a/src/core/page-body/read-range.ts b/src/core/page-body/read-range.tsindex 916cb598d7..61869b8593 100644--- a/src/core/page-body/read-range.ts+++ b/src/core/page-body/read-range.ts@@ -427,7 +427,7 @@ const MARKER_CARRY_MAX = Math.max(FACTS_FENCE_BEGIN.length, FACTS_FENCE_END.lengexport function markerCarry(text: string): string {for (let k = Math.min(text.length, MARKER_CARRY_MAX); k > 0; k--) {const tail = text.slice(-k);- if (FACTS_FENCE_BEGIN.startsWith(tail) || FACTS_FENCE_END.startsWith(tail)) return tail;+ if ((k < FACTS_FENCE_BEGIN.length && FACTS_FENCE_BEGIN.startsWith(tail)) || (k < FACTS_FENCE_END.length && FACTS_FENCE_END.startsWith(tail))) return tail;}return '';}diff --git a/test/large-pages/segmented-truth-end-fence.test.ts b/test/large-pages/segmented-truth-end-fence.test.tsnew file mode 100644index 0000000000..39a49079ef--- /dev/null+++ b/test/large-pages/segmented-truth-end-fence.test.ts@@ -0,0 +1,43 @@+/**+ * GBRA-133: a segmented page whose truth ends with a facts fence reads back whole for a trusted caller when the source+ * has withdrawals.+ *+ * 1. Protects: the trusted stored read joins segments only around a marker a cut splits (markerCarry carries a proper+ * prefix of a marker, never a whole one), so the truth's last segment is not glued to the timeline's first, where+ * the end marker would read as quoted prose and the fence as crossing a forced cut.+ * 2. Fails when: markerCarry returns a complete end marker (the read refuses with page_projection_stale, or keeps a+ * purged row).+ * 3. PGLite.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { FACTS_FENCE_BEGIN, FACTS_FENCE_END } from '../../src/core/facts-fence.ts';+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';+import { markerCarry, openSegmentedBody } from '../../src/core/page-body/read-range.ts';+import { mulberry32 } from '../helpers/seeded-random.ts';+import { PRIVATE, importSegmented, overlaidStoredBody, pageContent, privacyFacts, privacyText, readRange, seedLedger } from '../helpers/segmented-privacy-fixture.ts';++let engine: PGLiteEngine;+beforeAll(async () => { engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema(); }, 120_000);+afterAll(async () => { await engine.disconnect(); });++describe('a facts fence at the end of a segmented truth', () => {+ test('markerCarry carries only a marker a cut splits', () => {+ expect(markerCarry(`row\n${FACTS_FENCE_END}`)).toBe('');+ expect(markerCarry(`row\n${FACTS_FENCE_BEGIN}`)).toBe('');+ expect(markerCarry(`row\n${FACTS_FENCE_END.slice(0, 12)}`)).toBe(FACTS_FENCE_END.slice(0, 12));+ expect(markerCarry(`row\n${FACTS_FENCE_BEGIN.slice(0, -1)}`)).toBe(FACTS_FENCE_BEGIN.slice(0, -1));+ });++ test('the trusted stored read equals the overlaid stored body, purged row dropped', async () => {+ const slug = 'notes/fence-at-truth-end';+ const pageId = await importSegmented(engine, slug, pageContent(`${privacyText(mulberry32(7), 1_100_000, { keys: false })}\n${privacyFacts()}`, 'notes on the timeline\n'));+ await seedLedger(engine, slug);+ const stored = await overlaidStoredBody(engine, slug, pageId);+ expect(stored.compiled_truth.endsWith(FACTS_FENCE_END)).toBe(true);+ expect(stored.compiled_truth).not.toContain(PRIVATE.purged);+ const body = await openSegmentedBody(engine, { pageId });+ expect(body.rows.some((r, i) => r.region === 'truth' && body.rows[i + 1]?.region === 'timeline')).toBe(true);+ const trusted = await readRange(engine, { pageId, space: 'stored', start: 0, end: Infinity, audience: 'trusted', body });+ expect(trusted.bytes.toString('utf8')).toBe(stored.compiled_truth + stored.timeline);+ }, 120_000);+});