gemini-cli - ✅(Solved) Fix bug: A2A pushMessage accesses array[-1] when messageLog is empty [2 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
google-gemini/gemini-cli#24894Fetched 2026-04-09 08:17:45
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Author
Participants
Timeline (top)
cross-referenced ×2labeled ×2

Fix Action

Fixed

PR fix notes

PR #24882: fix(core): add empty array guard in A2A duplicate message check

Description (problem / solution / changelog)

Fixes #24894

Summary

A2AResultReassembler.pushMessage checks messageLog[messageLog.length - 1] to avoid pushing duplicate consecutive messages. When messageLog is empty, length - 1 is -1 and array[-1] returns undefined in JavaScript.

While the current behavior happens to work correctly (since undefined !== text evaluates to true, the first message still gets pushed), the intent of the duplicate check is clearer with an explicit empty-array guard.

Changes

  • Add this.messageLog.length === 0 check before accessing the last element

Changed files

  • packages/core/src/agents/a2aUtils.ts (modified, +5/-1)

PR #24897: fix(core): add empty array guard in A2A duplicate message check

Description (problem / solution / changelog)

Fixes #24894

Summary

A2AResultReassembler.pushMessage checks messageLog[messageLog.length - 1] to avoid pushing duplicate consecutive messages. When messageLog is empty, length - 1 is -1 and array[-1] returns undefined in JavaScript.

While the current behavior happens to work correctly (since undefined !== text evaluates to true, the first message still gets pushed), the intent of the duplicate check is clearer with an explicit empty-array guard.

Changes

  • Add this.messageLog.length === 0 check before accessing the last element

Test plan

  • Verified current behavior with empty array
  • Confirmed guard makes intent explicit without changing behavior

Changed files

  • packages/core/src/agents/a2aUtils.ts (modified, +5/-1)
RAW_BUFFERClick to expand / collapse

A2AResultReassembler.pushMessage in packages/core/src/agents/a2aUtils.ts checks messageLog[messageLog.length - 1] to avoid pushing duplicate consecutive messages. When messageLog is empty, length - 1 is -1 and array[-1] returns undefined in JavaScript.

While the current behavior happens to work correctly (since undefined !== text evaluates to true, the first message still gets pushed), the code relies on an implicit JavaScript quirk rather than expressing intent clearly. An explicit empty-array guard makes the duplicate check self-documenting and prevents potential issues if the comparison logic changes in the future.

Steps to reproduce

  1. Create a new A2AResultReassembler instance
  2. Call pushMessage with any text
  3. Observe that messageLog[-1] is accessed (returns undefined)

Expected behavior

An explicit length check should guard the last-element access.

Actual behavior

array[-1] is accessed, which returns undefined — works by coincidence, not by design.

extent analysis

TL;DR

Add an explicit check for an empty messageLog array before accessing its last element to prevent relying on implicit JavaScript behavior.

Guidance

  • Introduce a conditional statement to check if messageLog is empty before attempting to access its last element.
  • Modify the pushMessage method in a2aUtils.ts to include this check, ensuring the code's intent is clear and less prone to future issues.
  • Verify the fix by creating a new A2AResultReassembler instance, calling pushMessage with any text, and confirming that the messageLog is correctly handled when empty.
  • Consider adding a test case to cover this scenario and prevent regressions.

Example

if (messageLog.length > 0 && messageLog[messageLog.length - 1] === text) {
  // Handle duplicate message
} else {
  // Push the message
  messageLog.push(text);
}

Notes

This fix assumes that the intention is to prevent pushing duplicate consecutive messages. If the logic for handling duplicates changes, this fix may need to be adjusted accordingly.

Recommendation

Apply a workaround by adding an explicit empty-array guard to make the duplicate check self-documenting and prevent potential issues. This approach ensures the code's behavior is intentional and less reliant on JavaScript quirks.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

An explicit length check should guard the last-element access.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING