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
22 changes: 22 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,28 @@ export function resolveMitmDomains(env: NodeJS.ProcessEnv): string[] {
]);
}

/** The mitm tier exactly as a spawned proxy child RECORDS it in its instance
* file (server.ts writes [] when mitm is disabled, even with domains
* configured): loadOptions' BILI_MITM / file mitm.enabled gate applied on top
* of its file-first/env-second union, evaluated against the env the child will
* actually see. `passed` is the explicit list a launcher injects into the
* child's BILI_MITM_DOMAINS at spawn time (CLI lanes); undefined/empty means
* the child inherits the ambient value (native lanes). Attachers must want
* THIS value — neither the raw caller list nor the raw union converges:
* domainless native bootstraps never matched a configured owner's tier, CLI
* lists miss the config-file layer, and a disabled-mitm child records []
* (#3436/#3540/#3551). */
export function resolveRecordedMitmDomains(passed: readonly string[] | undefined, env: NodeJS.ProcessEnv = process.env): string[] {
const fileConfig = loadConfigFile();
const enabled = (env.BILI_MITM ?? (fileConfig.mitm?.enabled === false ? "0" : "1")) !== "0";
if (!enabled) return [];
const effective = passed && passed.length > 0 ? passed.join(",") : env.BILI_MITM_DOMAINS;
return dedupeDomains([
...(fileConfig.mitm?.domains ?? []),
...splitCsv(effective),
]);
}

/** #1392: opted-in non-http(s) baseUrl providers — providers-table entries
* (key = provider id) with `compactionOptIn: true` ∪
* BILI_NON_HTTP_PROVIDERS, deduped. Exported so launchers can mirror the list
Expand Down
7 changes: 6 additions & 1 deletion src/kimi/native-mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
activateKimiPluginMode,
bootstrapKimiNative,
defaultLog,
kimiConfigModelWindows,
probeProxyHealth,
routeKimiConfig,
unrouteKimi,
Expand Down Expand Up @@ -39,8 +40,12 @@ function startWatchdog(applied: KimiRouteApplied, attached: boolean, log: (msg:
log(`attached proxy ${state.origin} unhealthy — waiting for it to recover`);
return;
}
// #3436/#3557: same windows source as the plugin bootstrap path —
// a respawn presenting a different map than the original proxy
// recorded would refuse each other on attach and stack instances.
const modelWindows = kimiConfigModelWindows();
const handle = await ensureProxyRunning(
{ host: LAUNCHER_DEFAULT_HOST, port: 0, passthrough: false, debug: false, lane: "kimi" },
{ host: LAUNCHER_DEFAULT_HOST, port: 0, passthrough: false, debug: false, lane: "kimi", ...(modelWindows ? { modelWindows } : {}) },
{ scriptPath: nativeProxyScriptPath() },
);
if (handle.origin !== state.origin) {
Expand Down
19 changes: 14 additions & 5 deletions src/kimi/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,19 @@ export async function bootstrapKimiNative(opts: BootstrapKimiOptions = {}): Prom
return { mode: "active", attached, routed };
}

/** #2954/#3436: every model window the client's own config.toml declares, or
* undefined when none (no file / no windows / unreadable). Both kimi proxy
* paths (plugin bootstrap AND the MCP watchdog respawn, native-mcp.ts) must
* present this SAME map — different sources make their proxies mutually
* unattachable on the model-windows cell and stack instances (#3557). */
export function kimiConfigModelWindows(env: NodeJS.ProcessEnv = process.env): Record<string, number> | undefined {
try {
const scanned = scanKimiModelWindows(fs.readFileSync(path.join(resolveKimiHome(env), "config.toml"), "utf8"));
if (Object.keys(scanned).length > 0) return scanned;
} catch {}
return undefined;
}

async function defaultEnsureProxy(): Promise<{ origin: string; attached: boolean }> {
// The spawned proxy's parent-gone watchdog (#server.ts BILI_PARENT_PID)
// keys off OUR pid: kimi kills this MCP child when its session ends, so
Expand All @@ -302,11 +315,7 @@ async function defaultEnsureProxy(): Promise<{ origin: string; attached: boolean
// (not just the active model's one-shot runtime-info report) so per-request
// window resolution knows the real window of any other model that flows
// through — mid-session switches used to resolve against a stale guess.
let modelWindows: Record<string, number> | undefined;
try {
const scanned = scanKimiModelWindows(fs.readFileSync(path.join(resolveKimiHome(process.env), "config.toml"), "utf8"));
if (Object.keys(scanned).length > 0) modelWindows = scanned;
} catch {}
const modelWindows = kimiConfigModelWindows();
const handle = await ensureProxyRunning(
{ host: LAUNCHER_DEFAULT_HOST, port: 0, passthrough: false, debug: false, lane: "kimi", ...(modelWindows ? { modelWindows } : {}) },
{ scriptPath: nativeProxyScriptPath() },
Expand Down
41 changes: 31 additions & 10 deletions src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ function selfDistFile(name: string): string {
return path.join(selfPackageRoot(), "dist", name);
}
import { nonEmpty, resolvePiHome, resolveOmpHome, resolveDshHome, resolveCodexHome, resolveCodexEffectiveView, loadClientConfig, collectModelWindows, collectModelMaxOutputs, type ClientConfig, type CodexConfig, resolveOpencodeConfigFile, readOpencodeConfigRoot, opencodePluginBaseDir, type OpencodeConfig, type OpencodeProvider, type HermesConfig, type HermesProvider, qoderIsCnSite, QODER_DEFAULT_MODEL_HOSTS, resolveTraeHome, readTraeConfig, TRAE_DEFAULT_MODEL_HOSTS, JCODE_DEFAULT_MODEL_HOSTS, type TraeConfig, resolveKimiHome, readKimiConfig, parseKimiToml, KIMI_DEFAULT_MODEL_HOSTS, QWEN_DEFAULT_MODEL_HOSTS, type KimiConfig, type KimiProvider, readOpencodeProjectLayer, type OpencodeProjectLayer, readMcodeConfig, resolveMcodeInstallDir, MCODE_DEFAULT_MODEL_HOSTS, type McodeConfig, discoverAiderArgUrls, AIDER_DEFAULT_MODEL_HOSTS, COPILOT_DEFAULT_MODEL_HOSTS, AMP_DEFAULT_MODEL_HOSTS, CRUSH_DEFAULT_MODEL_HOSTS, ZED_DEFAULT_MODEL_HOSTS, readZedConfig, resolveGooseDirs, readGooseConfig, type GooseConfig, type GooseDirs } from "./client-config.js";
import { allowDshCompactionState, loadRoutes, resolveConfiguredContextLimit, lookupContextLimit, resolveNativeAttachExternal, resolveNativeAttachLanePriority, resolveNativeAttachPolicy, resolveNativeAttachUrl, resolveMitmDomains, resolveNonHttpProviders, type ProviderRoutes } from "./config.js";
import { allowDshCompactionState, loadRoutes, resolveConfiguredContextLimit, lookupContextLimit, resolveNativeAttachExternal, resolveNativeAttachLanePriority, resolveNativeAttachPolicy, resolveNativeAttachUrl, resolveMitmDomains, resolveNonHttpProviders, resolveRecordedMitmDomains, type ProviderRoutes } from "./config.js";
import { discoverMitmDomains } from "./discover.js";
import { contextFromRegistry } from "./registry.js";
import { parseLauncherModelWindows, parseLauncherModelMaxOutputs } from "./server/context-window.js";
import { normalizeVertexModelId, stripBiliWrappers, vertexBaseUrlForLocation, vertexBaseUrlLacksV1 } from "./vertex.js";

export {
Expand Down Expand Up @@ -3573,6 +3574,20 @@ function isStartingMarkerActive(marker: ProxyStartingMarker, nowMs: number): boo
return isPidAlive(marker.pid) && nowMs - marker.startedAt < STARTING_MARKER_TTL_MS;
}

/** #3436: project a caller's model-map opts onto what a spawned child will
* RECORD for them: CLI lanes inject JSON.stringify(opts) into the named env
* var at spawn time, native lanes inherit the ambient value; the child
* records the parser applied to exactly that string (context-window.ts).
* Running the want-side through the same parse keeps the comparison
* byte-stable regardless of how the map was produced. */
function recordedModelMap(
passed: Record<string, number> | undefined,
ambientRaw: string | undefined,
parse: (raw: string | undefined) => Record<string, number>,
): Record<string, number> {
return parse(passed && Object.keys(passed).length > 0 ? JSON.stringify(passed) : ambientRaw);
}

/** #1225: reuse requires identical CODE, not just identical config shape —
* same version number with different dist contents (local rebuild, npm link,
* unpublished branch) must not be served by the stale instance. codeFingerprint
Expand All @@ -3587,16 +3602,22 @@ function instanceCompatible(inst: ProxyInstanceFile, opts: LaunchOptions, codeFi
// listed it in native.attachLanePriority — isolation by default (#1225/#1232).
if (opts.lane !== undefined && inst.lane !== undefined && inst.lane !== opts.lane && !foreignLanes.includes(inst.lane)) return "lane-mismatch";
if (inst.host !== opts.host || inst.passthrough !== opts.passthrough) return "host-or-passthrough-mismatch";
const wantDomains = opts.mitmDomains ?? [];
// #3436: each cell wants what a spawned child will RECORD, not the raw
// caller input — the child records loadOptions' gated mitm tier and the
// parse of the BILI_LAUNCHER_MODEL_* env it receives. Native lanes pass no
// domains/windows at all, so the bare `?? []` / `?? {}` fallbacks never
// matched an instance whose owner configured ANY tier: every attach was a
// mismatch and every host process spawned its own proxy (instances only
// grew). Projecting onto the recorded values makes same-lane peers converge
// while genuine drift (config edited after spawn) still mismatches.
const wantDomains = resolveRecordedMitmDomains(opts.mitmDomains);
if (inst.mitmDomains.length !== wantDomains.length || inst.mitmDomains.some((d, i) => d !== wantDomains[i])) return "mitm-domains-mismatch";
const wantWindows = opts.modelWindows ?? {};
const keys = Object.keys(wantWindows);
if (Object.keys(inst.modelWindows).length !== keys.length) return "model-windows-mismatch";
if (!keys.every((k) => inst.modelWindows[k] === wantWindows[k])) return "model-windows-mismatch";
const wantMax = opts.modelMaxOutputs ?? {};
const maxKeys = Object.keys(wantMax);
if (Object.keys(inst.modelMaxOutputs ?? {}).length !== maxKeys.length) return "model-max-outputs-mismatch";
return maxKeys.every((k) => (inst.modelMaxOutputs ?? {})[k] === wantMax[k]) ? undefined : "model-max-outputs-mismatch";
const wantWindows = recordedModelMap(opts.modelWindows, process.env.BILI_LAUNCHER_MODEL_WINDOWS, parseLauncherModelWindows);
if (Object.keys(inst.modelWindows).length !== Object.keys(wantWindows).length) return "model-windows-mismatch";
if (!Object.keys(wantWindows).every((k) => inst.modelWindows[k] === wantWindows[k])) return "model-windows-mismatch";
const wantMax = recordedModelMap(opts.modelMaxOutputs, process.env.BILI_LAUNCHER_MODEL_MAX_OUTPUTS, parseLauncherModelMaxOutputs);
if (Object.keys(inst.modelMaxOutputs ?? {}).length !== Object.keys(wantMax).length) return "model-max-outputs-mismatch";
return Object.keys(wantMax).every((k) => (inst.modelMaxOutputs ?? {})[k] === wantMax[k]) ? undefined : "model-max-outputs-mismatch";
}

/** #1232: every healthy live instance — not just the last writer of the
Expand Down
Loading
Loading