phronomy 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +87 -0
  3. data/README.md +24 -2
  4. data/SECURITY.md +1 -1
  5. data/docs/architecture/agent-approval-recovery.md +320 -0
  6. data/docs/architecture/agent-child-approval-recovery.md +387 -0
  7. data/docs/architecture/agent-child-execution-recovery.md +425 -0
  8. data/docs/architecture/agent-dispatch-recovery.md +308 -0
  9. data/docs/architecture/agent-terminal-persistence-evidence.md +360 -0
  10. data/docs/architecture/agent-terminal-recovery.md +298 -0
  11. data/docs/architecture/agent-terminal-submission-recovery.md +275 -0
  12. data/docs/architecture/agent-tool-child-lifetime.md +29 -0
  13. data/docs/architecture/agent-transition-ownership.md +24 -0
  14. data/docs/architecture/backend-groups.md +6 -0
  15. data/docs/architecture/detached-outcome-proof-projection.md +576 -0
  16. data/docs/architecture/execution-abstraction-review.md +5 -0
  17. data/docs/architecture/handoff-dispatch-recovery.md +205 -0
  18. data/docs/architecture/handoff-initial-terminal-application.md +321 -0
  19. data/docs/architecture/initial-preparation-recovery.md +308 -0
  20. data/docs/architecture/multi-agent-handoff.md +17 -0
  21. data/docs/architecture/native-outcome-preparation.md +715 -0
  22. data/docs/architecture/native-parent-dispatch-recovery.md +460 -0
  23. data/docs/architecture/native-parent-outcome-recovery.md +644 -0
  24. data/docs/architecture/parent-child-terminal-acknowledgement.md +297 -0
  25. data/docs/architecture/r8-unit12.md +6 -3
  26. data/docs/architecture/r8-unit4.md +11 -2
  27. data/docs/architecture/recovery-physical-attempt.md +88 -0
  28. data/docs/architecture/recovery-submission-custody.md +247 -0
  29. data/docs/architecture/retained-attempts.md +229 -0
  30. data/docs/architecture/retained-outcome-materialization.md +636 -0
  31. data/docs/architecture/retained-outcome-publication.md +506 -0
  32. data/docs/architecture/storage-execution-boundaries.md +1 -1
  33. data/docs/architecture/structured-output-and-scope.md +15 -0
  34. data/docs/architecture/synchronous-participant-application.md +397 -0
  35. data/docs/architecture/synchronous-participant-contract.md +195 -0
  36. data/docs/architecture/team-admission-rejection.md +91 -0
  37. data/docs/architecture/team-cancellation-delivery.md +245 -0
  38. data/docs/architecture/team-child-retirement.md +104 -0
  39. data/docs/architecture/tracing.md +26 -0
  40. data/docs/architecture/workflow-terminal-ownership-design.md +25 -9
  41. data/docs/architecture/workflow-terminal-reconciliation.md +221 -0
  42. data/docs/architecture.md +16 -9
  43. data/docs/decisions/025-process-local-agent-ownership-and-runtime-admission.md +8 -0
  44. data/docs/features.md +3 -3
  45. data/docs/mcp-client.md +138 -5
  46. data/docs/migrations/orchestrator-subagent-dispatch.md +89 -0
  47. data/docs/migrations/vector-async-clients.md +18 -3
  48. data/docs/persistence-backends.md +30 -4
  49. data/docs/runtime-and-concurrency.md +235 -11
  50. data/lib/phronomy/agent/admission.rb +37 -17
  51. data/lib/phronomy/agent/api/agent.rb +25 -0
  52. data/lib/phronomy/agent/approval_recovery.rb +30 -0
  53. data/lib/phronomy/agent/approval_recovery_api.rb +61 -0
  54. data/lib/phronomy/agent/approval_recovery_error.rb +39 -0
  55. data/lib/phronomy/agent/approval_recovery_result.rb +94 -0
  56. data/lib/phronomy/agent/async_event_api.rb +22 -5
  57. data/lib/phronomy/agent/base.rb +76 -48
  58. data/lib/phronomy/agent/context_preparation.rb +10 -8
  59. data/lib/phronomy/agent/dispatch_recovery.rb +29 -0
  60. data/lib/phronomy/agent/dispatch_recovery_api.rb +84 -0
  61. data/lib/phronomy/agent/dispatch_recovery_error.rb +36 -0
  62. data/lib/phronomy/agent/dispatch_recovery_result.rb +93 -0
  63. data/lib/phronomy/agent/execution/agent_invocation.rb +11 -3
  64. data/lib/phronomy/agent/execution/approval_continuation_plan.rb +131 -0
  65. data/lib/phronomy/agent/execution/approval_parent_evidence.rb +116 -0
  66. data/lib/phronomy/agent/execution/approval_persistence_evidence.rb +111 -0
  67. data/lib/phronomy/agent/execution/approval_recovery_operation.rb +496 -0
  68. data/lib/phronomy/agent/execution/approval_resume_commit.rb +174 -10
  69. data/lib/phronomy/agent/execution/dispatch_continuation_delivery.rb +96 -0
  70. data/lib/phronomy/agent/execution/dispatch_persistence_evidence.rb +147 -0
  71. data/lib/phronomy/agent/execution/dispatch_preparation.rb +63 -7
  72. data/lib/phronomy/agent/execution/dispatch_recovery_operation.rb +495 -0
  73. data/lib/phronomy/agent/execution/execution_coordinator.rb +724 -158
  74. data/lib/phronomy/agent/execution/execution_outcome_committer.rb +312 -39
  75. data/lib/phronomy/agent/execution/execution_registry.rb +508 -13
  76. data/lib/phronomy/agent/execution/initial_persistence_evidence.rb +269 -0
  77. data/lib/phronomy/agent/execution/initial_preparation.rb +132 -40
  78. data/lib/phronomy/agent/execution/initial_preparation_recovery_operation.rb +513 -0
  79. data/lib/phronomy/agent/execution/invocation_transitions.rb +3 -0
  80. data/lib/phronomy/agent/execution/outcome_planning.rb +108 -0
  81. data/lib/phronomy/agent/execution/outcome_preparation.rb +81 -0
  82. data/lib/phronomy/agent/execution/outcome_recovery_operation.rb +609 -0
  83. data/lib/phronomy/agent/execution/outcome_text_projection.rb +73 -0
  84. data/lib/phronomy/agent/execution/outcome_write_plan.rb +48 -0
  85. data/lib/phronomy/agent/execution/runtime_record_encoder.rb +7 -4
  86. data/lib/phronomy/agent/execution/terminal_persistence_evidence.rb +280 -0
  87. data/lib/phronomy/agent/execution/terminal_recovery_operation.rb +38 -0
  88. data/lib/phronomy/agent/execution_change.rb +138 -26
  89. data/lib/phronomy/agent/execution_environment.rb +29 -0
  90. data/lib/phronomy/agent/execution_rejected_error.rb +34 -0
  91. data/lib/phronomy/agent/initial_preparation_recovery.rb +32 -0
  92. data/lib/phronomy/agent/initial_preparation_recovery_api.rb +85 -0
  93. data/lib/phronomy/agent/initial_preparation_recovery_error.rb +41 -0
  94. data/lib/phronomy/agent/initial_preparation_recovery_result.rb +115 -0
  95. data/lib/phronomy/agent/lifecycle/hosted_approval_context.rb +86 -0
  96. data/lib/phronomy/agent/lifecycle/hosted_invocation.rb +42 -10
  97. data/lib/phronomy/agent/lifecycle/identity_write.rb +101 -0
  98. data/lib/phronomy/agent/lifecycle/lifetime_api.rb +28 -0
  99. data/lib/phronomy/agent/lifecycle/ownership_registry.rb +136 -70
  100. data/lib/phronomy/agent/lifecycle/scope.rb +3 -3
  101. data/lib/phronomy/agent/outcome_recovery.rb +47 -0
  102. data/lib/phronomy/agent/outcome_recovery_api.rb +114 -0
  103. data/lib/phronomy/agent/outcome_recovery_error.rb +36 -0
  104. data/lib/phronomy/agent/outcome_recovery_result.rb +92 -0
  105. data/lib/phronomy/agent/output_contract.rb +16 -6
  106. data/lib/phronomy/agent/persistence/identity_receipt_repository.rb +61 -0
  107. data/lib/phronomy/agent/persistence/records.rb +2 -0
  108. data/lib/phronomy/agent/persistence/storage_schema.rb +2 -0
  109. data/lib/phronomy/agent/recovery/invocation_restorer.rb +8 -5
  110. data/lib/phronomy/agent/recovery/recovery_coordinator/continuation.rb +6 -4
  111. data/lib/phronomy/agent/recovery/recovery_coordinator/installation.rb +21 -43
  112. data/lib/phronomy/agent/recovery/recovery_coordinator.rb +19 -6
  113. data/lib/phronomy/agent/runtime_binding/engine_environment.rb +12 -0
  114. data/lib/phronomy/agent/state_writer.rb +4 -3
  115. data/lib/phronomy/agent/store.rb +7 -1
  116. data/lib/phronomy/agent/terminal_recovery.rb +41 -0
  117. data/lib/phronomy/agent/terminal_recovery_api.rb +101 -0
  118. data/lib/phronomy/agent/terminal_recovery_error.rb +36 -0
  119. data/lib/phronomy/agent/terminal_recovery_result.rb +67 -0
  120. data/lib/phronomy/agent/tool_execution/tool_approval_request.rb +25 -1
  121. data/lib/phronomy/common/values/serializable.rb +37 -4
  122. data/lib/phronomy/engine/concurrency/offload_pool.rb +32 -4
  123. data/lib/phronomy/engine/concurrency/pool_registry.rb +6 -0
  124. data/lib/phronomy/engine/event_loop.rb +161 -11
  125. data/lib/phronomy/engine/execution_binding.rb +4 -2
  126. data/lib/phronomy/engine/execution_receiver_binding.rb +5 -0
  127. data/lib/phronomy/engine/runtime/shutdown_result.rb +21 -0
  128. data/lib/phronomy/engine/runtime/timer_queue.rb +7 -2
  129. data/lib/phronomy/engine/runtime.rb +119 -15
  130. data/lib/phronomy/execution/attempt_completion.rb +91 -0
  131. data/lib/phronomy/execution/concurrency/cancellation_token.rb +7 -2
  132. data/lib/phronomy/execution/concurrency/physical_completion_task.rb +7 -2
  133. data/lib/phronomy/execution/execution.rb +17 -2
  134. data/lib/phronomy/execution/execution_receiver.rb +27 -0
  135. data/lib/phronomy/execution/execution_retirement.rb +30 -0
  136. data/lib/phronomy/execution/invocation_controls.rb +2 -2
  137. data/lib/phronomy/execution/retained_attempts.rb +179 -0
  138. data/lib/phronomy/execution/stopped_terminal_custody.rb +99 -0
  139. data/lib/phronomy/llm_adapter/response.rb +7 -1
  140. data/lib/phronomy/multi_agent/admission_registry.rb +6 -0
  141. data/lib/phronomy/multi_agent/durable_subagent_coordinator.rb +175 -39
  142. data/lib/phronomy/multi_agent/handoff_dispatch_evidence.rb +176 -0
  143. data/lib/phronomy/multi_agent/handoff_finalization_error.rb +33 -0
  144. data/lib/phronomy/multi_agent/handoff_history_release.rb +80 -0
  145. data/lib/phronomy/multi_agent/handoff_participant.rb +31 -6
  146. data/lib/phronomy/multi_agent/handoff_runner.rb +195 -21
  147. data/lib/phronomy/multi_agent/handoff_terminal_finalization.rb +176 -0
  148. data/lib/phronomy/multi_agent/native_approval_evidence.rb +26 -0
  149. data/lib/phronomy/multi_agent/native_child_authority.rb +135 -0
  150. data/lib/phronomy/multi_agent/native_dispatch_evidence.rb +43 -0
  151. data/lib/phronomy/multi_agent/native_outcome_evidence.rb +46 -0
  152. data/lib/phronomy/multi_agent/native_persistence_plan.rb +93 -0
  153. data/lib/phronomy/multi_agent/orchestrator.rb +28 -52
  154. data/lib/phronomy/multi_agent/reserved_child_admission.rb +90 -1
  155. data/lib/phronomy/multi_agent/store.rb +5 -34
  156. data/lib/phronomy/multi_agent/subagent_history_release.rb +88 -0
  157. data/lib/phronomy/multi_agent/team_cancellation_delivery_error.rb +21 -0
  158. data/lib/phronomy/multi_agent/team_cancellation_request.rb +134 -0
  159. data/lib/phronomy/multi_agent/team_coordinator.rb +174 -27
  160. data/lib/phronomy/persistence_composition/stores.rb +10 -3
  161. data/lib/phronomy/runtime_composition/execution_defaults.rb +18 -0
  162. data/lib/phronomy/storage/backends/in_memory.rb +3 -1
  163. data/lib/phronomy/storage/resource.rb +10 -1
  164. data/lib/phronomy/tools/agent.rb +7 -1
  165. data/lib/phronomy/tools/mcp.rb +210 -91
  166. data/lib/phronomy/tools/mcp_http_close_response.rb +23 -0
  167. data/lib/phronomy/tools/mcp_transport_registry.rb +210 -0
  168. data/lib/phronomy/tools/vector_search.rb +28 -5
  169. data/lib/phronomy/tracing/automatic.rb +8 -1
  170. data/lib/phronomy/version.rb +1 -1
  171. data/lib/phronomy/workflow/execution/workflow.rb +34 -0
  172. data/lib/phronomy/workflow/execution/workflow_execution_environment.rb +1 -0
  173. data/lib/phronomy/workflow/execution/workflow_execution_registry.rb +106 -7
  174. data/lib/phronomy/workflow/execution/workflow_runner.rb +53 -108
  175. data/lib/phronomy/workflow/execution/workflow_terminal_operation.rb +489 -0
  176. data/lib/phronomy/workflow/execution/workflow_terminal_persistence.rb +81 -0
  177. data/lib/phronomy/workflow/execution/workflow_terminal_recovery.rb +38 -0
  178. data/lib/phronomy/workflow/execution/workflow_terminal_recovery_error.rb +34 -0
  179. data/lib/phronomy/workflow/execution/workflow_terminal_recovery_result.rb +56 -0
  180. data/lib/phronomy/workflow/runtime_binding/workflow_engine_environment.rb +10 -1
  181. data/lib/phronomy/workflow/runtime_binding/workflow_task_session.rb +3 -1
  182. data/lib/phronomy/workflow/runtime_binding/workflow_terminal_policy.rb +2 -1
  183. data/scripts/api_snapshot.rb +46 -1
  184. data/sig/_private/phronomy/multi_agent/team_cancellation_request.rbs +19 -0
  185. data/sig/phronomy/agent/approval_recovery.rbs +54 -0
  186. data/sig/phronomy/agent/dispatch_recovery.rbs +49 -0
  187. data/sig/phronomy/agent/execution_contract.rbs +4 -1
  188. data/sig/phronomy/agent/execution_environment.rbs +2 -0
  189. data/sig/phronomy/agent/initial_preparation_recovery.rbs +48 -0
  190. data/sig/phronomy/agent/outcome_recovery.rbs +51 -0
  191. data/sig/phronomy/agent/store.rbs +1 -0
  192. data/sig/phronomy/agent/terminal_persistence_evidence.rbs +111 -0
  193. data/sig/phronomy/agent/terminal_recovery.rbs +46 -0
  194. data/sig/phronomy/agent.rbs +36 -0
  195. data/sig/phronomy/engine/concurrency/timer_bindings.rbs +2 -0
  196. data/sig/phronomy/engine/execution_binding.rbs +2 -1
  197. data/sig/phronomy/engine/execution_receiver.rbs +4 -0
  198. data/sig/phronomy/engine/observability.rbs +22 -1
  199. data/sig/phronomy/execution/execution.rbs +16 -1
  200. data/sig/phronomy/execution/execution_receiver.rbs +12 -0
  201. data/sig/phronomy/execution/execution_retirement.rbs +18 -0
  202. data/sig/phronomy/execution/stopped_terminal_custody.rbs +19 -0
  203. data/sig/phronomy/handoff.rbs +1 -0
  204. data/sig/phronomy/multi_agent/handoff_finalization_error.rbs +18 -0
  205. data/sig/phronomy/multi_agent/store.rbs +1 -1
  206. data/sig/phronomy/multi_agent/team_cancellation_delivery_error.rbs +9 -0
  207. data/sig/phronomy/persistence_composition.rbs +2 -1
  208. data/sig/phronomy/storage/contracts.rbs +2 -1
  209. data/sig/phronomy/tools/mcp.rbs +27 -0
  210. data/sig/phronomy/workflow.rbs +42 -0
  211. metadata +104 -2
