Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(minions): restored-operation journey for legacy jobs on PGLite and Postgres (#5157, DX-O15(a), DX-O3(f)(g))

garrytan8f57f3903447
diff --git a/scripts/e2e-test-map.ts b/scripts/e2e-test-map.ts
index e882386380..bfdcd2d9d5 100644
--- a/scripts/e2e-test-map.ts
+++ b/scripts/e2e-test-map.ts
@@ -30,7 +30,11 @@ export const E2E_TEST_MAP: Record<string, string[]> = {
"src/core/source-delete.ts": ["test/e2e/sources-remove-bindings.test.ts"],
"src/core/persistence/orphan-bindings.ts": ["test/e2e/sources-remove-bindings.test.ts"],
// Fix wave 5 shared commit: preview-bound approved sets in op_checkpoints.
- "src/core/persistence/preview-approval.ts": ["test/e2e/preview-approval-postgres.test.ts"],
+ "src/core/persistence/preview-approval.ts": ["test/e2e/preview-approval-postgres.test.ts", "test/e2e/minions-legacy-journey-postgres.test.ts"],
+ // #5157: the legacy recovery commands and doctor check.
+ "src/commands/jobs/authorize-legacy.ts": ["test/e2e/minions-legacy-journey-postgres.test.ts"],
+ "src/commands/jobs/cancel.ts": ["test/e2e/minions-legacy-journey-postgres.test.ts"],
+ "src/commands/doctor/checks/legacy-job-authority.ts": ["test/e2e/minions-legacy-journey-postgres.test.ts"],
"src/core/persistence/deactivation.ts": ["test/e2e/persistence-deactivate-race.test.ts", "test/e2e/fix-wave-4-integration.test.ts"],
// SkillOpt orchestrator, outcome/resume, models plan + strict mode, spend ledger.
"src/core/skillopt/**": [
@@ -193,6 +197,7 @@ export const E2E_TEST_MAP: Record<string, string[]> = {
// Any minions queue/worker/handler change exercises all minion E2E.
"src/core/minions/**": [
"test/e2e/minions-legacy-coalesce-postgres.test.ts",
+ "test/e2e/minions-legacy-journey-postgres.test.ts",
"test/e2e/worker-readiness-cli.test.ts",
"test/e2e/worker-configuration-release.test.ts",
"test/e2e/delegated-grants-withdrawal.test.ts",
@@ -485,7 +490,7 @@ export const E2E_TEST_MAP: Record<string, string[]> = {
"src/commands/serve-http-admin-api.ts": ["test/e2e/serve-http-consent.test.ts", "test/e2e/serve-http-oauth.test.ts"],
"src/commands/serve-http-metrics.ts": ["test/e2e/serve-http-oauth.test.ts"],
"src/commands/serve-http-spa.ts": ["test/e2e/serve-http-consent.test.ts"],
- "src/commands/serve-http-webhooks.ts": ["test/e2e/serve-http-ingest-webhook.test.ts"],
+ "src/commands/serve-http-webhooks.ts": ["test/e2e/serve-http-ingest-webhook.test.ts", "test/e2e/minions-legacy-journey-postgres.test.ts"],
"src/commands/serve-http-mcp.ts": [
"test/e2e/serve-http-oauth.test.ts",
"test/e2e/harness-access.test.ts",
diff --git a/test/e2e/minions-legacy-journey-postgres.test.ts b/test/e2e/minions-legacy-journey-postgres.test.ts
new file mode 100644
index 0000000000..96820da4b1
--- /dev/null
+++ b/test/e2e/minions-legacy-journey-postgres.test.ts
@@ -0,0 +1,24 @@
+/**
+ * #5157 restored-operation journey on real Postgres (DX-O15(a),
+ * DX-O3(f)(g)); the PGLite run is `test/minions-legacy-journey.test.ts`.
+ */
+import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
+import { getEngine, hasDatabase, setupDB, teardownDB } from './helpers.ts';
+import { runLegacyJourney, runUnsupportedRowRecovery } from '../helpers/legacy-journey.ts';
+
+
+const suite = hasDatabase() ? describe : describe.skip;
+suite('Postgres legacy job recovery journey (#5157)', () => {
+ beforeAll(async () => { await setupDB(); }, 120_000);
+ afterAll(async () => { await teardownDB(); });
+ beforeEach(async () => { await getEngine().executeRaw('DELETE FROM minion_jobs'); });
+
+ test('/ingest 409 -> printed commands verbatim -> 202, worker runs, search finds the memory', async () => {
+ const { commands } = await runLegacyJourney(getEngine());
+ expect(commands).toHaveLength(6);
+ }, 120_000);
+
+ test('an unsupported non-NULL row needs its own printed cancel before a worker starts', async () => {
+ expect(await runUnsupportedRowRecovery(getEngine())).toHaveLength(3);
+ }, 120_000);
+});
diff --git a/test/helpers/legacy-journey.ts b/test/helpers/legacy-journey.ts
new file mode 100644
index 0000000000..2f44d27b40
--- /dev/null
+++ b/test/helpers/legacy-journey.ts
@@ -0,0 +1,224 @@
+/**
+ * #5157 restored-operation journey (CEO-A4, DX-O3(f)(g), DX-O15(a)), shared
+ * by the PGLite unit test and the Postgres E2E test.
+ *
+ * A brain upgraded across v0.50 holds terminal and live legacy keyed rows,
+ * an unkeyed live SQL NULL row and an active row the upgrade orphaned.
+ * Terminal rows need no operator step. `POST /ingest` returns 409 with a
+ * hint; the journey then runs the commands the product prints, verbatim,
+ * through the real `gbrain jobs` dispatcher, counts them, and proves
+ * restored operation: `/ingest` returns 202, a worker starts and completes
+ * the queued synthesize and ingest work, and `search` finds the memory.
+ */
+import { expect } from 'bun:test';
+import express from 'express';
+import type { Server } from 'node:http';
+import type { AddressInfo } from 'node:net';
+import type { BrainEngine } from '../../src/core/engine.ts';
+import { mountWebhooks } from '../../src/commands/serve-http-webhooks.ts';
+import type { ServeHttpContext } from '../../src/commands/serve-http.ts';
+import { runJobs } from '../../src/commands/jobs.ts';
+import { _resetCliExitVerdictForTests, currentExitCode } from '../../src/core/cli-force-exit.ts';
+import { makeIngestCaptureHandler } from '../../src/core/minions/handlers/ingest-capture.ts';
+import { MinionQueue } from '../../src/core/minions/queue.ts';
+import { MinionWorker } from '../../src/core/minions/worker.ts';
+import { assertNoUnreviewedJobs } from '../../src/core/minions/submission-authority.ts';
+import { legacyJobAuthorityCheck } from '../../src/commands/doctor/checks/legacy-job-authority.ts';
+import { operations } from '../../src/core/operations.ts';
+import type { OperationContext } from '../../src/core/ops/contract.ts';
+
+export const JOURNEY_CLIENT = 'journey-client';
+
+/** A minimal `gbrain serve --http` stand-in: the real /ingest route on an ephemeral port. */
+export async function startIngestServer(engine: BrainEngine): Promise<{ base: string; stop(): Promise<void> }> {
+ const app = express();
+ const pass: express.RequestHandler = (_req, _res, next) => next();
+ mountWebhooks(app, {
+ engine,
+ resourceVerifier: {
+ verifyAccessToken: async (token: string) => ({ token, clientId: JOURNEY_CLIENT, clientName: 'example-client', scopes: ['read', 'write'], sourceId: 'default', expiresAt: Math.floor(Date.now() / 1000) + 3600 }),
+ },
+ resourceMetadataUrl: 'http://127.0.0.1/.well-known/oauth-protected-resource',
+ ingestRateLimiter: pass,
+ githubWebhookLimiter: pass,
+ broadcastEvent: () => {},
+ } as unknown as ServeHttpContext);
+ const server = await new Promise<Server>(resolve => { const s = app.listen(0, '127.0.0.1', () => resolve(s)); });
+ return {
+ base: `http://127.0.0.1:${(server.address() as AddressInfo).port}`,
+ stop: () => new Promise<void>(resolve => server.close(() => resolve())),
+ };
+}
+
+export function postIngest(base: string, content: string) {
+ return fetch(`${base}/ingest`, { method: 'POST', headers: { authorization: 'Bearer test-only', 'content-type': 'text/markdown' }, body: content });
+}
+
+/** Splits one printed `gbrain ...` command into argv, honoring double quotes. */
+export function argv(command: string): string[] {
+ return [...command.matchAll(/"([^"]*)"|(\S+)/g)].map(m => m[1] ?? m[2]!);
+}
+
+/** Runs a printed `gbrain jobs …` command verbatim through the real dispatcher. */
+export async function runPrinted(engine: BrainEngine, command: string): Promise<{ out: string; err: string; exit: number }> {
+ const args = argv(command);
+ expect(args.slice(0, 2)).toEqual(['gbrain', 'jobs']);
+ const out: string[] = [], err: string[] = [];
+ const log = console.log, error = console.error;
+ console.log = (...a: unknown[]) => { out.push(a.join(' ')); };
+ console.error = (...a: unknown[]) => { err.push(a.join(' ')); };
+ _resetCliExitVerdictForTests();
+ try { await runJobs(engine, args.slice(2)); } finally { console.log = log; console.error = error; }
+ const exit = currentExitCode();
+ _resetCliExitVerdictForTests();
+ process.exitCode = 0;
+ return { out: out.join('\n'), err: err.join('\n'), exit };
+}
+
+/** Rewrites rows to what an upgrade across v0.50 leaves behind (the protocol trigger forbids inserting them). */
+async function legacy(engine: BrainEngine, id: number, status?: string): Promise<void> {
+ if (status === 'active') {
+ await engine.executeRaw(`UPDATE minion_jobs SET status = 'active', lock_token = 'pre-upgrade', lock_until = now() + interval '1 hour',
+ claim_generation = claim_generation + 1 WHERE id = $1`, [id]);
+ } else if (status) {
+ await engine.executeRaw('UPDATE minion_jobs SET status = $2 WHERE id = $1', [id, status]);
+ }
+ await engine.executeRaw('UPDATE minion_jobs SET submission_authority = NULL WHERE id = $1', [id]);
+}
+
+async function drain(engine: BrainEngine, ids: number[]): Promise<Record<number, string>> {
+ const queue = new MinionQueue(engine);
+ const worker = new MinionWorker(engine, { pollInterval: 10, healthCheckInterval: 0, stalledInterval: 60_000 });
+ worker.register('ingest_capture', makeIngestCaptureHandler(engine));
+ worker.register('synthesize', async () => ({ synthesized: true }));
+ const running = worker.start();
+ const statuses = async () => Object.fromEntries(await Promise.all(ids.map(async id => [id, (await queue.getJob(id))?.status ?? 'missing'])));
+ for (let i = 0; i < 400; i++) {
+ if (Object.values(await statuses()).every(status => status === 'completed' || status === 'failed')) break;
+ await new Promise(r => setTimeout(r, 25));
+ }
+ worker.stop();
+ await running;
+ return statuses();
+}
+
+export interface JourneyResult { commands: string[] }
+
+/** `gbrain search <query>` through the search operation, as a trusted local caller; returns hit slugs. */
+async function searchSlugs(engine: BrainEngine, query: string): Promise<string[]> {
+ const op = operations.find(o => o.name === 'search')!;
+ const ctx: OperationContext = { engine, config: {} as OperationContext['config'], dryRun: false, remote: false, sourceId: 'default', logger: { info() {}, warn() {}, error() {} } };
+ const result = await op.handler(ctx, { query }) as unknown;
+ const rows = (Array.isArray(result) ? result : (result as { results?: unknown[] }).results ?? []) as Array<{ slug?: string }>;
+ return rows.map(row => String(row.slug));
+}
+
+export async function runLegacyJourney(engine: BrainEngine): Promise<JourneyResult> {
+ const queue = new MinionQueue(engine);
+ const trusted = { allowProtectedSubmit: true };
+ const content = '# Journey capture\n\nThe zebra-journey-marker note survives the v0.50 upgrade.';
+
+ // The capture was queued before the upgrade: its row holds the capture's key.
+ let serve = await startIngestServer(engine);
+ const first = await postIngest(serve.base, content);
+ expect(first.status).toBe(202);
+ const captureId = (await first.json() as { job_id: number }).job_id;
+ await legacy(engine, captureId);
+ const completed = await queue.add('synthesize', { day: 1 }, { idempotency_key: 'dream:synth-v2:journey-done' }, trusted);
+ await legacy(engine, completed.id, 'completed');
+ const dead = await queue.add('synthesize', { day: 2 }, { idempotency_key: 'dream:synth-v2:journey-dead' }, trusted);
+ await legacy(engine, dead.id, 'dead');
+ const unkeyed = await queue.add('synthesize', { day: 3 }, {}, trusted);
+ await legacy(engine, unkeyed.id);
+ const orphan = await queue.add('embed', { orphaned: true });
+ await legacy(engine, orphan.id, 'active');
+
+ // Terminal legacy keys need no operator step for application callers.
+ expect((await queue.add('synthesize', { day: 1 }, { idempotency_key: 'dream:synth-v2:journey-done' }, trusted)).id).toBe(completed.id);
+ const resubmitted = await queue.add('synthesize', { day: 2 }, { idempotency_key: 'dream:synth-v2:journey-dead' }, trusted);
+ expect(resubmitted.id).not.toBe(dead.id);
+
+ // Live rows: doctor names the recovery, workers are blocked, and /ingest says 409, never 500.
+ expect((await legacyJobAuthorityCheck(engine)).status).toBe('fail');
+ await expect(assertNoUnreviewedJobs(engine)).rejects.toThrow('legacy jobs');
+ const refused = await postIngest(serve.base, content);
+ expect(refused.status).toBe(409);
+ const body = await refused.json() as { error: string; hint: string; docs_url: string };
+ expect(body.error).toBe('permission_denied');
+ expect(body.docs_url).toBe('docs/guides/repair.md#legacy-job-authority');
+
+ const commands: string[] = [];
+ // 1. Stop producers and workers: the hint's first instruction.
+ expect(body.hint).toStartWith('Stop producers (gbrain serve, gbrain autopilot) and workers');
+ await serve.stop();
+ commands.push('stop gbrain serve');
+ // 2. Cancel the active job the hint names.
+ const cancel = /cancel active jobs \((gbrain jobs cancel \d+)\)/.exec(body.hint)?.[1];
+ expect(cancel).toBe(`gbrain jobs cancel ${orphan.id}`);
+ expect((await runPrinted(engine, cancel!)).exit).toBe(0);
+ commands.push(cancel!);
+ // 3. Preview with the hint's filled --select.
+ const preview = /preview with (gbrain jobs authorize-legacy --select "[^"]+")/.exec(body.hint)?.[1];
+ expect(preview).toBe('gbrain jobs authorize-legacy --select "status=waiting|delayed|waiting-children|paused"');
+ const previewed = await runPrinted(engine, preview!);
+ expect(previewed.exit).toBe(0);
+ expect(previewed.out).toContain('(SQL NULL authority, authorizable): 2');
+ commands.push(preview!);
+ // 4. Apply with the printed --expect.
+ const apply = /Apply exactly this set: (.+)$/m.exec(previewed.out)?.[1];
+ expect(apply).toMatch(/--expect [a-f0-9]{64} --yes$/);
+ expect((await runPrinted(engine, apply!)).exit).toBe(0);
+ commands.push(apply!);
+ // 5. Restart serve.
+ serve = await startIngestServer(engine);
+ commands.push('restart gbrain serve');
+
+ await assertNoUnreviewedJobs(engine);
+ expect((await legacyJobAuthorityCheck(engine)).status).toBe('ok');
+ const accepted = await postIngest(serve.base, content);
+ expect(accepted.status).toBe(202);
+ expect((await accepted.json() as { job_id: number }).job_id).toBe(captureId);
+ await serve.stop();
+
+ // A worker starts and completes the queued synthesize and capture work.
+ const statuses = await drain(engine, [captureId, unkeyed.id, resubmitted.id]);
+ expect(statuses).toEqual({ [captureId]: 'completed', [unkeyed.id]: 'completed', [resubmitted.id]: 'completed' });
+
+ // 6. search finds the ingested memory.
+ const hits = await searchSlugs(engine, 'zebra-journey-marker');
+ commands.push('gbrain search zebra-journey-marker');
+ expect(hits.some(slug => slug.startsWith('inbox/'))).toBe(true);
+ expect(commands.length).toBeLessThanOrEqual(6);
+ return { commands };
+}
+
+/**
+ * DX-O3(g): an unsupported non-NULL row keeps workers blocked after the
+ * SQL NULL rows are authorized; its own printed step (local cancel) clears
+ * it and a worker starts.
+ */
+export async function runUnsupportedRowRecovery(engine: BrainEngine): Promise<string[]> {
+ const queue = new MinionQueue(engine);
+ const trusted = { allowProtectedSubmit: true };
+ const legacyRow = await queue.add('synthesize', { day: 4 }, {}, trusted);
+ await legacy(engine, legacyRow.id);
+ const future = await queue.add('synthesize', { day: 5 }, {}, trusted);
+ await engine.executeRaw(`UPDATE minion_jobs SET submission_authority = '{"version":2,"kind":"application"}'::jsonb WHERE id = $1`, [future.id]);
+
+ const doctor = await legacyJobAuthorityCheck(engine);
+ const commands: string[] = [];
+ const preview = /preview with (gbrain jobs authorize-legacy --select "[^"]+")/.exec(doctor.message)![1]!;
+ const previewed = await runPrinted(engine, preview);
+ expect(previewed.out).toContain(`Unsupported non-NULL authority (not authorizable; cancel locally): ${future.id}`);
+ commands.push(preview);
+ const apply = /Apply exactly this set: (.+)$/m.exec(previewed.out)![1]!;
+ expect((await runPrinted(engine, apply)).exit).toBe(0);
+ commands.push(apply);
+ await expect(assertNoUnreviewedJobs(engine)).rejects.toThrow('1 legacy jobs');
+ const cancel = new RegExp(`gbrain jobs cancel ${future.id}\\b`).exec(doctor.message)![0];
+ expect((await runPrinted(engine, cancel)).exit).toBe(0);
+ commands.push(cancel);
+ await assertNoUnreviewedJobs(engine);
+ expect(await drain(engine, [legacyRow.id])).toEqual({ [legacyRow.id]: 'completed' });
+ return commands;
+}
diff --git a/test/minions-legacy-journey.test.ts b/test/minions-legacy-journey.test.ts
new file mode 100644
index 0000000000..ff5454c3f5
--- /dev/null
+++ b/test/minions-legacy-journey.test.ts
@@ -0,0 +1,30 @@
+/**
+ * #5157 restored-operation journey on PGLite (DX-O15(a), DX-O3(f)(g)); the
+ * Postgres run is `test/e2e/minions-legacy-journey-postgres.test.ts`. Both
+ * drive `test/helpers/legacy-journey.ts`.
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { runLegacyJourney, runUnsupportedRowRecovery } from './helpers/legacy-journey.ts';
+
+let engine: PGLiteEngine;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+}, 60_000);
+afterAll(async () => { await engine?.disconnect(); }, 60_000);
+
+describe('legacy job recovery journey (PGLite)', () => {
+ test('/ingest 409 -> printed commands verbatim -> 202, worker runs, search finds the memory', async () => {
+ const { commands } = await runLegacyJourney(engine);
+ expect(commands).toHaveLength(6);
+ }, 120_000);
+
+ test('an unsupported non-NULL row needs its own printed cancel before a worker starts', async () => {
+ await engine.executeRaw('DELETE FROM minion_jobs');
+ const commands = await runUnsupportedRowRecovery(engine);
+ expect(commands).toHaveLength(3);
+ }, 120_000);
+});