fix(core): guard message inspectors against empty parts arrays#28068
fix(core): guard message inspectors against empty parts arrays#28068AriaZhao-coder wants to merge 2 commits into
Conversation
`isFunctionCall()` and `isFunctionResponse()` in `messageInspectors.ts` returned `true` for messages with an empty `parts` array, because `[].every(...)` is vacuously `true` in JavaScript. This misclassified any `role: "model"` message with `parts: []` as a function call, and any `role: "user"` message with `parts: []` as a function response, affecting the routing, loop-detection, and chat subsystems that rely on these checks. Add a `parts.length > 0` guard to both functions so empty-parts messages are correctly treated as neither a function call nor a function response. Also add the previously missing test coverage for `messageInspectors.ts`. Fixes google-gemini#23195 Fixes google-gemini#22912
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a logic error in the message inspection utilities where empty parts arrays were causing false positives for function call and response classification. By enforcing a non-empty parts array requirement, the fix ensures more accurate message handling across the agent's subsystems. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
📊 PR Size: size/M
|
There was a problem hiding this comment.
Code Review
This pull request updates the isFunctionResponse and isFunctionCall utility functions in packages/core/src/utils/messageInspectors.ts to ensure they return false when the parts array is empty. Additionally, a new test file packages/core/src/utils/messageInspectors.test.ts has been added to thoroughly test these utilities. There are no review comments, and I have no feedback to provide.
Summary
isFunctionCall()andisFunctionResponse()inpackages/core/src/utils/messageInspectors.tsreturnedtruefor messages with an emptypartsarray, because[].every(...)is vacuouslytruein JavaScript.As a result:
role: "model"message withparts: []was misclassified as a function call, androle: "user"message withparts: []was misclassified as a function response.These helpers are consumed by the routing, loop-detection, and chat subsystems, so the false positives can propagate incorrect behavior across the agent.
Fix
Add a
parts.length > 0guard to both functions so empty-parts messages are correctly treated as neither a function call nor a function response.Tests
The file previously had no test coverage. Added
messageInspectors.test.tswith 12 tests covering both functions: valid calls/responses, wrong role, non-matching parts, mixed parts,undefinedparts, and the empty-array regression case.Verified locally (Node 20):
vitest run— new tests pass (12/12); affected callers (routing strategies, loop detection, nextSpeaker) still pass (131/131)prettier --check,eslint --max-warnings 0,tsc --noEmit— all cleanFixes #23195
Fixes #22912