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ea
37cb069
 
 
 
 
 
 
 
 
 
470bcea
37cb069
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470bcea
37cb069
 
 
 
 
 
 
470bcea
37cb069
 
 
 
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
"""Redact internal reviews into publishable ICSAC-branded review artifacts.

Operates on the authoritative internal review markdown in ``reviews/`` and
emits a sanitized version that is safe to publish on icsacinstitute.org.

The redaction removes all vendor/model identifiers, renames reviewers
generically ("Reviewer 1", "Reviewer 2", ...), drops internal workflow
detail (raw API error payloads, slot indices, fallback chains), and
replaces the disagreement flag with a human-readable consensus label.

A grep-gate (``assert_clean``) fails hard if any forbidden token survives
redacting. Callers must catch the exception and abort publication.
"""

from __future__ import annotations

import os
import re
from dataclasses import dataclass, field


# Hard-fail vendor/model identifiers. Any case-insensitive substring hit
# indicates a leak of panel composition and must abort publication.
#
# Tokens chosen to catch identity leaks (OpenRouter route paths, specific
# model family IDs, vendor names rarely legitimate in academic prose)
# WITHOUT catching subject-matter discussion of published transformers.
# A paper reviewing "GPT-2 and Gemma-2 activations" must pass the gate β€”
# those are scientific subjects, not panel self-references.
FORBIDDEN_VENDOR_TOKENS: tuple[str, ...] = (
    # Infrastructure names β€” never appear in legitimate academic prose.
    "openrouter",
    "anthropic",
    # OpenRouter route prefixes β€” the "/" guarantees a path, not a word.
    "openai/",
    "nvidia/",
    "google/gemma",
    "meta-llama/",
    "z-ai/",
    "minimax/",
    "nousresearch/",
    "qwen/",
    "mistralai/",
    "deepseek/",
    "cognitivecomputations/",
    "liquid/",
    # Specific panel model families β€” narrow enough that a hit is a leak.
    "nemotron",
    "gpt-oss",
    # Panelist name β€” small false-positive risk ("Claude Shannon") accepted
    # to catch "As Claude, I..." self-reference leaks from slot 0.
    "claude",
)

# Hard-fail credential/infra phrases. Substring match, case-insensitive.
# These are structurally always leaks β€” there's no legitimate prose
# reason for a paper review to contain these compounds.
FORBIDDEN_SECRET_PHRASES: tuple[str, ...] = (
    "api key",
    "api keys",
    "access token",
    "auth token",
    "auth key",
    "bearer token",
    "secret key",
    "private key",
    "api token",
    "api tokens",
    "bearer ",
)

# Soft-warn tokens β€” bare "key", "api", "token", "google" that appear
# regularly in academic prose ("key findings", "Google Scholar", "tokenizer").
# Surfaced in the redaction report but do not abort. Operators can grep the
# published review manually if they want extra assurance.
SOFT_WARN_TOKENS: tuple[str, ...] = (
    "google",
    "api",
    "token",
    "key",
)

# Regex patterns indicating attempted exfiltration via review output.
# Added 2026-04-18 after prompt-injection attack-surface audit. Triggered
# by file paths pointing at our hosts, env-var assignments, and known
# credential prefixes. Match anywhere in the redacted review text.
FORBIDDEN_EXFIL_PATTERNS: tuple[str, ...] = (
    # Absolute filesystem paths likely pointing at our hosts
    r"/home/orangepi\b",
    r"/home/dietpi\b",
    r"/opt/orchestrator\b",
    r"/etc/passwd\b",
    r"/etc/shadow\b",
    r"/root/",
    r"\.config/[a-z][a-z0-9_-]*\.env\b",
    r"C:\\\\Users\\\\",
    # Env-var assignments of the form UPPER_SNAKE=longvalue
    r"\b[A-Z][A-Z0-9_]{3,}=\S{8,}",
    # Known credential prefixes
    r"\bsk-ant-api03-[A-Za-z0-9_-]{8,}",
    r"\bsk-[A-Za-z0-9]{20,}",
    r"\bghp_[A-Za-z0-9]{10,}",
    r"\bgho_[A-Za-z0-9]{10,}",
    r"\bAKIA[0-9A-Z]{16}\b",
    # Bearer tokens of non-trivial length following the keyword
    r"\bBearer\s+[A-Za-z0-9._-]{32,}",
    # Internal rubric filenames β€” reviewers and the RQC auditor occasionally
    # echo filenames from the prompt ("drift from tone.md"). Public output
    # must reference rubrics by prose name, never by repo filename. Rewriting
    # pass runs first (_rewrite_rubric_filenames); this gate catches anything
    # that slipped through.
    r"\b(?:rubrics/)?(?:scope|methodology|ai-provenance|tone|calibration|review_quality_control)\.md\b",
)


