Skip to content

fix(tests): a test that writes a real config.yaml fails at the write and names itself, and every config write keeps a backup and a change record #1921

Closed

Description

@dan-petty

Problem

A person had a setting ssh.key_prefix in the local config.yaml. The setting is gone. The person reports that the main cause was tests that did not isolate the environment, so they wrote the real file. Nothing records which test or command changed the file, and nothing can restore it.

Evidence

  • config.yaml is ignored by git. Its last write was on 2026-10-07 at 16:10. It holds no key_prefix, and the copy of 2026-09-28 (.data/config copy.yaml) holds none either. The loss happened before 2026-10-22, and no history shows who wrote it.
  • The loss has a visible effect. The managed key of this project is named devops_cli-id_ed25519-20260831 (underscore), and without the setting the derived prefix is devops-cli (hyphen). This is why devops ssh list listed the keys of other projects (issue fix(ssh): the key prefix matches with a hyphen or an underscore in one place, and list, register, rotate and status never fall back to the keys of other projects #1920, fixed).
  • get_active_config_path resolves DEVOPS_CLI_CONFIG, then a project config.yaml found from the working directory, then ~/.config/devops-cli/config.yaml. A test that calls save_settings or a writing command without DEVOPS_CLI_CONFIG set writes one of the real files. A walk from a git worktree stops at its .git file, so a worktree does not find the config of the main checkout.
  • Eight call sites run save_settings in src/devops_cli/commands/: ai_runs.py:171, k8s/networking.py:977, k8s/cluster_context.py:66, ai.py:484, telemetry.py:194, ai_gateway.py:219 and config.py:196 and :295. It rewrites the whole file from the in-memory model and keeps no backup.
  • tests/support/workspace_tripwire.py fails a test session that changed config.yaml, but it compares bytes at the start and the end of the session. It detects after the damage and names no test.
  • An audit of the current suite on 2026-10-22 (tip 80e07bb1): the whole suite ran with a sys.addaudithook that logs every write to a file named config.yaml outside the pytest tmp directories, with HOME set to a throwaway directory. The suite passed and the hook logged no write. The hook fired on a probe write outside the suite. So no test of the current suite writes a real-looking config file, and the earlier escape is fixed or is not reachable by this run.

Remedy

  1. Make the escape impossible and named at the moment it happens. Extend the hermetic floor (it already refuses network sockets and programs) with a stdlib audit hook, loaded for the test process and its subprocesses the way the socket block is, that raises when anything opens for writing, renames onto or copies onto a file named config.yaml or the user-level config path outside the pytest tmp directories, and names the test (PYTEST_CURRENT_TEST) and the stack. Test the hook with a probe test, and show that a test which calls save_settings without isolation fails with the test name. Do not add test detection to product code.
  2. Before each write, copy the file to config.yaml.bak-<YYYYMMDD-HHMMSS> and keep the newest ten copies, so a real write can be undone. The write is save_settings (one function).
  3. Append one line to <data_dir>/audit/config-writes.jsonl for each write: the time, the command line and the dotted names of the keys that changed. Never write the value of a secret key.
  4. Optional: write only the changed keys and keep comments and unknown keys (ruamel.yaml round trip), and decide for each of the eight call sites whether the command must persist a detected value. These are not needed to stop the loss. Check ruamel.yaml for maintenance before use and write the cost.
  5. Optional second layer: a person adds permission entries that deny the writing commands to agents in .claude/settings.json.

Proposed design

Problem statement

Tests write to real config.yaml files. This causes data loss and makes it hard to track changes.

Acceptance criteria

  • Criterion 1: A test that writes to config.yaml fails with a clear error message naming the test.
    Verification: Run a test that calls save_settings without isolation. The test should fail and show PYTEST_CURRENT_TEST in the error.
  • Criterion 2: Before each write to config.yaml, a backup file is created with timestamp.
    Verification: Run a test that saves settings. Check that config.yaml.bak-YYYYMMDD-HHMMSS exists in the directory.
  • Criterion 3: Each config write is logged to audit/config-writes.jsonl with time, command line and changed keys.
    Verification: Run a test that modifies config. Verify that audit/config-writes.jsonl contains one new line with the expected information.

Key questions

  • What is the function that saves settings to config files?: The function save_settings in src/devops_cli/config/settings.py. [sources: src/devops_cli/commands/config.py:196]
  • Where are the call sites that use save_settings?: Eight call sites in src/devops_cli/commands/: ai_runs.py:171, k8s/networking.py:977, k8s/cluster_context.py:66, ai.py:484, telemetry.py:194, ai_gateway.py:219 and config.py:196 and :295. [sources: src/devops_cli/commands/ai_runs.py:171, src/devops_cli/commands/k8s/networking.py:977, src/devops_cli/commands/k8s/cluster_context.py:66, src/devops_cli/commands/ai.py:484, src/devops_cli/commands/telemetry.py:194, src/devops_cli/commands/ai_gateway.py:219, src/devops_cli/commands/config.py:196, src/devops_cli/commands/config.py:295]
  • How does the system currently detect config changes?: The workspace tripwire in tests/support/workspace_tripwire.py compares bytes at start and end of session. It detects damage after it happens and does not name the test. [sources: tests/support/workspace_tripwire.py]

Fit

Needs splitting into 3 items:

  1. Extend hermetic floor with audit hook that raises on config.yaml writes
  2. Create backup files before each write to config.yaml
  3. Log config writes to audit/config-writes.jsonl

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    on Oct 22, 2026
  2. dan-petty commented on Oct 22, 2026

    @dan-petty
    OwnerAuthor

    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.

    • Type: type/bug. The candidate fixes a problem with config file handling and data loss.
    • Priority: P1-High. The issue causes data loss and affects core functionality of the tool.
    • Value: High. The change prevents data loss and improves user experience for config management.
    • Effort: High. The fix requires changes to file handling, backup logic, audit logging, and multiple call sites.
  3. changed the title [-]fix(config): a config write keeps a backup, records what changed and edits only the changed keys, and eight commands stop saving values nobody asked for[/-] [+]fix(tests): a test that writes a real config.yaml fails at the write and names itself, and every config write keeps a backup and a change record[/+] on Oct 22, 2026
  4. dan-petty commented on Oct 23, 2026

    @dan-petty
    OwnerAuthor

    Proposal needs splitting into 3 items at 72b8a3c (see proposed design in issue body).

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

    source/agenttype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions