Skip to content

fix: cross-platform body-dump paths via XDG state dir (#864) - #865

Merged
ranxianglei merged 3 commits into
masterfrom
2026-09-17_cross-platform-paths
Sep 17, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-09-17_cross-platform-paths

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Issue #864 asked to find and fix all hardcoded Linux directories so bili works cross-platform, following the XDG spec where applicable.

Investigation result: src/ contained no literal /tmp — every temp-file site already uses os.tmpdir(), and src/paths.ts already implements the XDG base dirs (XDG_CONFIG_HOME/XDG_DATA_HOME/XDG_CACHE_HOME/XDG_STATE_HOME with ~/.config, ~/.local/share, ~/.cache, ~/.local/state fallbacks). The real offenders were the four body-dump writers, three of which disagreed about where dumps go:

Site Before Problem
src/loop/core.ts (REREQUEST dump, ACP_DUMP_BODY=1) raw ${process.env.HOME}/.local/state/billion-context/dumps $HOME is undefined on Windows → mkdirSync created a junk undefined/.local/... tree under cwd; ignored XDG_STATE_HOME; inconsistent with the other sites
src/server.ts (req JSON + raw REQ/RES/INCOMING) ${stateDir()}/dumps, ${stateDir()}/raw slash-concatenated worked, but string-concat path building
src/error-dump.ts (BILI_DUMP_4XX) same slash concat same
tests/issue-393-registry-window.test.ts logFile /tmp/issue-393-test.log on Windows this silently degrades to stderr-only logging, so file logging was never exercised there

Changes

  • src/paths.ts: new dumpsDir() = ACP_DUMP_DIR override ‖ path.join(stateDir(), "dumps"). All dump writers now share one XDG-based location that co-locates with bili.log.
  • src/loop/core.ts: REREQUEST dump uses dumpsDir(). Also sanitizes the session id embedded in the filename ([^a-zA-Z0-9_-] → _) like error-dump.ts already did — session ids are client-provided verbatim (see src/session-id.ts), so a hostile id containing / would previously break or escape the path.
  • src/server.ts / src/error-dump.ts: slash-concatenated paths → path.join.
  • src/ca.ts: explicit win32 branch in PLATFORM_CA_CANDIDATES (empty list — Node's built-in Mozilla root set is the only root source on Windows) instead of falling through to Linux bundle paths that can never exist there. Behavior unchanged; the list no longer lies about what is probed.
  • tests/issue-393-registry-window.test.ts: /tmp/... → os.tmpdir().
  • tests/dumps-dir.test.ts (new): unit tests for dumpsDir() precedence — ACP_DUMP_DIR > XDG_STATE_HOME > ~/.local/state/billion-context/dumps default.

Reviewed and intentionally unchanged: src/launcher.ts / src/client-config.ts / src/discover.ts / src/plugin-install.ts / src/update.ts / src/decompress-shared.ts (all already os.homedir()/os.tmpdir()/XDG-based), test fixture strings that are pure mock values without real I/O, and the e2e suite's repo-local tmp/ working dir (deliberate — codex refuses TMPDIR homes).

Windows keeps the documented %USERPROFILE%\.config|\.local\share|\.local\state\billion-context layout rather than mapping to %APPDATA%: changing it now would orphan existing sessions and CA state with zero user benefit.

Pre-flight

  • npm run typecheck ✓
  • npm test ✓ (1514 pass / 0 fail / 2 skipped e2e-gated)
  • npm run build ✓

Closes #864

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Reviewing PR #865 now — pulling the branch, auditing the diff against master, running typecheck/test/build locally, and checking CI status. Full verdict to follow shortly.

src/loop/core.ts: the compress-loop REREQUEST dump (ACP_DUMP_BODY=1) used a
raw `${process.env.HOME}/.local/state/billion-context/dumps` path. $HOME is
undefined on Windows, so mkdirSync created a junk `undefined/.local/...`
directory under cwd, and XDG_STATE_HOME was ignored — inconsistent with the
other two dump sites which used stateDir(). Now uses dumpsDir(). The session
id in the filename is also sanitized like error-dump.ts does: session ids are
client-provided verbatim (src/session-id.ts) and could contain '/'.

src/paths.ts: new dumpsDir() = ACP_DUMP_DIR override || <stateDir>/dumps,
so every dump writer (server req JSON + raw REQ/RES/INCOMING, loop REREQUEST,
BILI_DUMP_4XX err dumps) shares one location that co-locates with bili.log.

src/server.ts / src/error-dump.ts: slash-concatenated dump paths -> path.join.

src/ca.ts: explicit win32 branch in PLATFORM_CA_CANDIDATES (empty list —
Node's Mozilla root set is the only root source on Windows) instead of
falling through to Linux bundle paths that can never exist there.

tests/issue-393-registry-window.test.ts: logFile /tmp/issue-393-test.log ->
os.tmpdir(); on Windows the old path silently degraded to stderr-only logging
so file logging was never actually exercised there.

tests/dumps-dir.test.ts: unit tests for dumpsDir() precedence (ACP_DUMP_DIR >
XDG_STATE_HOME > ~/.local/state default).

Pre-flight: typecheck OK, 1514 tests pass / 0 fail, build OK.
@ranxianglei
ranxianglei force-pushed the 2026-09-17_cross-platform-paths branch from a3a22f1 to dd5ee7e Compare September 16, 2026 16:32
@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-17_cross-platform-paths (323d8c1)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-865

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr865.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] 🏷 Review update — rebased onto current master, CI in flight

While reviewing I found the PR had gone unmergeable (mergeable: false): master advanced ~20 commits past the branch base (bfb0b68 → d650db7) and produced one textual conflict in src/server.ts — a trivial import-line clash (this PR adds import path from "node:path" next to the crypto import; master separately extended that line to { createHash, randomUUID }).

Per the repo's review discipline (rebase to current master before claiming mergeable), I:

  1. Rebased 2026-09-17_cross-platform-paths onto origin/master (d650db7) and resolved the single import hunk as a union (both imports kept). Nothing else touched — the diff is unchanged: same 7 files, +100/−22.
  2. Re-ran pre-flight on the rebased head dd5ee7e: npm run typecheck ✓ · npm run build ✓ · npm test 1664 pass / 1 fail / 2 skipped (e2e-gated).
  3. The one failure is pre-existing on master, unrelated to this PR: tests/issue857-image-baseline.test.ts → #857 D: upward self-heal trusts usage baselines only (estimate blocked, usage confirmed) fails identically on bare origin/master (verified via detached checkout, deterministic assertion, failed twice). It comes from [Question] 非白名单上游(如 DeepSeek)图片按 b64/4 估算,实测高估约 100×;且该估算被写回 lastInputTokens 并自愈成 confirmed 窗口,使 #496 逃生门永久失效 #857/fix: stop image estimates poisoning the usage baseline (#857) #860 (image-baseline provenance), which landed after this branch forked. Reporting here per policy (no new issue filed from a working session).
  4. Pushed the rebased head with --force-with-lease to the PR head ref (feature branch only — master untouched, nothing merged). GitHub now reports mergeable: true, state blocked purely on pending CI.

Hot-file check: several open PRs also touch src/server.ts / src/loop/core.ts (#858, #804, #750, #823, #813…), but none of them edit the dump/REREQUEST region this PR owns — contention is limited to the generic import block, i.e. normal merge-order territory.

Code review itself is done and positive (see my earlier comment for the full walkthrough). Final verdict pending the CI matrix (ubuntu/windows × node 22/24 + zero-token e2e + version-guard) on dd5ee7e — I'll follow up as soon as it lands.

Windows CI failed 'dumpsDir: respects XDG_STATE_HOME': stateDir() resolves the env override via path.resolve (paths.ts xdg()), which on win32 turns the drive-relative fixture /custom/state into <cwd-drive>:\custom\state, while the test's bare path.join expectation stayed drive-less. Mirror the product resolution in the assertion instead of hardcoding POSIX semantics.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — two commits added by me during review, verdict below

Code review (the fix itself): positive. All seven files are on-purpose and minimal; no stray changes, no lock-file churn, version untouched (version-guard ✓). The design holds up under scrutiny:

  • dumpsDir() = ACP_DUMP_DIR ‖ path.join(stateDir(), "dumps") gives all four dump writers one XDG location co-located with bili.log; the session-id sanitization in src/loop/core.ts closes a real path-escape via client-provided ids.
  • The empty win32 CA-candidate list is safe: the probe loop just skips and writeCombinedBundle() always merges Node's built-in Mozilla roots (tls.rootCertificates).
  • No residual process.env.HOME / /tmp literals remain in src/; both compression modes unaffected (dump paths are host-side only).

What I changed while reviewing (both pushed to the PR head; nothing merged):

  1. Rebase onto current master (d650db7): the branch had gone unmergeable (~20 master commits ahead) with one trivial import-line conflict in src/server.ts (this PR adds import path; master extended the crypto import with createHash) — resolved as a union. Diff content otherwise byte-identical (+100/−22).
  2. Fixed a Windows-only bug in the new test (test: make dumpsDir XDG_STATE_HOME assertion platform-aware, commit 91f32af). First CI run caught it: dumpsDir: respects XDG_STATE_HOME failed on both windows legs because stateDir() resolves env overrides via path.resolve() (existing convention in src/paths.ts:22), which on win32 turns the drive-relative fixture /custom/state into <cwd-drive>:\custom\state, while the test's bare path.join expectation stayed drive-less (D:\custom\state\... vs \custom\state\...). The product behavior is correct and consistent — the test hardcoded POSIX semantics, violating the repo's "tests must be deterministic" rule (§7.2). Fix mirrors the product resolution in the assertion; now green on windows node 22+24 (verified in logs: all three dumpsDir tests pass on both legs).

CI on final head 91f32af: zero-token e2e ✓ · codex CODEX_HOME overlay ✓ · version-guard ✓ · build-artifact ✓ · 4× test matrix fails only on one pre-existing test, unrelated to this PR:

Verdict: this PR is clean, correct in both compression modes, and mergeable from its own scope once master's #857 test is dealt with (either fixed first, or merged knowingly-red at your call — merge is yours, not mine). Hot-file note: several open PRs touch src/server.ts/src/loop/core.ts (#858, #804, #750, …) but none edit the dump region, so expect at most import-block shuffle when sequencing merges.

中文摘要:PR 代码本身审查通过(路径统一走 XDG、会话 id 已消毒、CA 空列表安全);review 期间我直接补了两个 commit——rebase 到最新 master(解决一处 import 冲突)+ 修复新测试在 Windows 上必挂的断言(path.resolve 与 path.join 语义差异);现在四个测试矩阵只剩一个与本 PR 无关的 master 既有失败(#857 image-baseline,裸 master 同样挂),修掉或知情的情况下本 PR 可以合并。

@ranxianglei
ranxianglei merged commit 305e902 into master Sep 17, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-17_cross-platform-paths branch October 15, 2026 05:32
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.

调查并修复所有写死的linux目录比如/tmp做到跨平台

1 participant