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