@alexgreensh/token-optimizer 5.13.63 → 5.13.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/hooks/sessionstart_runner.py +5 -0
- package/package.json +1 -1
- package/skills/fleet-auditor/SKILL.md +1 -1
- package/skills/token-coach/SKILL.md +49 -40
- package/skills/token-coach/references/deterministic-stages.md +1 -1
- package/skills/token-coach/references/keepwarm-consent.md +7 -7
- package/skills/token-dashboard/SKILL.md +30 -6
- package/skills/token-optimizer/SKILL.md +42 -44
- package/skills/token-optimizer/assets/dashboard.html +180 -44
- package/skills/token-optimizer/references/agent-prompts.md +4 -1
- package/skills/token-optimizer/references/codex-workflow.md +30 -12
- package/skills/token-optimizer/references/cursor-workflow.md +9 -7
- package/skills/token-optimizer/references/implementation-playbook.md +4 -4
- package/skills/token-optimizer/references/keepwarm-consent.md +7 -7
- package/skills/token-optimizer/references/opencode-workflow.md +4 -2
- package/skills/token-optimizer/references/optimization-checklist.md +9 -9
- package/skills/token-optimizer/references/phase0-setup.md +9 -9
- package/skills/token-optimizer/references/presentation-workflow.md +3 -3
- package/skills/token-optimizer/references/session-continuity.md +1 -1
- package/skills/token-optimizer/references/token-flow-architecture.md +2 -1
- package/skills/token-optimizer/scripts/archive_result.py +6 -1
- package/skills/token-optimizer/scripts/archive_window.py +274 -0
- package/skills/token-optimizer/scripts/checkpoint_state.py +385 -0
- package/skills/token-optimizer/scripts/codex_session.py +149 -1
- package/skills/token-optimizer/scripts/devin_source.py +2 -2
- package/skills/token-optimizer/scripts/measure.py +3498 -1111
- package/skills/token-optimizer/scripts/read_cache.py +20 -7
- package/skills/token-optimizer/scripts/recovery_output.py +67 -18
- package/skills/token-optimizer/scripts/session_activity.py +169 -0
- package/skills/token-optimizer/scripts/session_store.py +63 -9
- package/skills/token-optimizer/scripts/statusline.js +4 -6
|
@@ -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.
|
|
16
|
+
"version": "5.13.65",
|
|
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.
|
|
58
|
+
"version": "5.13.65",
|
|
59
59
|
"author": {
|
|
60
60
|
"name": "Alex Greenshpun",
|
|
61
61
|
"url": "https://linkedin.com/in/alexgreensh"
|
|
@@ -462,6 +462,11 @@ def _sub_clear_compacted(hook_input: dict) -> None:
|
|
|
462
462
|
Emits nothing on stdout by design (all its output, success and failure, goes
|
|
463
463
|
to stderr), so it cannot perturb the SessionStart context stream.
|
|
464
464
|
"""
|
|
465
|
+
# The context just emptied, so resume blocks this session already received
|
|
466
|
+
# (prompt-continuity dedups identical ones) are no longer in it. Cheap local
|
|
467
|
+
# unlink; never raises.
|
|
468
|
+
if hook_input:
|
|
469
|
+
measure._clear_resume_dedup_markers(hook_input.get("session_id"))
|
|
465
470
|
budget = _runner_budget(10)
|
|
466
471
|
if budget < 0.1:
|
|
467
472
|
return
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexgreensh/token-optimizer",
|
|
3
|
-
"version": "5.13.
|
|
3
|
+
"version": "5.13.65",
|
|
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)",
|
|
@@ -52,7 +52,7 @@ done <<EOF
|
|
|
52
52
|
$(find -L "$HOME/.claude/skills" "$HOME/.claude/plugins/cache" "$HOME/.claude/token-optimizer" "$HOME/.codex/skills" "$HOME/.codex/plugins/cache" "$HOME/.config/opencode/plugins/cache" "$HOME/.config/opencode/plugins" -type f -name fleet.py -path '*fleet-auditor*/scripts/fleet.py' 2>/dev/null)
|
|
53
53
|
EOF
|
|
54
54
|
if [ -z "$FLEET_PY" ]; then echo "[Error] fleet.py not found. Is Fleet Auditor installed?"; exit 1; fi
|
|
55
|
-
echo "Using: $FLEET_PY"
|
|
55
|
+
echo "Using: $FLEET_PY" >&2
|
|
56
56
|
export TOKEN_OPTIMIZER_RUNTIME="$RUNTIME"
|
|
57
57
|
```
|
|
58
58
|
Use `$FLEET_PY` for all subsequent fleet.py calls.
|
|
@@ -11,44 +11,30 @@ Token Optimizer's own skills (`token-optimizer`, `token-coach`, `token-dashboard
|
|
|
11
11
|
|
|
12
12
|
## 1. Collect the data
|
|
13
13
|
|
|
14
|
-
Run exactly this first. It finds the newest installed copy, so a stale plugin cache never answers:
|
|
14
|
+
Run exactly this first. It finds the newest installed copy, so a stale plugin cache never answers. Each Bash call is a fresh shell, so env vars do not persist: the block echoes the resolved values, and every later command must use them verbatim.
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
17
|
RUNTIME="${TOKEN_OPTIMIZER_RUNTIME:-}"
|
|
18
18
|
if [ -z "$RUNTIME" ]; then
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
elif [ -n "$OPENCODE" ] || [ -n "$OPENCODE_BIN" ] || [ -n "$OPENCODE_CONFIG_DIR" ] || [ -n "$OPENCODE_CONFIG" ]; then
|
|
22
|
-
|
|
23
|
-
elif [ -n "$
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
elif [ -n "$
|
|
28
|
-
|
|
29
|
-
elif [ -
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
RUNTIME="cursor"
|
|
33
|
-
elif [ -n "$TOKEN_OPTIMIZER_ANTIGRAVITY_HOME" ]; then
|
|
34
|
-
RUNTIME="antigravity"
|
|
35
|
-
elif [ -n "$GROK_HOME" ] || [ -n "$TOKEN_OPTIMIZER_GROK_HOME" ]; then
|
|
36
|
-
RUNTIME="grok"
|
|
37
|
-
elif [ -n "$CLAUDECODE" ] || [ -n "$CLAUDE_CODE_ENTRYPOINT" ] || [ -n "$CLAUDE_CODE_SESSION_ID" ]; then
|
|
38
|
-
RUNTIME="claude"
|
|
39
|
-
elif [ -d "$HOME/.config/opencode" ] && [ ! -d "$HOME/.codex" ]; then
|
|
40
|
-
RUNTIME="opencode"
|
|
41
|
-
elif [ -d "$HOME/.codex" ]; then
|
|
42
|
-
RUNTIME="codex"
|
|
43
|
-
else
|
|
44
|
-
RUNTIME="claude"
|
|
19
|
+
# Harness config-dir env sits above CLAUDECODE, which every Claude Code child inherits.
|
|
20
|
+
if [ -n "$CLAUDE_PLUGIN_ROOT" ] || [ -n "$CLAUDE_PLUGIN_DATA" ]; then RUNTIME="claude"
|
|
21
|
+
elif [ -n "$OPENCODE" ] || [ -n "$OPENCODE_BIN" ] || [ -n "$OPENCODE_CONFIG_DIR" ] || [ -n "$OPENCODE_CONFIG" ]; then RUNTIME="opencode"
|
|
22
|
+
elif [ -n "$CODEX_HOME" ]; then RUNTIME="codex"
|
|
23
|
+
elif [ -n "$HERMES_HOME" ]; then RUNTIME="hermes"
|
|
24
|
+
elif [ -n "$COPILOT_HOME" ] || [ -n "$TOKEN_OPTIMIZER_COPILOT_HOME" ]; then RUNTIME="copilot"
|
|
25
|
+
elif [ -n "$TOKEN_OPTIMIZER_CURSOR_HOME" ]; then RUNTIME="cursor"
|
|
26
|
+
elif [ -n "$TOKEN_OPTIMIZER_ANTIGRAVITY_HOME" ]; then RUNTIME="antigravity"
|
|
27
|
+
elif [ -n "$GROK_HOME" ] || [ -n "$TOKEN_OPTIMIZER_GROK_HOME" ]; then RUNTIME="grok"
|
|
28
|
+
elif [ -n "$CLAUDECODE" ] || [ -n "$CLAUDE_CODE_ENTRYPOINT" ] || [ -n "$CLAUDE_CODE_SESSION_ID" ]; then RUNTIME="claude"
|
|
29
|
+
elif [ -d "$HOME/.config/opencode" ] && [ ! -d "$HOME/.codex" ]; then RUNTIME="opencode"
|
|
30
|
+
elif [ -d "$HOME/.codex" ]; then RUNTIME="codex"
|
|
31
|
+
else RUNTIME="claude"
|
|
45
32
|
fi
|
|
46
33
|
fi
|
|
47
34
|
|
|
48
35
|
# Resolve measure.py to the NEWEST installed copy across channels so a stale
|
|
49
|
-
# plugin-cache copy never shadows a fresh install
|
|
50
|
-
# install.sh symlink
|
|
51
|
-
# copy's plugin.json for its version. find (not bare globs) never errors under zsh.
|
|
36
|
+
# plugin-cache copy never shadows a fresh install (find -L follows the
|
|
37
|
+
# install.sh symlink; cd -P resolves it before reading each copy's plugin.json).
|
|
52
38
|
MEASURE_PY=""; _best_ver=""
|
|
53
39
|
while IFS= read -r _cand; do
|
|
54
40
|
[ -f "$_cand" ] || continue
|
|
@@ -62,21 +48,44 @@ done <<EOF
|
|
|
62
48
|
$(find -L "$HOME/.claude/skills" "$HOME/.claude/plugins/cache" "$HOME/.claude/token-optimizer" "$HOME/.codex/skills" "$HOME/.codex/plugins/cache" "$HOME/.config/opencode/plugins/cache" "$HOME/.config/opencode/plugins" -type f -name measure.py -path '*token-optimizer*/scripts/measure.py' 2>/dev/null)
|
|
63
49
|
EOF
|
|
64
50
|
if [ -z "$MEASURE_PY" ] || [ ! -f "$MEASURE_PY" ]; then echo "[Error] measure.py not found. Is Token Optimizer installed?"; exit 1; fi
|
|
51
|
+
|
|
52
|
+
# Resolve an interpreter that really runs Python. On native-Windows Git Bash
|
|
53
|
+
# `python3` is often the Microsoft Store alias stub, which fails silently under
|
|
54
|
+
# a pipe (#301). Probe `python` first there, `python3` first elsewhere; require >=3.9.
|
|
55
|
+
PY=""; _py_candidates="python3 python"
|
|
56
|
+
case "$(uname -s 2>/dev/null)" in MINGW*|MSYS*|CYGWIN*) _py_candidates="python python3" ;; esac
|
|
57
|
+
for _py in $_py_candidates; do
|
|
58
|
+
command -v "$_py" >/dev/null 2>&1 || continue
|
|
59
|
+
_py_ver="$("$_py" -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")' 2>/dev/null)" || _py_ver=""
|
|
60
|
+
_py_major="$(echo "$_py_ver" | cut -d. -f1)"; _py_minor="$(echo "$_py_ver" | cut -d. -f2)"
|
|
61
|
+
[ -n "$_py_major" ] && [ -n "$_py_minor" ] || continue
|
|
62
|
+
case "$_py_major$_py_minor" in *[!0-9]*) continue ;; esac
|
|
63
|
+
if [ "$_py_major" -lt 3 ] 2>/dev/null || { [ "$_py_major" -eq 3 ] && [ "$_py_minor" -lt 9 ]; } 2>/dev/null; then continue; fi
|
|
64
|
+
PY="$_py"; break
|
|
65
|
+
done
|
|
66
|
+
if [ -z "$PY" ]; then echo "[Error] Python 3.9+ not found (the Windows Store python3 stub does not count). Install Python, then retry."; exit 1; fi
|
|
65
67
|
export TOKEN_OPTIMIZER_RUNTIME="$RUNTIME"
|
|
66
68
|
# The coach files ship beside measure.py, so they are always the same version.
|
|
67
69
|
COACH_DIR="$(cd -P -- "$(dirname -- "$MEASURE_PY")/../../token-coach" 2>/dev/null && pwd)"
|
|
68
|
-
[ -d "$COACH_DIR/references" ] || echo "[Error] token-coach references not found beside $MEASURE_PY. Reinstall Token Optimizer."
|
|
70
|
+
[ -d "$COACH_DIR/references" ] || echo "[Error] token-coach references not found beside $MEASURE_PY. Reinstall Token Optimizer." >&2
|
|
71
|
+
|
|
72
|
+
# Separate Bash calls do not share shell state: copy these into every later command.
|
|
73
|
+
# They print to stderr so a --json command chained in the same call keeps stdout parseable.
|
|
74
|
+
printf 'RUNTIME=%q\n' "$RUNTIME" >&2
|
|
75
|
+
printf 'MEASURE_PY=%q\n' "$MEASURE_PY" >&2
|
|
76
|
+
printf 'PY=%q\n' "$PY" >&2
|
|
77
|
+
printf 'COACH_DIR=%q\n' "$COACH_DIR" >&2
|
|
69
78
|
```
|
|
70
79
|
|
|
71
80
|
Then gather, skipping any command that fails (older installs lack some):
|
|
72
81
|
|
|
73
82
|
```bash
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
[ "$RUNTIME" = "codex" ] &&
|
|
83
|
+
"$PY" "$MEASURE_PY" coach --json # snapshot, patterns, history, questions
|
|
84
|
+
"$PY" "$MEASURE_PY" quality current --json # this session's quality score and issues
|
|
85
|
+
"$PY" "$MEASURE_PY" subagent-cache status --json # Claude Code: subagent cache lifetime and its payoff
|
|
86
|
+
"$PY" "$MEASURE_PY" compact-advice --json # Claude Code: would an earlier compact window pay off
|
|
87
|
+
"$PY" "$MEASURE_PY" recommendations --json # Claude Code: the daily usage-recommendations record
|
|
88
|
+
[ "$RUNTIME" = "codex" ] && "$PY" "$MEASURE_PY" codex-doctor --project "$PWD" --json
|
|
80
89
|
```
|
|
81
90
|
|
|
82
91
|
What the coach JSON holds: `snapshot` (current overhead), `patterns_good` and `patterns_bad` (named patterns with detail and fix), `history` (7-day against earlier: quality, session length, cache hit rate, grade spread, cost per session, short against long sessions, compression gap, share of sessions that switched model), `deterministic_candidates` when present (work the user repeats that a script could do), and `usage_recommendations` on Claude Code (the daily-measured record: one item each for the compact window and the subagent cache lifetime, plus `wins` once the user acted on an earlier recommendation and the numbers moved). Field-by-field meaning and the numbers to quote: [references/quick-reference.md](references/quick-reference.md).
|
|
@@ -126,16 +135,16 @@ Sound like a knowledgeable friend. Two to four exchanges is typical; follow the
|
|
|
126
135
|
|
|
127
136
|
Close with three to five actions ordered by impact. Each has a bold name, one line on what to do, the estimated saving and where that estimate comes from, and whether it is a quick win or a deeper change. Include, when they apply:
|
|
128
137
|
|
|
129
|
-
- Quality under 70 on Claude Code: `
|
|
138
|
+
- Quality under 70 on Claude Code: `"$PY" "$MEASURE_PY" setup-smart-compact`. On Codex: `TOKEN_OPTIMIZER_RUNTIME=codex "$PY" "$MEASURE_PY" codex-install --project .`
|
|
130
139
|
- Quality under 50: `/compact` or `/clear` before more work.
|
|
131
140
|
- Steps to move from a model to a script, each with the mechanism (hook, script, scheduled job).
|
|
132
141
|
|
|
133
142
|
Then check the plan against the data: every figure traces to a command output or the quick reference, and no action touches Token Optimizer's own skills. Fix what fails and check again before sending.
|
|
134
143
|
|
|
135
|
-
Offer `/token-optimizer` when they want the changes made for them, and the dashboard (`
|
|
144
|
+
Offer `/token-optimizer` when they want the changes made for them, and the dashboard (`"$PY" "$MEASURE_PY" dashboard`; on Codex quote the `Dashboard:` line it prints, never a remembered path) when they want to watch the trend.
|
|
136
145
|
|
|
137
146
|
## Keep-Warm (Claude Code, asked once)
|
|
138
147
|
|
|
139
|
-
After the plan, run `
|
|
148
|
+
After the plan, run `"$PY" "$MEASURE_PY" keepwarm-consent-status`. Only when `should_ask` is true, follow [references/keepwarm-consent.md](references/keepwarm-consent.md) exactly: it spends the user's money, so the wording and the order of commands are fixed.
|
|
140
149
|
|
|
141
150
|
Written for and checked on Claude Opus 5.5 and Sonnet 5.5, and GPT-5.6 on Codex.
|
|
@@ -33,7 +33,7 @@ A script costs no tokens, takes milliseconds, and returns the same answer every
|
|
|
33
33
|
|
|
34
34
|
## Finding candidates in the user's own data
|
|
35
35
|
|
|
36
|
-
`
|
|
36
|
+
`"$PY" "$MEASURE_PY" coach --json` may include `deterministic_candidates`: tool-call sequences the user repeats across sessions with the same shape, subagents launched again and again from near-identical prompts, and loops that poll or retry. Each entry carries how often it ran and what it cost. Start from those, since they are measured.
|
|
37
37
|
|
|
38
38
|
When the list is empty or missing, ask the user to describe the workflow as numbered steps, then apply the one test to each. Useful prompts for them:
|
|
39
39
|
|
|
@@ -5,7 +5,7 @@ Keep-Warm pings the prompt cache before it expires so a paused session resumes w
|
|
|
5
5
|
## 1. Check whether to ask
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
|
|
8
|
+
"$PY" "$MEASURE_PY" keepwarm-consent-status # JSON: {billing_mode, consent, should_ask}
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
`should_ask` false: say nothing. True (API-billed, not yet asked): continue.
|
|
@@ -13,7 +13,7 @@ python3 "$MEASURE_PY" keepwarm-consent-status # JSON: {billing_mode, consent,
|
|
|
13
13
|
## 2. Compute the user's own projection
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
|
|
16
|
+
"$PY" "$MEASURE_PY" keepwarm-backfill --json --no-fence # read modes."probe-only".net_usd
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## 3. Pitch
|
|
@@ -28,21 +28,21 @@ When a session pauses past its cache window and resumes, the whole prefix is wri
|
|
|
28
28
|
Recording the answer before anything else means an interrupted run never leaves an "asked" marker with no answer.
|
|
29
29
|
|
|
30
30
|
```bash
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
"$PY" "$MEASURE_PY" keepwarm-enable # yes: records consent and installs the scheduler (macOS)
|
|
32
|
+
"$PY" "$MEASURE_PY" keepwarm-disable # no
|
|
33
33
|
```
|
|
34
34
|
|
|
35
35
|
Both are final, so the user is not asked again. Only when the user neither accepts nor declines:
|
|
36
36
|
|
|
37
37
|
```bash
|
|
38
|
-
|
|
38
|
+
"$PY" "$MEASURE_PY" keepwarm-consent-asked # marks the question as shown
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
## 5. Confirm it is armed (after a yes)
|
|
42
42
|
|
|
43
43
|
```bash
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
"$PY" "$MEASURE_PY" keepwarm-scheduler status # installed and loaded state (macOS)
|
|
45
|
+
"$PY" "$MEASURE_PY" keepwarm-tick --dry-run # what the next tick would decide
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
On Linux and Windows the scheduler is not shipped yet: Keep-Warm runs watchdog-only until the user wires `keepwarm-tick` to their own cron or timer. Tell them that plainly.
|
|
@@ -9,7 +9,7 @@ Answers what a session cost from the user's own words, and opens an up-to-date d
|
|
|
9
9
|
|
|
10
10
|
## Instructions
|
|
11
11
|
|
|
12
|
-
1. **Resolve runtime and measure.py path
|
|
12
|
+
1. **Resolve runtime and measure.py path** (each Bash call is a fresh shell, so env vars do not persist: the block echoes the resolved values, and every later command must use them verbatim):
|
|
13
13
|
```bash
|
|
14
14
|
RUNTIME="${TOKEN_OPTIMIZER_RUNTIME:-}"
|
|
15
15
|
if [ -z "$RUNTIME" ]; then
|
|
@@ -59,12 +59,36 @@ done <<EOF
|
|
|
59
59
|
$(find -L "$HOME/.claude/skills" "$HOME/.claude/plugins/cache" "$HOME/.claude/token-optimizer" "$HOME/.codex/skills" "$HOME/.codex/plugins/cache" "$HOME/.config/opencode/plugins/cache" "$HOME/.config/opencode/plugins" -type f -name measure.py -path '*token-optimizer*/scripts/measure.py' 2>/dev/null)
|
|
60
60
|
EOF
|
|
61
61
|
if [ -z "$MEASURE_PY" ]; then echo "[Error] measure.py not found. Is Token Optimizer installed?"; exit 1; fi
|
|
62
|
+
|
|
63
|
+
# Resolve an interpreter that really runs Python. On native-Windows Git Bash
|
|
64
|
+
# `python3` is often the Microsoft Store alias stub, which prints "Python was
|
|
65
|
+
# not found" instead of running -- and a gate piped to `head` then looks like
|
|
66
|
+
# a pass (#301). Probe `python` first there, `python3` first elsewhere, and
|
|
67
|
+
# accept only a clean >=3.9 version reply (same ladder install.sh uses).
|
|
68
|
+
PY=""; _py_candidates="python3 python"
|
|
69
|
+
case "$(uname -s 2>/dev/null)" in MINGW*|MSYS*|CYGWIN*) _py_candidates="python python3" ;; esac
|
|
70
|
+
for _py in $_py_candidates; do
|
|
71
|
+
command -v "$_py" >/dev/null 2>&1 || continue
|
|
72
|
+
_py_ver="$("$_py" -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")' 2>/dev/null)" || _py_ver=""
|
|
73
|
+
_py_major="$(echo "$_py_ver" | cut -d. -f1)"; _py_minor="$(echo "$_py_ver" | cut -d. -f2)"
|
|
74
|
+
[ -n "$_py_major" ] && [ -n "$_py_minor" ] || continue
|
|
75
|
+
case "$_py_major$_py_minor" in *[!0-9]*) continue ;; esac
|
|
76
|
+
if [ "$_py_major" -lt 3 ] 2>/dev/null || { [ "$_py_major" -eq 3 ] && [ "$_py_minor" -lt 9 ]; } 2>/dev/null; then continue; fi
|
|
77
|
+
PY="$_py"; break
|
|
78
|
+
done
|
|
79
|
+
if [ -z "$PY" ]; then echo "[Error] Python 3.9+ not found (the Windows Store python3 stub does not count). Install Python, then retry."; exit 1; fi
|
|
62
80
|
export TOKEN_OPTIMIZER_RUNTIME="$RUNTIME"
|
|
81
|
+
|
|
82
|
+
# Separate Bash calls do not share shell state: copy these into every later command.
|
|
83
|
+
# They print to stderr so a --json command chained in the same call keeps stdout parseable.
|
|
84
|
+
printf 'RUNTIME=%q\n' "$RUNTIME" >&2
|
|
85
|
+
printf 'MEASURE_PY=%q\n' "$MEASURE_PY" >&2
|
|
86
|
+
printf 'PY=%q\n' "$PY" >&2
|
|
63
87
|
```
|
|
64
88
|
|
|
65
89
|
2. **A cost question** ("what did the Garden session yesterday cost?", "how much were my TR sync sessions?", "cost of session <id>"): run this instead of steps 3-4.
|
|
66
90
|
```bash
|
|
67
|
-
|
|
91
|
+
"$PY" "$MEASURE_PY" session-cost <the user's words> --json
|
|
68
92
|
```
|
|
69
93
|
- Pass the user's own words (`garden yesterday`, `tr sync`, an id or its first 8 characters). Names match topic, slug and project, case-insensitively, and all of them must match; `today`, `yesterday` and `YYYY-MM-DD` set the day. Add `--days N` for "this week" (default 30), `--limit N` to list more.
|
|
70
94
|
- Answer from `sessions[]`: date, topic, `cost_usd.total`, the main-thread and subagent split when subagents ran, `cost_usd.on_date` when a day was asked and the session ran over several days, and `partial_reason` when set. Several matches: give `total_cost_usd` and list them, asking which one only when it changes the answer.
|
|
@@ -77,16 +101,16 @@ python3 "$MEASURE_PY" session-cost <the user's words> --json
|
|
|
77
101
|
# TOKEN_OPTIMIZER_INTERACTIVE=1 marks this as a user-initiated open, so the 20s
|
|
78
102
|
# hook budget does not cut off a heavy rebuild on a large history. The user is
|
|
79
103
|
# waiting for this open, so it runs unbounded.
|
|
80
|
-
TOKEN_OPTIMIZER_INTERACTIVE=1
|
|
81
|
-
TOKEN_OPTIMIZER_INTERACTIVE=1
|
|
104
|
+
TOKEN_OPTIMIZER_INTERACTIVE=1 "$PY" "$MEASURE_PY" collect --quiet && \
|
|
105
|
+
TOKEN_OPTIMIZER_INTERACTIVE=1 "$PY" "$MEASURE_PY" dashboard
|
|
82
106
|
```
|
|
83
107
|
|
|
84
108
|
This collects the latest session data into the trends database, regenerates the dashboard HTML, and opens it in your default browser.
|
|
85
109
|
|
|
86
110
|
4. **Tell the user** the dashboard is open. Probe the daemon before mentioning any URL:
|
|
87
|
-
- Probe daemon: `
|
|
111
|
+
- Probe daemon: `"$PY" "$MEASURE_PY" daemon-status 2>/dev/null`
|
|
88
112
|
- If DAEMON_RUNNING: lead with `URL: http://localhost:24842/token-optimizer` (bookmarkable, auto-updates), then mention the file fallback.
|
|
89
|
-
- If DAEMON_NOT_RUNNING: do not print the `localhost:24842` URL. Tell the user the dashboard opened as a file, and suggest `
|
|
113
|
+
- If DAEMON_NOT_RUNNING: do not print the `localhost:24842` URL. Tell the user the dashboard opened as a file, and suggest `"$PY" "$MEASURE_PY" setup-daemon` (macOS and Windows) if they want a bookmarkable URL.
|
|
90
114
|
- File path: never hardcode it. It is install-dependent. Cite the ` Dashboard: ` line from the step-3 output.
|
|
91
115
|
- For Codex when DAEMON_NOT_RUNNING: do not imply the Claude daemon is required; the generated file works, and Stop hooks refresh it when balanced hooks are installed.
|
|
92
116
|
|
|
@@ -17,28 +17,17 @@ The audit below reads and edits `~/.claude`, which is the wrong target anywhere
|
|
|
17
17
|
**a. Environment check** (touches no file; an explicit `TOKEN_OPTIMIZER_RUNTIME` wins, and Claude plugin variables are checked before stray `OPENCODE_*` exports, the same order `detect_runtime()` uses):
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
|
-
if [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "claude" ] || [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "codex" ]; then
|
|
21
|
-
|
|
22
|
-
elif [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "
|
|
23
|
-
|
|
24
|
-
elif [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "
|
|
25
|
-
|
|
26
|
-
elif [ "${
|
|
27
|
-
|
|
28
|
-
elif [ "${
|
|
29
|
-
|
|
30
|
-
elif [ -n "${
|
|
31
|
-
:
|
|
32
|
-
elif [ -n "${OPENCODE_BIN:-}${OPENCODE_CONFIG_DIR:-}${OPENCODE_DATA_DIR:-}${OPENCODE_CONFIG:-}${OPENCODE_CLIENT:-}" ]; then
|
|
33
|
-
echo "Token Optimizer — OpenCode runtime detected."
|
|
34
|
-
elif [ -n "${COPILOT_HOME:-}${TOKEN_OPTIMIZER_COPILOT_HOME:-}" ]; then
|
|
35
|
-
echo "Token Optimizer — GitHub Copilot runtime detected."
|
|
36
|
-
elif [ -n "${TOKEN_OPTIMIZER_CURSOR_HOME:-}" ]; then
|
|
37
|
-
echo "Token Optimizer — Cursor runtime detected."
|
|
38
|
-
elif [ -n "${CURSOR_PROJECT_DIR:-}" ] && [ -n "${CURSOR_VERSION:-}" ]; then
|
|
39
|
-
echo "Token Optimizer — Cursor runtime detected."
|
|
40
|
-
elif [ -n "${TOKEN_OPTIMIZER_ANTIGRAVITY_HOME:-}" ]; then
|
|
41
|
-
echo "Token Optimizer — Google Antigravity runtime detected."
|
|
20
|
+
if [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "claude" ] || [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "codex" ]; then :
|
|
21
|
+
elif [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "opencode" ]; then echo "Token Optimizer — OpenCode runtime detected."
|
|
22
|
+
elif [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "copilot" ]; then echo "Token Optimizer — GitHub Copilot runtime detected."
|
|
23
|
+
elif [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "cursor" ]; then echo "Token Optimizer — Cursor runtime detected."
|
|
24
|
+
elif [ "${TOKEN_OPTIMIZER_RUNTIME:-}" = "antigravity" ]; then echo "Token Optimizer — Google Antigravity runtime detected."
|
|
25
|
+
elif [ -n "${CLAUDE_PLUGIN_ROOT:-}${CLAUDE_PLUGIN_DATA:-}" ]; then :
|
|
26
|
+
elif [ -n "${OPENCODE_BIN:-}${OPENCODE_CONFIG_DIR:-}${OPENCODE_DATA_DIR:-}${OPENCODE_CONFIG:-}${OPENCODE_CLIENT:-}" ]; then echo "Token Optimizer — OpenCode runtime detected."
|
|
27
|
+
elif [ -n "${COPILOT_HOME:-}${TOKEN_OPTIMIZER_COPILOT_HOME:-}" ]; then echo "Token Optimizer — GitHub Copilot runtime detected."
|
|
28
|
+
elif [ -n "${TOKEN_OPTIMIZER_CURSOR_HOME:-}" ]; then echo "Token Optimizer — Cursor runtime detected."
|
|
29
|
+
elif [ -n "${CURSOR_PROJECT_DIR:-}" ] && [ -n "${CURSOR_VERSION:-}" ]; then echo "Token Optimizer — Cursor runtime detected."
|
|
30
|
+
elif [ -n "${TOKEN_OPTIMIZER_ANTIGRAVITY_HOME:-}" ]; then echo "Token Optimizer — Google Antigravity runtime detected."
|
|
42
31
|
fi
|
|
43
32
|
```
|
|
44
33
|
|
|
@@ -50,13 +39,12 @@ fi
|
|
|
50
39
|
| "Google Antigravity runtime detected." | Stop. Follow `docs/antigravity.md`. |
|
|
51
40
|
| nothing | Continue to b. |
|
|
52
41
|
|
|
53
|
-
**b. Resolve `$MEASURE_PY`
|
|
42
|
+
**b. Resolve `$MEASURE_PY` and `$PY`.** Each Bash call is a fresh shell, so env vars do not persist between calls: the block ends by echoing both values, and every later command must use the echoed values verbatim. `$MEASURE_PY` is the newest installed measure.py; `$PY` is an interpreter that really runs Python (on Windows the bare `python3` name is often the Store stub, which fails silently under a pipe):
|
|
54
43
|
|
|
55
44
|
```bash
|
|
56
45
|
# Resolve measure.py to the NEWEST installed copy across channels so a stale
|
|
57
|
-
# plugin-cache copy never shadows a fresh install
|
|
58
|
-
# install.sh symlink
|
|
59
|
-
# copy's plugin.json for its version. find (not bare globs) never errors under zsh.
|
|
46
|
+
# plugin-cache copy never shadows a fresh install (find -L follows the
|
|
47
|
+
# install.sh symlink; cd -P resolves it before reading each copy's plugin.json).
|
|
60
48
|
MEASURE_PY=""; _best_ver=""
|
|
61
49
|
while IFS= read -r _cand; do
|
|
62
50
|
[ -f "$_cand" ] || continue
|
|
@@ -70,14 +58,31 @@ done <<EOF
|
|
|
70
58
|
$(find -L "$HOME/.claude/skills" "$HOME/.claude/plugins/cache" "$HOME/.claude/token-optimizer" "$HOME/.codex/skills" "$HOME/.codex/plugins/cache" "$HOME/.config/opencode/plugins" -type f -name measure.py -path '*token-optimizer*/scripts/measure.py' 2>/dev/null)
|
|
71
59
|
EOF
|
|
72
60
|
if [ -z "$MEASURE_PY" ]; then echo "[Error] measure.py not found. Is Token Optimizer installed?"; exit 1; fi
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
**c. The authoritative gate.** The environment check cannot see an OpenCode started without its variables; this one scans the process tree:
|
|
76
61
|
|
|
77
|
-
|
|
78
|
-
python3
|
|
62
|
+
# Resolve an interpreter that really runs Python. On native-Windows Git Bash
|
|
63
|
+
# `python3` is often the Microsoft Store alias stub, which fails silently under
|
|
64
|
+
# a pipe (#301). Probe `python` first there, `python3` first elsewhere; require >=3.9.
|
|
65
|
+
PY=""; _py_candidates="python3 python"
|
|
66
|
+
case "$(uname -s 2>/dev/null)" in MINGW*|MSYS*|CYGWIN*) _py_candidates="python python3" ;; esac
|
|
67
|
+
for _py in $_py_candidates; do
|
|
68
|
+
command -v "$_py" >/dev/null 2>&1 || continue
|
|
69
|
+
_py_ver="$("$_py" -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")' 2>/dev/null)" || _py_ver=""
|
|
70
|
+
_py_major="$(echo "$_py_ver" | cut -d. -f1)"; _py_minor="$(echo "$_py_ver" | cut -d. -f2)"
|
|
71
|
+
[ -n "$_py_major" ] && [ -n "$_py_minor" ] || continue
|
|
72
|
+
case "$_py_major$_py_minor" in *[!0-9]*) continue ;; esac
|
|
73
|
+
if [ "$_py_major" -lt 3 ] 2>/dev/null || { [ "$_py_major" -eq 3 ] && [ "$_py_minor" -lt 9 ]; } 2>/dev/null; then continue; fi
|
|
74
|
+
PY="$_py"; break
|
|
75
|
+
done
|
|
76
|
+
if [ -z "$PY" ]; then echo "[Error] Python 3.9+ not found (the Windows Store python3 stub does not count). Install Python, then retry."; exit 1; fi
|
|
77
|
+
|
|
78
|
+
# Separate Bash calls do not share shell state: copy these into every later command.
|
|
79
|
+
# They print to stderr so a --json command chained in the same call keeps stdout parseable.
|
|
80
|
+
printf 'MEASURE_PY=%q\n' "$MEASURE_PY" >&2
|
|
81
|
+
printf 'PY=%q\n' "$PY" >&2
|
|
79
82
|
```
|
|
80
83
|
|
|
84
|
+
**c. The authoritative gate.** The environment check cannot see an OpenCode started without its variables; this one scans the process tree: `"$PY" "$MEASURE_PY" report 2>&1 | head -1`
|
|
85
|
+
|
|
81
86
|
Any "... runtime detected." line means stop and follow that runtime's file from the table above. On Codex (`TOKEN_OPTIMIZER_RUNTIME=codex` or a Codex environment) follow `references/codex-workflow.md`. Claude Code continues below.
|
|
82
87
|
|
|
83
88
|
## The audit (Claude Code)
|
|
@@ -93,9 +98,9 @@ Work through these in order and tick each off; a skipped phase stays in the list
|
|
|
93
98
|
- [ ] 5. Verify and report before/after references/agent-prompts.md (Verification Agent)
|
|
94
99
|
```
|
|
95
100
|
|
|
96
|
-
**0. Setup.** Context window detection, pre-check, backup, the coordination folder (`COORD_PATH`), hook checks, daemon, smart compaction. Then, only when `
|
|
101
|
+
**0. Setup.** Context window detection, pre-check, backup, the coordination folder (`COORD_PATH`), hook checks, daemon, smart compaction. Then, only when `"$PY" "$MEASURE_PY" keepwarm-consent-status` returns `should_ask: true`, follow `references/keepwarm-consent.md` exactly; it spends the user's money, so its wording and command order are fixed.
|
|
97
102
|
|
|
98
|
-
**1. Parallel audit.** Dispatch six agents in one message, each with `COORD_PATH` and its prompt from `references/agent-prompts.md`. They gather and count, so they run on the cheapest capable model; the synthesis is where judgement is spent.
|
|
103
|
+
**1. Parallel audit.** Dispatch six agents in one message, each with `COORD_PATH` and its prompt from `references/agent-prompts.md`. They gather and count, so they run on the cheapest capable model; the synthesis is where judgement is spent. A missing output file is a gap to note, not a reason to stop.
|
|
99
104
|
|
|
100
105
|
| Agent | Writes | Looks at |
|
|
101
106
|
|-------|--------|----------|
|
|
@@ -106,24 +111,17 @@ Work through these in order and tick each off; a skipped phase stays in the list
|
|
|
106
111
|
| Commands | `audit/commands.md` | Count, menu overhead |
|
|
107
112
|
| Settings and advanced | `audit/advanced.md` | Hooks, rules, settings, @imports, cache lifetime, compaction window |
|
|
108
113
|
|
|
109
|
-
A missing output file is a gap to note, not a reason to stop.
|
|
110
114
|
**2. Synthesis.** One agent on the strongest available model reads every audit file and writes `{COORD_PATH}/analysis/optimization-plan.md`. If it fails, present the raw audit files.
|
|
111
115
|
|
|
112
|
-
**3. Present.** Generate the dashboard and show the findings as the presentation workflow describes, then wait:
|
|
113
|
-
|
|
114
|
-
```bash
|
|
115
|
-
python3 "$MEASURE_PY" dashboard --coord-path "$COORD_PATH"
|
|
116
|
-
```
|
|
116
|
+
**3. Present.** Generate the dashboard and show the findings as the presentation workflow describes, then wait: `"$PY" "$MEASURE_PY" dashboard --coord-path "$COORD_PATH"`
|
|
117
117
|
|
|
118
118
|
**4. Implement.** Only what the user approved, one change at a time, following the playbook (actions 4A-4P: CLAUDE.md, MEMORY.md, skills, file exclusion, MCP, hooks, cache, rules, settings, descriptions, compact instructions, model routing, smart compaction, quality check, version-aware optimizations, smart routing). Templates are in `examples/`. Also offer, with the measured numbers:
|
|
119
119
|
|
|
120
|
-
- `
|
|
121
|
-
- `
|
|
120
|
+
- `"$PY" "$MEASURE_PY" recommendations`: the daily-measured usage recommendations (compact window, subagent cache lifetime), each with the user's own numbers and the exact command when the state is `recommend`. Relay those lines verbatim; Token Optimizer recommends, it never sets either key.
|
|
121
|
+
- `"$PY" "$MEASURE_PY" subagent-cache status` / `compact-advice`: the single-measurement detail behind each item. Both are estimates from the user's own history; the user decides.
|
|
122
122
|
- Repeated work a script could do instead of a model: hand over to `token-coach`, goal e.
|
|
123
123
|
|
|
124
|
-
**5. Verify.** The verification agent re-measures and computes the saving. Show before and after, then the habits that keep it.
|
|
125
|
-
|
|
126
|
-
If a verification number is worse than the baseline, find which change caused it, restore that file from the backup, and return to step 4.
|
|
124
|
+
**5. Verify.** The verification agent re-measures and computes the saving. Show before and after, then the habits that keep it. If a verification number is worse than the baseline, find which change caused it, restore that file from the backup, and return to step 4.
|
|
127
125
|
|
|
128
126
|
## Rules for changing a user's setup
|
|
129
127
|
|