agentinputlab 0.1.0a1__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.
- agentinputlab/__init__.py +28 -0
- agentinputlab/algorithms/__init__.py +6 -0
- agentinputlab/algorithms/acquire/__init__.py +145 -0
- agentinputlab/algorithms/acquire/adaptive_rag/__init__.py +43 -0
- agentinputlab/algorithms/acquire/adaptive_rag/replay.py +367 -0
- agentinputlab/algorithms/acquire/adaptive_rag/runtime.py +1202 -0
- agentinputlab/algorithms/acquire/adaptive_rag_common_support.py +542 -0
- agentinputlab/algorithms/acquire/hyde.py +296 -0
- agentinputlab/algorithms/acquire/hyde_controlled.py +200 -0
- agentinputlab/algorithms/acquire/hyde_external.py +148 -0
- agentinputlab/algorithms/acquire/ircot.py +28 -0
- agentinputlab/algorithms/acquire/ircot_common_support.py +478 -0
- agentinputlab/algorithms/acquire/ircot_native.py +355 -0
- agentinputlab/algorithms/acquire/ircot_replay.py +327 -0
- agentinputlab/algorithms/acquire/qrag/__init__.py +79 -0
- agentinputlab/algorithms/acquire/qrag/bridge.py +828 -0
- agentinputlab/algorithms/acquire/qrag/common_support.py +891 -0
- agentinputlab/algorithms/acquire/qrag/native_axis.py +71 -0
- agentinputlab/algorithms/acquire/qrag/reader_adapter.py +139 -0
- agentinputlab/algorithms/acquire/r2ag/__init__.py +57 -0
- agentinputlab/algorithms/acquire/r2ag/bridge.py +1312 -0
- agentinputlab/algorithms/acquire/r2ag/common_support.py +1029 -0
- agentinputlab/algorithms/acquire/r2ag/socket_runtime.py +255 -0
- agentinputlab/algorithms/clarify/__init__.py +61 -0
- agentinputlab/algorithms/clarify/_structured.py +64 -0
- agentinputlab/algorithms/clarify/active_task_disambiguation.py +371 -0
- agentinputlab/algorithms/clarify/ask_or_assume/__init__.py +17 -0
- agentinputlab/algorithms/clarify/ask_or_assume/monitor.py +410 -0
- agentinputlab/algorithms/clarify/future_turns.py +379 -0
- agentinputlab/algorithms/clarify/udcs.py +255 -0
- agentinputlab/algorithms/clarify/udcs_deterministic.py +196 -0
- agentinputlab/algorithms/curate/__init__.py +65 -0
- agentinputlab/algorithms/curate/acon/__init__.py +27 -0
- agentinputlab/algorithms/curate/acon/common.py +284 -0
- agentinputlab/algorithms/curate/acon/common_support.py +136 -0
- agentinputlab/algorithms/curate/acon/history_bridge.py +309 -0
- agentinputlab/algorithms/curate/acon/observation_bridge.py +290 -0
- agentinputlab/algorithms/curate/llmlingua/__init__.py +30 -0
- agentinputlab/algorithms/curate/llmlingua/adapters.py +537 -0
- agentinputlab/algorithms/curate/llmlingua/budget.py +48 -0
- agentinputlab/algorithms/curate/llmlingua/controlled.py +175 -0
- agentinputlab/algorithms/curate/llmlingua/multilingual.py +105 -0
- agentinputlab/algorithms/curate/llmlingua/span_map.py +167 -0
- agentinputlab/algorithms/curate/recomp/__init__.py +47 -0
- agentinputlab/algorithms/curate/recomp/external.py +1129 -0
- agentinputlab/algorithms/curate/recomp/selective.py +304 -0
- agentinputlab/algorithms/decompose/__init__.py +107 -0
- agentinputlab/algorithms/decompose/decomp/__init__.py +53 -0
- agentinputlab/algorithms/decompose/decomp/common_support.py +478 -0
- agentinputlab/algorithms/decompose/decomp/planner_component.py +199 -0
- agentinputlab/algorithms/decompose/decomp/worker_bridge.py +416 -0
- agentinputlab/algorithms/decompose/llm_compiler/__init__.py +35 -0
- agentinputlab/algorithms/decompose/llm_compiler/bridge.py +520 -0
- agentinputlab/algorithms/decompose/llm_compiler/common_support.py +504 -0
- agentinputlab/algorithms/decompose/plan_and_budget/__init__.py +43 -0
- agentinputlab/algorithms/decompose/plan_and_budget/cached_soft_budget.py +362 -0
- agentinputlab/algorithms/decompose/plan_and_budget/decomposer_component.py +107 -0
- agentinputlab/algorithms/decompose/plan_and_budget/full.py +205 -0
- agentinputlab/algorithms/decompose/plan_and_budget/hard_cap.py +246 -0
- agentinputlab/algorithms/decompose/plan_and_budget/regenerated_soft_budget.py +448 -0
- agentinputlab/algorithms/decompose/plan_and_solve/__init__.py +33 -0
- agentinputlab/algorithms/decompose/plan_and_solve/plan_component.py +192 -0
- agentinputlab/algorithms/decompose/plan_and_solve/ps_prompt.py +127 -0
- agentinputlab/algorithms/decompose/self_ask/__init__.py +27 -0
- agentinputlab/algorithms/decompose/self_ask/loop.py +362 -0
- agentinputlab/algorithms/decompose/self_ask/planner_component.py +117 -0
- agentinputlab/algorithms/reformulate/__init__.py +85 -0
- agentinputlab/algorithms/reformulate/infocqr/__init__.py +40 -0
- agentinputlab/algorithms/reformulate/infocqr/cache_replay.py +362 -0
- agentinputlab/algorithms/reformulate/infocqr/external.py +675 -0
- agentinputlab/algorithms/reformulate/infocqr/student.py +189 -0
- agentinputlab/algorithms/reformulate/query2doc/__init__.py +42 -0
- agentinputlab/algorithms/reformulate/query2doc/controlled_live.py +190 -0
- agentinputlab/algorithms/reformulate/query2doc/models.py +95 -0
- agentinputlab/algorithms/reformulate/query2doc/official_cached.py +335 -0
- agentinputlab/algorithms/reformulate/query2doc/querygym_bridge.py +223 -0
- agentinputlab/algorithms/reformulate/query2doc/querygym_runtime.py +326 -0
- agentinputlab/algorithms/reformulate/reformer/__init__.py +23 -0
- agentinputlab/algorithms/reformulate/reformer/external.py +417 -0
- agentinputlab/algorithms/reformulate/rrr.py +696 -0
- agentinputlab/annotation/__init__.py +5 -0
- agentinputlab/annotation/independent.py +428 -0
- agentinputlab/annotation/server.py +233 -0
- agentinputlab/annotation/static/app.js +121 -0
- agentinputlab/annotation/static/index.html +60 -0
- agentinputlab/annotation/static/review.css +2 -0
- agentinputlab/annotation/static/review.html +10 -0
- agentinputlab/annotation/static/review.js +11 -0
- agentinputlab/annotation/static/styles.css +55 -0
- agentinputlab/annotation/store.py +444 -0
- agentinputlab/api.py +368 -0
- agentinputlab/artifacts/__init__.py +15 -0
- agentinputlab/artifacts/json_file.py +104 -0
- agentinputlab/artifacts/manifest.py +98 -0
- agentinputlab/artifacts/store.py +157 -0
- agentinputlab/backends/__init__.py +98 -0
- agentinputlab/backends/embedding/__init__.py +25 -0
- agentinputlab/backends/embedding/base.py +145 -0
- agentinputlab/backends/embedding/callable.py +75 -0
- agentinputlab/backends/embedding/replay.py +129 -0
- agentinputlab/backends/llm/__init__.py +31 -0
- agentinputlab/backends/llm/base.py +149 -0
- agentinputlab/backends/llm/callable.py +108 -0
- agentinputlab/backends/llm/decomp_completion.py +88 -0
- agentinputlab/backends/llm/final_content_stop.py +72 -0
- agentinputlab/backends/llm/gpt_oss_final_channel.py +84 -0
- agentinputlab/backends/llm/openai_compatible.py +332 -0
- agentinputlab/backends/llm/transformers.py +171 -0
- agentinputlab/backends/retrieval/__init__.py +35 -0
- agentinputlab/backends/retrieval/base.py +50 -0
- agentinputlab/backends/retrieval/dense.py +777 -0
- agentinputlab/backends/retrieval/memory.py +119 -0
- agentinputlab/backends/retrieval/sqlite_fts.py +155 -0
- agentinputlab/backends/user/__init__.py +15 -0
- agentinputlab/backends/user/base.py +66 -0
- agentinputlab/backends/user/callable.py +54 -0
- agentinputlab/backends/user/replay.py +89 -0
- agentinputlab/backends/vector.py +38 -0
- agentinputlab/benchmarks/__init__.py +246 -0
- agentinputlab/benchmarks/acquisition.py +106 -0
- agentinputlab/benchmarks/ale.py +540 -0
- agentinputlab/benchmarks/appworld_ul.py +669 -0
- agentinputlab/benchmarks/assets.py +226 -0
- agentinputlab/benchmarks/base.py +68 -0
- agentinputlab/benchmarks/beir_archive.py +235 -0
- agentinputlab/benchmarks/beir_sample.py +269 -0
- agentinputlab/benchmarks/bfcl_native.py +245 -0
- agentinputlab/benchmarks/bfcl_v3.py +607 -0
- agentinputlab/benchmarks/cache.py +218 -0
- agentinputlab/benchmarks/clariq.py +203 -0
- agentinputlab/benchmarks/data/registry.json +321 -0
- agentinputlab/benchmarks/data/smoke_assets.json +511 -0
- agentinputlab/benchmarks/data/statebench_v0_8_1_task_ids.json +475 -0
- agentinputlab/benchmarks/data/toolsandbox_165848b_scenario_names.json +1 -0
- agentinputlab/benchmarks/execution.py +1111 -0
- agentinputlab/benchmarks/interactive.py +257 -0
- agentinputlab/benchmarks/ir_query.py +204 -0
- agentinputlab/benchmarks/longbench.py +203 -0
- agentinputlab/benchmarks/paired_replay.py +479 -0
- agentinputlab/benchmarks/preprocess.py +2313 -0
- agentinputlab/benchmarks/production.py +236 -0
- agentinputlab/benchmarks/qa.py +963 -0
- agentinputlab/benchmarks/registry.py +110 -0
- agentinputlab/benchmarks/retrieval.py +459 -0
- agentinputlab/benchmarks/runtime.py +509 -0
- agentinputlab/benchmarks/statebench.py +938 -0
- agentinputlab/benchmarks/tau3.py +725 -0
- agentinputlab/benchmarks/toolsandbox.py +626 -0
- agentinputlab/benchmarks/trec_dl_sample.py +263 -0
- agentinputlab/campaign.py +654 -0
- agentinputlab/campaign_runner.py +953 -0
- agentinputlab/cli/__init__.py +6 -0
- agentinputlab/cli/main.py +933 -0
- agentinputlab/connectors/__init__.py +46 -0
- agentinputlab/connectors/knowledge/__init__.py +4 -0
- agentinputlab/connectors/tools/__init__.py +30 -0
- agentinputlab/connectors/tools/base.py +57 -0
- agentinputlab/connectors/tools/mcp.py +222 -0
- agentinputlab/connectors/tools/memory.py +41 -0
- agentinputlab/connectors/tools/openapi.py +695 -0
- agentinputlab/connectors/tools/schema.py +102 -0
- agentinputlab/contracts/__init__.py +91 -0
- agentinputlab/contracts/availability.py +24 -0
- agentinputlab/contracts/dependency.py +58 -0
- agentinputlab/contracts/enums.py +82 -0
- agentinputlab/contracts/errors.py +63 -0
- agentinputlab/contracts/evidence.py +43 -0
- agentinputlab/contracts/intent.py +110 -0
- agentinputlab/contracts/lifecycle.py +164 -0
- agentinputlab/contracts/patch.py +167 -0
- agentinputlab/contracts/serialization.py +93 -0
- agentinputlab/contracts/state.py +127 -0
- agentinputlab/contracts/trace.py +59 -0
- agentinputlab/contracts/worker.py +42 -0
- agentinputlab/core/__init__.py +7 -0
- agentinputlab/data/__init__.py +32 -0
- agentinputlab/data/checksums.py +9 -0
- agentinputlab/data/downloader.py +235 -0
- agentinputlab/data/http.py +171 -0
- agentinputlab/data/index.py +21 -0
- agentinputlab/data/overlap.py +28 -0
- agentinputlab/data/preprocess.py +32 -0
- agentinputlab/data/storage.py +57 -0
- agentinputlab/data/trec_sample.py +427 -0
- agentinputlab/evaluation/__init__.py +5 -0
- agentinputlab/evaluation/outcomes.py +32 -0
- agentinputlab/experiments/__init__.py +278 -0
- agentinputlab/experiments/analysis.py +981 -0
- agentinputlab/experiments/artifacts.py +687 -0
- agentinputlab/experiments/automatic_measurement.py +678 -0
- agentinputlab/experiments/auxiliary_matrix.py +203 -0
- agentinputlab/experiments/bindings.py +430 -0
- agentinputlab/experiments/blueprints.py +147 -0
- agentinputlab/experiments/builders/__init__.py +45 -0
- agentinputlab/experiments/builders/executable_external_payload.py +423 -0
- agentinputlab/experiments/builders/external_process_arena.py +1018 -0
- agentinputlab/experiments/builders/registry_intervention.py +463 -0
- agentinputlab/experiments/cli.py +158 -0
- agentinputlab/experiments/controls.py +340 -0
- agentinputlab/experiments/driver_guard.py +205 -0
- agentinputlab/experiments/drivers/__init__.py +89 -0
- agentinputlab/experiments/drivers/external_arena.py +2908 -0
- agentinputlab/experiments/drivers/method_process.py +662 -0
- agentinputlab/experiments/drivers/parity_process.py +1484 -0
- agentinputlab/experiments/drivers/static_production.py +1574 -0
- agentinputlab/experiments/executor.py +448 -0
- agentinputlab/experiments/loader.py +605 -0
- agentinputlab/experiments/measurement.py +675 -0
- agentinputlab/experiments/paper_analysis.py +1818 -0
- agentinputlab/experiments/parity_identity.py +605 -0
- agentinputlab/experiments/plan.py +277 -0
- agentinputlab/experiments/production_builders.py +3739 -0
- agentinputlab/experiments/readiness.py +432 -0
- agentinputlab/experiments/schema.py +514 -0
- agentinputlab/experiments/secrets.py +177 -0
- agentinputlab/experiments/stateful_analysis.py +1738 -0
- agentinputlab/experiments/transfer_analysis.py +1316 -0
- agentinputlab/experiments/unseen_analysis.py +1025 -0
- agentinputlab/integrations/__init__.py +1 -0
- agentinputlab/integrations/action_space/__init__.py +1 -0
- agentinputlab/integrations/external_workers/__init__.py +76 -0
- agentinputlab/integrations/external_workers/ace.py +431 -0
- agentinputlab/integrations/external_workers/base.py +181 -0
- agentinputlab/integrations/external_workers/decomp_live.py +249 -0
- agentinputlab/integrations/external_workers/decomp_planner.py +30 -0
- agentinputlab/integrations/external_workers/infocqr_student.py +280 -0
- agentinputlab/integrations/external_workers/ircot.py +576 -0
- agentinputlab/integrations/external_workers/llmcompiler_live.py +360 -0
- agentinputlab/integrations/external_workers/longllmlingua.py +162 -0
- agentinputlab/integrations/external_workers/method.py +314 -0
- agentinputlab/integrations/external_workers/process.py +678 -0
- agentinputlab/integrations/external_workers/supervisor.py +1071 -0
- agentinputlab/integrations/full_architecture/__init__.py +1 -0
- agentinputlab/integrations/full_architecture/hyde.py +212 -0
- agentinputlab/integrations/full_architecture/plan_and_budget.py +188 -0
- agentinputlab/integrations/live_history.py +222 -0
- agentinputlab/integrations/prompt_optimization/__init__.py +1 -0
- agentinputlab/integrations/training/__init__.py +1 -0
- agentinputlab/metrics/__init__.py +50 -0
- agentinputlab/metrics/catalog.py +313 -0
- agentinputlab/metrics/harm.py +95 -0
- agentinputlab/metrics/paired.py +40 -0
- agentinputlab/metrics/qa.py +76 -0
- agentinputlab/metrics/text.py +33 -0
- agentinputlab/metrics/third_party.py +161 -0
- agentinputlab/models/__init__.py +4 -0
- agentinputlab/observability/__init__.py +56 -0
- agentinputlab/observability/budget.py +316 -0
- agentinputlab/observability/instrumentation.py +754 -0
- agentinputlab/observability/models.py +403 -0
- agentinputlab/observability/monitor.py +380 -0
- agentinputlab/optimizers/__init__.py +1 -0
- agentinputlab/optimizers/clarify/__init__.py +12 -0
- agentinputlab/optimizers/context/__init__.py +22 -0
- agentinputlab/optimizers/decompose/__init__.py +64 -0
- agentinputlab/optimizers/evidence/__init__.py +22 -0
- agentinputlab/optimizers/rewrite/__init__.py +30 -0
- agentinputlab/parity/__init__.py +5 -0
- agentinputlab/parity/base.py +206 -0
- agentinputlab/production_campaign.py +763 -0
- agentinputlab/py.typed +1 -0
- agentinputlab/recipes/__init__.py +333 -0
- agentinputlab/recipes/ace.py +418 -0
- agentinputlab/recipes/ace_bfcl.py +273 -0
- agentinputlab/recipes/ace_common_support.py +169 -0
- agentinputlab/recipes/ace_native_plan.py +322 -0
- agentinputlab/recipes/ace_online.py +711 -0
- agentinputlab/recipes/ace_retrieval.py +330 -0
- agentinputlab/recipes/ace_statebench.py +382 -0
- agentinputlab/recipes/ace_tau.py +463 -0
- agentinputlab/recipes/ace_toolsandbox.py +432 -0
- agentinputlab/recipes/acon_guideline.py +22 -0
- agentinputlab/recipes/adaptive_rag.py +822 -0
- agentinputlab/recipes/llmlingua2_inference.py +146 -0
- agentinputlab/recipes/llmlingua2_training.py +796 -0
- agentinputlab/recipes/mce.py +1486 -0
- agentinputlab/recipes/mce_final.py +335 -0
- agentinputlab/recipes/mce_text_fit.py +499 -0
- agentinputlab/recipes/mce_text_only.py +298 -0
- agentinputlab/recipes/qrag.py +729 -0
- agentinputlab/recipes/qrag_inference.py +364 -0
- agentinputlab/recipes/r2ag_features.py +236 -0
- agentinputlab/recipes/r2ag_inference.py +503 -0
- agentinputlab/recipes/r2ag_training.py +948 -0
- agentinputlab/recipes/recomp_abstractive.py +592 -0
- agentinputlab/recipes/recomp_extractive.py +525 -0
- agentinputlab/recipes/reformer.py +17 -0
- agentinputlab/recipes/rqrag.py +1516 -0
- agentinputlab/recipes/rqrag_common_support.py +193 -0
- agentinputlab/recipes/rqrag_completion.py +147 -0
- agentinputlab/recipes/skillopt.py +462 -0
- agentinputlab/recipes/skillopt_common_support.py +154 -0
- agentinputlab/recipes/skillopt_environment.py +165 -0
- agentinputlab/recipes/skillopt_environment_cli.py +5 -0
- agentinputlab/recipes/skillopt_retrieval.py +188 -0
- agentinputlab/recipes/smartsearch.py +897 -0
- agentinputlab/recipes/smartsearch_curriculum.py +283 -0
- agentinputlab/recipes/smartsearch_grpo_contracts.py +58 -0
- agentinputlab/recipes/smartsearch_grpo_data.py +314 -0
- agentinputlab/recipes/smartsearch_grpo_meter.py +324 -0
- agentinputlab/recipes/smartsearch_grpo_prefix.py +144 -0
- agentinputlab/recipes/smartsearch_grpo_rollout.py +119 -0
- agentinputlab/recipes/smartsearch_grpo_tools.py +364 -0
- agentinputlab/recipes/smartsearch_grpo_training.py +305 -0
- agentinputlab/recipes/smartsearch_grpo_transport.py +248 -0
- agentinputlab/recipes/smartsearch_grpo_worker.py +364 -0
- agentinputlab/recipes/smartsearch_inference.py +524 -0
- agentinputlab/recipes/smartsearch_native_data.py +172 -0
- agentinputlab/recipes/smartsearch_policy.py +616 -0
- agentinputlab/recipes/smartsearch_policy_worker.py +9 -0
- agentinputlab/recipes/smartsearch_sampling.py +232 -0
- agentinputlab/recipes/smartsearch_teacher.py +203 -0
- agentinputlab/recipes/smartsearch_training.py +265 -0
- agentinputlab/registry/__init__.py +46 -0
- agentinputlab/registry/beta.py +727 -0
- agentinputlab/registry/data/algorithms.json +1959 -0
- agentinputlab/registry/data/auxiliary_method_profiles.json +34 -0
- agentinputlab/registry/loader.py +100 -0
- agentinputlab/registry/result_manifest.py +184 -0
- agentinputlab/registry/runtime.py +195 -0
- agentinputlab/registry/schema.py +449 -0
- agentinputlab/runner/__init__.py +30 -0
- agentinputlab/runner/balance.py +248 -0
- agentinputlab/runner/cache.py +135 -0
- agentinputlab/runner/composite.py +130 -0
- agentinputlab/runner/engine.py +285 -0
- agentinputlab/runner/runtime_guard.py +268 -0
- agentinputlab/runner/worker.py +80 -0
- agentinputlab/runtime_bootstrap.py +288 -0
- agentinputlab/selectors/__init__.py +196 -0
- agentinputlab/selectors/advanced.py +1514 -0
- agentinputlab/selectors/artifacts.py +1087 -0
- agentinputlab/selectors/base.py +112 -0
- agentinputlab/selectors/campaigns.py +1893 -0
- agentinputlab/selectors/catalog.py +86 -0
- agentinputlab/selectors/discovery.py +1860 -0
- agentinputlab/selectors/evaluation.py +1348 -0
- agentinputlab/selectors/features.py +383 -0
- agentinputlab/selectors/learned.py +502 -0
- agentinputlab/selectors/no_op.py +28 -0
- agentinputlab/selectors/outcome_table.py +475 -0
- agentinputlab/selectors/producer.py +853 -0
- agentinputlab/selectors/rules.py +109 -0
- agentinputlab/selectors/runtime.py +420 -0
- agentinputlab/studies/__init__.py +16 -0
- agentinputlab/studies/design.py +165 -0
- agentinputlab/studies/paired.py +322 -0
- agentinputlab/studio/__init__.py +6 -0
- agentinputlab/studio/campaigns.py +819 -0
- agentinputlab/studio/engine.py +1408 -0
- agentinputlab/studio/model_backends.py +425 -0
- agentinputlab/studio/prepared.py +410 -0
- agentinputlab/studio/server.py +613 -0
- agentinputlab/studio/static/app.js +397 -0
- agentinputlab/studio/static/campaigns.html +175 -0
- agentinputlab/studio/static/campaigns.js +426 -0
- agentinputlab/studio/static/index.html +192 -0
- agentinputlab/studio/static/result.html +103 -0
- agentinputlab/studio/static/result.js +273 -0
- agentinputlab/studio/static/styles.css +467 -0
- agentinputlab-0.1.0a1.data/data/share/agentinputlab/CITATION.cff +22 -0
- agentinputlab-0.1.0a1.dist-info/METADATA +392 -0
- agentinputlab-0.1.0a1.dist-info/RECORD +469 -0
- agentinputlab-0.1.0a1.dist-info/WHEEL +5 -0
- agentinputlab-0.1.0a1.dist-info/entry_points.txt +6 -0
- agentinputlab-0.1.0a1.dist-info/licenses/LICENSE +21 -0
- agentinputlab-0.1.0a1.dist-info/licenses/NOTICE +31 -0
- agentinputlab-0.1.0a1.dist-info/licenses/THIRD_PARTY_NOTICES.md +94 -0
- agentinputlab-0.1.0a1.dist-info/top_level.txt +2 -0
- workers/README.md +94 -0
- workers/ace/README.md +261 -0
- workers/ace/__init__.py +1 -0
- workers/ace/compatibility_manifest.json +39 -0
- workers/ace/driver.py +1682 -0
- workers/acon/README.md +215 -0
- workers/acon/__init__.py +5 -0
- workers/acon/compatibility_manifest.json +93 -0
- workers/acon/driver.py +1859 -0
- workers/acon/fitted.py +395 -0
- workers/acon/guideline.py +470 -0
- workers/acon/guideline_source_manifest.json +20 -0
- workers/adaptive_rag/README.md +96 -0
- workers/adaptive_rag/__init__.py +15 -0
- workers/adaptive_rag/compatibility_manifest.json +61 -0
- workers/adaptive_rag/driver.py +1470 -0
- workers/adaptive_rag/fixture_runtime.py +163 -0
- workers/adaptive_rag/runtime.py +252 -0
- workers/ask_or_assume/README.md +124 -0
- workers/ask_or_assume/__init__.py +1 -0
- workers/ask_or_assume/compatibility_manifest.json +36 -0
- workers/ask_or_assume/driver.py +1227 -0
- workers/decomp/README.md +133 -0
- workers/decomp/UPSTREAM_LICENSE.Apache-2.0.txt +201 -0
- workers/decomp/__init__.py +5 -0
- workers/decomp/compatibility_manifest.json +205 -0
- workers/decomp/driver.py +1015 -0
- workers/decomp/live.py +137 -0
- workers/decomp/runtime.py +231 -0
- workers/infocqr/README.md +106 -0
- workers/infocqr/__init__.py +1 -0
- workers/infocqr/compatibility_manifest.json +84 -0
- workers/infocqr/driver.py +1766 -0
- workers/ircot/README.md +167 -0
- workers/ircot/__init__.py +7 -0
- workers/ircot/compatibility_manifest.json +121 -0
- workers/ircot/controlled.py +457 -0
- workers/ircot/driver.py +1373 -0
- workers/ircot/fixture_bindings.json +39 -0
- workers/ircot/fixture_config.json +48 -0
- workers/ircot/fixture_prompt.txt +2 -0
- workers/ircot/native_qa.py +398 -0
- workers/ircot/qa_compatibility_manifest.json +57 -0
- workers/ircot/runtime.py +97 -0
- workers/llmcompiler/README.md +160 -0
- workers/llmcompiler/__init__.py +1 -0
- workers/llmcompiler/compatibility_manifest.json +45 -0
- workers/llmcompiler/driver.py +1514 -0
- workers/llmcompiler/export_prompts.py +74 -0
- workers/llmcompiler/live.py +174 -0
- workers/llmcompiler/patch_manifest.json +147 -0
- workers/mce/README.md +198 -0
- workers/mce/__init__.py +29 -0
- workers/mce/compatibility_manifest.json +115 -0
- workers/mce/driver.py +2585 -0
- workers/mce/final_eval.py +173 -0
- workers/official_arenas/README.md +25 -0
- workers/official_arenas/__init__.py +1 -0
- workers/official_arenas/capture_records.py +110 -0
- workers/official_arenas/driver.py +619 -0
- workers/official_arenas/protocol.py +334 -0
- workers/official_arenas/providers/__init__.py +1 -0
- workers/official_arenas/providers/_live_history.py +228 -0
- workers/official_arenas/providers/_message_compat.py +77 -0
- workers/official_arenas/providers/appworld_ul.py +196 -0
- workers/official_arenas/providers/fixture.py +112 -0
- workers/official_arenas/providers/statebench.py +960 -0
- workers/official_arenas/providers/tau3.py +1672 -0
- workers/official_arenas/providers/tau_retail.py +586 -0
- workers/official_arenas/providers/toolsandbox.py +1321 -0
- workers/official_arenas/providers/tooltalk.py +699 -0
- workers/official_arenas/retail_capture.py +188 -0
- workers/official_arenas/retail_driver.py +275 -0
- workers/qrag/README.md +172 -0
- workers/qrag/__init__.py +1 -0
- workers/qrag/compatibility_manifest.json +64 -0
- workers/qrag/driver.py +673 -0
- workers/r2ag/README.md +154 -0
- workers/r2ag/__init__.py +5 -0
- workers/r2ag/compatibility_manifest.json +55 -0
- workers/r2ag/driver.py +1287 -0
- workers/r2ag/feature_schema.json +8 -0
- workers/r2ag/runtime.py +589 -0
- workers/recomp/README.md +120 -0
- workers/recomp/__init__.py +1 -0
- workers/recomp/compatibility_manifest.json +43 -0
- workers/recomp/driver.py +1653 -0
- workers/reformer/README.md +125 -0
- workers/reformer/__init__.py +1 -0
- workers/reformer/compatibility_manifest.json +68 -0
- workers/reformer/driver.py +1459 -0
- workers/reformer/induction.py +457 -0
- workers/rrr/README.md +126 -0
- workers/rrr/__init__.py +1 -0
- workers/rrr/compatibility_manifest.json +57 -0
- workers/rrr/driver.py +1133 -0
- workers/smartsearch/README.md +331 -0
- workers/smartsearch/__init__.py +1 -0
- workers/smartsearch/compatibility_manifest.json +41 -0
- workers/smartsearch/driver.py +1643 -0
workers/decomp/driver.py
ADDED
|
@@ -0,0 +1,1015 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""JSONL driver for allenai/DecomP at one audited upstream revision.
|
|
3
|
+
|
|
4
|
+
This worker deliberately lives outside the core package. It imports the
|
|
5
|
+
upstream checkout, preserves its controller/participant implementations, and
|
|
6
|
+
applies only the bounded and observable search-loop changes declared in
|
|
7
|
+
``compatibility_manifest.json``.
|
|
8
|
+
|
|
9
|
+
The bounded best-first compatibility loop follows the Apache-2.0-licensed
|
|
10
|
+
``BestFirstDecomposer.find_answer_decomp`` control flow at the pinned revision;
|
|
11
|
+
see ``THIRD_PARTY_NOTICES.md`` and the compatibility manifest for attribution.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import contextlib
|
|
17
|
+
import copy
|
|
18
|
+
import hashlib
|
|
19
|
+
import heapq
|
|
20
|
+
import importlib
|
|
21
|
+
import io
|
|
22
|
+
import json
|
|
23
|
+
import math
|
|
24
|
+
import os
|
|
25
|
+
import subprocess
|
|
26
|
+
import sys
|
|
27
|
+
from collections.abc import Callable, Iterator, Mapping, Sequence
|
|
28
|
+
from dataclasses import dataclass
|
|
29
|
+
from pathlib import Path
|
|
30
|
+
from types import SimpleNamespace
|
|
31
|
+
from typing import Any, TextIO
|
|
32
|
+
|
|
33
|
+
UPSTREAM_ENV = "AGENTINPUTLAB_UPSTREAM_ROOT"
|
|
34
|
+
UPSTREAM_COMMIT = "8b157f6fe9ff5676ae51bd054f1df7b015adf278"
|
|
35
|
+
REQUEST_SCHEMA = "agentinputlab.decomp.worker.v2"
|
|
36
|
+
PROFILE_ID = "plan.decomp.full"
|
|
37
|
+
PLANNER_SCHEMA = "agentinputlab.decomp.planner.v1"
|
|
38
|
+
PLANNER_PROFILE_ID = "plan.decomp.planner_component"
|
|
39
|
+
MANIFEST_SCHEMA = "agentinputlab.decomp.compatibility.v1"
|
|
40
|
+
DEFAULT_MANIFEST = Path(__file__).with_name("compatibility_manifest.json")
|
|
41
|
+
MAX_REQUEST_BYTES = 1024 * 1024
|
|
42
|
+
MAX_QUESTION_CHARS = 200_000
|
|
43
|
+
MAX_CONTEXT_BLOCKS = 512
|
|
44
|
+
MAX_STEPS = 256
|
|
45
|
+
_REQUEST_FIELDS = frozenset(
|
|
46
|
+
{
|
|
47
|
+
"case_id",
|
|
48
|
+
"question",
|
|
49
|
+
"paras",
|
|
50
|
+
"paragraph_ids",
|
|
51
|
+
"context_hash",
|
|
52
|
+
"config_artifact_id",
|
|
53
|
+
"backend_binding",
|
|
54
|
+
"max_steps",
|
|
55
|
+
"upstream_revision",
|
|
56
|
+
"schema_version",
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
_SECRET_KEY_NAMES = frozenset(
|
|
60
|
+
{
|
|
61
|
+
"api_key",
|
|
62
|
+
"apikey",
|
|
63
|
+
"authorization",
|
|
64
|
+
"authorization_header",
|
|
65
|
+
"auth_header",
|
|
66
|
+
"credential",
|
|
67
|
+
"credentials",
|
|
68
|
+
"password",
|
|
69
|
+
"passwd",
|
|
70
|
+
"secret",
|
|
71
|
+
"client_secret",
|
|
72
|
+
"api_secret",
|
|
73
|
+
"token",
|
|
74
|
+
"access_token",
|
|
75
|
+
"auth_token",
|
|
76
|
+
"bearer_token",
|
|
77
|
+
"refresh_token",
|
|
78
|
+
"session_token",
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
_SECRET_KEY_SUFFIXES = (
|
|
82
|
+
"access_token",
|
|
83
|
+
"api_key",
|
|
84
|
+
"apikey",
|
|
85
|
+
"auth_token",
|
|
86
|
+
"bearer_token",
|
|
87
|
+
"credential",
|
|
88
|
+
"credentials",
|
|
89
|
+
"password",
|
|
90
|
+
"refresh_token",
|
|
91
|
+
"secret",
|
|
92
|
+
"session_token",
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class DriverError(RuntimeError):
|
|
97
|
+
"""A public, secret-free worker failure."""
|
|
98
|
+
|
|
99
|
+
def __init__(self, code: str, message: str) -> None:
|
|
100
|
+
super().__init__(message)
|
|
101
|
+
self.code = code
|
|
102
|
+
self.public_message = message
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass(frozen=True, slots=True)
|
|
106
|
+
class WorkerRequest:
|
|
107
|
+
case_id: str
|
|
108
|
+
question: str
|
|
109
|
+
paras: tuple[str, ...]
|
|
110
|
+
paragraph_ids: tuple[str, ...]
|
|
111
|
+
context_hash: str
|
|
112
|
+
config_artifact_id: str
|
|
113
|
+
backend_binding: Mapping[str, Any]
|
|
114
|
+
max_steps: int
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@dataclass(frozen=True, slots=True)
|
|
118
|
+
class Checkout:
|
|
119
|
+
root: Path
|
|
120
|
+
manifest: Mapping[str, Any]
|
|
121
|
+
manifest_sha256: str
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class _BoundedCapture(io.TextIOBase):
|
|
125
|
+
def __init__(self, limit: int = 64 * 1024) -> None:
|
|
126
|
+
self.limit = limit
|
|
127
|
+
self.size = 0
|
|
128
|
+
self.digest = hashlib.sha256()
|
|
129
|
+
|
|
130
|
+
def writable(self) -> bool:
|
|
131
|
+
return True
|
|
132
|
+
|
|
133
|
+
def write(self, value: str) -> int:
|
|
134
|
+
if not isinstance(value, str):
|
|
135
|
+
raise TypeError("upstream output must be text")
|
|
136
|
+
encoded = value.encode("utf-8", errors="replace")
|
|
137
|
+
self.size += len(encoded)
|
|
138
|
+
if self.size > self.limit:
|
|
139
|
+
raise DriverError("UPSTREAM_OUTPUT_LIMIT", "upstream diagnostic output exceeded limit")
|
|
140
|
+
self.digest.update(encoded)
|
|
141
|
+
return len(value)
|
|
142
|
+
|
|
143
|
+
@property
|
|
144
|
+
def hexdigest(self) -> str:
|
|
145
|
+
return self.digest.hexdigest()
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class _AuditedBestFirst:
|
|
149
|
+
"""Control-flow-equivalent bounded form of upstream BestFirstDecomposer."""
|
|
150
|
+
|
|
151
|
+
def __init__(self, controller: Any, search_state_class: type[Any], max_steps: int) -> None:
|
|
152
|
+
self.controller = controller
|
|
153
|
+
self.search_state_class = search_state_class
|
|
154
|
+
self.max_steps = max_steps
|
|
155
|
+
self._state_ids: dict[int, str] = {}
|
|
156
|
+
self._states: list[Any] = []
|
|
157
|
+
self._depths: dict[int, int] = {}
|
|
158
|
+
self.edges: list[tuple[str, str]] = []
|
|
159
|
+
self.participant_calls: list[dict[str, Any]] = []
|
|
160
|
+
self.frontier: list[dict[str, Any]] = []
|
|
161
|
+
self.execute_calls = 0
|
|
162
|
+
|
|
163
|
+
def _register(self, state: Any, *, parent: Any | None = None) -> str:
|
|
164
|
+
_validate_search_state(state)
|
|
165
|
+
identity = id(state)
|
|
166
|
+
if identity in self._state_ids:
|
|
167
|
+
return self._state_ids[identity]
|
|
168
|
+
state_id = f"s{len(self._states):04d}"
|
|
169
|
+
self._state_ids[identity] = state_id
|
|
170
|
+
self._states.append(state)
|
|
171
|
+
depth = 0 if parent is None else self._depths[id(parent)] + 1
|
|
172
|
+
self._depths[identity] = depth
|
|
173
|
+
if parent is not None:
|
|
174
|
+
edge = (self._state_ids[id(parent)], state_id)
|
|
175
|
+
if edge not in self.edges:
|
|
176
|
+
self.edges.append(edge)
|
|
177
|
+
return state_id
|
|
178
|
+
|
|
179
|
+
def run(self, example: Mapping[str, Any]) -> tuple[Any | None, str]:
|
|
180
|
+
start_command = self.controller.start_state
|
|
181
|
+
start_data = self.controller.init_data(dict(example))
|
|
182
|
+
initial = self.search_state_class(start_data, start_command, score=0.0)
|
|
183
|
+
self._register(initial)
|
|
184
|
+
frontier_heap = [initial]
|
|
185
|
+
|
|
186
|
+
while frontier_heap:
|
|
187
|
+
current = heapq.heappop(frontier_heap)
|
|
188
|
+
current_id = self._register(current)
|
|
189
|
+
event = {
|
|
190
|
+
"index": len(self.frontier),
|
|
191
|
+
"state_id": current_id,
|
|
192
|
+
"score": _finite_score(current.score),
|
|
193
|
+
"next_command": _command_text(current.next),
|
|
194
|
+
"action": "pop",
|
|
195
|
+
}
|
|
196
|
+
self.frontier.append(event)
|
|
197
|
+
|
|
198
|
+
if current.next == self.controller.end_state:
|
|
199
|
+
if current.data.has_tasks():
|
|
200
|
+
new_task = current.data.pop_task()
|
|
201
|
+
new_state = current.copy()
|
|
202
|
+
if new_task.task_question:
|
|
203
|
+
new_state.data.add_qgen(new_task.task_question)
|
|
204
|
+
new_state.next = new_task.task_participant
|
|
205
|
+
self._register(new_state, parent=current)
|
|
206
|
+
event["action"] = "resume_task"
|
|
207
|
+
heapq.heappush(frontier_heap, new_state)
|
|
208
|
+
continue
|
|
209
|
+
event["action"] = "terminate"
|
|
210
|
+
return current, "end_state"
|
|
211
|
+
|
|
212
|
+
if self.execute_calls >= self.max_steps:
|
|
213
|
+
event["action"] = "stop_max_steps"
|
|
214
|
+
return current, "max_steps"
|
|
215
|
+
|
|
216
|
+
participant = _command_text(current.next)
|
|
217
|
+
outputs = self.controller.execute(current, debug=False)
|
|
218
|
+
self.execute_calls += 1
|
|
219
|
+
if not isinstance(outputs, list):
|
|
220
|
+
raise DriverError(
|
|
221
|
+
"UPSTREAM_OUTPUT_INVALID",
|
|
222
|
+
"upstream ModelController.execute returned a non-list result",
|
|
223
|
+
)
|
|
224
|
+
if not outputs:
|
|
225
|
+
self.participant_calls.append(
|
|
226
|
+
{
|
|
227
|
+
"index": len(self.participant_calls),
|
|
228
|
+
"participant": participant,
|
|
229
|
+
"input_state_id": current_id,
|
|
230
|
+
"output_state_id": current_id,
|
|
231
|
+
"command": participant,
|
|
232
|
+
}
|
|
233
|
+
)
|
|
234
|
+
for output in outputs:
|
|
235
|
+
output_id = self._register(output, parent=current)
|
|
236
|
+
self.participant_calls.append(
|
|
237
|
+
{
|
|
238
|
+
"index": len(self.participant_calls),
|
|
239
|
+
"participant": participant,
|
|
240
|
+
"input_state_id": current_id,
|
|
241
|
+
"output_state_id": output_id,
|
|
242
|
+
"command": participant,
|
|
243
|
+
}
|
|
244
|
+
)
|
|
245
|
+
heapq.heappush(frontier_heap, output)
|
|
246
|
+
|
|
247
|
+
return None, "frontier_exhausted"
|
|
248
|
+
|
|
249
|
+
def nodes(self, fallback_question: str) -> list[dict[str, Any]]:
|
|
250
|
+
nodes: list[dict[str, Any]] = []
|
|
251
|
+
for state in self._states:
|
|
252
|
+
data = state.data
|
|
253
|
+
nodes.append(
|
|
254
|
+
{
|
|
255
|
+
"node_id": self._state_ids[id(state)],
|
|
256
|
+
"question": _last_text(data, "get_last_question", fallback_question),
|
|
257
|
+
"answer": _last_text(data, "get_last_answer", ""),
|
|
258
|
+
"handler": _command_text(state.next),
|
|
259
|
+
"depth": self._depths[id(state)],
|
|
260
|
+
}
|
|
261
|
+
)
|
|
262
|
+
return nodes
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
class DecompDriver:
|
|
266
|
+
def __init__(
|
|
267
|
+
self,
|
|
268
|
+
*,
|
|
269
|
+
manifest_path: Path = DEFAULT_MANIFEST,
|
|
270
|
+
environment: Mapping[str, str] | None = None,
|
|
271
|
+
native_completion: Callable[..., Mapping[str, Any]] | None = None,
|
|
272
|
+
) -> None:
|
|
273
|
+
self.manifest_path = Path(manifest_path)
|
|
274
|
+
self.environment = dict(os.environ if environment is None else environment)
|
|
275
|
+
self.native_completion = native_completion
|
|
276
|
+
|
|
277
|
+
def smoke_upstream_core(self) -> Mapping[str, str]:
|
|
278
|
+
checkout = self._checkout()
|
|
279
|
+
with _upstream_import_scope(checkout.root):
|
|
280
|
+
try:
|
|
281
|
+
module = importlib.import_module("decomp.inference.model_search")
|
|
282
|
+
except ModuleNotFoundError as exc:
|
|
283
|
+
raise _dependency_error(exc) from exc
|
|
284
|
+
symbol = getattr(module, "BestFirstDecomposer", None)
|
|
285
|
+
if not isinstance(symbol, type):
|
|
286
|
+
raise DriverError(
|
|
287
|
+
"UPSTREAM_API_MISMATCH",
|
|
288
|
+
"upstream BestFirstDecomposer is unavailable",
|
|
289
|
+
)
|
|
290
|
+
return {
|
|
291
|
+
"upstream_revision": UPSTREAM_COMMIT,
|
|
292
|
+
"symbol": "decomp.inference.model_search.BestFirstDecomposer",
|
|
293
|
+
"manifest_sha256": checkout.manifest_sha256,
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
def run(self, value: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
297
|
+
return self._run_native(value, planner_component=False)
|
|
298
|
+
|
|
299
|
+
def run_planner(self, value: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
300
|
+
"""Execute exactly the source controller's first planner transition."""
|
|
301
|
+
if value.get("schema_version") != PLANNER_SCHEMA or value.get("max_steps") != 1:
|
|
302
|
+
raise DriverError("INVALID_REQUEST", "planner requires its own schema and one transition")
|
|
303
|
+
native_value = dict(value, schema_version=REQUEST_SCHEMA)
|
|
304
|
+
return self._run_native(native_value, planner_component=True)
|
|
305
|
+
|
|
306
|
+
def _run_native(self, value: Mapping[str, Any], *, planner_component: bool) -> Mapping[str, Any]:
|
|
307
|
+
request = _parse_request(value)
|
|
308
|
+
checkout = self._checkout()
|
|
309
|
+
artifact = _resolve_artifact(checkout, request.config_artifact_id)
|
|
310
|
+
config = _load_config(artifact)
|
|
311
|
+
config = _validate_and_patch_config(config, checkout, request.max_steps)
|
|
312
|
+
if planner_component:
|
|
313
|
+
start = config.get("start_state")
|
|
314
|
+
if config["models"].get(start, {}).get("name") not in {"llmqadecomp", "lmgen"}:
|
|
315
|
+
raise DriverError("CONTROLLED_CONFIG_UNSUPPORTED", "component start must be a native planner")
|
|
316
|
+
capture = _BoundedCapture()
|
|
317
|
+
|
|
318
|
+
with (
|
|
319
|
+
_working_directory(checkout.root),
|
|
320
|
+
_upstream_import_scope(checkout.root),
|
|
321
|
+
contextlib.redirect_stdout(capture),
|
|
322
|
+
):
|
|
323
|
+
try:
|
|
324
|
+
configurable = importlib.import_module("decomp.inference.configurable_inference")
|
|
325
|
+
model_search = importlib.import_module("decomp.inference.model_search")
|
|
326
|
+
except ModuleNotFoundError as exc:
|
|
327
|
+
raise _dependency_error(exc) from exc
|
|
328
|
+
if self.native_completion is not None:
|
|
329
|
+
_bind_native_completion(checkout, config, self.native_completion)
|
|
330
|
+
builder = getattr(configurable, "build_decomposer_and_models", None)
|
|
331
|
+
search_state_class = getattr(model_search, "SearchState", None)
|
|
332
|
+
best_first_class = getattr(model_search, "BestFirstDecomposer", None)
|
|
333
|
+
if not callable(builder) or not isinstance(search_state_class, type):
|
|
334
|
+
raise DriverError(
|
|
335
|
+
"UPSTREAM_API_MISMATCH",
|
|
336
|
+
"upstream configurable inference API is incompatible",
|
|
337
|
+
)
|
|
338
|
+
try:
|
|
339
|
+
upstream_decomposer, model_map = builder(copy.deepcopy(config))
|
|
340
|
+
except ModuleNotFoundError as exc:
|
|
341
|
+
raise _dependency_error(exc) from exc
|
|
342
|
+
except DriverError:
|
|
343
|
+
raise
|
|
344
|
+
except Exception as exc:
|
|
345
|
+
raise DriverError(
|
|
346
|
+
"UPSTREAM_BOOT_FAILED",
|
|
347
|
+
"upstream participant construction failed",
|
|
348
|
+
) from exc
|
|
349
|
+
if not isinstance(best_first_class, type) or not isinstance(
|
|
350
|
+
upstream_decomposer, best_first_class
|
|
351
|
+
):
|
|
352
|
+
raise DriverError(
|
|
353
|
+
"UPSTREAM_API_MISMATCH",
|
|
354
|
+
"upstream builder did not return BestFirstDecomposer",
|
|
355
|
+
)
|
|
356
|
+
if not isinstance(model_map, Mapping):
|
|
357
|
+
raise DriverError(
|
|
358
|
+
"UPSTREAM_API_MISMATCH",
|
|
359
|
+
"upstream builder returned an invalid model map",
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
search = _AuditedBestFirst(
|
|
363
|
+
upstream_decomposer.controller,
|
|
364
|
+
search_state_class,
|
|
365
|
+
request.max_steps,
|
|
366
|
+
)
|
|
367
|
+
example = {
|
|
368
|
+
"qid": request.case_id,
|
|
369
|
+
"query": request.question,
|
|
370
|
+
"question": request.question,
|
|
371
|
+
"paras": list(request.paras),
|
|
372
|
+
"paragraph_ids": list(request.paragraph_ids),
|
|
373
|
+
"context_hash": request.context_hash,
|
|
374
|
+
}
|
|
375
|
+
try:
|
|
376
|
+
final_state, termination = search.run(example)
|
|
377
|
+
except DriverError:
|
|
378
|
+
raise
|
|
379
|
+
except Exception as exc:
|
|
380
|
+
raise DriverError(
|
|
381
|
+
"UPSTREAM_EXECUTION_FAILED",
|
|
382
|
+
"upstream DecomP execution failed",
|
|
383
|
+
) from exc
|
|
384
|
+
model_calls, call_breakdown = _model_call_usage(
|
|
385
|
+
model_map,
|
|
386
|
+
checkout.manifest,
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
if model_calls < 1:
|
|
390
|
+
raise DriverError(
|
|
391
|
+
"USAGE_ACCOUNTING_UNAVAILABLE",
|
|
392
|
+
"upstream run did not report a model call",
|
|
393
|
+
)
|
|
394
|
+
if planner_component:
|
|
395
|
+
if model_calls != 1 or search.execute_calls != 1 or final_state is None:
|
|
396
|
+
raise DriverError("UPSTREAM_OUTPUT_INVALID", "planner did not stop after one native call")
|
|
397
|
+
if any(call["participant"] != config["start_state"] for call in search.participant_calls):
|
|
398
|
+
raise DriverError("UPSTREAM_OUTPUT_INVALID", "component executed a non-planner participant")
|
|
399
|
+
if any(_last_text(s.data, "get_last_answer", "") for s in search._states):
|
|
400
|
+
raise DriverError("UPSTREAM_OUTPUT_INVALID", "planner component unexpectedly produced an answer")
|
|
401
|
+
candidates = [
|
|
402
|
+
{"question": _last_text(s.data, "get_last_question", ""),
|
|
403
|
+
"next_command": _command_text(s.next), "score": _finite_score(s.score)}
|
|
404
|
+
for s in search._states[1:]
|
|
405
|
+
]
|
|
406
|
+
selected = {"question": _last_text(final_state.data, "get_last_question", ""),
|
|
407
|
+
"next_command": _command_text(final_state.next),
|
|
408
|
+
"score": _finite_score(final_state.score)}
|
|
409
|
+
if not candidates or not selected["question"].strip():
|
|
410
|
+
raise DriverError("UPSTREAM_OUTPUT_INVALID", "native planner returned no next question")
|
|
411
|
+
return {
|
|
412
|
+
"schema_version": PLANNER_SCHEMA, "profile_id": PLANNER_PROFILE_ID,
|
|
413
|
+
"case_id": request.case_id, "upstream_revision": UPSTREAM_COMMIT,
|
|
414
|
+
"paragraph_ids": list(request.paragraph_ids), "context_hash": request.context_hash,
|
|
415
|
+
"component_only": True, "selected": selected, "candidates": candidates,
|
|
416
|
+
"participant_calls": search.participant_calls, "frontier": search.frontier,
|
|
417
|
+
"native_termination": termination, "router_executed": False, "qa_executed": False,
|
|
418
|
+
"usage": {"model_calls": 1, "retrieval_calls": 0,
|
|
419
|
+
"input_tokens": 0, "output_tokens": 0, "unpriced_model_calls": 1},
|
|
420
|
+
"native_artifacts": {"config_artifact_id": request.config_artifact_id,
|
|
421
|
+
"config_sha256": artifact["sha256"],
|
|
422
|
+
"compatibility_manifest_sha256": checkout.manifest_sha256,
|
|
423
|
+
"backend_binding_hash": _sha256_bytes(_canonical_json(request.backend_binding).encode()),
|
|
424
|
+
"upstream_stdout_sha256": capture.hexdigest,
|
|
425
|
+
"boundary": "first_native_planner_transition_before_router_or_qa",
|
|
426
|
+
"model_call_breakdown": _canonical_json(call_breakdown)},
|
|
427
|
+
}
|
|
428
|
+
reasoning_chain, final_answer = _project_final_state(
|
|
429
|
+
final_state,
|
|
430
|
+
request.question,
|
|
431
|
+
)
|
|
432
|
+
backend_hash = _sha256_bytes(_canonical_json(request.backend_binding).encode("utf-8"))
|
|
433
|
+
patch_ids = [
|
|
434
|
+
patch["id"]
|
|
435
|
+
for patch in checkout.manifest["patches"]
|
|
436
|
+
if isinstance(patch, Mapping) and isinstance(patch.get("id"), str)
|
|
437
|
+
]
|
|
438
|
+
return {
|
|
439
|
+
"case_id": request.case_id,
|
|
440
|
+
"profile_id": PROFILE_ID,
|
|
441
|
+
"upstream_revision": UPSTREAM_COMMIT,
|
|
442
|
+
"paragraph_ids": list(request.paragraph_ids),
|
|
443
|
+
"context_hash": request.context_hash,
|
|
444
|
+
"nodes": search.nodes(request.question),
|
|
445
|
+
"dependency_edges": [list(edge) for edge in search.edges],
|
|
446
|
+
"participant_calls": search.participant_calls,
|
|
447
|
+
"frontier": search.frontier,
|
|
448
|
+
"reasoning_chain": reasoning_chain,
|
|
449
|
+
"final_answer": final_answer,
|
|
450
|
+
"termination": termination,
|
|
451
|
+
"component_only": False,
|
|
452
|
+
"usage": {
|
|
453
|
+
"model_calls": model_calls,
|
|
454
|
+
"retrieval_calls": 0,
|
|
455
|
+
"unpriced_model_calls": model_calls,
|
|
456
|
+
"input_tokens": 0,
|
|
457
|
+
"output_tokens": 0,
|
|
458
|
+
},
|
|
459
|
+
"schema_version": REQUEST_SCHEMA,
|
|
460
|
+
"native_artifacts": {
|
|
461
|
+
"config_artifact_id": request.config_artifact_id,
|
|
462
|
+
"config_sha256": artifact["sha256"],
|
|
463
|
+
"compatibility_manifest_sha256": checkout.manifest_sha256,
|
|
464
|
+
"compatibility_patch_ids": _canonical_json(patch_ids),
|
|
465
|
+
"backend_binding_hash": backend_hash,
|
|
466
|
+
"backend_binding_mode": "audit_identity_only; upstream-native model clients",
|
|
467
|
+
"context_hash": request.context_hash,
|
|
468
|
+
"paragraph_ids_hash": _sha256_bytes(
|
|
469
|
+
_canonical_json(request.paragraph_ids).encode("utf-8")
|
|
470
|
+
),
|
|
471
|
+
"paragraph_count": str(len(request.paragraph_ids)),
|
|
472
|
+
"model_call_breakdown": _canonical_json(call_breakdown),
|
|
473
|
+
"token_accounting": "unavailable_in_pinned_upstream",
|
|
474
|
+
"upstream_stdout_sha256": capture.hexdigest,
|
|
475
|
+
},
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
def _checkout(self) -> Checkout:
|
|
479
|
+
root_value = self.environment.get(UPSTREAM_ENV, "")
|
|
480
|
+
if not root_value:
|
|
481
|
+
raise DriverError(
|
|
482
|
+
"UPSTREAM_ROOT_MISSING",
|
|
483
|
+
f"{UPSTREAM_ENV} must point to the pinned DecomP checkout",
|
|
484
|
+
)
|
|
485
|
+
root = Path(root_value).expanduser().resolve()
|
|
486
|
+
if not root.is_dir():
|
|
487
|
+
raise DriverError("UPSTREAM_ROOT_INVALID", "upstream checkout directory is unavailable")
|
|
488
|
+
manifest, manifest_sha = _load_manifest(self.manifest_path)
|
|
489
|
+
_verify_checkout(root, manifest)
|
|
490
|
+
return Checkout(root=root, manifest=manifest, manifest_sha256=manifest_sha)
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
def _parse_request(value: Mapping[str, Any]) -> WorkerRequest:
|
|
494
|
+
if not isinstance(value, Mapping):
|
|
495
|
+
raise DriverError("INVALID_REQUEST", "worker request must be a JSON object")
|
|
496
|
+
unknown = set(value) - _REQUEST_FIELDS
|
|
497
|
+
missing = _REQUEST_FIELDS - set(value)
|
|
498
|
+
if unknown or missing:
|
|
499
|
+
raise DriverError("INVALID_REQUEST", "worker request fields do not match schema")
|
|
500
|
+
if value.get("upstream_revision") != UPSTREAM_COMMIT:
|
|
501
|
+
raise DriverError("REVISION_MISMATCH", "request upstream revision is not pinned")
|
|
502
|
+
if value.get("schema_version") != REQUEST_SCHEMA:
|
|
503
|
+
raise DriverError("SCHEMA_MISMATCH", "request schema version is unsupported")
|
|
504
|
+
case_id = _bounded_text(value.get("case_id"), "case_id", limit=512)
|
|
505
|
+
question = _bounded_text(value.get("question"), "question", limit=MAX_QUESTION_CHARS)
|
|
506
|
+
paras = _bounded_text_sequence(
|
|
507
|
+
value.get("paras"),
|
|
508
|
+
"paras",
|
|
509
|
+
item_limit=MAX_QUESTION_CHARS,
|
|
510
|
+
)
|
|
511
|
+
paragraph_ids = _bounded_text_sequence(
|
|
512
|
+
value.get("paragraph_ids"),
|
|
513
|
+
"paragraph_ids",
|
|
514
|
+
item_limit=512,
|
|
515
|
+
)
|
|
516
|
+
if len(paras) != len(paragraph_ids):
|
|
517
|
+
raise DriverError("INVALID_REQUEST", "paras and paragraph_ids must have equal length")
|
|
518
|
+
if any(not paragraph_id.strip() for paragraph_id in paragraph_ids):
|
|
519
|
+
raise DriverError("INVALID_REQUEST", "paragraph_ids must contain non-empty IDs")
|
|
520
|
+
if len(paragraph_ids) != len(set(paragraph_ids)):
|
|
521
|
+
raise DriverError("INVALID_REQUEST", "paragraph_ids must be unique")
|
|
522
|
+
context_hash = _bounded_hash(value.get("context_hash"), "context_hash")
|
|
523
|
+
if context_hash != _context_hash(paragraph_ids, paras):
|
|
524
|
+
raise DriverError("CONTEXT_IDENTITY_MISMATCH", "context_hash does not match paragraphs")
|
|
525
|
+
artifact_id = _bounded_text(
|
|
526
|
+
value.get("config_artifact_id"),
|
|
527
|
+
"config_artifact_id",
|
|
528
|
+
limit=256,
|
|
529
|
+
)
|
|
530
|
+
max_steps = value.get("max_steps")
|
|
531
|
+
if (
|
|
532
|
+
isinstance(max_steps, bool)
|
|
533
|
+
or not isinstance(max_steps, int)
|
|
534
|
+
or not 1 <= max_steps <= MAX_STEPS
|
|
535
|
+
):
|
|
536
|
+
raise DriverError("INVALID_REQUEST", f"max_steps must be between 1 and {MAX_STEPS}")
|
|
537
|
+
backend_binding = value.get("backend_binding")
|
|
538
|
+
if not isinstance(backend_binding, Mapping):
|
|
539
|
+
raise DriverError("INVALID_REQUEST", "backend_binding must be a JSON object")
|
|
540
|
+
_reject_secret_fields(backend_binding)
|
|
541
|
+
try:
|
|
542
|
+
encoded_binding = _canonical_json(backend_binding)
|
|
543
|
+
except (TypeError, ValueError) as exc:
|
|
544
|
+
raise DriverError("INVALID_REQUEST", "backend_binding must be canonical JSON") from exc
|
|
545
|
+
if len(encoded_binding.encode("utf-8")) > 64 * 1024:
|
|
546
|
+
raise DriverError("INVALID_REQUEST", "backend_binding exceeds its size limit")
|
|
547
|
+
return WorkerRequest(
|
|
548
|
+
case_id=case_id,
|
|
549
|
+
question=question,
|
|
550
|
+
paras=paras,
|
|
551
|
+
paragraph_ids=paragraph_ids,
|
|
552
|
+
context_hash=context_hash,
|
|
553
|
+
config_artifact_id=artifact_id,
|
|
554
|
+
backend_binding=json.loads(encoded_binding),
|
|
555
|
+
max_steps=max_steps,
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
def _bind_native_completion(
|
|
560
|
+
checkout: Checkout, config: Mapping[str, Any], completion: Callable[..., Mapping[str, Any]],
|
|
561
|
+
) -> None:
|
|
562
|
+
"""Bind only the original GPT3Generator completion seam in this isolated run."""
|
|
563
|
+
for model in config["models"].values():
|
|
564
|
+
if model["name"] in {"llmqa", "llmqadecomp", "lmgen"} and model.get("gen_model", "gpt3") != "gpt3":
|
|
565
|
+
raise DriverError("CONTROLLED_CONFIG_UNSUPPORTED", "live binding requires native gpt3 participants")
|
|
566
|
+
relative = "src/decomp/models/gpt3generator.py"
|
|
567
|
+
if relative not in checkout.manifest["source_files"]:
|
|
568
|
+
raise DriverError("SOURCE_MISMATCH", "live generator must be source-pinned")
|
|
569
|
+
module = importlib.import_module("decomp.models.gpt3generator")
|
|
570
|
+
if Path(module.__file__).resolve() != _checkout_file(checkout.root, relative):
|
|
571
|
+
raise DriverError("IMPORT_STATE_UNSAFE", "native generator import escaped checkout")
|
|
572
|
+
# The original generator still assembles prompts, parses choices, computes
|
|
573
|
+
# sequence scores and sorts branches. Only its provider call/cache seam is
|
|
574
|
+
# replaced; no participant, parser, or search answer is synthesized here.
|
|
575
|
+
module.openai_call = completion
|
|
576
|
+
module.openai = SimpleNamespace(Completion=SimpleNamespace(create=completion))
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
def _load_manifest(path: Path) -> tuple[Mapping[str, Any], str]:
|
|
580
|
+
try:
|
|
581
|
+
raw = path.read_bytes()
|
|
582
|
+
value = json.loads(raw)
|
|
583
|
+
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
584
|
+
raise DriverError("MANIFEST_INVALID", "compatibility manifest is unavailable") from exc
|
|
585
|
+
if not isinstance(value, Mapping) or value.get("schema_version") != MANIFEST_SCHEMA:
|
|
586
|
+
raise DriverError("MANIFEST_INVALID", "compatibility manifest schema is invalid")
|
|
587
|
+
upstream = value.get("upstream")
|
|
588
|
+
if not isinstance(upstream, Mapping) or upstream.get("commit") != UPSTREAM_COMMIT:
|
|
589
|
+
raise DriverError("MANIFEST_INVALID", "compatibility manifest revision is invalid")
|
|
590
|
+
for field in (
|
|
591
|
+
"source_files",
|
|
592
|
+
"config_artifacts",
|
|
593
|
+
"allowed_model_names",
|
|
594
|
+
"forbidden_model_names",
|
|
595
|
+
"allowed_reader_names",
|
|
596
|
+
"model_call_metric_names",
|
|
597
|
+
"patches",
|
|
598
|
+
):
|
|
599
|
+
if field not in value:
|
|
600
|
+
raise DriverError("MANIFEST_INVALID", f"compatibility manifest lacks {field}")
|
|
601
|
+
return value, _sha256_bytes(raw)
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
def _verify_checkout(root: Path, manifest: Mapping[str, Any]) -> None:
|
|
605
|
+
upstream = manifest["upstream"]
|
|
606
|
+
actual_commit = _read_git_head(root)
|
|
607
|
+
if actual_commit != UPSTREAM_COMMIT:
|
|
608
|
+
raise DriverError("REVISION_MISMATCH", "upstream checkout is at the wrong revision")
|
|
609
|
+
license_path = root / "LICENSE"
|
|
610
|
+
expected_license = upstream.get("license_sha256")
|
|
611
|
+
if upstream.get("license_spdx") != "Apache-2.0" or not isinstance(expected_license, str):
|
|
612
|
+
raise DriverError("MANIFEST_INVALID", "upstream license declaration is invalid")
|
|
613
|
+
if _sha256_file(license_path) != expected_license:
|
|
614
|
+
raise DriverError("LICENSE_MISMATCH", "upstream Apache-2.0 license does not match pin")
|
|
615
|
+
source_files = manifest["source_files"]
|
|
616
|
+
if not isinstance(source_files, Mapping) or not source_files:
|
|
617
|
+
raise DriverError("MANIFEST_INVALID", "source-file pins are invalid")
|
|
618
|
+
for relative, expected_hash in source_files.items():
|
|
619
|
+
path = _checkout_file(root, relative)
|
|
620
|
+
if not isinstance(expected_hash, str) or _sha256_file(path) != expected_hash:
|
|
621
|
+
raise DriverError("SOURCE_MISMATCH", "upstream source does not match manifest")
|
|
622
|
+
if upstream.get("require_clean_checkout") is True:
|
|
623
|
+
_require_clean_git_checkout(root)
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
def _resolve_artifact(checkout: Checkout, artifact_id: str) -> Mapping[str, Any]:
|
|
627
|
+
artifacts = checkout.manifest["config_artifacts"]
|
|
628
|
+
if not isinstance(artifacts, Mapping):
|
|
629
|
+
raise DriverError("MANIFEST_INVALID", "config artifact manifest is invalid")
|
|
630
|
+
artifact = artifacts.get(artifact_id)
|
|
631
|
+
if not isinstance(artifact, Mapping):
|
|
632
|
+
raise DriverError("CONFIG_NOT_ALLOWED", "config artifact ID is not allowlisted")
|
|
633
|
+
path = _checkout_file(checkout.root, artifact.get("path"))
|
|
634
|
+
expected_hash = artifact.get("sha256")
|
|
635
|
+
if not isinstance(expected_hash, str) or _sha256_file(path) != expected_hash:
|
|
636
|
+
raise DriverError("CONFIG_MISMATCH", "config artifact does not match checksum pin")
|
|
637
|
+
ext_vars = artifact.get("ext_vars", {})
|
|
638
|
+
if not isinstance(ext_vars, Mapping) or not all(
|
|
639
|
+
isinstance(key, str) and isinstance(item, str) for key, item in ext_vars.items()
|
|
640
|
+
):
|
|
641
|
+
raise DriverError("MANIFEST_INVALID", "config extVars must be pinned strings")
|
|
642
|
+
return {"path": str(path), "sha256": expected_hash, "ext_vars": dict(ext_vars)}
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
def _load_config(artifact: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
646
|
+
path = Path(artifact["path"])
|
|
647
|
+
try:
|
|
648
|
+
if path.suffix == ".jsonnet":
|
|
649
|
+
try:
|
|
650
|
+
jsonnet = importlib.import_module("_jsonnet")
|
|
651
|
+
except ModuleNotFoundError as exc:
|
|
652
|
+
raise _dependency_error(exc) from exc
|
|
653
|
+
rendered = jsonnet.evaluate_file(
|
|
654
|
+
str(path),
|
|
655
|
+
ext_vars=dict(artifact["ext_vars"]),
|
|
656
|
+
)
|
|
657
|
+
value = json.loads(rendered)
|
|
658
|
+
elif path.suffix == ".json":
|
|
659
|
+
value = json.loads(path.read_text(encoding="utf-8"))
|
|
660
|
+
else:
|
|
661
|
+
raise DriverError("CONFIG_NOT_ALLOWED", "config artifact type is unsupported")
|
|
662
|
+
except DriverError:
|
|
663
|
+
raise
|
|
664
|
+
except (OSError, UnicodeDecodeError, json.JSONDecodeError, RuntimeError) as exc:
|
|
665
|
+
raise DriverError("CONFIG_INVALID", "pinned config could not be loaded") from exc
|
|
666
|
+
if not isinstance(value, Mapping):
|
|
667
|
+
raise DriverError("CONFIG_INVALID", "pinned config root must be an object")
|
|
668
|
+
return value
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
def _validate_and_patch_config(
|
|
672
|
+
config: Mapping[str, Any],
|
|
673
|
+
checkout: Checkout,
|
|
674
|
+
max_steps: int,
|
|
675
|
+
) -> Mapping[str, Any]:
|
|
676
|
+
patched = copy.deepcopy(dict(config))
|
|
677
|
+
models = patched.get("models")
|
|
678
|
+
if not isinstance(models, Mapping) or not models:
|
|
679
|
+
raise DriverError("CONFIG_INVALID", "pinned config requires models")
|
|
680
|
+
allowed = _string_set(checkout.manifest["allowed_model_names"], "allowed model names")
|
|
681
|
+
forbidden = _string_set(checkout.manifest["forbidden_model_names"], "forbidden model names")
|
|
682
|
+
for model_config in models.values():
|
|
683
|
+
if not isinstance(model_config, dict):
|
|
684
|
+
raise DriverError("CONFIG_INVALID", "model config must be an object")
|
|
685
|
+
model_name = model_config.get("name")
|
|
686
|
+
if not isinstance(model_name, str) or model_name in forbidden or model_name not in allowed:
|
|
687
|
+
raise DriverError("UNSAFE_CONFIG", "config requests a forbidden participant model")
|
|
688
|
+
if model_name in {"llmqadecomp", "lmgen", "randgen"}:
|
|
689
|
+
declared = model_config.get("max_steps", max_steps)
|
|
690
|
+
if isinstance(declared, bool) or not isinstance(declared, int) or declared < 1:
|
|
691
|
+
raise DriverError("CONFIG_INVALID", "participant max_steps must be positive")
|
|
692
|
+
model_config["max_steps"] = min(declared, max_steps)
|
|
693
|
+
for field in ("prompt_file", "operations_file", "model_questions_file"):
|
|
694
|
+
if field in model_config:
|
|
695
|
+
_checkout_file(checkout.root, model_config[field])
|
|
696
|
+
reader = patched.get("reader")
|
|
697
|
+
if reader is not None:
|
|
698
|
+
if not isinstance(reader, Mapping):
|
|
699
|
+
raise DriverError("CONFIG_INVALID", "reader config must be an object")
|
|
700
|
+
readers = _string_set(checkout.manifest["allowed_reader_names"], "allowed readers")
|
|
701
|
+
if reader.get("name") not in readers:
|
|
702
|
+
raise DriverError("UNSAFE_CONFIG", "config requests a forbidden dataset reader")
|
|
703
|
+
return patched
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
def _model_call_usage(
|
|
707
|
+
model_map: Mapping[str, Any],
|
|
708
|
+
manifest: Mapping[str, Any],
|
|
709
|
+
) -> tuple[int, Mapping[str, int]]:
|
|
710
|
+
accepted = _string_set(manifest["model_call_metric_names"], "model-call metric names")
|
|
711
|
+
breakdown: dict[str, int] = {}
|
|
712
|
+
total = 0
|
|
713
|
+
for participant_name, participant in model_map.items():
|
|
714
|
+
reporter = getattr(participant, "return_model_calls", None)
|
|
715
|
+
if not callable(reporter):
|
|
716
|
+
raise DriverError("USAGE_ACCOUNTING_UNAVAILABLE", "participant lacks call counters")
|
|
717
|
+
counters = reporter()
|
|
718
|
+
if not isinstance(counters, Mapping):
|
|
719
|
+
raise DriverError("USAGE_ACCOUNTING_UNAVAILABLE", "participant counters are invalid")
|
|
720
|
+
for metric, amount in counters.items():
|
|
721
|
+
if isinstance(amount, bool) or not isinstance(amount, int) or amount < 0:
|
|
722
|
+
raise DriverError("USAGE_ACCOUNTING_UNAVAILABLE", "participant counter is invalid")
|
|
723
|
+
key = f"{participant_name}.{metric}"
|
|
724
|
+
breakdown[key] = amount
|
|
725
|
+
if metric in accepted:
|
|
726
|
+
total += amount
|
|
727
|
+
return total, breakdown
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
def _project_final_state(final_state: Any | None, question: str) -> tuple[str, str]:
|
|
731
|
+
if final_state is None:
|
|
732
|
+
return question, ""
|
|
733
|
+
data = final_state.data
|
|
734
|
+
printable = _last_text(data, "get_printable_reasoning_chain", "")
|
|
735
|
+
reasoning = question if not printable else f"{question}\n{printable}"
|
|
736
|
+
raw_answer = _last_text(data, "get_last_answer", "")
|
|
737
|
+
if not raw_answer:
|
|
738
|
+
return reasoning, ""
|
|
739
|
+
try:
|
|
740
|
+
parsed = json.loads(raw_answer)
|
|
741
|
+
except (TypeError, json.JSONDecodeError):
|
|
742
|
+
return reasoning, raw_answer
|
|
743
|
+
if isinstance(parsed, str):
|
|
744
|
+
return reasoning, parsed
|
|
745
|
+
if isinstance(parsed, list):
|
|
746
|
+
return reasoning, _canonical_json(parsed)
|
|
747
|
+
return reasoning, raw_answer
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
@contextlib.contextmanager
|
|
751
|
+
def _working_directory(path: Path) -> Iterator[None]:
|
|
752
|
+
previous = Path.cwd()
|
|
753
|
+
os.chdir(path)
|
|
754
|
+
try:
|
|
755
|
+
yield
|
|
756
|
+
finally:
|
|
757
|
+
os.chdir(previous)
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
@contextlib.contextmanager
|
|
761
|
+
def _upstream_import_scope(root: Path) -> Iterator[None]:
|
|
762
|
+
existing = {name for name in sys.modules if name == "decomp" or name.startswith("decomp.")}
|
|
763
|
+
if existing:
|
|
764
|
+
raise DriverError(
|
|
765
|
+
"IMPORT_STATE_UNSAFE",
|
|
766
|
+
"a DecomP module was already loaded before worker execution",
|
|
767
|
+
)
|
|
768
|
+
source_root = str(root / "src")
|
|
769
|
+
sys.path.insert(0, source_root)
|
|
770
|
+
importlib.invalidate_caches()
|
|
771
|
+
try:
|
|
772
|
+
yield
|
|
773
|
+
finally:
|
|
774
|
+
sys.path.remove(source_root)
|
|
775
|
+
for name in tuple(sys.modules):
|
|
776
|
+
if name == "decomp" or name.startswith("decomp."):
|
|
777
|
+
del sys.modules[name]
|
|
778
|
+
importlib.invalidate_caches()
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
def _read_git_head(root: Path) -> str:
|
|
782
|
+
git_entry = root / ".git"
|
|
783
|
+
try:
|
|
784
|
+
if git_entry.is_file():
|
|
785
|
+
marker = git_entry.read_text(encoding="utf-8").strip()
|
|
786
|
+
if not marker.startswith("gitdir: "):
|
|
787
|
+
raise DriverError("REVISION_MISMATCH", "checkout gitdir marker is invalid")
|
|
788
|
+
git_dir = (root / marker.removeprefix("gitdir: ")).resolve()
|
|
789
|
+
else:
|
|
790
|
+
git_dir = git_entry.resolve()
|
|
791
|
+
head = (git_dir / "HEAD").read_text(encoding="utf-8").strip()
|
|
792
|
+
if head.startswith("ref: "):
|
|
793
|
+
ref = head.removeprefix("ref: ")
|
|
794
|
+
ref_path = _checkout_file(git_dir, ref)
|
|
795
|
+
head = ref_path.read_text(encoding="utf-8").strip()
|
|
796
|
+
except (OSError, UnicodeDecodeError) as exc:
|
|
797
|
+
raise DriverError("REVISION_MISMATCH", "checkout HEAD is unavailable") from exc
|
|
798
|
+
if len(head) != 40 or any(character not in "0123456789abcdef" for character in head):
|
|
799
|
+
raise DriverError("REVISION_MISMATCH", "checkout HEAD is not a commit digest")
|
|
800
|
+
return head
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
def _require_clean_git_checkout(root: Path) -> None:
|
|
804
|
+
environment = {"PATH": os.environ.get("PATH", "")}
|
|
805
|
+
try:
|
|
806
|
+
result = subprocess.run(
|
|
807
|
+
(
|
|
808
|
+
"git",
|
|
809
|
+
"-C",
|
|
810
|
+
str(root),
|
|
811
|
+
"status",
|
|
812
|
+
"--porcelain=v1",
|
|
813
|
+
"--untracked-files=all",
|
|
814
|
+
),
|
|
815
|
+
check=False,
|
|
816
|
+
capture_output=True,
|
|
817
|
+
text=True,
|
|
818
|
+
timeout=10,
|
|
819
|
+
env=environment,
|
|
820
|
+
)
|
|
821
|
+
except (OSError, subprocess.TimeoutExpired) as exc:
|
|
822
|
+
raise DriverError(
|
|
823
|
+
"GIT_VERIFICATION_FAILED", "git cleanliness check is unavailable"
|
|
824
|
+
) from exc
|
|
825
|
+
if result.returncode != 0:
|
|
826
|
+
raise DriverError("GIT_VERIFICATION_FAILED", "git cleanliness check failed")
|
|
827
|
+
if result.stdout.strip():
|
|
828
|
+
raise DriverError("DIRTY_CHECKOUT", "upstream checkout contains uncommitted files")
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
def _checkout_file(root: Path, relative: object) -> Path:
|
|
832
|
+
if not isinstance(relative, str) or not relative or "\\" in relative or "\x00" in relative:
|
|
833
|
+
raise DriverError("PATH_INVALID", "manifest contains an invalid relative path")
|
|
834
|
+
candidate = (root / relative).resolve()
|
|
835
|
+
try:
|
|
836
|
+
candidate.relative_to(root.resolve())
|
|
837
|
+
except ValueError as exc:
|
|
838
|
+
raise DriverError("PATH_INVALID", "manifest path escapes the checkout") from exc
|
|
839
|
+
if not candidate.is_file():
|
|
840
|
+
raise DriverError("PATH_INVALID", "manifest path is not a file")
|
|
841
|
+
return candidate
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
def _last_text(data: Any, method_name: str, fallback: str) -> str:
|
|
845
|
+
method = getattr(data, method_name, None)
|
|
846
|
+
if not callable(method):
|
|
847
|
+
return fallback
|
|
848
|
+
try:
|
|
849
|
+
value = method()
|
|
850
|
+
except (IndexError, KeyError, ValueError):
|
|
851
|
+
return fallback
|
|
852
|
+
return value if isinstance(value, str) else _canonical_json(value)
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
def _validate_search_state(state: Any) -> None:
|
|
856
|
+
for attribute in ("data", "next", "score"):
|
|
857
|
+
if not hasattr(state, attribute):
|
|
858
|
+
raise DriverError("UPSTREAM_OUTPUT_INVALID", "upstream search state is incomplete")
|
|
859
|
+
_finite_score(state.score)
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
def _finite_score(value: Any) -> float:
|
|
863
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
864
|
+
raise DriverError("UPSTREAM_OUTPUT_INVALID", "upstream search score must be numeric")
|
|
865
|
+
result = float(value)
|
|
866
|
+
if not math.isfinite(result):
|
|
867
|
+
raise DriverError("UPSTREAM_OUTPUT_INVALID", "upstream search score must be finite")
|
|
868
|
+
return result
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
def _command_text(value: Any) -> str:
|
|
872
|
+
if value is None:
|
|
873
|
+
return ""
|
|
874
|
+
if not isinstance(value, str):
|
|
875
|
+
raise DriverError("UPSTREAM_OUTPUT_INVALID", "upstream command must be text")
|
|
876
|
+
return value
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
def _bounded_text(value: Any, name: str, *, limit: int) -> str:
|
|
880
|
+
if not isinstance(value, str) or not value.strip() or len(value) > limit or "\x00" in value:
|
|
881
|
+
raise DriverError("INVALID_REQUEST", f"{name} must be bounded non-empty text")
|
|
882
|
+
return value
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
def _bounded_text_sequence(
|
|
886
|
+
value: Any,
|
|
887
|
+
name: str,
|
|
888
|
+
*,
|
|
889
|
+
item_limit: int,
|
|
890
|
+
) -> tuple[str, ...]:
|
|
891
|
+
if isinstance(value, (str, bytes)) or not isinstance(value, Sequence):
|
|
892
|
+
raise DriverError("INVALID_REQUEST", f"{name} must be a sequence")
|
|
893
|
+
result = tuple(value)
|
|
894
|
+
if len(result) > MAX_CONTEXT_BLOCKS:
|
|
895
|
+
raise DriverError("INVALID_REQUEST", f"{name} exceeds the context block limit")
|
|
896
|
+
if any(
|
|
897
|
+
not isinstance(item, str) or not item or len(item) > item_limit or "\x00" in item
|
|
898
|
+
for item in result
|
|
899
|
+
):
|
|
900
|
+
raise DriverError("INVALID_REQUEST", f"{name} contains invalid text")
|
|
901
|
+
if len(_canonical_json(result).encode("utf-8")) > MAX_REQUEST_BYTES:
|
|
902
|
+
raise DriverError("INVALID_REQUEST", f"{name} exceeds its byte limit")
|
|
903
|
+
return result # type: ignore[return-value]
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
def _bounded_hash(value: Any, name: str) -> str:
|
|
907
|
+
if (
|
|
908
|
+
not isinstance(value, str)
|
|
909
|
+
or len(value) != 64
|
|
910
|
+
or any(character not in "0123456789abcdef" for character in value)
|
|
911
|
+
):
|
|
912
|
+
raise DriverError("INVALID_REQUEST", f"{name} must be a SHA-256 digest")
|
|
913
|
+
return value
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
def _context_hash(paragraph_ids: tuple[str, ...], paras: tuple[str, ...]) -> str:
|
|
917
|
+
return _sha256_bytes(
|
|
918
|
+
_canonical_json({"paragraph_ids": paragraph_ids, "paras": paras}).encode("utf-8")
|
|
919
|
+
)
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
def _reject_secret_fields(value: Mapping[str, Any]) -> None:
|
|
923
|
+
pending: list[Any] = [value]
|
|
924
|
+
while pending:
|
|
925
|
+
current = pending.pop()
|
|
926
|
+
if isinstance(current, Mapping):
|
|
927
|
+
for key, item in current.items():
|
|
928
|
+
if not isinstance(key, str):
|
|
929
|
+
raise DriverError("INVALID_REQUEST", "backend_binding keys must be strings")
|
|
930
|
+
normalized = key.lower().replace("-", "_")
|
|
931
|
+
if normalized in _SECRET_KEY_NAMES or any(
|
|
932
|
+
normalized.endswith(f"_{suffix}") for suffix in _SECRET_KEY_SUFFIXES
|
|
933
|
+
):
|
|
934
|
+
raise DriverError(
|
|
935
|
+
"SECRET_REJECTED",
|
|
936
|
+
"backend_binding must contain identity metadata only",
|
|
937
|
+
)
|
|
938
|
+
pending.append(item)
|
|
939
|
+
elif isinstance(current, Sequence) and not isinstance(current, (str, bytes, bytearray)):
|
|
940
|
+
pending.extend(current)
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
def _string_set(value: Any, label: str) -> frozenset[str]:
|
|
944
|
+
if isinstance(value, (str, bytes)) or not isinstance(value, Sequence):
|
|
945
|
+
raise DriverError("MANIFEST_INVALID", f"{label} must be a string list")
|
|
946
|
+
result = tuple(value)
|
|
947
|
+
if not all(isinstance(item, str) and item for item in result):
|
|
948
|
+
raise DriverError("MANIFEST_INVALID", f"{label} must contain strings")
|
|
949
|
+
return frozenset(result)
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
def _dependency_error(error: ModuleNotFoundError) -> DriverError:
|
|
953
|
+
name = error.name if isinstance(error.name, str) else "optional dependency"
|
|
954
|
+
return DriverError("DEPENDENCY_MISSING", f"required upstream dependency is missing: {name}")
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
def _sha256_file(path: Path) -> str:
|
|
958
|
+
try:
|
|
959
|
+
return _sha256_bytes(path.read_bytes())
|
|
960
|
+
except OSError as exc:
|
|
961
|
+
raise DriverError("SOURCE_MISSING", "pinned upstream file is unavailable") from exc
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
def _sha256_bytes(value: bytes) -> str:
|
|
965
|
+
return hashlib.sha256(value).hexdigest()
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
def _canonical_json(value: Any) -> str:
|
|
969
|
+
return json.dumps(
|
|
970
|
+
value,
|
|
971
|
+
ensure_ascii=False,
|
|
972
|
+
sort_keys=True,
|
|
973
|
+
separators=(",", ":"),
|
|
974
|
+
allow_nan=False,
|
|
975
|
+
)
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
def _error_response(error: DriverError) -> Mapping[str, str]:
|
|
979
|
+
return {
|
|
980
|
+
"status": "error",
|
|
981
|
+
"failure_code": error.code,
|
|
982
|
+
"message": error.public_message,
|
|
983
|
+
"schema_version": REQUEST_SCHEMA,
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
def serve_jsonl(driver: DecompDriver, source: TextIO, sink: TextIO) -> int:
|
|
988
|
+
for line in source:
|
|
989
|
+
if len(line.encode("utf-8")) > MAX_REQUEST_BYTES:
|
|
990
|
+
response: Mapping[str, Any] = _error_response(
|
|
991
|
+
DriverError("REQUEST_TOO_LARGE", "worker request exceeds byte limit")
|
|
992
|
+
)
|
|
993
|
+
else:
|
|
994
|
+
try:
|
|
995
|
+
value = json.loads(line)
|
|
996
|
+
response = driver.run(value)
|
|
997
|
+
except json.JSONDecodeError:
|
|
998
|
+
response = _error_response(DriverError("INVALID_JSON", "request is not valid JSON"))
|
|
999
|
+
except DriverError as exc:
|
|
1000
|
+
response = _error_response(exc)
|
|
1001
|
+
except Exception:
|
|
1002
|
+
response = _error_response(
|
|
1003
|
+
DriverError("WORKER_FAILURE", "worker failed without a safe diagnostic")
|
|
1004
|
+
)
|
|
1005
|
+
sink.write(_canonical_json(response) + "\n")
|
|
1006
|
+
sink.flush()
|
|
1007
|
+
return 0
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
def main() -> int:
|
|
1011
|
+
return serve_jsonl(DecompDriver(), sys.stdin, sys.stdout)
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
if __name__ == "__main__":
|
|
1015
|
+
raise SystemExit(main())
|