Skip to content

ADR-010: SonarCloud for Continuous Code Quality

Status

superseded in part by ADR-064

ADR-064 supersedes the statement that C901 is the only Ruff rule. Enrolled code also runs the ADR-064 rules. The SonarCloud gate and the global C901 gate stay in force.

Date

2026-03-07

Applicability

This record keeps the chronology of quality-gate decisions. Dated sections describe their own revision, including obsolete hook ordering and advisory states. CI verification describes the current workflow. ADR-064 defines current staged layer and rule enrollment.

Context

APTL had no automated code quality analysis. Code quality was assessed through manual review only—first during development, then formally in the team review documented in notes/team-review-synthesis.md. That review found 30 issues (17 major, 13 minor), including any-typed MCP args, duplicated code, dead config fields, and a process-global SSL disable.

As the codebase grew to include both Python (src/aptl/, 587+ tests) and TypeScript (mcp/, vitest tests), a continuous quality gate was needed to catch regressions before they accumulate.

Decision

Integrate SonarCloud via GitHub Actions for continuous code quality analysis across both language stacks.

Configuration

sonar-project.properties: - Sources: src (Python) and mcp (TypeScript) - Tests: tests (Python) and mcp/aptl-mcp-common/tests (TypeScript) - Exclusions: node_modules, __pycache__, dist, .venv, build, containers, and mcp/**/tests/** (to prevent test files from being double-indexed as both source and test)

Coverage pipeline (.github/workflows/sonarcloud.yml): 1. Python: pytest --cov generates coverage.xml 2. TypeScript: vitest --coverage in mcp/aptl-mcp-common/ generates lcov.info 3. SonarCloud scan reads both coverage reports

Key Implementation Details

  • Test file separation: MCP test files under mcp/**/tests/** are added to sonar.exclusions (excluded from source analysis) while simultaneously listed in sonar.tests (recognized as test sources). Without this, SonarCloud reported can't be indexed twice errors (v4.6.8).
  • Coverage bootstrapping: The initial CI workflow ran SonarCloud without running tests first, resulting in 0% coverage. Fixed by adding pytest and vitest steps before the scan (v4.5.0, v4.6.8).
  • Scanner migration: Migrated from deprecated SonarSource/sonarcloud-github-action to SonarSource/sonarqube-scan-action@v7 (v4.7.0).
  • CI action versions: Upgraded to Node.js 24-compatible versions (actions/checkout@v6, actions/setup-python@v6, actions/setup-node@v6) in v4.7.0.

Consequences

Positive

  • Continuous quality gate: Every PR and push to main gets automated quality analysis
  • Dual-language coverage: Both Python and TypeScript codebases have measured test coverage
  • Historical tracking: SonarCloud maintains quality metrics over time, showing trends

Negative

  • External dependency: SonarCloud is a third-party service. Outages block quality gate checks.
  • CI pipeline complexity: The workflow must install Python, Node.js, run two test suites, and then scan—adding ~3-5 minutes to every CI run.

Risks

  • SonarCloud free tier limits may be reached as the codebase grows (currently well within limits for open-source projects)
  • Coverage thresholds are not yet enforced as quality gates—currently informational only

Update (2026-06-21): SonarCloud gate is required again (issue #502)

The SonarCloud job in .github/workflows/checks.yml is a required merge gate. It uses -Dsonar.qualitygate.wait=true and tools/sonar/assert_no_new_issues.py. An open new-code issue fails CI. Advisory vulnerability scanners (Trivy, OSV-scanner, dependency-audit) stay non-blocking per ADR-026.

Branch protection on main and dev uses strict required status checks (documented in .github/branch-protection-baseline.json): pre-commit, docs, Python/MCP/web test jobs, and SonarCloud. Admin bypass is retained (enforce_admins: false).

The hard in-repo per-function complexity gates (ruff C901, ESLint complexity) remain unchanged and continue to gate through the required Pre-commit hooks job.

Update (2026-05-10): SonarCloud gate stayed advisory; hard complexity gate added

The former sonarcloud.yml moved into .github/workflows/checks.yml. The 2026-05-10 policy used advisory analysis with continue-on-error and no quality-gate wait. Its reason was the 80% new-code coverage condition on infrastructure-only commits. Issue #502 reverts that posture now that the repo also carries the new-code issue gate script.

The original hard Python gate used only Ruff C901 across src/. Its McCabe threshold was 15. This is cyclomatic complexity, a different metric from SonarCloud cognitive complexity. That decision enabled only the complexity rule. Later rule additions required an explicit decision, supplied for enrolled code by ADR-064.

Update (ADR-064, 2026-10-31): ADR-064 adds Ruff rules for enrolled code. ruff check src/ with only C901 still runs on all code.

TypeScript complexity extension guardrails

Issue #286 extends the same hard gate to the TypeScript surfaces. Treat that as the same cross-cutting quality policy as ruff-complexity, not as a package-local lint cleanup. The TypeScript gate must stay narrow: per-function complexity only, threshold 15, no formatting/import/style/recommended-rule overhaul bundled with it.

The canonical enforcement point is the required Pre-commit hooks check in .github/workflows/checks.yml, matching the Python gate. If implementation chooses a separate required CI job instead, keep the pre-commit hook for local parity. In either form, the gate must run before slower vitest hooks and must not be skipped by the existing CI SKIP list.

Scope is mcp/*/src/**/*.ts and web/src/**/*.{ts,svelte}. Generated output, tests, node_modules, package build/ directories, and web/.svelte-kit/ remain out of scope. web/src contains Svelte components, so a TS-only glob would miss functions in <script lang="ts"> blocks and does not satisfy the contract.

