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
17 changes: 13 additions & 4 deletions ai_sync/collab.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""
from __future__ import annotations

import posixpath
import re
from datetime import datetime, timedelta
from pathlib import Path
Expand Down Expand Up @@ -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]:
Expand All @@ -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(),
Expand Down
45 changes: 45 additions & 0 deletions tests/test_collab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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), [])
Expand Down Expand Up @@ -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))