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
@@ -36,6 +36,11 @@ module Phronomy
36
36
  # Starts or observes the reserved execution and returns its committed result.
37
37
  # Failed executions, including OutputError, fulfill the Task with status
38
38
  # and error details. Admission, recovery and observation errors reject it.
39
+ # ExecutionRejectedError identifies durable-context, input-extraction or
40
+ # preparation-metadata validation rejected before durable admission.
41
+ # It identifies this start request, not global absence; coordinating owners
42
+ # must observe the reservation under their guard before ending their run.
43
+ # @see file:docs/architecture/team-admission-rejection.md
39
44
  # @return [Phronomy::TaskResult] the exact execution result
40
45
  # @api public
41
46
  def start_reserved_async(reservation:, input:, config: {}, output_schema: nil, schema_delivery: nil)
@@ -275,11 +280,18 @@ module Phronomy
275
280
  reader.call(agent_id, parent_execution_id, child_execution_id, approval_id,
276
281
  parent_revision: parent_wait.revision, live_child: owner.agent)
277
282
  end
278
- wait = lookup.call(execution_id, approval_request_id)
279
- raise Phronomy::AgentBusyError, "No matching hosted child approval wait" unless wait
283
+ parent_evidence = ApprovalParentEvidence.capture(persistence: owner.agent.persistence,
284
+ parent_agent_id: agent_id, parent_execution_id: parent_execution_id,
285
+ parent_revision: parent_wait.revision, child_agent_id: owner.agent.agent_id) do
286
+ lookup.call(execution_id, approval_request_id)
287
+ end
288
+ wait = parent_evidence.wait
289
+ context = HostedApprovalContext.new(parent: self, child: owner.agent,
290
+ parent_wait: parent_wait, parent_evidence: parent_evidence, reader: reader)
280
291
  task = owner.coordinator.resume(wait.execution_id, approval_request_id: wait.approval_request_id, approved: approved,
281
- config: owner.agent._prepare_invocation_config(config, nil), parent_wait: parent_wait)
282
- HostedInvocation.new(self, parent_execution_id, task, resumed_from: wait, &lookup)
292
+ config: owner.agent._prepare_invocation_config(config, nil), parent_wait: parent_wait, hosted_context: context)
293
+ context.build_invocation(observation: task, detach_task: task) ||
294
+ HostedInvocation.new(self, parent_execution_id, task, resumed_from: wait)
283
295
  end
284
296
  end
285
297
 
@@ -294,7 +306,12 @@ module Phronomy
294
306
  end
295
307
 
296
308
  def _start_agent_operation(input, config:, mode:, listener:)
297
- config = __invocation_config(_snapshot_durable_context(config))
309
+ begin
310
+ config = __invocation_config(_snapshot_durable_context(config))
311
+ rescue => error
312
+ raise ExecutionRejectedError.from_error(error, agent_id: agent_id,
313
+ execution_id: config[:phronomy_exact_observers] && config[:phronomy_reserved_execution_id])
314
+ end
298
315
  @_phronomy_execution_wiring = config.dup.freeze
299
316
  approval = _approval_configuration_snapshot(nil)
300
317
  execution_coordinator_for(config).start(
@@ -229,7 +229,7 @@ module Phronomy
229
229
  listener_supplied = !on_event.nil? || !event_block.nil?
230
230
  materialized = false
231
231
 
232
- agent = OwnershipRegistry.current.load(key, expected_class: self) do |owner_runtime|
232
+ agent = OwnershipRegistry.current.load(key, expected_class: self) do |owner_runtime, before_install|
233
233
  materialized = true
234
234
  instance = __construct_owned_agent(
235
235
  owner_runtime,
@@ -241,7 +241,7 @@ module Phronomy
241
241
  on_event: on_event,
242
242
  &event_block
243
243
  )
244
- Phronomy::Agent::RecoveryCoordinator.new(instance).recover_on_load!
244
+ Phronomy::Agent::RecoveryCoordinator.new(instance).recover_on_load!(&before_install)
245
245
  instance
246
246
  end
247
247
 
@@ -258,6 +258,18 @@ module Phronomy
258
258
  agent
259
259
  end
260
260
 
261
+ # Observe the original unconfirmed create or purge without replaying it.
262
+ # Requires the same Store and concrete Agent definition in this Runtime.
263
+ # Returns nil when there is no retained identity operation to observe.
264
+ # @return [Symbol, nil] :committed, :not_committed, :superseded, :unknown, or nil
265
+ # @api public
266
+ def reconcile_identity(agent_id, persistence:)
267
+ if Phronomy::WaitPolicy.blocking_forbidden?
268
+ raise Phronomy::EventLoopReentrancyError, "Agent identity reconciliation requires an external thread"
269
+ end
270
+ OwnershipRegistry.existing_current&.reconcile_identity(agent_id, expected_class: self, persistence: persistence)
271
+ end
272
+
261
273
  # Returns only the process-local live Agent owner. Does not access
262
274
  # Persistence and returns nil when this Runtime has no live owner.
263
275
  def get(agent_id)
@@ -316,9 +328,9 @@ module Phronomy
316
328
  _class_tool_result_filters.each { |filter| derived.tool_result_filter(filter) }
317
329
  end
318
330
 
319
- def __construct_owned_agent(owner_runtime, reserved_agent_id, *args, **kwargs, &block)
331
+ def __construct_owned_agent(owner_runtime, reserved_agent_id, *args, identity_write: nil, **kwargs, &block)
320
332
  instance = allocate
321
- instance.send(:__prepare_runtime_owner!, owner_runtime, reserved_agent_id)
333
+ instance.send(:__prepare_runtime_owner!, owner_runtime, reserved_agent_id, identity_write: identity_write)
322
334
  instance.send(:initialize, *args, **kwargs, &block)
323
335
  instance
324
336
  end
@@ -412,8 +424,8 @@ module Phronomy
412
424
  end
413
425
 
414
426
  begin
415
- OwnershipRegistry.current.create(effective_agent_id, expected_class: self.class) do |owner_runtime|
416
- __prepare_runtime_owner!(owner_runtime, effective_agent_id)
427
+ OwnershipRegistry.current.create(effective_agent_id, expected_class: self.class) do |owner_runtime, write|
428
+ __prepare_runtime_owner!(owner_runtime, effective_agent_id, identity_write: write)
417
429
  initialize_owned_state(
418
430
  agent_id: effective_agent_id,
419
431
  context: context,
@@ -540,40 +552,36 @@ module Phronomy
540
552
  __assert_live_agent!
541
553
  runtime = @_phronomy_runtime_owner
542
554
  registry = OwnershipRegistry.for(runtime)
543
- token = registry.begin_purge(self)
544
- if runtime.existing_registry&.agent_execution_admitted?(agent_id)
545
- registry.abort_purge(self, token)
546
- raise Phronomy::AgentBusyError,
547
- "Agent #{agent_id.inspect} has a nonterminal top-level execution"
548
- end
549
-
550
- begin
551
- persistence.transaction do |tx|
552
- tx.executions.assert_idle!(agent_id)
553
- tx.guard_agent!(agent_id)
554
- unless tx.retentions.list(agent_id).empty?
555
- raise Phronomy::AgentBusyError, "Agent #{agent_id} is retained by durable coordination history"
555
+ write = IdentityWrite.new(agent_id: agent_id, kind: :purge, agent_class: self.class,
556
+ generation: @_phronomy_identity_generation)
557
+ Thread.handle_interrupt(Object => :never) do
558
+ token = registry.begin_purge(self)
559
+ disposition = :abort_purge
560
+ begin
561
+ if runtime.existing_registry&.agent_execution_admitted?(agent_id)
562
+ raise Phronomy::AgentBusyError,
563
+ "Agent #{agent_id.inspect} has a nonterminal top-level execution"
556
564
  end
557
- tx.cancellations.delete_for_agent(agent_id)
558
- tx.journals.delete(agent_id)
559
- tx.executions.delete_for_agent(agent_id)
560
- tx.agents.delete(agent_id)
561
- end
562
- rescue Phronomy::AgentBusyError,
563
- Phronomy::Persistence::ConflictError,
564
- Phronomy::Persistence::NotFoundError,
565
- Phronomy::Persistence::SerializationError,
566
- ArgumentError
567
- registry.abort_purge(self, token)
568
- raise
569
- rescue
570
- # Without F1 reconciliation support we cannot infer that a failed
571
- # durable delete did not commit. Keep the identity fail-closed.
572
- registry.leave_purge_uncertain(self, token)
573
- raise
574
- end
575
-
576
- registry.complete_purge(self, token)
565
+ disposition = :leave_purge_uncertain
566
+ Thread.handle_interrupt(Object => :immediate) { purge_durable_records!(write) }
567
+ disposition = :complete_purge
568
+ rescue Phronomy::AgentBusyError,
569
+ Phronomy::Persistence::ConflictError,
570
+ Phronomy::Persistence::NotFoundError,
571
+ Phronomy::Persistence::SerializationError,
572
+ ArgumentError
573
+ disposition = :abort_purge unless write.prepared?
574
+ raise
575
+ ensure
576
+ if disposition == :complete_purge || write.committed?
577
+ registry.complete_purge(self, token)
578
+ elsif write.rolled_back? || disposition == :abort_purge
579
+ registry.abort_purge(self, token)
580
+ else
581
+ registry.leave_purge_uncertain(self, token, write: write)
582
+ end
583
+ end
584
+ end
577
585
  true
578
586
  end
579
587
 
@@ -581,11 +589,26 @@ module Phronomy
581
589
  # methods; application code must use new/create/load/get/purge!.
582
590
  private
583
591
 
592
+ def purge_durable_records!(write)
593
+ write.perform(persistence) do |tx|
594
+ tx.executions.assert_idle!(agent_id)
595
+ tx.guard_agent!(agent_id)
596
+ unless tx.retentions.list(agent_id).empty?
597
+ raise Phronomy::AgentBusyError, "Agent #{agent_id} is retained by durable coordination history"
598
+ end
599
+ tx.cancellations.delete_for_agent(agent_id)
600
+ tx.journals.delete(agent_id)
601
+ tx.executions.delete_for_agent(agent_id)
602
+ tx.agents.delete(agent_id)
603
+ end
604
+ end
605
+
584
606
  # @api private
585
- def __prepare_runtime_owner!(runtime, reserved_agent_id)
607
+ def __prepare_runtime_owner!(runtime, reserved_agent_id, identity_write: nil)
586
608
  @_phronomy_runtime_owner = runtime
587
609
  @_phronomy_runtime_owner_state = :constructing
588
610
  @_phronomy_reserved_agent_id = reserved_agent_id.to_s.freeze
611
+ @_phronomy_identity_write = identity_write
589
612
  self
590
613
  end
591
614
 
@@ -614,7 +637,7 @@ module Phronomy
614
637
  # @api private
615
638
  def __restore_live_after_purge_abort!(runtime)
616
639
  unless @_phronomy_runtime_owner.equal?(runtime) &&
617
- @_phronomy_runtime_owner_state == :purging
640
+ %i[purging recovery_required].include?(@_phronomy_runtime_owner_state)
618
641
  raise Phronomy::Error, "Agent Runtime ownership purge rollback state is invalid"
619
642
  end
620
643
  @_phronomy_runtime_owner_state = :live
@@ -634,7 +657,7 @@ module Phronomy
634
657
  # @api private
635
658
  def __mark_purged!(runtime)
636
659
  unless @_phronomy_runtime_owner.equal?(runtime) &&
637
- @_phronomy_runtime_owner_state == :purging
660
+ %i[purging recovery_required].include?(@_phronomy_runtime_owner_state)
638
661
  raise Phronomy::Error, "Agent Runtime ownership purge completion state is invalid"
639
662
  end
640
663
  @root = nil
@@ -679,7 +702,9 @@ module Phronomy
679
702
 
680
703
  if load_existing
681
704
  root = records = nil
705
+ receipts = IdentityWrite.supported?(@persistence)
682
706
  @persistence.transaction do |tx|
707
+ tx.identity_receipts.guard!(@agent_id) if receipts
683
708
  begin
684
709
  root = tx.agents.load(@agent_id)
685
710
  rescue Phronomy::Persistence::NotFoundError => error
@@ -690,21 +715,23 @@ module Phronomy
690
715
  @agent_id,
691
716
  limit: root.journal_position
692
717
  )
718
+ @_phronomy_identity_generation = tx.identity_receipts.read(@agent_id)&.operation_id if receipts
693
719
  end
694
720
  validate_loaded_definition!(root)
695
721
  @root = root
696
722
  @_phronomy_journal_records = Array(records).dup.freeze
697
723
  else
698
- @root = create_agent_root!(
724
+ update = create_agent_root!(
699
725
  context: context,
700
726
  knowledge: knowledge,
701
727
  metadata: metadata
702
728
  )
703
- @_phronomy_journal_records = @persistence.journals.read(
704
- @agent_id,
705
- limit: @root.journal_position
706
- ).dup.freeze
729
+ @root = update.root
730
+ @_phronomy_journal_records = update.records
731
+ @_phronomy_identity_generation = @_phronomy_identity_write.generation
707
732
  end
733
+ ensure
734
+ @_phronomy_identity_write = nil
708
735
  end
709
736
 
710
737
  def validate_loaded_definition!(loaded)
@@ -725,7 +752,8 @@ module Phronomy
725
752
  def create_agent_root!(context:, knowledge:, metadata:)
726
753
  definition = self.class.agent_definition
727
754
  state_writer.create_root(
728
- definition: definition, context: context, knowledge: knowledge, metadata: metadata, retention: @initial_retention
755
+ definition: definition, context: context, knowledge: knowledge, metadata: metadata,
756
+ retention: @initial_retention, write: @_phronomy_identity_write
729
757
  )
730
758
  end
731
759
 
@@ -13,10 +13,12 @@ module Phronomy
13
13
  agent:,
14
14
  persistence:,
15
15
  candidate_resolver: nil,
16
- journal_records: nil
16
+ journal_records: nil,
17
+ contents: persistence.contents
17
18
  )
18
19
  @agent = agent
19
20
  @persistence = persistence
21
+ @contents = contents
20
22
  @policy = agent.class.context_policy
21
23
  @journal_records = journal_records
22
24
  @candidate_resolver = candidate_resolver || ContextCandidateResolver.new(
@@ -36,7 +38,7 @@ module Phronomy
36
38
  system_text = build_system_text(input)
37
39
  hook_candidates = normalize_candidates(patch.segment_candidates)
38
40
  input_ref = execution.metadata.fetch("current_input_ref")
39
- current_input_content = @persistence.contents.fetch_text(input_ref)
41
+ current_input_content = @contents.fetch_text(input_ref)
40
42
  excluded = [execution.metadata["current_input_record_id"]].compact
41
43
  handoff_context = config[:phronomy_transfer_context]
42
44
 
@@ -391,9 +393,9 @@ module Phronomy
391
393
  generated = hooks.each_with_index.map do |hook, index|
392
394
  content = hook.fetch(:content)
393
395
  content_ref = if content.is_a?(String)
394
- @persistence.contents.put_text(content)
396
+ @contents.put_text(content)
395
397
  else
396
- @persistence.contents.put_json(content)
398
+ @contents.put_json(content)
397
399
  end
398
400
  metadata = hook.fetch(:metadata).merge(
399
401
  SEGMENT_ORIGIN_METADATA_KEY => BEFORE_LLM_INPUT_ORIGIN,
@@ -433,9 +435,9 @@ module Phronomy
433
435
  next_sequence = Array(candidates).filter_map(&:sequence).max.to_i
434
436
  transferred = handoff_context.items.each_with_index.map do |item, index|
435
437
  content_ref = if item.content_format == :json
436
- @persistence.contents.put_json(item.content)
438
+ @contents.put_json(item.content)
437
439
  else
438
- @persistence.contents.put_text(item.content.to_s)
440
+ @contents.put_text(item.content.to_s)
439
441
  end
440
442
  metadata = item.metadata.merge(
441
443
  SEGMENT_ORIGIN_METADATA_KEY => HANDOFF_CONTEXT_ORIGIN,
@@ -483,9 +485,9 @@ module Phronomy
483
485
  end
484
486
 
485
487
  def fetch_content(ref)
486
- @persistence.contents.fetch_text(ref)
488
+ @contents.fetch_text(ref)
487
489
  rescue
488
- @persistence.contents.fetch(ref)
490
+ @contents.fetch(ref)
489
491
  end
490
492
 
491
493
  def build_system_text(input)
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # A process-local capability for one retained pre-dispatch save on the exact
6
+ # original Agent incarnation. Its identities describe responsibility; they
7
+ # do not reconstruct authority in another Runtime or process.
8
+ # @api public
9
+ class DispatchRecovery
10
+ # @api public
11
+ attr_reader :agent_id, :execution_id, :recovery_id, :dispatch_kind
12
+
13
+ # @api private
14
+ def initialize(operation)
15
+ @operation = operation
16
+ @agent_id = operation.agent_id.to_s.dup.freeze
17
+ @execution_id = operation.execution_id.to_s.dup.freeze
18
+ @recovery_id = operation.recovery_id.to_s.dup.freeze
19
+ @dispatch_kind = operation.dispatch_kind
20
+ freeze
21
+ end
22
+ private_class_method :new
23
+
24
+ private
25
+
26
+ attr_reader :operation
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # Recovery observes responsibility already held by this Agent, including
6
+ # during Runtime drain. It does not create a replacement owner or enter
7
+ # ordinary new-execution admission.
8
+ # @api private
9
+ module DispatchRecoveryApi
10
+ # Finds pre-dispatch work retained by this exact Agent incarnation.
11
+ # Lookup performs no persistence I/O and creates no Runtime. Resolved work
12
+ # leaves the index; an already held capability can return its final receipt.
13
+ # @param execution_id [String] nonempty accepted execution identity
14
+ # @return [DispatchRecovery, nil]
15
+ # @api public
16
+ def pending_dispatch_recovery(execution_id:)
17
+ unless execution_id.is_a?(String) && !execution_id.empty?
18
+ raise ArgumentError, "execution_id must be a nonempty String"
19
+ end
20
+ @_phronomy_runtime_owner&.existing_registry&.pending_agent_dispatch_recovery(execution_id, agent: self)
21
+ end
22
+
23
+ # Reads the retained barrier and applies confirmed post-state on its exact
24
+ # owner. A positively established pre-barrier failure instead redelivers
25
+ # only its retained setup-failure notification. Neither path repeats
26
+ # provider/Tool work to infer its outcome or retries barrier writes.
27
+ # Cancelling this observation does not cancel retained work or continuation.
28
+ # @param recovery [DispatchRecovery] reference from this Agent
29
+ # @return [TaskResult<DispatchRecoveryResult>] a fresh observation
30
+ # @api public
31
+ def reconcile_dispatch_async(recovery:)
32
+ _dispatch_recovery_operation(recovery).observe(:reconcile)
33
+ end
34
+
35
+ # Waits for reconciliation on a host thread. The earlier failed execution
36
+ # observation stays closed; a resumed receipt supplies a new observation.
37
+ # @param recovery [DispatchRecovery] reference from this Agent
38
+ # @return [DispatchRecoveryResult]
39
+ # @raise [EventLoopReentrancyError] when called on EventLoop
40
+ # @api public
41
+ def reconcile_dispatch(recovery:)
42
+ _check_event_loop_reentrancy(:reconcile_dispatch, :reconcile_dispatch_async)
43
+ reconcile_dispatch_async(recovery: recovery).wait_result
44
+ end
45
+
46
+ # Explicitly retries only the captured barrier writes after checking the
47
+ # complete pre-state under the original Store guard and transaction.
48
+ # Hooks, Policy, provider requests and Tool work are never replayed to
49
+ # reconstruct a proposal. A positively established pre-barrier failure can
50
+ # only redeliver its retained notification; other incomplete proposals stay
51
+ # retained and pending.
52
+ # @param recovery [DispatchRecovery] reference from this Agent
53
+ # @return [TaskResult<DispatchRecoveryResult>] a fresh observation
54
+ # @api public
55
+ def retry_dispatch_async(recovery:)
56
+ _dispatch_recovery_operation(recovery).observe(:retry)
57
+ end
58
+
59
+ # Waits for the explicit persistence-only retry on a host thread.
60
+ # @param recovery [DispatchRecovery] reference from this Agent
61
+ # @return [DispatchRecoveryResult]
62
+ # @raise [EventLoopReentrancyError] when called on EventLoop
63
+ # @api public
64
+ def retry_dispatch(recovery:)
65
+ _check_event_loop_reentrancy(:retry_dispatch, :retry_dispatch_async)
66
+ retry_dispatch_async(recovery: recovery).wait_result
67
+ end
68
+
69
+ private
70
+
71
+ def _dispatch_recovery_operation(recovery)
72
+ unless recovery.is_a?(DispatchRecovery)
73
+ raise ArgumentError, "recovery must be an Agent::DispatchRecovery"
74
+ end
75
+ operation = recovery.send(:operation)
76
+ unless operation.is_a?(DispatchRecoveryOperation) &&
77
+ operation.recovery.equal?(recovery) && operation.belongs_to?(self)
78
+ raise ArgumentError, "recovery belongs to another Agent incarnation"
79
+ end
80
+ operation
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # The caller's execution observation ended while its original Agent retained
6
+ # pre-dispatch responsibility. This does not classify the job as failed or
7
+ # prove that an external request occurred. Recovery is a new observation.
8
+ # @api public
9
+ class DispatchRecoveryError < Phronomy::Error
10
+ # @api public
11
+ attr_reader :recovery, :agent_id, :execution_id, :recovery_id,
12
+ :dispatch_kind, :original_error
13
+
14
+ # @api private
15
+ def initialize(recovery:, error:, original_error: error)
16
+ @recovery = recovery
17
+ @agent_id = recovery.agent_id.to_s.dup.freeze
18
+ @execution_id = recovery.execution_id.to_s.dup.freeze
19
+ @recovery_id = recovery.recovery_id.to_s.dup.freeze
20
+ @dispatch_kind = recovery.dispatch_kind
21
+ @operation_error = error
22
+ @original_error = original_error
23
+ super("Agent #{@agent_id.inspect} execution #{@execution_id.inspect} " \
24
+ "#{@dispatch_kind} dispatch recovery #{@recovery_id.inspect} requires an explicit " \
25
+ "recovery observation", code: "agent.dispatch_recovery")
26
+ end
27
+
28
+ # Preserve the immediate operation error across TaskResult delivery.
29
+ # @return [Exception, nil]
30
+ # @api public
31
+ def cause
32
+ @operation_error || super
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # One immutable observation of pre-dispatch persistence and continuation.
6
+ # error describes recovery; execution_task observes the job's actual result.
7
+ # Resolving this obligation neither proves job success nor releases the job.
8
+ # @api public
9
+ class DispatchRecoveryResult
10
+ OUTCOMES = %i[confirmed_pre confirmed_post not_prepared unresolved unavailable].freeze
11
+ APPLICATION_STATES = %i[pending applied].freeze
12
+ CONTINUATION_STATES = %i[pending resumed stopped].freeze
13
+ private_constant :OUTCOMES, :APPLICATION_STATES, :CONTINUATION_STATES
14
+
15
+ # revision describes the captured barrier's confirmed record. An execution
16
+ # observation is fresh for each receipt, never the owner's canonical task.
17
+ # @api public
18
+ attr_reader :recovery, :agent_id, :execution_id, :recovery_id, :dispatch_kind,
19
+ :outcome, :application_state, :continuation_state, :revision, :execution_task, :error
20
+
21
+ # @api private
22
+ def initialize(recovery:, outcome:, application_state:, continuation_state:,
23
+ physical_complete:, revision: nil, execution_task: nil, error: nil)
24
+ unless OUTCOMES.include?(outcome) && APPLICATION_STATES.include?(application_state) &&
25
+ CONTINUATION_STATES.include?(continuation_state)
26
+ raise ArgumentError, "unsupported Agent dispatch recovery state"
27
+ end
28
+ unless physical_complete.equal?(true) || physical_complete.equal?(false)
29
+ raise ArgumentError, "dispatch physical completion must be a boolean"
30
+ end
31
+ if %i[confirmed_pre confirmed_post].include?(outcome)
32
+ unless revision.is_a?(Integer) && revision >= 0
33
+ raise ArgumentError, "confirmed dispatch persistence requires its nonnegative execution revision"
34
+ end
35
+ elsif !revision.nil?
36
+ raise ArgumentError, "unclassified or unprepared dispatch work has no confirmed revision"
37
+ end
38
+ case outcome
39
+ when :confirmed_post
40
+ if continuation_state == :stopped
41
+ raise ArgumentError, "only positively unprepared dispatch work can use stopped continuation"
42
+ end
43
+ when :not_prepared
44
+ if continuation_state == :resumed
45
+ raise ArgumentError, "unprepared dispatch work cannot resume a barrier"
46
+ end
47
+ else
48
+ if application_state != :pending || continuation_state != :pending
49
+ raise ArgumentError, "dispatch work without confirmed post-state must remain pending"
50
+ end
51
+ end
52
+ if %i[resumed stopped].include?(continuation_state)
53
+ unless application_state == :applied && physical_complete && execution_task.is_a?(Phronomy::TaskResult)
54
+ raise ArgumentError, "dispatch continuation requires application, physical completion and an execution observation"
55
+ end
56
+ elsif !execution_task.nil?
57
+ raise ArgumentError, "only resumed or stopped dispatch work has an execution observation"
58
+ end
59
+
60
+ @recovery = recovery
61
+ @agent_id = recovery.agent_id.to_s.dup.freeze
62
+ @execution_id = recovery.execution_id.to_s.dup.freeze
63
+ @recovery_id = recovery.recovery_id.to_s.dup.freeze
64
+ @dispatch_kind = recovery.dispatch_kind
65
+ @outcome = outcome
66
+ @application_state = application_state
67
+ @continuation_state = continuation_state
68
+ @physical_complete = physical_complete
69
+ @revision = revision
70
+ @execution_task = execution_task
71
+ @error = error
72
+ freeze
73
+ end
74
+
75
+ # True only after the original preparation and recovery workers stopped.
76
+ # The continued provider, Tool or overall Agent execution may still run.
77
+ # @return [Boolean]
78
+ # @api public
79
+ def physical_complete?
80
+ @physical_complete
81
+ end
82
+
83
+ # True after the exact owner handed off the confirmed barrier continuation,
84
+ # or its positively established pre-barrier failure notification. Stopped
85
+ # never permits preparation replay; execution_task observes the job result.
86
+ # @return [Boolean]
87
+ # @api public
88
+ def resolved?
89
+ @physical_complete && @application_state == :applied && %i[resumed stopped].include?(@continuation_state)
90
+ end
91
+ end
92
+ end
93
+ end
@@ -47,6 +47,7 @@ module Phronomy
47
47
  :error
48
48
 
49
49
  attr_reader :final_response,
50
+ :output_contract,
50
51
  :execution_id,
51
52
  :agent,
52
53
  :config,
@@ -69,13 +70,15 @@ module Phronomy
69
70
  approval_listener: nil,
70
71
  event_listener: nil,
71
72
  mode: nil,
72
- execution_id: nil
73
+ execution_id: nil,
74
+ output_contract: config[:phronomy_output_contract]
73
75
  )
74
76
  @agent = agent
75
77
  @input = input
76
78
  # The caller's Hash must not become a shared mutable Runtime object.
77
- # OutputContract is admission-only; the durable Manifest owns subsequent
78
- # schema projection. Never forward the parent promise to an Agent Tool.
79
+ # Keep the final-output promise on its owner. The Manifest supplies
80
+ # provider schemas, and child Tools receive only the ordinary config.
81
+ @output_contract = output_contract
79
82
  @config = config.except(:phronomy_output_contract)
80
83
  resolved_execution_id = execution_id || @config[:execution_id]
81
84
  @execution_id = resolved_execution_id&.to_s&.freeze
@@ -168,6 +171,11 @@ module Phronomy
168
171
  end
169
172
 
170
173
  def handle_fsm_event(event)
174
+ if event.type == :approval_resume_cancelled
175
+ @error ||= event.payload.fetch(:error)
176
+ return true
177
+ end
178
+
171
179
  if event.type == :llm_stream_chunk
172
180
  payload = event.payload || {}
173
181
  return :consume unless current_llm_result_authoritative?(payload[:llm_call_id])