← Back to Pull requests

fix(agt-policies): refuse malformed governance input in the migrator instead of crashing or silently dropping it #4377

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 20, 2026 08:38 UTC
Author

Related Issue


Part of #3137. The findings this PR does not fix are tracked there.

Problem & Solution


Issue #3137 lists 36 findings from an audit of the agt-policies package. I re-audited every one against current main (51d7dede):

  • 15 were already fixed by earlier PRs.

  • 10 are obsolete: the code they describe was removed in #3444 and #3451, and evaluation now lives in the agent-control-spec crate.

  • 1 (R1) moved into the ACS Python SDK run_sync; it is out of scope here and noted for a follow-up.

  • 10 remained in the agt migrate v4-to-v5 path. This PR fixes them.
  • | Finding | Original file | Status |
    |---|---|---|
    | B1 | bridge.py | already fixed |
    | B2 | bridge.py | already fixed |
    | B3 | bridge.py | already fixed |
    | R1 | runtime.py | moved (ACS Python SDK run_sync) |
    | R2 | runtime.py | obsolete |
    | R3 | runtime.py | obsolete |
    | R4 | runtime.py | obsolete |
    | S1 | snapshot.py | already fixed |
    | S2 | snapshot.py | obsolete |
    | S3 | snapshot.py | already fixed |
    | S4 | snapshot.py | already fixed |
    | M1 | merge.py | already fixed |
    | M2 | merge.py | fixed here |
    | BU1 | build.py | already fixed |
    | BU2 | build.py | fixed here |
    | BU3 | build.py | already fixed |
    | BU4 | build.py | already fixed |
    | BU5 | build.py | fixed here |
    | BU6 | build.py | already fixed |
    | D1 | discover.py | fixed here |
    | D2 | discover.py | fixed here |
    | D3 | discover.py | fixed here |
    | SC1 | scope.py | already fixed |
    | SC2 | scope.py | fixed here |
    | SC3 | scope.py | fixed here |
    | O1 | opa_runner.py | obsolete |
    | O2 | opa_runner.py | obsolete |
    | O3 | opa_runner.py | obsolete |
    | O4 | opa_runner.py | obsolete |
    | O5 | opa_runner.py | obsolete |
    | O6 | opa_runner.py | obsolete |
    | MG1 | migrate.py | already fixed |
    | MG2 | migrate.py | already fixed |
    | MG3 | migrate.py | fixed here |
    | MG4 | migrate.py | already fixed |
    | MG5 | migrate.py | fixed here |

    What changes for a user of the migrator:

  • A rule the generated Rego cannot express exactly is refused with a ResolutionError that names the rule. This covers compound and/or/not conditions, a rule without a condition, an unsupported operator, an invalid field path, a value of the wrong shape for its operator, a value with no JSON spelling (a YAML date, !!binary, .nan), a priority that is not a finite number, a scope that is present but blank or not a string, and a regex rule when no RE2 validator is available. Before, each of these either rendered an always-matching fail-closed deny (so every lower-priority rule became dead code and the bundle denied everything) or crashed the CLI with a bare TypeError or AttributeError traceback.

  • A rules key that is not a list is refused instead of raising inside the sort.

  • Chain discovery turns OSError from Path.resolve() and from stat calls during the walk into ResolutionError naming the path. A non-existent action_path is refused instead of being walked as a directory. filter_by_scope with a path outside the root raises ResolutionError, not ValueError.

  • --write runs a preflight pass: a policies path that exists but is not a writable directory (a regular file, a broken symlink, a directory without write permission) is recorded as a refusal before any governance file moves, instead of crashing with FileExistsError or PermissionError after the chains have moved. A staging-directory failure is recorded the same way.

  • --write-report creates the parent directory and reports any write error (including a NUL byte in the path) on stderr. It no longer changes the exit code once the migration itself has succeeded.
  • The migration refuses rather than guesses. Nothing is coerced.

    Impact on Your Work


    Running agt migrate v4-to-v5 on a project whose governance files hold any of the inputs above either produced a traceback with no report or wrote a bundle that denied every action. Both outcomes hid the real problem. With this change the CLI prints which rule or path it could not translate and exits cleanly, so the user can fix the source file and rerun.

    Timeline


    None

    Alternatives Considered


    Coerce the input instead of refusing it (for example, treat a null priority as 0, treat a blank scope as the whole tree, or drop a compound condition). Rejected: the migrator must not guess at policy intent, and a silently widened or narrowed rule is worse than a clear refusal.

    Type of Change


  • Bug fix (non-breaking change that fixes an issue)

  • New feature (non-breaking change that adds functionality)

  • Breaking change (fix or feature that would cause existing functionality to change)

  • Documentation update

  • Maintenance (dependency updates, CI/CD, refactoring)

  • Security fix
  • Package(s) Affected


    Core & runtime:
  • agent-governance-toolkit-core

  • agent-primitives

  • agent-os

  • agent-mesh

  • agent-runtime

  • agent-sre

  • agent-compliance
  • Governance & security:

  • agent-mcp-governance

  • agent-rag-governance

  • agent-sandbox

  • agent-discovery

  • agt-policies

  • policy-engine
  • Platform & tooling:

  • agent-hypervisor

  • agent-lightning

  • agent-marketplace

  • agent-governance-toolkit-cli

  • agent-governance-toolkit-integrations

  • agent-governance-toolkit-protocols

  • agentmesh-integrations (framework integrations)
  • CLI plugins:

  • agent-governance CLI plugins (copilot-cli / claude-code / opencode / antigravity-cli)
  • Shared / other:

  • schemas

  • action (GitHub Action)

  • examples

  • docs / root
  • Testing

    Unit Testing


    New and changed tests in agent-governance-python/agt-policies/tests/test_migrate.py (7) and tests/test_migrate_resolution.py (42). They cover each refusal above (compound conditions, unsupported operators, invalid fields, non-JSON values, bad and non-finite priorities including 10400, blank and non-string scope, non-list rules, missing RE2 validator), the discovery OSError paths and NUL bytes, the --write preflight (regular file, broken symlink, unwritable directory), and --write-report failures leaving the exit code alone.

    Manual Testing


    Environment: venv /tmp/agt-3137-venv2 with agent-control-specification 0.4.0b0 built from policy-engine/sdk/python, PYTHONPATH=src from agent-governance-python/agt-policies.

    Full agt-policies suite on this branch at 3c2769e6 (pytest -o addopts="" to get the summary line the repo's -q addopts suppress):

    297 passed, 17 skipped in 4.79s
    

    tests/test_migrate.py and tests/test_migrate_resolution.py alone:

    194 passed in 2.36s
    

    The 17 skips are the pre-existing agent_os import and cedar-dispatcher skips. The new root-only skip did not fire; test_write_refuses_when_policies_dir_is_not_writable ran and passed.

    To confirm the tests detect the defects, I copied the branch's two test files into a throwaway worktree of origin/main (51d7dede):

    49 failed, 145 passed in 2.65s
    

    All 49 failures are tests this branch added or changed. The same copy into a worktree of 134334ce (the state before the last two commits) gave:

    12 failed, 182 passed in 2.56s
    

    The 12 are exactly the tests added in 4e3ef197 and 3c2769e6 (broken symlink, not writable, write-report NUL, two int-too-large-for-float, six non-list rules, discover NUL). test_merge_missing_rules_key_means_no_rules passes on all three states, as intended. The two 10400 tests also pass on origin/main because main has no finiteness check at all, so the "12 of 13 fail on the old source" claim holds relative to 134334ce, not relative to origin/main.

    CI checks run locally: ruff check src/ --select E,F,W --ignore E501, scripts/check_license_headers.py on new files, and the cspell added-lines check. All clean.

    Checklist


  • I have linked a related issue above, or completed "Problem & Solution", "Impact on Your Work", and "Alternatives Considered"

  • My code follows the project style guidelines (ruff check)

  • I have added tests that prove my fix/feature works

  • All new and existing tests pass (pytest)

  • I have updated documentation as needed

  • I have signed the Microsoft CLA
  • Attribution & Prior Art


  • This contribution does not contain code copied or derived from other projects without attribution

  • Any external projects that inspired this design are credited in code comments or documentation

  • If this PR implements functionality similar to an existing open-source project, I have listed it below
  • Prior art / related projects (if any):

    AI Assistance


  • I can explain every meaningful change in this PR: what it does, why, and what tradeoffs were considered

  • I have run tests and verification appropriate for this change

  • No part of this PR was autonomously submitted by an AI agent without my review

  • I have not used AI to generate review comments on others' PRs
  • If AI tools materially shaped this change, briefly note what was used:
    Claude Code re-audited the findings and drafted the fixes and tests under my direction; an independent verification pass re-ran the tests on main and on the branch and probed the original defects with adversarial inputs, and I reviewed the result.

    IP, Patents, and Licensing


  • This contribution does not implement patent-pending or patent-encumbered techniques

  • This contribution does not require an NDA or licensing agreement to understand or use

  • Any AI tools used have terms compatible with the MIT License
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: prayagupa Review duration: 48191m
    All Vouch review confidence checks passed
    Residual risk 0.33 = Change Risk (0.59) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 650 lines diff across 8 files
    0.59
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #4377"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 59% (MEDIUM) · Higher = larger blast radius
    59%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 32.6% · Safe to merge
    32.6%