Skip to content

fix(gateway): guard against non-iterable messages in tool-calling.ts#2325

Open
kilo-code-bot[bot] wants to merge 3 commits intomainfrom
fix/messages-not-iterable
Open

fix(gateway): guard against non-iterable messages in tool-calling.ts#2325
kilo-code-bot[bot] wants to merge 3 commits intomainfrom
fix/messages-not-iterable

Conversation

@kilo-code-bot
Copy link
Copy Markdown
Contributor

@kilo-code-bot kilo-code-bot bot commented Apr 11, 2026

Summary

Fixes Sentry error TypeError: e.messages is not iterable in kilocode-web, occurring in apps/web/src/lib/tool-calling.ts during POST /api/gateway/[...path].

The API gateway route handler parses incoming request bodies with JSON.parse and TypeScript type assertions only — there is no runtime validation that the messages field exists and is an array. When a client sends a chat completions request without a messages array, normalizeToolCallIds crashes when iterating over it with for...of.

Two changes:

  • Route handler (route.ts): Validates messages is an array for chat_completions and messages request kinds, returning 400 early if not. This matches the existing validation pattern for the model field.
  • normalizeToolCallIds (tool-calling.ts): Added Array.isArray guard before for...of iteration, matching the existing pattern used by repairTools in the same file.

Verification

  • Confirmed the Sentry stacktrace points to tool-calling.ts in POST /api/gateway/[...path]
  • Verified the Array.isArray guard matches the existing pattern in repairTools (same file, line 82-84)
  • Confirmed the route handler validation is consistent with existing model field validation

Visual Changes

N/A

Reviewer Notes

  • repairTools (same file) already has if (!Array.isArray(requestToMutate.messages)) { return; } — the same guard is now applied to normalizeToolCallIds.
  • The route handler validation catches the issue earlier and returns a proper 400 error instead of an unhandled exception.

Built for Christiaan Arnoldus by Kilo for Slack

Clients can send requests without a messages array, causing
TypeError: e.messages is not iterable at runtime. The route handler
parses bodies with JSON.parse + type assertions only, so messages
is never validated.

Add Array.isArray guard in the route handler (returns 400) and
defense-in-depth guards in all downstream functions that iterate
over messages.
// Validate that messages is a non-empty array for request kinds that require it
if (
(requestBodyParsed.kind === 'chat_completions' || requestBodyParsed.kind === 'messages') &&
!Array.isArray(requestBodyParsed.body.messages)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Array-only validation still allows malformed message entries

This guard only checks that messages is an array. Requests like {"messages":[null]} or {"messages":[{"role":"user"}]} still pass here and can still throw later when downstream code reads m.role or calls content.filter(...) in the prompt-extraction helpers. Because this route is still parsing raw JSON without schema validation, it needs to reject non-object / non-conforming message items as well to fully prevent the runtime 500s this PR is targeting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid concern for a follow-up, but out of scope for this PR. The Sentry error is specifically TypeError: e.messages is not iterable — meaning messages is undefined/null/non-array, not that individual items within the array are malformed. Adding full Zod schema validation for the entire request body would be a larger effort that should be done separately. The Array.isArray guard matches the existing pattern used by repairTools in the same file and the addCacheBreakpoints function in request-helpers.ts.

@kilo-code-bot
Copy link
Copy Markdown
Contributor Author

kilo-code-bot bot commented Apr 11, 2026

Code Review Summary

Status: 1 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0

Fix these issues in Kilo Cloud

Issue Details (click to expand)

WARNING

File Line Issue
apps/web/src/app/api/openrouter/[...path]/route.ts 202 messages is only checked with Array.isArray, so malformed entries can still reach downstream helpers and trigger runtime errors.
Other Observations (not in diff)

No additional issues found outside the diff. The previously reported route-level validation gap remains unresolved in unchanged code.

Files Reviewed (1 files)
  • apps/web/src/routers/kilo-pass-router.test.ts - 0 issues

Reviewed by gpt-5.4-20260305 · 371,452 tokens

…cktrace

The Sentry issue points specifically to tool-calling.ts in the
POST /api/gateway/[...path] handler. Reverted defensive guards
in unrelated files.
@kilo-code-bot kilo-code-bot bot changed the title fix(gateway): guard against non-iterable messages in request body fix(gateway): guard against non-iterable messages in tool-calling.ts Apr 11, 2026
The isEligibleForFirstMonthPromo tests hardcoded 'true' but the
promo cutoff (2026-04-11T06:59:59Z) has passed, causing CI to fail.
Use the cutoff constant to compute the expected value dynamically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant