Skip to content

fix(backend): increase AG-UI event buffer size from 1MB to 10MB#964

Merged
Gkrumbach07 merged 2 commits intomainfrom
fix/json-buffer-overflow
Mar 20, 2026
Merged

fix(backend): increase AG-UI event buffer size from 1MB to 10MB#964
Gkrumbach07 merged 2 commits intomainfrom
fix/json-buffer-overflow

Conversation

@Gkrumbach07
Copy link
Contributor

Summary

  • Increase scannerMaxLineSize from 1MB to 10MB in agui_store.go to support large MCP tool results
  • Fixes "Failed to decode JSON: JSON message exceeded maximum buffer size of 1048576 bytes" error

Problem

When MCP tools (like Jira) return large datasets, the JSON-serialized tool result can exceed the 1MB scanner buffer limit. This causes:

  • Events failing to persist to the JSONL event log
  • Session export failures
  • Event replay failures on reconnection

Solution

Increased the buffer size from 1MB to 10MB:

  • Scanner still starts with 64KB and only grows as needed (no memory waste for small events)
  • 10MB is reasonable for large MCP tool results (Jira boards, database queries, etc.)
  • Matches common HTTP body size limits

Changes

  • components/backend/websocket/agui_store.go: Increase scannerMaxLineSize from 1024 * 1024 (1MB) to 10 * 1024 * 1024 (10MB)

Test plan

  • Run websocket tests: go test ./websocket/... -v - all pass
  • Run go vet: go vet ./websocket/... - no issues
  • Run golangci-lint: golangci-lint run ./websocket/... - 0 issues
  • Verify gofmt: gofmt -l websocket/ - no formatting issues
  • Manual test: Fetch large Jira board via MCP and verify events persist correctly

🤖 Generated with Claude Code

@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 53e5c4f4-c4e8-4814-80b6-783a2ce67397

📥 Commits

Reviewing files that changed from the base of the PR and between 48d2d28 and bb60b5d.

📒 Files selected for processing (1)
  • components/backend/websocket/agui_store.go

Walkthrough

The JSONL scanner max line size in agui_store.go was increased from 1MB to 10MB, and the agent-status tail-read window was changed from a fixed 64KB to a dynamic value based on the scanner size (maxTailBytes := int64(scannerMaxLineSize * 2)).

Changes

Cohort / File(s) Summary
Websocket JSONL scanner & tail-read
components/backend/websocket/agui_store.go
Increased scannerMaxLineSize from 1 * 1024 * 1024 to 10 * 1024 * 1024; replaced const maxTailBytes = 64 * 1024 with maxTailBytes := int64(scannerMaxLineSize * 2) in DeriveAgentStatus so the tail-read window scales with scanner buffer size.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately summarizes the main change: increasing the AG-UI event buffer size from 1MB to 10MB, which is the primary objective of this changeset.
Description check ✅ Passed The PR description comprehensively explains the problem (JSON buffer overflow with large MCP tool results), the solution (10MB buffer increase), and test results, all directly related to the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/json-buffer-overflow
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@components/backend/websocket/agui_store.go`:
- Around line 60-61: DeriveAgentStatus currently reads only maxTailBytes (64KB)
from the file tail which can truncate final JSONL lines now that
scannerMaxLineSize is 10MB; change the logic in DeriveAgentStatus to set
maxTailBytes to at least scannerMaxLineSize (or a bounded multiple, e.g.,
min(fileSize, scannerMaxLineSize * 2)) so the scanner buffer can contain an
entire max-sized line before parsing; update any related variables (maxTailBytes
usage and scannerInitialBufferSize consideration) to ensure bufio.Scanner is
configured with scannerMaxLineSize and will not drop/trim the final line when
parsing status.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 629e282d-d6ef-427d-8146-580f782885f5

📥 Commits

Reviewing files that changed from the base of the PR and between f74e275 and 10c85fe.

📒 Files selected for processing (1)
  • components/backend/websocket/agui_store.go

@Gkrumbach07
Copy link
Contributor Author

PR Review Summary

I've checked the CI status and reviewed the code changes. Here's what I found:

✅ Passing Checks

  • Backend Unit Tests: All pass ✓
  • Go Lint (Backend): 0 issues ✓
  • CodeQL (Go, JavaScript, Python, Actions): All pass ✓
  • Build & Push Images: Both amd64 and arm64 builds successful ✓
  • Local Dev Simulation: Pass ✓
  • Lint Summary: Pass ✓

❌ E2E Test Failure (Unrelated to this PR)

The E2E test failure is not caused by this change:

  • Error: Expected to find element: [data-testid="new-session-btn"], but never found it
  • Root cause: Test environment setup issue - frontend UI didn't load properly in the test runner
  • Evidence: E2E tests are passing on main branch (5 recent runs all successful)
  • Impact: This is a backend-only change (buffer size constant) that cannot affect frontend element rendering

📝 Code Review

Change: Increase scannerMaxLineSize from 1MB to 10MB in components/backend/websocket/agui_store.go

Security: ✓ No concerns

  • Just a constant change, no code logic modifications
  • No new attack surface

Error Handling: ✓ Appropriate

  • Scanner already has proper error handling
  • No new error paths introduced

Memory Safety: ✓ Safe

  • Scanner starts with 64KB buffer and only grows as needed
  • 10MB is reasonable for large MCP tool results
  • No memory waste for small events (dynamic growth)

Testing: ✓ Comprehensive

  • All websocket tests pass
  • go vet: clean
  • golangci-lint: 0 issues
  • Formatting verified

Logic: ✓ Correct

🎯 Recommendation

This PR is ready to merge. The E2E test failure is a transient infrastructure issue unrelated to this change. All relevant tests for the modified code pass, and the change is safe, well-tested, and solves the reported issue.

@Gkrumbach07
Copy link
Contributor Author

CodeRabbit Review - Addressed

Issue: CodeRabbit flagged that DeriveAgentStatus reads only 64KB from file tail, which could truncate large events (now that max line size is 10MB).

Fix: Commit 576a0ec - Increased maxTailBytes from 64KB to int64(scannerMaxLineSize * 2) (20MB)

This ensures:

  • ✅ Can read at least one complete max-sized event (10MB)
  • ✅ Additional events available for proper status derivation
  • ✅ Value stays in sync with scannerMaxLineSize for future changes
  • ✅ All tests pass

The review thread has been resolved. Thanks @coderabbitai for catching this edge case!

@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

Tip

For best results, initiate chat on the files or code changes.

@Gkrumbach07 Great fix! Tying maxTailBytes to int64(scannerMaxLineSize * 2) is a solid approach — it ensures at least one full max-sized event can always be read from the tail, and keeps the two values automatically in sync if scannerMaxLineSize is adjusted in the future. Thanks for the thorough follow-up!

@Gkrumbach07
Copy link
Contributor Author

✅ Rebased onto latest main

Successfully rebased branch onto origin/main (20 commits ahead):

  • No conflicts during rebase
  • All tests pass ✓
  • Linters clean (go vet, golangci-lint) ✓
  • Both commits preserved with updated SHAs

Commits after rebase:

  1. c461a8a9 - fix(backend): increase AG-UI event buffer size from 1MB to 10MB
  2. 48d2d285 - fix(backend): align DeriveAgentStatus tail buffer with scanner max line size

PR is ready for review and merge.

Gkrumbach07 and others added 2 commits March 19, 2026 23:57
When MCP tools return large data (e.g., Jira board lists with many issues),
the JSON-serialized tool result can exceed the 1MB scanner buffer limit.
This causes errors like "Failed to decode JSON: JSON message exceeded
maximum buffer size of 1048576 bytes" and prevents events from being
persisted and replayed.

Changes:
- Increase scannerMaxLineSize from 1MB to 10MB in agui_store.go
- Affects both event persistence (loadEvents) and session export
- Scanner still starts with 64KB buffer and only grows as needed
- 10MB is a reasonable limit for large MCP tool results

Fixes: #<issue-number>

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…ne size

CodeRabbit correctly identified that DeriveAgentStatus was reading only 64KB
from the file tail, which could truncate large events now that scannerMaxLineSize
is 10MB. This would cause JSON parsing failures and return empty status.

Changes:
- Increase maxTailBytes from 64KB to 2x scannerMaxLineSize (20MB)
- This ensures we can read at least one complete max-sized event plus additional
  events for proper status derivation
- Keeps the value in sync with scannerMaxLineSize for future changes

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@Gkrumbach07 Gkrumbach07 force-pushed the fix/json-buffer-overflow branch from 48d2d28 to bb60b5d Compare March 20, 2026 04:58
@Gkrumbach07
Copy link
Contributor Author

Final Pre-Merge Review ✅

Diff Audit

  • Files changed: 1 (components/backend/websocket/agui_store.go)
  • No stray files: No markdown docs, no .claude/ changes, no .env files, no test artifacts, no unrelated formatting

CI Status (all green)

Check Status
Backend Unit Tests ✅ Pass
Go Lint - Backend ✅ Pass
Build (amd64) ✅ Pass
Build (arm64) ✅ Pass
End-to-End Tests ✅ Pass
CodeQL (Go, JS/TS, Python, Actions) ✅ Pass
CodeRabbit ✅ Pass
test-local-dev-simulation ✅ Pass
kustomize-build ✅ Pass
validate-manifests ✅ Pass

Local Checks

  • gofmt -l . — clean ✅
  • go vet ./... — clean ✅
  • golangci-lint run — 0 issues ✅
  • go test ./websocket/... — all pass ✅

Review Items Addressed

  • ✅ CodeRabbit finding resolved (aligned DeriveAgentStatus tail buffer with scanner max line size)

This PR is clean and ready to merge.

@Gkrumbach07 Gkrumbach07 merged commit 1af6d3a into main Mar 20, 2026
35 checks passed
@Gkrumbach07 Gkrumbach07 deleted the fix/json-buffer-overflow branch March 20, 2026 12:42
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