Burnmydays Claude Sonnet 4.6 commited on
Commit
d30f28f
·
1 Parent(s): 22eb036

feat: CODEX.md items 1+4 — turn-delta anchor + board estimated marker

Browse files

Item 1 (anchor refinement): parse_codex now uses turn-delta method when
daily granularity is present — estimates cache_create from per-day context
growth deltas instead of fixed 2:1. sigrank.py --codex first fetches
Claude's measured I/O ratio and passes it as fallback anchor, so the
estimate is grounded in real data rather than a provisional constant.

Item 4 (board marker): estimated rows (Codex-anchored) now show a dimmed
~ marker next to the operator name in the leaderboard HTML so measured
vs estimated is visible at a glance.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (3) hide show
  1. app.py +4 -1
  2. ingest.py +61 -27
  3. sigrank.py +15 -1
app.py CHANGED
@@ -4,6 +4,7 @@ Operator pastes ccusage/codex output -> ingestion -> full profile + board placem
4
  Board ranks by Net Volumetric Yield (Υ). Four raw integers drive everything.
5
  """
6
  import gradio as gr
 
7
  import math as _math
8
  import re as _re
9
  from metrics import compute, SEED
@@ -60,9 +61,11 @@ def board_html(extra=None):
60
  d=f"{m['dev10x']:.2f}" if m['dev10x'] is not None else "\u2014"
61
  rank_cls = f"mb-rank-{i}" if i <= 3 else ""
62
  cls = "mb-row you" if you else ("mb-row rank1" if i==1 else "mb-row")
 
 
63
  out.append(f'<div class="{cls}">'
64
  f'<span class="mb-rank {rank_cls}">{i}</span>'
65
- f'<span class="mb-op"><b>{n}</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>'
66
  f'<span class="mb-num">{m["snr"]:.3f}</span>'
67
  f'<span class="mb-num">{d}</span>'
68
  f'<span class="mb-num">{m["velocity"]:.2f}</span>'
 
4
  Board ranks by Net Volumetric Yield (Υ). Four raw integers drive everything.
5
  """
6
  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
 
61
  d=f"{m['dev10x']:.2f}" if m['dev10x'] is not None else "\u2014"
62
  rank_cls = f"mb-rank-{i}" if i <= 3 else ""
63
  cls = "mb-row you" if you else ("mb-row rank1" if i==1 else "mb-row")
64
+ ne = _html.escape(n)
65
+ est_mark = " <span class='mb-est' title='estimated (Codex anchor)'>~</span>" if m.get("cost_estimated") else ""
66
  out.append(f'<div class="{cls}">'
67
  f'<span class="mb-rank {rank_cls}">{i}</span>'
68
+ 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>'
69
  f'<span class="mb-num">{m["snr"]:.3f}</span>'
70
  f'<span class="mb-num">{d}</span>'
71
  f'<span class="mb-num">{m["velocity"]:.2f}</span>'
ingest.py CHANGED
@@ -53,62 +53,96 @@ 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):
57
  """
58
  Codex reports combined input_tokens (incl. cached) + cached_input_tokens +
59
- output + reasoning. No cache_create exists. We estimate the split using the
60
- measured field anchor input:output ~ 2:1 (provisional, being refined):
61
- est_fresh_input = 2 * output
62
- cache_create = combined_input - est_fresh_input (clamped >=0)
63
- cache_read = cached_input_tokens (measured)
64
- Returns (i, o, cache_create, cache_read, meta) with a directional caveat.
 
 
 
 
 
65
  """
66
  d = json.loads(text) if isinstance(text, str) else text
67
  tot = {"in":0, "cached":0, "out":0, "reason":0, "cost":0.0}
68
- def add(e):
 
69
  if not isinstance(e, dict): return
70
- tot["in"] += e.get("input_tokens", e.get("inputTokens",0)) or 0
71
- tot["cached"] += e.get("cached_input_tokens", e.get("cachedInputTokens",0)) or 0
72
- tot["out"] += e.get("output_tokens", e.get("outputTokens",0)) or 0
73
- tot["reason"] += e.get("reasoning_output_tokens", e.get("reasoningOutputTokens",0)) or 0
74
- tot["cost"] += e.get("costUSD", e.get("cost",0)) or 0.0
 
 
 
 
75
  if isinstance(d, list):
76
  for e in d: add(e)
77
  else:
78
  for key in ("daily","session","sessions","data","entries","events"):
79
  v = d.get(key) if isinstance(d, dict) else None
80
  if isinstance(v, list):
81
- for e in v: add(e)
82
  break
83
  if isinstance(v, dict):
84
  for e in v.values(): add(e)
85
  break
86
  else:
87
  add(d)
 
88
  combined_in = tot["in"]; read = tot["cached"]; O = tot["out"] + tot["reason"]
89
- est_fresh = 2 * O
90
- create = combined_in - est_fresh
91
- if create >= 0:
92
- I = est_fresh
93
- caveat = ("estimated \u2191 input-heavy vs 2:1 baseline" if combined_in > est_fresh
94
- else "estimated \u2248 2:1 baseline")
95
- else:
96
  create = 0
97
- I = max(combined_in - read, 0) or combined_in
98
- caveat = "estimated \u2193 output-rich (2:1 anchor inverted)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  meta = {"source":"codex", "estimated":True, "caveat":caveat,
100
- "anchor":"input:output 2:1 (provisional)",
101
  "cost": tot["cost"] if tot["cost"] > 0 else None}
102
- return I, O, create, read, meta
103
 
104
- def ingest_meta(text):
105
  """Returns (i,o,cw,cr,meta) with estimated/caveat/cost."""
106
  text=text.strip()
107
  if not text: raise ValueError("empty")
108
  if text[0] in "{[":
109
  d=json.loads(text)
110
  if is_codex_shape(d):
111
- return parse_codex(d)
112
  i,o,cw,cr,cost = parse_ccusage(text)
113
  return i,o,cw,cr,{"source":"ccusage","estimated":False,"caveat":None,"cost":cost}
114
  i,o,cw,cr = parse_four(text)
 
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)
sigrank.py CHANGED
@@ -143,9 +143,23 @@ def main(argv=None):
143
  args.stdin = args.stdin_dash == "-"
144
 
145
  color = sys.stdout.isatty() and not args.no_color
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  try:
147
  raw, how = _grab_usage(args)
148
- i, o, cw, cr, meta = ingest_meta(raw)
149
  except Exception as e:
150
  print(f"sigrank: {e}", file=sys.stderr)
151
  return 1
 
143
  args.stdin = args.stdin_dash == "-"
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