Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(#7632): leave procedures out of the pg_temp hardening; e2e rewinds to v276's own version
garrytan77f6ce4a70ff
diff --git a/docs/architecture/key-files/commands-4-continued.md b/docs/architecture/key-files/commands-4-continued.mdindex 5f8a3b5d6a..06cee44b7e 100644--- a/docs/architecture/key-files/commands-4-continued.md+++ b/docs/architecture/key-files/commands-4-continued.md@@ -28,7 +28,7 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL- `src/commands/reindex-vectors.ts` — `gbrain reindex --vectors [--dry-run] [--json]` (dispatched from cli.ts beside `--aliases`). Lists every HNSW index in the current schema from `pg_indexes` and runs `REINDEX INDEX` on each (`REINDEX INDEX CONCURRENTLY` on Postgres), rebuilding the ANN index from stored vectors with no re-embedding; each rebuild runs inside `withHnswBuildMemory` (vector-index.ts), which sizes PGLite's `maintenance_work_mem` to the graph or refuses with `pglite_vector_index_too_large`. The PGLite WAL-repair notice (`buildWalRepairNotice`) and `gbrain pglite-repair` name it, because a WAL reset does not rebuild indexes (#4616). Tests: `test/embedding-zero-norm-4616.serial.test.ts` on both engines.-- `src/commands/reindex-search-vector.ts` — `gbrain reindex-search-vector [--dry-run] [--yes] [--json]`. Escape hatch for changing `GBRAIN_FTS_LANGUAGE` after the `configurable_fts_language` migration has run (the migration shows applied and is skipped): recreates `update_page_search_vector` + `update_chunk_search_vector` with the configured language through `pageSearchVectorTriggerFnSql`/`chunkSearchVectorTriggerFnSql` (`fts-language.ts`; they KEEP the `SET search_path = pg_catalog, public` hardening, which CREATE OR REPLACE resets) — then backfills `pages` and `content_chunks` through `src/core/search-vector-backfill.ts` in id-keyset batches of `BACKFILL_BATCH_SIZE` (5000). Pages: an UPDATE-to-self of `id` reads each batch (`RETURNING id, timeline, knowledge_revision`; row locks only for that statement), the timeline is sanitized with `sanitizeRemoteBody` as the seal does, and one `UPDATE … FROM unnest(…)` writes `search_vector` through `pageSearchVectorSql` (the seal's own expression), guarded by the revision read, so a page edited in between is skipped (its seal reindexes it) and private, withdrawn, takes or malformed fence text never enters the vector. Chunks: direct vector recompute via `UPDATE … WHERE id IN (SELECT … LIMIT n) RETURNING id`. It then replaces `idx_facts_fts` (v237) under the language through `ensureFactsFtsIndex` (`src/core/facts/fts-index.ts`): the new index is built beside the old one (CONCURRENTLY on Postgres) and renamed into place, so writes never wait and search keeps an index (no row rewrite). Both stream phases `reindex_search_vector.pages`/`.chunks` through the shared progress reporter (stderr). Consent gate (agent operator wave C2): `requireConsent` (effect `destructive`, not plan-bound: derived index, the undo is a re-run under the previous language) — `--yes` or a TTY prompt; `--json` never implies it, and a refusal changes nothing, returns `status: 'confirmation_required'` and exits 3 with the consent payload. Interrupted-run safety: stamps `fts.reindex_in_progress` (= target language, key exported as `FTS_REINDEX_MARKER_KEY` from `fts-language.ts`) in the config table BEFORE the autocommitted DDL and clears it only after both backfills return; each batch persists its keyset cursor under `backfill.fts_pages.last_id` / `backfill.fts_content_chunks.last_id` (the `checkpointKey` convention from `backfill-base.ts`), so a re-run with the same language resumes from the checkpoint and a different language resets it. Deliberately not one `engine.transaction()` (whole-run row locks on Postgres, zero banked progress on a hang). Idempotent. Pinned by `test/reindex-search-vector.serial.test.ts` and `test/reindex-search-vector-pages.serial.test.ts`.+- `src/commands/reindex-search-vector.ts` — `gbrain reindex-search-vector [--dry-run] [--yes] [--json]`. Escape hatch for changing `GBRAIN_FTS_LANGUAGE` after the `configurable_fts_language` migration has run (the migration shows applied and is skipped): recreates `update_page_search_vector` + `update_chunk_search_vector` with the configured language through `pageSearchVectorTriggerFnSql`/`chunkSearchVectorTriggerFnSql` (`fts-language.ts`; CREATE OR REPLACE resets their pinned `search_path = pg_catalog, public`, so the command then runs `FUNCTION_SEARCH_PATH_HARDENING_SQL` from `src/core/function-search-path.ts` to put `pg_temp` back last, #7632; keep that call after the recreate) — then backfills `pages` and `content_chunks` through `src/core/search-vector-backfill.ts` in id-keyset batches of `BACKFILL_BATCH_SIZE` (5000). Pages: an UPDATE-to-self of `id` reads each batch (`RETURNING id, timeline, knowledge_revision`; row locks only for that statement), the timeline is sanitized with `sanitizeRemoteBody` as the seal does, and one `UPDATE … FROM unnest(…)` writes `search_vector` through `pageSearchVectorSql` (the seal's own expression), guarded by the revision read, so a page edited in between is skipped (its seal reindexes it) and private, withdrawn, takes or malformed fence text never enters the vector. Chunks: direct vector recompute via `UPDATE … WHERE id IN (SELECT … LIMIT n) RETURNING id`. It then replaces `idx_facts_fts` (v237) under the language through `ensureFactsFtsIndex` (`src/core/facts/fts-index.ts`): the new index is built beside the old one (CONCURRENTLY on Postgres) and renamed into place, so writes never wait and search keeps an index (no row rewrite). Both stream phases `reindex_search_vector.pages`/`.chunks` through the shared progress reporter (stderr). Consent gate (agent operator wave C2): `requireConsent` (effect `destructive`, not plan-bound: derived index, the undo is a re-run under the previous language) — `--yes` or a TTY prompt; `--json` never implies it, and a refusal changes nothing, returns `status: 'confirmation_required'` and exits 3 with the consent payload. Interrupted-run safety: stamps `fts.reindex_in_progress` (= target language, key exported as `FTS_REINDEX_MARKER_KEY` from `fts-language.ts`) in the config table BEFORE the autocommitted DDL and clears it only after both backfills return; each batch persists its keyset cursor under `backfill.fts_pages.last_id` / `backfill.fts_content_chunks.last_id` (the `checkpointKey` convention from `backfill-base.ts`), so a re-run with the same language resumes from the checkpoint and a different language resets it. Deliberately not one `engine.transaction()` (whole-run row locks on Postgres, zero banked progress on a hang). Idempotent. Pinned by `test/reindex-search-vector.serial.test.ts` and `test/reindex-search-vector-pages.serial.test.ts`.- `src/commands/init-mode-picker.ts` — install-time mode recommendation and mandatory operator choice. `recommendModeFor` recommends `tokenmax` with a chat key, `balanced` with a ready reranker and no chat key, and `conservative` for a Haiku subagent or when neither exists; `resolveModePickerInputs` reads keys through `mergedProviderEnv` (so `~/.gbrain/config.json` keys count) and the reranker through `rerankerReadiness`. `gbrain init --search-mode <mode>` (`searchModeChoiceFromArgs`) records the operator's choice, suppresses the first-run `search_mode` ask and refuses on a brain with another mode unless `--force`; picker choices record `search.mode_source: auto`, explicit ones `operator`. Reasons, the `[AGENT]` banner and the cost matrix come from `src/core/search/mode-advice.ts`. Pinned by `test/init-mode-picker.test.ts`.diff --git a/docs/guides/rls-and-you.md b/docs/guides/rls-and-you.mdindex 6103e76744..d467208591 100644--- a/docs/guides/rls-and-you.md+++ b/docs/guides/rls-and-you.md@@ -268,8 +268,10 @@ could shadow a purge or withdrawal ledger a guard reads with an empty temporarythe same name and store purged content again. On an older brain, migration v276 appends`pg_temp` to every gbrain function it owns with `ALTER FUNCTION` (no body changes), and`gbrain doctor` (`function_search_path`) names any function it could not change, with the-`ALTER FUNCTION` to run as its owner. [docs/ENGINES.md](../ENGINES.md) ("Purge and-withdrawal guards") has what this does not change for a scoped write role. The three fact-fingerprint functions+`ALTER FUNCTION` to run as its owner. This does not close the scoped-writer gap: on+Postgres, a write role with no RLS policy on a ledger still reads it empty, so its purged+or withdrawn writes still go through (gbrain#7659 tracks the fix; [docs/ENGINES.md](../ENGINES.md)+("Purge and withdrawal guards") has the policy to add meanwhile). The three fact-fingerprint functions(`gbrain_fact_fingerprint`, `gbrain_fact_fingerprint_v1`,`gbrain_fact_normalize`) are SQL functions that an index expression uses, sothey call `pg_catalog.`-qualified built-ins instead of pinning a setting (adiff --git a/src/commands/doctor/checks/function-search-path.ts b/src/commands/doctor/checks/function-search-path.tsindex c1f562f85a..b74004cfad 100644--- a/src/commands/doctor/checks/function-search-path.ts+++ b/src/commands/doctor/checks/function-search-path.ts@@ -19,7 +19,7 @@ export async function functionSearchPathProblems(engine: BrainEngine): Promise<{const rows = await engine.executeRaw<{ sig: string; path: string }>(`SELECT p.oid::regprocedure::text AS sig, substr(c.setting, 13) AS pathFROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace CROSS JOIN LATERAL unnest(p.proconfig) AS c(setting)- WHERE n.nspname = 'public' AND c.setting = ANY($1::text[]) ORDER BY 1`,+ WHERE n.nspname = 'public' AND p.prokind = 'f' AND c.setting = ANY($1::text[]) ORDER BY 1`,[[...LEGACY_SEARCH_PATH_SETTINGS]]);return {functions: rows.map(r => r.sig),diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.tsindex 2d01dde48e..ffc5101eb0 100644--- a/src/core/behavior-change-notice.ts+++ b/src/core/behavior-change-notice.ts@@ -1309,7 +1309,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;{ since: '0.60.464.0', text: 'A managed sync pass whose process was killed or crashed now reads `resumable` / `pass_interrupted` in `gbrain sync status` (with an `interrupted` block naming the pass and the dead holder) instead of `pass_unfinished`, and a second killed pass reads `blocked` / `pass_abandoned` at once. `gbrain doctor` `managed_sync_blocked` lists both (warn, fail after an hour). A live pass still committing is no longer counted abandoned because its row is old.' },{ since: '0.60.465.0', text: 'On a managed brain, `gbrain trust release h<id>` of a held fact whose entity page exists now writes the fact into that page\'s `## Facts` fence through the page-write door (a `trust_release` page write) and flips the hold in the same publication, so the released fact lives in the page\'s file and a re-import keeps it. Before, the release inserted a database-only row the file never held; a fence-guard hold that names an existing fence row created a database-only twin of it, and now indexes that row and leaves the file unchanged. The row is stamped `user_confirmed` with origin `trust_release` and the page keeps its trust tier. A hold decided after the release was accepted fails `preview_changed` and writes nothing; a remote request, or a deleted or private entity page, refuses and the fact stays held. A hold with no entity page, and unmanaged brains, release database-only as before.' },// #7632 function search_path. `since` is stamped at the merge slot.- { since: 'TBD', id: 'function_search_path_pg_temp', text: 'Every gbrain function that pins a search_path now ends it with `pg_temp` (`ALTER FUNCTION ... SET search_path`, no body changes). Before, the purge and withdrawal guards pinned `pg_catalog, public`, which leaves the session\'s temporary schema first for tables and views: any session, the owner included, could create an empty temp table or view named like a ledger (`fact_purges`, `page_purges`, `take_purges`, `fact_withdrawals`, `page_segment_derivation_purges`) and store a purged claim, page, take or derivation again, or save a withdrawn fact active. The guards are otherwise unchanged (SECURITY INVOKER, same bodies, same timing). Still open, not fixed here: on Postgres, a write role with no RLS policy on a ledger reads it empty, so its purged or withdrawn writes still go through (gbrain#7659). Where the migrating role does not own a function, the migration leaves it, names it on stderr and carries on, and `gbrain doctor` (`function_search_path`) prints the `ALTER FUNCTION` to run as its owner; that changes no owner, so later boots still succeed. An older gbrain binary booting against the brain drops `pg_temp` from the functions its schema re-creates; doctor flags them and the next boot of this release puts it back.' },+ { since: 'TBD', id: 'function_search_path_pg_temp', text: 'Every gbrain function that pins a search_path now ends it with `pg_temp` (`ALTER FUNCTION ... SET search_path`, no body changes). Before, the purge and withdrawal guards pinned `pg_catalog, public`, which leaves the session\'s temporary schema first for tables and views: any session, the owner included, could create an empty temp table or view named like a ledger (`fact_purges`, `page_purges`, `take_purges`, `fact_withdrawals`, `page_segment_derivation_purges`) and store a purged claim, page, take or derivation again, or save a withdrawn fact active. The guards are otherwise unchanged (SECURITY INVOKER, same bodies, same timing). Still open, not fixed here: on Postgres, a write role with no RLS policy on a ledger reads it empty, so its purged or withdrawn writes still go through (gbrain#7659). Where the migrating role does not own a function, the migration leaves it, names it on stderr and carries on, and `gbrain doctor` (`function_search_path`) prints the `ALTER FUNCTION` to run as its owner; that changes no owner, so later boots still succeed. An older gbrain binary that replays its schema against the brain (`gbrain init` or `gbrain upgrade` from it; an ordinary boot replays nothing) drops `pg_temp` from the functions it re-creates; doctor flags them, and `gbrain init` or `gbrain upgrade` with this release, or the printed `ALTER FUNCTION`, puts it back. An ordinary boot of this release does not.' },];/** The newest disclosed change's release: the notice id moves only when a release adds rows. */diff --git a/src/core/function-search-path.ts b/src/core/function-search-path.tsindex f0a845c6ac..97e775a9c0 100644--- a/src/core/function-search-path.ts+++ b/src/core/function-search-path.ts@@ -18,7 +18,7 @@ export const LEGACY_SEARCH_PATH_SETTINGS = ['search_path=pg_catalog, public', 's/*** Appends pg_temp to the pinned search_path of every function in public the current role may alter that still pins* `pg_catalog, public` or `pg_catalog` (every gbrain function that pins one did). A function the role does not own is- * left as it was. Shared by the schema blob (its last statement, so a replay's CREATE OR REPLACE never leaves a function+ * left as it was, and so is a procedure (ALTER FUNCTION refuses one, and gbrain installs none). Shared by the schema blob (its last statement, so a replay's CREATE OR REPLACE never leaves a function* without it), the migration and `gbrain reindex-search-vector`, which rewrites the search_vector trigger functions.*/export const FUNCTION_SEARCH_PATH_HARDENING_SQL = `DO $harden$@@ -26,7 +26,7 @@ DECLARE f RECORD;BEGINFOR f IN SELECT p.oid::regprocedure AS sig, substr(c.setting, 13) AS pathFROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace CROSS JOIN LATERAL unnest(p.proconfig) AS c(setting)- WHERE n.nspname = 'public' AND c.setting IN (${LEGACY_SEARCH_PATH_SETTINGS.map(v => `'${v}'`).join(', ')})+ WHERE n.nspname = 'public' AND p.prokind = 'f' AND c.setting IN (${LEGACY_SEARCH_PATH_SETTINGS.map(v => `'${v}'`).join(', ')})AND pg_has_role(current_user, p.proowner, 'USAGE')ORDER BY 1LOOPdiff --git a/src/core/pglite-schema.generated.ts b/src/core/pglite-schema.generated.tsindex e6358c1604..a6df466d7f 100644--- a/src/core/pglite-schema.generated.ts+++ b/src/core/pglite-schema.generated.ts@@ -2826,7 +2826,7 @@ DECLARE f RECORD;BEGINFOR f IN SELECT p.oid::regprocedure AS sig, substr(c.setting, 13) AS pathFROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace CROSS JOIN LATERAL unnest(p.proconfig) AS c(setting)- WHERE n.nspname = 'public' AND c.setting IN ('search_path=pg_catalog, public', 'search_path=pg_catalog')+ WHERE n.nspname = 'public' AND p.prokind = 'f' AND c.setting IN ('search_path=pg_catalog, public', 'search_path=pg_catalog')AND pg_has_role(current_user, p.proowner, 'USAGE')ORDER BY 1LOOPdiff --git a/src/core/schema-embedded.generated.ts b/src/core/schema-embedded.generated.tsindex 421b8d536e..5a097c85c3 100644--- a/src/core/schema-embedded.generated.ts+++ b/src/core/schema-embedded.generated.ts@@ -3615,7 +3615,7 @@ DECLARE f RECORD;BEGINFOR f IN SELECT p.oid::regprocedure AS sig, substr(c.setting, 13) AS pathFROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace CROSS JOIN LATERAL unnest(p.proconfig) AS c(setting)- WHERE n.nspname = 'public' AND c.setting IN ('search_path=pg_catalog, public', 'search_path=pg_catalog')+ WHERE n.nspname = 'public' AND p.prokind = 'f' AND c.setting IN ('search_path=pg_catalog, public', 'search_path=pg_catalog')AND pg_has_role(current_user, p.proowner, 'USAGE')ORDER BY 1LOOPdiff --git a/src/core/schema-migrations/v276-function-search-path-pg-temp.ts b/src/core/schema-migrations/v276-function-search-path-pg-temp.tsindex 9a3f738faf..d2c81c7adc 100644--- a/src/core/schema-migrations/v276-function-search-path-pg-temp.ts+++ b/src/core/schema-migrations/v276-function-search-path-pg-temp.ts@@ -31,7 +31,7 @@ export async function hardenFunctionSearchPaths(engine: BrainEngine, label: stri}const left = (await engine.executeRaw<{ sig: string }>(`SELECT p.oid::regprocedure::text AS sig FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace- WHERE n.nspname = 'public' AND p.proconfig && $1::text[] ORDER BY 1`, [[...LEGACY_SEARCH_PATH_SETTINGS]])).map(r => r.sig);+ WHERE n.nspname = 'public' AND p.prokind = 'f' AND p.proconfig && $1::text[] ORDER BY 1`, [[...LEGACY_SEARCH_PATH_SETTINGS]])).map(r => r.sig);if (left.length > 0) {process.stderr.write(` ${label}: ${left.length} function(s) left without pg_temp on their search_path (${left.join(', ')}): `+ 'ALTER FUNCTION needs the owner. Run gbrain doctor for the SQL to run as that role.\n');diff --git a/src/schema.sql b/src/schema.sqlindex 73b16820ab..b778be4439 100644--- a/src/schema.sql+++ b/src/schema.sql@@ -3611,7 +3611,7 @@ DECLARE f RECORD;BEGINFOR f IN SELECT p.oid::regprocedure AS sig, substr(c.setting, 13) AS pathFROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace CROSS JOIN LATERAL unnest(p.proconfig) AS c(setting)- WHERE n.nspname = 'public' AND c.setting IN ('search_path=pg_catalog, public', 'search_path=pg_catalog')+ WHERE n.nspname = 'public' AND p.prokind = 'f' AND c.setting IN ('search_path=pg_catalog, public', 'search_path=pg_catalog')AND pg_has_role(current_user, p.proowner, 'USAGE')ORDER BY 1LOOPdiff --git a/test/e2e/function-search-path-postgres.test.ts b/test/e2e/function-search-path-postgres.test.tsindex a0366c101c..d292abacd6 100644--- a/test/e2e/function-search-path-postgres.test.ts+++ b/test/e2e/function-search-path-postgres.test.ts@@ -17,6 +17,7 @@ import { assertSafeE2eDatabaseUrl } from '../helpers/db-guard.ts';import { PostgresEngine } from '../../src/core/postgres-engine.ts';import { LATEST_VERSION, runMigrations } from '../../src/core/migrate.ts';import { derivationNeedles } from '../../src/core/page-body/segment-derive.ts';+import { v276 } from '../../src/core/schema-migrations/v276-function-search-path-pg-temp.ts';const DATABASE_URL = process.env.DATABASE_URL;const describeE2E = DATABASE_URL ? describe : describe.skip;@@ -110,14 +111,14 @@ async function catalog(c: Sql) {};}-/** Puts a brain back in the shape every brain had before #7632 (no pg_temp on any pinned path), at the version before. */+/** Puts a brain back in the shape every brain had before #7632 (no pg_temp on any pinned path), at the version before v276. */async function rewind(c: Sql): Promise<void> {await c.unsafe(`DO $$ DECLARE f RECORD; BEGINFOR f IN SELECT p.oid::regprocedure AS sig, substr(s.setting, 13) AS path FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespaceCROSS JOIN LATERAL unnest(p.proconfig) AS s(setting) WHERE n.nspname = 'public' AND s.setting LIKE 'search_path=%, pg_temp' LOOPEXECUTE format('ALTER FUNCTION %s SET search_path = %s', f.sig, left(f.path, -9));END LOOP; END $$;- UPDATE config SET value = '${LATEST_VERSION - 1}' WHERE key = 'version';`);+ UPDATE config SET value = '${v276.version - 1}' WHERE key = 'version';`);}describeE2E('a temp table or view named like a ledger hides nothing from the guards (#7632)', () => {@@ -294,7 +295,6 @@ describeE2E('a temp table or view named like a ledger hides nothing from the gua}, 300_000);test('7. v276 refuses with schema_lock_blocked while another session holds a function it alters, and finishes after', async () => {- const { v276 } = await import('../../src/core/schema-migrations/v276-function-search-path-pg-temp.ts');const { withEnv } = await import('../helpers/with-env.ts');const engine = new PostgresEngine();const holder = postgres(url, { max: 1, prepare: false, onnotice: () => {} });diff --git a/test/function-search-path-pglite.test.ts b/test/function-search-path-pglite.test.tsindex 8f4e8cc5f4..19fccfbac7 100644--- a/test/function-search-path-pglite.test.ts+++ b/test/function-search-path-pglite.test.ts@@ -51,4 +51,19 @@ describe('function search_path ends in pg_temp on PGLite (#7632)', () => {for (const ledger of SHADOWED) await engine.executeRaw(`DROP TABLE IF EXISTS pg_temp.${ledger}`);}});++ test('a procedure in public pinning the old search_path is left alone: boots and the migration still succeed, and doctor does not name it', async () => {+ const { functionSearchPathProblems } = await import('../src/commands/doctor/checks/function-search-path.ts');+ const { v276 } = await import('../src/core/schema-migrations/v276-function-search-path-pg-temp.ts');+ await engine.executeRaw(`CREATE PROCEDURE public.proc_example() LANGUAGE plpgsql SET search_path = pg_catalog, public AS $p$ BEGIN END $p$`);+ try {+ await engine.initSchema();+ await v276.handler!(engine);+ expect(await functionSearchPathProblems(engine)).toEqual({ functions: [], fix: [] });+ const [proc] = await engine.executeRaw<{ config: string[] }>(`SELECT proconfig AS config FROM pg_proc WHERE proname = 'proc_example'`);+ expect(proc!.config).toEqual(['search_path=pg_catalog, public']);+ } finally {+ await engine.executeRaw('DROP PROCEDURE IF EXISTS public.proc_example()');+ }+ }, 120_000);});