Skip to content

[AAASM-4757] ♻️ (examples): Clear 5 Sonar smells in generate_example_metadata#322

Merged
Chisanan232 merged 5 commits into
masterfrom
v0.1.0/AAASM-4757/fix/sonar_smells
Jul 17, 2026
Merged

[AAASM-4757] ♻️ (examples): Clear 5 Sonar smells in generate_example_metadata#322
Chisanan232 merged 5 commits into
masterfrom
v0.1.0/AAASM-4757/fix/sonar_smells

Conversation

@Chisanan232

Copy link
Copy Markdown
Contributor

What changed

Clears all 5 SonarCloud maintainability smells in scripts/generate_example_metadata.py, taking the examples repo's Maintainability rating debt on this file to zero. Every change is strictly behaviour-preserving — the generator's output stays byte-identical (verified against the CI metadata drift check), and the two regex simplifications were proven to match the same set as before.

  • python:S8786 (x2, ReDoS / super-linear regex)_GO_PIN_RE tail and _VERSION_TOKEN_RE pre-release alternation rewritten as prefix-free forms that remove the flagged overlap. _GO_PIN_RE: (?:\s+//.*)?\s* -> \s+//[^\n]*|\s* (comment .* no longer competes with trailing \s*). _VERSION_TOKEN_RE: (?:rc|beta|alpha|b|a) -> (?:rc|b(?:eta)?|a(?:lpha)?) (folds the b/beta and a/alpha prefix shadows).
  • python:S1172 (unused parameter) — dropped the unused versions argument from _audit_py_operator and updated its sole caller audit(). It is an internal helper, not a shared interface, so removal (not underscore-prefix) is the correct fix.
  • python:S3776 (cognitive complexity, x2)process_prereq_rows (16 -> under limit) by extracting _align_prereq_readme; _audit_prose (45 -> under limit) by splitting into flat per-line helpers (_audit_prose_label/_backtick/_install/_line/_file). Emit order and message text are unchanged.

Related ticket

Jira: https://lightning-dust-mite.atlassian.net/browse/AAASM-4757 (Epic AAASM-4754)

How to verify

Run locally from the repo root:

  • python3 -m py_compile scripts/generate_example_metadata.py — passes.
  • python scripts/generate_example_metadata.py then git diff — no diff (byte-identical output; the CI drift check stays green).
  • python scripts/generate_example_metadata.py --check — every audited surface matches the SoT.
  • python -m unittest scripts.test_generate_example_metadata — 39/39 pass.
  • Regex equivalence: old-vs-new fuzzed over ~1.4M inputs (structured version/go.mod corpora + random) comparing match/no-match, captured groups, spans and findall — IDENTICAL for both regexes.

Checklist

  • PR title follows [AAASM-XXXX] <GitEmoji> (<scope>): <summary>
  • No secrets, API keys, or .env files committed
  • Example sub-projects include their own README.md with prerequisites and run instructions
  • SDK/runtime version dependencies are documented or pinned

🤖 Generated with Claude Code

Chisanan232 and others added 5 commits July 17, 2026 13:03
The old ``(?:\s+//.*)?\s*`` tail let the comment's ``.*`` and the trailing
``\s*`` both consume trailing whitespace — a super-linear overlap static
analysis flags as a ReDoS risk (python:S8786). Rewrite the tail as the
disjoint alternation ``\s+//[^\n]*|\s*``; proven to match the same set and
capture identical groups across ~0.4M fuzz inputs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TdxntF32Wi278LGD22dVS4
``(?:rc|beta|alpha|b|a)`` has ``b``/``a`` shadowing ``beta``/``alpha`` — an
alternation overlap static analysis flags as super-linear (python:S8786).
Fold the pairs into ``b(?:eta)?`` / ``a(?:lpha)?`` so no branch is a prefix
of another. Proven equivalent (same token, span, findall) across ~0.5M fuzz
inputs plus a structured version-string corpus.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TdxntF32Wi278LGD22dVS4
The operator audit only inspects the pin operator via _PY_PIN_AUDIT_RE and
never reads the SoT versions (python:S1172). Remove the dead parameter and
update its sole caller, audit(). Not a shared interface — the only caller is
internal.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TdxntF32Wi278LGD22dVS4
process_prereq_rows nested two rewrite loops and a touched flag inside its
per-README loop, pushing cognitive complexity to 16 (python:S3776, limit 15).
Move the per-README alignment into _align_prereq_readme, leaving the outer
function a flat collect loop. Behaviour identical.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TdxntF32Wi278LGD22dVS4
_audit_prose nested three checks inside a per-line loop inside a per-file
loop, reaching cognitive complexity 45 (python:S3776, limit 15). Extract the
label, backtick-row and install-hint checks into flat helpers, a per-line
combiner (_audit_prose_line) and a per-file walker (_audit_prose_file); the
outer function is now a flat collect loop. Emit order and message text are
unchanged, so a generator-produced tree still audits identically.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TdxntF32Wi278LGD22dVS4
@sonarqubecloud

Copy link
Copy Markdown

@Chisanan232

Copy link
Copy Markdown
Contributor Author

Claude Code — PR review

Verdict: ready to approve & merge (pending required Pioneer approval).

Check Result
CI ✅ green — CodeQL, SonarCloud, metadata drift, run, all Analyze pass
Scope vs AAASM-4757 ✅ exactly the 5 targeted smells in one file, no stray changes
Side effects ✅ none — byte-identical generator output proven
Front-End N/A (no FE change)

Scope coverage — all 5 Sonar smells in scripts/generate_example_metadata.py cleared:

  • S8786 (ReDoS) L250 — _GO_PIN_RE tail rewritten prefix-free
  • S8786 (ReDoS) L455 — _VERSION_TOKEN_RE alternation folds b/beta, a/alpha prefix shadows
  • S1172 L1020 — removed unused versions param from _audit_py_operator + its sole caller (internal helper, one caller — removal correct vs underscore-prefix)
  • S3776 L865 — process_prereq_rows 16→under limit (_align_prereq_readme)
  • S3776 L1041 — _audit_prose 45→under limit (flat helpers, unchanged emit order/text)

Side-effect analysis — the strongest possible proof: running the generator after the change leaves git diff empty (excluding the script) → the emitted metadata is unchanged, so the metadata drift CI check stays green (and it did). Both ReDoS regex rewrites were fuzzed old-vs-new over ~1.4M structured+random inputs comparing match/no-match, named groups, spans, and findallidentical. Removed param was genuinely unused.

Validation: 39/39 unit tests pass; --check audit clean; py_compile OK.

@Chisanan232
Chisanan232 merged commit b9652bd into master Jul 17, 2026
10 checks passed
@Chisanan232
Chisanan232 deleted the v0.1.0/AAASM-4757/fix/sonar_smells branch July 17, 2026 05:43
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