~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
feat(ci): add CI/CD health analyzer with live scanner status (#5523)
LearningCircuita7764484c181
diff --git a/.github/CI_HEALTH.md b/.github/CI_HEALTH.mdnew file mode 100644index 0000000000..bc949cd490--- /dev/null+++ b/.github/CI_HEALTH.md@@ -0,0 +1,112 @@+# CI/CD Configuration Health++> Auto-generated by `scripts/generate_ci_health.py` (run by+> `.github/workflows/ci-health-report.yml`). This reports+> **static configuration health** plus a **live scanner status**+> section; only the static half is scored. For live run status+> across every workflow see+> [`docs/ci/workflow-status.md`](../docs/ci/workflow-status.md).++- **Report baseline:** `ebfaf0795`+- **Workflows analyzed:** 68+- **Overall score:** **100/100** (grade **A**)+- **Findings:** 0++---++## Summary++| Check | Result | Detail |+|---|---|---|+| Actions pinned by SHA | PASS | 100% (weight 35) |+| Explicit permissions | PASS | 100% (weight 15) |+| Harden-runner first | PASS | 100% (weight 15) |+| Safe checkouts | PASS | 100% (weight 10) |+| Pre-commit | PASS | 100% (weight 10) |+| Required scanners | PASS | 100% (weight 15) |++## Headline counts++- Non-local action references: **407/407 pinned**+- Checkout occurrences: **87/87 safe**+- Non-exempt jobs: **105**+- Jobs missing harden-runner as step 0: **0**+- Jobs without explicit permissions: **0**++## Security-scanner coverage++| Category | Requirement | Status | Evidence |+|---|---|---|---|+| Dependencies | required | SATISFIED | npm-audit.yml, osv-scanner-scheduled.yml, osv-scanner.yml, retirejs.yml, .github/dependabot.yml |+| SAST | required | SATISFIED | bearer.yml, codeql.yml, devskim.yml, semgrep.yml |+| Secrets | required | SATISFIED | gitleaks-main.yml, gitleaks.yml |+| Supply-Chain | required | SATISFIED | ossf-scorecard.yml, sbom.yml |+| Workflow-Security | required | SATISFIED | zizmor-security.yml |+| Containers | optional | SATISFIED | container-security.yml, dockle.yml, grype.yml, hadolint.yml |+| DAST | optional | SATISFIED | nuclei.yml, owasp-zap-scan.yml |+| IaC | optional | SATISFIED | checkov.yml |+++## Live scanner status++_Queried via `gh run list` (up to 5 most recent `main` runs per+scanner workflow; pull-request events excluded because a fork+PR branch may itself be named `main`). A scanner with no+default-branch history of its own is read through the gate that+calls it, and each gate run is resolved to that scanner's OWN+job conclusions (`gh run view --json jobs`) — the gate's roll-up+is never shown under a scanner's name; `UNRESOLVED` covers+causes such as a job list that could not be read or didn't+name this scanner, or the live time budget running out+before it could be checked. Runtime status, not static+configuration — a failed run here may be transient, and it+does not affect the score._++| Workflow | Latest | Last run | Recent runs (newest first) | Note |+|---|---|---|---|---|+| `bearer.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | — |+| `checkov.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | gated by `release-gate.yml` (this scanner's own jobs) |+| `codeql.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | — |+| `container-security.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | gated by `release-gate.yml` (this scanner's own jobs) |+| `devskim.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | — |+| `dockle.yml` | PASS | 2026-09-22 | PASS PASS PASS PASS PASS | — |+| `gitleaks-main.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | gated by `release-gate.yml` (this scanner's own jobs) |+| `gitleaks.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS FAIL | — |+| `grype.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | gated by `release-gate.yml` (this scanner's own jobs) |+| `hadolint.yml` | PASS | 2026-09-22 | PASS PASS PASS PASS PASS | — |+| `npm-audit.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | gated by `release-gate.yml` (this scanner's own jobs) |+| `nuclei.yml` | NO RUNS | — | — | no qualifying run on `main` in the query window |+| `ossf-scorecard.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | — |+| `osv-scanner-scheduled.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | — |+| `osv-scanner.yml` | PASS | 2026-09-21 | PASS PASS PASS | — |+| `owasp-zap-scan.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | gated by `release-gate.yml` (this scanner's own jobs) |+| `retirejs.yml` | PASS | 2026-09-21 | PASS PASS PASS PASS PASS | — |+| `sbom.yml` | PASS | 2026-09-23 | PASS PASS PASS PASS PASS | — |+| `semgrep.yml` | PASS | 2026-09-24 | PASS PASS PASS PASS PASS | gated by `release-gate.yml` (this scanner's own jobs) |+| `zizmor-security.yml` | PASS | 2026-09-21 | PASS PASS PASS PASS PASS | — |++_`NO RUNS` means no qualifying default-branch run was found — absence of evidence, not a healthy scanner._++## Pre-commit hooks++76 hooks configured in `.pre-commit-config.yaml`.+0 hooks use a tag or otherwise non-SHA static revision.++## Recent CI/CD config changes++| Commit | Date | Author | Subject |+|---|---|---|---|+| `ebfaf0795` | 2026-09-25 | LearningCircuit | fix(ci): fix falsifiable no-success assertion; scope UNRESOLVED to what was actually checked |+| `30990abd8` | 2026-09-24 | LearningCircuit | fix(ci): stale-when-no-success, budget-truncated gate resolution, job-name prefixes |+| `5d9515f31` | 2026-09-24 | LearningCircuit | fix(ci): resolve gated scanners per job in the CI health report |+| `11f632b28` | 2026-09-24 | LearningCircuit | fix(ci): make the CI health report's live section trustworthy |+| `241aa482d` | 2026-09-19 | LearningCircuit | fix(ci): recognize self-repository action references |+| `d325d3c6a` | 2026-09-07 | LearningCircuit | Validate CI step commands and reusable job structure |+| `b03f83a55` | 2026-09-07 | LearningCircuit | Reject malformed CI health inputs and bound live scanner queries |+| `f28ea21ba` | 2026-09-07 | LearningCircuit | Merge remote-tracking branch 'origin/main' into gate-5523-20260907 |+| `00c92d3bf` | 2026-09-07 | dependabot[bot] | chore(deps): bump zizmorcore/zizmor-action from 0.6.2 to 0.6.3 (#6319) |+| `2a9f94dfa` | 2026-09-07 | dependabot[bot] | chore(deps): bump actions/checkout from 7.0.0 to 7.0.1 (#6320) |++---++Regenerate locally: `python scripts/generate_ci_health.py`diff --git a/.github/workflows/ci-health-report.yml b/.github/workflows/ci-health-report.ymlnew file mode 100644index 0000000000..f76271bc56--- /dev/null+++ b/.github/workflows/ci-health-report.yml@@ -0,0 +1,156 @@+name: CI/CD Health Report++# Regenerates .github/CI_HEALTH.md (static config-health: action pinning,+# permissions hygiene, harden-runner coverage, pre-commit hook freshness,+# security-scanner coverage, recent CI/CD changes) and opens a PR with the+# updated report.+#+# This is COMPLEMENTARY to check-workflow-status.yml + docs/ci/workflow-status.md,+# which track LIVE workflow run status via the GitHub API. This workflow's score+# is entirely CONFIGURATION-based; the `--live` section below additionally reads+# recent scanner run conclusions on main via `gh run list`, which costs a handful+# of Actions API calls and needs `actions: read` plus GH_TOKEN (the default+# GITHUB_TOKEN). Live results are reported, never folded into the score.+#+# This workflow keeps the status doc fresh on a schedule.++on:+ schedule:+ - cron: '0 6 * * 1' # weekly (Mon 06:00 UTC). The static half of the+ # output is byte-stable, but the --live table+ # moves whenever a scanner has run since the last+ # report, so a scheduled run normally DOES open a+ # PR -- that live movement is the reason to run+ # this on a cadence at all. Only a week with+ # neither a config change nor a scanner run+ # produces no PR.+ workflow_dispatch:+ push:+ branches: [main]+ paths:+ - '.github/workflows/**'+ - '.pre-commit-config.yaml'+ - '.github/dependabot.yml'+ - 'scripts/generate_ci_health.py'+ # NOTE: .github/CI_HEALTH.md is intentionally NOT a trigger. Including+ # it would re-run this workflow the moment its own PR merges, creating a+ # report-update loop. The schedule + config-path pushes are enough.++permissions: {} # Minimal top-level for OSSF Scorecard Token-Permissions.++concurrency:+ group: ci-health-report+ cancel-in-progress: true++jobs:+ report:+ name: Generate CI/CD health report+ runs-on: ubuntu-latest+ timeout-minutes: 5+ permissions:+ contents: write # commit the regenerated report+ pull-requests: write # open/update the PR carrying it+ actions: read # --live reads run conclusions via `gh run list`++ steps:+ - name: Harden the runner (Audit all outbound calls)+ uses: step-security/harden-runner@b09bb98e06d4d774595224525879c09bc6e98c40 # v2.20.1+ with:+ egress-policy: audit++ - name: Checkout+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ persist-credentials: false+ ref: main+ # Full history so generate_ci_health.py's `git log` over CI/CD+ # config paths resolves recent changes without an extra fetch.+ fetch-depth: 0++ - name: Set up Python+ uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0+ with:+ python-version: '3.12'+ cache: 'pip'++ - name: Install PyYAML+ # Version-pinned (== matches the lock used by check-workflow-status.yml).+ # Floating ~= ranges can pick up yanked/replaced patch versions silently.+ run: pip install pyyaml==6.0.3++ - name: Generate .github/CI_HEALTH.md+ id: gen+ # --github-output writes the markdown file AND appends+ # score/grade/findings_* to $GITHUB_OUTPUT, so the PR title/body and+ # the run summary read structured fields instead of grepping markdown.+ # This job publishes the report; no step here fails on its findings,+ # and nothing outside this job consumes the outputs, so every one of+ # them is read by the summary step below. tests/ci/test_ci_health.py+ # asserts that structurally, over the parsed `run:` commands.+ #+ # --live is what makes the report mean something. The static checks+ # score configuration, so they happily report 100/100 while a scanner+ # workflow is actually red -- the exact blind spot this tool was built+ # for. It needs `actions: read` above and GH_TOKEN below; without+ # either, live entries are explicitly marked unavailable. All queries+ # share a 120-second budget, leaving time to publish unavailable entries+ # within this job's five-minute limit even when gh calls time out.+ env:+ GH_TOKEN: ${{ github.token }}+ run: python scripts/generate_ci_health.py --github-output --live++ - name: Post run summary+ run: |+ # shellcheck disable=SC2016+ {+ echo '## CI/CD Configuration Health'+ echo+ echo "**Score:** ${{ steps.gen.outputs.score }}/100 (grade ${{ steps.gen.outputs.grade }})"+ echo+ echo "**Findings:** ${{ steps.gen.outputs.findings }} total — ${{ steps.gen.outputs.findings_critical }} critical, ${{ steps.gen.outputs.findings_high }} high"+ echo+ echo 'Generated by `scripts/generate_ci_health.py`. See `.github/CI_HEALTH.md` for the full report.'+ echo+ cat .github/CI_HEALTH.md+ } >> "$GITHUB_STEP_SUMMARY"++ - name: Check for changes+ id: diff+ run: |+ if git diff --quiet -- .github/CI_HEALTH.md; then+ echo "changed=false" >> "$GITHUB_OUTPUT"+ echo "No changes to CI_HEALTH.md."+ else+ echo "changed=true" >> "$GITHUB_OUTPUT"+ echo "CI_HEALTH.md changed — opening/updating PR."+ fi++ - name: Open / update pull request+ uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1+ if: steps.diff.outputs.changed == 'true'+ with:+ branch: ci-health-report+ title: "🤖 Update CI/CD health report (score ${{ steps.gen.outputs.score }}/100)"+ body: |+ Auto-generated update to `.github/CI_HEALTH.md` from the+ `CI/CD Health Report` workflow.++ **Score:** ${{ steps.gen.outputs.score }}/100 (grade ${{ steps.gen.outputs.grade }})++ This carries **static configuration health** (action pinning,+ permissions, harden-runner coverage, pre-commit hook freshness,+ scanner coverage, recent CI/CD changes) **plus a live scanner+ status** section: recent `main` run conclusions per scanner+ workflow, read with `gh run list` and, for scanners run through a+ release gate, resolved to that scanner's own jobs. Only the static+ half is scored. For live status across *every* workflow see+ [`docs/ci/workflow-status.md`](../docs/ci/workflow-status.md).++ Review the **Files changed** tab for the full diff.+ sign-commits: true+ add-paths: .github/CI_HEALTH.md+ commit-message: "🤖 Update CI/CD health report"+ labels: maintenance+ draft: false+ base: main+ delete-branch: falsediff --git a/changelog.d/5523.feature.md b/changelog.d/5523.feature.mdnew file mode 100644index 0000000000..897f715c22--- /dev/null+++ b/changelog.d/5523.feature.md@@ -0,0 +1 @@+Added `scripts/generate_ci_health.py`, a CI/CD configuration-health analyzer that scores action pinning, permission hygiene, harden-runner coverage, pre-commit hook freshness and security-scanner coverage, plus an optional `--live` section reporting each scanner's most recent default-branch runs (a scanner with no default-branch history of its own is read through the gate that calls it, and each gate run is resolved to that scanner's own job conclusions rather than the gate's roll-up, so a red gate summary never renders as a red scanner and an unresolvable job list renders `UNRESOLVED`; runs older than a staleness threshold — `max(60 days, 2 x cron cadence)` measured on the last successful run, similar to but not identical with `docs/ci/workflow-status.md` (workflows without a cron get the 60-day floor, gated scanners use the gate's cadence, and there is no new-file exemption) — are reported as `STALE`, never `PASS`; a scanner with no default-branch run in the window is a finding and does not count as coverage for its category). Workflows named `*-test.yml`/`*-tests.yml` are not treated as scanners. The report is regenerated weekly by `.github/workflows/ci-health-report.yml`.diff --git a/docs/ci/workflow-status.md b/docs/ci/workflow-status.mdindex c9d4871a68..2c7fbb5654 100644--- a/docs/ci/workflow-status.md+++ b/docs/ci/workflow-status.md@@ -22,10 +22,10 @@workflow is disabled in the GitHub UI. **Stale** = scheduled triggerbut no successful run within 2× its cron cadence (and ≥60 days). Thethree top sections are the action items.-- Reusable workflows (those triggered only by `workflow_call:`) show- their **gated** run — the most recent run of their parent (release.yml,- release-gate.yml, ci-gate.yml) that included them — not their own- empty direct-run history.+- Reusable workflows (whose only non-gate triggers are `workflow_dispatch`+ or pull-request events) show their **gated** run — the most recent run+ of their parent (release.yml, release-gate.yml, ci-gate.yml) that+ included them — not their own empty direct-run history.<!-- BEGIN GENERATED -->@@ -100,6 +100,7 @@| Workflow | Last activity | Trigger | Live badge ||---|---|---|---|+| `ci-health-report.yml` | never | schedule(0 6 * * 1), manual, push:main | [](https://github.com/LearningCircuit/local-deep-research/actions/workflows/ci-health-report.yml?query=event%3Aschedule) || `compose-published-smoke.yml` | last 30 days | manual, schedule(0 5 * * 1) | [](https://github.com/LearningCircuit/local-deep-research/actions/workflows/compose-published-smoke.yml?query=event%3Aschedule) || `fuzz.yml` | last 30 days | schedule(0 0 * * 0), manual, PR | [](https://github.com/LearningCircuit/local-deep-research/actions/workflows/fuzz.yml?query=event%3Aschedule) || `gitleaks.yml` | last 30 days | PR, manual, schedule(0 3 * * *) | [](https://github.com/LearningCircuit/local-deep-research/actions/workflows/gitleaks.yml?query=event%3Aschedule) |diff --git a/scripts/generate_ci_health.py b/scripts/generate_ci_health.pynew file mode 100644index 0000000000..a5001b3ea2--- /dev/null+++ b/scripts/generate_ci_health.py@@ -0,0 +1,2359 @@+#!/usr/bin/env python3+"""Generate ``.github/CI_HEALTH.md`` — a static configuration-health report.++This is *complementary* to ``docs/ci/workflow-status.md``:++* ``docs/ci/workflow-status.md`` — LIVE run status, generated by+ ``scripts/generate_workflow_status.py`` using the GitHub API.+* ``.github/CI_HEALTH.md`` (this script) — STATIC configuration health:+ GitHub-Actions pinning, permission hygiene, harden-runner coverage,+ pre-commit hook freshness, security-scanner coverage, and a recent-changes+ log for CI/CD config files.++Runs in CI (``.github/workflows/ci-health-report.yml``) and locally:++ python scripts/generate_ci_health.py # write .github/CI_HEALTH.md+ python scripts/generate_ci_health.py --stdout # print instead of write+ python scripts/generate_ci_health.py --check # exit 1 on high/critical findings++Only depends on the standard library + PyYAML (already used elsewhere in CI).+"""++from __future__ import annotations++import argparse+import datetime+import importlib+import importlib.util+import json+import os+import pathlib+import re+import subprocess+import sys+import time+from collections.abc import Iterator+from dataclasses import dataclass, replace+from typing import (+ Final,+ Literal,+ NotRequired,+ Protocol,+ TypeAlias,+ TypedDict,+ runtime_checkable,+)++ROOT: Final = pathlib.Path(__file__).resolve().parents[1]+WORKFLOWS_DIR: Final = ROOT / ".github" / "workflows"+PRECOMMIT_FILE: Final = ROOT / ".pre-commit-config.yaml"+DEPENDABOT_FILE: Final = ROOT / ".github" / "dependabot.yml"+OUT_FILE: Final = ROOT / ".github" / "CI_HEALTH.md"+CI_PATHS: Final = (+ ".github/workflows/",+ ".pre-commit-config.yaml",+ ".github/dependabot.yml",+ "scripts/generate_ci_health.py",+)+SHA_PIN_RE: Final = re.compile(r"[0-9a-f]{40}", re.IGNORECASE)+DOCKER_DIGEST_RE: Final = re.compile(r"sha256:[0-9a-f]{64}")+FLOATING_REVS: Final = frozenset(+ {"main", "master", "latest", "develop", "stable", "head"}+)+PRIVILEGED_PIN_RE: Final = re.compile(r"publish|release|deploy|docker-publish")+PRIVILEGED_PERMS_RE: Final = re.compile(r"publish|release|deploy|gate")++Severity: TypeAlias = Literal["critical", "high", "medium", "low"]+YamlScalar: TypeAlias = (+ None | bool | int | float | str | datetime.date | datetime.datetime+)+YamlValue: TypeAlias = (+ YamlScalar | list["YamlValue"] | dict[YamlScalar, "YamlValue"]+)+++@runtime_checkable+class YamlApi(Protocol):+ YAMLError: type[Exception]++ def safe_load(self, stream: str) -> YamlValue: ...++ def safe_load_all(self, stream: str) -> Iterator[YamlValue]: ...+++try:+ _yaml_module = importlib.import_module("yaml")+except ImportError: # pragma: no cover+ sys.exit("PyYAML is required: pip install pyyaml==6.0.3")+if not isinstance(_yaml_module, YamlApi): # pragma: no cover+ sys.exit("Installed PyYAML does not provide the expected loader API")+YAML: Final[YamlApi] = _yaml_module++REQUIRED_SCANNERS: Final[dict[str, tuple[str, ...]]] = {+ "Dependencies": ("osv-scanner", "dependabot", "npm-audit", "retirejs"),+ "SAST": ("codeql", "semgrep", "bandit", "devskim", "bearer"),+ "Secrets": ("gitleaks", "trufflehog"),+ "Supply-Chain": ("ossf-scorecard", "sbom"),+ "Workflow-Security": ("zizmor",),+}+OPTIONAL_SCANNERS: Final[dict[str, tuple[str, ...]]] = {+ "Containers": (+ "grype",+ "trivy",+ "dockle",+ "hadolint",+ "container-security",+ ),+ "DAST": ("owasp-zap", "nuclei"),+ "IaC": ("checkov",),+}+++@dataclass(frozen=True, slots=True)+class Finding:+ severity: Severity+ area: str+ file: str+ message: str+ fixable: bool = True+++@dataclass(frozen=True, slots=True)+class WorkflowFile:+ name: str+ uses: int+ pinned: int+ unpinned: tuple[str, ...]+++@dataclass(frozen=True, slots=True)+class WorkflowAnalysis:+ files: tuple[WorkflowFile, ...]+ total_uses: int+ pinned_uses: int+ total_checkouts: int+ safe_checkouts: int+ non_exempt_jobs: int+ permissions_jobs: int+ permissions_ok_jobs: int+ harden_ok_jobs: int+ missing_permissions: tuple[str, ...]+ missing_harden: tuple[str, ...]+++@dataclass(frozen=True, slots=True)+class PrecommitHook:+ hook_id: str+ repo: str+ rev: str+ revision_kind: Literal["commit", "floating", "tag_or_unknown", "special"]+++class PrecommitAnalysis(TypedDict):+ present: bool+ parse_error: NotRequired[str]+ hook_count: int+ hooks: list[PrecommitHook]+ floating_revs: list[PrecommitHook]+ has_secrets_hook: bool+ tag_or_unknown_count: NotRequired[int]+++@dataclass(frozen=True, slots=True)+class ScannerCategory:+ name: str+ required: bool+ satisfied: bool+ workflows: tuple[str, ...]+ dependabot_config: bool+++@dataclass(frozen=True, slots=True)+class GitChange:+ sha: str+ date: str+ author: str+ subject: str+++@dataclass(frozen=True, slots=True)+class GitHistory:+ changes: tuple[GitChange, ...]+ unavailable: str | None+++@dataclass(frozen=True, slots=True)+class WorkflowRun:+ database_id: int+ conclusion: str+ event: str+ head_branch: str+ created_at: str+ # True only for the synthetic UNRESOLVED run manufactured when the live+ # time budget ran out before a settled run could be resolved from the+ # fetched gate history: its OWN job list was never queried, so nothing+ # about "not naming this scanner" is known.+ budget_exhausted: bool = False+ # Why this run resolved to `_UNRESOLVED` (``None`` for any other+ # conclusion): "job_list_unreadable" (`gh run view --json jobs` failed,+ # timed out, or returned unparseable JSON -- nothing was learned about+ # this scanner), "job_list_lacked_scanner" (the list WAS read and+ # genuinely didn't name this scanner), "gate_has_no_caller_job" (the+ # gate's workflow file has no job calling this workflow at all -- a+ # per-gate fact, not a per-run one), or "budget_exhausted" (mirrors+ # `budget_exhausted` above: this run's job list was never even+ # queried). Not an exhaustive enum of every failure mode -- a small,+ # short string so a new cause can be added without inventing a type.+ unresolved_reason: str | None = None+++@dataclass(frozen=True, slots=True)+class LiveStatus:+ workflow: str+ runs: tuple[WorkflowRun, ...]+ latest_conclusion: str | None+ unavailable: str | None+ # Set when `runs` are the runs of the gate that calls this workflow+ # rather than the workflow's own (reusable scanners have no useful+ # direct history).+ gated_by: str | None = None+ # Computed at collection time against the wall clock so that render()+ # stays a pure function of its inputs.+ stale: bool = False+ # Collection-time remarks that render() cannot derive from the runs alone+ # (a truncated gate history, an unparseable timestamp).+ notes: tuple[str, ...] = ()+++@dataclass(frozen=True, slots=True)+class Dimension:+ name: str+ weight: int+ ratio: float | None+++def _read(path: pathlib.Path) -> str:+ try:+ return path.read_text(encoding="utf-8")+ except (FileNotFoundError, OSError):+ return ""+++def _workflow_files() -> tuple[pathlib.Path, ...]:+ return tuple(+ sorted((*WORKFLOWS_DIR.glob("*.yml"), *WORKFLOWS_DIR.glob("*.yaml")))+ )+++def _iter_action_mappings(+ value: YamlValue,+) -> Iterator[tuple[dict[YamlScalar, YamlValue], bool]]:+ if not isinstance(value, dict):+ return+ jobs = value.get("jobs")+ if not isinstance(jobs, dict):+ return+ for job in jobs.values():+ if not isinstance(job, dict):+ continue+ if "uses" in job:+ yield job, False+ steps = job.get("steps")+ if not isinstance(steps, list):+ continue+ for step in steps:+ if isinstance(step, dict) and "uses" in step:+ yield step, True+++def _action_head(ref: str) -> str:+ head, separator, _tail = ref.rpartition("@")+ return head if separator else ref+++def _pinning_kind(+ ref: str,+) -> Literal["local", "pinned", "unpinned", "expression"]:+ head, separator, tail = ref.rpartition("@")+ if ref.startswith(("./", "$/")):+ return "local"+ if ref.startswith("docker://"):+ return (+ "pinned"+ if separator and DOCKER_DIGEST_RE.fullmatch(tail)+ else "unpinned"+ )+ if "${{" in ref:+ return "expression"+ if separator and head and SHA_PIN_RE.fullmatch(tail):+ return "pinned"+ return "unpinned"+++def _is_false(value: YamlValue) -> bool:+ return value is False or (+ isinstance(value, str) and value.casefold() == "false"+ )+++def _has_explicit_permissions(mapping: dict[YamlScalar, YamlValue]) -> bool:+ if "permissions" not in mapping:+ return False+ value = mapping["permissions"]+ return isinstance(value, dict) or (+ isinstance(value, str) and value.casefold() in ("read-all", "write-all")+ )+++def _workflow_is_privileged(+ filename: str, workflow_name: str, pattern: re.Pattern[str]+) -> bool:+ return pattern.search(f"{filename} {workflow_name}".casefold()) is not None+++def analyze_workflows(findings: list[Finding]) -> WorkflowAnalysis:+ rows: list[WorkflowFile] = []+ total_uses = pinned_uses = total_checkouts = safe_checkouts = 0+ non_exempt_jobs = permissions_jobs = permissions_ok_jobs = (+ harden_ok_jobs+ ) = 0+ missing_permissions: list[str] = []+ missing_harden: list[str] = []++ for path in _workflow_files():+ try:+ documents = list(YAML.safe_load_all(_read(path)))+ except YAML.YAMLError as error:+ findings.append(+ Finding(+ "high", "workflow", path.name, f"YAML parse error: {error}"+ )+ )+ continue+ if len(documents) != 1:+ findings.append(+ Finding(+ "high",+ "workflow",+ path.name,+ "malformed: multi-document YAML",+ )+ )+ continue+ data = documents[0]+ if not isinstance(data, dict):+ findings.append(+ Finding(+ "high",+ "workflow",+ path.name,+ "malformed: top-level YAML must be a mapping",+ )+ )+ continue++ jobs_value = data.get("jobs")+ if not isinstance(jobs_value, dict) or not jobs_value:+ findings.append(+ Finding(+ "high",+ "workflow",+ path.name,+ "malformed: jobs must be a non-empty mapping",+ )+ )+ continue++ workflow_name_value = data.get("name", "")+ workflow_name = (+ workflow_name_value if isinstance(workflow_name_value, str) else ""+ )+ pin_privileged = _workflow_is_privileged(+ path.name, workflow_name, PRIVILEGED_PIN_RE+ )+ perms_privileged = _workflow_is_privileged(+ path.name, workflow_name, PRIVILEGED_PERMS_RE+ )+ file_uses = file_pinned = 0+ file_unpinned: list[str] = []++ for mapping, is_step in _iter_action_mappings(data):+ uses_value = mapping.get("uses")+ if uses_value is None:+ continue+ if not isinstance(uses_value, str):+ findings.append(+ Finding(+ "medium",+ "workflow",+ path.name,+ "malformed uses value is not a string",+ )+ )+ continue+ ref = uses_value.strip()+ kind = _pinning_kind(ref)+ if kind != "local":+ total_uses += 1+ file_uses += 1+ if kind == "pinned":+ pinned_uses += 1+ file_pinned += 1+ elif kind == "expression":+ findings.append(+ Finding(+ "high",+ "pinning",+ path.name,+ "action ref uses an expression, cannot verify pinning",+ )+ )+ elif kind == "unpinned":+ severity: Severity = "critical" if pin_privileged else "high"+ findings.append(+ Finding(+ severity,+ "pinning",+ path.name,+ f"unpinned action: {ref}",+ )+ )+ file_unpinned.append(ref)++ if is_step and _action_head(ref).casefold() == "actions/checkout":+ total_checkouts += 1+ with_value = mapping.get("with")+ persist_value: YamlValue = None+ if isinstance(with_value, dict):+ persist_value = with_value.get("persist-credentials")+ if _is_false(persist_value):+ safe_checkouts += 1+ else:+ findings.append(+ Finding(+ "high",+ "checkout",+ path.name,+ "checkout without persist-credentials: false",+ )+ )++ rows.append(+ WorkflowFile(+ path.name, file_uses, file_pinned, tuple(file_unpinned)+ )+ )++ top_permissions = _has_explicit_permissions(data)+ for job_name_value, job_value in sorted(+ jobs_value.items(), key=lambda item: str(item[0])+ ):+ job_name = str(job_name_value)+ location = f"{path.name} / {job_name}"+ if not isinstance(job_value, dict):+ findings.append(+ Finding(+ "high",+ "workflow",+ location,+ "malformed: job must be a mapping",+ )+ )+ continue+ permissions_jobs += 1+ if top_permissions or _has_explicit_permissions(job_value):+ permissions_ok_jobs += 1+ else:+ severity = "high" if perms_privileged else "medium"+ message = (+ "no explicit permissions block (uses default token scopes)"+ )+ findings.append(+ Finding(severity, "permissions", location, message)+ )+ missing_permissions.append(location)++ steps_value = job_value.get("steps")+ if "uses" in job_value and "steps" in job_value:+ findings.append(+ Finding(+ "high",+ "workflow",+ location,+ "malformed: a job cannot combine reusable uses with steps",+ )+ )+ if "steps" in job_value and (+ not isinstance(steps_value, list) or not steps_value+ ):+ findings.append(+ Finding(+ "high",+ "workflow",+ location,+ "malformed: steps must be a non-empty list",+ )+ )+ continue+ if not isinstance(steps_value, list):+ reusable_uses = job_value.get("uses")+ if (+ not isinstance(reusable_uses, str)+ or not reusable_uses.strip()+ ):+ findings.append(+ Finding(+ "high",+ "workflow",+ location,+ "job has neither steps nor uses",+ )+ )+ continue+ non_exempt_jobs += 1+ for index, step in enumerate(steps_value):+ if not isinstance(step, dict):+ findings.append(+ Finding(+ "high",+ "workflow",+ location,+ f"malformed: step {index} must be a mapping",+ )+ )+ continue+ has_uses = "uses" in step+ has_run = "run" in step+ command = step.get("uses" if has_uses else "run")+ if (+ has_uses == has_run+ or not isinstance(command, str)+ or (has_uses and not command.strip())+ ):+ findings.append(+ Finding(+ "high",+ "workflow",+ location,+ f"malformed: step {index} requires exactly one valid uses or run command",+ )+ )++ first_uses = ""+ if steps_value and isinstance(steps_value[0], dict):+ first_uses_value = steps_value[0].get("uses")+ if isinstance(first_uses_value, str):+ first_uses = first_uses_value+ if (+ _action_head(first_uses).casefold()+ == "step-security/harden-runner"+ ):+ harden_ok_jobs += 1+ else:+ findings.append(+ Finding(+ "medium",+ "harden-runner",+ location,+ "first step is not step-security/harden-runner",+ )+ )+ missing_harden.append(location)++ return WorkflowAnalysis(+ tuple(rows),+ total_uses,+ pinned_uses,+ total_checkouts,+ safe_checkouts,+ non_exempt_jobs,+ permissions_jobs,+ permissions_ok_jobs,+ harden_ok_jobs,+ tuple(missing_permissions),+ tuple(missing_harden),+ )+++def analyze_precommit(+ findings: list[Finding] | None = None,+ path: pathlib.Path = PRECOMMIT_FILE,+) -> PrecommitAnalysis:+ finding_list = findings if findings is not None else []+ text = _read(path)+ if not text:+ finding_list.append(+ Finding(+ "medium",+ "pre-commit",+ path.name,+ "configuration file is missing",+ )+ )+ return {+ "present": False,+ "hook_count": 0,+ "hooks": [],+ "floating_revs": [],+ "has_secrets_hook": False,+ }+ try:+ config = YAML.safe_load(text)+ except YAML.YAMLError as error:+ finding_list.append(+ Finding("high", "pre-commit", path.name, "YAML parse error")+ )+ return {+ "present": True,+ "parse_error": str(error),+ "hook_count": 0,+ "hooks": [],+ "floating_revs": [],+ "has_secrets_hook": False,+ }++ hooks: list[PrecommitHook] = []+ has_secrets_hook = False+ if not isinstance(config, dict):+ finding_list.append(+ Finding("medium", "pre-commit", path.name, "repos must be a list")+ )+ return {+ "present": True,+ "hook_count": 0,+ "hooks": [],+ "floating_revs": [],+ "has_secrets_hook": False,+ }++ repos_value = config.get("repos")+ if not isinstance(repos_value, list):+ finding_list.append(+ Finding("medium", "pre-commit", path.name, "repos must be a list")+ )+ return {+ "present": True,+ "hook_count": 0,+ "hooks": [],+ "floating_revs": [],+ "has_secrets_hook": False,+ }+ for index, repo_value in enumerate(repos_value):+ if not isinstance(repo_value, dict):+ finding_list.append(+ Finding(+ "medium",+ "pre-commit",+ path.name,+ f"malformed repository entry {index}",+ )+ )+ continue+ hooks_value = repo_value.get("hooks")+ if not isinstance(hooks_value, list):+ finding_list.append(+ Finding(+ "medium",+ "pre-commit",+ path.name,+ f"malformed repository entry {index}",+ )+ )+ continue+ repo_raw = repo_value.get("repo")+ if not isinstance(repo_raw, str) or not repo_raw.strip():+ finding_list.append(+ Finding(+ "medium",+ "pre-commit",+ path.name,+ f"repository entry {index} has an empty repo",+ )+ )+ continue+ repo = repo_raw.strip()+ rev_raw = repo_value.get("rev", "")+ rev = rev_raw.strip() if isinstance(rev_raw, str) else ""+ if repo in ("local", "meta"):+ revision_kind: Literal[+ "commit", "floating", "tag_or_unknown", "special"+ ] = "special"+ elif not rev or rev.casefold() in FLOATING_REVS:+ revision_kind = "floating"+ elif SHA_PIN_RE.fullmatch(rev):+ revision_kind = "commit"+ else:+ revision_kind = "tag_or_unknown"++ for hook_index, hook_value in enumerate(hooks_value):+ if not isinstance(hook_value, dict):+ finding_list.append(+ Finding(+ "medium",+ "pre-commit",+ path.name,+ f"malformed hook {index}.{hook_index}",+ )+ )+ continue+ hook_id_raw = hook_value.get("id", "")+ hook_id = hook_id_raw if isinstance(hook_id_raw, str) else ""+ if hook_id in (+ "gitleaks",+ "trufflehog",+ "detect-secrets",+ "gitleaks-system",+ ):+ has_secrets_hook = True+ hook = PrecommitHook(hook_id, repo, rev, revision_kind)+ hooks.append(hook)+ if revision_kind == "floating":+ finding_list.append(+ Finding(+ "medium",+ "pre-commit",+ path.name,+ f"hook {hook_id or '<unnamed>'} has a floating revision",+ )+ )++ floating = [hook for hook in hooks if hook.revision_kind == "floating"]+ tag_count = sum(hook.revision_kind == "tag_or_unknown" for hook in hooks)+ if not has_secrets_hook:+ finding_list.append(+ Finding(+ "medium",+ "pre-commit",+ path.name,+ "no secret-scanning hook configured",+ )+ )+ return {+ "present": True,+ "hook_count": len(hooks),+ "hooks": hooks,+ "floating_revs": floating,+ "has_secrets_hook": has_secrets_hook,+ "tag_or_unknown_count": tag_count,+ }+++# A workflow whose name only mentions a scanner because it TESTS that+# scanner's configuration (``gitleaks-rule-tests.yml``) is not a scan.+_TEST_WORKFLOW_SUFFIXES: Final = ("-test", "-tests", "_test", "_tests")+++def _is_test_workflow(name: str) -> bool:+ stem = name.casefold().rsplit(".", 1)[0]+ return stem.endswith(_TEST_WORKFLOW_SUFFIXES)+++def analyze_scanners(findings: list[Finding]) -> tuple[ScannerCategory, ...]:+ workflow_names = tuple(+ path.name.casefold()+ for path in _workflow_files()+ if not _is_test_workflow(path.name)+ )+ categories: list[ScannerCategory] = []+ for required, definitions in (+ (True, REQUIRED_SCANNERS),+ (False, OPTIONAL_SCANNERS),+ ):+ for category, scanners in sorted(definitions.items()):+ matches = tuple(+ name+ for name in workflow_names+ if any(scanner in name for scanner in scanners)+ )+ dependabot_config = (+ category == "Dependencies" and DEPENDABOT_FILE.is_file()+ )+ satisfied = bool(matches) or dependabot_config+ categories.append(+ ScannerCategory(+ category, required, satisfied, matches, dependabot_config+ )+ )+ if required and not satisfied:+ findings.append(+ Finding(+ "medium",+ "scanners",+ category,+ f"required security category {category} has no executing scanner",+ )+ )+ return tuple(categories)+++def _scanner_workflow_files() -> tuple[str, ...]:+ """Return actual filenames of workflows matching any scanner keyword."""+ keywords = [+ keyword+ for group in (REQUIRED_SCANNERS, OPTIONAL_SCANNERS)+ for scanners in group.values()+ for keyword in scanners+ ]+ return tuple(+ sorted(+ path.name+ for path in _workflow_files()+ if any(keyword in path.name.casefold() for keyword in keywords)+ and not _is_test_workflow(path.name)+ )+ )+++def discount_idle_scanners(+ scanners: tuple[ScannerCategory, ...],+ live: tuple[LiveStatus, ...],+ findings: list[Finding],+) -> tuple[ScannerCategory, ...]:+ """Drop scanner workflows with NO RUNS from their category's evidence.++ A workflow that exists but never ran on ``main`` in the query window+ (for example one whose only caller is commented out) is not coverage.+ Unavailable live entries are left alone: that is a failed query, not+ evidence of an idle scanner. A required category left with no evidence+ gets the same finding :func:`analyze_scanners` raises statically.+ """+ idle = {+ entry.workflow.casefold()+ for entry in live+ if entry.unavailable is None and not entry.runs+ }+ if not idle:+ return scanners+ adjusted: list[ScannerCategory] = []+ for category in scanners:+ workflows = tuple(+ name for name in category.workflows if name not in idle+ )+ satisfied = bool(workflows) or category.dependabot_config+ if category.required and category.satisfied and not satisfied:+ findings.append(+ Finding(+ "medium",+ "scanners",+ category.name,+ f"required security category {category.name} has no executing scanner (no runs on {_LIVE_BRANCH} in the query window)",+ )+ )+ adjusted.append(+ replace(category, satisfied=satisfied, workflows=workflows)+ )+ return tuple(adjusted)+++def _gh_available() -> str | None:+ """Return ``None`` if gh is usable, or an error reason string."""+ try:+ result = subprocess.run(+ ["gh", "--version"],+ capture_output=True,+ text=True,+ timeout=5,+ check=False,+ )+ except (subprocess.TimeoutExpired, OSError):+ return "gh not found or timed out"+ return None if result.returncode == 0 else f"gh exit {result.returncode}"+++# Conclusions that mean the scanner did not produce a clean result. Kept+# separate from "cancelled" and "skipped", which are routinely deliberate (a+# superseded run, a path filter that did not match) and would make the gate+# noisy. "timed_out" is a scanner that never finished and "startup_failure" is+# one that never began (broken workflow YAML, an unresolvable reusable-workflow+# ref); both are the failure mode this whole report exists to surface --+# reporting them as healthy is the same class of lie as reporting a+# never-triggered job as passing. "stale" is deliberately left out: it is rare+# and ambiguous enough that gating on it would cost more than it catches.+_FAILED_CONCLUSIONS = frozenset({"failure", "timed_out", "startup_failure"})++# The report describes the health of the default branch, so live queries are+# scoped to it. Without this, `gh run list` returns runs from every branch and+# the newest one is typically a contributor's pull-request run -- a failure+# there says something about that PR, not about whether the scanner works.+#+# `--branch` is a help, NOT the guard: it matches the run's HEAD REF NAME, and+# a fork whose default branch is also called `main` produces `pull_request`+# runs whose headBranch is literally "main". Those runs execute the+# contributor's tree, so their conclusion says something about that pull+# request, not about whether the scanner works on this repository's main.+# `pull_request_target` is excluded for the same reason: it checks out the+# pull-request head. Hence the event filter below, in addition to --branch.+_LIVE_BRANCH = "main"+_PULL_REQUEST_EVENTS: Final = frozenset({"pull_request", "pull_request_target"})++# Staleness rules are NOT invented here. `scripts/generate_workflow_status.py`+# already defines what "stale" means for this repository (STALE_MULTIPLIER x+# the workflow's own cron cadence) and `docs/ci/workflow-status.md` documents+# that reusable workflows are read through the gate that calls them. Both+# scripts live in `scripts/` and neither is importable as a package, so the+# sibling is loaded by path instead of copied.+_STATUS_RULES_SCRIPT: Final = ROOT / "scripts" / "generate_workflow_status.py"+# The sibling's floor, verbatim: `classify_staleness` computes+# `threshold_days = max(60.0, STALE_MULTIPLIER * min(cadences))`+# (scripts/generate_workflow_status.py:618). Keeping a different floor here+# would make the two dashboards disagree about the same workflow more often+# than the known differences listed in `_stale_threshold_days` already do.+# It doubles as the fallback for a workflow with no cron of its own.+_STALE_FLOOR_DAYS: Final = 60.0+_FALLBACK_GATE_ROOTS: Final = frozenset(+ {"release-gate.yml", "ci-gate.yml", "release.yml"}+)+# A workflow with only these triggers has no DEFAULT-BRANCH run history of+# its own worth reading: its work shows up under whichever gate calls it.+# `pull_request`/`pull_request_target` belong in the set because+# `_is_default_branch_run` already discards runs from those events, so they+# can never become evidence here -- counting them as an own history is how+# `owasp-zap-scan.yml` (path-filtered `pull_request:` + `workflow_call:` ++# `workflow_dispatch:`) fell back to its dead direct-run log and reported+# STALE while release-gate.yml ran it every night.+_GATED_ONLY_TRIGGERS: Final = frozenset(+ {+ "workflow_call",+ "workflow_dispatch",+ "pull_request",+ "pull_request_target",+ }+)+# The line scanner sees `uses:` values verbatim, quotes included; only the+# YAML parser strips them. Both spellings are valid and both appear in the+# wild, so accept either quoting (or none).+_REUSABLE_USES_RE: Final = re.compile(+ r"""^\s*uses:\s*(?P<q>['"]?)[.$]/\.github/workflows/"""+ r"(?P<file>[A-Za-z0-9._-]+\.ya?ml)(?P=q)"+)+# The same reference as PyYAML hands it back (unquoted, whole value).+_REUSABLE_USES_VALUE_RE: Final = re.compile(+ r"^[.$]/\.github/workflows/(?P<file>[A-Za-z0-9._-]+\.ya?ml)$"+)+# Preference order for the gate a reusable scanner is read through.+# `GATE_ROOTS` in the sibling script is an unordered set, and picking its+# first element alphabetically prefers `ci-gate.yml` -- whose `main` history+# is two cancelled dispatches -- over `release-gate.yml`, which runs nightly.+_GATE_PREFERENCE: Final = ("release-gate.yml", "ci-gate.yml", "release.yml")++_status_rules_cache: object | None = None+_status_rules_loaded = False+++def _status_rules() -> object | None:+ """Load ``scripts/generate_workflow_status.py`` for its staleness rules."""+ global _status_rules_cache, _status_rules_loaded+ if _status_rules_loaded:+ return _status_rules_cache+ _status_rules_loaded = True+ try:+ spec = importlib.util.spec_from_file_location(+ "_ci_health_status_rules", _STATUS_RULES_SCRIPT+ )+ if spec is None or spec.loader is None:+ return None+ module = importlib.util.module_from_spec(spec)+ # Register BEFORE exec_module: a module absent from sys.modules while+ # its body runs cannot resolve its own name, and+ # `@dataclass(slots=True)` in the sibling would silently degrade.+ sys.modules[spec.name] = module+ try:+ spec.loader.exec_module(module)+ except BaseException:+ sys.modules.pop(spec.name, None)+ raise+ except Exception: # pragma: no cover - the sibling script is missing/broken+ return None+ _status_rules_cache = module+ return module+++def _gate_roots() -> frozenset[str]:+ roots = getattr(_status_rules(), "GATE_ROOTS", None)+ if isinstance(roots, (set, frozenset)):+ return frozenset(str(name) for name in roots)+ return _FALLBACK_GATE_ROOTS+++def _utcnow() -> datetime.datetime:+ """Isolated so tests can freeze the clock; ``render()`` stays clock-free."""+ return datetime.datetime.now(datetime.timezone.utc)+++def _run_date(created_at: str) -> str:+ """The calendar date of an ISO-8601 run timestamp (stable across runs)."""+ return created_at[:10] if created_at else "—"+++def _run_age_days(created_at: str, now: datetime.datetime) -> float | None:+ try:+ created = datetime.datetime.fromisoformat(+ created_at.replace("Z", "+00:00")+ )+ except ValueError:+ return None+ if created.tzinfo is None:+ created = created.replace(tzinfo=datetime.timezone.utc)+ return (now - created).total_seconds() / 86400.0+++def _is_default_branch_run(run: WorkflowRun) -> bool:+ """True when the run executed this repository's default-branch tree."""+ return (+ run.event not in _PULL_REQUEST_EVENTS+ and run.head_branch == _LIVE_BRANCH+ )+++def _workflow_on_block(path: pathlib.Path) -> dict[str, YamlValue]:+ """Return a workflow's ``on:`` mapping, normalised to a dict."""+ try:+ data = YAML.safe_load(_read(path))+ except YAML.YAMLError:+ return {}+ if not isinstance(data, dict):+ return {}+ # PyYAML follows YAML 1.1 and may coerce the key `on` to boolean True.+ triggers = data.get("on", data.get(True, {}))+ if isinstance(triggers, str):+ return {triggers: None}+ if isinstance(triggers, list):+ return {item: None for item in triggers if isinstance(item, str)}+ if isinstance(triggers, dict):+ return {+ key: value+ for key, value in triggers.items()+ if isinstance(key, str)+ }+ return {}+++def _cron_strings(workflow: str) -> tuple[str, ...]:+ schedule = _workflow_on_block(WORKFLOWS_DIR / workflow).get("schedule")+ if not isinstance(schedule, list):+ return ()+ crons: list[str] = []+ for entry in schedule:+ if isinstance(entry, dict):+ cron = entry.get("cron")+ if isinstance(cron, str):+ crons.append(cron)+ return tuple(crons)+++def _stale_threshold_days(workflow: str) -> float:+ """Days after which *workflow*'s last SUCCESSFUL run stops being evidence.++ Similar to ``classify_staleness`` in ``scripts/generate_workflow_status.py``+ (:616-618): ``max(60.0, STALE_MULTIPLIER * min(cadences))`` over the+ queried workflow's cron cadences. It is not identical, so the two+ dashboards can disagree: here a workflow with no cron falls back to the+ 60-day floor (the sibling never marks it stale), a gated scanner uses its+ gate's cadence, there is no new-file exemption, a window with no runs at+ all is not stale (it is reported as ``NO RUNS`` instead), and ages are+ compared as fractional rather than whole days.+ """+ rules = _status_rules()+ cadence_of = getattr(rules, "cron_cadence_days", None)+ multiplier = getattr(rules, "STALE_MULTIPLIER", 2)+ if cadence_of is None:+ return _STALE_FLOOR_DAYS+ cadences = [+ cadence+ for cadence in (cadence_of(cron) for cron in _cron_strings(workflow))+ if isinstance(cadence, (int, float)) and cadence > 0+ ]+ if not cadences:+ return _STALE_FLOOR_DAYS+ return max(_STALE_FLOOR_DAYS, float(multiplier) * min(cadences))+++def _reusable_workflow_callers() -> dict[str, tuple[str, ...]]:+ """Map a reusable workflow's filename to the workflows that call it."""+ callers: dict[str, list[str]] = {}+ for path in _workflow_files():+ for line in _read(path).splitlines():+ match = _REUSABLE_USES_RE.match(line)+ if match is None:+ continue+ called = match.group("file")+ if called == path.name:+ continue+ bucket = callers.setdefault(called, [])+ if path.name not in bucket:+ bucket.append(path.name)+ return {name: tuple(sorted(value)) for name, value in callers.items()}+++def _gating_parent(+ workflow: str, callers: dict[str, tuple[str, ...]]+) -> str | None:+ """Return the workflow whose runs carry *workflow*, or ``None``.++ ``docs/ci/workflow-status.md`` states the rule this implements: a+ ``workflow_call:`` workflow whose only OTHER triggers are+ ``workflow_dispatch`` or pull-request events shows its GATED run --+ the most recent run of the parent that included it -- not its own+ empty direct-run history. Reading their own history is how a scanner+ last dispatched eight months ago reports PASS.+ """+ triggers = set(_workflow_on_block(WORKFLOWS_DIR / workflow))+ if not triggers or not triggers <= _GATED_ONLY_TRIGGERS:+ return None+ if "workflow_call" not in triggers:+ return None+ names = callers.get(workflow, ())+ gates = [name for name in names if name in _gate_roots()]+ if gates:+ # NOT `gates[0]`: that is alphabetical order, which prefers+ # `ci-gate.yml` over `release-gate.yml` (see _GATE_PREFERENCE).+ return min(gates, key=_gate_rank)+ return names[0] if names else None+++def _gate_rank(gate: str) -> tuple[int, str]:+ """Sort key putting the gate that actually runs on ``main`` first."""+ if gate in _GATE_PREFERENCE:+ return (_GATE_PREFERENCE.index(gate), gate)+ return (len(_GATE_PREFERENCE), gate)+++def _gate_job_ids(gate: str) -> dict[str, tuple[str, ...]]:+ """Map each workflow *gate* calls to the job-name PREFIX GitHub renders.++ `release-gate.yml`'s `jobs:` block names them: `semgrep-scan:` has+ `uses: ./.github/workflows/semgrep.yml`, `gitleaks-scan:` has+ `uses: ./.github/workflows/gitleaks-main.yml`, and so on. GitHub renders+ a job's YAML key (the "job id") as its displayed name only when the job+ has no `name:` of its own; when it does, THAT is the prefix every job the+ called workflow contributes is nested under (see+ :data:`_JOB_PATH_SEPARATOR`) -- `job.get("name") or job_id`, not the id+ alone. A matrixed caller job appends its matrix value in parens directly+ to that same prefix (`prefix (ubuntu-latest) / scan`), so callers must+ also prefix-match `f"{prefix} ("`.+ """+ try:+ data = YAML.safe_load(_read(WORKFLOWS_DIR / gate))+ except YAML.YAMLError:+ return {}+ jobs = data.get("jobs") if isinstance(data, dict) else None+ if not isinstance(jobs, dict):+ return {}+ mapping: dict[str, list[str]] = {}+ for job_id, job in jobs.items():+ if not isinstance(job_id, str) or not isinstance(job, dict):+ continue+ uses = job.get("uses")+ if not isinstance(uses, str):+ continue+ match = _REUSABLE_USES_VALUE_RE.match(uses.strip())+ if match is not None:+ name = job.get("name")+ prefix = name if isinstance(name, str) and name else job_id+ mapping.setdefault(match.group("file"), []).append(prefix)+ return {name: tuple(ids) for name, ids in mapping.items()}+++# Leave time for setup, rendering and report publication in the five-minute job.+_LIVE_BUDGET_SECONDS = 120.0+# A gate run is only resolved per job while this much of the shared budget is+# left, so the per-job lookups cannot starve the remaining scanners' queries.+_JOBS_QUERY_RESERVE_SECONDS = 15.0+# Jobs a called ("reusable") workflow contributes to the caller's run are+# named "<caller job id> / <called job name>". Verified on release-gate run+# 35810068506: `semgrep-scan / semgrep-scan`, `owasp-zap-scan / ZAP API Scan`,+# `grype-scan / Grype Filesystem Scan`. A nested call adds another segment+# (`osv-scan / scan / osv-scan`), hence a prefix match rather than equality.+_JOB_PATH_SEPARATOR: Final = " / "+# Synthetic conclusion for a gate run whose job list did not name the+# scanner. It is NOT a gh conclusion (so it stays out of+# _KNOWN_CONCLUSIONS) and it is NOT a failure: it means "not measured".+_UNRESOLVED: Final = "unresolved"+# Conclusions that decide nothing: a run still in flight, and one whose jobs+# could not be resolved. Neither may hide the newest run that did conclude.+_INCONCLUSIVE: Final = frozenset({"unknown", _UNRESOLVED})+++def _fetch_run_jobs(+ run_id: int, timeout: float+) -> tuple[tuple[str, str], ...] | None:+ """``(job name, conclusion)`` for one run, or ``None`` if unreadable."""+ try:+ result = subprocess.run(+ ["gh", "run", "view", str(run_id), "--json=jobs"],+ cwd=ROOT,+ capture_output=True,+ text=True,+ timeout=timeout,+ check=False,+ )+ except (subprocess.TimeoutExpired, OSError):+ return None+ if result.returncode != 0:+ return None+ try:+ payload = json.loads(result.stdout)+ except json.JSONDecodeError:+ return None+ jobs = payload.get("jobs") if isinstance(payload, dict) else None+ if not isinstance(jobs, list):+ return None+ parsed: list[tuple[str, str]] = []+ for job in jobs:+ if not isinstance(job, dict):+ return None+ name = job.get("name")+ conclusion = job.get("conclusion")+ if not isinstance(name, str) or (+ conclusion is not None and not isinstance(conclusion, str)+ ):+ return None+ parsed.append((name, conclusion or "unknown"))+ return tuple(parsed)+++def _scanner_conclusion_from_jobs(+ jobs: tuple[tuple[str, str], ...], job_ids: tuple[str, ...]+) -> str | None:+ """This scanner's own conclusion inside a gate run, or ``None``.++ ``None`` means the run's job list named no job of this scanner: that is+ an unresolved measurement, never a failure.+ """+ own = [+ conclusion+ for name, conclusion in jobs+ if any(+ name == job_id+ or name.startswith(job_id + _JOB_PATH_SEPARATOR)+ # A matrixed caller job's name gets its matrix value appended+ # in parens BEFORE the " / " path separator, e.g.+ # "owasp-zap-scan (staging) / ZAP API Scan".+ or name.startswith(f"{job_id} (")+ for job_id in job_ids+ )+ ]+ if not own:+ return None+ # A scanner may contribute several jobs (ZAP runs a baseline and an API+ # scan). The worst of them is the scanner's result.+ for conclusion in own:+ if conclusion in _FAILED_CONCLUSIONS:+ return conclusion+ if "unknown" in own:+ return "unknown"+ for conclusion in own:+ if conclusion != "success":+ return conclusion+ return "success"+++def _resolve_gated_runs(+ workflow: str,+ gate: str,+ runs: tuple[WorkflowRun, ...],+ jobs_cache: dict[int, tuple[tuple[str, str], ...] | None],+ deadline: float,+) -> tuple[tuple[WorkflowRun, ...], list[str]]:+ """Replace each GATE run's conclusion with *workflow*'s own job result.++ The gate's roll-up is not this scanner's result. `release-gate.yml`+ concluded `failure` on 19 of its last 20 `main` runs because of a+ standing `Check Code Scanning Alerts` condition while every scanner job+ in those runs succeeded; printing that `failure` under a scanner's name+ is the same class of lie this report exists to catch, in the opposite+ direction. One `gh run view <id> --json jobs` call per gate run, memoised+ in *jobs_cache*, makes it a measurement: the seven scanners sharing+ `release-gate.yml` cost one call per run between them, not seven.+ """+ job_ids = _gate_job_ids(gate).get(workflow, ())+ if not job_ids:+ return (+ tuple(+ replace(+ run,+ conclusion=_UNRESOLVED,+ unresolved_reason="gate_has_no_caller_job",+ )+ for run in runs+ ),+ [f"`{gate}` has no job calling this workflow"],+ )+ resolved: list[WorkflowRun] = []+ notes: list[str] = []+ for run in runs:+ if run.database_id not in jobs_cache:+ remaining = deadline - time.monotonic()+ if remaining <= _JOBS_QUERY_RESERVE_SECONDS:+ notes.append(+ f"gate history truncated to {len(resolved)} of "+ f"{len(runs)} runs by the live time budget"+ )+ break+ jobs_cache[run.database_id] = _fetch_run_jobs(+ run.database_id, min(15.0, remaining)+ )+ jobs = jobs_cache[run.database_id]+ conclusion = (+ None+ if jobs is None+ else _scanner_conclusion_from_jobs(jobs, job_ids)+ )+ if (+ conclusion is None+ and jobs is not None+ and run.conclusion == "unknown"+ ):+ # The gate run itself is still in flight: this scanner's job+ # simply hasn't been created in it yet, which is "in flight",+ # not "a completed run whose job list stayed silent about it"+ # (UNRESOLVED). Keep it "unknown" so the caller reports RUNNING+ # and computes its finding from the previous CONCLUSIVE gate run.+ resolved.append(replace(run, conclusion="unknown"))+ continue+ if conclusion is None:+ resolved.append(+ replace(+ run,+ conclusion=_UNRESOLVED,+ unresolved_reason=(+ "job_list_unreadable"+ if jobs is None+ else "job_list_lacked_scanner"+ ),+ )+ )+ else:+ resolved.append(replace(run, conclusion=conclusion))+ return tuple(resolved), notes+++_KNOWN_CONCLUSIONS = frozenset(+ {+ "success",+ "failure",+ "neutral",+ "cancelled",+ "skipped",+ "timed_out",+ "action_required",+ "startup_failure",+ "stale",+ }+)+++def _parse_workflow_runs(+ payload: list[object],+) -> tuple[WorkflowRun, ...] | None:+ """Reject an incomplete response rather than silently choosing an older run."""+ runs: list[WorkflowRun] = []+ for row in payload:+ if not isinstance(row, dict):+ return None+ run_id = row.get("databaseId")+ conclusion = row.get("conclusion")+ event = row.get("event")+ branch = row.get("headBranch")+ created_at = row.get("createdAt")+ if (+ not isinstance(run_id, int)+ or isinstance(run_id, bool)+ or run_id <= 0+ or not isinstance(event, str)+ or not isinstance(branch, str)+ or not isinstance(created_at, str)+ or (conclusion is not None and not isinstance(conclusion, str))+ ):+ return None+ if conclusion and conclusion not in _KNOWN_CONCLUSIONS:+ return None+ runs.append(+ WorkflowRun(+ run_id, conclusion or "unknown", event, branch, created_at+ )+ )+ return tuple(runs)+++def analyze_live_status(+ findings: list[Finding],+ scanner_workflows: tuple[str, ...],+ *,+ enabled: bool = True,+ limit: int = 5,+) -> tuple[LiveStatus, ...]:+ """Query recent CI run conclusions for each scanner workflow via ``gh``.++ Only default-branch runs count: ``--branch`` plus an event filter, because+ ``--branch`` matches the run's head ref name and a fork PR from a branch+ called ``main`` passes it (see :data:`_PULL_REQUEST_EVENTS`).++ A scanner with no default-branch history of its own is read through the+ gate that calls it, matching the rule already documented in+ ``docs/ci/workflow-status.md``; its entry records ``gated_by``. The+ gate's own conclusion is never shown under the scanner's name: each gate+ run is resolved to the scanner's OWN job conclusions via+ ``gh run view <id> --json jobs`` (:func:`_resolve_gated_runs`), and a run+ whose job list does not name the scanner renders ``UNRESOLVED``.++ Returns an empty tuple when *enabled* is False. When ``gh`` is+ unavailable or unauthenticated, every entry carries an *unavailable*+ reason and no findings are emitted. When the most recent COMPLETED run of+ a scanner workflow ended in a :data:`_FAILED_CONCLUSIONS` conclusion, a+ ``high`` finding is appended so that ``--check`` gates on it -- an+ in-flight run does not hide it. A gated scanner's failing JOB is a+ measurement of that scanner, so it is ``high`` too; only an unresolved+ one is ``medium``. When the last SUCCESSFUL run is older than the+ workflow's staleness threshold a ``medium`` finding is appended and the+ entry renders ``STALE``: an eight-month-old success is not evidence that a+ scanner works, and reporting it as ``PASS`` is the same class of lie as+ reporting a never-triggered job as passing. A workflow with NO successful+ run in the window is stale unconditionally, however young its newest+ (non-passing) run is: the ``medium`` finding names that run's OWN+ conclusion, never a fabricated success, and the row keeps that+ conclusion's label -- ``STALE`` only ever replaces a ``PASS`` label, never+ ``FAIL`` or another one. Staleness stays below the ``--check`` gate+ because it reports absent evidence, not a failed scan. So does a+ workflow with NO qualifying run in the window at all (``NO RUNS``): it+ raises a ``medium`` finding, and :func:`discount_idle_scanners` stops+ counting it as coverage for its category.+ Invalid run entries make that workflow unavailable, and so does a+ non-empty page whose runs are ALL filtered out (pull-request or+ other-branch runs): that is a crowded query window, not an idle scanner.+ Collection shares a+ 120-second budget; remaining workflows are marked unavailable when it is+ exhausted.+ """+ if not enabled or not scanner_workflows:+ return ()++ deadline = time.monotonic() + _LIVE_BUDGET_SECONDS+ unavailable = _gh_available()+ if unavailable:+ return tuple(+ LiveStatus(workflow, (), None, unavailable)+ for workflow in scanner_workflows+ )++ callers = _reusable_workflow_callers()+ now = _utcnow()+ # run id -> job list, shared across every scanner behind the same gate.+ jobs_cache: dict[int, tuple[tuple[str, str], ...] | None] = {}+ statuses: list[LiveStatus] = []+ for workflow in scanner_workflows:+ remaining = deadline - time.monotonic()+ if remaining <= 0:+ statuses.append(+ LiveStatus(+ workflow, (), None, "live query time budget exhausted"+ )+ )+ continue+ parent = _gating_parent(workflow, callers)+ queried = parent or workflow+ threshold_days = _stale_threshold_days(queried)+ command = [+ "gh",+ "run",+ "list",+ f"--workflow={queried}",+ f"--branch={_LIVE_BRANCH}",+ f"--limit={limit}",+ "--json=databaseId,conclusion,event,headBranch,createdAt",+ ]+ try:+ result = subprocess.run(+ command,+ cwd=ROOT,+ capture_output=True,+ text=True,+ timeout=min(15.0, remaining),+ check=False,+ )+ except (subprocess.TimeoutExpired, OSError):+ statuses.append(LiveStatus(workflow, (), None, "gh timed out"))+ continue++ if result.returncode != 0:+ stderr = result.stderr.strip()+ reason = (+ stderr.splitlines()[0][:200]+ if stderr+ else f"exit {result.returncode}"+ )+ statuses.append(LiveStatus(workflow, (), None, reason))+ continue++ try:+ payload = json.loads(result.stdout)+ except json.JSONDecodeError:+ statuses.append(+ LiveStatus(workflow, (), None, "gh returned invalid JSON")+ )+ continue++ if not isinstance(payload, list):+ statuses.append(+ LiveStatus(workflow, (), None, "gh returned non-list payload")+ )+ continue++ runs = _parse_workflow_runs(payload)+ if runs is None:+ statuses.append(+ LiveStatus(+ workflow, (), None, "gh returned malformed run entries"+ )+ )+ continue++ fetched_count = len(runs)+ runs = tuple(run for run in runs if _is_default_branch_run(run))+ if fetched_count and not runs:+ # Every fetched row was a pull-request (e.g. a fork's `main`) or+ # other-branch run: the page was crowded out, which says nothing+ # about whether the scanner ran on the default branch. Treat it+ # as an unavailable query, not as an idle scanner (`NO RUNS`),+ # so discount_idle_scanners() keeps counting it as coverage.+ statuses.append(+ LiveStatus(+ workflow,+ (),+ None,+ f"all {fetched_count} fetched runs were pull-request or "+ f"non-{_LIVE_BRANCH} runs; default-branch status unknown",+ )+ )+ continue+ notes: list[str] = []+ if parent is not None:+ # The rows carry this scanner's own job conclusions inside the+ # gate runs, never the gate's roll-up.+ fetched = runs+ runs, notes = _resolve_gated_runs(+ workflow, parent, runs, jobs_cache, deadline+ )+ truncated = any("time budget" in note for note in notes)+ no_settled_run = not any(+ run.conclusion != "unknown" for run in runs+ )+ if truncated and no_settled_run and fetched:+ # The live time budget ran out before a settled run could be+ # resolved from the fetched gate history: absence of+ # evidence from the budget, not absence of a qualifying run+ # (which is what `NO RUNS` would claim -- false, some were+ # found). This also covers the newest run being in flight+ # ("unknown", kept as-is below to render RUNNING) while+ # every OLDER run got truncated away before its job list+ # could even be queried: without this finding, an older+ # scanner failure sitting behind the cut would be silently+ # invisible.+ findings.append(+ Finding(+ "medium",+ "scanner-runtime",+ workflow,+ "gate history could not be resolved within the live time budget",+ )+ )+ if not runs:+ runs = (+ replace(+ fetched[0],+ conclusion=_UNRESOLVED,+ budget_exhausted=True,+ unresolved_reason="budget_exhausted",+ ),+ )+ latest = runs[0] if runs else None+ # A run still in flight has no conclusion yet ("unknown"), and an+ # unresolved gate run measured nothing. Neither may hide the last+ # run that did conclude, or a push racing the report turns a red+ # scanner green.+ completed = next(+ (run for run in runs if run.conclusion not in _INCONCLUSIVE), None+ )+ latest_conclusion = latest.conclusion if latest else None+ gated = f" (gated by {parent})" if parent else ""+ if not runs:+ # A scanner that never ran in the window is not coverage: an+ # absent run is absent evidence (e.g. a caller commented out).+ findings.append(+ Finding(+ "medium",+ "scanner-runtime",+ workflow,+ f"no qualifying run on {_LIVE_BRANCH} in the query window{gated}; an idle scanner is not coverage",+ )+ )++ if (+ completed is not None+ and completed.conclusion in _FAILED_CONCLUSIONS+ ):+ where = f"(branch={completed.head_branch}, event={completed.event}, run #{completed.database_id})"+ if parent is None:+ findings.append(+ Finding(+ "high",+ "scanner-runtime",+ workflow,+ f"latest completed run {completed.conclusion} on {_run_date(completed.created_at)} {where}",+ )+ )+ else:+ # This is the scanner's OWN job conclusion inside the gate+ # run, not the gate's roll-up, so it gates exactly like a+ # direct failure would.+ findings.append(+ Finding(+ "high",+ "scanner-runtime",+ workflow,+ f"this scanner's job in {parent} concluded {completed.conclusion} on {_run_date(completed.created_at)} {where}",+ )+ )+ # The newest run that resolved to ANYTHING -- not `latest`, which is+ # "unknown" while the newest gate run is still in flight. Mirrors+ # `completed`'s treatment: an in-flight newest run must not hide an+ # unresolved run underneath it, exactly as it must not hide a failed+ # one (the `completed` finding above).+ newest_settled = next(+ (run for run in runs if run.conclusion != "unknown"), None+ )+ if (+ newest_settled is not None+ and newest_settled.conclusion == _UNRESOLVED+ and not newest_settled.budget_exhausted+ ):+ # Unresolved is absent evidence, not a failed scan: reported,+ # but below the --check gate. A run only UNRESOLVED because the+ # live time budget ran out before it could even be queried+ # (`budget_exhausted`) already got its own "could not be+ # resolved within the live time budget" finding above and was+ # never actually checked, so it does not get a second finding+ # naming a run number that was never queried.+ findings.append(+ Finding(+ "medium",+ "scanner-runtime",+ workflow,+ f"could not resolve this scanner's jobs in {parent} run #{newest_settled.database_id}; its result is unknown, not passing",+ )+ )++ # Measured from the last SUCCESSFUL run, as `classify_staleness`+ # does (generate_workflow_status.py:616-619): a scanner whose runs+ # in the window include NO success is stale unconditionally -- a+ # rule similar to the sibling's "no success ⇒ stale" (see+ # _stale_threshold_days for the differences) -- never "fresh" just+ # because its most recent (failing) run happens to be young.+ success = next(+ (run for run in runs if run.conclusion == "success"), None+ )+ stale = False+ if success is None:+ # `completed`, not `latest`: an in-flight or unresolved newest+ # run is not evidence one way or the other (R5) -- report the+ # last run that actually concluded something, exactly like the+ # FAILED_CONCLUSIONS finding above does.+ if completed is not None:+ stale = True+ findings.append(+ Finding(+ "medium",+ "scanner-runtime",+ workflow,+ f"no successful run in the query window (newest run "+ f"{_run_date(completed.created_at)}, {completed.conclusion}){gated}",+ )+ )+ else:+ age_days = _run_age_days(success.created_at, now)+ if age_days is None:+ # Fail closed: a timestamp we cannot parse is not evidence of+ # a recent run.+ stale = True+ notes.append(+ f"unparseable run timestamp {success.created_at!r} — treated as stale"+ )+ findings.append(+ Finding(+ "medium",+ "scanner-runtime",+ workflow,+ f"run #{success.database_id} has an unparseable createdAt ({success.created_at!r}); treated as stale{gated}",+ )+ )+ elif age_days > threshold_days:+ stale = True+ findings.append(+ Finding(+ "medium",+ "scanner-runtime",+ workflow,+ f"newest successful run is {int(age_days)} days old ({_run_date(success.created_at)}), past the {int(threshold_days)}-day staleness threshold{gated}",+ )+ )++ statuses.append(+ LiveStatus(+ workflow,+ runs,+ latest_conclusion,+ None,+ parent,+ stale,+ tuple(notes),+ )+ )++ return tuple(statuses)+++def recent_cicd_changes(limit: int = 10) -> GitHistory:+ command = [+ "git",+ "log",+ f"-{limit}",+ "--pretty=format:%h%x1f%ad%x1f%an%x1f%s%x1e",+ "--date=short",+ "--",+ *CI_PATHS,+ ]+ try:+ result = subprocess.run(+ command,+ cwd=ROOT,+ capture_output=True,+ text=True,+ timeout=15,+ check=False,+ )+ except subprocess.TimeoutExpired:+ return GitHistory((), "returncode 124")+ except OSError:+ return GitHistory((), "returncode unavailable")+ if result.returncode != 0:+ return GitHistory((), f"returncode {result.returncode}")++ changes: list[GitChange] = []+ for record in result.stdout.split("\x1e"):+ fields = record.strip().split("\x1f")+ if len(fields) == 4:+ changes.append(GitChange(*fields))+ return GitHistory(tuple(changes), None)+++def report_baseline() -> str:+ command = ["git", "log", "-1", "--format=%h", "--", *CI_PATHS]+ try:+ result = subprocess.run(+ command,+ cwd=ROOT,+ capture_output=True,+ text=True,+ timeout=15,+ check=False,+ )+ except (subprocess.TimeoutExpired, OSError):+ return "unknown"+ return (+ result.stdout.strip()+ if result.returncode == 0 and result.stdout.strip()+ else "unknown"+ )+++def _precommit_ratio(precommit: PrecommitAnalysis) -> float:+ if not precommit["present"] or precommit.get("parse_error"):+ return 0.0+ return (+ 0.4+ + (0.3 if precommit["has_secrets_hook"] else 0.0)+ + (0.3 if not precommit["floating_revs"] else 0.0)+ )+++def score_dimensions(+ workflows: WorkflowAnalysis,+ precommit: PrecommitAnalysis,+ scanners: tuple[ScannerCategory, ...],+) -> tuple[Dimension, ...]:+ required = tuple(category for category in scanners if category.required)+ return (+ Dimension(+ "Actions pinned by SHA",+ 35,+ workflows.pinned_uses / workflows.total_uses+ if workflows.total_uses+ else None,+ ),+ Dimension(+ "Explicit permissions",+ 15,+ workflows.permissions_ok_jobs / workflows.permissions_jobs+ if workflows.permissions_jobs+ else None,+ ),+ Dimension(+ "Harden-runner first",+ 15,+ workflows.harden_ok_jobs / workflows.non_exempt_jobs+ if workflows.non_exempt_jobs+ else None,+ ),+ Dimension(+ "Safe checkouts",+ 10,+ workflows.safe_checkouts / workflows.total_checkouts+ if workflows.total_checkouts+ else None,+ ),+ Dimension("Pre-commit", 10, _precommit_ratio(precommit)),+ Dimension(+ "Required scanners",+ 15,+ sum(category.satisfied for category in required) / len(required)+ if required+ else None,+ ),+ )+++def compute_score(dimensions: tuple[Dimension, ...]) -> int:+ active = tuple(+ dimension for dimension in dimensions if dimension.ratio is not None+ )+ denominator = sum(dimension.weight for dimension in active)+ raw = sum(+ dimension.weight * dimension.ratio+ for dimension in active+ if dimension.ratio is not None+ )+ return (+ max(0, min(100, round(100 * raw / denominator))) if denominator else 0+ )+++def grade(score: int) -> str:+ if score >= 90:+ return "A"+ if score >= 80:+ return "B"+ if score >= 70:+ return "C"+ if score >= 60:+ return "D"+ return "F"+++def _icon(ok: bool) -> str:+ return "PASS" if ok else "FAIL"+++def _ratio_detail(dimension: Dimension) -> str:+ if dimension.ratio is None:+ suffix = (+ "no checkouts"+ if dimension.name == "Safe checkouts"+ else "no applicable occurrences"+ )+ return f"N/A — {suffix}"+ return f"{round(100 * dimension.ratio)}% (weight {dimension.weight})"+++def _markdown_cell(value: str) -> str:+ escaped = (+ value.replace("\\", "\\\\")+ .replace("|", "\\|")+ .replace("\r", " ")+ .replace("\n", " ")+ )+ return "".join(+ character+ for character in escaped+ if ord(character) >= 32 and ord(character) != 127+ )+++_STATUS_LABELS: Final[dict[str, str]] = {+ "success": "PASS",+ "failure": "FAIL",+ "cancelled": "CANCELLED",+ "skipped": "SKIPPED",+ "timed_out": "TIMED_OUT",+ "startup_failure": "STARTUP_FAILURE",+ "action_required": "ACTION_REQUIRED",+ # `gh` also reports these two. "stale" is a gh conclusion meaning the run+ # outlived its log retention; it is NOT this report's staleness verdict,+ # so it is labelled distinctly rather than colliding with STALE.+ "neutral": "NEUTRAL",+ "stale": "STALE (gh)",+ # A run with no conclusion yet is in flight, not an unknowable state.+ "unknown": "RUNNING",+ # A gate run whose job list did not name this scanner: not measured.+ _UNRESOLVED: "UNRESOLVED",+}+++def _status_label(conclusion: str | None) -> str:+ if conclusion is None:+ return "—"+ return _STATUS_LABELS.get(conclusion, conclusion)+++def render(+ workflows: WorkflowAnalysis,+ precommit: PrecommitAnalysis,+ scanners: tuple[ScannerCategory, ...],+ history: GitHistory,+ findings: list[Finding],+ live: tuple[LiveStatus, ...] = (),+) -> tuple[str, int]:+ dimensions = score_dimensions(workflows, precommit, scanners)+ score = compute_score(dimensions)+ ordered_findings = sorted(+ findings,+ key=lambda item: (+ {"critical": 0, "high": 1, "medium": 2, "low": 3}[item.severity],+ item.area,+ item.file,+ item.message,+ ),+ )+ if live:+ intro = [+ "> Auto-generated by `scripts/generate_ci_health.py` (run by",+ "> `.github/workflows/ci-health-report.yml`). This reports",+ "> **static configuration health** plus a **live scanner status**",+ "> section; only the static half is scored. For live run status",+ "> across every workflow see",+ "> [`docs/ci/workflow-status.md`](../docs/ci/workflow-status.md).",+ ]+ else:+ intro = [+ "> Auto-generated by `scripts/generate_ci_health.py` (run by",+ "> `.github/workflows/ci-health-report.yml`). This reports",+ "> **static configuration health**, not live run status — for",+ "> that see [`docs/ci/workflow-status.md`](../docs/ci/workflow-status.md).",+ ]+ # NOTE: no line below may end in whitespace. Markdown hard breaks (two+ # trailing spaces) are stripped by the repo's `trailing-whitespace`+ # pre-commit hook (.pre-commit-config.yaml:9), which would make the+ # generated file impossible to commit unchanged.+ lines = [+ "# CI/CD Configuration Health",+ "",+ *intro,+ "",+ f"- **Report baseline:** `{report_baseline()}`",+ f"- **Workflows analyzed:** {len(workflows.files)}",+ f"- **Overall score:** **{score}/100** (grade **{grade(score)}**)",+ f"- **Findings:** {len(ordered_findings)}",+ "",+ "---",+ "",+ "## Summary",+ "",+ "| Check | Result | Detail |",+ "|---|---|---|",+ ]+ for dimension in dimensions:+ result = (+ "N/A" if dimension.ratio is None else _icon(dimension.ratio == 1.0)+ )+ lines.append(+ f"| {dimension.name} | {result} | {_ratio_detail(dimension)} |"+ )+ lines.extend(+ [+ "",+ "## Headline counts",+ "",+ f"- Non-local action references: **{workflows.pinned_uses}/{workflows.total_uses} pinned**",+ f"- Checkout occurrences: **{workflows.safe_checkouts}/{workflows.total_checkouts} safe**",+ f"- Non-exempt jobs: **{workflows.non_exempt_jobs}**",+ f"- Jobs missing harden-runner as step 0: **{len(workflows.missing_harden)}**",+ f"- Jobs without explicit permissions: **{len(workflows.missing_permissions)}**",+ "",+ ]+ )++ if ordered_findings:+ lines.extend(+ [+ "## Findings",+ "",+ "| Severity | Area | File/job | Finding |",+ "|---|---|---|---|",+ ]+ )+ for finding in ordered_findings:+ finding_row = (+ f"| {finding.severity.upper()} | {_markdown_cell(finding.area)} | "+ + f"{_markdown_cell(finding.file)} | {_markdown_cell(finding.message)} |"+ )+ lines.append(finding_row)+ lines.append("")++ unpinned = [+ (row.name, ref) for row in workflows.files for ref in row.unpinned+ ]+ if unpinned:+ lines.extend(["## Unpinned actions (not pinned by commit SHA)", ""])+ for filename, ref in sorted(unpinned):+ lines.append(f"- `{filename}`: `{ref}`")+ lines.append("")++ lines.extend(+ [+ "## Security-scanner coverage",+ "",+ "| Category | Requirement | Status | Evidence |",+ "|---|---|---|---|",+ ]+ )+ for category in scanners:+ evidence = ", ".join(category.workflows)+ if category.dependabot_config:+ evidence = (+ f"{evidence}, .github/dependabot.yml"+ if evidence+ else ".github/dependabot.yml"+ )+ scanner_row = (+ f"| {category.name} | {'required' if category.required else 'optional'} | "+ + f"{'SATISFIED' if category.satisfied else 'MISSING'} | {evidence or '—'} |"+ )+ lines.append(scanner_row)+ lines.append("")++ if live:+ lines.extend(+ [+ "",+ "## Live scanner status",+ "",+ "_Queried via `gh run list` (up to 5 most recent `main` runs per",+ "scanner workflow; pull-request events excluded because a fork",+ "PR branch may itself be named `main`). A scanner with no",+ "default-branch history of its own is read through the gate that",+ "calls it, and each gate run is resolved to that scanner's OWN",+ "job conclusions (`gh run view --json jobs`) — the gate's roll-up",+ "is never shown under a scanner's name; `UNRESOLVED` covers",+ "causes such as a job list that could not be read or didn't",+ "name this scanner, or the live time budget running out",+ "before it could be checked. Runtime status, not static",+ "configuration — a failed run here may be transient, and it",+ "does not affect the score; a `NO RUNS` scanner does not",+ "count as coverage for its category._",+ "",+ "| Workflow | Latest | Last run | Recent runs (newest first) | Note |",+ "|---|---|---|---|---|",+ ]+ )+ any_unavailable = False+ any_no_runs = False+ any_unresolved = False+ for entry in live:+ if entry.unavailable:+ any_unavailable = True+ lines.append(+ f"| `{_markdown_cell(entry.workflow)}` | — | — | — "+ f"| _unavailable: {_markdown_cell(entry.unavailable)}_ |"+ )+ continue+ latest = entry.runs[0] if entry.runs else None+ completed = next(+ (+ run+ for run in entry.runs+ if run.conclusion not in _INCONCLUSIVE+ ),+ None,+ )+ any_unresolved = any_unresolved or any(+ run.conclusion == _UNRESOLVED for run in entry.runs+ )+ notes: list[str] = []+ if entry.gated_by:+ # The runs are the gate's, but every conclusion in this row is+ # this scanner's own job result inside them.+ notes.append(+ f"gated by `{entry.gated_by}` (this scanner's own jobs)"+ )+ notes.extend(entry.notes)+ if latest is None:+ # R8: an empty history is absence of evidence. Rendering it as+ # an em dash next to PASS rows reads as "nothing to report".+ any_no_runs = True+ latest_label = "NO RUNS"+ last_run = "—"+ notes.append("no qualifying run on `main` in the query window")+ else:+ last_run = _run_date(latest.created_at)+ if latest.conclusion == "unknown":+ # R7: in flight. Show it as such and carry the previous+ # conclusion, so a racing push cannot mask a red scanner.+ latest_label = "RUNNING"+ previous = (+ _status_label(completed.conclusion)+ if completed is not None+ else "none"+ )+ notes.append(f"in flight — previous run {previous}")+ elif latest.conclusion == _UNRESOLVED:+ latest_label = "UNRESOLVED"+ if latest.unresolved_reason == "job_list_unreadable":+ # `gh run view --json jobs` failed/timed out/returned+ # unparseable JSON: nothing was learned about this+ # scanner, so say so rather than guessing it was+ # simply absent from a list that was never read.+ notes.append(+ "the gate run's job list could not be read"+ )+ elif latest.unresolved_reason not in (+ "budget_exhausted",+ "gate_has_no_caller_job",+ ):+ # `latest` here is genuinely fetched and resolved --+ # its job list was checked and did not name this+ # scanner. Skipped for two reasons that already have+ # their own explanation and would otherwise duplicate+ # it: the run synthesized because the live time+ # budget ran out before it could even be queried+ # (`budget_exhausted` -- its job list was never+ # checked, so claiming its name was absent would be a+ # guess, and the truncation note already explains+ # it), and a gate with no job calling this workflow+ # at all (`gate_has_no_caller_job` -- the entry-level+ # note above already says so; repeating it here would+ # be the same cause twice). A genuinely unresolved+ # run keeps this note even when OTHER runs in the+ # same row were truncated by the budget.+ notes.append(+ "the gate run's job list did not name this scanner"+ )+ else:+ latest_label = _status_label(latest.conclusion)+ if entry.stale:+ # Staleness is measured on the last SUCCESSFUL run, so the+ # date named here is that run's, not necessarily the newest.+ success = next(+ (+ run+ for run in entry.runs+ if run.conclusion == "success"+ ),+ None,+ )+ if success is None:+ # `completed`, not `latest`: an in-flight/unresolved+ # newest run is not evidence either way (R5).+ named = completed if completed is not None else latest+ notes.append(+ "no successful run in the query window (newest "+ f"run {_run_date(named.created_at)}, "+ f"{named.conclusion})"+ )+ else:+ notes.append(+ f"last successful run {_run_date(success.created_at)}"+ " is past the staleness threshold"+ )+ if latest_label == _status_label("success"):+ # A stale PASS is the lie this label exists to stop.+ latest_label = "STALE"+ recent = (+ " ".join(_status_label(run.conclusion) for run in entry.runs)+ or "—"+ )+ if (+ completed is not None+ and completed.conclusion in _FAILED_CONCLUSIONS+ ):+ notes.append("latest run did not pass — see Actions tab")+ lines.append(+ f"| `{_markdown_cell(entry.workflow)}` | {latest_label} "+ f"| {last_run} | {recent} "+ f"| {_markdown_cell('; '.join(notes) or '—')} |"+ )+ lines.append("")+ if any_no_runs:+ lines.extend(+ [+ "_`NO RUNS` means no qualifying default-branch run was found — absence of evidence, not a healthy scanner._",+ "",+ ]+ )+ if any_unresolved:+ lines.extend(+ [+ "_`UNRESOLVED` covers causes such as a job list that could not be read or didn't name that scanner, or gate history that could not be resolved within the live time budget — absence of evidence, not a passing or failing scan._",+ "",+ ]+ )+ if any_unavailable:+ lines.extend(+ [+ "_Some entries unavailable — `gh` may not be installed or authenticated, or every fetched run was a pull-request run._",+ "",+ ]+ )++ lines.extend(["## Pre-commit hooks", ""])+ parse_error = precommit.get("parse_error")+ if parse_error:+ lines.extend(+ [+ f"`.pre-commit-config.yaml` YAML parse error: `{_markdown_cell(parse_error)}`",+ "",+ ]+ )+ elif not precommit["present"]:+ lines.extend(["`.pre-commit-config.yaml` is not present.", ""])+ else:+ lines.append(+ f"{precommit['hook_count']} hooks configured in `.pre-commit-config.yaml`."+ )+ lines.append(+ f"{precommit.get('tag_or_unknown_count', 0)} hooks use a tag or otherwise non-SHA static revision."+ )+ if precommit["floating_revs"]:+ lines.extend(["", "### Hooks not pinned to a commit SHA", ""])+ for hook in precommit["floating_revs"]:+ lines.append(+ f"- `{hook.hook_id}` — `{hook.repo}` @ `{hook.rev or '<absent>'}`"+ )+ lines.append("")++ lines.extend(["## Recent CI/CD config changes", ""])+ if history.unavailable:+ lines.append(f"_Git history unavailable ({history.unavailable})._")+ elif history.changes:+ lines.extend(+ ["| Commit | Date | Author | Subject |", "|---|---|---|---|"]+ )+ for change in history.changes:+ change_row = (+ f"| `{_markdown_cell(change.sha)}` | {_markdown_cell(change.date)} | "+ + f"{_markdown_cell(change.author)} | {_markdown_cell(change.subject)} |"+ )+ lines.append(change_row)+ else:+ lines.append("_No recent CI/CD config changes found._")+ lines.extend(+ [+ "",+ "---",+ "",+ "Regenerate locally: `python scripts/generate_ci_health.py`",+ "",+ ]+ )+ return "\n".join(lines), score+++def _write_github_output(score: int, findings: list[Finding]) -> bool:+ output_path = os.environ.get("GITHUB_OUTPUT")+ if not output_path:+ print(+ "--github-output requires GITHUB_OUTPUT to be set", file=sys.stderr+ )+ return False+ critical = sum(finding.severity == "critical" for finding in findings)+ high = sum(finding.severity == "high" for finding in findings)+ with pathlib.Path(output_path).open("a", encoding="utf-8") as output:+ github_values = (+ f"score={score}\ngrade={grade(score)}\nfindings={len(findings)}\n"+ + f"findings_critical={critical}\nfindings_high={high}\n"+ )+ _ = output.write(github_values)+ return True+++class CliArgs(argparse.Namespace):+ stdout: bool = False+ check: bool = False+ out: str = str(OUT_FILE)+ github_output: bool = False+ live: bool = False+++def main() -> int:+ parser = argparse.ArgumentParser(description=__doc__)+ _ = parser.add_argument(+ "--stdout",+ action="store_true",+ help="print report instead of writing file",+ )+ _ = parser.add_argument(+ "--check",+ action="store_true",+ help="exit 1 on critical or high findings",+ )+ _ = parser.add_argument("--out", default=str(OUT_FILE), help="output path")+ _ = parser.add_argument(+ "--github-output",+ action="store_true",+ help="append summary values to $GITHUB_OUTPUT",+ )+ _ = parser.add_argument(+ "--live",+ action="store_true",+ help="query recent scanner runs; combine with --check to fail on latest failures",+ )+ args = parser.parse_args(namespace=CliArgs())++ findings: list[Finding] = []+ workflows = analyze_workflows(findings)+ precommit = analyze_precommit(findings)+ scanners = analyze_scanners(findings)+ live = analyze_live_status(+ findings,+ _scanner_workflow_files(),+ enabled=args.live,+ )+ scanners = discount_idle_scanners(scanners, live, findings)+ report, score = render(+ workflows,+ precommit,+ scanners,+ recent_cicd_changes(),+ findings,+ live,+ )++ if args.stdout:+ print(report)+ else:+ requested = pathlib.Path(args.out)+ output_path = requested if requested.is_absolute() else ROOT / requested+ output_path.parent.mkdir(parents=True, exist_ok=True)+ _ = output_path.write_text(report, encoding="utf-8")+ try:+ display_path = output_path.relative_to(ROOT)+ except ValueError:+ display_path = output_path+ print(f"Wrote {display_path} (score {score}/100, grade {grade(score)})")++ if args.github_output and not _write_github_output(score, findings):+ return 2+ gate_findings = [+ finding+ for finding in findings+ if finding.severity in ("critical", "high")+ ]+ if args.check and gate_findings:+ print(+ f"CI health has {len(gate_findings)} critical/high finding(s) — failing gate.",+ file=sys.stderr,+ )+ return 1+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/tests/ci/test_ci_health.py b/tests/ci/test_ci_health.pynew file mode 100644index 0000000000..aa5d3c37ec--- /dev/null+++ b/tests/ci/test_ci_health.py@@ -0,0 +1,3045 @@+"""Regression tests for ``scripts/generate_ci_health.py``.++These tests lock in the correctness fixes from the analyzer review:++* structural YAML parsing (not regex), counting EVERY ``uses:`` occurrence —+ both the named form (``- name: X\\n uses: Y``) and the unnamed form+ (``- uses: Y``) — with NO deduplication;+* per-occurrence pinning classification (local / pinned-SHA / unpinned-tag /+ expression / docker-digest);+* checkout settings paired to their step's ``with.persist-credentials``;+* per-job harden-runner assessment with reusable-workflow jobs exempt;+* effective permissions (top-level or job-level);+* pre-commit ``local``/``meta`` exempt from floating-rev detection;+* malformed pre-commit YAML rendered as a finding, never a traceback;+* byte-stable report output (no wall-clock timestamp);+* ``--check`` gate fails on raw critical/high findings, not a rounded score;+* ``--out`` accepts absolute paths; ``--github-output`` emits all fields;+* ``--live`` degrades to "unavailable" instead of silently reporting health+ when ``gh`` is missing, erroring, or returning junk, raises a ``high``+ finding for every conclusion in ``_FAILED_CONCLUSIONS``, never raises a+ ``high`` finding for deliberate ``cancelled``/``skipped`` outcomes, and+ queries only ``main``;+* a window whose runs include NO success is stale unconditionally, like the+ sibling's "no success ⇒ stale" rule — never "fresh" just because+ its most recent (failing, cancelled, ...) run happens to be young — and+ raises a ``medium`` finding that names the newest run's OWN conclusion,+ never relabelling it a success;+* no rendered line ends in whitespace (the repo runs the bare+ ``trailing-whitespace`` hook, so anything else could never be committed);+* live status is default-branch status: ``pull_request`` runs are excluded+ even when their head branch is called ``main`` (fork default branches are);+* a scanner with no default-branch history of its own is read through the+ gate that calls it, and every conclusion shown under its name is resolved+ to that scanner's OWN jobs inside the gate run — a red gate summary next to+ green scanner jobs must not paint the scanners red, an unresolved job list+ renders ``UNRESOLVED`` (never ``FAIL``), and a failed scanner job is a+ ``high`` finding because it is now a measurement;+* the staleness threshold uses the sibling's formula+ (``max(60.0, STALE_MULTIPLIER * min cadence)`` measured on the last+ SUCCESSFUL run) — similar, not identical: no-cron workflows get the floor,+ gated scanners use the gate's cadence, there is no new-file exemption and+ ages are fractional days — and a run older than it renders ``STALE`` with+ its date instead of ``PASS``;+* an empty history renders ``NO RUNS`` with a ``medium`` finding and does+ not count as coverage for its category, and an in-flight run renders+ ``RUNNING`` with the previous conclusion — neither masquerades as healthy;+* the workflow's comments do not claim wiring (a gating step) that no step+ performs — asserted structurally, over the parsed ``run:`` commands, so an+ explanatory comment cannot satisfy the assertion.++Run: ``pytest tests/ci/test_ci_health.py -v``+"""++from __future__ import annotations++import datetime+import importlib.util+import pathlib+import sys++import pytest++# The analyzer lives in scripts/ (not a package), so load it by path.+_SCRIPT = (+ pathlib.Path(__file__).resolve().parents[2]+ / "scripts"+ / "generate_ci_health.py"+)+_spec = importlib.util.spec_from_file_location("generate_ci_health", _SCRIPT)+assert _spec is not None and _spec.loader is not None+cih = importlib.util.module_from_spec(_spec)+sys.modules["generate_ci_health"] = cih+_spec.loader.exec_module(cih)+++SHA = "1af3b93b6815bc44a9784bd300feb67ff0d1eeb3" # 40-hex, real actions/checkout sha+SHA2 = "0a1b2c3d4e5f60718293a4b5c6d7e8f901234567"+++def _run_cli(argv: list[str]) -> int:+ """Invoke the analyzer's main() with a synthetic argv (it uses argparse)."""+ old = sys.argv+ sys.argv = ["generate_ci_health", *argv]+ try:+ return cih.main()+ finally:+ sys.argv = old+++# --------------------------------------------------------------------------- #+# Pure helpers+# --------------------------------------------------------------------------- #+class TestPinningKind:+ @pytest.mark.parametrize(+ "ref",+ [+ "./.github/actions/build",+ "$/.github/actions/build",+ "$/.github/workflows/release.yml",+ ],+ )+ def test_local_action_excluded(self, ref):+ assert cih._pinning_kind(ref) == "local"++ def test_docker_digest_pinned(self):+ assert (+ cih._pinning_kind("docker://alpine@sha256:" + "a" * 64) == "pinned"+ )++ def test_docker_tag_unpinned(self):+ assert cih._pinning_kind("docker://alpine:3.19") == "unpinned"++ def test_expression_unverifiable(self):+ assert (+ cih._pinning_kind("actions/checkout@${{ inputs.version }}")+ == "expression"+ )++ def test_sha_pinned(self):+ assert cih._pinning_kind(f"actions/checkout@{SHA}") == "pinned"++ def test_sha_pinned_case_insensitive(self):+ assert cih._pinning_kind(f"actions/checkout@{SHA.upper()}") == "pinned"++ def test_floating_tag_unpinned(self):+ assert cih._pinning_kind("actions/checkout@v4") == "unpinned"++ def test_branch_unpinned(self):+ assert cih._pinning_kind("owner/repo@main") == "unpinned"++ def test_marketplace_subpath_pinned(self):+ # owner/repo/sub/path@sha must classify on the @sha, not the slash.+ assert (+ cih._pinning_kind(f"owner/repo/.github/actions/foo@{SHA2}")+ == "pinned"+ )+++def test_self_repository_actions_and_workflows_do_not_raise_pinning_findings():+ workflow = {+ "jobs": {+ "build": {"steps": [{"uses": "$/.github/actions/build"}]},+ "release": {"uses": "$/.github/workflows/release.yml"},+ }+ }+ findings = []+ analysis = _assert_via_tempfile(+ workflow, findings, tmp_checkouts=0, tmp_safe=0+ )+ assert analysis.total_uses == 0+ assert not [finding for finding in findings if finding.area == "pinning"]+++class TestActionHead:+ def test_simple(self):+ assert cih._action_head("actions/checkout@v4") == "actions/checkout"++ def test_subpath(self):+ assert (+ cih._action_head("owner/repo/.github/actions/foo@sha1")+ == "owner/repo/.github/actions/foo"+ )++ def test_no_at(self):+ assert cih._action_head("./local-action") == "./local-action"+++class TestIsFalse:+ @pytest.mark.parametrize("value", [False, "false", "False", "FALSE"])+ def test_falsey(self, value):+ assert cih._is_false(value) is True++ @pytest.mark.parametrize("value", [None, True, "true", 0, ""])+ def test_not_falsey(self, value):+ assert cih._is_false(value) is False+++# --------------------------------------------------------------------------- #+# Structural parsing — occurrence counting, no dedup, unnamed steps+# --------------------------------------------------------------------------- #+def _uses_in(parsed: object) -> list[str]:+ found: list[str] = []+ for mapping, _is_step in cih._iter_action_mappings(parsed):+ if isinstance(mapping, dict):+ v = mapping.get("uses")+ if isinstance(v, str):+ found.append(v.strip())+ return found+++class TestStructuralParsing:+ def test_unnamed_uses_step_is_counted(self):+ """The bug: `- uses:` (no `- name:`) was missed by the old regex."""+ parsed = {+ "jobs": {+ "a": {+ "steps": [+ {"uses": f"actions/checkout@{SHA}"}, # unnamed+ {"name": "X", "uses": f"actions/setup-python@{SHA}"},+ ]+ }+ }+ }+ assert len(_uses_in(parsed)) == 2++ def test_duplicate_occurrences_not_collapsed(self):+ """The bug: repeated refs were deduped, hiding unpinned copies."""+ parsed = {+ "jobs": {+ "a": {+ "steps": [+ {"uses": f"actions/checkout@{SHA}"},+ {+ "uses": f"actions/checkout@{SHA}"+ }, # same ref, second occurrence+ {+ "uses": "actions/checkout@v4"+ }, # unpinned third occurrence+ ]+ }+ }+ }+ assert len(_uses_in(parsed)) == 3++ def test_nested_composite_and_matrix(self):+ parsed = {+ "jobs": {+ "build": {"steps": [{"uses": f"actions/checkout@{SHA}"}]},+ "matrix": {+ "strategy": {"matrix": {"x": [{"uses": "deep@v1"}]}}+ },+ }+ }+ uses = _uses_in(parsed)+ assert uses == [f"actions/checkout@{SHA}"]+++class TestCheckoutPairing:+ def test_safe_checkout_counted(self):+ findings = []+ wf = {+ "jobs": {+ "a": {+ "steps": [+ {+ "uses": f"step-security/harden-runner@{SHA}",+ "with": {"egress-policy": "audit"},+ },+ {+ "uses": f"actions/checkout@{SHA}",+ "with": {"persist-credentials": False},+ },+ ]+ }+ }+ }+ # Drive analyze_workflows via a temp workflow file + monkeypatch.+ _assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)++ def test_unsafe_checkout_flagged(self):+ findings = []+ wf = {+ "jobs": {+ "a": {+ "steps": [+ {+ "uses": f"step-security/harden-runner@{SHA}",+ "with": {"egress-policy": "audit"},+ },+ {+ "uses": f"actions/checkout@{SHA}"+ }, # no persist-credentials+ ]+ }+ }+ }+ _assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=0)+ assert any(+ f.area == "checkout" and f.severity == "high" for f in findings+ )++ def test_string_false_accepted(self):+ findings = []+ wf = {+ "jobs": {+ "a": {+ "steps": [+ {+ "uses": f"step-security/harden-runner@{SHA}",+ "with": {"egress-policy": "audit"},+ },+ {+ "uses": f"actions/checkout@{SHA}",+ "with": {"persist-credentials": "false"},+ },+ ]+ }+ }+ }+ _assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)++ def test_exact_action_identity_checkout_other_not_checkout(self):+ findings = []+ wf = {+ "permissions": {},+ "jobs": {+ "a": {+ "steps": [+ {"uses": f"step-security/harden-runner@{SHA}"},+ {"uses": f"actions/checkout-other@{SHA}"},+ ]+ }+ },+ }+ _assert_via_tempfile(wf, findings, tmp_checkouts=0, tmp_safe=0)+++class TestReusableJobExempt:+ def test_job_level_uses_not_flagged_for_harden(self):+ """The bug: reusable-workflow wrappers were flagged missing harden-runner."""+ findings = []+ wf = {+ "jobs": {+ "call": {+ "uses": "./.github/workflows/reusable.yml"+ }, # no steps → exempt+ "ordinary": {+ "steps": [+ {+ "uses": f"step-security/harden-runner@{SHA}",+ "with": {"egress-policy": "audit"},+ },+ {+ "uses": f"actions/checkout@{SHA}",+ "with": {"persist-credentials": False},+ },+ ]+ },+ }+ }+ analysis = _assert_via_tempfile(+ wf, findings, tmp_checkouts=1, tmp_safe=1+ )+ assert analysis.non_exempt_jobs == 1 # only "ordinary" counts+ assert analysis.missing_harden == ()+ assert not any(+ "call" in f.file for f in findings if f.area == "harden-runner"+ )++ def test_steps_absent_job_is_malformed(self):+ """A job without steps or a reusable call is invalid and must fail --check."""+ findings = []+ wf = {+ "permissions": {},+ "jobs": {"norunner": {"runs-on": "ubuntu-latest"}},+ }+ analysis = _assert_via_tempfile(+ wf, findings, tmp_checkouts=0, tmp_safe=0+ )+ assert analysis.non_exempt_jobs == 0+ assert any(+ f.severity == "high" and "neither steps nor uses" in f.message+ for f in findings+ )++ def test_reusable_job_exempt_from_harden_but_permissions_assessed(self):+ findings = []+ wf = {"jobs": {"call": {"uses": "./.github/workflows/x.yml"}}}+ analysis = _assert_via_tempfile(+ wf, findings, tmp_checkouts=0, tmp_safe=0+ )+ assert analysis.non_exempt_jobs == 0+ assert analysis.permissions_jobs == 1+ assert any(+ f.area == "permissions" and "call" in f.file for f in findings+ )+ assert not any(f.area == "harden-runner" for f in findings)++ def test_exact_action_identity_harden_lookalike_not_harden(self):+ findings = []+ wf = {+ "permissions": {},+ "jobs": {+ "a": {+ "steps": [+ {+ "uses": f"step-security/harden-runner-lookalike@{SHA}"+ },+ ]+ }+ },+ }+ analysis = _assert_via_tempfile(+ wf, findings, tmp_checkouts=0, tmp_safe=0+ )+ assert analysis.harden_ok_jobs == 0+ assert any(f.area == "harden-runner" for f in findings)+++class TestPermissions:+ def test_top_level_permissions_cover_all_jobs(self):+ findings = []+ wf = {+ "permissions": {"contents": "read"},+ "jobs": {+ "a": {+ "steps": [+ {+ "uses": f"step-security/harden-runner@{SHA}",+ "with": {"egress-policy": "audit"},+ },+ {+ "uses": f"actions/checkout@{SHA}",+ "with": {"persist-credentials": False},+ },+ ]+ }+ },+ }+ _assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)+ assert not any(f.area == "permissions" for f in findings)++ def test_missing_permissions_flagged(self):+ findings = []+ wf = {+ "jobs": {+ "a": {+ "steps": [+ {+ "uses": f"step-security/harden-runner@{SHA}",+ "with": {"egress-policy": "audit"},+ },+ {+ "uses": f"actions/checkout@{SHA}",+ "with": {"persist-credentials": False},+ },+ ]+ }+ }+ }+ _assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)+ assert any(f.area == "permissions" for f in findings)++ def test_empty_permissions_object_counts(self):+ findings = []+ wf = {+ "permissions": {}, # the minimal `{}` form is an explicit declaration+ "jobs": {+ "a": {+ "steps": [+ {+ "uses": f"step-security/harden-runner@{SHA}",+ "with": {"egress-policy": "audit"},+ },+ {+ "uses": f"actions/checkout@{SHA}",+ "with": {"persist-credentials": False},+ },+ ]+ }+ },+ }+ _assert_via_tempfile(wf, findings, tmp_checkouts=1, tmp_safe=1)+ assert not any(f.area == "permissions" for f in findings)++ def test_read_all_permissions_string_counts(self):+ findings = []+ wf = {+ "permissions": "read-all",+ "jobs": {"call": {"uses": "./.github/workflows/x.yml"}},+ }+ analysis = _assert_via_tempfile(+ wf, findings, tmp_checkouts=0, tmp_safe=0+ )+ assert analysis.permissions_jobs == 1+ assert analysis.permissions_ok_jobs == 1+ assert not any(f.area == "permissions" for f in findings)+++# --------------------------------------------------------------------------- #+# Pre-commit analysis+# --------------------------------------------------------------------------- #+class TestPrecommit:+ def test_local_not_floating(self, monkeypatch, tmp_path):+ cfg = {"repos": [{"repo": "local", "hooks": [{"id": "my-hook"}]}]}+ pc = _run_precommit(monkeypatch, tmp_path, cfg)+ assert pc["floating_revs"] == []++ def test_meta_not_floating(self, monkeypatch, tmp_path):+ cfg = {+ "repos": [+ {"repo": "meta", "rev": "", "hooks": [{"id": "check-yaml"}]}+ ]+ }+ pc = _run_precommit(monkeypatch, tmp_path, cfg)+ assert pc["floating_revs"] == []++ def test_branch_rev_is_floating(self, monkeypatch, tmp_path):+ cfg = {+ "repos": [+ {+ "repo": "https://github.com/owner/repo",+ "rev": "main",+ "hooks": [{"id": "x"}],+ }+ ]+ }+ pc = _run_precommit(monkeypatch, tmp_path, cfg)+ assert len(pc["floating_revs"]) == 1++ def test_sha_rev_not_floating(self, monkeypatch, tmp_path):+ cfg = {+ "repos": [+ {+ "repo": "https://github.com/owner/repo",+ "rev": SHA,+ "hooks": [{"id": "x"}],+ }+ ]+ }+ pc = _run_precommit(monkeypatch, tmp_path, cfg)+ assert pc["floating_revs"] == []++ def test_release_tag_not_floating(self, monkeypatch, tmp_path):+ cfg = {+ "repos": [+ {+ "repo": "https://github.com/owner/repo",+ "rev": "v1.2.3",+ "hooks": [{"id": "x"}],+ }+ ]+ }+ pc = _run_precommit(monkeypatch, tmp_path, cfg)+ assert pc["floating_revs"] == []+ assert pc["hooks"][0].revision_kind == "tag_or_unknown"+++class TestMalformedWorkflows:+ def test_multi_document_workflow_rejected(self, monkeypatch, tmp_path):+ path = tmp_path / "multi.yml"+ path.write_text(+ f"jobs:\n a:\n uses: owner/repo@{SHA}\n---\njobs:\n b:\n uses: owner/other@{SHA2}\n",+ encoding="utf-8",+ )+ monkeypatch.setattr(cih, "_workflow_files", lambda: (path,))+ findings = []+ analysis = cih.analyze_workflows(findings)+ assert analysis.total_uses == 0+ assert any(+ f.severity == "high" and "multi-document" in f.message+ for f in findings+ )+++class TestScanners:+ def test_required_category_satisfied_and_missing(+ self, monkeypatch, tmp_path+ ):+ codeql = tmp_path / "codeql.yml"+ codeql.write_text("name: CodeQL\n", encoding="utf-8")+ monkeypatch.setattr(cih, "_workflow_files", lambda: (codeql,))+ monkeypatch.setattr(cih, "DEPENDABOT_FILE", tmp_path / "dependabot.yml")+ findings = []+ categories = {row.name: row for row in cih.analyze_scanners(findings)}+ assert categories["SAST"].satisfied is True+ assert categories["Secrets"].satisfied is False+ assert any(+ f.severity == "medium" and f.file == "Secrets" for f in findings+ )++ def test_missing_optional_category_emits_no_finding(+ self, monkeypatch, tmp_path+ ):+ """Only REQUIRED categories emit findings when unsatisfied. An unsatisfied+ OPTIONAL category (DAST, Containers, IaC, …) must be reported as+ not-satisfied but NEVER produce a scanner finding."""+ codeql = tmp_path / "codeql.yml"+ codeql.write_text("name: CodeQL\n", encoding="utf-8")+ monkeypatch.setattr(cih, "_workflow_files", lambda: (codeql,))+ monkeypatch.setattr(cih, "DEPENDABOT_FILE", tmp_path / "nope.yml")+ findings = []+ rows = cih.analyze_scanners(findings)+ optional_names = {r.name for r in rows if not r.required}+ assert optional_names, (+ "no optional scanner categories — analyzer contract changed"+ )+ unsatisfied_optional = {+ r.name for r in rows if not r.required and not r.satisfied+ }+ assert unsatisfied_optional, (+ "expected ≥1 unsatisfied optional category in this fixture"+ )+ scanner_finding_files = {+ f.file for f in findings if f.area == "scanners"+ }+ assert not (unsatisfied_optional & scanner_finding_files), (+ f"optional categories must not produce findings: "+ f"{unsatisfied_optional & scanner_finding_files}"+ )++ def test_test_workflows_are_not_scanners(self, monkeypatch, tmp_path):+ """``gitleaks-rule-tests.yml`` tests the gitleaks config; it is not+ Secrets-scanner evidence and gets no live tracking."""+ paths = []+ for name in (+ "gitleaks.yml",+ "gitleaks-rule-tests.yml",+ "codeql-test.yml",+ ):+ path = tmp_path / name+ path.write_text("name: X\n", encoding="utf-8")+ paths.append(path)+ monkeypatch.setattr(cih, "_workflow_files", lambda: tuple(paths))+ monkeypatch.setattr(cih, "DEPENDABOT_FILE", tmp_path / "nope.yml")+ findings = []+ categories = {row.name: row for row in cih.analyze_scanners(findings)}+ assert categories["Secrets"].workflows == ("gitleaks.yml",)+ assert categories["SAST"].satisfied is False+ assert cih._scanner_workflow_files() == ("gitleaks.yml",)++ def test_idle_scanner_does_not_count_for_its_category(+ self, monkeypatch, tmp_path+ ):+ """A scanner with NO RUNS in the live window is not coverage: it is+ dropped from its category's evidence, and a required category left+ empty is unsatisfied (scored and reported) like a missing one."""+ paths = []+ for name in (+ "codeql.yml",+ "gitleaks.yml",+ "nuclei.yml",+ "owasp-zap-scan.yml",+ ):+ path = tmp_path / name+ path.write_text("name: X\n", encoding="utf-8")+ paths.append(path)+ monkeypatch.setattr(cih, "_workflow_files", lambda: tuple(paths))+ monkeypatch.setattr(cih, "DEPENDABOT_FILE", tmp_path / "nope.yml")+ findings = []+ scanners = cih.analyze_scanners(findings)+ run = cih.WorkflowRun(+ 1, "success", "push", "main", "2026-01-01T00:00:00Z"+ )+ live = (+ cih.LiveStatus("codeql.yml", (run,), "success", None),+ cih.LiveStatus("gitleaks.yml", (), None, None),+ cih.LiveStatus("nuclei.yml", (), None, None),+ cih.LiveStatus("owasp-zap-scan.yml", (), None, "gh timed out"),+ )+ before = len(findings)+ categories = {+ row.name: row+ for row in cih.discount_idle_scanners(scanners, live, findings)+ }+ assert categories["SAST"].satisfied is True+ assert categories["Secrets"].satisfied is False+ assert categories["Secrets"].workflows == ()+ # An unavailable query is not evidence of an idle scanner.+ assert categories["DAST"].workflows == ("owasp-zap-scan.yml",)+ assert categories["DAST"].satisfied is True+ assert [(f.severity, f.file) for f in findings[before:]] == [+ ("medium", "Secrets")+ ]+ required = cih.score_dimensions(+ cih.analyze_workflows([]),+ cih.analyze_precommit([]),+ tuple(categories.values()),+ )[-1]+ assert required.name == "Required scanners"+ assert required.ratio is not None and required.ratio < 1.0++ def test_malformed_yaml_returns_parse_error_not_traceback(+ self, monkeypatch, tmp_path+ ):+ pc_file = tmp_path / ".pre-commit-config.yaml"+ pc_file.write_text(+ "repos:\n - repo: x\nbad: [unclosed", encoding="utf-8"+ )+ findings = []+ pc = cih.analyze_precommit(findings, path=pc_file)+ assert pc["present"] is True+ assert "parse_error" in pc+ assert pc["hook_count"] == 0+ assert any(+ f.severity in ("high", "medium")+ and "parse error" in f.message.casefold()+ for f in findings+ )+++# --------------------------------------------------------------------------- #+# Real-repo invariants + CLI contract+# --------------------------------------------------------------------------- #+class TestRealRepoInvariants:+ """Guardian tests against the actual repository tree (like+ test_release_gate_integrity.py). These assert the POLICY holds on the+ real workflows, not a frozen count."""++ def test_all_actions_pinned(self):+ findings = []+ wf = cih.analyze_workflows(findings)+ assert wf.total_uses > 0, "no actions found — analyzer is broken"+ assert wf.pinned_uses == wf.total_uses, (+ f"{wf.total_uses - wf.pinned_uses} unpinned action(s); "+ f"findings: {[f for f in findings if f.area == 'pinning']}"+ )++ def test_all_checkouts_safe(self):+ findings = []+ wf = cih.analyze_workflows(findings)+ assert wf.total_checkouts > 0+ assert wf.safe_checkouts == wf.total_checkouts++ def test_no_missing_harden_on_non_exempt_jobs(self):+ findings = []+ cih.analyze_workflows(findings)+ # The previously-false-positive reusable wrappers must NOT appear.+ # (osv-scanner.yml, osv-scanner-scheduled.yml, ui-full-shards.yml)+ harden_files = [f.file for f in findings if f.area == "harden-runner"]+ for wrapper in (+ "osv-scanner.yml",+ "osv-scanner-scheduled.yml",+ "ui-full-shards.yml",+ ):+ assert not any(wrapper in filename for filename in harden_files), (+ f"{wrapper} incorrectly flagged (reusable wrapper should be exempt): {harden_files}"+ )++ def test_report_is_byte_stable(self):+ a, _ = cih.render(+ cih.analyze_workflows([]),+ cih.analyze_precommit(),+ cih.analyze_scanners([]),+ cih.recent_cicd_changes(),+ [],+ )+ b, _ = cih.render(+ cih.analyze_workflows([]),+ cih.analyze_precommit(),+ cih.analyze_scanners([]),+ cih.recent_cicd_changes(),+ [],+ )+ assert a == b, (+ "report output is not deterministic (volatile timestamp?)"+ )++ def test_render_never_reads_the_clock(self):+ """Freezing ``_utcnow`` proves nothing about ``render()`` unless+ ``render()`` is the only thing that could read a clock: it never+ calls ``_utcnow``, so assert structurally that it reads no clock at+ all."""+ import inspect++ source = inspect.getsource(cih.render)+ for forbidden in ("datetime.now", "utcnow", "time.time"):+ assert forbidden not in source, (+ f"render() reads the wall clock via {forbidden}; the report "+ "would change byte-for-byte on every run"+ )++ def test_live_dates_come_from_created_at_not_the_clock(self, monkeypatch):+ """The live table is the one part of the report that carries dates.+ With the clock frozen at a sentinel instant and the run entries+ fixed, every date rendered must be a ``createdAt``."""+ frozen = datetime.datetime(+ 2019, 3, 4, 13, 37, 5, tzinfo=datetime.timezone.utc+ )+ monkeypatch.setattr(cih, "_utcnow", lambda: frozen)+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry(+ "success",+ databaseId=11,+ createdAt="2026-01-02T03:04:05Z",+ ),+ _run_entry(+ "success",+ databaseId=10,+ createdAt="2026-01-01T03:04:05Z",+ ),+ ]+ ),+ )+ live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)+ markdown = _render_report(live)+ row = _row_for(markdown, "codeql.yml")+ assert "| 2026-01-02 |" in row, row+ assert "2019-03-04" not in markdown, (+ "the sentinel generation date reached the report"+ )+ assert "13:37" not in markdown+++class TestCLIGate:+ def test_check_passes_when_no_findings(self, tmp_path):+ """On the healthy real repo, --check exits 0."""+ rc = _run_cli(["--check", "--out", str(tmp_path / "check.md")])+ assert rc == 0++ def test_check_fails_on_unpinned_action(self, monkeypatch, tmp_path):+ bad_wf = {+ "permissions": {},+ "jobs": {+ "a": {+ "steps": [+ {"uses": f"step-security/harden-runner@{SHA}"},+ {+ "uses": f"actions/checkout@{SHA}",+ "with": {"persist-credentials": False},+ },+ {"uses": "owner/action@v4"},+ ]+ }+ },+ }+ monkeypatch.setattr(+ cih,+ "_workflow_files",+ lambda: (_write_wf(tmp_path, "bad.yml", bad_wf),),+ )+ rc = _run_cli(["--check", "--out", str(tmp_path / "out.md")])+ assert rc == 1++ def test_check_fails_on_unsafe_checkout(self, monkeypatch, tmp_path):+ bad_wf = {+ "permissions": {},+ "jobs": {+ "a": {+ "steps": [+ {"uses": f"step-security/harden-runner@{SHA}"},+ {"uses": f"actions/checkout@{SHA}"},+ ]+ }+ },+ }+ monkeypatch.setattr(+ cih,+ "_workflow_files",+ lambda: (_write_wf(tmp_path, "bad.yml", bad_wf),),+ )+ rc = _run_cli(["--check", "--out", str(tmp_path / "out.md")])+ assert rc == 1++ def test_check_fails_on_expression_ref(self, monkeypatch, tmp_path):+ bad_wf = {+ "permissions": {},+ "jobs": {+ "a": {+ "steps": [+ {"uses": f"step-security/harden-runner@{SHA}"},+ {"uses": "owner/action@${{ inputs.ref }}"},+ ]+ }+ },+ }+ monkeypatch.setattr(+ cih,+ "_workflow_files",+ lambda: (_write_wf(tmp_path, "bad.yml", bad_wf),),+ )+ rc = _run_cli(["--check", "--out", str(tmp_path / "out.md")])+ assert rc == 1+++class TestCLIPaths:+ def test_absolute_out_does_not_crash(self, tmp_path):+ out = tmp_path / "abs.md"+ rc = _run_cli(["--out", str(out)])+ assert rc == 0+ assert out.exists()++ def test_github_output_emits_all_fields(self, tmp_path, monkeypatch):+ gh_out = tmp_path / "gh_output"+ monkeypatch.setenv("GITHUB_OUTPUT", str(gh_out))+ rc = _run_cli(["--github-output", "--out", str(tmp_path / "o.md")])+ assert rc == 0+ text = gh_out.read_text()+ for key in (+ "score=",+ "grade=",+ "findings=",+ "findings_critical=",+ "findings_high=",+ ):+ assert key in text, f"missing {key!r} in GITHUB_OUTPUT:\n{text}"+++# --------------------------------------------------------------------------- #+# Helpers — drive analyze_workflows against synthetic temp workflows+# --------------------------------------------------------------------------- #+def _write_wf(tmp_path: pathlib.Path, name: str, obj: object) -> pathlib.Path:+ import yaml as _yaml++ p = tmp_path / name+ p.write_text(_yaml.safe_dump(obj), encoding="utf-8")+ return p+++def _assert_via_tempfile(wf_obj, findings, tmp_checkouts: int, tmp_safe: int):+ """Point the analyzer at a single temp workflow and assert checkout counts."""+ import tempfile++ with tempfile.TemporaryDirectory() as d:+ dpath = pathlib.Path(d)+ import yaml as _yaml++ wf_path = dpath / "wf.yml"+ wf_path.write_text(_yaml.safe_dump(wf_obj), encoding="utf-8")+ # Monkeypatch the module-level file list to our single temp file.+ orig = cih._workflow_files+ setattr(cih, "_workflow_files", lambda: (wf_path,))+ try:+ analysis = cih.analyze_workflows(findings)+ finally:+ setattr(cih, "_workflow_files", orig)+ assert analysis.total_checkouts == tmp_checkouts, (+ f"expected {tmp_checkouts} checkouts, got {analysis.total_checkouts}"+ )+ assert analysis.safe_checkouts == tmp_safe, (+ f"expected {tmp_safe} safe checkouts, got {analysis.safe_checkouts}"+ )+ return analysis+++def _run_precommit(monkeypatch, tmp_path, cfg_obj):+ import yaml as _yaml++ pc_file = tmp_path / ".pre-commit-config.yaml"+ pc_file.write_text(_yaml.safe_dump(cfg_obj), encoding="utf-8")+ # Pass path= explicitly: analyze_precommit binds PRECOMMIT_FILE as a+ # default arg at def-time, so monkeypatching the module global has no+ # effect. The path= param is the intended test seam.+ return cih.analyze_precommit([], path=pc_file)+++# --------------------------------------------------------------------------- #+# Live scanner status (--live)+# --------------------------------------------------------------------------- #+def _fake_run(monkeypatch, handler):+ """Replace subprocess.run inside the analyzer with *handler*.++ *handler* receives the argv list and returns a+ ``(returncode, stdout, stderr)`` triple, or raises to simulate OSError /+ timeout. Calls are recorded on the returned list.+ """+ calls: list[list[str]] = []+ real_run = cih.subprocess.run++ class _Result:+ def __init__(self, returncode: int, stdout: str, stderr: str):+ self.returncode = returncode+ self.stdout = stdout+ self.stderr = stderr++ def _run(command, **kwargs):+ # subprocess.run is shared with recent_cicd_changes()'s `git log`;+ # only gh invocations are faked, everything else runs for real.+ if not (isinstance(command, list) and command and command[0] == "gh"):+ return real_run(command, **kwargs)+ calls.append(list(command))+ return _Result(*handler(list(command)))++ monkeypatch.setattr(cih.subprocess, "run", _run)+ return calls+++def _gh_ok(runs: list[dict]):+ """Handler where `gh --version` succeeds and `gh run list` returns *runs*."""+ import json as _json++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ return (0, _json.dumps(runs), "")++ return handler+++def _gh_with_jobs(runs: list[dict], jobs: list[dict]):+ """Handler where `gh run list` returns *runs* and `gh run view` *jobs*."""+ import json as _json++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ if command[:3] == ["gh", "run", "view"]:+ return (0, _json.dumps({"jobs": jobs}), "")+ return (0, _json.dumps(runs), "")++ return handler+++def _job(name: str, conclusion: str | None) -> dict:+ return {"name": name, "conclusion": conclusion}+++def _iso_days_ago(days: float) -> str:+ """An ISO-8601 run timestamp *days* in the past, as ``gh`` renders one."""+ moment = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(+ days=days+ )+ return moment.strftime("%Y-%m-%dT%H:%M:%SZ")+++def _run_entry(conclusion: str, **kw) -> dict:+ # The default run is RECENT: a fixed calendar date would age into the+ # staleness threshold and silently start raising findings in tests that+ # are about something else entirely.+ return {+ "databaseId": kw.get("databaseId", 123),+ "conclusion": conclusion,+ "event": kw.get("event", "schedule"),+ "headBranch": kw.get("headBranch", "main"),+ "createdAt": kw.get("createdAt", _iso_days_ago(1)),+ }+++class TestLiveStatusDisabled:+ def test_disabled_returns_empty_and_never_shells_out(self, monkeypatch):+ calls = _fake_run(monkeypatch, _gh_ok([_run_entry("failure")]))+ findings: list = []+ assert (+ cih.analyze_live_status(findings, ("codeql.yml",), enabled=False)+ == ()+ )+ assert findings == []+ assert calls == [], "disabled --live must not invoke gh at all"++ def test_no_scanner_workflows_returns_empty(self, monkeypatch):+ calls = _fake_run(monkeypatch, _gh_ok([]))+ assert cih.analyze_live_status([], (), enabled=True) == ()+ assert calls == []+++class TestLiveStatusUnavailable:+ """The dangerous failure mode: looking clean while checking nothing."""++ def test_gh_missing_marks_unavailable_without_findings(self, monkeypatch):+ def handler(command):+ raise OSError("no gh")++ _fake_run(monkeypatch, handler)+ findings: list = []+ live = cih.analyze_live_status(+ findings, ("codeql.yml", "gitleaks.yml"), enabled=True+ )+ assert [entry.workflow for entry in live] == [+ "codeql.yml",+ "gitleaks.yml",+ ]+ assert all(entry.unavailable for entry in live)+ assert all(entry.runs == () for entry in live)+ assert findings == [], "unavailable gh must not manufacture findings"++ def test_gh_version_nonzero_marks_unavailable(self, monkeypatch):+ _fake_run(monkeypatch, lambda command: (127, "", ""))+ live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)+ assert live[0].unavailable == "gh exit 127"++ def test_run_list_nonzero_uses_first_stderr_line(self, monkeypatch):+ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0", "")+ return (1, "", "gh: not authenticated\nrun gh auth login\n")++ _fake_run(monkeypatch, handler)+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].unavailable == "gh: not authenticated"+ assert findings == []++ def test_run_list_nonzero_without_stderr_reports_exit_code(+ self, monkeypatch+ ):+ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0", "")+ return (3, "", " ")++ _fake_run(monkeypatch, handler)+ live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)+ assert live[0].unavailable == "exit 3"++ def test_invalid_json_is_reported_not_raised(self, monkeypatch):+ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0", "")+ return (0, "not json at all", "")++ _fake_run(monkeypatch, handler)+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].unavailable == "gh returned invalid JSON"+ assert findings == []++ def test_non_list_payload_is_reported(self, monkeypatch):+ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0", "")+ return (0, '{"conclusion": "failure"}', "")++ _fake_run(monkeypatch, handler)+ live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)+ assert live[0].unavailable == "gh returned non-list payload"++ def test_timeout_on_run_list_is_contained_per_workflow(self, monkeypatch):+ import subprocess as _sp++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0", "")+ if "--workflow=codeql.yml" in command:+ raise _sp.TimeoutExpired(cmd=command, timeout=15)+ return (0, "[]", "")++ _fake_run(monkeypatch, handler)+ live = cih.analyze_live_status(+ [], ("codeql.yml", "gitleaks.yml"), enabled=True+ )+ assert live[0].unavailable == "gh timed out"+ assert live[1].unavailable is None, (+ "one slow workflow must not abort the remaining queries"+ )+++class TestLiveStatusFindings:+ @pytest.mark.parametrize(+ "conclusion", ["failure", "timed_out", "startup_failure"]+ )+ def test_failing_conclusions_raise_high_finding(+ self, monkeypatch, conclusion+ ):+ _fake_run(+ monkeypatch,+ _gh_ok([_run_entry(conclusion, databaseId=99, event="push")]),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].latest_conclusion == conclusion+ # The lone run also has no success in the window (BL3-1/R1), so a+ # companion "no successful run" medium finding is expected too --+ # only the high one is asserted on below.+ highs = [f for f in findings if f.severity == "high"]+ assert len(highs) == 1+ finding = highs[0]+ assert finding.area == "scanner-runtime"+ assert finding.file == "codeql.yml"+ # Message must carry enough context to act on without re-querying.+ assert conclusion in finding.message+ assert "99" in finding.message+ assert "push" in finding.message++ def test_success_raises_nothing(self, monkeypatch):+ _fake_run(monkeypatch, _gh_ok([_run_entry("success")]))+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].latest_conclusion == "success"+ assert findings == [], "a passing run is not noise"++ @pytest.mark.parametrize("conclusion", ["cancelled", "skipped", "neutral"])+ def test_benign_conclusions_raise_no_high_but_flag_no_success(+ self, monkeypatch, conclusion+ ):+ """Not a --check-gating failure (`_FAILED_CONCLUSIONS` excludes+ these), but also not evidence the scanner works: BL3-1/R1 marks a+ window with no successful run stale unconditionally, so a lone+ cancelled/skipped/neutral run still raises the "no successful run"+ medium finding -- it just never raises a high one."""+ _fake_run(monkeypatch, _gh_ok([_run_entry(conclusion)]))+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].latest_conclusion == conclusion+ assert live[0].stale+ assert [f.severity for f in findings] == ["medium"], (+ f"{conclusion!r} must never raise a high (--check-gating) finding"+ )+ assert "no successful run in the query window" in findings[0].message++ def test_only_the_latest_run_gates(self, monkeypatch):+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry("success", databaseId=3),+ _run_entry("failure", databaseId=2),+ _run_entry("failure", databaseId=1),+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].latest_conclusion == "success"+ assert len(live[0].runs) == 3+ assert findings == [], "an already-fixed scanner must not stay red"++ def test_empty_history_is_a_medium_finding_not_a_failure(self, monkeypatch):+ """A scanner that never ran on main in the window (e.g. its only+ caller is commented out) is absent evidence: a ``medium`` finding,+ never a ``high`` (--check-gating) one."""+ _fake_run(monkeypatch, _gh_ok([]))+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].runs == ()+ assert live[0].latest_conclusion is None+ assert live[0].unavailable is None+ assert [(f.severity, f.file) for f in findings] == [+ ("medium", "codeql.yml")+ ]+ assert "no qualifying run on main" in findings[0].message++ def test_null_conclusion_becomes_unknown_not_a_crash(self, monkeypatch):+ _fake_run(monkeypatch, _gh_ok([_run_entry(None)]))+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].latest_conclusion == "unknown"+ assert findings == []+++class TestLiveQueryShape:+ def test_query_is_scoped_to_main_and_limited(self, monkeypatch):+ calls = _fake_run(monkeypatch, _gh_ok([_run_entry("success")]))+ _ = cih.analyze_live_status([], ("codeql.yml",), enabled=True, limit=5)+ run_list = [call for call in calls if call[:3] == ["gh", "run", "list"]]+ assert len(run_list) == 1+ command = run_list[0]+ assert "--workflow=codeql.yml" in command+ assert f"--branch={cih._LIVE_BRANCH}" in command, (+ "--branch narrows the query to runs whose HEAD REF is named "+ "main; it is a help, not the guard -- a fork PR from a branch "+ "called main passes it, so the event filter does the real work "+ "(see TestLiveEventFilter)"+ )+ assert "--limit=5" in command++ def test_one_query_per_scanner_workflow(self, monkeypatch):+ calls = _fake_run(monkeypatch, _gh_ok([_run_entry("success")]))+ workflows = ("codeql.yml", "gitleaks.yml", "semgrep.yml")+ live = cih.analyze_live_status([], workflows, enabled=True)+ run_list = [call for call in calls if call[:3] == ["gh", "run", "list"]]+ assert len(run_list) == len(workflows)+ assert tuple(entry.workflow for entry in live) == workflows+++class TestScannerWorkflowFiles:+ def test_resolves_real_scanner_filenames(self):+ files = cih._scanner_workflow_files()+ assert files, "no scanner workflows resolved from the real repo tree"+ assert files == tuple(sorted(files))+ assert len(set(files)) == len(files)+ for name in ("codeql.yml", "gitleaks.yml", "semgrep.yml"):+ assert name in files, f"{name} missing from scanner workflow list"+ real = {path.name for path in cih._workflow_files()}+ assert set(files) <= real, "scanner list drifted from the repo tree"+++class TestLiveRendering:+ def _render(self, live):+ markdown, _ = cih.render(+ cih.analyze_workflows([]),+ cih.analyze_precommit(),+ cih.analyze_scanners([]),+ cih.recent_cicd_changes(),+ [],+ live,+ )+ return markdown++ def test_no_live_section_when_live_is_empty(self):+ assert "## Live scanner status" not in self._render(())++ @pytest.mark.parametrize(+ ("conclusion", "label"),+ [+ ("success", "PASS"),+ ("failure", "FAIL"),+ ("timed_out", "TIMED_OUT"),+ ("startup_failure", "STARTUP_FAILURE"),+ ("cancelled", "CANCELLED"),+ ("skipped", "SKIPPED"),+ ("action_required", "ACTION_REQUIRED"),+ ("neutral", "NEUTRAL"),+ # `gh`'s own "stale" conclusion (a run that outlived its log+ # retention) is NOT this report's staleness verdict.+ ("stale", "STALE (gh)"),+ ("unresolved", "UNRESOLVED"),+ ],+ )+ def test_conclusions_render_distinctly(self, conclusion, label):+ run = cih.WorkflowRun(1, conclusion, "schedule", "main", "2025-01-01")+ markdown = self._render(+ (cih.LiveStatus("codeql.yml", (run,), conclusion, None),)+ )+ assert "## Live scanner status" in markdown+ assert f"| {label} " in markdown++ @pytest.mark.parametrize(+ "conclusion", ["failure", "timed_out", "startup_failure"]+ )+ def test_every_failing_conclusion_gets_the_note(self, conclusion):+ run = cih.WorkflowRun(1, conclusion, "schedule", "main", "2025-01-01")+ markdown = self._render(+ (cih.LiveStatus("codeql.yml", (run,), conclusion, None),)+ )+ assert "latest run did not pass" in markdown, (+ f"{conclusion} raises a high finding but the table stayed silent"+ )++ def test_gh_stale_conclusion_does_not_collide_with_the_staleness_label(+ self,+ ):+ """Both `gh`'s `stale` conclusion and this report's staleness verdict+ want the word STALE. They mean different things, so they must not+ render the same cell."""+ run = cih.WorkflowRun(1, "stale", "schedule", "main", "2025-01-01")+ row = _row_for(+ self._render(+ (cih.LiveStatus("codeql.yml", (run,), "stale", None),)+ ),+ "codeql.yml",+ )+ assert "| STALE (gh) |" in row, row+ assert "| STALE |" not in row, (+ "gh's stale conclusion is indistinguishable from the report's "+ "own staleness verdict"+ )+ assert "| unresolved |" not in row++ def test_every_known_gh_conclusion_has_a_label(self):+ unlabelled = sorted(+ conclusion+ for conclusion in cih._KNOWN_CONCLUSIONS+ if conclusion not in cih._STATUS_LABELS+ )+ assert unlabelled == [], (+ f"{unlabelled} would render as a raw API string in the table"+ )++ def test_passing_run_gets_no_note(self):+ run = cih.WorkflowRun(1, "success", "schedule", "main", "2025-01-01")+ markdown = self._render(+ (cih.LiveStatus("codeql.yml", (run,), "success", None),)+ )+ assert "latest run did not pass" not in markdown++ def test_unavailable_entry_is_called_out(self):+ markdown = self._render(+ (cih.LiveStatus("codeql.yml", (), None, "gh exit 127"),)+ )+ assert "_unavailable: gh exit 127_" in markdown+ assert "`gh` may not be installed or authenticated" in markdown++ def test_unavailable_reason_cannot_break_the_table(self):+ markdown = self._render(+ (cih.LiveStatus("codeql.yml", (), None, "a|b\nc"),)+ )+ rows = [+ line+ for line in markdown.splitlines()+ if line.startswith("| `codeql.yml`")+ ]+ assert len(rows) == 1+ # The newline is flattened and the pipe is backslash-escaped, so the+ # cell cannot open a sixth column.+ assert "\\|" in rows[0]+ assert rows[0].replace("\\|", "").count("|") == 6, (+ f"row split by injection: {rows[0]}"+ )++ def test_live_findings_do_not_move_the_score(self):+ baseline_md, baseline_score = cih.render(+ cih.analyze_workflows([]),+ cih.analyze_precommit(),+ cih.analyze_scanners([]),+ cih.recent_cicd_changes(),+ [],+ )+ finding = cih.Finding(+ "high", "scanner-runtime", "codeql.yml", "latest run failure"+ )+ run = cih.WorkflowRun(1, "failure", "schedule", "main", "2025-01-01")+ _, live_score = cih.render(+ cih.analyze_workflows([]),+ cih.analyze_precommit(),+ cih.analyze_scanners([]),+ cih.recent_cicd_changes(),+ [finding],+ (cih.LiveStatus("codeql.yml", (run,), "failure", None),),+ )+ assert live_score == baseline_score, (+ "runtime status must not be folded into the configuration score"+ )+ assert baseline_md != ""+++class TestLiveCLIGate:+ def test_live_failure_fails_the_check_gate(self, monkeypatch, tmp_path):+ _fake_run(monkeypatch, _gh_ok([_run_entry("failure")]))+ rc = _run_cli(["--check", "--live", "--out", str(tmp_path / "o.md")])+ assert rc == 1++ def test_live_success_passes_the_check_gate(self, monkeypatch, tmp_path):+ _fake_run(monkeypatch, _gh_ok([_run_entry("success")]))+ rc = _run_cli(["--check", "--live", "--out", str(tmp_path / "o.md")])+ assert rc == 0++ def test_unavailable_gh_still_passes_but_says_so(+ self, monkeypatch, tmp_path+ ):+ _fake_run(monkeypatch, lambda command: (127, "", ""))+ out = tmp_path / "o.md"+ rc = _run_cli(["--check", "--live", "--out", str(out)])+ assert rc == 0+ assert "unavailable" in out.read_text()++ def test_live_off_by_default(self, monkeypatch, tmp_path):+ calls = _fake_run(monkeypatch, _gh_ok([_run_entry("failure")]))+ out = tmp_path / "o.md"+ rc = _run_cli(["--out", str(out)])+ assert rc == 0+ assert not [c for c in calls if c[:3] == ["gh", "run", "list"]]+ assert "## Live scanner status" not in out.read_text()+++class TestLiveWorkflowWiring:+ """--live once shipped wired to nothing; lock the wiring down."""++ def test_workflow_passes_live_with_token_and_actions_read(self):+ import yaml as _yaml++ path = (+ pathlib.Path(cih.ROOT)+ / ".github"+ / "workflows"+ / "ci-health-report.yml"+ )+ workflow = _yaml.safe_load(path.read_text(encoding="utf-8"))+ job = workflow["jobs"]["report"]+ assert job["permissions"].get("actions") == "read", (+ "without actions: read, gh run list 403s and the live section "+ "silently reports nothing"+ )+ steps = [+ step+ for step in job["steps"]+ if "--live" in str(step.get("run", ""))+ ]+ assert len(steps) == 1, "no step actually runs the analyzer with --live"+ assert "GH_TOKEN" in steps[0].get("env", {}), (+ "without GH_TOKEN the live query degrades to 'unavailable'"+ )+++ "workflow",+ [+ {"name": "Missing jobs", "on": "push"},+ {"jobs": None},+ {"jobs": []},+ {"jobs": {}},+ {"jobs": {"scan": None}},+ {"jobs": {"scan": {"steps": []}}},+ {"jobs": {"scan": {"steps": "invalid"}}},+ {"jobs": {"scan": {"steps": [None]}}},+ ],+)+def test_malformed_workflow_structure_fails_check(+ monkeypatch, tmp_path, workflow+):+ """Invalid jobs must not disappear from the report's security checks."""+ path = _write_wf(tmp_path, "invalid.yml", workflow)+ monkeypatch.setattr(cih, "_workflow_files", lambda: (path,))+ findings = []+ cih.analyze_workflows(findings)+ assert any(f.severity == "high" and f.area == "workflow" for f in findings)+ assert _run_cli(["--check", "--out", str(tmp_path / "report.md")]) == 1++[email protected]("run_id", [None, "invalid", [], {}, True, 1.5, 0, -1])+def test_malformed_run_id_is_unavailable_and_does_not_abort_others(+ monkeypatch, run_id+):+ """Invalid row data must degrade per workflow instead of raising during conversion."""+ import json++ def handler(command):+ if command == ["gh", "--version"]:+ return 0, "synthetic gh", ""+ row = _run_entry("success")+ if "--workflow=codeql.yml" in command:+ row["databaseId"] = run_id+ return 0, json.dumps([row]), ""++ _fake_run(monkeypatch, handler)+ findings = []+ live = cih.analyze_live_status(findings, ("codeql.yml", "gitleaks.yml"))+ assert live[0].unavailable == "gh returned malformed run entries"+ assert live[0].runs == () and live[0].latest_conclusion is None+ assert (+ live[1].latest_conclusion == "success" and live[1].unavailable is None+ )+ assert findings == []+++ "newest",+ [False, {}, _run_entry("success", event=[]), _run_entry("unrecognized")],+)+def test_malformed_newest_run_cannot_promote_older_success(monkeypatch, newest):+ """Dropping a malformed newest entry would falsely label the previous run latest."""+ _fake_run(monkeypatch, _gh_ok([newest, _run_entry("success")]))+ live = cih.analyze_live_status([], ("codeql.yml",))+ assert live[0].unavailable == "gh returned malformed run entries"+ assert live[0].latest_conclusion is None and live[0].runs == ()+++def test_live_queries_share_a_bounded_time_budget(monkeypatch):+ """Serial per-call timeouts must leave room to publish unavailable results."""+ import subprocess+ from types import SimpleNamespace++ clock = [0.0]+ timeouts = []+ monkeypatch.setattr(+ cih, "time", SimpleNamespace(monotonic=lambda: clock[0])+ )+ monkeypatch.setattr(cih, "_gh_available", lambda: None)++ def run(command, **kwargs):+ timeout = kwargs["timeout"]+ timeouts.append(timeout)+ clock[0] += timeout+ raise subprocess.TimeoutExpired(command, timeout)++ monkeypatch.setattr(cih.subprocess, "run", run)+ live = cih.analyze_live_status(+ [], tuple(f"scanner{i}.yml" for i in range(20))+ )+ assert sum(timeouts) == 120.0+ assert timeouts == [15.0] * 8+ assert len(live) == 20+ assert all(entry.unavailable == "gh timed out" for entry in live[:8])+ assert all(+ entry.unavailable == "live query time budget exhausted"+ for entry in live[8:]+ )+++def test_last_live_query_uses_only_remaining_time(monkeypatch):+ """The last request must not overrun the shared budget by a full timeout."""+ import subprocess+ from types import SimpleNamespace++ clock = [0.0]+ timeouts = []+ monkeypatch.setattr(+ cih, "time", SimpleNamespace(monotonic=lambda: clock[0])+ )+ monkeypatch.setattr(cih, "_gh_available", lambda: None)+ monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 20.0)++ def run(command, **kwargs):+ timeout = kwargs["timeout"]+ timeouts.append(timeout)+ clock[0] += timeout+ raise subprocess.TimeoutExpired(command, timeout)++ monkeypatch.setattr(cih.subprocess, "run", run)+ live = cih.analyze_live_status(+ [], ("codeql.yml", "gitleaks.yml", "semgrep.yml")+ )+ assert timeouts == [15.0, 5.0]+ assert live[2].unavailable == "live query time budget exhausted"+++ "step",+ [+ {},+ {"name": "No command"},+ {"uses": None},+ {"uses": 23},+ {"uses": ""},+ {"uses": " "},+ {"run": None},+ {"run": False},+ {"uses": "./local", "run": "echo conflicting"},+ ],+)+def test_invalid_step_commands_fail_check(monkeypatch, tmp_path, step):+ """A valid first step must not hide a later malformed action or shell step."""+ workflow = {+ "permissions": {},+ "jobs": {+ "scan": {+ "steps": [+ {"uses": f"step-security/harden-runner@{SHA}"},+ step,+ ]+ }+ },+ }+ path = _write_wf(tmp_path, "invalid-command.yml", workflow)+ monkeypatch.setattr(cih, "_workflow_files", lambda: (path,))+ findings = []+ cih.analyze_workflows(findings)+ assert any(+ f.severity == "high" and "exactly one valid uses or run" in f.message+ for f in findings+ )+ assert _run_cli(["--check", "--out", str(tmp_path / "report.md")]) == 1+++def test_reusable_job_cannot_also_define_steps(monkeypatch, tmp_path):+ """A reusable call and a runner job are mutually exclusive workflow forms."""+ workflow = {+ "permissions": {},+ "jobs": {+ "scan": {+ "uses": "./.github/workflows/reuse.yml",+ "steps": [{"uses": f"step-security/harden-runner@{SHA}"}],+ }+ },+ }+ path = _write_wf(tmp_path, "invalid-job.yml", workflow)+ monkeypatch.setattr(cih, "_workflow_files", lambda: (path,))+ findings = []+ cih.analyze_workflows(findings)+ assert any(+ f.severity == "high"+ and "cannot combine reusable uses with steps" in f.message+ for f in findings+ )+ assert _run_cli(["--check", "--out", str(tmp_path / "report.md")]) == 1+++# --------------------------------------------------------------------------- #+# Round-1 review fixes+# --------------------------------------------------------------------------- #+def _render_report(live=()):+ markdown, _ = cih.render(+ cih.analyze_workflows([]),+ cih.analyze_precommit(),+ cih.analyze_scanners([]),+ cih.recent_cicd_changes(),+ [],+ live,+ )+ return markdown+++def _row_for(markdown: str, workflow: str) -> str:+ rows = [+ line+ for line in markdown.splitlines()+ if line.startswith(f"| `{workflow}`")+ ]+ assert len(rows) == 1, f"expected one row for {workflow}, got {rows}"+ return rows[0]+++class TestNoTrailingWhitespace:+ """``.pre-commit-config.yaml:9`` runs the bare hook::++ - id: trailing-whitespace++ with no ``--markdown-linebreak-ext`` and no ``exclude``. A markdown hard+ break (two trailing spaces) in the generator's output is therefore+ stripped from the committed file by the hook, so the artefact can never+ match its own generator and the report PR never stops re-opening.+ """++ def test_static_render_has_no_trailing_whitespace(self):+ for line in _render_report().splitlines():+ assert line == line.rstrip(), f"trailing whitespace: {line!r}"++ def test_live_render_has_no_trailing_whitespace(self):+ run = cih.WorkflowRun(+ 1, "success", "schedule", "main", "2026-01-02T03:04:05Z"+ )+ live = (+ cih.LiveStatus("codeql.yml", (run,), "success", None),+ cih.LiveStatus(+ "semgrep.yml", (), None, None, "release-gate.yml", False+ ),+ cih.LiveStatus("grype.yml", (run,), "success", None, None, True),+ cih.LiveStatus("bandit.yml", (), None, "gh exit 127"),+ )+ for line in _render_report(live).splitlines():+ assert line == line.rstrip(), f"trailing whitespace: {line!r}"++ def test_committed_report_has_no_trailing_whitespace(self):+ path = pathlib.Path(cih.ROOT) / ".github" / "CI_HEALTH.md"+ text = path.read_text(encoding="utf-8")+ for number, line in enumerate(text.splitlines(), 1):+ assert line == line.rstrip(), (+ f".github/CI_HEALTH.md:{number} has trailing whitespace; the "+ "trailing-whitespace hook would strip it back out"+ )+++class TestHeaderMatchesContent:+ """The header claimed "not live run status" even while rendering one."""++ def test_static_only_header_disclaims_live_status(self):+ markdown = _render_report()+ assert "not live run status" in markdown+ assert "## Live scanner status" not in markdown++ def test_live_header_announces_the_live_section(self):+ run = cih.WorkflowRun(+ 1, "success", "schedule", "main", "2026-01-02T03:04:05Z"+ )+ markdown = _render_report(+ (cih.LiveStatus("codeql.yml", (run,), "success", None),)+ )+ assert "## Live scanner status" in markdown+ assert "not live run status" not in markdown, (+ "the report denies carrying live status while printing a live table"+ )+ assert "plus a **live scanner status**" in markdown+ assert "docs/ci/workflow-status.md" in markdown+++class TestLiveEventFilter:+ """`--branch=main` matches the run's HEAD REF NAME. A fork's default+ branch is also called `main`, so its pull-request runs pass that filter+ while executing the contributor's tree, not this repository's."""++ def test_fork_pull_request_run_named_main_is_excluded(self, monkeypatch):+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry(+ "failure",+ databaseId=2,+ event="pull_request",+ headBranch="main",+ ),+ _run_entry("success", databaseId=1, event="schedule"),+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert [run.database_id for run in live[0].runs] == [1]+ assert live[0].latest_conclusion == "success"+ assert findings == [], (+ "a fork pull-request failure must not be reported as main being red"+ )++ def test_all_fetched_runs_filtered_out_is_not_idle(self, monkeypatch):+ """Five fork PRs from a branch named `main` can fill the whole query+ page. That is a crowded window, not a scanner that never ran: the+ entry must be unavailable (not `NO RUNS`), raise no idle finding,+ and keep counting as coverage for its category."""+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry(+ "success",+ databaseId=index,+ event="pull_request",+ headBranch="main",+ )+ for index in range(5, 0, -1)+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(+ findings, ("zizmor-security.yml",), enabled=True+ )+ assert live[0].runs == ()+ assert live[0].unavailable is not None+ assert "pull-request" in live[0].unavailable+ assert findings == [], "a crowded window is not an idle scanner"+ category = cih.ScannerCategory(+ "Workflow-Security",+ True,+ True,+ ("zizmor-security.yml",),+ False,+ )+ [adjusted] = cih.discount_idle_scanners((category,), live, findings)+ assert adjusted.satisfied is True+ assert adjusted.workflows == ("zizmor-security.yml",)+ assert findings == []++ def test_pull_request_target_run_is_excluded(self, monkeypatch):+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry(+ "failure", databaseId=2, event="pull_request_target"+ ),+ _run_entry("success", databaseId=1, event="push"),+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert [run.database_id for run in live[0].runs] == [1]+ assert findings == []++ def test_other_branch_run_is_excluded(self, monkeypatch):+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry(+ "failure",+ databaseId=2,+ event="push",+ headBranch="release/1.2",+ )+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].runs == ()+ # The excluded run is not a failure of main, and a page that held+ # only excluded runs is inconclusive, not idle.+ assert findings == []+ assert live[0].unavailable is not None+++# release-gate.yml:64-65 gives `semgrep.yml` the job id `semgrep-scan`, and+# GitHub names every job a called workflow contributes to the caller's run+# "<caller job id> / <called job name>" -- verified on release-gate run+# 35810068506, whose job list holds `semgrep-scan / semgrep-scan`,+# `checkov-scan / checkov` and `owasp-zap-scan / ZAP API Scan` next to the+# roll-up jobs `Check Code Scanning Alerts` and `Release Gate Summary`.+_SEMGREP_JOB = "semgrep-scan / semgrep-scan"+_CHECKOV_JOB = "checkov-scan / checkov"+# The two jobs that failed on run 35810068506 while every scanner passed:+# a standing alert-inventory condition, not a scanner result.+_GATE_ROLLUP_FAILURES = [+ _job("Check Code Scanning Alerts", "failure"),+ _job("Release Gate Summary", "failure"),+]+++class TestGatedScanners:+ """`docs/ci/workflow-status.md` already states the rule: a workflow with+ no default-branch history of its own shows its GATED run, not its own+ direct-run history (which is a stale dispatch log)."""++ def test_workflow_call_only_scanner_is_read_through_its_gate(+ self, monkeypatch+ ):+ calls = _fake_run(+ monkeypatch,+ _gh_with_jobs(+ [_run_entry("success")], [_job(_SEMGREP_JOB, "success")]+ ),+ )+ live = cih.analyze_live_status([], ("semgrep.yml",), enabled=True)+ run_list = [call for call in calls if call[:3] == ["gh", "run", "list"]]+ assert len(run_list) == 1+ assert "--workflow=release-gate.yml" in run_list[0], (+ "semgrep.yml is workflow_call/workflow_dispatch only; its own run "+ "list is the manual-dispatch log, not evidence the scanner runs"+ )+ assert live[0].workflow == "semgrep.yml"+ assert live[0].gated_by == "release-gate.yml"++ def test_direct_failure_is_still_high(self, monkeypatch):+ _fake_run(monkeypatch, _gh_ok([_run_entry("failure")]))+ findings: list = []+ cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ # Also carries the "no successful run in the window" medium finding+ # (BL3-1/R1): the lone run is a failure, so there is no success.+ assert sorted(finding.severity for finding in findings) == [+ "high",+ "medium",+ ]++ def test_self_triggering_scanner_is_queried_directly(self, monkeypatch):+ calls = _fake_run(monkeypatch, _gh_ok([_run_entry("success")]))+ live = cih.analyze_live_status([], ("codeql.yml",), enabled=True)+ run_list = [call for call in calls if call[:3] == ["gh", "run", "list"]]+ assert "--workflow=codeql.yml" in run_list[0]+ assert live[0].gated_by is None++ def test_real_tree_resolves_the_documented_gated_scanners(self):+ callers = cih._reusable_workflow_callers()+ assert cih._gating_parent("semgrep.yml", callers) == "release-gate.yml"+ assert cih._gating_parent("checkov.yml", callers) == "release-gate.yml"+ assert cih._gating_parent("codeql.yml", callers) is None, (+ "codeql.yml has its own push/PR/schedule triggers"+ )+ assert (+ cih._gating_parent("owasp-zap-scan.yml", callers)+ == "release-gate.yml"+ )++ def test_owasp_zap_scan_stays_gated_once_it_gains_a_pull_request_trigger(+ self, monkeypatch+ ):+ """On `main` (unlike this PR's base tree), `owasp-zap-scan.yml` adds+ a path-filtered `pull_request:` trigger next to `workflow_call:`/+ `workflow_dispatch:` -- exactly the shape that previously fell back+ to a dead direct-run log and reported STALE (see+ TestGateResolution). Simulate that shape over the real tree (rather+ than depend on `origin/main` being fetched in CI) by patching+ `_workflow_on_block` for this one file; everything else --+ `_reusable_workflow_callers()`'s real `uses:` scan included -- comes+ from the actual repo tree."""+ real_on_block = cih._workflow_on_block++ def patched(path):+ if path.name == "owasp-zap-scan.yml":+ return {+ "pull_request": None,+ "workflow_call": None,+ "workflow_dispatch": None,+ }+ return real_on_block(path)++ monkeypatch.setattr(cih, "_workflow_on_block", patched)+ callers = cih._reusable_workflow_callers()+ assert (+ cih._gating_parent("owasp-zap-scan.yml", callers)+ == "release-gate.yml"+ )++ def test_gate_is_named_in_the_rendered_row(self):+ run = cih.WorkflowRun(+ 1, "success", "schedule", "main", "2026-01-02T03:04:05Z"+ )+ markdown = _render_report(+ (+ cih.LiveStatus(+ "semgrep.yml", (run,), "success", None, "release-gate.yml"+ ),+ )+ )+ assert "gated by `release-gate.yml`" in _row_for(+ markdown, "semgrep.yml"+ )+++class TestGatedScannersResolvePerJob:+ """A gate's conclusion is a roll-up over every job it runs, so it is not+ this scanner's result in either direction. `release-gate.yml` concluded+ `failure` on 19 of its last 20 `main` runs over a standing+ `Check Code Scanning Alerts` condition while every scanner job in them+ succeeded (run 35810068506). Each gate run is therefore resolved to the+ scanner's own jobs."""++ def test_failed_gate_summary_does_not_fail_the_scanners(self, monkeypatch):+ calls = _fake_run(+ monkeypatch,+ _gh_with_jobs(+ [_run_entry("failure", databaseId=900)],+ [+ _job(_SEMGREP_JOB, "success"),+ _job(_CHECKOV_JOB, "success"),+ *_GATE_ROLLUP_FAILURES,+ ],+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(+ findings, ("checkov.yml", "semgrep.yml"), enabled=True+ )+ assert [entry.latest_conclusion for entry in live] == [+ "success",+ "success",+ ], "the gate's roll-up was printed under the scanners' names"+ assert findings == []+ views = [call for call in calls if call[:3] == ["gh", "run", "view"]]+ assert [call[3] for call in views] == ["900"], (+ "the jobs lookup must be memoised across the scanners sharing "+ f"the gate run, got {views}"+ )+ markdown = _render_report(live)+ for workflow in ("checkov.yml", "semgrep.yml"):+ row = _row_for(markdown, workflow)+ assert "| PASS |" in row, row+ assert "FAIL" not in row, row+ assert "this scanner's own jobs" in row++ def test_failed_scanner_job_is_high_and_renders_fail(self, monkeypatch):+ _fake_run(+ monkeypatch,+ _gh_with_jobs(+ [_run_entry("failure", databaseId=901)],+ [_job(_SEMGREP_JOB, "failure"), *_GATE_ROLLUP_FAILURES],+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ assert live[0].latest_conclusion == "failure"+ # A failed scanner job inside the gate IS a measurement of that+ # scanner (high) -- and, with no success in the window, also the+ # BL3-1/R1 "no successful run" medium finding.+ highs = [f for f in findings if f.severity == "high"]+ assert len(highs) == 1, findings+ assert sorted(f.severity for f in findings) == ["high", "medium"]+ assert "release-gate.yml" in highs[0].message+ assert highs[0].file == "semgrep.yml"+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| FAIL |" in row, row+ assert "latest run did not pass" in row++ def test_unnamed_scanner_is_unresolved_not_failed(self, monkeypatch):+ """No job for this scanner in the gate run: the scanner was not+ measured. Rendering FAIL would invent a failure; rendering PASS would+ invent a success."""+ _fake_run(+ monkeypatch,+ _gh_with_jobs(+ [_run_entry("failure", databaseId=902)],+ _GATE_ROLLUP_FAILURES,+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ assert live[0].latest_conclusion == "unresolved"+ assert [finding.severity for finding in findings] == ["medium"]+ assert "unknown, not passing" in findings[0].message+ markdown = _render_report(live)+ row = _row_for(markdown, "semgrep.yml")+ assert "| UNRESOLVED |" in row, row+ assert "FAIL" not in row and "PASS" not in row, row+ assert "absence of evidence" in markdown+ # This run WAS fetched and its job list genuinely lacked the+ # scanner (no time-budget truncation, no unreadable response+ # involved), so the row must say so -- this is the base case the+ # "did not name this scanner" note exists for.+ assert "did not name this scanner" in row, row++ def test_settled_run_above_an_unresolved_run_raises_no_unresolved_medium(+ self, monkeypatch+ ):+ """R5-4: the UNRESOLVED medium is keyed off the NEWEST run that+ settled to anything, not off "any run in the row is UNRESOLVED". A+ genuinely resolved PASS/FAIL newer than an older UNRESOLVED run+ means the scanner WAS measured most recently; the older run's own+ gap is not news."""+ runs = [+ _run_entry("success", databaseId=920, createdAt=_iso_days_ago(1)),+ _run_entry("failure", databaseId=919, createdAt=_iso_days_ago(2)),+ ]+ jobs_by_run = {+ "920": [_job(_SEMGREP_JOB, "success"), *_GATE_ROLLUP_FAILURES],+ "919": [_job("Other Job", "success"), *_GATE_ROLLUP_FAILURES],+ }++ def handler(command):+ import json as _json++ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ if command[:3] == ["gh", "run", "view"]:+ return (0, _json.dumps({"jobs": jobs_by_run[command[3]]}), "")+ return (0, _json.dumps(runs), "")++ _fake_run(monkeypatch, handler)+ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ assert [run.conclusion for run in live[0].runs] == [+ "success",+ "unresolved",+ ]+ assert findings == [], (+ "a settled PASS newer than an UNRESOLVED run must not raise the "+ f"UNRESOLVED medium: {findings}"+ )+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| PASS |" in row, row+ assert "UNRESOLVED" in row, row++ def test_unreadable_job_list_is_unresolved_not_failed(self, monkeypatch):+ """R5-2: `gh run view` failing (exit != 0, timeout, malformed JSON)+ means this scanner's job list was never READ -- distinct from a job+ list that WAS read and simply didn't name this scanner. Saying "did+ not name this scanner" here would be a guess about a list nothing is+ known about."""++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0", "")+ if command[:3] == ["gh", "run", "view"]:+ return (1, "", "HTTP 403\n")+ import json as _json++ return (0, _json.dumps([_run_entry("failure", databaseId=903)]), "")++ _fake_run(monkeypatch, handler)+ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ assert live[0].latest_conclusion == "unresolved"+ assert [finding.severity for finding in findings] == ["medium"]+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| UNRESOLVED |" in row, row+ assert "the gate run's job list could not be read" in row, row+ assert "did not name this scanner" not in row, row++ def test_recent_runs_cell_shows_the_scanners_own_conclusions(+ self, monkeypatch+ ):+ runs = [+ _run_entry("failure", databaseId=910),+ _run_entry("failure", databaseId=909),+ ]+ jobs_by_run = {+ "910": [_job(_SEMGREP_JOB, "success"), *_GATE_ROLLUP_FAILURES],+ "909": [_job(_SEMGREP_JOB, "failure"), *_GATE_ROLLUP_FAILURES],+ }++ def handler(command):+ import json as _json++ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0", "")+ if command[:3] == ["gh", "run", "view"]:+ return (0, _json.dumps({"jobs": jobs_by_run[command[3]]}), "")+ return (0, _json.dumps(runs), "")++ _fake_run(monkeypatch, handler)+ live = cih.analyze_live_status([], ("semgrep.yml",), enabled=True)+ assert [run.conclusion for run in live[0].runs] == [+ "success",+ "failure",+ ]+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| PASS FAIL |" in row, (+ f"the recent-runs cell still shows the gate's history: {row}"+ )++ def test_worst_of_several_jobs_wins(self):+ jobs = (+ ("owasp-zap-scan / ZAP Baseline Scan", "success"),+ ("owasp-zap-scan / ZAP API Scan", "failure"),+ )+ assert (+ cih._scanner_conclusion_from_jobs(jobs, ("owasp-zap-scan",))+ == "failure"+ )++ def test_nested_reusable_job_path_still_matches(self):+ # A called workflow that itself calls one adds another segment.+ jobs = (("osv-scan / scan / osv-scan", "success"),)+ assert (+ cih._scanner_conclusion_from_jobs(jobs, ("osv-scan",)) == "success"+ )++ def test_job_id_prefix_does_not_match_a_different_job(self):+ jobs = (("grype-scan-extra / thing", "failure"),)+ assert cih._scanner_conclusion_from_jobs(jobs, ("grype-scan",)) is None++ def test_in_flight_gate_job_is_running_not_passing(self):+ jobs = (("semgrep-scan / semgrep-scan", "unknown"),)+ assert (+ cih._scanner_conclusion_from_jobs(jobs, ("semgrep-scan",))+ == "unknown"+ )++ def test_real_tree_maps_the_gate_jobs_to_their_scanners(self):+ mapping = cih._gate_job_ids("release-gate.yml")+ assert mapping.get("semgrep.yml") == ("semgrep-scan",)+ assert mapping.get("gitleaks-main.yml") == ("gitleaks-scan",)+ assert mapping.get("owasp-zap-scan.yml") == ("owasp-zap-scan",)++ def test_gate_job_ids_prefix_is_the_jobs_name_when_set(+ self, monkeypatch, tmp_path+ ):+ """R3: GitHub renders a called workflow's own jobs nested under the+ caller job's DISPLAYED name -- its `name:`, when the job has one,+ not the YAML key `_gate_job_ids` used to return unconditionally."""+ _write_wf(+ tmp_path,+ "gate.yml",+ {+ "on": {"workflow_dispatch": {}},+ "jobs": {+ "gitleaks-scan": {+ "name": "Gitleaks Scan",+ "uses": "./.github/workflows/gitleaks-main.yml",+ }+ },+ },+ )+ monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)+ mapping = cih._gate_job_ids("gate.yml")+ assert mapping.get("gitleaks-main.yml") == ("Gitleaks Scan",)+ assert (+ cih._scanner_conclusion_from_jobs(+ (("Gitleaks Scan / gitleaks", "failure"),), ("Gitleaks Scan",)+ )+ == "failure"+ )++ def test_gate_job_ids_matrix_caller_prefix_matches(+ self, monkeypatch, tmp_path+ ):+ """R3: a matrixed caller job's name gets its matrix value appended+ in parens directly onto the same prefix (no `name:` override), e.g.+ `owasp-zap-scan (staging) / ZAP API Scan`."""+ _write_wf(+ tmp_path,+ "gate.yml",+ {+ "on": {"workflow_dispatch": {}},+ "jobs": {+ "owasp-zap-scan": {+ "strategy": {"matrix": {"target": ["staging", "prod"]}},+ "uses": "./.github/workflows/owasp-zap-scan.yml",+ }+ },+ },+ )+ monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)+ mapping = cih._gate_job_ids("gate.yml")+ assert mapping.get("owasp-zap-scan.yml") == ("owasp-zap-scan",)+ assert (+ cih._scanner_conclusion_from_jobs(+ (("owasp-zap-scan (staging) / ZAP API Scan", "failure"),),+ ("owasp-zap-scan",),+ )+ == "failure"+ )++ def test_budget_truncated_to_zero_resolved_runs_is_unresolved_not_no_runs(+ self, monkeypatch+ ):+ """R2: `_resolve_gated_runs` breaking before resolving even the+ newest of the five FOUND gate runs must not render as `NO RUNS`+ (which claims no qualifying run existed at all -- false) and must+ not drop the failing newest run silently."""+ runs = [+ _run_entry(+ "failure", databaseId=100 + i, createdAt=_iso_days_ago(1 + i)+ )+ for i in range(5)+ ]++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ if command[:3] == ["gh", "run", "view"]:+ raise AssertionError(+ "jobs call must not happen once the budget is spent"+ )+ import json as _json++ return (0, _json.dumps(runs), "")++ _fake_run(monkeypatch, handler)+ monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 100.0)+ # Always inside the reserve window, so the very first run trips it.+ monkeypatch.setattr(cih, "_JOBS_QUERY_RESERVE_SECONDS", 1000.0)++ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ entry = live[0]+ assert entry.latest_conclusion == "unresolved"+ assert any("time budget" in note for note in entry.notes)+ assert any(+ f.severity == "medium"+ and f.message+ == "gate history could not be resolved within the live time budget"+ for f in findings+ ), findings+ # R4-b: the run synthesized only because the budget ran out was+ # never queried, so it must not ALSO raise the ordinary "could not+ # resolve this scanner's jobs in ... run #N" medium naming a run+ # number nothing was ever fetched for -- one medium, not two.+ assert [f.severity for f in findings] == ["medium"], findings+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| UNRESOLVED |" in row, row+ assert "NO RUNS" not in row, (+ "five runs were found; the budget just couldn't resolve them"+ )+ # R4-c/R4-g: this run's job list was never checked (the budget ran+ # out before it could be queried), so the row must not claim it+ # "did not name this scanner" -- that would be a guess.+ assert "did not name this scanner" not in row, row++ def test_budget_truncated_to_some_keeps_the_resolved_runs(+ self, monkeypatch+ ):+ """R2: truncated to >= 1 resolved run must keep those runs' real+ conclusions (not synthesize UNRESOLVED) and still carry the+ truncation note."""+ runs = [+ _run_entry("failure", databaseId=205, createdAt=_iso_days_ago(1)),+ _run_entry("failure", databaseId=204, createdAt=_iso_days_ago(2)),+ _run_entry("failure", databaseId=203, createdAt=_iso_days_ago(3)),+ ]+ jobs_by_run = {+ "205": [_job(_SEMGREP_JOB, "success")],+ "204": [_job(_SEMGREP_JOB, "failure")],+ }++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ if command[:3] == ["gh", "run", "view"]:+ rid = command[3]+ if rid not in jobs_by_run:+ raise AssertionError(+ f"resolved past the truncation point: {rid}"+ )+ import json as _json++ return (0, _json.dumps({"jobs": jobs_by_run[rid]}), "")+ import json as _json++ return (0, _json.dumps(runs), "")++ _fake_run(monkeypatch, handler)+ # `time.monotonic()` is called: once for the deadline, once for the+ # outer per-workflow check, then once per run inside+ # `_resolve_gated_runs` (205, 204, 203) -- land the 203 check inside+ # the reserve window so only 205 and 204 get resolved.+ from types import SimpleNamespace++ seq = iter([0.0, 1.0, 2.0, 3.0, 90.0])+ monkeypatch.setattr(+ cih, "time", SimpleNamespace(monotonic=lambda: next(seq))+ )+ monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 100.0)+ monkeypatch.setattr(cih, "_JOBS_QUERY_RESERVE_SECONDS", 15.0)++ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ entry = live[0]+ assert [run.conclusion for run in entry.runs] == ["success", "failure"]+ assert any("truncated to 2 of 3" in note for note in entry.notes)++ def test_truncated_run_that_was_resolved_keeps_the_unnamed_scanner_note(+ self, monkeypatch+ ):+ """R5-1: the newest gate run IS fetched and resolved (its job list+ genuinely lacked this scanner), and the budget then runs out before+ the OLDER run can be queried. Both facts are about this row and+ neither should hide the other: the truncation note AND the+ "did not name this scanner" note must both render."""+ runs = [+ _run_entry("failure", databaseId=10, createdAt=_iso_days_ago(1)),+ _run_entry("failure", databaseId=9, createdAt=_iso_days_ago(2)),+ ]++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ if command[:3] == ["gh", "run", "view"]:+ assert command[3] == "10", command+ import json as _json++ return (+ 0,+ _json.dumps({"jobs": [_job("Other Job", "success")]}),+ "",+ )+ import json as _json++ return (0, _json.dumps(runs), "")++ _fake_run(monkeypatch, handler)+ from types import SimpleNamespace++ seq = iter([0.0, 1.0, 2.0, 90.0])+ monkeypatch.setattr(+ cih, "time", SimpleNamespace(monotonic=lambda: next(seq))+ )+ monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 100.0)+ monkeypatch.setattr(cih, "_JOBS_QUERY_RESERVE_SECONDS", 15.0)++ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "truncated to 1 of 2" in row, row+ assert "did not name this scanner" in row, row++ def test_gate_with_no_job_for_this_workflow_renders_one_note_not_two(+ self, monkeypatch+ ):+ """R5-5: `_gate_job_ids` found no job in the gate calling this+ workflow at all -- a per-gate fact, not a per-run one. The+ entry-level note already says so; the per-run "did not name this+ scanner" note would repeat the same cause under a different+ sentence."""+ _fake_run(+ monkeypatch,+ _gh_with_jobs(+ [_run_entry("failure", databaseId=904)],+ [_job("Other Job", "success")],+ ),+ )+ monkeypatch.setattr(cih, "_gate_job_ids", lambda gate: {})+ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ assert live[0].latest_conclusion == "unresolved"+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| UNRESOLVED |" in row, row+ assert row.count("has no job calling this workflow") == 1, row+ assert "did not name this scanner" not in row, row++ def test_in_flight_gate_run_with_unstarted_scanner_job_is_running(+ self, monkeypatch+ ):+ """R5: the gate run itself is still in progress and its job list+ (fetched successfully) simply does not have this scanner's job YET+ -- that is "in flight", not "a completed run whose job list stayed+ silent" (UNRESOLVED). The finding must come from the previous+ CONCLUSIVE gate run, not be swallowed."""+ jobs_by_run = {+ "301": [_job("Other Job", "success")], # scanner's job absent+ "300": [_job(_SEMGREP_JOB, "failure")],+ }++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ if command[:3] == ["gh", "run", "view"]:+ import json as _json++ return (+ 0,+ _json.dumps({"jobs": jobs_by_run[command[3]]}),+ "",+ )+ import json as _json++ return (+ 0,+ _json.dumps(+ [+ _run_entry(+ None, databaseId=301, createdAt=_iso_days_ago(0)+ ),+ _run_entry(+ "failure",+ databaseId=300,+ createdAt=_iso_days_ago(1),+ ),+ ]+ ),+ "",+ )++ _fake_run(monkeypatch, handler)+ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ entry = live[0]+ assert entry.runs[0].conclusion == "unknown", (+ "an unstarted scanner job in an in-flight gate run must stay "+ f"'unknown' (RUNNING), not UNRESOLVED: {entry.runs[0]}"+ )+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| RUNNING |" in row, row+ assert "previous run FAIL" in row, row+ # Finding(s) computed from the previous CONCLUSIVE gate run (#300,+ # failure), not swallowed by the in-flight newest run.+ assert findings, "the previous run's failure must not be swallowed"+ assert any(+ f.severity == "high" and "failure" in f.message for f in findings+ )++ def test_in_flight_gate_run_with_no_earlier_resolved_run_still_flags_unresolved(+ self, monkeypatch+ ):+ """R4-d: the newest gate run is in flight (its job list, fetched+ successfully, simply doesn't have this scanner's job YET -- stays+ 'unknown'/RUNNING, not UNRESOLVED) and NO earlier run ever resolved+ to a conclusion either -- their job lists were fetched too and also+ never named this scanner, so they are UNRESOLVED. The UNRESOLVED+ medium must still fire, keyed off the newest run that resolved to+ ANYTHING (not `latest`, which never resolves while in flight); the+ row itself is unchanged (RUNNING label, UNRESOLVED chips beneath+ it)."""+ jobs_by_run = {+ str(300 - i): [_job("Other Job", "success")] for i in range(5)+ }++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ if command[:3] == ["gh", "run", "view"]:+ import json as _json++ return (+ 0,+ _json.dumps({"jobs": jobs_by_run[command[3]]}),+ "",+ )+ import json as _json++ runs = [+ _run_entry(None, databaseId=300, createdAt=_iso_days_ago(0))+ ] + [+ _run_entry(+ "failure", databaseId=300 - i, createdAt=_iso_days_ago(i)+ )+ for i in range(1, 5)+ ]+ return (0, _json.dumps(runs), "")++ _fake_run(monkeypatch, handler)+ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ entry = live[0]+ assert entry.runs[0].conclusion == "unknown", entry.runs+ assert entry.runs[1].conclusion == "unresolved", entry.runs+ assert [f.severity for f in findings] == ["medium"], findings+ assert "run #299" in findings[0].message, findings+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| RUNNING |" in row, row+ assert "UNRESOLVED" in row, row++ def test_in_flight_newest_plus_truncated_older_run_still_flags_unresolved(+ self, monkeypatch+ ):+ """R5-3: the newest gate run is in flight (kept "unknown", RUNNING)+ and the OLDER run -- a FAILURE -- never even gets its job list+ queried because the live time budget runs out first. No run in the+ row ever settles to anything, so neither the "not runs" empty-tuple+ branch nor the newest-settled-is-UNRESOLVED branch fires; without+ this fix that FAILURE is invisible with zero findings raised."""+ runs = [+ _run_entry(None, databaseId=10, createdAt=_iso_days_ago(0)),+ _run_entry("failure", databaseId=9, createdAt=_iso_days_ago(1)),+ ]++ def handler(command):+ if command[:2] == ["gh", "--version"]:+ return (0, "gh version 2.0.0\n", "")+ if command[:3] == ["gh", "run", "view"]:+ assert command[3] == "10", command+ import json as _json++ return (+ 0,+ _json.dumps({"jobs": [_job("Other Job", "success")]}),+ "",+ )+ import json as _json++ return (0, _json.dumps(runs), "")++ _fake_run(monkeypatch, handler)+ from types import SimpleNamespace++ seq = iter([0.0, 1.0, 2.0, 90.0])+ monkeypatch.setattr(+ cih, "time", SimpleNamespace(monotonic=lambda: next(seq))+ )+ monkeypatch.setattr(cih, "_LIVE_BUDGET_SECONDS", 100.0)+ monkeypatch.setattr(cih, "_JOBS_QUERY_RESERVE_SECONDS", 15.0)++ findings: list = []+ live = cih.analyze_live_status(findings, ("semgrep.yml",), enabled=True)+ assert [run.conclusion for run in live[0].runs] == ["unknown"], (+ "the in-flight newest run must stay 'unknown' (RUNNING), not be "+ f"overwritten: {live[0].runs}"+ )+ assert [f.severity for f in findings] == ["medium"], findings+ assert (+ findings[0].message+ == "gate history could not be resolved within the live time budget"+ ), findings+ row = _row_for(_render_report(live), "semgrep.yml")+ assert "| RUNNING |" in row, row+ assert "truncated to 1 of 2" in row, row+++class TestGateResolution:+ """Which gate a scanner is read through, and which workflows qualify."""++ def _gate(self, tmp_path, monkeypatch, scanner_on, gates=("release-gate",)):+ _write_wf(+ tmp_path,+ "scanner.yml",+ {"on": scanner_on, "jobs": {"scan": {"steps": []}}},+ )+ for gate in gates:+ _write_wf(+ tmp_path,+ f"{gate}.yml",+ {+ "on": {"schedule": [{"cron": "0 2 * * *"}]},+ "jobs": {+ "scanner": {"uses": "./.github/workflows/scanner.yml"}+ },+ },+ )+ monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)+ return cih._reusable_workflow_callers()++ def test_pull_request_only_scanner_is_still_read_through_its_gate(+ self, monkeypatch, tmp_path+ ):+ """`owasp-zap-scan.yml` on `main` adds a path-filtered+ `pull_request:` trigger to `workflow_call:`/`workflow_dispatch:`.+ Pull-request runs are discarded by `_is_default_branch_run`, so that+ trigger gives the workflow no default-branch history of its own --+ treating it as one sent the scanner back to a dead dispatch log and+ reported STALE while the gate ran it nightly."""+ callers = self._gate(+ tmp_path,+ monkeypatch,+ {+ "pull_request": {"paths": ["src/**"]},+ "workflow_call": {},+ "workflow_dispatch": {},+ },+ )+ assert callers["scanner.yml"] == ("release-gate.yml",)+ assert cih._gating_parent("scanner.yml", callers) == "release-gate.yml"++ def test_pull_request_target_only_scanner_is_gated_too(+ self, monkeypatch, tmp_path+ ):+ callers = self._gate(+ tmp_path,+ monkeypatch,+ {"pull_request_target": {}, "workflow_call": {}},+ )+ assert cih._gating_parent("scanner.yml", callers) == "release-gate.yml"++ def test_a_scanner_with_its_own_push_history_is_not_gated(+ self, monkeypatch, tmp_path+ ):+ callers = self._gate(+ tmp_path,+ monkeypatch,+ {"push": {"branches": ["main"]}, "workflow_call": {}},+ )+ assert cih._gating_parent("scanner.yml", callers) is None, (+ "a workflow that runs on pushes to main has its own history"+ )++ def test_release_gate_beats_ci_gate(self, monkeypatch, tmp_path):+ """`GATE_ROOTS` is an unordered set; alphabetical order would pick+ `ci-gate.yml`, whose `main` history is two cancelled dispatches."""+ callers = self._gate(+ tmp_path,+ monkeypatch,+ {"workflow_call": {}},+ gates=("ci-gate", "release-gate"),+ )+ assert callers["scanner.yml"] == ("ci-gate.yml", "release-gate.yml"), (+ "the caller list is sorted, so gates[0] would be ci-gate.yml"+ )+ assert cih._gating_parent("scanner.yml", callers) == "release-gate.yml"++ @pytest.mark.parametrize(+ "uses",+ [+ "./.github/workflows/scanner.yml",+ "'./.github/workflows/scanner.yml'",+ '"./.github/workflows/scanner.yml"',+ ],+ )+ def test_quoted_uses_paths_are_recognised(+ self, monkeypatch, tmp_path, uses+ ):+ """A quoted `uses:` value is the same reference; the line scanner+ sees the quotes, the YAML parser does not."""+ (tmp_path / "scanner.yml").write_text(+ "on:\n workflow_call: {}\njobs:\n scan:\n steps: []\n",+ encoding="utf-8",+ )+ (tmp_path / "release-gate.yml").write_text(+ "on:\n schedule:\n - cron: '0 2 * * *'\n"+ f"jobs:\n scanner:\n uses: {uses}\n",+ encoding="utf-8",+ )+ monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)+ callers = cih._reusable_workflow_callers()+ assert callers.get("scanner.yml") == ("release-gate.yml",)+ assert cih._gating_parent("scanner.yml", callers) == "release-gate.yml"+ assert cih._gate_job_ids("release-gate.yml") == {+ "scanner.yml": ("scanner",)+ }+++class TestLiveStaleness:+ """An eight-month-old success is not evidence that a scanner works."""++ def test_old_run_is_stale_and_raises_a_finding(self, monkeypatch):+ _fake_run(+ monkeypatch,+ _gh_ok([_run_entry("success", createdAt="2020-01-01T00:00:00Z")]),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].stale+ stale = [+ finding+ for finding in findings+ if "staleness threshold" in finding.message+ ]+ assert len(stale) == 1, f"no staleness finding in {findings}"+ assert stale[0].file == "codeql.yml"+ assert stale[0].area == "scanner-runtime"+ assert "2020-01-01" in stale[0].message++ def test_recent_run_is_not_stale(self, monkeypatch):+ _fake_run(+ monkeypatch,+ _gh_ok([_run_entry("success", createdAt=_iso_days_ago(2))]),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert not live[0].stale+ assert findings == []++ def test_stale_entry_renders_stale_with_the_date_not_pass(self):+ run = cih.WorkflowRun(+ 7, "success", "schedule", "main", "2020-01-01T00:00:00Z"+ )+ markdown = _render_report(+ (cih.LiveStatus("codeql.yml", (run,), "success", None, None, True),)+ )+ row = _row_for(markdown, "codeql.yml")+ assert "| STALE |" in row, f"stale run still reported as: {row}"+ assert "| 2020-01-01 |" in row++ def test_threshold_reuses_the_repository_rule(self):+ rules = cih._status_rules()+ assert rules is not None, (+ "scripts/generate_workflow_status.py failed to load; the "+ "staleness rule must not be a second, drifting copy"+ )+ crons = cih._cron_strings("codeql.yml")+ assert crons, "codeql.yml lost its cron; pick another scheduled scanner"+ expected = max(+ cih._STALE_FLOOR_DAYS,+ rules.STALE_MULTIPLIER+ * min(rules.cron_cadence_days(cron) for cron in crons),+ )+ assert cih._stale_threshold_days("codeql.yml") == expected++ def test_threshold_falls_back_to_the_floor_without_a_cron(self):+ assert (+ cih._stale_threshold_days("no-such-workflow.yml")+ == cih._STALE_FLOOR_DAYS+ )++ def test_floor_is_the_siblings_floor_to_the_digit(self):+ """`classify_staleness` uses `max(60.0, ...)`+ (scripts/generate_workflow_status.py:618). A different floor here+ would let the two dashboards call the same workflow fresh and stale+ on the same day."""+ assert cih._STALE_FLOOR_DAYS == 60.0++ @pytest.mark.parametrize(+ ("cron", "expected"),+ [+ # daily: 2 x 1 day is under the floor, so the floor wins+ ("0 3 * * *", 60.0),+ # yearly: 2 x 365 days -- well clear of the floor, so this is the+ # multiplier being exercised rather than the floor being restated+ ("0 3 1 1 *", 730.0),+ ],+ )+ def test_threshold_follows_the_cadence_not_only_the_floor(+ self, monkeypatch, tmp_path, cron, expected+ ):+ _write_wf(+ tmp_path,+ "scanner.yml",+ {"on": {"schedule": [{"cron": cron}]}, "jobs": {}},+ )+ monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)+ assert cih._stale_threshold_days("scanner.yml") == expected++ def test_threshold_multiplies_a_cadence_above_the_floor(+ self, monkeypatch, tmp_path+ ):+ """`cron_cadence_days()`'s real buckets are coarse (daily/weekly/+ monthly=30/yearly=365), so a monthly cron (2 x 30 = 60) lands+ exactly on the floor and cannot tell the multiplier apart from the+ floor clamping it -- the previous "monthly" case here did exactly+ that and proved nothing beyond the daily case above it. Patch+ `cron_cadence_days` to a cadence a real cron can't express (45+ days, comfortably clear of the floor) so `2 x 45 = 90` can only+ come from the multiplier actually running."""+ _write_wf(+ tmp_path,+ "scanner.yml",+ {"on": {"schedule": [{"cron": "0 3 1 * *"}]}, "jobs": {}},+ )+ monkeypatch.setattr(cih, "WORKFLOWS_DIR", tmp_path)+ rules = cih._status_rules()+ assert rules is not None+ monkeypatch.setattr(rules, "cron_cadence_days", lambda cron: 45.0)+ assert cih._stale_threshold_days("scanner.yml") == 90.0++ def test_staleness_is_measured_on_the_last_successful_run(+ self, monkeypatch+ ):+ """The sibling measures `last_success_iso`, not the newest run: a+ scanner whose recent runs all failed is not fresh because it failed+ recently."""+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry("failure", databaseId=2),+ _run_entry(+ "success",+ databaseId=1,+ createdAt="2020-01-01T00:00:00Z",+ ),+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].stale, (+ "a recent failure made an eight-month-old success look fresh"+ )+ severities = sorted(finding.severity for finding in findings)+ assert severities == ["high", "medium"], findings+ row = _row_for(_render_report(live), "codeql.yml")+ assert "| FAIL |" in row, (+ f"the staleness label swallowed the failing latest run: {row}"+ )+ assert "last successful run 2020-01-01" in row++ def test_unparseable_created_at_fails_closed_to_stale(self, monkeypatch):+ """A timestamp we cannot read is not evidence of a recent run."""+ _fake_run(+ monkeypatch, _gh_ok([_run_entry("success", createdAt="whenever")])+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].stale+ assert [finding.severity for finding in findings] == ["medium"]+ assert "unparseable" in findings[0].message+ row = _row_for(_render_report(live), "codeql.yml")+ assert "| STALE |" in row, row+ assert "unparseable run timestamp" in row++ def test_staleness_stays_below_the_check_gate(self, monkeypatch, tmp_path):+ """Absent evidence is a medium finding: it is reported, but it does+ not fail --check the way a red scanner does."""+ _fake_run(+ monkeypatch,+ _gh_ok([_run_entry("success", createdAt="2020-01-01T00:00:00Z")]),+ )+ out = tmp_path / "o.md"+ assert _run_cli(["--check", "--live", "--out", str(out)]) == 0+ assert "| STALE |" in out.read_text(encoding="utf-8")+++class TestNoSuccessInWindowIsStaleUnconditionally:+ """BL3-1/R1: `generate_workflow_status.py:618-619`'s sibling rule is "no+ success ⇒ stale unconditionally" -- not "fall back to the newest run of+ any conclusion and call IT the measured success". A scanner whose recent+ runs are all failures (or all cancelled, or any non-success mix) is+ stale regardless of how young its newest failing run is, and the+ finding/row must never claim a failure was a success."""++ def test_five_failures_are_stale_with_the_no_success_message(+ self, monkeypatch+ ):+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry(+ "failure",+ databaseId=500 + i,+ createdAt=_iso_days_ago(400 + i),+ )+ for i in range(5)+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].stale+ assert live[0].latest_conclusion == "failure"+ no_success = [+ f+ for f in findings+ if "no successful run in the query window" in f.message+ ]+ assert len(no_success) == 1, findings+ assert no_success[0].severity == "medium"+ # The message must name the newest run's OWN conclusion, never+ # relabel a failure as a success (BL3-1).+ assert "failure" in no_success[0].message+ assert "newest successful run" not in no_success[0].message+ row = _row_for(_render_report(live), "codeql.yml")+ # The row's label stays the latest conclusion (FAIL), never PASS --+ # there was never a success to lie about.+ assert "| FAIL |" in row, row+ assert "| PASS |" not in row+ assert "| STALE |" not in row+ assert "no successful run in the query window" in row+ assert "(newest run" in row, row++ def test_five_cancelled_are_stale_too(self, monkeypatch):+ """Not a failure -- `cancelled` never raises a high finding -- but+ still no evidence the scanner works, so still stale."""+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry(+ "cancelled",+ databaseId=600 + i,+ createdAt=_iso_days_ago(5 + i),+ )+ for i in range(5)+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].stale+ assert live[0].latest_conclusion == "cancelled"+ assert [f.severity for f in findings] == ["medium"], findings+ assert "no successful run in the query window" in findings[0].message+ row = _row_for(_render_report(live), "codeql.yml")+ assert "| CANCELLED |" in row, row+ assert "| PASS |" not in row+++class TestInFlightAndEmptyHistory:+ def test_in_flight_run_does_not_mask_a_failed_previous_run(+ self, monkeypatch+ ):+ _fake_run(+ monkeypatch,+ _gh_ok(+ [+ _run_entry(None, databaseId=2),+ _run_entry("failure", databaseId=1),+ ]+ ),+ )+ findings: list = []+ live = cih.analyze_live_status(findings, ("codeql.yml",), enabled=True)+ assert live[0].latest_conclusion == "unknown"+ # Also carries the BL3-1/R1 "no successful run" medium finding: the+ # in-flight run is inconclusive, and the previous CONCLUSIVE run+ # (#1) was a failure, so the window has no success either.+ highs = [f for f in findings if f.severity == "high"]+ assert len(highs) == 1, (+ f"a run racing the report suppressed the failure finding: {findings}"+ )+ assert sorted(f.severity for f in findings) == ["high", "medium"]+ assert "run #1" in highs[0].message++ def test_in_flight_renders_running_with_the_previous_conclusion(self):+ runs = (+ cih.WorkflowRun(+ 2, "unknown", "schedule", "main", "2026-02-02T00:00:00Z"+ ),+ cih.WorkflowRun(+ 1, "failure", "schedule", "main", "2026-02-01T00:00:00Z"+ ),+ )+ row = _row_for(+ _render_report(+ (cih.LiveStatus("codeql.yml", runs, "unknown", None),)+ ),+ "codeql.yml",+ )+ assert "| RUNNING |" in row+ assert "previous run FAIL" in row+ assert "latest run did not pass" in row++ def test_empty_history_renders_no_runs_with_a_footnote(self):+ markdown = _render_report(+ (cih.LiveStatus("codeql.yml", (), None, None),)+ )+ row = _row_for(markdown, "codeql.yml")+ assert "| NO RUNS |" in row, f"empty history rendered as: {row}"+ assert "absence of evidence" in markdown+++class TestWorkflowClaimsMatchWiring:+ """The workflow's comment described a gate reading the findings outputs.+ No step ran ``--check``, and nothing read ``findings_*``."""++ def _workflow_text(self) -> str:+ path = (+ pathlib.Path(cih.ROOT)+ / ".github"+ / "workflows"+ / "ci-health-report.yml"+ )+ return path.read_text(encoding="utf-8")++ def test_no_step_gates_on_the_report_and_none_is_claimed(self):+ """Structural, so an explanatory comment cannot satisfy it: the old+ form asserted `"--check" in text`, which the comment saying there is+ no --check step made true."""+ import yaml as _yaml++ text = self._workflow_text()+ workflow = _yaml.safe_load(text)+ commands = [+ step["run"]+ for job in workflow["jobs"].values()+ for step in job["steps"]+ if isinstance(step, dict) and isinstance(step.get("run"), str)+ ]+ assert commands, "no run: steps parsed out of the workflow"+ assert any(+ "generate_ci_health.py" in command for command in commands+ ), "no step runs the analyzer at all"+ for command in commands:+ assert "--check" not in command, (+ f"a step gates on the report: {command!r}; either keep the "+ "gating step and say so, or do not claim one"+ )+ assert "the gate" not in text.casefold(), (+ "the workflow's prose names a gate that no step performs"+ )++ def test_every_emitted_output_is_consumed(self, tmp_path, monkeypatch):+ gh_out = tmp_path / "gh_output"+ monkeypatch.setenv("GITHUB_OUTPUT", str(gh_out))+ assert (+ _run_cli(["--github-output", "--out", str(tmp_path / "o.md")]) == 0+ )+ emitted = [+ line.split("=", 1)[0]+ for line in gh_out.read_text(encoding="utf-8").splitlines()+ if "=" in line+ ]+ text = self._workflow_text()+ for key in emitted:+ assert f"steps.gen.outputs.{key}" in text, (+ f"{key} is written to $GITHUB_OUTPUT but no step reads it"+ )