diff --git a/bin/muse-proxy.mjs b/bin/muse-proxy.mjs index d676970e59..32f0d28b38 100644 --- a/bin/muse-proxy.mjs +++ b/bin/muse-proxy.mjs @@ -399,10 +399,24 @@ const server = http.createServer((req, res) => { const failed = (code !== 0 && fullContent.length === 0) || (terminalState && terminalState !== 'completed' && fullContent.length === 0); if (failed) { const detail = (stderrTail.trim().split('\n').pop() || `muse exec exited with code ${code}`).trim(); + const errorBody = { error: { message: `Muse execution failed: ${detail}` } }; + if (stream) { + // The 200 + text/event-stream headers are already on the wire, + // so a bare JSON body would be invisible to an SSE client + // parsing `data:` lines — it would see an empty, successful + // stream (#670). Frame the error as an SSE event and close + // the stream with [DONE] instead. + if (!res.writableEnded) { + res.write(`data: ${JSON.stringify(errorBody)}\n\n`); + res.write('data: [DONE]\n\n'); + } + res.end(); + return; + } if (!res.headersSent) { res.writeHead(500, { 'Content-Type': 'application/json' }); } - res.end(JSON.stringify({ error: { message: `Muse execution failed: ${detail}` } })); + res.end(JSON.stringify(errorBody)); return; } diff --git a/bin/muse-proxy.test.mjs b/bin/muse-proxy.test.mjs index e9f068ec7d..36a77e36a0 100644 --- a/bin/muse-proxy.test.mjs +++ b/bin/muse-proxy.test.mjs @@ -25,6 +25,10 @@ for a in "$@"; do if [ "$prev" = "--prompt-file" ]; then prompt="$a"; fi prev="$a" done +if grep -q FAILME "$prompt" 2>/dev/null; then + echo "stub muse: auth expired" >&2 + exit 3 +fi if grep -q DIVERGE "$prompt" 2>/dev/null; then printf '%s\\n' '{"payload_type":"run.output.delta","payload":{"text":"abc"}}' printf '%s\\n' '{"payload_type":"run.terminal.completed","payload":{"terminal":"completed","text":"xyz-longer"}}' @@ -112,6 +116,35 @@ test('streaming emits the terminal text tail the deltas missed (#484)', async (t assert.equal(finish, 'stop'); }); +test('execution failure: stream gets an SSE-framed error + [DONE], non-stream gets 500 (#670)', async (t) => { + const { port, stop } = await startProxy(); + t.after(stop); + const messages = [{ role: 'user', content: 'please FAILME now' }]; + + const plainRes = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ messages }) + }); + assert.equal(plainRes.status, 500); + const plainBody = JSON.parse(await plainRes.text()); + assert.match(plainBody.error.message, /Muse execution failed: stub muse: auth expired/); + + const streamRes = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ messages, stream: true }) + }); + assert.equal(streamRes.status, 200); + assert.match(streamRes.headers.get('content-type'), /text\/event-stream/); + const sse = await streamRes.text(); + const dataLines = sse.split('\n').filter(l => l.startsWith('data: ')); + assert.ok(dataLines.length >= 2, `expected a framed error event and [DONE], got: ${JSON.stringify(sse)}`); + const errorEvent = JSON.parse(dataLines[0].slice('data: '.length)); + assert.match(errorEvent.error.message, /Muse execution failed: stub muse: auth expired/); + assert.equal(dataLines[dataLines.length - 1], 'data: [DONE]'); +}); + test('divergent terminal text: non-stream uses it, stream keeps the deltas already sent', async (t) => { const { port, stop } = await startProxy(); t.after(stop);