Skip to content
13 changes: 9 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ jobs:
if: >-
github.event_name == 'push' &&
github.ref == 'refs/heads/codex/thesis-ledger-facts'
# Runs in the repo's hash-locked env: the verifier may import
# packaged dependencies (receipt) once the consumption shims land.
# Runs in the repo's hash-locked env: the verifier imports the
# packaged receipt. The env does not install this project, so the
# checkout's root is not on sys.path, and `python -I` keeps scripts/
# off it too: no other module in either can stand in for one the
# verifier imports. It loads scripts/receipt_pins.py by path.
# `uv run --no-sync` keeps uv from installing the project again;
# the steps below install it for the tests.
run: |
uv sync --locked --no-dev
uv run --locked --no-dev python scripts/verify_release_chain.py --full
uv sync --locked --no-dev --no-install-project
uv run --no-sync python -I scripts/verify_release_chain.py --full

- name: Set up Python
uses: actions/setup-python@v5
Expand Down
139 changes: 95 additions & 44 deletions .github/workflows/thesis-facts-append.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,62 @@ jobs:

git clone --no-checkout "$repository_url" "$base_gate"
git -C "$base_gate" fetch --no-tags origin "$BASE_SHA"
git -C "$base_gate" checkout --detach "$BASE_SHA"
# Check the base gate out with an empty attribute tree, so the files
# it runs from, its trust anchors included, are the committed bytes.
# (The runner's own git config still applies; GitHub's ubuntu
# runners set no core.autocrlf or core.attributesFile.)
# A .gitattributes anywhere in the base, on the gate surface or off
# it, then can neither rewrite them (working-tree-encoding, ident, a
# filter, eol=crlf) nor stop the checkout (a boolean
# working-tree-encoding, a required filter whose smudge fails). A
# stopped checkout would fail this job on every later pull request,
# the one that removes the file included. Two pull requests that
# each pass alone can merge into such a base. The candidate above
# keeps its attributes: if they stop its checkout, that refuses the
# candidate.
empty_tree="$(git -C "$base_gate" hash-object -t tree /dev/null)"
git -C "$base_gate" --attr-source="$empty_tree" \
checkout --detach "$BASE_SHA"
test "$(git -C "$base_gate" rev-parse HEAD)" = "$BASE_SHA"

# Provision the BASE commit's own hash-locked environment and run
# the base gate with it: the gate scripts may import packaged
# dependencies, and installing from the base's uv.lock
# keeps the PR unable to influence what code judges it.
uv sync --locked --no-dev --project "$base_gate"
#
# Every start of the base gate, the --help question included, uses
# `python -I` and sets no PYTHONPATH, and the environment does not
# install this project. -I keeps the script's own directory off
# sys.path and ignores PYTHON* variables; without the project, no
# editable .pth puts the base checkout's root on sys.path, and the
# sync builds nothing from the checkout (no README.md, hatch.toml or
# hatch_build.py). Most of scripts/ and the root are off the gate
# surface, so an earlier data proposal can add a module there (a
# scripts/tempfile.py, a scripts/receipt/ package, a root
# sitecustomize/ package); started this way, the gate never imports
# it. `uv run --no-sync` keeps uv from installing the project again.
#
# A base gate that still imports its pins by name finds them only
# as scripts.receipt_pins, through that root, so for such a base
# alone the project is installed as before. -I still keeps scripts/
# off sys.path, but the root is on it, last, so a module there that
# the old gate looks up and finds nowhere earlier (a root
# receipt_pins.py, a scripts/__init__.py or a scripts/receipt_pins/
# package) still reaches it. The check reads the base checkout's
# copy of the gate, which the candidate cannot change. Delete the
# first branch below once no pull request into this branch can
# still have a base from before the gate loaded its pins by path. A
# gate older than the pins imports its sibling scripts by name,
# cannot start this way, and stops the step.
base_gate_script="$base_gate/scripts/check_thesis_facts_append.py"
if grep -qE '^[[:space:]]*from scripts\.receipt_pins import ' \
"$base_gate_script"; then
uv sync --locked --no-dev --project "$base_gate"
base_gate_python=(uv run --locked --no-dev --project "$base_gate" python -I)
else
uv sync --locked --no-dev --no-install-project --project "$base_gate"
base_gate_python=(uv run --no-sync --project "$base_gate" python -I)
fi

# Receipt 0.6 reads and rehashes the named commit's objects; the shim
# prints the commit and tree from that verdict. --root only locates
Expand All @@ -118,33 +166,24 @@ jobs:
# cannot run at all stops the step instead of being read as an old
# one. Delete the second branch below once no base reachable from
# this branch predates --commit.
base_gate_help="$(
uv run --locked --no-dev --project "$base_gate" \
python "$base_gate/scripts/check_thesis_facts_append.py" --help
)"
base_gate_help="$("${base_gate_python[@]}" "$base_gate_script" --help)"

cd "$RUNNER_TEMP"
case "$base_gate_help" in
*--commit*)
PYTHONPATH="$base_gate/scripts" \
PYTHONNOUSERSITE=1 \
uv run --locked --no-dev --project "$base_gate" \
python "$base_gate/scripts/check_thesis_facts_append.py" \
--root "$candidate" \
--commit "$MERGE_SHA" \
--base-ref "$BASE_SHA"
"${base_gate_python[@]}" "$base_gate_script" \
--root "$candidate" \
--commit "$MERGE_SHA" \
--base-ref "$BASE_SHA"
;;
*)
echo "note: the base gate at $BASE_SHA predates --commit, so it" \
"judges the working tree at $candidate, without binding its" \
"verdict to $MERGE_SHA. This is the base's own guarantee and" \
"the only one available against that base." >&2
PYTHONPATH="$base_gate/scripts" \
PYTHONNOUSERSITE=1 \
uv run --locked --no-dev --project "$base_gate" \
python "$base_gate/scripts/check_thesis_facts_append.py" \
--root "$candidate" \
--base-ref "$BASE_SHA"
"${base_gate_python[@]}" "$base_gate_script" \
--root "$candidate" \
--base-ref "$BASE_SHA"
;;
esac

Expand Down Expand Up @@ -174,54 +213,66 @@ jobs:
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git" \
"$base_gate"
git -C "$base_gate" fetch --no-tags --depth 1 origin "$base_sha"
git -C "$base_gate" checkout --detach "$base_sha"
# As in the trusted job: an empty attribute tree, so the base
# gate runs from the committed bytes and no .gitattributes can
# stop its checkout.
empty_tree="$(git -C "$base_gate" hash-object -t tree /dev/null)"
git -C "$base_gate" --attr-source="$empty_tree" \
checkout --detach "$base_sha"
test "$(git -C "$base_gate" rev-parse HEAD)" = "$base_sha"

# Base gate runs from the BASE commit's own hash-locked env so
# gate dependencies resolve without the PR influencing them.
uv sync --locked --no-dev --project "$base_gate"
# gate dependencies resolve without the PR influencing them. As
# in the trusted job above, the env does not install this
# project and every start uses `python -I` with no PYTHONPATH,
# so no module in the base's scripts/ or at its root can stand in
# for one the gate imports, except in a base whose gate still
# imports its pins by name (see the trusted job above). Delete
# the first branch below when the trusted job's goes.
base_gate_script="$base_gate/scripts/check_thesis_facts_append.py"
if grep -qE '^[[:space:]]*from scripts\.receipt_pins import ' \
"$base_gate_script"; then
uv sync --locked --no-dev --project "$base_gate"
base_gate_python=(uv run --locked --no-dev --project "$base_gate" python -I)
else
uv sync --locked --no-dev --no-install-project --project "$base_gate"
base_gate_python=(uv run --no-sync --project "$base_gate" python -I)
fi

workspace_sha="$(git -C "$GITHUB_WORKSPACE" rev-parse HEAD)"
[[ "$workspace_sha" =~ ^[0-9a-f]{40,64}$ ]]

# As in the trusted job above: the commit is named, not inferred,
# and the base gate is asked what it accepts for the same reason.
base_gate_help="$(
uv run --locked --no-dev --project "$base_gate" \
python "$base_gate/scripts/check_thesis_facts_append.py" --help
)"
base_gate_help="$("${base_gate_python[@]}" "$base_gate_script" --help)"

cd "$RUNNER_TEMP"
case "$base_gate_help" in
*--commit*)
PYTHONPATH="$base_gate/scripts" \
PYTHONNOUSERSITE=1 \
uv run --locked --no-dev --project "$base_gate" \
python "$base_gate/scripts/check_thesis_facts_append.py" \
--root "$GITHUB_WORKSPACE" \
--commit "$workspace_sha" \
--base-ref "$base_sha"
"${base_gate_python[@]}" "$base_gate_script" \
--root "$GITHUB_WORKSPACE" \
--commit "$workspace_sha" \
--base-ref "$base_sha"
;;
*)
echo "note: the base gate at $base_sha predates --commit, so" \
"it judges the workspace, without binding its verdict to" \
"$workspace_sha." >&2
PYTHONPATH="$base_gate/scripts" \
PYTHONNOUSERSITE=1 \
uv run --locked --no-dev --project "$base_gate" \
python "$base_gate/scripts/check_thesis_facts_append.py" \
--root "$GITHUB_WORKSPACE" \
--base-ref "$base_sha"
"${base_gate_python[@]}" "$base_gate_script" \
--root "$GITHUB_WORKSPACE" \
--base-ref "$base_sha"
;;
esac
else
# The push path names the pushed commit explicitly too. The
# argument is required, so there is no spelling of this run in
# which the commit is taken from the checkout instead.
uv sync --locked --no-dev
uv run --locked --no-dev \
python scripts/check_thesis_facts_append.py \
--commit "$GITHUB_SHA"
# which the commit is taken from the checkout instead. It runs
# the pushed commit's own gate, which loads its pins by path, so
# it starts it isolated, as the jobs above do, with no project
# install to undo.
uv sync --locked --no-dev --no-install-project
uv run --no-sync python -I scripts/check_thesis_facts_append.py \
--commit "$GITHUB_SHA"
fi

- name: Install uv
Expand Down
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,43 @@ environment. Verification machinery is consumed from the `receipt`
package with this repository's trust pins committed in
`scripts/receipt_pins.py`; see `releases/README.md` (immutable
post-genesis, like everything under `releases/`) for the schema, offline
verification procedure, and security limits.
verification procedure, and security limits. The gate surface,
`APPEND_GATE_SPEC.gate_surface` in `scripts/receipt_pins.py`, lists the
files in the base checkout that decide the gate's verdict:

- the gate's scripts, the trust pins, the anchors, and the append workflow;
- the files that build its interpreter and environment: `pyproject.toml`,
`uv.lock`, `uv.toml`, `.python-version`, `.python-versions`, `.venv`,
`hatch.toml`, `hatch_build.py`, `sitecustomize.py` (in `scripts/` and at
the root) and `scripts/__pycache__`;
- every `.gitattributes` git applies to these files when it checks them
out: at the root and in `releases/`, which decide the anchor bytes, and
in `scripts/`, `.github/` and `.github/workflows/`. Receipt already refuses
`filter`, `ident` and `working-tree-encoding` on the gate's scripts and the
workflow, and line endings do not change how Python reads a script, so
these three cannot change a verdict today; listing them keeps that from
depending on the receipt version.

The gate refuses a pull request that changes any of them together with the
ledger, and names them when a pull request changes only them. The rest of
`scripts/` is off the surface, and so is any new module that could shadow one
of the gate's imports, there or at the root. None can reach the gate. The
append workflow builds the gate's environment without installing this
project, so the root is not on its import path. It starts the gate with
`python -I` and no `PYTHONPATH`, so `scripts/` is not on it either, and the
gate loads `receipt_pins.py` by path. The trusted pull request job runs the
default branch's copy of the workflow, which needs the same steps. A base
whose gate still imports its pins by name gets the project installed, so a
module that gate looks up at the root, such as a root `receipt_pins.py` or a
`scripts/__init__.py`, can still reach it.

Both pull request jobs also check the base gate out with an empty attribute
tree, so it runs from its committed bytes on a runner whose git sets no
`core.autocrlf` or `core.attributesFile`, as GitHub's ubuntu runners do. A
`.gitattributes` anywhere in the base, on the surface or off it, cannot
rewrite those bytes or stop the checkout. A stopped checkout would fail the
job on every later pull request, including the one that removes the file, and
two pull requests that each pass alone can merge into such a base. The
attributes still apply where the gate is checked out with them: on the push
path, in a local run, and in the trusted job until the default branch carries
this workflow. That is why they stay on the surface.
29 changes: 21 additions & 8 deletions scripts/check_thesis_facts_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,34 @@
import subprocess
import sys
import tempfile
import types
from collections.abc import Iterator
from typing import Any

import receipt.append_gate as _receipt
from receipt.release_chain import MANIFEST_RE, ReleaseChainError

try:
from receipt_pins import APPEND_GATE_SPEC
except ModuleNotFoundError as exc:
if exc.name != "receipt_pins":
raise
# The test suite copies the legacy three-script surface into temporary
# repositories. The editable consumer tree remains the sole pin owner.
from scripts.receipt_pins import APPEND_GATE_SPEC

def _load_receipt_pins() -> types.ModuleType:
"""Load the trust pins from ``receipt_pins.py`` beside this file, by path.

The append workflow starts this script with ``python -I``, which keeps its
directory off ``sys.path``. No other module in ``scripts/`` can then stand
in for one this script imports, such as a ``tempfile.py`` or a
``receipt/`` package. The pins are compiled from their source here rather
than imported, so a ``__pycache__`` entry cannot stand in for them either.
``scripts/verify_release_chain.py`` loads them the same way.
"""

path = pathlib.Path(__file__).resolve().with_name("receipt_pins.py")
module = types.ModuleType("receipt_pins")
module.__file__ = str(path)
code = compile(path.read_bytes(), str(path), "exec", dont_inherit=True)
exec(code, module.__dict__)
return module


APPEND_GATE_SPEC = _load_receipt_pins().APPEND_GATE_SPEC

CODE_ROOT = pathlib.Path(__file__).resolve().parents[1]
RELEASE_MANIFEST_PREFIX = APPEND_GATE_SPEC.release_manifest_prefix
Expand Down
Loading
Loading