Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(chronicle): Postgres arm for the ledger rollups; regenerate the Postgres doctor golden (#5876)
garrytan766c8d8487f7
diff --git a/scripts/e2e-test-map.ts b/scripts/e2e-test-map.tsindex 076ed36392..1b61a6beef 100644--- a/scripts/e2e-test-map.ts+++ b/scripts/e2e-test-map.ts@@ -493,7 +493,11 @@ export const E2E_TEST_MAP: Record<string, string[]> = {"src/core/chronicle/**": ["test/e2e/ontology-merge-parity.test.ts","test/e2e/chronicle-event-projection-parity.test.ts",+ "test/e2e/chronicle-surfaces-postgres.test.ts",],+ // #5876: doctor auto_chronicle + the chronicle advisor read the ledger rollups.+ "src/commands/doctor/checks/auto-chronicle.ts": ["test/e2e/chronicle-surfaces-postgres.test.ts"],+ "src/core/advisor/collect-chronicle.ts": ["test/e2e/chronicle-surfaces-postgres.test.ts"],"src/core/ops/chronicle.ts": ["test/e2e/ontology-merge-parity.test.ts","test/e2e/chronicle-event-projection-parity.test.ts",diff --git a/test/e2e/chronicle-surfaces-postgres.test.ts b/test/e2e/chronicle-surfaces-postgres.test.tsnew file mode 100644index 0000000000..c7b8e1255a--- /dev/null+++ b/test/e2e/chronicle-surfaces-postgres.test.ts@@ -0,0 +1,63 @@+/**+ * #5876 (T11) on real Postgres: the chronicle ledger rollups behind doctor `auto_chronicle` and the+ * advisor (FILTER aggregates, numeric -> float8 spend, the reservation/ledger join for writer+ * shares, interval windows) return the same numbers as on PGLite+ * (test/auto-chronicle-surfaces-5876.test.ts). Uses the TEMPORARY ledger stub until Lane 1's+ * `chronicle_page_state` migration merges.+ */+import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';+import { getEngine, hasDatabase, setupDB, teardownDB } from './helpers.ts';+import { readChronicleLedgerStats } from '../../src/core/chronicle/ledger-stats.ts';+import { autoChronicleEntry } from '../../src/commands/doctor/checks/auto-chronicle.ts';+import type { DoctorContext } from '../../src/commands/doctor/context.ts';+import type { Check } from '../../src/commands/doctor.ts';+import { createChronicleLedgerStub, insertChronicleLedgerRow } from '../helpers/chronicle-ledger-stub.ts';++const d = hasDatabase() ? describe : describe.skip;++beforeAll(async () => {+ if (!hasDatabase()) return;+ await setupDB();+ await createChronicleLedgerStub(getEngine());+});+afterAll(async () => {+ if (!hasDatabase()) return;+ await getEngine().executeRaw('DROP TABLE IF EXISTS chronicle_page_state');+ await getEngine().executeRaw('DROP TABLE IF EXISTS chronicle_judge_reservations');+ await teardownDB();+});+beforeEach(async () => {+ if (!hasDatabase()) return;+ await getEngine().executeRaw('DELETE FROM chronicle_page_state');+ await getEngine().executeRaw('DELETE FROM chronicle_judge_reservations');+ for (const k of ['auto_chronicle', 'chronicle.auto_daily_limit']) await getEngine().unsetConfig(k);+});++d('chronicle ledger rollups on Postgres', () => {+ test('24 h calls include retries, writer share, 7-day outcomes and spend kept apart', async () => {+ const engine = getEngine();+ await insertChronicleLedgerRow(engine, { slug: 'meetings/a', state: 'extracted', principal: ['oauth_client', 'client-a'], cost: 0.01, retries: 1 });+ await insertChronicleLedgerRow(engine, { slug: 'meetings/b', state: 'extracted', principal: ['oauth_client', 'client-a'], cost: 0.02 });+ await insertChronicleLedgerRow(engine, { slug: 'meetings/c', state: 'extracted', principal: ['local_cli', 'w1'], unpriced: true });+ await insertChronicleLedgerRow(engine, { slug: 'meetings/old', state: 'extracted', cost: 5, hoursAgo: 24 * 9 });+ await insertChronicleLedgerRow(engine, { slug: 'meetings/s1', state: 'skipped', reason: 'superseded' });+ await insertChronicleLedgerRow(engine, { slug: 'meetings/f1', state: 'failed', reason: 'no_pricing' });+ await insertChronicleLedgerRow(engine, { slug: 'meetings/p', state: 'pending' });+ await insertChronicleLedgerRow(engine, { slug: 'meetings/bf', state: 'extracted', trigger: 'backfill', cost: 1 });+ const stats = await readChronicleLedgerStats(engine);+ expect(stats).toEqual({+ available: true, pending: 1, autoCalls24h: 5,+ principals24h: [{ principal: 'oauth_client:client-a', calls: 3 }, { principal: 'local_cli:w1', calls: 1 }, { principal: 'unattributed', calls: 1 }],+ last7d: { extracted: 3, failed: 1, skipped: 1, reasons: { no_pricing: 1, superseded: 1 }, failedReasons: { no_pricing: 1 } },+ spend7d: { knownUsd: 0.03, unpricedCalls: 1, incompleteRecords: 1 },+ });+ });++ test('doctor reports the failed reason with its fix', async () => {+ const engine = getEngine();+ await insertChronicleLedgerRow(engine, { slug: 'meetings/f1', state: 'failed', reason: 'judge_truncated' });+ const checks = (await autoChronicleEntry.run({ engine, progress: { heartbeat() {} } } as unknown as DoctorContext)) as Check[];+ expect(checks.find((c) => c.name === 'auto_chronicle')).toMatchObject({ status: 'warn',+ details: { code: 'judge_truncated', fix: { argv: ['gbrain', 'config', 'set', 'chronicle.judge_max_tokens', '8000'] } } });+ });+});diff --git a/test/fixtures/goldens/doctor/json-postgres-fresh.json b/test/fixtures/goldens/doctor/json-postgres-fresh.jsonindex 286d7522f2..cb5f47ab96 100644--- a/test/fixtures/goldens/doctor/json-postgres-fresh.json+++ b/test/fixtures/goldens/doctor/json-postgres-fresh.json@@ -1106,12 +1106,100 @@{"category": "brain","details": {- "enabled": false+ "auto_calls_24h": 0,+ "chat_available": false,+ "daily_limit": 200,+ "docs": "docs/guides/life-chronicle.md",+ "enabled": true,+ "job_budget_usd": 0.25,+ "last_7d": {+ "extracted": 0,+ "failed": 0,+ "failedReasons": {},+ "reasons": {},+ "skipped": 0+ },+ "ledger_available": false,+ "pending": 0,+ "principals_24h": [],+ "readiness": "missing",+ "run_now": [+ "gbrain",+ "dream",+ "--phase",+ "chronicle"+ ],+ "severity": "info",+ "source": "default",+ "spend_7d": {+ "incompleteRecords": 0,+ "knownUsd": 0,+ "unpricedCalls": 0+ }},- "message": "auto_chronicle is off.",+ "message": "auto_chronicle is on (default). The chronicle ledger is not created yet; run `gbrain apply-migrations --yes --no-autopilot-install`. No chat provider is configured, so nothing will be extracted until one is.","name": "auto_chronicle","status": "ok"},+ {+ "category": "brain",+ "message": "chronicle.* settings are valid.",+ "name": "chronicle_config_invalid",+ "status": "ok"+ },+ {+ "category": "brain",+ "details": {+ "ask_user": true,+ "code": "auto_chronicle_default_on",+ "decisions": [+ {+ "argv": [+ "gbrain",+ "config",+ "set",+ "auto_chronicle",+ "true"+ ],+ "id": "keep",+ "label": "Keep automatic extraction on"+ },+ {+ "argv": [+ "gbrain",+ "config",+ "set",+ "auto_chronicle",+ "false"+ ],+ "id": "opt_out",+ "label": "Turn automatic extraction off"+ }+ ],+ "default": "keep",+ "docs": "docs/guides/life-chronicle.md",+ "fix": {+ "actor": "agent",+ "argv": [+ "gbrain",+ "config",+ "set",+ "auto_chronicle",+ "true"+ ],+ "consent": [+ "paid",+ "egress"+ ],+ "requires_exclusive": false,+ "why": "Keeping the default records that the user accepted the per-page cost; the alternative is the opt-out."+ },+ "severity": "info"+ },+ "message": "Automatic event extraction is on by default and the user has not confirmed it yet. Each eligible new or changed meeting, conversation or calendar page gets one paid chat call (cap $0.25 per page; at most 200 calls per day, so at most $50.00 per day for a priced model; an unpriced model has no cap), and page text goes to the configured chat provider. Relay this to the user, then run `gbrain config set auto_chronicle true` to keep it or `gbrain config set auto_chronicle false` to opt out.",+ "name": "auto_chronicle_default_on",+ "status": "ok"+ },{"category": "brain","details": {