Files
gbrain/test/ambient-recall-cli.test.ts
Garry TanandClaude Fable 5 15ecc65b24 v0.45.7.0 feat(mcp,context): ambient recall — context_pack + delta frozen verbs + boundary runtime (#1) (#4028)
* feat(mcp,context): ambient recall — context_pack + delta frozen verbs + boundary runtime (#1)

Two new frozen MEMORY_VERBS (context_pack, delta) on the pull surface + a
Claude Code hook boundary runtime on the push surface, sharing one stateless
assembler core (assembleTurnContext mode: turn|pack|delta) and a keyset
session cursor (migration v126). World-only by default; include_private
gated fail-closed to trusted-local. protocol_version stays 1 (additive
5→7 verbs). Survived three adversarial review waves.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* v0.45.7.0 feat(mcp,context): ambient recall — context_pack + delta frozen verbs + boundary runtime (#1)

Two new frozen MEMORY_VERBS (context_pack, delta) grow the frozen set 5→7
without a wire bump — all seven stamp protocol_version: 1. context_pack
assembles a deterministic, zero-LLM, budget-packed bundle (entity cards +
open threads + hot facts) for a set of standing entities; delta returns
only what changed since a timestamp for cheap heartbeats, with a
per-session keyset cursor for at-least-once delivery. A boundary runtime
wires these into Claude Code lifecycle hooks (SessionStart warm pack,
PreCompact entity banking for post-compaction rehydration); Codex and any
MCP host pull the same verbs at their own boundaries. World-only by
default on all arms; include_private widens only for local trusted
callers. Migration v126 adds session_context_state (additive).

Includes the coverage close-out wave (~55 tests): real-serve compact→
session-start round trip over the live socket, --surface verbs stdio
session pinning exactly 7 tools fail-closed, HTTP-transport verb calls
with per-token cursor isolation, Postgres engine-parity for keyset
pagination + the session-cursor table, migration v126 shape + rewind
test, sub-second latency gates, CLI-level invocations, rendered-protocol
boundary assertions, and a live-Codex boundary-call check. The wave
caught and fixed three real bugs: the delta CLI wedging on first wake
(floating GC promise racing engine teardown), the compact hook probing
the PGLite socket on a Postgres config with a leftover database_path,
and the verbs-surface banner hardcoding a stale verb count.

Also the /document-release sweep: stale "five verbs" → seven across the
protocol doc, README, INSTALL, DEPLOY, the Claude Code MCP guide, and
the query skill; deferred scope filed in TODOS.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(release): bump openclaw.plugin.json to 0.45.7.0 — the sixth version location

The #4033 merge auto-resolved the OpenClaw plugin manifest at master's
version while the trio moved to 0.45.7.0, failing the manifest drift test
on CI shard 4. Register the file in CLAUDE.md's version-locations table
(five → six) so every future ship and merge re-bumps it with the trio.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-12 10:56:11 -07:00

93 lines
3.8 KiB
TypeScript

/**
* v0.45.7 — ambient recall CLI surface: the commands the bootstrap templates
* tell agents to run (`gbrain context-pack …`, `gbrain delta …`) actually
* execute end-to-end against the real cli.ts entrypoint. Subprocess tests
* against a shared temp PGLite home (schema init is paid once in beforeAll;
* later spawns reuse the persisted brain). Pins:
* - exit 0 + parseable JSON envelope with protocol_version 1 on both verbs
* - `since` echoed NORMALIZED to ISO (never the raw string)
* - unparseable --since → exit 1 + the verbError rendering on stderr
* (`Error [invalid_params]: …` + the `Fix:` suggestion line)
*/
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
import { spawnSync } from 'node:child_process';
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
let home: string;
function run(args: string[]): { stdout: string; stderr: string; status: number } {
const r = spawnSync('bun', ['run', 'src/cli.ts', ...args], {
cwd: process.cwd(),
encoding: 'utf8',
env: {
...process.env,
GBRAIN_HOME: home,
DATABASE_URL: '',
GBRAIN_DATABASE_URL: '',
GBRAIN_SKIP_STARTUP_HOOKS: '1', // no detached check-update child
},
timeout: 60_000,
});
return { stdout: r.stdout ?? '', stderr: r.stderr ?? '', status: r.status ?? -1 };
}
beforeAll(() => {
home = mkdtempSync(join(tmpdir(), 'gbrain-ambient-cli-'));
mkdirSync(join(home, '.gbrain'), { recursive: true });
writeFileSync(
join(home, '.gbrain', 'config.json'),
JSON.stringify({ engine: 'pglite', database_path: join(home, '.gbrain', 'brain.pglite') }),
);
// Warm the brain once: the first CLI touch runs initSchema/migrations; the
// tests below then measure command behavior, not schema-init behavior.
const warm = run(['delta', '--since', '1970-01-01T00:00:00Z', '--json']);
expect(warm.status).toBe(0);
}, 120_000);
afterAll(() => {
rmSync(home, { recursive: true, force: true });
});
describe('gbrain context-pack (CLI)', () => {
test('--entities + --budget-tokens --json: exit 0, protocol_version 1, budget fields', () => {
const { stdout, status } = run([
'context-pack', '--entities', 'alice-example', '--budget-tokens', '2000', '--json',
]);
expect(status).toBe(0);
const env = JSON.parse(stdout);
expect(env.protocol_version).toBe(1);
expect(env.entities).toEqual(['alice-example']);
// Empty brain → empty pack, but the budget contract still rides the envelope.
expect(env.budget_tokens).toBe(2000);
expect(env.budget_used).toBe(0);
expect(env.dropped_count).toBe(0);
expect(Array.isArray(env.cards)).toBe(true);
expect(Array.isArray(env.facts)).toBe(true);
}, 60_000);
});
describe('gbrain delta (CLI)', () => {
test('--since <ISO> --json: exit 0, protocol_version 1, ISO-normalized echo', () => {
const { stdout, status } = run(['delta', '--since', '1970-01-01T00:00:00Z', '--json']);
expect(status).toBe(0);
const env = JSON.parse(stdout);
expect(env.protocol_version).toBe(1);
expect(env.since).toBe('1970-01-01T00:00:00.000Z');
expect(env.pages).toEqual([]);
expect(env.has_more).toBe(false);
expect(env.next_cursor).toEqual({ since: '1970-01-01T00:00:00.000Z', slug: '' });
}, 60_000);
test('unparseable --since: exit 1, invalid_params rendering on stderr', () => {
const { stdout, stderr, status } = run(['delta', '--since', 'not-a-date', '--json']);
expect(status).toBe(1);
// verbError CLI rendering: `Error [code]: message` + the Fix line (stderr).
expect(stderr).toContain('Error [invalid_params]:');
expect(stderr).toContain('not a parseable timestamp');
expect(stderr).toContain('Fix:');
expect(stdout).toBe(''); // no envelope on the error path
}, 60_000);
});