From df7509b9c4e97f5aa2aee67ca47b4478ee8f9854 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 1 Sep 2026 07:51:16 +0800 Subject: [PATCH] fix: resolve relay model windows via registry + panel + variant + bidirectional self-heal (#393) Claude Opus 5 behind a relay (Anthropic /v1/messages, --upstream mode) was served the static-table 200K window instead of its real 1M window. Four independent defects compounded: 1. models.dev lookup was gated on a parsed host that is only present in MITM / /bili/ mode, so plain --upstream relays always fell through to the static table. Removed the host gate: peekRegistryContext and the async contextFromRegistry now run unconditionally (a relay host resolves via the */ suffix scan). 2. The status panel hardcoded a 200K fallback and effectiveContextLimit was only written for plugin sessions, so wire-mode panels always showed 200K. effectiveContextLimit is now recorded in every mode (post self-heal + output-headroom), and the panel shows 0 (honest) instead of a fake 200K. 3. Inference suffixes like -thinking did not match the base model's entry. registryLookup now tries modelVariants (progressive suffix strip) so claude-opus-5-thinking resolves to the base model's 1M window. 4. Self-heal only corrected downward (from overflows). Added an upward path: when a low-confidence fallback window is in effect and a prior successful turn's input exceeded the window it was measured under, raise the window to that input and persist it per-model. Also classify GET /models as a known discovery path so omp's model enumeration no longer logs a warn-level "unrecognized path". --- src/plugin.ts | 2 +- src/registry.ts | 83 +++++++--- src/server.ts | 51 +++++- tests/issue-393-registry-window.test.ts | 210 ++++++++++++++++++++++++ 4 files changed, 318 insertions(+), 28 deletions(-) create mode 100644 tests/issue-393-registry-window.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index 093bdb440e..c5ee625967 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -327,7 +327,7 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt if (entry) entry.lastSeen = Date.now(); const limit = session.metadata.effectiveContextLimit; const mem = remembered.get(session.id); - const modelContextLimit = typeof limit === "number" && limit > 0 ? limit : 200000; + const modelContextLimit = typeof limit === "number" && limit > 0 ? limit : 0; let panel: string | undefined; try { panel = buildStatusPanel({ diff --git a/src/registry.ts b/src/registry.ts index 64cdd2cf3d..1811a9fe00 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -252,33 +252,70 @@ export function bundledSnapshotLookup(model: string, host?: string): number | un return registryLookup(bundledSnapshotRegistry(), model, host); } +// Inference-mode suffixes denoting the SAME base model under a different +// reasoning setting (a "-thinking" variant shares the base model's context +// window). Longest-first so "-interleaved-thinking" wins over "-thinking". +const VARIANT_SUFFIXES = [ + "-interleaved-thinking", + "-thinking", + "-reasoning", + "-extended", + "-fast", + "-high", + "-medium", + "-low", +]; + +export function modelVariants(name: string): string[] { + const variants: string[] = [name]; + let current = name; + for (;;) { + let stripped = false; + for (const suffix of VARIANT_SUFFIXES) { + if (current.length > suffix.length && current.endsWith(suffix)) { + current = current.slice(0, -suffix.length); + variants.push(current); + stripped = true; + break; + } + } + if (!stripped) break; + } + return variants; +} + function registryLookup(reg: RegistryShape | null, model: string, host?: string): number | undefined { if (!reg || !model) return undefined; const provider = host ? providerFromHost(host) : undefined; - const candidates = provider ? [`${provider}/${model}`, model] : [model]; - for (const key of candidates) { - const entry = reg[key]; - const ctx = entry?.limit?.context; - if (typeof ctx === "number" && ctx > 0) return ctx; - } - // Relay host (not in HOST_TO_PROVIDER): the bare name can miss while the - // model exists under a provider-prefixed key (a relay serving - // "deepseek-v4-flash" is stored as "deepseek/deepseek-v4-flash"). Scan - // */ and adopt the value only when every match agrees — a - // conflict means different deployments, and guessing is worse than the - // static table. Known-provider hosts keep the old behavior: a miss there - // means the model is genuinely unlisted for that provider. - if (provider === undefined) { - const suffix = `/${model}`; - let agreed: number | undefined; - for (const key of Object.keys(reg)) { - if (!key.endsWith(suffix)) continue; - const ctx = reg[key].limit?.context; - if (typeof ctx !== "number" || ctx <= 0) continue; - if (agreed === undefined) agreed = ctx; - else if (agreed !== ctx) return undefined; + for (const name of modelVariants(model)) { + const candidates = provider ? [`${provider}/${name}`, name] : [name]; + for (const key of candidates) { + const entry = reg[key]; + const ctx = entry?.limit?.context; + if (typeof ctx === "number" && ctx > 0) return ctx; + } + // Relay host (not in HOST_TO_PROVIDER): the bare name can miss while + // the model exists under a provider-prefixed key (a relay serving + // "deepseek-v4-flash" is stored as "deepseek/deepseek-v4-flash"). Scan + // */ and adopt the value only when every match agrees — a + // conflict means different deployments, and guessing is worse than the + // static table. A conflicting/missing original name falls through to + // the next (stripped) variant. Known-provider hosts keep the old + // behavior: a miss there means the model is genuinely unlisted for + // that provider. + if (provider === undefined) { + const suffix = `/${name}`; + let agreed: number | undefined; + let conflicted = false; + for (const key of Object.keys(reg)) { + if (!key.endsWith(suffix)) continue; + const ctx = reg[key].limit?.context; + if (typeof ctx !== "number" || ctx <= 0) continue; + if (agreed === undefined) agreed = ctx; + else if (agreed !== ctx) { conflicted = true; break; } + } + if (!conflicted && agreed !== undefined) return agreed; } - return agreed; } return undefined; } diff --git a/src/server.ts b/src/server.ts index 351ea5f517..6fe4f93971 100644 --- a/src/server.ts +++ b/src/server.ts @@ -111,6 +111,13 @@ export function logUnrecognizedPath(log: (level: string, msg: string) => void, u } } +// Model-enumeration endpoints clients probe at startup (omp's openai-models-list +// discovery). Expected passthroughs, not unknown protocols — #393: exempt from +// the warn-level "unrecognized path" log. +function isModelDiscoveryPath(path: string): boolean { + return path.replace(/\/+$/, "").endsWith("/models"); +} + // #300: bili→bili chain marker. When a bili instance forwards a request it has // processed upstream, it stamps this header with its own instance id. A bili // instance that RECEIVES a request already carrying it knows an upstream bili @@ -833,7 +840,7 @@ async function handle( const pluginWindow = pluginReportedContextWindow(req.headers); const launcherWindow = launcherContextWindow(model); const configuredWindow = resolveConfiguredContextLimit(opts.routes, embeddedUrl, model); - const peekWindow = host ? peekRegistryContext(model, host) : undefined; + const peekWindow = peekRegistryContext(model, host); let native = betaWindow ?? pluginWindow ?? launcherWindow @@ -847,7 +854,7 @@ async function handle( // negotiation), so it also clears the fallback flag. const operatorWindowTuned = resolveCompress(opts.routes, embeddedUrl, model, opts.compress).modelContextLimit !== undefined; nativeFromFallback = !betaWindow && !pluginWindow && !launcherWindow && !peekWindow && !configuredWindow && !operatorWindowTuned; - if (!native && host) { + if (!native) { native = await contextFromRegistry(model, host); if (native) nativeFromFallback = false; } @@ -1074,7 +1081,6 @@ async function handle( if (pluginAgent && !pluginConversation) pluginConversation = conversation; if (pluginAgent) { if (session.metadata.pluginAgent !== pluginAgent) session.metadata.pluginAgent = pluginAgent; - session.metadata.effectiveContextLimit = reqConfig.modelContextLimit; recordPluginSession(pluginConversation ?? conversation, session.id); } // Responses, OpenAI-chat AND Anthropic-wire clients that send their @@ -1115,6 +1121,33 @@ async function handle( // not be floored back up (that would undo the self-heal). nativeFromFallback = false; log("info", `[${session.id}] self-healed context window: ${resolved} → ${learnedLimit} (learned from an upstream overflow)`); + } else if (nativeFromFallback && reqModel) { + // Self-heal UPWARD — complement of the overflow self-heal above. An + // overflow only proves the window is SMALLER; a too-small fallback + // guess never overflows (we compress early), so the only signal that + // can raise it is a prior SUCCESSFUL turn whose reported input + // EXCEEDED the window it was measured under (prevWindow, stored in + // metadata at the end of prepare()). A context that merely FIT inside + // a larger (e.g. beta) window is not evidence. Fires only on a + // low-confidence fallback (nativeFromFallback) — an authoritative + // window is never second-guessed from one turn. lastInputTokens is a + // lower bound on the real window, so raising to it is safe (overshoot + // self-corrects via the overflow path). + const prevInput = session.stats.lastInputTokens ?? 0; + const prevWindow = session.metadata.lastTurnWindow as number | undefined; + const resolved = reqConfig.modelContextLimit; + if (prevWindow !== undefined && prevInput > prevWindow && prevInput > resolved && prevInput >= 1000) { + const map = (session.metadata.learnedContextLimits as Record | undefined) ?? {}; + const prev = map[reqModel]; + if (prev === undefined || prevInput > prev) { + map[reqModel] = prevInput; + session.metadata.learnedContextLimits = map; + markDirty(session); + } + reqConfig = { ...reqConfig, modelContextLimit: prevInput }; + nativeFromFallback = false; + log("info", `[${session.id}] self-healed context window upward: ${resolved} → ${prevInput} (a prior turn used ${prevInput} input tokens, exceeding the fallback window)`); + } } // Reserve the model's OUTPUT budget for this turn from the window so the // kernel's nudge/truncate bands sit below (window - maxOutput) and a @@ -1146,6 +1179,16 @@ async function handle( } if (reserved !== reqConfig.modelContextLimit) reqConfig = { ...reqConfig, modelContextLimit: reserved }; } + // Record the FINAL effective window (post self-heal + output-headroom) + // so the status panel / acp_status show the window the kernel is actually + // using, in every mode (plugin AND wire). #393: previously this was set + // pre-self-heal and only for plugin sessions, so wire-mode panels fell + // back to a hardcoded 200K. + session.metadata.effectiveContextLimit = reqConfig.modelContextLimit; + // Window THIS turn runs under — read by the NEXT turn's upward self-heal + // to tell "context exceeded our window" (evidence) from "context fit + // inside a larger window" (not evidence). #393. + session.metadata.lastTurnWindow = reqConfig.modelContextLimit; // acquireInFlight must precede the lock so evictOldest() cannot flush // this session between getSession and lock acquisition (inFlight===0 // window). Released in the outer finally after forward completes. @@ -1219,7 +1262,7 @@ async function handle( } } if (!prepared) { - if (protocol === null && !opts.passthrough) { + if (protocol === null && !opts.passthrough && !isModelDiscoveryPath(urlPath)) { logUnrecognizedPath(log, req.url ?? ""); } await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log, route, instanceId, undefined); diff --git a/tests/issue-393-registry-window.test.ts b/tests/issue-393-registry-window.test.ts new file mode 100644 index 0000000000..53ffcce543 --- /dev/null +++ b/tests/issue-393-registry-window.test.ts @@ -0,0 +1,210 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { + _setForTest as setRegistryForTest, + bundledRegistryForTestsOnly, + bundledSnapshotLookup, + modelVariants, +} from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; +import { setLogCapture } from "../src/logger.ts"; + +// #393: Claude Opus 5 via a relay (Anthropic /v1/messages) was treated as 200K +// instead of 1M. Four root causes, each fixed and covered here: +// 1. the models.dev lookup was gated on a host that only exists in MITM / +// zero-config /bili/ mode, so plain --upstream mode always fell to the +// static table (claude-* -> 200K); +// 2. the status panel had a hardcoded 200K fallback and +// effectiveContextLimit was only written in plugin mode; +// 3. variant suffixes like -thinking missed the registry (claude-opus-5 is +// 1M in the snapshot, claude-opus-5-thinking resolved to 200K); +// 4. the overflow self-heal only lowered the window, never raised it. + +const ONE_M = 1_000_000; + +function okSse(inputTokens: number): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: inputTokens } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +interface Rig { + proxyPort: number; + upstreamPort: number; + proxy: http.Server; + upstream: http.Server; +} + +async function startRig(inputTokens = 500, log = false): Promise { + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + if (req.method === "GET") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ data: [{ id: "claude-opus-5" }] })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse(inputTokens)); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + + _setStoreForTest(new SessionStore({ enabled: false })); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: `http://127.0.0.1:${upstreamPort}`, + routes: {}, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log, + logFile: log ? "/tmp/issue-393-test.log" : undefined, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + return { proxyPort, upstreamPort, proxy, upstream }; +} + +function closeRig(rig: Rig): Promise { + rig.proxy.close(); + return once(rig.proxy, "close").then(() => { + rig.upstream.close(); + return once(rig.upstream, "close"); + }); +} + +function body(model: string): string { + return JSON.stringify({ model, max_tokens: 1024, stream: true, messages: [{ role: "user", content: "hi" }] }); +} + +// --- Fix 3 (unit): variant suffix normalization --- + +test("#393: modelVariants strips inference suffixes (original first)", () => { + assert.deepEqual(modelVariants("claude-opus-5-thinking"), ["claude-opus-5-thinking", "claude-opus-5"]); + assert.deepEqual(modelVariants("claude-opus-5"), ["claude-opus-5"]); + assert.deepEqual(modelVariants("gpt-5-high"), ["gpt-5-high", "gpt-5"]); +}); + +test("#393: bundledSnapshotLookup resolves the -thinking variant to the base model's window", () => { + assert.equal(bundledSnapshotLookup("claude-opus-5"), ONE_M); + assert.equal(bundledSnapshotLookup("claude-opus-5-thinking"), ONE_M, "variant normalizes to the base model in the snapshot"); +}); + +// --- Fix 1 + 3 (integration): --upstream mode (host=undefined) resolves the registry --- + +test("#393: --upstream mode (no /bili/ prefix) resolves claude-opus-5(-thinking) to 1M", async () => { + setRegistryForTest(bundledRegistryForTestsOnly()!); + const rig = await startRig(); + try { + // No /bili/ prefix and no MITM -> route=undefined -> host=undefined. + // The registry lookup must still run (Fix 1) and normalize the variant (Fix 3). + for (const model of ["claude-opus-5", "claude-opus-5-thinking"]) { + const sid = `upstream-${model}`; + const r = await fetch(`http://127.0.0.1:${rig.proxyPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sid, "x-bili-plugin": "test-agent" }, + body: body(model), + }); + assert.equal(r.status, 200); + await r.text(); + const sess = listSessions().find((s) => s.id === sid); + assert.equal(sess?.metadata.effectiveContextLimit, ONE_M, `${model} -> 1M in --upstream mode`); + } + } finally { + await closeRig(rig); + } +}); + +// --- Fix 2 (integration): wire mode (no plugin header) records effectiveContextLimit --- + +test("#393: wire mode (no x-bili-plugin) also records effectiveContextLimit", async () => { + setRegistryForTest(bundledRegistryForTestsOnly()!); + const rig = await startRig(); + try { + const r = await fetch(`http://127.0.0.1:${rig.proxyPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "wire-sess" }, + body: body("claude-opus-5"), + }); + assert.equal(r.status, 200); + await r.text(); + const sess = listSessions().find((s) => s.id === "wire-sess"); + assert.equal(sess?.metadata.effectiveContextLimit, ONE_M, "wire-mode session records the resolved window"); + } finally { + await closeRig(rig); + } +}); + +// --- Fix 5 (integration): upward self-heal --- + +test("#393: self-heal raises a too-small fallback window when a turn exceeded it", async () => { + setRegistryForTest({}); // empty registry -> claude-unknown falls to the static table (200K) + const rig = await startRig(300_000); // upstream truthfully reports a 300K input + try { + const headers = { "content-type": "application/json", "x-acp-session": "selfheal-sess" }; + + // Request 1: the 200K fallback window is used; the turn's real input (300K) + // is recorded but does not yet correct the window. + const r1 = await fetch(`http://127.0.0.1:${rig.proxyPort}/v1/messages`, { method: "POST", headers, body: body("claude-unknown") }); + assert.equal(r1.status, 200); + await r1.text(); + let sess = listSessions().find((s) => s.id === "selfheal-sess")!; + assert.equal(sess.metadata.effectiveContextLimit, 200_000, "first turn uses the 200K fallback"); + + // Request 2: a prior successful turn whose input (300K) exceeded the 200K + // window it was measured under is the only signal the real window is larger + // (a too-small fallback never overflows, it just compresses early) -> raise. + const r2 = await fetch(`http://127.0.0.1:${rig.proxyPort}/v1/messages`, { method: "POST", headers, body: body("claude-unknown") }); + assert.equal(r2.status, 200); + await r2.text(); + sess = listSessions().find((s) => s.id === "selfheal-sess")!; + assert.equal(sess.metadata.effectiveContextLimit, 300_000, "window raised to the observed input"); + assert.equal((sess.metadata.learnedContextLimits as Record)["claude-unknown"], 300_000, "learned window persisted per model"); + } finally { + await closeRig(rig); + } +}); + +// --- Fix 6 (integration): /models is a known discovery path --- + +test("#393: GET /models does not log an unrecognized-path warn (control: unknown path does)", async () => { + const rig = await startRig(500, true); // log on so the warn path is observable + const captured: string[] = []; + setLogCapture((level, msg) => { captured.push(`${level}:${msg}`); }); + try { + const r1 = await fetch(`http://127.0.0.1:${rig.proxyPort}/models`, { method: "GET" }); + await r1.text(); + assert.equal(captured.filter((l) => l.includes("unrecognized path")).length, 0, "/models is a known discovery path"); + + const r2 = await fetch(`http://127.0.0.1:${rig.proxyPort}/totally-unknown-xyz`, { method: "GET" }); + await r2.text(); + assert.ok(captured.some((l) => l.includes("unrecognized path") && l.includes("totally-unknown-xyz")), "unknown path still warns (control)"); + } finally { + setLogCapture(null); + await closeRig(rig); + } +});