Skip to content

refactor(ai): delete the dead pydantic_ai re-export facades, the legacy provider layer and the unused exception mirrors #1852

Closed

Description

@dan-petty

From the open-library audit of 2026-10-22 (ranked R02, about 4200 lines of source and tests; risk L). Merged ai-core-2, ai-core-5, ai-core-7 and core-5 (ai/exceptions.py is the same file in two reports; budget_diff_chunks moved to R05). Spot-checked: 0 src importers of ai.exceptions/profiles/result/run/ext_langchain; direct.py is reached only from unused LLMClient methods; get_provider has 0 callers outside ai/providers; 17 of 17 sampled helper names have 0 callers; 6 of 7 exception mirrors have 0 references outside their definitions. Sequence after R01 (same tests and conftest providers registry).

What the code is (ai-core-2): Modules whose body re-exports pydantic_ai.exceptions, pydantic_ai.profiles/, pydantic_ai.result, pydantic_ai.run and pydantic_ai.direct with thin wrappers. ext_langchain.py duplicates pydantic_ai.ext.langchain. None has an importer in src: ai/exceptions, profiles, result, run and ext_langchain have zero; ai/direct.py is reached only from LLMClient.direct_request/model_request(_sync) (unified.py lines 959-1085), which have no callers.

Files: src/devops_cli/ai/exceptions.py (175), src/devops_cli/ai/profiles/init.py (216), src/devops_cli/ai/result/init.py (110), src/devops_cli/ai/run/init.py (131), src/devops_cli/ai/ext_langchain.py (131), src/devops_cli/ai/direct.py (338), src/devops_cli/ai/client/unified.py (126)

Existing tool considered: Import from pydantic_ai directly where ever needed (pydantic_ai.exceptions, .profiles, .result, .run, .direct, .ext.langchain). All are already installed (pydantic-ai 2.54.0). Behaviour gap: None: no production caller. The wrappers add only defaults, tracing and name normalisation.

Evidence: /tmp/open-libs-audit-deadscan.py (module-level importer scan) plus grep for string/lazy imports; also re-checked relative and importlib imports (none for these modules).

Tests affected: tests/test_pydantic_ai_exceptions.py (441); tests/test_pydantic_ai_result.py (138); tests/test_pydantic_ai_run.py (114); tests/test_ext_langchain.py (103); tests/test_pydantic_ai_direct.py (239); tests/test_pydantic_ai_profiles_and_providers.py (profiles part); tests/test_model_contract.py; tests/test_ai_spend_dispatch.py; tests/test_review_backend_activity.py (model_request references); tests/test_runtime_security_and_ssrf_hardening.py (ext_langchain reference)

Risk (L): Zero src importers by AST and grep; only tests import them.

What the code is (ai-core-5): A second, hand-rolled httpx2 provider stack (get_provider/register_provider/reset_providers). Nothing in src calls get_provider; CopilotProvider.generate returns a stub dict. Only tests (and the test-only MockProvider) use it. The live code is create_pydantic_ai_provider in the same init, which returns native pydantic_ai providers.

Files: src/devops_cli/ai/providers/base.py (41), src/devops_cli/ai/providers/anthropic.py (84), src/devops_cli/ai/providers/copilot.py (35), src/devops_cli/ai/providers/ollama.py (69), src/devops_cli/ai/providers/openai.py (69), src/devops_cli/ai/providers/init.py (25)

Existing tool considered: pydantic_ai.providers.* via the existing create_pydantic_ai_provider (kept). Behaviour gap: None; legacy classes are never invoked in production.

Evidence: grep get_provider/register_provider/BaseLLMProvider/CopilotProvider across src: zero callers outside ai/providers.

Tests affected: tests/test_providers.py; tests/test_openai_auth.py; tests/mock_provider.py; tests/conftest.py (lines 86-107, 171-175); tests/test_rag_loopback_policy.py (OllamaProvider assertions); tests/test_security_sandbox_and_protection.py (OllamaProvider test); tests/test_pydantic_ai_profiles_and_providers.py (get_provider tests)

Risk (L): Dead in src; MockProvider registration in conftest must be removed.

