Skip to content

feat(cli): lossless structured error reflection for schema retries (#437) - #581

Merged
dan-petty merged 2 commits into
release/v0.2.23from
feat/437-lossless-structured-error-reflection
Sep 25, 2026
Merged

dan-petty merged 2 commits into
release/v0.2.23from
feat/437-lossless-structured-error-reflection

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

Closes #437

Summary & Context

Enhances Pydantic schema validation error feedback by preserving up to 5 field paths with type violations and prescriptive fix hints, enabling single-turn model self-correction during structured schema retries in chat_structured, fix_llm_response, and Agent.run.

Key Changes

  • Constants Centralization: Declared CONST_MAX_SCHEMA_REFLECTION_ERRORS = 5, CONST_MAX_INPUT_VALUE_REPR_LENGTH = 60, and CONST_SCHEMA_FIX_HINT_TEMPLATES in src/devops_cli/config/constants.py and re-exported in src/devops_cli/config/init.py.
  • Lossless Schema Reflection Engine: Implemented src/devops_cli/ai/schema_reflection.py with typed models SchemaViolation and SchemaReflectionReport, dot-and-bracket path formatting format_field_path(loc), table-driven fix hint synthesis synthesize_fix_hint(error_dict), extract_schema_reflection(exc, max_errors=5), and build_schema_reflection_message(error_source, max_errors=5).
  • Structured Client Reflection Integration: Updated src/devops_cli/ai/client/structured.py with format_schema_validation_error and _build_reflection_message, bounding single-error summary length <= 256 chars while providing prescriptive multi-field fix hints in retry prompts.
  • Universal Response Repair Enrichment: Extended FormattedLLMResponse in src/devops_cli/ai/response_repair.py with validation_error and schema_reflection fields. Decomposed parsing pipelines into pure helpers (_try_model_validate, _try_python_validate, _try_json_validate, _parse_schema_model, _build_extracted_tool_calls, _resolve_schema_parsed_model), dropping complexity to M <= 4.
  • Agent Schema Validation Retries: Added _handle_schema_validation_retry in src/devops_cli/ai/agents/runner.py and wired into Agent.run in src/devops_cli/ai/agents/agent.py to trigger structured reflection retry turns when schema validation fails.
  • Unit & Integration Test Coverage: Added comprehensive test suite tests/test_schema_reflection.py with structural tuple equality assertions covering path formatting, fix hint synthesis, error extraction, bounded truncation, prompt formatting, and repair integration.
  • Complexity Compliance: All modified files and new modules adhere strictly to McCabe complexity M <= 10 and maximum nesting depth <= 5.

Verification

  • uv run pytest tests/test_schema_reflection.py -v: 10/10 passed
  • uv run pytest tests/test_ai_client_structured.py tests/test_pydantic_agent.py -v: 45/45 passed
  • uv run pytest tests/test_structural_positional_oracles.py -v: 11/11 passed
  • uv run devops scan complexity src/devops_cli/ai/schema_reflection.py: Compliant (M <= 10, depth <= 5)
  • uv run devops scan complexity tests/test_schema_reflection.py: Compliant (M <= 10, depth <= 5)
  • uv run devops scan complexity src/devops_cli/ai/client/structured.py: Compliant (M <= 10, depth <= 5)
  • uv run devops ci: 10/10 Gated CI Quality Gates passing

@dan-petty
dan-petty enabled auto-merge (squash) September 25, 2026 17:12
@dan-petty
dan-petty merged commit 951609a into release/v0.2.23 Sep 25, 2026
7 checks passed
@dan-petty
dan-petty deleted the feat/437-lossless-structured-error-reflection branch September 25, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant