Skip to content

chore(sonar): Tier 1 cleanup — 31 code smells across 5 rules#16

Merged
aksOps merged 1 commit into
mainfrom
chore/sonar-tier-1-cleanup
May 15, 2026
Merged

chore(sonar): Tier 1 cleanup — 31 code smells across 5 rules#16
aksOps merged 1 commit into
mainfrom
chore/sonar-tier-1-cleanup

Conversation

@aksOps

@aksOps aksOps commented May 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Mechanical hygiene pass on Sonar's lower-effort code-smell rules. Targets ~32% of the 97 open smells in a single PR with no behavior change.

What's removed

Rule Count Type Approach
python:S1192 4 CRITICAL — Literal duplicates Extract module-level constants
python:S2772 5 MINOR — Unneeded pass Remove (bundler auto-fixes orphaned blocks; one block restructured to keep the bundler happy)
python:S5713 7 MINOR — Redundant exception subclass Drop subclass from except tuple
python:S8409 6 MINOR — Redundant FastAPI response_model= Drop the kwarg (return-type annotation suffices)
python:S7503 9 MINOR — Async without await (false positive) Expand sonar.issue.ignore.multicriteria
Total 31

Constants extracted (S1192)

File Constant Replaces
api.py _SSE_MEDIA_TYPE "text/event-stream" ×5
api.py _SESSION_NOT_FOUND_DETAIL "session not found" ×4
orchestrator.py _AGENT_FAILURE_MARKER "agent failed:" ×3
storage/models.py _ACTIVE_ROW_SQL "deleted_at IS NULL" ×4

Bundler interaction (heads-up for review)

S2772's removal exposed a constraint in the bundler's import-stripping pass. scripts/build_single_file.py:_ORPHANED_TYPE_CHECKING_RE injects a pass body when an if TYPE_CHECKING: block becomes empty after intra-import stripping. Two cases broke that auto-fix:

  1. policy.py had if TYPE_CHECKING: # pragma: no cover ... — the trailing comment prevented the regex from matching. Trimmed the comment (TYPE_CHECKING blocks aren't executed at runtime, so the pragma is redundant).
  2. orchestrator.py had explanatory comments INSIDE the if TYPE_CHECKING: block — the regex doesn't see the body as empty when it contains comment lines (Python treats comments as no-ops, but the regex is structural). Moved the comments above the if line.

The bundle tests (tests/test_build_*.py, tests/test_bundle_*.py) caught both cases pre-commit.

Test plan

  • uv run ruff check src/ tests/ — passed
  • uv run pyright src/runtime — 0 errors / 0 warnings
  • uv run pytest -x — 1,310 passed / 8 skipped
  • uv run pytest --cov=src/runtime --cov-fail-under=85 -x — 88.90%
  • uv run python scripts/build_single_file.pydist/* regenerated and committed
  • Bundle staleness gate (HARD-08) — passes (dist regenerated)
  • CI green on PR
  • SonarCloud code_smells drops from 97 toward ~66

Out of scope

  • S3776 cognitive complexity refactors (Tier 3 — multi-day work)
  • S8415 HTTPException doc declarations (Tier 2)
  • S1172 unused params (Tier 2)
  • S3358 nested ternary (Tier 2)

🤖 Generated with Claude Code

Mechanical hygiene pass on Sonar's lower-effort code-smell rules.
Targeted: S1192 (literal duplicates), S2772 (unneeded pass), S5713
(redundant exception classes), S8409 (redundant FastAPI response_model
kwarg), S7503 (async-without-await false positives in framework code).

Changes per rule:

  S1192 — extract module-level constants:
    * api.py:                _SSE_MEDIA_TYPE, _SESSION_NOT_FOUND_DETAIL
                             (replaces 5 + 4 literal duplicates)
    * orchestrator.py:       _AGENT_FAILURE_MARKER (replaces 3)
    * storage/models.py:     _ACTIVE_ROW_SQL (replaces 4)

  S2772 — remove redundant ``pass`` after type-checking-only imports:
    * orchestrator.py, triggers/base.py, triggers/transports/schedule.py,
      triggers/transports/webhook.py — pass removed; the bundler's
      ``_ORPHANED_TYPE_CHECKING_RE`` rewrite injects pass when imports
      get stripped
    * orchestrator.py: also moved the explanatory comment OUT of the
      ``if TYPE_CHECKING:`` block so the bundler regex sees the body
      as empty after stripping (comments inside the block prevent the
      regex from matching, which would break the dist bundle)
    * policy.py: pass removed; trailing ``# pragma: no cover`` comment
      also dropped from the ``if`` line so the bundler regex matches

  S5713 — drop redundant exception subclasses from ``except`` tuples:
    * agents/turn_output.py + graph.py: ``(json.JSONDecodeError, ValueError)``
      -> ``ValueError`` (JSONDecodeError is a ValueError subclass)
    * api.py x4: ``(FileNotFoundError, ValueError, KeyError, LookupError)``
      -> ``(FileNotFoundError, ValueError, LookupError)`` (KeyError is
      a LookupError subclass)
    * triggers/transports/webhook.py: ``(ValueError, TypeError, ValidationError)``
      -> ``(ValueError, TypeError)`` (pydantic ValidationError is a
      ValueError subclass)

  S8409 — remove FastAPI ``response_model=`` kwargs already implied by
  the route handler's return-type annotation:
    * api.py x5 + api_dedup.py x1

  S7503 — expand the suppression list (sonar-project.properties) to
  cover the remaining 9 ``async def`` sites whose async signature is
  required by the surrounding framework contract:
    * learning/scheduler.py + tools/approval_watchdog.py — APScheduler
    * llm.py — langchain BaseChatModel.ainvoke
    * agents/supervisor.py — LangGraph node
    * api.py — FastAPI handler / Depends callable
    * service.py — service.submit_async() awaitable contract
    * triggers/auth.py — FastAPI Depends bearer dependency

Verification:
  ruff check src/ tests/                         passed
  pyright src/runtime                            0 errors / 0 warnings
  pytest -x                                      1310 passed / 8 skipped
  pytest --cov=src/runtime --cov-fail-under=85   88.90%
  build_single_file.py                           dist/* regenerated

Projected SonarCloud after merge:
  code_smells   97 -> ~66 (32% drop)
@sonarqubecloud

Copy link
Copy Markdown

@aksOps aksOps merged commit 2355202 into main May 15, 2026
8 checks passed
@aksOps aksOps deleted the chore/sonar-tier-1-cleanup branch May 16, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant