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
@@ -35,6 +35,93 @@ module Phronomy
35
35
 
36
36
  private
37
37
 
38
+ # Private opt-in: retry repeats Agent writes under current reservation
39
+ # guards. It never invokes the original thread-affine admission callback.
40
+ def initial_recovery_authority(admission, scope:, persistence:)
41
+ InitialAuthority.new(self, admission, scope, persistence, store: @persistence, owner: @owner)
42
+ end
43
+
44
+ class InitialAuthority
45
+ Identity = Data.define(:agent_id, :execution_id, :correlation)
46
+ private_constant :Identity
47
+
48
+ def initialize(participant, admission, scope, persistence, store:, owner:)
49
+ @participant = participant
50
+ @store, @owner = store, owner
51
+ @identity = persistence.execution_evidence_identity
52
+ @admission = Identity.new(agent_id: admission.agent_id, execution_id: admission.execution_id,
53
+ correlation: admission.correlation)
54
+ guard(scope: scope, persistence: persistence)
55
+ @handoff_footprint = handoff_footprint(scope) if @owner.fetch("kind") == "handoff"
56
+ assert_current!(scope: scope, persistence: persistence)
57
+ freeze
58
+ end
59
+
60
+ def guard(scope:, persistence:)
61
+ assert_store!(persistence)
62
+ @store.participate(scope) do |records|
63
+ case @owner.fetch("kind")
64
+ when "team" then records.guard_team!(@owner.fetch("team_id"))
65
+ when "subagent"
66
+ persistence.guard_agents(scope, agent_ids: [@owner.fetch("parent_agent_id"), @admission.agent_id])
67
+ when "handoff"
68
+ routing = records.handoff_states.load_locked(@owner.fetch("main_agent_id"))
69
+ ids = [@owner.fetch("main_agent_id"), @admission.agent_id,
70
+ (routing.metadata["source_agent_id"] if routing && routing.phase != "stable")].compact
71
+ persistence.guard_agents(scope, agent_ids: ids)
72
+ end
73
+ end
74
+ nil
75
+ end
76
+
77
+ def assert_current!(scope:, persistence:)
78
+ assert_store!(persistence)
79
+ unless @admission.correlation == @owner
80
+ raise Phronomy::Persistence::StateConflictError, "Initial reservation identity changed"
81
+ end
82
+ @store.participate(scope) do |records|
83
+ case @owner.fetch("kind")
84
+ when "team" then @participant.send(:validate_team!, records, @admission)
85
+ when "subagent" then @participant.send(:validate_subagent!, records, @admission, scope)
86
+ when "handoff"
87
+ @participant.send(:validate_handoff!, records, @admission, scope)
88
+ unless handoff_footprint(scope) == @handoff_footprint
89
+ raise Phronomy::Persistence::StateConflictError, "Initial Handoff routing or retention changed"
90
+ end
91
+ end
92
+ end
93
+ nil
94
+ end
95
+
96
+ # Terminal cleanup is not a fresh dispatch grant. The Agent proof checks
97
+ # the exact root and execution; a cancelled parent may still finish it.
98
+ def assert_cleanup!(scope:, persistence:)
99
+ assert_store!(persistence)
100
+ end
101
+
102
+ private
103
+
104
+ def assert_store!(persistence)
105
+ unless persistence.equal?(@store.agent_store) &&
106
+ persistence.execution_evidence_identity.equal?(@identity)
107
+ raise Phronomy::Persistence::StateConflictError, "Initial reservation belongs to another Store"
108
+ end
109
+ end
110
+
111
+ def handoff_footprint(scope)
112
+ @store.participate(scope) do |records|
113
+ routing = records.handoff_states.load(@owner.fetch("main_agent_id"))
114
+ ids = [@owner.fetch("main_agent_id"), @admission.agent_id,
115
+ (routing.metadata["source_agent_id"] if routing && routing.phase != "stable")].compact.uniq.sort
116
+ Phronomy::Values::Immutable.copy([routing&.to_h, ids.map { |id|
117
+ @store.agent_store.retained_references(agent_id: id, scope: scope).map(&:to_h)
118
+ .sort_by { |hold| Phronomy::CanonicalJSON.dump(hold) }
119
+ }])
120
+ end
121
+ end
122
+ end
123
+ private_constant :InitialAuthority
124
+
38
125
  def validate_team!(records, admission)
39
126
  records.guard_team!(@owner.fetch("team_id"))
40
127
  team = records.team_executions.load(@owner.fetch("team_execution_id"))
@@ -78,7 +165,9 @@ module Phronomy
78
165
  if Array(routing.metadata["cancelled_execution_ids"]).include?(admission.execution_id)
79
166
  raise Phronomy::CancellationError, "Handoff reservation was cancelled"
80
167
  end
81
- @persistence.agent_store.guard_agents(scope, agent_ids: [@owner.fetch("main_agent_id"), admission.agent_id])
168
+ ids = [@owner.fetch("main_agent_id"), admission.agent_id]
169
+ ids << routing.metadata["source_agent_id"] unless routing.phase == "stable"
170
+ @persistence.agent_store.guard_agents(scope, agent_ids: ids.compact)
82
171
  if routing.phase != "stable" && routing.pending_target_execution_id != admission.execution_id
83
172
  raise Phronomy::Persistence::StateConflictError, "Handoff reserved Target identity mismatch"
84
173
  end
@@ -88,43 +88,14 @@ module Phronomy
88
88
  end
89
89
  end
90
90
 
91
- def forget_subagents(agent_id:, execution_id:)
92
- transaction do |records, scope|
93
- extension = agent_store.execution_extension(agent_id: agent_id, execution_id: execution_id,
94
- binding_key: DurableSubagentCoordinator::BINDING_KEY, scope: scope)
95
- raise Phronomy::Persistence::NotFoundError, "Subagent history is absent" unless extension&.state_ref
96
- snapshot = records.contents.fetch_json(extension.state_ref)
97
- children = snapshot.fetch("children")
98
- ids = ([agent_id] + children.map { |child| child.fetch("agent_id") }).uniq.sort.select { |id| agent_store.exist?(id) }
99
- agent_store.guard_agents(scope, agent_ids: ids)
100
- parent = agent_store.observe_execution(agent_id: agent_id, execution_id: execution_id, scope: scope)
101
- raise Phronomy::AgentBusyError, "Subagent history retains unfinished parent" unless parent.terminal?
102
- children.each do |child|
103
- observed = agent_store.observe_execution(agent_id: child.fetch("agent_id"), execution_id: child.fetch("execution_id"), scope: scope)
104
- if !observed.terminal? && !(observed.absent? && snapshot["cancel_requested"])
105
- raise Phronomy::AgentBusyError, "Subagent history retains unfinished child"
106
- end
107
- end
108
- agent_store.remove_execution_extension(agent_id: agent_id, execution_id: execution_id,
109
- binding_key: DurableSubagentCoordinator::BINDING_KEY, scope: scope)
110
- ids.each { |id| agent_store.release_retention(agent_id: id, owner_key: "subagent:#{execution_id}", scope: scope) }
111
- end
112
- true
91
+ # Applications that require business import can condition the deletion on
92
+ # the durable slot receipt in the same transaction that removes the holds.
93
+ def forget_subagents(agent_id:, execution_id:, require_import: false)
94
+ SubagentHistoryRelease.new(self, agent_id, execution_id, require_import).call
113
95
  end
114
96
 
115
97
  def forget_handoff(main_agent_id)
116
- transaction do |records, scope|
117
- routing = records.handoff_states.load_locked(main_agent_id)
118
- raise Phronomy::Persistence::NotFoundError, "Handoff history is absent" unless routing
119
- ids = (Array(routing.metadata["retained_agent_ids"]) + [main_agent_id, routing.active_agent_id]).uniq.sort
120
- agent_store.guard_agents(scope, agent_ids: ids)
121
- unless routing.phase == "stable" && ids.all? { |id| agent_store.active_executions(agent_id: id, scope: scope).empty? }
122
- raise Phronomy::AgentBusyError, "Handoff history retains unfinished work"
123
- end
124
- records.handoff_states.delete(main_agent_id, expected_revision: routing.handoff_revision)
125
- ids.each { |id| agent_store.release_retention(agent_id: id, owner_key: "handoff:#{main_agent_id}", scope: scope) }
126
- end
127
- true
98
+ HandoffHistoryRelease.new(self, main_agent_id).call
128
99
  end
129
100
 
130
101
  def result(team_execution_id)
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module MultiAgent
5
+ # The slot records durable import. Agent owns the independent local
6
+ # completion boundary for that child and its parent.
7
+ # @api private
8
+ class SubagentHistoryRelease
9
+ Proof = Data.define(:extension, :executions, :present_ids, :retentions)
10
+ private_constant :Proof
11
+
12
+ def initialize(store, parent_id, execution_id, require_import)
13
+ unless [true, false].include?(require_import)
14
+ raise ArgumentError, "require_import must be true or false"
15
+ end
16
+ @store, @agents = store, store.agent_store
17
+ @parent_id, @execution_id, @require_import = parent_id, execution_id, require_import
18
+ @binding = DurableSubagentCoordinator::BINDING_KEY
19
+ @owner = "subagent:#{execution_id}"
20
+ end
21
+
22
+ def call
23
+ if Phronomy::WaitPolicy.blocking_forbidden?
24
+ raise Phronomy::EventLoopReentrancyError, "Subagent history release requires a host thread"
25
+ end
26
+ proof = @store.transaction { |records, scope| capture(records, scope) }
27
+ Phronomy::Agent::ExecutionEnvironment.current.with_idle_history_owners(persistence: @agents, executions: proof.executions) do
28
+ @store.transaction do |records, scope|
29
+ unless capture(records, scope) == proof
30
+ raise Phronomy::Persistence::StateConflictError, "Subagent history authority changed before release"
31
+ end
32
+ @agents.remove_execution_extension(agent_id: @parent_id, execution_id: @execution_id, binding_key: @binding, scope: scope)
33
+ proof.present_ids.each { |id| @agents.release_retention(agent_id: id, owner_key: @owner, scope: scope) }
34
+ end
35
+ end
36
+ true
37
+ end
38
+
39
+ private
40
+
41
+ def extension(scope)
42
+ @agents.execution_extension(agent_id: @parent_id, execution_id: @execution_id, binding_key: @binding, scope: scope)
43
+ end
44
+
45
+ def capture(records, scope)
46
+ unless scope.outermost?
47
+ raise Phronomy::Persistence::TransactionError, "Subagent history release requires an outermost transaction"
48
+ end
49
+ saved = extension(scope)
50
+ raise Phronomy::Persistence::NotFoundError, "Subagent history is absent" unless saved&.state_ref
51
+ snapshot = records.contents.fetch_json(saved.state_ref)
52
+ children = snapshot.fetch("children")
53
+ members = [[@parent_id, @execution_id]] + children.map { |child| child.fetch_values("agent_id", "execution_id") }
54
+ executions = members.group_by(&:first).transform_values { |pairs| pairs.map(&:last).uniq.sort }
55
+ present_ids = executions.keys.sort.select { |id| @agents.exist?(id) }
56
+ @agents.guard_agents(scope, agent_ids: present_ids)
57
+ unless extension(scope) == saved
58
+ raise Phronomy::Persistence::StateConflictError, "Subagent history changed before removal"
59
+ end
60
+ if @require_import && saved.binding_version != 1
61
+ raise Phronomy::ConfigurationError, "Unsupported native subagent history version"
62
+ end
63
+ parent = @agents.observe_execution(agent_id: @parent_id, execution_id: @execution_id, scope: scope)
64
+ raise Phronomy::AgentBusyError, "Subagent history retains unfinished parent" unless parent.terminal?
65
+ children.each { |child| verify_child(child, snapshot, scope) }
66
+ holds = present_ids.to_h do |id|
67
+ retained = @agents.retained_references(agent_id: id, scope: scope)
68
+ .select { |hold| hold.owner_key == @owner }.sort_by { |hold| hold.execution_id.to_s }
69
+ [id, retained]
70
+ end
71
+ Proof.new(extension: saved, executions: executions, present_ids: present_ids, retentions: holds)
72
+ end
73
+
74
+ def verify_child(child, snapshot, scope)
75
+ observed = @agents.observe_execution(agent_id: child.fetch("agent_id"), execution_id: child.fetch("execution_id"), scope: scope)
76
+ if @require_import
77
+ correlation = NativeChildAuthority.correlation(@parent_id, @execution_id, child)
78
+ unless observed.terminal? && observed.reservation&.correlation == correlation && child["imported"] == true
79
+ raise Phronomy::AgentBusyError, "Subagent result has not been imported by the parent"
80
+ end
81
+ end
82
+ if !observed.terminal? && !(observed.absent? && snapshot["cancel_requested"])
83
+ raise Phronomy::AgentBusyError, "Subagent history retains unfinished child"
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../common/error"
4
+
5
+ module Phronomy
6
+ module MultiAgent
7
+ # Cancellation delivery is unresolved; this is not a cancelled Team result.
8
+ # The exact run remains available for explicit cancel or resume recovery.
9
+ # @api public
10
+ class TeamCancellationDeliveryError < Phronomy::Error
11
+ attr_reader :team_id, :team_execution_id
12
+
13
+ def initialize(team_id:, team_execution_id:)
14
+ @team_id = team_id.to_s.dup.freeze
15
+ @team_execution_id = team_execution_id.to_s.dup.freeze
16
+ super("Team #{@team_id} cancellation delivery is unresolved for run #{@team_execution_id}; " \
17
+ "retry cancel or resume with this exact run ID", code: "team.cancellation_delivery")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ module MultiAgent
5
+ # One Team run's local cancellation responsibility and child observations.
6
+ # The host performs durable work; callbacks only retain intent and wake it.
7
+ # @api private
8
+ class TeamCancellationRequest
9
+ attr_reader :team_execution_id, :token, :observations
10
+
11
+ def initialize(team_execution_id:, admissions:)
12
+ @team_execution_id = team_execution_id.to_s.freeze
13
+ @admissions = admissions
14
+ @token = Phronomy::Concurrency::CancellationToken.new
15
+ @observations = {}
16
+ @mutex = Mutex.new
17
+ @condition = ConditionVariable.new
18
+ @registration = nil
19
+ @admitted = @pending = @terminal = @confirmed = false
20
+ @failure = nil
21
+ end
22
+
23
+ # Admission precedes subscription, which can deliver inline. Each host
24
+ # attachment gets a new identity so a previously taken callback stays stale.
25
+ def attach
26
+ @mutex.synchronize do
27
+ raise Phronomy::Error, "Team cancellation request already has a host" if @registration
28
+
29
+ unless @admitted
30
+ @admissions.admit!(self)
31
+ @admitted = true
32
+ end
33
+ @registration = Object.new.freeze
34
+ end
35
+ end
36
+
37
+ def request(registration)
38
+ @mutex.synchronize do
39
+ return false unless @registration.equal?(registration) && !@terminal && !@confirmed
40
+
41
+ @pending = true
42
+ @condition.broadcast
43
+ true
44
+ end
45
+ end
46
+
47
+ def pending?
48
+ @mutex.synchronize { @pending }
49
+ end
50
+
51
+ def failure
52
+ @mutex.synchronize { @failure }
53
+ end
54
+
55
+ def failed(error)
56
+ @mutex.synchronize { @failure = error if @pending }
57
+ end
58
+
59
+ # The caller has confirmed a durable cancellation or terminal disposition.
60
+ # Keep admission through notification; shutdown must not pass between the
61
+ # durable write and the signal to an existing child.
62
+ def confirm(cancel:, terminal: false)
63
+ @token.cancel! if cancel
64
+ @mutex.synchronize do
65
+ @confirmed = true if cancel
66
+ @terminal = true if terminal
67
+ @pending = false
68
+ @failure = nil
69
+ release_if_idle
70
+ @condition.broadcast
71
+ end
72
+ end
73
+
74
+ def terminal
75
+ @mutex.synchronize do
76
+ @terminal = true
77
+ @pending = false
78
+ @failure = nil
79
+ release_if_idle
80
+ @condition.broadcast
81
+ end
82
+ end
83
+
84
+ # Fencing and the pending decision are atomic with callback acceptance.
85
+ # An unresolved request retains admission, token and physical observations.
86
+ def detach(registration)
87
+ @mutex.synchronize do
88
+ return unless @registration.equal?(registration)
89
+
90
+ @registration = nil
91
+ release_if_idle
92
+ @pending
93
+ end
94
+ end
95
+
96
+ # The existing synchronous Team host can receive control while a child is
97
+ # unfinished. Its source TaskResult is never settled by this observation.
98
+ def wait(operation)
99
+ completed = false
100
+ value = error = nil
101
+ subscriptions = Phronomy::Concurrency::ResultSubscriptions.new
102
+ subscriptions.result(operation) do |result, failure|
103
+ @mutex.synchronize do
104
+ value, error, completed = result, failure, true
105
+ @condition.broadcast
106
+ end
107
+ end
108
+ loop do
109
+ action = @mutex.synchronize do
110
+ @condition.wait(@mutex) until @pending || completed
111
+ @pending ? :control : :complete
112
+ end
113
+ if action == :control
114
+ yield
115
+ else
116
+ raise error if error
117
+ return value
118
+ end
119
+ end
120
+ ensure
121
+ subscriptions&.close
122
+ end
123
+
124
+ private
125
+
126
+ def release_if_idle
127
+ return if @registration || @pending || !@admitted
128
+
129
+ @admissions.release!(self)
130
+ @admitted = false
131
+ end
132
+ end
133
+ end
134
+ end