# Closing Agents

`dot-agent-deck close` stops agents from the command line: a unit a [dispatcher](dispatcher-mode.md) started, one agent, or a whole [orchestration](orchestration.md). It does what closing a card or a tab does in the TUI or the desktop app, and it works from a shell or from a dispatcher agent, so a dispatcher can tidy up the units it started when you ask it to.

By default, closing a dispatched unit also removes its worktree when that worktree has no uncommitted changes and no other worktrees inside it, unless you ask to keep it (see [Keep a worktree on purpose](#keep-a-worktree-on-purpose)). The unit's branch is always kept.

## Close from a shell

Run `close` from a terminal that is not one of the deck's panes. Name the units by the name they were dispatched under:

```bash
dot-agent-deck close fix-auth-bug
dot-agent-deck close fix-auth-bug verify-pr-1201
```

To see first what you could close, list everything:

```bash
dot-agent-deck close --all
```

`--all` on its own closes nothing. It prints every unit with its unit id, its panes and their statuses, whether it has reported back, its worktree and branch, and the dispatcher that started it. When the list is what you want closed, run:

```bash
dot-agent-deck close --all --yes
```

That lists the units again and then closes exactly the units in that list, by their unit ids, so a unit started in the meantime is not swept up. A listed unit marked `would be refused` is refused again unless you add `--force`. If more than 256 units are running, `--all` lists the first 256 and says so; close those, then list again. `--all --yes` closes the 256 it listed and says that more are still running.

To close one unit by the id a list printed, use `--unit-id`:

```bash
dot-agent-deck close --unit-id u-3f9a2c71d04e8b56-4
```

**Check it worked:** the output has a `closed:` section naming each unit and each of its panes with `— stopped`, and a `worktrees:` section saying what happened to each worktree. The unit's card (or its tab in the TUI, its **ORCHESTRATION** group in the desktop app) disappears from every client attached to the daemon.

```
closed:
  fix-auth-bug (u-3f9a2c71d04e8b56-4, single)
    pane 7 agent 12 Idle — stopped
worktrees:
  /home/you/src/api-dispatch-fix-auth-bug: removed (branch agent/dispatch-fix-auth-bug kept)
```

Inside a pane the deck started, `close` acts as that pane's agent, not as you, and the agent rules below apply. A plain shell you opened in a deck pane counts as that pane.

## Close from a dispatcher agent

A dispatcher pane knows the `close` verb. Ask it: *"close the units that have reported back"*, or *"close fix-auth-bug"*. It runs `close` by the deck's full path, the same way it runs `dispatch`. If you let the agent run commands through a permission rule, write the rule against the path shown in the dispatcher's pane.

A dispatcher closes units only when you ask; deciding when work is finished stays with you. A standing instruction works too: *"each time a unit reports back and its PR is merged, close it"*.

An agent running `close` is held to these rules, and `--force` does not change any of them:

- It can close only units it dispatched itself. A unit another dispatcher started is refused (`not-your-unit`), and so is any other agent on the deck.
- It cannot close its own pane, or any role of the orchestration it belongs to (`own-pane`).
- Closing a unit does not close the units that unit dispatched in turn. Those are left running, and the output lists them under `left open (dispatched by it)` so you can close them yourself.
- `close --all` from an agent lists only the units that agent may close.

## Choosing what to close

```
dot-agent-deck close <UNIT>... [--dry-run] [--force] [--keep-worktree] [--json]
dot-agent-deck close --unit-id <ID>... [--dry-run] [--force] [--keep-worktree] [--json]
dot-agent-deck close --pane <PANE_ID> [--dry-run] [--force] [--keep-worktree] [--json]
dot-agent-deck close --orchestration-of <PANE_ID> [--dry-run] [--force] [--keep-worktree] [--json]
dot-agent-deck close --all [--yes] [--dry-run] [--force] [--keep-worktree] [--json]
```

| Argument | What it closes |
|---|---|
| `<UNIT>...` | The dispatched units with these names: the name given to `dispatch`. A unit started as an orchestration is closed whole, orchestrator first. |
| `--unit-id <ID>...` | The dispatched units with these unit ids, as `--all` or an `ambiguous` refusal printed them. A unit id names one unit and the daemon never reuses it while it runs, so it cannot reach a different unit that took the same name later. Unit ids do not survive a daemon restart, and an id from before a restart is very unlikely to match a unit of the new daemon. |
| `--pane <PANE_ID>` | The one agent in this pane. |
| `--orchestration-of <PANE_ID>` | Every role of the orchestration this pane belongs to, orchestrator first. |
| `--all` | Nothing: it lists every unit you may close. |
| `--all --yes` | The units `--all` listed. |
| `--dry-run` | Nothing: it shows what would be closed and what would be refused. |
| `--force` | Also closes a unit that has not reported back, an agent that is busy, and one role of a running orchestration (see the next section). |
| `--keep-worktree` | Closes the same things, but leaves each dispatched worktree on disk instead of removing it (see [Keep a worktree on purpose](#keep-a-worktree-on-purpose)). |
| `--json` | Prints the report as JSON instead of text. |

Pane ids appear in the `--all` list and in `dot-agent-deck daemon status`. There is no pattern or wildcard selector; name what you mean, or use `--all` and read the list. One command names at most 256 units.

## What is refused, and why

Without a terminal you cannot be shown a confirmation dialog, so `close` refuses the cases a dialog would make you think twice about. Each refusal names the reason. A refused unit is left exactly as it was, including every role of a refused orchestration; the other units named in the same command are still closed, and the command exits `1`.

| Reason | What it means | What to do |
|---|---|---|
| `not-reported` | The unit has not reported back with `work-done --done`. It may still be working. | Wait for its report, or check its card. Pass `--force` to close it anyway. |
| `busy` | One of the agents is Thinking, Working, Compacting, waiting for input (**Needs Input**), Blocked or **Waiting**, or is Idle with commands or subagents still running in the background (the **Background:** line on its card). | Let it finish its turn, or answer it. Pass `--force` to close it anyway, which also stops that background work. |
| `strands-orchestration` | `--pane` named one role of a running orchestration, which would leave the rest of the team without it. | Close the whole team with `--orchestration-of <PANE_ID>`, or pass `--force`. |
| `not-your-unit` | An agent asked to close something it did not dispatch. | Close it yourself from a shell, or ask the dispatcher that started it. |
| `own-pane` | An agent asked to close itself or its own team. | Close it from a shell or a client. |
| `not-attested` / `superseded` | The request came from a pane the deck cannot confirm, or from an agent that has since been replaced in its pane. | Run `close` from the agent now in that pane, or from a shell. |
| `unknown-unit` | No running unit has that name. Units are not remembered after the daemon restarts. | Check the name with `close --all`. After a restart, `dot-agent-deck interrupted list` shows the units the restart interrupted, and reclaims or dismisses them ([Interrupted Work](interrupted-work.md)). |
| `already-ended` | The unit has already been closed or has ended. | Nothing to close. |
| `ambiguous` | Units with that name are running in more than one repository. The refusal lists each one with its unit id, worktree and panes. | Close the one you mean with `--unit-id` and its id from that list. |
| `unknown-pane` | No agent is in that pane. | Check the id with `daemon status`. |
| `not-an-orchestration` | `--orchestration-of` named a pane that is not part of an orchestration. | Use `--pane`. |
| `selector-too-large` | The command named more than 256 units, or a name or id far longer than any real one. Nothing was closed. | Close them in smaller batches. |

`--force` overrides `not-reported`, `busy` and `strands-orchestration` only. A forced close says so in its output, for example `(forced: was Working)`.

**Has it reported back** is the check that works the same for every agent: it depends only on the unit running `work-done --done`. The `busy` check depends on the status each agent reports, and some report less than others:

- **Pi** does not report **Needs Input** or **Blocked**, so a Pi agent at a prompt or out of credit is not refused as busy.
- **Devin** does not report **Blocked** or **Error**.
- **Antigravity** does not report **Blocked**. An `agy` started from a shell inside a pane reports **Needs Input** only from its first prompt on ([Installation → Antigravity CLI](installation.md#antigravity-cli)), so before that one at a start-up question is not refused as busy.
- **Codex** reports its status fully when the deck's Codex hooks are trusted. When they are not (see [Codex events not showing](troubleshooting.md#codex-events-not-showing)), a Codex agent reads **Idle** once its screen has been still for about three seconds, including while a permission prompt waits for an answer, so it is not refused as busy.

An agent whose status the deck has not heard yet is not refused as busy either. Read the `--dry-run` output, or the agent's card, when it matters.

## Worktrees

When the last agent of a dispatched unit is closed, whether you named the unit, its pane or its orchestration, the deck deals with the unit's worktree the same way closing its card does, and `close` waits for the result and reports it. Closing one role of an orchestration with `--pane` while other roles keep running leaves the shared worktree alone:

| Output | What happened |
|---|---|
| `removed (branch … kept)` | The worktree had no uncommitted changes and was removed. Its branch is kept, because it may hold committed work. |
| `kept on request` | You passed `--keep-worktree`, so the worktree was left on disk whatever its state. The output also tells you how to find or remove it later. |
| `kept: uncommitted changes` | The worktree is left on disk so the work can be recovered. |
| `kept: holds other worktrees` | Other worktrees or Git checkouts are inside it, of its own repository or of any other, for example ones an agent created under a git-ignored directory such as `.worktrees/`. Removing it would delete them along with their uncommitted work, so it is kept. The output names up to 16 of them under `holds worktree`; `git worktree list`, run in the repository, shows those of its own repository. Move or remove those first (`git worktree remove <path>`, run in the repository each one belongs to), then this worktree. The worktree may also have uncommitted changes; this line is shown instead so that you see the worktrees inside it before you discard anything. |
| `kept: could not check for uncommitted changes or worktrees inside it` | The deck could not tell whether the worktree was clean, or whether other worktrees or Git checkouts lie inside it (a folder it could not read, or more files than it looks through), so it kept it. |
| `kept: removing it failed` | Removing the worktree failed; it is still on disk. |
| `kept: still in use` | Something else was using the directory at the time. |
| `cleanup still running: …` | The agents are stopped, but the removal had not finished within about 30 seconds. Check with `dot-agent-deck worktree list`. |
| `not recorded by the daemon …` | The daemon has no record of this worktree, for example because it restarted after the unit started. |

### Keep a worktree on purpose

Removing a clean worktree also removes the files git ignores in it, such as the unit's `.dot-agent-deck/` (task files, saved reports, an orchestrator's notes) and its build cache. When you are stopping a unit to start it again in the same directory, for example to upgrade the deck in the middle of a long orchestration, add `--keep-worktree` and the deck leaves the worktree on disk whatever its state, clean or not:

```bash
dot-agent-deck close fix-auth-bug --keep-worktree
dot-agent-deck close fix-auth-bug --keep-worktree --dry-run
```

`--keep-worktree` works with every way of choosing what to close: unit names, `--unit-id`, `--pane`, `--orchestration-of` and `--all --yes`. With `--dry-run` it shows `would be kept on request` instead of closing anything. The `worktrees:` section reports the directory as `kept on request` and reminds you how to find it again. If you close just one role of a running orchestration with `--keep-worktree` (`--pane <PANE_ID> --force`, since `close` otherwise refuses to leave the rest of the team without it), the worktree the roles share stays on disk even after you close the other roles without `--keep-worktree`. The TUI and the desktop app offer the same choice in their close confirmations (see [Closing in the TUI and the desktop app](#closing-in-the-tui-and-the-desktop-app)).

A worktree kept this way is yours: closing a later agent you start in the same directory does not remove it. When you are done with it, `dot-agent-deck worktree list` shows it and `dot-agent-deck worktree reclaim` removes it under the same rules as any other leftover worktree, which keep a worktree whose pull request has not merged ([Dispatcher Mode → Finish up](dispatcher-mode.md#finish-up)); run both from the repository the worktree belongs to. To remove it before that, run `git worktree remove <directory>` there, adding `--force` to discard uncommitted changes in it.

If the daemon you are running was started by an older version of the deck, it cannot keep a worktree on close. `close --keep-worktree` then refuses with `cannot keep a worktree on close; nothing was closed` and exits `2`, so nothing is closed and nothing is removed. Restart the daemon onto this version with `dot-agent-deck daemon restart` and run the command again. The TUI and the desktop app do not offer the choice against such a daemon.

### Leftover worktrees

After a daemon restart, the units it interrupted are listed in [Interrupted Work](interrupted-work.md), in the TUI (`I`), the desktop app and `dot-agent-deck interrupted list`, where **Reclaim** removes a unit's worktree and keeps its branch. To clean up other worktrees the deck no longer knows about, use `dot-agent-deck worktree list` and `dot-agent-deck worktree reclaim` ([Dispatcher Mode → Finish up](dispatcher-mode.md#finish-up)). A kept branch blocks dispatching the same name again until you delete it (`git branch -D agent/dispatch-<name>`).

## Exit status and JSON

| Exit status | Meaning |
|---|---|
| `0` | Everything named was closed, or listed. |
| `1` | Something was refused, a close failed or was only partial, or the command was run with an incomplete deck environment (see below). |
| `2` | The running daemon is too old for `close`, or for `--keep-worktree`. Nothing was closed. |
| `3` | No daemon is running. Nothing was closed. |

`--json` prints the daemon's report with four lists added for scripts: `closed`, `refused`, `listed` and `worktrees`, plus `exit_code`. Each entry names the unit in `name` and gives its `unit_id`. In `closed`, `refused` and `worktrees`, a unit id the deck does not know is `null`, and a refusal of the whole command, rather than of one unit, is an entry in `refused` whose `name` and `unit_id` are both `null`. Entries in `listed` always carry `panes`, and carry `name`, `unit_id`, `reported`, `completed_at_ms`, `worktree`, `branch` and `clone` only when there is something to report, leaving each one out rather than setting it to `null`: `completed_at_ms`, for example, is missing for a unit that has not finished. With `--all --yes`, the list that was closed from is under `preview`. `truncated` is `true` when `--all` listed only the first 256 units. An entry of the report's `targets`, and of `listed`, carries its own `truncated: true` when one of its lists — the candidates of an `ambiguous` refusal, their panes, the units it left open, or its panes — shows only part of what there was, or when a report too large to send whole had its long fields shortened, each ending in `…`; the human output says so under that entry. A unit's `unit_id` is never shortened, so you can always pass it to `close --unit-id`. A shortened list or field changes only the report: every pane of a unit that was closed is stopped.

A script that closes the units whose pull requests merged can join `close --all --json` with `dot-agent-deck worktree list --json`, which reports each worktree's PR state, on the branch, and close each selected unit with `close --unit-id` and the `unit_id` from `listed`.

## Closing in the TUI and the desktop app

The TUI and the desktop app close the same things and clean up worktrees the same way, with a confirmation dialog instead of the refusals above.

**TUI:** select the card on the dashboard, or go to the orchestration's tab, and press `Ctrl+W` in command mode, or click the card's `[Close]` button or the tab's `[×]`. Choose **Close** in the confirmation. If the unit's worktree has uncommitted changes, the confirmation says it will be kept and names it, and the status line reports what happened after the close. If other worktrees are inside it, the confirmation reads `Holds other worktrees, so it is KEPT:` and lists them under `holds:`. To keep a dispatched unit's worktree whatever its state, choose **Close, keep worktree** instead: the status line then reads `Worktree kept on request:` followed by its directory. The choice appears only when the deck daemon you are attached to can keep a worktree; after an upgrade, restart the daemon to get it. See [Keyboard Shortcuts](keyboard-shortcuts.md).

**Desktop:** press the stop control on the agent's row and then **Close agent**, or **Close** on an orchestration's group header and then **Close all N roles**. For a role of an orchestration, the agent's confirmation offers both **Close agent**, which closes only that role, and **Close orchestration**, which closes every role as the group header's **Close** does; on the orchestrator, **Close orchestration** is the highlighted choice. As in the TUI, the confirmation warns when uncommitted changes, or other worktrees inside it, will keep a dispatched worktree, and names its directory and those worktrees. For a dispatched unit, the confirmation also shows a **Keep the worktree** checkbox; tick it and the close you press keeps the worktree whatever its state, and the notice then reads `Worktree kept on request:` followed by the directory. Closing one role of an orchestration with the box ticked keeps the worktree the roles share, even after you close the other roles without it. The TUI has no single-role close: closing a role's card closes its whole orchestration, so its keep choice keeps the worktree for every role at once, and it offers the keep as a separate **Close, keep worktree** choice rather than a checkbox. As in the TUI, the checkbox appears only when the daemon can keep a worktree. After the close, a notice names the daemon and the directory actually kept; it stays until you dismiss it with its **×**. See [Desktop App → Dashboard](desktop/dashboard.md#closing-agents-and-orchestrations).

A unit closed with `close` disappears from both clients, and a unit closed in a client is gone from `close --all`.

## When something goes wrong

Stopping an agent has a time limit. If its process does not confirm that it exited, the deck reports that the stop could not be confirmed instead of waiting indefinitely; the process may still be running. In the TUI and desktop app, the close error leaves the card available to inspect. With `close`, every pane whose stop is unconfirmed is listed as a survivor, the command exits `1`, and the deck does not report the unit closed or remove its worktree. Check the process and the daemon log, then retry the same close: the deck keeps the unconfirmed agent's cleanup record so the retry can finish the stop and its cleanup.

| Message | Cause | What to do |
|---|---|---|
| `the running daemon (…) is too old for close; nothing was closed` | The daemon was started by an older version of the deck. | Restart it onto this version with `dot-agent-deck daemon restart`, or close from the TUI or the desktop app. |
| `no daemon is running …; nothing was closed` | No daemon is running for this user. `close` never starts one. | Nothing is running, so there is nothing to close. |
| `close: …` naming `DOT_AGENT_DECK_PANE_ID`, `DOT_AGENT_DECK_AGENT_ID` or `DOT_AGENT_DECK_PANE_CAPABILITY` | Some of the variables the deck sets in its panes are present and some are missing or empty, so `close` cannot tell whether a person or an agent is asking. It refuses rather than guess. | Run it from a terminal outside the deck, where none of them is set, or from the agent's own pane with the environment the deck gave it. |
| `the running daemon (…) cannot keep a worktree on close; nothing was closed` | `--keep-worktree` was given, and the daemon was started by an older version of the deck. | Restart it onto this version with `dot-agent-deck daemon restart`, then run the command again. |
| `PARTIALLY: still running: …` | Some agents of the unit stopped and some did not, or their exits could not be confirmed. | Inspect the named survivors. Close any still listed with `--pane`, or from a client. |
| An agent's `close --all` lists nothing | That agent dispatched no units that are still running. | Close from a shell instead. |

## What the agent rules protect against

The agent rules stop an agent from closing the wrong thing by mistake: a unit another dispatcher started, its own team, a pane whose agent has been replaced, or a unit that has not finished. They are not a lock. Any program running as your user account can stop the deck's agents by other means, and the deck does not try to prevent that.

## See also

- [Dispatcher Mode](dispatcher-mode.md): start units and hear back from them
- [Orchestration](orchestration.md): the teams a unit can start as
- [Session Management](session-management.md): what each status means
