Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(#7632): name the list PINNED_FUNCTION_NAMES so the GBRAIN_* env-name scan does not read it as a variable

garrytan0592dc531317
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a7920af6fe..37676fae05 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -39,7 +39,7 @@ gbrain doctor --json # function_search_path: ok
### Itemized changes
-- `src/core/function-search-path.ts`: `FUNCTION_SEARCH_PATH_HARDENING_SQL` appends `pg_temp` to every gbrain function in `public` (`GBRAIN_PINNED_FUNCTIONS`, `prokind = 'f'`) the current role owns that pins `pg_catalog, public` or `pg_catalog`; another application's functions in the same database keep their search_path. ALTER only, no body changes. It is the last statement of the schema blob, runs in migration v277 (`function_search_path_pg_temp`), after every `initSchema` migration run (also when a migration fails, so an upgrade that stops early does not leave a re-created guard without it) and after `gbrain reindex-search-vector` re-creates the search-vector trigger functions.
+- `src/core/function-search-path.ts`: `FUNCTION_SEARCH_PATH_HARDENING_SQL` appends `pg_temp` to every gbrain function in `public` (`PINNED_FUNCTION_NAMES`, `prokind = 'f'`) the current role owns that pins `pg_catalog, public` or `pg_catalog`; another application's functions in the same database keep their search_path. ALTER only, no body changes. It is the last statement of the schema blob, runs in migration v277 (`function_search_path_pg_temp`), after every `initSchema` migration run (also when a migration fails, so an upgrade that stops early does not leave a re-created guard without it) and after `gbrain reindex-search-vector` re-creates the search-vector trigger functions.
- `src/commands/doctor/checks/function-search-path.ts`: new `function_search_path` check (fail) naming each function still without `pg_temp`, with the `ALTER FUNCTION ... SET search_path` to run as its owner. It never suggests an owner change.
- The purge and withdrawal guards are unchanged: `SECURITY INVOKER`, same bodies, same `BEFORE ROW` timing.
- Tests: `test/e2e/function-search-path-postgres.test.ts` (temporary table and view shadows for the owner and a non-owner writer, the migration from the previous version, a migrating role that owns all but one function, schema replay, lock timeout), `test/function-search-path-pglite.test.ts`, `test/doctor-function-search-path.test.ts`.
diff --git a/src/commands/doctor/checks/function-search-path.ts b/src/commands/doctor/checks/function-search-path.ts
index 692c70d423..d23a5f5943 100644
--- a/src/commands/doctor/checks/function-search-path.ts
+++ b/src/commands/doctor/checks/function-search-path.ts
@@ -12,7 +12,7 @@ import type { BrainEngine } from '../../../core/engine.ts';
import type { Check } from '../../doctor.ts';
import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';
import { checkError } from '../check-fix.ts';
-import { GBRAIN_PINNED_FUNCTIONS, LEGACY_SEARCH_PATH_SETTINGS } from '../../../core/function-search-path.ts';
+import { PINNED_FUNCTION_NAMES, LEGACY_SEARCH_PATH_SETTINGS } from '../../../core/function-search-path.ts';
/** The gbrain functions whose pinned search_path lacks a trailing pg_temp, and the ALTER that fixes each. */
export async function functionSearchPathProblems(engine: BrainEngine): Promise<{ functions: string[]; fix: string[] }> {
@@ -20,7 +20,7 @@ export async function functionSearchPathProblems(engine: BrainEngine): Promise<{
`SELECT p.oid::regprocedure::text AS sig, substr(c.setting, 13) AS path
FROM 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 p.prokind = 'f' AND c.setting = ANY($1::text[]) AND p.proname = ANY($2::text[]) ORDER BY 1`,
- [[...LEGACY_SEARCH_PATH_SETTINGS], [...GBRAIN_PINNED_FUNCTIONS]]);
+ [[...LEGACY_SEARCH_PATH_SETTINGS], [...PINNED_FUNCTION_NAMES]]);
return {
functions: rows.map(r => r.sig),
fix: rows.map(r => `ALTER FUNCTION ${r.sig} SET search_path = ${r.path}, pg_temp;`),
diff --git a/src/core/function-search-path.ts b/src/core/function-search-path.ts
index 282a24fb23..45a976b259 100644
--- a/src/core/function-search-path.ts
+++ b/src/core/function-search-path.ts
@@ -21,7 +21,7 @@ export const LEGACY_SEARCH_PATH_SETTINGS = ['search_path=pg_catalog, public', 's
* pinned function joins this list; the fresh-install tests (test/function-search-path-pglite.test.ts, and case 3 of
* test/e2e/function-search-path-postgres.test.ts) fail until it does.
*/
-export const GBRAIN_PINNED_FUNCTIONS = [
+export const PINNED_FUNCTION_NAMES = [
'auto_enable_rls', 'bump_page_generation_clock_fn', 'bump_page_generation_fn', 'enforce_minion_queue_protocol',
'gbrain_advance_page_revision', 'gbrain_advance_tag_revision', 'gbrain_bump_trust_generation',
'gbrain_clear_atom_page_state', 'gbrain_count_git_canonical_sources', 'gbrain_count_modifications', 'gbrain_forget_page_retrievals',
@@ -36,7 +36,7 @@ export const GBRAIN_PINNED_FUNCTIONS = [
] as const;
/**
- * Appends pg_temp to the pinned search_path of every gbrain function (GBRAIN_PINNED_FUNCTIONS) in public the current
+ * Appends pg_temp to the pinned search_path of every gbrain function (PINNED_FUNCTION_NAMES) in public the current
* role may alter that still pins `pg_catalog, public` or `pg_catalog`. A function the role does not own is
* 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), both engines' initSchema after migrations (a migration that re-creates a function resets its path, and
@@ -49,7 +49,7 @@ BEGIN
FOR f IN SELECT p.oid::regprocedure AS sig, substr(c.setting, 13) AS path
FROM 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 p.prokind = 'f' AND c.setting IN (${LEGACY_SEARCH_PATH_SETTINGS.map(v => `'${v}'`).join(', ')})
- AND p.proname IN (${GBRAIN_PINNED_FUNCTIONS.map(name => `'${name}'`).join(', ')})
+ AND p.proname IN (${PINNED_FUNCTION_NAMES.map(name => `'${name}'`).join(', ')})
AND pg_has_role(current_user, p.proowner, 'USAGE')
ORDER BY 1
LOOP
diff --git a/src/core/schema-migrations/v277-function-search-path-pg-temp.ts b/src/core/schema-migrations/v277-function-search-path-pg-temp.ts
index 6c9de09dfc..5af6d98a7c 100644
--- a/src/core/schema-migrations/v277-function-search-path-pg-temp.ts
+++ b/src/core/schema-migrations/v277-function-search-path-pg-temp.ts
@@ -3,7 +3,7 @@ import type { BrainEngine } from '../engine.ts';
import { migrationStatementTimeoutMs } from '../migration-timeout.ts';
import { isLockTimeoutError } from '../retry-matcher.ts';
import { resolveSchemaLockTimeoutMs, schemaLockBlockedError } from '../postgres-engine/schema-lock-timeout.ts';
-import { FUNCTION_SEARCH_PATH_HARDENING_SQL, GBRAIN_PINNED_FUNCTIONS, LEGACY_SEARCH_PATH_SETTINGS } from '../function-search-path.ts';
+import { FUNCTION_SEARCH_PATH_HARDENING_SQL, PINNED_FUNCTION_NAMES, LEGACY_SEARCH_PATH_SETTINGS } from '../function-search-path.ts';
// Applied by src/core/migrate.ts on the next initSchema(); see docs/ENGINES.md
// ("Canonical schema sources") for when schema.sql or a TS fragment also changes.
@@ -32,7 +32,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.prokind = 'f' AND p.proconfig && $1::text[] AND p.proname = ANY($2::text[]) ORDER BY 1`,
- [[...LEGACY_SEARCH_PATH_SETTINGS], [...GBRAIN_PINNED_FUNCTIONS]])).map(r => r.sig);
+ [[...LEGACY_SEARCH_PATH_SETTINGS], [...PINNED_FUNCTION_NAMES]])).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/test/e2e/function-search-path-postgres.test.ts b/test/e2e/function-search-path-postgres.test.ts
index 7907b4b52d..4b10e31195 100644
--- a/test/e2e/function-search-path-postgres.test.ts
+++ b/test/e2e/function-search-path-postgres.test.ts
@@ -18,7 +18,7 @@ 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 { v277 } from '../../src/core/schema-migrations/v277-function-search-path-pg-temp.ts';
-import { GBRAIN_PINNED_FUNCTIONS } from '../../src/core/function-search-path.ts';
+import { PINNED_FUNCTION_NAMES } from '../../src/core/function-search-path.ts';
const DATABASE_URL = process.env.DATABASE_URL;
const describeE2E = DATABASE_URL ? describe : describe.skip;
@@ -109,7 +109,7 @@ async function catalog(c: Sql) {
triggers: triggers.map(t => ({ trigger: t.trigger, timing: t.timing })),
pinned: paths.length,
withoutTemp: paths.filter(p => !p.setting.endsWith(', pg_temp')).map(p => p.sig),
- unlisted: paths.filter(p => !(GBRAIN_PINNED_FUNCTIONS as readonly string[]).includes(p.name)).map(p => p.sig),
+ unlisted: paths.filter(p => !(PINNED_FUNCTION_NAMES as readonly string[]).includes(p.name)).map(p => p.sig),
};
}
diff --git a/test/fixtures/goldens/migrations/records.json b/test/fixtures/goldens/migrations/records.json
index 40e1445a85..afead80ea1 100644
--- a/test/fixtures/goldens/migrations/records.json
+++ b/test/fixtures/goldens/migrations/records.json
@@ -23,8 +23,7 @@
"FACT_UNLINKED_INDEX_SQL": "87dcf23c3fa570d93bfb214b6bbb024faa9ff9bff227aa78869a29580be03298",
"FTS_CJK_BOUNDARY_REGEX": "e8605bc0f2c6f3ecd2b451cbc8a4fe27ae0974d048f0bea655533cf37e0ff88d",
"FTS_INPUT_FUNCTION_SQL": "d2f44fc1ef338ff7563c07659d938b743fedb20230948792c71068c022ec54c3",
- "FUNCTION_SEARCH_PATH_HARDENING_SQL": "cd0e9a5e2027517b78278043889f1abedf14ac8154b96e70331a84805ff55acb",
- "GBRAIN_PINNED_FUNCTIONS": "5ffbb89d1ba67231422deaf824f4eab9788ae3021678f9a8a1e24de534d2460f",
+ "FUNCTION_SEARCH_PATH_HARDENING_SQL": "cad3e8c5c052c7eadcd0cbe88b1fccded412a9272393887789dece500f0403a4",
"INGEST_LOG_MARKED_INDEX": "1099f19af7873878974239a9a6f1a627827221eebd75aea01896246e0809e91c",
"INGEST_LOG_UNMARKED_INDEX": "c94e6b009f830527d2f1d6af1683bee94189474e8e67aa4c60165ab4abbd8726",
"ISSUANCE_CLOCK_SLACK_SECONDS": "8f36c80392df6f9b9c754826289b63969d9d41ca8955213291e4413d614dce35",
@@ -46,6 +45,7 @@
"PERSISTENCE_REQUEST_SLUG_INTENT_INDEX": "5f2e4a3dde452e610fe7eaae949642c00d7c61a363b1cd344d7f0917db233aa5",
"PERSISTENCE_SYNC_RUN_INDEXES": "e1b12613c5fd2facb850e6857b0bddcf49b2478ec258fa226fe15e71e55df9c1",
"PERSISTENCE_SYNC_WATERMARK_INDEX": "93523954a30e8483717f07ec97a3e0cd062b15d8a60cc759281054b06c23bb40",
+ "PINNED_FUNCTION_NAMES": "8f9ab677193ee126fd9c7dcb54c7a21e29ef84c65acc4ca18529257d38f911cc",
"PLANNER_REPAIR_TABLES": "008725595e099adba279d4292661eb4a59930b777c35db43c340220f7f12e093",
"TABLES": "3fc77a0fccb525f9b791098f9eae27b19adaa3f5dadbabcff2a719137cbc3ef8",
"TOKEN_TTL_MAX_SECONDS": "f4a45bec6ec0bacc859dc57732ceb76cec7eb72f242b5a6c89d47190d0525ab4",
@@ -65,7 +65,7 @@
"ensureFactsFtsIndex": "95e723314ac30cea2db48e0e167c554ef1fa06df80cae765faa2a080a3a655c2",
"getFtsLanguage": "0d790b4c4af1f67a2a1bbd59470beb13f434917b381931baf8404a41c5d903db",
"grantFromRow": "2d0603bb0f964d7e45e04aa76bac54cd4f6f63c91a8d9f028b60b4792bc1695e",
- "hardenFunctionSearchPaths": "5cfa215e766d47a8d17a9b0308a0bb9ee52010d985ee8cc07cb81a3b3b30b62e",
+ "hardenFunctionSearchPaths": "49bb15a6af8ac0820e3ee1430417caf43e350832c8204a76bc3a3e6029fc996e",
"hnswMaxDimsForType": "a187cdcc520f813b7c39da97ce53da58509232ed9f851627ef41abfe1b68bd26",
"isLockTimeoutError": "52a3720e1ffb74ce381cc3269f78130c38a89c807e308283ec3d557bae2b89b1",
"lockTableForAnalyze": "b6e4f173cffd877d2cfd85af813aa694a6d2c32907eaea613c6879800d3a7b44",
@@ -4485,8 +4485,8 @@
"form": "arrow",
"helpers": [
"FUNCTION_SEARCH_PATH_HARDENING_SQL",
- "GBRAIN_PINNED_FUNCTIONS",
"LEGACY_SEARCH_PATH_SETTINGS",
+ "PINNED_FUNCTION_NAMES",
"hardenFunctionSearchPaths",
"isLockTimeoutError",
"migrationStatementTimeoutMs",
diff --git a/test/function-search-path-pglite.test.ts b/test/function-search-path-pglite.test.ts
index 28e7bb91af..5a5e4fc0e3 100644
--- a/test/function-search-path-pglite.test.ts
+++ b/test/function-search-path-pglite.test.ts
@@ -5,7 +5,7 @@
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
-import { GBRAIN_PINNED_FUNCTIONS } from '../src/core/function-search-path.ts';
+import { PINNED_FUNCTION_NAMES } from '../src/core/function-search-path.ts';
const PURGED_CLAIM = 'alice-example joined acme-example as cto';
const WITHDRAWN_CLAIM = 'alice-example lives in city-a';
@@ -29,13 +29,13 @@ describe('function search_path ends in pg_temp on PGLite (#7632)', () => {
}, 120_000);
afterAll(async () => { await engine?.disconnect(); });
- test('every function that pins a search_path ends it with pg_temp, none is SECURITY DEFINER, and each is in GBRAIN_PINNED_FUNCTIONS', async () => {
+ test('every function that pins a search_path ends it with pg_temp, none is SECURITY DEFINER, and each is in PINNED_FUNCTION_NAMES', async () => {
const rows = await engine.executeRaw<{ name: string; sig: string; setting: string; secdef: boolean }>(
`SELECT p.proname AS name, p.oid::regprocedure::text AS sig, s.setting, p.prosecdef AS secdef FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
CROSS JOIN LATERAL unnest(p.proconfig) AS s(setting) WHERE n.nspname = 'public' AND s.setting LIKE 'search_path=%' ORDER BY 1`);
expect(rows.length).toBeGreaterThanOrEqual(35);
expect(rows.filter(r => !r.setting.endsWith(', pg_temp') || r.secdef).map(r => r.sig)).toEqual([]);
- expect(rows.map(r => r.name).filter(name => !(GBRAIN_PINNED_FUNCTIONS as readonly string[]).includes(name))).toEqual([]);
+ expect(rows.map(r => r.name).filter(name => !(PINNED_FUNCTION_NAMES as readonly string[]).includes(name))).toEqual([]);
});
test('another application\'s function in public pinning the old search_path keeps it: boots and the migration leave it, and doctor does not name it', async () => {