Skip to content

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

Closed as not planned

Description

@dan-petty

Problem

Nothing stops new tests from adding the coupling that #1307 retires. Counted at 36cfbbb by a static AST pass over every .py under tests/: test modules, conftest.py and the helper modules [measured: 2026-10-07; two independent passes gave 2,777 and 2,771 internal patch sites].

  • Internal patch sites: about 2,770.
    • Nearly all of them, in about 260 files, are patch calls (patch, patch.object, patch.dict, patch.multiple, mocker.patch, monkeypatch.setattr/setitem/delattr) whose target resolves to an object defined in devops-cli.
    • Eight or nine, in three or four files, are direct assignments to devops-cli attributes, such as the console singletons [code: tests/conftest.py:268-269], QdrantClient.__init__ [code: tests/conftest.py:519] and ALLOW_MODEL_REQUESTS [code: tests/test_ai_agent_capabilities.py:660, :663].
    • The floor's own patches are included, for example _own_source_dir [code: tests/conftest.py:321], _github_token [code: :357] and CONFIG_PATH [code: :473]. So are the shared fake's patches of private LLMClient methods [code: tests/llm_stream_fakes.py:120, :123].
    • The census, matching names in test modules only, counted 2,770.
  • Private-name imports: 937 in 210 files. About 90 more private names are read as attributes of imported devops-cli modules or classes, in some 35 files.
  • Test accommodations in src: 9 sites.
    • unittest.mock imports [code: security/base.py:17; ai/review/pipeline.py:711];
    • PYTEST_CURRENT_TEST [code: commands/ci.py:253, :257; core/process.py:93];
    • mock attributes probed by name: assert_called [code: ai/mcp/server.py:88] and _mock_return_value [code: sandbox/host.py:93];
    • patch detection by comparing __module__ with "subprocess" [code: core/process.py:545; sandbox/host.py:91].
  • A precedent exists. test_c901_suppressions_stay_under_the_ceiling [code: tests/test_architectural_invariants.py:1327] holds the C901 markers under one ceiling. When it fails, it names files, not lines.

Acceptance criteria

What is counted. AST only; no test module is imported.

  • Internal patch sites in every .py under tests/. A site is a patch call whose target resolves statically to an object defined in devops-cli, or an assignment to or deletion of an attribute of a devops-cli module or class. These are not counted:
    • a stdlib or third-party object reached through a devops-cli module, such as patch("devops_cli.core.process.subprocess.run"), which is an edge patch;
    • a patch of a local object;
    • patch.dict(os.environ, ...).
  • Private-name imports in every .py under tests/: a name with one leading underscore imported from devops-cli.
  • Test accommodations in src/devops_cli, of four kinds:
    • an import of unittest or unittest.mock;
    • the string PYTEST_CURRENT_TEST;
    • an attribute, or an attribute-name string, from unittest.mock.NonCallableMock that starts with assert_ or _mock_, taken from the class rather than a hand-kept list;
    • a comparison of a __module__ value, read as an attribute or through getattr(..., "__module__"), with a name in sys.stdlib_module_names.

Ceilings

  • Each ratchet's ceiling is today's count, stored per file. The PR records the totals its check measures at its base; the counts above give about 2,770, 937 and 9 at 36cfbbb.
  • A file over its ceiling fails. The message lists every counted site in that file as path:line with its target, and says to remove the new site rather than raise the ceiling.
  • A file under its ceiling fails until the ceiling is lowered to its count, so a removed site cannot come back later. The message names the command that lowers the file's rows to the measured counts, so a PR outside test(cli): tests double only process edges and mirror the source tree #1307 that removes a site knows what to run. That command lowers and deletes rows but never raises or adds one.
  • A file without an entry has a ceiling of 0.

Tests of the checks. Fixture trees under tmp_path, each test under 1 s.

  • One fixture per counted form is counted, with its path:line: a string target, patch.object through an imported module, a direct assignment, and a private import.
  • An edge patch through a devops-cli module, patch.object on a local object, patch.dict(os.environ, ...) and a public import are not counted.
  • Each accommodation kind is counted in a fixture src/ tree, the getattr(..., "__module__") form included. A __module__ read that is not compared with a stdlib name, as in ai/library/introspector.py:74, is not counted.
  • The three ratchets parse each file once per run and add at most 3 s to the summed test time, as recorded by --durations.
  • The layout ratchet is not added here; it lands with the move (test(cli): every test file sits in the directory mirroring the package it tests or in tests/invariants/, and the root conftest holds only the hermetic floor #1313).
  • uv run devops ci passes.

Key questions

Strategic alignment

Proposed design

Problem statement

Tests add internal patch sites, private imports and test accommodations that break test isolation. A ratchet prevents new ones from being added.

Acceptance criteria

  • Criterion 1: Internal patch sites in test modules are counted by static AST analysis.
    Verification: Run AST check on test files. Count patch calls to devops-cli objects and direct assignments to devops-cli attributes.
  • Criterion 2: Private-name imports from devops-cli are counted in test modules.
    Verification: Run AST check on test files. Count names with leading underscore imported from devops-cli.
  • Criterion 3: Test accommodations in src code are counted by static AST analysis.
    Verification: Run AST check on source files. Count unittest.mock imports, PYTEST_CURRENT_TEST strings, mock attribute checks and module comparisons.
  • Criterion 4: Each ratchet's ceiling is set to today's count for that file.
    Verification: Check test results show file-specific ceilings. Command records current counts at PR base commit.
  • Criterion 5: A file over its ceiling fails with list of counted sites.
    Verification: Run tests on a file with new patch site. Fail and show path:line for each counted site.
  • Criterion 6: A file under its ceiling fails until ceiling is lowered to count.
    Verification: Run tests on a file that removes a site. Show command to lower the file's rows to measured counts.

Key questions

  • Which ratchets?: Patches aimed at devops-cli internals, imports of private names from devops-cli, and invariant that bans unittest.mock, PYTEST_CURRENT_TEST and mock-attribute checks in src. (proposed)
  • Do the floor's own patches count?: Yes. Conftest's _github_token, _own_source_dir and _CONSOLE count toward the ratchet. (proposed)
  • Where do the checks live?: Invariant tests live in tests/invariants/, in files named by subject, and the source tree's file is test_source_tree.py. (proposed)
  • Per-file or total ceilings?: Per file, in one table next to the test, sorted by path. (proposed)
  • How are rows lowered?: By one command beside the table that recounts and rewrites it. It writes only rows whose count fell, deletes rows that reach 0, and exits non-zero without writing when any count rose. (proposed)
  • Private names read as module._name?: Count them in the private-import ratchet as well; that adds about 90 reads. (proposed)
  • Structural accommodations: _K8sModule forwarding, _LAZY_OBJECT_MAPPING, # noqa: F401 patch-target re-imports, the settings cache keyed on CONFIG_PATH?: Not counted, because no single syntactic form covers them. Each package item deletes its own, and they have no reason to come back once the patches they serve are gone. (proposed)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

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

    @dan-petty
    OwnerAuthor

    Linked: #1347. Both add per-file ceiling tables with a lowering command, so they share one mechanism. 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. It is a guardrail preventing regression of the coupling #1307 retires, but it does not block a user-facing feature or fix a defect.
    • Value: High. Without the ratchets, new tests can silently re-introduce the internal coupling that the #1307 work stream is eliminating.
    • Effort: Medium. Requires a static AST pass over all test files, per-file ceiling storage, failure messages naming each site, and a lowering command, but no runtime behaviour change.
  4. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Priority: P1-High. The change addresses test isolation and prevents regressions in the test suite.
    • Value: High. This change improves test reliability and maintainability for the project.
    • Effort: Medium. Implementing ratchets requires significant development effort in both the tooling and test checks.
    • Possible duplicate: test(cli): migrate 49 tests to public interfaces #2167, which may ask for the same change. The model names ratchets that hold internal patches, private imports and test accommodations in src at today's counts and name each site that would raise one as the change that both ask for. Both items ask for ratchets to track internal patches, private imports and test accommodations in src at today's counts. A person closes one of the two if that is so.
  5. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at 48c78ca (see proposed design in issue body).

  6. added
    priority/p1-high
    status/ready
    effort/medium
    value/high
    and removed
    priority/p2-medium
    on Oct 28, 2026
  7. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Closed as not planned. The item conflicts with a more recent project direction. No code was written.

    This item builds three new ratchets, each with a per-file ceiling table, a lowering command and an AST counter. Its first comment says it shares one mechanism with #1347, the swallowed-failures ratchet. Three owner decisions that are newer than this item go against that mechanism:

    1. docs(agents): a matching table lives beside its one owner, and moves to config/ only when a second package imports it #1016, 2026-10-22: "No gate checks the placement. The project adds no new ratchet." The reason given is the cost of merge conflicts in shared tables.
    2. The rigid-rules review, 2026-10-22, decision Q4 (recorded on feat(cli): blind exception handling policy #423): "Bespoke ceilings, ratchets and anti-escape meta-tests stand in for a tool's own configuration ... The remedy is that the tool owns its configuration and its baseline." For swallowed failures it chose a gate with no baseline file, and it deleted the test(cli): swallowed failures may not grow: blind excepts, except-pass, except-continue and unread check=False are held at their ceilings #1347 visitor, its ceiling and its baseline.
    3. feat(cli): blind exception handling policy #423, 2026-10-24: the owner cancelled that gate as well: "policy is brittle with excessive maintenance friction across shallow checkouts and concurrent rebases."

    The per-file tables of this item are edited by every child of #1307 that removes a site (#1310 to #1335, #2167). That is the concurrent-rebase friction that decision 3 names. docs/TESTING.md still lists these ratchets as a target, because the item that was to reword it (#423) was cancelled.

    Measured on origin/main (2026-10-28), the counts are still large, so a ban at zero is not possible for two of the three checks:

    • Patches of devops-cli objects in tests/: about 2,770 sites at 36cfbbb (the item's count). The children of test(cli): tests double only process edges and mirror the source tree #1307 remove them package by package.
    • Private-name imports from devops-cli in tests/: 937 at 36cfbbb. Ruff has PLC2701 (import-private-name), but in Ruff 0.16.8 it is a preview rule.
    • Test accommodations in src/devops_cli: 9 sites, the same total as the item, at different lines:
      • unittest.mock imports: security/base.py:18, ai/review/pipeline.py:657;
      • PYTEST_CURRENT_TEST: commands/ci.py:418, :422, core/process.py:109;
      • a mock attribute probed by name: sandbox/host.py:101 (_mock_return_value);
      • __module__ compared with "subprocess": core/process.py:670, :1080, sandbox/host.py:99.

    The third check fits the newer direction: with only 9 sites, the sites can be removed and a tool can ban the forms at zero, with no baseline. Ruff's banned-api (TID251) is already configured in pyproject.toml, and the repository already runs ast-grep rules with rule tests. A new item carries that part.

    #1336 ("the test ratchets become bans") names this item as its input. A person decides its scope.

  8. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    The part that fits the newer direction is now #2512: the 9 test accommodations in src/ are removed, and Ruff banned-api and ast-grep rules ban them at zero, with no baseline.

  9. removed
    status/ready
    on Oct 30, 2026
  10. added a commit that references this issue on Nov 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

    effort/mediumpriority/p1-highscope/clitype/testvalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions