File size: 10,027 Bytes
3ca7119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for eval_run_summary_csv append log."""

from __future__ import annotations

import csv
import json
import sys
from pathlib import Path

_REPO = Path(__file__).resolve().parents[1]
_EVAL = _REPO / "agent-redact" / "eval"
if str(_EVAL) not in sys.path:
    sys.path.insert(0, str(_EVAL))

from eval_run_summary_csv import (  # noqa: E402
    SUMMARY_CSV_COLUMNS,
    append_eval_run_summary_row,
    rebuild_eval_run_summary_csv,
    run_stack_from_dir,
)


def test_append_eval_run_summary_row(tmp_path, monkeypatch):
    monkeypatch.setenv("AGENT_EVAL_DEEPEVAL_TRACING", "1")
    monkeypatch.setenv(
        "AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "eval_run_summary.csv")
    )

    run_dir = tmp_path / "run_20261104_122626"
    run_dir.mkdir()
    summary = {
        "document_name": "doc.pdf",
        "gold_case": "/gold/example_live",
        "overall_pass": False,
        "workflow_incomplete": False,
        "eval_turn_kind": "initial_task",
        "follow_up_steer_count": 0,
        "metrics": [
            {"name": "policy_strict", "pass": False, "score": 0.0, "skipped": False},
            {"name": "tool_correctness", "pass": True, "score": 1.0, "skipped": False},
            {"name": "task_completion", "pass": True, "score": 0.9, "skipped": False},
        ],
    }
    (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8")
    (run_dir / "policy_gate.json").write_text(
        json.dumps({"pass_strict": False}), encoding="utf-8"
    )
    (run_dir / "gold_geometry.json").write_text(
        json.dumps({"f1": 0.9, "recall": 0.88, "precision": 0.92}),
        encoding="utf-8",
    )
    (run_dir / "ocr_gold.json").write_text(
        json.dumps({"skipped": False, "f1": 0.95, "recall": 0.94, "precision": 0.96}),
        encoding="utf-8",
    )
    (run_dir / "policy_extraction.json").write_text(
        json.dumps(
            {
                "must_redact": {"f1": 1.0},
                "deny_list": {"f1": 0.8},
            }
        ),
        encoding="utf-8",
    )

    out = append_eval_run_summary_row(
        summary=summary,
        run_dir=run_dir,
        task_instructions="Redact names",
        run_id="gradio_abc_20261104_122626",
    )
    assert out is not None
    assert out.is_file()

    with out.open(encoding="utf-8", newline="") as handle:
        rows = list(csv.DictReader(handle))
    assert len(rows) == 1
    row = rows[0]
    assert row["run_id"] == "gradio_abc_20261104_122626"
    assert row["document_name"] == "doc.pdf"
    assert row["gold_case_path"] == "/gold/example_live"
    assert row["run_timestamp_utc"] == "2026-11-04T12:26:26Z"
    assert row["policy_strict_pass"] == "False"
    assert row["tool_correctness_pass"] == "True"
    assert row["pass_strict_from_gate"] == "false"
    assert float(row["gold_geometry_f1"]) == 0.9
    assert row["gold_geometry_skipped"] == "False"
    assert float(row["ocr_gold_f1"]) == 0.95
    assert row["ocr_gold_skipped"] == "False"
    assert float(row["policy_extraction_must_redact_f1"]) == 1.0
    assert float(row["policy_extraction_deny_list_f1"]) == 0.8
    assert "eval_summary.json" in row["output_files"]
    assert set(row.keys()) == set(SUMMARY_CSV_COLUMNS)


def test_run_stack_from_artifacts_and_csv_columns(tmp_path, monkeypatch):
    monkeypatch.delenv("AGENT_DEFAULT_MODEL", raising=False)
    monkeypatch.delenv("AGENT_DEFAULT_PROVIDER", raising=False)
    monkeypatch.setenv("AGENT_EVAL_DEEPEVAL_TRACING", "1")
    monkeypatch.setenv(
        "AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "eval_run_summary.csv")
    )

    run_dir = tmp_path / "run_20261104_170815"
    run_dir.mkdir()
    (run_dir / "gradio_run_meta.json").write_text(
        json.dumps(
            {
                "orchestrator": "langgraph",
                "model_name": "llama/gemma-4-31b",
                "ocr_method": "paddle",
                "pii_method": "aws_comprehend",
                "pii_llm_model": "amazon.nova-pro-v1:0",
                "chosen_local_ocr_model": "hybrid-paddle-vlm",
                "chosen_ocr_model": "qwen_3_5_27b",
                "encourage_vlm_faces": True,
                "encourage_vlm_signatures": False,
            }
        ),
        encoding="utf-8",
    )
    summary = {
        "document_name": "doc.pdf",
        "overall_pass": True,
        "workflow_incomplete": False,
        "metrics": [],
    }
    (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8")

    stack = run_stack_from_dir(run_dir)
    assert stack["model_name"] == "llama/gemma-4-31b"
    assert stack["ocr_method"] == "paddle"
    assert stack["chosen_local_ocr_model"] == "hybrid-paddle-vlm"
    assert stack["chosen_ocr_model"] == "qwen_3_5_27b"

    append_eval_run_summary_row(
        summary=summary,
        run_dir=run_dir,
        run_id="gradio_test",
    )
    with (tmp_path / "eval_run_summary.csv").open(
        encoding="utf-8", newline=""
    ) as handle:
        row = next(csv.DictReader(handle))
    assert row["model_name"] == "llama/gemma-4-31b"
    assert row["orchestrator"] == "langgraph"
    assert row["agent_orchestrator"] == "langgraph"
    assert row["agent_model_name"] == "llama/gemma-4-31b"
    assert row["ocr_method"] == "paddle"
    assert row["pii_method"] == "aws_comprehend"
    assert row["pii_llm_model"] == "amazon.nova-pro-v1:0"
    assert row["chosen_local_ocr_model"] == "hybrid-paddle-vlm"
    assert row["chosen_ocr_model"] == "qwen_3_5_27b"
    assert row["encourage_vlm_faces"] == "true"
    assert row["encourage_vlm_signatures"] == "false"


def test_main_app_row_does_not_inherit_agent_env(tmp_path, monkeypatch):
    """Non-agentic (main_app) rows must not pick up ambient AGENT_* env values."""
    monkeypatch.setenv("AGENT_EVAL_RUN_SUMMARY_CSV", "1")
    monkeypatch.setenv(
        "AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "eval_run_summary.csv")
    )
    monkeypatch.setenv("AGENT_DEFAULT_PROVIDER", "amazon-bedrock")
    monkeypatch.setenv("AGENT_DEFAULT_MODEL", "amazon.nova-pro-v1:0")
    monkeypatch.setenv("AGENT_ORCHESTRATOR", "pi")

    run_dir = tmp_path / "run_20261104_180000"
    run_dir.mkdir()
    (run_dir / "app_run_meta.json").write_text(
        json.dumps(
            {
                "document_name": "doc.pdf",
                "ocr_method": "AWS Textract",
                "pii_method": "AWS Comprehend",
                "chosen_ocr_model": "",
                "eval_profile": "app_gradio",
            }
        ),
        encoding="utf-8",
    )
    summary = {
        "document_name": "doc.pdf",
        "overall_pass": True,
        "metrics": [],
        "eval_turn_kind": "app_redact",
    }
    (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8")

    append_eval_run_summary_row(
        summary=summary,
        run_dir=run_dir,
        run_id="app_test",
        run_source="main_app",
    )
    with (tmp_path / "eval_run_summary.csv").open(
        encoding="utf-8", newline=""
    ) as handle:
        row = next(csv.DictReader(handle))
    assert row["run_source"] == "main_app"
    assert row["orchestrator"] == ""
    assert row["model_name"] == ""
    assert row["agent_orchestrator"] == ""
    assert row["agent_model_name"] == ""
    assert row["ocr_method"] == "AWS Textract"
    assert row["pii_method"] == "AWS Comprehend"
    assert row["pii_llm_model"] == ""


def test_agent_row_falls_back_to_agent_env(tmp_path, monkeypatch):
    """Agentic rows without meta still resolve orchestrator/model from AGENT_* env."""
    monkeypatch.setenv("AGENT_EVAL_RUN_SUMMARY_CSV", "1")
    monkeypatch.setenv(
        "AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "eval_run_summary.csv")
    )
    monkeypatch.setenv("AGENT_DEFAULT_PROVIDER", "llama-cpp")
    monkeypatch.setenv("AGENT_DEFAULT_MODEL", "unsloth/Inkling-2-Mini-GGUF")
    monkeypatch.setenv("AGENT_ORCHESTRATOR", "pi")

    run_dir = tmp_path / "run_20261104_181500"
    run_dir.mkdir()
    summary = {"document_name": "doc.pdf", "overall_pass": True, "metrics": []}
    (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8")

    append_eval_run_summary_row(
        summary=summary,
        run_dir=run_dir,
        run_id="agent_test",
        run_source="agent_gradio",
    )
    with (tmp_path / "eval_run_summary.csv").open(
        encoding="utf-8", newline=""
    ) as handle:
        row = next(csv.DictReader(handle))
    assert row["orchestrator"] == "pi"
    assert row["model_name"] == "llama-cpp/unsloth/Inkling-2-Mini-GGUF"
    assert row["agent_orchestrator"] == "pi"
    assert row["agent_model_name"] == "llama-cpp/unsloth/Inkling-2-Mini-GGUF"


def test_run_stack_manifest_fallback(tmp_path):
    run_dir = tmp_path / "run_manifest"
    run_dir.mkdir()
    (run_dir / "manifest.json").write_text(
        json.dumps(
            {
                "ocr_method": "pymupdf",
                "pii_method": "spacy",
                "run_model_name": "openai/gpt-4o",
                "run_chosen_ocr_model": "gemma_3_27b",
            }
        ),
        encoding="utf-8",
    )
    stack = run_stack_from_dir(run_dir)
    assert stack["ocr_method"] == "pymupdf"
    assert stack["pii_method"] == "spacy"
    assert stack["model_name"] == "openai/gpt-4o"
    assert stack["chosen_ocr_model"] == "gemma_3_27b"


def test_rebuild_from_runs(tmp_path, monkeypatch):
    monkeypatch.setenv("AGENT_EVAL_RUN_SUMMARY_CSV_PATH", str(tmp_path / "rebuilt.csv"))
    runs = tmp_path / "runs"
    run_dir = runs / "gradio_x_20261104_100000"
    run_dir.mkdir(parents=True)
    summary = {
        "document_name": "a.pdf",
        "overall_pass": True,
        "metrics": [],
    }
    (run_dir / "eval_summary.json").write_text(json.dumps(summary), encoding="utf-8")

    count = rebuild_eval_run_summary_csv(
        search_root=runs, dest=tmp_path / "rebuilt.csv"
    )
    assert count == 1
    text = (tmp_path / "rebuilt.csv").read_text(encoding="utf-8")
    assert "a.pdf" in text