Skip to content

Embedded terminal T1: PTY engine, IPC, lifecycle and packaging (no UI yet) - #25

Merged
BunsDev merged 8 commits into
mainfrom
term-engine
Sep 28, 2026
Merged

BunsDev merged 8 commits into
mainfrom
term-engine

Conversation

@BunsDev

@BunsDev BunsDev commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

First of two PRs porting the embedded terminal from the parked chat-workspace branch (3bc4ca6) onto current main. Plan: T1 engine (this PR, no visible change), T2 panel + ⌃` toggle.

Changes

  • engine/terminal.ts (taken unchanged from the parked branch): one login shell per thread, started in the thread's cwd (the worktree for worktree threads); 128 KB replay history; session id + sequence numbers so a reopened panel catches up without duplicates; validated sizes (2–500 × 2–300) and input (≤64 KB); idempotent close that ignores stale session ids.
  • IPC: terminal:open / write / resize / close + a terminal:event push channel (preload, bridge, shared types).
  • Trust: isTrustedTerminalSender allows only the live app window's top frame to drive a shell. will-navigate is blocked so the window, which keeps its preload across navigations, never leaves the app page. The renderer has no in-page links, so nothing else is affected.
  • Lifecycle: shells close on thread delete (before its worktree is removed), project remove, window closed, renderer gone and app quit.
  • Packaging: node-pty 1.1.0 (N-API prebuilds, so no Electron rebuild). scripts/prepare-terminal.mjs runs in build:main and restores spawn-helper's exec bit (npm installs it 0644). asarUnpack: node-pty/** is explicit; electron-builder also unpacks it on its own (verified by packaging without it).
  • Unchanged: the ⋯ menu's "Open terminal here" (Terminal.app).

Tests

  • Unit: the parked branch's 3 terminal tests + a new trust-check test. Mutation-checked: dropping the top-frame condition fails it (subframe).
  • Local: typecheck clean, unit 15 + 59, e2e 27/27.
  • Real PTY probe through IPC (.probes/pty-smoke.mjs, local only) on the dev build and a packaged Modex.app: tty=yes, cwd = the thread's folder, exit 7 delivered as an exit event.
  • CI runs no real PTY yet; T2's e2e will.

Copilot AI lite review requested due to automatic review settings September 27, 2026 03:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Four moderate findings in terminal sizing, stale-ID handling, byte-limit enforcement, and replay bounds must be fixed.

Review effort: Lite
Findings: 2 Medium severity

Open (2)
What changed in this PR

Adds the backend infrastructure for embedded per-thread terminals, including PTY management, IPC, lifecycle cleanup, trust checks, and packaging support, without UI changes.

Changes:

  • Adds terminal session management, replay, validation, and lifecycle handling.
  • Exposes terminal IPC and renderer bridge APIs.
  • Adds node-pty packaging configuration and tests.
File Summary
package-lock.json Locks PTY dependencies.
apps/​desktop/​test/​terminal.test.ts Adds terminal and trust tests.
apps/​desktop/​src/​shared/​types.ts Defines terminal IPC contracts; includes a documentation nit.
apps/​desktop/​src/​renderer/​bridge.ts Adds terminal bridge support.
apps/​desktop/​src/​main/​preload.cjs Exposes terminal event subscriptions.
apps/​desktop/​src/​main/​index.ts Registers IPC handlers and lifecycle cleanup.
apps/​desktop/​src/​main/​engine/​terminal.ts Implements PTY sessions; has four moderate findings around sizing, session IDs, byte limits, and replay bounds.
apps/​desktop/​scripts/​prepare-terminal.mjs Restores PTY helper executable permissions.
apps/​desktop/​package.json Configures PTY dependency, build, and packaging.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@BunsDev

BunsDev commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in b8c834f by merging main into term-engine and fixing the conflict in apps/desktop/src/main/index.ts so it keeps the terminal lifecycle/navigation guard changes alongside main's newer window-state and project-removal behavior.

BunsDev and others added 8 commits September 28, 2026 03:45
… yet)

Ports the shell engine from the parked chat-workspace branch onto current
main. Nothing visible changes; the panel and its title-bar toggle are T2.

- engine/terminal.ts (from 3bc4ca6, unchanged): one login shell per thread
  in the thread's cwd (the worktree for worktree threads), 128 KB replay
  history, session id + sequence so a reopened panel catches up without
  duplicates, validated sizes and input, idempotent close.
- IPC terminal:open/write/resize/close + a terminal:event push channel.
  isTrustedTerminalSender: only the live app window's top frame may drive a
  shell. will-navigate is blocked so the window (and its preload) never
  leaves the app page.
- Shells close on thread delete (before its worktree is removed), project
  remove, window closed, renderer gone and app quit.
- node-pty 1.1.0 (N-API prebuilds; no Electron rebuild). prepare-terminal.mjs
  restores spawn-helper's exec bit (npm installs it 0644). asarUnpack
  node-pty kept explicit; electron-builder also unpacks it on its own.
- The external "Open terminal here" action is unchanged.

Tests: 3 unit tests from the parked branch + a trust-check test
(mutation-checked: dropping the top-frame condition fails it). A local probe
opened a real PTY through IPC in both the dev build and a packaged
Modex.app: tty=yes, cwd = thread folder, exit code delivered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
BunsDev added a commit that referenced this pull request Sep 28, 2026
…hell

Stacked on T1 (#25). The thread's shell, from the T1 engine, now has UI.

- TerminalPanel: a bottom panel in the main pane on the design tokens and
  IconButton (new terminal/restart glyphs): folder or worktree branch,
  "Exited (n)", Restart, Close, drag or arrow-key resize. xterm 6 + fit
  addon load lazily on first open. The native viewport scrollbar is hidden
  (xterm 6 draws its own; wheel scrolling measured identical).
- Title-bar Terminal toggle one 34 px step left of ⋯ (⋯ and Changes do not
  move), and ⌃` from anywhere, including inside the shell. Visibility is per
  thread; hiding keeps the shell running and reopening replays its output.
- Under MODEX_E2E the shell is /bin/bash --noprofile --norc: a user's rc
  files (zsh + nvm here) raced the test's typing. Real use still starts the
  login shell; a unit test covers both paths.

Tests: e2e/terminal.spec.ts (6: real TTY in the thread folder, hide keeps
state, ⌃C/⌃J reach the shell, per-thread shells, taller panel = more rows,
exit/Restart/Close, worktree delete ends the shell) and a layout gate for
the toggle and panel. Mutation-checked: dropping the delete cleanup,
the output replay, or the panel's placement each fail a test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@BunsDev
BunsDev merged commit f657a67 into main Sep 28, 2026
2 checks passed
BunsDev added a commit that referenced this pull request Sep 28, 2026
…hell (#26)

* Embedded terminal T2: panel, title-bar toggle and ⌃`, e2e on a real shell

Stacked on T1 (#25). The thread's shell, from the T1 engine, now has UI.

- TerminalPanel: a bottom panel in the main pane on the design tokens and
  IconButton (new terminal/restart glyphs): folder or worktree branch,
  "Exited (n)", Restart, Close, drag or arrow-key resize. xterm 6 + fit
  addon load lazily on first open. The native viewport scrollbar is hidden
  (xterm 6 draws its own; wheel scrolling measured identical).
- Title-bar Terminal toggle one 34 px step left of ⋯ (⋯ and Changes do not
  move), and ⌃` from anywhere, including inside the shell. Visibility is per
  thread; hiding keeps the shell running and reopening replays its output.
- Under MODEX_E2E the shell is /bin/bash --noprofile --norc: a user's rc
  files (zsh + nvm here) raced the test's typing. Real use still starts the
  login shell; a unit test covers both paths.

Tests: e2e/terminal.spec.ts (6: real TTY in the thread folder, hide keeps
state, ⌃C/⌃J reach the shell, per-thread shells, taller panel = more rows,
exit/Restart/Close, worktree delete ends the shell) and a layout gate for
the toggle and panel. Mutation-checked: dropping the delete cleanup,
the output replay, or the panel's placement each fail a test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Refactor exit code assignment in TerminalPanel

Update exit code handling in TerminalPanel component.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

* Update aria-valuemax for terminal resize

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
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.

3 participants