diff --git a/ai_sync/collab.py b/ai_sync/collab.py index 6eb57c2..65539dd 100644 --- a/ai_sync/collab.py +++ b/ai_sync/collab.py @@ -333,13 +333,27 @@ def load_claims(cfg: core.Config) -> list[dict]: return out +def _covers(parent: str, child: str) -> bool: + """True when a directory claim on ``parent`` contains the path ``child``. + + A claim ``x`` covers ``y`` when ``y`` equals ``x`` or lies beneath it, + compared by whole path segment. The earlier substring test wrongly flagged + e.g. ``docs`` against ``mydocs/x.md``, because ``"docs/" in "mydocs/x.md"`` + is true for a substring but the two are in different directories. + """ + parent, child = parent.rstrip("/"), child.rstrip("/") + return child == parent or child.startswith(parent + "/") + + 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 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( + _covers(x, y) or _covers(y, x) for x in fa for y in fb + ) def find_overlaps(cfg: core.Config, agent: str, project: str, files) -> list[dict]: diff --git a/tests/test_collab.py b/tests/test_collab.py index d37b80c..32a876d 100644 --- a/tests/test_collab.py +++ b/tests/test_collab.py @@ -134,6 +134,17 @@ 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_overlap_matches_path_prefix_not_substring(self): + # Issue #31: a claimed directory must match a whole path segment, not a + # substring. ``docs`` must not overlap ``mydocs/x.md``, and ``src`` must + # not overlap ``lib/src/a.py``. + collab.claim(self.cfg, agent="cursor", project="webapp", summary="s", files=["docs", "src"]) + self.assertEqual(collab.find_overlaps(self.cfg, "trae", "webapp", ["mydocs/x.md"]), []) + self.assertEqual(collab.find_overlaps(self.cfg, "trae", "webapp", ["lib/src/a.py"]), []) + # A real child path, and an exact claim, still overlap. + self.assertTrue(collab.find_overlaps(self.cfg, "trae", "webapp", ["docs/guide.md"])) + self.assertTrue(collab.find_overlaps(self.cfg, "trae", "webapp", ["src"])) + 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), [])