Class: Brute::Middleware::ToolErrorTracking
- Defined in:
- lib/brute/middleware/tool_error_tracking.rb
Overview
Tracks per-tool error counts and total tool call count across LLM calls, and signals when the error ceiling is reached.
This middleware doesn’t execute tools itself — it inspects the tool results that were sent as input to the LLM call (env) and counts failures and totals.
When any tool exceeds max_failures, it sets env[:tool_error_limit_reached] so the orchestrator can decide to stop.
Also stores env[:tool_calls] with the cumulative number of tool invocations in the current session.
Constant Summary collapse
- DEFAULT_MAX_FAILURES =
3
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, max_failures: DEFAULT_MAX_FAILURES) ⇒ ToolErrorTracking
constructor
A new instance of ToolErrorTracking.
-
#reset! ⇒ Object
Reset counts (e.g., between user turns).
Constructor Details
#initialize(app, max_failures: DEFAULT_MAX_FAILURES) ⇒ ToolErrorTracking
Returns a new instance of ToolErrorTracking.
26 27 28 29 30 31 |
# File 'lib/brute/middleware/tool_error_tracking.rb', line 26 def initialize(app, max_failures: DEFAULT_MAX_FAILURES) super(app) @max_failures = max_failures @errors = Hash.new(0) # tool_name → count @total_tool_calls = 0 end |
Instance Method Details
#call(env) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/brute/middleware/tool_error_tracking.rb', line 33 def call(env) # PRE: count errors and totals from tool results that are about to be sent if (results = env[:tool_results]) @total_tool_calls += results.size results.each do |name, result| @errors[name] += 1 if result.is_a?(Hash) && result[:error] end end env[:metadata][:tool_calls] = @total_tool_calls env[:metadata][:tool_errors] = @errors.dup env[:metadata][:tool_error_limit_reached] = @errors.any? { |_, c| c >= @max_failures } @app.call(env) end |
#reset! ⇒ Object
Reset counts (e.g., between user turns).
51 52 53 54 |
# File 'lib/brute/middleware/tool_error_tracking.rb', line 51 def reset! @errors.clear @total_tool_calls = 0 end |