Skip to content

test: poll for proxy-origin in claude hook e2e (fixes #1031 flake) - #1033

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-20_fix-claude-hook-e2e-race
Sep 20, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-20_fix-claude-hook-e2e-race

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Fixes #1031 — hook e2e: a healthy proxy on ANOTHER port is never attached (static URL) flaked with ENOENT ... proxy-origin (~1 in 4 full-suite runs).

Test-only change to tests/claude-native.test.ts (+21/-3):

  • new helper waitForInstanceFile(file, ms): bounded poll (50ms interval) for <state>/billion-context/proxy-origin; non-ENOENT errors rethrow; still absent past the deadline = hard failure with a clear message.
  • the three spawn-path reads that followed waitForPort now use it (static-URL test ×2, second-run test ×1). Post-attach settled-state reads and the cleanup sweep keep immediate reads by design.

Why this is the right layer

The race is real and structural, verified empirically before touching anything:

  1. The proxy writes proxy-origin synchronously inside its 'listening' callback (src/server.ts announceListening). The kernel starts accepting TCP connects on the port before that callback runs — a micro-repro of the exact pattern (child http server writing a marker in 'listening', parent tight-polling fresh ports, n=300) shows the port connectable with the marker absent in 23/300 runs (~7.7%) unloaded, widening under load.
  2. HTTP health cannot answer before the write (event-loop phase ordering; 0/300), so the bootstrap's no-file fallback path is not the trigger — the trigger is the reader side observing the [bind, write] window, e.g. when startup is delayed under full-suite concurrency and the test's first successful port probe lands inside the window.
  3. The test asserted on an unsettled state: one immediate readFileSync after waitForPort. The issue's suggested fix (poll for the file / await a deterministic signal, keep negative assertions on settled state) is exactly what this implements.

The poll also converts the residual "file never written" case (swallowed error in announceListening's best-effort try/catch) from a racy ENOENT into a deterministic timeout failure — no more false greens or false reds depending on timing luck.

Verification

  • npm run typecheck green (src); root tsconfig clean for the touched file (pre-existing unrelated errors in ws-upgrade.test.ts/zero-config-routing.test.ts unchanged).
  • 80-iteration load harness replicating the exact flow: no repro pre-fix on this box (flake needs full-suite pressure), post-fix behavior covered by repeated real runs.
  • tests/claude-native.test.ts ×4 back-to-back: 23/23 pass each run.
  • Full npm test: 2020 tests, 2018 pass, 0 fail, 2 skipped (expected skips).

No wire/config/persistence changes; both compression modes unaffected.

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-20_fix-claude-hook-e2e-race (2e6e58d)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1033

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1033.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] 🏷 收到。这是已完成的 PR(#1033),按规范不重做——正在拉取分支核对代码、检查 CI 状态,稍后给出结论。

Acknowledged — this is a completed PR, so I'm reviewing rather than redoing it: fetching 2026-09-20_fix-claude-hook-e2e-race, verifying the diff matches the description, checking CI, then I'll report back here.

The proxy writes <state>/billion-context/proxy-origin inside its
'listening' callback, which runs AFTER the kernel starts accepting TCP
connects on the port. The hook-e2e assertions therefore race: waitForPort
can return while the file is still absent (#1031, ~1-in-4 full-suite
runs, ENOENT at the immediate readFileSync).

Add waitForInstanceFile (bounded 30s poll, 50ms interval; non-ENOENT
errors rethrow; timeout = hard failure) and use it at the three
spawn-path reads in the hook e2e tests. Post-attach settled-state reads
and the cleanup sweep keep their immediate reads by design.
@ranxianglei
ranxianglei force-pushed the 2026-09-20_fix-claude-hook-e2e-race branch from c8004b0 to 2e6e58d Compare September 20, 2026 04:44
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 ## Review complete — ✅ mergeable

中文摘要:修复了 claude hook e2e 里 proxy-origin 文件的竞态(端口已可连接但文件尚未落盘时的一次性 readFileSync 撞 ENOENT,#1031 flake);把三处 spawn 路径的读取改成带 30s 上限的轮询,其余 settled-state/cleanup 读取按设计保留即时读。已在当前 master 上 rebase 并全量验证通过,可以合并(合并操作留给人工)。

What I verified

Diff matches the description exactly. Single file tests/claude-native.test.ts, +21/-3, one commit. New helper waitForInstanceFile(file, ms): 50 ms-interval poll, non-ENOENT errors rethrown immediately, past-deadline = hard failure with a clear message. The three converted reads are precisely the racy spawn-path ones:

  • tests/claude-native.test.ts:502 and :511 — static-URL test, both right after waitForPort
  • tests/claude-native.test.ts:572 — second-run test, first read after spawn

Settled-state reads keep immediate reads by design: :581 (post-attach inst2) and :589 (cleanup sweep, already guarded by catch {}). Test-only change — no wire/config/persistence surface, so neither compression mode is affected.

Root-cause claim holds against current master. src/server.ts:455 does server.listen(port, opts.host, announceListening), and announceListening (src/server.ts:378) writes the instance file via atomicWriteInstanceFile — synchronously inside the 'listening' callback, wrapped in a best-effort try/catch. The kernel accepts TCP connects before that callback runs, so the [bind, write] window is real and structural; a reader that probes the port then does one immediate readFileSync can land inside it. Polling on the reader side is the right layer (and matches the fix suggested in #1031), and it converts the residual "file never written" case from a timing-luck ENOENT into a deterministic timeout failure.

Stale base handled. The branch was 14 commits behind master, and 5 of those touched this same test file (the #964 claude-hook work) — a hot-file collision risk. I cherry-picked the commit onto current master (af42145): applied cleanly, zero conflicts. No other open PR (#1028/#1008/#984) touches tests/claude-native.test.ts.

Verification on the rebased head (run locally):

  • npm run typecheck — green
  • npm run build — green
  • Full npm test — 2048 tests, 2046 pass, 0 fail, 2 skipped (expected skips)
  • tests/claude-native.test.ts ×3 back-to-back — 23/23 pass each run

Branch updated. Force-pushed the rebased commit to ranxianglei:2026-09-20_fix-claude-hook-e2e-race (with-lease, single commit) so the PR sits on current master. GitHub now reports mergeable: true, and all 8 CI checks are green on the new head (2e6e58d): ubuntu/windows test matrices, build-artifact, version-guard, codex e2e, CODEX_HOME overlay.

Merge when ready: #1033

@ranxianglei
ranxianglei merged commit ea9f635 into master Sep 20, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-20_fix-claude-hook-e2e-race 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.

flaky test: claude-native hook e2e "never attached (static URL)" — ENOENT proxy-origin (~1 in 4 runs)

1 participant