typst-pack 0.5.0

Portable single-file packs of Typst projects: sources, resources, packages, and fonts
Documentation
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
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pack Lifecycle Architecture Prototype</title>
  <style>
    :root {
      --ink: #14213d;
      --muted: #526178;
      --paper: #f5f1e8;
      --panel: #fffdf8;
      --line: #c8d0d9;
      --accent: #006d77;
      --accent-soft: #d9efef;
      --danger: #9f2d20;
      --danger-soft: #f7dfda;
      --shadow: 0 10px 30px rgba(20, 33, 61, 0.08);
    }

    * { box-sizing: border-box; }

    body {
      margin: 0;
      color: var(--ink);
      background:
        linear-gradient(rgba(20, 33, 61, 0.035) 1px, transparent 1px),
        linear-gradient(90deg, rgba(20, 33, 61, 0.035) 1px, transparent 1px),
        var(--paper);
      background-size: 24px 24px;
      font: 16px/1.5 ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;
    }

    button { font: inherit; }

    .shell {
      width: min(1500px, calc(100% - 32px));
      margin: 0 auto;
      padding: 36px 0 64px;
    }

    header {
      border-top: 8px solid var(--accent);
      background: var(--panel);
      box-shadow: var(--shadow);
      padding: 28px clamp(20px, 4vw, 56px) 32px;
    }

    .eyebrow {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      align-items: center;
      color: var(--accent);
      font-size: 0.78rem;
      font-weight: 800;
      letter-spacing: 0.08em;
      text-transform: uppercase;
    }

    .badge {
      border: 1px solid var(--accent);
      border-radius: 999px;
      padding: 3px 9px;
    }

    h1, h2, h3, p { margin-top: 0; }

    h1 {
      max-width: 900px;
      margin: 18px 0 12px;
      font: 800 clamp(2rem, 5vw, 4.4rem)/0.98 Georgia, "Times New Roman", serif;
      letter-spacing: -0.045em;
    }

    .question {
      max-width: 1060px;
      margin: 0;
      color: var(--muted);
      font-size: clamp(1rem, 1.7vw, 1.2rem);
    }

    .layout {
      display: grid;
      grid-template-columns: minmax(0, 1.25fr) minmax(360px, 0.75fr);
      gap: 20px;
      margin-top: 20px;
      align-items: start;
    }

    .stack { display: grid; gap: 20px; }

    .panel {
      background: var(--panel);
      border: 1px solid rgba(20, 33, 61, 0.16);
      box-shadow: var(--shadow);
      padding: 22px;
    }

    .panel-title {
      display: flex;
      justify-content: space-between;
      gap: 16px;
      align-items: baseline;
      margin-bottom: 18px;
      padding-bottom: 12px;
      border-bottom: 1px solid var(--line);
    }

    .panel-title h2 { margin: 0; font-size: 1.05rem; }

    .panel-title span { color: var(--muted); font-size: 0.8rem; }

    .route {
      display: grid;
      grid-template-columns: repeat(5, minmax(120px, 1fr));
      gap: 8px;
      overflow-x: auto;
      padding-bottom: 4px;
    }

    .route-step {
      min-height: 88px;
      border: 1px solid var(--line);
      border-top: 4px solid var(--line);
      background: #faf8f2;
      padding: 10px;
    }

    .route-step.active {
      border-top-color: var(--accent);
      background: var(--accent-soft);
    }

    .route-step strong { display: block; font-size: 0.82rem; }
    .route-step small { display: block; margin-top: 6px; color: var(--muted); }

    .state-grid {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 10px;
    }

    .state-card {
      min-height: 112px;
      border-left: 4px solid var(--accent);
      background: #f7f7f2;
      padding: 12px 14px;
    }

    .state-card h3 {
      margin-bottom: 8px;
      color: var(--muted);
      font-size: 0.72rem;
      letter-spacing: 0.06em;
      text-transform: uppercase;
    }

    .state-card p, .state-card ul { margin-bottom: 0; font-size: 0.88rem; }

    .state-card ul { padding-left: 18px; }

    .last-change {
      margin-top: 12px;
      border: 1px solid var(--accent);
      background: var(--accent-soft);
      padding: 10px 12px;
      font-size: 0.88rem;
    }

    .last-change.rejected {
      border-color: var(--danger);
      background: var(--danger-soft);
      color: var(--danger);
    }

    .controls {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 8px;
    }

    .action {
      min-height: 54px;
      border: 1px solid var(--ink);
      background: transparent;
      color: var(--ink);
      cursor: pointer;
      padding: 9px 11px;
      text-align: left;
    }

    .action:hover, .action:focus-visible {
      color: white;
      background: var(--ink);
      outline: 3px solid var(--accent-soft);
      outline-offset: 1px;
    }

    .action.primary {
      border-color: var(--accent);
      background: var(--accent);
      color: white;
    }

    .scenario-tabs {
      display: flex;
      gap: 6px;
      overflow-x: auto;
      padding-bottom: 8px;
    }

    .tab {
      flex: 0 0 auto;
      border: 1px solid var(--line);
      background: #faf8f2;
      color: var(--ink);
      cursor: pointer;
      padding: 8px 10px;
    }

    .tab.selected {
      border-color: var(--accent);
      background: var(--accent-soft);
      color: var(--accent);
      font-weight: 800;
    }

    .scenario-copy {
      min-height: 92px;
      margin: 12px 0;
      color: var(--muted);
      font-size: 0.9rem;
    }

    .walkthrough { display: grid; gap: 7px; }

    .walkthrough button {
      border: 1px solid var(--line);
      background: #fff;
      cursor: pointer;
      padding: 10px 12px;
      text-align: left;
    }

    .walkthrough button.current {
      border-color: var(--accent);
      box-shadow: inset 4px 0 var(--accent);
    }

    .walkthrough button.complete {
      color: var(--muted);
      text-decoration: line-through;
    }

    .walkthrough button:disabled { cursor: default; opacity: 0.62; }

    .migration-list { display: grid; gap: 6px; }

    .migration-step {
      display: grid;
      grid-template-columns: 34px 1fr;
      gap: 10px;
      border: 1px solid var(--line);
      padding: 9px;
      font-size: 0.84rem;
    }

    .migration-step .index {
      display: grid;
      place-items: center;
      width: 30px;
      height: 30px;
      background: var(--ink);
      color: white;
      font-weight: 800;
    }

    .migration-step.complete { background: var(--accent-soft); }
    .migration-step.next { border: 2px solid var(--accent); }
    .migration-step strong, .migration-step small { display: block; }
    .migration-step small { margin-top: 3px; color: var(--muted); }

    details { border-top: 1px solid var(--line); padding-top: 12px; }
    details + details { margin-top: 12px; }
    summary { cursor: pointer; font-weight: 800; }

    pre {
      overflow-x: auto;
      margin: 12px 0 0;
      padding: 14px;
      background: var(--ink);
      color: #eef7f7;
      font-size: 0.78rem;
      line-height: 1.55;
    }

    .provisional {
      margin: 0;
      padding-left: 20px;
      color: var(--muted);
      font-size: 0.88rem;
    }

    .review {
      margin-top: 20px;
      border: 2px solid var(--accent);
      background: var(--panel);
      padding: 22px;
    }

    .review ol { margin-bottom: 0; padding-left: 24px; }

    @media (max-width: 980px) {
      .layout { grid-template-columns: 1fr; }
      .route { grid-template-columns: repeat(5, 170px); }
    }

    @media (max-width: 620px) {
      .shell { width: min(100% - 18px, 1500px); padding-top: 10px; }
      header, .panel, .review { padding: 16px; }
      .state-grid, .controls { grid-template-columns: 1fr; }
      h1 { font-size: 2.4rem; }
    }
  </style>
</head>
<body>
  <main class="shell">
    <header>
      <div class="eyebrow">
        <span class="badge">Throwaway prototype</span>
        <span>Validate the complete Pack lifecycle architecture</span>
      </div>
      <h1>Can one Pack travel the whole lifecycle without crossing the wrong seam?</h1>
      <p class="question">
        This prototype tests whether the selected domain values, deep modules, phase ordering,
        ownership rules, failures, and destination effects compose from source reading to
        writing. It also tests whether the migration can preserve version-1 <code>.typk</code>,
        filesystem, and CLI behavior while replacing one seam at a time.
      </p>
    </header>

    <section class="layout">
      <div class="stack">
        <section class="panel">
          <div class="panel-title">
            <h2>Lifecycle route</h2>
            <span id="route-status">No Pack yet</span>
          </div>
          <div class="route" id="route"></div>
        </section>

        <section class="panel">
          <div class="panel-title">
            <h2>Current state</h2>
            <span>Full relevant state after every action</span>
          </div>
          <div class="state-grid" id="state-grid"></div>
          <div class="last-change" id="last-change"></div>
        </section>

        <section class="panel">
          <div class="panel-title">
            <h2>Free play</h2>
            <span>Try legal and illegal orderings</span>
          </div>
          <div class="controls" id="controls"></div>
        </section>

        <section class="panel">
          <div class="panel-title">
            <h2>Selected interface skeleton</h2>
            <span>Shape, not production code</span>
          </div>
          <details open>
            <summary>Semantic core and Pack Archive</summary>
            <pre>pub fn create(
    input: PackCreationInput&lt;'_&gt;,
) -&gt; Result&lt;PackCreationOutcome, PackCreationError&gt;;

pub fn encode(
    pack: &amp;Pack,
    limits: EncodeLimits,
) -&gt; Result&lt;PackArchiveBytes, pack_archive::EncodeError&gt;;

pub fn decode(
    archive: &amp;PackArchiveBytes,
    limits: DecodeLimits,
) -&gt; Result&lt;Pack, pack_archive::DecodeError&gt;;</pre>
          </details>
          <details>
            <summary>Reference filesystem Pack Assembler</summary>
            <pre>pub struct FilesystemPackAssembler { /* private */ }

impl FilesystemPackAssembler {
    pub fn new(
        config: FilesystemPackAssemblerConfig,
    ) -&gt; Result&lt;Self, FilesystemPackAssemblerConfigError&gt;;

    pub fn assemble(
        &amp;self,
        request: FilesystemPackAssemblyRequest&lt;'_&gt;,
    ) -&gt; Result&lt;PackAssemblyReport,
                 FilesystemPackAssemblyError&gt;;
}

// Config owns concrete host policy, authorities, clock, and limits.
// A per-run request borrows roots and carries semantic controls.
// The report owns the Pack and discovery warnings; no partial Pack
// crosses an error.</pre>
          </details>
          <details>
            <summary>Compilation</summary>
            <pre>pub fn compile(
    request: PackCompilationRequest,
    limits: CompilationLimits,
) -&gt; Result&lt;CompilationReport, CompilationRequestRejection&gt;;

// Private ordered phases:
// accept request -&gt; establish identity -&gt; verify exact
// fulfillments -&gt; materialize World -&gt; compile/export

// Request acceptance resolves each semantic slot by origin:
// caller supplied &gt; adapter resolved &gt; core defaulted.
// Same-origin setters replace. Additive features form a canonical
// union and retain the strongest origin. Fulfillments use a separate
// duplicate-rejecting exact set.</pre>
          </details>
          <details>
            <summary>Planning and destination adapters</summary>
            <pre>pub fn plan_pack_extraction(
    pack: &amp;Pack,
    selection: PackExtractionSelection,
) -&gt; Result&lt;PackExtractionPlan, PackExtractionPlanError&gt;;

pub fn apply_pack_extraction_to_filesystem(
    plan: &amp;PackExtractionPlan,
    destination: &amp;Path,
    policy: FilesystemTreeWritingPolicy,
) -&gt; Result&lt;PackExtractionFilesystemReceipt,
             PackExtractionFilesystemError&gt;;</pre>
          </details>
        </section>
      </div>

      <aside class="stack">
        <section class="panel">
          <div class="panel-title">
            <h2>Guided walkthroughs</h2>
            <span>Click steps in order</span>
          </div>
          <div class="scenario-tabs" id="scenario-tabs"></div>
          <p class="scenario-copy" id="scenario-copy"></p>
          <div class="walkthrough" id="walkthrough"></div>
        </section>

        <section class="panel">
          <div class="panel-title">
            <h2>Migration proof</h2>
            <span id="migration-status">0 / 12 slices</span>
          </div>
          <div class="migration-list" id="migration-list"></div>
        </section>

        <section class="panel">
          <div class="panel-title">
            <h2>Still provisional</h2>
            <span>Not silently decided here</span>
          </div>
          <ul class="provisional">
            <li>Exact accessors used to inspect a <code>CompilationReport</code>.</li>
            <li>Exact stream and file writing operation names and recovery methods.</li>
          </ul>
        </section>
      </aside>
    </section>

    <section class="review">
      <h2>Validated verdict</h2>
      <p>
        HITL review accepted the lifecycle composition and the 12-slice migration. It also fixed
        the configured reference filesystem Pack Assembler seam and origin-priority rules for
        compilation controls. No remaining provisional detail changes a domain invariant, seam,
        execution phase, ownership contract, or preservation gate.
      </p>
      <ol>
        <li>No destination effect occurs before its semantic plan or validated value exists.</li>
        <li>Errors, Rejections, Outcomes, Reports, Receipts, and Progress remain phase-specific.</li>
        <li>Migration freezes interoperability before changing ownership, values, format, or workflows.</li>
        <li>Remaining accessor and operation naming is implementation detail, not wayfinding fog.</li>
      </ol>
    </section>
  </main>

  <script>
    const Architecture = (() => {
      const initialState = () => ({
        project: "not read",
        packages: "empty catalog",
        pack: "none",
        packIdentity: "not established",
        archive: "none",
        extraction: "none",
        compilation: "none",
        world: "not materialized",
        destinationEffects: [],
        evidence: [],
        payloads: "no retained payloads",
        migration: 0,
        last: "Reset. Source reading may begin.",
        rejected: false,
      });

      const reject = (state, message) => ({
        ...state,
        last: `Illegal route: ${message}`,
        rejected: true,
      });

      const accept = (state, patch, message) => ({
        ...state,
        ...patch,
        last: message,
        rejected: false,
      });

      const hasPack = (state) => state.pack === "validated Pack";

      function reduce(state, action) {
        switch (action.type) {
          case "RESET":
            return initialState();
          case "READ_PROJECT":
            return accept(state, {
              project: "validated Project Snapshot",
              payloads: "Project Snapshot owns shared immutable payloads",
              evidence: ["Filesystem reader selected membership before assembly"],
            }, "The source-specific reader applied the Project Ignore Policy, bounded its reads, and submitted exact selected bytes to Project Snapshot assembly.");
          case "DISCOVER_MISSING":
            if (state.project !== "validated Project Snapshot") {
              return reject(state, "Pack Creation requires a Project Snapshot.");
            }
            return accept(state, {
              packages: "missing @preview/plotst and @preview/tablex",
              evidence: [...state.evidence, "Missing package specifications are exact and canonical"],
            }, "Pack Creation accepted its borrowed inputs, ran one Dependency Discovery, and returned a resumable Missing Package Specifications outcome. No Pack exists yet.");
          case "RECORD_PACKAGE_FAILURE":
            if (!state.packages.startsWith("missing")) {
              return reject(state, "An reading failure must correspond to a reported missing specification.");
            }
            return accept(state, {
              packages: "one Package Tree plus one reading failure",
              evidence: [...state.evidence, "Failure data remains outside the Package Catalog"],
            }, "Pack Assembly retained the failed reading separately so Dependency Discovery can attach it to the importing source request.");
          case "SUPPLY_PACKAGES":
            if (state.project !== "validated Project Snapshot") {
              return reject(state, "Package Catalog preparation does not replace Project Snapshot reading.");
            }
            return accept(state, {
              packages: "validated complete Package Catalog",
              evidence: [...state.evidence, "Supplied Package Trees supersede older reading failures"],
              payloads: "Project and package payloads use shared immutable allocations",
            }, "Every Package Tree was validated before catalog insertion, including declarations that Dependency Discovery may not use.");
          case "CREATE_PACK":
            if (state.project !== "validated Project Snapshot" || state.packages !== "validated complete Package Catalog") {
              return reject(state, "Pack Creation needs validated project, package, and font inputs. Use the prepared Package Catalog in this prototype.");
            }
            return accept(state, {
              pack: "validated Pack",
              packIdentity: "established from canonical semantic state",
              payloads: "Pack shares selected immutable payloads; creation copied no payload bytes",
              evidence: [...state.evidence, "Private Pack construction owns whole-Pack invariants"],
            }, "Pack Creation returned Created with one validated Pack and discovery warnings. The Pack is independent of archive representation and host destinations.");
          case "READ_ARCHIVE":
            return accept(state, {
              archive: "bounded exact Pack Archive bytes",
              payloads: "PackArchiveBytes uniquely owns its allocation and is not Clone",
              evidence: [...state.evidence, "Read applied mandatory finite limits"],
            }, "A concrete source adapter read exact Pack Archive bytes without decoding them.");
          case "DECODE_AMBIGUOUS":
            if (!state.archive.includes("Pack Archive bytes")) {
              return reject(state, "Pack Archive Decoding requires read exact bytes.");
            }
            return accept(state, {
              pack: "none",
              packIdentity: "not established",
              evidence: [...state.evidence, "DecodeError::Archive rejected raw ZIP ambiguity"],
            }, "Decoding rejected ambiguous raw archive members before domain interpretation. This is a Pack Archive error, not a Pack invariant error.");
          case "DECODE_PACK":
            if (!state.archive.includes("Pack Archive bytes")) {
              return reject(state, "Pack Archive Decoding requires read exact bytes.");
            }
            return accept(state, {
              pack: "validated Pack",
              packIdentity: "established only after whole-Pack validation",
              payloads: "Decoded semantic payloads are shared; archive bytes remain unique",
              evidence: [...state.evidence, "Safe unknown entries were ignored without becoming Pack state"],
            }, "The format module parsed version 1 and submitted sequence-shaped content to authoritative Pack construction. Only then did a Pack exist.");
          case "ENCODE_TOO_SMALL":
            if (!hasPack(state)) return reject(state, "Pack Archive Encoding requires a validated Pack.");
            return accept(state, {
              archive: "EncodeError::Limit; no archive value",
              evidence: [...state.evidence, "The valid Pack survived an encoding-only failure"],
            }, "The Pack remains valid although the selected representation limits cannot encode it. Pack validity did not absorb ZIP constraints.");
          case "ENCODE_PACK":
            if (!hasPack(state)) return reject(state, "Pack Archive Encoding requires a validated Pack.");
            return accept(state, {
              archive: "complete exact Pack Archive bytes",
              payloads: "PackArchiveBytes uniquely owns the complete encoded allocation",
              evidence: [...state.evidence, "Encoding performed no whole-Pack revalidation"],
            }, "The format module produced complete version-1 Pack Archive bytes before destination writing began.");
          case "WRITE_ARCHIVE_FAILURE":
            if (state.archive !== "complete exact Pack Archive bytes") {
              return reject(state, "Archive writing requires complete encoded bytes.");
            }
            return accept(state, {
              destinationEffects: [...state.destinationEffects, "archive destination: Not Committed"],
              evidence: [...state.evidence, "Exact encoded bytes retained for retry"],
            }, "Atomic filesystem writing failed before commit. The error returned the exact PackArchiveBytes, so retry needs no re-encoding.");
          case "WRITE_ARCHIVE":
            if (state.archive !== "complete exact Pack Archive bytes") {
              return reject(state, "Archive writing requires complete encoded bytes.");
            }
            return accept(state, {
              destinationEffects: [...state.destinationEffects, "archive destination: Committed"],
              evidence: [...state.evidence, "Pack Archive Writing Receipt"],
            }, "A concrete filesystem adapter atomically committed the complete archive and issued a Writing Receipt.");
          case "PLAN_COLLIDING_EXTRACTION":
            if (!hasPack(state)) return reject(state, "Pack Extraction planning requires a Pack.");
            return accept(state, {
              extraction: "planning rejected a cross-role ancestor collision",
              evidence: [...state.evidence, "No destination was accessed"],
            }, "Semantic planning found a project/package path collision before any destination or write policy entered the operation.");
          case "PLAN_EXTRACTION":
            if (!hasPack(state)) return reject(state, "Pack Extraction planning requires a Pack.");
            return accept(state, {
              extraction: "owned Pack Extraction Plan",
              payloads: "The plan owns shared payload handles and survives dropping the Pack",
              evidence: [...state.evidence, "Plan contains no destination or write policy"],
            }, "Pack Extraction produced one collision-checked, canonically ordered semantic plan.");
          case "APPLY_EXTRACTION_PARTIAL":
            if (state.extraction !== "owned Pack Extraction Plan") {
              return reject(state, "Destination effects require a Pack Extraction Plan.");
            }
            return accept(state, {
              destinationEffects: [...state.destinationEffects, "a.typ: Committed", "b.typ: Not Committed"],
              evidence: [...state.evidence, "Ordered Writing Progress retained the failed path and staging residue"],
            }, "Merge writing atomically committed one exact file, then failed on the next. The adapter reported truthful partial progress without exposing a truncated target.");
          case "INVALID_COMPILE_REQUEST":
            if (!hasPack(state)) return reject(state, "Pack Compilation requires a Pack.");
            return accept(state, {
              compilation: "Compilation Request Rejection",
              world: "not materialized",
              evidence: [...state.evidence, "All request issues aggregated before fulfillment inspection"],
            }, "Invalid semantic controls were rejected before Compilation Identity, fulfillment verification, World materialization, or compiler execution.");
          case "ACCEPT_COMPILE_REQUEST":
            if (!hasPack(state)) return reject(state, "Pack Compilation requires a Pack.");
            return accept(state, {
              compilation: "accepted request with Compilation Identity",
              world: "not materialized",
              evidence: [...state.evidence, "Request inventory and implementation identities fixed"],
            }, "The semantic request was accepted and canonicalized. Compilation Identity now exists, but no external fulfillment has been trusted and no World exists.");
          case "VERIFY_BAD_FULFILLMENTS":
            if (state.compilation !== "accepted request with Compilation Identity") {
              return reject(state, "Exact-set fulfillment verification follows request acceptance.");
            }
            return accept(state, {
              compilation: "Compilation Operation Outcome with complete fulfillment issues",
              world: "not materialized",
              evidence: [...state.evidence, "Missing, unexpected, embedded, and mismatched entries ordered canonically"],
            }, "The accepted operation returned one report containing every independently detectable fulfillment issue. World materialization never began.");
          case "VERIFY_FULFILLMENTS":
            if (state.compilation !== "accepted request with Compilation Identity") {
              return reject(state, "Exact-set fulfillment verification follows request acceptance.");
            }
            return accept(state, {
              compilation: "exact Compilation Fulfillment Set verified",
              evidence: [...state.evidence, "Every external requirement matched exactly once"],
            }, "Validated Package Trees and Font Containers exactly fulfill the Pack's external requirements.");
          case "MATERIALIZE_WORLD":
            if (state.compilation !== "exact Compilation Fulfillment Set verified") {
              return reject(state, "The private Pack-backed World may exist only after exact fulfillment verification.");
            }
            return accept(state, {
              world: "private ambient-free Pack-backed World",
              evidence: [...state.evidence, "No ambient filesystem, fonts, clock, environment, cache, or network"],
            }, "The private embedded Typst adapter received one confined World assembled from the Pack, overrides, and verified fulfillments.");
          case "COMPILER_REJECTS":
            if (state.world !== "private ambient-free Pack-backed World") {
              return reject(state, "Compiler execution requires the private verified World.");
            }
            return accept(state, {
              compilation: "rejected semantic Compilation Result with no artifacts",
              evidence: [...state.evidence, "Compiler diagnostics belong to a result, not a Rust error"],
            }, "The compiler rejected the document. The accepted operation still returned a Compilation Result and Compilation Report with diagnostics and identities.");
          case "COMPILER_SUCCEEDS":
            if (state.world !== "private ambient-free Pack-backed World") {
              return reject(state, "Compiler execution requires the private verified World.");
            }
            return accept(state, {
              compilation: "succeeded Compilation Result with canonical artifacts",
              payloads: "Artifacts own shared immutable payloads",
              evidence: [...state.evidence, "Parallel completion order did not affect semantic ordering"],
            }, "Compilation and export succeeded under invocation-scoped limits. Artifacts, diagnostics, trace, and identities are canonical.");
          case "PLAN_ARTIFACTS":
            if (state.compilation !== "succeeded Compilation Result with canonical artifacts") {
              return reject(state, "Artifact writing planning accepts only a succeeded Compilation Result.");
            }
            return accept(state, {
              compilation: "owned Compilation Artifact Writing Plan",
              payloads: "The plan shares artifact payload handles",
              evidence: [...state.evidence, "Artifact destinations remain outside Compilation Result"],
            }, "A semantic plan assigned canonical relative paths to result artifacts without touching a destination.");
          case "WRITE_ARTIFACTS":
            if (state.compilation !== "owned Compilation Artifact Writing Plan") {
              return reject(state, "Artifact destination effects require a writing plan.");
            }
            return accept(state, {
              destinationEffects: [...state.destinationEffects, "compilation artifacts: Committed"],
              evidence: [...state.evidence, "Artifact Filesystem Receipt"],
            }, "A concrete destination adapter atomically written the planned artifacts and returned workflow-specific evidence.");
          case "MIGRATE": {
            if (action.index !== state.migration) {
              return reject(state, `Migration slice ${action.index + 1} is not next. Complete slice ${state.migration + 1} first.`);
            }
            return accept(state, {
              migration: state.migration + 1,
              evidence: [...state.evidence, `Migration gate passed: ${migration[action.index].name}`],
            }, `Migration slice ${action.index + 1} passed its preservation gates: ${migration[action.index].gate}`);
          }
          default:
            return reject(state, `Unknown action ${action.type}.`);
        }
      }

      const migration = [
        { name: "Freeze interoperability", gate: "Version-1 fixtures, identity vectors, and CLI behavior are pinned before structural edits." },
        { name: "Share immutable payloads", gate: "Semantic clones copy no payload bytes and identities remain unchanged." },
        { name: "Validate input values", gate: "Package Trees, Font Containers, and catalogs reject ambiguity before use." },
        { name: "Deepen Pack construction", gate: "One private sequence-shaped seam owns whole-Pack invariants and identity." },
        { name: "Split Pack Archive format", gate: "Old and independent fixtures interoperate; representation failures leave valid Packs valid." },
        { name: "Add reading and writing", gate: "Exact bytes are bounded, atomic file writing is old-or-complete, and retry material survives." },
        { name: "Move membership to readers", gate: "Filesystem membership remains equal while core assembly loses source policy and budgets." },
        { name: "Migrate Pack Creation and Assembly", gate: "Resumption, source order, offline policy, warnings, and CLI defaults remain stable." },
        { name: "Complete amplification limits", gate: "Every dimension passes below, exactly-at, and one-over tests without changing identities." },
        { name: "Complete compilation contracts", gate: "Request precedence, exact fulfillments, confinement, and canonical output remain stable." },
        { name: "Split plans from destination effects", gate: "Extraction and artifact semantics are testable without filesystems; CLI workflows remain stable." },
        { name: "Reorganize conformance evidence", gate: "Operation-specific suites replace obsolete tests only after each new seam is active." },
      ];

      return { initialState, reduce, migration };
    })();

    const actions = [
      ["RESET", "Reset lifecycle"],
      ["READ_PROJECT", "Read Project Snapshot"],
      ["DISCOVER_MISSING", "Discover missing packages"],
      ["RECORD_PACKAGE_FAILURE", "Record reading failure"],
      ["SUPPLY_PACKAGES", "Supply validated Package Trees"],
      ["CREATE_PACK", "Create validated Pack"],
      ["READ_ARCHIVE", "Read Pack Archive bytes"],
      ["DECODE_AMBIGUOUS", "Decode ambiguous archive"],
      ["DECODE_PACK", "Decode valid archive"],
      ["ENCODE_TOO_SMALL", "Encode under a tiny limit"],
      ["ENCODE_PACK", "Encode Pack Archive"],
      ["WRITE_ARCHIVE_FAILURE", "Fail archive writing"],
      ["WRITE_ARCHIVE", "Write archive atomically"],
      ["PLAN_COLLIDING_EXTRACTION", "Plan colliding extraction"],
      ["PLAN_EXTRACTION", "Plan Pack Extraction"],
      ["APPLY_EXTRACTION_PARTIAL", "Partially apply extraction"],
      ["INVALID_COMPILE_REQUEST", "Reject invalid compilation request"],
      ["ACCEPT_COMPILE_REQUEST", "Accept compilation request"],
      ["VERIFY_BAD_FULFILLMENTS", "Verify inexact fulfillments"],
      ["VERIFY_FULFILLMENTS", "Verify exact fulfillments"],
      ["MATERIALIZE_WORLD", "Materialize private World"],
      ["COMPILER_REJECTS", "Compiler rejects document"],
      ["COMPILER_SUCCEEDS", "Compile and export"],
      ["PLAN_ARTIFACTS", "Plan artifact writing"],
      ["WRITE_ARTIFACTS", "Write artifacts"],
    ];

    const scenarios = [
      {
        name: "Resumable creation",
        description: "Watch Pack Assembly retain source-specific failures outside the semantic catalog, then let a later Package Tree supersede the failure. No Pack exists until authoritative construction succeeds.",
        steps: ["READ_PROJECT", "DISCOVER_MISSING", "RECORD_PACKAGE_FAILURE", "SUPPLY_PACKAGES", "CREATE_PACK"],
      },
      {
        name: "Representation split",
        description: "A valid Pack first fails an encoding-only limit, then encodes successfully. Writing can fail without losing exact retry bytes. Pack validity never absorbs ZIP limits or destination effects.",
        steps: ["READ_PROJECT", "SUPPLY_PACKAGES", "CREATE_PACK", "ENCODE_TOO_SMALL", "ENCODE_PACK", "WRITE_ARCHIVE_FAILURE", "WRITE_ARCHIVE"],
      },
      {
        name: "Decode boundary",
        description: "Raw archive ambiguity is rejected before domain interpretation. A separate valid reading reaches whole-Pack validation and establishes Pack Identity only after decoding submits canonical content.",
        steps: ["READ_ARCHIVE", "DECODE_AMBIGUOUS", "READ_ARCHIVE", "DECODE_PACK"],
      },
      {
        name: "Compilation precedence",
        description: "First prove semantic request rejection wins before fulfillment inspection. Then accept a request, observe an exact-set operational outcome without a World, and finally follow the successful confined path.",
        steps: ["READ_PROJECT", "SUPPLY_PACKAGES", "CREATE_PACK", "INVALID_COMPILE_REQUEST", "ACCEPT_COMPILE_REQUEST", "VERIFY_BAD_FULFILLMENTS", "ACCEPT_COMPILE_REQUEST", "VERIFY_FULFILLMENTS", "MATERIALIZE_WORLD", "COMPILER_SUCCEEDS", "PLAN_ARTIFACTS", "WRITE_ARTIFACTS"],
      },
      {
        name: "Extraction effects",
        description: "A semantic collision fails before destination access. A valid owned plan then survives independently and a concrete merge adapter reports truthful partial progress after atomic per-file commits.",
        steps: ["READ_PROJECT", "SUPPLY_PACKAGES", "CREATE_PACK", "PLAN_COLLIDING_EXTRACTION", "PLAN_EXTRACTION", "APPLY_EXTRACTION_PARTIAL"],
      },
      {
        name: "Migration order",
        description: "Advance through the implementation migration. Each slice is allowed only after the contracts that protect it are in place; try clicking a later migration slice in the Migration proof panel to see the dependency guard.",
        steps: Architecture.migration.map((_, index) => `MIGRATE:${index}`),
      },
    ];

    const routeSteps = [
      ["Read", "Concrete readers and authorities"],
      ["Validate", "Canonical semantic values"],
      ["Transform", "Creation, codec, plans, compilation"],
      ["Apply", "Concrete destination adapters"],
      ["Evidence", "Outcome, Report, Receipt, Progress"],
    ];

    let state = Architecture.initialState();
    let scenarioIndex = 0;
    let scenarioStep = 0;

    const $ = (id) => document.getElementById(id);

    function dispatch(encoded) {
      const [type, rawIndex] = encoded.split(":");
      const action = type === "MIGRATE" ? { type, index: Number(rawIndex) } : { type };
      state = Architecture.reduce(state, action);
      render();
    }

    function activeRouteIndex() {
      if (state.destinationEffects.length) return 4;
      if (state.extraction.includes("Plan") || state.compilation.includes("Plan") || state.archive.includes("bytes")) return 3;
      if (state.pack === "validated Pack" || state.compilation !== "none" || state.extraction !== "none") return 2;
      if (state.project !== "not read" || state.packages !== "empty catalog") return 1;
      return 0;
    }

    function renderRoute() {
      const active = activeRouteIndex();
      $("route").innerHTML = routeSteps.map(([name, copy], index) => `
        <div class="route-step ${index <= active ? "active" : ""}">
          <strong>${index + 1}. ${name}</strong>
          <small>${copy}</small>
        </div>
      `).join("");
      $("route-status").textContent = state.pack === "validated Pack" ? "Pack exists" : "No Pack yet";
    }

    function list(items, empty) {
      if (!items.length) return `<p>${empty}</p>`;
      return `<ul>${items.slice(-6).map((item) => `<li>${item}</li>`).join("")}</ul>`;
    }

    function renderState() {
      const cards = [
        ["Semantic values", `<p>Project: ${state.project}</p><p>Packages: ${state.packages}</p><p>Pack: ${state.pack}</p>`],
        ["Identity and representation", `<p>Pack Identity: ${state.packIdentity}</p><p>Archive: ${state.archive}</p>`],
        ["Compilation confinement", `<p>Compilation: ${state.compilation}</p><p>World: ${state.world}</p>`],
        ["Plans and effects", `<p>Extraction: ${state.extraction}</p>${list(state.destinationEffects, "No destination effects")}`],
        ["Ownership", `<p>${state.payloads}</p>`],
        ["Retained evidence", list(state.evidence, "No terminal evidence yet")],
      ];
      $("state-grid").innerHTML = cards.map(([title, body]) => `
        <article class="state-card"><h3>${title}</h3>${body}</article>
      `).join("");
      $("last-change").className = `last-change ${state.rejected ? "rejected" : ""}`;
      $("last-change").textContent = state.last;
    }

    function renderControls() {
      $("controls").innerHTML = actions.map(([type, label], index) => `
        <button class="action ${index === 0 ? "primary" : ""}" data-action="${type}">${label}</button>
      `).join("");
      $("controls").querySelectorAll("button").forEach((button) => {
        button.addEventListener("click", () => dispatch(button.dataset.action));
      });
    }

    function selectScenario(index) {
      scenarioIndex = index;
      scenarioStep = 0;
      state = Architecture.initialState();
      render();
    }

    function runScenarioStep(index) {
      if (index !== scenarioStep) return;
      dispatch(scenarios[scenarioIndex].steps[index]);
      scenarioStep += 1;
      renderScenario();
    }

    function stepLabel(encoded) {
      const [type, rawIndex] = encoded.split(":");
      if (type === "MIGRATE") return Architecture.migration[Number(rawIndex)].name;
      return actions.find(([action]) => action === type)?.[1] ?? type;
    }

    function renderScenario() {
      $("scenario-tabs").innerHTML = scenarios.map((scenario, index) => `
        <button class="tab ${index === scenarioIndex ? "selected" : ""}" data-scenario="${index}">${scenario.name}</button>
      `).join("");
      $("scenario-copy").textContent = scenarios[scenarioIndex].description;
      $("walkthrough").innerHTML = scenarios[scenarioIndex].steps.map((step, index) => `
        <button class="${index < scenarioStep ? "complete" : index === scenarioStep ? "current" : ""}"
                data-step="${index}" ${index > scenarioStep ? "disabled" : ""}>
          ${index + 1}. ${stepLabel(step)}
        </button>
      `).join("");
      $("scenario-tabs").querySelectorAll("button").forEach((button) => {
        button.addEventListener("click", () => selectScenario(Number(button.dataset.scenario)));
      });
      $("walkthrough").querySelectorAll("button").forEach((button) => {
        button.addEventListener("click", () => runScenarioStep(Number(button.dataset.step)));
      });
    }

    function renderMigration() {
      $("migration-status").textContent = `${state.migration} / ${Architecture.migration.length} slices`;
      $("migration-list").innerHTML = Architecture.migration.map((slice, index) => `
        <button class="migration-step ${index < state.migration ? "complete" : index === state.migration ? "next" : ""}"
                data-migration="${index}">
          <span class="index">${index + 1}</span>
          <span><strong>${slice.name}</strong><small>${slice.gate}</small></span>
        </button>
      `).join("");
      $("migration-list").querySelectorAll("button").forEach((button) => {
        button.addEventListener("click", () => dispatch(`MIGRATE:${button.dataset.migration}`));
      });
    }

    function render() {
      renderRoute();
      renderState();
      renderControls();
      renderScenario();
      renderMigration();
    }

    render();
  </script>
</body>
</html>