@alexgreensh/token-optimizer 5.13.56 → 5.13.59

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.
Files changed (30) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/hooks/hooks.json +16 -16
  4. package/hooks/python-launcher.sh +10 -0
  5. package/hooks/stop_runner.py +27 -4
  6. package/hooks/windows-launcher.py +57 -3
  7. package/package.json +1 -1
  8. package/skills/fleet-auditor/scripts/shared.py +29 -1
  9. package/skills/token-optimizer/pricing/prices.json +13 -1
  10. package/skills/token-optimizer/scripts/codex_compact_prompt.py +93 -9
  11. package/skills/token-optimizer/scripts/codex_doctor.py +134 -16
  12. package/skills/token-optimizer/scripts/codex_install.py +436 -26
  13. package/skills/token-optimizer/scripts/codex_session.py +38 -8
  14. package/skills/token-optimizer/scripts/copilot_doctor.py +129 -6
  15. package/skills/token-optimizer/scripts/copilot_hook_bridge.py +62 -14
  16. package/skills/token-optimizer/scripts/copilot_install.py +56 -4
  17. package/skills/token-optimizer/scripts/detectors/cache_instability.py +18 -4
  18. package/skills/token-optimizer/scripts/deterministic_candidates.py +55 -15
  19. package/skills/token-optimizer/scripts/devin_source.py +1 -1
  20. package/skills/token-optimizer/scripts/hermes_doctor.py +177 -1
  21. package/skills/token-optimizer/scripts/hermes_hook_bridge.py +190 -0
  22. package/skills/token-optimizer/scripts/hermes_install.py +43 -0
  23. package/skills/token-optimizer/scripts/hermes_session.py +133 -0
  24. package/skills/token-optimizer/scripts/hermes_state.py +58 -0
  25. package/skills/token-optimizer/scripts/hermes_tool_hooks.py +291 -0
  26. package/skills/token-optimizer/scripts/measure.py +1442 -325
  27. package/skills/token-optimizer/scripts/plugin_env.py +18 -0
  28. package/skills/token-optimizer/scripts/recovery_output.py +15 -2
  29. package/skills/token-optimizer/scripts/refetch_fingerprint.py +36 -1
  30. package/skills/token-optimizer/scripts/runtime_env.py +5 -1
@@ -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.56",
16
+ "version": "5.13.59",
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.56",
58
+ "version": "5.13.59",
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.56",
10
+ "version": "5.13.59",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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; 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",
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
  ]
@@ -187,6 +187,16 @@ _is_safe_prefix() {
187
187
  # managers are enumerated here. (mise pattern, trekie86.)
188
188
  /[a-zA-Z]/Users/*/AppData/Local/mise/shims/*) return 0 ;;
189
189
  /[a-zA-Z]/Users/*/.pyenv/pyenv-win/shims/*) return 0 ;;
190
+ # Default per-user installs of conda, Scoop and uv-managed Pythons,
191
+ # and the all-users conda dirs under ProgramData (admin-writable).
192
+ # Same trust tier as AppData/Local/Programs/Python above: without
193
+ # them every hook exited 0 doing nothing for these users.
194
+ /[a-zA-Z]/Users/*/miniconda3/*|/[a-zA-Z]/Users/*/anaconda3/*) return 0 ;;
195
+ /[a-zA-Z]/Users/*/miniforge3/*|/[a-zA-Z]/Users/*/mambaforge/*) return 0 ;;
196
+ /[a-zA-Z]/ProgramData/miniconda3/*|/[a-zA-Z]/ProgramData/anaconda3/*) return 0 ;;
197
+ /[a-zA-Z]/Users/*/scoop/apps/python+([0-9])/*) return 0 ;;
198
+ /[a-zA-Z]/Users/*/scoop/apps/python/*) return 0 ;;
199
+ /[a-zA-Z]/Users/*/AppData/Roaming/uv/python/*) return 0 ;;
190
200
  # All-users `py` launcher lives in the (admin-only-writable) Windows dir.
191
201
  # Exact filename keeps the anti-hijack intent (no wildcard in that dir).
192
202
  /[a-zA-Z]/Windows/py.exe) return 0 ;;
@@ -125,6 +125,23 @@ def _read_hook_input() -> dict:
125
125
  return {}
126
126
 
127
127
 
128
+ def _record_payload_shape(hook_input: dict) -> None:
129
+ """Remember WHICH identity fields the host sent this Stop hook (never their
130
+ values), so the doctor can tell "host sent nothing" from "we lost it"
131
+ (issue #278). Best effort."""
132
+ try:
133
+ def has(key):
134
+ return "yes" if isinstance(hook_input.get(key), str) and hook_input.get(key) else "no"
135
+ measure._record_hook_status(
136
+ "stop_payload",
137
+ f"stdin fields: {len(hook_input)}; session_id={has('session_id')}; "
138
+ f"transcript_path={has('transcript_path')}; "
139
+ f"CODEX_THREAD_ID env={'yes' if os.environ.get('CODEX_THREAD_ID') else 'no'}",
140
+ )
141
+ except Exception:
142
+ pass
143
+
144
+
128
145
  def _is_session_end(hook_input: dict) -> bool:
129
146
  """True when the firing hook is SessionEnd, not Stop.
130
147
 
@@ -292,10 +309,15 @@ def _sub_session_end_flush_stop(hook_input: dict) -> None:
292
309
  budget = _runner_budget(8)
293
310
  if budget < 0.1:
294
311
  return
295
- measure._dispatch_session_end_flush(
296
- ["session-end-flush", "--trigger", "stop", "--quiet", "--defer"],
297
- budget_seconds=budget,
298
- )
312
+ args = ["session-end-flush", "--trigger", "stop", "--quiet", "--defer"]
313
+ # The deferred worker has no stdin, so hand it the session identity the
314
+ # host gave THIS hook; without it the worker can only guess and (on Codex)
315
+ # wrote an "unknown" placeholder checkpoint (issue #278).
316
+ for flag, key in (("--session-id", "session_id"), ("--transcript-path", "transcript_path")):
317
+ value = hook_input.get(key)
318
+ if isinstance(value, str) and value and len(value) <= 4096 and "\x00" not in value:
319
+ args += [flag, value]
320
+ measure._dispatch_session_end_flush(args, budget_seconds=budget)
299
321
 
300
322
 
301
323
  def _sub_keepwarm_arm(hook_input: dict) -> None:
@@ -345,6 +367,7 @@ def main() -> int:
345
367
  try:
346
368
  hook_input = _read_hook_input()
347
369
  is_session_end = _is_session_end(hook_input)
370
+ _record_payload_shape(hook_input)
348
371
 
349
372
  # ONE shared HookDeadline for the entire runner. The budget differs by
350
373
  # event: 13s for Stop (under the 15s declared timeout), 58s for SessionEnd
@@ -1,8 +1,11 @@
1
1
  #!/usr/bin/env python3
2
2
  """Token Optimizer Windows launcher for Codex hooks.
3
3
 
4
- Codex spawns ``command`` hooks through ``%COMSPEC% /C`` on native Windows,
5
- and cmd.exe parses a /C command line ONCE, before anything on it runs:
4
+ Codex spawns ``command`` hooks through the session shell on native Windows
5
+ (PowerShell, ``-NoProfile -Command``) and falls back to ``%COMSPEC% /C``
6
+ (cmd.exe) when it has none, so the generated command is written to parse the
7
+ same in both (issue #278). cmd.exe parses a /C command line ONCE, before
8
+ anything on it runs:
6
9
  ``%VAR%`` expands to the pre-line value and ``setlocal
7
10
  EnableDelayedExpansion`` only takes effect from the NEXT line, so no cmd
8
11
  expansion form can carry a resolved marketplace version into the runner
@@ -28,9 +31,19 @@ Invocation contract (generated by codex_install._windows_launcher_command):
28
31
  * ``--baked-root`` is the install dir the command was generated from. It is
29
32
  the fail-open fallback only: the launcher rescans its sibling version
30
33
  dirs on every run and runs the NEWEST semver install, so hooks keep
31
- working after a marketplace upgrade prunes the baked one.
34
+ working after a marketplace upgrade prunes the baked one. A relative
35
+ value (just the version name) is taken relative to this file's folder.
36
+ * When the launcher path holds ``$`` or ``%`` (for example in the Windows user
37
+ name), the command starts it as ``python.exe -c "import os,runpy;
38
+ runpy.run_path(os.path.join(os.environ['USERPROFILE'],...))"``: plain
39
+ source that reads the folder from the environment, so neither shell ever
40
+ sees the character.
41
+ * PowerShell 5.1 may drop the bare ``--`` before a native program sees it;
42
+ the parser below already treats the first unknown token as the script.
32
43
  * ``--env KEY=VALUE`` applies per-hook environment (e.g.
33
44
  TOKEN_OPTIMIZER_NO_UPDATED_TOOL_OUTPUT=1) before dispatch.
45
+ ``--env TOKEN_OPTIMIZER_HOOK_SILENT=1`` is not forwarded: it sends the
46
+ hook's stdout/stderr to the null device, in place of a shell ``>NUL``.
34
47
  * Everything after ``--`` is forwarded verbatim: ``sys.argv`` for the
35
48
  runner becomes ``[run.py, script, *args]`` and stdin passes through
36
49
  untouched. A trailing token equal to the generated-command ownership
@@ -96,11 +109,38 @@ def _parse(argv: list[str]) -> tuple[str, list[str], str, list[str]]:
96
109
  return baked_root, env, rest[0], rest[1:]
97
110
 
98
111
 
112
+ _SILENT_ENV = "TOKEN_OPTIMIZER_HOOK_SILENT"
113
+
114
+
115
+ def _silence_output() -> None:
116
+ """Point fds 1 and 2 (and the Python streams) at the null device."""
117
+ try:
118
+ sys.stdout.flush()
119
+ sys.stderr.flush()
120
+ except (OSError, ValueError):
121
+ pass
122
+ try:
123
+ null = os.open(os.devnull, os.O_WRONLY)
124
+ os.dup2(null, 1)
125
+ os.dup2(null, 2)
126
+ os.close(null)
127
+ except OSError:
128
+ pass
129
+
130
+
99
131
  def main(argv: list[str] | None = None) -> None:
100
132
  baked_root, env_pairs, script, args = _parse(
101
133
  sys.argv[1:] if argv is None else argv
102
134
  )
103
135
  root = Path(baked_root)
136
+ # A bare version name ("5.13.59") is relative to this file's own folder.
137
+ # The generator writes that form only when the full path holds a character
138
+ # PowerShell or cmd.exe would expand, and then starts this file through a
139
+ # ``python -c`` bootstrap that reads the folder from the environment
140
+ # (issue #278).
141
+ here = globals().get("__file__")
142
+ if not root.is_absolute() and here:
143
+ root = Path(here).parent / root
104
144
  baked = root
105
145
  # Resolve the newest semver sibling of the install parent; on any
106
146
  # listing failure keep the baked install (fail-open, same contract the
@@ -117,6 +157,9 @@ def main(argv: list[str] | None = None) -> None:
117
157
  # opens with real content: an unreadable, missing, or incomplete
118
158
  # candidate is skipped (not fatal), and a genuine ACL denial is honored.
119
159
  try:
160
+ if not _SEMVER_DIR_RE.fullmatch(root.name):
161
+ # A development checkout is pinned: never trade it for a sibling.
162
+ raise OSError("not a versioned install")
120
163
  candidates = sorted(
121
164
  (p for p in root.parent.iterdir()
122
165
  if _SEMVER_DIR_RE.fullmatch(p.name)),
@@ -149,10 +192,21 @@ def main(argv: list[str] | None = None) -> None:
149
192
  pass
150
193
  os.environ["TOKEN_OPTIMIZER_RUNTIME"] = "codex"
151
194
  os.environ["TOKEN_OPTIMIZER_RUNTIME_ROOT"] = str(root)
195
+ # Per-hook "no output" request: replaces the ">NUL 2>&1" redirect, which
196
+ # PowerShell and cmd.exe do not spell the same way (issue #278). Carried
197
+ # as an --env pair so a launcher older than this one still parses the
198
+ # command (it just stays noisy). Consumed here, never set in the runner's
199
+ # environment.
200
+ silent = False
152
201
  for pair in env_pairs:
153
202
  key, sep, value = pair.partition("=")
203
+ if key == _SILENT_ENV:
204
+ silent = silent or value == "1"
205
+ continue
154
206
  if sep and key: # skip malformed pairs rather than breaking the hook
155
207
  os.environ[key] = value
208
+ if silent:
209
+ _silence_output()
156
210
  runner = root / "hooks" / "run.py"
157
211
  sys.argv = [str(runner), script, *args]
158
212
  sys.path.insert(0, str(runner.parent))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexgreensh/token-optimizer",
3
- "version": "5.13.56",
3
+ "version": "5.13.59",
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)",
@@ -9,6 +9,7 @@ Zero external dependencies. Python 3.10+ (for match/case and type unions).
9
9
  from __future__ import annotations # PEP 604 union syntax compat for Python 3.9
10
10
 
11
11
  import json
12
+ import os
12
13
  import re
13
14
  import sqlite3
14
15
  from datetime import datetime
@@ -19,7 +20,34 @@ from pathlib import Path
19
20
  # ---------------------------------------------------------------------------
20
21
 
21
22
  HOME = Path.home()
22
- CLAUDE_DIR = HOME / ".claude"
23
+
24
+
25
+ def _resolve_claude_dir() -> Path:
26
+ """Claude Code's config dir, honoring CLAUDE_CONFIG_DIR (#198).
27
+
28
+ Uses Token Optimizer's runtime_env.claude_home() when it is importable (the
29
+ plugin layout puts it on sys.path); standalone, applies the same rules: an
30
+ absolute, existing, non-symlink directory wins, else ~/.claude.
31
+ """
32
+ try:
33
+ from runtime_env import claude_home # type: ignore
34
+ return claude_home()
35
+ except Exception:
36
+ pass
37
+ fallback = HOME / ".claude"
38
+ raw = os.environ.get("CLAUDE_CONFIG_DIR", "").strip()
39
+ if not raw:
40
+ return fallback
41
+ try:
42
+ candidate = Path(raw).expanduser()
43
+ if candidate.is_absolute() and candidate.is_dir() and not candidate.is_symlink():
44
+ return candidate.resolve(strict=False)
45
+ except (OSError, RuntimeError):
46
+ pass
47
+ return fallback
48
+
49
+
50
+ CLAUDE_DIR = _resolve_claude_dir()
23
51
  CHARS_PER_TOKEN = 4.0
24
52
  _KNOWN_PROVIDER_PREFIXES = {
25
53
  "anthropic", "openai", "google", "gemini", "vertex", "bedrock",
@@ -445,6 +445,12 @@
445
445
  "cache_read": 0.1,
446
446
  "cache_write": 2.5
447
447
  },
448
+ "gpt-6.1-terra": {
449
+ "input": 1.0,
450
+ "output": 5.0,
451
+ "cache_read": 0.05,
452
+ "cache_write": 1.25
453
+ },
448
454
  "o1": {
449
455
  "input": 15.0,
450
456
  "output": 60.0,
@@ -550,6 +556,12 @@
550
556
  "output": 15.0,
551
557
  "cache_read": 0.2,
552
558
  "cache_write": 5.0
559
+ },
560
+ "gpt-6.1-terra": {
561
+ "input": 2.0,
562
+ "output": 7.5,
563
+ "cache_read": 0.1,
564
+ "cache_write": 2.5
553
565
  }
554
566
  },
555
567
  "gemini": {
@@ -691,5 +703,5 @@
691
703
  "cache_read": 0.4
692
704
  }
693
705
  },
694
- "generated_at": "2026-10-16T11:36:52Z"
706
+ "generated_at": "2026-10-27T06:50:43Z"
695
707
  }
@@ -93,13 +93,62 @@ def _comment_out_setting(pattern: re.Pattern[str], text: str) -> str:
93
93
  return pattern.sub(r"\1# replaced by Token Optimizer: \2\3", text)
94
94
 
95
95
 
96
+ # A TOML table header line: [name] or [[name]], optionally followed by a
97
+ # comment. Candidates only: a line inside a multi-line array can look the same,
98
+ # so _insert_at_root confirms the placement with a real TOML parser when one
99
+ # is available.
100
+ _TABLE_HEADER_RE = re.compile(r"(?m)^[ \t]*\[\[?[^\[\]\n]+\]\]?[ \t]*(?:#.*)?$")
101
+
102
+
103
+ def _toml_root_value(config_text: str):
104
+ """Return (state, root value of the setting). state is "ok", "invalid"
105
+ (the text does not parse) or "unavailable" (no TOML parser, Python < 3.11)."""
106
+ try:
107
+ import tomllib # Python 3.11+
108
+ except ImportError:
109
+ return "unavailable", None
110
+ try:
111
+ data = tomllib.loads(config_text)
112
+ except Exception:
113
+ return "invalid", None
114
+ return "ok", data.get("experimental_compact_prompt_file")
115
+
116
+
117
+ def _insert_at_root(config_text: str, block: str, prompt_path: Path) -> str:
118
+ """Place the managed block at the TOML root, before the first table.
119
+
120
+ Appending at end of file put the key inside whatever table came last
121
+ (issue #276: it landed in a [projects.'...'] table and Codex never saw
122
+ it). The blank-line separator mirrors _MANAGED_BLOCK_RE so uninstall
123
+ stays byte-faithful.
124
+ """
125
+ # Without a parser, or when the user's file does not parse to begin with,
126
+ # trust the first header-looking line. Otherwise keep only a placement the
127
+ # parser confirms puts the key at the root.
128
+ verify = _toml_root_value(config_text)[0] == "ok"
129
+ candidates = [m.start() for m in _TABLE_HEADER_RE.finditer(config_text)]
130
+ for pos in candidates:
131
+ if pos == 0:
132
+ updated = block + config_text
133
+ else:
134
+ updated = config_text[:pos] + block + "\n" + config_text[pos:]
135
+ if not verify:
136
+ return updated
137
+ state, value = _toml_root_value(updated)
138
+ if state == "ok" and value == str(prompt_path):
139
+ return updated
140
+ suffix = "" if not config_text or config_text.endswith("\n") else "\n"
141
+ return config_text + suffix + "\n" + block
142
+
143
+
96
144
  def _replace_or_append_config(config_text: str, prompt_path: Path, *, force: bool) -> tuple[str, str]:
97
145
  block = _managed_block(prompt_path)
98
- managed_re = re.compile(
99
- rf"(?ms)^{re.escape(MANAGED_BEGIN)}.*?^{re.escape(MANAGED_END)}\n?"
100
- )
101
- if managed_re.search(config_text):
102
- return managed_re.sub(block, config_text), "updated"
146
+ stripped, had_block = _strip_managed_block(config_text)
147
+ if had_block:
148
+ # Re-insert rather than replace in place, so a block an older version
149
+ # appended under a table moves back to the root. A block already at
150
+ # the root lands in the same spot, so this is a no-op for it.
151
+ return _insert_at_root(stripped, block, prompt_path), "updated"
103
152
 
104
153
  if COMPACT_FILE_RE.search(config_text) and not force:
105
154
  raise ValueError("config.toml already has experimental_compact_prompt_file; rerun with --force to replace")
@@ -110,8 +159,40 @@ def _replace_or_append_config(config_text: str, prompt_path: Path, *, force: boo
110
159
  if INLINE_COMPACT_RE.search(config_text):
111
160
  config_text = _comment_out_setting(INLINE_COMPACT_LINE_RE, config_text)
112
161
 
113
- suffix = "" if not config_text or config_text.endswith("\n") else "\n"
114
- return config_text + suffix + "\n" + block, "installed"
162
+ return _insert_at_root(config_text, block, prompt_path), "installed"
163
+
164
+
165
+ def configured_prompt_state(config_text: str, prompt_path: Path) -> str:
166
+ """Classify the compact-prompt setting: "ours", "misplaced", "custom", or "none".
167
+
168
+ Compares the decoded path, not raw text: TOML stores Windows backslashes
169
+ escaped, so str(prompt_path) never appears verbatim in the file there.
170
+ """
171
+ state, value = _toml_root_value(config_text)
172
+ has_block = bool(_MANAGED_BLOCK_RE.search(config_text))
173
+ if state == "ok":
174
+ if value is not None:
175
+ return "ours" if str(value) == str(prompt_path) else "custom"
176
+ if has_block:
177
+ return "misplaced"
178
+ else:
179
+ match = re.search(
180
+ rf"(?ms)^{re.escape(MANAGED_BEGIN)}\s*^\s*experimental_compact_prompt_file\s*=\s*(\"(?:[^\"\\\\]|\\\\.)*\")",
181
+ config_text,
182
+ )
183
+ if match:
184
+ try:
185
+ decoded = json.loads(match.group(1))
186
+ except ValueError:
187
+ decoded = None
188
+ if decoded == str(prompt_path):
189
+ header = _TABLE_HEADER_RE.search(config_text)
190
+ if header is not None and header.start() < match.start():
191
+ return "misplaced"
192
+ return "ours"
193
+ if "compact_prompt" in config_text or "experimental_compact_prompt_file" in config_text:
194
+ return "custom"
195
+ return "none"
115
196
 
116
197
 
117
198
  def plan_install(force: bool = False) -> dict[str, str | bool]:
@@ -237,9 +318,12 @@ def status() -> str:
237
318
  text = config_path.read_text(encoding="utf-8")
238
319
  except (OSError, UnicodeDecodeError):
239
320
  return f"not configured: {config_path} unreadable"
240
- if str(prompt_path) in text and prompt_path.exists():
321
+ state = configured_prompt_state(text, prompt_path)
322
+ if state == "ours" and prompt_path.exists():
241
323
  return f"configured: {prompt_path}"
242
- if "compact_prompt" in text or "experimental_compact_prompt_file" in text:
324
+ if state == "misplaced":
325
+ return "misplaced: setting is inside a table; rerun measure.py codex-compact-prompt --install"
326
+ if state in ("ours", "custom"):
243
327
  return "configured: custom compact prompt present"
244
328
  return "not configured"
245
329