Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ repos:
# Pure-stdlib AST walk over the staged files: no devops_cli import, because loading
# the package costs about five seconds before a single file is read, and a
# commit-time gate has to cost less than the commit. Blocks on nesting depth, which
# the tree currently satisfies; complexity is reported without blocking, since 240
# functions breach that cap today and nothing enforces it.
# the tree currently satisfies. Complexity is not checked here: it is Ruff's C901,
# run by the ruff-check hook above.
- id: structural-invariants
name: AST structural invariants (nesting depth)
entry: uv run python3 -m devops_cli.security.structural_invariants
Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This document provides foundational context, architectural principles, and opera
- **High Reliability & Quality First**: Build robust, resilient workstation automation and developer tooling with defensive error handling, bounded timeouts, and zero tolerance for flaky tests.
- **Poetic Conciseness, Expressive Integration & Zero Boilerplate**:
- The codebase must read as an expressive, poetically concise integration of tools, libraries, docs, AI, and automation rather than a sprawl of procedural loops or boilerplate.
- **Strict Complexity & Nesting Caps**: Strictly enforce cyclomatic complexity $\le 10$ and maximum nesting depth $\le 5$ (< 6 indentation levels) project-wide across all functions, closures, and blocks. Continuous compliance is validated by `devops scan complexity` and automated architectural invariant gates ([`tests/test_architectural_invariants.py`](tests/test_architectural_invariants.py)).
- **Strict Complexity & Nesting Caps**: Cyclomatic complexity $\le 10$ is Ruff `C901` (standard McCabe), run by `uv run devops ci` and the pre-commit `ruff-check` hook. Functions over the cap when it was turned on carry `# noqa: C901`. RUF100 removes a marker once its function is fixed, and `test_c901_suppressions_stay_under_the_ceiling` ([`tests/test_architectural_invariants.py`](tests/test_architectural_invariants.py)) keeps the count from growing. A function over the cap is decomposed, never suppressed: `test_the_complexity_cap_has_no_escape` rejects every other committed way past it, including Ruff's own `# ruff: ignore[C901]` and `# ruff: disable[C901]` comments and ignore files that hide a source. Nesting depth $\le 5$ (< 6 indentation levels) is enforced by the `structural-invariants` pre-commit hook and `test_no_excessive_nesting_in_src`. `devops scan complexity` reports a different, in-house count and does not gate.
- **Structural Tuple Equality Consolidation in Test Suites (Mitigating Assertion Sprawl)**:
- Under Python AST semantics, every `assert expr` statement compiles to `if not (expr): raise AssertionError`, adding $+1$ to McCabe cyclomatic complexity $M$.
- When asserting multiple object attributes or properties in test suites, developers and AI agents **MUST CONSOLIDATE LINEAR ASSERTIONS INTO STRUCTURAL TUPLE EQUALITY CHECKS** (`assert (actual_a, actual_b, actual_c) == (expected_a, expected_b, expected_c)`) or collection predicates (`assert all(...)`). This prevents purely linear test code from breaching the $M \le 10$ complexity cap while fully preserving Pytest element-level diff diagnostics.
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DevOps CLI follows an uncompromising quality-first, test-driven engineering cult

- **Test-First Implementation (TDD)**: All features, fixes, and refactorings **must** have tests written first. Tests serve as the executable documentation of public interfaces, edge cases, and architectural boundaries.
- **Strict Complexity & Nesting Caps**:
- Cyclomatic complexity $\le 10$ across all functions and closures (`tests/test_architectural_invariants.py`).
- Cyclomatic complexity $\le 10$ is Ruff `C901` (standard McCabe), run by `uv run devops ci` and the pre-commit `ruff-check` hook. Functions over the cap when it was turned on carry `# noqa: C901`, and `tests/test_architectural_invariants.py` keeps their count from growing; `devops scan complexity` reports a different, in-house count and does not gate.
- Maximum nesting depth $\le 5$ (< 6 indentation levels) project-wide.
- Decompose multi-step procedures into dedicated single-responsibility helpers and pure predicates.
- **Strongly Typed Domain Exceptions**:
Expand Down
22 changes: 22 additions & 0 deletions changelog.d/586.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### Added
- **Ruff Enforces the Complexity Cap of 10 (`pyproject.toml`, `uv run devops ci`, the `ruff-check` pre-commit hook)**:
- `pyproject.toml` selects `C901` and sets `[tool.ruff.lint.mccabe] max-complexity = 10`, so a function over the cap (standard McCabe) fails `ruff check` in `devops ci`, in the `ruff-check` pre-commit hook and in GitHub CI. `AGENTS.md` called the cap strictly enforced, but nothing checked it: `C901` was not selected, and the `structural-invariants` hook only printed a note (#586).
- The 66 functions over the cap when it was turned on, 65 in `src/` and 1 in `tests/`, carry a `# noqa: C901` marker. `ruff check --select C901 --add-noqa` wrote the markers in one run, and no function was decomposed in this change. `RUF100` reports a marker once its function is back under the cap, and the default `devops ci --fix` removes it. `PGH004` bans the blanket `noqa` that would hide `C901` without naming it (#586).
- With `RUF100` selected, nine `noqa` codes that did nothing are gone. Five name rules the project does not select: `S310` three times in `ai/gateway_bench.py`, `S603` in `ai/gateway_tune.py` and `S102` in `tests/test_ai_gateway_tune.py`. Four sit where Ruff raises nothing: `F401` twice in `tests/test_exceptions.py`, and `N802` in `tests/test_sandbox_metrics.py` and `tests/test_sandbox_probe.py`. The `# nosec` comments that Bandit reads stay, and the reason "the script under test" stays as a plain comment (#586).
- `test_c901_suppressions_stay_under_the_ceiling` counts the `C901` markers in `src/` and `tests/`, in every spelling Ruff honours, including a form feed or a no-break space where a space would go. It fails when the count exceeds `DEFAULT_C901_SUPPRESSION_CEILING` (66), names the files that carry markers, and says to decompose the function rather than raise the ceiling. A change that removes markers lowers the ceiling in the same change (#586).
- `test_the_complexity_cap_has_no_escape` fails when `[tool.ruff]` or `[tool.ruff.lint]` holds a key outside its allowlist. The allowlists keep out Ruff's deprecated top-level `per-file-ignores` and `extend-ignore`, as well as `include`, `extend`, `extend-exclude` and `lint.exclude` (#586).
- The same test fails when `exclude` is not `["repos"]`, when `C901`, `RUF100` or `PGH004` is not enforced or a per-file ignore waives it, or when `max-complexity` is not 10. A rule counts as enforced only when `select` names it by its own code, because Ruff reads `C9` as `C90`, so `C90` selected with `C9` ignored turns `C901` off (#586).
- It also fails when a file under `src/` or `tests/` has a file-level exemption, when a `ruff.toml`, `.ruff.toml` or `pyproject.toml` sits under those directories, or when a `ruff.toml` or `.ruff.toml` sits beside `pyproject.toml` (#586).
- It fails when a file under `src/` or `tests/` carries one of Ruff's own suppression comments naming `C901`: a `# ruff: ignore[C901]`, a `# ruff: disable[C901]` that holds until its `enable` or the end of the file, or a `# ruff: file-ignore[C901]`. One such comment hides any number of functions and leaves no marker to count, so these comments are banned rather than counted (#586).
- It fails when an `.ignore` or `.gitignore` sits under `src/` or `tests/`, when an `.ignore` sits beside `pyproject.toml`, or when the project's `.gitignore` matches a source file there. Ruff skips the files an ignore file lists. Ignore files outside the checkout differ per machine and never reach GitHub CI's clean checkout. No test runs a Ruff process (#586).

### Changed
- **The `structural-invariants` Hook Checks Nesting Only (`.pre-commit-config.yaml`, `devops_cli.security.structural_invariants`)**:
- The hook no longer counts complexity or prints its "Not blocking" complexity note, because complexity is now Ruff's `C901`. `breaches` and `check_structural_invariants` return only the nesting breaches and take no `max_complexity`, `measure` returns a function's nesting depth, and `MAX_COMPLEXITY` is gone. Nesting is measured as before: the old and new measures agree on all 14,749 functions in `src/` and `tests/` (#586).
- `measure` walks a function with its own stack instead of recursing, so a long expression such as a 3,000-term sum is measured instead of stopping the hook with a `RecursionError` traceback. The released hook already failed that way at 3,000 terms. A parser stack overflow is reported as a file the hook could not parse (#586).
- Two tests are removed. `test_complexity_is_reported_without_blocking` started a hook subprocess, and `test_filesystem_get_tools_complexity` guarded a function that `C901` now covers (#586).

### Fixed
- **The Docs Name What Enforces the Complexity Cap (`AGENTS.md`, `CONTRIBUTING.md`, `docs/SDLC.md`, `docs/ROUTINE_TASKS.md`)**:
- These four files said `devops scan complexity` and the architectural invariant tests enforced the cap of 10. They now say that Ruff `C901` enforces it through `uv run devops ci` and the pre-commit hook, and that a test keeps the number of functions carrying markers from growing. `AGENTS.md` adds that a function over the cap is decomposed, never suppressed, and that `test_the_complexity_cap_has_no_escape` rejects Ruff's own `ruff:` suppression comments and ignore files that hide a source (#586).
- They also say that the `structural-invariants` hook and `test_no_excessive_nesting_in_src` enforce nesting, and that `devops scan complexity` reports a different, in-house count and does not gate (#586).
2 changes: 1 addition & 1 deletion docs/ROUTINE_TASKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The following matrix categorizes all project routine tasks by operational layer,
| **Security & Audits** | Dependency Security Audit | Weekly / Pre-Release | `uv run devops ci audit` (`uv audit`) | Scans installed packages for known vulnerabilities | 0 known vulnerabilities |
| **Security & Audits** | Static Security Scan (SAST) | Weekly / Pre-Release | `uv run devops ci security` (`bandit`) | Static security scan for code vulnerabilities | 0 high/medium issues identified |
| **Security & Audits** | Kubernetes Manifest Scans | Per Manifest Change | `devops scan [kubelinter\|popeye\|pluto\|trivy]` | Validates manifests against K8s security best practices | Zero deprecated APIs or misconfigurations |
| **Security & Audits** | Codebase Deduplication & Invariant Audit | Weekly / Pre-PR | `devops scan complexity` && `pytest tests/test_architectural_invariants.py` | Enforces complexity <= 10, nesting <= 5, and shared helper adoption | Zero invariant violations |
| **Security & Audits** | Codebase Deduplication & Invariant Audit | Weekly / Pre-PR | `uv run ruff check .` && `pytest tests/test_architectural_invariants.py` | Ruff `C901` enforces complexity <= 10; the tests enforce nesting <= 5, the `C901` marker ceiling and shared helper adoption | Zero invariant violations |
| **Observability & Diagnostics** | Command Output, Logs, Metrics & Trace Audit | Continuous / On Execution | `devops telemetry status` / `devops telemetry logfire-status` | Comprehensive review of command outputs, application logs (`.data/logs/`), metrics, and tracing spans for latency, misconfigurations, and warnings | Nominal latencies, zero unhandled errors/warnings |
| **Workspace & Sync** | DevContainer Lifecycle Hooks | Daily / On Start | `devops devcontainer run-lifecycle --post-start` | Cross-platform container initialization tasks | All lifecycle tasks complete successfully |
| **Workspace & Sync** | Multi-Repo Synchronization | Daily / On Demand | `devops repos sync` / `devops repos status` | Pulls upstream changes across all managed repos | All repositories up to date |
Expand Down
2 changes: 1 addition & 1 deletion docs/SDLC.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ graph TD
1. **Cyclomatic Complexity $\le 10$ & Nesting Depth $\le 5$**:
- Every function, method, and code block must maintain cyclomatic complexity $\le 10$ and nesting depth $\le 5$ (< 6 indentation levels).
- Decompose multi-step branching into pure predicate helpers, table-driven dispatch dictionaries, or functional pipelines (`functools`, `itertools`, `pathlib`).
- Validated continuously by `devops scan complexity` and `tests/test_architectural_invariants.py`.
- Complexity is Ruff `C901` (standard McCabe), run by `uv run devops ci` and the pre-commit `ruff-check` hook; functions over the cap when it was turned on carry `# noqa: C901`, and `tests/test_architectural_invariants.py` keeps their count from growing. Nesting is enforced by the `structural-invariants` pre-commit hook and `test_no_excessive_nesting_in_src`. `devops scan complexity` reports a different, in-house count and does not gate.
2. **Standardized Domain Exception Taxonomy**:
- Raising bare Python built-ins (`ValueError`, `RuntimeError`, `TypeError`, `Exception`) in domain logic is strictly prohibited.
- All domain errors must inherit from `DevOpsCLIError` under `src/devops_cli/exceptions/`, supplying an exit code, machine-readable code (`CONST_ERROR_CODE_*`), and contextual details.
Expand Down
Loading
Loading