Class: Phronomy::Agent::AgentExecutionActivation

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/agent/agent_execution_activation.rb

Defined Under Namespace

Classes: ApplicationCallbackFailure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execution:, agent:, runtime_projection:, coordinator:, application_listener: nil) ⇒ AgentExecutionActivation

Returns a new instance of AgentExecutionActivation.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 13

def initialize(
  execution:,
  agent:,
  runtime_projection:,
  coordinator:,
  application_listener: nil
)
  @execution_id = execution.execution_id
  @execution = execution
  @agent = agent
  @runtime_projection = runtime_projection
  @base_manifest = runtime_projection.manifest
  @coordinator = coordinator
  @application_listener = application_listener
  @mutex = Mutex.new
  @active_call = nil
  @llm_results = []
  @runtime_events = []
  @callback_errors = []
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



9
10
11
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 9

def agent
  @agent
end

#application_listenerObject (readonly)

Returns the value of attribute application_listener.



9
10
11
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 9

def application_listener
  @application_listener
end

#base_manifestObject (readonly)

Returns the value of attribute base_manifest.



9
10
11
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 9

def base_manifest
  @base_manifest
end

#callback_failureObject (readonly)

Returns the value of attribute callback_failure.



121
122
123
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 121

def callback_failure
  @callback_failure
end

#coordinatorObject (readonly)

Returns the value of attribute coordinator.



9
10
11
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 9

def coordinator
  @coordinator
end

#execution_idObject (readonly)

Returns the value of attribute execution_id.



9
10
11
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 9

def execution_id
  @execution_id
end

#invocationObject

Returns the value of attribute invocation.



11
12
13
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 11

def invocation
  @invocation
end

#sessionObject

Returns the value of attribute session.



11
12
13
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 11

def session
  @session
end

Instance Method Details

#acknowledge_runtime_snapshot(snapshot) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 97

def acknowledge_runtime_snapshot(snapshot)
  @mutex.synchronize do
    @llm_results.shift(snapshot.fetch(:llm_results).length)
    @runtime_events.shift(snapshot.fetch(:runtime_events).length)
    if snapshot[:active_call] && @active_call == snapshot[:active_call]
      @active_call = nil
    end
  end
end

#begin_llm_call(projection) ⇒ Object

Allocates Provider Call identity before transport begins. This identity is provenance only; Context selection must not use it as a semantic boundary.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 52

def begin_llm_call(projection)
  call_context = {
    llm_call_id: SecureRandom.uuid,
    manifest_ref: projection.manifest_ref,
    started_at: Time.now.utc.iso8601(6)
  }.freeze
  @mutex.synchronize do
    if @active_call
      raise Phronomy::Error,
        "cannot start a Provider Call while another Provider Call is active"
    end
    @runtime_projection = projection
    @active_call = call_context
  end
  call_context
end

#callback_failed?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 123

def callback_failed?
  @mutex.synchronize { !@callback_failure.nil? }
end

#executionObject



34
35
36
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 34

def execution
  @mutex.synchronize { @execution }
end

#record_event(event) ⇒ Object

Canonical runtime recording is independent of Application callback health. Once an event is observed it is appended even after a listener has failed.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 129

def record_event(event)
  listener = @mutex.synchronize do
    @runtime_events << event
    @callback_failure ? nil : @application_listener
  end
  return unless listener

  listener.call(event)
rescue => callback_error
  failure = ApplicationCallbackFailure.new(
    event_type: event.type, error: callback_error
  )
  @mutex.synchronize do
    @callback_failure ||= failure
    @application_listener = nil
  end
  notify_callback_failure(failure)
end

#record_llm_result(response:, error:, streaming:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 69

def record_llm_result(response:, error:, streaming:)
  @mutex.synchronize do
    active_call = @active_call
    unless active_call
      raise Phronomy::Error, "LLM result arrived without an active Provider Call"
    end
    @llm_results << {
      llm_call_id: active_call.fetch(:llm_call_id),
      response: response,
      error: error,
      streaming: streaming,
      manifest_ref: active_call.fetch(:manifest_ref),
      started_at: active_call.fetch(:started_at)
    }
    @active_call = nil
  end
end

#replace_execution(value) ⇒ Object



38
39
40
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 38

def replace_execution(value)
  @mutex.synchronize { @execution = value }
end

#replace_runtime_projection(value) ⇒ Object



46
47
48
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 46

def replace_runtime_projection(value)
  @mutex.synchronize { @runtime_projection = value }
end

#runtime_projectionObject



42
43
44
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 42

def runtime_projection
  @mutex.synchronize { @runtime_projection }
end

#runtime_snapshotObject



87
88
89
90
91
92
93
94
95
# File 'lib/phronomy/agent/agent_execution_activation.rb', line 87

def runtime_snapshot
  @mutex.synchronize do
    {
      llm_results: @llm_results.dup,
      runtime_events: @runtime_events.dup,
      active_call: @active_call&.dup
    }
  end
end