opencode-compaction-user-prefix
v0.1.0
Published
opencode plugin that prepends a synthetic user message when a compacted conversation would otherwise start with an assistant tool-call, avoiding the Anthropic `tool_use` without `tool_result` error.
Maintainers
Readme
opencode-compaction-user-prefix
An opencode plugin that works around a compaction bug where a freshly compacted conversation can start with an assistant tool-call message, causing Anthropic to reject the request with:
messages.N: `tool_use` ids were found without `tool_result` blocks immediately after.The plugin prepends a synthetic user text message at the experimental.chat.messages.transform boundary so the body sent to the model always begins with a user turn.
The bug
When manual or auto compaction runs after a previous compaction whose tail_start_id landed mid-turn on an assistant tool-call, compaction.process builds selected.head as a chain of consecutive assistant tool-call messages with no user message at the start.
MessageV2.toModelMessagesEffect then faithfully converts that to a model message array whose first entry is assistant tool_use, which Anthropic rejects.
Upstream reference: opencode session message msg_e5550bcfb001EjDfz6hFE3lpHB (compaction failure on session ses_1ae999d1dffeOLwEseaIFPDg0V).
How it works
The plugin hooks experimental.chat.messages.transform, which receives the same WithParts[] opencode is about to pass into toModelMessages. If the first message is an assistant, it prepends a synthetic user text part. opencode recognises text parts in user messages, so the prepended turn survives the conversion and the resulting request body starts with a user message.
This is safe on the normal chat path because real chat already begins with a user message, in which case the !== "assistant" check short-circuits and nothing is prepended.
Install
npm install opencode-compaction-user-prefixOr with bun / pnpm / yarn:
bun add opencode-compaction-user-prefix
pnpm add opencode-compaction-user-prefix
yarn add opencode-compaction-user-prefixUse
Register the plugin in your opencode config (opencode.json or ~/.config/opencode/opencode.json):
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-compaction-user-prefix"]
}Or import it directly in a local plugin file:
export { CompactionUserPrefixPlugin as default } from "opencode-compaction-user-prefix"License
MIT © Quentin Nivelais
