diff --git a/src/config.ts b/src/config.ts index 08099520d4..5835c372f1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 diff --git a/src/kimi/native-mcp.ts b/src/kimi/native-mcp.ts index e70fbfd8c0..7320f820c2 100644 --- a/src/kimi/native-mcp.ts +++ b/src/kimi/native-mcp.ts @@ -12,6 +12,7 @@ import { activateKimiPluginMode, bootstrapKimiNative, defaultLog, + kimiConfigModelWindows, probeProxyHealth, routeKimiConfig, unrouteKimi, @@ -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) { diff --git a/src/kimi/native.ts b/src/kimi/native.ts index b8438c2296..375959ec10 100644 --- a/src/kimi/native.ts +++ b/src/kimi/native.ts @@ -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 | 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 @@ -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 | 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() }, diff --git a/src/launcher.ts b/src/launcher.ts index 9464d0f398..0f78aca4ea 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -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 { @@ -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 | undefined, + ambientRaw: string | undefined, + parse: (raw: string | undefined) => Record, +): Record { + 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 @@ -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 diff --git a/tests/issue3436-native-proxy-reuse.test.ts b/tests/issue3436-native-proxy-reuse.test.ts new file mode 100644 index 0000000000..f6f2ec0c24 --- /dev/null +++ b/tests/issue3436-native-proxy-reuse.test.ts @@ -0,0 +1,231 @@ +// #3436: native-lane bootstraps pass no mitmDomains/modelWindows at all, but +// the spawned child RECORDS its resolved tier (config-file mitm.domains ∪ +// inherited BILI_MITM_DOMAINS, gated by the enabled flag; the parsed +// BILI_LAUNCHER_MODEL_* env) in the instance file — so the attach comparison +// wanted []/{} against an instance offering [configured...], never matched, +// and every host process spawned its own proxy (user log: another bili +// instance ×103, 15 instances in 1.5h). The compat check must want exactly +// what the child records (resolveRecordedMitmDomains / recordedModelMap), +// whatever the lane passes. + +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import type { ProxyInstanceFile } from "../src/instance.ts"; +import { entryScriptFingerprint } from "../src/instance.ts"; +import { ensureProxyRunning, type SpawnChild, type SpawnFn } from "../src/launcher.ts"; + +const FP_SCRIPT = path.join(os.tmpdir(), `bili-fp-3436-${process.pid}.js`); +fs.writeFileSync(FP_SCRIPT, "// fingerprint fixture\n"); +const FP = entryScriptFingerprint(FP_SCRIPT); +assert.ok(FP !== undefined, "fixture script must fingerprint"); + +const ORIGIN = "http://127.0.0.1:18797"; + +function tmpHome(): { dir: string; restore: () => void } { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-3436-home-")); + const prevCfg = process.env.XDG_CONFIG_HOME; + const prevState = process.env.XDG_STATE_HOME; + process.env.XDG_CONFIG_HOME = dir; + process.env.XDG_STATE_HOME = dir; + return { + dir, + restore: () => { + if (prevCfg === undefined) delete process.env.XDG_CONFIG_HOME; else process.env.XDG_CONFIG_HOME = prevCfg; + if (prevState === undefined) delete process.env.XDG_STATE_HOME; else process.env.XDG_STATE_HOME = prevState; + }, + }; +} + +function writeConfig(dir: string, cfg: unknown): void { + fs.mkdirSync(path.join(dir, "billion-context"), { recursive: true }); + fs.writeFileSync(path.join(dir, "billion-context", "billion-context.json"), JSON.stringify(cfg)); +} + +function liveInstance( + mitmDomains: string[], + modelWindows: Record = {}, + modelMaxOutputs?: Record, +): ProxyInstanceFile { + return { + origin: ORIGIN, + instanceId: "inst-3436", + pid: process.pid, + startedAt: Date.now(), + host: "127.0.0.1", + port: 18797, + passthrough: false, + mitmDomains, + modelWindows, + ...(modelMaxOutputs !== undefined && Object.keys(modelMaxOutputs).length > 0 ? { modelMaxOutputs } : {}), + lane: "dsh", + codeFingerprint: FP, + }; +} + +function makeFakeChild(pid: number): SpawnChild { + return { + pid, + unref() {}, + kill() { return true; }, + on() {}, + }; +} + +function fakeClock() { + let ticks = 0; + return { + now: () => ticks * 1000, + sleep: () => { + ticks += 1; + return Promise.resolve(); + }, + }; +} + +/** Native-lane shape bring-up: no mitmDomains unless the case says so. */ +async function bringUp(live: ProxyInstanceFile | undefined, passDomains?: string[]) { + let spawnCalls = 0; + const clock = fakeClock(); + const handle = await ensureProxyRunning( + { host: "127.0.0.1", port: 0, passthrough: false, debug: false, lane: "dsh", ...(passDomains !== undefined ? { mitmDomains: passDomains } : {}) }, + { + fetchImpl: async () => ({ ok: true }), + fetchHealthInfo: async (origin) => (live !== undefined && origin === ORIGIN ? { ok: true, watchdog: { armed: true } } : undefined), + readInstanceFile: () => live, + listRegistryInstances: () => [], + spawnImpl: (_cmd: string, _args: readonly string[]) => { + spawnCalls++; + return makeFakeChild(42_470); + }, + now: clock.now, + sleep: clock.sleep, + registerWatcher: async () => "ok", + attachDiag: () => {}, + scriptPath: FP_SCRIPT, + }, + ); + return { handle, spawnCalls }; +} + +test("#3436 native lane attaches to its own instance when config-file mitm.domains is set", async () => { + const home = tmpHome(); + try { + writeConfig(home.dir, { mitm: { domains: ["api.a.com"] } }); + // The child spawned by a previous dsh process recorded the tier it + // resolved (file layer, no env override because bootstraps pass + // nothing → env inherited, empty here). + const { handle, spawnCalls } = await bringUp(liveInstance(["api.a.com"])); + assert.equal(spawnCalls, 0, "no new proxy — the existing one is compatible"); + assert.equal(handle.attached, true); + assert.equal(handle.origin, ORIGIN); + } finally { + home.restore(); + } +}); + +test("#3436 caller-env BILI_MITM_DOMAINS layer is wanted too (native children inherit it)", async () => { + const home = tmpHome(); + try { + const prev = process.env.BILI_MITM_DOMAINS; + process.env.BILI_MITM_DOMAINS = "b1.com,b2.com"; + try { + const { handle, spawnCalls } = await bringUp(liveInstance(["b1.com", "b2.com"])); + assert.equal(spawnCalls, 0); + assert.equal(handle.attached, true); + } finally { + if (prev === undefined) delete process.env.BILI_MITM_DOMAINS; else process.env.BILI_MITM_DOMAINS = prev; + } + } finally { + home.restore(); + } +}); + +test("#3436 explicit (CLI-shape) lists want the file layer prepended, in record order", async () => { + const home = tmpHome(); + try { + writeConfig(home.dir, { mitm: { domains: ["a.com"] } }); + // A CLI lane spawns with BILI_MITM_DOMAINS=r.com while the file layer + // holds a.com → the child records [a.com, r.com] (file first). + const { handle, spawnCalls } = await bringUp(liveInstance(["a.com", "r.com"]), ["r.com"]); + assert.equal(spawnCalls, 0); + assert.equal(handle.attached, true); + } finally { + home.restore(); + } +}); + +test("#3436 mitm disabled records [] and wants [] (configured domains ignored)", async () => { + const home = tmpHome(); + try { + writeConfig(home.dir, { mitm: { enabled: false, domains: ["x.com"] } }); + const { handle, spawnCalls } = await bringUp(liveInstance([])); + assert.equal(spawnCalls, 0); + assert.equal(handle.attached, true); + } finally { + home.restore(); + } +}); + +test("#3436 default-empty behavior unchanged: no config, no env → wants []", async () => { + const home = tmpHome(); + try { + const { handle, spawnCalls } = await bringUp(liveInstance([])); + assert.equal(spawnCalls, 0); + assert.equal(handle.attached, true); + } finally { + home.restore(); + } +}); + +test("#3436 genuine mismatch still refuses to attach (spawn path)", async () => { + const home = tmpHome(); + try { + // Instance recorded a tier nobody configured anymore (config edited + // after spawn) — attaching would serve the wrong whitelist. + await assert.rejects(bringUp(liveInstance(["other.com"]))); + } finally { + home.restore(); + } +}); + +test("#3436 native lane attaches when exported BILI_LAUNCHER_MODEL_* match the peer's recorded maps", async () => { + const home = tmpHome(); + try { + const prevW = process.env.BILI_LAUNCHER_MODEL_WINDOWS; + const prevM = process.env.BILI_LAUNCHER_MODEL_MAX_OUTPUTS; + process.env.BILI_LAUNCHER_MODEL_WINDOWS = JSON.stringify({ "model-x": 200_000 }); + process.env.BILI_LAUNCHER_MODEL_MAX_OUTPUTS = JSON.stringify({ "model-x": 8192 }); + try { + // A previous same-lane process inherited the same ambient env and + // its child recorded exactly these maps (the launcher injects + // JSON.stringify(opts) only on CLI lanes; native children parse + // the inherited string). + const { handle, spawnCalls } = await bringUp(liveInstance([], { "model-x": 200_000 }, { "model-x": 8192 })); + assert.equal(spawnCalls, 0); + assert.equal(handle.attached, true); + } finally { + if (prevW === undefined) delete process.env.BILI_LAUNCHER_MODEL_WINDOWS; else process.env.BILI_LAUNCHER_MODEL_WINDOWS = prevW; + if (prevM === undefined) delete process.env.BILI_LAUNCHER_MODEL_MAX_OUTPUTS; else process.env.BILI_LAUNCHER_MODEL_MAX_OUTPUTS = prevM; + } + } finally { + home.restore(); + } +}); + +test("#3436 genuine model-windows mismatch still refuses to attach (spawn path)", async () => { + const home = tmpHome(); + try { + const prevW = process.env.BILI_LAUNCHER_MODEL_WINDOWS; + process.env.BILI_LAUNCHER_MODEL_WINDOWS = JSON.stringify({ "model-x": 200_000 }); + try { + await assert.rejects(bringUp(liveInstance([], { "model-y": 100_000 }))); + } finally { + if (prevW === undefined) delete process.env.BILI_LAUNCHER_MODEL_WINDOWS; else process.env.BILI_LAUNCHER_MODEL_WINDOWS = prevW; + } + } finally { + home.restore(); + } +}); diff --git a/tests/kimi-native.test.ts b/tests/kimi-native.test.ts index 2daba6c3b9..cefbf7f6eb 100644 --- a/tests/kimi-native.test.ts +++ b/tests/kimi-native.test.ts @@ -26,6 +26,7 @@ import { import { activateKimiPluginMode, bootstrapKimiNative, + kimiConfigModelWindows, planNativeKimi, probeProxyHealth, unrouteKimi, @@ -347,6 +348,20 @@ test("scanKimiModelWindows keys windows by model id across all sections (#2954)" assert.deepEqual(scanKimiModelWindows(SAMPLE_CONFIG), {}, "no windows declared → empty map, nothing to feed"); }); +test("kimiConfigModelWindows hands both kimi proxy paths ONE windows source (#3436/#3557)", () => { + const home = path.join(tmpdir(), `bili-kimi-win-${process.pid}-${Date.now()}`); + mkdirSync(home, { recursive: true }); + try { + writeFileSync(path.join(home, "config.toml"), THINKING_CONFIG); + assert.deepEqual(kimiConfigModelWindows({ ...process.env, KIMI_CODE_HOME: home }), { "deepseek-r1": 65536 }); + writeFileSync(path.join(home, "config.toml"), SAMPLE_CONFIG); + assert.equal(kimiConfigModelWindows({ ...process.env, KIMI_CODE_HOME: home }), undefined, "no windows → undefined, caller omits the field"); + assert.equal(kimiConfigModelWindows({ ...process.env, KIMI_CODE_HOME: path.join(home, "missing") }), undefined, "no file → undefined, never throws"); + } finally { + rmrf(home); + } +}); + interface FakeHome { home: string; cleanup: () => void } /** Deterministic fake $KIMI_CODE_HOME: PATH is REPLACED (not prepended) with