Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion heart/checks/worktree_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
reported missing because it isn't under the wt root. Claim values that are not
path-shaped (parked.md carries prose like ``worktree: none (…)``) are ignored.

Discovery under the wt root skips **hidden** directories: the wt root is a plain
directory other tools write into (a JetBrains project dir lands at
``<wt root>/.idea``), and such a dir is claimed by nobody, so it was reported as a
permanent, unfixable ORPHAN. Only *discovery* is filtered — an explicit claim on
a dotted path is still honoured, which is why ``.codex-worktrees`` claims keep
working.

Monitoring-only: surfaced on the dashboard/status, never a readiness reason.
``worktree_drift.sh`` is a thin shim over this module for tick.sh.
"""
Expand Down Expand Up @@ -105,7 +112,13 @@ def note(path: Path, name: str) -> None:

if wt_root.is_dir():
for entry in sorted(wt_root.iterdir()):
if entry.is_dir():
# Hidden dirs under the wt root are not task worktrees and never
# will be: the wt root is an ordinary directory a user's tools also
# write into (JetBrains puts its project dir at `<wt root>/.idea`).
# Discovery skips them; the claims leg below does NOT, so a task
# whose `worktree:` genuinely points at a dotted path is still
# tracked and never reported missing.
if entry.is_dir() and not entry.name.startswith("."):
note(entry, entry.name)
for c in active_claims + parked_claims:
note(Path(c["path"]), Path(c["path"]).name)
Expand Down
32 changes: 32 additions & 0 deletions tests/test_worktree_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ def test_unclaimed_dir_is_an_orphan(tmp_path):
assert [o["name"] for o in out["orphans"]] == ["mystery"]


def test_hidden_dirs_under_wt_root_are_not_orphans(tmp_path):
"""A `.idea` beside a real orphan yields one orphan, not two.

The wt root is an ordinary directory the user's tools also write into —
JetBrains puts its project dir at `<wt root>/.idea`. It is a directory, it
is claimed by neither registry, and before the hidden-name guard it was
reported as an ORPHAN forever: drift no one can ever clear.
"""
wt_root = tmp_path / "wt-root"
_git_repo(wt_root / "mystery" / "RepoA")
(wt_root / ".idea").mkdir()
(wt_root / ".idea" / "workspace.xml").write_text("<project/>")
out = wd.scan(wt_root, tmp_path / "active.md", tmp_path / "parked.md")
assert [o["name"] for o in out["orphans"]] == ["mystery"]
assert out["on_disk_count"] == 1


def test_hidden_dir_that_is_explicitly_claimed_is_still_tracked(tmp_path):
"""Only *discovery* is filtered — a claim on a dotted path still counts.

`.codex-worktrees/<task>` claims are real; the guard must not turn them
into permanent MISSING lines.
"""
claimed = tmp_path / ".codex-worktrees" / "some-task"
_git_repo(claimed / "RepoA")
active = _registry(tmp_path / "active.md", {"some-task": str(claimed)})
out = wd.scan(tmp_path / "wt-root", active, tmp_path / "parked.md")
assert out["missing"] == []
assert out["orphans"] == []
assert out["on_disk_count"] == 1


def test_prose_worktree_values_are_ignored(tmp_path):
parked = tmp_path / "parked.md"
parked.write_text("## shipped-task\n- worktree: none (pushed; local removed)\n")
Expand Down
Loading