Skip to content

test(machines): the bridge tunnel's own checks run again, and an unknown token fails its case - #3663

Merged
devswha merged 1 commit into
mainfrom
fix/machine-tests-real-tunnel
Nov 11, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/machine-tests-real-tunnel

Conversation

@devswha

@devswha devswha commented Nov 11, 2026

Copy link
Copy Markdown
Owner

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

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.

…own token fails its case

## 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` 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
    • Expanded coverage for machine connection relay authorization and credential validation.
    • Improved test coverage for relay port selection and machine connection scenarios.

The tests now use a shared SSH relay fixture that calls the real MachineManager.tunnel implementation. They also verify bridge credential validation and reject requests with retired bridge tokens.

Changes: SSH relay test coverage

Layer / File(s) Summary
Shared relay fixture and test setup
server/machine-tunnel.fixture.ts, server/machine-core-choice.test.ts, server/machine-herdr-choice.test.ts, server/machine-herdr-switch-retry.test.ts
The new tunnelRelays fixture records SSH forwards and maps matching tunnel URLs to local relay servers. The tests use the fixture instead of directly mocking MachineManager.tunnel.
Bridge credential and retired-token checks
server/machine-core-choice.test.ts
Tests verify that requests with retired tokens receive HTTP 401. Parameterized cases check invalid ports and tokens, and confirm invalid credentials do not add another forward.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Other

Possibly related PRs

  • devswha/ovis#3655: Introduced test-local relay mocks in the same test files; this PR replaces them with the shared fixture and exercises the real tunnel implementation.
Merge Risk: Minimal · up to 6ef62

This change only updates test infrastructure to restore coverage of the real tunnel and credential checks. It has no production behavior change and no actionable merge risk.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title clearly identifies the two main changes: restoring real bridge-tunnel checks and rejecting unknown tokens.
Description check Passed The description includes the required Change and Validation sections, explains the problem and resulting behavior, and reports the executed checks and results. It does not explicitly list skipped chec…
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: Description check

Explanation

The description includes the required Change and Validation sections, explains the problem and resulting behavior, and reports the executed checks and results. It does not explicitly list skipped checks, but the description is otherwise complete.

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 4 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 9c07ec1 and 6ef629f.

Files selected for processing: 4

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

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

@devswha
devswha merged commit 8a868f7 into main Nov 11, 2026
21 checks passed
@devswha

devswha commented Nov 11, 2026

Copy link
Copy Markdown
Owner Author

Merge pass

Merged head 6ef629f76c75 as 8a868f78f1b6 on GitHub's checks, with no unresolved review threads.

Reviewer Verdict Note
claude CLEAR
codex CLEAR

Test-only PR (three server unit test files and one new fixture). Two reviewers at this head: Claude (Opus) CLEAR, Codex Terra CLEAR with no findings.

It restores the coverage #3655 gave up, which the merge pass merged before reading its author's request to hold it: forward is mocked again, so the real MachineManager.tunnel runs in all three files; the relay answers 401 only to tokens of cases that already ended, and any other unknown token fails its case; a new eight-case test drives the real tunnel to "Invalid bridge credentials". The port-0 relay that #3655 added stays. The Opus reviewer made the stand-in return a wrong token in a scratch copy: 36 of the 52 core-choice tests failed on the token check. The three files pass (70 pass, 0 fail).

Left, LOW (Claude, not run): the fixture matches a tunnel to its relay by the freed local port alone, in call order; that holds while the stand-in forward resolves at once, and a mismatch would fail a test, never pass one.

@devswha
devswha deleted the fix/machine-tests-real-tunnel branch November 11, 2026 11:34
devswha added a commit that referenced this pull request Nov 12, 2026
…e 0.5.3 release (#3693)

## Change

The low-priority review findings left for after the 0.5.3 release, from
the reviews of #3653, #3654, #3656, #3663, #3666, #3668 and #3682. One
commit each:

**Product (changelog fragment `post-release-lows.md`)**
- `fix(opencode)`: a verified pane keeps the right OpenCode pid. A pid
herdr did not report is unknown, not `-1`; an older verification ending
after a newer one does not put its older pid back, nor a pid over the
newer one's empty reading; a read with nothing in front clears the pid,
as before. (#3653; the #3653 suggestion to keep the pid on an empty read
was dropped in review: a still-running pid is no proof it is the pane's
job.)
- `fix(opencode)`: a known OpenCode pid is followed up its parents to
the shell, so a shell with more than 256 processes still finds its
stopped OpenCode. Checked on a real `/proc` with 301 children. (#3653)
- `fix(ssh)`: a failed forward's error leaves out the lines `ssh -v`
writes without the debug prefix (`Authenticated to`, transfer totals,
some builds' banner). (#3666)
- `fix(ci)`: the runner route asks the Actions API again only after a
server error or no answer (a 4xx ends it), each attempt bounded to a
minute. (#3682)

**Tests and docs**
- `test(ssh)`: which ssh's word a forward waits for is its own function,
with a test for Git for Windows', Windows' own and other OpenSSH.
(#3666)
- `test(chat-fallback)`: the two waits on the chat's callbacks fail with
a reason after 10 s instead of hanging. (#3668)
- `test(copilot-chat)`: New messages is looked up and clicked in one
page step. (#3654)
- `test(machines)`: the tunnel fixture matches a relay by both of its
ports. (#3663)
- `test(pages)`: the stale-deploy gate is checked on the parsed
`pages.yml`. (#3656)
- `docs(pages)`: `docs/development.md` describes the `current` /
`deploy` split. (#3656)

Left out on purpose: a test seam for `MachineManager.verify` (60 tests
already reach its privates the same way), cancelling the move-badge read
in `MachineManager.stop()` (its answer is already dropped and the read
is bounded at 30 s), and `quietPort` across processes (a 1-in-~12k pick
collision; it needs cross-process claims). Workflow changes from those
reviews wait for the owner.

## Validation

- Each commit's own tests (ssh 35, opencode/status 443, route 25, pages
57, machine tunnel 70) and `bun run typecheck`: pass.
- `scripts/chat-fallback-browser-qa.ts`: pass locally.
`scripts/copilot-chat-browser-qa.ts`: **not run locally** (this host's
node names the fixture process `MainThread`, so the script stops before
the changed line); CI runs it.
- `bun run check fast` on the branch rebased onto 1f4368e: pass (16152
pass, 0 fail).
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