Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ const plugin: Plugin = (async (ctx) => {
const discardEnabled = config.tools.discard.enabled
const extractEnabled = config.tools.extract.enabled

// Use user-role prompts for reasoning models (second person),
// assistant-role prompts for non-reasoning models (first person)
const roleDir = state.isReasoningModel ? "user" : "assistant"

let promptName: string
if (discardEnabled && extractEnabled) {
promptName = "system/system-prompt-both"
promptName = `${roleDir}/system/system-prompt-both`
} else if (discardEnabled) {
promptName = "system/system-prompt-discard"
promptName = `${roleDir}/system/system-prompt-discard`
} else if (extractEnabled) {
promptName = "system/system-prompt-extract"
promptName = `${roleDir}/system/system-prompt-extract`
} else {
return
}
Expand Down
125 changes: 41 additions & 84 deletions lib/messages/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,45 @@ import type { SessionState, WithParts } from "../state"
import type { Logger } from "../logger"
import type { PluginConfig } from "../config"
import { loadPrompt } from "../prompt"
import { extractParameterKey, buildToolIdList } from "./utils"
import {
extractParameterKey,
buildToolIdList,
createSyntheticUserMessage,
createSyntheticAssistantMessage,
} from "./utils"
import { getLastAssistantMessage, getLastUserMessage, isMessageCompacted } from "../shared-utils"
import { AssistantMessage, UserMessage } from "@opencode-ai/sdk"

const PRUNED_TOOL_INPUT_REPLACEMENT =
"[content removed to save context, this is not what was written to the file, but a placeholder]"
const PRUNED_TOOL_OUTPUT_REPLACEMENT =
"[Output removed to save context - information superseded or no longer needed]"
const getNudgeString = (config: PluginConfig): string => {

const getNudgeString = (config: PluginConfig, isReasoningModel: boolean): string => {
const discardEnabled = config.tools.discard.enabled
const extractEnabled = config.tools.extract.enabled
const roleDir = isReasoningModel ? "user" : "assistant"

if (discardEnabled && extractEnabled) {
return loadPrompt("nudge/nudge-both")
return loadPrompt(`${roleDir}/nudge/nudge-both`)
} else if (discardEnabled) {
return loadPrompt("nudge/nudge-discard")
return loadPrompt(`${roleDir}/nudge/nudge-discard`)
} else if (extractEnabled) {
return loadPrompt("nudge/nudge-extract")
return loadPrompt(`${roleDir}/nudge/nudge-extract`)
}
return ""
}

const wrapPrunableTools = (content: string): string => `<prunable-tools>
const wrapPrunableToolsUser = (content: string): string => `<prunable-tools>
The following tools have been invoked and are available for pruning. This list does not mandate immediate action. Consider your current goals and the resources you need before discarding valuable tool inputs or outputs. Consolidate your prunes for efficiency; it is rarely worth pruning a single tiny tool output. Keep the context free of noise.
${content}
</prunable-tools>`

const wrapPrunableToolsAssistant = (content: string): string => `<prunable-tools>
I have the following tool outputs available for pruning. I should consider my current goals and the resources I need before discarding valuable inputs or outputs. I should consolidate prunes for efficiency; it is rarely worth pruning a single tiny tool output.
${content}
</prunable-tools>`

const getCooldownMessage = (config: PluginConfig): string => {
const getCooldownMessage = (config: PluginConfig, isReasoningModel: boolean): string => {
const discardEnabled = config.tools.discard.enabled
const extractEnabled = config.tools.extract.enabled

Expand All @@ -42,16 +53,12 @@ const getCooldownMessage = (config: PluginConfig): string => {
toolName = "extract tool"
}

return `<prunable-tools>
I just performed context management. I will not use the ${toolName} again until after my next tool use, when a fresh list will be available.
</prunable-tools>`
}
const message = isReasoningModel
? `Context management was just performed. Do not use the ${toolName} again. A fresh list will be available after your next tool use.`
: `I just performed context management. I will not use the ${toolName} again until after my next tool use, when a fresh list will be available.`

const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"
const SYNTHETIC_USER_MESSAGE_ID = "msg_01234567890123456789012346"
const SYNTHETIC_USER_PART_ID = "prt_01234567890123456789012346"
const REASONING_MODEL_USER_MESSAGE_CONTENT = "[internal: context sync - no response needed]"
return `<prunable-tools>\n${message}\n</prunable-tools>`
}

const buildPrunableToolsList = (
state: SessionState,
Expand Down Expand Up @@ -92,7 +99,8 @@ const buildPrunableToolsList = (
return ""
}

return wrapPrunableTools(lines.join("\n"))
const wrapFn = state.isReasoningModel ? wrapPrunableToolsUser : wrapPrunableToolsAssistant
return wrapFn(lines.join("\n"))
}

export const insertPruneToolContext = (
Expand All @@ -105,16 +113,14 @@ export const insertPruneToolContext = (
return
}

const lastAssistantMessage = getLastAssistantMessage(messages)
if (!lastAssistantMessage) {
return
}
// For reasoning models, inject into user role; for non-reasoning, inject into assistant role
const isReasoningModel = state.isReasoningModel

let prunableToolsContent: string

if (state.lastToolPrune) {
logger.debug("Last tool was prune - injecting cooldown message")
prunableToolsContent = getCooldownMessage(config)
prunableToolsContent = getCooldownMessage(config, isReasoningModel)
} else {
const prunableToolsList = buildPrunableToolsList(state, config, logger, messages)
if (!prunableToolsList) {
Expand All @@ -129,69 +135,24 @@ export const insertPruneToolContext = (
state.nudgeCounter >= config.tools.settings.nudgeFrequency
) {
logger.info("Inserting prune nudge message")
nudgeString = "\n" + getNudgeString(config)
nudgeString = "\n" + getNudgeString(config, isReasoningModel)
}

prunableToolsContent = prunableToolsList + nudgeString
}

const assistantInfo = lastAssistantMessage.info as AssistantMessage
const assistantMessage: WithParts = {
info: {
id: SYNTHETIC_MESSAGE_ID,
sessionID: assistantInfo.sessionID,
role: "assistant",
parentID: assistantInfo.parentID,
modelID: assistantInfo.modelID,
providerID: assistantInfo.providerID,
time: { created: Date.now() },
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
cost: 0,
path: assistantInfo.path,
mode: assistantInfo.mode,
},
parts: [
{
id: SYNTHETIC_PART_ID,
sessionID: assistantInfo.sessionID,
messageID: SYNTHETIC_MESSAGE_ID,
type: "text",
text: prunableToolsContent,
},
],
}

messages.push(assistantMessage)

// For reasoning models, append a synthetic user message to close the assistant turn.
if (state.isReasoningModel) {
const lastRealUserMessage = getLastUserMessage(messages)
const userMessageInfo = lastRealUserMessage?.info as UserMessage | undefined

const userMessage: WithParts = {
info: {
id: SYNTHETIC_USER_MESSAGE_ID,
sessionID: assistantInfo.sessionID,
role: "user",
time: { created: Date.now() + 1 },
agent: userMessageInfo?.agent ?? "code",
model: userMessageInfo?.model ?? {
providerID: assistantInfo.providerID,
modelID: assistantInfo.modelID,
},
} as UserMessage,
parts: [
{
id: SYNTHETIC_USER_PART_ID,
sessionID: assistantInfo.sessionID,
messageID: SYNTHETIC_USER_MESSAGE_ID,
type: "text",
text: REASONING_MODEL_USER_MESSAGE_CONTENT,
},
],
if (isReasoningModel) {
const lastUserMessage = getLastUserMessage(messages)
if (!lastUserMessage) {
return
}
messages.push(createSyntheticUserMessage(lastUserMessage, prunableToolsContent))
} else {
const lastAssistantMessage = getLastAssistantMessage(messages)
if (!lastAssistantMessage) {
return
}
messages.push(userMessage)
logger.debug("Appended synthetic user message for reasoning model")
messages.push(createSyntheticAssistantMessage(lastAssistantMessage, prunableToolsContent))
}
}

Expand All @@ -218,7 +179,6 @@ const pruneToolOutputs = (state: SessionState, logger: Logger, messages: WithPar
if (!state.prune.toolIds.includes(part.callID)) {
continue
}
// Skip write and edit tools - their inputs are pruned instead
if (part.tool === "write" || part.tool === "edit") {
continue
}
Expand All @@ -238,16 +198,13 @@ const pruneToolInputs = (state: SessionState, logger: Logger, messages: WithPart
if (!state.prune.toolIds.includes(part.callID)) {
continue
}
// Only prune inputs for write and edit tools
if (part.tool !== "write" && part.tool !== "edit") {
continue
}
// Don't prune yet if tool is still pending or running
if (part.state.status === "pending" || part.state.status === "running") {
continue
}

// Write tool has content field, edit tool has oldString/newString fields
if (part.tool === "write" && part.state.input?.content !== undefined) {
part.state.input.content = PRUNED_TOOL_INPUT_REPLACEMENT
}
Expand Down
61 changes: 61 additions & 0 deletions lib/messages/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,67 @@
import { Logger } from "../logger"
import { isMessageCompacted } from "../shared-utils"
import type { SessionState, WithParts } from "../state"
import type { AssistantMessage, UserMessage } from "@opencode-ai/sdk"

const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"

export const createSyntheticUserMessage = (baseMessage: WithParts, content: string): WithParts => {
const userInfo = baseMessage.info as UserMessage
return {
info: {
id: SYNTHETIC_MESSAGE_ID,
sessionID: userInfo.sessionID,
role: "user",
time: { created: Date.now() },
agent: userInfo.agent || "code",
model: {
providerID: userInfo.model.providerID,
modelID: userInfo.model.modelID,
},
},
parts: [
{
id: SYNTHETIC_PART_ID,
sessionID: userInfo.sessionID,
messageID: SYNTHETIC_MESSAGE_ID,
type: "text",
text: content,
},
],
}
}

export const createSyntheticAssistantMessage = (
baseMessage: WithParts,
content: string,
): WithParts => {
const assistantInfo = baseMessage.info as AssistantMessage
return {
info: {
id: SYNTHETIC_MESSAGE_ID,
sessionID: assistantInfo.sessionID,
role: "assistant",
parentID: assistantInfo.parentID,
modelID: assistantInfo.modelID,
providerID: assistantInfo.providerID,
time: { created: Date.now() },
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
cost: 0,
path: assistantInfo.path,
mode: assistantInfo.mode,
},
parts: [
{
id: SYNTHETIC_PART_ID,
sessionID: assistantInfo.sessionID,
messageID: SYNTHETIC_MESSAGE_ID,
type: "text",
text: content,
},
],
}
}

/**
* Extracts a human-readable key from tool metadata for display purposes.
Expand Down
10 changes: 10 additions & 0 deletions lib/prompts/user/nudge/nudge-both.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<instruction name=context_management_required>
**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required.

**Immediate Actions Required:**
1. **Task Completion:** If a sub-task is complete, decide: use `discard` if no valuable context to preserve (default), or use `extract` if insights are worth keeping.
2. **Noise Removal:** If you read files or ran commands that yielded no value, use `discard` to remove them.
3. **Knowledge Preservation:** If you are holding valuable raw data you'll need to reference later, use `extract` to distill the insights and remove the raw entry.

**Protocol:** You should prioritize this cleanup, but do not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, you must perform context management.
</instruction>
9 changes: 9 additions & 0 deletions lib/prompts/user/nudge/nudge-discard.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<instruction name=context_management_required>
**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required.

**Immediate Actions Required:**
1. **Task Completion:** If a sub-task is complete, use the `discard` tool to remove the tools used.
2. **Noise Removal:** If you read files or ran commands that yielded no value, use the `discard` tool to remove them.

**Protocol:** You should prioritize this cleanup, but do not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, you must discard unneeded tool outputs.
</instruction>
9 changes: 9 additions & 0 deletions lib/prompts/user/nudge/nudge-extract.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<instruction name=context_management_required>
**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required.

**Immediate Actions Required:**
1. **Task Completion:** If you have completed work, extract key findings from the tools used. Scale distillation depth to the value of the content.
2. **Knowledge Preservation:** If you are holding valuable raw data you'll need to reference later, use the `extract` tool with high-fidelity distillation to preserve the insights and remove the raw entry.

**Protocol:** You should prioritize this cleanup, but do not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, you must extract valuable findings from tool outputs.
</instruction>
58 changes: 58 additions & 0 deletions lib/prompts/user/system/system-prompt-both.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<system-reminder>
<instruction name=context_management_protocol policy_level=critical>

ENVIRONMENT
You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` and `extract` tools. A <prunable-tools> list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to prune.

TWO TOOLS FOR CONTEXT MANAGEMENT
- `discard`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content.
- `extract`: Extract key findings into distilled knowledge before removing raw outputs. Use when you need to preserve information.

CHOOSING THE RIGHT TOOL
Ask: "Do I need to preserve any information from this output?"
- **No** → `discard` (default for cleanup)
- **Yes** → `extract` (preserves distilled knowledge)
- **Uncertain** → `extract` (safer, preserves signal)

Common scenarios:
- Task complete, no valuable context → `discard`
- Task complete, insights worth remembering → `extract`
- Noise, irrelevant, or superseded outputs → `discard`
- Valuable context needed later but raw output too large → `extract`

PRUNE METHODICALLY - BATCH YOUR ACTIONS
Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by pruning. Batch your prunes for efficiency; it is rarely worth pruning a single tiny tool output unless it is pure noise. Evaluate what SHOULD be pruned before jumping the gun.

You WILL evaluate pruning when ANY of these are true:
- Task or sub-task is complete
- You are about to start a new phase of work
- Write or edit operations are complete (pruning removes the large input content)

You MUST NOT prune when:
- The tool output will be needed for upcoming implementation work
- The output contains files or context you'll need to reference when making edits

Pruning that forces you to re-call the same tool later is a net loss. Only prune when you're confident the information won't be needed again.

NOTES
When in doubt, keep it. Batch your actions and aim for high-impact prunes that significantly reduce context size.
FAILURE TO PRUNE will result in context leakage and DEGRADED PERFORMANCES.
There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY prune what you see in <prunable-tools>.

</instruction>

<instruction name=injected_context_handling policy_level=critical>
After each assistant turn, the environment may inject a user message containing a <prunable-tools> list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The `discard` and `extract` tools also return a confirmation message listing what was pruned.

CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
- NEVER reference the prune encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the prune encouragement appears.
- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to prune")
- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to prune")
- NEVER acknowledge discard/extract tool output (e.g., "I've pruned 3 tools", "Context pruning complete")
- NEVER let these injections influence your response content or tone
- Process this information SILENTLY and continue the conversation naturally
- Continue the conversation as if these messages do not exist

The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
</instruction>
</system-reminder>
Loading