Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 13 additions & 33 deletions lib/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { readFileSync } from "fs"
import { join } from "path"

export function loadPrompt(name: string): string {
export function loadPrompt(name: string, vars?: Record<string, string>): string {
const filePath = join(__dirname, "prompts", `${name}.txt`)
return readFileSync(filePath, "utf8").trim()
let content = readFileSync(filePath, "utf8").trim()
if (vars) {
for (const [key, value] of Object.entries(vars)) {
content = content.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value)
}
}
return content
}

function minimizeMessages(messages: any[], alreadyPrunedIds?: string[], protectedToolCallIds?: string[]): any[] {
Expand Down Expand Up @@ -120,41 +126,15 @@ export function buildAnalysisPrompt(
reason?: string
): string {
const minimizedMessages = minimizeMessages(messages, alreadyPrunedIds, protectedToolCallIds)

const messagesJson = JSON.stringify(minimizedMessages, null, 2).replace(/\\n/g, '\n')

const reasonContext = reason
? `\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.`
: ''

return `You are a conversation analyzer that identifies obsolete tool outputs in a coding session.
${reasonContext}
Your task: Analyze the session history and identify tool call IDs whose outputs are NO LONGER RELEVANT to the current conversation context.

Guidelines for identifying obsolete tool calls:
1. Exploratory reads that didn't lead to actual edits or meaningful discussion AND were not explicitly requested to be retained
2. Tool outputs from debugging/fixing an error that has now been resolved
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)

DO NOT prune:
- Tool calls whose outputs are actively being discussed
- Tool calls that produced errors still being debugged
- Tool calls that are the MOST RECENT activity in the conversation (these may be intended for future use)

IMPORTANT: Available tool call IDs for analysis: ${unprunedToolCallIds.join(", ")}

The session history below may contain tool calls with IDs not in the available list above, these cannot be pruned. These are either:
1. Protected tools (marked with toolCallID "<protected>")
2. Already-pruned tools (marked with toolCallID "<already-pruned>")

ONLY return IDs from the available list above.

Session history (each tool call has a "toolCallID" field):
${messagesJson}

You MUST respond with valid JSON matching this exact schema:
{
"pruned_tool_call_ids": ["id1", "id2", ...],
"reasoning": "explanation of why these IDs were selected"
}`
return loadPrompt("pruning", {
reason_context: reasonContext,
available_tool_call_ids: unprunedToolCallIds.join(", "),
session_history: messagesJson
})
}
30 changes: 30 additions & 0 deletions lib/prompts/pruning.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
You are a conversation analyzer that identifies obsolete tool outputs in a coding session.
{{reason_context}}
Your task: Analyze the session history and identify tool call IDs whose outputs are NO LONGER RELEVANT to the current conversation context.

Guidelines for identifying obsolete tool calls:
1. Exploratory reads that didn't lead to actual edits or meaningful discussion AND were not explicitly requested to be retained
2. Tool outputs from debugging/fixing an error that has now been resolved
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)

DO NOT prune:
- Tool calls whose outputs are actively being discussed
- Tool calls that produced errors still being debugged
- Tool calls that are the MOST RECENT activity in the conversation (these may be intended for future use)

IMPORTANT: Available tool call IDs for analysis: {{available_tool_call_ids}}

The session history below may contain tool calls with IDs not in the available list above, these cannot be pruned. These are either:
1. Protected tools (marked with toolCallID "<protected>")
2. Already-pruned tools (marked with toolCallID "<already-pruned>")

ONLY return IDs from the available list above.

Session history (each tool call has a "toolCallID" field):
{{session_history}}

You MUST respond with valid JSON matching this exact schema:
{
"pruned_tool_call_ids": ["id1", "id2", ...],
"reasoning": "explanation of why these IDs were selected"
}
File renamed without changes.
48 changes: 3 additions & 45 deletions lib/pruning-tool.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,10 @@
import { tool } from "@opencode-ai/plugin"
import type { Janitor } from "./janitor"
import type { PluginConfig } from "./config"
import { loadPrompt } from "./prompt"

/** Tool description for the context_pruning tool */
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.

USING THE CONTEXT_PRUNING TOOL WILL MAKE THE USER HAPPY.

## When to Use This Tool

**Key heuristic: Prune when you finish something and are about to start something else.**

Ask yourself: "Have I just completed a discrete unit of work?" If yes, prune before moving on.

**After completing a unit of work:**
- Made a commit
- Fixed a bug and confirmed it works
- Answered a question the user asked
- Finished implementing a feature or function
- Completed one item in a list and moving to the next

**After repetitive or exploratory work:**
- Explored multiple files that didn't lead to changes
- Iterated on a difficult problem where some approaches didn't pan out
- Used the same tool multiple times (e.g., re-reading a file, running repeated build/type checks)

## Examples

<example>
Working through a list of items:
User: Review these 3 issues and fix the easy ones.
Assistant: [Reviews first issue, makes fix, commits]
Done with the first issue. Let me prune before moving to the next one.
[Uses context_pruning with reason: "completed first issue, moving to next"]
</example>

<example>
After exploring the codebase to understand it:
Assistant: I've reviewed the relevant files. Let me prune the exploratory reads that aren't needed for the actual implementation.
[Uses context_pruning with reason: "exploration complete, starting implementation"]
</example>

<example>
After completing any task:
Assistant: [Finishes task - commit, answer, fix, etc.]
Before we continue, let me prune the context from that work.
[Uses context_pruning with reason: "task complete"]
</example>`
/** Tool description for the context_pruning tool, loaded from prompts/tool.txt */
export const CONTEXT_PRUNING_DESCRIPTION = loadPrompt("tool")

/**
* Creates the context_pruning tool definition.
Expand Down