|
2 | 2 |
|
3 | 3 | import argparse |
4 | 4 | import json |
| 5 | +import os |
5 | 6 | from datetime import datetime, timezone |
6 | 7 | from pathlib import Path |
7 | 8 | from typing import Callable |
8 | 9 |
|
| 10 | +import sentry_sdk |
| 11 | + |
9 | 12 | from agents.memory_extractor import extract_memories |
10 | 13 | from agents.semantic_confirmer import ALLOWED_RELATION_TYPES, CONFIDENCE_THRESHOLD, confirm_authority_changes |
11 | 14 | from agents.semantic_proposer import ( |
@@ -233,51 +236,72 @@ def run_engine(engine: str, cases: list[dict]) -> dict: |
233 | 236 | remove_citation_false_fires = 0 |
234 | 237 |
|
235 | 238 | for case in cases: |
236 | | - items = _items(case) |
237 | | - prompt = build_authority_change_prompt(items) |
238 | | - raw_output = "" |
239 | | - malformed_reasons: list[str] = [] |
240 | | - try: |
241 | | - raw_output = provider(prompt) |
242 | | - proposals, malformed_reasons = _parse_case_output(raw_output) |
243 | | - except Exception as error: # per-case malformed/degraded bucket, never abort the run |
244 | | - proposals = [] |
245 | | - malformed_reasons = [f"{type(error).__name__}: {error}"] |
246 | | - |
247 | | - if malformed_reasons: |
248 | | - malformed_count += 1 |
249 | | - |
250 | | - confirmed = confirm_authority_changes(proposals, items) |
251 | | - score = _score_confirmed(case, confirmed) |
252 | | - no_confirmer = _score_remove_confirmer(case, proposals) |
253 | | - no_citation = _score_confirmed(case, _confirm_without_citation(proposals, items)) |
254 | | - lexical = _lexical_result(case) |
255 | | - |
256 | | - if score["expected_positive"]: |
257 | | - positives += 1 |
258 | | - positives_caught += int(score["caught"]) |
259 | | - positives_caught_direction += int(score["direction_caught"]) |
260 | | - else: |
261 | | - negatives += 1 |
262 | | - negatives_passed += int(score["negative_passed"]) |
263 | | - remove_confirmer_false_fires += int(no_confirmer["would_false_fire_without_confirmation"]) |
264 | | - remove_citation_false_fires += int(not no_citation["negative_passed"]) |
265 | | - |
266 | | - case_results.append({ |
267 | | - "case_id": case["id"], |
268 | | - "class": case["class"], |
269 | | - "malformed": bool(malformed_reasons), |
270 | | - "malformed_reasons": malformed_reasons, |
271 | | - "proposal_count": len(proposals), |
272 | | - "proposals": proposals, |
273 | | - "confirmed_findings": confirmed["findings"], |
274 | | - "needs_human_judgment": confirmed["needs_human_judgment"], |
275 | | - "score": score, |
276 | | - "ablation_remove_confirmer": no_confirmer, |
277 | | - "ablation_remove_citation_requirement": no_citation, |
278 | | - "lexical_baseline_current_detector": lexical, |
279 | | - "raw_output": raw_output, |
280 | | - }) |
| 239 | + with sentry_sdk.start_span(op="eval.case", name=f"case:{case['id']}") as case_span: |
| 240 | + case_span.set_data("case_id", case["id"]) |
| 241 | + case_span.set_data("case_class", case["class"]) |
| 242 | + case_span.set_data("engine", engine) |
| 243 | + items = _items(case) |
| 244 | + prompt = build_authority_change_prompt(items) |
| 245 | + raw_output = "" |
| 246 | + malformed_reasons: list[str] = [] |
| 247 | + try: |
| 248 | + raw_output = provider(prompt) |
| 249 | + proposals, malformed_reasons = _parse_case_output(raw_output) |
| 250 | + except Exception as error: # per-case malformed/degraded bucket, never abort the run |
| 251 | + proposals = [] |
| 252 | + malformed_reasons = [f"{type(error).__name__}: {error}"] |
| 253 | + sentry_sdk.capture_exception(error) |
| 254 | + |
| 255 | + if malformed_reasons: |
| 256 | + malformed_count += 1 |
| 257 | + case_span.set_data("malformed", True) |
| 258 | + case_span.set_data("malformed_reasons", malformed_reasons) |
| 259 | + sentry_sdk.add_breadcrumb( |
| 260 | + category="eval", |
| 261 | + message=f"proposer output malformed: {case['id']}", |
| 262 | + level="warning", |
| 263 | + data={ |
| 264 | + "case_id": case["id"], |
| 265 | + "engine": engine, |
| 266 | + "reasons": malformed_reasons, |
| 267 | + }, |
| 268 | + ) |
| 269 | + |
| 270 | + confirmed = confirm_authority_changes(proposals, items) |
| 271 | + score = _score_confirmed(case, confirmed) |
| 272 | + no_confirmer = _score_remove_confirmer(case, proposals) |
| 273 | + no_citation = _score_confirmed(case, _confirm_without_citation(proposals, items)) |
| 274 | + lexical = _lexical_result(case) |
| 275 | + |
| 276 | + if score["expected_positive"]: |
| 277 | + positives += 1 |
| 278 | + positives_caught += int(score["caught"]) |
| 279 | + positives_caught_direction += int(score["direction_caught"]) |
| 280 | + else: |
| 281 | + negatives += 1 |
| 282 | + negatives_passed += int(score["negative_passed"]) |
| 283 | + remove_confirmer_false_fires += int(no_confirmer["would_false_fire_without_confirmation"]) |
| 284 | + remove_citation_false_fires += int(not no_citation["negative_passed"]) |
| 285 | + |
| 286 | + case_span.set_data("score", score) |
| 287 | + case_span.set_data("proposal_count", len(proposals)) |
| 288 | + case_span.set_data("confirmed_count", len(confirmed["findings"])) |
| 289 | + |
| 290 | + case_results.append({ |
| 291 | + "case_id": case["id"], |
| 292 | + "class": case["class"], |
| 293 | + "malformed": bool(malformed_reasons), |
| 294 | + "malformed_reasons": malformed_reasons, |
| 295 | + "proposal_count": len(proposals), |
| 296 | + "proposals": proposals, |
| 297 | + "confirmed_findings": confirmed["findings"], |
| 298 | + "needs_human_judgment": confirmed["needs_human_judgment"], |
| 299 | + "score": score, |
| 300 | + "ablation_remove_confirmer": no_confirmer, |
| 301 | + "ablation_remove_citation_requirement": no_citation, |
| 302 | + "lexical_baseline_current_detector": lexical, |
| 303 | + "raw_output": raw_output, |
| 304 | + }) |
281 | 305 |
|
282 | 306 | return { |
283 | 307 | "engine": engine, |
@@ -347,27 +371,65 @@ def _write_markdown(result: dict, path: Path) -> None: |
347 | 371 | path.write_text("\n".join(lines), encoding="utf-8") |
348 | 372 |
|
349 | 373 |
|
| 374 | +def _init_sentry() -> None: |
| 375 | + dsn = os.environ.get("SENTRY_DSN") |
| 376 | + if not dsn: |
| 377 | + return |
| 378 | + sentry_sdk.init( |
| 379 | + dsn=dsn, |
| 380 | + traces_sample_rate=1.0, |
| 381 | + send_default_pii=True, |
| 382 | + enable_logs=True, |
| 383 | + release=os.environ.get("SENTRY_RELEASE", "memory-authority-auditor@0.1.0"), |
| 384 | + environment=os.environ.get("SENTRY_ENVIRONMENT", "development"), |
| 385 | + ) |
| 386 | + |
| 387 | + |
350 | 388 | def main() -> int: |
351 | 389 | parser = argparse.ArgumentParser() |
352 | 390 | parser.add_argument("--engines", default="anthropic,local_llama3.2") |
353 | 391 | parser.add_argument("--output-dir", default=str(ARTIFACT_DIR)) |
354 | 392 | parser.add_argument("--fixture", default=str(FIXTURE)) |
355 | 393 | args = parser.parse_args() |
356 | 394 |
|
| 395 | + _init_sentry() |
| 396 | + |
357 | 397 | run_id = _utc_slug() |
358 | 398 | output_dir = Path(args.output_dir) |
359 | 399 | output_dir.mkdir(parents=True, exist_ok=True) |
360 | 400 | fixture_path = Path(args.fixture) |
361 | 401 | cases = _cases(fixture_path) |
362 | 402 | engines = [engine.strip() for engine in args.engines.split(",") if engine.strip()] |
363 | | - result = { |
364 | | - "run_id": run_id, |
365 | | - "generated_at_utc": datetime.now(timezone.utc).isoformat(), |
366 | | - "fixture": str(fixture_path), |
367 | | - "scoring_rules": SCORING_RULES, |
368 | | - "engines": [run_engine(engine, cases) for engine in engines], |
369 | | - "boundary": "No public claim from this artifact until Ka'el and Fable re-verify.", |
370 | | - } |
| 403 | + |
| 404 | + with sentry_sdk.start_transaction(op="eval.run", name="path_a_eval") as txn: |
| 405 | + txn.set_data("run_id", run_id) |
| 406 | + txn.set_data("fixture", str(fixture_path)) |
| 407 | + txn.set_data("engines", engines) |
| 408 | + txn.set_data("case_count", len(cases)) |
| 409 | + |
| 410 | + engine_results = [] |
| 411 | + for engine in engines: |
| 412 | + with sentry_sdk.start_span(op="eval.engine", name=f"engine:{engine}") as eng_span: |
| 413 | + eng_span.set_data("engine", engine) |
| 414 | + eng_result = run_engine(engine, cases) |
| 415 | + eng_span.set_data("summary", eng_result["summary"]) |
| 416 | + malformed = eng_result["summary"]["malformed_cases"] |
| 417 | + if malformed > 0: |
| 418 | + sentry_sdk.capture_message( |
| 419 | + f"engine {engine} completed with {malformed}/{len(cases)} malformed cases", |
| 420 | + level="warning", |
| 421 | + ) |
| 422 | + engine_results.append(eng_result) |
| 423 | + |
| 424 | + result = { |
| 425 | + "run_id": run_id, |
| 426 | + "generated_at_utc": datetime.now(timezone.utc).isoformat(), |
| 427 | + "fixture": str(fixture_path), |
| 428 | + "scoring_rules": SCORING_RULES, |
| 429 | + "engines": engine_results, |
| 430 | + "boundary": "No public claim from this artifact until Ka'el and Fable re-verify.", |
| 431 | + } |
| 432 | + |
371 | 433 | json_path = output_dir / f"path_a_eval_{run_id}.json" |
372 | 434 | md_path = output_dir / f"path_a_eval_{run_id}.md" |
373 | 435 | json_path.write_text(json.dumps(result, indent=2), encoding="utf-8") |
|
0 commit comments