Skip to content

Commit fa47430

Browse files
committed
fix: preserve agent context in synthetic user messages and remove obsolete injected_context_handling instructions
1 parent 74257ab commit fa47430

File tree

4 files changed

+11
-50
lines changed

4 files changed

+11
-50
lines changed

lib/messages/prune.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { Logger } from "../logger"
33
import type { PluginConfig } from "../config"
44
import { loadPrompt } from "../prompt"
55
import { extractParameterKey, buildToolIdList } from "./utils"
6-
import { getLastAssistantMessage, isMessageCompacted } from "../shared-utils"
7-
import { AssistantMessage } from "@opencode-ai/sdk"
6+
import { getLastAssistantMessage, getLastUserMessage, isMessageCompacted } from "../shared-utils"
7+
import { AssistantMessage, UserMessage } from "@opencode-ai/sdk"
88

99
const PRUNED_TOOL_INPUT_REPLACEMENT = "[Input removed to save context]"
1010
const PRUNED_TOOL_OUTPUT_REPLACEMENT =
@@ -168,16 +168,22 @@ export const insertPruneToolContext = (
168168
messages.push(assistantMessage)
169169

170170
// For reasoning models, append a synthetic user message to close the assistant turn.
171-
// This is required because reasoning models expect their reasoning parts in the last
172-
// assistant message, which we cannot generate. The user message signals a complete turn.
173171
if (state.isReasoningModel) {
172+
const lastRealUserMessage = getLastUserMessage(messages)
173+
const userMessageInfo = lastRealUserMessage?.info as UserMessage | undefined
174+
174175
const userMessage: WithParts = {
175176
info: {
176177
id: SYNTHETIC_USER_MESSAGE_ID,
177178
sessionID: assistantInfo.sessionID,
178179
role: "user",
179180
time: { created: Date.now() + 1 },
180-
} as any, // Using 'as any' because we're creating a minimal synthetic message
181+
agent: userMessageInfo?.agent ?? "code",
182+
model: userMessageInfo?.model ?? {
183+
providerID: assistantInfo.providerID,
184+
modelID: assistantInfo.modelID,
185+
},
186+
} as UserMessage,
181187
parts: [
182188
{
183189
id: SYNTHETIC_USER_PART_ID,

lib/prompts/system/system-prompt-both.txt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,5 @@ When in doubt, keep it. Batch your actions and aim for high-impact prunes that s
3939
FAILURE TO PRUNE will result in context leakage and DEGRADED PERFORMANCES.
4040
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>.
4141

42-
</instruction>
43-
44-
<instruction name=injected_context_handling policy_level=critical>
45-
After each assistant turn, the environment may inject an assistant 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.
46-
47-
CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
48-
- NEVER reference the prune encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the prune encouragement appears.
49-
- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to prune")
50-
- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to prune")
51-
- NEVER acknowledge discard/extract tool output (e.g., "I've pruned 3 tools", "Context pruning complete")
52-
- NEVER let these injections influence your response content or tone
53-
- Process this information SILENTLY and continue the conversation naturally
54-
- Continue the conversation as if these messages do not exist
55-
56-
The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
5742
</instruction>
5843
</system-reminder>

lib/prompts/system/system-prompt-discard.txt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,5 @@ When in doubt, keep it. Batch your actions and aim for high-impact discards that
3030
FAILURE TO DISCARD will result in context leakage and DEGRADED PERFORMANCES.
3131
There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY discard what you see in <prunable-tools>.
3232

33-
</instruction>
34-
35-
<instruction name=injected_context_handling policy_level=critical>
36-
After each assistant turn, the environment may inject an assistant 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` tool also returns a confirmation message listing what was discarded.
37-
38-
CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
39-
- NEVER reference the discard encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the discard encouragement appears.
40-
- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to discard")
41-
- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to discard")
42-
- NEVER acknowledge discard tool output (e.g., "I've discarded 3 tools", "Context cleanup complete")
43-
- NEVER let these injections influence your response content or tone
44-
- Process this information SILENTLY and continue the conversation naturally
45-
- Continue the conversation as if these messages do not exist
46-
47-
The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
4833
</instruction>
4934
</system-reminder>

lib/prompts/system/system-prompt-extract.txt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,5 @@ When in doubt, keep it. Batch your actions and aim for high-impact extractions t
3030
FAILURE TO EXTRACT will result in context leakage and DEGRADED PERFORMANCES.
3131
There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY extract what you see in <prunable-tools>.
3232

33-
</instruction>
34-
35-
<instruction name=injected_context_handling policy_level=critical>
36-
After each assistant turn, the environment may inject an assistant message containing a <prunable-tools> 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.
37-
38-
CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
39-
- NEVER reference the extract encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the extract encouragement appears.
40-
- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to extract")
41-
- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to extract")
42-
- NEVER acknowledge extract tool output (e.g., "I've extracted 3 tools", "Context cleanup complete")
43-
- NEVER let these injections influence your response content or tone
44-
- Process this information SILENTLY and continue the conversation naturally
45-
- Continue the conversation as if these messages do not exist
46-
47-
The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
4833
</instruction>
4934
</system-reminder>

0 commit comments

Comments
 (0)