Skip to content

refactor(ai): one agent runtime behind one construction point, with no silent fallback #1005

Closed as not planned

Description

@dan-petty

Context & Rationale

Callers pick between two agent runtimes with different contracts. Checked at 9c879ae.

  • PydanticAgent[T, DepsT] (ai/agents/agent.py, 1,230 lines, with runner.py, 496) is a hand-rolled tool loop over client.chat_messages (:887, :1086, :1131). It is constructed directly at 10 sites outside agents/: commands/ai.py (2), harness/agents.py (2), harness/workflow.py, review/pipeline.py (2), the bridge.
  • pydantic_ai_bridge.create_pydantic_ai_agent (:236-295) builds a native pydantic_ai.Agent and, on any exception, falls back to PydanticAgent, returning Any with a different run contract. durable.py:614 builds a native Agent directly. 34 source files import pydantic_ai directly.
  • ai/agents/pydantic_agent.py (931 lines) exists to make the hand-rolled class look like pydantic_ai.
  • tests/test_pydantic_agent.py (33 tests) fakes chat_messages; that is the de facto seam.

Deletion test: delete the hand-rolled loop and run on a native Agent with a Model built from the one model seam, and the loop's complexity vanishes into the dependency while the devops-specific capabilities (capabilities.py, guardrails, spend) stay. Or delete the bridge and keep the loop. Either way the silent fallback goes.

Scope & Deliverables

Strategic Alignment

Scope added 2026-10-02: checked at 38517a8. Production never builds a native pydantic_ai agent, so callers do not choose between two runtimes today. The only two pydantic_ai.Agent(...) constructions in src are in create_pydantic_ai_agent (ai/pydantic_ai_bridge.py:236-295, construction at :281) and create_durable_pydantic_agent (ai/durable.py:571-614). The first is called only by get_persona_pydantic_agent (:297-321), which nothing in src calls; the second is only re-exported (ai/agents/pydantic_agent.py:221). The silent fallback (:282-295) is therefore reachable only from tests. Every production agent is a PydanticAgent, built in commands/ai.py (2), ai/harness/agents.py, ai/harness/workflow.py, ai/review/pipeline.py (2) and the bridge's fallback. The bridge's other public functions (inject_prompt_cache_points, create_cached_user_prompt, execute_structured_workflow_sync, build_fallback_cascade_model and its one caller GatewayRouter.build_pydantic_cascade_model, ai/gateway.py:554) have no production caller either. They are also the only route to resolve_pydantic_ai_model, and through it to ai/providers and ai/direct.py (see #1001). The knowledge-base page ai/knowledge_base/it_domains/tools/pydantic_ai.md:22-26 teaches the dead factories. AGENTS.md:392 asks for provider protocols under providers/. That bullet is not in ai/instruction_generator.py, and AGENTS.md is curated, not generated (#840).

Acceptance criteria

  • PydanticAgent is the only agent runtime. create_pydantic_ai_agent, get_persona_pydantic_agent, _fallback_pydantic_agent, inject_prompt_cache_points, create_cached_user_prompt, build_fallback_cascade_model, execute_structured_workflow and execute_structured_workflow_sync (ai/pydantic_ai_bridge.py:210-449), GatewayRouter.build_pydantic_cascade_model and create_durable_pydantic_agent are deleted. rg 'pydantic_ai import .*\bAgent\b|pydantic_ai\.Agent\(' src finds nothing, and ai/agents/pydantic_agent.py:5-7 no longer re-exports pydantic_ai.agent.Agent, which no src module imports from it.
  • Everything only those functions reach is deleted with them: resolve_pydantic_ai_model and its helpers, ai/providers/, ai/direct.py, and their re-exports. The PR lists each deleted public name with the search that shows it had no caller in src.
  • Every agent construction in src builds a typed PydanticAgent[...]. No function that builds an agent returns Any, and none falls back on an exception.
  • Tests that cover only deleted code are deleted, and the PR names them (for example the bridge-fallback tests in tests/test_pydantic_agent.py, tests/test_providers.py and tests/mock_provider.py). tests/test_pydantic_agent.py keeps its chat_messages seam tests.
  • The knowledge-base page no longer shows the deleted factories.
  • AGENTS.md:392 is edited in place to name LLMClient as the model seam, with deterministic fakes for offline tests. AGENTS.md is not regenerated: devops ai agents would write its short template over the curated file (ai/instruction_generator.py:412-427, docs(agents): instruction surfaces state only what is true, and devops ai agents never overwrites AGENTS.md #840).
  • The new and changed tests run offline, with no model, network or cluster.
  • uv run devops ci passes.
  • (person-run) devops ai chat and one devops review path run as before.

Key questions

Update 2026-10-08: refinement

Owner decision on #1001: resolve_pydantic_ai_model, the pydantic-ai providers and ai/direct.py stay; #1331 deletes the legacy classes. The first criterion stands: delete the fallback and dead factories (ai/pydantic_ai_bridge.py:262-494, ai/gateway.py:555, and ai/durable.py:583 unless #1331 removed it). #1001 owns the provider route and the AGENTS.md:444 bullet. Open key question for the owner: adopt the native pydantic_ai.Agent? (proposed) Yes, once parity for deferred tools, thinking and MCP sampling passes.

Activity

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

    @dan-petty
    OwnerAuthor

    Owner decision on #1001, 2026-10-08: the model seam is pydantic-ai models through pydantic_ai.direct. That reverses this item's criterion deleting resolve_pydantic_ai_model, the pydantic-ai providers in ai/providers/__init__.py and ai/direct.py: #1001 rebuilds that route on the egress factory client as the one model seam. The legacy BaseLLMProvider classes (#1331) still go.

    Key question for the owner: with pydantic-ai as the model layer, should the agent runtime become the native pydantic_ai.Agent over that seam, deleting the hand-rolled PydanticAgent loop? This item's deletion test already names that option. Proposed answer: yes, once the parity check in this item's key questions passes (deferred tools, thinking, MCP sampling, #958). Until then, keep the hand-rolled loop and delete the silent fallback as planned.

  4. dan-petty commented on Oct 17, 2026

    @dan-petty
    OwnerAuthor

    Amendment from the gateway usage investigation of 2026-10-17, at main 3dd7fa6.

    • The native constructors this item deletes send requests through pydantic-ai's own HTTP clients and never call track_request_spend: create_pydantic_ai_agent, get_persona_pydantic_agent, execute_structured_workflow and its _sync variant, build_fallback_cascade_model, GatewayRouter.build_pydantic_cascade_model and create_durable_pydantic_agent.
    • SpendGuard (src/devops_cli/ai/agents/spend.py:56-76) and the governance token bucket only count in memory.
    • Deleting them closes that accounting gap. No scope change is needed.
    • If the owner later adopts the native pydantic_ai.Agent (this item's open key question), the adopting change attaches one capability whose after_model_request calls track_request_spend from response.usage, response.model_name and the current stage.
    • That change then needs an end-to-end test: a native run on TestModel shows up in devops_cli_ai_requests_total and in the ledger.

    No traffic on these paths in 30 days, so priority stays P2.

  5. modified the milestone: v0.2.37 on Oct 20, 2026
  6. added
    status/ready
    value/medium
    effort/medium
    on Oct 28, 2026
  7. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Closed as not planned. The item conflicts with a more recent project direction. #2489 carries its goal, one agent runtime with no silent fallback, in the direction that the project now takes.

    Evidence (origin/main 08d70ca, 2026-10-28):

    No code was written for this item.

  8. removed
    status/ready
    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

    effort/mediumpriority/p2-mediumscope/aitype/refactorvalue/medium

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions