Registers a session_restart tool that spawns a fresh child session on the current project, prompts it with a focused task (+ optional notes) via promptAsync, and aborts the current session by default so a single focused task continues in a clean context window instead of compacting. Includes an experimental.compaction.autocontinue hook to reject the synthetic continue turn after compaction (default on).
2.9 KiB
2.9 KiB
opencode-session-restart
opencode plugin that lets the agent restart a session into a fresh child session with a clean context window and a focused prompt, dodging context compaction loss.
When a session runs long enough to trigger compaction, opencode summarizes the conversation into a prompt and keeps going — fidelity you did not choose. This plugin gives the agent a first-class escape hatch: start a new child session on the same project, prompt it with a curated fresh prompt, and (by default) freeze the old session. The single focused task continues in a fresh window.
Install
Add the plugin to your opencode config:
{
"plugin": ["opencode-session-restart"]
}
or from a local clone:
{
"plugin": [["/path/to/opencode-session-restart", { "abort_current": true }]]
}
What it does
The plugin registers one tool, session_restart, that the agent can call when the session is getting long:
- Creates a child session (via the SDK
session.createwithparentID), so it shows up in the session tree under the original. - Immediately prompts it (via
session.promptAsync) with the focusedtaskplus optional carry-overnotesand a fresh-start preamble that tells the new session not to reconstruct the old conversation and to orient itself from the working tree. - Aborts the current session by default, so only the restarted session stays active.
- Returns the new session ID so the agent can tell you where to switch.
Tool arguments
| argument | type | default | description |
|---|---|---|---|
task |
string | — | The single focused task for the restarted session, written as a fresh prompt. |
notes |
string | — | Durable context to carry over: decisions, constraints, files touched, unfinished steps. Omit to rely on the working tree only. |
model |
string | current | "providerID/modelID" for the restarted session. |
agent |
string | current | Agent for the restarted session. |
abort_current |
boolean | true |
Abort this session after restarting. |
Notes on behavior
- The new session keeps the fresh prompt as its user-turn text and immediately starts working. On-disk state (git status, staged or uncommitted changes) carries over because it is the same project directory.
abort_currentfreezes the original session after the new one is started. Because abort cancels the current agent turn, the tool's returned text may not be delivered to the old session — the side effects (create + prompt) complete first.disable_compaction_continue(defaulttrue) rejects the synthetic "continue" turn opencode would append after a compaction, so a compaction that does slip through never silently continues. Set it tofalseto keep stock behavior.
Development
bun install
bunx tsc --noEmit
License
MIT