Repository navigation
Description
What to build
Make Failed run preservation act only on the Worktree instance and Issue branch the Run acquired. When a session switches or detaches the checkout, or another checkout replaces it at the same pathname, preservation must refuse before changing that checkout, retain the local resources, and report why the work could not be preserved.
Today preservation commits on the checkout's current HEAD but pushes the stored Issue branch. A switched checkout can therefore receive the failure commit while the unchanged Issue branch reaches origin, and preservation returns success. A recreated checkout can have its unrelated work committed and pushed by the old owner. Cleanup already detects these identity changes, but its inspection happens after preservation's mutations.
Acceptance criteria
- The existing Worktree preservation interface delegates the complete abort, stage, failure-marker commit and push sequence to acquired checkout ownership. Delivery and other callers need no additional identity checks or ordering rules.
- Preservation reuses the same acquired checkout inspection as disposal: pinned checkout, administrative and common repository directory instances; Git link and backlink consistency; unambiguous registration; and the selected attached Issue branch. Switched, detached, locked, replaced, missing, inconsistent or unreadable ownership evidence refuses preservation before the next mutation. The current head may advance normally on the acquired Issue branch.
- Completion execution acquires the existing ownership lock followed by the refs lock, holds them through preservation's final inspection, and never clears recorded Command interruption. The pinned common directory is verified before locking. No preservation step acquires those locks recursively or fetches origin.
- Ownership is inspected before merge abort, staging, commit and push, and again before declaring success, including the unchanged-work path. An ownership failure after a successful push still reports failure and retains local resources.
- Retention is armed before inspection or lock acquisition and remains armed on every error or unwinding. Only the final successful ownership inspection authorizes normal cleanup. A later successful retry on the original acquired instance can restore cleanup eligibility.
- Existing preservation behavior remains: merge abort; committed and uncommitted work; failure metadata; skipped rejecting local hooks; comparison with the last-fetched Base branch without another fetch; completed nonzero cached-diff probe semantics, including a missing comparison ref; and unchanged work creating no failure commit or remote branch. Existing Git operation diagnostics remain intact; new ownership failures name the acquired path, expected Issue branch and cause.
- Tests through the Worktree interface cover switched and detached checkouts, same-path recreation with the same branch and head, locked or damaged ownership evidence, both repository locks, ownership changing during a push, and refusal on the unchanged-work path. They assert errors, unchanged unrelated work and refs, origin contents and retention after Drop. Existing preservation and disposal tests remain green, including recorded interruption, normal commit advancement, retries, failed Git operations and unwinding.
- Delivery retains the original Failed run cause and uses its existing preservation warning when ownership refuses salvage; PR finishing and Claim ending keep their current behavior. Clarify the Worktree entry in the domain glossary that preservation acts only on the acquired instance and retains work on changed or uncertain ownership. No new glossary term or ADR is required.
Blocked by
None (can start immediately).
Decisions
The design tree was settled in three rounds: scope → seam and ownership → locking, execution and refusal → mutation gates, retention, compatibility, reporting, tests and documentation. The frontier is empty.
- Round 1 — scope and selection: deepen Failed run preservation with acquired Worktree ownership. This is the top recommendation because the recent Worktree hotspot has a reproducible wrong mutation and false success at an existing interface, and its design can reuse established ownership facts. All open issue titles and bodies were checked; none proposes this deepening. Ordinary Worktree synchronization and Base branch comparison policy are outside this Ticket because they add separate obligations beyond the demonstrated failure.
- Round 1 — seam and placement: keep the Worktree preservation interface unchanged. Its private acquired checkout ownership module owns the salvage sequence, rather than giving callers a separate validator they must remember to invoke. This provides leverage without expanding the interface and gives preservation and disposal locality through one inspection implementation. The private sequence may live beside ownership or in a private preservation module; that physical organization does not change the seam. Deletion test: removing ownership-aware preservation would spread identity, retention, completion and mutation-ordering obligations across the Failed run callers, rather than remove that complexity.
- Round 1 — lifetime identity: inspect the existing pinned directory instances, registration, repository and selected Issue branch, and observe the current head. Never compare with the acquisition commit: implement sessions and Repairs are expected to commit on that branch. Use the branch stored by acquired ownership for the push, so the head being committed and branch being pushed belong to the same inspected Worktree.
- Round 2 — locking: verify the pinned common directory before acquiring the ownership lock, then acquire the refs lock, using completion execution. Keep both until the sequence and its final ownership inspection finish. This matches acquisition/disposal's existing lock order and excludes cooperating factory acquisition, disposal and sibling fetches. The sequence contains no nested lock acquisition or fetch. Serializing a salvage push is accepted for this failure path; these advisory locks do not provide atomic exclusion against arbitrary concurrent Git or filesystem changes outside the factory protocol.
- Round 2 — execution: use completion Git views for both Launch directory inspection and checkout operations. Otherwise a recorded interruption prevents the very preservation that must finish afterward. Start no new session or Repair, fetch nothing, and never reset, reattach or discover a replacement checkout to rescue a mismatch: those actions would mutate work the acquired owner cannot establish as its own.
- Round 2 — refusal: failed or unreadable ownership evidence is an error, even if the work would otherwise compare unchanged with the Base branch. Context on these new gate failures names the acquired checkout and expected Issue branch, with the inspection cause. This makes uncertain ownership visible without granting cleanup authority.
- Round 3 — mutation gates: reuse inspection before each abort, stage, commit and push, and after the final action. Inspect the unchanged-work exit too. Initial inspection alone would miss ownership changing during earlier steps; a successful push alone proves neither final ownership nor safe cleanup. Repeated inspections accept the new head created by a valid failure-marker commit.
- Round 3 — retention: preserve the existing retention transition: arm it before delegation, clear it only after final verified success. Errors and unwinding retain the acquired Worktree and local Issue branch; changed resources are left in place. An ownership failure after remote work was pushed remains an error with retention. A later valid retry may clear retention because the existing interface already supports recovery after an error.
- Round 3 — compatibility: retain the existing preservation policy and observable Git failures, including the deliberately tested missing-comparison-ref behavior. This Ticket repairs acquired ownership, rather than changing what work merits a failure marker. Add context only to new ownership-gate failures: a prototype that wrapped every error broke four existing diagnostic tests, while gate-specific context preserves them.
- Round 3 — reporting: let preservation errors flow through Delivery's existing warning that work may exist only locally, while the original Failed run cause remains primary and PR finishing continues. This makes the ownership failure visible without changing established Failed run completion, Self-merge, budgets or Claims.
- Round 3 — testing seam: use the existing Worktree interface with real temporary Git repositories and a local bare origin adapter. Use the existing isolated Git execution fixtures for deterministic changes during a command; hold and release exact lock/thread handles and join every worker. No new hypothetical adapter seam is needed: production and tests cross the same preservation interface. Keep behavior tests for disposal because it remains a distinct observable operation; avoid tests of inspection's private fields or a new gate-only helper.
- Round 3 — required evidence: reproduce a switched checkout by acquiring the Issue branch, checking out another branch, and writing uncommitted work; reproduce replacement by removing and recreating the acquired pathname on the same Issue branch/head and writing replacement work. Both must refuse without staging, committing or pushing unrelated work. Add detached, locked/damaged, unchanged-work, lock-contention and during-push cases to exercise refusal and final retention. Assert work, refs and origin outcomes, rather than incidental read-command order. Existing interface tests provide prior art for interruption, hook rejection, merge abort and retries.
- Round 3 — documentation and ADRs: the Ticket clarifies the Worktree glossary entry through its pull request. It introduces no new domain term. It is consistent with ADR 0002's recoverable Continuation and ADR 0014's acquired-instance ownership; no ADR is reopened, and this reversible private refactor adds no decision that warrants a new ADR.
- Round 3 — plan size: publish one standalone Ticket with no parent or blockers and only
needs-triage. The salvage sequence is small, the inspector and test fixtures exist, and the complete change fits one fresh session. A Spec with Tickets would add no independently useful slice.
Review validation: on Base branch main at 736948e0bfbbd5015fa2230b525115e94a51333b, two throwaway Worktree-interface regressions both failed on the unchanged implementation: preservation falsely succeeded after a branch switch and after same-path checkout recreation. A throwaway prototype moved salvage into acquired ownership, added completion-mode locks and repeated inspection, and passed all 15 preservation tests, including both regressions. The broader Worktree suite also passed all 112 tests. Existing error wording was preserved after checking the diagnostic tests. Prototype files are discarded; implementation and permanent regression tests are this Ticket's work.
Architecture review
Strength: Strong · Dependencies: local-substitutable (real Git and a local origin adapter)
Files: src/worktree.rs, src/worktree/ownership.rs, src/worktree/preservation_tests.rs, src/worktree/cleanup_tests.rs, src/delivery.rs, CONTEXT.md
Before
flowchart LR
D[Delivery] --> W[Worktree]
W --> P[Failed run preservation]
P -. pathname and current HEAD .-> G[Git]
W --> O[Worktree ownership]
O --> I[Checkout inspection]
I --> G
classDef leak stroke:#dc2626,stroke-width:2px;
class P,G leak;
After
flowchart LR
D[Delivery] --> W[Worktree]
W --> O
subgraph O[Worktree ownership]
direction LR
P[Failed run preservation] --> I[Checkout inspection]
end
I --> G[Git]
P --> G
classDef deep stroke-width:3px;
class O deep;
Problem: Failed run preservation is shallow on ownership: its pathname-based implementation bypasses the acquired Worktree seam and can mutate another checkout while reporting success.
Solution: Deepen the Worktree ownership module so one preservation interface concentrates identity gates, completion execution and salvage mutations.
Wins
- Depth: identity prerequisites stay private
- Locality: preservation and disposal share inspection
- Leverage: unchanged interface owns safe salvage
- Tests cross the production seam
- Changed checkout work stays untouched
Activity