Skip to content

test(machine-core-choice): the tunnel's relay listens on a port of its own, and refuses another case's late reads - #3655

Merged
devswha merged 1 commit into
mainfrom
fix/machine-core-choice-flake
Nov 11, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/machine-core-choice-flake

Conversation

@devswha

@devswha devswha commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Change

Test-only fix for an intermittent failure of server/machine-core-choice.test.ts ("default herdr older peer"), seen in a local bun run check fast run, 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:

  1. A freed port gets taken. The tunnel stand-in started its relay on a port the app had just released. Another process on a busy host sometimes took that port first; one reproduction logged Is port 45243 in use?.
  2. A late request from an earlier case. A move-badge read still in flight from a case that had already ended reached the next case's relay. Every case used the same fixed tokens, so that relay answered it as its own.

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) and server/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

  • Whole-file runs, 16 copies at a time, 25 rounds each:
    • 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.ts and machine-herdr-switch-retry.test.ts: 400 of 400 passed each after.
  • CI: 0 hits in the last 200 CI runs, so this is about local check fast and the merge gate's Fast checks on a busy host, not about red CI runs.
  • bun run check fast: pass (16082 pass, 0 fail).
  • Not run: check full. No changelog fragment: test-only.

…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.
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Tests
    • Updated automated coverage for machine tunnel selection, token authorization, relay URL handling, and retry and switching scenarios. Test relays now use automatically assigned local ports, and fixtures use generated tokens.
    • No user-facing changes are included in this update.

The tests replace SshConnection.forward mocks with MachineManager.tunnel mocks. The fixtures use ephemeral relay ports. The core-choice fixture also generates random tokens and checks authorization against the selected descriptor.

Changes: Tunnel test fixture updates

Layer / File(s) Summary
Ephemeral tunnel mocks
server/machine-core-choice.test.ts, server/machine-herdr-choice.test.ts, server/machine-herdr-switch-retry.test.ts
The tests start local relays on OS-assigned ports and return their URLs and tokens. The core-choice fixture generates random tokens, selects the descriptor by remote port, and checks token authorization.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Possibly related PRs

  • devswha/ovis#3312: Adds runtime-move polling; this change uses per-case tokens to prevent a late poll from authenticating against a later test case.
  • devswha/ovis#3599: Adds a named-session core-choice case that uses the work bridge path covered by the updated fixture.
Merge Risk: Minimal · up to 59666

This is a test-only change that makes relay fixtures use OS-assigned ports and per-case random tokens. It has no production impact and no remaining merge-blocking risk.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title clearly identifies the main test fix: using an independently listening tunnel relay and rejecting late reads from another test case. It is specific and related to the changes.
Description check Passed The description includes the problem, causes, behavioral fix, affected files, validation results, skipped checks, and changelog rationale. It satisfies the required Change and Validation sections.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 3 files.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 856ef33 and 5966668.

Files selected for processing: 3

  • server/machine-core-choice.test.ts
  • server/machine-herdr-choice.test.ts
  • server/machine-herdr-switch-retry.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@devswha
devswha merged commit 50d99d0 into main Nov 11, 2026
29 of 31 checks passed
@devswha
devswha deleted the fix/machine-core-choice-flake branch November 11, 2026 09:35
@devswha

devswha commented Nov 11, 2026

Copy link
Copy Markdown
Owner Author

Merge pass

Merged head 59666683e981 as 50d99d03fabc on GitHub's checks, with no unresolved review threads.

Reviewer Verdict Note
claude CLEAR
codex CLEAR

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 server/machine-core-choice.test.ts: the tunnel stand-in's relay listened on a port the app had just freed, and a late read of an earlier case reached the next case's relay. The relays now listen on port 0, and each case has tokens of its own. The author's runs: 3 of 400 failed before, 0 of 400 after, at 16 copies.

Its first run was red on scripts/chat-virtual-browser-qa.ts:728 (twice in one job), a browser script this PR does not touch; the job was rerun once and that failure was sent to lanes fix-ci and ci-loop.

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 MachineManager.tunnel instead of forward, so they no longer reach the real tunnel's credential check.

devswha added a commit that referenced this pull request Nov 11, 2026
… 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).
devswha added a commit that referenced this pull request Nov 11, 2026
…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.
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.

1 participant