diff --git a/reference/architecture.md b/reference/architecture.md index bfe16b9e51..95675fb9c9 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -19,6 +19,7 @@ billion-context/ │ │ ├── context-window.ts # Context-window resolution (launcher model channel, beta headers) │ │ ├── dsh-compaction-guard.ts # Rejects dsh whole-prefix compaction replays (#1729) │ │ ├── handle.ts # Proxy request pipeline (route triage, compat relay, compress orchestration) (#1440 P2 cut 3) +│ │ ├── handoff-response.ts # Retains parsed assistant output for handoff export without changing request snapshots │ │ ├── headers.ts # Client-provided id sanitization for dump filenames (#286) │ │ ├── inject.ts # Per-wire tool-injection wrappers + FORCE_TEXT_PROTOCOL switch (#1440) │ │ ├── observability.ts # Body dumps (dumps/req-*.json, raw/*), unrecognized-path stats diff --git a/src/export.ts b/src/export.ts index 1b52781ae1..1b332de60b 100644 --- a/src/export.ts +++ b/src/export.ts @@ -1,6 +1,6 @@ import { existsSync, mkdirSync, writeFileSync } from "node:fs"; import path from "node:path"; -import { matchSession as matchSessionKernel, parseStoredPlaceholder, renderHandoff as renderHandoffKernel, type MessageContentStore } from "acp-kernel"; +import { matchSession as matchSessionKernel, parseStoredPlaceholder, renderHandoff as renderHandoffKernel, renderMessage, type MessageContentStore, type CoreMessage } from "acp-kernel"; import type { Session } from "./session.js"; import { SessionStore } from "./persist.js"; import { SqliteBackend, backendSelectionNotice, listBackendNames, openBackend, resolveBackendName, resolveMysqlDsn, selectedBackendRaw, type StorageBackend } from "./storage/backend.js"; @@ -82,12 +82,34 @@ function latestBlockTime(s: Session): number { return latest; } +/** Ends the reasoning cell inside a single assistant block. `renderMessage` + * emits `_reasoning_: …` with no terminator, so without it the handoff + * renderer folds the answer that follows into the collapsed thinking box. + * An HTML comment keeps the marker invisible in rendered Markdown. */ +export const HANDOFF_THINK_END = ""; + +function handoffCells(messages: CoreMessage[]): CoreMessage[] { + return messages.map((m) => m.contentType === "reasoning" + ? { ...m, text: (m.text ?? "").split("\n").map((line, index) => + index > 0 && line.trim() === HANDOFF_THINK_END ? line + "\n" + line : line + ).join("\n").trimEnd() + "\n" + HANDOFF_THINK_END } + : m); +} + export function renderHandoff(s: Session, full: boolean): string { + const response = s.metadata.handoffResponse as { request?: unknown; messages?: unknown } | undefined; + const reply = response?.request === s.stats.requests && Array.isArray(response.messages) && response.messages.every((m): m is CoreMessage => + m !== null && typeof m === "object" && typeof m.id === "string" && m.role === "assistant" + && ["text", "reasoning", "tool-call"].includes(m.contentType) + && [m.text, m.toolName, m.toolCallId].every((value) => value === undefined || typeof value === "string") + ) ? response.messages : []; const messages = s.lastMessages; + // Keep one assistant turn in arrival order, with explicit reasoning boundaries. + const latestReply = reply.length ? "\n### assistant\n\n" + handoffCells(reply).map(renderMessage).join("\n") + "\n" : ""; if (messages && messages.length > 0) { const folded = s.lastMessagesFolded === true; const base = renderHandoffKernel({ - coreMessages: messages, + coreMessages: handoffCells(messages), state: s.state, full, folded, @@ -103,7 +125,7 @@ export function renderHandoff(s: Session, full: boolean): string { ], }, }); - return base + blockSummariesSection(s, full && folded, base); + return base + latestReply + blockSummariesSection(s, full && folded, base); } // v2 fallback: no snapshot persisted. Block summaries (+ originals with @@ -122,7 +144,9 @@ export function renderHandoff(s: Session, full: boolean): string { lines.push(""); const active = s.state.blocks.filter((b) => b.active); if (active.length === 0) { - lines.push("No active compression blocks and no persisted conversation snapshot (v2 session file). Original messages are only persisted when they are compressed into a block, so this session's conversation content is not available for export."); + lines.push(reply.length + ? "No persisted request snapshot is available. Only the latest assistant response is included below." + : "No active compression blocks and no persisted conversation snapshot (v2 session file). Original messages are only persisted when they are compressed into a block, so this session's conversation content is not available for export."); lines.push(""); } for (const b of active) { @@ -146,7 +170,7 @@ export function renderHandoff(s: Session, full: boolean): string { lines.push("Paste the block summaries above into a new session to continue without the proxy."); lines.push(""); } - return lines.join("\n"); + return lines.join("\n") + latestReply; } // #845: the kernel handoff never emits state.blocks[].summary, and the persisted diff --git a/src/loop/core.ts b/src/loop/core.ts index a5e220734c..c3ec7008eb 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -4,6 +4,7 @@ import { type Config, type CoreMessage, } from "acp-kernel"; +import type { HandoffReply } from "../server/handoff-response.js"; import { estimateCoreMessages } from "../preflight.js"; import { estimateWireOverhead } from "../server/budget.js"; import { handleAcpStatus } from "../acp-status.js"; @@ -185,6 +186,7 @@ export interface LoopCtx { /** View handed to applyCompression (see RewriteCtx.compressMessages). */ compressMessages?: CoreMessage[]; session: Session; + reply?: HandoffReply; log: (msg: string) => void; proxyUrl?: string; /** #2809: diagnostic-only — the proxy the auto chain would have picked @@ -437,6 +439,13 @@ export async function* runCompressLoop( systemPrompt: string, signal?: AbortSignal, ): AsyncGenerator { + let replyPart = 0; + let replyKind: "text" | "reasoning" | undefined; + const recordReplyText = (kind: "text" | "reasoning", text: string) => { + if (replyKind !== kind) replyPart++; + replyKind = kind; + ctx.reply?.append(`part_${replyPart}`, kind, text); + }; let activeClearTimer: (() => void) | null = null; let currentUpstream = upstream; let roundBody: Record = requestBody; @@ -757,8 +766,10 @@ export async function* runCompressLoop( } tp.text += ev.delta; if (!ctx.textProtocol && ev.raw) { + recordReplyText("text", ev.delta); yield fwd(ev.raw, true); } else if (!ctx.textProtocol && round > 1 && ev.delta.length > 0) { + recordReplyText("text", ev.delta); yield fwd(adapter.emitText(ev.delta), true); } } else if (ev.kind === "reasoning") { @@ -781,8 +792,10 @@ export async function* runCompressLoop( if (ev.blockEnd) reasoningSealed = true; if (!ctx.textProtocol) { if (ev.raw) { + recordReplyText("reasoning", ev.delta); yield fwd(ev.raw); } else if (round > 1 && ev.delta.length > 0 && adapter.emitReasoning) { + recordReplyText("reasoning", ev.delta); yield fwd(adapter.emitReasoning(ev.delta)); } } @@ -792,6 +805,10 @@ export async function* runCompressLoop( roundParts.push({ kind: "standalone_signature", signature: ev.signature }); } else if (ev.kind === "tool_call") { calls.push({ name: ev.name, callId: ev.callId, arguments: ev.arguments, passthrough: ev.passthrough, signature: ev.signature }); + if (ev.passthrough) { + replyKind = undefined; + ctx.reply?.tool(`tool_${++replyPart}`, ev.name, ev.callId, ev.arguments); + } } else if (ev.kind === "usage") { usage = { inputTokens: ev.inputTokens, @@ -1139,6 +1156,7 @@ export async function* runCompressLoop( const functionCallIds = new Set(allCalls.map(c => c.callId)); if (ctx.textProtocol && resolvedText.length > 0) { + recordReplyText("text", resolvedText); yield adapter.emitText(resolvedText); } @@ -1389,6 +1407,8 @@ export async function* runCompressLoop( for (const tc of realToolCalls) { if (tc.passthrough) continue; + replyKind = undefined; + ctx.reply?.tool(`tool_${++replyPart}`, tc.name, tc.callId, tc.arguments); yield adapter.emitToolCall(tc); } @@ -1414,6 +1434,7 @@ export async function* runCompressLoop( yield adapter.emitError(msg); return; } + if (!truncatedDone && streamError === undefined && finishReason !== "failed" && finishReason !== "incomplete" && finishReason !== "error" && !signal?.aborted) ctx.reply?.save(); // A passthrough round already streamed the upstream's own finish // chunk + [DONE] verbatim (original id + order); re-emitting a // regenerated completion would duplicate them. diff --git a/src/plugin.ts b/src/plugin.ts index 55024362d1..39bb9ec0dd 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -26,6 +26,8 @@ import { ccrEnabled, ccrLoopConfig, contentStoreOf, retrieveToolName } from "./s import { emitStreamError, emitUpstreamTruncation } from "./stream-error.js"; import type { TruncationDiag } from "./stream-error.js"; import { recordAttributionReport } from "./server/attribution-status.js"; +import type { HandoffReply } from "./server/handoff-response.js"; +import { captureHandoffJson, HANDOFF_REPLY_MAX_BYTES } from "./server/handoff-response.js"; /** #2328 Q2: what the pipes know about the upstream HTTP response whose body * they are piping — threaded in by server.ts so the truncation diag can cite @@ -2379,11 +2381,27 @@ export async function pipePluginChatWithStrip( // stay source-compatible; the plugin lane's resolveFakeCompletion refetch // swallows the original response object, hence optional. upstreamMeta?: UpstreamMeta, + reply?: HandoffReply, ): Promise { let reader = stream.getReader(); let decoder = new TextDecoder("utf-8"); let buf = ""; const acc: UsageSample = {}; + let replyPart = 0; + let lastReplyEntry = ""; + let replyTool = 0; + const appendReply = (field: string, index: number, text: string) => { + if (!reply || !text || (protocol !== "anthropic" && index !== 0)) return; + const base = `${index}:${field}`; + if (lastReplyEntry !== base) replyPart++; + lastReplyEntry = base; + reply.append(`text_${replyPart}`, field === "content" || field === "text" ? "text" : "reasoning", text); + }; + const setReplyTool = (key: string, name: string, id: string, args: string, start = false) => { + if (start) lastReplyEntry = ""; + reply?.tool(key, name, id, args); + }; + let sawReplyError = false; let sawStrippedEcho = false; // #2405(c): one warn line per event, session-prefixed — the former second // write through the request logger landed in the SAME tee file, doubling @@ -2723,6 +2741,8 @@ export async function pipePluginChatWithStrip( const tail = s.filter.flush(); if (tail.length > 0) { out += syntheticTail(s, tail); + const replyIndex = protocol === "anthropic" ? s.index + (inRetry ? retryIndexOffset : 0) : s.index; + appendReply(s.field, replyIndex, tail); proseAcc += tail; if (s.field === "content" || s.field === "text") { visibleTextChars += tail.length; @@ -2879,7 +2899,9 @@ export async function pipePluginChatWithStrip( const t = tcf as Record; const tIdx = typeof t["index"] === "number" ? t["index"] : ti; const key = `${ci}:${tIdx}`; + const firstToolFragment = !seenToolCalls.has(key); const accTc = seenToolCalls.get(key) ?? { label: `idx=${tIdx}`, id: "", name: "", argsLen: 0, frags: 0, args: "" }; + if (ci === 0 && firstToolFragment) setReplyTool(key, "", "", "", true); if (typeof t["id"] === "string" && t["id"]) accTc.id = t["id"]; const fn = t["function"]; if (fn && typeof fn === "object") { @@ -2892,6 +2914,7 @@ export async function pipePluginChatWithStrip( } accTc.frags++; seenToolCalls.set(key, accTc); + if (ci === 0) setReplyTool(key, accTc.name, accTc.id, accTc.args); } } } @@ -2913,6 +2936,7 @@ export async function pipePluginChatWithStrip( if (!mayStartRenderTag(v) && !mayStartMarkerLine(v) && !mayStartBiliInternal(v) && !mayStartDegenerateRenderTag(v) && !(absorbInstructed === true && mayStartToolCallEmission(v)) && !anyPending()) { if (v.length > 0) { keptText = true; + appendReply(field, typeof ch?.["index"] === "number" ? ch["index"] as number : ci, v); proseAcc += v; // #2190: residue audit — bytes that bypassed the state // machine must be observable, not silent. @@ -2938,6 +2962,7 @@ export async function pipePluginChatWithStrip( proseAcc += tail; } } + appendReply(field, typeof ch?.["index"] === "number" ? ch["index"] as number : ci, clean); if (clean.length === 0) droppedText = true; else { keptText = true; @@ -3015,8 +3040,11 @@ export async function pipePluginChatWithStrip( frags: 1, args: "", }); + setReplyTool(`block:${blockIndex}`, tcName, tcId, JSON.stringify(cb?.["input"] ?? {}), true); if (tcName === COMPRESS_TOOL_NAME) heldCompressBlocks.set(blockIndex, { id: tcId, frags: 0, json: "" }); } else if (bt === "thinking" || bt === "redacted_thinking") sawThinking = true; + const field = bt === "thinking" ? "thinking" : bt === "text" ? "text" : undefined; + if (field && typeof cb?.[field] === "string") appendReply(field, typeof ev["index"] === "number" ? ev["index"] : 0, cb[field]); // #2248: raw exit — content_block_start payloads never enter the filter. auditRawForward(rawEvent); return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n"; @@ -3028,6 +3056,7 @@ export async function pipePluginChatWithStrip( heldCompressBlocks.delete(stopIndex); const salvage = salvageCompressArgs(held.json, held.id || `block=${stopIndex}`); loggerLog(salvage.action === "verbatim" ? "debug" : "warn", `[plugin] compress tool args ${salvage.action}: ${held.json.length} char(s), ${held.frags} fragment(s), ${salvage.ranges} range(s)${salvage.kind ? `, kind=${salvage.kind}` : ""} (#2580)`); + setReplyTool(`block:${stopIndex}`, COMPRESS_TOOL_NAME, held.id, salvage.out); return compressDeltaFrame(stopIndex, salvage.out) + rawEvent + "\n\n"; } } @@ -3039,6 +3068,7 @@ export async function pipePluginChatWithStrip( heldCompressBlocks.delete(idx); const salvage = salvageCompressArgs(held.json, held.id || `block=${idx}`); loggerLog("warn", `[plugin] compress tool args flushed at terminal without content_block_stop (${salvage.action}, ${held.json.length} char(s)) (#2580)`); + setReplyTool(`block:${idx}`, COMPRESS_TOOL_NAME, held.id, salvage.out); pre += compressDeltaFrame(idx, salvage.out); } return pre + rawEvent + "\n\n"; @@ -3059,6 +3089,7 @@ export async function pipePluginChatWithStrip( if (accTc && typeof d["partial_json"] === "string") { accTc.argsLen += d["partial_json"].length; accTc.args += d["partial_json"]; + setReplyTool(`block:${index}`, accTc.name, accTc.id, accTc.args); } const held = heldCompressBlocks.get(index); if (held && typeof d["partial_json"] === "string") { @@ -3081,6 +3112,7 @@ export async function pipePluginChatWithStrip( // field, so the fast path must not pre-empt it. if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending()) { if (raw.length > 0) { + appendReply(field, index, raw); proseAcc += raw; // #2190: residue audit — see the twin above. if (containsEchoResidue(raw)) loggerLog("warn", `[tag-echo] fast path forwarded echo-residue-shaped bytes (#2190): ${raw.slice(0, 80).replace(/\n/g, " ")}`); @@ -3092,6 +3124,7 @@ export async function pipePluginChatWithStrip( return rawEvent + "\n\n"; } const [clean, changed] = pushField(field, index, raw); + appendReply(field, index, clean); if (!changed) { if (field === "text" && raw.length > 0) { visibleTextChars += raw.length; @@ -3173,6 +3206,9 @@ export async function pipePluginChatWithStrip( frags: 1, args: fcArgs, }); + if (protocol !== "google" || ci === 0) { + setReplyTool(`google_${++replyTool}`, fcObj && typeof fcObj["name"] === "string" ? fcObj["name"] : "", typeof fcObj?.["id"] === "string" ? fcObj["id"] : "", fcArgs, true); + } } if (p["thought"] === true) sawThinking = true; const raw = p["text"]; @@ -3189,6 +3225,7 @@ export async function pipePluginChatWithStrip( if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending()) { if (raw.length > 0) { keptText = true; + appendReply(field, index, raw); proseAcc += raw; // #2190: residue audit — see the twin above. if (containsEchoResidue(raw)) loggerLog("warn", `[tag-echo] fast path forwarded echo-residue-shaped bytes (#2190): ${raw.slice(0, 80).replace(/\n/g, " ")}`); @@ -3214,6 +3251,7 @@ export async function pipePluginChatWithStrip( proseAcc += tail; } } + appendReply(field, index, clean); if (clean.length === 0) droppedText = true; else { keptText = true; @@ -3346,6 +3384,7 @@ export async function pipePluginChatWithStrip( diagPushType(typeof fr === "string" ? `chunk:finish=${fr}` : "chunk"); } else diagPushType("frame"); if (retryFraming(ev)) continue; + if (ev["error"] || ev["type"] === "error") sawReplyError = true; if (protocol === "anthropic" && ev["type"] === "content_block_start") blocksForwarded++; // After the retryFraming gate: only the first attempt's // message_start reaches here (the retry's was consumed). @@ -3433,7 +3472,9 @@ export async function pipePluginChatWithStrip( maybeWarnNamelessToolCalls(); maybeSummarizeStrips(); settleWitnesses(); + if (sawTerminal && !sawReplyError && !res.destroyed && !res.writableEnded) reply?.save(); if (truncated) { + reply?.discard(); // #721 → #2563: the client still owns the stream from here. // Permitlist hosts (pi) get the raw cut — their native layer // classifies it as transient and retries; everyone else gets the @@ -3588,11 +3629,33 @@ export async function pipePluginResponsesWithStrip( requestText?: string, // #2328 Q2 — see pipePluginChatWithStrip. upstreamMeta?: UpstreamMeta, + reply?: HandoffReply, ): Promise { let reader = stream.getReader(); let decoder = new TextDecoder("utf-8"); let buf = ""; const acc: UsageSample = {}; + const replyItems = new Map>(); + let replyBytes = 0; + let replyDropped = false; + const appendReplyDelta = (meta: Record, text: string, reasoning = false) => { + if (!reply || replyDropped || !text) return; + const index = Number(meta["output_index"] ?? 0); + const existing = replyItems.get(index); + replyBytes += Buffer.byteLength(text) + (existing ? 0 : 128); + if (replyBytes > HANDOFF_REPLY_MAX_BYTES) { replyDropped = true; replyItems.clear(); reply.discard(); return; } + const item = existing ?? (reasoning ? { type: "reasoning", summary: [] } : { type: "message", role: "assistant", content: [] }); + const field = reasoning ? "summary" : "content"; + const parts = (Array.isArray(item[field]) ? item[field] : []) as Array<{ type: string; text: string }>; + const at = Number(meta[reasoning ? "summary_index" : "content_index"] ?? 0); + if (!Number.isInteger(at) || at < 0 || at > 10000) { replyDropped = true; replyItems.clear(); reply.discard(); return; } + const part = parts[at] ?? { type: reasoning ? "summary_text" : "output_text", text: "" }; + part.text += text; + parts[at] = part; + item[field] = parts; + replyItems.set(index, item); + }; + let replyCompleted = false; // #2328 Q2/Q4 — see the chat-pipe twin. let diagEvents = 0; let diagBytes = 0; @@ -3788,6 +3851,7 @@ export async function pipePluginResponsesWithStrip( visibleTextChars += tail.length; answerProse += tail; const meta = inRetry && heldItemId !== undefined ? { item_id: heldItemId, output_index: heldOutputIndex } : (lastDeltaMeta ?? {}); + appendReplyDelta(meta, tail); return `data: ${JSON.stringify({ type: "response.output_text.delta", ...meta, delta: tail })}\n\n` + after; } return after; @@ -4120,6 +4184,7 @@ export async function pipePluginResponsesWithStrip( let rebuild = hadEchoText || retryRewritePending(); if (rebuild) evOut = stripResponsesText(ev, dropEmission, requestText); rewriteRetryIds(evOut); + if (reply && type === "response.output_item.done" && evOut["item"] && typeof evOut["item"] === "object") replyItems.set(Number(evOut["output_index"] ?? 0), evOut["item"] as Record); const doneText = responsesEventText(evOut, true); heldVisibleChars += doneText.length; answerProse += doneText; @@ -4154,6 +4219,7 @@ export async function pipePluginResponsesWithStrip( argStreams.clear(); heldEvents = []; heldVisibleChars = 0; + replyItems.clear(); continue; } const tailFrame = flushTail(""); @@ -4175,6 +4241,12 @@ export async function pipePluginResponsesWithStrip( let rebuild = hadEchoText || retryRewritePending(); if (rebuild) evOut = stripResponsesText(ev, dropEmission, requestText); rewriteRetryIds(evOut); + if (type === "response.completed") { + const response = evOut["response"] as Record | undefined; + const output = Array.isArray(response?.["output"]) && response["output"].length ? response["output"] : [...replyItems.entries()].sort(([a], [b]) => a - b).map(([, item]) => item); + captureHandoffJson(reply, "responses", { ...response, output }); + replyCompleted = true; + } else reply?.discard(); // Hosts finish on this frame, before EOF settles billing and late usage. if (session && acc.inputTokens !== undefined && acc.inputTokens > 0) { const credit = session.stats.compressCreditTokens ?? 0; @@ -4198,6 +4270,7 @@ export async function pipePluginResponsesWithStrip( visibleTextChars += delta.length; fastPathChars += delta.length; answerProse += delta; + appendReplyDelta(ev, delta); // #2190: residue audit — see the twin above. if (delta.length > 0 && containsEchoResidue(delta)) loggerLog("warn", `[tag-echo] fast path forwarded echo-residue-shaped bytes (#2190): ${delta.slice(0, 80).replace(/\n/g, " ")}`); await write(rawEvent + "\n\n"); @@ -4216,6 +4289,7 @@ export async function pipePluginResponsesWithStrip( visibleTextChars += clean.length; proseAcc += clean; answerProse += clean; + appendReplyDelta(ev, clean); if (clean === delta && !retryRewritePending()) { await write(rawEvent + "\n\n"); continue; @@ -4232,6 +4306,7 @@ export async function pipePluginResponsesWithStrip( // generic rawEvent write below. if (type === "response.reasoning_summary_text.delta" && typeof ev["delta"] === "string") { const v = ev["delta"] as string; + appendReplyDelta(ev, v, true); if (v.length > 0) proseAcc += v; await write(rawEvent + "\n\n"); continue; @@ -4258,6 +4333,7 @@ export async function pipePluginResponsesWithStrip( maybeSummarizeStrips(); settleUsage(); settleWitnesses(); + if (replyCompleted && !res.destroyed && !res.writableEnded) reply?.save(); // #721 → #2563: never leave the stream hanging on a missing done-family // event. Permitlist hosts (pi) get the raw cut for their native // classifier; everyone else gets the in-band error frame. @@ -4368,6 +4444,7 @@ export async function pipePluginJson( absorbInstructed?: boolean, // m00885: echoed (user-requested verbatim) emission spans survive. requestText?: string, + reply?: HandoffReply, ): Promise { // Also serves proxy-mode JSON responses that skipped compress injection // (#460 residual) — pass no session there so usage accounting stays off. @@ -4452,6 +4529,10 @@ export async function pipePluginJson( : stripOpenaiChatText(json, dropEmission, requestText); mutated = true; } + if (json && !res.destroyed) { + captureHandoffJson(reply, protocol, json); + reply?.save(); + } if (mutated && json) { res.end(Buffer.from(JSON.stringify(json), "utf8")); return; diff --git a/src/server/handoff-response.ts b/src/server/handoff-response.ts new file mode 100644 index 0000000000..8cc2dbb56e --- /dev/null +++ b/src/server/handoff-response.ts @@ -0,0 +1,68 @@ +import type { CoreMessage } from "acp-kernel"; +import { anthropicToCore, googleToCore, openaiToCore, responsesToCore } from "acp-kernel/wire"; +import { markDirty, type Session } from "../session.js"; +import { log as loggerLog } from "../logger.js"; + +export const HANDOFF_REPLY_MAX_BYTES = 1_500_000; + +export type HandoffReply = ReturnType; + +export function createHandoffReply(session: Session, canSave: () => boolean = () => true) { + const request = session.stats.requests; + const snapshot = session.lastMessages; + const messages = new Map(); + let bytes = 0; + let discarded = false; + const discard = () => { discarded = true; messages.clear(); }; + const put = (key: string, message: CoreMessage) => { + if (discarded) return; + const previous = messages.get(key); + const size = (m: CoreMessage) => Buffer.byteLength(m.text ?? "") + Buffer.byteLength(m.toolName ?? "") + Buffer.byteLength(m.toolCallId ?? "") + 128; + bytes += size(message) - (previous ? size(previous) : 0); + if (bytes > HANDOFF_REPLY_MAX_BYTES) { discard(); return; } + messages.set(key, message); + }; + return { + append(key: string, contentType: "text" | "reasoning", text: string) { + if (!text || discarded) return; + put(key, { id: `reply_${key}`, role: "assistant", contentType, text: (messages.get(key)?.text ?? "") + text }); + }, + tool(key: string, toolName: string, toolCallId: string, text: string) { + put(key, { id: `reply_${key}`, role: "assistant", contentType: "tool-call", toolName, toolCallId, text }); + }, + replace(next: CoreMessage[]) { + messages.clear(); + bytes = 0; + for (const [i, m] of next.entries()) put(String(i), { id: `reply_${i}`, role: "assistant", contentType: m.contentType, text: m.text, toolName: m.toolName, toolCallId: m.toolCallId }); + }, + discard, + save() { + if (discarded || !messages.size || session.stats.requests !== request || session.lastMessages !== snapshot || !canSave()) return; + session.metadata.handoffResponse = { request, messages: [...messages.values()] }; + markDirty(session); + }, + }; +} + +export function captureHandoffJson(reply: HandoffReply | undefined, protocol: string | undefined, json: Record): void { + if (!reply) return; + try { + if (json.error || (protocol === "responses" && json.status && json.status !== "completed")) { reply.discard(); return; } + let messages: CoreMessage[]; + if (protocol === "openai") { + const message = (json.choices as Array<{ message?: unknown }> | undefined)?.[0]?.message; + messages = openaiToCore({ messages: [message] } as Parameters[0]).msgs; + } else if (protocol === "anthropic") { + messages = anthropicToCore({ messages: [json] } as Parameters[0]).msgs; + } else if (protocol === "responses") { + messages = responsesToCore({ input: json.output } as Parameters[0]).msgs; + } else if (protocol === "google") { + const content = (json.candidates as Array<{ content?: unknown }> | undefined)?.[0]?.content; + messages = googleToCore({ contents: [content] } as Parameters[0]).msgs; + } else return; + reply.replace(messages.filter((m) => m.role === "assistant")); + } catch (err) { + loggerLog("debug", `[handoff] JSON reply capture skipped (codec failed for protocol ${protocol ?? "unknown"}): ${err instanceof Error ? err.message : String(err)}`); + reply.discard(); + } +} diff --git a/src/server/relay.ts b/src/server/relay.ts index 93281df4cb..f9362f9818 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -1,4 +1,5 @@ import { fetchWithCodexCatalogCache, isCodexCatalogRequest } from "./codex-catalog-cache.js"; +import { createHandoffReply, captureHandoffJson } from "./handoff-response.js"; import { checkOutboundBody, OutboundBodyTooLargeError } from "../request-body-budget.js"; import { maybeRunInvariantJudge } from "../invariant-judge.js"; // #1440 P2 cut 4: upstream relay zone (forward) extracted verbatim from src/server.ts — pure move, zero behavior change. @@ -267,6 +268,7 @@ async function forwardCaptured( v1Meta?: Record, ): Promise { const forwardStartedAt = Date.now(); + const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session, () => !res.destroyed) : undefined; // E2: a codex native-compaction request intercepted in prepare() carries a // forged success response — serve it without contacting upstream. if (prepared?.codexForge) { @@ -1776,6 +1778,7 @@ async function forwardCaptured( absorbInstructed, wireBodyText, upstreamMeta, + reply, ); } else { // #732/#821: the plugin pipe re-issues the agent's own body @@ -1803,10 +1806,11 @@ async function forwardCaptured( absorbInstructed, wireBodyText, upstreamMeta, + reply, ); } } else { - await pipePluginJson(pluginBody, res, prepared.session, prepared.protocol, targetOrigin, absorbInstructed, wireBodyText); + await pipePluginJson(pluginBody, res, prepared.session, prepared.protocol, targetOrigin, absorbInstructed, wireBodyText, reply); } } finally { clearUpstreamTimer(); @@ -1935,6 +1939,7 @@ async function forwardCaptured( undefined, undefined, upstreamMeta, + reply, ); } else { await pipeThrough(toClient, res); @@ -1979,6 +1984,7 @@ async function forwardCaptured( undefined, undefined, upstreamMeta, + reply, ); } else { await pipePluginChatWithStrip( @@ -2003,6 +2009,7 @@ async function forwardCaptured( undefined, undefined, upstreamMeta, + reply, ); } } else if ( @@ -2014,7 +2021,7 @@ async function forwardCaptured( // so a non-injected JSON response must not hand the model's echoes // back untouched. Same pipe as plugin mode; no session, so usage // accounting stays off for the same reason as above. - await pipePluginJson(responseBody, res, undefined, prepared.protocol); + await pipePluginJson(responseBody, res, undefined, prepared.protocol, undefined, undefined, undefined, reply); } else { await pipeThrough(responseBody, res); } @@ -2149,7 +2156,7 @@ async function forwardCaptured( const loopBillingUpstream = route?.rewrittenUrl ?? (/^https?:\/\//i.test(req.url ?? "") ? req.url ?? undefined : opts.upstream); const loop = runCompressLoop( responseBody, - { core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl, skippedProxy, upstreamOrigin: targetOrigin, protocol: prepared.protocol, textProtocol, debug: opts.debug, refreshFolded, visibilityMarkers, dumpSse: loopDumpDir ? (name, stream) => dumpStreamToFile(stream, loopDumpDir, name) : undefined, imageLearn: { host: upstreamHost(loopBillingUpstream), fp: `${imageBillingFor(opts, loopBillingUpstream)}:${imageTokenCapFor(opts, loopBillingUpstream)}` } }, + { core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, reply, log: ctx.log, proxyUrl, skippedProxy, upstreamOrigin: targetOrigin, protocol: prepared.protocol, textProtocol, debug: opts.debug, refreshFolded, visibilityMarkers, dumpSse: loopDumpDir ? (name, stream) => dumpStreamToFile(stream, loopDumpDir, name) : undefined, imageLearn: { host: upstreamHost(loopBillingUpstream), fp: `${imageBillingFor(opts, loopBillingUpstream)}:${imageTokenCapFor(opts, loopBillingUpstream)}` } }, parsedReq, // #3280: egress goes out enveloped via wireTransform, so every // round-2+ response comes back enveloped too — unwrap at the loop's @@ -2263,6 +2270,8 @@ async function forwardCaptured( } else { await withSessionLock(prepared.session, () => rewriteJsonResponseAsync(json, ctx, clientAbort.signal)); } + captureHandoffJson(reply, prepared.protocol, json); + reply?.save(); res.end(JSON.stringify(json)); } catch { res.end(text); diff --git a/src/session.ts b/src/session.ts index 5d581f2bab..278a75effe 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1369,6 +1369,7 @@ export function snapshotMessages(session: Session, messages: CoreMessage[]): voi if (messages.length > 0) { session.lastMessages = messages; session.lastMessagesFolded = false; + delete session.metadata.handoffResponse; } } diff --git a/src/web/client.ts b/src/web/client.ts index 0a719695ba..7556a9efb2 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -2107,20 +2107,33 @@ function changelogCatOf(rawLine) { if (lp < 0) return null; return { kind: kind, name: nameId.slice(0, lp), cid: nameId.slice(lp + 1), text: line.slice(mark + tailLen) }; } + // Terminates the reasoning cell of a reply: _reasoning_: has no end + // marker, so the handoff renderer would otherwise fold the answer that + // follows into the collapsed thinking box. Mirrored in HANDOFF_THINK_END. + const HANDOFF_THINK_END = ""; function splitHandoffBody(bodyLines) { const segs = []; - for (const raw of bodyLines) { + let thinkClosed = false; + for (let i = 0; i < bodyLines.length; i++) { + const raw = bodyLines[i]; if (raw.trim() === "" && !(segs.length && segs[segs.length - 1].type === "out")) continue; + const activeThink = segs[segs.length - 1]; + if (raw.trim() === HANDOFF_THINK_END && activeThink && activeThink.type === "think" && !thinkClosed) { + if (bodyLines[i + 1] === raw) { activeThink.lines.push(raw); i++; } + else thinkClosed = true; + continue; + } const tool = parseToolLine(raw); if (tool) { segs.push({ type: "tool", kind: tool.kind, name: tool.name, cid: tool.cid, lines: [tool.text] }); continue; } if (raw.indexOf("_reasoning_: ") === 0) { const lastT = segs[segs.length - 1]; - if (!lastT || lastT.type !== "think") segs.push({ type: "think", lines: [] }); + if (!lastT || lastT.type !== "think" || thinkClosed) segs.push({ type: "think", lines: [] }); segs[segs.length - 1].lines.push(raw.slice("_reasoning_: ".length)); + thinkClosed = false; continue; } const last = segs[segs.length - 1]; - if (last && (last.type === "out" || last.type === "think")) last.lines.push(raw); + if (last && (last.type === "out" || (last.type === "think" && !thinkClosed))) last.lines.push(raw); else segs.push({ type: "out", lines: [raw] }); } return segs; diff --git a/tests/cli-export.test.ts b/tests/cli-export.test.ts index 8eaea44ec4..132437b08b 100644 --- a/tests/cli-export.test.ts +++ b/tests/cli-export.test.ts @@ -5,7 +5,7 @@ import { tmpdir } from "node:os"; import path from "node:path"; import { SessionStore } from "../src/persist.ts"; import { Session, cacheBlockContent } from "../src/session.ts"; -import { exportSession, listSessions } from "../src/export.ts"; +import { exportSession, listSessions, renderHandoff } from "../src/export.ts"; import { parseArgs } from "../src/cli.ts"; import { createInitialState } from "acp-kernel"; import { rmrf } from "./tmp-rm.ts"; @@ -71,10 +71,62 @@ test("bili export lists sessions and renders a handoff doc with summaries and or await assert.rejects(() => exportSession("nope", { dir }), /no session matches/); } finally { + store.closeQuietly(); rmrf(dir); } }); +test("handoff preserves the latest tool call outside request pruning and across storage reloads", async () => { + const previous = process.env.BILI_STORAGE_UNIFIED; + try { + for (const mode of ["legacy", "full"]) { + process.env.BILI_STORAGE_UNIFIED = mode; + const dir = mkdtempSync(path.join(tmpdir(), "bili-handoff-reply-")); + const store = new SessionStore({ dir, enabled: true, debounceMs: 0 }); + try { + const s = makeSession("latest-tool", "Latest reply", undefined); + s.lastMessages = [{ id: "request", role: "user", contentType: "text", text: "read config" }]; + s.metadata.handoffResponse = { + request: s.stats.requests, + messages: [{ id: "reply", role: "assistant", contentType: "tool-call", toolName: "read", toolCallId: "call1", text: '{"path":"config.ts"}' }], + }; + const before = JSON.stringify(s.lastMessages); + for (const full of [false, true]) { + const md = renderHandoff(s, full); + assert.match(md, /### assistant\n/); + assert.match(md, /read\(call1\)/); + assert.match(md, /config\.ts/); + assert.match(md, /1 (?:client )?messages/); + } + assert.equal(JSON.stringify(s.lastMessages), before, "export does not alter the request snapshot"); + await store.writeNow(s); + const restored = store.loadSync(s.id); + assert.ok(restored); + assert.match(renderHandoff(restored, false), /read\(call1\)/); + restored.stats.requests++; + assert.doesNotMatch(renderHandoff(restored, false), /read\(call1\)/); + } finally { + store.closeQuietly(); + rmrf(dir); + } + } + } finally { + if (previous === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = previous; + } +}); + +test("handoff ignores malformed reply metadata and exports a reply without a request snapshot", () => { + const s = makeSession("reply-only", "Reply only", undefined); + s.metadata.handoffResponse = { request: s.stats.requests, messages: [{ id: "reply", role: "assistant", contentType: "text", text: "latest answer" }] }; + assert.match(renderHandoff(s, false), /latest answer/); + for (const messages of [[null], [{ role: "assistant", contentType: "text", text: {} }]]) { + s.metadata.handoffResponse = { request: s.stats.requests, messages }; + assert.doesNotThrow(() => renderHandoff(s, false)); + assert.doesNotMatch(renderHandoff(s, false), /latest answer|### assistant/); + } +}); + test("parseArgs recognizes bili export forms", () => { const a = parseArgs(["export"]); assert.equal(a.command, "export"); diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 43e74c9457..5974c371de 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "0a241435d456ba994c92b0072d2173bf2a6d00ee73223458fb4302e170efb1e3", - "stateDigestAligned": "c7079cd68bf9eb0075c3844a5cb1ae54ff6ecad8a43da424a7ffebd033eceb7f", + "stateDigest": "f4d5bfb97153ec99041c16888125a941e8b2657dc720a866a4099fa3b23fff5b", + "stateDigestAligned": "fc2df28feea1891b28a3a24ee966534443f83ee588d7aa3412bf0508ff82b379", "sectionDigests": { "state": "f93dc1c876c672d4", "blockContents": "dc7d13d250c94bb4", "stats": "c6f1d434975d047d", - "metadata": "16cad648ccf41950" + "metadata": "b8ac4625e095b31b" }, "stateKeyDigests": { "blocks": "3a454b9a16e4f0b9", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 79d5eb6b70..c9c8916631 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "9f1b8258a916fcf63af14625def9ed0236eb8ec9c542f0027206985598d2a5b8", - "stateDigestAligned": "86e45900d61e81f0c8239bfe4b4019451b600c2a5258e89e53c68bc09fd2d5ca", + "stateDigest": "5b21dc73366b4d9f635eadc5c760820d2f4c5949d173eb14d9b8f82121359bae", + "stateDigestAligned": "83a8d4c007385d59e926368e454f0a1c74ca6a74da300f382a424e3a1c3432ab", "sectionDigests": { "state": "8390f21336885863", "blockContents": "94a58f96d7c5cfc3", "stats": "24d705b0e39bd13c", - "metadata": "2198f47054c8acae" + "metadata": "608c588a7c3c55ff" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index a0fa7d5830..c8582a2597 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "c4979d4f58cfa49bc8be8c43b8e1f1b0861b3b9d44cdc415ee09e4ca21d9aa5a", - "stateDigestAligned": "43e4aa88c25bcb08f0435e60e0e37270061479086893d8148749d09c839db65b", + "stateDigest": "39b79a4e9d4d1a133290e92882d43147c7ed9a55590fd373aca88488c276a527", + "stateDigestAligned": "c5aa77d58420ac25a41668aea343a300a9db0b983b60d0e9ab0ec864d251bf8d", "sectionDigests": { "state": "2e1def3a3234afdc", "blockContents": "473264d9df8ba338", "stats": "900481685c848462", - "metadata": "939dc0c09b6cbcbf" + "metadata": "8b1393bbafbc1f70" }, "stateKeyDigests": { "blocks": "0cfd0b9c0fc53073", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 3ece8b999d..9f4e2d426e 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "b3bddfa75ce3f8b94424be9a0164c1b9a183faa9daf705e6175cabdd671c95ff", - "stateDigestAligned": "ae78cdd8a1784474f1ffa5e9c8e8871871e4498b75df568b614a977e60af2339", + "stateDigest": "751bb961c1e95f77f16444872b8b1e616d659b4d4023ba84e2890af66dcaa024", + "stateDigestAligned": "5100f87feb53586ee44dfb3df279056cc678505dc6557296931d5d6d96e09a5d", "sectionDigests": { "state": "db313e8eba2d65fc", "blockContents": "b6701268a0bdcd8a", "stats": "327d3967b8371aa8", - "metadata": "a9eecd3816738158" + "metadata": "d79cf65292d64a9c" }, "stateKeyDigests": { "blocks": "34b0dc17f8c24595", diff --git a/tests/handoff-response.test.ts b/tests/handoff-response.test.ts new file mode 100644 index 0000000000..d299cde36e --- /dev/null +++ b/tests/handoff-response.test.ts @@ -0,0 +1,261 @@ +import assert from "node:assert/strict"; +import { createServer } from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import type { CoreMessage } from "acp-kernel"; +import { createSession, snapshotMessages, type Session } from "../src/session.ts"; +import { createHandoffReply } from "../src/server/handoff-response.ts"; +import { pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip } from "../src/plugin.ts"; + +type Protocol = "openai" | "anthropic" | "responses" | "google"; + +async function serve(protocol: string, stream: boolean, chunks: Array, opts: { error?: boolean; abort?: boolean; advance?: (session: Session) => void; capture?: boolean } = {}) { + const session = createSession(`handoff-test-${Math.random()}`); + session.stats.requests = 1; + const server = createServer((_req, res) => { + const reply = opts.capture === false ? undefined : createHandoffReply(session, () => !opts.abort && !res.destroyed); + const input = new ReadableStream({ start(controller) { + for (const chunk of chunks) controller.enqueue(Buffer.from(chunk)); + if (opts.error) controller.enqueue(Buffer.from(event({ type: "error", error: { message: "failed" } }))); + opts.advance?.(session); + controller.close(); + } }); + const wireProtocol = protocol as Protocol; + const run = !stream ? pipePluginJson(input, res, undefined, wireProtocol, undefined, undefined, undefined, reply) + : protocol === "responses" ? pipePluginResponsesWithStrip(input, res, undefined, undefined, undefined, undefined, undefined, undefined, undefined, reply) + : pipePluginChatWithStrip(input, res, wireProtocol, undefined, undefined, undefined, undefined, undefined, undefined, undefined, reply); + void run.catch((error: unknown) => res.destroy(error instanceof Error ? error : new Error(String(error)))); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert(address && typeof address !== "string"); + try { + const response = await fetch(`http://127.0.0.1:${address.port}`); + return { wire: Buffer.from(await response.arrayBuffer()), session }; + } finally { + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + } +} + +function event(value: unknown, ending = "\r\n\r\n"): string { + return `data: ${JSON.stringify(value)}${ending}`; +} + +const cases: Array<{ protocol: Protocol; stream: string[]; json: string; args: string }> = [ + { + protocol: "openai", + stream: [event({ choices: [{ delta: { content: "hé", tool_calls: [{ index: 0, id: "call_1", function: { name: "look", arguments: "{\"q\":" } }] } }] }), event({ choices: [{ delta: { content: "llo", tool_calls: [{ index: 0, function: { arguments: "\"x\"}" } }] }, finish_reason: "tool_calls" }] }), "data: [DONE]\r\n\r\n"], + json: JSON.stringify({ choices: [{ message: { role: "assistant", content: "héllo", tool_calls: [{ id: "call_1", type: "function", function: { name: "look", arguments: "{\"q\":\"x\"}" } }] } }] }), + args: "{\"q\":\"x\"}", + }, + { + protocol: "anthropic", + stream: [event({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), event({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "héllo" } }), event({ type: "content_block_start", index: 1, content_block: { type: "tool_use", id: "tool_1", name: "look", input: {} } }), event({ type: "content_block_delta", index: 1, delta: { type: "input_json_delta", partial_json: "{\"q\":\"x\"}" } }), event({ type: "message_stop" })], + json: JSON.stringify({ role: "assistant", content: [{ type: "text", text: "héllo" }, { type: "tool_use", id: "tool_1", name: "look", input: { q: "x" } }] }), + args: "{\"q\":\"x\"}", + }, + { + protocol: "responses", + stream: [event({ type: "response.output_text.delta", delta: "héllo" }), event({ type: "response.output_item.done", output_index: 0, item: { type: "function_call", call_id: "fc_1", name: "look", arguments: "{\"q\":\"x\"}" } }), event({ type: "response.completed", response: { output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "héllo" }] }, { type: "function_call", call_id: "fc_1", name: "look", arguments: "{\"q\":\"x\"}" }] } })], + json: JSON.stringify({ output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "héllo" }] }, { type: "function_call", call_id: "fc_1", name: "look", arguments: "{\"q\":\"x\"}" }] }), + args: "{\"q\":\"x\"}", + }, + { + protocol: "google", + stream: [event({ candidates: [{ content: { role: "model", parts: [{ text: "hé" }] } }] }), event({ candidates: [{ content: { role: "model", parts: [{ text: "llo" }, { functionCall: { name: "look", args: { q: "x" } } }] }, finishReason: "STOP" }] })], + json: JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: "héllo" }, { functionCall: { name: "look", args: { q: "x" } } }] }, finishReason: "STOP" }] }), + args: "{\"q\":\"x\"}", + }, +]; + +function captured(session: Session): CoreMessage[] { + return (session.metadata.handoffResponse as { messages?: CoreMessage[] } | undefined)?.messages ?? []; +} + +function assertReply(session: Session, args: string) { + const messages = captured(session); + assert.equal(messages.filter((m) => m.contentType === "text").map((m) => m.text).join(""), "héllo"); + const tool = messages.find((m) => m.contentType === "tool-call"); + assert.equal(tool?.toolName, "look"); + assert.equal(tool?.text, args); +} + +for (const c of cases) { + test(`${c.protocol}: captures stream text and tool output through production parser`, async () => { + const chunks = c.stream.map((part) => Buffer.from(part)); + // Split the multibyte character across writes, while retaining CRLF framing. + const textFrame = chunks.find((b) => b.includes(Buffer.from("hé"))); + if (textFrame) { + const at = textFrame.indexOf(Buffer.from("é")); + if (at >= 0) chunks.splice(chunks.indexOf(textFrame), 1, textFrame.subarray(0, at + 1), textFrame.subarray(at + 1, at + 2), textFrame.subarray(at + 2)); + } + const { wire, session } = await serve(c.protocol, true, chunks); + const baseline = await serve(c.protocol, true, chunks, { capture: false }); + assert.deepEqual(wire, baseline.wire); + assertReply(session, c.args); + }); + test(`${c.protocol}: captures JSON text and tool output through production codec`, async () => { + const { wire, session } = await serve(c.protocol, false, [c.json]); + const baseline = await serve(c.protocol, false, [c.json], { capture: false }); + assert.deepEqual(wire, baseline.wire); + assertReply(session, c.args); + }); +} + +test("Responses retains filtered deltas and reasoning when completion has no output", async () => { + const chunks = [ + event({ type: "response.reasoning_summary_text.delta", output_index: 0, delta: "thinking" }), + event({ type: "response.output_text.delta", output_index: 1, delta: "\x3cacp tokens=\"2\" type=\"text\"\x3em00001\x3c/acp\x3e" }), + event({ type: "response.output_text.delta", output_index: 1, delta: "answer" }), + event({ type: "response.completed", response: { status: "completed", output: [] } }), + ]; + const result = await serve("responses", true, chunks); + const baseline = await serve("responses", true, chunks, { capture: false }); + assert.deepEqual(result.wire, baseline.wire); + assert.deepEqual(captured(result.session).map((m) => [m.contentType, m.text]), [["reasoning", "thinking"], ["text", "answer"]]); +}); + +test("handoff captures flushed prose tails and preserves thinking bytes", async () => { + for (const protocol of ["openai", "responses"] as const) { + const thinking = "think \x3cacp tokens=\"1\"\x3em00002\x3c/acp\x3e"; + const chunks = protocol === "openai" ? [ + event({ choices: [{ delta: { reasoning_content: thinking } }] }), + event({ choices: [{ delta: { content: "answer <" } }] }), + event({ choices: [{ delta: {}, finish_reason: "stop" }] }), + "data: [DONE]\n\n", + ] : [ + event({ type: "response.reasoning_summary_text.delta", output_index: 0, delta: thinking }), + event({ type: "response.output_text.delta", output_index: 1, delta: "answer <" }), + event({ type: "response.completed", response: { output: [] } }), + ]; + const result = await serve(protocol, true, chunks); + const baseline = await serve(protocol, true, chunks, { capture: false }); + assert.deepEqual(result.wire, baseline.wire); + assert.equal(captured(result.session).filter((m) => m.contentType === "text").map((m) => m.text).join(""), "answer <"); + assert.equal(captured(result.session).filter((m) => m.contentType === "reasoning").map((m) => m.text).join(""), thinking); + } +}); + +test("Anthropic captures initial text and thinking blocks verbatim", async () => { + const chunks = [ + event({ type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "think" } }), + event({ type: "content_block_start", index: 1, content_block: { type: "text", text: "answer" } }), + event({ type: "message_stop" }), + ]; + const result = await serve("anthropic", true, chunks); + const baseline = await serve("anthropic", true, chunks, { capture: false }); + assert.deepEqual(result.wire, baseline.wire); + assert.deepEqual(captured(result.session).map((m) => [m.contentType, m.text]), [["reasoning", "think"], ["text", "answer"]]); +}); + +test("does not capture without semantic terminal, on error, abort, unsupported protocol, or oversize body", async () => { + const incomplete = await serve("openai", true, [event({ choices: [{ delta: { content: "partial" } }] })]); + assert.equal(incomplete.session.metadata.handoffResponse, undefined); + const failed = await serve("openai", true, [event({ choices: [{ delta: { content: "x" } }] }), event({ error: { message: "bad" } }), event({ choices: [{ finish_reason: "stop" }] })]); + assert.equal(failed.session.metadata.handoffResponse, undefined); + const aborted = await serve("openai", true, [event({ choices: [{ delta: { content: "partial" } }] })], { abort: true }); + assert.equal(aborted.session.metadata.handoffResponse, undefined); + const unsupported = await serve("unsupported", false, ["{}"]); + assert.equal(unsupported.session.metadata.handoffResponse, undefined); + const session = createSession("handoff-cap-test"); + const reply = createHandoffReply(session); + reply.append("large", "text", "界".repeat(500_001)); + reply.save(); + assert.equal(session.metadata.handoffResponse, undefined); +}); + +test("Responses incomplete outputs and oversized multibyte replies are omitted without changing bytes", async () => { + const output = [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "partial" }] }]; + for (const stream of [true, false]) { + const body = stream ? event({ type: "response.incomplete", response: { status: "incomplete", output } }) : JSON.stringify({ status: "incomplete", output }); + const { wire, session } = await serve("responses", stream, [body]); + const baseline = await serve("responses", stream, [body], { capture: false }); + assert.deepEqual(wire, baseline.wire); + assert.equal(session.metadata.handoffResponse, undefined); + } + const session = createSession("handoff-cap-test"); + const reply = createHandoffReply(session); + reply.append("large", "text", "界".repeat(500_001)); + reply.save(); + assert.equal(session.metadata.handoffResponse, undefined); +}); + +test("malformed SSE remains byte-identical and does not prevent valid terminal capture", async () => { + const raw = "data: {broken\r\n\r\ndata: [DONE]\r\n\r\n"; + const { wire, session } = await serve("openai", true, [raw]); + const baseline = await serve("openai", true, [raw], { capture: false }); + assert.deepEqual(wire, baseline.wire); + assert.equal(session.metadata.handoffResponse, undefined); +}); + +test("an older response cannot attach to a newer request snapshot", async () => { + for (const advance of [ + (session: Session) => { session.stats.requests++; }, + (session: Session) => { snapshotMessages(session, [{ id: "new", role: "user", contentType: "text", text: "new request" }]); }, + ]) { + const body = event({ choices: [{ delta: { content: "old answer" }, finish_reason: "stop" }] }); + const { wire, session } = await serve("openai", true, [body], { advance }); + const baseline = await serve("openai", true, [body], { capture: false, advance }); + assert.deepEqual(wire, baseline.wire); + assert.equal(session.metadata.handoffResponse, undefined); + } +}); + +test("snapshotMessages clears handoff metadata and does not mutate lastMessages", () => { + const session: Session = createSession("handoff-snapshot-test"); + const previous: CoreMessage[] = [{ id: "previous", role: "user", contentType: "text", text: "previous" }]; + session.lastMessages = previous; + session.metadata.handoffResponse = { request: 1, messages: [] }; + const next: CoreMessage[] = [{ id: "next", role: "user", contentType: "text", text: "next" }]; + snapshotMessages(session, next); + assert.equal(session.metadata.handoffResponse, undefined); + assert.deepEqual(session.lastMessages, next); + assert.notEqual(session.lastMessages, previous); + assert.deepEqual(previous, [{ id: "previous", role: "user", contentType: "text", text: "previous" }]); +}); + + +test("captures ordered interleaved text, reasoning, and multiple tools", () => { + const session = createSession("handoff-order-test"); + const reply = createHandoffReply(session); + reply.append("0", "text", "first"); + reply.tool("1", "search", "call_1", "{\"q\":1}"); + reply.append("2", "reasoning", "thinking"); + reply.append("3", "text", "second"); + reply.tool("4", "fetch", "call_2", "{\"url\":\"x\"}"); + reply.save(); + assert.deepEqual(captured(session).map((m) => [m.contentType, m.text, m.toolName, m.toolCallId]), [ + ["text", "first", undefined, undefined], + ["tool-call", "{\"q\":1}", "search", "call_1"], + ["reasoning", "thinking", undefined, undefined], + ["text", "second", undefined, undefined], + ["tool-call", "{\"url\":\"x\"}", "fetch", "call_2"], + ]); +}); + +test("request count, snapshot changes, failed acceptance, and discard prevent stale saves", () => { + const session = createSession("handoff-stale-test"); + session.stats.requests = 1; + const changedCount = createHandoffReply(session); + changedCount.append("0", "text", "old"); + session.stats.requests++; + changedCount.save(); + assert.equal(session.metadata.handoffResponse, undefined); + const changedSnapshot = createHandoffReply(session); + changedSnapshot.append("0", "text", "old"); + snapshotMessages(session, [{ id: "new", role: "user", contentType: "text", text: "new request" }]); + changedSnapshot.save(); + assert.equal(session.metadata.handoffResponse, undefined); + const rejected = createHandoffReply(session, () => false); + rejected.append("0", "text", "unaccepted"); + rejected.save(); + assert.equal(session.metadata.handoffResponse, undefined); + const discarded = createHandoffReply(session); + discarded.append("0", "text", "partial"); + discarded.discard(); + discarded.append("1", "text", "later"); + discarded.save(); + assert.equal(session.metadata.handoffResponse, undefined); +}); diff --git a/tests/loop-core.test.ts b/tests/loop-core.test.ts index f8c2ce000f..f7c5659f59 100644 --- a/tests/loop-core.test.ts +++ b/tests/loop-core.test.ts @@ -3,8 +3,9 @@ import assert from "node:assert/strict"; import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; import type { Session } from "../src/session.ts"; -import { runCompressLoop, createResponsesAdapter, MAX_LOOP_ROUNDS } from "../src/loop/index.ts"; +import { runCompressLoop, createResponsesAdapter, createOpenaiAdapter, MAX_LOOP_ROUNDS } from "../src/loop/index.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; +import { createHandoffReply } from "../src/server/handoff-response.ts"; import type { WireProtocol } from "../src/util.ts"; import { isTransientUpstreamError, REPLAY_MAX_ATTEMPTS, replayBackoffMs, replayMaxAttempts } from "../src/fetch-util.ts"; @@ -66,6 +67,42 @@ function fcEvents(outputIndex: number, callId: string, name: string, args: strin ].join(""); } +test("handoff uses the loop's parsed output without changing client bytes", async () => { + const raw = sse("response.output_text.delta", { type: "response.output_text.delta", item_id: "msg_reply", delta: "loop answer" }) + + fcEvents(1, "call_read", "read", '{"path":"file.ts"}') + COMPLETED; + const ctx = makeCtx([], "responses"); + const baseline = await drain(new Response(raw).body!, makeCtx([], "responses"), { input: [], stream: true }, { url: "http://mock", headers: {} }); + const reply = createHandoffReply(ctx.session); + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop(new Response(raw).body!, { ...ctx, reply }, { input: [], stream: true }, { url: "http://mock", headers: {} }, createResponsesAdapter(), buildCompressSystemPrompt())) chunks.push(chunk); + assert.equal(Buffer.concat(chunks).toString(), baseline); + const saved = (ctx.session.metadata.handoffResponse as { messages: CoreMessage[] }).messages; + assert.equal(saved.find((m) => m.contentType === "text")?.text, "loop answer"); + assert.equal(saved.find((m) => m.contentType === "tool-call")?.text, '{"path":"file.ts"}'); + assert.equal(ctx.session.lastMessages, undefined); +}); + +test("handoff retains forwarded continuation text and omits a failed terminal", async () => { + const frame = (content: string, finish_reason: string | null = null) => `data: ${JSON.stringify({ choices: [{ index: 0, delta: { content }, finish_reason }] })}\n\n`; + const originalFetch = globalThis.fetch; + try { + for (const fails of [false, true]) { + globalThis.fetch = async () => fails + ? new Response(frame("unfinished")) + : new Response(frame(" tail", "stop") + "data: [DONE]\n\n"); + const ctx = makeCtx([], "openai"); + const body = { messages: [], stream: true }; + const reply = createHandoffReply(ctx.session); + for await (const _chunk of runCompressLoop(new Response(frame("head")).body!, { ...ctx, reply }, body, { url: "http://mock", headers: {} }, createOpenaiAdapter(body), buildCompressSystemPrompt())) {} + const saved = ctx.session.metadata.handoffResponse as { messages: CoreMessage[] } | undefined; + if (fails) assert.equal(saved, undefined); + else assert.equal(saved?.messages.filter((m) => m.contentType === "text").map((m) => m.text).join(""), "head tail"); + } + } finally { + globalThis.fetch = originalFetch; + } +}); + const COMPLETED = sse("response.completed", { response: { id: "resp_done", status: "completed", output: [] } }); test("loop #1: acp_status-only round → marker surfaced + re-request (avoids tool_calls-no-body hang)", async () => { diff --git a/tests/plugin-protocol.test.ts b/tests/plugin-protocol.test.ts index 7a94c7f69f..4a9bf278e5 100644 --- a/tests/plugin-protocol.test.ts +++ b/tests/plugin-protocol.test.ts @@ -660,6 +660,53 @@ test("plugin mode: streamed response forwards verbatim while usage is sniffed in } }); +test("plugin handoff includes the completed reply before another request", async () => { + const h = await startHarness([textScript()]); + try { + const conv = "plug-handoff-latest"; + await callPluginAnthropic(h, conv, [{ role: "user", content: "hello" }]); + const stats = await (await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/stats`)).json() as { + sessions: Array<{ id: string; label?: string }>; + }; + const id = stats.sessions.find((s) => s.label === conv)!.id; + const detail = await (await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/sessions/${id}/detail`)).json() as { handoffMd: string; outputTokens: number }; + assert.match(detail.handoffMd, /### assistant\n\nplain answer/); + assert.equal(detail.outputTokens, 7); + await callPluginAnthropic(h, conv, [ + { role: "user", content: "hello" }, + { role: "assistant", content: "plain answer" }, + { role: "user", content: "continue" }, + ]); + const next = await (await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/sessions/${id}/detail`)).json() as { handoffMd: string }; + assert.equal(next.handoffMd.split("plain answer").length - 1, 2, "one historical reply and one newly completed reply"); + } finally { + await h.close(); + } +}); + +test("proxy handoff includes the completed reply for streaming and JSON requests", async () => { + const h = await startHarness([textScript()]); + try { + for (const stream of [true, false]) { + const id = `proxy-handoff-${stream}`; + const response = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": id }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(response.status, 200); + await response.text(); + const detailResponse = await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/sessions/${id}/detail`); + assert.equal(detailResponse.status, 200); + const detail = await detailResponse.json() as { handoffMd: string }; + assert.match(detail.handoffMd, /### assistant\n/); + assert.ok(detail.handoffMd.includes(stream ? "plain answer" : "non-stream answer")); + } + } finally { + await h.close(); + } +}); + test("plugin mode: relay-echoed message_delta input_tokens:0 must not zero the sniffed input", async () => { // Some relays echo a schema-shaped usage in message_delta with // input_tokens: 0 (the spec field is normally absent — message_start is diff --git a/tests/web-handoff-roles.test.ts b/tests/web-handoff-roles.test.ts index 238dab8d54..cccb83419a 100644 --- a/tests/web-handoff-roles.test.ts +++ b/tests/web-handoff-roles.test.ts @@ -7,6 +7,9 @@ import test from "node:test"; import assert from "node:assert/strict"; import vm from "node:vm"; import { WEB_CLIENT } from "../src/web/client.ts"; +import { createInitialState } from "acp-kernel"; +import { renderHandoff, HANDOFF_THINK_END } from "../src/export.ts"; +import { snapshotMessages, type Session } from "../src/session.ts"; interface DomEl { tagName: string; @@ -106,6 +109,190 @@ const roleCount = (html: string) => (html.match(/class="msg-role /g) || []).leng const userRoles = (html: string) => (html.match(/class="msg-role user"/g) || []).length; const assistantRoles = (html: string) => (html.match(/class="msg-role assistant"/g) || []).length; +test("latest reply uses the existing assistant block without adding a heading to user content", () => { + const session: Session = { + id: "latest-reply-layout", + meta: {}, + stats: { requests: 1, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0, compressCreditTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: { handoffResponse: { request: 1, messages: [{ id: "reply", role: "assistant", contentType: "text", text: "ok" }] } }, + createdAt: 0, + lastSeen: 0, + state: createInitialState(), + pendingRetrievals: [], + blockContents: new Map(), + inFlight: 0, + persisted: false, + lastMessages: [{ id: "user", role: "user", contentType: "text", text: "测试 回复ok" }], + }; + const render = loadRenderer(); + for (const full of [false, true]) { + const md = renderHandoff(session, full); + const html = render(md); + assert.doesNotMatch(md, /## Latest assistant response/); + assert.equal(userRoles(html), 1); + assert.equal(assistantRoles(html), 1); + assert.match(html, /测试 回复ok/); + assert.match(html, /class="msg-role assistant">assistant<\/h3>

ok/); + assert.doesNotMatch(html, /Latest assistant response/); + } +}); + +test("latest reply keeps thinking out of the answer text", () => { + const session = { + id: "reply-thinking", + meta: { protocol: "openai", title: "Thinking reply" }, + stats: { requests: 3, inputTokens: 0, cachedTokens: 0, outputTokens: 0, contextTokens: 0 }, + metadata: { + handoffResponse: { + request: 3, + messages: [ + { id: "r1", role: "assistant", contentType: "reasoning", text: "plan the answer\n\nthen write it" }, + { id: "r2", role: "assistant", contentType: "text", text: "最终答案在这里" }, + ], + }, + }, + createdAt: 0, + lastSeen: 0, + state: createInitialState(), + pendingRetrievals: [], + blockContents: new Map(), + inFlight: 0, + persisted: false, + lastMessages: [{ id: "user", role: "user", contentType: "text", text: "提问" }], + } as unknown as Session; + const render = loadRenderer(); + const md = renderHandoff(session, false); + const html = render(md); + assert.match(md, /_reasoning_: plan the answer/); + assert.equal(md.split("\n").filter((l) => l === "### assistant").length, 1, "one reply stays one assistant block"); + assert.ok(md.includes(HANDOFF_THINK_END), "reasoning cell is terminated inside the block"); + assert.ok( + md.indexOf(HANDOFF_THINK_END) < md.indexOf("最终答案在这里"), + "the marker sits between the reasoning and the answer", + ); + const thinkBoxes = html.match(/

[\s\S]*?<\/details>/g) ?? []; + assert.equal(thinkBoxes.length, 1); + assert.match(thinkBoxes[0]!, /plan the answer/); + assert.doesNotMatch(thinkBoxes[0]!, /最终答案在这里/, "answer must not be swallowed by the thinking box"); + assert.match(html, /

最终答案在这里/); + assert.equal(assistantRoles(html), 1, "thinking and answer share one assistant card"); + assert.ok( + html.indexOf('

') < html.indexOf('

最终答案在这里'), + "the reasoning block renders before the answer", + ); + assert.doesNotMatch(html, /\/reasoning/, "the end marker is not rendered as text"); +}); + +test("reasoning boundaries survive the next request snapshot without changing stored messages", () => { + const cells = [ + { id: "think", role: "assistant" as const, contentType: "reasoning" as const, text: "first thought" }, + { id: "answer", role: "assistant" as const, contentType: "text" as const, text: "visible answer" }, + ]; + const user = { id: "user", role: "user" as const, contentType: "text" as const, text: "question" }; + const session = { + id: "history-boundaries", meta: {}, stats: { requests: 1 }, + metadata: { handoffResponse: { request: 1, messages: cells } }, + state: createInitialState(), lastMessages: [user], blockContents: new Map(), + } as unknown as Session; + const render = loadRenderer(); + for (const historical of [false, true]) { + if (historical) { + snapshotMessages(session, [user, ...cells, { ...user, id: "next", text: "next question" }]); + session.stats.requests++; + assert.equal(session.metadata.handoffResponse, undefined); + } + for (const folded of [false, true]) { + session.lastMessagesFolded = folded; + for (const full of [false, true]) { + const before = JSON.stringify(session); + const md = renderHandoff(session, full); + const html = render(md); + assert.equal(assistantRoles(html), 1); + assert.equal(md.split("\n").filter((line) => line === "### assistant").length, 1); + const think = html.match(/

[\s\S]*?<\/details>/g) ?? []; + assert.equal(think.length, 1); + assert.match(think[0]!, /first thought/); + assert.doesNotMatch(think[0]!, /visible answer/); + assert.match(html, /class="msg-out">visible answer/); + assert.equal(JSON.stringify(session), before); + } + } + } +}); + +test("literal reasoning-end markers remain visible in user, answer, and reasoning content", () => { + const render = loadRenderer(); + for (const role of ["user", "assistant"]) { + const html = render(`## Conversation\n### ${role}\nA literal example:\n\`\`\`html\n${HANDOFF_THINK_END}\n\`\`\`\nEnd`); + assert.match(html, /<!-- \/reasoning -->/); + } + const session = { + id: "literal-markers", meta: {}, stats: { requests: 1 }, + metadata: { handoffResponse: { request: 1, messages: [ + { id: "r", role: "assistant", contentType: "reasoning", text: `Before\n${HANDOFF_THINK_END}\nAfter` }, + { id: "a", role: "assistant", contentType: "text", text: `Answer\n${HANDOFF_THINK_END}\nEnd` }, + ] } }, + state: createInitialState(), lastMessages: [], blockContents: new Map(), + } as unknown as Session; + const html = render(renderHandoff(session, false)); + const think = html.match(/
[\s\S]*?<\/details>/g) ?? []; + assert.equal(think.length, 1); + assert.match(think[0]!, /Before<!-- \/reasoning -->After/); + assert.doesNotMatch(think[0]!, /Answer/); + assert.equal((html.match(/<!-- \/reasoning -->/g) ?? []).length, 2); +}); + +test("handoff think marker is shared by the export and the web renderer", () => { + assert.ok(WEB_CLIENT.includes(HANDOFF_THINK_END), "WEB_CLIENT mirrors the export's think-end marker"); +}); + +test("a reply that thinks again after a tool call keeps its chronology", () => { + const session = { + id: "reply-thinking-twice", + meta: { protocol: "openai", title: "Interleaved reply" }, + stats: { requests: 4, inputTokens: 0, cachedTokens: 0, outputTokens: 0, contextTokens: 0 }, + metadata: { + handoffResponse: { + request: 4, + messages: [ + { id: "r1", role: "assistant", contentType: "reasoning", text: "第一段思考" }, + { id: "r2", role: "assistant", contentType: "text", text: "先看工具输出" }, + { id: "r3", role: "assistant", contentType: "tool-call", toolName: "read", toolCallId: "call_1", text: "{}" }, + { id: "r4", role: "assistant", contentType: "reasoning", text: "第二段思考" }, + { id: "r5", role: "assistant", contentType: "text", text: "最终答案" }, + ], + }, + }, + createdAt: 0, + lastSeen: 0, + state: createInitialState(), + pendingRetrievals: [], + blockContents: new Map(), + inFlight: 0, + persisted: false, + lastMessages: [{ id: "user", role: "user", contentType: "text", text: "提问" }], + } as unknown as Session; + const md = renderHandoff(session, false); + const html = loadRenderer()(md); + const order = ["第一段思考", "先看工具输出", "read(call_1)", "第二段思考", "最终答案"].map((needle) => { + const at = md.indexOf(needle); + assert.ok(at >= 0, `missing ${needle}`); + return at; + }); + assert.deepEqual(order, [...order].sort((a, b) => a - b), "cells keep arrival order (no regrouping around the tool call)"); + assert.equal(md.split("\n").filter((l) => l === HANDOFF_THINK_END).length, 2, "each reasoning cell is closed"); + const thinkBoxes = html.match(/
[\s\S]*?<\/details>/g) ?? []; + assert.equal(thinkBoxes.length, 2, "two thinking cells stay two collapsed boxes"); + assert.match(thinkBoxes[0]!, /第一段思考/); + assert.doesNotMatch(thinkBoxes[0]!, /先看工具输出/); + assert.match(thinkBoxes[1]!, /第二段思考/); + assert.doesNotMatch(thinkBoxes[1]!, /最终答案/); + assert.match(html, /

先看工具输出/); + assert.match(html, /

最终答案/); + assert.match(html, /msg-tool/); + assert.equal(assistantRoles(html), 1, "the whole turn stays one assistant card"); +}); + test("#2065: in-body '### ' headings stay content, not mislabeled assistant blocks", () => { const render = loadRenderer(); const md = [