Skip to content

[PR] In-place auto-update 100% failure fix: Backup must not exist prior to atomic swap (Gate Lane X repro) | 就地自动更新 100% 失败修复:备份不能先于原子交换存在(门禁 Lane X 复现) - #6643

Merged
ranxianglei merged 1 commit into
masterfrom
fix/update-inplace-backup
Nov 14, 2026

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

本地自动更新(bili update)在全真门禁里 100% 失败:旧安装永远替换不掉

这是啥问题

全真模型门禁(Lane X,本机 8299 sglang 真模型)里,代理自动从 0.1.213 更新到 0.1.214-gate.0 时下载成功、但落盘失败:

[update] install failed: backup of current install failed (install left untouched):
SystemError [ERR_FS_CP_EINVAL]: cannot copy .../node_modules/mime/cli.js
to a subdirectory of self .../node_modules/mime/cli.js

结果:npm 布局(真实 node_modules)安装的就地更新必然失败,每次 bili update 都原地不动地报这个错。门禁 4 项 X 检查里挂 2 项。

根本原因

src/update.ts 的 #6309 原子交换改造(30fb96fb0)把「先备份旧安装」放在了原子交换之前:

  1. 先 cp(installDir, backupDir) —— backup 目录变非空;
  2. 原子交换 rename(installDir, backupDir) 要求目标目录不存在,非空必 ENOTEMPTY → 原子路径永远走不到;
  3. 回退路径又对着已有内容的 backup 再 cp 一次,而 npm 的 node_modules/.bin/* 是相对符号链接(指向同目录树内部)→ libuv 的 self-copy 保护直接 EINVAL。

门禁容器里逐步复现:第一次 cp 成功 → rename ENOTEMPTY → 第二次 cp EINVAL,与线上报错一字不差。

修复

文件备份只在回退(merge-copy)路径里、对着全新目录做;原子路径继续用被 rename 到一边的旧安装目录作为回滚源(行为不变)。改动集中在 src/update.ts 的 installViaTarball 一个函数内。

新增 tests/update-inplace-symlink-layout.test.ts 两个钉子:

  • 相对 .bin 符号链接布局下连续两次就地更新必须成功(旧代码上红、本提交后绿,报错串与门禁一致);
  • 坏产物(版本漂移)必须干净回滚到旧版本。

46/46 既有 update 套件全绿。

验证

  • 全真门禁 Lane X(2026-11-14 复跑)复现上述失败;修复后待门禁复跑确认(X 4/4)。
  • 该问题 master-only(36a04a798 区间内的 30fb96f),v0.1.214 发布分支不受影响,无需进发布分支。

…ap (#6309 regression)

The #6309 atomic-swap rewrite (30fb96f) copied the current install into
backupDir BEFORE attempting the rename(installDir, backupDir) swap. A
non-empty backup target makes that rename fail ENOTEMPTY every time, so
the atomic path could never succeed, and the fallback then re-copied the
install onto the EXISTING backup tree — where libuv's self-copy guard
aborts on npm's relative node_modules/.bin symlinks (EINVAL /
"cannot copy ... to a subdirectory of self").

Net effect: every in-place auto-update on an npm-layout install (real
node_modules, as npm i -g lays it down) failed with
"backup of current install failed (install left untouched)" and the
stuck version kept running. Found by the local real-model gate, Lane X
(0.1.213 -> 0.1.214-gate.0 install), master re-review 2026-11-14.

Fix: create the file backup only on the fallback merge-copy path, into a
FRESH destination; the atomic path keeps using the renamed-aside install
as its rollback source (behavior unchanged).

Pinned by tests/update-inplace-symlink-layout.test.ts: two consecutive
in-place updates over a relative-.bin-symlink layout + corrupt-artifact
rollback. Red on the old code (same error string as the gate), green
after the fix; 46/46 update suites green.
@ranxianglei ranxianglei changed the title 就地自动更新 100% 失败修复:备份不能先于原子交换存在(门禁 Lane X 复现) [PR] In-place auto-update 100% failure fix: Backup must not exist prior to atomic swap (Gate Lane X repro) | 就地自动更新 100% 失败修复:备份不能先于原子交换存在(门禁 Lane X 复现) Nov 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: fix/update-inplace-backup (6bea05b)

Latest build — install this exact version

npm install -g billion-context@0.0.9841-pr.6643.cdd7cf6

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
6bea05b billion-context@0.0.9841-pr.6643.cdd7cf6 build

Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

查重:不重复(本 PR 针对 #6309 原子交换改造引入的就地更新回归,门禁 Lane X 已逐步复现钉死机制)。

上一会话因 35 分钟无输出被强制重启,现汇报验证进度——代码与 CI 核查已完成,结论如下。

独立验证(未照抄 PR 描述,重新做过):

  1. 代码审查:diff 仅 src/update.ts 的 installViaTarball(29 行)+ 新测试文件(114 行)。修复逻辑正确:文件备份只在回退(merge-copy)路径、对着全新目录做;原子路径继续用被 rename 到一边的旧安装作回滚源。边界情形逐一走过:原子交换第二步 rename 失败时先 rename 回原位并置 renamedBackup=false,再进回退路径,备份仍对着空目录,不会 EINVAL。
  2. 本地定向红绿对照:
    • master(旧代码)跑新测试 tests/update-inplace-symlink-layout.test.ts → 红,报错串与门禁一字不差:ERR_FS_CP_EINVAL: cannot copy .../node_modules/mime/cli.js to a subdirectory of self ... + backup of current install failed (install left untouched)——证明测试真钉住此 bug;
    • PR head(6bea05b)→ 2/2 绿(连续两次就地更新成功 + 坏产物干净回滚到旧版本)。
  3. CI:主 CI 两批 run 均被取消(03:45:53Z 五个 run 同时被取消,非代码失败)——我已重跑,主 CI 绿(05:07:10Z 完成);batch 2 被取消的 4 条 e2e 车道(dsh/fake-codex/local-registry/pi-codex-ws)也已重跑(进行中;这 4 条在 batch 1 同 sha 上全绿)。
  4. master 漂移:PR base(36a04a798)之后 master 只进了 [PR] fix: watermark fastpath (plugin full-resend watermark fastpath) silently disabled by input release mutation | fix: 水印快路径(plugin full-resend watermark fastpath)被输入释放改动静默禁用 #6640(watermark fastpath,与本 PR 无交集,未碰 src/update.ts)——无冲突。

结构化八问:

  1. 是什么问题:npm 布局安装(真实 node_modules + 相对 .bin 符号链接)上的就地自动更新(bili update)100% 失败——下载成功但落盘必败,旧安装永远替换不掉,每次报 "backup of current install failed (install left untouched)"。
  2. 什么原因导致的:[Bug][update] In-place self-update is not atomic: if the process dies mid-copy, the install directory ends up with a mix | [Bug][update] 原地自更新不是原子的:拷到一半进程死掉,安装目录会新旧混杂(或连入口都没有),而备份从来不会被拿来恢复 #6309 原子交换改造(30fb96fb0)把「先备份旧安装」放在原子交换之前:(a) cp(installDir→backupDir) 后 backup 非空,而 rename(installDir→backupDir) 要求目标不存在 → 永远 ENOTEMPTY → 原子路径永远走不到;(b) 回退路径对已有内容的 backup 再 cp,npm 相对 .bin 符号链接触发 libuv self-copy 保护 → EINVAL。
  3. 影响范围与严重程度:master(30fb96fb0..36a04a7 区间)上所有 npm 布局安装——就地自动更新完全不可用(永远停在旧版本);代理本体不受影响(install left untouched),无数据/会话损失 → 单功能失效,p2 级;v0.1.214 发布分支不受影响。
  4. 怎么修复的:文件备份移到只在回退(merge-copy)路径、对着全新目录做;原子路径回滚源不变(被 rename 到一边的旧安装);改动限定单函数内。
  5. 是否有回归问题:逐项审过——pnpm-link 车道(不做文件备份、被挪开的 link 即回滚源)不变;原子路径成功/回滚语义不变;merge-copy 路径「备份失败→install 不动」的 fail-closed 保留;post-install 版本验证 + boot smoke 回滚不变。唯一行为变化是「备份对着全新目录而非已有目录创建」,严格更稳(旧行为在该布局下必然坏,不存在可回归的旧语义)。
  6. 是否破坏缓存:不涉及(未触碰缓存/重试/游标/去重逻辑)。
  7. 是否改了设计方向:无——原子 rename 优先、merge-copy 兜底的双路径设计不变,只修正备份时机。
  8. 是否引入新配置:无。

判定:代码 + 定向红绿验证 + 主 CI 全绿;4 条 e2e 重跑进行中(同 sha batch 1 全绿,风险低)。重跑完成后本 PR 可合。全真门禁 Lane X 复跑(X 4/4)按 PR 描述仍待执行。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

CI 收尾:4 条 e2e 重跑车道全部完成——全绿。当前 PR head(6bea05b)上所有 check 状态:主 CI ✅、Windows codex overlay ✅、e2e-native-pi / e2e-codex-persona / e2e-native-opencode / e2e-native-dsh / e2e-fake-codex / e2e-local-registry / e2e-pi-codex-ws ✅、PR Build Artifact ✅。

CI 侧已无阻塞,PR 可合。剩余事项:全真门禁 Lane X 复跑确认(X 4/4),按 PR 描述由门禁侧执行。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

收到重启通知——核实过线程实况:本条通知对应的是上一轮会话的收尾时刻(05:29Z 已正常 completed,29 min),工作已全部交付并汇报,无中断、无遗留操作。

当前状态(刚核实):

  • 代码 + 定向红绿验证 + 全部 CI check 绿(主 CI、Windows overlay、7 条 e2e 车道、Build Artifact)——详见上面两条 [bot] 回复;
  • PR 仍 OPEN,未合并(合并权在 owner);
  • 剩余事项只有一项:全真门禁 Lane X 复跑确认(X 4/4),由门禁侧执行。

无新问题、无阻塞,本线程 agent 侧工作到此结束。

@ranxianglei
ranxianglei merged commit 1ff89e8 into master Nov 14, 2026
59 of 98 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1192-master.1ff89e8 (rolling master tag).

npm install -g billion-context@0.0.1192-master.1ff89e8  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

@ranxianglei

Copy link
Copy Markdown
Owner Author

补充:Windows 真机验证(2026-11-14,Win10 22H2 VM,Node v22.23.2 / npm 10.9.8,宿主 verdaccio 私有 registry)

按用户要求在 Windows 虚拟机上对修复后的 master(1ff89e878,含本 PR)做了端到端就地自更新实测:

  1. 干净更新:npm i -g billion-context@0.1.214-win.1 → bili update(registry latest = 0.1.216-win.1)→ exit 0,bili --version 从 0.1.214-win.1 跳到 0.1.216-win.1。✅
  2. 重复更新无变化:再跑一次 bili update(已是最新)→ exit 0,版本不动,无状态残留。✅
  3. 运行中更新([Bug][update] In-place self-update is not atomic: if the process dies mid-copy, the install directory ends up with a mix | [Bug][update] 原地自更新不是原子的:拷到一半进程死掉,安装目录会新旧混杂(或连入口都没有),而备份从来不会被拿来恢复 #6309 原始场景):先 bili start --port 18991 让代理从安装目录运行(health 显示 v0.1.216-win.1, pid 4676),再 bili update(latest = 0.1.217-win.1)→ exit 0;安装目录版本升到 0.1.217-win.1,运行中的代理全程健康、无新旧混杂(同一 pid/instanceId 继续按 0.1.216 服务,重启后自然拿新码,launcher 既有语义);%TEMP% 无 .update-wip-*/backup 残留,安装目录树完整。✅
  4. 新构建(含 pure-SQLite 默认存储)在 Windows 上启动正常:unified FULL mode ENABLED [engine: node:sqlite/builtin],日志无 [update] 错误。

修复前后行为对照:修复前该代码路径在 npm 布局(相对 .bin 链接)上 100% 失败(容器内已复现 EINVAL),Windows 真机上修复后三条路径全部通过。

ranxianglei added a commit that referenced this pull request Nov 14, 2026
[PR] ci: add real npm layout e2e for self-update (#6643 regression class now covered by CI) | ci: 自更新补真实 npm 布局 e2e(#6643 回归类从此 CI 兜底)
Louie0806 pushed a commit to Louie0806/billion-context that referenced this pull request Nov 14, 2026
…6643 regression class)

The update code paths were only covered by unit tests with hand-built
directories and by the real-model gate Lane X, which materializes a REAL
npm global install. ranxianglei#6643 (pre-ranxianglei#6340 atomic-swap rewrite) made every
in-place update on an npm-layout install fail 100% — cp-onto-existing-
backup hit libuv's self-copy guard on the relative node_modules/.bin
symlinks — and no CI lane caught it.

New tests/e2e/e2e-selfupdate-cli.test.ts in the existing hermetic
registry lane (ACP_TEST_REGISTRY=1, ci-registry.yml, ubuntu+windows):
publish a fixture package with a bin-bearing dependency (so npm lays
down a real relative .bin symlink tree), npm install -g it, then run
the REAL CLI 'bili update' and assert the version advances on disk +
in 'bili --version', plus a clean no-op second update. Verified:
- green on master (fix in place) in the bili-gate container (node 22 / npm 10)
- red with ranxianglei#6643 reverted: exact gate failure signature
  'backup of current install failed ... ERR_FS_CP_EINVAL ... node_modules/.bin/mime'
- green locally (node 25 / npm 11 writes absolute .bin links — trap inert, flow still exercised)
@github-actions github-actions Bot mentioned this pull request Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant