From 6d9b30632df3cdd68353d467d47e7e1079bf1985 Mon Sep 17 00:00:00 2001 From: GinWU Date: Sun, 3 May 2026 11:31:30 +0800 Subject: [PATCH] fix(cli): honor positive tool preview length --- agent/display.py | 6 +++-- tests/agent/test_display.py | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/agent/display.py b/agent/display.py index 474595d76c..1dd65c3514 100644 --- a/agent/display.py +++ b/agent/display.py @@ -852,13 +852,15 @@ def get_cute_tool_message( s = str(s) if _tool_preview_max_len == 0: return s # no limit - return (s[:n-3] + "...") if len(s) > n else s + limit = _tool_preview_max_len + return (s[:limit-3] + "...") if len(s) > limit else s def _path(p, n=35): p = str(p) if _tool_preview_max_len == 0: return p # no limit - return ("..." + p[-(n-3):]) if len(p) > n else p + limit = _tool_preview_max_len + return ("..." + p[-(limit-3):]) if len(p) > limit else p def _wrap(line: str) -> str: """Apply skin tool prefix and failure suffix.""" diff --git a/tests/agent/test_display.py b/tests/agent/test_display.py index 4c1309a44c..c6ad837af9 100644 --- a/tests/agent/test_display.py +++ b/tests/agent/test_display.py @@ -8,12 +8,21 @@ from agent.display import ( build_tool_preview, capture_local_edit_snapshot, extract_edit_diff, + get_cute_tool_message, + set_tool_preview_max_len, _render_inline_unified_diff, _summarize_rendered_diff_sections, render_edit_diff_with_delta, ) +@pytest.fixture(autouse=True) +def reset_tool_preview_max_len(): + set_tool_preview_max_len(0) + yield + set_tool_preview_max_len(0) + + class TestBuildToolPreview: """Tests for build_tool_preview defensive handling and normal operation.""" @@ -102,6 +111,45 @@ class TestBuildToolPreview: assert build_tool_preview("terminal", []) is None +class TestCuteToolMessagePreviewLength: + def test_terminal_preview_unlimited_when_config_is_zero(self): + set_tool_preview_max_len(0) + command = "curl -s http://localhost:9222/json/list | jq -r '.[] | select(.type==\"page\")' | head -5" + + line = get_cute_tool_message("terminal", {"command": command}, 0.1) + + assert command in line + assert "..." not in line + + def test_terminal_preview_uses_positive_configured_limit(self): + set_tool_preview_max_len(80) + command = "curl -s http://localhost:9222/json/list | jq -r '.[] | select(.type==\"page\")' | head -5" + + line = get_cute_tool_message("terminal", {"command": command}, 0.1) + + assert command[:77] in line + assert "..." in line + assert "head -5" not in line + + def test_search_files_preview_uses_positive_configured_limit_not_default(self): + set_tool_preview_max_len(80) + pattern = "function.formatToolCall.context.preview.compactPreview.maxLength.truncate" + + line = get_cute_tool_message("search_files", {"pattern": pattern}, 0.1) + + assert pattern in line + assert "..." not in line + + def test_path_preview_uses_positive_configured_limit_not_default(self): + set_tool_preview_max_len(80) + path = "/tmp/hermes-test-preview-length/deeply/nested/path/test-output.txt" + + line = get_cute_tool_message("read_file", {"path": path}, 0.1) + + assert path in line + assert "..." not in line + + class TestEditDiffPreview: def test_extract_edit_diff_for_patch(self): diff = extract_edit_diff("patch", '{"success": true, "diff": "--- a/x\\n+++ b/x\\n"}')