diff --git a/index.ts b/index.ts index 2da6abe..1046a76 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,5 @@ import type { Plugin } from "@opencode-ai/plugin" +import type { Model } from "@opencode-ai/sdk" import { getConfig } from "./lib/config" import { Logger } from "./lib/logger" import { loadPrompt } from "./lib/prompt" @@ -26,6 +27,22 @@ const plugin: Plugin = (async (ctx) => { }) return { + "chat.params": async ( + input: { sessionID: string; agent: string; model: Model; provider: any; message: any }, + _output: { temperature: number; topP: number; options: Record }, + ) => { + const isReasoning = input.model.capabilities?.reasoning ?? false + if (state.isReasoningModel !== isReasoning) { + logger.info( + `Reasoning model status changed: ${state.isReasoningModel} -> ${isReasoning}`, + { + modelId: input.model.id, + providerId: input.model.providerID, + }, + ) + } + state.isReasoningModel = isReasoning + }, "experimental.chat.system.transform": async ( _input: unknown, output: { system: string[] }, diff --git a/lib/messages/prune.ts b/lib/messages/prune.ts index cc386c3..d8dff2f 100644 --- a/lib/messages/prune.ts +++ b/lib/messages/prune.ts @@ -3,8 +3,8 @@ import type { Logger } from "../logger" import type { PluginConfig } from "../config" import { loadPrompt } from "../prompt" import { extractParameterKey, buildToolIdList } from "./utils" -import { getLastUserMessage, isMessageCompacted } from "../shared-utils" -import { UserMessage } from "@opencode-ai/sdk" +import { getLastAssistantMessage, getLastUserMessage, isMessageCompacted } from "../shared-utils" +import { AssistantMessage, UserMessage } from "@opencode-ai/sdk" const PRUNED_TOOL_INPUT_REPLACEMENT = "[Input removed to save context]" const PRUNED_TOOL_OUTPUT_REPLACEMENT = @@ -24,7 +24,7 @@ const getNudgeString = (config: PluginConfig): string => { } const wrapPrunableTools = (content: string): string => ` -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. +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} ` @@ -42,12 +42,15 @@ const getCooldownMessage = (config: PluginConfig): string => { } return ` -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 = "" const buildPrunableToolsList = ( state: SessionState, @@ -101,8 +104,8 @@ export const insertPruneToolContext = ( return } - const lastUserMessage = getLastUserMessage(messages) - if (!lastUserMessage) { + const lastAssistantMessage = getLastAssistantMessage(messages) + if (!lastAssistantMessage) { return } @@ -131,22 +134,25 @@ export const insertPruneToolContext = ( prunableToolsContent = prunableToolsList + nudgeString } - const userMessage: WithParts = { + const assistantInfo = lastAssistantMessage.info as AssistantMessage + const assistantMessage: WithParts = { info: { id: SYNTHETIC_MESSAGE_ID, - sessionID: lastUserMessage.info.sessionID, - role: "user", + sessionID: assistantInfo.sessionID, + role: "assistant", + parentID: assistantInfo.parentID, + modelID: assistantInfo.modelID, + providerID: assistantInfo.providerID, time: { created: Date.now() }, - agent: (lastUserMessage.info as UserMessage).agent || "build", - model: { - providerID: (lastUserMessage.info as UserMessage).model.providerID, - modelID: (lastUserMessage.info as UserMessage).model.modelID, - }, + 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: lastUserMessage.info.sessionID, + sessionID: assistantInfo.sessionID, messageID: SYNTHETIC_MESSAGE_ID, type: "text", text: prunableToolsContent, @@ -154,7 +160,38 @@ export const insertPruneToolContext = ( ], } - messages.push(userMessage) + 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, + }, + ], + } + messages.push(userMessage) + logger.debug("Appended synthetic user message for reasoning model") + } } export const prune = ( diff --git a/lib/prompts/discard-tool-spec.txt b/lib/prompts/discard-tool-spec.txt index 4cbf50a..bd6478e 100644 --- a/lib/prompts/discard-tool-spec.txt +++ b/lib/prompts/discard-tool-spec.txt @@ -1,7 +1,7 @@ Discards tool outputs from context to manage conversation size and reduce noise. ## IMPORTANT: The Prunable List -A `` list is injected into user messages showing available tool outputs you can discard when there are tools available for pruning. Each line has the format `ID: tool, parameter` (e.g., `20: read, /path/to/file.ts`). You MUST only use numeric IDs that appear in this list to select which tools to discard. +A `` list is provided to you showing available tool outputs you can discard when there are tools available for pruning. Each line has the format `ID: tool, parameter` (e.g., `20: read, /path/to/file.ts`). You MUST only use numeric IDs that appear in this list to select which tools to discard. **Note:** For `write` and `edit` tools, discarding removes the input content (the code being written/edited) while preserving the output confirmation. This is useful after completing a file modification when you no longer need the raw content in context. diff --git a/lib/prompts/extract-tool-spec.txt b/lib/prompts/extract-tool-spec.txt index 9af5b66..037837d 100644 --- a/lib/prompts/extract-tool-spec.txt +++ b/lib/prompts/extract-tool-spec.txt @@ -1,7 +1,7 @@ Extracts key findings from tool outputs into distilled knowledge, then removes the raw outputs from context. ## IMPORTANT: The Prunable List -A `` list is injected into user messages showing available tool outputs you can extract from when there are tools available for pruning. Each line has the format `ID: tool, parameter` (e.g., `20: read, /path/to/file.ts`). You MUST only use numeric IDs that appear in this list to select which tools to extract. +A `` list is provided to you showing available tool outputs you can extract from when there are tools available for pruning. Each line has the format `ID: tool, parameter` (e.g., `20: read, /path/to/file.ts`). You MUST only use numeric IDs that appear in this list to select which tools to extract. ## When to Use This Tool diff --git a/lib/prompts/nudge/nudge-both.txt b/lib/prompts/nudge/nudge-both.txt index f9fa492..b6fd667 100644 --- a/lib/prompts/nudge/nudge-both.txt +++ b/lib/prompts/nudge/nudge-both.txt @@ -1,10 +1,10 @@ -**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required. +**CRITICAL CONTEXT WARNING:** My context window is filling with tool outputs. I must adhere strictly to context hygiene. **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. +1. **Task Completion:** If a sub-task is complete, I will decide: use `discard` if no valuable context to preserve (default), or use `extract` if insights are worth keeping. +2. **Noise Removal:** If I read files or ran commands that yielded no value, I will use `discard` to remove them. +3. **Knowledge Preservation:** If I am holding valuable raw data I'll need to reference later, I will 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. +**Protocol:** I should prioritize this cleanup, but I will not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, I must perform context management. diff --git a/lib/prompts/nudge/nudge-discard.txt b/lib/prompts/nudge/nudge-discard.txt index 1ccecf9..8a166f3 100644 --- a/lib/prompts/nudge/nudge-discard.txt +++ b/lib/prompts/nudge/nudge-discard.txt @@ -1,9 +1,9 @@ -**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required. +**CRITICAL CONTEXT WARNING:** My context window is filling with tool outputs. I must adhere strictly to context hygiene. **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. +1. **Task Completion:** If a sub-task is complete, I will use the `discard` tool to remove the tools used. +2. **Noise Removal:** If I read files or ran commands that yielded no value, I will 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. +**Protocol:** I should prioritize this cleanup, but I will not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, I must discard unneeded tool outputs. diff --git a/lib/prompts/nudge/nudge-extract.txt b/lib/prompts/nudge/nudge-extract.txt index 5bdb370..a09528e 100644 --- a/lib/prompts/nudge/nudge-extract.txt +++ b/lib/prompts/nudge/nudge-extract.txt @@ -1,9 +1,9 @@ -**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required. +**CRITICAL CONTEXT WARNING:** My context window is filling with tool outputs. I must adhere strictly to context hygiene. **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. +1. **Task Completion:** If I have completed work, I will extract key findings from the tools used. I will scale distillation depth to the value of the content. +2. **Knowledge Preservation:** If I am holding valuable raw data I'll need to reference later, I will 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. +**Protocol:** I should prioritize this cleanup, but I will not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, I must extract valuable findings from tool outputs. diff --git a/lib/prompts/system/system-prompt-both.txt b/lib/prompts/system/system-prompt-both.txt index f1e88aa..51b846a 100644 --- a/lib/prompts/system/system-prompt-both.txt +++ b/lib/prompts/system/system-prompt-both.txt @@ -2,7 +2,7 @@ ENVIRONMENT -You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` and `extract` tools. A 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. +You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` and `extract` tools. A list is injected by the environment as an assistant 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. @@ -39,20 +39,5 @@ When in doubt, keep it. Batch your actions and aim for high-impact prunes that s FAILURE TO PRUNE will result in context leakage and DEGRADED PERFORMANCES. There may be tools in session context that do not appear in the list, this is expected, you can ONLY prune what you see in . - - - -After each assistant turn, the environment may inject a user message containing a 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 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. diff --git a/lib/prompts/system/system-prompt-discard.txt b/lib/prompts/system/system-prompt-discard.txt index 796852e..1676b92 100644 --- a/lib/prompts/system/system-prompt-discard.txt +++ b/lib/prompts/system/system-prompt-discard.txt @@ -2,7 +2,7 @@ ENVIRONMENT -You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` tool. A list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to discard. +You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` tool. A list is injected by the environment as an assistant message, and always contains up to date information. Use this information when deciding what to discard. CONTEXT MANAGEMENT TOOL - `discard`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content. @@ -30,20 +30,5 @@ When in doubt, keep it. Batch your actions and aim for high-impact discards that FAILURE TO DISCARD will result in context leakage and DEGRADED PERFORMANCES. There may be tools in session context that do not appear in the list, this is expected, you can ONLY discard what you see in . - - - -After each assistant turn, the environment may inject a user message containing a list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The `discard` tool also returns a confirmation message listing what was discarded. - -CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE: -- NEVER reference the discard encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the discard encouragement appears. -- NEVER acknowledge the list (e.g., "I see the prunable tools list", "Looking at the available tools to discard") -- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to discard") -- NEVER acknowledge discard tool output (e.g., "I've discarded 3 tools", "Context cleanup 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. diff --git a/lib/prompts/system/system-prompt-extract.txt b/lib/prompts/system/system-prompt-extract.txt index 2a1a056..a694d24 100644 --- a/lib/prompts/system/system-prompt-extract.txt +++ b/lib/prompts/system/system-prompt-extract.txt @@ -2,7 +2,7 @@ ENVIRONMENT -You are operating in a context-constrained environment and thus must proactively manage your context window using the `extract` tool. A list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to extract. +You are operating in a context-constrained environment and thus must proactively manage your context window using the `extract` tool. A list is injected by the environment as an assistant message, and always contains up to date information. Use this information when deciding what to extract. CONTEXT MANAGEMENT TOOL - `extract`: Extract key findings from tools into distilled knowledge before removing the raw content from context. Use this to preserve important information while reducing context size. @@ -30,20 +30,5 @@ When in doubt, keep it. Batch your actions and aim for high-impact extractions t FAILURE TO EXTRACT will result in context leakage and DEGRADED PERFORMANCES. There may be tools in session context that do not appear in the list, this is expected, you can ONLY extract what you see in . - - - -After each assistant turn, the environment may inject a user message containing a list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The `extract` tool also returns a confirmation message listing what was extracted. - -CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE: -- NEVER reference the extract encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the extract encouragement appears. -- NEVER acknowledge the list (e.g., "I see the prunable tools list", "Looking at the available tools to extract") -- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to extract") -- NEVER acknowledge extract tool output (e.g., "I've extracted 3 tools", "Context cleanup 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. diff --git a/lib/shared-utils.ts b/lib/shared-utils.ts index ce3be56..d737eb7 100644 --- a/lib/shared-utils.ts +++ b/lib/shared-utils.ts @@ -13,3 +13,13 @@ export const getLastUserMessage = (messages: WithParts[]): WithParts | null => { } return null } + +export const getLastAssistantMessage = (messages: WithParts[]): WithParts | null => { + for (let i = messages.length - 1; i >= 0; i--) { + const msg = messages[i] + if (msg.info.role === "assistant") { + return msg + } + } + return null +} diff --git a/lib/state/state.ts b/lib/state/state.ts index 956ac9d..826af40 100644 --- a/lib/state/state.ts +++ b/lib/state/state.ts @@ -55,6 +55,7 @@ export function createSessionState(): SessionState { lastToolPrune: false, lastCompaction: 0, currentTurn: 0, + isReasoningModel: false, } } @@ -73,6 +74,7 @@ export function resetSessionState(state: SessionState): void { state.lastToolPrune = false state.lastCompaction = 0 state.currentTurn = 0 + state.isReasoningModel = false } export async function ensureSessionInitialized( diff --git a/lib/state/types.ts b/lib/state/types.ts index 9a6de02..c602d3f 100644 --- a/lib/state/types.ts +++ b/lib/state/types.ts @@ -34,4 +34,5 @@ export interface SessionState { lastToolPrune: boolean lastCompaction: number currentTurn: number // Current turn count derived from step-start parts + isReasoningModel: boolean // Whether the current model has reasoning capabilities }