fix(agent-core): handle incomplete tool_calls on session resume (#660)#664
fix(agent-core): handle incomplete tool_calls on session resume (#660)#664LifeJiggy wants to merge 2 commits into
Conversation
🦋 Changeset detectedLatest commit: c066cba The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
f9a4217 to
1d681f1
Compare
When a session is killed mid-tool-call and later resumed, the history contains an assistant message with tool_calls but no matching tool results. This caused a 400 API error and silently deferred all new user messages. Fixes MoonshotAI#660. Changes: - project() now applies trimTrailingOpenToolExchange() to strip trailing assistant messages with unanswered tool_calls before sending to the LLM - Added cleanupOrphanedToolCalls() to ContextMemory that clears stale pendingToolResultIds after resume, so new user messages are not silently deferred - Fixed trimTrailingOpenToolExchange edge case where history with no assistant message returned [] instead of the full history Tests: - project() trims trailing assistant with unanswered tool_calls - project() keeps assistant when all tool_calls are answered - cleanupOrphanedToolCalls clears stale pendingToolResultIds
- Add missing type: 'function' to ToolCall objects in test - Fix LoopToolCallEvent shape (use stepUuid instead of parentUuid, add uuid/turnId/step/args) - Add changeset for agent-core and kimi-code patch bumps
1d681f1 to
c066cba
Compare
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c066cba25e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| for (const id of this.pendingToolResultIds) { | ||
| if (!answeredIds.has(id)) { | ||
| this.pendingToolResultIds.delete(id); | ||
| } |
There was a problem hiding this comment.
Drop orphaned assistant messages before accepting prompts
When resuming a session killed after a tool.call, this cleanup clears pendingToolResultIds but leaves the assistant message with unanswered toolCalls in _history; the next user prompt is then appended after that assistant, so project() no longer treats the open tool exchange as trailing and sends [assistant tool_calls, user] to the provider, reproducing the 400 this change is meant to avoid. In the resume-after-interrupt path, clear or trim the orphaned assistant/tool exchange from history before allowing new messages through.
Useful? React with 👍 / 👎.
Related Issue
Resolve #660
Problem
When a session is killed mid-tool-call (OOM, force kill) and later resumed, the history contains an assistant message with tool_calls but no matching tool result messages. This caused:
What changed
Checklist