Skip to content

fix(agent-core): cap background-task output buffer by bytes, not code units#733

Open
Dmatut7 wants to merge 1 commit into
MoonshotAI:mainfrom
Dmatut7:fix/agent-core-output-byte-cap
Open

fix(agent-core): cap background-task output buffer by bytes, not code units#733
Dmatut7 wants to merge 1 commit into
MoonshotAI:mainfrom
Dmatut7:fix/agent-core-output-byte-cap

Conversation

@Dmatut7

@Dmatut7 Dmatut7 commented Jun 13, 2026

Copy link
Copy Markdown

Related Issue

No existing issue — internal memory-bound fix explained below.

Problem

The in-memory background-task output ring buffer is bounded by MAX_OUTPUT_BYTES (1 MiB) and entry.outputSizeBytes is tracked with Buffer.byteLength, but the eviction loop measured chunks with string.length (UTF-16 code units). For multibyte output (CJK, emoji) the resident buffer could grow well past the byte cap (~3 MiB of 3-byte characters before any eviction), defeating the memory bound.

What changed

Measure eviction in UTF-8 bytes so the cap holds regardless of encoding. Test added.

Checklist

  • I have read the CONTRIBUTING document.
  • I have explained the problem above.
  • I have added a test.
  • This PR needs no changeset (internal memory bound; no user-visible behavior change).
  • This PR needs no doc update.

… units

The in-memory output ring buffer is bounded by `MAX_OUTPUT_BYTES` (1 MiB)
and `entry.outputSizeBytes` is tracked with `Buffer.byteLength`, but the
eviction loop measured chunks with `string.length` (UTF-16 code units).
For multibyte output (CJK, emoji) the resident buffer could therefore grow
well past the byte cap — e.g. ~3 MiB of 3-byte characters before any
eviction — defeating the memory bound the constant promises.

Measure eviction in UTF-8 bytes so the cap holds regardless of encoding.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Jun 13, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: ffeaa11

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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