File size: 12,765 Bytes
d82db76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Audit a Limbu book missing-line review queue artifact."""

from __future__ import annotations

import argparse
import csv
import json
import platform
from collections import Counter
from datetime import UTC, datetime
from pathlib import Path
from typing import Any

from ocrtech.manifest import sha256_file


REPORT_JSON = "book-missing-line-review-queue.json"
REVIEW_TSV = "book-missing-line-review-queue.tsv"
REVIEW_MD = "book-missing-line-review-queue.md"
REVIEW_HTML = "book-missing-line-review-dashboard.html"
COMMAND_TXT = "command.txt"
AUDIT_JSON = "book-missing-line-review-queue-audit.json"

REQUIRED_TSV_FIELDS = {
    "book_page_number",
    "book_page_id",
    "book_page_output_dir",
    "book_document_dir",
    "review_priority",
    "review_category",
    "suggested_review_action",
    "candidate_row_id",
    "candidate_sample_id",
    "candidate_line_id",
    "candidate_structural_role",
    "candidate_confidence",
    "max_selected_iou",
    "candidate_bbox",
    "candidate_text",
    "best_selected_text",
    "crop_path",
    "crop_sha256",
    "page_context_path",
    "page_context_sha256",
    "review_status",
    "corrected_text",
    "reviewer",
    "notes",
}


def _read_json_object(path: Path, issues: list[str], *, label: str) -> dict[str, Any] | None:
    if not path.is_file():
        issues.append(f"{label} is missing: {path}")
        return None
    try:
        payload = json.loads(path.read_text(encoding="utf-8"))
    except json.JSONDecodeError as exc:
        issues.append(f"{label} is invalid JSON: {exc}")
        return None
    if not isinstance(payload, dict):
        issues.append(f"{label} must contain a JSON object")
        return None
    return payload


def _read_tsv(path: Path, issues: list[str]) -> tuple[list[dict[str, str]], list[str]]:
    if not path.is_file():
        issues.append(f"review TSV is missing: {path}")
        return [], []
    try:
        with path.open("r", encoding="utf-8", newline="") as handle:
            reader = csv.DictReader(handle, delimiter="\t")
            if reader.fieldnames is None:
                issues.append(f"review TSV has no header: {path}")
                return [], []
            return [dict(row) for row in reader], list(reader.fieldnames)
    except OSError as exc:
        issues.append(f"review TSV could not be read: {type(exc).__name__}: {exc}")
        return [], []


def _check_hash(path: Path, expected: object, issues: list[str], *, label: str) -> str | None:
    if not path.is_file():
        issues.append(f"{label} is missing: {path}")
        return None
    actual = sha256_file(path)
    if not isinstance(expected, str) or not expected:
        issues.append(f"{label} missing recorded SHA-256")
    elif actual != expected:
        issues.append(f"{label} SHA-256 mismatch: recorded={expected} actual={actual} path={path}")
    return actual


def _path_from_report(report: dict[str, Any], key: str, issues: list[str]) -> Path:
    value = report.get(key)
    if not isinstance(value, str) or not value:
        issues.append(f"report missing {key}")
        return Path("")
    return Path(value)


def _audit_rows(rows: list[dict[str, str]], fieldnames: list[str], report: dict[str, Any], issues: list[str]) -> dict[str, Any]:
    missing_fields = sorted(REQUIRED_TSV_FIELDS - set(fieldnames))
    if missing_fields:
        issues.append(f"review TSV missing required fields: {missing_fields}")
    row_ids: set[str] = set()
    category_counts: Counter[str] = Counter()
    priority_counts: Counter[str] = Counter()
    pending_count = 0
    accepted_count = 0
    crop_count = 0
    page_context_count = 0
    for index, row in enumerate(rows, start=2):
        row_id = str(row.get("candidate_row_id") or "").strip()
        if not row_id:
            issues.append(f"review TSV row {index} missing candidate_row_id")
        elif row_id in row_ids:
            issues.append(f"review TSV duplicate candidate_row_id: {row_id}")
        row_ids.add(row_id)
        page_id = str(row.get("book_page_id") or "").strip()
        candidate_sample_id = str(row.get("candidate_sample_id") or "").strip()
        if not page_id:
            issues.append(f"review TSV row {index} missing book_page_id")
        if candidate_sample_id != page_id:
            issues.append(
                f"review TSV row {index} candidate_sample_id does not match book_page_id: "
                f"{candidate_sample_id!r} != {page_id!r}"
            )
        status = str(row.get("review_status") or "").strip().lower()
        if status == "pending":
            pending_count += 1
        elif status in {"accepted", "approved", "corrected", "verified", "done"}:
            accepted_count += 1
            if not str(row.get("corrected_text") or "").strip():
                issues.append(f"review TSV row {index} accepted without corrected_text")
        else:
            issues.append(f"review TSV row {index} has unsupported review_status: {status!r}")
        category = str(row.get("review_category") or "").strip()
        if not category:
            issues.append(f"review TSV row {index} missing review_category")
        else:
            category_counts[category] += 1
        priority = str(row.get("review_priority") or "").strip()
        if not priority:
            issues.append(f"review TSV row {index} missing review_priority")
        else:
            priority_counts[priority] += 1
        crop_path = Path(str(row.get("crop_path") or ""))
        expected_crop_sha = str(row.get("crop_sha256") or "")
        if not crop_path.is_file():
            issues.append(f"review TSV row {index} crop_path is missing: {crop_path}")
        else:
            crop_count += 1
            actual_crop_sha = sha256_file(crop_path)
            if actual_crop_sha != expected_crop_sha:
                issues.append(
                    f"review TSV row {index} crop SHA-256 mismatch: "
                    f"recorded={expected_crop_sha} actual={actual_crop_sha} path={crop_path}"
                )
        page_context_path_text = str(row.get("page_context_path") or "").strip()
        page_context_sha = str(row.get("page_context_sha256") or "").strip()
        if page_context_path_text or page_context_sha:
            page_context_path = Path(page_context_path_text)
            if not page_context_path.is_file():
                issues.append(f"review TSV row {index} page_context_path is missing: {page_context_path}")
            else:
                page_context_count += 1
                actual_context_sha = sha256_file(page_context_path)
                if actual_context_sha != page_context_sha:
                    issues.append(
                        f"review TSV row {index} page context SHA-256 mismatch: "
                        f"recorded={page_context_sha} actual={actual_context_sha} path={page_context_path}"
                    )
    if report.get("review_row_count") != len(rows):
        issues.append(f"review_row_count mismatch: recorded={report.get('review_row_count')} actual={len(rows)}")
    recorded_category_counts = report.get("category_counts")
    if recorded_category_counts != dict(sorted(category_counts.items())):
        issues.append(f"category_counts mismatch: recorded={recorded_category_counts} actual={dict(sorted(category_counts.items()))}")
    recorded_priority_counts = report.get("priority_counts")
    if recorded_priority_counts != dict(sorted(priority_counts.items())):
        issues.append(f"priority_counts mismatch: recorded={recorded_priority_counts} actual={dict(sorted(priority_counts.items()))}")
    recorded_page_context_count = report.get("page_context_count")
    if recorded_page_context_count is not None and recorded_page_context_count != page_context_count:
        issues.append(f"page_context_count mismatch: recorded={recorded_page_context_count} actual={page_context_count}")
    return {
        "row_count": len(rows),
        "pending_count": pending_count,
        "accepted_count": accepted_count,
        "crop_count": crop_count,
        "page_context_count": page_context_count,
        "category_counts": dict(sorted(category_counts.items())),
        "priority_counts": dict(sorted(priority_counts.items())),
    }


def audit_queue(queue_dir: str | Path, audit_dir: str | Path | None = None) -> dict[str, Any]:
    queue_root = Path(queue_dir)
    issues: list[str] = []
    warnings: list[str] = []
    report_path = queue_root / REPORT_JSON
    report = _read_json_object(report_path, issues, label="book missing-line queue report")
    if report is None:
        audit = _audit_report(queue_root, report_path, issues, warnings, {})
        _write_audit(audit, Path(audit_dir) if audit_dir is not None else queue_root)
        return audit
    if report.get("tool") != "tools/prepare_limbu_book_missing_line_review_queue.py":
        issues.append(f"unexpected report tool: {report.get('tool')!r}")
    if report.get("out") != str(queue_root):
        issues.append(f"report out mismatch: recorded={report.get('out')!r} actual={str(queue_root)!r}")
    book_run = _path_from_report(report, "book_run", issues)
    candidate_review_file = _path_from_report(report, "candidate_review_file", issues)
    review_tsv = _path_from_report(report, "review_queue_tsv", issues)
    review_md = _path_from_report(report, "review_queue_md", issues)
    review_dashboard_value = report.get("review_dashboard")
    command = _path_from_report(report, "command", issues)
    _check_hash(book_run, report.get("book_run_sha256"), issues, label="book-run.json")
    _check_hash(candidate_review_file, report.get("candidate_review_file_sha256"), issues, label="candidate review file")
    _check_hash(review_tsv, report.get("review_queue_tsv_sha256"), issues, label="review TSV")
    _check_hash(review_md, report.get("review_queue_md_sha256"), issues, label="review Markdown")
    if isinstance(review_dashboard_value, str) and review_dashboard_value:
        _check_hash(Path(review_dashboard_value), report.get("review_dashboard_sha256"), issues, label="review dashboard")
    else:
        warnings.append("report does not include a review dashboard")
    _check_hash(command, report.get("command_sha256"), issues, label="command.txt")
    rows, fieldnames = _read_tsv(review_tsv, issues)
    row_summary = _audit_rows(rows, fieldnames, report, issues)
    recorded_dashboard_cards = report.get("review_dashboard_card_count")
    if recorded_dashboard_cards is not None and recorded_dashboard_cards != row_summary.get("row_count"):
        issues.append(
            "review_dashboard_card_count mismatch: "
            f"recorded={recorded_dashboard_cards} actual={row_summary.get('row_count')}"
        )
    boundary = str(report.get("boundary") or "")
    if "review evidence only" not in boundary:
        warnings.append("report boundary does not explicitly say review evidence only")
    if any(row.get("review_status") != "pending" for row in rows):
        warnings.append("review queue contains non-pending rows; regenerate a fresh queue after review before treating report hashes as current")
    audit = _audit_report(queue_root, report_path, issues, warnings, row_summary)
    _write_audit(audit, Path(audit_dir) if audit_dir is not None else queue_root)
    return audit


def _audit_report(
    queue_dir: Path,
    report_path: Path,
    issues: list[str],
    warnings: list[str],
    row_summary: dict[str, Any],
) -> dict[str, Any]:
    return {
        "created_at_utc": datetime.now(UTC).isoformat(),
        "machine": platform.node(),
        "tool": "tools/audit_limbu_book_missing_line_review_queue.py",
        "queue_dir": str(queue_dir),
        "report": str(report_path),
        "report_sha256": sha256_file(report_path) if report_path.is_file() else None,
        "audit_passed": not issues,
        "operational_review_queue_safe": not issues and row_summary.get("accepted_count", 0) == 0,
        "issues": issues,
        "warnings": warnings,
        **row_summary,
    }


def _write_audit(audit: dict[str, Any], out: Path) -> None:
    out.mkdir(parents=True, exist_ok=True)
    (out / AUDIT_JSON).write_text(json.dumps(audit, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--queue-dir", type=Path, required=True)
    parser.add_argument("--audit-dir", type=Path)
    args = parser.parse_args(argv)
    result = audit_queue(args.queue_dir, args.audit_dir)
    print(f"audit_passed={result['audit_passed']} issues={len(result['issues'])}")
    return 0 if result["audit_passed"] else 1


if __name__ == "__main__":
    raise SystemExit(main())