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
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../../execution/concurrency/worker_input_restricted"
4
+ require "digest"
4
5
 
5
6
  module Phronomy
6
7
  module Agent
@@ -15,6 +16,8 @@ module Phronomy
15
16
  :root, :execution, :approval_request_id, :approved, :tool_batch_snapshot
16
17
  )
17
18
  Result = Data.define(:execution, :root)
19
+ UnwrittenProof = Data.define(:before)
20
+ private_constant :UnwrittenProof
18
21
 
19
22
  # @api private
20
23
  def initialize(agent_id:, persistence:)
@@ -29,8 +32,163 @@ module Phronomy
29
32
  persist_decision(execution, operation.root, request, operation.approved)
30
33
  end
31
34
 
35
+ # Captures one bounded decision without interpreting an ambiguous
36
+ # response. The execution owner retains the proof and decides when to read.
37
+ # @api private
38
+ def commit_retained(operation, on_evidence: nil, on_outcome: nil, parent_evidence: nil, participant: nil, on_write_started: nil)
39
+ request = validated_approval_request(operation)
40
+ validate_retained_operation!(operation, participant: participant)
41
+ parent_evidence&.validate_target!(persistence: @persistence, operation: operation)
42
+ native_evidence = capture_native_evidence(operation, participant)
43
+ evidence = nil
44
+ result = persist_retained_decision(operation, request,
45
+ parent_evidence: parent_evidence, native_evidence: native_evidence,
46
+ on_write_started: on_write_started) { |captured| evidence = captured }
47
+ rescue => original_error
48
+ begin
49
+ publish_retained_evidence(evidence, on_evidence, on_outcome)
50
+ rescue
51
+ # A collector cannot replace the original persistence failure.
52
+ nil
53
+ end
54
+ raise original_error
55
+ else
56
+ publish_retained_evidence(evidence, on_evidence, on_outcome)
57
+ result
58
+ end
59
+
60
+ # The original worker positively reported that no persistence write began.
61
+ # A later explicit observation may prove and restore that exact live wait,
62
+ # but may neither write nor turn the old approval into a new decision.
63
+ # @api private
64
+ def observe_unwritten(operation, parent_evidence: nil, participant: nil, original_error: nil)
65
+ validated_approval_request(operation)
66
+ validate_retained_operation!(operation, participant: participant)
67
+ parent_evidence&.validate_target!(persistence: @persistence, operation: operation)
68
+ native_evidence = capture_native_evidence(operation, participant)
69
+ before = continuation_error = nil
70
+ @persistence.coordinator.atomic do |scope|
71
+ ApprovalPersistenceEvidence.require_outer_scope!(scope)
72
+ guard_approval(scope, parent_evidence, native_evidence)
73
+ parent_evidence&.assert_current!(persistence: @persistence, scope: scope)
74
+ before = ApprovalPersistenceEvidence.read(persistence: @persistence,
75
+ agent_id: @agent_id, execution_id: operation.execution_id,
76
+ journal_position: operation.root.journal_position, scope: scope, parent_evidence: parent_evidence)
77
+ validate_retained_before!(operation, before)
78
+ parent_evidence&.assert_child_before!(before)
79
+ begin
80
+ native_evidence&.assert_current!(persistence: @persistence, scope: scope)
81
+ rescue => error
82
+ continuation_error = error
83
+ end
84
+ end
85
+ ApprovalPersistenceEvidence::Result.new(disposition: :not_committed,
86
+ evidence: UnwrittenProof.new(before: before), original_error: original_error,
87
+ read_error: nil, continuation_error: continuation_error)
88
+ rescue => error
89
+ ApprovalPersistenceEvidence::Result.new(disposition: :unknown, evidence: nil,
90
+ original_error: original_error, read_error: error)
91
+ end
92
+
32
93
  private
33
94
 
95
+ def capture_native_evidence(operation, participant)
96
+ evidence = participant&.__send__(:capture_approval_recovery,
97
+ execution: operation.execution, persistence: @persistence)
98
+ if participant && !evidence
99
+ raise ArgumentError, "Approval participant did not supply detached authority"
100
+ end
101
+ evidence
102
+ end
103
+
104
+ def guard_approval(scope, parent_evidence, native_evidence)
105
+ ids = parent_evidence ? parent_evidence.agent_ids : [@agent_id]
106
+ ids += native_evidence.guarded_agent_ids(persistence: @persistence, scope: scope) if native_evidence
107
+ @persistence.guard_agents(scope, agent_ids: ids.uniq)
108
+ end
109
+
110
+ def validate_retained_operation!(operation, participant:)
111
+ execution = operation.execution
112
+ unless operation.root.agent_id == @agent_id && execution.agent_id == @agent_id &&
113
+ operation.execution_id == execution.execution_id &&
114
+ operation.expected_execution_revision == execution.execution_revision &&
115
+ execution.status == :suspended && !operation.approval_request_id.empty? &&
116
+ [true, false].include?(operation.approved)
117
+ raise ArgumentError, "Retained approval does not match its suspended execution"
118
+ end
119
+ supported = participant&.respond_to?(:approval_recovery_supported?, true) &&
120
+ participant.__send__(:approval_recovery_supported?, execution: execution, persistence: @persistence)
121
+ if (participant && !supported) || (execution.metadata["execution_extension"] && !supported) ||
122
+ execution.metadata["cancellation_requested"]
123
+ raise ArgumentError, "Retained approval requires an uncancelled supported execution"
124
+ end
125
+ end
126
+
127
+ def persist_retained_decision(operation, request, parent_evidence:, native_evidence:, on_write_started:)
128
+ bytes = Phronomy::CanonicalJSON.dump(
129
+ "approval_request_id" => operation.approval_request_id,
130
+ "approved" => operation.approved
131
+ ).b.freeze
132
+ result = scope = nil
133
+ @persistence.transaction do |records, current_scope|
134
+ scope = current_scope
135
+ ApprovalPersistenceEvidence.require_outer_scope!(scope)
136
+ guard_approval(scope, parent_evidence, native_evidence)
137
+ parent_evidence&.assert_current!(persistence: @persistence, scope: scope)
138
+ native_evidence&.assert_current!(persistence: @persistence, scope: scope)
139
+ before = ApprovalPersistenceEvidence.read(persistence: @persistence,
140
+ agent_id: @agent_id, execution_id: operation.execution_id,
141
+ journal_position: operation.root.journal_position, scope: scope, parent_evidence: parent_evidence)
142
+ validate_retained_before!(operation, before)
143
+ parent_evidence&.assert_child_before!(before)
144
+ decision_ref = "sha256:#{Digest::SHA256.hexdigest(bytes)}"
145
+ decision = approval_decision(operation.execution, operation.root, decision_ref)
146
+ execution = stage_recovery_snapshot(operation)
147
+ updated = resumed_execution(execution, request, operation.approved, decision)
148
+ root = resumed_root(operation.root)
149
+ result = Result.new(execution: updated, root: root)
150
+ evidence = ApprovalPersistenceEvidence.capture(persistence: @persistence,
151
+ before: before, outcome: result, decision: decision, decision_bytes: bytes,
152
+ parent_evidence: parent_evidence, native_evidence: native_evidence)
153
+ yield evidence
154
+ on_write_started&.call
155
+ saved_ref = records.contents.put(bytes, canonicalization_version: Phronomy::CanonicalJSON::VERSION)
156
+ unless saved_ref == decision_ref
157
+ raise Phronomy::ContentStore::IntegrityError, "Approval content differs from its captured identity"
158
+ end
159
+ records.executions.save(execution.execution_id,
160
+ expected_revision: operation.expected_execution_revision, execution: updated)
161
+ records.agents.save(@agent_id,
162
+ expected_revision: operation.root.agent_revision, root: root)
163
+ after = ApprovalPersistenceEvidence.read(persistence: @persistence,
164
+ agent_id: @agent_id, execution_id: operation.execution_id,
165
+ journal_position: operation.root.journal_position, scope: scope, parent_evidence: parent_evidence)
166
+ unless after == evidence.after && records.contents.fetch(decision_ref) == bytes
167
+ raise Phronomy::Persistence::StateConflictError, "Approval write differs from its captured proof"
168
+ end
169
+ end
170
+ unless scope&.committed?
171
+ raise Phronomy::Persistence::TransactionError, "Approval decision has not committed its outer scope"
172
+ end
173
+ result
174
+ end
175
+
176
+ def validate_retained_before!(operation, before)
177
+ snapshot = before.fetch(:agent)
178
+ # Cancellation can win while this decision is already queued. Preserve
179
+ # its captured bit in both proofs; the owner must honor it on either path.
180
+ unless snapshot.fetch(0) == operation.root.to_h && snapshot.fetch(1) == operation.execution.to_h &&
181
+ before.fetch(:journal_head) == operation.root.journal_position && snapshot.fetch(2).empty?
182
+ raise Phronomy::Persistence::StateConflictError, "Suspended approval changed before its decision"
183
+ end
184
+ end
185
+
186
+ def publish_retained_evidence(evidence, collector, outcome_collector)
187
+ return unless evidence
188
+ outcome_collector&.call(evidence.outcome)
189
+ collector&.call(evidence)
190
+ end
191
+
34
192
  def validated_approval_request(operation)
