█████╗ ██╗ ██╗████████╗ ██████╗ ██╗ ██╗██╗██╗ ██╗
██╔══██╗██║ ██║╚══██╔══╝██╔═══██╗ ██║ ██╔╝██║██║ ██║
███████║██║ ██║ ██║ ██║ ██║█████╗█████╔╝ ██║██║ ██║
██╔══██║██║ ██║ ██║ ██║ ██║╚════╝██╔═██╗ ██║██║ ██║
██║ ██║╚██████╔╝ ██║ ╚██████╔╝ ██║ ██╗██║███████╗███████╗
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝╚══════╝╚══════╝
████████╗███████╗██████╗ ███╗ ███╗██╗███╗ ██╗ █████╗ ██╗
╚══██╔══╝██╔════╝██╔══██╗████╗ ████║██║████╗ ██║██╔══██╗██║
██║ █████╗ ██████╔╝██╔████╔██║██║██╔██╗ ██║███████║██║
██║ ██╔══╝ ██╔══██╗██║╚██╔╝██║██║██║╚██╗██║██╔══██║██║
██║ ███████╗██║ ██║██║ ╚═╝ ██║██║██║ ╚████║██║ ██║███████╗
╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝💀 Stop AI agents from leaving zombie terminals in your Codespace
问题 · 修复 · 快速开始 · 设置 · 原理 · 常见问题解答
______________________________________________________________________
问题
AI编码代理(GitHub Copilot、Claude Code、Gemini、Cursor等)每次运行命令时都会生成终端会话。 他们几乎从不清理自己。
在GitHub Codespace中,这是灾难性的:
| 症状 | 原因 |
|---|---|
| 🧟 幽灵终端 | 前台终端被重用——过时的会话会持续存在,并默默地阻止新的会话 |
| 👻 无形的积累 | 代码空间中的背景端子未显示在UI面板中 |
| 🔄 无限次重试 | 特工们会重复同样的死会话,而不是杀死它并重新开始 |
| 🆔 丢失终端ID | 没有 isBackground: true,不返回终端ID——无法清理 |
结果: 您的代码空间充满了孤立的终端→ 命令超时→ 代理人感到困惑→ 你在重试时浪费了令牌。
修复
将终端管理规则添加到代理指令文件中。这些 五个要点 你需要的都是:
## 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就这样 将这5行复制到您使用的任何代理指令文件中。
快速开始
# 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由代理设置
🤖 GitHub Copilot — copilot-instructions.md
## 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🧠 Claude Code — CLAUDE.md
### 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💎 Gemini — GEMINI.md
### 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🔮 Cursor / AGENTS.md — Multi-agent projects
### 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⚡ One-liners — For inline prompt space
短:
**Terminal rules:** Always use `isBackground: true` for every terminal command, then kill the terminal after.两条线:
**Terminal management**: Always use background terminals (`isBackground: true`). Always kill terminals after use.为什么是后台终端?
| 方法 | 返回终端ID吗? | 可以被杀死吗? | 挡壳? |
|---|---|---|---|
isBackground: false | ❌ | ❌ | ✅ |
isBackground: true | ✅ | ✅ (kill_terminal) | ❌ |
后台终端返回代理可以传递的ID kill_terminal (或 await_terminal → kill_terminal).没有那个身份证,就没有办法清理。
Agent Terminal
| |
|-- run_in_terminal -----------------> | (isBackground: true)
| | (wait for completion)
| | x_x
| X
|
|-- (clean slate for next command)支持的环境
| 环境 | 状态 |
|---|---|
| GitHub代码空间 | ✅ |
| VS代码(本地) | ✅ |
| VS代码(远程SSH) | ✅ |
| VS代码(WSL) | ✅ |
| 任何VS代码终端API消费者 | ✅ |
Supported Agents
| 代理 | 指令文件 | 状态 |
|---|---|---|
| GitHub副本 | copilot-instructions.md | ✅ |
| 克劳德代码 | CLAUDE.md | ✅ |
| 双子座 | GEMINI.md | ✅ |
| 光标 | AGENTS.md / .cursorrules | ✅ |
| 风帆冲浪 | AGENTS.md | ✅ |
| 助手 | .aider.conf.yml | ✅ |
| 任何使用VS Code终端的代理 | 任何指令文件 | ✅ |
常见问题解答
Do I need to install anything?
不是的。这只是复制粘贴文本,将其放入您的代理指令文件中。没有包,没有扩展,没有配置。
Does this work outside Codespaces?
对。终端管理规则适用于使用VS Code的终端API的任何地方。它们在代码空间中最为关键,因为鬼终端在那里是不可见的,但它们在任何地方都有帮助。
What if my agent doesn't support instruction files?
您可以直接在提示中包含规则。使用上面“一个衬垫”部分中的一个衬垫。
Will this break my agent's workflow?
否。带背景的终端 isBackground: true 其行为与前台终端相同,只是它们不会阻塞shell,并且会返回一个ID进行清理。代理的命令仍然正常执行。
贡献
欢迎投稿!如果您发现了其他代理或环境的终端管理技巧,请打开PR。
- 分叉回购
- 创建您的分支:
git checkout -b feat/my-improvement - 承诺:
git commit -m "✨ feat: add support for X agent" - 推:
git push origin feat/my-improvement - 打开拉取请求
许可证
MIT© nirholas
______________________________________________________________________
Stop wasting tokens on zombie terminals.
Made with 💀 for the AI agent community
⭐ Found this useful? Star the repo! ⭐
It helps others discover this project and keeps development active
