Files
stack/packages/plugins/tests/token-limit.test.mjs
jason.woltje 2627321f64 fix(model-proxy): row 333 R1 repair overlay as reviewed (source-tests.patch a78bc385, #1831)
Commits the 18 product paths of the row 333 R1 repair exactly as the reviewed overlay. Packet hashes: source-tests.patch sha256 a78bc385801b64be270fb9d6e84672c443738a5fce8eb68f7d2a01359a57a770 (40623 bytes), INDEX.json sha256 3eb9b8d10445aa328be3b854385e4792dda2c5712b19d443d485d1b00a18d0f8, changed-source-tests.json sha256 42f770fc3e2384362efd1e416091355c064b6cca22dd40f3b5ef8f7b67e43a52. The committed bytes were checked by applying source-tests.patch to 503b91e56 in a scratch tree and comparing the 18 paths. The approvals are source only: R2 (APPROVE source-only, 2026-10-26 23:32:39Z) and Boyd's first review (APPROVE, source only, 2026-10-27T00:27:55Z), both on HEAD 503b91e56 plus this overlay. No test has run on these bytes yet.
2026-11-02 13:36:45 -06:00

161 lines
10 KiB
JavaScript

// Row 125 (#1619, REQ-LIMIT-3, REQ-LIMIT-4): the token-limit check on its own. It is pure, so
// these tests pass it limits and records and read its answers; no loader, store or file.
import test from "node:test";
import assert from "node:assert/strict";
import * as check from "../limit-check/token-limit/index.mjs";
const SINCE = "2026-10-18T00:00:00.000Z";
const NOW = "2026-10-18T12:00:00.000Z";
const subject = (over = {}) => ({ instance: "coder", provider: "anthropic", model: "opus", account: null, ...over });
let seq = 0;
const known = (over = {}, counts = [10, 20, 30, 40]) => {
const [input, output, cacheWrite, cacheRead] = counts;
return { at: "2026-10-18T06:00:00.000Z", run: "r1", turn: `t${++seq}`, source: "pi-session", instance: "coder",
provider: "anthropic", model: "opus", account: null, status: "known", input, output, cacheWrite, cacheRead, ...over };
};
const unknown = (over = {}) => ({ ...known(over), status: "unknown", input: null, output: null, cacheWrite: null, cacheRead: null });
const limits = tokens => ({ tokens });
const args = (tokens, records, over = {}) => ({ business: "demo", limits: limits(tokens), subject: subject(), records, now: NOW, ...over });
test("REQ-LIMIT-3 token-limit exports interface version 2 with mayLaunch, mustStop and report", () => {
assert.equal(check.interfaceVersion, 2);
for (const name of ["mayLaunch", "mustStop", "report"]) assert.equal(typeof check[name], "function");
});
test("REQ-LIMIT-3 each subject kind refuses at its limit and allows under it", () => {
const records = [known(), known()]; // 100 tokens each, 200 in all
const cases = [
{ tokens: { instances: { coder: { limit: 200, since: SINCE } } }, path: ["tokens", "instances", "coder"], subject: {} },
{ tokens: { providers: { anthropic: { limit: 200, since: SINCE } } }, path: ["tokens", "providers", "anthropic"], subject: {} },
{ tokens: { models: { anthropic: { opus: { limit: 200, since: SINCE } } } }, path: ["tokens", "models", "anthropic", "opus"], subject: {} },
{ tokens: { keys: { k1: { limit: 200, since: SINCE } } }, path: ["tokens", "keys", "k1"], subject: { account: "k1" },
records: records.map(r => ({ ...r, account: "k1" })) },
];
for (const c of cases) {
const recs = c.records ?? records;
const at = args(c.tokens, recs, { subject: subject(c.subject) });
const hit = { path: c.path, limit: 200, count: 200, unknownTurns: 0 };
assert.deepEqual(check.mayLaunch(at), { allow: false, hit }, c.path.join("."));
assert.deepEqual(check.mustStop(at), { stop: true, hit }, c.path.join("."));
const over = JSON.parse(JSON.stringify(c.tokens).replace('"limit":200', '"limit":201'));
assert.deepEqual(check.mayLaunch(args(over, recs, { subject: subject(c.subject) })), { allow: true }, c.path.join("."));
assert.deepEqual(check.mustStop(args(over, recs, { subject: subject(c.subject) })), { stop: false }, c.path.join("."));
}
});
test("REQ-LIMIT-3 a limit covers only its own subject's records and launches", () => {
const tokens = { instances: { coder: { limit: 150, since: SINCE } }, models: { anthropic: { opus: { limit: 150, since: SINCE } } } };
const records = [known({ instance: "tester", model: "sonnet" }), known({ instance: "tester", model: "sonnet" })];
// Another instance's and another model's records count toward neither limit.
assert.deepEqual(check.mayLaunch(args(tokens, records)), { allow: true });
// A launch of another subject isn't covered by a limit that is over.
const full = [known(), known()];
assert.deepEqual(check.mayLaunch(args(tokens, full, { subject: subject({ instance: "tester", model: "sonnet" }) })), { allow: true });
// A model limit is per provider: the same model name under another provider doesn't match it.
assert.deepEqual(check.mayLaunch(args({ models: { anthropic: { opus: { limit: 150, since: SINCE } } } },
full.map(r => ({ ...r, provider: "other" })), { subject: subject({ provider: "other" }) })), { allow: true });
// A provider limit counts only that provider's records and covers only its launches.
const byProvider = { providers: { anthropic: { limit: 150, since: SINCE } } };
const other = full.map(r => ({ ...r, provider: "other" }));
assert.deepEqual(check.mayLaunch(args(byProvider, other)), { allow: true });
assert.equal(check.report(args(byProvider, other))[0].count, 0);
assert.deepEqual(check.mayLaunch(args(byProvider, full, { subject: subject({ provider: "other" }) })), { allow: true });
assert.equal(check.mayLaunch(args(byProvider, full)).allow, false);
});
test("REQ-LIMIT-4 only known records count; an unknown turn adds nothing and is counted apart", () => {
const tokens = { instances: { coder: { limit: 100, since: SINCE } } };
const u = unknown({ run: "r2", turn: "u1" });
const records = [known({}, [1, 2, 3, 4]), u, { ...u, source: "other-source" }, unknown({ run: "r2", turn: "u2" })];
assert.deepEqual(check.mayLaunch(args(tokens, records)), { allow: true });
const [entry] = check.report(args(tokens, records));
assert.deepEqual(entry, { path: ["tokens", "instances", "coder"], limit: 100, since: SINCE, count: 10,
kinds: { input: 1, output: 2, cacheWrite: 3, cacheRead: 4 }, unknownTurns: 2 });
// With no usage source, every turn is the '@none' unknown record: the count is 0.
const none = [unknown({ source: "@none" }), unknown({ source: "@none" })];
const [empty] = check.report(args(tokens, none));
assert.equal(empty.count, 0);
assert.equal(empty.unknownTurns, 2);
assert.deepEqual(check.mayLaunch(args(tokens, none)), { allow: true });
});
test("REQ-LIMIT-3 a reset moves since: records before it no longer count", () => {
const tokens = { instances: { coder: { limit: 100, since: "2026-10-18T07:00:00.000Z" } } };
const records = [known({ at: "2026-10-18T06:59:59.999Z" }), known({ at: "2026-10-18T07:00:00.000Z" }, [1, 1, 1, 1])];
assert.deepEqual(check.mayLaunch(args(tokens, records)), { allow: true });
assert.equal(check.report(args(tokens, records))[0].count, 4);
const before = { instances: { coder: { limit: 100, since: SINCE } } };
assert.equal(check.mayLaunch(args(before, records)).allow, false);
});
test("REQ-LIMIT-3 limit 0 refuses with no records; the first limit hit in file order is named", () => {
assert.deepEqual(check.mayLaunch(args({ instances: { coder: { limit: 0, since: SINCE } } }, [])),
{ allow: false, hit: { path: ["tokens", "instances", "coder"], limit: 0, count: 0, unknownTurns: 0 } });
const tokens = { instances: { coder: { limit: 50, since: SINCE } }, providers: { anthropic: { limit: 50, since: SINCE } } };
assert.deepEqual(check.mustStop(args(tokens, [known()])).hit.path, ["tokens", "instances", "coder"]);
});
test("REQ-LIMIT-3 a key limit is not enforced while every record's account is null (decision 150 Q1)", () => {
const tokens = { keys: { k1: { limit: 1, since: SINCE } }, instances: { coder: { limit: 1000, since: SINCE } } };
const report = check.report(args(tokens, [known()]));
assert.deepEqual(report, [
{ path: ["tokens", "instances", "coder"], limit: 1000, since: SINCE, count: 100,
kinds: { input: 10, output: 20, cacheWrite: 30, cacheRead: 40 }, unknownTurns: 0 },
{ path: ["tokens", "keys", "k1"], limit: 1, since: SINCE, notEnforced: "no records name a key" },
]);
// A launch whose subject has no account isn't covered by a key limit.
assert.deepEqual(check.mayLaunch(args(tokens, [known()])), { allow: true });
// Once a record names an account, the key limit is counted.
const named = check.report(args(tokens, [known({ account: "k1" })]));
assert.equal(named[1].count, 100);
});
test("REQ-LIMIT-3 counts past the safe integer range come back as digit strings", () => {
const big = Number.MAX_SAFE_INTEGER;
const tokens = { instances: { coder: { limit: 1, since: SINCE } } };
const [entry] = check.report(args(tokens, [known({}, [big, big, 0, 0])]));
assert.equal(entry.count, (2n * BigInt(big)).toString());
assert.equal(entry.kinds.input, big);
assert.equal(check.mustStop(args(tokens, [known({}, [big, big, 0, 0])])).hit.count, (2n * BigInt(big)).toString());
});
test("REQ-LIMIT-4 bad records or times throw a code, and the check reads nothing but its arguments", () => {
const tokens = { instances: { coder: { limit: 1, since: SINCE } } };
for (const fn of ["mayLaunch", "mustStop", "report"]) {
assert.throws(() => check[fn](args(tokens, null)), { code: "bad-records" }, fn);
assert.throws(() => check[fn](args(tokens, [known({ at: "yesterday" })])), { code: "bad-time" }, fn);
assert.throws(() => check[fn](args(tokens, [known({}, [-1, 0, 0, 0])])), { code: "bad-record" }, fn);
assert.throws(() => check[fn](args({ instances: { coder: { limit: 1, since: "never" } } }, [known()])), { code: "bad-time" }, fn);
}
const input = args(tokens, [known()]);
const copy = structuredClone(input);
check.mayLaunch(input); check.mustStop(input); check.report(input);
assert.deepEqual(input, copy);
});
test("REQ-LIMIT-3 no token limits: every launch is allowed and the report is empty", () => {
for (const l of [{}, { tokens: {} }, { agents: 3 }]) {
const a = { business: "demo", limits: l, subject: subject(), records: [known()], now: NOW };
assert.deepEqual(check.mayLaunch(a), { allow: true });
assert.deepEqual(check.mustStop(a), { stop: false });
assert.deepEqual(check.report(a), []);
}
});
test("REQ-LIMIT-3 token-limit ignores tokens.daily, which the daily-quota check enforces (row 126)", () => {
const daily = { k1: { limit: 0, since: SINCE, reset: "05:00", timeZone: "America/Chicago" } };
const a = { business: "demo", limits: { tokens: { daily } }, subject: subject({ account: "k1" }), records: [known({ account: "k1" })], now: NOW };
assert.deepEqual(check.mayLaunch(a), { allow: true });
assert.deepEqual(check.mustStop(a), { stop: false });
assert.deepEqual(check.report(a), []);
});
test('P5 plain twelve-hex keys keep legacy account matching rather than cumulative model matching', () => {
const keyId = 'abcdef012345';
const args = { business: 'demo', limits: { tokens: { keys: { [keyId]: { limit: 5, since: '2026-10-18T00:00:00.000Z' } } } },
subject: { instance: 'coder', modelKeyId: '012345abcdef', account: keyId }, records: [{ at: '2026-10-18T01:00:00.000Z',
run: 'run', turn: 'turn', modelKeyId: '012345abcdef', account: keyId, status: 'known', input: 5, output: 0, cacheWrite: 0, cacheRead: 0 }] };
assert.equal(check.mayLaunch(args).allow, false);
assert.equal(check.report(args)[0].count, 5);
});