ββββββ βββ ββββββββββββ βββββββ βββ βββββββββ βββ
βββββββββββ βββββββββββββββββββββ βββ ββββββββββ βββ
βββββββββββ βββ βββ βββ ββββββββββββββββ ββββββ βββ
βββββββββββ βββ βββ βββ ββββββββββββββββ ββββββ βββ
βββ ββββββββββββ βββ βββββββββ βββ ββββββββββββββββββββββ
βββ βββ βββββββ βββ βββββββ βββ ββββββββββββββββββββββ
ββββββββββββββββββββββββ ββββ βββββββββββ βββ ββββββ βββ
ββββββββββββββββββββββββββββββ βββββββββββββ ββββββββββββββ
βββ ββββββ ββββββββββββββββββββββββββββ ββββββββββββββ
βββ ββββββ βββββββββββββββββββββββββββββββββββββββββββ
βββ βββββββββββ ββββββ βββ βββββββββ βββββββββ βββββββββββ
βββ βββββββββββ ββββββ βββββββββ ββββββββ βββββββββββ
Problem Β· Fix Β· Quick Start Β· Setup Β· How it Works Β· FAQ
The Problem
AI coding agents (GitHub Copilot, Claude Code, Gemini, Cursor, etc.) spawn terminal sessions every time they run a command. They almost never clean up after themselves.
In GitHub Codespaces, this is catastrophic:
| Symptom | Cause |
|---|---|
| π§ Ghost terminals | Foreground terminals get reused β stale sessions persist and silently block new ones |
| π» Invisible accumulation | Background terminals in Codespaces aren't shown in the UI panel |
| π Infinite retries | Agents hammer the same dead session instead of killing it and starting fresh |
| π Lost terminal IDs | Without isBackground: true, no terminal ID is returned β cleanup is impossible |
The result: your Codespace fills with orphaned terminals β commands timeout β the agent gets confused β you waste tokens on retries.
The Fix
Add terminal management rules to your agent instruction files. These five bullet points are all you need:
## Terminal Management
- **Always use background terminals** (`isBackground: true`) for every command so a terminal ID is returned
- **Always kill the terminal** after the command completes, whether it succeeds or fails β never leave terminals open
- Do not reuse foreground shell sessions β stale sessions block future terminal operations in Codespaces
- In GitHub Codespaces, agent-spawned terminals may be hidden β they still work. Do not assume a terminal is broken if you cannot see it
- If a terminal appears unresponsive, kill it and create a new one rather than retrying in the same terminal
That's it. Copy those 5 lines into whatever agent instruction file you use.
Quick Start
# Option 1: Clone and copy what you need
git clone https://github.com/nirholas/auto-kill-terminal.git
cp auto-kill-terminal/.github/copilot-instructions.md your-project/.github/
# Option 2: Just copy the 5 bullet points from "The Fix" into your existing instruction file
Setup by Agent
## Terminal Management
- **Always use background terminals** (`isBackground: true`) for every command so a terminal ID is returned
- **Always kill the terminal** after the command completes, whether it succeeds or fails β never leave terminals open
- Do not reuse foreground shell sessions β stale sessions block future terminal operations in Codespaces
- In GitHub Codespaces, agent-spawned terminals may be hidden β they still work. Do not assume a terminal is broken if you cannot see it
- If a terminal appears unresponsive, kill it and create a new one rather than retrying in the same terminal
### Terminal Management
- **Always use background terminals** (`isBackground: true`) for every command so a terminal ID is returned
- **Always kill the terminal** after the command completes, whether it succeeds or fails β never leave terminals open
- Do not reuse foreground shell sessions β stale sessions block future terminal operations in Codespaces
- In GitHub Codespaces, agent-spawned terminals may be hidden β they still work. Do not assume a terminal is broken if you cannot see it
- If a terminal appears unresponsive, kill it and create a new one rather than retrying in the same terminal
### Terminal Management
- **Always use background terminals** (`isBackground: true`) for every command so a terminal ID is returned
- **Always kill the terminal** after the command completes, whether it succeeds or fails β never leave terminals open
- Do not reuse foreground shell sessions β stale sessions block future terminal operations in Codespaces
- In GitHub Codespaces, agent-spawned terminals may be hidden β they still work. Do not assume a terminal is broken if you cannot see it
- If a terminal appears unresponsive, kill it and create a new one rather than retrying in the same terminal
### Terminal Management
- **Always use background terminals** (`isBackground: true`) for every command so a terminal ID is returned
- **Always kill the terminal** after the command completes, whether it succeeds or fails β never leave terminals open
- Do not reuse foreground shell sessions β stale sessions block future terminal operations in Codespaces
- In GitHub Codespaces, agent-spawned terminals may be hidden β they still work. Do not assume a terminal is broken if you cannot see it
- If a terminal appears unresponsive, kill it and create a new one rather than retrying in the same terminal
Short:
**Terminal rules:** Always use `isBackground: true` for every terminal command, then kill the terminal after.
Two-line:
**Terminal management**: Always use background terminals (`isBackground: true`). Always kill terminals after use.
Why Background Terminals?
| Approach | Terminal ID returned? | Can be killed? | Blocks shell? |
|---|---|---|---|
isBackground: false |
β | β | β |
isBackground: true |
β | β
(kill_terminal) |
β |
Background terminals return an ID that the agent can pass to kill_terminal (or await_terminal β kill_terminal). Without that ID, there's no way to clean up.
Agent Terminal
| |
|-- run_in_terminal -----------------> | (isBackground: true)
| <-- terminal_id: a7b3c9d1 |
| |
|-- await_terminal ------------------> | (wait for completion)
| <-- output + exit code |
| |
|-- kill_terminal -------------------> | x_x
| X
|
|-- (clean slate for next command)
Supported Environments
| Environment | Status |
|---|---|
| GitHub Codespaces | β |
| VS Code (local) | β |
| VS Code (remote SSH) | β |
| VS Code (WSL) | β |
| Any VS Code terminal API consumer | β |
| Agent | Instruction File | Status |
|---|---|---|
| GitHub Copilot | copilot-instructions.md |
β |
| Claude Code | CLAUDE.md |
β |
| Gemini | GEMINI.md |
β |
| Cursor | AGENTS.md / .cursorrules |
β |
| Windsurf | AGENTS.md |
β |
| Aider | .aider.conf.yml |
β |
| Any agent using VS Code terminals | Any instruction file | β |
FAQ
No. This is just copy-paste text that goes into your agent instruction files. No packages, no extensions, no config.
Yes. The terminal management rules work anywhere VS Code's terminal API is used. They're most critical in Codespaces because ghost terminals are invisible there, but they help everywhere.
You can include the rules in your prompt directly. Use the one-liner from the "One-liners" section above.
No. Background terminals with isBackground: true behave the same as foreground terminals, except they don't block the shell and they return an ID for cleanup. The agent's commands still execute normally.
Contributing
Contributions are welcome! If you've found terminal management tricks for other agents or environments, please open a PR.
- Fork the repo
- Create your branch:
git checkout -b feat/my-improvement - Commit:
git commit -m "β¨ feat: add support for X agent" - Push:
git push origin feat/my-improvement - Open a Pull Request
License
All rights reserved. See LICENSE.
No comments yet
Be the first to share your take.