From cce3a965b2e087a79aa0e10922701cd8ab7fe2af Mon Sep 17 00:00:00 2001 From: CyberKun <186809931+CyberKun@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:46:55 +0530 Subject: [PATCH] fix: normalize file paths in claim overlap checks --- ai_sync/collab.py | 17 +++++++++++++---- tests/test_collab.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/ai_sync/collab.py b/ai_sync/collab.py index 6eb57c2..f1aa04a 100644 --- a/ai_sync/collab.py +++ b/ai_sync/collab.py @@ -19,6 +19,7 @@ """ from __future__ import annotations +import posixpath import re from datetime import datetime, timedelta from pathlib import Path @@ -333,13 +334,21 @@ def load_claims(cfg: core.Config) -> list[dict]: return out +def _claim_path(path: str) -> str: + """Normalize project-relative claim spellings without accessing the filesystem.""" + return posixpath.normpath(str(path).replace("\\", "/")) + + def _overlaps(a: dict, b: dict) -> bool: if a["project"] != b["project"] or a["agent"] == b["agent"]: return False - fa, fb = set(a.get("files") or []), set(b.get("files") or []) - if not fa or not fb: # a claim on the whole repo overlaps everything + fa = {_claim_path(f) for f in a.get("files") or []} + fb = {_claim_path(f) for f in b.get("files") or []} + # Empty file lists and the project root claim the whole repository. + if not fa or not fb or "." in fa or "." in fb: return True - return bool(fa & fb) or any(x.rstrip("/") + "/" in y or y.rstrip("/") + "/" in x for x in fa for y in fb) + return bool(fa & fb) or any(y.startswith(x.rstrip("/") + "/") or + x.startswith(y.rstrip("/") + "/") for x in fa for y in fb) def find_overlaps(cfg: core.Config, agent: str, project: str, files) -> list[dict]: @@ -350,7 +359,7 @@ def find_overlaps(cfg: core.Config, agent: str, project: str, files) -> list[dic def claim(cfg: core.Config, *, agent: str, project: str, summary: str, files=(), ttl_min: int = CLAIM_TTL_MIN) -> dict: if not (agent and project and summary): raise ValueError("claim needs --agent, --project and --summary") - files = [str(f) for f in files] + files = [_claim_path(f) for f in files] with core.locked(cfg): claims = [c for c in load_claims(cfg) if not (c["agent"] == agent and c["project"] == project)] new = {"agent": agent, "project": project, "files": files, "summary": summary.strip(), diff --git a/tests/test_collab.py b/tests/test_collab.py index d37b80c..7f3126d 100644 --- a/tests/test_collab.py +++ b/tests/test_collab.py @@ -134,6 +134,20 @@ def test_claim_overlap_release(self): self.assertEqual(self.run_cli("release", "-a", "cursor")[0], 0) self.assertNotIn("cursor", self.run_cli("claims")[1]) + def test_claim_normalizes_paths_and_matches_legacy_claims(self): + self.run_cli("claim", "-a", "first", "-p", "webapp", "-f", "./src/../src/a.py", + "-m", "first edit") + claims = collab.load_claims(self.cfg) + self.assertEqual(claims[0]["files"], ["src/a.py"]) + # Existing saved claims may still contain the original spelling. + claims[0]["files"] = ["./src/a.py"] + core.save_json(self.cfg.state / "claims.json", claims) + overlaps = collab.find_overlaps(self.cfg, "second", "webapp", ["src/a.py"]) + self.assertEqual([claim["agent"] for claim in overlaps], ["first"]) + code, _ = self.run_cli("claim", "-a", "second", "-p", "webapp", "-f", "src/a.py", + "-m", "second edit") + self.assertEqual(code, 3) + def test_expired_claims_disappear(self): collab.claim(self.cfg, agent="cursor", project="p", summary="s", ttl_min=-1) self.assertEqual(collab.load_claims(self.cfg), []) @@ -284,3 +298,34 @@ def test_sentence_count_ignores_dots_in_names_and_semicolons(): assert _sentences("Keys are in ~/.config/awesome-factory/x.env and jobs/x_autopilot.py reads them; verified 2026-10-27.") == 1 assert _sentences("One. Two! Three? Four.") == 4 assert _sentences("第一句。第二句!第三句") == 3 + + +class ClaimPathTests(unittest.TestCase): + def test_equivalent_paths_and_directories_overlap(self): + for first, second in [ + ("./src/a.py", "src/a.py"), + ("src/../src/a.py", "src/a.py"), + ("src/", "./src/a.py"), + ("src", "src/"), + ("src\\a.py", "src/a.py"), + (".", "src/a.py"), + ]: + with self.subTest(first=first, second=second): + a = {"agent": "a", "project": "p", "files": [first]} + b = {"agent": "b", "project": "p", "files": [second]} + self.assertTrue(collab._overlaps(a, b)) + self.assertTrue(collab._overlaps(b, a)) + + def test_distinct_paths_and_claim_scopes_do_not_overlap(self): + a = {"agent": "a", "project": "p", "files": ["src/a.py"]} + for files, project, agent in [ + (["other/src/a.py/child"], "p", "b"), + (["src/a.py.bak"], "p", "b"), + (["src/A.py"], "p", "b"), + (["./src/a.py"], "other", "b"), + (["./src/a.py"], "p", "a"), + ]: + with self.subTest(files=files, project=project, agent=agent): + b = {"agent": agent, "project": project, "files": files} + self.assertFalse(collab._overlaps(a, b)) + self.assertFalse(collab._overlaps(b, a))