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,489 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module Phronomy
|
|
6
|
+
# Workflow-owned evidence, observation and application for one terminal save.
|
|
7
|
+
# Only evidence mailboxes and observer lists are shared with worker callbacks.
|
|
8
|
+
# Domain application and admission release stay on the captured serial owner.
|
|
9
|
+
# @api private
|
|
10
|
+
class WorkflowTerminalOperation
|
|
11
|
+
include Phronomy::StoppedTerminalCustody::Source
|
|
12
|
+
|
|
13
|
+
Observe = Data.define(:operation, :observer)
|
|
14
|
+
Ready = Data.define(:operation, :attempt_id)
|
|
15
|
+
|
|
16
|
+
attr_reader :workflow_instance_id, :recovery_id, :owner_token,
|
|
17
|
+
:fsm_session_id, :recovery
|
|
18
|
+
|
|
19
|
+
def initialize(runner_identity:, environment:, registry:, execution:, command:,
|
|
20
|
+
terminal_type:, event_sink:, context_class:)
|
|
21
|
+
@runner_identity = runner_identity
|
|
22
|
+
@environment = environment
|
|
23
|
+
@registry = registry
|
|
24
|
+
@command = command
|
|
25
|
+
@workflow_instance_id = execution.workflow_instance_id.to_s.freeze
|
|
26
|
+
@owner_token = execution.owner_token
|
|
27
|
+
@fsm_session_id = registry.workflow_admission_fsm_session_id(@workflow_instance_id)
|
|
28
|
+
@terminal_type = terminal_type
|
|
29
|
+
@event_sink = event_sink
|
|
30
|
+
@context_class = context_class
|
|
31
|
+
@snapshot = command.snapshot
|
|
32
|
+
@original_observer = execution.result_task
|
|
33
|
+
@mutex = Mutex.new
|
|
34
|
+
@attempts = Phronomy::RetainedAttempts.new(mutex: @mutex)
|
|
35
|
+
@original_error = nil
|
|
36
|
+
@last_error = nil
|
|
37
|
+
@uncertain = false
|
|
38
|
+
@adopted = false
|
|
39
|
+
@initial_delivered = false
|
|
40
|
+
@session_decision = nil
|
|
41
|
+
@confirmation = nil
|
|
42
|
+
@application_state = :pending
|
|
43
|
+
@ownership_state = :retained
|
|
44
|
+
@recovery_id = SecureRandom.uuid.freeze
|
|
45
|
+
@recovery = WorkflowTerminalRecovery.send(:new, self)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def belongs_to?(identity)
|
|
49
|
+
@runner_identity.equal?(identity)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def pending?
|
|
53
|
+
@mutex.synchronize { @uncertain && @ownership_state == :retained }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def start
|
|
57
|
+
@registry.retain_terminal_operation(self)
|
|
58
|
+
task = submit_worker(nil) do
|
|
59
|
+
WorkflowTerminalPersistence.save(@command, on_error: method(:remember_save_error))
|
|
60
|
+
end
|
|
61
|
+
watch(nil)
|
|
62
|
+
self
|
|
63
|
+
rescue => error
|
|
64
|
+
refused = @mutex.synchronize do
|
|
65
|
+
initial = @attempts.snapshot_locked(nil)
|
|
66
|
+
initial[:rejection_confirmed] && !initial[:started]
|
|
67
|
+
end
|
|
68
|
+
if task || !refused
|
|
69
|
+
# An absent handle is not refusal evidence. Keep the command and original
|
|
70
|
+
# owner when admission is unknown, just as for callback binding failure.
|
|
71
|
+
reject_initial_delivery(error)
|
|
72
|
+
return self
|
|
73
|
+
end
|
|
74
|
+
# Only synchronous executor refusal proves that this save cannot run.
|
|
75
|
+
# Workflow preserves its ordinary known-failure release policy in that case.
|
|
76
|
+
@mutex.synchronize do
|
|
77
|
+
@attempts.submission_failed_locked(nil, error, refused: true)
|
|
78
|
+
@session_decision = :known_failure
|
|
79
|
+
@application_state = :applied
|
|
80
|
+
end
|
|
81
|
+
raise
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def remember_save_error(error)
|
|
85
|
+
@mutex.synchronize { @original_error ||= error }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Called by the Workflow terminal policy, on the owner, before translating
|
|
89
|
+
# the result into a generic FSM terminal decision.
|
|
90
|
+
def accept_terminal_result(result)
|
|
91
|
+
@mutex.synchronize do
|
|
92
|
+
unless @initial_delivered && result.equal?(@delivered_result)
|
|
93
|
+
raise Phronomy::Error, "stale Workflow terminal persistence result"
|
|
94
|
+
end
|
|
95
|
+
@session_decision = result.outcome
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def adopt_retirement(retirement)
|
|
100
|
+
return false unless retirement.fsm_session_id.to_s == @fsm_session_id.to_s
|
|
101
|
+
@mutex.synchronize do
|
|
102
|
+
@retirement = retirement
|
|
103
|
+
@uncertain = true
|
|
104
|
+
@last_error ||= retirement.error
|
|
105
|
+
end
|
|
106
|
+
true
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def acknowledge_retained_retirement(retirement)
|
|
110
|
+
@mutex.synchronize do
|
|
111
|
+
retirement.acceptance if @ownership_state == :retained && @retirement.equal?(retirement)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def confirm_retirement(retirement)
|
|
116
|
+
@mutex.synchronize do
|
|
117
|
+
unless @retirement.equal?(retirement)
|
|
118
|
+
raise Phronomy::Error, "Workflow completion custody does not match its accepted retirement"
|
|
119
|
+
end
|
|
120
|
+
@adopted = true
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def retirement_confirmed?
|
|
125
|
+
@mutex.synchronize { @adopted }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def normal_finalization?
|
|
129
|
+
@mutex.synchronize do
|
|
130
|
+
!@uncertain && @attempts.snapshot_locked(nil)[:physical] && %i[success known_failure].include?(@session_decision)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def apply_normal_finalization
|
|
135
|
+
@mutex.synchronize { @application_state = :applied }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def release_permitted?
|
|
139
|
+
@mutex.synchronize do
|
|
140
|
+
@application_state == :applied && (!@uncertain || @adopted) && @attempts.quiescent_locked?
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def released
|
|
145
|
+
@mutex.synchronize do
|
|
146
|
+
@ownership_state = :released
|
|
147
|
+
@original_observer = nil
|
|
148
|
+
@environment = @registry = @command = @event_sink = nil
|
|
149
|
+
@attempts.release_task_locked(nil)
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Observation-only settlement. Never complete the Engine source as an
|
|
154
|
+
# ordinary failure here: its finalizer may release domain ownership.
|
|
155
|
+
def fail_original(error = nil)
|
|
156
|
+
observer, failure = @mutex.synchronize do
|
|
157
|
+
next [nil, nil] if @ownership_state == :released
|
|
158
|
+
@uncertain = true
|
|
159
|
+
@last_error = error if error
|
|
160
|
+
[@original_observer, diagnostic_locked(@last_error || @original_error)]
|
|
161
|
+
end
|
|
162
|
+
observer&.fail(failure)
|
|
163
|
+
failure
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def observe
|
|
167
|
+
receipt = @mutex.synchronize { @stopped_receipt }
|
|
168
|
+
if receipt
|
|
169
|
+
return TaskResult.completed(WorkflowTerminalRecoveryResult.new(recovery: recovery, outcome: receipt.outcome,
|
|
170
|
+
revision: receipt.revision, application_state: :pending, ownership_state: :transferred,
|
|
171
|
+
error: receipt.error))
|
|
172
|
+
end
|
|
173
|
+
observer = TaskResult.deferred(name: "workflow-reconcile:#{@recovery_id}")
|
|
174
|
+
@attempts.register(observer)
|
|
175
|
+
registry, released = @mutex.synchronize { [@registry, @ownership_state == :released] }
|
|
176
|
+
if released
|
|
177
|
+
observer.complete(build_result)
|
|
178
|
+
elsif !registry || !registry.post(Observe.new(operation: self, observer: observer),
|
|
179
|
+
completion: delivery_observer(observer))
|
|
180
|
+
observer.complete(unavailable_result(Phronomy::RuntimeShutdownError.new("Original Workflow owner is unavailable")))
|
|
181
|
+
end
|
|
182
|
+
observer
|
|
183
|
+
rescue => error
|
|
184
|
+
observer.complete(unavailable_result(error))
|
|
185
|
+
observer
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def shutdown(error)
|
|
189
|
+
fail_original(error)
|
|
190
|
+
observers = @attempts.observers
|
|
191
|
+
observers.each { |observer| observer.complete(unavailable_result(error)) }
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def deliver(message)
|
|
195
|
+
case message
|
|
196
|
+
when Observe then observe_on_owner(message.observer)
|
|
197
|
+
when Ready
|
|
198
|
+
message.attempt_id ? finish_attempt(message.attempt_id) : deliver_initial_result
|
|
199
|
+
else
|
|
200
|
+
raise Phronomy::Error, "unsupported Workflow recovery message"
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
private
|
|
205
|
+
|
|
206
|
+
def read_stopped_terminal(retry_save:)
|
|
207
|
+
raise Phronomy::Error, "stopped Workflow supports reconciliation only" if retry_save
|
|
208
|
+
proof = WorkflowTerminalPersistence.reconcile(@command, @original_error || @last_error)
|
|
209
|
+
case proof.outcome
|
|
210
|
+
when :success
|
|
211
|
+
Phronomy::StoppedTerminalCustody::Receipt.new(recovery_id: recovery_id, outcome: :confirmed_post,
|
|
212
|
+
revision: proof.revision, value: @snapshot, error: nil)
|
|
213
|
+
when :known_failure
|
|
214
|
+
Phronomy::StoppedTerminalCustody::Receipt.new(recovery_id: recovery_id, outcome: :confirmed_pre,
|
|
215
|
+
revision: nil, value: nil, error: proof.error)
|
|
216
|
+
else
|
|
217
|
+
raise(proof.error || Phronomy::Error.new("original Workflow terminal disposition is unconfirmed"))
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def delivery_observer(observer)
|
|
222
|
+
TaskResult.deferred(name: "workflow-reconcile-delivery:#{@recovery_id}").tap do |delivery|
|
|
223
|
+
delivery.on_complete do |_value, error|
|
|
224
|
+
observer.complete(unavailable_result(error)) if error
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def submit_worker(id, &work)
|
|
230
|
+
@attempts.submit(id, submitter: @environment,
|
|
231
|
+
missing_handle_message: "Workflow submission returned no task", &work)
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def watch(id)
|
|
235
|
+
@attempts.watch(id) { signal(id) }
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def signal(id)
|
|
239
|
+
registry = @mutex.synchronize { @registry }
|
|
240
|
+
return unless registry
|
|
241
|
+
return if registry.post(Ready.new(operation: self, attempt_id: id))
|
|
242
|
+
delivery_failed(id, Phronomy::RuntimeShutdownError.new("Workflow owner rejected terminal evidence"))
|
|
243
|
+
rescue => error
|
|
244
|
+
delivery_failed(id, error)
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def delivery_failed(id, error)
|
|
248
|
+
if id
|
|
249
|
+
observers = @mutex.synchronize do
|
|
250
|
+
@attempts.take_observers_locked(id)
|
|
251
|
+
end
|
|
252
|
+
result = unavailable_result(error)
|
|
253
|
+
observers.each { |observer| observer.complete(result) }
|
|
254
|
+
else
|
|
255
|
+
fail_original(error)
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def persistence_result(slot)
|
|
260
|
+
return slot[:result] if slot[:physical] && slot[:result]
|
|
261
|
+
if slot[:physical] && !slot[:started] && slot[:error]
|
|
262
|
+
return WorkflowTerminalPersistence::Result.new(
|
|
263
|
+
outcome: :known_failure, revision: nil, error: slot[:error]
|
|
264
|
+
)
|
|
265
|
+
end
|
|
266
|
+
WorkflowTerminalPersistence::Result.new(outcome: :outcome_unknown,
|
|
267
|
+
revision: nil, error: slot[:error] || Phronomy::Error.new("Workflow persistence is not physically complete"))
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def retain_classification(result)
|
|
271
|
+
@mutex.synchronize do
|
|
272
|
+
if %i[success known_failure].include?(result.outcome)
|
|
273
|
+
@confirmation ||= result
|
|
274
|
+
else
|
|
275
|
+
@uncertain = true
|
|
276
|
+
@last_error = result.error
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
result
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def deliver_initial_result
|
|
283
|
+
result, sink = @mutex.synchronize do
|
|
284
|
+
return unless @attempts.snapshot_locked(nil)[:logical]
|
|
285
|
+
return if @initial_delivered || @ownership_state == :released
|
|
286
|
+
result = persistence_result(@attempts.snapshot_locked(nil))
|
|
287
|
+
if @uncertain
|
|
288
|
+
result = WorkflowTerminalPersistence::Result.new(
|
|
289
|
+
outcome: :outcome_unknown, revision: nil, error: @last_error || result.error
|
|
290
|
+
)
|
|
291
|
+
end
|
|
292
|
+
@initial_delivered = true
|
|
293
|
+
@delivered_result = result
|
|
294
|
+
[result, @event_sink]
|
|
295
|
+
end
|
|
296
|
+
retain_classification(result)
|
|
297
|
+
return if sink.post(:workflow_terminal_persistence_result, result)
|
|
298
|
+
reject_initial_delivery(Phronomy::Error.new("Workflow session rejected terminal evidence"))
|
|
299
|
+
rescue => error
|
|
300
|
+
reject_initial_delivery(error)
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
def reject_initial_delivery(error)
|
|
304
|
+
fail_original(error)
|
|
305
|
+
@environment&.retire_session(fsm_session_id: @fsm_session_id, error: error)
|
|
306
|
+
rescue => retirement_error
|
|
307
|
+
fail_original(retirement_error)
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def observe_on_owner(observer)
|
|
311
|
+
@attempts.refresh(nil)
|
|
312
|
+
released, adopted, physical, active = @mutex.synchronize do
|
|
313
|
+
[@ownership_state == :released, @adopted, @attempts.snapshot_locked(nil)[:physical], @attempts.active_snapshot_locked]
|
|
314
|
+
end
|
|
315
|
+
return observer.complete(build_result) if released
|
|
316
|
+
unless @registry.owns_terminal_operation?(self)
|
|
317
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("Workflow recovery owner no longer matches")))
|
|
318
|
+
end
|
|
319
|
+
unless adopted
|
|
320
|
+
@environment.retire_session(fsm_session_id: @fsm_session_id,
|
|
321
|
+
error: diagnostic(@last_error || @original_error))
|
|
322
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("Workflow retirement custody is not yet accepted")))
|
|
323
|
+
end
|
|
324
|
+
unless physical
|
|
325
|
+
return observer.complete(unavailable_result(Phronomy::Error.new("Original Workflow save has no physical completion proof")))
|
|
326
|
+
end
|
|
327
|
+
return observe_active_attempt(observer, active) if active
|
|
328
|
+
|
|
329
|
+
# A late physical worker result remains useful even when its logical
|
|
330
|
+
# TaskResult was abandoned. It is evidence, never a replayable write.
|
|
331
|
+
save_result = @mutex.synchronize { persistence_result(@attempts.snapshot_locked(nil)) }
|
|
332
|
+
retain_classification(save_result) unless @confirmation
|
|
333
|
+
if @confirmation
|
|
334
|
+
return observer.complete(apply_confirmation)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
begin_reconciliation(observer)
|
|
338
|
+
rescue => error
|
|
339
|
+
observer.complete(unavailable_result(error))
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def observe_active_attempt(observer, active)
|
|
343
|
+
@attempts.refresh(active[:id])
|
|
344
|
+
unknown, error, watch_error = @mutex.synchronize do
|
|
345
|
+
active = @attempts.snapshot_locked(active[:id])
|
|
346
|
+
[active[:admission_unknown], active[:error],
|
|
347
|
+
(active[:watch_error] if !active[:physical_watch] && !active[:physical])]
|
|
348
|
+
end
|
|
349
|
+
if unknown
|
|
350
|
+
return observer.complete(unavailable_result(error))
|
|
351
|
+
end
|
|
352
|
+
return observer.complete(unavailable_result(watch_error)) if watch_error
|
|
353
|
+
@mutex.synchronize { @attempts.join_locked(active[:id], observer: observer) }
|
|
354
|
+
finish_attempt(active[:id])
|
|
355
|
+
nil
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def begin_reconciliation(observer)
|
|
359
|
+
id = Object.new.freeze
|
|
360
|
+
@mutex.synchronize do
|
|
361
|
+
@attempts.reserve_locked(id, observer: observer)
|
|
362
|
+
end
|
|
363
|
+
task = submit_worker(id) do
|
|
364
|
+
WorkflowTerminalPersistence.reconcile(@command, @original_error || @last_error)
|
|
365
|
+
end
|
|
366
|
+
watch(id)
|
|
367
|
+
rescue => error
|
|
368
|
+
if task
|
|
369
|
+
# An admitted read still owns its physical lane even when callback
|
|
370
|
+
# registration fails. Another explicit observation may inspect it.
|
|
371
|
+
delivery_failed(id, error)
|
|
372
|
+
return
|
|
373
|
+
end
|
|
374
|
+
observers = @mutex.synchronize do
|
|
375
|
+
active = @attempts.active_snapshot_locked
|
|
376
|
+
if defined?(id) && id && active && active[:id].equal?(id)
|
|
377
|
+
values = @attempts.take_observers_locked(id)
|
|
378
|
+
# Unknown admission keeps the attempt even after all observers settle.
|
|
379
|
+
# A late worker result alone cannot prove physical executor completion.
|
|
380
|
+
@attempts.retire_locked(id) unless active[:admission_unknown]
|
|
381
|
+
values
|
|
382
|
+
else
|
|
383
|
+
[observer]
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
result = unavailable_result(error)
|
|
387
|
+
observers.each { |item| item.complete(result) }
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
def finish_attempt(id)
|
|
391
|
+
@attempts.refresh(id)
|
|
392
|
+
result, error = @mutex.synchronize do
|
|
393
|
+
slot = @attempts.snapshot_locked(id)
|
|
394
|
+
return unless slot && slot[:physical] && slot[:logical]
|
|
395
|
+
[slot[:result], slot[:error]]
|
|
396
|
+
end
|
|
397
|
+
if error && !error.is_a?(StandardError)
|
|
398
|
+
observers = @attempts.retire(id)
|
|
399
|
+
observers.each { |observer| observer.fail(error) }
|
|
400
|
+
return
|
|
401
|
+
end
|
|
402
|
+
retain_classification(result) if result
|
|
403
|
+
observation = if @confirmation
|
|
404
|
+
apply_confirmation
|
|
405
|
+
elsif error
|
|
406
|
+
unavailable_result(error)
|
|
407
|
+
else
|
|
408
|
+
build_result(outcome: result&.error.is_a?(Phronomy::Persistence::StateConflictError) ? :unresolved : :unavailable,
|
|
409
|
+
error: diagnostic(result&.error))
|
|
410
|
+
end
|
|
411
|
+
@attempts.retire(id).each_with_index do |observer, index|
|
|
412
|
+
# Sharing a physical read does not share the mutable returned context.
|
|
413
|
+
value = if index.zero?
|
|
414
|
+
observation
|
|
415
|
+
else
|
|
416
|
+
build_result(outcome: observation.outcome, error: observation.error,
|
|
417
|
+
materialize: observation.application_state == :applied)
|
|
418
|
+
end
|
|
419
|
+
observer.complete(value)
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
def apply_confirmation
|
|
424
|
+
# Reconstruct before release: a constructor failure leaves application
|
|
425
|
+
# pending while preserving the independently confirmed durable fact.
|
|
426
|
+
context = materialize if @confirmation.outcome == :success
|
|
427
|
+
@mutex.synchronize { @application_state = :applied }
|
|
428
|
+
unless @registry.release_workflow(@workflow_instance_id, owner_token: @owner_token)
|
|
429
|
+
return build_result(error: diagnostic(Phronomy::Error.new("Workflow owner could not be released")))
|
|
430
|
+
end
|
|
431
|
+
build_result(context: context)
|
|
432
|
+
rescue => error
|
|
433
|
+
build_result(error: diagnostic(error))
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
def materialize
|
|
437
|
+
fields = duplicate_value(@snapshot.fetch(:fields))
|
|
438
|
+
@context_class.new(**fields).set_graph_metadata(
|
|
439
|
+
workflow_instance_id: @workflow_instance_id,
|
|
440
|
+
phase: @snapshot.fetch(:phase).to_sym
|
|
441
|
+
)
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
def duplicate_value(value)
|
|
445
|
+
case value
|
|
446
|
+
when Hash then value.to_h { |key, child| [key, duplicate_value(child)] }
|
|
447
|
+
when Array then value.map { |child| duplicate_value(child) }
|
|
448
|
+
when String then value.dup
|
|
449
|
+
else value
|
|
450
|
+
end
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def unavailable_result(error)
|
|
454
|
+
build_result(outcome: :unavailable, error: diagnostic(error), materialize: false)
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
def build_result(outcome: nil, context: nil, error: nil, materialize: true)
|
|
458
|
+
confirmation, applied, ownership = @mutex.synchronize do
|
|
459
|
+
[@confirmation, @application_state, @ownership_state]
|
|
460
|
+
end
|
|
461
|
+
if confirmation
|
|
462
|
+
outcome = (confirmation.outcome == :success) ? :confirmed_post : :confirmed_pre
|
|
463
|
+
error ||= confirmation.error if outcome == :confirmed_pre
|
|
464
|
+
context ||= self.materialize if materialize && outcome == :confirmed_post && !error
|
|
465
|
+
end
|
|
466
|
+
WorkflowTerminalRecoveryResult.new(recovery: @recovery,
|
|
467
|
+
outcome: outcome || :unavailable, application_state: applied,
|
|
468
|
+
ownership_state: ownership, revision: confirmation&.revision,
|
|
469
|
+
context: context, error: error)
|
|
470
|
+
rescue => materialization_error
|
|
471
|
+
WorkflowTerminalRecoveryResult.new(recovery: @recovery,
|
|
472
|
+
outcome: outcome || :unavailable, application_state: applied,
|
|
473
|
+
ownership_state: ownership, revision: confirmation&.revision,
|
|
474
|
+
context: nil, error: diagnostic(materialization_error))
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
def diagnostic(error)
|
|
478
|
+
@mutex.synchronize { diagnostic_locked(error) }
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
def diagnostic_locked(error)
|
|
482
|
+
return error if error && !error.is_a?(StandardError)
|
|
483
|
+
return error if error.is_a?(WorkflowTerminalRecoveryError) && error.recovery.equal?(@recovery)
|
|
484
|
+
WorkflowTerminalRecoveryError.new(recovery: @recovery,
|
|
485
|
+
error: error || Phronomy::Error.new("Workflow terminal outcome requires reconciliation"),
|
|
486
|
+
original_error: @original_error || error)
|
|
487
|
+
end
|
|
488
|
+
end
|
|
489
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phronomy
|
|
4
|
+
# Repository I/O and evidence classification for an immutable terminal save.
|
|
5
|
+
# The caller owns scheduling, retained responsibility and owner-side release.
|
|
6
|
+
# This component never restarts a Workflow or chooses a recovery Runtime.
|
|
7
|
+
# @api private
|
|
8
|
+
class WorkflowTerminalPersistence
|
|
9
|
+
Command = Data.define(:repository, :workflow_instance_id, :expected_revision, :snapshot)
|
|
10
|
+
Result = Data.define(:outcome, :revision, :error)
|
|
11
|
+
|
|
12
|
+
def self.save(operation, on_error: nil)
|
|
13
|
+
revision = operation.repository.save(
|
|
14
|
+
operation.workflow_instance_id,
|
|
15
|
+
expected_revision: operation.expected_revision,
|
|
16
|
+
snapshot: operation.snapshot
|
|
17
|
+
)
|
|
18
|
+
Result.new(
|
|
19
|
+
outcome: :success,
|
|
20
|
+
revision: revision,
|
|
21
|
+
error: nil
|
|
22
|
+
)
|
|
23
|
+
rescue Phronomy::Persistence::ConflictError,
|
|
24
|
+
Phronomy::Persistence::NotFoundError,
|
|
25
|
+
Phronomy::Persistence::SerializationError,
|
|
26
|
+
Phronomy::Persistence::UnsupportedBackendError => error
|
|
27
|
+
on_error&.call(error)
|
|
28
|
+
Result.new(
|
|
29
|
+
outcome: :known_failure,
|
|
30
|
+
revision: nil,
|
|
31
|
+
error: error
|
|
32
|
+
)
|
|
33
|
+
rescue => error
|
|
34
|
+
on_error&.call(error)
|
|
35
|
+
reconcile(operation, error)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.reconcile(operation, original_error)
|
|
39
|
+
record = operation.repository.load(operation.workflow_instance_id)
|
|
40
|
+
case Phronomy::Persistence::SnapshotComparison.compare_revisioned_snapshot(
|
|
41
|
+
record: record,
|
|
42
|
+
expected_pre_revision: operation.expected_revision,
|
|
43
|
+
intended_snapshot: operation.snapshot,
|
|
44
|
+
intended_post_revision: operation.expected_revision.nil? ? 1 : operation.expected_revision + 1
|
|
45
|
+
)
|
|
46
|
+
when :post_state
|
|
47
|
+
revision = Phronomy::Persistence::SnapshotComparison.fetch_value(record, :revision)
|
|
48
|
+
Result.new(
|
|
49
|
+
outcome: :success,
|
|
50
|
+
revision: revision,
|
|
51
|
+
error: nil
|
|
52
|
+
)
|
|
53
|
+
when :pre_state
|
|
54
|
+
# The uncertain save is now known not to have committed. Do not blindly
|
|
55
|
+
# retry it here; surface a known failure through the ordinary terminal
|
|
56
|
+
# barrier semantics.
|
|
57
|
+
Result.new(
|
|
58
|
+
outcome: :known_failure,
|
|
59
|
+
revision: nil,
|
|
60
|
+
error: original_error
|
|
61
|
+
)
|
|
62
|
+
else
|
|
63
|
+
conflict = Phronomy::Persistence::StateConflictError.new(
|
|
64
|
+
"Workflow terminal Persistence outcome conflicts with both expected " \
|
|
65
|
+
"pre-state and intended post-state for #{operation.workflow_instance_id.inspect}"
|
|
66
|
+
)
|
|
67
|
+
Result.new(
|
|
68
|
+
outcome: :outcome_unknown,
|
|
69
|
+
revision: nil,
|
|
70
|
+
error: conflict
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
rescue => reconciliation_error
|
|
74
|
+
Result.new(
|
|
75
|
+
outcome: :outcome_unknown,
|
|
76
|
+
revision: nil,
|
|
77
|
+
error: reconciliation_error
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phronomy
|
|
4
|
+
# Process-local reference to one retained Workflow terminal operation.
|
|
5
|
+
# Obtain this from a recovery error or pending_terminal_recovery; it is not
|
|
6
|
+
# a serializable ticket. A designated StoppedTerminalCustody can accept a
|
|
7
|
+
# verified terminal disposition after the original Runtime stops.
|
|
8
|
+
# @api public
|
|
9
|
+
class WorkflowTerminalRecovery
|
|
10
|
+
# @return [String]
|
|
11
|
+
# @api public
|
|
12
|
+
attr_reader :workflow_instance_id, :recovery_id
|
|
13
|
+
|
|
14
|
+
# @api private
|
|
15
|
+
def initialize(operation)
|
|
16
|
+
@operation = operation
|
|
17
|
+
@workflow_instance_id = operation.workflow_instance_id.dup.freeze
|
|
18
|
+
@recovery_id = operation.recovery_id.dup.freeze
|
|
19
|
+
freeze
|
|
20
|
+
end
|
|
21
|
+
private_class_method :new
|
|
22
|
+
|
|
23
|
+
# Designate a fresh in-process recipient for a stopped terminal disposition.
|
|
24
|
+
# Retain the returned recipient before calling its blocking reconcile method.
|
|
25
|
+
# @return [Phronomy::StoppedTerminalCustody] the designated recipient
|
|
26
|
+
# @api public
|
|
27
|
+
def new_stopped_recipient
|
|
28
|
+
unless @operation.is_a?(WorkflowTerminalOperation) && @operation.recovery.equal?(self)
|
|
29
|
+
raise ArgumentError, "recovery is not the exact original terminal capability"
|
|
30
|
+
end
|
|
31
|
+
Phronomy::StoppedTerminalCustody.send(:new, @operation)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
attr_reader :operation
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../common/error"
|
|
4
|
+
|
|
5
|
+
module Phronomy
|
|
6
|
+
# The terminal persistence obligation needs an explicit recovery observation.
|
|
7
|
+
# This error does not establish business failure or release Workflow ownership.
|
|
8
|
+
# The capability is process-local and belongs to the original Workflow.
|
|
9
|
+
# @api public
|
|
10
|
+
class WorkflowTerminalRecoveryError < Phronomy::Error
|
|
11
|
+
# @api public
|
|
12
|
+
attr_reader :recovery, :workflow_instance_id, :recovery_id, :original_error
|
|
13
|
+
|
|
14
|
+
# Constructed by the owner after retaining the terminal obligation.
|
|
15
|
+
# @api private
|
|
16
|
+
def initialize(recovery:, error:, original_error: error)
|
|
17
|
+
@recovery = recovery
|
|
18
|
+
@workflow_instance_id = recovery.workflow_instance_id.to_s.dup.freeze
|
|
19
|
+
@recovery_id = recovery.recovery_id.to_s.dup.freeze
|
|
20
|
+
@operation_error = error
|
|
21
|
+
@original_error = original_error
|
|
22
|
+
super("Workflow #{@workflow_instance_id.inspect} terminal recovery #{@recovery_id.inspect} " \
|
|
23
|
+
"requires an explicit reconciliation observation", code: "workflow.terminal_recovery")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Preserve the immediate operation failure even when delivered through a
|
|
27
|
+
# TaskResult rather than raised inside its original rescue clause.
|
|
28
|
+
# @return [Exception, nil]
|
|
29
|
+
# @api public
|
|
30
|
+
def cause
|
|
31
|
+
@operation_error || super
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Phronomy
|
|
4
|
+
# One immutable observation of terminal persistence, owner application and
|
|
5
|
+
# ownership release. Confirmed persistence alone is not completed recovery.
|
|
6
|
+
# The context is an independent application view, not retained proof.
|
|
7
|
+
# @api public
|
|
8
|
+
class WorkflowTerminalRecoveryResult
|
|
9
|
+
OUTCOMES = %i[confirmed_post confirmed_pre unresolved unavailable].freeze
|
|
10
|
+
APPLICATION_STATES = %i[pending applied].freeze
|
|
11
|
+
OWNERSHIP_STATES = %i[retained released transferred].freeze
|
|
12
|
+
private_constant :OUTCOMES, :APPLICATION_STATES, :OWNERSHIP_STATES
|
|
13
|
+
|
|
14
|
+
# @api public
|
|
15
|
+
attr_reader :recovery, :workflow_instance_id, :recovery_id, :outcome,
|
|
16
|
+
:application_state, :ownership_state, :revision, :context, :error
|
|
17
|
+
|
|
18
|
+
# Constructed from facts retained by the exact Workflow owner.
|
|
19
|
+
# @api private
|
|
20
|
+
def initialize(recovery:, outcome:, application_state:, ownership_state:,
|
|
21
|
+
revision: nil, context: nil, error: nil)
|
|
22
|
+
unless OUTCOMES.include?(outcome) && APPLICATION_STATES.include?(application_state) &&
|
|
23
|
+
OWNERSHIP_STATES.include?(ownership_state)
|
|
24
|
+
raise ArgumentError, "unsupported Workflow terminal recovery state"
|
|
25
|
+
end
|
|
26
|
+
if ownership_state == :transferred && application_state != :pending
|
|
27
|
+
raise ArgumentError, "a stopped transfer cannot apply a live owner transition"
|
|
28
|
+
end
|
|
29
|
+
if ownership_state == :released && application_state != :applied
|
|
30
|
+
raise ArgumentError, "Workflow ownership cannot be released before application"
|
|
31
|
+
end
|
|
32
|
+
if %i[unresolved unavailable].include?(outcome) &&
|
|
33
|
+
(application_state != :pending || ownership_state != :retained)
|
|
34
|
+
raise ArgumentError, "unclassified Workflow persistence must remain pending and retained"
|
|
35
|
+
end
|
|
36
|
+
if outcome == :confirmed_post
|
|
37
|
+
unless revision.is_a?(Integer) && revision.positive?
|
|
38
|
+
raise ArgumentError, "confirmed Workflow persistence requires its positive revision"
|
|
39
|
+
end
|
|
40
|
+
elsif !revision.nil? || !context.nil?
|
|
41
|
+
raise ArgumentError, "only confirmed Workflow post-state has a revision or context"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
@recovery = recovery
|
|
45
|
+
@workflow_instance_id = recovery.workflow_instance_id.to_s.dup.freeze
|
|
46
|
+
@recovery_id = recovery.recovery_id.to_s.dup.freeze
|
|
47
|
+
@outcome = outcome
|
|
48
|
+
@application_state = application_state
|
|
49
|
+
@ownership_state = ownership_state
|
|
50
|
+
@revision = revision
|
|
51
|
+
@context = context
|
|
52
|
+
@error = error
|
|
53
|
+
freeze
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|