cgcardona / muse public
render_html.py python
1735 lines 60.8 KB
9cdb32a8 fix: remove Domain Registry from nav — muse logo already links home Gabriel Cardona <gabriel@tellurstori.com> 2d ago
1 #!/usr/bin/env python3
2 """Muse Tour de Force — HTML renderer.
3
4 Takes the structured TourData dict produced by tour_de_force.py and renders
5 a self-contained, shareable HTML file with an interactive D3 commit DAG,
6 operation log, architecture diagram, and animated replay.
7
8 Stand-alone usage
9 -----------------
10 python tools/render_html.py artifacts/tour_de_force.json
11 python tools/render_html.py artifacts/tour_de_force.json --out custom.html
12 """
13 from __future__ import annotations
14
15 import json
16 import pathlib
17 import sys
18 import urllib.request
19
20
21 # ---------------------------------------------------------------------------
22 # D3.js fetcher
23 # ---------------------------------------------------------------------------
24
25 _D3_CDN = "https://cdn.jsdelivr.net/npm/d3@7.9.0/dist/d3.min.js"
26 _D3_FALLBACK = f'<script src="{_D3_CDN}"></script>'
27
28
29 def _fetch_d3() -> str:
30 """Download D3.js v7 minified. Returns the source or a CDN script tag."""
31 try:
32 with urllib.request.urlopen(_D3_CDN, timeout=15) as resp:
33 src = resp.read().decode("utf-8")
34 print(f" ↓ D3.js fetched ({len(src)//1024}KB)")
35 return f"<script>\n{src}\n</script>"
36 except Exception as exc:
37 print(f" ⚠ Could not fetch D3 ({exc}); using CDN link in HTML")
38 return _D3_FALLBACK
39
40
41 # ---------------------------------------------------------------------------
42 # Architecture SVG
43 # ---------------------------------------------------------------------------
44
45 _ARCH_HTML = """\
46 <div class="arch-flow">
47 <div class="arch-row">
48 <div class="arch-box cli">
49 <div class="box-title">muse CLI</div>
50 <div class="box-sub">14 commands</div>
51 <div class="box-detail">init · commit · log · diff · show · branch<br>
52 checkout · merge · reset · revert · cherry-pick<br>
53 stash · tag · status</div>
54 </div>
55 </div>
56 <div class="arch-connector"><div class="connector-line"></div><div class="connector-arrow">▼</div></div>
57 <div class="arch-row">
58 <div class="arch-box registry">
59 <div class="box-title">Plugin Registry</div>
60 <div class="box-sub">resolve_plugin(root)</div>
61 </div>
62 </div>
63 <div class="arch-connector"><div class="connector-line"></div><div class="connector-arrow">▼</div></div>
64 <div class="arch-row">
65 <div class="arch-box core">
66 <div class="box-title">Core Engine</div>
67 <div class="box-sub">DAG · Content-addressed Objects · Branches · Store · Log Graph · Merge Base</div>
68 </div>
69 </div>
70 <div class="arch-connector"><div class="connector-line"></div><div class="connector-arrow">▼</div></div>
71 <div class="arch-row">
72 <div class="arch-box protocol">
73 <div class="box-title">MuseDomainPlugin Protocol</div>
74 <div class="box-sub">Implement 6 methods → get the full VCS for free</div>
75 </div>
76 </div>
77 <div class="arch-connector"><div class="connector-line"></div><div class="connector-arrow">▼</div></div>
78 <div class="arch-row plugins-row">
79 <div class="arch-box plugin active">
80 <div class="box-title">MusicPlugin</div>
81 <div class="box-sub">reference impl<br>MIDI · notes · CC · pitch</div>
82 </div>
83 <div class="arch-box plugin planned">
84 <div class="box-title">GenomicsPlugin</div>
85 <div class="box-sub">planned<br>sequences · variants</div>
86 </div>
87 <div class="arch-box plugin planned">
88 <div class="box-title">SpacetimePlugin</div>
89 <div class="box-sub">planned<br>3D fields · time-slices</div>
90 </div>
91 <div class="arch-box plugin planned">
92 <div class="box-title">YourPlugin</div>
93 <div class="box-sub">implement 6 methods<br>get VCS for free</div>
94 </div>
95 </div>
96 </div>
97
98 <div class="protocol-table">
99 <div class="proto-row header">
100 <div class="proto-method">Method</div>
101 <div class="proto-sig">Signature</div>
102 <div class="proto-desc">Purpose</div>
103 </div>
104 <div class="proto-row">
105 <div class="proto-method">snapshot</div>
106 <div class="proto-sig">snapshot(live_state) → StateSnapshot</div>
107 <div class="proto-desc">Capture current state as a content-addressable JSON blob</div>
108 </div>
109 <div class="proto-row">
110 <div class="proto-method">diff</div>
111 <div class="proto-sig">diff(base, target) → StateDelta</div>
112 <div class="proto-desc">Compute minimal change between two snapshots (added · removed · modified)</div>
113 </div>
114 <div class="proto-row">
115 <div class="proto-method">merge</div>
116 <div class="proto-sig">merge(base, left, right) → MergeResult</div>
117 <div class="proto-desc">Three-way reconcile divergent state lines; surface conflicts</div>
118 </div>
119 <div class="proto-row">
120 <div class="proto-method">drift</div>
121 <div class="proto-sig">drift(committed, live) → DriftReport</div>
122 <div class="proto-desc">Detect uncommitted changes between HEAD and working state</div>
123 </div>
124 <div class="proto-row">
125 <div class="proto-method">apply</div>
126 <div class="proto-sig">apply(delta, live_state) → LiveState</div>
127 <div class="proto-desc">Apply a delta during checkout to reconstruct historical state</div>
128 </div>
129 <div class="proto-row">
130 <div class="proto-method">schema</div>
131 <div class="proto-sig">schema() → DomainSchema</div>
132 <div class="proto-desc">Declare data structure — drives diff algorithm selection per dimension</div>
133 </div>
134 </div>
135 """
136
137
138 # ---------------------------------------------------------------------------
139 # HTML template
140 # ---------------------------------------------------------------------------
141
142 _HTML_TEMPLATE = """\
143 <!DOCTYPE html>
144 <html lang="en">
145 <head>
146 <meta charset="utf-8">
147 <meta name="viewport" content="width=device-width, initial-scale=1">
148 <title>Muse — Demo</title>
149 <style>
150 /* ---- Reset & base ---- */
151 *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
152 :root {
153 --bg: #0d1117;
154 --bg2: #161b22;
155 --bg3: #21262d;
156 --border: #30363d;
157 --text: #e6edf3;
158 --text-mute: #8b949e;
159 --text-dim: #484f58;
160 --accent: #4f8ef7;
161 --accent2: #58a6ff;
162 --green: #3fb950;
163 --red: #f85149;
164 --yellow: #d29922;
165 --purple: #bc8cff;
166 --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
167 --font-ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
168 --radius: 8px;
169 }
170 html { scroll-behavior: smooth; }
171 body {
172 background: var(--bg);
173 color: var(--text);
174 font-family: var(--font-ui);
175 font-size: 14px;
176 line-height: 1.6;
177 min-height: 100vh;
178 }
179
180 /* ---- Stats header ---- */
181 header {
182 background: var(--bg2);
183 border-bottom: 1px solid var(--border);
184 padding: 16px 40px;
185 }
186 .stats-bar {
187 display: flex;
188 gap: 24px;
189 margin-top: 14px;
190 flex-wrap: wrap;
191 }
192 .stat {
193 display: flex;
194 flex-direction: column;
195 align-items: center;
196 gap: 2px;
197 }
198 .stat-num {
199 font-size: 22px;
200 font-weight: 700;
201 font-family: var(--font-mono);
202 color: var(--accent2);
203 }
204 .stat-label {
205 font-size: 11px;
206 color: var(--text-mute);
207 text-transform: uppercase;
208 letter-spacing: 0.8px;
209 }
210 .stat-sep { color: var(--border); font-size: 22px; align-self: center; }
211
212 /* ---- Main layout ---- */
213 .main-container {
214 display: grid;
215 grid-template-columns: 1fr 380px;
216 gap: 0;
217 height: calc(100vh - 130px);
218 min-height: 600px;
219 }
220
221 /* ---- DAG panel ---- */
222 .dag-panel {
223 border-right: 1px solid var(--border);
224 display: flex;
225 flex-direction: column;
226 overflow: hidden;
227 }
228 .dag-header {
229 display: flex;
230 align-items: center;
231 gap: 12px;
232 padding: 12px 20px;
233 border-bottom: 1px solid var(--border);
234 background: var(--bg2);
235 flex-shrink: 0;
236 }
237 .dag-header h2 {
238 font-size: 13px;
239 font-weight: 600;
240 color: var(--text-mute);
241 text-transform: uppercase;
242 letter-spacing: 0.8px;
243 }
244 .controls { display: flex; gap: 8px; margin-left: auto; align-items: center; }
245 .btn {
246 padding: 6px 14px;
247 border-radius: var(--radius);
248 border: 1px solid var(--border);
249 background: var(--bg3);
250 color: var(--text);
251 cursor: pointer;
252 font-size: 12px;
253 font-family: var(--font-ui);
254 transition: all 0.15s;
255 }
256 .btn:hover { background: var(--border); }
257 .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
258 .btn.primary:hover { background: var(--accent2); }
259 .btn:disabled { opacity: 0.35; cursor: not-allowed; }
260 .btn:disabled:hover { background: var(--bg3); }
261 .step-counter {
262 font-size: 11px;
263 font-family: var(--font-mono);
264 color: var(--text-mute);
265 min-width: 80px;
266 text-align: right;
267 }
268 .dag-scroll {
269 flex: 1;
270 overflow: auto;
271 padding: 20px;
272 }
273 #dag-svg { display: block; }
274 .branch-legend {
275 display: flex;
276 flex-wrap: wrap;
277 gap: 10px;
278 padding: 8px 20px;
279 border-top: 1px solid var(--border);
280 background: var(--bg2);
281 flex-shrink: 0;
282 }
283 .legend-item {
284 display: flex;
285 align-items: center;
286 gap: 6px;
287 font-size: 11px;
288 color: var(--text-mute);
289 }
290 .legend-dot {
291 width: 10px;
292 height: 10px;
293 border-radius: 50%;
294 flex-shrink: 0;
295 }
296
297 /* ---- Log panel ---- */
298 .log-panel {
299 display: flex;
300 flex-direction: column;
301 overflow: hidden;
302 background: var(--bg);
303 }
304 .log-header {
305 padding: 12px 16px;
306 border-bottom: 1px solid var(--border);
307 background: var(--bg2);
308 flex-shrink: 0;
309 }
310 .log-header h2 {
311 font-size: 13px;
312 font-weight: 600;
313 color: var(--text-mute);
314 text-transform: uppercase;
315 letter-spacing: 0.8px;
316 }
317 .log-scroll {
318 flex: 1;
319 overflow-y: auto;
320 padding: 0;
321 }
322 .act-header {
323 padding: 10px 16px 6px;
324 font-size: 11px;
325 font-weight: 700;
326 text-transform: uppercase;
327 letter-spacing: 1px;
328 color: var(--text-dim);
329 border-top: 1px solid var(--border);
330 margin-top: 4px;
331 position: sticky;
332 top: 0;
333 background: var(--bg);
334 z-index: 1;
335 }
336 .act-header:first-child { border-top: none; margin-top: 0; }
337 .event-item {
338 padding: 8px 16px;
339 border-bottom: 1px solid #1a1f26;
340 opacity: 0.3;
341 transition: opacity 0.3s, background 0.2s;
342 cursor: default;
343 }
344 .event-item.revealed { opacity: 1; }
345 .event-item.active { background: rgba(79,142,247,0.08); border-left: 2px solid var(--accent); }
346 .event-item.failed { border-left: 2px solid var(--red); }
347 .event-cmd {
348 font-family: var(--font-mono);
349 font-size: 12px;
350 color: var(--text);
351 margin-bottom: 3px;
352 }
353 .event-cmd .cmd-prefix { color: var(--text-dim); }
354 .event-cmd .cmd-name { color: var(--accent2); font-weight: 600; }
355 .event-cmd .cmd-args { color: var(--text); }
356 .event-output {
357 font-family: var(--font-mono);
358 font-size: 11px;
359 color: var(--text-mute);
360 white-space: pre-wrap;
361 word-break: break-all;
362 max-height: 80px;
363 overflow: hidden;
364 text-overflow: ellipsis;
365 }
366 .event-output.conflict { color: var(--red); }
367 .event-output.success { color: var(--green); }
368 .event-item.rich-act .event-output { max-height: 220px; }
369
370 /* ---- Act jump bar ---- */
371 .act-jump-bar {
372 display: flex;
373 flex-wrap: wrap;
374 gap: 4px;
375 padding: 6px 12px;
376 border-bottom: 1px solid var(--border);
377 background: var(--bg2);
378 flex-shrink: 0;
379 }
380 .act-jump-bar span {
381 font-size: 10px;
382 color: var(--text-dim);
383 align-self: center;
384 margin-right: 4px;
385 font-weight: 600;
386 text-transform: uppercase;
387 letter-spacing: 0.6px;
388 }
389 .act-jump-btn {
390 font-size: 10px;
391 padding: 2px 8px;
392 border-radius: 4px;
393 background: var(--bg3);
394 border: 1px solid var(--border);
395 color: var(--text-mute);
396 cursor: pointer;
397 font-family: var(--font-mono);
398 transition: background 0.15s, color 0.15s;
399 }
400 .act-jump-btn:hover { background: var(--bg); color: var(--accent); border-color: var(--accent); }
401 .act-jump-btn.reveal-all { border-color: var(--green); color: var(--green); }
402 .act-jump-btn.reveal-all:hover { background: rgba(63,185,80,0.08); }
403
404 .event-meta {
405 display: flex;
406 gap: 8px;
407 margin-top: 3px;
408 font-size: 10px;
409 color: var(--text-dim);
410 }
411 .tag-commit { background: rgba(79,142,247,0.15); color: var(--accent2); padding: 1px 5px; border-radius: 3px; font-family: var(--font-mono); }
412 .tag-time { color: var(--text-dim); }
413
414 /* ---- DAG SVG styles ---- */
415 .commit-node { cursor: pointer; }
416 .commit-node:hover circle { filter: brightness(1.3); }
417 .commit-node.highlighted circle { filter: brightness(1.5) drop-shadow(0 0 6px currentColor); }
418 .commit-label { font-size: 10px; fill: var(--text-mute); font-family: var(--font-mono); }
419 .commit-msg { font-size: 10px; fill: var(--text-mute); }
420 .commit-node.highlighted .commit-label,
421 .commit-node.highlighted .commit-msg { fill: var(--text); }
422 text { font-family: -apple-system, system-ui, sans-serif; }
423
424 /* ---- Registry callout ---- */
425 .registry-callout {
426 background: var(--bg2);
427 border-top: 1px solid var(--border);
428 padding: 40px;
429 }
430 .registry-callout-inner {
431 max-width: 1100px;
432 margin: 0 auto;
433 display: flex;
434 align-items: center;
435 gap: 32px;
436 flex-wrap: wrap;
437 }
438 .registry-callout-text { flex: 1; min-width: 200px; }
439 .registry-callout-title {
440 font-size: 16px;
441 font-weight: 700;
442 color: var(--text);
443 margin-bottom: 6px;
444 }
445 .registry-callout-sub {
446 font-size: 13px;
447 color: var(--text-mute);
448 line-height: 1.6;
449 }
450 .registry-callout-btn {
451 flex-shrink: 0;
452 display: inline-block;
453 padding: 10px 22px;
454 background: var(--accent);
455 color: #fff;
456 font-size: 13px;
457 font-weight: 600;
458 border-radius: var(--radius);
459 text-decoration: none;
460 transition: opacity 0.15s;
461 }
462 .registry-callout-btn:hover { opacity: 0.85; }
463
464 /* ---- Domain Dashboard section ---- */
465 .domain-section {
466 background: var(--bg);
467 border-top: 1px solid var(--border);
468 padding: 60px 40px;
469 }
470 .domain-inner { max-width: 1100px; margin: 0 auto; }
471 .domain-section h2, .crdt-section h2 {
472 font-size: 22px;
473 font-weight: 700;
474 margin-bottom: 8px;
475 color: var(--text);
476 }
477 .domain-section .section-intro, .crdt-section .section-intro {
478 color: var(--text-mute);
479 max-width: 680px;
480 margin-bottom: 36px;
481 line-height: 1.7;
482 }
483 .domain-section .section-intro strong, .crdt-section .section-intro strong { color: var(--text); }
484 .domain-grid {
485 display: grid;
486 grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
487 gap: 20px;
488 }
489 .domain-card {
490 border: 1px solid var(--border);
491 border-radius: var(--radius);
492 background: var(--bg2);
493 overflow: hidden;
494 transition: border-color 0.2s;
495 }
496 .domain-card:hover { border-color: var(--accent); }
497 .domain-card.active-domain { border-color: rgba(249,168,37,0.5); }
498 .domain-card.scaffold-domain { border-style: dashed; opacity: 0.85; }
499 .domain-card-header {
500 padding: 14px 16px;
501 border-bottom: 1px solid var(--border);
502 display: flex;
503 align-items: center;
504 gap: 10px;
505 background: var(--bg3);
506 }
507 .domain-badge {
508 font-family: var(--font-mono);
509 font-size: 11px;
510 padding: 2px 8px;
511 border-radius: 4px;
512 background: rgba(79,142,247,0.12);
513 border: 1px solid rgba(79,142,247,0.3);
514 color: var(--accent2);
515 }
516 .domain-badge.active { background: rgba(249,168,37,0.12); border-color: rgba(249,168,37,0.4); color: #f9a825; }
517 .domain-name {
518 font-weight: 700;
519 font-size: 15px;
520 font-family: var(--font-mono);
521 color: var(--text);
522 }
523 .domain-active-dot {
524 margin-left: auto;
525 width: 8px;
526 height: 8px;
527 border-radius: 50%;
528 background: var(--green);
529 }
530 .domain-card-body { padding: 14px 16px; }
531 .domain-desc {
532 font-size: 13px;
533 color: var(--text-mute);
534 margin-bottom: 12px;
535 line-height: 1.5;
536 }
537 .domain-caps {
538 display: flex;
539 flex-wrap: wrap;
540 gap: 6px;
541 margin-bottom: 12px;
542 }
543 .cap-pill {
544 font-size: 10px;
545 padding: 2px 8px;
546 border-radius: 12px;
547 border: 1px solid var(--border);
548 color: var(--text-mute);
549 background: var(--bg3);
550 }
551 .cap-pill.cap-crdt { border-color: rgba(188,140,255,0.4); color: var(--purple); background: rgba(188,140,255,0.08); }
552 .cap-pill.cap-ot { border-color: rgba(88,166,255,0.4); color: var(--accent2); background: rgba(88,166,255,0.08); }
553 .cap-pill.cap-schema { border-color: rgba(63,185,80,0.4); color: var(--green); background: rgba(63,185,80,0.08); }
554 .cap-pill.cap-delta { border-color: rgba(249,168,37,0.4); color: #f9a825; background: rgba(249,168,37,0.08); }
555 .domain-dims {
556 font-size: 11px;
557 color: var(--text-dim);
558 }
559 .domain-dims strong { color: var(--text-mute); }
560 .domain-new-card {
561 border: 2px dashed var(--border);
562 border-radius: var(--radius);
563 background: transparent;
564 display: flex;
565 flex-direction: column;
566 align-items: center;
567 justify-content: center;
568 padding: 32px 20px;
569 text-align: center;
570 gap: 12px;
571 transition: border-color 0.2s;
572 cursor: default;
573 }
574 .domain-new-card:hover { border-color: var(--accent); }
575 .domain-new-icon { font-size: 28px; color: var(--text-dim); }
576 .domain-new-title { font-size: 14px; font-weight: 600; color: var(--text-mute); }
577 .domain-new-cmd {
578 font-family: var(--font-mono);
579 font-size: 12px;
580 background: var(--bg3);
581 border: 1px solid var(--border);
582 border-radius: 4px;
583 padding: 6px 12px;
584 color: var(--accent2);
585 }
586 .domain-new-link {
587 font-size: 11px;
588 color: var(--text-dim);
589 }
590 .domain-new-link a { color: var(--accent); text-decoration: none; }
591 .domain-new-link a:hover { text-decoration: underline; }
592
593 /* ---- CRDT Primitives section ---- */
594 .crdt-section {
595 background: var(--bg2);
596 border-top: 1px solid var(--border);
597 padding: 60px 40px;
598 }
599 .crdt-inner { max-width: 1100px; margin: 0 auto; }
600 .crdt-grid {
601 display: grid;
602 grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
603 gap: 20px;
604 }
605 .crdt-card {
606 border: 1px solid var(--border);
607 border-radius: var(--radius);
608 background: var(--bg);
609 overflow: hidden;
610 transition: border-color 0.2s;
611 }
612 .crdt-card:hover { border-color: var(--purple); }
613 .crdt-card-header {
614 padding: 12px 16px;
615 border-bottom: 1px solid var(--border);
616 background: rgba(188,140,255,0.06);
617 display: flex;
618 align-items: center;
619 gap: 8px;
620 }
621 .crdt-type-badge {
622 font-family: var(--font-mono);
623 font-size: 11px;
624 padding: 2px 8px;
625 border-radius: 4px;
626 background: rgba(188,140,255,0.12);
627 border: 1px solid rgba(188,140,255,0.3);
628 color: var(--purple);
629 }
630 .crdt-card-title { font-weight: 700; font-size: 14px; color: var(--text); }
631 .crdt-card-sub { font-size: 11px; color: var(--text-mute); }
632 .crdt-card-body { padding: 14px 16px; }
633 .crdt-output {
634 font-family: var(--font-mono);
635 font-size: 11px;
636 color: var(--text-mute);
637 white-space: pre-wrap;
638 line-height: 1.6;
639 background: var(--bg3);
640 border: 1px solid var(--border);
641 border-radius: 4px;
642 padding: 10px 12px;
643 }
644 .crdt-output .out-win { color: var(--green); }
645 .crdt-output .out-key { color: var(--accent2); }
646
647 /* ---- Architecture section ---- */
648 .arch-section {
649 background: var(--bg2);
650 border-top: 1px solid var(--border);
651 padding: 48px 40px;
652 }
653 .arch-inner { max-width: 1100px; margin: 0 auto; }
654 .arch-section h2 {
655 font-size: 22px;
656 font-weight: 700;
657 margin-bottom: 8px;
658 color: var(--text);
659 }
660 .arch-section .section-intro {
661 color: var(--text-mute);
662 max-width: 680px;
663 margin-bottom: 40px;
664 line-height: 1.7;
665 }
666 .arch-section .section-intro strong { color: var(--text); }
667 .arch-content {
668 display: grid;
669 grid-template-columns: 380px 1fr;
670 gap: 48px;
671 align-items: start;
672 }
673
674 /* Architecture flow diagram */
675 .arch-flow {
676 display: flex;
677 flex-direction: column;
678 align-items: center;
679 gap: 0;
680 }
681 .arch-row { width: 100%; display: flex; justify-content: center; }
682 .plugins-row { gap: 8px; flex-wrap: wrap; }
683 .arch-box {
684 border: 1px solid var(--border);
685 border-radius: var(--radius);
686 padding: 12px 16px;
687 background: var(--bg3);
688 width: 100%;
689 max-width: 340px;
690 transition: border-color 0.2s;
691 }
692 .arch-box:hover { border-color: var(--accent); }
693 .arch-box.cli { border-color: rgba(79,142,247,0.4); }
694 .arch-box.registry { border-color: rgba(188,140,255,0.3); }
695 .arch-box.core { border-color: rgba(63,185,80,0.3); background: rgba(63,185,80,0.05); }
696 .arch-box.protocol { border-color: rgba(79,142,247,0.5); background: rgba(79,142,247,0.05); }
697 .arch-box.plugin { max-width: 160px; width: auto; flex: 1; }
698 .arch-box.plugin.active { border-color: rgba(249,168,37,0.5); background: rgba(249,168,37,0.05); }
699 .arch-box.plugin.planned { opacity: 0.6; border-style: dashed; }
700 .box-title { font-weight: 600; font-size: 13px; color: var(--text); }
701 .box-sub { font-size: 11px; color: var(--text-mute); margin-top: 3px; }
702 .box-detail { font-size: 10px; color: var(--text-dim); margin-top: 4px; line-height: 1.5; }
703 .arch-connector {
704 display: flex;
705 flex-direction: column;
706 align-items: center;
707 height: 24px;
708 color: var(--border);
709 }
710 .connector-line { width: 1px; flex: 1; background: var(--border); }
711 .connector-arrow { font-size: 10px; }
712
713 /* Protocol table */
714 .protocol-table { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
715 .proto-row {
716 display: grid;
717 grid-template-columns: 80px 220px 1fr;
718 gap: 0;
719 border-bottom: 1px solid var(--border);
720 }
721 .proto-row:last-child { border-bottom: none; }
722 .proto-row.header { background: var(--bg3); }
723 .proto-row > div { padding: 10px 14px; }
724 .proto-method {
725 font-family: var(--font-mono);
726 font-size: 12px;
727 color: var(--accent2);
728 font-weight: 600;
729 border-right: 1px solid var(--border);
730 }
731 .proto-sig {
732 font-family: var(--font-mono);
733 font-size: 11px;
734 color: var(--text-mute);
735 border-right: 1px solid var(--border);
736 word-break: break-all;
737 }
738 .proto-desc { font-size: 12px; color: var(--text-mute); }
739 .proto-row.header .proto-method,
740 .proto-row.header .proto-sig,
741 .proto-row.header .proto-desc {
742 font-family: var(--font-ui);
743 font-size: 11px;
744 font-weight: 700;
745 text-transform: uppercase;
746 letter-spacing: 0.6px;
747 color: var(--text-dim);
748 }
749
750 /* ---- Footer ---- */
751 footer {
752 background: var(--bg);
753 border-top: 1px solid var(--border);
754 padding: 16px 40px;
755 display: flex;
756 justify-content: space-between;
757 align-items: center;
758 font-size: 12px;
759 color: var(--text-dim);
760 }
761 footer a { color: var(--accent2); text-decoration: none; }
762 footer a:hover { text-decoration: underline; }
763
764 /* ---- Scrollbar ---- */
765 ::-webkit-scrollbar { width: 6px; height: 6px; }
766 ::-webkit-scrollbar-track { background: var(--bg); }
767 ::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
768 ::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
769
770 /* ---- Tooltip ---- */
771 .tooltip {
772 position: fixed;
773 background: var(--bg2);
774 border: 1px solid var(--border);
775 border-radius: var(--radius);
776 padding: 10px 14px;
777 font-size: 12px;
778 pointer-events: none;
779 opacity: 0;
780 transition: opacity 0.15s;
781 z-index: 100;
782 max-width: 280px;
783 box-shadow: 0 8px 24px rgba(0,0,0,0.4);
784 }
785 .tooltip.visible { opacity: 1; }
786 .tip-id { font-family: var(--font-mono); font-size: 11px; color: var(--accent2); margin-bottom: 4px; }
787 .tip-msg { color: var(--text); margin-bottom: 4px; }
788 .tip-branch { font-size: 11px; margin-bottom: 4px; }
789 .tip-files { font-size: 11px; color: var(--text-mute); font-family: var(--font-mono); }
790
791 /* ---- Dimension dots on DAG nodes ---- */
792 .dim-dots { pointer-events: none; }
793
794 /* ---- Dimension State Matrix section ---- */
795 .dim-section {
796 background: var(--bg);
797 border-top: 2px solid var(--border);
798 padding: 28px 40px 32px;
799 }
800 .dim-inner { max-width: 1200px; margin: 0 auto; }
801 .dim-section-header { display:flex; align-items:baseline; gap:14px; margin-bottom:6px; }
802 .dim-section h2 { font-size:16px; font-weight:700; color:var(--text); }
803 .dim-section .dim-tagline { font-size:12px; color:var(--text-mute); }
804 .dim-matrix-wrap { overflow-x:auto; margin-top:18px; padding-bottom:4px; }
805 .dim-matrix { display:table; border-collapse:separate; border-spacing:0; min-width:100%; }
806 .dim-matrix-row { display:table-row; }
807 .dim-label-cell {
808 display:table-cell; padding:6px 14px 6px 0;
809 font-size:11px; font-weight:600; color:var(--text-mute);
810 text-transform:uppercase; letter-spacing:0.6px;
811 white-space:nowrap; vertical-align:middle; min-width:100px;
812 }
813 .dim-label-dot { display:inline-block; width:9px; height:9px; border-radius:50%; margin-right:6px; vertical-align:middle; }
814 .dim-cell { display:table-cell; padding:4px 3px; vertical-align:middle; text-align:center; min-width:46px; }
815 .dim-cell-inner {
816 width:38px; height:28px; border-radius:5px; margin:0 auto;
817 display:flex; align-items:center; justify-content:center;
818 font-size:11px; font-weight:700;
819 transition:transform 0.2s, box-shadow 0.2s;
820 cursor:default;
821 background:var(--bg3); border:1px solid transparent; color:transparent;
822 }
823 .dim-cell-inner.active { border-color:currentColor; }
824 .dim-cell-inner.conflict-dim { box-shadow:0 0 0 2px #f85149; }
825 .dim-cell-inner.col-highlight { transform:scaleY(1.12); box-shadow:0 0 14px 2px rgba(255,255,255,0.12); }
826 .dim-commit-cell {
827 display:table-cell; padding:8px 3px 0; text-align:center;
828 font-size:9px; font-family:var(--font-mono); color:var(--text-dim);
829 vertical-align:top; transition:color 0.2s;
830 }
831 .dim-commit-cell.col-highlight { color:var(--accent2); font-weight:700; }
832 .dim-commit-label { display:table-cell; padding-top:10px; vertical-align:top; }
833 .dim-legend { display:flex; gap:18px; margin-top:18px; flex-wrap:wrap; font-size:11px; color:var(--text-mute); }
834 .dim-legend-item { display:flex; align-items:center; gap:6px; }
835 .dim-legend-swatch { width:22px; height:14px; border-radius:3px; border:1px solid currentColor; display:inline-block; }
836 .dim-conflict-note {
837 margin-top:16px; padding:12px 16px;
838 background:rgba(248,81,73,0.08); border:1px solid rgba(248,81,73,0.25);
839 border-radius:6px; font-size:12px; color:var(--text-mute);
840 }
841 .dim-conflict-note strong { color:var(--red); }
842 .dim-conflict-note em { color:var(--green); font-style:normal; }
843
844 /* ---- Dimension pills in the operation log ---- */
845 .dim-pills { display:flex; flex-wrap:wrap; gap:3px; margin-top:4px; }
846 .dim-pill {
847 display:inline-block; padding:1px 6px; border-radius:10px;
848 font-size:9px; font-weight:700; letter-spacing:0.4px; text-transform:uppercase;
849 border:1px solid currentColor; opacity:0.85;
850 }
851 .dim-pill.conflict-pill { background:rgba(248,81,73,0.2); color:var(--red) !important; }
852
853 /* ---- inline SVG icons ---- */
854 .ico-inline {
855 width: 13px; height: 13px;
856 display: inline-block; vertical-align: -0.15em;
857 flex-shrink: 0;
858 }
859 .ico-conflict { color: #f85149; }
860 .ico-check { color: #3fb950; }
861 .ico { width: 1em; height: 1em; display: inline-block; vertical-align: -0.15em; flex-shrink: 0; }
862
863 /* ---- shared nav ---- */
864 nav {
865 background: var(--header-bg);
866 border-bottom: 1px solid rgba(255,255,255,0.08);
867 padding: 0 40px;
868 display: flex;
869 align-items: center;
870 gap: 0;
871 height: 52px;
872 position: sticky;
873 top: 0;
874 z-index: 100;
875 }
876 .nav-logo {
877 font-family: var(--mono);
878 font-size: 16px;
879 font-weight: 700;
880 color: #6ea8fe;
881 margin-right: 32px;
882 text-decoration: none;
883 }
884 .nav-logo:hover { text-decoration: none; }
885 .nav-link {
886 font-size: 13px;
887 color: rgba(255,255,255,0.45);
888 padding: 0 14px;
889 height: 100%;
890 display: flex;
891 align-items: center;
892 border-bottom: 2px solid transparent;
893 text-decoration: none;
894 transition: color 0.15s, border-color 0.15s;
895 }
896 .nav-link:hover { color: #e6edf3; text-decoration: none; }
897 .nav-link.current { color: #e6edf3; border-bottom-color: #6ea8fe; }
898 .nav-spacer { flex: 1; }
899 .nav-badge {
900 font-size: 11px;
901 background: rgba(79,142,247,0.12);
902 border: 1px solid rgba(79,142,247,0.3);
903 color: #6ea8fe;
904 border-radius: 4px;
905 padding: 2px 8px;
906 font-family: var(--mono);
907 }
908 </style>
909 </head>
910 <body>
911
912 <nav>
913 <a class="nav-logo" href="index.html">muse</a>
914 <a class="nav-link current" href="demo.html">Demo</a>
915 <a class="nav-link" href="https://github.com/cgcardona/muse/blob/main/docs/guide/plugin-authoring-guide.md">Plugin Guide</a>
916 <div class="nav-spacer"></div>
917 <span class="nav-badge">v{{VERSION}}</span>
918 </nav>
919
920 <header>
921 <div class="stats-bar">
922 <div class="stat"><span class="stat-num">{{COMMITS}}</span><span class="stat-label">Commits</span></div>
923 <div class="stat-sep">·</div>
924 <div class="stat"><span class="stat-num">{{BRANCHES}}</span><span class="stat-label">Branches</span></div>
925 <div class="stat-sep">·</div>
926 <div class="stat"><span class="stat-num">{{MERGES}}</span><span class="stat-label">Merges</span></div>
927 <div class="stat-sep">·</div>
928 <div class="stat"><span class="stat-num">{{CONFLICTS}}</span><span class="stat-label">Conflicts Resolved</span></div>
929 <div class="stat-sep">·</div>
930 <div class="stat"><span class="stat-num">{{OPS}}</span><span class="stat-label">Operations</span></div>
931 </div>
932 </header>
933
934 <div class="main-container">
935 <div class="dag-panel">
936 <div class="dag-header">
937 <h2>Commit Graph</h2>
938 <div class="controls">
939 <button class="btn primary" id="btn-play">&#9654; Play Tour</button>
940 <button class="btn" id="btn-prev" title="Previous step (←)">&#9664;</button>
941 <button class="btn" id="btn-next" title="Next step (→)">&#9654;</button>
942 <button class="btn" id="btn-reset">&#8635; Reset</button>
943 <span class="step-counter" id="step-counter"></span>
944 </div>
945 </div>
946 <div class="dag-scroll" id="dag-scroll">
947 <svg id="dag-svg"></svg>
948 </div>
949 <div class="branch-legend" id="branch-legend"></div>
950 </div>
951
952 <div class="log-panel">
953 <div class="log-header"><h2>Operation Log</h2></div>
954 <div class="act-jump-bar" id="act-jump-bar"></div>
955 <div class="log-scroll" id="log-scroll">
956 <div id="event-list"></div>
957 </div>
958 </div>
959 </div>
960
961
962 <div class="dim-section">
963 <div class="dim-inner">
964 <div class="dim-section-header">
965 <h2>Dimension State Matrix</h2>
966 <span class="dim-tagline">
967 Unlike Git (binary file conflicts), Muse merges each orthogonal dimension independently —
968 only conflicting dimensions require human resolution.
969 </span>
970 </div>
971 <div class="dim-matrix-wrap">
972 <div class="dim-matrix" id="dim-matrix"></div>
973 </div>
974 <div class="dim-legend">
975 <div class="dim-legend-item"><span class="dim-legend-swatch" style="background:rgba(188,140,255,0.35);color:#bc8cff"></span> Melodic</div>
976 <div class="dim-legend-item"><span class="dim-legend-swatch" style="background:rgba(63,185,80,0.35);color:#3fb950"></span> Rhythmic</div>
977 <div class="dim-legend-item"><span class="dim-legend-swatch" style="background:rgba(88,166,255,0.35);color:#58a6ff"></span> Harmonic</div>
978 <div class="dim-legend-item"><span class="dim-legend-swatch" style="background:rgba(249,168,37,0.35);color:#f9a825"></span> Dynamic</div>
979 <div class="dim-legend-item"><span class="dim-legend-swatch" style="background:rgba(239,83,80,0.35);color:#ef5350"></span> Structural</div>
980 <div class="dim-legend-item" style="margin-left:8px"><span style="display:inline-block;width:22px;height:14px;border-radius:3px;border:2px solid #f85149;vertical-align:middle;margin-right:6px"></span> Conflict (required resolution)</div>
981 <div class="dim-legend-item"><span style="display:inline-block;width:22px;height:14px;border-radius:3px;background:var(--bg3);border:1px solid var(--border);vertical-align:middle;margin-right:6px"></span> Unchanged</div>
982 </div>
983 <div class="dim-conflict-note">
984 <strong><svg class="ico-inline ico-conflict" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg> Merge conflict (shared-state.mid)</strong> — shared-state.mid had both-sides changes in
985 <strong style="color:#ef5350">structural</strong> (manual resolution required).
986 <em><svg class="ico-inline ico-check" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg> melodic auto-merged from left</em> · <em><svg class="ico-inline ico-check" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg> harmonic auto-merged from right</em> —
987 only 1 of 5 dimensions conflicted. Git would have flagged the entire file as a conflict.
988 </div>
989 </div>
990 </div>
991
992 <footer>
993 <span>Generated {{GENERATED_AT}} · {{ELAPSED}}s · {{OPS}} operations</span>
994 <span><a href="https://github.com/cgcardona/muse">github.com/cgcardona/muse</a></span>
995 </footer>
996
997 <div class="tooltip" id="tooltip">
998 <div class="tip-id" id="tip-id"></div>
999 <div class="tip-msg" id="tip-msg"></div>
1000 <div class="tip-branch" id="tip-branch"></div>
1001 <div class="tip-files" id="tip-files"></div>
1002 <div id="tip-dims" style="margin-top:6px;font-size:10px;line-height:1.8"></div>
1003 </div>
1004
1005 {{D3_SCRIPT}}
1006
1007 <script>
1008 /* ===== Embedded tour data ===== */
1009 const DATA = {{DATA_JSON}};
1010
1011 /* ===== Constants ===== */
1012 const ROW_H = 62;
1013 const COL_W = 90;
1014 const PAD = { top: 30, left: 55, right: 160 };
1015 const R_NODE = 11;
1016 const BRANCH_ORDER = ['main','alpha','beta','gamma','conflict/left','conflict/right'];
1017 const PLAY_INTERVAL_MS = 1200;
1018
1019 /* ===== Dimension data ===== */
1020 const DIM_COLORS = {
1021 melodic: '#bc8cff',
1022 rhythmic: '#3fb950',
1023 harmonic: '#58a6ff',
1024 dynamic: '#f9a825',
1025 structural: '#ef5350',
1026 };
1027 const DIMS = ['melodic','rhythmic','harmonic','dynamic','structural'];
1028
1029 // Commit message → dimension mapping (stable across re-runs, independent of hash)
1030 function getDims(commit) {
1031 const m = (commit.message || '').toLowerCase();
1032 if (m.includes('root') || m.includes('initial state'))
1033 return ['melodic','rhythmic','harmonic','dynamic','structural'];
1034 if (m.includes('layer 1') || m.includes('rhythmic dimension'))
1035 return ['rhythmic','structural'];
1036 if (m.includes('layer 2') || m.includes('harmonic dimension'))
1037 return ['harmonic','structural'];
1038 if (m.includes('texture pattern a') || m.includes('sparse'))
1039 return ['melodic','rhythmic'];
1040 if (m.includes('texture pattern b') || m.includes('dense'))
1041 return ['melodic','dynamic'];
1042 if (m.includes('syncopated'))
1043 return ['rhythmic','dynamic'];
1044 if (m.includes('descending'))
1045 return ['melodic','harmonic'];
1046 if (m.includes('ascending'))
1047 return ['melodic'];
1048 if (m.includes("merge branch 'beta'"))
1049 return ['rhythmic','dynamic'];
1050 if (m.includes('left:') || m.includes('version a'))
1051 return ['melodic','structural'];
1052 if (m.includes('right:') || m.includes('version b'))
1053 return ['harmonic','structural'];
1054 if (m.includes('resolve') || m.includes('reconciled'))
1055 return ['structural'];
1056 if (m.includes('cherry-pick') || m.includes('cherry pick'))
1057 return ['melodic'];
1058 if (m.includes('revert'))
1059 return ['melodic'];
1060 return [];
1061 }
1062
1063 function getConflicts(commit) {
1064 const m = (commit.message || '').toLowerCase();
1065 if (m.includes('resolve') && m.includes('reconciled')) return ['structural'];
1066 return [];
1067 }
1068
1069 // Build per-short-ID lookup tables once the DATA is available (populated at init)
1070 const DIM_DATA = {};
1071 const DIM_CONFLICTS = {};
1072 function _initDimMaps() {
1073 DATA.dag.commits.forEach(c => {
1074 DIM_DATA[c.short] = getDims(c);
1075 DIM_CONFLICTS[c.short] = getConflicts(c);
1076 });
1077 // Also key by the short prefix used in events (some may be truncated)
1078 DATA.events.forEach(ev => {
1079 if (ev.commit_id && !DIM_DATA[ev.commit_id]) {
1080 const full = DATA.dag.commits.find(c => c.short.startsWith(ev.commit_id) || ev.commit_id.startsWith(c.short));
1081 if (full) {
1082 DIM_DATA[ev.commit_id] = getDims(full);
1083 DIM_CONFLICTS[ev.commit_id] = getConflicts(full);
1084 }
1085 }
1086 });
1087 }
1088
1089
1090 /* ===== State ===== */
1091 let currentStep = -1;
1092 let isPlaying = false;
1093 let playTimer = null;
1094
1095 /* ===== Utilities ===== */
1096 function escHtml(s) {
1097 return String(s)
1098 .replace(/&/g,'&amp;')
1099 .replace(/</g,'&lt;')
1100 .replace(/>/g,'&gt;')
1101 .replace(/"/g,'&quot;');
1102 }
1103
1104 /* ===== Topological sort ===== */
1105 function topoSort(commits) {
1106 const map = new Map(commits.map(c => [c.id, c]));
1107 const visited = new Set();
1108 const result = [];
1109 function visit(id) {
1110 if (visited.has(id)) return;
1111 visited.add(id);
1112 const c = map.get(id);
1113 if (!c) return;
1114 (c.parents || []).forEach(pid => visit(pid));
1115 result.push(c);
1116 }
1117 commits.forEach(c => visit(c.id));
1118 // Oldest commit at row 0 (top of DAG); newest at the bottom so the DAG
1119 // scrolls down in sync with the operation log during playback.
1120 return result;
1121 }
1122
1123 /* ===== Layout ===== */
1124 function computeLayout(commits) {
1125 const sorted = topoSort(commits);
1126 const branchCols = {};
1127 let nextCol = 0;
1128 // Assign columns in BRANCH_ORDER first, then any extras
1129 BRANCH_ORDER.forEach(b => { branchCols[b] = nextCol++; });
1130 commits.forEach(c => {
1131 if (!(c.branch in branchCols)) branchCols[c.branch] = nextCol++;
1132 });
1133 const numCols = nextCol;
1134 const positions = new Map();
1135 sorted.forEach((c, i) => {
1136 positions.set(c.id, {
1137 x: PAD.left + (branchCols[c.branch] || 0) * COL_W,
1138 y: PAD.top + i * ROW_H,
1139 row: i,
1140 col: branchCols[c.branch] || 0,
1141 });
1142 });
1143 const svgW = PAD.left + numCols * COL_W + PAD.right;
1144 const svgH = PAD.top + sorted.length * ROW_H + PAD.top;
1145 return { sorted, positions, branchCols, svgW, svgH };
1146 }
1147
1148 /* ===== Draw DAG ===== */
1149 function drawDAG() {
1150 const { dag, dag: { commits, branches } } = DATA;
1151 if (!commits.length) return;
1152
1153 const layout = computeLayout(commits);
1154 const { sorted, positions, svgW, svgH } = layout;
1155 const branchColor = new Map(branches.map(b => [b.name, b.color]));
1156 const commitMap = new Map(commits.map(c => [c.id, c]));
1157
1158 const svg = d3.select('#dag-svg')
1159 .attr('width', svgW)
1160 .attr('height', svgH);
1161
1162 // ---- Edges ----
1163 const edgeG = svg.append('g').attr('class', 'edges');
1164 sorted.forEach(commit => {
1165 const pos = positions.get(commit.id);
1166 (commit.parents || []).forEach((pid, pIdx) => {
1167 const ppos = positions.get(pid);
1168 if (!pos || !ppos) return;
1169 const color = pIdx === 0
1170 ? (branchColor.get(commit.branch) || '#555')
1171 : (branchColor.get(commitMap.get(pid)?.branch || '') || '#555');
1172
1173 let pathStr;
1174 if (Math.abs(pos.x - ppos.x) < 4) {
1175 // Same column → straight line
1176 pathStr = `M${pos.x},${pos.y} L${ppos.x},${ppos.y}`;
1177 } else {
1178 // Different columns → S-curve bezier
1179 const mid = (pos.y + ppos.y) / 2;
1180 pathStr = `M${pos.x},${pos.y} C${pos.x},${mid} ${ppos.x},${mid} ${ppos.x},${ppos.y}`;
1181 }
1182 edgeG.append('path')
1183 .attr('d', pathStr)
1184 .attr('stroke', color)
1185 .attr('stroke-width', 1.8)
1186 .attr('fill', 'none')
1187 .attr('opacity', 0.45)
1188 .attr('class', `edge-from-${commit.id.slice(0,8)}`);
1189 });
1190 });
1191
1192 // ---- Nodes ----
1193 const nodeG = svg.append('g').attr('class', 'nodes');
1194 const tooltip = document.getElementById('tooltip');
1195
1196 sorted.forEach(commit => {
1197 const pos = positions.get(commit.id);
1198 if (!pos) return;
1199 const color = branchColor.get(commit.branch) || '#78909c';
1200 const isMerge = (commit.parents || []).length >= 2;
1201
1202 const g = nodeG.append('g')
1203 .attr('class', 'commit-node')
1204 .attr('data-id', commit.id)
1205 .attr('data-short', commit.short)
1206 .attr('transform', `translate(${pos.x},${pos.y})`);
1207
1208 if (isMerge) {
1209 g.append('circle')
1210 .attr('r', R_NODE + 6)
1211 .attr('fill', 'none')
1212 .attr('stroke', color)
1213 .attr('stroke-width', 1.5)
1214 .attr('opacity', 0.35);
1215 }
1216
1217 g.append('circle')
1218 .attr('r', R_NODE)
1219 .attr('fill', color)
1220 .attr('stroke', '#0d1117')
1221 .attr('stroke-width', 2);
1222
1223 // Short ID
1224 g.append('text')
1225 .attr('x', R_NODE + 7)
1226 .attr('y', 0)
1227 .attr('dy', '0.35em')
1228 .attr('class', 'commit-label')
1229 .text(commit.short);
1230
1231 // Message (truncated)
1232 const maxLen = 38;
1233 const msg = commit.message.length > maxLen
1234 ? commit.message.slice(0, maxLen) + '…'
1235 : commit.message;
1236 g.append('text')
1237 .attr('x', R_NODE + 7)
1238 .attr('y', 13)
1239 .attr('class', 'commit-msg')
1240 .text(msg);
1241
1242
1243 // Dimension dots below node
1244 const dims = DIM_DATA[commit.short] || [];
1245 if (dims.length > 0) {
1246 const dotR = 4, dotSp = 11;
1247 const totalW = (DIMS.length - 1) * dotSp;
1248 const dotsG = g.append('g')
1249 .attr('class', 'dim-dots')
1250 .attr('transform', `translate(${-totalW/2},${R_NODE + 9})`);
1251 DIMS.forEach((dim, di) => {
1252 const active = dims.includes(dim);
1253 const isConf = (DIM_CONFLICTS[commit.short] || []).includes(dim);
1254 dotsG.append('circle')
1255 .attr('cx', di * dotSp).attr('cy', 0).attr('r', dotR)
1256 .attr('fill', active ? DIM_COLORS[dim] : '#21262d')
1257 .attr('stroke', isConf ? '#f85149' : (active ? DIM_COLORS[dim] : '#30363d'))
1258 .attr('stroke-width', isConf ? 1.5 : 0.8)
1259 .attr('opacity', active ? 1 : 0.35);
1260 });
1261 }
1262
1263 // Hover tooltip
1264 g.on('mousemove', (event) => {
1265 tooltip.classList.add('visible');
1266 document.getElementById('tip-id').textContent = commit.id;
1267 document.getElementById('tip-msg').textContent = commit.message;
1268 document.getElementById('tip-branch').innerHTML =
1269 `<span style="color:${color}">⬤</span> ${commit.branch}`;
1270 document.getElementById('tip-files').textContent =
1271 commit.files.length
1272 ? commit.files.join('\\n')
1273 : '(empty snapshot)';
1274 const tipDims = DIM_DATA[commit.short] || [];
1275 const tipConf = DIM_CONFLICTS[commit.short] || [];
1276 const tipDimEl = document.getElementById('tip-dims');
1277 if (tipDimEl) {
1278 tipDimEl.innerHTML = tipDims.length
1279 ? tipDims.map(d => {
1280 const c = tipConf.includes(d);
1281 return `<span style="color:${DIM_COLORS[d]};margin-right:6px">${SVG.dot} ${d}${c?' '+SVG.zap:''}</span>`;
1282 }).join('')
1283 : '';
1284 }
1285 tooltip.style.left = (event.clientX + 12) + 'px';
1286 tooltip.style.top = (event.clientY - 10) + 'px';
1287 }).on('mouseleave', () => {
1288 tooltip.classList.remove('visible');
1289 });
1290 });
1291
1292 // ---- Branch legend ----
1293 const legend = document.getElementById('branch-legend');
1294 DATA.dag.branches.forEach(b => {
1295 const item = document.createElement('div');
1296 item.className = 'legend-item';
1297 item.innerHTML =
1298 `<span class="legend-dot" style="background:${b.color}"></span>` +
1299 `<span>${escHtml(b.name)}</span>`;
1300 legend.appendChild(item);
1301 });
1302 }
1303
1304 /* ===== SVG icon library ===== */
1305 const SVG = {
1306 music: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><path d="M9 18V5l12-2v13"/><circle cx="6" cy="18" r="3"/><circle cx="18" cy="16" r="3"/></svg>`,
1307 branch: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><line x1="6" y1="3" x2="6" y2="15"/><circle cx="18" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M18 9a9 9 0 0 1-9 9"/></svg>`,
1308 merge: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><circle cx="18" cy="18" r="3"/><circle cx="6" cy="6" r="3"/><path d="M6 21V9a9 9 0 0 0 9 9"/></svg>`,
1309 conflict: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>`,
1310 revert: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 .49-3.53"/></svg>`,
1311 check: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>`,
1312 dot: `<svg class="ico" viewBox="0 0 24 24" fill="currentColor" stroke="none"><circle cx="12" cy="12" r="5"/></svg>`,
1313 zap: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>`,
1314 pause: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="6" y="4" width="4" height="16"/><rect x="14" y="4" width="4" height="16"/></svg>`,
1315 eye: `<svg class="ico" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>`,
1316 };
1317
1318 /* ===== Act metadata ===== */
1319 const ACT_ICONS = {
1320 1: SVG.music, 2: SVG.branch, 3: SVG.merge, 4: SVG.conflict, 5: SVG.revert,
1321 };
1322 const ACT_COLORS = {
1323 1:'#4f8ef7', 2:'#3fb950', 3:'#f85149', 4:'#ab47bc', 5:'#f9a825',
1324 };
1325
1326 /* ===== Act jump navigation ===== */
1327 function buildActJumpBar() {
1328 const bar = document.getElementById('act-jump-bar');
1329 if (!bar) return;
1330
1331 const lbl = document.createElement('span');
1332 lbl.textContent = 'Jump:';
1333 bar.appendChild(lbl);
1334
1335 // Collect unique acts
1336 const acts = [];
1337 let last = -1;
1338 DATA.events.forEach(ev => {
1339 if (ev.act !== last) { acts.push({ num: ev.act, title: ev.act_title }); last = ev.act; }
1340 });
1341
1342 acts.forEach(a => {
1343 const btn = document.createElement('button');
1344 btn.className = 'act-jump-btn';
1345 btn.title = `Jump to Act ${a.num}: ${a.title}`;
1346 const icon = ACT_ICONS[a.num] || '';
1347 btn.innerHTML = `${icon} ${a.num}`;
1348 if (a.num >= 6) btn.style.borderColor = ACT_COLORS[a.num] + '66';
1349 btn.addEventListener('click', () => {
1350 pauseTour();
1351 // Find first event index for this act
1352 const idx = DATA.events.findIndex(ev => ev.act === a.num);
1353 if (idx >= 0) {
1354 // Reveal up to this point
1355 revealStep(idx);
1356 // Scroll the act header into view
1357 const hdr = document.getElementById(`act-hdr-${a.num}`);
1358 if (hdr) hdr.scrollIntoView({ behavior: 'smooth', block: 'start' });
1359 }
1360 });
1361 bar.appendChild(btn);
1362 });
1363
1364 // Reveal All button
1365 const allBtn = document.createElement('button');
1366 allBtn.className = 'act-jump-btn reveal-all';
1367 allBtn.innerHTML = SVG.eye + ' Reveal All';
1368 allBtn.title = 'Reveal all 69 events at once';
1369 allBtn.addEventListener('click', () => {
1370 pauseTour();
1371 revealStep(DATA.events.length - 1);
1372 });
1373 bar.appendChild(allBtn);
1374 }
1375
1376 /* ===== Event log ===== */
1377 function buildEventLog() {
1378 const list = document.getElementById('event-list');
1379 let lastAct = -1;
1380
1381 DATA.events.forEach((ev, idx) => {
1382 if (ev.act !== lastAct) {
1383 lastAct = ev.act;
1384
1385 // Act header — always visible (no opacity fade)
1386 const hdr = document.createElement('div');
1387 hdr.className = 'act-header';
1388 hdr.id = `act-hdr-${ev.act}`;
1389 const icon = ACT_ICONS[ev.act] || '';
1390 const col = ACT_COLORS[ev.act] || 'var(--text-dim)';
1391 hdr.innerHTML =
1392 `<span style="color:${col};margin-right:6px">${icon}</span>` +
1393 `Act ${ev.act} <span style="opacity:0.6">—</span> ${ev.act_title}`;
1394 if (ev.act >= 6) {
1395 hdr.style.color = col;
1396 hdr.style.borderTop = `1px solid ${col}33`;
1397 }
1398 list.appendChild(hdr);
1399 }
1400
1401 const isCliCmd = ev.cmd.startsWith('muse ') || ev.cmd.startsWith('git ');
1402
1403 const item = document.createElement('div');
1404 item.className = 'event-item';
1405 item.id = `ev-${idx}`;
1406
1407 if (ev.exit_code !== 0 && ev.output.toLowerCase().includes('conflict')) {
1408 item.classList.add('failed');
1409 }
1410
1411 // Parse cmd
1412 const parts = ev.cmd.split(' ');
1413 const cmdName = parts.slice(0, 2).join(' ');
1414 const cmdArgs = parts.slice(2).join(' ');
1415
1416 // Output class
1417 let outClass = '';
1418 if (ev.output.toLowerCase().includes('conflict')) outClass = 'conflict';
1419 else if (ev.exit_code === 0 && ev.commit_id) outClass = 'success';
1420
1421 const outLines = ev.output.split('\\n').slice(0, 6).join('\\n');
1422
1423 const cmdLine =
1424 `<div class="event-cmd">` +
1425 `<span class="cmd-prefix">$ </span>` +
1426 `<span class="cmd-name">${escHtml(cmdName)}</span>` +
1427 (cmdArgs
1428 ? ` <span class="cmd-args">${escHtml(cmdArgs.slice(0, 80))}${cmdArgs.length > 80 ? '…' : ''}</span>`
1429 : '') +
1430 `</div>`;
1431
1432 item.innerHTML =
1433 cmdLine +
1434 (outLines
1435 ? `<div class="event-output ${outClass}">${escHtml(outLines)}</div>`
1436 : '') +
1437 (() => {
1438 if (!ev.commit_id) return '';
1439 const dims = DIM_DATA[ev.commit_id] || [];
1440 const conf = DIM_CONFLICTS[ev.commit_id] || [];
1441 if (!dims.length) return '';
1442 return '<div class="dim-pills">' + dims.map(d => {
1443 const isc = conf.includes(d);
1444 const col = DIM_COLORS[d];
1445 const cls = isc ? 'dim-pill conflict-pill' : 'dim-pill';
1446 const sty = isc ? '' : `color:${col};border-color:${col};background:${col}22`;
1447 return `<span class="${cls}" style="${sty}">${isc ? SVG.zap+' ' : ''}${d}</span>`;
1448 }).join('') + '</div>';
1449 })() +
1450 `<div class="event-meta">` +
1451 (ev.commit_id ? `<span class="tag-commit">${escHtml(ev.commit_id)}</span>` : '') +
1452 `<span class="tag-time">${ev.duration_ms}ms</span>` +
1453 `</div>`;
1454
1455 list.appendChild(item);
1456 });
1457 }
1458
1459
1460
1461 /* ===== Dimension Timeline ===== */
1462 function buildDimTimeline() {
1463 const matrix = document.getElementById('dim-matrix');
1464 if (!matrix) return;
1465 const sorted = topoSort(DATA.dag.commits);
1466
1467 // Commit ID header row
1468 const hrow = document.createElement('div');
1469 hrow.className = 'dim-matrix-row';
1470 const sp = document.createElement('div');
1471 sp.className = 'dim-label-cell';
1472 hrow.appendChild(sp);
1473 sorted.forEach(c => {
1474 const cell = document.createElement('div');
1475 cell.className = 'dim-commit-cell';
1476 cell.id = `dim-col-label-${c.short}`;
1477 cell.title = c.message;
1478 cell.textContent = c.short.slice(0,6);
1479 hrow.appendChild(cell);
1480 });
1481 matrix.appendChild(hrow);
1482
1483 // One row per dimension
1484 DIMS.forEach(dim => {
1485 const row = document.createElement('div');
1486 row.className = 'dim-matrix-row';
1487 const lbl = document.createElement('div');
1488 lbl.className = 'dim-label-cell';
1489 const dot = document.createElement('span');
1490 dot.className = 'dim-label-dot';
1491 dot.style.background = DIM_COLORS[dim];
1492 lbl.appendChild(dot);
1493 lbl.appendChild(document.createTextNode(dim.charAt(0).toUpperCase() + dim.slice(1)));
1494 row.appendChild(lbl);
1495
1496 sorted.forEach(c => {
1497 const dims = DIM_DATA[c.short] || [];
1498 const conf = DIM_CONFLICTS[c.short] || [];
1499 const active = dims.includes(dim);
1500 const isConf = conf.includes(dim);
1501 const col = DIM_COLORS[dim];
1502 const cell = document.createElement('div');
1503 cell.className = 'dim-cell';
1504 const inner = document.createElement('div');
1505 inner.className = 'dim-cell-inner' + (active ? ' active' : '') + (isConf ? ' conflict-dim' : '');
1506 inner.id = `dim-cell-${dim}-${c.short}`;
1507 if (active) {
1508 inner.style.background = col + '33';
1509 inner.style.color = col;
1510 inner.innerHTML = isConf ? SVG.zap : SVG.dot;
1511 }
1512 cell.appendChild(inner);
1513 row.appendChild(cell);
1514 });
1515 matrix.appendChild(row);
1516 });
1517 }
1518
1519 function highlightDimColumn(shortId) {
1520 document.querySelectorAll('.dim-commit-cell.col-highlight, .dim-cell-inner.col-highlight')
1521 .forEach(el => el.classList.remove('col-highlight'));
1522 if (!shortId) return;
1523 const lbl = document.getElementById(`dim-col-label-${shortId}`);
1524 if (lbl) {
1525 lbl.classList.add('col-highlight');
1526 lbl.scrollIntoView({ behavior:'smooth', block:'nearest', inline:'center' });
1527 }
1528 DIMS.forEach(dim => {
1529 const cell = document.getElementById(`dim-cell-${dim}-${shortId}`);
1530 if (cell) cell.classList.add('col-highlight');
1531 });
1532 }
1533
1534 /* ===== Replay animation ===== */
1535 function revealStep(stepIdx) {
1536 if (stepIdx < 0 || stepIdx >= DATA.events.length) return;
1537
1538 const ev = DATA.events[stepIdx];
1539
1540 // Reveal all events up to this step
1541 for (let i = 0; i <= stepIdx; i++) {
1542 const el = document.getElementById(`ev-${i}`);
1543 if (el) el.classList.add('revealed');
1544 }
1545
1546 // Mark current as active (remove previous)
1547 document.querySelectorAll('.event-item.active').forEach(el => el.classList.remove('active'));
1548 const cur = document.getElementById(`ev-${stepIdx}`);
1549 if (cur) {
1550 cur.classList.add('active');
1551 cur.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
1552 }
1553
1554 // Highlight commit node
1555 document.querySelectorAll('.commit-node.highlighted').forEach(el => el.classList.remove('highlighted'));
1556 if (ev.commit_id) {
1557 const node = document.querySelector(`.commit-node[data-short="${ev.commit_id}"]`);
1558 if (node) {
1559 node.classList.add('highlighted');
1560 // Scroll DAG to show the node
1561 const transform = node.getAttribute('transform');
1562 if (transform) {
1563 const m = transform.match(/translate\\(([\\d.]+),([\\d.]+)\\)/);
1564 if (m) {
1565 const scroll = document.getElementById('dag-scroll');
1566 const y = parseFloat(m[2]);
1567 scroll.scrollTo({ top: Math.max(0, y - 200), behavior: 'smooth' });
1568 }
1569 }
1570 }
1571 }
1572
1573 // Highlight dimension matrix column
1574 highlightDimColumn(ev.commit_id || null);
1575
1576 // Update counter and step button states
1577 document.getElementById('step-counter').textContent =
1578 `Step ${stepIdx + 1} / ${DATA.events.length}`;
1579 document.getElementById('btn-prev').disabled = (stepIdx === 0);
1580 document.getElementById('btn-next').disabled = (stepIdx === DATA.events.length - 1);
1581
1582 currentStep = stepIdx;
1583 }
1584
1585 function playTour() {
1586 if (isPlaying) return;
1587 isPlaying = true;
1588 document.getElementById('btn-play').innerHTML = SVG.pause + ' Pause';
1589
1590 function advance() {
1591 if (!isPlaying) return;
1592 const next = currentStep + 1;
1593 if (next >= DATA.events.length) {
1594 pauseTour();
1595 document.getElementById('btn-play').innerHTML = SVG.check + ' Done';
1596 return;
1597 }
1598 revealStep(next);
1599 playTimer = setTimeout(advance, PLAY_INTERVAL_MS);
1600 }
1601 advance();
1602 }
1603
1604 function pauseTour() {
1605 isPlaying = false;
1606 clearTimeout(playTimer);
1607 document.getElementById('btn-play').textContent = '▶ Play Tour';
1608 highlightDimColumn(null);
1609 }
1610
1611 function resetTour() {
1612 pauseTour();
1613 currentStep = -1;
1614 document.querySelectorAll('.event-item').forEach(el => {
1615 el.classList.remove('revealed','active');
1616 });
1617 document.querySelectorAll('.commit-node.highlighted').forEach(el => {
1618 el.classList.remove('highlighted');
1619 });
1620 document.getElementById('step-counter').textContent = '';
1621 document.getElementById('log-scroll').scrollTop = 0;
1622 document.getElementById('dag-scroll').scrollTop = 0;
1623 document.getElementById('btn-play').textContent = '▶ Play Tour';
1624 document.getElementById('btn-prev').disabled = true;
1625 document.getElementById('btn-next').disabled = false;
1626 highlightDimColumn(null);
1627 }
1628
1629 /* ===== Init ===== */
1630 document.addEventListener('DOMContentLoaded', () => {
1631 _initDimMaps();
1632 drawDAG();
1633 buildEventLog();
1634 buildActJumpBar();
1635 buildDimTimeline();
1636
1637 document.getElementById('btn-prev').disabled = true; // nothing to go back to yet
1638
1639 document.getElementById('btn-play').addEventListener('click', () => {
1640 if (isPlaying) pauseTour(); else playTour();
1641 });
1642 document.getElementById('btn-prev').addEventListener('click', () => {
1643 pauseTour();
1644 if (currentStep > 0) revealStep(currentStep - 1);
1645 });
1646 document.getElementById('btn-next').addEventListener('click', () => {
1647 pauseTour();
1648 if (currentStep < DATA.events.length - 1) revealStep(currentStep + 1);
1649 });
1650 document.getElementById('btn-reset').addEventListener('click', resetTour);
1651
1652 // Keyboard shortcuts: ← → for step, Space for play/pause
1653 document.addEventListener('keydown', (e) => {
1654 if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
1655 if (e.key === 'ArrowLeft') {
1656 e.preventDefault();
1657 pauseTour();
1658 if (currentStep > 0) revealStep(currentStep - 1);
1659 } else if (e.key === 'ArrowRight') {
1660 e.preventDefault();
1661 pauseTour();
1662 if (currentStep < DATA.events.length - 1) revealStep(currentStep + 1);
1663 } else if (e.key === ' ') {
1664 e.preventDefault();
1665 if (isPlaying) pauseTour(); else playTour();
1666 }
1667 });
1668 });
1669 </script>
1670 </body>
1671 </html>
1672 """
1673
1674
1675 # ---------------------------------------------------------------------------
1676 # Main render function
1677 # ---------------------------------------------------------------------------
1678
1679
1680 def render(tour: dict, output_path: pathlib.Path) -> None:
1681 """Render the tour data into a self-contained HTML file."""
1682 print(" Rendering HTML visualization...")
1683 d3_script = _fetch_d3()
1684
1685 meta = tour.get("meta", {})
1686 stats = tour.get("stats", {})
1687
1688 # Format generated_at nicely
1689 gen_raw = meta.get("generated_at", "")
1690 try:
1691 from datetime import datetime, timezone
1692 dt = datetime.fromisoformat(gen_raw).astimezone(timezone.utc)
1693 gen_str = dt.strftime("%Y-%m-%d %H:%M UTC")
1694 except Exception:
1695 gen_str = gen_raw[:19]
1696
1697 html = _HTML_TEMPLATE
1698 html = html.replace("{{VERSION}}", str(meta.get("muse_version", "0.1.1")))
1699 html = html.replace("{{DOMAIN}}", str(meta.get("domain", "music")))
1700 html = html.replace("{{ELAPSED}}", str(meta.get("elapsed_s", "?")))
1701 html = html.replace("{{GENERATED_AT}}", gen_str)
1702 html = html.replace("{{COMMITS}}", str(stats.get("commits", 0)))
1703 html = html.replace("{{BRANCHES}}", str(stats.get("branches", 0)))
1704 html = html.replace("{{MERGES}}", str(stats.get("merges", 0)))
1705 html = html.replace("{{CONFLICTS}}", str(stats.get("conflicts_resolved", 0)))
1706 html = html.replace("{{OPS}}", str(stats.get("operations", 0)))
1707 html = html.replace("{{ARCH_HTML}}", _ARCH_HTML)
1708 html = html.replace("{{D3_SCRIPT}}", d3_script)
1709 html = html.replace("{{DATA_JSON}}", json.dumps(tour, separators=(",", ":")))
1710
1711 output_path.write_text(html, encoding="utf-8")
1712 size_kb = output_path.stat().st_size // 1024
1713 print(f" HTML written ({size_kb}KB) → {output_path}")
1714
1715
1716 # ---------------------------------------------------------------------------
1717 # Stand-alone entry point
1718 # ---------------------------------------------------------------------------
1719
1720 if __name__ == "__main__":
1721 import argparse
1722 parser = argparse.ArgumentParser(description="Render tour_de_force.json → HTML")
1723 parser.add_argument("json_file", help="Path to tour_de_force.json")
1724 parser.add_argument("--out", default=None, help="Output HTML path")
1725 args = parser.parse_args()
1726
1727 json_path = pathlib.Path(args.json_file)
1728 if not json_path.exists():
1729 print(f"❌ File not found: {json_path}", file=sys.stderr)
1730 sys.exit(1)
1731
1732 data = json.loads(json_path.read_text())
1733 out_path = pathlib.Path(args.out) if args.out else json_path.with_suffix(".html")
1734 render(data, out_path)
1735 print(f"Open: file://{out_path.resolve()}")