sandcastle: worker worktrees and branches are never cleaned up — sweep them post-run #187

Closed
opened 2026-07-30 16:00:40 +00:00 by ben · 2 comments
Owner

What happens

Sandcastle's branchStrategy: merge-to-head + copyToWorktree (.sandcastle/main.mts) creates a git worktree .sandcastle/worktrees/sandcastle-worker-<ts>-<id> and a branch sandcastle/worker/<ts>-<id> per iteration — and nothing ever removes them. By 2026-07-30 the swarm workdir on matou-workstation had accumulated 18 stale worktrees (2.9 GB, oldest 07-24) and 198 orphaned sandcastle/worker/* branches (every one already merged to main).

Why it matters

  • Disk leak on a dedicated droplet (each worktree is a full checkout, ~160 MB+).
  • go test ./... breaks in the workdir root: TestWireTags_AreSnakeCase (internal/wireconvention) walks the tree and skips only node_modules/vendor/.git — the stale checkouts contributed 1867 phantom camelCase findings. This poisoned the healer's own seam repro on 2026-07-30 (its repro-seam.log showed a wireconvention FAIL that CI never sees) and will mislead any human debugging in the workdir.

Manual cleanup already done (2026-07-30)

git worktree remove --force on all 18 + git worktree prune; all 198 branches deleted with merged-only -d (none refused — no committed work dangled). Uncommitted scraps archived to ~/swarm/worktree-prune-20260730.tar.gz on the workstation (superseded drafts only). wireconvention green in the workdir again. It will regrow from the next swarm run.

The ask

  1. Add a post-run sweep to run-swarm.sh (exit trap, after the harness returns): for every .sandcastle/worktrees/* not owned by the current run — the global /tmp/matou-swarm.lock flock means anything else is dead — git worktree remove --force it, git worktree prune, and delete merged sandcastle/worker/* branches (git branch -d, never -D: an unmerged branch is evidence of lost work and should be left + surfaced, not destroyed).
  2. Defense in depth (optional): teach TestWireTags_AreSnakeCase to skip .sandcastle/ the way it skips node_modules, so a future leak can't poison test runs or healer repros again.

Acceptance: after a swarm run completes on the workstation, .sandcastle/worktrees/ contains no dead worktrees, no merged sandcastle/worker/* branches remain, and nix develop -c go test ./internal/wireconvention/... passes in the workdir root.

## What happens Sandcastle's `branchStrategy: merge-to-head` + `copyToWorktree` (`.sandcastle/main.mts`) creates a git worktree `.sandcastle/worktrees/sandcastle-worker-<ts>-<id>` and a branch `sandcastle/worker/<ts>-<id>` per iteration — and nothing ever removes them. By 2026-07-30 the swarm workdir on matou-workstation had accumulated **18 stale worktrees (2.9 GB, oldest 07-24)** and **198 orphaned `sandcastle/worker/*` branches** (every one already merged to main). ## Why it matters - **Disk leak** on a dedicated droplet (each worktree is a full checkout, ~160 MB+). - **`go test ./...` breaks in the workdir root**: `TestWireTags_AreSnakeCase` (internal/wireconvention) walks the tree and skips only `node_modules`/`vendor`/`.git` — the stale checkouts contributed **1867 phantom camelCase findings**. This poisoned the healer's own seam repro on 2026-07-30 (its `repro-seam.log` showed a wireconvention FAIL that CI never sees) and will mislead any human debugging in the workdir. ## Manual cleanup already done (2026-07-30) `git worktree remove --force` on all 18 + `git worktree prune`; all 198 branches deleted with merged-only `-d` (none refused — no committed work dangled). Uncommitted scraps archived to `~/swarm/worktree-prune-20260730.tar.gz` on the workstation (superseded drafts only). `wireconvention` green in the workdir again. **It will regrow from the next swarm run.** ## The ask 1. Add a post-run sweep to `run-swarm.sh` (exit trap, after the harness returns): for every `.sandcastle/worktrees/*` not owned by the current run — the global `/tmp/matou-swarm.lock` flock means anything else is dead — `git worktree remove --force` it, `git worktree prune`, and delete merged `sandcastle/worker/*` branches (`git branch -d`, never `-D`: an unmerged branch is evidence of lost work and should be left + surfaced, not destroyed). 2. Defense in depth (optional): teach `TestWireTags_AreSnakeCase` to skip `.sandcastle/` the way it skips `node_modules`, so a future leak can't poison test runs or healer repros again. Acceptance: after a swarm run completes on the workstation, `.sandcastle/worktrees/` contains no dead worktrees, no merged `sandcastle/worker/*` branches remain, and `nix develop -c go test ./internal/wireconvention/...` passes in the workdir root.
Author
Owner

This was generated by AI during triage.

Triage result: bug + ready-for-agent

Verification: Confirmed against the current tree. .sandcastle/run-swarm.sh has no trap/cleanup logic of any kind, and no other script in .sandcastle/ removes worktrees or branches — the leak is real, not already handled. internal/wireconvention/convention_test.go's directory walk currently skips only node_modules, vendor, and .git — it does not skip .sandcastle/, confirming the phantom-finding mechanism described in the report. The worktrees directory and worker branches are currently clean (the reporter's manual sweep), so this is a regrowth-prevention fix, not an active-incident cleanup.

This issue already carries its own acceptance criteria and a precise, scoped ask from the maintainer (numbered items 1–2, plus the git branch -d-never--D safety constraint). No ambiguity requiring a human judgement call was found, so it's routed straight to ready-for-agent rather than ready-for-human.

Agent Brief

Category: bug
Summary: Sandcastle's per-iteration worker worktrees and branches are never cleaned up, leaking disk and poisoning wireconvention's repo-wide scan.

Current behavior:
Every Sandcastle iteration using branchStrategy: merge-to-head + copyToWorktree creates a git worktree under .sandcastle/worktrees/ and a sandcastle/worker/<ts>-<id> branch. Nothing removes either after the run. Over time this leaks disk (each worktree is a full checkout) and, because internal/wireconvention's snake-case-wire-tag test walks the whole tree skipping only node_modules/vendor/.git, stale worktree checkouts get scanned too and can produce large numbers of phantom findings — corrupting both CI-adjacent local runs and the healer's own repro logs.

Desired behavior:
After a swarm run completes (success or failure), a post-run sweep removes every worktree under .sandcastle/worktrees/ that isn't owned by the run currently holding the global swarm lock (/tmp/matou-swarm.lock), via git worktree remove --force followed by git worktree prune. It then deletes every sandcastle/worker/* branch that is fully merged, using git branch -d (never -D) — an unmerged branch indicates undelivered work and must be left in place and surfaced (e.g. logged), not destroyed. The sweep must run via an exit trap (or equivalent) so it fires whether the run's harness step succeeds or fails.

As defense in depth, internal/wireconvention's tree-walking test should skip .sandcastle/ the same way it already skips node_modules/vendor/.git, so a future leak (from this or any other tool) can't reintroduce phantom findings into that test.

Key interfaces:

  • The swarm run script's top-level control flow — needs an exit trap (or unconditional cleanup step) that runs after the harness invocation regardless of its exit status.
  • Whatever currently establishes "this run owns the lock" (the existing /tmp/matou-swarm.lock flock check) — the sweep's ownership test should reuse that same signal rather than inventing a new one.
  • internal/wireconvention's directory-walk skip list (currently node_modules, vendor, .git) — add .sandcastle (or the specific .sandcastle/worktrees path, whichever keeps the test's intent — skipping non-source trees — clearest).

Acceptance criteria:

  • After a swarm run completes on a machine where no other run holds the global lock, .sandcastle/worktrees/ contains no worktrees left over from that run.
  • After that run, no sandcastle/worker/* branch that was merged during the run remains.
  • A sandcastle/worker/* branch that is not fully merged is left untouched by the sweep (verify with a deliberately unmerged test branch) and its presence is surfaced (e.g. a log line), not silently dropped.
  • The sweep runs even when the harness step itself fails/exits non-zero (verify via a forced-failure run).
  • nix develop -c go test ./internal/wireconvention/... passes in a workdir root that has a stray .sandcastle/worktrees/* checkout present (simulating a not-yet-swept leftover).

Out of scope:

  • Changing branchStrategy/copyToWorktree semantics themselves.
  • Any change to how the global /tmp/matou-swarm.lock is acquired.
  • Retroactive cleanup of any machine's existing state (already done manually per the issue body).
> *This was generated by AI during triage.* **Triage result:** `bug` + `ready-for-agent` **Verification:** Confirmed against the current tree. `.sandcastle/run-swarm.sh` has no `trap`/cleanup logic of any kind, and no other script in `.sandcastle/` removes worktrees or branches — the leak is real, not already handled. `internal/wireconvention/convention_test.go`'s directory walk currently skips only `node_modules`, `vendor`, and `.git` — it does not skip `.sandcastle/`, confirming the phantom-finding mechanism described in the report. The worktrees directory and worker branches are currently clean (the reporter's manual sweep), so this is a regrowth-prevention fix, not an active-incident cleanup. This issue already carries its own acceptance criteria and a precise, scoped ask from the maintainer (numbered items 1–2, plus the `git branch -d`-never-`-D` safety constraint). No ambiguity requiring a human judgement call was found, so it's routed straight to `ready-for-agent` rather than `ready-for-human`. ## Agent Brief **Category:** bug **Summary:** Sandcastle's per-iteration worker worktrees and branches are never cleaned up, leaking disk and poisoning `wireconvention`'s repo-wide scan. **Current behavior:** Every Sandcastle iteration using `branchStrategy: merge-to-head` + `copyToWorktree` creates a git worktree under `.sandcastle/worktrees/` and a `sandcastle/worker/<ts>-<id>` branch. Nothing removes either after the run. Over time this leaks disk (each worktree is a full checkout) and, because `internal/wireconvention`'s snake-case-wire-tag test walks the whole tree skipping only `node_modules`/`vendor`/`.git`, stale worktree checkouts get scanned too and can produce large numbers of phantom findings — corrupting both CI-adjacent local runs and the healer's own repro logs. **Desired behavior:** After a swarm run completes (success or failure), a post-run sweep removes every worktree under `.sandcastle/worktrees/` that isn't owned by the run currently holding the global swarm lock (`/tmp/matou-swarm.lock`), via `git worktree remove --force` followed by `git worktree prune`. It then deletes every `sandcastle/worker/*` branch that is fully merged, using `git branch -d` (never `-D`) — an unmerged branch indicates undelivered work and must be left in place and surfaced (e.g. logged), not destroyed. The sweep must run via an exit trap (or equivalent) so it fires whether the run's harness step succeeds or fails. As defense in depth, `internal/wireconvention`'s tree-walking test should skip `.sandcastle/` the same way it already skips `node_modules`/`vendor`/`.git`, so a future leak (from this or any other tool) can't reintroduce phantom findings into that test. **Key interfaces:** - The swarm run script's top-level control flow — needs an exit trap (or unconditional cleanup step) that runs after the harness invocation regardless of its exit status. - Whatever currently establishes "this run owns the lock" (the existing `/tmp/matou-swarm.lock` flock check) — the sweep's ownership test should reuse that same signal rather than inventing a new one. - `internal/wireconvention`'s directory-walk skip list (currently `node_modules`, `vendor`, `.git`) — add `.sandcastle` (or the specific `.sandcastle/worktrees` path, whichever keeps the test's intent — skipping non-source trees — clearest). **Acceptance criteria:** - [ ] After a swarm run completes on a machine where no other run holds the global lock, `.sandcastle/worktrees/` contains no worktrees left over from that run. - [ ] After that run, no `sandcastle/worker/*` branch that was merged during the run remains. - [ ] A `sandcastle/worker/*` branch that is *not* fully merged is left untouched by the sweep (verify with a deliberately unmerged test branch) and its presence is surfaced (e.g. a log line), not silently dropped. - [ ] The sweep runs even when the harness step itself fails/exits non-zero (verify via a forced-failure run). - [ ] `nix develop -c go test ./internal/wireconvention/...` passes in a workdir root that has a stray `.sandcastle/worktrees/*` checkout present (simulating a not-yet-swept leftover). **Out of scope:** - Changing `branchStrategy`/`copyToWorktree` semantics themselves. - Any change to how the global `/tmp/matou-swarm.lock` is acquired. - Retroactive cleanup of any machine's existing state (already done manually per the issue body).
Author
Owner

Done — landed as a19b521 on main.

The ask:

  1. Post-run sweep added to run-swarm.sh via an EXIT trap (new .sandcastle/sweep-lib.sh). With the global /tmp/matou-swarm.lock held for the whole run, no other swarm is live at sweep time, so every leftover .sandcastle/worktrees/* is dead: git worktree remove --force (with an rm -rf fallback for locked/odd worktrees) → git worktree prune → git branch -d every merged sandcastle/worker/*. Unmerged branches are never -Dd — left intact and surfaced to Mattermost as possible lost work. Fires on every exit path (limit-pause, push-fail, normal).
  2. Defense in depth: TestWireTags_AreSnakeCase now skips .sandcastle/ the way it skips node_modules/vendor, so a future leak cannot poison test runs or healer repros again.

Evidence: offline test .sandcastle/tests/sweep-lib-test.sh (7 groups) proves merged worktree+branch removed, unmerged branch left + surfaced, non-worker branch untouched, idempotent re-run, and non-git no-op. bash -n clean on run-swarm.sh. (Go toolchain is unavailable in the swarm sandbox, so the wireconvention test change — a one-token .sandcastle case addition + comment — is verified by CI in the workdir; the acceptance nix develop -c go test ./internal/wireconvention/... runs there.)

Done — landed as `a19b521` on `main`. **The ask:** 1. Post-run sweep added to `run-swarm.sh` via an EXIT trap (new `.sandcastle/sweep-lib.sh`). With the global `/tmp/matou-swarm.lock` held for the whole run, no other swarm is live at sweep time, so every leftover `.sandcastle/worktrees/*` is dead: `git worktree remove --force` (with an `rm -rf` fallback for locked/odd worktrees) → `git worktree prune` → `git branch -d` every **merged** `sandcastle/worker/*`. Unmerged branches are **never** `-D`d — left intact and surfaced to Mattermost as possible lost work. Fires on every exit path (limit-pause, push-fail, normal). 2. Defense in depth: `TestWireTags_AreSnakeCase` now skips `.sandcastle/` the way it skips `node_modules`/`vendor`, so a future leak cannot poison test runs or healer repros again. **Evidence:** offline test `.sandcastle/tests/sweep-lib-test.sh` (7 groups) proves merged worktree+branch removed, unmerged branch left + surfaced, non-worker branch untouched, idempotent re-run, and non-git no-op. `bash -n` clean on `run-swarm.sh`. (Go toolchain is unavailable in the swarm sandbox, so the wireconvention test change — a one-token `.sandcastle` case addition + comment — is verified by CI in the workdir; the acceptance `nix develop -c go test ./internal/wireconvention/...` runs there.)
ben closed this issue 2026-07-30 16:08:47 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Matou/idss#187
No description provided.