Skip to content

fix(test): the test network guard refuses loopback connects to services the test did not start #837

Closed

Description

@dan-petty

Context & Rationale

prevent_external_network_calls (tests/conftest.py:56-120) claims to 'Guarantee that tests never hit external APIs or endpoints'. AGENTS.md:381 forbids live endpoints in test suites.

What the guard actually does:

  • It blocks DNS lookups and connects to hosts that are not loopback.
  • _is_loopback (:65-71) lets every loopback connect and connect_ex through (:95-115).
  • The isolated test config turns telemetry on at http://localhost:4318 (:231, :236).

On a workstation that holds kubectl port-forwards, a loopback connect reaches the cluster.

ROADMAP.md:1046 (P0, no issue) measured this with a probe plugin that refused loopback connects to ports without an in-process listen():

  • 5,217 refused connects to the OTLP collector;
  • 26 to Valkey;
  • 21 to Ollama;
  • the suite still passed 5,732 tests.

CI declares no services:, so it only runs the connection-refused branches, while a workstation runs the live ones. The only per-client escape hatch is the pytest check at ai/rag/embeddings.py:264, together with DEVOPS_CLI_TEST_LIVE_VALKEY (tests/test_rag_embeddings.py:584, :603).

Scope & Deliverables

  • Deliverable (the design in ROADMAP.md:1046):
    • The session guard wraps socket.socket.listen and records the ports the test process owns.
    • A loopback connect to any other port raises ConnectionRefusedError(ECONNREFUSED), and connect_ex returns ECONNREFUSED.
    • Non-loopback connects keep raising the RuntimeError.
    • Delete the check at embeddings.py:264 and DEVOPS_CLI_TEST_LIVE_VALKEY, and rewrite the two tests that set it.
    • Optional: list the refused loopback attempts by port in the terminal summary.
  • Acceptance criteria:
    • A connect to a loopback port that nothing in-process listens on raises ConnectionRefusedError, and connect_ex returns ECONNREFUSED.
    • A test that starts its own server in-process can still connect to it.
    • The full suite passes both with the usual port-forwards running and with none.
    • The task file records the devops ci test-step duration on ext4 before and after the change, with the usual port-forwards running.
    • rg DEVOPS_CLI_TEST_LIVE_VALKEY src tests returns nothing.
  • Key questions:
    • Would pytest-socket be simpler? (proposed) No. ROADMAP.md:1046 found that its --allow-hosts takes hosts rather than ports, and its errors subclass RuntimeError.
  • Constraint:
    • The denial must look like a refused connection. ValkeyClient maps only OSError (valkey/client.py:168-174), and ValkeyCache.is_available catches only ValkeyError (ai/cache/valkey_cache.py:58-61), so a RuntimeError would escape both.
    • Subprocesses spawned by tests are not covered by an in-process guard. Say so in the guard's docstring.
    • One PR, and the task file marks ROADMAP.md:1046 delivered.

Strategic Alignment

  • Milestone: v0.2.24 if admitted as a critical fix; otherwise Backlog, ranked first.
  • Design: CONTEXT.md (Critical fix)
  • Depends on: none

Activity

  1. added
    type/bug
    scope/cli
    priority/p0-critical
    on Oct 1, 2026
  2. added this to the v0.2.24 milestone on Oct 1, 2026
  3. dan-petty commented on Oct 1, 2026

    @dan-petty
    OwnerAuthor

    Admitted to v0.2.24 as a critical fix, by the owner's decision (2026-10-01): a P0 defect fix. Every test run reaches live loopback services, and the guard's own docstring says it shouldn't.

  4. dan-petty commented on Oct 1, 2026

    @dan-petty
    OwnerAuthor

    Closed by #866, merged into release/v0.2.24.

    GitHub only acts on closing keywords when a pull request merges into the default branch, so this was closed by devops gh issues close-merged.

  5. added a commit that references this issue on Oct 1, 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

    priority/p0-criticalscope/clitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions