Skip to content

fix(policy): reject unknown keys and duplicate rule ids on load (#160) - #162

Open
RupeshMaster wants to merge 1 commit into
CodewithJha:mainfrom
RupeshMaster:fix/policy-strict-validation
Open

RupeshMaster wants to merge 1 commit into
CodewithJha:mainfrom
RupeshMaster:fix/policy-strict-validation

Conversation

@RupeshMaster

Copy link
Copy Markdown

Summary

Fixes #160. Policy models ignored keys they did not know, so a typo in policy.yaml changed what a rule checked without any error.

  • PolicySet, PolicyRule and ArgConstraint now use extra="forbid": a misspelled operator (lt_eq), rule key (wen) or top-level key (ruels) fails loading instead of being dropped.
  • Rule ids must be unique (hits, regressions and reports are keyed by id); id and tool must be non-empty.
  • Schema errors for unknown keys name the location and suggest the closest known key:
error: invalid project policy — policy failed schema validation — path=…/policy.yaml — rules.0.wen: unknown key (did you mean 'when'?)

Behavior change to review

This is stricter than before: a policy that carries extra keys (for example a home-grown severity: or notes: field) will stop loading until they are removed. All policies shipped in the repo (examples/**, the mutiny init scaffold, the demo harness) load unchanged, and a new test keeps it that way. If you prefer a softer rollout, I can turn unknown keys into a warning and keep only duplicate ids as an error.

Files

  • mutiny_core/policy/models.py, mutiny_core/policy/constraints.py: strict models, unique ids.
  • mutiny_core/policy/load.py: friendlier message for unknown keys.
  • docs/SYSTEM_DESIGN.md, CHANGELOG.md (under Changed, since it can reject files that loaded before).
  • Tests: 13 in tests/unit/test_policy_load.py.

Type of change

Checklist

  • I read CONTRIBUTING.md (where to ask, how reviews work, how to run tests)
  • Linked related issue (Fixes #N) or Discussion when applicable
  • Core stays free of framework SDK imports (adapters own glue)
  • Tests added or updated when behavior changes (uv run pytest tests/unit -q)
  • Docs / README claims match reality (no PyPI / multi-adapter claims unless shipped)
  • One concern per PR when possible
  • Authorized testing only — no open-internet attack-proxy behavior

Test plan

  • Python lint: uv run --locked ruff check → all checks passed
  • Backend tests: uv run pytest tests/unit tests/integration tests/reliability -q → 861 passed. Run locally on Python 3.12 only (macOS, OPENAI_API_KEY unset); 3.11 is left for CI.
  • CLI smoke: from a copy of examples/openai_support_agent, mutiny --help, mutiny init, mutiny run with MUTINY_SAMPLE_OFFLINE=1 and empty OPENAI_API_KEY / FEATHERLESS_API_KEY → violation found, regression saved.
  • Web typecheck + build: not run; apps/web is not touched.
  • Package build + isolated artifact gate: tests/unit/test_package_release.py passed as part of the unit run; ./scripts/verify_release_artifacts.sh not run.

Extra: 11 of the new tests fail on main (ee7e898). With when: changed to wen: in the sample policy, mutiny run now exits 2 with the message above. The existing suite needed no changes.

Notes

Policy models ignored keys they did not know, so a typo changed what a
rule checked without any error:

- a misspelled operator (`lt_eq: 500` next to a valid one) was dropped,
  weakening a require rule so real violations passed
- `wen:` instead of `when:` made a conditional rule apply to every call
- duplicate rule ids loaded, although hits, regressions and reports are
  keyed by rule id; empty id / tool loaded too

PolicySet, PolicyRule and ArgConstraint now forbid extra keys, rule ids
must be unique, and id / tool must be non-empty. Schema errors name the
key's location and suggest the closest known key.
@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

@RupeshMaster is attempting to deploy a commit to the priyanshu's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] policy.yaml typos (unknown keys/operators) and duplicate rule ids load silently and change what rules check

1 participant