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,495 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "weakref"
|
|
5
|
+
|
|
6
|
+
module Phronomy
|
|
7
|
+
module Agent
|
|
8
|
+
# Retains one original dispatch preparation across uncertain persistence and
|
|
9
|
+
# lost observations. Only the original EventLoop may apply its proof or grant
|
|
10
|
+
# the continuation permission; a worker never starts Provider or Tool work.
|
|
11
|
+
# @api private
|
|
12
|
+
class DispatchRecoveryOperation
|
|
13
|
+
Observe = Data.define(:operation, :observer, :kind)
|
|
14
|
+
Ready = Data.define(:operation, :attempt_id)
|
|
15
|
+
RecoveryOutcome = Data.define(:proof, :result)
|
|
16
|
+
private_constant :RecoveryOutcome
|
|
17
|
+
|
|
18
|
+
attr_reader :agent, :coordinator, :environment, :agent_id, :execution_id,
|
|
19
|
+
:fsm_session_id, :expected_execution_revision, :recovery_id, :recovery,
|
|
20
|
+
:command, :invocation, :dispatch_kind, :continuation_task, :continuation_delivery, :preparation_error
|
|
21
|
+
|
|
22
|
+
def initialize(agent:, coordinator:, environment:, command:, invocation:,
|
|
23
|
+
dispatch_kind:, preparer:, original_waiters:)
|
|
24
|
+
unless %i[provider tool].include?(dispatch_kind)
|
|
25
|
+
raise ArgumentError, "unsupported Agent dispatch kind"
|
|
26
|
+
end
|
|
27
|
+
@agent, @coordinator, @environment = agent, coordinator, environment
|
|
28
|
+
@agent_reference = WeakRef.new(agent)
|
|
29
|
+
@registry, @persistence, @preparer = environment.registry, agent.persistence, preparer
|
|
30
|
+
@command, @invocation, @dispatch_kind = command, invocation, dispatch_kind
|
|
31
|
+
@agent_id, @execution_id = agent.agent_id.to_s.freeze, command.execution_id
|
|
32
|
+
@fsm_session_id, @expected_execution_revision = command.fsm_session_id, command.expected_execution_revision
|
|
33
|
+
@recovery_id = SecureRandom.uuid.freeze
|
|
34
|
+
@recovery = DispatchRecovery.send(:new, self)
|
|
35
|
+
@mutex = Mutex.new
|
|
36
|
+
@attempts = Phronomy::RetainedAttempts.new(mutex: @mutex,
|
|
37
|
+
on_worker_recorded: ->(id, _result, error) { @original_error ||= error if id.nil? })
|
|
38
|
+
@original_waiters = original_waiters.dup
|
|
39
|
+
@evidence = @intended = @confirmation = @revision = nil
|
|
40
|
+
@original_error = @last_error = @preparation_error = nil
|
|
41
|
+
@uncertain = @initial_delivered = @continuation_authorized = @continuation_returned = @barrier_started = false
|
|
42
|
+
@application_state, @continuation_state = :pending, :pending
|
|
43
|
+
@continuation_task = @resolution_target = @continuation_delivery = nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def belongs_to?(candidate)
|
|
47
|
+
@agent_reference.__getobj__.equal?(candidate)
|
|
48
|
+
rescue WeakRef::RefError
|
|
49
|
+
false
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def pending? = @mutex.synchronize { @uncertain && !resolved_locked? }
|
|
53
|
+
def applied? = @mutex.synchronize { @application_state == :applied }
|
|
54
|
+
def resolved? = @mutex.synchronize { resolved_locked? }
|
|
55
|
+
def application_applied! = @mutex.synchronize { @application_state = :applied }
|
|
56
|
+
def continuation_authorized? = @mutex.synchronize { @continuation_authorized }
|
|
57
|
+
def continuation_returned? = @mutex.synchronize { @continuation_returned }
|
|
58
|
+
|
|
59
|
+
# A helper's normal return is separate from permission to invoke it. A
|
|
60
|
+
# raised admission call leaves this false even when no message is pending.
|
|
61
|
+
def continuation_returned!
|
|
62
|
+
@mutex.synchronize do
|
|
63
|
+
unless @continuation_authorized
|
|
64
|
+
raise Phronomy::Error, "Agent dispatch continuation has not been authorized"
|
|
65
|
+
end
|
|
66
|
+
@continuation_returned = true
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Taking this right precedes any call which might admit external work. A
|
|
71
|
+
# raised call is not evidence of rejection and cannot restore the right.
|
|
72
|
+
def authorize_continuation!
|
|
73
|
+
@mutex.synchronize do
|
|
74
|
+
return false if @continuation_authorized
|
|
75
|
+
unless %i[confirmed_post not_prepared].include?(@confirmation) && @application_state == :applied && @attempts.quiescent_locked?
|
|
76
|
+
raise Phronomy::Error, "Agent dispatch continuation requires applied preparation proof and physical completion"
|
|
77
|
+
end
|
|
78
|
+
@continuation_authorized = true
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def captured_cancellation_requested?
|
|
83
|
+
@mutex.synchronize { @evidence&.before&.fetch(0)&.fetch(4) == true }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# An external matching session completion can resolve custody outside this
|
|
87
|
+
# operation's own message delivery. Its caller retires only after use ends.
|
|
88
|
+
def retire_after_delivery
|
|
89
|
+
retire_resolved
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def prepare_continuation
|
|
93
|
+
@continuation_task ||= Phronomy::TaskResult.deferred(name: "dispatch-continuation:#{recovery_id}")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Preserve the exact admitted continuation's notification custody. A
|
|
97
|
+
# rejected notification may be redelivered without restarting its work.
|
|
98
|
+
def prepare_continuation_delivery
|
|
99
|
+
@continuation_delivery ||= DispatchContinuationDelivery.new(event_sink: invocation.event_sink)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def resolution_permitted?
|
|
103
|
+
@mutex.synchronize do
|
|
104
|
+
@resolution_target && @application_state == :applied && @attempts.quiescent_locked?
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Registry invokes this under its removal lock, preserving one publication
|
|
109
|
+
# point between retained barrier custody and the original job continuation.
|
|
110
|
+
def resolution_released!
|
|
111
|
+
@mutex.synchronize do
|
|
112
|
+
@continuation_state = @resolution_target
|
|
113
|
+
@resolution_target = nil
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def continuation_completed!(state = nil)
|
|
118
|
+
@mutex.synchronize do
|
|
119
|
+
unless @continuation_authorized
|
|
120
|
+
raise Phronomy::Error, "Agent dispatch continuation has not been authorized"
|
|
121
|
+
end
|
|
122
|
+
expected = (@confirmation == :not_prepared) ? :stopped : :resumed
|
|
123
|
+
state ||= expected
|
|
124
|
+
raise Phronomy::Error, "Agent dispatch continuation contradicts its proof" unless state == expected
|
|
125
|
+
end
|
|
126
|
+
resolve_as(state)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# A physically finished worker which never entered its persistence barrier
|
|
130
|
+
# can hand its original setup failure onward. Missing proof after barrier
|
|
131
|
+
# entry is insufficient; it must remain an unresolved save instead.
|
|
132
|
+
def prepare_failed_continuation!
|
|
133
|
+
@mutex.synchronize do
|
|
134
|
+
unless not_prepared_locked?
|
|
135
|
+
raise Phronomy::Error, "Agent dispatch preparation has no positive pre-barrier failure proof"
|
|
136
|
+
end
|
|
137
|
+
@confirmation, @revision = :not_prepared, nil
|
|
138
|
+
@preparation_error ||= @attempts.snapshot_locked(nil)[:error]
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def start
|
|
143
|
+
@registry.retain_dispatch_operation(self)
|
|
144
|
+
retained = true
|
|
145
|
+
task = submit_worker(nil) do
|
|
146
|
+
method = (dispatch_kind == :provider) ? :prepare_provider : :prepare_tools
|
|
147
|
+
@preparer.public_send(method, @command,
|
|
148
|
+
on_barrier_started: -> { @mutex.synchronize { @barrier_started = true } },
|
|
149
|
+
on_evidence: ->(value) { @mutex.synchronize { @evidence = value } },
|
|
150
|
+
on_outcome: ->(value) { @mutex.synchronize { @intended = value } })
|
|
151
|
+
end
|
|
152
|
+
watch(nil)
|
|
153
|
+
self
|
|
154
|
+
rescue => error
|
|
155
|
+
raise unless retained
|
|
156
|
+
task ? mark_uncertain_on_owner(error) : signal(nil)
|
|
157
|
+
self
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def observe(kind = :reconcile)
|
|
161
|
+
unless %i[reconcile retry].include?(kind)
|
|
162
|
+
raise ArgumentError, "unsupported Agent dispatch recovery operation"
|
|
163
|
+
end
|
|
164
|
+
observer = Phronomy::TaskResult.deferred(name: "agent-dispatch-#{kind}:#{recovery_id}")
|
|
165
|
+
@attempts.register(observer)
|
|
166
|
+
registry, resolved = @mutex.synchronize { [@registry, resolved_locked?] }
|
|
167
|
+
if resolved
|
|
168
|
+
observer.complete(build_result)
|
|
169
|
+
elsif !registry || !registry.post(Observe.new(operation: self, observer: observer, kind: kind),
|
|
170
|
+
completion: delivery_observer(observer))
|
|
171
|
+
observer.complete(unavailable_result(Phronomy::RuntimeShutdownError.new("Original Agent owner is unavailable")))
|
|
172
|
+
end
|
|
173
|
+
observer
|
|
174
|
+
rescue => error
|
|
175
|
+
observer&.complete(unavailable_result(error))
|
|
176
|
+
observer || raise
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def deliver(message)
|
|
180
|
+
case message
|
|
181
|
+
when Observe then observe_on_owner(message.observer, message.kind)
|
|
182
|
+
when Ready then message.attempt_id ? finish_attempt(message.attempt_id) : deliver_initial
|
|
183
|
+
else raise Phronomy::Error, "unsupported Agent dispatch recovery message"
|
|
184
|
+
end
|
|
185
|
+
ensure
|
|
186
|
+
retire_resolved
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def retain_original_waiters(waiters)
|
|
190
|
+
@mutex.synchronize { @original_waiters |= waiters }
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Closing the original observation never acknowledges the physical end of
|
|
194
|
+
# its still-live FSM or releases its operation lease.
|
|
195
|
+
def fail_original(error)
|
|
196
|
+
waiters, failure = @mutex.synchronize do
|
|
197
|
+
@uncertain = true
|
|
198
|
+
@last_error = error
|
|
199
|
+
@original_error ||= error
|
|
200
|
+
[@original_waiters.dup, diagnostic_locked(error)]
|
|
201
|
+
end
|
|
202
|
+
waiters.each { |task| task.fail(failure) }
|
|
203
|
+
failure
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def shutdown(error)
|
|
207
|
+
failure = fail_original(error)
|
|
208
|
+
@attempts.observers.each { |observer| observer.complete(unavailable_result(error)) }
|
|
209
|
+
failure
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
private
|
|
213
|
+
|
|
214
|
+
def resolved_locked?
|
|
215
|
+
@application_state == :applied && %i[resumed stopped].include?(@continuation_state) && @attempts.quiescent_locked?
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def not_prepared_locked?
|
|
219
|
+
completion = @attempts.snapshot_locked(nil)
|
|
220
|
+
completion[:physical] && completion[:error] && !@evidence &&
|
|
221
|
+
(!@barrier_started || (completion[:rejection_confirmed] && !completion[:started]))
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def resolve_as(state)
|
|
225
|
+
@mutex.synchronize { @resolution_target = state }
|
|
226
|
+
unless @registry.resolve_dispatch_operation(self)
|
|
227
|
+
raise Phronomy::Error, "Agent dispatch continuation ownership changed before resolution"
|
|
228
|
+
end
|
|
229
|
+
rescue => error
|
|
230
|
+
raise error unless resolved?
|
|
231
|
+
ensure
|
|
232
|
+
@mutex.synchronize { @resolution_target = nil }
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def retire_resolved
|
|
236
|
+
@mutex.synchronize do
|
|
237
|
+
return unless resolved_locked? && !@attempts.active_snapshot_locked
|
|
238
|
+
@agent = @coordinator = @environment = @registry = @persistence = @preparer = nil
|
|
239
|
+
@command = @invocation = @evidence = @intended = @continuation_delivery = nil
|
|
240
|
+
@original_waiters = []
|
|
241
|
+
@attempts.release_references_locked(nil)
|
|
242
|
+
@original_error = @last_error = @preparation_error = nil
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def submit_worker(id, &work)
|
|
247
|
+
@attempts.submit(id, submitter: @environment,
|
|
248
|
+
missing_handle_message: "Agent dispatch submission returned no task handle", &work)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def watch(id)
|
|
252
|
+
@attempts.watch(id) { |_event| signal(id) }
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def signal(id)
|
|
256
|
+
registry = @mutex.synchronize { @registry }
|
|
257
|
+
return unless registry
|
|
258
|
+
return if registry.post(Ready.new(operation: self, attempt_id: id))
|
|
259
|
+
delivery_failed(id, Phronomy::RuntimeShutdownError.new("Agent owner rejected dispatch evidence"))
|
|
260
|
+
rescue => error
|
|
261
|
+
delivery_failed(id, error)
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def delivery_failed(id, error)
|
|
265
|
+
return fail_original(error) unless id
|
|
266
|
+
observers = @mutex.synchronize do
|
|
267
|
+
@attempts.take_observers_locked(id)
|
|
268
|
+
end
|
|
269
|
+
observers.each { |observer| observer.complete(unavailable_result(error)) }
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def delivery_observer(observer)
|
|
273
|
+
Phronomy::TaskResult.deferred(name: "agent-dispatch-delivery:#{recovery_id}").tap do |delivery|
|
|
274
|
+
delivery.on_complete { |_value, error| observer.complete(unavailable_result(error)) if error }
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def deliver_initial
|
|
279
|
+
@attempts.refresh(nil)
|
|
280
|
+
slot, uncertain, not_prepared, delivered = @mutex.synchronize do
|
|
281
|
+
[@attempts.snapshot_locked(nil), @uncertain, not_prepared_locked?, @initial_delivered]
|
|
282
|
+
end
|
|
283
|
+
return if delivered || resolved? || !slot[:logical]
|
|
284
|
+
if not_prepared
|
|
285
|
+
@mutex.synchronize { @initial_delivered = true }
|
|
286
|
+
apply_preparation_failure
|
|
287
|
+
return
|
|
288
|
+
end
|
|
289
|
+
if slot[:error] || uncertain
|
|
290
|
+
mark_uncertain_on_owner(slot[:error] || @last_error || Phronomy::Error.new("Dispatch preparation requires explicit recovery"))
|
|
291
|
+
@mutex.synchronize { @initial_delivered = true }
|
|
292
|
+
return
|
|
293
|
+
end
|
|
294
|
+
return unless slot[:physical]
|
|
295
|
+
@mutex.synchronize { @initial_delivered = true }
|
|
296
|
+
remember_post(slot[:result])
|
|
297
|
+
apply_confirmation
|
|
298
|
+
rescue => error
|
|
299
|
+
mark_uncertain_on_owner(error)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def mark_uncertain_on_owner(error)
|
|
303
|
+
failure = fail_original(error)
|
|
304
|
+
coordinator = @mutex.synchronize { @coordinator }
|
|
305
|
+
coordinator&.send(:dispatch_recovery_failed_on_event_loop, self, failure)
|
|
306
|
+
failure
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def observe_on_owner(observer, kind)
|
|
310
|
+
return if observer.done?
|
|
311
|
+
@attempts.refresh(nil)
|
|
312
|
+
return observer.complete(build_result) if resolved?
|
|
313
|
+
registry, physical, active = @mutex.synchronize do
|
|
314
|
+
[@registry, @attempts.snapshot_locked(nil)[:physical], @attempts.active_snapshot_locked]
|
|
315
|
+
end
|
|
316
|
+
unless registry&.owns_dispatch_operation?(self)
|
|
317
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("Agent dispatch recovery owner no longer matches")))
|
|
318
|
+
end
|
|
319
|
+
# Ready rejection can close the original observer off the EventLoop.
|
|
320
|
+
# Publish the matching domain transition before any cached outcome is
|
|
321
|
+
# applied, including closure of its former stream listener.
|
|
322
|
+
mark_uncertain_on_owner(@mutex.synchronize { @last_error }) if pending?
|
|
323
|
+
unless physical
|
|
324
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("Original dispatch preparation has no physical completion proof")))
|
|
325
|
+
end
|
|
326
|
+
return observe_active_attempt(observer, active, kind) if active
|
|
327
|
+
if @mutex.synchronize { @confirmation == :not_prepared || not_prepared_locked? }
|
|
328
|
+
return observer.complete(apply_preparation_failure)
|
|
329
|
+
end
|
|
330
|
+
# A participant or reserved child needs current authority before new
|
|
331
|
+
# admission, including participants with no persisted extension. A
|
|
332
|
+
# historical save cannot prove their current continuation permission.
|
|
333
|
+
fresh_proof = @mutex.synchronize do
|
|
334
|
+
(@command.invocation_config[:phronomy_execution_participant] ||
|
|
335
|
+
@command.execution.metadata["reservation"] || @command.execution.metadata["execution_extension"] ||
|
|
336
|
+
@evidence&.participation?) && !@continuation_authorized
|
|
337
|
+
end
|
|
338
|
+
if !fresh_proof && @mutex.synchronize { @confirmation == :confirmed_post }
|
|
339
|
+
return observer.complete(apply_confirmation)
|
|
340
|
+
end
|
|
341
|
+
outcome = @mutex.synchronize { @attempts.snapshot_locked(nil)[:result] }
|
|
342
|
+
if outcome && (!fresh_proof || @mutex.synchronize { @evidence })
|
|
343
|
+
remember_post(outcome)
|
|
344
|
+
return observer.complete(apply_confirmation) unless fresh_proof
|
|
345
|
+
end
|
|
346
|
+
begin_recovery_attempt(observer, kind)
|
|
347
|
+
rescue => error
|
|
348
|
+
observer.complete(unavailable_result(error))
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def observe_active_attempt(observer, active, kind)
|
|
352
|
+
if active[:admission_unknown]
|
|
353
|
+
return observer.complete(unavailable_result(active[:error]))
|
|
354
|
+
end
|
|
355
|
+
if active[:watch_error]
|
|
356
|
+
@attempts.refresh(active[:id])
|
|
357
|
+
active = @mutex.synchronize { @attempts.snapshot_locked(active[:id]) }
|
|
358
|
+
observable = active && ((active[:logical] && active[:physical]) || active[:physical_watch])
|
|
359
|
+
return observer.complete(unavailable_result(active&.fetch(:watch_error))) unless observable
|
|
360
|
+
end
|
|
361
|
+
if active[:kind] != kind
|
|
362
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("A different Agent dispatch recovery operation is physically active")))
|
|
363
|
+
end
|
|
364
|
+
@mutex.synchronize { @attempts.join_locked(active[:id], observer: observer) }
|
|
365
|
+
finish_attempt(active[:id])
|
|
366
|
+
nil
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def begin_recovery_attempt(observer, kind)
|
|
370
|
+
evidence, persistence, preparer = @mutex.synchronize { [@evidence, @persistence, @preparer] }
|
|
371
|
+
unless evidence
|
|
372
|
+
message = if @command.invocation_config[:phronomy_execution_participant] || @command.execution.metadata["execution_extension"]
|
|
373
|
+
"Synchronous participant has no supported retained dispatch proof; callback replay is unavailable"
|
|
374
|
+
else
|
|
375
|
+
"Dispatch preparation has no complete captured proposal"
|
|
376
|
+
end
|
|
377
|
+
return observer.complete(unavailable_result(Phronomy::Error.new(message)))
|
|
378
|
+
end
|
|
379
|
+
id = Object.new.freeze
|
|
380
|
+
evidence_action = @mutex.synchronize do
|
|
381
|
+
# A later write or read invalidates a previously observed pre-state.
|
|
382
|
+
# Keep confirmed post-state sticky, but never turn a lost retry reply
|
|
383
|
+
# into a new claim that the original pre-state still holds.
|
|
384
|
+
@confirmation = @revision = nil unless @confirmation == :confirmed_post
|
|
385
|
+
@attempts.reserve_locked(id, kind: kind, observer: observer)
|
|
386
|
+
(@confirmation == :confirmed_post) ? :reconcile : kind
|
|
387
|
+
end
|
|
388
|
+
task = submit_worker(id) do
|
|
389
|
+
proof = evidence.public_send(evidence_action, persistence: persistence, original_error: @original_error)
|
|
390
|
+
result = preparer.recover_result(evidence) if proof.disposition == :committed
|
|
391
|
+
RecoveryOutcome.new(proof: proof, result: result)
|
|
392
|
+
end
|
|
393
|
+
watch(id)
|
|
394
|
+
rescue => error
|
|
395
|
+
if task || (id && @mutex.synchronize { @attempts.snapshot_locked(id)&.fetch(:admission_unknown) })
|
|
396
|
+
delivery_failed(id, error)
|
|
397
|
+
else
|
|
398
|
+
observers = @attempts.retire(defined?(id) && id)
|
|
399
|
+
(observers.empty? ? [observer] : observers).each { |item| item.complete(unavailable_result(error)) }
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
def finish_attempt(id)
|
|
404
|
+
@attempts.refresh(id)
|
|
405
|
+
slot = @mutex.synchronize { @attempts.snapshot_locked(id) }
|
|
406
|
+
return unless slot && slot[:logical] && slot[:physical]
|
|
407
|
+
if slot[:error] && !slot[:error].is_a?(StandardError)
|
|
408
|
+
@attempts.retire(id).each { |observer| observer.fail(slot[:error]) }
|
|
409
|
+
return
|
|
410
|
+
end
|
|
411
|
+
proof, result = slot[:result]&.then { |value| [value.proof, value.result] }
|
|
412
|
+
observation = if proof&.disposition == :committed
|
|
413
|
+
remember_post(result)
|
|
414
|
+
if proof.continuation_error
|
|
415
|
+
build_result(error: diagnostic(proof.continuation_error))
|
|
416
|
+
else
|
|
417
|
+
apply_confirmation
|
|
418
|
+
end
|
|
419
|
+
elsif proof&.disposition == :not_committed && @mutex.synchronize { @confirmation != :confirmed_post }
|
|
420
|
+
@mutex.synchronize { @confirmation, @revision = :confirmed_pre, expected_execution_revision }
|
|
421
|
+
build_result(error: proof.read_error && diagnostic(proof.read_error))
|
|
422
|
+
else
|
|
423
|
+
error = slot[:error] || proof&.read_error
|
|
424
|
+
if !error && @mutex.synchronize { @confirmation == :confirmed_post }
|
|
425
|
+
error = Phronomy::Persistence::StateConflictError.new("Saved dispatch authority no longer matches its captured evidence")
|
|
426
|
+
end
|
|
427
|
+
build_result(outcome: error ? :unavailable : :unresolved, error: error && diagnostic(error))
|
|
428
|
+
end
|
|
429
|
+
@attempts.retire(id).each_with_index do |observer, index|
|
|
430
|
+
observer.complete(index.zero? ? observation : build_result(outcome: observation.outcome, error: observation.error))
|
|
431
|
+
end
|
|
432
|
+
rescue => error
|
|
433
|
+
@attempts.retire(id).each { |observer| observer.complete(unavailable_result(error)) }
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
def remember_post(outcome)
|
|
437
|
+
raise Phronomy::Error, "Agent dispatch save returned no semantic outcome" unless outcome
|
|
438
|
+
@mutex.synchronize do
|
|
439
|
+
@confirmation, @revision = :confirmed_post, outcome.execution.execution_revision
|
|
440
|
+
@intended = outcome
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
def apply_preparation_failure
|
|
445
|
+
prepare_failed_continuation!
|
|
446
|
+
@coordinator.send(:stop_retained_dispatch_on_event_loop, self, @preparation_error)
|
|
447
|
+
build_result
|
|
448
|
+
rescue => error
|
|
449
|
+
mark_uncertain_on_owner(error)
|
|
450
|
+
build_result(error: diagnostic(error))
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def apply_confirmation
|
|
454
|
+
coordinator, outcome = @mutex.synchronize { [@coordinator, @intended] }
|
|
455
|
+
return build_result if resolved?
|
|
456
|
+
coordinator.send(:apply_retained_dispatch_on_event_loop, self, outcome)
|
|
457
|
+
build_result
|
|
458
|
+
rescue => error
|
|
459
|
+
mark_uncertain_on_owner(error)
|
|
460
|
+
build_result(error: diagnostic(error))
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
def unavailable_result(error)
|
|
464
|
+
build_result(outcome: :unavailable, error: diagnostic(error))
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
def build_result(outcome: nil, error: nil)
|
|
468
|
+
confirmation, application, continuation, revision, physical = @mutex.synchronize do
|
|
469
|
+
[@confirmation, @application_state, @continuation_state, @revision, @attempts.quiescent_locked?]
|
|
470
|
+
end
|
|
471
|
+
DispatchRecoveryResult.new(recovery: recovery, outcome: confirmation || outcome || :unavailable,
|
|
472
|
+
application_state: application, continuation_state: continuation, revision: revision,
|
|
473
|
+
physical_complete: physical, execution_task: %i[resumed stopped].include?(continuation) ? execution_observer : nil,
|
|
474
|
+
error: error)
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
def execution_observer
|
|
478
|
+
Phronomy::TaskResult.deferred(name: "dispatch-execution-observer:#{recovery_id}").tap do |observer|
|
|
479
|
+
@continuation_task.on_complete { |value, error| error ? observer.fail(error) : observer.complete(value) }
|
|
480
|
+
end
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
def diagnostic(error)
|
|
484
|
+
@mutex.synchronize { diagnostic_locked(error) }
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
def diagnostic_locked(error)
|
|
488
|
+
return error if error.is_a?(DispatchRecoveryError) && error.recovery.equal?(@recovery)
|
|
489
|
+
DispatchRecoveryError.new(recovery: @recovery,
|
|
490
|
+
error: error || Phronomy::Error.new("Agent dispatch outcome requires reconciliation"),
|
|
491
|
+
original_error: @original_error || error)
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
end
|
|
495
|
+
end
|