Skip to content

fix(migrate-apim): give the partialWhen conditions one owner - #73

Merged
Facundo Santiago (santiagxf) merged 1 commit into
mainfrom
fix/partial-when-one-owner
Dec 3, 2026
Merged

Facundo Santiago (santiagxf) merged 1 commit into
mainfrom
fix/partial-when-one-owner

Conversation

@santiagxf

@santiagxf Facundo Santiago (santiagxf) commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Fixes #63.

The problem

A rule in capabilities/rules.json saying ip-filter maps 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:

jsonschema.validate(rules, schema)  ->  valid
compile --strict                    ->  0 error(s), 0 warning(s), exit 0
pytest -q                           ->  passes

And the plan says the opposite of what the rule says:

_apply_partial_when(rule, element)["verdict"]  ->  'native'   # the author wrote partial

inUnconditionalSection is not a condition the planner implements, and nothing anywhere says so. Which keywords are legal is declared in schemas/rules.schema.json; which ones do anything is declared inline in plan._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

  1. One declaration, beside the code that implements it. plan._PARTIAL_WHEN pairs 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 as dispatch.Selector, which both performs a match and states what the match reads.
  2. compile holds the data to it. check_partial_when_conditions fails 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.
  3. The planner refuses rather than ignores. An unknown condition raises. compile covers the shipped rules, but plan --rules takes a file that never compiled, and silence there puts the same defect one layer down.

Two choices that had a plausible alternative:

  • A check, not generating the schema from the code. The schema carries per-key types and the prose a reader opens it for, and the tool has no codegen step to keep an artifact fresh. check_paths_are_fields already does this for selectors against inventory.schema.json.
  • The declaration lives in plan.py, not dispatch.py. These are not selection criteria — they apply to a rule already chosen. Beside Selector they 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 0
  • test_decisions_unchanged.py passes untouched: no decision moved, only the dispatch

The same rule on this branch:

error: [partial-when-implemented] policies {"selects": "apim.policies[].elements[]", "where":
  {"name": ["ip-filter"]}}: demotes on 'inUnconditionalSection', which
  `plan._apply_partial_when` does not implement, so the condition never holds and the element
  keeps the stronger verdict this rule exists to take away.
error: [partial-when-implemented] rules.schema.json allows partialWhen 'inUnconditionalSection',
  which `plan._apply_partial_when` does not implement. A rule naming it would validate and never
  fire. Give `plan._PARTIAL_WHEN` the predicate, or drop the key.
2 error(s), 0 warning(s)  exit=1

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

`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>

@paulyuk Paul Yuknewicz (paulyuk) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@santiagxf
Facundo Santiago (santiagxf) merged commit e4bd3b9 into main Dec 3, 2026
4 checks passed
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.

migrate-apim: partialWhen conditions are declared in the schema and implemented in the planner, and nothing checks the two agree

2 participants