What the code is (ai-core-7): Functions with zero src callers that wrap library calls: combine_toolsets/prefix_toolset/filter_toolset/rename_toolset/require_approval_toolset (native toolset .prefixed()/.filtered()/.renamed()/.approval_required()), normalize_thinking_level/service_tier/tool_choice, create_async_retry_transport, normalize_agent_retries, four of the six format_*_as_xml wrappers around pydantic_ai.format_as_xml, create_template_str, create_pydantic_ai_agent (no caller; includes an inspect.signature fallback to PydanticAgent), get_persona_pydantic_agent, execute_structured_workflow(_sync), inject_prompt_cache_points, budget_diff_chunks.

Files: src/devops_cli/ai/pydantic_ai_bridge.py (270), src/devops_cli/ai/tools/init.py (88), src/devops_cli/ai/format_prompt.py (74), src/devops_cli/ai/settings/init.py (52), src/devops_cli/ai/retries/init.py (50), src/devops_cli/ai/toolsets/init.py (40), src/devops_cli/ai/concurrency.py (22), src/devops_cli/ai/template/init.py (15), src/devops_cli/ai/output/init.py (16), src/devops_cli/ai/context_budget.py (67)

Existing tool considered: The pydantic_ai API each one wraps (call sites in tests become direct calls). Behaviour gap: None; no production caller.

Evidence: grep -w per name in src excluding definition and all: 0 references for each (28 names checked); tests reference each from exactly one file.

Tests affected: tests/test_pydantic_ai_workflows.py; tests/test_pydantic_ai_toolsets.py; tests/test_pydantic_ai_settings.py; tests/test_pydantic_ai_retries.py; tests/test_pydantic_ai_format_prompt.py; tests/test_pydantic_ai_template.py; tests/test_pydantic_ai_output.py; tests/test_pydantic_ai_concurrency.py; tests/test_pydantic_agent.py

Risk (L): Mechanical dead-symbol removal; test count is approximate (about 500 lines).

What the code is (core-5): ModelAPIError, ModelHTTPError, UsageLimitExceeded, ConcurrencyLimitExceeded, RunCancelled, IncompleteToolCall and SuspendedResponseExpired are dual-inheritance mirrors of pydantic_ai.exceptions. An AST scan finds 0 Load references to them anywhere in src outside the 3-layer re-export lists. ai/exceptions.py and concurrency.py use pydantic_ai.exceptions directly.

Files: src/devops_cli/exceptions/ai_pydantic.py (177), src/devops_cli/exceptions/ai.py (21), src/devops_cli/ai/exceptions.py (14)

Existing tool considered: pydantic_ai.exceptions itself, the library's public module that the code already imports. Behaviour gap: None in src. The other 8 wrappers are raised or caught by devops code and stay. Observation, not verified at runtime: except ModelRetry at ai/agents/runner.py:146 and :510 catches the wrapper subclass, so it misses a pydantic-ai ModelRetry raised by the library itself.

Evidence: AST scan over src and tests, listing every Name Load per wrapper (/tmp/open-libs-audit/core is not needed: the scan was run inline).

Tests affected: tests/test_pydantic_ai_concurrency.py; tests/test_pydantic_ai_workflows.py

Risk (L): No src caller. The only references are 4 in tests/test_pydantic_ai_concurrency.py and tests/test_pydantic_ai_workflows.py.

Land after the harness deletion (#1836), which shares the tests, conftest registry and invariants lists.

Acceptance criteria:

  • The listed modules, symbols and their tests are deleted; none is skipped or xfailed; a grep of src, tests, AGENTS.md and docs (outside docs/agent history and CHANGELOG) for the deleted paths and names returns nothing.
  • Every shared list is updated (CONST_ lists, invariants, ceilings fixture, conftest, generated docs) and devops docs check is clean.
  • devops ci passes with coverage at or above 90%.

Proposed design

Problem statement

The devops-cli project contains dead code in the AI subsystem that re-exports pydantic_ai components, legacy provider layers, and unused exception mirrors. These components are not used by any production code but still exist in the codebase, increasing maintenance burden and reducing clarity.

Acceptance criteria

  • Criterion 1: All dead pydantic_ai re-export facades (ai/exceptions.py, profiles/, result/, run/, ext_langchain.py, direct.py) are deleted from the codebase
    Verification: A grep of src directory for any of these files returns no matches
  • Criterion 2: All legacy provider layer modules (ai/providers/base.py, anthropic.py, copilot.py, ollama.py, openai.py, init.py) are deleted from the codebase
    Verification: A grep of src directory for any of these files returns no matches
  • Criterion 3: All unused exception mirrors (ai_pydantic.py, ai.py, exceptions.py) are deleted from the codebase
    Verification: A grep of src directory for any of these files returns no matches
  • Criterion 4: All dead functions and symbols wrapped by pydantic_ai API are removed from their respective modules
    Verification: A grep of src directory for function names like combine_toolsets, prefix_toolset, normalize_thinking_level, etc. returns no matches
  • Criterion 5: Tests referencing the deleted code are updated to directly import from pydantic_ai instead of the dead wrappers
    Verification: All tests pass with coverage at or above 90%
  • Criterion 6: devops docs check passes without errors after the deletion
    Verification: Running devops docs check produces no output

Key questions

  • Are there any production callers of the deleted pydantic_ai re-export facades?: No, the audit states that 'Zero src importers by AST and grep; only tests import them.' [sources: src/devops_cli/ai/exceptions.py, src/devops_cli/ai/profiles/init.py, src/devops_cli/ai/result/init.py, src/devops_cli/ai/run/init.py, src/devops_cli/ai/ext_langchain.py, src/devops_cli/ai/direct.py]
  • Are there any production callers of the legacy provider layer?: No, the audit states that 'Nothing in src calls get_provider; CopilotProvider.generate returns a stub dict. Only tests (and the test-only MockProvider) use it.' [sources: src/devops_cli/ai/providers/base.py, src/devops_cli/ai/providers/anthropic.py, src/devops_cli/ai/providers/copilot.py, src/devops_cli/ai/providers/ollama.py, src/devops_cli/ai/providers/openai.py, src/devops_cli/ai/providers/init.py]
  • Are there any production callers of the functions wrapped by pydantic_ai API?: No, the audit states that 'Functions with zero src callers that wrap library calls: combine_toolsets/prefix_toolset/filter_toolset/rename_toolset/require_approval_toolset (native toolset .prefixed()/.filtered()/.renamed()/.approval_required()), normalize_thinking_level/service_tier/tool_choice, create_async_retry_transport, normalize_agent_retries, four of the six format_*_as_xml wrappers around pydantic_ai.format_as_xml, create_template_str, create_pydantic_ai_agent (no caller; includes an inspect.signature fallback to PydanticAgent), get_persona_pydantic_agent, execute_structured_workflow(_sync), inject_prompt_cache_points, budget_diff_chunks. Evidence: grep -w per name in src excluding definition and all: 0 references for each.' [sources: src/devops_cli/ai/pydantic_ai_bridge.py, src/devops_cli/ai/tools/init.py, src/devops_cli/ai/format_prompt.py, src/devops_cli/ai/settings/init.py, src/devops_cli/ai/retries/init.py, src/devops_cli/ai/toolsets/init.py, src/devops_cli/ai/concurrency.py, src/devops_cli/ai/template/init.py, src/devops_cli/ai/output/init.py, src/devops_cli/ai/context_budget.py]
  • Are there any production callers of the exception mirrors?: No, the audit states that 'AST scan finds 0 Load references to them anywhere in src outside the 3-layer re-export lists. The only references are 4 in tests/test_pydantic_ai_concurrency.py and tests/test_pydantic_ai_workflows.py.' [sources: src/devops_cli/exceptions/ai_pydantic.py, src/devops_cli/exceptions/ai.py, src/devops_cli/ai/exceptions.py]
  • What is the expected behavior after deletion of these components?: None; no production caller. The wrappers add only defaults, tracing and name normalisation. [sources: src/devops_cli/ai/exceptions.py, src/devops_cli/ai/profiles/init.py, src/devops_cli/ai/result/init.py, src/devops_cli/ai/run/init.py, src/devops_cli/ai/ext_langchain.py, src/devops_cli/ai/direct.py]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#1836

Activity

  1. added
    type/refactor
    source/agent
    on Oct 22, 2026
  2. dan-petty commented on Oct 22, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Type: type/refactor. The candidate proposes deleting dead code and refactoring AI-related modules, which fits the refactor type.
    • Priority: P2-Medium. This is a medium-priority refactor as it removes dead code without affecting functionality or security.
    • Value: Medium. The change improves code hygiene and reduces maintenance overhead but doesn't directly benefit end users.
    • Effort: Low. The effort is low because all the code being removed has no source callers, making it straightforward to delete.
  3. added this to the v0.2.39 milestone on Oct 22, 2026
  4. added
    status/in-progress
    on Oct 22, 2026
  5. dan-petty commented on Oct 22, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at ae8c2d9 (see proposed design in issue body).

  6. added a commit that references this issue on Oct 22, 2026
    ab595c1
  7. dan-petty commented on Oct 22, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1869, merged into release/v0.2.39 as ab595c1.

    Cause: the repository carried a second layer over pydantic-ai that no command, server tool or module used. An AST import graph from commands/*, main and server/* (74 root modules, 543 of 567 modules reachable at the base) reaches no importer of ai/exceptions.py, ai/profiles, ai/result, ai/run or ai/ext_langchain.py. ai/direct.py was imported only by four LLMClient.direct_* methods that nothing calls. The five legacy provider classes were imported only by ai/providers/__init__.py, whose get_provider had no caller. Around them sat wrappers of single library calls and exception subclasses of library exceptions that nothing raised or caught. A scan after each deletion found the code the deletions orphaned: the token governor, the async retry transport's clock seam and pool closer, and the facade-only response converters. After the change 534 of 553 modules are reachable and the unreachable set is the same pre-existing one, minus the five facades.

    Change: the modules above are deleted, with ai/concurrency.py and ai/template (which shrank to re-exports once their unused helpers went) and ai/governance.py. ai/providers/ keeps create_pydantic_ai_provider and is_pydantic_ai_provider. LLMClient.direct_*, model_request and model_request_sync go, as do the bridge's agent factories and workflow helpers, the wrappers around single library calls in the tools, toolsets, settings, retries, format-prompt and output helpers, and 20 pydantic-ai exceptio…

    80 file(s) changed.

    How it was verified

    Check runs at 05c4a14:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • devops-cli: pass
    • zizmor: pass
    • bandit: pass
    • CodeQL: pass
    • DevContainer Image: skipping
    • Fail-to-Pass Check: pass
    • Changed-Test Independence: pass
    • Tests & Coverage: pass
    • Analyze (actions): pass
    • Fail-to-Pass Self-Check: pass
    • Rule Pack Tests: pass
    • Analyze (python): pass
    • Static Analysis: pass
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1852-dead-pydantic-ai-facades.md:

    • The listed modules, symbols and their tests are deleted, none is skipped or xfailed, and a search of src, tests, AGENTS.md, README.md and docs (outside docs/agent/tasks, docs/agent/archive and CHANGELOG.md) for the deleted module paths and names finds nothing.
    • The import graph from commands/, server/ and main reaches no deleted module, no top-level symbol or class method in src lost its last reference to the deletion without being deleted, and every from devops_cli... import in src and tests resolves.
    • The shared lists are updated (conftest reset entries, the ceilings fixture, DEFAULT_* and CONST_* constants and the devops_cli.config exports, the generated docs/ERRORS.md and docs/TELEMETRY.md) and uv run devops docs check reports every file up to date.
    • ruff check, ruff format --check and mypy pass over src and tests, the swallowed-failure and suppression ceilings equal the counts measured on this tree, and the touched test modules, the architectural-invariant, export-surface, AGENTS.md claim and startup-import tests, and every test that imports a touched module pass.
    • Pending a person: uv run devops ci passes on the delivering tree. This change ran the targeted tests and two coverage runs of the tests that import the touched modules, not the gate.
  8. added a commit that references this issue on Oct 23, 2026
    f209f6c
  9. removed
    status/in-progress
    on Oct 30, 2026
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

    source/agenttype/refactor

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions