@alexgreensh/token-optimizer 5.13.54 → 5.13.56

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.54",
16
+ "version": "5.13.56",
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.54",
58
+ "version": "5.13.56",
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.54",
10
+ "version": "5.13.56",
11
11
  "license": "PolyForm-Noncommercial-1.0.0",
12
12
  "keywords": [
13
13
  "token",
@@ -106,6 +106,23 @@ _COLD_COMPILE_WAIT_S = {
106
106
  "measure": 4.0,
107
107
  "codex_hook_bridge": 1.5,
108
108
  }
109
+ # The PreCompact save (measure.py compact-capture --trigger auto) is the one
110
+ # measure.py hook whose skip loses data: no save means the restore after the
111
+ # compaction falls back to an older checkpoint. Its host timeout is 30 s and it
112
+ # runs async, so it can wait far longer than the 4 s the tightest measure.py
113
+ # hook allows. 12 s + its own 15 s budget (hook_runtime.BUDGET_PRECOMPACT_CAPTURE)
114
+ # + start-up stays under the 30 s.
115
+ _COLD_COMPILE_WAIT_PRECOMPACT_SAVE_S = 12.0
116
+
117
+
118
+ def _is_precompact_save(module_name: str, script_args) -> bool:
119
+ args = list(script_args or ())
120
+ if module_name != "measure" or not args or args[0] != "compact-capture":
121
+ return False
122
+ try:
123
+ return args[args.index("--trigger") + 1] == "auto"
124
+ except (ValueError, IndexError):
125
+ return False
109
126
  # Seconds; overrides every wait above. A negative value turns the gate off
110
127
  # (compile inline, the pre-5.13.51 behaviour).
111
128
  _COLD_COMPILE_WAIT_ENV = "TOKEN_OPTIMIZER_COLD_COMPILE_WAIT_S"
@@ -332,10 +349,12 @@ def _measure_bytecode_ready(plugin_scripts: str, wait_s: float) -> bool:
332
349
  time.sleep(0.05)
333
350
 
334
351
 
335
- def _cold_compile_wait(module_name: str):
352
+ def _cold_compile_wait(module_name: str, script_args=()):
336
353
  """Seconds this entry may wait for a background compile, or None when the
337
354
  entry is not gated (it does not import measure.py up front)."""
338
355
  wait = _COLD_COMPILE_WAIT_S.get(module_name)
356
+ if _is_precompact_save(module_name, script_args):
357
+ wait = _COLD_COMPILE_WAIT_PRECOMPACT_SAVE_S
339
358
  if wait is None:
340
359
  return None
341
360
  raw = os.environ.get(_COLD_COMPILE_WAIT_ENV, "").strip()
@@ -386,7 +405,7 @@ def main() -> int:
386
405
 
387
406
  # Before anything imports measure.py: never compile it on the hook's
388
407
  # deadline (#264). See _measure_bytecode_ready.
389
- _wait = _cold_compile_wait(module_name)
408
+ _wait = _cold_compile_wait(module_name, script_args)
390
409
  if _wait is not None:
391
410
  try:
392
411
  if not _measure_bytecode_ready(_plugin_scripts, _wait):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexgreensh/token-optimizer",
3
- "version": "5.13.54",
3
+ "version": "5.13.56",
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)",
@@ -35046,7 +35046,7 @@ def _remove_our_settings_entries(settings: dict) -> list[str]:
35046
35046
  sl = settings.get("statusLine")
35047
35047
  if isinstance(sl, dict):
35048
35048
  cmd = sl.get("command", "")
35049
- if isinstance(cmd, str) and "statusline.js" in cmd and "token-optimizer" in cmd:
35049
+ if _statusline_cmd_is_ours(cmd):
35050
35050
  del settings["statusLine"]
35051
35051
  removed.append("statusLine (token-optimizer)")
35052
35052
  # Hook entries we own, across every event.
@@ -46833,6 +46833,28 @@ def _statusline_js_in_command(cmd):
46833
46833
  return m.start(3), m.end(3), m.group(3)
46834
46834
 
46835
46835
 
46836
+ def _statusline_cmd_is_ours(cmd):
46837
+ """True when a statusLine command runs Token Optimizer's statusline.js.
46838
+
46839
+ Judged by the folder the script sits in (``token-optimizer/`` or
46840
+ ``token-optimizer/scripts/``), not by "token-optimizer" appearing anywhere
46841
+ in the command: another tool's statusline.js under a home or project folder
46842
+ that happens to contain that name is not ours. A command whose script path
46843
+ cannot be parsed falls back to the plain substring test.
46844
+ """
46845
+ if not isinstance(cmd, str) or "statusline.js" not in cmd or "token-optimizer" not in cmd:
46846
+ return False
46847
+ found = _statusline_js_in_command(cmd)
46848
+ if not found:
46849
+ return True
46850
+ parts = [p for p in re.split(r"[\\/]", found[2].lower()) if p]
46851
+ if len(parts) < 2 or parts[-1] != "statusline.js":
46852
+ return False
46853
+ if parts[-2] == "token-optimizer":
46854
+ return True
46855
+ return len(parts) >= 3 and parts[-2] == "scripts" and parts[-3] == "token-optimizer"
46856
+
46857
+
46836
46858
  def _path_is_under(path, base):
46837
46859
  try:
46838
46860
  Path(path).expanduser().resolve(strict=False).relative_to(Path(base).resolve(strict=False))
@@ -46870,7 +46892,7 @@ def _heal_dangling_statusline():
46870
46892
  if not isinstance(sl, dict):
46871
46893
  return False
46872
46894
  cmd = sl.get("command") or ""
46873
- if not isinstance(cmd, str) or "statusline.js" not in cmd or "token-optimizer" not in cmd:
46895
+ if not _statusline_cmd_is_ours(cmd):
46874
46896
  return False
46875
46897
  found = _statusline_js_in_command(cmd)
46876
46898
  if not found:
@@ -47162,7 +47184,7 @@ def _is_quality_bar_installed(settings=None):
47162
47184
  # Check statusline
47163
47185
  sl = (settings.get("statusLine") or {})
47164
47186
  cmd = sl.get("command", "")
47165
- if "statusline.js" in cmd and "token-optimizer" in cmd:
47187
+ if _statusline_cmd_is_ours(cmd):
47166
47188
  result["statusline"] = True
47167
47189
 
47168
47190
  # Check UserPromptSubmit hook (settings.json). Recognizes both the legacy
@@ -47971,7 +47993,7 @@ def setup_quality_bar(dry_run=False, uninstall=False, status_only=False, force=F
47971
47993
 
47972
47994
  # Remove statusLine if it's ours
47973
47995
  sl = settings.get("statusLine", {})
47974
- if "statusline.js" in sl.get("command", "") and "token-optimizer" in sl.get("command", ""):
47996
+ if _statusline_cmd_is_ours(sl.get("command", "")):
47975
47997
  del settings["statusLine"]
47976
47998
  removed += 1
47977
47999
 
@@ -56436,7 +56458,7 @@ def run_ensure_health():
56436
56458
  # make setup_quality_bar skip silently; never claim an
56437
56459
  # install we didn't deliver.
56438
56460
  _eh_sl = (_read_settings_json()[0].get("statusLine") or {}).get("command", "") or ""
56439
- if "statusline.js" in _eh_sl and "token-optimizer" in _eh_sl:
56461
+ if _statusline_cmd_is_ours(_eh_sl):
56440
56462
  print(
56441
56463
  " [Token Optimizer] Quality statusline enabled "
56442
56464
  "(context % + quality score). "
@@ -56445,7 +56467,7 @@ def run_ensure_health():
56445
56467
  )
56446
56468
  else:
56447
56469
  statusline_cmd = (settings.get("statusLine") or {}).get("command", "") or ""
56448
- statusline_is_ours = "statusline.js" in statusline_cmd and "token-optimizer" in statusline_cmd
56470
+ statusline_is_ours = _statusline_cmd_is_ours(statusline_cmd)
56449
56471
  hooks = settings.get("hooks", {}).get("UserPromptSubmit", [])
56450
56472
  # Recognize the consolidated dispatcher as well as
56451
56473
  # the legacy standalone hook, else this re-adds the