Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,12 @@ export async function pipePluginChatWithStrip(
// swallows the original response object, hence optional.
upstreamMeta?: UpstreamMeta,
reply?: HandoffReply,
// #5958: how the double-degenerate terminal (#870) is shaped — threaded from
// ProxyOptions.streamErrorShape so this lane obeys the same policy as every
// other stream-error site. Omitted → protocol-native error frame (the global
// default); hosts that need the legacy completed-turn shape opt back in with
// streamErrorShape:"completion".
streamErrorShape?: "protocol" | "completion",
): Promise<void> {
let reader = stream.getReader();
let decoder = new TextDecoder("utf-8");
Expand Down Expand Up @@ -2795,11 +2801,16 @@ export async function pipePluginChatWithStrip(
// model that produced nothing and the session reads as idle while it is
// dead, so the client gets an error the host would never surface (#870).
log?.("[plugin] degenerate terminal turn again after the retry; emitting an in-band error (#870)");
// #870 deliberately chose a COMPLETED turn carrying the error text (visible to the host); keep the legacy shape here regardless of the global streamErrorShape default.
// #5958: the terminal shape follows the configured streamErrorShape
// (default protocol → a native error frame the host records as a
// failure) instead of the old hard-wired completed-turn shape, which
// let hosts like DSH book an unfinished turn as stop/completed.
// streamErrorShape:"completion" restores the #870 legacy shape for
// hosts that need the error text inside a well-formed turn (#2689).
// #2689: hand the emitter the client-known stream state — the error
// block opens at the next free index (blocksForwarded), and no
// message_start is synthesized when the first attempt already sent one.
emitStreamError(res, protocol, "the turn degenerated again after the continuation nudge", undefined, "completion", { blockIndex: blocksForwarded, messageStarted: sawMessageStart });
emitStreamError(res, protocol, "the turn degenerated again after the continuation nudge", undefined, streamErrorShape, { blockIndex: blocksForwarded, messageStarted: sawMessageStart });
return true;
}
// A turn the model left genuinely bare — no thought, no stripped echo,
Expand Down
2 changes: 2 additions & 0 deletions src/server/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,7 @@ async function forwardCaptured(
wireBodyText,
upstreamMeta,
reply,
opts.streamErrorShape,
);
}
} else {
Expand Down Expand Up @@ -2158,6 +2159,7 @@ async function forwardCaptured(
undefined,
upstreamMeta,
reply,
opts.streamErrorShape,
);
}
} else if (
Expand Down
7 changes: 5 additions & 2 deletions tests/issue2303-draft-tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,13 @@ test("#2303 chat: a draft-tail RETRY degenerates into the in-band error (#870 pa
const refetch = () => Promise.resolve(streamOf(draftTailTurn()));
await pipePluginChatWithStrip(streamOf(draftTailTurn()), makeRes(out), "openai", makeSession(), undefined, refetch);
const text = out.join("");
// #5958: default streamErrorShape="protocol" → a wire-native error frame, not
// the legacy completed-turn shape (no finish_reason:"stop" success terminal).
assert.ok(
text.includes("[ACP] stream error"),
"the client is told, rather than left with a second silent draft tail",
text.includes('"code":"stream_error"'),
"the client is told via a native error frame, rather than left with a second silent draft tail",
);
assert.ok(!text.includes('"finish_reason":"stop"'), "an exhausted retry is not serialized as a successful completion");
});

