Files
gbrain/test/brain-allowlist.test.ts
Garry Tan 96178d726e fix(subagent): v0.16.3 — bind Anthropic SDK correctly + enable tsc in CI (#318)
* fix(subagent): bind Anthropic SDK messages.create() correctly

The makeSubagentHandler was casting `new Anthropic()` directly to
MessagesClient, but MessagesClient.create() maps to sdk.messages.create(),
not sdk.create(). Every subagent job immediately died with:

  client.create is not a function

Fix: wrap the SDK instance so .create() delegates to .messages.create()
with proper `this` binding via .bind(sdk.messages).

Discovered on first production run of gbrain agent against Supabase.

Co-Authored-By: Wintermute <wintermute@openclaw.ai>

* chore(ci): add typescript typecheck to test pipeline + clean up baseline errors

Root cause infra gap that let the v0.16.0 subagent bug ship: CI ran
only `bun test`, which transpiles types without checking them. Type
errors only surfaced at runtime, in production.

Changes:
- Add `typescript` devDep and a `typecheck` npm script (`tsc --noEmit`).
- Chain `bun run typecheck` into `bun run test` so developers get the
  same pipeline locally that CI runs.
- Flip `.github/workflows/test.yml` to invoke `bun run test` (the npm
  script, including typecheck) instead of `bun test` (runner only).
- Clean up 100+ pre-existing type errors across 30+ files so the first
  run of `tsc --noEmit` is green. Root causes were:
  - `databaseUrl` → `database_url` rename drift in test fixtures (9 files)
  - `PageType` union missing `'meeting'` / `'note'` entries that are
    already used in both src and tests (link-extraction.ts comments
    acknowledged the gap)
  - `GBrainConfig.storage` field never declared despite being read in
    files.ts and operations.ts
  - `ErrorCode` union missing `'permission_denied'`
  - `OrchestratorOpts` shape changed; test callers not updated
  - Dead-code comparisons in migration orchestrators against narrowed
    status types
  - postgres.js `Row`-callback type drift on several `.map()` calls
  - Buffer-as-BodyInit assignment in supabase.ts (real but non-fatal
    runtime bug; Uint8Array slice works and is type-correct)
  - Various `as X` single-step casts that now need `as unknown as X`
    per TS's stricter structural-conversion rules
- Bump `beforeAll` hook timeout to 30s on four PGLite-heavy tests that
  were flaky under parallel test execution: wait-for-completion,
  extract-fs, e2e/search-quality, e2e/graph-quality. All pass in
  isolation; timeouts only happened when dozens of PGLite instances
  init'd simultaneously.

The new CI pipeline now fails on any type error across src/ or test/,
giving us the compile-time regression guard the subagent fix depends on.

* fix(subagent): bind Anthropic SDK messages.create() correctly

Shipped bug: v0.16.0 cast `new Anthropic()` to `MessagesClient`, but
`.create()` lives at `sdk.messages.create`, not on the top-level client.
Every subagent job in production died on first LLM call with
`client.create is not a function`. Discovered on the first `gbrain agent
run` against Supabase.

Fix: assign `sdk.messages` directly to the `MessagesClient` slot.
`sdk.messages` IS the object with a callable `.create()`; the original
bug was picking the wrong entry point on the SDK. No helper, no
wrapper, no `.bind()` — JS method-call semantics preserve `this` at
the call site because `subagent.ts:336` invokes `client.create(...)`
with `client === sdk.messages`.

The one-line assignment also typechecks cleanly against the existing
`MessagesClient` interface (SDK's first `create` overload:
`(MessageCreateParamsNonStreaming, Core.RequestOptions?) =>
APIPromise<Message>` is assignable structurally). This gives us
compile-time regression protection: anyone reverting to
`new Anthropic()` would fail tsc because `Anthropic` has no top-level
`.create`. (The companion chore commit puts `tsc --noEmit` in CI so
this guard is enforced.)

Also adds a `makeAnthropic?: () => Anthropic` dep-injection seam so
the factory default construction branch is testable without real API
calls. Regression test drives one handler turn through a fake SDK,
asserting `sdk.messages.create` is actually called. If someone later
reverts to `new Anthropic()`, both guards fire: tsc fails AND the test
fails.

Co-Authored-By: Wintermute <wintermute@garrytan.com>

* chore(tests): add bunfig.toml + 60s hook timeouts to stabilize PGLite-heavy suites

After turning on tsc in CI (previous commit), running the full `bun run test`
suite in one shot triggered flaky `beforeEach/afterEach hook timed out`
failures on 8+ test files. Every failure traced to PGLite WASM init
contention when many test files spin up fresh PGLite instances in parallel;
each one alone passes in isolation.

- `bunfig.toml` sets the global test hook timeout to 60s (default is 5s),
  covering every test file without per-file edits.
- Individual `beforeAll(fn, 60_000)` / `beforeEach(fn, 15_000)` calls on
  the 8 tests that flaked most stay in place as explicit safety nets so
  a future bunfig config change doesn't silently re-introduce the flake.

Result: 1997 pass, 0 fail on `bun run test` (117 tests added since the
prior baseline by picking up typecheck-gated passes). No infrastructure
flake tolerated in CI.

* chore: bump version and changelog (v0.16.3)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Wintermute <wintermute@garrytan.com>
Co-authored-by: Wintermute <wintermute@openclaw.ai>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 01:34:22 -07:00

178 lines
7.0 KiB
TypeScript

/**
* Subagent brain-tool registry tests. Covers:
* - every allow-list name exists in OPERATIONS (catches renames upstream)
* - Anthropic tool-name constraint enforced
* - put_page schema is namespace-wrapped per subagent
* - execute() invokes the op handler with viaSubagent=true + subagentId
* - filterAllowedTools narrows registry + rejects unknown names
* - denied ops (file_upload etc.) do NOT appear in the registry
*/
import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { operations, OperationError } from '../src/core/operations.ts';
import {
BRAIN_TOOL_ALLOWLIST,
buildBrainTools,
filterAllowedTools,
__testing,
} from '../src/core/minions/tools/brain-allowlist.ts';
import type { GBrainConfig } from '../src/core/config.ts';
import type { ToolCtx } from '../src/core/minions/types.ts';
let engine: PGLiteEngine;
const config: GBrainConfig = { engine: 'pglite' } as GBrainConfig;
beforeAll(async () => {
engine = new PGLiteEngine();
await engine.connect({ database_url: '' });
await engine.initSchema();
});
afterAll(async () => {
await engine.disconnect();
});
beforeEach(async () => {
await engine.executeRaw('DELETE FROM pages');
});
describe('BRAIN_TOOL_ALLOWLIST', () => {
test('every name exists in src/core/operations.ts OPERATIONS', () => {
const opNames = new Set(operations.map(o => o.name));
const missing = [...BRAIN_TOOL_ALLOWLIST].filter(n => !opNames.has(n));
expect(missing).toEqual([]);
});
test('contains the read-only 10 + put_page', () => {
expect(BRAIN_TOOL_ALLOWLIST.size).toBe(11);
expect(BRAIN_TOOL_ALLOWLIST.has('query')).toBe(true);
expect(BRAIN_TOOL_ALLOWLIST.has('search')).toBe(true);
expect(BRAIN_TOOL_ALLOWLIST.has('get_page')).toBe(true);
expect(BRAIN_TOOL_ALLOWLIST.has('list_pages')).toBe(true);
expect(BRAIN_TOOL_ALLOWLIST.has('put_page')).toBe(true);
});
test('does NOT contain destructive ops', () => {
expect(BRAIN_TOOL_ALLOWLIST.has('file_upload')).toBe(false);
expect(BRAIN_TOOL_ALLOWLIST.has('delete_page')).toBe(false);
expect(BRAIN_TOOL_ALLOWLIST.has('delete_file')).toBe(false);
expect(BRAIN_TOOL_ALLOWLIST.has('sync')).toBe(false);
});
});
describe('buildBrainTools', () => {
test('produces one ToolDef per allow-listed op that exists in operations.ts', () => {
const tools = buildBrainTools({ subagentId: 42, engine, config });
const opNames = new Set(operations.map(o => o.name));
const expected = [...BRAIN_TOOL_ALLOWLIST].filter(n => opNames.has(n)).length;
expect(tools.length).toBe(expected);
});
test('tool names are brain_<op> and match Anthropic constraint', () => {
const tools = buildBrainTools({ subagentId: 7, engine, config });
for (const t of tools) {
expect(t.name).toMatch(__testing.ANTHROPIC_NAME_RE);
expect(t.name.startsWith('brain_')).toBe(true);
}
});
test('tools are flagged idempotent in v0.15', () => {
const tools = buildBrainTools({ subagentId: 1, engine, config });
expect(tools.every(t => t.idempotent === true)).toBe(true);
});
test('tools carry the op description verbatim', () => {
const tools = buildBrainTools({ subagentId: 1, engine, config });
const getPage = tools.find(t => t.name === 'brain_get_page');
const op = operations.find(o => o.name === 'get_page');
expect(getPage?.description).toBe(op!.description);
});
test('put_page schema is namespace-wrapped per subagent', () => {
const tools42 = buildBrainTools({ subagentId: 42, engine, config });
const putPage42 = tools42.find(t => t.name === 'brain_put_page');
const slug42 = ((putPage42!.input_schema as any).properties as any).slug;
expect(slug42.pattern).toBe('^wiki/agents/42/.+');
expect(slug42.description).toContain('wiki/agents/42/');
const tools7 = buildBrainTools({ subagentId: 7, engine, config });
const putPage7 = tools7.find(t => t.name === 'brain_put_page');
const slug7 = ((putPage7!.input_schema as any).properties as any).slug;
expect(slug7.pattern).toBe('^wiki/agents/7/.+');
});
test('non-put_page tools do NOT get a pattern on slug', () => {
const tools = buildBrainTools({ subagentId: 42, engine, config });
const getPage = tools.find(t => t.name === 'brain_get_page');
const slug = ((getPage!.input_schema as any).properties as any).slug;
expect(slug).toBeDefined();
expect(slug.pattern).toBeUndefined();
});
test('execute() on put_page with valid namespace slug succeeds', async () => {
const tools = buildBrainTools({ subagentId: 42, engine, config });
const putPage = tools.find(t => t.name === 'brain_put_page');
const ctx: ToolCtx = { engine, jobId: 1, remote: true };
const res = await putPage!.execute(
{ slug: 'wiki/agents/42/notes', content: '---\ntitle: Notes\n---\nbody' },
ctx,
);
expect(res).toBeTruthy();
});
test('execute() on put_page with out-of-namespace slug throws permission_denied', async () => {
const tools = buildBrainTools({ subagentId: 42, engine, config });
const putPage = tools.find(t => t.name === 'brain_put_page');
const ctx: ToolCtx = { engine, jobId: 1, remote: true };
await expect(
putPage!.execute(
{ slug: 'wiki/analysis/stomp', content: '---\ntitle: x\n---\nb' },
ctx,
),
).rejects.toBeInstanceOf(OperationError);
});
});
describe('filterAllowedTools', () => {
test('passes prefixed names through', () => {
const tools = buildBrainTools({ subagentId: 1, engine, config });
const filtered = filterAllowedTools(tools, ['brain_get_page', 'brain_search']);
expect(filtered.map(t => t.name)).toEqual(['brain_get_page', 'brain_search']);
});
test('accepts un-prefixed names as a convenience', () => {
const tools = buildBrainTools({ subagentId: 1, engine, config });
const filtered = filterAllowedTools(tools, ['get_page', 'search']);
expect(filtered.map(t => t.name)).toEqual(['brain_get_page', 'brain_search']);
});
test('rejects unknown tool names (no silent ignore)', () => {
const tools = buildBrainTools({ subagentId: 1, engine, config });
expect(() => filterAllowedTools(tools, ['brain_typo_nope'])).toThrow(/unknown tool/);
});
test('deduplicates when both prefixed + unprefixed given', () => {
const tools = buildBrainTools({ subagentId: 1, engine, config });
const filtered = filterAllowedTools(tools, ['brain_get_page', 'get_page']);
expect(filtered.length).toBe(1);
});
test('empty array yields empty registry', () => {
const tools = buildBrainTools({ subagentId: 1, engine, config });
expect(filterAllowedTools(tools, [])).toEqual([]);
});
});
describe('sanitizeToolName', () => {
test('returns within 64 chars', () => {
// Synthetic: simulate an op name long enough to need slicing.
const long = 'a'.repeat(100);
expect(__testing.sanitizeToolName(long).length).toBeLessThanOrEqual(64);
});
test('replaces non-conforming chars with _', () => {
expect(__testing.sanitizeToolName('foo.bar')).toBe('brain_foo_bar');
});
});