@alexgreensh/token-optimizer 5.13.50 → 5.13.51

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.50",
16
+ "version": "5.13.51",
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.50",
58
+ "version": "5.13.51",
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.50",
10
+ "version": "5.13.51",
11
11
  "license": "PolyForm-Noncommercial-1.0.0",
12
12
  "keywords": [
13
13
  "token",
package/hooks/hooks.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "hooks": [
10
10
  {
11
11
  "type": "command",
12
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/read_cache.py --quiet; done; exit 0",
12
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/read_cache.py --quiet; done; exit 0",
13
13
  "timeout": 10
14
14
  }
15
15
  ]
@@ -19,7 +19,7 @@
19
19
  "hooks": [
20
20
  {
21
21
  "type": "command",
22
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/bash_hook.py --quiet; done; exit 0",
22
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/bash_hook.py --quiet; done; exit 0",
23
23
  "timeout": 10
24
24
  }
25
25
  ]
@@ -29,7 +29,7 @@
29
29
  "hooks": [
30
30
  {
31
31
  "type": "command",
32
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/measure.py checkpoint-trigger --milestone pre-fanout --quiet; done; exit 0",
32
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/measure.py checkpoint-trigger --milestone pre-fanout --quiet; done; exit 0",
33
33
  "async": true,
34
34
  "timeout": 20
35
35
  }
@@ -40,7 +40,7 @@
40
40
  "hooks": [
41
41
  {
42
42
  "type": "command",
43
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/refetch_guard.py --quiet; done; exit 0",
43
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/refetch_guard.py --quiet; done; exit 0",
44
44
  "timeout": 10
45
45
  }
46
46
  ]
@@ -51,7 +51,7 @@
51
51
  "hooks": [
52
52
  {
53
53
  "type": "command",
54
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/measure.py dynamic-compact-instructions --quiet; done; exit 0",
54
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/measure.py dynamic-compact-instructions --quiet; done; exit 0",
55
55
  "timeout": 20
56
56
  }
57
57
  ]
@@ -60,7 +60,7 @@
60
60
  "hooks": [
61
61
  {
62
62
  "type": "command",
63
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/measure.py compact-capture --trigger auto --quiet; done; exit 0",
63
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/measure.py compact-capture --trigger auto --quiet; done; exit 0",
64
64
  "async": true,
65
65
  "timeout": 30
66
66
  }
@@ -70,7 +70,7 @@
70
70
  "hooks": [
71
71
  {
72
72
  "type": "command",
73
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/read_cache.py --clear --quiet; done; exit 0",
73
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/read_cache.py --clear --quiet; done; exit 0",
74
74
  "timeout": 10
75
75
  }
76
76
  ]
@@ -81,7 +81,7 @@
81
81
  "hooks": [
82
82
  {
83
83
  "type": "command",
84
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" hooks/sessionstart_runner.py; done; exit 0",
84
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" hooks/sessionstart_runner.py; done; exit 0",
85
85
  "timeout": 20
86
86
  }
87
87
  ]
@@ -92,7 +92,7 @@
92
92
  "hooks": [
93
93
  {
94
94
  "type": "command",
95
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" hooks/stop_runner.py; done; exit 0",
95
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" hooks/stop_runner.py; done; exit 0",
96
96
  "timeout": 15
97
97
  }
98
98
  ]
@@ -103,7 +103,7 @@
103
103
  "hooks": [
104
104
  {
105
105
  "type": "command",
106
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" hooks/stop_runner.py; done; exit 0",
106
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" hooks/stop_runner.py; done; exit 0",
107
107
  "async": true,
108
108
  "timeout": 60
109
109
  }
@@ -115,7 +115,7 @@
115
115
  "hooks": [
116
116
  {
117
117
  "type": "command",
118
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/measure.py compact-capture --trigger stop-failure --quiet; done; exit 0",
118
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/measure.py compact-capture --trigger stop-failure --quiet; done; exit 0",
119
119
  "timeout": 12
120
120
  }
121
121
  ]
@@ -126,7 +126,7 @@
126
126
  "hooks": [
127
127
  {
128
128
  "type": "command",
129
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" hooks/userpromptsubmit_runner.py; done; exit 0",
129
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" hooks/userpromptsubmit_runner.py; done; exit 0",
130
130
  "timeout": 20
131
131
  }
132
132
  ]
@@ -138,7 +138,7 @@
138
138
  "hooks": [
139
139
  {
140
140
  "type": "command",
141
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" hooks/posttooluse_runner.py; done; exit 0",
141
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" hooks/posttooluse_runner.py; done; exit 0",
142
142
  "timeout": 10
143
143
  }
144
144
  ]
@@ -150,7 +150,7 @@
150
150
  "hooks": [
151
151
  {
152
152
  "type": "command",
153
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" hooks/posttooluse_runner.py; done; exit 0",
153
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" hooks/posttooluse_runner.py; done; exit 0",
154
154
  "timeout": 10
155
155
  }
156
156
  ]
@@ -161,7 +161,7 @@
161
161
  "hooks": [
162
162
  {
163
163
  "type": "command",
164
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/measure.py quality-cache --force --quiet; done; exit 0",
164
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/measure.py quality-cache --force --quiet; done; exit 0",
165
165
  "timeout": 20
166
166
  }
167
167
  ]
@@ -172,7 +172,7 @@
172
172
  "hooks": [
173
173
  {
174
174
  "type": "command",
175
- "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; L=\"${CLAUDE_PLUGIN_ROOT}/hooks/python-launcher.sh\"; [ -r \"$L\" ] || exit 0; exec \"$b\" \"$L\" \"${CLAUDE_PLUGIN_ROOT}/hooks/run.py\" skills/token-optimizer/scripts/read_cache.py --clear --quiet; done; exit 0",
175
+ "command": "for b in bash /bin/bash /usr/bin/bash /usr/local/bin/bash /opt/homebrew/bin/bash; do command -v \"$b\" >/dev/null 2>&1 || continue; D=\"${CLAUDE_PLUGIN_ROOT}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || D=\"$(ls -d \"${D%/*}\"/*/ 2>/dev/null | grep -E \"/[0-9]+[.][0-9]+[.][0-9]+/$\" | sort -V | tail -n1)\"; D=\"${D%/}\"; [ -r \"$D/hooks/python-launcher.sh\" ] || exit 0; exec \"$b\" \"$D/hooks/python-launcher.sh\" \"$D/hooks/run.py\" skills/token-optimizer/scripts/read_cache.py --clear --quiet; done; exit 0",
176
176
  "timeout": 10
177
177
  }
178
178
  ]
@@ -4,10 +4,21 @@ its __pycache__ bytecode across invocations.
4
4
 
5
5
  Script-mode execution (`python foo.py`) never checks or writes __pycache__ for
6
6
  the file being run as __main__ -- only for things it imports. Since every hook
7
- call is a fresh process, running measure.py (35k+ lines) as a script recompiles
8
- it from source every single time: ~0.3s of pure CPython parse/compile on top of
9
- whatever the hook actually does. Module-mode goes through the import system,
10
- which does check/write __pycache__, cutting that to ~0.1s after the first call.
7
+ call is a fresh process, running measure.py as a script would recompile it from
8
+ source every single time. Module-mode goes through the import system, which
9
+ checks and writes __pycache__, so only the first call pays the compile.
10
+
11
+ That first compile is not small. measure.py is ~2.7 MB / 58k lines (5.13.51).
12
+ Compiling it takes ~1.7 s on an Apple-silicon Mac and ~4 s under WSL2 Linux on
13
+ a 2015 i7, but 20-28 s with Windows CPython 3.12/3.13 on that same i7 (#264;
14
+ compile time grows faster than linearly with file size there). That is longer
15
+ than the host's hook timeouts (Stop 15 s, UserPromptSubmit/SessionStart 20 s),
16
+ and Python writes the .pyc only once the compile finishes, so a hook killed
17
+ mid-compile never warms the cache and every later call starts cold again.
18
+ _measure_bytecode_ready() therefore moves the cold compile into ONE detached
19
+ background process and lets the hook wait for it only as long as the host
20
+ deadline allows; a warm cache costs one stat and a 16-byte read. The
21
+ structural fix is splitting measure.py so each hook compiles only what it uses.
11
22
 
12
23
  sys.path is stripped of '' and '.' (the cwd-equivalent entries the interpreter
13
24
  would otherwise add) before inserting scripts_dir explicitly, so a same-named
@@ -75,6 +86,267 @@ def _warn_readonly_scripts_dir_once(scripts_dir: str) -> None:
75
86
  pass
76
87
 
77
88
 
89
+ # --- Cold-compile gate (#264) ------------------------------------------------
90
+ # Entry points that import measure.py before doing anything, and how long each
91
+ # may wait for a background compile of it. Each wait leaves 8 s of the host's
92
+ # hook timeout (4.5 s for the Codex subagent bridge, whose own budget is 2.5 s)
93
+ # for interpreter start-up, loading the fresh .pyc and the hook's own work
94
+ # (1.0-1.5 s warm on the #264 machine):
95
+ # UserPromptSubmit / SessionStart runners: host 20 s (Claude Code and Codex)
96
+ # Stop runner: host 15 s (SessionEnd runs it async with 60 s)
97
+ # measure.py subcommands: StopFailure is the tightest at 12 s
98
+ # codex_hook_bridge: Codex SubagentStart/Stop, host 6 s
99
+ # A compile that finishes inside the wait gives exactly the old behaviour (the
100
+ # hook runs, warm). One that does not makes the hook exit 0 with no output, the
101
+ # host's "nothing to add", instead of being killed with the cache still cold.
102
+ _COLD_COMPILE_WAIT_S = {
103
+ "userpromptsubmit_runner": 12.0,
104
+ "sessionstart_runner": 12.0,
105
+ "stop_runner": 7.0,
106
+ "measure": 4.0,
107
+ "codex_hook_bridge": 1.5,
108
+ }
109
+ # Seconds; overrides every wait above. A negative value turns the gate off
110
+ # (compile inline, the pre-5.13.51 behaviour).
111
+ _COLD_COMPILE_WAIT_ENV = "TOKEN_OPTIMIZER_COLD_COMPILE_WAIT_S"
112
+ # A lock older than this belongs to a compile that died (killed, machine
113
+ # slept): the next hook takes over. ~4x the slowest compile measured (28 s).
114
+ _COLD_COMPILE_LOCK_STALE_S = 120.0
115
+ _COLD_COMPILE_LOCK = ".token-optimizer-compile.lock"
116
+ _COLD_COMPILE_FAILED = ".token-optimizer-compile.failed"
117
+ _COLD_COMPILE_TAG = "token-optimizer-bytecode-warmup"
118
+
119
+ # The detached compiler, run as `python -c`. It compiles measure.py first and
120
+ # drops the lock as soon as that one is written (the hooks only wait for it),
121
+ # then warms the consolidated runners and the plugin's other scripts. A
122
+ # failure on measure.py leaves a marker keyed to the source's mtime and size,
123
+ # so hooks stop deferring to a compile that cannot succeed and compile inline
124
+ # as before.
125
+ _COMPILER_SOURCE = r'''
126
+ import sys
127
+ sys.path[:] = [p for p in sys.path if p not in ("", ".")]
128
+ sys.dont_write_bytecode = False
129
+ import os, importlib.util, py_compile
130
+ _tag, lock, failed, prefix, opt, measure_src, hooks_dir, scripts_dir = sys.argv[1:9]
131
+ sys.pycache_prefix = prefix or None
132
+ opt = int(opt)
133
+ mode = py_compile.PycInvalidationMode.TIMESTAMP
134
+
135
+ def _fresh(src, cfile):
136
+ try:
137
+ st = os.stat(src)
138
+ with open(cfile, "rb") as fh:
139
+ head = fh.read(16)
140
+ except OSError:
141
+ return False
142
+ if len(head) < 16 or head[:4] != importlib.util.MAGIC_NUMBER:
143
+ return False
144
+ if int.from_bytes(head[4:8], "little") & 1:
145
+ return True
146
+ return (int.from_bytes(head[8:12], "little") == int(st.st_mtime) & 0xFFFFFFFF
147
+ and int.from_bytes(head[12:16], "little") == st.st_size & 0xFFFFFFFF)
148
+
149
+ def _compile(src):
150
+ cfile = importlib.util.cache_from_source(src, optimization="" if opt == 0 else opt)
151
+ if not _fresh(src, cfile):
152
+ py_compile.compile(src, cfile=cfile, doraise=True, optimize=opt, invalidation_mode=mode)
153
+
154
+ try:
155
+ _compile(measure_src)
156
+ except BaseException:
157
+ try:
158
+ st = os.stat(measure_src)
159
+ with open(failed, "w", encoding="utf-8") as fh:
160
+ fh.write("%d:%d" % (st.st_mtime_ns, st.st_size))
161
+ except OSError:
162
+ pass
163
+ finally:
164
+ try:
165
+ os.unlink(lock)
166
+ except OSError:
167
+ pass
168
+ # Then the rest a hook imports: the consolidated runners and the scripts.
169
+ for folder, suffix in ((hooks_dir, "_runner.py"), (scripts_dir, ".py")):
170
+ try:
171
+ names = sorted(os.listdir(folder))
172
+ except OSError:
173
+ continue
174
+ for name in names:
175
+ src = os.path.join(folder, name)
176
+ if name.endswith(suffix) and name[:-3].isidentifier() and src != measure_src:
177
+ try:
178
+ _compile(src)
179
+ except BaseException:
180
+ pass
181
+ '''
182
+
183
+
184
+ def _bytecode_path(source: str) -> str:
185
+ """Where the import system reads and writes ``source``'s bytecode for this
186
+ interpreter, honouring sys.pycache_prefix (PYTHONPYCACHEPREFIX, or the
187
+ per-user prefix run.py chooses inside a git work tree or a hosted VM)."""
188
+ import importlib.util
189
+
190
+ return importlib.util.cache_from_source(source)
191
+
192
+
193
+ def _bytecode_fresh(source: str, cache: str) -> bool:
194
+ """True when ``cache`` is bytecode the import system will accept for
195
+ ``source`` without recompiling: one stat and a 16-byte read. The checks
196
+ mirror importlib's: magic number, then the source mtime and size a
197
+ timestamp .pyc records. A hash-based .pyc counts as fresh (import itself
198
+ validates its hash)."""
199
+ import importlib.util
200
+
201
+ try:
202
+ st = os.stat(source)
203
+ with open(cache, "rb") as fh:
204
+ head = fh.read(16)
205
+ except OSError:
206
+ return False
207
+ if len(head) < 16 or head[:4] != importlib.util.MAGIC_NUMBER:
208
+ return False
209
+ if int.from_bytes(head[4:8], "little") & 1:
210
+ return True
211
+ return (int.from_bytes(head[8:12], "little") == int(st.st_mtime) & 0xFFFFFFFF
212
+ and int.from_bytes(head[12:16], "little") == st.st_size & 0xFFFFFFFF)
213
+
214
+
215
+ def _compile_failed_for(marker: str, source: str) -> bool:
216
+ """The background compile already failed on this exact source."""
217
+ try:
218
+ st = os.stat(source)
219
+ with open(marker, encoding="utf-8") as fh:
220
+ return fh.read(64).strip() == f"{st.st_mtime_ns}:{st.st_size}"
221
+ except (OSError, ValueError):
222
+ return False
223
+
224
+
225
+ def _take_compile_lock(lock: str):
226
+ """True: this hook owns the compile. False: another hook's compile is
227
+ running. None: no lock can be made here (read-only cache dir), so the
228
+ caller compiles inline as before. A lock past its stale age is taken over
229
+ once."""
230
+ import time
231
+
232
+ for _attempt in range(2):
233
+ try:
234
+ fd = os.open(lock, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
235
+ except FileExistsError:
236
+ try:
237
+ age = time.time() - os.path.getmtime(lock)
238
+ except OSError:
239
+ continue # released between the two calls: try again
240
+ if age < _COLD_COMPILE_LOCK_STALE_S:
241
+ return False
242
+ try:
243
+ os.unlink(lock)
244
+ except OSError:
245
+ return False
246
+ continue
247
+ except OSError:
248
+ return None
249
+ try:
250
+ os.write(fd, f"{os.getpid()} {time.time():.3f}\n".encode("ascii"))
251
+ except OSError:
252
+ pass
253
+ finally:
254
+ os.close(fd)
255
+ return True
256
+ return False
257
+
258
+
259
+ def _start_background_compile(lock, failed, measure_src, plugin_scripts):
260
+ """Start the one detached compiler. Returns its Popen, or None (the lock is
261
+ then released again and the caller compiles inline)."""
262
+ import subprocess
263
+
264
+ hooks_dir = os.path.dirname(os.path.abspath(__file__))
265
+ argv = [sys.executable, "-c", _COMPILER_SOURCE, _COLD_COMPILE_TAG, lock, failed,
266
+ getattr(sys, "pycache_prefix", None) or "", str(sys.flags.optimize),
267
+ measure_src, hooks_dir, plugin_scripts]
268
+ proc = None
269
+ try:
270
+ from spawn_utils import spawn_detached
271
+
272
+ # stdio to DEVNULL: an inherited stdout would hold the hook's pipe
273
+ # open, and the host waits for EOF on it.
274
+ proc = spawn_detached(argv, cwd=plugin_scripts, stdin=subprocess.DEVNULL,
275
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
276
+ except Exception:
277
+ proc = None
278
+ if proc is None:
279
+ try:
280
+ os.unlink(lock)
281
+ except OSError:
282
+ pass
283
+ return proc
284
+
285
+
286
+ def _measure_bytecode_ready(plugin_scripts: str, wait_s: float) -> bool:
287
+ """False only when measure.py's bytecode is still being compiled in the
288
+ background after ``wait_s``: the hook should then exit 0 without output.
289
+
290
+ True means import as usual: the cache is warm, a compile finished inside
291
+ the wait, or the background route is unavailable (no cache_tag, read-only
292
+ cache dir, spawn refused, the compile failed or died), in which case the
293
+ import compiles inline exactly as it did before this gate existed."""
294
+ import time
295
+
296
+ measure_src = os.path.join(plugin_scripts, "measure.py")
297
+ try:
298
+ cache = _bytecode_path(measure_src)
299
+ except Exception:
300
+ return True
301
+ if _bytecode_fresh(measure_src, cache):
302
+ return True
303
+ if not os.path.isfile(measure_src):
304
+ return True
305
+ cache_dir = os.path.dirname(cache)
306
+ lock = os.path.join(cache_dir, _COLD_COMPILE_LOCK)
307
+ failed = os.path.join(cache_dir, _COLD_COMPILE_FAILED)
308
+ try:
309
+ os.makedirs(cache_dir, exist_ok=True)
310
+ except OSError:
311
+ return True
312
+ end = time.monotonic() + max(0.0, wait_s)
313
+ child = None
314
+ while True:
315
+ if _bytecode_fresh(measure_src, cache):
316
+ return True
317
+ if _compile_failed_for(failed, measure_src):
318
+ return True
319
+ if child is not None and child.poll() is not None:
320
+ # Ours exited and the cache is still cold: compile inline.
321
+ return True
322
+ if child is None:
323
+ owned = _take_compile_lock(lock)
324
+ if owned is None:
325
+ return True
326
+ if owned:
327
+ child = _start_background_compile(lock, failed, measure_src, plugin_scripts)
328
+ if child is None:
329
+ return True
330
+ if time.monotonic() >= end:
331
+ return False
332
+ time.sleep(0.05)
333
+
334
+
335
+ def _cold_compile_wait(module_name: str):
336
+ """Seconds this entry may wait for a background compile, or None when the
337
+ entry is not gated (it does not import measure.py up front)."""
338
+ wait = _COLD_COMPILE_WAIT_S.get(module_name)
339
+ if wait is None:
340
+ return None
341
+ raw = os.environ.get(_COLD_COMPILE_WAIT_ENV, "").strip()
342
+ if raw:
343
+ try:
344
+ wait = float(raw)
345
+ except ValueError:
346
+ pass
347
+ return None if wait < 0 else wait
348
+
349
+
78
350
  def main() -> int:
79
351
  if len(sys.argv) < 3:
80
352
  return 0
@@ -96,10 +368,32 @@ def main() -> int:
96
368
 
97
369
  _warn_readonly_scripts_dir_once(scripts_dir)
98
370
 
371
+ # A host-set PYTHONDONTWRITEBYTECODE=1 would stop __pycache__ being written.
372
+ sys.dont_write_bytecode = False
373
+
99
374
  sys.path = [p for p in sys.path if p not in ("", ".")]
100
375
  sys.path.insert(0, scripts_dir)
101
376
  sys.argv = [module_name, *script_args]
102
377
 
378
+ # The plugin's own scripts dir (hook_runtime, spawn_utils, measure.py).
379
+ # The consolidated runners live in hooks/, so scripts_dir is hooks/ for
380
+ # them; APPENDED so it can never shadow a module of the hook being run.
381
+ _plugin_scripts = os.path.join(
382
+ os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
383
+ "skills", "token-optimizer", "scripts")
384
+ if os.path.isdir(_plugin_scripts) and _plugin_scripts not in sys.path:
385
+ sys.path.append(_plugin_scripts)
386
+
387
+ # Before anything imports measure.py: never compile it on the hook's
388
+ # deadline (#264). See _measure_bytecode_ready.
389
+ _wait = _cold_compile_wait(module_name)
390
+ if _wait is not None:
391
+ try:
392
+ if not _measure_bytecode_ready(_plugin_scripts, _wait):
393
+ return 0
394
+ except Exception:
395
+ pass
396
+
103
397
  # Per-event entry budget first (PreToolUse / PostToolUse / Stop). These are
104
398
  # self-imposed deadlines that land far under the host's hooks.json timeout,
105
399
  # so an over-budget hook exits 0 with NO output instead of being killed
@@ -126,6 +420,11 @@ def main() -> int:
126
420
  # already self-bounds at 60s, so 110s is a last-resort backstop for any
127
421
  # OTHER hook that runs past run.py's wait while orphaned on Windows.
128
422
  # Fail-open if hook_runtime is missing (unit-test dummy plugin trees).
423
+ # The consolidated runners live in hooks/, so scripts_dir is hooks/ and
424
+ # hook_runtime (in skills/token-optimizer/scripts) was never importable for
425
+ # them: the import failed, the except swallowed it, and every runner ran
426
+ # with no backstop at all. It is found through the plugin's own scripts
427
+ # dir, appended to sys.path above.
129
428
  deadline = None
130
429
  try:
131
430
  from hook_runtime import HookDeadline, arm_entry_budget
package/hooks/run.py CHANGED
@@ -579,7 +579,14 @@ def main() -> int:
579
579
  return 0
580
580
 
581
581
  plugin_root = os.environ.get("CLAUDE_PLUGIN_ROOT", "").strip()
582
- if plugin_root:
582
+ # A CLAUDE_PLUGIN_ROOT that no longer exists is the host's stale view of a
583
+ # version dir an upgrade deleted under the running session (Codex keeps the
584
+ # root it loaded at session start). The manifest already found the live
585
+ # sibling version and exec'd ITS run.py, so this file's own location is the
586
+ # live root. Trusting the stale value made every hook exit 0 having done
587
+ # nothing until the session restarted.
588
+ stale_plugin_root = bool(plugin_root) and not os.path.isdir(plugin_root)
589
+ if plugin_root and not stale_plugin_root:
583
590
  root_path = Path(plugin_root)
584
591
  else:
585
592
  # Fallback: relative to this wrapper's parent directory.
@@ -603,9 +610,11 @@ def main() -> int:
603
610
  # directly: CPython never caches __pycache__ bytecode for a script run as
604
611
  # __main__, only for imported modules, so a direct `python script_path`
605
612
  # recompiles the target from source on every single hook invocation. For
606
- # measure.py (35k+ lines) that is ~0.3s of pure parse/compile paid on
607
- # nearly every tool call. module_runner.py runs it as a module instead, so
608
- # the import system's normal bytecode cache applies. See module_runner.py.
613
+ # measure.py (~2.7 MB) that is 1.7 s on a fast Mac and 20+ s on some
614
+ # Windows machines (#264), paid on every call. module_runner.py runs it as
615
+ # a module instead, so the import system's normal bytecode cache applies,
616
+ # and moves the one cold compile off the hook's deadline. See
617
+ # module_runner.py.
609
618
  module_runner = Path(__file__).resolve().parent / "module_runner.py"
610
619
  cmd = [sys.executable, str(module_runner), str(script_path.parent), script_path.stem, *script_args]
611
620
 
@@ -645,6 +654,9 @@ def main() -> int:
645
654
  # never crash a hook with UnicodeDecode/EncodeError. PYTHONUTF8 also makes the
646
655
  # child's default open() encoding UTF-8; PYTHONIOENCODING covers its std streams.
647
656
  child_env = {**os.environ, "PYTHONUTF8": "1", "PYTHONIOENCODING": "utf-8"}
657
+ if stale_plugin_root:
658
+ # The handler and everything it imports resolve paths from this too.
659
+ child_env["CLAUDE_PLUGIN_ROOT"] = str(root_resolved)
648
660
  # The child (and every detached worker it spawns) inherits the bytecode
649
661
  # prefix chosen at the top of main(), so no __pycache__ lands in a repo.
650
662
  _apply_bytecode_prefix(_pycache_prefix, child_env)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexgreensh/token-optimizer",
3
- "version": "5.13.50",
3
+ "version": "5.13.51",
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)",
@@ -19,20 +19,19 @@ Use when running multiple agent systems, spending $2-5/day on agents, suspecting
19
19
  ```bash
20
20
  RUNTIME="${TOKEN_OPTIMIZER_RUNTIME:-}"
21
21
  if [ -z "$RUNTIME" ]; then
22
- if [ -n "$CLAUDE_PLUGIN_ROOT" ] || [ -n "$CLAUDE_PLUGIN_DATA" ]; then
23
- RUNTIME="claude"
24
- elif [ -n "$OPENCODE" ] || [ -n "$OPENCODE_BIN" ] || [ -n "$OPENCODE_CONFIG_DIR" ] || [ -n "$OPENCODE_CONFIG" ]; then
25
- RUNTIME="opencode"
26
- elif [ -n "$CODEX_HOME" ]; then
27
- RUNTIME="codex"
28
- elif [ -n "$CLAUDECODE" ] || [ -n "$CLAUDE_CODE_ENTRYPOINT" ] || [ -n "$CLAUDE_CODE_SESSION_ID" ]; then
29
- RUNTIME="claude"
30
- elif [ -d "$HOME/.config/opencode" ] && [ ! -d "$HOME/.codex" ]; then
31
- RUNTIME="opencode"
32
- elif [ -d "$HOME/.codex" ]; then
33
- RUNTIME="codex"
34
- else
35
- RUNTIME="claude"
22
+ if [ -n "$CLAUDE_PLUGIN_ROOT" ] || [ -n "$CLAUDE_PLUGIN_DATA" ]; then RUNTIME="claude"
23
+ elif [ -n "$OPENCODE" ] || [ -n "$OPENCODE_BIN" ] || [ -n "$OPENCODE_CONFIG_DIR" ] || [ -n "$OPENCODE_CONFIG" ]; then RUNTIME="opencode"
24
+ elif [ -n "$CODEX_HOME" ]; then RUNTIME="codex"
25
+ # Another harness's config-dir env: same tier as Python's detect_runtime().
26
+ elif [ -n "$HERMES_HOME" ]; then RUNTIME="hermes"
27
+ elif [ -n "$COPILOT_HOME" ] || [ -n "$TOKEN_OPTIMIZER_COPILOT_HOME" ]; then RUNTIME="copilot"
28
+ elif [ -n "$TOKEN_OPTIMIZER_CURSOR_HOME" ]; then RUNTIME="cursor"
29
+ elif [ -n "$TOKEN_OPTIMIZER_ANTIGRAVITY_HOME" ]; then RUNTIME="antigravity"
30
+ elif [ -n "$GROK_HOME" ] || [ -n "$TOKEN_OPTIMIZER_GROK_HOME" ]; then RUNTIME="grok"
31
+ elif [ -n "$CLAUDECODE" ] || [ -n "$CLAUDE_CODE_ENTRYPOINT" ] || [ -n "$CLAUDE_CODE_SESSION_ID" ]; then RUNTIME="claude"
32
+ elif [ -d "$HOME/.config/opencode" ] && [ ! -d "$HOME/.codex" ]; then RUNTIME="opencode"
33
+ elif [ -d "$HOME/.codex" ]; then RUNTIME="codex"
34
+ else RUNTIME="claude"
36
35
  fi
37
36
  fi
38
37