Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(minions): job deferral E2E on Postgres and PgBouncer; facts_drain matrix row tolerates queued jobs from earlier phases

garrytanc4891650b2d7
diff --git a/test/e2e/job-deferral-postgres.test.ts b/test/e2e/job-deferral-postgres.test.ts
new file mode 100644
index 0000000000..80511bb481
--- /dev/null
+++ b/test/e2e/job-deferral-postgres.test.ts
@@ -0,0 +1,45 @@
+/**
+ * JobDeferredError on Postgres (and, when DATABASE_URL points at a
+ * transaction-mode pooler, through PgBouncer): the worker returns a deferred
+ * job to delayed via MinionQueue.deferJob with no attempt counted and no
+ * stacktrace entry, and a stale lock token defers nothing.
+ */
+import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
+import { hasDatabase, setupDB, teardownDB, getEngine } from './helpers.ts';
+import { MinionQueue } from '../../src/core/minions/queue.ts';
+import { MinionWorker } from '../../src/core/minions/worker.ts';
+import { JobDeferredError } from '../../src/core/minions/errors.ts';
+
+const describeE2E = hasDatabase() ? describe : describe.skip;
+
+describeE2E('E2E: job deferral (Lane D)', () => {
+ beforeAll(async () => { await setupDB(); });
+ afterAll(async () => { await teardownDB(); });
+
+ test('a deferred job waits with no attempt counted', async () => {
+ const queue = new MinionQueue(getEngine());
+ const job = await queue.add('defer-e2e', {}, { max_attempts: 2 });
+ const worker = new MinionWorker(getEngine(), { pollInterval: 50 });
+ worker.register('defer-e2e', async () => { throw new JobDeferredError('no_key', 'waiting for a key', 60_000); });
+ const running = worker.start();
+ for (let i = 0; i < 200; i++) {
+ if ((await queue.getJob(job.id))?.status === 'delayed') break;
+ await new Promise(r => setTimeout(r, 50));
+ }
+ worker.stop();
+ await running;
+ const row = (await queue.getJob(job.id))!;
+ expect(row).toMatchObject({ status: 'delayed', attempts_made: 0, error_text: 'deferred (no_key): waiting for a key' });
+ expect(row.stacktrace ?? []).toEqual([]);
+ expect(row.delay_until!.getTime()).toBeGreaterThan(Date.now() + 30_000);
+ });
+
+ test('deferJob is token-fenced', async () => {
+ const queue = new MinionQueue(getEngine());
+ const job = await queue.add('defer-e2e-fence', {});
+ const claimed = await queue.claim('token-a', 30_000, 'default', ['defer-e2e-fence']);
+ expect(claimed?.id).toBe(job.id);
+ expect(await queue.deferJob(job.id, 'token-b', 'deferred (x): y', 0)).toBeNull();
+ expect((await queue.deferJob(job.id, 'token-a', 'deferred (x): y', 0))?.status).toBe('delayed');
+ });
+});
diff --git a/test/managed-phase-matrix.test.ts b/test/managed-phase-matrix.test.ts
index 3e609ac61a..f0c71cfbda 100644
--- a/test/managed-phase-matrix.test.ts
+++ b/test/managed-phase-matrix.test.ts
@@ -292,6 +292,7 @@ const MATRIX: Record<CyclePhase, Entry> = {
facts_drain: {
config: { embedding_disabled: 'true' },
seed: async ({ engine, sourceId }) => {
+ if (engine.kind !== 'pglite') return;
await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');
await put(engine, sourceId, 'notes/drain-example', page('note', 'Field notes', 'Carol Example founded Widget Co in 2019 and leads its design team. '.repeat(3)));
const held = await engine.executeRaw<{ id: number; next_attempt_at: string | null }>(
@@ -299,13 +300,15 @@ const MATRIX: Record<CyclePhase, Entry> = {
await engine.executeRaw("UPDATE persistence_effects SET next_attempt_at=now()+interval '1 hour' WHERE kind<>'facts-backstop'");
const effect = await claimPersistenceEffect(engine, localHostId());
for (const h of held) await engine.executeRaw('UPDATE persistence_effects SET next_attempt_at=$2::timestamptz WHERE id=$1', [h.id, h.next_attempt_at]);
- expect(effect?.kind).toBe('facts-backstop');
- await dispatchFactsBackstopEffect(engine, effect!, localHostId());
+ if (effect) await dispatchFactsBackstopEffect(engine, effect, localHostId());
+ const jobs = await engine.executeRaw("SELECT id FROM minion_jobs WHERE name='facts-absorb' AND data->>'slug'='notes/drain-example' AND status='waiting'");
+ expect(jobs).toHaveLength(1);
},
reply: () => JSON.stringify({ facts: [{ fact: 'Carol founded Widget Co in 2019', kind: 'fact', entity: 'people/carol-example', confidence: 0.9, notability: 'high' }] }),
assert: async ({ engine, sourceId, result }) => {
if (engine.kind !== 'pglite') { expect(result.details.reason).toBe('not_applicable'); return; }
- expect(result.details).toMatchObject({ outcome: 'drained', completed: 1, backlog_after: 0 });
+ expect(result.details).toMatchObject({ outcome: 'drained', failed: 0, backlog_after: 0 });
+ expect(Number(result.details.completed)).toBeGreaterThanOrEqual(1);
expect(await engine.executeRaw("SELECT fact FROM facts WHERE source_id=$1 AND fact LIKE '%Widget Co%'", [sourceId])).not.toHaveLength(0);
},
},