Skip to content

Commit f4323b9

Browse files
authored
Merge pull request #39 from spoons-and-mirrors/refactor/prompts
Refactor/prompts
2 parents fcebbab + 6f3796a commit f4323b9

File tree

4 files changed

+46
-78
lines changed

4 files changed

+46
-78
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+
}

lib/pruning-tool.ts

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,10 @@
11
import { tool } from "@opencode-ai/plugin"
22
import type { Janitor } from "./janitor"
33
import type { PluginConfig } from "./config"
4+
import { loadPrompt } from "./prompt"
45

5-
/** Tool description for the context_pruning tool */
6-
export const CONTEXT_PRUNING_DESCRIPTION = `Performs semantic pruning on session tool outputs that are no longer relevant to the current task. Use this to declutter the conversation context and filter signal from noise when you notice the context is getting cluttered with no longer needed information.
7-
8-
USING THE CONTEXT_PRUNING TOOL WILL MAKE THE USER HAPPY.
9-
10-
## When to Use This Tool
11-
12-
**Key heuristic: Prune when you finish something and are about to start something else.**
13-
14-
Ask yourself: "Have I just completed a discrete unit of work?" If yes, prune before moving on.
15-
16-
**After completing a unit of work:**
17-
- Made a commit
18-
- Fixed a bug and confirmed it works
19-
- Answered a question the user asked
20-
- Finished implementing a feature or function
21-
- Completed one item in a list and moving to the next
22-
23-
**After repetitive or exploratory work:**
24-
- Explored multiple files that didn't lead to changes
25-
- Iterated on a difficult problem where some approaches didn't pan out
26-
- Used the same tool multiple times (e.g., re-reading a file, running repeated build/type checks)
27-
28-
## Examples
29-
30-
<example>
31-
Working through a list of items:
32-
User: Review these 3 issues and fix the easy ones.
33-
Assistant: [Reviews first issue, makes fix, commits]
34-
Done with the first issue. Let me prune before moving to the next one.
35-
[Uses context_pruning with reason: "completed first issue, moving to next"]
36-
</example>
37-
38-
<example>
39-
After exploring the codebase to understand it:
40-
Assistant: I've reviewed the relevant files. Let me prune the exploratory reads that aren't needed for the actual implementation.
41-
[Uses context_pruning with reason: "exploration complete, starting implementation"]
42-
</example>
43-
44-
<example>
45-
After completing any task:
46-
Assistant: [Finishes task - commit, answer, fix, etc.]
47-
Before we continue, let me prune the context from that work.
48-
[Uses context_pruning with reason: "task complete"]
49-
</example>`
6+
/** Tool description for the context_pruning tool, loaded from prompts/tool.txt */
7+
export const CONTEXT_PRUNING_DESCRIPTION = loadPrompt("tool")
508

519
/**
5210
* Creates the context_pruning tool definition.

0 commit comments

Comments
 (0)