From 8215da2d2ed6b4ce3164d23c8f3376d2d658a387 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 09:13:10 -0500 Subject: [PATCH] fix(muse-proxy): alias the shell tool on the direct path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since #2753 the muse seat has failed every real agent request with 403 client_attestation_denied: Meta's subscription-key gate rejects request bodies whose function tools carry the well-known harness shell names (shell, bash). Verified live by bisecting a captured opencode request — every other tool, the instructions, and all client headers pass; the shell tool alone trips it, and aliases (run_command, terminal) pass. The direct path now presents shell as run_command (bash as terminal) in tool definitions, history calls, and tool_choice, and translates the model's calls back in both the buffered and streaming converters. An attestation denial that still occurs (the fingerprint moved) now falls back to the CLI door instead of failing the seat. The /v1/responses passthrough still forwards verbatim and will surface the same denial to any future native-provider client; aliasing there is a follow-up if that route gains a client. --- bin/muse-proxy.mjs | 39 ++++++++++++++++++++++++++++------ bin/muse-proxy.test.mjs | 47 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 8 deletions(-) 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());