Skip to content

Reduce cognitive complexity to <= 15 and de-duplicate web UI export - #196

Merged
kevmoo merged 3 commits into
mainfrom
cognitive-complexity-1.0
Oct 8, 2026
Merged

kevmoo merged 3 commits into
mainfrom
cognitive-complexity-1.0

Conversation

@kevmoo

@kevmoo kevmoo commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner
  • Bring all 11 functions that scored above cognitive complexity 15 down to 15 or below. No behavior changes intended.
    • graph_renderer.dart: _updateOver 38 -> 13, _renderWithWorker 29 -> 10, _updateBody 26 -> 7
    • executable.dart: run 29 -> 8
    • terminate.dart: waitForTerminate 26 -> 14
    • ui_manager.dart: UIManager constructor 25 -> 10, _exportPng 23 -> 6, _injectStyles 19 -> 15, _exportAction 17 -> 8
    • update_order.dart: computeUpdateOrder 16 -> 14
    • dot.dart: _writeDot (34) removed; logic now lives in toDot plus _nodeProperties / _edgeProperties
  • UI export: dot, mermaid and svg share one copy/download path (_exportText), driven by label / extension / contentType on _ExportFormat; svg and png share _graphSvg. Toasts, filenames and MIME types are unchanged.
  • UI controls: _toggleControls / _showControlsToast replace the duplicated "Controls Shown/Hidden" toast.
  • Graph renderer: _GraphNode / _GraphEdge typedefs, classList.toggle, _onWorkerMessage, and reuse of VizPackage.isOutdated.
  • executable.dart: extract _resolveLocalPath and _printUpdateOrder; remove the unused _printContent / _getContentDot.
  • mermaid.dart: a small _MermaidStyles class owns the node and link style lists (addNode / addEdge / write). Output is byte-identical (checked on this package and test/demo_workspace).
  • Rebuild embedded web assets (lib/src/assets.g.dart) via dart tool/update_assets.dart.

Gates: dart format, dart analyze --fatal-infos, dart test, dart test -P presubmit, api_summary --check (no API change), cognitive_complexity --fail-threshold 15 (max 15), --git-diff origin/main --fail-on-increase (0 violations, net delta -116), and kscripts pr-check.

- Reduce all 11 functions above cognitive complexity 15 into the 8-15 target zone across `dot.dart`, `executable.dart`, `terminate.dart`, `update_order.dart`, `graph_renderer.dart`, and `ui_manager.dart`.
- Re-inline single-caller `SAFE_INLINE` micro-helpers and group shared parameter clumps into private records in `mermaid.dart` and `service.dart`.
- Rebuild embedded web assets (`lib/src/assets.g.dart`) via `dart tool/update_assets.dart`.
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

📊 Cognitive Complexity Analysis

Net Delta: -116 | Added: 15 | Increased: 1 | Improved: 12 | Removed: 3 | Violations: 0

Status Declaration Location Delta Score
🔵 GraphRenderer._attachListeners lib/src/web/graph_renderer.dart:L233-275 +15 new -> 15
🟢 _injectStyles lib/src/web/ui_manager.dart:L647-682 -4 19 -> 15
🟢 computeUpdateOrder lib/src/update_order.dart:L4-51 -2 16 -> 14
🟢 waitForTerminate lib/src/terminate.dart:L20-87 -12 26 -> 14
🟢 GraphRenderer._updateOver lib/src/web/graph_renderer.dart:L277-329 -25 38 -> 13
🟢 UIManager lib/src/web/ui_manager.dart:L146-239 -15 25 -> 10
🟢 GraphRenderer._renderWithWorker lib/src/web/graph_renderer.dart:L105-142 -19 29 -> 10
🔵 _edgeProperties lib/src/dot.dart:L93-126 +8 new -> 8
🔵 _nodeProperties lib/src/dot.dart:L51-91 +8 new -> 8
🟢 UIManager._exportAction lib/src/web/ui_manager.dart:L315-355 -9 17 -> 8
🟢 run lib/src/executable.dart:L23-94 -21 29 -> 8
🟡 VizRootExt.toDot lib/src/dot.dart:L10-48 +6 1 -> 7
🟢 GraphRenderer._updateBody lib/src/web/graph_renderer.dart:L163-231 -19 26 -> 7
🔵 _printUpdateOrder lib/src/executable.dart:L121-139 +6 new -> 6
🔵 GraphRenderer._onWorkerMessage lib/src/web/graph_renderer.dart:L144-161 +6 new -> 6
🟢 UIManager._exportPng lib/src/web/ui_manager.dart:L389-440 -17 23 -> 6
🔵 _MermaidStyles.addEdge lib/src/mermaid.dart:L94-103 +5 new -> 5
🔵 _resolveLocalPath lib/src/executable.dart:L96-119 +4 new -> 4
🔵 _MermaidStyles.addNode lib/src/mermaid.dart:L88-92 +4 new -> 4
🔵 UIManager._handleBodyChange lib/src/web/ui_manager.dart:L241-257 +4 new -> 4
🟢 _writeNode lib/src/mermaid.dart:L50-69 -2 6 -> 4
🔵 UIManager._handlePngBlob lib/src/web/ui_manager.dart:L442-460 +3 new -> 3
🔵 UIManager._exportText lib/src/web/ui_manager.dart:L372-387 +2 new -> 2
🔵 _MermaidStyles._writeClass lib/src/mermaid.dart:L122-124 +1 new -> 1
🔵 _MermaidStyles._writeLinkStyle lib/src/mermaid.dart:L126-128 +1 new -> 1
🔵 UIManager._graphSvg lib/src/web/ui_manager.dart:L364-370 +1 new -> 1
🔵 UIManager._showControlsToast lib/src/web/ui_manager.dart:L280-284 +1 new -> 1
🟢 UIManager._toggleControls lib/src/web/ui_manager.dart:L275-278 -1 1 -> 0
🗑️ _classifyEdge lib/src/mermaid.dart -5 5 -> deleted
🗑️ _writeClassesAndStyles lib/src/mermaid.dart -6 6 -> deleted
🗑️ _writeDot lib/src/dot.dart -34 34 -> deleted

- service.dart: restore _loadPackageGraphFile, _loadPackageConfigFile,
  _loadPubspecForPackage, _resolvePrimaryVersion; drop the
  _PrimaryBuildContext record and return _buildPrimaryPackage to named
  params
- viz_root.dart: restore _filterIgnored, _filterStandard, _filterIsolated
  so every filter() pipeline step is a named method
- ui_manager.dart: share the controls toast via _toggleControls and
  _showControlsToast; make the unreachable png case throw StateError
- pubviz.dart: restore _indent
- mermaid.dart: pass rootName/isWorkspace to _writeNode instead of VizRoot
- ui_manager.dart: dispatch all export formats in one exhaustive switch;
  dot/mermaid/svg share _exportText, svg/png share _graphSvg, so the
  unreachable png arm is gone. Toasts, filenames, and MIME types are
  unchanged.
- mermaid.dart: replace the six-list _MermaidStyleLists record with a
  _MermaidStyles class (addNode/addEdge/write). Output is byte-identical.
@kevmoo kevmoo changed the title Reduce cognitive complexity and inline shallow helpers Reduce cognitive complexity to <= 15 and de-duplicate web UI export Oct 8, 2026
@kevmoo
kevmoo merged commit 4381902 into main Oct 8, 2026
9 checks passed
@kevmoo
kevmoo deleted the cognitive-complexity-1.0 branch October 8, 2026 17:45
kevmoo added a commit to kevmoo/analytica.dart that referenced this pull request Oct 9, 2026
…184)

The `dart-cognitive-complexity` skill pushes agents to clear every
`shallow` and `file_split` finding, which caused over-refactoring and
regressions in dogfood runs.

## Changes

- **Stop rule:** only declarations above the threshold are work items.
When max CC <= threshold, report done and stop; anything further needs a
user ask.
- **Advisory, not gates:** `shallow` and `file_split` are documented as
optional review aids. Removed the "batch re-inline SAFE_INLINE" triage
option and every `--fail-on-safe-inline` gate recommendation. The
findings-table action now reads "Review: consider re-inlining if ...".
- **Keep/revert heuristics (new §5.3):** keep de-duplication, guard
clauses, and named step helpers. Don't inline helpers with same-shape
siblings or meaningful names/docs, don't make style-only edits, and
don't split files without a cohesive name (no barrels or cycles).
- **Hot-loop rule (new §5.4):** benchmark before splitting per-element
kernels. Prefer an inline kernel with `// cognitive_complexity:ignore`
on the line before the declaration, plus a reason comment.
- **Blind self-review:** for diffs over ~300 lines, a fresh agent or
human reviewer given only the before/after trees and diff judges each
change.
- **`refactoring_recipes.md`:** Pattern G no longer says "always
re-inline". Pattern F now says to name split files by their contents.
- **Evals:** replaced the shallow-gate assertion and added negative
assertions (no batch re-inline, sibling helpers stay extracted, no
style-only edits). Added `complexity_stop_when_clean`.

Docs-only: no version, pin, or CHANGELOG changes.

## Evidence

Blind reviews of the dogfood PRs found that fixing CC > 15 helped, but
re-inlining `SAFE_INLINE` helpers and name-by-one-function file splits
mostly hurt:

- kevmoo/qr.dart#174, kevmoo/dash_skills#59, kevmoo/pubviz#196,
kevmoo/scripts.dart#147
- kevmoo/qr.dart#175 restored an inline hot kernel after the helper
split made generation 39-66% slower on AOT

Related (not fixed here): #181, #183
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