Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* Evidence delivery off-path golden: with `return_unit: chunk` (explicit or
* from config) search, query, recall, think's prompt and the MCP
* serialization are byte-identical to the pre-feature release, and the
* implied `auto` default is byte-identical wherever no hit is a conversation.
*
* Authoring gate: protects the frozen off-path contract (plan amendment 6)
* and auto's "unchanged elsewhere" promise. Regression: any stage/param/meta
* change that leaks into chunk responses (a `delivery` key, a changed result
* count, recall field order, think excerpting), or auto touching a
* non-conversation hit. The fixture was generated by this same file on the
* release before evidence delivery (refactor wave 1, 9a0e01b4) and must not
* be regenerated on a branch that changes the off path. The cost wave's two
* declared remote-output changes (lean rows, compact MCP JSON) are applied to
* the frozen bytes by `withCostWave`, not regenerated into them.
*
* The Postgres arm lives in test/e2e/evidence-delivery-parity.test.ts.
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import type { SearchResult } from '../src/core/types.ts';
import { dispatchToolCall } from '../src/mcp/dispatch.ts';
import { __resetProcessNoticeLedgerForTests } from '../src/core/notice-ledger.ts';
import { captureOffPath, offPathMcpBytes, OFF_PATH_PAGES, seedOffPath, withCostWave } from './helpers/evidence-delivery-fixture.ts';
import { withEnv } from './helpers/with-env.ts';
const FIXTURE = join(import.meta.dir, 'fixtures/goldens/evidence-delivery/off-path.json');
const CONVERSATION = 'chat/session-a';
let engine: PGLiteEngine;
let plain: PGLiteEngine;
async function withChunkConfig<T>(e: PGLiteEngine, run: () => Promise<T>): Promise<T> {
await e.setConfig('search.return_unit', 'chunk');
await e.setConfig('think.return_unit', 'chunk');
try { return await run(); } finally {
await e.executeRaw(`DELETE FROM config WHERE key IN ('search.return_unit', 'think.return_unit')`);
}
}
beforeAll(async () => {
engine = new PGLiteEngine();
await engine.connect({});
await engine.initSchema();
await seedOffPath(engine);
plain = new PGLiteEngine();
await plain.connect({});
await plain.initSchema();
await seedOffPath(plain, OFF_PATH_PAGES.filter(p => p.slug !== CONVERSATION));
}, 240_000);
afterAll(async () => {
await engine.disconnect();
await plain.disconnect();
}, 240_000);
describe('evidence delivery off path', () => {
test('a config-level chunk unit is byte-identical to the pre-feature release', async () => {
const got = await withChunkConfig(engine, () => captureOffPath(engine));
if (process.env.GBRAIN_TEST_UPDATE_GOLDENS === '1') {
mkdirSync(dirname(FIXTURE), { recursive: true });
writeFileSync(FIXTURE, JSON.stringify(got, null, 2) + '\n');
}
const want = JSON.parse(readFileSync(FIXTURE, 'utf8')) as Record<string, string>;
expect(Object.keys(got).sort()).toEqual(Object.keys(want).sort());
for (const key of Object.keys(want)) expect(`${key}: ${got[key]}`).toBe(`${key}: ${withCostWave(key, want[key])}`);
});
test('an explicit return_unit chunk is byte-identical to the pre-feature release', async () => {
const want = JSON.parse(readFileSync(FIXTURE, 'utf8')) as Record<string, string>;
const got = await captureOffPath(engine);
const keys = Object.keys(want).filter(k => k.endsWith(':chunk'));
expect(keys.length).toBeGreaterThan(5);
for (const key of keys) expect(`${key}: ${got[key]}`).toBe(`${key}: ${withCostWave(key, want[key])}`);
});
test('the implied auto default is byte-identical to chunk on a corpus without conversations', async () => {
const chunk = await withChunkConfig(plain, () => captureOffPath(plain));
const auto = await captureOffPath(plain);
expect(Object.keys(auto).sort()).toEqual(Object.keys(chunk).sort());
for (const key of Object.keys(chunk)) expect(`${key}: ${auto[key]}`).toBe(`${key}: ${chunk[key]}`);
expect(Object.values(auto).some(v => v.includes('"delivery"') || v.includes('"delivered"'))).toBe(false);
});
test('the implied auto default expands only the conversation page; every other row is the frozen chunk row', async () => {
const want = JSON.parse(readFileSync(FIXTURE, 'utf8')) as Record<string, string>;
const got = await captureOffPath(engine);
let expanded = 0;
for (const key of ['search', 'search-remote', 'query', 'query-low']) {
const frozen = (JSON.parse(withCostWave(key, want[key])) as { result: SearchResult[] }).result;
const auto = JSON.parse(got[key]) as { result: SearchResult[] };
if (!auto.result.some(r => r.slug === CONVERSATION)) {
expect(`${key}: ${got[key]}`).toBe(`${key}: ${withCostWave(key, want[key])}`);
continue;
}
const others = auto.result.filter(r => r.slug !== CONVERSATION).map(({ delivered, ...r }) => {
expect(delivered, key).toMatchObject({ unit: 'chunk', reason: 'not_conversation' });
return r;
});
expect(others, key).toEqual(frozen.filter(r => r.slug !== CONVERSATION));
for (const r of auto.result.filter(r => r.slug === CONVERSATION)) {
expect(r.delivered, key).toMatchObject({ unit: 'page', reason: 'conversation_slug' });
expanded++;
}
}
expect(expanded).toBeGreaterThan(0);
});
test('a once-per-process notice landing on recall leaves the frozen recall bytes intact', async () => {
const want = JSON.parse(readFileSync(FIXTURE, 'utf8')) as Record<string, string>;
__resetProcessNoticeLedgerForTests();
const res = await withChunkConfig(engine, () => withEnv({ GBRAIN_BACKUP_CHECK: '0' }, () =>
dispatchToolCall(engine, 'recall', { query: 'ocelot' }, { remote: true, transport: 'stdio', sourceId: 'default' })));
expect(res._meta?.gbrain_notices).toBeDefined();
expect(offPathMcpBytes(res)).toBe(withCostWave('mcp-recall', want['mcp-recall']));
});
});