From cbb7ba2736a18bd0675701147d1fe5d291b87965 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 4 Oct 2026 19:31:22 +0800 Subject: [PATCH 1/2] fix: bili codex pins embedded mode via probed --no-daemon, stopping daemon attach/bypass (#1867) --- CLIENTS.md | 17 ++++ src/launcher.ts | 51 ++++++++++- tests/launcher.test.ts | 198 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 265 insertions(+), 1 deletion(-) diff --git a/CLIENTS.md b/CLIENTS.md index fbee76d06e..8c854d7fff 100644 --- a/CLIENTS.md +++ b/CLIENTS.md @@ -348,6 +348,23 @@ client through bili, either resend the full input/output history or set now logs a `warn` (#1954). Full chaining support is tracked as #1973. See the [official migration guide](https://developers.openai.com/api/docs/guides/migrate-to-responses). +### Run mode: `bili codex` pins embedded (#1867) + +Since ~0.156 Codex can attach to (or auto-start) a machine-wide shared +background server whose model traffic uses the environment that was present +**when the daemon started**, not when a session starts. The launcher's proxy is +session-scoped (its port dies with the process), so a long-lived daemon cannot +route through it safely: if codex starts first without bili's env, later +`bili codex` sessions silently attach to it and bypass compression entirely; +if bili starts first, the surviving daemon keeps pointing at a dead port. The +launcher therefore passes `--no-daemon` explicitly — after probing +`codex --help` for the flag (older binaries launch unchanged) and only when the +user has not already pinned a mode (`--no-daemon` or `--remote`). Result: +deterministic embedded runs, no per-launch fallback warning, no silent bypass. +The #321 budget `-c` args are kept verbatim (embedded mode honors them +identically). If you want the shared background server, run native `codex` +directly — no compression, but tools still work via `bili plugin install codex`. + ## Gemini family (Gemini CLI / iFlow CLI / Qwen Code) Three launchers for the gemini-cli architecture family (#1043 tier 1). Two of diff --git a/src/launcher.ts b/src/launcher.ts index 00cdad7dd1..ebcdd639af 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -34,7 +34,7 @@ import os from "node:os"; import path from "node:path"; import { createRequire } from "node:module"; import { pathToFileURL } from "node:url"; -import { execFileSync, spawn, type StdioOptions } from "node:child_process"; +import { execFileSync, spawn, spawnSync, type StdioOptions } from "node:child_process"; import { DEFAULT_MITM_DOMAINS } from "./mitm.js"; import { claimStartingMarker, @@ -2956,6 +2956,48 @@ export function opencodeMajorVersion(command: string): number { return major; } +/** #1867: true when the user already pinned codex's run mode themselves — an + * explicit `--no-daemon` (embedded) or `--remote [url]` (remote app server). + * In either case bili must not add its own `--no-daemon`: the first would be a + * duplicate, and codex hard-errors on --no-daemon combined with --remote. */ +export function codexRunModePinned(args: readonly string[]): boolean { + return args.some((a) => a === "--no-daemon" || a === "--remote" || a.startsWith("--remote=")); +} + +const codexNoDaemonCache = new Map(); + +/** #1867: whether a Codex CLI binary accepts `--no-daemon`, probed from its + * `--help` output (cached per resolved command). The probe spawns the binary + * exactly the way runClient will (planClientSpawn) with a short timeout; any + * failure defaults to false — injecting a flag the binary does not know would + * break the launch, while skipping it only restores the pre-probe behavior. + * Mirrors the opencodeMajorVersion() precedent. spawnSync (not execFileSync): + * its options accept windowsVerbatimArguments, which the #679 cmd.exe wrap needs. */ +export function codexSupportsNoDaemon( + command: string, + prefixArgs: readonly string[], + platform: NodeJS.Platform = process.platform, +): boolean { + const key = JSON.stringify([command, ...prefixArgs]); + const hit = codexNoDaemonCache.get(key); + if (hit !== undefined) return hit; + let supported = false; + try { + const plan = planClientSpawn(command, [...prefixArgs, "--help"], process.env, platform); + const res = spawnSync(plan.command, plan.args, { + timeout: 5000, + stdio: ["ignore", "pipe", "ignore"], + windowsVerbatimArguments: plan.windowsVerbatimArguments, + windowsHide: true, + }); + // line-anchored exact-flag match: clap lists each option on its own + // indented line; \b would fail anyway since '-' is not a word char + if (res.status === 0 && !res.error) supported = /(?:^|\n)[ \t]*--no-daemon[ \t]*(?:\r?\n|$)/.test(res.stdout.toString("utf8")); + } catch {} + codexNoDaemonCache.set(key, supported); + return supported; +} + /** * opencode counterpart of preparePiHttpRewrite: write a full copy of the user's * (JSONC-tolerant, merged) config with the discovered providers' baseURL @@ -4641,6 +4683,13 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} } else { env = buildCodexEnv(origin, codexCaPath, stripInheritedProxy(process.env)); clientArgs = buildCodexArgs(origin, routes.httpRewrites, routes.httpsRewrites, clientArgs); + if (!codexRunModePinned(params.clientArgs)) { + const { command: codexBin, prefixArgs: codexPrefix } = resolveClientCommand(base, process.env); + if (codexSupportsNoDaemon(codexBin, codexPrefix, deps.platform ?? process.platform)) { + clientArgs = ["--no-daemon", ...clientArgs]; + console.error("bili: codex pinned to embedded mode (--no-daemon) — the launcher proxy is session-scoped; a shared background server would outlive it and bypass compression."); + } + } const budgetArgs = await resolveCodexBudgetArgs({ model: config.codex?.model, clientWindow: config.codex?.contextWindow, diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 23639b5b83..03842a4049 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -85,6 +85,8 @@ import { stopProxyGuarded, resolveLauncherWindow, resolveCodexBudgetArgs, + codexRunModePinned, + codexSupportsNoDaemon, resolveClaudeBudgetEnv, resolveQoderBudgetEnv, buildQoderEnv, @@ -4200,6 +4202,202 @@ test("runLaunch codex: budget args injected for MITM mode (built-in table window } }); +test("codexRunModePinned: detects user-pinned run mode (#1867)", () => { + assert.equal(codexRunModePinned([]), false); + assert.equal(codexRunModePinned(["-c", "model_context_window=400000"]), false); + assert.equal(codexRunModePinned(["resume", "abc"]), false); + assert.equal(codexRunModePinned(["--no-daemon"]), true); + assert.equal(codexRunModePinned(["--remote", "ws://127.0.0.1:9"]), true); + assert.equal(codexRunModePinned(["--remote=ws://127.0.0.1:9"]), true); +}); + +test("codexSupportsNoDaemon: probes --help output, fails soft (#1867)", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-nodaemon-")); + const isWin = process.platform === "win32"; + const helpWithFlag = isWin + ? "@echo Options:\r\n@echo --no-daemon\r\n@echo Run without the shared background server\r\n" + : "#!/bin/sh\necho \"Options:\"\necho \" --no-daemon\"\necho \" Run without the shared background server\"\n"; + const helpWithoutFlag = isWin + ? "@echo Options:\r\n@echo --profile \r\n" + : "#!/bin/sh\necho \"Options:\"\necho \" --profile \"\n"; + const mk = (name: string, body: string): string => { + const p = path.join(dir, name + (isWin ? ".cmd" : "")); + fs.writeFileSync(p, body); + if (!isWin) fs.chmodSync(p, 0o755); + return p; + }; + try { + assert.equal(codexSupportsNoDaemon(mk("new-codex", helpWithFlag), []), true); + assert.equal(codexSupportsNoDaemon(mk("old-codex", helpWithoutFlag), []), false); + assert.equal(codexSupportsNoDaemon(mk("dead-codex", isWin ? "@exit /b 3\r\n" : "#!/bin/sh\nexit 3\n"), []), false); + assert.equal(codexSupportsNoDaemon(path.join(dir, "missing-codex" + (isWin ? ".cmd" : "")), []), false); + // cached per command path + const again = mk("new-codex-2", helpWithFlag); + assert.equal(codexSupportsNoDaemon(again, []), true); + fs.writeFileSync(again, helpWithoutFlag); + if (!isWin) fs.chmodSync(again, 0o755); + assert.equal(codexSupportsNoDaemon(again, []), true); + } finally { + rmrf(dir); + } +}); + +test("runLaunch codex: --no-daemon pinned when supported, escape hatches honored (#1867)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-nodaemon-run-")); + const prevHome = process.env.HOME; + const prevUserProfile = process.env.USERPROFILE; + const prevClientBin = process.env.BILI_CLIENT_BIN; + const prevAnthropicModel = process.env.ANTHROPIC_MODEL; + const prevAutoCompact = process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + process.env.HOME = home; + if (prevUserProfile !== undefined) process.env.USERPROFILE = home; + delete process.env.ANTHROPIC_MODEL; + delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + const isWin = process.platform === "win32"; + const fakeCodex = path.join(home, isWin ? "fake-codex.cmd" : "fake-codex"); + fs.writeFileSync(fakeCodex, isWin ? "@echo Options:\r\n@echo --no-daemon\r\n@echo Run without the shared background server\r\n" : "#!/bin/sh\necho \"Options:\"\necho \" --no-daemon\"\necho \" Run without the shared background server\"\n"); + if (!isWin) fs.chmodSync(fakeCodex, 0o755); + process.env.BILI_CLIENT_BIN = fakeCodex; + const codexHome = path.join(home, ".codex"); + fs.mkdirSync(codexHome, { recursive: true }); + fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "gpt-5.5"\n'); + + const clientArgsSeen: string[][] = []; + const spawnImpl: SpawnFn = (cmd, args) => { + if (cmd === fakeCodex) { + clientArgsSeen.push([...args]); + const child = makeFakeChild(0); + const orig = child.on!.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + } + return makeFakeChild(42422); + }; + const fetchImpl = async () => ({ ok: true }); + const prevExit = process.exit; + process.exit = (() => undefined) as typeof process.exit; + const launch = (clientArgs: string[]) => + runLaunch( + { client: "codex", clientArgs, overrides: {} }, + { fetchImpl, fetchHealthInfo: async () => ({ ok: true, pid: lastFakeChildPid }), spawnImpl, sleep: () => Promise.resolve() }, + ); + + try { + // fresh launch → #321 budget args AND explicit embedded pin + await launch([]); + assert.equal(clientArgsSeen.length, 1); + let args = clientArgsSeen[0]; + assert.ok(args.includes("--no-daemon"), JSON.stringify(args)); + assert.ok(args.includes("model_context_window=400000") && args.includes("model_auto_compact_token_limit=400000"), JSON.stringify(args)); + + // user self-aligned window (no budget injection) → still pinned: without this + // the session would silently attach to an external daemon and bypass bili + fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "gpt-5.5"\nmodel_context_window = 1000000\n'); + clientArgsSeen.length = 0; + await launch([]); + args = clientArgsSeen[0]; + assert.ok(args.includes("--no-daemon"), JSON.stringify(args)); + assert.ok(!args.some((a) => a.startsWith("model_context_window=")), JSON.stringify(args)); + + // unresolvable model (no budget injection) → still pinned + fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "totally-unknown-model"\n'); + clientArgsSeen.length = 0; + await launch([]); + args = clientArgsSeen[0]; + assert.ok(args.includes("--no-daemon"), JSON.stringify(args)); + + // user already passed --no-daemon → not duplicated + clientArgsSeen.length = 0; + await launch(["--no-daemon"]); + args = clientArgsSeen[0]; + assert.equal(args.filter((a) => a === "--no-daemon").length, 1, JSON.stringify(args)); + + // user passed --remote → no injection (codex hard-errors the combo) + clientArgsSeen.length = 0; + await launch(["--remote", "ws://127.0.0.1:9"]); + args = clientArgsSeen[0]; + assert.ok(!args.includes("--no-daemon"), JSON.stringify(args)); + assert.ok(args.includes("--remote"), JSON.stringify(args)); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = prevUserProfile; + if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevClientBin; + if (prevAnthropicModel === undefined) delete process.env.ANTHROPIC_MODEL; + else process.env.ANTHROPIC_MODEL = prevAnthropicModel; + if (prevAutoCompact === undefined) delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + else process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW = prevAutoCompact; + rmrf(home); + } +}); + +test("runLaunch codex: old binary without --no-daemon launches unchanged (#1867)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-nodaemon-old-")); + const prevHome = process.env.HOME; + const prevUserProfile = process.env.USERPROFILE; + const prevClientBin = process.env.BILI_CLIENT_BIN; + const prevAnthropicModel = process.env.ANTHROPIC_MODEL; + const prevAutoCompact = process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + process.env.HOME = home; + if (prevUserProfile !== undefined) process.env.USERPROFILE = home; + delete process.env.ANTHROPIC_MODEL; + delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + const isWin = process.platform === "win32"; + const fakeCodex = path.join(home, isWin ? "old-codex.cmd" : "old-codex"); + fs.writeFileSync(fakeCodex, isWin ? "@echo Options:\r\n@echo --profile \r\n" : "#!/bin/sh\necho \"Options:\"\necho \" --profile \"\n"); + if (!isWin) fs.chmodSync(fakeCodex, 0o755); + process.env.BILI_CLIENT_BIN = fakeCodex; + const codexHome = path.join(home, ".codex"); + fs.mkdirSync(codexHome, { recursive: true }); + fs.writeFileSync(path.join(codexHome, "config.toml"), ""); + + const clientArgsSeen: string[][] = []; + const spawnImpl: SpawnFn = (cmd, args) => { + if (cmd === fakeCodex) { + clientArgsSeen.push([...args]); + const child = makeFakeChild(0); + const orig = child.on!.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + } + return makeFakeChild(42422); + }; + const fetchImpl = async () => ({ ok: true }); + const prevExit = process.exit; + process.exit = (() => undefined) as typeof process.exit; + + try { + await runLaunch( + { client: "codex", clientArgs: [], overrides: {} }, + { fetchImpl, fetchHealthInfo: async () => ({ ok: true, pid: lastFakeChildPid }), spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgsSeen.length, 1); + assert.ok(!clientArgsSeen[0].includes("--no-daemon"), JSON.stringify(clientArgsSeen[0])); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = prevUserProfile; + if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevClientBin; + if (prevAnthropicModel === undefined) delete process.env.ANTHROPIC_MODEL; + else process.env.ANTHROPIC_MODEL = prevAnthropicModel; + if (prevAutoCompact === undefined) delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + else process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW = prevAutoCompact; + rmrf(home); + } +}); + test("runLaunch claude: CLAUDE_CODE_AUTO_COMPACT_WINDOW injected (built-in table window)", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-budget-")); const prevHome = process.env.HOME; From 2fe98fdef5ac331be97828421bfd7419658fc86e Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 4 Oct 2026 20:15:26 +0800 Subject: [PATCH 2/2] test(launcher): make codex --no-daemon fakes planner-aware on win32 (#1867) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit runClient plans the spawn via planClientSpawn BEFORE calling deps.spawnImpl — on win32 a .cmd fake arrives wrapped as 'comspec /d /s /c ""', so the 'cmd === fakeCodex' capture guard never matched: the client child was a plain fake without the exit override and runClient's promise stayed pending forever, draining the event loop ('Promise resolution is still pending...') and cascading into every later test in the file. Match the embedded path instead and unwrap the comspec line back to client tokens with a quote-aware splitter (round-trips buildWindowsCommandLine), pinned by a platform-neutral round-trip unit test. --- tests/launcher.test.ts | 96 ++++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 22 deletions(-) diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 03842a4049..0315d88c46 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -87,6 +87,7 @@ import { resolveCodexBudgetArgs, codexRunModePinned, codexSupportsNoDaemon, + buildWindowsCommandLine, resolveClaudeBudgetEnv, resolveQoderBudgetEnv, buildQoderEnv, @@ -1056,6 +1057,29 @@ function makeFakeChild(pid: number): SpawnChild { }; } +// #679/#1867 test seam: inverse of planClientSpawn's comspec wrapping — splits +// the single `comspec /d /s /c ""` arg back into tokens (quote-aware; +// quoteWinToken only quotes whitespace-bearing tokens, so this round-trips it). +function splitWindowsCommandLine(line: string): string[] { + const tokens: string[] = []; + let cur = ""; + let quoted = false; + for (const ch of line) { + if (ch === '"') { + quoted = !quoted; + continue; + } + if (!quoted && /\s/.test(ch)) { + if (cur !== "") tokens.push(cur); + cur = ""; + continue; + } + cur += ch; + } + if (cur !== "") tokens.push(cur); + return tokens; +} + test("ensureProxyRunning: spawns a fresh proxy when no live instance is recorded", async () => { let spawnCalls = 0; const spawnImpl: SpawnFn = () => { @@ -4211,6 +4235,18 @@ test("codexRunModePinned: detects user-pinned run mode (#1867)", () => { assert.equal(codexRunModePinned(["--remote=ws://127.0.0.1:9"]), true); }); +test("splitWindowsCommandLine: round-trips buildWindowsCommandLine quoting (#679/#1867)", () => { + // helper contract: the inner line, i.e. the comspec arg minus its outer quote pair + assert.deepEqual( + splitWindowsCommandLine(buildWindowsCommandLine("C:\\temp\\fake-codex.cmd", ["--no-daemon", "-c", "model_context_window=400000"]).slice(1, -1)), + ["C:\\temp\\fake-codex.cmd", "--no-daemon", "-c", "model_context_window=400000"], + ); + assert.deepEqual( + splitWindowsCommandLine(buildWindowsCommandLine("C:\\Users\\Some User\\temp\\fake-codex.cmd", ["--remote", "ws://127.0.0.1:9"]).slice(1, -1)), + ["C:\\Users\\Some User\\temp\\fake-codex.cmd", "--remote", "ws://127.0.0.1:9"], + ); +}); + test("codexSupportsNoDaemon: probes --help output, fails soft (#1867)", () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-nodaemon-")); const isWin = process.platform === "win32"; @@ -4263,19 +4299,27 @@ test("runLaunch codex: --no-daemon pinned when supported, escape hatches honored fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "gpt-5.5"\n'); const clientArgsSeen: string[][] = []; + const fakeBase = path.basename(fakeCodex); const spawnImpl: SpawnFn = (cmd, args) => { - if (cmd === fakeCodex) { - clientArgsSeen.push([...args]); - const child = makeFakeChild(0); - const orig = child.on!.bind(child); - (child as { on: SpawnChild["on"] }).on = (event, listener) => { - orig(event, listener); - if (event === "exit") setTimeout(() => listener(0, null), 0); - return child; - }; + // #679: runClient plans the spawn before calling us — on win32 a .cmd + // fake arrives as `comspec /d /s /c ""`, so the client path sits + // inside one arg instead of being cmd itself. + const wrappedLine = cmd !== fakeCodex + ? args.find((a): a is string => typeof a === "string" && a.includes(fakeBase)) + : undefined; + if (cmd !== fakeCodex && wrappedLine === undefined) return makeFakeChild(42422); + const clientArgs = wrappedLine === undefined + ? [...args] + : splitWindowsCommandLine(wrappedLine.slice(1, -1)).slice(1); + clientArgsSeen.push(clientArgs); + const child = makeFakeChild(0); + const orig = child.on!.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); return child; - } - return makeFakeChild(42422); + }; + return child; }; const fetchImpl = async () => ({ ok: true }); const prevExit = process.exit; @@ -4358,19 +4402,27 @@ test("runLaunch codex: old binary without --no-daemon launches unchanged (#1867) fs.writeFileSync(path.join(codexHome, "config.toml"), ""); const clientArgsSeen: string[][] = []; + const fakeBase = path.basename(fakeCodex); const spawnImpl: SpawnFn = (cmd, args) => { - if (cmd === fakeCodex) { - clientArgsSeen.push([...args]); - const child = makeFakeChild(0); - const orig = child.on!.bind(child); - (child as { on: SpawnChild["on"] }).on = (event, listener) => { - orig(event, listener); - if (event === "exit") setTimeout(() => listener(0, null), 0); - return child; - }; + // #679: runClient plans the spawn before calling us — on win32 a .cmd + // fake arrives as `comspec /d /s /c ""`, so the client path sits + // inside one arg instead of being cmd itself. + const wrappedLine = cmd !== fakeCodex + ? args.find((a): a is string => typeof a === "string" && a.includes(fakeBase)) + : undefined; + if (cmd !== fakeCodex && wrappedLine === undefined) return makeFakeChild(42422); + const clientArgs = wrappedLine === undefined + ? [...args] + : splitWindowsCommandLine(wrappedLine.slice(1, -1)).slice(1); + clientArgsSeen.push(clientArgs); + const child = makeFakeChild(0); + const orig = child.on!.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); return child; - } - return makeFakeChild(42422); + }; + return child; }; const fetchImpl = async () => ({ ok: true }); const prevExit = process.exit;