agent_team_harness.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Cross-tool Agent Team harness helpers.
  2. Provides deterministic helpers shared by Devin, Cursor, and Codex adapters.
  3. All functions are offline, do not access hardware, and do not start model APIs.
  4. """
  5. from __future__ import annotations
  6. import hashlib
  7. import json
  8. import subprocess
  9. from pathlib import Path
  10. from typing import Any
  11. def repo_root(cwd: Path | None = None) -> Path:
  12. cmd = ["git", "rev-parse", "--show-toplevel"]
  13. result = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True, check=True)
  14. return Path(result.stdout.strip()).resolve()
  15. def current_commit_full(cwd: Path | None = None) -> str:
  16. result = subprocess.run(
  17. ["git", "rev-parse", "HEAD"],
  18. cwd=cwd,
  19. capture_output=True,
  20. text=True,
  21. check=True,
  22. )
  23. return result.stdout.strip().lower()
  24. def sha256_file(path: Path) -> str:
  25. digest = hashlib.sha256()
  26. with path.open("rb") as stream:
  27. for chunk in iter(lambda: stream.read(1024 * 1024), b""):
  28. digest.update(chunk)
  29. return digest.hexdigest().lower()
  30. def load_team_contract(repo: Path | None = None) -> dict[str, Any]:
  31. repo = repo or repo_root()
  32. path = repo / "agent-team" / "contract.json"
  33. return json.loads(path.read_text(encoding="utf-8-sig"))
  34. def is_protected_root(target: Path, protected: list[str]) -> bool:
  35. resolved = target.resolve()
  36. for root_str in protected:
  37. root = Path(root_str).resolve()
  38. try:
  39. resolved.relative_to(root)
  40. return True
  41. except ValueError:
  42. pass
  43. return False