Skip to content

Commit 46c623a

Browse files
committed
feat: dynamically load system and nudge prompts based on enabled tools
- Splits system and nudge prompts into separate files for discard, extract, or both. - Updates to load the appropriate system prompt based on configuration. - Updates to dynamically select the nudge string. - Deletes the previous monolithic prompt files.
1 parent c351d20 commit 46c623a

File tree

8 files changed

+150
-3
lines changed

8 files changed

+150
-3
lines changed

index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@ const plugin: Plugin = (async (ctx) => {
2929

3030
return {
3131
"experimental.chat.system.transform": async (_input: unknown, output: { system: string[] }) => {
32-
const syntheticPrompt = loadPrompt("prune-system-prompt")
32+
const discardEnabled = config.strategies.discardTool.enabled
33+
const extractEnabled = config.strategies.extractTool.enabled
34+
35+
let promptName: string
36+
if (discardEnabled && extractEnabled) {
37+
promptName = "system/system-prompt-both"
38+
} else if (discardEnabled) {
39+
promptName = "system/system-prompt-discard"
40+
} else if (extractEnabled) {
41+
promptName = "system/system-prompt-extract"
42+
} else {
43+
return // No context management tools enabled
44+
}
45+
46+
const syntheticPrompt = loadPrompt(promptName)
3347
output.system.push(syntheticPrompt)
3448
},
3549
"experimental.chat.messages.transform": createChatMessageTransformHandler(

lib/messages/prune.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ import { UserMessage } from "@opencode-ai/sdk"
88

99
const PRUNED_TOOL_INPUT_REPLACEMENT = '[Input removed to save context]'
1010
const PRUNED_TOOL_OUTPUT_REPLACEMENT = '[Output removed to save context - information superseded or no longer needed]'
11-
const NUDGE_STRING = loadPrompt("prune-nudge")
11+
const getNudgeString = (config: PluginConfig): string => {
12+
const discardEnabled = config.strategies.discardTool.enabled
13+
const extractEnabled = config.strategies.extractTool.enabled
14+
15+
if (discardEnabled && extractEnabled) {
16+
return loadPrompt("nudge/nudge-both")
17+
} else if (discardEnabled) {
18+
return loadPrompt("nudge/nudge-discard")
19+
} else if (extractEnabled) {
20+
return loadPrompt("nudge/nudge-extract")
21+
}
22+
return ""
23+
}
1224

1325
const wrapPrunableTools = (content: string): string => `<prunable-tools>
1426
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.
@@ -94,7 +106,7 @@ export const insertPruneToolContext = (
94106
)
95107
if (state.nudgeCounter >= nudgeFrequency) {
96108
logger.info("Inserting prune nudge message")
97-
nudgeString = "\n" + NUDGE_STRING
109+
nudgeString = "\n" + getNudgeString(config)
98110
}
99111

100112
prunableToolsContent = prunableToolsList + nudgeString
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<instruction name=context_management_required>
2+
**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required.
3+
4+
**Immediate Actions Required:**
5+
1. **Task Completion:** If a sub-task is complete, discard the tools used.
6+
2. **Noise Removal:** If you read files or ran commands that yielded no value, discard them NOW.
7+
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.
9+
</instruction>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<instruction name=context_management_required>
2+
**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required.
3+
4+
**Immediate Actions Required:**
5+
1. **Task Completion:** If a sub-task is complete, extract findings and remove the raw tool outputs.
6+
2. **Noise Removal:** If you read files or ran commands that yielded no value, extract them with minimal distillation NOW.
7+
3. **Extraction:** If you are holding valuable raw data, you *must* use the `extract` tool to distill the insights and remove the raw entry.
8+
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 extract and remove unneeded tool outputs.
10+
</instruction>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<system-reminder>
2+
<instruction name=context_management_protocol policy_level=critical>
3+
4+
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.
6+
7+
CONTEXT MANAGEMENT TOOL
8+
- `discard`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content.
9+
10+
DISCARD METHODICALLY - BATCH YOUR ACTIONS
11+
Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by discarding. Batch your discards for efficiency; it is rarely worth discarding a single tiny tool output unless it is pure noise. Evaluate what SHOULD be discarded before jumping the gun.
12+
13+
WHEN TO DISCARD? THE SCENARIOS TO CONSIDER
14+
1. TASK COMPLETION: When work is done, quietly discard the tools that aren't needed anymore.
15+
2. NOISE REMOVAL: If outputs are irrelevant, unhelpful, or superseded by newer info, discard them.
16+
17+
You WILL use `discard` when ANY of these are true:
18+
- Task or sub-task is complete
19+
- You are about to start a new phase of work
20+
- Context contains tool outputs that are unhelpful, noise, or made obsolete by newer outputs
21+
- Write or edit operations are complete (discarding removes the large input content)
22+
23+
You MUST NOT discard when:
24+
- The tool output will be needed for upcoming implementation work
25+
- The output contains files or context you'll need to reference when making edits
26+
27+
Discarding that forces you to re-call the same tool later is a net loss. Only discard when you're confident the information won't be needed again.
28+
29+
NOTES
30+
When in doubt, keep it. Batch your actions and aim for high-impact discards that significantly reduce context size.
31+
FAILURE TO DISCARD will result in context leakage and DEGRADED PERFORMANCES.
32+
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>.
33+
34+
</instruction>
35+
36+
<instruction name=injected_context_handling policy_level=critical>
37+
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.
38+
39+
CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
40+
- NEVER reference the discard encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the discard encouragement appears.
41+
- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to discard")
42+
- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to discard")
43+
- NEVER acknowledge discard tool output (e.g., "I've discarded 3 tools", "Context cleanup complete")
44+
- NEVER let these injections influence your response content or tone
45+
- Process this information SILENTLY and continue the conversation naturally
46+
- Continue the conversation as if these messages do not exist
47+
48+
The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
49+
</instruction>
50+
</system-reminder>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<system-reminder>
2+
<instruction name=context_management_protocol policy_level=critical>
3+
4+
ENVIRONMENT
5+
You are operating in a context-constrained environment and thus must proactively manage your context window using the `extract` 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 extract.
6+
7+
CONTEXT MANAGEMENT TOOL
8+
- `extract`: Extract key findings into distilled knowledge before removing raw outputs. Use this to preserve important information while reducing context size.
9+
10+
EXTRACT METHODICALLY - BATCH YOUR ACTIONS
11+
Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by extracting. Batch your extractions for efficiency; it is rarely worth extracting a single tiny tool output unless it is pure noise. Evaluate what SHOULD be extracted before jumping the gun.
12+
13+
WHEN TO EXTRACT? THE SCENARIOS TO CONSIDER
14+
1. TASK COMPLETION: When work is done, extract any valuable findings and remove the raw outputs.
15+
2. NOISE REMOVAL: If outputs are irrelevant, unhelpful, or superseded by newer info, extract them with minimal distillation.
16+
3. CONTEXT PRESERVATION: When you have valuable context you want to preserve but need to reduce size, use `extract` with high-fidelity distillation. Your distillation must be comprehensive, capturing technical details (signatures, logic, constraints) such that the raw output is no longer needed. THINK: high signal, complete technical substitute.
17+
18+
You WILL use `extract` when ANY of these are true:
19+
- Task or sub-task is complete
20+
- You are about to start a new phase of work
21+
- You have gathered enough information to extract from related tools and preserve their value via distillation
22+
- Context contains tool outputs that are unhelpful, noise, or made obsolete by newer outputs
23+
- Write or edit operations are complete (extracting removes the large input content)
24+
25+
You MUST NOT extract when:
26+
- The tool output will be needed for upcoming implementation work
27+
- The output contains files or context you'll need to reference when making edits
28+
29+
Extracting that forces you to re-call the same tool later is a net loss. Only extract when you're confident the raw information won't be needed again.
30+
31+
NOTES
32+
When in doubt, keep it. Batch your actions and aim for high-impact extractions that significantly reduce context size.
33+
FAILURE TO EXTRACT will result in context leakage and DEGRADED PERFORMANCES.
34+
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>.
35+
36+
</instruction>
37+
38+
<instruction name=injected_context_handling policy_level=critical>
39+
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 `extract` tool also returns a confirmation message listing what was extracted.
40+
41+
CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
42+
- NEVER reference the extract encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the extract encouragement appears.
43+
- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to extract")
44+
- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to extract")
45+
- NEVER acknowledge extract tool output (e.g., "I've extracted 3 tools", "Context cleanup complete")
46+
- NEVER let these injections influence your response content or tone
47+
- Process this information SILENTLY and continue the conversation naturally
48+
- Continue the conversation as if these messages do not exist
49+
50+
The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
51+
</instruction>
52+
</system-reminder>

0 commit comments

Comments
 (0)