Skip to content

fix(automations): a headless host closes finished run terminals, keeping the newest few - #25831

Merged
OrcaWin merged 5 commits into
OrcaWin/node-rt-phase3from
OrcaWin/node-rt-p3-headless-run-terminal-retention
Oct 6, 2026
Merged

OrcaWin merged 5 commits into
OrcaWin/node-rt-phase3from
OrcaWin/node-rt-p3-headless-run-terminal-retention

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​276 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​276
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​176 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​176

ELI5

Each time an automation runs on a remote Orca server, it opens a terminal. On the desktop, Orca closes that terminal when the run finishes. On a remote server nobody did that, so every run's terminal stayed open. One test host had 28 open shells after about six hours of three hourly automations. Over days that adds up to hundreds of shells, until the host runs out of resources. Now the server closes a completed run's terminal after 10 minutes and keeps the newest three per automation so you can still look at them.

What Changed

Before:

  • On the desktop, the renderer closes a run's terminal tab when the run completes. It keeps the tab if the user took it over, or if the run failed.
  • On orcad (and Electron orca serve) there is no renderer, and the server-side automation path had no equivalent. Every run's terminal tab, shell and PTY stayed open forever.

After:

  • The server-side automation service runs a sweep once a minute.
  • Closing: a terminal is closed once its run is completed and 10 minutes have passed since the server first saw it completed. The newest three completed runs per automation are always kept open. The close goes through the runtime's closeTerminal, which closes only the run's own pane, so a pane a user split into the same tab survives. Before closing, it checks which PTY the pane holds now. It closes only when that is still the run's own PTY. If a restart put a new PTY in the pane (restarting an exited pane, or the Codex account-switch restart), it closes nothing and only clears the run's terminal fields.
  • Run history: the closed terminal is then cleared from the run record. Its status, error and output snapshot are kept, so history still opens the run's result instead of a dead terminal. The desktop does the same after it closes a terminal.
  • Never closed:
    • a run that is still dispatching, dispatched or working, since its agent may still be running;
    • a failed or skipped run, for the same reason as on the desktop. A dispatch_failed run can still hold a live agent: one blocked on a permission prompt, one past the 6-hour watch window, or one after an observer error;
    • a terminal a client has taken over. That mirrors the desktop rule. It covers a terminal any paired client typed into since spawn (input classed driving; the automation's own launch writes are classed launch and don't count) and one a client is attached to or viewing (a raw or remote terminal view subscriber);
    • a terminal this process can't judge, for example one it adopted after an orcad restart instead of spawning it. Those stay open.
  • Lifecycle: the sweep starts and stops with the automation service. The desktop runner is unchanged.

Why

  • Why a grace period and the last N. That's what was asked for, and it's more forgiving than the desktop's close-on-completion. On a server you may want to look at a recent run's terminal from a paired client. The cap per automation is what bounds resources. Failed runs are left alone, as the desktop leaves them.
  • Why a periodic sweep instead of hooking each completion. It needs no extra wiring into the completion paths (watcher, runner, retained-run reconciliation). It also cleans up terminals left over from before an orcad restart.
  • How it detects a client taking over. The server has no focus signal like the desktop's, so it uses two facts it already tracks for each PTY. One is the first user-driven input. The other is whether a client view subscriber is attached. When it can't tell, it keeps the terminal, because closing a terminal someone is using would destroy their work.

Linked Issue

Fixes # (Phase 3 real-host finding on r7-b: 28 open shells after ~6h)

Visual Proof

N/A. No UI change.

Testing

All tests use fake timers.

  • Retention unit tests:

    • With six finished hourly runs, nothing closes before the grace period; after it, the three oldest close and the newest three stay.
    • Dispatched and dispatching runs are never closed, however old.
    • Failed and skipped runs are never closed.
    • Only the run's own pane is closed (closeTerminal, never closeTerminalTab).
    • A pane a restart gave a new PTY is never closed, and the run still forgets its old terminal.
    • The newest three are counted per automation.
    • The once-a-minute sweep runs by itself and stops when stopped.
  • Takeover unit tests: a terminal a client typed into is never closed. Neither is one a client is viewing, or one whose use can't be told.

  • Runtime tests for readTerminalClientUse:

    • A terminal with only launch input is unused.
    • One with client input is used.
    • One with a view subscriber is used, and goes back to unused when the subscriber releases.
    • An adopted PTY is unknown.
  • Service test: a server-side service closes the oldest run terminals through closeTerminalTab, clears them from the run record, and stops sweeping with the service. A desktop service never closes them. Run against the old service, the server-side test fails.

  • Local results: the automations and orcad-automations suites pass (36 files, 250 tests). pnpm tc:node and the changed-code quality gate are clean.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Server hosts on every platform get this, local and over SSH alike. It closes only terminals that belong to runs, never other terminals. No wire changes.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…ing the newest few

The desktop closes a run's terminal when the run completes; orcad had no
renderer to do it, so hourly automations left a shell and PTY per run open
forever (28 after ~6h on a real host). The headless service now closes a
finished run's terminal after a 10-minute grace, keeps the newest three per
automation viewable, and never touches a run that has not finished.
@OrcaWin
OrcaWin marked this pull request as draft October 6, 2026 07:55
m4air added 2 commits October 6, 2026 00:56
…s viewing

Mirrors the desktop's take-over rule on headless hosts: a finished run's
terminal stays open when any client drove input to it since spawn, is
attached to or viewing it, or when this process cannot tell (it adopted
the PTY rather than spawned it).
@OrcaWin
OrcaWin marked this pull request as ready for review October 6, 2026 07:57
@OrcaWin
OrcaWin marked this pull request as draft October 6, 2026 08:09
A failed run can still hold a live agent (blocked on a prompt, past the
watch window, or after an observer error), so like the desktop only a
completed run's terminal is closed. And only the run's own pane closes, so
a pane a user split into the same tab survives.
@OrcaWin
OrcaWin marked this pull request as ready for review October 6, 2026 08:10
@OrcaWin
OrcaWin marked this pull request as draft October 6, 2026 08:29
…s PTY

The use check read run.terminalPtyId, but the close hit whatever PTY now
occupies the run's pane. Restart-exited-pane and the Codex account-switch
restart put a new PTY there, so a terminal a user was using could be killed.
The close now resolves the pane's current PTY and closes only when it is the
run's own; otherwise it closes nothing and only clears the run's terminal.
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