Skip to content

Refactor terminal architecture to use Pty Host pattern#352

Merged
gregpriday merged 1 commit intomainfrom
refactor/issue-350-pty-host
Dec 2, 2025
Merged

Refactor terminal architecture to use Pty Host pattern#352
gregpriday merged 1 commit intomainfrom
refactor/issue-350-pty-host

Conversation

@gregpriday
Copy link
Copy Markdown
Collaborator

Summary

This PR refactors the terminal architecture to use the Pty Host pattern, moving terminal management into a separate UtilityProcess for improved Main thread responsiveness and concurrent agent scalability.

Closes #350

Changes Made

  • Implement PtyClient stub in Main process for IPC communication with Pty Host
  • Create pty-host.ts UtilityProcess entry point running PtyManager, TerminalObserver, and PtyPool
  • Define message protocol types for host-client IPC in shared/types/pty-host.ts
  • Update main.ts to initialize PtyClient instead of direct PtyManager usage
  • Add TerminalManager union type to support both PtyManager and PtyClient interfaces
  • Fix transition-state callback resolution with request correlation IDs
  • Reset ready promise on host restart to prevent race conditions
  • Null child process reference on exit to prevent posting to dead port

Technical Details

Architecture: The new pattern isolates CPU-intensive terminal I/O and state analysis in a separate UtilityProcess, preventing Main thread blocking. PtyClient provides a drop-in replacement for PtyManager with the same synchronous API surface, but all operations are forwarded to the host via IPC.

Restart Handling: The client automatically restarts the host up to 3 times on crashes, with exponential backoff. Terminal spawns are tracked locally and respawned after successful restart.

IPC Protocol: Request/response pairs use correlation IDs to handle concurrent operations correctly. Health checks run every 30s to detect hung hosts.

- Implement PtyClient stub in Main process for IPC communication
- Create pty-host.ts UtilityProcess entry point with PtyManager/TerminalObserver
- Define message protocol types for host-client IPC (shared/types/pty-host.ts)
- Update main.ts to initialize PtyClient instead of direct PtyManager usage
- Add TerminalManager union type to support both PtyManager and PtyClient
- Fix transition-state callback resolution with request correlation IDs
- Reset ready promise on host restart to prevent race conditions
- Null child process reference on exit to prevent posting to dead port
@gregpriday gregpriday merged commit 56c2ff2 into main Dec 2, 2025
@gregpriday gregpriday deleted the refactor/issue-350-pty-host branch December 2, 2025 14:32
@gregpriday
Copy link
Copy Markdown
Collaborator Author

gregpriday commented Apr 7, 2026

Terminal architecture refactor to Pty Host pattern disconnected the InputTracker service. Clear command detection (/clear, /new, /reset) broke. Fixed in #919.

Regression audit for training data.

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.

Refactor terminal architecture to use Pty Host pattern for concurrent agent scalability

1 participant