Skip to content

feat(pool): add return --if-clean - #217

Merged
kunchenguid merged 15 commits into
kunchenguid:mainfrom
Dematom1:fm/treehouse-216-if-clean
Nov 18, 2026
Merged

kunchenguid merged 15 commits into
kunchenguid:mainfrom
Dematom1:fm/treehouse-216-if-clean

Conversation

@Dematom1

@Dematom1 Dematom1 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Implements #216: treehouse return --if-clean returns a worktree only when it has no uncommitted changes, no commits unreachable from a remote ref, and no running processes, checked against one HEAD snapshot under the slot lock; refuses with exit 4 and a machine-readable reason otherwise; git only, jj refuses as unsupported; plain return unchanged. Known limit: a process starting in the final instant before the reset cannot be fenced.

What Changed

  • Add treehouse return --if-clean to release a slot only when its worktree has no uncommitted changes or commits unreachable from a remote ref, and no running processes; checks use one HEAD snapshot under the slot lock.
  • Report refusal with exit code 4 and a machine-readable reason; reject the option for jj while leaving plain return unchanged.

Risk Assessment

⚠️ Medium: The clean-only path is carefully guarded, but snapshot failures currently escape its documented machine-readable refusal contract.

Testing

Targeted command-level tests drove the real CLI against isolated Git repositories, verifying clean return and required refusals; captured CLI test transcripts. The JJ unsupported case was attempted but skipped because no jj executable is available; no visual surface applies.

  • Live validation: ✅ go - 6 of 7 scenarios driven live against the product
Scenario Result Live Evidence
Return clean landed Git worktree ✅ pass live return-if-clean-git.txt: TestReturnIfCleanReturnsACleanLandedSlot
Refuse dirty Git worktrees without changing them ✅ pass live return-if-clean-git.txt: TestReturnIfCleanRefusesUncommittedChanges
Refuse unreachable commits; accept once pushed to a remote ref ✅ pass live return-if-clean-git.txt: TestReturnIfCleanRefusesUnpushedCommitsUntilTheyLand
Refuse a live process without terminating it ✅ pass live return-if-clean-git.txt: TestReturnIfCleanRefusesALiveProcessWithoutTerminatingIt
Use current HEAD after waiting for slot lock ✅ pass live return-if-clean-git.txt: TestReturnIfCleanSeesACommitThatLandsWhileItWaitsForTheLock
Refuse edited or unverifiable seeded files ✅ pass live return-if-clean-git.txt: TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile
Reject JJ clean-only return as unsupported ⏸️ untested no Attempted the existing CLI end-to-end test and jj version; test skipped because jj is absent from PATH. No repository-local jj binary or credential/authority is supplied. Could not provide a live JJ…
Evidence: Git return --if-clean end-to-end transcript
=== RUN   TestReturnIfCleanReturnsACleanLandedSlot
--- PASS: TestReturnIfCleanReturnsACleanLandedSlot (1.11s)
=== RUN   TestReturnIfCleanRefusesUncommittedChanges
=== RUN   TestReturnIfCleanRefusesUncommittedChanges/tracked_edit
=== RUN   TestReturnIfCleanRefusesUncommittedChanges/edit_hidden_by_assume-unchanged
=== RUN   TestReturnIfCleanRefusesUncommittedChanges/untracked_file
--- PASS: TestReturnIfCleanRefusesUncommittedChanges (3.05s)
    --- PASS: TestReturnIfCleanRefusesUncommittedChanges/tracked_edit (1.03s)
    --- PASS: TestReturnIfCleanRefusesUncommittedChanges/edit_hidden_by_assume-unchanged (1.00s)
    --- PASS: TestReturnIfCleanRefusesUncommittedChanges/untracked_file (1.02s)
=== RUN   TestReturnIfCleanRefusesUnpushedCommitsUntilTheyLand
=== RUN   TestReturnIfCleanRefusesUnpushedCommitsUntilTheyLand/detached
=== RUN   TestReturnIfCleanRefusesUnpushedCommitsUntilTheyLand/branch
--- PASS: TestReturnIfCleanRefusesUnpushedCommitsUntilTheyLand (3.11s)
    --- PASS: TestReturnIfCleanRefusesUnpushedCommitsUntilTheyLand/detached (1.55s)
    --- PASS: TestReturnIfCleanRefusesUnpushedCommitsUntilTheyLand/branch (1.56s)
=== RUN   TestReturnIfCleanRefusesALiveProcessWithoutTerminatingIt
--- PASS: TestReturnIfCleanRefusesALiveProcessWithoutTerminatingIt (1.43s)
=== RUN   TestReturnIfCleanSeesACommitThatLandsWhileItWaitsForTheLock
--- PASS: TestReturnIfCleanSeesACommitThatLandsWhileItWaitsForTheLock (1.42s)
=== RUN   TestReturnIfCleanComposesWithTheLeaseCondition
--- PASS: TestReturnIfCleanComposesWithTheLeaseCondition (0.76s)
=== RUN   TestReturnIfCleanRejectsForceAndAll
--- PASS: TestReturnIfCleanRejectsForceAndAll (0.64s)
=== RUN   TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile
=== RUN   TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile/symlink_source
=== RUN   TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile/edited
=== RUN   TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile/source_gone
=== RUN   TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile/untouched
--- PASS: TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile (5.57s)
    --- PASS: TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile/symlink_source (2.15s)
    --- PASS: TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile/edited (1.05s)
    --- PASS: TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile/source_gone (1.07s)
    --- PASS: TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile/untouched (1.30s)
=== RUN   TestReturnIfCleanRefusalKeepsTheCheckedOutBranch
--- PASS: TestReturnIfCleanRefusalKeepsTheCheckedOutBranch (0.94s)
=== RUN   TestReturnIfCleanRunsNoCheckoutHook
--- PASS: TestReturnIfCleanRunsNoCheckoutHook (1.23s)
PASS
ok  	github.com/kunchenguid/treehouse/v3/cmd	20.086s
Evidence: JJ availability and attempted end-to-end test
=== RUN   TestJJReturnIfClean
    return_if_clean_e2e_test.go:317: jj is not installed
--- SKIP: TestJJReturnIfClean (0.00s)
PASS
ok  	github.com/kunchenguid/treehouse/v3/cmd	0.806s

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 warning
  • ⚠️ internal/pool/pool.go:1060 - If snapshotting the reset target or HEAD fails, resetIfClean returns the raw error rather than NotCleanError. For example, a registered Git slot whose configured base ref has disappeared reaches IsWorktreeSafeToReset, errors, and exits 1 with no machine-readable blocker instead of the required exit 4 refusal. Treat this pre-write inability to verify as a clean-only refusal (and preserve ordinary-return behavior).
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 6 of 7 scenarios driven live against the product
Scenario Result Live Evidence
Return clean landed Git worktree ✅ pass live return-if-clean-git.txt: TestReturnIfCleanReturnsACleanLandedSlot
Refuse dirty Git worktrees without changing them ✅ pass live return-if-clean-git.txt: TestReturnIfCleanRefusesUncommittedChanges
Refuse unreachable commits; accept once pushed to a remote ref ✅ pass live return-if-clean-git.txt: TestReturnIfCleanRefusesUnpushedCommitsUntilTheyLand
Refuse a live process without terminating it ✅ pass live return-if-clean-git.txt: TestReturnIfCleanRefusesALiveProcessWithoutTerminatingIt
Use current HEAD after waiting for slot lock ✅ pass live return-if-clean-git.txt: TestReturnIfCleanSeesACommitThatLandsWhileItWaitsForTheLock
Refuse edited or unverifiable seeded files ✅ pass live return-if-clean-git.txt: TestReturnIfCleanRefusesAnEditedOrUnverifiableSeededFile
Reject JJ clean-only return as unsupported ⏸️ untested no Attempted the existing CLI end-to-end test and jj version; test skipped because jj is absent from PATH. No repository-local jj binary or credential/authority is supplied. Could not provide a live JJ…
  • go test ./cmd -run '^TestReturnIfClean' -count=1
  • go test ./cmd -run '^(TestReturnIfClean|TestJJReturnIfClean)$' -count=1 -v (JJ case skipped: executable unavailable)
  • jj version (command not found)
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…ould be lost

Implements kunchenguid#216. 'treehouse return --if-clean' refuses, under the same pool
lock as the reset and after any --if-lease-* condition, when the worktree
has a live process, uncommitted changes, or commits that neither a
remote-tracking ref nor the base branch contains. A refusal keeps the slot
and its lease, exits 4 (ExitNotClean), and prints the blocker (processes,
dirty, or unpushed) alone on stdout. It never prompts and never terminates
processes. Git and jj slots are both covered through two new vcs.Backend
methods; plain return is unchanged.
@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest changes appear safe to merge.

Reviews (10) · Last reviewed commit: "no-mistakes(document): Document clean-re..." · Reviewed by Greptile

Comment thread internal/pool/pool.go Resolved
Comment thread internal/vcs/gitvcs/gitvcs.go Resolved
@greptile-apps

This comment has been minimized.

…rified reset target and HEAD and using the guarded reset, which rechecks HEAD and dirtiness before destructive updates. `go test ./internal/pool` and `git diff --check` pass; broader VCS tests timed out. Seeded ignored-file edits remain unresolved because the recorded inventory has paths but no seed-content baseline, so the CI finding is not fully addressed
Comment thread internal/pool/pool.go Resolved
Comment thread internal/pool/pool.go Resolved
… landed remote commits without the base-merge verdict, and made --if-clean recheck processes without terminating them. `go test ./cmd/ -run 'ReturnIfClean'`, `go test ./...`, and `git diff --check` pass. Seeded-file verification and post-checkout race coverage remain unimplemented; this phase is incomplete
Comment thread internal/pool/pool.go Resolved
Comment thread cmd/return_cmd.go Resolved
Rework the --if-clean release so a refusal can never discard work or change
the checkout, closing the races the earlier check-then-reset shape left open.

- Record HEAD and the reset target before any check, and judge landedness
  against that exact commit (vcs.IsCommitLanded), so a commit landing
  mid-return moves HEAD off the snapshot and the reset refuses it.
- Change nothing until every check passes: no git checkout (and so no
  checkout hooks), no detach, no process termination.
- Re-run every check inside the guarded reset after HEAD is locked and
  compared, immediately before anything is removed
  (vcs.ResetWorktreeToRefVerified).
- Treat a seeded file that differs from its source, or whose source is gone,
  as uncommitted work.
- Never report success when the guarded reset fails: re-judge and report the
  blocker, or the failure itself.
- Work on any remote-tracking ref has landed; no merged-into-base requirement.

Plain return is unchanged. Adds one regression test per earlier review
finding.
jj takes no lock that could hold the final verdict until its destructive
steps, and each attempt to re-check inside jj's reset left a window. Take the
"or refuse clearly" alternative from kunchenguid#216: --if-clean on a jj workspace
refuses up front, before any change, with exit 4 and `unsupported`. Plain jj
return is unchanged.

- Remove the partial jj verified-reset path: jjvcs is back to its state on
  main, and the clean-return helpers (UncommittedWork, IsCommitLanded,
  ResetWorktreeToRefVerified) are Git-only vcs functions outside the
  Backend interface, which returns to 21 operations.
- A tree already dirty when the git guarded reset takes HEAD's lock refuses
  even when the caller's verdict comes back empty, instead of reporting a
  reset that never ran.
- The jj test asserts the refusal on clean, dirty and unpushed workspaces,
  each left untouched; docs and help say --if-clean is Git-only.
@Dematom1 Dematom1 changed the title feat(pool): add clean-only worktree returns feat(pool): add guarded clean-only worktree returns Nov 16, 2026
Comment thread internal/pool/pool.go Resolved
Comment thread internal/pool/pool.go Resolved
…ds now compare as the regular-file target text seeding writes, while copied symlinks are rejected. Late reset-step errors are no longer reclassified by a clean recheck; Git attempts to restore the recorded HEAD tree/index and reports the failed step. Updated design docs. Both requested test commands pass. Note: the requested forced post-write failure regression test was not added
Comment thread internal/vcs/gitvcs/gitvcs.go Resolved
…-if-clean and verifies/authenticates the original worktree before restoring; replaced worktrees are left untouched. Plain reset behavior is unchanged. Full `go test ./...` and the requested focused command pass. The requested replacement/post-write regression tests were not added
Add the two regression tests requested for the guarded reset:

- A worktree directory replaced while a clean-only reset runs leaves the
  replacement checkout untouched. The test exposed that, without seeded
  files, nothing re-checked the directory before read-tree acted by
  pathname, so the clean-only reset now re-verifies the recorded directory
  identity right after its final checks, before the first write.
- A failure after the tree was rewritten (forced at the HEAD publish step)
  restores the recorded commit's tree and index, leaves HEAD there, and
  reports the failure.

Both tests fail with their guard removed and pass with it.
@Dematom1 Dematom1 changed the title feat(pool): add guarded clean-only worktree returns feat(pool): add clean-only worktree return Nov 17, 2026
Comment thread internal/vcs/gitvcs/gitvcs.go Resolved
…registration resolver used by validation and cleanup authentication. Added a regression covering cleanup authentication and verified reset from another cwd. Targeted tests and `go test ./...` pass
On macOS the temp dir is reachable as both /tmp and /private/tmp, so the
backlink computed across the two spellings climbed to / and resolved from any
working directory; the test passed with the old lookup too. Resolve symlinks
before computing the relative path, and assert the backlink does not resolve
from the test's working directory. The test now fails on the old
authenticateLinkedWorktree and passes with the shared resolver.

Also restore ValidateWorktreeRegistration's doc comment, which the shared
resolver had displaced, and document the resolver itself.
@Dematom1 Dematom1 changed the title feat(pool): add clean-only worktree return feat(pool): add return --if-clean Nov 17, 2026
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thanks @Dematom1 — this is the #216 implementation, and it's a nice, careful piece of work.

I reviewed the diff (no workflow changes, no security concerns) and approved the fork CI runs on 97ce126, so the checks are running now. Nothing is needed from you until they finish; if anything goes red I'll say so here. One heads-up: cmd and internal/pool already sit close to Go's 10-minute per-package limit on Windows runners, so keep an eye on test (windows-latest).

My read so far: contract-class opt-in — plain return keeps its path, and the clean-only behavior runs only with --if-clean. VISION per rule: Isolation and reuse aligns (never recycles dirty/in-use work); Safe lifecycle aligns (verifies under the slot/HEAD lock and refuses under uncertainty); Ownership and interface aligns (distinct exit 4, blocker on stdout); Scope aligns (small CLI primitive, git-only with an honest jj refusal).

@kunchenguid
kunchenguid merged commit 38fcb38 into kunchenguid:main Nov 18, 2026
8 checks passed
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: this is merged. Thank you @Dematom1 — really appreciate you taking the time on this.

VISION: aligns (isolation/reuse: a clean-only return never recycles dirty, in-use, or unverifiable work; safe lifecycle: verifies against current Git/process state and leaves everything in place with a named blocker under uncertainty; interface: blocker on stdout, distinct exit 4; scope: local CLI primitive, no new dependency). Contract-class: opt-in — plain return keeps its path; the only shared-path change resolves a relative worktree backlink against the Git metadata directory, matching what ValidateWorktreeRegistration already did.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants