Devin AI Deric J. McHenry commited on
Commit
6378511
·
1 Parent(s): 7a69f6e

Importer overhaul: two-pathway Codex parser, HF auth, timestamps, Greatest Hits

Browse files

- ingest.py: Replace parse_codex() with parse_codex_submission() implementing
Pathway Alpha (3:2:1 standard baseline, input = output × 2.0) and
Pathway Beta (Claude 1:9 closed-loop, input = output / 9.0).
Returns calibrated_user_input + structural_context_debt.

- app.py: Rename tab to 'Clock Your Signal'. Fix CLI instructions to show
all ccusage providers (claude/codex/combined). Fix paste format and examples.
Add HF LoginButton (Space-only, graceful local fallback). Wire HF auth
into run_ingest — only authenticated users get persisted board entries.
Add Greatest Hits section showing top sessions by Υ.

- db.py: Add submitted_at timestamp to save_operator(). Gate saves on
hf_user (no HF login = no persistence). Add sigrank_sessions table
for session history. Add load_session_history() for Greatest Hits.

- theme.py: CSS for parsing mode badge, estimation asterisk, Greatest
Hits table, HF login button.

- sigrank.py: Use parse_codex_submission with operator_profile detection
(Claude profile → Beta pathway, else Alpha).

- requirements.txt: gradio[oauth] for HF LoginButton.

metrics.py and SEED numbers are untouched.

Co-Authored-By: Deric J. McHenry <deric.mchenry@gmail.com>

Files changed (6) hide show
  1. app.py +143 -61
  2. db.py +68 -2
  3. ingest.py +86 -80
  4. requirements.txt +1 -1
  5. sigrank.py +7 -8
  6. theme.py +29 -0
app.py CHANGED
@@ -7,6 +7,7 @@ import gradio as gr
7
  import html as _html
8
  import math as _math
9
  import re as _re
 
10
  from metrics import compute, SEED
11
  from ingest import ingest_meta
12
  from theme import CSS
@@ -68,7 +69,7 @@ def board_html(extra=None):
68
  else:
69
  cls = f"mb-row rarity-{rkey}"
70
  ne = _html.escape(n)
71
- est_mark = " <span class='mb-est' title='estimated (Codex anchor)'>~</span>" if m.get("cost_estimated") else ""
72
  out.append(f'<div class="{cls}">'
73
  f'<span class="mb-rank {rank_cls}">{i}</span>'
74
  f'<span class="mb-op"><b>{ne}{est_mark}</b><br><span class="mb-raw">R {_fmt_int(m["raw"]["cache_read"])} \u00b7 C {_fmt_int(m["raw"]["cache_create"])} \u00b7 I {_fmt_int(m["raw"]["input"])} \u00b7 O {_fmt_int(m["raw"]["output"])}</span></span>'
@@ -81,7 +82,7 @@ def board_html(extra=None):
81
  f'<span class="mb-yval">{y:,.0f}</span></span>'
82
  f'</div>')
83
  out.append('</div>')
84
- out.append('<div class="mb-foot">\u03a5 bar is log-scaled \u00b7 MO\u00a7ES leads the field by ~4 orders of magnitude \u00b7 $/1M blended cost (~ = list-price estimate) \u00b7 volume can\'t buy rank</div>')
85
  return "".join(out)
86
 
87
  # ---------- profile ----------
@@ -142,6 +143,9 @@ def card_html(name, m, rank, total_ops, narration_text):
142
  archetype = classify(m).split("\u00b7")[0].strip()
143
  rkey, rlabel, passive, effect = rarity_class(m)
144
  c = m["composition"]
 
 
 
145
  if m["transmission"] is not None:
146
  cascade = (
147
  f'<div class="sig-card-cascade-box">{m["transmission"]:.1f}\u00d7<small>trans</small></div>'
@@ -163,6 +167,7 @@ def card_html(name, m, rank, total_ops, narration_text):
163
  f'<div class="sig-card-archetype">{archetype}</div>'
164
  f'<div class="sig-card-passive">Passive: {passive}</div>'
165
  f'<div class="sig-card-effect">{effect}</div>'
 
166
  f'<div class="sig-card-yield">{m["yield"]:,.0f}</div>'
167
  '<div class="sig-card-yield-label">net volumetric yield</div>'
168
  f'<div class="sig-card-rank">#<span>{rank}</span> of {total_ops} operators</div>'
@@ -181,8 +186,10 @@ def profile_md(name, m, rank, total_ops, read=None):
181
  cav = m.get("_caveat")
182
  cav_line = f"\n\n`\u26a0 {cav}`" if cav else ""
183
  cost_note = " (list-price estimate)" if m.get("cost_estimated") else " (from ccusage)"
 
 
184
  return f"""## OPERATOR \u00b7 {name}
185
- ranked **#{rank}** of {total_ops} by \u03a5{cav_line}
186
 
187
  > {read}
188
 
@@ -207,97 +214,172 @@ ranked **#{rank}** of {total_ops} by \u03a5{cav_line}
207
  | Avg $/1M | ${m['avg_cost_1m']:.3f} |{cost_note} |
208
  | **\u03a5 Yield** | **{m['yield']:,.2f}** | un-gameable rank |
209
 
210
- **cascade** {m['cascade_str']} (transmission \u00d7 commitment \u00d7 reuse)
211
- **scale V** {m['V']:.2f}
212
  """
213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  # ---------- ingestion handler ----------
215
- def run_ingest(blob, name):
 
 
 
216
  name=(name or "you").strip()[:24] or "you"
217
  try:
218
  i,o,cw,cr,meta = ingest_meta(blob or "")
219
  except Exception as e:
220
- return ("Paste your `npx ccusage@latest --json` output, your "
221
- "`ccusage codex --json` output, or four numbers: "
 
222
  "input output cache_create cache_read.\n\n"
223
- f"_parser said: {e}_"), "", "", board_html()
224
  if i+o+cw+cr==0:
225
- return "Got zeros check your paste.", "", "", board_html()
226
  m=compute(i,o,cw,cr, cost_usd=meta.get("cost"))
227
  if meta.get("estimated"):
228
  m["_caveat"]=meta.get("caveat")
229
- # persist only if writes are configured (curated demo leaves them off -> transient)
 
 
230
  saved=False
231
- if db.writes_enabled():
232
  saved=db.save_operator(name,i,o,cw,cr, cost=meta.get("cost"),
233
  source=meta.get("source","manual"),
234
  estimated=bool(meta.get("estimated")),
235
- caveat=meta.get("caveat"))
236
- base=operators(force=saved) # refresh cache only if a row was actually written
 
237
  rows=[(nn,compute(*vv)) for nn,vv in base.items() if nn!=name]+[(name,m)]
238
  rows.sort(key=lambda r:r[1]['yield'],reverse=True)
239
  rank=next(idx for idx,(nn,_) in enumerate(rows,1) if nn==name)
240
  read = narrate(name, m, classify(m))
241
- return (profile_md(name,m,rank,len(rows),read),
 
 
 
 
 
 
 
 
 
 
242
  comp_bar_html(m["composition"]),
243
  card_html(name,m,rank,len(rows),read),
 
244
  board_html((name,m)))
245
 
246
  # ---------- UI ----------
247
- _blocks_kw = {"title": "MO\u00a7ES SigRank"}
248
- try:
249
- _b = gr.Blocks(css=CSS, theme=gr.themes.Base(), **_blocks_kw)
250
- except TypeError:
251
- _b = gr.Blocks(**_blocks_kw)
252
- with _b as demo:
253
- with gr.Column(elem_id="moses-hero"):
254
- gr.HTML("<h1>MO§ES\u2122 SigRank</h1>"
255
- "<p>the diagnostic x-ray of the token economy \u00b7 ranked by \u03a5 (Net Volumetric Yield) \u00b7 volume can't buy rank</p>")
256
- gr.HTML('<div id="moses-stat-strip">'
257
- '<div>operators ranked <span>7</span></div>'
258
- '<div>MO§ES leads by <span>3,141\u00d7</span></div>'
259
- '<div>architecture beats budget</div>'
260
- '</div>')
261
 
262
- with gr.Tab("Leaderboard"):
263
- gr.Markdown("Ranked by **Υ = (Cache·Output)/Input²**. Raw Read·Create·In·Out stacked under each operator. $/1M is blended cost — efficient architecture is also the cheapest.")
264
- gr.Markdown("*Corpus is curated — pasting your usage scores you live against the field but doesn't add you to the persisted board. $/1M is a list-price recompute (~); real cost shows when you paste your own ccusage.*", elem_id="moses-foot")
265
- lb = gr.HTML(board_html())
 
 
 
 
 
 
 
 
 
 
 
266
 
267
- with gr.Tab("Measure yourself"):
268
- gr.Markdown("""**Get your operator profile automatic, no paste.**
 
 
269
 
270
- **① Run the local importer** (reads your usage on your machine, 100% local):
 
 
 
 
 
 
 
271
  ```
272
- ./sigrank
273
  ```
274
- (or `python3 sigrank.py`). It auto-runs `ccusage`, computes your profile + Υ, and prints your board rank. No paste, no upload, no token.
 
 
 
 
275
 
276
- **② Paste the backup.** No importer handy? Run `npx ccusage@latest --json` (Claude Code) or `ccusage codex --json` (Codex) and drop it below, or just four numbers (input, output, cache_create, cache_read).
277
- *Codex note: combined input is split via the 2:1 field anchor; estimated rows are flagged.*""")
278
- nm = gr.Textbox(label="operator name", placeholder="your handle", max_lines=1)
279
- blob = gr.Textbox(label="ccusage / codex JSON —or— four numbers", lines=6,
280
- placeholder='{"totals":{"inputTokens":...}} or 1251211 11296121 128196310 2555179769')
281
- go = gr.Button("Compute my SigRank", variant="primary", elem_id="compute-btn")
282
- prof = gr.Markdown(elem_id="moses-profile")
283
- prof_bar = gr.HTML()
284
- gr.Markdown("### share card")
285
- card = gr.HTML()
286
- gr.Markdown("*Screenshot to share \u00b7 right-click \u2192 Save image*", elem_id="moses-foot")
287
- gr.Markdown("### your placement")
288
- gr.Markdown("*Live placement against the curated field — your row is transient (not saved to the board).*", elem_id="moses-foot")
289
- ob = gr.HTML(board_html())
290
- go.click(run_ingest, [blob, nm], [prof, prof_bar, card, ob])
291
- gr.Examples(
292
- examples=[
293
- ['{"totals":{"inputTokens":1251211,"outputTokens":11296121,"cacheCreationTokens":128196310,"cacheReadTokens":2555179769}}','MO§ES'],
294
- ['{"data":[{"input_tokens":145809,"cached_input_tokens":112512,"output_tokens":2094,"reasoning_output_tokens":710}]}','codex-operator'],
295
- ],
296
- inputs=[blob, nm])
297
 
298
- gr.Markdown(elem_id="moses-foot", value="""Four integers in, full ledger out.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  Architecture is the only variable that matters.
300
- Wild-corpus values provisional · MO§ES row verified ccusage · [How it works ](#)""")
 
 
 
301
 
302
  if __name__ == "__main__":
303
  try:
 
7
  import html as _html
8
  import math as _math
9
  import re as _re
10
+ from datetime import datetime, timezone
11
  from metrics import compute, SEED
12
  from ingest import ingest_meta
13
  from theme import CSS
 
69
  else:
70
  cls = f"mb-row rarity-{rkey}"
71
  ne = _html.escape(n)
72
+ est_mark = " <span class='mb-est' title='* structural estimation'>*</span>" if m.get("cost_estimated") else ""
73
  out.append(f'<div class="{cls}">'
74
  f'<span class="mb-rank {rank_cls}">{i}</span>'
75
  f'<span class="mb-op"><b>{ne}{est_mark}</b><br><span class="mb-raw">R {_fmt_int(m["raw"]["cache_read"])} \u00b7 C {_fmt_int(m["raw"]["cache_create"])} \u00b7 I {_fmt_int(m["raw"]["input"])} \u00b7 O {_fmt_int(m["raw"]["output"])}</span></span>'
 
82
  f'<span class="mb-yval">{y:,.0f}</span></span>'
83
  f'</div>')
84
  out.append('</div>')
85
+ out.append('<div class="mb-foot">\u03a5 bar is log-scaled \u00b7 MO\u00a7ES leads the field by ~4 orders of magnitude \u00b7 $/1M blended cost (~ = list-price estimate) \u00b7 * = structural estimation \u00b7 volume can\'t buy rank</div>')
86
  return "".join(out)
87
 
88
  # ---------- profile ----------
 
143
  archetype = classify(m).split("\u00b7")[0].strip()
144
  rkey, rlabel, passive, effect = rarity_class(m)
145
  c = m["composition"]
146
+ parsing_mode = m.get("_parsing_mode", "")
147
+ mode_badge = (f'<div class="sig-card-mode">* {_html.escape(parsing_mode)}</div>'
148
+ if parsing_mode else "")
149
  if m["transmission"] is not None:
150
  cascade = (
151
  f'<div class="sig-card-cascade-box">{m["transmission"]:.1f}\u00d7<small>trans</small></div>'
 
167
  f'<div class="sig-card-archetype">{archetype}</div>'
168
  f'<div class="sig-card-passive">Passive: {passive}</div>'
169
  f'<div class="sig-card-effect">{effect}</div>'
170
+ f'{mode_badge}'
171
  f'<div class="sig-card-yield">{m["yield"]:,.0f}</div>'
172
  '<div class="sig-card-yield-label">net volumetric yield</div>'
173
  f'<div class="sig-card-rank">#<span>{rank}</span> of {total_ops} operators</div>'
 
186
  cav = m.get("_caveat")
187
  cav_line = f"\n\n`\u26a0 {cav}`" if cav else ""
188
  cost_note = " (list-price estimate)" if m.get("cost_estimated") else " (from ccusage)"
189
+ mode = m.get("_parsing_mode")
190
+ mode_line = f"\n\n`* {mode}`" if mode else ""
191
  return f"""## OPERATOR \u00b7 {name}
192
+ ranked **#{rank}** of {total_ops} by \u03a5{cav_line}{mode_line}
193
 
194
  > {read}
195
 
 
214
  | Avg $/1M | ${m['avg_cost_1m']:.3f} |{cost_note} |
215
  | **\u03a5 Yield** | **{m['yield']:,.2f}** | un-gameable rank |
216
 
217
+ **cascade** \u2014 {m['cascade_str']} (transmission \u00d7 commitment \u00d7 reuse)
218
+ **scale V** \u2014 {m['V']:.2f}
219
  """
220
 
221
+ def _greatest_hits_html(name):
222
+ """Render top sessions for this operator from session history."""
223
+ history = db.load_session_history(name, limit=5)
224
+ if not history:
225
+ return ""
226
+ rows = []
227
+ for h in history:
228
+ i = int(h.get("input", 0) or 0)
229
+ o = int(h.get("output", 0) or 0)
230
+ cw = int(h.get("cache_create", 0) or 0)
231
+ cr = int(h.get("cache_read", 0) or 0)
232
+ m = compute(i, o, cw, cr)
233
+ ts = h.get("submitted_at", "")
234
+ if ts:
235
+ try:
236
+ dt = datetime.fromisoformat(ts.replace("Z", "+00:00"))
237
+ ts = dt.strftime("%Y-%m-%d %H:%M UTC")
238
+ except (ValueError, TypeError):
239
+ pass
240
+ src = h.get("source", "")
241
+ rows.append(
242
+ f'<tr><td>{ts}</td><td>{_fmt_int(m["yield"])}</td>'
243
+ f'<td>{m["velocity"]:.2f}\u00d7</td><td>{m["leverage"]:,.0f}\u00d7</td>'
244
+ f'<td>{src}</td></tr>'
245
+ )
246
+ return (
247
+ '<div class="greatest-hits">'
248
+ '<h4>Greatest Hits</h4>'
249
+ '<table><thead><tr><th>when</th><th>\u03a5</th><th>vel</th><th>lev</th><th>source</th></tr></thead>'
250
+ '<tbody>' + "".join(rows) + '</tbody></table>'
251
+ '</div>'
252
+ )
253
+
254
  # ---------- ingestion handler ----------
255
+ def run_ingest(blob, name, request: gr.Request):
256
+ hf_user = None
257
+ if request:
258
+ hf_user = getattr(request, "username", None)
259
  name=(name or "you").strip()[:24] or "you"
260
  try:
261
  i,o,cw,cr,meta = ingest_meta(blob or "")
262
  except Exception as e:
263
+ return ("Paste your `ccusage claude --json` output, your "
264
+ "`ccusage codex --json` output, or `ccusage --json` "
265
+ "for all providers. You can also paste four numbers: "
266
  "input output cache_create cache_read.\n\n"
267
+ f"_parser said: {e}_"), "", "", "", board_html()
268
  if i+o+cw+cr==0:
269
+ return "Got zeros \u2014 check your paste.", "", "", "", board_html()
270
  m=compute(i,o,cw,cr, cost_usd=meta.get("cost"))
271
  if meta.get("estimated"):
272
  m["_caveat"]=meta.get("caveat")
273
+ if meta.get("parsing_mode"):
274
+ m["_parsing_mode"] = meta["parsing_mode"]
275
+ # persist only if HF-authenticated + writes configured
276
  saved=False
277
+ if hf_user and db.writes_enabled():
278
  saved=db.save_operator(name,i,o,cw,cr, cost=meta.get("cost"),
279
  source=meta.get("source","manual"),
280
  estimated=bool(meta.get("estimated")),
281
+ caveat=meta.get("caveat"),
282
+ hf_user=hf_user)
283
+ base=operators(force=saved)
284
  rows=[(nn,compute(*vv)) for nn,vv in base.items() if nn!=name]+[(name,m)]
285
  rows.sort(key=lambda r:r[1]['yield'],reverse=True)
286
  rank=next(idx for idx,(nn,_) in enumerate(rows,1) if nn==name)
287
  read = narrate(name, m, classify(m))
288
+
289
+ save_note = ""
290
+ if not hf_user:
291
+ save_note = "\n\n*\u26a0 Sign in with HuggingFace to save your entry to the board. Paste-only results are a snapshot \u2014 not persisted.*"
292
+ elif saved:
293
+ save_note = f"\n\n*Saved to the board as **{_html.escape(name)}** at {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}.*"
294
+
295
+ hits_html = _greatest_hits_html(name) if hf_user else ""
296
+ profile = profile_md(name,m,rank,len(rows),read) + save_note
297
+
298
+ return (profile,
299
  comp_bar_html(m["composition"]),
300
  card_html(name,m,rank,len(rows),read),
301
+ hits_html,
302
  board_html((name,m)))
303
 
304
  # ---------- UI ----------
305
+ import os as _os
306
+ _ON_SPACE = bool(_os.environ.get("SPACE_ID"))
 
 
 
 
 
 
 
 
 
 
 
 
307
 
308
+ def _build_demo():
309
+ _blocks_kw = {"title": "MO\u00a7ES SigRank"}
310
+ try:
311
+ _b = gr.Blocks(css=CSS, theme=gr.themes.Base(), **_blocks_kw)
312
+ except TypeError:
313
+ _b = gr.Blocks(**_blocks_kw)
314
+ with _b:
315
+ with gr.Column(elem_id="moses-hero"):
316
+ gr.HTML("<h1>MO\u00a7ES\u2122 SigRank</h1>"
317
+ "<p>the diagnostic x-ray of the token economy \u00b7 ranked by \u03a5 (Net Volumetric Yield) \u00b7 volume can't buy rank</p>")
318
+ gr.HTML('<div id="moses-stat-strip">'
319
+ '<div>operators ranked <span>7</span></div>'
320
+ '<div>MO\u00a7ES leads by <span>3,141\u00d7</span></div>'
321
+ '<div>architecture beats budget</div>'
322
+ '</div>')
323
 
324
+ with gr.Tab("Leaderboard"):
325
+ gr.Markdown("Ranked by **\u03a5 = (Cache\u00b7Output)/Input\u00b2**. Raw Read\u00b7Create\u00b7In\u00b7Out stacked under each operator. $/1M is blended cost \u2014 efficient architecture is also the cheapest.")
326
+ gr.Markdown("*Corpus is curated \u2014 pasting your usage scores you live against the field but doesn't add you to the persisted board unless you're signed in via HuggingFace. $/1M is a list-price recompute (~); real cost shows when you paste your own ccusage. * = structural estimation.*", elem_id="moses-foot")
327
+ gr.HTML(board_html())
328
 
329
+ with gr.Tab("Clock Your Signal"):
330
+ gr.Markdown("""**Get your operator profile \u2014 every provider, one command.**
331
+
332
+ **\u2460 Run ccusage** (reads your local usage \u2014 one command per provider):
333
+ ```
334
+ ccusage claude --json # Claude Code stats
335
+ ccusage codex --json # Codex stats (estimated *)
336
+ ccusage --json # ALL providers combined
337
  ```
338
+ Or use the local importer:
339
  ```
340
+ ./sigrank # Claude Code (measured)
341
+ ./sigrank --codex # Codex (applies field anchor *)
342
+ ```
343
+
344
+ **\u2461 Paste the JSON below.** Drop your `ccusage` output \u2014 Claude, Codex, or combined \u2014 and we'll route it automatically. Or paste four numbers: `input output cache_create cache_read`.
345
 
346
+ *Codex / non-Claude providers: input is calibrated via the 3:2:1 field anchor (or 1:9 if you have a Claude profile). Estimated rows are flagged with \\*.*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
 
348
+ **\u2462 Sign in to save.** HuggingFace users get one persistent board entry + session history. Paste without login = snapshot only.""")
349
+ if _ON_SPACE:
350
+ gr.LoginButton(elem_id="hf-login-btn")
351
+ else:
352
+ gr.Markdown("*HuggingFace login available on the hosted Space \u2014 local mode is transient.*", elem_id="moses-foot")
353
+ nm = gr.Textbox(label="operator name", placeholder="your handle", max_lines=1)
354
+ blob = gr.Textbox(label="ccusage JSON (any provider) \u2014or\u2014 four numbers (I O C R)", lines=6,
355
+ placeholder='Paste ccusage claude/codex/combined --json output here\n\nor four numbers: input output cache_create cache_read\n\nExample: 1251211 11296121 128196310 2555179769')
356
+ go = gr.Button("Clock My Signal", variant="primary", elem_id="compute-btn")
357
+ prof = gr.Markdown(elem_id="moses-profile")
358
+ prof_bar = gr.HTML()
359
+ gr.Markdown("### share card")
360
+ card = gr.HTML()
361
+ gr.Markdown("*Screenshot to share \u00b7 right-click \u2192 Save image*", elem_id="moses-foot")
362
+ gr.Markdown("### greatest hits")
363
+ hits = gr.HTML()
364
+ gr.Markdown("*Top sessions by \u03a5 \u2014 sign in with HuggingFace to track your history.*", elem_id="moses-foot")
365
+ gr.Markdown("### your placement")
366
+ gr.Markdown("*Live placement against the curated field \u2014 sign in to persist your entry.*", elem_id="moses-foot")
367
+ ob = gr.HTML(board_html())
368
+ go.click(run_ingest, [blob, nm], [prof, prof_bar, card, hits, ob])
369
+ gr.Examples(
370
+ examples=[
371
+ ['{"totals":{"inputTokens":1251211,"outputTokens":11296121,"cacheCreationTokens":128196310,"cacheReadTokens":2555179769}}','MO\u00a7ES'],
372
+ ['{"data":[{"inputTokens":58920000,"cachedInputTokens":707300000,"outputTokens":3500000,"reasoningOutputTokens":510000}]}','codex-operator'],
373
+ ['1251211 11296121 128196310 2555179769', 'manual-paste'],
374
+ ],
375
+ inputs=[blob, nm])
376
+
377
+ gr.Markdown(elem_id="moses-foot", value="""Four integers in, full ledger out.
378
  Architecture is the only variable that matters.
379
+ Wild-corpus values provisional \u00b7 MO\u00a7ES row verified ccusage \u00b7 * = structural estimation \u00b7 [How it works \u2197](#)""")
380
+ return _b
381
+
382
+ demo = _build_demo()
383
 
384
  if __name__ == "__main__":
385
  try:
db.py CHANGED
@@ -15,11 +15,13 @@ Curated mode (A):
15
  cleanly even where requests isn't installed (it just falls back to SEED).
16
  """
17
  import os
 
18
 
19
  _URL = os.environ.get("SUPABASE_URL", "").rstrip("/")
20
  _ANON = os.environ.get("SUPABASE_ANON_KEY", "")
21
  _SERVICE = os.environ.get("SUPABASE_SERVICE_KEY", "")
22
  _TABLE = "sigrank_operators"
 
23
  _TIMEOUT = 6
24
 
25
 
@@ -69,12 +71,16 @@ def load_operators():
69
 
70
 
71
  def save_operator(name, i, o, cw, cr, cost=None, source="manual",
72
- estimated=False, caveat=None):
73
  """Upsert one operator via the service key (PostgREST merge-duplicates on
74
  `name`). No-op returning False unless SUPABASE_SERVICE_KEY is set. Never
75
- raises — failures are swallowed and logged so ingestion can't crash."""
 
76
  if not writes_enabled():
77
  return False
 
 
 
78
  try:
79
  import requests
80
  payload = {
@@ -83,6 +89,8 @@ def save_operator(name, i, o, cw, cr, cost=None, source="manual",
83
  "cache_create": int(cw), "cache_read": int(cr),
84
  "cost_usd": cost, "source": source,
85
  "estimated": bool(estimated), "caveat": caveat,
 
 
86
  }
87
  r = requests.post(
88
  f"{_URL}/rest/v1/{_TABLE}",
@@ -93,7 +101,65 @@ def save_operator(name, i, o, cw, cr, cost=None, source="manual",
93
  json=payload, timeout=_TIMEOUT,
94
  )
95
  r.raise_for_status()
 
 
 
96
  return True
97
  except Exception as e:
98
  print(f"[db] save_operator failed (non-fatal): {e}")
99
  return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  cleanly even where requests isn't installed (it just falls back to SEED).
16
  """
17
  import os
18
+ from datetime import datetime, timezone
19
 
20
  _URL = os.environ.get("SUPABASE_URL", "").rstrip("/")
21
  _ANON = os.environ.get("SUPABASE_ANON_KEY", "")
22
  _SERVICE = os.environ.get("SUPABASE_SERVICE_KEY", "")
23
  _TABLE = "sigrank_operators"
24
+ _HIST_TABLE = "sigrank_sessions"
25
  _TIMEOUT = 6
26
 
27
 
 
71
 
72
 
73
  def save_operator(name, i, o, cw, cr, cost=None, source="manual",
74
+ estimated=False, caveat=None, hf_user=None):
75
  """Upsert one operator via the service key (PostgREST merge-duplicates on
76
  `name`). No-op returning False unless SUPABASE_SERVICE_KEY is set. Never
77
+ raises — failures are swallowed and logged so ingestion can't crash.
78
+ hf_user: HuggingFace username — only authenticated users get persisted."""
79
  if not writes_enabled():
80
  return False
81
+ if not hf_user:
82
+ return False
83
+ now = datetime.now(timezone.utc).isoformat()
84
  try:
85
  import requests
86
  payload = {
 
89
  "cache_create": int(cw), "cache_read": int(cr),
90
  "cost_usd": cost, "source": source,
91
  "estimated": bool(estimated), "caveat": caveat,
92
+ "hf_user": str(hf_user)[:64],
93
+ "submitted_at": now,
94
  }
95
  r = requests.post(
96
  f"{_URL}/rest/v1/{_TABLE}",
 
101
  json=payload, timeout=_TIMEOUT,
102
  )
103
  r.raise_for_status()
104
+ # also log to session history (best-effort)
105
+ _save_session(name, i, o, cw, cr, cost, source, estimated, caveat,
106
+ hf_user, now)
107
  return True
108
  except Exception as e:
109
  print(f"[db] save_operator failed (non-fatal): {e}")
110
  return False
111
+
112
+
113
+ def _save_session(name, i, o, cw, cr, cost, source, estimated, caveat,
114
+ hf_user, timestamp):
115
+ """Append a session record (never upserts — one row per submission).
116
+ Best-effort; failures are swallowed."""
117
+ if not writes_enabled():
118
+ return
119
+ try:
120
+ import requests
121
+ payload = {
122
+ "name": str(name)[:64],
123
+ "input": int(i), "output": int(o),
124
+ "cache_create": int(cw), "cache_read": int(cr),
125
+ "cost_usd": cost, "source": source,
126
+ "estimated": bool(estimated), "caveat": caveat,
127
+ "hf_user": str(hf_user)[:64],
128
+ "submitted_at": timestamp,
129
+ }
130
+ r = requests.post(
131
+ f"{_URL}/rest/v1/{_HIST_TABLE}",
132
+ headers={"apikey": _SERVICE, "Authorization": f"Bearer {_SERVICE}",
133
+ "Content-Type": "application/json",
134
+ "Prefer": "return=minimal"},
135
+ json=payload, timeout=_TIMEOUT,
136
+ )
137
+ r.raise_for_status()
138
+ except Exception as e:
139
+ print(f"[db] _save_session failed (non-fatal): {e}")
140
+
141
+
142
+ def load_session_history(name, limit=5):
143
+ """Load top N sessions for an operator by Υ (yield = cr/i * o/i).
144
+ Returns list of dicts with keys: input, output, cache_create, cache_read,
145
+ submitted_at. Falls back to empty list on any failure."""
146
+ if not enabled():
147
+ return []
148
+ try:
149
+ import requests
150
+ r = requests.get(
151
+ f"{_URL}/rest/v1/{_HIST_TABLE}",
152
+ params={
153
+ "select": "input,output,cache_create,cache_read,submitted_at,source",
154
+ "name": f"eq.{name}",
155
+ "order": "submitted_at.desc",
156
+ "limit": str(limit),
157
+ },
158
+ headers={"apikey": _ANON, "Authorization": f"Bearer {_ANON}"},
159
+ timeout=_TIMEOUT,
160
+ )
161
+ r.raise_for_status()
162
+ return r.json()
163
+ except Exception as e:
164
+ print(f"[db] load_session_history failed (non-fatal): {e}")
165
+ return []
ingest.py CHANGED
@@ -1,8 +1,14 @@
1
  """
2
  Ingestion — turn whatever the user pastes into four integers (+cost).
3
  Survives ccusage version differences and routes Codex shape separately.
 
 
 
 
 
4
  """
5
  import json
 
6
 
7
  def parse_ccusage(text):
8
  """Accept raw `ccusage --json` output (any known shape). Returns (i,o,cw,cr,cost)."""
@@ -34,13 +40,12 @@ def parse_ccusage(text):
34
  return tot["input"],tot["output"],tot["cache_create"],tot["cache_read"],cost
35
 
36
  def parse_four(text):
37
- """Accept four numbers in any delimiter."""
38
- import re
39
  nums=[int(float(x)) for x in re.findall(r"[\d,.]+", text.replace(",",""))]
40
  if len(nums)<4: raise ValueError("need 4 numbers: input output cache_create cache_read")
41
  return nums[0],nums[1],nums[2],nums[3]
42
 
43
- # ---------- Codex parse (combined-input split via 2:1 field anchor) ----------
44
  def is_codex_shape(d):
45
  keys = set()
46
  def scan(o):
@@ -53,96 +58,97 @@ def is_codex_shape(d):
53
  return ("cached_input_tokens" in keys or "cachedInputTokens" in keys or
54
  "reasoning_output_tokens" in keys or "reasoningOutputTokens" in keys)
55
 
56
- def parse_codex(text, io_ratio=None):
57
- """
58
- Codex reports combined input_tokens (incl. cached) + cached_input_tokens +
59
- output + reasoning. cache_create is never reported by OpenAI.
60
-
61
- Anchor strategy (turn-delta-first, ratio fallback):
62
- - If daily/session granularity is present, estimate cache_create from
63
- per-day context deltas (turn-delta method): each day's input growth above
64
- the previous day's total \u2248 new cache writes.
65
- - Else, fall back to io_ratio anchor: est_fresh = io_ratio * output.
66
- - io_ratio defaults to 2.0 (provisional); pass Claude's measured I/O ratio
67
- for better accuracy.
68
-
69
- cache_read = cachedInputTokens (measured directly).
70
- """
71
- d = json.loads(text) if isinstance(text, str) else text
72
  tot = {"in":0, "cached":0, "out":0, "reason":0, "cost":0.0}
73
- days = [] # for turn-delta
74
- def add(e, track=False):
75
  if not isinstance(e, dict): return
76
- i = e.get("input_tokens", e.get("inputTokens",0)) or 0
77
- ca = e.get("cached_input_tokens", e.get("cachedInputTokens",0)) or 0
78
- o = e.get("output_tokens", e.get("outputTokens",0)) or 0
79
- r = e.get("reasoning_output_tokens", e.get("reasoningOutputTokens",0)) or 0
80
- c = e.get("costUSD", e.get("cost",0)) or 0.0
81
- tot["in"] += i; tot["cached"] += ca
82
- tot["out"] += o; tot["reason"] += r; tot["cost"] += c
83
- if track and i > 0:
84
- days.append({"date": e.get("date",""), "in": i, "cached": ca})
85
  if isinstance(d, list):
86
  for e in d: add(e)
87
- else:
88
- for key in ("daily","session","sessions","data","entries","events"):
89
- v = d.get(key) if isinstance(d, dict) else None
 
 
90
  if isinstance(v, list):
91
- for e in v: add(e, track=True)
92
- break
93
  if isinstance(v, dict):
94
  for e in v.values(): add(e)
95
- break
96
- else:
97
- add(d)
98
-
99
- combined_in = tot["in"]; read = tot["cached"]; O = tot["out"] + tot["reason"]
100
- anchor_used = "2:1 fixed"
101
-
102
- # --- turn-delta method (when we have daily granularity) ---
103
- if len(days) >= 2:
104
- days.sort(key=lambda x: x["date"])
105
- # Each day's fresh input above the previous day's floor \u2248 new cache writes.
106
- # Heuristic: new context added each day = max(0, today.in - prev.in)
107
- create = 0
108
- prev = days[0]["in"]
109
- for day in days[1:]:
110
- delta = max(0, day["in"] - prev)
111
- create += delta
112
- prev = day["in"]
113
- create += days[0]["in"] # first day's full input is new cache
114
- I = combined_in # fresh input IS inputTokens (not combined with reads)
115
- anchor_used = "turn-delta"
116
- caveat = "estimated via turn-delta (cache_create from daily context growth)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  else:
118
- # --- ratio anchor fallback ---
119
- ratio = io_ratio if io_ratio and io_ratio > 0 else 2.0
120
- est_fresh = ratio * O
121
- create = combined_in - est_fresh
122
- if create >= 0:
123
- I = est_fresh
124
- label = f"{ratio:.2f}:1 anchor (Claude-measured)" if io_ratio else "2:1 anchor (fixed)"
125
- anchor_used = label
126
- caveat = f"estimated via {label}"
127
- else:
128
- create = 0
129
- I = max(combined_in - read, 0) or combined_in
130
- anchor_used = "fallback (anchor inverted)"
131
- caveat = "estimated \u2193 output-rich (anchor inverted)"
132
-
133
- meta = {"source":"codex", "estimated":True, "caveat":caveat,
134
- "anchor": anchor_used,
135
- "cost": tot["cost"] if tot["cost"] > 0 else None}
136
- return I, O, int(create), read, meta
137
-
138
- def ingest_meta(text, io_ratio=None):
139
- """Returns (i,o,cw,cr,meta) with estimated/caveat/cost."""
 
 
140
  text=text.strip()
141
  if not text: raise ValueError("empty")
142
  if text[0] in "{[":
143
  d=json.loads(text)
144
  if is_codex_shape(d):
145
- return parse_codex(d, io_ratio=io_ratio)
146
  i,o,cw,cr,cost = parse_ccusage(text)
147
  return i,o,cw,cr,{"source":"ccusage","estimated":False,"caveat":None,"cost":cost}
148
  i,o,cw,cr = parse_four(text)
 
1
  """
2
  Ingestion — turn whatever the user pastes into four integers (+cost).
3
  Survives ccusage version differences and routes Codex shape separately.
4
+
5
+ System Flag / Disclaimer *: All parsed values generated via the Codex pathway
6
+ are calculated structural estimations designed to isolate high-signal user
7
+ direction from background open-loop context noise. These values are optimized
8
+ for architectural modeling and are distinct from raw provider API payload logs.
9
  """
10
  import json
11
+ import re
12
 
13
  def parse_ccusage(text):
14
  """Accept raw `ccusage --json` output (any known shape). Returns (i,o,cw,cr,cost)."""
 
40
  return tot["input"],tot["output"],tot["cache_create"],tot["cache_read"],cost
41
 
42
  def parse_four(text):
43
+ """Accept four numbers in any delimiter: input output cache_create cache_read."""
 
44
  nums=[int(float(x)) for x in re.findall(r"[\d,.]+", text.replace(",",""))]
45
  if len(nums)<4: raise ValueError("need 4 numbers: input output cache_create cache_read")
46
  return nums[0],nums[1],nums[2],nums[3]
47
 
48
+ # ---------- Codex shape detection ----------
49
  def is_codex_shape(d):
50
  keys = set()
51
  def scan(o):
 
58
  return ("cached_input_tokens" in keys or "cachedInputTokens" in keys or
59
  "reasoning_output_tokens" in keys or "reasoningOutputTokens" in keys)
60
 
61
+ # ---------- Codex two-pathway parser ----------
62
+ def _extract_codex_totals(d):
63
+ """Walk a Codex JSON payload and sum the raw token fields."""
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  tot = {"in":0, "cached":0, "out":0, "reason":0, "cost":0.0}
65
+ def add(e):
 
66
  if not isinstance(e, dict): return
67
+ tot["in"] += e.get("input_tokens", e.get("inputTokens",0)) or 0
68
+ tot["cached"] += e.get("cached_input_tokens", e.get("cachedInputTokens",0)) or 0
69
+ tot["out"] += e.get("output_tokens", e.get("outputTokens",0)) or 0
70
+ tot["reason"] += e.get("reasoning_output_tokens", e.get("reasoningOutputTokens",0)) or 0
71
+ tot["cost"] += e.get("costUSD", e.get("cost",0)) or 0.0
 
 
 
 
72
  if isinstance(d, list):
73
  for e in d: add(e)
74
+ elif isinstance(d, dict):
75
+ for key in ("totals", "daily", "session", "sessions", "data", "entries", "events"):
76
+ v = d.get(key)
77
+ if key == "totals" and isinstance(v, dict):
78
+ add(v); return tot
79
  if isinstance(v, list):
80
+ for e in v: add(e)
81
+ return tot
82
  if isinstance(v, dict):
83
  for e in v.values(): add(e)
84
+ return tot
85
+ add(d)
86
+ return tot
87
+
88
+
89
+ def parse_codex_submission(payload, operator_profile=None):
90
+ """
91
+ Parses Codex token payloads to estimate true high-signal user input.
92
+
93
+ Two pathways depending on operator telemetry:
94
+
95
+ Pathway Alpha (Standard): No Claude footprint 3:2:1 baseline.
96
+ estimated_user_input = outputTokens × 2.0
97
+
98
+ Pathway Beta (Claude Engine): Operator has verified Claude profile →
99
+ dynamic 1:9 transmission velocity extraction.
100
+ estimated_user_input = outputTokens / 9.0
101
+
102
+ Returns (i, o, cw, cr, meta) mapped to the four pillars:
103
+ i = calibrated_user_input (high-signal core)
104
+ o = raw output (unchanged)
105
+ cw = structural_context_debt (non-essential friction tokens)
106
+ cr = retained_cache_read (measured directly)
107
+ """
108
+ d = json.loads(payload) if isinstance(payload, str) else payload
109
+ tot = _extract_codex_totals(d)
110
+
111
+ raw_out = tot["out"] + tot["reason"]
112
+ raw_in = tot["in"]
113
+ raw_cache = tot["cached"]
114
+ cost = tot["cost"] if tot["cost"] > 0 else None
115
+
116
+ # Pathway Beta: Dynamic User Profile Match (Claude Engine)
117
+ if operator_profile and operator_profile.get("model_type") == "claude":
118
+ estimated_user_input = raw_out / 9.0
119
+ parsing_mode = "Claude Closed-Loop Calibration (1:9)"
120
+ # Pathway Alpha: Fallback Standard (The Top 10 Wild Field Baseline)
121
  else:
122
+ estimated_user_input = raw_out * 2.0
123
+ parsing_mode = "Standard Open-Loop Baseline (3:2:1)"
124
+
125
+ context_debt = max(0, raw_in - int(estimated_user_input))
126
+
127
+ meta = {
128
+ "source": "codex",
129
+ "estimated": True,
130
+ "parsing_mode": parsing_mode,
131
+ "caveat": f"* {parsing_mode}",
132
+ "anchor": parsing_mode,
133
+ "cost": cost,
134
+ }
135
+ return int(estimated_user_input), raw_out, context_debt, raw_cache, meta
136
+
137
+
138
+ def ingest_meta(text, operator_profile=None):
139
+ """Returns (i,o,cw,cr,meta) with estimated/caveat/cost.
140
+
141
+ operator_profile: optional dict with at least {"model_type": "claude"}
142
+ when the submitting user has a verified Claude session profile. This
143
+ switches the Codex parser from the 3:2:1 baseline to the 1:9 closed-loop
144
+ calibration pathway.
145
+ """
146
  text=text.strip()
147
  if not text: raise ValueError("empty")
148
  if text[0] in "{[":
149
  d=json.loads(text)
150
  if is_codex_shape(d):
151
+ return parse_codex_submission(d, operator_profile=operator_profile)
152
  i,o,cw,cr,cost = parse_ccusage(text)
153
  return i,o,cw,cr,{"source":"ccusage","estimated":False,"caveat":None,"cost":cost}
154
  i,o,cw,cr = parse_four(text)
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio>=4.44.0
2
  spaces
3
  requests
4
  torch
 
1
+ gradio[oauth]>=4.44.0
2
  spaces
3
  requests
4
  torch
sigrank.py CHANGED
@@ -24,7 +24,7 @@ import shutil
24
  import subprocess
25
  import sys
26
 
27
- from ingest import ingest_meta
28
  from metrics import compute
29
  import db
30
 
@@ -144,22 +144,21 @@ def main(argv=None):
144
 
145
  color = sys.stdout.isatty() and not args.no_color
146
 
147
- # For Codex: compute Claude's real I/O ratio first and use it as anchor.
148
- io_ratio = None
149
  if args.codex:
150
  try:
151
- from ingest import parse_ccusage as _pcc
152
  _c_args = type("a", (), {"file": None, "stdin": False, "codex": False})()
153
  _c_raw, _ = _grab_usage(_c_args)
154
- _ci, _co, _, _, _ = _pcc(_c_raw)
155
  if _co > 0:
156
- io_ratio = _ci / _co
157
  except Exception:
158
- pass # fall back to 2:1
159
 
160
  try:
161
  raw, how = _grab_usage(args)
162
- i, o, cw, cr, meta = ingest_meta(raw, io_ratio=io_ratio)
163
  except Exception as e:
164
  print(f"sigrank: {e}", file=sys.stderr)
165
  return 1
 
24
  import subprocess
25
  import sys
26
 
27
+ from ingest import ingest_meta, parse_ccusage
28
  from metrics import compute
29
  import db
30
 
 
144
 
145
  color = sys.stdout.isatty() and not args.no_color
146
 
147
+ # For Codex: detect Claude profile so the parser uses 1:9 pathway.
148
+ operator_profile = None
149
  if args.codex:
150
  try:
 
151
  _c_args = type("a", (), {"file": None, "stdin": False, "codex": False})()
152
  _c_raw, _ = _grab_usage(_c_args)
153
+ _ci, _co, _, _, _ = parse_ccusage(_c_raw)
154
  if _co > 0:
155
+ operator_profile = {"model_type": "claude", "io_ratio": _ci / _co}
156
  except Exception:
157
+ pass # no Claude data — Alpha pathway (3:2:1)
158
 
159
  try:
160
  raw, how = _grab_usage(args)
161
+ i, o, cw, cr, meta = ingest_meta(raw, operator_profile=operator_profile)
162
  except Exception as e:
163
  print(f"sigrank: {e}", file=sys.stderr)
164
  return 1
theme.py CHANGED
@@ -171,6 +171,35 @@ button.primary:hover, #compute-btn:hover { background: #d8a449 !important; }
171
  display: flex; justify-content: space-between;
172
  }
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  footer { display: none !important; }
175
 
176
  @media (max-width: 700px) {
 
171
  display: flex; justify-content: space-between;
172
  }
173
 
174
+ /* parsing mode badge on trading card */
175
+ .sig-card-mode {
176
+ font-size: 8px; color: var(--moses-dim); letter-spacing: 0.06em;
177
+ margin-bottom: 10px; padding: 2px 6px;
178
+ border: 1px dashed var(--moses-line); border-radius: 2px;
179
+ display: inline-block;
180
+ }
181
+
182
+ /* estimation asterisk on board rows */
183
+ .mb-est { color: var(--moses-gold); font-weight: 700; font-size: 10px; cursor: help; }
184
+
185
+ /* greatest hits table */
186
+ .greatest-hits { margin: 16px 0; }
187
+ .greatest-hits h4 { color: var(--moses-gold); font-size: 13px; letter-spacing: 0.08em; margin-bottom: 8px; }
188
+ .greatest-hits table { width: 100%; border-collapse: collapse; font-size: 11px; }
189
+ .greatest-hits th {
190
+ color: var(--moses-gold); font-size: 9px; letter-spacing: 0.06em;
191
+ text-transform: uppercase; text-align: left; padding: 6px 8px;
192
+ border-bottom: 1px solid var(--moses-gold);
193
+ }
194
+ .greatest-hits td {
195
+ color: var(--moses-dim); padding: 6px 8px;
196
+ border-bottom: 1px solid var(--moses-line);
197
+ }
198
+ .greatest-hits tr:first-child td { color: var(--moses-ink); }
199
+
200
+ /* HF login button styling */
201
+ #hf-login-btn { margin-bottom: 8px; }
202
+
203
  footer { display: none !important; }
204
 
205
  @media (max-width: 700px) {