// ---------- plugin responses pipe ----------
Expand Down
51 changes: 43 additions & 8 deletions tests/plugin-empty-turn-retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,33 @@ test("plugin chat emits an in-band error when the retry degenerates too", async
const refetch = () => Promise.resolve(streamOf([chatChunk({ role: "assistant" }), chatStop(), DONE]));
await pipePluginChatWithStrip(streamOf(unclosedEchoTurn()), makeRes(out), "openai", makeSession(), undefined, refetch);
const text = out.join("");
assert.ok(
text.includes("[ACP] stream error"),
"the client is told, rather than left with an empty turn the host never reports",
);
// The error block closes the turn for the client; the retry stream's own
// terminator still follows it, which the client ignores because it stopped at
// the error's.
// #5958: an exhausted retry is a FAILURE, not a completed turn. The default
// streamErrorShape is "protocol", so the client gets the wire-native error
// frame — and, critically, NO finish_reason:"stop" terminal that a host like
// DSH would book as stop/completed while the task is still unfinished.
assert.ok(text.includes('"code":"stream_error"'), "a protocol-native error frame marks the turn as failed");
assert.ok(!text.includes('"finish_reason":"stop"'), "an exhausted retry must NOT be serialized as a successful completion");
assert.ok(!text.includes("[ACP] stream error"), "the legacy completed-turn error shape is off by default");
// The error's own terminal plus the retry stream's trailing terminator both
// close the turn; the client stops at the error either way.
assert.equal((text.match(/\[DONE\]/g) ?? []).length, 2, "the error's terminal, then the retry's trailing terminator");
});

test("plugin chat keeps the legacy completed-turn shape when streamErrorShape is \"completion\"", async () => {
const out: string[] = [];
const refetch = () => Promise.resolve(streamOf([chatChunk({ role: "assistant" }), chatStop(), DONE]));
await pipePluginChatWithStrip(
streamOf(unclosedEchoTurn()), makeRes(out), "openai", makeSession(), undefined, refetch,
undefined, undefined, undefined, undefined, undefined, "completion",
);
const text = out.join("");
// The opt-in escape hatch restores the #870/#2689 shape: the error text rides
// inside a well-formed completed turn (finish_reason:"stop") for hosts that
// need it visible as turn content rather than a protocol error frame.
assert.ok(text.includes("[ACP] stream error"), "the legacy in-band error text is present on opt-in");
assert.ok(text.includes('"finish_reason":"stop"'), "the legacy completed-turn terminal is preserved on opt-in");
});

/** Parse the emitted SSE text back into {event, data} pairs (test-side only). */
function sseEvents(raw: string): { event: string; data: Record<string, unknown> }[] {
return raw.split("\n\n")
Expand All @@ -504,7 +521,12 @@ function sseEvents(raw: string): { event: string; data: Record<string, unknown>
test("plugin chat (anthropic) #870: the in-band error block is well-formed at the next free index", async () => {
const out: string[] = [];
const refetch = () => Promise.resolve(streamOf(anthropicEchoOnlyTurn()));
await pipePluginChatWithStrip(streamOf(anthropicEchoOnlyTurn()), makeRes(out), "anthropic", makeSession(), undefined, refetch);
// #2689 pins the OPT-IN completion shape: hosts that set streamErrorShape:
// "completion" get the well-formed block lifecycle (ZCode schema compat).
await pipePluginChatWithStrip(
streamOf(anthropicEchoOnlyTurn()), makeRes(out), "anthropic", makeSession(), undefined, refetch,
undefined, undefined, undefined, undefined, undefined, "completion",
);
const text = out.join("");
assert.ok(text.includes("[ACP] stream error"), "#870 fired");

Expand Down Expand Up @@ -538,3 +560,16 @@ test("plugin chat (anthropic) #870: the in-band error block is well-formed at th
const priorIndices = new Set(events.slice(0, ownStartPos).filter((e) => e.event === "content_block_start").map((e) => e.data["index"]));
assert.equal(idx, priorIndices.size, `error block opened at the next free index (got ${idx}, expected ${priorIndices.size})`);
});

test("plugin chat (anthropic) defaults to a native error event, not a synthetic completion", async () => {
const out: string[] = [];
const refetch = () => Promise.resolve(streamOf(anthropicEchoOnlyTurn()));
await pipePluginChatWithStrip(streamOf(anthropicEchoOnlyTurn()), makeRes(out), "anthropic", makeSession(), undefined, refetch);
const text = out.join("");
// Default streamErrorShape="protocol": the exhausted retry surfaces as the
// Anthropic-native error event — NOT a synthetic content_block carrying the
// error text (that is the completion-only shape pinned above for opt-in hosts).
assert.ok(text.includes("event: error"), "a native Anthropic error event marks the failure");
assert.ok(text.includes('"code":"stream_error"'), "the error carries the stream_error code");
assert.ok(!text.includes("[ACP] stream error"), "no synthetic completed-turn error block by default");
});
Loading