TypeSafe Jev guidance for OpenCode's native compaction.
The plugin targets the current OpenCode plugin API rather than a pinned preview runtime. It uses the public experimental.session.compacting hook, fetches the current session transcript through the OpenCode client, asks Jev one set of narrow structured questions about tool evidence, and optionally appends compact guidance to OpenCode's native compaction prompt.
Jev never writes the final summary.
OpenCode compaction starts
↓
plugin fetches the session transcript
↓
deterministic eligibility + capacity preflight
↓
one chronological Jev state
↓
ONE Jev decision stage
↓
deterministic keep / provenance-only / stale policy
↓
guided-native: append compact guidance to output.context
observe: leave output.context untouched
↓
OpenCode writes its normal continuation summary
The plugin does not rewrite historical messages. This preserves the historical request prefix that provider prompt caching may reuse.
| Mode | Jev runs | Native compaction prompt changes | Native summary runs | Use |
|---|---|---|---|---|
guided-native |
yes | appends guidance only | yes | Default production candidate |
observe |
yes | no | yes | Validate Jev decisions without changing OpenCode behavior |
| plugin disabled | no | no | yes | Native OpenCode baseline |
The former direct checkpoint replacement mode was removed in 0.1.0 because the current public OpenCode compaction hook exposes prompt context, not a supported result-replacement API.
Code decides what Jev is allowed to de-emphasize.
| Policy | Examples | Allowed outcome |
|---|---|---|
pin_full |
question, skill, subagent, unknown tools, incomplete calls, failed results, pinned/recent calls | Full evidence remains protected |
protect_call |
write, edit, patch, shell/bash, execute, web fetch/search | Keep action/provenance; raw result may be summarized away |
drop_eligible |
read, grep, glob, find, list, ls | Keep, reduce to provenance, or mark stale |
Unknown tools fail toward full retention.
For a drop-eligible tool Jev answers two independent Noul questions:
- Is retaining the call/input still necessary for the current objective?
- Is retaining the exact full result still necessary?
For a protect-call tool only the exact-result question is needed because code has already protected call provenance.
The default keepThreshold is 0.15. Equality keeps.
Jev is always one semantic decision stage.
Questions may be split across independent API requests when the request budget requires batching. An answer from one batch is never used to construct a dependent verification request.
Before calling Jev, the plugin computes the maximum semantic payload that the deterministic tool policy could possibly remove or de-emphasize.
If that upper bound cannot satisfy minReductionRatio, the plugin falls through to native OpenCode compaction without calling Jev.
Protected-call results already at or below truncateHeadChars are also excluded from Jev questions because reducing them would have no useful effect.
The plugin:
- preserves user and assistant text in its analysis projection
- parses previous structured checkpoints when present
- ignores weak follow-ups such as
continueas standalone objectives - excludes private reasoning/control records from retained evidence
- handles OpenCode ignored text, aborted/error assistant turns, interrupted tool output, and already-compacted results
- excludes inlined plain-text and directory pseudo-files from attachment accounting
- sanitizes host-controlled tool IDs/names before they enter Jev instructions or native guidance
- redacts common secret patterns before TypeSafe requests
- requires HTTPS for TypeSafe except loopback HTTP during local testing
- rejects TypeSafe URL credentials, fragments, and redirects
- fails open to ordinary OpenCode compaction on errors or malformed Jev responses
No npm publication or OpenCode version pin is required. Build the JavaScript artifact locally before loading it in OpenCode.
cd ~/github/opencode-jev-compactor
git switch main
git pull --ff-only
corepack pnpm install
corepack pnpm run typecheck
corepack pnpm run build
corepack pnpm testSet the TypeSafe key in the environment that launches OpenCode:
export TYPESAFE_API_KEY='...'Then add the plugin to OpenCode config:
For the first test use observe. After validating Jev decisions, switch to:
"delivery": "guided-native"OpenCode can load the same plugin from either global or project configuration.
Use the global config when you want the plugin available across normal OpenCode sessions:
~/.config/opencode/opencode.jsonc
Use a project config when you only want it in one repository:
./opencode.jsonc
./opencode.json
The plugin URL is resolved by the OpenCode process, so containerized OpenCode must be able to see the checkout at that same absolute path.
christian-taillon/opencode-containment now supports this checkout directly while continuing to use ghcr.io/anomalyco/opencode:latest.
The containment launchers:
- mount
$HOME/github/opencode-jev-compactorread-only at the same absolute path when it exists - pass
TYPESAFE_API_KEYwhen it is set - make the same trusted checkout available to the Docker Sandboxes backend
- allow
api.typesafe.ai:443in the project sandbox network policy
Override or disable the automatic checkout mount with:
export OPENCODE_JEV_PLUGIN_DIR=/alternate/path
# or
export OPENCODE_JEV_PLUGIN_DIR=OpenCode's current public plugin API does not expose the old custom RPC/TUI surface used by the preview adapter.
The plugin therefore writes structured logs and persists bounded run history at:
$XDG_STATE_HOME/opencode/jev-compaction/state.json
or, when XDG_STATE_HOME is unset:
~/.local/state/opencode/jev-compaction/state.json
Each run records Jev request/input token cost, fitted-state size, maximum prunable payload, tool decisions, projected guidance size, and fallback reason.
See docs/TESTING.md.
The intended comparison is:
- native OpenCode with the plugin disabled
observeguided-native
Measure provider cache reads/writes, compaction input/output, Jev cost, summary size, and continuation quality.
corepack pnpm install
corepack pnpm run typecheck
corepack pnpm testCI runs the same install, typecheck, and test path with the lockfile frozen.
{ "$schema": "https://opencode.ai/config.json", "compaction": { "auto": true, "prune": false }, "plugin": [ [ "file:///home/christian/github/opencode-jev-compactor/dist/index.js", { "enabled": true, "delivery": "observe", "model": "jev-latest", "keepThreshold": 0.15, "preserveRecentMessages": 6, "minReductionRatio": 0.15, "toolResultPreviewChars": 300, "truncateHeadChars": 600, "maxStateChars": 100000, "maxStateTokens": 24000, "maxRequestTokens": 30000, "maxConcurrentRequests": 2, "timeoutMs": 6000, "historyLimit": 10 } ] ] }