Class: Xeno::TurnRunner

Inherits:
Object
  • Object
show all
Includes:
Compaction
Defined in:
lib/xeno/turn_runner.rb

Overview

Drives one turn to completion (or to a park). This is the durable core: RubyLLM's decomposed v2 loop (generate + self-executed tools + add_message injection), checkpointed in the database at every move. We never call run_tools (not idempotent) or complete (unbounded).

Crash-safety invariants:

  • The user message and instructions were persisted when the turn was staged; the runner only advances the transcript.
  • generate persists the assistant message via the acts_as callback path.
  • A tool execution and its transcript injection commit in ONE transaction (the action row is the checkpoint) — recorded calls are never re-run.
  • Every write is fenced on the claim token; zombies affect zero rows.
  • Trailing blank assistant rows (the mid-generate crash window) are removed before deciding anything (spike #2, hazard 1).

Constant Summary

Constants included from Compaction

Compaction::COMPACTION_PROMPT, Compaction::SUMMARY_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(turn, definition: Xeno.definition) ⇒ TurnRunner

Returns a new instance of TurnRunner.



21
22
23
24
25
# File 'lib/xeno/turn_runner.rb', line 21

def initialize(turn, definition: Xeno.definition)
  @turn = turn
  @session = turn.session
  @definition = definition
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



19
20
21
# File 'lib/xeno/turn_runner.rb', line 19

def definition
  @definition
end

#sessionObject (readonly)

Returns the value of attribute session.



19
20
21
# File 'lib/xeno/turn_runner.rb', line 19

def session
  @session
end

#tokenObject (readonly)

Returns the value of attribute token.



19
20
21
# File 'lib/xeno/turn_runner.rb', line 19

def token
  @token
end

#turnObject (readonly)

Returns the value of attribute turn.



19
20
21
# File 'lib/xeno/turn_runner.rb', line 19

def turn
  @turn
end

Instance Method Details

#runObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/xeno/turn_runner.rb', line 27

def run
  @token = turn.claim!
  return :not_claimed unless token

  ActiveSupport::Notifications.instrument("xeno.turn", turn_id: turn.id, session_id: session.id) do
    run_claimed
  end
rescue Xeno::Fenced
  :fenced
end