From 3403429dfc3cd75c92984e1d58fbdab4d4ff57a2 Mon Sep 17 00:00:00 2001 From: john Date: Thu, 5 Nov 2026 00:39:07 +0800 Subject: [PATCH 1/4] fix: collapse adjacent output_config effort directives after fold reconstruction (#5688) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fold reconstruction can remove the message sitting between two per-turn effort directives (reasoning hygiene, CCR excerpting, range trimming), leaving them adjacent. OpenRouter's Anthropic Messages surface rejects that shape with a hard 400 — "Two adjacent output_config directives are not allowed" — and the session fails deterministically on every retry. Repair at egress: collapse runs of adjacent {role:"system", content:[], output_config} directives down to the last (later effort wins, matching the trailing active-directive semantics). Detection is narrowly the directive shape; text-bearing system messages are never touched. Clean histories return the input array unchanged. --- src/server.ts | 40 ++++++++++++++ src/server/prepare-anthropic.ts | 14 +++-- .../issue5688-adjacent-output-config.test.ts | 53 +++++++++++++++++++ 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/issue5688-adjacent-output-config.test.ts diff --git a/src/server.ts b/src/server.ts index 9cdb0155ad..85daa44412 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2548,6 +2548,46 @@ export function warnReasoningPairs( log("warn", `[${sessionId}] reasoning-pair-violated: ${violators.length} assistant tool-call message(s) lack reasoning_content while ${withRc} carry it — strict-echo upstreams (DeepSeek thinking mode) will reject the request; first seen: ${violationNamed(tracked.fresh, tracked.ongoing, tracked.maxRepeat)} (#684/#762/#2864)`); } +/** [#5688] OpenRouter's Anthropic Messages surface rejects a history that + * contains two ADJACENT `{role:"system", content:[], output_config:{...}}` + * effort directives ("Two adjacent output_config directives are not + * allowed"). Clients that emit per-turn effort directives (pi's + * insertThinkingLevelMessages: one before every leveled assistant message + + * one trailing) are healthy on their own — every directive is separated by + * its assistant message. But bili's fold reconstruction can remove the + * message BETWEEN two directives (reasoning hygiene, CCR excerpting, range + * trimming, empty-message pruning), leaving them adjacent and the session + * deterministically 400s on every retry. + * + * Repair at egress: collapse runs of adjacent effort directives down to the + * LAST one (later effort wins — the same semantics the trailing active + * directive relies on). Detection is deliberately narrow: a system message + * whose content is an EMPTY array (or absent) carrying an output_config + * object — exactly the directive shape, nothing else. Ordinary system + * messages (text content) are never touched. Returns the input array + * unchanged when nothing was adjacent. */ +export function dedupeAdjacentOutputConfig(messages: T[]): T[] { + const isDirective = (m: unknown): boolean => { + const msg = m as { role?: unknown; content?: unknown; output_config?: unknown }; + if (msg?.role !== "system") return false; + if (msg.output_config === null || typeof msg.output_config !== "object") return false; + if (Array.isArray(msg.content) && msg.content.length > 0) return false; + if (typeof msg.content === "string" && msg.content.length > 0) return false; + return true; + }; + const out: T[] = []; + let removed = 0; + for (const m of messages) { + if (out.length > 0 && isDirective(m) && isDirective(out[out.length - 1])) { + out[out.length - 1] = m; // later effort wins + removed++; + continue; + } + out.push(m); + } + return removed > 0 ? out : messages; +} + /** [#684] Anthropic-wire twin of the openai sentinel, narrowed by [#1327]: * warn only when bili itself lost thinking — a tool_use block that rode an * inbound assistant message WITH a thinking block now rides an outbound diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 97e5cc8341..91583274a9 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -7,7 +7,7 @@ import { captureThreadsTurn, estimateThreadsTokens, isThreadsBody, threadsReforg import type { CompressReasoningConfig } from "../reasoning-drop.js"; import type { ProxyOptions } from "../config.js"; import { pluginAgentHeader } from "../plugin.js"; -import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, makeFoldPointReasoningDroppable, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js"; +import { applyReasoningDrop, dedupeAdjacentOutputConfig, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, makeFoldPointReasoningDroppable, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; @@ -597,8 +597,16 @@ export async function prepareAnthropic( snapshotMessages(session, originalMessages); markDirty(session); - const rebuilt: AnthropicRequestBody = { ...parsed, messages: rebuiltMessages, system: systemOut, tools: toolsOut }; - warnAnthropicThinkingPairs(parsed.messages, rebuiltMessages, log, sessionId); + // [#5688] fold reconstruction can leave two per-turn effort directives + // ({role:"system", content:[], output_config}) adjacent — OpenRouter rejects + // that shape with a hard 400 on every retry. Collapse to the latter before + // egress; the helper returns the input array unchanged on the clean path. + const dedupedMessages = dedupeAdjacentOutputConfig(rebuiltMessages); + if (dedupedMessages !== rebuiltMessages) { + log("warn", `[${sessionId}] adjacent-output-config: collapsed duplicate effort directive(s) after fold reconstruction (#5688)`); + } + warnAnthropicThinkingPairs(parsed.messages, dedupedMessages, log, sessionId); + const rebuilt: AnthropicRequestBody = { ...parsed, messages: dedupedMessages, system: systemOut, tools: toolsOut }; // prompt_cache_key is the omp plugin's session id stamped for the proxy's // identity chain (#268), not part of the Anthropic Messages API — strip it // so the real upstream never sees a field it doesn't know. diff --git a/tests/issue5688-adjacent-output-config.test.ts b/tests/issue5688-adjacent-output-config.test.ts new file mode 100644 index 0000000000..8038cf724e --- /dev/null +++ b/tests/issue5688-adjacent-output-config.test.ts @@ -0,0 +1,53 @@ +// #5688: fold reconstruction must not leave two adjacent output_config effort +// directives in the outbound history — OpenRouter rejects that shape with a +// hard 400 ("Two adjacent output_config directives are not allowed") and the +// session dies on every retry until the directives are collapsed. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { dedupeAdjacentOutputConfig } from "../src/server.js"; + +type Msg = Record; +const dir = (effort: string): Msg => ({ role: "system", content: [], output_config: { effort } }); +const asst = (): Msg => ({ role: "assistant", content: [{ type: "text", text: "ok" }] }); +const user = (): Msg => ({ role: "user", content: "q" }); +const isDir = (m: Msg) => m.output_config !== undefined; + +test("clean history returns the same array reference (fast path)", () => { + const msgs = [user(), dir("max"), asst(), user(), dir("low"), asst(), user()]; + assert.equal(dedupeAdjacentOutputConfig(msgs), msgs); +}); + +test("collapses two adjacent directives, keeping the latter effort", () => { + const msgs = [user(), dir("max"), dir("low"), asst(), user()]; + const out = dedupeAdjacentOutputConfig(msgs); + assert.equal(out.length, 4); + assert.deepEqual(out.filter(isDir), [dir("low")]); +}); + +test("collapses a run of three adjacent directives down to the last", () => { + const out = dedupeAdjacentOutputConfig([user(), dir("max"), dir("high"), dir("low"), asst(), user()]); + assert.deepEqual(out.filter(isDir), [dir("low")]); +}); + +test("text-bearing system messages are never treated as directives", () => { + const sysText: Msg = { role: "system", content: [{ type: "text", text: "note" }], output_config: { effort: "max" } }; + const msgs = [user(), sysText, dir("low"), asst(), user()]; + const out = dedupeAdjacentOutputConfig(msgs); + assert.equal(out.length, 5); + assert.ok(out.includes(sysText)); +}); + +test("empty system messages without output_config are left alone", () => { + const msgs = [user(), { role: "system", content: [] }, { role: "system", content: [] }, asst(), user()]; + assert.equal(dedupeAdjacentOutputConfig(msgs), msgs); +}); + +test("directive with absent content field is still detected", () => { + const out = dedupeAdjacentOutputConfig([user(), dir("low"), { role: "system", output_config: { effort: "max" } }, asst(), user()]); + assert.deepEqual(out.filter(isDir), [{ role: "system", output_config: { effort: "max" } }]); +}); + +test("non-adjacent directives are preserved (only adjacent runs collapse)", () => { + const out = dedupeAdjacentOutputConfig([user(), dir("max"), asst(), user(), dir("low"), asst(), user()]); + assert.equal(out.filter(isDir).length, 2); +}); From 8cab8fe45cd3d2445f4cb8176229e5b5bc0b8150 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 01:28:20 +0800 Subject: [PATCH 2/4] refactor: extract dedupeAdjacentOutputConfig to its own module (#5688) --- src/dedupe-adjacent-output-config.ts | 44 ++++++++++++++++++++++++++++ src/server.ts | 43 +++------------------------ 2 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 src/dedupe-adjacent-output-config.ts diff --git a/src/dedupe-adjacent-output-config.ts b/src/dedupe-adjacent-output-config.ts new file mode 100644 index 0000000000..54b09da9a9 --- /dev/null +++ b/src/dedupe-adjacent-output-config.ts @@ -0,0 +1,44 @@ +/** [#5688] OpenRouter's Anthropic Messages surface rejects a history that + * contains two ADJACENT `{role:"system", content:[], output_config:{...}}` + * effort directives ("Two adjacent output_config directives are not + * allowed"). Clients that emit per-turn effort directives (pi's + * insertThinkingLevelMessages: one before every leveled assistant message + + * one trailing) are healthy on their own — every directive is separated by + * its assistant message. But bili's fold reconstruction can remove the + * message BETWEEN two directives (reasoning hygiene, CCR excerpting, range + * trimming, empty-message pruning), leaving them adjacent and the session + * deterministically 400s on every retry. + * + * Repair at egress: collapse runs of adjacent effort directives down to the + * LAST one (later effort wins — the same semantics the trailing active + * directive relies on). Detection is deliberately narrow: a system message + * whose content is an EMPTY array (or absent / empty string) carrying an + * output_config object — exactly the directive shape, nothing else. Ordinary + * system messages (text content) are never touched. Returns the input array + * unchanged when nothing was adjacent. + * + * Shared by every anthropic-wire egress surface that renders from a core + * view fold reconstruction can leave adjacent in: the steady prepare egress + * (prepare-anthropic), the nudge-decide side body (same file), and the loop + * round-2 rebuild (loop/adapter-anthropic buildRequest). */ +export function dedupeAdjacentOutputConfig(messages: T[]): T[] { + const isDirective = (m: unknown): boolean => { + const msg = m as { role?: unknown; content?: unknown; output_config?: unknown }; + if (msg?.role !== "system") return false; + if (msg.output_config === null || typeof msg.output_config !== "object") return false; + if (Array.isArray(msg.content) && msg.content.length > 0) return false; + if (typeof msg.content === "string" && msg.content.length > 0) return false; + return true; + }; + const out: T[] = []; + let removed = 0; + for (const m of messages) { + if (out.length > 0 && isDirective(m) && isDirective(out[out.length - 1])) { + out[out.length - 1] = m; // later effort wins + removed++; + continue; + } + out.push(m); + } + return removed > 0 ? out : messages; +} diff --git a/src/server.ts b/src/server.ts index 85daa44412..ae656d31f3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2548,45 +2548,10 @@ export function warnReasoningPairs( log("warn", `[${sessionId}] reasoning-pair-violated: ${violators.length} assistant tool-call message(s) lack reasoning_content while ${withRc} carry it — strict-echo upstreams (DeepSeek thinking mode) will reject the request; first seen: ${violationNamed(tracked.fresh, tracked.ongoing, tracked.maxRepeat)} (#684/#762/#2864)`); } -/** [#5688] OpenRouter's Anthropic Messages surface rejects a history that - * contains two ADJACENT `{role:"system", content:[], output_config:{...}}` - * effort directives ("Two adjacent output_config directives are not - * allowed"). Clients that emit per-turn effort directives (pi's - * insertThinkingLevelMessages: one before every leveled assistant message + - * one trailing) are healthy on their own — every directive is separated by - * its assistant message. But bili's fold reconstruction can remove the - * message BETWEEN two directives (reasoning hygiene, CCR excerpting, range - * trimming, empty-message pruning), leaving them adjacent and the session - * deterministically 400s on every retry. - * - * Repair at egress: collapse runs of adjacent effort directives down to the - * LAST one (later effort wins — the same semantics the trailing active - * directive relies on). Detection is deliberately narrow: a system message - * whose content is an EMPTY array (or absent) carrying an output_config - * object — exactly the directive shape, nothing else. Ordinary system - * messages (text content) are never touched. Returns the input array - * unchanged when nothing was adjacent. */ -export function dedupeAdjacentOutputConfig(messages: T[]): T[] { - const isDirective = (m: unknown): boolean => { - const msg = m as { role?: unknown; content?: unknown; output_config?: unknown }; - if (msg?.role !== "system") return false; - if (msg.output_config === null || typeof msg.output_config !== "object") return false; - if (Array.isArray(msg.content) && msg.content.length > 0) return false; - if (typeof msg.content === "string" && msg.content.length > 0) return false; - return true; - }; - const out: T[] = []; - let removed = 0; - for (const m of messages) { - if (out.length > 0 && isDirective(m) && isDirective(out[out.length - 1])) { - out[out.length - 1] = m; // later effort wins - removed++; - continue; - } - out.push(m); - } - return removed > 0 ? out : messages; -} +// [#5688] shared egress repair for adjacent per-turn effort directives — +// implementation moved to its own leaf module so loop/ can import it without +// a server.ts import cycle; re-exported here for existing consumers. +export { dedupeAdjacentOutputConfig } from "./dedupe-adjacent-output-config.js"; /** [#684] Anthropic-wire twin of the openai sentinel, narrowed by [#1327]: * warn only when bili itself lost thinking — a tool_use block that rode an From 95ddfa482892dfb07e39b89c5b80a6dc41456a1e Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 01:28:20 +0800 Subject: [PATCH 3/4] fix: apply adjacent output_config dedupe to nudge-decide side body and loop round-2 rebuild (#5688) --- src/loop/adapter-anthropic.ts | 6 +++++- src/server/prepare-anthropic.ts | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index 20b030ea68..e72e0980af 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -1,5 +1,6 @@ import type { CoreMessage } from "acp-kernel"; import { coreToAnthropic, type AnthropicRequestBody } from "acp-kernel/wire"; +import { dedupeAdjacentOutputConfig } from "../dedupe-adjacent-output-config.js"; import { appendSystemText, declaredInclusiveFromUsage } from "../util.js"; import { stampAnthropicSystemCacheControl } from "./cache-control.js"; import { buildVisibilityMarker } from "./core.js"; @@ -307,7 +308,10 @@ export function createAnthropicAdapter(requestBody: Record, ori // steady path does (`anthropicCacheMarks ?? cacheControls`) — a // client-managed session has no bili marks, and dropping its // breakpoint leaves the post-fold prefix unwritten to cache. - const messages = coreToAnthropic(coreMessages, cacheMarks ?? clientCacheControls); + // [#5688] round-2 re-renders from the core view fold reconstruction + // can leave adjacent effort directives in — collapse like the + // steady egress or the rebuild 400s where the main request passed. + const messages = dedupeAdjacentOutputConfig(coreToAnthropic(coreMessages, cacheMarks ?? clientCacheControls)); // #1876: same append-not-merge rebuild as the steady path's // injectSystem — both must emit byte-identical system (F2 seam). const system = originalSystem !== undefined ? appendSystemText(systemPrompt, originalSystem) : systemPrompt; diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 91583274a9..a4425615cc 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -530,7 +530,9 @@ export async function prepareAnthropic( } } else if (useDecide) { const ranges = turn.nudge.compressibleRanges ?? []; - const sideBody: Record = { ...parsed, messages: [...rebuiltMessages, { role: "user", content: buildDecisionPrompt(ranges) }], system: systemOut, tools: toolsOut, stream: false, max_tokens: decide.maxTokens }; + // [#5688] the side call shares the main request's upstream — + // un-deduped rebuiltMessages would 400 the decision too. + const sideBody: Record = { ...parsed, messages: [...dedupeAdjacentOutputConfig(rebuiltMessages), { role: "user", content: buildDecisionPrompt(ranges) }], system: systemOut, tools: toolsOut, stream: false, max_tokens: decide.maxTokens }; delete sideBody.prompt_cache_key; stripDecisionReasoningFields("anthropic", sideBody); const outcome = await runNudgeDecision({ req, opts, protocol: "anthropic", sideBody, session, log }); From 2b3ef9f77775448c778b0699b060c01772821c61 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 01:55:38 +0800 Subject: [PATCH 4/4] test: pin the #5688 egress dedupe through the full prepare path (#5688) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The unit suite pins the helper in isolation; nothing pinned the wiring — deleting the call site at the final assembly would have stayed green. Hermetic A/B rig (mock upstream that enforces OpenRouter's adjacency rule) drives a real /bili/ request: positive direction asserts the collapsed single directive (keep-latter) reaches upstream with a 200, negative direction asserts the healthy pi shape passes through untouched. Added during PR review of this branch. --- tests/issue5688-egress-wiring.test.ts | 229 ++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 tests/issue5688-egress-wiring.test.ts diff --git a/tests/issue5688-egress-wiring.test.ts b/tests/issue5688-egress-wiring.test.ts new file mode 100644 index 0000000000..d1d5984ed2 --- /dev/null +++ b/tests/issue5688-egress-wiring.test.ts @@ -0,0 +1,229 @@ +// #5688 wiring pin: the egress repair (dedupeAdjacentOutputConfig) must actually +// run on the REAL HTTP path — the unit suite pins the helper, this file pins +// the call site. A fold can leave two per-turn effort directives +// ({role:"system", content:[], output_config:{effort}}) adjacent in the +// reconstructed history (the covered span between them is pruned while both +// directive-only survivors stay); OpenRouter rejects that shape with a hard +// 400 ("Two adjacent output_config directives are not allowed") and the +// session dies deterministically on every retry. The mock upstream below +// enforces the SAME validation rule, so a regression at the seam fails twice: +// on the forwarded bytes AND on the 400 the client receives. Assertions are +// structural by design: message text may legally ride ACP render tags +// depending on session maturity, which is orthogonal to the collapse. +import assert from "node:assert"; +import http from "node:http"; +import { once } from "node:events"; +import { randomUUID } from "node:crypto"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; + +type WireMsg = { role: string; content?: unknown; output_config?: unknown }; + +const directive = (effort: string): WireMsg => ({ role: "system", content: [], output_config: { effort } }); + +/** OpenRouter's per-turn-effort beta validation rule, enforced verbatim. */ +function hasAdjacentDirectives(messages: WireMsg[]): boolean { + const isDir = (m: WireMsg | undefined): boolean => + !!m && m.role === "system" && m.output_config != null && (!Array.isArray(m.content) || m.content.length === 0); + for (let i = 0; i + 1 < messages.length; i++) { + if (isDir(messages[i]) && isDir(messages[i + 1])) return true; + } + return false; +} + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + const { promise, resolve, reject } = Promise.withResolvers(); + server.close((error) => (error ? reject(error) : resolve())); + return promise; +} + +function anthropicSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function textScript(): string { + return anthropicSse("message_start", { type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: 42 } } }) + + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }) + + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } }) + + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }) + + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 2 } }) + + anthropicSse("message_stop", { type: "message_stop" }); +} + +interface Rig { + proxyPort: number; + portHint(): number; + upstreamBodies: string[]; + closeAll(): Promise; +} + +async function startRig(): Promise { + const upstreamBodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + upstreamBodies.push(raw); + const parsed = JSON.parse(raw) as { messages?: WireMsg[] }; + if (parsed.messages && hasAdjacentDirectives(parsed.messages)) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ + type: "error", + error: { + type: "invalid_request_error", + message: "Invalid Anthropic Messages API request", + metadata: { raw: "[{\"code\":\"custom\",\"path\":[\"messages\",2,\"output_config\"],\"message\":\"Two adjacent output_config directives are not allowed\"}]" }, + }, + })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(textScript()); + }); + }); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "l5688-model": { context: 100_000 } } } }, + modelContextLimit: 100_000, + kernelConfig: defaultConfig(100_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + log: false, + sessionHeader: "x-acp-session", + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const server = await startServer(opts); + await listen(server); + const proxyPort = (server.address() as { port: number }).port; + + return { + proxyPort, + portHint: () => upstreamPort, + upstreamBodies, + closeAll: async () => { + await close(server); + await close(upstream); + }, + }; +} + +async function postModel(rig: Rig, sessionId: string, messages: WireMsg[]): Promise { + const port = rig.portHint(); + return fetch(`http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${port}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-session-affinity": sessionId }, + body: JSON.stringify({ model: "l5688-model", max_tokens: 8192, stream: true, messages }), + }); +} + +async function waitFor(pred: () => boolean, what: string): Promise { + for (let i = 0; i < 200; i++) { + if (pred()) return; + await new Promise((r) => setTimeout(r, 25)); + } + assert.ok(pred(), `timeout waiting for ${what}`); +} + +function directivesOf(body: string): WireMsg[] { + const parsed = JSON.parse(body) as { messages: WireMsg[] }; + return parsed.messages.filter((m) => m.output_config != null); +} + +test("#5688: two adjacent effort directives (fold terminal condition) are collapsed to the latter before egress", async () => { + const rig = await startRig(); + try { + // The exact shape the fold reconstruction leaves behind: the covered + // span between two surviving directive-only systems is gone, the two + // directives sit next to each other. + const history: WireMsg[] = [ + { role: "user", content: "q1" }, + directive("max"), + directive("low"), + { role: "assistant", content: [{ type: "text", text: "a2" }] }, + { role: "user", content: "q3" }, + ]; + // Fresh id per run: the shared state dir persists sessions across + // local/CI runs, and a replayed id re-encodes history differently. + const res = await postModel(rig, `5688-a-${randomUUID()}`, history); + await waitFor(() => rig.upstreamBodies.length >= 1, "forward"); + + assert.equal(res.status, 200, `client saw ${res.status} — the validator rejected the forwarded shape`); + + const out = JSON.parse(rig.upstreamBodies[0]) as { messages: WireMsg[] }; + assert.ok(!hasAdjacentDirectives(out.messages), "no adjacent output_config directives survive egress"); + const dirs = directivesOf(rig.upstreamBodies[0]); + assert.equal(dirs.length, 1, "exactly one directive remains after the collapse"); + assert.deepEqual(dirs[0], directive("low"), "the LATTER effort wins (keep-latter semantics)"); + assert.deepEqual( + out.messages.map((m) => m.role), + ["user", "system", "assistant", "user"], + "5 inbound -> 4 outbound: only the earlier duplicate was removed, survivors keep their order", + ); + for (const m of out.messages.filter((x) => x.role !== "system")) { + const blocks = Array.isArray(m.content) ? (m.content as { type?: string }[]) : []; + assert.ok(blocks.length > 0, "surviving non-directive message keeps its content blocks"); + } + } finally { + await rig.closeAll(); + } +}); + +test("#5688 negative: a healthy pi-shaped history (directives separated by assistants) passes through with every directive intact", async () => { + const rig = await startRig(); + try { + // pi's native shape: one directive before every leveled assistant plus + // one trailing directive for the active level. Nothing adjacent. + const history: WireMsg[] = [ + { role: "user", content: "u1" }, + directive("max"), + { role: "assistant", content: [{ type: "text", text: "a1" }] }, + { role: "user", content: "u2" }, + directive("high"), + { role: "assistant", content: [{ type: "text", text: "a2" }] }, + directive("low"), + ]; + const res = await postModel(rig, `5688-b-${randomUUID()}`, history); + await waitFor(() => rig.upstreamBodies.length >= 1, "forward"); + assert.equal(res.status, 200); + + const out = JSON.parse(rig.upstreamBodies[0]) as { messages: WireMsg[] }; + assert.equal(out.messages.length, 7, "clean history is not reshaped (identity fast path)"); + const dirs = directivesOf(rig.upstreamBodies[0]); + assert.deepEqual(dirs.map((d) => d.output_config), [{ effort: "max" }, { effort: "high" }, { effort: "low" }], "all three directives survive, in order, unmodified"); + assert.ok(!hasAdjacentDirectives(out.messages)); + assert.deepEqual( + out.messages.map((m) => m.role), + ["user", "system", "assistant", "user", "system", "assistant", "system"], + "every non-directive message survives in place", + ); + } finally { + await rig.closeAll(); + } +});