File size: 9,874 Bytes
a0691ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
"""Slice-3 stdio smoke: real MCP client against isolated HAI_HOME.

Writes a project-local artifact under evals/stdio_smoke/.
Never touches ~/.hai or global Cursor MCP config.
"""

from __future__ import annotations

import argparse
import asyncio
import json
import os
import sys
import tempfile
import time
from pathlib import Path
from typing import Any

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

REPO = Path(__file__).resolve().parents[1]
ARTIFACT_DIR = REPO / "evals" / "stdio_smoke"

EXPECTED_TOOLS = frozenset(
    {
        "hai_health",
        "hai_status",
        "hai_get_next_step",
        "hai_read_artifacts",
        "hai_park",
        "hai_set_focus",
        "hai_propose_next_step",
        "hai_accept_next_step",
        "hai_checkpoint",
        "hai_recover",
        "hai_open_mission",
        "hai_bind_project",
        "hai_authorize_session",
        "hai_get_contract",
        "hai_check_activity",
        "hai_park_item",
        "hai_recontract",
        "hai_close_mission",
        "hai_intake",
        "hai_distill",
        "hai_mission_start",
        "hai_drift_check",
        "hai_proof",
        "hai_stop",
    }
)


async def _call(session: ClientSession, name: str, arguments: dict[str, Any]) -> dict[str, Any]:
    result = await session.call_tool(name, arguments)
    text = result.content[0].text if result.content else "{}"
    return json.loads(text)


async def run_smoke(*, hai_home: Path, project: Path) -> dict[str, Any]:
    steps: list[dict[str, Any]] = []
    env = {**os.environ, "HAI_HOME": str(hai_home)}
    command = "uv"
    args = ["run", "--directory", str(REPO), "hai-mcp"]
    params = StdioServerParameters(command=command, args=args, env=env)

    started = time.time()
    async with stdio_client(params) as (read, write):
        async with ClientSession(read, write) as session:
            init = await session.initialize()
            steps.append(
                {
                    "step": "initialize",
                    "ok": True,
                    "server": getattr(getattr(init, "serverInfo", None), "name", None),
                }
            )

            listed = await session.list_tools()
            names = sorted(t.name for t in listed.tools)
            missing = sorted(EXPECTED_TOOLS - set(names))
            extra = sorted(set(names) - EXPECTED_TOOLS)
            steps.append(
                {
                    "step": "list_tools",
                    "ok": not missing,
                    "tool_count": len(names),
                    "tools": names,
                    "missing": missing,
                    "extra": extra,
                }
            )

            health = await _call(session, "hai_health", {})
            steps.append(
                {
                    "step": "hai_health",
                    "ok": health.get("ok") is True and health.get("hai_home") == str(hai_home),
                    "response": health,
                }
            )

            # Gated fail-closed: integer owner_ack must not promote
            (project / "Projek-Managment").mkdir(parents=True, exist_ok=True)
            await _call(
                session,
                "hai_propose_next_step",
                {"project_path": str(project), "content": "# smoke gate\n"},
            )
            denied = await _call(
                session,
                "hai_accept_next_step",
                {"project_path": str(project), "owner_ack": 1, "reason": "smoke truthy int"},
            )
            steps.append(
                {
                    "step": "hai_accept_next_step_owner_ack_int",
                    "ok": denied.get("ok") is False and denied.get("error") == "owner_gate_required",
                    "response": denied,
                }
            )

            # Lifecycle: mission_start -> authorize -> drift (outside path)
            opened = await _call(
                session,
                "hai_mission_start",
                {
                    "problem": "write a single smoke note into out.md",
                    "artifact": "out.md",
                    "done_criteria": [
                        {"id": "c1", "description": "out.md exists", "evidence": "file"}
                    ],
                    "owner": "smoke",
                    "time_limit_hours": 1,
                    "constraints": {
                        "project_path": str(project),
                        "allowed_paths": ["."],
                        "capabilities": ["read", "write"],
                    },
                },
            )
            mid = opened.get("mission_id")
            ver = opened.get("contract_version")
            steps.append(
                {
                    "step": "hai_mission_start",
                    "ok": opened.get("ok") is True and opened.get("status") == "active" and bool(mid),
                    "response": {
                        k: opened.get(k)
                        for k in ("ok", "status", "mission_id", "contract_version", "error", "issues")
                    },
                }
            )

            auth: dict[str, Any] = {"ok": False}
            drift: dict[str, Any] = {"ok": False}
            if mid and ver is not None:
                auth = await _call(
                    session,
                    "hai_authorize_session",
                    {
                        "mission_id": mid,
                        "contract_version": ver,
                        "agent_identity": "smoke-agent",
                        "role": "coder",
                        "contribution": "smoke write",
                        "expected_result": "out.md",
                        "duration_minutes": 30,
                        "criterion_ids": ["c1"],
                        "capabilities": ["read", "write"],
                    },
                )
                steps.append(
                    {
                        "step": "hai_authorize_session",
                        "ok": auth.get("ok") is True and bool(auth.get("session_id")),
                        "response": {
                            k: auth.get(k) for k in ("ok", "session_id", "error", "message")
                        },
                    }
                )
                sid = auth.get("session_id")
                if sid:
                    drift = await _call(
                        session,
                        "hai_drift_check",
                        {
                            "session_id": sid,
                            "activity_step": "touching secrets",
                            "criterion_id": "c1",
                            "affected_paths": ["/etc/passwd"],
                            "declares_blocker": True,
                        },
                    )
                    steps.append(
                        {
                            "step": "hai_drift_check_outside_path",
                            "ok": (
                                drift.get("classification") == "drift"
                                and drift.get("required_action") == "stop"
                            ),
                            "response": {
                                k: drift.get(k)
                                for k in (
                                    "ok",
                                    "classification",
                                    "required_action",
                                    "reason",
                                    "error",
                                )
                            },
                        }
                    )

    elapsed_ms = int((time.time() - started) * 1000)
    all_ok = all(s.get("ok") for s in steps)
    return {
        "ok": all_ok,
        "exit_code": 0 if all_ok else 1,
        "elapsed_ms": elapsed_ms,
        "command": [command, *args],
        "env_boundary": {
            "HAI_HOME": str(hai_home),
            "project_path": str(project),
            "cwd": str(REPO),
            "touches_live_dot_hai": False,
        },
        "steps": steps,
    }


def write_artifact(payload: dict[str, Any]) -> Path:
    ARTIFACT_DIR.mkdir(parents=True, exist_ok=True)
    stamp = time.strftime("%Y%m%dT%H%M%S")
    stamped = ARTIFACT_DIR / f"smoke-{stamp}.json"
    latest = ARTIFACT_DIR / "latest.json"
    text = json.dumps(payload, indent=2, ensure_ascii=False, sort_keys=True) + "\n"
    stamped.write_text(text, encoding="utf-8")
    latest.write_text(text, encoding="utf-8")
    return latest


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--hai-home",
        type=Path,
        default=None,
        help="Isolated HAI_HOME (default: fresh /tmp dir)",
    )
    args = parser.parse_args(argv)

    hai_home = args.hai_home or Path(tempfile.mkdtemp(prefix="hai-mcp-stdio-smoke-"))
    hai_home.mkdir(parents=True, exist_ok=True)
    project = hai_home / "project"
    project.mkdir(parents=True, exist_ok=True)
    (project / "out.md").write_text("smoke\n", encoding="utf-8")

    try:
        payload = asyncio.run(run_smoke(hai_home=hai_home, project=project))
    except Exception as exc:  # noqa: BLE001 — smoke must record failure
        payload = {
            "ok": False,
            "exit_code": 2,
            "error": f"{type(exc).__name__}: {exc}",
            "env_boundary": {"HAI_HOME": str(hai_home), "project_path": str(project)},
            "steps": [],
        }

    path = write_artifact(payload)
    print(json.dumps({"artifact": str(path), "ok": payload.get("ok"), "exit_code": payload.get("exit_code")}, indent=2))
    return int(payload.get("exit_code", 1))


if __name__ == "__main__":
    sys.exit(main())