Skip to content

feat(cli): enforce the complexity cap with ruff C901 and an inline baseline that only shrinks #586

Closed

Description

@dan-petty

Context & Rationale

AGENTS.md:14 says cyclomatic complexity ≤ 10 is strictly enforced project-wide, and that devops scan complexity and the architectural invariant tests validate it. At ef21785 nothing enforces it:

  • pyproject.toml:93 does not select C901, and there is no [tool.ruff.lint.mccabe] section.
  • devops ci has no complexity step (commands/ci.py never mentions complexity). devops scan complexity only reports, and exits 0 on a breach (commands/scan.py:406-480).
  • The structural-invariants pre-commit hook blocks only on nesting. It prints complexity breaches as a note (security/structural_invariants.py:112-117), and tests/test_structural_invariant_hook.py:45-56 pins that it does not block.
  • test_no_excessive_nesting_in_src scans with max_complexity=100 (tests/test_architectural_invariants.py:152).

Reviewers and agents trust the claim, so stating it as enforced is worse than stating it as a target. The same claim names the wrong enforcer in CONTRIBUTING.md:13, docs/SDLC.md:129 and docs/ROUTINE_TASKS.md:55.

There are two counts. Owner decision D1 picks ruff's.

  • ruff C901 (standard McCabe): 73 functions over 10 in 44 src files, and 3 in tests (tests/conftest.py:58, tests/test_ai_client_providers_and_embeddings.py:657, tests/test_architectural_invariants.py:282). Measured with ruff 0.16.8: ruff check --no-cache --isolated --select C901 --config 'lint.mccabe.max-complexity = 10' src (or tests).
  • The in-house counter (security/complexity.py:106-123, mirrored in structural_invariants.py:15-39): 235 over 10 in src. It adds +1 for every assert, every with and every extra boolean operand, which McCabe does not. The old figure of 240 was an earlier in-house count.
  • The two sets are not nested. Ruff flags 5 src functions that the in-house scanner passes, for example get_tools at ai/harness/shell.py:335 (13) and the closure run_code at ai/harness/os_access.py:234 (22).

Other facts the builder needs:

  • _ruff_enforces (tests/test_architectural_invariants.py:445-470, used at :486-505) reads only lint.select and lint.ignore. An agent could still raise max-complexity, add a per-file-ignores entry, add a nested ruff.toml, or use a blanket or file-level noqa. Today there are no per-file-ignores, no file-level noqa and no nested ruff config. PGH004 (blanket noqa) reports 0 hits.

  • Ruff 0.16.8 still honours the deprecated top-level lint settings in [tool.ruff]. per-file-ignores = {"pkg/*" = ["C901"]} or extend-ignore = ["C901"] there silences C901 with only a deprecation warning. include and extend change which files or which settings apply. Today [tool.ruff] holds only target-version, line-length, exclude and lint, and [tool.ruff.lint] holds only select and ignore.

  • With RUF100 selected, 9 noqa codes are stale today:

    • ai/gateway_bench.py:69, :97 and :100 (S310);
    • ai/gateway_tune.py:432 (S603);
    • tests/test_ai_gateway_tune.py:338 (S102);
    • tests/test_exceptions.py:172-173 (F401);
    • tests/test_sandbox_metrics.py:229 and tests/test_sandbox_probe.py:133 (N802).

    Bandit reads the # nosec comments on those lines, not noqa.

  • --select on the command line replaces the configured select. So ruff check --select RUF100 reports every code for a rule it did not select as unused: 31 codes today, and 98 once the C901 markers exist (the 76 new markers plus F401, N802 and UP036 codes the project needs). With the project's select in force, RUF100 reports exactly the 9 above.

  • uv run devops ci defaults to --fix. It runs ruff check --fix . before the lint check (commands/ci.py:349-355, :709), so a stale marker is removed in the working tree and the gate still passes. --check turns the fix pass off. GitHub CI runs plain ruff check . (.github/workflows/ci.yml:51).

  • Under ruff, FileSystem.get_tools (ai/harness/filesystem.py:320) scores 2. C901 therefore covers what test_filesystem_get_tools_complexity (tests/test_architectural_invariants.py:160-167) guards.

  • test_complexity_is_reported_without_blocking starts a subprocess that imports devops_cli.security. Its call took 12.4 s when run alone at ef21785, and a bare hook run on one file takes 6.5-7.4 s.

  • ai/instruction_generator.py has no counterpart to AGENTS.md:14. Its only complexity text (:244-249) is about nesting, so it needs no change.

  • Adding C901,RUF100,PGH004 to ruff check . did not change lint time: 0.60-0.88 s with or without them.

  • Counting noqa comments with tokenize over the 867 files in src and tests took 1.2-8.0 s across runs. A regex over the file text took 0.02-0.24 s.

Scope & Deliverables

  • Deliverable:
    • Config. pyproject.toml selects C901, RUF100 and PGH004, and sets [tool.ruff.lint.mccabe] max-complexity = 10.

    • Baseline. On the release tip after the last rebase, with the config change in place:

      1. Run uv run ruff check --select C901 --add-noqa . once.
      2. Remove the 9 stale codes with uv run ruff check --fix --fixable RUF100 .. Do not pass --select RUF100: it replaces the configured select and strips all 98 codes, the new markers included.
      3. Keep the # nosec comments. Keep the reason on tests/test_ai_gateway_tune.py:338 ("the script under test") as a plain comment.

      No function is decomposed in this PR.

    • Meta-test in tests/test_architectural_invariants.py. One pure helper takes the parsed pyproject.toml and the source roots, and returns every escape it finds:

      • [tool.ruff] has a key outside target-version, line-length, exclude and lint. This one check covers the deprecated top-level lint settings, include, extend, extend-exclude and any key ruff adds later.
      • [tool.ruff.lint] has a key outside select, extend-select, ignore, extend-ignore, per-file-ignores, extend-per-file-ignores and mccabe. This covers lint.exclude.
      • exclude is not ["repos"].
      • one of C901, RUF100 or PGH004 is not enforced. This reuses _ruff_enforces over select + extend-select and ignore + extend-ignore. The three codes live in CONST_COMPLEXITY_LINT_RULES in config/constants.py, and the two key allowlists sit beside them.
      • mccabe has a key other than max-complexity, or max-complexity is missing or differs from DEFAULT_MAX_COMPLEXITY.
      • a per-file-ignores or extend-per-file-ignores entry covers C901 (ALL, C, C9, C90 or C901).
      • a file under src/ or tests/ has a file-level # ruff: noqa or # flake8: noqa.
      • a ruff.toml, .ruff.toml or pyproject.toml exists under src/ or tests/.
    • Ceiling test. It counts noqa comments that name C901 under src/ and tests/. It counts codes, so # noqa: C901, N802 counts once, and it uses a regex over the file text. It fails when the count exceeds DEFAULT_C901_SUPPRESSION_CEILING in config/defaults.py, next to DEFAULT_MAX_COMPLEXITY. The ceiling equals the count at merge. The failure message names the files and says to decompose the function, not to raise the ceiling.

    • Hook. The hook becomes nesting-only. Delete the complexity accumulation in _measure, MAX_COMPLEXITY, the max_complexity parameters, over_complex and the note in security/structural_invariants.py. Delete test_complexity_is_reported_without_blocking and test_filesystem_get_tools_complexity. Rewrite the complexity clause of the hook comment at .pre-commit-config.yaml:47-51: complexity is ruff C901 in the ruff-check hook. The comment's import claim belongs to refactor(ci): one nesting metric module shared by the pre-commit hook and the scanner #773.

    • Docs. AGENTS.md:14 names the real enforcers. Proposed text: "Cyclomatic complexity ≤ 10 is ruff C901 (standard McCabe), run by uv run devops ci and the pre-commit ruff hook. Functions over the cap when it was turned on carry # noqa: C901. RUF100 removes a marker once its function is fixed, and a test keeps the count from growing. Nesting depth ≤ 5 is enforced by the structural-invariants hook and test_no_excessive_nesting_in_src. devops scan complexity reports a different, in-house count and does not gate." Make the same correction in CONTRIBUTING.md:13, docs/SDLC.md:129 and docs/ROUTINE_TASKS.md:55.

    • Roadmap and task file. The edits depend on the state of docs/ROADMAP.md when the PR starts:

      Rewrite docs/agent/tasks/task-586-cyclomatic-complexity-debt-reduction.md so it matches what shipped. Drop the stale v0.2.24 milestone line and the duplicated deliverable lines. Record the counts and the durations from the criteria below.

  • Acceptance criteria:
    • uv run devops ci --check --no-cache passes on the committed PR head. --check skips the ruff check --fix pass, so this proves that no stale or blanket noqa remains in the commit. GitHub CI's plain ruff check . must also pass.
    • The baseline touched no noqa code the project needs: git diff -U0 <base> -- src tests | grep -c '^-.*# noqa' prints 9, the lines listed in the Context.
    • The meta-test passes on the real pyproject.toml. A parametrized test feeds the helper in-memory config dicts and tmp_path files, one per escape in the Deliverable, and asserts that each one is reported. The cases include a top-level [tool.ruff] per-file-ignores covering C901, a top-level extend-ignore = ["C901"], include, extend and lint.exclude. No test runs the ruff binary.
    • The ceiling test passes at merge. Three numbers are equal at merge, and the task file records all three: DEFAULT_C901_SUPPRESSION_CEILING, the counting helper's result over src and tests, and the count printed by uv run ruff check --select C901 --ignore-noqa --statistics --exit-zero ..
    • A tmp_path fixture over a ceiling makes the counting helper report the excess and name the file. The fixture uses both forms that --add-noqa writes: def f(x): # noqa: C901 and a merged # noqa: C901, N802. The new tests build that text at run time so that their own source does not match the counting regex.
    • The builder runs this and records the output in the task file:
      p=src/devops_cli/_c901_probe.py
      { echo 'def f(x):'; for i in $(seq 10); do printf '    if x == %d:\n        return %d\n' $i $i; done; echo '    return None'; } > $p
      uv run ruff check $p                         # C901 `f` is too complex (11 > 10), exit 1
      uv run pre-commit run ruff-check --files $p  # Failed
      sed -i '1s/$/  # noqa: C901/' $p && sed -i '/x == 10/,+1d' $p
      uv run ruff check $p                         # RUF100 unused noqa, exit 1
      uv run ruff check --fix $p; grep -c noqa $p  # 0
      rm $p
      
    • rg -n -i complexity src/devops_cli/security/structural_invariants.py returns nothing. The four remaining tests in tests/test_structural_invariant_hook.py pass.
    • rg -n C901 AGENTS.md CONTRIBUTING.md docs/SDLC.md docs/ROUTINE_TASKS.md shows each of the four corrected lines. rg -n 'nothing enforces it|240 functions' .pre-commit-config.yaml src tests returns nothing. git diff shows no change to AGENTS.md:15-17 or .github/pull_request_template.md.
    • uv run ruff check . wall time changes by less than 1 s, measured before and after. The task file records the --durations of each new test, which together stay under 1 s, and the test step's --durations difference, which should be negative because one hook subprocess (6.5-12.4 s measured) comes out.
  • Key questions:
  • Constraint:

Strategic Alignment

Owner decisions (2026-10-01)

From the agentic quality assessment (https://claude.ai/artifact/K7S7AkKJymFydhjwexigiN), decided by the owner. These replace any matching proposed answer above.

Activity

  1. added this to the v0.2.23 milestone on Sep 25, 2026
  2. added
    type/feature
    scope/cli
    priority/p1-high
    on Sep 25, 2026
  3. modified the milestones: v0.2.23, v0.2.24 on Sep 28, 2026
  4. modified the milestones: v0.2.23, v0.2.25 on Oct 1, 2026
  5. dan-petty commented on Oct 1, 2026

    @dan-petty
    OwnerAuthor

    Retargeted from the shipped v0.2.23 to v0.2.25. It is still valid (checked 2026-10-01), and a P1 left open in a shipped release keeps precedence over P1 work committed later.

  6. changed the title [-]feat(cli): cyclomatic complexity debt reduction[/-] [+]feat(cli): enforce the complexity cap with ruff C901 and an inline baseline that only shrinks[/+] on Oct 1, 2026
  7. dan-petty commented on Oct 3, 2026

    @dan-petty
    OwnerAuthor

    Closed by #1078, merged into release/v0.2.25.

    GitHub only acts on closing keywords when a pull request merges into the default branch, so this was closed by devops gh issues close-merged.

  8. added a commit that references this issue on Oct 3, 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/p1-highscope/clitype/feature

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions