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
11 changes: 9 additions & 2 deletions heart/checks/manifest_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
or Path.home() / ".pyauto-heart"
)

_CHECK_LINE = re.compile(r"^check (?P<label>.+?): (?P<status>OK|\d+ mismatch\(es\))$")
_CHECK_LINE = re.compile(r"^check (?P<label>.+?): (?P<status>.+)$")
_CHECK_STATUS = re.compile(r"^(?P<status>OK|\d+ mismatch\(es\))(?: \([^\n]*\))?$")
_PROBLEM_LINE = re.compile(r"^\s+[✗x] (?P<problem>.+)$")


Expand All @@ -53,7 +54,13 @@ def parse_check_output(text: str) -> dict[str, dict[str, Any]]:
for line in text.splitlines():
m = _CHECK_LINE.match(line)
if m:
current = {"ok": m.group("status") == "OK", "problems": []}
status = _CHECK_STATUS.fullmatch(m.group("status"))
current = {
"ok": bool(status and status.group("status") == "OK"),
"problems": [] if status else [
f"Unrecognized check status: {m.group('status')}"
],
}
checks[m.group("label")] = current
continue
m = _PROBLEM_LINE.match(line)
Expand Down
21 changes: 13 additions & 8 deletions heart/checks/worktree_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def _dirty_file_count(repo: Path) -> int:
return len(res.stdout.strip().splitlines()) if res.stdout.strip() else 0


def _repositories(path: Path) -> list[Path]:
"""A task may be one linked checkout or a bundle of repository children."""
if (path / ".git").exists():
return [path]
return [
child for child in path.iterdir()
if child.is_dir() and (child / ".git").exists()
]


def scan(
wt_root: Path = PYAUTO_WT_ROOT,
active_md: Path = ACTIVE_MD,
Expand All @@ -114,10 +124,7 @@ def note(path: Path, name: str) -> None:
if key in seen or not path.is_dir():
return
seen.add(key)
has_content = any(
child.is_symlink() or (child.is_dir() and (child / ".git").exists())
for child in path.iterdir()
)
has_content = any(not repo.is_symlink() for repo in _repositories(path))
on_disk.append({"name": name, "path": key, "has_real_worktrees": has_content})

if wt_root.is_dir():
Expand All @@ -143,15 +150,13 @@ def note(path: Path, name: str) -> None:
# never of whether it happens to sit under the wt root.
missing = [c for c in active_claims if not Path(c["path"]).is_dir()]

# Task-branch dirt: real (non-symlink) worktree children only. Dirty
# Task-branch dirt: standalone checkouts or real worktree children. Dirty
# canonical checkouts reached via symlinks are deduped by resolved path.
dirty: list[dict[str, Any]] = []
canonical_seen: set[str] = set()
canonical_dirty: list[dict[str, Any]] = []
for entry in on_disk:
for child in Path(entry["path"]).iterdir():
if not (child.is_dir() and (child / ".git").exists()):
continue
for child in _repositories(Path(entry["path"])):
if child.is_symlink():
target = str(child.resolve())
if target in canonical_seen:
Expand Down
25 changes: 25 additions & 0 deletions tests/test_manifest_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ def test_parse_garbage_yields_nothing():
assert md.parse_check_output("Traceback (most recent call last):\n boom\n") == {}


def test_coverage_suffix_keeps_hook_failures_on_their_own_check():
checks = md.parse_check_output(
"check CLAUDE.md pointers: OK\n"
"check generated hooks: 1 mismatch(es) (45 of 45 checked out, 1 excluded)\n"
" ✗ RepoA: session hook differs\n"
"check generated Codex hooks: OK (46 of 46 checked out)\n"
)
assert checks["CLAUDE.md pointers"] == {"ok": True, "problems": []}
assert checks["generated hooks"] == {
"ok": False, "problems": ["RepoA: session hook differs"]
}
assert checks["generated Codex hooks"] == {"ok": True, "problems": []}


def test_unknown_check_status_is_not_attributed_to_previous_surface():
checks = md.parse_check_output(
"check origins: OK\n"
"check generated hooks: unavailable\n"
" ✗ could not inspect hooks\n"
)
assert checks["origins"] == {"ok": True, "problems": []}
assert checks["generated hooks"]["ok"] is False
assert "could not inspect hooks" in checks["generated hooks"]["problems"]


def _run_with_fake_script(tmp_path, monkeypatch, script_body: str | None):
"""Run md.run() against a fake workspace root; None = no script on disk."""
if script_body is not None:
Expand Down
33 changes: 33 additions & 0 deletions tests/test_worktree_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@ def test_real_worktree_dirt_still_attributed(tmp_path):
assert out["canonical_dirty"] == []


def test_standalone_linked_worktree_is_real_and_reports_its_dirt(tmp_path):
canonical = _git_repo(tmp_path / "canonical")
subprocess.run(
["git", "-C", str(canonical), "-c", "user.name=Test", "-c",
"user.email=test@example.org", "commit", "--allow-empty", "-qm", "base"],
check=True,
)
wt_root = tmp_path / "wt-root"
standalone = wt_root / "standalone"
subprocess.run(
["git", "-C", str(canonical), "worktree", "add", "-qb", "task", str(standalone)],
check=True,
)
(standalone / "uncommitted.txt").write_text("preserve me")
assert (standalone / ".git").is_file()
out = wd.scan(wt_root, tmp_path / "active.md", tmp_path / "parked.md")
assert out["orphans"][0]["has_real_worktrees"] is True
assert out["dirty"] == [
{"worktree": "standalone", "repo": "standalone", "dirty_files": 1}
]


def test_symlink_only_bundle_does_not_claim_real_worktrees(tmp_path):
canonical = _git_repo(tmp_path / "canonical", dirty=True)
bundle = tmp_path / "wt-root" / "links-only"
bundle.mkdir(parents=True)
(bundle / "RepoA").symlink_to(canonical)
out = wd.scan(bundle.parent, tmp_path / "active.md", tmp_path / "parked.md")
assert out["orphans"][0]["has_real_worktrees"] is False
assert out["dirty"] == []
assert out["canonical_dirty"] == [{"repo": "RepoA", "dirty_files": 1}]


def test_parked_worktree_is_not_an_orphan(tmp_path):
wt_root = tmp_path / "wt-root"
_git_repo(wt_root / "parked-task" / "RepoA")
Expand Down
Loading