From facacd83d89df63318f6ee431ca63646a0953286 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 08:22:03 +0800 Subject: [PATCH] Prepare the CLI budget baseline with a private Git index Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- ...cli-output-base-head-differential-smoke.py | 125 +++++++++--------- .../test_cli_output_differential.py | 80 ++++++++--- 2 files changed, 122 insertions(+), 83 deletions(-) diff --git a/examples/control_plane/cli-output-base-head-differential-smoke.py b/examples/control_plane/cli-output-base-head-differential-smoke.py index a45d34a129..49f94601d0 100644 --- a/examples/control_plane/cli-output-base-head-differential-smoke.py +++ b/examples/control_plane/cli-output-base-head-differential-smoke.py @@ -33,25 +33,50 @@ ) -def _run(command: list[str], *, cwd: Path, env: dict[str, str] | None = None) -> None: +def _run( + command: list[str], *, cwd: Path, env: dict[str, str] | None = None, + input_bytes: bytes | None = None, +) -> bytes: completed = subprocess.run( command, cwd=cwd, env=env, + input=input_bytes, check=False, - text=True, - encoding="utf-8", - errors="replace", stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=240, ) if completed.returncode != 0: + stdout = completed.stdout.decode("utf-8", errors="replace") + stderr = completed.stderr.decode("utf-8", errors="replace") raise AssertionError( f"command failed ({completed.returncode}): {' '.join(command)}\n" - f"stdout:\n{completed.stdout[-4000:]}\n" - f"stderr:\n{completed.stderr[-4000:]}" + f"stdout:\n{stdout[-4000:]}\n" + f"stderr:\n{stderr[-4000:]}" ) + return completed.stdout + + +def _materialize_base_source(base_ref: str, base_root: Path) -> None: + """Export the pinned CLI source and its resources using a private Git index.""" + env = {**os.environ, "GIT_INDEX_FILE": str(base_root.with_suffix(".index"))} + _run(["git", "read-tree", base_ref], cwd=REPO_ROOT, env=env) + # Root files include build manifests. The probe itself comes from the + # candidate, so docs, test fixtures and other product trees are unnecessary. + paths = _run( + ["git", "ls-files", "-z", "--", ":(top,glob)*", "loopx/", "packages/", "scripts/", "skills/"], + cwd=REPO_ROOT, env=env, + ) + if b"loopx/__init__.py" not in paths.split(b"\0"): + raise ValueError(f"CLI comparison base {base_ref} has no LoopX Python source") + base_root.mkdir() + # Resolve checkout attributes from the historical index. + _run( + ["git", f"--work-tree={base_root}", "checkout-index", + f"--prefix={base_root.as_posix()}/", "--stdin", "-z"], + cwd=REPO_ROOT, env=env, input_bytes=paths, + ) def _run_probe( @@ -227,65 +252,39 @@ def main() -> int: shutil.copy2(FIXTURE_SUPPORT, temp_root / FIXTURE_SUPPORT.name) shutil.copy2(SEMANTICS_SOURCE, semantics_source) - _run( - [ - "git", - "-c", - f"core.hooksPath={os.devnull}", - "worktree", - "add", - "--detach", - str(base_root), - base_ref, - ], - cwd=REPO_ROOT, + _materialize_base_source(base_ref, base_root) + _run_probe( + source_root=base_root, + probe_runner=probe_runner, + probe_test=probe_test, + semantics_source=semantics_source, + fixture_root=fixture_root, + receipt_path=base_receipt, + cwd=temp_root, + enforce_budget=False, ) - try: - _run_probe( - source_root=base_root, - probe_runner=probe_runner, - probe_test=probe_test, - semantics_source=semantics_source, - fixture_root=fixture_root, - receipt_path=base_receipt, - cwd=temp_root, - enforce_budget=False, - ) - base_observation = _base_ceiling_observation(_load_receipt(base_receipt)) - if base_observation["failure_count"]: - # Candidate assertions still run unchanged. These metrics are - # attribution evidence, not a waiver for an over-budget base. - print( - "cli-output-base-current-ceilings " - + json.dumps( - {"base_ref": base_ref, **base_observation}, - sort_keys=True, separators=(",", ":"), - ), - flush=True, - ) - _run_probe( - source_root=REPO_ROOT, - probe_runner=probe_runner, - probe_test=probe_test, - semantics_source=semantics_source, - fixture_root=fixture_root, - receipt_path=candidate_receipt, - cwd=temp_root, - enforce_budget=True, - ) - finally: - _run( - [ - "git", - "-c", - f"core.hooksPath={os.devnull}", - "worktree", - "remove", - "--force", - str(base_root), - ], - cwd=REPO_ROOT, + base_observation = _base_ceiling_observation(_load_receipt(base_receipt)) + if base_observation["failure_count"]: + # Candidate assertions still run unchanged. These metrics are + # attribution evidence, not a waiver for an over-budget base. + print( + "cli-output-base-current-ceilings " + + json.dumps( + {"base_ref": base_ref, **base_observation}, + sort_keys=True, separators=(",", ":"), + ), + flush=True, ) + _run_probe( + source_root=REPO_ROOT, + probe_runner=probe_runner, + probe_test=probe_test, + semantics_source=semantics_source, + fixture_root=fixture_root, + receipt_path=candidate_receipt, + cwd=temp_root, + enforce_budget=True, + ) comparison = compare_cli_output_receipts( _load_receipt(base_receipt), diff --git a/tests/control_plane/test_cli_output_differential.py b/tests/control_plane/test_cli_output_differential.py index d5cff98a04..8be8bb6f35 100644 --- a/tests/control_plane/test_cli_output_differential.py +++ b/tests/control_plane/test_cli_output_differential.py @@ -320,15 +320,14 @@ def test_over_budget_base_is_reported_before_candidate_failure_without_waiver( row_id="surface/quota_should_run/crowded/json", surface_id="quota_should_run", scenario="crowded", chars=chars, lines=1, ) - probes, git_commands, workloads = [], [], [] + probes, materialized, workloads = [], [], [] fixture_bytes = namespace["PROBE_TEST"].read_bytes() - def git_transport(command, **_): - git_commands.append(command) - if "add" in command: - base_test = Path(command[-2]) / namespace["PROBE_TEST"].relative_to(namespace["REPO_ROOT"]) - base_test.parent.mkdir(parents=True) - base_test.write_text("# Synthetic historical fixture\n") + def materialize(base_ref, base_root): + materialized.append((base_ref, base_root)) + base_test = base_root / namespace["PROBE_TEST"].relative_to(namespace["REPO_ROOT"]) + base_test.parent.mkdir(parents=True) + base_test.write_text("# Synthetic historical fixture\n") def probe_transport(**kwargs): probes.append(kwargs["enforce_budget"]) @@ -343,7 +342,7 @@ def probe_transport(**kwargs): measurement={"chars": chars, "lines": 1}, ) - monkeypatch.setitem(namespace, "_run", git_transport) + monkeypatch.setitem(namespace, "_materialize_base_source", materialize) monkeypatch.setitem(namespace, "_run_probe", probe_transport) monkeypatch.setitem(namespace, "_is_ancestor", lambda *_: True) monkeypatch.setitem(namespace, "_pin_probe_base_ref", lambda ref, **_: ref) @@ -354,7 +353,8 @@ def probe_transport(**kwargs): assert workloads[0][0] == workloads[1][0] assert all(body == fixture_bytes for _, body, _ in workloads) assert workloads[0][2] != workloads[1][2] - assert "remove" in git_commands[-1] + assert materialized == [("synthetic-base", workloads[0][2])] + assert not workloads[0][2].exists() output = capsys.readouterr().out prefix = "cli-output-base-current-ceilings " assert output.startswith(prefix) @@ -709,12 +709,18 @@ def git(*args): probe_test = repo / "tests" / "probe.py" probe_test.parent.mkdir() probe_test.write_text("# Public synthetic probe\n", encoding="utf-8") - git("add", "packet.json", "tests/probe.py") + base_package = repo / "loopx" / "__init__.py" + base_package.parent.mkdir() + base_package.write_bytes(b"# Fixture package\n") + attributes = repo / ".gitattributes" + attributes.write_bytes(b"*.py text eol=lf\n") + git("add", ".gitattributes", "packet.json", "tests/probe.py", "loopx/__init__.py") git("commit", "-m", "common baseline") common = git("rev-parse", "HEAD") git("checkout", "-b", "candidate") + attributes.write_bytes(b"*.py text eol=crlf\n") packet.write_text(json.dumps({**common_payload, "note": "x" * (4000 + growth)}), encoding="utf-8") - git("add", "packet.json") + git("add", ".gitattributes", "packet.json") git("commit", "--allow-empty", "-m", "candidate") candidate = git("rev-parse", "HEAD") git("checkout", "main") @@ -725,19 +731,28 @@ def git(*args): upstream = git("rev-parse", "HEAD") git("update-ref", "refs/remotes/origin/main", upstream) git("checkout", "candidate") + (repo / "staged.txt").write_text("Uncommitted fixture change\n", encoding="utf-8") + git("add", "staged.txt") + original_index = git("ls-files", "--stage") probes = [] pinned = [] - run = namespace["_run"] + materialize = namespace["_materialize_base_source"] - def git_transport(command, **kwargs): - if "worktree" in command and "add" in command: - pinned.append(command[-1]) - # Advance the ref again after selection but before checkout. - git("update-ref", "refs/remotes/origin/main", candidate) - run(command, **kwargs) + def observe_materialization(base_ref, base_root): + pinned.append(base_ref) + # Advance the ref again after selection but before materialization. + git("update-ref", "refs/remotes/origin/main", candidate) + materialize(base_ref, base_root) def probe_transport(**kwargs): - text = (kwargs["source_root"] / "packet.json").read_text(encoding="utf-8") + source_root = kwargs["source_root"] + if not kwargs["enforce_budget"]: + assert (source_root / "loopx" / "__init__.py").read_bytes() == b"# Fixture package\n" + assert not (source_root / "tests").exists() + assert not (source_root / "staged.txt").exists() + assert not (source_root / ".git").exists() + assert git("worktree", "list", "--porcelain").count("worktree ") == 1 + text = (source_root / "packet.json").read_text(encoding="utf-8") measured = measure_cli_output(text, output_format="json") row = _row( row_id="surface/quota_should_run/crowded/json", @@ -756,7 +771,7 @@ def probe_transport(**kwargs): monkeypatch.setitem(namespace, "REPO_ROOT", repo) monkeypatch.setitem(namespace, "PROBE_TEST", probe_test) - monkeypatch.setitem(namespace, "_run", git_transport) + monkeypatch.setitem(namespace, "_materialize_base_source", observe_materialization) monkeypatch.setitem(namespace, "_run_probe", probe_transport) # An explicit historical commit remains authoritative even off this branch. args = ["probe", "--base-ref", upstream] if explicit_base else ["probe"] @@ -772,6 +787,7 @@ def probe_transport(**kwargs): assert pinned == [upstream if explicit_base else common] assert git("worktree", "list", "--porcelain").count("worktree ") == 1 assert git("rev-parse", "HEAD") == candidate + assert git("ls-files", "--stage") == original_index assert git("show", f"{common}:packet.json") == json.dumps(common_payload) @@ -791,6 +807,30 @@ def test_probe_rejects_an_unresolvable_base(tmp_path, monkeypatch, base_ref): pin(base_ref, main_ref="origin/main") +def test_probe_rejects_a_commit_without_its_own_cli_source(tmp_path, monkeypatch): + import os + import runpy + import subprocess + from pathlib import Path + + materialize = runpy.run_path(str( + Path(__file__).resolve().parents[2] + / "examples/control_plane/cli-output-base-head-differential-smoke.py" + ))["_materialize_base_source"] + repo = tmp_path / "repo" + subprocess.run(["git", "init", str(repo)], check=True, capture_output=True) + subprocess.run([ + "git", "-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid", + "-c", f"core.hooksPath={os.devnull}", "-c", "commit.gpgsign=false", + "commit", "--allow-empty", "-m", "Commit before the CLI existed", + ], cwd=repo, check=True, capture_output=True) + monkeypatch.setitem(materialize.__globals__, "REPO_ROOT", repo) + source_root = tmp_path / "base" + with pytest.raises(ValueError, match="has no LoopX Python source"): + materialize("HEAD", source_root) + assert not source_root.exists() + + @pytest.mark.parametrize("row_kind", ["surface", "variant"]) @pytest.mark.parametrize("mode", ["thin", "brief", "compact"]) def test_host_safety_restoration_budget_is_one_time_bounded_and_prompt_only(