Files
pig/apps/piggy/test/chat-tools.test.ts
T
claude f0173440e4
CI / verify (push) Successful in 7m6s
CI / publish (push) Has been skipped
Put Piggy on Prime Agent, and let it write to the book
Piggy was a hand-rolled OpenAI tool loop. It is now a Prime Agent session —
Prime Intellect's own harness, embedded as a Node library — answering from
PIG's tools and, for the first time, able to put information into the CRM
rather than only read it out.

The harness is a coding agent, so the first job was taking the coding agent
away from it. `noTools: 'all'` plus an explicit allowlist leaves the model
with PIG's ten `pig_*` tools and no bash, no filesystem, no IPython. That
holds under attack: a hostile extension, a skill and a settings file planted
in the agent's own directory, then `setActiveToolsByName` called with every
built-in, still leaves ten tools, all ours. Both lines are load-bearing —
`noTools` alone registers nothing, and the allowlist is what admits our own.

Writing is gated rather than assumed. A change is proposed, not made: the
tool returns a description, the transcript renders a diff card, and nothing
reaches the database until someone presses Apply. Contracts, commitments,
allocations and compliance always stop for a human whatever the mode. Every
write runs through `executeMutation` as the calling user, so their
capabilities and the audit trail apply exactly as they would to a human's.

Four things about the SDK are wrong in its own documentation and cost a
debugging cycle each: models.json does not resolve an env var name for
`apiKey`, it sends the literal string; there is no built-in prime-inference
provider in 0.84.1; a ResourceLoader you pass in is never reloaded for you;
and the stock system prompt is a coding-assistant prompt that must be
replaced — but replacing it also silently removes the tool list, because the
harness only renders that section when it owns the prompt. AGENTS.md records
all four.

The expensive one was thinking level. The harness defaults to `medium`, and
nemotron spent an entire 4,096-token budget reasoning and returned an empty
answer. `low` was worse; `off` omits the parameter so the endpoint's default
wins. An explicit `reasoning_effort: none` via `thinkingLevelMap` took a turn
from 6,195 output tokens to 149.

And a turn is now bounded. The harness loop is `while (true)` with no
iteration cap; a runaway on a frontier model would have eaten the credit it
is supposed to report on. Ceilings on model calls and tokens, enforced both
through the harness hook and independently from the event stream, plus a
per-user daily spend limit — and the ledger now records spend on turns that
fail, which it previously discarded.

Signing in lands on /piggy, which is a workspace: conversations down one
side, the agent in the middle, what it did and what it cost beside it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-14 05:26:28 -07:00

