Skip to content

Add truncation to ExecuteBashObservation and TextContent with warning checks - #124

Merged
xingyaoww merged 13 commits into
mainfrom
openhands/fix-issue-123-truncation
Sep 6, 2025
Merged

xingyaoww merged 13 commits into
mainfrom
openhands/fix-issue-123-truncation

Conversation

@xingyaoww

Copy link
Copy Markdown
Contributor

Summary

This PR implements output truncation for ExecuteBashObservation and TextContent with warning checks as requested in issue #123.

Changes Made

1. Moved truncation utility to SDK

  • Moved openhands/tools/str_replace_editor/utils/truncate.py to openhands/sdk/utils/truncate.py
  • Added openhands/sdk/utils/__init__.py to export truncation utilities
  • Updated str_replace_editor imports to use the SDK truncate utility

2. Added truncation to ExecuteBashObservation

  • Modified ExecuteBashObservation.agent_observation property to use maybe_truncate() with a 30,000 character limit
  • Used DEFAULT_BASH_TRUNCATE_LIMIT constant for consistency

3. Added warning check to TextContent

  • Enhanced TextContent.to_llm_dict() method to:
    • Log a warning when text exceeds 50,000 characters
    • Automatically truncate text to the limit
    • Use DEFAULT_TEXT_CONTENT_LIMIT constant

4. Comprehensive testing

  • Created tests/sdk/utils/test_truncate.py with 8 test cases covering all truncation scenarios
  • Added tests to tests/sdk/llm/test_message.py for TextContent truncation behavior
  • Created tests/tools/execute_bash/test_observation_truncation.py with 5 test cases for ExecuteBashObservation

Constants Added

  • DEFAULT_BASH_TRUNCATE_LIMIT = 30_000 - Character limit for bash output truncation
  • DEFAULT_TEXT_CONTENT_LIMIT = 50_000 - Character limit for TextContent with warning

Testing

All tests pass successfully:

  • ✅ 8 truncation utility tests
  • ✅ 3 TextContent truncation tests
  • ✅ 5 ExecuteBashObservation truncation tests
  • ✅ Pre-commit hooks (ruff format, ruff lint, pycodestyle, pyright)

Backward Compatibility

This change is fully backward compatible:

  • Existing functionality remains unchanged
  • Only adds truncation when content exceeds the specified limits
  • No breaking changes to existing APIs

Fixes

Closes #123

@xingyaoww can click here to continue refining the PR

… checks

- Move truncate.py from str_replace_editor to SDK utils for shared usage
- Add truncation to ExecuteBashObservation.agent_observation with 30,000 char limit
- Add warning check and truncation to TextContent.to_llm_dict with 50,000 char limit
- Update str_replace_editor to use SDK truncate utility
- Add comprehensive tests for all truncation functionality

Fixes #123

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Sep 5, 2025 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report
FileStmtsMissCoverMissing
openhands
   __init__.py10100% 
openhands/sdk
   __init__.py14285%22–23
   logger.py732171%33, 57, 64–67, 69–71, 124, 130–132, 135–136, 142–144, 151, 156–157
openhands/sdk/agent
   __init__.py30100% 
   base.py46491%36, 47, 83, 103
openhands/sdk/agent/agent
   __init__.py20100% 
   agent.py1262877%68–70, 127–128, 130–132, 134–136, 167, 181, 201, 229, 260–265, 272–273, 277–279, 306, 313
openhands/sdk/context
   __init__.py40100% 
   agent_context.py57296%146, 152
   manager.py330%1, 4–5
   view.py47687%46–50, 52
openhands/sdk/context/condenser
   __init__.py30100% 
   condenser.py17382%68–69, 73
   no_op_condenser.py60100% 
openhands/sdk/context/microagents
   __init__.py40100% 
   exceptions.py5180%11
   microagent.py1419036%47, 50–51, 58, 71, 74–75, 77, 81, 84–86, 89–90, 98–100, 102–104, 107, 110, 113–118, 127–130, 132–135, 142–143, 151, 209–215, 218, 223, 245–246, 251–252, 254, 258, 265–267, 275–277, 293–294, 296–297, 300, 303–304, 307–311, 314–316, 319–324, 326–327, 329–331, 333–334, 336, 340
   types.py300100% 
openhands/sdk/context/utils
   __init__.py20100% 
   prompt.py30583%12, 15, 24, 44–45
openhands/sdk/conversation
   __init__.py50100% 
   conversation.py83890%70, 78, 86–88, 92–93, 149
   state.py28292%47–48
   types.py30100% 
   visualizer.py1182083%46, 71, 84, 123–125, 128–129, 131, 154, 185, 189, 196, 199, 202–203, 226–228, 230
openhands/sdk/event
   __init__.py50100% 
   base.py642068%34, 38, 49, 53–55, 57–62, 64–65, 67–69, 71–72, 74
   condenser.py25580%29, 33–35, 49
   llm_convertible.py1071585%28–29, 34–35, 159–160, 165, 173, 205–206, 211, 226–227, 232, 248
   types.py30100% 
   utils.py110100% 
openhands/sdk/llm
   __init__.py50100% 
   exceptions.py360100% 
   llm.py39513964%249, 254, 267–269, 273–274, 306, 344, 348, 358, 364–365, 382–383, 452, 465–466, 471–472, 474–475, 477–480, 485–487, 491–493, 510–511, 514–516, 519–520, 528–529, 534, 537, 539–540, 544–545, 548–552, 562–565, 570–575, 579–580, 589–591, 594–595, 619, 625–626, 672, 699, 701, 707, 710, 719–720, 729, 736, 739, 743–745, 749, 751–756, 758–775, 778–782, 784–785, 791–800, 804–815
   llm_registry.py380100% 
   message.py100298%201–202
   metadata.py150100% 
openhands/sdk/llm/utils
   fn_call_converter.py34311267%74, 343, 345, 348–349, 367, 369, 375, 381, 383, 422, 424, 426, 428, 433–434, 481–482, 518–520, 522, 524, 545–547, 553, 575, 601–602, 610–613, 615, 617, 639, 648, 656, 701–704, 708–711, 723, 727, 738, 748, 794, 796–798, 800, 803, 819–821, 823–826, 829, 833, 844–845, 859, 866–867, 870–871, 875–876, 882–883, 886, 905–908, 912–913, 918–919, 924, 973–974, 980, 994, 1006, 1008–1009, 1012–1014, 1016–1017, 1023–1025, 1027–1028, 1030, 1032, 1036, 1038, 1043, 1045–1046, 1049
   metrics.py107298%17, 105
   model_features.py350100% 
   telemetry.py117794%72, 95, 100–101, 113–114, 209
openhands/sdk/mcp
   __init__.py50100% 
   client.py631182%41, 56–57, 78, 82, 94–95, 105–106, 112–113
   definition.py27196%54
   tool.py381365%33–36, 40, 43, 46–49, 89–90, 95
   utils.py30486%23–24, 27, 30
openhands/sdk/tool
   __init__.py40100% 
   schema.py89693%19–21, 23, 32, 191
   security_prompt.py30100% 
   tool.py821186%51, 107, 129, 149, 152–158
openhands/sdk/tool/builtins
   __init__.py30100% 
   finish.py150100% 
openhands/sdk/utils
   __init__.py20100% 
   json.py28280%1–3, 5, 7–8, 11, 14–21, 25, 28, 30–31, 34, 37–38, 40, 43, 45–48
   truncate.py100100% 
openhands/tools
   __init__.py8275%37–38
openhands/tools/execute_bash
   __init__.py40100% 
   constants.py90100% 
   definition.py410100% 
   impl.py100100% 
   metadata.py50394%95–96, 100
openhands/tools/execute_bash/terminal
   __init__.py60100% 
   factory.py491177%24–25, 30, 32, 35, 37–38, 44–46, 97
   interface.py661478%43, 52, 62, 71, 76, 85, 94, 99, 104, 145, 157, 162, 171, 180
   subprocess_terminal.py2365975%68, 98–99, 125, 131, 138, 145–146, 156–157, 163–164, 178, 180, 184–186, 192, 208, 217–221, 256–258, 263, 275, 289, 313, 315, 324, 345, 361, 366, 372–374, 382–383, 387–388, 390–396, 400–401, 404–405, 407–408, 410–412
   terminal_session.py178895%92, 96–98, 235, 281, 297, 317
   tmux_terminal.py752270%35, 41, 85, 98, 109, 123, 135–142, 150–151, 153–154, 156, 158–160
openhands/tools/execute_bash/utils
   command.py81495%48, 64–66
openhands/tools/str_replace_editor
   __init__.py30100% 
   definition.py33196%78
   editor.py2281892%102, 113, 115, 124, 126, 131, 263, 301, 337, 347, 397–398, 522, 635, 642–643, 657, 662
   exceptions.py220100% 
   impl.py26292%31–32
openhands/tools/str_replace_editor/utils
   __init__.py00100% 
   config.py20100% 
   constants.py50100% 
   encoding.py54198%81
   file_cache.py95990%44–46, 49–50, 54, 59, 151, 154
   history.py66198%79
   shell.py230100% 
openhands/tools/utils
   __init__.py00100% 
TOTAL402672681% 

openhands-agent and others added 9 commits September 5, 2025 02:11
- Restore str_replace_editor specific truncate.py file
- Import str_replace_editor specific constants from prompts.py
- Remove duplicate imports in editor.py
- Keep SDK truncate utility focused on general functionality
- Maintain backward compatibility for str_replace_editor

Co-authored-by: openhands <openhands@all-hands.dev>
- Modified maybe_truncate function to preserve head and tail content
- Updated all tests to reflect new head-and-tail behavior
- Fixed line length issues in tests
- All 18 tests passing

Co-authored-by: openhands <openhands@all-hands.dev>
- Removed DEFAULT_BASH_TRUNCATE_LIMIT from SDK utils
- Updated execute_bash to use existing MAX_CMD_OUTPUT_SIZE constant
- Updated imports and tests to use the correct constant
- Maintains proper separation of concerns between SDK and tools

Co-authored-by: openhands <openhands@all-hands.dev>
- Removed complex notice length handling logic
- Kept simple head-and-tail approach with proper odd number handling
- Updated tests to match simplified behavior
- Function is now much cleaner and easier to understand

Co-authored-by: openhands <openhands@all-hands.dev>
- Fix str_replace_editor to properly pass truncate_after parameter to maybe_truncate
- Add test for str_replace_editor view operation truncation functionality
- Ensure truncation works correctly with the editor's MAX_RESPONSE_LEN_CHAR limit

Co-authored-by: openhands <openhands@all-hands.dev>
- Move MAX_RESPONSE_LEN_CHAR from utils/truncate.py to utils/prompts.py
- Remove redundant utils/truncate.py file that only re-exported SDK functions
- Update imports to use SDK's maybe_truncate directly
- Consolidate str_replace_editor constants in prompts.py

Co-authored-by: openhands <openhands@all-hands.dev>
@xingyaoww
xingyaoww enabled auto-merge (squash) September 6, 2025 02:47
@openhands-ai

openhands-ai Bot commented Sep 6, 2025

Copy link
Copy Markdown

Looks like there are a few issues preventing this PR from being merged!

  • GitHub Actions are failing:
    • Run tests

If you'd like me to help, just leave a comment, like

@OpenHands please fix the failing actions on PR #124 at branch `openhands/fix-issue-123-truncation`

Feel free to include any additional details that might help me get this PR into a better state.

You can manage your notification settings

@xingyaoww
xingyaoww merged commit 155b63e into main Sep 6, 2025
@xingyaoww
xingyaoww deleted the openhands/fix-issue-123-truncation branch September 6, 2025 03:05
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
… checks (OpenHands#124)

Co-authored-by: openhands <openhands@all-hands.dev>
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.

Add maybe_truncate( to agent_observation of ExecuteBashObservation

2 participants