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..d9738cef3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixes + + +- **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/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..889dd17d7c 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 @@ -3018,6 +3018,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/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/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 }); + } +});