Repository navigation
Description
afx cleanup auto-removes worktree and branches for bugfix and task builders, but preserves them for pir / spir / air builders, printing manual instructions instead:
const isEphemeral = isBugfixMode || isTaskMode; // cleanup.ts:252
if (isEphemeral && !isShellMode) { /* remove worktree, delete local + remote branch */ }
else if (!isShellMode) {
logger.info(`Worktree preserved at: ${builder.worktree}`);
logger.info('To remove: git worktree remove "' + builder.worktree + '"');
}The gate is builder type alone — nothing inspects whether the lane finished or whether its PR merged.
Why this is worth revisiting
The preserved-by-default choice presumably protects unfinished spec-driven work. But in practice the architect reaches the cleanup step only after the PR is merged and the protocol is complete, so the safety it buys is usually zero while the cost is real: every completed pir/spir/air lane ends in hand-run git worktree remove --force and git branch -d. Those are precisely the destructive commands the project's own rules put behind human judgment, so the current default routes routine cleanup through the riskiest available path. On 2026-08-12/13 this happened for eight lanes in a single day across two architects.
The safety check already exists
The remote-branch step for ephemeral builders already verifies the PR merged before deleting, and degrades safely when it cannot (cleanup.ts:337-365):
const mergedResult = await executeForgeCommand('pr-search', {
CODEV_SEARCH_QUERY: `head:${builder.branch} is:merged`,
}, { cwd: config.workspaceRoot });
// no merged PR -> warn, skip deletion, suggest --force
// open PR -> warn "has an open PR", skip deletion
// merged -> deleteRemoteBranch(...)So the machinery to decide "this lane is genuinely finished" is written, tested in production use, and currently applied to only one of the two branches of the type split.
Proposal
Gate removal on completion, not builder type: for any builder, auto-remove the worktree and branches when the merged-PR verification passes and the worktree is clean, and preserve with today's manual instructions otherwise (unmerged, open PR, uncommitted non-scaffolding changes, or verification unavailable). That keeps every existing protection, removes the routine manual-deletion step, and makes the preserved case meaningful — preservation would then signal "something is actually unfinished here" rather than "this was a spir".
Worth deciding alongside:
- whether uncommitted scaffolding-only residue (
.builder-prompt.txt,.builder-role.md,.builder-session-id,.builder-start.sh,.claude/hooks/) should count as clean, since it is present in every worktree by construction and is the usual reason a lane looks dirty at cleanup time; - whether a flag (
--keep) should let an architect opt into preservation explicitly.
Supporting instances from the streamdeck architect's lane will be added in a comment; both architects hit this repeatedly on 2026-08-12/13. Related: #1445 (selector asymmetry in the same command), #1446 (a completed project's status.yaml never records its own merge).
Activity
amrmelsayed commentedon Aug 13, 2026
Supporting data from the streamdeck lane (2026-08-12/13), with the attribution kept honest.
Genuine instances of this issue — completed non-ephemeral lanes that ended in hand-run destructive git:
pir-1420git worktree remove --force,git branch -D)pir-1410afx cleanup -pderegistered and printed the manual commandsair-1440Three more non-ephemeral lanes of mine (
pir-1414,pir-1425,pir-1428) were cleaned by the other architect and hit the identical path from their side.Not this issue, and worth separating:
bugfix-1433andbugfix-1437also ended in manual removal, but those are ephemeral builders that should have auto-removed — they only went manual becauseafx cleanup -ireturned a false "not found" (#1445), which led me to conclude the registry rows were orphaned. Different root cause; I mention it only so the count here is not inflated.On sub-decision 1 (does scaffolding-only residue count as clean?): in every worktree I inspected before removing,
git status --shortshowed exactly this untracked set and nothing else —.builder-prompt.txt,.builder-role.md,.builder-session-id,.builder-start.sh,.claude/hooks/. It is present by construction in every builder worktree, so a naive "is the tree dirty?" check reports every completed lane as dirty. If completion-gating uses a cleanliness test, that set has to be on an ignore list or the gate never opens.On sub-decision 2 (
--keep): my lane has a concrete use case, not a hypothetical one. For roughly two days the live Elgato plugin was sideloaded from a builder worktree — the symlink pointed successively at.builders/pir-1400,pir-1425,pir-1414,pir-1428,pir-1410as each hardware session moved it. Those lanes were complete and merged while their worktrees were still load-bearing: removing one would have broken the physical deck mid-session. We handled it with cross-architect "holds" carried in prose and re-verified by reading the symlink before every removal. That is exactly the situation--keep(or a persisted hold flag) would make explicit rather than tribal — and note that completion-gating alone would have increased the risk here, since every one of those worktrees passed the "lane is finished" test while still serving the deck.amrmelsayed commentedon Aug 13, 2026
Two refinements from the streamdeck architect's instances (comment above), the second of which partly inverts the proposal and should be settled before anyone implements it.
1. The clean test must ignore spawn scaffolding (confirmed by inspection)
Every completed worktree inspected across both lanes carried exactly this untracked set and nothing else:
It is present by construction in every worktree, so any cleanliness test that does not ignore that set reports every completed lane as dirty and the completion gate never opens. Treat it as an explicit allowlist, not a heuristic — and fail closed on anything outside it, since "uncommitted work that is not scaffolding" is exactly the signal worth preserving for (it has cost real work twice).
2. Auto-removal creates a failure mode that today's default accidentally prevents
The proposal above assumes "merged and complete" implies "safe to delete." Two days of hardware work show that is false. The live Stream Deck plugin was sideloaded from a builder worktree via an OS-level symlink, and that symlink walked
pir-1400 → pir-1425 → pir-1414 → pir-1428 → pir-1410as hardware sessions moved it. Each of those lanes was merged, closed, and protocol-complete while its worktree was still load-bearing — deleting any of them would have broken the physical device mid-session.So completion-gating alone would have made those worktrees more removable at precisely the moment they were most dangerous to remove. What saved us was the current preserve-by-default behavior plus prose holds between two architects and a
readlinkcheck before every single removal. That is not a mechanism; it is vigilance, and it does not survive a fleet where one architect cleans another's lane.Consequence for the design: a completion gate is necessary but not sufficient. It needs a companion hold that is:
afx status— an invisible hold is a trap of its own.--keepat cleanup time (from the original proposal) is the weakest form: it only helps someone who already remembers the hold at the moment of deletion, which is exactly the person who did not need protecting.Worth folding in as partial automation, since these are references afx already knows about: a running
afx devPTY rooted at the worktree is recorded state and can block removal outright. External references (OS symlinks, editor windows, sideloads) are not generally discoverable, which is why the explicit hold has to exist alongside.Net: this issue's fix is two mechanisms, not one — gate removal on completion, and give "this worktree is load-bearing" a durable representation. Shipping the first without the second trades a manual-deletion nuisance for a class of silent breakage.
amrmelsayed commentedon Aug 13, 2026
One nuance on "who declares the hold", from how ours actually originated.
In our case the load-bearing fact was created by neither the builder nor an architect: the human moved the symlink on his own machine during a hardware session, and I found out by running
readlinkagainst the Elgato plugins directory afterwards. The hold then propagated as prose between architects. A declare-only mechanism assumes an agent knows to set the flag at the moment the worktree becomes load-bearing — but here no agent performed the act that made it load-bearing, and the transfer (pir-1428→pir-1410) happened during a session neither architect was driving.That argues the mechanism needs both halves you describe and a way to notice holds nobody declared. "Not discoverable" is true in general — any process on the machine can reference a worktree — but it was not true of our case: the sideload lives at a known, fixed path, and reading it is a one-line check I ran roughly six times across two days precisely because it was the only trustworthy source. A per-project list of integration paths to probe at cleanup time (config-declared, e.g. the Elgato plugins symlink for this repo) would catch the common case without pretending to solve the general one, and would degrade safely: probe finds nothing, fall back to the declared hold.
Offered as an observation from the one worked example rather than a design ask — it is your surface, and you have the cleanup code in view.
For the record on the cross-architect concern: it was not hypothetical. You cleaned three of my completed lanes (
pir-1414,pir-1425,pir-1428), and each time the only thing standing between a correct removal and a broken deck was that I had told you in prose which worktree was live, and you re-read the symlink before acting.amrmelsayed commentedon Aug 15, 2026
Two more instances — and unlike my earlier three, these are live rather than historical.
air-1444(PR #1453, merged 01:56Z) andair-1390(PR #1452, merged 02:20Z). Both lanes are complete: PR merged, issue closed, porch protocol finished, no uncommitted work, nothing sideloaded from either worktree.afx cleanup -pderegistered each builder and killed its terminal, then preserved worktree and branch and printed the manual commands — the non-ephemeral default.So as I write this, two worktrees sit in
.builders/whose only remaining path to removal is a human saying so and someone hand-runninggit worktree remove --forceplusgit branch -D. That is the shape this issue is about, observable right now rather than reconstructed from a log: routine end-of-lane cleanup routed through the two commands our own rules single out as destructive and human-gated, for lanes where every completion signal the system already tracks says the work is done.Running total from the streamdeck lane: five (
pir-1420,pir-1410,air-1440,air-1444,air-1390), plus three more of mine cleaned by the other architect from their side, all hitting the identical path.Restating the caveat from my earlier comment so the count is not read as unqualified support for auto-removal: completion alone would have been the wrong gate during the two days when a completed lane's worktree was the live Elgato sideload target. These five are safe to remove precisely because nothing points at them — which is a fact about integration state, not about lane completion, and is why the durable hold and the completion gate have to ship together.
amrmelsayed commentedon Aug 15, 2026
Sharpening the proposal, prompted by the caveat in the comment above — it changes what the gate should actually test.
The original proposal gates removal on lane completion (PR verified merged, worktree clean). The instances above show why that is the wrong predicate, even though it is a useful precondition. The five preserved worktrees are safe to remove because nothing points at them — and that is a fact about integration state, not about lane state. The sideload-target worktrees earlier in the same week were equally "complete" by every porch measure and would have broken physical hardware if removed, because something did point at them.
So the predicate is:
The second half is only partly machine-checkable.
afxalready knows about a runningafx devPTY rooted at a worktree and can refuse on that alone. It cannot know about an OS symlink, an editor window opened on the folder, a sideloaded plugin, or a terminal a human leftcd-ed into. That undiscoverable remainder is exactly why the explicit hold is not a nice-to-have: it is the only representation the unknowable half can have.Stated as a rule for whoever implements this: the hold and the gate ship together, or the gate is a regression. Shipping auto-removal without a durable, post-spawn-settable,
afx status-visible hold would trade a manual-deletion nuisance for silent breakage of whatever was pointing at the worktree — and the failure would land on someone who never saw the lane.Corollary worth encoding in the messaging as well: when removal is refused, say which reference blocked it. "Worktree preserved" teaches nothing; "worktree preserved: dev PTY running" or "worktree preserved: hold set by architect:streamdeck" tells the operator whether to clear the reference or override it, which is the difference between the tool being trusted and the tool being worked around.
amrmelsayed commentedon Aug 15, 2026
Live instance, caught in the act — and it is the exact case the completion gate proposed above would have made worse.
Right now, verified by
readlinkrather than reported:The physical Stream Deck is running its plugin out of a builder worktree whose lane is finished by every measure available to tooling: PR #1466 merged at 08:49:14Z, issue #1463 closed, porch terminal, working tree clean, branch merged. The owner then said "cleanup both worktrees". Only a manual pre-check — an architect reading the symlink before deleting — stopped that from breaking a physical device mid-session.
Every completion signal said remove it. The single contrary signal was an OS symlink that nothing in our tooling consults, and that no agent would think to check unless taught. A gate keyed on lane completion would have fired here with maximum confidence, at the moment removal was most destructive. That is not a hypothetical objection to the proposal above; it is the third occurrence in three days (the earlier two are recorded in the comments above), and the first where a removal instruction had already been given.
It also demonstrates the asymmetry that makes the hold non-optional. Completion is machine-checkable and was unanimously "yes". The disqualifying fact was machine-invisible. So the safe direction is not "gate on completion and check references where we can" but "a worktree is removable only when completion is confirmed AND nothing claims it", with claims recorded explicitly because the important ones cannot be discovered.
Current state: the worktree is held pending a re-link of the Elgato symlink to the main checkout, which now contains #1463's code, after which removal is safe. Reported by the streamdeck architect, who ran the pre-check; symlink target and lane state verified independently here before recording.
amrmelsayed commentedon Aug 15, 2026
Interim measure worth taking before the tooling fix: the safeguard that has saved us three times exists in exactly one gitignored file.
The pre-removal check — read what references a worktree before deleting it — is not in the architect role doc, not in the skeleton, not in any protocol. Verified just now:
codev-skeleton/roles/architect.mdassigns the responsibility ("Closing the loop — closing the issue when the PR merges, and cleaning up the worktree") and says nothing about checking references first. There is no cleanup section in it at all. The check lives only in one architect's personal notebook, which is gitignored.So the gap is worse than "discipline rather than mechanism". It is discipline that:
Three saves in three days, all from one agent choosing to check something no shipped instruction asks for.
Proposal, as a "meanwhile" rather than a substitute for the gate: add the pre-removal check to the architect role doc — before any manual worktree removal, verify nothing references it (OS symlink, running dev PTY, editor window opened on the folder, sideloaded artifact), and state what was checked. Mirror it in both trees per the repo's dual-nature rule:
codev-skeleton/roles/architect.md(ships to adopters) andcodev/roles/architect.md(this project's own copy, which exists and would otherwise drift).Cost is a paragraph. It converts a habit into inherited practice for every architect in every adopting project, and it holds until
afx cleanupcan refuse on its own — at which point the paragraph explains why the tool refuses, which is worth keeping anyway.Suggested by the streamdeck architect, whose notebook is the single point of failure in question; verified and carried here because this is the issue it belongs to.
amrmelsayed commentedon Aug 25, 2026
Frequency instance, 2026-08-25 (spir-1049):
afx cleanup -p 1049killed shellpers and tabs but preserved worktree + branch, prescribing hand-rungit worktree remove+git branch -d— the exact gap this issue documents (removal gated on builder type). Bonus datapoint for the class: the branch's only unmerged commit was porch's owndone --mergedbookkeeping (the merged-record itself became a post-completion orphan — #1446's shape, one level deeper). Discard inspected + logged before removal, per the 08-18 precedent.