Mission Specification: Charter & Sync Sonar Remediation

Mission Branch: fix/charter-sync-sonar-remediation Created: 2026-08-10 Status: Draft Input: Clear the SonarCloud maintainability backlog for the charter and sync modules (80 open findings) via behavior-preserving refactors with no new suppressions. Full findings inventory: scratchpad charter-sync-sonar-findings.txt.

User Scenarios & Testing (mandatory)

User Story 1 - The charter and sync modules carry no avoidable Sonar maintainability debt (Priority: P2)

src/charter/ (+ src/specify_cli/charter_runtime/) and src/specify_cli/sync/ together carry 80 open Sonar findings — duplicate literals, over-complex functions, malformed suppression comments, unused parameters, too-many-parameter signatures, and one super-linear-backtracking (ReDoS-class) regex flagged BLOCKER. This story clears them: hoist repeated literals to named constants; extract tested helpers to bring over-complex functions to the ≤15 cognitive-complexity ceiling; fix or remove malformed suppression comments; drop genuinely-unused parameters; simplify the ReDoS regex while preserving its match semantics — all behavior-preserving, with no new suppressions, and each extracted helper covered by a focused test.

Why this priority: maintainability + one performance/robustness BLOCKER; no functional gap. Opportunistic module sweep following the doctrine remediation (#3232). P2 (the BLOCKER regex is the highest-value item).

Independent Test: a fresh Sonar analysis of src/charter/ and src/specify_cli/sync/ reports 0 open findings for the addressed rules (except any documented, meaningfully-reduced complexity residual); the full tests/charter/ and tests/sync/ suites stay green (no behavior change).

Acceptance Scenarios:

1. Given a repeated literal flagged S1192, When the sweep runs, Then it becomes a single named module constant referenced at every site and the module's tests stay green. 2. Given an over-complex function flagged S3776, When it is refactored, Then deterministic sub-logic is extracted into tested helpers, its cognitive complexity is ≤15 (or a documented residual), and observable behavior is identical. 3. Given the S8786 BLOCKER regex in token_budget.py, When it is simplified, Then it no longer backtracks super-linearly AND matches exactly the same inputs (proven by a characterization test).

Edge Cases

rationale; only remove it when it suppresses nothing real. Never leave a malformed/no-op suppression.

is proven byte-equivalent (or ReDoS-equivalent) against representative inputs before landing.

leave a one-line inline rationale (an inline rationale is not a suppression). All charter/sync S3776 are 16-33 (tractable); no deferral is expected.

  • A suppression genuinely needed (S7632): fix the comment's syntax and keep it with a one-line
  • A "concise regex" or ReDoS rewrite that changes match semantics is a behavior bug — every regex change
  • A complexity function that cannot reach ≤15 without harming clarity/behavior: reduce as far as clean,

Requirements (mandatory)

Functional Requirements

IDTitleUser StoryPriorityStatus
FR-001Charter ReDoS BLOCKER regexSimplify the super-linear-backtracking regex at src/charter/context_renderers/token_budget.py:308 (S8786) so it is linear-time AND matches identically (characterization test).HighOpen
FR-002Charter complexityReduce the 20 S3776 charter functions (complexity 16-29) toward ≤15 via tested helper extraction, behavior-preserving.MediumOpen
FR-003Charter dup-literalsHoist the 6 S1192 charter repeated literals to named module constants.MediumOpen
FR-004Charter suppression commentsFix or remove the 13 S7632 malformed suppression comments in charter (prefer removal when the suppression is unnecessary).MediumOpen
FR-005Charter misc smellsResolve the remaining charter smells: S1172 unused params (3), S3516 invariant-return (1), S5890 (1).LowOpen
FR-006Sync complexityReduce the 7 S3776 sync functions (complexity 16-33) toward ≤15 via tested helper extraction, behavior-preserving.MediumOpen
FR-007Sync dup-literalsHoist the 9 S1192 sync repeated literals to named module constants.MediumOpen
FR-008Sync suppression commentsFix or remove the 7 S7632 malformed suppression comments in sync.MediumOpen
FR-009Sync misc smellsResolve the remaining sync smells: S107 too-many-params (3), S1172 unused params (2), S6353 regex (1), S7503 (1), S5713 (2), S5779 (1), S8572 (2).LowOpen

Non-Functional Requirements

IDTitleRequirementCategoryPriorityStatus
NFR-001Behavior-preservingNo observable behavior change: the full tests/charter/ and tests/sync/ suites stay green; every extracted S3776 helper carries a focused test exercising its branches; every regex change is proven match-equivalent.MaintainabilityHighOpen
NFR-002No new suppressionsNo # noqa, # type: ignore, or Sonar-suppression comment is ADDED to clear a finding; findings are cleared by real fixes. Pre-existing justified suppressions may be preserved (carried, not stripped).MaintainabilityHighOpen
NFR-003ReDoS fix is realThe S8786 regex no longer exhibits super-linear backtracking (verified), not merely reformatted.ReliabilityHighOpen

Constraints

IDTitleConstraintCategoryPriorityStatus
C-001Scoped to charter + syncTouch only src/charter/, src/specify_cli/charter_runtime/, src/specify_cli/sync/, and their tests under tests/charter/ / tests/sync/. No cross-module churn.TechnicalHighOpen
C-002merge_driver S8786 out of scopeThe second S8786 finding (src/specify_cli/cli/commands/merge_driver.py:519) is outside charter/sync — noted for a future follow-up, not fixed here.TechnicalMediumOpen

Success Criteria (mandatory)

Measurable Outcomes

S7632/S1172/S3516/S5890 and S3776 at ≤15 (or documented residual).

S6353/S7503/S5713/S5779/S8572 and S3776 at ≤15 (or documented residual).

  • SC-001: A fresh Sonar analysis of src/charter/ (+ charter_runtime) reports 0 open S8786/S1192/
  • SC-002: A fresh Sonar analysis of src/specify_cli/sync/ reports 0 open S1192/S7632/S107/S1172/
  • SC-003: The token_budget.py regex is linear-time and match-equivalent (characterization test proves both).
  • SC-004: tests/charter/ and tests/sync/ stay green; no new suppressions added anywhere.