Repository navigation
Keep malformed reviewer output in a private diagnostic file #1749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,10 @@ def __init__(self, message: str, tail: str = "") -> None: | |
| self.tail = tail | ||
|
|
||
|
|
||
| class RunnerMalformedOutput(RunnerBadOutput): | ||
| """The runner returned a payload that could not be parsed as a review.""" | ||
|
|
||
|
|
||
| class RunnerPromptTooLong(RunnerBadOutput): | ||
| """The provider rejected the prompt for exceeding its context window.""" | ||
|
|
||
|
|
@@ -214,33 +218,34 @@ def _claude_payload(envelope: dict) -> object: | |
| return _extract_json_object(str(envelope.get("result", ""))) if payload is None else payload | ||
|
|
||
|
|
||
| def parse_review(payload: object, *, family: str, head: str, kind: str, raw: str) -> ReviewResult: | ||
| def parse_review(payload: object, *, family: str, head: str, kind: str, raw: str, excerpt: str | None = None) -> ReviewResult: | ||
| """Validate a decoded review object against the findings schema.""" | ||
| diagnostic = _tail(raw if excerpt is None else excerpt) | ||
| if not isinstance(payload, dict): | ||
| raise RunnerBadOutput(f"{family}: review is not a JSON object", _tail(raw)) | ||
| raise RunnerMalformedOutput(f"{family}: review is not a JSON object", diagnostic) | ||
| if set(payload) != {"verdict", "findings"}: | ||
| raise RunnerBadOutput(f"{family}: review has unexpected or missing keys", _tail(raw)) | ||
| raise RunnerMalformedOutput(f"{family}: review has unexpected or missing keys", diagnostic) | ||
| verdict, items = payload["verdict"], payload["findings"] | ||
| if ( | ||
| not isinstance(verdict, str) | ||
| or verdict not in ("approve", "request_changes") | ||
| or not isinstance(items, list) | ||
| ): | ||
| raise RunnerBadOutput(f"{family}: review lacks verdict/findings", _tail(raw)) | ||
| raise RunnerMalformedOutput(f"{family}: review lacks verdict/findings", diagnostic) | ||
| findings: list[Finding] = [] | ||
| for item in items: | ||
| required = {"severity", "path", "line", "title", "body"} | ||
| if not isinstance(item, dict) or set(item) != required: | ||
| raise RunnerBadOutput(f"{family}: finding has unexpected or missing keys", _tail(raw)) | ||
| raise RunnerMalformedOutput(f"{family}: finding has unexpected or missing keys", diagnostic) | ||
| if not isinstance(item["severity"], str) or item["severity"] not in SEVERITIES: | ||
| raise RunnerBadOutput(f"{family}: finding has bad severity", _tail(raw)) | ||
| raise RunnerMalformedOutput(f"{family}: finding has bad severity", diagnostic) | ||
| path, line = item["path"], item["line"] | ||
| if path is not None and not isinstance(path, str): | ||
| raise RunnerBadOutput(f"{family}: finding path is not a string", _tail(raw)) | ||
| raise RunnerMalformedOutput(f"{family}: finding path is not a string", diagnostic) | ||
| if line is not None and (isinstance(line, bool) or not isinstance(line, int)): | ||
| raise RunnerBadOutput(f"{family}: finding line is not an integer", _tail(raw)) | ||
| raise RunnerMalformedOutput(f"{family}: finding line is not an integer", diagnostic) | ||
| if not isinstance(item["title"], str) or not isinstance(item["body"], str): | ||
| raise RunnerBadOutput(f"{family}: finding title/body must be strings", _tail(raw)) | ||
| raise RunnerMalformedOutput(f"{family}: finding title/body must be strings", diagnostic) | ||
| findings.append(Finding(**item)) | ||
| if any(f.severity in BLOCKING for f in findings): | ||
| verdict = "request_changes" | ||
|
|
@@ -1145,9 +1150,9 @@ def _review_claude( | |
| try: | ||
| envelope = _extract_json_object(raw) | ||
| except (json.JSONDecodeError, ValueError) as exc: | ||
| raise RunnerBadOutput("claude: stdout is not JSON", _tail(raw)) from exc | ||
| raise RunnerMalformedOutput("claude: stdout is not JSON", _tail(raw)) from exc | ||
| if not isinstance(envelope, dict): | ||
| raise RunnerBadOutput("claude: result envelope is not an object", _tail(raw)) | ||
| raise RunnerMalformedOutput("claude: result envelope is not an object", _tail(raw)) | ||
| if _claude_envelope_auth_failure(envelope): | ||
| raise RunnerAuthFailed(f"claude: CLI is not authenticated\n{_tail(raw)}") | ||
| if envelope.get("is_error") and _subscription_exhausted(raw, envelope): | ||
|
|
@@ -1162,15 +1167,15 @@ def _review_claude( | |
| try: | ||
| payload = _claude_payload(envelope) | ||
| except (json.JSONDecodeError, ValueError) as exc: | ||
| raise RunnerBadOutput("claude: result text is not JSON", _tail(raw)) from exc | ||
| raise RunnerMalformedOutput("claude: result text is not JSON", _tail(str(envelope.get("result", "")))) from exc | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: No tests cover the reclassified Claude/Codex errors or the narrowed Claude excerpt This delta makes three behaviour changes that no test checks. (1) Missing Claude/Codex provenance and an empty Codex final message now raise |
||
| usage = envelope.get("modelUsage") | ||
| models = tuple(key for key in usage if isinstance(key, str)) if isinstance(usage, dict) else () | ||
| reported_model = ", ".join(models) or None | ||
| provenance = _claude_provenance(envelope, raw) | ||
| provenance["requested_settings"] = [{"model": model, "effort": effort}] | ||
| provenance["configured_settings"] = [configured] | ||
| return replace( | ||
| parse_review(payload, family="claude", head=head, kind=kind, raw=raw), | ||
| parse_review(payload, family="claude", head=head, kind=kind, raw=raw, excerpt=json.dumps(payload)), | ||
| model=reported_model or model, effort=effort, | ||
| duration_s=done.duration_s, provenance=provenance, | ||
| ) | ||
|
|
@@ -1263,18 +1268,20 @@ def _review_codex( | |
| ) | ||
| final = _codex_final_file(last_file) | ||
| if final["status"] not in ("complete", "absent"): | ||
| raise RunnerBadOutput("codex: final message file could not be read completely") | ||
| raise RunnerMalformedOutput("codex: final message file could not be read completely") | ||
| message_bytes = base64.b64decode(final.get("bytes_base64", "")) | ||
| try: | ||
| message = base64.b64decode(final.get("bytes_base64", "")).decode("utf-8") | ||
| message = message_bytes.decode("utf-8") | ||
| except UnicodeDecodeError as exc: | ||
| raise RunnerBadOutput("codex: final message is not UTF-8") from exc | ||
| fragment = _tail(message_bytes.decode(errors="replace")) | ||
| raise RunnerMalformedOutput("codex: final message is not UTF-8", fragment) from exc | ||
| provenance, raw = _codex_provenance(done.stdout, done.stderr, message, home=home) | ||
| if not message.strip(): | ||
| raise RunnerBadOutput("codex: no final message written", _tail(done.stdout + done.stderr)) | ||
| try: | ||
| payload = _extract_json_object(message) | ||
| except (json.JSONDecodeError, ValueError) as exc: | ||
| raise RunnerBadOutput("codex: final message is not JSON", _tail(message)) from exc | ||
| raise RunnerMalformedOutput("codex: final message is not JSON", _tail(message)) from exc | ||
| envelope = _evidence_json(raw) | ||
| envelope["configured_settings"] = configured | ||
| envelope["cli_version"] = cli_version | ||
|
|
@@ -1285,7 +1292,7 @@ def _review_codex( | |
| provenance["requested_settings"] = [{"model": model, "effort": effort}] | ||
| banner_model = _CODEX_MODEL_RE.search(done.stderr) | ||
| return replace( | ||
| parse_review(payload, family="codex", head=head, kind=kind, raw=raw), | ||
| parse_review(payload, family="codex", head=head, kind=kind, raw=raw, excerpt=message), | ||
| model=model or (banner_model.group(1) if banner_model else None), effort=effort, | ||
| duration_s=done.duration_s, | ||
| provenance=provenance, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.