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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag

### Fixes

- **Cross-process startup-window race: concurrent launches no longer double-spawn proxies (#707)**: `ensureProxyRunning` only attached when it found a *healthy* instance at probe time — between the launcher's spawn and the child's first successful listen there was zero on-disk evidence that a bring-up was in flight, so two launches inside that window (~1s+) each spawned their own child: two live proxies over one sessions dir (double writers), last-writer-wins instance file, and the extra proxy living until ITS OWN spawner exited. The starter now claims `<state>/proxy-starting` with O_EXCL before spawning (carrying `{token, pid, host, port, startedAt}`); a concurrent caller that sees an active marker (owner pid alive AND younger than SPAWN_WAIT_MS + 30s) waits for the starter's instance with the same probe as attach (pid alive + `/__bili/health` + instanceId match), bounded by SPAWN_WAIT_MS, and attaches on success; marker disappearance (starter gave up / crashed) breaks the wait early so the caller spawns immediately. The starter clears the marker on every terminal path via a token-checked clear; a hard crash leaves a stale marker that the dead-owner/TTL checks render inert. The O_EXCL claim itself is the cross-process arbiter: the pre-spawn read is only a fast path, so a loser of the claim (read→claim race, or a stale/garbage marker file left by a crash mid-write) re-checks and either waits for the live holder or removes-and-reclaims the slot — it never spawns blindly, and an unreadable marker file self-heals on the next launch instead of permanently degrading coordination. A final probe after the wait deadline catches a starter that succeeded in its last moments — its success path clears the marker, which otherwise looks identical to a failure bail. Incompatible-config fall-through is preserved (a waited-for instance that isn't compatible with our launch options still spawns, matching today's incompatible-healthy-instance behavior); explicit `bili start` bypasses the marker entirely (it's a user action, not auto-bootstrap). In-process dedup (`singleFlight`, #706) is untouched — this is the cross-process half.
- **DeepSeek thinking mode 400 fixed: turns are atomic, reasoning-drop self-disables on strict-echo upstreams (#684)**: two paths could ship a rebuilt request whose assistant `tool_calls` turn lost its `reasoning_content` — (1) the proxy's recent-zone carve kept the last tool-call of a multi-call turn visible while its reasoning run folded into the block (kernel boundary logic only pulled the FIRST companion message after reasoning), and (2) #651's reasoning-drop stripped reasoning from a closed compress turn even on upstreams that document thinking-mode `reasoning_content` as mandatory round-trip. DeepSeek rejects both with 400 (`The \`reasoning_content\` in the thinking mode must be passed back to the API`) and the turn dies. Fixes: the kernel now computes atomic turn groups (reasoning run + its full assistant burst + every result paired by `toolCallId` — all fold together or none do; a range that would split a turn is withdrawn with a warning, or rejected with an explanatory error when nothing else remains), the forward boundary pull covers the whole burst, and the reasoning-drop never fires when the session is known strict-echo — statically for `deepseek` upstreams, or learned from the first 400 whose body mentions `reasoning_content` (persisted on the session, self-healing like #552). Three wire-level exit sentinels (`[reasoning-pair-violated]` on chat/completions and Responses, `[thinking-pair-violated]` on Anthropic) log loudly if any future path reintroduces a split turn.
- **Windows: spaced client/extension paths no longer break launched clients or silently drop extensions (#679, PR #682)**: `runClient` spawned clients with `shell: true` and no per-token quoting, so on Windows cmd.exe re-split the joined line at the first space — a client binary under a spaced path (`C:\Users\John Doe\...`) died with `'C:\Users\John' is not recognized`, and a surviving client whose `-e <extension>` path was split launched WITHOUT the bili extension (compression silently never activates). Spawning is now routed by executable class: real `.exe`/node binaries spawn directly (the OS quotes argv, spaces intact), npm `.cmd`/`.bat`/extensionless shims go through an explicit `comspec /d /s /c "…"` line with correct per-token quoting (`windowsVerbatimArguments`); `shell: true` is gone (also silences Node's DEP0190 warning). Known edge: `BILI_CLIENT_BIN` pointing at a `.js` file now fails loudly instead of relying on file association via the old shell path.
- **Windows: codex MCP server delivered via CODEX_HOME overlay, not inline `-c` (#681, PR #683)**: cmd.exe cannot transmit a literal `"` to a child, but every valid TOML string needs one — so the inline `-c mcp_servers.bili.command="C:\\Program Files\\nodejs\\node.exe"` arg arrived at codex as an unquoted, space-split, invalid TOML value for ANY absolute Windows path (spaced stock install just the common case), the bili MCP server never registered, and plugin-mode compression silently never activated. On `win32` the launcher now writes `[mcp_servers.bili]` (command/args/env.BILI_MCP_PROXY/env.BILI_CONVERSATION_ID) into a persistent `<CODEX_HOME>-bili` overlay's merged `config.toml` — sharing every other real-home entry (auth.json, sessions, model settings) — and points the spawned codex at it via `CODEX_HOME`. POSIX keeps the working inline `-c` path byte-identical; if the overlay can't be built the launch degrades to wire mode (server-side compression still active) with a warning. Real-Windows end-to-end verification tracked in #686.
Expand Down
85 changes: 85 additions & 0 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,91 @@ export function clearProxyInstanceFile(instanceId: string, file?: string): void
}
}

/** Cross-process starting marker (#707): while a launcher sits between spawn
* and healthy, no instance record exists yet, so concurrent launches see
* nothing to attach to and double-spawn — two writers over one sessions dir.
* The starter claims this file with O_EXCL before spawning; concurrent
* callers find it and wait for the bring-up instead of spawning their own. */
export interface ProxyStartingMarker {
token: string;
pid: number;
host: string;
port: number;
startedAt: number;
}

export function startingMarkerPath(): string {
return path.join(stateDir(), "proxy-starting");
}

export function readStartingMarker(file?: string): ProxyStartingMarker | undefined {
let raw: string;
try {
raw = fs.readFileSync(file ?? startingMarkerPath(), "utf8");
} catch {
return undefined;
}
try {
const parsed = JSON.parse(raw) as Partial<ProxyStartingMarker>;
if (typeof parsed.token !== "string" || parsed.token === "") return undefined;
if (typeof parsed.pid !== "number" || !Number.isInteger(parsed.pid) || parsed.pid <= 0) return undefined;
if (typeof parsed.startedAt !== "number") return undefined;
return {
token: parsed.token,
pid: parsed.pid,
host: typeof parsed.host === "string" ? parsed.host : "",
port: typeof parsed.port === "number" ? parsed.port : 0,
startedAt: parsed.startedAt,
};
} catch {
return undefined;
}
}

/** Claim the marker atomically across processes (O_EXCL create — a rename-based
* write would leave the same race at the marker level). Returns false when a
* claimant already holds it or the state dir is unwritable; coordination then
* degrades to the pre-#707 behavior instead of failing the launch. */
export function claimStartingMarker(marker: ProxyStartingMarker, file?: string): boolean {
const filePath = file ?? startingMarkerPath();
let descriptor: number | undefined;
try {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
descriptor = fs.openSync(filePath, "wx", 0o644);
fs.writeSync(descriptor, JSON.stringify(marker) + "\n", null, "utf8");
fs.fsyncSync(descriptor);
descriptor = undefined;
return true;
} catch {
if (descriptor !== undefined) {
try {
fs.closeSync(descriptor);
} catch {}
}
return false;
}
}

/** Remove the marker only if it still carries OUR token (a newer claimant may
* have taken it over). Mirrors clearProxyInstanceFile. */
export function clearStartingMarker(token: string, file?: string): void {
const filePath = file ?? startingMarkerPath();
const current = readStartingMarker(filePath);
if (current && current.token === token) {
try {
fs.unlinkSync(filePath);
} catch {}
}
}

/** Unconditional best-effort removal — used only on markers the caller has
* already verified stale (dead owner / expired). */
export function removeStartingMarker(file?: string): void {
try {
fs.unlinkSync(file ?? startingMarkerPath());
} catch {}
}

/** pid liveness (kill-0). EPERM means the process exists but is owned by
* another user — still alive. */
export function isPidAlive(pid: number): boolean {
Expand Down
Loading
Loading