# --------------------------------------------------------------------------
# Rubric filename β†’ prose rewrite
# --------------------------------------------------------------------------
#
# The RQC rubric references sibling rubrics by filename ("the standard
# mirrors tone.md"). Audit justifications inherit that phrasing and leak
# internal filenames into public-facing text ("a soft but consistent drift
# from tone.md"). Rewrite before rendering; the hard-gate above catches any
# new filename that isn't in this map so a future rubric addition can't
# silently leak.
RUBRIC_FILENAME_PROSE: tuple[tuple[str, str], ...] = (
    ("rubrics/review_quality_control.md", "the audit rubric"),
    ("rubrics/ai-provenance.md", "the AI provenance rubric"),
    ("rubrics/calibration.md", "the calibration rubric"),
    ("rubrics/methodology.md", "the methodology rubric"),
    ("rubrics/scope.md", "the scope rubric"),
    ("rubrics/tone.md", "the tone rubric"),
    ("review_quality_control.md", "the audit rubric"),
    ("ai-provenance.md", "the AI provenance rubric"),
    ("calibration.md", "the calibration rubric"),
    ("methodology.md", "the methodology rubric"),
    ("scope.md", "the scope rubric"),
    ("tone.md", "the tone rubric"),
)


def _rewrite_rubric_filenames(text: str) -> str:
    """Rewrite rubric filename references to prose descriptions."""
    if not text:
        return text
    out = text
    for needle, prose in RUBRIC_FILENAME_PROSE:
        pattern = re.compile(re.escape(needle), re.IGNORECASE)
        out = pattern.sub(prose, out)
    return out


@dataclass
class ParsedReview:
    """Structured view of a reviews/<id>_*.md file."""

    record_id: str
    title: str
    doi: str
    review_date: str
    recommendation: str
    disagreement: bool
    dimension_rows: list[tuple[str, str, list[str]]] = field(default_factory=list)
    reviewers: list[dict] = field(default_factory=list)


def _parse_frontmatter(body: str) -> tuple[dict, str]:
    """Strip YAML frontmatter; return (fields, remainder)."""
    if not body.startswith("---\n"):
        return {}, body
    end = body.find("\n---\n", 4)
    if end < 0:
        return {}, body
    raw = body[4:end]
    rest = body[end + 5 :]
    fields: dict = {}
    for line in raw.splitlines():
        if ":" not in line:
            continue
        k, v = line.split(":", 1)
        fields[k.strip()] = v.strip().strip('"').strip("'")
    return fields, rest


def _parse_aggregate_table(body: str) -> list[tuple[str, str, list[str]]]:
    """Pull rows out of the 'Aggregate Scores' markdown table."""
    rows: list[tuple[str, str, list[str]]] = []
    in_table = False
    for line in body.splitlines():
        stripped = line.strip()
        if stripped.startswith("## Aggregate Scores"):
            in_table = True
            continue
        if in_table and stripped.startswith("## "):
            break
        if not in_table or not stripped.startswith("|"):
            continue
        if set(stripped.replace("|", "").strip()) <= set("- "):
            continue
        cells = [c.strip() for c in stripped.strip("|").split("|")]
        if len(cells) < 3 or cells[0].lower() == "dimension":
            continue
        scores = [s.strip() for s in cells[2].split(",") if s.strip()]
        rows.append((cells[0], cells[1], scores))
    return rows


def _split_reviewer_sections(body: str) -> list[tuple[str, str]]:
    """Extract [(heading, content), ...] for each '### <Model>' block."""
    marker = "\n## Individual Model Reviews\n"
    idx = body.find(marker)
    if idx < 0:
        return []
    remainder = body[idx + len(marker) :]
    end = remainder.find("\n---\n")
    if end >= 0:
        remainder = remainder[:end]
    sections: list[tuple[str, str]] = []
    current_head: str | None = None
    current_lines: list[str] = []
    for line in remainder.splitlines():
        if line.startswith("### "):
            if current_head is not None:
                sections.append((current_head, "\n".join(current_lines).strip()))
            current_head = line[4:].strip()
            current_lines = []
        else:
            current_lines.append(line)
    if current_head is not None:
        sections.append((current_head, "\n".join(current_lines).strip()))
    return sections


def _parse_reviewer_body(content: str) -> dict:
    """Pull recommendation, summary, dimension scores out of one section."""
    if content.startswith("**Error:**"):
        return {"error": True}
    rec_match = re.search(r"\*\*Recommendation:\*\*\s*([A-Z_]+)", content)
    sum_match = re.search(r"\*\*Summary:\*\*\s*(.+?)(?:\n\n|\Z)", content, re.S)
    dims: list[tuple[str, str, str]] = []
    for m in re.finditer(
        r"^-\s+\*\*(?P<label>[^*]+)\*\*\s*\((?P<score>[^)]+)\):\s*(?P<just>.+?)(?=\n-\s+\*\*|\Z)",
        content,
        re.S | re.M,
    ):
        dims.append(
            (
                m.group("label").strip(),
                m.group("score").strip(),
                " ".join(m.group("just").split()),
            )
        )
    return {
        "error": False,
        "recommendation": rec_match.group(1) if rec_match else "N/A",
        "summary": " ".join(sum_match.group(1).split()) if sum_match else "",
        "dimensions": dims,
    }


def parse_review_file(path: str) -> ParsedReview:
    """Load a reviews/<id>_*.md file into structured form."""
    with open(path, "r", encoding="utf-8") as f:
        text = f.read()
    fm, body = _parse_frontmatter(text)
    sections = _split_reviewer_sections(body)
    reviewers = [
        {"raw_heading": head, **_parse_reviewer_body(cont)} for head, cont in sections
    ]
    title = fm.get("title", "").strip('"')
    if title.lower().startswith("review:"):
        title = title.split(":", 1)[1].strip()
    return ParsedReview(
        record_id=str(fm.get("record_id", "")),
        title=title,
        doi=fm.get("doi", ""),
        review_date=fm.get("review_date", ""),
        recommendation=fm.get("recommendation", "REVIEW_FURTHER"),
        disagreement=str(fm.get("disagreement", "False")).lower() == "true",
        dimension_rows=_parse_aggregate_table(body),
        reviewers=reviewers,
    )


def _consensus_label(parsed: ParsedReview) -> str:
    """Translate disagreement + score spread into reader-friendly label."""
    max_spread = 0
    for _, _, scores in parsed.dimension_rows:
        nums = [float(s) for s in scores if re.match(r"^\d+(\.\d+)?$", s)]
        if len(nums) >= 2:
            max_spread = max(max_spread, max(nums) - min(nums))
    if not parsed.disagreement and max_spread <= 1:
        return "strong consensus"
    if max_spread >= 2:
        return "divided"
    return "mixed"


def build_public_markdown(parsed: ParsedReview) -> str:
    """Render the sanitized review markdown (safe for public publication)."""
    valid_reviewers = [r for r in parsed.reviewers if not r["error"]]
    valid_n = len(valid_reviewers)
    consensus = _consensus_label(parsed)

    lines: list[str] = [
        "---",
        f'title: "Review: {parsed.title}"',
        f'doi: "{parsed.doi}"',
        f"record_id: {parsed.record_id}",
        f"review_date: {parsed.review_date}",
        f"recommendation: {parsed.recommendation}",
        f"consensus: {consensus}",
        f"reviewer_count: {valid_n}",
        "---",
        "",
        "## Open review",
        "",
        (
            f"This submission was evaluated by a panel of {valid_n} independent "
            f"advanced AI reviewers scoring six dimensions. Panel consensus was "
            f"**{consensus}**."
        ),
        "",
        "### Aggregate scores",
        "",
        "| Dimension | Mean | Per-reviewer |",
        "|-----------|------|--------------|",
    ]
    for label, mean, scores in parsed.dimension_rows:
        lines.append(f"| {label} | {mean} | {', '.join(scores) or 'β€”'} |")

    lines.extend([
        "",
        "### Reviewer assessments",
        "",
        (
            "Individual reviewer assessments are collapsed by default. "
            "Expand any row to read that reviewer's summary "
            "and per-dimension justification."
        ),
        "",
    ])
    # Emit raw HTML for reviewer blocks so we can use <details> for
    # collapsibility. Python-markdown passes block-level HTML through
    # unchanged, so the rendered landing page gets native browser-handled
    # expand/collapse on each reviewer without any JavaScript.
    import html as _html
    for idx, r in enumerate(valid_reviewers, start=1):
        rec = _html.escape(r["recommendation"])
        summary = _html.escape(_rewrite_rubric_filenames(r["summary"]))
        lines.append(f'<details class="reviewer-detail">')
        lines.append(f'<summary><strong>Reviewer {idx}</strong> β€” {rec}</summary>')
        lines.append("")
        lines.append(f'<p><strong>Summary:</strong> {summary}</p>')
        if r["dimensions"]:
            lines.append("<ul>")
            for label, score, just in r["dimensions"]:
                lines.append(
                    f'  <li><strong>{_html.escape(label)}</strong> '
                    f'({_html.escape(score)}): {_html.escape(_rewrite_rubric_filenames(just))}</li>'
                )
            lines.append("</ul>")
        lines.append("</details>")
        lines.append("")

    lines.extend(
        [
            "---",
            "",
            (
                "*Reviews at ICSAC are open and transparent. AI tooling helps "
                "the panel draft and structure each review; final acceptance "
                "decisions rest with human editors. Reviews are published "
                "alongside acceptance for accountability; individual reviewer "
                "identities are abstracted to keep focus on the assessment "
                "rather than the tooling behind it.*"
            ),
            "",
        ]
    )
    return _humanize_internal_jargon("\n".join(lines))


