Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes
garrytanbd39dc6282b8
diff --git a/scripts/capture-schema-history-fixture.ts b/scripts/capture-schema-history-fixture.tsindex 09df9f113b..a6c6f1eb80 100644--- a/scripts/capture-schema-history-fixture.ts+++ b/scripts/capture-schema-history-fixture.ts@@ -23,6 +23,7 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { LEGACY_EMBEDDING_CONFIG } from '../test/helpers/legacy-embedding-config.ts';import { diffCatalogSnapshots, formatCatalogDiffForFailure, snapshotCatalog, type CatalogQueryFn } from '../test/helpers/schema-diff.ts';import { FUNCTION_BODIES_SQL } from '../test/helpers/schema-catalog.ts';+import { sameUpToOrChainParentheses } from '../test/helpers/schema-history.ts';type Query = (sql: string) => Promise<Array<Record<string, any>>>;type Engine = 'pglite' | 'postgres';@@ -211,10 +212,8 @@ async function tableRows(query: Query): Promise<Record<string, string>> {async function fidelity(original: Query, restored: Query): Promise<string[]> {const problems: string[] = [];- // A trigger's WHEN clause deparses its OR chain flattened once restored; same expression, fewer parentheses.- const unparenthesized = (value: unknown) => JSON.stringify(value).replace(/[()]/g, '');const catalogDiff = diffCatalogSnapshots(await snapshotCatalog(original as CatalogQueryFn), await snapshotCatalog(restored as CatalogQueryFn), { compareOrdinals: true })- .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && unparenthesized(d.expected) === unparenthesized(d.actual)));+ .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && sameUpToOrChainParentheses(d.expected, d.actual)));if (catalogDiff.length > 0) problems.push(formatCatalogDiffForFailure(catalogDiff));const [a, b] = [await original(FUNCTION_BODIES_SQL), await restored(FUNCTION_BODIES_SQL)];if (JSON.stringify(a) !== JSON.stringify(b)) problems.push('function bodies differ');diff --git a/src/core/repair/request-indexes.ts b/src/core/repair/request-indexes.tsindex 1093283763..a61815ecaa 100644--- a/src/core/repair/request-indexes.ts+++ b/src/core/repair/request-indexes.ts@@ -1,10 +1,11 @@/**- * `gbrain repair request-indexes` (#5762, #6511, #7649): create a missing online-built- * request index (the managed sync checkpoint pair, the purge slug index, the- * request-id index, the write claims' foreground-commit index), or drop an- * INVALID one (an interrupted concurrent build) and rebuild it, on a brain- * whose schema version is already current, where their migrations never run again. Postgres builds CONCURRENTLY, one index at- * a time; PGLite builds inline. An index another session is still building is+ * `gbrain repair request-indexes` (#5762, #6511, #7649): create a missing+ * online-built request index (the managed sync checkpoint pair, the purge slug+ * index, the request-id index, the write claims' foreground-commit index), or+ * drop an INVALID one (an interrupted concurrent build) and rebuild it, on a+ * brain whose schema version is already current, where their migrations never+ * run again. Postgres builds CONCURRENTLY, one index at a time; PGLite builds+ * inline. An index another session is still building is* reported, never dropped. Brain-wide; no journal admission and no user data* changes, so `--all` and doctor remediation include it.*/diff --git a/test/e2e/schema-history-upgrade.test.ts b/test/e2e/schema-history-upgrade.test.tsindex eb6d5914ea..e6c5d199f3 100644--- a/test/e2e/schema-history-upgrade.test.ts+++ b/test/e2e/schema-history-upgrade.test.ts@@ -1,6 +1,6 @@/*** #7645 class closure, Postgres half (the PGLite half and the rationale are in- * test/schema-history-upgrade.test.ts): a Postgres brain created by any release+ * test/schema-history-upgrade.slow.test.ts): a Postgres brain created by any release* since v260 upgrades to the latest schema through initSchema and ends with the* catalog a fresh install has. Each fixture loads into its own throwaway* database. Skips without DATABASE_URL.diff --git a/test/helpers/schema-history.ts b/test/helpers/schema-history.tsindex 6d80f77673..9ff09493a2 100644--- a/test/helpers/schema-history.ts+++ b/test/helpers/schema-history.ts@@ -10,7 +10,7 @@ import { diffCatalogSnapshots, formatCatalogDiffForFailure, type CatalogSnapshotexport type SchemaHistoryEngine = 'pglite' | 'postgres';-/** The first schema version with a fixture; every later shipped version has one. */+/** The first schema version with a fixture; the fixtures run contiguously from here to the newest one captured. */export const FIRST_SCHEMA_HISTORY_VERSION = 260;export const SCHEMA_HISTORY_DIR = resolve(import.meta.dir, '..', 'fixtures', 'schema-history');@@ -33,8 +33,56 @@ export interface InstalledSchema {functionBodies: Map<string, string>;}-const collapseWhitespace = (body: string | undefined) => (body ?? '').replace(/\s+/g, ' ').trim();-const withoutParentheses = (value: unknown) => JSON.stringify(value).replace(/[()]/g, '');+const collapseWhitespace = (body: string) => body.replace(/\s+/g, ' ').trim();++type BooleanNode = { op: 'AND' | 'OR'; items: BooleanNode[] } | { text: string };++const printBoolean = (node: BooleanNode): string => ('text' in node ? node.text : `(${node.items.map(printBoolean).join(` ${node.op} `)})`);++/**+ * Canonical text of a SQL expression with nested AND/OR groups flattened+ * (`((a OR b) OR c)` and `(a OR b OR c)` print the same) and redundant+ * parentheses dropped, while a regrouping across AND and OR still prints+ * differently.+ */+function canonicalBoolean(sql: string): string {+ const tokens = (sql.match(/'(?:[^']|'')*'|\(|\)|\bAND\b|\bOR\b|[^()'\s]+|\s+/gi) ?? []);+ let i = 0;+ const keyword = (t: string | undefined) => t?.toUpperCase();+ const group = (op: 'AND' | 'OR', items: BooleanNode[]): BooleanNode => items.length === 1 ? items[0]+ : { op, items: items.flatMap(item => ('op' in item && item.op === op ? item.items : [item])) };+ const atom = (): BooleanNode => {+ const parts: BooleanNode[] = [];+ while (i < tokens.length && tokens[i] !== ')' && keyword(tokens[i]) !== 'AND' && keyword(tokens[i]) !== 'OR') {+ if (tokens[i] === '(') {+ i++;+ parts.push(or());+ if (tokens[i] === ')') i++;+ } else {+ if (tokens[i].trim()) parts.push({ text: tokens[i] });+ i++;+ }+ }+ return parts.length === 1 ? parts[0] : { text: parts.map(printBoolean).join(' ') };+ };+ const and = (): BooleanNode => { const items = [atom()]; while (keyword(tokens[i]) === 'AND') { i++; items.push(atom()); } return group('AND', items); };+ const or = (): BooleanNode => { const items = [and()]; while (keyword(tokens[i]) === 'OR') { i++; items.push(and()); } return group('OR', items); };+ let out = '';+ while (i < tokens.length) {+ out += printBoolean(or());+ if (i < tokens.length) out += tokens[i++];+ }+ return out;+}++/**+ * A restored constraint trigger deparses its WHEN clause with nested OR chains+ * flattened (same expression, fewer parentheses). Parentheses that regroup AND+ * and OR still count.+ */+export function sameUpToOrChainParentheses(expected: unknown, actual: unknown): boolean {+ return canonicalBoolean(JSON.stringify(expected)) === canonicalBoolean(JSON.stringify(actual));+}/*** The catalog difference between a fresh install and an upgraded brain, '' when@@ -46,12 +94,13 @@ const withoutParentheses = (value: unknown) => JSON.stringify(value).replace(/[(export function schemaHistoryDrift(fresh: InstalledSchema, upgraded: InstalledSchema): string {const drift = diffCatalogSnapshots(fresh.catalog, upgraded.catalog).filter((d) => {if (d.kind !== 'changed') return true;- if (d.section === 'triggers') return withoutParentheses(d.expected) !== withoutParentheses(d.actual);+ if (d.section === 'triggers') return !sameUpToOrChainParentheses(d.expected, d.actual);if (d.section !== 'functions') return true;const { bodySha256: _e, ...expected } = d.expected as Record<string, unknown>;const { bodySha256: _a, ...actual } = d.actual as Record<string, unknown>;- return JSON.stringify(expected) !== JSON.stringify(actual)- || collapseWhitespace(fresh.functionBodies.get(d.key)) !== collapseWhitespace(upgraded.functionBodies.get(d.key));+ const [freshBody, upgradedBody] = [fresh.functionBodies.get(d.key), upgraded.functionBodies.get(d.key)];+ return JSON.stringify(expected) !== JSON.stringify(actual) || freshBody === undefined || upgradedBody === undefined+ || collapseWhitespace(freshBody) !== collapseWhitespace(upgradedBody);});return drift.length === 0 ? '' : formatCatalogDiffForFailure(drift);}diff --git a/test/migration-persistence-foreground-committed-index.test.ts b/test/migration-persistence-foreground-committed-index.test.tsindex 82a01c1d6f..34b7f572af 100644--- a/test/migration-persistence-foreground-committed-index.test.ts+++ b/test/migration-persistence-foreground-committed-index.test.ts@@ -30,6 +30,8 @@ import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';import { testBackends } from './helpers/test-backends.ts';const INDEX = PERSISTENCE_FOREGROUND_COMMITTED_INDEX;+const RECEIPTS = 20_000;+const FOREGROUND_TAIL = 20;const migration = MIGRATIONS.find(m => m.name === 'persistence_foreground_committed_index');describe('#7649 the foreground-commit index migration module', () => {@@ -81,9 +83,9 @@ for (const backend of testBackends()) describe(`[${backend}] #7649 the foregrounawait engine.executeRaw(`INSERT INTO persistence_requests(principal_kind,principal_id,request_id,operation,source_id,source_incarnation,worktree_id,slug,digest,intent,authority,intent_bytes,terminal_reservation,state,completed_at)SELECT 'local_cli','seed',gen_random_uuid(),'put_page','default',(SELECT incarnation FROM sources WHERE id='default'),$1::uuid,'seed-'||g,'d',- CASE WHEN g>19980 THEN jsonb_build_object('kind','put_page') ELSE jsonb_build_object('kind','managed_sync_import','runId','r','index',g) END,+ CASE WHEN g>$2::int-$3::int THEN jsonb_build_object('kind','put_page') ELSE jsonb_build_object('kind','managed_sync_import','runId','r','index',g) END,'{}'::jsonb,1,64,'committed',now()- FROM generate_series(1,20000) g`, [worktree]);+ FROM generate_series(1,$2::int) g`, [worktree, RECEIPTS, FOREGROUND_TAIL]);for (const [slug, kind] of [['queued-sync', 'managed_sync_import'], ['queued-fg', 'put_page']]) {await engine.executeRaw(`INSERT INTO persistence_requests(principal_kind,principal_id,request_id,operation,source_id,source_incarnation,worktree_id,slug,digest,intent,authority,intent_bytes,terminal_reservation,state)diff --git a/test/schema-bootstrap-coverage.test.ts b/test/schema-bootstrap-coverage.test.tsindex d84396e245..4548d91185 100644--- a/test/schema-bootstrap-coverage.test.ts+++ b/test/schema-bootstrap-coverage.test.ts@@ -690,7 +690,7 @@ function parseIndexColumnReferences(sql: string): Array<{ table: string; column:if (!expr) continue;const bare = expr.replace(/'(?:[^']|'')*'/g, ' ').replace(/::\s*\w+(?:\s*\[\])?/g, ' ');for (const word of bare.matchAll(/\b([a-z_]\w*)\b(?!\s*\()/gi)) {- if (/^(and|or|not|is|null|true|false|in|like|ilike|between|any|all|distinct|from)$/i.test(word[1])) continue;+ if (/^(and|or|not|is|null|true|false|in|like|ilike|between|any|all|distinct|from|case|when|then|else|end|interval|array|current_date|current_timestamp|localtimestamp)$/i.test(word[1])) continue;result.push({ table, column: word[1].toLowerCase() });}}@@ -731,6 +731,16 @@ test('parseBaseTableColumns + parseIndexColumnReferences extract structural refeexpect(refs).toContainEqual({ table: 'pages', column: 'embedding' });});+test('parseIndexColumnReferences extracts partial-index WHERE and INCLUDE identifiers (#7645)', () => {+ const refs = parseIndexColumnReferences(`+ CREATE INDEX IF NOT EXISTS derived_generation_items_pending ON derived_generation_items (generation_id) WHERE pending;+ CREATE INDEX i ON t (a) INCLUDE (b) WHERE kind = 'x'::text AND lower(c) IS NOT NULL AND CASE WHEN d THEN true ELSE false END;+ `);+ expect(refs).toContainEqual({ table: 'derived_generation_items', column: 'pending' });+ for (const column of ['a', 'b', 'kind', 'c', 'd']) expect(refs).toContainEqual({ table: 't', column });+ expect(refs.filter(r => ['x', 'text', 'lower', 'case', 'when', 'then', 'else', 'end'].includes(r.column))).toEqual([]);+});+test('parseIndexColumnReferences catches v0.27 composite second-column case', () => {// The exact codex regression: `idx_subagent_messages_provider ON// subagent_messages (job_id, provider_id)` has provider_id as the SECONDdiff --git a/test/schema-history-upgrade.slow.test.ts b/test/schema-history-upgrade.slow.test.tsindex 99bb4bb29e..cf063c09aa 100644--- a/test/schema-history-upgrade.slow.test.ts+++ b/test/schema-history-upgrade.slow.test.ts@@ -37,6 +37,10 @@ test('a schema-history fixture exists for every schema version from v260 to theexpect(versions).toEqual(Array.from({ length: newest - FIRST_SCHEMA_HISTORY_VERSION + 1 }, (_, i) => FIRST_SCHEMA_HISTORY_VERSION + i));});+test('the Postgres fixtures cover the same schema versions as the PGLite ones', () => {+ expect(schemaHistoryVersions('postgres')).toEqual(versions);+});+for (const version of versions) {describe(`PGLite brain created at v${version} (#7645)`, () => {let engine: PGLiteEngine;@@ -48,6 +52,8 @@ for (const version of versions) {upgrade = withCatalogConfig(CATALOG_CONFIGS[0], () => withColdPglite(async () => {await engine.connect({});await engine.db.exec(schemaHistorySql('pglite', version));+ const [{ value }] = (await engine.db.query<{ value: string }>(`SELECT value FROM config WHERE key = 'version'`)).rows;+ if (value !== String(version)) throw new Error(`fixture v${version} restored a brain at schema v${value}`);await engine.initSchema();}));await upgrade.catch(() => {});diff --git a/test/scripts/capture-schema-history-fixture.test.ts b/test/scripts/capture-schema-history-fixture.test.tsindex d2b55e930a..19263a62c0 100644--- a/test/scripts/capture-schema-history-fixture.test.ts+++ b/test/scripts/capture-schema-history-fixture.test.ts@@ -11,6 +11,7 @@ import { PGLiteEngine } from '../../src/core/pglite-engine.ts';import { dumpBrainSchema } from '../../scripts/capture-schema-history-fixture.ts';import { FUNCTION_BODIES_SQL, pgliteCatalogQuery } from '../helpers/schema-catalog.ts';import { diffCatalogSnapshots, snapshotCatalog } from '../helpers/schema-diff.ts';+import { sameUpToOrChainParentheses } from '../helpers/schema-history.ts';import { withColdPglite } from '../helpers/with-snapshot.ts';let original: PGLiteEngine;@@ -55,9 +56,8 @@ afterAll(async () => {});test('a dumped brain restores to the same catalog, function bodies and rows', async () => {- const unparenthesized = (value: unknown) => JSON.stringify(value).replace(/[()]/g, '');const diff = diffCatalogSnapshots(await snapshotCatalog(pgliteCatalogQuery(original)), await snapshotCatalog(pgliteCatalogQuery(restored)), { compareOrdinals: true })- .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && unparenthesized(d.expected) === unparenthesized(d.actual)));+ .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && sameUpToOrChainParentheses(d.expected, d.actual)));expect(diff).toEqual([]);expect((await restored.db.query(FUNCTION_BODIES_SQL)).rows).toEqual((await original.db.query(FUNCTION_BODIES_SQL)).rows);for (const table of ['owners', 'items']) {