bus, cli, webui: token limits per instance, model, provider and key (row 125, #1619)

Adds token limits under launch.limits.tokens (instances, providers, keys,
models.P.M). Core (packages/bus/src/limits.mjs) reads the organization
file and the per-turn usage records row 124 writes, then asks every
switched-on limit-check plug-in; the first refusal wins. A limit no
switched-on check reports reads "not enforced". The limit-check kind is
interface 2 (mayLaunch, mustStop, report); a v1 check is refused at load.
The shipped token-limit check is pure and counts known end records only;
unknown turns add nothing and are shown as their own count.

The launcher refuses limit-reached through the host's mayLaunch op, or
limit-check-failed naming the check. The broker asks mustStop after a
usage end and stops the run at its next receive or state. Settings stamps
since on a new or changed limit and runs reset; launch.mjs and
business.mjs validate the shape. Readers: mosaic limits and mosaic limits
reset, the bus limits view, the Console /api/bus/limits route and the
Settings token table with Reset. Key limits stay "not enforced" until
row 126 names accounts on records.

Owner Dewey, design under lead decision 150.
Reviewer Filbert: r1 changes (comment 28965), r2 approve (28981), r3
approve (28996) on candidate manifest 8b71c405...fe56 after the rebase
onto row 164 (37 of 38 paths with identical +/- lines, TOOLS.md:621
bracket). Decision 123 go on the bus.js hunk from Mos (mos-0814). Sage
gate on @BASE@: @GATE@.

Live stays as pinned. Not part of the 8e7ea89a1 window.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
jason.woltjeandClaude Opus 5.5 committed 2026-10-18 15:03:20 -05:00
1 parent b6827d19ea
commit 5289ca1d2c
38 files changed
+1976 -83

No files matched your search

@@ -0,0 +1,77 @@
// Row 125 (#1619, REQ-LIMIT-3, REQ-LIMIT-4): the launcher asks the bus's limit checks before
// every launch, after the agent limits. A stub host answers the mayLaunch op; no sandbox, Docker
// or session process. Any answer that isn't an allow refuses (fail closed, lead decision 150).
import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {fileURLToPath} from 'node:url';
import {createLauncher} from '../src/launcher.mjs';
import {fixture,tmp} from './helpers.mjs';
import {launchLogFile} from '../../seat/src/session.mjs';
const HIT={path:['tokens','instances','coder'],limit:100,count:120,unknownTurns:1};
// answer is what the mayLaunch op returns, or an Error it throws.
function setup(t,answer){
const root=tmp(t),f=fixture(root,'acme',doc=>{doc.roles.coder.vars.model='claude-sonnet-5-5';return doc;}),ops=[];
const host={business:'acme',beforeClose:()=>{},op:async x=>{
ops.push(x);
if(x.op==='identity')return {business:'acme',role:'pm'};
if(x.op==='mayLaunch'){if(answer instanceof Error)throw answer;return answer;}
if(x.op==='authorizeLaunch')throw Object.assign(Error('fixture authorization stop'),{code:'fixture-policy'});
return {};
}};
const launcher=createLauncher({host,system:JSON.parse(readFileSync(f.config,'utf8')),env:f.env,bwrap:'/usr/bin/true',log:()=>{}});
const last=()=>JSON.parse(readFileSync(launchLogFile(f.dataRoot,'acme'),'utf8').trim().split('\n').at(-1));
return {launcher,ops,last};
}
test('REQ-LIMIT-4 the launcher asks mayLaunch with the instance, provider and model, and goes on to authorization on an allow',async t=>{
const {launcher,ops}=setup(t,{allow:true});
assert.equal((await launcher.fromSession('fixture-cap','coder')).error,'fixture-policy');
assert.deepEqual(ops.find(x=>x.op==='mayLaunch'),{op:'mayLaunch',business:'acme',instance:'coder',provider:'zai',model:'claude-sonnet-5-5'});
assert.ok(ops.findIndex(x=>x.op==='mayLaunch')<ops.findIndex(x=>x.op==='authorizeLaunch'));
});
test('REQ-LIMIT-3 REQ-LIMIT-4 a launch whose subject is at its limit is refused limit-reached and logged with the check, limit and count',async t=>{
const {launcher,ops,last}=setup(t,{allow:false,check:'token-limit',hit:HIT});
assert.deepEqual(await launcher.fromSession('fixture-cap','coder'),{ok:false,error:'limit-reached'});
const event=last();
assert.equal(event.event,'refused');
assert.equal(event.code,'limit-reached');
assert.deepEqual(event.limit,{check:'token-limit',...HIT});
assert.deepEqual(JSON.parse(event.why),HIT);
assert.ok(ops.find(x=>x.op==='refuse'&&x.code==='limit-reached'));
assert.equal(ops.find(x=>x.op==='authorizeLaunch'),undefined);
assert.deepEqual(launcher.registry.sessions,[]);
});
test('REQ-LIMIT-4 fail closed: a refused op, a check failure or a malformed answer refuses limit-check-failed, naming the check and the code',async t=>{
for(const [answer,why] of [
[Object.assign(Error('op refused'),{code:'quota-gone'}),'bus: quota-gone'],
[Error('no code'),'bus: launch-op-refused'],
[{allow:false,check:'token-limit',code:'not-loaded'},'token-limit: not-loaded'],
[{allow:false,check:'token-limit',code:'check-threw'},'token-limit: check-threw'],
[{allow:false,check:null,code:'organization-unreadable'},'bus: organization-unreadable'],
[{allow:'yes'},'bus: malformed-answer'],
[{},'bus: malformed-answer'],
[null,'bus: malformed-answer'],
[undefined,'bus: malformed-answer'],
]){
const {launcher,ops,last}=setup(t,answer);
assert.deepEqual(await launcher.fromSession('fixture-cap','coder'),{ok:false,error:'limit-check-failed'},why);
const event=last();
assert.equal(event.code,'limit-check-failed',why);
assert.equal(event.why,why);
assert.equal(event.limit,undefined,why);
assert.equal(ops.find(x=>x.op==='authorizeLaunch'),undefined,why);
}
});
test('REQ-LIMIT-4 the launcher reaches token-limit only through the bus: launcher.mjs imports no limit check',()=>{
const src=readFileSync(fileURLToPath(new URL('../src/launcher.mjs',import.meta.url)),'utf8');
assert.doesNotMatch(src,/token-limit/);
assert.doesNotMatch(src,/limit-check\//);
assert.doesNotMatch(src,/plugins\/src\/loader/);
assert.doesNotMatch(src,/\bimport\s*\(/);
});
+1 -1
View File
@@ -9,7 +9,7 @@ import {systemFor,writeJson} from '../../business/tests/helpers.mjs';
import {launchLogFile} from '../../seat/src/session.mjs';
test('each launch rereads limits and model selections; lowered limits keep sessions and log exact hit',async t=>{
const root=tmp(t),f=fixture(root,'acme',doc=>{doc.roles.coder.vars.model='claude-sonnet-5-5';return doc;}),file=join(root,'config/businesses/acme.json'),ops=[];
const host={business:'acme',beforeClose:()=>{},op:async x=>{ops.push(x);if(x.op==='identity')return {business:'acme',role:'pm'};if(x.op==='authorizeLaunch')throw Object.assign(Error('fixture authorization stop'),{code:'fixture-policy'});return {};}};
const host={business:'acme',beforeClose:()=>{},op:async x=>{ops.push(x);if(x.op==='identity')return {business:'acme',role:'pm'};if(x.op==='mayLaunch')return {allow:true};if(x.op==='authorizeLaunch')throw Object.assign(Error('fixture authorization stop'),{code:'fixture-policy'});return {};}};
const launcher=createLauncher({host,system:JSON.parse(readFileSync(f.config,'utf8')),env:f.env,bwrap:'/usr/bin/true',log:()=>{}});
const session={business:'acme',instance:'reviewer',run:'fixture-existing',harness:'pi',provider:'zai',model:'claude-sonnet-5-5'};launcher.registry.add(session);
const update=limits=>{f.doc.launch.limits=limits;writeJson(file,f.doc);};
+56
View File
@@ -0,0 +1,56 @@
// Row 125 (#1619, REQ-LIMIT-3, REQ-LIMIT-4): `mosaic limits` shows each token limit with its
// count, or why it isn't counted, and `mosaic limits reset PATH...` restarts a limit's count.
import test from 'node:test';
import assert from 'node:assert/strict';
import {main} from '../src/cli.mjs';
import {formatTokens} from '../src/limits.mjs';
import {io,tmp} from './helpers.mjs';
const SINCE='2026-10-18T07:00:00.000Z';
const TOKENS=[
{path:['tokens','instances','coder'],limit:100,since:SINCE,status:'counted',check:'token-limit',count:120,kinds:{input:10,output:20,cacheWrite:40,cacheRead:50},unknownTurns:2},
{path:['tokens','keys','k1'],limit:5,since:SINCE,status:'not-enforced',reason:'no records name a key'},
{path:['tokens','providers','zai'],limit:9,since:SINCE,status:'not-enforced',reason:null},
{path:['tokens','models','anthropic','claude-opus-5-5'],limit:7,since:SINCE,status:'check-failed',check:'token-limit',code:'check-threw'},
{path:['tokens','instances','pm'],limit:1,since:SINCE,status:'check-failed',check:null,code:'records-unreadable'},
];
test('REQ-LIMIT-3 REQ-LIMIT-4 formatTokens shows each limit counted, not enforced, or check failed, in reset path words',()=>{
assert.equal(formatTokens(TOKENS),'token limits:\n'+[
` tokens instances coder: limit 100 since ${SINCE}, count 120 (input 10, output 20, cacheWrite 40, cacheRead 50), unknown turns 2`,
` tokens keys k1: limit 5 since ${SINCE}, not enforced: no records name a key`,
` tokens providers zai: limit 9 since ${SINCE}, not enforced`,
` tokens models anthropic claude-opus-5-5: limit 7 since ${SINCE}, check failed: check-threw (token-limit)`,
` tokens instances pm: limit 1 since ${SINCE}, check failed: records-unreadable`,
].join('\n')+'\n');
for(const none of [[],undefined])assert.equal(formatTokens(none),'token limits: none\n');
});
test('REQ-LIMIT-3 the view prints the limits then the token lines; --json keeps tokens in the result',async t=>{
const root=tmp(t),deps={system:{dataRoot:root},transport:()=>()=>({revision:'b'.repeat(64),limits:{total:3},tokens:TOKENS})};
const x=io();await main(['limits','--business','acme'],x,deps);
const [json,lines]=x.out.text.split('token limits:\n');
assert.deepEqual(JSON.parse(json),{revision:'b'.repeat(64),limits:{total:3}});
assert.match(lines,/tokens keys k1: limit 5 since \S+, not enforced: no records name a key/);
const y=io();await main(['limits','--business','acme','--json'],y,deps);
assert.deepEqual(JSON.parse(y.out.text).tokens,TOKENS);
});
test('REQ-LIMIT-3 reset reads the revision, then sends limits.set with one path per limit, a word per level',async t=>{
const root=tmp(t),calls=[];
const deps={system:{dataRoot:root},transport:business=>(verb,args)=>{calls.push({business,verb,args});return {revision:'c'.repeat(64),limits:{},tokens:[]};}};
await main(['limits','reset','tokens','models','anthropic','claude-opus-5-5','--business','acme'],io(),deps);
assert.deepEqual(calls,[
{business:'acme',verb:'limits',args:{}},
{business:'acme',verb:'limits.set',args:{revision:'c'.repeat(64),reset:[['tokens','models','anthropic','claude-opus-5-5']]}},
]);
});
test('REQ-LIMIT-3 reset with no path, or with --file, is refused before the bus',async t=>{
const root=tmp(t),calls=[];
const deps={system:{dataRoot:root},transport:()=>verb=>{calls.push(verb);return {};}};
await assert.rejects(main(['limits','reset','--business','acme'],io(),deps),/mosaic limits/);
await assert.rejects(main(['limits','reset','tokens','--file','x.json','--business','acme'],io(),deps),/mosaic limits/);
await assert.rejects(main(['limits','view','extra','--business','acme'],io(),deps),/mosaic limits/);
assert.deepEqual(calls,[]);
});
+51 -2
View File
@@ -47,14 +47,14 @@ function gitIn(cwd, ...args) {
}
// The assistant is the thread instance; sonnet is its model's limit.
function prepare(t, { sonnet = 2, idle = 1 } = {}) {
function prepare(t, { sonnet = 2, idle = 1, edit = (doc) => doc } = {}) {
const root = tmp(t, "mosaic-thread-");
const f = fixture(root, "acme", (doc) => {
doc.roles.chief.vars = { harness: "pi", model: "claude-opus-5-5" };
doc.roles.assistant = { definition: "assistant", vars: { harness: "pi", model: "claude-sonnet-5-5" } };
doc.launch = launchDoc("chief", ["coder", "assistant"], { opus: 1, sonnet });
doc.launch.threadIdleSeconds = idle;
return doc;
return edit(doc);
});
for (const k of SESSION_CREDENTIALS.flat()) delete f.env[k];
mkdirSync(f.dataRoot, { mode: 0o700 });
@@ -84,6 +84,7 @@ async function start(t, { f, adapter }, { tickMs = 100, ops: replace } = {}) {
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
threadsState: (m) => runtime.threadsState(m.business),
threadIdle: (m) => runtime.threadIdle(m.record),
mayLaunch: (m) => runtime.mayLaunch(m),
landHeld: (m) => runtime.landHeld(m.record),
...replace?.(runtime),
};
@@ -368,3 +369,51 @@ test("REQ-THREAD-1 REQ-LAUNCH-1 the chief can't launch the assistant outside a t
assert.deepEqual(readSessions(f.dataRoot), []);
assert.deepEqual(launchLog(f).map((e) => [e.event, e.instance, e.code]), [["refused", "assistant", "thread-required"]]);
});
// Row 125 (#1619, REQ-LIMIT-3, REQ-LIMIT-4): the token-limit check switched on, with the
// assistant's limit at 0, so it is at its limit before any turn. The real check runs through the
// bus's plug-in loader.
const SINCE = "2026-10-18T00:00:00.000Z";
const atLimit = (doc) => {
doc.plugins = { "limit-check": ["token-limit"] };
doc.launch.limits.tokens = { instances: { assistant: { limit: 0, since: SINCE } } };
return doc;
};
const ASSISTANT_HIT = { path: ["tokens", "instances", "assistant"], limit: 0, count: 0, unknownTurns: 0 };
test("REQ-LIMIT-3 REQ-LIMIT-4 a thread start whose instance is at its token limit is refused, logged once with the limit and the count", { skip, timeout: 60000 }, async (t) => {
const prepared = prepare(t, { edit: atLimit });
const { f } = prepared;
const host = await start(t, prepared);
const project = host.call("project.create", { name: "stack work", repo: "stack" }).id;
const A = host.call("thread.create", { project, name: "alpha" }).id;
host.call("thread.send", { thread: A, body: "for A" });
const refused = await until(() => launchLog(f).find((e) => e.event === "refused" && e.thread === A));
assert.equal(refused.code, "limit-reached");
assert.equal(refused.instance, "assistant");
assert.deepEqual(refused.limit, { check: "token-limit", ...ASSISTANT_HIT });
assert.deepEqual(JSON.parse(refused.why), ASSISTANT_HIT);
await host.launcher.tick();
assert.deepEqual(threadRuns(f, A), []);
assert.equal(launchLog(f).filter((e) => e.event === "launch").length, 0);
assert.equal(launchLog(f).filter((e) => e.event === "refused" && e.thread === A).length, 1);
});
test("REQ-LIMIT-4 a thread run at its token limit stops at the end of its turn: the reply lands, and the end record names the limit", { skip, timeout: 60000 }, async (t) => {
const prepared = prepare(t, { edit: atLimit });
const { f } = prepared;
// The launch is allowed, as one started before the limit was lowered would have been; the
// stop after the turn comes from the real check through the bus.
const host = await start(t, prepared, { ops: () => ({ mayLaunch: () => ({ allow: true }) }) });
const project = host.call("project.create", { name: "stack work", repo: "stack" }).id;
const A = host.call("thread.create", { project, name: "alpha" }).id;
const { reply } = await ask(host.call, A, "one turn");
assert.equal(reply.class, "RESULT");
const run = launchLog(f).find((e) => e.event === "launch" && e.thread === A).run;
const end = await until(() => launchLog(f).find((e) => e.event === "end" && e.run === run));
assert.equal(end.limit, "limit: token-limit tokens.instances.assistant limit 0 count 0");
assert.equal(end.exitCode, 22);
assert.ok(!launchLog(f).find((e) => e.event === "thread-idle" && e.run === run), "a limit stop is not an idle stop");
assert.deepEqual(threadRuns(f, A), []);
assert.equal(host.runtime.threadsState("acme").find((x) => x.thread === A).holder, null);
});