feat: model repo standards and detect prefix collisions
Implement RMGR-WP-0004 T01 (flavor, required files, anti-patterns from canon) and T08 (prefix ownership registry plus uniqueness scan). rmgr conform and rmgr prefix-uniqueness are detection only.
This commit is contained in:
parent
106d7d5b2b
commit
04145a59d9
8 changed files with 640 additions and 4 deletions
|
|
@ -64,6 +64,17 @@ def main(argv: list[str] | None = None) -> int:
|
|||
|
||||
add_rapp_parser(sub)
|
||||
|
||||
p_conf = sub.add_parser("conform", help="Check a repository against flavor standards")
|
||||
p_conf.add_argument("--path", default=".", help="Repository checkout path")
|
||||
p_conf.add_argument("--slug", default=None)
|
||||
|
||||
p_pref = sub.add_parser(
|
||||
"prefix-uniqueness",
|
||||
help="Detect shared workplan prefixes and reused identifiers (ADR-007)",
|
||||
)
|
||||
p_pref.add_argument("--root", default=".", help="Fleet root or a single repository")
|
||||
p_pref.add_argument("--registry", default=None, help="Override prefix registry YAML")
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
if args.version or args.command in (None, "version"):
|
||||
|
|
@ -188,6 +199,21 @@ def main(argv: list[str] | None = None) -> int:
|
|||
print(json.dumps(result, indent=2))
|
||||
return 0 if result.get("ok") else 1
|
||||
|
||||
if args.command == "conform":
|
||||
from repo_manager.standards import check_repository
|
||||
|
||||
report = check_repository(Path(args.path), slug=args.slug)
|
||||
print(json.dumps(report.to_dict(), indent=2))
|
||||
return 0 if report.ok else 1
|
||||
|
||||
if args.command == "prefix-uniqueness":
|
||||
from repo_manager.prefix_registry import scan_prefixes
|
||||
|
||||
registry = Path(args.registry) if args.registry else None
|
||||
report = scan_prefixes(Path(args.root), registry_path=registry)
|
||||
print(json.dumps(report, indent=2))
|
||||
return 0 if report.get("ok") else 1
|
||||
|
||||
parser.print_help()
|
||||
return 0
|
||||
|
||||
|
|
|
|||
112
src/repo_manager/prefix_registry.py
Normal file
112
src/repo_manager/prefix_registry.py
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
"""Fleet workplan-prefix ownership and uniqueness (RMGR-WP-0004-T08)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import yaml
|
||||
|
||||
from repo_manager.standards import FLAVOR_MARKER_PREFIX, workplan_ids
|
||||
|
||||
DEFAULT_REGISTRY = Path(__file__).resolve().parents[2] / "config" / "workplan-prefix-registry.yaml"
|
||||
|
||||
|
||||
def load_registry(path: Path | None = None) -> dict[str, Any]:
|
||||
target = path or DEFAULT_REGISTRY
|
||||
data = yaml.safe_load(target.read_text(encoding="utf-8")) or {}
|
||||
if not isinstance(data, dict):
|
||||
return {}
|
||||
return data
|
||||
|
||||
|
||||
def _skip_dir(name: str) -> bool:
|
||||
return name.startswith(".") or name in {
|
||||
"node_modules",
|
||||
"__pycache__",
|
||||
".venv",
|
||||
"venv",
|
||||
"archive",
|
||||
}
|
||||
|
||||
|
||||
def iter_repo_roots(root: Path) -> list[Path]:
|
||||
root = root.resolve()
|
||||
if (root / "workplans").is_dir() and (root / ".git").exists():
|
||||
return [root]
|
||||
found: list[Path] = []
|
||||
if not root.is_dir():
|
||||
return found
|
||||
for child in sorted(root.iterdir()):
|
||||
if not child.is_dir() or _skip_dir(child.name):
|
||||
continue
|
||||
if (child / "workplans").is_dir():
|
||||
found.append(child)
|
||||
return found
|
||||
|
||||
|
||||
def scan_prefixes(root: Path, *, registry_path: Path | None = None) -> dict[str, Any]:
|
||||
"""Detect prefix sharing, identifier reuse, flavor-derived prefixes, number reuse.
|
||||
|
||||
Detection only. Does not rewrite files or remediations.
|
||||
"""
|
||||
registry = load_registry(registry_path)
|
||||
owners = {str(k): str(v) for k, v in (registry.get("owners") or {}).items()}
|
||||
retired = {str(item.get("prefix")) for item in registry.get("retired") or [] if item.get("prefix")}
|
||||
|
||||
by_prefix: dict[str, set[str]] = defaultdict(set)
|
||||
by_id: dict[str, list[tuple[str, str]]] = defaultdict(list)
|
||||
numbers: dict[tuple[str, str], list[str]] = defaultdict(list)
|
||||
|
||||
repos_scanned = []
|
||||
for repo in iter_repo_roots(root):
|
||||
repos_scanned.append(repo.name)
|
||||
for rel, prefix, number in workplan_ids(repo):
|
||||
by_prefix[prefix].add(repo.name)
|
||||
ident = f"{prefix}-{number}"
|
||||
by_id[ident].append((repo.name, rel))
|
||||
numbers[(repo.name, prefix)].append(number)
|
||||
|
||||
shared = {
|
||||
prefix: sorted(repos)
|
||||
for prefix, repos in sorted(by_prefix.items())
|
||||
if len(repos) > 1
|
||||
}
|
||||
reused = {
|
||||
ident: [{"repo": repo, "path": path} for repo, path in entries]
|
||||
for ident, entries in sorted(by_id.items())
|
||||
if len({repo for repo, _path in entries}) > 1 or len(entries) > 1
|
||||
}
|
||||
flavor_derived = sorted(repo for repo in by_prefix.get(FLAVOR_MARKER_PREFIX, []))
|
||||
retired_in_use = {
|
||||
prefix: sorted(repos)
|
||||
for prefix, repos in by_prefix.items()
|
||||
if prefix in retired
|
||||
}
|
||||
owner_mismatch = {
|
||||
prefix: {"declared_owner": owners[prefix], "seen_in": sorted(repos)}
|
||||
for prefix, repos in by_prefix.items()
|
||||
if prefix in owners and any(repo != owners[prefix] for repo in repos)
|
||||
}
|
||||
|
||||
reused_numbers = []
|
||||
for (repo, prefix), nums in numbers.items():
|
||||
seen: set[str] = set()
|
||||
for num in nums:
|
||||
if num in seen:
|
||||
reused_numbers.append({"repo": repo, "prefix": prefix, "number": num})
|
||||
seen.add(num)
|
||||
|
||||
return {
|
||||
"ok": not shared and not flavor_derived and not reused_numbers and not reused,
|
||||
"root": str(root.resolve()),
|
||||
"repos_scanned": repos_scanned,
|
||||
"shared_prefixes": shared,
|
||||
"reused_identifiers": reused,
|
||||
"flavor_derived_prefix": flavor_derived,
|
||||
"retired_in_use": retired_in_use,
|
||||
"owner_mismatch": owner_mismatch,
|
||||
"reused_numbers_in_repo": reused_numbers,
|
||||
"detection_only": True,
|
||||
}
|
||||
279
src/repo_manager/standards.py
Normal file
279
src/repo_manager/standards.py
Normal file
|
|
@ -0,0 +1,279 @@
|
|||
"""Repository standards as Repo Manager implements Custodian canon.
|
||||
|
||||
Canon remains authoritative. This module does not fork the rules; it names
|
||||
the signals, required files, and anti-patterns the canon already states.
|
||||
|
||||
Sources:
|
||||
- the-custodian/canon/standards/project-repository-flavor_v0.1.md
|
||||
- the-custodian/canon/standards/repo-classification-standard_v1.0.md
|
||||
- ADR-007 decision 1 (prefix from repo/project identity, never flavor)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any, Literal
|
||||
|
||||
import yaml
|
||||
|
||||
from repo_manager.observe import load_classification
|
||||
from repo_manager.parse.workplan import parse_frontmatter
|
||||
|
||||
CANON_FLAVOR = "the-custodian/canon/standards/project-repository-flavor_v0.1.md"
|
||||
CANON_CLASSIFICATION = "the-custodian/canon/standards/repo-classification-standard_v1.0.md"
|
||||
|
||||
DURABLE_FLAVORS = ("experimental", "research", "tooling", "product", "business")
|
||||
CATEGORIES = (*DURABLE_FLAVORS, "project")
|
||||
FLAVOR_MARKER_PREFIX = "PRJ-WP"
|
||||
WP_ID_RE = re.compile(r"^([A-Z][A-Z0-9]*)-WP-(\d{4})(?:-T\d{2})?$")
|
||||
GOAL_SECTIONS = ("outcome", "invariants", "success gates", "project retirement")
|
||||
|
||||
Severity = Literal["missing", "contradictory", "warning"]
|
||||
|
||||
|
||||
@dataclass
|
||||
class Finding:
|
||||
code: str
|
||||
severity: Severity
|
||||
path: str
|
||||
message: str
|
||||
canon: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class FlavorResolution:
|
||||
flavor: str
|
||||
prj_layout: bool
|
||||
signals: dict[str, str | None]
|
||||
disagreements: list[str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConformanceReport:
|
||||
slug: str
|
||||
flavor: FlavorResolution
|
||||
findings: list[Finding] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def ok(self) -> bool:
|
||||
return not any(f.severity in {"missing", "contradictory"} for f in self.findings)
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"ok": self.ok,
|
||||
"slug": self.slug,
|
||||
"flavor": self.flavor.flavor,
|
||||
"prj_layout": self.flavor.prj_layout,
|
||||
"signals": self.flavor.signals,
|
||||
"disagreements": self.flavor.disagreements,
|
||||
"findings": [f.__dict__ for f in self.findings],
|
||||
"canon": [CANON_FLAVOR, CANON_CLASSIFICATION],
|
||||
}
|
||||
|
||||
|
||||
def _slug(repo_root: Path) -> str:
|
||||
return re.sub(r"[^a-z0-9]+", "-", repo_root.name.lower()).strip("-") or "repo"
|
||||
|
||||
|
||||
def _goal_flavor(repo_root: Path) -> str | None:
|
||||
path = repo_root / "GOAL.md"
|
||||
if not path.is_file():
|
||||
return None
|
||||
fm, _ = parse_frontmatter(path.read_text(encoding="utf-8"))
|
||||
raw = fm.get("repo_flavor")
|
||||
return str(raw).strip().lower() if raw else None
|
||||
|
||||
|
||||
def resolve_flavor(repo_root: Path, *, slug: str | None = None) -> FlavorResolution:
|
||||
"""Precedence: classification category, GOAL.md repo_flavor, slug prefix."""
|
||||
slug = slug or _slug(repo_root)
|
||||
classification = load_classification(repo_root) or {}
|
||||
category = classification.get("category")
|
||||
category = str(category).strip().lower() if category else None
|
||||
goal_flavor = _goal_flavor(repo_root)
|
||||
slug_prj = slug.startswith("prj-")
|
||||
signals = {
|
||||
"classification.category": category,
|
||||
"GOAL.md.repo_flavor": goal_flavor,
|
||||
"slug_prefix": "prj-" if slug_prj else None,
|
||||
}
|
||||
|
||||
if category in CATEGORIES:
|
||||
flavor = category
|
||||
elif goal_flavor in CATEGORIES:
|
||||
flavor = goal_flavor
|
||||
elif slug_prj:
|
||||
flavor = "project"
|
||||
else:
|
||||
flavor = "tooling"
|
||||
|
||||
disagreements: list[str] = []
|
||||
if category and goal_flavor and category != goal_flavor:
|
||||
disagreements.append(
|
||||
f"classification.category={category!r} disagrees with GOAL.md repo_flavor={goal_flavor!r}"
|
||||
)
|
||||
if slug_prj and flavor != "project":
|
||||
disagreements.append(f"slug is prj-* but resolved flavor is {flavor!r}")
|
||||
if category == "project" and not slug_prj and goal_flavor != "project":
|
||||
disagreements.append(
|
||||
"category is project but slug is not prj-*; not every category:project is a prj- repo"
|
||||
)
|
||||
|
||||
prj_layout = slug_prj or goal_flavor == "project"
|
||||
return FlavorResolution(
|
||||
flavor=flavor,
|
||||
prj_layout=prj_layout,
|
||||
signals=signals,
|
||||
disagreements=disagreements,
|
||||
)
|
||||
|
||||
|
||||
def required_files(flavor: FlavorResolution) -> tuple[str, ...]:
|
||||
common = ("README.md", "SCOPE.md", "AGENTS.md", ".repo-classification.yaml", "workplans")
|
||||
if flavor.prj_layout:
|
||||
return ("GOAL.md", *common)
|
||||
return ("INTENT.md", *common)
|
||||
|
||||
|
||||
def purpose_document(flavor: FlavorResolution) -> str:
|
||||
return "GOAL.md" if flavor.prj_layout else "INTENT.md"
|
||||
|
||||
|
||||
def expected_workplan_prefix(repo_root: Path, *, slug: str | None = None) -> str | None:
|
||||
"""Prefix must come from repo/project identity, never the flavor marker."""
|
||||
slug = slug or _slug(repo_root)
|
||||
if slug.startswith("prj-"):
|
||||
stem = slug.removeprefix("prj-")
|
||||
letters = re.sub(r"[^a-z0-9]", "", stem)
|
||||
if letters:
|
||||
return letters[:8].upper() + "-WP"
|
||||
return None
|
||||
compact = re.sub(r"[^a-z0-9]", "", slug)
|
||||
if not compact:
|
||||
return None
|
||||
return compact[:8].upper() + "-WP"
|
||||
|
||||
|
||||
def workplan_ids(repo_root: Path) -> list[tuple[str, str, str]]:
|
||||
"""Return (path, prefix, number) for each PREFIX-WP-NNNN workplan id."""
|
||||
found: list[tuple[str, str, str]] = []
|
||||
wp_dir = repo_root / "workplans"
|
||||
if not wp_dir.is_dir():
|
||||
return found
|
||||
for path in sorted(wp_dir.rglob("*.md")):
|
||||
if path.name.startswith("ADHOC-"):
|
||||
continue
|
||||
fm, _ = parse_frontmatter(path.read_text(encoding="utf-8"))
|
||||
ident = fm.get("id")
|
||||
if ident is None:
|
||||
continue
|
||||
match = WP_ID_RE.match(str(ident).strip())
|
||||
if not match:
|
||||
continue
|
||||
found.append((str(path.relative_to(repo_root)), match.group(1) + "-WP", match.group(2)))
|
||||
return found
|
||||
|
||||
|
||||
def check_repository(repo_root: Path, *, slug: str | None = None) -> ConformanceReport:
|
||||
repo_root = repo_root.resolve()
|
||||
slug = slug or _slug(repo_root)
|
||||
flavor = resolve_flavor(repo_root, slug=slug)
|
||||
report = ConformanceReport(slug=slug, flavor=flavor)
|
||||
|
||||
for note in flavor.disagreements:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
code="flavor-signal-disagreement",
|
||||
severity="warning",
|
||||
path=".repo-classification.yaml",
|
||||
message=note,
|
||||
canon=CANON_CLASSIFICATION,
|
||||
)
|
||||
)
|
||||
|
||||
for rel in required_files(flavor):
|
||||
target = repo_root / rel
|
||||
present = target.is_dir() if rel.endswith("/") or rel == "workplans" else target.is_file()
|
||||
if not present:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
code="required-file-missing",
|
||||
severity="missing",
|
||||
path=rel,
|
||||
message=f"{rel} is required for flavor {flavor.flavor}",
|
||||
canon=CANON_FLAVOR if flavor.prj_layout else CANON_CLASSIFICATION,
|
||||
)
|
||||
)
|
||||
|
||||
has_intent = (repo_root / "INTENT.md").is_file()
|
||||
has_goal = (repo_root / "GOAL.md").is_file()
|
||||
if flavor.prj_layout and has_intent and has_goal:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
code="intent-and-goal",
|
||||
severity="contradictory",
|
||||
path="INTENT.md",
|
||||
message="prj- flavor forbids shipping both INTENT.md and GOAL.md",
|
||||
canon=CANON_FLAVOR,
|
||||
)
|
||||
)
|
||||
if flavor.prj_layout and has_intent and not has_goal:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
code="intent-instead-of-goal",
|
||||
severity="contradictory",
|
||||
path="INTENT.md",
|
||||
message="prj- flavor requires GOAL.md and must not use INTENT.md as the purpose document",
|
||||
canon=CANON_FLAVOR,
|
||||
)
|
||||
)
|
||||
if not flavor.prj_layout and has_intent and has_goal:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
code="intent-and-goal",
|
||||
severity="warning",
|
||||
path="GOAL.md",
|
||||
message="durable flavors use INTENT.md; GOAL.md is the project-flavor purpose document",
|
||||
canon=CANON_FLAVOR,
|
||||
)
|
||||
)
|
||||
|
||||
if flavor.prj_layout and has_goal:
|
||||
body = (repo_root / "GOAL.md").read_text(encoding="utf-8").lower()
|
||||
missing = [name for name in GOAL_SECTIONS if name not in body]
|
||||
if missing:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
code="goal-sections-missing",
|
||||
severity="missing",
|
||||
path="GOAL.md",
|
||||
message="GOAL.md must include " + ", ".join(GOAL_SECTIONS) + f"; missing {missing}",
|
||||
canon=CANON_FLAVOR,
|
||||
)
|
||||
)
|
||||
|
||||
ids = workplan_ids(repo_root)
|
||||
prefixes = {prefix for _path, prefix, _num in ids}
|
||||
if FLAVOR_MARKER_PREFIX in prefixes:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
code="flavor-derived-prefix",
|
||||
severity="contradictory",
|
||||
path="workplans/",
|
||||
message="PRJ-WP- is derived from the flavor marker; prefixes must come from project identity",
|
||||
canon=CANON_FLAVOR,
|
||||
)
|
||||
)
|
||||
if len(prefixes) > 1:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
code="multiple-prefixes",
|
||||
severity="warning",
|
||||
path="workplans/",
|
||||
message=f"repository uses more than one workplan prefix: {sorted(prefixes)}",
|
||||
canon="ADR-007",
|
||||
)
|
||||
)
|
||||
return report
|
||||
Loading…
Add table
Add a link
Reference in a new issue