Skip to content

fix: head+tail truncation for execute_code stdout (inspired by openclaw context-pruning)#755

Closed
teknium1 wants to merge 1 commit into
mainfrom
feat/head-tail-truncation-execute-code
Closed

fix: head+tail truncation for execute_code stdout (inspired by openclaw context-pruning)#755
teknium1 wants to merge 1 commit into
mainfrom
feat/head-tail-truncation-execute-code

Conversation

@teknium1

@teknium1 teknium1 commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Previously, execute_code's _drain() function only captured the first MAX_STDOUT_BYTES (50KB) of stdout, silently dropping all tail output. Scripts that print() their final results at the end would have those results lost — a critical issue since execute_code scripts are designed to print their final output.

Changes

  • New _drain_head_tail() function: Uses a two-buffer approach — a head buffer (first 40%) and a rolling tail deque (last 60%). Memory usage stays bounded.
  • Truncation notice: When output is truncated, shows [OUTPUT TRUNCATED - N chars omitted out of M total] between head and tail sections.
  • 3 new tests: Verify short output isn't truncated, large output preserves both HEAD and TAIL markers, and truncation notice format.

Rationale

terminal_tool.py already does head+tail truncation (40% head / 60% tail at line 1042-1051), but code_execution_tool.py was head-only. The tail is actually more important for execute_code since scripts print their final result at the end.

Inspired by openclaw's softTrim context-pruning (headChars/tailChars split in v2026.3.7).

Tests

  • All 31 tests pass (28 existing + 3 new)

…aw context-pruning)

Previously, _drain() only captured the first MAX_STDOUT_BYTES (50KB) of
stdout, silently dropping all tail output. Scripts that print() their
final results at the end would have those results lost.

Now uses a two-buffer approach: 40% head + 60% tail (rolling window).
This matches the pattern already used in terminal_tool.py (line 1042-1051)
but gives the tail more space since execute_code scripts typically
print() their final results at the end.

Inspired by openclaw's softTrim context-pruning (headChars/tailChars).
teknium1 added a commit that referenced this pull request Mar 11, 2026
Replaces head-only stdout capture with a two-buffer approach (40% head,
60% tail rolling window) so scripts that print() their final results
at the end never lose them. Adds truncation notice between sections.

Cherry-picked from PR #755, conflict resolved (test file additions).

3 new tests for short output, head+tail preservation, and notice format.
teknium1 added a commit that referenced this pull request Mar 11, 2026
Replaces head-only stdout capture with 40/60 head/tail split so final
print() output is never lost. 3 new tests.
@teknium1

Copy link
Copy Markdown
Contributor Author

Merged to main in 359352b. Head+tail truncation for execute_code stdout — cherry-picked and conflict resolved against current main.

@teknium1 teknium1 closed this Mar 11, 2026
angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
Replaces head-only stdout capture with a two-buffer approach (40% head,
60% tail rolling window) so scripts that print() their final results
at the end never lose them. Adds truncation notice between sections.

Cherry-picked from PR NousResearch#755, conflict resolved (test file additions).

3 new tests for short output, head+tail preservation, and notice format.
angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
… stdout

Replaces head-only stdout capture with 40/60 head/tail split so final
print() output is never lost. 3 new tests.
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
Replaces head-only stdout capture with a two-buffer approach (40% head,
60% tail rolling window) so scripts that print() their final results
at the end never lose them. Adds truncation notice between sections.

Cherry-picked from PR NousResearch#755, conflict resolved (test file additions).

3 new tests for short output, head+tail preservation, and notice format.
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
… stdout

Replaces head-only stdout capture with 40/60 head/tail split so final
print() output is never lost. 3 new tests.
olympus-terminal pushed a commit to olympus-terminal/hermes-agent that referenced this pull request May 16, 2026
Replaces head-only stdout capture with a two-buffer approach (40% head,
60% tail rolling window) so scripts that print() their final results
at the end never lose them. Adds truncation notice between sections.

Cherry-picked from PR NousResearch#755, conflict resolved (test file additions).

3 new tests for short output, head+tail preservation, and notice format.
olympus-terminal pushed a commit to olympus-terminal/hermes-agent that referenced this pull request May 16, 2026
… stdout

Replaces head-only stdout capture with 40/60 head/tail split so final
print() output is never lost. 3 new tests.
Egavasyug pushed a commit to Egavasyug/hermes-agent that referenced this pull request Jun 10, 2026
Replaces head-only stdout capture with a two-buffer approach (40% head,
60% tail rolling window) so scripts that print() their final results
at the end never lose them. Adds truncation notice between sections.

Cherry-picked from PR NousResearch#755, conflict resolved (test file additions).

3 new tests for short output, head+tail preservation, and notice format.
Egavasyug pushed a commit to Egavasyug/hermes-agent that referenced this pull request Jun 10, 2026
… stdout

Replaces head-only stdout capture with 40/60 head/tail split so final
print() output is never lost. 3 new tests.
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