Skip to content

refactor(ai): remove the agent test switches and test models #3757

Closed

Description

@dan-petty

Problem or Motivation

Part of #1330. The agent code in src carries switches and test models that only tests use. docs/TESTING.md says that product code holds nothing for tests, and that a fake never ships in the wheel.

Measured at origin/main 29309f0:

  • ALLOW_MODEL_REQUESTS is defined twice: ai/agents/agent.py:91 and ai/agents/testing.py:16. The agent loop reads both copies (agent.py:875-888). The bridge reads both in _is_testing_mode_active (ai/pydantic_ai_bridge.py:145-152).
  • resolve_pydantic_ai_model returns pydantic-ai's TestModel when that function is true or when the model name is the string "test" (pydantic_ai_bridge.py:189). Only tests/ai/agents/test_pydantic_agent.py:886 passes "test" to it. tests/ai/agents/test_pydantic_ai_native_capabilities.py:108 passes model="test" to PydanticAgent.
  • _RUN_MESSAGES_CAPTURE and the helper capture_run_messages (ai/agents/testing.py:17, :36) feed a buffer that the loop fills (agent.py:970, :996). Only one test reads it. AgentResponse.messages already holds the same list.
  • ModelNotAllowedError (testing.py:22) has a row in docs/ERRORS.md:83 and an entry in CONST_DOMAIN_EXCEPTION_MODULES (config/constants.py:156).
  • ai/agents/testing.py ships TestModel, FunctionModel and AgentInfo in the wheel. They are not pydantic-ai models. They have the chat and chat_messages methods of an LLMClient.
  • Three tests patch _is_testing_mode_active to a function that returns False: tests/ai/client/test_gateway_caller_headers.py:149, tests/ai/test_pydantic_ai_profiles_and_providers.py:114 and tests/roadmap/test_roadmap_implement_step_output_cap.py:178. They raise AttributeError after the function is deleted.
  • Four test files import the module or use the switches: tests/ai/agents/test_ai_agent_capabilities.py, tests/ai/mcp/test_pydantic_ai_mcp.py, tests/ai/models/test_pydantic_ai_ollama.py and tests/invariants/test_architectural_invariants.py (the __test__ = False check).
  • test_embedding_cost_of_a_local_model_is_zero_whatever_its_custom_price (test_ai_agent_capabilities.py:291-292) patches devops_cli.ai.spend.get_pricing_registry and spies on calculate_request_cost. No other child owns this patch.

Premise check. The 2026-10-07 text says that a test of an agent run with no model fake "fails with #1310's edge error". I ran such a call at origin/main with the root conftest, through pytester. The inner test get_llm_client().chat("x") with no model_fake passes at the call and fails at teardown. The error reads "network edge reached without a fake" and names the keyring edge devops-cli/ai_api_key. A second inner test, with model_fake, passes and has model_fake.call_count == 1. So the proof test runs an inner test through pytester, as tests/invariants/test_hermetic_floor.py does for the other edges. A direct assertion is impossible, because the floor fails the test at teardown. The proof test calls get_llm_client, not PydanticAgent, because #2489 deletes PydanticAgent.

Proposed Solution & Architectural Design

