Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .clinerules
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
# Go -> Rust
# Python -> Julia or Rust

# CONTAINERS
# CONTAINERS (only if this repo builds a container; otherwise skip)
# Runtime: Podman first.
# File: Containerfile (never Dockerfile).
# Base: cgr.dev/chainguard/wolfi-base:latest or cgr.dev/chainguard/static:latest.
# Base: cgr.dev/chainguard/wolfi-base or cgr.dev/chainguard/static, pinned by digest (@sha256:<digest>); a tag such as :latest is not a pin.

# ABI/FFI
# ABI: Idris2 with dependent types (src/interface/abi/).
# ABI/FFI (only if this repo has a src/interface/ seam; otherwise skip)
# ABI: Idris2 with dependent types (src/interface/Abi/).
# FFI: Zig with C ABI (src/interface/ffi/).
# Headers: src/interface/generated/.

Expand Down
8 changes: 6 additions & 2 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ migration destination is AffineScript.

## Containers

Only if this repo builds a container; otherwise skip.

- Use Podman, never Docker.
- Name the file `Containerfile`, never `Dockerfile`.
- Base image: `cgr.dev/chainguard/wolfi-base:latest`.
- Base image: `cgr.dev/chainguard/wolfi-base`, pinned by digest (`@sha256:<digest>`; `scripts/pin-container-digests.sh`). A tag alone, `:latest` included, is not a pin: `scripts/check-container-pins.sh` fails a `FROM` that has a tag and no digest (a tag followed by a digest passes).

## ABI/FFI

- ABI definitions in Idris2 (`src/interface/abi/`).
Only if this repo has a `src/interface/` seam; otherwise skip.

- ABI definitions in Idris2 (`src/interface/Abi/`).
- FFI implementations in Zig (`src/interface/ffi/`).
- Generated C headers in `src/interface/generated/`.

Expand Down
49 changes: 41 additions & 8 deletions .github/hooks/scan-secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,19 @@ DETECTOR_ARGS=(--exclude-detectors SonarCloud)
# `command -v` is not enough: a mise shim with no pinned global version sits on
# PATH, answers `command -v` happily, and fails every actual invocation with
# "No version is set for shim". Installed is not invokable. Each candidate is
# therefore probed with `--version` and only accepted on exit 0.
probe() { [ -n "${1:-}" ] && [ -x "$1" ] && "$1" --version >/dev/null 2>&1; }
# therefore probed (see probe() below) and only accepted when it answers.
# A binary is also rejected when it lacks --fail-on-scan-errors (added after
# v3.90): the hook passes it on every scan, so an older binary would be
# "installed" yet fail every push on an unknown flag. Output is captured, not
# piped into grep -q, because under pipefail grep's early exit can SIGPIPE the
# producer and turn a match into a failure.
probe() {
local bin="${1:-}" help_text
[[ -n "$bin" && -x "$bin" ]] || return 1
"$bin" --version >/dev/null 2>&1 || return 1
help_text="$("$bin" --help 2>&1 || true)"
[[ "$help_text" == *"--fail-on-scan-errors"* ]]
}

TH=""
if probe "${TRUFFLEHOG:-}"; then
Expand All @@ -99,7 +110,7 @@ fi

if [ -z "$TH" ]; then
cat >&2 <<'HINT'
[scan-secrets] BLOCKED: TruffleHog is not installed, or is a dead shim.
[scan-secrets] BLOCKED: TruffleHog is not installed, is a dead shim, or is too old (it must support --fail-on-scan-errors; use 3.96.0 or newer).

This gate fails closed on purpose — a secret scanner that skips silently is
indistinguishable from one that found nothing.
Expand Down Expand Up @@ -128,7 +139,11 @@ fi
# they must be REPORTED apart. Measured here: a stale remote ref pointing at
# 0000…0 made every scan exit 1 with "upload-pack: not our ref", and a handler
# that said "Secrets detected" for any non-zero told the developer to go hunting
# for a credential that did not exist. A gate that misnames its own failure
# for a credential that did not exist. NOTE the converse: TruffleHog checks for
# scan errors BEFORE it applies --fail, so a run that finds a secret AND hits a
# scan error exits non-zero but not 183. rc != 183 therefore never proves "no
# secret", and the error path below says the output may ALSO contain a finding.
# A gate that misnames its own failure
# sends people looking in the wrong place, which is how gates get switched off.
status=0
scanned=0
Expand All @@ -147,7 +162,7 @@ scan() {
local rc=0
# NOT piped — see trap 2 in the header.
"$TH" git "file://$REPO_ROOT" "$@" "${VERIFY_ARGS[@]}" "${DETECTOR_ARGS[@]}" \
--fail --no-update >"$out" 2>&1 || rc=$?
--fail --fail-on-scan-errors --no-update >"$out" 2>&1 || rc=$?
scanned=$((scanned + 1))
if [ "$rc" -eq 183 ]; then
echo "[scan-secrets] FINDINGS while scanning $label:" >&2
Expand All @@ -156,7 +171,7 @@ scan() {
status=1
elif [ "$rc" -ne 0 ]; then
echo "[scan-secrets] SCANNER ERROR (exit $rc) while scanning $label —" >&2
echo "[scan-secrets] this is a failure to scan, NOT a detected secret:" >&2
echo "[scan-secrets] this is a failure to scan; the output below may ALSO contain a detected secret:" >&2
cat "$out" >&2
errored=1
status=1
Expand All @@ -168,7 +183,25 @@ if [ -n "${K9_PUSH_RANGES:-}" ]; then
[ -z "${local_sha:-}" ] && continue
[ "$local_sha" = "$ZERO" ] && continue # branch deletion
if [ "${remote_sha:-$ZERO}" = "$ZERO" ]; then
scan "new branch ${local_ref:-HEAD} (last $MAX_DEPTH commits)" \
# TruffleHog's --max-depth bounds the WHOLE history it walks from
# $local_sha, not just the commits no remote has yet, and at the limit it
# can stop partway through the oldest commit it reaches. So a count that
# excludes remote history (--not --remotes) undercounts what is walked,
# and a count equal to the limit is already too small. Count the same
# traversal and require it to be strictly below the limit; otherwise
# block rather than approve a partial scan. Set K9_SECRETS_MAX_DEPTH
# above the count to scan the whole history.
new_commits="$(git -C "$REPO_ROOT" rev-list --count "$local_sha" 2>/dev/null || echo "")"
if [[ -z "$new_commits" || "$new_commits" -ge "$MAX_DEPTH" ]]; then
echo "[scan-secrets] SCANNER ERROR while scanning new branch ${local_ref:-HEAD} —" >&2
echo "[scan-secrets] its history is ${new_commits:-of unknown length} commits, not below K9_SECRETS_MAX_DEPTH=$MAX_DEPTH;" >&2
echo "[scan-secrets] the depth limit would leave part of it unscanned. Set K9_SECRETS_MAX_DEPTH above that count." >&2
scanned=$((scanned + 1))
errored=1
status=1
continue
fi
scan "new branch ${local_ref:-HEAD} (full history, $new_commits commits)" \
--branch "$local_sha" --max-depth "$MAX_DEPTH"
else
scan "${local_ref:-HEAD} since ${remote_sha:0:12}" \
Expand All @@ -181,7 +214,7 @@ fi
# HEAD rather than reporting a pass over nothing. A gate that reports success
# having examined zero commits is the vacuity this estate keeps re-learning.
if [ "$scanned" -eq 0 ]; then
scan "HEAD (no push ranges; last $MAX_DEPTH commits)" --max-depth "$MAX_DEPTH"
scan "HEAD (no push ranges; last $MAX_DEPTH commits)" --branch HEAD --max-depth "$MAX_DEPTH"
fi

if [ "$found" -ne 0 ]; then
Expand Down
4 changes: 2 additions & 2 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
- [ ] Repo deed `(meta)` and/or `docs/decisions/` updated (if architectural decisions changed)
- [ ] `docs/status/ROADMAP.adoc` updated (if milestones or next actions changed)
- [ ] Documentation updated for user-facing changes
- [ ] `TOPOLOGY.md` updated (if architecture changed)
- [ ] `docs/architecture/TOPOLOGY.adoc` updated (if architecture changed)
- [ ] `CHANGELOG` or release notes updated
- [ ] New dependencies reviewed for license compatibility (MPL-2.0 / MPL-2.0)
- [ ] New dependencies reviewed for license compatibility (compatible with MPL-2.0)
- [ ] ABI/FFI changes validated (`src/interface/abi/` and `src/interface/ffi/` consistent)

## Testing
Expand Down
137 changes: 54 additions & 83 deletions .github/workflows/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ what the canon says (standards `docs/CONTRACTILE-DESCRIPTILE-VALIDATION.adoc`).
`k9-grid` artifact; it asks for `pull-requests: read` only. `k9-verdict`
downloads that artifact and posts `report.md` to the pull request under
`<!-- verdict-grid:k9 regenerated on every run -->`, one comment per PR,
updated in place. `badges` publishes `badges/k9.json` to the
`governance-badges` branch on eligible pushes to `main`; the README badge reads it
updated in place. `badges` stages `badges/k9.json` on eligible pushes to
`main` and `pages.yml` deploys it to GitHub Pages; the README badge reads it
through the shields endpoint; the section "The README badges are
generated output" below states the contract that branch operates under.
generated output" below states the contract.
* `hypatia-scan.yml` -> `verdict` job posts the Hypatia grid the same way
(artifact `verdict-grid`, marker `<!-- verdict-grid:hypatia … -->`).

Expand All @@ -108,50 +108,68 @@ canonical tools, run from a standards checkout (`STANDARDS_DIR`), and skip
with a notice when none is present. k9iser is retired estate-wide: contracts
are authored to `1-formats/k9/spec/K9-CONTRACT-SPEC.adoc`, never generated.

== The README badges are generated output (`governance-badges`)
== The README badges are generated output (GitHub Pages)

Every repository minted from this template publishes one Shields-endpoint JSON
file per validation lane, on a *separate orphan branch* named
`governance-badges`:
file per validation lane on its *GitHub Pages site*:

[cols="1,1,3",options="header"]
|===
| Lane | File | Written by

| K9 conformance
| `badges/k9.json`
| `dogfood-gate.yml` -> `badges` job, on eligible pushes to `main`
| `dogfood-gate.yml` -> `badges` job stages it; `pages.yml` deploys it, on pushes to `main`

|===

`README.adoc` reads that file through the shields endpoint at a URL that must
not change:

----
https://raw.githubusercontent.com/<owner>/<repo>/governance-badges/badges/k9.json
https://<owner>.github.io/<repo>/badges/k9.json
----

Both halves - rendering the badge and deciding whether to publish it - live in
`scripts/governance-badge.js`. The workflow only supplies the lane's result and
the lane's artifact. `tests/workflows/governance_badge_test.sh` exercises the
helper against a mock of the GitHub API and checks this directory's wiring.

=== It is generated output, not source

* `governance-badges` is an *orphan* branch. Its commits are created with
`parents: []`, so there is *no common ancestor with `main`* - not "a little
history", none. It is not a variant, a release branch or a staging area.
* *It must never be merged*, and there is nothing to merge: a pull request from
it would show unrelated histories. Do not open one, do not merge one, do not
rebase it onto `main`. Merging it would import a badges-only tree over the
repository's real content.
* *Recent pushes are normal.* Eligible pushes to `main` republish the badge. A run
whose badge is unchanged makes *no commit at all*, so the branch is quiet
when the lane result is stable and moves only when the state changes.
* It is safe to delete. The next eligible push to `main` recreates it, with no parent,
from scratch.
* It carries no workflows, no code and nothing that runs. Treat it like a
generated site deployment, not like a branch of the repository.
Both halves - rendering the badge and deciding what to stage - live in
`scripts/governance-badge.js` (`render` and `stage`). The workflow only supplies
the lane's result and the lane's artifact. `tests/workflows/governance_badge_test.sh`
exercises the helper and checks this directory's wiring.

=== Why Pages and not a branch

The badge used to be written straight to a generated orphan branch
(`governance-badges`) through the contents API. The estate's `Branch-Floor`
repository ruleset applies to *all* branches and its `code_coverage` rule
requires a pull request, so that write was refused (HTTP 409) on every push to
`main`. A Pages deployment is not a branch write, so it satisfies the ruleset
without any exemption. Nothing here weakens, bypasses or excuses the ruleset.

=== How it flows

* `badges` (in `dogfood-gate.yml`, `contents: read` only) runs `governance-badge.js stage`
and uploads the result as the `governance-badge-site` artifact, even when the
lane's evidence was unusable (the honest grey badge is staged, then the step
fails the run).
* `pages.yml` runs on `workflow_run` of "Dogfood Gate" for pushes to `main`
that were not cancelled, downloads that artifact, and adds `badges/k9.json` to
the site before deploying. A missing artifact fails the deploy loudly rather
than silently deleting the live badge.
* A freshness gate in `pages.yml` skips (does not fail) any run whose commit is no
longer the default branch's head, so re-running an old Dogfood Gate run, or a
late finisher in a burst of merges, cannot overwrite the current badge. The check runs twice: at the start (`gate` job) and again in the `deploy` job right before `actions/deploy-pages`, because the build can take up to 20 minutes and in-flight runs are not cancelled. A superseded run is skipped at either point.
* There is one Pages site per repository and a deployment replaces it wholesale,
which is why the badge travels *with* the site deployment. The consequence:
`pages.yml` has no `push` or `workflow_dispatch` trigger any more; to
redeploy, re-run Dogfood Gate on `main`.
* Each deployment writes the whole badge from scratch, so the old "compare bytes
with the branch, make no commit" step has no meaning here and is not used.
* It carries no workflows, no code and nothing that runs.
* *Repository setting (owner):* Pages must be enabled with source
"GitHub Actions". Pages host names are lower-case; a mixed-case owner name
needs the lower-case form in the README URL. A repository that is itself
named `<owner>.github.io` is a user/organisation Pages site served at the root,
so its badge URL has no `/<repo>` segment; the template's URL does not handle
that case and it must be edited by hand.

=== What each colour means

Expand Down Expand Up @@ -197,67 +215,20 @@ helper against a mock of the GitHub API and checks this directory's wiring.
*Grey means "this run did not produce a trustworthy success/failure result".*
It does not mean "pending", "cached" or "about to go green", and it is never a
stale green left behind by an earlier run: a workflow that runs with a skipped,
cancelled or unavailable lane *publishes grey*.
cancelled or unavailable lane *stages grey*.

The badge reflects the *lane job's* result (`needs.k9-validate.result`), with
the lane's artifact as a *witness*: a `success` result is only believed when the
`k9-grid` artifact is present, well formed, byte-identical to the canonical
render, and itself records `success`. An artifact can never turn a failed lane
green; it can only stop a green one being published.
green; it can only stop a green one being staged.

[NOTE]
====
*Known limit.* If the whole workflow run is cancelled, GitHub does not start
the publisher at all, so the badge keeps its previous value until the next eligible push
to `main`. No job can publish a state for a run it was not allowed to execute.
Because the run that cancels a superseded one is itself publishing, the badge
converges on the newest run's result rather than staying stuck.
*Known limit.* If the whole Dogfood Gate run is cancelled, no badge is staged
and `pages.yml` skips the deploy, so the live badge keeps its previous value
until the next eligible push to `main`. Deployments are serialised by the
`pages` concurrency group and are not cancelled mid-flight; the badge converges
on the newest completed run.
====

=== Identical content produces no commit

Before writing anything, the publisher fetches the file that is currently on the
branch and compares it *byte for byte* with the badge it has just rendered.

* Identical -> it prints `unchanged`, writes no commit, and exits 0.
* Different -> it publishes with the blob sha it just read, so a concurrent
publication is detected (HTTP 409) and re-evaluated instead of silently
overwriting the other run's commit. Publications are also serialised by a
per-repository `concurrency` group.
* The JSON is never re-serialised or canonicalised to make a comparison
succeed. Key order and spacing are fixed by the renderer, and a file that
differs only in layout *is* a difference: it gets republished.

=== Missing is not the same as broken

The publisher distinguishes the two, because treating every API error as
"missing" is how a badge pipeline quietly creates branches it should not:

[cols="1,2",options="header"]
|===
| HTTP | What happens

| `404` on the file, `404` on the ref
| The branch really is absent: create it as an orphan (`parents: []`).

| `404` on the file, `200` on the ref
| The branch exists but the file does not: add the file, no new branch.

| `404` on the file, anything else on the ref
| Unknown. Fail the run; do not create a branch.

| `401` / `403`
| Authentication or permission failure. Fail; do not write.

| `429` / `5xx`, or a network error
| Transient. Retried a bounded number of times, then fail; nothing is treated
as missing.

| Any other status
| Fail; do not write.
|===

A malformed file that is already on the branch (not JSON, not an object, no
`schemaVersion`, no message) is *refused*, not overwritten: it did not come from
this tool, and silently replacing it would hide a hand edit. Repair or delete it
and re-run.
Loading
Loading