diff --git a/src/server.ts b/src/server.ts index 74b4410f16..021728c04f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -15,6 +15,12 @@ import { codexAlignedWindow } from "./codex-models.js"; import { fetchWithTimeout, MAX_REQUEST_BYTES, upstreamTimeoutMs } from "./fetch-util.js"; import { formatUpstreamError, getUpstreamConnectionStatus, recordUpstreamConnection, resolveProxy, resolveProxyDecision, proxyDispatcher, type UpstreamProxyDecision } from "./upstream-proxy.js"; import { maskHeaderForLog, maskHeadersForLog, maskHostPortForLog, setMaskHostsEnabled, maskUrlForLog, maskUrlsInText } from "./log-mask.js"; +import { injectTool } from "./server/inject.js"; +import { prepareAnthropic, prepareCountTokens } from "./server/prepare-anthropic.js"; +import { prepareGoogle, prepareGoogleCountTokens } from "./server/prepare-google.js"; +import { prepareOpenai } from "./server/prepare-openai.js"; +import { FORCE_TEXT_PROTOCOL, prepareResponses, prepareResponsesCompact, repairResponsesAssistantOrdering } from "./server/prepare-responses.js"; +import { Prepared, effectiveAbsorbBlock, imageBillingFor, stripKernelSummaries } from "./server/prepare-shared.js"; // Protocol codecs + the historical-image strip primitive live in the kernel now // (single source of truth shared with the omp/pi adapters): import from // "acp-kernel/wire" (kernel #215). @@ -130,29 +136,6 @@ import { droppedOpenaiParts } from "./wire-drop-warn.js"; // for a session. Re-exported under the old names for tests. export { WARNED_CHAIN_SESSION_CAP, _resetChainVerdictsForTest as _resetChainWarningsForTest, _chainVerdictMapForTest as _chainWarnSetForTest } from "./plugin.js"; -// #1205: sessions already warned about codec-dropped content parts (e.g. -// DeepSeek Files API file refs) — one warn per session per distinct type-set; -// attachment flows resend the same history every turn. Same bounded-FIFO shape -// as warnedChainSessions above. -const warnedWireDropKeys = new Set(); -export const WARNED_WIREDROP_KEY_CAP = 4096; -export function _resetWireDropWarningsForTest(): void { - warnedWireDropKeys.clear(); -} -export function _wireDropWarnSetForTest(): Set { - return warnedWireDropKeys; -} -export function warnDroppedOpenaiParts(parsed: unknown, sessionId: string, log: (level: string, msg: string) => void): void { - const report = droppedOpenaiParts(parsed); - if (!report) return; - const key = `${sessionId}:${report.types.join(",")}`; - if (warnedWireDropKeys.has(key)) return; - warnedWireDropKeys.add(key); - if (warnedWireDropKeys.size > WARNED_WIREDROP_KEY_CAP) { - warnedWireDropKeys.delete(warnedWireDropKeys.values().next().value as string); - } - log("warn", `[${sessionId}] wire codec will drop ${report.count} non-user content part(s) with unrecognized type(s) [${report.types.join(", ")}] (first at message #${report.firstIndex}) — kernel 0.0.85+ preserves all user-message parts (DeepSeek Files API refs included, #1205/#1188), but parts riding system/assistant/tool messages still reduce to text-only`); -} // #1284: upstream targets already warned about a "not a model conversation → // relaying verbatim" verdict — misrouted third-party endpoints can retry in @@ -700,82 +683,8 @@ export async function startServer(opts: ProxyOptions): Promise { return server; } -type Prepared = { - body: string | Buffer; - session: Session; - processedMessages: CoreMessage[]; - /** Original CoreMessages from the protocol conversion, BEFORE processTurn - * folded/replaced anything. compress/decompress/acp_status need the raw - * text (collectBlockContent reads message text by id); processedMessages - * has compressed messages replaced with placeholders → empty content. */ - originalMessages: CoreMessage[]; - protocol: WireProtocol; - stream: boolean; - compressInjected: boolean; - /** True when the session is driven by a cooperative agent-side plugin - * (x-bili-plugin header, see src/plugin.ts): tools are native, the - * response must pass through verbatim, and usage is sniffed instead of - * captured by the compress loop. */ - pluginMode?: boolean; - responsesTextProtocol?: boolean; - resetAfterSuccess?: boolean; - responsesProjection?: ResponsesProjection; - anthropicSystem?: AnthropicRequestBody["system"]; - /** Original leading system/developer prefix text captured by the kernel's - * openai hoist (0.0.37). The fold space no longer carries it, so every - * rebuilt payload and compress-loop round must re-inject it. */ - openaiSystemText?: string; - /** Google wire: the client's own `systemInstruction` text (the kernel hoists - * it out of the fold space) and the path-derived model. Both are needed to - * rebuild `systemInstruction` and to synthesize chunks on every compress - * loop round. */ - google?: { system?: string; model?: string }; - /** #1085: stable-system-anchor update notes for this turn — re-injected - * as trailing user messages so compress-loop rounds see the same - * updated-instructions context the main request did. */ - systemNotes?: string[]; - /** [#1343] Plugin-lane retrievals snapshotted onto THIS request's body. - * Carried so forward() commits them on upstream success or drops them - * (logged + corrective note) on failure — the ack is already out, so the - * full text must never vanish silently. */ - attachedRetrievals?: PendingRetrieval[]; - nudge?: NudgeDecision; - /** Render strategy the prepare used for processTurn ("none" for codex - * compaction triggers / ACP_RENDER_NONE). The #422 fold-refresh hook in - * forward() re-runs processTurn with the same strategy so the re-request - * renders tags exactly like the request that produced it. */ - renderTags?: "text-only" | "none"; - /** Effective compression prompts for this request (three-level cascade, - * defaults to the kernel's defaultPrompts). Carried so the compress loop - * in forward() rebuilds the SAME system prompt the request was prepared - * with. */ - prompts?: Prompts; - /** Effective pack surface (promptPack) for this request: tool prompts, - * system-prompt sections, nudge sections. Same carrying rationale as - * prompts — the compress loop must rebuild the identical surface. */ - surface?: PackSurface; - /** #388: side request (title-gen etc.) — transport with render-tag strip - * only. Skips preflight (handle() returns before it), the fake-completion - * retry wrapper, the compress loop, and every usage-sniffing pipe; the - * #460 strip pipes in forward() run with session=undefined. */ - sidePassthrough?: boolean; - /** Set when a codex native-compaction request was intercepted and a - * success response was forged locally (BILI_CODEX_COMPACT=intercept + - * gate passed). forward() serves `body` without contacting upstream. */ - codexForge?: { kind: "endpoint" | "trigger"; body: string; contentType: string }; -}; -// #767: per-request image billing mode — env BILI_IMAGE_BILLING (live, like -// BILI_IMAGE_TOKEN_CAP) wins over the per-provider route entry, which wins over -// the global config level; "auto"/unset classifies known first-party pixel-tile -// hosts by upstream URL. Every payload-size decision below consults this so one -// over-estimate cannot block all of them at once. -function imageBillingFor(opts: ProxyOptions, upstreamUrl: string | undefined): ResolvedImageBilling { - const env = process.env.BILI_IMAGE_BILLING; - const configured = env === "pixels" || env === "bytes" ? env : findRoute(opts.routes, upstreamUrl)?.imageBilling ?? opts.imageBilling ?? "auto"; - return resolveImageBilling(configured, upstreamUrl); -} function isTrustedAdminOrigin(origin: string | undefined, host: string | undefined, trustedHosts: Set): boolean { // Host must be one of OUR listen identities regardless of whether an @@ -2367,280 +2276,16 @@ async function handle( } } -const ACP_TAG_MARK = "\x3cacp "; -// acp-kernel injects an in-place `acp_summary_*` at the compressed range as a -// generic-library fallback. This host strips it ONLY when it is redundant: the -// block's compress tool-call also carries the summary (hideConsumedCompressCalls -// keeps active-block calls), and a mid-stream insertion would shift the upstream -// prefix-cache breakpoint. Blocks created without a tool call (preflight -// compression, src/preflight.ts — #247) have NO other carrier: their anchor is -// the only place the summary reaches the model, so it must survive. -// -// Per mode (see README "Two compression modes"): in plugin/launcher mode the -// tool call is ALWAYS in the re-sent history (the agent owns compression), so -// this strips every acp_summary and the carrier is the tool call; in proxy mode -// the tool call is usually absent (ephemeral server-side execution) or -// nonexistent (preflight), so acp_summary survives as the carrier and -// systemToUser later re-voices the survivors as USER messages (leaving them at -// their anchors) for strict backends (#377). -/** [#651] Strip oversized reasoning from closed compress turns (see - * src/reasoning-drop.ts) with an ops log line when anything was dropped. */ -function withReasoningDrop( - msgs: BiliMessage[], - reasoning: CompressReasoningConfig | undefined, - log: (level: string, msg: string) => void, - sessionId: string, - strictEcho: boolean, -): BiliMessage[] { - // [#684] strict-echo upstreams: reasoning must round-trip with tool_calls, - // so #651's drop must not fire. Learned/static strictness both land here. - if (strictEcho) return msgs; - const out = dropCompressReasoning(msgs, reasoning); - if (out.length !== msgs.length) { - log("info", `[${sessionId}] compress-reasoning: dropped ${msgs.length - out.length} reasoning message(s) from closed compress turns (#651)`); - } - return out; -} -/** [#684] Exit sentinel: in a thinking session, an assistant tool_calls - * message WITHOUT reasoning_content while sibling turns carry it is the - * signature of a split turn — strict-echo upstreams reject the whole request. - * The kernel turn gate makes this unreachable; warn if a new path - * reintroduces it. [#762] presence, not emptiness: a BLANK echo ("") is what - * DeepSeek accepts — counting it as absent fired on every turn of every - * healthy thinking session (38× in one). Only a missing field is the - * rejection signature. */ -export function warnReasoningPairs( - wireMessages: unknown[], - log: (level: string, msg: string) => void, - sessionId: string, -): void { - let withRc = 0; - let split = 0; - for (const m of wireMessages) { - const msg = m as { role?: string; tool_calls?: unknown; reasoning_content?: unknown }; - if (msg?.role !== "assistant") continue; - const hasRc = typeof msg.reasoning_content === "string"; - if (hasRc) withRc++; - else if (Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0) split++; - } - if (withRc > 0 && split > 0) { - log("warn", `[${sessionId}] reasoning-pair-violated: ${split} assistant tool-call message(s) lack reasoning_content while ${withRc} carry it — strict-echo upstreams (DeepSeek thinking mode) will reject the request (#684)`); - } -} - -/** [#684] Anthropic-wire twin of the openai sentinel, narrowed by [#1327]: - * warn only when bili itself lost thinking — a tool_use block that rode an - * inbound assistant message WITH a thinking block now rides an outbound - * assistant message with none. Outbound-only asymmetry (some tool_use turns - * think, others don't) is ordinary Claude Code traffic: turns without - * extended thinking never carry a block, and #651's dropCompressReasoning - * creates the same shape by design — the old heuristic warned on every - * healthy multi-turn session. Turns match by stable tool_use id (the kernel - * codec round-trips it verbatim); benign asymmetry stays fully silent. */ -export function warnAnthropicThinkingPairs( - inboundMessages: unknown[], - outboundMessages: unknown[], - log: (level: string, msg: string) => void, - sessionId: string, -): void { - const thinkingIds = new Set(); - for (const m of inboundMessages) { - const msg = m as { role?: string; content?: Array<{ type?: string; id?: string }> }; - if (msg?.role !== "assistant" || !Array.isArray(msg.content)) continue; - if (!msg.content.some((b) => b?.type === "thinking")) continue; - for (const b of msg.content) { - if (b?.type === "tool_use" && typeof b.id === "string") thinkingIds.add(b.id); - } - } - if (thinkingIds.size === 0) return; - let lost = 0; - const lostIds: string[] = []; - for (const m of outboundMessages) { - const msg = m as { role?: string; content?: Array<{ type?: string; id?: string }> }; - if (msg?.role !== "assistant" || !Array.isArray(msg.content)) continue; - if (msg.content.some((b) => b?.type === "thinking")) continue; - for (const b of msg.content) { - if (b?.type === "tool_use" && typeof b.id === "string" && thinkingIds.has(b.id)) { - lost++; - lostIds.push(b.id); - } - } - } - if (lost > 0) { - log("warn", `[${sessionId}] thinking-pair-violated: ${lost} tool_use block(s) lost their inbound thinking block in the outbound rebuild (${[...new Set(lostIds)].slice(0, 3).join(", ")}) — a stripped signature pair is rejected by the API (#684)`); - } -} -/** [#684] Responses-wire twin: function_call item with no reasoning item - * immediately preceding it while other turns carry reasoning. */ -export function warnResponsesReasoningPairs( - input: unknown[], - log: (level: string, msg: string) => void, - sessionId: string, -): void { - let withReasoning = 0; - let split = 0; - let prevWasReasoning = false; - for (const item of input) { - const it = item as { type?: string }; - if (it?.type === "reasoning") { - withReasoning++; - prevWasReasoning = true; - continue; - } - if (it?.type === "function_call" && !prevWasReasoning) split++; - prevWasReasoning = false; - } - if (withReasoning > 0 && split > 0) { - log("warn", `[${sessionId}] reasoning-pair-violated: ${split} function_call item(s) lack a preceding reasoning item while ${withReasoning} exist — strict-echo upstreams will reject the request (#684)`); - } -} -export function stripKernelSummaries(messages: BiliMessage[], state: CompressionState): BiliMessage[] { - const carried = new Set(); - for (const b of state.blocks) { - if (!b.active || !b.compressCallId) continue; - if (messages.some((m) => m.contentType === "tool-call" && m.toolCallId === b.compressCallId)) { - carried.add(`acp_summary_${b.blockId}`); - } - } - return messages.filter((m) => !(m.id ?? "").startsWith("acp_summary_") || !carried.has(m.id)); -} -// #564: folding + stripKernelSummaries can merge two assistant turns into one -// run, which Responses rejects (run order reasoning* -> message* -> -// function_call*, <=1 reasoning). Rebuild boundaries from the ORIGINAL history -// AFTER dedup: drop a reasoning whose turn body was wholly folded away (keep -// originally-reasoning-only turns), else separate the runs with a user marker. -const RESPONSES_TURN_SEPARATOR = "[The exchange between these two assistant turns was compressed.]"; -export function repairResponsesAssistantOrdering(folded: CoreMessage[], original: CoreMessage[]): CoreMessage[] { - const runOf = new Map(); - const runHasBody = new Map(); - let run = 0; - let inRun = false; - for (const m of original) { - if (m.role === "assistant") { - if (!inRun) { run++; inRun = true; } - runOf.set(m.id, run); - if (m.contentType !== "reasoning") runHasBody.set(run, true); - } else { - inRun = false; - } - } - const survivorCount = new Map(); - for (const m of folded) { - const r = m.role === "assistant" ? runOf.get(m.id) : undefined; - if (r !== undefined) survivorCount.set(r, (survivorCount.get(r) ?? 0) + 1); - } - const out: CoreMessage[] = []; - let phase = -1; - let seenReasoning = false; - let sepSeq = 0; - const pushSeparator = (): void => { - sepSeq++; - out.push({ id: `acp_turn_sep_${sepSeq}`, role: "user", contentType: "text", text: RESPONSES_TURN_SEPARATOR }); - phase = -1; - seenReasoning = false; - }; - for (const m of folded) { - if (m.role !== "assistant") { - out.push(m); - phase = -1; - seenReasoning = false; - continue; - } - const kind = m.contentType === "reasoning" ? "reasoning" : m.contentType === "tool-call" ? "tool-call" : "message"; - const r = runOf.get(m.id); - if (kind === "reasoning" && r !== undefined && runHasBody.get(r) && survivorCount.get(r) === 1) continue; - if ((kind === "reasoning" && (phase > 0 || seenReasoning)) || (kind === "message" && phase === 2)) pushSeparator(); - out.push(m); - if (kind === "reasoning") { phase = Math.max(phase, 0); seenReasoning = true; } - else if (kind === "message") phase = Math.max(phase, 1); - else phase = Math.max(phase, 2); - } - return out; -} -function diagTagSummary(messages: CoreMessage[], sessionId: string, strategy: string): string { - let textTagged = 0; - let toolTagged = 0; - for (const m of messages) { - const hasTag = (m.text ?? "").includes(ACP_TAG_MARK); - if (!hasTag) continue; - if (m.contentType === "tool-call" || m.contentType === "tool-result") toolTagged++; - else textTagged++; - } - return `[${sessionId}] processTurn: ${messages.length} msgs, renderTags=${strategy}, ${textTagged} text tagged, ${toolTagged} tool tagged (should be 0 with text-only)`; -} -function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; contextUsage: number; tier: number | null; breakdown?: Record } | null }, sessionId: string, tokenCount: number, limit: number, model: string | undefined, willInject: boolean): string { - const n = turn.nudge; - if (!n) return `[${sessionId}] nudge: unavailable`; - const b = n.breakdown ?? {}; - const pct = limit > 0 ? `${Math.round((tokenCount / limit) * 100)}%` : "?"; - const growth = b["growth"] ?? 0; - const floor = b["growthFloor"] ?? 0; - const interval = b["nudgeGrowthTokens"] ?? 0; - const pendingT1 = b["pendingT1"] ?? 0; - const ref = b["growthReference"] ?? 0; - // "INJECT" only when the nudge actually reaches the upstream payload. When - // armed but suppressed by config/mode, say so explicitly so the log never - // lies about delivery (#451, same class as #413). - const inject = willInject - ? (n.shouldInject ? `INJECT T${n.tier ?? "?"}` : `INJECT-ESC T${n.tier ?? "?"}`) - : (n.shouldInject ? `ARMED-SUPPRESSED T${n.tier ?? "?"}` : "idle"); - const modelTag = model ? ` model=${model}` : ""; - return `[${sessionId}] nudge ${inject}: usage=${pct} (${tokenCount}/${limit}), growth=${growth}/${floor} (ref=${ref}, interval=${interval}), pendingT1=${pendingT1}/${interval}${modelTag}, reason="${n.reason.slice(0, 120)}"`; -} -// Zero-baseline sessions are judged conservatively ONLY when they arrived -// anonymously (prefix-affinity forks/reloads, #553): they carry the full raw -// history but no measurement yet, so feeding 0 blinds the nudge (usage 0%, -// growth ref 0) and no compression trigger fires until overflow. Explicit- -// identity zero-baseline sessions previously stayed at 0 on the assumption -// that they "self-heal via the next measured usage report" — an assumption -// that breaks for upstreams that NEVER report usage (ChatGPT-login backends, -// #728): lastInputTokens stays 0 for the whole session, and the kernel's -// decideNudge is structurally unfireable at tokenCount == 0 (growth ref falls -// back to tokenCount itself → growth ≡ 0; firstSightMassReady/pressure bands -// all require usage >= their pct lines). #728 fix: such sessions fall back to -// the PREVIOUS turn's locally-measured outbound payload upper bound -// (session.stats.localInputEstimate, recorded in prepare* each turn). The -// estimator only errs EARLY (char-count upper bound → compress earlier, never -// later), is active only while lastInputTokens == 0 (a real usage report takes -// precedence immediately — same invariant as #604's armFailureShrink -// exception), and self-corrects after every fold (the post-fold payload -// shrinks → the next estimate drops). Turn 1 of a fresh explicit session -// still feeds 0 (nothing measured yet; nothing pending either), so -// first-turn behavior is byte-identical to pre-#728. -function effectiveTokenCount(session: Session, msgs: CoreMessage[], inboundImageTokens = 0): number { - if (session.stats.lastInputTokens > 0) return session.stats.lastInputTokens; - // The inbound upper bound carries the image term in BOTH zero-baseline - // regimes (#1119/#1137): the wire codecs move images out of - // CoreMessage.text into sidecars, so a text-only bound drops them. - // #1137: an anonymous prefix-affinity fork (#553) replays its FULL raw - // history — images included — in this one request, so this request's - // image mass IS the history's image mass; returning the text-only bound - // there left image-heavy forks reading single-digit usage % and nudging - // only at overflow. - const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens; - if (session.metadata.anonymousPrefixAffinity) return raw; - const est = session.stats.localInputEstimate ?? 0; - if (est <= 0) return 0; - // Cap by THIS request's inbound upper bound: the recorded estimate lags by - // one turn, so after a client-side shrink (native compaction echo, history - // edit) the previous turn's payload can be larger than what is in front of - // us now — never claim more context than the current request could hold. - // In steady state est <= raw bound always (the outbound fold is never - // larger than the inbound history), so this is a no-op there. A - // client-side shrink still shrinks the bound (fewer messages AND fewer - // images), preserving the stale-high invariant. - return Math.min(est, raw); -} // #1403: top-level prompt_cache_key is NOT part of the Anthropic Messages API. // It is the omp plugin's session id stamped for the proxy's identity chain @@ -2668,1030 +2313,11 @@ function scrubAnthropicPck(protocol: WireProtocol | null, bodyBuffer: Buffer, lo return Buffer.from(JSON.stringify(p), "utf8"); } -async function prepareAnthropic( - parsed: AnthropicRequestBody, - req: http.IncomingMessage, - opts: ProxyOptions, - core: CompressionCore, - config: Config, - prompts: Prompts, - surface: PackSurface, - log: (level: string, msg: string) => void, - session: Session, - pluginMode: boolean, - upstreamOrigin: string, - reasoning: CompressReasoningConfig | undefined, - visibilityMarkers: boolean, -): Promise { - const sessionId = session.id; - const stream = parsed.stream === true; - ++session.stats.requests; - const injectTools = opts.compress.injectTool && !pluginMode; - const stripReasoning = (msgs: BiliMessage[]): BiliMessage[] => withReasoningDrop(msgs, reasoning, log, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))); - - if (isAutoModeClassifier(parsed)) { - log("info", `[${sessionId}] auto-mode classifier passthrough (skipping compress injection)`); - return { body: JSON.stringify(parsed), session, processedMessages: [], originalMessages: [], anthropicSystem: parsed.system, protocol: "anthropic", stream, compressInjected: false, pluginMode, nudge: undefined, prompts, surface } as Prepared; - } - - let processedMessages: CoreMessage[] = []; - let attachedRetrievals: PendingRetrieval[] = []; - let originalMessages: CoreMessage[] = []; - let nudge: NudgeDecision | undefined; - let rebuiltMessages = parsed.messages; - let systemOut = parsed.system; - let toolsOut = parsed.tools; - - // #1085: sticky head-system anchor — freeze the client's own `system` text - // at first sight and forward it byte-stable; detected changes ride as - // trailing user notes. Mutating parsed.system in place makes every - // downstream consumer (injectSystem, Prepared.anthropicSystem → loop) - // inherit the anchor, and the failure path below keeps forwarding it too. - let sysNotes: string[] = []; - // Plugin-mode agents own their context management and may already apply - // their own cache-friendly head handling (#1085 scope: plain-proxy mode - // only) — anchoring them would double-process. - if (opts.stableSystemAnchor && !pluginMode) { - const fresh = extractSystem(parsed.system); - const outcome = reconcileSystemAnchor(session, "anthropic", fresh, sessionId, log); - sysNotes = outcome.notes; - if (outcome.outbound !== fresh) { - parsed.system = buildSystem(outcome.outbound, parsed.system); - } - } - - // The classifier passthrough above and prepareResponsesCompact return before - // this strip; no client emits an ACP panel on those paths, so that's safe. - const strippedPanels = stripAcpPanelMessages(parsed.messages); - if (strippedPanels > 0) { - log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); - } - const strippedMarkerLines = stripAcpStatusMarkers(parsed.messages); - if (strippedMarkerLines > 0) { - log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); - } - - try { - const { msgs, cacheControls } = anthropicToCore(parsed); - originalMessages = msgs; - // #1320: signature-only thinking blocks bill restored thinking tokens upstream - // but project as empty text locally — attribute the provider-vs-local residual - // to them so every meter sees the billed context (metering-only, wire untouched). - const inboundImageTokens = imageTokensInParsedBody("anthropic", parsed, imageBillingFor(opts, upstreamOrigin)); - projectThinkingMass(msgs, { - providerInputTokens: session.stats.lastInputTokens, - measured: session.stats.lastInputTokensSource === "usage", - systemText: extractSystem(parsed.system), - tools: parsed.tools, - imageTokens: inboundImageTokens, - storedOverhead: typeof session.metadata.systemPromptTokens === "number" ? session.metadata.systemPromptTokens : undefined, - }); - // #1001: pre-turn snapshot — processTurn below assigns fresh refs to every - // previously-unknown id, which would make rewrite detection read 1.0. - const knownRefsBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); - // tokenCount drives the nudge decision ("should we compress?"). It MUST - // be the real context size, never an estimate — estimates undercount - // CJK text 3-4x and never trigger compression for Chinese sessions. - // Use the upstream's own input_tokens from the PREVIOUS turn (known by - // now — the response came back). First turn has no history → 0 (never - // triggers anyway). extractSystem is still called so sysText flows into - // the fallback path below if we ever need it, but we no longer feed - // estimates to the kernel. - extractSystem(parsed.system); - // #553-follow-up exception to the "never estimates" rule above: anonymous - // zero-baseline forks replay their FULL raw history with no measurement, - // so feeding 0 blinds the nudge (usage 0%, growth ref 0) and no - // compression trigger fires until overflow. See effectiveTokenCount. - const tokenCount = effectiveTokenCount(session, msgs, inboundImageTokens); - const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); - // Absorb markers are injected by the kernel's processTurn from - // config.absorb. With no channel to call the tool (injection off), - // strip absorb from the loop config so the REQUIRED instruction never - // reaches the wire. Hiding recorded absorptions is unaffected - // (applyAbsorbView hides regardless of enablement). - const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); - const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); - const absorbActive = absorbBlock?.enabled === true && opts.compress.injectTool; - // acp_rule has no processTurn side effect (no markers/instructions are - // ever injected into messages), so unlike absorb it needs no loop- - // config stripping — only tool availability matters. - const rulesActive = rulesEnabled(config) && opts.compress.injectTool; - // [#1097] the kernel ccr-store node ID-references oversized tool results - // BEFORE absorb (ID-reference wins over distill); armed policy is - // stamped per-request — strip `ccr` from the loop config when disarmed - // (plugin mode / no tool channel) so placeholders never hit the wire. - const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); - // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside - // processTurn may deactivate fully-drifted blocks, erasing them. - const foldCoveredBefore = session.stats.pendingFoldUsage === true - ? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : []))) - : null; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); - session.state = turn.state; - adoptContentStore(session, turn.contentStore); - // The fold from last turn's compress has now materialized in state — - // future usage reports are post-fold reality, drop the credit. - session.stats.compressCreditTokens = 0; - if (foldCoveredBefore !== null && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) { - const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); - if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); - } - storeEffectiveAbsorb(session, loopConfig); - storeEffectiveRules(session, config); - turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); - // Drop sub-viability fragments before any consumer sees them: a tiny - // range in the list makes batched compress attempts fail atomically - // (kernel validates the whole batch). Mirrors billion-context-pi. - if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); - nudge = turn.nudge; - session.stats.contextTokens = tokenCount; - if (!session.meta.title) { - const t = deriveTitle(msgs); - if (t) session.meta.title = t; - } - log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); - log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); - processedMessages = stripReasoning(stripKernelSummaries(turn.messages, turn.state)); - // #1001: a silent client history rewrite takes the same archive+prune path - // as an announced /compact boundary — syncBlocks above has already - // deactivated the blocks whose sources left the context. - { - const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id)); - if (rewrite.detected) { - log("warn", `[${sessionId}] unannounced client history rewrite detected (${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known) — marking compaction boundary (#1001)`); - recordConflict(session, "unannounced-rewrite", `${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known`); - markCompactionBoundary(session); - } - } - applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); - reapOrphansLogged(session, msgs, log, sessionId); - // [#1095] downscale screenshot-like images ONCE at arrival (kernel routing - // decision + recipe; originals cached for the image_full restore channel). - // Deterministic encode ⇒ re-runs are byte-stable for the prefix cache. - await applyImageCompressionPass(session, processedMessages as BiliMessage[], { config, billing: imageBillingFor(opts, upstreamOrigin), log }); - // [#1271/#1343] plugin mode: acp_retrieve already acked via the tool API; snapshot - // the queued full text onto THIS forward (stays in the queue until commit/drop, so an - // upstream failure drops-and-logs it instead of vanishing it). - if (pluginMode && ccrEnabled(session)) { - reconcileReloadedRetrievals(session); - pruneExpiredRetrievals(session); - attachedRetrievals = snapshotPendingRetrievals(session); - if (attachedRetrievals.length > 0) processedMessages = [...processedMessages, ...attachedRetrievals.map((i) => i.injection)]; - } - rebuiltMessages = coreToAnthropic(processedMessages as BiliMessage[], cacheControls); - if (sysNotes.length > 0) { - rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; - } - - systemOut = injectSystem(parsed, opts, prompts, loopConfig, ensureCanonicalId(session), surface, visibilityMarkers); - if (injectTools) { - toolsOut = injectTool(parsed.tools, [...(absorbActive ? [absorbTools.anthropic] : []), ...(rulesActive ? [RULE_TOOL] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).anthropic] : []), ...(imageCompressionEnabled(session) ? [IMAGE_FULL_TOOL] : [])], surface?.toolPrompts); - } - // Nudge as a separate trailing user message (cache-friendly): the - // system block stays byte-stable so the prefix cache survives. - // Injected in BOTH modes (#451): in plugin mode the agent supplies the - // ACP tools but has NO nudge channel of its own, so this proxy-side - // nudge IS the proactive trigger — preflight alone only fires at the - // hard limit. Ephemeral user message: not persisted, never enters the - // agent's re-sent history, safe for the prefix-cache anchor. - if (willInjectNudge && turn.nudge) { - try { - const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); - if (rendered.text) { - rebuiltMessages = [...rebuiltMessages, { role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }]; - } - } catch { - } - } - // [#1095] restore-channel guidance — ephemeral trailing user message - // (see prepareAnthropic for why not system). - const imgNote = imageFullTrailingNote(session); - if (imgNote) rebuiltMessages = [...rebuiltMessages, { role: "user", content: imgNote }]; - // [#1343] surface any earlier undelivered retrieve as an ephemeral trailing - // user note (kept last so it never reorders cached messages). - const retrNote = flushRetrievalNotes(session); - if (retrNote) rebuiltMessages = [...rebuiltMessages, { role: "user", content: retrNote }]; - } catch (err) { - log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); - if (attachedRetrievals.length > 0) dropRetrievals(session, attachedRetrievals.map((i) => i.ref), "prepare failed; forwarded unprocessed"); - processedMessages = []; - } - // #532: measure the outbound system+tools overhead for the status panel's - // SysPrompt row — the kernel breakdown classifies messages only, and on - // this wire the system rides the top-level `system` field outside the fold. - session.metadata.systemPromptTokens = countSystemAndToolsTokens(extractSystem(systemOut), toolsOut); - snapshotMessages(session, originalMessages); - markDirty(session); - - const rebuilt: AnthropicRequestBody = { ...parsed, messages: rebuiltMessages, system: systemOut, tools: toolsOut }; - warnAnthropicThinkingPairs(parsed.messages, rebuiltMessages, log, sessionId); - // prompt_cache_key is the omp plugin's session id stamped for the proxy's - // identity chain (#268), not part of the Anthropic Messages API — strip it - // so the real upstream never sees a field it doesn't know. - delete (rebuilt as Record).prompt_cache_key; - // #728: record the char-count upper bound of THIS turn's outbound payload - // (post-fold messages + system/tools overhead + images) as the fallback - // token source for upstreams that never report usage — read only while - // lastInputTokens == 0 (effectiveTokenCount). When the kernel transform - // above failed, processedMessages is empty and the forwarded body is the - // UNPROCESSED projection — measure that instead so the fallback isn't - // blinded to a system+tools-only floor. - session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) - + countSystemAndToolsTokens(extractSystem(systemOut), toolsOut) - + imageTokensInParsedBody("anthropic", rebuilt, imageBillingFor(opts, upstreamOrigin)); - return { body: JSON.stringify(rebuilt), session, attachedRetrievals, processedMessages, originalMessages, anthropicSystem: parsed.system, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" } as Prepared; -} - -async function prepareOpenai( - parsed: OpenAIRequestBody, - req: http.IncomingMessage, - opts: ProxyOptions, - core: CompressionCore, - config: Config, - prompts: Prompts, - surface: PackSurface, - log: (level: string, msg: string) => void, - session: Session, - pluginMode: boolean, - upstreamOrigin: string, - nativeWindow: number, - reasoning: CompressReasoningConfig | undefined, - visibilityMarkers: boolean, - billingUpstream?: string, -): Promise { - const sessionId = session.id; - const stream = parsed.stream === true; - ++session.stats.requests; - let openaiSystemText = ""; - let sysNotes: string[] = []; - const stripReasoning = (msgs: BiliMessage[]): BiliMessage[] => withReasoningDrop(msgs, reasoning, log, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))); - let openaiOutboundSystem: string | undefined; - let processedMessages: CoreMessage[] = []; - let attachedRetrievals: PendingRetrieval[] = []; - let originalMessages: CoreMessage[] = []; - let nudge: NudgeDecision | undefined; - let rebuiltMessages = parsed.messages; - let toolsOut = parsed.tools; - - const maxTokens = typeof parsed.max_tokens === "number" ? parsed.max_tokens : 8192; - // Title-generation requests (tiny max_tokens) get no compress tooling so - // the model produces a clean short title. We do NOT key this off message - // count: a 2-message request is just turn 1 of a real conversation, and - // flipping shouldInject false→true between turn 1 and turn 2+ rewrites the - // system prompt bytes (compress prompt added/removed) — which breaks the - // provider prefix cache for every subsequent turn. - const isTitleGen = maxTokens <= 200; - const shouldInject = opts.compress.injectTool && !isTitleGen; - const injectTools = shouldInject && !pluginMode; - - const strippedPanels = stripAcpPanelMessages(parsed.messages); - if (strippedPanels > 0) { - log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); - } - const strippedMarkerLines = stripAcpStatusMarkers(parsed.messages); - if (strippedMarkerLines > 0) { - log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); - } - - try { - // Kernel 0.0.37 hoists the contiguous leading system/developer prefix - // OUT of the fold space: system content is host runtime state and - // must not feed ids/fingerprints. Capture it and re-inject below — - // otherwise the proxy would forward payloads without any system. - const { msgs, systemText } = openaiToCore(parsed); - openaiSystemText = systemText; - warnDroppedOpenaiParts(parsed, sessionId, log); - // Title-gen side-requests carry their own tiny system — reconciling - // them would pollute the conversation's anchor state. - if (opts.stableSystemAnchor && !pluginMode && !isTitleGen) { - const outcome = reconcileSystemAnchor(session, "openai", systemText, sessionId, log); - sysNotes = outcome.notes; - openaiSystemText = outcome.outbound; - } - originalMessages = msgs; - // #1001: pre-turn snapshot — processTurn below assigns fresh refs to every - // previously-unknown id, which would make rewrite detection read 1.0. - const knownRefsBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); - // tokenCount = upstream's real input_tokens from the previous turn - // tokenCount = upstream's real input_tokens from the previous turn - // (see anthropic branch comment + its #553-follow-up exception). - const tokenCount = effectiveTokenCount(session, msgs, imageTokensInParsedBody("openai", parsed, imageBillingFor(opts, billingUpstream ?? upstreamOrigin))); - const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); - // Absorb markers ride in the kernel's processTurn output (gated by - // config.absorb). Title-gen requests skip ALL injection for - // prefix-cache stability, so strip absorb from the loop config there. - const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); - const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); - const absorbActive = absorbBlock?.enabled === true && shouldInject; - const rulesActive = rulesEnabled(config) && shouldInject; - const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); - // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside - // processTurn may deactivate fully-drifted blocks, erasing them. - const foldCoveredBefore = session.stats.pendingFoldUsage === true - ? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : []))) - : null; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); - session.state = turn.state; - adoptContentStore(session, turn.contentStore); - // The fold from last turn's compress has now materialized in state — - // future usage reports are post-fold reality, drop the credit. - session.stats.compressCreditTokens = 0; - if (foldCoveredBefore !== null && !isTitleGen && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) { - const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); - if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); - } - storeEffectiveAbsorb(session, loopConfig); - storeEffectiveRules(session, config); - turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); - // Drop sub-viability fragments before any consumer sees them: a tiny - // range in the list makes batched compress attempts fail atomically - // (kernel validates the whole batch). Mirrors billion-context-pi. - if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); - nudge = turn.nudge; - session.stats.contextTokens = tokenCount; - if (!session.meta.title) { - const t = deriveTitle(msgs); - if (t) session.meta.title = t; - } - log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); - log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); - processedMessages = stripReasoning(stripKernelSummaries(turn.messages, turn.state)); - // #1001: a silent client history rewrite takes the same archive+prune path - // as an announced /compact boundary — syncBlocks above has already - // deactivated the blocks whose sources left the context. - { - const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id)); - if (rewrite.detected) { - log("warn", `[${sessionId}] unannounced client history rewrite detected (${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known) — marking compaction boundary (#1001)`); - recordConflict(session, "unannounced-rewrite", `${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known`); - markCompactionBoundary(session); - } - } - applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); - reapOrphansLogged(session, msgs, log, sessionId); - // [#1095] arrival-time image downscale (see prepareAnthropic) — one - // deterministic encode per fingerprint; byte-stable re-runs. - await applyImageCompressionPass(session, processedMessages as BiliMessage[], { config, billing: imageBillingFor(opts, billingUpstream ?? upstreamOrigin), log }); - // [#1271/#1343] plugin mode: acp_retrieve already acked via the tool API; snapshot - // the queued full text onto THIS forward (stays in the queue until commit/drop, so an - // upstream failure drops-and-logs it instead of vanishing it). - if (pluginMode && ccrEnabled(session)) { - reconcileReloadedRetrievals(session); - pruneExpiredRetrievals(session); - attachedRetrievals = snapshotPendingRetrievals(session); - if (attachedRetrievals.length > 0) processedMessages = [...processedMessages, ...attachedRetrievals.map((i) => i.injection)]; - } - rebuiltMessages = systemToUser(hardenOpenaiAssistantContent(coreToOpenai(processedMessages as BiliMessage[]))); - - // ONLY the static compress prompt goes into the system message — the - // system prompt is the prefix-cache anchor and must be byte-stable - // across turns. The nudge (which changes every turn: token count, - // growth %, dynamic example) is appended as a trailing user message - // instead, mirroring pai-acp's design. Putting the nudge in system - // would invalidate the cache every turn. - const sysParts: string[] = []; - if (openaiSystemText) sysParts.push(openaiSystemText); - if (shouldInject) sysParts.push(withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), ensureCanonicalId(session))); - if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); - rebuiltMessages = injectOpenaiSystem(rebuiltMessages, sysParts); - if (sysNotes.length > 0) { - rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; - } - // #532: capture what bili injects outside the fold space (client system - // + compress prompt). A head system message already in the rebuilt view - // is classified by the kernel breakdown — counting only these parts - // avoids double-counting it. - openaiOutboundSystem = sysParts.join("\n\n"); - if (injectTools) { - toolsOut = injectOpenaiTool(parsed.tools, [...(absorbActive ? [absorbTools.openai] : []), ...(rulesActive ? [RULE_TOOL_OPENAI] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).openai] : []), ...(imageCompressionEnabled(session) ? [IMAGE_FULL_TOOL_OPENAI] : [])], surface?.toolPrompts); - } - // Nudge as a separate trailing user message (cache-friendly). Injected - // in BOTH modes (#451): plugin agents supply the ACP tools but have no - // nudge channel of their own, so this proxy-side nudge is the proactive - // trigger (preflight alone fires only at the hard limit). Ephemeral user - // message — not persisted, never enters the agent's re-sent history, - // prefix-cache-anchor safe. - if (willInjectNudge && turn.nudge) { - try { - const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); - if (rendered.text) { - rebuiltMessages = [...rebuiltMessages, { role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }]; - } - } catch { - } - } - // [#1095] restore-channel guidance — ephemeral trailing user message - // (same pattern as prepareAnthropic/Google/Responses). - const imgNote = imageFullTrailingNote(session); - if (imgNote) rebuiltMessages = [...rebuiltMessages, { role: "user", content: imgNote }]; - // [#1343] surface any earlier undelivered retrieve as an ephemeral trailing - // user note (kept last so it never reorders cached messages). - const retrNote = flushRetrievalNotes(session); - if (retrNote) rebuiltMessages = [...rebuiltMessages, { role: "user", content: retrNote }]; - } catch (err) { - log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); - if (attachedRetrievals.length > 0) dropRetrievals(session, attachedRetrievals.map((i) => i.ref), "prepare failed; forwarded unprocessed"); - processedMessages = []; - } - // #762: repair the strict-echo rejection class BEFORE the sentinel sees - // the array — a normalized body must not fire its own canary. - rebuiltMessages = normalizeStrictEchoReasoning(rebuiltMessages, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)), log, sessionId); - const rebuilt: OpenAIRequestBody = { ...parsed, messages: rebuiltMessages, tools: toolsOut as OpenAITool[] | undefined }; - warnReasoningPairs(rebuiltMessages, log, sessionId); - clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("openai", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log); - // prompt_cache_retention is an OpenAI-host-only cache directive; the dsh - // launcher forces PI_CACHE_RETENTION=long (for the session-id - // prompt_cache_key) which makes the client also emit it. Third-party - // OpenAI-compatible upstreams may reject unknown fields, and cache policy - // is the upstream's business — strip it. prompt_cache_key itself passes - // through: upstreams that ignore it lose nothing, upstreams that use it - // get a per-conversation routing hint. - delete (rebuilt as Record).prompt_cache_retention; - // OpenAI Chat Completions only emits a usage object in the final stream - // chunk when the client sets stream_options.include_usage=true. Without - // it, streaming sessions never learn their real input_tokens → - // lastInputTokens stays 0 → compression never fires. Force it on for any - // streaming request that doesn't already opt in. (Anthropic/Responses - // emit usage unconditionally, so this is OpenAI-specific.) - if (stream && (rebuilt as Record).stream_options === undefined) { - (rebuilt as Record).stream_options = { include_usage: true }; - } - // #532: title-gen side requests carry their own tiny system and would - // clobber the conversation's measured overhead — skip them. - if (!isTitleGen && openaiOutboundSystem !== undefined) { - session.metadata.systemPromptTokens = countSystemAndToolsTokens(openaiOutboundSystem, toolsOut); - } - // #728: record this turn's outbound payload upper bound as the fallback - // token source for upstreams that never report usage (see effectiveTokenCount). - // Title-gen side requests are skipped like the overhead row above — their - // tiny payload would clobber the conversation's measurement. - if (!isTitleGen) { - session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) - + countSystemAndToolsTokens(openaiOutboundSystem || openaiSystemText, toolsOut) - + imageTokensInParsedBody("openai", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)); - } - snapshotMessages(session, originalMessages); - markDirty(session); - return { body: JSON.stringify(rebuilt), session, attachedRetrievals, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" } as Prepared; -} -/** Append the ephemeral nudge to a Gemini `contents` array. Gemini is - * strict about role alternation, so a trailing user turn is merged into - * rather than appended to (the nudge then reads as the model's last input, - * which is where it belongs); a model-final history gets a fresh user turn - * because a request must not end on the model side. */ -function appendGoogleNudge(contents: GoogleContent[], text: string): GoogleContent[] { - const last = contents[contents.length - 1]; - if (last && (last.role ?? "user") !== "model") { - const parts = Array.isArray(last.parts) ? last.parts : []; - return [...contents.slice(0, -1), { ...last, parts: [...parts, { text }] }]; - } - return [...contents, { role: "user", parts: [{ text }] }]; -} -/** Gemini native wire (`POST /v1beta/models/:generateContent| - * :streamGenerateContent`): the OpenAI branch's twin — hoist the system - * dimension out of the fold space, fold, re-inject — with three wire-specific - * differences: - * - the model, and whether the reply streams, live in the URL PATH, so the - * caller passes both in (the body carries neither); - * - the system rides in `systemInstruction`, never inside `contents`; - * - Gemini rejects non-alternating roles, which coreToGoogle enforces by - * merging same-side core runs, so the nudge merges into the trailing user - * turn instead of starting a new one. - * #651's reasoning drop deliberately does NOT apply here: Gemini 3 validates - * the `thoughtSignature` of replayed parts, and dropping a thought part takes - * its signature with it (400 INVALID_ARGUMENT). */ -async function prepareGoogle( - parsed: GoogleRequestBody, - opts: ProxyOptions, - core: CompressionCore, - config: Config, - prompts: Prompts, - surface: PackSurface, - log: (level: string, msg: string) => void, - session: Session, - pluginMode: boolean, - nativeWindow: number, - model: string | undefined, - stream: boolean, - visibilityMarkers: boolean, - upstreamOrigin: string, -): Promise { - const sessionId = session.id; - ++session.stats.requests; - let googleClientSystem = ""; - let sysNotes: string[] = []; - let googleOutboundSystem: string | undefined; - let systemInstruction: GoogleSystemInstruction | undefined = parsed.systemInstruction; - let processedMessages: CoreMessage[] = []; - let originalMessages: CoreMessage[] = []; - let nudge: NudgeDecision | undefined; - let rebuiltContents: GoogleContent[] = Array.isArray(parsed.contents) ? parsed.contents : []; - let toolsOut: GoogleTool[] | undefined = parsed.tools; - const genConfig = parsed.generationConfig; - const declaredMax = genConfig ? genConfig.maxOutputTokens : undefined; - const maxTokens = typeof declaredMax === "number" ? declaredMax : 8192; - // Title-generation requests (tiny budget) get no compress tooling — same - // heuristic and same prefix-cache rationale as prepareOpenai. - const isTitleGen = maxTokens <= 200; - const shouldInject = opts.compress.injectTool && !isTitleGen; - const injectTools = shouldInject && !pluginMode; - try { - const { msgs, systemText } = googleToCore(parsed); - googleClientSystem = systemText; - // Title-gen side-requests carry their own tiny system — reconciling - // them would pollute the conversation's anchor state. - if (opts.stableSystemAnchor && !pluginMode && !isTitleGen) { - const outcome = reconcileSystemAnchor(session, "google", systemText, sessionId, log); - sysNotes = outcome.notes; - googleClientSystem = outcome.outbound; - } - originalMessages = msgs; - const tokenCount = effectiveTokenCount(session, msgs, imageTokensInParsedBody("google", parsed, imageBillingFor(opts, upstreamOrigin))); - const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); - const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); - const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); - const absorbActive = absorbBlock?.enabled === true && shouldInject; - // acp_rule has no processTurn side effect (no markers/instructions are - // ever injected into messages), so unlike absorb it needs no loop- - // config stripping — only tool availability matters. - const rulesActive = rulesEnabled(config) && shouldInject; - const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); - // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside - // processTurn may deactivate fully-drifted blocks, erasing them. - const foldCoveredBefore = session.stats.pendingFoldUsage === true - ? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : []))) - : null; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: "text-only", contentStore: contentStoreOf(session) }); - session.state = turn.state; - adoptContentStore(session, turn.contentStore); - // The fold from last turn's compress has materialized in state — future - // usage reports are post-fold reality, drop the credit. - session.stats.compressCreditTokens = 0; - if (foldCoveredBefore !== null && !isTitleGen && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) { - const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); - if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); - } - storeEffectiveAbsorb(session, loopConfig); - storeEffectiveRules(session, config); - turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); - // Drop sub-viability fragments before any consumer sees them (the - // kernel validates a compress batch atomically). - if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); - nudge = turn.nudge; - session.stats.contextTokens = tokenCount; - if (!session.meta.title) { - const t = deriveTitle(msgs); - if (t) session.meta.title = t; - } - log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); - log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, model, willInjectNudge)); - processedMessages = stripKernelSummaries(turn.messages, turn.state); - applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); - reapOrphansLogged(session, msgs, log, sessionId); - // [#1095] arrival-time image downscale (see prepareAnthropic). - await applyImageCompressionPass(session, processedMessages as BiliMessage[], { config, billing: imageBillingFor(opts, upstreamOrigin), log }); - rebuiltContents = coreToGoogle(processedMessages as BiliMessage[]); - - // ONLY the static compress prompt joins the client's system text — the - // system instruction is the prefix-cache anchor and must stay - // byte-stable across turns. The per-turn nudge is appended to the - // trailing user content below (see prepareOpenai for the rationale). - const sysParts: string[] = []; - if (googleClientSystem) sysParts.push(googleClientSystem); - if (shouldInject) sysParts.push(withMarkerIntegrityNote(buildCompressSystemPrompt(prompts, surface?.promptSections), visibilityMarkers)); - if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); - googleOutboundSystem = sysParts.join("\n\n"); - // Untouched when nothing was added beyond the client's own text: the - // original `systemInstruction` object then rides through byte-identical - // instead of being re-serialized into a new shape. - const extraSystemParts = sysParts.slice(googleClientSystem ? 1 : 0); - systemInstruction = extraSystemParts.length > 0 ? { parts: sysParts.map((text) => ({ text })) } : parsed.systemInstruction; - if (injectTools) { - toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [absorbTools.google] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).google] : []), ...(imageCompressionEnabled(session) ? [IMAGE_FULL_TOOL_GOOGLE] : [])], surface?.toolPrompts); - } - if (sysNotes.length > 0) { - rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n")); - } - if (willInjectNudge && turn.nudge) { - try { - const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); - if (rendered.text) { - rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withStagedCompressGuidance(rendered.text), visibilityMarkers)); - } - } catch { - } - } - // [#1095] restore-channel guidance — ephemeral trailing note (see prepareAnthropic). - const imgNote = imageFullTrailingNote(session); - if (imgNote) rebuiltContents = appendGoogleNudge(rebuiltContents, imgNote); - } catch (err) { - log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); - processedMessages = []; - } - - const rebuilt: GoogleRequestBody = { ...parsed, contents: rebuiltContents, tools: toolsOut, systemInstruction }; - clampOutgoingOutput(rebuilt as Record, "generationConfig.maxOutputTokens", { systemText: googleClientSystem, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("google", rebuilt) }, sessionId, log); - // #532: title-gen side requests carry their own tiny system — skip them. - if (!isTitleGen && googleOutboundSystem !== undefined) { - session.metadata.systemPromptTokens = countSystemAndToolsTokens(googleOutboundSystem, toolsOut); - } - snapshotMessages(session, originalMessages); - markDirty(session); - return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "google", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: "text-only" } as Prepared; -} - -/** `POST /v1beta/models/:countTokens` — the fold-prune twin of - * prepareCountTokens: the client measures the payload the proxy would - * actually forward. Gemini's endpoint reads `contents`/`systemInstruction` - * and answers `{totalTokens}`, so only the contents array is rewritten. */ -export function prepareGoogleCountTokens( - parsed: GoogleRequestBody, - core: CompressionCore, - config: Config, - log: (level: string, msg: string) => void, - session: Session, -): Prepared { - const sessionId = session.id; - try { - const { msgs } = googleToCore(parsed); - // Read-only preview: the store rides in so placeholder substitution is - // counted, but nothing is adopted (state is discarded here too). - const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: session.stats.lastInputTokens, renderTags: "text-only", contentStore: contentStoreOf(session) }); - const stripped = stripKernelSummaries(turn.messages, turn.state); - const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) }; - log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`); - return { - body: JSON.stringify(rebuilt), - session, - processedMessages: [], - originalMessages: msgs, - protocol: "google", - stream: false, - compressInjected: false, - }; - } catch (err) { - log("warn", `[${sessionId}] countTokens prune failed, forwarding unchanged: ${String(err)}`); - return { - body: JSON.stringify({ ...parsed }), - session, - processedMessages: [], - originalMessages: [], - protocol: "google", - stream: false, - compressInjected: false, - }; - } -} - -async function prepareResponses( - parsed: ResponsesRequestBody, - req: http.IncomingMessage, - opts: ProxyOptions, - core: CompressionCore, - config: Config, - prompts: Prompts, - surface: PackSurface, - log: (level: string, msg: string) => void, - session: Session, - identity: ConversationIdentity, - pluginMode: boolean, - upstreamOrigin: string, - nativeWindow: number, - reasoning: CompressReasoningConfig | undefined, - visibilityMarkers: boolean, - billingUpstream?: string, -): Promise { - const sessionId = session.id; - const stream = parsed.stream === true; - ++session.stats.requests; - const stripReasoning = (msgs: BiliMessage[]): BiliMessage[] => withReasoningDrop(msgs, reasoning, log, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))); - if (reconcileNativeCompactionBoundary(session)) { - log("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`); - } - - // A codex client echoes our forged compaction item back in the next - // request; replace it with a plain summary-carrying user message so the - // handoff rides the replayable history (kernel-compressible, retained by - // codex's own user-message rule) instead of a foreign opaque blob. Real - // OpenAI blobs carry no bili marker and pass through untouched. - let echoReplaced = false; - if (Array.isArray(parsed.input)) { - const { items, replaced, dropped } = replaceBiliCompactionItems(parsed.input); - if (replaced + dropped > 0) { - // Only a real replacement carries a summary into the history; a - // drop-only echo removed a marker blob without inserting one, so the - // forge-time captured summaries must still be re-injected (#1064). - if (replaced > 0) echoReplaced = true; - log("info", `[${sessionId}] replaced ${replaced} echoed bili compaction item(s) with summary handoff message(s)${dropped > 0 ? `, dropped ${dropped} legacy marker item(s)` : ""}`); - parsed.input = items as typeof parsed.input; - } - } - - let processedMessages: CoreMessage[] = []; - let originalMessages: CoreMessage[] = []; - let nudge: NudgeDecision | undefined; - let responsesProjection: ResponsesProjection | undefined; - let rebuiltInput: ResponseInputItem[] | string = parsed.input; - let toolsOut = parsed.tools; - let transformOk = false; - let responsesDevContent: string | undefined; - let sysNotes: string[] = []; - - // #242: over-long input item ids (poisoned rollouts) 400 upstream on every - // request; rewrite them to short deterministic ids before anything reads - // or replays the input. - // omp-style type-less user items must be typed before the projection - // drops them (see normalizeResponsesMessageItems) — before id sanitize and - // whitespace drop so those see the canonical form. - const typedItems = normalizeResponsesMessageItems(parsed.input); - if (typedItems > 0) { - log("info", `[${sessionId}] stamped type:"message" on ${typedItems} type-less input item(s) before projection (omp wire form)`); - } - sanitizeResponsesInputIds(parsed.input); - - const droppedEmpty = dropWhitespaceResponsesMessages(parsed.input); - if (droppedEmpty > 0) { - log("info", `[${sessionId}] dropped ${droppedEmpty} whitespace-only message item(s) before projection (flattened-turn artifact)`); - } - - const strippedPanels = stripAcpPanelResponsesInput(parsed.input); - if (strippedPanels > 0) { - log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); - } - const strippedMarkerLines = stripAcpStatusMarkers(parsed.input); - if (strippedMarkerLines > 0) { - log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); - } - - const shouldInject = opts.compress.injectTool; - const injectTools = shouldInject && !pluginMode; - // Codex native remote-compact request: no compress prompt/tools (the model - // produces the compaction itself), no acp tags, plain passthrough so the - // response terminal state can gate the rebase marker. - const isCompactionTrigger = hasCompactionTrigger(parsed.input); - // Route config is keyed by the upstream THIS request goes to (#286: a - // session can outlive its first relay — session.meta.upstreamOrigin is - // first-wins and would silently ignore the new relay's route settings). - const responsesTextProtocol = FORCE_TEXT_PROTOCOL || - resolveCompressProtocol(opts.routes, upstreamOrigin) === "marker"; - const renderTags: "text-only" | "none" = process.env.ACP_RENDER_NONE || isCompactionTrigger ? "none" : "text-only"; - - try { - const projection = responsesToCore(parsed); - responsesProjection = projection; - // Compaction-trigger requests are the compression mechanism itself — - // their payload shape must not gain anchor state or note items. - if (opts.stableSystemAnchor && !pluginMode && !isCompactionTrigger) { - const fresh = projection.systemParts.join("\n\n---\n\n"); - const outcome = reconcileSystemAnchor(session, "responses", fresh, sessionId, log); - sysNotes = outcome.notes; - if (outcome.outbound !== fresh) projection.systemParts = outcome.outbound ? [outcome.outbound] : []; - } - const { msgs } = projection; - originalMessages = msgs; - if (process.env.ACP_DEBUG) { - log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); - } - const tokenCount = effectiveTokenCount(session, msgs, imageTokensInParsedBody("responses", parsed, imageBillingFor(opts, billingUpstream ?? upstreamOrigin))); - // Absorb markers ride in the kernel's processTurn output (gated by - // config.absorb). The marker/text protocol has no native tool channel, - // so strip absorb from the loop config there (both modes). - const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); - const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); - const absorbActive = absorbBlock?.enabled === true && shouldInject && !isCompactionTrigger && !responsesTextProtocol; - const rulesActive = rulesEnabled(config) && shouldInject && !isCompactionTrigger && !responsesTextProtocol; - const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); - // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside - // processTurn may deactivate fully-drifted blocks, erasing them. - const foldCoveredBefore = session.stats.pendingFoldUsage === true - ? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : []))) - : null; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags, contentStore: contentStoreOf(session) }); - session.state = turn.state; - adoptContentStore(session, turn.contentStore); - // The fold from last turn's compress has now materialized in state — - // future usage reports are post-fold reality, drop the credit. - session.stats.compressCreditTokens = 0; - if (foldCoveredBefore !== null && !isCompactionTrigger && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) { - const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); - if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); - } - storeEffectiveAbsorb(session, loopConfig); - storeEffectiveRules(session, config); - turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); - // Drop sub-viability fragments before any consumer sees them: a tiny - // range in the list makes batched compress attempts fail atomically - // (kernel validates the whole batch). Mirrors billion-context-pi. - if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); - nudge = turn.nudge; - session.stats.contextTokens = tokenCount; - if (!session.meta.title) { - const t = deriveTitle(msgs); - if (t) session.meta.title = t; - } - log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !isCompactionTrigger && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); - log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); - processedMessages = repairResponsesAssistantOrdering(stripReasoning(stripKernelSummaries(turn.messages, turn.state)), originalMessages); - reapOrphansLogged(session, msgs, log, sessionId); - // [#1095] arrival-time image downscale (see prepareAnthropic). - await applyImageCompressionPass(session, processedMessages as BiliMessage[], { config, billing: imageBillingFor(opts, billingUpstream ?? upstreamOrigin), log }); - rebuiltInput = patchResponsesInput(projection, processedMessages); - if (Array.isArray(rebuiltInput)) rebuiltInput = hoistTrappedToolItems(rebuiltInput); - // Fallback path: when the echo did NOT come back this turn (client - // dropped it / restarted), the history-borne handoff is absent and the - // forge-time captured summaries are re-injected into the developer - // message so the pre-compaction content is never lost. When the echo - // DID come back, the replacement message carries the summaries and - // the injection is suppressed to avoid duplicating them. - const forgedSummaries = echoReplaced - ? [] - : (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; - if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) { - const prompt = withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), ensureCanonicalId(session)); - const devParts = [...projection.systemParts, ...forgedSummaries, prompt]; - if (absorbActive) devParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); - const devContent = devParts.join("\n\n---\n\n"); - responsesDevContent = devContent; - rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); - if (!process.env.ACP_NO_INJECT_TOOL && injectTools) { - const respExtra = [...(absorbActive ? [absorbTools.responses] : []), ...(rulesActive ? [RULE_TOOL_RESPONSES] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).responses] : []), ...(imageCompressionEnabled(session) ? [IMAGE_FULL_TOOL_RESPONSES] : [])]; - toolsOut = responsesTextProtocol - ? injectResponsesTool(parsed.tools, BILI_ACP_READONLY_TOOLS_RESPONSES, surface?.toolPrompts) - : injectResponsesTool(parsed.tools, respExtra.length > 0 ? [...BILI_ACP_TOOLS_RESPONSES, ...respExtra] : BILI_ACP_TOOLS_RESPONSES, surface?.toolPrompts); - } - } else if (projection.systemParts.length > 0 || forgedSummaries.length > 0) { - const devContent = [...projection.systemParts, ...forgedSummaries].join("\n\n---\n\n"); - responsesDevContent = devContent; - rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); - } - if (sysNotes.length > 0) { - const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" - ? [{ type: "message", role: "user", content: rebuiltInput }] - : rebuiltInput; - for (const text of sysNotes) { - inputItems.push({ type: "message", role: "user", content: text }); - } - rebuiltInput = inputItems; - } - // A nudge appended after a trailing `compaction_trigger` would break - // the upstream's "must be the final input item" requirement and is - // redundant — the native compact IS the compression. Otherwise injected - // in BOTH modes (#451): plugin agents supply the ACP tools but have no - // nudge channel of their own, so this proxy-side nudge is the proactive - // trigger (preflight alone fires only at the hard limit). Ephemeral user - // message — not persisted, prefix-cache-anchor safe. - if (willInjectNudge && turn.nudge) { - try { - const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); - if (rendered.text) { - const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" - ? [{ type: "message", role: "user", content: rebuiltInput }] - : rebuiltInput; - inputItems.push({ type: "message", role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }); - rebuiltInput = inputItems; - } - } catch { - } - } - // [#1095] restore-channel guidance — ephemeral trailing note (see - // prepareAnthropic). Never after a compaction_trigger: that item must - // stay the final input item (#283/#209), and the forge path for trigger - // requests builds its own body anyway. - const imgNote = imageFullTrailingNote(session); - if (imgNote && !isCompactionTrigger) { - const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" - ? [{ type: "message", role: "user", content: rebuiltInput }] - : rebuiltInput; - inputItems.push({ type: "message", role: "user", content: imgNote }); - rebuiltInput = inputItems; - } - transformOk = true; - } catch (err) { - log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); - processedMessages = []; - } - - // E2 trigger form: codex's native remote-compaction request (final input item - // is compaction_trigger). When the kill-switch is on, the client is codex, and - // the safety gate passes, forge a success SSE (one compaction item + - // response.completed) and skip upstream — a deterministic handoff to the ACP - // state instead of a foreign compaction blob. - let codexForge: Prepared["codexForge"] | undefined; - if (transformOk - && codexCompactMode() === "intercept" - && isCodexClient(req.headers) - && hasCompactionTrigger(parsed.input) - && codexCompactGate(session, config.modelContextLimit, transformOk)) { - const summaries = session.state.blocks.filter((b) => b.active).map((b) => b.summary); - const prevForged = session.metadata.codexForgedSummaries as string[] | undefined; - const captured = mergeForgedSummaries(prevForged, session.state.blocks); - if (captured.length !== (prevForged?.length ?? 0)) { - session.metadata.codexForgedSummaries = captured; - markDirty(session); - } - // Codex recomputes its ledger from the next real request, but the - // usage we mint here must not read as an empty context: fall back to - // estimating the trigger payload itself when lastInputTokens is - // stale/zero. The reply honors parsed.stream (JSON body when not - // streaming). - const est = defaultCountTokens(typeof parsed.input === "string" ? parsed.input : JSON.stringify(parsed.input ?? "")); - const total = Math.max(session.stats.lastInputTokens, est, 1); - codexForge = { - kind: "trigger", - ...buildTriggerForgeBody(summaries.join("\n\n"), { inputTokens: total, outputTokens: 0, totalTokens: total }, stream), - }; - log("info", `[${sessionId}] codex compact intercepted (trigger); forged SSE with ${summaries.length} block summary(s), upstream not contacted`); - } - - const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut }; - warnResponsesReasoningPairs(Array.isArray(rebuiltInput) ? rebuiltInput : [], log, sessionId); - if (!isCompactionTrigger) { - clampOutgoingOutput(rebuilt as Record, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("responses", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log); - } - // Route with the upstream THIS request goes to — session.meta.upstreamOrigin - // is first-wins and would keep injecting pck toward a relay we switched - // away from (same class of bug as the compressProtocol fix above, #286). - const promptCacheKey = resolvePromptCacheKey( - rebuilt.prompt_cache_key, - identity, - opts.promptCache.routing, - upstreamOrigin, - ); - if (promptCacheKey && !rebuilt.prompt_cache_key) rebuilt.prompt_cache_key = promptCacheKey; - // This adapter is stateless: we replay the FULL conversation in `input`. - // Strip Responses' native chaining field so the upstream does not resolve - // stored server-side state ON TOP of the input we already sent (which would - // duplicate history for clients that use store:true + chaining). Empirically - // codex sends store:false and never sets previous_response_id, so this is a - // no-op for codex — kept defensively for any client that does chain. Set - // ACP_KEEP_RESPONSE_ID=1 to preserve it (diagnostic only). `instructions` - // was already lifted into the developer message at input[1]; forwarding it - // again here double-sends it and violates the responses_lite contract - // (top-level instructions must stay empty for code_mode tool exposure). - if (process.env.ACP_KEEP_RESPONSE_ID !== "1") delete rebuilt.previous_response_id; - delete rebuilt.instructions; - // Same rationale as prepareOpenai: strip the OpenAI-host-only cache - // directive; keep prompt_cache_key. Sent by hermes' codex transport and - // by any PI_CACHE_RETENTION=long client. - delete (rebuilt as Record).prompt_cache_retention; - // Log the final tools we forward upstream so we can confirm ACP tools are - // present. Distinguishes "compress" (top-level function) from Codex - // namespace items (type:namespace/custom). - if (process.env.ACP_DEBUG) { - const fwdTools = (Array.isArray(toolsOut) ? toolsOut : []).map((t) => { - const r = t as Record; - const sub = Array.isArray(r.tools) ? `(${r.tools.length} sub)` : ""; - return `${r.type as string}:${(r.name as string) ?? "?"}${sub}`; - }); - log("info", `[${sessionId}] responses forward tools=[${fwdTools.join(",")}] injectTool=${injectTools}${pluginMode ? " (plugin mode: wire injection suppressed)" : ""} NO_INJECT_TOOL=${!!process.env.ACP_NO_INJECT_TOOL} NO_COMPRESS_PROMPT=${!!process.env.ACP_NO_COMPRESS_PROMPT}`); - } - // #532: measure the outbound developer(system)+tools overhead for the panel. - // On this wire the system rides the injected developer message outside the - // fold space, so counting devContent + tools does not double-count the - // mid-history items the kernel already classifies. - if (transformOk) { - session.metadata.systemPromptTokens = countSystemAndToolsTokens(responsesDevContent ?? "", toolsOut); - } - // #728: record this turn's outbound payload upper bound as the fallback - // token source for upstreams that never report usage (see effectiveTokenCount). - // Compaction-trigger requests are the compression mechanism itself — no - // incremental decision hangs off them, so don't leave a stale reading. - if (!isCompactionTrigger) { - session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) - + countSystemAndToolsTokens(responsesDevContent ?? "", toolsOut) - + imageTokensInParsedBody("responses", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)); - } - snapshotMessages(session, originalMessages); - markDirty(session); - return { - body: JSON.stringify(rebuilt), - session, - processedMessages, - originalMessages, - responsesProjection, - systemNotes: sysNotes, - protocol: "responses", - stream, - compressInjected: injectTools && !isCompactionTrigger, - pluginMode, - responsesTextProtocol, - nudge, - prompts, - surface, - renderTags, - resetAfterSuccess: isCompactionTrigger, - codexForge, - }; -} export function isCountTokensRequest(method: string, urlPath: string, hasBody: boolean): boolean { return ( @@ -3702,131 +2328,7 @@ export function isCountTokensRequest(method: string, urlPath: string, hasBody: b ); } -export function prepareCountTokens( - parsed: AnthropicRequestBody, - core: CompressionCore, - config: Config, - log: (level: string, msg: string) => void, - session: Session, -): Prepared { - const sessionId = session.id; - try { - const { msgs, cacheControls } = anthropicToCore(parsed); - // Read-only preview: same policy as the google twin above. - const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); - const stripped = stripKernelSummaries(turn.messages as BiliMessage[], turn.state); - const rebuiltMessages = coreToAnthropic(stripped, cacheControls); - log("info", `[${sessionId}] count_tokens pruned: ${msgs.length} → ${stripped.length} msgs`); - const rebuilt: AnthropicRequestBody = { ...parsed, messages: rebuiltMessages }; - delete (rebuilt as Record).prompt_cache_key; - return { - body: JSON.stringify(rebuilt), - session, - processedMessages: [], - originalMessages: msgs, - protocol: "anthropic", - stream: false, - compressInjected: false, - }; - } catch (err) { - log("warn", `[${sessionId}] count_tokens prune failed, forwarding unchanged: ${String(err)}`); - const fallback: AnthropicRequestBody = { ...parsed }; - delete (fallback as Record).prompt_cache_key; - return { - body: JSON.stringify(fallback), - session, - processedMessages: [], - originalMessages: [], - protocol: "anthropic", - stream: false, - compressInjected: false, - }; - } -} -function prepareResponsesCompact( - body: Buffer, - parsed: ResponsesRequestBody, - session: Session, - req: http.IncomingMessage, - core: CompressionCore, - config: Config, - log: (level: string, msg: string) => void, -): Prepared { - ++session.stats.requests; - // A bili-forged compaction item is never for the upstream (it carries our - // sentinel blob) — strip it on every forwarding path, same as the normal - // /responses pipeline does. - const cleaned = Array.isArray(parsed.input) ? stripBiliCompactionItems(parsed.input) : parsed.input; - const stripped = Array.isArray(parsed.input) && cleaned.length !== parsed.input.length; - const forgeBody: ResponsesRequestBody = { ...parsed, input: cleaned }; - const base: Prepared = { - body: stripped ? Buffer.from(JSON.stringify(forgeBody)) : body, - session, - processedMessages: [], - originalMessages: [], - protocol: "responses", - stream: parsed.stream === true, - compressInjected: false, - resetAfterSuccess: true, - }; - // #332: gate preconditions BEFORE the transform — when they fail the - // request passes through verbatim without running processTurn (no state - // mutation as a side effect of a compact that will not be intercepted). - if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input) - || !codexCompactGatePre(session, config.modelContextLimit)) { - return base; - } - // The state commit below is all-or-nothing: every non-forge path restores - // the pre-turn state so a passthrough compact is not raced against a - // half-applied fold (the upstream's own compaction boundary is handled by - // markNativeCompactionBoundary + rebase instead). - const prevState = session.state; - const prevStore = session.contentStore; - const prevStoreDirty = session.contentStoreDirty === true; - // E2 endpoint form: /responses/compact. When the gate passes, run the same - // fold pipeline as a normal turn and forge the compacted history as - // {"output": [...]} — a deterministic handoff to the ACP state instead of a - // foreign compaction blob. - let transformOk = false; - try { - const projection = responsesToCore(forgeBody); - // The forged handoff is one-shot with no tool channel: strip absorb so - // no [ACP absorb] instruction bakes into the forged history, and run - // the absorb view so absorbed pairs stay hidden in it (wire parity). - const compactConfig = ccrLoopConfig(session, { ...config, absorb: undefined }); - const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); - session.state = turn.state; - adoptContentStore(session, turn.contentStore); - transformOk = true; - if (!codexCompactGate(session, config.modelContextLimit, transformOk)) { - session.state = prevState; - session.contentStore = prevStore; - session.contentStoreDirty = prevStoreDirty; - return base; - } - const viewed = applyAbsorbView(turn.messages, turn.state, compactConfig, session.stats.lastInputTokens); - const processed = repairResponsesAssistantOrdering(stripKernelSummaries(viewed, turn.state), projection.msgs); - let output = patchResponsesInput(projection, processed); - if (typeof output === "string") { - session.state = prevState; - session.contentStore = prevStore; - session.contentStoreDirty = prevStoreDirty; - return base; - } - output = hoistTrappedToolItems(output); - snapshotMessages(session, projection.msgs); - markDirty(session); - log("info", `[${session.id}] codex compact intercepted (endpoint); forged history with ${output.length} item(s), upstream not contacted`); - return { ...base, codexForge: { kind: "endpoint", body: JSON.stringify({ output }), contentType: "application/json" } }; - } catch (err) { - session.state = prevState; - session.contentStore = prevStore; - session.contentStoreDirty = prevStoreDirty; - log("warn", `[${session.id}] codex compact forge failed (${String(err)}); passing through to upstream`); - return base; - } -} export function isChatGptCodexUpstream(upstream: string | undefined): boolean { if (!upstream) return false; @@ -3844,145 +2346,15 @@ export function isCodexResponsesLite(headers: http.IncomingHttpHeaders, _body: R return false; } -export function shouldInjectPromptCacheKey( - routing: ProxyOptions["promptCache"]["routing"], - upstream: string | undefined, -): boolean { - if (routing === "enabled") return true; - if (routing === "disabled" || !upstream) return false; - try { - return new URL(upstream).hostname.toLowerCase() === "api.openai.com"; - } catch { - return false; - } -} -export function resolvePromptCacheKey( - explicit: string | undefined, - identity: ConversationIdentity, - routing: ProxyOptions["promptCache"]["routing"], - upstream: string | undefined, -): string | undefined { - if (explicit?.trim()) return explicit; - if (!identity.clientProvided || !shouldInjectPromptCacheKey(routing, upstream)) return undefined; - return identity.value; -} -// Claude Code's auto-mode safety classifier one-shots expect a strict XML -// verdict — the default `xml_2stage` mode stops the response at `` -// or ``. These are not compressible conversations: the compress -// system-prompt + ACP tools (or the kernel round-trip) derailed the small model -// from that verdict, so the classifier reported "could not evaluate" (#353). -// The magic stop sequences are the only in-body signal; forward them untouched. -const AUTO_MODE_CLASSIFIER_STOPS = new Set(["", ""]); -function isAutoModeClassifier(parsed: AnthropicRequestBody): boolean { - const stops = parsed.stop_sequences; - if (!Array.isArray(stops)) return false; - return stops.some((s) => typeof s === "string" && AUTO_MODE_CLASSIFIER_STOPS.has(s)); -} -// #1359: which absorb block governs a session, by lane. Proxy lane keeps the -// per-request merged block (provider/model overrides apply); plugin lane uses -// the base block so the manifest's advertised name and the gate's adjudicated -// name always agree — provider/model absorb.* overrides are proxy-lane-only. -function effectiveAbsorbBlock(pluginMode: boolean, config: Config, baseAbsorb?: CompressSettings["absorb"]): AbsorbConfig | undefined { - return pluginMode ? resolveAbsorbSettings(baseAbsorb) : config.absorb; -} -function injectSystem( - parsed: AnthropicRequestBody, - opts: ProxyOptions, - prompts: Prompts = defaultPrompts, - config: Config, - noteId: string, - surface?: PackSurface, - visibilityMarkers = true, -): string | AnthropicRequestBody["system"] { - // ONLY the static compress prompt goes into the system block — it is the - // prefix-cache anchor and must stay byte-stable across turns. The nudge - // (which changes every turn) is appended as a trailing user message by - // the caller (prepareAnthropic), never merged into system. - const baseText = extractSystem(parsed.system); - const parts: string[] = []; - if (opts.compress.injectTool) parts.push(withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), noteId)); - if (opts.compress.injectTool && absorbEnabled(config)) parts.push(buildAbsorbSystemPrompt(absorbToolName(config))); - if (parts.length === 0) return parsed.system; - const full = baseText ? `${baseText}\n\n---\n\n${parts.join("\n\n")}` : parts.join("\n\n"); - return buildSystem(full, parsed.system); -} -// #920: in proxy mode bili OWNS the compression tool names. Agent-side tools -// with the same name (opencode-acp's statically-registered DCP set ships in -// every opencode request body — the v1 tool registry is process-global and -// cannot be filtered per request) are dropped here so the upstream sees -// exactly one definition per name, and it is bili's (its arg schemas are what -// the compress loop dispatches on). Plugin mode never calls these helpers. -function injectTool(tools: unknown[] | undefined, extras?: readonly { name: string }[], toolPrompts?: ToolPrompts): unknown[] { - const acp = applyAcpToolOverrides(BILI_ACP_TOOLS_ANTHROPIC, toolPrompts); - const list = extras ?? []; - if (!Array.isArray(tools)) return [...acp, ...list]; - const owned = new Set(acp.map((t) => t.name)); - for (const e of list) owned.add(e.name); - const kept = tools.filter((t) => { - const n = (t as { name?: string })?.name; - return typeof n !== "string" || !owned.has(n); - }); - return [...kept, ...acp, ...list]; -} -function injectOpenaiTool(tools: OpenAITool[] | undefined, extras?: readonly OpenAITool[], toolPrompts?: ToolPrompts): OpenAITool[] { - const acp = applyAcpToolOverrides(BILI_ACP_TOOLS_OPENAI, toolPrompts) as OpenAITool[]; - const list = extras ?? []; - if (!Array.isArray(tools)) return [...acp, ...list] as OpenAITool[]; - const owned = new Set(acp.map((t) => t.function.name)); - for (const e of list) owned.add(e.function.name); - const kept = tools.filter((t) => { - const n = t?.function?.name; - return typeof n !== "string" || !owned.has(n); - }); - return [...kept, ...acp, ...list]; -} -/** Merge the ACP declarations into the client's Gemini `tools` array. Gemini - * nests declarations one level deeper than the OpenAI shape - * (`tools[].functionDeclarations[]`), so presence is collected across every - * entry and the missing declarations are appended as one new entry. */ -function injectGoogleTool(tools: GoogleTool[] | undefined, extra?: { name: string }[], toolPrompts?: ToolPrompts): GoogleTool[] { - const acp = applyAcpToolOverrides(BILI_ACP_TOOLS_GOOGLE, toolPrompts) as GoogleFunctionDeclaration[]; - const wanted: { name: string }[] = extra ? [...acp, ...extra] : [...acp]; - if (!Array.isArray(tools)) return [{ functionDeclarations: wanted as GoogleFunctionDeclaration[] }]; - const present = new Set(); - for (const tool of tools) { - for (const decl of tool?.functionDeclarations ?? []) { - if (typeof decl?.name === "string") present.add(decl.name); - } - } - const missing = wanted.filter((t) => !present.has(t.name)); - if (missing.length === 0) return tools; - return [...tools, { functionDeclarations: missing as GoogleFunctionDeclaration[] }]; -} -/** When true, the Responses path teaches compression via a text trigger - * instead of a function tool. Used for hosts (OpenAI Codex code_mode) whose - * server-side tools are disabled the moment any `tools` entry is declared. - * In text mode we keep `tools` untouched (undefined) so code_mode stays - * active, and detect the trigger in the output_text stream instead. */ -const FORCE_TEXT_PROTOCOL = process.env.ACP_COMPRESS_PROTOCOL === "text"; -/** Inject all ACP tools (compress/decompress/search_context/acp_status) in - * Responses API flat format, matching the PROXY_TOOL_NAMES set the compress - * loop dispatches on. Idempotent. */ -function injectResponsesTool(tools: unknown[] | undefined, toolsToAdd: readonly { name: string }[] = BILI_ACP_TOOLS_RESPONSES, toolPrompts?: ToolPrompts): unknown[] { - const base = applyAcpToolOverrides(toolsToAdd, toolPrompts); - if (!Array.isArray(tools)) return [...base]; - // Same #920 rule as injectTool/injectOpenaiTool: bili owns these names. - const owned = new Set(base.map((t) => t.name)); - const kept = tools.filter((t) => { - const n = (t as { name?: string })?.name; - return typeof n !== "string" || !owned.has(n); - }); - return [...kept, ...base]; -} type ForwardTarget = { upstreamUrl: string; @@ -5562,16 +3934,6 @@ async function resolveFakeCompletion( return buffer; } -/** Derive a short human-readable title from the first user text message. - * Used so the web UI can show "Fix auth bug" instead of an opaque hash. */ -function deriveTitle(messages: CoreMessage[]): string | undefined { - for (const m of messages) { - if (m.role !== "user" || m.contentType !== "text") continue; - const clean = (m.text ?? "").replace(/\s+/g, " ").trim(); - if (clean) return clean.length > 60 ? clean.slice(0, 57) + "\u2026" : clean; - } - return undefined; -} function handleConfigReload(opts: ProxyOptions, res: http.ServerResponse, log: (level: string, msg: string) => void): void { // Hot-reload routes from the config file into the running process — no @@ -5616,16 +3978,6 @@ function sendCacheReport(res: http.ServerResponse, url: string): void { res.end(JSON.stringify({ reports: sessions.map((s) => ({ id: s.id, report: buildSessionCacheReport(s) })) }, null, 2)); } -// #1206: orphan reaping was silent — blocks deactivated because their source -// messages vanished from client history are the strongest runtime signal that -// something outside bili (client auto-compaction or another compression plugin) -// rewrote the conversation. Log it and record it in the session ledger. -function reapOrphansLogged(session: Session, msgs: CoreMessage[], log: (level: string, msg: string) => void, sessionId: string): void { - const { reaped } = reapOrphanBlocks(session, msgs, deactivateBlock); - if (reaped.length === 0) return; - log("warn", `[${sessionId}] orphan-gc deactivated ${reaped.length} block(s) whose source messages left the client history (${reaped.join(", ")}) — the client or another compression plugin deleted summarized content; those summaries can no longer be decompressed (#1206)`); - recordConflict(session, "orphan-reap", `${reaped.length} block(s) deactivated: ${reaped.join(", ")}`); -} function sendStats(res: http.ServerResponse): void { const all = listSessions(); diff --git a/src/server/inject.ts b/src/server/inject.ts new file mode 100644 index 0000000000..bb943b735b --- /dev/null +++ b/src/server/inject.ts @@ -0,0 +1,97 @@ +// ACP tool/system injection helpers shared by the per-protocol prepare +// pipelines (extracted verbatim from src/server.ts — #1440 P2 four-cut +// disassembly, cut 2). + +import { absorbEnabled, absorbToolName } from "../absorb.js"; +import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildCompressSystemPrompt, withConversationIdNote, withMarkerIntegrityNote, withSummaryBudgetNote } from "../compress-tool.js"; +import { ProxyOptions } from "../config.js"; +import { Config, PackSurface, Prompts, ToolPrompts, applyAcpToolOverrides, defaultPrompts } from "acp-kernel"; +import { AnthropicRequestBody, GoogleFunctionDeclaration, GoogleTool, OpenAITool, buildSystem, extractSystem } from "acp-kernel/wire"; + +export function injectSystem( + parsed: AnthropicRequestBody, + opts: ProxyOptions, + prompts: Prompts = defaultPrompts, + config: Config, + noteId: string, + surface?: PackSurface, + visibilityMarkers = true, +): string | AnthropicRequestBody["system"] { + // ONLY the static compress prompt goes into the system block — it is the + // prefix-cache anchor and must stay byte-stable across turns. The nudge + // (which changes every turn) is appended as a trailing user message by + // the caller (prepareAnthropic), never merged into system. + const baseText = extractSystem(parsed.system); + const parts: string[] = []; + if (opts.compress.injectTool) parts.push(withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), noteId)); + if (opts.compress.injectTool && absorbEnabled(config)) parts.push(buildAbsorbSystemPrompt(absorbToolName(config))); + if (parts.length === 0) return parsed.system; + const full = baseText ? `${baseText}\n\n---\n\n${parts.join("\n\n")}` : parts.join("\n\n"); + return buildSystem(full, parsed.system); +} + +// #920: in proxy mode bili OWNS the compression tool names. Agent-side tools +// with the same name (opencode-acp's statically-registered DCP set ships in +// every opencode request body — the v1 tool registry is process-global and +// cannot be filtered per request) are dropped here so the upstream sees +// exactly one definition per name, and it is bili's (its arg schemas are what +// the compress loop dispatches on). Plugin mode never calls these helpers. +export function injectTool(tools: unknown[] | undefined, extras?: readonly { name: string }[], toolPrompts?: ToolPrompts): unknown[] { + const acp = applyAcpToolOverrides(BILI_ACP_TOOLS_ANTHROPIC, toolPrompts); + const list = extras ?? []; + if (!Array.isArray(tools)) return [...acp, ...list]; + const owned = new Set(acp.map((t) => t.name)); + for (const e of list) owned.add(e.name); + const kept = tools.filter((t) => { + const n = (t as { name?: string })?.name; + return typeof n !== "string" || !owned.has(n); + }); + return [...kept, ...acp, ...list]; +} + +export function injectOpenaiTool(tools: OpenAITool[] | undefined, extras?: readonly OpenAITool[], toolPrompts?: ToolPrompts): OpenAITool[] { + const acp = applyAcpToolOverrides(BILI_ACP_TOOLS_OPENAI, toolPrompts) as OpenAITool[]; + const list = extras ?? []; + if (!Array.isArray(tools)) return [...acp, ...list] as OpenAITool[]; + const owned = new Set(acp.map((t) => t.function.name)); + for (const e of list) owned.add(e.function.name); + const kept = tools.filter((t) => { + const n = t?.function?.name; + return typeof n !== "string" || !owned.has(n); + }); + return [...kept, ...acp, ...list]; +} + +/** Merge the ACP declarations into the client's Gemini `tools` array. Gemini + * nests declarations one level deeper than the OpenAI shape + * (`tools[].functionDeclarations[]`), so presence is collected across every + * entry and the missing declarations are appended as one new entry. */ +export function injectGoogleTool(tools: GoogleTool[] | undefined, extra?: { name: string }[], toolPrompts?: ToolPrompts): GoogleTool[] { + const acp = applyAcpToolOverrides(BILI_ACP_TOOLS_GOOGLE, toolPrompts) as GoogleFunctionDeclaration[]; + const wanted: { name: string }[] = extra ? [...acp, ...extra] : [...acp]; + if (!Array.isArray(tools)) return [{ functionDeclarations: wanted as GoogleFunctionDeclaration[] }]; + const present = new Set(); + for (const tool of tools) { + for (const decl of tool?.functionDeclarations ?? []) { + if (typeof decl?.name === "string") present.add(decl.name); + } + } + const missing = wanted.filter((t) => !present.has(t.name)); + if (missing.length === 0) return tools; + return [...tools, { functionDeclarations: missing as GoogleFunctionDeclaration[] }]; +} + +/** Inject all ACP tools (compress/decompress/search_context/acp_status) in + * Responses API flat format, matching the PROXY_TOOL_NAMES set the compress + * loop dispatches on. Idempotent. */ +export function injectResponsesTool(tools: unknown[] | undefined, toolsToAdd: readonly { name: string }[] = BILI_ACP_TOOLS_RESPONSES, toolPrompts?: ToolPrompts): unknown[] { + const base = applyAcpToolOverrides(toolsToAdd, toolPrompts); + if (!Array.isArray(tools)) return [...base]; + // Same #920 rule as injectTool/injectOpenaiTool: bili owns these names. + const owned = new Set(base.map((t) => t.name)); + const kept = tools.filter((t) => { + const n = (t as { name?: string })?.name; + return typeof n !== "string" || !owned.has(n); + }); + return [...kept, ...base]; +} diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts new file mode 100644 index 0000000000..853c01d49b --- /dev/null +++ b/src/server/prepare-anthropic.ts @@ -0,0 +1,357 @@ +// Anthropic protocol prepare pipeline (extracted verbatim from +// src/server.ts — #1440 P2 four-cut disassembly, cut 2). + +import { applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; +import { stripAcpPanelMessages, stripAcpStatusMarkers } from "../acp-panel.js"; +import { ABSORB_TOOL_NAME, IMAGE_FULL_TOOL, RULE_TOOL, absorbToolsFor, retrieveToolsFor, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "../compress-tool.js"; +import { ProxyOptions } from "../config.js"; +import { recordConflict } from "../conflict-watch.js"; +import { applyImageCompressionPass, imageCompressionEnabled, imageFullTrailingNote } from "../image-compress.js"; +import { imageTokensInParsedBody } from "../image-tokens.js"; +import { estimateCoreMessagesUpper } from "../preflight.js"; +import { CompressReasoningConfig } from "../reasoning-drop.js"; +import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; +import { countSystemAndToolsTokens, emergencyNudge, projectThinkingMass } from "../server/budget.js"; +import { PendingRetrieval, REWRITE_MIN_INCOMING_TOTAL, Session, applyCompactionArchive, detectUnannouncedHistoryRewrite, ensureCanonicalId, foldCoverage, markCompactionBoundary, markDirty, snapshotMessages } from "../session.js"; +import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, dropRetrievals, flushRetrievalNotes, pruneExpiredRetrievals, reconcileReloadedRetrievals, retrieveToolName, snapshotPendingRetrievals } from "../store.js"; +import { isStrictReasoningEcho, modelIdOf } from "../strict-echo.js"; +import { reconcileSystemAnchor } from "../system-anchor.js"; +import { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts, renderNudgeText, viableRanges } from "acp-kernel"; +import { AnthropicRequestBody, BiliMessage, anthropicToCore, buildSystem, coreToAnthropic, extractSystem } from "acp-kernel/wire"; +import http from "node:http"; +import { injectSystem, injectTool } from "./inject.js"; +import { Prepared, deriveTitle, diagNudge, diagTagSummary, effectiveAbsorbBlock, effectiveTokenCount, imageBillingFor, reapOrphansLogged, stripKernelSummaries, withReasoningDrop } from "./prepare-shared.js"; + +/** [#684] Anthropic-wire twin of the openai sentinel, narrowed by [#1327]: + * warn only when bili itself lost thinking — a tool_use block that rode an + * inbound assistant message WITH a thinking block now rides an outbound + * assistant message with none. Outbound-only asymmetry (some tool_use turns + * think, others don't) is ordinary Claude Code traffic: turns without + * extended thinking never carry a block, and #651's dropCompressReasoning + * creates the same shape by design — the old heuristic warned on every + * healthy multi-turn session. Turns match by stable tool_use id (the kernel + * codec round-trips it verbatim); benign asymmetry stays fully silent. */ +export function warnAnthropicThinkingPairs( + inboundMessages: unknown[], + outboundMessages: unknown[], + log: (level: string, msg: string) => void, + sessionId: string, +): void { + const thinkingIds = new Set(); + for (const m of inboundMessages) { + const msg = m as { role?: string; content?: Array<{ type?: string; id?: string }> }; + if (msg?.role !== "assistant" || !Array.isArray(msg.content)) continue; + if (!msg.content.some((b) => b?.type === "thinking")) continue; + for (const b of msg.content) { + if (b?.type === "tool_use" && typeof b.id === "string") thinkingIds.add(b.id); + } + } + if (thinkingIds.size === 0) return; + let lost = 0; + const lostIds: string[] = []; + for (const m of outboundMessages) { + const msg = m as { role?: string; content?: Array<{ type?: string; id?: string }> }; + if (msg?.role !== "assistant" || !Array.isArray(msg.content)) continue; + if (msg.content.some((b) => b?.type === "thinking")) continue; + for (const b of msg.content) { + if (b?.type === "tool_use" && typeof b.id === "string" && thinkingIds.has(b.id)) { + lost++; + lostIds.push(b.id); + } + } + } + if (lost > 0) { + log("warn", `[${sessionId}] thinking-pair-violated: ${lost} tool_use block(s) lost their inbound thinking block in the outbound rebuild (${[...new Set(lostIds)].slice(0, 3).join(", ")}) — a stripped signature pair is rejected by the API (#684)`); + } +} + +export async function prepareAnthropic( + parsed: AnthropicRequestBody, + req: http.IncomingMessage, + opts: ProxyOptions, + core: CompressionCore, + config: Config, + prompts: Prompts, + surface: PackSurface, + log: (level: string, msg: string) => void, + session: Session, + pluginMode: boolean, + upstreamOrigin: string, + reasoning: CompressReasoningConfig | undefined, + visibilityMarkers: boolean, +): Promise { + const sessionId = session.id; + const stream = parsed.stream === true; + ++session.stats.requests; + const injectTools = opts.compress.injectTool && !pluginMode; + const stripReasoning = (msgs: BiliMessage[]): BiliMessage[] => withReasoningDrop(msgs, reasoning, log, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))); + + if (isAutoModeClassifier(parsed)) { + log("info", `[${sessionId}] auto-mode classifier passthrough (skipping compress injection)`); + return { body: JSON.stringify(parsed), session, processedMessages: [], originalMessages: [], anthropicSystem: parsed.system, protocol: "anthropic", stream, compressInjected: false, pluginMode, nudge: undefined, prompts, surface } as Prepared; + } + + let processedMessages: CoreMessage[] = []; + let attachedRetrievals: PendingRetrieval[] = []; + let originalMessages: CoreMessage[] = []; + let nudge: NudgeDecision | undefined; + let rebuiltMessages = parsed.messages; + let systemOut = parsed.system; + let toolsOut = parsed.tools; + + // #1085: sticky head-system anchor — freeze the client's own `system` text + // at first sight and forward it byte-stable; detected changes ride as + // trailing user notes. Mutating parsed.system in place makes every + // downstream consumer (injectSystem, Prepared.anthropicSystem → loop) + // inherit the anchor, and the failure path below keeps forwarding it too. + let sysNotes: string[] = []; + // Plugin-mode agents own their context management and may already apply + // their own cache-friendly head handling (#1085 scope: plain-proxy mode + // only) — anchoring them would double-process. + if (opts.stableSystemAnchor && !pluginMode) { + const fresh = extractSystem(parsed.system); + const outcome = reconcileSystemAnchor(session, "anthropic", fresh, sessionId, log); + sysNotes = outcome.notes; + if (outcome.outbound !== fresh) { + parsed.system = buildSystem(outcome.outbound, parsed.system); + } + } + + // The classifier passthrough above and prepareResponsesCompact return before + // this strip; no client emits an ACP panel on those paths, so that's safe. + const strippedPanels = stripAcpPanelMessages(parsed.messages); + if (strippedPanels > 0) { + log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); + } + const strippedMarkerLines = stripAcpStatusMarkers(parsed.messages); + if (strippedMarkerLines > 0) { + log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); + } + + try { + const { msgs, cacheControls } = anthropicToCore(parsed); + originalMessages = msgs; + // #1320: signature-only thinking blocks bill restored thinking tokens upstream + // but project as empty text locally — attribute the provider-vs-local residual + // to them so every meter sees the billed context (metering-only, wire untouched). + const inboundImageTokens = imageTokensInParsedBody("anthropic", parsed, imageBillingFor(opts, upstreamOrigin)); + projectThinkingMass(msgs, { + providerInputTokens: session.stats.lastInputTokens, + measured: session.stats.lastInputTokensSource === "usage", + systemText: extractSystem(parsed.system), + tools: parsed.tools, + imageTokens: inboundImageTokens, + storedOverhead: typeof session.metadata.systemPromptTokens === "number" ? session.metadata.systemPromptTokens : undefined, + }); + // #1001: pre-turn snapshot — processTurn below assigns fresh refs to every + // previously-unknown id, which would make rewrite detection read 1.0. + const knownRefsBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); + // tokenCount drives the nudge decision ("should we compress?"). It MUST + // be the real context size, never an estimate — estimates undercount + // CJK text 3-4x and never trigger compression for Chinese sessions. + // Use the upstream's own input_tokens from the PREVIOUS turn (known by + // now — the response came back). First turn has no history → 0 (never + // triggers anyway). extractSystem is still called so sysText flows into + // the fallback path below if we ever need it, but we no longer feed + // estimates to the kernel. + extractSystem(parsed.system); + // #553-follow-up exception to the "never estimates" rule above: anonymous + // zero-baseline forks replay their FULL raw history with no measurement, + // so feeding 0 blinds the nudge (usage 0%, growth ref 0) and no + // compression trigger fires until overflow. See effectiveTokenCount. + const tokenCount = effectiveTokenCount(session, msgs, inboundImageTokens); + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + // Absorb markers are injected by the kernel's processTurn from + // config.absorb. With no channel to call the tool (injection off), + // strip absorb from the loop config so the REQUIRED instruction never + // reaches the wire. Hiding recorded absorptions is unaffected + // (applyAbsorbView hides regardless of enablement). + const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); + const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); + const absorbActive = absorbBlock?.enabled === true && opts.compress.injectTool; + // acp_rule has no processTurn side effect (no markers/instructions are + // ever injected into messages), so unlike absorb it needs no loop- + // config stripping — only tool availability matters. + const rulesActive = rulesEnabled(config) && opts.compress.injectTool; + // [#1097] the kernel ccr-store node ID-references oversized tool results + // BEFORE absorb (ID-reference wins over distill); armed policy is + // stamped per-request — strip `ccr` from the loop config when disarmed + // (plugin mode / no tool channel) so placeholders never hit the wire. + const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside + // processTurn may deactivate fully-drifted blocks, erasing them. + const foldCoveredBefore = session.stats.pendingFoldUsage === true + ? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : []))) + : null; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); + session.state = turn.state; + adoptContentStore(session, turn.contentStore); + // The fold from last turn's compress has now materialized in state — + // future usage reports are post-fold reality, drop the credit. + session.stats.compressCreditTokens = 0; + if (foldCoveredBefore !== null && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) { + const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); + if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); + } + storeEffectiveAbsorb(session, loopConfig); + storeEffectiveRules(session, config); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // Drop sub-viability fragments before any consumer sees them: a tiny + // range in the list makes batched compress attempts fail atomically + // (kernel validates the whole batch). Mirrors billion-context-pi. + if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); + nudge = turn.nudge; + session.stats.contextTokens = tokenCount; + if (!session.meta.title) { + const t = deriveTitle(msgs); + if (t) session.meta.title = t; + } + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); + log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); + processedMessages = stripReasoning(stripKernelSummaries(turn.messages, turn.state)); + // #1001: a silent client history rewrite takes the same archive+prune path + // as an announced /compact boundary — syncBlocks above has already + // deactivated the blocks whose sources left the context. + { + const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id)); + if (rewrite.detected) { + log("warn", `[${sessionId}] unannounced client history rewrite detected (${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known) — marking compaction boundary (#1001)`); + recordConflict(session, "unannounced-rewrite", `${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known`); + markCompactionBoundary(session); + } + } + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); + reapOrphansLogged(session, msgs, log, sessionId); + // [#1095] downscale screenshot-like images ONCE at arrival (kernel routing + // decision + recipe; originals cached for the image_full restore channel). + // Deterministic encode ⇒ re-runs are byte-stable for the prefix cache. + await applyImageCompressionPass(session, processedMessages as BiliMessage[], { config, billing: imageBillingFor(opts, upstreamOrigin), log }); + // [#1271/#1343] plugin mode: acp_retrieve already acked via the tool API; snapshot + // the queued full text onto THIS forward (stays in the queue until commit/drop, so an + // upstream failure drops-and-logs it instead of vanishing it). + if (pluginMode && ccrEnabled(session)) { + reconcileReloadedRetrievals(session); + pruneExpiredRetrievals(session); + attachedRetrievals = snapshotPendingRetrievals(session); + if (attachedRetrievals.length > 0) processedMessages = [...processedMessages, ...attachedRetrievals.map((i) => i.injection)]; + } + rebuiltMessages = coreToAnthropic(processedMessages as BiliMessage[], cacheControls); + if (sysNotes.length > 0) { + rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; + } + + systemOut = injectSystem(parsed, opts, prompts, loopConfig, ensureCanonicalId(session), surface, visibilityMarkers); + if (injectTools) { + toolsOut = injectTool(parsed.tools, [...(absorbActive ? [absorbTools.anthropic] : []), ...(rulesActive ? [RULE_TOOL] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).anthropic] : []), ...(imageCompressionEnabled(session) ? [IMAGE_FULL_TOOL] : [])], surface?.toolPrompts); + } + // Nudge as a separate trailing user message (cache-friendly): the + // system block stays byte-stable so the prefix cache survives. + // Injected in BOTH modes (#451): in plugin mode the agent supplies the + // ACP tools but has NO nudge channel of its own, so this proxy-side + // nudge IS the proactive trigger — preflight alone only fires at the + // hard limit. Ephemeral user message: not persisted, never enters the + // agent's re-sent history, safe for the prefix-cache anchor. + if (willInjectNudge && turn.nudge) { + try { + const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); + if (rendered.text) { + rebuiltMessages = [...rebuiltMessages, { role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }]; + } + } catch { + } + } + // [#1095] restore-channel guidance — ephemeral trailing user message + // (see prepareAnthropic for why not system). + const imgNote = imageFullTrailingNote(session); + if (imgNote) rebuiltMessages = [...rebuiltMessages, { role: "user", content: imgNote }]; + // [#1343] surface any earlier undelivered retrieve as an ephemeral trailing + // user note (kept last so it never reorders cached messages). + const retrNote = flushRetrievalNotes(session); + if (retrNote) rebuiltMessages = [...rebuiltMessages, { role: "user", content: retrNote }]; + } catch (err) { + log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); + if (attachedRetrievals.length > 0) dropRetrievals(session, attachedRetrievals.map((i) => i.ref), "prepare failed; forwarded unprocessed"); + processedMessages = []; + } + // #532: measure the outbound system+tools overhead for the status panel's + // SysPrompt row — the kernel breakdown classifies messages only, and on + // this wire the system rides the top-level `system` field outside the fold. + session.metadata.systemPromptTokens = countSystemAndToolsTokens(extractSystem(systemOut), toolsOut); + snapshotMessages(session, originalMessages); + markDirty(session); + + const rebuilt: AnthropicRequestBody = { ...parsed, messages: rebuiltMessages, system: systemOut, tools: toolsOut }; + warnAnthropicThinkingPairs(parsed.messages, rebuiltMessages, log, sessionId); + // prompt_cache_key is the omp plugin's session id stamped for the proxy's + // identity chain (#268), not part of the Anthropic Messages API — strip it + // so the real upstream never sees a field it doesn't know. + delete (rebuilt as Record).prompt_cache_key; + // #728: record the char-count upper bound of THIS turn's outbound payload + // (post-fold messages + system/tools overhead + images) as the fallback + // token source for upstreams that never report usage — read only while + // lastInputTokens == 0 (effectiveTokenCount). When the kernel transform + // above failed, processedMessages is empty and the forwarded body is the + // UNPROCESSED projection — measure that instead so the fallback isn't + // blinded to a system+tools-only floor. + session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + + countSystemAndToolsTokens(extractSystem(systemOut), toolsOut) + + imageTokensInParsedBody("anthropic", rebuilt, imageBillingFor(opts, upstreamOrigin)); + return { body: JSON.stringify(rebuilt), session, attachedRetrievals, processedMessages, originalMessages, anthropicSystem: parsed.system, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" } as Prepared; +} + +export function prepareCountTokens( + parsed: AnthropicRequestBody, + core: CompressionCore, + config: Config, + log: (level: string, msg: string) => void, + session: Session, +): Prepared { + const sessionId = session.id; + try { + const { msgs, cacheControls } = anthropicToCore(parsed); + // Read-only preview: same policy as the google twin above. + const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); + const stripped = stripKernelSummaries(turn.messages as BiliMessage[], turn.state); + const rebuiltMessages = coreToAnthropic(stripped, cacheControls); + log("info", `[${sessionId}] count_tokens pruned: ${msgs.length} → ${stripped.length} msgs`); + const rebuilt: AnthropicRequestBody = { ...parsed, messages: rebuiltMessages }; + delete (rebuilt as Record).prompt_cache_key; + return { + body: JSON.stringify(rebuilt), + session, + processedMessages: [], + originalMessages: msgs, + protocol: "anthropic", + stream: false, + compressInjected: false, + }; + } catch (err) { + log("warn", `[${sessionId}] count_tokens prune failed, forwarding unchanged: ${String(err)}`); + const fallback: AnthropicRequestBody = { ...parsed }; + delete (fallback as Record).prompt_cache_key; + return { + body: JSON.stringify(fallback), + session, + processedMessages: [], + originalMessages: [], + protocol: "anthropic", + stream: false, + compressInjected: false, + }; + } +} + +// Claude Code's auto-mode safety classifier one-shots expect a strict XML +// verdict — the default `xml_2stage` mode stops the response at `` +// or ``. These are not compressible conversations: the compress +// system-prompt + ACP tools (or the kernel round-trip) derailed the small model +// from that verdict, so the classifier reported "could not evaluate" (#353). +// The magic stop sequences are the only in-body signal; forward them untouched. +const AUTO_MODE_CLASSIFIER_STOPS = new Set(["", ""]); + +function isAutoModeClassifier(parsed: AnthropicRequestBody): boolean { + const stops = parsed.stop_sequences; + if (!Array.isArray(stops)) return false; + return stops.some((s) => typeof s === "string" && AUTO_MODE_CLASSIFIER_STOPS.has(s)); +} diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts new file mode 100644 index 0000000000..7be06038d8 --- /dev/null +++ b/src/server/prepare-google.ts @@ -0,0 +1,230 @@ +// Google (Gemini generateContent) protocol prepare pipeline (extracted +// verbatim from src/server.ts — #1440 P2 four-cut disassembly, cut 2). + +import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; +import { ABSORB_TOOL_NAME, IMAGE_FULL_TOOL_GOOGLE, RULE_TOOL_GOOGLE, absorbToolsFor, buildAbsorbSystemPrompt, buildCompressSystemPrompt, retrieveToolsFor, withMarkerIntegrityNote, withStagedCompressGuidance } from "../compress-tool.js"; +import { ProxyOptions } from "../config.js"; +import { applyImageCompressionPass, imageCompressionEnabled, imageFullTrailingNote } from "../image-compress.js"; +import { imageTokensInParsedBody } from "../image-tokens.js"; +import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; +import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge } from "../server/budget.js"; +import { REWRITE_MIN_INCOMING_TOTAL, Session, applyCompactionArchive, foldCoverage, markDirty, snapshotMessages } from "../session.js"; +import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, retrieveToolName } from "../store.js"; +import { reconcileSystemAnchor } from "../system-anchor.js"; +import { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts, renderNudgeText, viableRanges } from "acp-kernel"; +import { BiliMessage, GoogleContent, GoogleRequestBody, GoogleSystemInstruction, GoogleTool, coreToGoogle, googleToCore } from "acp-kernel/wire"; +import { injectGoogleTool, injectTool } from "./inject.js"; +import { Prepared, deriveTitle, diagNudge, diagTagSummary, effectiveAbsorbBlock, effectiveTokenCount, imageBillingFor, reapOrphansLogged, stripKernelSummaries } from "./prepare-shared.js"; + +/** Append the ephemeral nudge to a Gemini `contents` array. Gemini is + * strict about role alternation, so a trailing user turn is merged into + * rather than appended to (the nudge then reads as the model's last input, + * which is where it belongs); a model-final history gets a fresh user turn + * because a request must not end on the model side. */ +function appendGoogleNudge(contents: GoogleContent[], text: string): GoogleContent[] { + const last = contents[contents.length - 1]; + if (last && (last.role ?? "user") !== "model") { + const parts = Array.isArray(last.parts) ? last.parts : []; + return [...contents.slice(0, -1), { ...last, parts: [...parts, { text }] }]; + } + return [...contents, { role: "user", parts: [{ text }] }]; +} + +/** Gemini native wire (`POST /v1beta/models/:generateContent| + * :streamGenerateContent`): the OpenAI branch's twin — hoist the system + * dimension out of the fold space, fold, re-inject — with three wire-specific + * differences: + * - the model, and whether the reply streams, live in the URL PATH, so the + * caller passes both in (the body carries neither); + * - the system rides in `systemInstruction`, never inside `contents`; + * - Gemini rejects non-alternating roles, which coreToGoogle enforces by + * merging same-side core runs, so the nudge merges into the trailing user + * turn instead of starting a new one. + * #651's reasoning drop deliberately does NOT apply here: Gemini 3 validates + * the `thoughtSignature` of replayed parts, and dropping a thought part takes + * its signature with it (400 INVALID_ARGUMENT). */ +export async function prepareGoogle( + parsed: GoogleRequestBody, + opts: ProxyOptions, + core: CompressionCore, + config: Config, + prompts: Prompts, + surface: PackSurface, + log: (level: string, msg: string) => void, + session: Session, + pluginMode: boolean, + nativeWindow: number, + model: string | undefined, + stream: boolean, + visibilityMarkers: boolean, + upstreamOrigin: string, +): Promise { + const sessionId = session.id; + ++session.stats.requests; + let googleClientSystem = ""; + let sysNotes: string[] = []; + let googleOutboundSystem: string | undefined; + let systemInstruction: GoogleSystemInstruction | undefined = parsed.systemInstruction; + let processedMessages: CoreMessage[] = []; + let originalMessages: CoreMessage[] = []; + let nudge: NudgeDecision | undefined; + let rebuiltContents: GoogleContent[] = Array.isArray(parsed.contents) ? parsed.contents : []; + let toolsOut: GoogleTool[] | undefined = parsed.tools; + + const genConfig = parsed.generationConfig; + const declaredMax = genConfig ? genConfig.maxOutputTokens : undefined; + const maxTokens = typeof declaredMax === "number" ? declaredMax : 8192; + // Title-generation requests (tiny budget) get no compress tooling — same + // heuristic and same prefix-cache rationale as prepareOpenai. + const isTitleGen = maxTokens <= 200; + const shouldInject = opts.compress.injectTool && !isTitleGen; + const injectTools = shouldInject && !pluginMode; + + try { + const { msgs, systemText } = googleToCore(parsed); + googleClientSystem = systemText; + // Title-gen side-requests carry their own tiny system — reconciling + // them would pollute the conversation's anchor state. + if (opts.stableSystemAnchor && !pluginMode && !isTitleGen) { + const outcome = reconcileSystemAnchor(session, "google", systemText, sessionId, log); + sysNotes = outcome.notes; + googleClientSystem = outcome.outbound; + } + originalMessages = msgs; + const tokenCount = effectiveTokenCount(session, msgs, imageTokensInParsedBody("google", parsed, imageBillingFor(opts, upstreamOrigin))); + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); + const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); + const absorbActive = absorbBlock?.enabled === true && shouldInject; + // acp_rule has no processTurn side effect (no markers/instructions are + // ever injected into messages), so unlike absorb it needs no loop- + // config stripping — only tool availability matters. + const rulesActive = rulesEnabled(config) && shouldInject; + const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside + // processTurn may deactivate fully-drifted blocks, erasing them. + const foldCoveredBefore = session.stats.pendingFoldUsage === true + ? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : []))) + : null; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: "text-only", contentStore: contentStoreOf(session) }); + session.state = turn.state; + adoptContentStore(session, turn.contentStore); + // The fold from last turn's compress has materialized in state — future + // usage reports are post-fold reality, drop the credit. + session.stats.compressCreditTokens = 0; + if (foldCoveredBefore !== null && !isTitleGen && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) { + const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); + if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); + } + storeEffectiveAbsorb(session, loopConfig); + storeEffectiveRules(session, config); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // Drop sub-viability fragments before any consumer sees them (the + // kernel validates a compress batch atomically). + if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); + nudge = turn.nudge; + session.stats.contextTokens = tokenCount; + if (!session.meta.title) { + const t = deriveTitle(msgs); + if (t) session.meta.title = t; + } + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); + log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, model, willInjectNudge)); + processedMessages = stripKernelSummaries(turn.messages, turn.state); + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); + reapOrphansLogged(session, msgs, log, sessionId); + // [#1095] arrival-time image downscale (see prepareAnthropic). + await applyImageCompressionPass(session, processedMessages as BiliMessage[], { config, billing: imageBillingFor(opts, upstreamOrigin), log }); + rebuiltContents = coreToGoogle(processedMessages as BiliMessage[]); + + // ONLY the static compress prompt joins the client's system text — the + // system instruction is the prefix-cache anchor and must stay + // byte-stable across turns. The per-turn nudge is appended to the + // trailing user content below (see prepareOpenai for the rationale). + const sysParts: string[] = []; + if (googleClientSystem) sysParts.push(googleClientSystem); + if (shouldInject) sysParts.push(withMarkerIntegrityNote(buildCompressSystemPrompt(prompts, surface?.promptSections), visibilityMarkers)); + if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); + googleOutboundSystem = sysParts.join("\n\n"); + // Untouched when nothing was added beyond the client's own text: the + // original `systemInstruction` object then rides through byte-identical + // instead of being re-serialized into a new shape. + const extraSystemParts = sysParts.slice(googleClientSystem ? 1 : 0); + systemInstruction = extraSystemParts.length > 0 ? { parts: sysParts.map((text) => ({ text })) } : parsed.systemInstruction; + if (injectTools) { + toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [absorbTools.google] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).google] : []), ...(imageCompressionEnabled(session) ? [IMAGE_FULL_TOOL_GOOGLE] : [])], surface?.toolPrompts); + } + if (sysNotes.length > 0) { + rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n")); + } + if (willInjectNudge && turn.nudge) { + try { + const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); + if (rendered.text) { + rebuiltContents = appendGoogleNudge(rebuiltContents, withMarkerIntegrityNote(withStagedCompressGuidance(rendered.text), visibilityMarkers)); + } + } catch { + } + } + // [#1095] restore-channel guidance — ephemeral trailing note (see prepareAnthropic). + const imgNote = imageFullTrailingNote(session); + if (imgNote) rebuiltContents = appendGoogleNudge(rebuiltContents, imgNote); + } catch (err) { + log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); + processedMessages = []; + } + + const rebuilt: GoogleRequestBody = { ...parsed, contents: rebuiltContents, tools: toolsOut, systemInstruction }; + clampOutgoingOutput(rebuilt as Record, "generationConfig.maxOutputTokens", { systemText: googleClientSystem, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("google", rebuilt) }, sessionId, log); + // #532: title-gen side requests carry their own tiny system — skip them. + if (!isTitleGen && googleOutboundSystem !== undefined) { + session.metadata.systemPromptTokens = countSystemAndToolsTokens(googleOutboundSystem, toolsOut); + } + snapshotMessages(session, originalMessages); + markDirty(session); + return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "google", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: "text-only" } as Prepared; +} + +/** `POST /v1beta/models/:countTokens` — the fold-prune twin of + * prepareCountTokens: the client measures the payload the proxy would + * actually forward. Gemini's endpoint reads `contents`/`systemInstruction` + * and answers `{totalTokens}`, so only the contents array is rewritten. */ +export function prepareGoogleCountTokens( + parsed: GoogleRequestBody, + core: CompressionCore, + config: Config, + log: (level: string, msg: string) => void, + session: Session, +): Prepared { + const sessionId = session.id; + try { + const { msgs } = googleToCore(parsed); + // Read-only preview: the store rides in so placeholder substitution is + // counted, but nothing is adopted (state is discarded here too). + const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: session.stats.lastInputTokens, renderTags: "text-only", contentStore: contentStoreOf(session) }); + const stripped = stripKernelSummaries(turn.messages, turn.state); + const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) }; + log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`); + return { + body: JSON.stringify(rebuilt), + session, + processedMessages: [], + originalMessages: msgs, + protocol: "google", + stream: false, + compressInjected: false, + }; + } catch (err) { + log("warn", `[${sessionId}] countTokens prune failed, forwarding unchanged: ${String(err)}`); + return { + body: JSON.stringify({ ...parsed }), + session, + processedMessages: [], + originalMessages: [], + protocol: "google", + stream: false, + compressInjected: false, + }; + } +} diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts new file mode 100644 index 0000000000..203ec44915 --- /dev/null +++ b/src/server/prepare-openai.ts @@ -0,0 +1,309 @@ +// OpenAI chat protocol prepare pipeline (extracted verbatim from +// src/server.ts — #1440 P2 four-cut disassembly, cut 2). + +import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; +import { stripAcpPanelMessages, stripAcpStatusMarkers } from "../acp-panel.js"; +import { ABSORB_TOOL_NAME, IMAGE_FULL_TOOL_OPENAI, RULE_TOOL_OPENAI, absorbToolsFor, buildAbsorbSystemPrompt, buildCompressSystemPrompt, retrieveToolsFor, withConversationIdNote, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "../compress-tool.js"; +import { ProxyOptions } from "../config.js"; +import { recordConflict } from "../conflict-watch.js"; +import { applyImageCompressionPass, imageCompressionEnabled, imageFullTrailingNote } from "../image-compress.js"; +import { imageTokensInParsedBody } from "../image-tokens.js"; +import { estimateCoreMessagesUpper } from "../preflight.js"; +import { CompressReasoningConfig } from "../reasoning-drop.js"; +import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; +import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge } from "../server/budget.js"; +import { PendingRetrieval, REWRITE_MIN_INCOMING_TOTAL, Session, applyCompactionArchive, detectUnannouncedHistoryRewrite, ensureCanonicalId, foldCoverage, markCompactionBoundary, markDirty, snapshotMessages } from "../session.js"; +import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, dropRetrievals, flushRetrievalNotes, pruneExpiredRetrievals, reconcileReloadedRetrievals, retrieveToolName, snapshotPendingRetrievals } from "../store.js"; +import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoReasoning } from "../strict-echo.js"; +import { reconcileSystemAnchor } from "../system-anchor.js"; +import { hardenOpenaiAssistantContent, systemToUser } from "../util.js"; +import { droppedOpenaiParts } from "../wire-drop-warn.js"; +import { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts, renderNudgeText, viableRanges } from "acp-kernel"; +import { BiliMessage, OpenAIRequestBody, OpenAITool, coreToOpenai, injectOpenaiSystem, openaiToCore } from "acp-kernel/wire"; +import http from "node:http"; +import { injectOpenaiTool, injectTool } from "./inject.js"; +import { Prepared, deriveTitle, diagNudge, diagTagSummary, effectiveAbsorbBlock, effectiveTokenCount, imageBillingFor, reapOrphansLogged, stripKernelSummaries, withReasoningDrop } from "./prepare-shared.js"; + +// #1205: sessions already warned about codec-dropped content parts (e.g. +// DeepSeek Files API file refs) — one warn per session per distinct type-set; +// attachment flows resend the same history every turn. Same bounded-FIFO shape +// as warnedChainSessions above. +const warnedWireDropKeys = new Set(); +export const WARNED_WIREDROP_KEY_CAP = 4096; +export function _resetWireDropWarningsForTest(): void { + warnedWireDropKeys.clear(); +} + +export function warnDroppedOpenaiParts(parsed: unknown, sessionId: string, log: (level: string, msg: string) => void): void { + const report = droppedOpenaiParts(parsed); + if (!report) return; + const key = `${sessionId}:${report.types.join(",")}`; + if (warnedWireDropKeys.has(key)) return; + warnedWireDropKeys.add(key); + if (warnedWireDropKeys.size > WARNED_WIREDROP_KEY_CAP) { + warnedWireDropKeys.delete(warnedWireDropKeys.values().next().value as string); + } + log("warn", `[${sessionId}] wire codec will drop ${report.count} non-user content part(s) with unrecognized type(s) [${report.types.join(", ")}] (first at message #${report.firstIndex}) — kernel 0.0.85+ preserves all user-message parts (DeepSeek Files API refs included, #1205/#1188), but parts riding system/assistant/tool messages still reduce to text-only`); +} + +/** [#684] Exit sentinel: in a thinking session, an assistant tool_calls + * message WITHOUT reasoning_content while sibling turns carry it is the + * signature of a split turn — strict-echo upstreams reject the whole request. + * The kernel turn gate makes this unreachable; warn if a new path + * reintroduces it. [#762] presence, not emptiness: a BLANK echo ("") is what + * DeepSeek accepts — counting it as absent fired on every turn of every + * healthy thinking session (38× in one). Only a missing field is the + * rejection signature. */ +export function warnReasoningPairs( + wireMessages: unknown[], + log: (level: string, msg: string) => void, + sessionId: string, +): void { + let withRc = 0; + let split = 0; + for (const m of wireMessages) { + const msg = m as { role?: string; tool_calls?: unknown; reasoning_content?: unknown }; + if (msg?.role !== "assistant") continue; + const hasRc = typeof msg.reasoning_content === "string"; + if (hasRc) withRc++; + else if (Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0) split++; + } + if (withRc > 0 && split > 0) { + log("warn", `[${sessionId}] reasoning-pair-violated: ${split} assistant tool-call message(s) lack reasoning_content while ${withRc} carry it — strict-echo upstreams (DeepSeek thinking mode) will reject the request (#684)`); + } +} + +export async function prepareOpenai( + parsed: OpenAIRequestBody, + req: http.IncomingMessage, + opts: ProxyOptions, + core: CompressionCore, + config: Config, + prompts: Prompts, + surface: PackSurface, + log: (level: string, msg: string) => void, + session: Session, + pluginMode: boolean, + upstreamOrigin: string, + nativeWindow: number, + reasoning: CompressReasoningConfig | undefined, + visibilityMarkers: boolean, + billingUpstream?: string, +): Promise { + const sessionId = session.id; + const stream = parsed.stream === true; + ++session.stats.requests; + let openaiSystemText = ""; + let sysNotes: string[] = []; + const stripReasoning = (msgs: BiliMessage[]): BiliMessage[] => withReasoningDrop(msgs, reasoning, log, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))); + let openaiOutboundSystem: string | undefined; + let processedMessages: CoreMessage[] = []; + let attachedRetrievals: PendingRetrieval[] = []; + let originalMessages: CoreMessage[] = []; + let nudge: NudgeDecision | undefined; + let rebuiltMessages = parsed.messages; + let toolsOut = parsed.tools; + + const maxTokens = typeof parsed.max_tokens === "number" ? parsed.max_tokens : 8192; + // Title-generation requests (tiny max_tokens) get no compress tooling so + // the model produces a clean short title. We do NOT key this off message + // count: a 2-message request is just turn 1 of a real conversation, and + // flipping shouldInject false→true between turn 1 and turn 2+ rewrites the + // system prompt bytes (compress prompt added/removed) — which breaks the + // provider prefix cache for every subsequent turn. + const isTitleGen = maxTokens <= 200; + const shouldInject = opts.compress.injectTool && !isTitleGen; + const injectTools = shouldInject && !pluginMode; + + const strippedPanels = stripAcpPanelMessages(parsed.messages); + if (strippedPanels > 0) { + log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); + } + const strippedMarkerLines = stripAcpStatusMarkers(parsed.messages); + if (strippedMarkerLines > 0) { + log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); + } + + try { + // Kernel 0.0.37 hoists the contiguous leading system/developer prefix + // OUT of the fold space: system content is host runtime state and + // must not feed ids/fingerprints. Capture it and re-inject below — + // otherwise the proxy would forward payloads without any system. + const { msgs, systemText } = openaiToCore(parsed); + openaiSystemText = systemText; + warnDroppedOpenaiParts(parsed, sessionId, log); + // Title-gen side-requests carry their own tiny system — reconciling + // them would pollute the conversation's anchor state. + if (opts.stableSystemAnchor && !pluginMode && !isTitleGen) { + const outcome = reconcileSystemAnchor(session, "openai", systemText, sessionId, log); + sysNotes = outcome.notes; + openaiSystemText = outcome.outbound; + } + originalMessages = msgs; + // #1001: pre-turn snapshot — processTurn below assigns fresh refs to every + // previously-unknown id, which would make rewrite detection read 1.0. + const knownRefsBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); + // tokenCount = upstream's real input_tokens from the previous turn + // tokenCount = upstream's real input_tokens from the previous turn + // (see anthropic branch comment + its #553-follow-up exception). + const tokenCount = effectiveTokenCount(session, msgs, imageTokensInParsedBody("openai", parsed, imageBillingFor(opts, billingUpstream ?? upstreamOrigin))); + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + // Absorb markers ride in the kernel's processTurn output (gated by + // config.absorb). Title-gen requests skip ALL injection for + // prefix-cache stability, so strip absorb from the loop config there. + const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); + const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); + const absorbActive = absorbBlock?.enabled === true && shouldInject; + const rulesActive = rulesEnabled(config) && shouldInject; + const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside + // processTurn may deactivate fully-drifted blocks, erasing them. + const foldCoveredBefore = session.stats.pendingFoldUsage === true + ? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : []))) + : null; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); + session.state = turn.state; + adoptContentStore(session, turn.contentStore); + // The fold from last turn's compress has now materialized in state — + // future usage reports are post-fold reality, drop the credit. + session.stats.compressCreditTokens = 0; + if (foldCoveredBefore !== null && !isTitleGen && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) { + const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); + if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); + } + storeEffectiveAbsorb(session, loopConfig); + storeEffectiveRules(session, config); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // Drop sub-viability fragments before any consumer sees them: a tiny + // range in the list makes batched compress attempts fail atomically + // (kernel validates the whole batch). Mirrors billion-context-pi. + if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); + nudge = turn.nudge; + session.stats.contextTokens = tokenCount; + if (!session.meta.title) { + const t = deriveTitle(msgs); + if (t) session.meta.title = t; + } + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); + log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); + processedMessages = stripReasoning(stripKernelSummaries(turn.messages, turn.state)); + // #1001: a silent client history rewrite takes the same archive+prune path + // as an announced /compact boundary — syncBlocks above has already + // deactivated the blocks whose sources left the context. + { + const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id)); + if (rewrite.detected) { + log("warn", `[${sessionId}] unannounced client history rewrite detected (${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known) — marking compaction boundary (#1001)`); + recordConflict(session, "unannounced-rewrite", `${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known`); + markCompactionBoundary(session); + } + } + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); + reapOrphansLogged(session, msgs, log, sessionId); + // [#1095] arrival-time image downscale (see prepareAnthropic) — one + // deterministic encode per fingerprint; byte-stable re-runs. + await applyImageCompressionPass(session, processedMessages as BiliMessage[], { config, billing: imageBillingFor(opts, billingUpstream ?? upstreamOrigin), log }); + // [#1271/#1343] plugin mode: acp_retrieve already acked via the tool API; snapshot + // the queued full text onto THIS forward (stays in the queue until commit/drop, so an + // upstream failure drops-and-logs it instead of vanishing it). + if (pluginMode && ccrEnabled(session)) { + reconcileReloadedRetrievals(session); + pruneExpiredRetrievals(session); + attachedRetrievals = snapshotPendingRetrievals(session); + if (attachedRetrievals.length > 0) processedMessages = [...processedMessages, ...attachedRetrievals.map((i) => i.injection)]; + } + rebuiltMessages = systemToUser(hardenOpenaiAssistantContent(coreToOpenai(processedMessages as BiliMessage[]))); + + // ONLY the static compress prompt goes into the system message — the + // system prompt is the prefix-cache anchor and must be byte-stable + // across turns. The nudge (which changes every turn: token count, + // growth %, dynamic example) is appended as a trailing user message + // instead, mirroring pai-acp's design. Putting the nudge in system + // would invalidate the cache every turn. + const sysParts: string[] = []; + if (openaiSystemText) sysParts.push(openaiSystemText); + if (shouldInject) sysParts.push(withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), ensureCanonicalId(session))); + if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); + rebuiltMessages = injectOpenaiSystem(rebuiltMessages, sysParts); + if (sysNotes.length > 0) { + rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; + } + // #532: capture what bili injects outside the fold space (client system + // + compress prompt). A head system message already in the rebuilt view + // is classified by the kernel breakdown — counting only these parts + // avoids double-counting it. + openaiOutboundSystem = sysParts.join("\n\n"); + if (injectTools) { + toolsOut = injectOpenaiTool(parsed.tools, [...(absorbActive ? [absorbTools.openai] : []), ...(rulesActive ? [RULE_TOOL_OPENAI] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).openai] : []), ...(imageCompressionEnabled(session) ? [IMAGE_FULL_TOOL_OPENAI] : [])], surface?.toolPrompts); + } + // Nudge as a separate trailing user message (cache-friendly). Injected + // in BOTH modes (#451): plugin agents supply the ACP tools but have no + // nudge channel of their own, so this proxy-side nudge is the proactive + // trigger (preflight alone fires only at the hard limit). Ephemeral user + // message — not persisted, never enters the agent's re-sent history, + // prefix-cache-anchor safe. + if (willInjectNudge && turn.nudge) { + try { + const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); + if (rendered.text) { + rebuiltMessages = [...rebuiltMessages, { role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }]; + } + } catch { + } + } + // [#1095] restore-channel guidance — ephemeral trailing user message + // (same pattern as prepareAnthropic/Google/Responses). + const imgNote = imageFullTrailingNote(session); + if (imgNote) rebuiltMessages = [...rebuiltMessages, { role: "user", content: imgNote }]; + // [#1343] surface any earlier undelivered retrieve as an ephemeral trailing + // user note (kept last so it never reorders cached messages). + const retrNote = flushRetrievalNotes(session); + if (retrNote) rebuiltMessages = [...rebuiltMessages, { role: "user", content: retrNote }]; + } catch (err) { + log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); + if (attachedRetrievals.length > 0) dropRetrievals(session, attachedRetrievals.map((i) => i.ref), "prepare failed; forwarded unprocessed"); + processedMessages = []; + } + + // #762: repair the strict-echo rejection class BEFORE the sentinel sees + // the array — a normalized body must not fire its own canary. + rebuiltMessages = normalizeStrictEchoReasoning(rebuiltMessages, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)), log, sessionId); + const rebuilt: OpenAIRequestBody = { ...parsed, messages: rebuiltMessages, tools: toolsOut as OpenAITool[] | undefined }; + warnReasoningPairs(rebuiltMessages, log, sessionId); + clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("openai", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log); + // prompt_cache_retention is an OpenAI-host-only cache directive; the dsh + // launcher forces PI_CACHE_RETENTION=long (for the session-id + // prompt_cache_key) which makes the client also emit it. Third-party + // OpenAI-compatible upstreams may reject unknown fields, and cache policy + // is the upstream's business — strip it. prompt_cache_key itself passes + // through: upstreams that ignore it lose nothing, upstreams that use it + // get a per-conversation routing hint. + delete (rebuilt as Record).prompt_cache_retention; + // OpenAI Chat Completions only emits a usage object in the final stream + // chunk when the client sets stream_options.include_usage=true. Without + // it, streaming sessions never learn their real input_tokens → + // lastInputTokens stays 0 → compression never fires. Force it on for any + // streaming request that doesn't already opt in. (Anthropic/Responses + // emit usage unconditionally, so this is OpenAI-specific.) + if (stream && (rebuilt as Record).stream_options === undefined) { + (rebuilt as Record).stream_options = { include_usage: true }; + } + // #532: title-gen side requests carry their own tiny system and would + // clobber the conversation's measured overhead — skip them. + if (!isTitleGen && openaiOutboundSystem !== undefined) { + session.metadata.systemPromptTokens = countSystemAndToolsTokens(openaiOutboundSystem, toolsOut); + } + // #728: record this turn's outbound payload upper bound as the fallback + // token source for upstreams that never report usage (see effectiveTokenCount). + // Title-gen side requests are skipped like the overhead row above — their + // tiny payload would clobber the conversation's measurement. + if (!isTitleGen) { + session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + + countSystemAndToolsTokens(openaiOutboundSystem || openaiSystemText, toolsOut) + + imageTokensInParsedBody("openai", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)); + } + snapshotMessages(session, originalMessages); + markDirty(session); + return { body: JSON.stringify(rebuilt), session, attachedRetrievals, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" } as Prepared; +} diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts new file mode 100644 index 0000000000..e5ef5867b6 --- /dev/null +++ b/src/server/prepare-responses.ts @@ -0,0 +1,565 @@ +// Responses-API protocol prepare pipeline (extracted verbatim from +// src/server.ts — #1440 P2 four-cut disassembly, cut 2). + +import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; +import { stripAcpPanelResponsesInput, stripAcpStatusMarkers } from "../acp-panel.js"; +import { buildTriggerForgeBody, codexCompactGate, codexCompactGatePre, codexCompactMode, hasCompactionTrigger, isCodexClient, mergeForgedSummaries, replaceBiliCompactionItems, stripBiliCompactionItems } from "../codex-compact.js"; +import { ABSORB_TOOL_NAME, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_TOOLS_RESPONSES, IMAGE_FULL_TOOL_RESPONSES, RULE_TOOL_RESPONSES, absorbToolsFor, buildAbsorbSystemPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, retrieveToolsFor, withConversationIdNote, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "../compress-tool.js"; +import { ProxyOptions, resolveCompressProtocol } from "../config.js"; +import { applyImageCompressionPass, imageCompressionEnabled, imageFullTrailingNote } from "../image-compress.js"; +import { imageTokensInParsedBody } from "../image-tokens.js"; +import { dropWhitespaceResponsesMessages, normalizeResponsesMessageItems, sanitizeResponsesInputIds } from "../loop/adapter-responses.js"; +import { estimateCoreMessagesUpper } from "../preflight.js"; +import { CompressReasoningConfig } from "../reasoning-drop.js"; +import { patchResponsesInputWithToolImages as patchResponsesInput, responsesToCoreWithToolImages as responsesToCore } from "../responses-tool-output.js"; +import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; +import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge } from "../server/budget.js"; +import { ConversationIdentity } from "../session-id.js"; +import { REWRITE_MIN_INCOMING_TOTAL, Session, ensureCanonicalId, foldCoverage, markDirty, reconcileNativeCompactionBoundary, snapshotMessages } from "../session.js"; +import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, retrieveToolName } from "../store.js"; +import { isStrictReasoningEcho, modelIdOf } from "../strict-echo.js"; +import { reconcileSystemAnchor } from "../system-anchor.js"; +import { hoistTrappedToolItems } from "../tool-pair-order.js"; +import { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts, defaultCountTokens, renderNudgeText, viableRanges } from "acp-kernel"; +import { BiliMessage, ResponseInputItem, ResponsesProjection, ResponsesRequestBody, injectResponsesDeveloperMessage } from "acp-kernel/wire"; +import http from "node:http"; +import { injectResponsesTool, injectTool } from "./inject.js"; +import { Prepared, deriveTitle, diagNudge, diagTagSummary, effectiveAbsorbBlock, effectiveTokenCount, imageBillingFor, reapOrphansLogged, stripKernelSummaries, withReasoningDrop } from "./prepare-shared.js"; + +/** [#684] Responses-wire twin: function_call item with no reasoning item + * immediately preceding it while other turns carry reasoning. */ +export function warnResponsesReasoningPairs( + input: unknown[], + log: (level: string, msg: string) => void, + sessionId: string, +): void { + let withReasoning = 0; + let split = 0; + let prevWasReasoning = false; + for (const item of input) { + const it = item as { type?: string }; + if (it?.type === "reasoning") { + withReasoning++; + prevWasReasoning = true; + continue; + } + if (it?.type === "function_call" && !prevWasReasoning) split++; + prevWasReasoning = false; + } + if (withReasoning > 0 && split > 0) { + log("warn", `[${sessionId}] reasoning-pair-violated: ${split} function_call item(s) lack a preceding reasoning item while ${withReasoning} exist — strict-echo upstreams will reject the request (#684)`); + } +} + +// #564: folding + stripKernelSummaries can merge two assistant turns into one +// run, which Responses rejects (run order reasoning* -> message* -> +// function_call*, <=1 reasoning). Rebuild boundaries from the ORIGINAL history +// AFTER dedup: drop a reasoning whose turn body was wholly folded away (keep +// originally-reasoning-only turns), else separate the runs with a user marker. +const RESPONSES_TURN_SEPARATOR = "[The exchange between these two assistant turns was compressed.]"; + +export function repairResponsesAssistantOrdering(folded: CoreMessage[], original: CoreMessage[]): CoreMessage[] { + const runOf = new Map(); + const runHasBody = new Map(); + let run = 0; + let inRun = false; + for (const m of original) { + if (m.role === "assistant") { + if (!inRun) { run++; inRun = true; } + runOf.set(m.id, run); + if (m.contentType !== "reasoning") runHasBody.set(run, true); + } else { + inRun = false; + } + } + const survivorCount = new Map(); + for (const m of folded) { + const r = m.role === "assistant" ? runOf.get(m.id) : undefined; + if (r !== undefined) survivorCount.set(r, (survivorCount.get(r) ?? 0) + 1); + } + + const out: CoreMessage[] = []; + let phase = -1; + let seenReasoning = false; + let sepSeq = 0; + const pushSeparator = (): void => { + sepSeq++; + out.push({ id: `acp_turn_sep_${sepSeq}`, role: "user", contentType: "text", text: RESPONSES_TURN_SEPARATOR }); + phase = -1; + seenReasoning = false; + }; + for (const m of folded) { + if (m.role !== "assistant") { + out.push(m); + phase = -1; + seenReasoning = false; + continue; + } + const kind = m.contentType === "reasoning" ? "reasoning" : m.contentType === "tool-call" ? "tool-call" : "message"; + const r = runOf.get(m.id); + if (kind === "reasoning" && r !== undefined && runHasBody.get(r) && survivorCount.get(r) === 1) continue; + if ((kind === "reasoning" && (phase > 0 || seenReasoning)) || (kind === "message" && phase === 2)) pushSeparator(); + out.push(m); + if (kind === "reasoning") { phase = Math.max(phase, 0); seenReasoning = true; } + else if (kind === "message") phase = Math.max(phase, 1); + else phase = Math.max(phase, 2); + } + return out; +} + +export async function prepareResponses( + parsed: ResponsesRequestBody, + req: http.IncomingMessage, + opts: ProxyOptions, + core: CompressionCore, + config: Config, + prompts: Prompts, + surface: PackSurface, + log: (level: string, msg: string) => void, + session: Session, + identity: ConversationIdentity, + pluginMode: boolean, + upstreamOrigin: string, + nativeWindow: number, + reasoning: CompressReasoningConfig | undefined, + visibilityMarkers: boolean, + billingUpstream?: string, +): Promise { + const sessionId = session.id; + const stream = parsed.stream === true; + ++session.stats.requests; + const stripReasoning = (msgs: BiliMessage[]): BiliMessage[] => withReasoningDrop(msgs, reasoning, log, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))); + if (reconcileNativeCompactionBoundary(session)) { + log("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`); + } + + // A codex client echoes our forged compaction item back in the next + // request; replace it with a plain summary-carrying user message so the + // handoff rides the replayable history (kernel-compressible, retained by + // codex's own user-message rule) instead of a foreign opaque blob. Real + // OpenAI blobs carry no bili marker and pass through untouched. + let echoReplaced = false; + if (Array.isArray(parsed.input)) { + const { items, replaced, dropped } = replaceBiliCompactionItems(parsed.input); + if (replaced + dropped > 0) { + // Only a real replacement carries a summary into the history; a + // drop-only echo removed a marker blob without inserting one, so the + // forge-time captured summaries must still be re-injected (#1064). + if (replaced > 0) echoReplaced = true; + log("info", `[${sessionId}] replaced ${replaced} echoed bili compaction item(s) with summary handoff message(s)${dropped > 0 ? `, dropped ${dropped} legacy marker item(s)` : ""}`); + parsed.input = items as typeof parsed.input; + } + } + + let processedMessages: CoreMessage[] = []; + let originalMessages: CoreMessage[] = []; + let nudge: NudgeDecision | undefined; + let responsesProjection: ResponsesProjection | undefined; + let rebuiltInput: ResponseInputItem[] | string = parsed.input; + let toolsOut = parsed.tools; + let transformOk = false; + let responsesDevContent: string | undefined; + let sysNotes: string[] = []; + + // #242: over-long input item ids (poisoned rollouts) 400 upstream on every + // request; rewrite them to short deterministic ids before anything reads + // or replays the input. + // omp-style type-less user items must be typed before the projection + // drops them (see normalizeResponsesMessageItems) — before id sanitize and + // whitespace drop so those see the canonical form. + const typedItems = normalizeResponsesMessageItems(parsed.input); + if (typedItems > 0) { + log("info", `[${sessionId}] stamped type:"message" on ${typedItems} type-less input item(s) before projection (omp wire form)`); + } + sanitizeResponsesInputIds(parsed.input); + + const droppedEmpty = dropWhitespaceResponsesMessages(parsed.input); + if (droppedEmpty > 0) { + log("info", `[${sessionId}] dropped ${droppedEmpty} whitespace-only message item(s) before projection (flattened-turn artifact)`); + } + + const strippedPanels = stripAcpPanelResponsesInput(parsed.input); + if (strippedPanels > 0) { + log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); + } + const strippedMarkerLines = stripAcpStatusMarkers(parsed.input); + if (strippedMarkerLines > 0) { + log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); + } + + const shouldInject = opts.compress.injectTool; + const injectTools = shouldInject && !pluginMode; + // Codex native remote-compact request: no compress prompt/tools (the model + // produces the compaction itself), no acp tags, plain passthrough so the + // response terminal state can gate the rebase marker. + const isCompactionTrigger = hasCompactionTrigger(parsed.input); + // Route config is keyed by the upstream THIS request goes to (#286: a + // session can outlive its first relay — session.meta.upstreamOrigin is + // first-wins and would silently ignore the new relay's route settings). + const responsesTextProtocol = FORCE_TEXT_PROTOCOL || + resolveCompressProtocol(opts.routes, upstreamOrigin) === "marker"; + const renderTags: "text-only" | "none" = process.env.ACP_RENDER_NONE || isCompactionTrigger ? "none" : "text-only"; + + try { + const projection = responsesToCore(parsed); + responsesProjection = projection; + // Compaction-trigger requests are the compression mechanism itself — + // their payload shape must not gain anchor state or note items. + if (opts.stableSystemAnchor && !pluginMode && !isCompactionTrigger) { + const fresh = projection.systemParts.join("\n\n---\n\n"); + const outcome = reconcileSystemAnchor(session, "responses", fresh, sessionId, log); + sysNotes = outcome.notes; + if (outcome.outbound !== fresh) projection.systemParts = outcome.outbound ? [outcome.outbound] : []; + } + const { msgs } = projection; + originalMessages = msgs; + if (process.env.ACP_DEBUG) { + log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); + } + const tokenCount = effectiveTokenCount(session, msgs, imageTokensInParsedBody("responses", parsed, imageBillingFor(opts, billingUpstream ?? upstreamOrigin))); + // Absorb markers ride in the kernel's processTurn output (gated by + // config.absorb). The marker/text protocol has no native tool channel, + // so strip absorb from the loop config there (both modes). + const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); + const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); + const absorbActive = absorbBlock?.enabled === true && shouldInject && !isCompactionTrigger && !responsesTextProtocol; + const rulesActive = rulesEnabled(config) && shouldInject && !isCompactionTrigger && !responsesTextProtocol; + const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside + // processTurn may deactivate fully-drifted blocks, erasing them. + const foldCoveredBefore = session.stats.pendingFoldUsage === true + ? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : []))) + : null; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags, contentStore: contentStoreOf(session) }); + session.state = turn.state; + adoptContentStore(session, turn.contentStore); + // The fold from last turn's compress has now materialized in state — + // future usage reports are post-fold reality, drop the credit. + session.stats.compressCreditTokens = 0; + if (foldCoveredBefore !== null && !isCompactionTrigger && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) { + const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); + if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); + } + storeEffectiveAbsorb(session, loopConfig); + storeEffectiveRules(session, config); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // Drop sub-viability fragments before any consumer sees them: a tiny + // range in the list makes batched compress attempts fail atomically + // (kernel validates the whole batch). Mirrors billion-context-pi. + if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); + nudge = turn.nudge; + session.stats.contextTokens = tokenCount; + if (!session.meta.title) { + const t = deriveTitle(msgs); + if (t) session.meta.title = t; + } + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !isCompactionTrigger && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); + log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); + processedMessages = repairResponsesAssistantOrdering(stripReasoning(stripKernelSummaries(turn.messages, turn.state)), originalMessages); + reapOrphansLogged(session, msgs, log, sessionId); + // [#1095] arrival-time image downscale (see prepareAnthropic). + await applyImageCompressionPass(session, processedMessages as BiliMessage[], { config, billing: imageBillingFor(opts, billingUpstream ?? upstreamOrigin), log }); + rebuiltInput = patchResponsesInput(projection, processedMessages); + if (Array.isArray(rebuiltInput)) rebuiltInput = hoistTrappedToolItems(rebuiltInput); + // Fallback path: when the echo did NOT come back this turn (client + // dropped it / restarted), the history-borne handoff is absent and the + // forge-time captured summaries are re-injected into the developer + // message so the pre-compaction content is never lost. When the echo + // DID come back, the replacement message carries the summaries and + // the injection is suppressed to avoid duplicating them. + const forgedSummaries = echoReplaced + ? [] + : (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; + if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) { + const prompt = withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections)), visibilityMarkers), ensureCanonicalId(session)); + const devParts = [...projection.systemParts, ...forgedSummaries, prompt]; + if (absorbActive) devParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); + const devContent = devParts.join("\n\n---\n\n"); + responsesDevContent = devContent; + rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); + if (!process.env.ACP_NO_INJECT_TOOL && injectTools) { + const respExtra = [...(absorbActive ? [absorbTools.responses] : []), ...(rulesActive ? [RULE_TOOL_RESPONSES] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).responses] : []), ...(imageCompressionEnabled(session) ? [IMAGE_FULL_TOOL_RESPONSES] : [])]; + toolsOut = responsesTextProtocol + ? injectResponsesTool(parsed.tools, BILI_ACP_READONLY_TOOLS_RESPONSES, surface?.toolPrompts) + : injectResponsesTool(parsed.tools, respExtra.length > 0 ? [...BILI_ACP_TOOLS_RESPONSES, ...respExtra] : BILI_ACP_TOOLS_RESPONSES, surface?.toolPrompts); + } + } else if (projection.systemParts.length > 0 || forgedSummaries.length > 0) { + const devContent = [...projection.systemParts, ...forgedSummaries].join("\n\n---\n\n"); + responsesDevContent = devContent; + rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); + } + if (sysNotes.length > 0) { + const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" + ? [{ type: "message", role: "user", content: rebuiltInput }] + : rebuiltInput; + for (const text of sysNotes) { + inputItems.push({ type: "message", role: "user", content: text }); + } + rebuiltInput = inputItems; + } + // A nudge appended after a trailing `compaction_trigger` would break + // the upstream's "must be the final input item" requirement and is + // redundant — the native compact IS the compression. Otherwise injected + // in BOTH modes (#451): plugin agents supply the ACP tools but have no + // nudge channel of their own, so this proxy-side nudge is the proactive + // trigger (preflight alone fires only at the hard limit). Ephemeral user + // message — not persisted, prefix-cache-anchor safe. + if (willInjectNudge && turn.nudge) { + try { + const rendered = renderNudgeText(turn.nudge, prompts, surface?.nudgeSections); + if (rendered.text) { + const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" + ? [{ type: "message", role: "user", content: rebuiltInput }] + : rebuiltInput; + inputItems.push({ type: "message", role: "user", content: withMarkerIntegrityNote(withSummaryBudgetNote(withStagedCompressGuidance(rendered.text)), visibilityMarkers) }); + rebuiltInput = inputItems; + } + } catch { + } + } + // [#1095] restore-channel guidance — ephemeral trailing note (see + // prepareAnthropic). Never after a compaction_trigger: that item must + // stay the final input item (#283/#209), and the forge path for trigger + // requests builds its own body anyway. + const imgNote = imageFullTrailingNote(session); + if (imgNote && !isCompactionTrigger) { + const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" + ? [{ type: "message", role: "user", content: rebuiltInput }] + : rebuiltInput; + inputItems.push({ type: "message", role: "user", content: imgNote }); + rebuiltInput = inputItems; + } + transformOk = true; + } catch (err) { + log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); + processedMessages = []; + } + + // E2 trigger form: codex's native remote-compaction request (final input item + // is compaction_trigger). When the kill-switch is on, the client is codex, and + // the safety gate passes, forge a success SSE (one compaction item + + // response.completed) and skip upstream — a deterministic handoff to the ACP + // state instead of a foreign compaction blob. + let codexForge: Prepared["codexForge"] | undefined; + if (transformOk + && codexCompactMode() === "intercept" + && isCodexClient(req.headers) + && hasCompactionTrigger(parsed.input) + && codexCompactGate(session, config.modelContextLimit, transformOk)) { + const summaries = session.state.blocks.filter((b) => b.active).map((b) => b.summary); + const prevForged = session.metadata.codexForgedSummaries as string[] | undefined; + const captured = mergeForgedSummaries(prevForged, session.state.blocks); + if (captured.length !== (prevForged?.length ?? 0)) { + session.metadata.codexForgedSummaries = captured; + markDirty(session); + } + // Codex recomputes its ledger from the next real request, but the + // usage we mint here must not read as an empty context: fall back to + // estimating the trigger payload itself when lastInputTokens is + // stale/zero. The reply honors parsed.stream (JSON body when not + // streaming). + const est = defaultCountTokens(typeof parsed.input === "string" ? parsed.input : JSON.stringify(parsed.input ?? "")); + const total = Math.max(session.stats.lastInputTokens, est, 1); + codexForge = { + kind: "trigger", + ...buildTriggerForgeBody(summaries.join("\n\n"), { inputTokens: total, outputTokens: 0, totalTokens: total }, stream), + }; + log("info", `[${sessionId}] codex compact intercepted (trigger); forged SSE with ${summaries.length} block summary(s), upstream not contacted`); + } + + const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut }; + warnResponsesReasoningPairs(Array.isArray(rebuiltInput) ? rebuiltInput : [], log, sessionId); + if (!isCompactionTrigger) { + clampOutgoingOutput(rebuilt as Record, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("responses", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log); + } + // Route with the upstream THIS request goes to — session.meta.upstreamOrigin + // is first-wins and would keep injecting pck toward a relay we switched + // away from (same class of bug as the compressProtocol fix above, #286). + const promptCacheKey = resolvePromptCacheKey( + rebuilt.prompt_cache_key, + identity, + opts.promptCache.routing, + upstreamOrigin, + ); + if (promptCacheKey && !rebuilt.prompt_cache_key) rebuilt.prompt_cache_key = promptCacheKey; + // This adapter is stateless: we replay the FULL conversation in `input`. + // Strip Responses' native chaining field so the upstream does not resolve + // stored server-side state ON TOP of the input we already sent (which would + // duplicate history for clients that use store:true + chaining). Empirically + // codex sends store:false and never sets previous_response_id, so this is a + // no-op for codex — kept defensively for any client that does chain. Set + // ACP_KEEP_RESPONSE_ID=1 to preserve it (diagnostic only). `instructions` + // was already lifted into the developer message at input[1]; forwarding it + // again here double-sends it and violates the responses_lite contract + // (top-level instructions must stay empty for code_mode tool exposure). + if (process.env.ACP_KEEP_RESPONSE_ID !== "1") delete rebuilt.previous_response_id; + delete rebuilt.instructions; + // Same rationale as prepareOpenai: strip the OpenAI-host-only cache + // directive; keep prompt_cache_key. Sent by hermes' codex transport and + // by any PI_CACHE_RETENTION=long client. + delete (rebuilt as Record).prompt_cache_retention; + // Log the final tools we forward upstream so we can confirm ACP tools are + // present. Distinguishes "compress" (top-level function) from Codex + // namespace items (type:namespace/custom). + if (process.env.ACP_DEBUG) { + const fwdTools = (Array.isArray(toolsOut) ? toolsOut : []).map((t) => { + const r = t as Record; + const sub = Array.isArray(r.tools) ? `(${r.tools.length} sub)` : ""; + return `${r.type as string}:${(r.name as string) ?? "?"}${sub}`; + }); + log("info", `[${sessionId}] responses forward tools=[${fwdTools.join(",")}] injectTool=${injectTools}${pluginMode ? " (plugin mode: wire injection suppressed)" : ""} NO_INJECT_TOOL=${!!process.env.ACP_NO_INJECT_TOOL} NO_COMPRESS_PROMPT=${!!process.env.ACP_NO_COMPRESS_PROMPT}`); + } + // #532: measure the outbound developer(system)+tools overhead for the panel. + // On this wire the system rides the injected developer message outside the + // fold space, so counting devContent + tools does not double-count the + // mid-history items the kernel already classifies. + if (transformOk) { + session.metadata.systemPromptTokens = countSystemAndToolsTokens(responsesDevContent ?? "", toolsOut); + } + // #728: record this turn's outbound payload upper bound as the fallback + // token source for upstreams that never report usage (see effectiveTokenCount). + // Compaction-trigger requests are the compression mechanism itself — no + // incremental decision hangs off them, so don't leave a stale reading. + if (!isCompactionTrigger) { + session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + + countSystemAndToolsTokens(responsesDevContent ?? "", toolsOut) + + imageTokensInParsedBody("responses", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)); + } + snapshotMessages(session, originalMessages); + markDirty(session); + return { + body: JSON.stringify(rebuilt), + session, + processedMessages, + originalMessages, + responsesProjection, + systemNotes: sysNotes, + protocol: "responses", + stream, + compressInjected: injectTools && !isCompactionTrigger, + pluginMode, + responsesTextProtocol, + nudge, + prompts, + surface, + renderTags, + resetAfterSuccess: isCompactionTrigger, + codexForge, + }; +} + +export function prepareResponsesCompact( + body: Buffer, + parsed: ResponsesRequestBody, + session: Session, + req: http.IncomingMessage, + core: CompressionCore, + config: Config, + log: (level: string, msg: string) => void, +): Prepared { + ++session.stats.requests; + // A bili-forged compaction item is never for the upstream (it carries our + // sentinel blob) — strip it on every forwarding path, same as the normal + // /responses pipeline does. + const cleaned = Array.isArray(parsed.input) ? stripBiliCompactionItems(parsed.input) : parsed.input; + const stripped = Array.isArray(parsed.input) && cleaned.length !== parsed.input.length; + const forgeBody: ResponsesRequestBody = { ...parsed, input: cleaned }; + const base: Prepared = { + body: stripped ? Buffer.from(JSON.stringify(forgeBody)) : body, + session, + processedMessages: [], + originalMessages: [], + protocol: "responses", + stream: parsed.stream === true, + compressInjected: false, + resetAfterSuccess: true, + }; + // #332: gate preconditions BEFORE the transform — when they fail the + // request passes through verbatim without running processTurn (no state + // mutation as a side effect of a compact that will not be intercepted). + if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input) + || !codexCompactGatePre(session, config.modelContextLimit)) { + return base; + } + // The state commit below is all-or-nothing: every non-forge path restores + // the pre-turn state so a passthrough compact is not raced against a + // half-applied fold (the upstream's own compaction boundary is handled by + // markNativeCompactionBoundary + rebase instead). + const prevState = session.state; + const prevStore = session.contentStore; + const prevStoreDirty = session.contentStoreDirty === true; + // E2 endpoint form: /responses/compact. When the gate passes, run the same + // fold pipeline as a normal turn and forge the compacted history as + // {"output": [...]} — a deterministic handoff to the ACP state instead of a + // foreign compaction blob. + let transformOk = false; + try { + const projection = responsesToCore(forgeBody); + // The forged handoff is one-shot with no tool channel: strip absorb so + // no [ACP absorb] instruction bakes into the forged history, and run + // the absorb view so absorbed pairs stay hidden in it (wire parity). + const compactConfig = ccrLoopConfig(session, { ...config, absorb: undefined }); + const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); + session.state = turn.state; + adoptContentStore(session, turn.contentStore); + transformOk = true; + if (!codexCompactGate(session, config.modelContextLimit, transformOk)) { + session.state = prevState; + session.contentStore = prevStore; + session.contentStoreDirty = prevStoreDirty; + return base; + } + const viewed = applyAbsorbView(turn.messages, turn.state, compactConfig, session.stats.lastInputTokens); + const processed = repairResponsesAssistantOrdering(stripKernelSummaries(viewed, turn.state), projection.msgs); + let output = patchResponsesInput(projection, processed); + if (typeof output === "string") { + session.state = prevState; + session.contentStore = prevStore; + session.contentStoreDirty = prevStoreDirty; + return base; + } + output = hoistTrappedToolItems(output); + snapshotMessages(session, projection.msgs); + markDirty(session); + log("info", `[${session.id}] codex compact intercepted (endpoint); forged history with ${output.length} item(s), upstream not contacted`); + return { ...base, codexForge: { kind: "endpoint", body: JSON.stringify({ output }), contentType: "application/json" } }; + } catch (err) { + session.state = prevState; + session.contentStore = prevStore; + session.contentStoreDirty = prevStoreDirty; + log("warn", `[${session.id}] codex compact forge failed (${String(err)}); passing through to upstream`); + return base; + } +} + +export function shouldInjectPromptCacheKey( + routing: ProxyOptions["promptCache"]["routing"], + upstream: string | undefined, +): boolean { + if (routing === "enabled") return true; + if (routing === "disabled" || !upstream) return false; + try { + return new URL(upstream).hostname.toLowerCase() === "api.openai.com"; + } catch { + return false; + } +} + +export function resolvePromptCacheKey( + explicit: string | undefined, + identity: ConversationIdentity, + routing: ProxyOptions["promptCache"]["routing"], + upstream: string | undefined, +): string | undefined { + if (explicit?.trim()) return explicit; + if (!identity.clientProvided || !shouldInjectPromptCacheKey(routing, upstream)) return undefined; + return identity.value; +} + +/** When true, the Responses path teaches compression via a text trigger + * instead of a function tool. Used for hosts (OpenAI Codex code_mode) whose + * server-side tools are disabled the moment any `tools` entry is declared. + * In text mode we keep `tools` untouched (undefined) so code_mode stays + * active, and detect the trigger in the output_text stream instead. */ +export const FORCE_TEXT_PROTOCOL = process.env.ACP_COMPRESS_PROTOCOL === "text"; diff --git a/src/server/prepare-shared.ts b/src/server/prepare-shared.ts new file mode 100644 index 0000000000..1f3c6d6529 --- /dev/null +++ b/src/server/prepare-shared.ts @@ -0,0 +1,246 @@ +// Shared contract + helpers for the per-protocol prepare pipelines +// (extracted verbatim from src/server.ts — #1440 P2 four-cut disassembly, +// cut 2). The Prepared type is the wire contract between prepare*() and +// forward()/preflight. + +import { resolveAbsorbSettings } from "../compress-settings.js"; +import { CompressSettings, ProxyOptions, findRoute } from "../config.js"; +import { recordConflict } from "../conflict-watch.js"; +import { ResolvedImageBilling, resolveImageBilling } from "../image-tokens.js"; +import { reapOrphanBlocks } from "../orphan-gc.js"; +import { estimateCoreMessagesUpper } from "../preflight.js"; +import { CompressReasoningConfig, dropCompressReasoning } from "../reasoning-drop.js"; +import { PendingRetrieval, Session } from "../session.js"; +import { WireProtocol } from "../util.js"; +import { AbsorbConfig, CompressionState, Config, CoreMessage, NudgeDecision, PackSurface, Prompts, deactivateBlock } from "acp-kernel"; +import { AnthropicRequestBody, BiliMessage, ResponsesProjection } from "acp-kernel/wire"; + +export type Prepared = { + body: string | Buffer; + session: Session; + processedMessages: CoreMessage[]; + /** Original CoreMessages from the protocol conversion, BEFORE processTurn + * folded/replaced anything. compress/decompress/acp_status need the raw + * text (collectBlockContent reads message text by id); processedMessages + * has compressed messages replaced with placeholders → empty content. */ + originalMessages: CoreMessage[]; + protocol: WireProtocol; + stream: boolean; + compressInjected: boolean; + /** True when the session is driven by a cooperative agent-side plugin + * (x-bili-plugin header, see src/plugin.ts): tools are native, the + * response must pass through verbatim, and usage is sniffed instead of + * captured by the compress loop. */ + pluginMode?: boolean; + responsesTextProtocol?: boolean; + resetAfterSuccess?: boolean; + responsesProjection?: ResponsesProjection; + anthropicSystem?: AnthropicRequestBody["system"]; + /** Original leading system/developer prefix text captured by the kernel's + * openai hoist (0.0.37). The fold space no longer carries it, so every + * rebuilt payload and compress-loop round must re-inject it. */ + openaiSystemText?: string; + /** Google wire: the client's own `systemInstruction` text (the kernel hoists + * it out of the fold space) and the path-derived model. Both are needed to + * rebuild `systemInstruction` and to synthesize chunks on every compress + * loop round. */ + google?: { system?: string; model?: string }; + /** #1085: stable-system-anchor update notes for this turn — re-injected + * as trailing user messages so compress-loop rounds see the same + * updated-instructions context the main request did. */ + systemNotes?: string[]; + /** [#1343] Plugin-lane retrievals snapshotted onto THIS request's body. + * Carried so forward() commits them on upstream success or drops them + * (logged + corrective note) on failure — the ack is already out, so the + * full text must never vanish silently. */ + attachedRetrievals?: PendingRetrieval[]; + nudge?: NudgeDecision; + /** Render strategy the prepare used for processTurn ("none" for codex + * compaction triggers / ACP_RENDER_NONE). The #422 fold-refresh hook in + * forward() re-runs processTurn with the same strategy so the re-request + * renders tags exactly like the request that produced it. */ + renderTags?: "text-only" | "none"; + /** Effective compression prompts for this request (three-level cascade, + * defaults to the kernel's defaultPrompts). Carried so the compress loop + * in forward() rebuilds the SAME system prompt the request was prepared + * with. */ + prompts?: Prompts; + /** Effective pack surface (promptPack) for this request: tool prompts, + * system-prompt sections, nudge sections. Same carrying rationale as + * prompts — the compress loop must rebuild the identical surface. */ + surface?: PackSurface; + /** #388: side request (title-gen etc.) — transport with render-tag strip + * only. Skips preflight (handle() returns before it), the fake-completion + * retry wrapper, the compress loop, and every usage-sniffing pipe; the + * #460 strip pipes in forward() run with session=undefined. */ + sidePassthrough?: boolean; + /** Set when a codex native-compaction request was intercepted and a + * success response was forged locally (BILI_CODEX_COMPACT=intercept + + * gate passed). forward() serves `body` without contacting upstream. */ + codexForge?: { kind: "endpoint" | "trigger"; body: string; contentType: string }; +}; + +// #767: per-request image billing mode — env BILI_IMAGE_BILLING (live, like +// BILI_IMAGE_TOKEN_CAP) wins over the per-provider route entry, which wins over +// the global config level; "auto"/unset classifies known first-party pixel-tile +// hosts by upstream URL. Every payload-size decision below consults this so one +// over-estimate cannot block all of them at once. +export function imageBillingFor(opts: ProxyOptions, upstreamUrl: string | undefined): ResolvedImageBilling { + const env = process.env.BILI_IMAGE_BILLING; + const configured = env === "pixels" || env === "bytes" ? env : findRoute(opts.routes, upstreamUrl)?.imageBilling ?? opts.imageBilling ?? "auto"; + return resolveImageBilling(configured, upstreamUrl); +} + +const ACP_TAG_MARK = "\x3cacp "; + +// acp-kernel injects an in-place `acp_summary_*` at the compressed range as a +// generic-library fallback. This host strips it ONLY when it is redundant: the +// block's compress tool-call also carries the summary (hideConsumedCompressCalls +// keeps active-block calls), and a mid-stream insertion would shift the upstream +// prefix-cache breakpoint. Blocks created without a tool call (preflight +// compression, src/preflight.ts — #247) have NO other carrier: their anchor is +// the only place the summary reaches the model, so it must survive. +// +// Per mode (see README "Two compression modes"): in plugin/launcher mode the +// tool call is ALWAYS in the re-sent history (the agent owns compression), so +// this strips every acp_summary and the carrier is the tool call; in proxy mode +// the tool call is usually absent (ephemeral server-side execution) or +// nonexistent (preflight), so acp_summary survives as the carrier and +// systemToUser later re-voices the survivors as USER messages (leaving them at +// their anchors) for strict backends (#377). +/** [#651] Strip oversized reasoning from closed compress turns (see + * src/reasoning-drop.ts) with an ops log line when anything was dropped. */ +export function withReasoningDrop( + msgs: BiliMessage[], + reasoning: CompressReasoningConfig | undefined, + log: (level: string, msg: string) => void, + sessionId: string, + strictEcho: boolean, +): BiliMessage[] { + // [#684] strict-echo upstreams: reasoning must round-trip with tool_calls, + // so #651's drop must not fire. Learned/static strictness both land here. + if (strictEcho) return msgs; + const out = dropCompressReasoning(msgs, reasoning); + if (out.length !== msgs.length) { + log("info", `[${sessionId}] compress-reasoning: dropped ${msgs.length - out.length} reasoning message(s) from closed compress turns (#651)`); + } + return out; +} + +export function stripKernelSummaries(messages: BiliMessage[], state: CompressionState): BiliMessage[] { + const carried = new Set(); + for (const b of state.blocks) { + if (!b.active || !b.compressCallId) continue; + if (messages.some((m) => m.contentType === "tool-call" && m.toolCallId === b.compressCallId)) { + carried.add(`acp_summary_${b.blockId}`); + } + } + return messages.filter((m) => !(m.id ?? "").startsWith("acp_summary_") || !carried.has(m.id)); +} + +export function diagTagSummary(messages: CoreMessage[], sessionId: string, strategy: string): string { + let textTagged = 0; + let toolTagged = 0; + for (const m of messages) { + const hasTag = (m.text ?? "").includes(ACP_TAG_MARK); + if (!hasTag) continue; + if (m.contentType === "tool-call" || m.contentType === "tool-result") toolTagged++; + else textTagged++; + } + return `[${sessionId}] processTurn: ${messages.length} msgs, renderTags=${strategy}, ${textTagged} text tagged, ${toolTagged} tool tagged (should be 0 with text-only)`; +} + +export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; contextUsage: number; tier: number | null; breakdown?: Record } | null }, sessionId: string, tokenCount: number, limit: number, model: string | undefined, willInject: boolean): string { + const n = turn.nudge; + if (!n) return `[${sessionId}] nudge: unavailable`; + const b = n.breakdown ?? {}; + const pct = limit > 0 ? `${Math.round((tokenCount / limit) * 100)}%` : "?"; + const growth = b["growth"] ?? 0; + const floor = b["growthFloor"] ?? 0; + const interval = b["nudgeGrowthTokens"] ?? 0; + const pendingT1 = b["pendingT1"] ?? 0; + const ref = b["growthReference"] ?? 0; + // "INJECT" only when the nudge actually reaches the upstream payload. When + // armed but suppressed by config/mode, say so explicitly so the log never + // lies about delivery (#451, same class as #413). + const inject = willInject + ? (n.shouldInject ? `INJECT T${n.tier ?? "?"}` : `INJECT-ESC T${n.tier ?? "?"}`) + : (n.shouldInject ? `ARMED-SUPPRESSED T${n.tier ?? "?"}` : "idle"); + const modelTag = model ? ` model=${model}` : ""; + return `[${sessionId}] nudge ${inject}: usage=${pct} (${tokenCount}/${limit}), growth=${growth}/${floor} (ref=${ref}, interval=${interval}), pendingT1=${pendingT1}/${interval}${modelTag}, reason="${n.reason.slice(0, 120)}"`; +} + +// Zero-baseline sessions are judged conservatively ONLY when they arrived +// anonymously (prefix-affinity forks/reloads, #553): they carry the full raw +// history but no measurement yet, so feeding 0 blinds the nudge (usage 0%, +// growth ref 0) and no compression trigger fires until overflow. Explicit- +// identity zero-baseline sessions previously stayed at 0 on the assumption +// that they "self-heal via the next measured usage report" — an assumption +// that breaks for upstreams that NEVER report usage (ChatGPT-login backends, +// #728): lastInputTokens stays 0 for the whole session, and the kernel's +// decideNudge is structurally unfireable at tokenCount == 0 (growth ref falls +// back to tokenCount itself → growth ≡ 0; firstSightMassReady/pressure bands +// all require usage >= their pct lines). #728 fix: such sessions fall back to +// the PREVIOUS turn's locally-measured outbound payload upper bound +// (session.stats.localInputEstimate, recorded in prepare* each turn). The +// estimator only errs EARLY (char-count upper bound → compress earlier, never +// later), is active only while lastInputTokens == 0 (a real usage report takes +// precedence immediately — same invariant as #604's armFailureShrink +// exception), and self-corrects after every fold (the post-fold payload +// shrinks → the next estimate drops). Turn 1 of a fresh explicit session +// still feeds 0 (nothing measured yet; nothing pending either), so +// first-turn behavior is byte-identical to pre-#728. +export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inboundImageTokens = 0): number { + if (session.stats.lastInputTokens > 0) return session.stats.lastInputTokens; + // The inbound upper bound carries the image term in BOTH zero-baseline + // regimes (#1119/#1137): the wire codecs move images out of + // CoreMessage.text into sidecars, so a text-only bound drops them. + // #1137: an anonymous prefix-affinity fork (#553) replays its FULL raw + // history — images included — in this one request, so this request's + // image mass IS the history's image mass; returning the text-only bound + // there left image-heavy forks reading single-digit usage % and nudging + // only at overflow. + const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens; + if (session.metadata.anonymousPrefixAffinity) return raw; + const est = session.stats.localInputEstimate ?? 0; + if (est <= 0) return 0; + // Cap by THIS request's inbound upper bound: the recorded estimate lags by + // one turn, so after a client-side shrink (native compaction echo, history + // edit) the previous turn's payload can be larger than what is in front of + // us now — never claim more context than the current request could hold. + // In steady state est <= raw bound always (the outbound fold is never + // larger than the inbound history), so this is a no-op there. A + // client-side shrink still shrinks the bound (fewer messages AND fewer + // images), preserving the stale-high invariant. + return Math.min(est, raw); +} + +// #1359: which absorb block governs a session, by lane. Proxy lane keeps the +// per-request merged block (provider/model overrides apply); plugin lane uses +// the base block so the manifest's advertised name and the gate's adjudicated +// name always agree — provider/model absorb.* overrides are proxy-lane-only. +export function effectiveAbsorbBlock(pluginMode: boolean, config: Config, baseAbsorb?: CompressSettings["absorb"]): AbsorbConfig | undefined { + return pluginMode ? resolveAbsorbSettings(baseAbsorb) : config.absorb; +} + +/** Derive a short human-readable title from the first user text message. + * Used so the web UI can show "Fix auth bug" instead of an opaque hash. */ +export function deriveTitle(messages: CoreMessage[]): string | undefined { + for (const m of messages) { + if (m.role !== "user" || m.contentType !== "text") continue; + const clean = (m.text ?? "").replace(/\s+/g, " ").trim(); + if (clean) return clean.length > 60 ? clean.slice(0, 57) + "\u2026" : clean; + } + return undefined; +} + +// #1206: orphan reaping was silent — blocks deactivated because their source +// messages vanished from client history are the strongest runtime signal that +// something outside bili (client auto-compaction or another compression plugin) +// rewrote the conversation. Log it and record it in the session ledger. +export function reapOrphansLogged(session: Session, msgs: CoreMessage[], log: (level: string, msg: string) => void, sessionId: string): void { + const { reaped } = reapOrphanBlocks(session, msgs, deactivateBlock); + if (reaped.length === 0) return; + log("warn", `[${sessionId}] orphan-gc deactivated ${reaped.length} block(s) whose source messages left the client history (${reaped.join(", ")}) — the client or another compression plugin deleted summarized content; those summaries can no longer be decompressed (#1206)`); + recordConflict(session, "orphan-reap", `${reaped.length} block(s) deactivated: ${reaped.join(", ")}`); +} diff --git a/tests/codex-official.test.ts b/tests/codex-official.test.ts index adcd615483..28e78e60b8 100644 --- a/tests/codex-official.test.ts +++ b/tests/codex-official.test.ts @@ -4,7 +4,8 @@ import http from "node:http"; import { once } from "node:events"; import { gzipSync } from "node:zlib"; import { createInitialState, defaultConfig } from "acp-kernel"; -import { startServer, isChatGptCodexUpstream, isCodexResponsesLite, shouldInjectPromptCacheKey, resolvePromptCacheKey } from "../src/server.ts"; +import { startServer, isChatGptCodexUpstream, isCodexResponsesLite } from "../src/server.ts"; +import { resolvePromptCacheKey, shouldInjectPromptCacheKey } from "../src/server/prepare-responses.ts"; import { listSessions } from "../src/session.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import type { ProxyOptions } from "../src/config.ts"; diff --git a/tests/count-tokens.test.ts b/tests/count-tokens.test.ts index 525766ffd2..8893212742 100644 --- a/tests/count-tokens.test.ts +++ b/tests/count-tokens.test.ts @@ -2,7 +2,8 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { createCore, createInitialState, defaultConfig, coveredMessageIds } from "acp-kernel"; import type { Session } from "../src/session.ts"; -import { prepareCountTokens, isCountTokensRequest } from "../src/server.ts"; +import { isCountTokensRequest } from "../src/server.ts"; +import { prepareCountTokens } from "../src/server/prepare-anthropic.ts"; import { anthropicToCore, type AnthropicRequestBody } from "acp-kernel/wire"; function makeSession(): Session { diff --git a/tests/fix-1205-file-parts.test.ts b/tests/fix-1205-file-parts.test.ts index b17a97d4ad..b7398796e4 100644 --- a/tests/fix-1205-file-parts.test.ts +++ b/tests/fix-1205-file-parts.test.ts @@ -5,7 +5,7 @@ process.env.NODE_ENV = "test"; import { REMOTE_IMAGE_TOKENS, imageTokensInParsedBody, imageTokensInRawBody } from "../src/image-tokens.ts"; import { droppedOpenaiParts } from "../src/wire-drop-warn.ts"; -import { _resetWireDropWarningsForTest, warnDroppedOpenaiParts } from "../src/server.ts"; +import { _resetWireDropWarningsForTest, warnDroppedOpenaiParts } from "../src/server/prepare-openai.ts"; import { openaiToCore, coreToOpenai } from "acp-kernel/wire"; // Issue #1205: the openai wire codec silently dropped every user content part diff --git a/tests/issue564-responses-ordering.test.ts b/tests/issue564-responses-ordering.test.ts index dbae4744f6..ca00a08520 100644 --- a/tests/issue564-responses-ordering.test.ts +++ b/tests/issue564-responses-ordering.test.ts @@ -6,7 +6,9 @@ import { responsesToCore, coreToResponses, type ResponsesProjection, type BiliMe import type { Session } from "../src/session.ts"; import { runCompressLoop, createResponsesAdapter } from "../src/loop/index.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; -import { stripKernelSummaries, repairResponsesAssistantOrdering } from "../src/server.ts"; +import { startServer } from "../src/server.ts"; +import { stripKernelSummaries } from "../src/server/prepare-shared.ts"; +import { repairResponsesAssistantOrdering } from "../src/server/prepare-responses.ts"; // #564 regression: after a successful compress, upstream must still receive a // valid Responses assistant history. Within one assistant run the order is diff --git a/tests/reasoning-echo.test.ts b/tests/reasoning-echo.test.ts index d1e355b8de..df579d92b9 100644 --- a/tests/reasoning-echo.test.ts +++ b/tests/reasoning-echo.test.ts @@ -1,6 +1,9 @@ import { describe, it } from "node:test"; import assert from "node:assert/strict"; -import { isStrictReasoningEcho, normalizeStrictEchoReasoning, warnReasoningPairs, warnAnthropicThinkingPairs, warnResponsesReasoningPairs } from "../src/server.js"; +import { isStrictReasoningEcho, normalizeStrictEchoReasoning } from "../src/server.js"; +import { warnAnthropicThinkingPairs } from "../src/server/prepare-anthropic.ts"; +import { warnReasoningPairs } from "../src/server/prepare-openai.ts"; +import { warnResponsesReasoningPairs } from "../src/server/prepare-responses.ts"; import { modelIdOf, normalizeStrictEchoBody } from "../src/strict-echo.js"; import type { Session } from "../src/session.js"; import type { OpenAIMessage } from "acp-kernel/wire";