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
🤖 GitHub Copilot — copilot-instructions.md