Skip to content

Feature request: atomic return-if-clean (refuse on uncommitted, unpushed, or live processes under the slot lock) #216

Closed

Description

@Dematom1

Summary

Request a way to return a leased worktree only if it is clean and quiet, checked and acted on as one step under treehouse's own lock: no uncommitted changes, no commits that are not on a remote, and no processes in the slot. If any of those fail, treehouse keeps the slot and the lease and exits with a distinct code that says what blocked. If all pass, it resets and returns the slot.

Something like:

treehouse return <path|name> --if-lease-id <id> --if-clean

(The flag name is only a suggestion.)

Use case

dibs is a lease manager for local dev resources. It is adding an opt-in treehouse-worktree adapter. dibs acquires slots through treehouse get --lease or registers an existing slot with treehouse lease. It never allocates or resets slots itself. When a lease ends (released, or reclaimed after its owner process is proven dead or its TTL expired), dibs hands the slot back with the conditional treehouse return <path> --if-lease-id <id>.

dibs must never throw away work that has not landed. Before it returns a slot, it checks:

  1. git status shows no uncommitted changes.
  2. HEAD is reachable from a remote ref, so no commits are unpushed.
  3. treehouse status lists no processes in the slot.

If any check fails, dibs keeps the lease and reports what blocked the return.

The race

These checks run in dibs, and the return runs later in a separate treehouse process. Between the last check and the reset inside treehouse return, a process can start in the slot and make a commit. Example: an agent shell attaches with treehouse enter and runs git commit. return then terminates that process and resets the slot. The tree is clean, so return does not refuse. return does not check for unpushed commits, so it resets the slot anyway. A commit on a named branch survives in the shared repository but is no longer checked out anywhere. A commit made on a detached HEAD is reachable only from the reflog.

dibs narrows the window: it checks processes and unlanded work again right before it calls return, and the slot stays leased throughout, so get never hands it to anyone else. But only treehouse can close the window. It owns the slot lock, and it already terminates processes and resets under that lock. Running "is it clean, unpushed-free, and quiet?" in the same critical section as the reset would make the guarantee exact.

Proposed behavior

  • Under the slot's lock, after treehouse terminates lingering processes, or instead of terminating them (whichever fits; refusing on any live process is the most conservative option):
    • refuse if the worktree has uncommitted changes (including untracked files that are not ignored),
    • refuse if HEAD (or any local branch created in the slot) has commits that no remote ref contains,
    • otherwise reset and return as today.
  • On refusal: keep the lease, change nothing, and exit with a distinct non-zero code. A --json reason (dirty, unpushed, processes) would let callers report exactly what blocked.
  • Compose with --if-lease-id / --if-lease-holder, so a stale caller still cannot touch a reassigned slot.
  • Never prompt. This is for non-interactive callers.

Offer

We are happy to implement this and send a PR if the shape sounds right to you. Pointers to where the return critical section lives, or a different flag or exit-code design you prefer, are welcome.

Activity

  1. kunchenguid commented on Nov 16, 2026

    @kunchenguid
    Owner

    Speaking as Kun's firstmate: thanks for the detailed write-up, @Dematom1, and for the offer to implement it.

    I checked latest main (bd3cc28). treehouse return today only confirms on uncommitted changes (and --force or a non-TTY skips/aborts that), and it does not check for unpushed commits or refuse on live processes; it terminates them. So the race you describe is real and only treehouse can close it under its own lock.

    VISION.md check:

    • Isolation and reuse / Safe lifecycle operations: aligns. Refusing to reset work that cannot be verified as landed is exactly "under uncertainty, leave the worktree in place with an actionable explanation."
    • Ownership and interface: aligns. A distinct exit status plus a machine-readable reason keeps exit status honest for callers.
    • Scope: aligns. An optional flag for automated callers; no particular tool is required, and dibs stays an outside integration.
    • contract-class: opt-in. A plain treehouse return keeps behaving exactly as it does now; the new checks run only when the caller asks for them.

    I've labeled this ready-for-pr, and a PR from you is welcome. Pointers: the single-worktree path is releaseWorktree in cmd/return_cmd.go (which already composes --if-lease-id / --if-lease-holder via pool.ReleasePreconditions in internal/pool/pool.go), and exit codes live in cmd/exit.go (ExitNotReturned = 3 is the existing "not returned" code). Please cover the jj backend as well as git (or refuse clearly where "unpushed" cannot be verified), and include realistic end-to-end tests for the dirty, unpushed and live-process refusals, plus the race where a commit lands right before the return. VISION asks for that on lifecycle and destructive changes. Please also raise the PR via no-mistakes, since CI requires it.

  2. kunchenguid commented on Nov 18, 2026

    @kunchenguid
    Owner

    Speaking as Kun's firstmate: implemented by #217 (treehouse return --if-clean), now merged as 38fcb38. Thanks @Dematom1 for both the proposal and the implementation.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions