Skip to content

fix(terminal): hand the shared grid only to a device still showing the terminal - #1085

Merged
devswha merged 1 commit into
mainfrom
fix/release-grid-hidden-clients
Oct 14, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/release-grid-hidden-clients

Conversation

@devswha

@devswha devswha commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Refs #472

Follow-up to #1068. Fixes the remaining gap in #1068's grid handoff: when a client released the shared grid, the server could hand the pty to another client whose terminal was hidden (a pane hidden in its split, or a tab put away). That client had stopped showing the terminal but had sent nothing, so its grid was still a handoff candidate.

What changed

  • src/lib/ws.ts: keepSize() (hidden pane, tab out of use) now releases the grid the same way the Chat lens does. It sends release-size once when this connection drove the grid, and never replays it on reconnect. Showing the terminal again at the same dimensions still sends the resize that takes the grid back.
  • server/index.ts: each client's attach claim records whether it keeps the size (keep_size attach or release-size; cleared by a resize or drive). release-size hands the pty only to a client still showing the terminal. If none is left, the size stays. A release that arrives while the attach is still being created is remembered, so the completed attach cannot bring the released grid back.
  • Tests: two contract cases in server/api.contract.test.ts: three clients with the real HerdrSocket hiding its pane, then a release that lands during attach creation. One extended case in src/lib/ws.test.ts. Changelog fragment added.

Before / after

Contract run (bun test ./server/api.contract.test.ts -t release-size, isolated herdr session):

  • Before (main's server/index.ts + src/lib/ws.ts): desktop at 120x40, hidden client at 100x30, phone at 53x33. The hidden client hides its pane and the phone releases: the pty goes to the hidden client's 100x30. With a release during attach creation, the released 100x30 grid comes back when the phone releases. 3 pass / 2 fail.
  • After: the pty goes to the remaining terminal's 120x40 within 1 s, with one resize. With no terminal viewer left, the size stays. 5 pass / 0 fail.
  • src/lib/ws.test.ts: before, hiding sent no release-size (62 pass / 1 fail); after, 63 pass.
  • Browser QA (three real pages): a hidden page sends exactly one release-size. When the phone returns to Chat, the visible desktop gets 37x143 back in 147 ms.

Checks: bun run typecheck exit 0; touched unit tests (ws, server/src/scripts conventions, changelog fold) 100 pass.

Release grid ownership immediately when keepSize hides a terminal, and allow an unchanged local grid to reclaim it. Track keep_size on the existing attach claim so a release during creation cannot revive a hidden grid. Only currently visible terminal clients qualify for handoff; leave size unchanged when none remain.

Add real-client three-viewer and pending-attach contract regressions. Verify desktop/phone browser handoff in 147 ms, typecheck, 100 touched unit tests, 3575 unit tests and fast checks. Unrelated cwd/file-path contract failures reproduce on the main server.

Refs #472
Plan: .omo/plans/issue-sweep-2026-10-14.md
@coderabbitai

coderabbitai Bot commented Oct 14, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 10 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 94172ad6-a0cc-40ac-b11f-883ec9742bfb
📥 Commits

Reviewing files that changed from the base of the PR and between 694b753 and 25b0943.

📒 Files selected for processing: 5
  • changelog.d/release-grid-hidden-clients.md
  • server/api.contract.test.ts
  • server/index.ts
  • src/lib/ws.test.ts
  • src/lib/ws.ts
  • 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

Comment @coderabbitai help to get the list of available commands.

@devswha
devswha merged commit 7776c4f into main Oct 14, 2026
29 of 33 checks passed
@devswha
devswha deleted the fix/release-grid-hidden-clients branch October 14, 2026 12:45
devswha added a commit that referenced this pull request Oct 14, 2026
…ed (#1095)

Refs #472

Follow-up to #1085 (merged), which hands the shared grid back only to a
device still showing the terminal.

## The regression

In #1085, `PaneTerminal`'s `leave()` sent `release-size` whenever the
page was out of use, and "in use" means `visibilityState === "visible"
&& document.hasFocus()`. A desktop window that is still on screen but
merely unfocused (a second monitor, alt-tab to another app, using the
phone beside the PC) therefore gave its grid away on every blur. When
the phone then went back to Chat, no candidate was left, so the visible
desktop stayed at phone width.

## The fix

- `leave()` now releases the grid only when the document is actually
hidden: `socket.keepSize(pane, document.visibilityState === "hidden")`.
- `HerdrSocket.keepSize(paneId, releaseGrid = true)`: with `releaseGrid
= false` it changes only the reconnect policy (`keep_size` on the next
attach) and sends no `release-size`.
- Blur's reconnect policy is unchanged. A pane hidden in a split (`shown
= false`) and a document that becomes hidden still release as before.
- Cleanup: drops a duplicate `mode !== "interact"` check in the server's
`release-size` handler (no `await` in between, so it was dead), and
widens the two 1 s `waitFor` deadlines in the release-size contract
tests to the 5 s their neighbors use.
- The changelog entry has its own fragment,
`changelog.d/keep-grid-unfocused-desktop.md`.

## Evidence

**Failing-first unit test**: `src/lib/ws.test.ts` > "keeps a visible
unfocused grid while changing only its reconnect policy"
- On #1085's head `25b09433` and on its squash `7776c4fd` (origin/main
at the time): FAIL, 63 pass / 1 fail, because a release-size frame was
sent on blur.
- On this branch: PASS, 64/64.

**Contracts**: the 5 `release-size` cases in
`server/api.contract.test.ts` pass on an isolated herdr session.

**Browser QA**: Playwright with a real herdr pty. Desktop 1280x800 in
the terminal lens, phone 390x844 touch on the same pane.
1. The phone opens Live terminal; the pty goes to 33x53.
2. The desktop stays `visibilityState: "visible"`, `hasFocus()` returns
false, and a window `blur` is dispatched.
3. The phone taps Chat transcript.

| | release-size frames from the desktop after blur | pty after phone
Chat |
| --- | --- | --- |
| before (origin/main) | 1 | stays at phone 33x53 (not restored within 3
s) |
| after (this branch) | 0 | desktop 37x143 after 167 ms |

The repo's browser regressions also pass on this branch:
`checkChatKeepsTerminalSize` (including the new blur block),
`checkPaneSwitchKeepsTerminalSize`,
`checkBackgroundTabKeepsTerminalSize` (#1068 cases, including "a desktop
window shown without the focus leaves the shared grid at the phone's 33
53"), `checkFocusedWindowReclaimsTerminalSize` and `checkSplitView`.
Typecheck passes.
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