@alexgreensh/token-optimizer 5.13.73 → 5.13.75
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 -8
- package/hooks/sessionstart_runner.py +25 -0
- package/package.json +1 -1
- package/skills/token-optimizer/assets/dashboard.html +20 -7
- package/skills/token-optimizer/assets/savings_story.js +10 -6
- package/skills/token-optimizer/references/cli-reference.md +9 -0
- package/skills/token-optimizer/scripts/_measure/__init__.py +3 -2
- package/skills/token-optimizer/scripts/_measure/baseline_provenance.py +3 -1
- package/skills/token-optimizer/scripts/_measure/cache_policy.py +2 -79
- package/skills/token-optimizer/scripts/_measure/cli.py +24 -0
- package/skills/token-optimizer/scripts/_measure/daemon.py +77 -15
- package/skills/token-optimizer/scripts/_measure/install_baseline.py +157 -0
- package/skills/token-optimizer/scripts/_measure/mod_action_booking.py +16 -3
- package/skills/token-optimizer/scripts/_measure/mod_action_diagnostics.py +24 -0
- package/skills/token-optimizer/scripts/_measure/mod_action_selftest.py +33 -0
- package/skills/token-optimizer/scripts/_measure/mod_compaction_booking.py +4 -4
- package/skills/token-optimizer/scripts/_measure/mod_fresh_booking.py +2 -2
- package/skills/token-optimizer/scripts/_measure/nudge_followthrough.py +334 -0
- package/skills/token-optimizer/scripts/_measure/own_price_card.py +3 -0
- package/skills/token-optimizer/scripts/_measure/pilot_report.py +349 -0
- package/skills/token-optimizer/scripts/_measure/pricing.py +15 -414
- package/skills/token-optimizer/scripts/_measure/quality_archive.py +5 -10
- package/skills/token-optimizer/scripts/_measure/routing_credit.py +60 -0
- package/skills/token-optimizer/scripts/_measure/routing_default.py +374 -0
- package/skills/token-optimizer/scripts/_measure/routing_savings.py +368 -0
- package/skills/token-optimizer/scripts/_measure/runway_all_in.py +42 -0
- package/skills/token-optimizer/scripts/_measure/savings.py +33 -247
- package/skills/token-optimizer/scripts/_measure/savings_story.py +8 -1
- package/skills/token-optimizer/scripts/_measure/session_activity.py +128 -0
- package/skills/token-optimizer/scripts/_measure/ttl_period.py +59 -0
- package/skills/token-optimizer/scripts/cloud_kit.py +1 -4
- package/skills/token-optimizer/scripts/codex_doctor.py +1 -10
- package/skills/token-optimizer/scripts/hermes_doctor.py +1 -10
- package/skills/token-optimizer/scripts/hermes_install.py +1 -10
- package/skills/token-optimizer/scripts/injection.py +5 -2
- package/skills/token-optimizer/scripts/measure.py +4 -4
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"""Weekly pilot report: `measure.py pilot-report`.
|
|
2
|
+
|
|
3
|
+
One paste-able Slack summary a pilot user can run once a week. Plain text,
|
|
4
|
+
under a dozen lines, no personal paths; `--json` emits the same data. Every
|
|
5
|
+
figure comes from the same payloads the dashboard renders, so the report can
|
|
6
|
+
never quote a different dollar than the Savings page.
|
|
7
|
+
|
|
8
|
+
The report uses the shipped baseline, routing, ledger and nudge readers.
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import sys
|
|
14
|
+
from datetime import datetime
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from . import _compat
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _facade(facade):
|
|
22
|
+
return facade if facade is not None else _compat.facade()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _num(value):
|
|
26
|
+
try:
|
|
27
|
+
return float(value)
|
|
28
|
+
except (TypeError, ValueError):
|
|
29
|
+
return 0.0
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _money(value):
|
|
33
|
+
if value is None:
|
|
34
|
+
return None
|
|
35
|
+
try:
|
|
36
|
+
return "${:,.2f}".format(float(value))
|
|
37
|
+
except (TypeError, ValueError):
|
|
38
|
+
return None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# ---------------------------------------------------------------------------
|
|
42
|
+
# Install date
|
|
43
|
+
# ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
def _install(facade):
|
|
46
|
+
"""(date, source) of the install evidence, or (None, None)."""
|
|
47
|
+
try:
|
|
48
|
+
from . import baseline_provenance
|
|
49
|
+
ev = baseline_provenance.install_evidence(facade) or {}
|
|
50
|
+
if ev.get("date"):
|
|
51
|
+
return ev.get("date"), ev.get("source")
|
|
52
|
+
except Exception:
|
|
53
|
+
pass
|
|
54
|
+
try:
|
|
55
|
+
day = facade._install_date()
|
|
56
|
+
if day:
|
|
57
|
+
return day, None
|
|
58
|
+
except Exception:
|
|
59
|
+
pass
|
|
60
|
+
return None, None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# ---------------------------------------------------------------------------
|
|
64
|
+
# Savings payloads
|
|
65
|
+
# ---------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
def _dashboard(facade, days):
|
|
68
|
+
"""The dashboard savings payload for a window, or {} on any failure."""
|
|
69
|
+
try:
|
|
70
|
+
data = facade._dashboard_savings_data(days=days)
|
|
71
|
+
return data if isinstance(data, dict) else {}
|
|
72
|
+
except Exception:
|
|
73
|
+
return {}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _capacity(before_after):
|
|
77
|
+
"""(+pct, basis) from a before_after payload.
|
|
78
|
+
|
|
79
|
+
Mirrors the dashboard limits card: the counterfactual/actual cost ratio
|
|
80
|
+
minus one, shown only when the transformation state is ok and the
|
|
81
|
+
counterfactual actually exceeds actual. `basis` names what the "before"
|
|
82
|
+
side is; a fallback window (the tool may already have been running in the
|
|
83
|
+
earliest tracked month) is labeled as such instead of claiming "before".
|
|
84
|
+
"""
|
|
85
|
+
ba = before_after if isinstance(before_after, dict) else {}
|
|
86
|
+
actual = _num(ba.get("actual_monthly_usd"))
|
|
87
|
+
before = _num(ba.get("counterfactual_monthly_usd"))
|
|
88
|
+
if ba.get("transformation_state") != "ok" or not (actual > 0 and before > actual):
|
|
89
|
+
return None, None
|
|
90
|
+
pct = int(round((before / actual - 1.0) * 100))
|
|
91
|
+
prov = ba.get("baseline_provenance") or {}
|
|
92
|
+
if prov.get("coverage") == "pre_install":
|
|
93
|
+
basis = "your 30 days before install"
|
|
94
|
+
else:
|
|
95
|
+
basis = "your earliest tracked month (fallback window)"
|
|
96
|
+
return pct, basis
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _routing_in_total(all_in):
|
|
100
|
+
"""The routing dollars already inside all_in.total_usd.
|
|
101
|
+
|
|
102
|
+
`_savings_since_install` adds the routing figure only when a baseline
|
|
103
|
+
kind exists; components_usd.routing_estimated carries that exact amount.
|
|
104
|
+
"""
|
|
105
|
+
all_in = all_in if isinstance(all_in, dict) else {}
|
|
106
|
+
parts = all_in.get("components_usd") or {}
|
|
107
|
+
amount = parts.get("routing_estimated")
|
|
108
|
+
if amount is None:
|
|
109
|
+
amount = all_in.get("routing_usd") if all_in.get("routing_baseline_source") else 0.0
|
|
110
|
+
return _num(amount)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _since_install(data, routing_on):
|
|
114
|
+
"""All-in saved-since-install with the measured/estimated/routing split."""
|
|
115
|
+
si = (data or {}).get("since_install") or {}
|
|
116
|
+
all_in = si.get("all_in") or {}
|
|
117
|
+
out = {"saved_usd": None, "measured_usd": None,
|
|
118
|
+
"estimated_usd": None, "routing_usd": None}
|
|
119
|
+
if isinstance(all_in, dict) and all_in:
|
|
120
|
+
routing_in = _routing_in_total(all_in)
|
|
121
|
+
total = _num(all_in.get("total_usd"))
|
|
122
|
+
saved = total
|
|
123
|
+
estimated = _num(all_in.get("estimated_usd"))
|
|
124
|
+
routing = routing_in
|
|
125
|
+
out.update(saved_usd=round(saved, 2),
|
|
126
|
+
estimated_usd=round(estimated, 2),
|
|
127
|
+
routing_usd=round(routing, 2),
|
|
128
|
+
measured_usd=round(max(0.0, saved - estimated - routing), 2))
|
|
129
|
+
elif si:
|
|
130
|
+
# Pre-all-in shape: measured + estimated only, no routing credit.
|
|
131
|
+
out["saved_usd"] = round(_num(si.get("total_usd")), 2)
|
|
132
|
+
out["measured_usd"] = round(_num(si.get("measured_usd")), 2)
|
|
133
|
+
out["estimated_usd"] = round(_num(si.get("estimated_usd")), 2)
|
|
134
|
+
return out
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _saved_7d(data7, routing_on):
|
|
138
|
+
"""All-in dollars saved in the last 7 days (the headline recipe:
|
|
139
|
+
action_savings.total_usd plus realized routing)."""
|
|
140
|
+
action = (data7 or {}).get("action_savings") or {}
|
|
141
|
+
total = action.get("total_usd")
|
|
142
|
+
if total is None:
|
|
143
|
+
return None
|
|
144
|
+
routing = _num(((data7 or {}).get("model_routing") or {}).get("realized_cost_usd"))
|
|
145
|
+
return round(_num(total) + routing, 2)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
# ---------------------------------------------------------------------------
|
|
149
|
+
# Routing guidance state
|
|
150
|
+
# ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
def _routing(facade):
|
|
153
|
+
from . import routing_default
|
|
154
|
+
snap = getattr(facade, 'SNAPSHOT_DIR', None)
|
|
155
|
+
if snap is None:
|
|
156
|
+
return {'on': False, 'since': None}
|
|
157
|
+
try:
|
|
158
|
+
failure = (Path(snap) / 'routing_injection_failure').read_text(encoding='utf-8').strip()
|
|
159
|
+
if failure:
|
|
160
|
+
return {'on': False, 'since': None, 'failure': failure[:200]}
|
|
161
|
+
except OSError:
|
|
162
|
+
pass
|
|
163
|
+
injected, removed = routing_default.routing_credit_window(data_dir=snap)
|
|
164
|
+
return {'on': bool(injected and not removed), 'since': injected if injected and not removed else None}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
# ---------------------------------------------------------------------------
|
|
168
|
+
# Setup trims
|
|
169
|
+
# ---------------------------------------------------------------------------
|
|
170
|
+
|
|
171
|
+
def _trims(facade, data):
|
|
172
|
+
from . import structural_ledger
|
|
173
|
+
try:
|
|
174
|
+
return sum(1 for row in structural_ledger.load_trims(facade.SNAPSHOT_DIR) if not row.get('closed_at'))
|
|
175
|
+
except Exception:
|
|
176
|
+
return None
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _nudges(facade):
|
|
180
|
+
from . import nudge_followthrough
|
|
181
|
+
try:
|
|
182
|
+
return nudge_followthrough.weekly_counts(facade)
|
|
183
|
+
except Exception:
|
|
184
|
+
return {'followed': None, 'total': None}
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
# ---------------------------------------------------------------------------
|
|
188
|
+
# Report assembly
|
|
189
|
+
# ---------------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
def collect(facade=None):
|
|
192
|
+
"""The pilot report as a plain dict. Never raises."""
|
|
193
|
+
try:
|
|
194
|
+
f = _facade(facade)
|
|
195
|
+
except Exception:
|
|
196
|
+
f = None
|
|
197
|
+
report = {
|
|
198
|
+
"version": None, "install_date": None, "install_source": None,
|
|
199
|
+
"days_installed": None,
|
|
200
|
+
"capacity_pct": None, "capacity_basis": None,
|
|
201
|
+
"saved_since_install_usd": None,
|
|
202
|
+
"saved_7d_usd": None,
|
|
203
|
+
"split": {"measured_usd": None, "estimated_usd": None,
|
|
204
|
+
"routing_usd": None},
|
|
205
|
+
"routing": {"on": False, "since": None},
|
|
206
|
+
"setup_trims_applied": None,
|
|
207
|
+
"nudges": {"followed": None, "total": None},
|
|
208
|
+
}
|
|
209
|
+
if f is None:
|
|
210
|
+
return report
|
|
211
|
+
try:
|
|
212
|
+
report["version"] = getattr(f, "TOKEN_OPTIMIZER_VERSION", None) or "unknown"
|
|
213
|
+
except Exception:
|
|
214
|
+
report["version"] = "unknown"
|
|
215
|
+
|
|
216
|
+
install_date = install_source = None
|
|
217
|
+
try:
|
|
218
|
+
install_date, install_source = _install(f)
|
|
219
|
+
except Exception:
|
|
220
|
+
pass
|
|
221
|
+
report["install_date"] = install_date
|
|
222
|
+
report["install_source"] = install_source
|
|
223
|
+
days_installed = None
|
|
224
|
+
if install_date:
|
|
225
|
+
try:
|
|
226
|
+
days_installed = (f.datetime.now().date()
|
|
227
|
+
- f.datetime.fromisoformat(install_date).date()).days + 1
|
|
228
|
+
days_installed = max(1, days_installed)
|
|
229
|
+
except Exception:
|
|
230
|
+
days_installed = None
|
|
231
|
+
report["days_installed"] = days_installed
|
|
232
|
+
|
|
233
|
+
# The capacity window is "since install": the whole post-install period,
|
|
234
|
+
# bounded at 30 days for stability (the dashboard card's own window).
|
|
235
|
+
capacity_days = min(30, days_installed) if days_installed else 30
|
|
236
|
+
data = _dashboard(f, capacity_days)
|
|
237
|
+
pct, basis = _capacity(data.get("before_after"))
|
|
238
|
+
report["capacity_pct"] = pct
|
|
239
|
+
report["capacity_basis"] = basis
|
|
240
|
+
|
|
241
|
+
try:
|
|
242
|
+
routing = _routing(f)
|
|
243
|
+
except Exception:
|
|
244
|
+
routing = {"on": False, "since": None}
|
|
245
|
+
report["routing"] = routing
|
|
246
|
+
routing_on = bool(routing.get("on"))
|
|
247
|
+
|
|
248
|
+
since = _since_install(data, routing_on)
|
|
249
|
+
report["saved_since_install_usd"] = since["saved_usd"]
|
|
250
|
+
report["split"] = {"measured_usd": since["measured_usd"],
|
|
251
|
+
"estimated_usd": since["estimated_usd"],
|
|
252
|
+
"routing_usd": since["routing_usd"]}
|
|
253
|
+
|
|
254
|
+
try:
|
|
255
|
+
data7 = data if capacity_days == 7 else _dashboard(f, 7)
|
|
256
|
+
report["saved_7d_usd"] = _saved_7d(data7, routing_on)
|
|
257
|
+
except Exception:
|
|
258
|
+
pass
|
|
259
|
+
try:
|
|
260
|
+
report["setup_trims_applied"] = _trims(f, data)
|
|
261
|
+
except Exception:
|
|
262
|
+
report["setup_trims_applied"] = None
|
|
263
|
+
|
|
264
|
+
try:
|
|
265
|
+
report["nudges"] = _nudges(f)
|
|
266
|
+
except Exception:
|
|
267
|
+
pass
|
|
268
|
+
return report
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def render(report):
|
|
272
|
+
"""Plain-text lines for Slack. Always under 12 lines, never a path."""
|
|
273
|
+
report = report if isinstance(report, dict) else {}
|
|
274
|
+
version = report.get("version") or "unknown"
|
|
275
|
+
install = report.get("install_date") or "date not recorded"
|
|
276
|
+
lines = ["Token Optimizer %s, installed %s" % (version, install)]
|
|
277
|
+
|
|
278
|
+
pct = report.get("capacity_pct")
|
|
279
|
+
if pct is None:
|
|
280
|
+
lines.append("Capacity since install: not measured yet")
|
|
281
|
+
else:
|
|
282
|
+
lines.append("Capacity since install: %+d%% more work per token vs %s"
|
|
283
|
+
% (pct, report.get("capacity_basis") or "your baseline period"))
|
|
284
|
+
|
|
285
|
+
saved = _money(report.get("saved_since_install_usd"))
|
|
286
|
+
lines.append("Saved since install: %s (all-in, API-price equivalent)"
|
|
287
|
+
% (saved if saved is not None else "not available yet"))
|
|
288
|
+
saved7 = _money(report.get("saved_7d_usd"))
|
|
289
|
+
lines.append("Saved in the last 7 days: %s"
|
|
290
|
+
% (saved7 if saved7 is not None else "not available yet"))
|
|
291
|
+
|
|
292
|
+
split = report.get("split") or {}
|
|
293
|
+
parts = [_money(split.get(k)) for k in ("measured_usd", "estimated_usd", "routing_usd")]
|
|
294
|
+
if any(p is not None for p in parts):
|
|
295
|
+
lines.append("Since install split - measured %s / estimated %s / routing %s"
|
|
296
|
+
% tuple(p if p is not None else "$0.00" for p in parts))
|
|
297
|
+
else:
|
|
298
|
+
lines.append("Since install split: not available yet")
|
|
299
|
+
|
|
300
|
+
routing = report.get("routing") or {}
|
|
301
|
+
if routing.get("failure"):
|
|
302
|
+
lines.append("Routing guidance: failed (%s)" % routing["failure"])
|
|
303
|
+
elif routing.get("on"):
|
|
304
|
+
since = routing.get("since")
|
|
305
|
+
lines.append("Routing guidance: on%s" % (" since " + since if since else ""))
|
|
306
|
+
else:
|
|
307
|
+
lines.append("Routing guidance: off")
|
|
308
|
+
|
|
309
|
+
trims = report.get("setup_trims_applied")
|
|
310
|
+
if trims:
|
|
311
|
+
lines.append("Setup trims: %d applied" % trims)
|
|
312
|
+
elif trims == 0:
|
|
313
|
+
lines.append("Setup trims: none yet (run /token-optimizer)")
|
|
314
|
+
else:
|
|
315
|
+
lines.append("Setup trims: not counted yet")
|
|
316
|
+
|
|
317
|
+
nudges = report.get("nudges") or {}
|
|
318
|
+
followed, total = nudges.get("followed"), nudges.get("total")
|
|
319
|
+
if followed is None or total is None:
|
|
320
|
+
lines.append("Nudges: follow-up not resolved yet")
|
|
321
|
+
else:
|
|
322
|
+
lines.append("Nudges: followed %d of %d this week" % (followed, total))
|
|
323
|
+
return lines[:12]
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def main(argv=None, facade=None):
|
|
327
|
+
"""CLI entry: `pilot-report [--json]`. Returns a process exit code."""
|
|
328
|
+
args = list(sys.argv[1:] if argv is None else argv)
|
|
329
|
+
as_json = False
|
|
330
|
+
for arg in args:
|
|
331
|
+
if arg == "--json":
|
|
332
|
+
as_json = True
|
|
333
|
+
elif arg in ("-h", "--help"):
|
|
334
|
+
print("Usage: python3 measure.py pilot-report [--json]")
|
|
335
|
+
print(" Weekly pilot report for Slack. --json prints the same data as JSON.")
|
|
336
|
+
return 0
|
|
337
|
+
else:
|
|
338
|
+
print("[Error] Unknown pilot-report option: %s" % arg)
|
|
339
|
+
return 2
|
|
340
|
+
try:
|
|
341
|
+
report = collect(facade)
|
|
342
|
+
except Exception:
|
|
343
|
+
report = {"error": "pilot report unavailable"}
|
|
344
|
+
if as_json:
|
|
345
|
+
print(json.dumps(report, indent=2, sort_keys=True, default=str))
|
|
346
|
+
else:
|
|
347
|
+
for line in render(report):
|
|
348
|
+
print(line)
|
|
349
|
+
return 0
|