diff --git a/src/degenerate-retry.ts b/src/degenerate-retry.ts index 1ec7dee40c..dcb2ecb90d 100644 --- a/src/degenerate-retry.ts +++ b/src/degenerate-retry.ts @@ -90,7 +90,7 @@ export function makeContinuationRefetch(opts: ContinuationRetryOpts): () => Prom // owns, so its own idle timer is dropped — a stalled retry then ends // with the client abort instead of an upstream watchdog (the // streamed re-request in reasoning-guard.ts does the same). - r.clearTimer(); + r.stopIdleTimer(); return body; } catch (e) { opts.log("warn", `[${opts.label}] [plugin] degenerate-terminal retry failed (${e instanceof Error ? e.message : String(e)}); passing the empty turn through`); diff --git a/src/fetch-util.ts b/src/fetch-util.ts index 66a21f6994..723de590ec 100644 --- a/src/fetch-util.ts +++ b/src/fetch-util.ts @@ -87,7 +87,7 @@ export async function fetchWithTimeout( opts: FetchOptions, timeoutMs?: number, externalSignal?: AbortSignal, -): Promise<{ response: Response; clearTimer: () => void }> { +): Promise<{ response: Response; clearTimer: () => void; stopIdleTimer: () => void }> { const effective = timeoutMs ?? upstreamTimeoutMs(); const controller = new AbortController(); let cleared = false; @@ -118,10 +118,16 @@ export async function fetchWithTimeout( externalSignal.addEventListener("abort", onExternalAbort, { once: true }); } } - const cleanup = () => { + // Two distinct lifetimes: stopping the idle watchdog must NOT detach the + // client-abort listener — splice/continue callers drop the timer but still + // rely on client disconnect to cancel a stalled upstream (#1064 #15). + const stopIdleTimer = () => { cleared = true; clearTimeout(timer); liveUpstreamTimers.delete(timer); + }; + const cleanup = () => { + stopIdleTimer(); if (onExternalAbort && externalSignal) externalSignal.removeEventListener("abort", onExternalAbort); }; try { @@ -154,9 +160,10 @@ export async function fetchWithTimeout( headers: raw.headers, }), clearTimer: cleanup, + stopIdleTimer, }; } - return { response: raw, clearTimer: cleanup }; + return { response: raw, clearTimer: cleanup, stopIdleTimer }; } catch (e) { cleanup(); throw e; diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index f0caaf3695..202564e77f 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -52,11 +52,6 @@ const TTL_MS = 7 * 24 * 60 * 60 * 1000; * items, so the match offset must be free — not pinned to the stored tail). */ const TAIL_WINDOW = 8; -/** Minimum window size (items) for a tail-window reattach to be trusted. A - * 1-2 item window is too weak a signal (a single shared message is common); - * below this the request falls through to a new session as before. */ -const MIN_TAIL_MATCH = 3; - /** Per tracked chain, store at most this many per-item hashes (the trailing * ones). Bounds memory (256 chains × 128 × 64B ≈ 2MB) and keeps the tail * window (8) comfortably available. Chains deeper than this lose their head, @@ -213,10 +208,13 @@ export class PrefixAffinityResolver { // excluded: a head-to-head match is either a full-prefix case // (step 1) or a distinct conversation sharing a templated head — // never a truncation reattach (the retained suffix of a truncation - // starts strictly inside the stored chain). + // starts strictly inside the stored chain). Adoption requires the + // FULL window (incomingDepth >= TAIL_WINDOW): a sub-8 leading run is + // weak identity evidence and lets a short crafted request adopt an + // unrelated stored session by coincidence (#1064 #13). const w = Math.min(TAIL_WINDOW, incomingDepth); const candidates: ChainEntry[] = []; - if (w >= MIN_TAIL_MATCH) { + if (w === TAIL_WINDOW) { for (const entry of tracked.values()) { const stored = entry.itemHashes; for (let j = 1; j + w <= stored.length; j++) { diff --git a/src/reasoning-guard.ts b/src/reasoning-guard.ts index 5054667c4d..d5ac162133 100644 --- a/src/reasoning-guard.ts +++ b/src/reasoning-guard.ts @@ -542,7 +542,7 @@ export async function runReasoningGuard(p: GuardParams): Promise { return; } const contBody = roundRes.response.body; - roundRes.clearTimer(); + roundRes.stopIdleTimer(); result = contBody ? await consumeRound(contBody, st, p.res) : { error: "no_body" }; continue; } diff --git a/src/server.ts b/src/server.ts index 6b385ba7d2..9a24a43a77 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2288,7 +2288,7 @@ function prepareAnthropic( // blinded to a system+tools-only floor. session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + countSystemAndToolsTokens(extractSystem(systemOut), toolsOut) - + imageTokensInParsedBody("anthropic", rebuilt); + + imageTokensInParsedBody("anthropic", rebuilt, imageBillingFor(opts, upstreamOrigin)); return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, anthropicSystem: parsed.system, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" } as Prepared; } @@ -2473,7 +2473,7 @@ function prepareOpenai( if (!isTitleGen) { session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + countSystemAndToolsTokens(openaiOutboundSystem || openaiSystemText, toolsOut) - + imageTokensInParsedBody("openai", rebuilt); + + imageTokensInParsedBody("openai", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)); } snapshotMessages(session, originalMessages); markDirty(session); @@ -2934,7 +2934,7 @@ function prepareResponses( if (!isCompactionTrigger) { session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + countSystemAndToolsTokens(responsesDevContent ?? "", toolsOut) - + imageTokensInParsedBody("responses", rebuilt); + + imageTokensInParsedBody("responses", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)); } snapshotMessages(session, originalMessages); markDirty(session); @@ -3950,6 +3950,7 @@ async function forward( headers: new Headers(upstreamResult.response.headers), }), clearTimer: upstreamResult.clearTimer, + stopIdleTimer: upstreamResult.stopIdleTimer, }; const rejection = detectRoleRejection(upstreamResult.response.status, roleErrText); if (rejection && rejection.role !== "system") { @@ -4469,7 +4470,7 @@ async function forward( ? `\n\n---\n\n${buildAbsorbSystemPrompt(absorbToolName(loopConfig))}` : ""; const visibilityMarkers = resolveCompress(opts.routes, route?.rewrittenUrl, (parsedReq as { model?: string }).model, opts.compress).visibilityMarkers ?? true; - const systemPrompt = withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections)), visibilityMarkers) + absorbSection; + const systemPrompt = withConversationIdNote(withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections)), visibilityMarkers), ensureCanonicalId(prepared.session)) + absorbSection; const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google); const refreshFolded = async (current: CoreMessage[]): Promise => { return withSessionLock(prepared.session, () => { diff --git a/src/server/context-window.ts b/src/server/context-window.ts index 6df6fc6c04..e8a0f168b1 100644 --- a/src/server/context-window.ts +++ b/src/server/context-window.ts @@ -62,6 +62,14 @@ export function launcherContextWindow(model: string): number | undefined { export const windowSourceLogged = new Set(); +// Security cap on the beta-negotiated window: unbounded, a hostile +// `context-m` header would drive the effective window (and thus every +// compression threshold) toward infinity, disabling all triggers until a real +// 400 stalls the session (#1064 #12). Kept well above any shipping context +// window so future betas still generalize while the header stays bounded +// (#1064 #12). +export const MAX_ANTHROPIC_BETA_WINDOW = 10_000_000; + /** Parse an `anthropic-beta` header for a larger-context beta (e.g. * `context-1m-2025-08-07` → 1,000,000). The beta lets the CLIENT negotiate a * window beyond the model's standard size, so it is the most direct per-request @@ -69,8 +77,9 @@ export const windowSourceLogged = new Set(); * model table / registry (which list the STANDARD window, e.g. 200K for claude) * and must be re-read on every request (the header may appear/disappear between * requests of the same session, #302). `context-Nm` generalizes to future - * larger-context betas (N × 1,000,000). Returns the largest requested window, - * or undefined when no context beta is present. */ + * larger-context betas (N × 1,000,000), clamped to {@link MAX_ANTHROPIC_BETA_WINDOW}. + * Returns the largest requested window, or undefined when no context beta is + * present. */ export function anthropicBetaContextWindow(headers: Record): number | undefined { const raw = headers["anthropic-beta"]; if (raw === undefined) return undefined; @@ -81,7 +90,7 @@ export function anthropicBetaContextWindow(headers: Record best) best = w; } return best; diff --git a/tests/fix-15-abort-lifetime.test.ts b/tests/fix-15-abort-lifetime.test.ts new file mode 100644 index 0000000000..c5241c7a0e --- /dev/null +++ b/tests/fix-15-abort-lifetime.test.ts @@ -0,0 +1,91 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { fetchWithTimeout, _resetFetchUtilForTest, _liveUpstreamTimersForTest } from "../src/fetch-util.ts"; + +function listen(server: http.Server): Promise { + server.listen(0, "127.0.0.1"); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +test("stopIdleTimer drops the idle watchdog but keeps client-abort alive (#1064 #15)", async () => { + // Regression guard for #1064 #15: stopIdleTimer must clear the idle + // watchdog WITHOUT detaching the client-abort listener. splice/continue + // callers (degenerate-retry, reasoning-guard) drop the timer yet still rely + // on downstream disconnect to cancel a stalled upstream; if stopping the + // timer also removed the abort listener, such a stream would have neither + // watchdog nor abort (unbounded upstream billing). Asserts both halves by + // event ordering (no wall-clock timers). + const timeoutMs = 5_000; + _resetFetchUtilForTest(); + const upstream = http.createServer((_req, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.flushHeaders(); + res.write("chunk-1\n"); + // never ends — simulates a stalled upstream + }); + await listen(upstream); + const port = (upstream.address() as { port: number }).port; + try { + const ac = new AbortController(); + const result = await fetchWithTimeout(`http://127.0.0.1:${port}/stuck`, {}, timeoutMs, ac.signal); + assert.equal(_liveUpstreamTimersForTest(), 1, "one idle watchdog armed for the in-flight request"); + const reader = (result.response.body as ReadableStream).getReader(); + const first = await reader.read(); + assert.ok(first.value && first.value.length > 0, "first chunk arrives before the stall"); + let name = ""; + const pendingRead = reader.read().catch((e: unknown) => { + name = e instanceof Error ? e.name : String(e); + return { done: true }; + }); + result.stopIdleTimer(); + assert.equal(_liveUpstreamTimersForTest(), 0, "stopIdleTimer removes the idle watchdog"); + ac.abort(); + await pendingRead; + assert.equal(name, "AbortError", "client abort must still propagate after stopIdleTimer (#1064 #15)"); + reader.releaseLock(); + } finally { + upstream.closeAllConnections(); + await close(upstream); + } +}); + +test("clearTimer detaches the client-abort listener (full cleanup)", async () => { + // Contrast to the case above: clearTimer is the full cleanup used once a + // response is fully consumed/discarded, and it MUST remove the abort + // listener as well as the timer. A bounded window asserts the abort does + // NOT reach the stream (absence), without depending on a stall to surface. + const timeoutMs = 5_000; + _resetFetchUtilForTest(); + const upstream = http.createServer((_req, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.flushHeaders(); + res.write("chunk-1\n"); + }); + await listen(upstream); + const port = (upstream.address() as { port: number }).port; + try { + const ac = new AbortController(); + const result = await fetchWithTimeout(`http://127.0.0.1:${port}/stuck`, {}, timeoutMs, ac.signal); + const reader = (result.response.body as ReadableStream).getReader(); + await reader.read(); + let propagated = false; + const pendingRead = reader.read().catch(() => { + propagated = true; + return { done: true }; + }); + result.clearTimer(); + ac.abort(); + await Promise.race([pendingRead, new Promise((r) => setTimeout(r, 200))]); + assert.equal(propagated, false, "clearTimer must detach the client-abort listener"); + reader.releaseLock(); + } finally { + upstream.closeAllConnections(); + await close(upstream); + } +}); diff --git a/tests/model-windows.test.ts b/tests/model-windows.test.ts index cf80897fea..729e7d4a89 100644 --- a/tests/model-windows.test.ts +++ b/tests/model-windows.test.ts @@ -13,6 +13,7 @@ import { type ClientConfig, } from "../src/client-config.ts"; import { parseLauncherModelWindows, anthropicBetaContextWindow } from "../src/server.ts"; +import { MAX_ANTHROPIC_BETA_WINDOW } from "../src/server/context-window.ts"; test("parseOmpYaml: captures per-model contextWindow (models after baseUrl)", () => { const yml = [ @@ -253,6 +254,10 @@ test("anthropicBetaContextWindow: future larger-context beta generalizes (contex assert.equal(anthropicBetaContextWindow({ "anthropic-beta": "context-1m-2025-08-07, context-2m-2026-01-01" }), 2_000_000); }); +test("anthropicBetaContextWindow: pathological context-Nm clamps to the safety cap (#1064 #12)", () => { + assert.equal(anthropicBetaContextWindow({ "anthropic-beta": "context-9999999m-2030-01-01" }), MAX_ANTHROPIC_BETA_WINDOW); +}); + test("anthropicBetaContextWindow: rejects malformed / non-context tokens", () => { assert.equal(anthropicBetaContextWindow({ "anthropic-beta": "context-m-2025-08-07" }), undefined); assert.equal(anthropicBetaContextWindow({ "anthropic-beta": "context-1x-2025-08-07" }), undefined); diff --git a/tests/prefix-affinity.test.ts b/tests/prefix-affinity.test.ts index f51691b5b3..bafd638ad5 100644 --- a/tests/prefix-affinity.test.ts +++ b/tests/prefix-affinity.test.ts @@ -76,6 +76,20 @@ test("prefix-affinity: trimmed history no longer matches — safe new session", assert.notEqual(b!.sessionId, a!.sessionId); }); +test("prefix-affinity: short crafted window cannot adopt an unrelated stored session (#1064 #13)", () => { + const r = new PrefixAffinityResolver(); + const chain = Array.from({ length: 8 }, (_, i) => user(`stored turn ${i} with enough substance`)); + const a = r.resolve(chain); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); + // Replaying only 3 contiguous middle turns (sub-8 window) used to auto-adopt + // the victim's session via "tail-window"; it must now fall through to a new one. + const crafted = [chain[2]!, chain[3]!, chain[4]!]; + const b = r.resolve(crafted); + assert.ok(b); + assert.notEqual(b.sessionId, a.sessionId, "sub-8 leading run must not adopt the stored session"); + assert.equal(b.matchedDepth, 0); +}); + test("prefix-affinity: no environmental partitioning — same content survives credential/relay rotation (#286)", () => { const r = new PrefixAffinityResolver(); const history = [user("same words across rotating credentials")];