File size: 9,722 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 | <script lang="ts">
/**
* The baseline is generated once per prompt and then held fixed. It mirrors
* whatever stage the watermark panel is on, so every claim on the right has
* an unwatermarked control on the left.
*/
import {
baseline,
wizards,
activeMethod,
algoParams,
baselineCollapsed,
biasStepIndex,
editorDetect,
requestDetect,
METHOD_META,
} from '../lib/stores/app-state';
import { kirchenbauerSeed, greenUniform, keyFromSeed } from '../lib/utils/hashing';
import { isGreen } from '../lib/watermark/kirchenbauer';
import ProbBars from './shared/ProbBars.svelte';
import type { BarRow } from './shared/ProbBars.svelte';
const method = $derived($activeMethod);
const wizard = $derived($wizards[method]);
const stage = $derived(wizard.stage);
const dist = $derived($baseline.distribution);
const result = $derived($baseline.result);
const stageLabel = $derived(METHOD_META[method].stages[stage - 1]);
const BKEY = 'baseline';
const bundle = $derived($editorDetect[BKEY]?.detections ?? null);
// Score the baseline text with the same secrets whenever we reach a stage
// that compares classifications.
$effect(() => {
const text = result?.text;
if (!text) return;
if (stage >= 3) requestDetect(BKEY, text, false, 0);
});
const kirchDet = $derived(bundle?.kirchenbauer ?? null);
const ksemDet = $derived(bundle?.ksemstamp ?? null);
const sealDet = $derived(bundle?.textseal ?? null);
/** Mirror the exact step the watermark panel is illustrating. */
const steps = $derived(result?.trace.steps ?? []);
const stepIdx = $derived(Math.min($biasStepIndex, Math.max(0, steps.length - 1)));
const mirroredStep = $derived(steps[stepIdx]);
const barRows = $derived.by((): BarRow[] => {
if (mirroredStep) {
return mirroredStep.topCandidates.map((c) => ({
tokenId: c.tokenId,
text: c.tokenText,
before: c.prob,
kind: 'neutral' as const,
}));
}
if (!dist) return [];
return dist.candidates.slice(0, 12).map((c) => ({
tokenId: c.tokenId,
text: c.text,
before: c.prob,
kind: 'neutral' as const,
}));
});
const greenSummary = $derived.by(() => {
if (method !== 'kirchenbauer' || !kirchDet) return null;
const units = kirchDet.perUnit ?? [];
const scored = units.filter((u) => !u.skipped);
const green = scored.filter((u) => u.green).length;
return { green, total: scored.length };
});
</script>
<section class="panel base" class:collapsed={$baselineCollapsed}>
<header>
<div>
<strong>Baseline</strong>
<span class="badge">no watermark</span>
</div>
<button class="toggle" onclick={() => baselineCollapsed.update((v) => !v)}>
{$baselineCollapsed ? 'show' : 'hide'}
</button>
</header>
{#if !$baselineCollapsed}
<div class="mirror small dim">Mirroring stage {stage}: {stageLabel}</div>
{#if $baseline.status === 'idle'}
<p class="dim">Enter a prompt above and generate the baseline to begin.</p>
{:else if $baseline.status === 'running'}
<p class="text-box stream">{$baseline.streamingText || 'generating…'}</p>
{:else if $baseline.status === 'error'}
<p class="err small mono">{$baseline.error}</p>
{:else if result}
{#if stage === 1}
<!--
Nothing to mirror here: there is no secret on this side, so there is
no split, no cluster rule and no R values. Showing a greyed-out
version of the watermark's artefacts would invent a comparison that
does not exist. The text itself is the control.
-->
<p class="dim small">
Written once for this prompt and then left alone — the control the watermarked text on the
right is measured against.
</p>
<p class="text-box">{result.text}</p>
{:else if stage === 2}
{#if method === 'ksemstamp'}
<p class="dim small">
The baseline wrote every sentence in one pass. Nothing was embedded, judged or thrown
away — the cost the watermark on the right is paying does not exist here.
</p>
<ol class="text-box list sentences">
{#each result.text.split(/(?<=[.!?])\s+/).filter(Boolean) as s}
<li><span class="badge">kept</span><span>{s}</span></li>
{/each}
</ol>
{:else}
<p class="dim small">
The unmodified distribution at step {stepIdx} — this is what the watermark on the right
is transforming. Baseline sampling picked
<span class="mono">"{mirroredStep?.chosenTokenText ?? ''}"</span> here.
</p>
<ProbBars
rows={barRows}
chosenTokenId={mirroredStep?.chosenTokenId ?? null}
showAfter={false}
/>
{/if}
{:else if stage === 3 || stage === 4}
{#if method === 'kirchenbauer'}
<p class="dim small">
The same secret applied to unwatermarked text. Green appears only at the rate chance
allows.
</p>
{#if kirchDet}
<div class="text-box">
{#each kirchDet.perUnit ?? [] as u}
<span class={u.skipped ? 'tok-skip' : u.green ? 'tok-green' : 'tok-red'}
>{u.label}</span
>
{/each}
</div>
{#if greenSummary}
<div class="small dim">
{greenSummary.green} of {greenSummary.total} scored tokens green
(<span class="mono"
>{((greenSummary.green / Math.max(1, greenSummary.total)) * 100).toFixed(0)}%</span
>, expected {Math.round($algoParams.kirchenbauer.gamma * 100)}%)
</div>
{/if}
{:else}
<p class="dim small">scoring…</p>
{/if}
{:else if method === 'ksemstamp'}
<p class="dim small">Baseline sentences judged against the same valid regions.</p>
{#if ksemDet}
<ol class="text-box list sentences">
{#each ksemDet.perUnit ?? [] as u}
<li class={u.skipped ? '' : u.green ? 'valid' : 'invalid'}>
<span class="badge {u.skipped ? '' : u.green ? 'ok' : 'bad'}">
{u.skipped ? 'seed' : u.green ? 'valid' : 'invalid'}
</span>
<span>{u.label}</span>
</li>
{/each}
</ol>
{:else}
<p class="dim small">needs at least two sentences…</p>
{/if}
{:else if sealDet}
<p class="dim small">The same key over unwatermarked text: evidence stays flat.</p>
<div class="text-box plain mono heat">
{#each sealDet.perUnit ?? [] as u}
{@const s = u.score ?? 0}
<span
style="background: rgba(29,95,208,{Math.min(0.55, 0.05 + 0.14 * s).toFixed(2)})"
title={`s=${s.toFixed(2)}`}>{u.label}</span
>
{/each}
</div>
{:else}
<p class="dim small">scoring…</p>
{/if}
{#if stage === 4}
<div class="scores">
{#each [['Kirchenbauer', kirchDet], ['k-SemStamp', ksemDet], ['TextSeal', sealDet]] as const as [label, d]}
<div class="score">
<div class="small dim">{label}</div>
{#if d}
<div class="mono stat">{d.statistic.toFixed(2)}</div>
<span class="badge {d.inconclusive ? 'warn' : d.watermarked ? 'ok' : 'bad'}">
{d.inconclusive ? 'inconclusive' : d.watermarked ? 'detected' : 'not detected'}
</span>
{:else}
<div class="dim small">n/a</div>
{/if}
</div>
{/each}
</div>
{/if}
{/if}
{#if stage === 2}
<details class="raw">
<summary class="small dim">baseline text</summary>
<p class="text-box">{result.text}</p>
</details>
{/if}
<div class="meta small dim mono">
{result.tokenIds.length} tok · {(result.timings.totalMs / 1000).toFixed(1)}s ·
{result.timings.tokensPerSec.toFixed(1)} tok/s
</div>
{/if}
{/if}
</section>
<style>
.base {
display: flex;
flex-direction: column;
gap: 10px;
min-width: 0;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
}
.toggle {
font-size: 12px;
padding: 2px 8px;
}
.mirror {
border-bottom: 1px solid var(--border);
padding-bottom: 6px;
}
.heat {
font-size: 13px;
line-height: 2;
}
.stream {
opacity: 0.75;
}
.sentences {
font-size: 13.5px;
}
.sentences li {
display: flex;
gap: 8px;
align-items: baseline;
padding: 5px 9px;
border: 1px solid var(--border);
border-radius: 6px;
background: var(--panel-2);
}
.sentences li.valid {
background: var(--green-bg);
border-color: var(--green-border);
}
.sentences li.invalid {
background: var(--red-bg);
border-color: var(--red-border);
}
.scores {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
border-top: 1px solid var(--border);
padding-top: 8px;
}
.score {
background: var(--panel-2);
border: 1px solid var(--border);
border-radius: 6px;
padding: 6px 8px;
}
.stat {
font-size: 17px;
font-weight: 650;
}
.meta {
border-top: 1px solid var(--border);
padding-top: 6px;
}
.err {
color: var(--red);
}
</style>
|