Skip to content

test(lifecycle): GATE state machine with cross-cutting oracle (#4031) - #4040

Merged
Daniel Meppiel (danielmeppiel) merged 16 commits into
mainfrom
danielmeppiel-fictional-guacamole
Nov 20, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 16 commits into
mainfrom
danielmeppiel-fictional-guacamole

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

TL;DR

Adds M-GATE, a Hypothesis sub-machine of the generated lifecycle model that drives python -m apm_cli.cli through every executable-trust transition, with a cross-cutting oracle (I1–I9 plus reference law R) checked after every step. First, the existing generated machine stops replaying its mandatory spine per example (310.5 s → 121.1 s locally). Cells the machine shows failing on main are recorded as strict expected failures naming their tracking issues.

Refs #4031 — completes the approved scope (spine fix, M-GATE, oracle, cell coverage, mutant, one internal CHANGELOG line).

Important

Merge order: merge this PR early in the 0.38 batch, before the fix PRs below. Every recorded failing cell is strict: once its fix lands, the machine fails with "strict expected failure … did not fail; remove its expectation", so each fix PR removes its matching KnownCell when it rebases.

Cell markers and their fix PRs

Rebased on 0.37.0: gate-cell-a (#4041) and gate-cell-f (#4048) were fixed by #4004 and are removed; both were verified to pass on main first.

Cell Law Issue Fix PR
gate-cell-b R #4042 #4058
gate-cell-c-lock / c-pair I1 / I3 #4043 #4054
gate-cell-d R #4044 #4055
gate-cell-e I7 #4045 #4057
gate-cell-g I2 / I3 #4050 —

Problem (WHY)

  • The generated lifecycle machine ran its full mandatory spine inside @initialize, so every Hypothesis example paid for it (≈466 s on CI x86 for the three generated nodes).
  • No machine covered the gate family: approve / deny / revoke / org-policy tightening interleaved with install, --frozen, update, uninstall, prune and audit --ci, at both scopes.
  • Lifecycle checks were per-rule; nothing asserted cross-cutting laws (lock/deployed agreement, frozen/audit agreement, gate monotonicity, ASCII, exit codes) after every step.

Approach (WHAT)

# Change
1 Spine replays once per variant world inside the first generated example; later examples restore that world in place (restore_tree), because local receipts bind to the deployment root's inode.
2 check_cross_cutting(before, after, result, model, op) with laws I1–I9; each law aliases existing ledger properties (no new ledger rows).
3 LifecycleStateSnapshot optionally captures approval stores, the policy cache, native plugin registries and per-adapter MCP config files.
4 M-GATE: 8 rules, stateful_step_count=8; 4 derandomized examples per scope on PR (lifecycle shards), 25 fresh examples per scope in .github/workflows/gate-machine-nightly.yml.
5 Every step emits cell:<OP>|<SRC>|<PRIM>|<GATE>|<SCOPE>; a session hook writes the visited set; tests/quality checks it covers the checked-in obligation list (107 cells); the GATE Cell Coverage job merges every shard's set and requires both scope spines.
6 A recorded mutant (tests/fixtures/gate_mutants/skip_approval_check.py) that skips the approval check must fail the machine.

Implementation (HOW)

  • tests/integration/test_generated_lifecycle_state_machine.py — removes @initialize; _SpineWorld replays once and restores per example. The [BUG] Global install succeeds with empty skill directories when HOME/APM_HOME uses a symlink alias #2867 witness test_generated_lifecycle_mandatory_replay is unchanged. New draws exposed a model bug: a no-op update after a local edit keeps the edit (the CLI is right), so the model now resets clean only when the revision changes.
  • tests/utils/world_snapshot.py — in-place tree restore shared by both machines.
  • tests/utils/lifecycle_oracle.py — the oracle. Expected cells are strict per step; message-matched cells (output noise) are strict per run.
  • tests/utils/lifecycle_state.py — LifecycleSurfaceRoots + four optional snapshot fields; existing callers are unaffected.
  • tests/integration/_gate_machine_kit.py — world (hook, LSP and native-plugin packages), reference model, rules, known cells. Each known cell names the violation message it owns; anything else on that step still fails. I4 also checks a deployed ceiling recorded at each deny, revoke or tightening.
  • tests/integration/test_generated_gate_state_machine.py — mandatory GATE spine per scope (asserts obligation coverage and that every scope cell still fails), generated sequences per scope, mutant kill.
  • tests/quality/test_gate_cell_obligations.py, gate_cell_obligations.json — obligation schema, vocabulary, ≤10 rules, law aliases, visited coverage.
  • CI — lifecycle shards upload their visited cells; a new gate-cell-coverage job merges them and runs the coverage check. A new scheduled workflow, gate-machine-nightly.yml, runs 25 non-derandomized examples per scope and opens or updates one tracking issue on failure. build-release.yml is unchanged. x86 duration seeds for the new nodes.

Diagrams

Legend: one M-GATE step; the oracle and the reference law both consult the known-cell table, so an expected failure must fail and anything else must pass.

flowchart LR
    R["rule: decide / install / audit_ci ..."] --> C["emit cell event"]
    C --> B["observe before: snapshot + deployed surfaces"]
    B --> X["python -m apm_cli.cli"]
    X --> A["observe after"]
    A --> REF["reference law R"]
    A --> O["check_cross_cutting I1-I9"]
    K["KNOWN_CELLS strict"] --> REF
    K --> O
    O -->|"unexpected violation or expected pass"| F["fail run"]
    REF -->|"unexpected violation or expected pass"| F
Loading

Trade-offs

  • Spine stays in the generated test. Chose replay-once inside the first example; rejected dropping it, because the [BUG] Global install succeeds with empty skill directories when HOME/APM_HOME uses a symlink alias #2867 evidence contract expects the trajectory inside a generated model. The remaining floor is the per-variant spine, so CI x86 is estimated at ≈180 s, not the 135 s target.
  • Separate module for M-GATE. The existing machine is lifecycle_merge_group; M-GATE must run on PR, so it lives in its own lifecycle_smoke module and reuses the shared helpers.
  • User-scope audit --ci and prune have no --global form, so they are project-only rules (user spine substitutes install --frozen / update).
  • Nightly job is standalone, not added to the pinned integration-summary lane contract.
  • Lifecycle evidence script not used. scripts/check_lifecycle_evidence.py rejects the current apm_cli.entrypoint launcher on main too (pre-existing, not CI-gated), so the [BUG] Global install succeeds with empty skill directories when HOME/APM_HOME uses a symlink alias #2867 witnesses were verified by running their tests directly.
  • Quality check unscoped. It consumes lifecycle-shard artifacts, so it runs in the lifecycle shards rather than the repository ratchet job (which must stay the sole caller of ratchet modules).

Benefits

  1. Generated lifecycle sequences: 310.5 s → 121.1 s locally (−61%).
  2. GATE PR nodes on CI x86 (lifecycle shard): spine 63.9 / 56.9 s, generated 28.3 / 40.1 s — about 190 s serial, each under the 100 s planner floor (audit estimate was 350 s).
  3. 107 GATE obligation cells covered deterministically per run, plus generated exploration.
  4. Failing cells pinned as strict expected failures, each naming an issue.

Validation

Commands and results (local, 2 CPUs)
APM_E2E_TESTS=1 uv run --extra dev python -m pytest -q --durations=20 \
  tests/integration/test_generated_gate_state_machine.py \
  tests/integration/test_generated_lifecycle_state_machine.py
19 passed in 287.48s

APM_GATE_MAX_EXAMPLES=25 uv run --extra dev python -m pytest -q -k sequences \
  tests/integration/test_generated_gate_state_machine.py
2 passed in 164.35s   (user 83.5 s, project 80.7 s)

uv run --extra dev python -m pytest -q tests/quality/
212 passed

Lint contract: ruff check, ruff format --check, pylint R0801, lint-auth-signals,
lint-architecture-boundaries: clean

CI on the PR head: see the checks tab; the lifecycle shards run the GATE nodes and GATE Cell Coverage checks the merged visited set.

Before/after (same machine), -k sequences_preserve:

node before after
project 129.7 s 33.3 s
global-canonical 91.5 s 44.0 s
global-aliased 89.4 s 43.7 s

Scenario Evidence

# Scenario (user promise) Principle Test Type
1 Unapproved executables never deploy; approving deploys exactly what was approved Secure by default test_generated_gate_state_machine.py::test_gate_mandatory_spine_covers_obligations e2e
2 Deny, revoke and org tightening never widen what is deployed Governed by policy same, plus test_gate_generated_sequences_keep_cross_cutting_laws e2e
3 Skipping the approval check is caught Secure by default test_gate_machine_kills_the_recorded_approval_mutant e2e
4 Generated lifecycle model keeps its invariants with the faster spine DevX test_generated_lifecycle_sequences_preserve_reference_model e2e

How to test

  • uv run --extra dev pytest -q tests/integration/test_generated_gate_state_machine.py → 5 passed; build/gate-cells/visited-main.json is written.
  • uv run --extra dev pytest -q tests/quality/test_gate_cell_obligations.py → 6 passed.
  • Remove one KnownCell → the spine fails naming that cell's violation.

Issue and approved scope

Issue: Refs #4031

Human scope-approval comment: #4031 (comment)

Does this PR complete the issue, or what remains? It delivers the approved scope; the product bugs it found are tracked separately (out of scope per the record).

Type of change

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

Testing

  • Tested locally
  • All existing tests pass (affected suites; see Validation)
  • Added tests for new functionality (if applicable)

Changelog

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

Spec conformance (OpenAPM v0.1)

Not applicable: no normative behaviour changes.

Co-authored-by: Copilot [email protected]

#4029)

Replace the wall-clock budget (elapsed < 1.0s) in the three parallel
registry tests with an in-flight probe: each stubbed lookup holds until
all three lookups are in flight together, and the test asserts
max_in_flight == 3. The result no longer depends on runner speed, and a
serialized executor (workers = 1) fails all three tests.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
The installer job evidence comes from main instead.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
The generated lifecycle machine replayed the mandatory spine in an
@initialize rule, so every Hypothesis example paid for the full spine.
Replay it once per variant world inside the first generated model, then
restore that post-spine world in place for each later example. Restoring
in place keeps the deployment roots' inodes, which local receipts bind to.

The new draws reach a no-op 'update' after a local edit; the model now
keeps the edit unless the update changes the revision, matching the CLI.

Local timing (2 CPUs), generated sequences: 310.5 s -> 121.1 s.

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

Add check_cross_cutting(before, after, result, model, op) with laws I1-I9
(lock/deployed agreement, idempotence, frozen/audit agreement, gate
monotonicity, write set, ASCII and JSON output, failure atomicity, exit
codes, duplicated output). Each law aliases the lifecycle-ledger
properties it refines. A known failing cell is a strict expected failure:
it must keep failing where the model says it does.

LifecycleStateSnapshot can now capture approval stores, the policy
cache, native plugin registries and per-adapter MCP config files. The
in-place world restore moves to tests/utils/world_snapshot.py.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
#4031)

M-GATE drives python -m apm_cli.cli through declare/undeclare of hook,
LSP and native-plugin content, approve, deny, revoke, org policy
tightening, install, install --frozen, update, uninstall, prune and
audit --ci, at project scope and at user scope with APM_HOME outside
HOME, checking the cross-cutting oracle after every step. Derandomized,
step_count 8; 4 examples per scope in the lifecycle shards.

Every step emits a cell: event; a session hook writes the visited set
and tests/quality checks it covers tests/quality/gate_cell_obligations.json.
A recorded mutant that skips the approval check must fail the machine.
Cells failing on main are strict expected failures naming their
tracking issues.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
…4031)

The nightly GATE run (25 examples per scope) moves out of the release
pipeline into .github/workflows/gate-machine-nightly.yml, which opens or
updates one tracking issue on failure and closes it on recovery.
build-release.yml is unchanged from main. Nightly examples are no longer
derandomized, so each night explores fresh sequences; PR runs stay
derandomized.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Every known cell now names the violation message it owns; any other
violation of the same law on that step still fails, including the
reference law in the machine. I4 reports one violation per surface and
checks mutating steps against a deployed ceiling recorded at each deny,
revoke or tightening. Spine coverage is asserted on the machine's own
visits. First-run creation of the user config and version-check cache is
not a write-set violation.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Each lifecycle shard uploads its visited GATE cells; a GATE Cell
Coverage job merges them and requires both scope spines to report and
to cover the obligation list.

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

Drop the deployed ceiling: I4 is checked relative to the model, so after
each successful mutating step nothing may be deployed that current
approvals and policy do not allow. A regression test narrows one surface
after approving a hook and requires the hook to stay deployed.

A project-scope step may now write only its receipts and bookkeeping
under HOME. The lifecycle-smoke aggregate requires the GATE cell
coverage job, whose checkout no longer persists credentials.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Admit only ~/.cache/apm and ~/.local/state/gh (plus their exact parent
directory entries) besides receipts and local state, so a write such as
~/.local/bin/evil fails the write-set law.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
gate-cell-a (#4041) and gate-cell-f (#4048) now pass on main, so their
strict expected failures are removed.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 20, 2026
@danielmeppiel Daniel Meppiel (danielmeppiel) changed the title [0.38] test(lifecycle): GATE state machine with cross-cutting oracle (#4031) test(lifecycle): GATE state machine with cross-cutting oracle (#4031) Nov 20, 2026

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

Active gaps in the lifecycle oracle and test harness allow unexpected command failures, leftover declarations or LSP claims, and unauthorized artifact changes to pass undetected.

5 open issues
What changed in this PR

Adds M-GATE to APM's lifecycle tests, exercising executable-trust transitions at project and user scope with shared invariants and tracked expected failures.

Changes:

  • Adds generated GATE sequences, deterministic coverage obligations, and an approval-bypass mutation test.
  • Reduces lifecycle replay overhead through in-place world restoration and replaces timing-based concurrency assertions.
  • Adds cross-shard coverage enforcement, nightly exploration, and contributor documentation.
File Description
tests/​utils/​world_snapshot.py Restores test worlds while preserving surviving directories.
tests/​utils/​lifecycle_state.py Captures approval, policy, plugin and MCP surfaces.
tests/​utils/​lifecycle_oracle.py Defines shared lifecycle laws and strict expected failures.
tests/​unit/​integration/​test_mcp_registry_parallel.py Verifies concurrency using overlapping calls instead of elapsed time.
tests/​quality/​test_gate_cell_obligations.py Validates obligations and recorded cell coverage.
tests/​quality/​test_ci_topology.py Requires GATE coverage in the lifecycle aggregate.
tests/​quality/​gate_cell_obligations.json Records required GATE transition cells.
tests/​integration/​test_generated_lifecycle_state_machine.py Reuses the mandatory spine and corrects no-op update modeling.
tests/​integration/​test_generated_gate_state_machine.py Adds spines, generated sequences and oracle regression tests.
tests/​integration/​conftest.py Writes per-process visited-cell artifacts.
tests/​integration/​_gate_machine_kit.py Implements isolated GATE worlds, rules and reference checks.
tests/​fixtures/​gate_mutants/​skip_approval_check.py Provides an approval-bypass mutant.
docs/​src/​content/​docs/​contributing/​integration-testing.md Documents GATE coverage and lifecycle replay changes.
CHANGELOG.md Records internal testing and concurrency-test changes.
.gitignore Excludes generated cell-coverage artifacts.
.github/​workflows/​gate-machine-nightly.yml Runs nightly exploration and maintains a failure-tracking issue.
.github/​workflows/​ci.yml Uploads shard artifacts and enforces combined cell coverage.
.github/​ci-durations/​linux-x86_64.json Adds GATE duration seeds and revises lifecycle estimates.

Review effort: Balanced

Comment thread tests/integration/_gate_machine_kit.py Resolved
Comment thread tests/integration/_gate_machine_kit.py Resolved
Comment thread tests/integration/_gate_machine_kit.py Resolved
Comment thread tests/utils/lifecycle_oracle.py Resolved
Comment thread tests/utils/lifecycle_oracle.py Resolved
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit f371fcb into main Nov 20, 2026
32 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-fictional-guacamole branch November 20, 2026 08:18
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
…ess, flip #4040 gate-cell-e

- Ledger row for #4045 (ownership.preserve_unowned, outcome.status_matches_state).
- test_lsp_only_uninstall_clears_every_harness_it_wrote: install to copilot,
  claude and opencode, uninstall, and assert no harness config keeps the
  server, at project scope and at -g with APM_HOME outside HOME.
- #4040 GATE machine: gate-cell-e (#4045) is no longer a known failure.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
#4058 fixes #4042, so #4040's strict known failure for gate cell b
would XPASS; remove it.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
…orks (#4042)

#4040's spine reached update and deny with the plugin still pending
because approve never took effect for a root-mcp.json Agent Plugin.
With #4042 fixed the plugin is approved there, so the required
update|plugin|pending and deny|plugin|pending cells went unvisited.
Revoke and reinstall the plugin before update to keep them covered.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
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.

2 participants