Skip to content

[PR] Running tests locally writes test sessions into the real ~/.local/billion-context — preload sandboxes the default direct | 本地跑测试会把测试会话写进真实的 ~/.local/billion-context——预载把默认目录沙箱化(Fixes #5114) - #5115

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-29_fix-test-pollution-real-dirs
Oct 29, 2026

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

(ai: zhipuai-lb/glm-5.2)

Fixes #5114(发现于 #5082 调查,与已合并的 #5091 是同一调查的两个独立问题)。

什么东西坏了

本地跑测试时,凡是没钉目录的 store/server 构造走默认 XDG 路径解析,直接写进真实的 ~/.local/{share,state,cache}/billion-context:tests/anthropic-cache-control.test.ts 一个文件就往生产 sessions/index.db 写 sess-1637/1639/1876-* 行、往 sessions/anthropic/ 写 127.0.0.1_*.json envelope、改写 state 目录的 prefix-affinity.json/rollout-id.json/proxy-origin。生产库混进 364+ 测试会话,面板/诊断把测试会话当真会话,#4323 drift 采样也被干扰。

为什么没拦住

npm test/CI 都加载 tests/preload-storage-baseline.mjs,但它只定了 BILI_STORAGE_UNIFIED=legacy(挡住 SQLite 行写入);envelope、state、cache 文件照走真实目录。裸 targeted 跑(node --import tsx --test …)连预载都不加载,行+文件全写。

怎么修的

预载里把三个 XDG 默认指到 per-run 临时树($TMPDIR/bili-test-dirs-*/{data,state,cache}):

  • ||= 赋值:显式设置的 CI 容器、自钉目录的测试(launcher/claude-native/doctor 等大量自设 XDG 的套件)完全不受影响;空串视同未设置,与 scripts/test-suite-lock.mjs 的 XDG 处理口径一致。
  • XDG_CONFIG_HOME 不动——配置读取行为不变。
  • 契约测试 tests/preload-sandbox-dirs.test.ts 钉住两方向:默认进沙箱 + 显式环境永远赢(负断言)。
  • reference/testing.md:targeted 跑法统一推荐带预载链的形式。

验证

验证项 结果
5 个指纹源文件(acp-status/absorb-emission/anthropic-cache-control/external-summary-runtime/issue1843-learned-image-cost)经完整预载链跑,真实目录零指纹文件落地 ✅ 80 pass
存储家族(storage-* / bounded-storage* / identity-registry / export-jsonl) ✅ 222 pass
自设 XDG 重灾批(launcher/claude-native/advisory/log-mask/dsh-native/update-desktop-inplace) ✅ 535 pass
契约测试(两方向) ✅ 2 pass
npm run typecheck ✅ 干净

已知本地噪音(与本改动无关):tests/doctor.test.ts 在本机带不带预载都同样失败(mock registry 断言),以 CI 为准(§7.2)。

边界

  • 生产库已有的脏数据(364+ 测试行/envelope)不在本 PR 清理——那是对生产数据的写操作,清理脚本待 owner 确认后单独执行。
  • 不新增 env var(显式设 XDG_* 即表达「故意用真实目录」),不碰 config surface。

Suites that build stores/servers without pinning a directory resolve
through the XDG defaults into the developer's real ~/.local/{share,state,
cache}/billion-context — envelopes, index.db rows, and state bookkeeping
land beside production data (found triaging #5082: a local run of
tests/anthropic-cache-control.test.ts wrote sess-1639-* rows and
sessions/anthropic/127.0.0.1_* envelopes into the live sessions dir; the
legacy storage baseline stops the SQLite rows but not the files).

The preload now points XDG_DATA_HOME/XDG_STATE_HOME/XDG_CACHE_HOME at a
per-run temp tree via ||= (empty counts as unset, matching the lock
script's XDG handling); explicit env and tests pinning their own dirs win
unchanged. Contract pinned by tests/preload-sandbox-dirs.test.ts;
reference/testing.md documents the recommended targeted-run form.
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-29_fix-test-pollution-real-dirs (69bfb3d)

Latest build — install this exact version

npm install -g billion-context@0.0.6734-pr.5115.46f057c

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
69bfb3d billion-context@0.0.6734-pr.5115.46f057c build
f53028c billion-context@0.0.6732-pr.5115.ee16401 build

Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title 本地跑测试会把测试会话写进真实的 ~/.local/billion-context——预载把默认目录沙箱化(Fixes #5114) [PR] Running tests locally writes test sessions into the real ~/.local/billion-context — preload sandboxes the default direct | 本地跑测试会把测试会话写进真实的 ~/.local/billion-context——预载把默认目录沙箱化(Fixes #5114) Oct 29, 2026
The new contract test spawns a child node process, so the run-ci-tests
SPAWN_RE tripwire requires a LIVE_FILES entry (#1248 discipline).
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm/qwen3.8-27b)

查重: 不重复。 与 #5091(已合并,#5082 的二次进程告警修复)同源于一次调查,但机制不同:#5091 修的是「第二个进程打开 index.db 被误报为损坏」的告警逻辑;本 PR 修的是「未钉目录的测试走 XDG 默认路径写进真实目录」的路径解析问题。两者无代码交集。另核对了 GitHub API:本 PR 的 Fixes #5114 指向的正是测试污染 issue(GitHub #5114),引用无误(#5113 才是 fish-mind)。

标签: bug + scope/ci + p2(测试基建缺陷:本地跑测试污染生产存储,有绕行——自钉目录的套件不受影响;非 p1,不属缓存泄露/不可用类)。

审核进行中:已拉取分支 2026-10-29_fix-test-pollution-real-dirs(f53028c,单 commit、3 文件,与描述一致)。CI test 车道在跑,本地定向验证完成后按八问给结论。

@ranxianglei ranxianglei added bug Something isn't working p2 scope/ci labels Oct 29, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm/qwen3.8-27b)

审核结论:可合入。 独立复现 + 定向验证均通过,CI 全绿(30/30,failed 0 / pending 0,含 kernel-guard、config-surface-guard、windows 车道)。以下为八问。

1. 是什么问题:本地跑测试时,没钉目录的 store/server 构造走 XDG 默认路径解析,把测试会话(envelope、index.db 行、state 簿记文件、缓存)写进开发者真实的 billion-context 目录,生产存储被测试数据污染。

2. 什么原因导致的:三层缺口叠加——①共享预载 tests/preload-storage-baseline.mjs 只钉了 BILI_STORAGE_UNIFIED ??= "legacy",挡住 SQLite 行写入,但 envelope/state/cache 文件仍走真实目录;②裸 targeted 跑(node --import tsx --test …)连预载都不加载,行+文件全写;③没有任何契约测试钉住「默认目录必须进沙箱」这个行为。

3. 影响范围与严重程度:所有在开发机跑套件的人——生产库累积数百个假会话,面板把测试会话当真会话展示,#4323 drift 采样被干扰。终端用户不受影响(纯测试基建);p2 定位准确。

4. 怎么修复的:在共享预载里把三个 XDG 默认根(data/state/cache)指到 per-run mkdtemp 临时树,用 ||= 保证显式环境永远赢(空串视同未设置,与 scripts/test-suite-lock.mjs 的 XDG 处理口径一致);XDG_CONFIG_HOME 不动;新增契约测试两方向钉死(默认进沙箱 + 显式环境赢)。修法打在根因层(路径解析入口),不是在各测试里逐个堵。

5. 是否有回归问题——逐项审过的既有行为:

  • 自钉 XDG 的套件(issue4778/responses-instructions-drift/mcp-native-thread/fork-side-request-lane 等几十个文件):全部是显式非空赋值或 delete 还原,||= 不覆盖 → 不受影响;
  • 净化 env 派生子进程的测试(build-info、claude-native e2e):子进程 env 里已显式钉全部四个 XDG_* 或展开 process.env → 不受影响;
  • suite-lock 并发锁:锁路径由父进程 runner(不加载预载)解析,仍在机器级 cache 目录 → 跨进程互斥语义不变;
  • 配置读取:XDG_CONFIG_HOME 未动 → 行为逐字节不变;
  • Windows:src/paths.ts 的 xdg() 跨平台生效,沙箱在 windows 车道同样工作(CI windows 绿)。
  • 行为变更披露(有意):未钉目录的套件从「拿到机器真实状态」变为「每 run 一棵全新空树」——这正是本 PR 目的;唯一理论风险是某套件依赖真实目录里的既有内容,但那本身就是非 hermetic 缺陷,CI 全量绿证明不存在这种依赖。

6. 是否破坏缓存:不涉及(未触碰任何缓存/重试/游标逻辑)。

7. 是否改了设计方向:不涉及——纯测试基建加固,无架构变化,未触碰 kernel/(kernel-guard 绿)。

8. 是否引入了新的配置:没有——PR 明确不新增 env var,显式设 XDG_* 即表达「故意用真实目录」;config surface 零变化(config-surface-guard 绿)。

我的独立验证记录(非转述作者表格):

  • 复现成立:裸跑(无预载)+ 隔离 HOME 下,tests/anthropic-cache-control.test.ts 8 pass,落地物与 issue 描述完全同构——8 个 anthropic envelope、index.db(+chk/lock)、prefix-affinity/rollout-id/proxy-origin、instances/writers 记录、bili.log、models-dev.json;
  • 修复成立:同一文件经完整 CI 预载链再跑,8 pass,同一套落地物全部进 $TMPDIR/bili-test-dirs-* 沙箱树,真实目录零写入(期间真实目录出现的 instance 记录变动经 pid+codeFingerprint 比对证实来自两个并发的无关代理进程,非测试所写);
  • 契约测试:2/2 pass(两方向断言都有牙——负方向真的会抓回归)。

边界处理认可:生产库存量脏数据(364+ 测试行/envelope)属对生产数据的写操作,留在本 PR 之外待你确认后单独清理,处置正确。Fixes #5114 引用无误(GitHub #5114 即测试污染 issue,#5113 是 fish-mind)。

判定:可合入,等你合并(我不代合)。

@ranxianglei
ranxianglei merged commit 9628bb5 into master Oct 29, 2026
82 of 117 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.903-master.9628bb5 (rolling master tag).

npm install -g billion-context@0.0.903-master.9628bb5  # 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 ranxianglei added the duplicate This issue or pull request already exists label Oct 29, 2026
ranxianglei pushed a commit that referenced this pull request Oct 29, 2026
…ad (#5107)

The preload pointed BILI_SESSIONS_DIR/XDG_* at mkdtemp subdirs that were
never created. An explicit BILI_SESSIONS_DIR pointing at a MISSING dir is not
the pristine default layout — #2180/#1937 keep that loud on purpose:
walkSessionFiles' top-level ENOENT propagates out of refreshIndex and
/__bili/overview 500s with 'failed to load session data'. Every suite that
boots a proxy and reads the web overview or sessions index failed
deterministically — CI run 40841436177 went red on all 10 shards; local repro
of shard 1/3 found exactly this signature (log line: overview failed: Error:
ENOENT scandir <tmp>/bili-test-*/sessions) in tests/issue2645-pck-probe.test.ts
(undefined pckSuppressions -> TypeError reading 'length') and
tests/upstream-alerts.test.ts (#1682 rows: 500 !== 200, banner timeout).

Create the five advertised dirs at preload time: an existing EMPTY dir is the
normal 'no sessions yet' state the read paths already handle, while the
override stays explicit (loud-on-misconfig semantics untouched).

A/B evidence: issue2645 file 1 fail -> 8/8 pass post-patch; upstream-alerts +
web-storage-banner + preload-sandbox-dirs (#5115 contract) + test-suite-lock
(leak guard) = 46/46 pass; typecheck clean.
ranxianglei pushed a commit that referenced this pull request Oct 29, 2026
#4599)

Master red at 1fad21d (run 40852517737, 6 shards): two mechanisms, both in
the #5107/#5115/#5120 hermetic data-root cluster.

1. preload-test-env.mjs pinned BILI_CONFIG_FILE to an absent file.
   configFile() (src/paths.ts) consults that env BEFORE the XDG default, so
   the pin shadowed every fixture that re-points XDG_CONFIG_HOME at its own
   dir and writes the default config path: the claude-native attach-gate /
   lane-priority / policy / url file-layer assertions failed on all shards,
   and the #3436 compat checks resolved mitm.domains=[] -> spawn path -> 60s
   fake-child timeout. The XDG_CONFIG_HOME pin already keeps the default
   config path inside the throwaway root; DELETE the env instead of pinning
   so fixtures are un-shadowed AND a developer's ambient pin is dropped. The
   isolation probe now asserts configFile() resolves UNDER the sandboxed
   XDG_CONFIG_HOME (same property, sibling-check style) instead of equality
   with the removed pin.

2. test-suite-lock.mjs snapshotSessionsTree keyed entries by raw
   path.relative(): Windows yields 'anthropic\\s.json' while the diff, the
   banner and the tests all compare '/'-separated paths (4 failures on the
   windows s2 shards). Normalize keys to '/' at the source.

Verified locally with the CI preload chain (targeted): claude-native +
#3436 + test-suite-lock suites 96/96 pass, 0 fail. Windows separator
behavior holds by construction (split(path.sep).join('/')); full verdict =
remote CI.
@github-actions github-actions Bot mentioned this pull request Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working duplicate This issue or pull request already exists p2 scope/ci

Projects

None yet

1 participant