Skip to content

fix(core): clamp code-mode exec results to the model's truncation policy - #2

Open
argszero wants to merge 1 commit into
mainfrom
fix/code-mode-clamp-output
Open

argszero wants to merge 1 commit into
mainfrom
fix/code-mode-clamp-output

Conversation

@argszero

@argszero argszero commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Summary

In code mode, the size of a tool result the model receives is bounded only by what the script's @exec pragma requests. truncate_code_mode_result uses resolve_max_tokens(max_output_tokens) with no clamp, so a script that raises its max_output_tokens can push a shell result (up to the unified-exec buffer limit) straight into context. Observed: a ~497 KB result reaching the model as ~138K tokens.

The function-mode path (ExecCommandToolOutput::model_output_policy) already clamps to the model's truncation_policy. This mirrors that clamp in the code-mode path: thread the model's truncation policy into truncate_code_mode_result and clamp the requested budget, so a model never receives a tool result larger than its own output policy allows.

Related Issue

Closes openai#42367

Tests

  • Existing tests pass (codex-core code_mode unit tests)
  • New regression test high_max_output_tokens_is_clamped_to_model_policy asserts a raised max_output_tokens pragma is clamped to the model's truncation policy

In code mode, the size of a tool result the model receives is bounded only
by what the script's @exec pragma requests: truncate_code_mode_result uses
resolve_max_tokens(max_output_tokens) with no clamp, so a script that raises
its max_output_tokens can push a shell result (up to the unified-exec buffer
limit) straight into context.

Mirror the function-mode clamp in ExecCommandToolOutput::model_output_policy:
thread the model's truncation policy into truncate_code_mode_result and clamp
the requested budget to it, so a model can never receive a tool result larger
than its own output policy allows.

Adds a regression test asserting a high max_output_tokens pragma is clamped
to the model's truncation policy.
@argszero
argszero force-pushed the fix/code-mode-clamp-output branch from 5893a05 to ddf9b35 Compare October 8, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant