@alexgreensh/token-optimizer 5.13.62 → 5.13.63

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.
@@ -13,7 +13,7 @@
13
13
  "name": "token-optimizer",
14
14
  "source": "./",
15
15
  "description": "Audit, fix, and monitor Claude Code context window usage. Find the ghost tokens.",
16
- "version": "5.13.62",
16
+ "version": "5.13.63",
17
17
  "author": {
18
18
  "name": "Alex Greenshpun",
19
19
  "url": "https://linkedin.com/in/alexgreensh"
@@ -55,7 +55,7 @@
55
55
  "name": "token-optimizer-cowork",
56
56
  "source": "./cowork/token-optimizer",
57
57
  "description": "Token Optimizer for Claude Cowork \u2014 full skills + Cowork-native hooks (beta). Install THIS in Cowork; the standard token-optimizer entry is for desktop Claude Code.",
58
- "version": "5.13.62",
58
+ "version": "5.13.63",
59
59
  "author": {
60
60
  "name": "Alex Greenshpun",
61
61
  "url": "https://linkedin.com/in/alexgreensh"
@@ -7,7 +7,7 @@
7
7
  },
8
8
  "homepage": "https://github.com/alexgreensh/token-optimizer",
9
9
  "repository": "https://github.com/alexgreensh/token-optimizer",
10
- "version": "5.13.62",
10
+ "version": "5.13.63",
11
11
  "license": "PolyForm-Noncommercial-1.0.0",
12
12
  "keywords": [
13
13
  "token",
@@ -8,7 +8,7 @@ runpy(measure.py)`` and repeated the interpreter and module startup work:
8
8
  1. ``measure.py ensure-health --once-mark`` (timeout 15, async)
9
9
  2. ``measure.py quality-cache --force --quiet --once-mark`` (timeout 20)
10
10
  3. ``measure.py compact-restore --compact`` [matcher compact] (timeout 20)
11
- 4. ``read_cache.py --clear-compacted --quiet`` [matcher compact] (timeout 10)
11
+ 4. ``read_cache.py --clear-compacted --quiet`` [source compact|clear] (timeout 10)
12
12
  5. ``measure.py compact-restore --new-session-only --once-mark`` (timeout 20)
13
13
 
14
14
  This runner is invoked ONCE per session start, imports ``measure.py`` ONCE, and
@@ -197,6 +197,20 @@ def _is_compact_start(hook_input: dict) -> bool:
197
197
  return source == "compact"
198
198
 
199
199
 
200
+ def _is_context_reset_start(hook_input: dict) -> bool:
201
+ """True when this SessionStart follows a /clear or a compaction.
202
+
203
+ Both empty the model's context while the session id lives on, so the
204
+ "already in context" records must be dropped. Only ``compact`` also restores
205
+ state (``_is_compact_start``); ``clear`` is a deliberate fresh start and
206
+ restores nothing.
207
+ """
208
+ if _is_compact_start(hook_input):
209
+ return True
210
+ source = str(hook_input.get("source") or "").strip().lower()
211
+ return source == "clear"
212
+
213
+
200
214
  def _run_safely(name: str, fn, *args, **kwargs) -> None:
201
215
  """Run fn, swallow any failure to the diagnostics log, never propagate.
202
216
 
@@ -438,11 +452,11 @@ def _sub_compact_restore_compact(hook_input: dict, sink: list) -> None:
438
452
 
439
453
 
440
454
  def _sub_clear_compacted(hook_input: dict) -> None:
441
- """``read_cache.py --clear-compacted --quiet`` (entry 4, matcher ``compact``).
455
+ """``read_cache.py --clear-compacted --quiet`` (entry 4, source ``compact`` or ``clear``).
442
456
 
443
457
  Mirrors read_cache.main()'s --clear-compacted branch: bail loudly (stderr)
444
458
  when the stdin payload is empty, else call handle_clear_compacted(payload,
445
- quiet=True). read_cache is imported lazily so a non-compact SessionStart --
459
+ quiet=True). read_cache is imported lazily so a plain startup or resume --
446
460
  the overwhelming majority -- never pays for the import.
447
461
 
448
462
  Emits nothing on stdout by design (all its output, success and failure, goes
@@ -546,6 +560,7 @@ def _check_consent() -> bool:
546
560
  def main() -> int:
547
561
  hook_input = _read_hook_input()
548
562
  is_compact = _is_compact_start(hook_input)
563
+ is_context_reset = _is_context_reset_start(hook_input)
549
564
 
550
565
  # Consent gate. Pre-consolidation, run.py's exempt_commands check matched
551
566
  # the literal "ensure-health" arg on hooks.json entry 1, so THAT entry ran
@@ -600,7 +615,7 @@ def main() -> int:
600
615
  # matcher:"compact" subcommands only count on a compact start, so a normal
601
616
  # start gives its three subcommands a third of the budget each instead of a
602
617
  # fifth. Dispatch order matches the hooks.json entry order exactly.
603
- pending = 5 if is_compact else 3
618
+ pending = 3 + (1 if is_context_reset else 0) + (1 if is_compact else 0)
604
619
  _runner_budget(8, subcommand_count_hint=pending)
605
620
 
606
621
  _capture("ensure-health", _sub_ensure_health, hook_input)
@@ -608,6 +623,7 @@ def main() -> int:
608
623
  if is_compact:
609
624
  _capture("compact-restore --compact", _sub_compact_restore_compact,
610
625
  hook_input, _wrapped)
626
+ if is_context_reset:
611
627
  _capture("read_cache --clear-compacted", _sub_clear_compacted, hook_input)
612
628
  _capture("compact-restore --new-session-only", _sub_compact_restore_new_session,
613
629
  hook_input, _wrapped)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexgreensh/token-optimizer",
3
- "version": "5.13.62",
3
+ "version": "5.13.63",
4
4
  "description": "Token Optimizer for Claude Code cloud sessions: the real plugin, installed by your cloud environment's setup script.",
5
5
  "license": "PolyForm-Noncommercial-1.0.0",
6
6
  "author": "Alex Greenshpun (https://linkedin.com/in/alexgreensh)",
@@ -979,7 +979,10 @@ class ClaudeCodeAdapter(BaseAdapter):
979
979
  )
980
980
  cc = usage.get("cache_creation_input_tokens", 0) or (cc_1h + cc_5m)
981
981
  model_id = msg.get("model", "unknown")
982
- req_id = record.get("requestId")
982
+ # SDK and imported transcripts carry only the message id, which
983
+ # repeats across the streamed chunks of one reply the same way.
984
+ req_id = record.get("requestId") or (
985
+ msg.get("id") if isinstance(msg, dict) else None)
983
986
  current = {
984
987
  "input": inp_tok,
985
988
  "output": out_tok,
@@ -58,6 +58,27 @@ _KNOWN_PROVIDER_PREFIXES = {
58
58
  # Model normalization
59
59
  # ---------------------------------------------------------------------------
60
60
 
61
+ _GEMINI_ALIASES = (
62
+ "gemini-3.1-pro-preview",
63
+ "gemini-3.1-flash-lite",
64
+ "gemini-3.5-flash",
65
+ "gemini-3.1-pro",
66
+ "gemini-3-flash",
67
+ "gemini-3-pro",
68
+ "gemini-2.5-flash-lite",
69
+ "gemini-2.5-flash",
70
+ "gemini-2.5-pro",
71
+ )
72
+
73
+ # A dated or numbered snapshot of a priced id: -2026-01-01, -20260101, -0613,
74
+ # -001 or @20260101. Any other suffix names a different model.
75
+ _SNAPSHOT_RE = re.compile(r"(?:-\d{4}-\d{2}-\d{2}|-\d{8}|-\d{3,4}|@\d{8})")
76
+
77
+
78
+ def _is_card_or_snapshot(model: str, alias: str) -> bool:
79
+ return model == alias or (model.startswith(alias) and _SNAPSHOT_RE.fullmatch(model, len(alias)) is not None)
80
+
81
+
61
82
  def normalize_model_name(model_id: str) -> str | None:
62
83
  """Collapse model IDs like 'claude-sonnet-4-6' into 'sonnet'.
63
84
 
@@ -109,23 +130,19 @@ def normalize_model_name(model_id: str) -> str | None:
109
130
  "o4-mini",
110
131
  "o3",
111
132
  ):
112
- if m == alias or m.startswith(alias + "-"):
133
+ if _is_card_or_snapshot(m, alias):
113
134
  return alias
114
- if m == "gpt-5.6" or m.startswith("gpt-5.6-"):
135
+ if m in ("gpt-5.6", "gpt-5.6-sol-pro") or _is_card_or_snapshot(m, "gpt-5.6"):
115
136
  return "gpt-5.6-sol"
116
- for alias in (
117
- "gemini-3.1-pro-preview",
118
- "gemini-3.1-flash-lite",
119
- "gemini-3.5-flash",
120
- "gemini-3.1-pro",
121
- "gemini-3-flash",
122
- "gemini-3-pro",
123
- "gemini-2.5-flash-lite",
124
- "gemini-2.5-flash",
125
- "gemini-2.5-pro",
126
- ):
127
- if m == alias or m.startswith(alias + "-"):
137
+ for alias in _GEMINI_ALIASES:
138
+ if _is_card_or_snapshot(m, alias):
128
139
  return alias
140
+ # A preview build of a priced Gemini model is the same model.
141
+ base = re.sub(r"-preview(?:-\d{2}-\d{2,4})?$", "", m)
142
+ if base != m and base.startswith("gemini-"):
143
+ for alias in _GEMINI_ALIASES:
144
+ if _is_card_or_snapshot(base, alias):
145
+ return alias
129
146
  return m
130
147
 
131
148