diff --git a/bin/muse-proxy.mjs b/bin/muse-proxy.mjs index dc8b56b7e7..d27363cb28 100644 --- a/bin/muse-proxy.mjs +++ b/bin/muse-proxy.mjs @@ -1048,6 +1048,25 @@ const contentPartsForInput = (content) => { return content ? JSON.stringify(content) : ''; }; +// Meta's subscription-key gate rejects requests whose function tools carry +// the well-known agent-harness shell names (`shell`, `bash`) with 403 +// client_attestation_denied — verified live 2026-10-30 by bisecting a real +// opencode request body: every other tool, the instructions, and all client +// headers pass unchanged, while `run_command` / `terminal` pass. The direct +// path therefore presents those tools under alias names and translates the +// model's tool calls back on the way out. The CLI door never needed this — +// the CLI sends its own toolset. +const DIRECT_TOOL_ALIASES = { shell: 'run_command', bash: 'terminal' }; +const DIRECT_TOOL_NAMES = Object.fromEntries( + Object.entries(DIRECT_TOOL_ALIASES).map(([client, alias]) => [alias, client]) +); +const directToolAlias = (name, taken) => { + const alias = DIRECT_TOOL_ALIASES[name]; + if (!alias || (taken && taken.has(alias))) return name; + return alias; +}; +const clientToolName = (name) => DIRECT_TOOL_NAMES[name] || name; + const buildResponsesRequest = (body, baseModel, effort) => { const messages = Array.isArray(body.messages) ? body.messages : []; const instructions = messages @@ -1055,6 +1074,8 @@ const buildResponsesRequest = (body, baseModel, effort) => { .map(messageText) .filter(Boolean) .join('\n\n'); + const takenNames = new Set((Array.isArray(body.tools) ? body.tools : []) + .map(t => t && t.function && t.function.name).filter(Boolean)); const input = []; for (const m of messages) { if (m.role === 'system') continue; @@ -1067,7 +1088,7 @@ const buildResponsesRequest = (body, baseModel, effort) => { input.push({ type: 'function_call', call_id: tc.id, - name: tc.function ? tc.function.name : undefined, + name: tc.function ? directToolAlias(tc.function.name, takenNames) : undefined, arguments: (tc.function && tc.function.arguments) || '' }); } @@ -1085,13 +1106,13 @@ const buildResponsesRequest = (body, baseModel, effort) => { if (instructions) out.instructions = instructions; if (Array.isArray(body.tools) && body.tools.length > 0) { out.tools = body.tools.map(t => (t && t.type === 'function' && t.function) - ? { type: 'function', name: t.function.name, description: t.function.description, parameters: t.function.parameters } + ? { type: 'function', name: directToolAlias(t.function.name, takenNames), description: t.function.description, parameters: t.function.parameters } : t); } if (body.tool_choice !== undefined) { const tc = body.tool_choice; if (typeof tc === 'string') out.tool_choice = tc; - else if (tc && tc.type === 'function' && tc.function && tc.function.name) out.tool_choice = { type: 'function', name: tc.function.name }; + else if (tc && tc.type === 'function' && tc.function && tc.function.name) out.tool_choice = { type: 'function', name: directToolAlias(tc.function.name, takenNames) }; else out.tool_choice = tc; } const maxOut = body.max_completion_tokens !== undefined && body.max_completion_tokens !== null @@ -1118,7 +1139,7 @@ const extractResponsesOutput = (respBody) => { toolCalls.push({ id: item.call_id || item.id, type: 'function', - function: { name: item.name, arguments: item.arguments || '' } + function: { name: clientToolName(item.name), arguments: item.arguments || '' } }); } } @@ -1212,7 +1233,7 @@ const streamResponsesAsChat = async (response, { res, chatId, created, fullName index, id: evt.item.call_id || evt.item.id, type: 'function', - function: { name: evt.item.name || '', arguments: '' } + function: { name: clientToolName(evt.item.name || ''), arguments: '' } }] }); } else if (type === 'response.function_call_arguments.delta') { @@ -1296,8 +1317,14 @@ const tryDirectChat = async ({ body, baseModel, effort, fullName, stream, res, c } const response = outcome.response; if (!response.ok) { - // Meta answered: pass the response through as-is, whatever the status. const text = await response.text().catch(() => ''); + if (response.status === 403 && text.includes('client_attestation_denied')) { + // The attestation fingerprint moved (a tool name newly blocked): + // degrade to the CLI door instead of failing the seat outright. + console.warn('[muse-proxy] direct path attestation denied; falling back to the CLI door'); + return false; + } + // Meta answered: pass the response through as-is, whatever the status. res.writeHead(response.status, { 'Content-Type': response.headers.get('content-type') || 'application/json' }); diff --git a/bin/muse-proxy.test.mjs b/bin/muse-proxy.test.mjs index 583cb13d4c..4a78a1da6b 100644 --- a/bin/muse-proxy.test.mjs +++ b/bin/muse-proxy.test.mjs @@ -514,6 +514,11 @@ const startMetaStub = async (mode) => { return; } if (req.url === '/v1/responses') { + if (mode === 'attest') { + res.writeHead(403, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: { code: 'client_attestation_denied', message: 'This API key is billed to a Muse Code subscription. Muse Code subscriptions cover requests from the Muse Code client only.', type: 'permission_error' } })); + return; + } if (mode === '429') { res.writeHead(429, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: 'Subscription quota exhausted', type: 'rate_limit_error' } })); @@ -528,11 +533,13 @@ const startMetaStub = async (mode) => { try { parsed = JSON.parse(body || '{}'); } catch { /* keep empty */ } if (parsed.stream) { res.writeHead(200, { 'Content-Type': 'text/event-stream' }); - res.end(RESPONSES_SSE_FIXTURE); + res.end(mode === 'shell' ? RESPONSES_SSE_FIXTURE.replaceAll('get_weather', 'run_command') : RESPONSES_SSE_FIXTURE); return; } res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify(RESPONSES_FIXTURE)); + res.end(mode === 'shell' + ? JSON.stringify(RESPONSES_FIXTURE).replaceAll('get_weather', 'run_command') + : JSON.stringify(RESPONSES_FIXTURE)); return; } res.writeHead(404, { 'Content-Type': 'application/json' }); @@ -698,6 +705,42 @@ test('direct mode: upstream 429 passes through and never falls back to exec (#27 assert.ok(!stubLog.includes('--prompt-file'), 'no muse exec invocation after Meta answered'); }); +test('direct mode: the shell tool is aliased upstream and its calls translate back', async (t) => { + const meta = await startMetaStub('shell'); + t.after(() => meta.close()); + const { port, stop, home } = await startProxy({ MUSE_PROXY_META_API_BASE: meta.url }); + t.after(stop); + writeStubAuth(home); + + const plain = JSON.parse(await chat(port, { + messages: [ + { role: 'user', content: 'run echo hi' }, + { role: 'assistant', content: null, tool_calls: [{ id: 'call_0', type: 'function', function: { name: 'shell', arguments: '{"command":"echo hi"}' } }] }, + { role: 'tool', tool_call_id: 'call_0', content: 'hi' }, + { role: 'user', content: 'what did it print?' } + ], + tools: [{ type: 'function', function: { name: 'shell', description: 'Run a shell command', parameters: { type: 'object', properties: { command: { type: 'string' } } } } }] + })); + const upstream = JSON.parse(meta.requests.filter(r => r.url === '/v1/responses')[0].body); + assert.deepEqual(upstream.tools.map(x => x.name), ['run_command'], 'the shell tool is presented upstream under its alias'); + const historyCall = upstream.input.find(i => i.type === 'function_call'); + assert.equal(historyCall.name, 'run_command', 'history tool calls are aliased too'); + assert.equal(plain.choices[0].message.tool_calls[0].function.name, 'shell', 'the model call is translated back to the client name'); +}); + +test('direct mode: an attestation denial falls back to the CLI door', async (t) => { + const meta = await startMetaStub('attest'); + t.after(() => meta.close()); + const { port, stop, home, logFile } = await startProxy({ MUSE_PROXY_META_API_BASE: meta.url }); + t.after(stop); + writeStubAuth(home); + + const plain = JSON.parse(await chat(port, { messages: [{ role: 'user', content: 'hi' }] })); + assert.equal(plain.choices[0].message.content, 'Hello world', 'exec stub answer proves the CLI fallback ran'); + const stubLog = fs.existsSync(logFile) ? fs.readFileSync(logFile, 'utf-8') : ''; + assert.ok(stubLog.includes('--prompt-file'), 'the CLI door took over after the denial'); +}); + test('direct mode: /v1/responses passes the body through verbatim, swapping only Authorization (#2753)', async (t) => { const meta = await startMetaStub('echo'); t.after(() => meta.close());