Repository navigation
Description
What to build
A Run and a Spec run must synchronize only the Worktree they acquired, on its selected Issue branch. If an opening session or Repair switches or detaches that checkout, replaces it, or damages its ownership evidence, ordinary synchronization must fail before changing unrelated work or treating its head as the Run's. Keep legitimate commits on the acquired Issue branch valid.
Deepen acquired Worktree ownership so the existing Worktree interface owns these prerequisites for Delivery, Spec branch catch-up and the Repair loop. Confirmed Self-merge finishing must still complete after interruption, deleting the expected origin branch through the captured Launch repository rather than borrowing a replacement checkout's origin.
Acceptance criteria
- One acquired ownership implementation governs ordinary push, head observation, Base branch merge, Foreign commit observation and merge, Spec branch fast-forward, pending-merge validation and Base branch movement checks. Their callers keep the existing Worktree interface; callers neither acquire an ownership guard nor invoke a separate validator. Remove the independent pathname-only Git view from Worktree.
- Ordinary operations refuse switched, detached, recreated, missing, locked or uncertain checkout evidence, including changed root, administrative or common repository instances, symlinked Git links and inconsistent registrations/backlinks. Refusal applies to observations and unchanged/no-op paths too. New ownership errors name the acquired checkout and expected Issue branch; unrelated contents, index, refs and origin branches stay untouched when initial ownership is invalid.
- The acquired owner verifies the pinned common repository before acquiring the ordinary ownership lock and holds that lock through each complete ordinary operation. Fetch keeps its existing internal refs-lock scope, after the ownership lock; ordinary operations never recursively acquire their own lock or hold it across a session, CI watch or Repair. Waiting and Git execution remain interruptible, with release on errors and unwinding.
- Ownership is checked before checkout commands, after fetching and before merging or pushing, and before returning a successful observation, clean merge, pending conflict or push. An ownership change during an earlier command prevents later commands and success. The implementation keeps raw Git execution private so routines cannot bypass these gates. These checks and advisory locks cover observed identity and cooperating factory operations; they do not claim atomic exclusion against arbitrary concurrent filesystem or manual Git changes.
- Existing synchronization behavior survives: current commits may advance on the acquired branch; origin samples retain qualified refs and exact commit IDs; Foreign commit observations remain tied to their producing owner and local head; merge and fast-forward policy, pending-conflict checks, readable upstream labels, missing-ref/transport errors and existing synchronization diagnostics remain intact. A lock is released before the conflict Repair starts.
- Confirmed Self-merge branch deletion runs through a completion view of the captured Launch repository, independently of checkout attachment/root/admin validity. Verify the pinned common repository and the Launch directory's association with it before remote commands and before reporting success. Delete only the stored Issue branch and preserve reconciliation when it is already absent on origin. A repository identity failure remains a deletion warning; issue closing and Claim completion continue, and recorded interruption is never cleared.
- An ordinary ownership error follows Delivery's existing Failed run path: keep the original cause, attempt acquired-instance preservation, finish the captured PR and end the Claim as today. Preservation's existing retention transition and verified cleanup remain authoritative. The change adds no reset, reattachment, replacement discovery, session or Repair to recover ownership.
- Behavior tests cross the Worktree interface using real temporary Git repositories and a local bare origin adapter. Cover both demonstrated wrong-merge cases, refusal by the other ordinary operations, detached/locked/damaged evidence, no-op observations, ownership changing during fetch or push, ordinary interruption while waiting, and valid commit advancement. Assert contents, index, refs, origin state, errors and conservative disposal rather than private fields or incidental inspection-command order. Preserve the existing synchronization, acquisition, preservation and cleanup coverage.
- Completion tests prove that a switched or replaced checkout cannot redirect branch deletion to another origin, while a confirmed Self-merge still finishes after interruption or GitHub's earlier branch deletion. Merge run and Spec run tests remain green, including Spec catch-up and conflict Repairs. Every controlled worker is released and joined by its own handle before a test can unwind.
- Clarify the Worktree definition in
CONTEXT.md: ordinary synchronization and observations act only on the acquired checkout and selected Issue branch, and confirmed Self-merge branch deletion uses the captured repository independently of checkout validity. Preserve the existing acquisition, preservation, cleanup and stale-review guarantees. No new domain term or ADR is required. - Run focused Worktree, Merge run and Spec run checks, then the repository's formatting, clippy and full test checks. Keep supported Linux and macOS behavior intact.
Blocked by
None (can start immediately).
Decisions
The design tree is settled in four rounds: scope and compatibility → acquired operation seam → locking, execution and completion → failure behavior, tests, documentation and plan size. Every branch has a decision and reason; the frontier is empty.
- Round 1 — deepen ordinary Worktree ownership. This is Strong because two checks through the existing interface reproduced a Base branch merge that mutated an unrelated branch or a recreated checkout and returned success. Normal synchronization has a separate pathname authority even though preservation and cleanup already pin the acquired instance. Concentrating those prerequisites provides locality and fixes the inconsistency without requiring a human policy decision.
- Round 1 — cover the complete ordinary synchronization interface. Push, head observation, both kinds of merge, Foreign commit observation, fast-forward, pending-merge validation and Base branch movement checks must concern the same acquired Issue branch. Gating only mutation would still let unrelated head observations authorize later CI or merge decisions, so read-only and no-op results need the same ownership rule.
- Round 1 — preserve acquisition and synchronization policy. Keep branch selection, independent Launch/Base and acquisition sampling, qualified origin refs, pinned upstream commit IDs, Foreign commit owner/local-head checks, merging rather than rebasing, and current fast-forward/conflict behavior. They already hide useful behavior; this change deepens identity prerequisites rather than reopening those decisions.
- Round 2 — use the existing Worktree seam, with an internal acquired-operation scope. The owner holds ordinary execution and verification together; raw Git stays private behind checked execution. Worktree routines may retain their synchronization logic, but cannot reach a pathname-only Git view or obtain a reusable public validation token. This removes caller ordering obligations and gives leverage to Delivery, Spec catch-up and the Repair loop. Deleting this deeper ownership implementation would spread identity, locking and execution prerequisites back across those operations and callers instead of eliminating them.
- Round 2 — retain instance identity while allowing current HEAD to advance. Reuse pinned checkout, administrative and common directory handles, registration checks and expected attached branch. Do not pin normal operations to the acquisition commit: implement sessions and Repairs legitimately commit on the selected Issue branch. Reuse the existing inspector rather than duplicate a weaker branch-only check.
- Round 2 — keep dependencies local-substitutable. The existing Git adapter, temporary repositories, bare origin and controlled execution fixtures already exercise the production seam. No new polymorphic adapter or hypothetical port is justified, and this change needs no new runtime dependency.
- Round 3 — hold the ordinary ownership lock for a complete operation. Verify the captured common directory first, then acquire the existing ownership lock with ordinary interruption behavior. This prevents cooperating factory acquisition, preservation and disposal from changing checkout lifetime during the operation. Accept serialization through a normal fetch or push so an operation cannot act on another factory-owned checkout instance.
- Round 3 — let fetch own its existing refs-lock scope. Acquisition, preservation and disposal already take ownership before refs; fetch takes only refs. With the ownership lock held, fetch can safely take refs internally, release it and let the operation use its sampled commit. Do not hold refs and recursively invoke fetch, or call another guarded public Worktree operation from inside an operation. Private implementation steps share the acquired scope, which avoids self-deadlock and preserves lock order.
- Round 3 — gate successive commands and successful returns. Inspect before checkout commands, after fetch before using its result, before a later mutation and before final success. An initial check alone misses a session/manual change during an earlier command; a successful push alone does not establish final ownership. Checks permit valid new commits and a pending merge on the selected branch. Advisory locks and repeated inspection do not provide atomic exclusion against arbitrary outside writers; preserve that practical limitation explicitly.
- Round 3 — keep ordinary execution interruptible. Ordinary lock waits and Git commands retain their ordinary views; do not turn synchronization into completion work or clear interruption. Inspection's existing internal observations do not grant mutation authority after interruption. Release the operation scope before running any session, conflict Repair or CI watch so those steps never inherit its lock.
- Round 3 — give confirmed Self-merge finishing repository authority. Branch deletion follows an irreversible confirmed merge and must finish even if the checkout changed. Run it from the captured Launch repository with completion execution, using the pinned common directory and Launch association checks, rather than executing from the checkout pathname. Its authority is the already confirmed merge and stored Issue branch, so requiring a valid attached checkout would incorrectly obstruct completion. Verify repository evidence around the remote sequence, preserve already-absent reconciliation, and let deletion failures warn while issue/Claim finishing continues. Shared repository configuration is not frozen by directory identity, as in the existing behavior.
- Round 4 — preserve failure and retention behavior. An ordinary gate failure remains the cause passed into Delivery's existing Failed run path. Acquired preservation then decides whether work reached origin and whether cleanup is allowed. Do not introduce a second retention state or reset/reattach a checkout to make a gate pass: that would duplicate ownership policy or change work the owner cannot establish as its own. Add contextual diagnostics to ownership errors without rewriting existing Git/synchronization failures.
- Round 4 — test at the existing interface. Use real Git and local origin adapters, with the existing isolated fixtures for controlled command interleavings and signals. Assert changed-resource refusal, unchanged unrelated work/refs/origin and normal observable synchronization. Keep preservation and cleanup tests because they verify separate operations; replace only tests that become redundant private-helper checks. The interface is the test surface.
- Round 4 — verify both refusal and compatibility. Keep permanent regressions for switched-branch and same-path recreation merges, exercise the remaining ordinary operations and observation/no-op cases, and test changes during fetch/push plus ordinary interrupted lock waits. Existing sampled-commit, Foreign commit, conflict and fast-forward tests establish behavior that must survive. Add completion coverage for a checkout with a different origin, and retain Merge run and Spec run finishing/Repair checks because those are the real callers.
- Round 4 — record domain clarification through implementation. The Ticket updates the Worktree definition through its pull request, naming the ordinary-operation rule and captured-repository completion. The module keeps its established domain name. This follows ADR 0002's recoverable Continuation, ADR 0004's confirmed Self-merge and ADR 0014's acquired-instance ownership; no ADR is contradicted or reopened. The private seam and lock scope are reversible and need no new ADR.
- Round 4 — publish one standalone Ticket. The inspector, locks and adapters exist, and the complete operation refactor plus regressions fits one fresh session. Splitting it would duplicate a single ownership protocol across temporary seams. There is no parent or blocker, and the only label is
needs-triage; thirdshift decides readiness after this review ends.
Coverage check: all 47 open issue titles and bodies were read and checked again before publication; none proposes this deepening. Closed #503 established acquired lifetime cleanup and closed #518 established acquired Failed run preservation; ordinary synchronization was explicitly outside their scope.
Review validation: on Base branch main at 21ad9938738e2f49f425d820569f26208c0c4cfe, two throwaway Worktree-interface checks failed on the unchanged implementation: Base branch merging advanced an unrelated branch after a checkout switch, and advanced a replacement checkout recreated on the same Issue branch and starting head, reporting a clean merge in each case. A throwaway prototype routed ordinary operations through acquired inspection and one ordinary ownership-lock scope; all 122 Worktree tests, including both regressions, passed. The prototype also removed the independent Worktree Git view and moved confirmed branch deletion to captured-repository completion. All 20 Merge run and 64 Spec run tests passed, including interrupted post-merge finishing and Spec conflict/Repair paths. A separate completion probe replaced the checkout with an independent clone whose origin also had the Issue branch; branch deletion removed the acquired repository's remote branch and left the replacement origin's branch untouched. All three final probes passed. Prototype edits are discarded; permanent implementation and regressions are this Ticket's work.
Architecture review
Strength: Strong · Dependencies: local-substitutable (Git and a local origin adapter)
Files: src/worktree.rs, src/worktree/ownership.rs, src/worktree/synchronization_tests.rs, src/worktree/cleanup_tests.rs, src/worktree/preservation_tests.rs, src/delivery.rs, tests/merge_run.rs, tests/spec_run.rs, CONTEXT.md
Before
flowchart LR
D[Delivery] --> W[Worktree]
S[Spec run] --> W
W --> N[Worktree synchronization]
W --> C[Self-merge completion]
W --> O[Worktree ownership]
O --> I[Checkout inspection]
I --> R[Repository inspection]
R --> G[Git adapter]
N -. pathname .-> G
C -. checkout origin .-> G
classDef leak stroke:#dc2626,stroke-width:2px;
class N,C,G leak;
After
flowchart LR
D[Delivery] --> W[Worktree]
S[Spec run] --> W
W --> O
subgraph O[Worktree ownership]
N[Worktree synchronization] --> I[Checkout inspection]
I --> R[Repository inspection]
C[Self-merge completion] --> R
end
R --> G[Git adapter]
N --> G
C --> G
classDef deep stroke-width:3px;
class O deep;
Problem: Worktree synchronization is shallow on ownership: its implementation uses pathnames instead of the acquired checkout seam and can merge unrelated work while reporting success.
Solution: Make acquired Worktree ownership a deep module whose existing interface owns checked synchronization and Self-merge completion in the captured repository.
Wins
- Depth: identity prerequisites stay private
- Locality: operations share ownership inspection
- Leverage: callers keep one interface
- Tests cross the existing seam
- Replacement checkout work stays untouched
- Completion uses the captured repository
Activity