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,513 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "weakref"
|
|
5
|
+
|
|
6
|
+
module Phronomy
|
|
7
|
+
module Agent
|
|
8
|
+
# Owns one original initial admission through its durable save stages. The
|
|
9
|
+
# mutex protects captured stage facts and shared attempt bookkeeping; only
|
|
10
|
+
# EventLoop changes Runtime admission or installs the execution.
|
|
11
|
+
# @api private
|
|
12
|
+
class InitialPreparationRecoveryOperation
|
|
13
|
+
Observe = Data.define(:operation, :observer, :kind)
|
|
14
|
+
Ready = Data.define(:operation, :attempt_id)
|
|
15
|
+
RecoveryOutcome = Data.define(:proof, :result, :cancelled_admission)
|
|
16
|
+
private_constant :RecoveryOutcome
|
|
17
|
+
|
|
18
|
+
attr_reader :agent, :coordinator, :environment, :agent_id, :execution_id,
|
|
19
|
+
:recovery_id, :recovery, :command, :request, :controls, :authority
|
|
20
|
+
attr_accessor :filtered_input, :initial_session, :session_registration_attempted
|
|
21
|
+
|
|
22
|
+
def initialize(agent:, coordinator:, environment:, command:, request:)
|
|
23
|
+
@agent, @coordinator, @environment, @command, @request = agent, coordinator, environment, command, request
|
|
24
|
+
@agent_reference = WeakRef.new(agent)
|
|
25
|
+
@registry, @persistence = environment.registry, agent.persistence
|
|
26
|
+
@agent_id, @execution_id = agent.agent_id, request.config.fetch(:phronomy_reserved_execution_id)
|
|
27
|
+
@controls = request.controls
|
|
28
|
+
@recovery_id = SecureRandom.uuid.freeze
|
|
29
|
+
@recovery = InitialPreparationRecovery.send(:new, self)
|
|
30
|
+
@mutex = Mutex.new
|
|
31
|
+
@attempts = Phronomy::RetainedAttempts.new(mutex: @mutex)
|
|
32
|
+
@preparer = InitialPreparation.new(agent: agent, persistence: @persistence, owner: self)
|
|
33
|
+
@stage, @application_state, @continuation_state = :admission, :pending, :pending
|
|
34
|
+
@uncertain = @save_entered = @preparation_started = @initial_delivered = false
|
|
35
|
+
@checkpoint = @confirmation = @original_error = @last_error = nil
|
|
36
|
+
@original_waiters = [request.result_task]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def belongs_to?(candidate)
|
|
40
|
+
@agent_reference.__getobj__.equal?(candidate)
|
|
41
|
+
rescue WeakRef::RefError
|
|
42
|
+
false
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def pending? = @mutex.synchronize { @uncertain && !resolved_locked? }
|
|
46
|
+
def resolved? = @mutex.synchronize { resolved_locked? }
|
|
47
|
+
def save_entered? = @mutex.synchronize { @save_entered }
|
|
48
|
+
def save_entered! = @mutex.synchronize { @save_entered = true }
|
|
49
|
+
def save_returned! = @mutex.synchronize { @save_entered = false }
|
|
50
|
+
def capture_supported? = !@admission_participant || @admission_participant.respond_to?(:initial_recovery_authority, true)
|
|
51
|
+
|
|
52
|
+
def admission_started!(admission, participant)
|
|
53
|
+
@admission_participant = participant
|
|
54
|
+
# An arbitrary callback may own writes outside the Agent proposal.
|
|
55
|
+
save_entered! if participant && !capture_supported?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def capture_authority(admission, scope)
|
|
59
|
+
@authority ||= @admission_participant&.__send__(:initial_recovery_authority,
|
|
60
|
+
admission, scope: scope, persistence: @persistence)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def admitted_execution
|
|
64
|
+
@mutex.synchronize { @admitted_execution }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def preparation_started!
|
|
68
|
+
@mutex.synchronize do
|
|
69
|
+
raise Phronomy::Error, "Initial preparation has already started" if @preparation_started
|
|
70
|
+
@preparation_started = true
|
|
71
|
+
@save_entered = false
|
|
72
|
+
end
|
|
73
|
+
controls.check_start!
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def begin_stage(stage)
|
|
77
|
+
@mutex.synchronize do
|
|
78
|
+
@stage, @checkpoint, @confirmation, @save_entered = stage, nil, nil, false
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Worker records complete values before any mutable save. No callback or
|
|
83
|
+
# storage operation executes while this owner mutex is held.
|
|
84
|
+
def capture(evidence, **values)
|
|
85
|
+
@mutex.synchronize do
|
|
86
|
+
@stage = evidence.stage
|
|
87
|
+
@checkpoint = values.merge(evidence: evidence).freeze
|
|
88
|
+
@admitted_execution = evidence.execution if evidence.stage == :admission
|
|
89
|
+
@confirmation = nil
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def start
|
|
94
|
+
@registry.retain_initial_preparation(self)
|
|
95
|
+
task = submit(nil) { @preparer.prepare(command) }
|
|
96
|
+
request.result_task.track(task)
|
|
97
|
+
watch(nil)
|
|
98
|
+
self
|
|
99
|
+
rescue
|
|
100
|
+
signal(nil)
|
|
101
|
+
self
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def observe(kind)
|
|
105
|
+
observer = Phronomy::TaskResult.deferred(name: "initial-preparation-#{kind}:#{recovery_id}")
|
|
106
|
+
@attempts.register(observer)
|
|
107
|
+
if resolved?
|
|
108
|
+
observer.complete(build_result)
|
|
109
|
+
elsif !@registry.post(Observe.new(operation: self, observer: observer, kind: kind),
|
|
110
|
+
completion: delivery_observer(observer))
|
|
111
|
+
observer.complete(unavailable(Phronomy::RuntimeShutdownError.new("Original initial preparation owner is unavailable")))
|
|
112
|
+
end
|
|
113
|
+
observer
|
|
114
|
+
rescue => error
|
|
115
|
+
observer&.complete(unavailable(error))
|
|
116
|
+
observer || raise
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def deliver(message)
|
|
120
|
+
case message
|
|
121
|
+
when Observe then observe_on_owner(message.observer, message.kind)
|
|
122
|
+
when Ready then message.attempt_id ? finish_attempt(message.attempt_id) : deliver_initial
|
|
123
|
+
end
|
|
124
|
+
ensure
|
|
125
|
+
retire_resolved
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def retain_original_waiters(waiters)
|
|
129
|
+
@mutex.synchronize { @original_waiters |= waiters }
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def shutdown(error)
|
|
133
|
+
failure = fail_original(error)
|
|
134
|
+
@attempts.observers.each { |observer| observer.complete(unavailable(error)) }
|
|
135
|
+
failure
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def application_request
|
|
139
|
+
return request unless @mutex.synchronize { @uncertain } && request.result_task.done?
|
|
140
|
+
@continuation_request ||= begin
|
|
141
|
+
result_task = OperationResult.new(agent: @agent, name: "initial-preparation-continuation:#{execution_id}")
|
|
142
|
+
controls.bind(result_task, track_completion: false)
|
|
143
|
+
request.with(result_task: result_task, on_event: nil, approval_listener: nil)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def original_delivery?
|
|
148
|
+
@mutex.synchronize { !@uncertain && @attempts.quiescent_locked? }
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# This ordinary-delivery path has positive local non-admission evidence.
|
|
152
|
+
# Preserve the existing terminal protocol, including participant behavior.
|
|
153
|
+
def handover_setup_failure(prepared, error)
|
|
154
|
+
@terminal_handover_pending = true
|
|
155
|
+
custody = @coordinator.send(:begin_terminal_after_start_failure_on_event_loop,
|
|
156
|
+
request: request, prepared: prepared, error: error)
|
|
157
|
+
if custody&.accepted? && @registry.transfer_initial_terminal_custody(self, custody)
|
|
158
|
+
controls = @controls
|
|
159
|
+
request.result_task.on_physical_complete { controls.close }
|
|
160
|
+
@mutex.synchronize { @terminal_transferred = true }
|
|
161
|
+
else
|
|
162
|
+
@mutex.synchronize do
|
|
163
|
+
@uncertain = true
|
|
164
|
+
@original_error ||= custody&.error || error
|
|
165
|
+
@last_error = custody&.error || error
|
|
166
|
+
end
|
|
167
|
+
# The legacy terminal path may already have reported its own error.
|
|
168
|
+
fail_original(@last_error) unless request.result_task.done?
|
|
169
|
+
end
|
|
170
|
+
nil
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def resolution_permitted?
|
|
174
|
+
@mutex.synchronize { @attempts.quiescent_locked? && @resolution_state }
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Called only in the registry's matching-token removal transaction.
|
|
178
|
+
def resolution_released!
|
|
179
|
+
@mutex.synchronize do
|
|
180
|
+
@application_state, @continuation_state = :applied, @resolution_state
|
|
181
|
+
@resolution_state = nil
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
private
|
|
186
|
+
|
|
187
|
+
def resolved_locked?
|
|
188
|
+
@application_state == :applied && %i[resumed stopped].include?(@continuation_state) && @attempts.quiescent_locked?
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def submit(id, &work)
|
|
192
|
+
@attempts.submit(id, submitter: @environment,
|
|
193
|
+
missing_handle_message: "Initial preparation submission returned no task handle", &work)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def watch(id)
|
|
197
|
+
@attempts.watch(id) { |_event| signal(id) }
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def signal(id)
|
|
201
|
+
registry = @mutex.synchronize { @registry }
|
|
202
|
+
return unless registry
|
|
203
|
+
return if registry.post(Ready.new(operation: self, attempt_id: id))
|
|
204
|
+
delivery_failed(id, Phronomy::RuntimeShutdownError.new("Initial preparation evidence notification rejected"))
|
|
205
|
+
rescue => error
|
|
206
|
+
delivery_failed(id, error)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def delivery_failed(id, error)
|
|
210
|
+
return fail_original(error) unless id
|
|
211
|
+
observers = @mutex.synchronize { @attempts.take_observers_locked(id) }
|
|
212
|
+
observers.each { |observer| observer.complete(unavailable(error)) }
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def delivery_observer(observer)
|
|
216
|
+
Phronomy::TaskResult.deferred(name: "initial-preparation-delivery:#{recovery_id}").tap do |task|
|
|
217
|
+
task.on_complete { |_result, error| observer.complete(unavailable(error)) if error }
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def fail_original(error)
|
|
222
|
+
waiters, failure = @mutex.synchronize do
|
|
223
|
+
@uncertain = true
|
|
224
|
+
@original_error ||= error
|
|
225
|
+
@last_error = error
|
|
226
|
+
[@original_waiters.dup, diagnostic_locked(error)]
|
|
227
|
+
end
|
|
228
|
+
waiters.each do |task|
|
|
229
|
+
task.respond_to?(:settle) ? task.settle(error: failure) : task.fail(failure)
|
|
230
|
+
end
|
|
231
|
+
failure
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def deliver_initial
|
|
235
|
+
@attempts.refresh(nil)
|
|
236
|
+
slot, delivered = @mutex.synchronize { [@attempts.snapshot_locked(nil), @initial_delivered] }
|
|
237
|
+
return if delivered || resolved? || !slot[:logical]
|
|
238
|
+
# Once uncertainty was exposed, only an explicit observation may grant
|
|
239
|
+
# continuation. A delayed original notification cannot race its lane.
|
|
240
|
+
return if @mutex.synchronize { @uncertain }
|
|
241
|
+
if slot[:error]
|
|
242
|
+
if slot[:physical] && slot[:rejection_confirmed] && !slot[:started]
|
|
243
|
+
@mutex.synchronize { @confirmation = :not_prepared }
|
|
244
|
+
resolve_without_admission(slot[:error])
|
|
245
|
+
else
|
|
246
|
+
fail_original(slot[:error])
|
|
247
|
+
@registry.mark_agent_admission_recovery_required(agent_id, owner_token: request.admission_token)
|
|
248
|
+
end
|
|
249
|
+
elsif slot[:physical]
|
|
250
|
+
apply_result(slot[:result])
|
|
251
|
+
else
|
|
252
|
+
return
|
|
253
|
+
end
|
|
254
|
+
@mutex.synchronize { @initial_delivered = true }
|
|
255
|
+
rescue => error
|
|
256
|
+
fail_original(error)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def observe_on_owner(observer, kind)
|
|
260
|
+
return if observer.done?
|
|
261
|
+
@attempts.refresh(nil)
|
|
262
|
+
return observer.complete(build_result) if resolved?
|
|
263
|
+
unless @registry.owns_initial_preparation?(self)
|
|
264
|
+
return observer.complete(unavailable(Phronomy::Error.new("Initial preparation owner no longer matches")))
|
|
265
|
+
end
|
|
266
|
+
slot, active = @mutex.synchronize { [@attempts.snapshot_locked(nil), @attempts.active_snapshot_locked] }
|
|
267
|
+
unless slot[:physical]
|
|
268
|
+
return observer.complete(unavailable(Phronomy::Error.new("Original initial preparation has no physical completion proof")))
|
|
269
|
+
end
|
|
270
|
+
if active
|
|
271
|
+
return observer.complete(unavailable(active[:error])) if active[:admission_unknown]
|
|
272
|
+
if active[:kind] != kind
|
|
273
|
+
return observer.complete(unavailable(Phronomy::Error.new("Another initial recovery operation is physically active")))
|
|
274
|
+
end
|
|
275
|
+
@mutex.synchronize { @attempts.join_locked(active[:id], observer: observer) }
|
|
276
|
+
return finish_attempt(active[:id])
|
|
277
|
+
end
|
|
278
|
+
if slot[:rejection_confirmed] && !slot[:started] && slot[:error]
|
|
279
|
+
@mutex.synchronize { @confirmation = :not_prepared }
|
|
280
|
+
resolve_without_admission(slot[:error])
|
|
281
|
+
return observer.complete(build_result)
|
|
282
|
+
end
|
|
283
|
+
if slot[:result]&.admission_outcome == :not_established && !@mutex.synchronize { @checkpoint }
|
|
284
|
+
apply_result(slot[:result])
|
|
285
|
+
return observer.complete(build_result)
|
|
286
|
+
end
|
|
287
|
+
if initial_session && session_registration_attempted &&
|
|
288
|
+
(!environment.respond_to?(:session_admitted?) || environment.session_admitted?(initial_session.id))
|
|
289
|
+
return observer.complete(unavailable(Phronomy::Error.new("Initial session has no positive non-admission proof for recovery")))
|
|
290
|
+
end
|
|
291
|
+
if @terminal_handover_pending
|
|
292
|
+
return observer.complete(unavailable(Phronomy::Error.new("Initial setup is awaiting terminal ownership; activation cannot be repeated")))
|
|
293
|
+
end
|
|
294
|
+
begin_attempt(observer, kind)
|
|
295
|
+
rescue => error
|
|
296
|
+
observer.complete(unavailable(error))
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def begin_attempt(observer, kind)
|
|
300
|
+
checkpoint = @mutex.synchronize { @checkpoint }
|
|
301
|
+
unless checkpoint
|
|
302
|
+
return observer.complete(unavailable(Phronomy::Error.new("Initial preparation has no complete captured proposal")))
|
|
303
|
+
end
|
|
304
|
+
id = Object.new.freeze
|
|
305
|
+
@mutex.synchronize do
|
|
306
|
+
@confirmation = nil unless @confirmation == :confirmed_post
|
|
307
|
+
@attempts.reserve_locked(id, kind: kind, observer: observer)
|
|
308
|
+
end
|
|
309
|
+
submit(id) { recover_checkpoint(checkpoint, kind) }
|
|
310
|
+
watch(id)
|
|
311
|
+
rescue => error
|
|
312
|
+
slot = @mutex.synchronize { @attempts.snapshot_locked(id) } if id
|
|
313
|
+
if slot && (slot[:admission_unknown] || !slot[:physical])
|
|
314
|
+
delivery_failed(id, error)
|
|
315
|
+
else
|
|
316
|
+
@attempts.retire(id).each { |item| item.complete(unavailable(error)) }
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def recover_checkpoint(checkpoint, kind)
|
|
321
|
+
cancelled = captured_control_error unless checkpoint.fetch(:evidence).stage == :failure
|
|
322
|
+
proof = checkpoint.fetch(:evidence).public_send(cancelled ? :reconcile : kind,
|
|
323
|
+
persistence: @persistence, original_error: @original_error)
|
|
324
|
+
result = nil
|
|
325
|
+
cancelled_admission = nil
|
|
326
|
+
if proof.disposition == :not_committed && cancelled
|
|
327
|
+
if checkpoint.fetch(:evidence).stage == :admission
|
|
328
|
+
cancelled_admission = cancelled
|
|
329
|
+
else
|
|
330
|
+
result = @preparer.stop(checkpoint, cancelled, before: true)
|
|
331
|
+
end
|
|
332
|
+
elsif proof.disposition == :committed
|
|
333
|
+
error = cancelled || proof.continuation_error
|
|
334
|
+
result = if error.is_a?(Phronomy::CancellationError) || cancelled
|
|
335
|
+
@preparer.stop(checkpoint, error)
|
|
336
|
+
elsif !error
|
|
337
|
+
@preparer.continue(checkpoint)
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
RecoveryOutcome.new(proof: proof, result: result, cancelled_admission: cancelled_admission)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def captured_control_error
|
|
344
|
+
controls.check_start!
|
|
345
|
+
controls.token&.raise_if_cancelled!
|
|
346
|
+
nil
|
|
347
|
+
rescue => error
|
|
348
|
+
error
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def finish_attempt(id)
|
|
352
|
+
@attempts.refresh(id)
|
|
353
|
+
slot = @mutex.synchronize { @attempts.snapshot_locked(id) }
|
|
354
|
+
return unless slot && slot[:logical] && slot[:physical]
|
|
355
|
+
outcome = slot[:result]
|
|
356
|
+
receipt = if slot[:error]
|
|
357
|
+
@mutex.synchronize { @last_error = slot[:error] }
|
|
358
|
+
unavailable(slot[:error])
|
|
359
|
+
elsif outcome.cancelled_admission
|
|
360
|
+
@mutex.synchronize { @confirmation = :confirmed_pre }
|
|
361
|
+
resolve_without_admission(outcome.cancelled_admission)
|
|
362
|
+
build_result
|
|
363
|
+
elsif outcome.result
|
|
364
|
+
apply_result(outcome.result)
|
|
365
|
+
build_result
|
|
366
|
+
elsif outcome.proof.disposition == :committed
|
|
367
|
+
@mutex.synchronize { @confirmation = :confirmed_post }
|
|
368
|
+
build_result(error: diagnostic(outcome.proof.continuation_error))
|
|
369
|
+
elsif outcome.proof.disposition == :not_committed
|
|
370
|
+
@mutex.synchronize { @confirmation = :confirmed_pre }
|
|
371
|
+
build_result(error: outcome.proof.read_error && diagnostic(outcome.proof.read_error))
|
|
372
|
+
else
|
|
373
|
+
unavailable(outcome.proof.read_error)
|
|
374
|
+
end
|
|
375
|
+
@attempts.retire(id).each_with_index do |observer, index|
|
|
376
|
+
observer.complete(index.zero? ? receipt : build_result(error: receipt.error))
|
|
377
|
+
end
|
|
378
|
+
rescue => error
|
|
379
|
+
@attempts.retire(id).each { |observer| observer.complete(unavailable(error)) }
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
def apply_result(result)
|
|
383
|
+
unless result
|
|
384
|
+
raise Phronomy::Error, "Initial preparation returned no result"
|
|
385
|
+
end
|
|
386
|
+
case result.admission_outcome
|
|
387
|
+
when :not_established
|
|
388
|
+
@mutex.synchronize { @confirmation = :not_prepared }
|
|
389
|
+
resolve_without_admission(result.error)
|
|
390
|
+
when :active, :terminal
|
|
391
|
+
@mutex.synchronize { @confirmation = :confirmed_post }
|
|
392
|
+
if result.admission_outcome == :terminal && @mutex.synchronize { @checkpoint&.fetch(:evidence)&.stage == :failure }
|
|
393
|
+
capture_terminal_observation(result)
|
|
394
|
+
end
|
|
395
|
+
@coordinator.send(:apply_retained_initial_preparation_on_event_loop, self, result)
|
|
396
|
+
return if @terminal_handover_pending
|
|
397
|
+
state = (result.admission_outcome == :active) ? :resumed : :stopped
|
|
398
|
+
resolve(state)
|
|
399
|
+
(state == :resumed) ? controls.track(application_request.result_task) : controls.close
|
|
400
|
+
else
|
|
401
|
+
raise(result.error || Phronomy::Error.new("Initial preparation requires current durable recovery"))
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
def resolve_without_admission(error)
|
|
406
|
+
@execution_observation = Phronomy::TaskResult.deferred(name: "initial-unadmitted:#{recovery_id}")
|
|
407
|
+
@execution_observation.fail(error)
|
|
408
|
+
resolve(:stopped)
|
|
409
|
+
controls.close
|
|
410
|
+
@coordinator.send(:deliver_start_failure_on_event_loop, request, error) unless request.result_task.done?
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
def resolve(state)
|
|
414
|
+
@mutex.synchronize { @resolution_state = state }
|
|
415
|
+
unless @registry.resolve_initial_preparation(self, state: state)
|
|
416
|
+
raise Phronomy::Error, "Initial preparation token changed before handover"
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# A saved terminal failure is an exact execution value, not the original
|
|
421
|
+
# failed observation. Its diagnostic bytes were captured before the save.
|
|
422
|
+
def capture_terminal_observation(result)
|
|
423
|
+
evidence = @mutex.synchronize { @checkpoint.fetch(:evidence) }
|
|
424
|
+
execution = result.execution
|
|
425
|
+
diagnostic = evidence.content_writes.find { |write| write.reference == execution.error_ref }
|
|
426
|
+
value = {execution_id: execution_id, agent_id: agent_id,
|
|
427
|
+
status: execution.status, phase: execution.phase,
|
|
428
|
+
result_ref: execution.result_ref, error_ref: execution.error_ref,
|
|
429
|
+
result: nil, raw_output: nil, output: nil,
|
|
430
|
+
error: diagnostic && Phronomy::CanonicalJSON.load(diagnostic.bytes)}
|
|
431
|
+
@execution_observation = Phronomy::TaskResult.deferred(name: "initial-execution:#{recovery_id}")
|
|
432
|
+
@execution_observation.complete(Phronomy::Values::Immutable.copy(value))
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def execution_observer
|
|
436
|
+
canonical, connect = @mutex.synchronize do
|
|
437
|
+
unless @execution_observation
|
|
438
|
+
@execution_observation = Phronomy::TaskResult.deferred(name: "initial-execution:#{recovery_id}")
|
|
439
|
+
connect = true
|
|
440
|
+
end
|
|
441
|
+
[@execution_observation, connect]
|
|
442
|
+
end
|
|
443
|
+
if connect
|
|
444
|
+
task = (@continuation_request || request).result_task
|
|
445
|
+
persistence, environment, id = @persistence, @environment, execution_id
|
|
446
|
+
task.on_complete do |_value, failure|
|
|
447
|
+
read = environment.submit(on_full: :raise) do
|
|
448
|
+
saved = persistence.result(id)
|
|
449
|
+
unless %i[completed failed cancelled blocked rejected handed_off].include?(saved[:status])
|
|
450
|
+
raise(failure || Phronomy::ExecutionRehydrationRequiredError.new("Execution #{id} needs its next explicit continuation"))
|
|
451
|
+
end
|
|
452
|
+
saved.merge(output: saved[:result]).freeze
|
|
453
|
+
end
|
|
454
|
+
read.on_complete { |value, error| error ? canonical.fail(error) : canonical.complete(value) }
|
|
455
|
+
rescue => error
|
|
456
|
+
canonical.fail(error)
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
Phronomy::TaskResult.deferred(name: "initial-execution-observer:#{recovery_id}").tap do |observer|
|
|
460
|
+
canonical.on_complete { |value, error| error ? observer.fail(error) : observer.complete(value) }
|
|
461
|
+
end
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def retire_resolved
|
|
465
|
+
ready, needs_observation = @mutex.synchronize do
|
|
466
|
+
[(resolved_locked? || @terminal_transferred) && !@attempts.active_snapshot_locked,
|
|
467
|
+
@uncertain && @continuation_state == :resumed && !@execution_observation]
|
|
468
|
+
end
|
|
469
|
+
return unless ready
|
|
470
|
+
execution_observer if needs_observation
|
|
471
|
+
@mutex.synchronize do
|
|
472
|
+
return if @attempts.active_snapshot_locked || (!resolved_locked? && !@terminal_transferred)
|
|
473
|
+
@final_revision ||= @checkpoint&.fetch(:evidence)&.execution&.execution_revision
|
|
474
|
+
@agent = @coordinator = @environment = @registry = @persistence = @preparer = nil
|
|
475
|
+
@command = @request = @continuation_request = @checkpoint = @controls = @authority = @admission_participant = nil
|
|
476
|
+
@admitted_execution = @filtered_input = @initial_session = nil
|
|
477
|
+
@attempts.release_references_locked(nil)
|
|
478
|
+
@original_error = @last_error = nil
|
|
479
|
+
@original_waiters = []
|
|
480
|
+
end
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
def unavailable(error)
|
|
484
|
+
build_result(outcome: :unavailable, error: diagnostic(error || Phronomy::Error.new("Initial preparation remains unresolved")))
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
def diagnostic(error)
|
|
488
|
+
@mutex.synchronize { diagnostic_locked(error) }
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
def diagnostic_locked(error)
|
|
492
|
+
return error if error.is_a?(InitialPreparationRecoveryError) && error.recovery.equal?(recovery)
|
|
493
|
+
InitialPreparationRecoveryError.new(recovery: recovery, error: error, original_error: @original_error || error)
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
def build_result(outcome: nil, error: nil)
|
|
497
|
+
stage, confirmation, application, continuation, physical, checkpoint = @mutex.synchronize do
|
|
498
|
+
[@stage, @confirmation, @application_state, @continuation_state,
|
|
499
|
+
@attempts.quiescent_locked?, @checkpoint]
|
|
500
|
+
end
|
|
501
|
+
revision = if %i[confirmed_pre confirmed_post].include?(confirmation)
|
|
502
|
+
evidence = checkpoint&.fetch(:evidence)
|
|
503
|
+
(confirmation == :confirmed_post) ? (evidence&.execution&.execution_revision || @final_revision) : evidence&.before&.fetch(:execution)&.fetch("execution_revision", 0) || 0
|
|
504
|
+
end
|
|
505
|
+
task = execution_observer if %i[resumed stopped].include?(continuation)
|
|
506
|
+
InitialPreparationRecoveryResult.new(recovery: recovery, stage: stage,
|
|
507
|
+
outcome: confirmation || outcome || :unavailable, application_state: application,
|
|
508
|
+
continuation_state: continuation, physical_complete: physical,
|
|
509
|
+
revision: revision, execution_task: task, error: error)
|
|
510
|
+
end
|
|
511
|
+
end
|
|
512
|
+
end
|
|
513
|
+
end
|
|
@@ -68,6 +68,9 @@ module Phronomy
|
|
|
68
68
|
resume: [
|
|
69
69
|
{from: :suspended, to: :waiting_for_tools, guard: nil}
|
|
70
70
|
],
|
|
71
|
+
approval_resume_cancelled: [
|
|
72
|
+
{from: :waiting_for_tools, to: :failed, guard: nil}
|
|
73
|
+
],
|
|
71
74
|
application_callback_failed: [
|
|
72
75
|
{from: :filtering_input, to: :failed, guard: nil},
|
|
73
76
|
{from: :building_context, to: :failed, guard: nil},
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phronomy
|
|
4
|
+
module Agent
|
|
5
|
+
# Retains a validated native terminal subject across guarded storage reads.
|
|
6
|
+
# The retained operation serializes all access on its existing worker lane.
|
|
7
|
+
# @api private
|
|
8
|
+
class OutcomePlanning
|
|
9
|
+
Subject = Data.define(:agent_id, :execution_id, :extension, :cancel_requested, :recovery_required, :finishing) do
|
|
10
|
+
def kind = :terminal
|
|
11
|
+
def operations = []
|
|
12
|
+
def finishing? = finishing
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(subject, root:, current:, store:, participant:, prepare_outcome:, on_preparation:, on_partial:, read_facts:, select_plan:, approval_selection: nil)
|
|
16
|
+
@subject, @root, @current, @store = subject, root, current, store
|
|
17
|
+
@participant, @prepare_outcome, @on_preparation = participant, prepare_outcome, on_preparation
|
|
18
|
+
@read_facts, @select_plan = read_facts, select_plan
|
|
19
|
+
@on_partial, @approval_selection = on_partial, approval_selection
|
|
20
|
+
@decision_started = false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def reconcile(persistence:)
|
|
24
|
+
require_store!(persistence)
|
|
25
|
+
@store.coordinator.atomic do |scope|
|
|
26
|
+
with_guarded_source(scope, recovering: true) do
|
|
27
|
+
# A read fault precedes all writes. Positive source observations
|
|
28
|
+
# prove this attempt did not commit; they grant no right to encode.
|
|
29
|
+
Phronomy::Persistence::SaveOutcome.new(disposition: :not_committed, original_error: nil, read_error: nil)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def materialize(persistence:)
|
|
35
|
+
require_store!(persistence)
|
|
36
|
+
@store.coordinator.atomic { |scope| materialize_in(scope, recovering: true) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def materialize_in(scope, recovering: false)
|
|
40
|
+
with_guarded_source(scope, recovering: recovering) do |records, before, subject, facts|
|
|
41
|
+
# A fresh transaction may discard incomplete reads. Once selection
|
|
42
|
+
# starts, even an exception or a nonlocal exit cannot reopen it.
|
|
43
|
+
@decision_started = true
|
|
44
|
+
prepare_outcome = @prepare_outcome
|
|
45
|
+
select_approval = if @approval_selection
|
|
46
|
+
lambda do |observed|
|
|
47
|
+
subject, prepare_outcome = @approval_selection.call(observed)
|
|
48
|
+
subject
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
state, pending, state_data, participation = @select_plan.call(subject, scope, facts, before.fetch(1), &select_approval)
|
|
52
|
+
if state && (!subject.extension || state.binding_key != subject.extension.binding_key || state.binding_version != subject.extension.binding_version)
|
|
53
|
+
raise Phronomy::ConfigurationError, "Execution extension identity cannot change"
|
|
54
|
+
end
|
|
55
|
+
if pending && !(subject.cancel_requested || subject.recovery_required)
|
|
56
|
+
raise ArgumentError, "pending work requires cancellation or recovery"
|
|
57
|
+
end
|
|
58
|
+
build_preparation(records, before, state, pending, state_data, participation, subject.cancel_requested, prepare_outcome: prepare_outcome)
|
|
59
|
+
end
|
|
60
|
+
rescue Exception => error # rubocop:disable Lint/RescueException
|
|
61
|
+
@decision_error ||= error if @decision_started
|
|
62
|
+
raise
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def require_store!(persistence)
|
|
68
|
+
return if persistence.equal?(@store)
|
|
69
|
+
raise Phronomy::Persistence::StateConflictError, "Agent outcome planning belongs to another Store"
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def with_guarded_source(scope, recovering:)
|
|
73
|
+
raise @decision_error if @decision_error
|
|
74
|
+
raise Phronomy::Error, "Agent outcome selection cannot be repeated" if @decision_started
|
|
75
|
+
unless scope.outermost?
|
|
76
|
+
raise Phronomy::Persistence::TransactionError, "Agent outcome planning requires its own outer transaction"
|
|
77
|
+
end
|
|
78
|
+
@participant.guard_change(@subject, scope)
|
|
79
|
+
@store.guard_agents(scope, agent_ids: [@subject.agent_id])
|
|
80
|
+
agent_state = ExecutionChange.read_agent_evidence(store: @store, agent_id: @subject.agent_id,
|
|
81
|
+
execution_id: @subject.execution_id, journal_position: @root.journal_position, scope: scope)
|
|
82
|
+
retentions = Phronomy::Values::Immutable.json(@participant.change_evidence(@subject, scope))
|
|
83
|
+
before = [Phronomy::Values::Immutable.copy(agent_state), retentions].freeze
|
|
84
|
+
subject = @subject.with(cancel_requested: @subject.cancel_requested || agent_state.fetch(4))
|
|
85
|
+
@store.participate(scope) do |records|
|
|
86
|
+
records.assert_agent_watermark!(agent_id: subject.agent_id, agent_revision: @root.agent_revision, journal_position: @root.journal_position)
|
|
87
|
+
execution = agent_state.fetch(1)
|
|
88
|
+
unless execution.fetch("agent_id") == subject.agent_id && execution.fetch("execution_revision") == @current.execution_revision &&
|
|
89
|
+
execution.dig("metadata", "execution_extension") == @current.metadata["execution_extension"]
|
|
90
|
+
raise Phronomy::Persistence::StateConflictError, "Execution changed before outcome preparation"
|
|
91
|
+
end
|
|
92
|
+
yield records, before, subject, @read_facts.call(subject, scope, retentions, recovering: recovering)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def build_preparation(records, before, state, pending, state_data, participation, cancelled, prepare_outcome:)
|
|
97
|
+
plan = prepare_outcome.call(records, state, pending, cancelled)
|
|
98
|
+
after = plan.after(before: before, state_data: state_data, participation: participation)
|
|
99
|
+
values = {before: before, after: after, result: plan.outcome}.freeze
|
|
100
|
+
@on_preparation.call(values, plan).materialize(persistence: @store)
|
|
101
|
+
rescue OutcomeTextProjection::Required => error
|
|
102
|
+
@on_partial&.call(error.projection.bind(before: before, state_data: state_data,
|
|
103
|
+
participation: participation, persistence: @store, participant: @participant))
|
|
104
|
+
raise error.cause
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|