Files
ai-kit/test/capability.test.js
T
CatoandClaude Opus 5 d7051a4c72 feat(capability): what a model can do, observed rather than declared (#48)
Replaces the line every app in this fleet writes and every app gets
wrong:

    const TOOL_CAPABLE_PROVIDERS = ['groq', 'openrouter'];

That list is wrong the moment a user brings a model nobody has heard of,
which is every day — and OrangeCat measured the consequence: a user on
their own frontier key is handed NO tools at all, so the person paying
most gets the least capable assistant. It is also wrong in the other
direction, because it cannot express that five of nine free models
probed here answer tools only in prose; a native-only client loses most
of its chain while believing it is fine.

The replacement is not a better list. It is three rules.

THE FIRST REAL CALL IS THE PROBE. Send the tools, read what comes back,
write down what it proved. Every model a user brings classifies itself
on its first message, at no extra cost and with no release from us.
Nothing here spends a separate request, which matters most when the key
belongs to the user.

A POSITIVE IS CHEAP, A NEGATIVE IS EXPENSIVE AND STICKY. One tool_calls
response proves capability outright. A 400 proves nothing unless the
vendor SAYS it is about tools — a context-length overflow, a content
filter or a bad parameter must be recorded as nothing at all, because
writing one down as "no tools" cripples a capable model until the record
expires and nothing in the product explains why. That asymmetry is what
`record: false` is for: the right response to an uninformative failure
is to learn nothing, not to guess. Most of the test file defends it.

"NEVER ASKED" IS ITS OWN ANSWER, READ TWO WAYS. planToolAttempt is
optimistic about it, because asking is the only way to learn.
claimableVerdict is pessimistic about it, because announcing a
capability a model has never demonstrated is a promise it may not keep,
and the user meets that as a broken feature rather than a missing one. A
single boolean cannot hold both directions at once.

Records expire, negatives sooner than positives: a model that gained
tool support and is still marked incapable is invisibly crippled, while
one that lost it says so loudly on the next call. Observations are keyed
by a HASH of the credential, never the credential, because capability
genuinely differs per key and observations must not leak across them.
A real call always overrules a registry claim, even an older one.

Storage stays with the app. This owns the shape and the rules, which is
the part everyone gets wrong; it does not own where rows live, which is
the part where apps legitimately differ.

21 new tests, 183 total green.


Claude-Session: https://claude.ai/code/session_014nayuDvAPd5BYkSfsVPHHA

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-11 13:55:38 -07:00

251 lines
10 KiB
JavaScript

/**
* Tests for ai-kit/capability.
*
* The property most worth defending: a NEGATIVE is sticky. If an unrelated 400
* gets written down as "this model has no tools", that model is crippled until
* the record expires and nothing in the product explains why. So most of this
* file is about what must NOT be recorded.
*
* The second property: "never asked" must survive as its own answer, in both
* directions — optimistic on the wire, pessimistic in what we claim. A single
* boolean cannot hold that, which is the whole reason this module exists.
*/
import { test } from "node:test";
import assert from "node:assert/strict";
import {
classifyToolAttempt,
saysToolsUnsupported,
planToolAttempt,
claimableVerdict,
currentVerdict,
isStale,
makeRecord,
scopeKey,
shouldReplace,
} from "@bitbaum/ai-kit/capability";
// ── what a real response proves ─────────────────────────────────────────────
test("one tool_calls response proves capability outright", () => {
const c = classifyToolAttempt({
status: 200,
parsed: { choices: [{ message: { tool_calls: [{ id: "1", function: { name: "x" } }] } }] },
});
assert.equal(c.verdict, "native");
assert.equal(c.record, true);
});
test("finish_reason alone is enough, since some vendors report it that way", () => {
const c = classifyToolAttempt({
status: 200,
parsed: { choices: [{ finish_reason: "tool_calls", message: {} }] },
});
assert.equal(c.verdict, "native");
assert.equal(c.record, true);
});
test("a tool call found in prose is the text protocol, and it is a real capability", () => {
const c = classifyToolAttempt({
status: 200,
parsed: { choices: [{ message: { content: '{"tool":"search"}' } }] },
textProtocolFound: true,
});
assert.equal(c.verdict, "text");
assert.equal(c.record, true);
});
test("answering without calling a tool proves NOTHING either way", () => {
// The model may be incapable, or it may have correctly decided no tool was
// needed — which is the right behaviour for most messages. Recording this
// would mark almost every model `none` within a few turns of ordinary chat.
const c = classifyToolAttempt({
status: 200,
parsed: { choices: [{ finish_reason: "stop", message: { content: "Hello." } }] },
});
assert.equal(c.verdict, "unobserved");
assert.equal(c.record, false);
});
// ── the expensive direction ─────────────────────────────────────────────────
test("a 400 that NAMES tools is a real negative", () => {
for (const body of [
'{"error":{"message":"Tool use is not supported for this model"}}',
'{"error":{"message":"model gpt-x does not support tools"}}',
'{"error":{"message":"Unsupported parameter: \'tools\'"}}',
'{"error":{"message":"function calling is not supported"}}',
]) {
const c = classifyToolAttempt({ status: 400, bodyText: body });
assert.equal(c.verdict, "none", body);
assert.equal(c.record, true, body);
}
});
test("a 400 about ANYTHING ELSE must never be recorded as incapable", () => {
// This is the test the whole module exists for. Each of these is a real 400
// that has nothing to do with tools, and recording any of them as `none`
// would permanently downgrade a perfectly capable model.
for (const body of [
'{"error":{"message":"maximum context length is 8192 tokens, however you requested 9000"}}',
'{"error":{"message":"Invalid value for temperature"}}',
'{"error":{"message":"content filtered"}}',
'{"error":{"message":"Your account is not active"}}',
'{"error":{"message":"unsupported value: response_format"}}',
'{"error":{"message":"bad request"}}',
]) {
const c = classifyToolAttempt({ status: 400, bodyText: body });
assert.equal(c.record, false, `must not learn from: ${body}`);
assert.equal(c.verdict, "unobserved", body);
}
});
test("rate limits, auth failures and outages teach nothing", () => {
for (const status of [401, 403, 429, 500, 502, 503]) {
const c = classifyToolAttempt({ status, bodyText: '{"error":{"message":"nope"}}' });
assert.equal(c.record, false, `status ${status}`);
assert.equal(c.verdict, "unobserved", `status ${status}`);
}
});
test("a transport failure teaches nothing", () => {
const c = classifyToolAttempt({ bodyText: "" });
assert.equal(c.record, false);
assert.equal(c.verdict, "unobserved");
});
test("the unsupported-tools matcher does not fire on ordinary refusals", () => {
assert.equal(saysToolsUnsupported("maximum context length exceeded"), false);
assert.equal(saysToolsUnsupported("unsupported parameter: response_format"), false);
assert.equal(saysToolsUnsupported("this model is not supported on your plan"), false);
assert.equal(saysToolsUnsupported(""), false);
assert.equal(saysToolsUnsupported("tool calling is not supported"), true);
});
// ── optimistic on the wire, pessimistic in the claim ────────────────────────
test("an unobserved model is ASKED, and the request doubles as the probe", () => {
const plan = planToolAttempt({ observed: "unobserved" });
assert.equal(plan.sendTools, true);
assert.equal(plan.expectTextProtocol, true, "either answer must be accepted");
assert.equal(plan.isLearning, true);
});
test("an unobserved model may NOT be claimed as capable", () => {
// The same state, read the other way. A boolean cannot do this.
assert.equal(claimableVerdict("unobserved"), "none");
assert.equal(claimableVerdict("native"), "native");
assert.equal(claimableVerdict("text"), "text");
});
test("a registry saying no lowers the effort but still listens for prose", () => {
const plan = planToolAttempt({ observed: "unobserved", declared: "none" });
assert.equal(plan.sendTools, false);
assert.equal(plan.expectTextProtocol, true, "a declared no is a prior, not a fact");
});
test("an observed text-protocol model stops being sent definitions it ignores", () => {
const plan = planToolAttempt({ observed: "text" });
assert.equal(plan.sendTools, false);
assert.equal(plan.expectTextProtocol, true);
assert.equal(plan.isLearning, false);
});
test("an observed incapable model is not asked again until the record expires", () => {
const plan = planToolAttempt({ observed: "none" });
assert.equal(plan.sendTools, false);
assert.equal(plan.expectTextProtocol, false);
});
// ── records expire, because models change under their own names ─────────────
test("a stale record decays to unobserved rather than standing forever", () => {
const old = makeRecord({
provider: "p",
model: "m",
scope: "s",
capability: "tools",
verdict: "native",
via: "live",
now: new Date("2026-01-01T00:00:00Z"),
});
assert.equal(currentVerdict(old, new Date("2026-09-01T00:00:00Z")), "unobserved");
assert.equal(currentVerdict(old, new Date("2026-01-02T00:00:00Z")), "native");
});
test("a negative expires sooner than a positive", () => {
// A model that gained tools and is still marked `none` is invisibly
// crippled; one that lost them says so loudly on the next call.
const at = new Date("2026-01-01T00:00:00Z");
const base = { provider: "p", model: "m", scope: "s", capability: "tools", via: "live" };
const negative = makeRecord({ ...base, verdict: "none", now: at });
const positive = makeRecord({ ...base, verdict: "native", now: at });
const twoWeeksLater = new Date("2026-01-15T00:00:00Z");
assert.equal(isStale(negative, twoWeeksLater), true);
assert.equal(isStale(positive, twoWeeksLater), false);
});
test("no record at all is unobserved, not incapable", () => {
assert.equal(currentVerdict(null), "unobserved");
assert.equal(currentVerdict(undefined), "unobserved");
});
// ── credentials, and not storing them ───────────────────────────────────────
test("the scope identifies a credential without being one", () => {
const a = scopeKey("sk-secret-value");
const b = scopeKey("sk-different-value");
assert.notEqual(a, b, "different keys must not share observations");
assert.equal(a, scopeKey("sk-secret-value"), "stable for the same key");
assert.ok(!a.includes("secret"), "the key must not survive into the handle");
assert.equal(scopeKey(undefined), "anonymous");
});
// ── which observation wins ──────────────────────────────────────────────────
test("a real call overrules a registry claim", () => {
const declared = makeRecord({
provider: "p",
model: "m",
scope: "s",
capability: "tools",
verdict: "native",
via: "declared",
now: new Date("2026-09-01T00:00:00Z"),
});
const live = makeRecord({
provider: "p",
model: "m",
scope: "s",
capability: "tools",
verdict: "none",
via: "live",
now: new Date("2026-08-01T00:00:00Z"), // older, and still wins
});
assert.equal(shouldReplace(declared, live), true);
assert.equal(shouldReplace(live, declared), false);
});
test("a newer observation replaces an older one of the same strength, in both directions", () => {
const base = { provider: "p", model: "m", scope: "s", capability: "tools", via: "live" };
const older = makeRecord({ ...base, verdict: "native", now: new Date("2026-08-01T00:00:00Z") });
const newer = makeRecord({ ...base, verdict: "none", now: new Date("2026-09-01T00:00:00Z") });
// A model really can lose a capability; refusing to believe that is how a
// chain keeps calling something that no longer works.
assert.equal(shouldReplace(older, newer), true);
assert.equal(shouldReplace(newer, older), false);
});
test("anything replaces nothing", () => {
const rec = makeRecord({
provider: "p",
model: "m",
scope: "s",
capability: "tools",
verdict: "native",
via: "declared",
});
assert.equal(shouldReplace(null, rec), true);
});