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.
Files changed (211) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +87 -0
  3. data/README.md +24 -2
  4. data/SECURITY.md +1 -1
  5. data/docs/architecture/agent-approval-recovery.md +320 -0
  6. data/docs/architecture/agent-child-approval-recovery.md +387 -0
  7. data/docs/architecture/agent-child-execution-recovery.md +425 -0
  8. data/docs/architecture/agent-dispatch-recovery.md +308 -0
  9. data/docs/architecture/agent-terminal-persistence-evidence.md +360 -0
  10. data/docs/architecture/agent-terminal-recovery.md +298 -0
  11. data/docs/architecture/agent-terminal-submission-recovery.md +275 -0
  12. data/docs/architecture/agent-tool-child-lifetime.md +29 -0
  13. data/docs/architecture/agent-transition-ownership.md +24 -0
  14. data/docs/architecture/backend-groups.md +6 -0
  15. data/docs/architecture/detached-outcome-proof-projection.md +576 -0
  16. data/docs/architecture/execution-abstraction-review.md +5 -0
  17. data/docs/architecture/handoff-dispatch-recovery.md +205 -0
  18. data/docs/architecture/handoff-initial-terminal-application.md +321 -0
  19. data/docs/architecture/initial-preparation-recovery.md +308 -0
  20. data/docs/architecture/multi-agent-handoff.md +17 -0
  21. data/docs/architecture/native-outcome-preparation.md +715 -0
  22. data/docs/architecture/native-parent-dispatch-recovery.md +460 -0
  23. data/docs/architecture/native-parent-outcome-recovery.md +644 -0
  24. data/docs/architecture/parent-child-terminal-acknowledgement.md +297 -0
  25. data/docs/architecture/r8-unit12.md +6 -3
  26. data/docs/architecture/r8-unit4.md +11 -2
  27. data/docs/architecture/recovery-physical-attempt.md +88 -0
  28. data/docs/architecture/recovery-submission-custody.md +247 -0
  29. data/docs/architecture/retained-attempts.md +229 -0
  30. data/docs/architecture/retained-outcome-materialization.md +636 -0
  31. data/docs/architecture/retained-outcome-publication.md +506 -0
  32. data/docs/architecture/storage-execution-boundaries.md +1 -1
  33. data/docs/architecture/structured-output-and-scope.md +15 -0
  34. data/docs/architecture/synchronous-participant-application.md +397 -0
  35. data/docs/architecture/synchronous-participant-contract.md +195 -0
  36. data/docs/architecture/team-admission-rejection.md +91 -0
  37. data/docs/architecture/team-cancellation-delivery.md +245 -0
  38. data/docs/architecture/team-child-retirement.md +104 -0
  39. data/docs/architecture/tracing.md +26 -0
  40. data/docs/architecture/workflow-terminal-ownership-design.md +25 -9
  41. data/docs/architecture/workflow-terminal-reconciliation.md +221 -0
  42. data/docs/architecture.md +16 -9
  43. data/docs/decisions/025-process-local-agent-ownership-and-runtime-admission.md +8 -0
  44. data/docs/features.md +3 -3
  45. data/docs/mcp-client.md +138 -5
  46. data/docs/migrations/orchestrator-subagent-dispatch.md +89 -0
  47. data/docs/migrations/vector-async-clients.md +18 -3
  48. data/docs/persistence-backends.md +30 -4
  49. data/docs/runtime-and-concurrency.md +235 -11
  50. data/lib/phronomy/agent/admission.rb +37 -17
  51. data/lib/phronomy/agent/api/agent.rb +25 -0
  52. data/lib/phronomy/agent/approval_recovery.rb +30 -0
  53. data/lib/phronomy/agent/approval_recovery_api.rb +61 -0
  54. data/lib/phronomy/agent/approval_recovery_error.rb +39 -0
  55. data/lib/phronomy/agent/approval_recovery_result.rb +94 -0
  56. data/lib/phronomy/agent/async_event_api.rb +22 -5
  57. data/lib/phronomy/agent/base.rb +76 -48
  58. data/lib/phronomy/agent/context_preparation.rb +10 -8
  59. data/lib/phronomy/agent/dispatch_recovery.rb +29 -0
  60. data/lib/phronomy/agent/dispatch_recovery_api.rb +84 -0
  61. data/lib/phronomy/agent/dispatch_recovery_error.rb +36 -0
  62. data/lib/phronomy/agent/dispatch_recovery_result.rb +93 -0
  63. data/lib/phronomy/agent/execution/agent_invocation.rb +11 -3
  64. data/lib/phronomy/agent/execution/approval_continuation_plan.rb +131 -0
  65. data/lib/phronomy/agent/execution/approval_parent_evidence.rb +116 -0
  66. data/lib/phronomy/agent/execution/approval_persistence_evidence.rb +111 -0
  67. data/lib/phronomy/agent/execution/approval_recovery_operation.rb +496 -0
  68. data/lib/phronomy/agent/execution/approval_resume_commit.rb +174 -10
  69. data/lib/phronomy/agent/execution/dispatch_continuation_delivery.rb +96 -0
  70. data/lib/phronomy/agent/execution/dispatch_persistence_evidence.rb +147 -0
  71. data/lib/phronomy/agent/execution/dispatch_preparation.rb +63 -7
  72. data/lib/phronomy/agent/execution/dispatch_recovery_operation.rb +495 -0
  73. data/lib/phronomy/agent/execution/execution_coordinator.rb +724 -158
  74. data/lib/phronomy/agent/execution/execution_outcome_committer.rb +312 -39
  75. data/lib/phronomy/agent/execution/execution_registry.rb +508 -13
  76. data/lib/phronomy/agent/execution/initial_persistence_evidence.rb +269 -0
  77. data/lib/phronomy/agent/execution/initial_preparation.rb +132 -40
  78. data/lib/phronomy/agent/execution/initial_preparation_recovery_operation.rb +513 -0
  79. data/lib/phronomy/agent/execution/invocation_transitions.rb +3 -0
  80. data/lib/phronomy/agent/execution/outcome_planning.rb +108 -0
  81. data/lib/phronomy/agent/execution/outcome_preparation.rb +81 -0
  82. data/lib/phronomy/agent/execution/outcome_recovery_operation.rb +609 -0
  83. data/lib/phronomy/agent/execution/outcome_text_projection.rb +73 -0
  84. data/lib/phronomy/agent/execution/outcome_write_plan.rb +48 -0
  85. data/lib/phronomy/agent/execution/runtime_record_encoder.rb +7 -4
  86. data/lib/phronomy/agent/execution/terminal_persistence_evidence.rb +280 -0
  87. data/lib/phronomy/agent/execution/terminal_recovery_operation.rb +38 -0
  88. data/lib/phronomy/agent/execution_change.rb +138 -26
  89. data/lib/phronomy/agent/execution_environment.rb +29 -0
  90. data/lib/phronomy/agent/execution_rejected_error.rb +34 -0
  91. data/lib/phronomy/agent/initial_preparation_recovery.rb +32 -0
  92. data/lib/phronomy/agent/initial_preparation_recovery_api.rb +85 -0
  93. data/lib/phronomy/agent/initial_preparation_recovery_error.rb +41 -0
  94. data/lib/phronomy/agent/initial_preparation_recovery_result.rb +115 -0
  95. data/lib/phronomy/agent/lifecycle/hosted_approval_context.rb +86 -0
  96. data/lib/phronomy/agent/lifecycle/hosted_invocation.rb +42 -10
  97. data/lib/phronomy/agent/lifecycle/identity_write.rb +101 -0
  98. data/lib/phronomy/agent/lifecycle/lifetime_api.rb +28 -0
  99. data/lib/phronomy/agent/lifecycle/ownership_registry.rb +136 -70
  100. data/lib/phronomy/agent/lifecycle/scope.rb +3 -3
  101. data/lib/phronomy/agent/outcome_recovery.rb +47 -0
  102. data/lib/phronomy/agent/outcome_recovery_api.rb +114 -0
  103. data/lib/phronomy/agent/outcome_recovery_error.rb +36 -0
  104. data/lib/phronomy/agent/outcome_recovery_result.rb +92 -0
  105. data/lib/phronomy/agent/output_contract.rb +16 -6
  106. data/lib/phronomy/agent/persistence/identity_receipt_repository.rb +61 -0
  107. data/lib/phronomy/agent/persistence/records.rb +2 -0
  108. data/lib/phronomy/agent/persistence/storage_schema.rb +2 -0
  109. data/lib/phronomy/agent/recovery/invocation_restorer.rb +8 -5
  110. data/lib/phronomy/agent/recovery/recovery_coordinator/continuation.rb +6 -4
  111. data/lib/phronomy/agent/recovery/recovery_coordinator/installation.rb +21 -43
  112. data/lib/phronomy/agent/recovery/recovery_coordinator.rb +19 -6
  113. data/lib/phronomy/agent/runtime_binding/engine_environment.rb +12 -0
  114. data/lib/phronomy/agent/state_writer.rb +4 -3
  115. data/lib/phronomy/agent/store.rb +7 -1
  116. data/lib/phronomy/agent/terminal_recovery.rb +41 -0
  117. data/lib/phronomy/agent/terminal_recovery_api.rb +101 -0
  118. data/lib/phronomy/agent/terminal_recovery_error.rb +36 -0
  119. data/lib/phronomy/agent/terminal_recovery_result.rb +67 -0
  120. data/lib/phronomy/agent/tool_execution/tool_approval_request.rb +25 -1
  121. data/lib/phronomy/common/values/serializable.rb +37 -4
  122. data/lib/phronomy/engine/concurrency/offload_pool.rb +32 -4
  123. data/lib/phronomy/engine/concurrency/pool_registry.rb +6 -0
  124. data/lib/phronomy/engine/event_loop.rb +161 -11
  125. data/lib/phronomy/engine/execution_binding.rb +4 -2
  126. data/lib/phronomy/engine/execution_receiver_binding.rb +5 -0
  127. data/lib/phronomy/engine/runtime/shutdown_result.rb +21 -0
  128. data/lib/phronomy/engine/runtime/timer_queue.rb +7 -2
  129. data/lib/phronomy/engine/runtime.rb +119 -15
  130. data/lib/phronomy/execution/attempt_completion.rb +91 -0
  131. data/lib/phronomy/execution/concurrency/cancellation_token.rb +7 -2
  132. data/lib/phronomy/execution/concurrency/physical_completion_task.rb +7 -2
  133. data/lib/phronomy/execution/execution.rb +17 -2
  134. data/lib/phronomy/execution/execution_receiver.rb +27 -0
  135. data/lib/phronomy/execution/execution_retirement.rb +30 -0
  136. data/lib/phronomy/execution/invocation_controls.rb +2 -2
  137. data/lib/phronomy/execution/retained_attempts.rb +179 -0
  138. data/lib/phronomy/execution/stopped_terminal_custody.rb +99 -0
  139. data/lib/phronomy/llm_adapter/response.rb +7 -1
  140. data/lib/phronomy/multi_agent/admission_registry.rb +6 -0
  141. data/lib/phronomy/multi_agent/durable_subagent_coordinator.rb +175 -39
  142. data/lib/phronomy/multi_agent/handoff_dispatch_evidence.rb +176 -0
  143. data/lib/phronomy/multi_agent/handoff_finalization_error.rb +33 -0
  144. data/lib/phronomy/multi_agent/handoff_history_release.rb +80 -0
  145. data/lib/phronomy/multi_agent/handoff_participant.rb +31 -6
  146. data/lib/phronomy/multi_agent/handoff_runner.rb +195 -21
  147. data/lib/phronomy/multi_agent/handoff_terminal_finalization.rb +176 -0
  148. data/lib/phronomy/multi_agent/native_approval_evidence.rb +26 -0
  149. data/lib/phronomy/multi_agent/native_child_authority.rb +135 -0
  150. data/lib/phronomy/multi_agent/native_dispatch_evidence.rb +43 -0
  151. data/lib/phronomy/multi_agent/native_outcome_evidence.rb +46 -0
  152. data/lib/phronomy/multi_agent/native_persistence_plan.rb +93 -0
  153. data/lib/phronomy/multi_agent/orchestrator.rb +28 -52
  154. data/lib/phronomy/multi_agent/reserved_child_admission.rb +90 -1
  155. data/lib/phronomy/multi_agent/store.rb +5 -34
  156. data/lib/phronomy/multi_agent/subagent_history_release.rb +88 -0
  157. data/lib/phronomy/multi_agent/team_cancellation_delivery_error.rb +21 -0
  158. data/lib/phronomy/multi_agent/team_cancellation_request.rb +134 -0
  159. data/lib/phronomy/multi_agent/team_coordinator.rb +174 -27
  160. data/lib/phronomy/persistence_composition/stores.rb +10 -3
  161. data/lib/phronomy/runtime_composition/execution_defaults.rb +18 -0
  162. data/lib/phronomy/storage/backends/in_memory.rb +3 -1
  163. data/lib/phronomy/storage/resource.rb +10 -1
  164. data/lib/phronomy/tools/agent.rb +7 -1
  165. data/lib/phronomy/tools/mcp.rb +210 -91
  166. data/lib/phronomy/tools/mcp_http_close_response.rb +23 -0
  167. data/lib/phronomy/tools/mcp_transport_registry.rb +210 -0
  168. data/lib/phronomy/tools/vector_search.rb +28 -5
  169. data/lib/phronomy/tracing/automatic.rb +8 -1
  170. data/lib/phronomy/version.rb +1 -1
  171. data/lib/phronomy/workflow/execution/workflow.rb +34 -0
  172. data/lib/phronomy/workflow/execution/workflow_execution_environment.rb +1 -0
  173. data/lib/phronomy/workflow/execution/workflow_execution_registry.rb +106 -7
  174. data/lib/phronomy/workflow/execution/workflow_runner.rb +53 -108
  175. data/lib/phronomy/workflow/execution/workflow_terminal_operation.rb +489 -0
  176. data/lib/phronomy/workflow/execution/workflow_terminal_persistence.rb +81 -0
  177. data/lib/phronomy/workflow/execution/workflow_terminal_recovery.rb +38 -0
  178. data/lib/phronomy/workflow/execution/workflow_terminal_recovery_error.rb +34 -0
  179. data/lib/phronomy/workflow/execution/workflow_terminal_recovery_result.rb +56 -0
  180. data/lib/phronomy/workflow/runtime_binding/workflow_engine_environment.rb +10 -1
  181. data/lib/phronomy/workflow/runtime_binding/workflow_task_session.rb +3 -1
  182. data/lib/phronomy/workflow/runtime_binding/workflow_terminal_policy.rb +2 -1
  183. data/scripts/api_snapshot.rb +46 -1
  184. data/sig/_private/phronomy/multi_agent/team_cancellation_request.rbs +19 -0
  185. data/sig/phronomy/agent/approval_recovery.rbs +54 -0
  186. data/sig/phronomy/agent/dispatch_recovery.rbs +49 -0
  187. data/sig/phronomy/agent/execution_contract.rbs +4 -1
  188. data/sig/phronomy/agent/execution_environment.rbs +2 -0
  189. data/sig/phronomy/agent/initial_preparation_recovery.rbs +48 -0
  190. data/sig/phronomy/agent/outcome_recovery.rbs +51 -0
  191. data/sig/phronomy/agent/store.rbs +1 -0
  192. data/sig/phronomy/agent/terminal_persistence_evidence.rbs +111 -0
  193. data/sig/phronomy/agent/terminal_recovery.rbs +46 -0
  194. data/sig/phronomy/agent.rbs +36 -0
  195. data/sig/phronomy/engine/concurrency/timer_bindings.rbs +2 -0
  196. data/sig/phronomy/engine/execution_binding.rbs +2 -1
  197. data/sig/phronomy/engine/execution_receiver.rbs +4 -0
  198. data/sig/phronomy/engine/observability.rbs +22 -1
  199. data/sig/phronomy/execution/execution.rbs +16 -1
  200. data/sig/phronomy/execution/execution_receiver.rbs +12 -0
  201. data/sig/phronomy/execution/execution_retirement.rbs +18 -0
  202. data/sig/phronomy/execution/stopped_terminal_custody.rbs +19 -0
  203. data/sig/phronomy/handoff.rbs +1 -0
  204. data/sig/phronomy/multi_agent/handoff_finalization_error.rbs +18 -0
  205. data/sig/phronomy/multi_agent/store.rbs +1 -1
  206. data/sig/phronomy/multi_agent/team_cancellation_delivery_error.rbs +9 -0
  207. data/sig/phronomy/persistence_composition.rbs +2 -1
  208. data/sig/phronomy/storage/contracts.rbs +2 -1
  209. data/sig/phronomy/tools/mcp.rbs +27 -0
  210. data/sig/phronomy/workflow.rbs +42 -0
  211. metadata +104 -2
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # Retains the completed record prefix when a native final text conversion
6
+ # raised. Only the original owner can bind independently known text to it.
7
+ # @api private
8
+ class OutcomeTextProjection
9
+ class Required < StandardError
10
+ attr_reader :projection
11
+
12
+ def initialize(projection)
13
+ @projection = projection
14
+ super("Native outcome requires its independently known text")
15
+ end
16
+ end
17
+
18
+ attr_reader :operation, :state, :records, :calls, :writes
19
+
20
+ def initialize(committer:, operation:, state:, records:, calls:, writes:)
21
+ @committer, @state = committer, state
22
+ @operation = operation.with(wiring: nil, runtime_snapshot: nil,
23
+ terminal_view: operation.terminal_view.with(output: nil, response: nil, output_contract: nil))
24
+ @records, @calls, @writes = records.dup.freeze, calls.dup.freeze, writes.dup.freeze
25
+ end
26
+
27
+ def bind(before:, state_data:, participation:, persistence:, participant:)
28
+ raise Phronomy::Error, "Outcome text preparation is already bound" if @before
29
+ # Binding the failed field must not consult a failing backend. Only
30
+ # the original retained owner can later materialize this local prefix.
31
+ @store = persistence
32
+ @participant = participant
33
+ @before = Phronomy::Values::Immutable.copy(before)
34
+ @state_data = Phronomy::Values::Immutable.copy(state_data)
35
+ @participation = Phronomy::Values::Immutable.copy(participation)
36
+ self
37
+ end
38
+
39
+ def missing? = @text.nil?
40
+
41
+ # Called under the owner's existing mutex, outside the recovery worker lane.
42
+ def supply(value)
43
+ raise ArgumentError, "A different text fact is already bound to this outcome" if @text && @text != value
44
+ @text ||= value
45
+ true
46
+ end
47
+
48
+ # Called only by the retained owner's serialized recovery worker. Fixed
49
+ # IDs and encoded content remain in the prefix and the completed suffix.
50
+ def materialize(persistence:)
51
+ unless persistence.equal?(@store)
52
+ raise Phronomy::Persistence::StateConflictError, "Outcome text belongs to another Store"
53
+ end
54
+ raise Phronomy::Error, "Native outcome requires its independently known text" unless @text
55
+ unless @preparation
56
+ raise @suffix_error if @suffix_error
57
+ raise Phronomy::Error, "Outcome text preparation did not finish" if @suffix_started
58
+ @suffix_started = true
59
+ begin
60
+ plan = @committer.send(:finish_supplied_output, self, @text)
61
+ after = plan.after(before: @before, state_data: @state_data, participation: @participation)
62
+ @preparation = OutcomePreparation.new({before: @before, after: after, result: plan.outcome}, plan.writes,
63
+ journal_records: plan.journal_records, persistence: persistence, participant: @participant)
64
+ rescue Exception => error # rubocop:disable Lint/RescueException
65
+ @suffix_error = error
66
+ raise
67
+ end
68
+ end
69
+ @preparation.materialize(persistence: persistence)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # Detached Agent outcome records and content, prepared before backend writes.
6
+ # This is a content-only overlay, not a transaction or a replayable callback.
7
+ # @api private
8
+ class OutcomeWritePlan
9
+ class Contents < Phronomy::ContentStore::Base
10
+ attr_reader :writes
11
+
12
+ def initialize(contents, write_class:)
13
+ @contents, @write_class = contents, write_class
14
+ @values, @writes = {}, []
15
+ end
16
+
17
+ def put(bytes, canonicalization_version:)
18
+ value = String(bytes).b.dup.freeze
19
+ version = Integer(canonicalization_version)
20
+ reference = content_id_for(value).freeze
21
+ @values[reference] = value
22
+ @writes << @write_class.new(reference: reference, bytes: value, canonicalization_version: version)
23
+ reference
24
+ end
25
+
26
+ def fetch(reference) = (@values[reference] || @contents.fetch(reference)).dup
27
+ def exist?(reference) = @values.key?(reference) || @contents.exist?(reference)
28
+ end
29
+
30
+ ContentView = Data.define(:contents)
31
+ attr_reader :outcome, :writes, :journal_records
32
+
33
+ def initialize(outcome:, writes:, journal_records: nil)
34
+ @outcome, @writes = outcome, writes.dup.freeze
35
+ @journal_records = journal_records&.dup&.freeze
36
+ freeze
37
+ end
38
+
39
+ def after(before:, state_data:, participation:)
40
+ Phronomy::Values::Immutable.copy([
41
+ [outcome.root.to_h, outcome.execution.to_h, outcome.appended_records.map(&:to_h),
42
+ state_data, before.fetch(0).fetch(4)],
43
+ participation
44
+ ])
45
+ end
46
+ end
47
+ end
48
+ end
@@ -14,7 +14,8 @@ module Phronomy
14
14
  tx:,
15
15
  snapshot:,
16
16
  context_candidate:,
17
- agent_root:
17
+ agent_root:,
18
+ &diagnostic
18
19
  )
19
20
  root = agent_root
20
21
  records = []
@@ -29,10 +30,12 @@ module Phronomy
29
30
  output_ref = assistant_output_ref(tx, outcome)
30
31
  assistant_ref = assistant_message_ref(tx, outcome)
31
32
  error_ref = if call_error
32
- tx.contents.put_json(Phronomy::Error.diagnostic(call_error))
33
+ value = diagnostic ? diagnostic.call(call_error) : Phronomy::Error.diagnostic(call_error)
34
+ tx.contents.put_json(value)
33
35
  end
34
- usage_ref = if outcome && outcome.usage.to_h.values.any? { |value| !value.nil? }
35
- tx.contents.put_json(json_value(outcome.usage.to_h))
36
+ usage = outcome&.usage&.to_h
37
+ usage_ref = if usage&.any? { |_name, value| !value.nil? }
38
+ tx.contents.put_json(json_value(usage))
36
39
  end
37
40
  call = LLMCallRecord.new(
38
41
  llm_call_id: llm_call_id,
@@ -0,0 +1,280 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module Phronomy
6
+ module Agent
7
+ # Opt-in, process-local proof for captured Agent outcome writes. Standalone
8
+ # terminal facts and native final/wait outcomes share exact persistence replay.
9
+ # This value owns neither the original observer nor the live execution.
10
+ # The caller must retain it and use the original Persistence only after the
11
+ # previous backend transaction has settled and authoritative reads apply.
12
+ # Native writes include only the captured parent extension and retention
13
+ # changes. Later child progress does not invalidate a committed parent fact.
14
+ # @api private
15
+ class TerminalPersistenceEvidence
16
+ ContentWrite = Data.define(:reference, :bytes, :canonicalization_version)
17
+ # Replay facts and the byte-derived observation do not retain the owner's
18
+ # semantic result, original exception or live approval request.
19
+ Outcome = Data.define(:type, :execution, :root, :appended_records, :execution_observation)
20
+ Result = Data.define(:disposition, :evidence, :original_error, :read_error)
21
+
22
+ # Complete intended values survive a failed proof constructor. Rebuilding
23
+ # this proof uses the native read-only evidence adapter and writes nothing.
24
+ # It does not rerun participant commit, encoding or output validation.
25
+ class CapturedInput
26
+ attr_reader :values
27
+
28
+ def initialize(values, writes, persistence:, participant:)
29
+ @values = {before: Phronomy::Values::Immutable.copy(values.fetch(:before)),
30
+ after: Phronomy::Values::Immutable.copy(values.fetch(:after)),
31
+ result: values.fetch(:result)}.freeze
32
+ @writes = writes.dup.freeze
33
+ @outcome = TerminalPersistenceEvidence.detached_outcome(@values, @writes)
34
+ @identity, @participant = persistence.execution_evidence_identity, participant
35
+ freeze
36
+ end
37
+
38
+ def materialize(persistence:)
39
+ unless persistence.execution_evidence_identity.equal?(@identity)
40
+ raise Phronomy::Persistence::StateConflictError, "Agent outcome proposal belongs to another Store"
41
+ end
42
+ TerminalPersistenceEvidence.capture_values(@values, @writes, persistence: persistence,
43
+ participant: @participant, outcome: @outcome)
44
+ end
45
+ end
46
+
47
+ # Transaction-local recorder; only detached ContentWrite values escape.
48
+ class RecordingContents < Phronomy::ContentStore::Base
49
+ def initialize(contents, writes)
50
+ @contents, @writes = contents, writes
51
+ end
52
+
53
+ def put(bytes, canonicalization_version:)
54
+ value = String(bytes).b.dup.freeze
55
+ version = Integer(canonicalization_version)
56
+ reference = @contents.put(value, canonicalization_version: version)
57
+ @writes << ContentWrite.new(reference: reference.to_s.dup.freeze,
58
+ bytes: value, canonicalization_version: version)
59
+ reference
60
+ end
61
+
62
+ def fetch(reference) = @contents.fetch(reference)
63
+ def exist?(reference) = @contents.exist?(reference)
64
+ end
65
+
66
+ RecordingScope = Data.define(:contents, :agents, :executions, :journals)
67
+
68
+ # Decode only already captured bytes, using the ordinary content format.
69
+ # Missing input cannot be replaced by a new read or a live domain value.
70
+ class CapturedContents < Phronomy::ContentStore::Base
71
+ def initialize(writes)
72
+ @writes = writes
73
+ end
74
+
75
+ def fetch(reference)
76
+ write = @writes.find { |candidate| candidate.reference == reference }
77
+ unless write
78
+ raise Phronomy::ContentStore::IntegrityError, "Agent outcome observation has no captured content: #{reference}"
79
+ end
80
+ write.bytes.dup
81
+ end
82
+ end
83
+ private_constant :RecordingContents, :RecordingScope, :CapturedContents
84
+
85
+ attr_reader :before, :after, :outcome, :content_writes, :attempt_id, :agent_id, :execution_id
86
+
87
+ def self.recording_scope(records, writes)
88
+ RecordingScope.new(contents: RecordingContents.new(records.contents, writes),
89
+ agents: records.agents, executions: records.executions, journals: records.journals)
90
+ end
91
+
92
+ def self.capture(change, writes, persistence:, participant: nil)
93
+ values = change.captured_evidence
94
+ return unless values
95
+ capture_values(values, writes, persistence: persistence, participant: participant)
96
+ end
97
+
98
+ def self.capture_values(values, writes, persistence:, participant: nil, outcome: nil)
99
+ participation = if participant&.respond_to?(:capture_outcome_recovery, true)
100
+ participant.__send__(:capture_outcome_recovery, values: values, persistence: persistence)
101
+ end
102
+ intended = values.fetch(:result)
103
+ native_wait = participation && %i[coordination_wait suspended].include?(intended.type)
104
+ return unless intended.execution.terminal? || native_wait
105
+ participant_free = values.fetch(:before).fetch(1).nil? && values.fetch(:after).fetch(1).nil?
106
+ return unless participation || participant_free
107
+ new(values, writes, persistence.execution_evidence_identity, participation, outcome: outcome)
108
+ end
109
+
110
+ def self.detached_outcome(values, writes)
111
+ intended = values.fetch(:result)
112
+ Outcome.new(type: intended.type, execution: intended.execution,
113
+ root: intended.root, appended_records: intended.appended_records.dup.freeze,
114
+ execution_observation: execution_observation(intended, writes,
115
+ cancellation_requested: values.fetch(:after).fetch(0).fetch(4)))
116
+ end
117
+
118
+ # An observation describes encoded values, not whether their transaction
119
+ # committed. Retained owners publish it only after positive confirmation.
120
+ def self.execution_observation(intended, writes, cancellation_requested: false)
121
+ execution = intended.execution
122
+ return unless execution.terminal?
123
+ contents = CapturedContents.new(writes)
124
+ result = if execution.result_ref
125
+ (execution.metadata["result_format"] == "json") ? contents.fetch_json(execution.result_ref) : contents.fetch_text(execution.result_ref)
126
+ end
127
+ ExecutionObservation.new(agent_id: execution.agent_id, execution_id: execution.execution_id,
128
+ status: execution.status, result: result,
129
+ raw_output: execution.metadata["raw_output_ref"] && contents.fetch_text(execution.metadata.fetch("raw_output_ref")),
130
+ error: execution.error_ref && contents.fetch_json(execution.error_ref),
131
+ reservation: execution.metadata["reservation"] && ReservedExecution.new(agent_id: execution.agent_id,
132
+ execution_id: execution.execution_id, correlation: execution.metadata["reservation"]),
133
+ transfer_receipt: execution.metadata["transfer_receipt"],
134
+ cancellation_requested: execution.metadata["cancellation_requested"] == true || cancellation_requested == true)
135
+ end
136
+
137
+ def initialize(values, writes, persistence_identity, participation, outcome: nil)
138
+ @persistence_identity, @participation = persistence_identity, participation
139
+ @before, @after = values.fetch(:before), values.fetch(:after)
140
+ intended = values.fetch(:result)
141
+ @agent_id = intended.execution.agent_id
142
+ @execution_id = intended.execution.execution_id
143
+ @journal_position = before.fetch(0).fetch(0).fetch("journal_position")
144
+ @outcome = outcome || self.class.detached_outcome(values, writes)
145
+ @content_writes = writes.dup.freeze
146
+ @attempt_id = SecureRandom.uuid.freeze
147
+ freeze
148
+ end
149
+ private_class_method :new
150
+
151
+ def reconcile(persistence:, original_error: nil, stopped_outcome: false)
152
+ require_persistence!(persistence)
153
+ if stopped_outcome && !@participation.respond_to?(:stopped_agent_ids)
154
+ raise Phronomy::Error, "stopped Agent outcome requires native participant evidence"
155
+ end
156
+ disposition = persistence.coordinator.atomic do |scope|
157
+ require_outer_scope!(scope)
158
+ ids = stopped_outcome ? @participation.stopped_agent_ids : guarded_agent_ids(persistence, scope)
159
+ persistence.guard_agents(scope, agent_ids: ids)
160
+ classify(persistence, scope).tap do |observed|
161
+ if stopped_outcome && observed == :committed
162
+ @participation.assert_stopped_terminal!(persistence: persistence, scope: scope)
163
+ end
164
+ end
165
+ end
166
+ result(disposition, original_error: original_error)
167
+ rescue => error
168
+ result(:unknown, original_error: original_error, read_error: error)
169
+ end
170
+
171
+ # This repeats only the already encoded persistence writes. It never
172
+ # reconstructs a terminal transition or invokes domain/provider callbacks.
173
+ def retry(persistence:, original_error: nil)
174
+ require_persistence!(persistence)
175
+ retry_error = nil
176
+ disposition = persistence.coordinator.atomic do |scope|
177
+ require_outer_scope!(scope)
178
+ persistence.guard_agents(scope, agent_ids: guarded_agent_ids(persistence, scope))
179
+ current = classify(persistence, scope)
180
+ next current unless current == :not_committed
181
+ begin
182
+ @participation&.assert_retryable!(persistence: persistence, scope: scope)
183
+ rescue => error
184
+ retry_error = error
185
+ next :not_committed
186
+ end
187
+ @participation&.replay(persistence: persistence, scope: scope)
188
+ persistence.participate(scope) { |records| replay(records) }
189
+ unless classify(persistence, scope) == :committed
190
+ raise Phronomy::Persistence::StateConflictError, "Retried Agent terminal state differs from its captured proof"
191
+ end
192
+ :committed
193
+ end
194
+ result(disposition, original_error: original_error, read_error: retry_error)
195
+ rescue => error
196
+ # The retry uses the same intended bytes and record identities; another
197
+ # lost response therefore remains classifiable against this same proof.
198
+ reconcile(persistence: persistence, original_error: error)
199
+ end
200
+
201
+ private
202
+
203
+ # The native caller owns the real outer transaction and ordered guards.
204
+ # Compare the staged pre-state before applying the exact captured writes.
205
+ # Initial native planning already made the domain decision under those
206
+ # guards; later retry separately rechecks current continuation authority.
207
+ def apply_prepared_in(persistence:, scope:)
208
+ require_persistence!(persistence)
209
+ require_outer_scope!(scope)
210
+ unless classify(persistence, scope) == :not_committed
211
+ raise Phronomy::Persistence::StateConflictError, "Prepared Agent outcome no longer matches its pre-state"
212
+ end
213
+ @participation&.replay(persistence: persistence, scope: scope)
214
+ persistence.participate(scope) { |records| replay(records) }
215
+ unless classify(persistence, scope) == :committed
216
+ raise Phronomy::Persistence::StateConflictError, "Prepared Agent outcome differs from its intended state"
217
+ end
218
+ nil
219
+ end
220
+
221
+ def guarded_agent_ids(persistence, scope)
222
+ @participation ? @participation.guarded_agent_ids(persistence: persistence, scope: scope) : [agent_id]
223
+ end
224
+
225
+ def require_persistence!(persistence)
226
+ return if persistence.execution_evidence_identity.equal?(@persistence_identity)
227
+ raise Phronomy::Persistence::StateConflictError, "Agent terminal evidence belongs to another Store"
228
+ end
229
+
230
+ def require_outer_scope!(scope)
231
+ return if scope.outermost?
232
+ raise Phronomy::Persistence::TransactionError, "Agent terminal evidence requires its own outer transaction"
233
+ end
234
+
235
+ def classify(persistence, scope)
236
+ snapshot = [ExecutionChange.read_agent_evidence(store: persistence,
237
+ agent_id: agent_id, execution_id: execution_id,
238
+ journal_position: @journal_position, scope: scope),
239
+ @participation&.read(persistence: persistence, scope: scope)]
240
+ head = persistence.participate(scope) { |records| records.journals.head(agent_id) }
241
+ return :unknown unless head == snapshot.fetch(0).fetch(0).fetch("journal_position")
242
+ disposition = Phronomy::Persistence::SaveOutcome.compare(before: before, after: after) { snapshot }.disposition
243
+ if disposition == :committed
244
+ @participation&.assert_content!(persistence: persistence, scope: scope)
245
+ persistence.participate(scope) do |records|
246
+ content_writes.each do |write|
247
+ unless records.contents.fetch(write.reference) == write.bytes
248
+ raise Phronomy::ContentStore::IntegrityError, "Agent terminal content differs from its captured bytes"
249
+ end
250
+ end
251
+ end
252
+ end
253
+ disposition
254
+ end
255
+
256
+ def replay(records)
257
+ content_writes.each do |write|
258
+ reference = records.contents.put(write.bytes, canonicalization_version: write.canonicalization_version)
259
+ unless reference == write.reference
260
+ raise Phronomy::ContentStore::IntegrityError, "Agent terminal content reference changed during retry"
261
+ end
262
+ end
263
+ unless outcome.appended_records.empty?
264
+ records.journals.append(agent_id, expected_position: @journal_position, records: outcome.appended_records)
265
+ end
266
+ records.executions.save(execution_id,
267
+ expected_revision: before.fetch(0).fetch(1).fetch("execution_revision"), execution: outcome.execution)
268
+ unless outcome.root.to_h == before.fetch(0).fetch(0)
269
+ records.agents.save(agent_id,
270
+ expected_revision: before.fetch(0).fetch(0).fetch("agent_revision"), root: outcome.root)
271
+ end
272
+ end
273
+
274
+ def result(disposition, original_error: nil, read_error: nil)
275
+ Result.new(disposition: disposition, evidence: self,
276
+ original_error: original_error, read_error: read_error)
277
+ end
278
+ end
279
+ end
280
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # Actual-terminal specialization of the shared retained outcome mechanism.
6
+ # Existing terminal capabilities never expose coordination or approval waits.
7
+ # @api private
8
+ class TerminalRecoveryOperation < OutcomeRecoveryOperation
9
+ def recovery_kind = :terminal
10
+
11
+ private
12
+
13
+ def read_stopped_terminal(retry_save:)
14
+ evidence, store, failure = stopped_read_context
15
+ observation = evidence&.outcome&.execution_observation
16
+ unless observation&.terminal? && !observation.reservation && !@command.wiring[:phronomy_execution_participant]
17
+ raise Phronomy::Error, "stopped Agent transfer requires complete standalone terminal evidence"
18
+ end
19
+ proof = if retry_save
20
+ @registry.verify_stopped_terminal_transfer!(self)
21
+ evidence.retry(persistence: store, original_error: failure)
22
+ else
23
+ evidence.reconcile(persistence: store, original_error: failure)
24
+ end
25
+ stopped_terminal_receipt(evidence, proof)
26
+ end
27
+
28
+ def recovery_class = TerminalRecovery
29
+ def recovery_result_class = TerminalRecoveryResult
30
+ def recovery_error_class = TerminalRecoveryError
31
+ def receipt_details(_saved_outcome, _approval_request) = {}
32
+
33
+ def apply_to_owner(coordinator, outcome)
34
+ coordinator.send(:apply_retained_terminal_on_event_loop, self, outcome)
35
+ end
36
+ end
37
+ end
38
+ end