File size: 5,412 Bytes
339ddd8
 
 
 
 
 
 
 
 
2a9dc4c
 
 
339ddd8
2a9dc4c
e1de134
339ddd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1de134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Tests for the external topology, node subprocesses, CLI, and gate script."""

from __future__ import annotations

import json
import subprocess
import sys
from pathlib import Path

from lingologos_codex.agents import ROLES
from lingologos_codex.decision import dossier_from_dict
from lingologos_codex.envelope import ENVELOPE_KIND

from lingologos_python import cli
from lingologos_python.session import PythonWorkspaceSession, SessionResult


PYTHON_ROOT = Path(__file__).resolve().parent.parent


def test_external_topology_json_and_node_folders_are_valid() -> None:
    topo = json.loads((PYTHON_ROOT / "topology.json").read_text(encoding="utf-8"))
    assert topo["sessionId"] == "lingologos-python-01"
    assert topo["terminationCondition"] == "FIXED_PIPELINE"
    assert sorted(inst["id"] for inst in topo["instances"]) == sorted(ROLES)

    for role in ROLES:
        node_dir = PYTHON_ROOT / "nodes" / role
        assert (node_dir / "grammar.gf").is_file()
        assert (node_dir / "ontology.smt2").is_file()
        seed = json.loads((node_dir / "seed_payload.json").read_text(encoding="utf-8"))
        assert seed["kind"] == ENVELOPE_KIND
        assert seed["sender"] == role
        dossier_from_dict(json.loads(seed["payload"]["abstractSyntaxTree"]))


def test_six_isolated_subprocess_nodes_verify_workspace(sample_pkg: Path) -> None:
    session = PythonWorkspaceSession(sample_pkg, use_subprocesses=True)
    res = session.verify("src/mathops.py")
    assert res.verdict == "VERIFIED"
    assert res.assurance == 3
    assert [env["sender"] for env in res.trace] == ["arbiter", *ROLES]


def test_python_agent_stdin_stdout_roundtrip(sample_pkg: Path) -> None:
    seed = (PYTHON_ROOT / "nodes" / "arbiter" / "seed_payload.json").read_text(encoding="utf-8")
    proc = subprocess.run(
        [
            sys.executable,
            "-B",
            str(PYTHON_ROOT / "agents" / "python_agent.py"),
            "--role",
            "planner",
            "--node-id",
            "planner",
        ],
        input=seed,
        capture_output=True,
        text=True,
        encoding="utf-8",
        check=True,
    )
    out = json.loads(proc.stdout)
    assert out["sender"] == "planner"
    assert out["turn"] == 1
    dossier = json.loads(out["payload"]["abstractSyntaxTree"])
    assert dossier["plan"]["order"][0] == "planner"


def test_cli_subcommands(sample_pkg: Path, capsys: object) -> None:
    assert cli.main(["check-adapter", "--workspace", str(sample_pkg)]) == 0
    assert cli.main(["symbols", "--workspace", str(sample_pkg), "--json"]) == 0
    assert (
        cli.main(
            [
                "select-tests",
                "--workspace",
                str(sample_pkg),
                "--files",
                "src/mathops.py",
                "--json",
            ]
        )
        == 0
    )
    assert cli.main(["verify", "--workspace", str(sample_pkg), "--file", "src/mathops.py"]) == 0
    assert (
        cli.main(
            [
                "edit",
                "--workspace",
                str(sample_pkg),
                "--file",
                "src/mathops.py",
                "--search",
                "return left + right",
                "--replace",
                "return int(left + right)",
            ]
        )
        == 0
    )


def test_the_cli_exits_non_zero_when_a_verified_write_never_landed(
    sample_pkg: Path, monkeypatch
) -> None:
    """The exit code reports the write, not the verdict.

    The session can verify a change in staging and then lose the commit to the
    disk. A command line that exits 0 on that run promises a file it did not
    write, so the process reports failure whenever committed is false outside a
    dry run.
    """

    def fake_create(self, rel_path, content, **kwargs) -> SessionResult:
        return SessionResult(
            case_id="python-create",
            verdict="VERIFIED",
            assurance=3,
            reason="OK_DUAL_CONFIRMATION",
            detail="the commit failed, the workspace was restored from the in-memory originals",
            committed=False,
        )

    def fake_apply(self, pattern_id, **kwargs) -> SessionResult:
        return SessionResult(
            case_id="python-pattern",
            verdict="VERIFIED",
            assurance=3,
            reason="OK_DUAL_CONFIRMATION",
            detail="the text on disk after the commit is not the verified text",
            committed=False,
        )

    content_file = sample_pkg / "newcomer.txt"
    content_file.write_text("VALUE = 1\n", encoding="utf-8")

    monkeypatch.setattr(PythonWorkspaceSession, "create_file", fake_create)
    assert (
        cli.main(
            [
                "create",
                "--workspace",
                str(sample_pkg),
                "--file",
                "src/newcomer.py",
                "--content-file",
                str(content_file),
            ]
        )
        == 1
    )

    monkeypatch.setattr(PythonWorkspaceSession, "apply_pattern", fake_apply)
    assert (
        cli.main(
            [
                "patterns",
                "apply",
                "--workspace",
                str(sample_pkg),
                "--pattern",
                "chain-to-registry",
                "--target",
                "src/mathops.py::double_sum",
            ]
        )
        == 1
    )