phronomy 0.26.0 → 0.28.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.
- checksums.yaml +4 -4
- data/.mutant.yml +9 -9
- data/CHANGELOG.md +92 -0
- data/CONTRIBUTING.md +8 -8
- data/README.md +13 -3
- data/VERIFY.sh +27 -27
- data/benchmark/bench_agent_invoke.rb +26 -47
- data/benchmark/bench_context_assembler.rb +17 -15
- data/benchmark/bench_regression.rb +1 -1
- data/benchmark/bench_tool_schema.rb +1 -1
- data/docs/architecture/agent-chat-and-state-ownership.md +153 -0
- data/docs/architecture/agent-configuration-and-tool-binding.md +139 -0
- data/docs/architecture/agent-context.md +4 -2
- data/docs/architecture/agent-transition-ownership.md +13 -0
- data/docs/architecture/backend-groups.md +152 -0
- data/docs/architecture/before-llm-input.md +9 -3
- data/docs/architecture/context-management.md +31 -1
- data/docs/architecture/context-preparation-steps.md +90 -0
- data/docs/architecture/entry-action-and-team-wording.md +72 -0
- data/docs/architecture/execution-abstraction-review.md +88 -0
- data/docs/architecture/execution-boundaries.md +28 -0
- data/docs/architecture/execution-metadata-and-values.md +86 -0
- data/docs/architecture/generator-verifier-ownership.md +111 -0
- data/docs/architecture/multi-agent-handoff.md +11 -4
- data/docs/architecture/persistence-refactoring-plan.md +50 -0
- data/docs/architecture/persistence.md +45 -4
- data/docs/architecture/r8-unit1.md +104 -0
- data/docs/architecture/r8-unit10.md +72 -0
- data/docs/architecture/r8-unit11.md +71 -0
- data/docs/architecture/r8-unit12.md +67 -0
- data/docs/architecture/r8-unit13.md +61 -0
- data/docs/architecture/r8-unit14.md +72 -0
- data/docs/architecture/r8-unit15.md +65 -0
- data/docs/architecture/r8-unit16.md +54 -0
- data/docs/architecture/r8-unit17.md +73 -0
- data/docs/architecture/r8-unit18.md +92 -0
- data/docs/architecture/r8-unit19.md +61 -0
- data/docs/architecture/r8-unit2.md +55 -0
- data/docs/architecture/r8-unit20.md +51 -0
- data/docs/architecture/r8-unit21.md +79 -0
- data/docs/architecture/r8-unit22.md +74 -0
- data/docs/architecture/r8-unit23.md +81 -0
- data/docs/architecture/r8-unit3.md +130 -0
- data/docs/architecture/r8-unit4.md +79 -0
- data/docs/architecture/r8-unit5.md +115 -0
- data/docs/architecture/r8-unit6.md +71 -0
- data/docs/architecture/r8-unit7.md +70 -0
- data/docs/architecture/r8-unit8.md +78 -0
- data/docs/architecture/r8-unit9.md +73 -0
- data/docs/architecture/refactoring-closure.md +343 -0
- data/docs/architecture/remaining-refactoring-plan.md +390 -0
- data/docs/architecture/rubyllm-2-token-ownership.md +82 -0
- data/docs/architecture/storage-execution-boundaries.md +54 -0
- data/docs/architecture/tool-schema-recording-gap.md +56 -0
- data/docs/architecture/tracing.md +2 -2
- data/docs/architecture/workflow-terminal-ownership-design.md +104 -0
- data/docs/architecture.md +222 -0
- data/docs/decisions/014-unified-persistence-durable-state.md +11 -0
- data/docs/decisions/015-tool-public-facade-and-rbs-boundary.md +3 -3
- data/docs/decisions/024-event-loop-single-writer-agent-runtime.md +5 -1
- data/docs/decisions/025-process-local-agent-ownership-and-runtime-admission.md +13 -3
- data/docs/decisions/026-workflow-runtime-admission-and-durable-terminal-barrier.md +5 -1
- data/docs/decisions/027-llm-adapter-provider-boundary.md +2 -0
- data/docs/decisions/030-agent-handoff-domain-and-durable-responsibility.md +7 -0
- data/docs/decisions/031-durable-multi-agent-coordination.md +7 -0
- data/docs/decisions/032-storage-backend-composition.md +85 -0
- data/docs/decisions/033-domain-persistence-ownership.md +79 -0
- data/docs/decisions/034-handoff-runner-coordination-ownership.md +78 -0
- data/docs/decisions/035-tool-executor-capability-ownership.md +68 -0
- data/docs/decisions/036-context-contract-ownership.md +86 -0
- data/docs/decisions/037-common-definition-ownership.md +62 -0
- data/docs/decisions/038-responsibility-based-source-layout.md +145 -0
- data/docs/decisions/039-runtime-configuration-lifecycle-ownership.md +82 -0
- data/docs/decisions/040-configuration-default-composition.md +105 -0
- data/docs/decisions/041-feature-owned-identity-registries.md +100 -0
- data/docs/decisions/042-feature-owned-execution-state.md +106 -0
- data/docs/decisions/043-storage-execution-constraint-notifications.md +75 -0
- data/docs/decisions/044-agent-default-and-one-shot-composition.md +106 -0
- data/docs/decisions/045-worker-input-restriction-ownership.md +104 -0
- data/docs/decisions/046-agent-responsibility-layout-and-shared-records.md +110 -0
- data/docs/decisions/047-recovered-execution-continuation-contract.md +116 -0
- data/docs/decisions/048-dispatch-preparation-worker-ownership.md +120 -0
- data/docs/decisions/049-initial-preparation-worker-ownership.md +111 -0
- data/docs/decisions/050-approval-resume-snapshot-and-commit-ownership.md +98 -0
- data/docs/decisions/051-execution-outcome-worker-ownership.md +153 -0
- data/docs/decisions/052-tool-invocation-restoration-ownership.md +92 -0
- data/docs/decisions/053-shared-state-coordination-ownership.md +71 -0
- data/docs/decisions/054-workflow-terminal-save-single-owner.md +76 -0
- data/docs/decisions/055-terminal-observer-failure-settlement.md +60 -0
- data/docs/decisions/056-workflow-terminal-policy-ownership.md +87 -0
- data/docs/decisions/057-storage-transaction-boundaries.md +73 -0
- data/docs/decisions/058-neutral-storage-primitives.md +83 -0
- data/docs/decisions/059-backend-contracts-and-async-clients.md +74 -0
- data/docs/decisions/060-runtime-settings-and-application-configuration.md +86 -0
- data/docs/decisions/061-persistence-failure-contracts.md +89 -0
- data/docs/decisions/062-content-store-service.md +52 -0
- data/docs/decisions/063-execution-contracts-and-services.md +88 -0
- data/docs/decisions/064-agent-execution-and-multi-agent-responsibility.md +36 -0
- data/docs/decisions/065-vector-and-embeddings-common-operations.md +38 -0
- data/docs/decisions/066-llm-and-tool-operation-contracts.md +20 -0
- data/docs/decisions/067-agent-progress-and-engine-connection.md +29 -0
- data/docs/decisions/068-agent-tool-child-progress-and-tool-operation.md +36 -0
- data/docs/decisions/README.md +52 -6
- data/docs/design/durable-semantic-coordination/IMPLEMENTATION_DESIGN_V2.md +6 -0
- data/docs/design/durable-semantic-coordination/IMPLEMENTATION_REPORT.md +6 -0
- data/docs/execution-boundaries-preview.md +45 -0
- data/docs/features.md +16 -5
- data/docs/getting-started.md +11 -8
- data/docs/migrations/0.15.md +5 -0
- data/docs/migrations/0.22.md +2 -2
- data/docs/migrations/durable-semantic-coordination-v2.md +8 -0
- data/docs/migrations/handoff-runner-multi-agent.md +37 -0
- data/docs/migrations/neutral-storage-spi.md +43 -0
- data/docs/migrations/parallel-tool-chat-removal.md +49 -0
- data/docs/migrations/persistence-failure-contracts.md +67 -0
- data/docs/migrations/r8-unit10.md +36 -0
- data/docs/migrations/r8-unit11.md +33 -0
- data/docs/migrations/r8-unit12.md +31 -0
- data/docs/migrations/r8-unit13.md +36 -0
- data/docs/migrations/r8-unit14.md +31 -0
- data/docs/migrations/r8-unit15.md +31 -0
- data/docs/migrations/r8-unit16.md +26 -0
- data/docs/migrations/r8-unit17.md +28 -0
- data/docs/migrations/r8-unit18.md +29 -0
- data/docs/migrations/r8-unit19.md +28 -0
- data/docs/migrations/r8-unit20.md +24 -0
- data/docs/migrations/r8-unit21.md +28 -0
- data/docs/migrations/r8-unit22.md +29 -0
- data/docs/migrations/r8-unit23.md +20 -0
- data/docs/migrations/r8-unit4.md +67 -0
- data/docs/migrations/r8-unit5.md +75 -0
- data/docs/migrations/r8-unit6.md +109 -0
- data/docs/migrations/r8-unit7.md +31 -0
- data/docs/migrations/r8-unit8.md +33 -0
- data/docs/migrations/r8-unit9.md +28 -0
- data/docs/migrations/shared-state-multi-agent.md +44 -0
- data/docs/migrations/storage-async-client.md +103 -0
- data/docs/migrations/storage-backend-composition.md +117 -0
- data/docs/migrations/storage-transaction-boundaries.md +74 -0
- data/docs/migrations/vector-async-clients.md +110 -0
- data/docs/persistence-backends.md +190 -215
- data/docs/runtime-and-concurrency.md +142 -32
- data/lib/phronomy/agent/admission.rb +78 -0
- data/lib/phronomy/{agent_already_exists_error.rb → agent/agent_already_exists_error.rb} +2 -0
- data/lib/phronomy/{agent_busy_error.rb → agent/agent_busy_error.rb} +2 -0
- data/lib/phronomy/{agent_purged_error.rb → agent/agent_purged_error.rb} +2 -0
- data/lib/phronomy/agent/api/agent.rb +17 -0
- data/lib/phronomy/{execution_rehydration_required_error.rb → agent/api/execution_rehydration_required_error.rb} +2 -0
- data/lib/phronomy/agent/async_event_api.rb +38 -33
- data/lib/phronomy/agent/base.rb +106 -294
- data/lib/phronomy/agent/before_llm_input.rb +64 -0
- data/lib/phronomy/{agent.rb → agent/composition/run_once.rb} +4 -13
- data/lib/phronomy/agent/context_candidate_resolver.rb +2 -2
- data/lib/phronomy/agent/context_importer.rb +2 -2
- data/lib/phronomy/agent/{context_assembler.rb → context_preparation.rb} +157 -288
- data/lib/phronomy/agent/control_request.rb +21 -0
- data/lib/phronomy/agent/{agent_execution.rb → execution/agent_execution.rb} +8 -4
- data/lib/phronomy/agent/{agent_invocation.rb → execution/agent_invocation.rb} +32 -63
- data/lib/phronomy/agent/execution/approval_resume_commit.rb +108 -0
- data/lib/phronomy/agent/execution/dispatch_preparation.rb +195 -0
- data/lib/phronomy/agent/{exact_execution.rb → execution/exact_execution.rb} +43 -23
- data/lib/phronomy/agent/{execution_cancellation.rb → execution/execution_cancellation.rb} +8 -5
- data/lib/phronomy/agent/execution/execution_coordinator.rb +1744 -0
- data/lib/phronomy/agent/execution/execution_failure.rb +30 -0
- data/lib/phronomy/agent/execution/execution_metadata.rb +54 -0
- data/lib/phronomy/agent/execution/execution_outcome_committer.rb +288 -0
- data/lib/phronomy/agent/execution/execution_registry.rb +463 -0
- data/lib/phronomy/agent/execution/execution_session_runner.rb +113 -0
- data/lib/phronomy/agent/execution/initial_preparation.rb +323 -0
- data/lib/phronomy/agent/execution/invocation_actions.rb +368 -0
- data/lib/phronomy/agent/execution/invocation_transitions.rb +124 -0
- data/lib/phronomy/agent/execution/runtime_record_encoder.rb +210 -0
- data/lib/phronomy/agent/execution_change.rb +161 -0
- data/lib/phronomy/agent/execution_environment.rb +36 -0
- data/lib/phronomy/agent/execution_extension_state.rb +21 -0
- data/lib/phronomy/agent/execution_observation.rb +35 -0
- data/lib/phronomy/agent/filterable.rb +182 -0
- data/lib/phronomy/agent/{handoff_context.rb → handoff/transfer_context.rb} +6 -6
- data/lib/phronomy/agent/{handoff_projection.rb → handoff/transfer_projection.rb} +14 -33
- data/lib/phronomy/agent/handoff_error.rb +7 -0
- data/lib/phronomy/agent/{journal_projection.rb → journal/journal_projection.rb} +4 -0
- data/lib/phronomy/agent/{journal_record.rb → journal/journal_record.rb} +3 -3
- data/lib/phronomy/agent/{llm_call_record.rb → journal/llm_call_record.rb} +2 -2
- data/lib/phronomy/agent/knowledge_item.rb +13 -0
- data/lib/phronomy/agent/{agent_root.rb → lifecycle/agent_root.rb} +6 -2
- data/lib/phronomy/agent/lifecycle/default_persistence.rb +29 -0
- data/lib/phronomy/{engine/runtime/agent_ownership_registry.rb → agent/lifecycle/ownership_registry.rb} +24 -15
- data/lib/phronomy/agent/persistence/agent_repository.rb +69 -0
- data/lib/phronomy/agent/persistence/cancellation_repository.rb +51 -0
- data/lib/phronomy/agent/persistence/codec.rb +338 -0
- data/lib/phronomy/agent/persistence/execution_repository.rb +124 -0
- data/lib/phronomy/agent/persistence/journal_repository.rb +63 -0
- data/lib/phronomy/agent/persistence/records.rb +33 -0
- data/lib/phronomy/agent/persistence/retention_repository.rb +70 -0
- data/lib/phronomy/agent/persistence/storage_schema.rb +29 -0
- data/lib/phronomy/agent/persistence/watermark.rb +29 -0
- data/lib/phronomy/agent/recovery/invocation_restorer.rb +124 -0
- data/lib/phronomy/agent/{recovery_coordinator → recovery/recovery_coordinator}/continuation.rb +28 -51
- data/lib/phronomy/agent/{recovery_coordinator → recovery/recovery_coordinator}/installation.rb +50 -55
- data/lib/phronomy/agent/{recovery_coordinator → recovery/recovery_coordinator}/resolution.rb +38 -43
- data/lib/phronomy/agent/{recovery_coordinator.rb → recovery/recovery_coordinator.rb} +26 -23
- data/lib/phronomy/agent/recovery/recovery_rules.rb +119 -0
- data/lib/phronomy/agent/recovery/recovery_support.rb +224 -0
- data/lib/phronomy/agent/reserved_execution.rb +16 -0
- data/lib/phronomy/agent/retention.rb +17 -0
- data/lib/phronomy/agent/runtime_binding/engine_environment.rb +60 -0
- data/lib/phronomy/agent/runtime_binding/engine_session_builder.rb +34 -0
- data/lib/phronomy/agent/runtime_binding/phase_machine_builder.rb +64 -0
- data/lib/phronomy/agent/runtime_binding/tool_session_builder.rb +34 -0
- data/lib/phronomy/agent/{ruby_llm_materializer.rb → runtime_input.rb} +19 -39
- data/lib/phronomy/agent/saved_context_reader.rb +55 -0
- data/lib/phronomy/agent/settings.rb +47 -0
- data/lib/phronomy/agent/state_writer.rb +166 -0
- data/lib/phronomy/agent/store.rb +253 -0
- data/lib/phronomy/{stream_callback_error.rb → agent/stream_callback_error.rb} +2 -0
- data/lib/phronomy/agent/{approval_evaluation_request.rb → tool_execution/approval_evaluation_request.rb} +1 -12
- data/lib/phronomy/agent/{tool_approval_request.rb → tool_execution/tool_approval_request.rb} +1 -10
- data/lib/phronomy/agent/tool_execution/tool_binding.rb +30 -0
- data/lib/phronomy/agent/{tool_invocation.rb → tool_execution/tool_invocation.rb} +114 -248
- data/lib/phronomy/agent/tool_execution/tool_invocation_actions.rb +107 -0
- data/lib/phronomy/agent/tool_execution/tool_invocation_transitions.rb +70 -0
- data/lib/phronomy/common/configuration_error.rb +7 -0
- data/lib/phronomy/common/error.rb +5 -0
- data/lib/phronomy/{agent → common/values}/immutable.rb +9 -1
- data/lib/phronomy/common/values/serializable.rb +32 -0
- data/lib/phronomy/configuration/runtime_settings.rb +49 -0
- data/lib/phronomy/content_store/storage_schema.rb +12 -0
- data/lib/phronomy/content_store/stored_contents.rb +44 -0
- data/lib/phronomy/context/assembly.rb +202 -0
- data/lib/phronomy/{context_budget_exceeded_error.rb → context/budget_exceeded_error.rb} +5 -1
- data/lib/phronomy/context/candidate.rb +51 -0
- data/lib/phronomy/context/constraint.rb +47 -0
- data/lib/phronomy/{agent → context}/context_plan.rb +2 -2
- data/lib/phronomy/{agent → context}/context_plan_validator.rb +3 -3
- data/lib/phronomy/{agent → context}/context_policy.rb +1 -1
- data/lib/phronomy/{agent → context}/context_policy_input.rb +7 -7
- data/lib/phronomy/{agent → context}/context_policy_input_builder.rb +1 -1
- data/lib/phronomy/context/default_policy.rb +140 -0
- data/lib/phronomy/context/final_budget_validator.rb +33 -0
- data/lib/phronomy/{agent → context}/llm_input_build_context.rb +2 -2
- data/lib/phronomy/{agent → context}/llm_input_manifest.rb +21 -21
- data/lib/phronomy/{agent → context}/llm_input_patch.rb +3 -3
- data/lib/phronomy/context/manifest_error.rb +10 -0
- data/lib/phronomy/context/prompt_template.rb +98 -0
- data/lib/phronomy/{vector_store → documents}/loader/base.rb +1 -1
- data/lib/phronomy/{vector_store → documents}/loader/csv_loader.rb +2 -2
- data/lib/phronomy/{vector_store → documents}/loader/markdown_loader.rb +3 -3
- data/lib/phronomy/{vector_store → documents}/loader/plain_text_loader.rb +2 -2
- data/lib/phronomy/{vector_store → documents}/splitter/base.rb +1 -1
- data/lib/phronomy/{vector_store → documents}/splitter/fixed_size_splitter.rb +2 -2
- data/lib/phronomy/{vector_store → documents}/splitter/recursive_splitter.rb +2 -2
- data/lib/phronomy/embeddings/async/async_client.rb +44 -0
- data/lib/phronomy/embeddings/backends/ruby_llm_embeddings.rb +48 -0
- data/lib/phronomy/embeddings/base.rb +43 -0
- data/lib/phronomy/embeddings/invalid_result_error.rb +11 -0
- data/lib/phronomy/embeddings/transport_error.rb +11 -0
- data/lib/phronomy/engine/concurrency/offload_pool.rb +39 -20
- data/lib/phronomy/engine/concurrency/pool_registry.rb +47 -12
- data/lib/phronomy/{diagnostics.rb → engine/diagnostics.rb} +1 -1
- data/lib/phronomy/engine/event_loop.rb +164 -630
- data/lib/phronomy/engine/execution_binding.rb +23 -0
- data/lib/phronomy/engine/execution_receiver_binding.rb +43 -0
- data/lib/phronomy/engine/fsm_protocol.rb +9 -0
- data/lib/phronomy/engine/fsm_session.rb +37 -30
- data/lib/phronomy/engine/metrics.rb +28 -0
- data/lib/phronomy/engine/runtime/timer_queue.rb +1 -1
- data/lib/phronomy/engine/runtime.rb +104 -95
- data/lib/phronomy/engine/terminal_decision.rb +7 -0
- data/lib/phronomy/execution/async_operation.rb +41 -0
- data/lib/phronomy/execution/backpressure_error.rb +7 -0
- data/lib/phronomy/{blocking.rb → execution/blocking.rb} +2 -2
- data/lib/phronomy/execution/cancellation_error.rb +7 -0
- data/lib/phronomy/{engine → execution}/concurrency/cancellation_token.rb +5 -1
- data/lib/phronomy/{engine → execution}/concurrency/deadline.rb +0 -17
- data/lib/phronomy/{engine → execution}/concurrency/physical_completion_task.rb +1 -1
- data/lib/phronomy/{engine → execution}/concurrency/result_collector.rb +1 -1
- data/lib/phronomy/{engine → execution}/concurrency/result_composition.rb +33 -12
- data/lib/phronomy/{engine/concurrency/subscriptions.rb → execution/concurrency/result_subscriptions.rb} +14 -18
- data/lib/phronomy/execution/concurrency/subscriptions.rb +25 -0
- data/lib/phronomy/execution/concurrency/worker_input_restricted.rb +14 -0
- data/lib/phronomy/execution/event_loop_reentrancy_error.rb +8 -0
- data/lib/phronomy/{execution.rb → execution/execution.rb} +57 -30
- data/lib/phronomy/{execution_cancellation_error.rb → execution/execution_cancellation_error.rb} +2 -0
- data/lib/phronomy/execution/execution_receiver.rb +78 -0
- data/lib/phronomy/execution/execution_scope_state.rb +35 -0
- data/lib/phronomy/{execution_timeout_error.rb → execution/execution_timeout_error.rb} +2 -0
- data/lib/phronomy/{invalid_async_entry_action_error.rb → execution/invalid_async_entry_action_error.rb} +2 -0
- data/lib/phronomy/{invalid_async_transition_action_error.rb → execution/invalid_async_transition_action_error.rb} +2 -0
- data/lib/phronomy/{invalid_async_workflow_action_error.rb → execution/invalid_async_workflow_action_error.rb} +2 -0
- data/lib/phronomy/{invocation_context.rb → execution/invocation_context.rb} +0 -15
- data/lib/phronomy/execution/invocation_controls.rb +73 -0
- data/lib/phronomy/execution/pool_shutdown_error.rb +7 -0
- data/lib/phronomy/execution/recursion_limit_error.rb +7 -0
- data/lib/phronomy/execution/runnable.rb +24 -0
- data/lib/phronomy/execution/runtime_shutdown_error.rb +7 -0
- data/lib/phronomy/execution/runtime_shutdown_reentrancy_error.rb +7 -0
- data/lib/phronomy/execution/scheduler_reentrancy_error.rb +9 -0
- data/lib/phronomy/{engine → execution}/task_result.rb +7 -3
- data/lib/phronomy/execution/timeout_error.rb +7 -0
- data/lib/phronomy/execution/wait_policy.rb +24 -0
- data/lib/phronomy/filter/filter_block_error.rb +14 -0
- data/lib/phronomy/generation/generator_verifier/agent_operation.rb +65 -0
- data/lib/phronomy/generation/generator_verifier/agent_result_receiver.rb +89 -0
- data/lib/phronomy/generation/generator_verifier/default_parser.rb +27 -0
- data/lib/phronomy/generation/generator_verifier/pipeline_state.rb +57 -0
- data/lib/phronomy/generation/generator_verifier/workflow_builder.rb +112 -0
- data/lib/phronomy/generation/generator_verifier.rb +118 -0
- data/lib/phronomy/generation/low_confidence_error.rb +14 -0
- data/lib/phronomy/llm_adapter/async/async_client.rb +47 -0
- data/lib/phronomy/llm_adapter/authentication_error.rb +9 -0
- data/lib/phronomy/llm_adapter/backends/ruby_llm.rb +183 -0
- data/lib/phronomy/llm_adapter/base.rb +49 -68
- data/lib/phronomy/llm_adapter/context_length_error.rb +9 -0
- data/lib/phronomy/llm_adapter/invalid_result_error.rb +10 -0
- data/lib/phronomy/llm_adapter/message.rb +62 -0
- data/lib/phronomy/llm_adapter/rate_limit_error.rb +9 -0
- data/lib/phronomy/llm_adapter/request.rb +79 -0
- data/lib/phronomy/llm_adapter/response.rb +36 -0
- data/lib/phronomy/llm_adapter/stream_chunk.rb +18 -0
- data/lib/phronomy/llm_adapter/token_usage.rb +64 -0
- data/lib/phronomy/llm_adapter/transport_error.rb +9 -0
- data/lib/phronomy/llm_adapter.rb +3 -16
- data/lib/phronomy/llm_context_window/token_budget.rb +6 -7
- data/lib/phronomy/multi_agent/admission_registry.rb +15 -2
- data/lib/phronomy/multi_agent/durable_subagent_coordinator.rb +108 -84
- data/lib/phronomy/multi_agent/execution_environment.rb +29 -0
- data/lib/phronomy/{agent → multi_agent}/handoff.rb +2 -2
- data/lib/phronomy/{agent → multi_agent}/handoff_capability_factory.rb +21 -2
- data/lib/phronomy/multi_agent/handoff_participant.rb +79 -0
- data/lib/phronomy/{agent → multi_agent}/handoff_policy.rb +1 -1
- data/lib/phronomy/multi_agent/handoff_runner.rb +156 -0
- data/lib/phronomy/{agent → multi_agent}/handoff_state.rb +2 -2
- data/lib/phronomy/multi_agent/orchestrator.rb +61 -75
- data/lib/phronomy/multi_agent/persistence/codec.rb +75 -0
- data/lib/phronomy/multi_agent/persistence/handoff_state_repository.rb +77 -0
- data/lib/phronomy/multi_agent/persistence/records.rb +27 -0
- data/lib/phronomy/multi_agent/persistence/storage_schema.rb +21 -0
- data/lib/phronomy/multi_agent/persistence/team_execution_repository.rb +124 -0
- data/lib/phronomy/multi_agent/persistence/team_repository.rb +69 -0
- data/lib/phronomy/multi_agent/reserved_child_admission.rb +88 -0
- data/lib/phronomy/multi_agent/runtime_binding/engine_environment.rb +35 -0
- data/lib/phronomy/{agent → multi_agent}/shared_state.rb +56 -39
- data/lib/phronomy/multi_agent/store.rb +156 -0
- data/lib/phronomy/multi_agent/team_coordinator.rb +87 -75
- data/lib/phronomy/multi_agent/team_execution.rb +1 -1
- data/lib/phronomy/{engine/runtime → multi_agent}/team_ownership_registry.rb +5 -5
- data/lib/phronomy/multi_agent/team_root.rb +1 -1
- data/lib/phronomy/output_parser/parse_error.rb +7 -0
- data/lib/phronomy/persistence/content_repository.rb +26 -0
- data/lib/phronomy/persistence/errors.rb +16 -0
- data/lib/phronomy/persistence/migration/initial_format_migration.rb +21 -21
- data/lib/phronomy/persistence/persistence.rb +23 -0
- data/lib/phronomy/persistence/save_outcome.rb +24 -0
- data/lib/phronomy/persistence/snapshot_comparison.rb +81 -0
- data/lib/phronomy/persistence/storage_boundary.rb +32 -0
- data/lib/phronomy/persistence/transaction.rb +86 -0
- data/lib/phronomy/persistence_composition/storage_schema.rb +29 -0
- data/lib/phronomy/persistence_composition/stores.rb +34 -0
- data/lib/phronomy/persistence_composition/unit4_migration.rb +276 -0
- data/lib/phronomy/runtime_composition/agent_defaults.rb +11 -0
- data/lib/phronomy/runtime_composition/configuration.rb +146 -0
- data/lib/phronomy/runtime_composition/configuration_defaults.rb +9 -0
- data/lib/phronomy/runtime_composition/evaluation_defaults.rb +7 -0
- data/lib/phronomy/runtime_composition/execution_defaults.rb +5 -0
- data/lib/phronomy/runtime_composition/generation_defaults.rb +6 -0
- data/lib/phronomy/runtime_composition/global_configuration.rb +38 -0
- data/lib/phronomy/runtime_composition/global_runtime.rb +19 -0
- data/lib/phronomy/runtime_composition/multi_agent_defaults.rb +9 -0
- data/lib/phronomy/runtime_composition/workflow_defaults.rb +4 -0
- data/lib/phronomy/storage/async/async_client.rb +60 -0
- data/lib/phronomy/storage/backend.rb +107 -0
- data/lib/phronomy/storage/backends/in_memory.rb +157 -0
- data/lib/phronomy/storage/blob_conflict_error.rb +10 -0
- data/lib/phronomy/storage/blobs.rb +31 -0
- data/lib/phronomy/storage/condition.rb +25 -0
- data/lib/phronomy/storage/condition_failed_error.rb +16 -0
- data/lib/phronomy/storage/conflict_error.rb +9 -0
- data/lib/phronomy/{persistence → storage}/durable_record.rb +12 -12
- data/lib/phronomy/storage/entry.rb +33 -0
- data/lib/phronomy/storage/guard_ref.rb +13 -0
- data/lib/phronomy/storage/not_found_error.rb +9 -0
- data/lib/phronomy/storage/record_codec.rb +177 -0
- data/lib/phronomy/storage/records.rb +61 -0
- data/lib/phronomy/storage/resource.rb +126 -0
- data/lib/phronomy/storage/scope.rb +25 -0
- data/lib/phronomy/storage/serialization_error.rb +9 -0
- data/lib/phronomy/storage/streams.rb +44 -0
- data/lib/phronomy/storage/transaction_error.rb +10 -0
- data/lib/phronomy/storage/unique_constraint_error.rb +17 -0
- data/lib/phronomy/storage/unsupported_backend_error.rb +9 -0
- data/lib/phronomy/storage/validation.rb +53 -0
- data/lib/phronomy/storage/view.rb +131 -0
- data/lib/phronomy/testing/eval/metrics.rb +1 -1
- data/lib/phronomy/testing/eval/scorer/llm_judge.rb +19 -3
- data/lib/phronomy/testing/persistence_contract/a_content_store.rb +1 -1
- data/lib/phronomy/testing/persistence_contract/a_journal_repository.rb +2 -2
- data/lib/phronomy/testing/persistence_contract/a_persistence_backend.rb +54 -48
- data/lib/phronomy/testing/persistence_contract/a_workflow_state_repository.rb +1 -1
- data/lib/phronomy/testing/persistence_contract/an_agent_repository.rb +1 -1
- data/lib/phronomy/testing/persistence_contract/an_execution_repository.rb +5 -5
- data/lib/phronomy/testing/persistence_contract/coordination_repositories.rb +52 -46
- data/lib/phronomy/testing/persistence_contract/neutral_storage_primitives.rb +263 -0
- data/lib/phronomy/testing/persistence_contract/storage_transaction_boundaries.rb +124 -0
- data/lib/phronomy/testing/persistence_contract.rb +4 -0
- data/lib/phronomy/tool/authorization.rb +77 -0
- data/lib/phronomy/tool/base.rb +549 -9
- data/lib/phronomy/tool/call_request.rb +40 -0
- data/lib/phronomy/{agent/tool_definition_set.rb → tool/definition_set.rb} +16 -12
- data/lib/phronomy/tool/operation.rb +88 -0
- data/lib/phronomy/tool/schema.rb +214 -0
- data/lib/phronomy/tool/settings.rb +31 -0
- data/lib/phronomy/tool/tool_error.rb +7 -0
- data/lib/phronomy/{agent → tool}/tool_executor.rb +10 -7
- data/lib/phronomy/tools/agent.rb +22 -90
- data/lib/phronomy/tools/mcp.rb +21 -14
- data/lib/phronomy/tools/vector_search.rb +4 -4
- data/lib/phronomy/tracing/automatic.rb +11 -6
- data/lib/phronomy/tracing/base.rb +1 -1
- data/lib/phronomy/tracing/observation.rb +37 -0
- data/lib/phronomy/tracing/settings.rb +36 -0
- data/lib/phronomy/vector_store/{async_backend.rb → async/async_client.rb} +35 -29
- data/lib/phronomy/vector_store/{in_memory.rb → backends/in_memory.rb} +12 -10
- data/lib/phronomy/vector_store/{pgvector.rb → backends/pgvector.rb} +44 -16
- data/lib/phronomy/vector_store/{redis_search.rb → backends/redis_search.rb} +74 -37
- data/lib/phronomy/vector_store/base.rb +118 -49
- data/lib/phronomy/vector_store/invalid_result_error.rb +12 -0
- data/lib/phronomy/vector_store.rb +2 -17
- data/lib/phronomy/version.rb +1 -1
- data/lib/phronomy/workflow/completion.rb +11 -0
- data/lib/phronomy/{workflow.rb → workflow/execution/workflow.rb} +10 -8
- data/lib/phronomy/workflow/execution/workflow_action_rules.rb +67 -0
- data/lib/phronomy/{workflow_context.rb → workflow/execution/workflow_context.rb} +5 -1
- data/lib/phronomy/workflow/execution/workflow_context_ownership_error.rb +7 -0
- data/lib/phronomy/workflow/execution/workflow_execution_environment.rb +27 -0
- data/lib/phronomy/workflow/execution/workflow_execution_registry.rb +186 -0
- data/lib/phronomy/{workflow_runner.rb → workflow/execution/workflow_runner.rb} +145 -115
- data/lib/phronomy/workflow/execution/workflow_settings.rb +28 -0
- data/lib/phronomy/workflow/persistence/codec.rb +153 -0
- data/lib/phronomy/workflow/persistence/state_repository.rb +63 -0
- data/lib/phronomy/workflow/persistence/storage_schema.rb +13 -0
- data/lib/phronomy/workflow/runtime_binding/workflow_engine_environment.rb +38 -0
- data/lib/phronomy/workflow/runtime_binding/workflow_phase_machine_builder.rb +156 -0
- data/lib/phronomy/workflow/runtime_binding/workflow_terminal_policy.rb +40 -0
- data/lib/phronomy.rb +97 -98
- data/scripts/api_snapshot.rb +36 -16
- data/scripts/storage_spi_snapshot.rb +36 -0
- data/sig/_private/phronomy/agent/settings.rbs +19 -0
- data/sig/_private/phronomy/configuration/runtime_settings.rbs +20 -0
- data/sig/_private/phronomy/llm_adapter/async/async_client.rbs +10 -0
- data/sig/_private/phronomy/multi_agent/store.rbs +8 -0
- data/sig/_private/phronomy/persistence/storage_boundary.rbs +7 -0
- data/sig/_private/phronomy/tool/settings.rbs +13 -0
- data/sig/_private/phronomy/tracing/settings.rbs +15 -0
- data/sig/_private/phronomy/workflow/execution/workflow_settings.rbs +13 -0
- data/sig/phronomy/agent/admission.rbs +19 -0
- data/sig/phronomy/agent/execution_contract.rbs +132 -0
- data/sig/phronomy/agent/execution_environment.rbs +20 -0
- data/sig/phronomy/agent/store.rbs +73 -0
- data/sig/phronomy/agent.rbs +14 -139
- data/sig/phronomy/common.rbs +7 -0
- data/sig/phronomy/content_store/contracts.rbs +13 -0
- data/sig/phronomy/context.rbs +152 -0
- data/sig/phronomy/embeddings/async/async_client.rbs +9 -0
- data/sig/phronomy/embeddings/base.rbs +17 -0
- data/sig/phronomy/engine/concurrency/timer_bindings.rbs +22 -0
- data/sig/phronomy/engine/control_bindings.rbs +11 -0
- data/sig/phronomy/engine/control_protocols.rbs +23 -0
- data/sig/phronomy/engine/execution_binding.rbs +6 -0
- data/sig/phronomy/engine/execution_receiver.rbs +15 -0
- data/sig/phronomy/engine/observability.rbs +13 -0
- data/sig/phronomy/execution/async_operation.rbs +7 -0
- data/sig/phronomy/execution/blocking.rbs +5 -0
- data/sig/phronomy/execution/concurrency/cancellation_token.rbs +13 -0
- data/sig/phronomy/execution/concurrency/deadline.rbs +10 -0
- data/sig/phronomy/execution/concurrency/result_composition.rbs +7 -0
- data/sig/phronomy/execution/control_protocols.rbs +18 -0
- data/sig/phronomy/execution/errors.rbs +7 -0
- data/sig/phronomy/execution/execution.rbs +41 -0
- data/sig/phronomy/execution/execution_receiver.rbs +40 -0
- data/sig/phronomy/execution/execution_scope_state.rbs +9 -0
- data/sig/phronomy/execution/invocation_context.rbs +18 -0
- data/sig/phronomy/execution/invocation_controls.rbs +13 -0
- data/sig/phronomy/execution/task_result.rbs +29 -0
- data/sig/phronomy/extensions.rbs +0 -21
- data/sig/phronomy/handoff.rbs +12 -4
- data/sig/phronomy/llm_adapter/backends/ruby_llm.rbs +6 -0
- data/sig/phronomy/llm_adapter/base.rbs +14 -0
- data/sig/phronomy/llm_adapter/usage.rbs +14 -0
- data/sig/phronomy/llm_adapter/values.rbs +43 -0
- data/sig/phronomy/llm_context_window.rbs +10 -0
- data/sig/phronomy/multi_agent/reserved_child_admission.rbs +8 -0
- data/sig/phronomy/multi_agent/store.rbs +54 -0
- data/sig/phronomy/multi_agent.rbs +24 -3
- data/sig/phronomy/persistence/errors.rbs +18 -0
- data/sig/phronomy/persistence/migration.rbs +13 -0
- data/sig/phronomy/persistence/transaction.rbs +19 -0
- data/sig/phronomy/persistence.rbs +3 -185
- data/sig/phronomy/persistence_composition.rbs +19 -0
- data/sig/phronomy/runtime_composition/configuration.rbs +36 -0
- data/sig/phronomy/storage/async/async_client.rbs +8 -0
- data/sig/phronomy/storage/backends/in_memory.rbs +9 -0
- data/sig/phronomy/storage/contracts.rbs +160 -0
- data/sig/phronomy/tool.rbs +58 -5
- data/sig/phronomy/tracing/observation.rbs +7 -0
- data/sig/phronomy/vector_store/async/async_client.rbs +13 -0
- data/sig/phronomy/vector_store/base.rbs +23 -0
- data/sig/phronomy/workflow/execution/workflow_execution_environment.rbs +14 -0
- data/sig/phronomy/workflow/runtime_binding/workflow_terminal_policy.rbs +7 -0
- data/sig/phronomy/workflow/store.rbs +8 -0
- data/sig/phronomy/workflow.rbs +1 -1
- data/sig/phronomy.rbs +0 -49
- metadata +462 -130
- data/lib/phronomy/agent/agent_invocation_session_builder.rb +0 -584
- data/lib/phronomy/agent/concerns/before_llm_input.rb +0 -66
- data/lib/phronomy/agent/concerns/error_translation.rb +0 -45
- data/lib/phronomy/agent/concerns/filterable.rb +0 -184
- data/lib/phronomy/agent/context/capability/base.rb +0 -507
- data/lib/phronomy/agent/context/instruction/prompt_template.rb +0 -102
- data/lib/phronomy/agent/context_parts/validators/final_budget_validator.rb +0 -37
- data/lib/phronomy/agent/context_policies/default.rb +0 -142
- data/lib/phronomy/agent/execution_coordinator.rb +0 -3151
- data/lib/phronomy/agent/handoff_execution_coordinator.rb +0 -143
- data/lib/phronomy/agent/handoff_request.rb +0 -45
- data/lib/phronomy/agent/handoff_runner.rb +0 -178
- data/lib/phronomy/agent/phase_machine_builder.rb +0 -152
- data/lib/phronomy/agent/provider_call_outcome.rb +0 -106
- data/lib/phronomy/agent/recovery_support.rb +0 -504
- data/lib/phronomy/agent/selection/candidate.rb +0 -53
- data/lib/phronomy/agent/selection/constraint.rb +0 -49
- data/lib/phronomy/agent/token_budget_resolver.rb +0 -70
- data/lib/phronomy/agent/tool_call_intercepted.rb +0 -29
- data/lib/phronomy/agent/tool_invocation_session_builder.rb +0 -276
- data/lib/phronomy/configuration.rb +0 -59
- data/lib/phronomy/engine/concurrency/cancellation_scope.rb +0 -124
- data/lib/phronomy/engine/concurrency/operation_binding.rb +0 -43
- data/lib/phronomy/generator_verifier.rb +0 -369
- data/lib/phronomy/invalid_context_budget_configuration_error.rb +0 -8
- data/lib/phronomy/llm_adapter/ruby_llm.rb +0 -47
- data/lib/phronomy/metrics.rb +0 -26
- data/lib/phronomy/multi_agent/parallel_tool_chat.rb +0 -116
- data/lib/phronomy/persistence/durable_codec.rb +0 -706
- data/lib/phronomy/persistence/in_memory.rb +0 -690
- data/lib/phronomy/persistence/repository_facades.rb +0 -535
- data/lib/phronomy/persistence.rb +0 -276
- data/lib/phronomy/recovery.rb +0 -186
- data/lib/phronomy/ruby_llm_patches.rb +0 -24
- data/lib/phronomy/runnable.rb +0 -54
- data/lib/phronomy/token_usage.rb +0 -85
- data/lib/phronomy/vector_store/embeddings/base.rb +0 -44
- data/lib/phronomy/vector_store/embeddings/ruby_llm_embeddings.rb +0 -47
- data/lib/phronomy/workflow/phase_machine_builder.rb +0 -205
- data/lib/phronomy/workflow_recovery.rb +0 -123
- data/sig/phronomy/llm_adapter.rbs +0 -11
- data/sig/phronomy/runtime.rbs +0 -76
- /data/lib/phronomy/agent/{llm_operation_result.rb → execution/llm_operation_result.rb} +0 -0
- /data/lib/phronomy/{canonical_json.rb → common/canonical_json.rb} +0 -0
- /data/lib/phronomy/{event.rb → engine/event.rb} +0 -0
|
@@ -1,504 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "digest"
|
|
4
|
-
|
|
5
|
-
module Phronomy
|
|
6
|
-
module Agent
|
|
7
|
-
module RecoverySupport
|
|
8
|
-
CONTRACT_VERSION = 1
|
|
9
|
-
RECOVERY_METADATA_KEY = "recovery"
|
|
10
|
-
TOOL_BATCH_METADATA_KEY = "recovery_tool_batch"
|
|
11
|
-
PENDING_LLM_ID_KEY = "pending_llm_call_id"
|
|
12
|
-
PENDING_LLM_STARTED_AT_KEY = "pending_llm_started_at"
|
|
13
|
-
INVOCATION_MODE_KEY = "invocation_mode"
|
|
14
|
-
CONTRACT_VERSION_KEY = "recovery_contract_version"
|
|
15
|
-
|
|
16
|
-
module_function
|
|
17
|
-
|
|
18
|
-
def canonical_copy(value)
|
|
19
|
-
case value
|
|
20
|
-
when Hash
|
|
21
|
-
value.to_h { |key, child| [key.to_s, canonical_copy(child)] }
|
|
22
|
-
when Array
|
|
23
|
-
value.map { |child| canonical_copy(child) }
|
|
24
|
-
when Symbol
|
|
25
|
-
value.to_s
|
|
26
|
-
when String, Integer, Float, TrueClass, FalseClass, NilClass
|
|
27
|
-
value
|
|
28
|
-
else
|
|
29
|
-
if value.respond_to?(:to_h)
|
|
30
|
-
canonical_copy(value.to_h)
|
|
31
|
-
else
|
|
32
|
-
raise ArgumentError,
|
|
33
|
-
"Recovery value is not canonically serializable: #{value.class}"
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def build_tool_batch_snapshot(invocation)
|
|
39
|
-
Array(invocation.tool_invocations).map do |child|
|
|
40
|
-
entry = {
|
|
41
|
-
"tool_invocation_id" => child.id.to_s,
|
|
42
|
-
"tool_call_id" => child.tool_call_id&.to_s,
|
|
43
|
-
"tool_name" => child.tool_name.to_s,
|
|
44
|
-
"llm_call_id" => invocation.tool_batch_llm_call_id&.to_s,
|
|
45
|
-
"raw_arguments" => canonical_copy(child.raw_arguments || {}),
|
|
46
|
-
"arguments" => canonical_copy(child.raw_arguments || {}),
|
|
47
|
-
"status" => child.status.to_s
|
|
48
|
-
}
|
|
49
|
-
if child.execution_completed?
|
|
50
|
-
entry["result"] = canonical_copy(child.result)
|
|
51
|
-
end
|
|
52
|
-
entry.compact
|
|
53
|
-
end.freeze
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def with_recovery_metadata(execution, values)
|
|
57
|
-
execution.with(
|
|
58
|
-
execution_revision: execution.execution_revision,
|
|
59
|
-
metadata: execution.metadata.merge(values)
|
|
60
|
-
)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def semantic_tool_id(execution_id:, llm_call_id:, tool_call_id:, tool_name:)
|
|
64
|
-
source = [
|
|
65
|
-
"tool_invocation",
|
|
66
|
-
execution_id.to_s,
|
|
67
|
-
llm_call_id.to_s,
|
|
68
|
-
tool_call_id.to_s,
|
|
69
|
-
tool_name.to_s
|
|
70
|
-
].join("\0")
|
|
71
|
-
"tool_invocation-#{Digest::SHA256.hexdigest(source)}".freeze
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def manifest_from_ref(agent, ref)
|
|
75
|
-
raw = agent.persistence.contents.fetch_json(ref)
|
|
76
|
-
Phronomy::Agent::LLMInputManifest.from_h(raw)
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
def materialize_projection(agent, manifest_ref)
|
|
80
|
-
manifest = manifest_from_ref(agent, manifest_ref)
|
|
81
|
-
projection = Phronomy::Agent::RubyLLMMaterializer.new(
|
|
82
|
-
agent: agent,
|
|
83
|
-
persistence: agent.persistence
|
|
84
|
-
).materialize(manifest: manifest, manifest_ref: manifest_ref)
|
|
85
|
-
[manifest, projection]
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
def tool_calls_from_outcome(outcome)
|
|
89
|
-
Array(outcome.tool_calls)
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def normalize_provider_outcome(value)
|
|
93
|
-
return value if value.is_a?(Phronomy::Agent::ProviderCallOutcome)
|
|
94
|
-
|
|
95
|
-
if value.is_a?(Hash)
|
|
96
|
-
return Phronomy::Agent::ProviderCallOutcome.from_h(value)
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
captured = Phronomy::Agent::ProviderCallOutcome.capture(value)
|
|
100
|
-
return captured if captured
|
|
101
|
-
|
|
102
|
-
raise ArgumentError,
|
|
103
|
-
"LLM Recovery :succeeded requires a Provider result or ProviderCallOutcome-compatible Hash"
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
def resolution_failure(error)
|
|
107
|
-
{
|
|
108
|
-
"class" => error.class.name.to_s,
|
|
109
|
-
"message" => error.message.to_s
|
|
110
|
-
}.freeze
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def error_from_failure(failure)
|
|
114
|
-
hash = failure.to_h { |key, value| [key.to_s, value] }
|
|
115
|
-
Phronomy::Error.new(
|
|
116
|
-
"#{hash.fetch("class", "Error")}: #{hash.fetch("message", "Recovery-resolved failure")}"
|
|
117
|
-
)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def event_payload(execution, descriptor)
|
|
121
|
-
{
|
|
122
|
-
execution_id: execution.execution_id,
|
|
123
|
-
execution_revision: execution.execution_revision,
|
|
124
|
-
reason: descriptor.fetch(:reason),
|
|
125
|
-
subject: Phronomy::Recovery.normalize_subject(
|
|
126
|
-
descriptor.fetch(:subject)
|
|
127
|
-
),
|
|
128
|
-
allowed_outcomes: Array(
|
|
129
|
-
descriptor.fetch(:allowed_outcomes)
|
|
130
|
-
).map(&:to_sym).freeze,
|
|
131
|
-
facts: Phronomy::Agent::Immutable.copy(
|
|
132
|
-
descriptor.fetch(:facts, {})
|
|
133
|
-
)
|
|
134
|
-
}.freeze
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
def pending_llm_descriptor(execution)
|
|
138
|
-
llm_call_id = execution.metadata[PENDING_LLM_ID_KEY]
|
|
139
|
-
return unless llm_call_id
|
|
140
|
-
|
|
141
|
-
{
|
|
142
|
-
reason: :outcome_unknown,
|
|
143
|
-
subject: {
|
|
144
|
-
type: :llm_call,
|
|
145
|
-
llm_call_id: llm_call_id
|
|
146
|
-
},
|
|
147
|
-
allowed_outcomes: Phronomy::Recovery::OUTCOMES,
|
|
148
|
-
facts: {
|
|
149
|
-
manifest_ref: execution.metadata["manifest_ref"],
|
|
150
|
-
call_sequence: execution.llm_calls.length + 1
|
|
151
|
-
}.compact.freeze
|
|
152
|
-
}.freeze
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
def recovery_hash(execution)
|
|
156
|
-
value = execution.metadata[RECOVERY_METADATA_KEY]
|
|
157
|
-
value.is_a?(Hash) ? value : nil
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def current_tool_descriptor(execution)
|
|
161
|
-
recovery = recovery_hash(execution)
|
|
162
|
-
return unless recovery
|
|
163
|
-
|
|
164
|
-
subjects = Array(recovery["subjects"] || recovery[:subjects])
|
|
165
|
-
current = subjects.find do |entry|
|
|
166
|
-
hash = entry.to_h { |key, value| [key.to_s, value] }
|
|
167
|
-
hash.fetch("state", "unresolved") == "unresolved"
|
|
168
|
-
end
|
|
169
|
-
return unless current
|
|
170
|
-
|
|
171
|
-
hash = current.to_h { |key, value| [key.to_s, value] }
|
|
172
|
-
{
|
|
173
|
-
reason: (recovery["reason"] || recovery[:reason] || "outcome_unknown").to_sym,
|
|
174
|
-
subject: {
|
|
175
|
-
type: :tool_invocation,
|
|
176
|
-
tool_invocation_id: hash.fetch("tool_invocation_id")
|
|
177
|
-
},
|
|
178
|
-
allowed_outcomes: Array(
|
|
179
|
-
recovery["allowed_outcomes"] ||
|
|
180
|
-
recovery[:allowed_outcomes] ||
|
|
181
|
-
Phronomy::Recovery::OUTCOMES
|
|
182
|
-
).map(&:to_sym),
|
|
183
|
-
facts: {
|
|
184
|
-
tool_call_id: hash["tool_call_id"],
|
|
185
|
-
tool_name: hash["tool_name"],
|
|
186
|
-
arguments: hash["arguments"],
|
|
187
|
-
llm_call_id: hash["llm_call_id"]
|
|
188
|
-
}.compact
|
|
189
|
-
}.freeze
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
def pending_tool_subjects(execution)
|
|
193
|
-
Array(execution.metadata[TOOL_BATCH_METADATA_KEY]).filter_map do |entry|
|
|
194
|
-
hash = entry.to_h { |key, value| [key.to_s, value] }
|
|
195
|
-
next unless %w[authorized awaiting_approval].include?(
|
|
196
|
-
hash.fetch("status")
|
|
197
|
-
)
|
|
198
|
-
|
|
199
|
-
{
|
|
200
|
-
"tool_invocation_id" => hash.fetch("tool_invocation_id"),
|
|
201
|
-
"llm_call_id" => hash["llm_call_id"],
|
|
202
|
-
"tool_call_id" => hash.fetch("tool_call_id"),
|
|
203
|
-
"tool_name" => hash.fetch("tool_name"),
|
|
204
|
-
"arguments" => canonical_copy(
|
|
205
|
-
hash["arguments"] || hash["raw_arguments"] || {}
|
|
206
|
-
),
|
|
207
|
-
"state" => "unresolved"
|
|
208
|
-
}.compact.freeze
|
|
209
|
-
end.freeze
|
|
210
|
-
end
|
|
211
|
-
|
|
212
|
-
def pending_tool_descriptor(execution)
|
|
213
|
-
subjects = pending_tool_subjects(execution)
|
|
214
|
-
return if subjects.empty?
|
|
215
|
-
|
|
216
|
-
first = subjects.first
|
|
217
|
-
{
|
|
218
|
-
reason: :outcome_unknown,
|
|
219
|
-
subject: {
|
|
220
|
-
type: :tool_invocation,
|
|
221
|
-
tool_invocation_id: first.fetch("tool_invocation_id")
|
|
222
|
-
},
|
|
223
|
-
allowed_outcomes: Phronomy::Recovery::OUTCOMES,
|
|
224
|
-
facts: {
|
|
225
|
-
tool_call_id: first["tool_call_id"],
|
|
226
|
-
tool_name: first["tool_name"],
|
|
227
|
-
arguments: first["arguments"],
|
|
228
|
-
llm_call_id: first["llm_call_id"]
|
|
229
|
-
}.compact
|
|
230
|
-
}.freeze
|
|
231
|
-
end
|
|
232
|
-
|
|
233
|
-
def recovery_descriptor(execution)
|
|
234
|
-
case execution.phase.to_sym
|
|
235
|
-
when :calling_llm
|
|
236
|
-
pending_llm_descriptor(execution)
|
|
237
|
-
when :resuming
|
|
238
|
-
approved = execution.approval_request &&
|
|
239
|
-
(
|
|
240
|
-
execution.approval_request["approved"] ||
|
|
241
|
-
execution.approval_request[:approved]
|
|
242
|
-
)
|
|
243
|
-
approved ? pending_tool_descriptor(execution) : nil
|
|
244
|
-
when :dispatching_tools
|
|
245
|
-
pending_tool_descriptor(execution)
|
|
246
|
-
when :recovery_tools
|
|
247
|
-
current_tool_descriptor(execution)
|
|
248
|
-
end
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
def build_tool_subjects(execution, llm_call_id, outcome)
|
|
252
|
-
tool_calls_from_outcome(outcome).map do |call|
|
|
253
|
-
call_hash = canonical_copy(call)
|
|
254
|
-
tool_call_id = call_hash.fetch("id").to_s
|
|
255
|
-
tool_name = call_hash.fetch("name").to_s
|
|
256
|
-
{
|
|
257
|
-
"tool_invocation_id" => semantic_tool_id(
|
|
258
|
-
execution_id: execution.execution_id,
|
|
259
|
-
llm_call_id: llm_call_id,
|
|
260
|
-
tool_call_id: tool_call_id,
|
|
261
|
-
tool_name: tool_name
|
|
262
|
-
),
|
|
263
|
-
"llm_call_id" => llm_call_id.to_s,
|
|
264
|
-
"tool_call_id" => tool_call_id,
|
|
265
|
-
"tool_name" => tool_name,
|
|
266
|
-
"arguments" => canonical_copy(call_hash.fetch("arguments", {})),
|
|
267
|
-
"state" => "unresolved"
|
|
268
|
-
}.freeze
|
|
269
|
-
end.freeze
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
def build_recovery_hash(subjects, reason: :outcome_unknown, allowed_outcomes: Phronomy::Recovery::OUTCOMES)
|
|
273
|
-
{
|
|
274
|
-
"version" => CONTRACT_VERSION,
|
|
275
|
-
"reason" => reason.to_s,
|
|
276
|
-
"allowed_outcomes" => Array(allowed_outcomes).map(&:to_s),
|
|
277
|
-
"subjects" => Array(subjects).map { |entry| canonical_copy(entry) }
|
|
278
|
-
}.freeze
|
|
279
|
-
end
|
|
280
|
-
|
|
281
|
-
def update_recovery_subject(recovery, tool_invocation_id:, state:, outcome:, result_ref: nil)
|
|
282
|
-
copy = canonical_copy(recovery)
|
|
283
|
-
copy["subjects"] = Array(copy.fetch("subjects")).map do |entry|
|
|
284
|
-
next entry unless entry.fetch("tool_invocation_id").to_s == tool_invocation_id.to_s
|
|
285
|
-
|
|
286
|
-
entry.merge(
|
|
287
|
-
"state" => state.to_s,
|
|
288
|
-
"outcome" => outcome.to_s,
|
|
289
|
-
"result_ref" => result_ref
|
|
290
|
-
).compact
|
|
291
|
-
end
|
|
292
|
-
copy.freeze
|
|
293
|
-
end
|
|
294
|
-
|
|
295
|
-
def unresolved_subjects(recovery)
|
|
296
|
-
Array(recovery.fetch("subjects")).select do |entry|
|
|
297
|
-
entry.fetch("state", "unresolved") == "unresolved"
|
|
298
|
-
end
|
|
299
|
-
end
|
|
300
|
-
|
|
301
|
-
def latest_assistant_record(execution, llm_call_id: nil)
|
|
302
|
-
Array(execution.working_records).reverse.find do |record|
|
|
303
|
-
record.kind.to_sym == :assistant_message &&
|
|
304
|
-
(llm_call_id.nil? || record.llm_call_id.to_s == llm_call_id.to_s)
|
|
305
|
-
end
|
|
306
|
-
end
|
|
307
|
-
|
|
308
|
-
def build_invocation_for_suspended(agent, execution, projection, main_coordinator, listener, assistant_message:)
|
|
309
|
-
request = execution.approval_request && Phronomy::Agent::ToolApprovalRequest.from_h(
|
|
310
|
-
execution.approval_request
|
|
311
|
-
)
|
|
312
|
-
assistant_record = latest_assistant_record(execution)
|
|
313
|
-
unless assistant_record
|
|
314
|
-
raise Phronomy::ExecutionRehydrationRequiredError,
|
|
315
|
-
"suspended execution #{execution.execution_id} has no durable assistant Tool Call message"
|
|
316
|
-
end
|
|
317
|
-
|
|
318
|
-
config = {
|
|
319
|
-
execution_id: execution.execution_id,
|
|
320
|
-
phronomy_execution_coordinator: main_coordinator,
|
|
321
|
-
phronomy_runtime_projection: projection
|
|
322
|
-
}.merge(agent.__coordination_config)
|
|
323
|
-
config = agent.__invocation_config(config)
|
|
324
|
-
if execution.metadata["coordination_cancel_requested"]
|
|
325
|
-
config = config.merge(cancellation_token: Phronomy::Concurrency::CancellationToken.new.cancel!)
|
|
326
|
-
end
|
|
327
|
-
invocation = Phronomy::Agent::AgentInvocation.new(
|
|
328
|
-
agent: agent,
|
|
329
|
-
input: projection.ask_message,
|
|
330
|
-
config: config,
|
|
331
|
-
event_listener: listener,
|
|
332
|
-
mode: (execution.metadata[INVOCATION_MODE_KEY] || "invoke").to_sym,
|
|
333
|
-
execution_id: execution.execution_id
|
|
334
|
-
)
|
|
335
|
-
chat = agent.send(:build_chat, model_config: projection.model_config)
|
|
336
|
-
agent.send(
|
|
337
|
-
:_apply_runtime_projection_to_chat,
|
|
338
|
-
chat,
|
|
339
|
-
projection,
|
|
340
|
-
invocation: invocation
|
|
341
|
-
)
|
|
342
|
-
if projection.ask_message
|
|
343
|
-
chat.messages << RubyLLM::Message.new(
|
|
344
|
-
role: :user,
|
|
345
|
-
content: projection.ask_message
|
|
346
|
-
)
|
|
347
|
-
end
|
|
348
|
-
chat.messages << assistant_message
|
|
349
|
-
|
|
350
|
-
invocation.chat = chat
|
|
351
|
-
invocation.user_message_sent = true
|
|
352
|
-
invocation.approval_request = request
|
|
353
|
-
invocation.instance_variable_set(
|
|
354
|
-
:@tool_batch_llm_call_id,
|
|
355
|
-
assistant_record.llm_call_id&.to_s
|
|
356
|
-
)
|
|
357
|
-
|
|
358
|
-
by_id = (request ? request.items : []).to_h do |item|
|
|
359
|
-
[item.tool_invocation_id.to_s, item]
|
|
360
|
-
end
|
|
361
|
-
snapshots = Array(execution.metadata[TOOL_BATCH_METADATA_KEY])
|
|
362
|
-
if snapshots.empty?
|
|
363
|
-
raise Phronomy::ExecutionRehydrationRequiredError,
|
|
364
|
-
"suspended execution #{execution.execution_id} predates the durable Tool batch snapshot"
|
|
365
|
-
end
|
|
366
|
-
|
|
367
|
-
calls_by_id = if assistant_message.tool_calls.respond_to?(:values)
|
|
368
|
-
assistant_message.tool_calls.values.to_h do |call|
|
|
369
|
-
[call.id.to_s, call]
|
|
370
|
-
end
|
|
371
|
-
else
|
|
372
|
-
Array(assistant_message.tool_calls).to_h do |call|
|
|
373
|
-
[call.id.to_s, call]
|
|
374
|
-
end
|
|
375
|
-
end
|
|
376
|
-
|
|
377
|
-
children = snapshots.map do |snapshot|
|
|
378
|
-
entry = snapshot.to_h { |key, value| [key.to_s, value] }
|
|
379
|
-
call = calls_by_id.fetch(entry.fetch("tool_call_id").to_s) do
|
|
380
|
-
raise Phronomy::ExecutionRehydrationRequiredError,
|
|
381
|
-
"Tool Call #{entry.fetch("tool_call_id")} is missing from the durable assistant message"
|
|
382
|
-
end
|
|
383
|
-
tool = chat.tools[entry.fetch("tool_name").to_sym]
|
|
384
|
-
child = if tool
|
|
385
|
-
Phronomy::Agent::ToolInvocation.new(
|
|
386
|
-
execution_id: execution.execution_id,
|
|
387
|
-
agent: agent,
|
|
388
|
-
tool: tool,
|
|
389
|
-
tool_call: call,
|
|
390
|
-
config: config,
|
|
391
|
-
id: entry.fetch("tool_invocation_id")
|
|
392
|
-
)
|
|
393
|
-
else
|
|
394
|
-
Phronomy::Agent::ToolInvocation.missing(
|
|
395
|
-
execution_id: execution.execution_id,
|
|
396
|
-
agent: agent,
|
|
397
|
-
tool_call: call,
|
|
398
|
-
config: config,
|
|
399
|
-
id: entry.fetch("tool_invocation_id")
|
|
400
|
-
)
|
|
401
|
-
end
|
|
402
|
-
restore_tool_snapshot!(child, entry, by_id)
|
|
403
|
-
child
|
|
404
|
-
end
|
|
405
|
-
invocation.tool_invocations = children
|
|
406
|
-
invocation
|
|
407
|
-
end
|
|
408
|
-
|
|
409
|
-
def restore_tool_snapshot!(child, entry, approval_items)
|
|
410
|
-
status = entry.fetch("status").to_sym
|
|
411
|
-
case status
|
|
412
|
-
when :awaiting_approval
|
|
413
|
-
child.validate! unless child.terminal?
|
|
414
|
-
child.instance_variable_set(:@final_decision, :require_approval)
|
|
415
|
-
child.mark_awaiting_approval!
|
|
416
|
-
when :authorized
|
|
417
|
-
child.validate! unless child.terminal?
|
|
418
|
-
child.instance_variable_set(:@final_decision, :allow)
|
|
419
|
-
child.mark_authorized!
|
|
420
|
-
when :completed
|
|
421
|
-
child.instance_variable_set(:@result, entry["result"])
|
|
422
|
-
child.instance_variable_set(:@status, :completed)
|
|
423
|
-
when :rejected
|
|
424
|
-
child.mark_rejected!
|
|
425
|
-
when :failed
|
|
426
|
-
child.mark_framework_failed!(
|
|
427
|
-
Phronomy::ToolError.new("durably restored Tool preflight failure")
|
|
428
|
-
)
|
|
429
|
-
when :cancelled
|
|
430
|
-
child.mark_cancelled!
|
|
431
|
-
else
|
|
432
|
-
raise Phronomy::ExecutionRehydrationRequiredError,
|
|
433
|
-
"unsupported durable Tool snapshot state: #{status.inspect}"
|
|
434
|
-
end
|
|
435
|
-
|
|
436
|
-
item = approval_items[child.id.to_s]
|
|
437
|
-
if item
|
|
438
|
-
child.instance_variable_set(:@facts, Phronomy::Agent::Immutable.copy(item.facts))
|
|
439
|
-
child.instance_variable_set(:@authorization_reason, item.reason)
|
|
440
|
-
end
|
|
441
|
-
child
|
|
442
|
-
end
|
|
443
|
-
|
|
444
|
-
def build_chat_for_recovery(agent, execution, projection, main_coordinator, listener, messages:)
|
|
445
|
-
config = {
|
|
446
|
-
execution_id: execution.execution_id,
|
|
447
|
-
phronomy_execution_coordinator: main_coordinator,
|
|
448
|
-
phronomy_runtime_projection: projection
|
|
449
|
-
}.merge(agent.__coordination_config)
|
|
450
|
-
config = agent.__invocation_config(config)
|
|
451
|
-
if execution.metadata["coordination_cancel_requested"]
|
|
452
|
-
config = config.merge(cancellation_token: Phronomy::Concurrency::CancellationToken.new.cancel!)
|
|
453
|
-
end
|
|
454
|
-
invocation = Phronomy::Agent::AgentInvocation.new(
|
|
455
|
-
agent: agent,
|
|
456
|
-
input: projection.ask_message,
|
|
457
|
-
config: config,
|
|
458
|
-
event_listener: listener,
|
|
459
|
-
mode: (execution.metadata[INVOCATION_MODE_KEY] || "invoke").to_sym,
|
|
460
|
-
execution_id: execution.execution_id
|
|
461
|
-
)
|
|
462
|
-
chat = agent.send(:build_chat, model_config: projection.model_config)
|
|
463
|
-
agent.send(
|
|
464
|
-
:_apply_runtime_projection_to_chat,
|
|
465
|
-
chat,
|
|
466
|
-
projection,
|
|
467
|
-
invocation: invocation
|
|
468
|
-
)
|
|
469
|
-
if projection.ask_message
|
|
470
|
-
chat.messages << RubyLLM::Message.new(
|
|
471
|
-
role: :user,
|
|
472
|
-
content: projection.ask_message
|
|
473
|
-
)
|
|
474
|
-
end
|
|
475
|
-
|
|
476
|
-
messages.each { |message| chat.messages << message }
|
|
477
|
-
|
|
478
|
-
invocation.chat = chat
|
|
479
|
-
invocation.user_message_sent = true
|
|
480
|
-
invocation
|
|
481
|
-
end
|
|
482
|
-
|
|
483
|
-
def provider_output_and_usage(agent, execution)
|
|
484
|
-
assistant = latest_assistant_record(execution)
|
|
485
|
-
payload = assistant ? agent.persistence.contents.fetch_json(assistant.content_ref) : {}
|
|
486
|
-
output = payload["content"]
|
|
487
|
-
|
|
488
|
-
call = execution.llm_calls.last
|
|
489
|
-
usage_hash = if call&.usage_ref
|
|
490
|
-
agent.persistence.contents.fetch_json(call.usage_ref)
|
|
491
|
-
else
|
|
492
|
-
{}
|
|
493
|
-
end
|
|
494
|
-
usage = Phronomy::TokenUsage.new(
|
|
495
|
-
input: usage_hash["input"] || usage_hash[:input],
|
|
496
|
-
output: usage_hash["output"] || usage_hash[:output],
|
|
497
|
-
cached: usage_hash["cached"] || usage_hash[:cached],
|
|
498
|
-
cache_creation: usage_hash["cache_creation"] || usage_hash[:cache_creation]
|
|
499
|
-
)
|
|
500
|
-
[output, usage]
|
|
501
|
-
end
|
|
502
|
-
end
|
|
503
|
-
end
|
|
504
|
-
end
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Phronomy
|
|
4
|
-
module Agent
|
|
5
|
-
module Selection
|
|
6
|
-
Candidate = Data.define(
|
|
7
|
-
:candidate_id,
|
|
8
|
-
:source_kind,
|
|
9
|
-
:category,
|
|
10
|
-
:role,
|
|
11
|
-
:content_ref,
|
|
12
|
-
:record_id,
|
|
13
|
-
:agent_id,
|
|
14
|
-
:execution_id,
|
|
15
|
-
:llm_call_id,
|
|
16
|
-
:tool_call_id,
|
|
17
|
-
:sequence,
|
|
18
|
-
:constraint,
|
|
19
|
-
:priority,
|
|
20
|
-
:metadata
|
|
21
|
-
) do
|
|
22
|
-
def initialize(**values)
|
|
23
|
-
effective_constraint = values[:constraint] ||
|
|
24
|
-
Constraint.selectable(origin: :context_policy)
|
|
25
|
-
unless effective_constraint.is_a?(Constraint)
|
|
26
|
-
raise ArgumentError,
|
|
27
|
-
"Selection::Candidate constraint must be Selection::Constraint"
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
normalized = values.merge(
|
|
31
|
-
candidate_id: values.fetch(:candidate_id).to_s.freeze,
|
|
32
|
-
source_kind: values.fetch(:source_kind).to_sym,
|
|
33
|
-
category: values.fetch(:category).to_sym,
|
|
34
|
-
role: values[:role]&.to_sym,
|
|
35
|
-
content_ref: values.fetch(:content_ref).to_s.freeze,
|
|
36
|
-
record_id: values[:record_id]&.to_s&.freeze,
|
|
37
|
-
agent_id: values[:agent_id]&.to_s&.freeze,
|
|
38
|
-
execution_id: values[:execution_id]&.to_s&.freeze,
|
|
39
|
-
llm_call_id: values[:llm_call_id]&.to_s&.freeze,
|
|
40
|
-
tool_call_id: values[:tool_call_id]&.to_s&.freeze,
|
|
41
|
-
sequence: values[:sequence] && Integer(values[:sequence]),
|
|
42
|
-
constraint: effective_constraint,
|
|
43
|
-
priority: Integer(values[:priority] || 0),
|
|
44
|
-
metadata: Immutable.copy(values[:metadata] || {})
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
super(**normalized)
|
|
48
|
-
freeze
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Phronomy
|
|
4
|
-
module Agent
|
|
5
|
-
module Selection
|
|
6
|
-
CONSTRAINT_MODES = %i[required selectable forbidden].freeze
|
|
7
|
-
|
|
8
|
-
Constraint = Data.define(:mode, :origin, :reason) do
|
|
9
|
-
def initialize(mode:, origin:, reason: nil)
|
|
10
|
-
normalized_mode = mode.to_sym
|
|
11
|
-
unless CONSTRAINT_MODES.include?(normalized_mode)
|
|
12
|
-
raise ArgumentError, "unknown Selection constraint mode: #{mode.inspect}"
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
super(
|
|
16
|
-
mode: normalized_mode,
|
|
17
|
-
origin: origin.to_sym,
|
|
18
|
-
reason: reason&.to_s&.freeze
|
|
19
|
-
)
|
|
20
|
-
freeze
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def required?
|
|
24
|
-
mode == :required
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def selectable?
|
|
28
|
-
mode == :selectable
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def forbidden?
|
|
32
|
-
mode == :forbidden
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def self.required(origin:, reason: nil)
|
|
36
|
-
new(mode: :required, origin: origin, reason: reason)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def self.selectable(origin:, reason: nil)
|
|
40
|
-
new(mode: :selectable, origin: origin, reason: reason)
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def self.forbidden(origin:, reason: nil)
|
|
44
|
-
new(mode: :forbidden, origin: origin, reason: reason)
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Phronomy
|
|
4
|
-
module Agent
|
|
5
|
-
# Resolves a TokenBudget from the effective model configuration that will
|
|
6
|
-
# actually be materialized for one LLM call.
|
|
7
|
-
class TokenBudgetResolver
|
|
8
|
-
def initialize(agent:)
|
|
9
|
-
@agent = agent
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def resolve(model_config)
|
|
13
|
-
config = stringify_keys(model_config)
|
|
14
|
-
model_name = config["model"]
|
|
15
|
-
explicit_window = integer_or_nil(config["context_window"])
|
|
16
|
-
explicit_reserve = integer_or_nil(config["max_output_tokens"])
|
|
17
|
-
|
|
18
|
-
if explicit_window
|
|
19
|
-
reserve = explicit_reserve || configured_default_reserve
|
|
20
|
-
return build_budget(explicit_window, reserve, model_name)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
return nil unless model_name
|
|
24
|
-
|
|
25
|
-
model = RubyLLM.models.find(model_name)
|
|
26
|
-
return nil unless model
|
|
27
|
-
|
|
28
|
-
context_window = model.context_window.to_i
|
|
29
|
-
registry_max = model.max_output_tokens.to_i
|
|
30
|
-
reserve = explicit_reserve || configured_default_reserve
|
|
31
|
-
reserve ||= registry_max if registry_max.positive? && registry_max < context_window
|
|
32
|
-
build_budget(context_window, reserve, model_name)
|
|
33
|
-
rescue RubyLLM::ModelNotFoundError
|
|
34
|
-
nil
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
private
|
|
38
|
-
|
|
39
|
-
def build_budget(context_window, reserve, model_name)
|
|
40
|
-
unless reserve&.positive? && reserve < context_window
|
|
41
|
-
raise Phronomy::InvalidContextBudgetConfigurationError,
|
|
42
|
-
"Cannot determine a valid output reserve for model #{model_name.inspect}; " \
|
|
43
|
-
"set max_output_tokens or Phronomy.configuration.default_output_reserve"
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# Mandatory Manifest content is accounted separately by ContextAssembler
|
|
47
|
-
# and the final budget validator. TokenBudget reserves output capacity only.
|
|
48
|
-
Phronomy::LlmContextWindow::TokenBudget.new(
|
|
49
|
-
context_window: context_window,
|
|
50
|
-
max_output_tokens: reserve
|
|
51
|
-
)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def configured_default_reserve
|
|
55
|
-
integer_or_nil(Phronomy.configuration.default_output_reserve)
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def integer_or_nil(value)
|
|
59
|
-
return nil if value.nil?
|
|
60
|
-
Integer(value)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def stringify_keys(hash)
|
|
64
|
-
hash.to_h.each_with_object({}) do |(key, value), result|
|
|
65
|
-
result[key.to_s] = value
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Phronomy
|
|
4
|
-
module Agent
|
|
5
|
-
# Normal control-transfer signal raised before RubyLLM executes a Tool Call.
|
|
6
|
-
#
|
|
7
|
-
# RubyLLM >= 1.15 guarantees that the complete assistant message has already
|
|
8
|
-
# been added to Chat#messages before before_tool_call runs. Keeping that
|
|
9
|
-
# message here allows Phronomy to persist the complete Provider outcome even
|
|
10
|
-
# though the RubyLLM call itself unwinds through this exception.
|
|
11
|
-
# @api private
|
|
12
|
-
class ToolCallIntercepted < StandardError
|
|
13
|
-
attr_reader :tool_calls, :assistant_message, :assistant_outcome, :llm_call_id
|
|
14
|
-
|
|
15
|
-
def initialize(tool_calls, assistant_message: nil, assistant_outcome: nil, llm_call_id: nil)
|
|
16
|
-
@tool_calls = Array(tool_calls).freeze
|
|
17
|
-
@assistant_message = assistant_message
|
|
18
|
-
@assistant_outcome = assistant_outcome || ProviderCallOutcome.capture(assistant_message)
|
|
19
|
-
@llm_call_id = llm_call_id&.to_s&.freeze
|
|
20
|
-
names = @tool_calls.map(&:name).join(", ")
|
|
21
|
-
super("Tool call intercepted: #{names}")
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def tool_call
|
|
25
|
-
@tool_calls.first
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|