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 @@ -88,6 +88,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag
independent #1729 wire-level dsh-compaction refusal, the #2709 framing-absent rewrite attribution, and the
#2695 majority-absent zombie reap (its `orphan-reap` ledger record); none of those paths are touched here.
Goldens: metadata-only drift, exactly the new latch.
- **Self-restart: replacement no longer hops to the next port when the target port is occupied (#3044)** — with `autoRestartOnUpdate`, a replacement spawned while the old instance still held its port (or a racing process took it during the drain window) inherited the launcher's EADDRINUSE ladder and silently drifted to P+1, P+2, …: idle orphans serving no client while the prior instance never exited (reported shape: 14 orphans over 9 h, ~8 GB private memory combined). Now the replacement is marked at spawn (`BILI_UPGRADE_REPLACEMENT=1`, internal spawn channel) and must bind EXACTLY the freed port — after the bounded same-lane predecessor wait of #1723 — or fail fast with exit code 1 and a clear log line ("upgrade pending; the host decides retry"). The parent verifies the handover against the replacement's own registry record (pid + port it ACTUALLY bound) instead of a raw TCP answer: bound the wrong port → terminated, kill verified, service resumes; another live bili instance already owns the port → the upgrade counts as landed there and the parent exits cleanly instead of resuming an old-code copy that would ladder-drift into its own zombie; a kill that does not take effect escalates loudly with a manual-cleanup hint. A foreign non-bili squatter keeps today's resume behavior. A lane'd spawn that races a LIVE same-build instance on the same port (the host-respawn trigger path of the 2026-10-13 recurrence) now exits loudly instead of hopping: the duplicate loses, the serving instance keeps serving, and the next launch attaches to it. Tests: tests/listen-policy.test.ts (policy matrix), auto-restart.test.ts (marker env, misplaced termination, supersede-exit, unkillable escalation), port-zone-live.test.ts (live fail-fast end-to-end, CI lane).
- **Storage: drop the `better-sqlite3` optional dependency (#2823)** — the native SQLite engine lived in `optionalDependencies`, but pnpm ≥10's strict build-script gate (`strictDepBuilds`) blocks any unlisted install script and treats it as a hard error, so dsh plugin-market installs/updates on fresh profiles failed with `[ERR_PNPM_IGNORED_BUILDS] better-sqlite3`. Unified storage is OFF by default (legacy file persistence), so dropping the optional native engine is low-impact: the driver already prefers built-in `node:sqlite` when the native module is absent, and when no engine is available at all `UnifiedShadow.maybeOpen` degrades to legacy-only (one warn, never bricks). New tests pin the no-engine contract (`tests/storage-engine-proofs.test.ts`: `maybeOpen` → null; `SessionStore` runs legacy-only, writes survive, no `index.db`). The opportunistic `require("better-sqlite3")` probe is kept, so hosts that still have the native module keep using it. The dsh-side fix (downgrading build-blocking of *optional* deps to non-fatal) is tracked separately.
- **Inbound markers: one declarative engine for all four wires (#2789, #2798, #2811 plan B)** —
the per-protocol inbound stripping calls (panel → status markers → chain carriers, each wired
Expand Down
1 change: 1 addition & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed
| `BILI_MCP_PROXY` | string | unset | — | Spawn channel: MCP proxy target for plugin-hosted tools. |
| `BILI_PARENT_PID` | number | unset | — | Spawn channel: parent process id for lifecycle supervision. |
| `BILI_STRICT_PORT` | number | unset | — | Spawn channel: exact-port requirement (squatters refused loudly instead of hopping). |
| `BILI_UPGRADE_REPLACEMENT` | boolean-ish | unset | — | Spawn channel: marks a self-restart replacement — it must own exactly the freed port or exit without hopping (#3044). |
| `BILI_OPENCODE_ACP_SPEC` | string | unset | — | Spawn channel: ACP spec marker for opencode-native lanes. |
| `BILI_LAUNCHER_MODEL_WINDOWS` | string | unset | — | Launcher channel: model-window overrides passed into spawned clients. |
| `BILI_LAUNCHER_LANE` | string | unset | — | Launcher channel: lane identity of the spawning bili process. |
Expand Down
1 change: 1 addition & 0 deletions CONFIGURATION.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@
| `BILI_MCP_PROXY` | string | unset | — | 派生通道:插件宿主工具的 MCP 代理目标。 |
| `BILI_PARENT_PID` | number | unset | — | 派生通道:父进程 id,用于生命周期监管。 |
| `BILI_STRICT_PORT` | number | unset | — | 派生通道:精确端口要求(被占用时响亮拒绝而非跳端口)。 |
| `BILI_UPGRADE_REPLACEMENT` | boolean-ish | unset | — | 派生通道:标记自重启替换进程——必须独占被释放的精确端口,否则直接退出而非跳端口(#3044)。 |
| `BILI_OPENCODE_ACP_SPEC` | string | unset | — | 派生通道:opencode 原生车道的 ACP 规格标记。 |
| `BILI_LAUNCHER_MODEL_WINDOWS` | string | unset | — | 启动器通道:传入被派生客户端的模型窗口覆盖。 |
| `BILI_LAUNCHER_LANE` | string | unset | — | 启动器通道:派生方 bili 进程的车道身份。 |
Expand Down
22 changes: 22 additions & 0 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ export function findSameLanePredecessor(
return undefined;
}

/** #3044 follow-up: a live holder on the same lane running OUR build — a
* twin, not a predecessor. Nothing is draining (it is healthy and serving),
* so waiting cannot help; laddering +1 strands the newcomer as an orphan
* nobody routes to while the twin keeps serving (the second trigger path of
* #3044: a host respawn racing a live instance). The newcomer must exit
* loudly instead — the duplicate loses, the serving instance wins. Missing
* fingerprints never match: an unknown build is not our twin. */
export function findSameLaneTwin(
entries: RegistryEntry[],
port: number,
lane: string | undefined,
ownFingerprint: string | undefined,
): RegistryEntry | undefined {
if (!lane || !ownFingerprint) return undefined;
for (const e of entries) {
if (e.port !== port || e.lane !== lane) continue;
if (!isPidAlive(e.pid)) continue;
if (e.codeFingerprint === ownFingerprint) return e;
}
return undefined;
}

/** #1225: content identity of a bili entry script (sha256 of its bytes).
* The spawned child records this for ITS script; an attaching launcher
* compares it against the hash of the script it would spawn — so "same
Expand Down
73 changes: 73 additions & 0 deletions src/listen-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* EADDRINUSE retry policy for the proxy's own listen socket (#407/#964/#1723/#3044).
* Pure decision table extracted from the server's 'error' handler so every
* branch can be pinned without booting a server.
*
* #3044: a self-restart replacement must own EXACTLY the port its predecessor
* freed. Hopping +1 strands it serving no client while the prior instance
* keeps holding the real port — the orphan-leak shape reported in #3044. It
* gets the bounded same-port predecessor wait (#1723 overlap: slow release),
* then fails fast instead of laddering.
*
* #3044 follow-up: a live holder running OUR build on the same lane is a
* twin, not a predecessor — nothing is draining, so the newcomer exits loudly
* instead of hopping (the duplicate loses; the serving instance keeps serving
* and the next launch attaches to it). Applies to marked AND unmarked spawns.
*/
/** Internal spawn-channel env set by performSelfRestart onto its replacement
* (#3044): the marked process must bind-or-die on exactly the freed port. */
export const UPGRADE_REPLACEMENT_ENV = "BILI_UPGRADE_REPLACEMENT";

type EaddrinuseAction =
| { kind: "wait-predecessor"; port: number }
/** nextPort 0 = ask the OS for an ephemeral port (ladder exhausted). */
| { kind: "ladder"; nextPort: number }
| { kind: "fail-fast"; reason: "upgrade-replacement" | "strict-port" | "port-busy" | "twin-collision" };

export interface EaddrinuseContext {
/** #3044: this process is a self-restart replacement (BILI_UPGRADE_REPLACEMENT). */
upgradeReplacement: boolean;
/** #3044 follow-up: a live same-lane instance running our build holds the port. */
twinHolds: boolean;
launchToken: boolean;
strictPort: boolean;
attempts: number;
maxAttempts: number;
predecessorTicksLeft: number;
predecessorHolds: boolean;
lastTriedPort: number;
}

export function decideEaddrinuse(ctx: EaddrinuseContext): EaddrinuseAction {
// The twin rule outranks everything else: a healthy same-build peer is
// serving real traffic on that port, so no wait/ladder variant may touch
// it — the newcomer dies loudly regardless of its own spawn rights.
if (ctx.twinHolds) return { kind: "fail-fast", reason: "twin-collision" };
if (ctx.upgradeReplacement) {
if (ctx.predecessorTicksLeft > 0 && ctx.predecessorHolds) {
return { kind: "wait-predecessor", port: ctx.lastTriedPort };
}
return { kind: "fail-fast", reason: "upgrade-replacement" };
}
if (!ctx.launchToken || ctx.strictPort || ctx.attempts >= ctx.maxAttempts) {
return { kind: "fail-fast", reason: ctx.strictPort ? "strict-port" : "port-busy" };
}
if (ctx.predecessorTicksLeft > 0 && ctx.predecessorHolds) {
return { kind: "wait-predecessor", port: ctx.lastTriedPort };
}
const nextAttempts = ctx.attempts + 1;
return { kind: "ladder", nextPort: nextAttempts === ctx.maxAttempts ? 0 : ctx.lastTriedPort + 1 };
}

/** #3044/#2941: ladder rights for the POST-BIND claim check (port-owner's
* resolvePortClaim). A launcher-spawned child that finds a foreign live
* listener on its just-bound port may move +1 off the contested port (#2941);
* strict-port and manual launches never did. A marked upgrade replacement must
* not either, even though it inherits its parent's launch token: hopping is
* exactly the orphan shape #3044 kills, and this is the second path where a
* replacement could hop — on socket semantics that tolerate a concurrent bind
* onto an actively-listening port, a successful bind does not exclude the
* still-draining predecessor or a racing host respawn (#707/libuv#1360). */
export function postBindCanLadder(hasLaunchToken: boolean, strictPort: boolean, upgradeReplacement: boolean): boolean {
return hasLaunchToken && !strictPort && !upgradeReplacement;
}
Loading
Loading