Repository navigation
audit: fail closed when required policy cannot be resolved - #4920
Conversation
Bare `apm audit` treated any org-policy discovery error, and any unresolved policy, as "fail_on_drift off". When the project requires a policy (policy.fetch_failure_default: block, or an always-block outcome) and drift fails, the audit now exits 1 with a named [x] cause; an optional policy keeps drift advisory and prints a [!] notice. `apm audit --ci` routed a discovery exception as an unnamed crash; it now routes it as a fetch failure through the policy-outcome owner, so the existing [x]/[!] lines name the cause. Both paths reuse policy/outcome_routing.py: classify_project_policy decides "required", and the new discovery_failure_result converts a raised discovery (shared with discover_trust_policy). The outcome-routing guard now also requires the fail_on_drift reader to ask the owner. Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
A critical routing issue converts policy inheritance validation failures into cache misses, allowing CI audits to skip enforcement and exit successfully under the default warn posture.
1 open issue
What changed in this PR
This PR routes audit policy-discovery failures through the shared policy-outcome owner so required policies no longer silently leave failed drift checks advisory.
Changes:
- Adds shared discovery-error conversion and routed diagnostics for bare audit and
--ci. - Extends architecture guards and regression coverage for unresolved policies.
- Updates audit documentation and adds a changelog fragment.
| File | Description |
|---|---|
tests/unit/test_audit_unresolved_policy.py |
Adds seven policy-resolution regression cases. |
tests/spec_conformance/test_policy_reqs.py |
Uses typed discovery results in policy stubs. |
tests/lifecycle/security/test_policy_outcomes.py |
Updates blocked replay exit and diagnostic expectations. |
src/apm_cli/policy/outcome_routing.py |
Adds shared discovery-error conversion. |
src/apm_cli/commands/audit.py |
Routes discovery failures and applies required-policy drift gating. |
scripts/architecture_linter/checks/install_policy_outcome_routing.py |
Requires bare audit to call the routing owner. |
packages/apm-guide/.apm/skills/apm-usage/commands.md |
Documents required-policy audit failures. |
docs/src/content/docs/reference/cli/audit.md |
Updates policy diagnostics and exit-code guidance. |
changelog.d/4920.fixed.md |
Records the user-visible audit fix. |
.apm/architecture/owners/install-deployment.json |
Registers the bare-audit policy reader as a consumer. |
Review effort: Balanced
A rejected inheritance chain (cycle, cross-host extends, excess depth) now maps to the always-block incomplete_chain outcome in the owner, so a warn posture no longer skips enforcement for it. Audit converts only the owner's known discovery errors; any other exception propagates. Update the audit-policy-discovery mutation fragment for the new indentation. Co-authored-by: copilot-swe-agent[bot] <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
A critical uncaught PolicyInheritanceError can escape bare-audit drift checking before the named policy failure is rendered, requiring broader exception handling and a regression test.
1 open issue
1 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
The documentation incorrectly promises advisory drift under warn, although rejected inheritance chains, policy hash mismatches, and ambiguous git remotes still cause exit 1.
1 open issue
1 resolved since last review
Review effort: Balanced
Co-authored-by: copilot-swe-agent[bot] <[email protected]>


Description
Bare
apm auditresolvedsecurity.audit.fail_on_driftby discovering org policy and returning "off" on any discovery error or unresolved policy, ignoringpolicy.fetch_failure_default. A project that requires an org policy could therefore get exit 0 on a failed drift check.apm audit --cilet a raised discovery escape as an unnamed traceback.Both audit paths now route discovery through the policy-outcome owner (
policy/outcome_routing.py):discovery_failure_result(new, shared withdiscover_trust_policy) turns a raised discovery into a routable result. A rejected inheritance chain (PolicyInheritanceError: cycle, cross-hostextends, excess depth) becomes the always-blockincomplete_chainoutcome, with the already-redacted message. Any other error becomescache_miss_fetch_fail, reporting the exception type only. Audit converts only the owner'sPOLICY_DISCOVERY_ERRORS; any other exception propagates.apm audit:_resolve_fail_on_driftasksclassify_project_policy. When the owner says the policy is required (fetch_failure_default: block, or an always-block outcome) and drift fails, the audit exits 1 with[x] Org policy could not be resolved; fail_on_drift could not be applied: <cause> (policy.fetch_failure_default=block)and drift renders as[x]. An optional policy keeps drift advisory and prints the same text as a[!]notice.APM_POLICY_DISABLE=1still skips discovery.apm audit --ci: the error goes through the existing routed render, so it prints[x] Policy fetch failed: policy discovery failed (<Type>) (policy.fetch_failure_default=block)and exits 1, or[!] ...; enforcement skippedunder warn. That wording was already pinned by lifecycle journeys, so it is unchanged.install-deployment-policy-outcome-routingguard now also requires the bare-audit reader to callclassify_project_policy. The owner registry lists_resolve_fail_on_driftas a consumer.Behaviour change to note: under the warn posture, a transport-type discovery error (
OSError,ValueError,RuntimeError) in--ciused to crash with exit 1 and a traceback. It now behaves like any other fetch failure: a[!]line, enforcement skipped, and exit 0 when nothing else fails. That matches the existingcache_miss_fetch_failwarn behaviour. A rejected chain always fails, under any posture, and so does any unexpected exception.Issue and approved scope
Refs: maintainer-initiated hardening of
apm auditpolicy resolution.Scope: maintainer-initiated; reviewed by the maintainers.
Does this PR complete the issue, or what remains? Completes the scoped item.
Type of change
Testing
New module
tests/unit/test_audit_unresolved_policy.py(component,P-OWN-policy-outcome-routing). It drives the realauditcommand and routing owner, and replaces only discovery and the drift replay.[x]cause[x]cause[!]noticefail_on_drift: false-> exit 0 (control)APM_POLICY_DISABLE=1-> no discovery, exit 0 (control)--ci, required, discovery raises -> exit 1 +[x]cause--ci, optional, discovery raises -> exit 0 +[!][x]--ci, optional, rejected chain -> exit 1 +[x]Updated expectations:
tests/lifecycle/security/test_policy_outcomes.pyaudit-exec-replayblock cells now expect exit 1 plus the[x]cause. They had pinned the old exit 0.tests/integration/test_architecture_semantic_rule_mutations.py: theinstall-deployment-audit-policy-discoveryfragment now matches the new indentation of the lambda.req-pl-014spec cells now stub discovery with a realfoundPolicyFetchResult. They had used a bare namespace withoutoutcome, which the owner routes as an unknown outcome.Validation (all passed):
uv run --extra dev pytest -n 0 tests/unit/test_audit_unresolved_policy.py: 9 passed. Against main'ssrc/: 7 failed, 2 passed.uv run --extra dev pytest -n 4 tests/unit/test_audit*.py tests/unit/commands/test_audit_error_handling.py tests/unit/policy/test_outcome_routing.py tests/spec_conformance/test_audit_recorded_target_reqs.py tests/spec_conformance/test_policy_reqs.py tests/lifecycle/security/test_policy_outcomes.py tests/integration/test_audit_drift_exit_contract_lifecycle.py tests/integration/test_audit_scan_hermetic.py tests/integration/test_audit_coverage.py tests/unit/scripts/test_architecture_runner.py tests/unit/policy/ tests/integration/test_architecture_semantic_rule_mutations.py tests/integration/test_architecture_owner_rule_mutations.py: 3713 passed, 1 skipped.uvx [email protected] checkandformat --checkon changed files: passed.python scripts/check_test_assertions.py: clean.python scripts/lint_architecture_boundaries.py --root .: clean.python -m tests.quality.taxonomy --changed: clean.python -m tests.lifecycle.generator --check: up to date.scripts/check_complexity_ratchet.py --base-ref origin/main: clean. pylint R0801: 10.00/10.tests/spec_conformance/mode_b_detector.sh: OK (below threshold).Changelog
changelog.d/<pr>.<section>.md, holdingone line: user-visible effect first, 25 words or fewer, ending with
(#PR); breaking changes only under upgrade notes; no Highlights; nonew lines under
## [Unreleased]. Seechangelog.d/README.mdand.github/instructions/changelog.instructions.md.Spec conformance (OpenAPM v0.1)
CONFORMANCE.{md,json}regeneratedreq-pl-014still holds: drift alone does not change the exit status whenfail_on_driftis off. The new exit 1 comes from a required policy that could not be resolved.