Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ jobs:
--basetemp "${{ runner.temp }}/lxb-${{ github.run_id }}-${{ github.run_attempt }}"
tests/control_plane/test_cli_output_budget.py
tests/control_plane/test_cli_output_differential.py
-k "stable_budget_fixture or long_path_budget or probe_process_failure_preserves_utf8_diagnostics"
-k "stable_budget_fixture or long_path_budget or probe_process_failure_preserves_utf8_diagnostics or pinned_reader_follows"

- name: Qualify the native Windows CLI output differential
timeout-minutes: 12
Expand Down
31 changes: 30 additions & 1 deletion tests/control_plane/cli_output_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,37 @@
import json
import os
from pathlib import Path
import re
import shlex
import shutil
import subprocess
import tempfile


_POWERSHELL_READER_WORDS = re.compile(r"'(?:''|[^'])*'|[^'\s]+")
_POWERSHELL_DOUBLED_QUOTE = re.compile(r"(['\u2018\u2019\u201a\u201b])\1")
_DIRECT_POWERSHELL_READER = re.compile(r"^([A-Za-z0-9_./-]+) '")


def _reader_argv(command: str) -> list[str]:
"""Decode the literal-only form of the generated read command into argv."""
direct = _DIRECT_POWERSHELL_READER.match(command)
if not command.startswith("& ") and not (
direct and (os.name == "nt" or direct[1].lower().endswith(".ps1"))
):
return shlex.split(command)
# The host renderer uses '& ' only for a quoted executable. It also
# selects PowerShell for .ps1 on POSIX. shlex drops its doubled quotes.
literal_command = command.removeprefix("& ")
words = _POWERSHELL_READER_WORDS.findall(literal_command)
assert " ".join(words) == literal_command, "unexpected syntax in the generated reader command"
return [
_POWERSHELL_DOUBLED_QUOTE.sub(r"\1", word[1:-1])
if word.startswith("'") and word.endswith("'") else word
for word in words
]


def budget_fixture_path(root: Path) -> Path:
suffix = hashlib.sha256(str(root).encode("utf-8")).hexdigest()[:12]
if os.name == "nt":
Expand Down Expand Up @@ -61,7 +86,7 @@ def read_pinned_cli_body(command: str) -> tuple[str, int, int]:
reader_bytes = calls = 0
identity = total = digest = None
while True:
words = shlex.split(command)
words = _reader_argv(command)
quota = words.index("quota")
if words[quota + 1] != "context-read":
raise AssertionError("context recovery must be a read, never another guard")
Expand All @@ -75,6 +100,10 @@ def read_pinned_cli_body(command: str) -> tuple[str, int, int]:
if identity is None:
identity = scope
assert scope == identity, ("context continuation changed its source or reader route", scope, identity)
if words[0].lower().endswith(".ps1"):
from loopx.command_invocation import command_argv

words = command_argv(Path(words[0]), words[1:])
result = subprocess.run(words, capture_output=True, text=True, encoding="utf-8",
errors="strict", check=False, timeout=30)
assert result.returncode == 0, result.stdout + result.stderr
Expand Down
69 changes: 69 additions & 0 deletions tests/control_plane/test_cli_output_differential.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import json
import shutil
import sys

import pytest
Expand All @@ -24,6 +25,74 @@
)


def test_pinned_reader_follows_a_powershell_command_with_literal_paths(tmp_path, monkeypatch):
import hashlib
import os
from pathlib import Path

from loopx import install_contract
from tests.control_plane.cli_output_fixture import read_pinned_cli_body

executable = Path(sys.executable)
if os.name != "nt":
# A path requiring PowerShell's call operator reproduces the native
# command shape even when the reader itself runs on POSIX.
launcher = tmp_path / "test Python"
launcher.symlink_to(executable)
executable = launcher
repository = Path(__file__).resolve().parents[2]
monkeypatch.setenv("PYTHONPATH", str(repository))
monkeypatch.setattr(install_contract, "_is_windows_host", lambda: True)
source = tmp_path / "owner's ‘saved capture’.json"
payload = {"requirements": "保留原要求和停止条件。🙂" * 40, "turn": "original"}
raw = json.dumps(payload, ensure_ascii=False).encode()
source.write_bytes(raw)
command = install_contract.render_host_cli_command([
str(executable), "-m", "loopx.entrypoint", "--format", "json",
"quota", "context-read", "--capture-file", str(source),
"--capture-sha256", hashlib.sha256(raw).hexdigest(),
"--page-bytes", "256", "--max-page-output-bytes", "4000",
])
assert command.startswith("& ")

body, calls, reader_bytes = read_pinned_cli_body(command)
assert body.encode() == source.read_bytes() == raw
assert calls > 1 and reader_bytes > len(raw)


def test_pinned_reader_follows_a_relative_powershell_launcher(tmp_path, monkeypatch):
import hashlib
import os
from pathlib import Path

from loopx.command_invocation import loopx_cli_argv
from loopx.control_plane.turn_driver.decision_capture import context_capture_read_command
from tests.control_plane.cli_output_fixture import read_pinned_cli_body

if os.name != "nt" and shutil.which("pwsh") is None:
pytest.skip("Requires PowerShell 7")
repository = Path(__file__).resolve().parents[2]
monkeypatch.chdir(tmp_path)
launcher_arg = "./bin/loopx.ps1"
launcher = tmp_path / launcher_arg
launcher.parent.mkdir()
shutil.copyfile(repository / "scripts" / "loopx.ps1", launcher)
pointer = tmp_path / "release.json"
pointer.write_text(json.dumps({"release_root": str(repository), "python": sys.executable}), encoding="utf-8")
monkeypatch.setenv("LOOPX_CURRENT_RELEASE_FILE", str(pointer))
source = tmp_path / "owner's ‘saved capture’.json"
raw = json.dumps({"requirements": "完整的验收条件🙂" * 30}, ensure_ascii=False).encode("utf-8")
source.write_bytes(raw)
digest = hashlib.sha256(raw).hexdigest()
route = loopx_cli_argv(launcher_arg)
command = context_capture_read_command(source, digest, reader_argv=route,
page_bytes=256, max_page_output_bytes=4000)
assert command.startswith(launcher_arg + " '")

body, calls, reader_bytes = read_pinned_cli_body(command)
assert body.encode("utf-8") == raw and calls > 1 and reader_bytes > len(raw)


def test_probe_process_failure_preserves_utf8_diagnostics(tmp_path):
import runpy
from pathlib import Path
Expand Down
2 changes: 1 addition & 1 deletion tests/test_python_ci_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def test_windows_lifecycle_suite_references_existing_tests() -> None:
assert "tests/control_plane/test_cli_output_differential.py" in args
assert set(args[args.index("-k") + 1].split(" or ")) == {
"stable_budget_fixture", "long_path_budget",
"probe_process_failure_preserves_utf8_diagnostics",
"probe_process_failure_preserves_utf8_diagnostics", "pinned_reader_follows",
}


Expand Down
Loading