Skip to content

Background shells + auto-close for the create_shell MCP tool - #140

Draft
big-guy wants to merge 4 commits into
ness-dev:mainfrom
big-guy:background-shells
Draft

Background shells + auto-close for the create_shell MCP tool#140
big-guy wants to merge 4 commits into
ness-dev:mainfrom
big-guy:background-shells

Conversation

@big-guy

@big-guy big-guy commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Expands the create_shell MCP tool with two optional arguments so agents can run commands without commandeering the user's view, and clean up after themselves.

What's new

  • background: true — the shell tab is created without switching the UI to it. Its title renders italic (tab-unviewed) until the user selects it, at which point it's promoted to a normal tab.
  • close_delay (seconds, integer ≥ 0, default 30) — after a successful exit (code 0), the tab auto-closes once the delay elapses. 0 closes immediately on success.

Rules

  • A failed command (non-zero exit) never auto-closes — the user keeps the error output.
  • A tab the user is actively viewing when the timer fires stays open.
  • While a tab is armed, a corner prompt offers "Do not close" (disarm permanently) and "Close if successful" (dismiss the prompt, keep auto-close armed). The prompt hides once the command exits.

Implementation notes

  • background / closeDelay live on the TerminalTab slice (shared world state); the prompt-dismissed flag is renderer-local.
  • Exit codes aren't in the store's terminals/removed event, so the new ShellAutoCloseMonitor hooks PtyManager.addExitListener directly and re-reads state at fire time (so "Do not close" / active-tab transitions during the countdown are honored).
  • Background shells never receive a layout, so XTerminal spawns their PTY immediately at a fallback grid instead of deferring until visible.

Verification

npm run typecheck, npx electron-vite build, and npx vitest run all pass (71 reducer/FSM tests, including new coverage for the tabCloseDelayChanged reducer, addTab background activation, and selectTab flag-clearing).

🤖 Generated with Claude Code

@big-guy big-guy self-assigned this Jun 1, 2026
@big-guy
big-guy marked this pull request as ready for review June 1, 2026 05:37
big-guy and others added 4 commits June 4, 2026 17:47
Adds two optional args to the create_shell MCP tool:

- `background: true` — the tab is created without switching the UI to it,
  and its title renders italic until the tab is selected (selecting it
  drops the flag via selectTab). Background shells also spawn their PTY
  while hidden so the command actually runs even though the tab is never
  displayed.

- `close_delay` (seconds, >= 0, default 30) — after the command exits
  successfully the tab auto-closes once the delay elapses, unless it is
  its leaf's active tab at fire time. A non-zero (failed) exit never
  auto-closes. A corner banner ("Keep open" / "Close when done") lets the
  user cancel; "Keep open" clears closeDelay, which the monitor re-reads
  at fire time.

Implementation:
- terminals slice: new `background` + `closeDelay` tab fields and a
  terminals/tabCloseDelayChanged event/reducer.
- PanesFSM.addTab gains an { activate } option; selectTab clears the
  background flag; new setShellCloseDelay method (persists).
- PtyManager.addExitListener exposes exit codes; new
  ShellAutoCloseMonitor drives the close timer off it.
- XTerminal: spawn-while-hidden for background shells + the auto-close
  prompt banner; TerminalPanel italicizes background tab titles.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The "Keep open" / "Close when done" banner is only useful while the
command is still running. Wire the terminal-exit listener for shell tabs
(previously agent-only) and hide the banner once exited.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- Replace hardcoded `italic` tab styling with a semantic `tab-unviewed`
  Tailwind utility (styles.css) so the intent — an unviewed tab — is named.
- Reword the auto-close prompt buttons: "Keep open" -> "Do not close",
  "Close when done" -> "Close if successful" (and matching comments).
- Tighten the create_shell MCP `background` and `close_delay` arg
  descriptions in mcp-bridge.js.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- panes-fsm selectTab: replace the destructure + `void _bg` + `as TerminalTab`
  cast ceremony with a plain `{ ...tab, background: undefined }` patch.
- shell-autoclose-monitor locate(): collapse findLeafByTabId + redundant
  leaf.tabs.find re-search into one optional-chained lookup.

No behavior change. Typecheck, build, and 71 reducer/FSM tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@big-guy
big-guy force-pushed the background-shells branch from 385dedf to eaf70fb Compare June 4, 2026 21:49
@big-guy
big-guy marked this pull request as draft June 5, 2026 01:04
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