Skip to content

Commit 9170831

Browse files
authored
Merge pull request #277 from Opencode-DCP/fix/prune-tool-error-handling
fix: throw errors on prune tool failures instead of returning strings
2 parents db09411 + 6239897 commit 9170831

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

lib/logger.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,18 @@ export class Logger {
168168
callID: part.callID,
169169
}
170170

171+
if (part.state?.status) {
172+
toolPart.status = part.state.status
173+
}
171174
if (part.state?.input) {
172175
toolPart.input = part.state.input
173176
}
174177
if (part.state?.output) {
175178
toolPart.output = part.state.output
176179
}
180+
if (part.state?.error) {
181+
toolPart.error = part.state.error
182+
}
177183

178184
return toolPart
179185
}

lib/state/tool-cache.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export async function syncToolCache(
4242
turnProtectionTurns > 0 &&
4343
state.currentTurn - turnCounter < turnProtectionTurns
4444

45-
state.lastToolPrune = part.tool === "discard" || part.tool === "extract"
45+
state.lastToolPrune =
46+
(part.tool === "discard" || part.tool === "extract") &&
47+
part.state.status === "completed"
4648

4749
const allProtectedTools = config.tools.settings.protectedTools
4850

lib/strategies/tools.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ async function executePruneOperation(
3939

4040
if (!ids || ids.length === 0) {
4141
logger.debug(`${toolName} tool called but ids is empty or undefined`)
42-
return `No IDs provided. Check the <prunable-tools> list for available IDs to ${toolName.toLowerCase()}.`
42+
throw new Error(
43+
`No IDs provided. Check the <prunable-tools> list for available IDs to ${toolName.toLowerCase()}.`,
44+
)
4345
}
4446

4547
const numericToolIds: number[] = ids
@@ -48,7 +50,7 @@ async function executePruneOperation(
4850

4951
if (numericToolIds.length === 0) {
5052
logger.debug(`No numeric tool IDs provided for ${toolName}: ` + JSON.stringify(ids))
51-
return "No numeric IDs provided. Format: ids: [id1, id2, ...]"
53+
throw new Error("No numeric IDs provided. Format: ids: [id1, id2, ...]")
5254
}
5355

5456
// Fetch messages to calculate tokens and find current agent
@@ -65,7 +67,9 @@ async function executePruneOperation(
6567
// Validate that all numeric IDs are within bounds
6668
if (numericToolIds.some((id) => id < 0 || id >= toolIdList.length)) {
6769
logger.debug("Invalid tool IDs provided: " + numericToolIds.join(", "))
68-
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
70+
throw new Error(
71+
"Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list.",
72+
)
6973
}
7074

7175
// Validate that all IDs exist in cache and aren't protected
@@ -78,7 +82,9 @@ async function executePruneOperation(
7882
"Rejecting prune request - ID not in cache (turn-protected or hallucinated)",
7983
{ index, id },
8084
)
81-
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
85+
throw new Error(
86+
"Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list.",
87+
)
8288
}
8389
const allProtectedTools = config.tools.settings.protectedTools
8490
if (allProtectedTools.includes(metadata.tool)) {
@@ -87,7 +93,9 @@ async function executePruneOperation(
8793
id,
8894
tool: metadata.tool,
8995
})
90-
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
96+
throw new Error(
97+
"Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list.",
98+
)
9199
}
92100

93101
const filePath = getFilePathFromParameters(metadata.parameters)
@@ -98,7 +106,9 @@ async function executePruneOperation(
98106
tool: metadata.tool,
99107
filePath,
100108
})
101-
return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list."
109+
throw new Error(
110+
"Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list.",
111+
)
102112
}
103113
}
104114

@@ -158,7 +168,9 @@ export function createDiscardTool(ctx: PruneToolContext): ReturnType<typeof tool
158168
const validReasons = ["completion", "noise"] as const
159169
if (typeof reason !== "string" || !validReasons.includes(reason as any)) {
160170
ctx.logger.debug("Invalid discard reason provided: " + reason)
161-
return "No valid reason found. Use 'completion' or 'noise' as the first element."
171+
throw new Error(
172+
"No valid reason found. Use 'completion' or 'noise' as the first element.",
173+
)
162174
}
163175

164176
const numericIds = args.ids.slice(1)
@@ -186,7 +198,9 @@ export function createExtractTool(ctx: PruneToolContext): ReturnType<typeof tool
186198
ctx.logger.debug(
187199
"Extract tool called without distillation: " + JSON.stringify(args),
188200
)
189-
return "Missing distillation. You must provide a distillation string for each ID."
201+
throw new Error(
202+
"Missing distillation. You must provide a distillation string for each ID.",
203+
)
190204
}
191205

192206
// Log the distillation for debugging/analysis

0 commit comments

Comments
 (0)