Repository navigation
fix(policy): judge the declared constraint, accept package release tags - #3381
Kaustubh Arora (Kaustubh1235) wants to merge 1 commit into
Conversation
require_pinned_constraint judged the wrong string in two ways.
The resolve phase rewrites a direct dependency's in-memory reference to
the tag a semver range selected, and the install gate runs after that,
so a bounded range such as `pkg-a#^1.0.0` was classified as whatever tag
it resolved to. The resolve phase now records the manifest-declared refs
of the direct dependencies before any rewrite, and the gate and the
post-targets re-check classify those. The resolved reference is left in
place for download, drift and lockfile work; preflight and audit already
hold the manifest view and are unchanged.
The literal-tag classifier only knew the `v{version}` layout, while the
resolver selects from `v{version}`, `{name}--v{version}` and
`{name}-v{version}` and the dependency guide recommends pinning the
package-qualified forms. The classifier now accepts those layouts for
the dependency's own package name, using the resolver's tag patterns and
regex builder, so `pkg-a-v1.1.0` on a `pkg-a` dependency is pinned while
`pkg-b-v1.1.0`, `pkg-a-v1.1` or `release-pkg-a-v1.1.0` stay bare
branches. Missing refs, moving branches, wildcards and open-upper ranges
are classified as before, and a selected tag or SHA never launders an
unbounded declaration.
Refs microsoft#3224.
|
Kaustubh Arora (@Kaustubh1235) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
2 similar comments
|
Kaustubh Arora (@Kaustubh1235) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
|
Kaustubh Arora (@Kaustubh1235) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
🟡 Changes recommended
Duplicate dependency identities can overwrite declared constraints and let a branch pass the pinned-constraint gate; package-name derivation also needs one shared owner.
2 open issues
What changed in this PR
This PR addresses #3224 by making pinned-constraint policy checks evaluate declared references rather than resolved tags, and recognize package-qualified release tags.
Changes:
- Capture direct dependency declarations and forward them to install policy checks.
- Accept release tags matching the dependency's own package name.
- Add regression coverage and clarify policy behavior in documentation.
| File | Description |
|---|---|
| tests/unit/policy/test_pinned_constraint_declared.py | Tests declared-ref precedence and package-qualified tags. |
| tests/integration/policy/test_require_pinned_constraint_e2e.py | Checks declaration forwarding to the policy gate. |
| tests/integration/policy/test_require_pinned_constraint_declared_e2e.py | Adds real-binary install, replay, update, and audit coverage. |
| src/apm_cli/policy/policy_checks.py | Passes declared refs into classification and diagnostics. |
| src/apm_cli/policy/_help_text.py | Clarifies accepted tags and declared-constraint semantics. |
| src/apm_cli/policy/_constraint_pinning.py | Recognizes package-qualified tags and declared-ref overrides. |
| src/apm_cli/install/phases/resolve.py | Snapshots direct refs before resolution rewrites them. |
| src/apm_cli/install/phases/policy_target_check.py | Forwards declarations during target checks. |
| src/apm_cli/install/phases/policy_gate.py | Forwards declarations to the install gate. |
| src/apm_cli/install/context.py | Stores the declared-reference snapshot. |
| docs/src/content/docs/reference/policy-schema.md | Updates policy semantics and examples. |
| docs/src/content/docs/enterprise/policy-reference.md | Clarifies direct-only evaluation and accepted tags. |
| docs/src/content/docs/consumer/manage-dependencies.md | Explains policy acceptance of ranges and package tags. |
| docs/src/content/docs/consumer/governance-on-the-consumer-ramp.md | Clarifies that policy judges direct declarations. |
Review effort: Balanced
| ctx.declared_direct_refs = { | ||
| dep.get_unique_key(): dep.reference for dep in ctx.all_apm_deps if dep.reference is not None | ||
| } |
| virtual_path = getattr(dep, "virtual_path", None) | ||
| is_subdirectory = getattr(dep, "is_virtual_subdirectory", None) | ||
| if callable(is_subdirectory) and is_subdirectory() and virtual_path: | ||
| return virtual_path.rstrip("/").rsplit("/", 1)[-1] or None |


Description
require_pinned_constraintnow judges the constraint the author declared, and recognises a package's own release tags.Two things were wrong. The resolve phase rewrites a direct dependency's in-memory reference to the tag its semver range selected, and the install gate runs after that, so
pkg-a#^1.0.0was judged aspkg-a-v1.1.0. The literal-tag classifier also only knewv{version}, while the resolver selects fromv{version},{name}--v{version}and{name}-v{version}and the dependency guide tells people to pin the package-qualified forms.The resolve phase now snapshots the manifest-declared refs of the direct dependencies before any rewrite (
ctx.declared_direct_refs), andpolicy_gateandpolicy_target_checkpass that map torun_dependency_policy_checks, which classifies it in place of the rewritten reference. Nothing else sees a different reference: download, drift and lockfile work keep the resolved tag, and preflight and audit already held the manifest view. The classifier accepts{name}-v{version}and{name}--v{version}for the dependency's own package name only, reusing the resolver'sDEFAULT_TAG_PATTERNSandbuild_tag_regex, sopkg-b-v1.1.0,pkg-a-v1.1,release-pkg-a-v1.1.0andpkg-a_v1.1.0remain bare branches. The package name follows the resolver's rule (final virtual-path segment, else repository name), with a unit test asserting parity against_git_semver_package_name.Docs updated where the rule is described (
policy-reference.md,policy-schema.md,governance-on-the-consumer-ramp.md,manage-dependencies.md, and theREQUIRE_PINNED_CONSTRAINT_HELPtext), including correcting two places that said transitive deps are classified; the check has been direct-only since #1494.Issue and approved scope
Issue: #3224
Human scope-approval comment: #3224 (comment)
This PR completes the accepted issue scope. It does not request merge approval.
Type of change
Testing
Unit (
tests/unit/policy/test_pinned_constraint_declared.py, 13 tests): package-qualified forms for subdirectory and repository deps, prerelease and build suffixes, other-package and near-miss negatives, name parity with the resolver,declared_refprecedence (a declaredmainstays a violation after resolution replaced it with a tag; a declared>=1.0.0stays open-upper when the reference is a SHA), and_check_pinned_constraintswith and withoutdeclared_refs.Gate wiring (
TestDeclaredRefsWiringintest_require_pinned_constraint_e2e.py): pinned to the gate's own call. Verified it fails when either the gate stops forwardingdeclared_refsor the resolve phase stops recording them.Real binary (
tests/integration/policy/test_require_pinned_constraint_declared_e2e.py,e2e+requires_apm_binary): one local bare monorepo with a subdirectory package taggedpkg-a-v1.1.0/pkg-a--v1.1.0and the root taggedv1.0.0only, so^1.0.0resolves to the package-qualified tag the old gate rejected. Policy reachesinstallthe way the Govern contract does (ADO-shaped origin, leaf extending the strict policy at the cache-key path,policy statuswarm), and every governed command asserts the(cacheddiagnostic. Covers fresh install, frozen replay, repeat install,update --yesand audit for the range; the two literal forms; the repository-level range; andmain,pkg-b-v1.1.0,>=1.0.0blocked at install and failed by audit with the same hints. Audits are judged by thedependency-pinned-constraintcheck in the JSON, not the exit code.Local validation:
tests/unit/policy tests/unit/install tests/integration/policy tests/integration/test_policy_pinned_constraint_e2e.py: 4360 passed, 7 skipped, 1 xfailed.tests/spec_conformance/test_policy_reqs.pypasses. The real-binary contract passed on three consecutive runs. Ruff check and format clean on every changed file.Observed while writing the contract, out of scope here and identical on unmodified main (same scenario, same binary build, byte-for-byte the same lock and deployed state at every step): on this package shape,
install --frozenright after a fresh install redeploys the 1.0.0 skill while the lock still recordspkg-a-v1.1.0, and a plain repeatinstalldropsconstraintandresolved_tagand writes the raw range intoresolved_ref;update --yesrestores them. I'll file that separately with the probe script. The contract therefore asserts the policy verdict on those steps and not what they record.Spec conformance (OpenAPM v0.1)
CONFORMANCE.{md,json}req-pl-007 flags "a bare branch name"; a dependency's own release tag in a layout the resolver selects from is a tag, not a branch, so recognising it is a classification repair under the existing rule. req-pl-008 enumerates forms that MUST count as pinned and is unchanged. If the maintainers want req-pl-008 to list the package-qualified layouts explicitly, that would be a separate spec proposal under normal review.