35
193
  request = operation.execution.approval_request || {}
36
194
  request_id = request["id"] || request[:id]
@@ -65,6 +223,10 @@ module Phronomy
65
223
  "approval_request_id" => (request["id"] || request[:id]).to_s,
66
224
  "approved" => approved
67
225
  )
226
+ approval_decision(execution, root, decision_ref)
227
+ end
228
+
229
+ def approval_decision(execution, root, decision_ref)
68
230
  JournalRecord.new(
69
231
  agent_id: @agent_id,
70
232
  execution_id: execution.execution_id,
@@ -77,12 +239,7 @@ module Phronomy
77
239
  end
78
240
 
79
241
  def save_resumed_execution(tx, current, request, approved, decision)
80
- updated = current.with(
81
- status: :active,
82
- phase: :resuming,
83
- working_records: current.working_records + [decision],
84
- approval_request: request.merge("approved" => approved)
85
- )
242
+ updated = resumed_execution(current, request, approved, decision)
86
243
  tx.executions.save(
87
244
  current.execution_id,
88
245
  expected_revision: current.execution_revision,
@@ -92,10 +249,7 @@ module Phronomy
92
249
  end
93
250
 
94
251
  def save_resumed_root(tx, current)
95
- updated = current.with(
96
- agent_revision: current.agent_revision + 1,
97
- lifecycle_status: :active
98
- )
252
+ updated = resumed_root(current)
99
253
  tx.agents.save(
100
254
  current.agent_id,
101
255
  expected_revision: current.agent_revision,
@@ -103,6 +257,16 @@ module Phronomy
103
257
  )
104
258
  updated
105
259
  end
260
+
261
+ def resumed_execution(current, request, approved, decision)
262
+ current.with(status: :active, phase: :resuming,
263
+ working_records: current.working_records + [decision],
264
+ approval_request: request.merge("approved" => approved))
265
+ end
266
+
267
+ def resumed_root(current)
268
+ current.with(agent_revision: current.agent_revision + 1, lifecycle_status: :active)
269
+ end
106
270
  end
107
271
  end
108
272
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # Tracks only the progress messages crossing one dispatch handoff. A helper
6
+ # returning normally does not prove these messages were accepted. After the
7
+ # handoff seals, ordinary asynchronous delivery keeps its existing contract.
8
+ # @api private
9
+ class DispatchContinuationDelivery
10
+ PROGRESS_EVENTS = %i[
11
+ llm_setup_failed llm_failed llm_completed tool_setup_failed tool_dispatch_prepared
12
+ tool_failed tool_completed tool_cancelled tool_rejected tool_authorized tool_approval_required
13
+ ].freeze
14
+ Entry = Struct.new(:type, :payload, :state)
15
+ private_constant :PROGRESS_EVENTS, :Entry
16
+
17
+ def initialize(event_sink:)
18
+ @event_sink = event_sink
19
+ @mutex = Mutex.new
20
+ @entries = []
21
+ @sealed = @unknown = @retrying = false
22
+ end
23
+
24
+ def fsm_session_id = @event_sink.fsm_session_id
25
+
26
+ def post(type, payload)
27
+ unless PROGRESS_EVENTS.include?(type)
28
+ return @event_sink.post(type, payload)
29
+ end
30
+ captured = Phronomy::Values::Immutable.copy(payload)
31
+ entry = @mutex.synchronize do
32
+ next if @sealed
33
+ Entry.new(type, captured, :in_flight).tap { |value| @entries << value }
34
+ end
35
+ return @event_sink.post(type, payload) unless entry
36
+ deliver(entry)
37
+ end
38
+
39
+ # Only an explicit false establishes non-admission. Repeating this method
40
+ # resends those exact messages, never Provider/Tool work or unknown sends.
41
+ def retry_rejected!
42
+ acquired = @mutex.synchronize do
43
+ return true if @sealed
44
+ return false if @unknown || @retrying || @entries.any? { |entry| entry.state == :in_flight }
45
+ @retrying = true
46
+ end
47
+ loop do
48
+ entry = @mutex.synchronize do
49
+ next if @unknown
50
+ @entries.find { |value| value.state == :rejected }&.tap { |value| value.state = :in_flight }
51
+ end
52
+ break unless entry
53
+ return false unless deliver(entry).equal?(true)
54
+ end
55
+ @mutex.synchronize { !@unknown && @entries.empty? }
56
+ ensure
57
+ @mutex.synchronize { @retrying = false } if acquired
58
+ end
59
+
60
+ # Reservation of an in-flight post and sealing share one lock. No delegate
61
+ # call runs under that lock, including a retry with synchronous callbacks.
62
+ def finish_handoff?
63
+ @mutex.synchronize do
64
+ return true if @sealed
65
+ return false if @unknown || @retrying || !@entries.empty?
66
+ @sealed = true
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def deliver(entry)
73
+ accepted = @event_sink.post(entry.type, entry.payload)
74
+ @mutex.synchronize do
75
+ if accepted.equal?(true)
76
+ @entries.delete_if { |value| value.equal?(entry) }
77
+ elsif accepted.equal?(false)
78
+ entry.state = :rejected
79
+ else
80
+ entry.state = :unknown
81
+ @unknown = true
82
+ end
83
+ end
84
+ accepted
85
+ rescue Exception # rubocop:disable Lint/RescueException
86
+ # An accepted send may raise while returning its receipt. Never infer
87
+ # non-admission or replay from an exception, including control exits.
88
+ @mutex.synchronize do
89
+ entry.state = :unknown
90
+ @unknown = true
91
+ end
92
+ raise
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module Phronomy
6
+ module Agent
7
+ # Detached proof of one bounded pre-dispatch execution change. The proof
8
+ # does not authorize external dispatch or retain live Agent/participant state.
9
+ # Only the original Store may compare or repeat the captured execution save,
10
+ # after the prior backend attempt has settled and authoritative reads apply.
11
+ # @api private
12
+ class DispatchPersistenceEvidence
13
+ Result = Data.define(:disposition, :evidence, :original_error, :read_error, :continuation_error) do
14
+ def initialize(disposition:, evidence:, original_error:, read_error:, continuation_error: nil)
15
+ super
16
+ end
17
+ end
18
+
19
+ attr_reader :before, :after, :execution, :kind, :manifest, :manifest_ref,
20
+ :attempt_id, :agent_id, :execution_id
21
+
22
+ def self.capture(change, persistence:, kind:, manifest: nil, manifest_ref: nil, participant: nil)
23
+ values = change.captured_evidence
24
+ return unless values
25
+ participation = if participant&.respond_to?(:capture_dispatch_recovery, true)
26
+ participant.__send__(:capture_dispatch_recovery, values: values, persistence: persistence)
27
+ end
28
+ participant_free = values.fetch(:before).fetch(1).nil? && values.fetch(:after).fetch(1).nil?
29
+ return unless participation || participant_free
30
+ new(values, persistence.execution_evidence_identity, kind, manifest, manifest_ref, participation)
31
+ end
32
+
33
+ def initialize(values, persistence_identity, kind, manifest, manifest_ref, participation)
34
+ raise ArgumentError, "Unknown dispatch kind: #{kind.inspect}" unless %i[provider tool].include?(kind)
35
+ if kind == :provider && (!manifest || !manifest_ref)
36
+ raise ArgumentError, "Provider dispatch evidence requires its captured manifest"
37
+ end
38
+ @persistence_identity = persistence_identity
39
+ @before, @after = values.fetch(:before), values.fetch(:after)
40
+ @execution = values.fetch(:result)
41
+ @agent_id, @execution_id = execution.agent_id, execution.execution_id
42
+ @journal_position = before.fetch(0).fetch(0).fetch("journal_position")
43
+ @kind = kind
44
+ @participation = participation
45
+ @manifest = manifest && Phronomy::Context::LLMInputManifest.from_h(Phronomy::Values::Immutable.copy(manifest.to_h))
46
+ @manifest_ref = manifest_ref&.dup&.freeze
47
+ @attempt_id = SecureRandom.uuid.freeze
48
+ freeze
49
+ end
50
+ private_class_method :new
51
+
52
+ def participation? = !@participation.nil?
53
+
54
+ def reconcile(persistence:, original_error: nil)
55
+ require_persistence!(persistence)
56
+ continuation_error = nil
57
+ disposition = persistence.coordinator.atomic do |scope|
58
+ require_outer_scope!(scope)
59
+ persistence.guard_agents(scope, agent_ids: guarded_agent_ids(persistence, scope))
60
+ classify(persistence, scope).tap do |current|
61
+ continuation_error = continuation_error_in(persistence, scope) if current == :committed
62
+ end
63
+ end
64
+ result(disposition, original_error: original_error, continuation_error: continuation_error)
65
+ rescue => error
66
+ result(:unknown, original_error: original_error, read_error: error)
67
+ end
68
+
69
+ # Input preparation precedes the original dispatch transaction. An opt-in
70
+ # domain may also restore its captured opaque content and retention writes.
71
+ # Retry never repeats encoding, application hooks, Policy or external work.
72
+ def retry(persistence:, original_error: nil)
73
+ require_persistence!(persistence)
74
+ continuation_error = retry_error = nil
75
+ disposition = persistence.coordinator.atomic do |scope|
76
+ require_outer_scope!(scope)
77
+ persistence.guard_agents(scope, agent_ids: guarded_agent_ids(persistence, scope))
78
+ current = classify(persistence, scope)
79
+ if current != :not_committed
80
+ continuation_error = continuation_error_in(persistence, scope) if current == :committed
81
+ next current
82
+ end
83
+ begin
84
+ @participation&.assert_retryable!(persistence: persistence, scope: scope)
85
+ rescue => error
86
+ retry_error = error
87
+ next :not_committed
88
+ end
89
+ @participation&.replay(persistence: persistence, scope: scope)
90
+ persistence.participate(scope) do |records|
91
+ records.executions.save(execution_id,
92
+ expected_revision: before.fetch(0).fetch(1).fetch("execution_revision"), execution: execution)
93
+ end
94
+ unless classify(persistence, scope) == :committed
95
+ raise Phronomy::Persistence::StateConflictError, "Retried dispatch state differs from its captured proof"
96
+ end
97
+ continuation_error = continuation_error_in(persistence, scope)
98
+ :committed
99
+ end
100
+ result(disposition, original_error: original_error, read_error: retry_error,
101
+ continuation_error: continuation_error)
102
+ rescue => error
103
+ reconcile(persistence: persistence, original_error: error)
104
+ end
105
+
106
+ private
107
+
108
+ def guarded_agent_ids(persistence, scope)
109
+ @participation ? @participation.guarded_agent_ids(persistence: persistence, scope: scope) : [agent_id]
110
+ end
111
+
112
+ # A committed barrier is historical fact. Fresh domain authority is a
113
+ # separate condition for granting its still-unused continuation right.
114
+ def continuation_error_in(persistence, scope)
115
+ @participation&.assert_continuable!(persistence: persistence, scope: scope)
116
+ nil
117
+ rescue => error
118
+ error
119
+ end
120
+
121
+ def require_persistence!(persistence)
122
+ return if persistence.execution_evidence_identity.equal?(@persistence_identity)
123
+ raise Phronomy::Persistence::StateConflictError, "Dispatch evidence belongs to another Store"
124
+ end
125
+
126
+ def require_outer_scope!(scope)
127
+ return if scope.outermost?
128
+ raise Phronomy::Persistence::TransactionError, "Dispatch evidence requires its own outer transaction"
129
+ end
130
+
131
+ def classify(persistence, scope)
132
+ snapshot = [ExecutionChange.read_agent_evidence(store: persistence,
133
+ agent_id: agent_id, execution_id: execution_id,
134
+ journal_position: @journal_position, scope: scope),
135
+ @participation&.read(persistence: persistence, scope: scope)]
136
+ head = persistence.participate(scope) { |records| records.journals.head(agent_id) }
137
+ return :unknown unless head == snapshot.fetch(0).fetch(0).fetch("journal_position")
138
+ Phronomy::Persistence::SaveOutcome.compare(before: before, after: after) { snapshot }.disposition
139
+ end
140
+
141
+ def result(disposition, original_error:, read_error: nil, continuation_error: nil)
142
+ Result.new(disposition: disposition, evidence: self,
143
+ original_error: original_error, read_error: read_error, continuation_error: continuation_error)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -31,26 +31,44 @@ module Phronomy
31
31
  end
32
32
 
33
33
  # @api private
34
- def prepare_provider(operation)
34
+ def prepare_provider(operation, on_evidence: nil, on_outcome: nil, on_barrier_started: nil)
35
35
  staged = stage_provider_call(operation)
36
36
  recorded = encode_provider_records(operation, staged)
37
37
  IterationBudget.check!(recorded, default: @agent.class.max_iterations)
38
38
  assembler, prepared = prepare_provider_context(operation, recorded)
39
39
  committed, manifest, manifest_ref = commit_provider_preparation(
40
- operation, recorded, assembler, prepared
40
+ operation, recorded, assembler, prepared, on_evidence: on_evidence,
41
+ on_barrier_started: on_barrier_started
41
42
  )
42
- materialize_provider_result(committed.execution, manifest, manifest_ref)
43
+ result = materialize_provider_result(committed.execution, manifest, manifest_ref)
44
+ on_outcome&.call(result)
45
+ result
43
46
  end
44
47
 
45
48
  # @api private
46
- def prepare_tools(operation)
49
+ def prepare_tools(operation, on_evidence: nil, on_outcome: nil, on_barrier_started: nil)
47
50
  updated = @persistence.transaction do |tx|
48
51
  assert_local_durable_base!(tx, operation.root)
49
52
  build_tool_dispatch_execution(operation, tx)
50
53
  end
51
54
  change = ExecutionChange.new(agent: @agent, current: operation.execution,
52
55
  root: operation.root, kind: :dispatch, updated: updated, config: operation.invocation_config)
53
- ToolResult.new(execution: change.perform)
56
+ committed = perform_dispatch_change(change, operation, kind: :tool, collector: on_evidence,
57
+ on_barrier_started: on_barrier_started)
58
+ result = ToolResult.new(execution: committed)
59
+ on_outcome&.call(result)
60
+ result
61
+ end
62
+
63
+ # Materializes already captured input only after the owner has proved its
64
+ # dispatch barrier committed. No hook, Policy or business call is repeated.
65
+ # @api private
66
+ def recover_result(evidence)
67
+ if evidence.kind == :provider
68
+ materialize_provider_result(evidence.execution, evidence.manifest, evidence.manifest_ref)
69
+ else
70
+ ToolResult.new(execution: evidence.execution)
71
+ end
54
72
  end
55
73
 
56
74
  private
@@ -119,7 +137,7 @@ module Phronomy
119
137
  [assembler, prepared]
120
138
  end
121
139
 
122
- def commit_provider_preparation(operation, recorded, assembler, prepared)
140
+ def commit_provider_preparation(operation, recorded, assembler, prepared, on_evidence:, on_barrier_started:)
123
141
  manifest, manifest_ref = @persistence.transaction do |tx|
124
142
  assert_local_durable_base!(tx, operation.root)
125
143
  Phronomy::Context::Assembly.new.store(prepared, contents: tx.contents,
@@ -128,10 +146,48 @@ module Phronomy
128
146
  updated = execution_with_provider_manifest(recorded, manifest_ref)
129
147
  change = ExecutionChange.new(agent: @agent, current: operation.execution,
130
148
  root: operation.root, kind: :dispatch, updated: updated, config: operation.invocation_config)
131
- intended = ProviderResult.new(execution: change.perform, runtime_projection: nil, error: nil)
149
+ committed = perform_dispatch_change(change, operation, kind: :provider, collector: on_evidence,
150
+ manifest: manifest, manifest_ref: manifest_ref, on_barrier_started: on_barrier_started)
151
+ intended = ProviderResult.new(execution: committed, runtime_projection: nil, error: nil)
132
152
  [intended, manifest, manifest_ref]
133
153
  end
134
154
 
155
+ def perform_dispatch_change(change, operation, kind:, collector:, on_barrier_started:, manifest: nil, manifest_ref: nil)
156
+ capture = collector && retained_evidence_eligible?(operation)
157
+ participant = operation.invocation_config[:phronomy_execution_participant]
158
+ # Absence of a complete proof cannot establish non-admission. The owner
159
+ # separately records whether execution-change processing could start.
160
+ on_barrier_started&.call
161
+ result = change.perform
162
+ rescue => original_error
163
+ begin
164
+ publish_dispatch_evidence(change, kind, collector, manifest, manifest_ref, participant) if capture
165
+ rescue
166
+ # A failed collector cannot replace the original persistence error.
167
+ nil
168
+ end
169
+ raise original_error
170
+ else
171
+ publish_dispatch_evidence(change, kind, collector, manifest, manifest_ref, participant) if capture
172
+ result
173
+ end
174
+
175
+ def publish_dispatch_evidence(change, kind, collector, manifest, manifest_ref, participant)
176
+ evidence = DispatchPersistenceEvidence.capture(change, persistence: @agent.persistence,
177
+ kind: kind, manifest: manifest, manifest_ref: manifest_ref, participant: participant)
178
+ collector.call(evidence) if evidence
179
+ end
180
+
181
+ def retained_evidence_eligible?(operation)
182
+ return false unless operation.execution.status == :active
183
+ participant = operation.invocation_config[:phronomy_execution_participant]
184
+ return !operation.execution.metadata["execution_extension"] unless participant
185
+
186
+ participant.respond_to?(:dispatch_recovery_supported?, true) &&
187
+ participant.__send__(:dispatch_recovery_supported?,
188
+ execution: operation.execution, persistence: @agent.persistence)
189
+ end
190
+
135
191
  def execution_with_provider_manifest(recorded, manifest_ref)
136
192
  refs = Array(recorded.metadata["manifest_refs"]) + [manifest_ref]
137
193
  recorded.with(