joycode 0.2.7: an empty or unterminated Responses stream is an error, not a silent success (liangmouse, #720)

JoyCode's Responses SSE is checked one event at a time, never buffered whole:

- a JoyCode FAILED_RESPONSE envelope (code non-zero) becomes a Responses
  "error" event that keeps its code, message and cause; anything after it
  is dropped;
- a response.completed with no output seen and none in its output list
  becomes error empty_response (an unreadable completed frame too);
- EOF with no completed, incomplete, failed or error event becomes error
  incomplete_response;
- text, refusals, tool and *_call items, reasoning with a summary or
  encrypted content, and explicit incomplete/failed pass byte for byte,
  CRLF included. A completed with no [DONE] after it passes as it came.

magpie's gateway reads a Responses "error" event as a failed turn and owns
the retries; the plugin sends no request of its own. Tests that called
t.mock.method(fetch) several times in one test leaked the mock under Bun
(copilot's tests got "Body already used"); each fixture is its own test now.

Run at the reviewed head 7b74d0c (main d9a6c45 plus this PR, Bun 1.4.4
38afee6c8, CI's version):
- Linux (WSL magpie-desktop), fresh HOME: bun test 5501 pass, 18 skip,
  0 fail; bun scripts/check.mjs ok.
- macOS (mote): bun test packages/joycode 94/94; node --test
  packages/joycode/*.test.mjs 100/100; joycode chat + copilot in one Bun
  process 104 pass, 0 fail (53bf359 alone: 4 fails, "Body already used").
- Broken by hand: index.mjs back to 0.2.6 with the new tests, the 4
  behaviour tests go red; payload.response without ?. , the malformed
  completed test goes red (Bun and Node).
- Probes through loader.fetch: completed without [DONE] or a trailing
  blank line, completed then [DONE], and a double-wrapped full stream pass
  untouched; [DONE] alone, a comment alone, created + [DONE] and a delta
  cut mid-line each end in one incomplete_response.

Not tried: a real JoyCode account (the author ran GPT-6 Astra through
magpie 0.1.2392).
This commit is contained in:
liangmouse
2026-11-04 19:18:48 +08:00
committed by GitHub
parent d677c436d3
commit 16769b21dc
5 changed files with 314 additions and 7 deletions
+172 -2
View File
@@ -27,6 +27,15 @@ async function request(t, response, {
function chunks(parts) {
return new ReadableStream({ start(c) { for (const p of parts) c.enqueue(new TextEncoder().encode(p)); c.close() } })
}
function responseEvents(text) {
return text.split(/\r?\n\r?\n/).flatMap(frame => {
const data = frame.split(/\r?\n/)
.filter(line => line.startsWith('data:'))
.map(line => line.slice(5).replace(/^ /, ''))
if (!data.length) return []
try { return [JSON.parse(data.join('\n'))] } catch { return [] }
})
}
test('HTTP 200 raw gateway denial becomes actionable error, not empty SSE', async t => {
const denial = () => new Response(chunks([' {"error":', '{"code":"AI_GRAY_ACCESS_DENIED","message":"访问受限,请联系管理员开通"}}']), { headers: { 'content-type': 'text/event-stream' } })
const r = await request(t, denial)
@@ -122,7 +131,10 @@ for (const [name, input, payload, functionId, npmShape] of [
]) {
test(`${name} requests are signed on their own gateway and keep their shape`, async t => {
let seen
const r = await request(t, new Response('data: [DONE]\n\n', {
const stream = name === 'Responses'
? 'data: {"type":"response.incomplete","response":{"status":"incomplete","output":[{"type":"reasoning","summary":[]}]}}\n\n'
: 'data: [DONE]\n\n'
const r = await request(t, new Response(stream, {
headers: { 'content-type': 'text/event-stream' },
}), {
input,
@@ -141,7 +153,7 @@ for (const [name, input, payload, functionId, npmShape] of [
assert.equal(seen.init.headers.get('x-api-key'), null)
assert.equal(seen.init.headers.get('ptkey'), 'fake-key')
assert.equal(r.status, 200)
assert.equal(await r.text(), 'data: [DONE]\n\n')
assert.equal(await r.text(), stream)
})
}
@@ -392,6 +404,164 @@ test('Responses double-wrapped SSE is normalized across chunk boundaries', async
].join(''))
})
test('Responses malformed completed payload emits one empty_response without throwing', async t => {
const r = await request(t, new Response('event: response.completed\ndata: oops\n\n', {
headers: { 'content-type': 'text/event-stream' },
}), {
input: 'https://api-ai.jd.com/v1/responses',
payload: { model: 'GPT-6 Astra', input: 'OK', stream: true },
})
assert.equal(r.status, 200)
let body
await assert.doesNotReject(async () => { body = await r.text() })
const events = responseEvents(body)
assert.equal(events.length, 1)
assert.equal(events[0].type, 'error')
assert.equal(events[0].code, 'empty_response')
})
test('Responses converts a double-wrapped JoyCode failure and suppresses later success', async t => {
const failure = {
code: 'QUOTA_EXHAUSTED',
message: 'credits exhausted',
cause: { detail: 'billing limit' },
status: 'FAILED_RESPONSE',
}
const upstreamText = [
`data: data: ${JSON.stringify(failure)}\n\n`,
`data: data: ${JSON.stringify({ type: 'response.completed', response: { output: [{ type: 'message', content: [{ type: 'output_text', text: 'unexpected' }] }] } })}\n\n`,
].join('')
const split = upstreamText.indexOf('credits') + 3
const r = await request(t, new Response(chunks([
upstreamText.slice(0, split),
upstreamText.slice(split),
]), { headers: { 'content-type': 'text/event-stream' } }), {
input: 'https://api-ai.jd.com/v1/responses',
payload: { model: 'GPT-6 Astra', input: 'OK', stream: true },
})
const body = await r.text()
const events = responseEvents(body)
assert.equal(r.status, 200)
assert.equal(events.length, 1)
assert.equal(events[0].type, 'error')
assert.equal(events[0].code, failure.code)
assert.equal(events[0].cause.detail, failure.cause.detail)
assert.equal(events.some(event => event.type === 'response.completed'), false)
})
// Separate contexts avoid stacked fetch mocks leaking under Bun's node:test adapter.
for (const [label, output] of [['no output', []], ['empty reasoning', [{ type: 'reasoning', summary: [] }]]]) {
test(`Responses converts an empty completion into empty_response: ${label}`, async t => {
const completed = { type: 'response.completed', response: { status: 'completed', output } }
const r = await request(t, new Response(`data: ${JSON.stringify(completed)}\n\n`, {
headers: { 'content-type': 'text/event-stream' },
}), {
input: 'https://api-ai.jd.com/v1/responses',
payload: { model: 'GPT-6 Astra', input: 'OK', stream: true },
})
const body = await r.text()
const events = responseEvents(body)
assert.equal(events.length, 1)
assert.equal(events[0].type, 'error')
assert.equal(events[0].code, 'empty_response')
assert.equal(body.includes('response.completed'), false)
})
}
test('Responses reports incomplete_response when EOF has no terminal event', async t => {
const delta = { type: 'response.output_text.delta', delta: 'partial' }
const upstreamText = `data: ${JSON.stringify(delta)}\n\n`
const r = await request(t, new Response(upstreamText, {
headers: { 'content-type': 'text/event-stream' },
}), {
input: 'https://api-ai.jd.com/v1/responses',
payload: { model: 'GPT-6 Astra', input: 'OK', stream: true },
})
const body = await r.text()
const events = responseEvents(body)
assert.equal(body.startsWith(upstreamText), true)
assert.equal(events.at(-1).type, 'error')
assert.equal(events.at(-1).code, 'incomplete_response')
})
test('Responses preserves standard CRLF events byte for byte', async t => {
const upstreamText = 'event: response.incomplete\r\ndata: {"type":"response.incomplete","response":{"status":"incomplete","output":[]}}\r\n\r\n'
const r = await request(t, new Response(chunks([upstreamText.slice(0, 19), upstreamText.slice(19)]), {
headers: { 'content-type': 'text/event-stream' },
}), {
input: 'https://api-ai.jd.com/v1/responses',
payload: { model: 'GPT-6 Astra', input: 'OK', stream: true },
})
assert.equal(await r.text(), upstreamText)
})
{
const cases = [
['text delta', [
{ type: 'response.output_text.delta', delta: 'hello' },
{ type: 'response.completed', response: { output: [] } },
]],
['text and tool deltas', [
{ type: 'response.output_text.delta', delta: 'hello' },
{ type: 'response.function_call_arguments.delta', item_id: 'fc_1', delta: '{}' },
{ type: 'response.completed', response: { output: [] } },
]],
['function call', [
{ type: 'response.completed', response: { output: [{ type: 'function_call', call_id: 'call_1', name: 'lookup', arguments: '{}' }] } },
]],
['completed output text', [
{ type: 'response.completed', response: { output: [{ type: 'message', content: [{ type: 'output_text', text: 'from output' }] }] } },
]],
['refusal', [
{ type: 'response.completed', response: { output: [{ type: 'message', content: [{ type: 'refusal', refusal: 'cannot help' }] }] } },
]],
['search call', [
{ type: 'response.completed', response: { output: [{ type: 'web_search_call', id: 'search_1', status: 'completed', action: { query: 'weather' } }] } },
]],
['image call', [
{ type: 'response.completed', response: { output: [{ type: 'image_generation_call', id: 'image_1', status: 'completed', result: 'encoded-image' }] } },
]],
['reasoning incomplete', [
{ type: 'response.incomplete', response: { status: 'incomplete', output: [{ type: 'reasoning', summary: [] }] } },
]],
['failed terminal', [
{ type: 'response.failed', response: { status: 'failed', output: [] } },
]],
['reasoning completed item', [
{ type: 'response.completed', response: { output: [{ type: 'reasoning', summary: [{ type: 'summary_text', text: 'internal' }] }] } },
]],
['encrypted reasoning', [
{ type: 'response.completed', response: { output: [{ type: 'reasoning', encrypted_content: 'encrypted-reasoning' }] } },
]],
]
for (const [label, events] of cases) {
test(`Responses preserves output bytes: ${label}`, async t => {
const upstreamText = events.map(event => `data: ${JSON.stringify(event)}\n\n`).join('')
const r = await request(t, new Response(upstreamText, {
headers: { 'content-type': 'text/event-stream' },
}), {
input: 'https://api-ai.jd.com/v1/responses',
payload: { model: 'GPT-6 Astra', input: 'OK', stream: true },
})
assert.equal(await r.text(), upstreamText, label)
})
}
}
test('Responses does not append another error after an upstream error event', async t => {
const upstreamText = 'data: {"type":"error","code":"upstream_failure","message":"failed"}\n\n'
const r = await request(t, new Response(upstreamText, {
headers: { 'content-type': 'text/event-stream' },
}), {
input: 'https://api-ai.jd.com/v1/responses',
payload: { model: 'GPT-6 Astra', input: 'OK', stream: true },
})
const body = await r.text()
const errors = responseEvents(body).filter(event => event.type === 'error')
assert.equal(errors.length, 1)
assert.equal(body, upstreamText)
})
test('Responses drops unstored reasoning ids but keeps inline reasoning and other ids', async t => {
let sent
const payload = {