Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test: edit-arm privacy as its own line of defence; statement budget no longer counts consumer scans or a cache expiring mid-write
garrytand9241740be18
diff --git a/src/core/persistence/publication-deadline.ts b/src/core/persistence/publication-deadline.tsindex 6c80f71ce6..420ef6eb0e 100644--- a/src/core/persistence/publication-deadline.ts+++ b/src/core/persistence/publication-deadline.ts@@ -46,6 +46,9 @@ export async function readPublicationCeilingMs(engine: Pick<BrainEngine, 'getConreturn ceiling.value;}+/** Test seam: the next readPublicationCeilingMs reads the config again. */+export function _resetPublicationCeilingForTest(): void { ceiling = { ...ceiling, at: 0 }; }+/** The idle-in-transaction bound a publish transaction gets: the ceiling, never above the pool's own session value. */export function publicationIdleTimeoutMs(ceilingMs = ceiling.value): number {const session = gucMilliseconds(resolveSessionTimeouts().idle_in_transaction_session_timeout);diff --git a/test/e2e/persistence-statement-budget.test.ts b/test/e2e/persistence-statement-budget.test.tsindex b41d5e8445..7bf63e3bb7 100644--- a/test/e2e/persistence-statement-budget.test.ts+++ b/test/e2e/persistence-statement-budget.test.ts@@ -28,8 +28,8 @@ import { claimWorktree } from '../../src/core/persistence/ownership.ts';import { registerLocalWriter, withVerifiedLocalRegistration, type LocalRegistration } from '../../src/core/persistence/identity.ts';import { disposePersistenceConsumer } from '../../src/core/persistence/service.ts';import { _resetWriteThroughCacheForTest } from '../../src/core/write-through.ts';-import { readWriteSwitchSnapshot } from '../../src/core/persistence/switches.ts';-import { readPublicationCeilingMs } from '../../src/core/persistence/publication-deadline.ts';+import { readWriteSwitchSnapshot, resetWriteSwitches } from '../../src/core/persistence/switches.ts';+import { _resetPublicationCeilingForTest, readPublicationCeilingMs } from '../../src/core/persistence/publication-deadline.ts';interface TraceRecord { t: number; ms: number; conn: number; kind: string; sql: string }type Phase = 'admission' | 'claim' | 'prepare' | 'recovery_record' | 'publication' | 'completion' | 'consumer_scan' | 'wait_poll' | 'effects' | 'other';@@ -55,6 +55,8 @@ const SCAN = [/FROM persistence_effects e JOIN persistence_worktrees w ON w\.id=e\.worktree_id WHERE e\.recovery IS NOT NULL/,/FROM persistence_topology_changes c/, /FROM persistence_worktree_refreshes f JOIN/, /^WITH expired AS/, /page_projection_jobs/,/^WITH purged AS \( DELETE FROM persistence_consumers/,+ // #6650: the consumer's dead-owner claim scan, at most every 5 s on any tick (an idle tick can land inside a slow preparation).+ /^SELECT DISTINCT concat_ws\(':',r\.claim_phase->'owner'->>'pid'/,/^SELECT 1 FROM persistence_effects e WHERE e\.id>\$1::bigint AND e\.kind='embedding' AND e\.state='queued' AND e\.attempts=0/,];/**@@ -137,6 +139,21 @@ function classify(records: TraceRecord[]) {return result;}+test('the consumer\'s dead-owner claim scan landing inside preparation is a consumer scan, not preparation', () => {+ const at = (t: number, conn: number, kind: string, sql: string): TraceRecord => ({ t, ms: 0, conn, kind, sql });+ const records = [+ at(1, 1, 'simple', 'begin'), at(2, 1, 'execute', 'INSERT INTO persistence_requests (id) VALUES ($1)'), at(3, 1, 'simple', 'commit'),+ at(10, 2, 'execute', "UPDATE persistence_requests SET state='running', execution_token=$2::uuid WHERE id=$1"),+ at(11, 4, 'execute', "SELECT config->>'slug_root_mode' AS mode FROM sources WHERE id = $1"),+ at(12, 3, 'execute', "SELECT DISTINCT\n concat_ws(':',r.claim_phase->'owner'->>'pid',r.claim_phase->'owner'->>'nonce',r.claim_phase->'owner'->>'start') AS key,(r.claim_phase->'owner')::text AS owner\n FROM persistence_requests r WHERE r.state='running' LIMIT 100"),+ at(13, 4, 'execute', "SELECT key,value FROM config WHERE key LIKE 'persistence.limits.%'"),+ at(20, 5, 'simple', 'begin'), at(21, 5, 'execute', 'UPDATE persistence_requests SET publication_started=true WHERE id=$1'), at(22, 5, 'simple', 'commit'),+ ];+ const phases = classify(records);+ expect(phases.consumer_scan).toEqual({ statements: 1, describes: 0 });+ expect(phases.prepare).toEqual({ statements: 2, describes: 0 });+});+test('the consumer\'s no-op embedding settle pass landing after the claim commit is a consumer scan, not preparation', () => {const at = (t: number, conn: number, kind: string, sql: string): TraceRecord => ({ t, ms: 0, conn, kind, sql });const records = [@@ -194,7 +211,11 @@ d('#6007 put_page statement budget (Postgres)', () => {for (let i = 0; i < 5; i++) {await settle();// The 5 s config snapshots (write switches, publication ceiling) are refreshed outside the measured window, so a- // slow host whose writes run more than 5 s after the warm-up still measures one write with warm caches.+ // slow host whose writes run more than 5 s after the warm-up still measures one write with warm caches. A read+ // of a snapshot still inside its TTL returns it as is, so both are dropped first: one read 4.9 s earlier would+ // otherwise expire inside the measured write.+ resetWriteSwitches();+ _resetPublicationCeilingForTest();await Promise.all([readWriteSwitchSnapshot(engine), readPublicationCeilingMs(engine)]);const requestId = randomUUID();const started = now();diff --git a/test/proactive-brief.test.ts b/test/proactive-brief.test.tsindex b83c4c3776..664eb93ca1 100644--- a/test/proactive-brief.test.ts+++ b/test/proactive-brief.test.ts@@ -25,6 +25,7 @@ import {recordBriefDelivered,renderBriefLine,resetBriefLedger,+ assembleBrief,sessionStartBrief,turnBrief,withBriefDeadline,@@ -351,6 +352,48 @@ describe('page edits (brains that record a change by editing pages)', () => {expect(body.brief.items[0]!.text).toContain('Tuesday, October 20');});+ test('the edit arm filters a private page itself, even for a counterparty the resolver never produced', async () => {+ const CANARY = ['EDITPAGECANARY', 'Q7'].join('');+ const shapes: Array<[string, Record<string, unknown>]> = [+ ['people/secret-direct', { visibility: 'private' }],+ ['people/derived-direct', { derived_from: 'notes/private-src-direct' }],+ ];+ await page('notes/private-src-direct', 'note', 'Private source', { frontmatter: { visibility: 'private' } });+ for (const [slug, frontmatter] of shapes) {+ await rewrite(slug, 'Hidden Example', 'Hidden.', { frontmatter });+ await rewrite(slug, 'Hidden Example', `Hidden.\n- ${CANARY} moved the call.`, { frontmatter });+ const counterparties = [{ slug, source_id: 'default', display: 'Hidden Example', confidence: 1 }];+ for (const local of [true, false]) {+ expect(await assembleBrief(engine, { trigger: 'lexicon', counterparties, local, clock: clock() })).toBeNull();+ }+ const widened = await assembleBrief(engine, { trigger: 'pull', counterparties, local: true, includePrivate: true, clock: clock() });+ expect(widened!.items.map((i) => i.kind)).toEqual(['edit']);+ expect(widened!.text).toContain(CANARY);+ }+ });++ test('a remote caller never gets an edit from a private page or event, or from a page outside its source', async () => {+ const CANARY = ['REMOTEEDITCANARY', 'W3'].join('');+ await engine.executeRaw(`INSERT INTO sources (id, name, config) VALUES ('aa-other', 'aa-other', '{}'::jsonb) ON CONFLICT (id) DO NOTHING`);+ // The same slug in a source the caller is not bound to, written first and sorting first, so an unscoped page read would find it first.+ await engine.executeRaw(`DELETE FROM pages WHERE source_id = 'default' AND slug = 'people/alice-example'`);+ for (const body of ['Alice Example.', `Alice Example.\n- ${CANARY} other-source edit.`]) {+ expect((await importFromContent(engine, 'people/alice-example', `---\ntype: person\ntitle: Alice Example\n---\n\n${body}\n`, { noEmbed: true, sourceId: 'aa-other' })).status).toBe('imported');+ }+ await seedAlice();+ await rewrite('people/secret-remote', 'Secret Remote Example', 'Secret.', { frontmatter: { visibility: 'private' } });+ await rewrite('people/secret-remote', 'Secret Remote Example', `Secret.\n- ${CANARY} private edit.`, { frontmatter: { visibility: 'private' } });+ await page('events/private-remote', 'event', 'Private event', { frontmatter: { visibility: 'private' } });+ const [{ id: pid }] = await engine.executeRaw<{ id: number }>(`SELECT id FROM pages WHERE source_id = 'default' AND slug = 'people/alice-example'`);+ const [{ id: eid }] = await engine.executeRaw<{ id: number }>(`SELECT id FROM pages WHERE slug = 'events/private-remote'`);+ await engine.executeRaw(`INSERT INTO timeline_entries (page_id, date, source, summary, event_page_id) VALUES ($1, CURRENT_DATE, 'test', $2, $3)`, [pid, `${CANARY} private event`, eid]);+ for (const counterparty of ['Alice Example', 'people/alice-example', 'people/secret-remote', 'Secret Remote Example']) {+ const res = await dispatchToolCall(engine as never, 'context_pack', { counterparty, intent: 'meeting' }, { remote: true, transport: 'stdio', sourceId: 'default' });+ const text = res.content.map((c) => (c as { text?: string }).text ?? '').join('\n');+ expect(text).not.toContain(CANARY);+ }+ });+test('canary fuzz: an edit on a private page, in a fence, or on a private event never surfaces', async () => {let seed = 0x5eed;const rnd = () => { seed = (seed * 1103515245 + 12345) & 0x7fffffff; return seed; };