Files
stack/packages/cli/tests/launch-limit-checks.test.mjs
T
jason.woltjeandClaude Opus 5.5 aa625565f6 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 b6827d19e: 25 steps, all rc 0 (15 packages, 10 script suites), 2295 node tests and 422 script selftests, 0 failed, 20:03:25Z to 20:08:08Z, network sage-r125_default removed.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-18 15:21:49 -05:00

78 lines
4.1 KiB
JavaScript

// 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*\(/);
});