From 32503c73d86a3c4e4a5f39dff8a82fab7832c310 Mon Sep 17 00:00:00 2001 From: AryanSingh2029 Date: Tue, 17 Nov 2026 01:29:38 +0530 Subject: [PATCH] fix(claim): match claimed paths by segment prefix, not substring (#31) _overlaps compared claimed paths with a substring test, so a directory whose name merely contains another claim's name (docs vs mydocs/x.md, src vs lib/src/a.py) raised a false overlap and told an agent to stop and coordinate with someone working elsewhere. Compare path prefixes by whole segment instead: a claim x covers y when y equals x or starts with x.rstrip('/') + '/', checked both directions, via a small _covers helper. Scope is the substring-vs-prefix bug only; path normalisation (#32) is left untouched. --- ai_sync/collab.py | 16 +++++++++++++++- tests/test_collab.py | 11 +++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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), [])