Class: Agent::SessionContext::Loop

Inherits:
Data
  • Object
show all
Defined in:
lib/agent/session_context/loop.rb

Overview

One session read as an agent loop: round trips, the tool calls paired across them, who spoke at each step, and how the loop ended.

recorded is false for an empty session — nothing was recorded, and "absence must never read as presence" is this gem's standing rule (RoundTrip carries the same rule for one group).

warnings is the reader's warnings first, then this object's own, so the order is deterministic regardless of what the reader happened to find.

Constant Summary collapse

ENDINGS =

The exact sentence for each ending, so the Loop and whatever renders it say the same words.

{
  answered: "the model answered without asking for a tool",
  stopped_in_the_loop: "a tool was asked for and nothing answered it",
  not_a_model_record: "the session stops on a record the model did not write",
  empty: "no round trips were recorded"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ending ⇒ Object (readonly)

Returns the value of attribute ending

Returns:

  • (Object) —

    the current value of ending



15
16
17
# File 'lib/agent/session_context/loop.rb', line 15

def ending
  @ending
end

#recorded ⇒ Object (readonly)

Returns the value of attribute recorded

Returns:

  • (Object) —

    the current value of recorded



15
16
17
# File 'lib/agent/session_context/loop.rb', line 15

def recorded
  @recorded
end

#round_trips ⇒ Object (readonly)

Returns the value of attribute round_trips

Returns:

  • (Object) —

    the current value of round_trips



15
16
17
# File 'lib/agent/session_context/loop.rb', line 15

def round_trips
  @round_trips
end

#session ⇒ Object (readonly)

Returns the value of attribute session

Returns:

  • (Object) —

    the current value of session



15
16
17
# File 'lib/agent/session_context/loop.rb', line 15

def session
  @session
end

#speakers ⇒ Object (readonly)

Returns the value of attribute speakers

Returns:

  • (Object) —

    the current value of speakers



15
16
17
# File 'lib/agent/session_context/loop.rb', line 15

def speakers
  @speakers
end

#tool_calls ⇒ Object (readonly)

Returns the value of attribute tool_calls

Returns:

  • (Object) —

    the current value of tool_calls



15
16
17
# File 'lib/agent/session_context/loop.rb', line 15

def tool_calls
  @tool_calls
end

#warnings ⇒ Object (readonly)

Returns the value of attribute warnings

Returns:

  • (Object) —

    the current value of warnings



15
16
17
# File 'lib/agent/session_context/loop.rb', line 15

def warnings
  @warnings
end

Class Method Details

.for(reader) ⇒ Object

Reads the WHOLE session and says so by returning one built object rather than streaming — it cannot stream, because a tool result can arrive many records after the call it answers, and a streaming pass cannot look forward to find it. Readers::Base#tree carries the same shape of honesty for the same reason.



41
42
43
44
45
46
47
48
49
50
# File 'lib/agent/session_context/loop.rb', line 41

def for(reader)
  warnings = []
  trips = reader.round_trips
  speakers = {}
  trips.each { |trip| speakers[trip.index] = speaker_of(trip, warnings) }
  calls = pair(trips, warnings)
  new(session: reader.session, round_trips: trips, tool_calls: calls, speakers: speakers,
      ending: ending_for(trips, speakers), recorded: trips.any? && trips.all?(&:recorded),
      warnings: reader.warnings + warnings)
end

Instance Method Details

#ending_detail ⇒ Object



32
# File 'lib/agent/session_context/loop.rb', line 32

def ending_detail = ENDINGS.fetch(ending)

#ending_inferred? ⇒ Boolean

Always true: no store on disk records WHY a session stopped — the on-disk transcript holds no stop reason and no turn count, because those live in the streamed output of a non-interactive run, not in the session file. So the ending is always deduced, and must always be labelled as such rather than presented as a recorded fact.

Returns:

  • (Boolean)


30
# File 'lib/agent/session_context/loop.rb', line 30

def ending_inferred? = true