Skip to content

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

Closed

Description

@dan-petty

Problem

Today's floor (tests/conftest.py, 917 lines, 17 autouse fixtures) isolates the data directory, config, kubeconfig, gh config and session bus, and it blocks external network. It does not deny processes, the keyring, loopback services or the Docker socket, and it resets only some state. Instrumented full runs of a copy of 36cfbbb, with a plugin that recorded the edges each test reached, give these figures [measured: 2026-10-07; counts move by a few between runs, because a background export is charged to whichever test is running]:

  • Every test exports telemetry. The per-test config turns on export to localhost:4318 [code: tests/conftest.py:465, :471], and about 1,800 tests in about 280 files had that connect refused.
  • About 470 tests in about 120 files reach another edge with no fake:
    • Keyring: about 240 tests in 67 files call the keyring library, and none installs a backend. keyring registers every KeyringBackend subclass it has imported, and with the session bus removed it picks the test helper FakeKeyring [code: tests/cluster_secret_fakes.py:37-40] (priority 1) as the process backend. Each worker therefore shares one in-memory keyring, and what one test writes, a later test in that worker reads. The first call in each worker also loads keyring's backends, which start ldconfig, gcc and ld.
    • Processes: 59 tests in 18 files start a process other than git or bwrap. Most of them run Python children or kubectl kustomize [code: tests/test_k8s_argocd_apps.py:229, :284, :366]; fix(k8s): argo cd takes homelab values from deploy-time overrides, and the repository holds no private or placeholder config #1290 has since added more kubectl kustomize runs [code: tests/test_k8s_argocd_apps.py:423; tests/test_k8s_argocd_overrides.py:165, :224; tests/test_k8s.py:1692]. Some run real tools that no fake answers: gh [code: tests/test_github_metrics.py:256], sudo [code: tests/test_devcontainer.py:939], code [code: tests/test_repos.py:960], and Bandit and Semgrep in two tests in test_gateway_review_stall.py.
    • Loopback: about 95 tests in 31 files connect to a loopback port that no test owns, 70 of them to Valkey's 6379. The guard answers ECONNREFUSED, so these tests quietly take the offline path.
    • DNS: about 180 tests in 56 files look up a name that is not loopback with no DNS fake installed, mostly example.com; about 75 of them, in 30 files, look up real names such as radar.cloudflare.com and api.github.com. The guard answers with a lookup failure, so these tests also take the offline path.
    • Docker: 8 tests in test_sandbox_lifecycle.py and test_ui_dashboard.py reach the real engine at /var/run/docker.sock, because the guard passes any address that is not a (host, port) pair [code: tests/conftest.py:88-89].
  • Process-wide state outlives its test. 49 src modules hold it [measured: AST scan at 36cfbbb]: 29 names rebound through global, 20 module-level containers that functions mutate, 40 functools.cache/lru_cache sites and 4 class-level singletons.
    • The floor resets settings, the tracer, the console, the dry-run state, the GitHub session, the Docker engine and the LLM response cache.
    • It calls none of 14 other resets that exist, for example those of KubernetesService [code: k8s/service.py:83], the shared HTTP clients [code: http/pool.py:104] and the GitHub rate limiter [code: github/rate_limiter.py:1344]. It pins the GitHub token by setting a private name [code: tests/conftest.py:357] instead of calling reset_github_token [code: core/process.py:296].
    • 9 states have no reset at all, for example the spend ledger [code: ai/spend/ledger.py:558] and the default HTTP broker, which is built from the environment at import [code: http/broker.py:186]. Neither has the console: conftest assigns its private names directly [code: tests/conftest.py:268-269].
  • Fixture and helper defects.
    • guard_live_rag_data [code: tests/conftest.py:479-520] reads the developer's config before any test's config is isolated (:493). It guards only URLs that contain that config's k8s.domain, and it rebinds QdrantClient.__init__ for the rest of the run.
    • verify_test_paths_isolated [code: :537-550] cannot fail, because it checks the tmp_path values the other fixtures set. Its constant, CONST_ISOLATED_TEST_ENV_KEYS [code: config/constants.py:444], has no other reader.
    • tmp_ssh_dir, tmp_repos_dir and mock_settings [code: tests/conftest.py:695-706, :718-729] have no users; test_cloudflare_commands.py defines its own mock_settings [code: tests/test_cloudflare_commands.py:27-28].
    • forbid_requests [code: tests/cluster_secret_fakes.py:221], called 9 times in three files to refuse every process, keyring call and socket, does by hand what a default deny does for every test, and it patches the tracer's private _resolve_telemetry_settings to do it [code: tests/cluster_secret_fakes.py:231].
  • There is no shared CLI or config fixture. 130 test files build a module-level runner = CliRunner() [measured: AST at 5c45b1e], and tests replace settings by patching load_settings (135 sites in 38 files [measured: census at 36cfbbb]).

Acceptance criteria

Default deny

  • A test that reaches a faked edge with no fake installed fails with an error that names the edge and the target, such as process edge reached without a fake: gh api user. The floor records every reach and fails the test again at teardown, so a test still fails when the code under test catches the error. The deny covers three edges:
    • processes: any process other than git and bwrap, denied at subprocess.Popen, which also covers the async runner and the 11 direct spawners;
    • network: any connect outside loopback, a loopback port or AF_UNIX path that no listener in the test owns, and any DNS lookup of a name that is not loopback;
    • keyring: any read or write, refused by a deny backend that the floor installs with keyring.set_keyring before the first test, so keyring's backend discovery never runs and never selects a test helper such as FakeKeyring.
  • A connect to a port or socket the test itself listens on still passes, so in-process servers stay real.
  • A test that runs a real local tool declares it, and it is skipped where the tool is missing. git and bwrap need no declaration.
  • Every test that reaches an edge today passes under the deny. Each one does one of three things: installs a fake through an existing seam or fixture (docker_engine, stub_web, public_dns, roadmap_store, a keyring fake); declares the state it depends on; or becomes a live test. None adds an internal patch or a private import (test(cli): ratchets hold internal patches, private imports and test accommodations in src at today's counts and name each site that would raise one #1309). The real gh, sudo, code, Bandit, Semgrep and Docker-engine reaches are gone.
  • forbid_requests is deleted. Its 9 calls in tests/test_k8s.py, tests/test_k8s_push_secrets.py and tests/test_k8s_run_job.py rely on the deny instead, which also fails a test whose code caught the refusal.
  • pytester tests cover each edge. A test without a fake fails with the edge's name, including a test whose code catches the error. An in-process server passes. These tests live in tests/invariants/test_hermetic_floor.py, the subject file for the floor, which this PR creates; test(cli): Convert ratchets to bans and merge invariant tests #1336 later merges test_isolation_tripwire.py into it.

Telemetry and state

  • The floor's config turns telemetry export off, and a test opts in through config. No test connects to localhost:4318 unless it opted in.
  • After each test, the floor calls the public reset of every piece of process-wide state in src and closes the shared HTTP clients. Where a public reset is missing, this PR adds one, the console's included.
  • An invariant test fails, naming path:line, on module-level state in src that the floor does not reset. It checks four forms: a name rebound through global, a module-level container a function mutates, a functools.cache/lru_cache function, and a class attribute a classmethod rebinds. It joins the source-tree invariants: tests/invariants/test_source_tree.py if test(cli): ratchets hold internal patches, private imports and test accommodations in src at today's counts and name each site that would raise one #1309 has created it, otherwise tests/test_architectural_invariants.py.

Fixtures

  • config, in the root conftest, writes overrides into the per-test config.yaml that DEVOPS_CLI_CONFIG names, then resets the settings cache. Its tests show load_settings() reading an override, and two tests that do not see each other's overrides. None of them patches load_settings, CONFIG_PATH or get_settings.
  • cli, in the root conftest, runs devops <args> in process through devops_cli.main.app [code: main.py:80] with the floor's terminal settings. It takes input= for stdin and returns the result.
  • These are deleted: guard_live_rag_data, whose job the network deny now does for every Qdrant URL; verify_test_paths_isolated, with _check_test_paths_isolated and its tripwire test; CONST_ISOLATED_TEST_ENV_KEYS; tmp_ssh_dir; tmp_repos_dir; conftest's mock_settings.
  • Summed setup and teardown time from --durations (137.4 s and 20.4 s on 2026-10-07) grows by at most 10%.
  • If test(cli): ratchets hold internal patches, private imports and test accommodations in src at today's counts and name each site that would raise one #1309 has landed, its ceilings drop by the patches this PR removes (the console assignments, guard_live_rag_data's rebinding of QdrantClient.__init__ and forbid_requests' tracer patch among them).
  • changelog.d/<n>.md records the change; CHANGELOG.md and docs/ROADMAP.md are not edited.
  • uv run devops ci passes.

Key questions

Strategic alignment

Activity

  1. added
    type/test
    scope/cli
    priority/p2-medium
    scope/ci
    on Oct 7, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Linked: #1113. #1113 replaces the hand-written Valkey client (valkey/client.py, valkey/protocol.py) and cache/ with GLIDE behind one store; this item's Valkey work targets code #1113 deletes, so whichever lands second moves it onto #1113's store. Found by the 2026-10-08 backlog review.

  3. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. Test isolation gaps make the suite unreliable and can cause flaky or order-dependent failures, but no production code or security boundary is directly broken.
    • Value: High. Without a correct hermetic floor, ~1,800 tests silently take offline paths, ~470 reach un-faked edges, and 49 modules leak state, undermining the entire test suite's trustworthiness.
    • Effort: High. Fixing requires adding fakes for keyring, processes, loopback, DNS and Docker, wiring 14+ missing state resets, and correcting fixture ordering defects across a 917-line conftest with 17 autouse fixtures.
  4. added this to the v0.2.38 milestone on Oct 20, 2026
  5. dan-petty commented on Oct 20, 2026

    @dan-petty
    OwnerAuthor

    Amendment from #1725's kubectl decision (2026-10-20, in the comment on #1725).

    This item's process deny lets only git and bwrap through, and uses a live("kubectl") marker for tests that need kubectl. That now contradicts #1725.

    All 33 tests that start kubectl run its offline renderers: kubectl kustomize, plus one kubectl patch --local. Most of them check the committed k8s/ manifests or kubectl's own behaviour, and none reaches a cluster. There is no maintained kustomize in Python, and a recorded render would check the recording rather than the repository. So these renderers are allowed in tests, like git.

    Change to this item: the process deny admits kubectl kustomize and kubectl patch --local as well as git and bwrap, and refuses every other kubectl subcommand. The incidental kubectl config get-contexts from devcontainer post-start is already gone (perf/1725-devcontainer-tests).

  6. dan-petty commented on Oct 21, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1776, merged into release/v0.2.38 as e66ce73.

    Closes #1310

    Fail-to-pass: not applicable: modifies the test harness (tests/conftest.py edge guards, config/cli fixtures, @pytest.mark.live and @pytest.mark.unreachable markers in pyproject.toml, and process-wide state reset functions across 38 src/devops_cli/ modules).

    151 file(s) changed.

    How it was verified

    Check runs at dabd55d:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • zizmor: pass
    • devops-cli: pass
    • bandit: pass
    • CodeQL: pass
    • DevContainer Image: skipping
    • Fail-to-Pass Check: pass
    • Changed-Test Independence: pass
    • Rule Pack Tests: pass
    • Tests & Coverage: pass
    • Analyze (python): pass
    • Static Analysis: pass
    • Fail-to-Pass Self-Check: pass
    • Analyze (actions): pass
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1310-hermetic-floor.md:

    No Acceptance Criteria section.

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/p2-mediumscope/ciscope/clitype/test

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions