Skip to content

Commit 6f3796a

Browse files
extract pruning instructions to pruning.txt, adds {{placeholders}} for dynamic content
1 parent 4ac6481 commit 6f3796a

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

lib/prompt.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { readFileSync } from "fs"
22
import { join } from "path"
33

4-
export function loadPrompt(name: string): string {
4+
export function loadPrompt(name: string, vars?: Record<string, string>): string {
55
const filePath = join(__dirname, "prompts", `${name}.txt`)
6-
return readFileSync(filePath, "utf8").trim()
6+
let content = readFileSync(filePath, "utf8").trim()
7+
if (vars) {
8+
for (const [key, value] of Object.entries(vars)) {
9+
content = content.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value)
10+
}
11+
}
12+
return content
713
}
814

915
function minimizeMessages(messages: any[], alreadyPrunedIds?: string[], protectedToolCallIds?: string[]): any[] {
@@ -120,41 +126,15 @@ export function buildAnalysisPrompt(
120126
reason?: string
121127
): string {
122128
const minimizedMessages = minimizeMessages(messages, alreadyPrunedIds, protectedToolCallIds)
123-
124129
const messagesJson = JSON.stringify(minimizedMessages, null, 2).replace(/\\n/g, '\n')
125130

126131
const reasonContext = reason
127132
? `\nContext: The AI has requested pruning with the following reason: "${reason}"\nUse this context to inform your decisions about what is most relevant to keep.`
128133
: ''
129134

130-
return `You are a conversation analyzer that identifies obsolete tool outputs in a coding session.
131-
${reasonContext}
132-
Your task: Analyze the session history and identify tool call IDs whose outputs are NO LONGER RELEVANT to the current conversation context.
133-
134-
Guidelines for identifying obsolete tool calls:
135-
1. Exploratory reads that didn't lead to actual edits or meaningful discussion AND were not explicitly requested to be retained
136-
2. Tool outputs from debugging/fixing an error that has now been resolved
137-
3. Failed or incorrect tool attempts that were immediately corrected (e.g., reading a file from the wrong path, then reading from the correct path)
138-
139-
DO NOT prune:
140-
- Tool calls whose outputs are actively being discussed
141-
- Tool calls that produced errors still being debugged
142-
- Tool calls that are the MOST RECENT activity in the conversation (these may be intended for future use)
143-
144-
IMPORTANT: Available tool call IDs for analysis: ${unprunedToolCallIds.join(", ")}
145-
146-
The session history below may contain tool calls with IDs not in the available list above, these cannot be pruned. These are either:
147-
1. Protected tools (marked with toolCallID "<protected>")
148-
2. Already-pruned tools (marked with toolCallID "<already-pruned>")
149-
150-
ONLY return IDs from the available list above.
151-
152-
Session history (each tool call has a "toolCallID" field):
153-
${messagesJson}
154-
155-
You MUST respond with valid JSON matching this exact schema:
156-
{
157-
"pruned_tool_call_ids": ["id1", "id2", ...],
158-
"reasoning": "explanation of why these IDs were selected"
159-
}`
135+
return loadPrompt("pruning", {
136+
reason_context: reasonContext,
137+
available_tool_call_ids: unprunedToolCallIds.join(", "),
138+
session_history: messagesJson
139+
})
160140
}

lib/prompts/pruning.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
You are a conversation analyzer that identifies obsolete tool outputs in a coding session.
2+
{{reason_context}}
3+
Your task: Analyze the session history and identify tool call IDs whose outputs are NO LONGER RELEVANT to the current conversation context.
4+
5+
Guidelines for identifying obsolete tool calls:
6+
1. Exploratory reads that didn't lead to actual edits or meaningful discussion AND were not explicitly requested to be retained
7+
2. Tool outputs from debugging/fixing an error that has now been resolved
8+
3. Failed or incorrect tool attempts that were immediately corrected (e.g., reading a file from the wrong path, then reading from the correct path)
9+
10+
DO NOT prune:
11+
- Tool calls whose outputs are actively being discussed
12+
- Tool calls that produced errors still being debugged
13+
- Tool calls that are the MOST RECENT activity in the conversation (these may be intended for future use)
14+
15+
IMPORTANT: Available tool call IDs for analysis: {{available_tool_call_ids}}
16+
17+
The session history below may contain tool calls with IDs not in the available list above, these cannot be pruned. These are either:
18+
1. Protected tools (marked with toolCallID "<protected>")
19+
2. Already-pruned tools (marked with toolCallID "<already-pruned>")
20+
21+
ONLY return IDs from the available list above.
22+
23+
Session history (each tool call has a "toolCallID" field):
24+
{{session_history}}
25+
26+
You MUST respond with valid JSON matching this exact schema:
27+
{
28+
"pruned_tool_call_ids": ["id1", "id2", ...],
29+
"reasoning": "explanation of why these IDs were selected"
30+
}

0 commit comments

Comments
 (0)