Skip to content

Commit 2a629dc

Browse files
authored
fix(testing): preserve producer JSON in CLI tuple results (#7654)
* fix(testing): preserve CLI JSON in tuple results Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> * test(testing): consume tuple CLI status without payload metadata Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --------- Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
1 parent d3f25ce commit 2a629dc

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎loopx/control_plane/testing/canary_harness.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def run_json_cli_result(
6565
runtime_root: Path | None = None,
6666
cwd: Path | None = None,
6767
) -> tuple[int, dict[str, Any]]:
68+
"""Return process status separately from the producer's unchanged JSON.
69+
70+
Callers can validate or replay the returned wire without test metadata
71+
changing content commitments or overwriting a producer-owned field.
72+
"""
6873
command = [
6974
sys.executable,
7075
"-m",
@@ -89,7 +94,6 @@ def run_json_cli_result(
8994
payload = json.loads(result.stdout)
9095
if not isinstance(payload, dict):
9196
raise AssertionError(f"expected JSON object for {command!r}: {payload!r}")
92-
payload["_returncode"] = result.returncode
9397
return result.returncode, payload
9498

9599

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Tuple-returning CLI transport preserves producer JSON, including unknown fields."""
2+
from __future__ import annotations
3+
4+
import hashlib
5+
import json
6+
import subprocess
7+
import sys
8+
from types import SimpleNamespace
9+
10+
import pytest
11+
12+
from loopx.control_plane.testing import canary_harness
13+
14+
15+
@pytest.mark.parametrize("exit_code", [0, 7])
16+
@pytest.mark.parametrize("producer_field", [False, True])
17+
def test_result_transport_does_not_inject_or_overwrite_source_fields(tmp_path, monkeypatch, exit_code, producer_field):
18+
source = {"number": 1.0, "unknown": {"allowed": False, "evidence": None}}
19+
if producer_field:
20+
source["_returncode"] = {"producer_fact": "Preserve this exact value."}
21+
raw = json.dumps(source, ensure_ascii=False)
22+
monkeypatch.setattr(canary_harness.subprocess, "run", lambda *args, **kwargs:
23+
SimpleNamespace(stdout=raw, stderr="", returncode=exit_code))
24+
status, observed = canary_harness.run_json_cli_result("probe", registry_path=tmp_path / "absent.json")
25+
assert status == exit_code
26+
assert json.dumps(observed, ensure_ascii=False) == raw
27+
28+
29+
@pytest.mark.parametrize("valid_digest", [False, True])
30+
def test_real_capture_cli_returns_the_same_wire_through_the_helper(tmp_path, monkeypatch, valid_digest):
31+
monkeypatch.setenv("LOOPX_USAGE_PING", "0")
32+
capture = tmp_path / "source.json"
33+
raw = b'{"goal":"Keep the original source and stop before publication.","value":1.0}'
34+
capture.write_bytes(raw)
35+
digest = hashlib.sha256(raw).hexdigest() if valid_digest else "0" * 64
36+
registry, runtime = tmp_path / "absent-registry.json", tmp_path / "absent-runtime"
37+
arguments = ["quota", "context-read", "--capture-file", str(capture), "--capture-sha256", digest]
38+
direct = subprocess.run([sys.executable, "-m", "loopx.cli", "--registry", str(registry),
39+
"--runtime-root", str(runtime), "--format", "json", *arguments],
40+
cwd=tmp_path, capture_output=True, text=True, check=False)
41+
status, observed = canary_harness.run_json_cli_result(*arguments,
42+
registry_path=registry, runtime_root=runtime, cwd=tmp_path)
43+
assert status == direct.returncode == (0 if valid_digest else 1)
44+
assert observed == json.loads(direct.stdout)
45+
assert capture.read_bytes() == raw
46+
assert not registry.exists() and not runtime.exists()

‎tests/control_plane/test_todo_source_runtime_route.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ def test_global_cli_check_and_manager_read_the_original_provider(routed):
7070
expected = list_goal_todos(registry_path=source, goal_id=GOAL, json_references=True)
7171
code, actual = run_json_cli_result("todo", "list", "--goal-id", GOAL, registry_path=registry)
7272
assert code == 0, actual
73-
assert actual.pop("_returncode") == 0 # Test harness metadata, outside the CLI payload.
7473
assert_todo_read_unchanged(actual, expected)
7574
assert actual["todo_count"] == 5
7675
assert {row["todo_id"] for row in actual["todos"]} == {f"todo_source_{i}" for i in range(5)}

0 commit comments

Comments
 (0)