Keep one source of truth for the threshold, target globs, and legacy exemptions. Reuse the existing flat ESLint/TypeScript-parser pattern if ESLint is selected, but do not copy the full mcp-red / mcp-reverse style lint rules into every package. Biome is acceptable only if it covers the same mcp and Svelte inputs and can express explicit legacy exemptions. Existing exceptions needed the narrowest supported exemption and a measured score in the backlog. New code could not enter exempted files.

TypeScript complexity gate (realized)

Issue #286 ships the gate as ESLint's core complexity rule at ["error", { max: 15 }], the same cyclomatic metric as ruff C901. The single source of truth is the root eslint.config.js: it holds the threshold, the target globs (mcp/*/src/**/*.ts and web/src/**/*.{ts,svelte}), and the out-of-scope ignores (build/, dist/, node_modules/, web/.svelte-kit/, tests). It currently carries no per-file exemptions (see the cleared backlog below). Only the complexity rule is enabled (the config loads the TypeScript and Svelte parsers but none of their rule plugins), so it is a complexity guard, not a style linter, and it does not cascade into the per-package mcp-red / mcp-reverse configs. Svelte components are first-class: functions inside <script lang="ts"> are parsed by svelte-eslint-parser and gated like any .ts function.

Enforcement is the ts-complexity hook in .pre-commit-config.yaml, a language: node hook with pinned additional_dependencies, self-contained like ruff-complexity's pinned ruff. The original hook followed ruff-complexity and preceded slower Vitest hooks. It used the same local and required CI hook configuration, without another required job. CommonJS resolved pinned parsers through pre-commit's NODE_PATH. noInlineConfig prevented inline rule exceptions. --quiet suppressed ineffective-directive warnings without hiding errors.

TypeScript complexity backlog (cleared)

Empty. Issue #286 added the gate and split three functions above the threshold. At that revision, every scoped function passed 15 without complexity: 'off' exemptions. The historical offenders (all CC 18) were harvestSession (mcp/aptl-mcp-common/src/captures.ts), predefined_query (mcp/aptl-mcp-common/src/tools/api-handlers.ts), and buildBlockSequence (web/src/lib/workbench.ts). If a future change must temporarily exempt a file, add a whole-file complexity: 'off' override (never a per-function inline // eslint-disable, which edits the function and drags its pre-existing complexity into SonarCloud's new-code analysis) and record the offender here with its measured score.

Python complexity backlog (cleared)

Empty. Issue #286 split all eight functions that were over threshold into helpers, so pyproject.toml carries no [tool.ruff.lint.per-file-ignores] entries and every function under src/ is gated at max-complexity 15. The historical offenders (CC = ruff's McCabe cyclomatic complexity) were:

Function File CC (before)
compile_runtime_model src/aptl/core/runtime/compiler.py 45
_verify_objectives src/aptl/core/sdl/validator.py 43
_verify_workflows src/aptl/core/sdl/validator.py 37
_validate_manifest src/aptl/core/runtime/planner.py 31
_verify_infrastructure src/aptl/core/sdl/validator.py 31
_verify_agents src/aptl/core/sdl/validator.py 25
_collect_resources src/aptl/core/runtime/planner.py 18
_expand_shorthands src/aptl/core/sdl/parser.py 17

If a future change must temporarily exempt a file, re-add the per-file-ignore table (whole-file, never a per-def # noqa: C901) and record the offender here with its measured score.

(SonarCloud's cognitive-complexity rule flags a wider, partly different set— many more functions in validator.py plus deeply nested ones in snapshot.py / collectors.py / env.py / detection.py / cli/runs.py, and a CC-66 function in lab.py on the older main analysis; the two metrics are complementary and both worth driving to zero.)

Known wart: src/aptl/core/lab.py:63 has a malformed # noqa: delayed import for mocking (it predates this gate)—ruff check prints a one-line warning for it but still passes. The original record deferred the correction because it would touch lab.py. The dated update below records its later removal.

Update (2026-10-31): src/aptl/core/lab.py no longer has this comment.

Update (2026-09-20): Platform dependency gate (issue #969)

The earlier #502 statement that dependency-audit remains non-blocking is superseded for dev pull requests. The existing Dependency vulnerability scan job is now required and blocks on shipped platform dependencies, while the mixed target Trivy and OSV scanners remain advisory. ADR-026's dated amendment defines this boundary. The SonarCloud quality gate and other existing required contexts are unchanged.