@alexgreensh/token-optimizer 5.13.80 → 5.13.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/hooks/module_runner.py +1 -1
- package/package.json +1 -1
- package/skills/token-optimizer/assets/savings_story.js +6 -4
- package/skills/token-optimizer/scripts/_measure/__init__.py +2 -2
- package/skills/token-optimizer/scripts/_measure/audit.py +5 -6
- package/skills/token-optimizer/scripts/_measure/cache_policy.py +16 -39
- package/skills/token-optimizer/scripts/_measure/continuity.py +30 -3
- package/skills/token-optimizer/scripts/_measure/cost_per_message_trend.py +32 -15
- package/skills/token-optimizer/scripts/_measure/daemon.py +25 -2
- package/skills/token-optimizer/scripts/_measure/dashboard.py +2 -1
- package/skills/token-optimizer/scripts/_measure/feature_health.py +14 -5
- package/skills/token-optimizer/scripts/_measure/install_baseline.py +2 -1
- package/skills/token-optimizer/scripts/_measure/like_for_like.py +329 -0
- package/skills/token-optimizer/scripts/_measure/limit_units.py +39 -0
- package/skills/token-optimizer/scripts/_measure/model_families.py +133 -0
- package/skills/token-optimizer/scripts/_measure/own_price_card.py +38 -14
- package/skills/token-optimizer/scripts/_measure/pilot_report.py +197 -48
- package/skills/token-optimizer/scripts/_measure/pricing.py +7 -2
- package/skills/token-optimizer/scripts/_measure/project_md_walk.py +29 -0
- package/skills/token-optimizer/scripts/_measure/quality_archive.py +41 -19
- package/skills/token-optimizer/scripts/_measure/quality_continuity.py +2 -0
- package/skills/token-optimizer/scripts/_measure/savings.py +155 -80
- package/skills/token-optimizer/scripts/_measure/sessions.py +7 -2
- package/skills/token-optimizer/scripts/_measure/transcript_product.py +113 -58
- package/skills/token-optimizer/scripts/_measure/trends.py +20 -2
- package/skills/token-optimizer/scripts/_measure/ttl_file_memo.py +109 -0
- package/skills/token-optimizer/scripts/_measure/weekly_activity.py +64 -7
- package/skills/token-optimizer/scripts/antigravity_doctor.py +62 -17
- package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +20 -3
- package/skills/token-optimizer/scripts/antigravity_install.py +18 -9
- package/skills/token-optimizer/scripts/cloud_kit.py +1 -1
- package/skills/token-optimizer/scripts/codex_doctor.py +1 -1
- package/skills/token-optimizer/scripts/cursor_coach.py +5 -4
- package/skills/token-optimizer/scripts/cursor_install.py +1 -0
- package/skills/token-optimizer/scripts/delta_serve.py +174 -0
- package/skills/token-optimizer/scripts/devin_source.py +1 -1
- package/skills/token-optimizer/scripts/hermes_doctor.py +2 -1
- package/skills/token-optimizer/scripts/hermes_install.py +2 -1
- package/skills/token-optimizer/scripts/measure.py +14 -21
- package/skills/token-optimizer/scripts/read_cache.py +19 -76
- package/skills/token-optimizer/scripts/reset_epoch.py +5 -3
- package/skills/token-optimizer/scripts/session_activity.py +1 -1
- package/skills/token-optimizer/scripts/session_store.py +5 -6
|
@@ -12,6 +12,7 @@ from __future__ import annotations
|
|
|
12
12
|
import json
|
|
13
13
|
import sys
|
|
14
14
|
from datetime import datetime
|
|
15
|
+
from decimal import Decimal, ROUND_FLOOR, ROUND_HALF_UP
|
|
15
16
|
from pathlib import Path
|
|
16
17
|
|
|
17
18
|
from . import _compat
|
|
@@ -38,6 +39,39 @@ def _money(value):
|
|
|
38
39
|
return None
|
|
39
40
|
|
|
40
41
|
|
|
42
|
+
def _exact_cents(value):
|
|
43
|
+
try:
|
|
44
|
+
amount = Decimal(repr(float(value)))
|
|
45
|
+
except (TypeError, ValueError, OverflowError):
|
|
46
|
+
return Decimal(0)
|
|
47
|
+
if not amount.is_finite():
|
|
48
|
+
return Decimal(0)
|
|
49
|
+
return amount * 100
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _split_amounts(measured, estimated, routing):
|
|
53
|
+
"""Cent-rounded measured/estimated/routing parts and their exact-sum total."""
|
|
54
|
+
exact = [_exact_cents(value) for value in (measured, estimated, routing)]
|
|
55
|
+
target = int(sum(exact, Decimal(0)).quantize(Decimal(1), rounding=ROUND_HALF_UP))
|
|
56
|
+
cents = [int(value.to_integral_value(rounding=ROUND_FLOOR)) for value in exact]
|
|
57
|
+
leftover = target - sum(cents)
|
|
58
|
+
order = sorted(range(len(exact)),
|
|
59
|
+
key=lambda index: (exact[index] - cents[index], -index),
|
|
60
|
+
reverse=True)
|
|
61
|
+
for index in order[:leftover]:
|
|
62
|
+
cents[index] += 1
|
|
63
|
+
measured_usd, estimated_usd, routing_usd = (part / 100 for part in cents)
|
|
64
|
+
return {"saved_usd": target / 100,
|
|
65
|
+
"measured_usd": measured_usd,
|
|
66
|
+
"estimated_usd": estimated_usd,
|
|
67
|
+
"routing_usd": routing_usd}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _empty_split():
|
|
71
|
+
return {"saved_usd": None, "measured_usd": None,
|
|
72
|
+
"estimated_usd": None, "routing_usd": None}
|
|
73
|
+
|
|
74
|
+
|
|
41
75
|
# ---------------------------------------------------------------------------
|
|
42
76
|
# Install date
|
|
43
77
|
# ---------------------------------------------------------------------------
|
|
@@ -96,53 +130,140 @@ def _capacity(before_after):
|
|
|
96
130
|
return pct, basis
|
|
97
131
|
|
|
98
132
|
|
|
99
|
-
def
|
|
100
|
-
|
|
133
|
+
def _routing_excluded(si):
|
|
134
|
+
excluded = (si or {}).get("excluded_usd") or {}
|
|
135
|
+
return _num(excluded.get("routing"))
|
|
101
136
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
""
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
137
|
+
|
|
138
|
+
def _routing_credit_usd(si, all_in):
|
|
139
|
+
components = (all_in or {}).get("components_usd") or {}
|
|
140
|
+
for amount in (components.get("routing_estimated"),
|
|
141
|
+
((si or {}).get("excluded_usd") or {}).get("routing"),
|
|
142
|
+
(all_in or {}).get("routing_usd")):
|
|
143
|
+
value = _num(amount)
|
|
144
|
+
if value:
|
|
145
|
+
return value
|
|
146
|
+
return 0.0
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _routing_evidence_blocks_credit(routing):
|
|
150
|
+
return ((routing or {}).get("evidence") in ("pending", "unavailable"))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _measured_components_usd(container):
|
|
154
|
+
components = (container or {}).get("components_usd")
|
|
155
|
+
keys = ("logged", "class_premium", "reread")
|
|
156
|
+
if isinstance(components, dict) and any(key in components for key in keys):
|
|
157
|
+
return sum(_num(components.get(key)) for key in keys)
|
|
158
|
+
return None
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _floor_saved(split):
|
|
162
|
+
if split.get("saved_usd") is not None and split["saved_usd"] < 0.0:
|
|
163
|
+
split["saved_usd"] = 0.0
|
|
164
|
+
return split
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _action_estimated_usd(action):
|
|
168
|
+
display = (action or {}).get("display") or {}
|
|
169
|
+
display_estimates = display.get("estimates_usd")
|
|
170
|
+
if isinstance(display_estimates, dict):
|
|
171
|
+
return sum(_num(value) for value in display_estimates.values())
|
|
172
|
+
if (action or {}).get("estimated_usd") is not None:
|
|
173
|
+
return _num(action.get("estimated_usd"))
|
|
174
|
+
estimates = (action or {}).get("estimates_usd") or {}
|
|
175
|
+
if isinstance(estimates, dict):
|
|
176
|
+
return sum(_num(value) for value in estimates.values())
|
|
177
|
+
return 0.0
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _action_measured_usd(action, estimated):
|
|
181
|
+
display = (action or {}).get("display") or {}
|
|
182
|
+
measured = _measured_components_usd(display)
|
|
183
|
+
if measured is not None:
|
|
184
|
+
return measured
|
|
185
|
+
keys = ("logged_usd", "class_premium_usd", "reread_usd")
|
|
186
|
+
if any(key in (action or {}) for key in keys):
|
|
187
|
+
return sum(_num((action or {}).get(key)) for key in keys)
|
|
188
|
+
total = (action or {}).get("net_total_usd")
|
|
189
|
+
if total is None:
|
|
190
|
+
total = (action or {}).get("total_usd")
|
|
191
|
+
if total is None:
|
|
192
|
+
return None
|
|
193
|
+
return _num(total) - estimated
|
|
111
194
|
|
|
112
195
|
|
|
113
|
-
def
|
|
196
|
+
def _window_split(data):
|
|
197
|
+
"""One window's measured + estimated + routing split, routing included."""
|
|
198
|
+
action = (data or {}).get("action_savings") or {}
|
|
199
|
+
if not action:
|
|
200
|
+
return _empty_split()
|
|
201
|
+
estimated = _action_estimated_usd(action)
|
|
202
|
+
measured = _action_measured_usd(action, estimated)
|
|
203
|
+
if measured is None:
|
|
204
|
+
return _empty_split()
|
|
205
|
+
routing_data = (data or {}).get("model_routing") or {}
|
|
206
|
+
routing = (0.0 if _routing_evidence_blocks_credit(routing_data)
|
|
207
|
+
else _num(routing_data.get("realized_cost_usd")))
|
|
208
|
+
return _floor_saved(_split_amounts(measured, estimated, routing))
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _since_install(data):
|
|
114
212
|
"""All-in saved-since-install with the measured/estimated/routing split."""
|
|
115
213
|
si = (data or {}).get("since_install") or {}
|
|
214
|
+
routing_data = (data or {}).get("model_routing") or {}
|
|
215
|
+
blocked = _routing_evidence_blocks_credit(routing_data)
|
|
116
216
|
all_in = si.get("all_in") or {}
|
|
117
|
-
out = {"saved_usd": None, "measured_usd": None,
|
|
118
|
-
"estimated_usd": None, "routing_usd": None}
|
|
119
217
|
if isinstance(all_in, dict) and all_in:
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
218
|
+
routing = _routing_credit_usd(si, all_in)
|
|
219
|
+
if blocked or all_in.get("routing_pending"):
|
|
220
|
+
routing = 0.0
|
|
221
|
+
measured = _measured_components_usd(all_in)
|
|
222
|
+
if measured is None and all_in.get("measured_usd") is not None:
|
|
223
|
+
measured = _num(all_in.get("measured_usd"))
|
|
224
|
+
if measured is None and si.get("measured_usd") is not None:
|
|
225
|
+
measured = _num(si.get("measured_usd"))
|
|
226
|
+
if measured is None:
|
|
227
|
+
return _empty_split()
|
|
123
228
|
estimated = _num(all_in.get("estimated_usd"))
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
229
|
+
return _floor_saved(_split_amounts(measured, estimated, routing))
|
|
230
|
+
if si:
|
|
231
|
+
estimated = _num(si.get("estimated_usd"))
|
|
232
|
+
routing = 0.0 if blocked else _routing_excluded(si)
|
|
233
|
+
measured = _num(si.get("measured_usd"))
|
|
234
|
+
return _floor_saved(_split_amounts(measured, estimated, routing))
|
|
235
|
+
return _empty_split()
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
_BASIS_WORDS = {"standardized_reference_card": "priced on one standard model price card"}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _routing_basis(*payloads):
|
|
242
|
+
for data in payloads:
|
|
243
|
+
basis = (((data or {}).get("model_routing") or {}).get("realized_basis"))
|
|
244
|
+
if basis:
|
|
245
|
+
return basis
|
|
246
|
+
return None
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _capacity_alt(data, days=30):
|
|
250
|
+
"""Serge's same-budget capacity multiplier from the window's all-in saving."""
|
|
251
|
+
before_after = (data or {}).get("before_after") or {}
|
|
252
|
+
actual = _num(before_after.get("actual_monthly_usd"))
|
|
253
|
+
saved = _window_split(data).get("saved_usd")
|
|
254
|
+
if actual <= 0 or saved is None or saved <= 0 or not days:
|
|
143
255
|
return None
|
|
144
|
-
|
|
145
|
-
|
|
256
|
+
# actual is a monthly figure; a younger install's window saving must be
|
|
257
|
+
# put on the same 30-day footing or the share comes out too small.
|
|
258
|
+
saved = saved * 30.0 / days
|
|
259
|
+
denominator = actual + saved
|
|
260
|
+
if denominator <= 0:
|
|
261
|
+
return None
|
|
262
|
+
share = saved / denominator
|
|
263
|
+
if not 0 < share < 1:
|
|
264
|
+
return None
|
|
265
|
+
return {"capacity_pct": int(round((1 / (1 - share) - 1) * 100)),
|
|
266
|
+
"basis": "same budget, 1/(1-s) the work", "window_days": days}
|
|
146
267
|
|
|
147
268
|
|
|
148
269
|
# ---------------------------------------------------------------------------
|
|
@@ -197,11 +318,14 @@ def collect(facade=None):
|
|
|
197
318
|
report = {
|
|
198
319
|
"version": None, "install_date": None, "install_source": None,
|
|
199
320
|
"days_installed": None,
|
|
200
|
-
"capacity_pct": None, "capacity_basis": None,
|
|
321
|
+
"capacity_pct": None, "capacity_basis": None, "capacity_alt": None,
|
|
201
322
|
"saved_since_install_usd": None,
|
|
202
323
|
"saved_7d_usd": None,
|
|
203
324
|
"split": {"measured_usd": None, "estimated_usd": None,
|
|
204
325
|
"routing_usd": None},
|
|
326
|
+
"split_7d": {"measured_usd": None, "estimated_usd": None,
|
|
327
|
+
"routing_usd": None},
|
|
328
|
+
"routing_basis": None,
|
|
205
329
|
"routing": {"on": False, "since": None},
|
|
206
330
|
"setup_trims_applied": None,
|
|
207
331
|
"nudges": {"followed": None, "total": None},
|
|
@@ -237,15 +361,16 @@ def collect(facade=None):
|
|
|
237
361
|
pct, basis = _capacity(data.get("before_after"))
|
|
238
362
|
report["capacity_pct"] = pct
|
|
239
363
|
report["capacity_basis"] = basis
|
|
364
|
+
if pct is None:
|
|
365
|
+
report["capacity_alt"] = _capacity_alt(data, capacity_days)
|
|
240
366
|
|
|
241
367
|
try:
|
|
242
368
|
routing = _routing(f)
|
|
243
369
|
except Exception:
|
|
244
370
|
routing = {"on": False, "since": None}
|
|
245
371
|
report["routing"] = routing
|
|
246
|
-
routing_on = bool(routing.get("on"))
|
|
247
372
|
|
|
248
|
-
since = _since_install(data
|
|
373
|
+
since = _since_install(data)
|
|
249
374
|
report["saved_since_install_usd"] = since["saved_usd"]
|
|
250
375
|
report["split"] = {"measured_usd": since["measured_usd"],
|
|
251
376
|
"estimated_usd": since["estimated_usd"],
|
|
@@ -253,7 +378,12 @@ def collect(facade=None):
|
|
|
253
378
|
|
|
254
379
|
try:
|
|
255
380
|
data7 = data if capacity_days == 7 else _dashboard(f, 7)
|
|
256
|
-
|
|
381
|
+
seven = _window_split(data7)
|
|
382
|
+
report["saved_7d_usd"] = seven["saved_usd"]
|
|
383
|
+
report["split_7d"] = {"measured_usd": seven["measured_usd"],
|
|
384
|
+
"estimated_usd": seven["estimated_usd"],
|
|
385
|
+
"routing_usd": seven["routing_usd"]}
|
|
386
|
+
report["routing_basis"] = _routing_basis(data7, data)
|
|
257
387
|
except Exception:
|
|
258
388
|
pass
|
|
259
389
|
try:
|
|
@@ -276,11 +406,18 @@ def render(report):
|
|
|
276
406
|
lines = ["Token Optimizer %s, installed %s" % (version, install)]
|
|
277
407
|
|
|
278
408
|
pct = report.get("capacity_pct")
|
|
279
|
-
if pct is None:
|
|
280
|
-
lines.append("Capacity since install: not measured yet")
|
|
281
|
-
else:
|
|
409
|
+
if pct is not None:
|
|
282
410
|
lines.append("Capacity since install: %+d%% more work per token vs %s"
|
|
283
411
|
% (pct, report.get("capacity_basis") or "your baseline period"))
|
|
412
|
+
else:
|
|
413
|
+
alt = report.get("capacity_alt") or {}
|
|
414
|
+
alt_pct = alt.get("capacity_pct")
|
|
415
|
+
if alt_pct is None:
|
|
416
|
+
lines.append("Capacity since install: not measured yet")
|
|
417
|
+
else:
|
|
418
|
+
lines.append("Capacity, last %d days: %+d%% more work for the same budget (%s)"
|
|
419
|
+
% (alt.get("window_days") or 30, alt_pct,
|
|
420
|
+
alt.get("basis") or "same budget, 1/(1-s) the work"))
|
|
284
421
|
|
|
285
422
|
saved = _money(report.get("saved_since_install_usd"))
|
|
286
423
|
lines.append("Saved since install: %s (all-in, API-price equivalent)"
|
|
@@ -289,11 +426,23 @@ def render(report):
|
|
|
289
426
|
lines.append("Saved in the last 7 days: %s"
|
|
290
427
|
% (saved7 if saved7 is not None else "not available yet"))
|
|
291
428
|
|
|
429
|
+
split_keys = ("measured_usd", "estimated_usd", "routing_usd")
|
|
430
|
+
routing_basis = report.get("routing_basis")
|
|
431
|
+
# The payload carries an internal id; the reader gets plain words.
|
|
432
|
+
routing_basis = _BASIS_WORDS.get(routing_basis, routing_basis and routing_basis.replace("_", " "))
|
|
433
|
+
routing_label = " (estimated%s)" % (", " + routing_basis if routing_basis else "")
|
|
434
|
+
|
|
435
|
+
split7 = report.get("split_7d") or {}
|
|
436
|
+
parts7 = [_money(split7.get(k)) for k in split_keys]
|
|
437
|
+
if any(p is not None for p in parts7):
|
|
438
|
+
args7 = tuple(p if p is not None else "$0.00" for p in parts7) + (routing_label,)
|
|
439
|
+
lines.append("Last 7 days split - measured %s / estimated %s / routing %s%s" % args7)
|
|
440
|
+
|
|
292
441
|
split = report.get("split") or {}
|
|
293
|
-
parts = [_money(split.get(k)) for k in
|
|
442
|
+
parts = [_money(split.get(k)) for k in split_keys]
|
|
294
443
|
if any(p is not None for p in parts):
|
|
295
|
-
|
|
296
|
-
|
|
444
|
+
args = tuple(p if p is not None else "$0.00" for p in parts) + (routing_label,)
|
|
445
|
+
lines.append("Since install split - measured %s / estimated %s / routing %s%s" % args)
|
|
297
446
|
else:
|
|
298
447
|
lines.append("Since install split: not available yet")
|
|
299
448
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Definition-only implementations; mutable state belongs to measure."""
|
|
2
2
|
from __future__ import annotations
|
|
3
3
|
from . import _compat
|
|
4
|
+
from . import model_families
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
def _get_model_cost(model, input_tokens, output_tokens, cache_read=None, cache_create=None, tier=None,
|
|
@@ -1506,11 +1507,15 @@ def _routing_target_model():
|
|
|
1506
1507
|
def _routing_reference_model(family):
|
|
1507
1508
|
"""The model id whose card values a family in the standardized routing
|
|
1508
1509
|
comparison, in both arms: the newest generation card of its tier (legacy
|
|
1509
|
-
Sonnet is valued as Sonnet). Other labels (provider models) stand
|
|
1510
|
-
themselves."""
|
|
1510
|
+
Sonnet is valued as Sonnet). Other labels (unpriced provider models) stand
|
|
1511
|
+
for themselves."""
|
|
1511
1512
|
_measure_facade = _compat.facade()
|
|
1512
1513
|
if family == "sonnet_legacy":
|
|
1513
1514
|
family = "sonnet"
|
|
1515
|
+
foreign = model_families.newest_reference(
|
|
1516
|
+
family, _measure_facade.OPENAI_MODEL_PRICING, _measure_facade.GEMINI_MODEL_PRICING)
|
|
1517
|
+
if foreign:
|
|
1518
|
+
return foreign
|
|
1514
1519
|
return _measure_facade._newest_generation_model(family) or family
|
|
1515
1520
|
|
|
1516
1521
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Which directories the project CLAUDE.md walk must skip.
|
|
2
|
+
|
|
3
|
+
The walk climbs from cwd looking for project CLAUDE.md files. The home
|
|
4
|
+
directory is skipped because ~/CLAUDE.md and the global CLAUDE.md are counted
|
|
5
|
+
separately. When HOME points somewhere else (a test sandbox, a scratch run),
|
|
6
|
+
the account's real home must be skipped too, or the walk reads the user's
|
|
7
|
+
own instruction files into a run that was meant to be isolated.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def homes(facade):
|
|
13
|
+
"""HOME as this process sees it, plus the OS account's own home."""
|
|
14
|
+
found = [facade.HOME]
|
|
15
|
+
try:
|
|
16
|
+
real = facade._real_account_home()
|
|
17
|
+
except Exception:
|
|
18
|
+
real = None
|
|
19
|
+
if real is not None:
|
|
20
|
+
found.append(real)
|
|
21
|
+
return found
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def is_home(facade, parent, known=None):
|
|
25
|
+
"""True when ``parent`` is HOME or the account's real home."""
|
|
26
|
+
for home in (known if known is not None else homes(facade)):
|
|
27
|
+
if parent == home or facade._same_path(parent, home):
|
|
28
|
+
return True
|
|
29
|
+
return False
|
|
@@ -4387,25 +4387,27 @@ def _record_checkpoint_metadata(result, cache_path, trigger, checkpoint_path, *,
|
|
|
4387
4387
|
if quality_score is not None:
|
|
4388
4388
|
result["last_checkpoint_quality_score"] = round(quality_score, 1)
|
|
4389
4389
|
_measure_facade._write_quality_cache(cache_path, result)
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
checkpoint_path=checkpoint_path,
|
|
4394
|
-
fill_pct=fill_pct,
|
|
4395
|
-
quality_score=quality_score,
|
|
4396
|
-
)
|
|
4390
|
+
# No _append_checkpoint_event here: compact_capture now emits the telemetry
|
|
4391
|
+
# event for every successful write, so emitting here too would double-count
|
|
4392
|
+
# the progressive/quality/milestone captures that call both (#358).
|
|
4397
4393
|
|
|
4398
4394
|
|
|
4399
|
-
def _append_checkpoint_event(session_id, trigger, checkpoint_path, *, fill_pct=None, quality_score=None):
|
|
4395
|
+
def _append_checkpoint_event(session_id, trigger, checkpoint_path, *, fill_pct=None, quality_score=None, placeholder=False, quality_unmeasured=False):
|
|
4400
4396
|
"""Append a deterministic local checkpoint event for rollout telemetry."""
|
|
4401
4397
|
_measure_facade = _compat.facade()
|
|
4402
4398
|
if not _measure_facade._CHECKPOINT_TELEMETRY_ENABLED:
|
|
4403
4399
|
return
|
|
4404
4400
|
try:
|
|
4405
4401
|
_measure_facade.CHECKPOINT_EVENT_LOG.parent.mkdir(parents=True, exist_ok=True)
|
|
4402
|
+
try:
|
|
4403
|
+
_runtime = _measure_facade.detect_runtime()
|
|
4404
|
+
except Exception:
|
|
4405
|
+
_runtime = "claude"
|
|
4406
4406
|
event = {
|
|
4407
4407
|
"timestamp": _measure_facade.datetime.now(_measure_facade.timezone.utc).isoformat(),
|
|
4408
|
-
|
|
4408
|
+
# Keep the established "claude-code" label on Claude rows; other
|
|
4409
|
+
# runtimes (codex, ...) were previously lumped under it (#358).
|
|
4410
|
+
"platform": "claude-code" if _runtime == "claude" else _runtime,
|
|
4409
4411
|
"session_id": _measure_facade.sanitize_session_id(session_id),
|
|
4410
4412
|
"trigger": trigger,
|
|
4411
4413
|
"checkpoint_path": str(checkpoint_path),
|
|
@@ -4414,6 +4416,16 @@ def _append_checkpoint_event(session_id, trigger, checkpoint_path, *, fill_pct=N
|
|
|
4414
4416
|
event["fill_pct"] = round(fill_pct, 1)
|
|
4415
4417
|
if quality_score is not None:
|
|
4416
4418
|
event["quality_score"] = round(quality_score, 1)
|
|
4419
|
+
if placeholder:
|
|
4420
|
+
# A "No transcript data available" capture holds nothing a restore
|
|
4421
|
+
# could use; count it, but never as a substantive checkpoint (#358).
|
|
4422
|
+
event["placeholder"] = True
|
|
4423
|
+
if quality_unmeasured:
|
|
4424
|
+
# The session's quality was never measured; any score the caller
|
|
4425
|
+
# saw was the scorer's fallback. The event omits quality_score and
|
|
4426
|
+
# carries this marker instead, so checkpoint-stats never shows a
|
|
4427
|
+
# phantom 100 as if it were measured (#360 class, review cycle 9).
|
|
4428
|
+
event["quality_unmeasured"] = True
|
|
4417
4429
|
fd = _measure_facade.os.open(str(_measure_facade.CHECKPOINT_EVENT_LOG), _measure_facade.os.O_WRONLY | _measure_facade.os.O_CREAT | _measure_facade.os.O_APPEND, 0o600)
|
|
4418
4430
|
with _measure_facade.os.fdopen(fd, "a", encoding="utf-8") as f:
|
|
4419
4431
|
f.write(_measure_facade.json.dumps(event) + "\n")
|
|
@@ -4449,29 +4461,35 @@ def checkpoint_stats(days=None, as_json=None):
|
|
|
4449
4461
|
except OSError:
|
|
4450
4462
|
events = []
|
|
4451
4463
|
|
|
4452
|
-
|
|
4464
|
+
recent_unfiltered = [e for e in events if e["_ts"] >= cutoff]
|
|
4465
|
+
placeholders_recent = sum(1 for e in recent_unfiltered if e.get("placeholder"))
|
|
4466
|
+
recent = [e for e in recent_unfiltered if not e.get("placeholder")]
|
|
4453
4467
|
by_trigger = {}
|
|
4454
4468
|
for event in recent:
|
|
4455
4469
|
trigger = event.get("trigger", "unknown")
|
|
4456
4470
|
by_trigger[trigger] = by_trigger.get(trigger, 0) + 1
|
|
4457
4471
|
|
|
4458
4472
|
last_event = None
|
|
4459
|
-
if
|
|
4460
|
-
|
|
4473
|
+
if recent_unfiltered:
|
|
4474
|
+
recent_unfiltered.sort(key=lambda e: ((_measure_facade := _compat.facade()), e["_ts"])[1], reverse=True)
|
|
4461
4475
|
last_event = {
|
|
4462
|
-
"timestamp":
|
|
4463
|
-
"session_id":
|
|
4464
|
-
"trigger":
|
|
4465
|
-
"fill_pct":
|
|
4466
|
-
"quality_score":
|
|
4476
|
+
"timestamp": recent_unfiltered[0].get("timestamp"),
|
|
4477
|
+
"session_id": recent_unfiltered[0].get("session_id"),
|
|
4478
|
+
"trigger": recent_unfiltered[0].get("trigger"),
|
|
4479
|
+
"fill_pct": recent_unfiltered[0].get("fill_pct"),
|
|
4480
|
+
"quality_score": recent_unfiltered[0].get("quality_score"),
|
|
4467
4481
|
}
|
|
4468
4482
|
|
|
4483
|
+
unmeasured_quality_recent = sum(1 for e in recent if e.get("quality_unmeasured"))
|
|
4484
|
+
|
|
4469
4485
|
summary = {
|
|
4470
4486
|
"enabled": _measure_facade._CHECKPOINT_TELEMETRY_ENABLED,
|
|
4471
4487
|
"event_log": str(_measure_facade.CHECKPOINT_EVENT_LOG),
|
|
4472
4488
|
"days": days,
|
|
4473
|
-
"total_events":
|
|
4489
|
+
"total_events": sum(1 for e in events if not e.get("placeholder")),
|
|
4474
4490
|
"recent_events": len(recent),
|
|
4491
|
+
"placeholder_events": placeholders_recent,
|
|
4492
|
+
"unmeasured_quality_events": unmeasured_quality_recent,
|
|
4475
4493
|
"by_trigger": dict(sorted(by_trigger.items())),
|
|
4476
4494
|
"last_event": last_event,
|
|
4477
4495
|
}
|
|
@@ -4485,6 +4503,10 @@ def checkpoint_stats(days=None, as_json=None):
|
|
|
4485
4503
|
print(f" Event log: {summary['event_log']}")
|
|
4486
4504
|
print(f" Total events: {summary['total_events']}")
|
|
4487
4505
|
print(f" Recent events: {summary['recent_events']}")
|
|
4506
|
+
if summary["placeholder_events"]:
|
|
4507
|
+
print(f" Placeholder captures (no transcript data, unrestorable): {summary['placeholder_events']}")
|
|
4508
|
+
if summary["unmeasured_quality_events"]:
|
|
4509
|
+
print(f" Unmeasured-quality captures (no quality score recorded): {summary['unmeasured_quality_events']}")
|
|
4488
4510
|
if summary["by_trigger"]:
|
|
4489
4511
|
print(" By trigger:")
|
|
4490
4512
|
for trigger, count in summary["by_trigger"].items():
|
|
@@ -4817,7 +4839,7 @@ def _checkpoint_restore_credited_recently(session_id, window_seconds=None):
|
|
|
4817
4839
|
try:
|
|
4818
4840
|
row = conn.execute(
|
|
4819
4841
|
"SELECT 1 FROM savings_events WHERE event_type='checkpoint_restore' "
|
|
4820
|
-
"AND session_id = ? AND timestamp >= ? LIMIT 1",
|
|
4842
|
+
"AND session_id = ? AND timestamp >= ? AND tokens_saved > 0 LIMIT 1",
|
|
4821
4843
|
(session_id, cutoff),
|
|
4822
4844
|
).fetchone()
|
|
4823
4845
|
finally:
|
|
@@ -39,5 +39,7 @@ def session_quality(context_tokens=None, context_window=None, model=None, compac
|
|
|
39
39
|
'model_context_window': context_window, 'unmeasured': not measured,
|
|
40
40
|
}
|
|
41
41
|
result = measure.compute_quality_score(data)
|
|
42
|
+
if not measured:
|
|
43
|
+
result['score'] = None
|
|
42
44
|
result['band'] = measure.score_to_band(result['score']) if measured else 'Unmeasured'
|
|
43
45
|
return result
|