agentevolve-optimizer 0.5.0__py3-none-any.whl
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.
- agent_evolve/__init__.py +722 -0
- agent_evolve/agentic.py +2800 -0
- agent_evolve/api.py +767 -0
- agent_evolve/application/__init__.py +1580 -0
- agent_evolve/application/action_allocation.py +744 -0
- agent_evolve/application/action_allocation_frame.py +347 -0
- agent_evolve/application/action_allocation_frame_commit.py +185 -0
- agent_evolve/application/action_allocation_frame_commit_v3.py +184 -0
- agent_evolve/application/action_allocation_frame_v3.py +338 -0
- agent_evolve/application/action_archive_value.py +497 -0
- agent_evolve/application/action_evidence_consistency.py +455 -0
- agent_evolve/application/action_forecast_partitioning.py +1471 -0
- agent_evolve/application/action_metric_projection.py +211 -0
- agent_evolve/application/action_role_value.py +680 -0
- agent_evolve/application/action_score_authorities.py +363 -0
- agent_evolve/application/action_structural_signature.py +116 -0
- agent_evolve/application/action_target_realization.py +402 -0
- agent_evolve/application/agentic_evolution.py +7734 -0
- agent_evolve/application/agentic_portfolio_residual_expert.py +835 -0
- agent_evolve/application/anchor_residual_identification.py +463 -0
- agent_evolve/application/archive_conditioned_action_target.py +208 -0
- agent_evolve/application/artifact_journal.py +246 -0
- agent_evolve/application/artifact_replay.py +347 -0
- agent_evolve/application/budgeted_optimizer.py +1828 -0
- agent_evolve/application/calibrated_campaign.py +485 -0
- agent_evolve/application/calibrated_current_prefix_forecast_opportunity.py +322 -0
- agent_evolve/application/calibrated_positive_gain_opportunity.py +1581 -0
- agent_evolve/application/campaign_capacity_recourse.py +254 -0
- agent_evolve/application/campaign_contextual_outcomes.py +119 -0
- agent_evolve/application/campaign_diagnostic_blocks.py +930 -0
- agent_evolve/application/campaign_evidence_registry.py +262 -0
- agent_evolve/application/campaign_execution.py +2537 -0
- agent_evolve/application/campaign_generation_audit.py +942 -0
- agent_evolve/application/campaign_learning.py +1812 -0
- agent_evolve/application/campaign_learning_runtime.py +1977 -0
- agent_evolve/application/campaign_search_phase.py +227 -0
- agent_evolve/application/campaign_selector_context_extension.py +220 -0
- agent_evolve/application/campaign_variation_envelope.py +649 -0
- agent_evolve/application/campaign_variation_trace.py +451 -0
- agent_evolve/application/candidate_archive_consequence.py +128 -0
- agent_evolve/application/causal_opportunity_portfolio_gate.py +385 -0
- agent_evolve/application/composite_outcome_updater.py +145 -0
- agent_evolve/application/composition_portfolio_selection.py +363 -0
- agent_evolve/application/concurrent_stage.py +144 -0
- agent_evolve/application/contextual_action_allocation.py +181 -0
- agent_evolve/application/contextual_campaign_outcomes.py +267 -0
- agent_evolve/application/contextual_campaign_planning.py +1366 -0
- agent_evolve/application/contextual_delayed_credit.py +651 -0
- agent_evolve/application/contextual_search_controller.py +2374 -0
- agent_evolve/application/current_prefix_forecast_opportunity.py +714 -0
- agent_evolve/application/decision_metric_projection.py +112 -0
- agent_evolve/application/derived_action_semantics.py +129 -0
- agent_evolve/application/detailed_evaluation.py +449 -0
- agent_evolve/application/earned_lineage.py +1011 -0
- agent_evolve/application/effective_choice_audit.py +484 -0
- agent_evolve/application/empirical_consequence_calibration.py +908 -0
- agent_evolve/application/evaluation_accounting.py +325 -0
- agent_evolve/application/evaluation_cache.py +199 -0
- agent_evolve/application/evaluation_escrow.py +547 -0
- agent_evolve/application/evaluation_recourse.py +253 -0
- agent_evolve/application/event_recorder.py +151 -0
- agent_evolve/application/evolution_campaign.py +1840 -0
- agent_evolve/application/executable_hypothesis.py +323 -0
- agent_evolve/application/factorial_branch_pilot.py +772 -0
- agent_evolve/application/finite_acquisition_capacity_recourse.py +672 -0
- agent_evolve/application/finite_acquisition_residual_expert.py +373 -0
- agent_evolve/application/finite_acquisition_variation_envelope.py +802 -0
- agent_evolve/application/finite_action_hypothesis_semantics.py +446 -0
- agent_evolve/application/finite_action_selection.py +188 -0
- agent_evolve/application/finite_action_set.py +306 -0
- agent_evolve/application/finite_action_transition.py +537 -0
- agent_evolve/application/finite_variation_eligibility.py +296 -0
- agent_evolve/application/forecast_geometry_portfolio.py +799 -0
- agent_evolve/application/forecast_opportunity_shadow_calibration.py +316 -0
- agent_evolve/application/front_proximity_admission.py +311 -0
- agent_evolve/application/front_proximity_parent_basis.py +458 -0
- agent_evolve/application/frozen_hurdle_score.py +659 -0
- agent_evolve/application/g3_causal_screen.py +2257 -0
- agent_evolve/application/g3_causal_validation.py +1046 -0
- agent_evolve/application/g3_postseal_curation.py +818 -0
- agent_evolve/application/gated_agentic_generator.py +205 -0
- agent_evolve/application/generation_feedback.py +293 -0
- agent_evolve/application/generative_proposal_journal.py +185 -0
- agent_evolve/application/geometry_conditional_elasticity.py +453 -0
- agent_evolve/application/global_wave_action_allocation.py +1151 -0
- agent_evolve/application/head_mass_conditional_seat.py +268 -0
- agent_evolve/application/identifiable_reflection_evidence.py +1147 -0
- agent_evolve/application/identifiable_reflection_learning.py +395 -0
- agent_evolve/application/identifiable_reflection_request.py +364 -0
- agent_evolve/application/in_memory_residual_archive.py +341 -0
- agent_evolve/application/insight_memory.py +1804 -0
- agent_evolve/application/live_runtime_manifest.py +758 -0
- agent_evolve/application/llm_task_queue.py +769 -0
- agent_evolve/application/matched_finite_action_block.py +409 -0
- agent_evolve/application/materialized_action_broker.py +2328 -0
- agent_evolve/application/materialized_action_constraints.py +83 -0
- agent_evolve/application/materialized_variation.py +211 -0
- agent_evolve/application/multi_option_evolution.py +1536 -0
- agent_evolve/application/outcome_adaptive_action_racing.py +2827 -0
- agent_evolve/application/outcome_adaptive_residual_campaign_runtime.py +580 -0
- agent_evolve/application/outcome_adaptive_residual_portfolio_evolution.py +3671 -0
- agent_evolve/application/outcome_conditioned_portfolio_selection.py +1374 -0
- agent_evolve/application/outcome_relation.py +193 -0
- agent_evolve/application/paired_allocation_comparison.py +241 -0
- agent_evolve/application/paired_block_schedule.py +127 -0
- agent_evolve/application/parent_measurement.py +226 -0
- agent_evolve/application/pareto_archive.py +811 -0
- agent_evolve/application/portfolio_campaign_runtime.py +4739 -0
- agent_evolve/application/portfolio_evolution.py +2950 -0
- agent_evolve/application/portfolio_hypothesis_observations.py +814 -0
- agent_evolve/application/portfolio_memory_attribution.py +581 -0
- agent_evolve/application/portfolio_memory_dose.py +788 -0
- agent_evolve/application/portfolio_memory_matched_control.py +938 -0
- agent_evolve/application/portfolio_memory_transfer.py +297 -0
- agent_evolve/application/portfolio_optimization_memory.py +363 -0
- agent_evolve/application/portfolio_outcome_feedback.py +1613 -0
- agent_evolve/application/portfolio_projection.py +335 -0
- agent_evolve/application/portfolio_recombination.py +2032 -0
- agent_evolve/application/post_evolution_reflection.py +834 -0
- agent_evolve/application/postcommit_rank_authority.py +245 -0
- agent_evolve/application/precommitted_portfolio_racing.py +2762 -0
- agent_evolve/application/prequential_archive_opportunity_calibration.py +1154 -0
- agent_evolve/application/prequential_residual_exploration.py +343 -0
- agent_evolve/application/prequential_score_portfolio.py +954 -0
- agent_evolve/application/projections.py +292 -0
- agent_evolve/application/protected_action_committee.py +1027 -0
- agent_evolve/application/protected_branch_pilot.py +376 -0
- agent_evolve/application/protected_current_prefix_forecast_opportunity.py +552 -0
- agent_evolve/application/provider_replay.py +910 -0
- agent_evolve/application/rank_balanced_causal_pilot.py +1372 -0
- agent_evolve/application/recombination_residual_expert.py +403 -0
- agent_evolve/application/reflection_workflow.py +571 -0
- agent_evolve/application/region_conditional_credit.py +911 -0
- agent_evolve/application/residual_campaign_runtime.py +531 -0
- agent_evolve/application/residual_headroom_campaign_runtime.py +459 -0
- agent_evolve/application/residual_headroom_ledger.py +1544 -0
- agent_evolve/application/residual_learning_transaction.py +396 -0
- agent_evolve/application/residual_portfolio_evolution.py +1228 -0
- agent_evolve/application/residual_reachability.py +749 -0
- agent_evolve/application/residual_stage_credit.py +499 -0
- agent_evolve/application/same_prefix_paired_audit.py +1580 -0
- agent_evolve/application/semantic_coverage_score_portfolio.py +838 -0
- agent_evolve/application/sequential_lineage_allocation.py +1017 -0
- agent_evolve/application/sequential_market_replay.py +1395 -0
- agent_evolve/application/sequential_residual_campaign_runtime.py +305 -0
- agent_evolve/application/sequential_residual_portfolio_evolution.py +940 -0
- agent_evolve/application/single_score_action_allocation.py +299 -0
- agent_evolve/application/source_exposure_allocation.py +906 -0
- agent_evolve/application/staged_memory.py +210 -0
- agent_evolve/application/stratified_cold_start_allocation.py +732 -0
- agent_evolve/application/support_guarded_hurdle_score.py +549 -0
- agent_evolve/application/target_conditioned_action_forecast.py +595 -0
- agent_evolve/application/target_conditioned_campaign.py +566 -0
- agent_evolve/application/treatment_assignment.py +201 -0
- agent_evolve/application/trusted_objective_evidence.py +217 -0
- agent_evolve/application/two_stage_action_evolution.py +1131 -0
- agent_evolve/application/v8lite_allocation_policy.py +1083 -0
- agent_evolve/application/v9_candidate_policy.py +1303 -0
- agent_evolve/bootstrap.py +108 -0
- agent_evolve/campaign_presets.py +517 -0
- agent_evolve/campaign_profiles.py +452 -0
- agent_evolve/campaign_variation_topology.py +288 -0
- agent_evolve/campaign_workload.py +950 -0
- agent_evolve/cli.py +797 -0
- agent_evolve/contract.py +241 -0
- agent_evolve/core/__init__.py +91 -0
- agent_evolve/core/action_semantics.py +411 -0
- agent_evolve/core/authored.py +105 -0
- agent_evolve/core/formatting.py +286 -0
- agent_evolve/core/optimization_semantics.py +324 -0
- agent_evolve/core/problem.py +167 -0
- agent_evolve/core/results.py +323 -0
- agent_evolve/core/stats.py +70 -0
- agent_evolve/core/telemetry.py +100 -0
- agent_evolve/domain/__init__.py +89 -0
- agent_evolve/domain/artifact.py +162 -0
- agent_evolve/domain/durable_text.py +68 -0
- agent_evolve/domain/event.py +1454 -0
- agent_evolve/domain/finite_action_set.py +426 -0
- agent_evolve/domain/finite_variation.py +526 -0
- agent_evolve/domain/generative_emission.py +559 -0
- agent_evolve/domain/ids.py +163 -0
- agent_evolve/domain/inline_text.py +106 -0
- agent_evolve/domain/insight.py +27 -0
- agent_evolve/domain/lineage.py +737 -0
- agent_evolve/domain/llm_task_queue.py +960 -0
- agent_evolve/domain/outcome.py +96 -0
- agent_evolve/domain/patch.py +854 -0
- agent_evolve/domain/typed_json.py +542 -0
- agent_evolve/domain/variation_space.py +158 -0
- agent_evolve/driver.py +1014 -0
- agent_evolve/harness/__init__.py +29 -0
- agent_evolve/harness/base.py +242 -0
- agent_evolve/harness/directives.py +163 -0
- agent_evolve/harness/generative_seal.py +479 -0
- agent_evolve/harness/registry.py +41 -0
- agent_evolve/infrastructure/__init__.py +39 -0
- agent_evolve/infrastructure/artifacts/__init__.py +6 -0
- agent_evolve/infrastructure/artifacts/_verification.py +67 -0
- agent_evolve/infrastructure/artifacts/filesystem.py +343 -0
- agent_evolve/infrastructure/artifacts/in_memory.py +73 -0
- agent_evolve/infrastructure/asyncio_runtime.py +109 -0
- agent_evolve/infrastructure/authored_runtime.py +188 -0
- agent_evolve/infrastructure/authored_worker.py +171 -0
- agent_evolve/infrastructure/clock.py +53 -0
- agent_evolve/infrastructure/events/__init__.py +6 -0
- agent_evolve/infrastructure/events/_validation.py +89 -0
- agent_evolve/infrastructure/events/in_memory.py +56 -0
- agent_evolve/infrastructure/events/jsonl.py +193 -0
- agent_evolve/infrastructure/exception_provenance.py +215 -0
- agent_evolve/infrastructure/ids.py +118 -0
- agent_evolve/infrastructure/lineage_codec.py +1836 -0
- agent_evolve/infrastructure/outcome_adaptive_phase_journal.py +170 -0
- agent_evolve/infrastructure/residual_headroom_journal.py +221 -0
- agent_evolve/infrastructure/resource_lease.py +370 -0
- agent_evolve/infrastructure/sanitization/__init__.py +8 -0
- agent_evolve/infrastructure/sanitization/strict_json.py +484 -0
- agent_evolve/infrastructure/sequential_phase_journal.py +170 -0
- agent_evolve/infrastructure/stream_liveness.py +383 -0
- agent_evolve/infrastructure/subprocess_boundary.py +136 -0
- agent_evolve/integrations/__init__.py +1 -0
- agent_evolve/integrations/botorch/__init__.py +28 -0
- agent_evolve/integrations/botorch/finite_qlognehvi.py +190 -0
- agent_evolve/integrations/botorch/finite_qlognehvi_batch.py +155 -0
- agent_evolve/integrations/botorch/finite_qlognehvi_batch_identity.py +20 -0
- agent_evolve/integrations/botorch/finite_qlognehvi_batch_worker.py +55 -0
- agent_evolve/integrations/botorch/finite_qlognehvi_identity.py +22 -0
- agent_evolve/integrations/botorch/finite_qlognehvi_worker.py +55 -0
- agent_evolve/integrations/botorch/subprocess_qlognehvi.py +261 -0
- agent_evolve/integrations/botorch/subprocess_qlognehvi_batch.py +273 -0
- agent_evolve/integrations/completion.py +242 -0
- agent_evolve/integrations/pydantic_ai/__init__.py +441 -0
- agent_evolve/integrations/pydantic_ai/action_forecast.py +1068 -0
- agent_evolve/integrations/pydantic_ai/agentic_generator.py +2308 -0
- agent_evolve/integrations/pydantic_ai/async_generator.py +1604 -0
- agent_evolve/integrations/pydantic_ai/boundary_codec.py +1526 -0
- agent_evolve/integrations/pydantic_ai/calibrated_portfolio_campaign.py +756 -0
- agent_evolve/integrations/pydantic_ai/calibrated_portfolio_selection.py +7537 -0
- agent_evolve/integrations/pydantic_ai/campaign_acquisition.py +609 -0
- agent_evolve/integrations/pydantic_ai/execution_binding.py +138 -0
- agent_evolve/integrations/pydantic_ai/forecast_geometry_action_committee.py +217 -0
- agent_evolve/integrations/pydantic_ai/harness.py +159 -0
- agent_evolve/integrations/pydantic_ai/heterogeneous_model_execution.py +306 -0
- agent_evolve/integrations/pydantic_ai/hierarchical_residual_adaptive_semantic_view.py +179 -0
- agent_evolve/integrations/pydantic_ai/json_schema_dialect.py +108 -0
- agent_evolve/integrations/pydantic_ai/materialized_hierarchical_residual_expert.py +952 -0
- agent_evolve/integrations/pydantic_ai/materialized_portfolio_judge.py +520 -0
- agent_evolve/integrations/pydantic_ai/model_execution_profile.py +659 -0
- agent_evolve/integrations/pydantic_ai/outbound_request_manifest.py +1170 -0
- agent_evolve/integrations/pydantic_ai/portable_residual_consequence_features.py +575 -0
- agent_evolve/integrations/pydantic_ai/portfolio_selection.py +422 -0
- agent_evolve/integrations/pydantic_ai/progress_aware_openrouter.py +416 -0
- agent_evolve/integrations/pydantic_ai/provider_attempt_join.py +1523 -0
- agent_evolve/integrations/pydantic_ai/provider_free_calibrated_runner.py +607 -0
- agent_evolve/integrations/pydantic_ai/queued_runner.py +2634 -0
- agent_evolve/integrations/pydantic_ai/reconciled_residual_reachability.py +1417 -0
- agent_evolve/integrations/pydantic_ai/residual_forecast_geometry.py +445 -0
- agent_evolve/integrations/pydantic_ai/residual_reachability.py +674 -0
- agent_evolve/integrations/pydantic_ai/residual_semantic_cells.py +239 -0
- agent_evolve/integrations/pydantic_ai/sealed_output_replay.py +1068 -0
- agent_evolve/integrations/pydantic_ai/semantic_coverage_residual_portfolio.py +770 -0
- agent_evolve/integrations/pydantic_ai/semantic_decision_replay.py +383 -0
- agent_evolve/integrations/pydantic_ai/support_adaptive_residual_portfolio.py +135 -0
- agent_evolve/integrations/pydantic_ai/trusted_residual_prompt_context.py +143 -0
- agent_evolve/integrations/pydantic_ai/validated_openrouter_model.py +107 -0
- agent_evolve/integrations/pymoo_adapter.py +242 -0
- agent_evolve/policies/__init__.py +17 -0
- agent_evolve/policies/check.py +469 -0
- agent_evolve/policies/emit_scaffold.py +451 -0
- agent_evolve/policies/feedback/__init__.py +37 -0
- agent_evolve/policies/feedback/held_out_asn.py +1325 -0
- agent_evolve/policies/genetic.py +607 -0
- agent_evolve/policies/llm_backoff.py +183 -0
- agent_evolve/policies/llm_chooser.py +226 -0
- agent_evolve/policies/llm_generator.py +1760 -0
- agent_evolve/policies/llm_init.py +267 -0
- agent_evolve/policies/llm_operator.py +109 -0
- agent_evolve/policies/llm_prior.py +194 -0
- agent_evolve/policies/llm_surrogate.py +334 -0
- agent_evolve/policies/measurement_evidence.py +704 -0
- agent_evolve/policies/memory/__init__.py +223 -0
- agent_evolve/policies/memory/balanced_subset_blocks.py +707 -0
- agent_evolve/policies/memory/compatibility_matching.py +593 -0
- agent_evolve/policies/memory/global_falsification.py +1841 -0
- agent_evolve/policies/memory/prompt_shape.py +503 -0
- agent_evolve/policies/memory/randomized_subset.py +714 -0
- agent_evolve/policies/memory/staged_causal.py +1270 -0
- agent_evolve/policies/memory/treatment_compliance.py +759 -0
- agent_evolve/policies/objective_resolution/__init__.py +17 -0
- agent_evolve/policies/objective_resolution/fixed_grid.py +364 -0
- agent_evolve/policies/operator_portfolio.py +407 -0
- agent_evolve/policies/reguidance.py +1133 -0
- agent_evolve/policies/reward/__init__.py +83 -0
- agent_evolve/policies/reward/affine_candidate_consequence.py +156 -0
- agent_evolve/policies/reward/affine_candidate_consequence_3d.py +159 -0
- agent_evolve/policies/reward/affine_hypervolume.py +490 -0
- agent_evolve/policies/reward/affine_hypervolume_3d.py +567 -0
- agent_evolve/policies/reward/contextual_marginal_utility.py +318 -0
- agent_evolve/policies/reward/frozen_archive.py +360 -0
- agent_evolve/policies/reward/frozen_wave_archive.py +368 -0
- agent_evolve/policies/search_state.py +208 -0
- agent_evolve/policies/selection/__init__.py +345 -0
- agent_evolve/policies/selection/acquisition_certified_slate.py +684 -0
- agent_evolve/policies/selection/affine_frontier_context.py +330 -0
- agent_evolve/policies/selection/affine_frontier_target.py +473 -0
- agent_evolve/policies/selection/archive_elite.py +1346 -0
- agent_evolve/policies/selection/calibrated_portfolio_binding.py +640 -0
- agent_evolve/policies/selection/calibrated_slate.py +1394 -0
- agent_evolve/policies/selection/calibrated_slate_codec.py +579 -0
- agent_evolve/policies/selection/common_candidate_pool.py +685 -0
- agent_evolve/policies/selection/diagnostic_sampling.py +319 -0
- agent_evolve/policies/selection/disjoint_pairs.py +479 -0
- agent_evolve/policies/selection/elite_explorer.py +719 -0
- agent_evolve/policies/selection/finite_action.py +187 -0
- agent_evolve/policies/selection/finite_option_prompt_projection.py +377 -0
- agent_evolve/policies/selection/finite_palette_evidence.py +247 -0
- agent_evolve/policies/selection/forecast_calibration.py +922 -0
- agent_evolve/policies/selection/frontier_probe_slate.py +814 -0
- agent_evolve/policies/selection/frozen_archive_pairs.py +762 -0
- agent_evolve/policies/selection/full_support_slate.py +91 -0
- agent_evolve/policies/selection/meaningful_direction.py +240 -0
- agent_evolve/policies/selection/memory_dose_feasibility.py +259 -0
- agent_evolve/policies/selection/model_anchored_slate.py +826 -0
- agent_evolve/policies/selection/phenotype_recourse.py +979 -0
- agent_evolve/policies/selection/proposal_support.py +368 -0
- agent_evolve/policies/selection/random_portfolio.py +254 -0
- agent_evolve/policies/selection/regret_bounded_slate.py +1084 -0
- agent_evolve/policies/selection/residual_frontier.py +463 -0
- agent_evolve/policies/selection/residual_frontier_target.py +605 -0
- agent_evolve/policies/selection/structural_posterior_slate.py +1571 -0
- agent_evolve/policies/selection/target_conditioned_allocator.py +648 -0
- agent_evolve/policies/selection/target_conditioned_features.py +812 -0
- agent_evolve/policies/selection/target_conditioned_prequential.py +1527 -0
- agent_evolve/policies/selection/task_keyed_palette.py +906 -0
- agent_evolve/policies/semantics.py +147 -0
- agent_evolve/policies/structure.py +362 -0
- agent_evolve/policies/structured_output_budget.py +62 -0
- agent_evolve/policies/surrogate.py +696 -0
- agent_evolve/policies/variation/__init__.py +1 -0
- agent_evolve/policies/variation/compositional_finite_catalog.py +426 -0
- agent_evolve/policies/variation/crossover_inheritance.py +575 -0
- agent_evolve/policies/variation/disjoint_recombination.py +611 -0
- agent_evolve/policies/variation/exact_composition_capacity.py +214 -0
- agent_evolve/policies/variation/exact_parent_crossover.py +950 -0
- agent_evolve/policies/variation/multiscale_restart_catalog.py +372 -0
- agent_evolve/policies/variation/source_union_finite_catalog.py +403 -0
- agent_evolve/policies/variation/typed_patch.py +1981 -0
- agent_evolve/policies/weighted_prior.py +394 -0
- agent_evolve/ports/__init__.py +383 -0
- agent_evolve/ports/action_allocation.py +733 -0
- agent_evolve/ports/action_allocation_frame.py +1153 -0
- agent_evolve/ports/action_allocation_frame_commit.py +294 -0
- agent_evolve/ports/action_allocation_frame_commit_v3.py +432 -0
- agent_evolve/ports/action_allocation_frame_v3.py +995 -0
- agent_evolve/ports/action_forecast.py +1568 -0
- agent_evolve/ports/action_metric_projection.py +165 -0
- agent_evolve/ports/agentic_generator.py +1561 -0
- agent_evolve/ports/archive_context.py +136 -0
- agent_evolve/ports/artifact_sanitizer.py +44 -0
- agent_evolve/ports/artifact_store.py +225 -0
- agent_evolve/ports/clock.py +13 -0
- agent_evolve/ports/contextual_search_allocation.py +827 -0
- agent_evolve/ports/decision_metric_projection.py +258 -0
- agent_evolve/ports/event_store.py +55 -0
- agent_evolve/ports/executable_hypothesis.py +557 -0
- agent_evolve/ports/finite_acquisition.py +377 -0
- agent_evolve/ports/finite_acquisition_batch.py +296 -0
- agent_evolve/ports/finite_acquisition_batch_json.py +164 -0
- agent_evolve/ports/finite_acquisition_json.py +247 -0
- agent_evolve/ports/finite_acquisition_space.py +168 -0
- agent_evolve/ports/finite_action_selection.py +348 -0
- agent_evolve/ports/finite_action_set.py +256 -0
- agent_evolve/ports/frontier_target.py +396 -0
- agent_evolve/ports/generation_failure.py +43 -0
- agent_evolve/ports/hard_feasibility.py +233 -0
- agent_evolve/ports/id_factory.py +34 -0
- agent_evolve/ports/llm_task_queue.py +93 -0
- agent_evolve/ports/objective_resolution.py +419 -0
- agent_evolve/ports/paired_allocation_comparison.py +401 -0
- agent_evolve/ports/paired_block_schedule.py +475 -0
- agent_evolve/ports/parent_measurement.py +336 -0
- agent_evolve/ports/portfolio_memory_dose.py +643 -0
- agent_evolve/ports/portfolio_selection.py +3169 -0
- agent_evolve/ports/postcommit_rank_authority.py +467 -0
- agent_evolve/ports/presented_action_evidence.py +794 -0
- agent_evolve/ports/resource_lease.py +162 -0
- agent_evolve/ports/structured_generator.py +734 -0
- agent_evolve/ports/structured_output_budget.py +120 -0
- agent_evolve/ports/subprocess_boundary.py +138 -0
- agent_evolve/ports/treatment_assignment.py +466 -0
- agent_evolve/ports/variation_catalog.py +76 -0
- agent_evolve/ports/variation_source.py +226 -0
- agent_evolve/proposal_mode.py +157 -0
- agent_evolve/proposers/__init__.py +10 -0
- agent_evolve/proposers/random_proposer.py +188 -0
- agent_evolve/provider_accounting.py +163 -0
- agent_evolve/py.typed +0 -0
- agent_evolve/reference_method.py +1570 -0
- agent_evolve/session/__init__.py +11 -0
- agent_evolve/session/authorship.py +864 -0
- agent_evolve/session/evaluate.py +236 -0
- agent_evolve/session/fidelity.py +237 -0
- agent_evolve/session/genetic_loop.py +742 -0
- agent_evolve/session/loop.py +803 -0
- agent_evolve/session/screening.py +671 -0
- agent_evolve/settings.py +376 -0
- agent_evolve/workload_kit.py +368 -0
- agent_evolve/workload_prompt.py +398 -0
- agentevolve_optimizer-0.5.0.dist-info/METADATA +599 -0
- agentevolve_optimizer-0.5.0.dist-info/RECORD +414 -0
- agentevolve_optimizer-0.5.0.dist-info/WHEEL +5 -0
- agentevolve_optimizer-0.5.0.dist-info/entry_points.txt +2 -0
- agentevolve_optimizer-0.5.0.dist-info/licenses/LICENSE +21 -0
- agentevolve_optimizer-0.5.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1068 @@
|
|
|
1
|
+
"""Pydantic-AI adapter for enum-coded full and partition-block forecasts.
|
|
2
|
+
|
|
3
|
+
The adapter owns only the prompt/schema boundary and translation into the
|
|
4
|
+
provider-neutral forecast ports. Queueing, retry, transport liveness, model
|
|
5
|
+
routing, partition orchestration, and provider policy remain outside it.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import hashlib
|
|
11
|
+
import json
|
|
12
|
+
import math
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from functools import lru_cache
|
|
15
|
+
from typing import Annotated, Any, ClassVar, Literal, cast
|
|
16
|
+
|
|
17
|
+
from pydantic import (
|
|
18
|
+
BaseModel,
|
|
19
|
+
ConfigDict,
|
|
20
|
+
Field,
|
|
21
|
+
create_model,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
from agent_evolve.domain.finite_variation import FiniteVariationOption
|
|
25
|
+
from agent_evolve.domain.typed_json import thaw_json
|
|
26
|
+
from agent_evolve.integrations.pydantic_ai.agentic_generator import (
|
|
27
|
+
AttemptedStructuredGenerationResponse,
|
|
28
|
+
LowLevelRunner,
|
|
29
|
+
)
|
|
30
|
+
from agent_evolve.ports.action_forecast import (
|
|
31
|
+
ActionEvidenceCitation,
|
|
32
|
+
ActionForecastBlockRequest,
|
|
33
|
+
ActionForecastBlockResult,
|
|
34
|
+
ActionForecastDraft,
|
|
35
|
+
ActionForecastEvidenceMode,
|
|
36
|
+
ActionForecastRequest,
|
|
37
|
+
ActionForecastResult,
|
|
38
|
+
ActionMetricForecast,
|
|
39
|
+
resolve_action_forecast_block,
|
|
40
|
+
resolve_action_forecasts,
|
|
41
|
+
)
|
|
42
|
+
from agent_evolve.ports.agentic_generator import AgenticCallTelemetry
|
|
43
|
+
from agent_evolve.ports.structured_generator import (
|
|
44
|
+
StructuredGenerationRequest,
|
|
45
|
+
StructuredGenerationResponse,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
ACTION_FORECAST_TOOL_NAME = "forecast_all_actions"
|
|
50
|
+
ACTION_FORECAST_BLOCK_TOOL_NAME = "forecast_action_block"
|
|
51
|
+
ACTION_FORECAST_POLICY_ID = "pydantic_ai_all_option_action_forecast"
|
|
52
|
+
ACTION_FORECAST_V4_POLICY_VERSION = 4
|
|
53
|
+
ACTION_FORECAST_V4_POLICY_DEFINITION_SHA256 = hashlib.sha256(
|
|
54
|
+
b"agent-evolve:pydantic-ai-all-option-action-forecast:v4:"
|
|
55
|
+
b"positional-code-matrices-ordinal-metric-scale-effects-asymmetric-"
|
|
56
|
+
b"uncertainty-derived-confidence-discrete-validity-and-one-atomic-prompt-"
|
|
57
|
+
b"visible-evidence-slot-per-grounded-cell-with-no-visible-numeric-bounds-"
|
|
58
|
+
b"hash-bound-global-or-partition-block-positional-frames-block-local-only-"
|
|
59
|
+
b"emission-and-logarithmic-effect-and-uncertainty-tails-through-32"
|
|
60
|
+
).hexdigest()
|
|
61
|
+
ACTION_FORECAST_POLICY_VERSION = 5
|
|
62
|
+
ACTION_FORECAST_POLICY_DEFINITION_SHA256 = hashlib.sha256(
|
|
63
|
+
b"agent-evolve:pydantic-ai-all-option-action-forecast:v5:"
|
|
64
|
+
b"positional-code-matrices-ordinal-metric-scale-effects-asymmetric-"
|
|
65
|
+
b"adjacent-midpoint-quantization-floors-with-virtual-endpoints-minus-and-"
|
|
66
|
+
b"plus-64-and-prompt-semantics-schema-6-and-excess-epistemic-uncertainty-"
|
|
67
|
+
b"derived-confidence-excluding-quantization-floors-discrete-validity-and-"
|
|
68
|
+
b"one-atomic-prompt-"
|
|
69
|
+
b"visible-evidence-slot-per-grounded-cell-with-no-visible-numeric-bounds-"
|
|
70
|
+
b"hash-bound-global-or-partition-block-positional-frames-block-local-only-"
|
|
71
|
+
b"emission-and-logarithmic-effect-and-uncertainty-tails-through-32"
|
|
72
|
+
).hexdigest()
|
|
73
|
+
|
|
74
|
+
_STRICT_CONFIG = ConfigDict(
|
|
75
|
+
extra="forbid",
|
|
76
|
+
strict=True,
|
|
77
|
+
frozen=True,
|
|
78
|
+
validate_default=True,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
# The v2 live trace demonstrated that a model may copy a provider-visible
|
|
82
|
+
# numeric endpoint into every prediction. V4 removed numerical output fields
|
|
83
|
+
# and artificial floating-point boundaries; v5 preserves that exact enum shape
|
|
84
|
+
# while adding trusted quantization floors after typed admission. Codes are
|
|
85
|
+
# mapped to benchmark-supplied practical metric scales only after admission.
|
|
86
|
+
_EFFECT_MULTIPLIERS: dict[str, float] = {
|
|
87
|
+
"n32": -32.0,
|
|
88
|
+
"n16": -16.0,
|
|
89
|
+
"n8": -8.0,
|
|
90
|
+
"n4": -4.0,
|
|
91
|
+
"n2": -2.0,
|
|
92
|
+
"n1": -1.0,
|
|
93
|
+
"n0_5": -0.5,
|
|
94
|
+
"n0_25": -0.25,
|
|
95
|
+
"z": 0.0,
|
|
96
|
+
"p0_25": 0.25,
|
|
97
|
+
"p0_5": 0.5,
|
|
98
|
+
"p1": 1.0,
|
|
99
|
+
"p2": 2.0,
|
|
100
|
+
"p4": 4.0,
|
|
101
|
+
"p8": 8.0,
|
|
102
|
+
"p16": 16.0,
|
|
103
|
+
"p32": 32.0,
|
|
104
|
+
}
|
|
105
|
+
_UNCERTAINTY_MULTIPLIERS: dict[str, float] = {
|
|
106
|
+
"u0": 0.0,
|
|
107
|
+
"u0_25": 0.25,
|
|
108
|
+
"u0_5": 0.5,
|
|
109
|
+
"u1": 1.0,
|
|
110
|
+
"u2": 2.0,
|
|
111
|
+
"u4": 4.0,
|
|
112
|
+
"u8": 8.0,
|
|
113
|
+
"u16": 16.0,
|
|
114
|
+
"u32": 32.0,
|
|
115
|
+
}
|
|
116
|
+
_VALIDITY_VALUES: dict[str, float] = {
|
|
117
|
+
"p0_05": 0.05,
|
|
118
|
+
"p0_2": 0.2,
|
|
119
|
+
"p0_4": 0.4,
|
|
120
|
+
"p0_6": 0.6,
|
|
121
|
+
"p0_8": 0.8,
|
|
122
|
+
"p0_95": 0.95,
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
_EFFECT_CODES = tuple(_EFFECT_MULTIPLIERS)
|
|
126
|
+
_UNCERTAINTY_CODES = tuple(_UNCERTAINTY_MULTIPLIERS)
|
|
127
|
+
_VALIDITY_CODES = tuple(_VALIDITY_VALUES)
|
|
128
|
+
|
|
129
|
+
_SUPPORTED_PROVIDER_WIRE_VERSIONS = (4, 5)
|
|
130
|
+
_CURRENT_PROVIDER_WIRE_VERSION = 5
|
|
131
|
+
_VIRTUAL_LOWER_EFFECT_CENTER = -64.0
|
|
132
|
+
_VIRTUAL_UPPER_EFFECT_CENTER = 64.0
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _build_effect_quantization_floors() -> dict[str, tuple[float, float]]:
|
|
136
|
+
"""Return asymmetric half-bin widths around every ordinal effect center."""
|
|
137
|
+
|
|
138
|
+
items = tuple(_EFFECT_MULTIPLIERS.items())
|
|
139
|
+
centers = tuple(value for _, value in items)
|
|
140
|
+
if any(
|
|
141
|
+
not math.isfinite(value)
|
|
142
|
+
for value in (
|
|
143
|
+
*centers,
|
|
144
|
+
_VIRTUAL_LOWER_EFFECT_CENTER,
|
|
145
|
+
_VIRTUAL_UPPER_EFFECT_CENTER,
|
|
146
|
+
)
|
|
147
|
+
):
|
|
148
|
+
raise RuntimeError("effect centers and virtual endpoints must be finite")
|
|
149
|
+
extended = (
|
|
150
|
+
_VIRTUAL_LOWER_EFFECT_CENTER,
|
|
151
|
+
*centers,
|
|
152
|
+
_VIRTUAL_UPPER_EFFECT_CENTER,
|
|
153
|
+
)
|
|
154
|
+
if any(
|
|
155
|
+
lower >= upper
|
|
156
|
+
for lower, upper in zip(extended[:-1], extended[1:], strict=True)
|
|
157
|
+
):
|
|
158
|
+
raise RuntimeError("effect centers and virtual endpoints must be ordered")
|
|
159
|
+
return {
|
|
160
|
+
code: (
|
|
161
|
+
(center - extended[index]) / 2.0,
|
|
162
|
+
(extended[index + 2] - center) / 2.0,
|
|
163
|
+
)
|
|
164
|
+
for index, (code, center) in enumerate(items)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
_EFFECT_QUANTIZATION_FLOORS = _build_effect_quantization_floors()
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class _ActionForecastWireBase(BaseModel):
|
|
172
|
+
"""Validator-free base whose complete contract is visible in JSON Schema."""
|
|
173
|
+
|
|
174
|
+
model_config = _STRICT_CONFIG
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _prompt_visible_citation_pairs(
|
|
178
|
+
request: ActionForecastRequest,
|
|
179
|
+
) -> tuple[tuple[str, str], ...]:
|
|
180
|
+
pairs = tuple(
|
|
181
|
+
sorted(
|
|
182
|
+
(
|
|
183
|
+
(card.card_key, binding.identity_sha256)
|
|
184
|
+
for card in request.cards
|
|
185
|
+
for binding in card.finite_action_evidence
|
|
186
|
+
)
|
|
187
|
+
)
|
|
188
|
+
)
|
|
189
|
+
if len(set(pairs)) != len(pairs):
|
|
190
|
+
raise ValueError("prompt-visible card/action citation pairs must be unique")
|
|
191
|
+
return pairs
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _prompt_visible_evidence_slots(
|
|
195
|
+
request: ActionForecastRequest,
|
|
196
|
+
) -> tuple[tuple[str, str, str], ...]:
|
|
197
|
+
"""Assign compact atomic IDs to exact prompt-visible citation pairs."""
|
|
198
|
+
|
|
199
|
+
return tuple(
|
|
200
|
+
(f"e{index}", card_key, binding_sha256)
|
|
201
|
+
for index, (card_key, binding_sha256) in enumerate(
|
|
202
|
+
_prompt_visible_citation_pairs(request)
|
|
203
|
+
)
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _action_forecast_output_type(
|
|
208
|
+
request: ActionForecastRequest,
|
|
209
|
+
*,
|
|
210
|
+
option_count: int | None = None,
|
|
211
|
+
provider_wire_version: int = _CURRENT_PROVIDER_WIRE_VERSION,
|
|
212
|
+
) -> type[BaseModel]:
|
|
213
|
+
request.__post_init__()
|
|
214
|
+
grounded = request.evidence_mode is ActionForecastEvidenceMode.GROUNDED
|
|
215
|
+
evidence_slot_ids = tuple(
|
|
216
|
+
value[0] for value in _prompt_visible_evidence_slots(request)
|
|
217
|
+
)
|
|
218
|
+
return _cached_action_forecast_output_type(
|
|
219
|
+
(
|
|
220
|
+
len(request.finite_variation_contract.options)
|
|
221
|
+
if option_count is None
|
|
222
|
+
else option_count
|
|
223
|
+
),
|
|
224
|
+
len(request.required_metric_ids),
|
|
225
|
+
grounded,
|
|
226
|
+
evidence_slot_ids,
|
|
227
|
+
provider_wire_version,
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
@lru_cache(maxsize=512)
|
|
232
|
+
def _cached_action_forecast_output_type(
|
|
233
|
+
option_count: int,
|
|
234
|
+
metric_count: int,
|
|
235
|
+
grounded: bool,
|
|
236
|
+
evidence_slot_ids: tuple[str, ...],
|
|
237
|
+
provider_wire_version: int,
|
|
238
|
+
) -> type[BaseModel]:
|
|
239
|
+
"""Build a schema whose visible constraints are its complete wire contract."""
|
|
240
|
+
|
|
241
|
+
if type(option_count) is not int or option_count <= 0:
|
|
242
|
+
raise ValueError("option_count must be a positive exact integer")
|
|
243
|
+
if type(metric_count) is not int or metric_count <= 0:
|
|
244
|
+
raise ValueError("metric_count must be a positive exact integer")
|
|
245
|
+
if type(grounded) is not bool:
|
|
246
|
+
raise TypeError("grounded must be an exact bool")
|
|
247
|
+
if grounded and not evidence_slot_ids:
|
|
248
|
+
raise ValueError("grounded forecast schema requires evidence slots")
|
|
249
|
+
if not grounded and evidence_slot_ids:
|
|
250
|
+
raise ValueError("catalog-only forecast schema forbids evidence slots")
|
|
251
|
+
if provider_wire_version not in _SUPPORTED_PROVIDER_WIRE_VERSIONS:
|
|
252
|
+
raise ValueError("provider_wire_version must be 4 or 5")
|
|
253
|
+
|
|
254
|
+
effect_literal = Literal.__getitem__(_EFFECT_CODES)
|
|
255
|
+
uncertainty_literal = Literal.__getitem__(_UNCERTAINTY_CODES)
|
|
256
|
+
validity_literal = Literal.__getitem__(_VALIDITY_CODES)
|
|
257
|
+
|
|
258
|
+
effect_row = Annotated[
|
|
259
|
+
list[effect_literal],
|
|
260
|
+
Field(min_length=metric_count, max_length=metric_count),
|
|
261
|
+
]
|
|
262
|
+
uncertainty_row = Annotated[
|
|
263
|
+
list[uncertainty_literal],
|
|
264
|
+
Field(min_length=metric_count, max_length=metric_count),
|
|
265
|
+
]
|
|
266
|
+
matrix_fields: dict[str, Any] = {
|
|
267
|
+
"probability_valid_codes": (
|
|
268
|
+
list[validity_literal],
|
|
269
|
+
Field(
|
|
270
|
+
min_length=option_count,
|
|
271
|
+
max_length=option_count,
|
|
272
|
+
description=(
|
|
273
|
+
"One ordinal validity-probability code per ordered option."
|
|
274
|
+
),
|
|
275
|
+
),
|
|
276
|
+
),
|
|
277
|
+
"median_effect_codes": (
|
|
278
|
+
list[effect_row],
|
|
279
|
+
Field(
|
|
280
|
+
min_length=option_count,
|
|
281
|
+
max_length=option_count,
|
|
282
|
+
description=(
|
|
283
|
+
"Signed median child-minus-parent effects in metric-scale units; "
|
|
284
|
+
"matrix[i][j] maps to ordered option i and metric j."
|
|
285
|
+
),
|
|
286
|
+
),
|
|
287
|
+
),
|
|
288
|
+
"lower_uncertainty_codes": (
|
|
289
|
+
list[uncertainty_row],
|
|
290
|
+
Field(
|
|
291
|
+
min_length=option_count,
|
|
292
|
+
max_length=option_count,
|
|
293
|
+
description=(
|
|
294
|
+
"Nonnegative excess epistemic p50-to-p10 distance beyond "
|
|
295
|
+
"the effect code's lower quantization floor, in metric-scale "
|
|
296
|
+
"units."
|
|
297
|
+
if provider_wire_version == 5
|
|
298
|
+
else "Nonnegative p50-to-p10 distances in metric-scale units."
|
|
299
|
+
),
|
|
300
|
+
),
|
|
301
|
+
),
|
|
302
|
+
"upper_uncertainty_codes": (
|
|
303
|
+
list[uncertainty_row],
|
|
304
|
+
Field(
|
|
305
|
+
min_length=option_count,
|
|
306
|
+
max_length=option_count,
|
|
307
|
+
description=(
|
|
308
|
+
"Nonnegative excess epistemic p50-to-p90 distance beyond "
|
|
309
|
+
"the effect code's upper quantization floor, in metric-scale "
|
|
310
|
+
"units."
|
|
311
|
+
if provider_wire_version == 5
|
|
312
|
+
else "Nonnegative p50-to-p90 distances in metric-scale units."
|
|
313
|
+
),
|
|
314
|
+
),
|
|
315
|
+
),
|
|
316
|
+
}
|
|
317
|
+
if grounded:
|
|
318
|
+
evidence_slot_literal = Literal.__getitem__(evidence_slot_ids)
|
|
319
|
+
evidence_row = Annotated[
|
|
320
|
+
list[evidence_slot_literal],
|
|
321
|
+
Field(min_length=metric_count, max_length=metric_count),
|
|
322
|
+
]
|
|
323
|
+
matrix_fields["evidence_slot_codes"] = (
|
|
324
|
+
list[evidence_row],
|
|
325
|
+
Field(
|
|
326
|
+
min_length=option_count,
|
|
327
|
+
max_length=option_count,
|
|
328
|
+
description=(
|
|
329
|
+
"One atomic prompt-visible evidence slot per option-metric cell."
|
|
330
|
+
),
|
|
331
|
+
),
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
output_type = create_model(
|
|
335
|
+
f"AllOptionActionForecastMatrixV{provider_wire_version}",
|
|
336
|
+
__base__=_ActionForecastWireBase,
|
|
337
|
+
__module__=__name__,
|
|
338
|
+
**matrix_fields,
|
|
339
|
+
)
|
|
340
|
+
return output_type
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def _render_action_forecast_prompt_frame(
|
|
344
|
+
request: ActionForecastRequest,
|
|
345
|
+
*,
|
|
346
|
+
global_row_start: int,
|
|
347
|
+
global_row_stop: int,
|
|
348
|
+
frame_binding: dict[str, object],
|
|
349
|
+
provider_wire_version: int,
|
|
350
|
+
) -> str:
|
|
351
|
+
"""Render one position-bound full or block forecast frame."""
|
|
352
|
+
|
|
353
|
+
if type(request) is not ActionForecastRequest:
|
|
354
|
+
raise TypeError("request must be an exact ActionForecastRequest")
|
|
355
|
+
request.__post_init__()
|
|
356
|
+
contract = request.finite_variation_contract
|
|
357
|
+
if (
|
|
358
|
+
type(global_row_start) is not int
|
|
359
|
+
or type(global_row_stop) is not int
|
|
360
|
+
or global_row_start < 0
|
|
361
|
+
or global_row_stop <= global_row_start
|
|
362
|
+
or global_row_stop > len(contract.options)
|
|
363
|
+
):
|
|
364
|
+
raise ValueError("forecast frame must be a non-empty in-contract row slice")
|
|
365
|
+
if type(frame_binding) is not dict:
|
|
366
|
+
raise TypeError("frame_binding must be an exact dict")
|
|
367
|
+
if provider_wire_version not in _SUPPORTED_PROVIDER_WIRE_VERSIONS:
|
|
368
|
+
raise ValueError("provider_wire_version must be 4 or 5")
|
|
369
|
+
options = contract.options[global_row_start:global_row_stop]
|
|
370
|
+
evidence_slots = _prompt_visible_evidence_slots(request)
|
|
371
|
+
effect_meanings = {
|
|
372
|
+
"n32": "negative thirty-two times this metric's delta_scale",
|
|
373
|
+
"n16": "negative sixteen times this metric's delta_scale",
|
|
374
|
+
"n8": "negative eight times this metric's delta_scale",
|
|
375
|
+
"n4": "negative four times this metric's delta_scale",
|
|
376
|
+
"n2": "negative two times this metric's delta_scale",
|
|
377
|
+
"n1": "negative one times this metric's delta_scale",
|
|
378
|
+
"n0_5": "negative one half of this metric's delta_scale",
|
|
379
|
+
"n0_25": "negative one quarter of this metric's delta_scale",
|
|
380
|
+
"z": "zero child-minus-parent change",
|
|
381
|
+
"p0_25": "positive one quarter of this metric's delta_scale",
|
|
382
|
+
"p0_5": "positive one half of this metric's delta_scale",
|
|
383
|
+
"p1": "positive one times this metric's delta_scale",
|
|
384
|
+
"p2": "positive two times this metric's delta_scale",
|
|
385
|
+
"p4": "positive four times this metric's delta_scale",
|
|
386
|
+
"p8": "positive eight times this metric's delta_scale",
|
|
387
|
+
"p16": "positive sixteen times this metric's delta_scale",
|
|
388
|
+
"p32": "positive thirty-two times this metric's delta_scale",
|
|
389
|
+
}
|
|
390
|
+
if provider_wire_version == 5:
|
|
391
|
+
uncertainty_meanings = {
|
|
392
|
+
"u0": "zero excess epistemic scale units beyond the quantization floor",
|
|
393
|
+
"u0_25": (
|
|
394
|
+
"one quarter excess epistemic scale unit beyond the quantization floor"
|
|
395
|
+
),
|
|
396
|
+
"u0_5": (
|
|
397
|
+
"one half excess epistemic scale unit beyond the quantization floor"
|
|
398
|
+
),
|
|
399
|
+
"u1": "one excess epistemic scale unit beyond the quantization floor",
|
|
400
|
+
"u2": "two excess epistemic scale units beyond the quantization floor",
|
|
401
|
+
"u4": "four excess epistemic scale units beyond the quantization floor",
|
|
402
|
+
"u8": "eight excess epistemic scale units beyond the quantization floor",
|
|
403
|
+
"u16": (
|
|
404
|
+
"sixteen excess epistemic scale units beyond the quantization floor"
|
|
405
|
+
),
|
|
406
|
+
"u32": (
|
|
407
|
+
"thirty-two excess epistemic scale units beyond the quantization floor"
|
|
408
|
+
),
|
|
409
|
+
}
|
|
410
|
+
else:
|
|
411
|
+
uncertainty_meanings = {
|
|
412
|
+
"u0": "zero additional scale units",
|
|
413
|
+
"u0_25": "one quarter scale unit",
|
|
414
|
+
"u0_5": "one half scale unit",
|
|
415
|
+
"u1": "one scale unit",
|
|
416
|
+
"u2": "two scale units",
|
|
417
|
+
"u4": "four scale units",
|
|
418
|
+
"u8": "eight scale units",
|
|
419
|
+
"u16": "sixteen scale units",
|
|
420
|
+
"u32": "thirty-two scale units",
|
|
421
|
+
}
|
|
422
|
+
validity_meanings = {
|
|
423
|
+
"p0_05": "about five percent probability valid",
|
|
424
|
+
"p0_2": "about twenty percent probability valid",
|
|
425
|
+
"p0_4": "about forty percent probability valid",
|
|
426
|
+
"p0_6": "about sixty percent probability valid",
|
|
427
|
+
"p0_8": "about eighty percent probability valid",
|
|
428
|
+
"p0_95": "about ninety-five percent probability valid",
|
|
429
|
+
}
|
|
430
|
+
machine_contract = {
|
|
431
|
+
"schema_version": 6 if provider_wire_version == 5 else 5,
|
|
432
|
+
"request_sha256": request.request_sha256,
|
|
433
|
+
"context_sha256": request.context_sha256,
|
|
434
|
+
"context": thaw_json(request.context),
|
|
435
|
+
"optimization_semantics": request.optimization_semantics.to_record(),
|
|
436
|
+
"action_semantics": request.action_semantics.to_record(),
|
|
437
|
+
"finite_variation_contract": {
|
|
438
|
+
"catalog_id": contract.catalog_id,
|
|
439
|
+
"catalog_version": contract.catalog_version,
|
|
440
|
+
"catalog_definition_sha256": contract.catalog_definition_sha256,
|
|
441
|
+
"parent_configuration_sha256": contract.parent_configuration_sha256,
|
|
442
|
+
"contract_identity_sha256": contract.identity_sha256,
|
|
443
|
+
},
|
|
444
|
+
"forecast_frame": frame_binding,
|
|
445
|
+
"ordered_options": [
|
|
446
|
+
{
|
|
447
|
+
"row_index": local_index,
|
|
448
|
+
"global_row_index": global_row_start + local_index,
|
|
449
|
+
**option.prompt_record(),
|
|
450
|
+
}
|
|
451
|
+
for local_index, option in enumerate(options)
|
|
452
|
+
],
|
|
453
|
+
"forecast_metrics": [
|
|
454
|
+
{
|
|
455
|
+
"column_index": index,
|
|
456
|
+
"metric_id": parent.metric_id,
|
|
457
|
+
"parent_value": parent.value,
|
|
458
|
+
"delta_scale": scale.delta_scale,
|
|
459
|
+
"scale_definition_sha256": scale.definition_sha256,
|
|
460
|
+
}
|
|
461
|
+
for index, (parent, scale) in enumerate(
|
|
462
|
+
zip(
|
|
463
|
+
request.parent_metric_values,
|
|
464
|
+
request.metric_scales,
|
|
465
|
+
strict=True,
|
|
466
|
+
)
|
|
467
|
+
)
|
|
468
|
+
],
|
|
469
|
+
"evidence_mode": request.evidence_mode.value,
|
|
470
|
+
"cards": [card.prompt_record() for card in request.cards],
|
|
471
|
+
"evidence_slots": [
|
|
472
|
+
{
|
|
473
|
+
"evidence_slot_id": slot_id,
|
|
474
|
+
"card_key": card_key,
|
|
475
|
+
"action_binding_identity_sha256": binding_sha256,
|
|
476
|
+
}
|
|
477
|
+
for slot_id, card_key, binding_sha256 in evidence_slots
|
|
478
|
+
],
|
|
479
|
+
"ordinal_codebook": {
|
|
480
|
+
"median_effect_codes": effect_meanings,
|
|
481
|
+
"uncertainty_codes": uncertainty_meanings,
|
|
482
|
+
"probability_valid_codes": validity_meanings,
|
|
483
|
+
"derived_confidence": (
|
|
484
|
+
"trusted code derives confidence monotonically from only the total "
|
|
485
|
+
"lower-plus-upper excess epistemic uncertainty, excluding fixed "
|
|
486
|
+
"quantization floors; do not emit a confidence field"
|
|
487
|
+
if provider_wire_version == 5
|
|
488
|
+
else "trusted code derives confidence monotonically from the total "
|
|
489
|
+
"lower-plus-upper uncertainty; do not emit a confidence field"
|
|
490
|
+
),
|
|
491
|
+
},
|
|
492
|
+
"output_contract": {
|
|
493
|
+
"action_row_count": len(options),
|
|
494
|
+
"action_row_binding": (
|
|
495
|
+
"every top-level vector or matrix row i maps to ordered_options[i]"
|
|
496
|
+
),
|
|
497
|
+
"metric_cell_count_per_row": len(request.required_metric_ids),
|
|
498
|
+
"metric_cell_binding": (
|
|
499
|
+
"every matrix cell [i][j] maps to forecast_metrics[j]"
|
|
500
|
+
),
|
|
501
|
+
"delta_definition": "child_metric_minus_parent_metric",
|
|
502
|
+
"quantile_derivation": {
|
|
503
|
+
"p10_delta": (
|
|
504
|
+
"(median effect units - lower quantization-floor units - lower "
|
|
505
|
+
"excess epistemic uncertainty units) times delta_scale"
|
|
506
|
+
if provider_wire_version == 5
|
|
507
|
+
else "(median effect units - lower uncertainty units) times "
|
|
508
|
+
"delta_scale"
|
|
509
|
+
),
|
|
510
|
+
"p50_delta": "median effect units times delta_scale",
|
|
511
|
+
"p90_delta": (
|
|
512
|
+
"(median effect units + upper quantization-floor units + upper "
|
|
513
|
+
"excess epistemic uncertainty units) times delta_scale"
|
|
514
|
+
if provider_wire_version == 5
|
|
515
|
+
else "(median effect units + upper uncertainty units) times "
|
|
516
|
+
"delta_scale"
|
|
517
|
+
),
|
|
518
|
+
},
|
|
519
|
+
"provider_numeric_output_fields": [],
|
|
520
|
+
"one_evidence_slot_required_per_metric": (
|
|
521
|
+
request.evidence_mode is ActionForecastEvidenceMode.GROUNDED
|
|
522
|
+
),
|
|
523
|
+
},
|
|
524
|
+
}
|
|
525
|
+
if provider_wire_version == 5:
|
|
526
|
+
machine_contract["effect_quantization"] = {
|
|
527
|
+
"rule": "adjacent_midpoints_with_virtual_endpoint_centers",
|
|
528
|
+
"virtual_lower_effect_center_units": _VIRTUAL_LOWER_EFFECT_CENTER,
|
|
529
|
+
"virtual_upper_effect_center_units": _VIRTUAL_UPPER_EFFECT_CENTER,
|
|
530
|
+
"asymmetric_floor_units_by_effect_code": {
|
|
531
|
+
code: {
|
|
532
|
+
"lower_floor_units": floors[0],
|
|
533
|
+
"upper_floor_units": floors[1],
|
|
534
|
+
}
|
|
535
|
+
for code, floors in _EFFECT_QUANTIZATION_FLOORS.items()
|
|
536
|
+
},
|
|
537
|
+
}
|
|
538
|
+
encoded = json.dumps(
|
|
539
|
+
machine_contract,
|
|
540
|
+
allow_nan=False,
|
|
541
|
+
ensure_ascii=True,
|
|
542
|
+
separators=(",", ":"),
|
|
543
|
+
sort_keys=True,
|
|
544
|
+
)
|
|
545
|
+
citation_instruction = (
|
|
546
|
+
"For every metric cell choose exactly one supplied evidence slot in "
|
|
547
|
+
"evidence_slot_codes[i][j] as its primary attributable card/action source."
|
|
548
|
+
if request.evidence_mode is ActionForecastEvidenceMode.GROUNDED
|
|
549
|
+
else "Do not emit evidence_slot_codes; no evidence cards are supplied."
|
|
550
|
+
)
|
|
551
|
+
frame_instruction = (
|
|
552
|
+
"This is a partition block. Forecast exactly and only the "
|
|
553
|
+
"local_row_count ordered_options supplied in this frame; do not emit "
|
|
554
|
+
"rows for absent global options."
|
|
555
|
+
if frame_binding.get("frame_kind") == "partition_block"
|
|
556
|
+
else "This is the complete forecast frame."
|
|
557
|
+
)
|
|
558
|
+
uncertainty_instructions: tuple[str, ...] = ()
|
|
559
|
+
if provider_wire_version == 5:
|
|
560
|
+
uncertainty_instructions = (
|
|
561
|
+
"Lower and upper uncertainty codes encode only excess epistemic "
|
|
562
|
+
"distance beyond the selected median effect code's trusted asymmetric "
|
|
563
|
+
"midpoint quantization floors; trusted code adds those floors when "
|
|
564
|
+
"deriving p10 and p90, while confidence uses only the emitted excess "
|
|
565
|
+
"uncertainty.",
|
|
566
|
+
)
|
|
567
|
+
return "\n".join(
|
|
568
|
+
(
|
|
569
|
+
request.instruction,
|
|
570
|
+
"",
|
|
571
|
+
"ALL-OPTION ACTION FORECAST CONTRACT",
|
|
572
|
+
encoded,
|
|
573
|
+
"Return exactly one probability_valid_codes entry and one row in every "
|
|
574
|
+
"required code matrix for each ordered_options entry, with exactly one "
|
|
575
|
+
"code per forecast_metrics entry in each matrix row. "
|
|
576
|
+
"Do not emit option IDs, metric IDs, or candidate configurations; "
|
|
577
|
+
"trusted code reattaches those identities by position. Use only the "
|
|
578
|
+
"closed ordinal codes. Do not emit any numeric forecast or confidence "
|
|
579
|
+
"field. Negative/positive effect codes always mean signed raw "
|
|
580
|
+
"child-minus-parent change, independent of whether a metric is "
|
|
581
|
+
"minimized, maximized, or constrained.",
|
|
582
|
+
*uncertainty_instructions,
|
|
583
|
+
frame_instruction,
|
|
584
|
+
citation_instruction,
|
|
585
|
+
)
|
|
586
|
+
)
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
def render_action_forecast_prompt(request: ActionForecastRequest) -> str:
|
|
590
|
+
"""Render the current v5 prompt-safe all-option forecast contract."""
|
|
591
|
+
|
|
592
|
+
return _render_complete_action_forecast_prompt(
|
|
593
|
+
request,
|
|
594
|
+
provider_wire_version=_CURRENT_PROVIDER_WIRE_VERSION,
|
|
595
|
+
)
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
def render_action_forecast_v4_prompt(request: ActionForecastRequest) -> str:
|
|
599
|
+
"""Render the sealed v4 contract for historical replay compatibility."""
|
|
600
|
+
|
|
601
|
+
return _render_complete_action_forecast_prompt(request, provider_wire_version=4)
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
def _render_complete_action_forecast_prompt(
|
|
605
|
+
request: ActionForecastRequest,
|
|
606
|
+
*,
|
|
607
|
+
provider_wire_version: int,
|
|
608
|
+
) -> str:
|
|
609
|
+
"""Render one explicitly versioned complete forecast contract."""
|
|
610
|
+
|
|
611
|
+
if type(request) is not ActionForecastRequest:
|
|
612
|
+
raise TypeError("request must be an exact ActionForecastRequest")
|
|
613
|
+
request.__post_init__()
|
|
614
|
+
option_count = len(request.finite_variation_contract.options)
|
|
615
|
+
return _render_action_forecast_prompt_frame(
|
|
616
|
+
request,
|
|
617
|
+
global_row_start=0,
|
|
618
|
+
global_row_stop=option_count,
|
|
619
|
+
frame_binding={
|
|
620
|
+
"frame_kind": "complete",
|
|
621
|
+
"global_option_count": option_count,
|
|
622
|
+
"global_row_start": 0,
|
|
623
|
+
"global_row_stop": option_count,
|
|
624
|
+
"local_row_count": option_count,
|
|
625
|
+
},
|
|
626
|
+
provider_wire_version=provider_wire_version,
|
|
627
|
+
)
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
def render_action_forecast_block_prompt(
|
|
631
|
+
block_request: ActionForecastBlockRequest,
|
|
632
|
+
) -> str:
|
|
633
|
+
"""Render one current v5 block without pretending it is a complete batch."""
|
|
634
|
+
|
|
635
|
+
return _render_action_forecast_block_prompt(
|
|
636
|
+
block_request,
|
|
637
|
+
provider_wire_version=_CURRENT_PROVIDER_WIRE_VERSION,
|
|
638
|
+
)
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
def render_action_forecast_v4_block_prompt(
|
|
642
|
+
block_request: ActionForecastBlockRequest,
|
|
643
|
+
) -> str:
|
|
644
|
+
"""Render one sealed v4 block for historical replay compatibility."""
|
|
645
|
+
|
|
646
|
+
return _render_action_forecast_block_prompt(block_request, provider_wire_version=4)
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
def _render_action_forecast_block_prompt(
|
|
650
|
+
block_request: ActionForecastBlockRequest,
|
|
651
|
+
*,
|
|
652
|
+
provider_wire_version: int,
|
|
653
|
+
) -> str:
|
|
654
|
+
"""Render one explicitly versioned immutable block forecast contract."""
|
|
655
|
+
|
|
656
|
+
if type(block_request) is not ActionForecastBlockRequest:
|
|
657
|
+
raise TypeError("block_request must be exact ActionForecastBlockRequest")
|
|
658
|
+
block_request.__post_init__()
|
|
659
|
+
block = block_request.block
|
|
660
|
+
return _render_action_forecast_prompt_frame(
|
|
661
|
+
block_request.request,
|
|
662
|
+
global_row_start=block.global_row_start,
|
|
663
|
+
global_row_stop=block.global_row_stop,
|
|
664
|
+
frame_binding={
|
|
665
|
+
"frame_kind": "partition_block",
|
|
666
|
+
"global_option_count": block_request.layout.row_count,
|
|
667
|
+
"global_row_start": block.global_row_start,
|
|
668
|
+
"global_row_stop": block.global_row_stop,
|
|
669
|
+
"local_row_count": block.row_count,
|
|
670
|
+
"layout_sha256": block_request.layout.layout_sha256,
|
|
671
|
+
"block_index": block.block_index,
|
|
672
|
+
"block_spec_sha256": block.block_spec_sha256,
|
|
673
|
+
"block_request_sha256": block_request.block_request_sha256,
|
|
674
|
+
},
|
|
675
|
+
provider_wire_version=provider_wire_version,
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
def plan_action_forecast_request(
|
|
680
|
+
request: ActionForecastRequest,
|
|
681
|
+
) -> StructuredGenerationRequest[BaseModel]:
|
|
682
|
+
"""Purely plan the current v5 request without dispatching it."""
|
|
683
|
+
|
|
684
|
+
return _plan_action_forecast_request(
|
|
685
|
+
request,
|
|
686
|
+
provider_wire_version=_CURRENT_PROVIDER_WIRE_VERSION,
|
|
687
|
+
)
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
def plan_action_forecast_v4_request(
|
|
691
|
+
request: ActionForecastRequest,
|
|
692
|
+
) -> StructuredGenerationRequest[BaseModel]:
|
|
693
|
+
"""Plan the sealed v4 request for historical replay compatibility."""
|
|
694
|
+
|
|
695
|
+
return _plan_action_forecast_request(request, provider_wire_version=4)
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
def _plan_action_forecast_request(
|
|
699
|
+
request: ActionForecastRequest,
|
|
700
|
+
*,
|
|
701
|
+
provider_wire_version: int,
|
|
702
|
+
) -> StructuredGenerationRequest[BaseModel]:
|
|
703
|
+
"""Purely plan one explicitly versioned request without dispatching it."""
|
|
704
|
+
|
|
705
|
+
if type(request) is not ActionForecastRequest:
|
|
706
|
+
raise TypeError("request must be an exact ActionForecastRequest")
|
|
707
|
+
request.__post_init__()
|
|
708
|
+
return StructuredGenerationRequest(
|
|
709
|
+
call_id=request.call_id,
|
|
710
|
+
operation=request.operation,
|
|
711
|
+
prompt=(
|
|
712
|
+
render_action_forecast_prompt(request)
|
|
713
|
+
if provider_wire_version == _CURRENT_PROVIDER_WIRE_VERSION
|
|
714
|
+
else render_action_forecast_v4_prompt(request)
|
|
715
|
+
),
|
|
716
|
+
output_type=_action_forecast_output_type(
|
|
717
|
+
request,
|
|
718
|
+
provider_wire_version=provider_wire_version,
|
|
719
|
+
),
|
|
720
|
+
output_tool_name=ACTION_FORECAST_TOOL_NAME,
|
|
721
|
+
max_output_tokens=request.max_output_tokens,
|
|
722
|
+
temperature=request.temperature,
|
|
723
|
+
)
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
def plan_action_forecast_block_request(
|
|
727
|
+
block_request: ActionForecastBlockRequest,
|
|
728
|
+
) -> StructuredGenerationRequest[BaseModel]:
|
|
729
|
+
"""Plan one current v5 block retaining the global scientific binding."""
|
|
730
|
+
|
|
731
|
+
return _plan_action_forecast_block_request(
|
|
732
|
+
block_request,
|
|
733
|
+
provider_wire_version=_CURRENT_PROVIDER_WIRE_VERSION,
|
|
734
|
+
)
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
def plan_action_forecast_v4_block_request(
|
|
738
|
+
block_request: ActionForecastBlockRequest,
|
|
739
|
+
) -> StructuredGenerationRequest[BaseModel]:
|
|
740
|
+
"""Plan one sealed v4 block for historical replay compatibility."""
|
|
741
|
+
|
|
742
|
+
return _plan_action_forecast_block_request(block_request, provider_wire_version=4)
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
def _plan_action_forecast_block_request(
|
|
746
|
+
block_request: ActionForecastBlockRequest,
|
|
747
|
+
*,
|
|
748
|
+
provider_wire_version: int,
|
|
749
|
+
) -> StructuredGenerationRequest[BaseModel]:
|
|
750
|
+
"""Plan one explicitly versioned physical block request."""
|
|
751
|
+
|
|
752
|
+
if type(block_request) is not ActionForecastBlockRequest:
|
|
753
|
+
raise TypeError("block_request must be exact ActionForecastBlockRequest")
|
|
754
|
+
block_request.__post_init__()
|
|
755
|
+
request = block_request.request
|
|
756
|
+
return StructuredGenerationRequest(
|
|
757
|
+
call_id=block_request.block_call_id,
|
|
758
|
+
operation=request.operation,
|
|
759
|
+
prompt=(
|
|
760
|
+
render_action_forecast_block_prompt(block_request)
|
|
761
|
+
if provider_wire_version == _CURRENT_PROVIDER_WIRE_VERSION
|
|
762
|
+
else render_action_forecast_v4_block_prompt(block_request)
|
|
763
|
+
),
|
|
764
|
+
output_type=_action_forecast_output_type(
|
|
765
|
+
request,
|
|
766
|
+
option_count=block_request.block.row_count,
|
|
767
|
+
provider_wire_version=provider_wire_version,
|
|
768
|
+
),
|
|
769
|
+
output_tool_name=ACTION_FORECAST_BLOCK_TOOL_NAME,
|
|
770
|
+
max_output_tokens=request.max_output_tokens,
|
|
771
|
+
temperature=request.temperature,
|
|
772
|
+
)
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
def _validated_response(
|
|
776
|
+
result: object,
|
|
777
|
+
*,
|
|
778
|
+
output_type: type[BaseModel],
|
|
779
|
+
) -> tuple[StructuredGenerationResponse[Any], int]:
|
|
780
|
+
if type(result) is AttemptedStructuredGenerationResponse:
|
|
781
|
+
AttemptedStructuredGenerationResponse.__post_init__(result)
|
|
782
|
+
response = result.response
|
|
783
|
+
attempt_count = result.attempt_count
|
|
784
|
+
elif type(result) is StructuredGenerationResponse:
|
|
785
|
+
response = result
|
|
786
|
+
attempt_count = 1
|
|
787
|
+
else:
|
|
788
|
+
raise TypeError(
|
|
789
|
+
"low-level runner must return StructuredGenerationResponse or "
|
|
790
|
+
"AttemptedStructuredGenerationResponse"
|
|
791
|
+
)
|
|
792
|
+
StructuredGenerationResponse.__post_init__(response)
|
|
793
|
+
if type(response.value) is not output_type:
|
|
794
|
+
raise TypeError(
|
|
795
|
+
"low-level response value does not match the action forecast output type"
|
|
796
|
+
)
|
|
797
|
+
return response, attempt_count
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
def _telemetry(
|
|
801
|
+
response: StructuredGenerationResponse[Any],
|
|
802
|
+
*,
|
|
803
|
+
attempt_count: int,
|
|
804
|
+
) -> AgenticCallTelemetry:
|
|
805
|
+
return AgenticCallTelemetry(
|
|
806
|
+
requested_model=response.requested_model,
|
|
807
|
+
resolved_model=response.resolved_model,
|
|
808
|
+
resolved_provider=response.resolved_provider,
|
|
809
|
+
provider_response_id=response.provider_response_id,
|
|
810
|
+
finish_reason=response.finish_reason,
|
|
811
|
+
input_tokens=response.input_tokens,
|
|
812
|
+
output_tokens=response.output_tokens,
|
|
813
|
+
reasoning_tokens=response.reasoning_tokens,
|
|
814
|
+
cache_read_tokens=response.cache_read_tokens,
|
|
815
|
+
cache_write_tokens=response.cache_write_tokens,
|
|
816
|
+
cost_usd=response.cost_usd,
|
|
817
|
+
latency_ns=response.latency_ns,
|
|
818
|
+
attempt_count=attempt_count,
|
|
819
|
+
)
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
def _drafts_from_wire(
|
|
823
|
+
request: ActionForecastRequest,
|
|
824
|
+
value: Any,
|
|
825
|
+
options: tuple[FiniteVariationOption, ...],
|
|
826
|
+
*,
|
|
827
|
+
provider_wire_version: int,
|
|
828
|
+
) -> tuple[ActionForecastDraft, ...]:
|
|
829
|
+
"""Decode one admitted positional frame into provider-neutral drafts."""
|
|
830
|
+
|
|
831
|
+
if type(options) is not tuple or not options or any(
|
|
832
|
+
type(option) is not FiniteVariationOption for option in options
|
|
833
|
+
):
|
|
834
|
+
raise ValueError("options must be a non-empty exact finite-option tuple")
|
|
835
|
+
if provider_wire_version not in _SUPPORTED_PROVIDER_WIRE_VERSIONS:
|
|
836
|
+
raise ValueError("provider_wire_version must be 4 or 5")
|
|
837
|
+
citation_by_slot_id = {
|
|
838
|
+
slot_id: ActionEvidenceCitation(
|
|
839
|
+
card_key=card_key,
|
|
840
|
+
action_binding_identity_sha256=binding_sha256,
|
|
841
|
+
)
|
|
842
|
+
for slot_id, card_key, binding_sha256 in (
|
|
843
|
+
_prompt_visible_evidence_slots(request)
|
|
844
|
+
)
|
|
845
|
+
}
|
|
846
|
+
drafts_list: list[ActionForecastDraft] = []
|
|
847
|
+
for row_index, option in enumerate(options):
|
|
848
|
+
metric_forecasts: list[ActionMetricForecast] = []
|
|
849
|
+
for metric_index, (metric_id, scale) in enumerate(
|
|
850
|
+
zip(
|
|
851
|
+
request.required_metric_ids,
|
|
852
|
+
request.metric_scales,
|
|
853
|
+
strict=True,
|
|
854
|
+
)
|
|
855
|
+
):
|
|
856
|
+
effect_code = cast(
|
|
857
|
+
str,
|
|
858
|
+
value.median_effect_codes[row_index][metric_index],
|
|
859
|
+
)
|
|
860
|
+
median_units = _EFFECT_MULTIPLIERS[effect_code]
|
|
861
|
+
lower_excess_units = _UNCERTAINTY_MULTIPLIERS[
|
|
862
|
+
cast(
|
|
863
|
+
str,
|
|
864
|
+
value.lower_uncertainty_codes[row_index][metric_index],
|
|
865
|
+
)
|
|
866
|
+
]
|
|
867
|
+
upper_excess_units = _UNCERTAINTY_MULTIPLIERS[
|
|
868
|
+
cast(
|
|
869
|
+
str,
|
|
870
|
+
value.upper_uncertainty_codes[row_index][metric_index],
|
|
871
|
+
)
|
|
872
|
+
]
|
|
873
|
+
lower_floor_units, upper_floor_units = (
|
|
874
|
+
_EFFECT_QUANTIZATION_FLOORS[effect_code]
|
|
875
|
+
if provider_wire_version == 5
|
|
876
|
+
else (0.0, 0.0)
|
|
877
|
+
)
|
|
878
|
+
lower_units = lower_floor_units + lower_excess_units
|
|
879
|
+
upper_units = upper_floor_units + upper_excess_units
|
|
880
|
+
median = median_units * scale.delta_scale
|
|
881
|
+
lower = lower_units * scale.delta_scale
|
|
882
|
+
upper = upper_units * scale.delta_scale
|
|
883
|
+
if not all(math.isfinite(item) for item in (median, lower, upper)):
|
|
884
|
+
raise ValueError(
|
|
885
|
+
"metric-scale code denormalization produced a non-finite delta"
|
|
886
|
+
)
|
|
887
|
+
confidence = 1.0 / (
|
|
888
|
+
1.0 + lower_excess_units + upper_excess_units
|
|
889
|
+
)
|
|
890
|
+
citations = ()
|
|
891
|
+
if request.evidence_mode is ActionForecastEvidenceMode.GROUNDED:
|
|
892
|
+
slot_id = cast(
|
|
893
|
+
str,
|
|
894
|
+
value.evidence_slot_codes[row_index][metric_index],
|
|
895
|
+
)
|
|
896
|
+
citations = (citation_by_slot_id[slot_id],)
|
|
897
|
+
metric_forecasts.append(
|
|
898
|
+
ActionMetricForecast(
|
|
899
|
+
metric_id=metric_id,
|
|
900
|
+
p10_delta=median - lower,
|
|
901
|
+
p50_delta=median,
|
|
902
|
+
p90_delta=median + upper,
|
|
903
|
+
confidence=confidence,
|
|
904
|
+
citations=citations,
|
|
905
|
+
)
|
|
906
|
+
)
|
|
907
|
+
drafts_list.append(
|
|
908
|
+
ActionForecastDraft(
|
|
909
|
+
option_id=option.option_id,
|
|
910
|
+
probability_valid=_VALIDITY_VALUES[
|
|
911
|
+
cast(str, value.probability_valid_codes[row_index])
|
|
912
|
+
],
|
|
913
|
+
metric_forecasts=tuple(metric_forecasts),
|
|
914
|
+
)
|
|
915
|
+
)
|
|
916
|
+
return tuple(drafts_list)
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
@dataclass(slots=True)
|
|
920
|
+
class PydanticAIActionForecastPolicy:
|
|
921
|
+
"""Translate one queued structured call into a trusted all-option batch."""
|
|
922
|
+
|
|
923
|
+
generate_once: LowLevelRunner
|
|
924
|
+
|
|
925
|
+
policy_id: ClassVar[str] = ACTION_FORECAST_POLICY_ID
|
|
926
|
+
policy_version: ClassVar[int] = ACTION_FORECAST_POLICY_VERSION
|
|
927
|
+
policy_definition_sha256: ClassVar[str] = (
|
|
928
|
+
ACTION_FORECAST_POLICY_DEFINITION_SHA256
|
|
929
|
+
)
|
|
930
|
+
provider_wire_version: ClassVar[int] = _CURRENT_PROVIDER_WIRE_VERSION
|
|
931
|
+
|
|
932
|
+
def __post_init__(self) -> None:
|
|
933
|
+
if not callable(self.generate_once):
|
|
934
|
+
raise TypeError("generate_once must be callable")
|
|
935
|
+
|
|
936
|
+
async def forecast(
|
|
937
|
+
self,
|
|
938
|
+
request: ActionForecastRequest,
|
|
939
|
+
) -> ActionForecastResult:
|
|
940
|
+
if type(request) is not ActionForecastRequest:
|
|
941
|
+
raise TypeError("request must be an exact ActionForecastRequest")
|
|
942
|
+
request.__post_init__()
|
|
943
|
+
low_level_request = _plan_action_forecast_request(
|
|
944
|
+
request,
|
|
945
|
+
provider_wire_version=self.provider_wire_version,
|
|
946
|
+
)
|
|
947
|
+
output_type = low_level_request.output_type
|
|
948
|
+
raw = await self.generate_once(low_level_request)
|
|
949
|
+
response, attempt_count = _validated_response(
|
|
950
|
+
raw,
|
|
951
|
+
output_type=output_type,
|
|
952
|
+
)
|
|
953
|
+
value = cast(Any, response.value)
|
|
954
|
+
drafts = _drafts_from_wire(
|
|
955
|
+
request,
|
|
956
|
+
value,
|
|
957
|
+
request.finite_variation_contract.options,
|
|
958
|
+
provider_wire_version=self.provider_wire_version,
|
|
959
|
+
)
|
|
960
|
+
forecasts = resolve_action_forecasts(
|
|
961
|
+
request,
|
|
962
|
+
drafts,
|
|
963
|
+
policy_id=self.policy_id,
|
|
964
|
+
policy_version=self.policy_version,
|
|
965
|
+
policy_definition_sha256=self.policy_definition_sha256,
|
|
966
|
+
)
|
|
967
|
+
return ActionForecastResult(
|
|
968
|
+
forecasts=forecasts,
|
|
969
|
+
telemetry=_telemetry(response, attempt_count=attempt_count),
|
|
970
|
+
)
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
@dataclass(slots=True)
|
|
974
|
+
class PydanticAIActionForecastBlockPolicy:
|
|
975
|
+
"""Translate one queued physical block into a trusted partial receipt."""
|
|
976
|
+
|
|
977
|
+
generate_once: LowLevelRunner
|
|
978
|
+
|
|
979
|
+
policy_id: ClassVar[str] = ACTION_FORECAST_POLICY_ID
|
|
980
|
+
policy_version: ClassVar[int] = ACTION_FORECAST_POLICY_VERSION
|
|
981
|
+
policy_definition_sha256: ClassVar[str] = (
|
|
982
|
+
ACTION_FORECAST_POLICY_DEFINITION_SHA256
|
|
983
|
+
)
|
|
984
|
+
provider_wire_version: ClassVar[int] = _CURRENT_PROVIDER_WIRE_VERSION
|
|
985
|
+
|
|
986
|
+
def __post_init__(self) -> None:
|
|
987
|
+
if not callable(self.generate_once):
|
|
988
|
+
raise TypeError("generate_once must be callable")
|
|
989
|
+
|
|
990
|
+
async def forecast_block(
|
|
991
|
+
self,
|
|
992
|
+
block_request: ActionForecastBlockRequest,
|
|
993
|
+
) -> ActionForecastBlockResult:
|
|
994
|
+
if type(block_request) is not ActionForecastBlockRequest:
|
|
995
|
+
raise TypeError("block_request must be exact ActionForecastBlockRequest")
|
|
996
|
+
block_request.__post_init__()
|
|
997
|
+
low_level_request = _plan_action_forecast_block_request(
|
|
998
|
+
block_request,
|
|
999
|
+
provider_wire_version=self.provider_wire_version,
|
|
1000
|
+
)
|
|
1001
|
+
output_type = low_level_request.output_type
|
|
1002
|
+
raw = await self.generate_once(low_level_request)
|
|
1003
|
+
response, attempt_count = _validated_response(raw, output_type=output_type)
|
|
1004
|
+
value = cast(Any, response.value)
|
|
1005
|
+
spec = block_request.block
|
|
1006
|
+
options = block_request.request.finite_variation_contract.options[
|
|
1007
|
+
spec.global_row_start : spec.global_row_stop
|
|
1008
|
+
]
|
|
1009
|
+
drafts = _drafts_from_wire(
|
|
1010
|
+
block_request.request,
|
|
1011
|
+
value,
|
|
1012
|
+
options,
|
|
1013
|
+
provider_wire_version=self.provider_wire_version,
|
|
1014
|
+
)
|
|
1015
|
+
forecasts = resolve_action_forecast_block(
|
|
1016
|
+
block_request,
|
|
1017
|
+
drafts,
|
|
1018
|
+
policy_id=self.policy_id,
|
|
1019
|
+
policy_version=self.policy_version,
|
|
1020
|
+
policy_definition_sha256=self.policy_definition_sha256,
|
|
1021
|
+
)
|
|
1022
|
+
return ActionForecastBlockResult(
|
|
1023
|
+
forecasts=forecasts,
|
|
1024
|
+
telemetry=_telemetry(response, attempt_count=attempt_count),
|
|
1025
|
+
)
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
class PydanticAIActionForecastV4Policy(PydanticAIActionForecastPolicy):
|
|
1029
|
+
"""Decode and resolve the sealed v4 wire for historical replay only."""
|
|
1030
|
+
|
|
1031
|
+
policy_version: ClassVar[int] = ACTION_FORECAST_V4_POLICY_VERSION
|
|
1032
|
+
policy_definition_sha256: ClassVar[str] = (
|
|
1033
|
+
ACTION_FORECAST_V4_POLICY_DEFINITION_SHA256
|
|
1034
|
+
)
|
|
1035
|
+
provider_wire_version: ClassVar[int] = 4
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
class PydanticAIActionForecastV4BlockPolicy(PydanticAIActionForecastBlockPolicy):
|
|
1039
|
+
"""Decode and resolve sealed v4 partition blocks for historical replay."""
|
|
1040
|
+
|
|
1041
|
+
policy_version: ClassVar[int] = ACTION_FORECAST_V4_POLICY_VERSION
|
|
1042
|
+
policy_definition_sha256: ClassVar[str] = (
|
|
1043
|
+
ACTION_FORECAST_V4_POLICY_DEFINITION_SHA256
|
|
1044
|
+
)
|
|
1045
|
+
provider_wire_version: ClassVar[int] = 4
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
__all__ = [
|
|
1049
|
+
"ACTION_FORECAST_BLOCK_TOOL_NAME",
|
|
1050
|
+
"ACTION_FORECAST_POLICY_DEFINITION_SHA256",
|
|
1051
|
+
"ACTION_FORECAST_POLICY_ID",
|
|
1052
|
+
"ACTION_FORECAST_POLICY_VERSION",
|
|
1053
|
+
"ACTION_FORECAST_TOOL_NAME",
|
|
1054
|
+
"ACTION_FORECAST_V4_POLICY_DEFINITION_SHA256",
|
|
1055
|
+
"ACTION_FORECAST_V4_POLICY_VERSION",
|
|
1056
|
+
"PydanticAIActionForecastBlockPolicy",
|
|
1057
|
+
"PydanticAIActionForecastPolicy",
|
|
1058
|
+
"PydanticAIActionForecastV4BlockPolicy",
|
|
1059
|
+
"PydanticAIActionForecastV4Policy",
|
|
1060
|
+
"plan_action_forecast_block_request",
|
|
1061
|
+
"plan_action_forecast_request",
|
|
1062
|
+
"plan_action_forecast_v4_block_request",
|
|
1063
|
+
"plan_action_forecast_v4_request",
|
|
1064
|
+
"render_action_forecast_block_prompt",
|
|
1065
|
+
"render_action_forecast_prompt",
|
|
1066
|
+
"render_action_forecast_v4_block_prompt",
|
|
1067
|
+
"render_action_forecast_v4_prompt",
|
|
1068
|
+
]
|