Repository navigation
fix(migrate-apim): give the partialWhen conditions one owner - #73
Merged
Merged
Conversation
`partialWhen` was the last part of the rules vocabulary with two independent declarations and no join between them. `$defs.partialWhen` in rules.schema.json said which condition names were legal; `plan._apply_partial_when` said which ones did anything, reading each inline through `condition.get(...)` with no declared set. `capability._POLICY_FIELDS` lists `partialWhen` among the fields a site reads, so `rule-field-unread` was satisfied -- the field is read, and the condition names inside it were examined by nothing. Closing the schema to a named set answered one direction only. The other one is the live risk, because adding conditions is how this vocabulary grows: add a key to the schema and to a rule, forget the predicate, and `compile --strict` exits 0 with nothing to say while the rule is inert. The element keeps the stronger verdict that the rule exists to take away -- a failed inference reading as a confident negative, which is the one way this tool must not fail. No test could have caught it either. Renaming the one condition that existed fails two tests in test_plan.py, because they pin its behaviour; adding one that is never implemented has no behaviour to pin. So the conditions get one owner, the way `dispatch.Selector` already does for match keys and rule fields. `plan._PARTIAL_WHEN` pairs each condition with the test it runs, and `_apply_partial_when` dispatches through it: the mapping is the declaration, not a list kept beside the code that could drift from it. `compile.check_partial_when_conditions` reads it and fails on any condition a rule demotes on that nothing implements. The schema keeps its key list rather than becoming generated. It carries the per-key types and the prose a reader opens it for, so generating it would move that content into Python and leave the JSON a build artifact -- and this tool has no codegen step, so it would gain a stale-artifact failure mode and a command to remember. Instead `compile` holds the two sets equal in both directions, which is what `check_paths_are_fields` already does for the selectors and inventory.schema.json: a key the schema allows and nothing implements is caught as the schema is edited, before any rule uses it, and a condition implemented and not allowed is caught before it is written on a rule and rejected for its shape. The declaration sits in plan.py and not in dispatch.py because `partialWhen` is not a selection criterion: it is applied to a rule already chosen, and the predicates are the implementation. Putting it next to `Selector` would separate it from the code it describes, which is the arrangement this fixes. `_apply_partial_when` now refuses an unknown condition instead of skipping it. `compile` gates the bundled rules, but `plan --rules` can be pointed at a file that never compiled, and silence there would put the defect back one layer down. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #63.
The problem
A rule in
capabilities/rules.jsonsayingip-filtermaps natively, except when the source filter is driven by a policy expression — then it is a lossy mapping the operator has to settle:{ "selects": "apim.policies[].elements[]", "where": { "name": ["ip-filter"] }, "verdict": "native", "partialWhen": { "inUnconditionalSection": true }, "partial": { "proposal": "retain-in-apim", "rationale": "Expression-driven filter." } }Every gate passes:
And the plan says the opposite of what the rule says:
inUnconditionalSectionis not a condition the planner implements, and nothing anywhere says so. Which keywords are legal is declared inschemas/rules.schema.json; which ones do anything is declared inline inplan._apply_partial_when. Nothing compares the two. The rule is inert, and the operator reads a clean mapping where its author wrote a lossy one.Closing the schema to a named set — already done — only catches the reverse: a rule naming a keyword the schema omits. The direction above is the one the vocabulary grows in.
Proposed solution
plan._PARTIAL_WHENpairs each condition with its predicate, so a condition exists exactly when it has an entry and there is no second list to keep in step. Same arrangement asdispatch.Selector, which both performs a match and states what the match reads.compileholds the data to it.check_partial_when_conditionsfails on a condition a rule names that nothing implements, and on a key the schema permits that nothing implements — both directions, decidable offline from committed data, already run in CI.compilecovers the shipped rules, butplan --rulestakes a file that never compiled, and silence there puts the same defect one layer down.Two choices that had a plausible alternative:
check_paths_are_fieldsalready does this for selectors againstinventory.schema.json.plan.py, notdispatch.py. These are not selection criteria — they apply to a rule already chosen. BesideSelectorthey would be separated from the predicates implementing them.Verification
python -m pytest -q-> 1052 passed, 1 skipped (1043 before; 9 new)python -m migrate_apim compile --strict-> 0 errors, 0 warnings, exit 0test_decisions_unchanged.pypasses untouched: no decision moved, only the dispatchThe same rule on this branch:
New tests cover both directions, that the shipped data is clean, that the declaration is derived from the predicates rather than written out beside them, and that each condition demotes on its own — driven by the declaration, so one added without a test case fails rather than going untested.
🤖 Generated with Claude Code