Skip to content

fix(ai): response repair cuts fenced json at inner code fences and misreports validation errors #656

Closed

Description

@dan-petty

Summary

Found in the release/v0.2.23 review (line numbers at 7fdaa9d).

  • Model replies that put JSON inside a fence lose data. Given a json fence whose first finding has a python block in its fix field, repair_json_string returns 1 of 3 findings, and that finding has fix: "". This was reproduced directly.
    • It is reached from parse_review_response (src/devops_cli/ai/review_schema.py:1336-1340) and from agent tool-call parsing (src/devops_cli/ai/agents/agent.py:896, :1084).
    • write_file tool arguments that contain a fence are cut short in the same way, so the target file is overwritten with truncated content. main kept the whole object.
  • Retry reflections name the wrong error. When the repaired data fails a field check, the reflection reports root: Invalid JSON instead of the field that failed (e.g. count: valid integer), so the model spends its retries on the wrong problem.
  • Agents with plain-text output now make two model calls and raise. PydanticAgent(output_type=str) and TextOutput agents raise UnexpectedModelBehavior, where main returned the text after one call.

Root Cause

  1. _JSON_FENCE in src/devops_cli/ai/response_repair.py:48 is a non-greedy match that ends at the next triple backtick anywhere, including inside a JSON string. repair_json_string (:61-66) returns the repaired fragment before it ever tries the whole text.
  2. _parse_schema_model (response_repair.py:373-379) sets last_err = json_err or last_err. The json_invalid error from validating the raw text replaces the field-level ValidationError from the repaired data.
  3. The schema-retry stage (ai/agents/agent.py:960-972, ai/agents/runner.py:455-482) treats parsed_model is None as a validation failure. For str outputs, validation fails because TypeAdapter(str).validate_json rejects plain text. For TextOutput with a RunContext or async function, _text_only_function deliberately returns None.

Remediation Plan

  1. Parse the whole cleaned text first. Fall back to fenced blocks only when that yields no dict or list, and anchor closing fences to a line of their own.
  2. Prefer the most specific validation error: field-level errors from the repaired data win over raw-text JSON errors.
  3. Run schema retries only for structured output types. Plain-text outputs return after one call.
  4. Add regression tests:
    • a nested fence inside fix: all findings survive and fix is intact;
    • write_file arguments containing a fence arrive whole;
    • the reflection names the failing field;
    • an output_type=str agent makes one call.

Activity

  1. added this to the v0.2.23 milestone on Sep 28, 2026
  2. added
    type/bug
    scope/ai
    priority/p1-high
    on Sep 28, 2026
  3. dan-petty commented on Sep 28, 2026

    @dan-petty
    OwnerAuthor

    Closed by #675, merged into release/v0.2.23.

    GitHub only acts on closing keywords when a pull request merges into the default branch, so this was closed by devops gh issues close-merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/aitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions