feat: implement repoctl L1 core (models, schemas, validate, discover, context, init, adopt)
Adds the repository-stewardship Python package (src/stewardship) with: - JSON Schemas (Draft 2020-12) for RepositoryContract, KnowledgeBase, CatalogIndex, ContextBundle, BootstrapPlan, AdoptionAssessment/Plan/Report, user config and the repoctl-result envelope. - Safe YAML I/O, path confinement, deterministic clock and canonical-JSON hashing. - validate: schema, ID uniqueness, path safety, KDB references, freshness, secret-shaped fields, shell-string detection, and Markdown frontmatter/fence/link checks. - discover/CatalogIndex with deterministic sorting and duplicate-name detection. - describe (contract-only) and context (intent-scoped ContextBundle with budgeted materialization and --strict truncation). - init: plan-then-apply bootstrap with SHA-256 plan-hash approval, no fake domain artifacts, bundled Claude skills. - adopt: assess/plan/apply/verify overlay workflow per docs/adoption-runbook.md, preserving legacy content roots and KDB, with baseline-hash re-check and rollback. - argparse-based `repoctl` CLI with human/yaml/json output and spec exit codes. - pytest suite (39 tests) covering positive/negative fixtures, discovery, context, bootstrap and adoption (including LXD- and Antivirus-shaped legacy layouts). Managed with uv (.venv), ruff clean, mypy --strict clean. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
9a39b8a941
commit
96d4d08230
@@ -0,0 +1,91 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from stewardship import adoption
|
||||
|
||||
|
||||
def test_assess_lxd_legacy_preserves_layout(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
result = adoption.assess(repo)
|
||||
assert result["repository"]["dirty"] is False
|
||||
assert result["classification"]["proposedType"] == "infrastructure-config"
|
||||
kdb_actions = {(entry["path"], entry["action"]) for entry in result["knowledge"]}
|
||||
assert ("kdb.yaml", "wrap") in kdb_actions
|
||||
root_paths = {cr["path"] for cr in result["proposedContentRoots"]}
|
||||
assert "profiles-source" in root_paths
|
||||
|
||||
|
||||
def test_plan_lxd_legacy_creates_contract_and_kdb_adapter_without_moving_profiles(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(
|
||||
repo, assessment, confirmed_name="infra-lxd", confirmed_owners=["infrastructure"], confirmed_lifecycle="production"
|
||||
)
|
||||
changes_by_path = {c["path"]: c for c in plan["changes"]}
|
||||
assert changes_by_path["ops/contract.yaml"]["action"] == "create"
|
||||
assert changes_by_path["kdb/kdb.yaml"]["action"] == "create"
|
||||
assert changes_by_path["kdb.yaml"]["action"] == "preserve"
|
||||
assert "profiles-source/**" in plan["forbiddenChanges"]
|
||||
assert "profiles/**" in plan["forbiddenChanges"]
|
||||
assert plan["planHash"].startswith("sha256:")
|
||||
|
||||
|
||||
def test_apply_does_not_touch_working_tree_before_approval(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
before = sorted(p.relative_to(repo).as_posix() for p in repo.rglob("*") if p.is_file())
|
||||
assessment = adoption.assess(repo)
|
||||
adoption.build_plan(repo, assessment, confirmed_name="infra-lxd")
|
||||
after = sorted(p.relative_to(repo).as_posix() for p in repo.rglob("*") if p.is_file())
|
||||
assert before == after
|
||||
|
||||
|
||||
def test_apply_rejects_stale_baseline(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="infra-lxd")
|
||||
plan["baselineRevision"] = "0" * 40
|
||||
with pytest.raises(ValueError):
|
||||
adoption.apply_plan(repo, plan, plan["planHash"])
|
||||
assert not (repo / "ops" / "contract.yaml").exists()
|
||||
|
||||
|
||||
def test_apply_and_verify_lxd_legacy(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(
|
||||
repo, assessment, confirmed_name="infra-lxd", confirmed_owners=["infrastructure"], confirmed_lifecycle="production"
|
||||
)
|
||||
written = adoption.apply_plan(repo, plan, plan["planHash"])
|
||||
assert "ops/contract.yaml" in written
|
||||
assert (repo / "profiles-source" / "default.yaml").exists()
|
||||
assert (repo / "profiles" / "default.yaml").exists()
|
||||
assert (repo / "kdb.yaml").exists()
|
||||
|
||||
report = adoption.verify(repo, plan)
|
||||
assert "profiles-source" in report["preservedPaths"] or "kdb.yaml" in report["preservedPaths"]
|
||||
assert report["outcome"] in {"pass", "pass-with-warnings"}
|
||||
|
||||
|
||||
def test_assess_antivirus_legacy_detects_occupied_kdb_path(make_repo):
|
||||
repo = make_repo("adoption/antivirus-legacy")
|
||||
result = adoption.assess(repo)
|
||||
kdb_paths = {entry["path"] for entry in result["knowledge"]}
|
||||
assert "kdb/kdb.yaml" in kdb_paths
|
||||
|
||||
|
||||
def test_plan_antivirus_legacy_does_not_overwrite_existing_kdb(make_repo):
|
||||
repo = make_repo("adoption/antivirus-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="services-antivirus")
|
||||
changes_by_path = {c["path"]: c for c in plan["changes"]}
|
||||
assert changes_by_path["kdb/kdb.yaml"]["action"] == "preserve"
|
||||
|
||||
|
||||
def test_apply_preserves_existing_antivirus_kdb_bytes(make_repo):
|
||||
repo = make_repo("adoption/antivirus-legacy")
|
||||
original = (repo / "kdb" / "kdb.yaml").read_bytes()
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="services-antivirus")
|
||||
adoption.apply_plan(repo, plan, plan["planHash"])
|
||||
assert (repo / "kdb" / "kdb.yaml").read_bytes() == original
|
||||
Reference in New Issue
Block a user