forked from router-for-me/CLIProxyAPI
-
-
Notifications
You must be signed in to change notification settings - Fork 214
fix: filter out orphaned tool results from history and current context #269
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
luispater
merged 1 commit into
router-for-me:main
from
ClubWeGo:fix/filterOrphanedToolResults
Feb 24, 2026
Merged
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -578,6 +578,7 @@ func processOpenAIMessages(messages gjson.Result, modelID, origin string) ([]Kir | |
|
|
||
| // Truncate history if too long to prevent Kiro API errors | ||
| history = truncateHistoryIfNeeded(history) | ||
| history, currentToolResults = filterOrphanedToolResults(history, currentToolResults) | ||
|
|
||
| return history, currentUserMsg, currentToolResults | ||
| } | ||
|
|
@@ -593,6 +594,61 @@ func truncateHistoryIfNeeded(history []KiroHistoryMessage) []KiroHistoryMessage | |
| return history[len(history)-kiroMaxHistoryMessages:] | ||
| } | ||
|
|
||
| func filterOrphanedToolResults(history []KiroHistoryMessage, currentToolResults []KiroToolResult) ([]KiroHistoryMessage, []KiroToolResult) { | ||
| // Remove tool results with no matching tool_use in retained history. | ||
| // This happens after truncation when the assistant turn that produced tool_use | ||
| // is dropped but a later user/tool_result survives. | ||
| validToolUseIDs := make(map[string]bool) | ||
| for _, h := range history { | ||
| if h.AssistantResponseMessage == nil { | ||
| continue | ||
| } | ||
| for _, tu := range h.AssistantResponseMessage.ToolUses { | ||
| validToolUseIDs[tu.ToolUseID] = true | ||
| } | ||
| } | ||
|
|
||
| for i, h := range history { | ||
| if h.UserInputMessage == nil || h.UserInputMessage.UserInputMessageContext == nil { | ||
| continue | ||
| } | ||
| ctx := h.UserInputMessage.UserInputMessageContext | ||
| if len(ctx.ToolResults) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| filtered := make([]KiroToolResult, 0, len(ctx.ToolResults)) | ||
| for _, tr := range ctx.ToolResults { | ||
| if validToolUseIDs[tr.ToolUseID] { | ||
| filtered = append(filtered, tr) | ||
| continue | ||
| } | ||
| log.Debugf("kiro-openai: dropping orphaned tool_result in history[%d]: toolUseId=%s (no matching tool_use)", i, tr.ToolUseID) | ||
| } | ||
| ctx.ToolResults = filtered | ||
| if len(ctx.ToolResults) == 0 && len(ctx.Tools) == 0 { | ||
| h.UserInputMessage.UserInputMessageContext = nil | ||
| } | ||
| } | ||
|
|
||
| if len(currentToolResults) > 0 { | ||
| filtered := make([]KiroToolResult, 0, len(currentToolResults)) | ||
| for _, tr := range currentToolResults { | ||
| if validToolUseIDs[tr.ToolUseID] { | ||
| filtered = append(filtered, tr) | ||
| continue | ||
| } | ||
| log.Debugf("kiro-openai: dropping orphaned tool_result in currentMessage: toolUseId=%s (no matching tool_use)", tr.ToolUseID) | ||
| } | ||
| if len(filtered) != len(currentToolResults) { | ||
| log.Infof("kiro-openai: dropped %d orphaned tool_result(s) from currentMessage", len(currentToolResults)-len(filtered)) | ||
| } | ||
| currentToolResults = filtered | ||
|
Comment on lines
+635
to
+646
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the history filtering, originalLen := len(currentToolResults)
n := 0
for _, tr := range currentToolResults {
if validToolUseIDs[tr.ToolUseID] {
currentToolResults[n] = tr
n++
} else {
log.Debugf("kiro-openai: dropping orphaned tool_result in currentMessage: toolUseId=%s (no matching tool_use)", tr.ToolUseID)
}
}
if n < originalLen {
log.Infof("kiro-openai: dropped %d orphaned tool_result(s) from currentMessage", originalLen-n)
}
currentToolResults = currentToolResults[:n] |
||
| } | ||
|
|
||
| return history, currentToolResults | ||
| } | ||
|
|
||
| // buildUserMessageFromOpenAI builds a user message from OpenAI format and extracts tool results | ||
| func buildUserMessageFromOpenAI(msg gjson.Result, modelID, origin string) (KiroUserInputMessage, []KiroToolResult) { | ||
| content := msg.Get("content") | ||
|
|
||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better performance, you can filter the
ctx.ToolResultsslice in-place. This avoids allocating a newfilteredslice on each call, which is more memory-efficient, especially for long histories. This is a common Go idiom for slice filtering.