Files
stack/packages/cli/tests/proxy-launch-order.test.mjs
T
jason.woltje 85ecdfbde5 fix(row 332): proxy first check runs every key-independent refusal; testProxy test option (Vikunja #774)
F1: with system.proxy set, the first launcher check no longer returns
before the account, sandbox, capacity and limit checks. It skips only the
harness preflight and the session proxy key match, which need the key P2
admits in startLocked. A launch that can never start now refuses before
authorizeLaunch, the workspace and checkouts, and modelPreflight. The first
proxy pass accepts only credential "proxy" for a declared account, tries
the fallback model when the primary refuses, and lets mayLaunch
key-quota-no-model-key through so P2 can issue the first key. The final
check is unchanged and still runs before newRun and spawn.

F4: the proxy test option is renamed testProxy and createLauncher throws
test-proxy-refused when it is given with system.proxy.
launch-account.test.mjs is a second caller, so the option stays.

Tests: proxy-launch-order.test.mjs (6), launcher.test.mjs F4 throw test;
the managed launch fixture gains bwrap, admit and trace.ops.
2026-11-02 20:51:51 -06:00

125 lines
7.3 KiB
JavaScript

// Vikunja #774 F1 (row 332 review): with system.proxy set, a launch that can never start refuses in
// the first check, before authorizeLaunch, the workspace and checkouts, and the P2 modelPreflight
// that may issue a key. Only the harness preflight and the session proxy key match wait for the
// admitted key, in the final check. Real launcher, bus runtime and runner (managed P2 fixture).
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { join } from 'node:path';
import { sandboxProblem } from '../../seat/src/sandbox.mjs';
import { workspaceDir } from '../../seat/src/session.mjs';
import { managedLaunchFixture } from './model-managed-launch-fixture.mjs';
const skip = sandboxProblem() ?? false;
function editBusiness(f, update) {
const path = join(f.root, 'config', 'businesses', 'acme.json');
const document = JSON.parse(fs.readFileSync(path, 'utf8'));
update(document);
fs.writeFileSync(path, JSON.stringify(document) + '\n', { mode: 0o600 });
}
// A capability the bus issued to a claimed chief, as a launched chief holds.
function chief(f) {
const cap = f.bus.broker.bindLaunch({ business: 'acme', role: 'chief', run: 'chief-774', harness: f.f.doc.roles.chief.vars.harness });
f.bus.broker.request(cap, { verb: 'role.claim' });
return cap;
}
// The refusal came from the first check: nothing after it ran.
function refusedFirst(f, before, preflights, bindings = 0, instance = 'coder') {
assert.equal(f.trace.preflightAttempts, preflights, 'no modelPreflight for a launch the first check refuses');
assert.deepEqual(f.trace.ops.slice(before).filter(op => ['authorizeLaunch', 'credentialStatus', 'modelPreflight'].includes(op)), [],
'no authorizeLaunch, credentialStatus or modelPreflight after the first check');
assert.equal(fs.existsSync(join(workspaceDir(f.f.dataRoot, 'acme', instance), 'stack')), false, 'no checkout clone or fetch');
assert.equal(f.trace.bindings.length, bindings, 'no runner bound for the refused launch');
}
const refusal = (f, code) => f.launchLog().filter(e => e.event === 'refused' && e.code === code);
const claudeCoder = doc => {
doc.launch.models['claude-opus-5-5'].provider = 'anthropic';
doc.roles.coder.vars.model = 'claude-opus-5-5';
};
test('#774 F1 proxy capacity-full refuses in the first check: no modelPreflight, checkout or authorizeLaunch; the ordinary launch before it goes live', { skip, timeout: 60000 }, async t => {
const f = await managedLaunchFixture(t);
const cap = chief(f);
const ordinary = await f.launcher.fromSession(cap, 'reviewer');
assert.equal(ordinary.ok, true, JSON.stringify(ordinary));
assert.ok(f.sessions().find(s => s.run === ordinary.result.run)?.live, 'the ordinary proxy launch is live');
assert.equal(f.trace.preflightAttempts, 1, 'the ordinary proxy launch makes one P2 admission');
editBusiness(f, doc => { doc.launch.limits.total = 1; });
const before = f.trace.ops.length;
assert.deepEqual(await f.launcher.fromSession(cap, 'coder'), { ok: false, error: 'capacity-full' });
refusedFirst(f, before, 1, 1);
assert.deepEqual(refusal(f, 'capacity-full').map(e => e.limit), [{ scope: 'total', limit: 1, running: 1 }]);
f.valueFree();
});
test('#774 F1 proxy limit-reached refuses in the first check: no modelPreflight, checkout or authorizeLaunch', { skip, timeout: 60000 }, async t => {
const f = await managedLaunchFixture(t);
editBusiness(f, doc => {
doc.plugins = { ...doc.plugins, 'limit-check': ['token-limit'] };
doc.launch.limits.tokens = { instances: { coder: { limit: 0, since: '2026-10-18T00:00:00.000Z' } } };
});
const before = f.trace.ops.length;
assert.deepEqual(await f.launcher.fromSession(chief(f), 'coder'), { ok: false, error: 'limit-reached' });
refusedFirst(f, before, 0);
assert.equal(refusal(f, 'limit-reached')[0]?.limit?.check, 'token-limit');
assert.equal(f.runDirectories().length, 0);
f.valueFree();
});
test('#774 F1 proxy sandbox-unavailable refuses in the first check: no modelPreflight, checkout or authorizeLaunch', { skip, timeout: 60000 }, async t => {
const f = await managedLaunchFixture(t, { bwrap: '/nonexistent-774/bwrap' });
const before = f.trace.ops.length;
assert.deepEqual(await f.launcher.fromSession(chief(f), 'coder'), { ok: false, error: 'sandbox-unavailable' });
refusedFirst(f, before, 0);
assert.equal(f.runDirectories().length, 0);
f.valueFree();
});
test('#774 F1 proxy launch.account declared with an upstream credential refuses in the first check, before modelPreflight', { skip, timeout: 60000 }, async t => {
const f = await managedLaunchFixture(t, { harness: 'claude-code' });
editBusiness(f, doc => {
claudeCoder(doc);
doc.launch.account = { name: 'jason-max', credential: 'CLAUDE_CODE_OAUTH_TOKEN' };
});
const before = f.trace.ops.length;
assert.deepEqual(await f.launcher.fromSession(chief(f), 'coder'), { ok: false, error: 'account-credential-missing' });
refusedFirst(f, before, 0);
assert.equal(refusal(f, 'account-credential-missing')[0]?.why, 'account jason-max needs the declared proxy credential');
assert.equal(f.runDirectories().length, 0);
f.valueFree();
});
// The key match needs the admitted key, so it stays in the final check: after modelPreflight,
// before newRun and spawn. The fixture makes P2 admit a key equal to the account name.
test('#774 F1 proxy session key equal to launch.account.name still refuses account-is-credential in the final check', { skip, timeout: 60000 }, async t => {
const f = await managedLaunchFixture(t, { harness: 'claude-code', admit: value => ({ ...value, key: 'jason-max' }) });
editBusiness(f, doc => {
claudeCoder(doc);
doc.launch.account = { name: 'jason-max', credential: 'proxy' };
});
assert.deepEqual(await f.launcher.fromSession(chief(f), 'coder'), { ok: false, error: 'account-is-credential' });
assert.equal(f.trace.preflightAttempts, 1, 'the first check passed and P2 admitted a key');
assert.equal(f.trace.ops.filter(op => op === 'authorizeLaunch').length, 1);
assert.equal(refusal(f, 'account-is-credential')[0]?.why, 'launch.account.name is the value of session proxy key; it names the account, never its credential');
assert.equal(f.trace.bindings.length, 0);
assert.equal(f.sessions().length, 0);
assert.equal(f.runDirectories().length, 0, 'refused before newRun');
f.valueFree();
});
// Row 333: under a daily key quota the bus refuses mayLaunch key-quota-no-model-key for an agent
// with no model key. In the first check that is the one answer that waits for P2 to issue a key.
test('#774 F1 proxy launch of an agent with no key yet passes the first check under a daily key quota, and both checks ask mayLaunch', { skip, timeout: 60000 }, async t => {
const f = await managedLaunchFixture(t);
editBusiness(f, doc => {
doc.plugins = { ...doc.plugins, 'limit-check': ['token-limit'] };
doc.launch.limits.tokens = { keys: { '000000000774': { limit: 1000000, since: '2026-10-18T00:00:00.000Z', reset: '00:00', timeZone: 'UTC' } } };
});
assert.equal(f.bus.credentials.modelRecord('acme/chief'), null, 'chief holds no model key yet');
const answer = await f.launcher.launchPm();
assert.ok(f.sessions().find(s => s.run === answer.run)?.live, 'the launch goes live');
assert.equal(f.trace.successfulPreflights, 1);
assert.equal(f.trace.ops.filter(op => op === 'mayLaunch').length, 2, 'mayLaunch in the first and the final check');
f.valueFree();
});