phronomy 0.37.0 → 0.38.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 +87 -0
- data/README.md +24 -2
- data/SECURITY.md +1 -1
- data/docs/architecture/agent-approval-recovery.md +320 -0
- data/docs/architecture/agent-child-approval-recovery.md +387 -0
- data/docs/architecture/agent-child-execution-recovery.md +425 -0
- data/docs/architecture/agent-dispatch-recovery.md +308 -0
- data/docs/architecture/agent-terminal-persistence-evidence.md +360 -0
- data/docs/architecture/agent-terminal-recovery.md +298 -0
- data/docs/architecture/agent-terminal-submission-recovery.md +275 -0
- data/docs/architecture/agent-tool-child-lifetime.md +29 -0
- data/docs/architecture/agent-transition-ownership.md +24 -0
- data/docs/architecture/backend-groups.md +6 -0
- data/docs/architecture/detached-outcome-proof-projection.md +576 -0
- data/docs/architecture/execution-abstraction-review.md +5 -0
- data/docs/architecture/handoff-dispatch-recovery.md +205 -0
- data/docs/architecture/handoff-initial-terminal-application.md +321 -0
- data/docs/architecture/initial-preparation-recovery.md +308 -0
- data/docs/architecture/multi-agent-handoff.md +17 -0
- data/docs/architecture/native-outcome-preparation.md +715 -0
- data/docs/architecture/native-parent-dispatch-recovery.md +460 -0
- data/docs/architecture/native-parent-outcome-recovery.md +644 -0
- data/docs/architecture/parent-child-terminal-acknowledgement.md +297 -0
- data/docs/architecture/r8-unit12.md +6 -3
- data/docs/architecture/r8-unit4.md +11 -2
- data/docs/architecture/recovery-physical-attempt.md +88 -0
- data/docs/architecture/recovery-submission-custody.md +247 -0
- data/docs/architecture/retained-attempts.md +229 -0
- data/docs/architecture/retained-outcome-materialization.md +636 -0
- data/docs/architecture/retained-outcome-publication.md +506 -0
- data/docs/architecture/storage-execution-boundaries.md +1 -1
- data/docs/architecture/structured-output-and-scope.md +15 -0
- data/docs/architecture/synchronous-participant-application.md +397 -0
- data/docs/architecture/synchronous-participant-contract.md +195 -0
- data/docs/architecture/team-admission-rejection.md +91 -0
- data/docs/architecture/team-cancellation-delivery.md +245 -0
- data/docs/architecture/team-child-retirement.md +104 -0
- data/docs/architecture/tracing.md +26 -0
- data/docs/architecture/workflow-terminal-ownership-design.md +25 -9
- data/docs/architecture/workflow-terminal-reconciliation.md +221 -0
- data/docs/architecture.md +16 -9
- data/docs/decisions/025-process-local-agent-ownership-and-runtime-admission.md +8 -0
- data/docs/features.md +3 -3
- data/docs/mcp-client.md +138 -5
- data/docs/migrations/orchestrator-subagent-dispatch.md +89 -0
- data/docs/migrations/vector-async-clients.md +18 -3
- data/docs/persistence-backends.md +30 -4
- data/docs/runtime-and-concurrency.md +235 -11
- data/lib/phronomy/agent/admission.rb +37 -17
- data/lib/phronomy/agent/api/agent.rb +25 -0
- data/lib/phronomy/agent/approval_recovery.rb +30 -0
- data/lib/phronomy/agent/approval_recovery_api.rb +61 -0
- data/lib/phronomy/agent/approval_recovery_error.rb +39 -0
- data/lib/phronomy/agent/approval_recovery_result.rb +94 -0
- data/lib/phronomy/agent/async_event_api.rb +22 -5
- data/lib/phronomy/agent/base.rb +76 -48
- data/lib/phronomy/agent/context_preparation.rb +10 -8
- data/lib/phronomy/agent/dispatch_recovery.rb +29 -0
- data/lib/phronomy/agent/dispatch_recovery_api.rb +84 -0
- data/lib/phronomy/agent/dispatch_recovery_error.rb +36 -0
- data/lib/phronomy/agent/dispatch_recovery_result.rb +93 -0
- data/lib/phronomy/agent/execution/agent_invocation.rb +11 -3
- data/lib/phronomy/agent/execution/approval_continuation_plan.rb +131 -0
- data/lib/phronomy/agent/execution/approval_parent_evidence.rb +116 -0
- data/lib/phronomy/agent/execution/approval_persistence_evidence.rb +111 -0
- data/lib/phronomy/agent/execution/approval_recovery_operation.rb +496 -0
- data/lib/phronomy/agent/execution/approval_resume_commit.rb +174 -10
- data/lib/phronomy/agent/execution/dispatch_continuation_delivery.rb +96 -0
- data/lib/phronomy/agent/execution/dispatch_persistence_evidence.rb +147 -0
- data/lib/phronomy/agent/execution/dispatch_preparation.rb +63 -7
- data/lib/phronomy/agent/execution/dispatch_recovery_operation.rb +495 -0
- data/lib/phronomy/agent/execution/execution_coordinator.rb +724 -158
- data/lib/phronomy/agent/execution/execution_outcome_committer.rb +312 -39
- data/lib/phronomy/agent/execution/execution_registry.rb +508 -13
- data/lib/phronomy/agent/execution/initial_persistence_evidence.rb +269 -0
- data/lib/phronomy/agent/execution/initial_preparation.rb +132 -40
- data/lib/phronomy/agent/execution/initial_preparation_recovery_operation.rb +513 -0
- data/lib/phronomy/agent/execution/invocation_transitions.rb +3 -0
- data/lib/phronomy/agent/execution/outcome_planning.rb +108 -0
- data/lib/phronomy/agent/execution/outcome_preparation.rb +81 -0
- data/lib/phronomy/agent/execution/outcome_recovery_operation.rb +609 -0
- data/lib/phronomy/agent/execution/outcome_text_projection.rb +73 -0
- data/lib/phronomy/agent/execution/outcome_write_plan.rb +48 -0
- data/lib/phronomy/agent/execution/runtime_record_encoder.rb +7 -4
- data/lib/phronomy/agent/execution/terminal_persistence_evidence.rb +280 -0
- data/lib/phronomy/agent/execution/terminal_recovery_operation.rb +38 -0
- data/lib/phronomy/agent/execution_change.rb +138 -26
- data/lib/phronomy/agent/execution_environment.rb +29 -0
- data/lib/phronomy/agent/execution_rejected_error.rb +34 -0
- data/lib/phronomy/agent/initial_preparation_recovery.rb +32 -0
- data/lib/phronomy/agent/initial_preparation_recovery_api.rb +85 -0
- data/lib/phronomy/agent/initial_preparation_recovery_error.rb +41 -0
- data/lib/phronomy/agent/initial_preparation_recovery_result.rb +115 -0
- data/lib/phronomy/agent/lifecycle/hosted_approval_context.rb +86 -0
- data/lib/phronomy/agent/lifecycle/hosted_invocation.rb +42 -10
- data/lib/phronomy/agent/lifecycle/identity_write.rb +101 -0
- data/lib/phronomy/agent/lifecycle/lifetime_api.rb +28 -0
- data/lib/phronomy/agent/lifecycle/ownership_registry.rb +136 -70
- data/lib/phronomy/agent/lifecycle/scope.rb +3 -3
- data/lib/phronomy/agent/outcome_recovery.rb +47 -0
- data/lib/phronomy/agent/outcome_recovery_api.rb +114 -0
- data/lib/phronomy/agent/outcome_recovery_error.rb +36 -0
- data/lib/phronomy/agent/outcome_recovery_result.rb +92 -0
- data/lib/phronomy/agent/output_contract.rb +16 -6
- data/lib/phronomy/agent/persistence/identity_receipt_repository.rb +61 -0
- data/lib/phronomy/agent/persistence/records.rb +2 -0
- data/lib/phronomy/agent/persistence/storage_schema.rb +2 -0
- data/lib/phronomy/agent/recovery/invocation_restorer.rb +8 -5
- data/lib/phronomy/agent/recovery/recovery_coordinator/continuation.rb +6 -4
- data/lib/phronomy/agent/recovery/recovery_coordinator/installation.rb +21 -43
- data/lib/phronomy/agent/recovery/recovery_coordinator.rb +19 -6
- data/lib/phronomy/agent/runtime_binding/engine_environment.rb +12 -0
- data/lib/phronomy/agent/state_writer.rb +4 -3
- data/lib/phronomy/agent/store.rb +7 -1
- data/lib/phronomy/agent/terminal_recovery.rb +41 -0
- data/lib/phronomy/agent/terminal_recovery_api.rb +101 -0
- data/lib/phronomy/agent/terminal_recovery_error.rb +36 -0
- data/lib/phronomy/agent/terminal_recovery_result.rb +67 -0
- data/lib/phronomy/agent/tool_execution/tool_approval_request.rb +25 -1
- data/lib/phronomy/common/values/serializable.rb +37 -4
- data/lib/phronomy/engine/concurrency/offload_pool.rb +32 -4
- data/lib/phronomy/engine/concurrency/pool_registry.rb +6 -0
- data/lib/phronomy/engine/event_loop.rb +161 -11
- data/lib/phronomy/engine/execution_binding.rb +4 -2
- data/lib/phronomy/engine/execution_receiver_binding.rb +5 -0
- data/lib/phronomy/engine/runtime/shutdown_result.rb +21 -0
- data/lib/phronomy/engine/runtime/timer_queue.rb +7 -2
- data/lib/phronomy/engine/runtime.rb +119 -15
- data/lib/phronomy/execution/attempt_completion.rb +91 -0
- data/lib/phronomy/execution/concurrency/cancellation_token.rb +7 -2
- data/lib/phronomy/execution/concurrency/physical_completion_task.rb +7 -2
- data/lib/phronomy/execution/execution.rb +17 -2
- data/lib/phronomy/execution/execution_receiver.rb +27 -0
- data/lib/phronomy/execution/execution_retirement.rb +30 -0
- data/lib/phronomy/execution/invocation_controls.rb +2 -2
- data/lib/phronomy/execution/retained_attempts.rb +179 -0
- data/lib/phronomy/execution/stopped_terminal_custody.rb +99 -0
- data/lib/phronomy/llm_adapter/response.rb +7 -1
- data/lib/phronomy/multi_agent/admission_registry.rb +6 -0
- data/lib/phronomy/multi_agent/durable_subagent_coordinator.rb +175 -39
- data/lib/phronomy/multi_agent/handoff_dispatch_evidence.rb +176 -0
- data/lib/phronomy/multi_agent/handoff_finalization_error.rb +33 -0
- data/lib/phronomy/multi_agent/handoff_history_release.rb +80 -0
- data/lib/phronomy/multi_agent/handoff_participant.rb +31 -6
- data/lib/phronomy/multi_agent/handoff_runner.rb +195 -21
- data/lib/phronomy/multi_agent/handoff_terminal_finalization.rb +176 -0
- data/lib/phronomy/multi_agent/native_approval_evidence.rb +26 -0
- data/lib/phronomy/multi_agent/native_child_authority.rb +135 -0
- data/lib/phronomy/multi_agent/native_dispatch_evidence.rb +43 -0
- data/lib/phronomy/multi_agent/native_outcome_evidence.rb +46 -0
- data/lib/phronomy/multi_agent/native_persistence_plan.rb +93 -0
- data/lib/phronomy/multi_agent/orchestrator.rb +28 -52
- data/lib/phronomy/multi_agent/reserved_child_admission.rb +90 -1
- data/lib/phronomy/multi_agent/store.rb +5 -34
- data/lib/phronomy/multi_agent/subagent_history_release.rb +88 -0
- data/lib/phronomy/multi_agent/team_cancellation_delivery_error.rb +21 -0
- data/lib/phronomy/multi_agent/team_cancellation_request.rb +134 -0
- data/lib/phronomy/multi_agent/team_coordinator.rb +174 -27
- data/lib/phronomy/persistence_composition/stores.rb +10 -3
- data/lib/phronomy/runtime_composition/execution_defaults.rb +18 -0
- data/lib/phronomy/storage/backends/in_memory.rb +3 -1
- data/lib/phronomy/storage/resource.rb +10 -1
- data/lib/phronomy/tools/agent.rb +7 -1
- data/lib/phronomy/tools/mcp.rb +210 -91
- data/lib/phronomy/tools/mcp_http_close_response.rb +23 -0
- data/lib/phronomy/tools/mcp_transport_registry.rb +210 -0
- data/lib/phronomy/tools/vector_search.rb +28 -5
- data/lib/phronomy/tracing/automatic.rb +8 -1
- data/lib/phronomy/version.rb +1 -1
- data/lib/phronomy/workflow/execution/workflow.rb +34 -0
- data/lib/phronomy/workflow/execution/workflow_execution_environment.rb +1 -0
- data/lib/phronomy/workflow/execution/workflow_execution_registry.rb +106 -7
- data/lib/phronomy/workflow/execution/workflow_runner.rb +53 -108
- data/lib/phronomy/workflow/execution/workflow_terminal_operation.rb +489 -0
- data/lib/phronomy/workflow/execution/workflow_terminal_persistence.rb +81 -0
- data/lib/phronomy/workflow/execution/workflow_terminal_recovery.rb +38 -0
- data/lib/phronomy/workflow/execution/workflow_terminal_recovery_error.rb +34 -0
- data/lib/phronomy/workflow/execution/workflow_terminal_recovery_result.rb +56 -0
- data/lib/phronomy/workflow/runtime_binding/workflow_engine_environment.rb +10 -1
- data/lib/phronomy/workflow/runtime_binding/workflow_task_session.rb +3 -1
- data/lib/phronomy/workflow/runtime_binding/workflow_terminal_policy.rb +2 -1
- data/scripts/api_snapshot.rb +46 -1
- data/sig/_private/phronomy/multi_agent/team_cancellation_request.rbs +19 -0
- data/sig/phronomy/agent/approval_recovery.rbs +54 -0
- data/sig/phronomy/agent/dispatch_recovery.rbs +49 -0
- data/sig/phronomy/agent/execution_contract.rbs +4 -1
- data/sig/phronomy/agent/execution_environment.rbs +2 -0
- data/sig/phronomy/agent/initial_preparation_recovery.rbs +48 -0
- data/sig/phronomy/agent/outcome_recovery.rbs +51 -0
- data/sig/phronomy/agent/store.rbs +1 -0
- data/sig/phronomy/agent/terminal_persistence_evidence.rbs +111 -0
- data/sig/phronomy/agent/terminal_recovery.rbs +46 -0
- data/sig/phronomy/agent.rbs +36 -0
- data/sig/phronomy/engine/concurrency/timer_bindings.rbs +2 -0
- data/sig/phronomy/engine/execution_binding.rbs +2 -1
- data/sig/phronomy/engine/execution_receiver.rbs +4 -0
- data/sig/phronomy/engine/observability.rbs +22 -1
- data/sig/phronomy/execution/execution.rbs +16 -1
- data/sig/phronomy/execution/execution_receiver.rbs +12 -0
- data/sig/phronomy/execution/execution_retirement.rbs +18 -0
- data/sig/phronomy/execution/stopped_terminal_custody.rbs +19 -0
- data/sig/phronomy/handoff.rbs +1 -0
- data/sig/phronomy/multi_agent/handoff_finalization_error.rbs +18 -0
- data/sig/phronomy/multi_agent/store.rbs +1 -1
- data/sig/phronomy/multi_agent/team_cancellation_delivery_error.rbs +9 -0
- data/sig/phronomy/persistence_composition.rbs +2 -1
- data/sig/phronomy/storage/contracts.rbs +2 -1
- data/sig/phronomy/tools/mcp.rbs +27 -0
- data/sig/phronomy/workflow.rbs +42 -0
- metadata +104 -2
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phronomy
|
|
4
|
+
module MultiAgent
|
|
5
|
+
# Completes one already-terminal pending Target's parent routing obligation.
|
|
6
|
+
# Agent supplies local custody facts; this domain owns routing and retentions.
|
|
7
|
+
# A fresh instance represents each explicit operation and makes at most one
|
|
8
|
+
# synchronous save. Instances are not shared across concurrent callers.
|
|
9
|
+
# @api private
|
|
10
|
+
class HandoffTerminalFinalization
|
|
11
|
+
STATUSES = %i[failed cancelled blocked].freeze
|
|
12
|
+
Snapshot = Data.define(:routing, :source, :target, :proof)
|
|
13
|
+
private_constant :STATUSES, :Snapshot
|
|
14
|
+
|
|
15
|
+
def initialize(persistence:, main_agent_id:, agents:)
|
|
16
|
+
@persistence, @main_agent_id, @agents = persistence, main_agent_id, agents
|
|
17
|
+
@agent_store = persistence.agent_store
|
|
18
|
+
@store_identity = @agent_store.execution_evidence_identity
|
|
19
|
+
@owner = {"kind" => "handoff", "main_agent_id" => main_agent_id}.freeze
|
|
20
|
+
@owner_key = "handoff:#{main_agent_id}".freeze
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def call(execution_id:, target:)
|
|
24
|
+
@before = @after = @intended = nil
|
|
25
|
+
return target unless STATUSES.include?(target.status)
|
|
26
|
+
@execution_id, @target = execution_id, target
|
|
27
|
+
snapshot = capture_pending
|
|
28
|
+
return target unless snapshot
|
|
29
|
+
assert_released_custody!(snapshot)
|
|
30
|
+
commit(snapshot)
|
|
31
|
+
target
|
|
32
|
+
rescue HandoffFinalizationError
|
|
33
|
+
raise
|
|
34
|
+
rescue => error
|
|
35
|
+
raise diagnostic(error), cause: error
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def capture_pending
|
|
41
|
+
@persistence.transaction do |records, scope|
|
|
42
|
+
routing = records.handoff_states.load_locked(@main_agent_id)
|
|
43
|
+
next unless routing && routing.phase != "stable" && routing.pending_target_execution_id == @target.execution_id
|
|
44
|
+
unless scope.outermost?
|
|
45
|
+
raise Phronomy::Persistence::TransactionError, "Handoff finalization requires an outermost transaction before observing Agent custody"
|
|
46
|
+
end
|
|
47
|
+
unless routing.phase == "target_pending"
|
|
48
|
+
raise Phronomy::Persistence::StateConflictError, "Handoff finalization requires a pending Target transition"
|
|
49
|
+
end
|
|
50
|
+
capture_in(routing, scope)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Never wait for EventLoop while retaining routing or Agent Store guards.
|
|
55
|
+
def assert_released_custody!(snapshot)
|
|
56
|
+
[snapshot.source, snapshot.target].each do |execution|
|
|
57
|
+
agent = @agents[execution.agent_id]
|
|
58
|
+
unless agent && agent.persistence.equal?(@agent_store)
|
|
59
|
+
raise Phronomy::ExecutionRehydrationRequiredError, "Current Handoff graph lacks the exact Source or Target Agent"
|
|
60
|
+
end
|
|
61
|
+
if agent.send(:__execution_custody_pending?, execution.execution_id)
|
|
62
|
+
raise Phronomy::ExecutionRehydrationRequiredError,
|
|
63
|
+
"Agent #{execution.agent_id.inspect} execution #{execution.execution_id.inspect} retains local custody; " \
|
|
64
|
+
"resolve its original recovery before Handoff finalization"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def commit(snapshot)
|
|
70
|
+
@persistence.transaction do |records, scope|
|
|
71
|
+
routing = records.handoff_states.load_locked(@main_agent_id)
|
|
72
|
+
current = capture_in(routing, scope)
|
|
73
|
+
next if already_finalized?(snapshot, current)
|
|
74
|
+
unless current.proof == snapshot.proof
|
|
75
|
+
raise Phronomy::Persistence::StateConflictError, "Handoff finalization authority changed after custody observation"
|
|
76
|
+
end
|
|
77
|
+
@before = current.proof
|
|
78
|
+
@intended = routing.with(phase: "stable")
|
|
79
|
+
@after = Phronomy::Values::Immutable.copy(@before.merge(routing: @intended.to_h))
|
|
80
|
+
save_routing(records, routing)
|
|
81
|
+
end
|
|
82
|
+
rescue => error
|
|
83
|
+
raise unless @intended
|
|
84
|
+
proof = reconcile_save(error)
|
|
85
|
+
raise diagnostic(error, disposition: proof.disposition, read_error: proof.read_error), cause: error unless proof.disposition == :committed
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def save_routing(records, routing)
|
|
89
|
+
records.handoff_states.save(@main_agent_id, expected_revision: routing.handoff_revision, state: @intended)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def reconcile_save(error)
|
|
93
|
+
Phronomy::Persistence::SaveOutcome.compare(before: @before, after: @after, original_error: error) do
|
|
94
|
+
@persistence.transaction do |records, scope|
|
|
95
|
+
capture_in(records.handoff_states.load_locked(@main_agent_id), scope).proof
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def already_finalized?(before, current)
|
|
101
|
+
old_route, route = before.routing, current.routing
|
|
102
|
+
route.phase == "stable" && route.handoff_revision == old_route.handoff_revision + 1 &&
|
|
103
|
+
route.to_h.except("phase", "handoff_revision", "updated_at") == old_route.to_h.except("phase", "handoff_revision", "updated_at") &&
|
|
104
|
+
current.proof.except(:routing) == before.proof.except(:routing)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def capture_in(routing, scope)
|
|
108
|
+
assert_store!
|
|
109
|
+
unless routing && routing.active_agent_id == @target.agent_id && routing.pending_target_execution_id == @target.execution_id
|
|
110
|
+
raise Phronomy::Persistence::StateConflictError, "Handoff no longer owns this pending Target"
|
|
111
|
+
end
|
|
112
|
+
source_agent_id = routing.metadata.fetch("source_agent_id")
|
|
113
|
+
ids = [@main_agent_id, source_agent_id, @target.agent_id].uniq.sort
|
|
114
|
+
@agent_store.guard_agents(scope, agent_ids: ids)
|
|
115
|
+
source = @agent_store.observe_execution(agent_id: source_agent_id,
|
|
116
|
+
execution_id: routing.pending_source_execution_id, scope: scope)
|
|
117
|
+
target = @agent_store.observe_execution(agent_id: @target.agent_id, execution_id: @target.execution_id, scope: scope)
|
|
118
|
+
assert_executions!(routing, source, target, scope)
|
|
119
|
+
holds = ids.to_h { |id|
|
|
120
|
+
[id, @agent_store.retained_references(agent_id: id, scope: scope).select { |hold| hold.owner_key == @owner_key }.map(&:to_h)
|
|
121
|
+
.sort_by { |hold| Phronomy::CanonicalJSON.dump(hold) }]
|
|
122
|
+
}
|
|
123
|
+
assert_retention!(holds, source, target)
|
|
124
|
+
proof = Phronomy::Values::Immutable.copy(routing: routing.to_h,
|
|
125
|
+
source: execution_proof(source), target: execution_proof(target), holds: holds)
|
|
126
|
+
Snapshot.new(routing: routing, source: source, target: target, proof: proof)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def assert_executions!(routing, source, target, scope)
|
|
130
|
+
receipt = {"target_agent_id" => target.agent_id, "target_execution_id" => target.execution_id,
|
|
131
|
+
"context_ref" => routing.active_handoff_context_ref, "owner_key" => @owner_key}
|
|
132
|
+
unless source.status == :handed_off && source.reservation&.correlation == @owner && source.transfer_receipt == receipt &&
|
|
133
|
+
target.reservation&.correlation == @owner && STATUSES.include?(target.status) && execution_proof(target) == execution_proof(@target)
|
|
134
|
+
raise Phronomy::Persistence::StateConflictError, "Handoff terminal Target or Source transfer identity differs"
|
|
135
|
+
end
|
|
136
|
+
unless @agent_store.active_executions(agent_id: target.agent_id, scope: scope).empty?
|
|
137
|
+
raise Phronomy::AgentBusyError, "Handoff Target has another active execution"
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def assert_retention!(holds, source, target)
|
|
142
|
+
[[@main_agent_id, nil], [source.agent_id, source.execution_id], [target.agent_id, target.execution_id]].each do |id, execution_id|
|
|
143
|
+
unless holds.fetch(id).any? { |hold| hold.fetch("owner_key") == @owner_key && (!execution_id || hold["execution_id"] == execution_id) }
|
|
144
|
+
raise Phronomy::Persistence::StateConflictError, "Handoff required Source, Target or anchor retention is absent"
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def execution_proof(value)
|
|
150
|
+
value.to_result.merge(reservation: value.reservation&.to_h,
|
|
151
|
+
transfer_receipt: value.transfer_receipt, cancellation_requested: value.cancellation_requested)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def assert_store!
|
|
155
|
+
unless @agent_store.execution_evidence_identity.equal?(@store_identity) &&
|
|
156
|
+
@persistence.agent_store.equal?(@agent_store) && @persistence.coordinator.equal?(@agent_store.coordinator)
|
|
157
|
+
raise Phronomy::Persistence::StateConflictError, "Handoff finalization belongs to another Store"
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def diagnostic(error, disposition: :not_attempted, read_error: nil)
|
|
162
|
+
HandoffFinalizationError.new(main_agent_id: @main_agent_id, execution_id: @execution_id,
|
|
163
|
+
execution_result: @target.to_result, execution_error: saved_execution_error, finalization_error: error,
|
|
164
|
+
disposition: disposition, read_error: read_error)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Ask Agent's public semantic value to restore its own error variants.
|
|
168
|
+
def saved_execution_error
|
|
169
|
+
@target.value!
|
|
170
|
+
nil
|
|
171
|
+
rescue => error
|
|
172
|
+
error
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phronomy
|
|
4
|
+
module MultiAgent
|
|
5
|
+
# Read-only native authority for an approval decision on the parent itself.
|
|
6
|
+
# The Agent decision proof already contains its extension and retention set.
|
|
7
|
+
# @api private
|
|
8
|
+
class NativeApprovalEvidence
|
|
9
|
+
def self.capture(execution:, persistence:, native_persistence:, binding_key:)
|
|
10
|
+
unless native_persistence.agent_store.equal?(persistence) &&
|
|
11
|
+
native_persistence.coordinator.equal?(persistence.coordinator)
|
|
12
|
+
raise Phronomy::Persistence::StateConflictError, "Native approval proof belongs to another Store"
|
|
13
|
+
end
|
|
14
|
+
binding = execution.metadata.fetch("execution_extension")
|
|
15
|
+
unless binding.fetch("binding_key") == binding_key && binding.fetch("binding_version") == 1
|
|
16
|
+
raise Phronomy::Persistence::StateConflictError, "Unsupported native approval binding"
|
|
17
|
+
end
|
|
18
|
+
reference = binding["state_ref"]
|
|
19
|
+
snapshot = reference ? native_persistence.contents.fetch_json(reference) : {"children" => []}
|
|
20
|
+
children = snapshot.fetch("children")
|
|
21
|
+
NativeChildAuthority.new(agent_id: execution.agent_id, execution_id: execution.execution_id,
|
|
22
|
+
children: children, prior_slots: children.map { |child| child.fetch("slot") }, persistence: persistence)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module Phronomy
|
|
6
|
+
module MultiAgent
|
|
7
|
+
# Detached native child identities and current authority. Saving a captured
|
|
8
|
+
# outcome permits legal child progress; dispatch requires terminal or absent
|
|
9
|
+
# relationships before starting its still-unused continuation.
|
|
10
|
+
# @api private
|
|
11
|
+
class NativeChildAuthority
|
|
12
|
+
attr_reader :agent_ids
|
|
13
|
+
|
|
14
|
+
def initialize(agent_id:, execution_id:, children:, prior_slots:, persistence:)
|
|
15
|
+
@agent_id, @execution_id = agent_id, execution_id
|
|
16
|
+
@children = Phronomy::Values::Immutable.copy(children)
|
|
17
|
+
@prior_slots = Phronomy::Values::Immutable.copy(prior_slots)
|
|
18
|
+
@persistence_identity = persistence.execution_evidence_identity
|
|
19
|
+
validate_identities!
|
|
20
|
+
@agent_ids = ([@agent_id] + @children.map { |child| child.fetch("agent_id") }).uniq.sort.freeze
|
|
21
|
+
freeze
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Existing roots can be guarded. Authoritative absence is not a durable
|
|
25
|
+
# reservation extending beyond this transaction.
|
|
26
|
+
def guarded_agent_ids(persistence:, scope:)
|
|
27
|
+
validate_store!(persistence)
|
|
28
|
+
agent_ids.select { |id| id == @agent_id || read_root(persistence, scope, id) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def assert_current!(persistence:, scope:, outcome: false)
|
|
32
|
+
validate_store!(persistence)
|
|
33
|
+
@children.each do |child|
|
|
34
|
+
id, execution_id = child.fetch("agent_id"), child.fetch("execution_id")
|
|
35
|
+
root = read_root(persistence, scope, id)
|
|
36
|
+
# A coordinating reader can supply this transaction's previously
|
|
37
|
+
# observed child. Independent guarded retries always read afresh.
|
|
38
|
+
observed = if block_given?
|
|
39
|
+
yield(id, execution_id)
|
|
40
|
+
else
|
|
41
|
+
persistence.observe_execution(agent_id: id, execution_id: execution_id, scope: scope)
|
|
42
|
+
end
|
|
43
|
+
if !@prior_slots.include?(child.fetch("slot"))
|
|
44
|
+
unless root.nil? && observed.absent?
|
|
45
|
+
raise Phronomy::Persistence::StateConflictError, "Reserved native child appeared before continuation"
|
|
46
|
+
end
|
|
47
|
+
next
|
|
48
|
+
end
|
|
49
|
+
acknowledgement = child["terminal_acknowledgement"]
|
|
50
|
+
next if root.nil? && observed.absent? && !child.fetch("imported") && !acknowledgement
|
|
51
|
+
|
|
52
|
+
reservation = Phronomy::Agent::ReservedExecution.new(agent_id: id,
|
|
53
|
+
execution_id: execution_id, correlation: self.class.correlation(@agent_id, @execution_id, child))
|
|
54
|
+
definition = child.fetch("definition")
|
|
55
|
+
hold = Phronomy::Agent::Retention.new(agent_id: id, execution_id: execution_id,
|
|
56
|
+
owner_key: "subagent:#{@execution_id}")
|
|
57
|
+
allowed_status = observed.terminal? || (outcome && !child.fetch("imported") && !acknowledgement &&
|
|
58
|
+
observed.active?)
|
|
59
|
+
unless root && root.agent_definition_id == definition.fetch("id") &&
|
|
60
|
+
root.agent_definition_version == definition.fetch("version") && allowed_status &&
|
|
61
|
+
observed.reservation == reservation && persistence.retained_references(agent_id: id, scope: scope).include?(hold)
|
|
62
|
+
raise Phronomy::Persistence::StateConflictError, "Native child authority changed before application"
|
|
63
|
+
end
|
|
64
|
+
if acknowledgement && acknowledgement != self.class.terminal_acknowledgement(@agent_id, @execution_id, child, observed)
|
|
65
|
+
raise Phronomy::Persistence::StateConflictError, "Native terminal child acknowledgement changed before replay"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
nil
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.correlation(parent_id, parent_execution_id, child)
|
|
72
|
+
{"kind" => "subagent", "parent_agent_id" => parent_id,
|
|
73
|
+
"parent_execution_id" => parent_execution_id, "slot" => child.fetch("slot")}
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def self.terminal_acknowledgement(parent_id, parent_execution_id, child, observed)
|
|
77
|
+
owner = correlation(parent_id, parent_execution_id, child)
|
|
78
|
+
reservation = Phronomy::Agent::ReservedExecution.new(agent_id: child.fetch("agent_id"),
|
|
79
|
+
execution_id: child.fetch("execution_id"), correlation: owner)
|
|
80
|
+
return unless observed.terminal? && observed.reservation == reservation
|
|
81
|
+
|
|
82
|
+
# Cancellation intent and retention changes do not rewrite this result.
|
|
83
|
+
# The receipt identifies immutable child history without copying it.
|
|
84
|
+
outcome = {"result" => observed.result, "raw_output" => observed.raw_output,
|
|
85
|
+
"error" => observed.error, "transfer_receipt" => observed.transfer_receipt}
|
|
86
|
+
{"version" => 1, "agent_id" => observed.agent_id, "execution_id" => observed.execution_id,
|
|
87
|
+
"reservation" => owner, "status" => observed.status.to_s,
|
|
88
|
+
"outcome_digest" => Digest::SHA256.hexdigest(JSON.generate(ordered_outcome_value(outcome)))}
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def self.ordered_outcome_value(value)
|
|
92
|
+
# Results allow integers beyond Canonical JSON's safe number range.
|
|
93
|
+
# Order keys without narrowing the accepted JSON result values.
|
|
94
|
+
case value
|
|
95
|
+
when Hash then value.keys.sort.to_h { |key| [key, ordered_outcome_value(value.fetch(key))] }
|
|
96
|
+
when Array then value.map { |item| ordered_outcome_value(item) }
|
|
97
|
+
else value
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
private_class_method :ordered_outcome_value
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
def validate_identities!
|
|
105
|
+
unless @children.map { |child| child.fetch("slot") }.uniq.size == @children.size &&
|
|
106
|
+
@children.map { |child| child.fetch("agent_id") }.uniq.size == @children.size
|
|
107
|
+
raise Phronomy::Persistence::StateConflictError, "Native child identities are not unique"
|
|
108
|
+
end
|
|
109
|
+
@children.each do |child|
|
|
110
|
+
digest = Digest::SHA256.hexdigest(Phronomy::CanonicalJSON.dump(
|
|
111
|
+
["phronomy-subagent-v1", @agent_id, @execution_id, child.fetch("slot")]
|
|
112
|
+
))
|
|
113
|
+
unless child.fetch("agent_id") == "subagent-#{digest}" &&
|
|
114
|
+
child.fetch("execution_id") == "subagent-execution-#{digest}"
|
|
115
|
+
raise Phronomy::Persistence::StateConflictError, "Native child identity differs from its slot"
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def read_root(persistence, scope, id)
|
|
121
|
+
persistence.participate(scope) do |records|
|
|
122
|
+
records.agents.load(id)
|
|
123
|
+
rescue Phronomy::Persistence::NotFoundError
|
|
124
|
+
nil
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def validate_store!(persistence)
|
|
129
|
+
unless persistence.execution_evidence_identity.equal?(@persistence_identity)
|
|
130
|
+
raise Phronomy::Persistence::StateConflictError, "Native child proof belongs to another Store"
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phronomy
|
|
4
|
+
module MultiAgent
|
|
5
|
+
# Detached persistence plan for the native parent's dispatch participant.
|
|
6
|
+
# It restores only the original opaque state and its own parent retention;
|
|
7
|
+
# no participant, child admission, import callback or business work is replayed.
|
|
8
|
+
# @api private
|
|
9
|
+
class NativeDispatchEvidence
|
|
10
|
+
def self.capture(values:, persistence:, native_persistence:, binding_key:)
|
|
11
|
+
new(values, persistence, native_persistence, binding_key)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def initialize(values, persistence, native_persistence, binding_key)
|
|
15
|
+
@plan = NativePersistencePlan.new(values: values, execution: values.fetch(:result),
|
|
16
|
+
persistence: persistence, native_persistence: native_persistence, binding_key: binding_key)
|
|
17
|
+
prior = @plan.prior_children
|
|
18
|
+
preserved = prior.size <= @plan.children.size && prior.each_with_index.all? { |child, index|
|
|
19
|
+
current = @plan.children.fetch(index)
|
|
20
|
+
child.except("imported") == current.except("imported") && (!child.fetch("imported") || current.fetch("imported"))
|
|
21
|
+
}
|
|
22
|
+
unless preserved
|
|
23
|
+
raise Phronomy::Persistence::StateConflictError, "Native dispatch replaced an existing child slot"
|
|
24
|
+
end
|
|
25
|
+
@authority = NativeChildAuthority.new(agent_id: @plan.agent_id, execution_id: @plan.execution_id,
|
|
26
|
+
children: @plan.children, prior_slots: prior.map { |child| child.fetch("slot") }, persistence: persistence)
|
|
27
|
+
freeze
|
|
28
|
+
end
|
|
29
|
+
private_class_method :new
|
|
30
|
+
|
|
31
|
+
def agent_ids = @authority.agent_ids
|
|
32
|
+
def guarded_agent_ids(persistence:, scope:) = @authority.guarded_agent_ids(persistence: persistence, scope: scope)
|
|
33
|
+
def read(persistence:, scope:) = @plan.read(persistence: persistence, scope: scope)
|
|
34
|
+
def replay(persistence:, scope:) = @plan.replay(persistence: persistence, scope: scope)
|
|
35
|
+
def assert_retryable!(persistence:, scope:) = @authority.assert_current!(persistence: persistence, scope: scope)
|
|
36
|
+
|
|
37
|
+
def assert_continuable!(persistence:, scope:)
|
|
38
|
+
@plan.assert_content!(persistence: persistence, scope: scope)
|
|
39
|
+
@authority.assert_current!(persistence: persistence, scope: scope)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phronomy
|
|
4
|
+
module MultiAgent
|
|
5
|
+
# Captured native participant writes for an actual final outcome or wait.
|
|
6
|
+
# Child progress can continue while the same parent outcome is reconciled.
|
|
7
|
+
# Publishing these saved facts does not admit Provider or Tool work.
|
|
8
|
+
# @api private
|
|
9
|
+
class NativeOutcomeEvidence
|
|
10
|
+
def self.capture(values:, persistence:, native_persistence:, binding_key:)
|
|
11
|
+
new(values, persistence, native_persistence, binding_key)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def initialize(values, persistence, native_persistence, binding_key)
|
|
15
|
+
@plan = NativePersistencePlan.new(values: values, execution: values.fetch(:result).execution,
|
|
16
|
+
persistence: persistence, native_persistence: native_persistence, binding_key: binding_key)
|
|
17
|
+
prior = @plan.prior_children
|
|
18
|
+
preserved = prior.size == @plan.children.size && prior.each_with_index.all? { |child, index|
|
|
19
|
+
current = @plan.children.fetch(index)
|
|
20
|
+
child.except("terminal_acknowledgement") == current.except("terminal_acknowledgement") &&
|
|
21
|
+
(!child["terminal_acknowledgement"] || child["terminal_acknowledgement"] == current["terminal_acknowledgement"])
|
|
22
|
+
}
|
|
23
|
+
unless preserved
|
|
24
|
+
raise Phronomy::Persistence::StateConflictError, "Native outcome replaced an existing child slot"
|
|
25
|
+
end
|
|
26
|
+
@authority = NativeChildAuthority.new(agent_id: @plan.agent_id, execution_id: @plan.execution_id,
|
|
27
|
+
children: @plan.children, prior_slots: prior.map { |child| child.fetch("slot") }, persistence: persistence)
|
|
28
|
+
freeze
|
|
29
|
+
end
|
|
30
|
+
private_class_method :new
|
|
31
|
+
|
|
32
|
+
def guarded_agent_ids(persistence:, scope:) = @authority.guarded_agent_ids(persistence: persistence, scope: scope)
|
|
33
|
+
def read(persistence:, scope:) = @plan.read(persistence: persistence, scope: scope)
|
|
34
|
+
def replay(persistence:, scope:) = @plan.replay(persistence: persistence, scope: scope)
|
|
35
|
+
def assert_content!(persistence:, scope:) = @plan.assert_content!(persistence: persistence, scope: scope)
|
|
36
|
+
def assert_retryable!(persistence:, scope:) = @authority.assert_current!(persistence: persistence, scope: scope, outcome: true)
|
|
37
|
+
|
|
38
|
+
# Include deterministically reserved IDs even when their child never started.
|
|
39
|
+
def stopped_agent_ids = @authority.agent_ids
|
|
40
|
+
|
|
41
|
+
def assert_stopped_terminal!(persistence:, scope:)
|
|
42
|
+
@authority.assert_current!(persistence: persistence, scope: scope)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module Phronomy
|
|
6
|
+
module MultiAgent
|
|
7
|
+
# Captured native extension and parent retention writes. Domain adapters
|
|
8
|
+
# decide whether the captured relationships permit replay or continuation.
|
|
9
|
+
# @api private
|
|
10
|
+
class NativePersistencePlan
|
|
11
|
+
attr_reader :agent_id, :execution_id, :children, :prior_children
|
|
12
|
+
|
|
13
|
+
# A retention collection is a domain fact set, independent of the
|
|
14
|
+
# repository's enumeration order and private storage keys.
|
|
15
|
+
def self.retention_facts(values)
|
|
16
|
+
Phronomy::Values::Immutable.copy(values.sort_by { |value| Phronomy::CanonicalJSON.dump(value) })
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initialize(values:, execution:, persistence:, native_persistence:, binding_key:)
|
|
20
|
+
@persistence_identity, @native_persistence = persistence.execution_evidence_identity, native_persistence
|
|
21
|
+
validate_store!(persistence)
|
|
22
|
+
@agent_id, @execution_id = execution.agent_id, execution.execution_id
|
|
23
|
+
before, after = values.fetch(:before), values.fetch(:after)
|
|
24
|
+
before_binding = before.fetch(0).fetch(1).fetch("metadata").fetch("execution_extension")
|
|
25
|
+
after_binding = after.fetch(0).fetch(1).fetch("metadata").fetch("execution_extension")
|
|
26
|
+
[before_binding, after_binding].each do |binding|
|
|
27
|
+
unless binding.fetch("binding_key") == binding_key && binding.fetch("binding_version") == 1
|
|
28
|
+
raise Phronomy::Persistence::StateConflictError, "Unsupported native persistence binding"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
before_state = before.fetch(0).fetch(3) || {"children" => [], "cancel_requested" => false}
|
|
32
|
+
after_state = after.fetch(0).fetch(3)
|
|
33
|
+
@children = Phronomy::Values::Immutable.copy(after_state.fetch("children"))
|
|
34
|
+
@prior_children = Phronomy::Values::Immutable.copy(before_state.fetch("children"))
|
|
35
|
+
@reference = after_binding.fetch("state_ref").dup.freeze
|
|
36
|
+
@bytes = Phronomy::CanonicalJSON.dump(after_state).b.freeze
|
|
37
|
+
@canonicalization_version = Phronomy::CanonicalJSON::VERSION
|
|
38
|
+
unless @reference == "sha256:#{Digest::SHA256.hexdigest(@bytes)}"
|
|
39
|
+
raise Phronomy::Persistence::StateConflictError, "Native persistence content differs from its captured reference"
|
|
40
|
+
end
|
|
41
|
+
before_holds = self.class.retention_facts(before.fetch(1))
|
|
42
|
+
after_holds = self.class.retention_facts(after.fetch(1))
|
|
43
|
+
expected = Phronomy::Agent::Retention.new(agent_id: agent_id, execution_id: execution_id,
|
|
44
|
+
owner_key: "subagent:#{execution_id}")
|
|
45
|
+
additions = after_holds - before_holds
|
|
46
|
+
replayable = (before_holds - after_holds).empty? &&
|
|
47
|
+
(additions.empty? || additions == [expected.to_h]) &&
|
|
48
|
+
(children.empty? || after_holds.include?(expected.to_h))
|
|
49
|
+
unless replayable
|
|
50
|
+
raise Phronomy::Persistence::StateConflictError, "Native persistence retention change is not replayable"
|
|
51
|
+
end
|
|
52
|
+
@added_holds = additions.map { |hold| Phronomy::Agent::Retention.new(**hold.transform_keys(&:to_sym)) }.freeze
|
|
53
|
+
freeze
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def read(persistence:, scope:)
|
|
57
|
+
validate_store!(persistence)
|
|
58
|
+
self.class.retention_facts(persistence.retained_references(agent_id: agent_id, scope: scope).map(&:to_h))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def assert_content!(persistence:, scope:)
|
|
62
|
+
validate_store!(persistence)
|
|
63
|
+
@native_persistence.participate(scope) do |records|
|
|
64
|
+
unless records.contents.fetch(@reference) == @bytes
|
|
65
|
+
raise Phronomy::Persistence::StateConflictError, "Native persistence content differs from its captured bytes"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def replay(persistence:, scope:)
|
|
71
|
+
validate_store!(persistence)
|
|
72
|
+
@native_persistence.participate(scope) do |records|
|
|
73
|
+
reference = records.contents.put(@bytes, canonicalization_version: @canonicalization_version)
|
|
74
|
+
unless reference == @reference
|
|
75
|
+
raise Phronomy::Persistence::StateConflictError, "Native persistence replay changed content identity"
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
@added_holds.each { |hold| persistence.retain(hold, scope: scope) }
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def validate_store!(persistence)
|
|
85
|
+
unless persistence.execution_evidence_identity.equal?(@persistence_identity) &&
|
|
86
|
+
@native_persistence.agent_store.equal?(persistence) &&
|
|
87
|
+
@native_persistence.coordinator.equal?(persistence.coordinator)
|
|
88
|
+
raise Phronomy::Persistence::StateConflictError, "Native persistence proof belongs to another Store"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -59,8 +59,8 @@ module Phronomy
|
|
|
59
59
|
@participant = @coordination_store && DurableSubagentCoordinator.new(agent_class: self.class, persistence: @coordination_store)
|
|
60
60
|
end
|
|
61
61
|
|
|
62
|
-
# Release the local parent and its directly dispatched child after
|
|
63
|
-
#
|
|
62
|
+
# Release the local parent and its directly dispatched child after import
|
|
63
|
+
# or terminal-parent acknowledgement. The stored history remains intact.
|
|
64
64
|
# @api public
|
|
65
65
|
def unload_with_completed_child!(parent_execution_id, execution_id:)
|
|
66
66
|
owner = {"kind" => "subagent", "parent_agent_id" => agent_id, "parent_execution_id" => parent_execution_id}
|
|
@@ -99,73 +99,58 @@ module Phronomy
|
|
|
99
99
|
|
|
100
100
|
attr_writer :_orchestrator_context
|
|
101
101
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
input: input,
|
|
105
|
-
cancellation_token: cancellation_token,
|
|
106
|
-
config: {}
|
|
107
|
-
).wait_result
|
|
102
|
+
def execute(input:, cancellation_token: nil)
|
|
103
|
+
start_reserved_child(cancellation_token: cancellation_token, config: {}).wait_result
|
|
108
104
|
end
|
|
109
105
|
|
|
110
|
-
#
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
validated, schema_error = validate_arguments(args)
|
|
106
|
+
# An admitted slot must be reconciled even when its parent is cancelled.
|
|
107
|
+
def call_async(args, cancellation_token: nil, config: {})
|
|
108
|
+
if @_orchestrator_context&.fetch(:parent, nil) && config&.fetch(:phronomy_tool_invocation_id, nil)
|
|
109
|
+
_, schema_error = validate_arguments(args)
|
|
115
110
|
raise Phronomy::ToolError, schema_error if schema_error
|
|
116
|
-
|
|
111
|
+
start_reserved_child(cancellation_token: cancellation_token, config: config)
|
|
117
112
|
else
|
|
118
113
|
call_async_operation(args, cancellation_token: cancellation_token,
|
|
119
|
-
operation_name: "agent-tool-#{
|
|
120
|
-
|
|
114
|
+
operation_name: "agent-tool-#{name}") do
|
|
115
|
+
start_reserved_child(cancellation_token: cancellation_token, config: config)
|
|
121
116
|
end
|
|
122
117
|
end
|
|
123
118
|
rescue => error
|
|
124
119
|
Phronomy::TaskResult.failed(error, name: "subagent-dispatch-failed")
|
|
125
120
|
end
|
|
126
121
|
|
|
127
|
-
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
def start_reserved_child(cancellation_token:, config:)
|
|
128
125
|
ctx = @_orchestrator_context || {}
|
|
129
|
-
parent_ic = ctx[:invocation_context]
|
|
130
126
|
task_config = (ctx[:config] || {}).merge(config || {})
|
|
127
|
+
parent = ctx[:parent]
|
|
128
|
+
slot = task_config[:phronomy_tool_invocation_id]
|
|
129
|
+
execution_id = task_config[:execution_id]
|
|
130
|
+
unless parent && slot && execution_id
|
|
131
|
+
raise Phronomy::ToolError.new(
|
|
132
|
+
"#{name} requires an Orchestrator execution and a reserved Tool invocation. " \
|
|
133
|
+
"Use Orchestrator#subagent for direct calls.", code: "multi_agent.subagent_dispatch_unowned"
|
|
134
|
+
)
|
|
135
|
+
end
|
|
131
136
|
|
|
132
137
|
if cancellation_token && !task_config[:cancellation_token]
|
|
133
138
|
task_config = task_config.merge(cancellation_token: cancellation_token)
|
|
134
139
|
end
|
|
135
|
-
|
|
140
|
+
parent_ic = ctx[:invocation_context]
|
|
136
141
|
if parent_ic && !task_config[:invocation_context]
|
|
137
|
-
|
|
138
|
-
task_config = task_config.merge(invocation_context: child_ic)
|
|
142
|
+
task_config = task_config.merge(invocation_context: parent_ic.merge(parent_task_id: parent_ic.task_id))
|
|
139
143
|
end
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if parent && task_config[:phronomy_tool_invocation_id]
|
|
143
|
-
return DurableSubagentCoordinator.start(parent: parent,
|
|
144
|
-
tool_invocation_id: task_config.fetch(:phronomy_tool_invocation_id),
|
|
145
|
-
parent_execution_id: task_config.fetch(:execution_id), config: task_config)
|
|
146
|
-
end
|
|
147
|
-
agent = agent_class.new
|
|
148
|
-
if inherit_knowledge
|
|
149
|
-
Array(ctx[:knowledge]).each do |entry|
|
|
150
|
-
agent.add_knowledge(entry.content, metadata: entry.metadata)
|
|
151
|
-
end
|
|
152
|
-
end
|
|
153
|
-
Phronomy::AsyncOperation.call(name: "subagent-tool-#{name}",
|
|
154
|
-
on_error: ->(error) { raise error if on_error == :raise },
|
|
155
|
-
transform: ->(result) { result[:output] }) do
|
|
156
|
-
agent.invoke_async(input, config: task_config)
|
|
157
|
-
end
|
|
158
|
-
rescue => error
|
|
159
|
-
Phronomy::AsyncOperation.capture(name: "subagent-tool-#{name}",
|
|
160
|
-
on_error: ->(failure) { raise failure if on_error == :raise }) { raise error }
|
|
144
|
+
DurableSubagentCoordinator.start(parent: parent, tool_invocation_id: slot,
|
|
145
|
+
parent_execution_id: execution_id, config: task_config)
|
|
161
146
|
end
|
|
147
|
+
|
|
162
148
|
# Recovery requirements retain their Agent meaning across delegation.
|
|
163
149
|
def async_error_value(error)
|
|
164
150
|
raise error if error.is_a?(Phronomy::ExecutionRehydrationRequiredError)
|
|
165
151
|
|
|
166
152
|
super
|
|
167
153
|
end
|
|
168
|
-
private :execute_async, :async_error_value
|
|
169
154
|
end
|
|
170
155
|
|
|
171
156
|
@_subagent_tool_classes = (@_subagent_tool_classes || []) + [tool_class]
|
|
@@ -316,16 +301,7 @@ module Phronomy
|
|
|
316
301
|
prepared = super
|
|
317
302
|
return prepared unless self.class._subagent_tool_classes.include?(tool_class)
|
|
318
303
|
|
|
319
|
-
subagent_name = tool_class.tool_name.delete_prefix("dispatch_to_")
|
|
320
|
-
registration = self.class.registered_subagents.find do |name, _|
|
|
321
|
-
name.to_s == subagent_name
|
|
322
|
-
end&.last
|
|
323
|
-
inherits_knowledge = registration ? registration.fetch(:inherit_knowledge, true) : true
|
|
324
|
-
|
|
325
304
|
context = {parent: self}
|
|
326
|
-
# Invocation-owned Tools inherit the durable child slot's knowledge,
|
|
327
|
-
# captured by the existing preparation transaction off EventLoop.
|
|
328
|
-
context[:knowledge] = active_knowledge_snapshot if inherits_knowledge && !invocation
|
|
329
305
|
if invocation
|
|
330
306
|
context[:config] = invocation.config
|
|
331
307
|
context[:invocation_context] = invocation.config[:invocation_context]
|