from __future__ import annotations from pathlib import Path from typing import Any import httpx from harness.adapters import Adapter PROMPTS = Path(__file__).resolve().parent / "prompts" TRACE_LIMIT = 1200 RELAY_TIMEOUT_S = 100.0 def _trim(text: str, limit: int) -> str: return text if len(text) <= limit else text[-limit:] def _tail(session: str, limit: int) -> str: """Keep the end of the session, where the current work is. Cheaper than a full redo.""" if limit <= 0 or len(session) <= limit: return session cut = session[-limit:] start = cut.find("\n[") return "\n[… earlier steps omitted …]\n" + (cut[start + 1 :] if start >= 0 else cut) def repair( task: dict[str, Any], draft: Any, trace: str, errors: str, *, adapter: Adapter, relay_url: str, ticket: str, model: str | list[str], tail_chars: int = 0, max_tokens: int = 3072, ) -> Any: """Ask a frontier model through the relay to repair the draft. Only the end of the session is sent when tail_chars is set, which keeps the repair cheaper than a full frontier redo (the escalation score). Whatever comes back goes through the same deterministic finish as the harness, so the headings, facts and budget always hold. Any relay failure returns the draft. """ draft_text = adapter.as_text(draft) finish = getattr(adapter, "finish", None) session = str(task.get("user") or "") view = dict(task) view["user"] = _tail(session, tail_chars) budget = max_tokens if finish is not None: from harness.adapters.context_compression import budget_of budget = min(max_tokens, budget_of(task)) system = adapter.render(PROMPTS, "system", view) user = adapter.render( PROMPTS, "repair", view, draft=draft_text, errors=_trim(errors or "no verifier output", 400), trace=_trim(trace or "", TRACE_LIMIT), budget=str(budget), ) models = [model] if isinstance(model, str) else list(model) text = "" for name in [m for m in models if m]: body = { "model": name, "max_tokens": budget, "temperature": 0, "messages": [{"role": "system", "content": system}, {"role": "user", "content": user}], } try: with httpx.Client(timeout=RELAY_TIMEOUT_S) as http: response = http.post( relay_url.rstrip("/") + "/v1/chat/completions", json=body, headers={"x-mt-ticket": ticket}, ) except Exception: continue if response.status_code != 200: continue try: choices = response.json().get("choices") or [{}] text = str((choices[0].get("message") or {}).get("content") or "") except Exception: text = "" if text.strip(): break if finish is not None: answer, _note = finish(text or draft_text, task) return answer return adapter.clean(text) or draft