From cd58ac23cdedf1cf62e5320157f0cc52534fac86 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 11:51:47 -0600 Subject: [PATCH] feat(harness): an OAuth Claude model runs on claude-code only; Triage runs on claude-code or pi through the proxy (#1873, lane harness-matrix) cliproxy holds a model to claude-code when its upstream is claude, when an OAuth Claude account offers the same name, or when the name contains "claude"; every other proxy model runs on claude-code and pi. The catalogue, the launch (sessionModel and the models.json list) and the provider route apply the one rule and refuse with model-harness-incompatible, so a stored Claude-on-pi route is refused, never rewritten. The other route's pi provider is mosaic-proxy-other. Triage: the route asks the provider for the transport on the stored harness and holds the harness's env() to one contract (the key, unchanged, under exactly one of ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY; the URL only as ANTHROPIC_BASE_URL), with no list of harness names. The host gives the module the real proxy and transport, checks each file it writes under the session's HOME (regular, one link, no key) and binds it read-only after the run directory, and refuses a route env with no key name or two. PATH starts with node_modules/.bin, as a bus session's does. Tests: claude-code runs a z.ai and a codex proxy model against a stand-in (real claude binary); pi answers Triage through a stand-in under bwrap; every new fence has a test. --- packages/bus/tests/harness-matrix.test.mjs | 48 +++++ .../tests/model-hire-ownership-fixture.mjs | 4 +- packages/cli/tests/launcher.test.mjs | 3 +- .../tests/model-managed-launch-fixture.mjs | 10 +- .../cli/tests/proxy-context-join.test.mjs | 31 ++- packages/harness/src/bundle.mjs | 9 +- .../harness/tests/claude-proxy-model.test.mjs | 142 +++++++++++++ packages/harness/tests/proxy-bundle.test.mjs | 57 +++--- packages/harness/tests/proxy-routing.test.mjs | 23 ++- .../plugins/model-provider/cliproxy/index.mjs | 18 +- .../tests/model-provider-catalogue.test.mjs | 65 +++++- packages/seat/src/sandbox.mjs | 17 +- packages/seat/tests/hosted-binds.test.mjs | 33 ++- packages/webui/src/triage-host.mjs | 96 +++++++-- packages/webui/src/triage-route.mjs | 85 +++++--- packages/webui/tests/triage-host.test.mjs | 41 ++-- packages/webui/tests/triage-wiring.test.mjs | 192 +++++++++++++++++- 17 files changed, 754 insertions(+), 120 deletions(-) create mode 100644 packages/bus/tests/harness-matrix.test.mjs create mode 100644 packages/harness/tests/claude-proxy-model.test.mjs diff --git a/packages/bus/tests/harness-matrix.test.mjs b/packages/bus/tests/harness-matrix.test.mjs new file mode 100644 index 000000000..65616b0fa --- /dev/null +++ b/packages/bus/tests/harness-matrix.test.mjs @@ -0,0 +1,48 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, mkdirSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { startBroker } from '../src/runtime.mjs'; +import { checkedTransport } from '../../harness/src/bundle.mjs'; +import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs'; + +// #1873 harness-matrix: an OAuth Claude model runs on claude-code only. A stored route that puts a +// Claude model on pi is refused at launch admission with model-harness-incompatible, even when a stale +// catalogue row still says pi. A z.ai and a codex model are admitted on pi. +const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: { authority: { withinRole: [], crossRole: [] } } }; + +async function admission(t, entry, catalogueRow) { + const root = mkdtempSync(join(tmpdir(), 'harness-matrix-')); t.after(() => rmSync(root, { recursive: true, force: true })); + const rolesDir = rolesCopy(root), doc = businessDoc(root, 'demo'); + doc.plugins = { ...doc.plugins, harness: ['pi'], 'model-provider': ['cliproxy'] }; + doc.launch.models['fixture-key'] = entry; + doc.roles.coder.vars = { ...doc.roles.coder.vars, harness: 'pi', model: 'fixture-key', thinking: 'low' }; + writeJson(join(root, 'businesses/demo.json'), doc); mkdirSync(join(root, 'data'), { mode: 0o700 }); + const modelCatalogue = { invalidate() {}, read: async () => ({ status: 'ready', notices: [], models: [{ module: 'cliproxy', available: true, ...catalogueRow }] }) }; + const bus = await startBroker({ dataRoot: join(root, 'data'), businesses: { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles } }, organization: { dir: root, rolesDir, system: systemFor(root) }, modelCatalogue }); + t.after(() => bus.close()); + const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true }); + const revision = bus.broker.request(human, { verb: 'limits' }).revision; + // The launcher's own transport check refuses first; admission is the second gate, so the request + // carries the transport the provider would give with no harness named. + const transport = checkedTransport(entry); + return bus.modelAdmission({ business: 'demo', instance: 'coder', modelKey: 'fixture-key', harness: 'pi', revision, entry, provider: entry.provider, model: entry.model, account: null, route: 'primary', transport }); +} + +test('#1873 a stored Claude route on pi is refused model-harness-incompatible at admission, even with a stale catalogue row that says pi', async t => { + const entry = { provider: 'anthropic', model: 'claude-opus-5-5', module: 'cliproxy', upstream: 'claude' }; + assert.deepEqual(await admission(t, entry, { upstream: 'claude', model: 'claude-opus-5-5', harnesses: ['claude-code', 'pi'] }), { allow: false, code: 'model-harness-incompatible' }); +}); + +test('#1873 a Claude-named model on an Anthropic-format route (upstream other) is refused on pi at admission', async t => { + const entry = { provider: 'other', model: 'anthropic/claude-sonnet-5-5', module: 'cliproxy', upstream: 'other' }; + assert.deepEqual(await admission(t, entry, { upstream: 'other', model: 'anthropic/claude-sonnet-5-5', harnesses: ['claude-code', 'pi'] }), { allow: false, code: 'model-harness-incompatible' }); +}); + +test('#1873 a z.ai and a codex model are admitted on pi', async t => { + for (const [provider, upstream, model] of [['zai', 'zai', 'glm-fixture'], ['codex', 'codex', 'gpt-fixture']]) { + const entry = { provider, model, module: 'cliproxy', upstream }; + assert.deepEqual(await admission(t, entry, { upstream, model, harnesses: ['claude-code', 'pi'] }), { allow: true }, provider); + } +}); diff --git a/packages/bus/tests/model-hire-ownership-fixture.mjs b/packages/bus/tests/model-hire-ownership-fixture.mjs index ce680812a..1e14c7303 100644 --- a/packages/bus/tests/model-hire-ownership-fixture.mjs +++ b/packages/bus/tests/model-hire-ownership-fixture.mjs @@ -27,7 +27,9 @@ export async function ownershipFixture(t,{launches=true,listed=true,keep=false,r // Available before any acquired resource/setup can fail; no later f/launcher TDZ. const launchLog=()=>{const file=launchLogFile(join(root,'data'),'acme');return fs.existsSync(file)?fs.readFileSync(file,'utf8').trim().split('\n').filter(Boolean).map(line=>JSON.parse(line)):[];}; t.after(async()=>{const failures=[];for(const g of gates)g.release();try{await waitFor(()=>active.size===0,'owned operations did not settle',5000);}catch{failures.push('owned-operation-close');}for(const close of [...closers].reverse()){try{await close();}catch{failures.push('owned-close');}}try{server?.closeAllConnections();if(server?.listening)await new Promise((resolve,reject)=>server.close(e=>e?reject(e):resolve()));}catch{failures.push('owned-server-close');}try{checkHistory({logs,proxyReceipts,trace});}catch{failures.push('value-free-receipts');}try{checkHistory({launch:launchLog()});}catch{failures.push('owned-launch-log-guard');}if(stderr)process.stderr.write=stderr;try{fs.rmSync(root,{recursive:true,force:true});}catch{failures.push('owned-root-close');}if(failures.length)throw new AggregateError(failures.map(code=>Error(code)),'owned managed fixture cleanup failed');}); - const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:['pi']};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness:'pi'};doc.roles.chief.vars.model='claude-opus-5-5';doc.roles.aide={definition:'aide',vars:{harness:'pi',model:'claude-opus-5-5',thinking:'high'}};if(listed)doc.launch.instances.push('aide');if(keep)doc.launch.keep=['aide'];if(retired)doc.retired=['alpha'];if(noHire)doc.launch.noHire=['aide'];if(!listed)delete doc.roles.aide;return doc;}); + const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:['pi']};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness:'pi'};doc.roles.chief.vars.model='claude-opus-5-5';doc.roles.aide={definition:'aide',vars:{harness:'pi',model:'claude-opus-5-5',thinking:'high'}};if(listed)doc.launch.instances.push('aide');if(keep)doc.launch.keep=['aide'];if(retired)doc.retired=['alpha'];if(noHire)doc.launch.noHire=['aide'];if(!listed)delete doc.roles.aide;doc.launch.models['claude-opus-5-5']={provider:'zai',model:'fixture-aide-model'};return doc;}); + // #1873 harness-matrix: an OAuth Claude model runs on claude-code only, so on pi the model key + // claude-opus-5-5 names a non-Claude upstream model. // The established scratch aide pattern only; repository roles/policy stay untouched. for(const name of ['aide','helper']){fs.writeFileSync(join(f.roles,name+'.md'),'# Scratch aide\n\n## Authority\n');writeJson(join(f.roles,name+'.json'),{roleVersion:2,name,title:'Aide',contract:name+'.md',tools:['read'],network:'api-only',authority:{withinRole:['message.send'],crossRole:[]},credentials:[]});} const chiefFile=join(f.roles,'chief-of-staff.json'); diff --git a/packages/cli/tests/launcher.test.mjs b/packages/cli/tests/launcher.test.mjs index 2dc8277fd..a5e41bc2c 100644 --- a/packages/cli/tests/launcher.test.mjs +++ b/packages/cli/tests/launcher.test.mjs @@ -151,7 +151,8 @@ const log = (f) => readFileSync(launchLogFile(f.dataRoot, "acme"), "utf8").trim( test('P3 proxy key reaches the spawned runner but never session.json or the bundle', { skip, timeout: 30000 }, async t => { const proxy = { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }; const prepared = prepare(t, doc => { - doc.launch.models['claude-opus-5-5'] = { provider: 'proxy', model: 'claude-fixture', route: 'anthropic' }; + // #1873 harness-matrix: a Claude route on pi is refused, so the key check rides a non-Claude proxy route. + doc.launch.models['claude-opus-5-5'] = { provider: 'proxy', model: 'fixture-route-model', route: 'glm' }; return doc; }); const { launcher, f, close } = await setup(t, doc => doc, prepared, { testProxy: proxy }); diff --git a/packages/cli/tests/model-managed-launch-fixture.mjs b/packages/cli/tests/model-managed-launch-fixture.mjs index 8c2fd4e98..4376a431e 100644 --- a/packages/cli/tests/model-managed-launch-fixture.mjs +++ b/packages/cli/tests/model-managed-launch-fixture.mjs @@ -21,12 +21,20 @@ const id = value => createHash('sha256').update(value).digest('hex').slice(0,12) // #774 R2: extra names more harnesses to switch on and load (a fallback route's); real names the // harnesses loaded as the real module, so their own preflight runs; hostEnv adds launcher env // such as MOSAIC_CLAUDE_BIN. +// #1873 harness-matrix: an OAuth Claude model runs on claude-code only, so the pi stream case reads +// a non-Claude model the proxy serves in Anthropic format (upstream other) from a stand-in catalogue. +// The chief's model key stays claude-opus-5-5; on pi its upstream model is not a Claude name. +export const PI_CHIEF_MODEL='fixture-chief-model'; +const STREAM_OTHER=Object.freeze({provider:'other',model:'fixture-stream-model',module:'cliproxy',upstream:'other'}); +const streamCatalogue=()=>({invalidate(){},read:async()=>({status:'ready',observedAt:'2026-11-19T00:00:00.000Z',notices:[], + models:[{key:'other-fixture-stream-model',module:'cliproxy',upstream:'other',provider:'other',model:'fixture-stream-model',label:'other / fixture-stream-model',available:true,reason:'unverified',harnesses:['claude-code','pi']}]})}); export async function managedLaunchFixture(t,{proxy=true,launches=true,refuseProbe=false,harness='pi',bwrap=null,admit=null,extra=[],real=[],hostEnv={},modelStream=null,modelCatalogue=null}={}){ const root=fs.mkdtempSync(join(tmpdir(),'ml653-')),closers=[],history=new Set(['fx653-manage']),proxyReceipts=[],logs=[]; let server,bus,launcher,stderr;const trace={preflightAttempts:0,successfulPreflights:0,enableBeforeSpawn:[],bindings:[],stalls:[],ops:[]}; const checkHistory=entries=>{const text=JSON.stringify(entries);for(const value of history)if(text.includes(value))throw Error('fixture credential leaked');}; t.after(async()=>{const failures=[];for(const close of [...closers].reverse()){try{await close();}catch{failures.push('owned-close');}}try{server?.closeAllConnections();if(server?.listening)await new Promise((resolve,reject)=>server.close(e=>e?reject(e):resolve()));}catch{failures.push('owned-server-close');}try{checkHistory({logs,proxyReceipts,trace});}catch{failures.push('value-free-receipts');}if(stderr)process.stderr.write=stderr;try{fs.rmSync(root,{recursive:true,force:true});}catch{failures.push('owned-root-close');}if(failures.length)throw new AggregateError(failures.map(code=>Error(code)),'owned managed fixture cleanup failed');}); - const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:[harness,...extra]};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness};doc.roles.chief.vars.model='claude-opus-5-5';if(modelStream){doc.launch.models['claude-opus-5-5']={provider:'anthropic',model:'claude-opus-5-5'};}return doc;}); + const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:[harness,...extra]};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness};doc.roles.chief.vars.model='claude-opus-5-5';if(harness==='pi')doc.launch.models['claude-opus-5-5']={provider:'zai',model:PI_CHIEF_MODEL};if(modelStream){doc.launch.models['claude-opus-5-5']=harness==='pi'?STREAM_OTHER:{provider:'anthropic',model:'claude-opus-5-5'};if(harness==='pi')doc.plugins={...doc.plugins,'model-provider':['cliproxy']};}return doc;}); + if(modelStream&&harness==='pi'&&!modelCatalogue)modelCatalogue=streamCatalogue(); const env={PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR,MOSAIC_CONFIG:f.config,MOSAIC_ROLES_DIR:f.roles,...hostEnv}; // The nonproxy positive fixture supplies its own fake pi login. Managed // proxy fixtures deliberately hold no upstream credential at all. diff --git a/packages/cli/tests/proxy-context-join.test.mjs b/packages/cli/tests/proxy-context-join.test.mjs index 4ed8d442c..e99e5a634 100644 --- a/packages/cli/tests/proxy-context-join.test.mjs +++ b/packages/cli/tests/proxy-context-join.test.mjs @@ -34,10 +34,11 @@ function privateArtifacts(f, session, keys) { f.valueFree(); } +// #1873 harness-matrix: an OAuth Claude model runs on claude-code only, so pi joins the z.ai and +// codex routes on a non-Claude model, and a Claude route on pi refuses below. +const ROUTE_MODEL = 'fixture-route-model'; for (const [provider, route, generated, api] of [ ['zai', null, 'mosaic-proxy-glm', 'openai-completions'], - ['anthropic', null, 'anthropic', 'anthropic-messages'], - ['proxy', 'anthropic', 'anthropic', 'anthropic-messages'], ['proxy', 'glm', 'mosaic-proxy-glm', 'openai-completions'], ['proxy', 'codex', 'mosaic-proxy-codex', 'openai-responses'], ]) { @@ -45,8 +46,7 @@ for (const [provider, route, generated, api] of [ const f = await managedLaunchFixture(t); assert.deepEqual(f.upstreamCredentialNames, [], 'managed proxy fixture has no inherited fake upstream credential'); editBusiness(f, doc => { - doc.launch.models['claude-opus-5-5'].provider = provider; - if (route) doc.launch.models['claude-opus-5-5'].route = route; + doc.launch.models['claude-opus-5-5'] = { provider, model: ROUTE_MODEL, ...(route ? { route } : {}) }; }); const answer = await f.launcher.launchPm(); const session = f.sessions().find(value => value.run === answer.run); @@ -69,7 +69,7 @@ for (const [provider, route, generated, api] of [ assert.equal(models.providers[generated].api, api); assert.equal(models.providers[generated].baseUrl, other.baseUrl + (api === 'anthropic-messages' ? '' : '/v1')); - assert.equal(models.providers[generated].models.some(model => model.id === 'claude-opus-5-5'), true); + assert.equal(models.providers[generated].models.some(model => model.id === ROUTE_MODEL), true); for (const value of Object.values(models.providers)) assert.equal(value.apiKey, '$ANTHROPIC_API_KEY'); for (const key of [own.key, other.key]) assert.equal(text.includes(key), false, 'models contain no key value'); assert.equal(fs.statSync(path).mode & 0o777, 0o600); @@ -79,6 +79,23 @@ for (const [provider, route, generated, api] of [ }); } +for (const [provider, route] of [['anthropic', null], ['proxy', 'anthropic']]) { + test(`#1873 a stored Claude route ${provider}${route ? ':' + route : ''} on pi refuses model-harness-incompatible with no key acquisition or session`, { skip, timeout: 30000 }, async t => { + const f = await managedLaunchFixture(t); + editBusiness(f, doc => { + doc.launch.models['claude-opus-5-5'].provider = provider; + if (route) doc.launch.models['claude-opus-5-5'].route = route; + }); + await assert.rejects(f.launcher.launchPm(), error => error.code === 'model-harness-incompatible'); + assert.equal(f.trace.preflightAttempts, 0, 'refused before P2 issues a key'); + assert.equal(f.trace.bindings.length, 0); + assert.equal(f.sessions().length, 0); + assert.equal(f.runDirectories().length, 0); + assert.equal(f.launchLog().some(e => e.event === 'refused' && e.code === 'model-harness-incompatible'), true); + f.valueFree(); + }); +} + test('P3 joined Claude harness receives the admitted P2 key only through transient auth env', { skip, timeout: 30000 }, async t => { const f = await managedLaunchFixture(t, { harness: 'claude-code' }); assert.deepEqual(f.upstreamCredentialNames, [], 'Claude proxy fixture holds no upstream credential'); @@ -117,7 +134,7 @@ test('P3 joined unsupported provider refuses with no key acquisition, fallback, test('row 332 R2 joined pi launch goes live when launch.models also holds an unroutable entry, which models.json leaves out', { skip, timeout: 30000 }, async t => { const f = await managedLaunchFixture(t); editBusiness(f, doc => { - doc.launch.models['claude-opus-5-5'].provider = 'anthropic'; + doc.launch.models['claude-opus-5-5'] = { provider: 'zai', model: ROUTE_MODEL }; doc.launch.models.router = { provider: 'openrouter', model: 'x' }; }); const answer = await f.launcher.launchPm(); @@ -126,7 +143,7 @@ test('row 332 R2 joined pi launch goes live when launch.models also holds an unr assert.equal(f.trace.successfulPreflights, 1); assert.equal(f.runDirectories().length, 1, 'one run directory, the live one'); const { providers } = JSON.parse(fs.readFileSync(join(session.runDir, 'home', '.pi', 'agent', 'models.json'), 'utf8')); - assert.equal(providers.anthropic.models.some(model => model.id === 'claude-opus-5-5'), true); + assert.equal(providers['mosaic-proxy-glm'].models.some(model => model.id === ROUTE_MODEL), true); assert.equal(Object.values(providers).some(value => value.models.some(model => model.id === 'x')), false, 'no openrouter entry'); privateArtifacts(f, session, [f.bus.credentials.modelRecord('acme/chief').key]); }); diff --git a/packages/harness/src/bundle.mjs b/packages/harness/src/bundle.mjs index bb77e2d96..48376b68a 100644 --- a/packages/harness/src/bundle.mjs +++ b/packages/harness/src/bundle.mjs @@ -100,7 +100,8 @@ export function sessionModel(resolved, models = null, which = "primary", { proxy if (models === null) return { harness, provider: null, model, thinking }; const entry = Object.hasOwn(models, model) ? models[model] : null; if (!entry) throw new BundleError(`instance ${resolved.instance}: unknown model table selection for ${ROUTE_VARS[which][1]} var`); - const transport = entry.provider === "proxy" || proxy ? checkedTransport(entry, null, resolver) : null; + // #1873 harness-matrix: the route's own harness, so a model the module holds off it refuses here. + const transport = entry.provider === "proxy" || proxy ? checkedTransport(entry, harness, resolver) : null; return { harness, provider: entry.provider, model: entry.model, modelKey: model, thinking, ...(transport ? { proxyRoute: transport.routeId, proxyTransport: transport } : {}) }; @@ -432,9 +433,11 @@ export function buildBundle({ dir, resolved, contract, arbiter = false, tracker ]; // The module's own files. A name the bundle already wrote refuses at write (flag wx). const transports = {}; + // #1873 harness-matrix: only models this session's harness may run, so a pi session's + // models.json never names a model held to claude-code (an OAuth Claude model). if (proxy) for (const [key, entry] of Object.entries(models ?? {})) { - try { transports[key] = checkedTransport(entry, null, resolver); } - catch (e) { if (e?.code !== 'proxy-provider-unsupported') throw e; } + try { transports[key] = checkedTransport(entry, model.harness, resolver); } + catch (e) { if (!['proxy-provider-unsupported', 'model-harness-incompatible'].includes(e?.code)) throw e; } } const own = harness.bundle({ dir, files: { ...files }, session: { ...session, ...model }, write, quote, models, proxy, transports }); Object.assign(files, own.files); diff --git a/packages/harness/tests/claude-proxy-model.test.mjs b/packages/harness/tests/claude-proxy-model.test.mjs new file mode 100644 index 000000000..3750442a3 --- /dev/null +++ b/packages/harness/tests/claude-proxy-model.test.mjs @@ -0,0 +1,142 @@ +import { fixtureCacheEnv } from '../../../scripts/testing/lane-fixtures.mjs'; +// #1873 harness-matrix: claude-code runs a proxy model that is not a Claude model. The host's claude +// CLI runs through adapters/claude with the bundle and env a z.ai or codex selection gives, against a +// local stand-in for the proxy's Anthropic-format route. The stand-in records the path, the model +// name claude sends and the hash of the key it carries. Scratch CLAUDE_CONFIG_DIR and HOME, a fixture +// key, nonessential traffic off: nothing reaches a real model or account. Whether the real proxy +// turns that request into a z.ai or codex call is proved only on the dev instance. + +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +import { createServer as createHttp } from 'node:http'; +import { spawn, spawnSync } from 'node:child_process'; +import { mkdirSync, readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { buildBundle, checkedTransport } from '../src/bundle.mjs'; +import { adapterEnv } from '../src/runner.mjs'; +import { fakeToolSocket, HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs'; +import * as cliproxy from '../../plugins/model-provider/cliproxy/index.mjs'; + +const ADAPTER = join(REPO, 'adapters', 'claude', 'adapter.sh'); +const which = spawnSync('sh', ['-c', 'command -v claude'], { encoding: 'utf8' }); +const CLAUDE = which.status === 0 ? which.stdout.trim() : null; +const skip = CLAUDE ? false : 'claude is not on PATH'; +const sha = (s) => createHash('sha256').update(s).digest('hex'); + +// The proxy's Anthropic-format route, as a stand-in: every /v1/messages answers with one text block. +async function standIn(t, text) { + const seen = []; + const server = createHttp((req, res) => { + let raw = ''; + req.on('data', (c) => (raw += c)); + req.on('end', () => { + const auth = String(req.headers.authorization ?? '').replace(/^Bearer /, ''); + const apiKey = String(req.headers['x-api-key'] ?? ''); + if (req.method !== 'POST' || !/^\/v1\/messages(\?|$)/.test(req.url)) { + seen.push({ path: req.url, method: req.method, authSha: auth ? sha(auth) : null, apiKeySha: apiKey ? sha(apiKey) : null }); + res.writeHead(req.url.includes('count_tokens') ? 200 : 404, { 'content-type': 'application/json' }); + return res.end(JSON.stringify(req.url.includes('count_tokens') ? { input_tokens: 10 } : { type: 'error', error: { type: 'not_found_error', message: 'stand-in' } })); + } + const body = JSON.parse(raw || '{}'); + seen.push({ path: req.url.split('?')[0], method: req.method, model: body.model, authSha: auth ? sha(auth) : null, apiKeySha: apiKey ? sha(apiKey) : null }); + const send = (event, data) => res.write(`event: ${event}\ndata: ${JSON.stringify({ type: event, ...data })}\n\n`); + if (!body.stream) { + res.writeHead(200, { 'content-type': 'application/json' }); + return res.end(JSON.stringify({ id: 'msg_si', type: 'message', role: 'assistant', model: body.model, content: [{ type: 'text', text }], stop_reason: 'end_turn', stop_sequence: null, usage: { input_tokens: 10, output_tokens: 5 } })); + } + res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' }); + send('message_start', { message: { id: 'msg_si', type: 'message', role: 'assistant', model: body.model, content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 10, output_tokens: 1, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 } } }); + send('content_block_start', { index: 0, content_block: { type: 'text', text: '' } }); + send('content_block_delta', { index: 0, delta: { type: 'text_delta', text } }); + send('content_block_stop', { index: 0 }); + send('message_delta', { delta: { stop_reason: 'end_turn', stop_sequence: null }, usage: { output_tokens: 5 } }); + send('message_stop', {}); + res.end(); + }); + }); + await new Promise((r) => server.listen(0, '127.0.0.1', r)); + t.after(() => server.close()); + return { url: `http://127.0.0.1:${server.address().port}`, seen }; +} + +function turn(env, request, cwd) { + return new Promise((resolve) => { + const child = spawn('/bin/sh', [ADAPTER], { cwd, env: { ...env, ...fixtureCacheEnv(env.CLAUDE_CONFIG_DIR), MOSAIC_REQUEST: request }, stdio: ['ignore', 'pipe', 'pipe'], detached: true }); + let stdout = '', stderr = ''; + child.stdout.on('data', (b) => (stdout += b)); + child.stderr.on('data', (b) => (stderr += b)); + const clock = setTimeout(() => process.kill(-child.pid, 'SIGKILL'), 120_000); + child.on('close', (code) => { clearTimeout(clock); resolve({ code, stdout, stderr }); }); + }); +} + +const SELECTIONS = { + zai: { provider: 'zai', model: 'glm-fixture', module: 'cliproxy', upstream: 'zai' }, + codex: { provider: 'codex', model: 'gpt-fixture', module: 'cliproxy', upstream: 'codex' }, +}; + +// The route's api and endpoint describe pi's wire. claude-code ignores them: its env is the proxy +// root and the key, and it always posts Anthropic Messages there, whatever the upstream. +test('#1873 the catalogue offers a z.ai and a codex model to claude-code and pi, and the route admits both harnesses', async () => { + const c = await cliproxy.catalogue({ + listAccounts: async () => ({ files: [{ provider: 'codex', name: 'codex-fixture.json', status: 'active' }] }), + listModels: async () => ({ models: [{ id: 'gpt-fixture' }] }), + listGroups: async () => [{ 'base-url': cliproxy.ZAI_URL, keys: [{}], models: [{ name: 'glm-fixture' }] }], + now: () => 1, + }); + assert.equal(c.status, 'ready'); + for (const [name, entry] of Object.entries(SELECTIONS)) { + const row = c.models.find((m) => m.upstream === entry.upstream && m.model === entry.model); + assert.deepEqual(row?.harnesses, ['claude-code', 'pi'], name); + for (const harness of ['claude-code', 'pi']) { + assert.deepEqual(cliproxy.preflight({ selection: entry, harness, catalogue: c }), { allow: true }, `${name} ${harness}`); + assert.equal(checkedTransport(entry, harness).piProvider, `mosaic-proxy-${entry.upstream === 'zai' ? 'glm' : 'codex'}`, `${name} ${harness}`); + } + } +}); + +for (const [name, entry] of Object.entries(SELECTIONS)) { + test(`#1873 claude-code sends the ${name} model name to the proxy's Anthropic-format route with the session key`, { skip }, async (t) => { + const dir = scratch(t, 'mosaic-claude-proxy-'); + const workspace = join(dir, 'ws'); mkdirSync(workspace); + const runDir = join(dir, 'run'); mkdirSync(runDir, { mode: 0o700 }); + const api = await standIn(t, `stand-in answer for ${entry.model}`); + const key = `fx1873-claude-${name}`; + const resolved = resolvedFor(t, 'coder', (d) => { + d.launch.models.proxied = entry; + d.roles.coder.vars = { harness: 'claude-code', model: 'proxied', thinking: 'low', 'limits.tools': ['read'] }; + }); + const sock = await fakeToolSocket(t, dir, (tool, args) => ({ tool, args })); + const session = { workspace, sessionDir: join(dir, 'session'), toolSocket: sock.path, turnMarker: join(dir, 'turn.done'), runDir }; + const bundle = buildBundle({ + dir: join(runDir, 'bundle'), resolved, models: { proxied: entry }, contract: '# Coder\n', + harness: HARNESS['claude-code'], skillsDir: REPO_SKILLS, proxy: { baseUrl: api.url, key }, + session: { ...session, run: 'r1873', launchedBy: 'pm' }, + }); + // The proxy's names come from the harness module's env, and nothing on disk holds the key. + assert.equal(bundle.env.ANTHROPIC_BASE_URL, api.url); + assert.equal(bundle.env.ANTHROPIC_AUTH_TOKEN, key); + assert.equal(Object.hasOwn(bundle.env, 'ANTHROPIC_API_KEY'), false); + for (const file of Object.values(bundle.files)) if (typeof file === 'string') { + let text = ''; try { text = readFileSync(file, 'utf8'); } catch {} + assert.equal(text.includes(key), false, file); + } + const s = { ...session, harness: 'claude-code', provider: entry.provider, model: entry.model, bundle: bundle.files, harnessEnv: bundle.env }; + const env = adapterEnv(s, { + PATH: `${dirname(CLAUDE)}:${dirname(process.execPath)}:/usr/bin:/bin`, + HOME: join(dir, 'home'), CLAUDE_CONFIG_DIR: join(dir, 'claude-config'), + CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1', DISABLE_TELEMETRY: '1', DISABLE_AUTOUPDATER: '1', DISABLE_ERROR_REPORTING: '1', + }); + const r = await turn(env, 'Message 1 from pm, class ASSIGNMENT:\n\nsay hello', workspace); + assert.equal(r.code, 0, r.stderr.replaceAll(key, '')); + assert.match(r.stdout, new RegExp(`stand-in answer for ${entry.model}`)); + const messages = api.seen.filter((x) => x.path === '/v1/messages'); + assert.ok(messages.length >= 1, JSON.stringify(api.seen)); + // The main turn names the selected model. Claude Code may add its own small background + // requests; every request carries the session key as a bearer token and none uses x-api-key. + assert.ok(messages.some((x) => x.model === entry.model), JSON.stringify(messages.map((x) => x.model))); + for (const x of api.seen) { assert.equal(x.authSha, sha(key), x.path); assert.equal(x.apiKeySha, null, x.path); } + assert.equal(r.stdout.includes(key) || r.stderr.includes(key), false); + }); +} diff --git a/packages/harness/tests/proxy-bundle.test.mjs b/packages/harness/tests/proxy-bundle.test.mjs index 9b9651e08..29fcbd791 100644 --- a/packages/harness/tests/proxy-bundle.test.mjs +++ b/packages/harness/tests/proxy-bundle.test.mjs @@ -10,6 +10,7 @@ const models = { a: { provider: 'proxy', model: 'claude-fixture', route: 'anthropic' }, z: { provider: 'proxy', model: 'glm-fixture', route: 'glm' }, g: { provider: 'proxy', model: 'gpt-fixture', route: 'codex' }, + o: { provider: 'other', model: 'fixture-other', module: 'cliproxy', upstream: 'other' }, }; function build(t, selection, proxy, declared) { const root = scratch(t, 'p332-'), runDir = join(root, 'run'); @@ -32,14 +33,22 @@ function assertSafe(value) { else if (value && typeof value === 'object') for (const item of Object.values(value)) assertSafe(item); } -test('P3 Anthropic route uses messages at proxy root and the declared model id', t => { - const b = build(t, 'a', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { a: models.a }); - const { anthropic } = JSON.parse(b.text).providers; - assert.equal(anthropic.api, 'anthropic-messages'); - assert.equal(anthropic.baseUrl, 'http://127.0.0.1:12345'); - assert.deepEqual(anthropic.models.map(m => m.id), ['claude-fixture']); - assert.equal(b.env.PI_PROVIDER, 'anthropic'); - assert.equal(b.env.PI_MODEL, 'claude-fixture'); +// #1873 harness-matrix: a Claude account model runs on claude-code only, so a pi session on it +// refuses before any file is written. +test('a proxy Claude route on pi refuses model-harness-incompatible before model config generation', t => { + for (const a of [models.a, { provider: 'anthropic', model: 'fixture-chat' }, { provider: 'anthropic', model: 'fixture-chat', module: 'cliproxy', upstream: 'claude' }]) + assert.throws(() => build(t, 'a', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { a }), { code: 'model-harness-incompatible' }, JSON.stringify(a)); +}); + +test('an other-upstream route uses messages at proxy root under its own pi provider, never pi\'s built-in anthropic', t => { + const b = build(t, 'o', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { o: models.o }); + const providers = JSON.parse(b.text).providers; + assert.deepEqual(Object.keys(providers), ['mosaic-proxy-other']); + assert.equal(providers['mosaic-proxy-other'].api, 'anthropic-messages'); + assert.equal(providers['mosaic-proxy-other'].baseUrl, 'http://127.0.0.1:12345'); + assert.deepEqual(providers['mosaic-proxy-other'].models.map(m => m.id), ['fixture-other']); + assert.equal(b.env.PI_PROVIDER, 'mosaic-proxy-other'); + assert.equal(b.env.PI_MODEL, 'fixture-other'); }); test('P3 omitted route refuses proxy-route-missing before model config generation', t => { @@ -82,9 +91,11 @@ test('P3 a declared Codex route generates only the single build-time responses p test('P3 models file is private, references the one session key, and stores no executable values or keys', t => { const key = 'fx332-agent-a'; - const b = build(t, 'a', { baseUrl: 'http://127.0.0.1:12345', key }, { a: models.a, z: models.z }); + const b = build(t, 'z', { baseUrl: 'http://127.0.0.1:12345', key }, { a: models.a, z: models.z, o: models.o }); const document = JSON.parse(b.text); - assert.deepEqual(Object.keys(document.providers).sort(), ['anthropic', 'mosaic-proxy-glm']); + // #1873: the Claude account entry is left out of a pi session's file. + assert.deepEqual(Object.keys(document.providers).sort(), ['mosaic-proxy-glm', 'mosaic-proxy-other']); + assert.ok(!b.text.includes('claude-fixture')); for (const provider of Object.values(document.providers)) assert.equal(provider.apiKey, '$ANTHROPIC_API_KEY'); assertSafe(document); assert.ok(!b.text.includes(key)); @@ -96,20 +107,20 @@ test('P3 models file is private, references the one session key, and stores no e }); test('P3 every launch regenerates proxy port, declared models and own key without caching', t => { - const first = build(t, 'a', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { a: models.a, z: models.z }); - const second = build(t, 'a', { baseUrl: 'http://127.0.0.1:23456', key: 'fx332-agent-b' }, { - a: { provider: 'proxy', model: 'changed-model-id', route: 'anthropic' }, + const first = build(t, 'z', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { g: models.g, z: models.z }); + const second = build(t, 'z', { baseUrl: 'http://127.0.0.1:23456', key: 'fx332-agent-b' }, { + g: { provider: 'proxy', model: 'changed-model-id', route: 'codex' }, z: { provider: 'proxy', model: 'changed-glm-id', route: 'glm' }, }); assert.equal(second.env.ANTHROPIC_API_KEY, 'fx332-agent-b'); assert.ok(!JSON.stringify(second).includes('fx332-agent-a')); const { providers } = JSON.parse(second.text); - assert.deepEqual(Object.keys(providers), ['anthropic', 'mosaic-proxy-glm']); - assert.equal(providers.anthropic.baseUrl, 'http://127.0.0.1:23456'); - assert.deepEqual(providers.anthropic.models.map(m => m.id), ['changed-model-id']); + assert.deepEqual(Object.keys(providers), ['mosaic-proxy-codex', 'mosaic-proxy-glm']); + assert.equal(providers['mosaic-proxy-codex'].baseUrl, 'http://127.0.0.1:23456/v1'); + assert.deepEqual(providers['mosaic-proxy-codex'].models.map(m => m.id), ['changed-model-id']); assert.equal(providers['mosaic-proxy-glm'].baseUrl, 'http://127.0.0.1:23456/v1'); assert.deepEqual(providers['mosaic-proxy-glm'].models.map(m => m.id), ['changed-glm-id']); - assert.equal(JSON.parse(first.text).providers.anthropic.baseUrl, 'http://127.0.0.1:12345'); + assert.equal(JSON.parse(first.text).providers['mosaic-proxy-glm'].baseUrl, 'http://127.0.0.1:12345/v1'); }); // Row 332 R2 (#1830, review F2): launch.models is open table data. An entry whose provider has no @@ -121,14 +132,14 @@ test('row 332 R2 an unroutable model table entry is left out of models.json and glm: { provider: 'zai', model: 'glm-fixture' }, router: { provider: 'openrouter', model: 'x' }, }; - const b = build(t, 'opus', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, declared); + const b = build(t, 'glm', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, declared); const { providers } = JSON.parse(b.text); - assert.deepEqual(Object.keys(providers).sort(), ['anthropic', 'mosaic-proxy-glm']); - assert.deepEqual(providers.anthropic.models.map(m => m.id), ['claude-fixture']); + // #1873: the Claude account entry is one a pi session can't run, so it is left out too. + assert.deepEqual(Object.keys(providers).sort(), ['mosaic-proxy-glm']); assert.deepEqual(providers['mosaic-proxy-glm'].models.map(m => m.id), ['glm-fixture']); assert.ok(!Object.values(providers).some(p => p.models.some(m => m.id === 'x')), 'the openrouter entry is not written'); - assert.equal(b.env.PI_MODEL, 'claude-fixture'); - assert.throws(() => build(t, 'opus', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { + assert.equal(b.env.PI_MODEL, 'glm-fixture'); + assert.throws(() => build(t, 'glm', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { ...declared, broken: { provider: 'proxy', model: 'claude-fixture' }, }), { code: 'proxy-route-missing' }); }); @@ -140,7 +151,7 @@ test('row 332 R2 a session proxy context without a key or loopback URL refuses i { baseUrl: 'http://localhost:12345', key: 'fx332-agent-a' }, { baseUrl: 'http://127.0.0.1:12345' }, ]) { - assert.throws(() => build(t, 'a', proxy, { a: models.a }), + assert.throws(() => build(t, 'z', proxy, { z: models.z }), { code: 'invalid-proxy-context', message: 'invalid session proxy context' }, JSON.stringify(proxy)); } }); diff --git a/packages/harness/tests/proxy-routing.test.mjs b/packages/harness/tests/proxy-routing.test.mjs index 3577b8aea..e74236b39 100644 --- a/packages/harness/tests/proxy-routing.test.mjs +++ b/packages/harness/tests/proxy-routing.test.mjs @@ -3,12 +3,12 @@ import test from 'node:test'; import assert from 'node:assert/strict'; import { sessionModel } from '../src/bundle.mjs'; const resolved = { instance: 'fixture', vars: { harness: 'pi', model: 'selected', thinking: 'low' } }; -for (const [provider, model, expected] of [ - ['anthropic', 'glm-looking-id', 'anthropic'], - ['zai', 'gpt-looking-id', 'glm'], +for (const [provider, model, expected, harness] of [ + ['anthropic', 'glm-looking-id', 'anthropic', 'claude-code'], + ['zai', 'gpt-looking-id', 'glm', 'pi'], ]) { test(`joined proxy context maps provider ${provider} by identity, not model name`, () => { - const selected = sessionModel(resolved, { selected: { provider, model } }, 'primary', { proxy: true }); + const selected = sessionModel({ ...resolved, vars: { ...resolved.vars, harness } }, { selected: { provider, model } }, 'primary', { proxy: true }); assert.equal(selected.proxyRoute, expected); assert.equal(selected.provider, provider, 'public/accounting provider identity is retained'); assert.equal(selected.model, model); @@ -31,7 +31,7 @@ test('provider proxy still requires an explicit route inside joined context', () assert.throws(() => sessionModel(resolved, { selected: { provider: 'proxy', model: 'fixture', route: 'custom' } }, 'primary', { proxy: true }), { code: 'proxy-route-unknown', }); - assert.equal(sessionModel(resolved, { selected: { provider: 'proxy', model: 'claude-looking-id', route: 'codex' } }, 'primary', { proxy: true }).proxyRoute, 'codex'); + assert.equal(sessionModel(resolved, { selected: { provider: 'proxy', model: 'fixture-codex-id', route: 'codex' } }, 'primary', { proxy: true }).proxyRoute, 'codex'); }); test('a module-backed selection carries the provider transport descriptor without changing accounting', () => { const selected = sessionModel(resolved, { selected: { provider: 'codex', model: 'fixture-codex', module: 'cliproxy', upstream: 'codex' } }, 'primary', { proxy: true }); @@ -52,3 +52,16 @@ test('transport accepts bounded compatibility flags declared by a provider modul const resolver=()=>({provider:'fixture',model:'fixture-id',routeId:'fixture',endpoint:'v1',api:'openai-completions',piProvider:'fixture',compat:{supportsStore:false,thinkingFormat:'openai'}}); assert.deepEqual(sessionModel(resolved,{selected:entry},'primary',{proxy:true,resolver}).proxyTransport.compat,{supportsStore:false,thinkingFormat:'openai'}); }); + +// #1873 harness-matrix: the launch checks the route's own harness, so a stored Claude route on pi is +// refused before any run, with or without a model-provider module switched on. +test('a stored proxy Claude route on pi refuses model-harness-incompatible at launch', () => { + for (const selected of [{ provider: 'anthropic', model: 'fixture-chat' }, { provider: 'proxy', route: 'anthropic', model: 'fixture-chat' }, + { provider: 'anthropic', model: 'fixture-chat', module: 'cliproxy', upstream: 'claude' }, { provider: 'proxy', route: 'codex', model: 'claude-looking-id' }, + { provider: 'other', model: 'claude-looking-id', module: 'cliproxy', upstream: 'other' }]) + assert.throws(() => sessionModel(resolved, { selected }, 'primary', { proxy: true }), { code: 'model-harness-incompatible' }, JSON.stringify(selected)); + const claudeCode = { ...resolved, vars: { ...resolved.vars, harness: 'claude-code' } }; + assert.equal(sessionModel(claudeCode, { selected: { provider: 'anthropic', model: 'fixture-chat' } }, 'primary', { proxy: true }).proxyRoute, 'anthropic'); + for (const selected of [{ provider: 'zai', model: 'fixture-zai' }, { provider: 'codex', model: 'fixture-codex', module: 'cliproxy', upstream: 'codex' }]) + assert.equal(sessionModel(claudeCode, { selected }, 'primary', { proxy: true }).provider, selected.provider); +}); diff --git a/packages/plugins/model-provider/cliproxy/index.mjs b/packages/plugins/model-provider/cliproxy/index.mjs index 05211fa78..d9420bd80 100644 --- a/packages/plugins/model-provider/cliproxy/index.mjs +++ b/packages/plugins/model-provider/cliproxy/index.mjs @@ -5,7 +5,13 @@ export const MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/; const MODEL = MODEL_ID; const MAX_MODELS = 10000; const family = url => url === ZAI_URL ? 'zai' : 'other'; -const harnesses = upstream => upstream === 'claude' ? ['claude-code', 'pi'] : ['pi']; +// #1873 (Jason 2026-11-19): a Claude model from an OAuth account runs on claude-code only, so it +// is never billed outside Claude; every other proxy model runs on claude-code and pi. The proxy lists +// OAuth accounts apart from API-key groups, but it routes a request by model name across every +// account, so a group model is held to claude-code when an OAuth Claude account offers its name +// (`oauth`) or the name says Claude (an account the read did not list could offer it). Fail closed. +const CLAUDE_NAME = /claude/i; +const harnesses = (upstream, model, oauth = false) => upstream === 'claude' || oauth || CLAUDE_NAME.test(String(model ?? '')) ? ['claude-code'] : ['claude-code', 'pi']; const slug = (upstream, model) => `${upstream}-${model}`.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/-+$/g, ''); export async function catalogue({ listAccounts, listModels, listGroups, now }) { @@ -22,7 +28,7 @@ export async function catalogue({ listAccounts, listModels, listGroups, now }) { if (keys.has(key)) return; keys.set(key, identity); models.push({ key, module: 'cliproxy', upstream, provider: upstream === 'claude' ? 'anthropic' : upstream, model, label: `${upstream} / ${model}`, available: true, - reason: upstream === 'other' || upstream === 'codex' ? 'unverified' : null, harnesses: harnesses(upstream) }); + reason: upstream === 'other' || upstream === 'codex' ? 'unverified' : null, harnesses: harnesses(upstream, model) }); if (models.length > MAX_MODELS) throw Error(); }; for (const row of accounts.files) { @@ -43,6 +49,8 @@ export async function catalogue({ listAccounts, listModels, listGroups, now }) { if (!Array.isArray(group.models) || group.models.length > MAX_MODELS) throw Error(); for (const model of group.models) add(upstream, model?.alias || model?.name); } + const oauth = new Set(models.filter(m => m.upstream === 'claude').map(m => m.model.toLowerCase())); + for (const m of models) m.harnesses = harnesses(m.upstream, m.model, oauth.has(m.model.toLowerCase())); return { status: 'ready', observedAt: now(), models, notices: [...new Set(notices)] }; } catch (error) { return { status: 'unavailable', observedAt: null, models: [], notices: [], ...(error.code === 'model-key-collision' ? { reason: error.code } : {}) }; @@ -61,7 +69,9 @@ const ROUTES = Object.freeze({ claude: { routeId: 'anthropic', endpoint: 'root', api: 'anthropic-messages', piProvider: 'anthropic', compat: null }, zai: { routeId: 'glm', endpoint: 'v1', api: 'openai-completions', piProvider: 'mosaic-proxy-glm', compat: { thinkingFormat: 'zai', zaiToolStream: true } }, codex: { routeId: 'codex', endpoint: 'v1', api: 'openai-responses', piProvider: 'mosaic-proxy-codex', compat: null }, - other: { routeId: 'anthropic', endpoint: 'root', api: 'anthropic-messages', piProvider: 'anthropic', compat: null }, + // #1873: its own pi provider, never pi's built-in "anthropic", whose Claude models an entry there + // would keep and send to the proxy. + other: { routeId: 'anthropic', endpoint: 'root', api: 'anthropic-messages', piProvider: 'mosaic-proxy-other', compat: null }, }); export function route({ selection, harness }) { let upstream = selection?.upstream; @@ -72,7 +82,7 @@ export function route({ selection, harness }) { } if (!upstream) upstream = ({ anthropic: 'claude', zai: 'zai' })[selection?.provider]; if (!Object.hasOwn(ROUTES, upstream ?? '')) return { code: 'proxy-provider-unsupported' }; - if (harness && !harnesses(upstream).includes(harness)) return { code: 'model-harness-incompatible' }; + if (harness && !harnesses(upstream, selection.model).includes(harness)) return { code: 'model-harness-incompatible' }; if (typeof selection.model !== 'string' || !MODEL.test(selection.model)) return { code: 'model-provider-unavailable' }; return { provider: selection.provider ?? upstream, model: selection.model, upstream, ...ROUTES[upstream] }; } diff --git a/packages/plugins/tests/model-provider-catalogue.test.mjs b/packages/plugins/tests/model-provider-catalogue.test.mjs index 82f389e4a..8c4fee040 100644 --- a/packages/plugins/tests/model-provider-catalogue.test.mjs +++ b/packages/plugins/tests/model-provider-catalogue.test.mjs @@ -17,15 +17,16 @@ test('catalogue attributes configured accounts, uses aliases, and enforces harne const result = await catalogue(deps()); assert.equal(result.status, 'ready'); const zai = result.models.find(x => x.upstream === 'zai'); - assert.deepEqual([zai.key, zai.model, zai.available, zai.harnesses], ['zai-fixture-alias', 'fixture-alias', true, ['pi']]); + assert.deepEqual([zai.key, zai.model, zai.available, zai.harnesses], ['zai-fixture-alias', 'fixture-alias', true, ['claude-code', 'pi']]); const claude = result.models.find(x => x.upstream === 'claude'); - assert.deepEqual(claude.harnesses, ['claude-code', 'pi']); - assert.deepEqual(preflight({ selection: zai, harness: 'claude-code', catalogue: result }), { allow: false, code: 'model-harness-incompatible' }); + assert.deepEqual(claude.harnesses, ['claude-code']); + assert.deepEqual(preflight({ selection: zai, harness: 'claude-code', catalogue: result }), { allow: true }); + assert.deepEqual(preflight({ selection: zai, harness: 'fixture-harness', catalogue: result }), { allow: false, code: 'model-harness-incompatible' }); assert.deepEqual(preflight({ selection: zai, harness: 'pi', catalogue: result }), { allow: true }); assert.ok(!JSON.stringify(result).includes('credential-1')); }); -test('an empty group offers no models; unknown groups offer pi only and are unverified', async () => { +test('an empty group offers no models; unknown groups offer claude-code and pi and are unverified', async () => { const d = deps(); d.listAccounts = async () => ({ files: [] }); d.listGroups = async () => [ @@ -34,7 +35,7 @@ test('an empty group offers no models; unknown groups offer pi only and are unve ]; const c = await catalogue(d); assert.equal(c.models.length, 1); - assert.deepEqual([c.models[0].upstream, c.models[0].harnesses, c.models[0].reason], ['other', ['pi'], 'unverified']); + assert.deepEqual([c.models[0].upstream, c.models[0].harnesses, c.models[0].reason], ['other', ['claude-code', 'pi'], 'unverified']); assert.ok(c.notices.includes('z.ai account has no models configured. Add them on Model proxy.')); }); @@ -50,7 +51,7 @@ test('module declares only supported transports and legacy routes without wideni assert.deepEqual(route({ selection: { upstream: 'codex', model: 'fixture-codex' }, harness: 'pi' }), { provider: 'codex', model: 'fixture-codex', upstream: 'codex', routeId: 'codex', endpoint: 'v1', api: 'openai-responses', piProvider: 'mosaic-proxy-codex', compat: null, }); - assert.equal(route({ selection: { upstream: 'zai', model: 'fixture-zai' }, harness: 'claude-code' }).code, 'model-harness-incompatible'); + assert.equal(route({ selection: { upstream: 'zai', model: 'fixture-zai' }, harness: 'fixture-harness' }).code, 'model-harness-incompatible'); assert.equal(route({ selection: { provider: 'proxy', route: 'unknown', model: 'fixture' }, harness: 'pi' }).code, 'proxy-route-unknown'); assert.equal(route({ selection: { provider: 'proxy', model: 'fixture' }, harness: 'pi' }).code, 'proxy-route-missing'); }); @@ -68,3 +69,55 @@ test('a second fixture module resolves independently and ambiguous legacy mappin assert.equal(selectModelProvider([first,second],{...entry,module:'first-fixture'}),null); assert.equal(selectModelProvider([first,second,{...second,name:'third-fixture'}],entry),null); }); + +// #1873 harness-matrix (Jason 2026-11-19): a Claude model from an OAuth account runs on +// claude-code only; every other proxy model runs on claude-code and pi. +test('an OAuth Claude model is offered on claude-code only and refused on pi at catalogue, preflight and route', async () => { + const { route } = await import('../model-provider/cliproxy/index.mjs'); + const c = await catalogue(deps()); + const claude = c.models.find(x => x.upstream === 'claude'); + assert.deepEqual(claude.harnesses, ['claude-code']); + assert.deepEqual(preflight({ selection: claude, harness: 'pi', catalogue: c }), { allow: false, code: 'model-harness-incompatible' }); + assert.deepEqual(preflight({ selection: claude, harness: 'claude-code', catalogue: c }), { allow: true }); + for (const selection of [{ upstream: 'claude', model: 'fixture-chat' }, { provider: 'anthropic', model: 'fixture-chat' }, { provider: 'proxy', route: 'anthropic', model: 'fixture-chat' }]) { + assert.equal(route({ selection, harness: 'pi' }).code, 'model-harness-incompatible', JSON.stringify(selection)); + assert.equal(route({ selection, harness: 'claude-code' }).upstream, 'claude'); + } +}); + +test('a z.ai and a codex model run on claude-code and pi', async () => { + const { route } = await import('../model-provider/cliproxy/index.mjs'); + const d = deps(); + d.listAccounts = async () => ({ files: [{ name: 'credential-3', provider: 'codex', status: 'active' }] }); + d.listModels = async () => ({ models: [{ id: 'fixture-codex' }] }); + const c = await catalogue(d); + for (const upstream of ['zai', 'codex']) { + const m = c.models.find(x => x.upstream === upstream); + assert.deepEqual(m.harnesses, ['claude-code', 'pi'], upstream); + for (const harness of ['claude-code', 'pi']) { + assert.deepEqual(preflight({ selection: m, harness, catalogue: c }), { allow: true }, `${upstream} ${harness}`); + assert.equal(route({ selection: { upstream, model: m.model }, harness }).upstream, upstream, `${upstream} ${harness}`); + } + } + assert.equal(route({ selection: { provider: 'zai', model: 'fixture-zai' }, harness: 'claude-code' }).routeId, 'glm'); + assert.equal(route({ selection: { provider: 'proxy', route: 'codex', model: 'fixture-codex' }, harness: 'claude-code' }).routeId, 'codex'); +}); + +test('an API-key model that an OAuth Claude account could serve stays on claude-code (fail closed)', async () => { + const { route } = await import('../model-provider/cliproxy/index.mjs'); + const d = deps(); + // The proxy routes by model name across every account. A group model named as an OAuth Claude + // account's model can be served by that account, and a Claude-named one by an account the read + // did not list (disabled, signed out, added later). + d.listGroups = async () => [{ 'base-url': 'https://fixture.invalid', keys: [{ auth_index: 'k' }], models: [ + { name: 'fixture-chat' }, { name: 'upstream-x', alias: 'Claude-Fixture' }, { name: 'claude-upstream', alias: 'fixture-safe' }] }]; + const c = await catalogue(d); + const by = model => c.models.find(x => x.upstream === 'other' && x.model === model); + assert.deepEqual(by('fixture-chat').harnesses, ['claude-code']); + assert.deepEqual(by('Claude-Fixture').harnesses, ['claude-code']); + assert.deepEqual(by('fixture-safe').harnesses, ['claude-code', 'pi']); + assert.deepEqual(preflight({ selection: by('fixture-chat'), harness: 'pi', catalogue: c }), { allow: false, code: 'model-harness-incompatible' }); + for (const upstream of ['other', 'zai', 'codex']) + assert.equal(route({ selection: { upstream, model: 'claude-fixture' }, harness: 'pi' }).code, 'model-harness-incompatible', upstream); + assert.equal(route({ selection: { upstream: 'other', model: 'fixture-safe' }, harness: 'pi' }).upstream, 'other'); +}); diff --git a/packages/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index 290b70924..b98c047b4 100644 --- a/packages/seat/src/sandbox.mjs +++ b/packages/seat/src/sandbox.mjs @@ -313,7 +313,7 @@ export function checkHostedRoute({ adapter, harnessMounts, installRoots, protect // just made there, never a link the session left: bwrap resolves a bind's source on the host. // The adapter and the mounts pass checkHostedRoute against `installRoots`, `protectedPaths` and // `codeRoot` with its `codeHolder` here too; without install roots nothing binds. -export function hostedBinds({ runDir, healthDir, adapter, harnessMounts = [], installRoots, protectedPaths, codeRoot = null, codeHolder = null, relaySocket, node = process.execPath, readOnly = [] }) { +export function hostedBinds({ runDir, healthDir, adapter, harnessMounts = [], installRoots, protectedPaths, codeRoot = null, codeHolder = null, relaySocket, node = process.execPath, readOnly = [], harnessFiles = [] }) { if (relaySocket !== join(runDir, "relay.sock")) throw new Error("hosted binds: the relay socket must be the run directory's relay.sock"); const healthDest = join(runDir, "work", "health"); for (const p of [runDir, healthDir, join(runDir, "bundle"), join(runDir, "tmp"), join(runDir, "work"), healthDest]) { @@ -326,6 +326,20 @@ export function hostedBinds({ runDir, healthDir, adapter, harnessMounts = [], in try { st = lstatSync(p); } catch { throw new Error(`hosted binds: ${relative(runDir, p)} is missing`); } if (!ok(st)) throw new Error(`hosted binds: ${relative(runDir, p)} is not what the host made`); } + // #1873 harness-matrix: a harness's own config file the host wrote under the session's HOME (pi's + // models.json), bound read-only over the writable run directory so the session cannot change + // the proxy it reaches. Each is a regular file, not a link, inside runDir/home. + const homeDir = join(runDir, "home"); + if (!Array.isArray(harnessFiles) || harnessFiles.length > HOSTED_MOUNTS_MAX) refuseRoute("harness-files", "too many harness files"); + for (const f of harnessFiles) { + if (!cleanPath(f) || !f.startsWith(homeDir + "/")) refuseRoute("harness-files", "a harness file is outside the session's HOME"); + let st; + try { st = lstatSync(f); } catch { refuseRoute("harness-files", "a harness file is missing"); } + if (!st.isFile() || st.nlink !== 1) refuseRoute("harness-files", "a harness file is not a regular file"); + for (let parent = dirname(f); parent.length > runDir.length; parent = dirname(parent)) { + if (lstatSync(parent).isSymbolicLink()) refuseRoute("harness-files", "a harness file's directory is a link"); + } + } const route = checkHostedRoute({ adapter, harnessMounts, installRoots, protectedPaths, codeRoot, codeHolder, runDir, node }); const out = systemBinds(node); for (const f of RUNNER_FILES) out.push({ kind: "ro", src: f, why: "a repository file the runner loads" }); @@ -337,6 +351,7 @@ export function hostedBinds({ runDir, healthDir, adapter, harnessMounts = [], in { kind: "ro", src: healthDir, dest: healthDest, why: "the health snapshots the Console writes, read-only, inside the workspace the read tools reach" }, { kind: "ro", src: join(runDir, "bundle"), why: "the run's bundle stays as the host wrote it" }, { kind: "ro", src: join(runDir, "session.json"), why: "the session file stays as the host wrote it" }, + ...harnessFiles.map((f) => ({ kind: "ro", src: f, why: "the harness's own config file, as the host wrote it" })), { kind: "ro", src: relaySocket, why: "the host's relay, the only broker the runner reaches" }, { kind: "rw", src: join(runDir, "tmp"), dest: "/tmp", why: "a private /tmp, on disk in the run directory" }, ); diff --git a/packages/seat/tests/hosted-binds.test.mjs b/packages/seat/tests/hosted-binds.test.mjs index 6441ab048..08c0b574b 100644 --- a/packages/seat/tests/hosted-binds.test.mjs +++ b/packages/seat/tests/hosted-binds.test.mjs @@ -4,7 +4,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { createServer } from "node:net"; -import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { linkSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { HOSTED_MOUNTS_MAX, RUNNER_FILES, checkHostedRoute, hostedArgv, hostedBinds } from "../src/sandbox.mjs"; @@ -243,3 +243,34 @@ test("each mount field is read once, and no mount's dest lies in or around anoth } assert.equal(hostedBinds({ ...base, harnessMounts: [{ src: join(p.install, "m"), dest: "/opt/x", why: "w" }, { src: join(p.install, "n"), dest: "/opt/xy", why: "w" }] }).filter((b) => b.dest?.startsWith("/opt/")).length, 2); }); + +// #1873 harness-matrix: a harness's own config file under the session's HOME (pi's models.json) is +// bound read-only after the writable run directory, so the session cannot change the proxy it +// reaches. A file outside HOME, a link, a hard link, a missing file or a linked directory is refused. +test("hosted binds hold a harness file read-only over the run directory, and refuse one outside HOME or not a plain file", async (t) => { + const p = await layout(t); + const base = { runDir: p.runDir, healthDir: p.healthDir, adapter: p.adapter, relaySocket: p.relaySocket, installRoots: p.installRoots, protectedPaths: p.protectedPaths }; + const agentDir = join(p.runDir, "home", ".pi", "agent"); + mkdirSync(agentDir, { recursive: true }); + const file = join(agentDir, "models.json"); + writeFileSync(file, "{}"); + const list = hostedBinds({ ...base, harnessFiles: [file] }); + const rw = list.findIndex((b) => b.kind === "rw" && b.src === p.runDir); + const ro = list.findIndex((b) => b.kind === "ro" && b.src === file && b.dest === undefined); + assert.ok(rw >= 0 && ro > rw, "read-only, after the writable run directory"); + const refused = (files, label) => assert.throws(() => hostedBinds({ ...base, harnessFiles: files }), (e) => e.code === "hosted-binds-refused" && e.fence === "harness-files", label); + writeFileSync(join(p.runDir, "work", "x.json"), "{}"); + refused([join(p.runDir, "work", "x.json")], "outside HOME"); + refused([join(p.runDir, "home", "..", "work", "x.json")], "a .. path"); + refused([join(p.runDir, "home", "absent.json")], "missing"); + refused([join(p.runDir, "home")], "a directory"); + symlinkSync(join(p.runDir, "work", "x.json"), join(p.runDir, "home", "link.json")); + refused([join(p.runDir, "home", "link.json")], "a link"); + linkSync(join(p.runDir, "work", "x.json"), join(p.runDir, "home", "hard.json")); + refused([join(p.runDir, "home", "hard.json")], "a hard link"); + mkdirSync(join(p.runDir, "work", "d")); + writeFileSync(join(p.runDir, "work", "d", "m.json"), "{}"); + symlinkSync(join(p.runDir, "work", "d"), join(p.runDir, "home", "dlink")); + refused([join(p.runDir, "home", "dlink", "m.json")], "a linked directory"); + refused(Array.from({ length: 9 }, () => file), "too many"); +}); diff --git a/packages/webui/src/triage-host.mjs b/packages/webui/src/triage-host.mjs index 0d6193425..429b5ec58 100644 --- a/packages/webui/src/triage-host.mjs +++ b/packages/webui/src/triage-host.mjs @@ -66,7 +66,7 @@ import { BWRAP, REPO, RUNNER_FILES, checkHostedRoute, hostedArgv, hostedBinds } import { isProviderVariable } from "../../seat/src/session.mjs"; import { configDir as systemConfigDir } from "../../organization/src/organization-config.mjs"; import { typedTools } from "../../harness/src/tools.mjs"; -import { builtinTools } from "../../harness/src/bundle.mjs"; +import { builtinTools, checkedTransport } from "../../harness/src/bundle.mjs"; import { FOUNDER_ENV } from "../../harness/src/runner.mjs"; import { extractCards, plainText } from "./triage-cards.mjs"; import { SNAPSHOT_FILE, writeSnapshot } from "./triage-snapshot.mjs"; @@ -128,7 +128,17 @@ const RUNNER_NAMES = new Set(["MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", " export const KEY_REMOVED = "[key removed]"; // The marker, under host/ (never mounted), that the next start gets a fresh session/. const FRESH_MARKER = "fresh-session"; -export const ROUTE_ENV_ALLOW = Object.freeze(["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN"]); +// #1873 harness-matrix: the names a harness may take the Triage key through. claude-code takes it as +// ANTHROPIC_AUTH_TOKEN; pi as ANTHROPIC_API_KEY, which its models.json names. A route's env holds +// exactly one of them. +export const ROUTE_KEY_NAMES = Object.freeze(["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY"]); +export const ROUTE_ENV_ALLOW = Object.freeze(["ANTHROPIC_BASE_URL", ...ROUTE_KEY_NAMES]); +// The one key name a route env holds and its value, or null when it holds none or more than one. +export const routeKey = (routeEnv) => { + const held = ROUTE_KEY_NAMES.filter((k) => routeEnv?.[k] !== undefined); + if (held.length !== 1 || typeof routeEnv[held[0]] !== "string" || !routeEnv[held[0]]) return null; + return { name: held[0], value: routeEnv[held[0]] }; +}; const ENV_NAME = /^[A-Z][A-Z0-9_]{0,63}$/; const ENV_VALUE_MAX = 4096; const HARNESS_ENV_MAX = 64; @@ -162,7 +172,10 @@ async function repoHarnessModule(name) { } export function triageEnv(source, routeEnv = {}, { node = process.execPath } = {}) { - const env = { PATH: `${dirname(node)}:/usr/bin:/bin` }; + // #1873 harness-matrix: the repository's node_modules/.bin comes first, as a bus session's PATH has + // it (session.mjs sessionEnv): a harness whose adapter runs its binary from there finds it. It is in + // the sandbox only when the harness's own mounts bind node_modules. + const env = { PATH: `${join(REPO, "node_modules", ".bin")}:${dirname(node)}:/usr/bin:/bin` }; for (const k of SPAWN_ENV_ALLOW) if (k !== "PATH" && typeof source?.[k] === "string" && source[k].length <= 256) env[k] = source[k]; for (const k of SPAWN_PATH_ALLOW) if (typeof source?.[k] === "string" && source[k].startsWith("/") && source[k].length <= 256 && cleanValue(source[k])) env[k] = source[k]; for (const k of ROUTE_ENV_ALLOW) if (typeof routeEnv?.[k] === "string" && routeEnv[k].length <= 4096) env[k] = routeEnv[k]; @@ -385,7 +398,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async // #1873 triage-wiring: the module's bundle files and its MOSAIC_ names, which the host sets itself. const hostHarnessEnv = (mod, { admitted, bundleDir, files, policy, tools, toolSocket }) => { - if (typeof mod?.bundle !== "function" || typeof mod?.env !== "function") return {}; + if (typeof mod?.bundle !== "function" || typeof mod?.env !== "function") return { env: {}, files: [] }; const write = (name, text) => { if (typeof name !== "string" || !/^[a-z][a-z0-9.-]{0,63}$/.test(name) || typeof text !== "string") throw new Error("bundle file refused"); writeFileSync(join(bundleDir, name), text, { mode: 0o600, flag: "wx" }); @@ -398,18 +411,54 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async let levels = []; try { levels = typeof mod.levels === "function" ? mod.levels() : []; } catch { levels = []; } const thinking = Array.isArray(levels) && levels.includes("low") ? "low" : Array.isArray(levels) && typeof levels[0] === "string" ? levels[0] : ""; - const session = { model: admitted.model, provider: admitted.provider ?? "", thinking, toolSocket }; - const own = mod.bundle({ dir: bundleDir, files: { ...files }, session: { ...session }, write, quote, models: {}, proxy: null, transports: {} }); - const given = mod.env({ files: { ...(plainObject(own?.files) ? own.files : {}) }, policy, tools, session: { ...session }, models: {}, proxy: null }); + // #1873 harness-matrix: the module gets the real proxy and the route's transport, as a bus + // session's bundle does, so a harness that takes the proxy through its own files (pi's + // models.json) writes them here. Claude Code's bundle() takes no proxy. + const routed = routeKey(admitted.env); + const key = routed?.value; + if (!key) throw new Error("no key"); + const proxy = { baseUrl: admitted.env.ANTHROPIC_BASE_URL, key }; + const session = { model: admitted.model, provider: admitted.provider ?? "", thinking, toolSocket, ...(admitted.transport ? { proxyTransport: { ...admitted.transport } } : {}) }; + const models = { triage: { provider: admitted.provider ?? "", model: admitted.model } }; + const transports = admitted.transport ? { triage: { ...admitted.transport } } : {}; + const own = mod.bundle({ dir: bundleDir, files: { ...files }, session: { ...session }, write, quote, models, proxy: { ...proxy }, transports }); + const ownFiles = plainObject(own?.files) ? own.files : {}; + const given = mod.env({ files: { ...ownFiles }, policy, tools, session: { ...session }, models, proxy: { ...proxy } }); if (!plainObject(given)) throw new Error("env refused"); - const key = admitted.env?.ANTHROPIC_AUTH_TOKEN; + const holdsKey = (v) => typeof v === "string" && (v.includes(key) || Boolean(tokenAnywhere && v.match(tokenAnywhere))); + // The key goes only into the one key name the route answered, as the key itself; the URL, when + // env() names one, is the route's. + for (const [k, v] of Object.entries(given)) { + if (k === routed.name) continue; + if (holdsKey(v)) throw new Error("env holds the key"); + if (k === "ANTHROPIC_BASE_URL" && v !== proxy.baseUrl) throw new Error("env names another proxy"); + } + if (given[routed.name] !== key) throw new Error("env does not take the key as it is"); + // Files the module wrote outside bundle/ (pi's models.json): each a regular file the session's + // HOME holds, with no key in it, bound read-only over the run directory. A harness whose env() + // names no URL must take it from one of them. + const homeDir = join(paths.agent, "home"); + const harnessFiles = []; + let urlInFile = false; + for (const f of Object.values(ownFiles)) { + if (typeof f !== "string" || f.startsWith(bundleDir + "/")) continue; + if (!f.startsWith(homeDir + "/") || f.split("/").includes("..")) throw new Error("bundle file outside the session's HOME"); + const st = lstatSync(f); + if (!st.isFile() || st.nlink !== 1) throw new Error("bundle file is not a regular file"); + const text = readFileSync(f, "utf8"); + if (holdsKey(text)) throw new Error("bundle file holds the key"); + if (text.includes(proxy.baseUrl)) urlInFile = true; + harnessFiles.push(f); + } + if (given.ANTHROPIC_BASE_URL === undefined && !urlInFile) throw new Error("harness takes no proxy URL"); + // A name the route's harnessEnv holds that env() also answers must have env()'s value. + for (const [k, v] of Object.entries(admitted.harnessEnv ?? {})) if (Object.hasOwn(given, k) && given[k] !== v) throw new Error("harness env differs"); const out = {}; for (const [k, v] of Object.entries(given)) { if (!k.startsWith("MOSAIC_") || !mod.envNames.includes(k) || !ENV_NAME.test(k) || RUNNER_NAMES.has(k) || !cleanValue(v)) continue; - if ((typeof key === "string" && key && v.includes(key)) || (tokenAnywhere && v.match(tokenAnywhere))) throw new Error("env holds the key"); out[k] = v; } - return out; + return { env: out, files: harnessFiles }; }; const writeBundle = (admitted, mod) => { @@ -434,11 +483,11 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async writeFileSync(bundle.prompt, hostPrompt(contract, { snapshotPath: join(workspace, "health", SNAPSHOT_FILE), steps }), { mode: 0o600, flag: "wx" }); writeFileSync(bundle.policy, JSON.stringify(policy, null, 2) + "\n", { mode: 0o600, flag: "wx" }); writeFileSync(bundle.tools, JSON.stringify(tools, null, 2) + "\n", { mode: 0o600, flag: "wx" }); - let hostEnv; - try { hostEnv = hostHarnessEnv(mod, { admitted, bundleDir: join(a, "bundle"), files: bundle, policy, tools, toolSocket: join(a, "tools.sock") }); } + let hostEnv, harnessFiles; + try { ({ env: hostEnv, files: harnessFiles } = hostHarnessEnv(mod, { admitted, bundleDir: join(a, "bundle"), files: bundle, policy, tools, toolSocket: join(a, "tools.sock") })); } catch { log("triage host: route refused: harness-bundle"); - rmSync(join(a, "bundle"), { recursive: true, force: true }); + for (const d of ["bundle", "home"]) rmSync(join(a, d), { recursive: true, force: true }); throw new TriageError("triage-route-invalid", "the model route's answer is not usable"); } const run = `triage-${now().toISOString().replace(/[^0-9]/g, "").slice(0, 14)}`; @@ -450,7 +499,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async ...(Number.isInteger(pollInterval) && pollInterval >= 100 ? { pollInterval } : {}), }; writeFileSync(join(a, "session.json"), JSON.stringify(session, null, 2) + "\n", { mode: 0o600, flag: "wx" }); - return run; + return { run, harnessFiles }; }; // The route's answer, checked. It returns a copy taken once, so a getter or a later change to @@ -475,6 +524,10 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async for (const [k, v] of Object.entries(he)) if (refusedHarnessName(k) || !declared.includes(k) || !cleanValue(v)) refuse("harness-env"); const re = a.env ?? {}; if (!plainObject(re) || Object.entries(re).some(([k, v]) => !ROUTE_ENV_ALLOW.includes(k) || !cleanValue(v))) refuse("route-env"); + // #1873 harness-matrix: exactly one key name, and no harnessEnv value holds the key. + const routed = routeKey(re); + if (!routed) refuse("route-env"); + for (const v of Object.values(he)) if (v.includes(routed.value) || (tokenAnywhere && v.match(tokenAnywhere))) refuse("harness-env"); const url = LOOPBACK_URL.exec(re.ANTHROPIC_BASE_URL ?? ""); let port = proxyPort; if (typeof proxyPort === "function") { @@ -482,7 +535,12 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async if (!validPort(port)) refuse("route-env"); } if (!url || Number(url[1]) > 65535 || (port !== null && Number(url[1]) !== port)) refuse("route-env"); - if (tokenShape && !(typeof re.ANTHROPIC_AUTH_TOKEN === "string" && tokenShape.test(re.ANTHROPIC_AUTH_TOKEN))) refuse("route-env"); + if (tokenShape && !tokenShape.test(routed.value)) refuse("route-env"); + // The route's transport, when it gives one, held to the bus's own transport check. + if (a.transport !== undefined) { + try { checkedTransport({ provider: a.provider ?? "", model: a.model }, a.harness, () => ({ ...(plainObject(a.transport) ? a.transport : {}), provider: a.provider ?? "", model: a.model })); } + catch { refuse("transport"); } + } let installRoots; try { installRoots = structuredClone(mod.installRoots({ env })); } catch { refuse("module"); } try { @@ -616,9 +674,9 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async const start = async ({ admitted, installRoots, transcriptRoot, mod }) => { ensureDirs(); - const run = writeBundle(admitted, mod); + const { run, harnessFiles } = writeBundle(admitted, mod); // The key this session holds, and anything shaped like a key, never reaches the transcript. - const key = typeof admitted.env?.ANTHROPIC_AUTH_TOKEN === "string" && admitted.env.ANTHROPIC_AUTH_TOKEN ? admitted.env.ANTHROPIC_AUTH_TOKEN : null; + const key = routeKey(admitted.env)?.value ?? null; const scrub = (body) => { let t = typeof body === "string" ? body : ""; if (key) t = t.split(key).join(KEY_REMOVED); @@ -655,14 +713,14 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async }); let list; try { - list = hostedBinds({ runDir: paths.agent, healthDir: paths.health, adapter: admitted.adapter, harnessMounts: admitted.harnessMounts ?? [], installRoots, protectedPaths, codeRoot, codeHolder: home, relaySocket: join(paths.agent, "relay.sock"), node, readOnly }); + list = hostedBinds({ runDir: paths.agent, healthDir: paths.health, adapter: admitted.adapter, harnessMounts: admitted.harnessMounts ?? [], installRoots, protectedPaths, codeRoot, codeHolder: home, relaySocket: join(paths.agent, "relay.sock"), node, readOnly, harnessFiles }); } catch (e) { if (e?.code !== "hosted-binds-refused") throw e; log(`triage host: route refused at the binds: ${e.fence}`); // Nothing of the refused start stays: the relay, its socket, the bundle and session.json go. relay?.close(); relay = null; - for (const f of ["relay.sock", "session.json", "bundle"]) rmSync(join(paths.agent, f), { recursive: true, force: true }); + for (const f of ["relay.sock", "session.json", "bundle", "home"]) rmSync(join(paths.agent, f), { recursive: true, force: true }); throw new TriageError("triage-route-invalid", "the model route's answer is not usable"); } const argv = hostedArgv(list, paths.agent, [node, RUNNER, paths.agent], { bwrap }); diff --git a/packages/webui/src/triage-route.mjs b/packages/webui/src/triage-route.mjs index 3c628d08c..d41fb241e 100644 --- a/packages/webui/src/triage-route.mjs +++ b/packages/webui/src/triage-route.mjs @@ -8,15 +8,20 @@ // check an organization's route change runs: offered, available, compatible // the harness module adapter, turnEnd, mounts and harnessEnv (packages/plugins/harness/) // proxy.json the proxy's port, read at each admit (proxyPort may be a function) -// The answer's env holds only ANTHROPIC_BASE_URL (the instance proxy on 127.0.0.1) and -// ANTHROPIC_AUTH_TOKEN (the Triage key). harnessEnv holds only the module's own names that the host's -// fence takes (refusedHarnessName): no MOSAIC_* name, which the host sets itself from the module's -// bundle() and env() (triage-host.mjs). A harness runs Triage only when it has bundle() and env() and -// takes the proxy through exactly those two variables: env() given a stand-in proxy must answer the -// stand-in's URL as ANTHROPIC_BASE_URL and its key as ANTHROPIC_AUTH_TOKEN, and put that key nowhere -// else. pi takes its proxy through its own models.json and ANTHROPIC_API_KEY, so it is refused with -// triage-harness-incompatible. The host runs its fences on every answer (triage-host.mjs -// checkAdmitted), so nothing here is trusted on its own. +// the provider module the route's transport for this harness (packages/plugins/model-provider/ +// , route()), the check a bus launch runs: a model the provider holds +// off the harness (an OAuth Claude model on pi) is triage-harness-incompatible +// The answer's env holds ANTHROPIC_BASE_URL (the instance proxy on 127.0.0.1) and the Triage key +// under the one name the harness takes it by (ROUTE_KEY_NAMES: claude-code's ANTHROPIC_AUTH_TOKEN, +// pi's ANTHROPIC_API_KEY). harnessEnv holds only the module's own names that the host's fence takes +// (refusedHarnessName): no MOSAIC_* name, which the host sets itself from the module's bundle() and +// env() (triage-host.mjs). #1873 harness-matrix: a harness runs Triage when it has bundle() and env() +// and meets one contract, with no list of harness names: env() given a stand-in proxy puts the +// stand-in's key, unchanged, in exactly one ROUTE_KEY_NAMES name and nowhere else, and names the +// stand-in's URL only as ANTHROPIC_BASE_URL, if at all. A harness that takes the URL through its own +// file (pi's models.json) is held to that at the host, which checks the file bundle() writes. The +// host runs its fences on every answer (triage-host.mjs checkAdmitted), so nothing here is trusted +// on its own. // // Each refusal is a code from ROUTE_REFUSALS (triage-host.mjs) with a fixed plain line. No code or line carries a path, // a key or an env value. Whether the key is in the proxy's list cannot be known without the proxy @@ -29,11 +34,13 @@ import { fileURLToPath, pathToFileURL } from "node:url"; import { checkRoutes } from "../../organization/src/launch-settings.mjs"; import { SECRET, keyPrefix, readKey } from "../../cli/src/triage-key-file.mjs"; import { readRoute } from "./triage-route-store.mjs"; -import { ROUTE_REFUSALS, refusedHarnessName } from "./triage-host.mjs"; +import { ROUTE_KEY_NAMES, ROUTE_REFUSALS, refusedHarnessName } from "./triage-host.mjs"; +import { checkedTransport } from "../../harness/src/bundle.mjs"; export { ROUTE_REFUSALS }; const HARNESS_ROOT = fileURLToPath(new URL("../../plugins/harness/", import.meta.url)); +const PROVIDER_ROOT = fileURLToPath(new URL("../../plugins/model-provider/", import.meta.url)); const HARNESS_NAME = /^[a-z][a-z0-9-]{0,31}$/; const MODEL_KEY = /^[a-z0-9][a-z0-9._-]{0,63}$/; export const PROBE_TIMEOUT_MS = 1000; @@ -41,10 +48,17 @@ export const PROBE_TIMEOUT_MS = 1000; const refuse = (code) => ({ ok: false, code }); // The stand-in proxy env() is asked about. Neither value is a real URL or key. const PROXY_PROBE = Object.freeze({ baseUrl: "http://127.0.0.1:9", key: "triage-route-probe-key" }); -// True when env(), given the stand-in proxy, puts it in the two route variables and nowhere else. -const takesRouteProxy = (given) => plain(given) - && given.ANTHROPIC_BASE_URL === PROXY_PROBE.baseUrl && given.ANTHROPIC_AUTH_TOKEN === PROXY_PROBE.key - && Object.entries(given).every(([k, v]) => k === "ANTHROPIC_AUTH_TOKEN" || !String(v).includes(PROXY_PROBE.key)); +// The name env(), given the stand-in proxy, takes the key by, or null. The key goes unchanged into +// exactly one ROUTE_KEY_NAMES name and is in no other value; the URL is in no value but +// ANTHROPIC_BASE_URL, which, when present, is the stand-in's URL. +export const routeKeyName = (given) => { + if (!plain(given)) return null; + const holders = Object.entries(given).filter(([, v]) => String(v).includes(PROXY_PROBE.key)); + if (holders.length !== 1 || !ROUTE_KEY_NAMES.includes(holders[0][0]) || holders[0][1] !== PROXY_PROBE.key) return null; + if (given.ANTHROPIC_BASE_URL !== undefined && given.ANTHROPIC_BASE_URL !== PROXY_PROBE.baseUrl) return null; + if (Object.entries(given).some(([k, v]) => k !== "ANTHROPIC_BASE_URL" && String(v).includes(PROXY_PROBE.baseUrl))) return null; + return holders[0][0]; +}; const validPort = (p) => Number.isInteger(p) && p > 0 && p <= 65535; const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const identity = (m) => JSON.stringify([m.provider, m.model, m.module, m.upstream]); @@ -69,6 +83,14 @@ export async function repoHarnessModule(name, root = HARNESS_ROOT) { return import(pathToFileURL(file).href); } +// The repository's model-provider module of that name, or null. +export async function repoProviderModule(name, root = PROVIDER_ROOT) { + if (!HARNESS_NAME.test(name ?? "")) return null; + const file = join(root, name, "index.mjs"); + try { if (!lstatSync(file).isFile()) return null; } catch { return null; } + return import(pathToFileURL(file).href); +} + // Each active organization's catalogue, merged into one. Only ready answers count. A key is offered // when a ready catalogue lists it, and dropped when two disagree on what it is (provider, model, // module, upstream). It is available only when every catalogue listing it says so, and its harnesses @@ -137,17 +159,18 @@ export function checkModel({ model, harness, catalogue, harnesses }) { } } -// createTriageRoute({ dataRoot, configDir, proxyPort, catalogues, harnessModule, probe, env, installed }) +// createTriageRoute({ dataRoot, configDir, proxyPort, catalogues, harnessModule, providerModule, probe, env, installed }) // proxyPort the instance proxy's port (proxy.json), or null when none is configured; or a // function that answers it, called at each admit // catalogues async () -> [catalogue] each active organization's `routes` catalogue // harnessModule async (name) -> the harness module or null; the repository's by default +// providerModule async (name) -> the model-provider module or null; the repository's by default // probe async (port) -> true when the proxy accepts a connection // env the Console's environment, for the module's mounts (MOSAIC_CLAUDE_BIN) // installed () -> the installed harness names // Returns { isSet(), admit() }. isSet answers whether a route is stored (an unreadable store counts // as set, so the read does not hide it; admit refuses it). -export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catalogues = async () => [], harnessModule = repoHarnessModule, probe = probePort, env = process.env, installed = installedHarnesses, keyOptions = {} }) { +export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catalogues = async () => [], harnessModule = repoHarnessModule, providerModule = repoProviderModule, probe = probePort, env = process.env, installed = installedHarnesses, keyOptions = {} }) { if (typeof dataRoot !== "string" || !dataRoot.startsWith("/")) throw new TypeError("dataRoot must be an absolute path"); if (typeof configDir !== "string" || !configDir.startsWith("/")) throw new TypeError("configDir must be an absolute path"); if (proxyPort !== null && typeof proxyPort !== "function" && !validPort(proxyPort)) throw new TypeError("proxyPort must be a port number, null or a function"); @@ -180,28 +203,42 @@ export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catal const failed = checkModel({ model: stored.model, harness: stored.harness, catalogue, harnesses }); if (failed) return refuse(failed); const offered = catalogue.models.find((m) => m.key === stored.model); + // #1873 harness-matrix: the provider's own route for this harness, as a bus launch asks it, so + // a stale catalogue row that names a harness the provider now holds the model off is refused. + // A provider with no route() gives no transport; a harness that needs one then fails below. + let transport = null; + let provider = null; + try { provider = await providerModule(offered.module); } catch { provider = null; } + if (typeof provider?.route === "function") { + const entry = { provider: offered.provider, model: offered.model, module: offered.module, upstream: offered.upstream }; + try { transport = checkedTransport(entry, stored.harness, provider.route); } + catch (e) { return refuse(e?.code === "model-harness-incompatible" ? "triage-harness-incompatible" : "triage-model-not-offered"); } + } // 5. The harness module's own answers. let mod = null; try { mod = await harnessModule(stored.harness); } catch { mod = null; } if (!mod || typeof mod.adapter !== "function" || typeof mod.turnEnd !== "function" || !Array.isArray(mod.envNames) || typeof mod.bundle !== "function" || typeof mod.env !== "function") return refuse("triage-harness-incompatible"); - let adapter, turnEnd, harnessMounts, harnessEnv; + let adapter, turnEnd, harnessMounts, harnessEnv, keyName; try { adapter = mod.adapter(); turnEnd = mod.turnEnd(); harnessMounts = typeof mod.mounts === "function" ? mod.mounts({ env }) : []; - // The module's env() for this model, less every name the host's fence refuses (MOSAIC_* - // among them: the host sets those) and every name the module does not declare. - const session = { model: offered.model, provider: offered.provider ?? "" }; - const given = mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: null }); - harnessEnv = Object.fromEntries(Object.entries(plain(given) ? given : {}) + // The module's env() for this model and the stand-in proxy, held to the contract, less every + // name the host's fence refuses (MOSAIC_* and ANTHROPIC_* among them: the host sets those) + // and every name the module does not declare. + const session = { model: offered.model, provider: offered.provider ?? "", ...(transport ? { proxyTransport: { ...transport } } : {}) }; + const given = mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: { ...PROXY_PROBE } }); + keyName = routeKeyName(given); + if (!keyName) return refuse("triage-harness-incompatible"); + harnessEnv = Object.fromEntries(Object.entries(given) .filter(([k, v]) => mod.envNames.includes(k) && !refusedHarnessName(k) && typeof v === "string")); - if (!takesRouteProxy(mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: { ...PROXY_PROBE } }))) return refuse("triage-harness-incompatible"); } catch { return refuse("triage-harness-incompatible"); } return { ok: true, harness: stored.harness, adapter, turnEnd, harnessMounts, harnessEnv, model: offered.model, provider: offered.provider ?? "", - env: { ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`, ANTHROPIC_AUTH_TOKEN: record.key }, + ...(transport ? { transport } : {}), + env: { ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`, [keyName]: record.key }, }; }, }; diff --git a/packages/webui/tests/triage-host.test.mjs b/packages/webui/tests/triage-host.test.mjs index 8806cfef7..e0ce4846a 100644 --- a/packages/webui/tests/triage-host.test.mjs +++ b/packages/webui/tests/triage-host.test.mjs @@ -22,6 +22,9 @@ const FIXTURE = new URL("./fixtures/triage-adapter.mjs", import.meta.url).pathna const skip = sandboxProblem() ?? false; const scratch = () => mkdtempSync(join(process.env.TMPDIR, "th-")); const CAP = "a".repeat(64); +// #1873 harness-matrix: a route env holds exactly one key name; the fixture routes give this key. +const FIXTURE_KEY = "triage-fixture-key"; +const KEYED = { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", ANTHROPIC_AUTH_TOKEN: FIXTURE_KEY }; const SNAPSHOT = { snapshotVersion: 1, at: "2026-11-16T12:00:00.000Z", console: { state: "answering", at: "2026-11-16T12:00:00.000Z" }, proxy: "not read", systemConfig: { valid: true, exit: 0, code: null }, @@ -46,10 +49,13 @@ const raw = (socket, line) => new Promise((resolve, reject) => { test("triageEnv keeps the fixed names and the route's proxy variables, and nothing else", () => { const env = triageEnv(CONSOLE_ENV, { ANTHROPIC_BASE_URL: "http://127.0.0.1:1", ANTHROPIC_AUTH_TOKEN: "k", MOSAIC_CONFIG: "/y", OTHER: "z" }, { node: "/opt/node/bin/node" }); assert.deepEqual(Object.keys(env).sort(), ["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "LANG", "PATH", "TZ"]); - assert.equal(env.PATH, "/opt/node/bin:/usr/bin:/bin"); + // #1873 harness-matrix: the repository's node_modules/.bin first, as a bus session's PATH has it. + assert.equal(env.PATH, `${join(REPO, "node_modules", ".bin")}:/opt/node/bin:/usr/bin:/bin`); assert.deepEqual(SPAWN_ENV_ALLOW, ["PATH", "LANG", "LC_ALL", "TZ", "TERM"]); - assert.deepEqual(ROUTE_ENV_ALLOW, ["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN"]); - assert.equal(triageEnv(CONSOLE_ENV, { ANTHROPIC_API_KEY: "from-the-route" }).ANTHROPIC_API_KEY, undefined, "the session gets no key, only the proxy's token"); + // #1873 harness-matrix: the route's key goes under the one name its harness takes it by: claude-code + // ANTHROPIC_AUTH_TOKEN, pi ANTHROPIC_API_KEY. The host admits a route env with exactly one of them. + assert.deepEqual(ROUTE_ENV_ALLOW, ["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY"]); + assert.equal(triageEnv(CONSOLE_ENV, { ANTHROPIC_API_KEY: "from-the-route" }).ANTHROPIC_API_KEY, "from-the-route", "the route's key under the name its harness takes"); assert.equal(triageEnv(CONSOLE_ENV).ANTHROPIC_API_KEY, undefined, "a key in the Console's environment is not the route's"); // #1873 triage-wiring: the pinned harness binary passes as an absolute path, as a bus session's does. assert.deepEqual(SPAWN_PATH_ALLOW, ["MOSAIC_CLAUDE_BIN"]); @@ -141,7 +147,7 @@ function spy(root) { } const answer = (root, over = {}) => ({ ok: true, adapter: join(root, "install", "adapter.sh"), harness: "fake", harnessEnv: {}, harnessMounts: [], turnEnd: "exit", - model: "m", provider: "p", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9" }, ...over, + model: "m", provider: "p", env: { ...KEYED }, ...over, }); // One route answer through a fresh host: refused before anything is written or spawned, with the // one code, the owner-readable line, and a log line that names the fence. @@ -284,23 +290,28 @@ test("fence 1: the bundle holds the copy the host checked; no MOSAIC_ name reach await refusedRoute(t, "function", (root) => ({ harnessEnv: { FAKE_OK: "x" }, extra: () => 1 })); }); -test("fence 2: route env holds only the proxy's two names, the base URL is http on 127.0.0.1 with a port, and the host's port and token shape hold when given", async (t) => { +test("fence 2: route env holds only the proxy's names, exactly one key name, the base URL is http on 127.0.0.1 with a port, and the host's port and token shape hold when given", async (t) => { const bad = [ undefined, "", "http://localhost:9", "http://[::1]:9", "https://127.0.0.1:9", "http://127.0.0.1", "http://127.0.0.1:", "http://127.0.0.1:9/", "http://127.0.0.1:9/v1", "http://127.0.0.1:09", "http://127.0.0.1:0", "http://127.0.0.1:65536", "http://127.0.0.1:99999", "http://127.0.0.2:9", "http://user@127.0.0.1:9", "HTTP://127.0.0.1:9", "http://127.0.0.1:9\n", "http://127.0.0.1:9 ", "http://127.1:9", "http://2130706433:9", "http://127.0.0.1:9#x", "http://127.0.0.1:9?x", "http://10.0.0.1:9", 9, ]; - for (const url of bad) await refusedRoute(t, `url ${JSON.stringify(url)}`, () => ({ env: url === undefined ? {} : { ANTHROPIC_BASE_URL: url } })); + for (const url of bad) await refusedRoute(t, `url ${JSON.stringify(url)}`, () => ({ env: url === undefined ? { ANTHROPIC_AUTH_TOKEN: FIXTURE_KEY } : { ANTHROPIC_BASE_URL: url, ANTHROPIC_AUTH_TOKEN: FIXTURE_KEY } })); for (const extra of ["OTHER", "MOSAIC_CONFIG", "ANTHROPIC_API_KEY", "PATH", "anthropic_base_url"]) { - await refusedRoute(t, `env ${extra}`, () => ({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", [extra]: "x" } })); + await refusedRoute(t, `env ${extra}`, () => ({ env: { ...KEYED, [extra]: "x" } })); } + // #1873 harness-matrix: no key name, or both, is refused; the key as ANTHROPIC_API_KEY alone is admitted. + await refusedRoute(t, "no key name", () => ({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9" } })); + await refusedRoute(t, "both key names", () => ({ env: { ...KEYED, ANTHROPIC_API_KEY: FIXTURE_KEY } })); + await refusedRoute(t, "empty key", () => ({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", ANTHROPIC_API_KEY: "" } })); + await admittedRoute(t, "key as ANTHROPIC_API_KEY", () => ({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", ANTHROPIC_API_KEY: FIXTURE_KEY } })); for (const token of ["a\nb", "a\u2028b", "a\u0000b", "a\u007fb"]) await refusedRoute(t, `token ${JSON.stringify(token)}`, () => ({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", ANTHROPIC_AUTH_TOKEN: token } })); await refusedRoute(t, "env array", () => ({ env: ["ANTHROPIC_BASE_URL"] })); - await admittedRoute(t, "port 65535", () => ({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:65535" } })); + await admittedRoute(t, "port 65535", () => ({ env: { ...KEYED, ANTHROPIC_BASE_URL: "http://127.0.0.1:65535" } })); // The host's port. - await refusedRoute(t, "other port", () => ({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9" } }), { proxyPort: 9001 }); - await admittedRoute(t, "its port", () => ({ env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9001" } }), { proxyPort: 9001 }); + await refusedRoute(t, "other port", () => ({ env: { ...KEYED } }), { proxyPort: 9001 }); + await admittedRoute(t, "its port", () => ({ env: { ...KEYED, ANTHROPIC_BASE_URL: "http://127.0.0.1:9001" } }), { proxyPort: 9001 }); // The host's token shape: the whole token, never a part of it. const tokenPattern = /mst-[0-9a-f]{8}/g; for (const token of [undefined, "xmst-deadbeef", "mst-deadbeefx", "mst-DEADBEEF", "", "mst-deadbee"]) { @@ -418,7 +429,9 @@ test("a host whose HOME holds the repository starts the shipped pi harness's own const pi = await import(new URL("../../plugins/harness/pi/index.mjs", import.meta.url).href); const home = dirname(REPO); const opts = { home, harnessModule: async (name) => (name === "pi" ? pi : null) }; - const piRoute = (root) => answer(root, { harness: "pi", adapter: pi.adapter(), harnessMounts: pi.mounts({ env: {} }), harnessEnv: { PI_MODEL: "m", PI_OFFLINE: "1" } }); + // #1873 harness-matrix: pi takes the key as ANTHROPIC_API_KEY and the proxy through its models.json. + const transport = { routeId: "other", endpoint: "root", api: "anthropic-messages", piProvider: "mosaic-proxy-other", compat: null }; + const piRoute = (root) => answer(root, { harness: "pi", adapter: pi.adapter(), harnessMounts: pi.mounts({ env: {} }), harnessEnv: { PI_MODEL: "m", PI_OFFLINE: "1" }, env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", ANTHROPIC_API_KEY: FIXTURE_KEY }, transport }); const s = await admittedRoute(t, "pi under HOME", (root) => () => piRoute(root), opts); assert.equal(s.adapter, pi.adapter()); // The route's names, and the MOSAIC_ names the host sets itself from the module's env() (#1873 @@ -525,7 +538,7 @@ test("end to end under the sandbox: the first message starts the session, which join(dataRoot, "triage", "health", "snapshot.json"), join(dataRoot, "triage", "agent", "work", "health", "snapshot.json"), ]; let admits = 0; - const route = { admit: async () => { admits += 1; return { ok: true, adapter, harness: "fake", harnessEnv: { TRIAGE_PROBE: JSON.stringify(probe) }, harnessMounts: [], turnEnd: "exit", model: "stub", provider: "stub", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9" } }; } }; + const route = { admit: async () => { admits += 1; return { ok: true, adapter, harness: "fake", harnessEnv: { TRIAGE_PROBE: JSON.stringify(probe) }, harnessMounts: [], turnEnd: "exit", model: "stub", provider: "stub", env: { ...KEYED } }; } }; // Each read shows one more spend, so every turn reads as known and the session keeps going: two // turns in a row with unknown usage (a stub turn spends nothing) end it. let reads = 0; @@ -543,7 +556,7 @@ test("end to end under the sandbox: the first message starts the session, which assert.equal(h.running, true); const probed = JSON.parse((await replies(1))[0].text.replace(/\n\n.*$/s, "")); const allowed = new Set([ - "PATH", "LANG", "TZ", "HOME", "TMPDIR", "ANTHROPIC_BASE_URL", "TRIAGE_PROBE", "PWD", "SHLVL", "_", "OLDPWD", + "PATH", "LANG", "TZ", "HOME", "TMPDIR", "ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN", "TRIAGE_PROBE", "PWD", "SHLVL", "_", "OLDPWD", "MOSAIC_REQUEST", "MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", "MOSAIC_SESSION_DIR", "MOSAIC_POLICY_FILE", "MOSAIC_TOOLS_FILE", "MOSAIC_TOOL_SOCKET", "MOSAIC_TURN_MARKER", "MOSAIC_PROVIDER", "MOSAIC_MODEL", "MOSAIC_MODEL_ACTIVITY_DIR", "MOSAIC_UNSLOP_LOG", ]); @@ -593,7 +606,7 @@ function stubRoute(root) { mkdirSync(join(root, "install"), { recursive: true }); const adapter = join(root, "install", "adapter.sh"); writeFileSync(adapter, `exec '${process.execPath}' '${FIXTURE}'\n`, { mode: 0o644 }); - return { admit: async () => ({ ok: true, adapter, harness: "fake", harnessEnv: {}, harnessMounts: [], turnEnd: "exit", model: "stub", provider: "stub", env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9" } }) }; + return { admit: async () => ({ ok: true, adapter, harness: "fake", harnessEnv: {}, harnessMounts: [], turnEnd: "exit", model: "stub", provider: "stub", env: { ...KEYED } }) }; } const repliesOf = (h, n, from = "triage") => waitFor(async () => { const list = (await h.state()).transcript.filter((e) => e.from === from); diff --git a/packages/webui/tests/triage-wiring.test.mjs b/packages/webui/tests/triage-wiring.test.mjs index 9fcb33d24..84f3b1769 100644 --- a/packages/webui/tests/triage-wiring.test.mjs +++ b/packages/webui/tests/triage-wiring.test.mjs @@ -8,7 +8,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { createServer } from "node:net"; -import { request } from "node:http"; +import { createServer as createHttpServer, request } from "node:http"; import { execFileSync, spawnSync } from "node:child_process"; import { createHash, randomBytes } from "node:crypto"; import { chmodSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; @@ -361,15 +361,26 @@ test("refusal: the harness cannot run the model, or its module is missing or bro await refusal(t, "module lacks bundle", { setup, code: "triage-harness-incompatible", opts: { module: { bundle: undefined } } }); await refusal(t, "module lacks env", { setup, code: "triage-harness-incompatible", opts: { module: { env: undefined } } }); await refusal(t, "env ignores the proxy", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true } } }); - const viaApiKey = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_BASE_URL: proxy.baseUrl, ANTHROPIC_API_KEY: proxy.key } : {}) }); - await refusal(t, "env puts the key in another variable", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, env: viaApiKey } } }); + // #1873 harness-matrix: the key goes into exactly one of the route's key names (ROUTE_KEY_NAMES). + const viaOther = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_BASE_URL: proxy.baseUrl, FAKE_KEY: proxy.key } : {}) }); + await refusal(t, "env puts the key in a name the route does not take", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, envNames: ["FAKE_OK", "FAKE_KEY"], env: viaOther } } }); + const bothNames = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_AUTH_TOKEN: proxy.key, ANTHROPIC_API_KEY: proxy.key } : {}) }); + await refusal(t, "env puts the key in both route key names", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, env: bothNames } } }); + const changed = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_API_KEY: `Bearer ${proxy.key}` } : {}) }); + await refusal(t, "env changes the key", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, env: changed } } }); + const urlElsewhere = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key, FAKE_URL: proxy.baseUrl } : {}) }); + await refusal(t, "env names the proxy URL in another variable", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, envNames: ["FAKE_OK", "FAKE_URL"], env: urlElsewhere } } }); const twice = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_BASE_URL: proxy.baseUrl, ANTHROPIC_AUTH_TOKEN: proxy.key, FAKE_COPY: `k=${proxy.key}` } : {}) }); await refusal(t, "env puts the key in a second variable too", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, envNames: ["FAKE_OK", "FAKE_COPY"], env: twice } } }); const otherUrl = ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_BASE_URL: "http://127.0.0.1:1", ANTHROPIC_AUTH_TOKEN: proxy.key } : {}) }); await refusal(t, "env names another proxy URL", { setup, code: "triage-harness-incompatible", opts: { module: { envRaw: true, env: otherUrl } } }); }); -test("refusal: pi, which takes its proxy through its own models.json and ANTHROPIC_API_KEY, is refused as not compatible", { timeout: 30000 }, async (t) => { +// #1873 harness-matrix: pi takes the proxy through its own models.json, which the host writes, and +// the key as ANTHROPIC_API_KEY. The route admits it when the provider gives a transport for pi, and +// refuses a Claude model on pi even when a stale catalogue row still names pi. +const piOffer = (over = {}) => offer({ key: "glm-5", provider: "other", model: "fixture-triage-model", module: "cliproxy", upstream: "other", harnesses: ["claude-code", "pi"], ...over }); +async function piRoute(t, { offered = piOffer(), harness = "pi" } = {}) { const root = scratch(); const dataRoot = join(root, "data"); const configDir = join(root, "cfg"); @@ -377,15 +388,43 @@ test("refusal: pi, which takes its proxy through its own models.json and ANTHROP const proxyServer = createServer((s) => s.destroy()); const proxyPort = await listen(proxyServer); t.after(() => closed(proxyServer)); - const route = createTriageRoute({ dataRoot, configDir, proxyPort, catalogues: async () => [catalogue([offer({ harnesses: ["claude-code", "pi"] })])], env: {} }); + const route = createTriageRoute({ dataRoot, configDir, proxyPort, catalogues: async () => [catalogue([offered])], env: {} }); mkdirSync(join(dataRoot, "triage"), { recursive: true, mode: 0o700 }); chmodSync(dataRoot, 0o700); - writeRoute(dataRoot, { model: "glm-5", harness: "pi", limit: 1000, timeZone: "UTC", by: "owner" }); + writeRoute(dataRoot, { model: "glm-5", harness, limit: 1000, timeZone: "UTC", by: "owner" }); issue(configDir); - assert.deepEqual(await route.admit(), { ok: false, code: "triage-harness-incompatible" }); - // The same store with claude-code is admitted, so the refusal is pi's own. - writeRoute(dataRoot, { model: "glm-5", harness: "claude-code", limit: 1000, timeZone: "UTC", by: "owner", revision: 1 }); - assert.equal((await route.admit()).ok, true); + return { route, proxyPort, dataRoot }; +} + +test("#1873 pi is admitted through the proxy: the key as ANTHROPIC_API_KEY, the provider's transport, pi's own names", { timeout: 30000 }, async (t) => { + for (const [provider, upstream, model, piProvider] of [["other", "other", "fixture-triage-model", "mosaic-proxy-other"], ["zai", "zai", "glm-fixture", "mosaic-proxy-glm"], ["codex", "codex", "gpt-fixture", "mosaic-proxy-codex"]]) { + const { route, proxyPort } = await piRoute(t, { offered: piOffer({ provider, upstream, model }) }); + const a = await route.admit(); + assert.equal(a.ok, true, `${provider}: ${a.code}`); + assert.deepEqual(Object.keys(a.env).sort(), ["ANTHROPIC_API_KEY", "ANTHROPIC_BASE_URL"], provider); + assert.equal(a.env.ANTHROPIC_BASE_URL, `http://127.0.0.1:${proxyPort}`); + assert.equal(a.transport.piProvider, piProvider, provider); + assert.deepEqual([a.harness, a.model, a.turnEnd, a.harnessEnv.PI_PROVIDER, a.harnessEnv.PI_MODEL, a.harnessEnv.PI_OFFLINE], ["pi", model, "marker", piProvider, model, "1"], provider); + assert.equal(Object.keys(a.harnessEnv).some((k) => k.startsWith("ANTHROPIC") || k.startsWith("MOSAIC_")), false, provider); + } +}); + +test("#1873 a Claude model on pi is refused at the Triage route, even with a stale catalogue row that names pi", { timeout: 30000 }, async (t) => { + for (const offered of [ + piOffer({ provider: "anthropic", upstream: "claude", model: "claude-opus-5-5" }), + piOffer({ provider: "other", upstream: "other", model: "anthropic/claude-sonnet-5-5" }), + ]) { + const { route } = await piRoute(t, { offered }); + assert.deepEqual(await route.admit(), { ok: false, code: "triage-harness-incompatible" }, offered.model); + } + // The same Claude row on claude-code is admitted, so the refusal is the harness rule's. + const { route } = await piRoute(t, { offered: piOffer({ provider: "anthropic", upstream: "claude", model: "claude-opus-5-5" }), harness: "claude-code" }); + const a = await route.admit(); + assert.equal(a.ok, true, a.code); + assert.deepEqual(Object.keys(a.env).sort(), ["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL"]); + // A provider with no route() gives pi no transport, so pi cannot take the proxy and is refused. + const none = await piRoute(t, { offered: offer({ harnesses: ["claude-code", "pi"] }) }); + assert.deepEqual(await none.route.admit(), { ok: false, code: "triage-harness-incompatible" }); }); test("the proxy port is read at each admit: a proxy set up after the Console started counts, and the host's fence asks the same function", { timeout: 30000 }, async (t) => { @@ -496,6 +535,68 @@ test("fence: a resolved route that fails the host's fences launches nothing and } }); +// #1873 harness-matrix: a harness that takes the key as ANTHROPIC_API_KEY and the URL through its own +// file, as pi does, meets every host fence: one key name, the key in no harnessEnv value or file, the +// key's shape, a valid transport, the URL from the route only, the file inside the session's HOME. +const piLike = (over = {}) => ({ + envRaw: true, + env: ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}) }), + bundle: ({ dir, proxy }) => { + const file = join(dir, "..", "home", "harness.json"); + writeFileSync(file, JSON.stringify({ baseUrl: proxy.baseUrl, apiKey: "$ANTHROPIC_API_KEY" }), { mode: 0o600, flag: "wx" }); + return { files: { harness: join(dir.slice(0, -"/bundle".length), "home", "harness.json") } }; + }, + ...over, +}); +test("#1873 fence: a pi-shaped answer passes the host's fences, and each fence still fires on it", { timeout: 60000 }, async (t) => { + // The pi-shaped harness is admitted, writes its file under HOME and launches. + const root0 = scratch(); + const ok = await rig(t, { bwrap: spy(root0), module: piLike() }); + ok.setRoute(); + const key = issue(ok.configDir); + const a = await ok.route.admit(); + assert.deepEqual(Object.keys(a.env).sort(), ["ANTHROPIC_API_KEY", "ANTHROPIC_BASE_URL"]); + await ok.h.message("hello"); + await waitFor(() => existsSync(join(root0, "spawned"))); + const argv = readFileSync(join(root0, "spawn-argv"), "utf8").split("\n"); + const file = join(ok.dataRoot, "triage", "agent", "home", "harness.json"); + const at = argv.findIndex((v, i) => v === "--ro-bind" && argv[i + 1] === file && argv[i + 2] === file); + const rw = argv.findIndex((v, i) => v === "--bind" && argv[i + 1] === join(ok.dataRoot, "triage", "agent")); + assert.ok(at > rw && rw >= 0, "the harness file is bound read-only over the writable run directory"); + const spawnEnv = readFileSync(join(root0, "spawn-env"), "utf8"); + assert.ok(spawnEnv.split("\n").some((l) => l === `ANTHROPIC_API_KEY=${key}`), "the key goes to the spawn as ANTHROPIC_API_KEY"); + assert.equal(spawnEnv.includes("ANTHROPIC_AUTH_TOKEN="), false); + await ok.h.stop(); + + const fence = async (label, { module, patch = (x) => x, fenceName, tokenDir = null }) => { + const rootN = scratch(); + const r = await rig(t, { bwrap: spy(rootN), module, ...(tokenDir ? { tokenDir } : {}) }); + r.setRoute(); + const k = issue(r.configDir); + const admit = r.route.admit.bind(r.route); + r.route.admit = async () => { const x = await admit(); return x.ok ? patch(x, k) : x; }; + await assert.rejects(r.h.message("hello"), (e) => e.code === "triage-route-invalid", label); + assert.ok(r.logs.includes(`triage host: route refused: ${fenceName}`), `${label}: ${r.logs.join(" | ")}`); + assert.equal(existsSync(join(rootN, "spawned")), false, `${label}: nothing launched`); + assert.equal(existsSync(join(r.dataRoot, "triage", "agent", "home", "harness.json")), false, `${label}: the harness file is removed`); + assertNoKey(k, [["host log", r.logs.join("\n")]]); + }; + await fence("both key names", { module: piLike(), patch: (x) => ({ ...x, env: { ...x.env, ANTHROPIC_AUTH_TOKEN: x.env.ANTHROPIC_API_KEY } }), fenceName: "route-env" }); + await fence("no key name", { module: piLike(), patch: (x) => ({ ...x, env: { ANTHROPIC_BASE_URL: x.env.ANTHROPIC_BASE_URL } }), fenceName: "route-env" }); + await fence("the key in harnessEnv", { module: piLike(), patch: (x, k) => ({ ...x, harnessEnv: { FAKE_OK: `k=${k}` } }), fenceName: "harness-env" }); + await fence("a transport the bus would refuse", { module: piLike(), patch: (x) => ({ ...x, transport: { routeId: "x", endpoint: "elsewhere", api: "anthropic-messages", piProvider: "p", compat: null } }), fenceName: "transport" }); + await fence("a key of another instance's shape", { module: piLike(), fenceName: "route-env", tokenDir: "/another/instance" }); + await fence("env copies the real key at the host", { module: piLike({ env: ({ proxy }) => ({ FAKE_OK: proxy && !proxy.key.startsWith("triage-route-probe") ? `k=${proxy.key}` : "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}) }) }), fenceName: "harness-bundle" }); + await fence("env names another proxy at the host", { module: piLike({ env: ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key, ...(proxy.key.startsWith("triage-route-probe") ? {} : { ANTHROPIC_BASE_URL: "http://127.0.0.1:1" }) } : {}) }) }), fenceName: "harness-bundle" }); + await fence("no proxy URL in env or a file", { module: piLike({ bundle: () => ({ files: {} }) }), fenceName: "harness-bundle" }); + await fence("a harness file outside HOME", { module: piLike({ bundle: ({ dir, proxy }) => { const f = join(dir, "..", "work", "x.json"); writeFileSync(f, proxy.baseUrl, { flag: "wx" }); return { files: { x: join(dir.slice(0, -"/bundle".length), "work", "x.json") } }; } }), fenceName: "harness-bundle" }); + await fence("a harness file holding the key", { module: piLike({ bundle: ({ dir, proxy }) => { const f = join(dir.slice(0, -"/bundle".length), "home", "harness.json"); writeFileSync(f, `${proxy.baseUrl} ${proxy.key}`, { flag: "wx" }); return { files: { harness: f } }; } }), fenceName: "harness-bundle" }); + await fence("a harness file that is a link", { module: piLike({ bundle: ({ dir, proxy }) => { const base = dir.slice(0, -"/bundle".length); writeFileSync(join(base, "work", "real.json"), proxy.baseUrl); const f = join(base, "home", "harness.json"); symlinkSync(join(base, "work", "real.json"), f); return { files: { harness: f } }; } }), fenceName: "harness-bundle" }); + await fence("env changes the key at the host", { module: piLike({ env: ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key.startsWith("triage-route-probe") ? proxy.key : `Bearer ${proxy.key}` } : {}) }) }), fenceName: "harness-bundle" }); + await fence("env drops the key at the host", { module: piLike({ env: ({ proxy }) => ({ FAKE_OK: "1", ...(proxy?.key.startsWith("triage-route-probe") ? { ANTHROPIC_API_KEY: proxy.key } : {}) }) }), fenceName: "harness-bundle" }); + await fence("env answers another value at the host", { module: piLike({ env: ({ proxy }) => ({ FAKE_OK: proxy && !proxy.key.startsWith("triage-route-probe") ? "2" : "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}) }) }), fenceName: "harness-bundle" }); +}); + test("fence: every admit runs the fences, a running session or not: a message whose route fails is not queued", { timeout: 30000 }, async (t) => { const root0 = scratch(); const r = await rig(t, { bwrap: standIn(root0) }); @@ -578,6 +679,77 @@ printf '%s\\n' '{"type":"result","subtype":"success","is_error":false,"result":" assertNoKey(key, [...filesUnder(dataRoot), ["host log", logs.join("\n")], ["state", JSON.stringify(await h.state())]]); }); +// #1873 harness-matrix: the pi harness the repository ships, end to end under the real sandbox and +// the real pi, against a stand-in proxy on the route's port that answers the Anthropic messages API. +// The route admits pi with the provider's transport; the host writes pi's models.json under the +// session's HOME, binds it read-only, and gives pi the key as ANTHROPIC_API_KEY. No model is called. +function anthropicStandIn(text) { + const seen = []; + const server = createHttpServer((req, res) => { + const chunks = []; + req.on("data", (c) => chunks.push(c)); + req.on("end", () => { + let body = {}; + try { body = JSON.parse(Buffer.concat(chunks).toString("utf8")); } catch { body = {}; } + const keyHeader = req.headers["x-api-key"] ?? String(req.headers.authorization ?? "").replace(/^Bearer /, ""); + seen.push({ method: req.method, url: req.url, model: body.model, keySha: sha(keyHeader), stream: body.stream }); + if (req.method !== "POST" || !/^\/v1\/messages(\?|$)/.test(req.url ?? "")) { res.writeHead(404); return res.end(); } + const message = { id: "msg_stand_in", type: "message", role: "assistant", model: body.model, stop_sequence: null }; + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + const event = (type, data) => res.write(`event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`); + event("message_start", { message: { ...message, content: [], stop_reason: null, usage: { input_tokens: 1, output_tokens: 0 } } }); + event("content_block_start", { index: 0, content_block: { type: "text", text: "" } }); + event("content_block_delta", { index: 0, delta: { type: "text_delta", text } }); + event("content_block_stop", { index: 0 }); + event("message_delta", { delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 1 } }); + event("message_stop", {}); + res.end(); + }); + }); + return { server, seen }; +} + +test("#1873 the pi harness answers through the proxy: the route's model, the Triage key as ANTHROPIC_API_KEY, pi's models.json read-only, no key in any file", { skip, timeout: 120000 }, async (t) => { + const root = scratch(); + const dataRoot = join(root, "data"); + const configDir = join(root, "cfg"); + mkdirSync(configDir, { mode: 0o700 }); + const { server, seen } = anthropicStandIn("pi stand-in answer"); + const proxyPort = await listen(server); + t.after(() => { server.closeAllConnections?.(); return closed(server); }); + const logs = []; + const route = createTriageRoute({ dataRoot, configDir, proxyPort, catalogues: async () => [catalogue([piOffer()])], env: {} }); + const h = createTriageHost({ dataRoot, route, proxyPort, tokenPattern: triageTokenPattern(configDir), readSnapshot: async () => SNAPSHOT, env: CONSOLE_ENV, usageSource: spendSource, now: () => new Date(NOW), log: (l) => logs.push(l) }); + t.after(() => h.stop()); + mkdirSync(join(dataRoot, "triage"), { recursive: true, mode: 0o700 }); + chmodSync(dataRoot, 0o700); + writeRoute(dataRoot, { model: "glm-5", harness: "pi", limit: 100000, timeZone: "UTC", by: "owner" }); + const key = issue(configDir); + + await h.message("hello"); + const replies = await triageReplies(h, 1); + assert.equal(replies[0].text, "pi stand-in answer", JSON.stringify({ transcript: (await h.state()).transcript, logs })); + // What the stand-in proxy saw: the Anthropic messages route, the route's model, the Triage key. + const calls = seen.filter((c) => c.method === "POST"); + assert.ok(calls.length >= 1, JSON.stringify(seen)); + for (const c of calls) { + assert.match(c.url, /^\/v1\/messages/); + assert.equal(c.model, "fixture-triage-model"); + assert.equal(c.keySha, sha(key), "pi sends the Triage key"); + } + // pi's models.json: the route's provider and model at the proxy's URL, the key only by name. + const agent = join(dataRoot, "triage", "agent"); + const models = JSON.parse(readFileSync(join(agent, "home", ".pi", "agent", "models.json"), "utf8")); + assert.deepEqual(Object.keys(models.providers), ["mosaic-proxy-other"]); + const p = models.providers["mosaic-proxy-other"]; + assert.deepEqual([p.baseUrl, p.api, p.apiKey, p.models.map((m) => m.id)], [`http://127.0.0.1:${proxyPort}`, "anthropic-messages", "$ANTHROPIC_API_KEY", ["fixture-triage-model"]]); + const session = JSON.parse(readFileSync(join(agent, "session.json"), "utf8")); + assert.deepEqual([session.harness, session.turnEnd, session.harnessEnv.PI_PROVIDER, session.harnessEnv.PI_MODEL, session.harnessEnv.MOSAIC_THINKING], ["pi", "marker", "mosaic-proxy-other", "fixture-triage-model", "low"]); + assert.equal(Object.keys(session.harnessEnv).some((k) => k.startsWith("ANTHROPIC")), false); + await h.stop(); + assertNoKey(key, [...filesUnder(dataRoot), ["host log", logs.join("\n")], ["state", JSON.stringify(await h.state())]]); +}); + test("the key scrub: an answer that carries the session's key, or anything shaped like a key, reaches the transcript and the read with the key removed", { skip, timeout: 60000 }, async (t) => { const r = await rig(t); r.setRoute();