Class: Insika::ToolBatch

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeToolBatch

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.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/insika/tool_batch.rb', line 27

def closed?(message)
  role = field(message, :role).to_s
  return open(message) 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

Returns:

  • (Boolean)


45
# File 'lib/insika/tool_batch.rb', line 45

def halted? = @halted