@esuyo/esuyo-opencode-compaction
v1.0.0
Published
Esuyo Opencode Compaction: OpenCode plugin that compacts a session at a fixed 256k context reference (15% reserve) with a per-session on/off command.
Maintainers
Readme
Esuyo Opencode Compaction
An OpenCode V2 plugin that compacts a session before it reaches the model's
context limit, with a config file in ~/.config/opencode and a per-session
slash command to turn it off.
The default is fixed: a 256k reference window minus 15%, so compaction
starts at 217,600 tokens (85% full) on every model, regardless of the
context window the provider reports. Set contextWindow to null if you want
to follow each model's reported limit instead.
OpenCode already has automatic compaction (compaction.buffer, default 15%).
This plugin makes the trigger explicit and adds a session-local on/off switch.
See Relationship to built-in compaction.
Install
From npm
opencode plugin add @esuyo/esuyo-opencode-compactionThe package exposes both plugin surfaces (oc-plugin: server and tui), so
the installer registers the server part in the OpenCode config and the TUI part
in the terminal config.
From a local checkout
git clone https://github.com/8perezm/esuyo-opencode-compaction.git
cd esuyo-opencode-compaction
npm ciThen add the checkout's absolute path to the plugins array in
~/.config/opencode/opencode.jsonc:
{
"$schema": "https://opencode.ai/config.json",
"plugins": ["C:/SERVER/REPOS/Esuyo/esuyo-opencode-compaction"]
}OpenCode reloads plugins when the config changes, so no restart is normally needed.
Config file
Copy esuyo-opencode-compaction.example.json to:
~/.config/opencode/esuyo-opencode-compaction.jsonThe plugin uses built-in defaults when the file is missing, so this step is
optional. Any file named by $XDG_CONFIG_HOME/opencode is also supported.
Commands
| Command | Behavior |
| --- | --- |
| /compaction | Show status, current estimate, and the trigger |
| /compaction on | Enable Compaction for this session |
| /compaction off | Disable Compaction for this session |
| /compaction toggle | Switch this session |
| /compaction reset | Remove the session override and use the global default |
| /compaction reload | Reread the config file |
| /compaction help | Show command help |
Compaction is on by default. Session overrides are stored in OpenCode's plugin storage, so they survive a server restart until you reset them.
Feedback is shown as a TUI toast. It does not add a message to the conversation, so checking status or turning it off costs no context.
Configuration
~/.config/opencode/esuyo-opencode-compaction.json:
{
"enabled": true,
"reservePercent": 15,
"reserveTokens": null,
"triggerTokens": null,
"contextWindow": 256000,
"fallbackContextWindow": 256000,
"debounceMs": 2000,
"notify": false,
"debug": false
}| Field | Default | Meaning |
| --- | ---: | --- |
| enabled | true | Default for sessions without an override |
| reservePercent | 15 | Share of the window kept free. 15 = compact at 85% full. A string like "15%" also works |
| reserveTokens | null | Absolute reserve. Overrides reservePercent |
| triggerTokens | null | Exact trigger size, capped to the window. Overrides the window/reserve calculation |
| contextWindow | 256000 | Fixed reference window for every model. Set null to use the model's reported limit |
| fallbackContextWindow | 256000 | Window used when the model reports none |
| debounceMs | 2000 | Minimum gap between context checks for one session |
| notify | false | Show a toast when Compaction requests a compaction |
| debug | false | Log measurements to the OpenCode server log |
How the trigger is calculated
- Use
triggerTokenswhen it is set. - Otherwise, use
contextWindowif set. It defaults to 256000, so every model uses the same trigger. Set it tonullto use the model's reportedmin(input, context)limit, orfallbackContextWindowwhen the model reports none. - Reserve
reserveTokenswhen set, elsewindow × reservePercent / 100. - Compact when the estimated prompt reaches
window - reserve.
With the defaults, on every model:
window 256,000
reserve 38,400 (15%)
trigger 217,600 (85% full)Edits to the config file are picked up automatically through its modification time. A broken file keeps the last good values instead of turning Compaction off.
How it measures context
It uses the same idea as OpenCode's built-in compaction:
- The latest assistant response's provider-reported prompt tokens are exact:
input + cache.read + cache.write + output + reasoning. - Everything after that response is estimated from text at four characters per token. Attachments are sized by kind (image/PDF/text) rather than by their base64 payload.
- Before the first measured response the plugin waits instead of guessing the whole system prompt and tool schema.
This is deliberately conservative. A 15% reserve leaves room for the estimate to be a little low and for a large turn to overshoot before the next check.
Compaction checks on step boundaries, on idle, and after a model switch. Checks are debounced, and a session never has two plugin-initiated compactions pending. After a failed compaction it backs off (one minute, then longer) instead of retrying in a loop.
Relationship to built-in compaction
OpenCode's built-in automatic compaction is still controlled by compaction in
opencode.json(c):
{
"compaction": {
"auto": true,
"buffer": "15%"
}
}The built-in default is 15%, which starts compaction at 85% full. Compaction uses the same 15% reserve, so both fire at the same point. That is safe — either one compacts the session in time — but it means Compaction is not earlier than the built-in trigger. To make Compaction fire first, lower the built-in buffer (a smaller buffer means a later built-in trigger), for example:
{
"compaction": {
"auto": true,
"buffer": "5%"
}
}Then the plugin compacts at 85% full and the built-in one remains a safety net at 95% full:
- Compaction on: compaction at 85% of the 256k reference window.
/compaction off: the built-in compaction still protects the session.- Built-in compaction also recovers once if a provider rejects an oversized request.
If you want Compaction to be the only automatic compaction, turn the built-in one off:
{
"compaction": {
"auto": false
}
}Manual /compact and plugin-requested compaction still work with auto:
false, but the provider-overflow recovery is lost. In that setup,
/compaction off means no automatic compaction at all for that session.
Compaction cannot change the built-in threshold per session.
/compaction offonly disables this plugin's trigger.
Notes and limitations
- The session's selected model supplies the window. An agent that overrides the model may have a different real limit.
- Subagent sessions are covered too.
compactionis lossy. Increase OpenCode'scompaction.keep.tokensif exact recent details matter.- Compaction cannot create room when most of a request is fixed instructions and tool schemas.
- If the selected model reports no context window, the 256k fallback is used.
Set
contextWindowortriggerTokensto be explicit. - Status and toggle feedback is delivered through an RPC event and shown as a
TUI toast. Clients other than the TUI still get the command behavior, and
the status is available through the plugin RPC (
esuyo.compaction).
Development
npm install
npm run typecheck
npm test
npm run check # typecheck + test
npm run pack:check # verify the published file listLayout:
src/index.ts— plugin registration and config pathsrc/config.ts— JSONC loading, validation, threshold calculationsrc/measure.ts— prompt measurement and text estimationsrc/compaction.ts— event handling, trigger, storage, RPC, and slash commandsrc/rpc.ts— typed methods and the feedback eventsrc/tui.tsx— TUI toast renderer
Publishing
Pushes to main publish automatically through
.github/workflows/publish.yml:
- Run the tests.
- Bump the version from the commit message (
feat:minor,BREAKING CHANGE:ormajor:major, otherwise patch). - Commit and tag the bump, then run
npm publish --access public. - Create the GitHub release.
The workflow needs a repository secret named NPM_TOKEN. Without it the
Check npm token step stops the job before any version bump is pushed.
License
Apache-2.0. See LICENSE.
