Skip to content

refactor(terminal): move the terminal's mount effect into a hook - #1758

Merged
devswha merged 2 commits into
mainfrom
refactor/terminal-mount-hook
Oct 23, 2026
Merged

devswha merged 2 commits into
mainfrom
refactor/terminal-mount-hook

Conversation

@devswha

@devswha devswha commented Oct 23, 2026

Copy link
Copy Markdown
Owner

PR 1 of the PaneTerminal.tsx split. The plan is .omo/plans/paneterminal-split.md in the maintainer's workspace, and plan-reviewer approved it.

This PR moves the terminal's mount effect out of src/components/PaneTerminal.tsx into one hook, as one unchanged block. The hook is called at the same place in the component, so hook order, effect order, listener registration order and the cleanup order stay as they are. The effect still has [] dependencies and still captures the first render's values, exactly as before.

src/components/PaneTerminal.tsx goes from 3,665 to 1,966 lines.

Two commits: the pins first, then the move

  1. test(terminal): pin scrollback 0 and the mount-before-reset order before the split adds a describe block to src/conventions.test.ts, written against the unchanged code:

    • (a) every new Terminal({ in src/ sets scrollback: 0, wherever that code lives (the AGENTS.md trap);
    • (b) in PaneTerminal.tsx, the mount effect comes before the attach reset effect (the one with [paneId, terminalId]).

    Both pass on unchanged code. Removing scrollback: 0 fails (a), and moving the reset above the mount fails (b).

  2. refactor(terminal): move the terminal's mount effect into a hook

    • The mount effect: the whole useLayoutEffect moves, unchanged, into src/lib/useTerminalMount.ts. That is old lines 835-2494: xterm setup, IME, selection and copy, output and OSC handling, the attach stream, input and paste, resize/touch/pinch, and the lifecycle and cleanup.
      • It comes with the __HERDR_TEST_HOOKS__ declaration, which only this effect reads; its typeof guard keeps the Bun.build fixture working.
      • useTerminalMount(deps: TerminalMountDeps) destructures its deps and runs the block. The interface lists exactly the 105 names the effect reads from the component (69 refs, 26 setters and callbacks, 10 values), each typed as the component declares it. A missed name would be a type error.
      • The hook returns nothing. As before, the effect publishes through the refs it fills.
    • Shared helpers: RESIZE_SETTLE_MS, RELEASE_AFTER_MS, inUse, connectedSockets and plainError move verbatim, now exported, into src/lib/paneTerminalShared.ts. Both the hook and the code that stays use them.
    • Stays in PaneTerminal.tsx: one useTerminalMount({ … }) call where the block stood, after noteClipboard and before the theme effect. usePaneLane(stackRef) stays, as src/lib/settings.test.ts requires.
    • Pin (b) adapted: it now checks that useTerminalMount( comes before the attach reset. Moving the call below the reset fails it.
    • src/lib/i18n.test.ts: PART_FILES.terminal gains lib/useTerminalMount.ts, because the moved effect's t() keys belong to the terminal part.
    • scripts/terminal-ime-diagnostic.ts: this manual tool patches the onData anchor, which now lives in the hook, so it matches /src/lib/useTerminalMount.ts instead of /src/components/PaneTerminal.tsx.

Checks

  • --color-moved: git diff --color-moved=plain --color-moved-ws=allow-indentation-change origin/main...HEAD shows 3,375 lines as moved. The other 174 lines are:
    • 55 import lines;
    • the 7 export declarations and the 5 originals they replace;
    • the 105 interface fields, the destructuring line and the call-site object literal.
  • Moved block is exact: diff of old lines 835-2494 against the hook body is empty.
  • Typecheck: bun run typecheck passes.
  • Unit tests: HERDR_TEST_MODE=unit bun test src/conventions.test.ts src/lib/i18n.test.ts src/lib/settings.test.ts all pass.
  • Browser lane: bun run check integration browser passes, exit 0. That includes:
    • ui-regression and ui-contexts-regression;
    • the standalone terminal scripts;
    • chat-history-browser-qa, which builds the Bun.build fixture and proves the __HERDR_TEST_HOOKS__ guard moved intact.
  • Integration lane: one test fails, the same way on unchanged origin/main on this PC. That test is server/api.contract.test.ts "workspace and discovery endpoints > creates, edits, searches, inspects, and closes an owned workspace", line 1540, where /api/pane/files returns [] for the mention file. This PR changes no server code. CI's integration lane runs it in its own environment.
  • bun run check fast: passes (unit 6253 pass, 0 fail, 13 skip in 443 files, 8 at a time, 58.1s)

Outside PRs that touch the same files

No other maintainer PR touches these files. One outside contributor's PR touches src/components/PaneTerminal.tsx: #1639. git merge-tree shows it merges cleanly with both main and this branch.

The move touches the attach, observe and queued-input lines, so a code-review agent's stand-in review may be wanted before the merge.

Move-only: moved code plus imports, exports, the deps interface, one call, the pins and their adaptation, one PART_FILES entry and one diagnostic module id. No changelog fragment: users see no change.

Move the terminal's mount effect (new Terminal, the socket and session wiring, input and
touch handlers) verbatim into useTerminalMount, and the shared constants into paneTerminalShared.
PaneTerminal calls the hook at the position of the old effect, so effect order is unchanged.
Pin (b) now looks for the useTerminalMount( call before the attach reset; i18n PART_FILES and the
IME diagnostic's module id follow the moved code.
Plan: .omo/plans/paneterminal-split.md PR1.
@coderabbitai

coderabbitai Bot commented Oct 23, 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 1 minute.

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
📥 Commits

Reviewing files that changed from the base of the PR and between 8bd6480 and 55d91ab.

📒 Files selected for processing: 6
  • scripts/terminal-ime-diagnostic.ts
  • src/components/PaneTerminal.tsx
  • src/conventions.test.ts
  • src/lib/i18n.test.ts
  • src/lib/paneTerminalShared.ts
  • src/lib/useTerminalMount.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 commented Oct 23, 2026

Copy link
Copy Markdown
Owner Author

Merge pass: move-only PR (PaneTerminal split, PR 1). As AGENTS.md allows, it is merged on its checks without waiting for a CodeRabbit review.

  • Pure move. A line-multiset comparison shows the only differences are:
    • imports;
    • the TerminalMountDeps interface and its destructure;
    • the one useTerminalMount({ … }) call;
    • export on the five shared helpers.
  • Gate review. A gate reviewer on a different model than the author's confirmed:
    • the 1,660-line effect moved unchanged;
    • all 105 captured names are the same bindings, from the first render;
    • hook order is unchanged;
    • connectedSockets is still a single shared instance.
  • Tests.
    • 107 unit tests pass (conventions, i18n, settings), and the typecheck passes.
    • Both new pins (scrollback: 0 and mount-before-reset) fail when deliberately violated.
    • terminal-input-regression and terminal-ipad-ime-regression pass, on fresh builds and isolated test sessions.
  • Overlap. Outside contributor feat(chat): hold Space in the empty composer for Claude Code's voice mode #1639 also touches src/components/PaneTerminal.tsx and will need a rebase.

All required checks are green and no review thread is open.

@devswha
devswha merged commit e3aec15 into main Oct 23, 2026
9 checks passed
@devswha
devswha deleted the refactor/terminal-mount-hook branch October 23, 2026 12:44
devswha added a commit that referenced this pull request Oct 23, 2026
… sends into hooks (#1764)

PR 2 of the PaneTerminal.tsx split. The plan is
`.omo/plans/paneterminal-split.md` in the maintainer's workspace, and
plan-reviewer approved it. It follows PR #1758, which moved the mount
effect.

This PR moves four blocks out of `src/components/PaneTerminal.tsx` into
four hooks. Each block moves as one unchanged span. Each hook is called
at the exact line its block occupied, so hook order, effect order and
cleanup order stay as they are. Every effect keeps its dependency list,
and every callback keeps its `useCallback` dependencies.

`src/components/PaneTerminal.tsx` goes from 1,967 to 1,477 lines.

## What moves

| Hook | Block | Deps | Returns |
|---|---|---|---|
| `src/lib/useTerminalRefits.ts` | the six theme, font and fit effects
(133 lines) | 27 | nothing |
| `src/lib/usePaneAttach.ts` | the reset layout effect on a pane or
terminal replacement (`[paneId, terminalId]`) and the two one-line
effects after it (80 lines) | 50 | nothing |
| `src/lib/useTerminalSends.ts` | the send callbacks and the
direct-typing effect (144 lines) | 37 | `sendDraft`, `discardDraft`,
`submitComposerMessage`, `sendComposerText`, `sendTerminalLine`,
`reassertGrid`, `pressEnter`, `toggleDirect` |
| `src/lib/useComposerSends.ts` | `composerSend` and `actOnPending` (138
lines) | 29 | `composerSend`, `actOnPending` |

- **The hooks:** each follows `useTerminalMount`'s pattern. A
`<Name>Deps` interface lists every name the block reads from the
component, each typed as the component declares it, and the hook
destructures it on its first line. A missed name would be a type error.
- **Call order in `PaneTerminal.tsx`:** `useTerminalMount`, then
`useTerminalRefits`, then `usePaneAttach`. Further down, where their
blocks stood, come `useTerminalSends` and `useComposerSends`.
`usePaneLane(stackRef)` does not move.
- **Order pin:** `src/conventions.test.ts` used to check that the mount
hook comes before the attach reset effect, found via `}, [paneId,
terminalId]);`. That effect now lives in `usePaneAttach`, so the pin
checks that `useTerminalMount(` comes before `usePaneAttach(`. Swapping
the two calls fails it.
- **`src/lib/i18n.test.ts`:** `PART_FILES.terminal` gains
`lib/useComposerSends.ts`, because the moved callbacks' `t()` keys
belong to the terminal part.

## Checks

- **`--color-moved`:** `git diff --color-moved=plain
--color-moved-ws=allow-indentation-change origin/main...HEAD` shows
1,009 lines as moved. The other 229 lines are:
- PaneTerminal.tsx's narrowed imports, the four new imports and the four
call lines;
- the new files' imports, the four `Deps` interfaces (143 field lines),
signatures, destructuring lines and returns;
  - the pin's two-line adaptation and the `PART_FILES` entry.
- **Moved blocks are exact:** each original span (lines 797-929,
931-1010, 1070-1213 and 1386-1523 of `PaneTerminal.tsx` on `e3aec1528`)
appears byte for byte inside its hook.
- **Typecheck:** `bun run typecheck` passes.
- **Unit tests:** `HERDR_TEST_MODE=unit bun test src/conventions.test.ts
src/lib/i18n.test.ts src/lib/settings.test.ts
scripts/conventions.test.ts`: 117 pass, 0 fail.
- `bun run check fast`: passes (unit 6253 pass, 0 fail, 13 skip in 443
files, 8 at a time, 61.1s)
- **`bun run check integration browser`:** the browser lane exits 0
(every browser script passed, 2085 s), run on this commit before its
rebase onto the latest main (which changed only `src/App.tsx`,
`src/app-hooks.test.ts` and server files). The integration lane had
three failures, none in code this PR touches:
`server/api.contract.test.ts` "creates, edits, searches, inspects, and
closes an owned workspace" (line 1540, fails the same way on unchanged
`origin/main` on this PC), and two in `server/gjc.contract.test.ts`
("binds same-cwd panes independently by native PID…", "falls back to
scrollback when the native GJC binding is missing": `pane.process_info`
did not yet list the isolated GJC process while another lane ran beside
it). Rerun alone, `server/gjc.contract.test.ts` passes. CI runs the
integration lane in its own environment.

## Outside PRs that touch the same files

No other maintainer PR touches these files. One outside contributor's PR
touches `src/components/PaneTerminal.tsx`: #1639. `git merge-tree` shows
it still merges cleanly with both main and this branch.

The moved blocks include the attach reset, the queued-input sends and
the pending-message actions. A code-review agent's stand-in review may
be wanted before the merge.

Move-only: moved blocks plus imports, deps interfaces, four calls, the
adapted order pin and one `PART_FILES` entry. No changelog fragment:
users see no change.
devswha added a commit that referenced this pull request Oct 23, 2026
…1767)

PR 3 of 3 of the PaneTerminal.tsx split. The plan is
`.omo/plans/paneterminal-split.md` in the maintainer's workspace, and
plan-reviewer approved it. It follows PR #1758 and PR #1764.

This PR moves the chat lens's terminal logic out of
`src/components/PaneTerminal.tsx` into three hooks. Each block moves as
one unchanged span. Effects keep their dependency lists, and callbacks
keep their `useCallback` dependencies.

`src/components/PaneTerminal.tsx` goes from 1,477 to 1,174 lines. Over
the three PRs it went from 3,665.

## What moves

| Hook | Block | Deps | Returns | Called |
|---|---|---|---|---|
| `src/lib/usePeekReturn.ts` | after a peek: the manual-peek and
auto-return state, the still-open note, the input-row frame watch, and
the peek-end layout effect (153 lines) | 29 | 12 names | where the block
stood |
| `src/lib/useChatCallbacks.ts` | the chat lens's callbacks and state:
conversation watch, terminal output, sent and metadata, greeting room,
prompt, suggestion, restore and pending answer (90 lines) | 12 | 28
names | where the block stood, right after `usePeekReturn` |
| `src/lib/useTypedKeys.ts` | the key bar's `pressKey` and
`toggleModifier` (30 lines), and Hermes' one-Ctrl+C stop guard with
`abortTurn` (34 lines) | 16 | `pressKey`, `toggleModifier`, `abortTurn`
| where the stop block stood |

### Hook and effect order

- **`useTypedKeys`:** the key-bar callbacks used to sit about 15 lines
above the stop block. They have no effects, and only JSX reads them, so
creating them a few hooks later changes nothing. Nothing between the two
old positions reads them.
- **`usePeekReturn` and `useChatCallbacks`:** each sits exactly where
its block was, so their effects run in the same order as before.

### Other changes

- **`StillOpen`:** this was a local type alias inside the component. Two
hooks now use it, so it moves to `src/lib/paneTerminalShared.ts` as an
exported type. Types have no runtime effect.
- **`src/lib/i18n.test.ts`:** `PART_FILES.terminal` gains
`lib/usePeekReturn.ts` and `lib/useTypedKeys.ts`, because their `t()`
keys belong to the terminal part.
- **Imports:** `PaneTerminal.tsx` drops the imports only the moved code
used.

## Checks

- **`--color-moved`:** `git diff --color-moved=plain
--color-moved-ws=allow-indentation-change origin/main...HEAD` shows 629
lines as moved. The other 116 lines are:
- `PaneTerminal.tsx`'s narrowed imports, the three new imports and the
three calls;
- the hooks' imports, three `Deps` interfaces (57 field lines),
signatures, destructuring lines and returns;
  - the `StillOpen` type's move;
  - the two `PART_FILES` entries.
- **Moved blocks are exact:** lines 646-735, 819-848 and 863-896 of
`PaneTerminal.tsx` on `2fa93ab49` appear byte for byte in their hooks.
Lines 493-645 do too, apart from the one `StillOpen` line.
- **Typecheck:** `bun run typecheck` passes.
- **Unit tests:** `HERDR_TEST_MODE=unit bun test src/conventions.test.ts
src/lib/i18n.test.ts src/lib/settings.test.ts
scripts/conventions.test.ts src/lib/terminalPeek.test.ts
src/lib/keyBar.test.ts src/lib/hermesStop.test.ts`: 163 pass, 0 fail.
- `bun run check fast`: passes after the rebase (unit 6253 pass, 0 fail,
13 skip in 443 files, 8 at a time, 57.5s)
- **`bun run check integration browser`:** the browser lane exits 0
(every browser script passed, 1895 s), run on this commit before its
rebase onto the latest main (which changed only `src/App.tsx`,
`src/app-hooks.test.ts`, `src/lib/useSidebarLayout.ts` and server
files). The integration lane's only failure is
`server/api.contract.test.ts` "creates, edits, searches, inspects, and
closes an owned workspace" (line 1540, `/api/pane/files` returns `[]`),
which fails the same way on unchanged `origin/main` on this PC. CI runs
the integration lane in its own environment.

## Outside PRs that touch the same files

No other maintainer PR touches these files. One outside contributor's PR
touches `src/components/PaneTerminal.tsx`: #1639. It already conflicts
with main (`src/lib/i18n.ja.ts`, `src/lib/i18n.ko.ts`,
`src/lib/i18n.zh.ts`) and needs a rebase either way; against this branch
it conflicts in `src/components/PaneTerminal.tsx`, `src/lib/i18n.ja.ts`,
`src/lib/i18n.ko.ts`, `src/lib/i18n.zh.ts`.

The moved blocks include the queued-input stop path, Hermes' stop guard
and the peek's auto-return. A code-review agent's stand-in review may be
wanted before the merge.

Move-only: moved blocks plus imports, deps interfaces, three calls, one
shared type and two `PART_FILES` entries. No changelog fragment: users
see no change.
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