From 916d24c22a3e9711cfa3af029768a85890e8fba2 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 12 Oct 2026 02:43:25 +0800 Subject: [PATCH] fix(affinity): project responses/google wire renders into the neutral identity domain (#2791) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit blockText() only knew text|thinking|redacted_thinking content types, so a responses-wire render (input_text/output_text blocks) hashed to canon-JSON blobs instead of turns: openai==responses identity equality was FALSE while openai==google was TRUE. resolve()/chainFingerprint hash neutralTurns(raw wire input) (src/prefix-affinity.ts:553, :923) with no pre-conversion, so any responses-wire client (codex /v1/responses proxy mode, dsh openai-responses) forked a pfa-* session on a cross-wire model switch — the #2791 silent-fold-loss class. Also align the merge boundaries: gemini splits commentary and function call across contents[] items while openai merges across messages — model items now merge into the pending assistant run (mirroring the openai block-list branch); responses function_call sibling items merge into a pending run when one exists; google functionResponse unwraps its transport wrapper ({output:}/{result:}/{content:} single-key-string conventions). Tests: - unit: all four wire renders (openai/anthropic/responses/google) project to IDENTICAL neutral turns; mid-session switch onto responses AND google wires resolves via:"prefix" with the same sessionId in both directions. - proxy e2e: cross-wire switch onto the responses and google lanes keeps the session AND its fold (pfas===1, carrier attached, no re-fold) — red green proven against the pre-fix src (pfa fork fails the test). - real-client pi e2e: openai→responses switch (fakeresp provider on the openai-responses API) keeps conv/session/fold; the anthropic-switch test gains the reverse (anthropic→openai) leg. Note: responses sessions' neutral hashes shift (text-turn canon fix + merge boundaries) — a live responses-wire anonymous session will miss prefix-resolve ONCE on upgrade and re-fold, same migration class as the hv1→hv2 bridge, smaller population. --- src/affinity-neutral.ts | 39 ++++++- tests/affinity-crosswire-fold.test.ts | 156 ++++++++++++++++++++++++++ tests/affinity-neutral.test.ts | 88 +++++++++++++-- tests/e2e/e2e-native-pi.test.ts | 103 ++++++++++++++++- tests/e2e/fake-upstream-chat.mjs | 106 +++++++++++++++++ 5 files changed, 478 insertions(+), 14 deletions(-) diff --git a/src/affinity-neutral.ts b/src/affinity-neutral.ts index 59c44b58cc..0ca4a7af47 100644 --- a/src/affinity-neutral.ts +++ b/src/affinity-neutral.ts @@ -164,7 +164,9 @@ function canonicalCallArgs(args: unknown): string { function blockText(block: unknown): string | null { if (!block || typeof block !== "object") return null; const b = block as { type?: unknown; text?: unknown; thinking?: unknown }; - if (b.type === "text") return typeof b.text === "string" ? b.text : ""; + if (b.type === "text" || b.type === "input_text" || b.type === "output_text") { + return typeof b.text === "string" ? b.text : ""; + } if (b.type === "thinking" || b.type === "redacted_thinking") { return typeof b.thinking === "string" ? b.thinking : ""; } @@ -234,7 +236,6 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] { // google-generative contents[] items: {role:"user"|"model", parts:[…]} if (Array.isArray(m.parts)) { - flushAssistant(); const role = m.role === "model" ? "assistant" : typeof m.role === "string" ? m.role : "user"; const texts: string[] = []; const calls: string[] = []; @@ -248,11 +249,32 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] { calls.push(`${String(fc.name ?? "")}\u0000${canon(fc.args ?? {})}`); } if (p.functionResponse && typeof p.functionResponse === "object") { - toolBody = canon((p.functionResponse as { response?: unknown }).response ?? p.functionResponse); + const resp = (p.functionResponse as { response?: unknown }).response; + // gemini clients wrap the tool body under an arbitrary key + // ({output:…}/{result:…}/{content:…}); a single-string-valued + // object IS the body — unwrap so it equals the openai render. + if (typeof resp === "string") toolBody = resp; + else if (resp && typeof resp === "object" && !Array.isArray(resp)) { + const entries = Object.entries(resp as Record); + toolBody = entries.length === 1 && typeof entries[0]![1] === "string" ? entries[0]![1]! : canon(resp); + } else toolBody = canon(resp ?? p.functionResponse); } } + // model turns MERGE into the pending assistant run — gemini splits + // commentary and the function call across contents[] items while + // openai splits them across messages; both must reduce to the same + // single run or cross-wire replays fork (#2791). + if (role === "assistant") { + asst = asst ?? { kind: "assistant", text: "", calls: [] }; + const joined = texts.join(""); + asst.text = (asst.text ?? "") + joined; + asst.calls = [...(asst.calls ?? []), ...calls]; + asstFrags.push(fragmentHash("assistant", joined, calls)); + continue; + } + flushAssistant(); if (toolBody !== undefined) turns.push({ kind: "tool", text: toolBody, frags: [fragmentHash("tool", toolBody, [])] }); - if (calls.length > 0) turns.push({ kind: "assistant", calls, frags: [fragmentHash("assistant", "", calls)] }); + if (calls.length > 0 && toolBody === undefined) turns.push({ kind: "assistant", calls, frags: [fragmentHash("assistant", "", calls)] }); else if (texts.length > 0) pushTextTurn(role, texts.join(""), [fragmentHash(role, texts.join(""), [])]); continue; } @@ -260,8 +282,15 @@ export function neutralTurns(messages: unknown[]): NeutralTurn[] { // responses-protocol input items: {type:"function_call", …} / // {type:"function_call_output", …} arrive as siblings of messages. if (typeof m.type === "string" && m.type === "function_call" && typeof m.name === "string") { - flushAssistant(); const call = `${m.name}\u0000${canonicalCallArgs(m.arguments)}`; + // codex splits assistant commentary and the tool call across items — + // merge into the pending run so it reduces like the openai render. + if (asst !== null) { + asst.calls = [...(asst.calls ?? []), call]; + asstFrags.push(fragmentHash("assistant", "", [call])); + continue; + } + flushAssistant(); turns.push({ kind: "assistant", calls: [call], frags: [fragmentHash("assistant", "", [call])] }); continue; } diff --git a/tests/affinity-crosswire-fold.test.ts b/tests/affinity-crosswire-fold.test.ts index 37933ab106..71aa1e4993 100644 --- a/tests/affinity-crosswire-fold.test.ts +++ b/tests/affinity-crosswire-fold.test.ts @@ -117,6 +117,7 @@ test("cross-wire switch keeps the anonymous session AND its fold attached end-to modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] }, @@ -185,4 +186,159 @@ test("cross-wire switch keeps the anonymous session AND its fold attached end-to } }); +// #2791 follow-up (neutral projections for responses/google): the same +// proxy-level conjunction, extended onto the responses (/v1/responses, +// codex / dsh openai-responses shape) and google (v1beta +// :streamGenerateContent, Gemini shape) lanes. Phase A folds on the openai +// lane; phase B switches the SAME history onto the responses wire; phase C +// switches it again onto the google wire. Every switch must keep the one +// anonymous session, the fold carrier, and the covered content folded. +test("cross-wire switch onto the responses and google lanes keeps the session AND its fold (#2791)", { timeout: 180_000 }, async () => { + const bodies: { openai: string[]; responses: string[]; google: string[] } = { openai: [], responses: [], google: [] }; + let suppress = false; + let foldDemands = 0; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + const url = req.url ?? ""; + const idx = bodies.openai.length + bodies.responses.length + bodies.google.length + 1; + const wantFold = !suppress && Buffer.byteLength(body) > THRESHOLD && parseRefIds(body).length >= 12; + if (wantFold) { foldDemands++; suppress = true; } + const reply = `Reply ${idx}: acknowledged. ` + FILLER(idx % 7, 0.2); + if (url.endsWith("/v1/chat/completions")) { + bodies.openai.push(body); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + const line = (o: unknown): void => { res.write(`data: ${JSON.stringify(o)}\n\n`); }; + if (wantFold) { + const refs = parseRefIds(body); + const args = JSON.stringify({ content: [{ startId: refs[2]!, endId: refs[refs.length - 6]!, topic: "audit fold", summary: SUMMARY_TEXT }] }); + line({ id: `c_${idx}`, object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: null, tool_calls: [{ index: 0, id: `call_cmp_${idx}`, type: "function", function: { name: "compress", arguments: args } }] } }] }); + line({ id: `c_${idx}`, object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }], usage: { prompt_tokens: 100, completion_tokens: 2 } }); + } else { + line({ id: `c_${idx}`, object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: reply } }] }); + line({ id: `c_${idx}`, object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 100, completion_tokens: 5 } }); + } + res.write("data: [DONE]\n\n"); + res.end(); + return; + } + if (/\/responses$/.test(url)) { + // Responses-API SSE (shape mirrors the real-codex fake upstream). + bodies.responses.push(body); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + const rid = `resp_${idx}`; + const msgId = `msg_${rid}`; + const msg = { type: "message", id: msgId, role: "assistant", content: [{ type: "output_text", text: reply }] }; + const ev = (event: string, data: unknown): void => { res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`); }; + ev("response.created", { type: "response.created", response: { id: rid, status: "in_progress", output: [] } }); + ev("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { type: "message", id: msgId, role: "assistant", content: [] } }); + ev("response.output_text.delta", { type: "response.output_text.delta", item_id: msgId, output_index: 0, content_index: 0, delta: reply }); + ev("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: msg }); + ev("response.completed", { type: "response.completed", response: { id: rid, status: "completed", output: [msg], usage: { input_tokens: 100, output_tokens: 5 } } }); + res.write("data: [DONE]\n\n"); + res.end(); + return; + } + // google :streamGenerateContent?alt=sse — Gemini SSE frames. + bodies.google.push(body); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + const frame = (parts: Array>, finishReason?: string): void => { + const candidate: Record = { content: { role: "model", parts }, index: 0 }; + if (finishReason) candidate.finishReason = finishReason; + res.write(`data: ${JSON.stringify({ candidates: [candidate], modelVersion: "probe-model" })}\n\n`); + }; + frame([{ text: reply }]); + frame([], "STOP"); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, host: "127.0.0.1", upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "probe-model": { context: 200_000 } } } }, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, autoUpdate: false, + mitm: { enabled: false, domains: [] }, + compat: { roles: {} }, streamErrorShape: "protocol", passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + } as unknown as import("../src/config.ts").ProxyOptions); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + const openaiBase = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`; + const responsesBase = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; + const googleBase = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/probe-model:streamGenerateContent?alt=sse`; + + type Msg = { role: string; content: string }; + const hist: Msg[] = []; + try { + // Phase A — openai lane establishes the anonymous session and folds. + const openaiTurn = async (t: number): Promise => { + hist.push({ role: "user", content: `Turn ${t}: audit province ${t}. ` + FILLER(t, 6) }); + const resp = await fetch(openaiBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", stream: true, messages: [...hist] }) }); + assert.equal(resp.status, 200, `openai turn ${t}`); + await resp.text(); + hist.push({ role: "assistant", content: `Reply about province ${t}: acknowledged. ` + FILLER(t, 0.2) }); + }; + for (let t = 1; t <= 12; t++) await openaiTurn(t); + const pfas = prefixAffinity.trackedSessionIds().filter((id) => id.startsWith("pfa-")); + assert.equal(pfas.length, 1, "one anonymous session on the openai lane"); + const sessionId = pfas[0]!; + const blocksAfterFold = getSession(sessionId)!.state.blocks.map((b) => b.blockId); + assert.ok(blocksAfterFold.length >= 1, "the fold landed on the openai lane"); + const foldsBeforeSwitch = foldDemands; + + // Phase B — switch onto the RESPONSES wire (codex / dsh openai-responses + // render): same history re-rendered as input[] items. + hist.push({ role: "user", content: "Turn 13: which provinces did the audit cover, and were there anomalies?" }); + const respItems = hist.map((m) => ({ + type: "message", + role: m.role, + content: [{ type: m.role === "assistant" ? "output_text" : "input_text", text: m.content }], + })); + const respB = await fetch(responsesBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", stream: true, instructions: "crosswire probe", input: respItems }) }); + const respRaw = await respB.text(); + if (respB.status !== 200) assert.fail(`responses switch turn: ${respB.status}: ${respRaw.slice(0, 200)}`); + hist.push({ role: "assistant", content: "Responses reply about turn 13: acknowledged." }); + + const responsesBody = bodies.responses[bodies.responses.length - 1]!; + let pfasB = prefixAffinity.trackedSessionIds().filter((id) => id.startsWith("pfa-")); + assert.equal(pfasB.length, 1, `the responses switch must not fork: got ${pfasB.join(", ")}`); + assert.equal(pfasB[0], sessionId); + assert.ok(responsesBody.includes(SUMMARY_TEXT), "the fold carrier is attached on the responses wire"); + assert.equal((responsesBody.match(new RegExp(EARLY_FILLER_TAG, "g")) ?? []).length, 0, "covered early content must not replay unfolded on the responses switch"); + assert.equal(foldDemands, foldsBeforeSwitch, "no re-fold demanded on the responses switch"); + assert.deepEqual(getSession(sessionId)!.state.blocks.map((b) => b.blockId), blocksAfterFold, "fold blocks unchanged across the responses switch"); + + // Phase C — switch AGAIN onto the GOOGLE wire (Gemini render): same + // history re-rendered as contents[].parts[]. + hist.push({ role: "user", content: "Turn 14: continue with province seven." }); + const contents = hist.map((m) => ({ role: m.role === "assistant" ? "model" : "user", parts: [{ text: m.content }] })); + const respG = await fetch(googleBase, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "probe-model", contents }) }); + const googleRaw = await respG.text(); + if (respG.status !== 200) assert.fail(`google switch turn: ${respG.status}: ${googleRaw.slice(0, 200)}`); + + const googleBody = bodies.google[bodies.google.length - 1]!; + pfasB = prefixAffinity.trackedSessionIds().filter((id) => id.startsWith("pfa-")); + assert.equal(pfasB.length, 1, `the google switch must not fork: got ${pfasB.join(", ")}`); + assert.equal(pfasB[0], sessionId); + assert.ok(googleBody.includes(SUMMARY_TEXT), "the fold carrier is attached on the google wire"); + assert.equal((googleBody.match(new RegExp(EARLY_FILLER_TAG, "g")) ?? []).length, 0, "covered early content must not replay unfolded on the google switch"); + assert.equal(foldDemands, foldsBeforeSwitch, "no re-fold demanded on the google switch"); + assert.deepEqual(getSession(sessionId)!.state.blocks.map((b) => b.blockId), blocksAfterFold, "fold blocks unchanged across the google switch"); + } finally { + proxy.close(); + upstream.close(); + } +}); + test.after(() => fs.rmSync(testRoot, { recursive: true, force: true })); diff --git a/tests/affinity-neutral.test.ts b/tests/affinity-neutral.test.ts index 3ec5640777..5cb0aed3dd 100644 --- a/tests/affinity-neutral.test.ts +++ b/tests/affinity-neutral.test.ts @@ -71,17 +71,57 @@ function anthropicRender(extra: Array> = []): Array { +/** google-generative wire: contents[] with parts[], model role, functionCall/ + * functionResponse parts (Gemini native shape — commentary and the call + * share one parts array; functionResponse rides the following user turn). */ +function googleRender(extra: Array> = []): Array> { + return [ + ...transcript.slice(0, -1).map((t) => ({ role: t.role === "assistant" ? "model" : "user", parts: [{ text: t.text }] })), + { + role: "model", + parts: [ + { text: transcript[transcript.length - 1]!.text }, + { functionCall: { name: "read_file", args: { path: "/src/billing.ts" } } }, + ], + }, + { + role: "user", + parts: [{ functionResponse: { name: "read_file", response: { output: "export function bill() { return 42; }" } } }], + }, + ...extra, + ]; +} + +/** responses wire: input[] items — message items with input_text/output_text + * blocks, function_call / function_call_output as sibling items (codex / + * dsh openai-responses shape). */ +function responsesRender(extra: Array> = []): Array> { + return [ + ...transcript.map((t) => ({ + type: "message", + role: t.role, + content: [{ type: t.role === "user" ? "input_text" : "output_text", text: t.text }], + })), + { type: "function_call", call_id: "call_1", name: "read_file", arguments: JSON.stringify({ path: "/src/billing.ts" }) }, + { type: "function_call_output", call_id: "call_1", output: "export function bill() { return 42; }" }, + ...extra, + ]; +} + +test("#2791: all four wire renders of the same conversation project to IDENTICAL neutral turns", () => { const o = neutralTurns(openaiRender()); const a = neutralTurns(anthropicRender()); - assert.equal(o.length, a.length, `turn counts must agree (openai ${o.length} vs anthropic ${a.length})`); - assert.ok(o.length >= 9, "the projection must carry the substance turns (the trailing text assistant merges with the tool-call assistant: one run)"); + const r = neutralTurns(responsesRender()); + const g = neutralTurns(googleRender()); + assert.equal(o.length, a.length, `openai vs anthropic turn counts (got ${o.length} vs ${a.length})`); + assert.equal(o.length, r.length, `openai vs responses turn counts (got ${o.length} vs ${r.length})`); + assert.equal(o.length, g.length, `openai vs google turn counts (got ${o.length} vs ${g.length})`); + assert.ok(o.length >= 9, "the projection must carry the substance turns"); for (let i = 0; i < o.length; i++) { - assert.equal( - neutralTurnCanonical(o[i]!), - neutralTurnCanonical(a[i]!), - `turn ${i} (${o[i]!.kind}) must canonicalize identically across wires`, - ); + const oc = neutralTurnCanonical(o[i]!); + assert.equal(oc, neutralTurnCanonical(a[i]!), `turn ${i} (${o[i]!.kind}) openai vs anthropic`); + assert.equal(oc, neutralTurnCanonical(r[i]!), `turn ${i} (${o[i]!.kind}) openai vs responses`); + assert.equal(oc, neutralTurnCanonical(g[i]!), `turn ${i} (${o[i]!.kind}) openai vs google`); } }); @@ -109,6 +149,38 @@ test("#2791: a mid-session protocol switch keeps the SAME anonymous session (no assert.equal(back.sessionId, first.sessionId); }); +test("#2791: mid-session switch onto the responses and google wires keeps the SAME anonymous session", () => { + for (const [label, render, extraTurn, extraBack] of [ + ["responses", responsesRender, { type: "message", role: "user", content: [{ type: "input_text", text: "and the dunning letters stage?" }] }, [ + { role: "user", content: "and the dunning letters stage?" }, + { role: "assistant", content: "dunning 9: letter dispatch latency is stable; queue depth under four" }, + ]], + ["google", googleRender, { role: "user", parts: [{ text: "and the dunning letters stage?" }] }, [ + { role: "user", content: "and the dunning letters stage?" }, + { role: "assistant", content: "dunning 9: letter dispatch latency is stable; queue depth under four" }, + ]], + ] as const) { + const r = new PrefixAffinityResolver(); + const first = r.resolve(openaiRender())!; + assert.equal(first.via, "new"); + r.note(first.sessionId, first.incomingDepth, first.tailHash, first.itemHashes, false, first.sketches, first.userFlags); + + // switch: the SAME history arrives re-rendered on the other wire, + // plus one new user turn. + const switched = r.resolve(render([extraTurn as unknown as Record]))!; + assert.equal(switched.via, "prefix", `${label}: cross-wire replay must resolve by exact prefix, not mint`); + assert.equal(switched.sessionId, first.sessionId, `${label}: the session must survive the protocol switch`); + assert.ok(switched.incomingDepth > first.incomingDepth, `${label}: depth must grow past the switch`); + + // and back to openai: the render extended past the foreign-wire turn + // still matches (the fold ledger survived the round trip). + r.note(switched.sessionId, switched.incomingDepth, switched.tailHash, switched.itemHashes, false, switched.sketches, switched.userFlags); + const back = r.resolve(openaiRender(extraBack as unknown as Array>))!; + assert.equal(back.via, "prefix", `${label}: switching back must also resolve by exact prefix`); + assert.equal(back.sessionId, first.sessionId, `${label}: same session after the round trip`); + } +}); + test("#2791: dsh model-switch client markers are not user words — a marker turn never anchors identity", () => { const base = openaiRender(); const withMarker = [ diff --git a/tests/e2e/e2e-native-pi.test.ts b/tests/e2e/e2e-native-pi.test.ts index af1aee7d7a..d064516835 100644 --- a/tests/e2e/e2e-native-pi.test.ts +++ b/tests/e2e/e2e-native-pi.test.ts @@ -127,7 +127,7 @@ function filler(lines: number): string { type OracleEntry = { t: number; - wire?: "openai" | "anthropic"; + wire?: "openai" | "anthropic" | "responses"; stream: boolean; plugin: string | null; conv: string | null; @@ -335,6 +335,23 @@ async function startCtx(): Promise { }, ], }, + // Third wire (#2791 responses column): the openai-responses API — + // the input[]-items render whose neutral projection was the last + // un-projected lane (src/affinity-neutral.ts input_text/output_text). + fakeresp: { + baseUrl: `http://127.0.0.1:${ctx.fakePort}/v1`, + api: "openai-responses", + apiKey: "e2e-fake-key", + models: [ + { + id: "fake-resp-model", + name: "Fake Responses", + input: ["text"], + contextWindow: 60000, + maxTokens: 4096, + }, + ], + }, }, }, null, @@ -734,6 +751,18 @@ if (checkOnly) { `switch run failed (code=${switchRun.code}); stderr:\n${switchRun.stderr}`, ); + // And BACK onto the openai wire — the reverse direction of the switch + // must not fork either (a render asymmetry would only appear here). + const backRun = await piRun(ctx, "请调用probe_tool", { + resume: true, + model: "fake/fake-model", + }); + assert.equal( + backRun.code, + 0, + `back run failed (code=${backRun.code}); stderr:\n${backRun.stderr}`, + ); + const oracle = readOracle(ctx.reqLog); const openaiRows = oracle.filter((o) => o.wire !== "anthropic"); const anthropicRows = oracle.filter((o) => o.wire === "anthropic"); @@ -784,6 +813,12 @@ if (checkOnly) { `first anthropic request must inherit the conversation history (nmsg=${firstAnth.nmsg}; a fork would arrive nearly empty)`, ); + // The reverse switch re-enters the openai lane AFTER the anthropic + // turns — same conversation, still exactly one compress. + const anthIdx = oracle.findIndex((o) => o.wire === "anthropic"); + const backRow = oracle.slice(anthIdx + 1).find((o) => o.wire === "openai"); + assert.ok(backRow, "openai-wire rows must resume after the anthropic switch (reverse direction)"); + // One persisted bili session with blocks — a protocol-switch fork would // have written a SECOND session file with an empty ledger. await stopProxiesGracefully(ctx); @@ -801,6 +836,72 @@ if (checkOnly) { }, ); + test( + "pi openai→responses switch keeps the session, conv, and fold (#2791 responses column)", + async (t) => { + const ctx = await startCtx(); + t.after(() => teardown(ctx)); + try { + // Load + fold on the openai-completions wire (same shape as the + // compress test above — 4 probe rounds so the run-1 filler message + // sits OUTSIDE the protected zone when `compress` arrives). + const load = await piRun( + ctx, + `${filler(120)}\n${Array.from({ length: 4 }, () => "请调用probe_tool").join(";")}`, + ); + assert.equal(load.code, 0, `load run failed; stderr:\n${load.stderr}`); + const fold = await piRun( + ctx, + "请调用probe_tool;请调用compress;请调用acp_status", + { resume: true }, + ); + assert.equal(fold.code, 0, `fold run failed; stderr:\n${fold.stderr}`); + + // Switch mid-session onto the openai-RESPONSES wire (input[] items + // render). Before the neutral-projection fix the responses renders + // hashed to canon blobs → prefix resolve missed → pfa-* fork. + const switchRun = await piRun(ctx, "请调用probe_tool", { + resume: true, + model: "fakeresp/fake-resp-model", + }); + assert.equal( + switchRun.code, + 0, + `switch run failed (code=${switchRun.code}); stderr:\n${switchRun.stderr}`, + ); + + const oracle = readOracle(ctx.reqLog); + const respRows = oracle.filter((o) => o.wire === "responses"); + assert.ok(respRows.length >= 1, "no responses-wire requests reached the fake upstream"); + for (const row of respRows) { + assert.equal(row.plugin, "pi", `responses row must be plugin-stamped (got ${row.plugin})`); + assert.equal(row.model, "fake-resp-model"); + } + const convs = new Set(oracle.filter((o) => o.plugin === "pi").map((o) => o.conv)); + assert.equal(convs.size, 1, `fork across the switch: convs=${[...convs].join(", ")}`); + const compressCalls = oracle.filter((o) => o.toolName === "compress").length; + assert.equal(compressCalls, 1, `the fold must be REUSED, not re-executed (compressCalls=${compressCalls})`); + assert.ok( + respRows[0].nmsg >= 6, + `first responses request must inherit the conversation history (nmsg=${respRows[0].nmsg}; a fork would arrive nearly empty)`, + ); + } finally { + await stopProxiesGracefully(ctx); + } + const sessions = sessionFiles(ctx); + assert.equal( + sessions.length, + 1, + `exactly one persisted session across the switch (got ${sessions.length}: ${sessions.map((s) => path.basename(s.file)).join(", ")})`, + ); + const blocks = sessions[0]?.parsed.payload?.state?.blocks; + assert.ok( + blocks && Object.keys(blocks as Record).length >= 1, + "the persisted session must carry the folded blocks", + ); + }, + ); + test( "pi-subagents unmodified delegate child: proxy-style ACP channel fallback (#2268)", { skip: suiteSkipReason }, diff --git a/tests/e2e/fake-upstream-chat.mjs b/tests/e2e/fake-upstream-chat.mjs index 3ae0325990..1034f00761 100644 --- a/tests/e2e/fake-upstream-chat.mjs +++ b/tests/e2e/fake-upstream-chat.mjs @@ -159,6 +159,35 @@ function anthropicToOpenai(body) { }; } +function responsesToOpenai(body) { + // Responses-API input items (pi `openai-responses`, codex): message items + // carry content blocks; function_call / function_call_output arrive as + // SIBLING items. Normalized to the openai-ish shape the scripting core + // understands so a session that switches onto this wire keeps its + // directive queue and oracle fields. + const out = []; + if (typeof body.instructions === "string" && body.instructions) { + out.push({ role: "system", content: body.instructions }); + } + for (const it of body.input ?? []) { + if (it?.type === "function_call") { + out.push({ role: "assistant", content: null, tool_calls: [{ id: it.call_id ?? it.id, type: "function", function: { name: it.name, arguments: typeof it.arguments === "string" ? it.arguments : JSON.stringify(it.arguments ?? {}) } }] }); + } else if (it?.type === "function_call_output") { + out.push({ role: "tool", tool_call_id: it.call_id, content: typeof it.output === "string" ? it.output : JSON.stringify(it.output ?? "") }); + } else if (it?.type === "message") { + const text = flatContent(it.content); + if (text) out.push({ role: it.role, content: text }); + } + } + return { + messages: out, + model: body.model, + stream: body.stream, + tools: (body.tools ?? []).map((t) => ({ function: { name: t?.name } })), + nmsgNative: (body.input ?? []).length, + }; +} + function answerFor(convKey, firstUserText, body) { // Directives are parsed from the LAST user message: a `pi -p --continue` // follow-up run re-sends the whole history, and its fresh prompt must @@ -389,6 +418,83 @@ const server = http.createServer((req, res) => { }); return; } + if (req.method === "POST" && /\/responses$/.test(req.url)) { + let raw = ""; + req.on("data", (c) => { raw += c; }); + req.on("end", () => { + let parsed = {}; + try { parsed = JSON.parse(raw || "{}"); } catch { /* noop */ } + if (process.env.FAKE_DUMP) { try { fs.appendFileSync(process.env.FAKE_DUMP, raw + "\n"); } catch { /* noop */ } } + const norm = responsesToOpenai(parsed); + const messages = norm.messages; + const users = messages.filter((x) => x?.role === "user" && !isChainCarrierMsg(x)); + const firstUserText = users.length > 0 ? flatContent(users[0].content) : ""; + const lastUserMsg = users.length > 0 ? users[users.length - 1] : null; + const lastUserRefMatch = lastUserMsg + ? flatContent(lastUserMsg.content).match(/\x3cacp\s+[^>]*?\x3e(m\d{5})\x3c\/acp\x3e/) + : null; + const convKey = req.headers["x-bili-plugin-conversation"] ?? "anon"; + const notifCount = users.filter((u) => flatContent(u.content).replace(/\x3cacp\b[^>]*\x3e[\s\S]*?\x3c\/acp\x3e/g, "").includes("[acp_delegate")).length; + const reply = answerFor(convKey, firstUserText, { messages, tools: norm.tools }); + try { + fs.appendFileSync(REQLOG, JSON.stringify({ + t: Date.now(), + url: req.url, + wire: "responses", + stream: !!parsed.stream, + model: parsed.model, + plugin: req.headers["x-bili-plugin"] ?? null, + conv: req.headers["x-bili-plugin-conversation"] ?? null, + ctxwin: req.headers["x-bili-plugin-context-window"] ?? null, + maxout: req.headers["x-bili-plugin-max-output"] ?? null, + tools: (parsed.tools ?? []).map((t) => t?.name), + nmsg: norm.nmsgNative, + roles: messages.map((x) => x?.role).join(","), + acpTagCount: (JSON.stringify(parsed.input ?? []).match(/\x3cacp\s/g) ?? []).length, + fillerHits: (raw.match(/filler line \d+/g) ?? []).length, + toolResults: toolResultsOf(messages), + queueIdx: reply.queueIdx, + toolName: reply.toolName ?? null, + toolArgs: reply.toolArgs ?? null, + lastUser: firstUserText.slice(0, 120), + lastUserHead: (lastUserMsg ? flatContent(lastUserMsg.content) : "").slice(0, 120), + lastUserRef: lastUserRefMatch ? lastUserRefMatch[1] : null, + title: reply.title === true, + notifCount, + }) + "\n"); + } catch { /* noop */ } + const usageIn = estIn(raw); + const rid = `resp_fake_${++globalCallN}`; + const fcId = `fc_${rid}`; + const msgId = `msg_${rid}`; + const item = reply.tool_calls + ? { type: "function_call", id: fcId, call_id: reply.tool_calls[0].id, name: reply.tool_calls[0].function.name, arguments: reply.tool_calls[0].function.arguments, status: "completed" } + : { type: "message", id: msgId, role: "assistant", status: "completed", content: [{ type: "output_text", text: String(reply.content ?? ""), annotations: [] }] }; + if (parsed.stream) { + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache", connection: "keep-alive" }); + const ev = (event, data) => res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`); + ev("response.created", { type: "response.created", response: { id: rid, object: "response", status: "in_progress", output: [] } }); + ev("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { ...item, status: "in_progress", ...(reply.tool_calls ? { arguments: "" } : { content: [] }) } }); + if (reply.tool_calls) { + ev("response.function_call_arguments.delta", { type: "response.function_call_arguments.delta", item_id: fcId, output_index: 0, delta: reply.tool_calls[0].function.arguments }); + ev("response.function_call_arguments.done", { type: "response.function_call_arguments.done", item_id: fcId, output_index: 0, arguments: reply.tool_calls[0].function.arguments }); + } else { + ev("response.content_part.added", { type: "response.content_part.added", item_id: msgId, output_index: 0, content_index: 0, part: { type: "output_text", text: "", annotations: [] } }); + ev("response.output_text.delta", { type: "response.output_text.delta", item_id: msgId, output_index: 0, content_index: 0, delta: String(reply.content ?? "") }); + ev("response.output_text.done", { type: "response.output_text.done", item_id: msgId, output_index: 0, content_index: 0, text: String(reply.content ?? "") }); + ev("response.content_part.done", { type: "response.content_part.done", item_id: msgId, output_index: 0, content_index: 0, part: { type: "output_text", text: String(reply.content ?? ""), annotations: [] } }); + } + ev("response.output_item.done", { type: "response.output_item.done", output_index: 0, item }); + ev("response.completed", { type: "response.completed", response: { id: rid, object: "response", model: parsed.model ?? MODEL, status: "completed", output: [item], usage: { input_tokens: usageIn, output_tokens: reply.tool_calls ? 5 : estOut(String(reply.content ?? "")), total_tokens: usageIn + 10 } } }); + res.write("data: [DONE]\n\n"); + res.end(); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: rid, object: "response", status: "completed", model: parsed.model ?? MODEL, output: [item], usage: { input_tokens: usageIn, output_tokens: estOut(String(reply.content ?? "tool")) } })); + } + }); + return; + } res.writeHead(404, { "content-type": "application/json" }); res.end(JSON.stringify({ error: { message: "not found" } })); } catch (e) {