Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: PGLite keeps its full ANALYZE on small brains and unsampled or grown tables; classify jitOffRead

time-attackc8043c307019
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0605c4cf26..c808002b29 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,7 +32,7 @@ Wave 6 of the efficiency work. Nothing changes in search results, chunk boundari
| unchanged re-import, 3,700 pages | PGLite 5k | 23.7 s | 19.7 s |
| chunk pass, 2,000 CJK prose pages | in-process | 1,214 ms | 180 ms |
-- **Write passes.** Import, sync and reindex refreshed the planner statistics after every pass that changed a page: on Postgres `ANALYZE pages(...)` plus `ANALYZE content_chunks(...)` (about 650 ms at 50k), on PGLite a full `ANALYZE` of every table. A pass that changed fewer than 50 + 10% of pages pages now leaves statistics that already exist alone; PGLite still analyzes any hot table its row deltas mark stale. Larger passes and brains without statistics refresh as before.
+- **Write passes.** Import, sync and reindex refreshed the planner statistics after every pass that changed a page: on Postgres `ANALYZE pages(...)` plus `ANALYZE content_chunks(...)` (about 650 ms at 50k), on PGLite a full `ANALYZE` of every table. A pass that changed fewer than 50 + 10% of pages now leaves statistics that already exist alone; PGLite still analyzes any hot table its row deltas mark stale, and runs its full ANALYZE as before on a brain under 500 pages or when any table holds unsampled rows or grew more than 10% past its last sample. Larger passes and brains without statistics refresh as before.
- **PGLite queue upkeep.** Every short-lived CLI process vacuumed all five persistence queue tables on its first tick (0.6 s per sync at 5k). A queue table is now vacuumed once its heap grows more than 10% past the size its last VACUUM or ANALYZE recorded.
- **Edits.** `gbrain import` and a classic sync now keep every stored chunk row identical to its new chunk in place, as `put_page` and managed sync already did, `--no-embed` included: unchanged chunks keep their vectors, and `embed --stale` re-embeds only the chunks that changed. A never-embedded row stays in place under a chunk that gets no vector either. A one-paragraph edit now deletes and inserts one chunk row instead of every row of the page.
- **Postgres JIT.** Search's backlink count (about 370 candidate ids under query expansion) and `doctor`'s three health statements crossed `jit_above_cost` at 50k, and LLVM compilation was most of their time (backlink count 170 → 20 ms). They now run with JIT off, like the search statements.
diff --git a/src/core/search/projection-statistics.ts b/src/core/search/projection-statistics.ts
index c1e435033a..6e62ae0cb5 100644
--- a/src/core/search/projection-statistics.ts
+++ b/src/core/search/projection-statistics.ts
@@ -116,13 +116,18 @@ export async function withChunkStatisticsRefresh<T>(engine: BrainEngine, changed
*/
async function statisticsStillCurrent(engine: BrainEngine, changedPages: number): Promise<boolean> {
if (!Number.isFinite(changedPages)) return false;
+ // PGLite has no autovacuum, and its full ANALYZE is what covers the tables outside the planner-stats deltas: a brain
+ // under 500 pages (where it is cheap), or a table holding rows it never sampled or grown more than 10% (+8 pages)
+ // past the size it last sampled, refreshes.
const [state] = await engine.executeRaw<{ reltuples: number; columns: boolean }>(
`SELECT c.reltuples::float8 AS reltuples,
- $1::text <> 'postgres' OR (SELECT count(*) FROM pg_stats s WHERE s.schemaname = current_schema()
- AND ((s.tablename = 'pages' AND s.attname = 'deleted_at') OR (s.tablename = 'content_chunks' AND s.attname = 'model'))) = 2 AS columns
+ CASE WHEN $1::text = 'postgres' THEN (SELECT count(*) FROM pg_stats s WHERE s.schemaname = current_schema()
+ AND ((s.tablename = 'pages' AND s.attname = 'deleted_at') OR (s.tablename = 'content_chunks' AND s.attname = 'model'))) = 2
+ ELSE NOT EXISTS (SELECT 1 FROM pg_class t WHERE t.relnamespace = c.relnamespace AND t.relkind = 'r' AND pg_relation_size(t.oid) > 0
+ AND (t.reltuples < 0 OR pg_relation_size(t.oid) / current_setting('block_size')::int > t.relpages * 1.1 + 8)) END AS columns
FROM pg_class c WHERE c.oid = 'pages'::regclass`, [engine.kind]);
const rows = Number(state?.reltuples ?? -1);
- if (!state?.columns || rows <= 0 || changedPages >= 50 + 0.1 * rows) return false;
+ if (!state?.columns || rows <= (engine.kind === 'pglite' ? 500 : 0) || changedPages >= 50 + 0.1 * rows) return false;
return verifyProjectionStatistics(engine).then(() => true, () => false);
}
diff --git a/test/fixtures/goldens/exports/runtime.json b/test/fixtures/goldens/exports/runtime.json
index 31f8d5a112..099f3ab2f9 100644
--- a/test/fixtures/goldens/exports/runtime.json
+++ b/test/fixtures/goldens/exports/runtime.json
@@ -281,6 +281,7 @@
"insertFacts",
"invalidateContentDriftEmbeddings",
"invalidateStaleSignatureEmbeddings",
+ "jitOffRead",
"listActiveTakesForPages",
"listAllPageRefs",
"listAllSources",
diff --git a/test/fixtures/goldens/exports/types.json b/test/fixtures/goldens/exports/types.json
index ee5d9042aa..1652ed3acf 100644
--- a/test/fixtures/goldens/exports/types.json
+++ b/test/fixtures/goldens/exports/types.json
@@ -1137,6 +1137,7 @@
"private engineSql",
"private engineSqlOn",
"private getBulkRetryOpts",
+ "private jitOffRead",
"private onPoisoned",
"private poisonedDiscards",
"private resolveFactsEmbeddingCast",
diff --git a/test/fixtures/goldens/sql-text/_classification.json b/test/fixtures/goldens/sql-text/_classification.json
index 685bfb75fb..8dc8faa989 100644
--- a/test/fixtures/goldens/sql-text/_classification.json
+++ b/test/fixtures/goldens/sql-text/_classification.json
@@ -11,7 +11,7 @@
"out-of-scope:dream-verdicts": 3,
"out-of-scope:eval-capture": 5,
"out-of-scope:ingest-log": 2,
- "out-of-scope:internal-helper (private; exercised through public callers)": 17,
+ "out-of-scope:internal-helper (private; exercised through public callers)": 18,
"out-of-scope:lifecycle (connection, pool, transaction, raw seams)": 15,
"out-of-scope:no-sql-stub": 1,
"out-of-scope:raw-data": 2,
@@ -119,6 +119,7 @@
"insertFacts": "facts",
"invalidateContentDriftEmbeddings": "chunks",
"invalidateStaleSignatureEmbeddings": "chunks",
+ "jitOffRead": "out-of-scope:internal-helper (private; exercised through public callers)",
"listActiveTakesForPages": "takes",
"listAllPageRefs": "pages",
"listAllSources": "sources",
diff --git a/test/helpers/postgres-engine-sql-cases.ts b/test/helpers/postgres-engine-sql-cases.ts
index 11ec5f4b48..23385fc9ed 100644
--- a/test/helpers/postgres-engine-sql-cases.ts
+++ b/test/helpers/postgres-engine-sql-cases.ts
@@ -52,6 +52,7 @@ export const DOMAIN_OF: Record<string, string> = {
'engineSqlOn': OOS.helper,
'rlsScopeBindingEnabled': OOS.helper,
'withScopedReadTransaction': OOS.helper,
+ 'jitOffRead': OOS.helper,
'connect': OOS.lifecycle,
'disconnect': OOS.lifecycle,
'disconnectInternal': OOS.helper,