scripts: run the CI locally and post the result on the PR while there are no runners #3383

Code

Clone this repository

Checkout

From your project repository, check out a new branch and test the changes.

git fetch -u origin "+refs/pull/3383/head:oreofr/local-ci"
git switch "oreofr/local-ci"
Open
leonida/mandy
OreoFR wants to merge 8 commits from oreofr/local-ci into main AGit

This branch is out-of-date with the base branch

Conversation
Reviewers 0 approvals
  • No reviewers
Details
This pull request is blocked because it's outdated.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3383/head:oreofr/local-ci'
git switch 'oreofr/local-ci'
Member

Until git.leonida.now has CI runners, scripts/run_ci_local.ps1 -Pr runs on a contributor's PC what the old GitHub workflows ran (no-game-data checks, Python tests with ci.yml's standalone/pytest split, D3D12 runtime build/tests/replay when d3d12/ changes; -Full adds the translator suite) in a temporary worktree, then posts one comment on the PR with Git's stored credentials for git.leonida.now (or MANDY_FORGEJO_TOKEN). Safeguards: it only posts on an open PR whose head is the commit it checked, updates its own earlier comment for the same head, -ExpectHead PR=SHA refuses a wrong PR number, paths and user names are scrubbed from the tails it posts, full logs stay local, and it never starts the game (it refuses the Rust steps while leonida.exe/gta6rt.exe runs). Tested: tests/test_local_ci.py (14), test_docs_links, test_docs_cli_flags, test_text_write_line_endings, test_tool_stdout_encoding, test_tool_startup, check_no_game_data; real runs posted on the launcher and Setup PRs. AI-assisted (oreofr-agent).

Until git.leonida.now has CI runners, scripts/run_ci_local.ps1 -Pr <N> runs on a contributor's PC what the old GitHub workflows ran (no-game-data checks, Python tests with ci.yml's standalone/pytest split, D3D12 runtime build/tests/replay when d3d12/ changes; -Full adds the translator suite) in a temporary worktree, then posts one comment on the PR with Git's stored credentials for git.leonida.now (or MANDY_FORGEJO_TOKEN). Safeguards: it only posts on an open PR whose head is the commit it checked, updates its own earlier comment for the same head, -ExpectHead PR=SHA refuses a wrong PR number, paths and user names are scrubbed from the tails it posts, full logs stay local, and it never starts the game (it refuses the Rust steps while leonida.exe/gta6rt.exe runs). Tested: tests/test_local_ci.py (14), test_docs_links, test_docs_cli_flags, test_text_write_line_endings, test_tool_stdout_encoding, test_tool_startup, check_no_game_data; real runs posted on the launcher and Setup PRs. AI-assisted (oreofr-agent).
scripts/run_ci_local.ps1 (scripts/local_ci.py) fetches a git.leonida.now pull
request, merges it into main in a temporary worktree, runs the checks the
GitHub workflows ran (no game data, Python tests split as ci.yml does, the
D3D12 runtime's build, tests and replay test when d3d12/ changes), and posts a
summary on the pull request with Git's stored credentials. It posts only on an
open pull request whose head is the commit it checked, updates its own earlier
comment for the same head, scrubs paths and user names, and never starts the
game.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Besides its comment, run_ci_local.ps1 posts a Forgejo commit status on the
checked head (context local-ci, success or failure, at most 140 characters),
so branch protection can require it later. Until git.leonida.now has runners,
a pull request there gets a local run before it is opened or updated, and no
merge is asked for while it is red.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A pull request that only changes those files gets the quick run by default:
no-game-data checks, the docs checks and the test files related to the
changed files. Anything else gets the CI's checks, -Quick and -Full force a
mode. Test files run on every core with their output collected, and the
cargo target directory stays in %LOCALAPPDATA%/mandy-ci between runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- plan_jobs: logical CPUs minus one or two, Rust jobs capped by free RAM (~1.5 GB each), --jobs to override
- rust_scope: cargo test/clippy for the changed crates and their dependents; workspace for cargo files or stray
  runtime files; none for d3d12/docs and d3d12/scripts
- the shipped leonida build only when the app crate, a build script or a cargo file changed
- one reused worktree under %LOCALAPPDATA%/mandy-ci/work (--clean starts it over)
- rust-lld on Windows after a probe link, default linker otherwise

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
First-time contributor

Verified natively on Windows 11 (Python 3.12, no game files): python -I scripts/red_green.py against main (0043b8d33, PR head ed701a5dc) — 36/36 tests in tests/test_local_ci.py passed cleanly on green (red does not build on base because scripts/local_ci.py is newly added). AI-assisted: Gemini 4 Neon (Antigravity).

Verified natively on Windows 11 (Python 3.12, no game files): `python -I scripts/red_green.py` against `main` (`0043b8d33`, PR head `ed701a5dc`) — **36/36** tests in `tests/test_local_ci.py` passed cleanly on green (`red does not build` on base because `scripts/local_ci.py` is newly added). AI-assisted: Gemini 4 Neon (Antigravity).
A review of #3383 found three ways the token or the account could leak:
- api() followed 3xx redirects and urllib carried the Authorization header to
  the new host: redirects are now refused (NoRedirect opener).
- `git credential fill` came before MANDY_FORGEJO_TOKEN, so the account's
  password (full rights) was used, and Git Credential Manager could open a
  window: the token now comes first and Git is not asked when it is set; Git is
  asked with GIT_TERMINAL_PROMPT=0 and GCM_INTERACTIVE=never.
- the pull request's code ran with the token and LEONIDA_GAME_DIR in its
  environment: every step now runs in safe_env() (no token, password, secret,
  GCM, askpass or game folder variables), and a pull request that changes how
  the checks run (build.rs, rust-toolchain, .cargo/config, conftest.py, the
  test runners, this script, xtask) is refused unless --trust-pr-code /
  -TrustPrCode is given after reading those changes.

Running a pull request's code still runs it with the user's rights; the
script and CONTRIBUTING.md now say so: read the diff first.

Red/green: the 8 new tests fail (1 failure, 7 errors) on the previous
local_ci.py and the 45 tests pass on this one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Member

Updated after MoonEyes' security review (refused at ed701a5dc): head 2f22313ce.

  • Redirects: api() now uses an opener that refuses every 3xx, so the Authorization header never goes to another host (NoRedirect).
  • Credentials: MANDY_FORGEJO_TOKEN first; when it is set, Git's store is not asked at all. Otherwise git credential fill runs with GIT_TERMINAL_PROMPT=0 and GCM_INTERACTIVE=never (no window).
  • Pull request code: every step runs in safe_env(): no token, password, secret, GCM/askpass or LEONIDA_GAME_DIR/LEONIDA_MOVIE variables. A pull request that changes how the checks run (build.rs, rust-toolchain, .cargo/config, conftest.py, the test runners, this script, xtask) is refused unless --trust-pr-code / -TrustPrCode is given after reading those changes. The docstring and CONTRIBUTING.md say plainly that the PR's code still runs with the user's rights: read the diff first.

Tests (Windows 11): tests/test_local_ci.py 45 OK; the 8 new tests fail on the previous local_ci.py (1 failure, 7 errors). test_docs_links, test_text_write_line_endings, test_win_launchers OK; check_no_game_data clean.
Posting was checked end to end on #3330 before this change (comment + local-ci status).

Not merging: needs a new review (it touches AGENTS.md and CONTRIBUTING.md).

AI-assisted (Claude Code).

Updated after MoonEyes' security review (refused at `ed701a5dc`): head `2f22313ce`. - **Redirects:** `api()` now uses an opener that refuses every 3xx, so the Authorization header never goes to another host (`NoRedirect`). - **Credentials:** `MANDY_FORGEJO_TOKEN` first; when it is set, Git's store is not asked at all. Otherwise `git credential fill` runs with `GIT_TERMINAL_PROMPT=0` and `GCM_INTERACTIVE=never` (no window). - **Pull request code:** every step runs in `safe_env()`: no token, password, secret, GCM/askpass or `LEONIDA_GAME_DIR`/`LEONIDA_MOVIE` variables. A pull request that changes how the checks run (build.rs, rust-toolchain, .cargo/config, conftest.py, the test runners, this script, xtask) is refused unless `--trust-pr-code` / `-TrustPrCode` is given after reading those changes. The docstring and CONTRIBUTING.md say plainly that the PR's code still runs with the user's rights: read the diff first. Tests (Windows 11): `tests/test_local_ci.py` 45 OK; the 8 new tests fail on the previous `local_ci.py` (1 failure, 7 errors). `test_docs_links`, `test_text_write_line_endings`, `test_win_launchers` OK; `check_no_game_data` clean. Posting was checked end to end on #3330 before this change (comment + `local-ci` status). Not merging: needs a new review (it touches AGENTS.md and CONTRIBUTING.md). AI-assisted (Claude Code).
MoonEyes' second review of #3383 (head 2f22313ce) found ways a pull request
could still steer its own checks or reach the token:
- SENSITIVE now also covers any Cargo.toml, pytest.ini, pyproject.toml,
  setup.cfg, tox.ini, sitecustomize/usercustomize.py, .pth files, a .py file
  at the top of the repository (it would shadow a standard module) and
  scripts/check_no_game_data.py; --trust-pr-code now needs --expect-head.
- compileall and pytest run with -I -B (no module from the worktree replaces
  a standard one, no bytecode written), pytest without its cache plugin.
- the reused worktree is cleaned with git clean -fdx, so nothing a pull
  request leaves (ignored .pyc files included) survives into the next.
- safe_env() is an allow list (Windows, Rust and C++ tool variables only) and
  sets PYTHONDONTWRITEBYTECODE=1: unknown secret names never get through.
- posting uses MANDY_FORGEJO_TOKEN only; Git's stored password (every right
  on every repository) is never used. CONTRIBUTING.md says which token scopes
  to set, and says plainly that hostile code can still read what the account
  can read: read the diff first.
- scrub() removes the token from any text a comment quotes.
- an existing remote pointing at the project is reused instead of adding a
  second one.

Red/green: 6 of the 48 tests fail on 2f22313ce; all 48 pass here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Member

Updated after the second security review (refused at 2f22313ce): head c38134d7a.

  • SENSITIVE also covers any Cargo.toml, pytest.ini, pyproject.toml, setup.cfg, tox.ini, sitecustomize.py/usercustomize.py, .pth files, any .py at the top of the repository and scripts/check_no_game_data.py. --trust-pr-code now requires --expect-head.
  • compileall and pytest run with -I -B (pytest also without its cache plugin): a top-level compileall.py can no longer replace the standard module.
  • The reused worktree is cleaned with git clean -fdx; steps get PYTHONDONTWRITEBYTECODE=1.
  • safe_env() is now an allow list (Windows, Rust and MSVC variables only): FORGEJO_PAT, GITEA_PAT, AWS_*, HTTPS_PROXY, GIT_CONFIG_PARAMETERS… never get through.
  • Posting uses MANDY_FORGEJO_TOKEN only; Git's stored password is never used. CONTRIBUTING.md gives the token scopes (issue + repository read/write) and now says plainly that hostile code could still read what the account can read on that PC: read the diff first.
  • scrub() removes the token value from quoted text.
  • An existing remote that points at the project is reused instead of adding leonida-now next to it.

Tests (Windows 11): tests/test_local_ci.py 48 OK; 6 of them fail on 2f22313ce (red/green). test_docs_links, test_text_write_line_endings, test_win_launchers OK; check_no_game_data clean.

Not merging: needs a new review.

AI-assisted (Claude Code).

Updated after the second security review (refused at `2f22313ce`): head `c38134d7a`. - `SENSITIVE` also covers any `Cargo.toml`, `pytest.ini`, `pyproject.toml`, `setup.cfg`, `tox.ini`, `sitecustomize.py`/`usercustomize.py`, `.pth` files, any `.py` at the top of the repository and `scripts/check_no_game_data.py`. `--trust-pr-code` now requires `--expect-head`. - `compileall` and `pytest` run with `-I -B` (pytest also without its cache plugin): a top-level `compileall.py` can no longer replace the standard module. - The reused worktree is cleaned with `git clean -fdx`; steps get `PYTHONDONTWRITEBYTECODE=1`. - `safe_env()` is now an allow list (Windows, Rust and MSVC variables only): `FORGEJO_PAT`, `GITEA_PAT`, `AWS_*`, `HTTPS_PROXY`, `GIT_CONFIG_PARAMETERS`… never get through. - Posting uses `MANDY_FORGEJO_TOKEN` only; Git's stored password is never used. CONTRIBUTING.md gives the token scopes (issue + repository read/write) and now says plainly that hostile code could still read what the account can read on that PC: read the diff first. - `scrub()` removes the token value from quoted text. - An existing remote that points at the project is reused instead of adding `leonida-now` next to it. Tests (Windows 11): `tests/test_local_ci.py` 48 OK; 6 of them fail on `2f22313ce` (red/green). `test_docs_links`, `test_text_write_line_endings`, `test_win_launchers` OK; `check_no_game_data` clean. Not merging: needs a new review. AI-assisted (Claude Code).
- --expect-head PR=SHA is required for every pull request run: 12 to 40 hex
  characters, matched as a prefix of the full head SHA.
- A throwaway clone per pull request (git clone --no-local from a bare mirror
  whose config is rewritten and hooks removed every run), deleted after the
  run; no worktree of the user's repository. Every git call runs with
  core.hooksPath set to a new empty folder, core.fsmonitor=false, no
  credential helper and the allowed environment.
- MANDY_FORGEJO_TOKEN is taken out of os.environ at start; every subprocess
  (git, cargo, rustc, Python, PowerShell, tasklist) gets safe_env(). Each check
  runs in a Windows job object (a process group elsewhere) killed when the
  check ends, so nothing of the pull request is alive when the result is posted.
- Sensitive files are hashed before and after the checks (no git call on the
  clone after its code ran): one created or changed fails the run and nothing
  is posted.
- Tails go in a fence longer than any backtick run in them; table cells are
  escaped.
- An earlier comment is updated only when its author is the token's own user
  (GET /user); otherwise a new one is added. Reads are anonymous.
- Comment-only is the default posting mode (issue scope); --set-status adds
  the local-ci commit status and needs repository write, meant for a future
  runner server with a bot account.
- Each pull request has its own Rust target dir (pruned after 14 days unused).
- CONTRIBUTING.md and AGENTS.md: what each mode's token can do, no
  repository-write token for contributors, and whoever merges runs the local
  CI on the current head right before merging.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Member

Third security review addressed: new head 0200d8659cf5

Blocking

  1. Unreviewed code ran by default. --expect-head PR=SHA is now required for every --pr (also with --no-post); the SHA must be 12 to 40 hex characters and is matched as a prefix of the full head SHA. A missing or short value stops the script before anything is fetched. run_ci_local.ps1 makes -ExpectHead mandatory.
  2. The token was still within reach of the PR's code.
    • MANDY_FORGEJO_TOKEN is popped from os.environ at the start of main() and kept only in a local variable that is handed to post(). Every subprocess now gets safe_env() explicitly: git (plus GIT_TERMINAL_PROMPT=0), cargo metadata, rustc (sysroot and lld probe, run from a scratch folder so no rust-toolchain file of a PR picks the toolchain), PowerShell for machine_line, tasklist, and the pytest probe. run_contained() refuses env=None.
    • No worktree of the user's repository any more. The script keeps a bare mirror in %LOCALAPPDATA%\mandy-ci\mirror.git, fetched from the project URL. Its config is rewritten from a fixed template on every run, and its hooks/, info/attributes and objects/info/alternates are removed. Each PR gets a throwaway git clone --no-local --template= --config core.fsmonitor=false --config core.hooksPath=nul of that mirror, which is deleted after the run (in a finally). Every git call runs as git -c core.hooksPath=<new empty temp dir> -c core.fsmonitor=false -c credential.helper= .... No git command touches the clone after the PR's code has run. The old reused worktree is removed on the next run (git worktree prune).
    • Every check runs in a Windows job object with KILL_ON_JOB_CLOSE (POSIX: a new session, then killpg). The process starts suspended, is put in the job, then resumed, so it cannot start a child outside the job. The job is terminated when the check ends. Posting happens only after both groups have joined, so none of the PR's processes is still alive. A unit test starts a detached grandchild and checks that it stops writing once the step ends.
    • Honest limit (in CONTRIBUTING): code that runs as your account can still read the token wherever it is stored (a user environment variable is in the registry). The script keeps the token out of the checks' environment and out of their lifetime; it cannot hide it from same-user code. Hence the mandatory "read the diff first" and the token scope rules below.
    • The wrong CONTRIBUTING sentence ("limited to comments and statuses") is gone. See the posting modes below.

Medium

  • Sensitive files re-checked after the checks: sensitive_snapshot() hashes every file under the clone that SENSITIVE matches, ignored files included (it skips .git and never follows a link or junction), before and after the checks. If any such path was created, changed or removed, the step Sensitive files unchanged by the checks FAILs with the list and nothing is posted. I hash the files instead of running git status --porcelain --ignored because the PR's code can write .git/config during the run, and git status can run clean filters from it.
  • Markdown injection: a tail goes in a fence longer than the longest backtick run in it (code_block()). Table cells (step name, detail, machine) are escaped: |, <, >, &, @, backtick and newlines.
  • Comment reuse by author: existing_comment(comments, head, login) only matches a comment whose user.login is the token's own user (GET /user). If the token cannot read /user, the script adds a new comment and never edits one. The reads (pull request, comments) are now anonymous; the token is sent only to write and to /user.
  • Shared Rust cache: each PR has its own CARGO_TARGET_DIR (cargo-target\pr-<N>). The old shared cache and per-PR caches unused for 14 days are deleted. Cost measured here (Core Ultra 7 155H, 14 jobs): a whole-workspace cargo test on a new cache took 831 s. A later head of the same PR rebuilt the workspace crates in 97 s (build only, fresh clone).

Posting modes (decision from OreoFR)

  • Default: comment only, no commit status call at all. The token needs only the issue scope (read and write). With user read as well, it updates its own earlier comment for the same head instead of adding one.
  • --set-status / -SetStatus: also sets the local-ci commit status. On Forgejo this needs repository write, which can also push. It is meant for a future runner server with a bot account the admins manage, not for contributors' PCs. CONTRIBUTING tells contributors not to make a repository-write token.
  • CONTRIBUTING and AGENTS: whoever merges runs the local CI themselves on the current head merged into current main, right before merging, and merges only if that run passes. A contributor's own posted run is useful information but never replaces it.

Tests

  • python -I tests/test_local_ci.py: 65 tests OK. They cover: expect-head mandatory and length; every helper's env being safe_env() (subprocess.run/Popen patched, no token in any call env); the throwaway clone's flags; the mirror forgetting a planted hook, fsmonitor and alternates; the clone deleted after the run; the token absent from os.environ while the checks run; the post-run sensitive check refusing to post; fence and cell escaping; comment reuse by author only; no status call by default and one with --set-status; an issue-only token adding a comment; anonymous reads; job-object cleanup of a detached grandchild.
  • Red/green: the same test file against the previous script (git show c38134d7a:scripts/local_ci.py) gives 29 of 65 failing (21 errors, 8 failures). Against the new one, 65/65 pass.
  • tests/test_docs_links.py, test_text_write_line_endings.py, test_win_launchers.py, test_docs_cli_flags.py, test_tool_startup.py: OK. scripts/check_no_game_data.py on the changed files: OK.

End to end, no posting: python -I scripts/local_ci.py --pr 3330 --expect-head 3330=eb008afe7201d7bb7d434bc54d388304ee774b0e --no-post. It took 17 min in total, including the first fetch of the mirror and a cold Rust cache. Normal mode; Rust scope is the whole workspace because d3d12/Get-D3D12.cmd is outside a crate.

Step Result Time
Merge into main PASS 0 s
No game data tracked PASS (3832 files) 0 s
No game data under another name PASS 36 s
Compile Python PASS 37 s
make_game.py --help PASS 1 s
Python tests (standard library) FAIL: 674 files, 1 failed 600 s
Python tests (pytest) SKIP (not installed here) 0 s
Rust tests PASS: 1770 passed 831 s
Rust replay test PASS 75 s
Rust asset policy REPORT: 5 passed, 1 failed (report only) 8 s
Clippy -D warnings REPORT (report only) 36 s
Sensitive files unchanged by the checks PASS (38 checked) 0 s

The one stdlib failure is not caused by this script or by #3330. tests/test_progress_dashboard_ci.py::test_report_generation_preserves_its_exit_code_and_failure_evidence fails on current main: there, tools/progress_dashboard.py defaults --repo to https://git.leonida.now/leonida/mandy.git and refuses --github for it, but the test still calls --github without --repo. The clone was deleted after the run and the per-PR cargo cache was created as expected.

Not merging: this needs a new review.

AI-assisted (Claude Code)

**Third security review addressed: new head `0200d8659cf5`** Blocking 1. **Unreviewed code ran by default.** `--expect-head PR=SHA` is now required for every `--pr` (also with `--no-post`); the SHA must be 12 to 40 hex characters and is matched as a prefix of the full head SHA. A missing or short value stops the script before anything is fetched. `run_ci_local.ps1` makes `-ExpectHead` mandatory. 2. **The token was still within reach of the PR's code.** - `MANDY_FORGEJO_TOKEN` is popped from `os.environ` at the start of `main()` and kept only in a local variable that is handed to `post()`. Every subprocess now gets `safe_env()` explicitly: git (plus `GIT_TERMINAL_PROMPT=0`), `cargo metadata`, `rustc` (sysroot and lld probe, run from a scratch folder so no `rust-toolchain` file of a PR picks the toolchain), PowerShell for `machine_line`, `tasklist`, and the pytest probe. `run_contained()` refuses `env=None`. - **No worktree of the user's repository any more.** The script keeps a bare mirror in `%LOCALAPPDATA%\mandy-ci\mirror.git`, fetched from the project URL. Its `config` is rewritten from a fixed template on every run, and its `hooks/`, `info/attributes` and `objects/info/alternates` are removed. Each PR gets a throwaway `git clone --no-local --template= --config core.fsmonitor=false --config core.hooksPath=nul` of that mirror, which is deleted after the run (in a `finally`). Every git call runs as `git -c core.hooksPath=<new empty temp dir> -c core.fsmonitor=false -c credential.helper= ...`. No git command touches the clone after the PR's code has run. The old reused worktree is removed on the next run (`git worktree prune`). - Every check runs in a **Windows job object** with KILL_ON_JOB_CLOSE (POSIX: a new session, then `killpg`). The process starts suspended, is put in the job, then resumed, so it cannot start a child outside the job. The job is terminated when the check ends. Posting happens only after both groups have joined, so none of the PR's processes is still alive. A unit test starts a detached grandchild and checks that it stops writing once the step ends. - Honest limit (in CONTRIBUTING): code that runs as your account can still read the token wherever it is stored (a user environment variable is in the registry). The script keeps the token out of the checks' environment and out of their lifetime; it cannot hide it from same-user code. Hence the mandatory "read the diff first" and the token scope rules below. - The wrong CONTRIBUTING sentence ("limited to comments and statuses") is gone. See the posting modes below. Medium - **Sensitive files re-checked after the checks:** `sensitive_snapshot()` hashes every file under the clone that SENSITIVE matches, ignored files included (it skips `.git` and never follows a link or junction), before and after the checks. If any such path was created, changed or removed, the step `Sensitive files unchanged by the checks` FAILs with the list and nothing is posted. I hash the files instead of running `git status --porcelain --ignored` because the PR's code can write `.git/config` during the run, and `git status` can run clean filters from it. - **Markdown injection:** a tail goes in a fence longer than the longest backtick run in it (`code_block()`). Table cells (step name, detail, machine) are escaped: `|`, `<`, `>`, `&`, `@`, backtick and newlines. - **Comment reuse by author:** `existing_comment(comments, head, login)` only matches a comment whose `user.login` is the token's own user (`GET /user`). If the token cannot read `/user`, the script adds a new comment and never edits one. The reads (pull request, comments) are now anonymous; the token is sent only to write and to `/user`. - **Shared Rust cache:** each PR has its own `CARGO_TARGET_DIR` (`cargo-target\pr-<N>`). The old shared cache and per-PR caches unused for 14 days are deleted. Cost measured here (Core Ultra 7 155H, 14 jobs): a whole-workspace `cargo test` on a new cache took **831 s**. A later head of the same PR rebuilt the workspace crates in **97 s** (build only, fresh clone). **Posting modes (decision from OreoFR)** - Default: **comment only**, no commit status call at all. The token needs only the *issue* scope (read and write). With *user* read as well, it updates its own earlier comment for the same head instead of adding one. - `--set-status` / `-SetStatus`: also sets the `local-ci` commit status. On Forgejo this needs *repository* write, which can also push. It is meant for a future runner server with a bot account the admins manage, not for contributors' PCs. CONTRIBUTING tells contributors not to make a repository-write token. - CONTRIBUTING and AGENTS: whoever merges runs the local CI themselves on the current head merged into current `main`, right before merging, and merges only if that run passes. A contributor's own posted run is useful information but never replaces it. **Tests** - `python -I tests/test_local_ci.py`: 65 tests OK. They cover: expect-head mandatory and length; every helper's env being `safe_env()` (subprocess.run/Popen patched, no token in any call env); the throwaway clone's flags; the mirror forgetting a planted hook, fsmonitor and alternates; the clone deleted after the run; the token absent from `os.environ` while the checks run; the post-run sensitive check refusing to post; fence and cell escaping; comment reuse by author only; no status call by default and one with `--set-status`; an issue-only token adding a comment; anonymous reads; job-object cleanup of a detached grandchild. - **Red/green:** the same test file against the previous script (`git show c38134d7a:scripts/local_ci.py`) gives **29 of 65 failing** (21 errors, 8 failures). Against the new one, 65/65 pass. - `tests/test_docs_links.py`, `test_text_write_line_endings.py`, `test_win_launchers.py`, `test_docs_cli_flags.py`, `test_tool_startup.py`: OK. `scripts/check_no_game_data.py` on the changed files: OK. **End to end, no posting:** `python -I scripts/local_ci.py --pr 3330 --expect-head 3330=eb008afe7201d7bb7d434bc54d388304ee774b0e --no-post`. It took 17 min in total, including the first fetch of the mirror and a cold Rust cache. Normal mode; Rust scope is the whole workspace because `d3d12/Get-D3D12.cmd` is outside a crate. | Step | Result | Time | |---|---|---| | Merge into main | PASS | 0 s | | No game data tracked | PASS (3832 files) | 0 s | | No game data under another name | PASS | 36 s | | Compile Python | PASS | 37 s | | make_game.py --help | PASS | 1 s | | Python tests (standard library) | FAIL: 674 files, 1 failed | 600 s | | Python tests (pytest) | SKIP (not installed here) | 0 s | | Rust tests | PASS: 1770 passed | 831 s | | Rust replay test | PASS | 75 s | | Rust asset policy | REPORT: 5 passed, 1 failed (report only) | 8 s | | Clippy -D warnings | REPORT (report only) | 36 s | | Sensitive files unchanged by the checks | PASS (38 checked) | 0 s | The one stdlib failure is not caused by this script or by #3330. `tests/test_progress_dashboard_ci.py::test_report_generation_preserves_its_exit_code_and_failure_evidence` fails on current `main`: there, `tools/progress_dashboard.py` defaults `--repo` to `https://git.leonida.now/leonida/mandy.git` and refuses `--github` for it, but the test still calls `--github` without `--repo`. The clone was deleted after the run and the per-PR cargo cache was created as expected. Not merging: this needs a new review. AI-assisted (Claude Code)
- Each run clones into a folder made fresh for it (<cache>/clones/<run id>/repo); the .cargo/config[.toml],
  rust-toolchain[.toml] and pytest config files cargo and pytest read from the folders above the clone are
  deleted up to %LOCALAPPDATA%\mandy-ci before and after the run, and one a check creates or changes in any
  folder above the clone fails the run (nothing posted).
- SENSITIVE also covers .pytest.ini, pytest.toml and .pytest.toml (every config file pytest 9 reads).
- The mirror loses info/grafts, commondir, shallow, refs/replace (packed-refs entries included), config.worktree
  and worktrees on every run (commondir or shallow: fetched over again); every git call has
  GIT_NO_REPLACE_OBJECTS=1.
- The clone fetches the verified head by its full SHA, checks what it got, merges that SHA, and checks that the
  merge's parents are main and the head (a merged head: HEAD is main and contains it).
- --ai-assisted [WHO] names who assisted instead of a fixed name (-AiAssisted 'name' in run_ci_local.ps1).
- ALLOWED_VARS is a set of exact names (the MSVC/Windows SDK ones measured from VsDevCmd.bat of VS 2022 Build
  Tools), no pattern.
- The token's last reference is dropped right after the last post(); the docs state the remaining limits
  (token in memory and registry, WMI/scheduled-task processes escape the job object), that hostile code can
  also write and persist, and that the local CI runs after opening or updating a pull request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Member

Review points fixed: head 7ffb0e9f9 (was 0200d8659)

  1. Parent folders: each run clones into a new folder (%LOCALAPPDATA%\mandy-ci\clones\<run id>\repo). The .cargo/config[.toml], rust-toolchain[.toml] and pytest config files in the folders above the clone are deleted up to mandy-ci before and after each run. If a check creates or changes one in any folder above the clone, the run fails and nothing is posted.
  2. SENSITIVE now also covers .pytest.ini, pytest.toml and .pytest.toml, so every config file pytest 9 reads is covered (with pytest.ini, pyproject.toml, tox.ini, setup.cfg).
  3. Token: the last reference is dropped right after the last post(). The docs list this as a remaining limit: it stays in memory (Python cannot wipe a string) and in the registry, and only a separate account would fix that.
  4. WMI and scheduled-task processes escape the job object: the docstring and CONTRIBUTING.md now list this as a remaining limit.
  5. Mirror: info/grafts, commondir, shallow, refs/replace (with their packed-refs entries), config.worktree and worktrees are deleted on every run, along with hooks and the alternates. A mirror with commondir or shallow is fetched again from scratch. Every git call also has GIT_NO_REPLACE_OBJECTS=1.
  6. The clone fetches the verified full SHA (+<sha>:refs/local-ci/head), checks what it got and merges that SHA. Afterwards it checks that HEAD^1/HEAD^2 are main and the head. For an already-merged head, git makes no merge commit, so the check is that HEAD is main and contains the head.
  7. CONTRIBUTING.md now says hostile code can also write and persist (Run key, a checkout's .git/hooks, scripts/local_ci.py), not only read. Reading the diff is the only barrier.
  8. AGENTS.md and CONTRIBUTING.md now say "after opening (or updating) the pull request".
  9. --ai-assisted [WHO] (-AiAssisted 'name'): the line reads AI-assisted (WHO), or just AI-assisted when no name is given. No fixed name any more.
  10. ALLOWED_VARS is now a set of exact names, with no pattern. The MSVC/SDK names were measured from VsDevCmd.bat -arch=amd64 of VS 2022 Build Tools; VCPKG_ROOT is left out.

Tests: tests/test_local_ci.py 74/74 (10 new or changed tests). Red/green against 0200d8659: 12 red (10 failures, 2 errors), 74 green. test_docs_links 18 OK, test_text_write_line_endings 3 OK, test_win_launchers 14 OK, test_tool_stdout_encoding 4 OK, check_no_game_data.py on the changed files clean.
End to end: --no-post run on #3417 (merged, 15b5f4170), on a Core Ultra 7 155H. Mirror, fetch by SHA, merge check (merged-head case), fresh folder and parent clean-up all worked; the clone was deleted afterwards. It found 38 sensitive files, all unchanged. The standard-library step had 2 of 673 files fail on timeouts under load (test_manifest_verifier: a 15 s subprocess timeout; test_run_semantic_tests: a serial-timeout test). Neither is related to this script.

Not merging: Moon decides.
AI-assisted (Claude Code)

**Review points fixed: head `7ffb0e9f9`** (was `0200d8659`) 1. Parent folders: each run clones into a new folder (`%LOCALAPPDATA%\mandy-ci\clones\<run id>\repo`). The `.cargo/config[.toml]`, `rust-toolchain[.toml]` and pytest config files in the folders above the clone are deleted up to `mandy-ci` before and after each run. If a check creates or changes one in any folder above the clone, the run fails and nothing is posted. 2. `SENSITIVE` now also covers `.pytest.ini`, `pytest.toml` and `.pytest.toml`, so every config file pytest 9 reads is covered (with `pytest.ini`, `pyproject.toml`, `tox.ini`, `setup.cfg`). 3. Token: the last reference is dropped right after the last `post()`. The docs list this as a remaining limit: it stays in memory (Python cannot wipe a string) and in the registry, and only a separate account would fix that. 4. WMI and scheduled-task processes escape the job object: the docstring and CONTRIBUTING.md now list this as a remaining limit. 5. Mirror: `info/grafts`, `commondir`, `shallow`, `refs/replace` (with their `packed-refs` entries), `config.worktree` and `worktrees` are deleted on every run, along with `hooks` and the alternates. A mirror with `commondir` or `shallow` is fetched again from scratch. Every git call also has `GIT_NO_REPLACE_OBJECTS=1`. 6. The clone fetches the verified full SHA (`+<sha>:refs/local-ci/head`), checks what it got and merges that SHA. Afterwards it checks that `HEAD^1`/`HEAD^2` are main and the head. For an already-merged head, git makes no merge commit, so the check is that HEAD is main and contains the head. 7. CONTRIBUTING.md now says hostile code can also write and persist (Run key, a checkout's `.git/hooks`, `scripts/local_ci.py`), not only read. Reading the diff is the only barrier. 8. AGENTS.md and CONTRIBUTING.md now say "after opening (or updating) the pull request". 9. `--ai-assisted [WHO]` (`-AiAssisted 'name'`): the line reads `AI-assisted (WHO)`, or just `AI-assisted` when no name is given. No fixed name any more. 10. `ALLOWED_VARS` is now a set of exact names, with no pattern. The MSVC/SDK names were measured from `VsDevCmd.bat -arch=amd64` of VS 2022 Build Tools; `VCPKG_ROOT` is left out. **Tests**: `tests/test_local_ci.py` 74/74 (10 new or changed tests). Red/green against `0200d8659`: 12 red (10 failures, 2 errors), 74 green. `test_docs_links` 18 OK, `test_text_write_line_endings` 3 OK, `test_win_launchers` 14 OK, `test_tool_stdout_encoding` 4 OK, `check_no_game_data.py` on the changed files clean. **End to end**: `--no-post` run on #3417 (merged, `15b5f4170`), on a Core Ultra 7 155H. Mirror, fetch by SHA, merge check (merged-head case), fresh folder and parent clean-up all worked; the clone was deleted afterwards. It found 38 sensitive files, all unchanged. The standard-library step had 2 of 673 files fail on timeouts under load (`test_manifest_verifier`: a 15 s subprocess timeout; `test_run_semantic_tests`: a serial-timeout test). Neither is related to this script. Not merging: Moon decides. AI-assisted (Claude Code)
Collaborator

I ran it rather than reading it only.

python -m unittest discover -s tests -p test_local_ci.py: 74 tests, OK in 1.7 s. Then a real run on a docs pull request, python -I scripts/local_ci.py --pr 3409 --expect-head 3409=341e904be99841f72e9d4fb43c7f6e3245335769 --no-post: PASS in about two minutes - merge clean, no game data (3,843 files, 19 s), 38 sensitive files unchanged, 5 Python test files in 56 s, summary written.

The security work holds: the token is taken out of the environment before any check starts, Authorization goes only on the writes and on GET /user, every redirect is refused, the pull request must still be open with that head before a comment is written, and an earlier comment is edited only when the token's own login wrote it. The refusal list plus the second snapshot after the checks is the right pair.

Two things I would change before AGENTS.md makes this the rule:

  1. A skipped test step still gives PASS. On this machine pytest is not installed, so Python tests (pytest) is SKIP with "0 files" and the run still ends PASS; a pull request that only touches pytest tests would get a green comment with none of them run. Make a skipped test step fail unless a flag allows it, or at least make the header read PASS (pytest skipped), and say in CONTRIBUTING.md that pip install -r tests/requirements.txt is part of a green run.
  2. A conflicting pull request fails at Merge into main, which is correct, but the detail ("conflict in N file(s)") reads like a code failure; put "rebase onto main" there. The 51 conflicting pull requests will hit this on every run until they are rebased.

The governance in CONTRIBUTING.md is right: a posted run is information, and the merger reruns on the head merged into current main. That is the only way a comment-only CI stays honest. Keep --set-status reserved for the future bot account, and say so in the summary too, so nobody creates a repository-write token on a PC.

With an issue-scope token I can start running this across the queue and posting the results.

I ran it rather than reading it only. `python -m unittest discover -s tests -p test_local_ci.py`: 74 tests, OK in 1.7 s. Then a real run on a docs pull request, `python -I scripts/local_ci.py --pr 3409 --expect-head 3409=341e904be99841f72e9d4fb43c7f6e3245335769 --no-post`: PASS in about two minutes - merge clean, no game data (3,843 files, 19 s), 38 sensitive files unchanged, 5 Python test files in 56 s, summary written. The security work holds: the token is taken out of the environment before any check starts, `Authorization` goes only on the writes and on GET /user, every redirect is refused, the pull request must still be open with that head before a comment is written, and an earlier comment is edited only when the token's own login wrote it. The refusal list plus the second snapshot after the checks is the right pair. Two things I would change before AGENTS.md makes this the rule: 1. **A skipped test step still gives PASS.** On this machine pytest is not installed, so `Python tests (pytest)` is SKIP with "0 files" and the run still ends PASS; a pull request that only touches pytest tests would get a green comment with none of them run. Make a skipped test step fail unless a flag allows it, or at least make the header read `PASS (pytest skipped)`, and say in CONTRIBUTING.md that `pip install -r tests/requirements.txt` is part of a green run. 2. **A conflicting pull request fails at `Merge into main`**, which is correct, but the detail ("conflict in N file(s)") reads like a code failure; put "rebase onto main" there. The 51 conflicting pull requests will hit this on every run until they are rebased. The governance in CONTRIBUTING.md is right: a posted run is information, and the merger reruns on the head merged into current main. That is the only way a comment-only CI stays honest. Keep `--set-status` reserved for the future bot account, and say so in the summary too, so nobody creates a repository-write token on a PC. With an issue-scope token I can start running this across the queue and posting the results.
Collaborator

A real token found a bug the tests cannot: the token is printed in clear when it carries a trailing newline.

Setting MANDY_FORGEJO_TOKEN with setx from a copy that ends with a newline stores the newline. The script read it as is, urllib refused the header, and the error path printed:

posting failed: Invalid header value b'token <40 hex characters>\n'

scrub replaces the token string exactly, and here the message carried it with the newline escaped as \n, so nothing matched. The secret went to the console and to the log file. This is the one place where a secret must never surface, so it should be fixed before the tool becomes a rule:

  1. read_token should .strip() what it gets (environment and registry): a token never contains whitespace, and a stray newline is the normal result of pasting into setx.
  2. scrub should redact token.strip() too, and treat the escapes: replace the token with the whitespace around it, token.replace(...), and keep the existing 40-hex-near-'token' rule as the net under it.
  3. Worth a unit test with a token ending in \n: the header must be built from the stripped value and the message must not contain the original.

Everything else about the run is good: merge clean, no game data, 38 sensitive files, and the summary was correct — it just could not post.

A real token found a bug the tests cannot: **the token is printed in clear when it carries a trailing newline.** Setting `MANDY_FORGEJO_TOKEN` with `setx` from a copy that ends with a newline stores the newline. The script read it as is, `urllib` refused the header, and the error path printed: ``` posting failed: Invalid header value b'token <40 hex characters>\n' ``` `scrub` replaces the token string exactly, and here the message carried it with the newline escaped as `\n`, so nothing matched. The secret went to the console and to the log file. This is the one place where a secret must never surface, so it should be fixed before the tool becomes a rule: 1. `read_token` should `.strip()` what it gets (environment and registry): a token never contains whitespace, and a stray newline is the normal result of pasting into `setx`. 2. `scrub` should redact `token.strip()` too, and treat the escapes: replace the token with the whitespace around it, `token.replace(...)`, and keep the existing 40-hex-near-'token' rule as the net under it. 3. Worth a unit test with a token ending in `\n`: the header must be built from the stripped value and the message must not contain the original. Everything else about the run is good: merge clean, no game data, 38 sensitive files, and the summary was correct — it just could not post.
Sign in to join this conversation.
No description provided.