Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
perf(belief): the state read runs with a generic plan so bulk imports stop re-planning it per page (13.16); bulk cost table in section 9
garrytan3dcc924cd6ad
diff --git a/docs/designs/BELIEF_LEDGER.md b/docs/designs/BELIEF_LEDGER.mdindex 22299f723d..7f627df083 100644--- a/docs/designs/BELIEF_LEDGER.md+++ b/docs/designs/BELIEF_LEDGER.md@@ -1093,6 +1093,18 @@ managed sync of 20 typical pages on Postgres costs about 4–5 ms per member.End-to-end on/off medians on the 4-core bench machine vary by ±10 ms betweenidentical runs, so the in-transaction figure is the one to compare.+Bulk work with coverage on, 1000 typical pages plus 20 people pages, 4-core+machine, median of 2 runs, after section 13 item 16:++| Run | PGLite off → on | Postgres off → on |+|---|---|---|+| legacy `gbrain import` | 5.7 → 7.1 s (+1.4 ms/page) | 7.1 → 9.6 s (+2.6 ms/page) |+| re-import, every page edited | 6.3 → 7.6 s (+1.4 ms/page) | 7.4 → 9.4 s (+2.0 ms/page) |+| managed sync, create | 24.8 → 27.6 s (+11%) | 15.1 → 18.7 s (+24%) |+| managed sync, every page edited | 29.2 → 32.7 s (+12%) | within noise |+| `beliefs enable` baseline | 1.2 s (1.2 ms/page) | 1.8 s (1.8 ms/page) |+| `beliefs backfill`, 2 prior versions per page | 9.7 s (25,040 events) | 7.3 s |+Cheaper options, none in W1: skip the post-read of tables a publication didnot write (projections would report what they touched), keep each stream'ssubject snapshot on its lifetime row so the pre read is a key lookup, or@@ -1483,3 +1495,10 @@ Garry accepted those answers. Questions 5, 6, 7 and 8 are decided.`idx_facts_belief_stream (source_id, source_markdown_slug) WHEREexpired_at IS NULL` (v249's handler builds it online), and the post readreuses the pre read's claim fingerprints. Section 9 has the cost.+16. **The state read runs with a generic plan.** Under the default+ `plan_cache_mode` a bulk import kept re-planning the read for every page+ as its tables grew (about 3 ms each), so the ledger's per-page cost rose+ with the size of the import (0.8, 4.0 and 5.5 ms per page at 300, 1000+ and 2000 pages on PGLite). The read now sets `force_generic_plan` for+ that one statement, transaction-local, and restores the previous value;+ the cost stays about 0.6 ms per page at every size.diff --git a/src/core/belief/state.ts b/src/core/belief/state.tsindex 1e6a02dbdf..9e7cc35ff2 100644--- a/src/core/belief/state.ts+++ b/src/core/belief/state.ts@@ -60,11 +60,13 @@ const OWNED_ROWS = (table: 'links' | 'link_transitions', ids: string, columns =export async function readBeliefState(tx: BrainEngine, sourceId: string, slugs: readonly string[], pageIds: readonly number[] = [], known?: RawState): Promise<RawState> {// One statement: each part selects the batch's pages itself ($1 source, $2 slugs, $3 page ids) and returns its rows as JSON.// Fingerprints of claim texts `known` (the batch's pre-read) already has are not recomputed: the SQL fingerprint- // is most of a large page's read.+ // is most of a large page's read. The statement runs with a generic plan (restored after): under the default+ // plan_cache_mode a bulk import re-plans it for every page as the tables grow (about 3 ms each, rising with size).const knownFp = new Map<string, string>([...(known?.facts ?? []).map(f => [f.fact, f.fp] as const), ...(known?.takes ?? []).map(k => [k.claim, k.claim_fp] as const)]);const params = [sourceId, [...slugs], [...pageIds], [...knownFp.keys()]];const STREAM_SLUGS = `$2::text[] || ARRAY(SELECT p.slug FROM pages p WHERE p.source_id = $1 AND p.id = ANY($3::int[]))`;const PAGE_IDS = `ARRAY(SELECT p.id FROM pages p WHERE p.source_id = $1 AND (p.slug = ANY($2::text[]) OR p.id = ANY($3::int[])))`;+ const [mode] = await tx.executeRaw<{ prev: string }>(`SELECT current_setting('plan_cache_mode') AS prev, set_config('plan_cache_mode', 'force_generic_plan', true)`);const [row] = await tx.executeRaw<Record<string, Record<string, unknown>[]>>(`SELECT(SELECT COALESCE(json_agg(q), '[]'::json) FROM (SELECT p.id, p.slug, p.deleted_at IS NOT NULL AS deleted, p.knowledge_revision::text AS revision,@@ -98,6 +100,7 @@ SELECT x.id, x.from_page_id AS f, x.to_page_id AS t, x.origin_page_id AS o,SELECT p.id, p.slug, p.source_id, s.incarnation::text AS incarnation FROM pages p JOIN sources s ON s.id = p.source_idWHERE p.id = ANY(ARRAY(SELECT unnest(ARRAY[e.from_page_id, e.to_page_id, e.origin_page_id])FROM ((${OWNED_ROWS('links', PAGE_IDS, ENDS)}) UNION ALL (${OWNED_ROWS('link_transitions', PAGE_IDS, ENDS)})) e))) q) AS endpoints`, params);+ await tx.executeRaw(`SELECT set_config('plan_cache_mode', $1, true)`, [mode!.prev]);const { pages: pageRows, facts, takes, links, transitions, endpoints } = row!;const byId = new Map(endpoints.map(r => [Number(r.id), { id: Number(r.id), slug: String(r.slug), source_id: String(r.source_id), incarnation: String(r.incarnation) }]));const endpoint = (id: unknown): RawEndpoint | null => id == null ? null : byId.get(Number(id)) ?? null;