Skip to content
Open
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
16 changes: 15 additions & 1 deletion ai_sync/collab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_collab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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), [])
Expand Down