| #!/usr/bin/env magpie-python | |
| # SPDX-License-Identifier: Apache-2.0 | |
| # https://www.apache.org/licenses/LICENSE-2.0 | |
| """Sync and check the read-only policy resolver the two label-write ends share. | |
| Source of truth: `tools/vetted-ops/src/vetted_ops/policy_source.py`. | |
| Output targets: | |
| * `tools/agent-guard/src/agent_guard/policy_source.py` — the same bytes, with a | |
| note after the licence saying where they come from. The guard is loaded as | |
| `python3 .../agent_guard/__init__.py` with no installed package, so it cannot | |
| import `vetted_ops`; it carries the resolver instead, and a second | |
| implementation of the same authorisation rule is exactly what must not exist. | |
| * `plugins/magpie-agent-guard/tools/agent-guard/src/agent_guard/policy_source.py` | |
| and `plugins/magpie-vetted-ops/tools/vetted-ops/src/vetted_ops/policy_source.py` | |
| — what each plugin ships. `check-family-plugins.py` copies those trees | |
| verbatim, so these follow automatically; this tool checks them anyway, because | |
| the installed plugin is what actually runs. | |
| Usage: | |
| magpie-python tools/dev/sync-policy-source.py # Write the copies | |
| magpie-python tools/dev/sync-policy-source.py --check # Fail if any copy drifted | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import sys | |
| from pathlib import Path | |
| REPO_ROOT = Path(__file__).resolve().parent.parent.parent | |
| SOURCE = Path("tools/vetted-ops/src/vetted_ops/policy_source.py") | |
| GUARD_COPY = Path("tools/agent-guard/src/agent_guard/policy_source.py") | |
| BUNDLED_GUARD = Path("plugins/magpie-agent-guard/tools/agent-guard/src/agent_guard/policy_source.py") | |
| BUNDLED_VETTED = Path("plugins/magpie-vetted-ops/tools/vetted-ops/src/vetted_ops/policy_source.py") | |
| #: The end of the ASF licence block every Python file in the tree starts with. | |
| #: The note goes after it, so the licence stays the first thing in the file. | |
| ANCHOR = "# under the License.\n" | |
| #: Why the guard's copy exists, in the file a contributor opens to change it. | |
| NOTE = ( | |
| "# Generated from tools/vetted-ops/src/vetted_ops/policy_source.py by\n" | |
| "# tools/dev/sync-policy-source.py; edit that file, then run the tool.\n" | |
| "#\n" | |
| "# This file is the one implementation the vetted-ops write entry points and\n" | |
| "# agent-guard's rule 1b both resolve a label write's target with. It lives here\n" | |
| "# because the guard runs as a single file with no installed package, and a copy\n" | |
| "# someone edits by hand would drift from the dispatcher's answer.\n" | |
| ) | |
| def guard_copy(source: bytes) -> bytes: | |
| """The guard's copy of ``source``: the same bytes, with the note after the licence.""" | |
| text = source.decode("utf-8") | |
| index = text.index(ANCHOR) + len(ANCHOR) | |
| return (text[:index] + NOTE + "\n" + text[index:]).encode("utf-8") | |
| def expected() -> dict[Path, bytes]: | |
| """Every managed file, and the bytes it must hold.""" | |
| source = (REPO_ROOT / SOURCE).read_bytes() | |
| guard = guard_copy(source) | |
| return { | |
| Path(GUARD_COPY): guard, | |
| Path(BUNDLED_GUARD): guard, | |
| Path(BUNDLED_VETTED): source, | |
| } | |
| def main(argv: list[str] | None = None) -> int: | |
| parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) | |
| parser.add_argument("--check", action="store_true", help="report drift instead of writing") | |
| args = parser.parse_args(argv) | |
| managed = expected() | |
| drifted: list[Path] = [] | |
| for relative, wanted in managed.items(): | |
| target = REPO_ROOT / relative | |
| current = target.read_bytes() if target.is_file() else None | |
| if current == wanted: | |
| continue | |
| if args.check: | |
| drifted.append(relative) | |
| continue | |
| target.parent.mkdir(parents=True, exist_ok=True) | |
| target.write_bytes(wanted) | |
| print(f"{relative}: regenerated from {SOURCE}", file=sys.stderr) | |
| if drifted: | |
| for relative in drifted: | |
| print( | |
| f"sync-policy-source: {relative} does not match {SOURCE}; " | |
| "run magpie-python tools/dev/sync-policy-source.py", | |
| file=sys.stderr, | |
| ) | |
| return 1 | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) |