Skip to content

fix: re-roll freePort picks that collide in claude-native hook e2e - #1653

Merged
ranxianglei merged 2 commits into
masterfrom
2026-09-29_freeport-collision
Sep 29, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-09-29_freeport-collision

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #1652

What

freePort() in tests/claude-native.test.ts gains an exclusion set (freePort(exclude: readonly number[] = [])) and re-rolls until the pick misses it. The two-port hook e2e ("a healthy proxy on ANOTHER port is never attached (static URL)") now picks portB = await freePort([portA]). All other call sites keep the zero-arg form — identical behavior.

Why (root cause)

Release CI run 36519443543 (v0.1.171) failed attempt 1 on this single subtest:

error: 'spawned — not attached to A'
actual: bili: attaching to running proxy at http://127.0.0.1:44149 (pid 3445)
expected: /started at/

Linux bind(0) can return the SAME port twice — measured 26 adjacent pick-close-pick repeats in 200 000 trials (≈1.3e-4/pair), 5 re-hits of the first port in the next 20 000 picks. When portB === portA, hook B resolves plan.port === A's port, pickAttachable (src/launcher.ts:2555, c.inst.port === opts.port under strictPort) correctly matches A and attaches — the product behaves right, the test premise silently dies, and the assertion fails.

Forced-collision repro (locally setting portB := portA) reproduces the CI failure byte-for-byte (same assertion line :943, same attaching to running proxy actual) — see issue #1652.

Pre-flight

  • forced-collision repro fails on master, passes with the fix
  • fixed test ✔ and whole tests/claude-native.test.ts 48/48 ✔ under ACP_TEST_CLAUDE_NATIVE=1
  • npm run typecheck ✔ · npm run build ✔
  • full-suite caveat: local full runs on this shared sandbox time out / show 18 attach-mode failures in opencode-native/dsh-native — control experiment on CLEAN master (fix stashed) reproduces the identical failure set, i.e. pre-existing environmental flakiness (tests/claude-native.test.ts linux live-E2E subtests fail/hang in shared multi-agent sandboxes #1248 class), unrelated to this diff (which touches one test file's helper only)
  • version untouched; no merge/publish by agent (human merges)

Linux bind(0) can hand out the same port twice (~1.3e-4/pair measured);
when portB === portA the hook correctly attaches and the test premise
silently dies, failing the release CI run with 'spawned — not attached
to A'. freePort() now takes an exclusion set and re-rolls; the two-port
e2e passes [portA].

Fixes #1652
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-29_freeport-collision (801f2d6)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1605-pr.1653.51f680e

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1653.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

Reviewing (verifying, not redoing — this is completed agent work per protocol). Status so far:

  • Branch 2026-09-29_freeport-collision @ d65472b pulled; base is current master tip (e8a15c4), mergeable_state: clean, no conflicts.
  • Diff audited: single file tests/claude-native.test.ts (+18/−9). freePort(exclude) re-roll is trivially correct; all 5 other call sites keep the zero-arg form (identical behavior — empty exclude returns the first pick, same as before). No product code, no config surface, version untouched.
  • Local pre-flight: npm ci + npm run typecheck ✔ · npm run build ✔.
  • One discrepancy to chase down before verdict: my local tests/claude-native.test.ts run under ACP_TEST_CLAUDE_NATIVE=1 came back 45/48, not 48/48. Failures: #29 resolveClaudeHostPid: live ps walk…, #45 watchdog tracks the claude host…, #48 shared proxy survives the first session's exit… — none of them touch the changed two-port subtest (that one passed). They look like the tests/claude-native.test.ts linux live-E2E subtests fail/hang in shared multi-agent sandboxes #1248-class shared-sandbox flakiness the PR body flags for opencode/dsh, but I'm running the control experiment (same 3 tests on clean master's copy of the file) to prove it rather than assert it.

Will follow up with the full review conclusion (issue-direction check, regression inventory, CI status) once the control lands.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

Review complete — verdict: mergeable (merging stays human-only).

① Original issue (#1652) — direction check. Root-cause analysis verified against code at current master:

  • freePort() is pick-close-return (listen(0) → close); the two-port subtest picks back-to-back at tests/claude-native.test.ts:933-934.
  • When portB === portA, hook B resolves plan.port === portA and pickAttachable (src/launcher.ts:2555) matches instance A under strictPort (c.inst.port === opts.port) → hook B attaches, prints attaching to running proxy at …, and assert.match(rB.stderr, /started at/) at :952 fails with exactly the CI signature. Product behavior is correct; the test premise silently dies.
  • The fix targets the producer (a duplicated port handed to the test premise), not the symptom. Direction sound. ✔

② Regression review — existing behaviors touched by the diff (diff touches exactly one file, tests/claude-native.test.ts, +18/−9):

  1. freePort() zero-arg semantics at the other 5 call sites (:1007, :1054, :1137, :1237, :1331): preserved — empty exclude returns the first pick; that path is byte-identical to before.
  2. Changed call site :934: intentional change — portB now guaranteed ≠ portA (that IS the fix).
  3. No product code, wire/message shape, config surface, persistence format, cross-repo dependency, or version touched. No product behavior change (test infrastructure only).
  4. Local 45/48 vs claimed 48/48 — resolved by control experiment: the same 3 subtests run against clean master's copy of the file fail identically (0/3) → pre-existing tests/claude-native.test.ts linux live-E2E subtests fail/hang in shared multi-agent sandboxes #1248-class shared-sandbox flakiness, not this diff. Specifically, all 3 (resolveClaudeHostPid: live ps walk…, watchdog tracks the claude host…, shared proxy survives the first session's exit…) are among the 7 live subtests that tests/claude-native.test.ts linux live-E2E subtests fail/hang in shared multi-agent sandboxes #1248 (closed) gated behind ACP_TEST_CLAUDE_NATIVE=1 precisely because they are load-driven flakes on shared multi-agent boxes; I ran with the gate explicitly on. They pass on dedicated CI (below).

③ Code review. Re-roll loop trivially correct: the loop is unbounded only if the exclude set covered the entire ephemeral range (1 element here — effectively impossible; worst case one extra bind/close cycle). readonly number[] param with [] default keeps every existing caller source-compatible; no as any; the comment states the measured rate, justified.

④ Test validity. The fixed subtest pins the two-port non-attach behavior (B must spawn its own instance on its own port, distinct pid). The re-roll helper itself has no unit test — acceptable for a file-private helper whose logic is trivially inspectable; the regression evidence is the documented forced-collision A/B (pre-fix red with byte-for-byte CI signature / post-fix green), which is the practical substitute for a probabilistic ~1.3e-4/pair bug.

⑤ Diff cleanliness. One file, every line relevant to the fix; no whitespace churn, no generated artifacts, no lockfile drift.

CI status (head d65472b): all 12 check runs completed/success — test matrix ubuntu-latest 22+24, windows-latest 22+24, real codex/opencode/pi/dsh lanes, hermetic registry e2e, version-guard, build-artifact. Branch base = current master tip (e8a15c4); mergeable_state: clean.

Auto-merge gate (§7.4): pure fix:, single test-file scope, references Fixes #1652, green on rebased head, no config/wire/persistence/cross-repo/version/load-bearing-infra changes, clean diff. Gate criterion 2 (regression test reproducing the original bug) is met in forced-collision form, appropriate for a probabilistic flake. Nothing here falls into the must-stay-human categories beyond the merge act itself.

Regression verdict: none introduced by this PR — zero-arg helper paths unchanged, target subtest passes locally and in CI, and the only local failures reproduce identically on clean master.

中文摘要:修复了 claude-native hook e2e 中 freePort() 连续两次取到同一端口(Linux bind(0) 可重复发号,实测 ~1.3e-4/对)导致「另一端口不被 attach」子测试前提静默失效的偶发 CI 失败;改动仅限该测试文件的辅助函数(新增排除集、命中即重抽)且只改一处调用点,产品行为零变更;本地看到的 3 个额外失败经干净 master 对照实验证实为 #1248 类的共享沙箱负载抖动(与 diff 无关,CI 专用 runner 上全绿),可以合并。

@ranxianglei
ranxianglei merged commit 20d7c49 into master Sep 29, 2026
12 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-29_freeport-collision 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.

flake: claude-native hook e2e "never attached (static URL)" — freePort() collision lets the same port be handed to both hooks

1 participant