:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(devex): count squash-merged worktrees as merged in worktrees:clean (#130065)
Julian Bez82354d68651d
diff --git a/docs/published/handbook/engineering/flox-multi-instance-workflow.md b/docs/published/handbook/engineering/flox-multi-instance-workflow.mdindex 5c21bff433e6..efb5d4f6de32 100644--- a/docs/published/handbook/engineering/flox-multi-instance-workflow.md+++ b/docs/published/handbook/engineering/flox-multi-instance-workflow.md@@ -415,6 +415,7 @@ Every mode also removes folders of removed worktrees that hold only flox caches.Flox activation also runs this once a day in the background: `--mode deps,build` on worktrees idle for 7 days and `--mode full` on worktrees idle for 14 days.It skips worktrees with uncommitted or unpushed work and keeps every branch.+Commits that landed through a squash merge do not count as unpushed: hogli matches the branch's diff against the squash commit, and asks the GitHub CLI for the merged pull request when that fails.Set `POSTHOG_WORKTREES_AUTOCLEAN=0` to turn it off.The output goes to `.flox/cache/worktrees-clean.log` in the checkout that ran it.diff --git a/tools/hogli-commands/hogli_commands/tests/test_worktrees.py b/tools/hogli-commands/hogli_commands/tests/test_worktrees.pyindex ec328999f568..f60f187abfc4 100644--- a/tools/hogli-commands/hogli_commands/tests/test_worktrees.py+++ b/tools/hogli-commands/hogli_commands/tests/test_worktrees.py@@ -729,6 +729,57 @@ class TestComputeGitState:_compute_git_state(wt, has_remotes=True)assert wt.dirty and wt.unpushed == 3 and not wt.detached and wt.unsafe+ def test_untracked_file_is_dirty_when_git_config_hides_untracked_files(self, tmp_path) -> None:+ subprocess.run(["git", "init", "-q", str(tmp_path)], check=True)+ subprocess.run(["git", "config", "status.showUntrackedFiles", "no"], cwd=tmp_path, check=True)+ (tmp_path / "new.py").write_text("never added\n")++ wt = _make_worktree(tmp_path)+ _compute_git_state(wt, has_remotes=False)+ assert wt.dirty and wt.unsafe++ def test_squash_merged_branch_is_safe_until_it_gets_a_new_commit(self, tmp_path, monkeypatch) -> None:+ monkeypatch.setattr(worktrees.shutil, "which", lambda name: None)++ def git(cwd: Path, *args: str) -> None:+ subprocess.run(+ ["git", "-c", "user.name=t", "-c", "[email protected]", *args],+ cwd=cwd,+ check=True,+ capture_output=True,+ )++ remote, repo, worktree = tmp_path / "remote.git", tmp_path / "repo", tmp_path / "topic"+ git(tmp_path, "init", "-q", "--bare", "-b", "main", str(remote))+ git(tmp_path, "clone", "-q", str(remote), str(repo))+ (repo / "a.txt").write_text("one\n")+ git(repo, "add", "a.txt")+ git(repo, "commit", "-q", "-m", "first")+ git(repo, "push", "-q", "origin", "main")+ git(repo, "worktree", "add", "-q", "-b", "topic", str(worktree), "origin/main")+ for name, text in (("b.txt", "two\n"), ("a.txt", "one\nmore\n")):+ (worktree / name).write_text(text)+ git(worktree, "add", name)+ git(worktree, "commit", "-q", "-m", f"change {name}")+ (repo / "c.txt").write_text("landed first\n")+ git(repo, "add", "c.txt")+ git(repo, "commit", "-q", "-m", "unrelated")+ git(repo, "merge", "--squash", "topic")+ git(repo, "commit", "-q", "-m", "topic (#1)")+ git(repo, "push", "-q", "origin", "main")+ git(worktree, "fetch", "-q", "origin")++ merged = _make_worktree(worktree)+ _compute_git_state(merged, has_remotes=True)+ assert merged.unpushed == 2 and merged.squash_merged is not None and not merged.unsafe++ (worktree / "d.txt").write_text("after the merge\n")+ git(worktree, "add", "d.txt")+ git(worktree, "commit", "-q", "-m", "after the merge")+ later = _make_worktree(worktree)+ _compute_git_state(later, has_remotes=True)+ assert later.unpushed == 3 and later.squash_merged is None and later.unsafe+def test_no_remotes_skips_unpushed_check(self, tmp_path, monkeypatch) -> None:# Without remotes, rev-list HEAD --not --remotes would count every commit;# the check must be skipped so a clean local-only worktree isn't flagged.diff --git a/tools/hogli-commands/hogli_commands/worktrees.py b/tools/hogli-commands/hogli_commands/worktrees.pyindex 67e4d85ed4e5..1efc81633918 100644--- a/tools/hogli-commands/hogli_commands/worktrees.py+++ b/tools/hogli-commands/hogli_commands/worktrees.py@@ -13,7 +13,8 @@ Safety: age is a staleness heuristic, not a "no unsaved work" guarantee — editmade without any git operation may not move the activity signal. So `--mode full`skips a worktree with uncommitted/untracked changes, unpushed commits, or gitstate it cannot read (present-but-corrupt/locked) unless `--include-dirty` is-given. Orphaned worktrees (admin gitdir pruned) are removed by default: deleting+given. Unpushed commits whose work a squash merge landed on the default branch+do not count, because the merge queue rewrites every pull request that way. Orphaned worktrees (admin gitdir pruned) are removed by default: deletingthe directory never touches git refs, so committed work survives in the sharedrepo — only uncommitted edits in an already-stale tree could be lost. `--modedeps` and `--mode build` only remove recreatable artifacts, so they have no such@@ -215,6 +216,7 @@ class Worktree:artifact_items: list[Path] = field(default_factory=list)dirty: bool = Falseunpushed: int = 0+ squash_merged: str | None = Nonedetached: bool = Falsestate_unknown: bool = False@@ -225,8 +227,9 @@ class Worktree:Includes worktrees whose git state we couldn't read — an orphaned orunreadable worktree might contain uncommitted work, so we fail closedand treat "can't verify it's clean" as unsafe rather than deleting it.+ Unpushed commits that a squash merge landed are not work at risk."""- return self.dirty or self.unpushed > 0 or self.state_unknown+ return self.dirty or (self.unpushed > 0 and self.squash_merged is None) or self.state_unknown@dataclass(frozen=True)@@ -1421,7 +1424,8 @@ def _populate_git_state(worktrees: list[Worktree], has_remotes: bool) -> None:def _compute_git_state(wt: Worktree, has_remotes: bool) -> None:- status = _git(wt.path, ["status", "--porcelain"])+ # status.showUntrackedFiles=no would hide new files, and --force removes them without asking.+ status = _git(wt.path, ["status", "--porcelain", "--untracked-files=normal"])if status is not None and status.returncode == 0:wt.dirty = bool(status.stdout.strip())@@ -1440,6 +1444,8 @@ def _compute_git_state(wt: Worktree, has_remotes: bool) -> None:else:count = ahead.stdout.strip()wt.unpushed = int(count) if count.isdigit() else 0+ if wt.unpushed:+ wt.squash_merged = _squash_merge(wt.path)return# git couldn't read the worktree's status. Distinguish two cases:@@ -1475,12 +1481,178 @@ def _git(cwd: Path, args: Sequence[str], timeout: float = 30.0) -> subprocess.Coreturn None+# A branch with more changed files than this is no squash merge to look for, and its file list would+# not fit one git command line.+_SQUASH_MAX_FILES = 1000+_SQUASH_MAX_CANDIDATES = 2000+++def _squash_merge(worktree: Path) -> str | None:+ """Where the unpushed commits of *worktree* landed through a squash merge, or None.++ The merge queue lands a pull request as one new commit on the default branch+ and deletes the branch. The branch's own commits are then on no remote and+ are not ancestors of the default branch, although the work is merged.++ The offline check compares the patch id (a stable hash of a diff) of the+ branch's whole change with the default-branch commits since the branch left+ it that touch the same files. It misses a merge whose diff the queue changed,+ for example by resolving a conflict. The GitHub CLI, when it is installed and+ logged in, then looks for a merged pull request that contains HEAD.+ """++ head = _git(worktree, ["rev-parse", "HEAD"])+ if head is None or head.returncode != 0:+ return None+ sha = head.stdout.strip()+ default = _default_branch_ref(worktree)+ if default is None:+ return None+ commit = _squash_commit(worktree, sha, default)+ if commit is not None:+ return commit[:10]+ return _merged_pull_request(worktree, sha, default.removeprefix("origin/"))+++def _default_branch_ref(worktree: Path) -> str | None:+ symbolic = _git(worktree, ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"])+ if symbolic is not None and symbolic.returncode == 0 and symbolic.stdout.strip():+ return symbolic.stdout.strip()+ for candidate in ("origin/master", "origin/main"):+ verified = _git(worktree, ["rev-parse", "--verify", "--quiet", candidate])+ if verified is not None and verified.returncode == 0:+ return candidate+ return None+++def _squash_commit(worktree: Path, head: str, default: str) -> str | None:+ base = _git(worktree, ["merge-base", default, head])+ if base is None or base.returncode != 0:+ return None+ fork_point = base.stdout.strip()+ changed = _git(worktree, ["diff", "--name-only", "--no-renames", fork_point, head])+ if changed is None or changed.returncode != 0:+ return None+ files = changed.stdout.splitlines()+ if not files or len(files) > _SQUASH_MAX_FILES:+ return None+ own = _patch_ids(worktree, ["diff", "--no-renames", fork_point, head])+ if not own:+ return None+ candidates = _patch_ids(+ worktree,+ [+ "log",+ "-p",+ "--no-renames",+ "--format=commit %H",+ "-n",+ str(_SQUASH_MAX_CANDIDATES),+ f"{fork_point}..{default}",+ "--",+ *files,+ ],+ )+ commit = candidates.get(next(iter(own)))+ if commit is None or not _contains_branch(worktree, commit, head):+ return None+ return commit+++def _contains_branch(worktree: Path, commit: str, head: str) -> bool:+ """Whether merging *head* into *commit* changes nothing.++ A patch id leaves out where each hunk applies, so an edit moved to another block with the same+ context lines has the id of the squash it no longer matches. Merging catches that."""++ merged = _git(worktree, ["merge-tree", "--write-tree", commit, head])+ tree = _git(worktree, ["rev-parse", f"{commit}^{{tree}}"])+ if merged is None or tree is None or merged.returncode != 0 or tree.returncode != 0:+ return False+ return merged.stdout.split("\n", 1)[0].strip() == tree.stdout.strip()+++def _patch_ids(cwd: Path, git_args: Sequence[str], timeout: float = 120.0) -> dict[str, str]:+ """The commit of each patch id among the patches that ``git <git_args>`` prints, the first one per id.++ The patches stream straight into ``git patch-id``: a long history of a big, often rewritten file+ would not fit in memory, and several worktrees are checked at once.+ Diffs are bytes, not text: a binary or non-UTF-8 file would make a text decode fail.+ --verbatim keeps whitespace in the id, so a re-indent committed after the merge does not match it."""++ try:+ with subprocess.Popen(["git", *git_args], cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) as patch:+ try:+ ids = subprocess.run(+ ["git", "patch-id", "--verbatim"],+ cwd=cwd,+ stdin=patch.stdout,+ capture_output=True,+ timeout=timeout,+ )+ # Closed here too, so git exits on a broken pipe instead of blocking if patch-id stopped reading.+ patch.stdout.close()+ if patch.wait(timeout=timeout) != 0:+ return {}+ except subprocess.TimeoutExpired:+ # Leaving the with block waits for git without a timeout, so a stalled git is killed first.+ patch.kill()+ return {}+ except (FileNotFoundError, OSError):+ return {}+ commits: dict[str, str] = {}+ for line in ids.stdout.decode(errors="replace").splitlines():+ parts = line.split()+ if len(parts) == 2:+ commits.setdefault(parts[0], parts[1])+ return commits+++def _merged_pull_request(worktree: Path, head: str, base: str) -> str | None:+ """A pull request merged into *base* that contains HEAD. A stacked pull request merged into a+ branch that never reached *base* does not count."""++ gh = shutil.which("gh")+ if gh is None:+ return None+ # The search also matches pull requests that only mention the hash, so HEAD must be one of the commits.+ query = f'[.[] | select(any(.commits[]; .oid == "{head}")) | .number][0] // empty'+ try:+ result = subprocess.run(+ [+ gh,+ "pr",+ "list",+ "--state",+ "merged",+ "--base",+ base,+ "--search",+ head,+ "--json",+ "number,commits",+ "--jq",+ query,+ ],+ cwd=worktree,+ capture_output=True,+ text=True,+ timeout=20,+ )+ except (subprocess.TimeoutExpired, OSError):+ return None+ number = result.stdout.strip()+ return f"#{number}" if result.returncode == 0 and number.isdigit() else None++def _state_markers(wt: Worktree) -> str:parts: list[str] = []if wt.dirty:parts.append("uncommitted changes")if wt.unpushed:- parts.append(f"{wt.unpushed} unpushed commit(s)")+ parts.append(+ f"{wt.unpushed} unpushed commit(s)" + (f" merged as {wt.squash_merged}" if wt.squash_merged else "")+ )if wt.detached:parts.append("detached HEAD")if wt.state_unknown:@@ -1497,7 +1669,9 @@ def _listing_markers(wt: Worktree, mode: str) -> str:if mode == "full":if wt.dirty:parts.append("dirty")- if wt.unpushed:+ if wt.unpushed and wt.squash_merged:+ parts.append(f"merged as {wt.squash_merged}")+ elif wt.unpushed:parts.append(f"{wt.unpushed} unpushed")if wt.detached:parts.append("detached")