Repository navigation
Expand file tree
/
Copy pathflywheel.py
More file actions
1542 lines (1312 loc) · 57.5 KB
/
Copy pathflywheel.py
File metadata and controls
1542 lines (1312 loc) · 57.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
flywheel.py — codexbro flywheel runner for the retrace PCB RE toolkit.
Orchestrates all improvement flywheels to continuously improve the repo's
visual showcase. Safe to run repeatedly (idempotent).
Usage:
pytho
8000
n tools/flywheel.py run # Run all flywheels
python tools/flywheel.py run --quick # Stats + lint only (fast)
python tools/flywheel.py status # Show flywheel health
"""
from __future__ import annotations
import ast
import json
import re
import subprocess
import sys
from datetime import datetime, timezone
from pathlib import Path
import click
# Intelligence layer (P0/P1/P2)
try:
from flywheel_intelligence import FlywheelBrain
_HAS_BRAIN = True
except ImportError:
_HAS_BRAIN = False
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
REPO_ROOT = Path(__file__).parent.parent.resolve()
SRC_DIR = REPO_ROOT / "src" / "retrace"
TESTS_DIR = REPO_ROOT / "tests"
STATE_FILE = REPO_ROOT / ".flywheel_state.json"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _run(cmd: list[str], **kwargs) -> subprocess.CompletedProcess:
"""Run a subprocess, always capturing output."""
return subprocess.run(
cmd,
capture_output=True,
text=True,
cwd=REPO_ROOT,
**kwargs,
)
def _header(title: str) -> None:
click.echo("")
click.echo(click.style(f"── {title} ", fg="cyan", bold=True) + click.style("─" * (60 - len(title)), fg="cyan"))
def _ok(msg: str) -> None:
click.echo(click.style(" ✓ ", fg="green") + msg)
def _warn(msg: str) -> None:
click.echo(click.style(" ⚠ ", fg="yellow") + msg)
def _err(msg: str) -> None:
click.echo(click.style(" ✗ ", fg="red") + msg)
def _info(msg: str) -> None:
click.echo(click.style(" · ", fg="bright_black") + msg)
def _load_state() -> dict:
if STATE_FILE.exists():
try:
return json.loads(STATE_FILE.read_text())
except Exception:
pass
return {}
def _save_state(state: dict) -> None:
STATE_FILE.write_text(json.dumps(state, indent=2))
# ---------------------------------------------------------------------------
# Flywheel 1: Stats
# ---------------------------------------------------------------------------
def flywheel_stats() -> bool:
"""Run readme_stats.py update to keep README numbers current."""
_header("Stats flywheel — update README metrics")
stats_script = REPO_ROOT / "tools" / "readme_stats.py"
if not stats_script.exists():
_warn("tools/readme_stats.py not found — skipping")
return True
result = _run([sys.executable, str(stats_script), "update"])
if result.returncode == 0:
_ok("README stats updated")
for line in result.stdout.splitlines():
if line.strip():
_info(line.strip())
return True
else:
_warn(f"readme_stats.py exited {result.returncode}")
for line in (result.stdout + result.stderr).splitlines():
if line.strip():
_info(line.strip())
return True # non-fatal — stats update failures don't fail the run
# ---------------------------------------------------------------------------
# Flywheel 2: Lint
# ---------------------------------------------------------------------------
def flywheel_lint() -> tuple[bool, int]:
"""Run ruff check --fix and stage auto-fixed files."""
_header("Lint flywheel — ruff auto-fix")
result = _run(["ruff", "check", "src/", "tests/", "--fix"])
fixed_count = 0
for line in result.stdout.splitlines():
m = re.search(r"Fixed (\d+) error", line)
if m:
fixed_count = int(m.group(1))
if result.returncode == 0:
if fixed_count:
_ok(f"Ruff auto-fixed {fixed_count} issue(s)")
# Stage the fixed files
stage = _run(["git", "add", "src/", "tests/"])
if stage.returncode == 0:
_info("Staged lint-fixed files (not committed)")
else:
_warn("git add failed — manual staging needed")
else:
_ok("No lint issues found")
return True, fixed_count
else:
# Remaining violations that couldn't be auto-fixed
_warn(f"Ruff found un-fixable violations (exit {result.returncode})")
for line in result.stdout.splitlines()[:10]:
if line.strip():
_info(line.strip())
return True, fixed_count # lint failures are warnings, not fatal
# ---------------------------------------------------------------------------
# Flywheel 3: Tests
# ---------------------------------------------------------------------------
def flywheel_tests() -> tuple[bool, int, int, list[str]]:
"""Run pytest and report pass/fail counts."""
_header("Test flywheel — pytest")
result = _run([sys.executable, "-m", "pytest", "tests/", "-q", "--tb=short"])
passed = failed = 0
failed_tests: list[str] = []
for line in result.stdout.splitlines():
# "3 failed, 47 passed in 4.21s"
m = re.search(r"(\d+) failed", line)
if m:
failed = int(m.group(1))
m = re.search(r"(\d+) passed", line)
if m:
passed = int(m.group(1))
# Collect FAILED lines: "FAILED tests/test_foo.py::test_bar - ..."
if line.strip().startswith("FAILED "):
test_id = line.strip().split()[1]
failed_tests.append(test_id)
total = passed + failed
if failed == 0 and total > 0:
_ok(f"All {passed} tests passed")
return True, passed, failed, []
elif failed == 0 and total == 0:
_warn("No tests collected")
return True, 0, 0, []
else:
_err(f"{failed}/{total} tests FAILED")
for t in failed_tests[:20]:
_info(f" FAILED: {t}")
if len(failed_tests) > 20:
_info(f" ... and {len(failed_tests) - 20} more")
# Print short tb summary
in_tb = False
for line in result.stdout.splitlines():
if line.startswith("FAILED") or line.startswith("_ "):
in_tb = True
if in_tb and line.strip():
_info(line)
return False, passed, failed, failed_tests
# ---------------------------------------------------------------------------
# Flywheel 4: Demo generation
# ---------------------------------------------------------------------------
def flywheel_demo() -> bool:
"""Regenerate visual outputs via generate_demo.py (if present)."""
_header("Demo flywheel — regenerate visual outputs")
demo_script = REPO_ROOT / "tools" / "generate_demo.py"
if not demo_script.exists():
_info("tools/generate_demo.py not present yet — skipping gracefully")
return True
result = _run([sys.executable, str(demo_script), "generate"])
if result.returncode == 0:
_ok("Demo assets regenerated")
for line in result.stdout.splitlines():
if line.strip():
_info(line.strip())
# Stage generated assets
_run(["git", "add", "docs/", "demo/", "assets/"])
return True
else:
_warn(f"generate_demo.py failed (exit {result.returncode})")
for line in (result.stdout + result.stderr).splitlines()[:10]:
if line.strip():
_info(line.strip())
return True # non-fatal
# ---------------------------------------------------------------------------
# Flywheel 5: Coverage
# ---------------------------------------------------------------------------
def flywheel_coverage(state: dict) -> tuple[bool, str]:
"""Run pytest --cov and warn if coverage dropped."""
_header("Coverage flywheel — measure and track")
result = _run([
sys.executable, "-m", "pytest",
"--cov=retrace", "--cov-report=term",
"-q", "--tb=no", "--no-header",
])
combined = result.stdout + result.stderr
coverage_pct: str = "N/A"
dropped = False
m = re.search(r"^TOTAL\s+\d+\s+\d+\s+(\d+)%", combined, re.MULTILINE)
if m:
coverage_pct = m.group(1) + "%"
current_val = int(m.group(1))
prev_raw = state.get("coverage_pct", "N/A")
if prev_raw != "N/A":
prev_val = int(prev_raw.rstrip("%"))
if current_val < prev_val:
dropped = True
_warn(
f"Coverage DROPPED: {prev_val}% → {current_val}% "
f"(delta: {current_val - prev_val}%)"
)
elif current_val > prev_val:
_ok(f"Coverage improved: {prev_val}% → {current_val}%")
else:
_ok(f"Coverage stable at {current_val}%")
else:
_ok(f"Coverage: {current_val}% (baseline established)")
else:
_warn("Could not parse coverage from pytest output")
for line in combined.splitlines()[-20:]:
if "TOTAL" in line or "error" in line.lower():
_info(line.strip())
return not dropped, coverage_pct
# ---------------------------------------------------------------------------
# Flywheel 6: Gap analysis
# ---------------------------------------------------------------------------
def flywheel_gaps(coverage_output: str = "") -> dict:
"""Analyze the codebase for quality gaps."""
_header("Gap analysis flywheel — quality gaps")
gaps: dict = {
"zero_coverage_modules": [],
"untested_sources": [],
"todo_count": 0,
"todo_locations": [],
"untyped_functions": [],
}
# -- 6a. Zero-coverage modules --
result = _run([
sys.executable, "-m", "pytest",
"--cov=retrace", "--cov-report=term",
"-q", "--tb=no", "--no-header",
])
combined = result.stdout + result.stderr
zero_cov: list[str] = []
for line in combined.splitlines():
# Lines look like: "retrace/cli.py 120 85 29% ..."
m = re.match(r"\s*(retrace[\w/\\\.]+\.py)\s+\d+\s+\d+\s+0%", line)
if m:
zero_cov.append(m.group(1))
gaps["zero_coverage_modules"] = zero_cov
if zero_cov:
_warn(f"Modules at 0% coverage ({len(zero_cov)}):")
for mod in zero_cov:
_info(f" {mod}")
else:
_ok("No modules at 0% coverage")
# -- 6b. Source files without test files --
src_modules: list[str] = []
for src_file in SRC_DIR.rglob("*.py"):
if src_file.name == "__init__.py":
continue
# Compute expected test file name
rel = src_file.relative_to(SRC_DIR)
# e.g. analysis/cross_board.py → test_cross_board.py
stem = src_file.stem
expected_test = TESTS_DIR / f"test_{stem}.py"
if not expected_test.exists():
src_modules.append(str(src_file.relative_to(REPO_ROOT)))
gaps["untested_sources"] = src_modules
if src_modules:
_warn(f"Source files without corresponding tests ({len(src_modules)}):")
for s in src_modules:
_info(f" {s}")
else:
_ok("All source modules have test files")
# -- 6c. TODO/FIXME/HACK/XXX/WORKAROUND comments --
todo_pattern = re.compile(r"#.*\b(TODO|FIXME|HACK|XXX|WORKAROUND)\b", re.IGNORECASE)
todo_locations: list[str] = []
for py_file in SRC_DIR.rglob("*.py"):
try:
lines = py_file.read_text(encoding="utf-8").splitlines()
for i, line in enumerate(lines, 1):
if todo_pattern.search(line):
rel_path = str(py_file.relative_to(REPO_ROOT))
todo_locations.append(f"{rel_path}:{i}")
except Exception:
pass
gaps["todo_count"] = len(todo_locations)
gaps["todo_locations"] = todo_locations[:50] # cap for state file
if todo_locations:
_warn(f"TODO/FIXME/HACK comments in source: {len(todo_locations)}")
for loc in todo_locations[:10]:
_info(f" {loc}")
if len(todo_locations) > 10:
_info(f" ... and {len(todo_locations) - 10} more")
else:
_ok("No TODO/FIXME/HACK comments in source")
# -- 6d. Functions without type hints --
untyped: list[str] = []
for py_file in SRC_DIR.rglob("*.py"):
try:
tree = ast.parse(py_file.read_text(encoding="utf-8"))
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
# Skip private/dunder methods and test helpers
if node.name.startswith("_"):
continue
missing_return = node.returns is None
missing_args = any(
arg.annotation is None
for arg in node.args.args
if arg.arg != "self"
)
if missing_return or missing_args:
rel = str(py_file.relative_to(REPO_ROOT))
untyped.append(f"{rel}:{node.lineno} {node.name}()")
except Exception:
pass
gaps["untyped_functions"] = untyped[:50]
if untyped:
_warn(f"Public functions missing type hints: {len(untyped)}")
for fn in untyped[:8]:
_info(f" {fn}")
if len(untyped) > 8:
_info(f" ... and {len(untyped) - 8} more")
else:
_ok("All public functions have type hints")
return gaps
def flywheel_regression(state: dict, metrics: dict) -> list[str]:
"""Compare current metrics against previous run and flag regressions."""
_header("Regression analysis flywheel — delta tracking")
checks = [
("tests_passed", "Tests passed", True),
("tests_failed", "Tests failed", False),
("zero_coverage_count", "Modules at 0% coverage", False),
("untested_source_count", "Untested source files", False),
("todo_count", "TODO/FIXME comments", False),
("untyped_function_count", "Untyped public functions", False),
]
for key, label, higher_is_better in checks:
prev = state.get(key)
curr = metrics.get(key)
if prev is None or curr is None:
continue
if not isinstance(prev, (int, float)) or not isinstance(curr, (int, float)):
continue
delta = curr - prev
if delta == 0:
_ok(f"{label}: {curr} (unchanged)")
elif (higher_is_better and delta > 0) or (not higher_is_better and delta < 0):
_ok(f"{label}: {prev} → {curr} ({'+' if delta > 0 else ''}{delta}) ↑")
else:
msg = f"{label}: {prev} → {curr} ({'+' if delta > 0 else ''}{delta}) REGRESSION"
_warn(msg)
regressions.append(msg)
prev_cov = state.get("coverage_pct", "")
curr_cov = metrics.get("coverage_pct", "")
if prev_cov and curr_cov and prev_cov != "N/A" and curr_cov != "N/A":
try:
prev_pct = float(str(prev_cov).rstrip("%"))
curr_pct = float(str(curr_cov).rstrip("%"))
delta = curr_pct - prev_pct
if delta >= 0:
_ok(f"Coverage: {prev_cov} → {curr_cov} ({'+' if delta > 0 else ''}{delta:.1f}pp)")
else:
msg = f"Coverage: {prev_cov} → {curr_cov} ({delta:.1f}pp) REGRESSION"
_warn(msg)
regressions.append(msg)
except ValueError:
pass
history = state.get("regression_history", [])
now_iso = datetime.now(timezone.utc).isoformat()
history.append({
"timestamp": now_iso,
"regressions": regressions,
"tests_passed": metrics.get("tests_passed"),
"coverage": metrics.get("coverage_pct"),
})
state["regression_history"] = history[-50:]
if not regressions:
_ok("No regressions detected")
else:
_warn(f"{len(regressions)} regression(s) detected")
return regressions
def flywheel_component_db() -> dict:
"""Track component DB growth and identify missing categories."""
_header("Component DB flywheel — growth and coverage")
matcher_path = SRC_DIR / "identification" / "matcher.py"
if not matcher_path.exists():
_warn("matcher.py not found")
return {}
content = matcher_path.read_text(encoding="utf-8")
category_counts: dict[str, int] = {}
for m in re.finditer(r'"category"\s*:\s*"(\w+)"', content):
cat = m.group(1)
category_counts[cat] = category_counts.get(cat, 0) + 1
total = sum(category_counts.values())
_ok(f"Total component entries: {total}")
_ok(f"Categories: {len(category_counts)}")
for cat, count in sorted(category_counts.items(), key=lambda x: -x[1]):
_info(f" {cat}: {count}")
expected_categories = {
"mcu", "memory", "regulator", "fpga", "network", "rf",
"secure_element", "sensor", "pmic", "display", "automotive",
}
missing = expected_categories - set(category_counts.keys())
if missing:
_warn(f"Missing categories: {', '.join(sorted(missing))}")
else:
_ok("All expected categories present")
return {
"component_total": total,
"component_categories": len(category_counts),
"component_by_category": category_counts,
"missing_categories": sorted(missing) if missing else [],
}
# ---------------------------------------------------------------------------
# Design audit flywheel — scores README/showcase against psychology criteria
# ---------------------------------------------------------------------------
_DESIGN_CRITERIA = {
"hero_above_fold": {
"weight": 10,
"description": "Hero section with value prop in first 5 lines (primacy effect)",
},
"stats_visible": {
"weight": 8,
"description": "Quantitative social proof (tests, coverage, LOC) visible early (anchoring)",
},
"demo_before_install": {
"weight": 9,
"description": "Visual demo before installation instructions (picture superiority)",
},
"contrast_table": {
"weight": 7,
"description": "Competitive comparison table present (contrast principle)",
},
"progressive_disclosure": {
"weight": 8,
"description": "Details/summary elements for advanced content (cognitive load reduction)",
},
"copy_paste_install": {
"weight": 9,
"description": "Single copy-paste install command (friction reduction)",
},
"concrete_numbers": {
"weight": 7,
"description": "Specific numbers > vague claims (concreteness effect)",
},
"social_proof_badges": {
"weight": 6,
"description": "CI/coverage/license badges present (authority/trust signals)",
},
"cta_links": {
"weight": 7,
"description": "Quick navigation links near the top (scanning support)",
},
"visual_hierarchy": {
"weight": 8,
"description": "Clear heading progression H1 > H2 > H3 (gestalt/hierarchy)",
},
"real_world_examples": {
"weight": 9,
"description": "Named real devices/CVEs as examples (narrative transportation)",
},
"output_samples": {
"weight": 8,
"description": "Actual CLI output shown (expectation setting)",
},
"api_examples": {
"weight": 7,
"description": "Code examples with imports (developer confidence)",
},
"security_context": {
"weight": 6,
"description": "MITRE ATT&CK / CWE references (domain credibility)",
},
"image_density": {
"weight": 8,
"description": "Multiple SVG/image demos (dual coding theory)",
},
}
def flywheel_design_audit() -> dict:
"""Score README.md against evidence-based design psychology criteria.
Returns a dict of scores, suggestions, and total score.
Psychology principles applied:
- Primacy effect: first impressions dominate recall
- Picture superiority: images recalled 6x better than text
- Anchoring: first numbers seen set expectation frame
- Progressive disclosure: reduce cognitive load
- Contrast principle: comparisons make value obvious
- Narrative transportation: stories > abstractions
- Dual coding: text + images together > either alone
"""
_header("Design audit flywheel — README psychology scoring")
readme_path = REPO_ROOT / "README.md"
if not readme_path.exists():
_warn("README.md not found")
return {}
content = readme_path.read_text(encoding="utf-8")
lines = content.splitlines()
scores: dict[str, int] = {}
suggestions: list[str] = []
first_20 = "\n".join(lines[:20]).lower()
scores["hero_above_fold"] = 10 if ("photo in" in first_20 or "attack surface" in first_20) else 0
if not scores["hero_above_fold"]:
suggestions.append("Move value proposition to first 5 lines (primacy effect)")
first_30 = "\n".join(lines[:30]).lower()
scores["stats_visible"] = 8 if ("tests" in first_30 and "loc" in first_30) else 0
if not scores["stats_visible"]:
suggestions.append("Add quantitative stats (tests, LOC) in first 30 lines (anchoring)")
h2_positions = [i for i, l in enumerate(lines) if l.startswith("## ")]
img_positions = [i for i, l in enumerate(lines) if "<img" in l.lower() or "![" in l]
install_positions = [i for i, l in enumerate(lines) if "pip install" in l.lower()]
first_img = min(img_positions) if img_positions else 9999
first_install = min(install_positions) if install_positions else 9999
scores["demo_before_install"] = 9 if first_img < first_install else 0
if not scores["demo_before_install"]:
suggestions.append("Show demo images before install command (picture superiority)")
scores["contrast_table"] = 7 if "|" in content and ("prior work" in content.lower() or "comparison" in content.lower()) else 0
if not scores["contrast_table"]:
suggestions.append("Add competitive comparison table (contrast principle)")
detail_count = content.count("<details>")
scores["progressive_disclosure"] = min(8, detail_count * 2)
if detail_count < 3:
suggestions.append(f"Add more <details> sections ({detail_count} found, target 4+)")
scores["copy_paste_install"] = 9 if "pip install" in content else 0
number_pattern = re.compile(r'\b\d{2,}\b')
number_count = len(number_pattern.findall(content[:3000]))
scores["concrete_numbers"] = min(7, number_count)
if number_count < 5:
suggestions.append("Use more specific numbers in first 3000 chars (concreteness effect)")
badge_count = content.count("img.shields.io")
scores["social_proof_badges"] = min(6, badge_count * 2)
link_section = "\n".join(lines[:20])
nav_links = link_section.count("[") + link_section.count("href=")
scores["cta_links"] = min(7, nav_links)
h1 = sum(1 for l in lines if l.startswith("# ") and not l.startswith("## "))
h2 = sum(1 for l in lines if l.startswith("## "))
h3 = sum(1 for l in lines if l.startswith("### "))
scores["visual_hierarchy"] = 8 if (h1 >= 1 and h2 >= 3 and h3
10BC8
>= 3) else 4 if h2 >= 2 else 0
real_devices = ["xbox", "cisco", "thrangrycat", "cve-", "arcanedoor"]
found_devices = sum(1 for d in real_devices if d in content.lower())
scores["real_world_examples"] = min(9, found_devices * 2)
code_block_count = content.count("```")
output_blocks = content.count("```\n ") + content.count("```\nTotal") + content.count("```\nTop")
scores["output_samples"] = min(8, output_blocks * 2 + code_block_count // 3)
scores["api_examples"] = min(7, content.count("from retrace"))
mitre_refs = content.count("ATT&CK") + content.count("CWE-") + content.count("CVSS")
scores["security_context"] = min(6, mitre_refs)
img_count = len(img_positions)
scores["image_density"] = min(8, img_count)
total = sum(scores.values())
max_total = sum(c["weight"] for c in _DESIGN_CRITERIA.values())
pct = round(100 * total / max_total) if max_total > 0 else 0
_ok(f"Design score: {total}/{max_total} ({pct}%)")
for criterion, score in sorted(scores.items(), key=lambda x: x[1]):
max_score = _DESIGN_CRITERIA[criterion]["weight"]
status = "OK" if score >= max_score * 0.7 else "LOW" if score > 0 else "MISS"
color = "green" if status == "OK" else "yellow" if status == "LOW" else "red"
_info(f" [{click.style(status, fg=color)}] {criterion}: {score}/{max_score}")
if suggestions:
_warn(f"{len(suggestions)} improvement suggestions:")
for s in suggestions:
_info(f" - {s}")
else:
_ok("All design criteria met!")
return {
"design_total": total,
"design_max": max_total,
"design_pct": pct,
"design_scores": scores,
"design_suggestions": suggestions,
}
def flywheel_svg_audit() -> dict:
"""Audit all SVG output functions for design consistency.
Generates test SVGs from each export function and checks for:
- Consistent color palette (no rogue colors)
- Font family consistency
- Proper dark theme (background, text contrast)
- Valid SVG structure
- No inline styles that bypass the design system
"""
_header("SVG design audit flywheel")
scores: dict[str, int] = {}
issues: list[str] = []
# Check SVG export source files for design consistency
svg_sources = [
SRC_DIR / "export" / "svg.py",
SRC_DIR / "export" / "pinout_diagram.py",
SRC_DIR / "export" / "bom.py",
]
approved_colors = {
"#0a0e1a", "#111827", "#1e293b", "#e2e8f0", "#94a3b8", "#64748b",
"#22d3ee", "#3b82f6", "#22c55e", "#ef4444", "#6b7280", "#f59e0b",
"#a855f7", "#ff6b6b", "#334155",
"#0b1120", "#0f1629", "#111d35", "#131a2b", "#1a2332", "#1a2a40",
"#1e2d40", "#2a4060", "#7f1d1d",
"#06b6d4", "#14b8a6", "#f97316", "#ec4899",
"#1a3050", "#2e5070", "#8b5cf6", "#d946ef", "#fbbf24",
}
approved_fonts = {"JetBrains Mono", "Fira Code", "SF Mono", "monospace"}
for src in svg_sources:
if not src.exists():
continue
content = src.read_text(encoding="utf-8")
name = src.name
# Check color consistency
hex_colors = set(re.findall(r'#[0-9a-fA-F]{6}', content))
rogue_colors = hex_colors - approved_colors
if len(rogue_colors) <= 3:
scores[f"{name}_colors"] = 3
else:
scores[f"{name}_colors"] = 1
issues.append(f"{name}: {len(rogue_colors)} non-standard colors: {', '.join(sorted(rogue_colors)[:5])}")
# Check font consistency
has_approved_font = any(f in content for f in approved_fonts)
scores[f"{name}_fonts"] = 2 if has_approved_font else 0
if not has_approved_font:
issues.append(f"{name}: no approved monospace font found")
# Check dark theme
has_dark_bg = "#0a0e1a" in content or "#111827" in content
scores[f"{name}_dark_theme"] = 2 if has_dark_bg else 0
# Check generated SVG examples
examples_dir = REPO_ROOT / "docs" / "examples"
svg_files = list(examples_dir.glob("*.svg")) if examples_dir.exists() else []
scores["example_count"] = min(5, len(svg_files))
if len(svg_files) < 4:
issues.append(f"Only {len(svg_files)} example SVGs (target: 4+)")
for svg_file in svg_files[:8]:
svg_content = svg_file.read_text(encoding="utf-8")
if not svg_content.strip().startswith("<svg"):
issues.append(f"{svg_file.name}: invalid SVG (missing <svg> root)")
if not svg_content.strip().endswith("</svg>"):
issues.append(f"{svg_file.name}: unclosed </svg>")
# Check _IC_PINOUTS coverage
try:
sys.path.insert(0, str(REPO_ROOT / "src"))
from retrace.export.pinout_diagram import _IC_PINOUTS
scores["ic_pinout_coverage"] = min(3, len(_IC_PINOUTS))
if len(_IC_PINOUTS) < 2:
issues.append(f"Only {len(_IC_PINOUTS)} IC pinout packages (target: 2+)")
except Exception:
scores["ic_pinout_coverage"] = 0
total = sum(scores.values())
max_total = len(svg_sources) * 7 + 5 + 3 # 7 per source + example_count(5) + ic_pinout(3)
pct = round(100 * total / max_total) if max_total > 0 else 0
_ok(f"SVG design score: {total}/{max_total} ({pct}%)")
for criterion, score in sorted(scores.items(), key=lambda x: x[1]):
status = "OK" if score >= 2 else "LOW" if score > 0 else "MISS"
color = "green" if status == "OK" else "yellow" if status == "LOW" else "red"
_info(f" [{click.style(status, fg=color)}] {criterion}: {score}")
if issues:
_warn(f"{len(issues)} SVG design issues:")
for issue in issues:
_info(f" - {issue}")
else:
_ok("All SVG design checks passed!")
return {
"svg_total": total,
"svg_max": max_total,
"svg_pct": pct,
"svg_issues": issues,
}
# ---------------------------------------------------------------------------
# Flywheel 11: README formatting audit
# ---------------------------------------------------------------------------
def flywheel_readme_format() -> dict:
"""Audit README.md for professional formatting and layout quality.
Checks:
- HTML tables use consistent column widths (no colspan hacks)
- All referenced images exist on disk
- No broken image links
- Section separators between gallery groups
- Proper alt text on images
- No raw HTML where markdown suffices
- Consistent caption formatting
"""
_header("README format flywheel — layout quality audit")
readme_path = REPO_ROOT / "README.md"
if not readme_path.exists():
_warn("README.md not found")
return {}
content = readme_path.read_text(encoding="utf-8")
lines = content.splitlines()
issues: list[str] = []
scores: dict[str, int] = {}
# 11a. Check all img src references resolve to existing files
img_refs = re.findall(r'<img\s+[^>]*src="([^"]+)"', content)
missing_imgs: list[str] = []
for ref in img_refs:
img_path = REPO_ROOT / ref
if not img_path.exists():
missing_imgs.append(ref)
scores["images_exist"] = 5 if not missing_imgs else max(0, 5 - len(missing_imgs))
if missing_imgs:
_err(f"{len(missing_imgs)} broken image reference(s):")
for m in missing_imgs:
_info(f" missing: {m}")
else:
_ok(f"All {len(img_refs)} image references resolve to files on disk")
# 11b. Check for colspan hacks (uneven column layouts)
colspan_count = content.count("colspan=")
scores["no_colspan"] = 3 if colspan_count == 0 else max(0, 3 - colspan_count)
if colspan_count > 0:
_warn(f"{colspan_count} colspan usage(s) — prefer equal-width columns")
else:
_ok("No colspan hacks — clean column layout")
# 11c. Check all images have alt text
imgs_without_alt = re.findall(r'<img\s+(?!.*alt=)[^>]*>', content)
scores["alt_text"] = 3 if not imgs_without_alt else 0
if imgs_without_alt:
_warn(f"{len(imgs_without_alt)} image(s) missing alt text")
else:
_ok("All images have alt text")
# 11d. Check gallery section separators (--- between sections)
table_sections = content.split("<table>")
if len(table_sections) > 2:
between_tables = 0
for i in range(1, len(table_sections)):
before = table_sections[i - 1]
if "---" in before.split("</table>")[-1] if "</table>" in before else "":
between_tables += 1
scores["section_separators"] = min(3, between_tables)
else:
scores["section_separators"] = 3
# 11e. Check for consistent table column widths within each table
width_pattern = re.compile(r'width="(\d+%)"')
inconsistent_tables = 0
in_table = False
table_widths: list[str] = []
for line in lines:
if "<table>" in line:
in_table = True
table_widths = []
elif "</table>" in line:
in_table = False
if table_widths:
unique = set(table_widths)
if len(unique) > 2:
inconsistent_tables += 1
elif in_table:
for w in width_pattern.findall(line):
table_widths.append(w)
scores["consistent_widths"] = 3 if inconsistent_tables == 0 else max(0, 3 - inconsistent_tables)
if inconsistent_tables:
_warn(f"{inconsistent_tables} table(s) with inconsistent column widths")
else:
_ok("All tables use consistent column widths")
# 11f. No empty table cells
empty_cells = content.count("<td></td>") + content.count("<td>\n</td>")
scores["no_empty_cells"] = 2 if empty_cells == 0 else 0
if empty_cells:
_warn(f"{empty_cells} empty table cell(s)")
else:
_ok("No empty table cells")
total = sum(scores.values())
max_total = 19
pct = round(100 * total / max_total) if max_total > 0 else 0
_ok(f"README format score: {total}/{max_total} ({pct}%)")
for criterion, score in sorted(scores.items(), key=lambda x: x[1]):
status = "OK" if score >= 2 else "LOW" if score > 0 else "MISS"
color = "green" if status == "OK" else "yellow" if status == "LOW" else "red"
_info(f" [{click.style(status, fg=color)}] {criterion}: {score}")
if issues:
_warn(f"{len(issues)} formatting issues:")
for issue in issues:
_info(f" - {issue}")
return {
"readme_format_total": total,
"readme_format_max": max_total,
"readme_format_pct": pct,
"readme_missing_images": missing_imgs,
"readme_colspan_count": colspan_count,
}
# ---------------------------------------------------------------------------
# Flywheel 12: SVG rendering validation
# ---------------------------------------------------------------------------
def flywheel_svg_render_check() -> dict:
"""Validate generated SVGs are structurally correct and render properly.
Checks each SVG in docs/examples/ for:
- Valid XML structure (opens and closes properly)
- Has viewBox attribute (responsive rendering)
- Text elements aren't truncated (no text > viewBox width)
- No overlapping text elements at same Y coordinate
- Pin labels in pinout SVGs match expected count
- BOM tables have correct column count
"""
_header("SVG render validation flywheel")
examples_dir = REPO_ROOT / "docs" / "examples"
if not examples_dir.exists():
_warn("docs/examples/ not found")
return {}
svg_files = sorted(examples_dir.glob("*.svg"))
issues: list[str] = []
scores: dict[str, int] = {}
for svg_file in svg_files:
name = svg_file.name
content = svg_file.read_text(encoding="utf-8")
# Valid SVG structure
if not content.strip().startswith("<svg"):
issues.append(f"{name}: missing <svg> root element")
if not content.strip().endswith("</svg>"):
issues.append(f"{name}: unclosed </svg>")
# Has viewBox
if "viewBox" not in content:
issues.append(f"{name}: missing viewBox attribute (won't scale responsively)")
# Extract viewBox dimensions
vb_match = re.search(r'viewBox="0 0 (\d+) (\d+)"', content)
if vb_match:
vb_w = int(vb_match.group(1))
vb_h = int(vb_match.group(2))
# Check SVG height matches content (crude check for truncation)
height_match = re.search(r'height="(\d+)"', content)
if height_match:
declared_h = int(height_match.group(1))
if declared_h != vb_h:
issues.append(f"{name}: height ({declared_h}) != viewBox height ({vb_h})")
# Pinout-specific checks
if "pinout" in name.lower():
pin_dots = len(re.findall(r'<circle[^>]*fill="(?:#3b82f6|#22c55e|#ef4444|#6b7280|#f59e0b|#a855f7)"', content))
pin_labels = len(re.findall(r'Pin \d+', content))
if "JTAG" in name.upper() and pin_dots < 10:
issues.append(f"{name}: JTAG pinout has only {pin_dots} pin dots (expected 10-20)")
if "UART" in name.upper() and pin_dots < 3:
issues.append(f"{name}: UART pinout has only {pin_dots} pin dots (expected 3-6)")
# BOM-specific checks
if "bom" in name.lower():
row_count = len(re.findall(r'<rect x="0" y="\d+" width="\d+" height="20"', content))
if row_count < 10:
issues.append(f"{name}: BOM has only {row_count} component rows (expected 10+)")
header_texts = re.findall(r'font-weight="bold">(\w+)</text>', content)
expected_headers = {"Ref", "Type", "Part", "Marking", "Value", "Package", "Confidence"}
found_headers = set(header_texts) & expected_headers
if len(found_headers) < 5:
issues.append(f"{name}: BOM missing column headers (found {found_headers})")
valid_count = len(svg_files) - len([i for i in issues if "missing <svg>" in i or "unclosed" in i])
scores["valid_structure"] = min(5, valid_count)
scores["issue_free"] = max(0, 5 - len(issues))
total = sum(scores.values())
max_total = 10
pct = round(100 * total / max_total) if max_total > 0 else 0