Skip to content

Commit e1ec087

Browse files
committed
refactor: move prune tool ID validation to prune-tool.ts
1 parent f4ba0c1 commit e1ec087

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

lib/messages/utils.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,4 @@ export function buildToolIdList(messages: WithParts[]): string[] {
101101
}
102102
}
103103
return toolIds
104-
}
105-
106-
export function getPruneToolIds(
107-
numericToolIds: number[],
108-
toolIdList: string[],
109-
toolParameters: Map<string, { tool: string }>,
110-
protectedTools: string[]
111-
): string[] {
112-
const pruneToolIds: string[] = []
113-
for (const index of numericToolIds) {
114-
if (!isNaN(index) && index >= 0 && index < toolIdList.length) {
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)
121-
}
122-
}
123-
return pruneToolIds
124-
}
104+
}

lib/strategies/prune-tool.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { tool } from "@opencode-ai/plugin"
22
import type { SessionState, ToolParameterEntry, WithParts } from "../state"
33
import type { PluginConfig } from "../config"
4-
import { findCurrentAgent, buildToolIdList, getPruneToolIds } from "../messages/utils"
4+
import { findCurrentAgent, buildToolIdList } from "../messages/utils"
55
import { calculateTokensSaved } from "../utils"
66
import { PruneReason, sendUnifiedNotification } from "../ui/notification"
77
import { formatPruningResultForTool } from "../ui/display-utils"
@@ -70,12 +70,23 @@ export function createPruneTool(
7070

7171
const currentAgent: string | undefined = findCurrentAgent(messages)
7272
const toolIdList: string[] = buildToolIdList(messages)
73-
const pruneToolIds: string[] = getPruneToolIds(
74-
numericToolIds,
75-
toolIdList,
76-
state.toolParameters,
77-
config.strategies.pruneTool.protectedTools
78-
)
73+
74+
// Validate that all numeric IDs are within bounds
75+
const invalidIds = numericToolIds.filter(id => id < 0 || id >= toolIdList.length)
76+
if (invalidIds.length > 0) {
77+
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
78+
}
79+
80+
// Check for protected tools (model hallucinated an ID not in the prunable list)
81+
for (const index of numericToolIds) {
82+
const id = toolIdList[index]
83+
const metadata = state.toolParameters.get(id)
84+
if (metadata && config.strategies.pruneTool.protectedTools.includes(metadata.tool)) {
85+
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
86+
}
87+
}
88+
89+
const pruneToolIds: string[] = numericToolIds.map(index => toolIdList[index])
7990
state.prune.toolIds.push(...pruneToolIds)
8091

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

0 commit comments

Comments
 (0)