Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(graduation): the trust bookkeeping keys a fresh initSchema seeds do not make a target non-empty

garrytan6eb1da45af14
diff --git a/src/core/eligibility/scan.ts b/src/core/eligibility/scan.ts
index 7f556e84d8..8a7d1e98e3 100644
--- a/src/core/eligibility/scan.ts
+++ b/src/core/eligibility/scan.ts
@@ -25,6 +25,7 @@ import {
type WriteGateAssessment, type WriteGateConfig,
} from '../write-gate.ts';
import { recordWriteGateReceipt } from '../write-gate-store.ts';
+import { WRITE_GATE_SCAN_BASELINE_KEY } from '../write-gate-schema.ts';
import { storedTrustTier, TRUST_TIER_RANK, TRUST_TIERS, type TrustTier } from '../trust/tier.ts';
import { ACTIVATION_TIER_CEILING } from './sql.ts';
@@ -69,7 +70,7 @@ async function saveCursor(engine: Pick<BrainEngine, 'executeRaw'>, cursor: Curso
* under the current detector bounds the scan; a detector bump rescans all.
*/
async function readBaseline(engine: Pick<BrainEngine, 'executeRaw'>): Promise<Cursor['until'] | null> {
- const [row] = await engine.executeRaw<{ value: string }>(`SELECT value FROM config WHERE key = 'write_gate.scan_baseline'`);
+ const [row] = await engine.executeRaw<{ value: string }>('SELECT value FROM config WHERE key = $1', [WRITE_GATE_SCAN_BASELINE_KEY]);
const parsed = row ? JSON.parse(row.value) as { detector_version?: unknown; until?: unknown } : null;
return parsed?.detector_version === WRITE_GATE_DETECTOR_VERSION && parsed.until && typeof parsed.until === 'object'
? parsed.until as Cursor['until'] : null;
diff --git a/src/core/persistence/engine-graduation.ts b/src/core/persistence/engine-graduation.ts
index 877b2ba869..71dc3a5cb2 100644
--- a/src/core/persistence/engine-graduation.ts
+++ b/src/core/persistence/engine-graduation.ts
@@ -60,6 +60,8 @@ import {
} from './graduation-target.ts';
import { drainForGraduation, freezeSource, graduationBlockers, withSourceWritable } from './graduation-drain.ts';
import { buildDeferredIndexes, copySequences, copyTable, deferIndexes, detectTriggerBypass, reenableTriggers } from './graduation-copy.ts';
+import { TRUST_BACKFILL_COMPLETED_KEY } from '../trust/schema.ts';
+import { WRITE_GATE_SCAN_BASELINE_KEY } from '../write-gate-schema.ts';
const DEFAULT_DRAIN_TIMEOUT_MS = 60_000;
const HANDOFF_TIMEOUT_MS = 30_000;
@@ -421,7 +423,9 @@ function planHashOf(input: Record<string, unknown>): string {
function probeTargetEmpty(probe: TargetProbe): boolean { return probe.empty; }
/** initSchema writes these config keys (plus the copier's deferred-index marker); any other key is user data. */
-const SEED_CONFIG_KEYS = new Set(['chunk_strategy', 'embedding_dimensions', 'embedding_model', 'engine', 'version', 'graduation.deferred_indexes']);
+// The trust migrations seed two bookkeeping keys on an empty brain (v220 backfill completion, v221 scan baseline).
+const SEED_CONFIG_KEYS = new Set(['chunk_strategy', 'embedding_dimensions', 'embedding_model', 'engine', 'version', 'graduation.deferred_indexes',
+ TRUST_BACKFILL_COMPLETED_KEY, WRITE_GATE_SCAN_BASELINE_KEY]);
async function userConfigKeys(main: BrainEngine): Promise<string[]> {
const rows = await main.executeRaw<{ key: string }>('SELECT key FROM config ORDER BY key COLLATE "C"');
return rows.map(r => r.key).filter(key => !SEED_CONFIG_KEYS.has(key));
diff --git a/src/core/schema-migrations/v221-write-gate.ts b/src/core/schema-migrations/v221-write-gate.ts
index 67bcffdef2..51d822fa18 100644
--- a/src/core/schema-migrations/v221-write-gate.ts
+++ b/src/core/schema-migrations/v221-write-gate.ts
@@ -1,5 +1,5 @@
import type { Migration } from './types.ts';
-import { WRITE_GATE_DETECTOR_VERSION, WRITE_GATE_SCHEMA_SQL } from '../write-gate-schema.ts';
+import { WRITE_GATE_DETECTOR_VERSION, WRITE_GATE_SCAN_BASELINE_KEY, WRITE_GATE_SCHEMA_SQL } from '../write-gate-schema.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.
@@ -18,7 +18,7 @@ export const v221: Migration = {
idempotent: true,
sql: `${WRITE_GATE_SCHEMA_SQL}
INSERT INTO config (key, value)
- SELECT 'write_gate.scan_baseline', jsonb_build_object('detector_version', ${WRITE_GATE_DETECTOR_VERSION}, 'until', jsonb_build_object(
+ SELECT '${WRITE_GATE_SCAN_BASELINE_KEY}', jsonb_build_object('detector_version', ${WRITE_GATE_DETECTOR_VERSION}, 'until', jsonb_build_object(
'pages', COALESCE((SELECT max(id) FROM pages), 0), 'facts', COALESCE((SELECT max(id) FROM facts), 0),
'takes', COALESCE((SELECT max(id) FROM takes), 0), 'timeline_entries', COALESCE((SELECT max(id) FROM timeline_entries), 0)))::text
ON CONFLICT (key) DO NOTHING;
diff --git a/src/core/write-gate-schema.ts b/src/core/write-gate-schema.ts
index 09c75efb69..e0a7efbfb3 100644
--- a/src/core/write-gate-schema.ts
+++ b/src/core/write-gate-schema.ts
@@ -24,6 +24,9 @@
*/
export const WRITE_GATE_DETECTOR_VERSION = 2;
+/** Config key v221 seeds: the detector version and per-table max ids when the gate went live (eligibility/scan.ts). */
+export const WRITE_GATE_SCAN_BASELINE_KEY = 'write_gate.scan_baseline';
+
export const WRITE_GATE_SCHEMA_SQL = `
CREATE TABLE IF NOT EXISTS write_gate_receipts (
id BIGSERIAL PRIMARY KEY,