From f186ee11ea136e5200b5e02809bce29fe0f6ad0a Mon Sep 17 00:00:00 2001 From: Flavio Oliva Date: Mon, 23 Nov 2026 23:06:10 +0000 Subject: [PATCH 1/4] chore: make a Sonar suppression the exception: refactor first, a stated reason, listed in the pull request Signed-off-by: Flavio Oliva --- .github/PULL_REQUEST_TEMPLATE.md | 1 + AGENTS.md | 7 +++++++ tests/test_code_shape.py | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index b8b707119..f61f6ead3 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -30,3 +30,4 @@ All four gates must pass before requesting review: - [ ] Every non-merge commit carries `Signed-off-by: Name ` matching the commit author (`git commit -s`). - [ ] No AI attribution: no model attribution trailers, co-author trailers, or machine generation lines in commits or the PR description. - [ ] Changes adhere to the repository working rules in `AGENTS.md`. +- [ ] Each new `NOSONAR` is listed below with the refactor that was tried and why it was rejected (`AGENTS.md`, architecture rule 7), or there is none. diff --git a/AGENTS.md b/AGENTS.md index 6e5b71282..dce18a0bf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -138,6 +138,13 @@ to loosen. Change a rule only in that test and here, in the same PR, with the re 6. **Ratchets only go down** (`tests/test_code_shape.py`, `scripts/ci/sonar_ratchet.py`): no module under `src/` over 1000 lines; ruff C901 counts per file pinned in `tests/complexity_baseline.json`; open code-scan issues on `main` equal to `sonar-baseline.txt`. A change that fixes one lowers the pinned number in the same commit; nothing is ever raised. +7. **A Sonar suppression is the exception; the refactor comes first.** An issue is suppressed (`# NOSONAR - `, + `// NOSONAR - `) only when the refactor it asks for would change behaviour or break a contract (an ASGI + coroutine, a snapshot copy another thread needs, a pattern whose rewrite widens what it matches) or would touch a + safety path or an overhead budget without the evidence those need. The reason names why the refactor was rejected; + `tests/test_code_shape.py` fails a suppression without one. Each pull request that adds one lists it, with the + refactor that was tried, in its body. Issues are never accepted or marked false positive in the SonarCloud UI: a + suppression lives in the code, where review sees it. ### Writing tests that prove something diff --git a/tests/test_code_shape.py b/tests/test_code_shape.py index d728c863d..4c69e43dc 100644 --- a/tests/test_code_shape.py +++ b/tests/test_code_shape.py @@ -3,11 +3,13 @@ 1. No module under ``src/`` passes ``MAX_LINES``. 2. Complexity: ruff C901 (McCabe > 10) findings per file are pinned in ``tests/complexity_baseline.json``. A file may not gain one, and a file that loses one must lower its entry in the same change, so the count can only fall. +3. Every Sonar suppression comment states its reason: ``NOSONAR - ``. """ from __future__ import annotations import json +import re import subprocess import sys from collections import Counter @@ -18,6 +20,10 @@ ROOT = Path(__file__).resolve().parents[1] MAX_LINES = 1000 BASELINE = ROOT / "tests" / "complexity_baseline.json" +SUPPRESSION = "NO" + "SONAR" # split, so this file holds no suppression of its own +EXPLAINED = re.compile(SUPPRESSION + r" - \S") +SCANNED = ("src/sharpit", "tests", "scripts", "ui/src") +SOURCE_SUFFIXES = {".py", ".ts", ".js", ".svelte"} def oversized(root: Path = ROOT / "src", limit: int = MAX_LINES) -> list[str]: @@ -51,6 +57,17 @@ def ratchet_violations(found: Counter[str], pinned: dict[str, int]) -> list[str] return problems +def unexplained_suppressions(root: Path = ROOT, dirs: tuple[str, ...] = SCANNED) -> list[str]: + return [ + f"{path.relative_to(root).as_posix()}:{n}: {SUPPRESSION} needs ' - '" + for top in dirs + for path in sorted((root / top).rglob("*")) + if path.suffix in SOURCE_SUFFIXES and path.is_file() + for n, line in enumerate(path.read_text(encoding="utf-8", errors="replace").splitlines(), 1) + if SUPPRESSION in line and not EXPLAINED.search(line) + ] + + def test_no_module_is_oversized(): assert oversized() == [] @@ -59,6 +76,10 @@ def test_complexity_only_goes_down(): assert ratchet_violations(complexity_counts(), json.loads(BASELINE.read_text(encoding="utf-8"))) == [] +def test_every_suppression_states_its_reason(): + assert unexplained_suppressions() == [] + + def test_the_ratchets_catch_a_violation(tmp_path: Path) -> None: """Each ratchet fails on planted input, so a green run means the limits hold, not that nothing was checked.""" (tmp_path / "big.py").write_text("x = 1\n" * 3, encoding="utf-8") @@ -72,3 +93,8 @@ def test_the_ratchets_catch_a_violation(tmp_path: Path) -> None: "c.py", ] assert "lower its entry" in ratchet_violations(Counter({"a.py": 1, "b.py": 1}), pinned)[0] + + (tmp_path / "src").mkdir() + (tmp_path / "src" / "ok.py").write_text(f"x = 1 # {SUPPRESSION} - a reason\n", encoding="utf-8") + (tmp_path / "src" / "bare.ts").write_text(f"let x = 1; // {SUPPRESSION}\n", encoding="utf-8") + assert unexplained_suppressions(tmp_path, ("src",)) == [f"src/bare.ts:1: {SUPPRESSION} needs ' - '"] From 123f47fedb9aa481c7b6efa9144f56af89b03aab Mon Sep 17 00:00:00 2001 From: Flavio Oliva Date: Mon, 23 Nov 2026 23:19:13 +0000 Subject: [PATCH 2/4] docs: name the code-scan service as the repo's agent docs do Signed-off-by: Flavio Oliva --- AGENTS.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index dce18a0bf..2b2d98c22 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -138,12 +138,12 @@ to loosen. Change a rule only in that test and here, in the same PR, with the re 6. **Ratchets only go down** (`tests/test_code_shape.py`, `scripts/ci/sonar_ratchet.py`): no module under `src/` over 1000 lines; ruff C901 counts per file pinned in `tests/complexity_baseline.json`; open code-scan issues on `main` equal to `sonar-baseline.txt`. A change that fixes one lowers the pinned number in the same commit; nothing is ever raised. -7. **A Sonar suppression is the exception; the refactor comes first.** An issue is suppressed (`# NOSONAR - `, - `// NOSONAR - `) only when the refactor it asks for would change behaviour or break a contract (an ASGI +7. **A code-scan suppression is the exception; the refactor comes first.** An issue is suppressed + (`# NOSONAR - `, `// NOSONAR - `) only when the refactor it asks for would change behaviour or break a contract (an ASGI coroutine, a snapshot copy another thread needs, a pattern whose rewrite widens what it matches) or would touch a safety path or an overhead budget without the evidence those need. The reason names why the refactor was rejected; `tests/test_code_shape.py` fails a suppression without one. Each pull request that adds one lists it, with the - refactor that was tried, in its body. Issues are never accepted or marked false positive in the SonarCloud UI: a + refactor that was tried, in its body. Issues are never accepted or marked false positive in the code-scan service: a suppression lives in the code, where review sees it. ### Writing tests that prove something From 4c98b3e93f1ba73d67f66c3d7f5f6cc0e3d0412b Mon Sep 17 00:00:00 2001 From: Flavio Oliva Date: Mon, 23 Nov 2026 23:19:38 +0000 Subject: [PATCH 3/4] docs: rewrap architecture rule 7 Signed-off-by: Flavio Oliva --- AGENTS.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 2b2d98c22..948c7c1f0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -139,9 +139,9 @@ to loosen. Change a rule only in that test and here, in the same PR, with the re 1000 lines; ruff C901 counts per file pinned in `tests/complexity_baseline.json`; open code-scan issues on `main` equal to `sonar-baseline.txt`. A change that fixes one lowers the pinned number in the same commit; nothing is ever raised. 7. **A code-scan suppression is the exception; the refactor comes first.** An issue is suppressed - (`# NOSONAR - `, `// NOSONAR - `) only when the refactor it asks for would change behaviour or break a contract (an ASGI - coroutine, a snapshot copy another thread needs, a pattern whose rewrite widens what it matches) or would touch a - safety path or an overhead budget without the evidence those need. The reason names why the refactor was rejected; + (`# NOSONAR - `, `// NOSONAR - `) only when the refactor it asks for would change behaviour or + break a contract (an ASGI coroutine, a snapshot copy another thread needs, a pattern whose rewrite widens what it + matches) or would touch a safety path or an overhead budget without the evidence those need. The reason names why the refactor was rejected; `tests/test_code_shape.py` fails a suppression without one. Each pull request that adds one lists it, with the refactor that was tried, in its body. Issues are never accepted or marked false positive in the code-scan service: a suppression lives in the code, where review sees it. From 21b95510eb7901f47c95b1fd150eb37aceeab3a3 Mon Sep 17 00:00:00 2001 From: Flavio Oliva Date: Tue, 24 Nov 2026 01:42:15 +0000 Subject: [PATCH 4/4] chore: lower the Sonar baseline to 5, main's open count after #313 Signed-off-by: Flavio Oliva --- sonar-baseline.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-baseline.txt b/sonar-baseline.txt index 45a4fb75d..7ed6ff82d 100644 --- a/sonar-baseline.txt +++ b/sonar-baseline.txt @@ -1 +1 @@ -8 +5