Skip to content

panic: byte index out of bounds in chat response rendering when response contains triple backticks #3715

@sandikodev

Description

@sandikodev

Bug Report

Version: kiro-cli 1.25.1
OS: Debian GNU/Linux 12 (bookworm), kernel 6.1.0-44-amd64, x86_64
Install method: Official install script from kiro.dev (curl)
Binary location: /home/dev/.local/bin/kiro-cli

Description

The application panics with a byte index out of bounds error when the AI response contains triple backticks (```````) in the rendered output.

Steps to Reproduce

  1. Start a chat session: kiro-cli chat
  2. Ask a question that causes the AI to respond with a message containing triple backticks (e.g., a code block or a message referencing backtick syntax)
  3. The application crashes mid-response

Panic Output

The application panicked (crashed).
Message:  byte index 536 is out of bounds of `...`[...]
Location: crates/chat-cli/src/cli/chat/mod.rs:3849

Root Cause Analysis

In crates/chat-cli/src/cli/chat/mod.rs, the response rendering loop uses a byte offset to track parsing progress:

let input = Partial::new(&buf[offset..]);
// ...
offset += parsed.offset_from(&input);

parsed.offset_from(&input) returns a byte count relative to the input slice. This is accumulated into offset, which is used as a byte index into buf. When buf contains multi-byte UTF-8 characters (e.g., non-ASCII text, emoji, or characters adjacent to triple backticks), the accumulated offset can land in the middle of a multi-byte character boundary, causing the panic on the next iteration when &buf[offset..] is evaluated.

Suggested Fix

Validate that offset falls on a valid UTF-8 character boundary before slicing, or use buf.get(offset..) which returns None instead of panicking:

// Instead of:
let input = Partial::new(&buf[offset..]);

// Use:
let Some(slice) = buf.get(offset..) else { break };
let input = Partial::new(slice);

Alternatively, ensure offset is always advanced using ceil_char_boundary or equivalent to stay on valid UTF-8 boundaries.

Additional Context

This panic is reproducible when the AI response includes triple backticks in its text content (not just code blocks), particularly when the surrounding text contains non-ASCII characters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions