Skip to content

fix(preflight): address #3580 review findings - #3594

Closed
Daniel Meppiel (danielmeppiel) wants to merge 1 commit into
mainfrom
danielmeppiel-preflight-review-fixes
Closed

Daniel Meppiel (danielmeppiel) wants to merge 1 commit into
mainfrom
danielmeppiel-preflight-review-fixes

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

TL;DR

Fixes the 7 Copilot review findings left open on merged #3580 (the repo-local preflight-gate package). Four close gate bypasses (launcher fast path, git fetch chaining, implicit pushes under push config, API PR create/ready not bound to the real head). Two harden the runner (resistant child processes, stale dist/ binary). One de-flakes a test. The README also gets a short "Activation by Copilot surface" section.

Fixes #3584

Problem (WHY)

  • The gate's promise is "no push/PR of an unverified commit". Each of findings 1-4 is a concrete command that published an unverified commit while a green receipt existed for a different commit or was never checked.
  • The runner's promise is "the receipt reflects this source, and cancellation stops all work". Findings 5-6 broke that: a TERM-immune child kept running after cancel or timeout, and lifecycle checks could run an old dist/ build.
  • Finding 7: a runner test asserted that every check ran but used fail-fast, so it raced. On a single core it failed every time.
  • Anchor: a guard has to be checked by running something, not by trusting the model. PROSE says "Grounding outputs in deterministic tool execution transforms probabilistic generation into verifiable action." Each fix below makes one more publish path go through that check.

Review thread → fix → regression test

# Thread on #3580 Fix Regression test
1 r4547846242 launcher: update_pull_request in a PR title masks gh pr create gate-launcher.sh checks the push, update-tool and gh patterns in three separate case blocks, so one match cannot hide another test_launcher.py::test_gh_pr_create_mentioning_update_tool_name_starts_python
2 r4547846299 git fetch x:x && git push x fetch removed from READ_ONLY_GIT; chain tests and README allowlist updated test_chain.py::test_fetch_moving_branch_before_push_is_denied (+ 2 new rows in test_mutating_segment_before_publish_is_denied)
3 r4547846333 implicit push under push.default=matching / remote.<name>.push An implicit push (no refspec) is denied when the effective push.default is matching or any remote.*.push is set. The check reads git -c overrides and two local git config calls (no network). --config-env and GIT_CONFIG_* assignments on the command deny an implicit push outright. An explicit refspec is unaffected test_gate.py::test_implicit_push_with_push_default_matching_is_denied, test_gate.py::test_implicit_push_with_remote_push_refspecs_is_denied
4 r4547846380 API PR create/ready leave head unset Create (gh api POST pulls, GraphQL createPullRequest incl. $variables): keeps the requested head/base. The head must resolve to a branch whose commit equals receipt.head (local branch first, then origin/<branch>, as for gh pr create -H), and the base must equal the receipt base. A missing or --input head is denied. Ready (gh pr ready [n|url] [-R], gh api PATCH pulls/N, markPullRequestReadyForReview, *update_pull_request): gh pr view … --json headRefOid,baseRefName (or a GraphQL node lookup) reads the PR's actual head and base, capped at 8 s with the process group killed on timeout. The command is denied when no PR can be found, on timeout, or when the tool call has no PR number test_gate.py::test_api_pr_creation_binds_requested_head_and_base, ::test_ready_operations_bind_the_prs_actual_head, ::test_default_pr_lookup_asks_gh_for_the_pr_head, ::test_pr_lookup_timeout_denies_within_hook_budget
5 r4547846438 proc.wait() skips SIGKILL _kill_group: sends SIGTERM, then polls the leader and the group for the grace period, then always sends SIGKILL to the group test_concurrency.py::test_cancel_kills_child_that_ignores_sigterm
6 r4547846482 conftest prefers dist/ over PATH Verified tests/integration/conftest.py::_resolve_apm_binary checks APM_BINARY_PATH first, then dist/, then PATH. The runner now pins APM_BINARY_PATH=<project env>/bin/apm in every check environment test_runner.py::test_lifecycle_checks_pin_project_apm_over_stale_dist
7 r4547846540 test races under fail-fast --keep-going test_runner.py::test_failure_still_runs_shared_checks_of_every_tier (updated)

Tests 5 and 6 were run against the pre-fix preflight.py and both fail there; both pass with the fix.

How verification now binds each gated action

Legend: what the gate compares against the receipt once the receipt itself is fresh, green and complete.

flowchart TD
    A["gated action, fresh green receipt"] --> B{"kind"}
    B -->|"git push, no refspec"| C{"push.default=matching<br/>or remote.*.push set?"}
    C -->|yes| X["deny: name the source"]
    C -->|no| H["HEAD == receipt.head?"]
    B -->|"PR create: gh pr create, gh api POST pulls,<br/>GraphQL createPullRequest, *create_pull_request"| R["requested head branch -> commit<br/>== receipt.head, base == receipt base"]
    B -->|"PR ready: gh pr ready, gh api PATCH pulls/N,<br/>markPullRequestReadyForReview, *update_pull_request"| L["gh lookup of the PR (8 s cap)"]
    L -->|"unknown / timeout"| X2["deny: PR cannot be established"]
    L -->|"headRefOid, baseRefName"| P["PR head == receipt.head,<br/>PR base == receipt base"]
Loading

Implementation (HOW)

File Change
packages/preflight-gate/.apm/hooks/scripts/gate-launcher.sh Three separate case blocks (finding 1).
packages/preflight-gate/.apm/hooks/scripts/gate.py READ_ONLY_GIT without fetch. git -c / --config-env collection and Action.implicit, checked by _implicit_push_problem. _gh_api_actions / _api_fields / _graphql_arg keep the API head and base. PRTarget + lookup_pr, injectable as GateConfig.pr_lookup, power _verify_existing_pr. _resolve_head_arg resolves refs/heads/<b>, then refs/remotes/origin/<b>, and no longer accepts arbitrary revs.
packages/preflight-gate/.apm/hooks/scripts/preflight.py _kill_group + _group_alive (finding 5); APM_BINARY_PATH pin (finding 6).
packages/preflight-gate/README.md Allowlist without fetch, new Limits bullets (implicit push, API create, ready lookup) replacing "gh pr ready does not verify the PR base", and Activation by Copilot surface: Copilot CLI and cloud agent load repo .github/hooks. The desktop app needs a one-file user-level shim (~/.copilot/hooks/apm-preflight-gate-shim.json) that runs the repo's own launcher only inside repos that have the package. Hooks are read at session start.
packages/preflight-gate/tests/* One regression per finding (table above); gh pr ready in the existing allow test now uses a fake lookup.
.github/hooks/scripts/preflight-gate/… Byte-identical redeploy of the 3 changed scripts.
apm.lock.yaml Only the 6 per-file sha256 lines for the 3 deployed scripts and the ./packages/preflight-gate content_hash. No apm install churn.

Trade-offs

  • Implicit push: deny instead of resolving every source. Fully modelling matching / remote.*.push means re-implementing git's refspec and upstream rules in the hook. Denying and telling the agent to run git push origin HEAD is exact and costs at most 2 local git config calls. Any remote.*.push triggers the deny, not just the target remote's (slightly broader than necessary, and rare).
  • Ready operations make a network call. The PR's real head is only known to GitHub. The lookup is capped at 8 s (hook timeout 15 s) and kills its process group on timeout. Offline, or an unknown PR, means deny, not fail-open, as the finding asked. Plain gh pr ready also looks up the current-branch PR, because local HEAD can be ahead of the PR head.
  • *update_pull_request without a PR number is denied rather than guessed from the current branch.
  • Remote-tracking head fallback kept. A creation head may resolve via origin/<branch> when no local branch exists, as gh pr create -H already did. The commit equality check is unchanged.

Benefits

  1. 4 bypasses closed: each now has a test that denies the exact command from the review.
  2. Cancellation and timeouts leave no surviving child processes. The new test fails on the old code.
  3. Lifecycle checks always exercise the source being committed, never a stale dist/ build. The new test fails on the old code.
  4. The racy test now passes on a single core.
  5. Desktop-app users have a documented way to turn the gate on.

Validation

Commands and results
  • .venv/bin/pytest packages/preflight-gate/tests -q -p no:xdist → 364 passed, 2 failed in one full run under host load avg ~100-260 on 8 cores: test_phase_a_failure_cancels_siblings_and_skips_phase_b (wall-clock < 20 s bound hit 28 s; it also failed the same way on unmodified main in this session) and test_cancellation_kills_the_whole_process_group (pidfile not yet written before the cancel). Re-running tests/test_concurrency.py → 14 passed
  • .venv/bin/ruff check packages/preflight-gate && .venv/bin/ruff format --check packages/preflight-gate → All checks passed! / 15 files already formatted
  • Pre-fix proof: preflight.py swapped back to HEAD~, then pytest …::test_cancel_kills_child_that_ignores_sigterm …::test_lifecycle_checks_pin_project_apm_over_stale_dist → 2 failed; with the fix → pass
  • .venv/bin/pytest tests/unit/test_triage_advisory.py::test_local_package_snapshots_match_lockfile -q → 1 passed
  • .venv/bin/apm audit --ci → [*] All 10 check(s) passed
  • Deployed copy: cmp of every file in packages/preflight-gate/.apm/hooks/scripts/ vs .github/hooks/scripts/preflight-gate/.apm/hooks/scripts/ → identical

Dogfood (deployed runner at the PR head, from the repo root):

[*] phase A: 17 check(s) concurrently (4 job(s))
  [+] T0 bundle.stale (0.1s)
  [+] T0 ci.drift (0.0s)
  [+] T0 git.merge-conflict (0.2s)
  [+] T0 git.diff-check (0.6s)
  [+] T0 guard.ascii (0.3s)
  [+] T0 lint.ruff (3.3s)
  [+] T0 lint.ruff-format (3.2s)
  [+] T0 lint.yaml-io (3.0s)
  [+] T0 lint.file-length (98.8s)
  [+] T0 lint.relpath (1.0s)
  [+] T0 lint.pylint-dup (324.5s)
  [+] T0 lint.auth-signals (4.4s)
  [+] T0 lint.arch-boundaries (62.9s)
  [+] T0 spec.mode-b (9.3s)
  [+] T1 arch.contracts (674.7s)
  [+] T1 apm.self-check (22.2s)
  [+] T1 apm.lock-snapshots (14.8s)
  [i] wall time: phase A 691.9s, phase B 0.0s
[!] host grep lacks -P; grep -P steps ran through ggrep
[!] tolerated lock drift (APM_PREFLIGHT_LOCK_TOLERATE / git config apm.preflight.lockTolerate): filelock locked 4.2.10, installed 4.2.9
[+] preflight PASS; receipt <git-dir>/apm-preflight/receipt.json

Scenario evidence

Scenario (user promise) Test
An agent cannot create a PR by mentioning a tool name in its title test_gh_pr_create_mentioning_update_tool_name_starts_python
An agent cannot move a branch with fetch and publish it in the same call test_fetch_moving_branch_before_push_is_denied
An implicit push cannot publish branches other than the verified HEAD test_implicit_push_with_push_default_matching_is_denied, test_implicit_push_with_remote_push_refspecs_is_denied
An API-created or ready-marked PR has a verified head on the verified base test_api_pr_creation_binds_requested_head_and_base, test_ready_operations_bind_the_prs_actual_head, test_default_pr_lookup_asks_gh_for_the_pr_head, test_pr_lookup_timeout_denies_within_hook_budget
Cancelling a check stops all of its processes test_cancel_kills_child_that_ignores_sigterm
A receipt never reflects a stale binary test_lifecycle_checks_pin_project_apm_over_stale_dist

How to test

  • .venv/bin/pytest packages/preflight-gate/tests -q -p no:xdist (the package's session cleanup is not xdist-safe)
  • git -c push.default=matching push with a fresh receipt → deny naming git push origin HEAD
  • gh pr ready <n> for a PR whose head is not the verified commit → deny with the PR head sha
  • python3 .github/hooks/scripts/preflight-gate/.apm/hooks/scripts/preflight.py → PASS

Description

Follow-up to #3580 that fixes its 7 unresolved Copilot review findings (see the table above), plus a README activation section.

Issue and approved scope

Issue: Fixes #3584

Human scope-approval comment: maintainer request on #3580, "if those are legit, fix them" (recorded in #3584; human direction and review contact Daniel Meppiel (@danielmeppiel)). All 7 findings were triaged as legitimate.

Does this PR complete the issue, or what remains? It completes #3584.

Type of change

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

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour.

- launcher: check push / update-tool / gh publish patterns independently
- gate: drop `git fetch` from the read-only chain allowlist
- gate: deny implicit pushes under push.default=matching or
  remote.<name>.push (incl. -c, --config-env, GIT_CONFIG_* overrides)
- gate: bind API PR creation to its requested head/base and ready
  operations to the PR's actual head/base (bounded gh lookup); deny when
  the target cannot be established
- runner: SIGKILL the whole check process group after the grace period
  even when the leader already exited
- runner: pin APM_BINARY_PATH to the project env's apm so lifecycle checks
  never exercise a stale dist/ build
- tests: run the shared-checks test with --keep-going
- README: document the above and activation per Copilot surface

Fixes #3584

Co-authored-by: Copilot App <[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

Critical target-validation gaps allow multi-mutation queries, unresolved or changed bases, and repository overrides to authorize publishing unverified targets using verification for a different target.

4 open issues
What changed in this PR

This follow-up to #3580 addresses #3584 by strengthening the repo-local preflight gate's commit checks and runner reliability.

Changes:

  • Tightens publish detection, implicit-push checks, and PR head/base validation.
  • Hardens process-group cancellation, pins the lifecycle-test binary, and adds regression coverage.
  • Documents Copilot activation, updates deployed scripts, and refreshes lock hashes.
File Description
packages/​preflight-gate/​tests/​test_runner.py Tests binary pinning and removes a fail-fast race.
packages/​preflight-gate/​tests/​test_launcher.py Covers tool-name text masking PR creation.
packages/​preflight-gate/​tests/​test_gate.py Adds push-config, PR-target, and lookup-timeout coverage.
packages/​preflight-gate/​tests/​test_concurrency.py Tests cancellation of SIGTERM-resistant children.
packages/​preflight-gate/​tests/​test_chain.py Covers denial of fetch-before-publish chains.
packages/​preflight-gate/​README.md Documents activation and updated gate limitations.
packages/​preflight-gate/​.apm/​hooks/​scripts/​preflight.py Strengthens cancellation and pins the project binary.
packages/​preflight-gate/​.apm/​hooks/​scripts/​gate.py Adds push-config checks and PR-target verification.
packages/​preflight-gate/​.apm/​hooks/​scripts/​gate-launcher.sh Checks publish patterns independently.
apm.lock.yaml Refreshes package and deployed-script hashes.
.github/​hooks/​scripts/​preflight-gate/​.apm/​hooks/​scripts/​preflight.py Deploys the runner changes.
.github/​hooks/​scripts/​preflight-gate/​.apm/​hooks/​scripts/​gate.py Deploys the gate changes.
.github/​hooks/​scripts/​preflight-gate/​.apm/​hooks/​scripts/​gate-launcher.sh Deploys the launcher correction.

Review effort: Balanced

fields, opaque = _api_fields(api_args)
if path == "graphql":
query = fields.get("query") or " ".join(api_args)
found = set(GRAPHQL_PR_MUTATION.findall(" ".join(api_args)))
Comment on lines +665 to +667
if head is None:
action.deny = "cannot establish the head of the GraphQL createPullRequest"
action.fix = FIX_PR_CREATE_TARGET
argv += ["--json", "headRefOid,baseRefName"]
if target.repo:
argv += ["-R", target.repo]
env = dict(os.environ, GH_PROMPT_DISABLED="1", GH_NO_UPDATE_NOTIFIER="1", NO_COLOR="1")
f"commit {verified[:12]}"
)
return why, (FIX_PR_PUSH_FIRST if current == verified else None)
return _base_problem(str(info["baseRefName"]), receipt, hint)
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Closing: the preflight-gate hook from #3580 was reverted in #3598, and its local-preflight approach is dropped for now (Daniel Meppiel (@danielmeppiel), 2026-11-12). These review fixes no longer apply.


Posted with AI assistance on behalf of Daniel Meppiel (@danielmeppiel).

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.

fix(preflight): address #3580 review findings

2 participants