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
 
3fd9ca4
 
c2b7fe3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55c2c6a
 
 
 
 
 
 
 
 
 
c2b7fe3
 
 
 
 
 
21ad36a
3fd9ca4
 
c2b7fe3
 
21ad36a
 
c2b7fe3
 
 
 
 
 
 
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
import { useEffect, useMemo, useRef, useState } from 'react';
import {
  BrowserEngineClient,
  EngineClientError,
  evaluateModelContextPolicy,
  evaluateModelGate,
  loadModelManifestV2,
  type BenchmarkFlashMode,
  type BenchmarkKvCacheType,
  type BenchmarkWasmFlavor,
  type EngineCapabilities,
  type GenerateParams,
  type ModelManifestV2,
  type ModelTierId,
  type RequestedBackend,
} from '../engine';
import { formatBytes } from '../lib/format';
import {
  BENCHMARK_WORKLOADS,
  DEFAULT_BENCHMARK_WORKLOAD_ID,
  benchmarkReportFilename,
  buildBenchmarkReport,
  serializeBenchmarkReport,
  type BenchmarkReport,
  type BenchmarkWorkloadId,
  type TeacherForcedReferenceScoreEvidence,
} from './report';
import {
  STATE_DRIFT_REFERENCE_PATH,
  validateStateDriftReferenceFixture,
} from './state-drift-reference';

const MANIFEST_PATH = 'manifest/models.json';
const DEFAULT_MODEL: ModelTierId = '1_7b';
const MAX_GLUE_INT = 2_147_483_647;

interface RunProgress {
  label: string;
  loadedBytes: number;
  totalBytes: number;
}

let sharedBenchClient: BrowserEngineClient | null = null;

function getBenchClient(): BrowserEngineClient {
  sharedBenchClient ??= new BrowserEngineClient();
  return sharedBenchClient;
}

function manifestUrl(): string {
  return new URL(`${import.meta.env.BASE_URL}${MANIFEST_PATH}`, document.baseURI).href;
}

function stateDriftReferenceUrl(): string {
  return new URL(
    `${import.meta.env.BASE_URL}${STATE_DRIFT_REFERENCE_PATH}`,
    document.baseURI,
  ).href;
}

function errorMessage(error: unknown): string {
  if (error instanceof EngineClientError) return `${error.code}: ${error.message}`;
  return error instanceof Error ? error.message : String(error);
}

function isAbort(error: unknown, signal: AbortSignal): boolean {
  return signal.aborted
    || (error instanceof DOMException && error.name === 'AbortError')
    || (error instanceof EngineClientError && ['ABORTED', 'SHARD_DOWNLOAD_ABORTED'].includes(error.code));
}

function throwIfAborted(signal: AbortSignal): void {
  if (signal.aborted) throw new DOMException('Benchmark stopped by operator.', 'AbortError');
}

function parseOptionalInteger(value: string): number | null {
  if (value.trim() === '') return null;
  const parsed = Number(value);
  return Number.isSafeInteger(parsed) && parsed > 0 && parsed <= MAX_GLUE_INT ? parsed : null;
}

function formatDuration(value: number | null | undefined): string {
  if (value === null || value === undefined) return '—';
  return value >= 1_000 ? `${(value / 1_000).toFixed(2)} s` : `${value.toFixed(1)} ms`;
}

function formatRate(value: number | null | undefined): string {
  return value === null || value === undefined ? '—' : `${value.toFixed(2)} t/s`;
}

function downloadReport(report: BenchmarkReport): void {
  const blob = new Blob([serializeBenchmarkReport(report)], { type: 'application/json' });
  const url = URL.createObjectURL(blob);
  const anchor = document.createElement('a');
  anchor.href = url;
  anchor.download = benchmarkReportFilename(report);
  document.body.append(anchor);
  anchor.click();
  anchor.remove();
  window.setTimeout(() => URL.revokeObjectURL(url), 0);
}

export function BenchApp() {
  const [client] = useState(getBenchClient);
  const [manifest, setManifest] = useState<ModelManifestV2 | null>(null);
  const [capabilities, setCapabilities] = useState<EngineCapabilities | null>(null);
  const [workloadId, setWorkloadId] = useState<BenchmarkWorkloadId>(DEFAULT_BENCHMARK_WORKLOAD_ID);
  const [modelId, setModelId] = useState<ModelTierId>(DEFAULT_MODEL);
  const [backend, setBackend] = useState<RequestedBackend>('auto');
  const [contextSize, setContextSize] = useState(4_096);
  const [maxTokens, setMaxTokens] = useState(64);
  const [nBatchInput, setNBatchInput] = useState('');
  const [nUbatchInput, setNUbatchInput] = useState('');
  const [flashMode, setFlashMode] = useState<BenchmarkFlashMode>('off');
  const [kvCacheType, setKvCacheType] = useState<BenchmarkKvCacheType>('f16');
  const [wasmFlavor, setWasmFlavor] = useState<BenchmarkWasmFlavor>('auto');
  const [running, setRunning] = useState(false);
  const [progress, setProgress] = useState<RunProgress>({
    label: 'Waiting for an operator-initiated run',
    loadedBytes: 0,
    totalBytes: 0,
  });
  const [report, setReport] = useState<BenchmarkReport | null>(null);
  const [output, setOutput] = useState('');
  const [bootstrapError, setBootstrapError] = useState<string | null>(null);
  const [runError, setRunError] = useState<string | null>(null);
  const operationRef = useRef<AbortController | null>(null);

  useEffect(() => {
    let cancelled = false;
    const controller = new AbortController();
    setProgress((current) => ({ ...current, label: 'Inspecting manifest and browser runtime' }));
    void Promise.all([
      loadModelManifestV2(manifestUrl(), controller.signal),
      client.capabilities(),
    ]).then(([loadedManifest, loadedCapabilities]) => {
      if (cancelled) return;
      const initial = loadedManifest.models.find((model) => model.id === DEFAULT_MODEL)
        ?? loadedManifest.models[0];
      if (!initial) throw new Error('The benchmark manifest contains no model tiers.');
      setManifest(loadedManifest);
      setCapabilities(loadedCapabilities);
      setModelId(initial.id);
      setContextSize(initial.defaultContext);
      setProgress({
        label: 'Ready · no model is loaded until Run benchmark is pressed',
        loadedBytes: 0,
        totalBytes: 0,
      });
    }).catch((bootstrapError) => {
      if (cancelled || controller.signal.aborted) return;
      setBootstrapError(errorMessage(bootstrapError));
      setProgress((current) => ({ ...current, label: 'Runtime inspection failed' }));
    });
    return () => {
      cancelled = true;
      controller.abort();
    };
  }, [client]);

  const model = useMemo(
    () => manifest?.models.find((candidate) => candidate.id === modelId) ?? null,
    [manifest, modelId],
  );
  const workload = BENCHMARK_WORKLOADS[workloadId];
  const deviceGate = useMemo(() => {
    if (!model || !capabilities) return null;
    return evaluateModelGate(model, backend, capabilities.webgpu, {
      usageBytes: null,
      quotaBytes: null,
      persisted: capabilities.storage.persisted,
    });
  }, [backend, capabilities, model]);
  const storageGate = useMemo(() => {
    if (!model || !capabilities) return null;
    return evaluateModelGate(model, backend, capabilities.webgpu, capabilities.storage);
  }, [backend, capabilities, model]);

  const contextPolicy = model
    ? evaluateModelContextPolicy(model, contextSize, {
        tuningScope: 'benchmark',
        requestedBackend: backend,
        flashMode,
        kvCacheType,
      })
    : null;
  const contextLimit = contextPolicy?.limit ?? 4_096;
  const nBatch = parseOptionalInteger(nBatchInput);
  const nUbatch = parseOptionalInteger(nUbatchInput);
  const batchTuningError = nBatchInput.trim() !== '' && nBatch === null
    ? `nBatch must be an integer from 1 to ${MAX_GLUE_INT.toLocaleString()}.`
    : nUbatchInput.trim() !== '' && nUbatch === null
      ? `nUbatch must be an integer from 1 to ${MAX_GLUE_INT.toLocaleString()}.`
      : nBatch !== null && nUbatch !== null && nUbatch > nBatch
        ? 'nUbatch must not exceed nBatch.'
        : null;
  const runtimeTuningError = flashMode === 'off' && kvCacheType !== 'f16'
    ? 'Quantized KV requires Flash Attention auto.'
    : flashMode === 'auto' && backend !== 'webgpu'
      ? 'Flash Attention experiments require the explicit WebGPU backend.'
      : null;
  const wasmTuningError = wasmFlavor === 'jspi' && capabilities?.runtime.wasmFlavor === 'compat'
    ? 'JSPI is unavailable in this browser; use auto or compat.'
    : null;
  const workloadError = workload.requiredMaxTokens !== null && maxTokens !== workload.requiredMaxTokens
    ? `${workload.id} requires exactly ${workload.requiredMaxTokens.toLocaleString()} max tokens.`
    : null;
  const stateDriftReferenceError = workload.id === 'state-drift-1k-v1' && (
    model?.id !== '27b'
    || backend !== 'webgpu'
    || contextSize !== 2_048
    || nBatch !== 32
    || nUbatch !== 16
  )
    ? 'state-drift-1k-v1 reference scoring requires 27B, explicit WebGPU, context 2,048, nBatch 32, and nUbatch 16.'
    : null;
  const configurationTuningError = batchTuningError
    ?? runtimeTuningError
    ?? wasmTuningError
    ?? workloadError
    ?? stateDriftReferenceError;
  const configurationValid = contextPolicy?.allowed === true
    && Number.isSafeInteger(maxTokens)
    && maxTokens >= 8
    && maxTokens <= 1_024
    && configurationTuningError === null;
  const canRun = Boolean(manifest && capabilities && model && deviceGate?.allowed && configurationValid);

  const clearPreviousRun = () => {
    setReport(null);
    setOutput('');
    setRunError(null);
    if (manifest && capabilities) {
      setProgress({
        label: 'Ready · settings changed; run again to produce matching evidence',
        loadedBytes: 0,
        totalBytes: 0,
      });
    }
  };

  const handleModelChange = (nextId: ModelTierId) => {
    if (running || !manifest) return;
    const next = manifest.models.find((candidate) => candidate.id === nextId);
    if (!next) return;
    setModelId(nextId);
    setContextSize(next.defaultContext);
    if (!next.cpuFallback && backend === 'wasm') setBackend('auto');
    clearPreviousRun();
  };

  const runBenchmark = () => {
    if (!canRun || !manifest || !capabilities || !model || operationRef.current) return;
    const controller = new AbortController();
    operationRef.current = controller;
    setRunning(true);
    setReport(null);
    setOutput('');
    setRunError(null);
    const startedAt = new Date().toISOString();

    const run = async () => {
      let storagePersistent = capabilities.storage.persisted;
      setProgress({ label: 'Requesting persistent browser storage', loadedBytes: 0, totalBytes: 0 });
      try {
        const storage = await client.storagePersist();
        storagePersistent = storage.persisted;
        setCapabilities((current) => current ? { ...current, storage } : current);
      } catch {
        // Persistence is a browser policy choice. The runtime storage gate remains authoritative.
      }
      throwIfAborted(controller.signal);

      await client.unload();
      throwIfAborted(controller.signal);
      let coldCachedBytes: number | null = null;
      const loadParams = {
        manifest,
        modelId: model.id,
        backend,
        contextSize,
        benchmarkTuning: {
          ...(nBatch === null ? {} : { nBatch }),
          ...(nUbatch === null ? {} : { nUbatch }),
          flashMode,
          kvCacheType,
          wasmFlavor,
        },
      };

      const coldStarted = performance.now();
      const coldLoad = await client.loadModel(loadParams, {
        signal: controller.signal,
        onProgress: (event) => {
          if (event.phase === 'download' && coldCachedBytes === null) {
            coldCachedBytes = event.loadedBytes;
          }
          const shard = event.shardIndex === null ? '' : ` · shard ${event.shardIndex + 1}/${event.shardCount}`;
          setProgress({
            label: `Cold load · ${event.phase}${shard}`,
            loadedBytes: event.loadedBytes,
            totalBytes: event.totalBytes,
          });
        },
      });
      const coldLoadMs = performance.now() - coldStarted;
      throwIfAborted(controller.signal);

      setProgress({ label: 'Unloading model before verified-cache reload', loadedBytes: 0, totalBytes: 0 });
      await client.unload();
      throwIfAborted(controller.signal);

      const warmStarted = performance.now();
      const warmLoad = await client.loadModel(loadParams, {
        signal: controller.signal,
        onProgress: (event) => {
          const shard = event.shardIndex === null ? '' : ` · shard ${event.shardIndex + 1}/${event.shardCount}`;
          setProgress({
            label: `Warm load · ${event.phase}${shard}`,
            loadedBytes: event.loadedBytes,
            totalBytes: event.totalBytes,
          });
        },
      });
      const warmLoadMs = performance.now() - warmStarted;
      if (coldLoad.backend !== warmLoad.backend) {
        throw new Error(`Backend changed between cold and warm loads (${coldLoad.backend}${warmLoad.backend}).`);
      }
      if (coldLoad.tuning.observed.wasmFlavor !== warmLoad.tuning.observed.wasmFlavor) {
        throw new Error(
          `WASM flavor changed between cold and warm loads (${coldLoad.tuning.observed.wasmFlavor}${warmLoad.tuning.observed.wasmFlavor}).`,
        );
      }
      throwIfAborted(controller.signal);

      let firstTokenAt: number | null = null;
      let streamedTokenEvents = 0;
      const generationStarted = performance.now();
      const generationRequest = {
        messages: workload.messages.map((message) => ({ ...message })),
        maxTokens,
        temperature: 0,
        topK: 1,
        topP: 1,
        seed: 42,
        toolChoice: 'none',
        cachePrompt: false,
        returnTokenIds: true,
      } satisfies GenerateParams;
      setProgress({ label: `Generating fixed workload · ${workload.id}`, loadedBytes: 0, totalBytes: maxTokens });
      const generationResult = await client.generate(generationRequest, {
        signal: controller.signal,
        onToken: (text, reasoningDelta) => {
          if (!text && !reasoningDelta) return;
          if (firstTokenAt === null) firstTokenAt = performance.now();
          streamedTokenEvents += 1;
          setProgress({
            label: `Generating fixed prompt · ${streamedTokenEvents} stream event${streamedTokenEvents === 1 ? '' : 's'}`,
            loadedBytes: streamedTokenEvents,
            totalBytes: maxTokens,
          });
        },
      });
      const generationCompleted = performance.now();
      throwIfAborted(controller.signal);
      let teacherForcedReferenceScore: TeacherForcedReferenceScoreEvidence | null = null;
      if (workload.id === 'state-drift-1k-v1') {
        setProgress({
          label: 'Loading and validating pinned CPU reference',
          loadedBytes: 0,
          totalBytes: 1,
        });
        const fixtureResponse = await fetch(stateDriftReferenceUrl(), {
          signal: controller.signal,
          cache: 'no-cache',
        });
        if (!fixtureResponse.ok) {
          throw new Error(
            `CPU reference fixture request failed with HTTP ${fixtureResponse.status}.`,
          );
        }
        const fixture = validateStateDriftReferenceFixture(
          await fixtureResponse.json() as unknown,
          { model, capabilities, loadResult: warmLoad },
        );
        throwIfAborted(controller.signal);
        setProgress({
          label: 'Teacher-forcing the 1,024-token CPU reference',
          loadedBytes: 0,
          totalBytes: fixture.referenceTokenIds.length,
        });
        const score = await client.scoreSequence({
          promptTokenIds: fixture.promptTokenIds,
          referenceTokenIds: fixture.referenceTokenIds,
          topK: 5,
        }, { signal: controller.signal });
        teacherForcedReferenceScore = { fixture, score };
        throwIfAborted(controller.signal);
      }
      const backendReport = await client.backendReport();
      throwIfAborted(controller.signal);

      const nextReport = buildBenchmarkReport({
        startedAt,
        completedAt: new Date().toISOString(),
        manifest,
        model,
        capabilities,
        requestedBackend: backend,
        contextSize: warmLoad.context.size,
        workloadId,
        generationRequest,
        coldLoadMs,
        warmLoadMs,
        coldCachedBytes,
        storagePersistent,
        loadResult: warmLoad,
        generationResult,
        generationElapsedMs: generationCompleted - generationStarted,
        timeToFirstTokenMs: firstTokenAt === null ? null : firstTokenAt - generationStarted,
        streamedTokenEvents,
        backendReport,
        teacherForcedReferenceScore,
      });
      setReport(nextReport);
      setOutput(generationResult.text || generationResult.reasoningText);
      setProgress({
        label: 'Benchmark complete · JSON report is ready',
        loadedBytes: 1,
        totalBytes: 1,
      });
    };

    void run().catch((runError) => {
      if (isAbort(runError, controller.signal)) {
        setProgress({ label: 'Benchmark stopped by operator', loadedBytes: 0, totalBytes: 0 });
        return;
      }
      setRunError(errorMessage(runError));
      setProgress({ label: 'Benchmark failed safely · no report exported', loadedBytes: 0, totalBytes: 0 });
    }).finally(() => {
      if (operationRef.current === controller) operationRef.current = null;
      setRunning(false);
    });
  };

  const progressPercent = progress.totalBytes > 0
    ? Math.min(100, Math.max(0, progress.loadedBytes / progress.totalBytes * 100))
    : 0;
  const fullGateStorageOnly = storageGate && !storageGate.allowed && deviceGate?.allowed;

  return (
    <div className="bench-page station-frame">
      <header className="bench-masthead">
        <div className="brand-block" aria-label="Bonsai browser benchmark">
          <span className="brand-mark" aria-hidden="true"><span /></span>
          <div>
            <p className="eyebrow">Repeatable local inference</p>
            <h1>Bonsai <i>browser benchmark</i></h1>
          </div>
        </div>
        <div className="bench-masthead-note">
          <strong>Nothing runs on arrival</strong>
          <span>One click performs two loads, one fixed prompt, and no network call beyond model assets.</span>
        </div>
        <a className="bench-return" href={import.meta.env.BASE_URL}>Open chat <span aria-hidden="true"></span></a>
      </header>

      <div className="bench-layout">
        <aside className="bench-controls">
          <p className="eyebrow">Benchmark settings</p>
          <h2>Configure one run</h2>
          <p className="bench-control-intro">The fixed prompt and temperature stay locked so exported runs remain comparable.</p>

          <form onSubmit={(event) => { event.preventDefault(); runBenchmark(); }}>
            <label className="bench-field">
              <span>Workload</span>
              <select
                value={workloadId}
                disabled={running}
                onChange={(event) => {
                  const nextId = event.target.value as BenchmarkWorkloadId;
                  const next = BENCHMARK_WORKLOADS[nextId];
                  setWorkloadId(nextId);
                  setMaxTokens(next.requiredMaxTokens ?? 64);
                  clearPreviousRun();
                }}
              >
                {Object.values(BENCHMARK_WORKLOADS).map((candidate) => (
                  <option key={candidate.id} value={candidate.id}>{candidate.label}</option>
                ))}
              </select>
              <small>{workload.description}</small>
            </label>

            <label className="bench-field">
              <span>Model tier</span>
              <select
                value={modelId}
                disabled={running || !manifest}
                onChange={(event) => handleModelChange(event.target.value as ModelTierId)}
              >
                {(manifest?.models ?? []).map((candidate) => (
                  <option key={candidate.id} value={candidate.id}>{candidate.displayName}</option>
                ))}
              </select>
              <small>{model ? `${formatBytes(model.downloadBytes)} verified weights` : 'Loading manifest…'}</small>
            </label>

            <label className="bench-field">
              <span>Backend</span>
              <select
                value={backend}
                disabled={running}
                onChange={(event) => {
                  setBackend(event.target.value as RequestedBackend);
                  clearPreviousRun();
                }}
              >
                <option value="auto">Auto · gated</option>
                <option value="webgpu">WebGPU · explicit</option>
                <option value="wasm" disabled={model?.cpuFallback === false}>CPU-WASM</option>
              </select>
              <small>{deviceGate?.selectedBackend ? `Preflight selects ${deviceGate.selectedBackend}` : 'No backend selected'}</small>
            </label>

            <div className="bench-field-pair">
              <label className="bench-field">
                <span>Context</span>
                <input
                  type="number"
                  min="256"
                  max={contextLimit}
                  step="256"
                  value={contextSize}
                  disabled={running}
                  onChange={(event) => {
                    setContextSize(Number(event.target.value));
                    clearPreviousRun();
                  }}
                />
                <small>Max {contextLimit.toLocaleString()}</small>
              </label>
              <label className="bench-field">
                <span>Max tokens</span>
                <input
                  type="number"
                  min="8"
                  max="1024"
                  step="8"
                  value={maxTokens}
                  disabled={running}
                  onChange={(event) => {
                    setMaxTokens(Number(event.target.value));
                    clearPreviousRun();
                  }}
                />
                <small>8–1,024</small>
              </label>
            </div>

            <details className="bench-experimental">
              <summary>Experimental runtime tuning</summary>
              <p>Blank batch fields use the pinned runtime defaults. Every option is exported as an experiment, not a release recommendation.</p>
              <div className="bench-field-pair">
                <label className="bench-field">
                  <span>nBatch</span>
                  <input
                    type="number"
                    min="1"
                    max={MAX_GLUE_INT}
                    step="1"
                    placeholder="runtime default"
                    value={nBatchInput}
                    disabled={running}
                    onChange={(event) => {
                      setNBatchInput(event.target.value);
                      clearPreviousRun();
                    }}
                  />
                </label>
                <label className="bench-field">
                  <span>nUbatch</span>
                  <input
                    type="number"
                    min="1"
                    max={MAX_GLUE_INT}
                    step="1"
                    placeholder="runtime default"
                    value={nUbatchInput}
                    disabled={running}
                    onChange={(event) => {
                      setNUbatchInput(event.target.value);
                      clearPreviousRun();
                    }}
                  />
                </label>
              </div>
              <div className="bench-field-pair">
                <label className="bench-field">
                  <span>Flash Attention request</span>
                  <select
                    value={flashMode}
                    disabled={running}
                    onChange={(event) => {
                      setFlashMode(event.target.value as BenchmarkFlashMode);
                      clearPreviousRun();
                    }}
                  >
                    <option value="off">Off · default</option>
                    <option value="auto">Auto · observed on</option>
                  </select>
                </label>
                <label className="bench-field">
                  <span>KV cache K / V</span>
                  <select
                    value={kvCacheType}
                    disabled={running}
                    onChange={(event) => {
                      setKvCacheType(event.target.value as BenchmarkKvCacheType);
                      clearPreviousRun();
                    }}
                  >
                    <option value="f16">f16 / f16 · default</option>
                    <option value="q8_0">q8_0 / q8_0 · exp.</option>
                    <option value="q4_0">q4_0 / q4_0 · exp.</option>
                  </select>
                </label>
              </div>
              <label className="bench-field">
                <span>WASM runtime</span>
                <select
                  value={wasmFlavor}
                  disabled={running}
                  onChange={(event) => {
                    setWasmFlavor(event.target.value as BenchmarkWasmFlavor);
                    clearPreviousRun();
                  }}
                >
                  <option value="auto">Auto · browser-selected</option>
                  <option value="jspi">JSPI · explicit</option>
                  <option value="compat">Compat / Asyncify · explicit</option>
                </select>
                <small>Current auto path: {capabilities?.runtime.wasmFlavor ?? 'inspecting'}</small>
              </label>
              <dl className="bench-locks">
                <div><dt>Chat release</dt><dd>Flash off · KV f16/f16</dd></div>
                <div><dt>Evidence rule</dt><dd>native log must match or the run fails</dd></div>
              </dl>
            </details>

            {configurationTuningError && (
              <p className="bench-inline-warning" role="alert">{configurationTuningError}</p>
            )}

            <div className={`bench-gate ${deviceGate?.allowed ? 'pass' : 'fail'}`}>
              <strong>{deviceGate?.allowed ? 'Device preflight passed' : 'Device preflight blocked'}</strong>
              <span>
                {deviceGate?.reasons[0]
                  ?? (fullGateStorageOnly
                    ? 'Storage cannot be proven before cache inspection; the worker repeats the authoritative gate.'
                    : deviceGate?.warnings[0] ?? 'Waiting for capability inspection.')}
              </span>
            </div>

            <div className="bench-run-actions">
              <button className="bench-run-button" type="submit" disabled={!canRun || running}>
                {running ? 'Run in progress' : 'Run benchmark'}
              </button>
              {running && (
                <button
                  className="bench-stop-button"
                  type="button"
                  onClick={() => {
                    setProgress((current) => ({ ...current, label: 'Stopping benchmark…' }));
                    operationRef.current?.abort();
                  }}
                >
                  Stop
                </button>
              )}
            </div>
          </form>
        </aside>

        <main className="bench-workbench">
          <section className="bench-hero">
            <div>
              <p className="eyebrow ink">Phase 5 · operator evidence</p>
              <h2>Measure the whole <em>growth cycle.</em></h2>
            </div>
            <p>Cold load includes cache inspection and any missing download. Warm load repeats initialization after unload with verified shards retained. Generation uses one public, fixed prompt.</p>
          </section>

          <section className="bench-progress" aria-live="polite">
            <div><strong>{progress.label}</strong><span>{progress.totalBytes > 1 ? `${Math.round(progressPercent)}%` : ''}</span></div>
            <span className="bench-progress-track"><i style={{ width: `${progressPercent}%` }} /></span>
          </section>

          {bootstrapError && <div className="bench-error" role="alert"><strong>Runtime inspection failed</strong><span>{bootstrapError}</span></div>}
          {runError && <div className="bench-error" role="alert"><strong>Run rejected</strong><span>{runError}</span></div>}

          <section className="bench-core-section" aria-label="Benchmark measurements">
            <div className="bench-section-heading">
              <div><p className="eyebrow ink">Core sample</p><h3>Four boundaries, one run</h3></div>
              {report && <button type="button" onClick={() => downloadReport(report)}>Export JSON</button>}
            </div>
            <div className="bench-core-sample">
              <article>
                <span>Cold load</span>
                <strong>{formatDuration(report?.load.cold.durationMs)}</strong>
                <small>{report ? `${report.load.cold.cacheState} at start` : 'first boundary'}</small>
              </article>
              <article>
                <span>Warm load</span>
                <strong>{formatDuration(report?.load.warm.durationMs)}</strong>
                <small>verified cache retained</small>
              </article>
              <article>
                <span>TTFT</span>
                <strong>{formatDuration(report?.generation.timeToFirstTokenMs)}</strong>
                <small>request to first stream</small>
              </article>
              <article>
                <span>Decode</span>
                <strong>{formatRate(report?.generation.decodeTokensPerSecond)}</strong>
                <small>{report ? `${report.generation.completionTokens} completion tokens` : 'engine timing'}</small>
              </article>
            </div>
          </section>

          <section className="bench-detail-grid">
            <article className="bench-data-card">
              <p className="eyebrow ink">Throughput</p>
              <dl>
                <div><dt>Prompt</dt><dd>{formatRate(report?.generation.promptTokensPerSecond)}</dd></div>
                <div><dt>Decode</dt><dd>{formatRate(report?.generation.decodeTokensPerSecond)}</dd></div>
                <div><dt>Token count</dt><dd>{report?.generation.completionTokens ?? '—'}</dd></div>
                <div><dt>Finish</dt><dd>{report?.generation.finishReason ?? '—'}</dd></div>
                <div><dt>CPU-ref PPL</dt><dd>{report?.generation.teacherForcedReferenceScore?.score.summary.perplexity.toFixed(4) ?? '—'}</dd></div>
                <div><dt>CPU-ref mean NLL</dt><dd>{report?.generation.teacherForcedReferenceScore?.score.summary.meanNll.toFixed(6) ?? '—'}</dd></div>
              </dl>
            </article>
            <article className="bench-data-card">
              <p className="eyebrow ink">Graph tripwire</p>
              <dl>
                <div><dt>Graph splits</dt><dd>{report?.execution.graphSplits ?? '—'}</dd></div>
                <div><dt>CPU ops</dt><dd>{report?.execution.opsOnCpu ?? '—'}</dd></div>
                <div><dt>GPU layers</dt><dd>{report?.execution.gpuLayers ? `${report.execution.gpuLayers.offloaded}/${report.execution.gpuLayers.total}` : '—'}</dd></div>
                <div><dt>Backend</dt><dd>{report?.execution.selectedBackend.toUpperCase() ?? '—'}</dd></div>
              </dl>
            </article>
            <article className="bench-data-card bench-runtime-card">
              <p className="eyebrow ink">Runtime pins</p>
              <dl>
                <div><dt>Device</dt><dd>{report?.execution.device ?? capabilities?.webgpu.name ?? 'Inspecting…'}</dd></div>
                <div><dt>wllama</dt><dd title={report?.runtime.wllamaRevision}>{report?.runtime.wllamaRevision.slice(0, 12) ?? '—'}</dd></div>
                <div><dt>llama.cpp</dt><dd title={report?.runtime.llamaCppRevision}>{report?.runtime.llamaCppRevision.slice(0, 12) ?? '—'}</dd></div>
                <div><dt>Weights</dt><dd title={report?.runtime.manifestRevision ?? undefined}>{report?.runtime.manifestRevision?.slice(0, 12) ?? '—'}</dd></div>
                <div><dt>WASM</dt><dd>{report?.runtime.wasmFlavor.toUpperCase() ?? '—'}</dd></div>
              </dl>
            </article>
            <article className="bench-data-card bench-tuning-card">
              <p className="eyebrow ink">Native tuning</p>
              <dl>
                <div><dt>Applied</dt><dd>{report ? (report.load.tuning.applied ? 'yes' : 'no') : '—'}</dd></div>
                <div><dt>Flash</dt><dd>{report ? `${report.load.tuning.requested.flashMode} → ${String(report.load.tuning.observed.flashAttention)}` : '—'}</dd></div>
                <div><dt>K/V</dt><dd>{report ? `${report.load.tuning.observed.cacheTypeK}/${report.load.tuning.observed.cacheTypeV}` : '—'}</dd></div>
                <div><dt>KV bytes</dt><dd>{report?.load.tuning.observed.kvBufferBytes === null || report?.load.tuning.observed.kvBufferBytes === undefined ? '—' : formatBytes(report.load.tuning.observed.kvBufferBytes)}</dd></div>
              </dl>
            </article>
          </section>

          <section className="bench-output">
            <div>
              <p className="eyebrow ink">Fixed prompt</p>
              <h3>{workload.id}</h3>
              <p>{workload.messages.map((message) => message.content ?? '').join('\n')}</p>
            </div>
            <div>
              <p className="eyebrow ink">Generated output · visible text, or reasoning trace</p>
              <p data-testid="bench-generated-output">{output || 'Run the benchmark to inspect generated output.'}</p>
            </div>
          </section>
        </main>
      </div>
    </div>
  );
}