def _codex(self, prompt: str, *, output: Path, timeout: int, reviewer=False, canary=False, offline=False, schema_path=None, tool_workspace=None) -> None: from ops.model_role_inputs import MAX_STAGED_BYTES from ops.model_tool_workspace import ToolWorkspace from ops.native_policy_runtime import execute_guarded if tool_workspace is None: parent = Path(tempfile.mkdtemp(prefix="ledecaf-model-input-")) tool_workspace = ToolWorkspace(parent / "role", byte_budget=MAX_STAGED_BYTES) if not (reviewer or canary) and not tool_workspace.records: raise WeeklyRunError("Author needs explicit staged inputs before its prompt receipt") self._assert_subscription_auth() output.parent.mkdir(parents=True, exist_ok=True) output.unlink(missing_ok=True) protected = self._protected_publication_state(output) if not canary else None invocation = self._guarded_invocation(output=output, workspace=tool_workspace, reviewer=reviewer, canary=canary, offline=offline, schema_path=schema_path) result = None try: result = execute_guarded(invocation, prompt, timeout_seconds=timeout, failure_parser=reviewer_failure_category) finally: tool_workspace.verify_inputs() if protected is not None and self._protected_publication_state(output) != protected: raise WeeklyRunError("Codex changed the authoritative database or retained publication evidence") if not isinstance(result, dict): raise WeeklyRunError("Qualified native execution returned no bounded result") safe = {key: value for key, value in result.items() if key != "report"} atomic_json(invocation.private_dir / "guarded-result.json", safe) if canary: capture = result.get("execution") or result.get("preflight") or {} failure = None if result.get("status") == "completed" else ( "process_start" if capture.get("failure_kind") == "process_start" else "timeout" if capture.get("timed_out") else "nonzero_exit" if capture.get("exit_code") else "unknown") observation = subscription_preflight_observation(failure, exit_code=capture.get("exit_code")) if failure == "nonzero_exit" and capture.get("observed_failure_category") in {"authentication", "rate_or_usage_limit", "service_unavailable"}: observation["category"] = capture["observed_failure_category"] if type(capture.get("diagnostic_tail_truncated")) is bool: observation["diagnostic_tail_truncated"] = capture["diagnostic_tail_truncated"] for stream in ("stdout", "stderr"): details = capture.get(stream) or {} if (type(details.get("bytes")) is int and details["bytes"] >= 0 and isinstance(details.get("sha256"), str) and re.fullmatch(r"[0-9a-f]{64}", details["sha256"])): observation[stream] = {"representation": "captured_bytes", "bytes": details["bytes"], "sha256": details["sha256"]} self._subscription_command_observation = observation raw = result.get("report") if isinstance(raw, bytes): output.write_bytes(raw) if result.get("status") != "completed" or result.get("report_truncated") is not False: if result.get("category") == "timeout": phase = "execution" if "execution" in result else "preflight" raise WeeklyRunError( f"Qualified native execution did not complete before its deadline " f"(phase={phase}, budget_seconds={timeout})" ) raise WeeklyRunError("Qualified native execution did not complete") if not isinstance(raw, bytes) or not raw: if canary: self._subscription_command_observation.update(failure_kind="missing_output", category="missing_output") raise WeeklyRunError("Codex completed without writing its requested output")