hooks/hermes: add transform_tool_result output filter for high-token tools#2261
Open
cfournel wants to merge 2 commits into
Open
hooks/hermes: add transform_tool_result output filter for high-token tools#2261cfournel wants to merge 2 commits into
cfournel wants to merge 2 commits into
Conversation
Author
|
This allows also to compact tools output in Hermes kanban so all cards are optimized too . |
8ba9760 to
2bf3707
Compare
b230a6d to
92f9b15
Compare
… tools Filters 7 tools before their output enters the conversation context: - terminal: JSON unwrap + dedup + truncate 100L - browser_navigate: strip Layout* noise, dedup, truncate 120L (~21% reduction) - read_file / execute_code: dedup + truncate 100L - write_file / patch: replace full unified diff with 1-line summary (e.g. "[patch] file.cs (2 hunks, +13/-8 lines)") — main fix for patch-heavy tasks where 5+ diffs × 25L each saturate the context - search_files: truncate to 50 results - kanban_show / kanban_list: keep last 10 events only All filters fail open. plugin.yaml updated to declare the hook. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Tests (44 cases across 7 test classes): - TransformDispatchTest: unknown tool → None, None/empty result → None - FilterTextTest: dedup, blank-strip, 100-line truncation, no-change → None - FilterTerminalTest: JSON unwrap, exit_code/error preservation, non-JSON fallback - FilterBrowserTest: Layout* stripping, dedup, 120-line truncation, stealth field removal - FilterDiffTest: compact summary format, hunk singular/plural, shorter-than-original guard - FilterSearchFilesTest: 50-result limit, exact boundary, blank-line exclusion - FilterKanbanTest: 10-event limit, section preservation, kanban_show + kanban_list dispatch README: documents both hooks (pre_tool_call + transform_tool_result) with filter table, diff-summary example, browser noise context, fail-open behavior, and limitations section. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
3817c7a to
f5b5802
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a Hermes plugin (
hooks/hermes/rtk-rewrite/) that registers two hooks:Hook 1 —
pre_tool_call: automatic command rewritingRewrites bare terminal commands (
find,grep,cat,ls, …) to theirrtkequivalents before execution, usingrtk rewriteas the single source of truth. Workers benefit automatically without needing to prefix commands manually — identical to the Claude CodePreToolUsehook but applied inside Hermes so all backends (local, Docker, SSH, Modal) benefit.find /home -name '*.py'rtk find /home -name '*.py'grep -rn foo /srcrtk grep -rn foo /srccat large_file.yamlrtk read large_file.yamlls -la /some/dirrtk ls -la /some/dirpython3 script.pyHook 2 —
transform_tool_result: output filteringFilters high-token tool outputs before they enter the conversation context:
terminalbrowser_navigateLayout*accessibility noise, deduplicate, truncateread_file,execute_codewrite_file,patchsearch_fileskanban_show,kanban_listThe
write_file/patchfilter is the main fix for context overflow on patch-heavy tasks — a 50-line diff becomes[patch] src/foo.cs (2 hunks, +5/-3 lines).All filters fail open — if parsing fails or RTK is absent, the original output is passed through unchanged.
Motivation
Observed in Hermes kanban worker logs over 24h (pigwars board):
find[exit 124]at 10–15slsgrepcatContext overflow crashes (
context length exceeded at ~41k tokens) were traced to unfilteredwrite_file/patchdiffs and largebrowser_navigatesnapshots accumulating across turns.Files changed
hooks/hermes/rtk-rewrite/__init__.py— plugin implementation (pre_tool_call + transform_tool_result)hooks/hermes/rtk-rewrite/plugin.yaml— hook declarationshooks/hermes/README.md— full documentation for both hookshooks/hermes/tests/test_transform_tool_result.py— 44 tests covering all filtersTest plan
transform_tool_resultfilters (all pass)pre_tool_calltests (all pass)find /home -name '*.py'→ executes asrtk find, output filteredpython3 script.py→ passes through unchangedrtkabsent from PATH → all commands execute normally, one warning loggedwrite_filewith 50-line diff → result replaced with 1-line[patch]summarybrowser_navigatewith Layout* noise → snapshot stripped and truncatedkanban_showwith 80 events → truncated to last 10🤖 Generated with Claude Code