Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(contract): flip the #5856/#5854 connector job contract cases; real opt-in key; Postgres dispatch E2E
garrytan686b400171aa
diff --git a/src/commands/autopilot-dispatch.ts b/src/commands/autopilot-dispatch.tsindex 08aa6fea46..4ad4cc3e51 100644--- a/src/commands/autopilot-dispatch.ts+++ b/src/commands/autopilot-dispatch.ts@@ -390,7 +390,7 @@ async function autoDrainUnitsToday(engine: BrainEngine, utcDay: string): Promiseconst { STRUCTURAL_REFUSAL_PREFIX } = await import('../core/cycle/extract-atoms-drain.ts');const rows = await engine.executeRaw<{ cnt: number }>(`SELECT COALESCE(sum(CASE WHEN status = 'dead' AND attempts_started <= 1- AND left(COALESCE(error_text, ''), length($2)) = $2 THEN 0 ELSE GREATEST(attempts_started, 1) END), 0)::int AS cnt+ AND left(COALESCE(error_text, ''), length($2::text)) = $2::text THEN 0 ELSE GREATEST(attempts_started, 1) END), 0)::int AS cntFROM minion_jobs WHERE name = 'extract-atoms-drain' AND created_at >= $1::timestamptz`,[`${utcDay}T00:00:00Z`, STRUCTURAL_REFUSAL_PREFIX],);diff --git a/test/autopilot-auto-drain-dispatch.test.ts b/test/autopilot-auto-drain-dispatch.test.tsindex c7b61aa2fa..7341623359 100644--- a/test/autopilot-auto-drain-dispatch.test.ts+++ b/test/autopilot-auto-drain-dispatch.test.ts@@ -9,21 +9,31 @@ import { afterAll, beforeAll, beforeEach, expect, test } from 'bun:test';import { mkdirSync, mkdtempSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { testBackends } from './helpers/test-backends.ts';import { MinionQueue } from '../src/core/minions/queue.ts';import { submitAutoDrains, AUTO_DRAIN_NEXT_KIND_KEY } from '../src/commands/autopilot-dispatch.ts';import { withEnv } from './helpers/with-env.ts';-let engine: PGLiteEngine;+let engine: BrainEngine;let queue: MinionQueue;let home: string;+let closePostgres: (() => Promise<void>) | undefined;beforeAll(async () => {- engine = new PGLiteEngine();- await engine.connect({});- await engine.initSchema();+ if (testBackends().join() === 'postgres') {+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!);+ engine = pg.engine;+ closePostgres = pg.close;+ } else {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+ }queue = new MinionQueue(engine);-});-afterAll(async () => { await engine.disconnect(); });+}, 120_000);+afterAll(async () => { if (closePostgres) await closePostgres(); else await engine.disconnect(); });async function reset(): Promise<void> {await engine.executeRaw('DELETE FROM minion_jobs');await engine.executeRaw("DELETE FROM pages");diff --git a/test/e2e/autopilot-auto-drain-dispatch.test.ts b/test/e2e/autopilot-auto-drain-dispatch.test.tsnew file mode 100644index 0000000000..f13b43cb46--- /dev/null+++ b/test/e2e/autopilot-auto-drain-dispatch.test.ts@@ -0,0 +1,3 @@+import { registerPostgresTests } from '../helpers/test-backends.ts';++await registerPostgresTests(() => import('../autopilot-auto-drain-dispatch.test.ts'));diff --git a/test/helpers/managed-connector-job-contract.ts b/test/helpers/managed-connector-job-contract.tsindex d52e6193ec..962fac32f3 100644--- a/test/helpers/managed-connector-job-contract.ts+++ b/test/helpers/managed-connector-job-contract.ts@@ -92,10 +92,6 @@ export const EXPECTED_FAILURES: Partial<Record<ContractCase, { issue: string; sigmail_sweep_loops_enqueue: { issue: '#5867', signature: /loops_extract jobs enqueued by the managed sweep: 0/ },loops_close: { issue: '#5869', signature: /loops_close reported fact_expired=true but the fact is still active/ },cycle_extract: { issue: '#5875', signature: /cycle extract phase: skipped \(no_brain_dir\)/ },- atom_drain_default_off: { issue: '#5856', signature: /extract-atoms-drain ended dead after 3 attempt\(s\): Atom maintenance requires the configured canonical owner/ },- atom_drain_opted_in: { issue: '#5856', signature: /extract-atoms-drain ended dead after 3 attempt\(s\): Atom maintenance requires the configured canonical owner/ },- atom_dispatch: { issue: '#5856', signature: /auto-drain submitted extract-atoms-drain for the connector source with connector atoms off/, engines: ['postgres'] },- synthesize_publish_busy_writer: { issue: '#5854', signature: /The write is accepted and is still pending/ },extract_timeline_db: { issue: '#5904', signature: /guard refusals: timeline_entries=/ },};@@ -104,7 +100,7 @@ export const EXPECTED_FAILURES: Partial<Record<ContractCase, { issue: string; si* behind one setting, default off. Lane L6 owns the key's final name; keep* this constant in step with it.*/-export const CONNECTOR_ATOMS_OPT_IN = { key: 'autopilot.auto_drain.connector_sources', on: 'true' } as const;+export const CONNECTOR_ATOMS_OPT_IN = { key: 'cycle.extract_atoms.connector_pages', on: 'true' } as const;/*** Handlers from registerBuiltinHandlers that this contract does not drive on@@ -604,7 +600,8 @@ const cases: Record<ContractCase, (state: CaseState) => Promise<void>> = {async atom_drain_default_off(state) {await gmailSweep(state);- expect(await countExtractAtomsBacklog(state.brain.engine, state.sourceId)).toBeGreaterThan(0);+ // Off, connector email/meeting pages are outside discovery and the backlog (#5856 opt-in).+ expect(await countExtractAtomsBacklog(state.brain.engine, state.sourceId)).toBe(0);const [job] = await runJobs(state.brain.engine, [{ name: 'extract-atoms-drain', data: { sourceId: state.sourceId, window: 60 } }]);if (job.status !== 'completed') throw new Error(`extract-atoms-drain ended ${job.status} after ${job.attempts_started} attempt(s): ${job.error_text ?? ''}`);expect(job.attempts_started).toBe(1);@@ -615,9 +612,9 @@ const cases: Record<ContractCase, (state: CaseState) => Promise<void>> = {async atom_drain_opted_in(state) {await gmailSweep(state);const { engine } = state.brain;- expect(await countExtractAtomsBacklog(engine, state.sourceId)).toBeGreaterThan(0);await engine.setConfig(CONNECTOR_ATOMS_OPT_IN.key, CONNECTOR_ATOMS_OPT_IN.on);try {+ expect(await countExtractAtomsBacklog(engine, state.sourceId)).toBeGreaterThan(0);const [job] = await runJobs(engine, [{ name: 'extract-atoms-drain', data: { sourceId: state.sourceId, window: 60 } }]);if (job.status !== 'completed') throw new Error(`extract-atoms-drain ended ${job.status} after ${job.attempts_started} attempt(s): ${job.error_text ?? ''}`);expect(job.attempts_started).toBe(1);@@ -643,7 +640,7 @@ const cases: Record<ContractCase, (state: CaseState) => Promise<void>> = {};await engine.setConfig('autopilot.auto_drain.threshold', '1');try {- expect(await countExtractAtomsBacklog(engine, state.sourceId)).toBeGreaterThan(1);+ expect(await countExtractAtomsBacklog(engine, state.sourceId)).toBe(0);const off = await tick();// PGLite: the auto-drain dispatch is Postgres-only, so nothing is submitted.if (engine.kind === 'pglite') { expect(off).toHaveLength(0); return; }@@ -652,6 +649,7 @@ const cases: Record<ContractCase, (state: CaseState) => Promise<void>> = {throw new Error('auto-drain submitted extract-atoms-drain for the connector source with connector atoms off');}await engine.setConfig(CONNECTOR_ATOMS_OPT_IN.key, CONNECTOR_ATOMS_OPT_IN.on);+ expect(await countExtractAtomsBacklog(engine, state.sourceId)).toBeGreaterThan(1);const on = await tick();expect(on).toHaveLength(1);const [job] = await drainQueue(engine, on.map(j => j.id));