@@ -0,0 +1,308 @@
1
+ # Recovering an uncertain initial preparation
2
+
3
+ An initial request can lose the response to its admission save, first-manifest save,
4
+ or preparation-failure save. The original caller's failed observation does not prove
5
+ that the save failed. Starting another invocation would create another request; it
6
+ cannot repair the original admission.
7
+
8
+ The live Agent retains one initial-preparation obligation. Its capability identifies
9
+ that Agent, execution and recovery operation. `pending_initial_preparation_recovery`
10
+ needs no execution ID, because the original caller may never have received one.
11
+ Use the same live Agent and Runtime that accepted the request. This capability is
12
+ not a serialized restart recipe and does not replace ordinary durable recovery.
13
+
14
+ ## Public observations and decisions
15
+
16
+ | API or field | Meaning |
17
+ | --- | --- |
18
+ | `pending_initial_preparation_recovery` | Discover the retained initial obligation, or `nil`. |
19
+ | `reconcile_initial_preparation(recovery:)` | Observe authoritative save evidence and continue established work. It does not retry an uncommitted save. |
20
+ | `retry_initial_preparation(recovery:)` | Recheck authority and retry only a complete captured proposal whose pre-state still matches. |
21
+ | Corresponding `_async` methods | Return a Task for this recovery observation. Cancelling that observer does not undo physical work. |
22
+ | `stage` | `:admission`, `:activation` or `:failure`; the same capability can advance between stages. |
23
+ | `outcome` | `:confirmed_pre`, `:confirmed_post`, `:not_prepared`, `:unresolved` or `:unavailable`. |
24
+ | `resolved?` | The initial obligation has been applied and handed over or stopped; the execution need not be terminal. |
25
+ | `physical_complete?` | The observed recovery attempt has physically completed. This alone does not authorize continuation. |
26
+ | `execution_task` | A fresh observation of that same execution after resolution. Saved failure or cancellation is a result Hash. If cancellation is confirmed before admission, no execution exists and this Task fails with the cancellation error. |
27
+ | `error` | Error in this recovery observation. A successfully reconciled saved preparation failure is represented in `execution_task`, not as a failed reconciliation. |
28
+
29
+ A confirmed admission may run input filters, the before-LLM hook and context Policy
30
+ for the first time. Confirming the first manifest reuses its captured preparation;
31
+ those callbacks are not repeated. If another save response is lost during this
32
+ continuation, retain the same capability and make a later explicit observation.
33
+ An unreadable or changed proof remains pending; the example does not spin or infer
34
+ success from elapsed time.
35
+
36
+ The filtered input and context content produced for the first manifest are staged
37
+ alongside the manifest itself. The activation transaction saves these bytes and the
38
+ execution revision together. For a shipment review, the Context Policy can read
39
+ the filtered request and the priority added by a hook before persistence. A rolled
40
+ back save leaves the admission in preparation. Retrying the captured proposal
41
+ saves the same priority and filtered text without repeating those callbacks. A
42
+ committed save can resume dispatch after the original execution and captured
43
+ bytes are verified. Missing or mismatched bytes leave confirmation unavailable.
44
+
45
+ An opaque custom admission callback retains its ordinary successful behavior. If
46
+ initial processing becomes uncertain, including refusal of the completed worker's
47
+ notification, the framework cannot prove the callback's complete write set. Recovery
48
+ remains unavailable and never replays that arbitrary callback.
49
+
50
+ Parent cancellation and deadlines remain linked after the original observation
51
+ has closed. Later cancellation does not replace an already captured preparation
52
+ failure with a different terminal result. Cancellation can resolve a confirmed pre-admission obligation without
53
+ creating an execution, so inspect `resolved?` before choosing a retry. A read of an
54
+ already saved failure does not itself release the original admission. In particular,
55
+ a reserved exact observer can return a saved failure while recovery is still pending.
56
+
57
+ A partially installed initial projection is removable only when it matches the
58
+ retained original owner and the Runtime positively confirms that its exact session
59
+ was not admitted. This permits late cancellation to save its terminal result and
60
+ release the original slot after registration refusal. Unknown or admitted session
61
+ status does not authorize deleting the projection or stopping possibly live work.
62
+
63
+ The existing activation materialization path can turn an input-materialization
64
+ failure into a saved preparation failure. Recovery does not promise that all later
65
+ reads become separately retryable, nor does this API close the later Outcome-save
66
+ frontier.
67
+
68
+ ## Complete shared application setup
69
+
70
+ These examples use one serialized application session with an in-memory store.
71
+ Configure the normal provider credentials before running them. The acceptance
72
+ spec replaces provider traffic with its existing HTTP stub; no live service is used.
73
+ The shared fixture, all functions below, and the driver are in
74
+ `spec/fixtures/initial_preparation_recovery/application.rb`.
75
+
76
+ ```ruby
77
+ require "phronomy"
78
+
79
+ module InitialPreparationApplication
80
+ class IntakeAgent < Phronomy::Agent::Base
81
+ agent_definition id: "initial-preparation-intake-example", version: 1
82
+ model "gpt-4o-mini"
83
+ provider :openai
84
+ end
85
+
86
+ class ReviewAgent < Phronomy::Agent::Base
87
+ agent_definition id: "initial-preparation-review-example", version: 1
88
+ model "gpt-4o-mini"
89
+ provider :openai
90
+ end
91
+
92
+ def self.open_session(handoff: false)
93
+ stores = Phronomy::PersistenceComposition.in_memory
94
+ source = IntakeAgent.create(persistence: stores.agent)
95
+ target = handoff ? ReviewAgent.create(persistence: stores.agent) : nil
96
+ edge = if target
97
+ Phronomy::MultiAgent::Handoff.new(source_agent: source, target_agent: target)
98
+ end
99
+ runner = if edge
100
+ Phronomy::MultiAgent::HandoffRunner.new(main_agent: source,
101
+ persistence: stores.multi_agent, handoffs: [edge])
102
+ end
103
+ {stores: stores, source: source, target: target, edge: edge, runner: runner,
104
+ agents: [source, target].compact.to_h { |agent| [agent.agent_id, agent] },
105
+ config: {cancellation_token: Phronomy::Concurrency::CancellationToken.new}}
106
+ end
107
+
108
+ # The application decides when terminal history is no longer needed.
109
+ # Never unload a session while its preparation or execution is unresolved.
110
+ def self.close_completed_session(session)
111
+ session.fetch(:runner)&.forget_history!
112
+ session.fetch(:target)&.unload!
113
+ session.fetch(:source).unload!
114
+ end
115
+ end
116
+ ```
117
+
118
+ ## Normal application before and after
119
+
120
+ The ordinary successful invocation remains the same. These are complete functions,
121
+ not wrappers around an omitted implementation.
122
+
123
+ Before:
124
+
125
+ ```ruby
126
+ module InitialPreparationApplication
127
+ module Before
128
+ def self.run(session, input)
129
+ owner = session.fetch(:runner) || session.fetch(:source)
130
+ owner.invoke(input, config: session.fetch(:config))
131
+ end
132
+ end
133
+ end
134
+ ```
135
+
136
+ After:
137
+
138
+ ```ruby
139
+ module InitialPreparationApplication
140
+ module After
141
+ def self.run(session, input)
142
+ owner = session.fetch(:runner) || session.fetch(:source)
143
+ owner.invoke(input, config: session.fetch(:config))
144
+ end
145
+ end
146
+ end
147
+ ```
148
+
149
+ ## Recovery behavior before and after
150
+
151
+ The baseline function below propagates uncertainty. It is the complete baseline
152
+ application entry point, not a claim that the baseline implements this recovery API.
153
+ Its existing normal behavior is exercised against the applied U03 source.
154
+
155
+ Before:
156
+
157
+ ```ruby
158
+ module InitialPreparationApplication
159
+ module Before
160
+ # The baseline has no retained initial-preparation recovery capability.
161
+ # An uncertain initial save propagates; another invoke is a new request.
162
+ def self.start(session, input)
163
+ {state: :terminal, result: run(session, input)}
164
+ end
165
+ end
166
+ end
167
+ ```
168
+
169
+ The candidate application retains pending observations and makes retry policy
170
+ explicit. `retry_confirmed_pre: false` returns control to the application. Setting it
171
+ to `true` permits at most one captured-save retry per call. The caller can observe
172
+ again later after examining the pending receipt; it must not call `start` again to
173
+ repair that work.
174
+
175
+ A Handoff Runner can surface a saved business failure without the typed initial
176
+ recovery exception. The example therefore also checks the live Agents' pending
177
+ capabilities. After cleanup, Runner observation preserves its existing saved-error
178
+ semantics, including wrapping a saved exception as `Phronomy::Error`. Unrelated
179
+ errors are propagated. No pending session is unloaded in a rescue or ensure block.
180
+
181
+ After:
182
+
183
+ ```ruby
184
+ module InitialPreparationApplication
185
+ module After
186
+ def self.start(session, input, retry_confirmed_pre: false, wait_timeout: nil)
187
+ # This example's caller serializes operations on one session. Previously
188
+ # retained work must be passed to recover, never hidden by a new request.
189
+ if pending_preparation(session)
190
+ raise ArgumentError, "observe the existing initial preparation before starting another request"
191
+ end
192
+ begin
193
+ result = run(session, input)
194
+ rescue => failure
195
+ recovery = if failure.is_a?(Phronomy::Agent::InitialPreparationRecoveryError)
196
+ failure.recovery
197
+ else
198
+ pending_preparation(session)
199
+ end
200
+ raise unless recovery
201
+ return recover(session, recovery, retry_confirmed_pre: retry_confirmed_pre,
202
+ wait_timeout: wait_timeout).merge(original_observation_error: failure)
203
+ end
204
+ # Exact reserved observers can return a saved failure while the original
205
+ # initial admission is still retained. Reading a result is not release.
206
+ recovery = pending_preparation(session, execution_id: result[:execution_id])
207
+ if recovery
208
+ return recover(session, recovery, retry_confirmed_pre: retry_confirmed_pre,
209
+ wait_timeout: wait_timeout)
210
+ end
211
+ {state: :terminal, result: result}
212
+ end
213
+
214
+ def self.pending_preparation(session, execution_id: nil)
215
+ recoveries = session.fetch(:agents).values.filter_map(&:pending_initial_preparation_recovery)
216
+ recoveries.select! { |recovery| recovery.execution_id == execution_id } if execution_id
217
+ raise Phronomy::Error, "more than one initial obligation needs an explicit selection" if recoveries.size > 1
218
+ recoveries.first
219
+ end
220
+
221
+ # Reading proof does not choose a save retry. This explicit policy permits
222
+ # at most one retry of a complete proposal whose pre-state was confirmed.
223
+ def self.recover(session, recovery, retry_confirmed_pre: false, wait_timeout: nil)
224
+ agent = session.fetch(:agents).fetch(recovery.agent_id)
225
+ receipt = agent.reconcile_initial_preparation(recovery: recovery)
226
+ if !receipt.resolved? && receipt.outcome == :confirmed_pre && retry_confirmed_pre
227
+ receipt = agent.retry_initial_preparation(recovery: recovery)
228
+ end
229
+ pending = {state: :recovery_pending, recovery: recovery, receipt: receipt}
230
+ return pending unless receipt.resolved?
231
+ finish(session, pending, wait_timeout: wait_timeout)
232
+ end
233
+
234
+ # This timeout bounds only the execution Task wait. It does not impose a
235
+ # deadline on synchronous reconciliation or Handoff Runner continuation.
236
+ def self.finish(session, pending, wait_timeout: nil)
237
+ task = pending.fetch(:receipt).execution_task
238
+ begin
239
+ execution = task.wait_result(timeout: wait_timeout)
240
+ rescue Phronomy::TimeoutError => error
241
+ raise if task.done?
242
+ return pending.merge(state: :execution_observation_pending, error: error)
243
+ end
244
+ runner = session.fetch(:runner)
245
+ result = if runner
246
+ runner.resume(pending.fetch(:recovery).execution_id, config: session.fetch(:config))
247
+ else
248
+ execution
249
+ end
250
+ {state: :terminal, result: result}
251
+ end
252
+ end
253
+ end
254
+ ```
255
+
256
+ ## Complete driver
257
+
258
+ This driver returns both the session and the observation so the caller keeps custody
259
+ of pending work. An exception from `After.start` becomes an explicit
260
+ `:application_error` observation containing the original error and the session;
261
+ it is never labeled successful. The caller decides how to report or re-raise it and
262
+ retains the handles needed for explicit cleanup. On `:recovery_pending`, retain the capability and call `After.recover`
263
+ when policy or restored evidence permits another observation. On
264
+ `:execution_observation_pending`, retain the receipt and call `After.finish` again.
265
+ That state reports only a local wait timeout; it does not cancel the execution.
266
+ A terminal failed/cancelled result remains terminal; it is not converted to success.
267
+ Call `close_completed_session` only when the execution and any initial obligation
268
+ are terminal/resolved and their Handoff history is no longer needed. The separate
269
+ [Handoff initial terminal finalization](handoff-initial-terminal-application.md)
270
+ now completes the exact current pending routing through `invoke`, `resume` or
271
+ `cancel`, while preserving the saved failed/cancelled/blocked result. `result`
272
+ alone does not finalize it. If finalization or original Agent custody remains
273
+ unresolved, retain the session; do not infer history-release permission from a
274
+ saved terminal Agent status alone. Cleanup is covered for the settled chain,
275
+ not claimed as a proof of every older historical owner's local quiescence.
276
+
277
+ ```ruby
278
+ module InitialPreparationApplication
279
+ # A caller keeps session and any pending receipt until it decides to observe
280
+ # again. Saved failure, cancellation and unrelated errors remain visible.
281
+ def self.run_example(input, retry_confirmed_pre: false, handoff: false)
282
+ session = open_session(handoff: handoff)
283
+ begin
284
+ observation = After.start(session, input, retry_confirmed_pre: retry_confirmed_pre)
285
+ rescue => error
286
+ observation = {state: :application_error, error: error}
287
+ end
288
+ {session: session, observation: observation}
289
+ end
290
+ end
291
+ ```
292
+
293
+ ## Acceptance evidence
294
+
295
+ `spec/phronomy/agent/initial_preparation_recovery_application_spec.rb` exercises
296
+ real Runtime admissions and persistence with provider HTTP responses stubbed. It
297
+ covers all three initial save boundaries before and after commit, successive
298
+ admission/activation response loss, Handoff source and reserved-target recovery,
299
+ original observation closure, callback counts, parent cancellation and deadline
300
+ continuity, current routing authority, delayed physical work, observer cancellation,
301
+ notification refusal, structured output observation, saved-failure slot release,
302
+ and the complete application functions above. A recovery receipt and a terminal
303
+ job result are asserted separately.
304
+
305
+ The bounded formal model checks the initial-obligation protocol. Runtime tests
306
+ supply the concrete Ruby, persistence, callbacks and public API evidence. Neither
307
+ is evidence of exactly-once remote service effects or closure of later Outcome-save
308
+ uncertainty.
@@ -152,3 +152,20 @@ supplies compatible wiring. Source execution identity links each completed
152
152
  transfer to its exact reserved Target execution, independently of later turns.
153
153
  `Persistence#handoff_result(source_execution_id)` reads that turn without loading
154
154
  Agent owners, graph definitions, or listeners.
155
+
156
+ ### Removing retained Handoff history
157
+
158
+ A terminal result can become readable before its persistence worker returns.
159
+ An independent caller can already read that result through `resume`.
160
+ `forget_history!` checks the entire set of retained Agent identities, including
161
+ earlier Sources. It reserves the main Runner admission and the local Agent
162
+ identities, verifies physical custody, and rechecks the routing record and its
163
+ retentions under the persistence guards before removing them. The operation
164
+ preserves the Agent instances and permits a later retry after the workers settle.
165
+
166
+ `Store#forget_handoff` applies the same checks. Its caller must have the main
167
+ Agent loaded in the current Runtime using that Store. An absent historical peer
168
+ identity stays reserved during verification, preventing concurrent local
169
+ materialization. An enclosing persistence transaction, stale Runner, or
170
+ competing admitted operation leaves the history available. Across hosts the
171
+ application must hold the exclusive claim for these identities.