Compare commits

...
Author SHA1 Message Date
jason.woltje 4f63176676 test(triage): review round 5: a host bundle file replaced with a FIFO is refused before the host reads it (#1873, lane harness-matrix) 2026-11-19 14:43:26 -06:00
jason.woltje 1e0aba8b37 fix(triage): review round 4: the host refuses a pi command value (a string that starts with "!") in any file the module wrote, checks each host bundle file after env() whether or not the scan reaches it, and records ctime for the rest of the run directory (#1873, lane harness-matrix) 2026-11-19 14:43:26 -06:00
jason.woltje 680bb90af3 fix(triage): review round 3: the host checks every URL in a file the module wrote under bundle/, home/, work/ or tmp/, as written or as a URL parser reads it; refuses a backslash in a file JSON.parse refuses; refuses a change to its own bundle files or the rest of the run directory; and a refused start removes work/ and tmp/ too (#1873, lane harness-matrix) 2026-11-19 14:43:26 -06:00
jason.woltje 6a24cf743d test(triage): a declared harness file with a second hard link is refused (#1873, lane harness-matrix) 2026-11-19 14:43:26 -06:00
jason.woltje 06ee172a9d fix(triage): review round 2: the host reads every file under the run's bundle/, home/, work/ and tmp/ after bundle() and env(), refuses a link or a key there, and reads a JSON file decoded too, so an escaped key or URL is seen as the harness sees it (#1873, lane harness-matrix) 2026-11-19 14:43:26 -06:00
jason.woltje 66676804eb test(triage): write() refuses a bundle file that holds the key before the file exists, so the key never rests on disk until the scan (#1873, lane harness-matrix) 2026-11-19 14:43:26 -06:00
jason.woltje a3ad6578c4 fix(harness): review round: a legacy model entry (a provider or route name, no module) carries no account data, so a Claude-named model on it is treated as OAuth and held to claude-code; the Triage host refuses a key in any bundle/ file and any URL but the proxy's in a harness file; the models.json comment says what route() can and cannot see (#1873, lane harness-matrix) 2026-11-19 14:43:26 -06:00
jason.woltje 77a951cfe7 fix(harness): the harness rule keys on the account kind: a Claude model from an API key runs on claude-code and pi; an OAuth Claude model, or a group model whose name an OAuth Claude account offers, runs on claude-code only (#1873, lane harness-matrix)
Jason 2026-11-19: Claude models through an API key may run on any harness; OAuth Claude models only
on claude-code. The name check is gone. The catalogue reads every Claude OAuth account's model names,
whatever the account's state, and holds a group model with one of those names to claude-code, since
the proxy routes by name across every account. route() has no account data, so it holds upstream
claude only; admission and Triage hold the rest through the catalogue.
2026-11-19 14:43:26 -06:00
jason.woltje cd58ac23cd 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.
2026-11-19 14:43:26 -06:00
17 changed files with 1045 additions and 127 deletions

No files matched your search

@@ -0,0 +1,50 @@
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 an
// OAuth 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 model from an API-key group (upstream other) is admitted on pi; one whose name an OAuth account offers is refused', 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: true });
// The catalogue holds a group model to claude-code when an OAuth Claude account offers its name.
assert.deepEqual(await admission(t, entry, { upstream: 'other', model: 'anthropic/claude-sonnet-5-5', harnesses: ['claude-code'] }), { 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);
}
});
@@ -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');
+2 -1
View File
@@ -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 });
@@ -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.
+27 -7
View File
@@ -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,26 @@ for (const [provider, route, generated, api] of [
});
}
// A legacy entry with no provider plugin gets no catalogue check, so a Claude-named model on a z.ai
// or codex route is treated as OAuth too (review P1-1).
for (const [provider, route, name] of [['anthropic', null], ['proxy', 'anthropic'], ['zai', null, 'claude-opus-5-5'], ['proxy', 'glm', 'claude-opus-5-5'], ['proxy', 'codex', 'claude-sonnet-5-5']]) {
test(`#1873 a stored Claude route ${provider}${route ? ':' + route : ''}${name ? ' ' + name : ''} 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;
if (name) doc.launch.models['claude-opus-5-5'].model = name;
});
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 +137,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 +146,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]);
});
+8 -3
View File
@@ -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,13 @@ 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 the provider's route() lets this session's harness run, so a
// pi session's models.json never names an upstream claude model or a Claude-named legacy entry.
// route() has no account data: a module entry the catalogue holds to claude-code for an OAuth name
// can still be listed. Only the session's own model is admitted against the catalogue.
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);
@@ -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, '<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);
});
}
+34 -23
View File
@@ -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));
}
});
+25 -5
View File
@@ -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,23 @@ 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 OAuth 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' }])
assert.throws(() => sessionModel(resolved, { selected }, 'primary', { proxy: true }), { code: 'model-harness-incompatible' }, JSON.stringify(selected));
// Jason 2026-11-19: the rule keys on the account kind. A module entry, whose kind the catalogue
// knows, runs on pi with a Claude name. A legacy entry carries no account data, so a Claude name
// there is treated as OAuth.
for (const selected of [{ provider: 'other', model: 'anthropic/claude-sonnet-5-5', module: 'cliproxy', upstream: 'other' },
{ provider: 'zai', model: 'claude-looking-id', module: 'cliproxy', upstream: 'zai' }, { provider: 'zai', model: 'glm-fixture' }])
assert.equal(sessionModel(resolved, { selected }, 'primary', { proxy: true }).model, selected.model, JSON.stringify(selected));
for (const selected of [{ provider: 'proxy', route: 'codex', model: 'claude-looking-id' }, { provider: 'zai', model: 'claude-looking-id' }, { provider: 'proxy', route: 'glm', model: 'claude-opus-5-5' }])
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);
});
@@ -5,7 +5,12 @@ 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;
// a model from an API key, Claude or not, runs on claude-code and pi. The rule keys on the account
// kind: the proxy lists OAuth accounts (credentials, upstream `claude`) apart from API-key groups.
// It routes a request by model name across every account, so a group model whose name an OAuth
// Claude account offers (`oauth`) could be served by that account and is held to claude-code too.
const harnesses = (upstream, oauth = false) => upstream === 'claude' || oauth ? ['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 }) {
@@ -25,12 +30,20 @@ export async function catalogue({ listAccounts, listModels, listGroups, now }) {
reason: upstream === 'other' || upstream === 'codex' ? 'unverified' : null, harnesses: harnesses(upstream) });
if (models.length > MAX_MODELS) throw Error();
};
// Every OAuth Claude account's names, whatever its state: one disabled, cooling down or signed
// out now can serve a request later, before the catalogue is read again.
const oauth = new Set();
for (const row of accounts.files) {
if (!['claude', 'codex'].includes(row?.provider) || row.disabled === true || row.unavailable === true || !['ready', 'active'].includes(row.status)) continue;
if (!['claude', 'codex'].includes(row?.provider)) continue;
const listed = row.disabled !== true && row.unavailable !== true && ['ready', 'active'].includes(row.status);
if (!listed && row.provider !== 'claude') continue;
if (typeof row.name !== 'string' || !row.name || row.name.length > 512) throw Error();
const response = await listModels(row.name);
if (!Array.isArray(response?.models) || response.models.length > MAX_MODELS) throw Error();
for (const model of response.models) add(row.provider, model?.id);
for (const model of response.models) {
if (row.provider === 'claude' && typeof model?.id === 'string') oauth.add(model.id.toLowerCase());
if (listed) add(row.provider, model?.id);
}
}
for (const group of groups) {
if (!Array.isArray(group?.keys)) throw Error();
@@ -43,6 +56,7 @@ 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);
}
for (const m of models) m.harnesses = harnesses(m.upstream, 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 +75,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 +88,11 @@ 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' };
// A selection that names this module came from the catalogue, which knows the account kind and is
// checked at admission. A legacy one (a provider or route name only) gets no catalogue check, so
// the kind is unknown and a model named as Claude is treated as OAuth.
const legacyClaude = selection?.module !== 'cliproxy' && /claude/i.test(String(selection?.model ?? ''));
if (harness && !harnesses(upstream, legacyClaude).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] };
}
@@ -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,113 @@ 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 runs on claude-code and pi whatever its name; the rule keys on the account kind', async () => {
const { route } = await import('../model-provider/cliproxy/index.mjs');
const d = deps();
// Jason 2026-11-19: a Claude model from an API key runs on any harness. A name that says Claude,
// or an OpenRouter-style anthropic/ name, is not an OAuth account.
d.listGroups = async () => [{ 'base-url': 'https://fixture.invalid', keys: [{ auth_index: 'k' }], models: [
{ name: 'upstream-x', alias: 'Claude-Fixture' }, { name: 'anthropic/claude-sonnet-5-5' }, { name: 'claude-upstream', alias: 'fixture-safe' }] },
{ 'base-url': ZAI, keys: [{ auth_index: 'z' }], models: [{ name: 'claude-zai-fixture' }] }];
const c = await catalogue(d);
for (const [upstream, model] of [['other', 'Claude-Fixture'], ['other', 'anthropic/claude-sonnet-5-5'], ['other', 'fixture-safe'], ['zai', 'claude-zai-fixture']]) {
const row = c.models.find(x => x.upstream === upstream && x.model === model);
assert.deepEqual(row.harnesses, ['claude-code', 'pi'], model);
for (const harness of ['claude-code', 'pi'])
assert.deepEqual(preflight({ selection: row, harness, catalogue: c }), { allow: true }, `${model} ${harness}`);
}
for (const upstream of ['other', 'zai', 'codex'])
for (const model of ['claude-fixture', 'anthropic/claude-sonnet-5-5', 'fixture-safe'])
for (const harness of ['claude-code', 'pi'])
assert.equal(route({ selection: { module: 'cliproxy', upstream, model }, harness }).upstream, upstream, `${upstream} ${model} ${harness}`);
});
test('a legacy selection carries no account data, so a Claude-named model on it is treated as OAuth and held to claude-code', async () => {
const { route } = await import('../model-provider/cliproxy/index.mjs');
// A legacy entry (a provider or route name, no module) gets no catalogue check at launch, and the
// proxy may serve its name from an OAuth Claude account.
for (const selection of [{ provider: 'zai', model: 'claude-opus-5-5' }, { provider: 'proxy', route: 'glm', model: 'Claude-Opus-5-5' },
{ provider: 'proxy', route: 'codex', model: 'claude-sonnet-5-5' }, { upstream: 'other', model: 'anthropic/claude-sonnet-5-5' }]) {
assert.equal(route({ selection, harness: 'pi' }).code, 'model-harness-incompatible', JSON.stringify(selection));
assert.equal(route({ selection, harness: 'claude-code' }).model, selection.model, JSON.stringify(selection));
// The same name from the catalogue, where the account kind is known, runs on pi.
if (selection.upstream || selection.provider === 'zai') assert.equal(route({ selection: { ...selection, module: 'cliproxy' }, harness: 'pi' }).model, selection.model);
}
for (const selection of [{ provider: 'zai', model: 'glm-fixture' }, { provider: 'proxy', route: 'codex', model: 'gpt-fixture' }])
assert.equal(route({ selection, harness: 'pi' }).model, selection.model, JSON.stringify(selection));
});
test('an API-key model whose name an OAuth Claude account offers stays on claude-code, the account listed or not (fail closed)', async () => {
const { route } = await import('../model-provider/cliproxy/index.mjs');
// The proxy routes by model name across every account, so a group model named as an OAuth Claude
// account's model can be served by that account. A disabled, unavailable or signed-out account
// can serve it later, before the catalogue is read again, so its names count too.
for (const state of [{ status: 'active' }, { status: 'active', disabled: true }, { status: 'active', unavailable: true }, { status: 'error' }]) {
const d = deps();
d.listAccounts = async () => ({ files: [{ name: 'credential-1', provider: 'claude', ...state }, { name: 'credential-2', provider: 'codex', status: 'active' }] });
d.listModels = async name => ({ models: name === 'credential-1' ? [{ id: 'Fixture-Chat' }] : [{ id: 'fixture-codex' }] });
d.listGroups = async () => [{ 'base-url': 'https://fixture.invalid', keys: [{ auth_index: 'k' }], models: [{ name: 'fixture-chat' }, { name: 'fixture-safe' }] },
{ 'base-url': ZAI, keys: [{ auth_index: 'z' }], models: [{ name: 'upstream', alias: 'FIXTURE-CHAT' }] }];
const c = await catalogue(d);
const label = JSON.stringify(state);
assert.equal(c.status, 'ready', label);
const by = (upstream, model) => c.models.find(x => x.upstream === upstream && x.model === model);
assert.deepEqual(by('other', 'fixture-chat').harnesses, ['claude-code'], label);
assert.deepEqual(by('zai', 'FIXTURE-CHAT').harnesses, ['claude-code'], label);
assert.deepEqual(by('other', 'fixture-safe').harnesses, ['claude-code', 'pi'], label);
assert.deepEqual(by('codex', 'fixture-codex').harnesses, ['claude-code', 'pi'], label);
assert.deepEqual(preflight({ selection: by('other', 'fixture-chat'), harness: 'pi', catalogue: c }), { allow: false, code: 'model-harness-incompatible' }, label);
// Only a usable account's models are offered as rows.
assert.equal(c.models.some(x => x.upstream === 'claude'), state.status === 'active' && !state.disabled && !state.unavailable, label);
}
// route() has no account data: it holds upstream claude only, and admission holds the rest by the catalogue.
assert.equal(route({ selection: { upstream: 'other', model: 'fixture-chat' }, harness: 'pi' }).upstream, 'other');
});
test('a Claude OAuth account whose models cannot be read makes the catalogue unavailable, listed or not', async () => {
for (const state of [{ status: 'active' }, { status: 'active', disabled: true }]) {
const d = deps();
d.listAccounts = async () => ({ files: [{ name: 'credential-1', provider: 'claude', ...state }] });
d.listModels = async () => { throw new Error('fixture private error'); };
assert.equal((await catalogue(d)).status, 'unavailable', JSON.stringify(state));
}
// A codex account that is not usable is not read at all.
const d = deps();
d.listAccounts = async () => ({ files: [{ name: 'credential-2', provider: 'codex', status: 'active', disabled: true }] });
d.listModels = async () => { throw new Error('fixture private error'); };
assert.equal((await catalogue(d)).status, 'ready');
});
+16 -1
View File
@@ -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" },
);
+32 -1
View File
@@ -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");
});
+174 -23
View File
@@ -35,12 +35,15 @@
//
// #1873 triage-wiring: the host sets the harness's MOSAIC_ names itself. At each start it calls the
// module's bundle() into the run's bundle/ (Claude Code: settings.json with the tool gate hook and
// mcp.json with the typed tools' server) and its env() with no proxy, and keeps the MOSAIC_ names the
// mcp.json with the typed tools' server) and its env(), and keeps the MOSAIC_ names the
// module declares, with clean string values, never a name the runner sets. Every other name is the
// route's, under the fence above, so no name has two sources. The thinking
// level is the module's "low", or its first level. These join the route's fenced harnessEnv in
// session.json. The key is never among them: env() gets no proxy, and a value that holds the key
// refuses the start. MOSAIC_CLAUDE_BIN, the pinned binary's absolute path, passes from the
// session.json. The key is never among them: #1873 harness-matrix gives bundle() and env() the
// real proxy (pi takes it through models.json), and a MOSAIC_ value or any file under the run's
// bundle/, home/, work/ or tmp/ that holds the key, raw or JSON-decoded, refuses the start, as
// does a URL other than the proxy's in a file the module wrote there, or a change the module made
// to the rest of the run directory (session/). MOSAIC_CLAUDE_BIN, the pinned binary's absolute path, passes from the
// Console's environment as it does for a bus session. An answer the agent sends has the key, and
// anything shaped like it, replaced before it reaches the transcript.
//
@@ -57,7 +60,7 @@
import { spawn } from "node:child_process";
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
import { appendFileSync, closeSync, existsSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, chmodSync, writeFileSync } from "node:fs";
import { appendFileSync, closeSync, existsSync, lstatSync, mkdirSync, readdirSync, openSync, readFileSync, rmSync, chmodSync, writeFileSync } from "node:fs";
import { createServer } from "node:net";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
@@ -66,7 +69,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 +131,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 +175,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,9 +401,38 @@ 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: [] };
// #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. No file it writes, in
// bundle/ or outside it, may hold the key or anything shaped like it.
const routed = routeKey(admitted.env);
const key = routed?.value;
if (!key) throw new Error("no key");
const holdsKey = (v) => typeof v === "string" && (v.includes(key) || Boolean(tokenAnywhere && v.match(tokenAnywhere)));
// A file's text, and when it parses as JSON every key and string in it, decoded: an escaped key
// or URL ("\u0061...", "http:\/\/...") is seen as the harness that parses the file sees it. A
// harness can parse more loosely than JSON.parse (pi takes a byte order mark, comments and
// trailing commas), so a file that does not parse may hold no backslash: its escapes would be
// decoded by the harness and not here.
const decoded = (text) => {
const out = [text];
let doc;
try { doc = JSON.parse(text); } catch {
if (text.includes("\\")) throw new Error("bundle file holds an escape it does not parse");
return out;
}
const stack = [doc];
while (stack.length) {
const v = stack.pop();
if (typeof v === "string") out.push(v);
else if (v && typeof v === "object") for (const [k, x] of Object.entries(v)) { out.push(k); stack.push(x); }
}
return out;
};
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");
if (decoded(text).some(holdsKey)) throw new Error("bundle file holds the key");
writeFileSync(join(bundleDir, name), text, { mode: 0o600, flag: "wx" });
return [name, createHash("sha256").update(text).digest("hex")];
};
@@ -398,18 +443,114 @@ 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 });
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 } } : {};
// The rest of the run directory (session/, kept across starts) is the module's to leave alone:
// what it holds before bundle() must be what it holds after env(). ctime is in the record
// because no user process can set it back, as it can mtime.
const scanned = new Set(["bundle", "home", "work", "tmp"]);
const kept = () => {
const seen = new Map();
for (const name of readdirSync(paths.agent, { recursive: true })) {
const rel = String(name);
if (scanned.has(rel.split("/")[0])) continue;
const st = lstatSync(join(paths.agent, rel));
seen.set(rel, `${st.mode}:${st.ino}:${st.size}:${st.mtimeMs}:${st.ctimeMs}:${st.nlink}`);
}
return seen;
};
const before = kept();
// The host's own bundle files (prompt, policy, tools) must be as the host wrote them.
const hostFiles = new Map(Object.values(files).map((f) => [f, readFileSync(f, "utf8")]));
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 after = kept();
const changed = [...after].filter(([k, v]) => before.get(k) !== v).map(([k]) => k);
if (changed.length || after.size !== before.size) {
// What the module added or changed goes, and the next start takes a fresh session.
for (const k of changed) rmSync(join(paths.agent, k), { recursive: true, force: true });
freshSession = true;
throw new Error("bundle changed the run directory");
}
// Each host bundle file is still there, a regular file with one link and the same bytes,
// whether or not the scan below reaches it.
for (const [f, text] of hostFiles) {
const st = lstatSync(f, { throwIfNoEntry: false });
if (!st?.isFile() || st.nlink !== 1 || readFileSync(f, "utf8") !== text) throw new Error("bundle changed a host file");
}
// A module can write by its own means too, and name only some of its files. writeBundle makes
// bundle/, home/, work/ and tmp/ fresh just before, so every file in them came from this start:
// each is read again, and each must be a regular file with no key in it, raw or decoded, and,
// past the host's own bundle files, no URL but the proxy's (its root or /v1): a harness reads
// files from its HOME and workspace that it does not declare (pi's ~/.pi/agent/models.json).
// No string in them may be a pi command value: pi runs a config value that starts with "!" as a
// shell command, so no decoded string starts with "!" and no text holds a string that does ('"!').
// These fences see what the module left when env() returns, in the run directory only. They do not stop a module that
// hides the key (base64, split across files) or writes after: harness modules are reviewed
// repo code, and the fences catch their mistakes, not their intent.
const proxyUrl = (u) => u === proxy.baseUrl || u === proxy.baseUrl + "/v1";
const SPECIAL = new Set(["http:", "https:", "ws:", "wss:", "ftp:", "file:"]);
// Every URL a string names, as written ("http://", "http:host", "http:\\host") or as a URL
// parser reads the whole string (it drops tabs and newlines anywhere and controls at the ends).
const urlsIn = (text) => {
const out = [...text.matchAll(/[a-z][a-z0-9+.-]*:\/\/[^\s"'<>\\]*/gi), ...text.matchAll(/\b(?:https?|wss?|ftp|file):[^\s"'<>]*/gi)].map(([u]) => u);
if (URL.canParse(text) && SPECIAL.has(new URL(text).protocol)) out.push(text);
return out;
};
const strings = new Map();
for (const d of ["bundle", "home", "work", "tmp"]) {
const root = join(paths.agent, d);
for (const name of readdirSync(root, { recursive: true })) {
const f = join(root, String(name));
const st = lstatSync(f);
if (st.isDirectory()) continue;
if (!st.isFile()) throw new Error("bundle file is not a regular file");
const text = readFileSync(f, "utf8");
if (hostFiles.has(f)) { if (text !== hostFiles.get(f)) throw new Error("bundle changed a host file"); continue; }
const all = decoded(text);
if (all.some(holdsKey)) throw new Error("bundle file holds the key");
if (all.some((t) => urlsIn(t).some((u) => !proxyUrl(u)))) throw new Error("bundle file names another URL");
if (all.some((t) => t.startsWith("!") || t.includes('"!'))) throw new Error("bundle file holds a command value");
strings.set(f, all);
}
}
// 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, read above, with no URL but the proxy's (its root or /v1), raw or decoded, 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 all = strings.get(f);
if (!all) throw new Error("bundle file was not read");
if (all.some((t) => urlsIn(t).length > 0)) 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 +575,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", "work", "tmp"]) 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 +591,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 +616,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 +627,13 @@ 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. This checks its
// shape only: the harness rule ran at the route's admit, through the provider module's route().
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 +767,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 +806,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", "work", "tmp"]) 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 });
+61 -24
View File
@@ -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/<name>)
// 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/
// <module>, 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 },
};
},
};
+28 -15
View File
@@ -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://[email protected]: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
@@ -459,7 +472,7 @@ test("a route that changes after the first check is refused at the binds: nothin
assert.ok(logs.includes("triage host: route refused at the binds: adapter"), logs.join(" | "));
assert.equal(existsSync(join(root, "spawned")), false);
assert.equal(h.running, false);
for (const f of ["session.json", "relay.sock", "bundle"]) assert.equal(existsSync(join(agent, f)), false, f);
for (const f of ["session.json", "relay.sock", "bundle", "home", "work", "tmp"]) assert.equal(existsSync(join(agent, f)), false, f);
const [owner, note, ...rest] = (await h.state()).transcript;
assert.deepEqual([owner.from, note.from, note.in_reply_to, note.text, rest.length], ["owner", "host", owner.id, ROUTE_REFUSED, 0]);
rmSync(join(root, "install", "adapter.sh"));
@@ -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);
+292 -11
View File
@@ -8,10 +8,10 @@
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";
import { chmodSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, symlinkSync, utimesSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "../../bus/src/client.mjs";
@@ -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,50 @@ 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 an OAuth 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" }),
// An API-key group model whose name an OAuth Claude account offers: the catalogue holds it to claude-code.
piOffer({ provider: "other", upstream: "other", model: "anthropic/claude-sonnet-5-5", harnesses: ["claude-code"] }),
]) {
const { route } = await piRoute(t, { offered });
assert.deepEqual(await route.admit(), { ok: false, code: "triage-harness-incompatible" }, offered.model);
}
// Jason 2026-11-19: a Claude model from an API key runs on any harness, so the same name on an
// API-key row that names pi is admitted on pi.
const api = await piRoute(t, { offered: piOffer({ provider: "other", upstream: "other", model: "anthropic/claude-sonnet-5-5" }) });
const b = await api.route.admit();
assert.equal(b.ok, true, b.code);
assert.equal(b.transport.piProvider, "mosaic-proxy-other");
// 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 +542,170 @@ 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, seed = null }) => {
const rootN = scratch();
const r = await rig(t, { bwrap: spy(rootN), module, ...(tokenDir ? { tokenDir } : {}) });
r.setRoute();
seed?.(r);
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`);
// Nothing the refused start wrote keeps the key: not the log, not a file under the run directory.
assertNoKey(k, [["host log", r.logs.join("\n")], ...filesUnder(join(r.dataRoot, "triage", "agent"))]);
};
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" });
// The host gives bundle() the real key, so no file it writes in bundle/, by write() or by its own
// means, may hold the key; a harness file names no URL but the proxy's.
const piWith = (extra) => piLike({ bundle: (o) => { extra(o); return piLike().bundle(o); } });
// write() refuses before the file exists, so the key never rests on disk, not even until the scan.
let keyOnDisk = null;
await fence("a bundle file written through write() holding the key", { module: piWith(({ write, dir, proxy }) => {
try { write("auth.json", JSON.stringify({ apiKey: proxy.key })); } finally { keyOnDisk = existsSync(join(dir, "auth.json")); }
}), fenceName: "harness-bundle" });
assert.equal(keyOnDisk, false, "write() wrote no file that holds the key");
await fence("a bundle file written directly holding the key", { module: piWith(({ dir, proxy }) => writeFileSync(join(dir, "auth.json"), proxy.key, { flag: "wx" })), fenceName: "harness-bundle" });
await fence("a bundle file in a subdirectory holding the key", { module: piWith(({ dir, proxy }) => { mkdirSync(join(dir, "sub")); writeFileSync(join(dir, "sub", "auth.json"), proxy.key, { flag: "wx" }); }), fenceName: "harness-bundle" });
await fence("a harness file that names a second URL", { module: piLike({ bundle: ({ dir, proxy }) => { const f = join(dir.slice(0, -"/bundle".length), "home", "harness.json"); writeFileSync(f, JSON.stringify({ a: proxy.baseUrl, b: "https://api.example.invalid/v1" }), { flag: "wx" }); return { files: { harness: f } }; } }), fenceName: "harness-bundle" });
// Review round 2: the fences read decoded JSON and every file this start made, named or not.
const agentOf = (dir) => dir.slice(0, -"/bundle".length);
const escapedKey = (key) => `\\u${key.charCodeAt(0).toString(16).padStart(4, "0")}${key.slice(1)}`;
const harnessFile = (text) => piLike({ bundle: ({ dir, proxy }) => { const f = join(agentOf(dir), "home", "harness.json"); writeFileSync(f, text(proxy), { flag: "wx" }); return { files: { harness: f } }; } });
await fence("a harness file that names a second URL, JSON-escaped", { module: harnessFile((proxy) => `{"a":${JSON.stringify(proxy.baseUrl)},"baseUrl":"http:\\/\\/api.example.invalid\\/v1"}`), fenceName: "harness-bundle" });
await fence("a harness file that holds the key, JSON-escaped", { module: harnessFile((proxy) => `{"a":${JSON.stringify(proxy.baseUrl)},"apiKey":"${escapedKey(proxy.key)}"}`), fenceName: "harness-bundle" });
await fence("a harness file that holds the key as a JSON key, escaped", { module: harnessFile((proxy) => `{"a":${JSON.stringify(proxy.baseUrl)},"${escapedKey(proxy.key)}":1}`), fenceName: "harness-bundle" });
keyOnDisk = null;
await fence("a bundle file written through write() that holds the key, JSON-escaped", { module: piWith(({ write, dir, proxy }) => {
try { write("auth.json", `{"apiKey":"${escapedKey(proxy.key)}"}`); } finally { keyOnDisk = existsSync(join(dir, "auth.json")); }
}), fenceName: "harness-bundle" });
assert.equal(keyOnDisk, false, "write() wrote no file that holds the key, escaped");
await fence("an unnamed file under home/ that holds the key", { module: piWith(({ dir, proxy }) => writeFileSync(join(agentOf(dir), "home", "auth.json"), proxy.key, { flag: "wx" })), fenceName: "harness-bundle" });
await fence("an unnamed file under work/ that holds the key", { module: piWith(({ dir, proxy }) => writeFileSync(join(agentOf(dir), "work", "stash.txt"), proxy.key, { flag: "wx" })), fenceName: "harness-bundle" });
await fence("an unnamed file under tmp/ that holds the key", { module: piWith(({ dir, proxy }) => writeFileSync(join(agentOf(dir), "tmp", "stash.txt"), proxy.key, { flag: "wx" })), fenceName: "harness-bundle" });
await fence("a harness file with a second hard link", { module: piLike({ bundle: ({ dir, proxy }) => {
const f = join(agentOf(dir), "home", "harness.json");
writeFileSync(f, JSON.stringify({ baseUrl: proxy.baseUrl }), { flag: "wx" });
linkSync(f, join(agentOf(dir), "tmp", "harness.json"));
return { files: { harness: f } };
} }), fenceName: "harness-bundle" });
await fence("a link under home/", { module: piWith(({ dir }) => symlinkSync("/etc/hostname", join(agentOf(dir), "home", "link"))), fenceName: "harness-bundle" });
// A URL a harness reads without "//", or that a URL parser reads once it drops a newline.
const withUrl = (url) => harnessFile((proxy) => JSON.stringify({ a: proxy.baseUrl, baseUrl: url }));
await fence("a harness file that names a URL with no slashes", { module: withUrl("http:api.example.invalid/v1"), fenceName: "harness-bundle" });
await fence("a harness file that names a URL with backslashes", { module: withUrl("http:\\\\api.example.invalid/v1"), fenceName: "harness-bundle" });
await fence("a harness file that names a URL with a newline in its scheme", { module: withUrl("h\nttp:api.example.invalid/v1"), fenceName: "harness-bundle" });
// A file the module does not name, which the harness reads from HOME all the same (pi's
// ~/.pi/agent/models.json), while env() names the proxy.
const undeclared = (url) => piLike({
env: ({ proxy }) => ({ FAKE_OK: "1", ...(proxy ? { ANTHROPIC_API_KEY: proxy.key, ANTHROPIC_BASE_URL: proxy.baseUrl } : {}) }),
bundle: ({ dir, proxy }) => {
mkdirSync(join(agentOf(dir), "home", ".pi", "agent"), { recursive: true });
writeFileSync(join(agentOf(dir), "home", ".pi", "agent", "models.json"), JSON.stringify({ providers: { x: { baseUrl: url(proxy) } } }), { flag: "wx" });
return { files: {} };
},
});
await fence("an unnamed file under home/ that names another URL", { module: undeclared(() => "https://api.example.invalid/v1"), fenceName: "harness-bundle" });
// A file JSON.parse refuses but a looser parser takes (pi: a byte order mark, comments, trailing
// commas) may hold no escape the host cannot decode.
const escapedUrl = (proxy) => `{"a":${JSON.stringify(proxy.baseUrl)},"baseUrl":"http:\\/\\/api.example.invalid\\/v1"}`;
await fence("a harness file with a byte order mark and an escaped URL", { module: harnessFile((proxy) => `\uFEFF${escapedUrl(proxy)}`), fenceName: "harness-bundle" });
await fence("a harness file with a comment and an escaped URL", { module: harnessFile((proxy) => `// models\n${escapedUrl(proxy)}`), fenceName: "harness-bundle" });
await fence("a harness file with a trailing comma and an escaped key", { module: harnessFile((proxy) => `{"a":${JSON.stringify(proxy.baseUrl)},"apiKey":"${escapedKey(proxy.key)}",}`), fenceName: "harness-bundle" });
keyOnDisk = null;
await fence("a bundle file written through write() with a byte order mark and an escaped key", { module: piWith(({ write, dir, proxy }) => {
try { write("auth.json", `\uFEFF{"apiKey":"${escapedKey(proxy.key)}"}`); } finally { keyOnDisk = existsSync(join(dir, "auth.json")); }
}), fenceName: "harness-bundle" });
assert.equal(keyOnDisk, false, "write() refuses a loose file with an escape before it exists");
// session/, kept across starts, is not the module's to write: what it adds goes, and the next
// start takes a fresh session.
await fence("a file written under session/", { module: piWith(({ dir, proxy }) => writeFileSync(join(agentOf(dir), "session", "auth.txt"), proxy.key, { flag: "wx" })), fenceName: "harness-bundle" });
// A URL with no slashes inside a longer line, which no URL parser reads whole.
await fence("a plain-text harness file that names a URL with no slashes in a line", { module: harnessFile((proxy) => `${proxy.baseUrl}\nbaseUrl=http:api.example.invalid/v1\n`), fenceName: "harness-bundle" });
// The host's own bundle files are the host's: a module that rewrites one is refused.
await fence("a module that rewrites the host's policy file", { module: piWith(({ dir, proxy }) => writeFileSync(join(dir, "policy.json"), `{"k":"${escapedKey(proxy.key)}"}`)), fenceName: "harness-bundle" });
// Review round 4. pi runs a config value that starts with "!" as a shell command, in a parsed
// file or one only pi's looser parser takes.
await fence("a harness file with a command value", { module: harnessFile((proxy) => JSON.stringify({ a: proxy.baseUrl, apiKey: "!env" })), fenceName: "harness-bundle" });
await fence("a harness file with a command header and a comment", { module: harnessFile((proxy) => `// models\n{"a":${JSON.stringify(proxy.baseUrl)},"headers":{"x":"!env"}}`), fenceName: "harness-bundle" });
await fence("an unnamed file under home/ with a command value", { module: undeclared(() => "!env"), fenceName: "harness-bundle" });
await fence("a harness file with a JSON-escaped command value", { module: harnessFile((proxy) => `{"a":${JSON.stringify(proxy.baseUrl)},"apiKey":"\\u0021env"}`), fenceName: "harness-bundle" });
// A host bundle file the module deletes or replaces is refused, though the scan never reaches it.
await fence("a module that deletes the host's prompt", { module: piWith(({ dir }) => rmSync(join(dir, "prompt.md"))), fenceName: "harness-bundle" });
await fence("a module that hard-links the host's prompt", { module: piWith(({ dir }) => linkSync(join(dir, "prompt.md"), join(agentOf(dir), "tmp", "prompt.md"))), fenceName: "harness-bundle" });
// A FIFO there has one link, and a read of it with no writer would block: the host must see it is
// not a regular file before it reads.
await fence("a module that replaces the host's prompt with a FIFO", { module: piWith(({ dir }) => { rmSync(join(dir, "prompt.md")); execFileSync("mkfifo", [join(dir, "prompt.md")]); }), fenceName: "harness-bundle" });
await fence("a module that replaces the host's policy with a directory", { module: piWith(({ dir }) => { rmSync(join(dir, "policy.json")); mkdirSync(join(dir, "policy.json")); }), fenceName: "harness-bundle" });
// A session/ file rewritten in place at the same size, with its times set back, still shows: its
// ctime moved.
const seeded = (r) => { const d = join(r.dataRoot, "triage", "agent", "session"); mkdirSync(d, { recursive: true, mode: 0o700 }); writeFileSync(join(d, "history.jsonl"), "x".repeat(512), { mode: 0o600 }); };
await fence("a session/ file rewritten with its times set back", { seed: seeded, module: piWith(({ dir, proxy }) => {
const f = join(agentOf(dir), "session", "history.jsonl");
const st = statSync(f);
writeFileSync(f, (proxy.key + "x".repeat(512)).slice(0, 512));
utimesSync(f, st.atimeMs / 1000, st.mtimeMs / 1000);
}), fenceName: "harness-bundle" });
// The unnamed models.json with only the proxy's URL launches.
{
const rootN = scratch();
const p = await rig(t, { bwrap: spy(rootN), module: undeclared((proxy) => `${proxy.baseUrl}/v1`) });
p.setRoute();
issue(p.configDir);
await p.h.message("hello");
await waitFor(() => existsSync(join(rootN, "spawned")));
await p.h.stop();
}
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 +788,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();