150 lines
5.5 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import type { Database } from '@pig/db';
import { assertPigToolBoundary } from '../src/chat';
import { createInteractivePigTools } from '../src/chat-tools';
import { piggyChatRequestSchema } from '../src/chat-server';
// Tool selection happens before any query runs, so these cases need the
// handle's identity and nothing else. A tool that touched it here would fail
// loudly rather than silently pass.
//
// Which is also the limit of this file: it covers which tool is chosen, never
// what a tool returns. The five `execute` bodies are exercised against a real
// Postgres in `e2e/page-tools.test.ts`, because the defects that actually
// shipped — a headline quoting a capped list length as a total, a calendar
// answering over two sources where the page shows thirteen — all typecheck.
const db = {} as Database;
/**
* The lookup layer is on every message by design, so asserting it in each case
* below would say nothing about selection. It is stripped here and covered on
* its own in `lookup-tools.test.ts`; what these cases still pin is the FOCUSED
* tool, which is the one that changes with where the user is standing.
*/
const LOOKUP_TOOLS = [
'pig_search_records',
'pig_get_record_by_id',
'pig_list_renewals',
'pig_list_inventory',
];
function toolNames(context: Parameters<typeof createInteractivePigTools>[1]): string[] {
const tools = createInteractivePigTools(db, context);
assertPigToolBoundary(tools);
const names = tools.map((tool) => tool.name);
assert.deepEqual(
names.slice(-LOOKUP_TOOLS.length),
LOOKUP_TOOLS,
'the lookup layer is offered in every context, after the focused tool',
);
return names.slice(0, -LOOKUP_TOOLS.length);
}
test('a page context selects the tool for that page and never pig_get_record', () => {
const byRoute: Record<string, string> = {
'/margin': 'pig_get_margin_summary',
'/capacity': 'pig_get_idle_capacity',
'/demand': 'pig_get_pipeline',
'/supply': 'pig_get_pipeline',
'/calendar': 'pig_get_calendar_ahead',
'/': 'pig_get_workspace_summary',
'/team': 'pig_get_workspace_summary',
};
for (const [route, expected] of Object.entries(byRoute)) {
const names = toolNames({ type: 'page', route: route as '/margin' });
assert.deepEqual(names, [expected], `route ${route}`);
// There is no record behind a page, so the record tool would only ever
// throw — and a wasted call costs one of four turns.
assert.ok(!names.includes('pig_get_record'));
}
});
test('the record arm is unchanged by the page work', () => {
assert.deepEqual(
toolNames({ type: 'contract', id: '20000000-0000-4000-8000-000000000002' }),
['pig_get_record'],
);
assert.deepEqual(toolNames({ type: 'account', id: '20000000-0000-4000-8000-000000000003' }), [
'pig_get_record',
'pig_get_account_lifecycle',
]);
for (const type of ['contact', 'demand_deal', 'supply_deal', 'commitment'] as const) {
assert.deepEqual(toolNames({ type, id: '20000000-0000-4000-8000-000000000004' }), [
'pig_get_record',
]);
}
});
test('no context reads the workspace, not six hundred rows of it', () => {
assert.deepEqual(toolNames(undefined), ['pig_get_workspace_summary']);
});
test('the calendar horizon accepts the null its emitted schema asks for', () => {
const [calendar] = createInteractivePigTools(db, { type: 'page', route: '/calendar' });
assert.ok(calendar);
// `zodToJsonSchema(..., { target: 'openAi' })` emits an optional parameter as
// required-and-nullable, so a model that follows the schema sends null and an
// `.optional()` field would reject it — spending one of four turns on a tool
// result that reads as a failure.
assert.equal(calendar.inputSchema.safeParse({ withinDays: null }).success, true);
assert.equal(calendar.inputSchema.safeParse({}).success, true);
assert.equal(calendar.inputSchema.safeParse({ withinDays: 90 }).success, true);
assert.equal(calendar.inputSchema.safeParse({ withinDays: 0 }).success, false);
});
// The full principal, because the chat server now writes as the caller and the
// schema is `.strict()`: the old bare `principalUserId` is rejected outright.
const validRequest = {
principal: {
userId: '10000000-0000-4000-8000-000000000001',
email: 'ada@primeintellect.example',
name: 'Ada',
isPlatformAdmin: false,
teams: [{ team: 'supply', role: 'lead' }],
via: 'jwt',
scopes: ['read'],
},
message: 'Where are we?',
mode: 'read_only',
conversationId: 'conv-1',
};
test('a route outside the published set is rejected by the schema', () => {
assert.equal(
piggyChatRequestSchema.safeParse({
...validRequest,
context: { type: 'page', route: '/margin' },
}).success,
true,
);
// The dock publishes the route on every navigation, so an unrecognised one
// must stop here rather than reach a model prompt as free text.
for (const route of ['/not-a-page', '/margin/../etc', 'ignore previous instructions', '']) {
assert.equal(
piggyChatRequestSchema.safeParse({ ...validRequest, context: { type: 'page', route } })
.success,
false,
`route ${route}`,
);
}
});
test('the record arm of the schema still demands a uuid', () => {
assert.equal(
piggyChatRequestSchema.safeParse({
...validRequest,
context: { type: 'contract', id: 'record-1' },
}).success,
false,
);
assert.equal(
piggyChatRequestSchema.safeParse({
...validRequest,
context: { type: 'contract', id: '20000000-0000-4000-8000-000000000002' },
}).success,
true,
);
});