Skip to content

refactor(ai): one structured-output path for validate, repair, reflect and retry #1003

Closed

Description

@dan-petty

Context & Rationale

Three in-house "parse, validate, repair, reflect, retry" implementations exist beside pydantic_ai's native one. Checked at 9c879ae.

  • ai/client/structured.py (chat_structured :215, repair :105, reflection :123) has no source caller.
  • ai/agents/runner.py:431-496 (_handle_schema_validation_retry, _validate_agent_output) with ai/agents/agent.py:887-990 and :1086-1095 is the path the review takes.
  • ai/agents/pipeline.py:63-76 (_parse_pipeline_output) then re-parses the same reply with extract_json_block plus model_validate and downgrades failures to a warning.
  • One validated structured reply in a review crosses 14 files: commands/review.py → ai/review/runner.py → ai/review/pipeline.py:2201 → ai/agents/pipeline.py → ai/agents/agent.py:778 → ai/client/unified.py:681 → ai/client/ollama.py:209 → ai/client/network.py → ai/client/base.py → ai/client/models.py → ai/response_repair.py → ai/agents/runner.py → ai/schema_reflection.py → ai/review_schema.py. The module named "structured" is not on it.
  • feat(ai): one json repair seam that counts repairs per entry point #875 counts repairs across the json-repair call sites. This item is the module those calls belong to.

Deletion test: structured.py deletes clean. Deleting the agent.py/runner.py path makes it reappear in the loop, so that is where the one implementation concentrates.

Scope & Deliverables

  • Deliverable: one module validates model text against a schema under a bounded repair-and-reflection retry policy and returns either the model or the next retry message. The agent loop calls it; MultiAgentPipeline stops re-parsing; chat_structured becomes a thin verb over it or goes.
  • Tests: test_ai_client_structured.py (12), test_schema_reflection.py and the output-retry tests in test_pydantic_agent.py collapse into one table-driven suite.
  • Key questions (open):
  • Constraint: feat(ai): sequential pipeline stages carry a budgeted context, with the scratchpad appended once #859 changes what MultiAgentPipeline carries between stages; sequence the two.

Strategic Alignment

Scope added 2026-10-02: checked at 38517a8; the claims hold. chat_structured (ai/client/structured.py:215) is reachable as an LLMClient method through StructuredOutputMixin (ai/client/unified.py:161), but nothing calls it. _parse_pipeline_output (ai/agents/pipeline.py:63-73) logs a failed validation as a warning and returns None. JSON repair runs from ai/client/structured.py, ai/agents/agent.py, ai/agents/models.py, ai/review_schema.py and core/serialization.py. core.serialization.extract_json_block falls back to json_repair (core/serialization.py:76-80), and all 7 of its callers pass model replies (ai/review/verification.py:2057, ai/agents/pipeline.py:68, ai/review_schema.py:1376, ai/analyze/outlines.py:124, ai/benchmark/runner.py:593, ai/review/runner.py:527, :549). #875 (P2, backlog, refined) already owns one repair_json(text, *, source) entry in ai/response_repair.py, json_repair imported only there, the devops_cli_ai_json_repairs_total counter per entry point, and deleting chat_structured with its helpers and tests. This item does not repeat that work. The review evaluation of 2026-10-02 (section 4.3, N1) found that persona replies are cached with no validator (ai/agents/agent.py:887), so a reply that echoes the schema is replayed for 7 days. #1013 (filed 2026-10-02, P1) is that fix: the response cache stores and serves only replies the review can use. Its validator rejects a reply before it is cached; this item later owns the validation that validator runs. #859 (v0.2.27) changes what MultiAgentPipeline carries between stages.

Acceptance criteria

Key questions

Update 2026-10-08: refinement

Activity

  1. added
    type/refactor
    scope/ai
    priority/p2-medium
    on Oct 2, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    This item's premise is out of date (checked 2026-10-08 at 910b823).

    • The Problem and the 2026-10-02 scope note say chat_structured "has no source caller". Since v0.2.26, roadmap intake (src/devops_cli/roadmap/intake_model.py:184) and refine (src/devops_cli/roadmap/refine.py:344, :792) call it in the roadmap service.
    • A key question says the agent loop's policy survives "because it is the only one production runs". That no longer holds either: chat_structured's reflect-and-retry also runs in production.

    Two more things changed since refinement:

    Suggested amendment:

  3. modified the milestone: v0.2.37 on Oct 20, 2026
  4. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Re-scope, checked on 2026-10-28 at origin/main 4226904. The goal of this item stays: one path validates, repairs, reflects and retries a structured reply. But the premise of its design does not hold now, so the item is built by deletion, not by a new module.

    Evidence

    Re-scope (built in one pull request)

    The open questions, decided

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/p2-mediumscope/aitype/refactor

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions