Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(find,stats): keep untrusted find/stats reads off a new pool hold (#1794 guard); pin the new engine methods in the RLS inventory, gauge, SQL-text and surface goldens (#7191)
garrytan95c02e9962e3
diff --git a/src/core/engine-sql/page-lookup.ts b/src/core/engine-sql/page-lookup.tsindex aed728b92c..3f43f32a73 100644--- a/src/core/engine-sql/page-lookup.ts+++ b/src/core/engine-sql/page-lookup.ts@@ -19,8 +19,6 @@* in the C collation so both engines and every database locale agree; the* distinct-slug counts use it too (a C sort is several times faster than a* locale sort, and both collations are deterministic, so counts match).- * Postgres runs untrusted reads with JIT off (as #6276): the read-policy- * subplans push their cost estimate past the JIT thresholds.*/import type { FindPagesQuery, FoundPage, FoundPages, UntrustedReadPolicy } from '../page-find.ts';import type { PageCountQuery, PageCounts, TypeGrouper } from '../page-counts.ts';diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.tsindex d951140322..1965c12aff 100644--- a/src/core/postgres-engine.ts+++ b/src/core/postgres-engine.ts@@ -938,10 +938,10 @@ export class PostgresEngine implements BrainEngine {return this.withScopedReadTransaction(filters?.sourceIds, filters?.sourceId, tx => pagesImpl.listPages(scopedRead(this.engineSqlOn(tx)), filters));}async findPages(query: FindPagesQuery): Promise<FoundPages> {- return this.withScopedReadTransaction(query.sourceIds, query.sourceId, tx => pageLookupImpl.findPages(scopedRead(this.engineSqlOn(tx)), query), query.untrusted ? { alwaysTransaction: true, jitOff: true } : undefined);+ return this.withScopedReadTransaction(query.sourceIds, query.sourceId, tx => pageLookupImpl.findPages(scopedRead(this.engineSqlOn(tx)), query));}async countPagesBy(query: PageCountQuery, groupTypes?: TypeGrouper): Promise<PageCounts> {- return this.withScopedReadTransaction(query.sourceIds, query.sourceId, tx => pageLookupImpl.countPagesBy(scopedRead(this.engineSqlOn(tx)), query, groupTypes), query.untrusted ? { alwaysTransaction: true, jitOff: true } : undefined);+ return this.withScopedReadTransaction(query.sourceIds, query.sourceId, tx => pageLookupImpl.countPagesBy(scopedRead(this.engineSqlOn(tx)), query, groupTypes));}async getAllSlugs(opts?: { sourceId?: string }): Promise<Set<string>> {diff --git a/test/engine-surface-coverage.test.ts b/test/engine-surface-coverage.test.tsindex 1fc9c4ee6b..b5fc10e0a1 100644--- a/test/engine-surface-coverage.test.ts+++ b/test/engine-surface-coverage.test.ts@@ -51,7 +51,7 @@ const INTERFACE_METHODS: readonly string[] = ['connect', 'disconnect', 'reconnect', 'initSchema', 'transaction', 'transactionDirect', 'registerBeforeDisconnect', 'withReservedConnection',// Pages CRUD'getPage', 'readPageSnapshot', 'readPageSnapshotsBatch', 'lockPageKeys', 'putPage', 'findDuplicatePage', 'findDuplicatePages', 'deletePage', 'deletePages', 'resolveSlugsByPaths',- 'softDeletePage', 'softDeletePages', 'restorePage', 'purgeDeletedPages', 'listPages', 'resolveSlugs', 'getAllSlugs',+ 'softDeletePage', 'softDeletePages', 'restorePage', 'purgeDeletedPages', 'listPages', 'findPages', 'countPagesBy', 'resolveSlugs', 'getAllSlugs','listAllPageRefs', 'listAllSources', 'updateSourceConfig', 'listPrefixSampledPages', 'listCorpusSample',// Search'searchKeyword', 'countKeywordPages', 'searchKeywordPages', 'searchTitles', 'searchVector', 'getEmbeddingsByChunkIds',diff --git a/test/fixtures/goldens/postgres-engine-gauge/acquires-by-case.json b/test/fixtures/goldens/postgres-engine-gauge/acquires-by-case.jsonindex df59363358..f933ea2483 100644--- a/test/fixtures/goldens/postgres-engine-gauge/acquires-by-case.json+++ b/test/fixtures/goldens/postgres-engine-gauge/acquires-by-case.json@@ -26,6 +26,8 @@"consolidateFact#default": {},"countChunklessPagesWithContent#default": {},"countChunklessPagesWithContent#sourceId": {},+ "countPagesBy#grouped+since+remote": {},+ "countPagesBy#totals": {},"countStaleChunks#default": {"raw": 1},@@ -72,6 +74,8 @@"findOrphanPages#inbound": {},"findOrphanPages#sourceId": {},"findOrphanPages#sourceIds+excludePrivate": {},+ "findPages#glob+types+sourceIds+remote": {},+ "findPages#name": {},"findTrajectory#default": {},"findTrajectory#event": {},"findTrajectory#sourceIds+metric+since+until": {},diff --git a/test/fixtures/goldens/rls-scope-inventory.json b/test/fixtures/goldens/rls-scope-inventory.jsonindex c4452a8752..7bb8ee2b51 100644--- a/test/fixtures/goldens/rls-scope-inventory.json+++ b/test/fixtures/goldens/rls-scope-inventory.json@@ -159,6 +159,32 @@"src-a"]},+ "countPagesBy#grouped+since+remote": {+ "flagOffLanes": [+ "pool"+ ],+ "flagOffOpensTransaction": false,+ "flagOn": "scoped",+ "flagOnLanes": [+ "tx"+ ],+ "flagOnScopes": [+ "src-a"+ ]+ },+ "countPagesBy#totals": {+ "flagOffLanes": [+ "pool"+ ],+ "flagOffOpensTransaction": false,+ "flagOn": "scoped",+ "flagOnLanes": [+ "tx"+ ],+ "flagOnScopes": [+ "*"+ ]+ },"countStaleChunks#default": {"flagOffLanes": ["pool"@@ -592,6 +618,32 @@],"flagOnScopes": []},+ "findPages#glob+types+sourceIds+remote": {+ "flagOffLanes": [+ "pool"+ ],+ "flagOffOpensTransaction": false,+ "flagOn": "scoped",+ "flagOnLanes": [+ "tx"+ ],+ "flagOnScopes": [+ "src-a,src-b"+ ]+ },+ "findPages#name": {+ "flagOffLanes": [+ "pool"+ ],+ "flagOffOpensTransaction": false,+ "flagOn": "scoped",+ "flagOnLanes": [+ "tx"+ ],+ "flagOnScopes": [+ "*"+ ]+ },"findTrajectory#default": {"flagOffLanes": ["pool"@@ -3550,16 +3602,18 @@}},"static": {- "callSites": 33,+ "callSites": 35,"directMethods": {"_searchKeywordCJK": 1,"countChunklessPagesWithContent": 1,"countKeywordPages": 1,+ "countPagesBy": 1,"countStaleChunks": 1,"countStaleFrontmatterKeyPages": 1,"countStalePagesForExtraction": 1,"findDuplicatePage": 1,"findDuplicatePages": 1,+ "findPages": 1,"getAllSlugs": 1,"getBacklinks": 1,"getChunkPage": 1,@@ -3608,6 +3662,7 @@"consolidateFact": "none","countChunklessPagesWithContent": "direct","countKeywordPages": "direct",+ "countPagesBy": "direct","countStaleChunks": "direct","countStaleFrontmatterKeyPages": "direct","countStalePagesForExtraction": "direct",@@ -3637,6 +3692,7 @@"findDuplicatePages": "direct","findOntologyConflicts": "none","findOrphanPages": "none",+ "findPages": "direct","findTrajectory": "none","flipBodyGeneration": "none","getAdjacencyBoosts": "none",diff --git a/test/fixtures/goldens/sql-text/_driver.json b/test/fixtures/goldens/sql-text/_driver.jsonindex ab6820067c..44e5c0631b 100644--- a/test/fixtures/goldens/sql-text/_driver.json+++ b/test/fixtures/goldens/sql-text/_driver.json@@ -2920,14 +2920,6 @@"pages": {"countPagesBy": {"grouped+since+remote": [- {- "paramCount": 1,- "unsafeOptions": {- "prepare": false,- "simple": false- },- "via": "unsafe"- },{"paramCount": 2,"unsafeOptions": {@@ -3169,14 +3161,6 @@},"findPages": {"glob+types+sourceIds+remote": [- {- "paramCount": 1,- "unsafeOptions": {- "prepare": false,- "simple": false- },- "via": "unsafe"- },{"paramCount": 5,"unsafeOptions": {diff --git a/test/fixtures/goldens/sql-text/pages.json b/test/fixtures/goldens/sql-text/pages.jsonindex 16345300b9..e46a293cec 100644--- a/test/fixtures/goldens/sql-text/pages.json+++ b/test/fixtures/goldens/sql-text/pages.json@@ -5,19 +5,7 @@"countPagesBy": {"grouped+since+remote": [{- "event": "begin",- "lane": "tx"- },- {- "lane": "tx",- "params": [- "string"- ],- "sql": "SELECT set_config('jit', $1, true)",- "textSha256": "78f6db2efa50e2a77fa5136eed9ecd3939e1cdf280cd713c488292f4d39ce91c"- },- {- "lane": "tx",+ "lane": "pool","params": ["string","string"@@ -26,7 +14,7 @@"textSha256": "59392b963e454bef730e54f1438ca00274f00cc67238e37689216e99f76f4e00"},{- "lane": "tx",+ "lane": "pool","params": ["array<>","array<>",@@ -35,10 +23,6 @@],"sql": "SELECT m.grp, GROUPING(m.grp) = 1 AS is_total, count(*) AS pages, count(DISTINCT p.slug COLLATE \"C\") AS distinct_slugs FROM pages p JOIN sources s ON s.id = p.source_id JOIN unnest($1::text[], $2::text[]) AS m(type, grp) ON m.type = p.type WHERE p.deleted_at IS NULL AND NOT s.archived AND p.source_id = $3 AND ((COALESCE(p.frontmatter->>'visibility', CASE WHEN (p.type IN ('atom', 'question') OR (p.type = 'concept' AND p.frontmatter->>'synthesized_by' IS NOT NULL)) THEN 'private' ELSE 'world' END) <> 'private' AND NOT COALESCE(p.frontmatter ? 'pinned_question', false)) AND NOT (CASE WHEN p.type = 'atom' THEN EXISTS (SELECT 1 FROM pages derived_origin WHERE derived_origin.source_id = p.source_id AND derived_origin.slug = p.frontmatter->>'source_slug' AND derived_origin.frontmatter->>'visibility' = 'private') WHEN (p.type IN ('atom', 'question') OR (p.type = 'concept' AND p.frontmatter->>'synthesized_by' IS NOT NULL)) THEN EXISTS (SELECT 1 FROM links derived_input_link JOIN pages derived_input ON derived_input.id = derived_input_link.to_page_id WHERE derived_input_link.from_page_id = p.id AND derived_input_link.link_source = 'concept-provenance' AND derived_input_link.link_type = 'synthesized_from' AND (COALESCE(derived_input.frontmatter->>'visibility', CASE WHEN derived_input.type = 'atom' THEN 'private' ELSE 'world' END) = 'private' OR EXISTS (SELECT 1 FROM pages derived_input_origin WHERE derived_input_origin.source_id = derived_input.source_id AND derived_input_origin.slug = derived_input.frontmatter->>'source_slug' AND derived_input_origin.frontmatter->>'visibility' = 'private'))) WHEN p.type = 'event' AND p.frontmatter->>'captured_via' LIKE 'life-chronicle:%' THEN NOT EXISTS (SELECT 1 FROM pages chronicle_origin WHERE chronicle_origin.source_id = p.source_id AND chronicle_origin.slug = p.frontmatter->'event'->>'depth' AND (COALESCE(chronicle_origin.frontmatter->>'visibility', CASE WHEN (chronicle_origin.type IN ('atom', 'question') OR (chronicle_origin.type = 'concept' AND chronicle_origin.frontmatter->>'synthesized_by' IS NOT NULL)) THEN 'private' ELSE 'world' END) <> 'private' AND NOT COALESCE(chronicle_origin.frontmatter ? 'pinned_question', false)) AND NOT (CASE WHEN jsonb_typeof(chronicle_origin.frontmatter->'derived_from') IN ('array', 'string') THEN EXISTS ( SELECT 1 FROM pages declared_origin WHERE declared_origin.source_id = chronicle_origin.source_id AND declared_origin.frontmatter->>'visibility' = 'private' AND declared_origin.slug = ANY(ARRAY( SELECT regexp_replace(declared.slug, '\\.md$', '') FROM jsonb_array_elements_text( CASE WHEN jsonb_typeof(chronicle_origin.frontmatter->'derived_from') = 'array' THEN chronicle_origin.frontmatter->'derived_from' ELSE jsonb_build_array(chronicle_origin.frontmatter->'derived_from') END) AS declared(slug)))) ELSE false END)) ELSE false END) AND NOT (CASE WHEN jsonb_typeof(p.frontmatter->'derived_from') IN ('array', 'string') THEN EXISTS ( SELECT 1 FROM pages declared_origin WHERE declared_origin.source_id = p.source_id AND declared_origin.frontmatter->>'visibility' = 'private' AND declared_origin.slug = ANY(ARRAY( SELECT regexp_replace(declared.slug, '\\.md$', '') FROM jsonb_array_elements_text( CASE WHEN jsonb_typeof(p.frontmatter->'derived_from') = 'array' THEN p.frontmatter->'derived_from' ELSE jsonb_build_array(p.frontmatter->'derived_from') END) AS declared(slug)))) ELSE false END)) AND p.deleted_at IS NULL AND NOT (COALESCE(p.frontmatter, '{}'::jsonb) ? 'quarantine') AND EXISTS (SELECT 1 FROM sources read_source WHERE read_source.id = p.source_id AND NOT read_source.archived) AND COALESCE(p.effective_date, p.updated_at, p.created_at) >= $4::text::timestamptz GROUP BY GROUPING SETS ((m.grp), ())","textSha256": "227b4e785a94b61ccf7d279f5466c91a2f9ee4fc17d62a5408094f72900cafdd"- },- {- "event": "commit",- "lane": "tx"}],"totals": [@@ -290,19 +274,7 @@"findPages": {"glob+types+sourceIds+remote": [{- "event": "begin",- "lane": "tx"- },- {- "lane": "tx",- "params": [- "string"- ],- "sql": "SELECT set_config('jit', $1, true)",- "textSha256": "78f6db2efa50e2a77fa5136eed9ecd3939e1cdf280cd713c488292f4d39ce91c"- },- {- "lane": "tx",+ "lane": "pool","params": ["string","string",@@ -312,10 +284,6 @@],"sql": "SELECT p.slug, p.source_id, p.type, p.title, p.source_path, to_char(p.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') AS updated_at, count(*) OVER () AS total FROM pages p JOIN sources s ON s.id = p.source_id WHERE (p.slug ILIKE $1 OR p.source_path ILIKE $2) AND p.deleted_at IS NULL AND NOT s.archived AND p.source_id = ANY($3::text[]) AND ((COALESCE(p.frontmatter->>'visibility', CASE WHEN (p.type IN ('atom', 'question') OR (p.type = 'concept' AND p.frontmatter->>'synthesized_by' IS NOT NULL)) THEN 'private' ELSE 'world' END) <> 'private' AND NOT COALESCE(p.frontmatter ? 'pinned_question', false)) AND NOT (CASE WHEN p.type = 'atom' THEN EXISTS (SELECT 1 FROM pages derived_origin WHERE derived_origin.source_id = p.source_id AND derived_origin.slug = p.frontmatter->>'source_slug' AND derived_origin.frontmatter->>'visibility' = 'private') WHEN (p.type IN ('atom', 'question') OR (p.type = 'concept' AND p.frontmatter->>'synthesized_by' IS NOT NULL)) THEN EXISTS (SELECT 1 FROM links derived_input_link JOIN pages derived_input ON derived_input.id = derived_input_link.to_page_id WHERE derived_input_link.from_page_id = p.id AND derived_input_link.link_source = 'concept-provenance' AND derived_input_link.link_type = 'synthesized_from' AND (COALESCE(derived_input.frontmatter->>'visibility', CASE WHEN derived_input.type = 'atom' THEN 'private' ELSE 'world' END) = 'private' OR EXISTS (SELECT 1 FROM pages derived_input_origin WHERE derived_input_origin.source_id = derived_input.source_id AND derived_input_origin.slug = derived_input.frontmatter->>'source_slug' AND derived_input_origin.frontmatter->>'visibility' = 'private'))) WHEN p.type = 'event' AND p.frontmatter->>'captured_via' LIKE 'life-chronicle:%' THEN NOT EXISTS (SELECT 1 FROM pages chronicle_origin WHERE chronicle_origin.source_id = p.source_id AND chronicle_origin.slug = p.frontmatter->'event'->>'depth' AND (COALESCE(chronicle_origin.frontmatter->>'visibility', CASE WHEN (chronicle_origin.type IN ('atom', 'question') OR (chronicle_origin.type = 'concept' AND chronicle_origin.frontmatter->>'synthesized_by' IS NOT NULL)) THEN 'private' ELSE 'world' END) <> 'private' AND NOT COALESCE(chronicle_origin.frontmatter ? 'pinned_question', false)) AND NOT (CASE WHEN jsonb_typeof(chronicle_origin.frontmatter->'derived_from') IN ('array', 'string') THEN EXISTS ( SELECT 1 FROM pages declared_origin WHERE declared_origin.source_id = chronicle_origin.source_id AND declared_origin.frontmatter->>'visibility' = 'private' AND declared_origin.slug = ANY(ARRAY( SELECT regexp_replace(declared.slug, '\\.md$', '') FROM jsonb_array_elements_text( CASE WHEN jsonb_typeof(chronicle_origin.frontmatter->'derived_from') = 'array' THEN chronicle_origin.frontmatter->'derived_from' ELSE jsonb_build_array(chronicle_origin.frontmatter->'derived_from') END) AS declared(slug)))) ELSE false END)) ELSE false END) AND NOT (CASE WHEN jsonb_typeof(p.frontmatter->'derived_from') IN ('array', 'string') THEN EXISTS ( SELECT 1 FROM pages declared_origin WHERE declared_origin.source_id = p.source_id AND declared_origin.frontmatter->>'visibility' = 'private' AND declared_origin.slug = ANY(ARRAY( SELECT regexp_replace(declared.slug, '\\.md$', '') FROM jsonb_array_elements_text( CASE WHEN jsonb_typeof(p.frontmatter->'derived_from') = 'array' THEN p.frontmatter->'derived_from' ELSE jsonb_build_array(p.frontmatter->'derived_from') END) AS declared(slug)))) ELSE false END)) AND p.deleted_at IS NULL AND NOT (COALESCE(p.frontmatter, '{}'::jsonb) ? 'quarantine') AND EXISTS (SELECT 1 FROM sources read_source WHERE read_source.id = p.source_id AND NOT read_source.archived) AND p.type = ANY($4::text[]) AND NOT (p.slug LIKE 'test/%' OR p.slug LIKE '.raw/%') ORDER BY p.slug COLLATE \"C\", p.source_id COLLATE \"C\" LIMIT $5","textSha256": "0dac08a52c2874ca4ef564ae13f2c4bea9c87a3577f69e50581c968de7eeb84a"- },- {- "event": "commit",- "lane": "tx"}],"name": [diff --git a/test/postgres-rls-scope-inventory.test.ts b/test/postgres-rls-scope-inventory.test.tsindex 837c5a8b77..3ca66f5687 100644--- a/test/postgres-rls-scope-inventory.test.ts+++ b/test/postgres-rls-scope-inventory.test.ts@@ -180,8 +180,8 @@ describe('EO4 RLS scope inventory (master)', () => {const scoping = astScoping(capture.ast);const callSites = Object.values(capture.ast).reduce((a, m) => a + m.directCallSites, 0);- expect(callSites).toBe(33);- expect(Object.values(capture.ast).filter((m) => m.directCallSites > 0).length).toBe(33);+ expect(callSites).toBe(35);+ expect(Object.values(capture.ast).filter((m) => m.directCallSites > 0).length).toBe(35);const byMethod = new Map<string, RuntimeObservation[]>();for (const [key, obs] of Object.entries(capture.runtime)) {