-
Notifications
You must be signed in to change notification settings - Fork 941
fix: preserve long tool output #1062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7b9e812
fix: persist truncated foreground bash output
kermanx 2689a45
fix: persist oversized tool results
kermanx 117768f
fix: link background task notifications to saved output
kermanx eb08e1c
fix: avoid lossy tool result budgeting
kermanx 3a1f942
fix
kermanx 1d37c7a
fix: include fallback task output previews
kermanx 8969da8
fix
kermanx 2f9a582
fix
kermanx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Preserve full tool output logs when previews are truncated and link background task completion notifications to saved output. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { randomUUID } from 'node:crypto'; | ||
| import { mkdir, writeFile } from 'node:fs/promises'; | ||
|
|
||
| import type { ContentPart } from '@moonshot-ai/kosong'; | ||
| import { join } from 'pathe'; | ||
|
|
||
| import type { ExecutableToolResult } from '../../loop'; | ||
|
|
||
| const TOOL_RESULT_MAX_CHARS = 50_000; | ||
| const TOOL_RESULT_PREVIEW_CHARS = 2_000; | ||
|
|
||
| interface BudgetToolResultOptions { | ||
| readonly homedir?: string; | ||
| readonly toolName: string; | ||
| readonly toolCallId: string; | ||
| readonly result: ExecutableToolResult; | ||
| } | ||
|
|
||
| export async function budgetToolResultForModel( | ||
| options: BudgetToolResultOptions, | ||
| ): Promise<ExecutableToolResult> { | ||
| const text = persistableToolResultText(options.result.output); | ||
| if (text === undefined || text.length <= TOOL_RESULT_MAX_CHARS) return options.result; | ||
| if (options.result.truncated === true) return options.result; | ||
| if (options.homedir === undefined) return options.result; | ||
|
|
||
| const outputPath = await saveToolResult( | ||
| { homedir: options.homedir, toolName: options.toolName, toolCallId: options.toolCallId }, | ||
| text, | ||
| ); | ||
| if (outputPath === undefined) return options.result; | ||
| const output = renderPersistedToolResult(options.toolName, options.toolCallId, text, outputPath); | ||
| return options.result.isError === true | ||
| ? { ...options.result, output, isError: true } | ||
| : { ...options.result, output }; | ||
| } | ||
|
|
||
| function persistableToolResultText(output: ExecutableToolResult['output']): string | undefined { | ||
| if (typeof output === 'string') return output; | ||
| if ( | ||
| !output.every((part): part is Extract<ContentPart, { type: 'text' }> => part.type === 'text') | ||
| ) { | ||
| return undefined; | ||
| } | ||
| return output.map((part) => part.text).join(''); | ||
| } | ||
|
|
||
| async function saveToolResult( | ||
| options: { readonly homedir: string; readonly toolName: string; readonly toolCallId: string }, | ||
| text: string, | ||
| ): Promise<string | undefined> { | ||
| try { | ||
| const dir = join(options.homedir, 'tool-results'); | ||
| await mkdir(dir, { recursive: true, mode: 0o700 }); | ||
| const outputPath = join( | ||
| dir, | ||
| `${safeToolResultFileStem(options.toolName, options.toolCallId)}-${randomUUID()}.txt`, | ||
| ); | ||
| await writeFile(outputPath, text, { encoding: 'utf8', flag: 'wx' }); | ||
| return outputPath; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| function renderPersistedToolResult( | ||
| toolName: string, | ||
| toolCallId: string, | ||
| text: string, | ||
| outputPath: string, | ||
| ): string { | ||
| const lines = [ | ||
| `Tool output exceeded ${String(TOOL_RESULT_MAX_CHARS)} characters; showing a preview only.`, | ||
| `tool_name: ${toolName}`, | ||
| `tool_call_id: ${toolCallId}`, | ||
| `output_size_chars: ${String(text.length)}`, | ||
| `output_size_bytes: ${String(Buffer.byteLength(text, 'utf8'))}`, | ||
| `output_path: ${outputPath}`, | ||
| 'next_step: Use Read with output_path to page through the full output.', | ||
| ]; | ||
| lines.push('', '[preview]', text.slice(0, TOOL_RESULT_PREVIEW_CHARS)); | ||
| return lines.join('\n'); | ||
| } | ||
|
|
||
| function safeToolResultFileStem(toolName: string, toolCallId: string): string { | ||
| const label = `${toolName}-${toolCallId}` | ||
| .replace(/[^a-zA-Z0-9._-]+/g, '_') | ||
| .replace(/^_+|_+$/g, '') | ||
| .slice(0, 80); | ||
| return label || 'tool-result'; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.