// Row 126 (#1620, REQ-LIMIT-2, REQ-LIMIT-4): the daily-quota check on its own. It is pure, so // these tests pass it limits, records and a time and read its answers; no loader, store or file. // America/Chicago in 2026: CDT (UTC-5) until 2026-11-01 07:00Z, CST (UTC-6) from then, and CDT // again from 2026-03-08 08:00Z. import test from "node:test"; import assert from "node:assert/strict"; import * as check from "../limit-check/daily-quota/index.mjs"; const SINCE = "2026-01-01T00:00:00.000Z"; const NOW = "2026-10-18T12:00:00.000Z"; const ZONE = "America/Chicago"; const PATH = ["tokens", "daily", "max"]; const subject = (over = {}) => ({ instance: "coder", provider: "anthropic", model: "opus", account: "max", ...over }); let seq = 0; const known = (at, over = {}, counts = [10, 20, 30, 40]) => { const [input, output, cacheWrite, cacheRead] = counts; return { at, run: "r1", turn: `t${++seq}`, source: "pi-session", instance: "coder", provider: "anthropic", model: "opus", account: "max", status: "known", input, output, cacheWrite, cacheRead, ...over }; }; const unknown = (at, over = {}) => ({ ...known(at, over), status: "unknown", input: null, output: null, cacheWrite: null, cacheRead: null }); const quota = (over = {}) => ({ limit: 1000, since: SINCE, reset: "05:00", timeZone: ZONE, ...over }); const args = (daily, records, over = {}) => ({ business: "demo", limits: { tokens: { daily } }, subject: subject(), records, now: NOW, ...over }); const count = (daily, records, now) => check.report(args(daily, records, { now }))[0].count; test("REQ-LIMIT-2 daily-quota 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-2 the count sums every kind of the account's known records since the later of the day start and since", () => { const records = [known("2026-10-18T11:00:00.000Z", {}, [1, 2, 3, 4]), known("2026-10-18T11:30:00.000Z", { account: "other" }), known("2026-10-18T11:40:00.000Z", { account: null }), known("2026-10-18T11:50:00.000Z", {}, [5, 0, 0, 5])]; assert.deepEqual(check.report(args({ max: quota() }, records)), [{ path: PATH, limit: 1000, since: SINCE, count: 20, kinds: { input: 6, output: 2, cacheWrite: 3, cacheRead: 9 }, unknownTurns: 0 }]); // A since after the day's start moves the count's start to it; report keeps the quota's own since. const late = "2026-10-18T11:45:00.000Z"; assert.deepEqual(check.report(args({ max: quota({ since: late }) }, records))[0], { path: PATH, limit: 1000, since: late, count: 10, kinds: { input: 5, output: 0, cacheWrite: 0, cacheRead: 5 }, unknownTurns: 0 }); // Another account's quota covers neither these records nor this subject's launches. const full = [known("2026-10-18T11:00:00.000Z"), known("2026-10-18T11:10:00.000Z")]; assert.deepEqual(check.mayLaunch(args({ other: quota({ limit: 150 }) }, full)), { allow: true }); assert.equal(check.report(args({ other: quota({ limit: 150 }) }, full))[0].count, 0); assert.deepEqual(check.mayLaunch(args({ max: quota({ limit: 150 }) }, full, { subject: subject({ account: "other" }) })), { allow: true }); }); test("REQ-LIMIT-2 a launch with no account (pi) is covered by no daily quota", () => { const records = [known("2026-10-18T11:00:00.000Z", { account: null })]; const a = args({ max: quota({ limit: 0 }) }, records, { subject: subject({ account: null }) }); assert.deepEqual(check.mayLaunch(a), { allow: true }); assert.deepEqual(check.mustStop(a), { stop: false }); assert.equal(check.report(a)[0].count, 0); }); test("REQ-LIMIT-4 an unknown turn adds nothing and is counted apart", () => { const u = unknown("2026-10-18T11:00:00.000Z", { run: "r2", turn: "u1" }); const records = [known("2026-10-18T11:00:00.000Z", {}, [1, 1, 1, 1]), u, { ...u, source: "other-source" }, unknown("2026-10-18T11:10:00.000Z", { run: "r2", turn: "u2" }), unknown("2026-10-18T09:00:00.000Z", { run: "r2", turn: "u0" })]; const [entry] = check.report(args({ max: quota({ limit: 5 }) }, records)); assert.equal(entry.count, 4); assert.equal(entry.unknownTurns, 2); assert.deepEqual(check.mayLaunch(args({ max: quota({ limit: 5 }) }, records)), { allow: true }); }); test("REQ-LIMIT-2 the day turns at the reset in its zone, not in UTC, and before it the day is yesterday's", () => { const daily = { max: quota() }; // 05:00 America/Chicago is 10:00Z in October const records = [known("2026-10-17T11:00:00.000Z", {}, [1, 0, 0, 0]), known("2026-10-18T09:59:59.999Z", {}, [10, 0, 0, 0]), known("2026-10-18T10:00:00.000Z", {}, [100, 0, 0, 0])]; assert.equal(count(daily, records, "2026-10-18T12:00:00.000Z"), 100); assert.equal(count(daily, records, "2026-10-18T10:00:00.000Z"), 100); // At 04:59 CDT the day that started at 05:00 yesterday still runs. assert.equal(count(daily, records.slice(0, 2), "2026-10-18T09:59:59.999Z"), 11); assert.equal(count(daily, records.slice(0, 2), "2026-10-18T09:00:00.000Z"), 11); }); test("REQ-LIMIT-2 the spring DST change makes the day after a 00:00 reset 23 hours long", () => { const daily = { max: quota({ reset: "00:00" }) }; // 2026-03-08 00:00 CST is 06:00Z; 2026-03-09 00:00 CDT is 05:00Z. const records = [known("2026-03-08T05:30:00.000Z", {}, [1, 0, 0, 0]), known("2026-03-08T06:00:00.000Z", {}, [10, 0, 0, 0]), known("2026-03-09T04:59:59.999Z", {}, [100, 0, 0, 0]), known("2026-03-09T05:00:00.000Z", {}, [1000, 0, 0, 0])]; assert.equal(count(daily, records.slice(0, 3), "2026-03-09T04:59:59.999Z"), 110); assert.equal(count(daily, records, "2026-03-09T05:00:00.000Z"), 1000); }); test("REQ-LIMIT-2 the fall DST change makes the day after a 00:00 reset 25 hours long", () => { const daily = { max: quota({ reset: "00:00" }) }; // 2026-11-01 00:00 CDT is 05:00Z; 2026-11-02 00:00 CST is 06:00Z. const records = [known("2026-11-01T04:59:59.999Z", {}, [1, 0, 0, 0]), known("2026-11-01T05:30:00.000Z", {}, [10, 0, 0, 0]), known("2026-11-02T05:30:00.000Z", {}, [100, 0, 0, 0]), known("2026-11-02T06:00:00.000Z", {}, [1000, 0, 0, 0])]; assert.equal(count(daily, records.slice(0, 3), "2026-11-02T05:59:59.999Z"), 110); assert.equal(count(daily, records, "2026-11-02T06:00:00.000Z"), 1000); }); test("REQ-LIMIT-2 a reset the clocks skip starts the day at the first instant after the gap", () => { const daily = { max: quota({ reset: "02:30" }) }; // 02:30 doesn't happen on 2026-03-08: 01:59:59 CST (07:59:59Z) is followed by 03:00 CDT (08:00Z). const records = [known("2026-03-07T09:00:00.000Z", {}, [1, 0, 0, 0]), known("2026-03-08T07:59:59.999Z", {}, [10, 0, 0, 0]), known("2026-03-08T08:00:00.000Z", {}, [100, 0, 0, 0])]; assert.equal(count(daily, records, "2026-03-08T08:15:00.000Z"), 100); assert.equal(count(daily, records, "2026-03-08T08:00:00.000Z"), 100); // Before the gap ends, the day that started at 02:30 CST on 03-07 (08:30Z) still runs. assert.equal(count(daily, records.slice(0, 2), "2026-03-08T07:59:59.999Z"), 11); }); test("REQ-LIMIT-2 a reset the clocks repeat starts one day, at the first one", () => { const daily = { max: quota({ reset: "01:30" }) }; // 01:30 happens twice on 2026-11-01: CDT at 06:30Z and CST at 07:30Z. const records = [known("2026-11-01T06:29:59.999Z", {}, [1, 0, 0, 0]), known("2026-11-01T06:45:00.000Z", {}, [10, 0, 0, 0]), known("2026-11-01T07:45:00.000Z", {}, [100, 0, 0, 0])]; assert.equal(count(daily, records, "2026-11-01T07:50:00.000Z"), 110); assert.equal(count(daily, records.slice(0, 2), "2026-11-01T06:50:00.000Z"), 10); // Before the first 01:30, the day that started at 01:30 CDT on 10-31 (06:30Z) still runs. assert.equal(count(daily, records.slice(0, 1), "2026-11-01T06:29:59.999Z"), 1); }); test("REQ-LIMIT-2 a launch is refused at the quota, a turn that reaches it stops, and the next day allows again", () => { const records = [known("2026-10-18T11:00:00.000Z"), known("2026-10-18T11:10:00.000Z")]; // 200 tokens const hit = { path: PATH, limit: 200, count: 200, unknownTurns: 0 }; assert.deepEqual(check.mayLaunch(args({ max: quota({ limit: 200 }) }, records)), { allow: false, hit }); assert.deepEqual(check.mayLaunch(args({ max: quota({ limit: 201 }) }, records)), { allow: true }); // mustStop answers on the records the turn's usage end added. assert.deepEqual(check.mustStop(args({ max: quota({ limit: 200 }) }, records.slice(0, 1))), { stop: false }); assert.deepEqual(check.mustStop(args({ max: quota({ limit: 200 }) }, records)), { stop: true, hit }); // The next reset, 05:00 CDT on 10-19 (10:00Z), starts a day with nothing counted. const next = { now: "2026-10-19T10:00:00.000Z" }; assert.deepEqual(check.mayLaunch(args({ max: quota({ limit: 200 }) }, records, { now: "2026-10-19T09:59:59.999Z" })), { allow: false, hit }); assert.deepEqual(check.mayLaunch(args({ max: quota({ limit: 200 }) }, records, next)), { allow: true }); assert.deepEqual(check.mustStop(args({ max: quota({ limit: 200 }) }, records, next)), { stop: false }); // A limit of 0 refuses with no records. assert.deepEqual(check.mayLaunch(args({ max: quota({ limit: 0 }) }, [])), { allow: false, hit: { path: PATH, limit: 0, count: 0, unknownTurns: 0 } }); }); test("REQ-LIMIT-2 counts past the safe integer range come back as digit strings", () => { const big = Number.MAX_SAFE_INTEGER; const records = [known("2026-10-18T11:00:00.000Z", {}, [big, big, 0, 0])]; const [entry] = check.report(args({ max: quota({ limit: 1 }) }, records)); assert.equal(entry.count, (2n * BigInt(big)).toString()); assert.equal(entry.kinds.input, big); assert.equal(check.mustStop(args({ max: quota({ limit: 1 }) }, records)).hit.count, (2n * BigInt(big)).toString()); }); test("REQ-LIMIT-4 bad leaves, records or times throw a code, and the check reads nothing but its arguments", () => { const ok = [known("2026-10-18T11:00:00.000Z")]; const leaves = [{ limit: -1 }, { limit: 1.5 }, { limit: "1" }, { reset: "5:00" }, { reset: "24:00" }, { reset: "05:60" }, { reset: undefined }, { timeZone: "Mars/Phobos" }, { timeZone: undefined }, { timeZone: 5 }]; for (const fn of ["mayLaunch", "mustStop", "report"]) { for (const over of leaves) { assert.throws(() => check[fn](args({ max: quota(over) }, ok)), { code: "bad-leaf" }, `${fn} ${JSON.stringify(over)}`); } assert.throws(() => check[fn](args({ max: null }, ok)), { code: "bad-leaf" }, fn); assert.throws(() => check[fn](args({ max: quota({ since: "never" }) }, ok)), { code: "bad-time" }, fn); assert.throws(() => check[fn](args({ max: quota() }, ok, { now: "soon" })), { code: "bad-time" }, fn); assert.throws(() => check[fn](args({ max: quota() }, null)), { code: "bad-records" }, fn); assert.throws(() => check[fn](args({ max: quota() }, [known("yesterday")])), { code: "bad-time" }, fn); assert.throws(() => check[fn](args({ max: quota() }, [known("2026-10-18T11:00:00.000Z", {}, [-1, 0, 0, 0])])), { code: "bad-record" }, fn); } const input = args({ max: quota() }, ok); const copy = structuredClone(input); check.mayLaunch(input); check.mustStop(input); check.report(input); assert.deepEqual(input, copy); }); test("REQ-LIMIT-2 no daily quotas: every launch is allowed and the report is empty", () => { const records = [known("2026-10-18T11:00:00.000Z")]; for (const l of [{}, { tokens: {} }, { tokens: { daily: {} } }, { tokens: { instances: { coder: { limit: 0, since: SINCE } } } }]) { const a = { business: "demo", limits: l, subject: subject(), records, now: NOW }; assert.deepEqual(check.mayLaunch(a), { allow: true }); assert.deepEqual(check.mustStop(a), { stop: false }); assert.deepEqual(check.report(a), []); } }); test('P5 daily quota for a model key ID matches stack key records and resets the next day', () => { const keyId = '012345abcdef'; const a = { business: 'demo', now: NOW, limits: { tokens: { keys: { [keyId]: quota({ limit: 100 }) } } }, subject: { instance: 'coder', modelKeyId: keyId, account: null }, lineages: [[keyId], ['abcdef012345']], records: [known('2026-10-18T11:00:00.000Z', { modelKeyId: keyId, account: null })] }; assert.equal(check.mayLaunch(a).allow, false); assert.equal(check.mustStop(a).stop, true); assert.equal(check.report(a)[0].count, 100); assert.deepEqual(check.report(a)[0].path, ['tokens', 'keys', keyId]); assert.equal(check.mayLaunch({ ...a, now: '2026-10-19T11:00:00.000Z' }).allow, true); assert.equal(check.mayLaunch({ ...a, subject: { modelKeyId: 'abcdef012345' } }).allow, true); assert.equal(check.mayLaunch({ ...a, limits: {} }).allow, true); }); // Row 333 R3 (#1831): a key quota follows the agent's whole lineage, current, retiring and earlier ids. test('R3 a key quota matches a subject anywhere in the leaf id\'s lineage and counts the whole lineage', () => { const [a, b, c, other] = ['aaaaaaaaaaaa', 'bbbbbbbbbbbb', 'cccccccccccc', '012345abcdef']; const at = '2026-10-18T11:00:00.000Z', rec = (modelKeyId, n) => known(at, { modelKeyId, account: null }, [n, 0, 0, 0]); const records = [rec(a, 40), rec(b, 30), rec(c, 30), rec(other, 500)]; const base = { business: 'demo', now: NOW, records, lineages: [[c, b, a], [other]] }; // The leaf names the agent's earliest id; the subject holds the current one. for (const leafId of [a, b, c]) { const x = { ...base, limits: { tokens: { keys: { [leafId]: quota({ limit: 100 }) } } }, subject: { instance: 'coder', modelKeyId: c } }; const hit = { path: ['tokens', 'keys', leafId], limit: 100, count: 100, unknownTurns: 0 }; assert.deepEqual(check.mayLaunch(x), { allow: false, hit }, leafId); assert.deepEqual(check.mustStop({ ...x, subject: { instance: 'coder', modelKeyId: a } }), { stop: true, hit }, leafId); assert.equal(check.report(x)[0].count, 100, leafId); assert.deepEqual(check.mayLaunch({ ...x, limits: { tokens: { keys: { [leafId]: quota({ limit: 101 }) } } } }), { allow: true }, leafId); // Another agent is not under it, however much it used. assert.deepEqual(check.mayLaunch({ ...x, subject: { instance: 'other', modelKeyId: other } }), { allow: true }, leafId); } }); test('R3 an orphaned key quota counts its own id only and limits no launch', () => { const [a, c, gone] = ['aaaaaaaaaaaa', 'cccccccccccc', 'dddddddddddd']; const at = '2026-10-18T11:00:00.000Z', rec = (modelKeyId, n) => known(at, { modelKeyId, account: null }, [n, 0, 0, 0]); const x = { business: 'demo', now: NOW, records: [rec(gone, 100), rec(c, 1), rec(a, 1)], lineages: [[c, a]], limits: { tokens: { keys: { [gone]: quota({ limit: 100 }) } } }, subject: { instance: 'coder', modelKeyId: c } }; assert.deepEqual(check.mayLaunch(x), { allow: true }); assert.deepEqual(check.mustStop(x), { stop: false }); assert.equal(check.report(x)[0].count, 100); // A subject still holding the orphaned id itself (no hired agent has it) is matched by that id alone. assert.equal(check.mayLaunch({ ...x, subject: { modelKeyId: gone } }).allow, false); }); test('R3 a key quota with missing or malformed lineages refuses bad-lineages; account quotas never need them', () => { const id = 'aaaaaaaaaaaa', at = '2026-10-18T11:00:00.000Z'; const x = { business: 'demo', now: NOW, records: [known(at, { modelKeyId: id, account: null })], limits: { tokens: { keys: { [id]: quota({ limit: 100 }) } } }, subject: { instance: 'coder', modelKeyId: id } }; const bad = [undefined, null, {}, 'aaaaaaaaaaaa', [id], [[]], [[id, 5]], [['AAAAAAAAAAAA']], [['aaaaaaaaaaa']], [[id], [id]], [[id, id]]]; for (const fn of ['mayLaunch', 'mustStop', 'report']) for (const lineages of bad) assert.throws(() => check[fn]({ ...x, lineages }), { code: 'bad-lineages' }, `${fn} ${JSON.stringify(lineages)}`); // No key quota: lineages are not read at all. assert.deepEqual(check.mayLaunch(args({ max: quota() }, [known(at)], { lineages: null })), { allow: true }); const input = { ...x, lineages: [[id]] }, copy = structuredClone(input); check.mayLaunch(input); check.mustStop(input); check.report(input); assert.deepEqual(input, copy); });