Skip to content

feat: instance governance — attach, liveness, handshake, graceful exit, atomic state (#417) - #419

Merged
ranxianglei merged 2 commits into
masterfrom
2026-08-31_instance-governance
Aug 31, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-08-31_instance-governance

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #394, fixes #403, fixes #407, fixes #414, fixes #410, fixes #406. Umbrella design: #417.

What changed (six layers, one keystone)

L1 — identity + liveness (#403 read side)

  • proxy-origin file upgraded to JSON {origin, instanceId, pid, startedAt, host, port, passthrough, mitmDomains, modelWindows, launchToken} — atomic write (tmp+fsync+rename), legacy plain-string still readable (src/instance.ts)
  • /__bili/health echoes {instanceId, pid, startedAt} so readers verify they are talking to the recorded instance, not a port-squatter

L2 — attach-or-spawn (#394)

  • ensureProxyRunning now probes the recorded instance first: healthy + config-compatible (host/passthrough/mitmDomains/modelWindows) → attach and reuse, no second writer over the same sessions dir
  • stateDir()/instances.json registry: every proxy registers at listen, dead pids pruned, a second live instance triggers a loud [instances] warning

L3 — handshake kills the TOCTOU (#407)

  • No more parent-side probe-release-rebind: the child binds the port itself and, when launched with BILI_LAUNCH_TOKEN, retries EADDRINUSE (port+1 ×16 → ephemeral) instead of dying
  • The launcher waits on the child's instance-file record (token match) and picks up the REAL port
  • Forced --debug on spawned proxies removed (12MB/day)

L4 — graceful exit (#414)

  • Launcher children carry BILI_PARENT_PID and run the existing flush path when the parent disappears (≤2s) — Windows' TerminateProcess never gave them a chance
  • stopProxy: win32 no longer hard-kills (the watcher owns shutdown); attached instances are never killed

L5 — state hygiene (#406, #403 write side)

  • plugin-conversations.json: atomic writes + dirty flag (a dying EADDRINUSE path no longer rewrites the table) + corrupt bytes are preserved beside the original with a warning instead of silently zeroing every route
  • bili plugin install refuses to freeze a dead/unverifiable origin into persistent client configs (BILI_MCP_PROXY env still wins)

L6 — overlay antidote (#410)

  • mergeOverlayEntry threads the generatedFile exclusion through directory recursion — a nested models.yml can no longer be promoted into the real home
  • Dead proxy URLs already baked into a real models.yml are unpacked mechanically on launch (the original upstream is embedded in the /bili/ path); live origins are kept (deliberate power-user wiring)

Verification

  • typecheck ✓ / 881 tests, 881 pass ✓ (incl. 9 new instance-governance tests + 4 new launcher attach/handshake tests) / build ✓
  • Real-process smoke (isolated state dirs):
    • preferred port held → child rebound 18787→18788 and handshake delivered the real port ✓
    • second launch with compatible config → attaching to running proxy (pid …), zero spawns ✓
    • incompatible config (different modelWindows) → new instance spawned, busy port yielded to 18791, registry showed both + dual-writer warning ✓
    • parent exit → child flushed and cleared its instance record by itself ✓
    • SIGTERM → instance file + registry entry removed ✓

Notes for reviewers

…t, atomic state (#417)

L1 identity: proxy-origin file upgraded to JSON {origin, instanceId, pid,
startedAt, config fingerprint, launchToken} with atomic writes and legacy
plain-string read compatibility; /__bili/health echoes instanceId/pid/startedAt
(#403 read side).

L2 attach-or-spawn (#394): ensureProxyRunning attaches to a healthy,
config-compatible instance instead of doubling writers over one sessions dir;
stateDir instances.json registry prunes dead pids and warns loudly on dual
live instances.

L3 handshake (#407): no more probe-release-rebind TOCTOU — the child self-
binds, retries EADDRINUSE (+1 up to 16, then ephemeral) when launched with
BILI_LAUNCH_TOKEN, and reports the real origin via the instance file; forced
--debug on spawned proxies removed (12MB/day logs).

L4 graceful exit (#414): launcher children watch BILI_PARENT_PID and run the
flush path when the parent dies (Windows TerminateProcess never flushed);
stopProxy is a no-op kill on win32 (the watcher owns shutdown) and leaves
attached instances alone.

L5 state hygiene (#406, #403 write side): plugin-conversations.json gets
tmp+rename atomic writes, a dirty flag (dying paths no longer rewrite the
table), and corrupt-byte preservation + warning instead of silent zeroing;
plugin install refuses to freeze dead/unverifiable origins into client
configs (BILI_MCP_PROXY still wins).

L6 overlay antidote (#410): mergeOverlayEntry threads the generatedFile
exclusion through directory recursion (nested models.yml can no longer be
promoted into the real home); dead proxy URLs baked into a real models.yml
are unpacked mechanically on launch (live origins kept).
@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-08-31_instance-governance (43cc106)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-419

Each push to this PR publishes a new version under the pr-419 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr419.tgz
npm install -g package

This comment is automatically updated on each push.

…rs to the parent-gone watcher

CI caught the stale assertion: win32 stopProxy is intentionally a no-op
(child.kill is TerminateProcess, zero flush); the test now pins that
contract on both platforms instead of assuming POSIX behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

真实 models.yml 可被塞进死端口代理地址且永不解包:"real file never touched" 前提被嵌套 merge-back 递归与遗留插件写破,裸 omp 永久拨死号 launcher 端口 TOCTOU(探测-释放-重bind 无重试):可静默挂上陌生 bili 或 20s 挂死;且被启动代理强制 --debug 制造 12MB/天日志 minCompressRange: user docs specify tokens, kernel executes in chars (4× unit mismatch); note appended: registryLookup conflicts silently fall back. | minCompressRange 用户文档写 tokens、内核按 chars 执行(4× 单位缺口);附 registryLookup 冲突即静默回落的备注 ACP_PASSTHROUGH one-click silent kill of all compression: no logs / no banners / panel invisible and non-clearable, all sibling env knobs take effect in silence | ACP_PASSTHROUGH 一键静默关死全部压缩:无日志/无横幅/面板不可见且不可清除,同族 env 旋钮全部生效于无声 无单实例守卫:多个 bili 代理进程并存共写 sessions 目录,伴随孤儿 ref、有注册表无 transcript、计数自相矛盾的污染簇

1 participant