def _run_reviews( self, packet_path: Path, attempt_dir: Path ) -> tuple[str, dict[str, Path]]: try: attempt_dir.mkdir(parents=True, exist_ok=False) except OSError as exc: raise WeeklyRunError("Review attempt already exists or cannot be reserved; retained evidence was not replaced") from exc digest = packet_sha256(packet_path) packet = json.loads(packet_path.read_text(encoding="utf-8-sig")) pending = self._pending_external_findings() if pending: self._external_adjudication_projection(packet, pending) review_stage = self.run.get("stage", "independent_review") if not review_stage.startswith("independent_review"): review_stage = "independent_review" attempt_match = re.fullmatch(r"attempt-([0-2])", attempt_dir.name) attempt = int(attempt_match[1]) if attempt_match else None self.save("review_context_preparation", review_phase="review_context_preparation", partial_required_reviews=None) self._require_review_capacity() self._progress("source_context_started", attempt=attempt) context_path = ( self.work_dir / "review-context" / attempt_dir.name / "retrieved-context.json" ) context_path.parent.mkdir(parents=True, exist_ok=True) review_schema = json.loads(SCHEMA_PATH.read_text(encoding="utf-8")) if pending: review_schema["required"].append("external_findings_review") else: review_schema["properties"].pop("external_findings_review", None) review_schema_path = context_path.parent / "mandatory-review-schema.json" atomic_json(review_schema_path, review_schema) if len(review_schema_path.read_text(encoding="utf-8")) > MAX_REVIEW_SCHEMA_CHARS: raise WeeklyRunError("Mandatory reviewer schema exceeds its separate transport budget") historical_args = (["--historical-context", str(self._historical_context_path())] if self.run_kind == "retrospective" else []) if self._author_analytical_receipts() is not None: retained_input_path = context_path.parent / "author-analytical-input.json" retained_input_path.write_text( json.dumps(self.run["author_analytical_input"], ensure_ascii=False, separators=(",", ":"), allow_nan=False), encoding="utf-8", ) historical_args += ["--author-analytical-input", str(retained_input_path)] context_budget = self._review_context_budget(packet, digest) self._call( [ sys.executable, str(OPS / "prepare_ai_review_context.py"), str(packet_path), "--repository", str(ROOT), "--editorial-database", str(self.database), "--private-provenance-root", str(self.bundle_root), "--output", str(context_path), "--inline-budget", str(context_budget), ] + historical_args, timeout=min(10 * 60, self._remaining()), error="Public evidence and continuity context preparation", ) context = context_path.read_text(encoding="utf-8") self._verify_author_analytical_input(json.loads(context), packet, digest) self.save(review_stage, review_phase=review_stage) self._progress("source_context_ready", attempt=attempt) reports: dict[str, Path] = {} prompts: dict[str, Path] = {} for role in ("facts", "editorial"): reviewer_id = f"{role}-{uuid.uuid4()}" report_path = attempt_dir / f"{role}.json" prompt_path = ( self.work_dir / "review-prompts" / attempt_dir.name / f"{role}.prompt.txt" ) prompt_path.parent.mkdir(parents=True, exist_ok=True) prompt = self._review_prompt( role, packet, digest, reviewer_id, utc_now(), context ) prompt_path.write_text(prompt, encoding="utf-8") prompts[role] = prompt_path self._progress(f"{role}_review_started", attempt=attempt) self._codex( prompt, output=report_path, timeout=self._run_codex_timeout("review_timeout_seconds"), reviewer=True, schema_path=review_schema_path, ) if packet_sha256(packet_path) != digest: raise WeeklyRunError( "Packet changed during independent review; reports cannot authorize release" ) report = self._load_and_validate_report( report_path, role, reviewer_id, digest ) report["completed_at"] = utc_now() report_path.write_text( json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8" ) reports[role] = report_path self._validate_pending_external_findings(packet, {role: report_path}, require_approval=False) self._record_required_review_progress(packet, attempt_dir, reports) self._progress(f"{role}_review_finished", attempt=attempt) from ops.reporting_memory import save_review_record record = save_review_record( self.bundle_root, packet_path, context_path, prompts, reports, state_file=self.state_file, ) self.save(self.run.get("stage", "independent_review"), reporting_record=self._path_ref(record)) decision = self._merge_decisions(reports) mandatory_decision = decision self._progress("additional_review_check_started", attempt=attempt) external_decision = self._external_review(packet, context, attempt_dir) self._progress("additional_review_check_finished", attempt=attempt) if external_decision == "hold" or decision == "hold": decision = "hold" elif external_decision == "revise" or decision == "revise": decision = "revise" if decision == "hold": return decision, reports findings = self._origin_contract_findings(packet, reports) if mandatory_decision == "approve" else None if findings is not None: path = self._contract_findings_path(reports, findings) if path.exists() and json.loads(path.read_text(encoding="utf-8")) != findings: raise WeeklyRunError("Retained publication-contract findings changed") if not path.exists(): atomic_json(path, findings) return "revise", reports if decision != "approve": return decision, reports self._call( [ sys.executable, "-m", "prism.tools.publish_ai_issue", str(packet_path), "--review", str(reports["facts"]), "--review", str(reports["editorial"]), "--check", ], timeout=60, error="Independent AI review contract validation", ) return decision, reports