File size: 6,789 Bytes
08e2243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Command-line interface."""
import argparse
import json
import sys
from datetime import datetime, timezone
from pathlib import Path

from .runner import run


def _load_report(path):
    """Load one saved scan report, raising ValueError with a readable
    message when the file is missing, unreadable, or malformed."""
    try:
        data = json.loads(Path(path).read_text(encoding="utf-8"))
    except OSError as exc:
        raise ValueError(f"cannot read report {path}: {exc}") from exc
    except json.JSONDecodeError as exc:
        raise ValueError(f"report {path} is not valid JSON: {exc}") from exc
    if not isinstance(data, dict):
        raise ValueError(f"report {path} must be a JSON object")
    results = data.get("results", [])
    if not isinstance(results, list):
        raise ValueError(f"report {path}: 'results' must be a list")
    for item in results:
        if not isinstance(item, dict) or not isinstance(item.get("probe_id"), str):
            raise ValueError(
                f"report {path}: each result needs a string 'probe_id'")
        evaluation = item.get("evaluation")
        if not isinstance(evaluation, dict) or not isinstance(
                evaluation.get("status"), str):
            raise ValueError(
                f"report {path}: result {item.get('probe_id')!r} needs "
                "'evaluation' with a string 'status'")
    return data


def compare_reports(paths, output):
    """Combine per-provider scan reports into one comparison.

    Prints a probe-by-provider status matrix and writes a consolidated
    JSON file. A provider that was scanned twice (different models)
    gets a second column named provider/model.
    """
    providers = []
    by_probe = {}
    for path in paths:
        try:
            data = _load_report(path)
        except ValueError as exc:
            print(f"error: {exc}", file=sys.stderr)
            return 2
        provider = data.get("provider")
        if not isinstance(provider, str) or not provider:
            provider = Path(path).stem
        model = data.get("model")
        if not isinstance(model, str):
            model = None
        column = provider
        suffix = 0
        while any(p["column"] == column for p in providers):
            suffix += 1
            base = f"{provider}/{model}" if model else f"{provider}/{path}"
            column = base if suffix == 1 else f"{base}#{suffix}"
        providers.append({"column": column, "provider": provider,
                          "model": model, "path": str(path)})
        for item in data.get("results", []):
            by_probe.setdefault(item["probe_id"], {})[column] = {
                "status": item["evaluation"]["status"],
                "model": model,
                "error": item.get("error"),
            }
    comparison = {
        "schema_version": "1.0",
        "generated_at_utc": datetime.now(timezone.utc).isoformat(),
        "providers": providers,
        "probes": by_probe,
    }
    header = "Probe".ljust(22) + "".join(
        p["column"].ljust(22) for p in providers
    )
    print(header)
    for probe_id in sorted(by_probe):
        row = "".join(
            by_probe[probe_id].get(p["column"], {}).get("status", "-").ljust(22)
            for p in providers
        )
        print(probe_id.ljust(22) + row)
    out = Path(output)
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(
        json.dumps(comparison, indent=2, ensure_ascii=False) + "\n",
        encoding="utf-8",
    )
    print(f"Saved comparison: {out}")
    return 0


def main(argv=None):
    parser = argparse.ArgumentParser(
        description=(
            "Run authorized instruction-boundary probes "
            "against an AI API, or compare saved scan reports."
        )
    )
    parser.add_argument(
        "--provider",
        choices=("openai", "anthropic", "gemini", "openai-compatible"),
        help="Provider to scan (run mode).",
    )
    parser.add_argument(
        "--model",
        help="Optional provider model identifier.",
    )
    parser.add_argument(
        "--base-url",
        default=None,
        help="OpenAI-compatible API base URL "
             "(openai-compatible provider only).",
    )
    parser.add_argument(
        "--api-key-env",
        default="OPENAI_COMPAT_API_KEY",
        help="Environment variable holding the compatible endpoint API "
             "key; may be empty for local servers "
             "(openai-compatible provider only).",
    )
    parser.add_argument(
        "--output",
        default="openmind-scan-report.json",
        help="Report destination (run mode).",
    )
    sub = parser.add_subparsers(dest="cmd")
    compare = sub.add_parser(
        "compare",
        help="Compare saved scan reports across providers.",
    )
    compare.add_argument("reports", nargs="+")
    compare.add_argument(
        "--output",
        default="reports/comparison.json",
        help="Consolidated comparison destination.",
    )
    args = parser.parse_args(argv)
    if args.cmd == "compare":
        return compare_reports(args.reports, args.output)
    if not args.provider:
        parser.error("--provider is required")
    if args.provider == "openai-compatible":
        import os
        if not (args.base_url or os.getenv("OPENAI_COMPAT_BASE_URL")):
            parser.error(
                "Set --base-url or OPENAI_COMPAT_BASE_URL for this provider"
            )
        if not (args.model or os.getenv("OPENAI_COMPAT_MODEL")):
            parser.error("Set --model or OPENAI_COMPAT_MODEL for this provider")
    report, path = None, None
    try:
        report, path = run(
            args.provider,
            args.model,
            args.output,
            base_url=args.base_url,
            api_key_env=args.api_key_env,
        )
    except OSError as exc:
        print(f"error: cannot write report: {exc}", file=sys.stderr)
        return 2
    counts = {}
    for item in report["results"]:
        status = item["evaluation"]["status"]
        counts[status] = counts.get(status, 0) + 1
    print(f"Saved evidence report: {path}")
    print(
        "Result counts:",
        ", ".join(
            f"{key}={value}"
            for key, value in sorted(counts.items())
        ),
    )
    if any(
        item.get("error")
        or item.get("provider_response_status") in {
            "incomplete", "empty", "not_received", "partial"
        }
        for item in report["results"]
    ):
        print(
            "warning: one or more provider responses were incomplete, empty, partial, "
            "or not received; inconclusive results require review. Inspect "
            "the JSON evidence report.",
            file=sys.stderr,
        )
        return 1
    return 0