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,496 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "weakref"
|
|
5
|
+
|
|
6
|
+
module Phronomy
|
|
7
|
+
module Agent
|
|
8
|
+
# Retains one original approval decision until its saved pre/post state and
|
|
9
|
+
# exact local continuation are known. Workers publish facts; only the original
|
|
10
|
+
# EventLoop may restore the wait or start the already-authorized continuation.
|
|
11
|
+
# @api private
|
|
12
|
+
class ApprovalRecoveryOperation
|
|
13
|
+
Observe = Data.define(:operation, :observer)
|
|
14
|
+
Ready = Data.define(:operation, :attempt_id)
|
|
15
|
+
|
|
16
|
+
attr_reader :agent, :coordinator, :environment, :agent_id, :execution_id,
|
|
17
|
+
:approval_request_id, :approved, :recovery_id, :recovery, :command,
|
|
18
|
+
:request, :invocation, :suspended_cancellation, :continuation_plan
|
|
19
|
+
|
|
20
|
+
def initialize(agent:, coordinator:, environment:, command:, request:, invocation:,
|
|
21
|
+
suspended_cancellation:, committer:)
|
|
22
|
+
@agent, @coordinator, @environment = agent, coordinator, environment
|
|
23
|
+
@agent_reference = WeakRef.new(agent)
|
|
24
|
+
@registry, @persistence, @committer = environment.registry, agent.persistence, committer
|
|
25
|
+
@command, @request, @invocation = command, request, invocation
|
|
26
|
+
task = request.result_task
|
|
27
|
+
@hosted_context = task.respond_to?(:hosted_context) ? task.hosted_context : nil
|
|
28
|
+
@parent_wait = task.respond_to?(:parent_wait) ? task.parent_wait : nil
|
|
29
|
+
@suspended_cancellation = suspended_cancellation
|
|
30
|
+
@agent_id, @execution_id = agent.agent_id.to_s.freeze, command.execution_id
|
|
31
|
+
@approval_request_id, @approved = command.approval_request_id, command.approved
|
|
32
|
+
@recovery_id = SecureRandom.uuid.freeze
|
|
33
|
+
@recovery = ApprovalRecovery.send(:new, self)
|
|
34
|
+
@mutex = Mutex.new
|
|
35
|
+
@attempts = Phronomy::RetainedAttempts.new(mutex: @mutex,
|
|
36
|
+
on_worker_recorded: ->(id, _result, error) { @original_error ||= error if id.nil? })
|
|
37
|
+
@evidence = @intended = @confirmation = nil
|
|
38
|
+
@write_started = false
|
|
39
|
+
@unwritten_before = nil
|
|
40
|
+
@original_error = @last_error = nil
|
|
41
|
+
@uncertain = @initial_delivered = false
|
|
42
|
+
@application_state, @continuation_state = :pending, :pending
|
|
43
|
+
@canonical_task = @continuation_plan = 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 && @continuation_state == :pending }
|
|
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
|
+
|
|
57
|
+
def captured_cancellation_requested?
|
|
58
|
+
@mutex.synchronize { (@evidence&.before || @unwritten_before)&.fetch(:agent)&.fetch(4) == true }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def resolution_permitted?
|
|
62
|
+
@mutex.synchronize do
|
|
63
|
+
@resolution_target && @application_state == :applied &&
|
|
64
|
+
@attempts.quiescent_locked?
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Registry publishes this only after removing the exact retained entry.
|
|
69
|
+
def resolution_released!
|
|
70
|
+
@mutex.synchronize do
|
|
71
|
+
@continuation_state = @resolution_target
|
|
72
|
+
@resolution_target = nil
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def continuation_completed!(state)
|
|
77
|
+
@mutex.synchronize { @resolution_target = state }
|
|
78
|
+
unless @registry.resolve_approval_operation(self)
|
|
79
|
+
raise Phronomy::Error, "Approval continuation ownership changed before resolution"
|
|
80
|
+
end
|
|
81
|
+
rescue => error
|
|
82
|
+
# A wrapper may lose the reply after the registry performed its atomic
|
|
83
|
+
# removal/publication. An exception before that point cannot resolve us.
|
|
84
|
+
raise error unless resolved?
|
|
85
|
+
ensure
|
|
86
|
+
@mutex.synchronize { @resolution_target = nil }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def continue_terminal_if_ready
|
|
90
|
+
return unless resolved? && @terminal_continuation
|
|
91
|
+
continuation, @terminal_continuation = @terminal_continuation, nil
|
|
92
|
+
continuation.call
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def prepare_continuation
|
|
96
|
+
return @continuation_plan if @continuation_plan
|
|
97
|
+
prepare_continuation_task unless @canonical_task
|
|
98
|
+
owner_coordinator = coordinator
|
|
99
|
+
@continuation_plan = ApprovalContinuationPlan.new(environment: environment,
|
|
100
|
+
invocation: invocation, result_task: @canonical_task, approved: approved,
|
|
101
|
+
config: request.config, cancel_requested: captured_cancellation_requested?, on_complete: lambda do |**result|
|
|
102
|
+
owner_coordinator.deliver_on_event_loop(ExecutionCoordinator::SessionFinishedCommand.new(
|
|
103
|
+
coordinator: owner_coordinator, **result
|
|
104
|
+
))
|
|
105
|
+
end)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def continuation_task = @canonical_task
|
|
109
|
+
|
|
110
|
+
def validate_hosted_authority!
|
|
111
|
+
@hosted_context&.validate_live!
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def observe_continuation_once
|
|
115
|
+
return if @continuation_observed
|
|
116
|
+
@continuation_observed = true
|
|
117
|
+
yield
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# An accepted parent may cancel while another child is positively unadmitted.
|
|
121
|
+
# Its terminal handoff may abandon those children, never uncertain admission.
|
|
122
|
+
def parent_finished(fsm_session_id, &continuation)
|
|
123
|
+
return false unless @continuation_plan&.parent_accepted? &&
|
|
124
|
+
fsm_session_id.to_s == @continuation_plan.parent_id.to_s
|
|
125
|
+
unless @continuation_plan.finish_parent(fsm_session_id)
|
|
126
|
+
@terminal_continuation = continuation
|
|
127
|
+
return false
|
|
128
|
+
end
|
|
129
|
+
continuation_completed!(:resumed) unless resolved?
|
|
130
|
+
true
|
|
131
|
+
rescue => error
|
|
132
|
+
@terminal_continuation = continuation
|
|
133
|
+
mark_uncertain_on_owner(error)
|
|
134
|
+
false
|
|
135
|
+
ensure
|
|
136
|
+
retire_resolved
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def start
|
|
140
|
+
@registry.retain_approval_operation(self)
|
|
141
|
+
retained = true
|
|
142
|
+
task = submit_worker(nil) do
|
|
143
|
+
@committer.commit_retained(@command,
|
|
144
|
+
parent_evidence: @hosted_context&.parent_evidence,
|
|
145
|
+
participant: @invocation.config[:phronomy_execution_participant],
|
|
146
|
+
on_write_started: -> { @mutex.synchronize { @write_started = true } },
|
|
147
|
+
on_evidence: ->(value) { @mutex.synchronize { @evidence = value } },
|
|
148
|
+
on_outcome: ->(value) { @mutex.synchronize { @intended = value } })
|
|
149
|
+
end
|
|
150
|
+
watch(nil)
|
|
151
|
+
self
|
|
152
|
+
rescue => error
|
|
153
|
+
raise unless retained
|
|
154
|
+
if task
|
|
155
|
+
mark_uncertain_on_owner(error)
|
|
156
|
+
else
|
|
157
|
+
signal(nil)
|
|
158
|
+
end
|
|
159
|
+
self
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def observe
|
|
163
|
+
observer = Phronomy::TaskResult.deferred(name: "agent-approval-reconcile:#{recovery_id}")
|
|
164
|
+
@attempts.register(observer)
|
|
165
|
+
registry, resolved = @mutex.synchronize { [@registry, resolved_locked?] }
|
|
166
|
+
if resolved
|
|
167
|
+
observer.complete(build_result)
|
|
168
|
+
elsif !registry || !registry.post(Observe.new(operation: self, observer: observer), completion: delivery_observer(observer))
|
|
169
|
+
observer.complete(unavailable_result(Phronomy::RuntimeShutdownError.new("Original Agent owner is unavailable")))
|
|
170
|
+
end
|
|
171
|
+
observer
|
|
172
|
+
rescue => error
|
|
173
|
+
observer&.complete(unavailable_result(error))
|
|
174
|
+
observer || raise
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def deliver(message)
|
|
178
|
+
case message
|
|
179
|
+
when Observe then observe_on_owner(message.observer)
|
|
180
|
+
when Ready then message.attempt_id ? finish_attempt(message.attempt_id) : deliver_initial
|
|
181
|
+
else raise Phronomy::Error, "unsupported Agent approval recovery message"
|
|
182
|
+
end
|
|
183
|
+
ensure
|
|
184
|
+
retire_resolved
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def shutdown(error)
|
|
188
|
+
failure = fail_original(error)
|
|
189
|
+
@attempts.observers.each { |observer| observer.complete(unavailable_result(error)) }
|
|
190
|
+
failure
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def retain_original_waiters(_waiters)
|
|
194
|
+
# The original invocation still represents the Agent job, not this save.
|
|
195
|
+
# Shutdown may fail it logically but cannot acknowledge its physical end.
|
|
196
|
+
acknowledge_original_if_quiet
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def fail_original(error)
|
|
200
|
+
failure = @mutex.synchronize do
|
|
201
|
+
@uncertain = true
|
|
202
|
+
@last_error = error
|
|
203
|
+
@original_error ||= error
|
|
204
|
+
diagnostic_locked(error)
|
|
205
|
+
end
|
|
206
|
+
@request&.result_task&.fail(failure)
|
|
207
|
+
acknowledge_original_if_quiet
|
|
208
|
+
failure
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
private
|
|
212
|
+
|
|
213
|
+
def prepare_continuation_task
|
|
214
|
+
@canonical_task = @coordinator.send(:build_retained_approval_task,
|
|
215
|
+
name: "approval-continuation:#{recovery_id}", parent_wait: @parent_wait,
|
|
216
|
+
hosted_context: @hosted_context)
|
|
217
|
+
original = @request.result_task
|
|
218
|
+
original.bind_hosted_continuation(@canonical_task) if @hosted_context
|
|
219
|
+
@canonical_task.on_complete do |value, error|
|
|
220
|
+
original.finish_operation! if original.respond_to?(:finish_operation!)
|
|
221
|
+
error ? original.fail(error) : original.complete(value)
|
|
222
|
+
end
|
|
223
|
+
# The canonical task acquires its lease before this original decision
|
|
224
|
+
# relinquishes its own. Its still-open normal observation keeps only
|
|
225
|
+
# the guarded detach bridge, never a second unload-blocking lease.
|
|
226
|
+
original.finish_operation! if @hosted_context
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def resolved_locked?
|
|
230
|
+
@application_state == :applied && %i[restored resumed].include?(@continuation_state) &&
|
|
231
|
+
@attempts.quiescent_locked?
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def acknowledge_original_if_quiet
|
|
235
|
+
task = @mutex.synchronize { @request&.result_task if @uncertain && @attempts.snapshot_locked(nil)[:physical] }
|
|
236
|
+
task.finish_operation! if task.respond_to?(:finish_operation!)
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Public capabilities can outlive their job. Keep only the immutable
|
|
240
|
+
# receipt, weak identity and independent canonical execution observation.
|
|
241
|
+
# Called after the enclosing owner delivery has finished using the proposal.
|
|
242
|
+
def retire_resolved
|
|
243
|
+
@mutex.synchronize do
|
|
244
|
+
return unless resolved_locked? && !@attempts.active_snapshot_locked
|
|
245
|
+
@agent = @coordinator = @environment = @registry = @persistence = @committer = nil
|
|
246
|
+
@command = @request = @invocation = @suspended_cancellation = @parent_wait = nil
|
|
247
|
+
if @continuation_state == :restored
|
|
248
|
+
@hosted_context&.release!
|
|
249
|
+
@hosted_context = nil
|
|
250
|
+
end
|
|
251
|
+
@evidence = @intended = @continuation_plan = @terminal_continuation = nil
|
|
252
|
+
@unwritten_before = nil
|
|
253
|
+
@attempts.release_references_locked(nil)
|
|
254
|
+
@original_error = @last_error = nil
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def submit_worker(id, &work)
|
|
259
|
+
@attempts.submit(id, submitter: @environment,
|
|
260
|
+
missing_handle_message: "Agent approval submission returned no task handle", &work)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def watch(id)
|
|
264
|
+
@attempts.watch(id) do |event|
|
|
265
|
+
acknowledge_original_if_quiet if id.nil? && event == :physical
|
|
266
|
+
signal(id)
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def refresh_attempt(id)
|
|
271
|
+
acknowledge_original_if_quiet if @attempts.refresh(id) && id.nil?
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def signal(id)
|
|
275
|
+
registry = @mutex.synchronize { @registry }
|
|
276
|
+
return unless registry
|
|
277
|
+
return if registry.post(Ready.new(operation: self, attempt_id: id))
|
|
278
|
+
delivery_failed(id, Phronomy::RuntimeShutdownError.new("Agent owner rejected approval evidence"))
|
|
279
|
+
rescue => error
|
|
280
|
+
delivery_failed(id, error)
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def delivery_failed(id, error)
|
|
284
|
+
return fail_original(error) unless id
|
|
285
|
+
observers = @mutex.synchronize do
|
|
286
|
+
@attempts.take_observers_locked(id)
|
|
287
|
+
end
|
|
288
|
+
observers.each { |observer| observer.complete(unavailable_result(error)) }
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def delivery_observer(observer)
|
|
292
|
+
Phronomy::TaskResult.deferred(name: "agent-approval-delivery:#{@recovery_id}").tap do |delivery|
|
|
293
|
+
delivery.on_complete do |_value, error|
|
|
294
|
+
observer.complete(unavailable_result(error)) if error
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def deliver_initial
|
|
300
|
+
refresh_attempt(nil)
|
|
301
|
+
slot, uncertain, delivered = @mutex.synchronize { [@attempts.snapshot_locked(nil), @uncertain, @initial_delivered] }
|
|
302
|
+
return if delivered || resolved? || !slot[:logical]
|
|
303
|
+
if slot[:rejection_confirmed] && slot[:physical] && !slot[:started]
|
|
304
|
+
remember_pre
|
|
305
|
+
@coordinator.send(:restore_retained_approval_on_event_loop, self)
|
|
306
|
+
@request.result_task.finish_operation! if @request.result_task.respond_to?(:finish_operation!)
|
|
307
|
+
@request.result_task.fail(slot[:error])
|
|
308
|
+
@mutex.synchronize { @initial_delivered = true }
|
|
309
|
+
return
|
|
310
|
+
end
|
|
311
|
+
if slot[:error] || uncertain
|
|
312
|
+
mark_uncertain_on_owner(slot[:error] || @last_error || Phronomy::Error.new("Approval decision requires recovery"))
|
|
313
|
+
@mutex.synchronize { @initial_delivered = true }
|
|
314
|
+
return
|
|
315
|
+
end
|
|
316
|
+
return unless slot[:physical]
|
|
317
|
+
@mutex.synchronize { @initial_delivered = true }
|
|
318
|
+
remember_post(slot[:result])
|
|
319
|
+
apply_confirmation
|
|
320
|
+
rescue => error
|
|
321
|
+
mark_uncertain_on_owner(error)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def mark_uncertain_on_owner(error)
|
|
325
|
+
failure = fail_original(error)
|
|
326
|
+
@coordinator.send(:approval_recovery_failed_on_event_loop, self, failure)
|
|
327
|
+
failure
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def observe_on_owner(observer)
|
|
331
|
+
return if observer.done?
|
|
332
|
+
refresh_attempt(nil)
|
|
333
|
+
return observer.complete(build_result) if resolved?
|
|
334
|
+
unless @registry.owns_approval_operation?(self)
|
|
335
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("Agent approval recovery owner no longer matches")))
|
|
336
|
+
end
|
|
337
|
+
physical, active = @mutex.synchronize { [@attempts.snapshot_locked(nil)[:physical], @attempts.active_snapshot_locked] }
|
|
338
|
+
unless physical
|
|
339
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("Original approval save has no physical completion proof")))
|
|
340
|
+
end
|
|
341
|
+
return observe_active_attempt(observer, active) if active
|
|
342
|
+
# Hosted and native relationships require current authority for every
|
|
343
|
+
# unfinished continuation. An unwritten decision also requires a fresh
|
|
344
|
+
# exact wait observation; neither path may reuse historical permission.
|
|
345
|
+
fresh_authority = @hosted_context || @mutex.synchronize { !@evidence || @evidence.participation? }
|
|
346
|
+
return observer.complete(apply_confirmation) if !fresh_authority && @mutex.synchronize { !@confirmation.nil? }
|
|
347
|
+
outcome = @mutex.synchronize { @attempts.snapshot_locked(nil)[:result] }
|
|
348
|
+
if outcome && !fresh_authority
|
|
349
|
+
remember_post(outcome)
|
|
350
|
+
return observer.complete(apply_confirmation)
|
|
351
|
+
end
|
|
352
|
+
begin_reconciliation(observer)
|
|
353
|
+
rescue => error
|
|
354
|
+
observer.complete(unavailable_result(error))
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
def observe_active_attempt(observer, active)
|
|
358
|
+
if active[:admission_unknown]
|
|
359
|
+
return observer.complete(unavailable_result(active[:error]))
|
|
360
|
+
end
|
|
361
|
+
if active[:watch_error]
|
|
362
|
+
refresh_attempt(active[:id])
|
|
363
|
+
active = @mutex.synchronize { @attempts.snapshot_locked(active[:id]) }
|
|
364
|
+
observable = active && ((active[:logical] && active[:physical]) || active[:physical_watch])
|
|
365
|
+
return observer.complete(unavailable_result(active&.fetch(:watch_error))) unless observable
|
|
366
|
+
end
|
|
367
|
+
@mutex.synchronize { @attempts.join_locked(active[:id], observer: observer) }
|
|
368
|
+
finish_attempt(active[:id])
|
|
369
|
+
nil
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
def begin_reconciliation(observer)
|
|
373
|
+
evidence = @mutex.synchronize { @evidence }
|
|
374
|
+
if !evidence && @mutex.synchronize { @write_started }
|
|
375
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("Approval decision has no complete captured proposal")))
|
|
376
|
+
end
|
|
377
|
+
id = Object.new.freeze
|
|
378
|
+
@mutex.synchronize do
|
|
379
|
+
@attempts.reserve_locked(id, observer: observer)
|
|
380
|
+
end
|
|
381
|
+
task = submit_worker(id) do
|
|
382
|
+
if evidence
|
|
383
|
+
evidence.reconcile(persistence: @persistence, original_error: @original_error)
|
|
384
|
+
else
|
|
385
|
+
@committer.observe_unwritten(@command, parent_evidence: @hosted_context&.parent_evidence,
|
|
386
|
+
participant: @invocation.config[:phronomy_execution_participant], original_error: @original_error)
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
watch(id)
|
|
390
|
+
rescue => error
|
|
391
|
+
if task || (id && @mutex.synchronize { @attempts.snapshot_locked(id)&.fetch(:admission_unknown) })
|
|
392
|
+
delivery_failed(id, error)
|
|
393
|
+
else
|
|
394
|
+
observers = @attempts.retire(defined?(id) && id)
|
|
395
|
+
(observers.empty? ? [observer] : observers).each { |item| item.complete(unavailable_result(error)) }
|
|
396
|
+
end
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
def finish_attempt(id)
|
|
400
|
+
refresh_attempt(id)
|
|
401
|
+
slot = @mutex.synchronize { @attempts.snapshot_locked(id) }
|
|
402
|
+
return unless slot && slot[:logical] && slot[:physical]
|
|
403
|
+
if slot[:error] && !slot[:error].is_a?(StandardError)
|
|
404
|
+
@attempts.retire(id).each { |observer| observer.fail(slot[:error]) }
|
|
405
|
+
return
|
|
406
|
+
end
|
|
407
|
+
result = slot[:result]
|
|
408
|
+
observation = if result&.disposition == :committed
|
|
409
|
+
remember_post(@mutex.synchronize { @intended })
|
|
410
|
+
result.continuation_error ? build_result(error: diagnostic(result.continuation_error)) : apply_confirmation
|
|
411
|
+
elsif result&.disposition == :not_committed && @mutex.synchronize { @confirmation != :confirmed_post }
|
|
412
|
+
@mutex.synchronize { @unwritten_before = result.evidence.before } unless @evidence
|
|
413
|
+
remember_pre
|
|
414
|
+
result.continuation_error ? build_result(error: diagnostic(result.continuation_error)) : apply_confirmation
|
|
415
|
+
else
|
|
416
|
+
error = slot[:error] || result&.read_error
|
|
417
|
+
if !error && @mutex.synchronize { @confirmation == :confirmed_post }
|
|
418
|
+
error = Phronomy::Persistence::StateConflictError.new("Saved approval authority no longer matches its captured evidence")
|
|
419
|
+
end
|
|
420
|
+
build_result(outcome: error ? :unavailable : :unresolved, error: error && diagnostic(error))
|
|
421
|
+
end
|
|
422
|
+
@attempts.retire(id).each_with_index do |observer, index|
|
|
423
|
+
observer.complete(index.zero? ? observation : build_result(outcome: observation.outcome, error: observation.error))
|
|
424
|
+
end
|
|
425
|
+
rescue => error
|
|
426
|
+
@attempts.retire(id).each { |observer| observer.complete(unavailable_result(error)) }
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
def remember_pre
|
|
430
|
+
@mutex.synchronize do
|
|
431
|
+
@confirmation = :confirmed_pre
|
|
432
|
+
@revision = @command.expected_execution_revision
|
|
433
|
+
end
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
def remember_post(outcome)
|
|
437
|
+
raise Phronomy::Error, "Approval save returned no semantic outcome" unless outcome
|
|
438
|
+
@mutex.synchronize do
|
|
439
|
+
@confirmation = :confirmed_post
|
|
440
|
+
@intended = outcome
|
|
441
|
+
@revision = outcome.execution.execution_revision
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
def apply_confirmation
|
|
446
|
+
confirmation, outcome = @mutex.synchronize { [@confirmation, @intended] }
|
|
447
|
+
return build_result if resolved?
|
|
448
|
+
if confirmation == :confirmed_pre
|
|
449
|
+
@coordinator.send(:restore_retained_approval_on_event_loop, self)
|
|
450
|
+
else
|
|
451
|
+
@coordinator.send(:apply_retained_approval_on_event_loop, self, outcome)
|
|
452
|
+
end
|
|
453
|
+
build_result
|
|
454
|
+
rescue => error
|
|
455
|
+
mark_uncertain_on_owner(error)
|
|
456
|
+
build_result(error: diagnostic(error))
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
def unavailable_result(error)
|
|
460
|
+
build_result(outcome: :unavailable, error: diagnostic(error))
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
def build_result(outcome: nil, error: nil)
|
|
464
|
+
confirmation, application, continuation, revision, physical = @mutex.synchronize do
|
|
465
|
+
[@confirmation, @application_state, @continuation_state, @revision,
|
|
466
|
+
@attempts.quiescent_locked?]
|
|
467
|
+
end
|
|
468
|
+
observer = (continuation == :resumed) ? execution_observer : nil
|
|
469
|
+
hosted = observer && @hosted_context&.build_invocation(observation: execution_observer, detach_task: @canonical_task)
|
|
470
|
+
ApprovalRecoveryResult.new(recovery: recovery, outcome: confirmation || outcome || :unavailable,
|
|
471
|
+
application_state: application, continuation_state: continuation, revision: revision,
|
|
472
|
+
physical_complete: physical, execution_task: observer, hosted_invocation: hosted,
|
|
473
|
+
error: error)
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
def execution_observer
|
|
477
|
+
Phronomy::TaskResult.deferred(name: "approval-execution-observer:#{recovery_id}").tap do |observer|
|
|
478
|
+
@canonical_task.on_complete do |value, error|
|
|
479
|
+
error ? observer.fail(error) : observer.complete(value)
|
|
480
|
+
end
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
def diagnostic(error)
|
|
485
|
+
@mutex.synchronize { diagnostic_locked(error) }
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
def diagnostic_locked(error)
|
|
489
|
+
return error if error.is_a?(ApprovalRecoveryError) && error.recovery.equal?(@recovery)
|
|
490
|
+
ApprovalRecoveryError.new(recovery: @recovery,
|
|
491
|
+
error: error || Phronomy::Error.new("Agent approval outcome requires reconciliation"),
|
|
492
|
+
original_error: @original_error || error)
|
|
493
|
+
end
|
|
494
|
+
end
|
|
495
|
+
end
|
|
496
|
+
end
|