From 6ebc7002c279194fca90e21f7d7c0e7094969402 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:19:18 +0100 Subject: [PATCH 1/4] Accept check receipts across host environments Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 9 +++++---- src/loopzero/sandbox.py | 8 +++++--- tests/test_sandbox.py | 27 +++++++++++++++++---------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 49620cba..f8afff13 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -110,10 +110,11 @@ loading the delivery [commands](../src/loopzero/cli.py), which implement the con at that head with the current commands and check settings digest. A settings change or an older receipt without the digest requires a fresh check. The digest covers execution and stage reuse settings without recording raw - environment values. It excludes only inherited `HEAVY_SLOT_GRANTED_CORES` - for local sandbox receipts. Explicit settings, delegated runner environments - and stage reuse keys still bind the grant. Waiters check for policy changes - on every poll. + environment values. For local sandbox receipts it omits inherited `PATH`, + `LANG`, `LC_ALL`, `TERM`, and `HEAVY_SLOT_GRANTED_CORES` so readiness can + run in a different host environment. Explicit settings, delegated runner + environments, and stage reuse keys still bind these values. Waiters check + for policy changes on every poll. Local readiness and hosted eligibility carry a review forward only across a first-parent chain of verified merges from the configured PR base. They reconstruct each merge in an isolated Git repository and compare its tree diff --git a/src/loopzero/sandbox.py b/src/loopzero/sandbox.py index 6f725e28..731604d8 100644 --- a/src/loopzero/sandbox.py +++ b/src/loopzero/sandbox.py @@ -253,9 +253,11 @@ def settings_fingerprint(config: Config, worktree: Path, *, base: str | None = N settings = _execution_settings(config, base) if config.check_runner and settings["runner_executable"] is None: return None - if not config.check_runner and "HEAVY_SLOT_GRANTED_CORES" not in config.env: - # The inherited local worker limit is transient. Stage reuse still binds it. - settings["environment"].pop("HEAVY_SLOT_GRANTED_CORES", None) + if not config.check_runner: + # Inherited host settings can differ at readiness. Stage reuse still binds them. + host_env = {"PATH", "LANG", "LC_ALL", "TERM", "HEAVY_SLOT_GRANTED_CORES"} + for name in host_env - dict(config.env).keys(): + settings["environment"].pop(name, None) payload = [settings, config.inputs] return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest() diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index 674e4d8a..39910fd3 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -749,23 +749,25 @@ def cache(config, *, clean=False, verify_reuse=False): @pytest.mark.parametrize('change', ['other', 'input', 'shared', 'command', 'network', 'environment', 'version', 'failed', 'boolean_exit_code', 'dirty_receipt', 'keyless', - 'dirty_tree', 'legacy', 'grant']) -def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change): + 'dirty_tree', 'legacy', 'grant', 'host_environment']) +def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change, cache_check): cfg = make_config(checks=('echo one', 'echo two'), inputs={'echo one': ('*.md',), 'echo two': ('*.md',), '*': ('shared',)}) - if change == 'grant': - cfg = replace(cfg, env_allowlist=(*cfg.env_allowlist, "HEAVY_SLOT_GRANTED_CORES")) - monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "2") + original_path = os.environ["PATH"] + host_env = {"HEAVY_SLOT_GRANTED_CORES": "2"} + if change == 'host_environment': + host_env.update(PATH="/opt/tools:" + original_path, LANG="en_US.UTF-8", LC_ALL="en_US.UTF-8", TERM="dumb") + if change in {'grant', 'host_environment'}: + cfg = replace(cfg, env_allowlist=tuple(dict.fromkeys((*cfg.env_allowlist, *host_env)))) + for key, value in host_env.items(): + monkeypatch.setenv(key, value) for inputs in ({}, {'*': ()}, {'*': ('uv.lock',)}): assert sandbox._input_key(replace(cfg, inputs=inputs), git_repo, 'unlisted', fetched_base) is None whole = replace(cfg, inputs={'*': ('.',)}) key = sandbox._input_key(whole, git_repo, 'unlisted', fetched_base) assert key is not None - _save_report(git_repo, sandbox.run_checks(cfg, git_repo)) + cache_check(cfg) receipt = git_repo / '.loopzero/checks.json' - git(git_repo, 'config', 'core.excludesFile', str(git_repo / '.git/info/exclude')) - with (git_repo / '.git/info/exclude').open('a') as stream: - stream.write('\n.loopzero/\n') if change in {'other', 'input', 'shared', 'dirty_tree'}: path = 'README.md' if change in {'input', 'dirty_tree'} else 'src.py' if change == 'other' else change (git_repo / path).write_text('new content') @@ -780,7 +782,12 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch elif change == 'environment': monkeypatch.setenv('LZ_KEEP', 'changed') assert _current_check_report(git_repo, cfg, fetched_base) is None - elif change == 'grant': + elif change in {'grant', 'host_environment'}: + for key in host_env.keys() - {"PATH", "LANG", "HEAVY_SLOT_GRANTED_CORES"}: + monkeypatch.delenv(key) + if change == 'host_environment': + monkeypatch.setenv("PATH", original_path) + monkeypatch.setenv("LANG", "C") monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "4") assert _current_check_report(git_repo, cfg, fetched_base) is not None monkeypatch.delenv("HEAVY_SLOT_GRANTED_CORES") From 5c82dce740078e61938b23de0642c328d46ad75b Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:39:18 +0100 Subject: [PATCH 2/4] Exercise host portability at the ready entry point Co-Authored-By: Codex --- tests/test_cli.py | 14 +++++++------- tests/test_sandbox.py | 27 ++++++++++----------------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 5c136d5d..435939fa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3032,12 +3032,14 @@ def test_ready_matches_effective_inherited_check_environment(capsys, monkeypatch assert code == 1 and "not ready: check receipt has an incompatible settings fingerprint." in out -@pytest.mark.parametrize("worker", ["legacy", "different-path", "matching"]) +@pytest.mark.parametrize("worker", ["legacy", "different-environment", "matching"]) def test_ready_checks_transferred_whole_job_receipt(wt, gh, capsys, head, tmp_path, monkeypatch, worker): arm_approved_ready_pr(gh, head, wt, readiness={"local_exit": None}, isDraft=False) with monkeypatch.context() as worker_env: - if worker == "different-path": + if worker == "different-environment": worker_env.setenv("PATH", str(tmp_path / "worker-bin"), prepend=":") + for name, value in {"LANG": "en_US.UTF-8", "LC_ALL": "en_US.UTF-8", "TERM": "dumb"}.items(): + worker_env.setenv(name, value) assert run(capsys, "check")[0] == 0 receipt = wt / ".loopzero/checks.json" @@ -3045,11 +3047,9 @@ def test_ready_checks_transferred_whole_job_receipt(wt, gh, capsys, head, tmp_pa if worker == "legacy": # Older producers serialize these results without a settings fingerprint. del payload["settings_sha256"] - # Whole-job transport preserves the producer's fields. - receipt.write_text(json.dumps(payload)) - assert run(capsys, "ready")[0] == (0 if worker == "matching" else 1) - - if worker != "matching": + receipt.write_text(json.dumps(payload)) + assert run(capsys, "ready")[0] == int(worker == "legacy") + if worker == "legacy": assert run(capsys, "check")[0] == 0 assert run(capsys, "ready")[0] == 0 diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index 39910fd3..674e4d8a 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -749,25 +749,23 @@ def cache(config, *, clean=False, verify_reuse=False): @pytest.mark.parametrize('change', ['other', 'input', 'shared', 'command', 'network', 'environment', 'version', 'failed', 'boolean_exit_code', 'dirty_receipt', 'keyless', - 'dirty_tree', 'legacy', 'grant', 'host_environment']) -def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change, cache_check): + 'dirty_tree', 'legacy', 'grant']) +def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change): cfg = make_config(checks=('echo one', 'echo two'), inputs={'echo one': ('*.md',), 'echo two': ('*.md',), '*': ('shared',)}) - original_path = os.environ["PATH"] - host_env = {"HEAVY_SLOT_GRANTED_CORES": "2"} - if change == 'host_environment': - host_env.update(PATH="/opt/tools:" + original_path, LANG="en_US.UTF-8", LC_ALL="en_US.UTF-8", TERM="dumb") - if change in {'grant', 'host_environment'}: - cfg = replace(cfg, env_allowlist=tuple(dict.fromkeys((*cfg.env_allowlist, *host_env)))) - for key, value in host_env.items(): - monkeypatch.setenv(key, value) + if change == 'grant': + cfg = replace(cfg, env_allowlist=(*cfg.env_allowlist, "HEAVY_SLOT_GRANTED_CORES")) + monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "2") for inputs in ({}, {'*': ()}, {'*': ('uv.lock',)}): assert sandbox._input_key(replace(cfg, inputs=inputs), git_repo, 'unlisted', fetched_base) is None whole = replace(cfg, inputs={'*': ('.',)}) key = sandbox._input_key(whole, git_repo, 'unlisted', fetched_base) assert key is not None - cache_check(cfg) + _save_report(git_repo, sandbox.run_checks(cfg, git_repo)) receipt = git_repo / '.loopzero/checks.json' + git(git_repo, 'config', 'core.excludesFile', str(git_repo / '.git/info/exclude')) + with (git_repo / '.git/info/exclude').open('a') as stream: + stream.write('\n.loopzero/\n') if change in {'other', 'input', 'shared', 'dirty_tree'}: path = 'README.md' if change in {'input', 'dirty_tree'} else 'src.py' if change == 'other' else change (git_repo / path).write_text('new content') @@ -782,12 +780,7 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch elif change == 'environment': monkeypatch.setenv('LZ_KEEP', 'changed') assert _current_check_report(git_repo, cfg, fetched_base) is None - elif change in {'grant', 'host_environment'}: - for key in host_env.keys() - {"PATH", "LANG", "HEAVY_SLOT_GRANTED_CORES"}: - monkeypatch.delenv(key) - if change == 'host_environment': - monkeypatch.setenv("PATH", original_path) - monkeypatch.setenv("LANG", "C") + elif change == 'grant': monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "4") assert _current_check_report(git_repo, cfg, fetched_base) is not None monkeypatch.delenv("HEAVY_SLOT_GRANTED_CORES") From 486f9b78a22fe141b11af187ffc88780f7d5ec5c Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:40:48 +0100 Subject: [PATCH 3/4] Keep coverage for rerunning stages across worker environments Co-Authored-By: Codex --- tests/test_sandbox.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index 674e4d8a..39910fd3 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -749,23 +749,25 @@ def cache(config, *, clean=False, verify_reuse=False): @pytest.mark.parametrize('change', ['other', 'input', 'shared', 'command', 'network', 'environment', 'version', 'failed', 'boolean_exit_code', 'dirty_receipt', 'keyless', - 'dirty_tree', 'legacy', 'grant']) -def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change): + 'dirty_tree', 'legacy', 'grant', 'host_environment']) +def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change, cache_check): cfg = make_config(checks=('echo one', 'echo two'), inputs={'echo one': ('*.md',), 'echo two': ('*.md',), '*': ('shared',)}) - if change == 'grant': - cfg = replace(cfg, env_allowlist=(*cfg.env_allowlist, "HEAVY_SLOT_GRANTED_CORES")) - monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "2") + original_path = os.environ["PATH"] + host_env = {"HEAVY_SLOT_GRANTED_CORES": "2"} + if change == 'host_environment': + host_env.update(PATH="/opt/tools:" + original_path, LANG="en_US.UTF-8", LC_ALL="en_US.UTF-8", TERM="dumb") + if change in {'grant', 'host_environment'}: + cfg = replace(cfg, env_allowlist=tuple(dict.fromkeys((*cfg.env_allowlist, *host_env)))) + for key, value in host_env.items(): + monkeypatch.setenv(key, value) for inputs in ({}, {'*': ()}, {'*': ('uv.lock',)}): assert sandbox._input_key(replace(cfg, inputs=inputs), git_repo, 'unlisted', fetched_base) is None whole = replace(cfg, inputs={'*': ('.',)}) key = sandbox._input_key(whole, git_repo, 'unlisted', fetched_base) assert key is not None - _save_report(git_repo, sandbox.run_checks(cfg, git_repo)) + cache_check(cfg) receipt = git_repo / '.loopzero/checks.json' - git(git_repo, 'config', 'core.excludesFile', str(git_repo / '.git/info/exclude')) - with (git_repo / '.git/info/exclude').open('a') as stream: - stream.write('\n.loopzero/\n') if change in {'other', 'input', 'shared', 'dirty_tree'}: path = 'README.md' if change in {'input', 'dirty_tree'} else 'src.py' if change == 'other' else change (git_repo / path).write_text('new content') @@ -780,7 +782,12 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch elif change == 'environment': monkeypatch.setenv('LZ_KEEP', 'changed') assert _current_check_report(git_repo, cfg, fetched_base) is None - elif change == 'grant': + elif change in {'grant', 'host_environment'}: + for key in host_env.keys() - {"PATH", "LANG", "HEAVY_SLOT_GRANTED_CORES"}: + monkeypatch.delenv(key) + if change == 'host_environment': + monkeypatch.setenv("PATH", original_path) + monkeypatch.setenv("LANG", "C") monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "4") assert _current_check_report(git_repo, cfg, fetched_base) is not None monkeypatch.delenv("HEAVY_SLOT_GRANTED_CORES") From cadcc8d068cb5099e44f2117507d21ae0e7445c9 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:50:13 +0100 Subject: [PATCH 4/4] Prove receipt portability and stage reuse in the same check Co-Authored-By: Codex --- tests/test_cli.py | 17 ++++++++++------- tests/test_sandbox.py | 27 ++++++++++----------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 435939fa..b9a3c3f4 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3034,24 +3034,27 @@ def test_ready_matches_effective_inherited_check_environment(capsys, monkeypatch @pytest.mark.parametrize("worker", ["legacy", "different-environment", "matching"]) def test_ready_checks_transferred_whole_job_receipt(wt, gh, capsys, head, tmp_path, monkeypatch, worker): + policy = WORKFLOW.replace( + '[checks]', '[checks]\nenv_allowlist = ["PATH", "LANG", "LC_ALL", "TERM", "HEAVY_SLOT_GRANTED_CORES"]') + publish_workflow(wt, policy.replace('[delivery]', '[checks.inputs]\n"*" = ["."]\n\n[delivery]')) arm_approved_ready_pr(gh, head, wt, readiness={"local_exit": None}, isDraft=False) with monkeypatch.context() as worker_env: if worker == "different-environment": worker_env.setenv("PATH", str(tmp_path / "worker-bin"), prepend=":") - for name, value in {"LANG": "en_US.UTF-8", "LC_ALL": "en_US.UTF-8", "TERM": "dumb"}.items(): + for name, value in {"LANG": "en_US.UTF-8", "LC_ALL": "en_US.UTF-8", + "TERM": "dumb", "HEAVY_SLOT_GRANTED_CORES": "2"}.items(): worker_env.setenv(name, value) assert run(capsys, "check")[0] == 0 - receipt = wt / ".loopzero/checks.json" - payload = json.loads(receipt.read_bytes()) if worker == "legacy": - # Older producers serialize these results without a settings fingerprint. + receipt = wt / ".loopzero/checks.json" + payload = json.loads(receipt.read_bytes()) del payload["settings_sha256"] receipt.write_text(json.dumps(payload)) assert run(capsys, "ready")[0] == int(worker == "legacy") - if worker == "legacy": - assert run(capsys, "check")[0] == 0 - assert run(capsys, "ready")[0] == 0 + assert run(capsys, "check")[0] == 0 + assert all(bool(stage.reused_from) == (worker == "matching") for stage in cli._load_report(wt).results) + assert run(capsys, "ready")[0] == 0 def test_ready_ignores_forged_markers(wt: Path, gh: FakeGh, capsys, head) -> None: diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index 39910fd3..674e4d8a 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -749,25 +749,23 @@ def cache(config, *, clean=False, verify_reuse=False): @pytest.mark.parametrize('change', ['other', 'input', 'shared', 'command', 'network', 'environment', 'version', 'failed', 'boolean_exit_code', 'dirty_receipt', 'keyless', - 'dirty_tree', 'legacy', 'grant', 'host_environment']) -def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change, cache_check): + 'dirty_tree', 'legacy', 'grant']) +def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, change): cfg = make_config(checks=('echo one', 'echo two'), inputs={'echo one': ('*.md',), 'echo two': ('*.md',), '*': ('shared',)}) - original_path = os.environ["PATH"] - host_env = {"HEAVY_SLOT_GRANTED_CORES": "2"} - if change == 'host_environment': - host_env.update(PATH="/opt/tools:" + original_path, LANG="en_US.UTF-8", LC_ALL="en_US.UTF-8", TERM="dumb") - if change in {'grant', 'host_environment'}: - cfg = replace(cfg, env_allowlist=tuple(dict.fromkeys((*cfg.env_allowlist, *host_env)))) - for key, value in host_env.items(): - monkeypatch.setenv(key, value) + if change == 'grant': + cfg = replace(cfg, env_allowlist=(*cfg.env_allowlist, "HEAVY_SLOT_GRANTED_CORES")) + monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "2") for inputs in ({}, {'*': ()}, {'*': ('uv.lock',)}): assert sandbox._input_key(replace(cfg, inputs=inputs), git_repo, 'unlisted', fetched_base) is None whole = replace(cfg, inputs={'*': ('.',)}) key = sandbox._input_key(whole, git_repo, 'unlisted', fetched_base) assert key is not None - cache_check(cfg) + _save_report(git_repo, sandbox.run_checks(cfg, git_repo)) receipt = git_repo / '.loopzero/checks.json' + git(git_repo, 'config', 'core.excludesFile', str(git_repo / '.git/info/exclude')) + with (git_repo / '.git/info/exclude').open('a') as stream: + stream.write('\n.loopzero/\n') if change in {'other', 'input', 'shared', 'dirty_tree'}: path = 'README.md' if change in {'input', 'dirty_tree'} else 'src.py' if change == 'other' else change (git_repo / path).write_text('new content') @@ -782,12 +780,7 @@ def test_declared_input_reuse(git_repo, bwrap_log, monkeypatch, fetched_base, ch elif change == 'environment': monkeypatch.setenv('LZ_KEEP', 'changed') assert _current_check_report(git_repo, cfg, fetched_base) is None - elif change in {'grant', 'host_environment'}: - for key in host_env.keys() - {"PATH", "LANG", "HEAVY_SLOT_GRANTED_CORES"}: - monkeypatch.delenv(key) - if change == 'host_environment': - monkeypatch.setenv("PATH", original_path) - monkeypatch.setenv("LANG", "C") + elif change == 'grant': monkeypatch.setenv("HEAVY_SLOT_GRANTED_CORES", "4") assert _current_check_report(git_repo, cfg, fetched_base) is not None monkeypatch.delenv("HEAVY_SLOT_GRANTED_CORES")