File size: 4,595 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
"""Tests for synthetic Task Completion actual_output (main app eval)."""

from __future__ import annotations

import json
import sys
from pathlib import Path

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

from app_task_completion_output import (
    build_app_task_completion_actual_output,
)  # noqa: E402
from derive_app_eval_instructions import AppEvalRedactionSettings  # noqa: E402
from run_agent_regression import GateSummary  # noqa: E402


def _settings(**kwargs) -> AppEvalRedactionSettings:
    return AppEvalRedactionSettings(
        text_extraction_method="Local model - selectable text",
        pii_identification_method="Local",
        **kwargs,
    )


def test_build_output_non_empty_and_omits_pii_by_default(tmp_path, monkeypatch):
    monkeypatch.delenv("APP_EVAL_TASK_COMPLETION_INCLUDE_TERM_SAMPLES", raising=False)
    run_dir = tmp_path / "run"
    run_dir.mkdir()
    (run_dir / "agent_review_file.csv").write_text(
        "image,page,label,color,xmin,ymin,xmax,ymax,id,text\n"
        'x.png,1,NAME,"(0,0,0)",0,0,1,1,a,SecretName\n',
        encoding="utf-8",
    )
    (run_dir / "redacted.pdf").write_bytes(b"%PDF-1.4")
    (run_dir / "ocr_results_with_words.csv").write_text(
        "page,line,word_text\n1,1,hello\n", encoding="utf-8"
    )
    policy_gate = {
        "pass_strict": False,
        "must_redact_count": 2,
        "report": {
            "summary": {"pages_total": 1, "pages_with_issues": 1},
            "pages": {
                "1": {
                    "uncovered_terms": [{"text": "SecretUncovered", "page": 1}],
                }
            },
        },
    }
    (run_dir / "policy_gate.json").write_text(json.dumps(policy_gate), encoding="utf-8")
    (run_dir / "gold_geometry.json").write_text(
        json.dumps({"f1": 0.5, "iou_threshold": 0.85}), encoding="utf-8"
    )
    (run_dir / "ocr_gold.json").write_text(
        json.dumps({"f1": 0.9, "skipped": False}), encoding="utf-8"
    )

    summary = GateSummary()
    summary.policy_gate = policy_gate
    summary.gold_geometry = {"f1": 0.5, "iou_threshold": 0.85}
    summary.ocr_gold = {"f1": 0.9, "skipped": False}

    text = build_app_task_completion_actual_output(
        run_dir=run_dir,
        settings=_settings(),
        document_name="doc.pdf",
        gate_summary=summary,
        gold_case_id="test_case",
    )
    assert text
    assert "Main-app redaction run completed" in text
    assert "pass_strict: False" in text
    assert "SecretUncovered" not in text
    assert "SecretName" not in text


def test_include_term_samples_uses_placeholders(tmp_path, monkeypatch):
    monkeypatch.setenv("APP_EVAL_TASK_COMPLETION_INCLUDE_TERM_SAMPLES", "1")
    run_dir = tmp_path / "run"
    run_dir.mkdir()
    policy_gate = {
        "pass_strict": False,
        "report": {
            "summary": {"pages_total": 1, "pages_with_issues": 1},
            "pages": {
                "1": {
                    "uncovered_terms": [{"text": "HiddenToken", "page": 1}],
                }
            },
        },
    }
    summary = GateSummary()
    summary.policy_gate = policy_gate

    text = build_app_task_completion_actual_output(
        run_dir=run_dir,
        settings=_settings(text_extraction_only=True),
        document_name="doc.pdf",
        gate_summary=summary,
        gold_case_id="test_case",
    )
    assert "HiddenToken" not in text
    assert "[REDACTED len=" in text


def test_app_overall_pass_excludes_task_completion_by_default(monkeypatch):
    monkeypatch.delenv("APP_EVAL_TASK_COMPLETION_AFFECTS_OVERALL", raising=False)
    from post_run_app_eval import _app_eval_overall_pass

    metrics = [
        {"name": "policy_strict", "pass": False, "skipped": False},
        {"name": "task_completion", "pass": False, "skipped": False},
    ]
    assert _app_eval_overall_pass(metrics) is False

    metrics_task_only_fail = [
        {"name": "policy_strict", "pass": True, "skipped": False},
        {"name": "task_completion", "pass": False, "skipped": False},
    ]
    assert _app_eval_overall_pass(metrics_task_only_fail) is True


def test_app_overall_pass_includes_task_when_env_set(monkeypatch):
    monkeypatch.setenv("APP_EVAL_TASK_COMPLETION_AFFECTS_OVERALL", "1")
    from post_run_app_eval import _app_eval_overall_pass

    metrics = [
        {"name": "policy_strict", "pass": True, "skipped": False},
        {"name": "task_completion", "pass": False, "skipped": False},
    ]
    assert _app_eval_overall_pass(metrics) is False