Skip to content

refactor(cli): Unify HTTP and DNS seams with shared fakes #1314

Closed as not planned
3 of 3 issues completed
Closed as not planned

Description

@dan-petty

Problem

  • Three parallel HTTP seams, none injectable. new_http_client and new_async_http_client build plain clients [code: src/devops_cli/http/client.py:55, :66]. get_shared_client and get_shared_async_client cache pooled clients per key [code: http/pool.py:63, :81] and hand back an open one without building a new client [code: http/pool.py:74-75]. HttpClientBroker adds SSRF validation and trace headers; DEFAULT_HTTP_BROKER is built at import and reads DEVOPS_CLI_AI_ALLOW_PRIVATE_NETWORK then [code: http/broker.py:34, :186]. Eight modules outside http/ call them: ai/client/base.py, ai/common_tools.py, ai/harness/docs.py, k8s/service_http.py, security/vault_broker.py, security/vault_lease.py, telemetry/service_status.py and telemetry/memory_profiler.py, which sets its broker client's private _transport (:263-266) [measured: grep at 5c45b1e].
  • Most clients bypass all three. 27 modules outside http/ construct httpx2.Client or AsyncClient, or call httpx2.get/post, inline: ai/client/base.py, ai/gateway.py, ai/library/docs_ingester.py, ai/pool_load.py, ai/providers/{anthropic,ollama,openai}.py, ai/rag/embeddings.py, ai/spend/pricing.py, argo/{fleet,gitops,rollouts}.py, cloudflare/client.py, commands/{ai_gateway,argo,grafana,install_tools,prometheus}.py, github/{client,ssh}.py, k8s/{credentials,logql}.py, sandbox/{metrics,probe}.py, security/vulnerability_lookup.py and telemetry/{tracer,waterfall}.py [measured: grep at 5c45b1e]. Three more sites build a network transport themselves [code: ai/retries/__init__.py:132, :164; ai/client/base.py:62].
  • Tests patch the library. About 77 test patches target httpx2 itself and 44 target devops_cli HTTP wrappers [measured: census of 2026-10-07]; 19 patch lines in 7 test files target the factories above [measured: grep at 5c45b1e]. The only shared double, StubWeb behind stub_web, swaps the httpx2.Client and AsyncClient classes and DEFAULT_HTTP_BROKER [code: tests/conftest.py:231-234], so a pooled client built before it keeps its real transport. 2 test files call close_shared_clients [measured: grep].
  • DNS has a private seam and five bypasses. The bounded lookup is the private _resolve_host_ips [code: core/validation.py:192]; telemetry/waterfall.py:142, sandbox/probe.py:59, sandbox/models.py:157 and :194, and valkey/client.py:105 call socket.getaddrinfo directly. public_dns patches socket.getaddrinfo itself [code: tests/conftest.py:173], test_validation.py hand-builds address tuples [measured: census], and tests/test_pydantic_ai_ollama.py:96 patches _resolve_host_ips.
  • SDKs that bring their own HTTP stack are out of this seam's reach: AsyncOpenAI (ai/models/ollama.py:9), pydantic-ai's providers and qdrant-client over plain httpx (which ai/settings/__init__.py:11 also imports), kubernetes over urllib3, and docker and PyGithub over requests. Their edges are faked at their own seams: refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001, refactor(ai): Valkey, Qdrant and the embedder each have one seam, with shared fakes and contract suites #1317, refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite #1315, refactor(infra): DockerEngineService is the Docker engine seam, with a shared in-memory engine fake and its contract suite #1316, and refactor(github): the github session owns transport, ledger, error classes and link pagination #983's GitHub session once refactor(github): pull request, repository and ssh key calls move from pygithub onto the github session #891 moves the PyGithub calls onto it.

Acceptance criteria

HTTP seam

  • One entry point in http/ builds httpx2 clients, sync and async, pooled and unpooled. new_http_client, new_async_http_client, get_shared_client, get_shared_async_client, HttpClientBroker, get_broker and DEFAULT_HTTP_BROKER are deleted, and the eight modules above build their clients through the entry point, each keeping the timeouts, connection limits, redirect rules, SSRF validation and traceparent header it has today. memory_profiler.py hands its ASGI transport to the entry point instead of setting _transport.
  • The seam's Protocol is httpx2's transport interface (httpx2.BaseTransport, httpx2.AsyncBaseTransport). Every client the entry point builds takes its transport from the seam, pooled clients included, and the three sites that build a network transport themselves (ai/retries/__init__.py:132, :164; ai/client/base.py:62) wrap the seam's transport instead.
  • A hook installs a transport for a test. Installing or removing one closes the pooled clients first, so no client built earlier keeps the old transport. The pool keeps a public reset and its atexit close [code: http/pool.py:104, :257].
  • Nothing in http/ builds a client or reads a setting at import: the private-network setting is read when a client is built.
  • ARCHITECTURE.md (:193) describes the one seam instead of the broker and the pool.

DNS seam

  • One public resolver replaces _resolve_host_ips: a Protocol that resolves a host and port to addresses within a timeout, one place that supplies the real socket.getaddrinfo lookup, and a hook that installs another. core/validation.py resolves through it.

Fakes and fixtures

  • tests/support/fakes/http.py promotes StubWeb [code: tests/web_fakes.py:13]. It answers by method and URL with status, headers and body (JSON included), lets the client follow redirects by its own rules, raises httpx2's connect and timeout errors when told to, and records each request (method, URL, headers, body), so tests compare the recorded requests as one value.
  • tests/support/fakes/dns.py maps names to addresses, can answer every name with one public address as public_dns does, and fails an unknown name with the socket.gaierror the real lookup raises.
  • Two fixtures install the fakes through the seams' hooks, remove them at teardown and return them; both are registered once from the root conftest. Until the five bypassing lookups are routed, the DNS fixture also answers socket.getaddrinfo from the same fake, the stdlib fallback Q24 allows. It replaces public_dns, whose 5 files request it instead.
  • The 4 stub_web files whose code builds its clients through the seam (tests/test_common_tools.py, test_native_common_tools.py, test_http_broker.py, test_runtime_security_and_ssrf_hardening.py) request the HTTP fixture instead, changing only the fixture they request, their imports and their calls to the fake. stub_web stays, answering from the HTTP fake, only for the 2 files whose code builds httpx2 clients inline: tests/test_github_client.py, which test(github): github/ is tested through its public interface over the gh process and session fakes, with no internal patches or private imports #1321 switches, and test_embedding_benchmark.py, which test(ai): the remaining ai modules are tested through their public interfaces on edge fakes, and the legacy provider layer leaves src #1331 switches. Whichever of the two lands second deletes stub_web. tests/web_fakes.py is deleted.

Contract suites

  • The HTTP suite runs each case against the fake and against the real transport, the real side talking to a loopback server the test owns, which the floor lets a test reach [code: tests/conftest.py:86]. Cases: status, headers and body round trip; JSON; redirects and the redirect limit; connection refused and read timeout, raising the same httpx2 exception types on both sides; and a destination that SSRF validation refuses, failing before any request is sent.
  • The DNS suite runs against the fake and the real resolver, the real side resolving only localhost and IP literals, which the floor lets through [code: tests/conftest.py:125]. Cases: a loopback name, an IP literal, an unknown name and a timeout.

Migration

Key questions

  • Why httpx2's transport as the Protocol? Decided 2026-10-07: the HTTP fake runs on httpx2's MockTransport. (proposed) The Protocol is therefore httpx2's own transport interface, which MockTransport implements and StubWeb answers through today; a Protocol of devops-cli's own would restate httpx2's API.
  • Who routes the inline builders and the DNS bypasses? Decided 2026-10-07: each package's migration item (Q24).
  • What about SDKs with their own HTTP stack? Decided 2026-10-07: they are faked at their own seams; patching the stdlib or an SDK object is a fallback for stdlib edges only (Q23, Q24).
  • What about model traffic? (proposed) refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001's adapters take their transport from this seam like every other client. Tests of model behaviour use refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001's fake at the model seam, not this one.
  • Where do the resolver and the suites live? (proposed) The resolver in core/dns.py; the suites in tests/http/ and tests/core/, the mirror directories of their seams.
  • Fixture names? (proposed) web and dns, avoiding the Avoid words of TESTING.md such as stub.

Strategic alignment

Update 2026-10-08: refinement

#898 changed http/: the factories take a required level and dial through VettingBackend (http/client.py:142, :172; http/egress.py:152), and the import-time criterion already holds. The HTTP fake vets hosts with vet_addresses, as StubWeb does; the DNS seam serves the egress lookups (:131, :144).

  • create_retry_transport takes wrapped as required, and no import httpx fallback remains (http/broker.py:8-11, telemetry/memory_profiler.py:247-250).
  • (proposed, owner open) The user agent uses the package version (broker.py:44).

Split into parts (2026-11-06)

This item is the parent of three parts, each one pull request or more. It closes when all three are done.

  1. refactor(cli): one DNS seam under vet_addresses, with a fake #3752, the DNS seam: the DNS seam criterion, the dns fixture and tests/support/fakes/dns.py, and the DNS contract suite.
  2. refactor(cli): one HTTP entry point with a transport hook #3755, the HTTP entry point and fake: the HTTP seam criteria, the tests/support/fakes/http.py fake with the web fixture, the HTTP contract suite, the criteria of the refinement of 2026-10-08, and the tests of http/. The old factory functions stay until part 3.
  3. refactor(cli): callers use the HTTP entry point, old factories go #3758, the caller migration by package: the 27 inline builders, the migration of the stub_web, public_dns and scripted_resolver users to the fixtures, and the deletion of the four factory functions, stub_web and tests/web_fakes.py.

The criteria below stay as the full target. A part closes the criteria that its issue lists.

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

Proposed design

Problem statement

The HTTP clients come from three factory and broker names, and tests patch httpx2 directly. The DNS lookup is not injectable. This item replaces the names with one entry point, adds a resolver seam, and adds shared fakes and contract suites. The measured scope is too large for one pull request, so it splits into three parts.

Acceptance criteria

Requirement: single-http-entry-point

The http package SHALL expose one entry point that builds sync and async httpx2 clients, pooled and unpooled.

Scenario: factory-names-absent

  • WHEN A search of the source tree looks for new_http_client, new_async_http_client, get_shared_client, get_shared_async_client, HttpClientBroker, get_broker and DEFAULT_HTTP_BROKER.
  • THEN The search finds none of those names in src/devops_cli/.

Requirement: transport-seam

The entry point MUST accept a transport typed as httpx2.BaseTransport or httpx2.AsyncBaseTransport, and every client it builds MUST use that transport.

Scenario: retry-transport-wraps-seam

  • WHEN ai/retries/init.py builds a retry transport.
  • THEN It wraps the transport from the seam and does not build httpx2.HTTPTransport.

Requirement: test-hook

A hook SHALL install or remove a test transport and SHALL close pooled clients before the swap.

Scenario: pool-closed-before-swap

  • WHEN The hook installs a transport after a pooled client exists.
  • THEN close_shared_clients is called before the new transport is installed, and the pool keeps its public reset and atexit close.

Requirement: no-import-time-build

No module in http/ SHALL build a client or read a setting at import time.

Scenario: import-time-clean

  • WHEN A search of src/devops_cli/http/ looks for module-scope client construction or DEVOPS_CLI_AI_ALLOW_PRIVATE_NETWORK reads.
  • THEN The search finds no such hit, and the private-network setting is read only when a client is built.

Requirement: retry-transport-required

create_retry_transport SHALL take wrapped as a required argument with no default.

Scenario: wrapped-required

  • WHEN create_retry_transport is called without a wrapped argument.
  • THEN A TypeError is raised, and no import httpx fallback remains in http/ or telemetry/memory_profiler.py.

Requirement: user-agent-metadata

The user agent SHALL be built from importlib.metadata.version('devops-cli').

Scenario: version-from-metadata

  • WHEN The user agent string is built.
  • THEN It contains the installed package version and no hard-coded version string.

Requirement: http-callers-migrated

The eight calling modules outside http/ SHALL use the entry point and SHALL keep their timeouts, limits, redirects, SSRF validation and traceparent header.

Scenario: memory-profiler-transport

  • WHEN telemetry/memory_profiler.py builds its client.
  • THEN It passes its ASGI transport to the entry point and does not set the client's private _transport.

Requirement: dns-resolver-seam

One public resolver under vet_addresses SHALL replace the private resolver and SHALL expose a Protocol, a real lookup and an install hook.

Scenario: resolver-protocol

  • WHEN A search of core/validation.py and http/egress.py looks for _resolve_host_ips.
  • THEN The search finds none, and http/egress.py exposes the resolver Protocol, the real lookup and the install hook.

Requirement: http-fake

tests/support/fakes/http.py SHALL provide a fake that answers by method and URL, follows redirects, raises httpx2 errors, records requests and vets hosts with vet_addresses.

Scenario: fake-records-request

  • WHEN A request with a known method and URL is sent to the fake.
  • THEN The fake returns the configured status, headers and body, and the recorded request equals the expected value.

Requirement: dns-fake

tests/support/fakes/dns.py SHALL map names to addresses, answer every name with one public address on demand, and raise socket.gaierror for an unknown name.

Scenario: unknown-name-fails

  • WHEN The DNS fake resolves a name it does not map.
  • THEN socket.gaierror is raised.

Requirement: fixtures

The web and dns fixtures SHALL install and remove the fakes through the seam hooks, and the root conftest SHALL register both once.

Scenario: fixture-teardown

  • WHEN A test requests the web or dns fixture and finishes.
  • THEN The fixture's teardown removes the installed fake, and the test received the fixture object.

Requirement: dns-stdlib-fallback

Until the five bypassing lookups are routed, the DNS fixture SHALL answer socket.getaddrinfo from the same fake and SHALL replace public_dns.

Scenario: stdlib-fallback

  • WHEN Code under test calls socket.getaddrinfo for a mapped name.
  • THEN The DNS fixture returns the fake's address.

Requirement: stub-web-limited

The four seam-using test files SHALL request the web fixture, and stub_web SHALL remain only for the two files that #1321 and #1331 switch.

Scenario: stub-web-only-two

  • WHEN A search of the test files looks for stub_web requests.
  • THEN Only tests/test_github_client.py and tests/ai/benchmark/test_embedding_benchmark.py request it.

Requirement: http-contract-suite

The HTTP contract suite SHALL run each case against the fake and a real loopback transport, covering response, JSON, redirects, refusal, timeout and SSRF.

Scenario: ssrf-refusal

  • WHEN A request targets a destination that SSRF validation refuses.
  • THEN The request fails before any network I/O, and both sides raise the same httpx2 exception type.

Requirement: dns-contract-suite

The DNS contract suite SHALL run against the fake and the real resolver, covering a loopback name, an IP literal, an unknown name and a timeout.

Scenario: unknown-name

  • WHEN The real resolver resolves an unknown name.
  • THEN socket.gaierror is raised, and the fake raises the same error.

Requirement: no-library-patching

The migrated http/ tests and resolver tests SHALL patch no devops_cli name, no httpx2 class and not socket.getaddrinfo.

Scenario: no-patch-hits

  • WHEN A search of the migrated test files looks for mock.patch or monkeypatch targets.
  • THEN The search finds no target in httpx2, devops_cli.http or socket.getaddrinfo.

Requirement: architecture-doc

ARCHITECTURE.md SHALL describe the one HTTP seam and the DNS resolver seam instead of the broker and the pool.

Scenario: architecture-updated

  • WHEN ARCHITECTURE.md is read.
  • THEN It names the entry point and the resolver and does not describe HttpClientBroker or DEFAULT_HTTP_BROKER.

Requirement: bypasses-left-to-owners

This item SHALL leave the 27 inline httpx2 builders and the five direct getaddrinfo calls to their package items.

Scenario: bypasses-untouched

  • WHEN A reviewer inspects this item's diff.
  • THEN The diff changes no inline builder outside http/ and no direct getaddrinfo call.

Requirement: offline-fast

Each new test SHALL run offline and SHALL complete in under 1 second.

Scenario: timeout-guard

  • WHEN The new test files run with a 1 second timeout and no network.
  • THEN All tests pass.

Requirement: ci-passes

uv run devops ci SHALL pass on the pull request head.

Scenario: ci-green

  • WHEN uv run devops ci runs on the pull request head.
  • THEN All checks pass.

Existing Tool Considered

The alternatives check of 2026-10-29 returned not-applicable and found no applicable open, maintained tool or library. The project builds its own egress-checked transport over httpcore2 and VettingBackend. The written cost is about 120,000 tokens to build, keep and fix the seam, fakes and contract suites, and about 4 engineer-days.

Key questions

Fit

Needs splitting into 3 items:

  1. refactor(cli): one DNS seam under vet_addresses, with a fake #3752: the DNS seam. It delivers the DNS seam criterion, the dns fixture, tests/support/fakes/dns.py, and the DNS contract suite.
  2. refactor(cli): one HTTP entry point with a transport hook #3755: the HTTP entry point and fake. It delivers the HTTP seam criteria, tests/support/fakes/http.py, the web fixture, the HTTP contract suite, the refinement criteria of 2026-10-08, and the tests of http/.
  3. refactor(cli): callers use the HTTP entry point, old factories go #3758: the caller migration by package. It delivers the 27 inline builders, the migration of stub_web, public_dns and scripted_resolver users, and the deletion of the four factory functions, stub_web and tests/web_fakes.py.

Suspected block

None.

Dependencies

None.

Sub-issues

Sub-issues

3 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. added
    type/refactor
    scope/cli
    priority/p2-medium
    on Oct 7, 2026
  2. 19 remaining items

  3. dan-petty commented on Nov 5, 2026

    @dan-petty
    OwnerAuthor

    Return: the item is unblocked, but it is too large for one pull request

    Checked at origin/main 83f8b3d, 2026-11-05. No code was written.

    The premise now holds

    Measured scope (origin/main 83f8b3d)

    • 36 modules outside http/ name the seven factory symbols, at 180 sites.
    • 21 test files name them, at 97 sites.
    • 93 test files use stub_web, public_dns or scripted_resolver (39, 44 and 29 files, with overlap).
    • 141 test lines patch httpx2 and 124 lines mention getaddrinfo.
    • The deletion of the factory names cannot be partial. A part that keeps a name needs a shim, and the project keeps none before 1.0.

    One pull request that meets every criterion would change about 130 files across 20 packages. A reviewer cannot check that, and it conflicts with every parallel package item.

    Proposed split (each part is one reviewable pull request)

    1. DNS seam. The resolver Protocol, the real lookup (socket.getaddrinfo, anyio.getaddrinfo) and the install hook under vet_addresses. tests/support/fakes/dns.py, the dns fixture, and the DNS contract suite in tests/core/. The fixture replaces public_dns and scripted_resolver (73 files, fixture rename only). Needs no HTTP change.
    2. HTTP entry point and fake. One entry point in http/ with the transport hook (closes pooled clients first), required wrapped for create_retry_transport, the user agent from package metadata, and no import-time broker. tests/support/fakes/http.py, the web fixture and the HTTP contract suite in tests/http/. Moves only the 8 http/-adjacent callers named in the body. The old names stay until part 3 finishes, so this part adds the entry point and deletes HttpClientBroker, get_broker and DEFAULT_HTTP_BROKER only.
    3. Caller migration by package, in 4 to 6 pull requests (ai, k8s and argo, security and telemetry, commands and the rest). Each moves its callers and its stub_web tests to the entry point and web. The last one deletes new_http_client, new_async_http_client, get_shared_client, get_shared_async_client and stub_web.

    Part 2 keeps old factory names until part 3 ends. This is a planned order, not a shim: each name has callers that the later parts remove.

    A person decides the split and files the parts. Parts 1 and 2 can start now.

  4. devops-cli-service commented on Nov 5, 2026

    @devops-cli-service
    Contributor

    The proposal at e38c8f3 needs splitting into 3 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. DNS seam: the resolver Protocol, the real lookup and the install hook under vet_addresses, tests/support/fakes/dns.py, the dns fixture, and the DNS contract suite in tests/core/.
    2. HTTP entry point and fake: one entry point in http/ with the transport hook, required wrapped for create_retry_transport, the user agent from package metadata, no import-time broker, tests/support/fakes/http.py, the web fixture, the HTTP contract suite in tests/http/, and the eight http/-adjacent callers.
    3. Caller migration by package in 4 to 6 pull requests: ai; k8s and argo; security and telemetry; commands and the rest. The last part deletes new_http_client, new_async_http_client, get_shared_client, get_shared_async_client and stub_web.
  5. dan-petty commented on Nov 6, 2026

    @dan-petty
    OwnerAuthor

    Answer from project standards, under the owner's instruction of 2026-11-05.

    Question: Does this item fit in one pull request? What must merge before it can start?

    Answer: No. The comment of 2026-11-05 measured about 130 changed files across 20 packages, and the names cannot be removed in part. Nothing must merge first: #2430, #2431, #2437, #1319 and #1312 are closed, and _resolve_host_ips is gone.

    The split is routine work, so I filed the parts as the refine round lists them and linked them as sub-issues of this item:

    1. refactor(cli): one DNS seam under vet_addresses, with a fake #3752, the DNS seam. A pull request builds it now.
    2. refactor(cli): one HTTP entry point with a transport hook #3755, the HTTP entry point and fake.
    3. refactor(cli): callers use the HTTP entry point, old factories go #3758, the caller migration by package. Refine splits it into one issue for each package group when part 2 merges.

    I edited the body of this item to list the parts. This item stays open as the parent, and the pull request of part 1 closes #3752, not this item.

    Rule: AGENTS.md rule 1 (one issue, one pull request) and the "Partial work" rule: a name cannot be removed in part, because the project keeps no shim before 1.0.

  6. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor

    The proposal at 56f1520 needs splitting into 3 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. refactor(cli): one DNS seam under vet_addresses, with a fake #3752: the DNS seam. It delivers the DNS seam criterion, the dns fixture, tests/support/fakes/dns.py, and the DNS contract suite.
    2. refactor(cli): one HTTP entry point with a transport hook #3755: the HTTP entry point and fake. It delivers the HTTP seam criteria, tests/support/fakes/http.py, the web fixture, the HTTP contract suite, the refinement criteria of 2026-10-08, and the tests of http/.
    3. refactor(cli): callers use the HTTP entry point, old factories go #3758: the caller migration by package. It delivers the 27 inline builders, the migration of stub_web, public_dns and scripted_resolver users, and the deletion of the four factory functions, stub_web and tests/web_fakes.py.
  7. changed the title [-]refactor(cli): one HTTP seam replaces the three client factories and one DNS seam the private resolver, each with a shared fake and its contract suite[/-] [+]refactor(cli): Unify HTTP and DNS seams with shared fakes[/+] on Nov 6, 2026
  8. dan-petty commented on Nov 6, 2026

    @dan-petty
    OwnerAuthor

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

    • Before: refactor(cli): one HTTP seam replaces the three client factories and one DNS seam the private resolver, each with a shared fake and its contract suite
    • After: refactor(cli): Unify HTTP and DNS seams with shared fakes
    • Source of the summary: the background model
  9. dan-petty commented on Nov 9, 2026

    @dan-petty
    OwnerAuthor

    This issue was superseded by three completed issues and their merged pull requests:

    All acceptance criteria of this item are met on main. Under AGENTS.md Rule 2, this superseded item closes as rejected.

  10. removed
    status/new
    on Nov 9, 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/clitype/refactorvalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions