File size: 3,456 Bytes
fd11263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Unified command line for verification simulation runtimes."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path
from typing import Any

from verification_simulation_agents.common import atomic_json
from verification_simulation_agents.config import RUNTIMES, SimulationConfigError, load_simulation_config
from verification_simulation_agents.nano_runtime import run_nano
from verification_simulation_agents.offline_runtime import run_offline
from verification_simulation_agents.opencode_runtime import run_opencode
from verification_simulation_agents.workspace import WorkspaceError, create_workspace


REQUEST_VERSION = "verification-simulation/request-v1"


def _load_request(path: Path) -> dict[str, Any]:
    try:
        value = json.loads(Path(path).read_text(encoding="utf-8"))
    except (OSError, UnicodeError, json.JSONDecodeError) as error:
        raise ValueError(f"cannot load simulation request: {error}") from error
    if not isinstance(value, dict) or value.get("schema_version") != REQUEST_VERSION:
        raise ValueError(f"simulation request must use {REQUEST_VERSION}")
    for key in ("case_id", "case_name", "target_skill_path", "fixture_path"):
        if not isinstance(value.get(key), str) or not value[key].strip():
            raise ValueError(f"simulation request field {key} must be a non-empty string")
    runtime = value.get("runtime", "offline")
    if runtime not in RUNTIMES:
        raise ValueError(f"runtime must be one of {RUNTIMES}")
    value["runtime"] = runtime
    return value


def run(config_path: Path, request_path: Path, output_path: Path) -> tuple[int, Path]:
    output = Path(output_path).resolve()
    config = load_simulation_config(config_path)
    request = _load_request(request_path)
    if request["runtime"] not in config.verification.allowed_runtimes:
        raise ValueError(f"runtime {request['runtime']!r} is not allowed by nano_agent.toml")
    workspace = create_workspace(
        Path(request["target_skill_path"]),
        Path(request["fixture_path"]),
        output,
        request["case_name"],
    )
    if request["runtime"] == "offline":
        result = run_offline(config, request, workspace)
    elif request["runtime"] == "opencode":
        result = run_opencode(config, request, workspace)
    else:
        result = run_nano(config, request, workspace)
    result_path = output / "simulation-result.json"
    atomic_json(result_path, result)
    code = 0 if result["status"] == "SUCCEEDED" else (2 if result["status"] == "BLOCKED" else 1)
    return code, result_path


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(prog="verification-simulation-agent")
    subparsers = parser.add_subparsers(dest="command", required=True)
    command = subparsers.add_parser("run", help="run one supervised Skill-verification simulation")
    command.add_argument("--config", required=True)
    command.add_argument("--request", required=True)
    command.add_argument("--output", required=True)
    args = parser.parse_args(argv)
    try:
        code, result = run(Path(args.config), Path(args.request), Path(args.output))
    except (OSError, ValueError, SimulationConfigError, WorkspaceError) as error:
        print(f"verification simulation error: {error}", file=sys.stderr)
        return 2
    print(result)
    return code


if __name__ == "__main__":
    raise SystemExit(main())