Skip to content

Commit 317f074

Browse files
committed
feat: add OpenCode plugin with full Claude Code parity (v5.7.0)
Native TypeScript plugin for OpenCode (163K stars) delivering every Token Optimizer capability: 7-signal dual-score quality engine, smart compaction with mode-aware context injection, session continuity via checkpoint restore, quality nudges, loop detection, fill warnings, tool call fatigue detection, and a built-in dashboard. 21 source files, 2537 LOC. Ships as token-optimizer-opencode on npm. 12-agent torture room gauntlet: security, correctness, parity, reliability, performance, architecture, TypeScript, maintainability, testing, dashboard, and repo-forensics.
1 parent da6522e commit 317f074

30 files changed

Lines changed: 2871 additions & 8 deletions

.claude-plugin/marketplace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"name": "token-optimizer",
1414
"source": "./",
1515
"description": "Audit, fix, and monitor Claude Code context window usage. Find the ghost tokens.",
16-
"version": "5.6.13",
16+
"version": "5.7.0",
1717
"author": {
1818
"name": "Alex Greenshpun",
1919
"url": "https://linkedin.com/in/alexgreensh"

.claude-plugin/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"homepage": "https://github.com/alexgreensh/token-optimizer",
99
"repository": "https://github.com/alexgreensh/token-optimizer",
10-
"version": "5.6.13",
10+
"version": "5.7.0",
1111
"license": "PolyForm-Noncommercial-1.0.0",
1212
"keywords": ["token", "optimization", "context", "audit", "cost", "coach"]
1313
}

.codex-plugin/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "token-optimizer",
3-
"version": "5.6.13",
3+
"version": "5.7.0",
44
"description": "Audit, monitor, and reduce Codex context waste with continuity helpers and explicit outline tools.",
55
"skills": "./skills/",
66
"interface": {

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
</p>
44

55
<p align="center">
6-
<a href="https://github.com/alexgreensh/token-optimizer/releases"><img src="https://img.shields.io/badge/version-5.6.13-green" alt="Version 5.6.13"></a>
6+
<a href="https://github.com/alexgreensh/token-optimizer/releases"><img src="https://img.shields.io/badge/version-5.7.0-green" alt="Version 5.6.13"></a>
77
<a href="https://github.com/alexgreensh/token-optimizer/releases"><img src="https://img.shields.io/github/release-date/alexgreensh/token-optimizer?label=last%20release&color=blue" alt="Last Release"></a>
88
<a href="https://github.com/alexgreensh/token-optimizer"><img src="https://img.shields.io/badge/Claude_Code-Plugin-blueviolet" alt="Claude Code Plugin"></a>
99
<a href="https://github.com/alexgreensh/token-optimizer/tree/main/openclaw"><img src="https://img.shields.io/badge/OpenClaw-v2.4.1-brightgreen" alt="OpenClaw v2.4.1"></a>
10+
<a href="https://github.com/alexgreensh/token-optimizer/tree/main/opencode"><img src="https://img.shields.io/badge/OpenCode-v1.0.0-58a6ff" alt="OpenCode v1.0.0"></a>
1011
<a href="https://github.com/alexgreensh/token-optimizer/blob/main/docs/codex-beta.md"><img src="https://img.shields.io/badge/Codex-v0.1.0--beta-orange" alt="Codex v0.1.0-beta"></a>
1112
</p>
1213
<p align="center">
@@ -42,7 +43,7 @@ They compress command output, which covers 15-25% of your context on a good day.
4243
Token Optimizer covers all of it, keeps your work alive across compactions, measures whether the optimization actually helped, and gives you a <strong>live dashboard</strong> that shows every token, every dollar, and every turn, auto-updated after every session. Runs fully local. Zero context tokens used. Zero runtime dependencies.
4344
</p>
4445
<p align="center">
45-
Works on <strong>Claude Code</strong>, <strong>OpenClaw</strong>, and <strong>Codex</strong> (beta) today. Windsurf, Cursor, and more on the way.
46+
Works on <strong>Claude Code</strong>, <strong>OpenCode</strong>, <strong>OpenClaw</strong>, and <strong>Codex</strong> (beta) today. Windsurf, Cursor, and more on the way.
4647
</p>
4748

4849
<p align="center">
@@ -93,7 +94,7 @@ git clone https://github.com/alexgreensh/token-optimizer.git ~/.claude/token-opt
9394
bash ~/.claude/token-optimizer/install.sh
9495
```
9596

96-
Works on Claude Code and [OpenClaw](#openclaw-plugin). Each platform has its own native plugin (Python for Claude Code, TypeScript for OpenClaw). No bridging, no shared runtime, zero cross-platform dependencies.
97+
Works on Claude Code, [OpenCode](#opencode), and [OpenClaw](#openclaw). Each platform has its own native plugin (Python for Claude Code, TypeScript for OpenCode and OpenClaw). No bridging, no shared runtime, zero cross-platform dependencies.
9798

9899
</details>
99100

@@ -123,6 +124,33 @@ See [`docs/codex-beta.md`](docs/codex-beta.md) for the full feature parity table
123124

124125
</details>
125126

127+
<details>
128+
<summary><h3>OpenCode</h3></summary>
129+
130+
Native TypeScript plugin for [OpenCode](https://github.com/anomalyco/opencode) with full Claude Code feature parity. 7-signal dual-score quality engine, smart compaction with mode-aware context injection, session continuity, quality nudges, loop detection, and a built-in dashboard.
131+
132+
```bash
133+
opencode plugin add token-optimizer-opencode
134+
```
135+
136+
Or add to `.opencode/opencode.jsonc`:
137+
138+
```jsonc
139+
{
140+
"plugin": ["token-optimizer-opencode"]
141+
}
142+
```
143+
144+
Two custom tools are available inside OpenCode:
145+
- `token_status` for on-demand quality reports
146+
- `token_dashboard` to generate and open the visual dashboard
147+
148+
Works with every model OpenCode supports: Anthropic, OpenAI, Google, DeepSeek, Qwen, Mistral, xAI, and local models. MRCR quality curves are calibrated per model family.
149+
150+
See [`opencode/README.md`](opencode/README.md) for full docs, configuration options, and environment variable overrides.
151+
152+
</details>
153+
126154
<details>
127155
<summary><h3>OpenClaw</h3></summary>
128156

@@ -192,7 +220,7 @@ Token Optimizer handles both. And because it also checkpoints your session befor
192220

193221
### Fully local, zero dependencies, zero telemetry
194222

195-
Pure Python stdlib on Claude Code and Codex. Pure Node stdlib on OpenClaw. Nothing to `pip install`, nothing to `npm install` at runtime, no analytics endpoint, no phone-home. Every measurement is a local SQLite write to a file you own under your runtime home, such as `~/.claude/_backups/token-optimizer/trends.db` or `~/.codex/_backups/token-optimizer/trends.db`. You can inspect it, export it, or delete it.
223+
Pure Python stdlib on Claude Code and Codex. TypeScript with zero runtime deps on OpenCode and OpenClaw. Nothing to `pip install`, no analytics endpoint, no phone-home. Every measurement is a local SQLite write to a file you own under your runtime home. You can inspect it, export it, or delete it.
196224

197225
### Zero context tokens consumed
198226

@@ -254,7 +282,7 @@ No. All hooks are non-blocking with fail-open design. If a Token Optimizer scrip
254282
<details>
255283
<summary>📦 <strong>Does it have any runtime dependencies?</strong></summary>
256284

257-
No. Pure Python stdlib on Claude Code and Codex. Pure Node stdlib on OpenClaw. Nothing to `pip install`, nothing to `npm install` at runtime. What you clone is everything it needs.
285+
No. Pure Python stdlib on Claude Code and Codex. TypeScript with zero runtime deps on OpenCode and OpenClaw. What you clone (or `opencode plugin add`) is everything it needs.
258286
</details>
259287

260288
<details>

opencode/.forensicsignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Bun lockfile contains SHA-512 integrity hashes (not payloads)
2+
bun.lock
3+
4+
# bun:sqlite is a Bun built-in module, not an npm phantom dependency
5+
# (equivalent to node:fs - part of the runtime)

opencode/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Token Optimizer for OpenCode
2+
3+
Context quality scoring, smart compaction, and session continuity for [OpenCode](https://github.com/anomalyco/opencode). Full parity with the Claude Code Token Optimizer plugin.
4+
5+
## What It Does
6+
7+
Token Optimizer monitors your OpenCode sessions and helps you get the most out of your context window:
8+
9+
- **7-signal quality scoring** with dual ResourceHealth (monotonic) + SessionEfficiency (rolling window) architecture
10+
- **Smart compaction** with mode-aware PRESERVE/DROP guidance (code/debug/review/infra/general)
11+
- **Session continuity** that restores context from prior sessions via keyword matching
12+
- **Quality nudges** that warn when context health drops, fill exceeds thresholds, or retry loops are detected
13+
- **Dashboard** with quality trends, session history, and daily aggregates
14+
- **`token_status` tool** for on-demand quality reports
15+
- **`token_dashboard` tool** to generate and open the visual dashboard
16+
17+
## Install
18+
19+
```bash
20+
opencode plugin add token-optimizer-opencode
21+
```
22+
23+
## Configure
24+
25+
Add plugin options in `.opencode/opencode.jsonc`:
26+
27+
```jsonc
28+
{
29+
"plugin": [
30+
["token-optimizer-opencode", {
31+
"qualityWindow": 20,
32+
"features": {
33+
"qualityNudges": true,
34+
"loopDetection": true,
35+
"smartCompaction": true,
36+
"continuity": true,
37+
"activityTracking": true,
38+
"trends": true
39+
}
40+
}]
41+
]
42+
}
43+
```
44+
45+
All options are optional. Defaults are shown above.
46+
47+
## Environment Variables
48+
49+
Override any threshold via environment variables:
50+
51+
| Variable | Default | Description |
52+
|----------|---------|-------------|
53+
| `TOKEN_OPTIMIZER_QUALITY_WINDOW` | 20 | Rolling window size for ratio signals |
54+
| `TOKEN_OPTIMIZER_TOOL_CALL_WARN` | auto | Tool call warning threshold (scales with context window) |
55+
| `TOKEN_OPTIMIZER_TOOL_CALL_CRITICAL` | auto | Tool call critical threshold |
56+
| `TOKEN_OPTIMIZER_CHECKPOINT_RETENTION_DAYS` | 7 | Days to keep checkpoints |
57+
| `TOKEN_OPTIMIZER_CHECKPOINT_RETENTION_MAX` | 50 | Max checkpoints to scan for restore |
58+
| `TOKEN_OPTIMIZER_RELEVANCE_THRESHOLD` | 0.3 | Min relevance score for checkpoint restore |
59+
| `TOKEN_OPTIMIZER_NUDGES` | true | Enable quality nudges |
60+
| `TOKEN_OPTIMIZER_LOOP_DETECTION` | true | Enable retry loop detection |
61+
| `TOKEN_OPTIMIZER_SMART_COMPACTION` | true | Enable compaction context injection |
62+
| `TOKEN_OPTIMIZER_CONTINUITY` | true | Enable session continuity |
63+
| `TOKEN_OPTIMIZER_ACTIVITY` | true | Enable activity tracking |
64+
| `TOKEN_OPTIMIZER_TRENDS` | true | Enable trends collection |
65+
66+
## Quality Scoring
67+
68+
The quality score uses a dual-composite architecture:
69+
70+
**ResourceHealth** (monotonic, can only decrease within a session):
71+
- Context fill degradation (50%) - MRCR-curve-based quality estimate
72+
- Compaction depth (30%) - information loss from repeated compaction
73+
- Absolute waste tokens (20%) - stale reads + bloated results
74+
75+
**SessionEfficiency** (rolling window, can rise or fall):
76+
- Stale reads (30%) - re-reading files after writing them
77+
- Bloated results (30%) - large tool outputs never referenced
78+
- Decision density (20%) - ratio of substantive messages
79+
- Agent efficiency (20%) - agent dispatch result/prompt ratio
80+
81+
Grades: S (90+), A (80+), B (70+), C (55+), D (40+), F (<40)
82+
83+
## Hooks Used
84+
85+
| Hook | Purpose |
86+
|------|---------|
87+
| `chat.message` | Track user messages, trigger quality scoring |
88+
| `tool.execute.before` | Record file reads |
89+
| `tool.execute.after` | Record tool results, file writes, agent dispatches, activity tracking |
90+
| `experimental.chat.system.transform` | Inject warnings, restore session continuity |
91+
| `experimental.session.compacting` | Inject mode-aware compaction guidance, capture checkpoint |
92+
| `experimental.compaction.autocontinue` | Reset signals post-compaction, refresh quality |
93+
| `event` | Handle session lifecycle (created/deleted) |
94+
95+
## Model Support
96+
97+
Context window sizes are mapped for 30+ models across all major providers:
98+
Anthropic (Opus/Sonnet 1M, Haiku 200K), OpenAI (GPT-5.x, GPT-4.1, o3/o4), Google (Gemini 2.x/3.x),
99+
DeepSeek, Qwen, Mistral, xAI Grok, and more.
100+
101+
MRCR quality curves are calibrated per model family for accurate fill-degradation estimates.
102+
103+
## License
104+
105+
PolyForm Noncommercial 1.0.0

opencode/bun.lock

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)