Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Check each version's own page for quarantine, and stop expiring facts on a put over a deleted page
garrytandc87f95ee256
diff --git a/scripts/function-size-baseline.tsv b/scripts/function-size-baseline.tsvindex b2ce58970a..fccc410a87 100644--- a/scripts/function-size-baseline.tsv+++ b/scripts/function-size-baseline.tsv@@ -49,7 +49,7 @@ src/core/minions/queue.ts MinionQueue.add>result=this.engine.transaction(#0) 359src/core/minions/worker.ts MinionWorker.start 470 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1)src/core/ops/facts.ts recall.handler 358 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +3 #4830 (W14 PR3): every hit names its source_id, falling back to the scope's single source (the federated case stays absent); +3 W5: recall threads the page-unit ranking cut into its hybrid page arm (logic in search/page-rank-cut.ts; delivery reports it); +3 #7043: copies of one claim collapse before the limit cut (the collapse lives in facts/capture-dedup.ts collapseFactReads) and the row names its entity_slugs/collapsed_count; +1 HARN-15: max_chars is parsed before any read (the bound lives in boundRecallChars)src/core/ops/search.ts query.handler 338 +5 GBRA-81 item 5 per-stage search timings: the request's SearchTimings and setup-stage binding, the image branch's limit read hoisted out of the timed vector call, and its response stage wrapper (collector logic in search/stage-timings.ts); seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1); +2 GBRA-49 explain_target: one TargetTrace prep line and its HybridSearchOpts thread (diagnosis/meta live in prepareExplainTarget/finishExplainTarget); +1 W5 (#6445): query passes the page-unit ranking cut to hybrid search (logic in search/page-rank-cut.ts), merged with master growth-src/core/persistence/page-prepare.ts prepareInlineOrSegmentedMutation 305 +3 #7841: a put_page that asks for visibility: private is kept or refused (visibility_not_applied), checked before the no-op test and after the trust stamp (logic in persistence/visibility-request.ts); +2 #9036: a put over a soft-deleted page starts clean, without the deleted page's tags, timeline rows or facts (logic in retireTombstoneFacts)+src/core/persistence/page-prepare.ts prepareInlineOrSegmentedMutation 304 +3 #7841: a put_page that asks for visibility: private is kept or refused (visibility_not_applied), checked before the no-op test and after the trust stamp (logic in persistence/visibility-request.ts); +1 #9036: a put over a soft-deleted page takes its tags from its own content and skips the deleted page's timeline rowssrc/core/progressive-batch/orchestrator.ts runProgressiveBatch 348 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1)src/core/remediation/run.ts runRemediation 428 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1)src/core/skillopt/orchestrator.ts runOptimizationLoop 644 seed: W5 baseline, refactor wave 1 (decomposition deferred to wave 2, plan D1)diff --git a/src/core/ops/admin.ts b/src/core/ops/admin.tsindex 54fb715962..7542cebba1 100644--- a/src/core/ops/admin.ts+++ b/src/core/ops/admin.ts@@ -246,11 +246,11 @@ const get_versions: Operation = {const snapshotTiers = ctx.remote === false || versions.length === 0 ? new Map<number, unknown>() : new Map((await ctx.engine.executeRaw<{ id: number | string; trust_tier: string | null }>('SELECT id, trust_tier FROM page_versions WHERE id = ANY($1::bigint[])', [versions.map(v => Number(v.id))])).map(r => [Number(r.id), r.trust_tier] as const));const reason = await remoteReasons(ctx, String(p.slug), versions);- const [live] = ctx.remote === false || versions.length === 0 ? [] : await ctx.engine.executeRaw<{ slug: string; source_id: string; frontmatter: Record<string, unknown> }>(- 'SELECT slug, source_id, frontmatter FROM pages WHERE id = $1', [Number(versions[0]!.page_id)]);- const quarantine = live ? historyQuarantine(ctx, live) : () => null;+ const lives = ctx.remote === false || versions.length === 0 ? [] : await ctx.engine.executeRaw<{ id: number | string; slug: string; source_id: string; frontmatter: Record<string, unknown> }>(+ 'SELECT id, slug, source_id, frontmatter FROM pages WHERE id = ANY($1::bigint[])', [[...new Set(versions.map(v => Number(v.page_id)))]]);+ const quarantines = new Map(lives.map(live => [Number(live.id), historyQuarantine(ctx, live)] as const));for (const v of versions) {- const quarantined = quarantine(v.frontmatter as Record<string, unknown> | null);+ const quarantined = quarantines.get(Number(v.page_id))?.(v.frontmatter as Record<string, unknown> | null) ?? null;if (quarantined?.body_omitted) {const { compiled_truth: _b, timeline: _t, ...rest } = v;out.push({ ...rest, ...reason(v), quarantined });diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.tsindex 8b59a937d9..39d4be29bb 100644--- a/src/core/persistence/page-prepare.ts+++ b/src/core/persistence/page-prepare.ts@@ -1088,7 +1088,6 @@ async function prepareInlineOrSegmentedMutation(unbounded: BrainEngine, row: Wrilet segmentedWrite = false;if (!noop) {const prior = snapshot ? await storedPageTier(tx, row.source_id, row.slug) : null;- if (replacesTombstone) await retireTombstoneFacts(tx, row.source_id, row.slug);// ENG-1: a managed fence edit keeps the page's tier; CEO-12: an agent rewrite that lowers a page files a queue item.const applied = await (isFenceEditWrite(row) ? withTrustKeep(tx, ['pages'], () => ready.apply(tx, lean ? preimage : undefined)) : ready.apply(tx, lean ? preimage : undefined));projectionPending = applied?.segmented?.complete === false || applied?.projectionPending === true;@@ -1157,18 +1156,6 @@ async function prepareInlineOrSegmentedMutation(unbounded: BrainEngine, row: Wrireturn publication;}-/**- * A put over a soft-deleted page starts clean: it takes its tags from its own content only, skips the deleted page's- * timeline rows, and here expires (never deletes) the active facts the deleted page held and those about it that no live- * page holds. Facts other live pages hold about the slug stay theirs; restore_page is how the old page comes back.- */-async function retireTombstoneFacts(tx: BrainEngine, sourceId: string, slug: string): Promise<void> {- await tx.executeRaw(`UPDATE facts f SET expired_at = now(), row_num = NULL- WHERE f.source_id = $1 AND f.expired_at IS NULL- AND (f.source_markdown_slug = $2 OR (f.entity_slug = $2 AND NOT EXISTS (SELECT 1 FROM pages lp- WHERE lp.source_id = f.source_id AND lp.slug = f.source_markdown_slug AND lp.slug <> $2 AND lp.deleted_at IS NULL)))`, [sourceId, slug]);-}-/** What a page's projections report back: removed timeline rows and the contested proposal refs (#5575 guarded supersession). */function projectedRows(projected: { timelineRowsRemoved?: TimelineRowsRemoved | null; contested?: string[] } | undefined): { removedTimeline: TimelineRowsRemoved | null; contested: string[] } {return { removedTimeline: projected?.timelineRowsRemoved ?? null, contested: projected?.contested ?? [] };diff --git a/test/fact-reads-page-access.test.ts b/test/fact-reads-page-access.test.tsindex b742bda3d7..a5696dd839 100644--- a/test/fact-reads-page-access.test.ts+++ b/test/fact-reads-page-access.test.ts@@ -4,13 +4,13 @@* 1. Protects: a fact, timeline entry, tag or ontology observation is never wider than its page. A remote caller who* cannot read a page (it names readers the caller is not among, or it is visibility: private) gets nothing about it* from any fact read, even when the observation itself is world-visible. A soft-deleted page's facts, timeline and- * tags are hidden from every caller, local included, until restore_page brings them back unchanged, and a new page- * written over the deleted one starts clean.+ * tags are hidden from every caller, local included, until restore_page brings them back unchanged. A new page+ * written over the deleted one takes its tags from its own content; it hides and expires none of the old facts.* 2. Fails when: a world observation about a restricted or private page reaches a caller who cannot read that page* (ontology_get, volunteer_chronicle, delta, find_trajectory, recall, hot memory); a deleted page's facts, timeline* or tags come back from recall, get_timeline, get_tags, ontology_get, find_trajectory, volunteer_chronicle, delta or- * the entity card; a restore changes its facts; a new page at the slug shows the old page's facts or tags; or the- * fix deletes a fact row instead of hiding it.+ * the entity card; a restore changes its facts; a new page at the slug shows the old page's tags; or a fact row is+ * deleted or expired instead of hidden.* 3. Existing coverage: member-read-access pins page reads and facts held on a restricted page (provenance), not facts* about one held elsewhere; pages-soft-delete pins the page row only.* 4. Real seams: tokens minted by the token service and verified by the serve --http verifier, dispatched through MCP@@ -226,13 +226,12 @@ for (const kind of testBackends()) {await c.write('delete_page', { slug, expected_revision: await revision() });await c.write('put_page', { slug, expected_revision: await revision(),content: '---\ntype: person\ntitle: Carol Two\ntags: [new-tag]\n---\n# Carol Two\n\nA different page. canarynewpage' });- expect(await leaks()).toEqual([]);+ const replaced = await leaks();+ expect(replaced.filter(l => l.includes(old.tag))).toEqual([]);+ expect(replaced).toContain(`local recall entity: ${old.fact}`);expect((await c.local('get_tags', { slug })).text).toContain('new-tag');expect((await c.local('get_page', { slug })).text).toContain('canarynewpage');- // The old page's facts are hidden, never deleted: every row is still there, expired.- const after = await rows();- expect(after.map(r => r.id)).toEqual(before.map(r => r.id));- expect(after.every(r => r.expired_at !== null)).toBe(true);+ expect(await rows()).toEqual(before);}describe(`a deleted page takes its facts, timeline and tags with it (${kind})`, () => {@@ -252,7 +251,7 @@ for (const kind of testBackends()) {}, 240_000);afterAll(async () => { __setEmbedTransportForTests(null); resetGateway(); await close?.(); }, 60_000);- test('repo-less source: hidden while deleted for local and remote readers, back unchanged on restore, not inherited by a new page', async () => {+ test('repo-less source: hidden while deleted for local and remote readers, back unchanged on restore, tags not inherited by a new page', async () => {__resetPrivateVisibilityCacheForTests();await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-b', 'source-b') ON CONFLICT (id) DO NOTHING");await deletedPageScenario(engine, 'source-b', true);diff --git a/test/recorded-read-gate.test.ts b/test/recorded-read-gate.test.tsindex 94561efabe..0bdda22efb 100644--- a/test/recorded-read-gate.test.ts+++ b/test/recorded-read-gate.test.ts@@ -2,12 +2,14 @@* Recorded-time reads (get_page / get_versions with known_as_of) by a remote read-scope caller must not serve* what the current-time read withholds from that caller: (1) a quarantined page's body, (2) a page whose* readable_by excludes the caller, (3) a soft-deleted page. The plain version history (4) and delta's belief changes (5)- * keep a quarantined page's body and claims from the same caller. Runs on PGLite, and on Postgres when DATABASE_URL is set.+ * keep a quarantined page's body and claims from the same caller, (6) when the page was quarantined only by a later write too, and+ * (7) per page across granted sources. Runs on PGLite, and on Postgres when DATABASE_URL is set.*/import { describe, expect, test } from 'bun:test';import { operationsByName } from '../src/core/operations.ts';import { testBackends } from './helpers/test-backends.ts';import { beliefBrain, beliefEngineCase, beliefPage, type BeliefBrain } from './helpers/belief-ledger.ts';+import { importFromContent } from '../src/core/import-file.ts';const pause = () => new Promise(resolve => setTimeout(resolve, 20));const at = async (b: BeliefBrain) => {@@ -115,5 +117,21 @@ for (const engineCase of testBackends().map(beliefEngineCase)) {expect(out.text).toContain('canarylatecontrol');expect(out.text.includes('canarylateq')).toBe(false);}), 120_000);++ test('(7) a federated version history: each version follows its own page, so a newer clean page in another source does not open a quarantined page\'s bodies', () => beliefBrain(async b => {+ const slug = 'companies/federated-history-example';+ await b.put(slug, beliefPage({ prose: 'Canary body canaryfedq one.' }));+ await b.put(slug, beliefPage({ title: 'Attention Required! | Cloudflare', prose: 'Canary body canaryfedq two.' }));+ const [row] = await b.engine.executeRaw<{ q: unknown }>(`SELECT frontmatter->'quarantine' AS q FROM pages WHERE source_id = $1 AND slug = $2`, [b.sourceId, slug]);+ expect(row?.q).toMatchObject({ reason: 'junk_pattern' });+ await b.engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-b', 'source-b') ON CONFLICT (id) DO NOTHING");+ await pause();+ await importFromContent(b.engine, slug, beliefPage({ prose: 'Canary body canaryfedclean one.' }), { sourceId: 'source-b', noEmbed: true });+ await b.engine.createVersion(slug, { sourceId: 'source-b' });+ const remote = remoteOf(b);+ const federated = { ...remote, auth: { ...remote.auth, allowedSources: [b.sourceId, 'source-b'] } } as unknown as BeliefBrain['ctx'];+ const out = await call(b, federated, 'get_versions', { slug, include_body: true });+ expect({ ok: out.ok, control: out.text.includes('canaryfedclean'), leaked: out.text.includes('canaryfedq') }).toEqual({ ok: true, control: true, leaked: false });+ }, { databaseUrl: engineCase.databaseUrl }), 120_000);});}