Skip to content

refactor(ai): Replace PydanticAgent with pydantic-ai Agent #2489

Closed

Description

@dan-petty

Problem or Motivation

Two agent runtimes run in production (origin/main 08d70ca, 2026-10-28):

#1005 planned the opposite direction: keep the hand-rolled loop as the only runtime and delete every native Agent. #2398 reversed that direction, so #1005 is closed as not planned. pydantic-ai is the project's model library (owner decision on #1001, 2026-10-08), and the proposed answer on #1005 (2026-10-08) was to adopt the native Agent once parity passes.

Proposed Solution & Architectural Design

  • One construction point builds a pydantic-ai Agent over the task's model (get_task_model, src/devops_cli/ai/client/factory.py). It attaches one capability whose after_model_request hook records each model reply with track_request_spend (amendment on refactor(ai): one agent runtime behind one construction point, with no silent fallback #1005, 2026-10-17). A native run then shows in devops_cli_ai_requests_total and in the spend ledger. The refine investigation uses this construction point.
  • devops ai chat runs on it: text and thinking stream from run_stream_events (ThinkingPartDelta), the persona tools are pydantic-ai tools, the chat invariants are instructions, and the conversation is the message history.
  • devops ai pipeline runs each stage on it.
  • The hand-rolled loop is deleted: PydanticAgent, runner.py, MultiAgentPipeline, AgentMemory and their tests. The modules that other packages still import (agents/embeddings.py for RAG, agents/context.py and agents/tools.py for ai/toolsets/ and ai/tools/) move next to their users, or stay with a statement why.
  • GatewayRouter.build_pydantic_cascade_model (src/devops_cli/ai/gateway.py) has no caller in src. It is deleted with its tests.
  • The change is probably larger than one pull request. Proposed split: (1) the construction point with the spend capability, and the refine investigation on it; (2) devops ai chat; (3) devops ai pipeline; (4) delete the hand-rolled loop.

Target Subsystem

src/devops_cli/ai/agents/, src/devops_cli/commands/ai.py, src/devops_cli/roadmap/refine_investigation.py, src/devops_cli/ai/client/factory.py, src/devops_cli/ai/gateway.py.

Existing Tool Considered

pydantic-ai 2.54.0, the pinned version: Agent, run_stream, run_stream_events, ThinkingPart and ThinkingPartDelta, and the capabilities Hooks (with after_model_request), MCP, HandleDeferredToolCalls and ProcessHistory (read in the installed package). The change builds no tool loop, and it deletes the hand-rolled one.

Key questions

  • Which pydantic-ai feature covers each feature of the loop that devops ai chat and devops ai pipeline use: streaming, thinking, persona tools, chat invariants and memory, MCP tools? (proposed) The features above. A gap stops the move of that command, and the item names the gap.
  • Refine records one spend record for each run today (_record_spend). Does it record one for each model request? (proposed) Yes, as LLMClient does.
  • Does devops ai pipeline stay? (proposed) Yes, moved as it is.

Acceptance criteria

  • One function in src builds an agent, and rg "PydanticAgent|_detect_tool_intent" src finds nothing.
  • With TestModel or FunctionModel, a chat turn and a pipeline stage run offline, and each model request adds one spend record.
  • GatewayRouter.build_pydantic_cascade_model is deleted.
  • uv run devops ci passes.
  • Pending a person: devops ai chat with a persona and its tools runs against the gateway as before.

Related

Supersedes #1005. Related: #1001, #2398, #958, #1003 (#2452).

Source: #1005

Alternatives check of 2026-11-04: not-applicable.
Class: refactor, no search.
Tool: none found.

Proposed design

Problem statement

Two agent runtimes run in production. The hand-rolled PydanticAgent loop reads tool calls from the model's text and catches every RAG error. pydantic-ai's Agent already runs the refine investigation (#2398). This item makes that Agent the one runtime for devops ai chat and devops ai pipeline, records one spend record for each model request, and deletes the hand-rolled loop.

Acceptance criteria

Requirement: one-construction-point

The refine investigation, devops ai chat and devops ai pipeline SHALL build their pydantic-ai Agent through one function in src/devops_cli/ai/client/factory.py.

Scenario: the-three-users-call-the-construction-point

  • WHEN rg -n 'Agent\(' src/devops_cli/commands/ai.py src/devops_cli/roadmap/refine_investigation.py runs
  • THEN the command finds no match, and both files import the construction function from src/devops_cli/ai/client/factory.py

Requirement: hand-rolled-loop-deleted

The src tree MUST hold no PydanticAgent, _detect_tool_intent, MultiAgentPipeline or AgentMemory, and MUST hold no ai/agents/agent.py, ai/agents/runner.py, ai/agents/pipeline.py or ai/agents/memory.py.

Scenario: removed-identifiers-and-modules-absent

  • WHEN rg 'PydanticAgent|_detect_tool_intent|MultiAgentPipeline|AgentMemory' src runs
  • THEN the command finds no match, and the four loop modules do not exist under src/devops_cli/ai/agents/

Requirement: chat-runs-offline

A devops ai chat turn SHALL run offline on the pydantic-ai Agent with the model_fake fixture and its persona tools.

Scenario: chat-turn-with-a-persona-tool

  • WHEN devops ai chat runs one turn with a persona whose tool the model_fake fixture calls
  • THEN the console shows the tool result and the reply, and no model request leaves the machine

Requirement: pipeline-runs-offline

Each devops ai pipeline stage SHALL run offline on the pydantic-ai Agent with the model_fake fixture.

Scenario: two-stage-pipeline

  • WHEN devops ai pipeline runs two personas with the model_fake fixture
  • THEN the command prints one section for each stage, and no model request leaves the machine

Requirement: spend-per-model-request

The construction point SHALL add exactly one spend record for each model request of a run.

Scenario: two-request-run-adds-two-records

  • WHEN a chat turn makes two model requests with the model_fake fixture
  • THEN the spend ledger holds two new records for the run

Requirement: cascade-model-deleted

GatewayRouter MUST hold no build_pydantic_cascade_model method.

Scenario: method-absent

  • WHEN rg 'build_pydantic_cascade_model' src tests runs
  • THEN the command finds no match

Requirement: chat-alias-deleted

src/devops_cli/commands/ai.py MUST hold no _stream_interactive_chat_turn alias.

Scenario: alias-absent

  • WHEN rg '_stream_interactive_chat_turn' src tests runs
  • THEN the command finds no match

Requirement: surviving-tests-use-edge-fakes

A test that the deletion keeps SHALL patch no devops_cli name and SHALL import no name that starts with _.

Scenario: no-patch-of-a-project-name

  • WHEN the tests directory is searched for a patch target that starts with devops_cli
  • THEN the search finds no match in a test file that the deletion keeps

Requirement: ci-passes

uv run devops ci MUST pass on the changed branch.

Scenario: ci-suite-green

  • WHEN uv run devops ci runs on the changed branch
  • THEN every check completes with a passing status

Existing Tool Considered

pydantic-ai 2.54.0, the pinned version, covers the need: Agent, run_stream_events, ThinkingPart, ThinkingPartDelta, the Hooks capability with after_model_request, MCP, HandleDeferredToolCalls and ProcessHistory. It lacks a conversation summary of its own, so the chat keeps its history and trims it with ProcessHistory. It also keeps no ledger, so the construction point adds one hook that records each model reply. The alternatives check of 2026-11-04 gave not-applicable for this refactor, named no tool and ran no search, so no other tool was compared. The change builds no tool loop and deletes the hand-rolled one, so it pays no cost to build or keep a loop.

Key questions

  • Which files hold the hand-rolled loop and its tests?: The loop is in src/devops_cli/ai/agents/agent.py, runner.py, pipeline.py and memory.py. Its tests are the seven files that the tracker test(mcp): mcp, agent and tool tests run on edge fakes #1330 comment names, from tests/ai/agents/test_pydantic_agent.py to tests/ai/rag/test_pipeline_context_budget.py. [sources: src/devops_cli/ai/agents/agent.py, src/devops_cli/ai/agents/runner.py, src/devops_cli/ai/agents/pipeline.py, src/devops_cli/ai/agents/memory.py, tests/ai/agents/test_pydantic_agent.py]
  • Which pydantic-ai feature covers each feature that the hand-rolled loop gives devops ai chat and devops ai pipeline: streaming, thinking, persona tools, chat invariants and memory, and MCP tools?: run_stream_events streams the reply and the thinking of one run, with ThinkingPartDelta for the thinking and the text deltas for the reply. A persona tool is a pydantic-ai Tool. The instructions parameter holds the chat invariants. message_history holds the conversation. The MCP capability adds the MCP tools. A gap stops the move of that command, and the item names the gap. (best practice) [sources: src/devops_cli/roadmap/refine_investigation.py, src/devops_cli/ai/agents/agent.py]
  • How does the chat hold the conversation now that AgentMemory is deleted?: The chat keeps the conversation as the run's pydantic-ai message history and passes it to the next turn. The ProcessHistory capability trims it to the model's window. AgentMemory and its summary call go with the loop. (best practice) [sources: src/devops_cli/commands/ai.py, src/devops_cli/ai/agents/memory.py]
    • Chosen: message history with the ProcessHistory capability. pydantic-ai holds the conversation of a run as its message history, and ProcessHistory trims it. No project code stays to keep it.
    • Rejected: keep AgentMemory beside the Agent. The item deletes AgentMemory, and a second memory would keep the duplicate runtime that the item removes.
  • Does the construction point record one spend record for each model request?: Yes. The construction point attaches one capability whose after_model_request hook adds one spend record for each model reply. A run of two model requests adds two records. (best practice) [sources: src/devops_cli/roadmap/refine_investigation.py, src/devops_cli/ai/agents/agent.py]
    • Chosen: one record per model reply through after_model_request. The hook fires after each model request, so the count matches the requests that the run made.
    • Rejected: one record per run. A run of several turns then hides its real spend, and the ledger cannot price a turn.
  • Where does the one construction function live?: In src/devops_cli/ai/client/factory.py, next to get_task_model. That function already returns the pydantic-ai model of a task, with the task's provider and gateway URL. (best practice) [sources: src/devops_cli/ai/client/factory.py]
    • Chosen: src/devops_cli/ai/client/factory.py, next to get_task_model. The file already resolves the model of a task, and the construction point uses that result.
    • Rejected: a new module ai/agents/builder.py. The item deletes the other modules of ai/agents/, so a new module there would sit among the deletions.
  • Where do agents/embeddings.py, agents/context.py and agents/tools.py go?: agents/embeddings.py moves to src/devops_cli/ai/rag/, next to the investigator that uses it. agents/context.py and agents/tools.py stay in src/devops_cli/ai/agents/: they re-export the Tool, AbstractToolset and FunctionToolset types of ai/tools and ai/toolsets, so a move would invert that dependency. (best practice) [sources: src/devops_cli/ai/agents/tools.py, src/devops_cli/ai/agents/embeddings.py]
    • Chosen: move embeddings.py to ai/rag/ and keep context.py and tools.py. The RAG investigator is the one user of embeddings.py. context.py and tools.py hold the shared types that the rest of ai/ imports.
    • Rejected: move all three next to their users. tools.py imports ai/tools and ai/toolsets, so a move of it into those packages would make a cycle.
    • Rejected: keep all three. embeddings.py serves only the RAG path, so it belongs beside the investigator.
  • Does devops ai pipeline stay?: Yes. devops ai pipeline keeps its stages, its tools and its output, and each stage runs on a pydantic-ai Agent from the construction point. (best practice) [sources: src/devops_cli/commands/ai.py, src/devops_cli/ai/agents/pipeline.py]
    • Chosen: keep the command and move each stage to the Agent. The body keeps the command, and only its runtime changes.
    • Rejected: delete the command. The body keeps it, and no other item holds its behaviour.
  • How do the tests that survive the deletion avoid patches of devops_cli names and private imports?: The tests of the deleted loop go with it. A surviving test uses the model_fake, qdrant, embedder, config and cli fixtures, patches no devops_cli name and imports no name that starts with _. The _stream_interactive_chat_turn alias in commands/ai.py is deleted, and the test imports the public name. tests/ai/rag/test_pipeline_context_budget.py points DEFAULT_TIKTOKEN_CACHE_ENV_VARS at an empty directory in place of the patch of _get_tiktoken_encoding. (best practice) [sources: src/devops_cli/commands/ai.py, tests/ai/rag/test_pipeline_context_budget.py, tests/ai/agents/test_pydantic_agent.py]
    • Chosen: edge fakes and public names. The tracker test(mcp): mcp, agent and tool tests run on edge fakes #1330 comment asks for it, and a test on an edge fake keeps its check when the project code changes.
    • Rejected: keep the name patches and the private imports. They bind a test to the code that the item deletes, so the test fails for a reason that is not its own.

Fit

Needs splitting into 4 items:

  1. feat(ai): add one pydantic-ai Agent construction point with a per-request spend capability, and run the refine investigation on it
  2. refactor(ai): run devops ai chat on the construction point's pydantic-ai Agent
  3. refactor(ai): run devops ai pipeline on the construction point's pydantic-ai Agent
  4. refactor(ai): delete the hand-rolled PydanticAgent loop, its tests and GatewayRouter.build_pydantic_cascade_model

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/refactor
    source/agent
    status/new
    value/high
    effort/high
    on Oct 28, 2026
  2. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/refactor. The candidate replaces one agent implementation with another and deletes the old code.
    • Priority: P2-Medium. It is a planned architectural cleanup, not a defect or security fix, so it is not urgent.
    • Value: High. It removes a duplicate runtime, cuts maintenance cost, and aligns the codebase with the project's chosen model library.
    • Effort: High. The candidate states the work spans four pull requests across chat, pipeline, construction point, and deletion.
    • Related: refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001, which asks for a different change. Both work in the ai client layer, but #1001 unifies the model/provider seam while the candidate replaces the agent runtime above it.
  3. added
    priority/p2-medium
    on Oct 28, 2026
  4. devops-cli-service commented on Oct 29, 2026

    @devops-cli-service
    Contributor

    The proposal at 0f7bc26 needs splitting into 4 items. A person splits the item into the items below, or replies in a comment or edits the body to keep one item, and the next refine round looks at the item again.

    1. Construction point with the spend capability, and the refine investigation on it
    2. devops ai chat on the construction point
    3. devops ai pipeline on the construction point
    4. Delete the hand-rolled loop: PydanticAgent, runner.py, MultiAgentPipeline, AgentMemory, GatewayRouter.build_pydantic_cascade_model, and their tests
  5. added
    scope/ai
    on Oct 30, 2026
  6. devops-cli-service commented on Nov 4, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: refactor. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-04. A person decides what this means for the item. The check changes no label and closes no issue.

  7. dan-petty commented on Nov 6, 2026

    @dan-petty
    OwnerAuthor

    Tracker #1330 hands the tests of the hand-rolled loop to this item. #1330 builds no rename, split or new test for that code. This item deletes the code.

    The test files are:

    • tests/ai/agents/test_pydantic_agent.py
    • tests/ai/agents/test_agent.py
    • tests/ai/agents/test_agent_invariants.py
    • tests/ai/agents/test_ai_agent_deferred_tools.py
    • tests/ai/agents/test_pydantic_ai_exceptions.py
    • tests/ai/agents/test_pydantic_ai_native_capabilities.py
    • tests/ai/rag/test_pipeline_context_budget.py

    Measured at origin/main 29309f0. Together they hold 17 patches of devops_cli names and 11 private imports.

    Please keep these points in the pull requests of this item.

    • A test that survives the deletion runs on edge fakes: model_fake, qdrant, embedder, config and the cli fixture. It patches no devops_cli name and imports no _-name.
    • commands/ai.py has the alias _stream_interactive_chat_turn = stream_interactive_chat_turn. Only a test needs it. Delete the alias and import the public name.
    • investigate_rag_context builds its own QdrantClient and EmbeddingsEngine. test(ai): Wire rag once in ai/rag, test with edge fakes #1329 criterion 4 changes that. Until it lands, the pipeline tests patch the investigator.
    • test_pipeline_context_budget.py patches the token counter _get_tiktoken_encoding. Point the tiktoken cache environment variable (DEFAULT_TIKTOKEN_CACHE_ENV_VARS) at an empty directory instead. The counter then fails through its own code.
    • Child A of test(mcp): mcp, agent and tool tests run on edge fakes #1330 removes ALLOW_MODEL_REQUESTS and ai/agents/testing.py. It touches lines of agent.py that this item deletes. Whichever lands first, the other rebases.
  8. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor

    The proposal at 56f1520 needs splitting into 4 items. A person splits the item into the items below, or replies in a comment or edits the body to keep one item, and the next refine round looks at the item again.

    1. feat(ai): add one pydantic-ai Agent construction point with a per-request spend capability, and run the refine investigation on it
    2. refactor(ai): run devops ai chat on the construction point's pydantic-ai Agent
    3. refactor(ai): run devops ai pipeline on the construction point's pydantic-ai Agent
    4. refactor(ai): delete the hand-rolled PydanticAgent loop, its tests and GatewayRouter.build_pydantic_cascade_model
  9. changed the title [-]refactor(ai): pydantic-ai's Agent is the one agent runtime, and devops ai chat and devops ai pipeline leave the hand-rolled PydanticAgent loop[/-] [+]refactor(ai): Replace PydanticAgent with pydantic-ai Agent[/+] on Nov 6, 2026
  10. dan-petty commented on Nov 6, 2026

    @dan-petty
    OwnerAuthor

    The job shortened the title of this item. The summary had 128 characters, and the limit is 60. The type and the scope stay.

    • Before: refactor(ai): pydantic-ai's Agent is the one agent runtime, and devops ai chat and devops ai pipeline leave the hand-rolled PydanticAgent loop
    • After: refactor(ai): Replace PydanticAgent with pydantic-ai Agent
    • Source of the summary: the background model
  11. added
    status/in-progress
    status/in-review
    and removed
    status/new
    status/in-progress
    on Nov 9, 2026
  12. removed
    status/in-review
    on Nov 9, 2026
  13. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor
  14. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-09 17:44 UTC

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/highpriority/p2-mediumscope/aisource/agenttype/refactorvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions