a2a-rails 0.3.0 → 0.5.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 +30 -1
- data/README.md +153 -86
- data/app/controllers/a2a/rails/requests_controller.rb +37 -6
- data/lib/a2a/rails/client.rb +2 -1
- data/lib/a2a/rails/configuration.rb +8 -4
- data/lib/a2a/rails/dispatcher.rb +13 -0
- data/lib/a2a/rails/errors.rb +14 -0
- data/lib/a2a/rails/input_required.rb +50 -0
- data/lib/a2a/rails/protocol/agent2agent_adapter.rb +23 -2
- data/lib/a2a/rails/protocol/part_data.rb +89 -0
- data/lib/a2a/rails/protocol/request_handler.rb +115 -36
- data/lib/a2a/rails/protocol/task_mapper.rb +16 -5
- data/lib/a2a/rails/skill.rb +2 -0
- data/lib/a2a/rails/task/active_record_store.rb +96 -12
- data/lib/a2a/rails/task/lifecycle.rb +33 -12
- data/lib/a2a/rails/task/memory_store.rb +42 -6
- data/lib/a2a/rails/task/store.rb +77 -0
- data/lib/a2a/rails/task.rb +1 -1
- data/lib/a2a/rails/task_execution_arguments.rb +8 -3
- data/lib/a2a/rails/task_execution_job.rb +47 -12
- data/lib/a2a/rails/version.rb +1 -1
- data/lib/a2a-rails.rb +1 -0
- data/lib/generators/a2a/rails/task_continuation_generator.rb +25 -0
- data/lib/generators/a2a/rails/task_store_generator.rb +7 -0
- data/lib/generators/a2a/rails/templates/add_execution_to_a2a_rails_tasks.rb.tt +7 -0
- metadata +5 -1
|
@@ -23,13 +23,14 @@ module A2A
|
|
|
23
23
|
|
|
24
24
|
def initialize(cursor_secret:, clock: -> { Time.now.utc }, retention: nil,
|
|
25
25
|
prune_batch_size: 1_000, max_tasks_per_owner: nil,
|
|
26
|
-
max_history_entries: 100, max_artifacts: 50)
|
|
26
|
+
max_history_entries: 100, max_artifacts: 50, input_required_retention: nil)
|
|
27
27
|
unless cursor_secret.is_a?(String) && cursor_secret.bytesize >= 32
|
|
28
28
|
raise ArgumentError, "cursor_secret must be at least 32 bytes"
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
@clock = clock
|
|
32
32
|
@retention_seconds = normalize_retention(retention)
|
|
33
|
+
@input_required_retention = normalize_retention(input_required_retention)
|
|
33
34
|
@prune_batch_size = normalize_prune_batch_size(prune_batch_size)
|
|
34
35
|
@max_tasks_per_owner = normalize_optional_positive_integer(
|
|
35
36
|
max_tasks_per_owner,
|
|
@@ -56,7 +57,7 @@ module A2A
|
|
|
56
57
|
record = Record.transaction do
|
|
57
58
|
enforce_owner_quota!(task[:owner_id], at: now)
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
attributes = {
|
|
60
61
|
task_id: task.fetch(:id),
|
|
61
62
|
owner_id: task[:owner_id],
|
|
62
63
|
context_id: task.fetch(:context_id),
|
|
@@ -66,7 +67,12 @@ module A2A
|
|
|
66
67
|
history: task.key?(:history) ? deep_stringify(task[:history]) : nil,
|
|
67
68
|
artifacts: task.key?(:artifacts) ? deep_stringify(task[:artifacts]) : nil,
|
|
68
69
|
expires_at: terminal_state?(state) ? expiration_at(timestamp) : nil
|
|
69
|
-
|
|
70
|
+
}
|
|
71
|
+
if task[:execution]
|
|
72
|
+
require_execution_column!
|
|
73
|
+
attributes[:execution] = deep_stringify(task[:execution])
|
|
74
|
+
end
|
|
75
|
+
Record.create!(attributes)
|
|
70
76
|
end
|
|
71
77
|
|
|
72
78
|
deserialize(record)
|
|
@@ -76,12 +82,12 @@ module A2A
|
|
|
76
82
|
deserialize(find_record!(task_id, principal_id: principal_id))
|
|
77
83
|
end
|
|
78
84
|
|
|
79
|
-
def claim_execution(task_id, timestamp: Time.now.utc, principal_id: nil)
|
|
85
|
+
def claim_execution(task_id, timestamp: Time.now.utc, principal_id: nil, turn_id: nil)
|
|
80
86
|
timestamp = normalize_time(timestamp, field: "task status timestamp")
|
|
81
87
|
|
|
82
88
|
Record.transaction do
|
|
83
89
|
record = locked_record!(task_id, principal_id: principal_id)
|
|
84
|
-
if record.state == "submitted"
|
|
90
|
+
if record.state == "submitted" && turn_matches?(deserialize(record), turn_id)
|
|
85
91
|
record.update!(
|
|
86
92
|
state: "working",
|
|
87
93
|
status_timestamp: timestamp,
|
|
@@ -93,7 +99,7 @@ module A2A
|
|
|
93
99
|
end
|
|
94
100
|
|
|
95
101
|
def transition(task_id, state:, timestamp: Time.now.utc, artifacts: UNSET, message: UNSET,
|
|
96
|
-
principal_id: nil)
|
|
102
|
+
principal_id: nil, turn_id: nil)
|
|
97
103
|
state = normalized_state(state)
|
|
98
104
|
timestamp = normalize_time(timestamp, field: "task status timestamp")
|
|
99
105
|
validate_payload_collection!("artifacts", artifacts, @max_artifacts) unless artifacts.equal?(UNSET)
|
|
@@ -101,13 +107,23 @@ module A2A
|
|
|
101
107
|
Record.transaction do
|
|
102
108
|
record = locked_record!(task_id, principal_id: principal_id)
|
|
103
109
|
|
|
104
|
-
|
|
110
|
+
eligible = !terminal_record?(record) && turn_matches?(deserialize(record), turn_id)
|
|
111
|
+
eligible &&= %w[submitted working].include?(record.state) if turn_id
|
|
112
|
+
if eligible
|
|
105
113
|
attributes = {
|
|
106
114
|
state: state.to_s,
|
|
107
115
|
status_timestamp: timestamp,
|
|
108
116
|
status_message: message.equal?(UNSET) ? nil : deep_stringify(message)
|
|
109
117
|
}
|
|
110
118
|
attributes[:artifacts] = deep_stringify(artifacts) unless artifacts.equal?(UNSET)
|
|
119
|
+
if state == :input_required
|
|
120
|
+
if message.is_a?(Hash)
|
|
121
|
+
history = (record.history || []) + [deep_stringify(message)]
|
|
122
|
+
validate_payload_collection!("history", history, @max_history_entries)
|
|
123
|
+
attributes[:history] = history
|
|
124
|
+
end
|
|
125
|
+
attributes[:expires_at] = @input_required_retention && timestamp + @input_required_retention
|
|
126
|
+
end
|
|
111
127
|
if terminal_state?(state) && record.expires_at.nil?
|
|
112
128
|
attributes[:expires_at] = expiration_at(timestamp)
|
|
113
129
|
end
|
|
@@ -118,6 +134,35 @@ module A2A
|
|
|
118
134
|
end
|
|
119
135
|
end
|
|
120
136
|
|
|
137
|
+
def configure_execution(task_id, execution:, principal_id: nil)
|
|
138
|
+
require_execution_column!
|
|
139
|
+
Record.transaction do
|
|
140
|
+
record = locked_record!(task_id, principal_id: principal_id)
|
|
141
|
+
metadata = configured_execution(deserialize(record), execution)
|
|
142
|
+
record.update!(execution: deep_stringify(metadata))
|
|
143
|
+
deserialize(record)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def resume(task_id, message:, context_id: nil, turn_id:, timestamp: Time.now.utc, principal_id: nil)
|
|
148
|
+
timestamp = normalize_time(timestamp, field: "task status timestamp")
|
|
149
|
+
Record.transaction do
|
|
150
|
+
record = locked_record!(task_id, principal_id: principal_id)
|
|
151
|
+
task = deserialize(record)
|
|
152
|
+
validate_turn_id!(turn_id)
|
|
153
|
+
accepted = validate_resume!(task, message, context_id, @max_history_entries)
|
|
154
|
+
if accepted
|
|
155
|
+
metadata = task.fetch(:execution).merge(turn_id: turn_id, message_id: message.fetch(:message_id))
|
|
156
|
+
record.update!(
|
|
157
|
+
execution: deep_stringify(metadata),
|
|
158
|
+
history: (record.history || []) + [deep_stringify(message)],
|
|
159
|
+
state: "submitted", status_timestamp: timestamp, status_message: nil, expires_at: nil
|
|
160
|
+
)
|
|
161
|
+
end
|
|
162
|
+
{ task: deserialize(record), accepted: accepted }
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
121
166
|
def cancel(task_id, timestamp: Time.now.utc, principal_id: nil)
|
|
122
167
|
timestamp = normalize_time(timestamp, field: "task status timestamp")
|
|
123
168
|
|
|
@@ -130,7 +175,7 @@ module A2A
|
|
|
130
175
|
state: "canceled",
|
|
131
176
|
status_timestamp: timestamp,
|
|
132
177
|
status_message: nil,
|
|
133
|
-
expires_at:
|
|
178
|
+
expires_at: expiration_at(timestamp)
|
|
134
179
|
)
|
|
135
180
|
|
|
136
181
|
deserialize(record)
|
|
@@ -238,7 +283,7 @@ module A2A
|
|
|
238
283
|
|
|
239
284
|
return 0 if ids.empty?
|
|
240
285
|
|
|
241
|
-
Record.where(id: ids).delete_all
|
|
286
|
+
Record.where(id: ids).where.not(expires_at: nil).where("expires_at <= ?", at).delete_all
|
|
242
287
|
end
|
|
243
288
|
|
|
244
289
|
def maintenance_stats(at: now)
|
|
@@ -258,6 +303,13 @@ module A2A
|
|
|
258
303
|
|
|
259
304
|
private
|
|
260
305
|
|
|
306
|
+
def require_execution_column!
|
|
307
|
+
return if Record.column_names.include?("execution")
|
|
308
|
+
|
|
309
|
+
raise TaskContinuationUnavailableError,
|
|
310
|
+
"Run a2a:rails:task_continuation and db:migrate before using execution turns"
|
|
311
|
+
end
|
|
312
|
+
|
|
261
313
|
def find_record!(task_id, principal_id:)
|
|
262
314
|
record = Record.where(owner_id: principal_id, task_id: task_id).first
|
|
263
315
|
raise TaskNotFoundError, task_id unless record
|
|
@@ -412,12 +464,44 @@ module A2A
|
|
|
412
464
|
}
|
|
413
465
|
}
|
|
414
466
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
467
|
+
unless record.status_message.nil?
|
|
468
|
+
task[:status][:message] = deserialize_part_entries([record.status_message]).first
|
|
469
|
+
end
|
|
470
|
+
if record.has_attribute?(:execution) && record.execution
|
|
471
|
+
task[:execution] = deep_symbolize(record.execution)
|
|
472
|
+
end
|
|
473
|
+
task[:history] = deserialize_part_entries(record.history) unless record.history.nil?
|
|
474
|
+
task[:artifacts] = deserialize_part_entries(record.artifacts) unless record.artifacts.nil?
|
|
418
475
|
task
|
|
419
476
|
end
|
|
420
477
|
|
|
478
|
+
# The persistence envelope uses Ruby symbols; JSON inside the data
|
|
479
|
+
# field of a protocol Part is opaque. Do not treat a key named "data"
|
|
480
|
+
# inside metadata (or inside another arbitrary object) as a Part.
|
|
481
|
+
def deserialize_part_entries(entries)
|
|
482
|
+
return deep_symbolize(entries) unless entries.is_a?(Array)
|
|
483
|
+
|
|
484
|
+
entries.map do |entry|
|
|
485
|
+
next deep_symbolize(entry) unless entry.is_a?(Hash)
|
|
486
|
+
|
|
487
|
+
entry.each_with_object({}) do |(key, value), result|
|
|
488
|
+
result[key.to_sym] = if key == "parts" && value.is_a?(Array)
|
|
489
|
+
value.map { |part| deserialize_part(part) }
|
|
490
|
+
else
|
|
491
|
+
deep_symbolize(value)
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
end
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
def deserialize_part(part)
|
|
498
|
+
return deep_symbolize(part) unless part.is_a?(Hash)
|
|
499
|
+
|
|
500
|
+
part.each_with_object({}) do |(key, value), result|
|
|
501
|
+
result[key.to_sym] = key == "data" ? value : deep_symbolize(value)
|
|
502
|
+
end
|
|
503
|
+
end
|
|
504
|
+
|
|
421
505
|
def deep_stringify(value)
|
|
422
506
|
case value
|
|
423
507
|
when Hash
|
|
@@ -28,28 +28,49 @@ module A2A
|
|
|
28
28
|
history: [copy(message)]
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
yield task if block_given?
|
|
31
32
|
@store.save(task)
|
|
32
33
|
end
|
|
33
34
|
|
|
34
|
-
def start(task_id)
|
|
35
|
-
@store.transition(task_id, state: :working, timestamp: now, principal_id: @principal_id)
|
|
35
|
+
def start(task_id, turn_id: nil)
|
|
36
|
+
@store.transition(task_id, state: :working, timestamp: now, principal_id: @principal_id, turn_id: turn_id)
|
|
36
37
|
end
|
|
37
38
|
|
|
38
|
-
def claim_execution(task_id)
|
|
39
|
-
@store.claim_execution(task_id, timestamp: now, principal_id: @principal_id)
|
|
39
|
+
def claim_execution(task_id, turn_id: nil)
|
|
40
|
+
@store.claim_execution(task_id, timestamp: now, principal_id: @principal_id, turn_id: turn_id)
|
|
40
41
|
end
|
|
41
42
|
|
|
42
|
-
def complete(task_id, result)
|
|
43
|
-
|
|
43
|
+
def complete(task_id, result, turn_id: nil)
|
|
44
|
+
if result.is_a?(InputRequired)
|
|
45
|
+
question = { message_id: next_id, role: :agent, parts: result.parts }
|
|
46
|
+
transition_from_outcome(task_id, { state: :input_required, message: question }, turn_id: turn_id)
|
|
47
|
+
else
|
|
48
|
+
transition_from_outcome(task_id, @result_mapper.completed(result), turn_id: turn_id)
|
|
49
|
+
end
|
|
44
50
|
end
|
|
45
51
|
|
|
46
|
-
def reject(task_id, error)
|
|
47
|
-
transition_from_outcome(task_id, @result_mapper.rejected(error))
|
|
52
|
+
def reject(task_id, error, turn_id: nil)
|
|
53
|
+
transition_from_outcome(task_id, @result_mapper.rejected(error), turn_id: turn_id)
|
|
48
54
|
end
|
|
49
55
|
|
|
50
|
-
def fail(task_id, error)
|
|
56
|
+
def fail(task_id, error, turn_id: nil)
|
|
51
57
|
log_failure(task_id, error)
|
|
52
|
-
transition_from_outcome(task_id, @result_mapper.failed(error))
|
|
58
|
+
transition_from_outcome(task_id, @result_mapper.failed(error), turn_id: turn_id)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def configure_execution(task_id, plan:, message_id:)
|
|
62
|
+
@store.configure_execution(task_id, principal_id: @principal_id, execution: {
|
|
63
|
+
agent_class_name: plan.agent_class_name,
|
|
64
|
+
skill_id: plan.skill_id.to_s,
|
|
65
|
+
execution_mode: plan.execution_mode.to_s,
|
|
66
|
+
turn_id: next_id,
|
|
67
|
+
message_id: message_id
|
|
68
|
+
})
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def resume(task_id, message:, context_id: nil)
|
|
72
|
+
@store.resume(task_id, message: message, context_id: context_id,
|
|
73
|
+
turn_id: next_id, timestamp: now, principal_id: @principal_id)
|
|
53
74
|
end
|
|
54
75
|
|
|
55
76
|
def cancel(task_id)
|
|
@@ -66,8 +87,8 @@ module A2A
|
|
|
66
87
|
|
|
67
88
|
private
|
|
68
89
|
|
|
69
|
-
def transition_from_outcome(task_id, outcome)
|
|
70
|
-
attributes = { state: outcome.fetch(:state), timestamp: now }
|
|
90
|
+
def transition_from_outcome(task_id, outcome, turn_id: nil)
|
|
91
|
+
attributes = { state: outcome.fetch(:state), timestamp: now, turn_id: turn_id }
|
|
71
92
|
attributes[:artifacts] = outcome[:artifacts] if outcome.key?(:artifacts)
|
|
72
93
|
attributes[:message] = outcome[:message] if outcome.key?(:message)
|
|
73
94
|
@store.transition(task_id, **attributes, principal_id: @principal_id)
|
|
@@ -14,7 +14,11 @@ module A2A
|
|
|
14
14
|
MAX_PAGE_SIZE = 100
|
|
15
15
|
MAX_CACHED_PAGES = 128
|
|
16
16
|
|
|
17
|
-
def initialize
|
|
17
|
+
def initialize(max_history_entries: 100)
|
|
18
|
+
unless max_history_entries.is_a?(Integer) && max_history_entries.positive?
|
|
19
|
+
raise ArgumentError, "max_history_entries must be a positive integer"
|
|
20
|
+
end
|
|
21
|
+
@max_history_entries = max_history_entries
|
|
18
22
|
@tasks = {}
|
|
19
23
|
@pages = {}
|
|
20
24
|
@mutex = Mutex.new
|
|
@@ -22,6 +26,10 @@ module A2A
|
|
|
22
26
|
|
|
23
27
|
def save(task)
|
|
24
28
|
id = task.fetch(:id)
|
|
29
|
+
history = task[:history]
|
|
30
|
+
if history && (!history.is_a?(Array) || history.size > @max_history_entries)
|
|
31
|
+
raise TaskStorePayloadLimitError, "Task history exceeds configured limit"
|
|
32
|
+
end
|
|
25
33
|
@mutex.synchronize do
|
|
26
34
|
@tasks[id] = copy(task)
|
|
27
35
|
copy(@tasks[id])
|
|
@@ -32,10 +40,10 @@ module A2A
|
|
|
32
40
|
@mutex.synchronize { copy(find!(task_id, principal_id: principal_id)) }
|
|
33
41
|
end
|
|
34
42
|
|
|
35
|
-
def claim_execution(task_id, timestamp: Time.now.utc, principal_id: nil)
|
|
43
|
+
def claim_execution(task_id, timestamp: Time.now.utc, principal_id: nil, turn_id: nil)
|
|
36
44
|
@mutex.synchronize do
|
|
37
45
|
task = find!(task_id, principal_id: principal_id)
|
|
38
|
-
return nil unless task.dig(:status, :state) == :submitted
|
|
46
|
+
return nil unless turn_matches?(task, turn_id) && task.dig(:status, :state) == :submitted
|
|
39
47
|
|
|
40
48
|
task[:status] = status_hash(:working, timestamp, UNSET)
|
|
41
49
|
copy(task)
|
|
@@ -43,19 +51,47 @@ module A2A
|
|
|
43
51
|
end
|
|
44
52
|
|
|
45
53
|
def transition(task_id, state:, timestamp: Time.now.utc, artifacts: UNSET, message: UNSET,
|
|
46
|
-
principal_id: nil)
|
|
54
|
+
principal_id: nil, turn_id: nil)
|
|
47
55
|
validate_state!(state)
|
|
48
56
|
|
|
49
57
|
@mutex.synchronize do
|
|
50
58
|
task = find!(task_id, principal_id: principal_id)
|
|
51
|
-
return copy(task) if terminal?(task)
|
|
59
|
+
return copy(task) if terminal?(task) || !turn_matches?(task, turn_id)
|
|
60
|
+
return copy(task) if turn_id && !%i[submitted working].include?(task.dig(:status, :state))
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
if state == :input_required && message.is_a?(Hash)
|
|
63
|
+
history = task[:history] || []
|
|
64
|
+
raise TaskStorePayloadLimitError, "Task history limit reached" if history.length >= @max_history_entries
|
|
65
|
+
task[:history] = history + [copy(message)]
|
|
66
|
+
end
|
|
67
|
+
task[:status] = status_hash(state, timestamp, message.equal?(UNSET) ? UNSET : copy(message))
|
|
54
68
|
task[:artifacts] = copy(artifacts) unless artifacts.equal?(UNSET)
|
|
55
69
|
copy(task)
|
|
56
70
|
end
|
|
57
71
|
end
|
|
58
72
|
|
|
73
|
+
def configure_execution(task_id, execution:, principal_id: nil)
|
|
74
|
+
@mutex.synchronize do
|
|
75
|
+
task = find!(task_id, principal_id: principal_id)
|
|
76
|
+
task[:execution] = copy(configured_execution(task, execution))
|
|
77
|
+
copy(task)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def resume(task_id, message:, context_id: nil, turn_id:, timestamp: Time.now.utc, principal_id: nil)
|
|
82
|
+
@mutex.synchronize do
|
|
83
|
+
task = find!(task_id, principal_id: principal_id)
|
|
84
|
+
validate_turn_id!(turn_id)
|
|
85
|
+
accepted = validate_resume!(task, message, context_id, @max_history_entries)
|
|
86
|
+
if accepted
|
|
87
|
+
task[:history] = (task[:history] || []) + [copy(message)]
|
|
88
|
+
task[:execution] = task.fetch(:execution).merge(turn_id: turn_id, message_id: message.fetch(:message_id))
|
|
89
|
+
task[:status] = status_hash(:submitted, timestamp, UNSET)
|
|
90
|
+
end
|
|
91
|
+
{ task: copy(task), accepted: accepted }
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
59
95
|
def cancel(task_id, timestamp: Time.now.utc, principal_id: nil)
|
|
60
96
|
@mutex.synchronize do
|
|
61
97
|
task = find!(task_id, principal_id: principal_id)
|
data/lib/a2a/rails/task/store.rb
CHANGED
|
@@ -28,6 +28,14 @@ module A2A
|
|
|
28
28
|
raise NotImplementedError, "Task stores must implement #list"
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
def configure_execution(_task_id, execution:, principal_id: nil)
|
|
32
|
+
raise TaskContinuationUnavailableError, "Task store must implement #configure_execution for execution turns"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def resume(_task_id, message:, context_id: nil, turn_id:, principal_id: nil, **_attributes)
|
|
36
|
+
raise TaskContinuationUnavailableError, "Task store must implement atomic #resume"
|
|
37
|
+
end
|
|
38
|
+
|
|
31
39
|
def prune_expired(**_options)
|
|
32
40
|
raise NotImplementedError, "Task store does not implement maintenance pruning"
|
|
33
41
|
end
|
|
@@ -35,6 +43,75 @@ module A2A
|
|
|
35
43
|
def maintenance_stats(**_options)
|
|
36
44
|
raise NotImplementedError, "Task store does not implement maintenance stats"
|
|
37
45
|
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def turn_matches?(task, turn_id)
|
|
50
|
+
task.dig(:execution, :turn_id) == turn_id
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def validate_turn_id!(turn_id)
|
|
54
|
+
unless turn_id.is_a?(String) && !turn_id.empty?
|
|
55
|
+
raise TaskContinuationUnavailableError, "Execution turn_id must be a nonempty String"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def configured_execution(task, execution)
|
|
60
|
+
unless task.dig(:status, :state) == :submitted && task[:execution].nil?
|
|
61
|
+
raise TaskContinuationUnavailableError, "Execution can only be configured once on a submitted Task"
|
|
62
|
+
end
|
|
63
|
+
required = %i[skill_id execution_mode turn_id message_id]
|
|
64
|
+
unless execution.is_a?(Hash) && required.all? { |key| !execution[key].to_s.empty? }
|
|
65
|
+
raise TaskContinuationUnavailableError, "Execution metadata is incomplete"
|
|
66
|
+
end
|
|
67
|
+
validate_turn_id!(execution[:turn_id])
|
|
68
|
+
execution.merge(skill_id: execution[:skill_id].to_s, execution_mode: execution[:execution_mode].to_s)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Returns false for a previously accepted reply. The initial message is
|
|
72
|
+
# deliberately not a continuation receipt; new-Task dedupe is out of scope.
|
|
73
|
+
def validate_resume!(task, message, context_id, limit)
|
|
74
|
+
if !context_id.nil? && context_id != task.fetch(:context_id)
|
|
75
|
+
raise TaskContextMismatchError, "Message context does not match Task"
|
|
76
|
+
end
|
|
77
|
+
unless task[:execution].is_a?(Hash)
|
|
78
|
+
raise TaskContinuationUnavailableError, "Legacy Task has no continuation metadata"
|
|
79
|
+
end
|
|
80
|
+
unless message.is_a?(Hash) && !message[:message_id].to_s.empty?
|
|
81
|
+
raise InvalidRequestError, "Continuation message_id is required"
|
|
82
|
+
end
|
|
83
|
+
history = task[:history] || []
|
|
84
|
+
prior_index = history.index { |entry| entry[:message_id] == message[:message_id] }
|
|
85
|
+
if prior_index
|
|
86
|
+
prior = history.fetch(prior_index)
|
|
87
|
+
unless prior_index.positive? && prior[:role].to_s == "user" && canonical_message(prior) == canonical_message(message)
|
|
88
|
+
raise TaskMessageConflictError, "Message ID has already been used with a different request"
|
|
89
|
+
end
|
|
90
|
+
return false
|
|
91
|
+
end
|
|
92
|
+
unless task.dig(:status, :state) == :input_required
|
|
93
|
+
raise TaskNotResumableError.new(task.fetch(:id), state: task.dig(:status, :state))
|
|
94
|
+
end
|
|
95
|
+
# Reserve one slot for the handler's next question. Do not execute a
|
|
96
|
+
# reply whose question cannot be persisted without discarding input.
|
|
97
|
+
if history.length + 2 > limit
|
|
98
|
+
raise TaskStorePayloadLimitError, "Task history limit reached; cancel and start a new Task"
|
|
99
|
+
end
|
|
100
|
+
true
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def canonical_message(value)
|
|
104
|
+
case value
|
|
105
|
+
when Hash
|
|
106
|
+
value.to_h { |key, item| [key.to_s, canonical_message(item)] }
|
|
107
|
+
when Array
|
|
108
|
+
value.map { |item| canonical_message(item) }
|
|
109
|
+
when Symbol
|
|
110
|
+
value.to_s
|
|
111
|
+
else
|
|
112
|
+
value
|
|
113
|
+
end
|
|
114
|
+
end
|
|
38
115
|
end
|
|
39
116
|
end
|
|
40
117
|
end
|
data/lib/a2a/rails/task.rb
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module A2A
|
|
4
4
|
module Rails
|
|
5
5
|
module Task
|
|
6
|
-
STATES = %i[submitted working completed failed rejected canceled].freeze
|
|
6
|
+
STATES = %i[submitted working input_required completed failed rejected canceled].freeze
|
|
7
7
|
TERMINAL_STATES = %i[completed failed rejected canceled].freeze
|
|
8
8
|
end
|
|
9
9
|
end
|
|
@@ -7,19 +7,24 @@ module A2A
|
|
|
7
7
|
MAX_CLASS_NAME_BYTES = 512
|
|
8
8
|
KEYS = %i[task_id principal_id agent_class_name skill_id].freeze
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
OPTIONAL_KEYS = %i[turn_id].freeze
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
attr_reader(*(KEYS + OPTIONAL_KEYS))
|
|
13
|
+
|
|
14
|
+
def initialize(task_id:, principal_id:, agent_class_name:, skill_id:, turn_id: nil)
|
|
13
15
|
@task_id = normalize_identifier(task_id, "task_id")
|
|
14
16
|
@principal_id = normalize_principal_id(principal_id)
|
|
15
17
|
@agent_class_name = normalize_agent_class_name(agent_class_name)
|
|
16
18
|
@skill_id = normalize_skill_id(skill_id)
|
|
19
|
+
@turn_id = turn_id.nil? ? nil : normalize_identifier(turn_id, "turn_id")
|
|
17
20
|
@payload = {
|
|
18
21
|
task_id: @task_id,
|
|
19
22
|
principal_id: @principal_id,
|
|
20
23
|
agent_class_name: @agent_class_name,
|
|
21
24
|
skill_id: @skill_id
|
|
22
|
-
}
|
|
25
|
+
}
|
|
26
|
+
@payload[:turn_id] = @turn_id unless @turn_id.nil?
|
|
27
|
+
@payload.freeze
|
|
23
28
|
freeze
|
|
24
29
|
end
|
|
25
30
|
|
|
@@ -10,17 +10,19 @@ module A2A
|
|
|
10
10
|
# immediate and independent from any surrounding host Active Record transaction.
|
|
11
11
|
self.enqueue_after_transaction_commit = false
|
|
12
12
|
|
|
13
|
-
def perform(task_id:, principal_id:, agent_class_name:, skill_id:)
|
|
13
|
+
def perform(task_id:, principal_id:, agent_class_name:, skill_id:, turn_id: nil)
|
|
14
14
|
arguments = TaskExecutionArguments.new(
|
|
15
15
|
task_id: task_id,
|
|
16
16
|
principal_id: principal_id,
|
|
17
17
|
agent_class_name: agent_class_name,
|
|
18
|
-
skill_id: skill_id
|
|
18
|
+
skill_id: skill_id,
|
|
19
|
+
turn_id: turn_id
|
|
19
20
|
)
|
|
20
21
|
task_id = arguments.task_id
|
|
21
22
|
principal_id = arguments.principal_id
|
|
22
23
|
agent_class_name = arguments.agent_class_name
|
|
23
24
|
skill_id = arguments.skill_id
|
|
25
|
+
turn_id = arguments.turn_id
|
|
24
26
|
|
|
25
27
|
configuration = A2A::Rails.configuration
|
|
26
28
|
lifecycle = Task::Lifecycle.new(
|
|
@@ -29,7 +31,7 @@ module A2A
|
|
|
29
31
|
principal_id: principal_id
|
|
30
32
|
)
|
|
31
33
|
|
|
32
|
-
claimed = lifecycle.claim_execution(task_id)
|
|
34
|
+
claimed = lifecycle.claim_execution(task_id, turn_id: turn_id)
|
|
33
35
|
return unless claimed
|
|
34
36
|
|
|
35
37
|
agent = configuration.resolve_agent
|
|
@@ -37,7 +39,9 @@ module A2A
|
|
|
37
39
|
raise ConfigurationError, "Queued Agent no longer matches configured Agent"
|
|
38
40
|
end
|
|
39
41
|
|
|
40
|
-
|
|
42
|
+
verify_execution!(claimed, agent_class_name: agent_class_name, skill_id: skill_id,
|
|
43
|
+
turn_id: turn_id)
|
|
44
|
+
message = execution_message(claimed)
|
|
41
45
|
plan = ExecutionPlan.new(
|
|
42
46
|
agent_class_name: agent_class_name,
|
|
43
47
|
skill_id: skill_id,
|
|
@@ -47,7 +51,9 @@ module A2A
|
|
|
47
51
|
task_id: task_id,
|
|
48
52
|
context_id: claimed.fetch(:context_id),
|
|
49
53
|
principal_id: principal_id,
|
|
50
|
-
idempotency_key: task_id
|
|
54
|
+
idempotency_key: task_id,
|
|
55
|
+
execution_turn_id: turn_id,
|
|
56
|
+
history: copy(claimed.fetch(:history))
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
result = Dispatcher.new(agent: agent, configuration: configuration).execute(
|
|
@@ -55,24 +61,53 @@ module A2A
|
|
|
55
61
|
message: message,
|
|
56
62
|
context: context
|
|
57
63
|
)
|
|
58
|
-
lifecycle.complete(task_id, result)
|
|
64
|
+
lifecycle.complete(task_id, result, turn_id: turn_id)
|
|
59
65
|
rescue RejectedTask => error
|
|
60
|
-
lifecycle
|
|
66
|
+
lifecycle.reject(task_id, error, turn_id: turn_id) if claimed
|
|
61
67
|
rescue TaskNotFoundError
|
|
62
68
|
nil
|
|
63
69
|
rescue StandardError => error
|
|
64
|
-
lifecycle
|
|
70
|
+
lifecycle.fail(task_id, error, turn_id: turn_id) if claimed
|
|
65
71
|
end
|
|
66
72
|
|
|
67
73
|
private
|
|
68
74
|
|
|
69
|
-
def
|
|
75
|
+
def verify_execution!(task, agent_class_name:, skill_id:, turn_id:)
|
|
76
|
+
execution = task[:execution]
|
|
77
|
+
return if execution.nil? && turn_id.nil?
|
|
78
|
+
|
|
79
|
+
unless execution.is_a?(Hash) && execution[:turn_id] == turn_id &&
|
|
80
|
+
execution[:agent_class_name] == agent_class_name &&
|
|
81
|
+
execution[:skill_id].to_s == skill_id && execution[:execution_mode].to_s == "async"
|
|
82
|
+
raise ConfigurationError, "Queued execution does not match saved Task execution"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def execution_message(task)
|
|
70
87
|
history = task[:history]
|
|
71
|
-
unless history.is_a?(Array)
|
|
72
|
-
raise ConfigurationError, "Queued Task does not contain
|
|
88
|
+
unless history.is_a?(Array)
|
|
89
|
+
raise ConfigurationError, "Queued Task does not contain Message history"
|
|
73
90
|
end
|
|
74
91
|
|
|
75
|
-
|
|
92
|
+
execution = task[:execution]
|
|
93
|
+
message = if execution
|
|
94
|
+
history.find do |entry|
|
|
95
|
+
entry.is_a?(Hash) && entry[:role].to_s == "user" &&
|
|
96
|
+
entry[:message_id] == execution.fetch(:message_id)
|
|
97
|
+
end
|
|
98
|
+
else
|
|
99
|
+
# Jobs serialized by 0.4.0 remain executable only for legacy Tasks.
|
|
100
|
+
history.first
|
|
101
|
+
end
|
|
102
|
+
unless message.is_a?(Hash)
|
|
103
|
+
raise ConfigurationError, "Queued Task does not contain its execution Message"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
copy(message)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def copy(value)
|
|
110
|
+
Marshal.load(Marshal.dump(value))
|
|
76
111
|
end
|
|
77
112
|
end
|
|
78
113
|
end
|
data/lib/a2a/rails/version.rb
CHANGED
data/lib/a2a-rails.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require_relative "a2a/rails/version"
|
|
4
4
|
require_relative "a2a/rails/errors"
|
|
5
5
|
require_relative "a2a/rails/file_artifact"
|
|
6
|
+
require_relative "a2a/rails/input_required"
|
|
6
7
|
require_relative "a2a/rails/authentication"
|
|
7
8
|
require_relative "a2a/rails/request_guard"
|
|
8
9
|
require_relative "a2a/rails/configuration"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "task_store_generator"
|
|
4
|
+
|
|
5
|
+
module A2A
|
|
6
|
+
module Rails
|
|
7
|
+
class TaskContinuationGenerator < ::Rails::Generators::Base
|
|
8
|
+
include ::Rails::Generators::Migration
|
|
9
|
+
|
|
10
|
+
namespace "a2a:rails:task_continuation"
|
|
11
|
+
source_root File.expand_path("templates", __dir__)
|
|
12
|
+
|
|
13
|
+
def self.next_migration_number(dirname)
|
|
14
|
+
::ActiveRecord::Generators::Base.next_migration_number(dirname)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def create_continuation_migration
|
|
18
|
+
migration_template(
|
|
19
|
+
"add_execution_to_a2a_rails_tasks.rb.tt",
|
|
20
|
+
"db/migrate/add_execution_to_a2a_rails_tasks.rb"
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|