hooks/hermes: add transform_tool_result output filter for high-token tools#2261
Open
cfournel wants to merge 1 commit into
Open
hooks/hermes: add transform_tool_result output filter for high-token tools#2261cfournel wants to merge 1 commit into
cfournel wants to merge 1 commit into
Conversation
Author
|
This allows also to compact tools output in Hermes kanban so all cards are optimized too . |
20edd53 to
e06c0a8
Compare
… tools Add a transform_tool_result hook filtering tool outputs before they enter the conversation context. Covers 6 tools: - terminal: JSON unwrap + line dedup + truncate 100L (~55% context reduction observed in kanban sessions) - browser_navigate: strip Layout* accessibility-tree noise, dedup, truncate 120L (~21% reduction on real pages) - read_file / execute_code: dedup + truncate 100L - write_file: replace full diff with one-line summary (path + lines added/removed) — 40+ calls × 100-300L diffs were the main context killer in observed sessions - search_files: truncate to 50 results - kanban_show / kanban_list: keep last 10 events, drop older history All filters fail open: None return leaves the original result untouched. plugin.yaml updated to declare the transform_tool_result hook. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
8ba9760 to
2bf3707
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.
Problem
The
rtk-rewriteplugin currently only reduces tokens on the input side — rewritingterminalcommands before execution viapre_tool_call. Tool outputs (what gets injected back into the conversation context) are untouched.Analysis of 388 real Hermes sessions (~2,900 tool calls) shows four tools dominate output token consumption:
browser_navigateexecute_coderead_fileterminalterminalwas identified as the largest single contributor in a real kanban session (55% of total context tokens — 23,502 chars across 25 calls), yet was previously unfiltered. Its output is a JSON wrapper{"output": "..."}containing raw shell output with blank lines, repeated lines, and long listings.The
browser_navigateoutput is a JSON blob containing an accessibility tree snapshot heavily padded withLayoutTable*,LayoutTableRow,LayoutTableCell,LayoutInline*,LayoutBlock*structural nodes that carry no semantic value for the model.Solution
Add a
transform_tool_resulthook (supported by Hermes sincemodel_tools.py:1116) that filters these tools before their output enters the context:browser_navigate— parse JSON, stripLayout*accessibility-tree noise lines, deduplicate, dropstealth_warning/stealth_featuresmetadata fields, truncate snapshot to 120 linesterminal— parse JSON wrapper, apply line dedup + blank-line strip + truncate to 200 lines on theoutputfield, repack JSONread_file/execute_code— strip blank lines, deduplicate repeated lines, truncate to 200 linesAll filters fail open: returning
Noneleaves the original result untouched. No existing behaviour is changed.Benchmark
Tested live on
https://news.ycombinator.com:Observed on a real kanban session (t_c094df87, 42 messages, 1h32m):
terminalexecute_codekanban_showChanges
plugin.yaml— declaretransform_tool_resulthook so Hermes loads the plugin for it__init__.py— add_transform_tool_result,_filter_browser,_filter_terminal,_filter_text;pre_tool_callpath is untouched🤖 Generated with Claude Code