Skip to content

fix(terminal): give the shared grid back when a phone returns to Chat - #1068

Merged
devswha merged 2 commits into
mainfrom
fix/release-grid-on-chat-return
Oct 14, 2026
Merged

devswha merged 2 commits into
mainfrom
fix/release-grid-on-chat-return

Conversation

@devswha

@devswha devswha commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Fixes #472

Root cause

When a phone opened the Live terminal on a pane that a desktop was also showing, its attach and resize sized the shared pty to the phone's grid (server/index.ts:2864 attach, server/index.ts:3031 resize). Going back to Chat only marked the grid locally as covered (src/components/PaneTerminal.tsx:1910, keepSize). Nothing told the server, and the server didn't track whose grid the pty had, so the pty stayed at the phone's 53 columns. The desktop drew a phone-width terminal with a dead area on the right until its own resize, focus or click.

Change

  • shared/protocol.ts: new client frame release-size { pane_id }. It is never answered, and an older server ignores it.
  • src/lib/ws.ts / PaneTerminal.tsx: when the chat lens covers a terminal that this tab sized (interact mode only), it sends release-size once. A reconnect doesn't replay it; the reconnect attaches with keep_size.
  • server/index.ts: each attachment remembers each interact client's own grid while that client shows the terminal, plus which client drove the pty last. On release-size from the current driver, the pty takes the latest grid of another interact client still showing the terminal. If nothing qualifies, or another client resized in between, the size stays. A client covered by the chat lens never claims the size.
  • site/demo/transport.ts: accepts the frame without a reply. Changelog fragment added. REMOTE_BUNDLE_VERSION is not bumped: bundle sync happens at release time.

Evidence

  • Regression (fails on main): new api.contract.test.ts case. Desktop at 120x40, then the phone attaches at 53x33 and sends release-size: the desktop gets pane-geometry 120x40 within 1 s, and stty size in the pane reads 40 120. On origin/main it fails with "the grid given back not received within 1000ms".
  • Regression (fails on main): ws.test.ts case. The client sends release-size once, only for a grid it drove, never replays it, and never sends it as an observer.
  • Control: a competing resize from a third client after the release wins (the pty ends at 100x30, with at most 2 geometry frames and the last one the third client's).
  • Control: a release from a client that never drove, or for an unknown pane, changes nothing, sends no error frame and keeps the connection open. This also passes on main, so it covers an older server.
  • Browser QA (Playwright, built app, separate herdr session): desktop 1280x800 on the terminal, phone 390x844 (touch) opens in Chat, taps Live terminal (pty 33x53), then taps Chat transcript. Before: the pty stays at 33x53 and the desktop draws a 53-cell row. After: the phone sends release-size, the pty is back at the desktop's 37x143 within 46-157 ms, and the desktop draws a full 143-cell row. Screenshots at 1280x800 and 390x844 in light and dark: the phone's Chat screens are pixel-identical before and after, there is no horizontal scroll at 390, no console or CSP errors, and no new strings.

Checks

  • bun run typecheck: ok
  • HERDR_TEST_MODE=unit bun test ./src/lib/ws.test.ts ./scripts/conventions.test.ts ./src/conventions.test.ts ./server/conventions.test.ts ./scripts/changelog-fold.test.ts: 99 pass
  • bun run test:unit: 3516 pass, 0 fail, 8 skip
  • server/api.contract.test.ts -t release-size: 3 pass. Whole file: 120 pass, 2 fail, and both failures are unrelated to this change. "repository diff of a pane's folder" fails on origin/main the same way. "workspace and discovery endpoints" fails only under a nested TMPDIR on this machine, on origin/main too (2 of 3 runs), and passes 3/3 on both refs with a plain TMPDIR.

A phone that opened the Live terminal sized the shared pty to its own grid and kept it there after
going back to Chat, so a desktop on the same pane stayed at phone width until its own resize, focus
or click. The chat lens now sends a new client frame, release-size, when this tab drove the grid. The
server hands the pty to the latest grid of another interact client still showing the terminal, and
only while the releasing client is still the last driver: a resize another client sent in between is
never overwritten, and no client in Chat ever claims the size. An older server ignores the frame.

Tests: the api.contract release-size cases (regression, fails on main: the desktop grid returns within
1 s and the shell's stty size shows it; controls: a later competing resize wins with at most one
server resize, and a release from a client that never drove changes nothing and answers no error) and
a ws.test.ts case for when the client sends the frame (regression, fails on main). Fixes #472.
@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 5 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: 7cd4f76f-cf69-471c-af99-852ab217ccb2
📥 Commits

Reviewing files that changed from the base of the PR and between 0de23fc and 3d1b837.

📒 Files selected for processing: 8
  • changelog.d/release-grid-on-chat-return.md
  • server/api.contract.test.ts
  • server/index.ts
  • shared/protocol.ts
  • site/demo/transport.ts
  • src/components/PaneTerminal.tsx
  • 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.

Fix the blocking PR #1068 finding: keepSize marks reconnect policy, not whether the current connection still owes a grid release. Track that release separately and cover hide-then-Chat with a one-shot, no-replay regression.
@devswha
devswha merged commit 001cd89 into main Oct 14, 2026
17 checks passed
@devswha
devswha deleted the fix/release-grid-on-chat-return branch October 14, 2026 11:34
devswha added a commit that referenced this pull request Oct 14, 2026
…e terminal (#1085)

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.
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.

Returning to Chat leaves the desktop terminal at phone width

1 participant