Delete the switches and the test models. Replace their users with model_fake (#1001) or, for a pydantic-ai Model, pydantic-ai's own TestModel and FunctionModel. Add no new test for PydanticAgent, runner.py or the pipeline, because #2489 deletes them. A case that tests only the deleted module is deleted.

Acceptance criteria

  • git grep -nE 'ALLOW_MODEL_REQUESTS|ModelNotAllowedError|MODEL_REQUESTS_DISABLED|_RUN_MESSAGES_CAPTURE|_is_testing_mode_active|agents\.testing|agents/testing' -- src tests docs ':!docs/agent/archive' returns no line.
  • src/devops_cli/ai/agents/testing.py does not exist. Its entry is gone from CONST_DOMAIN_EXCEPTION_MODULES.
  • _is_testing_mode_active and the model == "test" branch are gone from ai/pydantic_ai_bridge.py. resolve_pydantic_ai_model has no mode check. The existing test of ollama: and bare-name resolution still passes.
  • Case 1 of test_resolve_pydantic_ai_model (the "test" string) is deleted. The cases for an existing Model instance, None and ollama: stay. test_dual_compatibility_with_devops_cli_pydantic_agent passes a pydantic-ai TestModel() instance, not the string "test".
  • The three patches of _is_testing_mode_active are deleted. Their tests still pass.
  • The agent loop has no mode check before self.client.chat_messages(...), and no capture-buffer write.
  • docs/ERRORS.md is regenerated with uv run devops docs generate, and uv run devops docs check passes. The file has no ModelNotAllowedError row.
  • A new test in tests/invariants/test_hermetic_floor.py, through _run_with_floor, runs an inner test that calls get_llm_client().chat("x") with no model_fake. The outer result has one error at teardown. The output holds "network edge reached without a fake". A second inner test runs the same call with model_fake and passes, and model_fake.call_count == 1.
  • test_testing_utilities_and_agent_override in test_ai_agent_capabilities.py keeps only its Agent.override cases. They run on model_fake or pydantic-ai's TestModel, and check the reply and len(response.messages) > 0. The cases for the deleted TestModel, FunctionModel, capture_run_messages and ALLOW_MODEL_REQUESTS are gone. If Agent.override cannot take a fake, delete the case. refactor(ai): Replace PydanticAgent with pydantic-ai Agent #2489 deletes the class.
  • test_embedding_cost_of_a_local_model_is_zero_whatever_its_custom_price gets its registry from get_pricing_registry(data_dir=tmp_path). It patches no devops_cli name and spies on no method. It checks the two zero costs. The check that the provider and the timestamp reach the registry moves to a test of PricingRegistry.calculate_request_cost in tests/ai/spend/test_ai_spend_pricing.py.
  • test_pydantic_ai_ollama.py loses its "Offline test mode bypass" block. It uses pydantic-ai's TestModel where it used the deleted one.
  • test_test_model_pytest_collection_disabled is deleted from test_architectural_invariants.py.
  • test_mcp_toolset (in test_ai_agent_capabilities.py) and test_pydantic_ai_mcp.py use pydantic-ai's TestModel. If the outbound MCP child (D) landed first, those tests are already gone, and this criterion needs nothing.
  • No new test sets a module global, and none patches a devops_cli name.
  • uv run devops ci passes. The pull request body gives the test count before and after.

No ratchet, no ceiling and no baseline file. #1336 bans the forms for the whole suite.

Target Subsystem

src/devops_cli/ai/agents/ (agent.py, testing.py), src/devops_cli/ai/pydantic_ai_bridge.py, src/devops_cli/config/constants.py, docs/ERRORS.md, tests/ai/agents/, tests/ai/mcp/, tests/ai/models/, tests/ai/client/test_gateway_caller_headers.py, tests/ai/test_pydantic_ai_profiles_and_providers.py, tests/ai/spend/, tests/roadmap/test_roadmap_implement_step_output_cap.py, tests/invariants/.

Existing Tool Considered

pydantic-ai ships TestModel and FunctionModel (https://pydantic.dev/docs/ai/guides/testing/). The project already ships model_fake (tests/support/fakes/model.py) and the pytester pattern of tests/invariants/test_hermetic_floor.py. Nothing new is built. This item only deletes code.

Proposed design

Problem statement

The agent code carries test-only switches and test models. docs/TESTING.md says that product code holds nothing for tests and that a fake never ships in the wheel. This change deletes those switches and test models and replaces their users with model_fake or pydantic-ai TestModel and FunctionModel. It also adds a floor test that proves a model edge without a fake fails at teardown.

Acceptance criteria

Requirement: Removed names are absent

The source, tests and documentation SHALL hold no reference to ALLOW_MODEL_REQUESTS, ModelNotAllowedError, MODEL_REQUESTS_DISABLED, _RUN_MESSAGES_CAPTURE, _is_testing_mode_active, agents.testing or agents/testing outside docs/agent/archive.

Scenario: Search for the removed names

  • WHEN A person runs the git grep command from the issue over src, tests and docs, excluding docs/agent/archive.
  • THEN The command exits with status 1 and prints no line.

Requirement: Test module and constants entry are gone

src/devops_cli/ai/agents/testing.py MUST not exist and CONST_DOMAIN_EXCEPTION_MODULES MUST not name devops_cli.ai.agents.testing.

Scenario: Check the deleted module

  • WHEN The change is applied and a test reads the constants tuple.
  • THEN The path does not exist and the tuple has no entry for the module.

Requirement: Bridge has no test mode

src/devops_cli/ai/pydantic_ai_bridge.py SHALL not define _is_testing_mode_active or treat the model name "test" as a TestModel request.

Scenario: Resolve a real model

  • WHEN The existing tests of ollama: and bare-name resolution run.
  • THEN Both tests pass and the bridge source has no mode check.

Requirement: Resolve test uses a model instance

The test of resolve_pydantic_ai_model SHALL remove only the "test" string case and SHALL keep the cases for a Model instance, None and ollama:.

Scenario: Run resolve model tests

  • WHEN The test file runs.
  • THEN The "test" string case is absent and the other three cases pass.

Requirement: Compatibility test uses TestModel instance

test_dual_compatibility_with_devops_cli_pydantic_agent SHALL pass a pydantic-ai TestModel() instance instead of the string "test".

Scenario: Run dual compatibility test

  • WHEN The test runs.
  • THEN It passes with a TestModel instance and no string "test" is passed.

Requirement: Patch sites are gone

The three tests that patch _is_testing_mode_active MUST no longer patch it and MUST still pass.

Scenario: Run the three former patch tests

  • WHEN The three test files run.
  • THEN No test patches _is_testing_mode_active and all three tests pass.

Requirement: Agent loop has no test branch

The agent loop SHALL call self.client.chat_messages(...) without a mode check and SHALL not write to a capture buffer.

Scenario: Run an agent on model_fake

  • WHEN A test runs an agent on model_fake.
  • THEN The fake receives the chat and the source has no capture-buffer write.

Requirement: Error docs are regenerated

docs/ERRORS.md MUST be regenerated with uv run devops docs generate and MUST have no ModelNotAllowedError row.

Scenario: Check generated error docs

  • WHEN A person runs uv run devops docs generate and uv run devops docs check.
  • THEN The check exits 0 and the file has no ModelNotAllowedError row.

Requirement: Floor test proves the model edge

A new test in tests/invariants/test_hermetic_floor.py SHALL run two inner tests through _run_with_floor and SHALL assert the unfaked model call fails at teardown with the network edge message.

Scenario: Model edge without a fake fails at teardown

  • WHEN The new floor test runs.
  • THEN The outer result reports one error at teardown, the second inner test passes, the output contains "network edge reached without a fake", and model_fake.call_count is 1.

Requirement: Agent override cases use a supported double

The Agent.override cases in tests/ai/agents/test_ai_agent_capabilities.py SHALL run on model_fake or pydantic-ai TestModel and SHALL assert the reply and a non-empty response.messages list.

Scenario: Run agent override cases

  • WHEN The Agent.override test runs.
  • THEN The reply matches and len(response.messages) > 0.

Requirement: Deleted utility cases are gone

The test cases for the deleted TestModel, FunctionModel, capture_run_messages and ALLOW_MODEL_REQUESTS SHALL be gone from tests/ai/agents/test_ai_agent_capabilities.py.

Scenario: Search the capability tests

  • WHEN The file is searched.
  • THEN No case names or imports reference the deleted utilities.

Requirement: Local embedding cost test uses a registry

test_embedding_cost_of_a_local_model_is_zero_whatever_its_custom_price SHALL get its registry from get_pricing_registry(data_dir=tmp_path) and SHALL assert both zero costs.

Scenario: Local embedding cost is zero

  • WHEN The test runs with and without a custom price.
  • THEN Both costs equal EmbeddingCost(total_price=0.0).

Requirement: Embedding cost test patches no product name

The embedding cost test SHALL patch no devops_cli name and SHALL spy on no method.

Scenario: Check the embedding cost test

  • WHEN The test source is searched.
  • THEN It holds no monkeypatch.setattr of a devops_cli name and no method spy.

Requirement: Provider and timestamp check moves to pricing

The check that the provider and the timestamp reach the registry SHALL move to a test of PricingRegistry.calculate_request_cost in tests/ai/spend/test_ai_spend_pricing.py.

Scenario: Run pricing registry test

  • WHEN The new pricing test runs.
  • THEN It asserts the provider and request timestamp that reach calculate_request_cost.

Requirement: Ollama tests use pydantic-ai TestModel

tests/ai/models/test_pydantic_ai_ollama.py MUST not hold an offline test mode bypass block and MUST use pydantic-ai TestModel where it used the deleted model.

Scenario: Run Ollama tests

  • WHEN The file is searched and its tests run.
  • THEN The block is absent and the tests pass.

Requirement: Collection-disabled invariant is deleted

tests/invariants/test_architectural_invariants.py SHALL not define test_test_model_pytest_collection_disabled.

Scenario: Collect the architectural invariants

  • WHEN pytest collects the file.
  • THEN No test named test_test_model_pytest_collection_disabled is collected.

Requirement: MCP tests use pydantic-ai TestModel

Each of test_mcp_toolset and tests/ai/mcp/test_pydantic_ai_mcp.py that still exists SHALL use pydantic-ai TestModel and SHALL not use the deleted test model.

Scenario: Run the MCP tests

  • WHEN The MCP tests exist and run.
  • THEN They pass with pydantic-ai TestModel.

Requirement: New tests follow the test rules

The change SHALL add no test that sets a module global or patches a devops_cli name.

Scenario: Search the new tests

  • WHEN The diff of the change is searched.
  • THEN It holds no module-global assignment and no patch of a devops_cli name.

Requirement: CI gate passes

uv run devops ci MUST pass and the pull request body MUST give the test count before and after.

Scenario: Run the full CI gate

  • WHEN uv run devops ci runs on the change.
  • THEN The command exits 0 and the pull request body holds the two test counts.

Requirement: No ratchet is added

The change SHALL add no ratchet, ceiling or baseline file.

Scenario: Check for new ratchets

  • WHEN The diff is searched for a new baseline or ceiling file.
  • THEN No ratchet, ceiling or baseline file is added.

Requirement: No new test for the deleted modules

The change SHALL add no new test for PydanticAgent, runner.py or the pipeline.

Scenario: Search the new tests

  • WHEN The diff of the change is searched.
  • THEN No new test names PydanticAgent, runner.py or the pipeline.

Existing Tool Considered

No alternatives check has run. pydantic-ai ships TestModel and FunctionModel (https://pydantic.dev/docs/ai/guides/testing/) for a pydantic-ai Model, and the repository ships model_fake (tests/support/fakes/model.py) for the LLMClient edge. pydantic-ai's doubles cannot stand in for devops-cli's LLMClient, and model_fake cannot stand in for a pydantic-ai Model. The change builds no new code, so the build cost is zero tokens and zero elapsed time.

Key questions

  • Which test double replaces the deleted hand-rolled TestModel and FunctionModel?: Use pydantic-ai's TestModel and FunctionModel for a pydantic-ai Model, and model_fake for the devops-cli LLMClient edge. The project already ships model_fake, and pydantic-ai ships the Model doubles, so the change builds no new code. (best practice) [sources: docs/TESTING.md, tests/support/fakes/model.py]
    • Chosen: pydantic-ai TestModel and FunctionModel with model_fake. pydantic-ai covers a Model and model_fake covers the LLMClient edge.
    • Rejected: Keep the hand-rolled TestModel and FunctionModel. They are test-only code in product, and they ship in the wheel.
    • Rejected: Use model_fake for every double. model_fake is an LLMClient double and cannot stand in for a pydantic-ai Model.
  • How does a test prove the hermetic floor refuses a model edge with no fake?: Add a test in tests/invariants/test_hermetic_floor.py that runs an inner test through _run_with_floor. The inner test calls get_llm_client().chat("x") with no model_fake. The floor fails the inner test at teardown with "network edge reached without a fake". A second inner test uses model_fake and passes with one call. (best practice) [sources: tests/invariants/test_hermetic_floor.py, docs/TESTING.md]
    • Rejected: Direct assertion in one test. The floor fails the test at teardown, so the call itself cannot be asserted.
    • Chosen: Pytester inner test through _run_with_floor. The existing floor tests use this pattern and can assert the teardown error.
    • Rejected: No new test. The proof that the model edge is still refused would be lost.
  • Should ModelNotAllowedError stay in docs/ERRORS.md and CONST_DOMAIN_EXCEPTION_MODULES?: No. Delete the row and the constants entry with the module. Regenerate docs/ERRORS.md with uv run devops docs generate. (best practice) [sources: docs/ERRORS.md, src/devops_cli/config/constants.py, src/devops_cli/docs/generator.py]
    • Chosen: Delete the error with the module. The error has no user after the module is deleted, and the docs generator reads the constants tuple.
    • Rejected: Keep the error row and constants entry. It would document an exception that no code raises.
  • Can Agent.override take model_fake?: Yes. test_agent_override_swaps_the_model_and_the_toolsets calls agent.override(model=model_fake) and asserts the reply and the messages. [sources: tests/ai/agents/test_ai_agent_capabilities.py]
  • Which pydantic-ai double replaces the string model="test"?: Pass a pydantic-ai TestModel() instance. Use FunctionModel only when a test needs a custom reply. (best practice) [sources: tests/ai/agents/test_pydantic_ai_native_capabilities.py]
    • Chosen: TestModel() instance. It is the pydantic-ai double for a model with no real provider.
    • Rejected: Keep the string "test" branch. It is a test-only branch in product code and the issue deletes it.
    • Rejected: FunctionModel for every case. FunctionModel is for a scripted reply and is not needed where TestModel answers.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/refactor
    scope/ai
    status/new
    value/medium
    effort/medium
    on Nov 6, 2026
  2. dan-petty commented on Nov 6, 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 refactor prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P2-Medium. It removes test-only code from production but does not block users or fix a defect.
    • Value: Medium. It removes confusion and dead code from the shipped wheel, but adds no user-facing behaviour.
    • Effort: Medium. It touches multiple source files, four or more test files, a constants list, and docs, with careful replacement of each usage.
    • Related: test(mcp): mcp, agent and tool tests run on edge fakes #1330, which asks for a different change. Both target removing test switches from src, but #1330 is the umbrella tracker for all six child items while this candidate is one specific child covering the agent switches and test models.
  3. added
    priority/p2-medium
    on Nov 6, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 7, 2026
  5. devops-cli-service commented on Nov 7, 2026

    @devops-cli-service
    Contributor

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

  6. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 7, 2026
  7. removed
    status/in-review
    on Nov 7, 2026
  8. devops-cli-service commented on Nov 7, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-07 17:33 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/mediumpriority/p2-mediumscope/aisource/agenttype/refactorvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions