diff --git a/AGENTS.md b/AGENTS.md index a4e1c7c74a..f047c667b2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -82,6 +82,7 @@ billion-context/ 4. **Tee logger**: all proxy logs go through `src/logger.ts` (file + stderr). Do NOT use `console.error` in server-side modules — use `loggerLog()`. 5. **acp-kernel MUST be pinned to an exact version** (e.g. `"acp-kernel": "0.0.17"`, NEVER `"^0.0.17"`). Because acp-kernel is a build-time dependency that tsup bundles inline into `dist`, a caret range makes the resolved version drift if `package-lock.json` is regenerated or absent, breaking reproducible builds. When bumping acp-kernel: set the exact version in `package.json`, run `npm install` to refresh the lockfile, then rebuild. The `package-lock.json` is committed and kept in sync. 6. **Two compression modes with different summary carriers** — `pluginMode` (the `x-bili-plugin` header / registered agent, e.g. `bili pi`) means the ACP-native agent OWNS compression: it executes `compress` locally, the call+result live in its own re-sent history, and the summary carrier on the wire is the **tool call** (the proxy suppresses tool + nudge injection; the agent's view never renders the kernel's `acp_summary`). Proxy mode (plain client, no header) means the proxy executes `compress` server-side: the tool call is ephemeral (never enters the client's history) and preflight blocks have none, so the summary carrier is the **`acp_summary` message**, which the kernel renders as role `system` but `systemToUser` (`src/util.ts`) re-voices as a **`user` message** (leaving it at its anchor) so strict backends (SGLang: exactly one system at index 0, #377) accept it and the head system message stays byte-stable for the prefix cache. The mode is decided per request and bound per session (`session.metadata.pluginAgent`, sticky, upgrade-only). See README "Two compression modes". +7. **修复重启、切模型或失败重试时**:`plugin-conversations` 也包含普通缓存键映射,不能据此认定插件身份;保留旧映射的压缩状态,并以原始历史边界约束重算后的可压缩范围,回归入口是 `tests/subagent-namespace.test.ts` 和 `tests/preflight-fail-fast.test.ts`。 ### Kernel Contract: Message Ids Are Never Reused diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aaa897124..56b4edf231 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixes +- **Compress failures are retryable in the same turn (#743)**: hosts (claude-code/openai/codex) pre-validate tool arguments against the served schema and reject a missing `content` CLIENT-side — the model only ever sees a bare `content: is required` and, observed in production, gives up instead of retrying, leaving the nudge pending forever. `content` is no longer schema-required on any served surface (the description now documents that validation happens at execution); a malformed call reaches the executor and gets the actionable `[Compression FAILED …]` receipt it can fix in the same loop. Failed compress call+result pairs are dropped from CLOSED turns before forwarding — a failed compress in history is dead weight once its turn has ended (the success that replaced it, or nothing, follows), and stale failure text under sustained pressure teaches the wrong lesson; current-turn pairs stay for in-place retry and successful compresses are never matched (they remain as plugin-mode block carriers). Tests in `tests/compress-retry.test.ts`; #349 and #841 pin the schema deltas. + +- **Failed compress receipts name the live compressible span**: after a fold the model keeps anchoring on refs an earlier block already consumed — a live session (01a0b0c4) burned 28 minutes on three consecutive `[Compression FAILED]` receipts (already compressed → cannot be anchored → no valid ranges), wasted two full T1 checkpoints, then gave up and let the nudge re-fire every turn. The kernel errors say "run acp_status and retry", but recovery needs two more round-trips a mid-task model won't take. Both failure receipts (no valid ranges, zero blocks created) now append the concrete span that is still raw and directly compressible — `Live compressible refs: m05373–m05448 (everything up to m05372 is already inside active blocks). Retry NOW in this same turn` — computed as the highest mapped raw ref minus the newest active-block boundary; when no raw refs remain it falls back to suggesting a run of ACTIVE blocks. Tests in `tests/compress-retry.test.ts`. + +- **Omp compression survives model switches and restored session mappings**: only an actual `omp` registration or persisted plugin ownership bypasses instruction-based subagent partitioning; a plain `prompt_cache_key` does not. Existing conversation mappings restore their original session and compression blocks instead of creating an empty bare-id session. Failed preflight freezes the original history boundary and recomputes remaining viable ranges after each fold, including soft-protected history when needed, without absorbing turns appended during the failure window. +- **Preflight no longer wedges on a stale usage baseline while the prepared payload fits (#739 follow-up)**: the preflight trigger floors on `session.stats.lastInputTokens`, and after an earlier as-is forward under preflight (#300) that measured baseline can stay pinned above the window — a live session kept a 257,613-token floor against a 256,000 window while its real prepared payload was ~127k. Every turn then re-ran the fold walk on ranges the payload no longer needed: three 60s non-stream summarization attempts (~3.4min stall), an upstream 500 → structured 502, a 5-minute dead-end cooldown, repeat. The trigger now forwards as-is when a known-baseline session's payload estimate fits yet its measured baseline sits above the window (#301: as-is is safe; the next usage report self-heals the baseline). Inside-window folding is untouched — the codex headroom target still walks fitting 90-100% payloads. `tests/preflight-fail-fast.test.ts` pins it: fitting payload + stale floor above the window → forwarded with zero summarization calls. - **Claude Code subagent requests no longer queue behind the main turn's lock (#970)**: Claude Code stamps the SAME `x-claude-code-session-id` on every model request of a session — background subagents (Task tool) included — so main turn and all subagents landed on ONE session and serialized on `withSessionLock` (which covers prepare + forward + the whole streamed response). A slow subagent first token head-of-line-blocked the main turn (and vice versa) for the full upstream latency; observed live as 116-142s first-byte stalls when two subagents launched concurrently. Discrimination is two-signal, both verified against live Claude Code 2.1.274 traffic (`-p` mode, general-purpose AND custom agent types like Explore): (1) the request carries `x-claude-code-agent-id` and/or `x-claude-code-parent-agent-id` — the parent header alone is NOT reliable (`-p` general-purpose subagents send it, custom agent types do not; main turns carry agent-id only from their second request on); (2) the MAIN agent's system always contains a block starting with "You are Claude Code…" (interactive) or "You are an interactive agent…" (`-p`/SDK), while a subagent's system blocks are its agent definition ("This session is a background job…", "You are a file search specialist for…", …) — no stable positive marker exists across agent types, so the check is negative (none of the main prefixes may match). The anthropic identity branch derives `|sub:` only when BOTH signals say subagent (`claudeSubagentAgentId`/`claudeSubagentSplit` in `src/session-id.ts`, mirroring the codex #316 / Responses `subagentNamespace` precedents): each subagent gets its own lock chain and its own compression state (a subagent is its own conversation — same isolation semantics as #150). Failure degrades in the safe direction: a future rewording of the main prompts only stops the split (back to serial), never mis-splits the main session. `anthropicIdentity` itself is NOT rewritten — `affinityToken`/`clientLabel` keep the raw value, so upstream prefix caches and the UI label stay continuous across main and subagent sessions. Two guards keep the plugin layer honest: a split session's first request cannot claim the ROOT's pending register (#317 mirror), and `recordPluginSession` keys a subagent session under its split id instead of the raw conversation value — the single-valued conversations map used to flip between main and subagent sessions on every interleaved request, breaking `/acp` lookups and MCP tool routing. Older Claude Code (no headers), other agents, and curl are untouched (no split, previous behavior). Opt out with `BILI_SUBAGENT_SPLIT=0` or `"subagentSplit": false` in the config file. Tests pin: two-signal discrimination across the captured system shapes, stable namespaces, session isolation with main-identity stability, a deterministic no-stall case (upstream gates the main stream 1200ms; the subagent's first byte must beat it by a wide margin — pre-fix it arrives after, reproduced at 1238ms), and the off-switch restoring single-session behavior. End-to-end verified against live traffic: two concurrent Task subagents through the proxy land on `…|sub:` sessions in the log and complete in parallel. ### Features diff --git a/src/compress-tool.ts b/src/compress-tool.ts index 5d8ef99638..887f845e9d 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -13,10 +13,14 @@ import { parseCompressArgs, ABSORB_TOOL_OPENAI, + COMPRESS_TOOL, + COMPRESS_TOOL_OPENAI, + COMPRESS_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL, SEARCH_CONTEXT_TOOL_OPENAI, SEARCH_CONTEXT_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, + COMPRESS_TOOL_NAME, ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, @@ -102,10 +106,62 @@ export const BILI_SEARCH_CONTEXT_TOOL_RESPONSES = { parameters: withConversationId(SEARCH_CONTEXT_TOOL_RESPONSES.parameters), }; -export const BILI_ACP_TOOLS_ANTHROPIC = ACP_TOOLS_ANTHROPIC.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL : t)); -export const BILI_ACP_TOOLS_OPENAI = ACP_TOOLS_OPENAI.map((t) => (t.function.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_OPENAI : t)); -export const BILI_ACP_TOOLS_RESPONSES = ACP_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t)); -export const BILI_ACP_READONLY_TOOLS_RESPONSES = ACP_READONLY_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t)); +// compress-retry: hosts pre-validate tool args against the served schema and +// reject a missing `content` CLIENT-side — the model only sees a bare +// "content: is required" and, observed in production, gives up instead of +// retrying. Softening `required` routes those calls to the executor, which +// answers with the actionable [Compression FAILED ...] receipt (src/stream.ts +// applyRanges) the model can fix in the same loop. +const COMPRESS_RETRY_NOTE = + "\n\n[NOTE: `content` is validated when the call EXECUTES, not by the schema. An omitted or invalid `content` returns a retry error and compresses NOTHING — re-issue the call with content: [{startId, endId, summary}].]"; + +function withoutRequiredContent(schema: JsonSchemaObject): JsonSchemaObject { + return { ...schema, required: (schema.required ?? []).filter((n) => n !== "content") }; +} + +export const BILI_COMPRESS_TOOL = { + ...COMPRESS_TOOL, + description: COMPRESS_TOOL.description + COMPRESS_RETRY_NOTE, + input_schema: withoutRequiredContent(COMPRESS_TOOL.input_schema), +}; + +export const BILI_COMPRESS_TOOL_OPENAI = { + ...COMPRESS_TOOL_OPENAI, + function: { + ...COMPRESS_TOOL_OPENAI.function, + description: COMPRESS_TOOL_OPENAI.function.description + COMPRESS_RETRY_NOTE, + parameters: withoutRequiredContent(COMPRESS_TOOL_OPENAI.function.parameters), + }, +}; + +export const BILI_COMPRESS_TOOL_RESPONSES = { + ...COMPRESS_TOOL_RESPONSES, + description: COMPRESS_TOOL_RESPONSES.description + COMPRESS_RETRY_NOTE, + parameters: withoutRequiredContent(COMPRESS_TOOL_RESPONSES.parameters), +}; + +function replaceTool(toolList: T[], name: string, replacement: T): T[] { + return toolList.map((t) => (t.name === name ? replacement : t)); +} + +export const BILI_ACP_TOOLS_ANTHROPIC = replaceTool( + ACP_TOOLS_ANTHROPIC.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL : t)), + COMPRESS_TOOL_NAME, + BILI_COMPRESS_TOOL, +); +export const BILI_ACP_TOOLS_OPENAI = ACP_TOOLS_OPENAI + .map((t) => (t.function.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_OPENAI : t)) + .map((t) => (t.function.name === COMPRESS_TOOL_NAME ? BILI_COMPRESS_TOOL_OPENAI : t)); +export const BILI_ACP_TOOLS_RESPONSES = replaceTool( + ACP_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t)), + COMPRESS_TOOL_NAME, + BILI_COMPRESS_TOOL_RESPONSES, +); +export const BILI_ACP_READONLY_TOOLS_RESPONSES = replaceTool( + ACP_READONLY_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t)), + COMPRESS_TOOL_NAME, + BILI_COMPRESS_TOOL_RESPONSES, +); // The kernel ships no Responses-format absorb const (the four ACP tools have // *_RESPONSES variants; absorb is host-registered opt-in). Synthesize it in diff --git a/src/plugin.ts b/src/plugin.ts index 8707afaa87..e71fc5b801 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -4,7 +4,7 @@ import { fileURLToPath } from "node:url"; import type { ServerResponse } from "node:http"; import fs from "node:fs"; import path from "node:path"; -import { acquireInFlight, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js"; +import { acquireInFlight, effectiveConfig, findSessionByCanonicalId, getSession, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js"; import { ABSORB_TOOL, ABSORB_TOOL_NAME, ABSORB_TOOL_OPENAI, ABSORB_TOOL_RESPONSES, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, PROXY_TOOL_NAMES, SEARCH_CONTEXT_CONVERSATION_ID_PARAM, SEARCH_CONTEXT_TOOL_NAME } from "./compress-tool.js"; import { effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js"; import { executeProxyTool } from "./loop/core.js"; @@ -240,6 +240,16 @@ export function recordPluginSession(conversationId: string, sessionId: string): scheduleSaveConversations(); } +/** Omp assigns each subagent its own UUID; a plain prompt_cache_key does not. + * Reuse the mapped session, including an existing |sub: namespace, rather + * than creating an empty bare-id session when the model changes. */ +export function registeredPluginSessionId(conversationId: string): string | undefined { + const mappedId = conversations.get(conversationId)?.sessionId; + const mapped = mappedId ? getSession(mappedId) : undefined; + const agent = registeredIds.get(conversationId) ?? mapped?.metadata.pluginAgent; + return agent === "omp" ? mappedId ?? conversationId : undefined; +} + /** Keep the last prepare()'s view for a plugin session so tool-API execution * sees the exact refs the model was shown (mirrors the wire-mode loop, which * runs executeProxyTool against prepared.processedMessages). */ diff --git a/src/preflight.ts b/src/preflight.ts index 02861d3df7..ad8ea0d309 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -56,6 +56,15 @@ export const MAX_SUMMARY_CALLS_PER_PREFLIGHT = 16; // incident class behind #868 (a 1.39x-window payload); it is a fixed depth, // not scaled to the overshoot — scaling it is a separate design question. +// Freeze the original history boundary, not a worklist: every successful fold +// changes which ranges remain viable. Failure-window turns stay outside it. +function readPreflightPin(metadata: Record): string | undefined { + const pin = metadata.preflightPin; + if (typeof pin !== "object" || pin === null) return undefined; + const endRef = (pin as Record).endRef; + return typeof endRef === "string" && /^m\d+$/.test(endRef) ? endRef : undefined; +} + export type PreflightProtocol = "anthropic" | "openai" | "responses"; export interface PreflightDeps { @@ -701,6 +710,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa let target = Math.min(limit, deps.compressionTarget ?? limit); const result: PreflightResult = { compressedRanges: 0, savedTokens: 0, payloadEstimate: estimateCoreMessages(messages) + (deps.imageFloor ?? 0) + (deps.wireOverhead ?? 0), rangesRemaining: 0, fitsWindow: true }; if (limit <= 0) return result; + let watermark = readPreflightPin(deps.session.metadata); const budget = Math.max(MIN_CHUNK_TOKENS, Math.floor(limit * CHUNK_FRACTION)); // applyCompression rejects ranges below config.compress.minCompressRange // chars, so never spend a summarization call on a chunk that can't apply. @@ -772,7 +782,20 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa result.payloadEstimate = estimateCoreMessages(turn.messages) + (deps.imageFloor ?? 0) + (deps.wireOverhead ?? 0); if (startTokens < 0) startTokens = currentTokens; if (currentTokens < target) break; - const ranges = viableRanges(turn.nudge?.compressibleRanges ?? []); + const maps = refMaps(messages, deps.session.state); + if (!watermark) { + for (const ref of maps.idxToRef.values()) { + if (!watermark || refNum(ref) > refNum(watermark)) watermark = ref; + } + } + const boundary = refNum(watermark ?? ""); + let endRef: string | undefined; + for (const ref of maps.idxToRef.values()) { + if (refNum(ref) <= boundary && (!endRef || refNum(ref) > refNum(endRef))) endRef = ref; + } + const ranges = viableRanges(turn.nudge?.compressibleRanges ?? []) + .filter((range) => endRef !== undefined && refNum(range.startRef) <= boundary) + .map((range) => ({ startRef: range.startRef, endRef: refNum(range.endRef) > boundary ? endRef! : range.endRef })); rangesRemaining = ranges.length; if (ranges.length === 0) { // #330: nothing foldable outside the soft-protected recent zone. @@ -808,9 +831,8 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa if (budgetHit) break; const skipKey = `${range.startRef}:${range.endRef}`; if (skipSet.has(skipKey)) continue; - const { refToIdx } = refMaps(messages, deps.session.state); - const startIdx = refToIdx.get(range.startRef); - const endIdx = refToIdx.get(range.endRef); + const startIdx = maps.refToIdx.get(range.startRef); + const endIdx = maps.refToIdx.get(range.endRef); if (startIdx === undefined || endIdx === undefined || startIdx > endIdx) { skipSet.add(skipKey); continue; @@ -836,7 +858,6 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa const span = spans.pop(); if (!span) break; const [cs, ce] = span; - const maps = refMaps(messages, deps.session.state); const startRef = maps.idxToRef.get(cs); const endRef = maps.idxToRef.get(ce); if (!startRef || !endRef) continue; @@ -1008,6 +1029,12 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa failure = { kind: "exhausted", detail: `the compress budget was exhausted after ${MAX_PREFLIGHT_ROUNDS} rounds${unusableNote}` }; } } + result.fitsWindow = baselineKnown ? result.payloadEstimate < limit : finalUpper < limit; + if (result.fitsWindow) { + delete deps.session.metadata.preflightPin; + } else if (failure && failure.kind !== "aborted" && watermark) { + deps.session.metadata.preflightPin = { endRef: watermark }; + } if (result.compressedRanges > 0) { // #857: never persist the IMAGE FLOOR into the usage baseline — images // are billed by the upstream and every fit/clamp gate adds their @@ -1024,6 +1051,5 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa result.rangesRemaining = rangesRemaining; result.savedTokens = Math.max(0, startTokens - currentTokens); if (currentTokens >= limit) result.failure = failure; - result.fitsWindow = baselineKnown ? result.payloadEstimate < limit : finalUpper < limit; return result; } diff --git a/src/server.ts b/src/server.ts index 5fa4e0e423..adba193a0a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -77,7 +77,7 @@ import { emitPreflightError, emitStreamError } from "./stream-error.js"; import { affinityToken, claudeSubagentAgentId, claudeSubagentSplit, clientConversationHeader, codexTurnIdentity, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js"; import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; import { flushPrefixAffinity, hydratePrefixAffinity, scheduleAffinityPersist } from "./affinity-persist.js"; -import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginRegister, handlePluginRuntimeInfo, handlePluginStatus, handlePluginTool, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginRuntimeInfoFor, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; +import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginRegister, handlePluginRuntimeInfo, handlePluginStatus, handlePluginTool, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginRuntimeInfoFor, recordPluginSession, registeredPluginSessionId, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream, getBlindTunnelStats } from "./mitm.js"; import type { BiliMessage } from "acp-kernel/wire"; import { BILI_PLUGIN_BYPASS_HEADER, hardenOpenaiAssistantContent, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, systemToUser, usageTotals, type WireProtocol } from "./util.js"; @@ -1187,29 +1187,29 @@ async function handle( parsed as { prompt_cache_key?: unknown }, ) : undefined; - const conversation = protocol === "anthropic" - ? // #970: a subagent's conversation value gets its own - // `|sub:` namespace so it lands on its own session - // (own lock chain, own compression state) instead of queueing - // behind the main turn. The identity itself is NOT rewritten: - // affinityToken/clientLabel below keep consuming the raw value - // so upstream prefix caches and the UI label stay continuous - // across main and subagent sessions. - (claudeSub !== undefined && opts.subagentSplit !== false - ? claudeSubagentSplit(anthropicIdentity?.value ?? anthropicSignal, req.headers, systemTextsForSplit) - : anthropicIdentity?.value ?? anthropicSignal) - : protocol === "openai" - ? openaiIdentity?.value ?? openaiSignal - : codexTurn - // Trusted Codex turn id enters the verbatim session chain - // directly — do NOT route it through subagentNamespace (the - // kernel's empty-instructions non-anchoring path is left - // untouched for metadata-less clients). - ? codexTurn.value - : subagentNamespace( - responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), - (parsed as ResponsesRequestBody).instructions, - ); + const bodyIdentity = responsesIdentity ?? openaiIdentity ?? anthropicIdentity; + const registeredSessionId = !codexTurn && bodyIdentity?.clientProvided + ? registeredPluginSessionId(bodyIdentity.value) + : undefined; + const conversation = codexTurn?.value ?? registeredSessionId ?? ( + protocol === "anthropic" + ? // #970: a subagent's conversation value gets its own + // `|sub:` namespace so it lands on its own session + // (own lock chain, own compression state) instead of queueing + // behind the main turn. The identity itself is NOT rewritten: + // affinityToken/clientLabel below keep consuming the raw value + // so upstream prefix caches and the UI label stay continuous + // across main and subagent sessions. + (claudeSub !== undefined && opts.subagentSplit !== false + ? claudeSubagentSplit(anthropicIdentity?.value ?? anthropicSignal, req.headers, systemTextsForSplit) + : anthropicIdentity?.value ?? anthropicSignal) + : protocol === "openai" + ? openaiIdentity?.value ?? openaiSignal + : subagentNamespace( + responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), + (parsed as ResponsesRequestBody).instructions, + ) + ); // The session ID is the client-provided conversation value VERBATIM — // no hash, no protocol/credential/upstream dimensions (#286): those // are all mutable mid-conversation (bearer rotation, relay switching, @@ -1267,7 +1267,6 @@ async function handle( // body.session_id) — never the synthetic one — so a user can tell // at a glance which client owns a session. pi sends nothing, so its // label stays empty (shown as "—" in the UI). - const bodyIdentity = responsesIdentity ?? openaiIdentity ?? anthropicIdentity; const affinity = affinityToken(bodyIdentity ?? { value: clientConv ?? conversation, source: clientConv ? "header" : "generated", @@ -1297,10 +1296,11 @@ async function handle( // race-free. Fall back to the headless pending queue (codex spawn) // for the first request that creates a new session. if (!pluginAgent && !anonAffinity) { - const identityAgent = consumePluginRegisterFor(clientConv ?? conversation); + const identityConversation = registeredSessionId ? bodyIdentity!.value : clientConv ?? conversation; + const identityAgent = consumePluginRegisterFor(identityConversation); if (identityAgent) { pluginAgent = identityAgent; - pluginConversation = clientConv ?? conversation; + pluginConversation = identityConversation; } } if (!pluginAgent && session.stats.requests === 0 && codexTurnIdentity(req.headers) === undefined && claudeSub === undefined) { @@ -1316,7 +1316,7 @@ async function handle( } } if (!pluginAgent && typeof session.metadata.pluginAgent === "string") pluginAgent = session.metadata.pluginAgent; - if (pluginAgent && !pluginConversation) pluginConversation = conversation; + if (pluginAgent && !pluginConversation) pluginConversation = registeredSessionId ? bodyIdentity!.value : conversation; if (pluginAgent) { if (session.metadata.pluginAgent !== pluginAgent) session.metadata.pluginAgent = pluginAgent; // #970: for a split subagent session, record it under its split @@ -1810,6 +1810,39 @@ export function stripKernelSummaries(messages: BiliMessage[], state: Compression return messages.filter((m) => !(m.id ?? "").startsWith("acp_summary_") || !carried.has(m.id)); } +// compress-retry cleanup: a failed compress call is dead weight once its turn +// has closed — the model either retried (the success replaces it) or moved on, +// and stale failure text under sustained pressure teaches the wrong lesson. +// Drop compress call+result PAIRS from closed turns (before the last user +// message) whose result is a failure marker. Pairs in the CURRENT turn stay so +// the model can still read the error and retry in place; successful compress +// calls never match (no block was created) and remain as plugin-mode carriers. +export function stripFailedCompressCalls(messages: CoreMessage[]): CoreMessage[] { + let lastUser = -1; + for (let i = messages.length - 1; i >= 0; i--) { + if (messages[i]!.role === "user") { + lastUser = i; + break; + } + } + if (lastUser < 0) return messages; + const limit = lastUser; + const compressCallIds = new Set(); + const failed = new Set(); + for (let i = 0; i < limit; i++) { + const m = messages[i]!; + if (m.contentType === "tool-call" && m.toolName === COMPRESS_TOOL_NAME && m.toolCallId) compressCallIds.add(m.toolCallId); + } + for (let i = 0; i < limit; i++) { + const m = messages[i]!; + const t = m.text ?? ""; + if (m.contentType !== "tool-result" || !m.toolCallId || !compressCallIds.has(m.toolCallId)) continue; + if (t.includes("Compression FAILED") || t.includes('Validation failed for tool "compress"')) failed.add(m.toolCallId); + } + if (failed.size === 0) return messages; + return messages.filter((m) => !m.toolCallId || !failed.has(m.toolCallId)); +} + // #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 @@ -2017,7 +2050,7 @@ function prepareAnthropic( 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)); + processedMessages = stripReasoning(stripFailedCompressCalls(stripKernelSummaries(turn.messages, turn.state))); applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltMessages = coreToAnthropic(processedMessages as BiliMessage[], cacheControls); @@ -2154,7 +2187,7 @@ function prepareOpenai( 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)); + processedMessages = stripReasoning(stripFailedCompressCalls(stripKernelSummaries(turn.messages, turn.state))); applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltMessages = systemToUser(hardenOpenaiAssistantContent(coreToOpenai(processedMessages as BiliMessage[]))); @@ -2358,7 +2391,7 @@ function prepareResponses( 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); + processedMessages = repairResponsesAssistantOrdering(stripReasoning(stripFailedCompressCalls(stripKernelSummaries(turn.messages, turn.state))), originalMessages); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltInput = patchResponsesInput(projection, processedMessages); if (Array.isArray(rebuiltInput)) rebuiltInput = hoistTrappedToolItems(rebuiltInput); @@ -3018,6 +3051,15 @@ async function preflightCompressIfNeeded( : Math.max(session.stats.lastInputTokens, payloadEstimate); if (limit <= 0 || !model || tokenCount < compressionTarget) return prepared; const payloadFitsWindow = (unknownBaseline ? tokenCount : payloadEstimate) < limit; + // #739: A stale measured baseline (e.g. an earlier as-is forward under + // preflight, #300) can pin session.stats.lastInputTokens above the window + // while the prepared payload itself fits. Running the walk then burns + // summarization calls folding ranges the payload no longer needs, and + // can fail-fast 502 a payload that would have forwarded fine (#301: + // forwarding as-is is safe when the payload's own estimate fits; the + // usage report self-heals the baseline). Keep folding inside the window: + // the codex headroom target (#300) legitimately walks fitting payloads. + if (!unknownBaseline && session.stats.lastInputTokens >= limit && payloadFitsWindow) return prepared; // #496 forward-once-then-learn: the default image cost (base64/4) matches byte // relays (#488) but overestimates pixel-tile upstreams (a 400KB JPEG ≈ 1.6K real // tokens, not ~133K), so an image-dominated payload can clear the window on ESTIMATE diff --git a/src/stream.ts b/src/stream.ts index 0c2f410548..8c09b634f7 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -49,18 +49,34 @@ function executeAnthropicProxyTool(toolName: string, args: Record Math.max(m, r.startsWith("m") ? Number(r.slice(1)) || 0 : 0), 0); + const boundary = state.blocks.reduce((m, b) => (b.endRef?.startsWith("m") ? Math.max(m, Number(b.endRef.slice(1)) || 0) : m), 0); + const fmt = (n: number) => `m${String(n).padStart(5, "0")}`; + if (highest <= boundary) return ` No raw refs are directly compressible right now — compress a run of ACTIVE blocks instead (e.g. startId b12, endId b15).`; + return ` Live compressible refs: ${fmt(boundary + 1)}–${fmt(highest)} (everything up to ${fmt(boundary)} is already inside active blocks). Retry NOW in this same turn with startId/endId inside that span.`; +} + + export function applyRanges(parsed: ReturnType, ctx: RewriteCtx): string { const { ranges, diagnostics } = parsed; if (ranges.length === 0) { ctx.log("[acp-proxy: compress call had no valid ranges; nothing compressed.]"); const reasons = diagnostics.invalidReasons?.slice(0, 8).map((r) => (r.length > 200 ? r.slice(0, 200) + "..." : r)) ?? []; const why = reasons.length > 0 ? ` Rejected entries:\n${reasons.map((r) => `- ${r}`).join("\n")}` : ""; - return `[Compression FAILED: no valid ranges parsed (kind=${diagnostics.kind}, dropped=${diagnostics.invalidItems}).${why}\n compress requires a non-empty 'content' array of {startId, endId, summary} ranges, where startId/endId are mNNNNN message refs from the conversation. Re-issue the compress call with a valid content array.]`; + return `[Compression FAILED: no valid ranges parsed (kind=${diagnostics.kind}, dropped=${diagnostics.invalidItems}). NOTHING was compressed — the nudge is still pending.${why}\ncompress requires a non-empty 'content' array: compress({ content: [{ startId: "m00123", endId: "m00180", summary: "one short line per range" }] }). startId/endId are mNNNNN message refs from the conversation; get the current ranges from the nudge or acp_status.${compressibleSpanHint(ctx.session.state)} Retry NOW in this same turn — write your own summary text for each range, do not skip the compression.]`; } ctx.log(`[acp-proxy: compress requested ${ranges.length} range(s): ${ranges.map((r) => `${r.startRef}–${r.endRef}`).join(", ")}]`); ctx.log(`[acp-proxy: ctx has ${ctx.messages.length} message(s), state has ${ctx.session.state.messageRefs?.byRef?.size ?? "?"} ref(s) mapped]`); @@ -104,7 +120,7 @@ export function applyRanges(parsed: ReturnType, ctx: if (r.blocksCreated === 0) { const errs = r.errors.join("; ") || "no blocks created"; ctx.log(`[acp-proxy: compress FAILED ${detail} → 0 blocks. ${errs}]`); - return `[Compression FAILED: ${errs}]`; + return `[Compression FAILED: ${errs}${compressibleSpanHint(ctx.session.state)}]`; } // #189 observability: record the rewrite magnitude + fold point so a diff --git a/tests/compress-observability.test.ts b/tests/compress-observability.test.ts index 053a134f39..be2c1d44a9 100644 --- a/tests/compress-observability.test.ts +++ b/tests/compress-observability.test.ts @@ -116,7 +116,7 @@ test("#349: empty compress args → actionable no-valid-ranges message (missing- const out = runApply(ctx, {}); assert.ok(out.startsWith("[Compression FAILED"), `expected failure, got: ${out}`); assert.ok(out.includes("non-empty 'content' array"), `steers to a content array: ${out}`); - assert.ok(out.includes("startId, endId, summary"), `names the required fields: ${out}`); + assert.ok(["startId", "endId", "summary"].every((f) => out.includes(f)), `names the required fields: ${out}`); assert.ok(!out.includes("Check your startId/endId parameters"), "old misleading hint removed"); }); diff --git a/tests/compress-retry.test.ts b/tests/compress-retry.test.ts new file mode 100644 index 0000000000..f4ed7bf086 --- /dev/null +++ b/tests/compress-retry.test.ts @@ -0,0 +1,105 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import type { CompressionState, CoreMessage } from "acp-kernel"; +import { + BILI_ACP_TOOLS_ANTHROPIC, + BILI_ACP_TOOLS_OPENAI, + BILI_ACP_TOOLS_RESPONSES, + BILI_COMPRESS_TOOL, + BILI_COMPRESS_TOOL_OPENAI, + BILI_COMPRESS_TOOL_RESPONSES, + COMPRESS_TOOL_NAME, +} from "../src/compress-tool.ts"; +import { stripFailedCompressCalls } from "../src/server.ts"; +import { compressibleSpanHint } from "../src/stream.ts"; + +// #743: hosts pre-validate tool args against the served schema and reject a +// missing `content` CLIENT-side, showing the model a bare "content: is +// required" it gives up on instead of retrying. The softened schema routes +// those calls to the executor's rich retry receipt instead. +test("#743: compress schema no longer requires content on any surface", () => { + for (const schema of [BILI_COMPRESS_TOOL.input_schema, BILI_COMPRESS_TOOL_OPENAI.function.parameters, BILI_COMPRESS_TOOL_RESPONSES.parameters]) { + assert.ok(!(schema.required ?? []).includes("content"), `content must not be schema-required: ${JSON.stringify(schema.required)}`); + } + const anthropic = BILI_ACP_TOOLS_ANTHROPIC.find((t) => t.name === COMPRESS_TOOL_NAME); + const openai = BILI_ACP_TOOLS_OPENAI.find((t) => t.function.name === COMPRESS_TOOL_NAME); + const responses = BILI_ACP_TOOLS_RESPONSES.find((t) => t.name === COMPRESS_TOOL_NAME); + assert.ok(anthropic && openai && responses, "compress tool present on all three surfaces"); + for (const schema of [anthropic!.input_schema, openai!.function.parameters, responses!.parameters]) { + assert.ok(!(schema.required ?? []).includes("content")); + } +}); + +test("#743: compress description tells the model how to recover a rejected call", () => { + for (const description of [BILI_COMPRESS_TOOL.description, BILI_COMPRESS_TOOL_OPENAI.function.description, BILI_COMPRESS_TOOL_RESPONSES.description]) { + assert.ok(description!.includes("retry"), "description must point at retry"); + } +}); + +let seq = 0; +function msg(m: Omit): CoreMessage { + return { id: `m${++seq}`, ...m }; +} + +const FAILED = "[Compression FAILED: no valid ranges parsed. NOTHING was compressed — the nudge is still pending.]"; +const clientRejected = 'Validation failed for tool "compress": content: is required'; +const okCall = (toolCallId: string) => msg({ role: "assistant", contentType: "tool-call", toolName: COMPRESS_TOOL_NAME, toolCallId }); +const failedResult = (toolCallId: string) => msg({ role: "tool", contentType: "tool-result", toolCallId, text: FAILED }); +const successResult = (toolCallId: string) => msg({ role: "tool", contentType: "tool-result", toolCallId, text: "[Compressed m00002-m00007 -> 1 block(s), ~6459 tokens saved]" }); +const user = (text: string) => msg({ role: "user", contentType: "text", text }); + +test("#743: failed compress pair in a closed turn is dropped", () => { + const messages = [user("investigate"), okCall("tc1"), failedResult("tc1"), user("continue")]; + const out = stripFailedCompressCalls(messages); + assert.equal(out.length, 2); + assert.ok(!out.some((m) => m.toolCallId === "tc1")); +}); + +test("#743: failed compress pair in the current turn is kept for in-place retry", () => { + const messages = [user("investigate"), okCall("tc1"), failedResult("tc1"), user("continue"), okCall("tc2"), failedResult("tc2")]; + const out = stripFailedCompressCalls(messages); + assert.equal(out.length, 4); + assert.ok(out.some((m) => m.toolCallId === "tc2"), "current-turn pair survives"); + assert.ok(!out.some((m) => m.toolCallId === "tc1"), "closed-turn pair dropped"); +}); + +test("#743: client-side validation failures are stripped too", () => { + const messages = [user("investigate"), okCall("tc1"), msg({ role: "tool", contentType: "tool-result", toolCallId: "tc1", text: clientRejected }), user("continue")]; + const out = stripFailedCompressCalls(messages); + assert.equal(out.length, 2); +}); + +test("#743: successful compress calls are never stripped", () => { + const messages = [user("investigate"), okCall("tc1"), successResult("tc1"), user("continue")]; + const out = stripFailedCompressCalls(messages); + assert.equal(out.length, 4); +}); + +test("#743: non-compress tools with failure-looking text are untouched", () => { + const messages = [ + user("investigate"), + msg({ role: "assistant", contentType: "tool-call", toolName: "bash", toolCallId: "tc1" }), + msg({ role: "tool", contentType: "tool-result", toolCallId: "tc1", text: FAILED }), + user("continue"), + ]; + const out = stripFailedCompressCalls(messages); + assert.equal(out.length, 4); +}); + +test("#743: history without a user message is returned unchanged", () => { + const messages = [okCall("tc1"), failedResult("tc1")]; + const out = stripFailedCompressCalls(messages); + assert.equal(out.length, 2); +}); + +// 01a0b0c4 (2026-09-18): every failed compress receipt must name the live +// compressible span, so a model that anchors on refs an earlier fold consumed +// recovers in one retry instead of loop-failing and giving up. +test("failed compress receipts carry the live compressible span", () => { + const state = (refs: string[], blockEnds: (string | undefined)[]): CompressionState => + ({ messageRefs: { byRef: Object.fromEntries(refs.map((r) => [r, r])), byRaw: {} }, blocks: blockEnds.map((endRef, i) => ({ endRef, active: true, blockId: `b${i}` })) }) as CompressionState; + const hint = compressibleSpanHint(state(["m05400", "m05448", "b104"], ["m05372"])); + assert.ok(hint.includes("m05373–m05448"), `hint names the span past the block boundary: ${hint}`); + const exhausted = compressibleSpanHint(state(["m05372"], ["m05372"])); + assert.ok(exhausted.includes("compress a run of ACTIVE blocks"), `hint falls back to block compression: ${exhausted}`); +}); diff --git a/tests/preflight-fail-fast.test.ts b/tests/preflight-fail-fast.test.ts index 81e4dc50da..8edea6c1fa 100644 --- a/tests/preflight-fail-fast.test.ts +++ b/tests/preflight-fail-fast.test.ts @@ -147,7 +147,7 @@ test("e2e #301: overflow + summary upstream 429 → structured 503, over-window } }); -test("e2e #301: payload fits the window + preflight 429 → request still forwarded (no false positive)", async () => { +test("e2e #301: payload fits the window + stale floor above it → forwarded, no summarization attempt", async () => { const calls: Call[] = []; let streamCalls = 0; const upstream = http.createServer((req, res) => { @@ -200,10 +200,10 @@ test("e2e #301: payload fits the window + preflight 429 → request still forwar headers, body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages: bigConversation() }), }); - assert.equal(r2.status, 200, "a fitting payload is forwarded even when preflight 429s"); + assert.equal(r2.status, 200, "a fitting payload is forwarded without a summarization attempt"); await r2.text(); - assert.ok(calls.filter((c) => !c.stream).length >= 1, "preflight did attempt the (429'd) summarization call"); + assert.equal(calls.filter((c) => !c.stream).length, 0, "preflight skips the walk entirely: the stale floor fits nothing to fold"); assert.equal(calls.filter((c) => c.stream).length, 2, "both requests were forwarded upstream"); } finally { proxy.close(); @@ -355,3 +355,84 @@ test("e2e #470: system + tools overhead counts in the preflight trigger — text await once(upstream, "close"); } }); + +// A retry must advance through the remaining original history, including its +// soft-protected tail, without folding turns appended after the failure. +test("e2e #739: failed preflight resumes within its watermark without repeating completed chunks", async () => { + const calls: Call[] = []; + // Summarizer: first (non-stream) call 429s — request 1's preflight fails + // and pins. Later summarizer calls succeed — request 2's pinned retry. + let summaryCalls = 0; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + let parsed: { stream?: boolean } = {}; + try { parsed = JSON.parse(raw); } catch { /* keep {} */ } + calls.push({ stream: !!parsed.stream, body: raw }); + if (parsed.stream) { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse(1000)); + } else { + summaryCalls++; + if (summaryCalls === 1) { + res.writeHead(429, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: "rate limited", type: "rate_limit_error" } })); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ content: [{ type: "text", text: "SUMMARY: the large recent user message was a deterministic load-growth payload; its raw content is no longer needed." }] })); + } + } + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + + const proxy = await startProxy(upstreamPort, { "claude-small": { context: 10_000 }, "claude-smaller": { context: 8_000 } }, { preserveRecentMessages: 10 }); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + + try { + // Request 1: ~13k history vs a 10k window — preflight fires, its + // summarization call 429s, the proxy fails fast (and pins). + const r1 = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "preflight-pin-sess" }, + body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages: bigConversation() }), + }); + assert.equal(r1.status, 503, "request 1 fails fast when the summary upstream rate-limits"); + assert.equal(calls.filter((c) => c.stream).length, 0, "request 1 forwarded nothing"); + assert.ok(calls.filter((c) => !c.stream).length >= 1, "request 1 attempted the (429'd) summarization call"); + + // Request 2: same session, history GREW during the failure window (a + // new tail turn). A different model dodges the #301 dead-end cooldown + // (same model+window would fail fast on the cached diagnosis without + // reaching the pinned retry) — simulating the post-cooldown retry. + const grown = [...bigConversation(), { role: "user", content: `FAILURE_WINDOW_NOISE ${"noise_".repeat(200)}` }]; + const r2 = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "preflight-pin-sess" }, + body: JSON.stringify({ model: "claude-smaller", max_tokens: 1024, stream: true, messages: grown }), + }); + assert.equal(r2.status, 200, "request 2's pinned retry compresses and forwards"); + const summarizers = calls.filter((c) => !c.stream); + assert.ok(summarizers.length >= 2, "request 2 ran the pinned summarization call"); + const successful = summarizers.slice(1); + assert.ok(successful.some((c) => c.body.includes("Message 7 of the long conversation.")), "retry reaches the original soft-protected history"); + for (let i = 0; i < 12; i++) { + assert.ok(successful.filter((c) => c.body.includes(`Message ${i} of the long conversation.`)).length <= 1, `message ${i} is not summarized again after a successful fold`); + } + const firstRetry = summarizers[1]?.body ?? ""; + assert.ok(firstRetry.includes("Message 0 of the long conversation."), "the retry compresses the pinned (original) range"); + assert.ok(summarizers.every((c) => !c.body.includes("FAILURE_WINDOW_NOISE")), "the failure-window turn stayed OUT of every compression input"); + assert.equal(calls.filter((c) => c.stream).length, 1, "request 2's folded payload was forwarded upstream"); + assert.ok(calls.find((c) => c.stream)!.body.includes("FAILURE_WINDOW_NOISE"), "the appended user turn remains visible rather than being lost"); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); diff --git a/tests/search-context-cross-session.test.ts b/tests/search-context-cross-session.test.ts index c85fc19120..3e2756ee37 100644 --- a/tests/search-context-cross-session.test.ts +++ b/tests/search-context-cross-session.test.ts @@ -6,7 +6,7 @@ import { tmpdir } from "node:os"; import { createHash } from "node:crypto"; import { ACP_READONLY_TOOLS_RESPONSES, ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, createCore, createInitialState, defaultConfig } from "acp-kernel"; import { anthropicToCore, type AnthropicRequestBody } from "acp-kernel/wire"; -import { BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES } from "../src/compress-tool.ts"; +import { BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, COMPRESS_TOOL_NAME } from "../src/compress-tool.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _resetSessionsForTest, getSession, type Session } from "../src/session.ts"; import { executeSearchContext, executeSearchContextTarget } from "../src/decompress-shared.ts"; @@ -78,8 +78,15 @@ test("#841 schema: BILI arrays add optional conversation_id to search_context on const kernelProps = paramsOf(kernelEntry!).properties as Record; assert.equal(kernelProps.conversation_id, undefined, "kernel constant must not be mutated"); - const biliRest = bili.filter((t) => t !== entry); - const kernelRest = kernel.filter((t) => t !== kernelEntry); + // #743: compress is the one other intentional BILI-vs-kernel delta + // (softened required + retry note); its schema is pinned in + // tests/compress-retry.test.ts. + const nameOf = (t: unknown): string | undefined => { + const e = t as FlatTool; + return shape === "openai" ? e.function?.name : e.name; + }; + const biliRest = bili.filter((t) => t !== entry && nameOf(t) !== COMPRESS_TOOL_NAME); + const kernelRest = kernel.filter((t) => t !== kernelEntry && nameOf(t) !== COMPRESS_TOOL_NAME); assert.deepEqual(biliRest, kernelRest, "no other tool may change"); } const ro = searchEntry(BILI_ACP_READONLY_TOOLS_RESPONSES, "flat")!; diff --git a/tests/subagent-namespace.test.ts b/tests/subagent-namespace.test.ts index 44b2832996..9201d20473 100644 --- a/tests/subagent-namespace.test.ts +++ b/tests/subagent-namespace.test.ts @@ -2,12 +2,19 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; -import { defaultConfig } from "acp-kernel"; +import { createCore, defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; -import { subagentNamespace, deriveMessageId } from "acp-kernel/wire"; +import { subagentNamespace, deriveMessageId, responsesToCore } from "acp-kernel/wire"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { getSession, snapshotMessages, _resetSessionsForTest, flushAllSessions } from "../src/session.ts"; +import { recordPluginSession, flushConversations, loadConversations, _resetPluginStateForTest } from "../src/plugin.ts"; +import { applyRanges } from "../src/stream.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; function listen(server: http.Server): Promise { if (server.listening) return Promise.resolve(); @@ -132,6 +139,7 @@ test("e2e #150: guardian subagent request bypasses the main session's compressio model: "gpt-guard-e2e", stream: true, session_id: SESSION_ID, + prompt_cache_key: SESSION_ID, instructions: "You are Codex, the main coding agent.", input: [ { type: "message", role: "user", content: MAIN_TURN_1 }, @@ -148,6 +156,7 @@ test("e2e #150: guardian subagent request bypasses the main session's compressio model: "gpt-guard-e2e", stream: true, session_id: SESSION_ID, + prompt_cache_key: SESSION_ID, instructions: "You are Guardian, the Codex approval reviewer. Evaluate the proposed action against the user authorization.", input: [ { type: "message", role: "user", content: "Approval request: exec command proposed by the main agent." }, @@ -186,3 +195,130 @@ test("e2e #150: guardian subagent request bypasses the main session's compressio await close(upstream); } }); + +test("e2e #739: registered omp restores mapped blocks from disk across instructions drift", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-resume-")); + const previousStateHome = process.env.XDG_STATE_HOME; + process.env.XDG_STATE_HOME = root; + const store = new SessionStore({ dir: path.join(root, "sessions"), enabled: true, debounceMs: 0 }); + _setStoreForTest(store); + _resetSessionsForTest(); + _resetPluginStateForTest(); + setRegistryForTest({}); + + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + bodies.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + res.write(textEvents("ok")); + res.write(completed(120)); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upstreamAddr = upstream.address(); + if (upstreamAddr === null || typeof upstreamAddr === "string") throw new Error("upstream did not listen on tcp"); + const upstreamPort = upstreamAddr.port; + + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { + [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-reg-e2e": { context: 400_000 } } }, + }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000, { preserveRecentTokens: 0 }), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await listen(proxy); + const proxyAddr = proxy.address(); + if (proxyAddr === null || typeof proxyAddr === "string") throw new Error("proxy did not listen on tcp"); + const proxyPort = proxyAddr.port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; + const SESSION_ID = "e2e-launcher-sess"; + const LEGACY_ID = `${SESSION_ID}|sub:legacy`; + const bodyWith = (instructions: string) => ({ + model: "gpt-reg-e2e", + stream: true, + session_id: SESSION_ID, + instructions, + input: [ + { type: "message", role: "user", content: "run the task" }, + ...Array.from({ length: 6 }, (_, i) => ({ type: "message", role: i % 2 === 0 ? "assistant" : "user", content: `OLD_RAW_${i} ${"history ".repeat(1000)}` })), + ...Array.from({ length: 5 }, (_, i) => ({ type: "message", role: i % 2 === 0 ? "assistant" : "user", content: `recent turn ${i}` })), + ], + }); + + try { + const reg = await fetch(`http://127.0.0.1:${proxyPort}/__bili/plugin/register`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ conversationId: SESSION_ID, agent: "omp", identity: true }), + }); + assert.equal(reg.status, 200, "launcher registration accepted"); + + const r1 = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(bodyWith("You are Pi, the omp launcher coding agent.")) }); + assert.equal(r1.status, 200); + await r1.text(); + + const legacy = getSession(LEGACY_ID); + legacy.state = getSession(SESSION_ID).state; + legacy.metadata.pluginAgent = "omp"; + const messages = responsesToCore(bodyWith("original instructions")).msgs; + snapshotMessages(legacy, messages); + const compressed = applyRanges(parseCompressInput({ content: [1, 3, 5].map((i) => ({ + startId: legacy.state.messageRefs.byRaw[messages[i]!.id], + endId: legacy.state.messageRefs.byRaw[messages[i + 1]!.id], + summary: `RESTORED_SUMMARY_${i}: historical work was completed and its results remain available in this summary.`, + })) }), { core: createCore(), config: opts.kernelConfig, messages, session: legacy, log: () => {} }); + assert.doesNotMatch(compressed, /FAILED/); + assert.equal(legacy.state.blocks.filter((block) => block.active).length, 3, compressed); + await store.writeNow(legacy); + recordPluginSession(SESSION_ID, LEGACY_ID); + flushConversations(); + _resetSessionsForTest(); + _resetPluginStateForTest(); + loadConversations(); + + // No fresh registration: recover ownership and all three blocks from + // the persisted mapping, including a session absent from memory. + const r2 = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(bodyWith("You are Pi, switched to a different model with a rewritten system prompt.")) }); + assert.equal(r2.status, 200); + await r2.text(); + + const status = await (await fetch(`http://127.0.0.1:${proxyPort}/__bili/plugin/status?conversationId=${SESSION_ID}`)).json(); + assert.equal(status.pluginAgent, "omp"); + assert.equal(status.blocks.filter((block: { active: boolean }) => block.active).length, 3); + assert.doesNotMatch(bodies[1]!, /OLD_RAW_/); + assert.ok(bodies[1]!.length < bodies[0]!.length / 2, "restored blocks shrink the actual upstream payload"); + const stats = await (await fetch(`http://127.0.0.1:${proxyPort}/__bili/stats`)).json(); + const mine = stats.sessions.filter((s: { id: string }) => s.id === SESSION_ID || s.id.startsWith(`${SESSION_ID}|`)); + assert.equal(bodies.length, 2, "two rounds, no extra compress re-requests"); + assert.equal(mine.length, 1, "instructions drift must NOT fork a registered conversation into a second namespace"); + assert.equal(mine[0].id, LEGACY_ID, "resume reuses the mapped state rather than a fresh bare-id session"); + } finally { + await close(proxy); + await close(upstream); + await flushAllSessions(); + flushConversations(); + _resetSessionsForTest(); + _resetPluginStateForTest(); + _setStoreForTest(new SessionStore({ enabled: false })); + if (previousStateHome === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = previousStateHome; + rmSync(root, { recursive: true, force: true }); + } +});