npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@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.

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-compaction

The 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 ci

Then 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.json

The 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

  1. Use triggerTokens when it is set.
  2. Otherwise, use contextWindow if set. It defaults to 256000, so every model uses the same trigger. Set it to null to use the model's reported min(input, context) limit, or fallbackContextWindow when the model reports none.
  3. Reserve reserveTokens when set, else window × reservePercent / 100.
  4. 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 off only 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.
  • compaction is lossy. Increase OpenCode's compaction.keep.tokens if 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 contextWindow or triggerTokens to 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 list

Layout:

  • src/index.ts — plugin registration and config path
  • src/config.ts — JSONC loading, validation, threshold calculation
  • src/measure.ts — prompt measurement and text estimation
  • src/compaction.ts — event handling, trigger, storage, RPC, and slash command
  • src/rpc.ts — typed methods and the feedback event
  • src/tui.tsx — TUI toast renderer

Publishing

Pushes to main publish automatically through .github/workflows/publish.yml:

  1. Run the tests.
  2. Bump the version from the commit message (feat: minor, BREAKING CHANGE: or major: major, otherwise patch).
  3. Commit and tag the bump, then run npm publish --access public.
  4. 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.