Skip to content

Commit 8f8a5c6

Browse files
authored
Merge pull request #187 from Opencode-DCP/feat/assistant-role-injection
Use assistant role for prunable-tools injection
2 parents 936a1b5 + 1960877 commit 8f8a5c6

File tree

13 files changed

+101
-79
lines changed

13 files changed

+101
-79
lines changed

index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Plugin } from "@opencode-ai/plugin"
2+
import type { Model } from "@opencode-ai/sdk"
23
import { getConfig } from "./lib/config"
34
import { Logger } from "./lib/logger"
45
import { loadPrompt } from "./lib/prompt"
@@ -26,6 +27,22 @@ const plugin: Plugin = (async (ctx) => {
2627
})
2728

2829
return {
30+
"chat.params": async (
31+
input: { sessionID: string; agent: string; model: Model; provider: any; message: any },
32+
_output: { temperature: number; topP: number; options: Record<string, any> },
33+
) => {
34+
const isReasoning = input.model.capabilities?.reasoning ?? false
35+
if (state.isReasoningModel !== isReasoning) {
36+
logger.info(
37+
`Reasoning model status changed: ${state.isReasoningModel} -> ${isReasoning}`,
38+
{
39+
modelId: input.model.id,
40+
providerId: input.model.providerID,
41+
},
42+
)
43+
}
44+
state.isReasoningModel = isReasoning
45+
},
2946
"experimental.chat.system.transform": async (
3047
_input: unknown,
3148
output: { system: string[] },

lib/messages/prune.ts

Lines changed: 53 additions & 16 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 { getLastUserMessage, isMessageCompacted } from "../shared-utils"
7-
import { UserMessage } 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 =
@@ -24,7 +24,7 @@ const getNudgeString = (config: PluginConfig): string => {
2424
}
2525

2626
const wrapPrunableTools = (content: string): string => `<prunable-tools>
27-
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.
27+
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.
2828
${content}
2929
</prunable-tools>`
3030

@@ -42,12 +42,15 @@ const getCooldownMessage = (config: PluginConfig): string => {
4242
}
4343

4444
return `<prunable-tools>
45-
Context management was just performed. Do not use the ${toolName} again. A fresh list will be available after your next tool use.
45+
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.
4646
</prunable-tools>`
4747
}
4848

4949
const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
5050
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"
51+
const SYNTHETIC_USER_MESSAGE_ID = "msg_01234567890123456789012346"
52+
const SYNTHETIC_USER_PART_ID = "prt_01234567890123456789012346"
53+
const REASONING_MODEL_USER_MESSAGE_CONTENT = "<system-context-injection/>"
5154

5255
const buildPrunableToolsList = (
5356
state: SessionState,
@@ -101,8 +104,8 @@ export const insertPruneToolContext = (
101104
return
102105
}
103106

104-
const lastUserMessage = getLastUserMessage(messages)
105-
if (!lastUserMessage) {
107+
const lastAssistantMessage = getLastAssistantMessage(messages)
108+
if (!lastAssistantMessage) {
106109
return
107110
}
108111

@@ -131,30 +134,64 @@ export const insertPruneToolContext = (
131134
prunableToolsContent = prunableToolsList + nudgeString
132135
}
133136

134-
const userMessage: WithParts = {
137+
const assistantInfo = lastAssistantMessage.info as AssistantMessage
138+
const assistantMessage: WithParts = {
135139
info: {
136140
id: SYNTHETIC_MESSAGE_ID,
137-
sessionID: lastUserMessage.info.sessionID,
138-
role: "user",
141+
sessionID: assistantInfo.sessionID,
142+
role: "assistant",
143+
parentID: assistantInfo.parentID,
144+
modelID: assistantInfo.modelID,
145+
providerID: assistantInfo.providerID,
139146
time: { created: Date.now() },
140-
agent: (lastUserMessage.info as UserMessage).agent || "build",
141-
model: {
142-
providerID: (lastUserMessage.info as UserMessage).model.providerID,
143-
modelID: (lastUserMessage.info as UserMessage).model.modelID,
144-
},
147+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
148+
cost: 0,
149+
path: assistantInfo.path,
150+
mode: assistantInfo.mode,
145151
},
146152
parts: [
147153
{
148154
id: SYNTHETIC_PART_ID,
149-
sessionID: lastUserMessage.info.sessionID,
155+
sessionID: assistantInfo.sessionID,
150156
messageID: SYNTHETIC_MESSAGE_ID,
151157
type: "text",
152158
text: prunableToolsContent,
153159
},
154160
],
155161
}
156162

157-
messages.push(userMessage)
163+
messages.push(assistantMessage)
164+
165+
// For reasoning models, append a synthetic user message to close the assistant turn.
166+
if (state.isReasoningModel) {
167+
const lastRealUserMessage = getLastUserMessage(messages)
168+
const userMessageInfo = lastRealUserMessage?.info as UserMessage | undefined
169+
170+
const userMessage: WithParts = {
171+
info: {
172+
id: SYNTHETIC_USER_MESSAGE_ID,
173+
sessionID: assistantInfo.sessionID,
174+
role: "user",
175+
time: { created: Date.now() + 1 },
176+
agent: userMessageInfo?.agent ?? "code",
177+
model: userMessageInfo?.model ?? {
178+
providerID: assistantInfo.providerID,
179+
modelID: assistantInfo.modelID,
180+
},
181+
} as UserMessage,
182+
parts: [
183+
{
184+
id: SYNTHETIC_USER_PART_ID,
185+
sessionID: assistantInfo.sessionID,
186+
messageID: SYNTHETIC_USER_MESSAGE_ID,
187+
type: "text",
188+
text: REASONING_MODEL_USER_MESSAGE_CONTENT,
189+
},
190+
],
191+
}
192+
messages.push(userMessage)
193+
logger.debug("Appended synthetic user message for reasoning model")
194+
}
158195
}
159196

160197
export const prune = (

lib/prompts/discard-tool-spec.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Discards tool outputs from context to manage conversation size and reduce noise.
22

33
## IMPORTANT: The Prunable List
4-
A `<prunable-tools>` 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.
4+
A `<prunable-tools>` 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.
55

66
**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.
77

lib/prompts/extract-tool-spec.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Extracts key findings from tool outputs into distilled knowledge, then removes the raw outputs from context.
22

33
## IMPORTANT: The Prunable List
4-
A `<prunable-tools>` 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.
4+
A `<prunable-tools>` 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.
55

66
## When to Use This Tool
77

lib/prompts/nudge/nudge-both.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<instruction name=context_management_required>
2-
**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required.
2+
**CRITICAL CONTEXT WARNING:** My context window is filling with tool outputs. I must adhere strictly to context hygiene.
33

44
**Immediate Actions Required:**
5-
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.
6-
2. **Noise Removal:** If you read files or ran commands that yielded no value, use `discard` to remove them.
7-
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.
5+
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.
6+
2. **Noise Removal:** If I read files or ran commands that yielded no value, I will use `discard` to remove them.
7+
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.
88

9-
**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.
9+
**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.
1010
</instruction>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<instruction name=context_management_required>
2-
**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required.
2+
**CRITICAL CONTEXT WARNING:** My context window is filling with tool outputs. I must adhere strictly to context hygiene.
33

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

8-
**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.
8+
**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.
99
</instruction>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<instruction name=context_management_required>
2-
**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required.
2+
**CRITICAL CONTEXT WARNING:** My context window is filling with tool outputs. I must adhere strictly to context hygiene.
33

44
**Immediate Actions Required:**
5-
1. **Task Completion:** If you have completed work, extract key findings from the tools used. Scale distillation depth to the value of the content.
6-
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.
5+
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.
6+
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.
77

8-
**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.
8+
**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.
99
</instruction>

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<instruction name=context_management_protocol policy_level=critical>
33

44
ENVIRONMENT
5-
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.
5+
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 an assistant message, and always contains up to date information. Use this information when deciding what to prune.
66

77
TWO TOOLS FOR CONTEXT MANAGEMENT
88
- `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
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 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.
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: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<instruction name=context_management_protocol policy_level=critical>
33

44
ENVIRONMENT
5-
You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` tool. 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 discard.
5+
You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` tool. A <prunable-tools> 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.
66

77
CONTEXT MANAGEMENT TOOL
88
- `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
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 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` 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>

0 commit comments

Comments
 (0)