Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* CLI→MCP gap-closure wave — get_job_stats op (`gbrain jobs stats` was the
* one jobs verb with no MCP equivalent). Pins the payload shape, the shared
* wedge derivation (queue.ts deriveWedgeSignal — same math as the CLI line
* and the doctor wedged_queue check, #1801), and the relation-missing guard.
*/
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { dispatchToolCall } from '../src/mcp/dispatch.ts';
import { deriveWedgeSignal } from '../src/core/minions/queue.ts';
import type { BrainEngine } from '../src/core/engine.ts';
import { operationsByName } from '../src/core/operations.ts';
import { withEnv } from './helpers/with-env.ts';
let engine: PGLiteEngine;
beforeAll(async () => {
engine = new PGLiteEngine();
await engine.connect({});
await engine.initSchema();
});
afterAll(async () => {
await engine.disconnect();
});
const STDIO = { remote: true, transport: 'stdio' as const, sourceId: 'default' };
function parsed(result: { content: Array<{ text: string }> }) {
return JSON.parse(result.content[0].text);
}
describe('get_job_stats op', () => {
test('returns the stats shape with the derived wedge signal (empty queue → not wedged)', async () => {
const res = await dispatchToolCall(engine, 'get_job_stats', {}, { ...STDIO });
expect(res.isError ?? false).toBe(false);
const body = parsed(res);
expect(body.schema_version).toBe(1);
expect(body.window_hours).toBe(24);
expect(typeof body.by_status).toBe('object');
expect(Array.isArray(body.by_type)).toBe(true);
expect(typeof body.queue_health.waiting).toBe('number');
expect(body.wedge.queue).toBe('default');
// Empty queue: nothing waiting → never wedged.
expect(body.wedged).toBe(false);
expect(body.wedge_threshold_minutes).toBeGreaterThan(0);
});
test('queue + since_hours params: wedge scopes to the named queue, window clamps', async () => {
const res = await dispatchToolCall(engine, 'get_job_stats', { queue: 'reports', since_hours: 100000 }, { ...STDIO });
const body = parsed(res);
expect(body.wedge.queue).toBe('reports');
expect(body.window_hours).toBe(720); // clamp ceiling
});
test('admin scope declared (jobs-family consistency)', () => {
expect(operationsByName['get_job_stats'].scope).toBe('admin');
expect(operationsByName['get_job_stats'].area).toBe('jobs');
});
test('missing minions schema → actionable unavailable, not a raw relation error', async () => {
const bare = {
executeRaw: async (sql: string) => {
if (sql.includes('minion_jobs')) throw new Error('relation "minion_jobs" does not exist');
return [];
},
} as unknown as BrainEngine;
const res = await dispatchToolCall(bare, 'get_job_stats', {}, { ...STDIO });
expect(res.isError).toBe(true);
const body = parsed(res);
expect(body.error).toBe('unavailable');
expect(body.reason).toBe('schema_missing');
expect(body.message).not.toContain('relation "minion_jobs"');
// The cause (pending migrations or a dropped table) is not knowable here: the fix is the diagnostic
// doctor read, and the suggestion quotes it instead of naming a different command.
expect(body.suggestion).toContain('pending schema migrations');
expect(body.fix.mcp).toEqual({ tool: 'run_doctor', arguments: {} });
expect(body.suggestion).toContain('Next: run_doctor {}');
expect(body.suggestion).not.toContain('apply-migrations');
});
});
describe('get_job_stats — private dream-inline queues', () => {
const PRIVATE_QUEUE = 'dream-inline-1700000000000-abcd1234';
test('a would-be-wedged dream-inline queue reports private_queue true, wedged false', async () => {
// Exact wedge signature (waiting, 0 live-lock active, stale completion):
// ONLY the private-queue classification keeps wedged false here — if it
// regressed, this fixture would trip the wedge derivation.
await engine.executeRaw(
`INSERT INTO minion_jobs (submission_authority, name, queue, status, created_at, updated_at)
VALUES ('{"version":1,"kind":"application"}'::jsonb, 'subagent', '${PRIVATE_QUEUE}', 'waiting', now() - interval '2 hours', now() - interval '2 hours')`,
);
await engine.executeRaw(
`INSERT INTO minion_jobs (submission_authority, name, queue, status, updated_at)
VALUES ('{"version":1,"kind":"application"}'::jsonb, 'subagent', '${PRIVATE_QUEUE}', 'completed', now() - interval '90 minutes')`,
);
await withEnv({ GBRAIN_WEDGED_QUEUE_WARN_MINUTES: undefined }, async () => {
const res = await dispatchToolCall(engine, 'get_job_stats', { queue: PRIVATE_QUEUE }, { ...STDIO });
expect(res.isError ?? false).toBe(false);
const body = parsed(res);
expect(body.wedge.queue).toBe(PRIVATE_QUEUE);
expect(body.wedge.waiting).toBeGreaterThan(0); // the fixture really is the wedge shape
expect(body.private_queue).toBe(true);
expect(body.wedged).toBe(false);
});
});
test('the default queue reports private_queue false', async () => {
const res = await dispatchToolCall(engine, 'get_job_stats', { queue: 'default' }, { ...STDIO });
const body = parsed(res);
expect(body.private_queue).toBe(false);
});
test('description pins the private-queue wedge guidance MCP consumers act on', () => {
const desc = operationsByName['get_job_stats'].description ?? '';
expect(desc).toContain('wedged is NEVER true');
expect(desc).toContain('parent-owned dream-inline queue');
});
});
describe('deriveWedgeSignal (shared with the CLI line + doctor check)', () => {
// deriveWedgeSignal reads GBRAIN_WEDGED_QUEUE_WARN_MINUTES per call — pin
// it to unset so a tuned dev/CI host (e.g. =120) can't flip the 60-minute
// fixtures below. The default threshold (15) is what these assertions pin.
test('wedged: no healthy actives, work waiting, stale completion', async () => {
await withEnv({ GBRAIN_WEDGED_QUEUE_WARN_MINUTES: undefined }, async () => {
const { wedged } = deriveWedgeSignal({ active_healthy: 0, waiting: 3, minutes_since_completion: 60 });
expect(wedged).toBe(true);
});
});
test('wedged: no completions on record counts as stale', async () => {
await withEnv({ GBRAIN_WEDGED_QUEUE_WARN_MINUTES: undefined }, async () => {
expect(deriveWedgeSignal({ active_healthy: 0, waiting: 1, minutes_since_completion: null }).wedged).toBe(true);
});
});
test('not wedged: live active row masks nothing, recent completion clears', async () => {
await withEnv({ GBRAIN_WEDGED_QUEUE_WARN_MINUTES: undefined }, async () => {
expect(deriveWedgeSignal({ active_healthy: 1, waiting: 5, minutes_since_completion: 60 }).wedged).toBe(false);
expect(deriveWedgeSignal({ active_healthy: 0, waiting: 5, minutes_since_completion: 2 }).wedged).toBe(false);
expect(deriveWedgeSignal({ active_healthy: 0, waiting: 0, minutes_since_completion: null }).wedged).toBe(false);
});
});
});