Skip to content

refactor(ai): Valkey, Qdrant and the embedder each have one seam, with shared fakes and contract suites #1317

Description

@dan-petty

Problem

  • Valkey is reached seven ways. ValkeyClient [code: src/devops_cli/valkey/client.py:64] opens its socket in _create_socket (:131-133); the process-wide pool lives in valkey/pool.py (get_pool :202, reset_pool :211). Six modules outside valkey/ construct ValkeyClient inline: commands/valkey.py:47, ui/data_providers.py:642, security/vulnerability_lookup.py:392, ai/run_store.py:250, ai/cache/valkey_cache.py:48 and ai/rag/embeddings.py:423; cache/tiered.py:196 takes the pool. EmbeddingsEngine's default valkey_client sentinel [code: ai/rag/embeddings.py:73] opens and pings a socket on construction and sets the timeout of the private client._sock (:433-434).
  • Qdrant is built in four places. resolve_qdrant_client [code: ai/rag/indexer.py:397] builds the QdrantClient wrapper [code: ai/rag/qdrant.py:142]; commands/rag.py:81, ai/rag/investigator.py:215 and ai/tools/builtin_tools.py:378 build it inline. The session guard guard_live_rag_data reassigns the wrapper's and the SDK's __init__ for the whole run [code: tests/conftest.py:480].
  • The embedder is built in nine places. EmbeddingsEngine [code: ai/rag/embeddings.py:322] is constructed inline at commands/rag.py:87, commands/ai_ingest.py:244, roadmap/intake_model.py:160, ai/rag/investigator.py:227, ai/rag/indexer.py:458, ai/tools/builtin_tools.py:387, ai/agents/embeddings.py:249, ai/benchmark/embedding_runner.py:284 and ai/rag/embeddings.py:1057. Its one Protocol, Embedder, lives in roadmap [code: roadmap/intake_model.py:103].
  • Tests copy their own doubles. Six Qdrant doubles: FakeQdrantClient and RecordingQdrant (tests/test_rag_indexer_retriever.py:17, :236), DummyQdrant (test_rag_retriever_filters.py:24, test_rag_multi_project_indexer.py:45), StubQdrant (test_rag_query_telemetry.py:75) and RecordingQdrant (test_qdrant_timeout_setting.py:107). Valkey doubles include FakeValkey (test_run_store.py:49, test_run_comparison.py:39) and _MockValkey (test_rag_embeddings.py:554). Embedder doubles include FixedVectorEngine (test_rag_indexer_retriever.py:91, test_rag_retriever_filters.py:11), DummyEmbedder (test_rag_multi_project_indexer.py:71), StubEmbedder (test_rag_query_telemetry.py:61) and _Embedder (test_roadmap_intake.py:1190). ValkeyClient._create_socket is patched in 2 test files [measured: grep at 5c45b1e].

Acceptance criteria

Seams

  • Valkey: one place supplies a connected client for the configured endpoint, pooled or not. A Protocol in valkey/ names the calls devops-cli makes: the typed commands (PING, GET, SET with expiry, DEL, MGET, KEYS, SCAN, INFO, DBSIZE, EVAL, FLUSHDB, FLUSHALL, BGSAVE), raw execute and pipelines (ai/run_store.py sends ZADD and ZRANGE this way), with AUTH and SELECT on connect.
  • Qdrant: resolve_qdrant_client is the seam's factory for the wrapper, and the wrapper uses a native qdrant_client.QdrantClient it is given instead of building one in _build_client (ai/rag/qdrant.py:197). A Protocol in ai/rag/ names the wrapper calls the RAG code makes.
  • Embedder: the Embedder Protocol moves from roadmap/intake_model.py:103 next to EmbeddingsEngine, under a name that does not clash with the pydantic-ai Embedder of ai/agents/embeddings.py:175, which ai/rag/embeddings.py already imports (:30). One factory builds the real engine from settings.
  • Each fixture below removes its fake at teardown. Resetting the pool and the module caches (_MODEL_DIMENSION_CACHE, ai/rag/embeddings.py:88; _DIM_MISMATCH_WARNED_COLLECTIONS, ai/rag/qdrant.py:60) after every test is left to the floor (test(cli): the hermetic floor fails a test that reaches an edge without a fake and resets all process-wide state after every test #1310, Q28).
  • The inline constructions listed above are left to the package items that own them.

Fakes and fixtures

  • tests/support/fakes/valkey.py: an in-memory keyspace of strings and sorted sets answering the Protocol's calls, with expiry, recording each command. It can refuse the connection or the password as a server would. It is promoted from FakeValkey (tests/test_run_store.py:49).
  • tests/support/fakes/qdrant.py: in-memory collections with upsert, cosine search, payload filters and deletes, recording each call. It can answer as an unreachable server. It is promoted from FakeQdrantClient (tests/test_rag_indexer_retriever.py:17).
  • tests/support/fakes/embeddings.py: the same text always gets the same vector of the configured dimension; it records each text and whether it was a query, and can fail as a provider error does.
  • Three fixtures install the fakes through the seams and return them, registered once from the root conftest.

Contract suites

  • Valkey: the real ValkeyClient speaks RESP to a loopback server the test owns, which replays recorded exchanges (valkey-server is not installed in the devcontainer [measured: which]). Cases: each command, a wrong password and a refused connection, raising the same exceptions/valkey classes on both sides.
  • Qdrant: the real wrapper runs over qdrant-client's own local mode (the :memory: location), which needs no server [measured: qdrant-client 1.19.1]. Cases: create, upsert, search order and scores, filters, delete, a dimension mismatch, and an unreachable server (a native client pointed at a loopback port nothing listens on), raising QdrantClientError [code: ai/rag/qdrant.py:36] on both sides.
  • Embedder: the real engine, without Valkey, runs against recorded Ollama /api/embed and OpenAI-compatible /v1/embeddings responses served from a loopback server the test owns. Cases: one vector per text in input order, the dimension, empty input and a provider error.
  • (person-run) The Valkey exchanges and provider responses are captured once from local servers and committed under tests/fixtures/ with the commands that captured them.

Migration

Key questions

Strategic alignment

Proposed design

Problem statement

Valkey, Qdrant and the embedder are each constructed inline in multiple modules, and tests copy their own fake doubles. This makes the seams hard to test, the fakes hard to maintain, and the real-client behaviour unverified. The Valkey seam and fake already exist; the Qdrant and embedder seams, fakes and contract suites are missing.

Acceptance criteria

  • Criterion 1: A Protocol in valkey/ names the typed commands, raw execute and pipelines that devops-cli makes.
    Verification: rg -n 'class.*Protocol' src/devops_cli/valkey/ shows a Protocol with ping, get, set, delete, mget, keys, scan, info, dbsize, eval, flushdb, flushall, bgsave, execute and pipeline methods.
  • Criterion 2: resolve_qdrant_client is the factory for the Qdrant wrapper, and the wrapper accepts a native qdrant_client.QdrantClient instead of building one.
    Verification: rg -n 'def resolve_qdrant_client' src/devops_cli/ai/rag/indexer.py exists; rg -n '_build_client' src/devops_cli/ai/rag/qdrant.py prints nothing.
  • Criterion 3: A Protocol in ai/rag/ names the wrapper calls the RAG code makes.
    Verification: rg -n 'class.*Protocol' src/devops_cli/ai/rag/qdrant.py shows a Protocol with ensure_collection, upsert_points, search_points, scroll_points, count_points, delete_points_by_files, delete_points_by_project and is_alive.
  • Criterion 4: The Embedder Protocol lives next to EmbeddingsEngine under a name that does not clash with pydantic-ai Embedder.
    Verification: rg -n 'class.*Embedder.*Protocol' src/devops_cli/ai/rag/embeddings.py exists; rg -n 'from devops_cli.roadmap.intake_model import.*Embedder' src/ prints nothing.
  • Criterion 5: One factory builds the real EmbeddingsEngine from settings.
    Verification: rg -n 'def.*build.*embedder|def.*create.*embedder' src/devops_cli/ai/rag/embeddings.py shows a factory function that takes AIConfig and returns an EmbeddingsEngine.
  • Criterion 6: tests/support/fakes/qdrant.py provides an in-memory fake with upsert, cosine search, payload filters and deletes, recording each call, and can answer as an unreachable server.
    Verification: uv run pytest tests/test_rag_qdrant.py -x --tb=short passes; the file contains a class with upsert_points, search_points, delete_points_by_files and a refusing mode.
  • Criterion 7: tests/support/fakes/embeddings.py provides a deterministic fake that returns the same vector for the same text, records each text and whether it was a query, and can fail as a provider error.
    Verification: uv run pytest tests/test_rag_indexer_retriever.py -x --tb=short passes; the file contains a class with embed_texts that returns fixed vectors and a failing mode.
  • Criterion 8: Three fixtures named valkey, qdrant and embedder install the fakes through the seams and return them, registered from the root conftest.
    Verification: rg -n 'def valkey\b|def qdrant\b|def embedder\b' tests/conftest.py shows three fixture definitions.
  • Criterion 9: The Valkey contract suite tests each command, a wrong password and a refused connection against a loopback server, raising the same exceptions on both sides.
    Verification: uv run pytest tests/test_valkey.py -x --tb=short passes; the tests use serving_valkey or the valkey_client fixture and assert ValkeyAuthenticationError, ValkeyConnectionError and ValkeyCommandError.
  • Criterion 10: The Qdrant contract suite runs the real wrapper over qdrant-client local mode for create, upsert, search order, filters, delete, dimension mismatch and an unreachable server.
    Verification: uv run pytest tests/test_rag_qdrant.py -x --tb=short passes; the tests use QdrantClient with a :memory: location and assert QdrantClientError for the unreachable case.
  • Criterion 11: The embedder contract suite runs the real engine against recorded Ollama and OpenAI-compatible responses from a loopback server.
    Verification: uv run pytest tests/test_rag_embeddings.py -x --tb=short passes; the tests serve /api/embed and /v1/embeddings from a loopback listener and assert one vector per text, the dimension, empty input and a provider error.
  • Criterion 12: Existing tests in test_valkey_concurrency.py, TestValkeyClient in test_valkey.py and test_rag_qdrant.py use the shared fakes or contract suites and patch no devops_cli name or _create_socket.
    Verification: rg -n '_create_socket|patch.*devops_cli' tests/test_valkey_concurrency.py tests/test_valkey.py tests/test_rag_qdrant.py prints nothing.
  • Criterion 13: Tests run offline and each new test runs under 1 second.
    Verification: uv run pytest tests/test_valkey.py tests/test_rag_qdrant.py tests/test_rag_embeddings.py tests/test_rag_indexer_retriever.py --durations=10 -x --tb=short passes with no test over 1 s.
  • Criterion 14: uv run devops ci passes.
    Verification: uv run devops ci exits 0.

Key questions

Open questions

  • Qdrant's real side? (proposed) qdrant-client's local mode: a real implementation of the same client API, in process, so no Qdrant server and no recording are needed.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-10-29: not-applicable.
Tool: none found.

Activity

  1. added
    type/refactor
    scope/ai
    priority/p2-medium
    scope/infra
    on Oct 7, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Folded here instead of a new issue: the agent filing quota (#1153) is spent for this cycle, and this refactor shares this item's change surface. It is recorded here in full so it is not lost.

    refactor(ai): embedding and qdrant calls retry through tenacity and classify failures by exception type and status, not message text

    Problem

    Checked at release/v0.2.29 (05e9388). Embeddings and the Qdrant wrapper each hand-roll retries and decide what failed by searching the error message. The project already retries model traffic with pydantic-ai's HTTPX2TenacityTransport and wait_retry_after (ai/retries/__init__.py:14-22, :108-137), reached through new_http_client(retries=...) (http/client.py:163-166), and tenacity is a direct dependency.

    • Embeddings (ai/rag/embeddings.py):
      • two backoff functions, _calculate_backoff_delay (:76-84) and _compute_retry_delay (:267-274), with time.sleep loops (:811-813);
      • Retry-After is honoured only when it is all digits (:270-272), so an HTTP-date value is ignored;
      • _is_transient_embedding_error (:239-264) matches substrings such as "timeout" and "errno 104";
      • four raw httpx2.Clients (:380, :629, :659, :796) bypass the factory and its retry transport, and the endpoint is joined by string (:654-657).
    • Qdrant (ai/rag/qdrant.py):
      • _execute_with_retry (:246-253) loops with a linear time.sleep(0.5 * attempt) and no jitter (:122);
      • _is_transient_qdrant_error (:40-57) matches substrings such as "timeout" and "connection reset";
      • not-found is "not found" in err or "404" in err (:308, :373, :466), so any message containing "404" (a 500 body, a collection name) reads as not found;
      • a dimension mismatch is recognised by the text "dimension error" (:468);
      • qdrant-client already raises ResponseHandlingException for transport failures and UnexpectedResponse, which carries status_code, for HTTP errors.

    Proposed remedy

    Acceptance criteria

    Related: #1066, #1317, #1381, #1001, #1065, #1019, #1320

    Found by the 2026-10-08 client-library audit.

  3. 5 remaining items

  4. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

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

    • Priority: P2-Medium. This is an architectural improvement that enhances testability and maintainability but does not address a critical issue or bug.
    • Value: High. Improving the architecture of core components like Valkey, Qdrant, and embedder will benefit all downstream code and testing efforts.
    • Effort: High. Refactoring multiple components with their associated tests and fakes requires significant development effort.
    • Related: refactor(cli): Unify HTTP and DNS seams with shared fakes #1314, which asks for a different change. Both items involve introducing a single seam for a component with multiple inline constructions. However, the candidate focuses on Valkey, Qdrant and embedder specifically, while item 1314 targets HTTP clients and DNS resolution.
  5. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Refine cannot set this item to Ready at 69636b6 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.

    • Qdrant's real side? (proposed) qdrant-client's local mode: a real implementation of the same client API, in process, so no Qdrant server and no recording are needed.
  6. added
    status/new
    effort/high
    value/high
    on Oct 28, 2026
  7. devops-cli-service commented on Oct 30, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Tool: none found

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

  8. added
    status/in-progress
    and removed
    status/new
    on Oct 31, 2026
  9. added
    status/in-review
    and removed
    status/in-progress
    on Oct 31, 2026
  10. removed
    status/in-review
    on Oct 31, 2026
  11. added a commit that references this issue on Oct 31, 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/aiscope/infratype/refactorvalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions