From 9249d9e358ce8e3e4239058e96e3c319beaf07f6 Mon Sep 17 00:00:00 2001 From: Luan Vu Date: Mon, 9 Mar 2026 15:49:25 +0700 Subject: [PATCH] fix(claude-cli): check scanner.Err() and increase stream buffer to 10MB (#96) Fixes #94. The stream-json scanner never checked scanner.Err() after the scan loop, so if a line exceeded the 1MB buffer limit the scanner would stop with bufio.ErrTooLong and the response would be silently truncated. - Check scanner.Err() after the loop and return an explicit error - Increase max buffer from 1MB to 10MB to handle large tool outputs Co-authored-by: Luvu182 <208665161+Luvu182@users.noreply.github.com> --- internal/providers/claude_cli_chat.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/providers/claude_cli_chat.go b/internal/providers/claude_cli_chat.go index 8acc8025..7c8271f6 100644 --- a/internal/providers/claude_cli_chat.go +++ b/internal/providers/claude_cli_chat.go @@ -138,7 +138,7 @@ func (p *ClaudeCLIProvider) ChatStream(ctx context.Context, req ChatRequest, onC // Parse stream-json line-by-line scanner := bufio.NewScanner(stdout) - scanner.Buffer(make([]byte, 0, 256*1024), 1024*1024) // 256KB initial, 1MB max + scanner.Buffer(make([]byte, 0, 256*1024), 10*1024*1024) // 256KB initial, 10MB max var finalResp ChatResponse var contentBuf strings.Builder @@ -194,6 +194,10 @@ func (p *ClaudeCLIProvider) ChatStream(ctx context.Context, req ChatRequest, onC } } + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("claude-cli: stream read error: %w", err) + } + if err := cmd.Wait(); err != nil { if debugFile != nil { fmt.Fprintf(debugFile, "\n=== STDERR:\n%s\n=== EXIT ERROR: %v\n", stderrBuf.String(), err)