ruby_llm-ag_ui 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +44 -0
- data/CONTRIBUTING.md +27 -3
- data/README.md +39 -115
- data/docs/_advanced/client-example.md +107 -0
- data/docs/_advanced/rails-integration.md +5 -2
- data/docs/_advanced/security-and-deployment.md +1 -1
- data/docs/_advanced/worker-recovery.md +104 -0
- data/docs/_core_features/cancellation.md +84 -0
- data/docs/_core_features/reconnects.md +13 -1
- data/docs/_core_features/streaming-runs.md +15 -0
- data/docs/_core_features/tool-progress.md +90 -0
- data/docs/_getting_started/configuration.md +2 -1
- data/docs/_getting_started/whats-new-in-0-3.md +61 -0
- data/docs/_reference/compatibility.md +83 -15
- data/docs/_reference/protocol-coverage.md +6 -5
- data/docs/_reference/release-history.md +4 -2
- data/docs/_reference/releasing.md +25 -0
- data/docs/_reference/upgrading.md +32 -18
- data/docs/{_getting_started → _reference}/whats-new-in-0-1.md +2 -1
- data/docs/_reference/whats-new-in-0-2.md +54 -0
- data/lib/ruby_llm/ag_ui/configuration.rb +7 -1
- data/lib/ruby_llm/ag_ui/coverage.rb +2 -2
- data/lib/ruby_llm/ag_ui/emitter.rb +5 -1
- data/lib/ruby_llm/ag_ui/error.rb +13 -2
- data/lib/ruby_llm/ag_ui/error_sanitizer.rb +1 -0
- data/lib/ruby_llm/ag_ui/mapper.rb +20 -1
- data/lib/ruby_llm/ag_ui/persistence.rb +18 -0
- data/lib/ruby_llm/ag_ui/rails/active_record_store.rb +21 -11
- data/lib/ruby_llm/ag_ui/rails/record.rb +8 -3
- data/lib/ruby_llm/ag_ui/rails/streaming.rb +3 -3
- data/lib/ruby_llm/ag_ui/redaction.rb +1 -0
- data/lib/ruby_llm/ag_ui/resume.rb +14 -6
- data/lib/ruby_llm/ag_ui/run.rb +33 -23
- data/lib/ruby_llm/ag_ui/run_record.rb +17 -4
- data/lib/ruby_llm/ag_ui/sse_writer.rb +16 -4
- data/lib/ruby_llm/ag_ui/store/base.rb +25 -2
- data/lib/ruby_llm/ag_ui/store/memory_store.rb +3 -7
- data/lib/ruby_llm/ag_ui/version.rb +1 -1
- metadata +30 -3
data/lib/ruby_llm/ag_ui/error.rb
CHANGED
|
@@ -37,6 +37,10 @@ module RubyLLM
|
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
+
# An observed run was changed by another writer. Reload before deciding
|
|
41
|
+
# whether an update is still valid; never overwrite the newer outcome.
|
|
42
|
+
class StaleRecordError < RunConflictError; end
|
|
43
|
+
|
|
40
44
|
# Raised when run identity is required but missing.
|
|
41
45
|
class InvalidRunIdentityError < InvalidInputError; end
|
|
42
46
|
|
|
@@ -56,10 +60,17 @@ module RubyLLM
|
|
|
56
60
|
# Raised when the pinned protocol version is not the one this gem speaks.
|
|
57
61
|
class UnsupportedProtocolVersionError < InputError; end
|
|
58
62
|
|
|
59
|
-
# Raised when
|
|
60
|
-
# transport died mid-run). The run is reported as RUN_ERROR.
|
|
63
|
+
# Raised when run or stream ordering cannot be honored.
|
|
61
64
|
class StreamError < Error; end
|
|
62
65
|
|
|
66
|
+
# The response transport is closed. A running producer detaches its writer
|
|
67
|
+
# and continues to persist the real outcome while its worker remains alive.
|
|
68
|
+
class TransportError < StreamError; end
|
|
69
|
+
|
|
70
|
+
# A store write failed without a reliable acknowledgement. The persisted
|
|
71
|
+
# outcome may be unknown; inspect storage before attempting recovery.
|
|
72
|
+
class PersistenceError < Error; end
|
|
73
|
+
|
|
63
74
|
# Raised when RubyLLM paused the run for something the pinned AG-UI shape
|
|
64
75
|
# cannot express as an interrupt this producer may answer, such as an MCP
|
|
65
76
|
# input request or a long-running MCP task. Reported as a sanitized
|
|
@@ -40,6 +40,7 @@ module RubyLLM
|
|
|
40
40
|
],
|
|
41
41
|
'RubyLLM::AgUI::StreamError' => ['The run could not be streamed to completion.', 'stream_failed'],
|
|
42
42
|
'RubyLLM::Error' => ['The model provider returned an error.', 'provider_error'],
|
|
43
|
+
'Faraday::TimeoutError' => ['The model provider request timed out.', 'transport_timeout'],
|
|
43
44
|
'Faraday::Error' => ['The model provider request failed.', 'transport_error'],
|
|
44
45
|
'Timeout::Error' => ['The model provider request timed out.', 'transport_timeout'],
|
|
45
46
|
'Errno::ETIMEDOUT' => ['The model provider request timed out.', 'transport_timeout']
|
|
@@ -53,6 +53,7 @@ module RubyLLM
|
|
|
53
53
|
@last_message_id = nil
|
|
54
54
|
@text_emitted = false
|
|
55
55
|
@calls = {}
|
|
56
|
+
@completed_tools = {}
|
|
56
57
|
@open_step = nil
|
|
57
58
|
@step_kind = nil
|
|
58
59
|
@warned_redaction = false
|
|
@@ -95,6 +96,23 @@ module RubyLLM
|
|
|
95
96
|
events
|
|
96
97
|
end
|
|
97
98
|
|
|
99
|
+
# Reports a tool's actual progress as a replacing activity snapshot.
|
|
100
|
+
# Each call has one run-scoped activity id. Values pass through the host's
|
|
101
|
+
# :tool_progress redaction policy; the tool call id remains structural.
|
|
102
|
+
# Empty updates and callbacks for finished tools or runs produce nothing.
|
|
103
|
+
def tool_progress(tool_call, progress)
|
|
104
|
+
return [] if @closed || @completed_tools[tool_call.id]
|
|
105
|
+
|
|
106
|
+
payload = { 'value' => progress.value, 'total' => progress.total, 'message' => progress.message }.compact
|
|
107
|
+
return [] if payload.empty?
|
|
108
|
+
|
|
109
|
+
[::AgUiProtocol::Core::Events::ActivitySnapshotEvent.new(
|
|
110
|
+
message_id: "#{@run_id}:progress:#{tool_call.id}", activity_type: 'TOOL_PROGRESS',
|
|
111
|
+
content: { 'toolCallId' => tool_call.id, 'progress' => redact(Redaction::TOOL_PROGRESS, payload) },
|
|
112
|
+
replace: true
|
|
113
|
+
)]
|
|
114
|
+
end
|
|
115
|
+
|
|
98
116
|
# Closes any open text message, tool call and step, and closes the mapper.
|
|
99
117
|
#
|
|
100
118
|
# The caller writes these before the terminal event: AG-UI has no way to
|
|
@@ -261,6 +279,7 @@ module RubyLLM
|
|
|
261
279
|
# --- tool results --------------------------------------------------------
|
|
262
280
|
|
|
263
281
|
def tool_result_events(message)
|
|
282
|
+
@completed_tools[message.tool_call_id] = true
|
|
264
283
|
events = close_text_events
|
|
265
284
|
events.concat(close_tool_calls)
|
|
266
285
|
events.concat(ensure_step(STEP_TOOL))
|
|
@@ -320,7 +339,7 @@ module RubyLLM
|
|
|
320
339
|
return if @warned_redaction
|
|
321
340
|
|
|
322
341
|
@warned_redaction = true
|
|
323
|
-
@logger&.warn('[ruby_llm_ag_ui] tool results are redacted by default; ' \
|
|
342
|
+
@logger&.warn('[ruby_llm_ag_ui] tool results and progress are redacted by default; ' \
|
|
324
343
|
'configure RubyLLM::AgUI.config.redact to publish them')
|
|
325
344
|
end
|
|
326
345
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyLLM
|
|
4
|
+
module AgUI
|
|
5
|
+
# Keep uncertain write acknowledgements separate from run outcomes.
|
|
6
|
+
module Persistence # :nodoc:
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def write
|
|
10
|
+
yield
|
|
11
|
+
rescue Error
|
|
12
|
+
raise
|
|
13
|
+
rescue StandardError => e
|
|
14
|
+
raise PersistenceError, "The store write could not be acknowledged: #{e.class}"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -50,10 +50,8 @@ module RubyLLM
|
|
|
50
50
|
def save(record)
|
|
51
51
|
scope_key(record.scope)
|
|
52
52
|
row = row_for(record.scope, record.thread_id, record.run_id)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
row.write_run_record(record)
|
|
53
|
+
updated = next_record(row&.as_run_record, record)
|
|
54
|
+
row.write_run_record(updated)
|
|
57
55
|
end
|
|
58
56
|
|
|
59
57
|
def find_interrupt(scope:, thread_id:, interrupt_id:)
|
|
@@ -62,21 +60,34 @@ module RubyLLM
|
|
|
62
60
|
row_for_interrupt(scope, thread_id, interrupt_id)&.as_run_record&.interrupt(interrupt_id)
|
|
63
61
|
end
|
|
64
62
|
|
|
65
|
-
# Records +decision+
|
|
66
|
-
#
|
|
63
|
+
# Records +decision+ with a conditional update. Competing writers reload
|
|
64
|
+
# and revalidate, so only one can redeem the approval.
|
|
67
65
|
def redeem_interrupt(scope:, thread_id:, interrupt_id:, decision:, now:)
|
|
68
66
|
unless Interrupt::DECISIONS.include?(decision)
|
|
69
67
|
raise ConfigurationError, "decision must be one of #{Interrupt::DECISIONS.join(', ')}"
|
|
70
68
|
end
|
|
71
69
|
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
retry_stale do
|
|
71
|
+
row = row_for_interrupt(scope, thread_id, interrupt_id)
|
|
72
|
+
raise ResumeRejectedError, "no interrupt #{interrupt_id}" if row.nil?
|
|
74
73
|
|
|
75
|
-
|
|
74
|
+
redeem(row.as_run_record, interrupt_id, decision, now)
|
|
75
|
+
end
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
private
|
|
79
79
|
|
|
80
|
+
def retry_stale
|
|
81
|
+
attempts = 0
|
|
82
|
+
begin
|
|
83
|
+
yield
|
|
84
|
+
rescue StaleRecordError
|
|
85
|
+
attempts += 1
|
|
86
|
+
retry if attempts < 3
|
|
87
|
+
raise
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
80
91
|
# A retry of the same identity attaches; a different input under the
|
|
81
92
|
# same key is a conflict, because one run id may not mean two requests.
|
|
82
93
|
def attach(row, digest)
|
|
@@ -91,8 +102,7 @@ module RubyLLM
|
|
|
91
102
|
raise ResumeRejectedError, "interrupt #{interrupt_id} expired" if interrupt.expired?(now)
|
|
92
103
|
|
|
93
104
|
answered = interrupt.decide(decision, now: now)
|
|
94
|
-
|
|
95
|
-
row.write_run_record(record.replace_interrupt(answered, now: now))
|
|
105
|
+
save(record.replace_interrupt(answered, now: now))
|
|
96
106
|
Redemption.new(interrupt: answered, replayed: false)
|
|
97
107
|
end
|
|
98
108
|
|
|
@@ -28,13 +28,18 @@ module RubyLLM
|
|
|
28
28
|
::RubyLLM::AgUI::RunRecord.from_storage(::JSON.parse(payload))
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
-
# Writes +record+
|
|
31
|
+
# Writes +record+ only if this row's observed payload is still current.
|
|
32
|
+
# Returns the record or raises StaleRecordError on a competing update.
|
|
32
33
|
#
|
|
33
34
|
# The identity columns are not writable: a row keeps the identity it was
|
|
34
35
|
# reserved with, so a record cannot be stored under another run's key.
|
|
35
36
|
def write_run_record(record)
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
changed = self.class.where(id: id, payload: payload).update_all(
|
|
38
|
+
digest: record.digest, status: record.status.to_s, sequence: record.sequence,
|
|
39
|
+
payload: ::JSON.generate(record.to_storage)
|
|
40
|
+
)
|
|
41
|
+
raise StaleRecordError, "run #{record.run_id} changed before it could be saved" unless changed == 1
|
|
42
|
+
|
|
38
43
|
record
|
|
39
44
|
end
|
|
40
45
|
|
|
@@ -45,9 +45,9 @@ module RubyLLM
|
|
|
45
45
|
include ::ActionController::Live
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
-
# Streams one run and returns
|
|
49
|
-
#
|
|
50
|
-
#
|
|
48
|
+
# Streams one run and returns its record. A disconnected transport does
|
|
49
|
+
# not stop its worker from completing and storing the real outcome.
|
|
50
|
+
# The block receives the parsed input and returns the authorized chat.
|
|
51
51
|
def stream_ag_ui_run(&resolve_chat)
|
|
52
52
|
if resolve_chat.nil?
|
|
53
53
|
raise ConfigurationError, 'stream_ag_ui_run needs a block that returns the chat for the input'
|
|
@@ -32,9 +32,13 @@ module RubyLLM
|
|
|
32
32
|
@input.resume?
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
# Checks
|
|
36
|
-
#
|
|
35
|
+
# Checks a new resume and builds its plan before the run opens a stream.
|
|
36
|
+
# An existing identity is left to reservation and reconnect: an exact
|
|
37
|
+
# retry reports its recorded outcome, never redeems the decision twice.
|
|
37
38
|
def plan(now:)
|
|
39
|
+
existing = @store.find(scope: @scope, thread_id: @input.thread_id, run_id: @input.run_id)
|
|
40
|
+
return self if existing
|
|
41
|
+
|
|
38
42
|
source = interrupted_run
|
|
39
43
|
@plan = @input.resume.map { |entry| plan_entry(source, entry, now) }
|
|
40
44
|
self
|
|
@@ -84,15 +88,19 @@ module RubyLLM
|
|
|
84
88
|
# The store's redemption is atomic: the first answer record wins, and a
|
|
85
89
|
# repeat comes back as a replay that must not execute anything.
|
|
86
90
|
def redeem(planned, now:)
|
|
87
|
-
redemption =
|
|
88
|
-
scope: @scope, thread_id: @input.thread_id,
|
|
89
|
-
interrupt_id: planned.interrupt.id, decision: planned.decision, now: now
|
|
90
|
-
)
|
|
91
|
+
redemption = persist_decision(planned, now: now)
|
|
91
92
|
raise ResumeRejectedError, "interrupt #{planned.interrupt.id} was already answered" if redemption.replayed?
|
|
92
93
|
|
|
93
94
|
redemption.approved? ? @chat.approve(redemption.tool_call_id) : @chat.deny(redemption.tool_call_id)
|
|
94
95
|
end
|
|
95
96
|
|
|
97
|
+
def persist_decision(planned, now:)
|
|
98
|
+
Persistence.write do
|
|
99
|
+
@store.redeem_interrupt(scope: @scope, thread_id: @input.thread_id,
|
|
100
|
+
interrupt_id: planned.interrupt.id, decision: planned.decision, now: now)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
96
104
|
# The protocol's resume vocabulary is resolved or cancelled; the interrupt
|
|
97
105
|
# records the truthful decision, and a cancelled answer denies the call
|
|
98
106
|
# rather than approving it.
|
data/lib/ruby_llm/ag_ui/run.rb
CHANGED
|
@@ -25,15 +25,17 @@ module RubyLLM
|
|
|
25
25
|
|
|
26
26
|
# rubocop:disable-next Metrics/ParameterLists -- one keyword per host-supplied or overridable value
|
|
27
27
|
def initialize(chat:, input:, scope:, store: nil, logger: nil, redact: nil, clock: nil, expires_in: nil,
|
|
28
|
-
state: nil)
|
|
28
|
+
state: nil, emit_tool_progress: RubyLLM::AgUI.config.emit_tool_progress)
|
|
29
|
+
defaults = RubyLLM::AgUI.config
|
|
29
30
|
@chat = chat
|
|
30
31
|
@input = input
|
|
31
32
|
@scope = scope
|
|
32
|
-
@store = store ||
|
|
33
|
-
@logger = logger ||
|
|
33
|
+
@store = store || defaults.run_store!
|
|
34
|
+
@logger = logger || defaults.logger
|
|
34
35
|
@redact = redact
|
|
35
|
-
@clock = clock ||
|
|
36
|
-
@expires_in = expires_in ||
|
|
36
|
+
@clock = clock || defaults.clock
|
|
37
|
+
@expires_in = expires_in || defaults.interrupt_ttl
|
|
38
|
+
@emit_tool_progress = emit_tool_progress
|
|
37
39
|
@mutex = Mutex.new
|
|
38
40
|
validate!
|
|
39
41
|
@state = state.nil? ? @input.state : state
|
|
@@ -62,9 +64,9 @@ module RubyLLM
|
|
|
62
64
|
end
|
|
63
65
|
|
|
64
66
|
def reserve
|
|
65
|
-
|
|
66
|
-
scope: @scope, thread_id: @input.thread_id, run_id: @input.run_id, digest: @input.digest
|
|
67
|
-
|
|
67
|
+
Persistence.write do
|
|
68
|
+
@store.reserve(scope: @scope, thread_id: @input.thread_id, run_id: @input.run_id, digest: @input.digest)
|
|
69
|
+
end
|
|
68
70
|
end
|
|
69
71
|
|
|
70
72
|
# An identity that is already reserved is never executed a second time.
|
|
@@ -84,22 +86,30 @@ module RubyLLM
|
|
|
84
86
|
end
|
|
85
87
|
|
|
86
88
|
def execute(record, to)
|
|
87
|
-
|
|
88
|
-
@emitter = build_emitter(record, to, mapper: @mapper)
|
|
89
|
-
@emitter.start(run_started_event(record), state: @state)
|
|
89
|
+
start_execution(record, to)
|
|
90
90
|
resume.apply(now: @clock.call) if resume.needed?
|
|
91
91
|
drive
|
|
92
|
-
@record = finish
|
|
92
|
+
@record = finalize { finish }
|
|
93
93
|
rescue RubyLLM::CancelledError
|
|
94
|
-
@record = @emitter.cancel
|
|
94
|
+
@record = finalize { @emitter.cancel }
|
|
95
|
+
rescue PersistenceError, RunConflictError
|
|
96
|
+
raise
|
|
95
97
|
rescue StandardError => e
|
|
96
|
-
@record = @emitter.fail(e)
|
|
98
|
+
@record = finalize { @emitter.fail(e) }
|
|
99
|
+
ensure
|
|
100
|
+
finalize { @mapper&.close }
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def start_execution(record, to)
|
|
104
|
+
@mapper = build_mapper(record)
|
|
105
|
+
@emitter = build_emitter(record, to, mapper: @mapper)
|
|
106
|
+
@emitter.start(run_started_event(record), state: @state)
|
|
97
107
|
end
|
|
98
108
|
|
|
99
109
|
def build_emitter(record, to, mapper: build_mapper(record))
|
|
100
110
|
Emitter.new(
|
|
101
|
-
writer: SSEWriter.new(to, logger: @logger), mapper: mapper,
|
|
102
|
-
|
|
111
|
+
writer: SSEWriter.new(to, logger: @logger), mapper: mapper, store: @store,
|
|
112
|
+
record: record, clock: @clock, logger: @logger
|
|
103
113
|
)
|
|
104
114
|
end
|
|
105
115
|
|
|
@@ -113,9 +123,14 @@ module RubyLLM
|
|
|
113
123
|
def drive
|
|
114
124
|
@chat.before_tool_call { |tool_call| project { @mapper.before_tool_call(tool_call) } }
|
|
115
125
|
@chat.after_message { |message| project { @mapper.message(message) } }
|
|
126
|
+
if @emit_tool_progress == true
|
|
127
|
+
@chat.after_tool_progress { |call, progress| project { @mapper.tool_progress(call, progress) } }
|
|
128
|
+
end
|
|
116
129
|
@chat.complete { |chunk| project { @mapper.chunk(chunk) } }
|
|
117
130
|
end
|
|
118
131
|
|
|
132
|
+
def finalize(&) = @mutex.synchronize(&)
|
|
133
|
+
|
|
119
134
|
def project
|
|
120
135
|
@mutex.synchronize { @emitter.write(yield) }
|
|
121
136
|
end
|
|
@@ -153,8 +168,7 @@ module RubyLLM
|
|
|
153
168
|
|
|
154
169
|
def run_started_event(record)
|
|
155
170
|
Protocol.run_started(
|
|
156
|
-
thread_id: record.thread_id, run_id: record.run_id,
|
|
157
|
-
parent_run_id: @input.parent_run_id, input: @input.to_h
|
|
171
|
+
thread_id: record.thread_id, run_id: record.run_id, parent_run_id: @input.parent_run_id, input: @input.to_h
|
|
158
172
|
)
|
|
159
173
|
end
|
|
160
174
|
|
|
@@ -164,13 +178,9 @@ module RubyLLM
|
|
|
164
178
|
end
|
|
165
179
|
raise ConfigurationError, 'input must be a RubyLLM::AgUI::Input' unless @input.is_a?(Input)
|
|
166
180
|
|
|
167
|
-
validate_scope!
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
def validate_scope!
|
|
171
181
|
return if @scope.is_a?(String) && !@scope.empty?
|
|
172
182
|
|
|
173
|
-
raise ConfigurationError,
|
|
183
|
+
raise ConfigurationError, Store::Base::SCOPE_MESSAGE
|
|
174
184
|
end
|
|
175
185
|
end
|
|
176
186
|
end
|
|
@@ -52,6 +52,10 @@ module RubyLLM
|
|
|
52
52
|
# reconnect are ordered by it rather than by wall-clock time.
|
|
53
53
|
attr_reader :sequence
|
|
54
54
|
|
|
55
|
+
# Storage revision, distinct from the event sequence. Stores increment it
|
|
56
|
+
# atomically and reject writes from older snapshots.
|
|
57
|
+
attr_reader :revision
|
|
58
|
+
|
|
55
59
|
# The host-owned run state kept for a reconnect snapshot, or nil.
|
|
56
60
|
attr_reader :snapshot
|
|
57
61
|
|
|
@@ -67,13 +71,14 @@ module RubyLLM
|
|
|
67
71
|
|
|
68
72
|
# rubocop:disable-next Metrics/ParameterLists -- one keyword per field the record owns
|
|
69
73
|
def initialize(scope:, thread_id:, run_id:, digest:, created_at:, status: RESERVED, sequence: 0,
|
|
70
|
-
snapshot: nil, interrupts: [], failure: nil, updated_at: nil)
|
|
74
|
+
snapshot: nil, interrupts: [], failure: nil, updated_at: nil, revision: 0)
|
|
71
75
|
@scope = scope
|
|
72
76
|
@thread_id = thread_id
|
|
73
77
|
@run_id = run_id
|
|
74
78
|
@digest = digest
|
|
75
79
|
@status = assert_status!(status)
|
|
76
80
|
@sequence = sequence
|
|
81
|
+
@revision = revision
|
|
77
82
|
@snapshot = snapshot
|
|
78
83
|
@interrupts = interrupts.freeze
|
|
79
84
|
@failure = failure
|
|
@@ -122,6 +127,11 @@ module RubyLLM
|
|
|
122
127
|
with(now: now, sequence: sequence + 1)
|
|
123
128
|
end
|
|
124
129
|
|
|
130
|
+
# Storage bookkeeping; hosts retain the record returned by save.
|
|
131
|
+
def next_revision # :nodoc:
|
|
132
|
+
with(now: updated_at, revision: revision + 1)
|
|
133
|
+
end
|
|
134
|
+
|
|
125
135
|
# Closes the run with +status+: :succeeded, :interrupted, :cancelled or
|
|
126
136
|
# :failed, and, for a failure, the sanitized error a reconnect reports.
|
|
127
137
|
# Re-applying the same terminal status is an idempotent no-op — so the
|
|
@@ -158,6 +168,7 @@ module RubyLLM
|
|
|
158
168
|
with(now: now, interrupts: replaced)
|
|
159
169
|
end
|
|
160
170
|
|
|
171
|
+
# rubocop:disable-next Metrics/AbcSize -- one entry per serialized record field
|
|
161
172
|
def to_h
|
|
162
173
|
{
|
|
163
174
|
'scope' => scope.to_s,
|
|
@@ -166,6 +177,7 @@ module RubyLLM
|
|
|
166
177
|
'digest' => digest,
|
|
167
178
|
'status' => status.to_s,
|
|
168
179
|
'sequence' => sequence,
|
|
180
|
+
'revision' => revision,
|
|
169
181
|
'snapshot' => snapshot,
|
|
170
182
|
'failure' => failure,
|
|
171
183
|
'interrupts' => interrupts.map(&:to_h),
|
|
@@ -186,7 +198,8 @@ module RubyLLM
|
|
|
186
198
|
new(
|
|
187
199
|
scope: hash.fetch('scope'), thread_id: hash.fetch('threadId'), run_id: hash.fetch('runId'),
|
|
188
200
|
digest: hash.fetch('digest'), status: hash.fetch('status').to_sym,
|
|
189
|
-
sequence: hash.fetch('sequence'),
|
|
201
|
+
sequence: hash.fetch('sequence'), revision: hash.fetch('revision', 0),
|
|
202
|
+
snapshot: hash['snapshot'], failure: hash['failure'],
|
|
190
203
|
interrupts: interrupts_from_storage(hash['interrupts']),
|
|
191
204
|
created_at: Interrupt.parse_time(hash.fetch('createdAt')),
|
|
192
205
|
updated_at: Interrupt.parse_time(hash.fetch('updatedAt'))
|
|
@@ -201,10 +214,10 @@ module RubyLLM
|
|
|
201
214
|
|
|
202
215
|
# rubocop:disable-next Metrics/ParameterLists -- one keyword per field, each defaulted to the record's own
|
|
203
216
|
def with(now:, status: self.status, sequence: self.sequence, snapshot: self.snapshot,
|
|
204
|
-
interrupts: self.interrupts, failure: self.failure)
|
|
217
|
+
interrupts: self.interrupts, failure: self.failure, revision: self.revision)
|
|
205
218
|
self.class.new(
|
|
206
219
|
scope: scope, thread_id: thread_id, run_id: run_id, digest: digest, status: status,
|
|
207
|
-
sequence: sequence, snapshot: snapshot, interrupts: interrupts, failure: failure,
|
|
220
|
+
sequence: sequence, revision: revision, snapshot: snapshot, interrupts: interrupts, failure: failure,
|
|
208
221
|
created_at: created_at, updated_at: now
|
|
209
222
|
)
|
|
210
223
|
end
|
|
@@ -10,7 +10,7 @@ module RubyLLM
|
|
|
10
10
|
# buffer frames or choose how a host relays a background job to a client.
|
|
11
11
|
# What it does own is the ordering and the framing of the events it writes.
|
|
12
12
|
#
|
|
13
|
-
# A dead stream is reported as a
|
|
13
|
+
# A dead stream is reported as a TransportError and the writer stays closed.
|
|
14
14
|
# The reason is a transport truncation, not a run outcome: it is never
|
|
15
15
|
# evidence that the work finished, succeeded or was cancelled.
|
|
16
16
|
class SSEWriter
|
|
@@ -82,7 +82,7 @@ module RubyLLM
|
|
|
82
82
|
# Encodes +event+ and writes it, then flushes so the client sees it now.
|
|
83
83
|
# Returns the encoded frame.
|
|
84
84
|
def write(event)
|
|
85
|
-
raise
|
|
85
|
+
raise TransportError, 'The event stream is closed.' if @closed
|
|
86
86
|
|
|
87
87
|
frame = @encoder.encode(event)
|
|
88
88
|
@stream.write(frame)
|
|
@@ -90,12 +90,14 @@ module RubyLLM
|
|
|
90
90
|
@events_written += 1
|
|
91
91
|
@bytes_written += frame.bytesize
|
|
92
92
|
frame
|
|
93
|
-
rescue
|
|
93
|
+
rescue StandardError => e
|
|
94
|
+
raise unless transport_error?(e)
|
|
95
|
+
|
|
94
96
|
# The client is gone. The class name is enough for the host log; the
|
|
95
97
|
# message may embed a path or a socket address and is not echoed.
|
|
96
98
|
@logger&.warn("[ruby_llm_ag_ui] the event stream failed: #{e.class}")
|
|
97
99
|
@closed = true
|
|
98
|
-
raise
|
|
100
|
+
raise TransportError, "The event stream failed: #{e.class}"
|
|
99
101
|
end
|
|
100
102
|
|
|
101
103
|
# Pushes buffered bytes to the client. A no-op for a stream that cannot
|
|
@@ -114,6 +116,16 @@ module RubyLLM
|
|
|
114
116
|
def closed?
|
|
115
117
|
@closed
|
|
116
118
|
end
|
|
119
|
+
|
|
120
|
+
private
|
|
121
|
+
|
|
122
|
+
def transport_error?(error)
|
|
123
|
+
return true if error.is_a?(IOError) || error.is_a?(SystemCallError)
|
|
124
|
+
return true if defined?(::Puma::ConnectionError) && error.is_a?(::Puma::ConnectionError)
|
|
125
|
+
|
|
126
|
+
defined?(::ActionController::Live::ClientDisconnected) &&
|
|
127
|
+
error.is_a?(::ActionController::Live::ClientDisconnected)
|
|
128
|
+
end
|
|
117
129
|
end
|
|
118
130
|
end
|
|
119
131
|
end
|
|
@@ -9,7 +9,7 @@ module RubyLLM
|
|
|
9
9
|
# already in progress instead of executing twice, and it keeps the
|
|
10
10
|
# interrupts that a later resume has to answer.
|
|
11
11
|
#
|
|
12
|
-
# Two stores ship with the gem: Store::MemoryStore
|
|
12
|
+
# Two stores ship with the gem: Store::MemoryStore for
|
|
13
13
|
# a single process, and the optional Rails adapter's ActiveRecord store. A
|
|
14
14
|
# host may implement this interface instead; every method must be safe to
|
|
15
15
|
# call from concurrent threads.
|
|
@@ -63,11 +63,24 @@ module RubyLLM
|
|
|
63
63
|
raise NotImplementedError
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
-
#
|
|
66
|
+
# Atomically persists a record only if its revision is still current.
|
|
67
|
+
# Returns a new record with its revision incremented. Retain this return
|
|
68
|
+
# value for later writes. A stale write raises StaleRecordError.
|
|
67
69
|
def save(record)
|
|
68
70
|
raise NotImplementedError
|
|
69
71
|
end
|
|
70
72
|
|
|
73
|
+
# Closes an observed non-terminal run after the host confirms its worker
|
|
74
|
+
# has stopped. Does not cancel a worker, undo effects, release identity,
|
|
75
|
+
# or execute anything. A newer revision or terminal run is refused.
|
|
76
|
+
def recover(record)
|
|
77
|
+
raise RunConflictError, 'only a non-terminal run can be recovered' if record.terminal?
|
|
78
|
+
|
|
79
|
+
failure = { 'code' => 'worker_lost',
|
|
80
|
+
'message' => 'The worker stopped before recording an outcome. Tool effects may be incomplete.' }
|
|
81
|
+
save(record.finish(RunRecord::FAILED, now: now, failure: failure))
|
|
82
|
+
end
|
|
83
|
+
|
|
71
84
|
# The interrupt with +id+ anywhere on the thread, or nil.
|
|
72
85
|
def find_interrupt(scope:, thread_id:, interrupt_id:)
|
|
73
86
|
raise NotImplementedError
|
|
@@ -88,6 +101,16 @@ module RubyLLM
|
|
|
88
101
|
|
|
89
102
|
private
|
|
90
103
|
|
|
104
|
+
def next_record(stored, candidate)
|
|
105
|
+
raise InvalidRunIdentityError, "run #{candidate.run_id} was never reserved" if stored.nil?
|
|
106
|
+
raise RunConflictError, conflict_message(candidate) if stored.digest != candidate.digest
|
|
107
|
+
if stored.revision != candidate.revision || (stored.terminal? && stored.status != candidate.status)
|
|
108
|
+
raise StaleRecordError, "run #{candidate.run_id} changed; reload its record before writing"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
candidate.next_revision
|
|
112
|
+
end
|
|
113
|
+
|
|
91
114
|
# The host's scope must be a canonical string. An arbitrary object would
|
|
92
115
|
# make two requests from the same tenant hash differently, so a retry
|
|
93
116
|
# would not attach and the run would execute twice.
|
|
@@ -55,13 +55,8 @@ module RubyLLM
|
|
|
55
55
|
def save(record)
|
|
56
56
|
key = key_for(record.scope, record.thread_id, record.run_id)
|
|
57
57
|
@lock.synchronize do
|
|
58
|
-
|
|
59
|
-
raise InvalidRunIdentityError, "run #{record.run_id} was never reserved" if stored.nil?
|
|
60
|
-
raise RunConflictError, conflict_message(record) if stored.digest != record.digest
|
|
61
|
-
|
|
62
|
-
@records[key] = record
|
|
58
|
+
@records[key] = next_record(@records[key], record)
|
|
63
59
|
end
|
|
64
|
-
record
|
|
65
60
|
end
|
|
66
61
|
|
|
67
62
|
def find_interrupt(scope:, thread_id:, interrupt_id:)
|
|
@@ -89,7 +84,8 @@ module RubyLLM
|
|
|
89
84
|
raise ResumeRejectedError, "interrupt #{interrupt_id} expired" if interrupt.expired?(now)
|
|
90
85
|
|
|
91
86
|
answered = interrupt.decide(decision, now: now)
|
|
92
|
-
@records[key_for(scope, thread_id, record.run_id)] =
|
|
87
|
+
@records[key_for(scope, thread_id, record.run_id)] =
|
|
88
|
+
record.replace_interrupt(answered, now: now).next_revision
|
|
93
89
|
Redemption.new(interrupt: answered, replayed: false)
|
|
94
90
|
end
|
|
95
91
|
|