Skip to content

Avoid full-string scans in token-budget truncation - #49712

Merged
copyberry[bot] merged 1 commit into
mainfrom
copyberry/codex-internal-to-codex-oss/37fee0c8d4ede8e74b491d3d641b0ea0e9003dd4
Sep 30, 2026
Merged

copyberry[bot] merged 1 commit into
mainfrom
copyberry/codex-internal-to-codex-oss/37fee0c8d4ede8e74b491d3d641b0ea0e9003dd4

Conversation

@copyberry

@copyberry copyberry Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Avoid full-string scans in token-budget truncation

Why

Token-budget truncation uses byte counts to estimate tokens, but previously scanned the entire string to count characters, including the discarded middle.

What changed

Use UTF-8 boundary lookups to select the retained prefix and suffix for token-budget truncation. Skip character counting for a zero-token budget and defer it to paths that need character counts.

Testing

Add regression coverage for multibyte characters crossing both retained boundaries, checking the truncated output and original token estimate.

## Why

Token-budget truncation uses byte counts to estimate tokens, but previously scanned the entire string to count characters, including the discarded middle.

## What changed

Use UTF-8 boundary lookups to select the retained prefix and suffix for token-budget truncation. Skip character counting for a zero-token budget and defer it to paths that need character counts.

## Testing

Add regression coverage for multibyte characters crossing both retained boundaries, checking the truncated output and original token estimate.

GitOrigin-RevId: 37fee0c8d4ede8e74b491d3d641b0ea0e9003dd4
@copyberry
copyberry Bot force-pushed the copyberry/codex-internal-to-codex-oss/37fee0c8d4ede8e74b491d3d641b0ea0e9003dd4 branch from 1d3b613 to 5e96aab Compare September 30, 2026 19:15
@copyberry
copyberry Bot merged commit 5e96aab into main Sep 30, 2026
@copyberry
copyberry Bot deleted the copyberry/codex-internal-to-codex-oss/37fee0c8d4ede8e74b491d3d641b0ea0e9003dd4 branch September 30, 2026 19:15
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant