From b15c48944ba71134fe210120e1f4698533c538e6 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 10:59:16 -0600 Subject: [PATCH] fix(muse-proxy): surface failed/incomplete direct Responses bodies as errors (#2780) Non-streaming direct translation ignored respBody.status, reporting failed bodies (even with partial output) and non-max incomplete bodies as 200 chat completions with finish_reason stop. Failed bodies now return a 500 carrying the upstream error message; incomplete bodies whose reason is not max_output_tokens return a 500 naming the reason. Completed and incomplete/max_output_tokens behavior is unchanged. Regression coverage via stub-Meta modes failed-empty, failed-partial, incomplete-filter plus an incomplete-max control. --- bin/muse-proxy.mjs | 19 +++++++++++++ bin/muse-proxy.test.mjs | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/bin/muse-proxy.mjs b/bin/muse-proxy.mjs index d69550c9e8..28fbca1df6 100644 --- a/bin/muse-proxy.mjs +++ b/bin/muse-proxy.mjs @@ -1432,6 +1432,25 @@ const tryDirectChat = async ({ body, baseModel, effort, fullName, stream, res, c writeJsonBody(res, 502, { error: { message: 'Direct Meta route returned an unreadable response body' } }); return true; } + const failedMessage = respBody && respBody.status === 'failed' + ? ((respBody.error && respBody.error.message) || 'Direct Meta Responses request failed') + : null; + if (failedMessage) { + // A failed upstream body must surface as an error even when output + // carries partial text (#2780): never report it as a 200 completion. + writeJsonBody(res, 500, { error: { message: failedMessage } }); + return true; + } + const incompleteReason = respBody && respBody.status === 'incomplete' && + (!respBody.incomplete_details || respBody.incomplete_details.reason !== 'max_output_tokens') + ? ((respBody.incomplete_details && respBody.incomplete_details.reason) || 'unknown') + : null; + if (incompleteReason) { + // An incomplete body that was not cut off by max_output_tokens is an + // upstream failure, not a finish_reason stop (#2780). + writeJsonBody(res, 500, { error: { message: `Direct Meta response incomplete: ${incompleteReason}` } }); + return true; + } writeJsonBody(res, 200, responsesToChatCompletion(respBody, { chatId, created, fullName })); return true; } diff --git a/bin/muse-proxy.test.mjs b/bin/muse-proxy.test.mjs index 2ee3fd88d8..7bbe88c7c8 100644 --- a/bin/muse-proxy.test.mjs +++ b/bin/muse-proxy.test.mjs @@ -601,6 +601,31 @@ const startMetaStub = async (mode) => { res.end(JSON.stringify({ received: JSON.parse(body || '{}'), authorization: req.headers.authorization })); return; } + if (mode === 'failed-empty' || mode === 'failed-partial') { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + id: 'resp_failed', + object: 'response', + status: 'failed', + error: { message: 'stub upstream boom', type: 'server_error' }, + output: mode === 'failed-partial' + ? [{ type: 'message', id: 'msg_p', role: 'assistant', content: [{ type: 'output_text', text: 'partial text that must not mask the failure' }] }] + : [] + })); + return; + } + if (mode === 'incomplete-filter' || mode === 'incomplete-max') { + const reason = mode === 'incomplete-max' ? 'max_output_tokens' : 'content_filter'; + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + id: 'resp_incomplete', + object: 'response', + status: 'incomplete', + incomplete_details: { reason }, + output: [{ type: 'message', id: 'msg_i', role: 'assistant', content: [{ type: 'output_text', text: 'cut off but usable' }] }] + })); + return; + } let parsed = {}; try { parsed = JSON.parse(body || '{}'); } catch { /* keep empty */ } if (parsed.stream) { @@ -728,6 +753,44 @@ test('direct mode: one exchange mints the key, chat is translated to Responses a assert.equal(health.direct.subsActive, true); }); +test('direct mode: failed/incomplete upstream bodies are 500 errors, never a 200 stop (#2780)', async (t) => { + const postChat = (port) => fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model: 'muse-spark-1.3', messages: [{ role: 'user', content: 'hi' }] }) + }); + for (const [mode, match] of [ + ['failed-empty', /stub upstream boom/], + ['failed-partial', /stub upstream boom/], + ['incomplete-filter', /incomplete: content_filter/], + ]) { + const meta = await startMetaStub(mode); + const { port, stop, home } = await startProxy({ MUSE_PROXY_META_API_BASE: meta.url }); + try { + writeStubAuth(home); + const plainRes = await postChat(port); + assert.equal(plainRes.status, 500, `${mode}: non-stream must be a 500, not a 200 'stop'`); + const plainBody = JSON.parse(await plainRes.text()); + assert.match(plainBody.error.message, match, `${mode}: error must carry the upstream cause`); + assert.ok(!('choices' in plainBody), `${mode}: no masked completion may be present`); + } finally { + stop(); + meta.close(); + } + } + + const meta = await startMetaStub('incomplete-max'); + t.after(() => meta.close()); + const { port, stop, home } = await startProxy({ MUSE_PROXY_META_API_BASE: meta.url }); + t.after(stop); + writeStubAuth(home); + const maxRes = await postChat(port); + assert.equal(maxRes.status, 200, 'incomplete/max_output_tokens stays a 200'); + const maxBody = JSON.parse(await maxRes.text()); + assert.equal(maxBody.choices[0].finish_reason, 'length'); + assert.equal(maxBody.choices[0].message.content, 'cut off but usable'); +}); + test('direct mode: Responses SSE is translated into chat chunks with tool calls (#2753)', async (t) => { const meta = await startMetaStub('chat'); t.after(() => meta.close());