Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: pre-landing review fixes (pass 2)

garrytan0e4d51d21932
diff --git a/scripts/capture-schema-history-fixture.ts b/scripts/capture-schema-history-fixture.ts
index a6c6f1eb80..d95fe936ba 100644
--- a/scripts/capture-schema-history-fixture.ts
+++ b/scripts/capture-schema-history-fixture.ts
@@ -23,7 +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';
+import { sameUpToFlattenedBooleanChains } from '../test/helpers/schema-history.ts';
type Query = (sql: string) => Promise<Array<Record<string, any>>>;
type Engine = 'pglite' | 'postgres';
@@ -213,7 +213,7 @@ async function tableRows(query: Query): Promise<Record<string, string>> {
async function fidelity(original: Query, restored: Query): Promise<string[]> {
const problems: string[] = [];
const catalogDiff = diffCatalogSnapshots(await snapshotCatalog(original as CatalogQueryFn), await snapshotCatalog(restored as CatalogQueryFn), { compareOrdinals: true })
- .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && sameUpToOrChainParentheses(d.expected, d.actual)));
+ .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && sameUpToFlattenedBooleanChains(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/scripts/structural-suites.tsv b/scripts/structural-suites.tsv
index 8c1400538b..3b08aa2c70 100644
--- a/scripts/structural-suites.tsv
+++ b/scripts/structural-suites.tsv
@@ -286,7 +286,7 @@ test/resolver.test.ts RESOLVER.md trigger round-trip (D5/C) 2 readFileSync
test/resolver.test.ts Skill example-name validator (D13) 4 readFileSync
test/retrieval-reflex-recipe-routing.test.ts retrieval-reflex recipe routing is MECE with the query skill (#4292) 5 readFileSync
test/runtime-version.test.ts stated Bun floor 1 readFileSync
-test/schema-bootstrap-coverage.test.ts (file-level) 14 source-surface-helper
+test/schema-bootstrap-coverage.test.ts (file-level) 15 source-surface-helper
test/schema-pack-unify-types-handler.test.ts #1575 unify-types worker dry-run default 1 source-surface-helper
test/schema-pack/suggest.test.ts runSuggest — source-text contract (structural) 1 readFileSync
test/scripts/build-schema-migrations.test.ts generator diagnostics (FAIL/Why/Fix/See) 9 exec-scan
diff --git a/src/core/persistence/schema.ts b/src/core/persistence/schema.ts
index 60a6c693aa..8d75434591 100644
--- a/src/core/persistence/schema.ts
+++ b/src/core/persistence/schema.ts
@@ -39,7 +39,8 @@ export const PERSISTENCE_REQUEST_ID_INDEX = { name: 'persistence_requests_reques
* #7649: the write claims' foreground-commit probe (journal.ts `FOREGROUND_WAITING_ON`: a committed non-sync request on the
* worktree after the `syncSeen` bound). After a restart the bound is 0, and with no index on (worktree, sequence) for committed
* rows the probe walked the sequence key (or the heap) through every receipt, sync receipts included, on each claim. The
- * predicate is the probe's `state='committed' AND NOT SYNC_KIND(c)` written the same way, so the planner can use it. Postgres
+ * predicate is the probe's `state='committed' AND NOT SYNC_KIND(c)` written the same way, so the planner can use it; like the probe it
+ * also covers compacted receipts of any kind (their intent is NULL), so on a long-lived brain it holds most committed rows. Postgres
* builds it CONCURRENTLY in the persistence_foreground_committed_index migration (`transaction: false`); PGLite inline.
*/
export const PERSISTENCE_FOREGROUND_COMMITTED_INDEX_SQL = `CREATE INDEX IF NOT EXISTS persistence_requests_foreground_committed
diff --git a/test/helpers/schema-history.ts b/test/helpers/schema-history.ts
index 9ff09493a2..a3fd5fbab6 100644
--- a/test/helpers/schema-history.ts
+++ b/test/helpers/schema-history.ts
@@ -13,7 +13,7 @@ export type SchemaHistoryEngine = 'pglite' | 'postgres';
/** 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');
+const SCHEMA_HISTORY_DIR = resolve(import.meta.dir, '..', 'fixtures', 'schema-history');
export function schemaHistoryVersions(engine: SchemaHistoryEngine): number[] {
return readdirSync(join(SCHEMA_HISTORY_DIR, engine))
@@ -40,10 +40,11 @@ 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.
+ * Canonical text of a SQL expression: nested chains of the same AND or OR
+ * operator are flattened (`((a OR b) OR c)` and `(a OR b OR c)` print the
+ * same) and parentheses around a whole operand are dropped. Every other
+ * parenthesis is kept, so a regrouping across AND and OR, arithmetic grouping
+ * and call parentheses still print differently.
*/
function canonicalBoolean(sql: string): string {
const tokens = (sql.match(/'(?:[^']|'')*'|\(|\)|\bAND\b|\bOR\b|[^()'\s]+|\s+/gi) ?? []);
@@ -52,18 +53,19 @@ function canonicalBoolean(sql: string): string {
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[] = [];
+ const parts: Array<{ node: BooleanNode; grouped: boolean }> = [];
while (i < tokens.length && tokens[i] !== ')' && keyword(tokens[i]) !== 'AND' && keyword(tokens[i]) !== 'OR') {
if (tokens[i] === '(') {
i++;
- parts.push(or());
+ parts.push({ node: or(), grouped: true });
if (tokens[i] === ')') i++;
} else {
- if (tokens[i].trim()) parts.push({ text: tokens[i] });
+ if (tokens[i].trim()) parts.push({ node: { text: tokens[i] }, grouped: false });
i++;
}
}
- return parts.length === 1 ? parts[0] : { text: parts.map(printBoolean).join(' ') };
+ if (parts.length === 1) return parts[0].node;
+ return { text: parts.map(({ node, grouped }) => (grouped && 'text' in node ? `(${node.text})` : printBoolean(node))).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); };
@@ -77,10 +79,10 @@ function canonicalBoolean(sql: string): string {
/**
* 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.
+ * flattened (same expression, fewer parentheses). Only parentheses that nest a
+ * chain inside the same AND or OR operator are ignored; all others count.
*/
-export function sameUpToOrChainParentheses(expected: unknown, actual: unknown): boolean {
+export function sameUpToFlattenedBooleanChains(expected: unknown, actual: unknown): boolean {
return canonicalBoolean(JSON.stringify(expected)) === canonicalBoolean(JSON.stringify(actual));
}
@@ -94,7 +96,7 @@ export function sameUpToOrChainParentheses(expected: unknown, actual: unknown):
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 !sameUpToOrChainParentheses(d.expected, d.actual);
+ if (d.section === 'triggers') return !sameUpToFlattenedBooleanChains(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>;
diff --git a/test/scripts/capture-schema-history-fixture.test.ts b/test/scripts/capture-schema-history-fixture.test.ts
index 19263a62c0..180892191e 100644
--- a/test/scripts/capture-schema-history-fixture.test.ts
+++ b/test/scripts/capture-schema-history-fixture.test.ts
@@ -11,7 +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 { sameUpToFlattenedBooleanChains } from '../helpers/schema-history.ts';
import { withColdPglite } from '../helpers/with-snapshot.ts';
let original: PGLiteEngine;
@@ -57,7 +57,7 @@ afterAll(async () => {
test('a dumped brain restores to the same catalog, function bodies and rows', async () => {
const diff = diffCatalogSnapshots(await snapshotCatalog(pgliteCatalogQuery(original)), await snapshotCatalog(pgliteCatalogQuery(restored)), { compareOrdinals: true })
- .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && sameUpToOrChainParentheses(d.expected, d.actual)));
+ .filter((d) => !(d.section === 'triggers' && d.kind === 'changed' && sameUpToFlattenedBooleanChains(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']) {