Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
graduation: alias-qualified keyset ordering and an independent count(*) check in verify (spike finding 1)
garrytanb43f878a4c45
diff --git a/src/core/persistence/graduation-copy.ts b/src/core/persistence/graduation-copy.tsindex 2a8f8d9813..be580f75aa 100644--- a/src/core/persistence/graduation-copy.ts+++ b/src/core/persistence/graduation-copy.ts@@ -129,7 +129,7 @@ const ENABLE_BY_MODE: Readonly<Record<string, string>> = { O: 'ENABLE TRIGGER',async function readSourceBatch(source: BrainEngine, plan: DigestPlan, extraWhere: string | null, afterKey: readonly string[] | null, limit: number):Promise<Array<{ key: string[]; values: Array<string | null> }>> {const collate = (c: ColumnMeta, expr: string) => c.collatable ? `${expr} COLLATE "C"` : expr;- const order = plan.key.map(k => collate(k, quoteIdent(k.name))).join(', ');+ const order = plan.key.map(k => collate(k, `t.${quoteIdent(k.name)}`)).join(', ');const where = [plan.filter ? `(${plan.filter})` : '',extraWhere ? `(${extraWhere})` : '',diff --git a/src/core/persistence/graduation-digest.ts b/src/core/persistence/graduation-digest.tsindex 32b51451a2..ec7323747c 100644--- a/src/core/persistence/graduation-digest.ts+++ b/src/core/persistence/graduation-digest.ts@@ -121,9 +121,9 @@ export async function digestPlan(engine: BrainEngine, entry: InventoryEntry, appif (!columns.some(c => c.name === column)) throw new Error(`Transform names ${entry.relation}.${column}, which does not exist.`);if (key.some(k => k.name === column)) throw new Error(`Transform on primary-key column ${entry.relation}.${column} is not permitted.`);}- const keyCols = key.map((k, i) => `${quoteIdent(k.name)}::text AS ${quoteIdent(`k${i}`)}`);+ const keyCols = key.map((k, i) => `t.${quoteIdent(k.name)}::text AS ${quoteIdent(`k${i}`)}`);const valueCols = columns.map((c, i) => `(${transforms.get(c.name) ?? quoteIdent(c.name)})::text AS ${quoteIdent(`c${i}`)}`);- return { relation: entry.relation, columns, key, select: `SELECT ${[...keyCols, ...valueCols].join(', ')} FROM ${quoteIdent(entry.relation)}`, filter: entry.rowFilter ?? '' };+ return { relation: entry.relation, columns, key, select: `SELECT ${[...keyCols, ...valueCols].join(', ')} FROM ${quoteIdent(entry.relation)} AS t`, filter: entry.rowFilter ?? '' };}export interface DigestRow { key: readonly string[]; text: string }@@ -132,7 +132,7 @@ const collate = (c: ColumnMeta, expr: string) => c.collatable ? `${expr} COLLATE/** Up to `limit` rows strictly after `afterKey` (raw key texts), in COLLATE "C" primary-key order. Run inside a digest transaction. */export async function readDigestRows(tx: BrainEngine, plan: DigestPlan, afterKey: readonly string[] | null, limit: number): Promise<DigestRow[]> {- const order = plan.key.map(k => collate(k, quoteIdent(k.name))).join(', ');+ const order = plan.key.map(k => collate(k, `t.${quoteIdent(k.name)}`)).join(', ');const where = [plan.filter ? `(${plan.filter})` : '',afterKey ? `(${order}) > (${plan.key.map((k, i) => collate(k, `$${i + 1}::${k.type}`)).join(', ')})` : '',diff --git a/src/core/persistence/graduation-verify.ts b/src/core/persistence/graduation-verify.tsindex ca9f895953..80956d3fea 100644--- a/src/core/persistence/graduation-verify.ts+++ b/src/core/persistence/graduation-verify.ts@@ -87,6 +87,12 @@ async function verifyTable(e: GraduationEngines, entry: InventoryEntry, recordedfailures.push({ relation, kind: 'digest', detail: `The source changed since the copy (rows ${recorded.rows} -> ${source.rows}); re-copy this table and its FK closure.` });}if (source.rows !== target.rows) failures.push({ relation, kind: 'count', detail: `Source has ${source.rows} rows, target has ${target.rows}.` });+ const countSql = `SELECT count(*)::text AS n FROM ${quoteIdent(relation)}${entry.rowFilter ? ` WHERE (${entry.rowFilter})` : ''}`;+ const [[srcCount], [dstCount]] = await Promise.all([+ e.source.executeRaw<{ n: string }>(countSql), e.target.executeRaw<{ n: string }>(countSql)]);+ for (const [side, counted, digested] of [['source', Number(srcCount?.n), source.rows], ['target', Number(dstCount?.n), target.rows]] as const) {+ if (counted !== digested) failures.push({ relation, kind: 'count', detail: `The ${side} has ${counted} rows by count(*) but the keyset digest read ${digested}; batching skipped rows.` });+ }if (source.rootSha256 !== target.rootSha256) {const diff = await firstDifference(e, entry, source, target, batchRows);failures.push({ relation, kind: 'digest', ...diff, detail: `Canonical content differs${diff.firstKey ? ` first at key ${diff.firstKey}` : ''}${diff.column ? ` in column ${diff.column}` : ''}.` });