Skip to content

fix(pm-dispatch): clean up task worktrees after session completion #12217

Closed

Description

@dezren39

Gap

PR #12214 moved each pm-dispatch launch into its own detached worktree under ~/git/fleet/dispatch-<repo>-<task>, fixing interference with the primary clone. It explicitly leaves cleanup as a follow-up. The dispatch worktree creator (pm-stack/dispatch/src/worktree.ts::createDispatchWorktree) creates the tree and returns its path, but the launch lifecycle has no corresponding cleanup after session completion. The generic infra/worktree-reaper cannot retire these trees: worktree_reaper.py::evaluate_tree rejects detached worktrees with reason detached-v1, and test_worktree_reaper.py asserts that detached trees are retained.

This means each dispatched run can leave another checkout and its ignored build output under the fleet worktree root. Repeated runs can accumulate disk use and stale directories. A scan during this review found no current ~/git/fleet/dispatch-* directories; this is a verified lifecycle gap, not a claim that accumulation has already caused an incident.

Expected behavior

Add a dispatch-specific lifecycle for retiring a task worktree once its session is known to have ended, without weakening the generic reaper's conservative detached-worktree policy.

Acceptance criteria:

  • Associate the created worktree with the dispatched task/session so cleanup targets only the exact tree created for that launch.
  • Clean up after confirmed session termination, including failure/early-exit paths where a session was created; do not remove a worktree while its session may still be running or resumable.
  • Fail closed and preserve the tree when session state is ambiguous, the tree is occupied, or it contains uncommitted/untracked work or commits not safely preserved. Do not silently discard user work.
  • Make cleanup idempotent and report cleanup failures without masking the original dispatch/session result.
  • Add tests for successful cleanup, active/ambiguous session retention, dirty/unpublished work retention, and cleanup failure behavior.

Related: merged PR #12214 (worktree isolation; explicitly defers cleanup), closed issue #12213 (primary-clone interference).

Activity

  1. added
    task/triageQueue packet kind: verify, reproduce, and route this item
    source/creatorFiled by the new-issue-creator job
    priority/normalOrdinary priority (the default)
    effort/sSmall: expected under 2 hours of work
    on Nov 21, 2026
  2. developing-today commented on Nov 21, 2026

    @developing-today
    Contributor
  3. dezren39 commented on Nov 21, 2026

    @dezren39
    MemberAuthor

    agent-note:v1
    task: 20903
    lane: oc-code-06
    state: picked_up
    by: repo-worker-code (OpenChamber scheduled lane)
    verified: issue #12217 OPEN, no hard-stop labels; PR #12214 is MERGED with cleanup explicitly deferred; related #12213 is CLOSED by the isolated-worktree change.
    action: triaging the remaining cleanup lifecycle gap against current dispatch and reaper behavior; implementation remains out of scope for this triage task.
    next: record verified findings and route a scoped implementation follow-up if the gap remains actionable.

  4. dezren39 commented on Nov 21, 2026

    @dezren39
    MemberAuthor

    Triage — confirmed and routed (2026-11-21, task 20903, lane oc-code-06)

    • Verdict: REAL AND CURRENT. fix(pm-dispatch): clean up task worktrees after session completion #12217 is OPEN. PR fix(pm-dispatch): run dispatched sessions in their own worktree, never the primary clone #12214 is MERGED and explicitly deferred cleanup; pm-dispatch sessions run in the primary clone and leave ~/git/code on their PR branches #12213 is CLOSED by the isolation change. Fresh PR searches found no open/superseding cleanup PR. No current accumulation incident is claimed.
    • Current main evidence: pm-stack/dispatch/src/worktree.ts deterministically names and creates one detached tree per task, but has no cleanup helper. pm-stack/dispatch/src/server.ts creates the tree before session creation and registers no cleanup after normal launch/session completion. pm-stack/dispatch/src/opencode.ts can read session state and distinguish 404 from upstream errors, but there is no completion lifecycle callback. The generic reaper intentionally retains detached-v1 trees (infra/worktree-reaper/bin/worktree_reaper.py); that policy must remain unchanged.
    • Safety disposition: cleanup must target only the exact tree associated with the task/session; require confirmed session termination and preserve active, resumable, ambiguous, occupied, dirty, untracked or unpublished work. Cleanup must be idempotent and must not mask the session outcome.
    • Route: enqueued bounded small-fix task 20904 (dedupe repo-health:v1:fix:developing-today/code:issue:12217) for implementation and lifecycle tests. No code changes were made in this triage task.
    • Verification: repo-queue validate passed for the follow-up packet; issue/PR state and current-main source were checked fresh.
  5. added and removed
    queue/claimedQueue worker lifecycle state
    work/assignedQueue worker lifecycle state
    on Nov 21, 2026
  6. 9 remaining items

  7. dezren39 commented on Nov 21, 2026

    @dezren39
    MemberAuthor

    agent-note:v1
    state: parked
    task: 20904
    lane: oc-code-01
    stage: reproduced

    Parked for 30 minutes with the task branch/worktree preserved at queue/20904-12217-dispatch-cleanup / /home/user/git/fleet/20904-12217-dispatch-cleanup. Reproduced the gap: dispatch creates the detached worktree but does not persist its exact association with the returned session ID; getSession only distinguishes an extant session from HTTP 404 and no terminal-state contract is documented in this service. We must not treat idle, missing/ambiguous, or transport failure as confirmed termination. Resume by finding an authoritative terminal signal and durable exact worktree↔session mapping, then implement idempotent fail-closed cleanup and lifecycle tests. #12217 remains OPEN; no hard-stop labels.

  8. added
    queue/claimedQueue worker lifecycle state
    and removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    on Nov 22, 2026
  9. dezren39 commented on Nov 22, 2026

    @dezren39
    MemberAuthor

    Picked up to shepherd PR #12219. Revalidating review findings and refreshing the branch against current main before deciding whether it is merge-ready.

  10. dezren39 commented on Nov 22, 2026

    @dezren39
    MemberAuthor

    agent-note:v1\nstate: done\ntask: 21063\nlane: code-f\nresult: PR #12219 merged as 7848651 after refreshing against current main; GitHub closed #12217 via the PR closing keyword. Review findings were verified/fixed and documented on the PR. Verification on the final branch: bun test --timeout 10000 (259 passed) and bunx tsc --noEmit passed. The only reported check was CodeRabbit skipping; no required checks were pending.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/sSmall: expected under 2 hours of workkind/enhancementpriority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobstatus/triagedTriaged and routed (see agent note)status:donetask/triageQueue packet kind: verify, reproduce, and route this item

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions