Repository navigation
test(machine-core-choice): the tunnel's relay listens on a port of its own, and refuses another case's late reads - #3655
Conversation
…s own, and refuses another case's late reads
## Change
Fixes a flaky unit file, `server/machine-core-choice.test.ts`. In a
`bun run check fast` run (unit files 8 at a time) "default herdr older
peer" connected the PC being added to herdr instead of asking. It
failed in none of the last 200 CI runs (only one Fast checks job
failed, in another file), so it shows up on loaded local hosts.
### Cause
Two races in the fixture, not in the app.
- The fixture's SSH tunnel stand-in served its relay on the port
`MachineManager.tunnel` had freed for it (`freePort` binds port 0,
closes it, then hands the number to `ssh -L`). On a busy host another
process took that port first: `Bun.serve` threw "Failed to start
server. Is port 45243 in use?" (seen in a repro log). The Ovis found
then went unproven, so depending on the case the PC connected to
herdr unasked, failed, or retried instead of asking.
- Every case used the same bridge tokens. A PC connected through a
bridge still had its move-badge read (`GET /api/runtime/move`) under
way when its case ended a few ms later; when a later case's relay was
given the same port, that read arrived with a known token and failed
the relay's own assertion. Bun then started the next case while the
failed one still ran, and the cases after it failed too.
### Fix
The tunnel stand-in now replaces `MachineManager.tunnel` and serves its
relay on port 0, returning the address it got. Each case's bridges get
random tokens, and a relay answers a token none of them holds with 401,
as a bridge does. It still asserts that each request of the case
carries the token of the bridge it reaches.
`server/machine-herdr-choice.test.ts` and
`server/machine-herdr-switch-retry.test.ts` started their relays the
same way, on the freed port; their tunnel stand-ins get the same port-0
relay. Their relays check no token, so an earlier case's late read is
answered and ignored by its stopped manager: they keep their tokens.
## Validation
The whole file, 16 copies at a time on this host (load 20-50), before
and after:
- before: 3 of 400 runs failed ("open folder", "wrong runtime",
"default bridge")
- with the port change alone: 1 of 240 failed (the late read above)
- after: 0 of 400 failed
`machine-herdr-choice.test.ts` and `machine-herdr-switch-retry.test.ts`
after the change, 16 copies at a time (load 41-61): 400 of 400 runs
passed each.
`bun run check fast` passes.
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
Merge passMerged head
Test-only PR (three server unit test files). Two reviewers at this head: Claude (Opus) CLEAR, Codex Terra CLEAR with no findings. It closes an intermittent failure of Its first run was red on Left, both LOW (Claude, read only), sent to the author's lane: the relay's new 401 for an unknown token can hide a wrong token the app sends; these flows now mock |
… rows it uncovered (#3659) ## Why `scripts/chat-virtual-browser-qa.ts:728` ("PageUp stays at its anchor through output") failed in both attempts of CI run 42989933116 on PR #3655 (an unrelated change): the row the reader was on moved 20 px. The test reads the reader's place right after PageUp. On a loaded runner the list draws the rows that PageUp uncovered a frame or more later, so the top of the view is still blank and the first drawn row (ROW-987, y=116) is taken as the anchor. The list then draws ROW-986 at its estimate and holds that one; ROW-986 measures 20 px shorter, so ROW-987 moves up 20 px. CI's numbers fit exactly: the `away` keys `[7, 8, 987..999]` show nothing above 987 was drawn, and the scroll height changed by -18 = -20 + 2. This is not #3650's `hearUnheardScroll`: it never fires in this step, and the same forced order fails the same way on 301934f, the commit before #3650. ## What - `scripts/chat-virtual-browser-qa.ts`: after `page(-1)`, wait until a drawn row covers the top of the view, then read the place. No sleep, no timeout change, the assertion unchanged. No product change, no `patches/` change, so no changelog fragment. ## Proof In the CI runner image (fix-ci harness, `WORKERS=1`): a forced order (PageUp, then 600 px past the drawn rows, the place read in the same task) fails 2/2 on main and 2/2 on 301934f at the same assertion (anchor 240 -> 226), and passes 2/2 with this change. Traces on main show `hearUnheardScroll` never firing in the step (16 plain, 12 CPU-throttled steps). ## Validation - `bun run check fast` on this head (rebased on a55d9b7): pass (unit 16104 pass, 0 fail). - `chat-virtual-browser-qa.ts`, plain, interleaved: main 4/4, this change 4/4. - #3650's :97 forcing: fails 2/2 on 301934f, passes 2/2 with this change. - The 16 chat scripts from #3650 plus chat-virtual-browser-qa: 17/17 pass. - Skipped: `check full`, the integration suite, `ui-regression.ts`, WebKit, Firefox, macOS, Windows. ## Not verified - The exact frame order on the CI runner. The forcing reproduces the same mechanism, and CI's numbers fit it. ## Not in this PR (LOW) - When a slow device draws the uncovered rows a frame late, the list holds a row drawn at its estimate, so text the reader already sees moves by the estimate's error (14 px forced, 20 px in CI). Upstream behaviour, on main and before #3650. - `hearUnheardScroll` may treat the chat's selection-hold write as a reader's scroll, so a row that grew above would be applied twice. Found by reading, not run (Opus's LOW on #3650).
…own token fails its case (#3663) Follow-up to #3655, which the owner kept merged (2026-11-11). #3655 fixed two races in three machine tests by mocking `MachineManager.tunnel` itself, and so lost two things: these flows no longer ran the real tunnel and its "Invalid bridge credentials" check (`server/machines.ts`, near line 1961), which no other test reached, and the core-choice relay answered 401 to any unknown token, so a wrong token the app sent no longer failed the case. This PR brings both back and keeps both races fixed. ## Change Follow-up to #3655, which fixed two races in the SSH tunnel stand-ins of `server/machine-core-choice.test.ts`, `server/machine-herdr-choice.test.ts` and `server/machine-herdr-switch-retry.test.ts` by replacing `MachineManager.tunnel` itself. Review found that this lost coverage: - the real `tunnel` (it refuses a descriptor whose port or token is not one of this app's with "Invalid bridge credentials", frees a local port, forwards it and builds the address and token) ran in none of these files, and no test anywhere reached "Invalid bridge credentials"; - the core-choice relay answered 401 to any token none of the case's bridges holds, so a wrong token the app sent was refused like a bridge would refuse it instead of failing the case. ### Fix - A shared stand-in (`server/machine-tunnel.fixture.ts`) mocks `SshConnection.forward` again, so `MachineManager.tunnel` runs whole. `forward`'s stand-in still serves the relay on port 0, never on the number `freePort` freed (the race #3655 fixed); `tunnel` is wrapped only to point the address it returned at the relay `forward` was asked to open at that number, with the token it returned. - The core-choice relay answers 401 only to the tokens of cases already ended (a late move-badge read of theirs, the other race #3655 fixed). Any other token must again be the token of the bridge it reaches. - A focused test drives the real `tunnel` with a descriptor whose port or token is invalid: it rejects with "Invalid bridge credentials" and forwards nothing, while the same valid descriptor opens a tunnel. ### Seen in production code, not changed here `MachineManager.tunnel` takes a port number from `freePort` (bind port 0, close it) and only then has `ssh -L` bind it, so another local process can take the number between the two. In multiplexed mode the forward then fails. In direct mode (`forward` polls until something listens on the port), the app could take another process's listener for the tunnel and send it the bridge's token. Letting ssh choose the port (`-L 0:...`) or binding before handing it over would remove that window. ## Validation Each file 400 times, 16 copies at a time on this host (load 20-32), as CI's unit lane runs them (`bun test --timeout 60000`, `HERDR_TEST_MODE=unit`): - `machine-core-choice.test.ts`: 400 of 400 passed - `machine-herdr-choice.test.ts`: 400 of 400 passed - `machine-herdr-switch-retry.test.ts`: 400 of 400 passed The restored checks bite (in a scratch copy, discarded): - `tunnel` returning a well-formed wrong token: 44 of 52 core-choice tests fail on the relay's token assertion; with main's tests all 44 pass. - `tunnel`'s credential check disabled: all 8 new cases fail. `bun run check fast` on this branch, rebased on main 9c07ec1: pass (16117 pass, 0 fail). The production race in `MachineManager.tunnel` noted above is handled in its own PR.
Change
Test-only fix for an intermittent failure of
server/machine-core-choice.test.ts("default herdr older peer"), seen in a localbun run check fastrun, plus the same relay fix in two sibling test files that start their relay the same way.Cause
Two races in the test fixtures, not in the app:
Is port 45243 in use?.Fix
server/machine-core-choice.test.ts: the relay listens on port 0 and returns the address it actually got. Each case uses random tokens, and a relay answers 401 to a token no bridge of the current case holds.server/machine-herdr-choice.test.ts(3 relays) andserver/machine-herdr-switch-retry.test.ts(1 relay): the same port-0 change. Their relays do not check tokens, and a late reply goes to a manager that has already stopped and ignores it, so they need no token change.No sleep, timeout or retry is added.
Validation
machine-core-choice.test.ts: 3 of 400 failed before, 0 of 400 after. With only the port change, 1 of 240 still failed, which is why the tokens changed too.machine-herdr-choice.test.tsandmachine-herdr-switch-retry.test.ts: 400 of 400 passed each after.check fastand the merge gate's Fast checks on a busy host, not about red CI runs.bun run check fast: pass (16082 pass, 0 fail).check full. No changelog fragment: test-only.