def _find_substring_hits(text: str, tokens: tuple[str, ...]) -> list[tuple[str, int]]:
    hits: list[tuple[str, int]] = []
    lowered = text.lower()
    for tok in tokens:
        start = 0
        while True:
            at = lowered.find(tok, start)
            if at < 0:
                break
            hits.append((tok, at))
            start = at + 1
    return hits


def _find_wordboundary_hits(text: str, tokens: tuple[str, ...]) -> list[tuple[str, int]]:
    hits: list[tuple[str, int]] = []
    for tok in tokens:
        for m in re.finditer(rf"\b{re.escape(tok)}\b", text, flags=re.IGNORECASE):
            hits.append((tok, m.start()))
    return hits


@dataclass
class ScrubReport:
    fatal_hits: list[tuple[str, int]]
    warn_hits: list[tuple[str, int]]

    @property
    def clean(self) -> bool:
        return not self.fatal_hits


class RedactionLeak(Exception):
    """Raised when a redacted artifact still contains a fatal token."""

    def __init__(self, hits: list[tuple[str, int]], artifact_path: str | None):
        self.hits = hits
        self.artifact_path = artifact_path
        preview = ", ".join(sorted({t for t, _ in hits}))
        loc = f" in {artifact_path}" if artifact_path else ""
        super().__init__(
            f"Redacted review leaked forbidden tokens{loc}: {preview} "
            f"({len(hits)} total hit(s))"
        )



def _find_regex_hits(text: str, patterns: tuple[str, ...]) -> list[tuple[str, int]]:
    """Return [(matched_text, offset), ...] for each regex pattern that matches."""
    hits: list[tuple[str, int]] = []
    for pat in patterns:
        for m in re.finditer(pat, text):
            hits.append((f"[regex {pat!r}] {m.group(0)[:80]}", m.start()))
    return hits

_REVIEWER_DETAIL_BLOCK = re.compile(
    r'<details class="reviewer-detail">.*?</details>',
    re.DOTALL,
)


_JARGON_REWRITES = (
    (re.compile(r"\bSlots\b"), "Reviewers"),
    (re.compile(r"\bslots\b"), "reviewers"),
    (re.compile(r"\bSlot\b"), "Reviewer"),
    (re.compile(r"\bslot\b"), "reviewer"),
)


def _humanize_internal_jargon(text: str) -> str:
    """Standardize public-facing language on "reviewer" instead of "slot".

    Pipeline configuration calls each panel position a "slot"; reviewers
    occasionally echo the term when describing peer assessments. For
    public output we rewrite to "reviewer" so the report matches the
    institute's published panel description and never exposes the
    pipeline's internal naming.
    """
    for pat, repl in _JARGON_REWRITES:
        text = pat.sub(repl, text)
    return text


def _strip_reviewer_prose(text: str) -> str:
    """Remove <details class="reviewer-detail"> blocks before vendor-token
    screening.

    Reviewer-detail blocks hold per-reviewer summary + dimension
    justifications β€” prose that LEGITIMATELY describes what the submission
    itself says, including author disclosures of AI-assisted writing or
    references to prior-art papers (e.g. OpenAI/Gemma citations). A
    reviewer noting "the author acknowledges Claude/Gemini as writing
    assistants" is factual description, not panel-composition leak.

    Structural content β€” frontmatter, section headers, the aggregate
    scores table, the boilerplate footer β€” still receives the full vendor
    check. Secret and exfil patterns are checked across the whole text.
    """
    return _REVIEWER_DETAIL_BLOCK.sub("", text)


def scan(text: str) -> ScrubReport:
    """Return a hit report without raising."""
    structural = _strip_reviewer_prose(text)
    fatal = _find_substring_hits(structural, FORBIDDEN_VENDOR_TOKENS)
    fatal.extend(_find_substring_hits(text, FORBIDDEN_SECRET_PHRASES))
    fatal.extend(_find_regex_hits(text, FORBIDDEN_EXFIL_PATTERNS))
    warn = _find_wordboundary_hits(text, SOFT_WARN_TOKENS)
    return ScrubReport(fatal_hits=fatal, warn_hits=warn)


def assert_clean(text: str, artifact_path: str | None = None) -> ScrubReport:
    """Grep-gate: raise RedactionLeak on any fatal hit; return the scan report."""
    report = scan(text)
    if report.fatal_hits:
        raise RedactionLeak(report.fatal_hits, artifact_path)
    return report


def _strip_frontmatter(md: str) -> str:
    """Drop the YAML frontmatter from a markdown string for HTML rendering."""
    if not md.startswith("---\n"):
        return md
    end = md.find("\n---\n", 4)
    if end < 0:
        return md
    return md[end + 5 :]


def render_public_html(public_md: str) -> str:
    """Render redacted markdown into an HTML fragment for the landing page.

    Uses python-markdown with the 'tables' extension; falls back to a
    preformatted-text dump if markdown is not importable.
    """
    body = _strip_frontmatter(public_md)
    try:
        import markdown as _md

        return _md.markdown(body, extensions=["tables"])
    except ImportError:
        import html as _html

        return f"<pre>{_html.escape(body)}</pre>"


def publish_public_review(
    record_id: str,
    reviews_dir: str,
    website_repo: str,
) -> str:
    """Redact reviews/<id>_*.md and write md + html to website repo public-reviews/.

    Returns the written .md path. Raises RedactionLeak if grep-gate trips on
    either the markdown source or the rendered HTML.
    """
    # Exclude artifacts that are not the primary review markdown.
    # Both RQC and citation reports live alongside in reviews/ as
    # <id>_review_quality_control.md and <id>_citations.md; including
    # them here would cause sorted()[-1] to silently pick the wrong
    # source for any paper whose slug sorts before "review_quality_control".
    _NON_REVIEW_SUFFIXES = ("_review_quality_control.md", "_citations.md")
    matches = [
        f for f in os.listdir(reviews_dir)
        if f.startswith(f"{record_id}_") and f.endswith(".md")
        and not any(f.endswith(s) for s in _NON_REVIEW_SUFFIXES)
    ]
    if not matches:
        raise FileNotFoundError(
            f"No review markdown found for record_id={record_id} in {reviews_dir}"
        )
    src = os.path.join(reviews_dir, sorted(matches)[-1])
    parsed = parse_review_file(src)
    public_md = build_public_markdown(parsed)
    report_md = assert_clean(public_md, artifact_path=src)

    public_html = render_public_html(public_md)
    assert_clean(public_html, artifact_path=f"{src} (rendered html)")

    out_dir = os.path.join(website_repo, "src", "data", "public-reviews")
    os.makedirs(out_dir, exist_ok=True)
    md_path = os.path.join(out_dir, f"{record_id}.md")
    html_path = os.path.join(out_dir, f"{record_id}.html")
    with open(md_path, "w", encoding="utf-8") as f:
        f.write(public_md)
    with open(html_path, "w", encoding="utf-8") as f:
        f.write(public_html)
    if report_md.warn_hits:
        uniq = sorted({t for t, _ in report_md.warn_hits})
        print(
            f"  redaction: {len(report_md.warn_hits)} soft-warn hit(s) "
            f"({', '.join(uniq)}) β€” inspect {md_path} if unexpected."
        )
    return md_path


# --------------------------------------------------------------------------
# Review Quality Control β€” public redaction
# --------------------------------------------------------------------------

# The RQC rubric defines five dimensions. The first four are publishable;
# ``injection_indicators`` is INTERNAL ONLY and must never appear in any
# file written under src/data/public-reviews/. See rubrics/review_quality_control.md.
RQC_PUBLIC_DIMENSIONS: tuple[str, ...] = (
    "Rubric Adherence",
    "Internal Consistency",
    "Specificity",
    "Tone",
)
RQC_INJECTION_DIM_LABEL = "Injection Indicators"

# Substring-guard against the redacted dimension leaking into public text.
# Match case-insensitively on the prose label AND any plausible variants.
RQC_FORBIDDEN_PUBLIC_TOKENS: tuple[str, ...] = (
    "injection_indicators",
    "injection indicators",
    "injection indicator",
    "prompt injection",
    "prompt-injection",
    "prompt_injection",
)


@dataclass
class ParsedRQC:
    """Structured view of a reviews/<id>_review_quality_control.md file."""

    record_id: str
    title: str
    doi: str
    audit_date: str
    flag: bool
    summary: str
    overall_concerns: list[str] = field(default_factory=list)
    # Each slot: {"reviewer": str, "errored": bool,
    #             "dimensions": [(label, score, justification), ...]}
    slots: list[dict] = field(default_factory=list)


def _parse_rqc_slots(body: str) -> list[dict]:
    """Parse '### Reviewer N' sections out of the Per-slot audit block."""
    marker = "\n## Per-slot audit\n"
    idx = body.find(marker)
    if idx < 0:
        return []
    remainder = body[idx + len(marker):]
    end = remainder.find("\n---\n")
    if end >= 0:
        remainder = remainder[:end]

    sections: list[tuple[str, str]] = []
    current_head: str | None = None
    current_lines: list[str] = []
    for line in remainder.splitlines():
        if line.startswith("### "):
            if current_head is not None:
                sections.append((current_head, "\n".join(current_lines).strip()))
            current_head = line[4:].strip()
            current_lines = []
        else:
            current_lines.append(line)
    if current_head is not None:
        sections.append((current_head, "\n".join(current_lines).strip()))

    slots: list[dict] = []
    for head, content in sections:
        errored = content.lstrip().startswith("*Errored:")
        dims: list[tuple[str, str, str]] = []
        if not errored:
            for m in re.finditer(
                r"^-\s+\*\*(?P<label>[^*]+)\*\*\s*\((?P<score>[^)]+)\):\s*"
                r"(?P<just>.+?)(?=\n-\s+\*\*|\Z)",
                content,
                re.S | re.M,
            ):
                dims.append(
                    (
                        m.group("label").strip(),
                        m.group("score").strip(),
                        " ".join(m.group("just").split()),
                    )
                )
        slots.append({"reviewer": head, "errored": errored, "dimensions": dims})
    return slots


def parse_rqc_file(path: str) -> ParsedRQC:
    """Load a reviews/<id>_review_quality_control.md file into structured form."""
    with open(path, "r", encoding="utf-8") as f:
        text = f.read()
    fm, body = _parse_frontmatter(text)
    title = fm.get("title", "").strip('"')
    if title.lower().startswith("review quality control:"):
        title = title.split(":", 1)[1].strip()

    flag_raw = str(fm.get("review_quality_control_flag", "false")).lower()
    flag = flag_raw == "true"

    # Pull the summary paragraph between "## Summary" and the next "## " header
    summary = ""
    m = re.search(r"^##\s+Summary\s*\n(.+?)(?=^##\s+|\Z)", body, re.S | re.M)
    if m:
        summary = " ".join(m.group(1).split())

    concerns: list[str] = []
    m = re.search(r"^##\s+Overall concerns\s*\n(.+?)(?=^##\s+|\Z)", body, re.S | re.M)
    if m:
        for line in m.group(1).splitlines():
            line = line.strip()
            if line.startswith("- "):
                concerns.append(line[2:].strip())

    return ParsedRQC(
        record_id=str(fm.get("record_id", "")),
        title=title,
        doi=fm.get("doi", ""),
        audit_date=fm.get("audit_date", ""),
        flag=flag,
        summary=summary,
        overall_concerns=concerns,
        slots=_parse_rqc_slots(body),
    )


def _flag_cause_is_injection_only(parsed: ParsedRQC) -> bool:
    """True iff flag=true AND no scholarly dimension scored <=2.

    When the only trigger is the internal injection_indicators dimension,
    the public rendering shows a generic operator-review note rather than
    identifying which scholarly dim tripped it.
    """
    if not parsed.flag:
        return False
    for slot in parsed.slots:
        if slot["errored"]:
            continue
        for label, score, _ in slot["dimensions"]:
            if label == RQC_INJECTION_DIM_LABEL:
                continue
            try:
                n = int(re.match(r"(\d+)", score).group(1))
            except Exception:
                continue
            if n <= 2:
                return False
    return True


def build_public_rqc_markdown(parsed: ParsedRQC) -> str:
    """Render the redacted RQC markdown. Omits injection_indicators entirely.

    Contract: the returned string contains no reference to the
    injection_indicators dimension under any spelling. ``assert_rqc_clean``
    enforces this before the redaction writes anything to the site.
    """
    status_line = (
        "Review Quality Control: flagged β€” reviewed by human editors before acceptance."
        if parsed.flag
        else "Review Quality Control: passed."
    )

    lines: list[str] = [
        "---",
        f'title: "Review Quality Control: {parsed.title}"',
        f'doi: "{parsed.doi}"',
        f"record_id: {parsed.record_id}",
        f"audit_date: {parsed.audit_date}",
        f"review_quality_control_flag: {str(parsed.flag).lower()}",
        "---",
        "",
        "## Review Quality Control",
        "",
        f"**{status_line}**",
        "",
        (
            "This audit quality checks each AI reviewer's assessment for "
            "rubric adherence, internal consistency, specificity, and "
            "institutional voice. It is published alongside the panel review "
            "so the quality of the review process is as auditable as the "
            "review itself."
        ),
        "",
    ]

    # When flag was tripped only by the internal dimension, say so generically.
    if parsed.flag and _flag_cause_is_injection_only(parsed):
        lines.extend([
            (
                "The audit surfaced a concern outside the four scholarly "
                "dimensions above. A human editor reviewed the panel output "
                "before the acceptance decision was recorded."
            ),
            "",
        ])
    elif parsed.overall_concerns:
        # Concerns may reference the redacted injection dimension, name
        # specific reviewer numbers that won't match the public 1..N
        # renumbering (we drop errored slots from public view), or call
        # out pipeline-level mechanics that are operator noise rather than
        # author-facing scholarly concerns. Filter aggressively.
        operator_noise_patterns = (
            "pipeline-level", "pipeline level", "slot-run", "slot run",
            "errored slot", "errored slots", "slot errored", "reviewer defect",
            "operator", "panel composition", "pass 1", "pass 2", "pass 3",
        )
        safe_concerns = []
        for c in parsed.overall_concerns:
            cl = c.lower()
            if any(tok in cl for tok in RQC_FORBIDDEN_PUBLIC_TOKENS):
                continue
            if any(tok in cl for tok in operator_noise_patterns):
                continue
            # Concerns that name specific panel members by vendor identity
            # are panel-composition leaks, not author-facing scholarly
            # concerns. Drop them rather than try to rewrite.
            if any(tok in cl for tok in FORBIDDEN_VENDOR_TOKENS):
                continue
            # Drop concerns that cite specific reviewer numbers β€” our public
            # renumbering (valid-only 1..N) won't line up with whatever
            # index the audit claude-p referenced in its internal output.
            # Singular AND plural forms both count.
            if re.search(r"\breviewers?\s+\d+\b", cl):
                continue
            safe_concerns.append(c)
        if safe_concerns:
            lines.extend(["### Notes", ""])
            for c in safe_concerns:
                lines.append(f"- {_rewrite_rubric_filenames(c)}")
            lines.append("")

    # Public audit shows only valid-slot outputs, renumbered 1..N to match
    # the open review's reviewer count. Errored slots are operator-layer
    # signal; surfacing them publicly creates a count mismatch with the
    # review section and invites reader confusion.
    valid_slots = [s for s in parsed.slots if not s["errored"]]

    lines.extend(["### Reviewer Quality Control Audit", ""])

    # Condensed table: reviewer Γ— four scholarly dimensions.
    lines.append(
        "| Reviewer | " + " | ".join(RQC_PUBLIC_DIMENSIONS) + " |"
    )
    lines.append(
        "|----------|" + "|".join(["----"] * len(RQC_PUBLIC_DIMENSIONS)) + "|"
    )
    for idx, slot in enumerate(valid_slots, start=1):
        label = f"Reviewer {idx}"
        by_label = {lbl: (score, just) for lbl, score, just in slot["dimensions"]}
        cells = [label]
        for dim in RQC_PUBLIC_DIMENSIONS:
            score, _ = by_label.get(dim, ("β€”", ""))
            cells.append(score)
        lines.append("| " + " | ".join(cells) + " |")

    lines.append("")
    # Detail block per valid reviewer, collapsed by default. Scholarly dims
    # only; injection_indicators is redacted upstream. Browser-native
    # <details> means no JavaScript and works with the site's scoped CSS.
    import html as _html
    for idx, slot in enumerate(valid_slots, start=1):
        label = f"Reviewer {idx}"
        by_label = {lbl: (score, just) for lbl, score, just in slot["dimensions"]}
        lines.append(f'<details class="reviewer-detail">')
        lines.append(f'<summary><strong>{label}</strong></summary>')
        lines.append("")
        lines.append("<ul>")
        for dim in RQC_PUBLIC_DIMENSIONS:
            score, just = by_label.get(dim, ("β€”", ""))
            just_clean = _rewrite_rubric_filenames(just) if just else "No justification recorded."
            lines.append(
                f'  <li><strong>{_html.escape(dim)}</strong> '
                f'({_html.escape(score)}): {_html.escape(just_clean)}</li>'
            )
        lines.append("</ul>")
        lines.append("</details>")
        lines.append("")

    lines.extend([
        "---",
        "",
        (
            "*Review Quality Control is an internal ICSAC audit of the "
            "panel review itself. The four dimensions above are published "
            "as part of ICSAC's open review commitment.*"
        ),
        "",
    ])
    return _humanize_internal_jargon("\n".join(lines))


def assert_rqc_clean(text: str, artifact_path: str | None = None) -> ScrubReport:
    """Grep-gate for RQC public output.

    Runs the standard fatal/warn gate (vendors, secrets, exfil patterns)
    AND asserts that no reference to the redacted injection_indicators
    dimension survives. A leak is treated as a RedactionLeak.
    """
    report = scan(text)
    lowered = text.lower()
    extra_hits: list[tuple[str, int]] = []
    for tok in RQC_FORBIDDEN_PUBLIC_TOKENS:
        start = 0
        while True:
            at = lowered.find(tok, start)
            if at < 0:
                break
            extra_hits.append((f"[rqc-redacted] {tok}", at))
            start = at + 1
    if extra_hits or report.fatal_hits:
        raise RedactionLeak(report.fatal_hits + extra_hits, artifact_path)
    return report


def publish_public_rqc(
    record_id: str,
    reviews_dir: str,
    website_repo: str,
) -> str | None:
    """Redact reviews/<id>_review_quality_control.md β†’ public-reviews/<id>_review_quality_control.{md,html}.

    Returns the written .md path, or None if no RQC file exists for the
    record. Raises RedactionLeak if any forbidden token (including references
    to the redacted injection_indicators dimension) survives.
    """
    src = os.path.join(reviews_dir, f"{record_id}_review_quality_control.md")
    if not os.path.isfile(src):
        return None
    parsed = parse_rqc_file(src)
    public_md = build_public_rqc_markdown(parsed)
    report_md = assert_rqc_clean(public_md, artifact_path=src)

    public_html = render_public_html(public_md)
    assert_rqc_clean(public_html, artifact_path=f"{src} (rendered html)")

    out_dir = os.path.join(website_repo, "src", "data", "public-reviews")
    os.makedirs(out_dir, exist_ok=True)
    md_path = os.path.join(out_dir, f"{record_id}_review_quality_control.md")
    html_path = os.path.join(out_dir, f"{record_id}_review_quality_control.html")
    with open(md_path, "w", encoding="utf-8") as f:
        f.write(public_md)
    with open(html_path, "w", encoding="utf-8") as f:
        f.write(public_html)
    if report_md.warn_hits:
        uniq = sorted({t for t, _ in report_md.warn_hits})
        print(
            f"  redaction/rqc: {len(report_md.warn_hits)} soft-warn hit(s) "
            f"({', '.join(uniq)}) β€” inspect {md_path} if unexpected."
        )
    return md_path


if __name__ == "__main__":
    import sys

    if len(sys.argv) < 2:
        print(
            "usage: python3 redaction.py <record_id> [reviews_dir] [website_repo]\n"
            "       python3 redaction.py rqc <record_id> [reviews_dir] [website_repo]",
            file=sys.stderr,
        )
        sys.exit(2)

    if sys.argv[1] == "rqc":
        if len(sys.argv) < 3:
            print("usage: python3 redaction.py rqc <record_id> ...", file=sys.stderr)
            sys.exit(2)
        record = sys.argv[2]
        rdir = sys.argv[3] if len(sys.argv) > 3 else os.path.join(os.path.dirname(__file__), "reviews")
        wrepo = (
            sys.argv[4]
            if len(sys.argv) > 4
            else os.path.expanduser("~/Desktop/icsac/icsacinstitute.org")
        )
        try:
            written = publish_public_rqc(record, rdir, wrepo)
        except RedactionLeak as e:
            print(f"SCRUB LEAK: {e}", file=sys.stderr)
            sys.exit(1)
        if written is None:
            print(f"No RQC file for record {record} β€” nothing to publish.")
            sys.exit(0)
        print(f"wrote {written}")
        sys.exit(0)

    record = sys.argv[1]
    rdir = sys.argv[2] if len(sys.argv) > 2 else os.path.join(os.path.dirname(__file__), "reviews")
    wrepo = (
        sys.argv[3]
        if len(sys.argv) > 3
        else os.path.expanduser("~/Desktop/icsac/icsacinstitute.org")
    )
    try:
        written = publish_public_review(record, rdir, wrepo)
    except RedactionLeak as e:
        print(f"SCRUB LEAK: {e}", file=sys.stderr)
        sys.exit(1)
    print(f"wrote {written}")

    # Best-effort companion RQC publish.
    try:
        rqc_written = publish_public_rqc(record, rdir, wrepo)
    except RedactionLeak as e:
        print(f"SCRUB LEAK (rqc): {e}", file=sys.stderr)
        sys.exit(1)
    if rqc_written:
        print(f"wrote {rqc_written}")