Class: Insika::ToolBatch
- Inherits:
-
Object
- Object
- Insika::ToolBatch
- Defined in:
- lib/insika/tool_batch.rb
Overview
The batch arithmetic behind every mid-turn user append.
A model step that calls tools produces ONE assistant message announcing N
tool calls, followed by N role: tool messages. Anthropic rejects a user
message that lands between two of those results outright, so the ONLY valid
append point inside a turn is the instant the Nth result closes the batch.
SteerInjector discovered this rule; LoopDetector and TurnBudget both live by
it, which is why the counting lives here instead of twice.
Not a general-purpose helper: it answers one question ("did a batch just
close?") and remembers one fact ("was this batch halted"), because a
halt_when batch has no next model step and anything appended there would
sit unread forever.
Instance Method Summary collapse
-
#closed?(message) ⇒ Boolean
Feeds a RubyLLM message (duck-typed).
-
#halt!(result) ⇒ Object
From after_tool_result, with the RAW result: a Tool::Halt is only recognizable there.
- #halted? ⇒ Boolean
-
#initialize ⇒ ToolBatch
constructor
A new instance of ToolBatch.
Constructor Details
#initialize ⇒ ToolBatch
Returns a new instance of ToolBatch.
18 19 20 21 22 |
# File 'lib/insika/tool_batch.rb', line 18 def initialize @expected = nil # tool calls announced by the batch in flight (nil = none) @seen = 0 @halted = false end |
Instance Method Details
#closed?(message) ⇒ Boolean
Feeds a RubyLLM message (duck-typed). True EXACTLY on the message that closes a batch of tool calls — the append boundary. Everything else, including the assistant message that opens the batch, is false.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/insika/tool_batch.rb', line 27 def closed?() role = field(, :role).to_s return open() if role == "assistant" return false unless role == "tool" && @expected @seen += 1 return false if @seen < @expected @expected = nil true end |
#halt!(result) ⇒ Object
From after_tool_result, with the RAW result: a Tool::Halt is only recognizable there.
41 42 43 |
# File 'lib/insika/tool_batch.rb', line 41 def halt!(result) @halted = true if defined?(RubyLLM::Tool::Halt) && result.is_a?(RubyLLM::Tool::Halt) end |
#halted? ⇒ Boolean
45 |
# File 'lib/insika/tool_batch.rb', line 45 def halted? = @halted |