Skip to content

fix(ci): tests touch no state outside their temporary directories #1311

Closed

Description

@dan-petty

Problem

A test run writes state outside its temporary directories. Full runs of a copy of 36cfbbb with HOME pointed at an empty directory (more than 9,270 tests passed in each) left the following in that home [measured: 2026-10-07]:

  • .config/devops-cli/tls/ with a CA key and certificate, a server key and certificate, and a full chain.
    • tests/test_k8s.py:676 runs enable-tls --stack all without --tls-dir, so the command uses DEFAULT_TLS_DIR [code: config/defaults.py:170] and generates a bundle there when none exists [code: commands/k8s/tls_management.py:229-231].
    • On a developer's machine, the same test uses the developer's own certificate and key from that directory [inference].
  • .local/share/devops-cli/worktrees/, from the review worktrees of tests/test_git_worktree.py:96 and tests/test_review_worktree_regression.py:80.
    • git/worktree.py imports CONST_USER_DATA_ROOT by name [code: git/worktree.py:19, :31], so the floor's patch of core.repo.CONST_USER_DATA_ROOT [code: tests/conftest.py:308] misses it.
    • The floor never sets DEVOPS_CLI_USER_DATA_ROOT either [code: git/worktree.py:30].
  • An empty .ssh/. A likely writer is _ensure_known_host, which creates ~/.ssh for known_hosts when it is called [code: git/operations.py:139-141] [inference].
  • Files from tools and libraries: .local/state/gh/device-id, .semgrep/ and .cache/semgrep_version from the real gh and Semgrep runs, which 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 removes, and the library caches .cache/python-tldextract/ and .cache/python-entrypoints/.

Outside the home:

  • The system temp directory. The run updated devops_cli_ollama_rr_<uid> [code: ai/client/network.py:94], a round-robin index shared by every xdist worker and every real CLI run of the same user [measured: its modification time moved during the run]. It also left devops-k8s-bootstrap.log [code: commands/devcontainer.py:1021] and two tmp*.out files of a test's own [code: tests/test_ci.py:980] [measured].
  • Home paths are fixed at import. Every home-derived path is computed when its module is imported [code: config/constants.py:15, :21; config/defaults.py:55-56, :170], so setting HOME in a fixture is too late.
  • Not in scope: the checkout's .data leaks belong to fix(ai): the durable-run store resolves under the data directory, so tests never write .data/durable_runs.db into the checkout #1036: the durable-run store, which the run left as .data/durable_runs.db [measured], the port-forward state file [code: k8s/port_forward_daemon.py:21] and the docs-ingest directory. Hypothesis's example database, .hypothesis/ in the checkout, is gitignored and is Hypothesis's own replay store.

Acceptance criteria

  • Home. Each pytest process (each xdist worker, or the one process under -n 0) runs with an empty home of its own under pytest's base temp. The home is set before devops-cli is first imported, so no home-derived path resolves into the developer's home. Its git config gives tests that commit an identity on example.com.
  • Per-test state. Each writer above writes under the test's tmp_path:
    • the TLS test passes --tls-dir;
    • one function resolves the user data root, for git/worktree.py and for core/repo.py [code: core/repo.py:282, :291], and it honours DEVOPS_CLI_USER_DATA_ROOT. The floor sets that variable per test and drops its patch at tests/conftest.py:308. The two worktree tests assert that their worktrees are under tmp_path;
    • the Ollama round-robin index lives under the data directory, and a test asserts that the file appears under DEVOPS_CLI_DATA_DIR and nowhere in the temp directory;
    • whatever creates .ssh/ is found and given a tmp_path location.
  • Temp directory. During a test, tempfile.gettempdir() returns a directory under its tmp_path, so code that writes the system temp directory writes there instead [code: ai/harness/compaction.py:107, :124; commands/devcontainer.py:1021, :1209].
  • Config. Now that the home is redirected, the floor's patch of config.settings.CONFIG_PATH [code: tests/conftest.py:473] is removed, and the global config layer reads the worker's empty home.
  • Session check. The workspace tripwire [code: tests/conftest.py:880-917] also fails when the run leaves anything at one of devops-cli's own locations in a worker's home, naming each path. Those locations are CONST_CONFIG_DIR, CONST_USER_DATA_ROOT, DEFAULT_SSH_KEY_DIR and DEFAULT_LOCAL_BIN_DIR, as computed in that home. A pytester test shows a test that writes under CONST_CONFIG_DIR failing the session with that path.
  • 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 two floor patches this PR removes (CONST_USER_DATA_ROOT and CONFIG_PATH).
  • (person-run) Run uv run pytest -p no:cacheprovider in full on a machine where nothing else runs devops-cli. Afterwards, the modification times under ~/.config/devops-cli and ~/.local/share/devops-cli are unchanged, and the system temp directory has no new devops-cli entry.
  • 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/bug
    scope/cli
    priority/p2-medium
    scope/ci
    on Oct 7, 2026
  2. added this to the v0.2.31 milestone on Oct 8, 2026
  3. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Placed in v0.2.31 on 2026-10-08 at the owner's request ("Populate 10 security and ci issues into each v0.2.30, v0.2.31, and v0.2.32 milestone"). This is a person's placement. Release theme: finding identity v2, URL credential masking, tests that leave no state, the Docker endpoint check and the Argo CD/OTel perimeters. Position 5 of 10 in the release's planned merge order. Its board Status is set to Ready, so the release-start fill keeps it.

  4. dan-petty-bot commented on Oct 9, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1484, merged into release/v0.2.31 as 8dfb0c1.

    Summary

    Fixes #1311.

    Tests touch no state outside their temporary directories:

    • Worker Home Isolation: Each pytest worker process runs with an isolated home under pytest basetemp before devops-cli is imported, giving tests a git identity on example.com and registering worker homes.
    • Temp Directory Isolation: isolate_tempdir autouse fixture sets tempfile.tempdir and temp environment variables (TMPDIR, TEMP, TMP) to tmp_path, ensuring code writing to system temp writes inside tmp_path.
    • User Data Root Resolution: Replaced floor patches of CONST_USER_DATA_ROOT with user_data_root() helper in core/repo.py honoring DEVOPS_CLI_USER_DATA_ROOT across core/repo.py, git/worktree.py, and tools_lock.
    • Config Isolation: Removed floor patch of CONFIG_PATH in tests/conftest.py, allowing the global config layer to read the worker's empty home.
    • Writers Remediated:
      • tests/test_k8s.py passes --tls-dir.
      • Ollama round-robin index in ai/client/network.py resolved under DEVOPS_CLI_DATA_DIR instead of system temp.
      • _ensure_known_host in git/operations.py accepts optional known_hosts path so tests write under tmp_path.
      • Worktree tests in test_git_worktree.py and test_review_worktree_regression.py assert paths stay under tmp_path.
    • Session Tripwire Auditing: The workspace tripwire verifies that worker homes leave no state at CONST_CONFIG_DIR, CONST_USER_DATA_ROOT, DEFAULT_SSH_KEY_DIR, or DEFAULT_LOCAL_BIN_DIR, and fails the session naming any leaked path.…

    14 file(s) changed.

    How it was verified

    Check runs at 7d74a88:

    • Prune PR DevContainer Images: pass
    • CodeQL: pass
    • Static Analysis: pass
    • Analyze (actions): pass
    • Analyze (python): pass
    • Tests & Coverage: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1311-tests-touch-no-state-outside-temp.md:

    • Each pytest process (each xdist worker, or single process under -n 0) runs with an empty home under pytest's base temp, configured before devops-cli is imported, with git identity on example.com.
    • Writers write under test tmp_path:
      • tests/test_k8s.py passes --tls-dir.
      • user_data_root() resolves user data root honoring DEVOPS_CLI_USER_DATA_ROOT across core/repo.py, git/worktree.py, and tools_lock.
      • isolate_user_data_root sets DEVOPS_CLI_USER_DATA_ROOT and drops the CONST_USER_DATA_ROOT patch.
      • tests/test_git_worktree.py and tests/test_review_worktree_regression.py assert worktrees are under tmp_path.
      • Ollama round-robin index lives under data directory (DEVOPS_CLI_DATA_DIR) and nowhere in temp directory; tested in tests/test_ai_client_network.py.
      • _ensure_known_host accepts an optional known_hosts path so tests write under tmp_path.
    • tempfile.gettempdir() returns a directory under tmp_path during each test via isolate_tempdir fixture.
    • Floor patch of config.settings.CONFIG_PATH in tests/conftest.py is removed, and global config layer reads the worker's empty home.
    • Workspace tripwire fails when the run leaves state at CONST_CONFIG_DIR, CONST_USER_DATA_ROOT, DEFAULT_SSH_KEY_DIR, or DEFAULT_LOCAL_BIN_DIR in a worker's home, naming each leaked path.
    • Pytester test in tests/test_isolation_tripwire.py verifies a test writing under CONST_CONFIG_DIR fails the session naming the path.
    • Person-run check: running tests leaves no new entries under developer home or system temp.
    • changelog.d/1311.md records the change; CHANGELOG.md and docs/ROADMAP.md are not edited.
    • uv run devops ci passes with 100% compliance.
  5. added a commit that references this issue on Oct 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

    priority/p2-mediumscope/ciscope/clitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions