Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/degenerate-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
13 changes: 10 additions & 3 deletions src/fetch-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions src/prefix-affinity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion src/reasoning-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ export async function runReasoningGuard(p: GuardParams): Promise<void> {
return;
}
const contBody = roundRes.response.body;
roundRes.clearTimer();
roundRes.stopIdleTimer();
result = contBody ? await consumeRound(contBody, st, p.res) : { error: "no_body" };
continue;
}
Expand Down
9 changes: 5 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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<CoreMessage[]> => {
return withSessionLock(prepared.session, () => {
Expand Down
15 changes: 12 additions & 3 deletions src/server/context-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,24 @@ export function launcherContextWindow(model: string): number | undefined {

export const windowSourceLogged = new Set<string>();

// Security cap on the beta-negotiated window: unbounded, a hostile
// `context-<N>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
* evidence of the window the upstream will actually serve — it outranks the
* 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<string, string | string[] | undefined>): number | undefined {
const raw = headers["anthropic-beta"];
if (raw === undefined) return undefined;
Expand All @@ -81,7 +90,7 @@ export function anthropicBetaContextWindow(headers: Record<string, string | stri
if (!m) continue;
const n = Number.parseInt(m[1], 10);
if (!Number.isFinite(n) || n <= 0) continue;
const w = n * 1_000_000;
const w = Math.min(n * 1_000_000, MAX_ANTHROPIC_BETA_WINDOW);
if (best === undefined || w > best) best = w;
}
return best;
Expand Down
91 changes: 91 additions & 0 deletions tests/fix-15-abort-lifetime.test.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
server.listen(0, "127.0.0.1");
return once(server, "listening").then(() => undefined);
}

function close(server: http.Server): Promise<void> {
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<Uint8Array>).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<Uint8Array>).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);
}
});
5 changes: 5 additions & 0 deletions tests/model-windows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/prefix-affinity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")];
Expand Down
Loading