Skip to content

fix(policy): an unreachable earlier candidate no longer hides the cached org policy (#4456) - #4464

Merged
Daniel Meppiel (danielmeppiel) merged 719 commits into
mainfrom
danielmeppiel-stale-cache-policy-4456
Nov 21, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 719 commits into
mainfrom
danielmeppiel-stale-cache-policy-4456

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

When the org policy cache was stale but under 7 days old and refresh failed (offline), discovery tried <org>/.github-private first. It failed to connect and returned cache_miss_fetch_fail before reaching <org>/.github, whose policy was cached. Install, MCP-only install, lifecycle scripts and dry-run preflight then ran without the policy ("proceeding without policy enforcement").

The fix is at the discovery owner (policy/discovery.py _auto_discover, with a helper in policy/_cached_candidates.py). On a transport failure (connection error or timeout) of a GitHub/GHE candidate, the cascade serves the first lower-precedence candidate's cache entry:

  • A fresh entry is served as found.
  • An entry past its TTL but within MAX_STALE_TTL (7 days) is served as cached_stale with the fetch error, which is exactly what that candidate's own refresh failure gives. The output then says the policy is stale.

Absent candidates (404) are not cached, so a cached .github policy means .github-private was absent when it was cached. Auth and other errors (401/403, HTTP errors, malformed) still fail closed, because a newly readable-but-denied private repo may hold a stricter policy. TTL and cache-pin verification are out of scope (#4218 / #4315).

Stacked on #4461 (the S-lead re-key of these cells to #4456); the diff shrinks once it lands.

Issue and approved scope

Issue: Fixes #4456

Human scope-approval comment: apm-scope:v1 record on #4456 (status/accepted, milestone 0.37.0)

Does this PR complete the issue, or what remains? It completes it. The cache=stale<7d rows for install-pipeline, mcp-install-preflight, lifecycle-scripts, bundle-allow-map, install-dry-run-preflight, audit-ci, approve, policy-status, mcp-admission, bundle-install-preflight and the exec-map rows are now required. update-regate-shortcircuit|cache=stale<7d now behaves exactly like its fresh-cache row, so it carries the same #4458 short-circuit xfail.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally (targeted)

  • All existing tests pass (pending CI)

  • Added tests for new functionality (if applicable)

  • tests/lifecycle/security/test_policy_outcomes.py: all 13 cache=stale<7d cells were run locally. 12 pass and 1 is an xfail tied to apm update with nothing to change exits 0 when org policy is unavailable under block #4458 (with --runxfail before the re-key: 12 passed, update failed exactly like its fresh row). _OFFLINE is deleted.

  • tests/unit/policy/test_discovery.py: new test_unreachable_github_private_serves_the_stale_github_cache fails before the fix and passes after; test_github_private_auth_error_fails_closed_despite_a_cached_github keeps auth errors fail-closed. All 328 discovery unit tests pass.

  • tests/quality/test_lifecycle_bug_ledger.py passes, with the new org policy cached under 7 days is not applied the same way as a fresh one #4456 row.

  • Arch linter, complexity ratchet and ruff are clean. discovery.py is at 2090 lines (limit 2100).

Changelog

  • One line in one section under ## [Unreleased] in CHANGELOG.md (Fixed).
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour (the documented 7-day stale-cache behaviour now holds offline).

…pack line

Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	tests/fixtures/lifecycle_bug_ledger.json
…ns; name every credential tried

Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	src/apm_cli/core/auth.py
#	src/apm_cli/marketplace/version_pins.py
#	tests/fixtures/lifecycle_bug_ledger.json
…oject and user scope

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…er match without a local receipt; cleanup retains files the install kept

Co-authored-by: Copilot App <[email protected]>
The pre-download frozen MCP check stays silent (the hydrated check and the
MCP phase report parked servers); the commit gate does not repeat the org
policy discovery notice. An install refuses a symlinked lockfile only
when it adds or removes a dependency or updates refs, so an unchanged
install still reads through it.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Register update's provenance re-verification write as a commit site of the
lockfile write authority, and name the install-writes-lockfile decision in
one helper the preflight guard reads.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…touched (#4318)

apm update refreshes apm.yml dependencies only. With a local bundle
installed it now names the bundle (from its hook ownership markers and
compile staging slugs, else its file count) and gives the runnable next
step: apm install <newer-bundle-file>. The lifecycle generator's strict
cell rule|U8|update-leaves-a-local-bundle-untouched becomes required.

Co-authored-by: Copilot App <[email protected]>
… writing

`apm install --frozen --global` now checks each locked native plugin root
against its recorded fingerprint in the frozen hash verification, before any
write. A mismatch exits 1 naming the plugin, the expected and observed
digests and `apm install --global`, as `apm audit --ci` reports it; nothing
is written. A plain install that restores a changed root prints a [!] line.

The lifecycle I1 oracle treats a path under a native_plugin root as recorded
only when the same lock records that root, the observation is user scope and
the tree carries the record's fingerprint. Four gate cells become required;
six org-policy cells stay strict under #3703.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…; strict known cell for step 5

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…n, not a fixed column count (#4409)

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…er match without a local receipt; cleanup retains files the install kept

Co-authored-by: Copilot App <[email protected]>
…; one row re-keyed to #4443

Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	tests/quality/generated_obligations.json
… on a terminal and honors NO_COLOR

Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	tests/fixtures/lifecycle_bug_ledger.json
#	tests/utils/lifecycle_evidence.py
…fusals as [x] errors

Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	src/apm_cli/commands/update.py
#	tests/lifecycle/generator/rules.py
…acts module

tests/quality/test_generated_obligations.py is not in any CI job's file
list; test_promise_obligations.py runs in Architecture Ratchet Contracts.

Co-authored-by: Copilot App <[email protected]>
native.py keeps every strict cell still failing on the merged tree: the
re-keyed #4444/#4445/#4446/#4447 rows and the train #3703 native-plugin
rows; the #4154 and #4225 cells whose fixes are in are dropped (35 native
rows: 23 pass, 12 strict xfail).

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	tests/lifecycle/machines/test_m_source.py
#	tests/lifecycle/security/gate_slices/bin.py
Replaces the remaining issue-less strict reasons with the open issue that
tracks each defect. No cell is relaxed, skipped or removed.

Co-authored-by: Copilot App <[email protected]>
…ble next step

Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	tests/quality/generated_obligations.json
…nt promise ids

Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	tests/quality/generated_obligations.json
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 20, 2026
Copilot AI balanced review requested due to automatic review settings November 20, 2026 18:33
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The witness catalog still exempts fixed stale-cache cells from required receipt coverage and references #4456 instead of #4458 for the update cell, causing the freshness check to fail.

1 open issue
What changed in this PR

This PR fixes organization-policy discovery so an unreachable earlier GitHub candidate does not hide a usable policy cached from a later candidate, addressing #4456.

Changes:

  • Adds cache fallback for connection errors and timeouts while preserving fail-closed handling for authentication errors.
  • Promotes stale-cache lifecycle cases to required regressions, retaining the update short-circuit failure under #4458.
  • Documents offline behavior and carries forward issue-linked expected-failure reasons from #4461.
File Description
tests/​unit/​policy/​test_discovery.py Adds offline fallback and authentication-error regression tests.
tests/​spec_conformance/​test_security_behavior_reqs.py Links expected failures to tracking issues.
tests/​quality/​generated_obligations.json Updates generated witness and known-failure mappings.
tests/​lifecycle/​security/​test_policy_outcomes.py Requires fixed stale-cache cases and reassigns the update failure.
tests/​lifecycle/​security/​test_origin_and_credentials.py Links the provenance failure to #4228.
tests/​lifecycle/​security/​test_integrity.py Links integrity failures to tracking issues.
tests/​lifecycle/​security/​gate_slices/​native.py Links the uninstall failure to #4446.
tests/​lifecycle/​security/​gate_slices/​mcp.py Adds issue-linked MCP failure reasons.
tests/​lifecycle/​security/​gate_slices/​lsp.py Adds issue-linked LSP failure reasons.
tests/​lifecycle/​security/​gate_slices/​hook.py Adds issue-linked hook failure reasons.
tests/​lifecycle/​security/​gate_slices/​bin.py Links the dry-run failure to #4454.
tests/​lifecycle/​security/​gate_slices/​__init__.py Updates the expected-failure format comment.
tests/​lifecycle/​security/​egress_slices/​__init__.py Updates the expected-failure format comment.
tests/​fixtures/​lifecycle_bug_ledger.json Records #4456 and its regression coverage.
src/​apm_cli/​policy/​discovery.py Invokes cached-candidate fallback after transport failures.
src/​apm_cli/​policy/​_cached_candidates.py Selects a usable lower-precedence cached policy.
docs/​src/​content/​docs/​reference/​policy-schema.md Documents offline fallback and authentication-error behavior.
CHANGELOG.md Records the user-visible offline policy fix.

Review effort: Balanced

Comment on lines +1083 to +1086
"P|install-pipeline|cache=stale<7d": "#4456",
"P|lifecycle-scripts|cache=stale<7d": "#4456",
"P|mcp-admission(trust.py)|cache=stale<7d": "#4456",
"P|mcp-install-preflight|cache=stale<7d": "#4456",
…ng-rekey

# Conflicts:
#	tests/lifecycle/security/gate_slices/bin.py
#	tests/lifecycle/security/gate_slices/hook.py
#	tests/lifecycle/security/gate_slices/mcp.py
#	tests/lifecycle/security/gate_slices/native.py
#	tests/quality/generated_obligations.json
#	tests/spec_conformance/test_security_behavior_reqs.py
…hed org policy (#4456)

Offline, discovery tried .github-private first, failed to connect and
returned cache_miss_fetch_fail before reaching .github, whose policy was
cached (fresh, or stale within 7 days). Install, MCP install, lifecycle
scripts and dry-run then ran without the policy. On a transport failure the
cascade now serves the first lower-precedence candidate's cache entry, as
that candidate's own refresh failure does (cached_stale when past TTL).
Auth and other errors still fail closed.

The cache=stale<7d site rows flip to required; update's row moves to the
and CHANGELOG added.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Steward fold: the cascade fell through to ANY lower candidate's cache when
a higher candidate was unreachable, which could downgrade a private
.github-private policy to a public .github one. A fresh resolution now
records its source and the candidates absent (404) above it. Offline, the
cascade falls through only when that record proves the unreachable
candidate was absent and names the lower source; otherwise the failure
stands (block fails closed). Negative test added.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel
Daniel Meppiel (danielmeppiel) force-pushed the danielmeppiel-stale-cache-policy-4456 branch from 63299f3 to 5b1c18b Compare November 20, 2026 18:51
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
…onger hides the cached org policy (#4456)

Co-authored-by: Copilot App <[email protected]>

# Conflicts:
#	tests/fixtures/lifecycle_bug_ledger.json
…t the cache bytes

The three #4456 discovery cells patched discovery's own _fetch_github_contents
and asserted only on return values (AQ004, AQ003 over the baseline). They now
fake only HTTP (policy_get) and credentials (AuthResolver.try_with_fallback),
so the real fetch and error mapping run, and assert the cache files on disk
are unchanged by the offline or denied refresh.

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 8de1172 into main Nov 21, 2026
3 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-stale-cache-policy-4456 branch November 21, 2026 05:24
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.

org policy cached under 7 days is not applied the same way as a fresh one

2 participants