Repository navigation
Description
Item 8 of 9 in the Crews: organic teamwork milestone.
The problem
Every Crew seat on a new worktree leaves that worktree behind forever. Archiving a Crew says "Worktrees, branches, and pull requests remain. Cleanup is a separate action", and the separate action is me going through them by hand. Most of them are clean, with their work merged, so there's nothing to keep.
Where this sits
This is the "Workspace cleanup" row and the "Archive & cleanup" flow of Crews proposal v2 (25 October). When J5's archive runs, worktrees J5 created whose work is clean, committed, and merged are removed with no prompt. Anything with uncommitted or unmerged work is kept, and asking me about those is #749. Finishing an assignment never removes anything, because members stay available for follow-up work.
This changes a documented promise: crews.md AC19 and archive-flow.md AC9 say archiving never deletes a worktree.
What we already have
-
Seat workspaces are
shared(the Captain's checkout),worktree, andexisting_worktree(apps/server/src/j5/a2a/spawnWorkspace.ts:64-71). Aworktreeseat hands off to upstream's ThreadLaunch, which creates the worktree. -
Nothing records that J5 created a worktree. The only binding is upstream's thread
branchandworktreePath. -
J5's archive:
ArchiveCrewService.tsretires each seat throughArchiveAgentService.ts.- The Fleet page's Archive crew and
j5_archive_crew(mcp/tools.ts:797) both reach it. - The warning text is in
apps/web/src/j5/a2a/ArchiveWarningContent.tsx:213.
-
Upstream's archive (
thread.archive) only setsarchivedAt. -
Upstream's hourly StorageCleanup (
apps/server/src/storageCleanup.ts) already does most of the checking we need:- keeps shared worktrees (
:139,:622); - checks for local changes (
:496); - checks merges with
merge-base --is-ancestoragainst the fetched default branch (:703); - catches squash and rebase merges through a merged PR whose head SHA is this exact commit (
storageCleanupPullRequestMerged,:169); - re-reads before removing and skips live sessions (
:789); - keeps the branch and path (
:843).
It only considers active threads, so archived threads are never candidates.
- keeps shared worktrees (
-
When a thread's worktree path is missing, its next turn prunes and recreates the worktree from the recorded branch (
apps/server/src/orchestration-v2/ProviderTurnStartService.ts:475-480). On failure that code only logs a warning.
Proposal
- Record at spawn which worktrees J5 created, and for which thread.
- When J5's archive flow archives a thread, J5 checks each worktree it owns. If it's clean, merged, not used by any other unarchived thread, and has no live session, J5 removes the checkout and keeps the branch.
- The checks reuse upstream's StorageCleanup helpers where they're exported, rather than copying them.
Acceptance criteria
- When a spawn creates a new worktree, J5 records it in a J5-owned table: thread id, project, path, branch, and when it was created. Worktrees from before this ships have no record and are never removed automatically.
- Archiving through J5's archive flow (agent archive, Archive crew, and
j5_archive_crew) checks each J5-created worktree bound to an archived thread. - A worktree is removed only when all of these hold at the time of removal:
- no uncommitted or untracked changes;
- its HEAD is merged into the project's fetched default branch, or a merged PR's head SHA is that exact commit, so squash and rebase merges count;
- no unarchived thread is bound to the same path;
- no live session is using it.
- The Captain's checkout and worktrees chosen with Existing worktree are never removed, since J5 didn't create them.
- Removal runs
git worktree removeand keeps the branch. Recorded branch and path stay on the thread. - Removal runs after the archive commits and never blocks or fails the archive. A removal that fails leaves the worktree as it was and is reported once in the archive result.
- Worktrees that fail a check are left untouched. They're listed in the archive result with the reason (uncommitted changes, unmerged commits, shared, in use) for Crews: one Inbox approval for worktrees with unfinished work #749 to pick up.
- One archive call checks every affected worktree in one pass. Removed worktrees don't wait on anything else.
- Unarchiving a thread whose worktree was removed, or forking it, works: its next turn recreates the worktree from the kept branch through the existing repair. A test proves it.
- Before archiving, the warning lists which worktrees will be removed and which will be kept, replacing "Worktrees, branches, and pull requests remain".
- Upstream's sidebar archive is unchanged. If we want it to clean up too, that's a separate decision brought to Jackson with its fork cost.
- Focused tests cover:
- clean and merged (removed);
- uncommitted changes (kept);
- committed but unmerged (kept);
- a squash-merged PR (removed);
- shared with an unarchived thread (kept);
- a live session (kept);
- a worktree from before this ships (kept);
- a failed removal;
- unarchive after removal.
- crews.md AC19 and its definition line, and archive-flow.md (definition, AC9, AC13), are rewritten for the new promise. upstream.md gains an entry only if upstream behavior changes.
Out of scope
- Approving removal of worktrees with unfinished work. That's Crews: one Inbox approval for worktrees with unfinished work #749.
- Cleanup when an assignment finishes or a member goes idle.
- Deleting branches or closing PRs.
Open question
When the repair on unarchive can't recreate the worktree, upstream's code only logs a warning, so the thread looks fine until its turn fails. Is that acceptable for now, or do we want J5 to say so in the thread? Changing it would mean an upstream edit.
Activity
fix(j5): archiving a Crew keeps a worktree a seat was unarchived into (…
test(j5): provide TerminalManager where the Crew worktree check now r…
feat(j5): archiving a Crew removes its clean, merged worktrees (#756)