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,269 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+
5
+ module Phronomy
6
+ module Agent
7
+ # Exact persistence facts for one initial admission, activation or failure.
8
+ # The initial owner retains the proposal; this proof only compares and writes
9
+ # captured records under the original Store and current domain guards.
10
+ # @api private
11
+ class InitialPersistenceEvidence
12
+ ContentWrite = Data.define(:reference, :bytes, :canonicalization_version)
13
+ Result = Data.define(:disposition, :evidence, :original_error, :read_error, :continuation_error)
14
+
15
+ # Content construction finishes before the first durable proposal write.
16
+ # The fallback reader belongs to the preparation call and is never retained by the proof.
17
+ class Contents < Phronomy::ContentStore::Base
18
+ def initialize(contents)
19
+ @contents, @values, @writes = contents, {}, []
20
+ end
21
+
22
+ def put(bytes, canonicalization_version:)
23
+ value = String(bytes).b.dup.freeze
24
+ version = Integer(canonicalization_version)
25
+ reference = content_id_for(value).freeze
26
+ @values[reference] = value
27
+ @writes << ContentWrite.new(reference: reference, bytes: value, canonicalization_version: version)
28
+ reference
29
+ end
30
+
31
+ def writes = @writes.dup.freeze
32
+ def fetch(reference) = (@values[reference] || @contents.fetch(reference)).dup
33
+ def exist?(reference) = @values.key?(reference) || @contents.exist?(reference)
34
+
35
+ def self.persist(writes, to:)
36
+ writes.each do |write|
37
+ reference = to.put(write.bytes, canonicalization_version: write.canonicalization_version)
38
+ unless reference == write.reference
39
+ raise Phronomy::ContentStore::IntegrityError, "Initial persistence content reference changed during save"
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ attr_reader :stage, :agent_id, :execution_id, :root, :execution, :next_root,
46
+ :appended_records, :content_writes, :before, :after
47
+
48
+ def self.capture(stage:, persistence:, scope:, root:, current_execution:, execution:, next_root:,
49
+ content_writes:, appended_records: [], authority: nil)
50
+ new(stage: stage, persistence: persistence, root: root, current_execution: current_execution,
51
+ execution: execution, next_root: next_root, content_writes: content_writes,
52
+ appended_records: appended_records, authority: authority).send(:capture_in, persistence, scope)
53
+ end
54
+
55
+ def initialize(stage:, persistence:, root:, current_execution:, execution:, next_root:,
56
+ content_writes:, appended_records:, authority:)
57
+ @stage, @root, @current_execution = stage, root, current_execution
58
+ @execution, @next_root = execution, next_root
59
+ @identity, @authority = persistence.execution_evidence_identity, authority
60
+ @agent_id, @execution_id = execution.agent_id, execution.execution_id
61
+ @journal_position = root.journal_position
62
+ @appended_records = appended_records.each_with_index.map { |record, index|
63
+ record.with_sequence(@journal_position + index + 1)
64
+ }.freeze
65
+ @content_writes = content_writes.map do |write|
66
+ ContentWrite.new(reference: write.reference.dup.freeze, bytes: write.bytes.b.dup.freeze,
67
+ canonicalization_version: write.canonicalization_version)
68
+ end.freeze
69
+ validate_proposal!
70
+ end
71
+ private_class_method :new
72
+
73
+ def apply_in(persistence:, scope:)
74
+ require_persistence!(persistence)
75
+ guard(persistence, scope)
76
+ unless classify(persistence, scope) == :not_committed
77
+ raise Phronomy::Persistence::StateConflictError, "Initial proposal no longer matches its captured pre-state"
78
+ end
79
+ assert_write_authority!(persistence, scope)
80
+ persistence.participate(scope) { |records| write_captured(records) }
81
+ unless classify(persistence, scope) == :committed
82
+ raise Phronomy::Persistence::StateConflictError, "Initial proposal differs from its captured post-state"
83
+ end
84
+ self
85
+ end
86
+
87
+ def reconcile(persistence:, original_error: nil)
88
+ require_persistence!(persistence)
89
+ continuation_error = nil
90
+ disposition = persistence.coordinator.atomic do |scope|
91
+ guard(persistence, scope)
92
+ classify(persistence, scope).tap do |current|
93
+ continuation_error = continuation_error_in(persistence, scope) if current == :committed
94
+ end
95
+ end
96
+ result(disposition, original_error: original_error, continuation_error: continuation_error)
97
+ rescue => error
98
+ result(:unknown, original_error: original_error, read_error: error)
99
+ end
100
+
101
+ def retry(persistence:, original_error: nil)
102
+ require_persistence!(persistence)
103
+ continuation_error = retry_error = nil
104
+ disposition = persistence.coordinator.atomic do |scope|
105
+ guard(persistence, scope)
106
+ current = classify(persistence, scope)
107
+ if current == :not_committed
108
+ begin
109
+ assert_write_authority!(persistence, scope)
110
+ rescue => error
111
+ retry_error = error
112
+ next :not_committed
113
+ end
114
+ persistence.participate(scope) { |records| write_captured(records) }
115
+ current = classify(persistence, scope)
116
+ unless current == :committed
117
+ raise Phronomy::Persistence::StateConflictError, "Retried initial proposal differs from its captured post-state"
118
+ end
119
+ end
120
+ continuation_error = continuation_error_in(persistence, scope) if current == :committed
121
+ current
122
+ end
123
+ result(disposition, original_error: original_error, read_error: retry_error, continuation_error: continuation_error)
124
+ rescue => error
125
+ reconcile(persistence: persistence, original_error: error)
126
+ end
127
+
128
+ private
129
+
130
+ def capture_in(persistence, scope)
131
+ guard(persistence, scope)
132
+ observed = snapshot(persistence, scope)
133
+ if stage == :admission && !observed.fetch(:active).empty?
134
+ raise Phronomy::AgentBusyError, "Agent #{agent_id.inspect} already has a durable active execution"
135
+ end
136
+ expected = footprint(root, @current_execution, [], observed.fetch(:cancellation))
137
+ unless observed == expected
138
+ raise Phronomy::Persistence::StateConflictError, "Initial proposal was prepared against another durable state"
139
+ end
140
+ @before = Phronomy::Values::Immutable.copy(observed)
141
+ @after = Phronomy::Values::Immutable.copy(footprint(next_root, execution, appended_records, observed.fetch(:cancellation)))
142
+ freeze
143
+ end
144
+
145
+ def validate_proposal!
146
+ unless %i[admission activation failure].include?(stage)
147
+ raise ArgumentError, "Unknown initial persistence stage"
148
+ end
149
+ if execution.metadata["reservation"] && !@authority
150
+ raise ArgumentError, "Reserved initial persistence requires its captured authority"
151
+ end
152
+ current_identity = !@current_execution || (@current_execution.agent_id == agent_id && @current_execution.execution_id == execution_id)
153
+ unless root.agent_id == agent_id && next_root.agent_id == agent_id && current_identity
154
+ raise ArgumentError, "Initial persistence proposal identity mismatch"
155
+ end
156
+ if stage == :admission
157
+ valid = @current_execution.nil? && execution.status == :preparing && execution.execution_revision.zero?
158
+ else
159
+ valid = @current_execution&.active? && execution.execution_revision == @current_execution.execution_revision + 1
160
+ valid &&= (stage == :activation) ? (execution.status == :active && execution.phase == :calling_llm) : %i[failed cancelled blocked].include?(execution.status)
161
+ end
162
+ raise ArgumentError, "Initial persistence stage contradicts its execution transition" unless valid
163
+ root_unchanged = root.to_h == next_root.to_h
164
+ root_revision_valid = root_unchanged || next_root.agent_revision == root.agent_revision + 1
165
+ unless root_revision_valid &&
166
+ next_root.journal_position == @journal_position + appended_records.size &&
167
+ appended_records.all? { |record| record.agent_id == agent_id && record.execution_id == execution_id }
168
+ raise ArgumentError, "Initial persistence proposal has inconsistent root or journal facts"
169
+ end
170
+ content_writes.each do |write|
171
+ unless write.reference == "sha256:#{Digest::SHA256.hexdigest(write.bytes)}" && write.canonicalization_version.is_a?(Integer)
172
+ raise ArgumentError, "Initial persistence proposal contains invalid content facts"
173
+ end
174
+ end
175
+ end
176
+
177
+ def guard(persistence, scope)
178
+ unless scope.outermost?
179
+ raise Phronomy::Persistence::TransactionError, "Initial persistence proof requires its own outer transaction"
180
+ end
181
+ @authority&.guard(scope: scope, persistence: persistence)
182
+ persistence.guard_agents(scope, agent_ids: [agent_id])
183
+ end
184
+
185
+ def require_persistence!(persistence)
186
+ return if persistence.execution_evidence_identity.equal?(@identity)
187
+ raise Phronomy::Persistence::StateConflictError, "Initial persistence proof belongs to another Store"
188
+ end
189
+
190
+ def footprint(observed_root, observed_execution, records, cancellation)
191
+ {root: observed_root.to_h, execution: observed_execution&.to_h,
192
+ journal: records.map(&:to_h), journal_head: observed_root.journal_position,
193
+ cancellation: cancellation,
194
+ active: observed_execution&.active? ? [observed_execution.to_h] : []}
195
+ end
196
+
197
+ def snapshot(persistence, scope)
198
+ persistence.participate(scope) do |records|
199
+ observed_root = records.agents.load(agent_id)
200
+ observed_execution = begin
201
+ records.executions.load(execution_id)
202
+ rescue Phronomy::Persistence::NotFoundError
203
+ nil
204
+ end
205
+ head = records.journals.head(agent_id)
206
+ journal = records.journals.read(agent_id, after: @journal_position, limit: [head - @journal_position, 1].max)
207
+ {root: observed_root.to_h, execution: observed_execution&.to_h,
208
+ journal: journal.map(&:to_h), journal_head: head,
209
+ cancellation: records.cancellations.requested?(agent_id, execution_id),
210
+ active: records.executions.list_active(agent_id).sort_by(&:execution_id).map(&:to_h)}
211
+ end
212
+ end
213
+
214
+ def classify(persistence, scope)
215
+ current = snapshot(persistence, scope)
216
+ # Cancellation is a separate ledger. A later request cannot erase an
217
+ # already saved initial transition; it separately prevents new dispatch.
218
+ disposition = Phronomy::Persistence::SaveOutcome.compare(
219
+ before: before.except(:cancellation), after: after.except(:cancellation)
220
+ ) { current.except(:cancellation) }.disposition
221
+ if disposition == :committed
222
+ persistence.participate(scope) do |records|
223
+ content_writes.each do |write|
224
+ unless records.contents.fetch(write.reference) == write.bytes
225
+ raise Phronomy::ContentStore::IntegrityError, "Initial persistence content differs from its captured bytes"
226
+ end
227
+ end
228
+ end
229
+ end
230
+ disposition
231
+ end
232
+
233
+ def write_captured(records)
234
+ Contents.persist(content_writes, to: records.contents)
235
+ records.journals.append(agent_id, expected_position: @journal_position, records: appended_records) unless appended_records.empty?
236
+ if stage == :admission
237
+ records.executions.create_active(execution)
238
+ else
239
+ records.executions.save(execution_id, expected_revision: @current_execution.execution_revision, execution: execution)
240
+ end
241
+ unless next_root.to_h == root.to_h
242
+ records.agents.save(agent_id, expected_revision: root.agent_revision, root: next_root)
243
+ end
244
+ end
245
+
246
+ def continuation_error_in(persistence, scope)
247
+ assert_write_authority!(persistence, scope) unless stage == :failure
248
+ nil
249
+ rescue => error
250
+ error
251
+ end
252
+
253
+ def assert_write_authority!(persistence, scope)
254
+ if stage == :failure
255
+ @authority&.assert_cleanup!(scope: scope, persistence: persistence)
256
+ else
257
+ cancelled = persistence.participate(scope) { |records| records.cancellations.requested?(agent_id, execution_id) }
258
+ raise Phronomy::CancellationError, "Initial execution was cancelled before continuation" if cancelled
259
+ @authority&.assert_current!(scope: scope, persistence: persistence)
260
+ end
261
+ end
262
+
263
+ def result(disposition, original_error:, read_error: nil, continuation_error: nil)
264
+ Result.new(disposition: disposition, evidence: self, original_error: original_error,
265
+ read_error: read_error, continuation_error: continuation_error)
266
+ end
267
+ end
268
+ end
269
+ end
@@ -24,9 +24,10 @@ module Phronomy
24
24
  )
25
25
 
26
26
  # @api private
27
- def initialize(agent:, persistence:)
27
+ def initialize(agent:, persistence:, owner: nil)
28
28
  @agent = agent
29
29
  @persistence = persistence
30
+ @owner = owner
30
31
  end
31
32
 
32
33
  # @api private
@@ -34,7 +35,9 @@ module Phronomy
34
35
  begin
35
36
  raw_message = @agent.send(:extract_message, operation.input)
36
37
  rescue => error
37
- return unadmitted_result(operation, error, :not_established)
38
+ rejection = ExecutionRejectedError.from_error(error, agent_id: @agent.agent_id,
39
+ execution_id: operation.config[:phronomy_exact_observers] && operation.config[:phronomy_reserved_execution_id])
40
+ return unadmitted_result(operation, rejection, :not_established)
38
41
  end
39
42
 
40
43
  begin
@@ -45,7 +48,11 @@ module Phronomy
45
48
  config: operation.config,
46
49
  preparation_replayable: operation.preparation_replayable
47
50
  )
51
+ rescue ExecutionRejectedError => error
52
+ raise if @owner&.save_entered?
53
+ return unadmitted_result(operation, error, :not_established)
48
54
  rescue Phronomy::AgentBusyError => error
55
+ raise if @owner&.save_entered?
49
56
  # A durable nonterminal execution keeps Runtime admission fail-closed
50
57
  # until recovery can establish its lineage, including after F4 loss.
51
58
  return unadmitted_result(operation, error, :recovery_required)
@@ -54,8 +61,10 @@ module Phronomy
54
61
  Phronomy::Persistence::SerializationError,
55
62
  ArgumentError,
56
63
  Phronomy::ConfigurationError => error
64
+ raise if @owner&.save_entered?
57
65
  return unadmitted_result(operation, error, :not_established)
58
66
  rescue => error
67
+ raise if @owner
59
68
  return unadmitted_result(operation, error, :outcome_unknown)
60
69
  end
61
70
 
@@ -87,6 +96,36 @@ module Phronomy
87
96
  )
88
97
  end
89
98
 
99
+ # Continue one captured save; completed preparation callbacks never run again.
100
+ def continue(checkpoint)
101
+ evidence = checkpoint.fetch(:evidence)
102
+ case evidence.stage
103
+ when :admission
104
+ prepare_admitted(input: @owner.command.input, config: @owner.command.config,
105
+ execution: evidence.execution, active_root: evidence.next_root,
106
+ journal_records: @owner.command.journal_records)
107
+ when :activation
108
+ begin
109
+ materialize_preparation(evidence.execution, evidence.next_root,
110
+ checkpoint.fetch(:manifest), checkpoint.fetch(:manifest_ref),
111
+ checkpoint.fetch(:filtered_input), checkpoint.fetch(:config))
112
+ rescue => error
113
+ preparation_failure_result(evidence.execution, evidence.next_root,
114
+ checkpoint.fetch(:config), error)
115
+ end
116
+ when :failure
117
+ failure_result(evidence.execution, evidence.next_root, evidence.appended_records,
118
+ checkpoint.fetch(:config), checkpoint.fetch(:error))
119
+ end
120
+ end
121
+
122
+ def stop(checkpoint, error, before: false)
123
+ evidence = checkpoint.fetch(:evidence)
124
+ execution = before ? AgentExecution.from_h(evidence.before.fetch(:execution)) : evidence.execution
125
+ root = before ? evidence.root : evidence.next_root
126
+ preparation_failure_result(execution, root, @owner.command.config, error)
127
+ end
128
+
90
129
  private
91
130
 
92
131
  def unadmitted_result(operation, error, outcome)
@@ -105,40 +144,41 @@ module Phronomy
105
144
  def prepare_admitted(input:, config:, execution:, active_root:, journal_records:)
106
145
  current_execution = execution
107
146
  begin
147
+ @owner&.preparation_started!
108
148
  # Legacy executions acquire a limit at their first resumed preparation.
109
- current_execution = execution.with(
149
+ current_execution = @owner ? execution : execution.with(
110
150
  execution_revision: execution.execution_revision,
111
151
  metadata: execution.metadata.merge(IterationBudget::LIMIT_KEY =>
112
152
  IterationBudget.limit_for(execution, default: @agent.class.max_iterations))
113
153
  )
114
154
  IterationBudget.check!(current_execution, default: @agent.class.max_iterations)
115
155
  filtered_input = filter_input(input, config)
116
- staged = stage_filtered_input(current_execution, active_root, filtered_input)
117
- assembler, prepared = prepare_context(staged, active_root, filtered_input, config, journal_records)
156
+ @owner.filtered_input = filtered_input if @owner
157
+ contents = InitialPersistenceEvidence::Contents.new(@persistence.contents)
158
+ staged = stage_filtered_input(current_execution, active_root, filtered_input, contents)
159
+ prepared = prepare_context(staged, active_root, filtered_input, config, journal_records, contents)
118
160
  active_execution, manifest, manifest_ref = commit_preparation(
119
- staged, active_root, assembler, prepared
161
+ staged, active_root, prepared, contents
120
162
  )
121
- # Advance the failure base only after the commit response is known.
122
- # F1 response loss must not be reported as a confirmed terminal result.
123
163
  current_execution = active_execution
124
- projection = RuntimeInput.new(
125
- agent: @agent, persistence: @persistence
126
- ).materialize(manifest: manifest, manifest_ref: manifest_ref)
127
- Result.new(
128
- execution: active_execution,
129
- root: active_root,
130
- runtime_projection: projection,
131
- filtered_input: filtered_input,
132
- config: config,
133
- appended_records: [].freeze,
134
- error: nil,
135
- admission_outcome: :active
136
- )
164
+ materialize_preparation(active_execution, active_root, manifest, manifest_ref,
165
+ filtered_input, config)
137
166
  rescue => error
167
+ raise if @owner&.save_entered?
138
168
  preparation_failure_result(current_execution, active_root, config, error)
139
169
  end
140
170
  end
141
171
 
172
+ def materialize_preparation(execution, root, manifest, manifest_ref, filtered_input, config)
173
+ @owner&.controls&.check_start!
174
+ @agent.send(:check_cancellation!, config, "invocation cancelled before initial activation")
175
+ projection = RuntimeInput.new(agent: @agent, persistence: @persistence)
176
+ .materialize(manifest: manifest, manifest_ref: manifest_ref)
177
+ Result.new(execution: execution, root: root, runtime_projection: projection,
178
+ filtered_input: filtered_input, config: config, appended_records: [].freeze,
179
+ error: nil, admission_outcome: :active)
180
+ end
181
+
142
182
  def filter_input(input, config)
143
183
  @agent.send(:check_cancellation!, config, "invocation cancelled before input filtering")
144
184
  filtered = @agent.send(:run_input_filters!, input)
@@ -146,11 +186,9 @@ module Phronomy
146
186
  filtered
147
187
  end
148
188
 
149
- def stage_filtered_input(execution, root, filtered_input)
189
+ def stage_filtered_input(execution, root, filtered_input, contents)
150
190
  message = @agent.send(:extract_message, filtered_input)
151
- # Content-addressed source material may precede Policy. Mutable execution
152
- # and Agent records are only advanced by the later commit transaction.
153
- filtered_ref = @persistence.contents.put_text(message)
191
+ filtered_ref = contents.put_text(message)
154
192
  record = JournalRecord.new(
155
193
  agent_id: @agent.agent_id,
156
194
  execution_id: execution.execution_id,
@@ -172,9 +210,9 @@ module Phronomy
172
210
  )
173
211
  end
174
212
 
175
- def prepare_context(staged, root, input, config, journal_records)
213
+ def prepare_context(staged, root, input, config, journal_records, contents)
176
214
  assembler = ContextPreparation.new(
177
- agent: @agent, persistence: @persistence, journal_records: journal_records
215
+ agent: @agent, persistence: @persistence, journal_records: journal_records, contents: contents
178
216
  )
179
217
  prepared = assembler.prepare_initial(
180
218
  input: input,
@@ -184,13 +222,15 @@ module Phronomy
184
222
  patch: @agent.send(:run_before_llm_input_hooks, call_sequence: 1, config: config)
185
223
  )
186
224
  @agent.send(:check_cancellation!, config, "invocation cancelled after context policy")
187
- [assembler, prepared]
225
+ prepared
188
226
  end
189
227
 
190
- def commit_preparation(staged, root, assembler, prepared)
228
+ def commit_preparation(staged, root, prepared, contents)
229
+ return commit_captured_preparation(staged, root, prepared, contents) if @owner&.capture_supported?
191
230
  active = manifest = manifest_ref = nil
192
231
  @persistence.transaction do |tx|
193
232
  assert_local_durable_base!(tx, root)
233
+ InitialPersistenceEvidence::Contents.persist(contents.writes, to: tx.contents)
194
234
  manifest, manifest_ref = Phronomy::Context::Assembly.new.store(prepared, contents: tx.contents,
195
235
  response_schema_ref: OutputContract.response_schema_ref(staged.metadata))
196
236
  active = execution_with_initial_manifest(staged, manifest_ref)
@@ -203,6 +243,25 @@ module Phronomy
203
243
  [active, manifest, manifest_ref]
204
244
  end
205
245
 
246
+ def commit_captured_preparation(staged, root, prepared, contents)
247
+ @owner.begin_stage(:activation)
248
+ manifest, manifest_ref = Phronomy::Context::Assembly.new.store(prepared, contents: contents,
249
+ response_schema_ref: OutputContract.response_schema_ref(staged.metadata))
250
+ active = execution_with_initial_manifest(staged, manifest_ref)
251
+ @persistence.coordinator.atomic do |scope|
252
+ evidence = InitialPersistenceEvidence.capture(stage: :activation, persistence: @persistence,
253
+ scope: scope, root: root, current_execution: @owner.admitted_execution,
254
+ execution: active, next_root: root, content_writes: contents.writes,
255
+ authority: @owner.authority)
256
+ @owner.capture(evidence, manifest: manifest, manifest_ref: manifest_ref,
257
+ filtered_input: @owner.filtered_input, config: @owner.command.config)
258
+ @owner.save_entered!
259
+ evidence.apply_in(persistence: @persistence, scope: scope)
260
+ end
261
+ @owner.save_returned!
262
+ [active, manifest, manifest_ref]
263
+ end
264
+
206
265
  def execution_with_initial_manifest(staged, manifest_ref)
207
266
  staged.with(
208
267
  status: :active,
@@ -216,14 +275,22 @@ module Phronomy
216
275
  end
217
276
 
218
277
  def admit_execution(raw_message, root:, mode: :invoke, config: {}, preparation_replayable: false)
278
+ begin
279
+ metadata = initial_preparation_metadata(mode.to_sym, preparation_replayable)
280
+ rescue => error
281
+ raise ExecutionRejectedError.from_error(error, agent_id: @agent.agent_id,
282
+ execution_id: config[:phronomy_exact_observers] && config[:phronomy_reserved_execution_id])
283
+ end
219
284
  admission = Admission.new(persistence: @persistence, root: root,
220
285
  input: raw_message, config: config,
221
- preparation_metadata: initial_preparation_metadata(mode.to_sym, preparation_replayable))
286
+ preparation_metadata: metadata, recovery_owner: @owner)
222
287
  participant = config[:phronomy_admission]
223
288
  if admission.correlation && !participant
224
289
  raise Phronomy::ConfigurationError, "Coordinated admission requires its current owner"
225
290
  end
291
+ @owner&.admission_started!(admission, participant)
226
292
  participant ? participant.admit(admission) : admission.accept
293
+ @owner&.save_returned!
227
294
  admission.result
228
295
  end
229
296
 
@@ -241,6 +308,7 @@ module Phronomy
241
308
  def recovered_config(execution)
242
309
  mode = (execution.metadata[ExecutionMetadata::INVOCATION_MODE_KEY] || "invoke").to_sym
243
310
  config = {phronomy_recovery_mode: mode}.merge(@agent.__execution_wiring)
311
+ config[:phronomy_output_contract] = OutputContract.restore(execution, @persistence.contents)
244
312
  if execution.metadata.key?("durable_context_ref")
245
313
  context = @persistence.contents.fetch_json(execution.metadata.fetch("durable_context_ref"))
246
314
  unless context.is_a?(Hash)
@@ -255,19 +323,18 @@ module Phronomy
255
323
 
256
324
  def preparation_failure_result(execution, root, config, error)
257
325
  failure = commit_preparation_failure(execution, root, error)
258
- Result.new(
259
- execution: failure.fetch(:execution),
260
- root: failure.fetch(:root),
261
- runtime_projection: nil,
262
- filtered_input: nil,
263
- config: config,
264
- appended_records: failure.fetch(:appended_records),
265
- error: failure.fetch(:error),
266
- admission_outcome: :terminal
267
- )
326
+ failure_result(failure.fetch(:execution), failure.fetch(:root),
327
+ failure.fetch(:appended_records), config, failure.fetch(:error))
328
+ end
329
+
330
+ def failure_result(execution, root, records, config, error)
331
+ Result.new(execution: execution, root: root, runtime_projection: nil,
332
+ filtered_input: nil, config: config, appended_records: records,
333
+ error: error, admission_outcome: :terminal)
268
334
  end
269
335
 
270
336
  def commit_preparation_failure(execution, root, error)
337
+ return commit_captured_failure(execution, root, error) if @owner&.capture_supported?
271
338
  translated_error = error
272
339
  failed = next_root = appended = nil
273
340
  @persistence.transaction do |tx|
@@ -302,6 +369,31 @@ module Phronomy
302
369
  }.freeze
303
370
  end
304
371
 
372
+ def commit_captured_failure(execution, root, error)
373
+ @owner.begin_stage(:failure)
374
+ contents = InitialPersistenceEvidence::Contents.new(@persistence.contents)
375
+ error_ref = contents.put_json(Phronomy::Error.diagnostic(error))
376
+ status = ExecutionFailure.status_for(error)
377
+ records = preparation_failure_records(execution, root, status, error_ref)
378
+ failed = execution.with(status: status, phase: status, working_records: [],
379
+ error_ref: error_ref, terminal_reason: error.class.name)
380
+ next_root = root.with(agent_revision: root.agent_revision + 1,
381
+ journal_position: root.journal_position + records.length, lifecycle_status: :idle)
382
+ @persistence.coordinator.atomic do |scope|
383
+ evidence = InitialPersistenceEvidence.capture(stage: :failure, persistence: @persistence,
384
+ scope: scope, root: root, current_execution: execution, execution: failed,
385
+ next_root: next_root, appended_records: records, content_writes: contents.writes,
386
+ authority: @owner.authority)
387
+ @owner.capture(evidence, config: @owner.command.config, error: error)
388
+ @owner.save_entered!
389
+ evidence.apply_in(persistence: @persistence, scope: scope)
390
+ end
391
+ @owner.save_returned!
392
+ {execution: failed, root: next_root,
393
+ appended_records: records.each_with_index.map { |record, index| record.with_sequence(root.journal_position + index + 1) }.freeze,
394
+ error: error}.freeze
395
+ end
396
+
305
397
  def preparation_failure_records(execution, root, status, error_ref)
306
398
  records = execution.working_records.map do |record|
307
399
  JournalRecord.from_h(record.to_h.merge("context_candidate" => false))