aws / aws/amazon-q-developer-cli

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

Open
#3,715 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2k
Forks
439
PR merge metrics
No merged PRs in 30d

Description

## 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:

```rust
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:

```rust
// 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.