fix(agt-policies): refuse malformed governance input in the migrator instead of crashing or silently dropping it #4377
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
prayagupa
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):run_sync; it is out of scope here and noted for a follow-up.agt migrate v4-to-v5path. 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:
ResolutionErrorthat names the rule. This covers compoundand/or/notconditions, 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, ascopethat 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 bareTypeErrororAttributeErrortraceback.ruleskey that is not a list is refused instead of raising inside the sort.OSErrorfromPath.resolve()and from stat calls during the walk intoResolutionErrornaming the path. A non-existentaction_pathis refused instead of being walked as a directory.filter_by_scopewith a path outside the root raisesResolutionError, notValueError.--writeruns a preflight pass: apoliciespath 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 withFileExistsErrororPermissionErrorafter the chains have moved. A staging-directory failure is recorded the same way.--write-reportcreates 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-v5on 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
Package(s) Affected
Core & runtime:
Governance & security:
Platform & tooling:
CLI plugins:
Shared / other:
Testing
Unit Testing
New and changed tests in
agent-governance-python/agt-policies/tests/test_migrate.py(7) andtests/test_migrate_resolution.py(42). They cover each refusal above (compound conditions, unsupported operators, invalid fields, non-JSON values, bad and non-finite priorities including10400, blank and non-string scope, non-listrules, missing RE2 validator), the discoveryOSErrorpaths and NUL bytes, the--writepreflight (regular file, broken symlink, unwritable directory), and--write-reportfailures leaving the exit code alone.Manual Testing
Environment: venv
/tmp/agt-3137-venv2withagent-control-specification 0.4.0b0built frompolicy-engine/sdk/python,PYTHONPATH=srcfromagent-governance-python/agt-policies.Full agt-policies suite on this branch at 3c2769e6 (
pytest -o addopts=""to get the summary line the repo's-qaddopts suppress):tests/test_migrate.pyandtests/test_migrate_resolution.pyalone:The 17 skips are the pre-existing
agent_osimport and cedar-dispatcher skips. The new root-only skip did not fire;test_write_refuses_when_policies_dir_is_not_writableran 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):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:
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_rulespasses on all three states, as intended. The two10400tests also pass onorigin/mainbecause main has no finiteness check at all, so the "12 of 13 fail on the old source" claim holds relative to 134334ce, not relative toorigin/main.CI checks run locally:
ruff check src/ --select E,F,W --ignore E501,scripts/check_license_headers.pyon new files, and the cspell added-lines check. All clean.Checklist
Attribution & Prior Art
Prior art / related projects (if any):
AI Assistance
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