Skip to content

test(ai): builtin tool tests run on the process, RAG and HTTP fakes #3761

Closed

Description

@dan-petty

Problem or Motivation

Part of #1330. tests/security/test_builtin_tools.py (938 lines, 31 tests) tests the builtin agent tools. It patches internals for the tools that start a process, for rag_search, and for the OSV, Shodan and Radar clients. The tool rag_search also builds its own Qdrant client and embedder, so no fake can reach it.

Measured at origin/main 29309f0. The file has 23 patch calls:

  • 6 aim at process code: builtin_tools.run_subprocess (5) and builtin_tools._run_tool_cmd (1).
  • 8 aim at rag_search: QdrantClient.is_alive (2), SemanticRetriever.retrieve_context (2), _is_safe_workspace_path (1), config.settings.load_settings, get_ai_api_key and get_qdrant_api_key (1 each).
  • 2 aim at the stdlib: time.sleep.
  • 7 aim at the OSV, Shodan and Radar clients: OSVClient.query_package (2), ShodanInternetDBClient.check_ip (2), httpx2.Client.get (2) and httpx2.Client.post (1).
  • The file imports two private names: _is_safe_workspace_path and _run_tool_cmd.

Already done in this file: the scanner tools run on scanner_fake, with recorded tool output. The file tests/ai/review/test_review_security_tools.py has no patch and no private import. No work remains there.

The OSV, Shodan and Radar clients are reachable by the HTTP fake. security/vulnerability_lookup.py builds all three clients through _public_api_client(). That function calls new_http_client (lines 29, 212-218, 234, 406, 554). The stub_web fixture (tests/support/web.py) answers every httpx2 client from canned pages. I ran two scratch tests on it. scan_osv("requests", "2.0.0") sent POST https://api.osv.dev/v1/query to the fake and gave the clean reply. With stub_web.fail(url, httpx2.ConnectError(...)) it gave "OSV lookup failed: connection refused". A Shodan answer needs the shape of InternetDBHost (ip, ports and vulns). A body without ip gave "the HTTP 200 reply is not from InternetDB". So this child needs no wait on #1314. #1314 plans a web fixture that replaces stub_web for some files. If it has landed when this child starts, use web.

Why the fakes do not reach rag_search today. The Qdrant override (install_qdrant_client) is read only by resolve_qdrant_client (ai/rag/indexer.py:440). The embedder override is read only by create_embedder (ai/rag/embeddings.py:429). rag_search calls QdrantClient(...) and EmbeddingsEngine(...) directly (ai/tools/builtin_tools.py:399, :410).

Proposed Solution & Architectural Design

  1. In rag_search, get the Qdrant client from resolve_qdrant_client(base_url=..., api_key=..., allow_private_network=...) and the embedder from create_embedder(ai_config=..., api_key=...). Both functions exist. SemanticRetriever holds only logic and needs no seam. test(ai): Wire rag once in ai/rag, test with edge fakes #1329 criterion 4 asks the same for the RAG investigator and may add one shared wiring function. Whichever pull request lands first does the work for rag_search. The other one reuses it.
  2. Run the process tools on process_fake. Compare process_fake.calls with the expected argv lists.
  3. Run the RAG tests on the qdrant and embedder fixtures, with keyring_store for the API key and the config fixture for the settings. A test that needs a dead Qdrant sets refusing on the qdrant fake. The retry waits then do not occur, and both time.sleep patches are deleted. If a test keeps refusing_port, it uses the clock fixture of refactor(cli): tests control time: time-machine fakes wall-clock reads, and retry and rate-limit waits return at once and are recorded #1320.
  4. Run the six OSV, Shodan and Radar tests on stub_web. Register the OSV answer, the Shodan answer, a failure status and a connect error. Compare stub_web.sent with the expected requests.

Two gaps that I found in the fakes. I ran a copy of the rag_search body. The copy calls resolve_qdrant_client and create_embedder. It ran on the qdrant, embedder and config fixtures with one seeded chunk. I did not change rag_search. I did not run a Radar case.

  • FakeEmbedder has no embed_query. SemanticRetriever calls embedder.embed_query (ai/rag/retriever.py:63), but EmbedderProtocol declares only embed_texts. The run failed with AttributeError. The fix: declare embed_query in EmbedderProtocol, because a caller uses it, and add it to FakeEmbedder (embed_texts([text], is_query=True)[0]). With that method the run returned the chunk. An empty store gave no result. A refusing qdrant gave "unavailable". A failing embedder raised EmbeddingsError.
  • rag_search calls ensure_knowledge_base_ready before the search. On the first call it finds no point of the installed knowledge base, and starts a rebuild. The rebuild fails on FakeQdrantClient (no attribute 'api_key'), prints "Failed rebuilding knowledge base index" and goes on. The test then passes with a warning on the console. The fix: seed the docs collection with one point whose payload has project_name=CONST_RAG_KB_PROJECT and kb_digest=knowledge_base_digest(). With that seed the check returned True and rebuilt nothing. Put the seed in one helper next to the qdrant fixture. The root conftest calls reset_kb_sync_state after each test, so the state of the first call does not leak.

Acceptance criteria

  • rag_search builds no QdrantClient and no EmbeddingsEngine itself. git grep -nE 'QdrantClient\(|EmbeddingsEngine\(' -- src/devops_cli/ai/tools returns no line.
  • EmbedderProtocol declares embed_query, and FakeEmbedder implements it.
  • A test with the embedder fixture and the qdrant fixture shows that rag_search("query") returns one indexed chunk and prints no rebuild warning. The qdrant fake holds that chunk and the seeded knowledge base. A second test shows the "No semantic matches" reply for an empty store. A third shows the "unavailable" reply when the fake is refusing.
  • The embedding failure test (test_rag_search_names_an_embedding_failure) makes the embedder fake raise EmbeddingsError, and checks the reply.
  • git grep -nE 'patch\("devops_cli\.|monkeypatch\.setattr\("devops_cli\.|patch\("time\.|setattr\("time\.' -- tests/security/test_builtin_tools.py returns no line.
  • The file imports no private name from devops_cli. The tests of _run_tool_cmd and _is_safe_workspace_path run through list_files, read_file, git_status, git_diff, k8s_pods and the other public tools.
  • The tests of the tool wrappers compare process_fake.calls argv with the expected list (kubectl get pods, argocd app list, git status, git diff).
  • Each of the six OSV, Shodan and Radar tests registers its answers on the HTTP fake. It compares the recorded requests (method, URL, body) with the expected list.
  • A test shows that a target the tool excludes (private, loopback, link-local, example) sends no request: the fake records zero requests.
  • A test shows that a failed OSV lookup gives a reply that says the lookup failed, and not a clean package.
  • No test of this file reaches the network. uv run devops ci passes. The pull request body gives the patch and private-import counts before and after.

Target Subsystem

src/devops_cli/ai/tools/builtin_tools.py (rag_search), src/devops_cli/ai/rag/embeddings.py (EmbedderProtocol), tests/support/fakes/embeddings.py, tests/support/fakes/qdrant.py, tests/security/test_builtin_tools.py.

Existing Tool Considered

No new tool. The project seams and fakes exist: process_fake, qdrant, embedder, keyring_store, clock, refusing_port and stub_web. The change calls resolve_qdrant_client and create_embedder, which are already in ai/rag/. The own code is one Protocol method, one fake method and one seed helper. Cost: about 15,000 tokens and 2 hours.

Activity

  1. added
    source/agent
    type/test
    scope/ai
    status/new
    value/medium
    effort/high
    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/test. The test prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P2-Medium. This is a test-quality migration with no user-facing defect, part of a larger tracked effort.
    • Value: Medium. It removes 23 patches and private imports from a 938-line test file, improving reliability, but does not change product behaviour.
    • Effort: High. It requires rewiring rag_search, integrating process_fake, qdrant and embedder fixtures, and stub_web for three HTTP clients across 31 tests.
    • Related: test(ai): Wire rag once in ai/rag, test with edge fakes #1329, which asks for a different change. Both wire rag_search to resolve_qdrant_client and create_embedder, but #1329 covers the RAG and library test files while the candidate covers test_builtin_tools.py including process and HTTP fakes.
  3. added
    priority/p2-medium
    on Nov 6, 2026
  4. added
    status/in-progress
    status/in-review
    and removed
    status/new
    status/in-progress
    on Nov 6, 2026
  5. removed
    status/in-review
    on Nov 6, 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/highpriority/p2-mediumscope/aisource/agenttype/testvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions