@alexgreensh/token-optimizer 0.0.0-stage → 5.13.50

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 (161) hide show
  1. package/.claude-plugin/marketplace.json +76 -0
  2. package/.claude-plugin/plugin.json +21 -0
  3. package/LICENSE +130 -0
  4. package/LICENSE-SMALL-BUSINESS.md +62 -0
  5. package/PRIVACY.md +200 -0
  6. package/README.md +13 -2
  7. package/hooks/codex-hooks.json +4 -0
  8. package/hooks/hooks.json +182 -0
  9. package/hooks/module_runner.py +149 -0
  10. package/hooks/posttooluse_runner.py +1110 -0
  11. package/hooks/python-launcher.sh +816 -0
  12. package/hooks/run.py +776 -0
  13. package/hooks/sessionstart_runner.py +629 -0
  14. package/hooks/stop_runner.py +408 -0
  15. package/hooks/userpromptsubmit_runner.py +722 -0
  16. package/hooks/windows-launcher.py +163 -0
  17. package/install-cloud.sh +23 -0
  18. package/package.json +29 -4
  19. package/skills/fleet-auditor/SKILL.md +150 -0
  20. package/skills/fleet-auditor/references/fleet-systems.md +167 -0
  21. package/skills/fleet-auditor/references/report-template.md +50 -0
  22. package/skills/fleet-auditor/references/waste-patterns.md +126 -0
  23. package/skills/fleet-auditor/scripts/fleet.py +3612 -0
  24. package/skills/fleet-auditor/scripts/shared.py +277 -0
  25. package/skills/resume-checkpoint/SKILL.md +52 -0
  26. package/skills/resume-checkpoint/scripts/pull_checkpoint.py +188 -0
  27. package/skills/token-coach/SKILL.md +129 -0
  28. package/skills/token-coach/assets/coach-dashboard-tab.html +45 -0
  29. package/skills/token-coach/examples/coaching-session-agentic.md +62 -0
  30. package/skills/token-coach/examples/coaching-session-heavy-setup.md +46 -0
  31. package/skills/token-coach/examples/coaching-session-new-project.md +38 -0
  32. package/skills/token-coach/references/agentic-systems.md +147 -0
  33. package/skills/token-coach/references/coach-patterns.md +120 -0
  34. package/skills/token-coach/references/coaching-scripts.md +146 -0
  35. package/skills/token-coach/references/deterministic-stages.md +102 -0
  36. package/skills/token-coach/references/keepwarm-consent.md +48 -0
  37. package/skills/token-coach/references/quick-reference.md +155 -0
  38. package/skills/token-dashboard/SKILL.md +72 -0
  39. package/skills/token-optimizer/SKILL.md +150 -0
  40. package/skills/token-optimizer/assets/dashboard.html +7251 -0
  41. package/skills/token-optimizer/examples/claude-md-optimized.md +40 -0
  42. package/skills/token-optimizer/examples/hooks-starter.json +179 -0
  43. package/skills/token-optimizer/examples/permissions-deny-template.json +24 -0
  44. package/skills/token-optimizer/pricing/prices.json +695 -0
  45. package/skills/token-optimizer/references/agent-prompts.md +687 -0
  46. package/skills/token-optimizer/references/cli-reference.md +92 -0
  47. package/skills/token-optimizer/references/codex-workflow.md +188 -0
  48. package/skills/token-optimizer/references/cursor-workflow.md +74 -0
  49. package/skills/token-optimizer/references/error-recovery.md +37 -0
  50. package/skills/token-optimizer/references/implementation-playbook.md +416 -0
  51. package/skills/token-optimizer/references/keepwarm-consent.md +48 -0
  52. package/skills/token-optimizer/references/opencode-workflow.md +72 -0
  53. package/skills/token-optimizer/references/optimization-checklist.md +714 -0
  54. package/skills/token-optimizer/references/phase0-setup.md +142 -0
  55. package/skills/token-optimizer/references/presentation-workflow.md +72 -0
  56. package/skills/token-optimizer/references/session-continuity.md +19 -0
  57. package/skills/token-optimizer/references/token-flow-architecture.md +629 -0
  58. package/skills/token-optimizer/scripts/activity_tracker.py +143 -0
  59. package/skills/token-optimizer/scripts/antigravity_doctor.py +548 -0
  60. package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +511 -0
  61. package/skills/token-optimizer/scripts/antigravity_install.py +376 -0
  62. package/skills/token-optimizer/scripts/antigravity_proto.py +413 -0
  63. package/skills/token-optimizer/scripts/antigravity_session.py +346 -0
  64. package/skills/token-optimizer/scripts/antigravity_state.py +486 -0
  65. package/skills/token-optimizer/scripts/archive_result.py +1725 -0
  66. package/skills/token-optimizer/scripts/bash_compress.py +2490 -0
  67. package/skills/token-optimizer/scripts/bash_compress_hook.py +766 -0
  68. package/skills/token-optimizer/scripts/bash_hook.py +61 -0
  69. package/skills/token-optimizer/scripts/bash_whitelist.py +243 -0
  70. package/skills/token-optimizer/scripts/benchmark.py +1765 -0
  71. package/skills/token-optimizer/scripts/build_output_compress.py +776 -0
  72. package/skills/token-optimizer/scripts/cloud_kit.py +991 -0
  73. package/skills/token-optimizer/scripts/codex_command_compress.py +434 -0
  74. package/skills/token-optimizer/scripts/codex_compact_prompt.py +273 -0
  75. package/skills/token-optimizer/scripts/codex_doctor.py +454 -0
  76. package/skills/token-optimizer/scripts/codex_hook_bridge.py +311 -0
  77. package/skills/token-optimizer/scripts/codex_install.py +810 -0
  78. package/skills/token-optimizer/scripts/codex_io.py +119 -0
  79. package/skills/token-optimizer/scripts/codex_log_index.py +219 -0
  80. package/skills/token-optimizer/scripts/codex_models.py +34 -0
  81. package/skills/token-optimizer/scripts/codex_session.py +1186 -0
  82. package/skills/token-optimizer/scripts/codex_state.py +707 -0
  83. package/skills/token-optimizer/scripts/codex_statusline.py +303 -0
  84. package/skills/token-optimizer/scripts/command_filters.py +386 -0
  85. package/skills/token-optimizer/scripts/compression_backfill.py +600 -0
  86. package/skills/token-optimizer/scripts/compression_log.py +230 -0
  87. package/skills/token-optimizer/scripts/context_intel.py +331 -0
  88. package/skills/token-optimizer/scripts/context_pressure.py +115 -0
  89. package/skills/token-optimizer/scripts/copilot_doctor.py +406 -0
  90. package/skills/token-optimizer/scripts/copilot_hook_bridge.py +709 -0
  91. package/skills/token-optimizer/scripts/copilot_install.py +277 -0
  92. package/skills/token-optimizer/scripts/copilot_session.py +496 -0
  93. package/skills/token-optimizer/scripts/copilot_state.py +569 -0
  94. package/skills/token-optimizer/scripts/copilot_vscode.py +832 -0
  95. package/skills/token-optimizer/scripts/cowork_doctor.py +270 -0
  96. package/skills/token-optimizer/scripts/cowork_install.py +380 -0
  97. package/skills/token-optimizer/scripts/credential_patterns.py +1514 -0
  98. package/skills/token-optimizer/scripts/cursor_doctor.py +602 -0
  99. package/skills/token-optimizer/scripts/cursor_hook_bridge.py +843 -0
  100. package/skills/token-optimizer/scripts/cursor_install.py +423 -0
  101. package/skills/token-optimizer/scripts/cursor_session.py +303 -0
  102. package/skills/token-optimizer/scripts/cursor_state.py +348 -0
  103. package/skills/token-optimizer/scripts/delta_diff.py +110 -0
  104. package/skills/token-optimizer/scripts/detectors/__init__.py +2 -0
  105. package/skills/token-optimizer/scripts/detectors/bad_decomposition.py +78 -0
  106. package/skills/token-optimizer/scripts/detectors/cache_instability.py +504 -0
  107. package/skills/token-optimizer/scripts/detectors/looping.py +88 -0
  108. package/skills/token-optimizer/scripts/detectors/output_waste.py +143 -0
  109. package/skills/token-optimizer/scripts/detectors/overpowered.py +116 -0
  110. package/skills/token-optimizer/scripts/detectors/pdf_ingestion.py +52 -0
  111. package/skills/token-optimizer/scripts/detectors/registry.py +65 -0
  112. package/skills/token-optimizer/scripts/detectors/respond_to_bash.py +76 -0
  113. package/skills/token-optimizer/scripts/detectors/retry_churn.py +83 -0
  114. package/skills/token-optimizer/scripts/detectors/tool_cascade.py +62 -0
  115. package/skills/token-optimizer/scripts/detectors/wasteful_thinking.py +75 -0
  116. package/skills/token-optimizer/scripts/detectors/weak_model.py +55 -0
  117. package/skills/token-optimizer/scripts/detectors/websearch_routing.py +72 -0
  118. package/skills/token-optimizer/scripts/deterministic_candidates.py +1458 -0
  119. package/skills/token-optimizer/scripts/devin_compress.py +790 -0
  120. package/skills/token-optimizer/scripts/devin_doctor.py +403 -0
  121. package/skills/token-optimizer/scripts/devin_hooks.py +453 -0
  122. package/skills/token-optimizer/scripts/devin_install.py +1086 -0
  123. package/skills/token-optimizer/scripts/devin_source.py +119 -0
  124. package/skills/token-optimizer/scripts/devin_state.py +330 -0
  125. package/skills/token-optimizer/scripts/devin_verify.py +67 -0
  126. package/skills/token-optimizer/scripts/grok_doctor.py +560 -0
  127. package/skills/token-optimizer/scripts/grok_hook_bridge.py +699 -0
  128. package/skills/token-optimizer/scripts/grok_install.py +397 -0
  129. package/skills/token-optimizer/scripts/grok_session.py +317 -0
  130. package/skills/token-optimizer/scripts/grok_state.py +299 -0
  131. package/skills/token-optimizer/scripts/hermes_doctor.py +479 -0
  132. package/skills/token-optimizer/scripts/hermes_hook_bridge.py +285 -0
  133. package/skills/token-optimizer/scripts/hermes_install.py +668 -0
  134. package/skills/token-optimizer/scripts/hermes_session.py +691 -0
  135. package/skills/token-optimizer/scripts/hermes_state.py +448 -0
  136. package/skills/token-optimizer/scripts/hook_io.py +120 -0
  137. package/skills/token-optimizer/scripts/hook_runtime.py +713 -0
  138. package/skills/token-optimizer/scripts/injection.py +156 -0
  139. package/skills/token-optimizer/scripts/install_reconcile.py +952 -0
  140. package/skills/token-optimizer/scripts/measure.py +58739 -0
  141. package/skills/token-optimizer/scripts/outline.py +53 -0
  142. package/skills/token-optimizer/scripts/pipeline_analyzer.py +723 -0
  143. package/skills/token-optimizer/scripts/plugin_env.py +562 -0
  144. package/skills/token-optimizer/scripts/py_trust.py +80 -0
  145. package/skills/token-optimizer/scripts/quality_cache_gate.py +320 -0
  146. package/skills/token-optimizer/scripts/read_cache.py +2489 -0
  147. package/skills/token-optimizer/scripts/recovery_output.py +110 -0
  148. package/skills/token-optimizer/scripts/refetch_fingerprint.py +236 -0
  149. package/skills/token-optimizer/scripts/refetch_guard.py +244 -0
  150. package/skills/token-optimizer/scripts/routing_advisor.py +389 -0
  151. package/skills/token-optimizer/scripts/runtime_env.py +1893 -0
  152. package/skills/token-optimizer/scripts/session_store.py +979 -0
  153. package/skills/token-optimizer/scripts/spawn_utils.py +122 -0
  154. package/skills/token-optimizer/scripts/statusline.js +660 -0
  155. package/skills/token-optimizer/scripts/structure_map.py +2880 -0
  156. package/skills/token-optimizer/scripts/structure_map_ts.py +676 -0
  157. package/skills/token-optimizer/scripts/structure_replay.py +1556 -0
  158. package/skills/token-optimizer/scripts/thrash_guard.py +604 -0
  159. package/skills/token-optimizer/scripts/token_estimate.py +80 -0
  160. package/skills/token-optimizer/scripts/tool_search_diagnostic.py +61 -0
  161. package/skills/token-optimizer/scripts/utf8_io.py +219 -0
@@ -0,0 +1,142 @@
1
+ # Phase 0: Setup Details
2
+
3
+ ## Contents
4
+
5
+ - [Context Window Detection](#context-window-detection)
6
+ - [Quick Pre-Check](#quick-pre-check)
7
+ - [Backup](#backup)
8
+ - [Coordination Folder](#coordination-folder)
9
+ - [SessionEnd Hook Check](#sessionend-hook-check)
10
+ - [Dashboard Daemon](#dashboard-daemon)
11
+ - [Smart Compaction Check](#smart-compaction-check)
12
+
13
+ ---
14
+
15
+ ## Context Window Detection
16
+
17
+ Check if `TOKEN_OPTIMIZER_CONTEXT_SIZE` env var is already set. If not:
18
+ - Most current models have a 1M window by default (Haiku 5.5, Sonnet 5 and later, Opus 4.7 and later, Fable). Sonnet 4.6 and Opus 4.6 have 1M only through the `[1m]` variant and 200K without it.
19
+ - Let `measure.py` detect the window from the session's model. Only if the user says it is wrong, ask which window they run and `export TOKEN_OPTIMIZER_CONTEXT_SIZE=<tokens>` (for example `1000000`) for this session.
20
+ Keep this quick, one question max.
21
+
22
+ ## Quick Pre-Check
23
+
24
+ Run `python3 $MEASURE_PY report`.
25
+ If estimated controllable tokens < 1,000 and no CLAUDE.md exists, short-circuit:
26
+ ```
27
+ [Token Optimizer] Your setup is already minimal (~X tokens overhead).
28
+ Focus on behavioral changes instead: /compact at natural breakpoints, /clear between topics,
29
+ route data-gathering agents to a lighter model, batch requests.
30
+ ```
31
+
32
+ ## Backup
33
+
34
+ ```bash
35
+ BACKUP_DIR="$HOME/.claude/_backups/token-optimizer-$(date +%Y%m%d-%H%M%S)"
36
+ mkdir -p "$BACKUP_DIR"
37
+ chmod 700 "$BACKUP_DIR"
38
+ cp ~/.claude/CLAUDE.md "$BACKUP_DIR/" 2>/dev/null || true
39
+ cp ~/.claude/settings.json "$BACKUP_DIR/" 2>/dev/null || true
40
+ cp -r ~/.claude/commands "$BACKUP_DIR/" 2>/dev/null || true
41
+ for memfile in ~/.claude/projects/*/memory/MEMORY.md; do
42
+ if [ -f "$memfile" ]; then
43
+ projname=$(basename "$(dirname "$(dirname "$memfile")")")
44
+ cp "$memfile" "$BACKUP_DIR/MEMORY-${projname}.md" 2>/dev/null || true
45
+ fi
46
+ done
47
+
48
+ if [ -z "$(ls -A "$BACKUP_DIR" 2>/dev/null)" ]; then
49
+ echo "[Warning] Backup directory is empty. No files were backed up."
50
+ fi
51
+ ```
52
+
53
+ ## Coordination Folder
54
+
55
+ ```bash
56
+ # Project-local, so the audit files live with the work. Keep it out of git:
57
+ # echo '.token-optimizer-audit-*/' >> .git/info/exclude
58
+ COORD_PATH=$(mktemp -d "$PWD/.token-optimizer-audit-XXXXXXXXXX")
59
+ [ -d "$COORD_PATH" ] || { echo "[Error] Failed to create coordination folder."; exit 1; }
60
+ mkdir -p "$COORD_PATH"/{audit,analysis,plan,verification}
61
+ ```
62
+
63
+ ## SessionEnd Hook Check
64
+
65
+ ```bash
66
+ python3 $MEASURE_PY check-hook
67
+ ```
68
+ - Exit 0: already installed, skip to Phase 1.
69
+ - Exit 1 (manual/script install users only): explain and offer to install:
70
+
71
+ ```
72
+ [Token Optimizer] Want to track your token usage over time?
73
+
74
+ Right now, the optimizer can audit your setup. But to track *trends* (which
75
+ skills you actually use, how your context fills up day to day, model costs),
76
+ it needs to save a small log after each Claude Code session.
77
+
78
+ What this does:
79
+ - When you close a Claude Code session, it automatically saves usage stats
80
+ - Takes ~2 seconds, runs silently in the background, then stops
81
+ - All data stays on your machine (stored in ~/.claude/_backups/token-optimizer/)
82
+ - Powers the Trends and Health tabs in your dashboard
83
+
84
+ Remove anytime: python3 measure.py setup-hook --uninstall
85
+ ```
86
+
87
+ Ask user: 1) Install (dry-run first), 2) Show JSON first, 3) Skip
88
+
89
+ ## Dashboard Daemon
90
+
91
+ Run BOTH probes in one pass:
92
+ ```bash
93
+ python3 "$MEASURE_PY" daemon-status
94
+ python3 "$MEASURE_PY" daemon-consent --get
95
+ ```
96
+
97
+ `daemon-status` prints `DAEMON_RUNNING`, `DAEMON_FOREIGN`, or `DAEMON_NOT_RUNNING`.
98
+ `daemon-consent --get` prints JSON: `{}` (never prompted) or `{"prompted": true, "consent": true|false}`.
99
+
100
+ | Daemon \ Consent | unrecorded | `consent: true` | `consent: false` |
101
+ |---|---|---|---|
102
+ | `DAEMON_RUNNING` | skip; lead with URL next time | skip; URL works | offer `setup-daemon --uninstall` once |
103
+ | `DAEMON_FOREIGN` | prompt with port conflict warning | note conflict | skip silently |
104
+ | `DAEMON_NOT_RUNNING` | first-time install prompt | offer to reinstall | skip silently |
105
+
106
+ First-time install prompt:
107
+ ```
108
+ [Token Optimizer] Want a bookmarkable dashboard URL?
109
+
110
+ After you run `setup-daemon`, the dashboard is served at:
111
+ URL: http://localhost:24842/token-optimizer
112
+ Until then, open the file path `measure.py dashboard` prints on its ` Dashboard: ` line. The location is install-dependent, so read it from the output rather than assuming a literal path.
113
+
114
+ What installing the URL does:
115
+ - Runs a tiny web server on your machine (~2MB memory)
116
+ - Starts automatically at login, restarts if it ever stops
117
+ - Only reachable from this machine (localhost)
118
+
119
+ Remove anytime: python3 measure.py setup-daemon --uninstall
120
+ ```
121
+
122
+ Ask user: 1) Install (write consent first, then install), 2) Skip (set consent no)
123
+ On Linux/BSD: skip silently, mention `file://` URL works.
124
+
125
+ ## Smart Compaction Check
126
+
127
+ ```bash
128
+ python3 $MEASURE_PY setup-smart-compact --status
129
+ ```
130
+ - All 4 hooks installed: skip entirely.
131
+ - Partially or not installed: explain and offer:
132
+
133
+ ```
134
+ [Token Optimizer] Smart Compaction captures session state BEFORE compaction fires,
135
+ then restores it afterward. Decisions, modified files, errors, agent state.
136
+
137
+ Remove anytime: python3 measure.py setup-smart-compact --uninstall
138
+ ```
139
+
140
+ Ask user: 1) Install (dry-run first), 2) Show JSON first, 3) Skip
141
+
142
+ Output: `[Token Optimizer Initialized] Backup: $BACKUP_DIR | Coordination: $COORD_PATH`
@@ -0,0 +1,72 @@
1
+ # Phase 3: Presentation Workflow
2
+
3
+ ## Findings Template
4
+
5
+ Read the optimization plan and present. For MODEL ROUTING, also read `{COORD_PATH}/audit/advanced.md` for "Has routing instructions" and "Usage Pattern" data.
6
+
7
+ ```
8
+ [Token Optimizer Results]
9
+
10
+ CURRENT STATE
11
+ Your per-message overhead: ~X tokens
12
+ Context used before first message: ~X%
13
+
14
+ QUICK WINS (do these today)
15
+ - [Action 1]: Save ~X tokens/msg (~Y%)
16
+ - [Action 2]: Save ~X tokens/msg (~Y%)
17
+
18
+ MODEL ROUTING
19
+ [Has instructions: Yes/No] | [Token distribution: X% Opus, Y% Sonnet, Z% Haiku or "Not measured yet"]
20
+
21
+ FULL OPTIMIZATION POTENTIAL
22
+ If all implemented: ~X tokens/msg saved (~Y% reduction)
23
+
24
+ Ready to implement? I can:
25
+ 1. Auto-fix safe changes (consolidate CLAUDE.md, archive skills)
26
+ 2. Generate permissions.deny rules (if missing)
27
+ 3. Create optimized CLAUDE.md template
28
+ 4. Show MCP servers to consider disabling
29
+
30
+ Some optimizations have side effects:
31
+ - Deny rules block file access for ALL tools (may break MCP servers that read databases). Keep them narrow and only for paths Claude won't read anyway: a broad rule on an actively-read path makes Claude repeatedly hit "permission denied," and that feedback accumulates in context, costing tokens instead of saving them.
32
+ - Archiving skills breaks anything that @imports them
33
+ - Disabling MCP servers breaks skills that use their tools
34
+ I'll check for dependencies and warn you before each change.
35
+
36
+ What should we tackle first?
37
+ ```
38
+
39
+ ## Dashboard Generation
40
+
41
+ ```bash
42
+ python3 $MEASURE_PY dashboard --coord-path $COORD_PATH
43
+ ```
44
+
45
+ Tell the user: "Dashboard opened in your browser. Browse findings by category, check the optimizations you want, click Copy Prompt and paste back here. Or just tell me directly what to tackle."
46
+
47
+ ## Dashboard URL Presentation
48
+
49
+ Check daemon status first:
50
+ ```bash
51
+ python3 "$MEASURE_PY" daemon-status 2>/dev/null || echo "DAEMON_NOT_RUNNING"
52
+ ```
53
+
54
+ If DAEMON_RUNNING:
55
+ ```
56
+ Your persistent dashboard (auto-updated every session):
57
+ URL: http://localhost:24842/token-optimizer
58
+ File: <read from `measure.py dashboard` output, line beginning ` Dashboard: `>
59
+ ```
60
+
61
+ If DAEMON_NOT_RUNNING:
62
+ ```
63
+ Your persistent dashboard (auto-updated every session):
64
+ File: <read from `measure.py dashboard` output, line beginning ` Dashboard: `>
65
+ ```
66
+ Never hardcode the file path. It is install-dependent, so cite the path the command actually printed.
67
+ On macOS only, suggest: "Want a bookmarkable URL? Run: `python3 $MEASURE_PY setup-daemon`"
68
+ Do NOT mention `localhost:24842` if daemon is not running.
69
+
70
+ For headless/remote: user can run `dashboard --serve` separately. Never use `--serve` from within the orchestrator (it blocks with `serve_forever`).
71
+
72
+ **Wait for user decision before proceeding.**
@@ -0,0 +1,19 @@
1
+ # Session continuity: cold-resume-lean
2
+
3
+ Reopen a forgotten/cold session cheaply, no `--resume`, no command. On a fresh
4
+ session, when the user naturally asks to continue prior work ("continue the X
5
+ work, check what we discussed last session"), the continuity hook reconstructs a
6
+ **lean** context for the right **same-project** prior session and injects it.
7
+
8
+ - **Selection** ("both"): if the user names a topic → keyword-match winner; if
9
+ vague ("where we left off") → most-recent same-project session.
10
+ - **Token-free**: reconstruction reads checkpoints + `session_log` only (no LLM,
11
+ no subprocess). The only cost is the fresh session's normal first turn.
12
+ - **Same-project = files touched** (path-prefix vs cwd), never a cross-project leak.
13
+ - **Savings** are credited as a realized `resume_lean` event (avoided cold-resume
14
+ cache-rewrite minus the lean block), idempotent per target session, shown in the
15
+ Savings view. Realized tier, same as `checkpoint_restore`.
16
+ - **Manual fallback**: `python3 $MEASURE_PY resume-lean` lists cold sessions;
17
+ `resume-lean <#|session_id> --print` emits the block for `claude "$(...)"`.
18
+ - Ported across Claude Code, Codex, OpenClaw, opencode (checkpoint richness varies
19
+ by platform; the lean block adapts to available fields).