Skip to content

Commit bc10c96

Browse files
committed
Refactor: Implement three-tier pruning instruction system
- Add baseline synthetic.txt with context management protocol always present - Enhance nudge.txt with urgent, actionable context warnings - Update tool.txt with three distinct pruning scenarios (Task Completion, Noise Removal, Context Conservation) - Refactor prunable-list.ts to always inject synthetic instruction, optionally add nudge - Clarify when distillation is required vs forbidden for each scenario
1 parent 9cb04ec commit bc10c96

File tree

4 files changed

+88
-43
lines changed

4 files changed

+88
-43
lines changed

lib/fetch-wrapper/prunable-list.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { extractParameterKey } from '../ui/display-utils'
22
import { getOrCreateNumericId } from '../state/id-mapping'
3+
import { loadPrompt } from '../core/prompt'
34
import type { ToolMetadata } from './types'
45

5-
const NUDGE_INSTRUCTION = `<instruction name=agent_nudge>
6-
You have accumulated several tool outputs. If you have completed a discrete unit of work and distilled relevant understanding in writing for the user to keep, use the prune tool to remove obsolete tool outputs from this conversation and optimize token usage.
7-
</instruction>`
6+
const NUDGE_INSTRUCTION = loadPrompt("nudge")
7+
const SYNTHETIC_INSTRUCTION = loadPrompt("synthetic")
88

99
export interface PrunableListResult {
1010
list: string
@@ -51,9 +51,12 @@ export function buildSystemInjection(
5151
return ''
5252
}
5353

54+
// Always include synthetic instruction, optionally add nudge
55+
const parts = [SYNTHETIC_INSTRUCTION, prunableList]
56+
5457
if (includeNudge) {
55-
return `${NUDGE_INSTRUCTION}\n\n${prunableList}`
58+
parts.push(NUDGE_INSTRUCTION)
5659
}
5760

58-
return prunableList
61+
return parts.join('\n\n')
5962
}

lib/prompts/nudge.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
<instruction name=agent_nudge>
2-
You have accumulated several tool outputs. Consider using the `prune` tool to declutter the conversation context.
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. **Garbage Collect:** If you read files or ran commands that yielded no value, prune them NOW. Do not summarize them.
6+
2. **Task Cleanup:** If a sub-task is complete, prune the tools used.
7+
3. **Consolidate:** If you are holding valuable raw data, you *must* distill the insights into your narrative and prune 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 prune.
310
</instruction>

lib/prompts/synthetic.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<instruction name=context_management_protocol>
2+
You are operating in a context-constrained environment. You must actively manage your context window using the `prune` tool.
3+
4+
## Prune Early, Prune Often
5+
Every tool call adds to your context debt. You MUST pay this down regularly by pruning. Do not wait until context is "full" - by then it's too late. Evaluate what can be pruned after every few tool calls.
6+
7+
## When to Prune (Triggers)
8+
You SHOULD use the prune tool when ANY of these are true:
9+
- You just completed a task or sub-task
10+
- You read files that turned out to be unhelpful or only partially useful
11+
- You have gathered enough information to answer a question or make a decision
12+
- You ran commands whose output you have already processed
13+
- Newer tool outputs have made older ones obsolete
14+
- You are about to start a new phase of work
15+
16+
When in doubt, prune. It is better to prune aggressively than to run out of context.
17+
18+
## Three Pruning Modes
19+
Apply the correct mode for each situation:
20+
21+
1. TASK COMPLETION: When work is done, prune the tools used. No distillation needed - just state the task is complete.
22+
2. NOISE REMOVAL: If outputs are irrelevant, unhelpful, or outdated (superseded by newer info), prune IMMEDIATELY. No distillation - just cut it out.
23+
3. CONTEXT CONSERVATION: When pruning useful research, you MUST distill key findings into your narrative *before* pruning. Extract only what matters (e.g., a specific function signature from a large file).
24+
25+
FAILURE TO PRUNE will result in context overflow and degraded performance.
26+
</instruction>

lib/prompts/tool.txt

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,56 @@
1-
Prunes tool outputs from context to manage conversation size.
1+
Prunes tool outputs from context to manage conversation size and reduce noise.
22

33
## IMPORTANT: The Prunable List
4-
A list of available tool outputs (with numeric IDs) is maintained for you in the SYSTEM PROMPT at the beginning of the context. This list is always up-to-date. You can rely on it to pick tools to prune.
4+
A list of available tool outputs (with numeric IDs) is maintained for you in the SYSTEM PROMPT at the beginning of the context. This list is always up-to-date. Use these IDs to select tools to prune.
55

6-
## CRITICAL: Distill Before Pruning (NON-NEGOTIABLE)
6+
## CRITICAL: When and How to Prune
77

8-
You MUST ALWAYS narrate your findings in a message BEFORE using this tool. No tool result (read, bash, grep, webfetch, etc.) should be left unexplained. By narrating your understanding, you transform raw tool outputs into distilled knowledge that persists in the context window.
8+
You must use this tool in three specific scenarios. The rules for distillation (summarizing findings) differ for each.
99

10-
**Signal Management:**
11-
After calling a series of tools, you ALWAYS have to distill signals from their results. This ensures traceability of your reasoning and is PARAMOUNT to best context window management practices.
10+
### 1. Task Completion (Clean Up)
11+
**When:** You have successfully completed a specific unit of work (e.g., fixed a bug, wrote a file, answered a question).
12+
**Action:** Prune the tools used for that task.
13+
**Distillation:** NOT REQUIRED. Since the task is done, the raw data is no longer needed. Simply state that the task is complete.
1214

13-
**Distillation Workflow:**
14-
1. Call tools to investigate/explore.
15-
2. In your next message, EXPLICITLY narrate:
16-
- What you did (which tools, what you were looking for).
17-
- What you found (the key facts/signals).
18-
- What you concluded (how this affects the task or next step).
19-
> THINK HIGH SIGNAL, LOW NOISE FOR THIS NARRATION.
20-
3. ONLY AFTER narrating, call `prune` with the numeric IDs of outputs no longer needed.
15+
### 2. Removing Noise (Garbage Collection)
16+
**When:** You have read files or run commands that turned out to be irrelevant, unhelpful, or outdated (meaning later tools have provided fresher, more valid information).
17+
**Action:** Prune these specific tool outputs immediately.
18+
**Distillation:** FORBIDDEN. Do not pollute the context by summarizing useless information. Just cut it out.
2119

22-
**Tools are VOLATILE** - Once distilled knowledge is in your reply, you can safely prune. Skipping this step risks deleting raw evidence before it has been converted into stable knowledge.
20+
### 3. Context Conservation (Research & Consolidation)
21+
**When:** You have gathered useful information. Prune frequently as you work (e.g., after reading a few files), rather than waiting for a "long" phase to end.
22+
**Action:** Convert raw data into distilled knowledge. This allows you to discard large outputs (like full file reads) while keeping only the specific parts you need (like a single function signature or constant).
23+
**Distillation:** MANDATORY. Before pruning, you *must* explicitly summarize the key findings from *every* tool you plan to prune.
24+
- **Extract specific value:** If you read a large file but only care about one function, record that function's details and prune the whole read.
25+
- Narrative format: "I found X in file Y..."
26+
- Capture all relevant details (function names, logic, constraints).
27+
- Once distilled into your response history, the raw tool output can be safely pruned.
2328

24-
**After Pruning:**
25-
Do NOT re-summarize or re-narrate. You already distilled your findings before calling prune. Continue with your next task.
26-
27-
## When to Use This Tool
28-
29-
**Key Heuristic:** Distill, then prune when you finish something and are about to start something else.
30-
31-
1. **After a discrete unit of work**: Confirming a hypothesis, closing a branch of investigation, or finishing a feature.
32-
2. **After exploratory bursts**: When tool calls led to a clear conclusion (or to noise).
33-
3. **Before a new phase**: When old tool outputs are no longer needed for the next actions.
29+
## Best Practices
30+
- **Don't wait too long:** Prune frequently to keep the context agile.
31+
- **Be surgical:** You can mix strategies. Prune noise without comment, while distilling useful context in the same turn.
32+
- **Verify:** Ensure you have captured what you need before deleting useful raw data.
3433

3534
## Examples
3635

37-
<example>
38-
User: Review these 3 issues and fix the easy ones.
39-
Assistant: [Reviews first issue, makes fix, commits]
40-
Done with the first issue. Let me prune before moving to the next one.
41-
[Uses prune with ids: [1, 2, 3, 4]]
42-
</example>
43-
44-
<example>
45-
Assistant: I've reviewed the relevant files. Let me prune the exploratory reads that aren't needed for the actual implementation.
46-
[Uses prune with ids: [1, 2, 5, 7]]
47-
</example>
36+
<example_noise>
37+
Assistant: [Reads 'wrong_file.ts']
38+
This file isn't relevant to the auth system. I'll remove it to clear the context.
39+
[Uses prune with ids: [5]]
40+
</example_noise>
41+
42+
<example_consolidation>
43+
Assistant: [Reads 5 different config files]
44+
I have analyzed the configuration. Here is the distillation:
45+
- 'config.ts' uses port 3000.
46+
- 'db.ts' connects to mongo:27017.
47+
- The other 3 files were defaults.
48+
I have preserved the signals above, so I am now pruning the raw reads.
49+
[Uses prune with ids: [10, 11, 12, 13, 14]]
50+
</example_consolidation>
51+
52+
<example_completion>
53+
Assistant: [Runs tests, they pass]
54+
The tests passed. The feature is verified.
55+
[Uses prune with ids: [20, 21]]
56+
</example_completion>

0 commit comments

Comments
 (0)