Skip to content

Deepen failed Worktree acquisition recovery with verified retry authority #530

Closed

Description

@JacobStephens2

What to build

Deepen failed Worktree acquisition recovery so it retains work and changed resources that appear while Git retries checkout or local Issue branch removal. A failed fresh Run, either Continuation, or detached Architecture review must remove only positively identified, unchanged, clean partial effects before returning its original acquisition failure.

One standalone Ticket covers the private recovery operation, its use of existing checked Git execution, behavior regressions and glossary clarification; the change fits one fresh session.

Acceptance criteria

  • One private recovery operation owns checkout eligibility, work inspection, destructive attempt guards and the verified outcome that permits local Issue branch recovery. All four acquisition paths use it through their existing interface.
  • Every recovery checkout-removal and ref-deletion attempt, including retries, freshly verifies its stage authority before execution and checks the observed outcome afterward using existing transition-aware Git execution.
  • Eligible checkout recovery pins the checkout, administrative directory and common repository instances. Same-path, same-branch, same-head replacements, switched or locked registrations, symlinked links, inconsistent evidence and failed inspection retain remaining resources.
  • Checkout removal remains non-force and requires the sampled acquisition head plus clean index and working tree on every attempt: tracked and staged edits, untracked and ignored work, initialized submodules, assume-unchanged or skip-worktree flags and uncertain observations prevent removal. Work arriving during a retry survives.
  • Checkout removal advances only after the original checkout and administrative paths and their registration are absent in the unchanged captured repository. A failed attempt with partial effects, a false-success result, or a recreated unregistered target stops later mutation and reports retention.
  • A local Issue branch is eligible only when it was absent before the add, is still at the sampled acquisition head, and no registered checkout uses it. These checks and the verified checkout outcome repeat for every ref-deletion attempt; keep expected-old-value deletion. A same-head branch newly attached elsewhere survives.
  • Preserve early-add refusal behavior: a pre-existing occupied unregistered target and its files remain untouched while a newly created unused branch may be recovered. A pre-existing equal Continuation branch keeps its head and reflog. A partial registration at a pre-existing target remains ineligible for removal.
  • Initially absent targets that acquire an unregistered path are retained. A retained or uncertain checkout blocks branch mutation; a branch already absent requires no deletion.
  • Successful removal of a new branch is confirmed by ref absence and the remaining stage predicates. Failed ref attempts may retry only while the expected unused ref and checkout outcome still hold; partial or uncertain effects retain remaining resources.
  • Recovery retains completion execution, recorded interruption, Worktree-then-refs lock order, panic-safe fallback and original acquisition error. Diagnostics name every remaining or uncertain checkout/ref, its expected identity, known head or uncertainty, and the recovery cause.
  • Failed review-publication recovery removes only its already identified attempt-owned marker artifacts before cleanliness inspection. Preserve ADR 0014's successful-acquisition eligibility and evidence format; failed recovery grants no stale-disposal authority.
  • Interface regressions cover later ignored/hidden tracked work, replacement checkout or administration, same-head attachment during ref retry, false-success and partial effects, changed repository or registration evidence, and unchanged-authority positive retry controls. Cover all four acquisition kinds for shared checkout rules and both newly created Issue branch kinds for ref rules.
  • Keep existing occupied-target, pre-existing branch/reflog, dirty/index/submodule, publication, interruption, serialization and successful-acquisition behavior coverage. Formatting, all-target clippy and the full test suite pass.
  • Sharpen the existing Worktree glossary definition: failed-acquisition recovery rechecks clean-only, instance-specific authority around every destructive attempt, advances only on verified transitions, and retains remaining or uncertain partial effects. This documentation lands with the Ticket; no new ADR or domain term is needed.

Blocked by

None (can start immediately).

Decisions

The design tree is settled in four rounds. Each round resolves the frontier enabled by its prerequisites; every branch is settled.

Round 1 — responsibility and constraints

  1. Deepen the existing Worktree acquisition module with one private recovery operation. Its interface remains the current acquisition interface used by fresh Runs, both Continuation branch states and Architecture reviews. Recovery owns eligibility, per-attempt authority and transition outcomes together. Deleting this operation would redistribute those rules across checkout inspection, removal, branch recovery and Git retries; this is added depth, with locality and leverage across four paths.
  2. Keep clean-only failed-acquisition recovery distinct from successful-acquisition disposal. A failed add has partial ownership evidence and must preserve work, pre-existing targets and pre-existing branches. Successful lifetime cleanup may force-remove owned scratch and advanced heads. Reuse ownership inspection primitives, but never invoke that force-disposal policy for a failed add. ADR 0014 remains intact.
  3. Limit the change to recovery after a failed add, capture or review-record publication. Keep acquisition selection, ordinary add retry behavior, origin sampling, successful lifetime disposal, Failed run preservation, confirmed Self-merge origin deletion, branch configuration and Claim behavior as they are. These have different authority or are already deep modules; widening the change would dilute this verifiable recovery protocol.
  4. Use the existing Git adapter and its transition-aware execution. Reuse retry classification, wait budget, interruption policy and error formatting, with distinct before/after guards. Real Git and the existing isolated Git fault adapter already vary at the subprocess seam; another trait, adapter or retry loop would add no leverage.

Round 2 — ownership and stage shape, depending on 1–4

  1. Pin the common repository instance during pre-add inspection, before mutation is armed. Retain the sampled start, target/registration pre-existence and branch pre-existence/head facts. Verify the captured common repository and Launch directory relationship before recovery observations and around each destructive attempt. A canonical pathname alone cannot identify a replaced repository.
  2. Pin an eligible failed checkout once, then retain that same identity. If checkout capture succeeded before a later publication failure, recovery reuses that capture. Otherwise capture the matching partial checkout at recovery entry, after confirming that its path and registration were absent before this attempt and its head/attachment match the sampled start. Reuse the existing root, administration, common-repository and link checks, comparing against the pre-add repository capture. Never recapture a replacement to make a later retry eligible; uncertain or incomplete capture means retention.
  3. Replace the checkout-safe boolean with a verified stage outcome. Distinguish a positively observed no-checkout effect, successful verified removal and retained/uncertain checkout. The no-effect case preserves an untouched pre-existing unregistered target; if the target was initially absent, it requires path absence. A partial registration at a pre-existing target is retained. Any later branch attempt must revalidate the relevant outcome, so a vanished registration alone cannot make a remaining new path disposable.
  4. Keep review marker recovery before cleanliness inspection, once per recovery. Continue removing only markers already pinned by this attempt's publication owner. An artifact ownership/removal failure stops checkout and branch mutation. Recovery neither publishes successful-review evidence nor borrows authority from durable stale-scratch records, because a failed acquisition must remain ineligible for stale disposal.

Round 3 — checked transitions, depending on 5–8

  1. Before every checkout-removal attempt, repeat complete ownership and clean-work inspection. Require unchanged pinned directory instances and links, complete matching registration information, the sampled head and expected branch/detached state. Inspect index flags, status with filesystem monitoring disabled and ignored/untracked/submodule work included, and initialized submodules. Recheck identity after those observations so changing a path during inspection cannot authorize mutation. Removal remains non-force because only clean partial effects qualify.
  2. Check the checkout transition's actual outcome before permitting a retry or ref recovery. Success requires both original checkout and administration paths and the matching registration absent, with common repository identity unchanged. Failure can retry only if the same eligible, clean checkout remains positively verified. A partial removal, replacement, unreadable result or success without the expected absence stops recovery; preserve the attempt failure alongside the authority diagnostic. Do not reinterpret a failed command as success just because something vanished.
  3. Before every ref-deletion attempt, revalidate provenance, checkout outcome, registration completeness and the expected head. Only a branch absent before this add qualifies. No current registration may use it, including another checkout attached at the same head. Keep Git's conditional expected-head deletion, because complete registration checks alone do not protect against a concurrent head movement. Recheck absence of removed paths for the removal case; recheck the original no-effect predicates for early add refusal.
  4. Verify ref outcomes with stage-specific predicates. Success requires ref absence and unchanged repository/checkout-outcome/registration predicates. Failure can retry only while the expected head is still unused and those predicates hold. An already absent branch needs no destructive command. Changed or uncertain state stops mutation and retains whatever remains; never delete a pre-existing Continuation branch or branch configuration.
  5. Use the existing finishing lifetime for errors and unwinding. Recovery runs while the caller still holds the Worktree and refs locks, through completion execution even after SIGINT, SIGTERM or SIGHUP. It never clears recorded interruption, reacquires held locks, fetches, commits, pushes or starts a session. Explicit failure returns the original acquisition cause plus retained-resource diagnostics; the armed owner's panic-safe fallback applies the same conservative recovery without a second panic. Advisory locks coordinate factory operations; fresh checks do not claim an atomic guarantee against arbitrary outside writers between inspection and a Git mutation.

Round 4 — verification, documentation and size, depending on 9–13

  1. The interface is the test surface. Use the existing acquisition constructors with real temporary repositories, bare origins, hooks and the isolated Git fault adapter. Inject mutations at a known attempt rather than depending on sleeps. Assert surviving work, paths, registrations, refs/reflogs and original-cause diagnostics; do not expose or assert private recovery stage names.
  2. Pair refusal regressions with positive controls and preserve distinct behavior coverage. Exercise ignored and hidden tracked work arriving during removal retry across all four acquisition kinds; exercise same-head attachment for fresh and absent-branch Continuations. Also verify instance replacement, false-success/partial effects and changed repository evidence, plus unchanged-authority retry completion. Keep existing signal, publication, pre-existing-resource and successful-acquisition tests; replace overlapping procedural assertions instead of building a second test surface.
  3. Sharpen Worktree in the Ticket, without a new ADR. This is a stronger implementation guarantee for an existing domain concept, follows ADRs 0002 and 0014, and is reversible. Its glossary change belongs in the implementation pull request, not this Architecture review.
  4. Publish one standalone Ticket with no blockers and only needs-triage. Checkout and ref recovery are one protocol and can be changed and verified in one fresh session; separating them or their tests would not deliver independent behavior. thirdshift marks the plan ready after this review ends.

Evidence used to settle the design

At reviewed main head 1cf8fa9a25c4cc04eb665d2a7cb0a4674bc7b938, a throwaway test crossed the existing Worktree acquisition interface with the isolated Git fault adapter. The first checkout-removal attempt inserted ignored work or tracked edits hidden with assume-unchanged, then returned a retryable lock error; the retry deleted the checkout and that work for Fresh, ContinuationAbsent, ContinuationEqual and Review. A first ref-deletion attempt attached the same-head Issue branch to a manual Worktree before returning a lock error; the retry deleted the attached branch for Fresh and ContinuationAbsent, while the manual work file survived.

cargo test architecture_probe_failed_acquisition_retry_authority --bin thirdshift -- --exact worktree::acquisition_tests::architecture_probe_failed_acquisition_retry_authority --nocapture confirmed all ten cases. The probe passed because it explicitly asserted the destructive baseline outcomes; it was then removed. On the restored source, cargo test --bin thirdshift worktree::acquisition_tests passed all 34 tests, with zero failures. Both report diagrams passed Mermaid parsing without a browser. No implementation or domain-document edits remain.

All 47 open issue titles and bodies were read; none proposes this deepening. Closed #501 established clean-only failed-add recovery, and closed #528 explicitly excluded failed acquisition from successful-acquisition disposal. This candidate ranks first because retry authority escapes a recently changed module with destructive observable consequences.

Architecture review

Strength: Strong · Dependencies: local-substitutable

Files: src/worktree/acquisition.rs, src/worktree/ownership.rs, src/git.rs, src/worktree/acquisition_tests.rs, src/worktree/cleanup_tests.rs, src/test_support.rs, CONTEXT.md

Before

flowchart LR
  Run["Run and Continuation"] --> Recovery["Worktree acquisition recovery"]
  Review["Architecture review"] --> Recovery
  Recovery --> Inspection["Ownership and work inspection"]
  Recovery --> Checkout["Checkout removal"]
  Recovery --> Ref["Issue branch removal"]
  Checkout -. "one check then retries" .-> Git["Git adapter"]
  Ref -. "one check then retries" .-> Git
  classDef leak stroke:#dc2626,stroke-width:2px;
  class Checkout,Ref leak;
Loading

After

flowchart LR
  Run["Run and Continuation"] --> Recovery
  Review["Architecture review"] --> Recovery
  subgraph Recovery["Worktree acquisition recovery"]
    Inspection["Ownership and work inspection"] --> Checkout["Checkout removal"]
    Checkout -->|"verified outcome"| Ref["Issue branch removal"]
  end
  Checkout --> Git["Git adapter"]
  Ref --> Git
  Git -->|"attempt guards"| Inspection
  classDef deep stroke-width:3px;
  class Recovery deep;
Loading

Problem: The recovery module's shallow Git interface separates ownership and clean-work inspection from retries, allowing later attempts to erase work or delete an attached Issue branch.

Solution: Add depth behind the existing Worktree acquisition interface: one recovery implementation owns clean-only verified transitions at the Git adapter seam.

Wins

  • locality: recovery owns attempt authority
  • leverage: four acquisition paths share recovery
  • Tests cross the existing interface
  • Later work survives retry refusal

Activity

  1. added
    needs-triage
    ready-for-agent
    architect-plan
    in-progress
    and removed
    needs-triage
    ready-for-agent
    on Oct 7, 2026
  2. removed
    in-progress
    on Oct 8, 2026
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

    architect-plan

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions