From 16b86c767deda53046bfd1b6fd982de945e87596 Mon Sep 17 00:00:00 2001 From: Daniel Smolsky Date: Mon, 22 Dec 2025 19:51:06 -0500 Subject: [PATCH 1/6] refactor: use assistant role for prunable-tools injection Switch from user role to assistant role message for injecting prunable-tools context. This reduces the likelihood of the model directly addressing the injected content as if responding to user input, improving the conversational experience. --- lib/messages/prune.ts | 31 +++++++++++++++++-------------- lib/shared-utils.ts | 10 ++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/messages/prune.ts b/lib/messages/prune.ts index cc386c32..241a49ce 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, isMessageCompacted } from "../shared-utils" +import { AssistantMessage } from "@opencode-ai/sdk" const PRUNED_TOOL_INPUT_REPLACEMENT = "[Input removed to save context]" const PRUNED_TOOL_OUTPUT_REPLACEMENT = @@ -101,8 +101,8 @@ export const insertPruneToolContext = ( return } - const lastUserMessage = getLastUserMessage(messages) - if (!lastUserMessage) { + const lastAssistantMessage = getLastAssistantMessage(messages) + if (!lastAssistantMessage) { return } @@ -131,22 +131,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 +157,7 @@ export const insertPruneToolContext = ( ], } - messages.push(userMessage) + messages.push(assistantMessage) } export const prune = ( diff --git a/lib/shared-utils.ts b/lib/shared-utils.ts index ce3be56a..d737eb72 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 +} From 2886029e95274344386f74a355de53e677c45213 Mon Sep 17 00:00:00 2001 From: Daniel Smolsky Date: Mon, 22 Dec 2025 21:56:15 -0500 Subject: [PATCH 2/6] feat: add reasoning model support for synthetic message injection Reasoning models expect their encrypted reasoning parts in assistant messages, which we cannot generate. This adds detection via chat.params hook and appends a synthetic user message () to close the assistant turn properly when a reasoning model is active. --- index.ts | 17 +++++++++++++++++ lib/messages/prune.ts | 33 +++++++++++++++++++++++++++++++++ lib/state/state.ts | 2 ++ lib/state/types.ts | 1 + 4 files changed, 53 insertions(+) diff --git a/index.ts b/index.ts index 2da6abee..1046a763 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 241a49ce..02c36670 100644 --- a/lib/messages/prune.ts +++ b/lib/messages/prune.ts @@ -48,6 +48,14 @@ Context management was just performed. Do not use the ${toolName} again. A fresh 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" + +// Content for the synthetic user message appended after the assistant message for reasoning models. +// This is required because reasoning models expect their reasoning parts in assistant messages, +// and we cannot generate those encrypted/proprietary parts. By closing the assistant turn with +// a user message, the model sees a complete conversation structure. +const REASONING_MODEL_USER_MESSAGE_CONTENT = "" const buildPrunableToolsList = ( state: SessionState, @@ -158,6 +166,31 @@ export const insertPruneToolContext = ( } messages.push(assistantMessage) + + // For reasoning models, append a synthetic user message to close the assistant turn. + // This is required because reasoning models expect their reasoning parts in the last + // assistant message, which we cannot generate. The user message signals a complete turn. + if (state.isReasoningModel) { + const userMessage: WithParts = { + info: { + id: SYNTHETIC_USER_MESSAGE_ID, + sessionID: assistantInfo.sessionID, + role: "user", + time: { created: Date.now() + 1 }, + } as any, // Using 'as any' because we're creating a minimal synthetic message + 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/state/state.ts b/lib/state/state.ts index 956ac9dd..826af401 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 9a6de02d..c602d3f7 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 } From 2c80d037aab91ee68135bb76b244ac1dce8403b8 Mon Sep 17 00:00:00 2001 From: Daniel Smolsky Date: Mon, 22 Dec 2025 22:05:57 -0500 Subject: [PATCH 3/6] fix: update injected prompts to use first-person language for assistant messages - Nudge prompts now use 'I must/I will' instead of 'You must/You should' - Prunable tools wrapper and cooldown message use first-person - System prompts updated to correctly describe injection as assistant message --- lib/messages/prune.ts | 4 ++-- lib/prompts/nudge/nudge-both.txt | 10 +++++----- lib/prompts/nudge/nudge-discard.txt | 8 ++++---- lib/prompts/nudge/nudge-extract.txt | 8 ++++---- lib/prompts/system/system-prompt-both.txt | 4 ++-- lib/prompts/system/system-prompt-discard.txt | 4 ++-- lib/prompts/system/system-prompt-extract.txt | 4 ++-- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/messages/prune.ts b/lib/messages/prune.ts index 02c36670..53e88ed3 100644 --- a/lib/messages/prune.ts +++ b/lib/messages/prune.ts @@ -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 will consider my current goals and the resources I need before discarding valuable inputs or outputs. I will consolidate prunes for efficiency; it is rarely worth pruning a single tiny tool output. ${content} ` @@ -42,7 +42,7 @@ 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 should not use the ${toolName} again until after my next tool use, when a fresh list will be available. ` } diff --git a/lib/prompts/nudge/nudge-both.txt b/lib/prompts/nudge/nudge-both.txt index f9fa4926..b6fd6673 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 1ccecf9e..8a166f37 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 5bdb370f..a09528e6 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 f1e88aa2..4ba3bfee 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. @@ -42,7 +42,7 @@ There may be tools in session context that do not appear in the -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. +After each assistant turn, the environment may inject an assistant 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. diff --git a/lib/prompts/system/system-prompt-discard.txt b/lib/prompts/system/system-prompt-discard.txt index 796852e3..384686a6 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. @@ -33,7 +33,7 @@ There may be tools in session context that do not appear in the -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. +After each assistant turn, the environment may inject an assistant 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. diff --git a/lib/prompts/system/system-prompt-extract.txt b/lib/prompts/system/system-prompt-extract.txt index 2a1a0568..810cef72 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. @@ -33,7 +33,7 @@ There may be tools in session context that do not appear in the -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. +After each assistant turn, the environment may inject an assistant 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. From 74257ab3cd91a4ad2fdd3c0fca081a6612465035 Mon Sep 17 00:00:00 2001 From: Daniel Smolsky Date: Mon, 22 Dec 2025 22:26:10 -0500 Subject: [PATCH 4/6] fix: update tool specs and injected prompts for assistant-role injection --- lib/messages/prune.ts | 4 ++-- lib/prompts/discard-tool-spec.txt | 2 +- lib/prompts/extract-tool-spec.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/messages/prune.ts b/lib/messages/prune.ts index 53e88ed3..073f74d6 100644 --- a/lib/messages/prune.ts +++ b/lib/messages/prune.ts @@ -24,7 +24,7 @@ const getNudgeString = (config: PluginConfig): string => { } const wrapPrunableTools = (content: string): string => ` -I have the following tool outputs available for pruning. I will consider my current goals and the resources I need before discarding valuable inputs or outputs. I will consolidate prunes for efficiency; it is rarely worth pruning a single tiny tool output. +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,7 +42,7 @@ const getCooldownMessage = (config: PluginConfig): string => { } return ` -I just performed context management. I should not use the ${toolName} again until after my next tool use, when a fresh list will be available. +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. ` } diff --git a/lib/prompts/discard-tool-spec.txt b/lib/prompts/discard-tool-spec.txt index 4cbf50a5..bd6478e8 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 9af5b666..037837dc 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 From fa47430647ba30f3ba4c144ad5f98cde1997355e Mon Sep 17 00:00:00 2001 From: Daniel Smolsky Date: Mon, 22 Dec 2025 23:04:18 -0500 Subject: [PATCH 5/6] fix: preserve agent context in synthetic user messages and remove obsolete injected_context_handling instructions --- lib/messages/prune.ts | 16 +++++++++++----- lib/prompts/system/system-prompt-both.txt | 15 --------------- lib/prompts/system/system-prompt-discard.txt | 15 --------------- lib/prompts/system/system-prompt-extract.txt | 15 --------------- 4 files changed, 11 insertions(+), 50 deletions(-) diff --git a/lib/messages/prune.ts b/lib/messages/prune.ts index 073f74d6..b6220fab 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 { getLastAssistantMessage, isMessageCompacted } from "../shared-utils" -import { AssistantMessage } 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 = @@ -168,16 +168,22 @@ export const insertPruneToolContext = ( messages.push(assistantMessage) // For reasoning models, append a synthetic user message to close the assistant turn. - // This is required because reasoning models expect their reasoning parts in the last - // assistant message, which we cannot generate. The user message signals a complete 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 }, - } as any, // Using 'as any' because we're creating a minimal synthetic message + agent: userMessageInfo?.agent ?? "code", + model: userMessageInfo?.model ?? { + providerID: assistantInfo.providerID, + modelID: assistantInfo.modelID, + }, + } as UserMessage, parts: [ { id: SYNTHETIC_USER_PART_ID, diff --git a/lib/prompts/system/system-prompt-both.txt b/lib/prompts/system/system-prompt-both.txt index 4ba3bfee..51b846a7 100644 --- a/lib/prompts/system/system-prompt-both.txt +++ b/lib/prompts/system/system-prompt-both.txt @@ -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 an assistant 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 384686a6..1676b923 100644 --- a/lib/prompts/system/system-prompt-discard.txt +++ b/lib/prompts/system/system-prompt-discard.txt @@ -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 an assistant 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 810cef72..a694d24c 100644 --- a/lib/prompts/system/system-prompt-extract.txt +++ b/lib/prompts/system/system-prompt-extract.txt @@ -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 an assistant 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. From 19608770474dd4390c3bd855a07b9f5f39129098 Mon Sep 17 00:00:00 2001 From: Daniel Smolsky Date: Mon, 22 Dec 2025 23:10:12 -0500 Subject: [PATCH 6/6] cleanup --- lib/messages/prune.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/messages/prune.ts b/lib/messages/prune.ts index b6220fab..d8dff2f3 100644 --- a/lib/messages/prune.ts +++ b/lib/messages/prune.ts @@ -50,11 +50,6 @@ 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" - -// Content for the synthetic user message appended after the assistant message for reasoning models. -// This is required because reasoning models expect their reasoning parts in assistant messages, -// and we cannot generate those encrypted/proprietary parts. By closing the assistant turn with -// a user message, the model sees a complete conversation structure. const REASONING_MODEL_USER_MESSAGE_CONTENT = "" const buildPrunableToolsList = (