Skip to content

Commit f4ba0c1

Browse files
committed
fix: prevent protected tools from being pruned
1 parent cecaff1 commit f4ba0c1

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

lib/messages/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,21 @@ export function buildToolIdList(messages: WithParts[]): string[] {
103103
return toolIds
104104
}
105105

106-
export function getPruneToolIds(numericToolIds: number[], toolIdList: string[]): string[] {
106+
export function getPruneToolIds(
107+
numericToolIds: number[],
108+
toolIdList: string[],
109+
toolParameters: Map<string, { tool: string }>,
110+
protectedTools: string[]
111+
): string[] {
107112
const pruneToolIds: string[] = []
108113
for (const index of numericToolIds) {
109114
if (!isNaN(index) && index >= 0 && index < toolIdList.length) {
110-
pruneToolIds.push(toolIdList[index])
115+
const id = toolIdList[index]
116+
const metadata = toolParameters.get(id)
117+
if (metadata && protectedTools.includes(metadata.tool)) {
118+
continue
119+
}
120+
pruneToolIds.push(id)
111121
}
112122
}
113123
return pruneToolIds

lib/prompts/tool.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Prunes tool outputs from context to manage conversation size and reduce noise.
22

33
## IMPORTANT: The Prunable List
4-
A `<prunable-tools>` list is injected into user messages showing available tool outputs you can prune. Each line has the format `ID: tool, parameter` (e.g., `20: read, /path/to/file.ts`). Use these numeric IDs to select which tools to prune.
4+
A `<prunable-tools>` list is injected into user messages showing available tool outputs you can prune. Each line has the format `ID: tool, parameter` (e.g., `20: read, /path/to/file.ts`). You MUST only use numeric IDs that appear in this list to select which tools to prune.
55

66
## CRITICAL: When and How to Prune
77

lib/strategies/prune-tool.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ export function createPruneTool(
7070

7171
const currentAgent: string | undefined = findCurrentAgent(messages)
7272
const toolIdList: string[] = buildToolIdList(messages)
73-
const pruneToolIds: string[] = getPruneToolIds(numericToolIds, toolIdList)
73+
const pruneToolIds: string[] = getPruneToolIds(
74+
numericToolIds,
75+
toolIdList,
76+
state.toolParameters,
77+
config.strategies.pruneTool.protectedTools
78+
)
7479
state.prune.toolIds.push(...pruneToolIds)
7580

7681
const toolMetadata = new Map<string, ToolParameterEntry>()

0 commit comments

Comments
 (0)