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
@@ -162,11 +162,11 @@ module Phronomy
162
162
 
163
163
  def acquire(kind, agent_class, key, options, listener)
164
164
  materialized = false
165
- agent = @ownership.public_send(kind, key, expected_class: agent_class, scope_owner: @owner_token) do |environment|
165
+ agent = @ownership.public_send(kind, key, expected_class: agent_class, scope_owner: @owner_token) do |environment, transition|
166
166
  materialized = true
167
167
  instance = agent_class.send(:__construct_owned_agent, environment, key,
168
- **options, load_existing: kind == :load, &listener)
169
- RecoveryCoordinator.new(instance).recover_on_load! if kind == :load
168
+ **options, load_existing: kind == :load, identity_write: (transition if kind == :create), &listener)
169
+ RecoveryCoordinator.new(instance).recover_on_load!(&transition) if kind == :load
170
170
  instance
171
171
  end
172
172
  # Register before any result or post-materialization check reaches the
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # Process-local reference to one retained Agent final-or-wait save.
6
+ # Its recovery lifetime is distinct from the Agent execution lifetime.
7
+ # Obtain it from a recovery error or pending_outcome_recovery.
8
+ # @api public
9
+ class OutcomeRecovery
10
+ # Diagnostic identities; possession of their strings is not authority.
11
+ # @api public
12
+ attr_reader :agent_id, :execution_id, :recovery_id, :expected_execution_revision
13
+
14
+ # The retained native save awaits an independently established plain text result.
15
+ # @return [Boolean]
16
+ # @api public
17
+ def output_text_required? = @operation.output_text_required?
18
+
19
+ # Designate a recipient for a verified stopped native terminal outcome.
20
+ # Keep the recipient across unsuccessful or interrupted observations.
21
+ # @return [Phronomy::StoppedTerminalCustody] the designated recipient
22
+ # @api public
23
+ def new_stopped_recipient
24
+ unless @operation.is_a?(OutcomeRecoveryOperation) && @operation.recovery_kind == :outcome &&
25
+ @operation.recovery.equal?(self)
26
+ raise ArgumentError, "recovery is not the exact original outcome capability"
27
+ end
28
+ Phronomy::StoppedTerminalCustody.send(:new, @operation)
29
+ end
30
+
31
+ # @api private
32
+ def initialize(operation)
33
+ @operation = operation
34
+ @agent_id = operation.agent_id.to_s.dup.freeze
35
+ @execution_id = operation.execution_id.to_s.dup.freeze
36
+ @expected_execution_revision = operation.expected_execution_revision
37
+ @recovery_id = operation.recovery_id.to_s.dup.freeze
38
+ freeze
39
+ end
40
+ private_class_method :new
41
+
42
+ private
43
+
44
+ attr_reader :operation
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # Recovery addresses retained responsibility, including during Runtime drain.
6
+ # These observations do not enter ordinary new-execution admission or create
7
+ # a coordinator, Runtime, replacement Agent or persistence authority.
8
+ # @api private
9
+ module OutcomeRecoveryApi
10
+ # Finds uncertain outcome work retained by this exact Agent incarnation.
11
+ # This local lookup performs no persistence I/O and creates no Runtime.
12
+ # Resolved recovery leaves the index; a held capability keeps its receipt.
13
+ # A saved wait can resolve recovery while the Agent execution remains open.
14
+ # @param execution_id [String] nonempty accepted execution identity
15
+ # @return [OutcomeRecovery, nil]
16
+ # @api public
17
+ def pending_outcome_recovery(execution_id:)
18
+ unless execution_id.is_a?(String) && !execution_id.empty?
19
+ raise ArgumentError, "execution_id must be a nonempty String"
20
+ end
21
+ @_phronomy_runtime_owner&.existing_registry&.pending_agent_outcome_recovery(execution_id, agent: self)
22
+ end
23
+
24
+ # Records independently known final text when the native result's original
25
+ # projection failed. Supplying a fact performs no I/O or write retry.
26
+ # A repeated identical fact is accepted while the same owner is retained.
27
+ # @param recovery [OutcomeRecovery] reference from this Agent
28
+ # @param expected_revision [Integer] durable execution revision bound to the known result
29
+ # @param text [String] the known unstructured final output
30
+ # @return [Boolean] true when the original owner holds this fact
31
+ # @api public
32
+ def supply_outcome_text(recovery:, expected_revision:, text:)
33
+ _outcome_recovery_operation(recovery).supply_output_text(expected_revision: expected_revision, text: text)
34
+ end
35
+
36
+ # Classifies final or waiting persistence and applies its saved kind on
37
+ # the exact original owner. It never retries writes or business actions.
38
+ # Cancelling the returned observation does not cancel retained work.
39
+ # @param recovery [OutcomeRecovery] reference from this Agent
40
+ # @return [TaskResult<OutcomeRecoveryResult>] a fresh observation
41
+ # @api public
42
+ def reconcile_outcome_async(recovery:)
43
+ _outcome_recovery_operation(recovery).observe(:reconcile)
44
+ end
45
+
46
+ # Waits for reconciliation on a host thread. An earlier failed or cancelled
47
+ # observation is never reopened, even after recovery succeeds.
48
+ # @param recovery [OutcomeRecovery] reference from this Agent
49
+ # @return [OutcomeRecoveryResult]
50
+ # @raise [EventLoopReentrancyError] when called on EventLoop
51
+ # @api public
52
+ def reconcile_outcome(recovery:)
53
+ _check_event_loop_reentrancy(:reconcile_outcome, :reconcile_outcome_async)
54
+ reconcile_outcome_async(recovery: recovery).wait_result
55
+ end
56
+
57
+ # Under the original Store guard, retries a captured write or starts the
58
+ # first native outcome selection after a pre-selection read failed.
59
+ # Existing conversions, Provider calls and Tool effects are never replayed.
60
+ # @param recovery [OutcomeRecovery] reference from this Agent
61
+ # @return [TaskResult<OutcomeRecoveryResult>] a fresh observation
62
+ # @api public
63
+ def retry_outcome_async(recovery:)
64
+ _outcome_recovery_operation(recovery).observe(:retry)
65
+ end
66
+
67
+ # Waits for the explicitly authorized outcome retry on a host thread.
68
+ # @param recovery [OutcomeRecovery] reference from this Agent
69
+ # @return [OutcomeRecoveryResult]
70
+ # @raise [EventLoopReentrancyError] when called on EventLoop
71
+ # @api public
72
+ def retry_outcome(recovery:)
73
+ _check_event_loop_reentrancy(:retry_outcome, :retry_outcome_async)
74
+ retry_outcome_async(recovery: recovery).wait_result
75
+ end
76
+
77
+ # Explicitly resubmits outcome work only after the original execution
78
+ # mechanism proved it was not admitted and no handle or worker started.
79
+ # Validation and encoding may run for the first time; provider and Tool
80
+ # work are not repeated. This never broadens captured-write retry.
81
+ # @param recovery [OutcomeRecovery] reference from this Agent
82
+ # @return [TaskResult<OutcomeRecoveryResult>] a fresh observation
83
+ # @api public
84
+ def retry_outcome_submission_async(recovery:)
85
+ _outcome_recovery_operation(recovery).observe(:submission)
86
+ end
87
+
88
+ # Waits for the explicit first outcome-work submission on a host thread.
89
+ # A previous not_admitted receipt is a fact, not a reusable permission.
90
+ # @param recovery [OutcomeRecovery] reference from this Agent
91
+ # @return [OutcomeRecoveryResult]
92
+ # @raise [EventLoopReentrancyError] when called on EventLoop
93
+ # @api public
94
+ def retry_outcome_submission(recovery:)
95
+ _check_event_loop_reentrancy(:retry_outcome_submission, :retry_outcome_submission_async)
96
+ retry_outcome_submission_async(recovery: recovery).wait_result
97
+ end
98
+
99
+ private
100
+
101
+ def _outcome_recovery_operation(recovery)
102
+ unless recovery.is_a?(OutcomeRecovery)
103
+ raise ArgumentError, "recovery must be an Agent::OutcomeRecovery"
104
+ end
105
+ operation = recovery.send(:operation)
106
+ unless operation.is_a?(OutcomeRecoveryOperation) && operation.recovery_kind == :outcome &&
107
+ operation.recovery.equal?(recovery) && operation.belongs_to?(self)
108
+ raise ArgumentError, "recovery belongs to another Agent incarnation"
109
+ end
110
+ operation
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # The observation ended while its Agent still owns a outcome obligation.
6
+ # This error does not classify the job as finished or cancel its saved work.
7
+ # A later explicit recovery call returns an independent observation.
8
+ # @api public
9
+ class OutcomeRecoveryError < Phronomy::Error
10
+ # @api public
11
+ attr_reader :recovery, :agent_id, :execution_id, :recovery_id, :original_error
12
+
13
+ # Constructed only after retaining responsibility for this operation.
14
+ # original_error is the original persistence/operation error, not a
15
+ # diagnostic reconstructed into an arbitrary execution exception.
16
+ # @api private
17
+ def initialize(recovery:, error:, original_error: error)
18
+ @recovery = recovery
19
+ @agent_id = recovery.agent_id.to_s.dup.freeze
20
+ @execution_id = recovery.execution_id.to_s.dup.freeze
21
+ @recovery_id = recovery.recovery_id.to_s.dup.freeze
22
+ @operation_error = error
23
+ @original_error = original_error
24
+ super("Agent #{@agent_id.inspect} execution #{@execution_id.inspect} outcome recovery " \
25
+ "#{@recovery_id.inspect} requires an explicit recovery observation", code: "agent.outcome_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,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ # One immutable observation of a final-or-wait persistence obligation.
6
+ # Releasing this recovery owner does not mean a waiting Agent job finished.
7
+ # saved_outcome distinguishes a terminal result from coordination/approval
8
+ # waits; error describes recovery rather than the saved business result.
9
+ # @api public
10
+ class OutcomeRecoveryResult
11
+ OUTCOMES = %i[confirmed_post confirmed_pre unresolved unavailable].freeze
12
+ SAVED_OUTCOMES = %i[terminal coordination_wait approval_wait].freeze
13
+ APPLICATION_STATES = %i[pending applied].freeze
14
+ OWNERSHIP_STATES = %i[retained released transferred].freeze
15
+ SUBMISSION_STATES = %i[not_admitted admitted unknown].freeze
16
+ private_constant :OUTCOMES, :SAVED_OUTCOMES, :APPLICATION_STATES, :OWNERSHIP_STATES, :SUBMISSION_STATES
17
+
18
+ # execution is present only for a terminal saved outcome. approval_request
19
+ # is the exact saved wait request, not permission to bypass owner admission.
20
+ # @api public
21
+ attr_reader :recovery, :agent_id, :execution_id, :recovery_id, :outcome,
22
+ :saved_outcome, :application_state, :ownership_state, :revision,
23
+ :execution, :approval_request, :error, :submission_state
24
+
25
+ # @api private
26
+ def initialize(recovery:, outcome:, application_state:, ownership_state:,
27
+ saved_outcome: nil, revision: nil, execution: nil, approval_request: nil,
28
+ error: nil, submission_state: :unknown)
29
+ unless OUTCOMES.include?(outcome) && APPLICATION_STATES.include?(application_state) &&
30
+ OWNERSHIP_STATES.include?(ownership_state) && SUBMISSION_STATES.include?(submission_state)
31
+ raise ArgumentError, "unsupported Agent outcome recovery state"
32
+ end
33
+ if submission_state == :not_admitted && %i[confirmed_pre confirmed_post].include?(outcome)
34
+ raise ArgumentError, "unadmitted Agent outcome work cannot have a captured persistence disposition"
35
+ end
36
+ if ownership_state == :released && application_state != :applied
37
+ raise ArgumentError, "Agent recovery ownership cannot be released before application"
38
+ end
39
+ if ownership_state == :transferred &&
40
+ (application_state != :pending || saved_outcome != :terminal || submission_state != :admitted)
41
+ raise ArgumentError, "a stopped transfer requires an admitted terminal save without live application"
42
+ end
43
+ if outcome != :confirmed_post &&
44
+ (application_state != :pending || ownership_state != :retained)
45
+ raise ArgumentError, "Agent outcome work without confirmed post-state must remain pending and retained"
46
+ end
47
+ if outcome == :confirmed_post
48
+ unless revision.is_a?(Integer) && revision.positive? && SAVED_OUTCOMES.include?(saved_outcome)
49
+ raise ArgumentError, "confirmed Agent persistence requires its positive revision and saved outcome kind"
50
+ end
51
+ validate_saved_outcome!(recovery, saved_outcome, execution, approval_request)
52
+ elsif !saved_outcome.nil? || !revision.nil? || !execution.nil? || !approval_request.nil?
53
+ raise ArgumentError, "only confirmed Agent post-state has saved outcome values"
54
+ end
55
+
56
+ @recovery = recovery
57
+ @agent_id = recovery.agent_id.to_s.dup.freeze
58
+ @execution_id = recovery.execution_id.to_s.dup.freeze
59
+ @recovery_id = recovery.recovery_id.to_s.dup.freeze
60
+ @outcome = outcome
61
+ @saved_outcome = saved_outcome
62
+ @application_state = application_state
63
+ @ownership_state = ownership_state
64
+ @revision = revision
65
+ @execution = execution
66
+ @approval_request = approval_request
67
+ @error = error
68
+ @submission_state = submission_state
69
+ freeze
70
+ end
71
+
72
+ private
73
+
74
+ def validate_saved_outcome!(recovery, saved_outcome, execution, approval_request)
75
+ if saved_outcome == :terminal
76
+ unless execution.is_a?(ExecutionObservation) && execution.terminal? &&
77
+ execution.agent_id == recovery.agent_id && execution.execution_id == recovery.execution_id &&
78
+ approval_request.nil?
79
+ raise ArgumentError, "terminal saved outcome requires its exact terminal execution observation"
80
+ end
81
+ elsif saved_outcome == :approval_wait
82
+ unless execution.nil? && approval_request.is_a?(ToolApprovalRequest) &&
83
+ approval_request.execution_id == recovery.execution_id
84
+ raise ArgumentError, "approval wait requires its exact saved request and no terminal observation"
85
+ end
86
+ elsif !execution.nil? || !approval_request.nil?
87
+ raise ArgumentError, "coordination wait has no terminal observation or approval request"
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -15,7 +15,7 @@ module Phronomy
15
15
  FACTORIES.replace(parser: factory).freeze
16
16
  end
17
17
 
18
- def initialize(schema, schema_delivery: :native)
18
+ def initialize(schema, schema_delivery: :native, schema_ref: nil)
19
19
  unless %i[native none].include?(schema_delivery)
20
20
  raise ArgumentError, "schema_delivery must be :native or :none"
21
21
  end
@@ -23,6 +23,7 @@ module Phronomy
23
23
  @parser = FACTORIES.fetch(:parser).call(schema)
24
24
  @schema = @parser.json_schema
25
25
  @digest = Digest::SHA256.hexdigest(JSON.generate(@schema, sort_keys: true)).freeze
26
+ @schema_ref = (schema_ref || "sha256:#{@digest}").dup.freeze
26
27
  freeze
27
28
  end
28
29
 
@@ -33,12 +34,21 @@ module Phronomy
33
34
 
34
35
  def self.restore(execution, contents)
35
36
  stored = execution.metadata["output_contract"]
36
- return nil unless stored
37
- contract = new(contents.fetch_json(stored.fetch("schema_ref")), schema_delivery: delivery(stored))
38
- unless contract.digest == stored.fetch("schema_digest")
39
- raise Phronomy::ContentStore::IntegrityError, "Agent output schema digest mismatch"
37
+ return unless stored
38
+ reference = stored.fetch("schema_ref")
39
+ contract = new(contents.fetch_json(reference), schema_delivery: delivery(stored), schema_ref: reference)
40
+ contract.for_execution(execution)
41
+ end
42
+
43
+ # The admitted or restored immutable schema can validate this execution
44
+ # only while its content reference, digest and delivery policy match.
45
+ def for_execution(execution)
46
+ stored = execution.metadata["output_contract"]
47
+ unless stored && stored.fetch("schema_ref") == @schema_ref && stored.fetch("schema_digest") == digest &&
48
+ self.class.delivery(stored) == schema_delivery
49
+ raise Phronomy::ContentStore::IntegrityError, "Agent output contract differs from its saved schema"
40
50
  end
41
- contract
51
+ self
42
52
  end
43
53
 
44
54
  # The schema stays durable even when the provider receives no schema.
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module Agent
5
+ module Persistence
6
+ # Latest identity mutation per Agent key. It survives removal of the root.
7
+ # @api private
8
+ class IdentityReceiptRepository
9
+ include Phronomy::Storage::RecordCodec
10
+
11
+ TYPE = "phronomy.agent_identity_receipt"
12
+ VERSION = "0.1"
13
+ Receipt = Data.define(:revision, :operation_id, :kind)
14
+
15
+ def initialize(view)
16
+ @view = view
17
+ @records = view.records(StorageSchema::IDENTITY_RECEIPTS)
18
+ end
19
+
20
+ def guard!(key)
21
+ @view.check!(guards: [Phronomy::Storage::GuardRef.new(resource: StorageSchema::IDENTITY_RECEIPTS, key: key)], conditions: [])
22
+ end
23
+
24
+ def read(key)
25
+ entry = @records.read(key)
26
+ entry && decode(key, entry)
27
+ end
28
+
29
+ def next_receipt(before:, operation_id:, kind:)
30
+ Receipt.new(revision: before ? before.revision + 1 : 0, operation_id: operation_id, kind: kind)
31
+ end
32
+
33
+ def write(key, before:, after:)
34
+ payload = {"operation_id" => after.operation_id, "kind" => after.kind.to_s}
35
+ attributes = {key: key, attributes: {}, record: build_record(TYPE, VERSION, payload)}
36
+ entry = if before
37
+ @records.replace(**attributes, expected_revision: before.revision, next_revision: after.revision)
38
+ else
39
+ @records.insert(**attributes, revision: after.revision)
40
+ end
41
+ unless decode(key, entry) == after
42
+ raise Phronomy::Storage::SerializationError, "backend returned a different Agent identity receipt"
43
+ end
44
+ nil
45
+ end
46
+
47
+ private
48
+
49
+ def decode(key, entry)
50
+ payload = current_payload!(entry.record, record_type: TYPE, format_version: VERSION,
51
+ keys: %w[operation_id kind], label: "Agent identity receipt")
52
+ operation_id, kind = payload.values_at("operation_id", "kind")
53
+ unless entry.key == key && operation_id.is_a?(String) && !operation_id.empty? && %w[create purge].include?(kind)
54
+ raise Phronomy::Storage::SerializationError, "invalid Agent identity receipt"
55
+ end
56
+ Receipt.new(revision: entry.revision, operation_id: operation_id, kind: kind.to_sym)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -22,6 +22,8 @@ module Phronomy
22
22
  Watermark.new(@view).check!(agent_id: agent_id, agent_revision: agent_revision, journal_position: journal_position)
23
23
  end
24
24
 
25
+ def identity_receipts = IdentityReceiptRepository.new(@view)
26
+
25
27
  def guard_agent!(agent_id)
26
28
  Phronomy::Persistence::StorageBoundary.call do
27
29
  @view.check!(guards: [Phronomy::Storage::GuardRef.new(resource: StorageSchema::ROOTS, key: agent_id)], conditions: [])
@@ -8,6 +8,8 @@ module Phronomy
8
8
  module StorageSchema
9
9
  ROOTS = Phronomy::Storage::Resource.new(id: "agent.roots", kind: :records,
10
10
  guard: {resource: "agent.roots", via: :key})
11
+ IDENTITY_RECEIPTS = Phronomy::Storage::Resource.new(id: "agent.identity_receipts", kind: :records,
12
+ guard_missing: true)
11
13
  EXECUTIONS = Phronomy::Storage::Resource.new(id: "agent.executions", kind: :records,
12
14
  attributes: {owner: :string, active: :boolean}, immutable_attributes: [:owner],
13
15
  indexes: {owner: [:owner], owner_active: [:owner, :active]},
@@ -8,7 +8,7 @@ module Phronomy
8
8
  module InvocationRestorer
9
9
  module_function
10
10
 
11
- def build_invocation_for_suspended(agent, execution, projection, main_coordinator, listener, assistant_message:, cancellation_requested: false)
11
+ def build_invocation_for_suspended(agent, execution, projection, main_coordinator, listener, assistant_message:, output_contract: nil, cancellation_requested: false)
12
12
  request = execution.approval_request && Phronomy::Agent::ToolApprovalRequest.from_h(
13
13
  execution.approval_request
14
14
  )
@@ -18,7 +18,8 @@ module Phronomy
18
18
  "suspended execution #{execution.execution_id} has no durable assistant Tool Call message"
19
19
  end
20
20
 
21
- invocation, config = prepare_invocation(agent, execution, projection, main_coordinator, listener, cancellation_requested: cancellation_requested)
21
+ invocation, config = prepare_invocation(agent, execution, projection, main_coordinator, listener,
22
+ output_contract: output_contract, cancellation_requested: cancellation_requested)
22
23
  invocation.messages << assistant_message
23
24
 
24
25
  invocation.user_message_sent = true
@@ -82,8 +83,9 @@ module Phronomy
82
83
  invocation
83
84
  end
84
85
 
85
- def build_input_for_recovery(agent, execution, projection, main_coordinator, listener, messages:, cancellation_requested: false)
86
- invocation, = prepare_invocation(agent, execution, projection, main_coordinator, listener, cancellation_requested: cancellation_requested)
86
+ def build_input_for_recovery(agent, execution, projection, main_coordinator, listener, messages:, output_contract: nil, cancellation_requested: false)
87
+ invocation, = prepare_invocation(agent, execution, projection, main_coordinator, listener,
88
+ output_contract: output_contract, cancellation_requested: cancellation_requested)
87
89
 
88
90
  messages.each { |message| invocation.messages << message }
89
91
 
@@ -91,7 +93,7 @@ module Phronomy
91
93
  invocation
92
94
  end
93
95
 
94
- def prepare_invocation(agent, execution, projection, main_coordinator, listener, cancellation_requested:)
96
+ def prepare_invocation(agent, execution, projection, main_coordinator, listener, output_contract:, cancellation_requested:)
95
97
  config = {
96
98
  execution_id: execution.execution_id,
97
99
  phronomy_execution_coordinator: main_coordinator,
@@ -105,6 +107,7 @@ module Phronomy
105
107
  agent: agent,
106
108
  input: projection.ask_message,
107
109
  config: config,
110
+ output_contract: output_contract,
108
111
  event_listener: listener,
109
112
  mode: (execution.metadata[ExecutionMetadata::INVOCATION_MODE_KEY] || "invoke").to_sym,
110
113
  execution_id: execution.execution_id
@@ -28,7 +28,7 @@ module Phronomy
28
28
  # operation-local projections transferred to the new invocation, never a
29
29
  # shared cache or a second source of continuation decisions.
30
30
  def prepare_recovery_material(execution, projection: nil)
31
- OutputContract.restore(execution, agent.persistence.contents)
31
+ output_contract = OutputContract.restore(execution, agent.persistence.contents)
32
32
  action = recovery_action(execution)
33
33
  suspended = execution.status == :suspended || execution.phase.to_sym == :resuming
34
34
  return if !suspended && %i[resolution_required failed_terminal].include?(action)
@@ -41,7 +41,7 @@ module Phronomy
41
41
  messages = records.map { |record| materializer.materialize_journal_record(record) }.freeze
42
42
  assistant = messages.reverse.find { |message| message.role.to_sym == :assistant }
43
43
  output, usage, response = SavedContextReader.provider_output_and_usage(agent, execution) if execution.phase.to_sym == :recovery_provider_completed
44
- RecoveryMaterial.new(projection: projection, messages: messages,
44
+ RecoveryMaterial.new(output_contract: output_contract, projection: projection, messages: messages,
45
45
  assistant_message: assistant, output: output, usage: usage, response: response,
46
46
  cancellation_requested: agent.persistence.cancellation_requested?(agent_id: agent.agent_id, execution_id: execution.execution_id))
47
47
  end
@@ -68,10 +68,12 @@ module Phronomy
68
68
  projection = material.projection
69
69
  invocation = if action == :framework_tools
70
70
  InvocationRestorer.build_invocation_for_suspended(agent, execution, projection, main,
71
- agent.send(:_phronomy_event_listener), assistant_message: material.assistant_message, cancellation_requested: material.cancellation_requested)
71
+ agent.send(:_phronomy_event_listener), assistant_message: material.assistant_message,
72
+ output_contract: material.output_contract, cancellation_requested: material.cancellation_requested)
72
73
  else
73
74
  InvocationRestorer.build_input_for_recovery(agent, execution, projection, main,
74
- agent.send(:_phronomy_event_listener), messages: material.messages, cancellation_requested: material.cancellation_requested)
75
+ agent.send(:_phronomy_event_listener), messages: material.messages,
76
+ output_contract: material.output_contract, cancellation_requested: material.cancellation_requested)
75
77
  end
76
78
  if execution.phase.to_sym == :recovery_provider_completed
77
79
  invocation.apply_llm_response!(material.response)
@@ -146,32 +146,29 @@ module Phronomy
146
146
  environment = @environment
147
147
  plan = command.plan
148
148
  execution = plan.execution
149
+ registry = environment.registry
149
150
  main = agent.send(:execution_coordinator_for, agent.__execution_wiring)
150
- admitted = false
151
- bound = false
152
- installed = false
153
151
 
154
- environment.registry.admit_agent_execution(
152
+ registry.admit_agent_execution(
155
153
  agent.agent_id,
156
154
  owner_token: command.owner_token
157
155
  )
158
- admitted = true
159
- environment.registry.bind_agent_execution_admission(
156
+ registry.bind_agent_execution_admission(
160
157
  agent.agent_id,
161
158
  owner_token: command.owner_token,
162
159
  execution_id: execution.execution_id
163
160
  )
164
- bound = true
165
161
 
166
162
  invocation = nil
167
163
  if execution.status == :suspended || execution.phase.to_sym == :resuming
168
164
  invocation = InvocationRestorer.build_invocation_for_suspended(
169
165
  agent, execution, plan.projection, main, agent.send(:_phronomy_event_listener),
170
- assistant_message: plan.material.assistant_message, cancellation_requested: plan.material.cancellation_requested
166
+ assistant_message: plan.material.assistant_message, output_contract: plan.material.output_contract,
167
+ cancellation_requested: plan.material.cancellation_requested
171
168
  )
172
169
  end
173
170
 
174
- environment.registry.install_agent_execution(
171
+ registry.install_agent_execution(
175
172
  execution_id: execution.execution_id,
176
173
  agent: agent,
177
174
  coordinator: main,
@@ -181,10 +178,9 @@ module Phronomy
181
178
  invocation: invocation,
182
179
  fsm_session_id: nil
183
180
  )
184
- installed = true
185
181
 
186
182
  if execution.status == :suspended
187
- environment.registry.mark_agent_execution_admission(
183
+ registry.mark_agent_execution_admission(
188
184
  agent.agent_id,
189
185
  execution_id: execution.execution_id,
190
186
  state: :suspended
@@ -215,7 +211,7 @@ module Phronomy
215
211
 
216
212
  case plan.classification.disposition
217
213
  when Phronomy::Agent::RecoveryRules::RESOLUTION_REQUIRED
218
- environment.registry.mark_agent_execution_admission(
214
+ registry.mark_agent_execution_admission(
219
215
  agent.agent_id,
220
216
  execution_id: execution.execution_id,
221
217
  state: :recovery_required
@@ -248,6 +244,7 @@ module Phronomy
248
244
  end
249
245
  agent.send(:_complete_result_task, command.completion, agent)
250
246
  when Phronomy::Agent::RecoveryRules::RESUMABLE
247
+ continuation_started = true
251
248
  continue_resumable_on_event_loop(
252
249
  execution,
253
250
  completion: command.completion, material: plan.material
@@ -257,39 +254,20 @@ module Phronomy
257
254
  "unsupported Recovery installation disposition: #{plan.classification.disposition.inspect}"
258
255
  end
259
256
  rescue => caught
260
- # simplecov:disable
261
- if installed
262
- begin
263
- environment.registry.release_agent_execution(
264
- execution.execution_id
265
- )
266
- rescue
267
- nil
268
- end
257
+ # Once an external continuation may have been submitted, a failed
258
+ # observer cannot establish that its worker or command never ran.
259
+ rolled_back = begin
260
+ !continuation_started && (!registry || registry.rollback_recovery_installation(
261
+ agent, execution_id: execution.execution_id, owner_token: command.owner_token
262
+ ))
263
+ rescue
264
+ false
269
265
  end
270
- if admitted
271
- if bound
272
- begin
273
- environment.registry.release_agent_execution_admission(
274
- agent.agent_id,
275
- execution_id: execution&.execution_id
276
- )
277
- rescue
278
- nil
279
- end
280
- else
281
- begin
282
- environment.registry.release_agent_execution_admission(
283
- agent.agent_id,
284
- owner_token: command.owner_token
285
- )
286
- rescue
287
- nil
288
- end
289
- end
266
+ if rolled_back
267
+ command.completion.complete(RejectedInstallation.new(error: caught))
268
+ else
269
+ agent.send(:_fail_result_task, command.completion, caught)
290
270
  end
291
- agent.send(:_fail_result_task, command.completion, caught)
292
- # simplecov:enable
293
271
  end
294
272
 
295
273
  def observe_recovery_execution(task, execution)