File size: 25,841 Bytes
c126239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Dedicated inference worker. Owns the Transformers.js model, tokenizer and
 * sentence embedder; runs the unified generation loop and all detectors.
 *
 * Pattern follows the official transformers.js-examples llama-3.2-webgpu
 * worker (singleton lazy-load + postMessage protocol), extended with a manual
 * autoregressive loop for custom sampling.
 */

import {
  AutoModel,
  AutoModelForCausalLM,
  AutoTokenizer,
  Tensor,
  env,
} from '@huggingface/transformers';
import { getModelSpec, EMBEDDING_MODEL, type ModelSpec } from '../model/model-adapter';
import { runGenerationLoop, entropyOf, softmaxT, type LoopModel } from '../model/generation-engine';
import { tokenizeWithOffsets } from '../model/tokenizer';
import { applyGreenBias } from '../watermark/kirchenbauer';
import { isGreenToken, kirchenbauerSeed } from '../utils/hashing';
import { deriveKeys, gumbelMaxChoose, type TextsealKeys } from '../watermark/textseal';
import {
  acceptSentence,
  assignCluster,
  assertCentroidsMatch,
  type Centroids,
} from '../watermark/ksemstamp';
import { detectKirchenbauer } from '../detectors/kirchenbauer-detector';
import { detectTextseal } from '../detectors/textseal-detector';
import { detectKsemstamp } from '../detectors/ksemstamp-detector';
import { splitSentences } from '../utils/sentences';
import { RandomStream } from '../utils/rng';
import type {
  WorkerRequest,
  WorkerResponse,
  DeviceInfo,
  AlgorithmParams,
  DistributionResult,
  NextTokenCandidate,
} from './worker-protocol';
import type {
  AlgorithmId,
  Detection,
  GenerationResult,
  GenerationTrace,
  SamplingConfig,
  SentenceTrace,
  StepTrace,
} from '../watermark/types';

const post = (msg: WorkerResponse) => (self as unknown as Worker).postMessage(msg);

env.allowLocalModels = false;

// ---------------------------------------------------------------------------
// Device detection
// ---------------------------------------------------------------------------

/** Minimal WebGPU surface (avoids a dependency on @webgpu/types). */
interface MinimalGPUAdapter {
  features: { has(name: string): boolean };
  info?: { vendor?: string; architecture?: string };
}
interface MinimalGPU {
  requestAdapter(): Promise<MinimalGPUAdapter | null>;
}

async function detectDevice(): Promise<DeviceInfo> {
  const nav = navigator as Navigator & { gpu?: MinimalGPU };
  let webgpuSupported = false;
  let shaderF16 = false;
  let adapterInfo: DeviceInfo['adapterInfo'];
  try {
    if (nav.gpu) {
      const adapter = await nav.gpu.requestAdapter();
      if (adapter) {
        webgpuSupported = true;
        shaderF16 = adapter.features.has('shader-f16');
        if (adapter.info) {
          adapterInfo = { vendor: adapter.info.vendor, architecture: adapter.info.architecture };
        }
      }
    }
  } catch {
    webgpuSupported = false;
  }
  const device = webgpuSupported ? 'webgpu' : 'wasm';
  const dtype = webgpuSupported ? (shaderF16 ? 'q4f16' : 'q4') : 'q4';
  return { webgpuSupported, shaderF16, device, dtype, adapterInfo };
}

// ---------------------------------------------------------------------------
// Model singleton
// ---------------------------------------------------------------------------

type Tok = Awaited<ReturnType<typeof AutoTokenizer.from_pretrained>>;
type LM = Awaited<ReturnType<typeof AutoModelForCausalLM.from_pretrained>>;

let spec: ModelSpec | null = null;
let tokenizer: Tok | null = null;
let model: LM | null = null;
let deviceInfo: DeviceInfo | null = null;
let embedder: { tokenizer: Tok; model: unknown } | null = null;
let centroids: Centroids | null = null;

async function loadModel(modelId: string): Promise<void> {
  const t0 = performance.now();
  spec = getModelSpec(modelId);
  deviceInfo = await detectDevice();
  const dtype = deviceInfo.device === 'webgpu' ? (deviceInfo.shaderF16 ? spec.dtype : 'q4') : 'q4';
  deviceInfo.dtype = dtype;

  const progress_callback = (p: {
    status: string;
    file?: string;
    progress?: number;
    loaded?: number;
    total?: number;
  }) => {
    if (p.status === 'progress' && p.file) {
      post({
        type: 'load-progress',
        file: p.file,
        progress: p.progress ?? 0,
        loadedMB: (p.loaded ?? 0) / 1e6,
        totalMB: (p.total ?? 0) / 1e6,
      });
    }
  };

  tokenizer = await AutoTokenizer.from_pretrained(spec.repo, { progress_callback });
  model = await AutoModelForCausalLM.from_pretrained(spec.repo, {
    dtype,
    device: deviceInfo.device,
    progress_callback,
  });

  // Warm-up: compile shaders with a single-token generation.
  try {
    const warm = tokenizer('a');
    const out = await (model as any)({ ...warm });
    disposeOutputs(out);
  } catch {
    /* warm-up is best-effort */
  }

  post({
    type: 'load-done',
    modelId,
    initMs: performance.now() - t0,
    info: deviceInfo,
  });
}

/**
 * The encoder is called through AutoModel rather than the feature-extraction
 * pipeline: EmbeddingGemma's ONNX graph already applies pooling and the dense
 * projection and returns `sentence_embedding` (L2-normalized), whereas the
 * pipeline would mean-pool hidden states and skip the projection.
 */
async function loadEmbedder(device: 'webgpu' | 'wasm'): Promise<{ tokenizer: Tok; model: unknown }> {
  return {
    tokenizer: await AutoTokenizer.from_pretrained(EMBEDDING_MODEL.repo),
    // q4 rather than q4f16: the half-precision build of this model returns
    // NaN embeddings on some adapters, and a NaN silently collapses every
    // sentence into cluster 0 instead of failing loudly.
    model: await AutoModel.from_pretrained(EMBEDDING_MODEL.repo, {
      dtype: EMBEDDING_MODEL.dtype,
      device,
    }),
  };
}

async function getEmbedder(): Promise<{ tokenizer: Tok; model: unknown }> {
  if (!embedder) {
    embedder = await loadEmbedder(deviceInfo?.device === 'webgpu' ? 'webgpu' : 'wasm');
  }
  return embedder;
}

async function getCentroids(): Promise<Centroids> {
  if (!centroids) {
    // Served from public/ at the site root (works in dev and in the built Space).
    const res = await fetch(
      new URL('centroids/embeddinggemma-k8.json', self.location.origin + '/'),
    );
    if (!res.ok) throw new Error(`Failed to load centroids: HTTP ${res.status}`);
    const loaded = (await res.json()) as Centroids;
    assertCentroidsMatch(loaded, {
      encoder: EMBEDDING_MODEL.repo,
      prefix: EMBEDDING_MODEL.clusteringPrefix,
      dtype: EMBEDDING_MODEL.dtype,
      dim: EMBEDDING_MODEL.dim,
    });
    centroids = loaded;
  }
  return centroids;
}

async function runEmbedder(
  enc: { tokenizer: Tok; model: unknown },
  text: string,
): Promise<number[]> {
  const inputs = await (enc.tokenizer as any)([EMBEDDING_MODEL.clusteringPrefix + text], {
    padding: true,
  });
  const out = await (enc.model as any)(inputs);
  const emb = out.sentence_embedding as Tensor;
  const data = Array.from(emb.data as Float32Array);
  disposeOutputs(out);
  for (const v of Object.values(inputs as Record<string, unknown>)) {
    (v as { dispose?: () => void })?.dispose?.();
  }
  return data;
}

/** True once we have proven the current encoder returns usable numbers. */
let embedderChecked = false;

async function embedText(text: string): Promise<number[]> {
  let enc = await getEmbedder();
  let data = await runEmbedder(enc, text);

  // A NaN here would not throw; it would quietly make every sentence land in
  // the same cluster and every k-SemStamp verdict meaningless. Catch it once
  // and fall back to the CPU build, which is slower but correct.
  if (!embedderChecked) {
    if (data.some((x) => !Number.isFinite(x))) {
      if (deviceInfo?.device === 'webgpu') {
        embedder = await loadEmbedder('wasm');
        enc = embedder;
        data = await runEmbedder(enc, text);
      }
      if (data.some((x) => !Number.isFinite(x))) {
        throw new Error('Sentence encoder returned non-finite embeddings on this device.');
      }
    }
    embedderChecked = true;
  }
  return data;
}

// ---------------------------------------------------------------------------
// LoopModel adapter over Transformers.js
// ---------------------------------------------------------------------------

function disposeOutputs(outputs: unknown): void {
  if (!outputs || typeof outputs !== 'object') return;
  for (const v of Object.values(outputs as Record<string, unknown>)) {
    (v as { dispose?: () => void })?.dispose?.();
  }
}

function disposePastObject(past: unknown): void {
  if (!past || typeof past !== 'object') return;
  for (const v of Object.values(past as Record<string, unknown>)) {
    (v as { dispose?: () => void })?.dispose?.();
  }
}

function makeLoopModel(): LoopModel {
  if (!model || !tokenizer) throw new Error('Model not loaded');
  const tok = tokenizer;
  const lm = model as any;
  const eos: number[] = [];
  const eosCfg = lm.generation_config?.eos_token_id ?? lm.config?.eos_token_id;
  if (Array.isArray(eosCfg)) eos.push(...eosCfg.map(Number));
  else if (eosCfg != null) eos.push(Number(eosCfg));

  let vocabSize = 0;

  return {
    get vocabSize() {
      return vocabSize;
    },
    eosTokenIds: eos,
    decode(ids: number[]): string {
      return tok.decode(ids, { skip_special_tokens: true });
    },
    async forward(inputIds: number[], fullSeqLen: number, past: unknown) {
      const n = inputIds.length;
      const pastLen = fullSeqLen - n;
      const input_ids = new Tensor('int64', BigInt64Array.from(inputIds.map(BigInt)), [1, n]);
      const attention_mask = new Tensor('int64', new BigInt64Array(fullSeqLen).fill(1n), [
        1,
        fullSeqLen,
      ]);
      const position_ids = new Tensor(
        'int64',
        BigInt64Array.from({ length: n }, (_, i) => BigInt(pastLen + i)),
        [1, n],
      );
      const inputs: Record<string, unknown> = { input_ids, attention_mask, position_ids };
      if (past) inputs.past_key_values = past;

      const outputs = await lm.forward(inputs);
      const logitsT = outputs.logits as Tensor;
      const [, seqLen, vocab] = logitsT.dims as number[];
      vocabSize = vocab;
      const all = logitsT.data as Float32Array;
      const logits = new Float32Array(vocab);
      logits.set(all.subarray((seqLen - 1) * vocab, seqLen * vocab));

      // getPastKeyValues moves present.* into a past object and disposes the
      // previous step's GPU buffers.
      const newPast = lm.getPastKeyValues(outputs, past ?? null);
      (logitsT as any).dispose?.();
      input_ids.dispose?.();
      attention_mask.dispose?.();
      position_ids.dispose?.();
      return { logits, past: newPast };
    },
    disposePast(past: unknown): void {
      disposePastObject(past);
    },
  };
}

/** Full-sequence forward to get per-position entropies (for TextSeal weighted detection). */
async function computeEntropies(tokenIds: number[], temperature: number): Promise<number[]> {
  if (!model) throw new Error('Model not loaded');
  if (tokenIds.length < 2 || tokenIds.length > 512) return [];
  const lm = model as any;
  const n = tokenIds.length;
  const input_ids = new Tensor('int64', BigInt64Array.from(tokenIds.map(BigInt)), [1, n]);
  const attention_mask = new Tensor('int64', new BigInt64Array(n).fill(1n), [1, n]);
  const position_ids = new Tensor(
    'int64',
    BigInt64Array.from({ length: n }, (_, i) => BigInt(i)),
    [1, n],
  );
  const outputs = await lm.forward({ input_ids, attention_mask, position_ids });
  const logitsT = outputs.logits as Tensor;
  const [, seqLen, vocab] = logitsT.dims as number[];
  const all = logitsT.data as Float32Array;
  // Entropy of the distribution predicting token i (from logits at i-1).
  const entropies: number[] = [0];
  for (let i = 1; i < seqLen; i++) {
    const row = all.subarray((i - 1) * vocab, i * vocab) as Float32Array;
    entropies.push(entropyOf(softmaxT(row, temperature)));
  }
  disposeOutputs(outputs);
  input_ids.dispose?.();
  attention_mask.dispose?.();
  position_ids.dispose?.();
  return entropies;
}

// ---------------------------------------------------------------------------
// Generation per algorithm
// ---------------------------------------------------------------------------

function buildPromptIds(prompt: string): number[] {
  if (!tokenizer || !spec) throw new Error('Model not loaded');
  const messages = [{ role: 'user', content: prompt }];
  const templated = (tokenizer as any).apply_chat_template(messages, {
    tokenize: false,
    add_generation_prompt: true,
    ...(spec.chatTemplateOptions ?? {}),
  }) as string;
  const enc = (tokenizer as any).encode(templated, { add_special_tokens: false }) as number[];
  return Array.from(enc, Number);
}

/** Per-algorithm secrets (decimal strings from the UI) -> bigint keys. */
interface SecretKeys {
  kirchenbauer: bigint;
  ksemstamp: bigint;
  textseal: TextsealKeys;
}

function toKeys(keys?: { kirchenbauer?: string; ksemstamp?: string; textseal?: string }): SecretKeys {
  return {
    kirchenbauer: keys?.kirchenbauer ? BigInt(keys.kirchenbauer) : 0n,
    ksemstamp: keys?.ksemstamp ? BigInt(keys.ksemstamp) : 0n,
    textseal: deriveKeys(keys?.textseal ?? 'default'),
  };
}

/** Top-N next-token distribution for the wizard's Stage 1 / Stage 2 views. */
async function nextTokenDistribution(
  prompt: string,
  topN: number,
  temperature: number,
): Promise<DistributionResult> {
  const loopModel = makeLoopModel();
  const promptIds = buildPromptIds(prompt);
  let past: unknown = null;
  try {
    const { logits, past: p } = await loopModel.forward(promptIds, promptIds.length, null);
    past = p;
    const probs = softmaxT(logits, temperature);
    const idx = Array.from(probs.keys());
    idx.sort((a, b) => probs[b] - probs[a]);
    const candidates: NextTokenCandidate[] = idx.slice(0, topN).map((v) => ({
      tokenId: v,
      text: loopModel.decode([v]),
      logit: logits[v],
      prob: probs[v],
    }));
    const prevTokenId = promptIds[promptIds.length - 1];
    return {
      prevTokenId,
      prevTokenText: loopModel.decode([prevTokenId]),
      prevTokenIds: promptIds.slice(-8),
      vocabSize: logits.length,
      candidates,
    };
  } finally {
    loopModel.disposePast(past);
  }
}

async function detectAll(
  text: string,
  sampling: SamplingConfig,
  params: AlgorithmParams,
  withEntropy: boolean,
  secrets: SecretKeys,
): Promise<{ kirchenbauer: Detection; textseal: Detection; ksemstamp: Detection | null }> {
  if (!tokenizer) throw new Error('Model not loaded');
  const tok = tokenizeWithOffsets(
    {
      encode: (t, o) => Array.from((tokenizer as any).encode(t, o) as number[], Number),
      decode: (ids, o) => (tokenizer as any).decode(ids, o) as string,
    },
    text,
  );
  const keys = secrets.textseal;

  const kirch = detectKirchenbauer(tok, params.kirchenbauer, true, secrets.kirchenbauer);

  let entropies: number[] | undefined;
  if (withEntropy) {
    try {
      entropies = await computeEntropies(tok.tokenIds, sampling.temperature);
      if (entropies.length === 0) entropies = undefined;
    } catch {
      entropies = undefined;
    }
  }
  const seal = detectTextseal(tok, keys, params.textseal, { entropies });

  let ksem: Detection | null = null;
  try {
    const sentences = splitSentences(text);
    if (sentences.length >= 2) {
      const cents = await getCentroids();
      const embeddings: number[][] = [];
      for (const s of sentences) embeddings.push(await embedText(s.text));
      ksem = detectKsemstamp(sentences, embeddings, cents, params.ksemstamp, secrets.ksemstamp);
    }
  } catch (e) {
    ksem = null;
  }

  return { kirchenbauer: kirch, textseal: seal, ksemstamp: ksem };
}

async function generate(
  requestId: number,
  algorithm: AlgorithmId,
  prompt: string,
  sampling: SamplingConfig,
  params: AlgorithmParams,
  secrets: SecretKeys,
): Promise<GenerationResult> {
  if (!spec || !deviceInfo) throw new Error('Model not loaded');
  const loopModel = makeLoopModel();
  const promptIds = buildPromptIds(prompt);
  const keys = secrets.textseal;
  const sentenceTraces: SentenceTrace[] = [];
  const notes: string[] = [];

  /** Set by the Kirchenbauer hook just before each step is sampled. */
  let stepSeed = 0n;
  /** Text of each accepted token; the engine already decoded them in context. */
  const pieces: string[] = [];

  const onToken = (step: StepTrace) => {
    // index is authoritative: a k-SemStamp rollback moves it backwards.
    pieces.length = step.index;
    pieces.push(step.chosenTokenText);
    post({
      type: 'token',
      requestId,
      algorithm,
      index: step.index,
      text: step.chosenTokenText,
      green:
        algorithm === 'kirchenbauer'
          ? isGreenToken(
              stepSeed,
              step.chosenTokenId,
              params.kirchenbauer.gamma,
              secrets.kirchenbauer,
            )
          : undefined,
      keyId: step.keyId,
      entropy: step.entropy,
    });
    if (step.index % 8 === 0) {
      post({
        type: 'generate-progress',
        requestId,
        algorithm,
        text: pieces.join(''),
        tokensDone: step.index + 1,
        retries: 0,
      });
    }
  };

  const hooks: Parameters<typeof runGenerationLoop>[3] = {};
  if (algorithm === 'kirchenbauer') {
    hooks.transformLogits = (_i, ctx, logits) => {
      stepSeed = applyGreenBias(
        logits,
        ctx[ctx.length - 1],
        params.kirchenbauer,
        secrets.kirchenbauer,
      );
      return stepSeed;
    };
    notes.push('Shares the base RNG stream with Baseline (same seed, same uniform consumption).');
  } else if (algorithm === 'textseal') {
    const router = new RandomStream(BigInt(sampling.baseSeed) + 7777n);
    hooks.sampleOverride = (_i, ctx, probs) =>
      gumbelMaxChoose(probs, ctx, keys, params.textseal, router);
    notes.push(
      'Sampling is replaced by deterministic Gumbel-max (PRF-driven); the base RNG stream cannot be shared with Baseline.',
    );
  } else if (algorithm === 'ksemstamp') {
    const cents = await getCentroids();
    // Seed chain with the prompt's semantic cluster.
    let prevCluster = 0;
    try {
      const promptSents = splitSentences(prompt);
      const seedText = promptSents.length > 0 ? promptSents[promptSents.length - 1].text : prompt;
      prevCluster = assignCluster(await embedText(seedText), cents).clusterId;
    } catch {
      prevCluster = 0;
    }
    let retriesSoFar = 0;
    hooks.maxSentenceTrials = params.ksemstamp.maxTrials;
    hooks.onSentenceEnd = async (text, sentenceIndex, attempt) => {
      const emb = await embedText(text);
      const res = acceptSentence(emb, prevCluster, cents, params.ksemstamp, secrets.ksemstamp);
      const entry: SentenceTrace = {
        sentenceIndex,
        attempt,
        text,
        clusterId: res.assignment.clusterId,
        targetClusters: res.targets,
        distances: res.assignment.distances,
        margin: res.assignment.margin,
        accepted: res.accepted || attempt >= params.ksemstamp.maxTrials,
        rejectionReason: res.accepted
          ? undefined
          : attempt >= params.ksemstamp.maxTrials
            ? 'maxTrials'
            : res.reason,
      };
      sentenceTraces.push(entry);
      // Stream each judged candidate so the UI can show rejection sampling live.
      post({ type: 'candidate', requestId, algorithm, sentence: entry });
      if (res.accepted || attempt >= params.ksemstamp.maxTrials) {
        prevCluster = res.assignment.clusterId;
        retriesSoFar = 0;
        return { accept: true };
      }
      retriesSoFar++;
      post({
        type: 'generate-progress',
        requestId,
        algorithm,
        text: '',
        tokensDone: 0,
        retries: retriesSoFar,
      });
      return { accept: false };
    };
    notes.push(
      'Sentence-level rejection sampling re-generates candidates; RNG consumption differs from Baseline by construction.',
    );
    notes.push(
      'Simplified vs paper: general-purpose EmbeddingGemma encoder (no paraphrase-contrastive fine-tuning), K-means centroids from a small public corpus.',
    );
  }

  const t0 = performance.now();
  const loop = await runGenerationLoop(
    loopModel,
    promptIds,
    {
      temperature: sampling.temperature,
      topP: sampling.topP,
      maxNewTokens: sampling.maxNewTokens,
      baseSeed: sampling.baseSeed,
      topKTrace: 8,
      retryTemperatureStep: algorithm === 'ksemstamp' ? 0.12 : 0,
      onToken,
    },
    hooks,
  );
  const totalMs = performance.now() - t0;

  // Annotate Kirchenbauer trace candidates with green membership.
  if (algorithm === 'kirchenbauer') {
    let prev = promptIds[promptIds.length - 1];
    for (const step of loop.steps) {
      const seed = kirchenbauerSeed(prev);
      step.seed = String(seed);
      step.chosenIsGreen = isGreenToken(
        seed,
        step.chosenTokenId,
        params.kirchenbauer.gamma,
        secrets.kirchenbauer,
      );
      let greens = 0;
      for (const c of step.topCandidates) {
        c.isGreen = isGreenToken(seed, c.tokenId, params.kirchenbauer.gamma, secrets.kirchenbauer);
        if (c.isGreen) greens++;
      }
      step.greenFraction = step.topCandidates.length > 0 ? greens / step.topCandidates.length : 0;
      prev = step.chosenTokenId;
    }
  }

  const trace: GenerationTrace = {
    algorithm,
    steps: loop.steps,
    sentences: sentenceTraces.length > 0 ? sentenceTraces : undefined,
    vocabSize: loopModel.vocabSize,
    params: {
      ...(algorithm === 'kirchenbauer' ? params.kirchenbauer : {}),
      ...(algorithm === 'ksemstamp' ? params.ksemstamp : {}),
      ...(algorithm === 'textseal' ? params.textseal : {}),
    } as Record<string, number>,
    notes,
  };

  const detections = await detectAll(loop.text, sampling, params, false, secrets);
  /**
   * Never substitute another algorithm's Detection: it would travel into the
   * JSON export labelled as this algorithm's score.
   */
  const unscored: Detection = {
    algorithm,
    statistic: 0,
    statisticName: 'z-score',
    statisticDefinition: 'not enough sentences to score (at least two are needed)',
    threshold: Infinity,
    watermarked: false,
    unitsScored: 0,
    unitName: 'sentences',
    notes: ['The generation was too short to judge sentence by sentence.'],
  };
  const own: Detection =
    algorithm === 'textseal'
      ? detections.textseal
      : algorithm === 'ksemstamp'
        ? (detections.ksemstamp ?? unscored)
        : detections.kirchenbauer;

  return {
    algorithm,
    text: loop.text,
    tokenIds: loop.tokenIds,
    trace,
    detection: own,
    timings: {
      totalMs,
      tokensPerSec: loop.tokenIds.length / (totalMs / 1000),
      retries: loop.retries,
    },
    metadata: {
      model: spec.repo,
      dtype: deviceInfo.dtype,
      device: deviceInfo.device,
      algorithm,
      keyId: sampling.secret,
      seed: sampling.baseSeed,
      temperature: sampling.temperature,
      topP: sampling.topP,
      maxNewTokens: sampling.maxNewTokens,
      params: trace.params,
    },
  };
}

// ---------------------------------------------------------------------------
// Message handling (serialized: one generation at a time)
// ---------------------------------------------------------------------------

let busy: Promise<void> = Promise.resolve();

self.addEventListener('message', (ev: MessageEvent<WorkerRequest>) => {
  const msg = ev.data;
  busy = busy.then(async () => {
    try {
      if (msg.type === 'check') {
        post({ type: 'check-result', info: await detectDevice() });
      } else if (msg.type === 'load') {
        try {
          await loadModel(msg.modelId);
        } catch (e) {
          post({ type: 'load-error', error: String(e instanceof Error ? e.message : e) });
        }
      } else if (msg.type === 'distribution') {
        try {
          const result = await nextTokenDistribution(msg.prompt, msg.topN, msg.temperature);
          post({ type: 'distribution-done', requestId: msg.requestId, result });
        } catch (e) {
          post({
            type: 'distribution-error',
            requestId: msg.requestId,
            error: String(e instanceof Error ? e.message : e),
          });
        }
      } else if (msg.type === 'generate') {
        try {
          const secrets = toKeys({
            kirchenbauer: msg.algorithm === 'kirchenbauer' ? msg.secretKey : undefined,
            ksemstamp: msg.algorithm === 'ksemstamp' ? msg.secretKey : undefined,
            textseal: msg.algorithm === 'textseal' ? msg.secretKey : undefined,
          });
          const result = await generate(
            msg.requestId,
            msg.algorithm,
            msg.prompt,
            msg.sampling,
            msg.params,
            secrets,
          );
          post({ type: 'generate-done', requestId: msg.requestId, result });
        } catch (e) {
          post({
            type: 'generate-error',
            requestId: msg.requestId,
            algorithm: msg.algorithm,
            error: String(e instanceof Error ? (e.stack ?? e.message) : e),
          });
        }
      } else if (msg.type === 'detect') {
        try {
          const detections = await detectAll(
            msg.text,
            msg.sampling,
            msg.params,
            msg.withEntropy ?? false,
            toKeys(msg.keys),
          );
          post({ type: 'detect-done', requestId: msg.requestId, detections });
        } catch (e) {
          post({
            type: 'detect-error',
            requestId: msg.requestId,
            error: String(e instanceof Error ? e.message : e),
          });
        }
      }
    } catch (e) {
      // Last-resort: never let the queue die.
      console.error('worker error', e);
    }
  });
});