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/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,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({
Expand Down
101 changes: 69 additions & 32 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,43 +253,80 @@ 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
// */<model> and take the MAXIMUM window across matches when they
// disagree: a conflicting value used to return undefined (silent static
// fallback, typically 200K). Max is never smaller than any single
// declared deployment, so compression thresholds never fire earlier
// than some real deployment would allow. Conflicts are logged once per
// model. 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 max: number | undefined;
const distinct = new Set<number>();
const parts: string[] = [];
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 (max === undefined || ctx > max) max = ctx;
distinct.add(ctx);
parts.push(`${key}=${ctx}`);
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;
}
if (max !== undefined && distinct.size > 1 && !warnedConflicts.has(model)) {
warnedConflicts.add(model);
loggerLog("warn", `[acp-registry] conflicting context windows for "${model}" (${parts.join(", ")}) — using max ${max}`);
// 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
// */<name> and take the MAXIMUM window across matches when they
// disagree: max is never smaller than any single declared deployment,
// so compression thresholds never fire earlier than some real
// deployment would allow; conflicts are logged once per name. Zero
// valid matches falls through to the next (stripped) variant.
// Known-provider hosts do not cross-provider scan: a miss means the
// model is genuinely unlisted for that provider (its stripped
// variants still get their exact-key chance above).
if (provider === undefined) {
const suffix = `/${name}`;
let max: number | undefined;
const distinct = new Set<number>();
const parts: string[] = [];
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 (max === undefined || ctx > max) max = ctx;
distinct.add(ctx);
parts.push(`${key}=${ctx}`);
}
if (max !== undefined) {
if (distinct.size > 1 && !warnedConflicts.has(name)) {
warnedConflicts.add(name);
loggerLog("warn", `[acp-registry] conflicting context windows for "${name}" (${parts.join(", ")}) — using max ${max}`);
}
return max;
}
}
return max;
}
return undefined;
}
Expand Down
51 changes: 47 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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
Expand Down Expand Up @@ -941,7 +948,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
Expand All @@ -955,7 +962,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;
}
Expand Down Expand Up @@ -1182,7 +1189,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
Expand Down Expand Up @@ -1237,6 +1243,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<string, number> | 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
Expand Down Expand Up @@ -1269,6 +1302,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.
Expand Down Expand Up @@ -1342,7 +1385,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);
Expand Down
Loading
Loading