forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnose.py
More file actions
384 lines (343 loc) · 13.9 KB
/
Copy pathdiagnose.py
File metadata and controls
384 lines (343 loc) · 13.9 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""soup diagnose — post-training model report card (v0.56.0).
Top-level CLI command (NOT a sub-group) — operators type:
soup diagnose <run-id>
soup diagnose <run-id> --output diagnose.json
soup diagnose <run-id> --badge diagnose.svg
soup diagnose <run-id> --attach-to-registry <id>
Since v0.71.7 (#165) the six probe runners (forgetting / refusal / format /
mode_collapse / memorization / contamination) run LIVE when ``--base-model``
is supplied: the model (+ optional ``--adapter`` LoRA path, ``--dataset``,
``--tokenizer``) is loaded and each probe is fed real generator output via
``soup_cli.utils.diagnose.live.run_live_diagnose``. Without ``--base-model``
the command computes scores from ``--evidence`` JSON or defaults to neutral OK.
"""
from __future__ import annotations
import json
import os
import stat
import tempfile
from typing import Optional
import typer
from rich.console import Console
from rich.markup import escape
from rich.panel import Panel
from rich.table import Table
from soup_cli import __version__
from soup_cli.utils.diagnose import FailureReport, compose_report
from soup_cli.utils.diagnose.badge import render_badge_svg
from soup_cli.utils.diagnose.report import FAILURE_MODES, FailureScore, classify_score
from soup_cli.utils.diagnose.runner import (
build_report,
neutral_score,
write_report,
)
from soup_cli.utils.paths import enforce_under_cwd_and_no_symlink
console = Console()
# 16 MiB cap on evidence JSON files (security review HIGH — prevents
# multi-GB / `/dev/zero` symlink-pointed OOM at json.load time).
_MAX_EVIDENCE_BYTES = 16 * 1024 * 1024
def _verdict_style(verdict: str) -> str:
return {"OK": "green", "MINOR": "yellow", "MAJOR": "red"}.get(
verdict, "white"
)
def _render_report(report: FailureReport) -> None:
table = Table(title=f"soup diagnose: {escape(report.adapter or report.run_id)}")
table.add_column("Mode", style="bold")
table.add_column("Score", justify="right")
table.add_column("Verdict")
table.add_column("Evidence", overflow="fold")
for mode in FAILURE_MODES:
score = report.scores.get(mode)
if score is None:
continue
table.add_row(
escape(mode),
f"{score.score:.3f}",
f"[{_verdict_style(score.verdict)}]{score.verdict}[/]",
escape(score.evidence),
)
console.print(table)
console.print(
Panel.fit(
f"[bold {_verdict_style(report.overall)}]{report.overall}[/] — "
f"run [bold]{escape(report.run_id)}[/]",
title="overall",
)
)
def _load_evidence(path: str) -> dict:
"""Load an evidence JSON (cwd-contained, symlink-rejected, size-capped).
Opens with ``O_NOFOLLOW`` (where available) and fstats the open fd so a
symlink swapped in after the containment check cannot redirect the read
(TOCTOU defence — backports the v0.71.25 ``soup ship`` hardened loader;
v0.71.25 known-limitation (4)).
"""
enforce_under_cwd_and_no_symlink(path, "evidence path")
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
try:
fd = os.open(path, flags)
except OSError as exc:
raise typer.BadParameter(f"evidence path unreadable: {exc}") from exc
with os.fdopen(fd, "r", encoding="utf-8") as handle:
if os.fstat(handle.fileno()).st_size > _MAX_EVIDENCE_BYTES:
raise typer.BadParameter(
f"evidence file exceeds {_MAX_EVIDENCE_BYTES} bytes"
)
payload = json.load(handle)
if not isinstance(payload, dict):
raise typer.BadParameter("evidence file must contain a JSON object")
return payload
def _scores_from_evidence(payload: dict) -> dict:
"""Translate a user-supplied evidence dict into FailureScore entries."""
raw_scores = payload.get("scores", {})
if not isinstance(raw_scores, dict):
raise typer.BadParameter("evidence.scores must be an object")
out = {}
for mode in FAILURE_MODES:
entry = raw_scores.get(mode)
if entry is None:
continue
if not isinstance(entry, dict):
raise typer.BadParameter(f"scores.{mode} must be an object")
score = entry.get("score", 1.0)
# Validate numeric before float() — a non-numeric score otherwise raised
# ValueError that exited 1 with zero output. BadParameter prints a clear
# message.
if isinstance(score, bool) or not isinstance(score, (int, float)):
raise typer.BadParameter(
f"scores.{mode}.score must be a number, got {score!r}"
)
evidence = entry.get("evidence", "supplied by --evidence")
verdict = entry.get("verdict") or classify_score(score)
out[mode] = FailureScore(
mode=mode,
score=float(score),
verdict=verdict,
evidence=str(evidence),
)
return out
def _write_badge(badge_path: str, svg: str) -> None:
"""Atomic SVG write with TOCTOU symlink rejection.
Mirrors the v0.33.0 #22 / v0.43.0 / v0.46.0 / v0.47.0 project policy:
`os.lstat + S_ISLNK` on the RAW path BEFORE `realpath`, then
`tempfile.mkstemp + os.replace` for the atomic swap.
"""
enforce_under_cwd_and_no_symlink(badge_path, "badge path")
if os.path.lexists(badge_path):
st = os.lstat(badge_path)
if stat.S_ISLNK(st.st_mode):
raise ValueError("badge path must not be a symlink")
parent = os.path.dirname(os.path.realpath(badge_path)) or "."
if not os.path.isdir(parent):
os.makedirs(parent, exist_ok=True)
fd, tmp_path = tempfile.mkstemp(
prefix=".badge-", suffix=".svg.tmp", dir=parent
)
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
handle.write(svg)
os.replace(tmp_path, badge_path)
except Exception:
if os.path.exists(tmp_path):
try:
os.unlink(tmp_path)
except OSError:
pass
raise
def _attach_to_registry(report: FailureReport, registry_id: str, output: str) -> None:
try:
from soup_cli.registry.attach import attach_artifact
except Exception as exc: # noqa: BLE001 — registry is optional
console.print(
f"[yellow]Warning:[/] could not import registry attach helper: "
f"{escape(type(exc).__name__)}"
)
return
try:
attach_artifact(registry_id, "diagnose_report", output)
console.print(
f"[green]Attached[/] diagnose_report to registry entry "
f"[bold]{escape(registry_id)}[/]"
)
except Exception as exc: # noqa: BLE001
console.print(
f"[yellow]Warning:[/] could not attach to registry: "
f"{escape(type(exc).__name__)}: {escape(str(exc))}"
)
def _emit_report(
report: FailureReport,
*,
output: Optional[str],
badge: Optional[str],
attach_to_registry: Optional[str],
) -> None:
"""Shared render + optional output/badge/registry-attach (no exit)."""
_render_report(report)
if output:
try:
write_report(report, output)
console.print(f"[green]Wrote[/] {escape(output)}")
except (OSError, ValueError) as exc:
console.print(
f"[red]Error:[/] cannot write --output: "
f"{escape(type(exc).__name__)}: {escape(str(exc))}"
)
raise typer.Exit(code=1) from exc
if badge:
try:
svg = render_badge_svg(report)
_write_badge(badge, svg)
console.print(f"[green]Badge written[/] to {escape(badge)}")
except (OSError, ValueError, TypeError) as exc:
console.print(
f"[red]Error:[/] cannot write --badge: "
f"{escape(type(exc).__name__)}: {escape(str(exc))}"
)
raise typer.Exit(code=1) from exc
if attach_to_registry and output:
_attach_to_registry(report, attach_to_registry, output)
elif attach_to_registry and not output:
console.print(
"[yellow]Warning:[/] --attach-to-registry needs --output (skipped)."
)
def diagnose(
run_id: str = typer.Argument(..., help="Registry run id (or any opaque tag)."),
base: str = typer.Option("", "--base", help="Base model name (informational)."),
adapter: str = typer.Option("", "--adapter", help="Adapter name (informational)."),
evidence_path: Optional[str] = typer.Option(
None,
"--evidence",
help="JSON file with pre-computed probe scores (see README).",
),
output: Optional[str] = typer.Option(
None, "--output", "-o", help="Write the report JSON to this path."
),
badge: Optional[str] = typer.Option(
None, "--badge", help="Write an SVG badge to this path."
),
attach_to_registry: Optional[str] = typer.Option(
None, "--attach-to-registry", help="Attach the report to a registry entry id."
),
base_model: Optional[str] = typer.Option(
None,
"--base-model",
help=(
"Base model id to LOAD for a live diagnose run (e.g. "
"HuggingFaceTB/SmolLM2-135M). When set, the 6 probes run live "
"against the model instead of emitting neutral scores."
),
),
dataset: Optional[str] = typer.Option(
None,
"--dataset",
help="Training JSONL for the live forgetting / format / memorization "
"probes (must stay under cwd).",
),
tokenizer: Optional[str] = typer.Option(
None,
"--tokenizer",
help="Tokenizer id/path for a sub-word memorization probe (live).",
),
device: Optional[str] = typer.Option(
None, "--device", help="Device for the live run (cuda / cpu)."
),
citation_style: str = typer.Option(
"bracket",
"--citation-style",
help=(
"Citation style for the live RAFT citation probe: bracket "
"([doc-id]) / inline ((doc-id)) / footnote ([^id]). Must match the "
"style the model was trained to emit. (v0.71.17 #254)"
),
),
shuffle_seed: Optional[int] = typer.Option(
None,
"--shuffle-seed",
help=(
"RAFT document-shuffle seed for the live citation probe (composes "
"the [doc-N] ids the model is scored against). (v0.71.17 #254)"
),
),
) -> None:
"""Compute a 6-mode FailureReport for a completed run.
With ``--base-model`` the six probes run LIVE against the loaded model
(+ optional ``--adapter`` LoRA path, ``--dataset``, ``--tokenizer``).
Without it, scores come from ``--evidence`` JSON or default to neutral OK.
"""
if not isinstance(run_id, str) or not run_id.strip():
raise typer.BadParameter("run_id must be a non-empty string")
if "\x00" in run_id or len(run_id) > 512:
raise typer.BadParameter("run_id has a null byte or is too long")
# v0.71.17 #254 — validate the citation style up front (a typo surfaces
# before any model load). Reuses the closed allowlist validator.
from soup_cli.utils.citation_faithful import validate_citation_style
try:
resolved_citation_style = validate_citation_style(citation_style)
except (TypeError, ValueError) as exc:
console.print(
f"[red]Invalid --citation-style:[/] {escape(str(exc))}"
)
raise typer.Exit(code=2) from exc
if base_model is not None:
from soup_cli.utils.diagnose.live import run_live_diagnose
try:
report = run_live_diagnose(
run_id=run_id,
base=base_model,
adapter=adapter or None,
dataset_path=dataset,
device=device,
tokenizer=tokenizer,
soup_version=__version__,
citation_style=resolved_citation_style,
shuffle_seed=shuffle_seed,
)
except (ValueError, TypeError, OSError, RuntimeError) as exc:
console.print(
f"[red]Error:[/] live diagnose failed: "
f"{escape(type(exc).__name__)}: {escape(str(exc))}"
)
raise typer.Exit(code=1) from exc
_emit_report(report, output=output, badge=badge, attach_to_registry=attach_to_registry)
if report.overall == "MAJOR":
raise typer.Exit(code=2)
return
scores = {}
extras = {}
if evidence_path:
try:
payload = _load_evidence(evidence_path)
except (OSError, json.JSONDecodeError, ValueError) as exc:
console.print(
f"[red]Error:[/] cannot read --evidence: "
f"{escape(type(exc).__name__)}"
)
raise typer.Exit(code=1) from exc
scores = _scores_from_evidence(payload)
# Sanitise extras — null-byte rejection + 256-char cap on both
# key and value (security review MEDIUM — prevents Rich markup
# injection through user-supplied evidence metadata).
for key, value in (payload.get("extras") or {}).items():
key_s = str(key)
value_s = str(value)
if "\x00" in key_s or "\x00" in value_s:
console.print(
"[red]Error:[/] extras key/value must not contain null bytes"
)
raise typer.Exit(code=1)
extras[key_s[:256]] = value_s[:256]
# Fill missing modes with neutral OK + advisory.
for mode in FAILURE_MODES:
scores.setdefault(mode, neutral_score(mode, "no evidence"))
report = build_report(
run_id=run_id,
base=base,
adapter=adapter,
scores=scores,
soup_version=__version__,
extras=extras,
)
_emit_report(report, output=output, badge=badge, attach_to_registry=attach_to_registry)
if report.overall == "MAJOR":
raise typer.Exit(code=2)
# Compose a tiny helper so the CLI module is callable from tests.
__all__ = ["diagnose", "compose_report", "build_report", "render_badge_svg"]