firstpass-proxy 0.9.1

Drop-in, Anthropic-compatible LLM proxy that routes each request to the cheapest model that provably passes a quality gate, escalates on failure, and records a tamper-evident audit trace.
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<title>Firstpass — panel</title>
<style>
  :root {
    --bg: #f6f8fa; --surface: #ffffff; --surface2: #eef2f6; --line: #d5dce4;
    --text: #14202b; --dim: #55646f;
    --ok: #0a7d5f; --warn: #94600a; --bad: #b3263f; --info: #1f5fbf;
    --ok-bg: #dff5ec; --warn-bg: #fdf0d5; --bad-bg: #fbe3e8; --info-bg: #e1ecfb;
    --focus: #1f5fbf;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #0c1116; --surface: #141b22; --surface2: #1b252f; --line: #2a3643;
      --text: #e6edf3; --dim: #93a3b2;
      --ok: #19e3b1; --warn: #f5a524; --bad: #f4506a; --info: #6aa8ff;
      --ok-bg: #0f2e27; --warn-bg: #33270d; --bad-bg: #351620; --info-bg: #14263f;
      --focus: #6aa8ff;
    }
  }
  * { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; }
  body {
    margin: 0; background: var(--bg); color: var(--text);
    font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
    overflow-x: hidden;
  }
  .mono, code, pre, .hash { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
  :focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
  .skip { position: absolute; left: -999px; top: 0; background: var(--surface); padding: 8px 12px; z-index: 20; }
  .skip:focus { left: 8px; top: 8px; }
  header.top {
    display: flex; flex-wrap: wrap; align-items: center; gap: 8px 20px;
    padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--surface);
  }
  .brand { font-weight: 700; letter-spacing: .02em; }
  .brand b { color: var(--ok); }
  nav[role=tablist] { display: flex; gap: 4px; flex-wrap: wrap; }
  nav button {
    font: inherit; color: var(--dim); background: none; border: 1px solid transparent;
    padding: 6px 12px; border-radius: 6px; cursor: pointer; min-height: 36px;
  }
  nav button[aria-selected=true] { color: var(--text); background: var(--surface2); border-color: var(--line); font-weight: 600; }
  .live { margin-left: auto; color: var(--dim); font-size: 12px; display: flex; align-items: center; gap: 6px; }
  .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dim); display: inline-block; }
  .dot.on { background: var(--ok); }
  .dot.err { background: var(--bad); }
  main { padding: 16px; max-width: 1100px; margin: 0 auto; }
  h2 { font-size: 16px; margin: 0 0 4px; }
  .lede { color: var(--dim); margin: 0 0 16px; max-width: 70ch; }
  .tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
  .tile { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; min-width: 0; }
  .tile .k { color: var(--dim); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
  .tile .v { font-size: 22px; margin-top: 2px; overflow-wrap: anywhere; }
  .tile .s { color: var(--dim); font-size: 12px; }
  .v.pos { color: var(--ok); } .v.neg { color: var(--warn); }
  .box { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; margin-bottom: 14px; min-width: 0; }
  .mix { display: flex; height: 12px; border-radius: 6px; overflow: hidden; background: var(--surface2); margin: 8px 0; }
  .mix span { display: block; height: 100%; }
  .mix .p { background: var(--ok); } .mix .f { background: var(--bad); } .mix .a { background: var(--warn); }
  .legend { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 13px; }
  .pill {
    display: inline-block; padding: 1px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
    border: 1px solid currentColor; white-space: nowrap;
  }
  .pill.undecided { color: var(--info); background: var(--info-bg); }
  .pill.certified { color: var(--ok); background: var(--ok-bg); }
  .pill.halted { color: var(--bad); background: var(--bad-bg); }
  .pill.unavailable { color: var(--warn); background: var(--warn-bg); }
  .pass { color: var(--ok); } .fail { color: var(--bad); } .abstain { color: var(--warn); }
  .card h3 { margin: 0; font-size: 15px; }
  .card .hd { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; justify-content: space-between; }
  .ladder { color: var(--dim); font-size: 13px; overflow-wrap: anywhere; margin: 4px 0 10px; }
  .wrap { overflow-x: auto; max-width: 100%; }
  table { border-collapse: collapse; width: 100%; font-size: 13px; }
  th, td { text-align: left; padding: 4px 10px 4px 0; border-bottom: 1px solid var(--line); white-space: nowrap; }
  th { color: var(--dim); font-weight: 600; }
  .bar { height: 10px; background: var(--surface2); border-radius: 5px; overflow: hidden; border: 1px solid var(--line); }
  .bar > i { display: block; height: 100%; background: var(--info); }
  .bar.cert > i { background: var(--ok); } .bar.halt > i { background: var(--bad); }
  .ev { margin: 8px 0; }
  .ev .l { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-size: 13px; }
  .note { background: var(--surface2); border-radius: 6px; padding: 8px 12px; margin: 10px 0; overflow-wrap: anywhere; }
  .note.adopt { background: var(--ok-bg); border: 1px solid var(--ok); }
  pre { margin: 6px 0; padding: 10px; background: var(--surface2); border-radius: 6px; overflow-x: auto; max-width: 100%; font-size: 12.5px; }
  button.btn, select, input[type=password] {
    font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
    border-radius: 6px; padding: 5px 10px; min-height: 34px;
  }
  button.btn { cursor: pointer; }
  button.btn:hover { background: var(--surface2); }
  details { margin: 6px 0; } summary { cursor: pointer; color: var(--dim); }
  .filters { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: end; margin-bottom: 12px; }
  .filters label { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--dim); }
  .rows { display: grid; gap: 6px; }
  .row {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; width: 100%; text-align: left;
    font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
    border-radius: 8px; padding: 8px 12px; cursor: pointer;
  }
  .row:hover { background: var(--surface2); }
  .row .id { overflow-wrap: anywhere; color: var(--dim); font-size: 12px; }
  .row .meta { color: var(--dim); font-size: 12px; text-align: right; }
  .badge { font-size: 11px; padding: 0 7px; border-radius: 4px; border: 1px solid var(--line); color: var(--dim); }
  .badge.challenger { color: var(--info); background: var(--info-bg); border-color: var(--info); }
  .empty { border: 1px dashed var(--line); border-radius: 8px; padding: 20px; color: var(--dim); }
  .empty h3 { color: var(--text); margin: 0 0 6px; font-size: 15px; }
  .banner { border: 1px solid var(--warn); background: var(--warn-bg); border-radius: 8px; padding: 12px 16px; margin-bottom: 14px; }
  .banner.err { border-color: var(--bad); background: var(--bad-bg); }
  .banner form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
  .hidden { display: none; }
  #drawer {
    position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100%); background: var(--surface);
    border-left: 1px solid var(--line); box-shadow: -8px 0 24px rgba(0,0,0,.25); padding: 16px;
    overflow-y: auto; z-index: 10; transition: transform .15s ease;
  }
  #drawer[hidden] { display: none; }
  #drawer .close { float: right; }
  .kv { display: grid; grid-template-columns: 8.5em minmax(0, 1fr); gap: 2px 10px; font-size: 13px; }
  .kv dt { color: var(--dim); } .kv dd { margin: 0; overflow-wrap: anywhere; }
  .att { border-top: 1px solid var(--line); padding: 8px 0; }
  @media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; scroll-behavior: auto !important; } }
</style>
</head>
<body>
<a class="skip" href="#main">Skip to content</a>
<header class="top">
  <div class="brand"><b>firstpass</b> panel</div>
  <nav role="tablist" aria-label="Panel sections">
    <button role="tab" id="tab-overview" aria-controls="view-overview" aria-selected="true" data-view="overview">Overview</button>
    <button role="tab" id="tab-experiments" aria-controls="view-experiments" aria-selected="false" data-view="experiments" tabindex="-1">Experiments</button>
    <button role="tab" id="tab-receipts" aria-controls="view-receipts" aria-selected="false" data-view="receipts" tabindex="-1">Receipts</button>
  </nav>
  <div class="live" role="status" aria-live="polite"><span class="dot" id="dot"></span><span id="livetxt">connecting</span></div>
</header>

<main id="main">
  <div id="banner"></div>

  <section id="view-overview" role="tabpanel" aria-labelledby="tab-overview">
    <h2>Overview</h2>
    <p class="lede">Every routing decision this proxy made for your tenant, and what it cost against always calling the top rung.</p>
    <div class="tiles" id="tiles" aria-live="off"></div>
    <div class="box" id="mixbox"></div>
  </section>

  <section id="view-experiments" role="tabpanel" aria-labelledby="tab-experiments" class="hidden">
    <h2>Experiments</h2>
    <p class="lede">A route with a <code>[route.challenger]</code> tests a cheaper model against your current one: in live mode a random share of sessions is served by it and you report task outcomes; in paired mode it re-runs sampled requests off-path and your gates grade both answers. Firstpass never switches models on its own: it stops sending traffic to a challenger that is proven worse, and shows you the change to make when one is proven good enough.</p>
    <div id="exp-list"></div>
  </section>

  <section id="view-receipts" role="tabpanel" aria-labelledby="tab-receipts" class="hidden">
    <h2>Receipts</h2>
    <p class="lede">Newest first. Select a receipt for every attempt, gate verdict, cost, latency and its place in the hash chain.</p>
    <div class="filters" role="group" aria-label="Filter receipts">
      <label>Served rung<select id="f-rung"><option value="">any</option></select></label>
      <label>Verdict<select id="f-verdict"><option value="">any</option><option value="pass">pass</option><option value="fail">fail</option><option value="abstain">abstain</option><option value="none">none served</option></select></label>
      <label>Experiment arm<select id="f-arm"><option value="">any</option><option value="incumbent">incumbent</option><option value="challenger">challenger</option><option value="none">no experiment</option></select></label>
      <span id="f-count" class="lede" style="margin:0"></span>
    </div>
    <div class="rows" id="rows"></div>
  </section>
</main>

<aside id="drawer" role="dialog" aria-modal="false" aria-labelledby="drawer-title" hidden tabindex="-1">
  <button class="btn close" id="drawer-close" type="button">Close</button>
  <h2 id="drawer-title">Receipt</h2>
  <div id="drawer-body"></div>
</aside>

<script>
  'use strict';
  // Sign goes outside the symbol: "-$0.0027" reads as money lost, "$-0.0027" reads as a typo.
  const usd = n => (Number(n || 0) < 0 ? '-$' : '$') + Math.abs(Number(n || 0)).toFixed(4);
  const num = n => Number(n || 0).toLocaleString();
  const pct = (a, b) => (Number(b) > 0 ? Math.round(Number(a) / Number(b) * 100) + '%' : 'n/a');

  // Receipt and experiment fields are ATTACKER-CONTROLLED. `model` is echoed from the caller's
  // request body on the observe path and stored in the trace, so anyone who can send one request
  // through this proxy can plant markup that runs when an operator opens this page. Everything
  // interpolated into innerHTML below goes through here, or through one of the numeric formatters
  // above (which coerce with Number), or through an `h_` renderer that is itself built from these.
  const esc = s => String(s ?? '').replace(/[&<>"']/g, c =>
    ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c]);

  const cls = v => ({ pass: 'pass', fail: 'fail', abstain: 'abstain' })[String(v).toLowerCase()] || '';
  const $ = id => document.getElementById(id);

  const SNIPPET = '[route.challenger]\nladder  = ["openrouter/minimax-m2.5"]\npercent = 10\n# then restart the proxy';
  const FEEDBACK = 'curl -s localhost:8080/v1/feedback -d \'{"trace_id":"<id>","gate_id":"tests","verdict":"pass","reporter":"ci"}\'';

  const state = { receipts: [], summary: null, experiments: null, view: 'overview', openId: null, key: '', sig: '' };
  try { state.key = sessionStorage.getItem('fp-key') || ''; } catch (e) { /* storage may be blocked */ }

  // ---- fetching -------------------------------------------------------------------------------
  async function get(path) {
    const headers = state.key ? { Authorization: 'Bearer ' + state.key } : {};
    const res = await fetch(path, { headers, cache: 'no-store' });
    if (res.status === 401) { const e = new Error('auth'); e.auth = true; throw e; }
    if (!res.ok) throw new Error('http ' + res.status);
    return res.json();
  }

  let timer = null;
  async function tick() {
    timer = null;
    try {
      const [r, x] = await Promise.all([get('/v1/receipts?limit=200'), get('/v1/experiments')]);
      state.receipts = r.receipts || [];
      state.summary = r.summary || null;
      state.experiments = x;
      setLive('live', 'on');
      banner(null);
      const sig = JSON.stringify([r, x]);
      if (sig !== state.sig) { state.sig = sig; render(); }
    } catch (e) {
      if (e && e.auth) { setLive('auth required', 'err'); banner('auth'); }
      else { setLive('unreachable', 'err'); banner('down'); }
    }
    schedule();
  }
  // Poll every 2s, but not while the tab is hidden: a background tab should not keep a proxy busy.
  function schedule() {
    if (timer !== null || document.hidden) return;
    timer = setTimeout(tick, 2000);
  }
  document.addEventListener('visibilitychange', () => {
    if (document.hidden) { if (timer !== null) { clearTimeout(timer); timer = null; } setLive('paused (tab hidden)', ''); }
    else if (timer === null) { tick(); }
  });

  function setLive(text, kind) {
    $('livetxt').textContent = text;
    $('dot').className = 'dot' + (kind ? ' ' + kind : '');
  }

  function banner(kind) {
    const el = $('banner');
    if (kind === 'auth') {
      if (el.dataset.k === 'auth') return;
      el.dataset.k = 'auth';
      el.innerHTML = `<div class="banner" role="alert"><b>This proxy requires a tenant key (401).</b>
        Receipts and experiments are per-tenant data, so they stay behind your key; the page itself carries none.
        Paste the key (<code>tenant.secret</code>) to view them. It is kept in this tab only (session storage) and sent to this proxy only.
        <form id="keyform"><label for="keyin" class="lede" style="margin:0">Tenant key</label>
        <input id="keyin" type="password" autocomplete="off" aria-label="Tenant key" required>
        <button class="btn" type="submit">Use key</button></form></div>`;
      $('keyform').addEventListener('submit', ev => {
        ev.preventDefault();
        state.key = $('keyin').value.trim();
        try { sessionStorage.setItem('fp-key', state.key); } catch (e) { /* ignore */ }
        state.sig = ''; el.dataset.k = ''; tick();
      });
    } else if (kind === 'down') {
      if (el.dataset.k === 'down') return;
      el.dataset.k = 'down';
      el.innerHTML = '<div class="banner err" role="alert"><b>Cannot reach the proxy.</b> Is <code>firstpass up</code> still running? This page retries every 2 seconds.</div>';
    } else { el.dataset.k = ''; el.innerHTML = ''; }
  }

  // ---- overview -------------------------------------------------------------------------------
  function tile(k, v, s, tone) {
    return `<div class="tile"><div class="k">${esc(k)}</div><div class="v ${esc(tone || '')}">${esc(v)}</div><div class="s">${esc(s || '')}</div></div>`;
  }
  // A fail on an answer that stopped at max_tokens may describe the cap rather than the model.
  const h_cut = n => (Number(n) > 0
    ? `<div class="lede" style="margin:8px 0 0">${esc(num(n))} answers stopped at the output-token limit (max_tokens). A gate fail on a cut-off answer may describe the cap, not the model.</div>` : '');

  function h_overview() {
    const s = state.summary;
    if (!s || !s.requests) {
      $('tiles').innerHTML = '';
      $('mixbox').innerHTML = `<div class="empty"><h3>No receipts yet</h3>Send a request through the proxy to see it here. Fastest way: <code>firstpass kiosk</code> (or <code>firstpass demo</code> with no API key).</div>`;
      return;
    }
    // Escalating past the top rung genuinely costs MORE than going straight there. Showing that
    // loss in the same green as a win would make the panel flatter the product it is auditing.
    const saved = Number(s.saved_usd);
    $('tiles').innerHTML = [
      tile('requests', num(s.requests), 'all time, this tenant'),
      tile('spent', usd(s.spent_usd), 'what you paid'),
      tile('baseline', usd(s.baseline_usd), 'always calling the top rung'),
      tile('saved', usd(saved), saved < 0 ? 'negative: escalation cost more than going straight to the top' : pct(saved, s.baseline_usd) + ' of baseline', saved < 0 ? 'neg' : 'pos'),
      tile('first-pass rate', pct(s.first_pass, s.requests), num(s.first_pass) + ' served at rung 0'),
      tile('escalation rate', pct(s.escalated, s.requests), num(s.escalated) + ' climbed at least one rung'),
    ].join('');
    const g = s.gates || {};
    const total = (g.pass || 0) + (g.fail || 0) + (g.abstain || 0);
    const h_w = n => (total ? (Number(n) / total * 100).toFixed(2) : 0);
    $('mixbox').innerHTML = `<h2>Gate verdicts</h2>
      <div class="lede" style="margin:0">Every gate run across every attempt. Fails are what trigger an escalation; abstains are never treated as passes.</div>
      <div class="mix" role="img" aria-label="Gate verdict mix: ${esc(num(g.pass))} pass, ${esc(num(g.fail))} fail, ${esc(num(g.abstain))} abstain">
        <span class="p" style="width:${h_w(g.pass)}%"></span><span class="f" style="width:${h_w(g.fail)}%"></span><span class="a" style="width:${h_w(g.abstain)}%"></span></div>
      <div class="legend"><span class="pass">pass ${esc(num(g.pass))} (${esc(pct(g.pass, total))})</span>
        <span class="fail">fail ${esc(num(g.fail))} (${esc(pct(g.fail, total))})</span>
        <span class="abstain">abstain ${esc(num(g.abstain))} (${esc(pct(g.abstain, total))})</span></div>${h_cut(s.truncated)}`;
  }

  // ---- experiments ----------------------------------------------------------------------------
  const EXPLAIN = {
    undecided: 'Not enough evidence either way yet. Keep running and keep posting task outcomes: one per session. Nothing is decided and nothing changes.',
    certified: 'The challenger is no worse than your current model by more than the margin, with error at most alpha, however often you looked. If it is also cheaper, the change to adopt is below. You make that edit; Firstpass never switches models on its own.',
    halted: 'The challenger is worse than your current model by more than the margin. Firstpass has already stopped sending it new sessions (automatic: it only ever moves traffic back to your ladder). Nothing for you to do.',
    unavailable: 'The receipt history could not be read at startup, so Firstpass cannot tell whether this challenger was already halted. It fails closed: every session is served by your current ladder until the store is readable and the proxy restarted.',
  };
  // Paired mode has no feedback call and never serves a session from the challenger, so the two
  // states whose advice is about sessions and outcomes read differently there.
  const EXPLAIN_PAIRED = {
    undecided: 'Not enough evidence either way yet. Keep sending traffic: each sampled request adds one pair, graded by your gates. Nothing is decided and nothing changes.',
    halted: 'The challenger is worse than your current model by more than the margin. Firstpass has already stopped re-running it on your requests. Nothing for you to do.',
  };
  const explain = (status, conf) => (isPaired(conf) && EXPLAIN_PAIRED[status]) || EXPLAIN[status] || '';
  const EVIDENCE ='Evidence is an e-value: a running bet against "the challenger is too different". It starts at 1 and the test decides when it reaches 1/alpha (20 at the default alpha 0.05). The bar is log scale. Because it is anytime-valid you may read it as often as you like without inflating the error rate.';

  function h_evbar(label, e, target, kind) {
    const v = Number(e) || 0;
    const frac = v <= 1 || target <= 1 ? 0 : Math.min(1, Math.log(v) / Math.log(target));
    const p = Math.round(frac * 100);
    return `<div class="ev"><div class="l"><span title="${esc(EVIDENCE)}">${esc(label)}</span><span class="mono">${esc(v >= 1000 ? '>=1000' : v.toFixed(2))} of ${esc(target.toFixed(0))} needed</span></div>
      <div class="bar ${esc(kind)}" role="progressbar" aria-label="${esc(label)}" aria-valuemin="0" aria-valuemax="100" aria-valuenow="${esc(p)}"><i style="width:${esc(p)}%"></i></div></div>`;
  }
  function rate(a) { return a.outcomes > 0 ? (a.outcome_sum / a.outcomes) : null; }
  function cpr(a) { return a.requests > 0 ? (a.cost_usd / a.requests) : null; }
  const fpct = v => (v === null ? 'n/a' : (v * 100).toFixed(1) + '%');
  const fusd = v => (v === null ? 'n/a' : '$' + v.toFixed(6));

  function h_arm(name, a) {
    return `<tr><td>${esc(name)}</td><td>${esc(num(a.outcomes))}</td><td>${esc(fpct(rate(a)))}</td><td>${esc(num(a.requests))}</td><td>${esc(fusd(cpr(a)))}</td></tr>`;
  }
  function adoptToml(v) {
    return '# firstpass.toml: route ' + v.route_ix + '\nladder = [' + (v.challenger_ladder || []).map(m => JSON.stringify(String(m))).join(', ') + ']';
  }
  const h_delta = d => (d === null ? '' : ' (' + esc(d) + '% $/request)');
  function h_card(v, conf) {
    const r = v.report;
    const status = conf && conf.unavailable ? 'unavailable' : r.status;
    const target = 1 / (r.alpha || 0.05);
    const ci = cpr(r.incumbent), cc = cpr(r.challenger);
    const delta = ci && cc !== null ? Math.round((cc - ci) / ci * 100) : null;
    let h_rec = '';
    if (status === 'certified' && v.recommendation === 'adopt') {
      h_rec = `<div class="note adopt"><b>Adopt it.</b> Certified non-inferior and cheaper${h_delta(delta)}. Replace this route's ladder with the challenger's, then remove <code>[route.challenger]</code>:
        <pre id="toml-${esc(v.route_ix)}">${esc(adoptToml(v))}</pre>
        <button class="btn" type="button" data-copy="toml-${esc(v.route_ix)}">Copy</button></div>`;
    } else if (status === 'certified') {
      h_rec = '<div class="note">Certified, but not measurably cheaper per request, so there is nothing to gain. Keep your current ladder.</div>';
    }
    const h_stale = v.ignored_stale > 0
      ? `<div class="note">${esc(num(v.ignored_stale))} receipts from an earlier experiment configuration were ignored. Changing either ladder, the margin or alpha starts a fresh experiment.</div>` : '';
    const hh = v.health || {};
    const h_lines = [['current', hh.incumbent], ['challenger', hh.challenger]]
      .filter(([, a]) => a && (a.errors > 0 || a.truncated > 0))
      .map(([name, a]) => name + ': ' + num(a.errors) + ' errors, ' + num(a.truncated) + ' answers cut off at max_tokens');
    const h_health = h_lines.length
      ? `<div class="note"><b>Check before trusting the rates.</b> ${esc(h_lines.join('; '))}. Errors and cut-off answers cluster on long, hard requests, so neither is random. A current-model error drops its pair, a challenger error counts as a failure, and a cut-off answer is graded as it is.${esc(isPaired(conf) ? ' Challenger counts cover this process only.' : '')}</div>` : '';
    return `<article class="box card" aria-label="Experiment, route ${esc(v.route_ix)}">
      <div class="hd"><h3>Route ${esc(v.route_ix)}</h3><span class="pill ${esc(status)}">${esc(status)}</span></div>
      <div class="ladder">current: ${esc((conf ? conf.incumbent_ladder : []).join(' > ') || 'your ladder')} &nbsp;|&nbsp; challenger: ${esc((v.challenger_ladder || []).join(' > '))}</div>
      ${h_mode(conf)}
      <p style="margin:0 0 8px">${esc(explain(status, conf))}</p>
      <div class="wrap"><table><caption class="lede" style="text-align:left;margin:0">${esc(num(r.pairs))} matched pairs. Margin ${esc(r.margin)}, alpha ${esc(r.alpha)}.${esc(isPaired(conf) ? ' Success is gate pass on the same request.' : '')}</caption>
        <thead><tr><th>arm</th><th>${esc(isPaired(conf) ? 'requests compared' : 'sessions with outcome')}</th><th>success</th><th>requests</th><th>$/request</th></tr></thead>
        <tbody>${h_arm('current', r.incumbent)}${h_arm('challenger', r.challenger)}</tbody></table></div>
      ${h_evbar('Evidence the challenger is good enough', r.e_certify, target, 'cert')}
      ${h_evbar('Evidence the challenger is too weak', r.e_halt, target, 'halt')}
      <details><summary>What is evidence?</summary><div class="lede">${esc(EVIDENCE)}</div></details>
      ${h_rec}${h_health}${h_stale}
    </article>`;
  }
  const isPaired = conf => !!conf && conf.mode === 'paired';
  // How the challenger gets its evidence, in one line: a share of sessions (live) or a sample of
  // requests re-run off-path under a daily cap (paired).
  function modeLine(conf) {
    if (!conf) return '';
    if (!isPaired(conf)) return 'live: ' + Number(conf.percent) + '% of sessions served by the challenger';
    const p = conf.paired || {};
    const cap = p.max_usd_per_day === null || p.max_usd_per_day === undefined ? 'no cap' : usd(p.max_usd_per_day);
    return 'paired: ' + Math.round(Number(p.sample_rate) * 100) + '% of requests re-run on the challenger, ' + usd(p.spent_today_usd) + ' of ' + cap + ' spent today (UTC)';
  }
  const h_mode = conf => (conf ? `<div class="ladder">${esc(modeLine(conf))}</div>` : '');
  const h_feedbackpre = conf => (conf.unavailable || isPaired(conf) ? '' : `<pre>${esc(FEEDBACK)}</pre>`);
  const WAIT_LIVE = 'Configured, but no outcomes have been recorded yet. Send traffic, then post one task outcome per session to the feedback endpoint:';
  const WAIT_PAIRED = 'Configured in paired mode, no pairs yet. After each sampled request is answered, the challenger runs on the same request and both are graded by your gates. No feedback call is needed, and no caller is ever served by the challenger.';
  function h_waiting(conf) {
    const status = conf.unavailable ? 'unavailable' : 'undecided';
    return `<article class="box card" aria-label="Experiment, route ${esc(conf.route_ix)}">
      <div class="hd"><h3>Route ${esc(conf.route_ix)}</h3><span class="pill ${esc(status)}">${esc(status)}</span></div>
      <div class="ladder">current: ${esc(conf.incumbent_ladder.join(' > '))} &nbsp;|&nbsp; challenger: ${esc(conf.challenger_ladder.join(' > '))}</div>
      ${h_mode(conf)}
      <p style="margin:0 0 8px">${esc(conf.unavailable ? EXPLAIN.unavailable : (isPaired(conf) ? WAIT_PAIRED : WAIT_LIVE))}</p>
      ${h_feedbackpre(conf)}
    </article>`;
  }
  function h_experiments() {
    const x = state.experiments;
    const views = (x && x.experiments) || [], conf = (x && x.configured) || [];
    const byIx = new Map(conf.map(c => [c.route_ix, c]));
    const seen = new Set(views.map(v => v.route_ix));
    const cards = views.map(v => h_card(v, byIx.get(v.route_ix))).concat(conf.filter(c => !seen.has(c.route_ix)).map(h_waiting));
    if (!cards.length) {
      return `<div class="empty"><h3>No experiments yet</h3>
        Find out if a cheaper model is good enough on <i>your</i> traffic. Add four lines to a route in <code>firstpass.toml</code>:
        <pre>${esc(SNIPPET)}</pre>
        Then post task outcomes (CI result, test run, human rating) to <code>/v1/feedback</code> as you already do. Read the result here or with <code>firstpass experiment</code>. Typical length: a few thousand sessions for a close call, a few hundred for a clearly worse model.</div>`;
    }
    return cards.join('');
  }

  // ---- receipts -------------------------------------------------------------------------------
  function servedAttempt(r) { return (r.attempts || []).find(a => a.rung === r.served_rung); }
  function servedVerdict(r) { const a = servedAttempt(r); return a ? String(a.verdict).toLowerCase() : 'none'; }
  function armOf(r) { return r.experiment ? r.experiment.arm : 'none'; }
  function visible() {
    const fr = $('f-rung').value, fv = $('f-verdict').value, fa = $('f-arm').value;
    return state.receipts.filter(r =>
      (fr === '' || String(r.served_rung ?? 'none') === fr) && (fv === '' || servedVerdict(r) === fv) && (fa === '' || armOf(r) === fa));
  }
  function h_badge(r) {
    return r.experiment ? `<span class="badge ${esc(r.experiment.arm)}">experiment: ${esc(r.experiment.arm)}</span>` : '';
  }
  function h_rows(list) {
    return list.map(r => `<button class="row" type="button" data-id="${esc(r.trace_id)}" aria-label="Open receipt ${esc(r.trace_id)}">
        <span class="id mono">${esc(r.trace_id)}</span>
        <span class="meta">${esc(usd(r.total_cost_usd))} of ${esc(usd(r.baseline_usd))}</span>
        <span>${esc(r.served_model ?? 'nothing served')} <span class="${cls(servedVerdict(r))}">${esc(servedVerdict(r).toUpperCase())}</span> ${h_badge(r)}</span>
        <span class="meta">rung ${esc(r.served_rung ?? '-')} | ${esc(r.escalations)} esc | ${esc(num(r.total_latency_ms))}ms</span>
      </button>`).join('');
  }
  function renderRows() {
    const rungs = [...new Set(state.receipts.map(r => String(r.served_rung ?? 'none')))].sort();
    const sel = $('f-rung'), cur = sel.value;
    sel.innerHTML = '<option value="">any</option>' + rungs.map(g => `<option value="${esc(g)}">${esc(g === 'none' ? 'none served' : 'rung ' + g)}</option>`).join('');
    sel.value = rungs.includes(cur) ? cur : '';
    const list = visible();
    $('f-count').textContent = list.length + ' of ' + state.receipts.length + ' shown (newest 200)';
    const focused = document.activeElement && document.activeElement.dataset ? document.activeElement.dataset.id : null;
    $('rows').innerHTML = state.receipts.length
      ? (list.length ? h_rows(list) : '<div class="empty">No receipts match these filters.</div>')
      : '<div class="empty"><h3>No receipts yet</h3>Send a request through the proxy: try <code>firstpass kiosk</code>.</div>';
    if (focused) { const el = $('rows').querySelector('[data-id="' + CSS.escape(focused) + '"]'); if (el) el.focus(); }
  }

  const h_served = (a, r) => (a.rung === r.served_rung ? '<span class="badge">served</span>' : '');
  const h_gate = g => `<div class="mono" style="font-size:12px">gate ${esc(g.gate_id)}: <span class="${cls(g.verdict)}">${esc(String(g.verdict).toUpperCase())}</span>${h_score(g)}${h_reason(g)}</div>`;
  const h_score = g => (g.score === null || g.score === undefined ? '' : ' score ' + esc(Number(g.score).toFixed(3)));
  const h_reason = g => (g.reason ? ' (' + esc(g.reason) + ')' : '');
  const h_gates = a => (a.gates || []).map(h_gate).join('');
  function h_attempts(r) {
    return (r.attempts || []).map(a => `<div class="att">
        <div><b>rung ${esc(a.rung)}</b> <span class="mono">${esc(a.model)}</span> <span class="${cls(a.verdict)}">${esc(String(a.verdict).toUpperCase())}</span>
          ${h_served(a, r)}</div>
        <div class="lede" style="margin:0">${esc(usd(a.cost_usd))} | ${esc(num(a.latency_ms))}ms</div>
        ${h_gates(a)}
      </div>`).join('');
  }
  // Live mode requires 0 < percent < 100, so a recorded percent of 0 is a paired experiment: the
  // caller was always served by the current ladder and the challenger only ever ran off-path.
  const h_exp = r => (!r.experiment ? 'none: this route has no challenger'
    : Number(r.experiment.percent) === 0 ? esc('paired: served by your ladder; the challenger may also have run on this request off-path, if sampled')
      : h_badge(r) + ' ' + esc(r.experiment.percent) + '% of sessions, bucket ' + esc(r.experiment.bucket));
  const h_deferred = r => ((r.deferred || []).length ? '<h2>Outcome feedback</h2>' + r.deferred.map(d => `<div class="mono" style="font-size:12px">${esc(d.gate_id)}: <span class="${cls(d.verdict)}">${esc(String(d.verdict).toUpperCase())}</span> by ${esc(d.reporter)}</div>`).join('') : '');
  function openDrawer(id) {
    const r = state.receipts.find(x => x.trace_id === id);
    if (!r) return;
    state.openId = id;
    $('drawer-body').innerHTML = `
      <dl class="kv">
        <dt>trace id</dt><dd class="mono">${esc(r.trace_id)}</dd>
        <dt>time</dt><dd>${esc(r.ts)}</dd>
        <dt>session</dt><dd class="mono">${esc(r.session_id)}</dd>
        <dt>served</dt><dd>rung ${esc(r.served_rung ?? '-')} | ${esc(r.served_model ?? 'nothing')} | from ${esc(r.served_from)}</dd>
        <dt>cost</dt><dd>${esc(usd(r.total_cost_usd))} vs ${esc(usd(r.baseline_usd))} baseline (saved ${esc(usd(r.savings_usd))})</dd>
        <dt>latency</dt><dd>${esc(num(r.total_latency_ms))}ms | ${esc(r.escalations)} escalations</dd>
        <dt>experiment arm</dt><dd>${h_exp(r)}</dd>
        <dt>prev_hash</dt><dd class="hash">${esc(r.prev_hash)}</dd>
        <dt>hash</dt><dd class="hash">${esc(r.hash ?? 'unavailable')}</dd>
      </dl>
      <p class="lede" style="margin:8px 0">Re-derive the chain yourself with <code>firstpass verify</code>.</p>
      <h2>Attempts</h2>${h_attempts(r)}
      ${h_deferred(r)}`;
    $('drawer').hidden = false;
    $('drawer').focus();
  }
  function closeDrawer() {
    const id = state.openId;
    state.openId = null;
    $('drawer').hidden = true;
    if (id) { const el = $('rows').querySelector('[data-id="' + CSS.escape(id) + '"]'); if (el) el.focus(); }
  }

  // ---- shell ----------------------------------------------------------------------------------
  function render() {
    h_overview();
    $('exp-list').innerHTML = h_experiments();
    renderRows();
  }
  function show(view) {
    state.view = view;
    for (const b of document.querySelectorAll('nav [role=tab]')) {
      const on = b.dataset.view === view;
      b.setAttribute('aria-selected', on ? 'true' : 'false');
      b.tabIndex = on ? 0 : -1;
      $('view-' + b.dataset.view).classList.toggle('hidden', !on);
    }
    if (location.hash !== '#' + view) history.replaceState(null, '', '#' + view);
  }
  document.querySelector('nav').addEventListener('click', ev => { const b = ev.target.closest('[data-view]'); if (b) show(b.dataset.view); });
  document.querySelector('nav').addEventListener('keydown', ev => {
    const tabs = [...document.querySelectorAll('nav [role=tab]')];
    const i = tabs.findIndex(t => t.dataset.view === state.view);
    const j = ev.key === 'ArrowRight' ? (i + 1) % tabs.length : ev.key === 'ArrowLeft' ? (i + tabs.length - 1) % tabs.length : -1;
    if (j >= 0) { ev.preventDefault(); show(tabs[j].dataset.view); tabs[j].focus(); }
  });
  $('rows').addEventListener('click', ev => { const b = ev.target.closest('[data-id]'); if (b) openDrawer(b.dataset.id); });
  $('drawer-close').addEventListener('click', closeDrawer);
  document.addEventListener('keydown', ev => { if (ev.key === 'Escape' && !$('drawer').hidden) closeDrawer(); });
  for (const id of ['f-rung', 'f-verdict', 'f-arm']) $(id).addEventListener('change', renderRows);
  $('exp-list').addEventListener('click', async ev => {
    const b = ev.target.closest('[data-copy]');
    if (!b) return;
    const text = $(b.dataset.copy).textContent;
    try { await navigator.clipboard.writeText(text); b.textContent = 'Copied'; }
    catch (e) {
      const range = document.createRange(); range.selectNodeContents($(b.dataset.copy));
      const s = getSelection(); s.removeAllRanges(); s.addRange(range); b.textContent = 'Press Ctrl+C';
    }
    setTimeout(() => { b.textContent = 'Copy'; }, 2000);
  });

  const VIEWS = ['overview', 'experiments', 'receipts'];
  const start = (location.hash || '').slice(1);
  show(VIEWS.includes(start) ? start : 'overview');
  // A pasted link, back/forward, or an edited hash changes only the fragment: no reload, so follow
  // it here or the page would show one view while the address bar names another.
  window.addEventListener('hashchange', () => {
    const v = (location.hash || '').slice(1);
    if (VIEWS.includes(v) && v !== state.view) show(v);
  });
  tick();
</script>
</body>
</html>