Sweep Claude Code Desktop sessions — classify which are finished, check their worktrees are safe to discard, write resume stubs, and archive the safe ones. Use for "archive finished sessions", "session cleanup", "how many sessions do I have open".
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Sessions?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/djnsty23-sessions)
---
name: sessions
description: Sweep Claude Code Desktop sessions — classify which are finished, check their worktrees are safe to discard, write resume stubs, and archive the safe ones. Use for "archive finished sessions", "session cleanup", "how many sessions do I have open".
when_to_use: "Invoked when the user says \"sessions\", \"archive sessions\", asks to clean up finished sessions, says \"fleet\", or asks which sessions are waiting or blocked."
allowed-tools: Bash, Read, Write, mcp__ccd_session_mgmt__archive_session, mcp__ccd_session_mgmt__send_message
model: haiku
user-invocable: true
---
# Sessions
Archive sessions whose work is done, without destroying work that isn't.
## Why this is not a one-liner
`archive_session` stops the session process **and cleans up its git worktree**.
So the risky part is never "which sessions look finished" — it is "which
worktrees are actually disposable". On the first real run, two sessions that
looked finished held live work: one had 2 uncommitted files, one had a branch
that existed nowhere but that disk. A naive sweep deletes both.
The sweep therefore fails closed. A worktree it cannot read is unsafe, not safe.
## Step 1 — classify (read-only, archives nothing)
```bash
node "${CLAUDE_PLUGIN_ROOT}/scripts/session-sweep.js"
```
Verdicts:
| verdict | meaning |
|---|---|
| `MERGED` | every PR settled (merged or closed) — finished |
| `DONE` | no PR of any state, hand-started, cold past `--done-minutes` (default 240) |
| `STALE` | no PR, and idle past its threshold |
| `PR-OPEN` | at least one PR still open — **not** finished |
| `ACTIVE` | recent activity — leave alone |
Three thresholds, because "finished" and "cold" are different questions:
- **`--stale-days` (default 14)** for hand-started work.
- **`--ephemeral-days` (default 2)** for sessions the app launched from a
schedule. Recurrence does not make their uncommitted work or ignored evidence
disposable; the same safety checks still apply. They dominated the population
on the machine this was built
for, 261 of ~480 records carried a `scheduledTaskId`.
- **`--merged-min-minutes` (default 30)** floors the MERGED verdict. A settled
PR bypasses the idle clock entirely, so without this a session whose PR merged
three minutes ago reads as finished while its author is still working in it —
measured on two real sessions. Finished is not the same as cold; it needs both.
The floor is in **minutes** because what it guards is a liveness ping.
`lastActivityAt` is refreshed by the app only while a session is actually
running, and it freezes the moment one stops — so a record hours stale is not
someone typing slowly, it is a session the app is no longer running. The floor
was 12 **hours** until 2026-08-22, which conflated the two: measured over 42
live records, four sessions with every PR settled and clean pushed worktrees
sat unarchivable at 1-9h idle, and not one record in the population fell
between 4h and 24h — so the extra eleven hours bought no discrimination, only
false ACTIVEs. `--merged-min-hours` is still accepted as the retired spelling.
- **`--done-minutes` (default 240)** is the same-day clock for PR-less work.
Before it, a triage or audit session with no PR waited 14 days to become
STALE, so the sidebar held every one of them. It defaults to the
live-transcript window because a DONE row still has to clear that guard.
PRs are matched to sessions by **branch** as well as by the app's binding. A
PR whose head is the session's branch but which the record does not carry is
listed under "PRs NOT BOUND", and it counts toward the verdict: an unbound open
PR makes the row PR-OPEN, never DONE. Trunk branches claim nothing.
Detection is structural (`scheduledTaskId`), never a title regex. A regex would
miss renamed tasks and catch hand-started work that happens to be called
"daily digest".
Use `--preserve-local` to archive exactly the local-only files named by the blocker in finished, clean linked worktrees, or files in untracked `leftover-dir(N files)` folders without `.git`, into verified `~/.claude/autodev/archives/worktrees/<basename>-<repo-hash>-<time>-<nonce>.tgz` archives before reporting SAFE, with leftovers skipping only node_modules, .venv and __pycache__.
`SESSION_SWEEP_ARCHIVE_DIR` overrides the archive directory, and any refused preservation, tracked checkout subdirectory, unreferenced detached commit or changed final safety check leaves the row blocked without removing source files.
In linked worktrees, only node_modules, .next, __pycache__ and .venv are regenerable directories at any depth, while other build-output names apply at the root. Windows refuses non-ASCII preserve paths before tar, and tar failures report fixed text with at most a numeric exit code.
Disposition is separate from verdict, and it is the one that decides:
- `SAFE` — finished, own repo, worktree clean, branch pushed. Archivable.
It also requires the record to sit in the live workspace, the only one
`archive_session` can resolve.
- `orphaned-ws`: clean, but in a workspace no account has used for two days.
`archive_session` answers "not found" for it. `--archive-orphaned` clears it.
- `other-ws`: clean, but in another account's warm workspace. Archive it from
that account, never from this one.
- `dirty(N)` / `unpushed(N)` / `branch-not-on-remote` / `stashed(N)` — finished
but holds work that exists nowhere else. **Never archive.** Report it and let
the user commit or push first.
- `unpushed-uncheckable(<git's error>)`: the unpushed check itself failed,
most often because the branch is on origin but this clone never fetched it.
Unknown is not safe: fetch, then sweep again.
- `local-only(N files: paths)`: ignored local files exist only in this worktree. Preserve and verify them before archiving.
- `leftover-dir(N files)`: an untracked folder without `.git` holds local residue. Preserve its files before clearing the blocker.
- `leftover-dir-unreadable`: leftover files cannot be inventoried. Keep the folder until it can be read.
- `tracked-subdirectory`: the target contains tracked checkout files. Keep it and correct the worktree target.
- `main-checkout`: the target is a main checkout. Never remove it through session cleanup.
- `orphan-commits(N)`: commits reachable from the current HEAD, including a detached HEAD, or held only by `refs/worktree/*`, `refs/bisect/*` or `refs/rewritten/*` are not proven retained elsewhere. A commit held only by ORIG_HEAD or the HEAD reflog is not detected. Retain local commits before archiving.
- `commit-uncheckable`: commit reachability cannot be verified. Keep the worktree until the check succeeds.
- `submodules(N)`: initialized submodules or a private modules directory hold data the parent cannot inventory. Keep the worktree with or without preservation.
- `submodules-uncheckable`: submodule state cannot be verified. Keep the worktree until it can be checked.
- `submodule-dir-not-empty(N)`: uninitialized gitlink paths hold files or directories, including manual clones. Keep the worktree with or without preservation.
- `hidden-index-changes(N)`: skip-worktree or assume-unchanged files differ from their index entries. Retain those changes before archiving.
- `hidden-index-uncheckable`: flagged index entries cannot be compared safely. Keep the worktree until they can be checked.
- `preserve-refused(reason)`: preservation failed a safety or archive check. The original blocker remains and source files stay in place.
- `third-party` — remote is not the operator's own account. Excluded entirely;
client work is not swept by a tool.
BLOCKED and EXCLUDED are reported separately, and the split matters. BLOCKED
means work exists in exactly one place — act on it. EXCLUDED (third-party or
`autoArchiveExempt`) is permanent and identical every run, so it is counted and
not listed; `--list-excluded` names them. They shared one list originally, and
five permanent rows appeared under BLOCKED every single run, which teaches a
reader to skip the one section where a real warning can appear.
- `exempt` — the session carries the app's own `autoArchiveExempt` flag.
## Step 2 — resume stubs before archiving, not after
```bash
node "${CLAUDE_PLUGIN_ROOT}/scripts/session-sweep.js" --write-resume
```
Writes one `.claude/handoffs/resume-<slug>.md` per SAFE row: session id, branch,
worktree, PR links, verdict, and how to pick the work back up.
Prefer restarting from the repo over reopening a long transcript. The transcript
is intact either way, but a deep one re-bills its whole context on every turn and
carries its own accumulated wrong turns; `RESUME.md` and `DECISIONS.md` carry the
conclusions without the cost.
## The reach limit and orphaned record updates
`archive_session` reaches only the workspace directory the app currently tracks
— measured at ~70 of 482 records, and `limit` does not change it. The cause is
structural, not a cap: sessions live under `<store>/<workspace>/`, the app holds
one workspace, and everything in the others returns "not found".
Age does NOT predict this. Four-day-old records were unreachable while older ones
were not. The directory decides.
```bash
node "${CLAUDE_PLUGIN_ROOT}/scripts/session-sweep.js" --archive-orphaned
```
This mode marks `orphaned-ws`
records archived by string-replacing `"isArchived":false` in the store JSON, and
**only** for orphaned workspaces. It still never touches a git worktree.
Why it is safe there and nowhere else: the app never loaded those records, so it
holds nothing to overwrite them with. Records in the live workspace are skipped
and still require `archive_session`, and `other-ws` records are skipped too. If
two workspaces are both recently active,
the script treats neither as orphaned — that is a shape it does not model, and
the safe reading is to leave both alone.
A string replace, not parse-then-stringify: reserializing rewrites field order
and escaping across a file the app owns, so any breakage would be
indistinguishable from the change being made. Take a copy of the store first;
it is around 50MB.
## Step 3 — archive, only the SAFE list
Use the current host's actual archive API and semantics. For Claude Desktop's
`archive_session`, re-read the SAFE row immediately before acting; retain needed
ignored reports/evidence and the resume stub outside any worktree the API removes.
A closed PR can mean rejected work, so record that disposition rather than calling
it delivered. Never archive a row the script did not mark SAFE, or self unless asked.
Show the concrete list and count. An explicit request to archive the safe,
finished set authorizes that scoped action; ask only if the proposed set or
destructive consequences exceed that request. Preserve ongoing work.
## Unbound open PRs: fix the binding in the owning session
The app archives a session when its PR closes, but only for a PR it bound.
`bind_pr` binds the CALLING session, so a sweep cannot bind for another one.
For each unbound OPEN row, send the owning session one line with
`send_message`: "Bind your PR: call bind_pr with <url>." Skip a row marked
`already bound to "<title>"`: another session holds that binding, and its close
already archives that session. An unbound MERGED or
CLOSED PR needs no binding; its row already reads MERGED and follows Step 3.
## Settle this session (self)
A session whose work is delivered should leave the sidebar itself rather than
wait for a sweep:
```bash
node "${CLAUDE_PLUGIN_ROOT}/scripts/session-sweep.js" --self
```
It prints JSON for the record whose worktree or cwd is the current directory.
Call `archive_session` with `self` only when `"settle": true`. Otherwise report
the `blockers` by name (`dirty(N)`, `unpushed(N)`, `pr-unsettled(#N)`,
`no-session-for-cwd`, `ambiguous(...)`) and stop. The idle clocks and the
live-transcript guard do not apply, since the fresh transcript is the caller's
own; every other guard does. Archiving ends the conversation, so give the final
report first. Offer it as the panel's tail option, never as the recommendation,
per `rule-options-protocol`.
## Do NOT then start a session per archived item
The reflex is to pre-warm replacements so they "share cached context". Measured
over 158 transcripts, that is backwards:
- A fresh session's first request reads ~34k tokens from the shared prefix and
**writes ~95k** of its own. Only ~26% of the opening prompt is shared, and that
share is a fixed ceiling — it does not grow by starting more sessions.
- The expensive part is the per-session cache, which nothing shares: the median
request re-reads **333k** tokens, 10x the shared portion.
So N pre-warmed sessions is N independent caches, each re-read on every turn. A
resume doc pasted into a prompt makes it worse, because it lands in the *unique*
part of the prefix and is re-billed every turn. Put context in the repo, where it
is read on demand, and start a session when there is work for it.
## Fleet
Load [references/fleet.md](references/fleet.md) when asked what other sessions are doing, which are waiting on an unanswered question, or to open the fleet board.
## Config
`~/.claude/session-sweep-denylist.txt` — one substring per line, matched against
the git remote and origin path. Third-party repos go here. Local only; never
committed, which is why no client name appears in this plugin.
`SESSION_SWEEP_OWNER` — the operator's GitHub account. Any `github.com` remote
not containing it is treated as third-party.