fix(tools): preserve bounded fallback guidance

This commit is contained in:
Tianyi Cui
2026-07-23 03:15:15 +08:00
parent 2821826e2c
commit 94adb60e1a
15 changed files with 144 additions and 21 deletions
+15 -4
View File
@@ -1057,9 +1057,15 @@ export class ToolRegistry extends Service {
* @internal
*/
private finishScheduledExecution(exec: ToolRunContext, result: ToolExecutionResult): ToolExecutionResult {
let snapshottedResult: ToolExecutionResult
try {
snapshottedResult = this.snapshotFinalResult(result)
} catch (error: unknown) {
snapshottedResult = toolErrorResult(error)
}
let finalResult: ToolExecutionResult
try {
finalResult = this.materializeFinalResult(this.applyFinalContent(exec, result))
finalResult = this.materializeFinalResult(this.applyFinalContent(exec, snapshottedResult))
} catch (error: unknown) {
finalResult = this.materializeFinalResult(toolErrorResult(error))
}
@@ -1187,13 +1193,18 @@ export class ToolRegistry extends Service {
}
}
/** Materialize the authoritative commit outcome once, immediately before `tools/result`. */
private materializeFinalResult(result: ToolExecutionResult): ToolExecutionResult {
/** Validate and detach one candidate outcome before tool-owned final content. */
private snapshotFinalResult(result: ToolExecutionResult): ToolExecutionResult {
const detached = snapshotJsonValue(result)
if (detached === undefined) {
throw new TypeError('tool result must be losslessly JSON-serializable')
}
return deepFreeze(detached)
return detached
}
/** Materialize the authoritative commit outcome once, immediately before `tools/result`. */
private materializeFinalResult(result: ToolExecutionResult): ToolExecutionResult {
return deepFreeze(this.snapshotFinalResult(result))
}
}