Skip to content

test(cli): whole-tree import-cycle and interface-layer invariants #848

Closed

Description

@dan-petty

Context & Rationale

test_no_circular_imports_in_decoupled_subsystems (tests/test_architectural_invariants.py:282-347) runs a hand-written Tarjan algorithm over 4 roots. Its ast.walk (:275) counts function-local and TYPE_CHECKING imports as edges.

Findings from a stdlib probe:

  • With module-level, non-TYPE_CHECKING imports and submodule-only resolution, the tree has no cycle.
  • Counting every import gives a strongly connected component of 105 modules.
  • Naive package-plus-submodule edges report a false ai.client <-> ai.client.unified cycle (ai/client/unified.py:16).

The interface layering is prose only (ARCHITECTURE.md:11-50, AGENTS.md:377). ai/mcp/server.py:2844 already imports the private _load_local_contracts from commands/ai_ingest.py:114; that function has 3 callers.

docs/ROADMAP.md:1040 part (2) plans this. :1043 rejects import-linter and pylint R0401.

Scope & Deliverables

  • Deliverable:
    • src/devops_cli/core/import_graph.py (stdlib ast only) builds module-level, non-TYPE_CHECKING edges. from pkg import name resolves to the submodule when name is a submodule, and to the package otherwise.
    • A cycle rule built on graphlib.TopologicalSorter(graph).prepare(). The CycleError message names the cycle.
    • An interface-layer rule: no module outside devops_cli.commands.* and devops_cli.main imports devops_cli.commands, at any scope.
    • Both rules run inside the one whole-tree test that refactor(ci): one nesting metric module shared by the pre-commit hook and the scanner #773 leaves, which parses src once. Delete the Tarjan test and its helper.
    • Move _load_local_contracts below the command layer, not into models/, and update its 3 callers.
    • Failure messages give file:line, the rule and the fix. The docstring states that string-based lazy imports are invisible to the graph.
  • Acceptance criteria:
    • Both rules pass on the tree.
    • Fixtures:
      • a package __init__ doing from . import sub, where sub does from pkg import name, reports no cycle;
      • top-level mutual imports fail, naming both modules;
      • mutual imports only inside function bodies or under TYPE_CHECKING add no cycle;
      • a function-local import of devops_cli.commands.x outside commands fails with file:line.
    • No change to pyproject.toml or uv.lock.
    • In --durations, the combined whole-tree test is no slower than the Tarjan test and refactor(ci): one nesting metric module shared by the pre-commit hook and the scanner #773's remaining scan were together, recorded in the task file.
  • Key questions:
    • Does core/cli.py:136, which imports devops_cli.main, count as a violation? (proposed) No. Only the commands rule is stated.
    • What happens to the vulture step in ROADMAP.md:1040? (proposed) It is dropped, and the task file records that.
  • Constraint: Add a layer rule only when an ADR or ARCHITECTURE.md states it.

Strategic Alignment

Activity

  1. added
    type/test
    scope/cli
    priority/p2-medium
    on Oct 1, 2026
  2. added this to the v0.2.36 milestone on Oct 14, 2026
  3. dan-petty commented on Oct 14, 2026

    @dan-petty
    OwnerAuthor

    Moved to v0.2.36: v0.2.36 started with fewer than its 16 items and was topped up to 16 from Ready items, critical fixes and P0 features first. (devops roadmap reprioritize)

  4. added
    status/in-progress
    on Oct 16, 2026
  5. dan-petty commented on Oct 16, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1592, merged into release/v0.2.36 as df9efa3.

    Summary

    • Add src/devops_cli/core/import_graph.py (stdlib only: ast, graphlib, itertools, os, collections.abc, dataclasses, pathlib) building the static module-level import graph of src/devops_cli/ (skipping if TYPE_CHECKING: blocks, resolving from pkg import name to pkg.name when a submodule exists or pkg otherwise) and verifying two architectural invariants:
      • verify_acyclic_imports / import_cycle_violations: runs graphlib.TopologicalSorter(graph.edges).prepare() over the entire devops_cli.* tree, naming every cycle on failure.
      • interface_layer_violations: checks imports at all scopes (top-level and inside functions/methods) so no module outside devops_cli.commands.* and devops_cli.main imports devops_cli.commands.
    • Move load_local_contracts (_load_local_contracts) from src/devops_cli/commands/ai_ingest.py to src/devops_cli/ai/rag/library_store.py and extract the release-cut domain (cut_release, _format_release_title, _build_release_pr_command, milestone_issues_args, milestone_pull_requests_args) to src/devops_cli/release/cut.py so ai/mcp/server.py and roadmap/* no longer import devops_cli.commands.*.
    • Wire both invariants into test_the_source_tree_keeps_its_structural_invariants over the single _parse_src() pass in tests/test_architectural_invariants.py, delete test_no_circular_imports_in_decoupled_subsystems and its 4-subsystem Tarjan helpers, a…

    15 file(s) changed.

    How it was verified

    Check runs at 2b4e2f5:

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

    Acceptance Criteria of docs/agent/tasks/task-848-whole-tree-import-cycle-invariants.md:

    No Acceptance Criteria section.

  6. added a commit that references this issue on Oct 17, 2026
  7. removed
    status/in-progress
    on Oct 30, 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/clitype/test

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions