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.
Files changed (469) hide show
  1. agentinputlab/__init__.py +28 -0
  2. agentinputlab/algorithms/__init__.py +6 -0
  3. agentinputlab/algorithms/acquire/__init__.py +145 -0
  4. agentinputlab/algorithms/acquire/adaptive_rag/__init__.py +43 -0
  5. agentinputlab/algorithms/acquire/adaptive_rag/replay.py +367 -0
  6. agentinputlab/algorithms/acquire/adaptive_rag/runtime.py +1202 -0
  7. agentinputlab/algorithms/acquire/adaptive_rag_common_support.py +542 -0
  8. agentinputlab/algorithms/acquire/hyde.py +296 -0
  9. agentinputlab/algorithms/acquire/hyde_controlled.py +200 -0
  10. agentinputlab/algorithms/acquire/hyde_external.py +148 -0
  11. agentinputlab/algorithms/acquire/ircot.py +28 -0
  12. agentinputlab/algorithms/acquire/ircot_common_support.py +478 -0
  13. agentinputlab/algorithms/acquire/ircot_native.py +355 -0
  14. agentinputlab/algorithms/acquire/ircot_replay.py +327 -0
  15. agentinputlab/algorithms/acquire/qrag/__init__.py +79 -0
  16. agentinputlab/algorithms/acquire/qrag/bridge.py +828 -0
  17. agentinputlab/algorithms/acquire/qrag/common_support.py +891 -0
  18. agentinputlab/algorithms/acquire/qrag/native_axis.py +71 -0
  19. agentinputlab/algorithms/acquire/qrag/reader_adapter.py +139 -0
  20. agentinputlab/algorithms/acquire/r2ag/__init__.py +57 -0
  21. agentinputlab/algorithms/acquire/r2ag/bridge.py +1312 -0
  22. agentinputlab/algorithms/acquire/r2ag/common_support.py +1029 -0
  23. agentinputlab/algorithms/acquire/r2ag/socket_runtime.py +255 -0
  24. agentinputlab/algorithms/clarify/__init__.py +61 -0
  25. agentinputlab/algorithms/clarify/_structured.py +64 -0
  26. agentinputlab/algorithms/clarify/active_task_disambiguation.py +371 -0
  27. agentinputlab/algorithms/clarify/ask_or_assume/__init__.py +17 -0
  28. agentinputlab/algorithms/clarify/ask_or_assume/monitor.py +410 -0
  29. agentinputlab/algorithms/clarify/future_turns.py +379 -0
  30. agentinputlab/algorithms/clarify/udcs.py +255 -0
  31. agentinputlab/algorithms/clarify/udcs_deterministic.py +196 -0
  32. agentinputlab/algorithms/curate/__init__.py +65 -0
  33. agentinputlab/algorithms/curate/acon/__init__.py +27 -0
  34. agentinputlab/algorithms/curate/acon/common.py +284 -0
  35. agentinputlab/algorithms/curate/acon/common_support.py +136 -0
  36. agentinputlab/algorithms/curate/acon/history_bridge.py +309 -0
  37. agentinputlab/algorithms/curate/acon/observation_bridge.py +290 -0
  38. agentinputlab/algorithms/curate/llmlingua/__init__.py +30 -0
  39. agentinputlab/algorithms/curate/llmlingua/adapters.py +537 -0
  40. agentinputlab/algorithms/curate/llmlingua/budget.py +48 -0
  41. agentinputlab/algorithms/curate/llmlingua/controlled.py +175 -0
  42. agentinputlab/algorithms/curate/llmlingua/multilingual.py +105 -0
  43. agentinputlab/algorithms/curate/llmlingua/span_map.py +167 -0
  44. agentinputlab/algorithms/curate/recomp/__init__.py +47 -0
  45. agentinputlab/algorithms/curate/recomp/external.py +1129 -0
  46. agentinputlab/algorithms/curate/recomp/selective.py +304 -0
  47. agentinputlab/algorithms/decompose/__init__.py +107 -0
  48. agentinputlab/algorithms/decompose/decomp/__init__.py +53 -0
  49. agentinputlab/algorithms/decompose/decomp/common_support.py +478 -0
  50. agentinputlab/algorithms/decompose/decomp/planner_component.py +199 -0
  51. agentinputlab/algorithms/decompose/decomp/worker_bridge.py +416 -0
  52. agentinputlab/algorithms/decompose/llm_compiler/__init__.py +35 -0
  53. agentinputlab/algorithms/decompose/llm_compiler/bridge.py +520 -0
  54. agentinputlab/algorithms/decompose/llm_compiler/common_support.py +504 -0
  55. agentinputlab/algorithms/decompose/plan_and_budget/__init__.py +43 -0
  56. agentinputlab/algorithms/decompose/plan_and_budget/cached_soft_budget.py +362 -0
  57. agentinputlab/algorithms/decompose/plan_and_budget/decomposer_component.py +107 -0
  58. agentinputlab/algorithms/decompose/plan_and_budget/full.py +205 -0
  59. agentinputlab/algorithms/decompose/plan_and_budget/hard_cap.py +246 -0
  60. agentinputlab/algorithms/decompose/plan_and_budget/regenerated_soft_budget.py +448 -0
  61. agentinputlab/algorithms/decompose/plan_and_solve/__init__.py +33 -0
  62. agentinputlab/algorithms/decompose/plan_and_solve/plan_component.py +192 -0
  63. agentinputlab/algorithms/decompose/plan_and_solve/ps_prompt.py +127 -0
  64. agentinputlab/algorithms/decompose/self_ask/__init__.py +27 -0
  65. agentinputlab/algorithms/decompose/self_ask/loop.py +362 -0
  66. agentinputlab/algorithms/decompose/self_ask/planner_component.py +117 -0
  67. agentinputlab/algorithms/reformulate/__init__.py +85 -0
  68. agentinputlab/algorithms/reformulate/infocqr/__init__.py +40 -0
  69. agentinputlab/algorithms/reformulate/infocqr/cache_replay.py +362 -0
  70. agentinputlab/algorithms/reformulate/infocqr/external.py +675 -0
  71. agentinputlab/algorithms/reformulate/infocqr/student.py +189 -0
  72. agentinputlab/algorithms/reformulate/query2doc/__init__.py +42 -0
  73. agentinputlab/algorithms/reformulate/query2doc/controlled_live.py +190 -0
  74. agentinputlab/algorithms/reformulate/query2doc/models.py +95 -0
  75. agentinputlab/algorithms/reformulate/query2doc/official_cached.py +335 -0
  76. agentinputlab/algorithms/reformulate/query2doc/querygym_bridge.py +223 -0
  77. agentinputlab/algorithms/reformulate/query2doc/querygym_runtime.py +326 -0
  78. agentinputlab/algorithms/reformulate/reformer/__init__.py +23 -0
  79. agentinputlab/algorithms/reformulate/reformer/external.py +417 -0
  80. agentinputlab/algorithms/reformulate/rrr.py +696 -0
  81. agentinputlab/annotation/__init__.py +5 -0
  82. agentinputlab/annotation/independent.py +428 -0
  83. agentinputlab/annotation/server.py +233 -0
  84. agentinputlab/annotation/static/app.js +121 -0
  85. agentinputlab/annotation/static/index.html +60 -0
  86. agentinputlab/annotation/static/review.css +2 -0
  87. agentinputlab/annotation/static/review.html +10 -0
  88. agentinputlab/annotation/static/review.js +11 -0
  89. agentinputlab/annotation/static/styles.css +55 -0
  90. agentinputlab/annotation/store.py +444 -0
  91. agentinputlab/api.py +368 -0
  92. agentinputlab/artifacts/__init__.py +15 -0
  93. agentinputlab/artifacts/json_file.py +104 -0
  94. agentinputlab/artifacts/manifest.py +98 -0
  95. agentinputlab/artifacts/store.py +157 -0
  96. agentinputlab/backends/__init__.py +98 -0
  97. agentinputlab/backends/embedding/__init__.py +25 -0
  98. agentinputlab/backends/embedding/base.py +145 -0
  99. agentinputlab/backends/embedding/callable.py +75 -0
  100. agentinputlab/backends/embedding/replay.py +129 -0
  101. agentinputlab/backends/llm/__init__.py +31 -0
  102. agentinputlab/backends/llm/base.py +149 -0
  103. agentinputlab/backends/llm/callable.py +108 -0
  104. agentinputlab/backends/llm/decomp_completion.py +88 -0
  105. agentinputlab/backends/llm/final_content_stop.py +72 -0
  106. agentinputlab/backends/llm/gpt_oss_final_channel.py +84 -0
  107. agentinputlab/backends/llm/openai_compatible.py +332 -0
  108. agentinputlab/backends/llm/transformers.py +171 -0
  109. agentinputlab/backends/retrieval/__init__.py +35 -0
  110. agentinputlab/backends/retrieval/base.py +50 -0
  111. agentinputlab/backends/retrieval/dense.py +777 -0
  112. agentinputlab/backends/retrieval/memory.py +119 -0
  113. agentinputlab/backends/retrieval/sqlite_fts.py +155 -0
  114. agentinputlab/backends/user/__init__.py +15 -0
  115. agentinputlab/backends/user/base.py +66 -0
  116. agentinputlab/backends/user/callable.py +54 -0
  117. agentinputlab/backends/user/replay.py +89 -0
  118. agentinputlab/backends/vector.py +38 -0
  119. agentinputlab/benchmarks/__init__.py +246 -0
  120. agentinputlab/benchmarks/acquisition.py +106 -0
  121. agentinputlab/benchmarks/ale.py +540 -0
  122. agentinputlab/benchmarks/appworld_ul.py +669 -0
  123. agentinputlab/benchmarks/assets.py +226 -0
  124. agentinputlab/benchmarks/base.py +68 -0
  125. agentinputlab/benchmarks/beir_archive.py +235 -0
  126. agentinputlab/benchmarks/beir_sample.py +269 -0
  127. agentinputlab/benchmarks/bfcl_native.py +245 -0
  128. agentinputlab/benchmarks/bfcl_v3.py +607 -0
  129. agentinputlab/benchmarks/cache.py +218 -0
  130. agentinputlab/benchmarks/clariq.py +203 -0
  131. agentinputlab/benchmarks/data/registry.json +321 -0
  132. agentinputlab/benchmarks/data/smoke_assets.json +511 -0
  133. agentinputlab/benchmarks/data/statebench_v0_8_1_task_ids.json +475 -0
  134. agentinputlab/benchmarks/data/toolsandbox_165848b_scenario_names.json +1 -0
  135. agentinputlab/benchmarks/execution.py +1111 -0
  136. agentinputlab/benchmarks/interactive.py +257 -0
  137. agentinputlab/benchmarks/ir_query.py +204 -0
  138. agentinputlab/benchmarks/longbench.py +203 -0
  139. agentinputlab/benchmarks/paired_replay.py +479 -0
  140. agentinputlab/benchmarks/preprocess.py +2313 -0
  141. agentinputlab/benchmarks/production.py +236 -0
  142. agentinputlab/benchmarks/qa.py +963 -0
  143. agentinputlab/benchmarks/registry.py +110 -0
  144. agentinputlab/benchmarks/retrieval.py +459 -0
  145. agentinputlab/benchmarks/runtime.py +509 -0
  146. agentinputlab/benchmarks/statebench.py +938 -0
  147. agentinputlab/benchmarks/tau3.py +725 -0
  148. agentinputlab/benchmarks/toolsandbox.py +626 -0
  149. agentinputlab/benchmarks/trec_dl_sample.py +263 -0
  150. agentinputlab/campaign.py +654 -0
  151. agentinputlab/campaign_runner.py +953 -0
  152. agentinputlab/cli/__init__.py +6 -0
  153. agentinputlab/cli/main.py +933 -0
  154. agentinputlab/connectors/__init__.py +46 -0
  155. agentinputlab/connectors/knowledge/__init__.py +4 -0
  156. agentinputlab/connectors/tools/__init__.py +30 -0
  157. agentinputlab/connectors/tools/base.py +57 -0
  158. agentinputlab/connectors/tools/mcp.py +222 -0
  159. agentinputlab/connectors/tools/memory.py +41 -0
  160. agentinputlab/connectors/tools/openapi.py +695 -0
  161. agentinputlab/connectors/tools/schema.py +102 -0
  162. agentinputlab/contracts/__init__.py +91 -0
  163. agentinputlab/contracts/availability.py +24 -0
  164. agentinputlab/contracts/dependency.py +58 -0
  165. agentinputlab/contracts/enums.py +82 -0
  166. agentinputlab/contracts/errors.py +63 -0
  167. agentinputlab/contracts/evidence.py +43 -0
  168. agentinputlab/contracts/intent.py +110 -0
  169. agentinputlab/contracts/lifecycle.py +164 -0
  170. agentinputlab/contracts/patch.py +167 -0
  171. agentinputlab/contracts/serialization.py +93 -0
  172. agentinputlab/contracts/state.py +127 -0
  173. agentinputlab/contracts/trace.py +59 -0
  174. agentinputlab/contracts/worker.py +42 -0
  175. agentinputlab/core/__init__.py +7 -0
  176. agentinputlab/data/__init__.py +32 -0
  177. agentinputlab/data/checksums.py +9 -0
  178. agentinputlab/data/downloader.py +235 -0
  179. agentinputlab/data/http.py +171 -0
  180. agentinputlab/data/index.py +21 -0
  181. agentinputlab/data/overlap.py +28 -0
  182. agentinputlab/data/preprocess.py +32 -0
  183. agentinputlab/data/storage.py +57 -0
  184. agentinputlab/data/trec_sample.py +427 -0
  185. agentinputlab/evaluation/__init__.py +5 -0
  186. agentinputlab/evaluation/outcomes.py +32 -0
  187. agentinputlab/experiments/__init__.py +278 -0
  188. agentinputlab/experiments/analysis.py +981 -0
  189. agentinputlab/experiments/artifacts.py +687 -0
  190. agentinputlab/experiments/automatic_measurement.py +678 -0
  191. agentinputlab/experiments/auxiliary_matrix.py +203 -0
  192. agentinputlab/experiments/bindings.py +430 -0
  193. agentinputlab/experiments/blueprints.py +147 -0
  194. agentinputlab/experiments/builders/__init__.py +45 -0
  195. agentinputlab/experiments/builders/executable_external_payload.py +423 -0
  196. agentinputlab/experiments/builders/external_process_arena.py +1018 -0
  197. agentinputlab/experiments/builders/registry_intervention.py +463 -0
  198. agentinputlab/experiments/cli.py +158 -0
  199. agentinputlab/experiments/controls.py +340 -0
  200. agentinputlab/experiments/driver_guard.py +205 -0
  201. agentinputlab/experiments/drivers/__init__.py +89 -0
  202. agentinputlab/experiments/drivers/external_arena.py +2908 -0
  203. agentinputlab/experiments/drivers/method_process.py +662 -0
  204. agentinputlab/experiments/drivers/parity_process.py +1484 -0
  205. agentinputlab/experiments/drivers/static_production.py +1574 -0
  206. agentinputlab/experiments/executor.py +448 -0
  207. agentinputlab/experiments/loader.py +605 -0
  208. agentinputlab/experiments/measurement.py +675 -0
  209. agentinputlab/experiments/paper_analysis.py +1818 -0
  210. agentinputlab/experiments/parity_identity.py +605 -0
  211. agentinputlab/experiments/plan.py +277 -0
  212. agentinputlab/experiments/production_builders.py +3739 -0
  213. agentinputlab/experiments/readiness.py +432 -0
  214. agentinputlab/experiments/schema.py +514 -0
  215. agentinputlab/experiments/secrets.py +177 -0
  216. agentinputlab/experiments/stateful_analysis.py +1738 -0
  217. agentinputlab/experiments/transfer_analysis.py +1316 -0
  218. agentinputlab/experiments/unseen_analysis.py +1025 -0
  219. agentinputlab/integrations/__init__.py +1 -0
  220. agentinputlab/integrations/action_space/__init__.py +1 -0
  221. agentinputlab/integrations/external_workers/__init__.py +76 -0
  222. agentinputlab/integrations/external_workers/ace.py +431 -0
  223. agentinputlab/integrations/external_workers/base.py +181 -0
  224. agentinputlab/integrations/external_workers/decomp_live.py +249 -0
  225. agentinputlab/integrations/external_workers/decomp_planner.py +30 -0
  226. agentinputlab/integrations/external_workers/infocqr_student.py +280 -0
  227. agentinputlab/integrations/external_workers/ircot.py +576 -0
  228. agentinputlab/integrations/external_workers/llmcompiler_live.py +360 -0
  229. agentinputlab/integrations/external_workers/longllmlingua.py +162 -0
  230. agentinputlab/integrations/external_workers/method.py +314 -0
  231. agentinputlab/integrations/external_workers/process.py +678 -0
  232. agentinputlab/integrations/external_workers/supervisor.py +1071 -0
  233. agentinputlab/integrations/full_architecture/__init__.py +1 -0
  234. agentinputlab/integrations/full_architecture/hyde.py +212 -0
  235. agentinputlab/integrations/full_architecture/plan_and_budget.py +188 -0
  236. agentinputlab/integrations/live_history.py +222 -0
  237. agentinputlab/integrations/prompt_optimization/__init__.py +1 -0
  238. agentinputlab/integrations/training/__init__.py +1 -0
  239. agentinputlab/metrics/__init__.py +50 -0
  240. agentinputlab/metrics/catalog.py +313 -0
  241. agentinputlab/metrics/harm.py +95 -0
  242. agentinputlab/metrics/paired.py +40 -0
  243. agentinputlab/metrics/qa.py +76 -0
  244. agentinputlab/metrics/text.py +33 -0
  245. agentinputlab/metrics/third_party.py +161 -0
  246. agentinputlab/models/__init__.py +4 -0
  247. agentinputlab/observability/__init__.py +56 -0
  248. agentinputlab/observability/budget.py +316 -0
  249. agentinputlab/observability/instrumentation.py +754 -0
  250. agentinputlab/observability/models.py +403 -0
  251. agentinputlab/observability/monitor.py +380 -0
  252. agentinputlab/optimizers/__init__.py +1 -0
  253. agentinputlab/optimizers/clarify/__init__.py +12 -0
  254. agentinputlab/optimizers/context/__init__.py +22 -0
  255. agentinputlab/optimizers/decompose/__init__.py +64 -0
  256. agentinputlab/optimizers/evidence/__init__.py +22 -0
  257. agentinputlab/optimizers/rewrite/__init__.py +30 -0
  258. agentinputlab/parity/__init__.py +5 -0
  259. agentinputlab/parity/base.py +206 -0
  260. agentinputlab/production_campaign.py +763 -0
  261. agentinputlab/py.typed +1 -0
  262. agentinputlab/recipes/__init__.py +333 -0
  263. agentinputlab/recipes/ace.py +418 -0
  264. agentinputlab/recipes/ace_bfcl.py +273 -0
  265. agentinputlab/recipes/ace_common_support.py +169 -0
  266. agentinputlab/recipes/ace_native_plan.py +322 -0
  267. agentinputlab/recipes/ace_online.py +711 -0
  268. agentinputlab/recipes/ace_retrieval.py +330 -0
  269. agentinputlab/recipes/ace_statebench.py +382 -0
  270. agentinputlab/recipes/ace_tau.py +463 -0
  271. agentinputlab/recipes/ace_toolsandbox.py +432 -0
  272. agentinputlab/recipes/acon_guideline.py +22 -0
  273. agentinputlab/recipes/adaptive_rag.py +822 -0
  274. agentinputlab/recipes/llmlingua2_inference.py +146 -0
  275. agentinputlab/recipes/llmlingua2_training.py +796 -0
  276. agentinputlab/recipes/mce.py +1486 -0
  277. agentinputlab/recipes/mce_final.py +335 -0
  278. agentinputlab/recipes/mce_text_fit.py +499 -0
  279. agentinputlab/recipes/mce_text_only.py +298 -0
  280. agentinputlab/recipes/qrag.py +729 -0
  281. agentinputlab/recipes/qrag_inference.py +364 -0
  282. agentinputlab/recipes/r2ag_features.py +236 -0
  283. agentinputlab/recipes/r2ag_inference.py +503 -0
  284. agentinputlab/recipes/r2ag_training.py +948 -0
  285. agentinputlab/recipes/recomp_abstractive.py +592 -0
  286. agentinputlab/recipes/recomp_extractive.py +525 -0
  287. agentinputlab/recipes/reformer.py +17 -0
  288. agentinputlab/recipes/rqrag.py +1516 -0
  289. agentinputlab/recipes/rqrag_common_support.py +193 -0
  290. agentinputlab/recipes/rqrag_completion.py +147 -0
  291. agentinputlab/recipes/skillopt.py +462 -0
  292. agentinputlab/recipes/skillopt_common_support.py +154 -0
  293. agentinputlab/recipes/skillopt_environment.py +165 -0
  294. agentinputlab/recipes/skillopt_environment_cli.py +5 -0
  295. agentinputlab/recipes/skillopt_retrieval.py +188 -0
  296. agentinputlab/recipes/smartsearch.py +897 -0
  297. agentinputlab/recipes/smartsearch_curriculum.py +283 -0
  298. agentinputlab/recipes/smartsearch_grpo_contracts.py +58 -0
  299. agentinputlab/recipes/smartsearch_grpo_data.py +314 -0
  300. agentinputlab/recipes/smartsearch_grpo_meter.py +324 -0
  301. agentinputlab/recipes/smartsearch_grpo_prefix.py +144 -0
  302. agentinputlab/recipes/smartsearch_grpo_rollout.py +119 -0
  303. agentinputlab/recipes/smartsearch_grpo_tools.py +364 -0
  304. agentinputlab/recipes/smartsearch_grpo_training.py +305 -0
  305. agentinputlab/recipes/smartsearch_grpo_transport.py +248 -0
  306. agentinputlab/recipes/smartsearch_grpo_worker.py +364 -0
  307. agentinputlab/recipes/smartsearch_inference.py +524 -0
  308. agentinputlab/recipes/smartsearch_native_data.py +172 -0
  309. agentinputlab/recipes/smartsearch_policy.py +616 -0
  310. agentinputlab/recipes/smartsearch_policy_worker.py +9 -0
  311. agentinputlab/recipes/smartsearch_sampling.py +232 -0
  312. agentinputlab/recipes/smartsearch_teacher.py +203 -0
  313. agentinputlab/recipes/smartsearch_training.py +265 -0
  314. agentinputlab/registry/__init__.py +46 -0
  315. agentinputlab/registry/beta.py +727 -0
  316. agentinputlab/registry/data/algorithms.json +1959 -0
  317. agentinputlab/registry/data/auxiliary_method_profiles.json +34 -0
  318. agentinputlab/registry/loader.py +100 -0
  319. agentinputlab/registry/result_manifest.py +184 -0
  320. agentinputlab/registry/runtime.py +195 -0
  321. agentinputlab/registry/schema.py +449 -0
  322. agentinputlab/runner/__init__.py +30 -0
  323. agentinputlab/runner/balance.py +248 -0
  324. agentinputlab/runner/cache.py +135 -0
  325. agentinputlab/runner/composite.py +130 -0
  326. agentinputlab/runner/engine.py +285 -0
  327. agentinputlab/runner/runtime_guard.py +268 -0
  328. agentinputlab/runner/worker.py +80 -0
  329. agentinputlab/runtime_bootstrap.py +288 -0
  330. agentinputlab/selectors/__init__.py +196 -0
  331. agentinputlab/selectors/advanced.py +1514 -0
  332. agentinputlab/selectors/artifacts.py +1087 -0
  333. agentinputlab/selectors/base.py +112 -0
  334. agentinputlab/selectors/campaigns.py +1893 -0
  335. agentinputlab/selectors/catalog.py +86 -0
  336. agentinputlab/selectors/discovery.py +1860 -0
  337. agentinputlab/selectors/evaluation.py +1348 -0
  338. agentinputlab/selectors/features.py +383 -0
  339. agentinputlab/selectors/learned.py +502 -0
  340. agentinputlab/selectors/no_op.py +28 -0
  341. agentinputlab/selectors/outcome_table.py +475 -0
  342. agentinputlab/selectors/producer.py +853 -0
  343. agentinputlab/selectors/rules.py +109 -0
  344. agentinputlab/selectors/runtime.py +420 -0
  345. agentinputlab/studies/__init__.py +16 -0
  346. agentinputlab/studies/design.py +165 -0
  347. agentinputlab/studies/paired.py +322 -0
  348. agentinputlab/studio/__init__.py +6 -0
  349. agentinputlab/studio/campaigns.py +819 -0
  350. agentinputlab/studio/engine.py +1408 -0
  351. agentinputlab/studio/model_backends.py +425 -0
  352. agentinputlab/studio/prepared.py +410 -0
  353. agentinputlab/studio/server.py +613 -0
  354. agentinputlab/studio/static/app.js +397 -0
  355. agentinputlab/studio/static/campaigns.html +175 -0
  356. agentinputlab/studio/static/campaigns.js +426 -0
  357. agentinputlab/studio/static/index.html +192 -0
  358. agentinputlab/studio/static/result.html +103 -0
  359. agentinputlab/studio/static/result.js +273 -0
  360. agentinputlab/studio/static/styles.css +467 -0
  361. agentinputlab-0.1.0a1.data/data/share/agentinputlab/CITATION.cff +22 -0
  362. agentinputlab-0.1.0a1.dist-info/METADATA +392 -0
  363. agentinputlab-0.1.0a1.dist-info/RECORD +469 -0
  364. agentinputlab-0.1.0a1.dist-info/WHEEL +5 -0
  365. agentinputlab-0.1.0a1.dist-info/entry_points.txt +6 -0
  366. agentinputlab-0.1.0a1.dist-info/licenses/LICENSE +21 -0
  367. agentinputlab-0.1.0a1.dist-info/licenses/NOTICE +31 -0
  368. agentinputlab-0.1.0a1.dist-info/licenses/THIRD_PARTY_NOTICES.md +94 -0
  369. agentinputlab-0.1.0a1.dist-info/top_level.txt +2 -0
  370. workers/README.md +94 -0
  371. workers/ace/README.md +261 -0
  372. workers/ace/__init__.py +1 -0
  373. workers/ace/compatibility_manifest.json +39 -0
  374. workers/ace/driver.py +1682 -0
  375. workers/acon/README.md +215 -0
  376. workers/acon/__init__.py +5 -0
  377. workers/acon/compatibility_manifest.json +93 -0
  378. workers/acon/driver.py +1859 -0
  379. workers/acon/fitted.py +395 -0
  380. workers/acon/guideline.py +470 -0
  381. workers/acon/guideline_source_manifest.json +20 -0
  382. workers/adaptive_rag/README.md +96 -0
  383. workers/adaptive_rag/__init__.py +15 -0
  384. workers/adaptive_rag/compatibility_manifest.json +61 -0
  385. workers/adaptive_rag/driver.py +1470 -0
  386. workers/adaptive_rag/fixture_runtime.py +163 -0
  387. workers/adaptive_rag/runtime.py +252 -0
  388. workers/ask_or_assume/README.md +124 -0
  389. workers/ask_or_assume/__init__.py +1 -0
  390. workers/ask_or_assume/compatibility_manifest.json +36 -0
  391. workers/ask_or_assume/driver.py +1227 -0
  392. workers/decomp/README.md +133 -0
  393. workers/decomp/UPSTREAM_LICENSE.Apache-2.0.txt +201 -0
  394. workers/decomp/__init__.py +5 -0
  395. workers/decomp/compatibility_manifest.json +205 -0
  396. workers/decomp/driver.py +1015 -0
  397. workers/decomp/live.py +137 -0
  398. workers/decomp/runtime.py +231 -0
  399. workers/infocqr/README.md +106 -0
  400. workers/infocqr/__init__.py +1 -0
  401. workers/infocqr/compatibility_manifest.json +84 -0
  402. workers/infocqr/driver.py +1766 -0
  403. workers/ircot/README.md +167 -0
  404. workers/ircot/__init__.py +7 -0
  405. workers/ircot/compatibility_manifest.json +121 -0
  406. workers/ircot/controlled.py +457 -0
  407. workers/ircot/driver.py +1373 -0
  408. workers/ircot/fixture_bindings.json +39 -0
  409. workers/ircot/fixture_config.json +48 -0
  410. workers/ircot/fixture_prompt.txt +2 -0
  411. workers/ircot/native_qa.py +398 -0
  412. workers/ircot/qa_compatibility_manifest.json +57 -0
  413. workers/ircot/runtime.py +97 -0
  414. workers/llmcompiler/README.md +160 -0
  415. workers/llmcompiler/__init__.py +1 -0
  416. workers/llmcompiler/compatibility_manifest.json +45 -0
  417. workers/llmcompiler/driver.py +1514 -0
  418. workers/llmcompiler/export_prompts.py +74 -0
  419. workers/llmcompiler/live.py +174 -0
  420. workers/llmcompiler/patch_manifest.json +147 -0
  421. workers/mce/README.md +198 -0
  422. workers/mce/__init__.py +29 -0
  423. workers/mce/compatibility_manifest.json +115 -0
  424. workers/mce/driver.py +2585 -0
  425. workers/mce/final_eval.py +173 -0
  426. workers/official_arenas/README.md +25 -0
  427. workers/official_arenas/__init__.py +1 -0
  428. workers/official_arenas/capture_records.py +110 -0
  429. workers/official_arenas/driver.py +619 -0
  430. workers/official_arenas/protocol.py +334 -0
  431. workers/official_arenas/providers/__init__.py +1 -0
  432. workers/official_arenas/providers/_live_history.py +228 -0
  433. workers/official_arenas/providers/_message_compat.py +77 -0
  434. workers/official_arenas/providers/appworld_ul.py +196 -0
  435. workers/official_arenas/providers/fixture.py +112 -0
  436. workers/official_arenas/providers/statebench.py +960 -0
  437. workers/official_arenas/providers/tau3.py +1672 -0
  438. workers/official_arenas/providers/tau_retail.py +586 -0
  439. workers/official_arenas/providers/toolsandbox.py +1321 -0
  440. workers/official_arenas/providers/tooltalk.py +699 -0
  441. workers/official_arenas/retail_capture.py +188 -0
  442. workers/official_arenas/retail_driver.py +275 -0
  443. workers/qrag/README.md +172 -0
  444. workers/qrag/__init__.py +1 -0
  445. workers/qrag/compatibility_manifest.json +64 -0
  446. workers/qrag/driver.py +673 -0
  447. workers/r2ag/README.md +154 -0
  448. workers/r2ag/__init__.py +5 -0
  449. workers/r2ag/compatibility_manifest.json +55 -0
  450. workers/r2ag/driver.py +1287 -0
  451. workers/r2ag/feature_schema.json +8 -0
  452. workers/r2ag/runtime.py +589 -0
  453. workers/recomp/README.md +120 -0
  454. workers/recomp/__init__.py +1 -0
  455. workers/recomp/compatibility_manifest.json +43 -0
  456. workers/recomp/driver.py +1653 -0
  457. workers/reformer/README.md +125 -0
  458. workers/reformer/__init__.py +1 -0
  459. workers/reformer/compatibility_manifest.json +68 -0
  460. workers/reformer/driver.py +1459 -0
  461. workers/reformer/induction.py +457 -0
  462. workers/rrr/README.md +126 -0
  463. workers/rrr/__init__.py +1 -0
  464. workers/rrr/compatibility_manifest.json +57 -0
  465. workers/rrr/driver.py +1133 -0
  466. workers/smartsearch/README.md +331 -0
  467. workers/smartsearch/__init__.py +1 -0
  468. workers/smartsearch/compatibility_manifest.json +41 -0
  469. workers/smartsearch/driver.py +1643 -0
@@ -0,0 +1,1653 @@
1
+ #!/usr/bin/env python3
2
+ """Pinned, local-only JSONL inference worker for carriex/recomp.
3
+
4
+ The worker imports the upstream extractive scoring function directly and uses
5
+ the exact abstractive input/decode path recorded in the pinned summarization
6
+ script. Checkpoints are user-mounted and hash-attested; no model or dataset is
7
+ downloaded, copied, or redistributed by AgentInputLab.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+ import contextlib
14
+ import hashlib
15
+ import importlib
16
+ import importlib.metadata
17
+ import importlib.util
18
+ import io
19
+ import json
20
+ import math
21
+ import os
22
+ import platform
23
+ import re
24
+ import stat
25
+ import subprocess
26
+ import sys
27
+ from collections.abc import Callable, Mapping, Sequence
28
+ from dataclasses import dataclass
29
+ from pathlib import Path, PurePosixPath
30
+ from types import ModuleType
31
+ from typing import Any
32
+
33
+ UPSTREAM_COMMIT = "51d4432151efb3275257a9407dc71d1e5ec6634d"
34
+ EXTRACTIVE_RELEASE = "curate.recomp.extractive"
35
+ ABSTRACTIVE_RELEASE = "curate.recomp.abstractive"
36
+ EXTRACTIVE_PROTOCOL = "agentinputlab.recomp.extractive.v3"
37
+ ABSTRACTIVE_PROTOCOL = "agentinputlab.recomp.abstractive.v3"
38
+ MANIFEST_SCHEMA = "agentinputlab.recomp.compatibility.v1"
39
+ CHECKPOINT_SCHEMA = "agentinputlab.recomp.checkpoint.v1"
40
+ DRIVER_ID = "agentinputlab.recomp.pinned.v1"
41
+ DEFAULT_MANIFEST = Path(__file__).with_name("compatibility_manifest.json")
42
+ DEFAULT_MANIFEST_SHA256 = "33c6773a062e61416b2b040f9239fc7e0b4150b11a14562353ea15338ce255be"
43
+ ABSTRACTIVE_INPUT_TEMPLATE = "Question: {question}\n Document: {passages}\n Summary: "
44
+ ABSTRACTIVE_INPUT_TEMPLATE_SHA256 = hashlib.sha256(
45
+ ABSTRACTIVE_INPUT_TEMPLATE.encode("utf-8")
46
+ ).hexdigest()
47
+ CANDIDATE_UNIT_KIND = "presegmented_sentence"
48
+ MAX_REQUEST_BYTES = 16 * 1024 * 1024
49
+ MAX_TEXT_CHARS = 8 * 1024 * 1024
50
+ MAX_CANDIDATES = 4096
51
+ _HEX64 = re.compile(r"^[0-9a-f]{64}$")
52
+ _DEVICE = re.compile(r"^(cpu|mps|cuda(?::[0-9]+)?)$")
53
+ _CONTAINER_DIGEST = re.compile(r"^sha256:[0-9a-f]{64}$")
54
+ _WEIGHT_SUFFIXES = (".bin", ".pt", ".pth", ".safetensors")
55
+ _COMMON_PAYLOAD_FIELDS = frozenset(
56
+ {
57
+ "phase",
58
+ "preflight_token",
59
+ "checkpoint_license_acknowledged",
60
+ "remaining_budget",
61
+ }
62
+ )
63
+ _TOP_FIELDS = frozenset(
64
+ {
65
+ "method_id",
66
+ "release_id",
67
+ "upstream_commit",
68
+ "state_hash",
69
+ "payload",
70
+ "dependency_fingerprints",
71
+ "minimum_reservation",
72
+ "protocol_version",
73
+ "request_id",
74
+ }
75
+ )
76
+ _EXTRACTIVE_FIELDS = _COMMON_PAYLOAD_FIELDS | frozenset(
77
+ {
78
+ "task_id",
79
+ "query",
80
+ "documents",
81
+ "document_ids",
82
+ "checkpoint_revision",
83
+ "tokenizer_revision",
84
+ "sentence_splitter_revision",
85
+ "asset_manifest_hash",
86
+ "retriever_artifact_hash",
87
+ "input_artifact_hash",
88
+ "candidate_unit_kind",
89
+ "controls",
90
+ "seed",
91
+ }
92
+ )
93
+ _ABSTRACTIVE_FIELDS = _COMMON_PAYLOAD_FIELDS | frozenset(
94
+ {
95
+ "task_id",
96
+ "query",
97
+ "documents",
98
+ "document_ids",
99
+ "passages",
100
+ "passages_hash",
101
+ "checkpoint_revision",
102
+ "tokenizer_revision",
103
+ "input_template_hash",
104
+ "asset_manifest_hash",
105
+ "retriever_artifact_hash",
106
+ "input_artifact_hash",
107
+ "controls",
108
+ "seed",
109
+ }
110
+ )
111
+
112
+
113
+ class DriverError(RuntimeError):
114
+ """Stable, secret-free worker failure."""
115
+
116
+ def __init__(
117
+ self,
118
+ code: str,
119
+ message: str,
120
+ *,
121
+ consumed: bool = False,
122
+ usage: Mapping[str, int] | None = None,
123
+ ) -> None:
124
+ super().__init__(message)
125
+ self.code = code
126
+ self.public_message = message
127
+ self.consumed = consumed
128
+ self.usage = dict(usage) if usage is not None else None
129
+
130
+
131
+ @dataclass(frozen=True, slots=True)
132
+ class Checkout:
133
+ root: Path
134
+ manifest: Mapping[str, Any]
135
+ manifest_sha256: str
136
+ evidence_level: str
137
+
138
+
139
+ @dataclass(frozen=True, slots=True)
140
+ class Checkpoint:
141
+ root: Path
142
+ manifest_sha256: str
143
+ mode: str
144
+ checkpoint_revision: str
145
+ tokenizer_revision: str
146
+ tree_sha256: str
147
+ file_count: int
148
+
149
+
150
+ @dataclass(frozen=True, slots=True)
151
+ class ParsedRequest:
152
+ request_id: str
153
+ protocol_version: str
154
+ mode: str
155
+ payload: Mapping[str, Any]
156
+ retriever_fingerprint: Mapping[str, Any]
157
+ state_hash: str
158
+ phase: str
159
+ preflight_token: str
160
+ remaining_budget: Mapping[str, Any]
161
+
162
+
163
+ ModelLoader = Callable[[str, Path, str], tuple[Any, Any]]
164
+
165
+
166
+ class _DiscardTextIO(io.TextIOBase):
167
+ def write(self, value: str) -> int:
168
+ return len(value)
169
+
170
+ def flush(self) -> None:
171
+ return None
172
+
173
+
174
+ class _CountingTokenizer:
175
+ def __init__(self, tokenizer: Any) -> None:
176
+ self.tokenizer = tokenizer
177
+ self.input_tokens = 0
178
+
179
+ def __call__(self, *args: Any, **kwargs: Any) -> Any:
180
+ encoded = self.tokenizer(*args, **kwargs)
181
+ self.input_tokens += _token_count(encoded)
182
+ return encoded
183
+
184
+ def __getattr__(self, name: str) -> Any:
185
+ return getattr(self.tokenizer, name)
186
+
187
+
188
+ def _sha256_bytes(value: bytes) -> str:
189
+ return hashlib.sha256(value).hexdigest()
190
+
191
+
192
+ def _canonical_json(value: Any) -> str:
193
+ return json.dumps(
194
+ value,
195
+ ensure_ascii=False,
196
+ sort_keys=True,
197
+ separators=(",", ":"),
198
+ allow_nan=False,
199
+ )
200
+
201
+
202
+ def _content_hash(value: Any) -> str:
203
+ return _sha256_bytes(_canonical_json(value).encode("utf-8"))
204
+
205
+
206
+ def _read_bytes(path: Path, *, label: str, limit: int = MAX_REQUEST_BYTES) -> bytes:
207
+ if path.is_symlink() or not path.is_file():
208
+ raise DriverError("ARTIFACT_INVALID", f"{label} must be a regular non-symlink file")
209
+ try:
210
+ if path.stat().st_size > limit:
211
+ raise DriverError("ARTIFACT_INVALID", f"{label} exceeds its byte limit")
212
+ return path.read_bytes()
213
+ except DriverError:
214
+ raise
215
+ except OSError as exc:
216
+ raise DriverError("ARTIFACT_INVALID", f"{label} is unreadable") from exc
217
+
218
+
219
+ def _load_json(path: Path, *, label: str) -> tuple[Mapping[str, Any], str]:
220
+ raw = _read_bytes(path, label=label)
221
+ try:
222
+ value = json.loads(raw)
223
+ except (UnicodeDecodeError, json.JSONDecodeError) as exc:
224
+ raise DriverError("ARTIFACT_INVALID", f"{label} is not valid UTF-8 JSON") from exc
225
+ if not isinstance(value, Mapping):
226
+ raise DriverError("ARTIFACT_INVALID", f"{label} must contain a JSON object")
227
+ return value, _sha256_bytes(raw)
228
+
229
+
230
+ def _canonical_checkpoint_relative(value: Any) -> str:
231
+ if not isinstance(value, str) or not value or "\\" in value:
232
+ raise DriverError("CHECKPOINT_INVALID", "checkpoint paths must be canonical POSIX text")
233
+ pure = PurePosixPath(value)
234
+ if (
235
+ pure.is_absolute()
236
+ or pure.as_posix() != value
237
+ or any(part in {"", ".", ".."} for part in pure.parts)
238
+ ):
239
+ raise DriverError("CHECKPOINT_INVALID", "checkpoint paths must be canonical and relative")
240
+ return value
241
+
242
+
243
+ def _checkpoint_tree(root: Path) -> tuple[tuple[str, Path], ...]:
244
+ """Return the closed regular-file tree, rejecting links and special files."""
245
+
246
+ if root.is_symlink() or not root.is_dir():
247
+ raise DriverError(
248
+ "CHECKPOINT_MISSING",
249
+ "the user-mounted RECOMP checkpoint must be a regular directory, not a symlink",
250
+ )
251
+ rows: list[tuple[str, Path]] = []
252
+ try:
253
+ for current, directories, files in os.walk(root, topdown=True, followlinks=False):
254
+ current_path = Path(current)
255
+ for name in tuple(directories):
256
+ candidate = current_path / name
257
+ mode = candidate.lstat().st_mode
258
+ if stat.S_ISLNK(mode):
259
+ raise DriverError(
260
+ "CHECKPOINT_TREE_NOT_CLOSED",
261
+ "RECOMP checkpoint tree contains a symlink",
262
+ )
263
+ if not stat.S_ISDIR(mode):
264
+ raise DriverError(
265
+ "CHECKPOINT_TREE_NOT_CLOSED",
266
+ "RECOMP checkpoint tree contains a non-directory entry",
267
+ )
268
+ for name in files:
269
+ candidate = current_path / name
270
+ mode = candidate.lstat().st_mode
271
+ if stat.S_ISLNK(mode):
272
+ raise DriverError(
273
+ "CHECKPOINT_TREE_NOT_CLOSED",
274
+ "RECOMP checkpoint tree contains a symlink",
275
+ )
276
+ if not stat.S_ISREG(mode):
277
+ raise DriverError(
278
+ "CHECKPOINT_TREE_NOT_CLOSED",
279
+ "RECOMP checkpoint tree contains a non-regular file",
280
+ )
281
+ relative = candidate.relative_to(root).as_posix()
282
+ _canonical_checkpoint_relative(relative)
283
+ rows.append((relative, candidate))
284
+ except DriverError:
285
+ raise
286
+ except OSError as exc:
287
+ raise DriverError("CHECKPOINT_INVALID", "RECOMP checkpoint tree is unreadable") from exc
288
+ return tuple(sorted(rows, key=lambda item: item[0]))
289
+
290
+
291
+ def _hash_regular_file(path: Path) -> tuple[str, int]:
292
+ if path.is_symlink():
293
+ raise DriverError("CHECKPOINT_TREE_NOT_CLOSED", "checkpoint file became a symlink")
294
+ digest = hashlib.sha256()
295
+ size = 0
296
+ try:
297
+ with path.open("rb") as handle:
298
+ while chunk := handle.read(8 * 1024 * 1024):
299
+ digest.update(chunk)
300
+ size += len(chunk)
301
+ except OSError as exc:
302
+ raise DriverError("CHECKPOINT_INVALID", "checkpoint file is unreadable") from exc
303
+ if path.is_symlink() or not path.is_file():
304
+ raise DriverError("CHECKPOINT_TREE_NOT_CLOSED", "checkpoint file identity changed")
305
+ return digest.hexdigest(), size
306
+
307
+
308
+ def _sha_text(value: Any, *, label: str) -> str:
309
+ if not isinstance(value, str) or not _HEX64.fullmatch(value):
310
+ raise DriverError("INVALID_REQUEST", f"{label} must be a SHA-256 digest")
311
+ return value
312
+
313
+
314
+ def _bounded_text(value: Any, *, label: str, allow_empty: bool = False) -> str:
315
+ if not isinstance(value, str) or (not allow_empty and not value.strip()):
316
+ raise DriverError("INVALID_REQUEST", f"{label} must be text")
317
+ if len(value) > MAX_TEXT_CHARS:
318
+ raise DriverError("INVALID_REQUEST", f"{label} exceeds its character limit")
319
+ return value
320
+
321
+
322
+ def _nonnegative_int(value: Any, *, label: str) -> int:
323
+ if isinstance(value, bool) or not isinstance(value, int) or value < 0:
324
+ raise DriverError("INVALID_REQUEST", f"{label} must be a non-negative integer")
325
+ return value
326
+
327
+
328
+ def _positive_int(value: Any, *, label: str) -> int:
329
+ result = _nonnegative_int(value, label=label)
330
+ if result < 1:
331
+ raise DriverError("INVALID_REQUEST", f"{label} must be positive")
332
+ return result
333
+
334
+
335
+ def _git(root: Path, *arguments: str) -> str:
336
+ environment = {
337
+ "PATH": os.environ.get("PATH", ""),
338
+ "LANG": "C",
339
+ "LC_ALL": "C",
340
+ "GIT_CONFIG_NOSYSTEM": "1",
341
+ "GIT_TERMINAL_PROMPT": "0",
342
+ }
343
+ try:
344
+ result = subprocess.run(
345
+ ("git", "-C", str(root), *arguments),
346
+ check=False,
347
+ capture_output=True,
348
+ text=True,
349
+ timeout=10,
350
+ env=environment,
351
+ )
352
+ except (FileNotFoundError, OSError, subprocess.TimeoutExpired) as exc:
353
+ raise DriverError("CHECKOUT_VERIFICATION_FAILED", "git verification failed") from exc
354
+ if result.returncode != 0:
355
+ raise DriverError("CHECKOUT_VERIFICATION_FAILED", "git verification failed")
356
+ return result.stdout.strip()
357
+
358
+
359
+ def _validate_manifest(manifest: Mapping[str, Any]) -> None:
360
+ expected = {
361
+ "schema_version",
362
+ "evidence_level",
363
+ "upstream",
364
+ "source_files",
365
+ "runtime_contract",
366
+ "legacy_dependencies",
367
+ "artifact_policy",
368
+ }
369
+ if set(manifest) != expected or manifest.get("schema_version") != MANIFEST_SCHEMA:
370
+ raise DriverError("MANIFEST_INVALID", "RECOMP compatibility manifest schema mismatch")
371
+ if manifest.get("evidence_level") not in {
372
+ "pinned_upstream_source",
373
+ "fake_checkout_plumbing",
374
+ }:
375
+ raise DriverError("MANIFEST_INVALID", "RECOMP evidence level is invalid")
376
+ upstream = manifest.get("upstream")
377
+ if not isinstance(upstream, Mapping) or upstream.get("commit") != UPSTREAM_COMMIT:
378
+ raise DriverError("MANIFEST_INVALID", "RECOMP upstream identity mismatch")
379
+ if upstream.get("license_spdx") != "MIT":
380
+ raise DriverError("MANIFEST_INVALID", "RECOMP source license must be MIT")
381
+ _sha_text(upstream.get("license_sha256"), label="upstream.license_sha256")
382
+ source_files = manifest.get("source_files")
383
+ if not isinstance(source_files, Mapping) or not source_files:
384
+ raise DriverError("MANIFEST_INVALID", "RECOMP source hash mapping is empty")
385
+ required_sources = {
386
+ "README.md",
387
+ "requirements.txt",
388
+ "run_extractive_compressor.py",
389
+ "train_hf_summarization_model.py",
390
+ }
391
+ if set(source_files) != required_sources:
392
+ raise DriverError("MANIFEST_INVALID", "RECOMP source hash mapping is incomplete")
393
+ if any(
394
+ not isinstance(path, str) or not _HEX64.fullmatch(str(digest))
395
+ for path, digest in source_files.items()
396
+ ):
397
+ raise DriverError("MANIFEST_INVALID", "RECOMP source hash entry is invalid")
398
+ contract = manifest.get("runtime_contract")
399
+ if not isinstance(contract, Mapping) or contract.get("extractive_symbol") != (
400
+ "run_extractive_compressor.get_contriever_scores"
401
+ ):
402
+ raise DriverError("MANIFEST_INVALID", "RECOMP extractive contract is invalid")
403
+ if (
404
+ contract.get("extractive_top_k") != -1
405
+ or contract.get("candidate_unit_kind") != CANDIDATE_UNIT_KIND
406
+ or contract.get("abstractive_input_template") != ABSTRACTIVE_INPUT_TEMPLATE
407
+ or contract.get("abstractive_decode") != "batch_decode_skip_special_tokens_strip"
408
+ ):
409
+ raise DriverError("MANIFEST_INVALID", "RECOMP runtime contract drifted")
410
+ dependencies = manifest.get("legacy_dependencies")
411
+ if not isinstance(dependencies, Mapping) or set(dependencies) != {
412
+ "required_versions",
413
+ "extractive_unpinned_imports",
414
+ }:
415
+ raise DriverError("MANIFEST_INVALID", "RECOMP dependency contract is invalid")
416
+ versions = dependencies.get("required_versions")
417
+ if not isinstance(versions, Mapping):
418
+ raise DriverError("MANIFEST_INVALID", "RECOMP version mapping is invalid")
419
+ for name, version in versions.items():
420
+ if not isinstance(name, str) or not isinstance(version, str) or not version:
421
+ raise DriverError("MANIFEST_INVALID", "RECOMP version entry is invalid")
422
+ unpinned = dependencies.get("extractive_unpinned_imports")
423
+ if not isinstance(unpinned, Sequence) or isinstance(unpinned, (str, bytes)):
424
+ raise DriverError("MANIFEST_INVALID", "RECOMP unpinned import list is invalid")
425
+ if not all(isinstance(name, str) and name for name in unpinned):
426
+ raise DriverError("MANIFEST_INVALID", "RECOMP unpinned import entry is invalid")
427
+ policy = manifest.get("artifact_policy")
428
+ if not isinstance(policy, Mapping) or policy != {
429
+ "checkpoint_license": "NOASSERTION",
430
+ "bundle_checkpoint": False,
431
+ "bundle_training_data": False,
432
+ "local_files_only": True,
433
+ "trust_remote_code": False,
434
+ }:
435
+ raise DriverError("MANIFEST_INVALID", "RECOMP artifact policy drifted")
436
+
437
+
438
+ def verify_checkout(
439
+ root: Path,
440
+ *,
441
+ manifest_path: Path = DEFAULT_MANIFEST,
442
+ allow_fixture_manifest: bool = False,
443
+ ) -> Checkout:
444
+ manifest, manifest_hash = _load_json(manifest_path, label="RECOMP compatibility manifest")
445
+ if not allow_fixture_manifest and manifest_hash != DEFAULT_MANIFEST_SHA256:
446
+ raise DriverError("WORKER_INTEGRITY_FAILURE", "RECOMP compatibility manifest hash mismatch")
447
+ _validate_manifest(manifest)
448
+ root = root.resolve()
449
+ if not root.is_dir():
450
+ raise DriverError("CHECKOUT_MISSING", "the pinned RECOMP checkout is missing")
451
+ evidence = str(manifest["evidence_level"])
452
+ if evidence == "pinned_upstream_source":
453
+ if _git(root, "rev-parse", "HEAD") != UPSTREAM_COMMIT:
454
+ raise DriverError("CHECKOUT_REVISION_MISMATCH", "RECOMP checkout revision mismatch")
455
+ if _git(root, "status", "--porcelain=v1", "--untracked-files=all"):
456
+ raise DriverError("CHECKOUT_DIRTY", "RECOMP checkout must be clean")
457
+ else:
458
+ marker = _read_bytes(root / ".git" / "HEAD", label="fixture checkout marker")
459
+ if marker.decode("utf-8").strip() != UPSTREAM_COMMIT:
460
+ raise DriverError("CHECKOUT_REVISION_MISMATCH", "fixture checkout revision mismatch")
461
+ upstream = manifest["upstream"]
462
+ assert isinstance(upstream, Mapping)
463
+ if (
464
+ _sha256_bytes(_read_bytes(root / "LICENSE", label="upstream LICENSE"))
465
+ != upstream["license_sha256"]
466
+ ):
467
+ raise DriverError("LICENSE_MISMATCH", "RECOMP MIT license hash mismatch")
468
+ source_files = manifest["source_files"]
469
+ assert isinstance(source_files, Mapping)
470
+ for relative, expected in source_files.items():
471
+ candidate = root / str(relative)
472
+ try:
473
+ candidate.resolve().relative_to(root)
474
+ except ValueError as exc:
475
+ raise DriverError("MANIFEST_INVALID", "RECOMP source path escapes checkout") from exc
476
+ if _sha256_bytes(_read_bytes(candidate, label=f"upstream source {relative}")) != expected:
477
+ raise DriverError("SOURCE_MISMATCH", f"RECOMP source mismatch: {relative}")
478
+ return Checkout(
479
+ root=root,
480
+ manifest=manifest,
481
+ manifest_sha256=manifest_hash,
482
+ evidence_level=evidence,
483
+ )
484
+
485
+
486
+ def verify_checkpoint(root: Path, manifest_path: Path) -> Checkpoint:
487
+ manifest, digest = _load_json(manifest_path, label="RECOMP checkpoint manifest")
488
+ if (
489
+ set(manifest)
490
+ != {
491
+ "schema_version",
492
+ "mode",
493
+ "checkpoint_revision",
494
+ "tokenizer_revision",
495
+ "license",
496
+ "files",
497
+ }
498
+ or manifest.get("schema_version") != CHECKPOINT_SCHEMA
499
+ ):
500
+ raise DriverError("CHECKPOINT_INVALID", "RECOMP checkpoint manifest schema mismatch")
501
+ mode = manifest.get("mode")
502
+ if mode not in {"extractive", "abstractive"}:
503
+ raise DriverError("CHECKPOINT_INVALID", "RECOMP checkpoint mode is invalid")
504
+ if manifest.get("license") != "NOASSERTION":
505
+ raise DriverError("CHECKPOINT_INVALID", "RECOMP checkpoint license must remain NOASSERTION")
506
+ checkpoint_revision = _bounded_text(
507
+ manifest.get("checkpoint_revision"), label="checkpoint_revision"
508
+ )
509
+ tokenizer_revision = _bounded_text(
510
+ manifest.get("tokenizer_revision"), label="tokenizer_revision"
511
+ )
512
+ files = manifest.get("files")
513
+ if not isinstance(files, Mapping) or not files:
514
+ raise DriverError("CHECKPOINT_INVALID", "RECOMP checkpoint file manifest is empty")
515
+ canonical_files: dict[str, str] = {}
516
+ for relative, expected in files.items():
517
+ normalized = _canonical_checkpoint_relative(relative)
518
+ if not _HEX64.fullmatch(str(expected)):
519
+ raise DriverError("CHECKPOINT_INVALID", "RECOMP checkpoint file entry is invalid")
520
+ canonical_files[normalized] = str(expected)
521
+ if not any(relative.lower().endswith(_WEIGHT_SUFFIXES) for relative in canonical_files):
522
+ raise DriverError("CHECKPOINT_INVALID", "RECOMP checkpoint manifest omits model weights")
523
+ if root.is_symlink():
524
+ raise DriverError("CHECKPOINT_TREE_NOT_CLOSED", "checkpoint root must not be a symlink")
525
+ root = root.resolve()
526
+ tree = _checkpoint_tree(root)
527
+ actual_paths = {relative for relative, _ in tree}
528
+ declared_paths = set(canonical_files)
529
+ if actual_paths != declared_paths:
530
+ unlisted = sorted(actual_paths - declared_paths)
531
+ missing = sorted(declared_paths - actual_paths)
532
+ detail = "unlisted regular files" if unlisted else "manifest files missing from tree"
533
+ if unlisted and missing:
534
+ detail = "unlisted and missing checkpoint files"
535
+ raise DriverError(
536
+ "CHECKPOINT_TREE_NOT_CLOSED",
537
+ f"RECOMP checkpoint tree closure failed: {detail}",
538
+ )
539
+ tree_records: list[Mapping[str, Any]] = []
540
+ for relative, candidate in tree:
541
+ actual_hash, size = _hash_regular_file(candidate)
542
+ if actual_hash != canonical_files[relative]:
543
+ raise DriverError("CHECKPOINT_MISMATCH", f"RECOMP checkpoint file mismatch: {relative}")
544
+ tree_records.append({"path": relative, "sha256": actual_hash, "bytes": size})
545
+ return Checkpoint(
546
+ root=root,
547
+ manifest_sha256=digest,
548
+ mode=str(mode),
549
+ checkpoint_revision=checkpoint_revision,
550
+ tokenizer_revision=tokenizer_revision,
551
+ tree_sha256=_content_hash(tree_records),
552
+ file_count=len(tree_records),
553
+ )
554
+
555
+
556
+ def _verify_dependencies(manifest: Mapping[str, Any], *, mode: str, device: str = "") -> None:
557
+ legacy = manifest["legacy_dependencies"]
558
+ assert isinstance(legacy, Mapping)
559
+ versions = legacy["required_versions"]
560
+ assert isinstance(versions, Mapping)
561
+ for name, expected in versions.items():
562
+ try:
563
+ actual = importlib.metadata.version(str(name))
564
+ except importlib.metadata.PackageNotFoundError as exc:
565
+ raise DriverError(
566
+ "LEGACY_DEPENDENCY_MISSING",
567
+ f"RECOMP requires {name}=={expected}",
568
+ ) from exc
569
+ # Official CPU/CUDA 11.7 wheels retain the pinned public version but
570
+ # append a build tag. Keep this allowlist explicit: no newer patch or
571
+ # arbitrary local suffix, and never admit a CPU-only wheel on CUDA.
572
+ official_torch_build = name == "torch" and expected == "1.13.0" and (
573
+ (actual == "1.13.0+cpu" and device == "cpu")
574
+ or (
575
+ actual == "1.13.0+cu117"
576
+ and (device == "cpu" or re.fullmatch(r"cuda(?::[0-9]+)?", device) is not None)
577
+ )
578
+ )
579
+ if actual != expected and not official_torch_build:
580
+ raise DriverError(
581
+ "LEGACY_DEPENDENCY_VERSION_MISMATCH",
582
+ f"RECOMP requires {name}=={expected}",
583
+ )
584
+ if mode == "extractive":
585
+ imports = legacy["extractive_unpinned_imports"]
586
+ assert isinstance(imports, Sequence)
587
+ for name in imports:
588
+ try:
589
+ importlib.import_module(str(name))
590
+ except ModuleNotFoundError as exc:
591
+ raise DriverError(
592
+ "LEGACY_DEPENDENCY_MISSING",
593
+ f"RECOMP extractive script requires the unpinned dependency {name}",
594
+ ) from exc
595
+
596
+
597
+ def _installed_version(name: str) -> str:
598
+ try:
599
+ return importlib.metadata.version(name)
600
+ except importlib.metadata.PackageNotFoundError:
601
+ return "NOT_INSTALLED"
602
+
603
+
604
+ def _runtime_fingerprint(
605
+ manifest: Mapping[str, Any],
606
+ *,
607
+ mode: str,
608
+ device: str,
609
+ container_digest: str,
610
+ custom_loader: bool,
611
+ ) -> Mapping[str, Any]:
612
+ legacy = manifest["legacy_dependencies"]
613
+ assert isinstance(legacy, Mapping)
614
+ names = {"torch", "transformers", "tokenizers", "safetensors"}
615
+ if mode == "extractive":
616
+ imports = legacy["extractive_unpinned_imports"]
617
+ assert isinstance(imports, Sequence)
618
+ names.update(str(name) for name in imports)
619
+ dependencies = {name: _installed_version(name) for name in sorted(names)}
620
+ return {
621
+ "device": device,
622
+ "python_version": platform.python_version(),
623
+ "python_implementation": platform.python_implementation(),
624
+ "dependency_versions": dependencies,
625
+ "container_digest": container_digest,
626
+ "custom_model_loader": custom_loader,
627
+ }
628
+
629
+
630
+ def _remaining_budget(value: Any) -> Mapping[str, Any]:
631
+ if not isinstance(value, Mapping) or set(value) != {
632
+ "model_calls",
633
+ "context_tokens",
634
+ "output_tokens",
635
+ "cost_usd",
636
+ }:
637
+ raise DriverError("INVALID_REQUEST", "RECOMP remaining_budget schema mismatch")
638
+ model_calls = _nonnegative_int(value.get("model_calls"), label="remaining model_calls")
639
+ output_tokens = _nonnegative_int(value.get("output_tokens"), label="remaining output_tokens")
640
+ context_tokens = value.get("context_tokens")
641
+ if context_tokens is not None:
642
+ context_tokens = _nonnegative_int(context_tokens, label="remaining context_tokens")
643
+ cost = value.get("cost_usd")
644
+ if cost is not None and (
645
+ isinstance(cost, bool)
646
+ or not isinstance(cost, (int, float))
647
+ or not math.isfinite(float(cost))
648
+ or float(cost) < 0
649
+ ):
650
+ raise DriverError("INVALID_REQUEST", "remaining cost_usd must be finite and non-negative")
651
+ return {
652
+ "model_calls": model_calls,
653
+ "context_tokens": context_tokens,
654
+ "output_tokens": output_tokens,
655
+ "cost_usd": None if cost is None else float(cost),
656
+ }
657
+
658
+
659
+ def _gate_known_budget(budget: Mapping[str, Any], *, output_tokens_required: int) -> None:
660
+ if budget["model_calls"] < 1:
661
+ raise DriverError("MODEL_BUDGET_EXHAUSTED", "RECOMP model-call budget is exhausted")
662
+ if budget["output_tokens"] < output_tokens_required:
663
+ raise DriverError(
664
+ "OUTPUT_BUDGET_EXHAUSTED",
665
+ "RECOMP output-token budget is below the declared generation maximum",
666
+ )
667
+ if budget["cost_usd"] is not None:
668
+ raise DriverError(
669
+ "COST_BUDGET_UNENFORCEABLE",
670
+ "RECOMP local inference is unpriced; a cost cap must fail closed",
671
+ )
672
+
673
+
674
+ def _gate_context_budget(budget: Mapping[str, Any], *, input_tokens: int) -> None:
675
+ cap = budget["context_tokens"]
676
+ if cap is not None and input_tokens > cap:
677
+ raise DriverError(
678
+ "CONTEXT_BUDGET_EXHAUSTED",
679
+ "RECOMP tokenized input exceeds the remaining context-token budget",
680
+ )
681
+
682
+
683
+ def _documents(value: Any, ids_value: Any) -> tuple[Mapping[str, Any], ...]:
684
+ if isinstance(value, (str, bytes)) or not isinstance(value, Sequence):
685
+ raise DriverError("INVALID_REQUEST", "documents must be a sequence")
686
+ if isinstance(ids_value, (str, bytes)) or not isinstance(ids_value, Sequence):
687
+ raise DriverError("INVALID_REQUEST", "document_ids must be a sequence")
688
+ ids = tuple(ids_value)
689
+ if not all(isinstance(item, str) and item for item in ids):
690
+ raise DriverError("INVALID_REQUEST", "document_ids must contain non-empty strings")
691
+ if not ids or len(ids) > MAX_CANDIDATES or len(ids) != len(set(ids)):
692
+ raise DriverError("INVALID_REQUEST", "document_ids are empty, duplicate, or excessive")
693
+ documents: list[Mapping[str, Any]] = []
694
+ expected_fields = {"text", "block_id", "source", "score", "metadata"}
695
+ for index, item in enumerate(value):
696
+ if not isinstance(item, Mapping) or set(item) != expected_fields:
697
+ raise DriverError("INVALID_REQUEST", f"documents[{index}] has an invalid schema")
698
+ text = _bounded_text(item.get("text"), label=f"documents[{index}].text")
699
+ block_id = _bounded_text(item.get("block_id"), label=f"documents[{index}].block_id")
700
+ if index >= len(ids) or block_id != ids[index]:
701
+ raise DriverError("SOURCE_MAPPING_MISMATCH", "document IDs/order changed")
702
+ if not isinstance(item.get("source"), str) or not isinstance(item.get("metadata"), Mapping):
703
+ raise DriverError("INVALID_REQUEST", "document source/metadata is invalid")
704
+ score = item.get("score")
705
+ if score is not None and (
706
+ isinstance(score, bool)
707
+ or not isinstance(score, (int, float))
708
+ or not math.isfinite(float(score))
709
+ ):
710
+ raise DriverError("INVALID_REQUEST", "document score is invalid")
711
+ documents.append(
712
+ {
713
+ "text": text,
714
+ "block_id": block_id,
715
+ "source": item["source"],
716
+ "score": score,
717
+ "metadata": dict(item["metadata"]),
718
+ }
719
+ )
720
+ if len(documents) != len(ids):
721
+ raise DriverError("SOURCE_MAPPING_MISMATCH", "documents and IDs have different lengths")
722
+ return tuple(documents)
723
+
724
+
725
+ def parse_request(value: Any) -> ParsedRequest:
726
+ if not isinstance(value, Mapping) or set(value) != _TOP_FIELDS:
727
+ raise DriverError("INVALID_REQUEST", "worker request fields do not match schema")
728
+ if value.get("method_id") != "recomp" or value.get("upstream_commit") != UPSTREAM_COMMIT:
729
+ raise DriverError("IDENTITY_MISMATCH", "worker accepts only the pinned RECOMP identity")
730
+ release = value.get("release_id")
731
+ protocol = value.get("protocol_version")
732
+ if release == EXTRACTIVE_RELEASE and protocol == EXTRACTIVE_PROTOCOL:
733
+ mode = "extractive"
734
+ fields = _EXTRACTIVE_FIELDS
735
+ elif release == ABSTRACTIVE_RELEASE and protocol == ABSTRACTIVE_PROTOCOL:
736
+ mode = "abstractive"
737
+ fields = _ABSTRACTIVE_FIELDS
738
+ else:
739
+ raise DriverError("IDENTITY_MISMATCH", "unsupported RECOMP release/protocol")
740
+ request_id = _sha_text(value.get("request_id"), label="request_id")
741
+ state_hash = _sha_text(value.get("state_hash"), label="state_hash")
742
+ payload = value.get("payload")
743
+ dependencies = value.get("dependency_fingerprints")
744
+ reservation = value.get("minimum_reservation")
745
+ if not isinstance(payload, Mapping) or set(payload) != fields:
746
+ raise DriverError("INVALID_REQUEST", "RECOMP payload fields do not match schema")
747
+ if (
748
+ not isinstance(dependencies, Mapping)
749
+ or set(dependencies) != {"retriever"}
750
+ or not isinstance(dependencies.get("retriever"), Mapping)
751
+ ):
752
+ raise DriverError("INVALID_REQUEST", "RECOMP requires one retriever fingerprint")
753
+ if not isinstance(reservation, Mapping):
754
+ raise DriverError("INVALID_REQUEST", "RECOMP reservation must be a mapping")
755
+ try:
756
+ expected_id = _content_hash({key: value[key] for key in value if key != "request_id"})
757
+ except (TypeError, ValueError) as exc:
758
+ raise DriverError("INVALID_REQUEST", "RECOMP request is not canonical JSON") from exc
759
+ if request_id != expected_id:
760
+ raise DriverError("REQUEST_ID_MISMATCH", "RECOMP request_id is not canonical")
761
+ normalized = json.loads(_canonical_json(payload))
762
+ retriever = json.loads(_canonical_json(dependencies["retriever"]))
763
+ if normalized.get("checkpoint_license_acknowledged") is not True:
764
+ raise DriverError(
765
+ "CHECKPOINT_LICENSE_NOT_ACKNOWLEDGED",
766
+ "RECOMP request must acknowledge the NOASSERTION checkpoint license",
767
+ )
768
+ phase = normalized.get("phase")
769
+ preflight_token = normalized.get("preflight_token")
770
+ if phase == "preflight":
771
+ if preflight_token != "" or dict(reservation):
772
+ raise DriverError(
773
+ "INVALID_REQUEST",
774
+ "RECOMP preflight cannot carry a token or reserve a model call",
775
+ )
776
+ elif phase == "execute":
777
+ _sha_text(preflight_token, label="preflight_token")
778
+ if dict(reservation) != {"model_calls": 1}:
779
+ raise DriverError("INVALID_REQUEST", "RECOMP execute must reserve one model call")
780
+ else:
781
+ raise DriverError("INVALID_REQUEST", "RECOMP phase must be preflight or execute")
782
+ budget = _remaining_budget(normalized.get("remaining_budget"))
783
+ query = _bounded_text(normalized.get("query"), label="query")
784
+ _bounded_text(normalized.get("task_id"), label="task_id", allow_empty=True)
785
+ documents = _documents(normalized.get("documents"), normalized.get("document_ids"))
786
+ _bounded_text(normalized.get("checkpoint_revision"), label="checkpoint_revision")
787
+ _bounded_text(normalized.get("tokenizer_revision"), label="tokenizer_revision")
788
+ _sha_text(normalized.get("asset_manifest_hash"), label="asset_manifest_hash")
789
+ _sha_text(normalized.get("retriever_artifact_hash"), label="retriever_artifact_hash")
790
+ _sha_text(normalized.get("input_artifact_hash"), label="input_artifact_hash")
791
+ if normalized["retriever_artifact_hash"] != _content_hash(retriever):
792
+ raise DriverError("DEPENDENCY_MISMATCH", "retriever artifact hash mismatch")
793
+ _nonnegative_int(normalized.get("seed"), label="seed")
794
+ controls = normalized.get("controls")
795
+ if not isinstance(controls, Mapping):
796
+ raise DriverError("INVALID_REQUEST", "controls must be an object")
797
+ if mode == "extractive":
798
+ if normalized.get("candidate_unit_kind") != CANDIDATE_UNIT_KIND:
799
+ raise DriverError("SOURCE_MAPPING_MISMATCH", "candidate units are not fixed sentences")
800
+ if set(controls) != {
801
+ "max_sentences",
802
+ "minimum_score",
803
+ "output_order",
804
+ "assembly_profile",
805
+ "dataset",
806
+ }:
807
+ raise DriverError("INVALID_REQUEST", "extractive controls do not match schema")
808
+ max_sentences = controls.get("max_sentences")
809
+ if (
810
+ isinstance(max_sentences, bool)
811
+ or not isinstance(max_sentences, int)
812
+ or (max_sentences != -1 and max_sentences < 1)
813
+ ):
814
+ raise DriverError("INVALID_REQUEST", "max_sentences must be -1 or positive")
815
+ minimum_score = controls.get("minimum_score")
816
+ if minimum_score is not None and (
817
+ isinstance(minimum_score, bool)
818
+ or not isinstance(minimum_score, (int, float))
819
+ or not math.isfinite(float(minimum_score))
820
+ ):
821
+ raise DriverError("INVALID_REQUEST", "minimum_score must be finite or null")
822
+ if controls.get("output_order") not in {"source_order", "score_descending"}:
823
+ raise DriverError("INVALID_REQUEST", "output_order is invalid")
824
+ profile = controls.get("assembly_profile")
825
+ dataset = controls.get("dataset")
826
+ if profile == "controlled":
827
+ if dataset != "":
828
+ raise DriverError("INVALID_REQUEST", "controlled assembly cannot claim a dataset")
829
+ elif profile == "native_paper_qa":
830
+ expected_top_n = {"nq": 1, "tqa": 1, "hotpotqa": 2}.get(dataset)
831
+ if (
832
+ expected_top_n is None
833
+ or max_sentences != expected_top_n
834
+ or minimum_score is not None
835
+ or controls.get("output_order") != "score_descending"
836
+ ):
837
+ raise DriverError("INVALID_REQUEST", "paper-native top-N controls drifted")
838
+ else:
839
+ raise DriverError("INVALID_REQUEST", "assembly_profile is invalid")
840
+ expected_input_hash = _content_hash(
841
+ {
842
+ "query": query,
843
+ "documents": documents,
844
+ "document_ids": normalized["document_ids"],
845
+ "candidate_unit_kind": CANDIDATE_UNIT_KIND,
846
+ }
847
+ )
848
+ else:
849
+ if set(controls) != {
850
+ "max_source_tokens",
851
+ "max_target_tokens",
852
+ "num_beams",
853
+ "empty_output_policy",
854
+ "input_assembly",
855
+ }:
856
+ raise DriverError("INVALID_REQUEST", "abstractive controls do not match schema")
857
+ _positive_int(controls.get("max_source_tokens"), label="max_source_tokens")
858
+ _positive_int(controls.get("max_target_tokens"), label="max_target_tokens")
859
+ _positive_int(controls.get("num_beams"), label="num_beams")
860
+ if controls.get("empty_output_policy") != "clear_context":
861
+ raise DriverError("INVALID_REQUEST", "RECOMP empty-output policy drifted")
862
+ assembly = controls.get("input_assembly")
863
+ if assembly == "prejoined_official_artifact":
864
+ if len(documents) != 1:
865
+ raise DriverError(
866
+ "SOURCE_MAPPING_MISMATCH",
867
+ "prejoined official passages require exactly one context block",
868
+ )
869
+ expected_passages = str(documents[0]["text"])
870
+ elif assembly == "controlled_concatenate_no_separator":
871
+ expected_passages = "".join(str(item["text"]) for item in documents)
872
+ else:
873
+ raise DriverError("INVALID_REQUEST", "unsupported passages assembly")
874
+ passages = _bounded_text(normalized.get("passages"), label="passages")
875
+ if passages != expected_passages or normalized.get("passages_hash") != _content_hash(
876
+ passages
877
+ ):
878
+ raise DriverError("SOURCE_MAPPING_MISMATCH", "abstractive passages binding mismatch")
879
+ if normalized.get("input_template_hash") != ABSTRACTIVE_INPUT_TEMPLATE_SHA256:
880
+ raise DriverError("SOURCE_MAPPING_MISMATCH", "abstractive input template drifted")
881
+ expected_input_hash = _content_hash(
882
+ {
883
+ "query": query,
884
+ "documents": documents,
885
+ "document_ids": normalized["document_ids"],
886
+ "passages": passages,
887
+ "input_template_hash": ABSTRACTIVE_INPUT_TEMPLATE_SHA256,
888
+ }
889
+ )
890
+ if normalized["input_artifact_hash"] != expected_input_hash:
891
+ raise DriverError("SOURCE_MAPPING_MISMATCH", "RECOMP input artifact hash mismatch")
892
+ return ParsedRequest(
893
+ request_id=request_id,
894
+ protocol_version=str(protocol),
895
+ mode=mode,
896
+ payload=normalized,
897
+ retriever_fingerprint=retriever,
898
+ state_hash=state_hash,
899
+ phase=str(phase),
900
+ preflight_token=str(preflight_token),
901
+ remaining_budget=budget,
902
+ )
903
+
904
+
905
+ def _load_extractive_module(checkout: Checkout) -> ModuleType:
906
+ path = checkout.root / "run_extractive_compressor.py"
907
+ specification = importlib.util.spec_from_file_location(
908
+ f"agentinputlab_recomp_{checkout.manifest_sha256}", path
909
+ )
910
+ if specification is None or specification.loader is None:
911
+ raise DriverError("UPSTREAM_IMPORT_FAILURE", "cannot load RECOMP extractive script")
912
+ module = importlib.util.module_from_spec(specification)
913
+ discarded = _DiscardTextIO()
914
+ previous_dont_write_bytecode = sys.dont_write_bytecode
915
+ sys.dont_write_bytecode = True
916
+ try:
917
+ try:
918
+ with contextlib.redirect_stdout(discarded):
919
+ specification.loader.exec_module(module)
920
+ except ModuleNotFoundError as exc:
921
+ raise DriverError(
922
+ "LEGACY_DEPENDENCY_MISSING",
923
+ f"RECOMP extractive script dependency is unavailable: {exc.name}",
924
+ ) from exc
925
+ except Exception as exc:
926
+ raise DriverError(
927
+ "UPSTREAM_IMPORT_FAILURE",
928
+ f"RECOMP extractive import failed: {type(exc).__name__}",
929
+ ) from exc
930
+ finally:
931
+ sys.dont_write_bytecode = previous_dont_write_bytecode
932
+ location = getattr(module, "__file__", None)
933
+ if not isinstance(location, str) or Path(location).resolve() != path.resolve():
934
+ raise DriverError("UPSTREAM_IMPORT_FAILURE", "RECOMP extractive module origin mismatch")
935
+ if not callable(getattr(module, "get_contriever_scores", None)):
936
+ raise DriverError("UPSTREAM_API_MISMATCH", "official RECOMP scoring API is missing")
937
+ return module
938
+
939
+
940
+ def _default_model_loader(mode: str, checkpoint: Path, device: str) -> tuple[Any, Any]:
941
+ # The source-pinned worker uses legacy Torch/Transformers. Their default bin
942
+ # loader is not a weights-only boundary; never deserialize a mounted pickle
943
+ # merely because its bytes have a checksum. Repacking must happen separately
944
+ # with a modern restricted loader and a retained tensor-equality proof.
945
+ safe_index = checkpoint / "model.safetensors.index.json"
946
+ if not ((checkpoint / "model.safetensors").is_file() or safe_index.is_file()):
947
+ raise DriverError(
948
+ "CHECKPOINT_FORMAT_UNSAFE",
949
+ "legacy RECOMP loading requires safetensors; convert and verify tensor equality outside the worker",
950
+ )
951
+ if any(
952
+ path.is_file() and path.suffix.lower() in {".bin", ".pt", ".pth"}
953
+ for path in checkpoint.rglob("*")
954
+ ):
955
+ raise DriverError(
956
+ "CHECKPOINT_FORMAT_UNSAFE", "legacy worker mounts must exclude pickle weights"
957
+ )
958
+ if safe_index.is_file():
959
+ index, _ = _load_json(safe_index, label="safetensors index")
960
+ shards = index.get("weight_map")
961
+ if not isinstance(shards, Mapping) or not shards:
962
+ raise DriverError("CHECKPOINT_FORMAT_UNSAFE", "safetensors shard mapping is invalid")
963
+ for name in shards.values():
964
+ if (
965
+ not isinstance(name, str)
966
+ or not name.endswith(".safetensors")
967
+ or Path(name).is_absolute()
968
+ or ".." in Path(name).parts
969
+ or not (checkpoint / name).is_file()
970
+ ):
971
+ raise DriverError(
972
+ "CHECKPOINT_FORMAT_UNSAFE", "safetensors index references unsafe weights"
973
+ )
974
+ try:
975
+ transformers = importlib.import_module("transformers")
976
+ except ModuleNotFoundError as exc:
977
+ raise DriverError("LEGACY_DEPENDENCY_MISSING", "transformers is unavailable") from exc
978
+ try:
979
+ # Transformers4.24 predates the use_safetensors keyword. Its actual
980
+ # local loader selects safe weights iff this native availability gate
981
+ # is true; the closed mount above makes pickle fallback impossible.
982
+ if not transformers.utils.is_safetensors_available():
983
+ raise DriverError(
984
+ "LEGACY_DEPENDENCY_MISSING", "install safetensors in the isolated RECOMP worker"
985
+ )
986
+ tokenizer = transformers.AutoTokenizer.from_pretrained(
987
+ str(checkpoint),
988
+ local_files_only=True,
989
+ trust_remote_code=False,
990
+ )
991
+ model_type = (
992
+ transformers.AutoModel if mode == "extractive" else transformers.AutoModelForSeq2SeqLM
993
+ )
994
+ model = model_type.from_pretrained(
995
+ str(checkpoint),
996
+ local_files_only=True,
997
+ trust_remote_code=False,
998
+ )
999
+ model = model.to(device)
1000
+ # This worker performs frozen inference, never SFT/RL. The pinned
1001
+ # extractive function calls the model without a no_grad context;
1002
+ # eval() alone would still retain a gradient graph for its full batch.
1003
+ # Freezing parameters changes neither weights nor scoring/decoding.
1004
+ model.requires_grad_(False)
1005
+ eval_method = getattr(model, "eval", None)
1006
+ if callable(eval_method):
1007
+ eval_method()
1008
+ except DriverError:
1009
+ raise
1010
+ except Exception as exc:
1011
+ raise DriverError(
1012
+ "CHECKPOINT_LOAD_FAILURE",
1013
+ f"RECOMP checkpoint load failed: {type(exc).__name__}",
1014
+ ) from exc
1015
+ return model, tokenizer
1016
+
1017
+
1018
+ def _token_count(encoded: Any) -> int:
1019
+ mask = (
1020
+ encoded.get("attention_mask")
1021
+ if isinstance(encoded, Mapping)
1022
+ else getattr(encoded, "attention_mask", None)
1023
+ )
1024
+ if mask is not None:
1025
+ total = getattr(mask, "sum", lambda: 0)()
1026
+ item = getattr(total, "item", None)
1027
+ return int(item() if callable(item) else total)
1028
+ input_ids = (
1029
+ encoded.get("input_ids")
1030
+ if isinstance(encoded, Mapping)
1031
+ else getattr(encoded, "input_ids", None)
1032
+ )
1033
+ if input_ids is None:
1034
+ raise DriverError("USAGE_ACCOUNTING_FAILURE", "tokenizer omitted input_ids")
1035
+ try:
1036
+ shape = input_ids.shape
1037
+ return int(shape[0] * shape[1])
1038
+ except (AttributeError, IndexError, TypeError):
1039
+ if isinstance(input_ids, Sequence):
1040
+ return sum(len(row) if isinstance(row, Sequence) else 1 for row in input_ids)
1041
+ raise DriverError("USAGE_ACCOUNTING_FAILURE", "cannot count tokenizer input IDs")
1042
+
1043
+
1044
+ def _output_token_count(outputs: Any, pad_token_id: int | None) -> int:
1045
+ try:
1046
+ row = outputs[0]
1047
+ except (KeyError, IndexError, TypeError) as exc:
1048
+ raise DriverError("UPSTREAM_OUTPUT_INVALID", "generation returned no token IDs") from exc
1049
+ values = row.tolist() if callable(getattr(row, "tolist", None)) else list(row)
1050
+ return sum(1 for value in values if pad_token_id is None or value != pad_token_id)
1051
+
1052
+
1053
+ def _usage(*, input_tokens: int, output_tokens: int = 0) -> dict[str, int]:
1054
+ return {
1055
+ "model_calls": 1,
1056
+ "retrieval_calls": 0,
1057
+ "unpriced_model_calls": 1,
1058
+ "input_tokens": input_tokens,
1059
+ "output_tokens": output_tokens,
1060
+ }
1061
+
1062
+
1063
+ def _zero_usage() -> dict[str, int]:
1064
+ return {
1065
+ "model_calls": 0,
1066
+ "retrieval_calls": 0,
1067
+ "unpriced_model_calls": 0,
1068
+ "input_tokens": 0,
1069
+ "output_tokens": 0,
1070
+ }
1071
+
1072
+
1073
+ def _preflight_input_tokens(request: ParsedRequest, tokenizer: Any) -> tuple[int, int]:
1074
+ payload = request.payload
1075
+ controls = payload["controls"]
1076
+ try:
1077
+ if request.mode == "extractive":
1078
+ encoded = tokenizer(
1079
+ [payload["query"]] + [item["text"] for item in payload["documents"]],
1080
+ padding=True,
1081
+ truncation=True,
1082
+ return_tensors="pt",
1083
+ )
1084
+ output_required = 0
1085
+ else:
1086
+ prompt = ABSTRACTIVE_INPUT_TEMPLATE.format(
1087
+ question=payload["query"], passages=payload["passages"]
1088
+ )
1089
+ encoded = tokenizer(
1090
+ [prompt],
1091
+ max_length=controls["max_source_tokens"],
1092
+ padding=False,
1093
+ truncation=True,
1094
+ return_tensors="pt",
1095
+ )
1096
+ output_required = int(controls["max_target_tokens"])
1097
+ input_tokens = _token_count(encoded)
1098
+ except DriverError:
1099
+ raise
1100
+ except Exception as exc:
1101
+ raise DriverError(
1102
+ "TOKEN_PREFLIGHT_FAILURE",
1103
+ f"RECOMP tokenizer preflight failed: {type(exc).__name__}",
1104
+ ) from exc
1105
+ return input_tokens, output_required
1106
+
1107
+
1108
+ def _preflight_token(
1109
+ request: ParsedRequest,
1110
+ *,
1111
+ checkout: Checkout,
1112
+ checkpoint: Checkpoint,
1113
+ runtime_fingerprint_hash: str,
1114
+ input_tokens_required: int,
1115
+ output_tokens_required: int,
1116
+ ) -> str:
1117
+ stable_payload = {
1118
+ key: value
1119
+ for key, value in request.payload.items()
1120
+ if key not in {"phase", "preflight_token"}
1121
+ }
1122
+ return _content_hash(
1123
+ {
1124
+ "state_hash": request.state_hash,
1125
+ "mode": request.mode,
1126
+ "payload": stable_payload,
1127
+ "retriever_fingerprint": request.retriever_fingerprint,
1128
+ "compatibility_manifest_sha256": checkout.manifest_sha256,
1129
+ "checkpoint_manifest_sha256": checkpoint.manifest_sha256,
1130
+ "checkpoint_tree_sha256": checkpoint.tree_sha256,
1131
+ "runtime_fingerprint_hash": runtime_fingerprint_hash,
1132
+ "input_tokens_required": input_tokens_required,
1133
+ "output_tokens_required": output_tokens_required,
1134
+ }
1135
+ )
1136
+
1137
+
1138
+ class _ForwardBudgetGate:
1139
+ """Delegate the official scorer's model call only after its tokenizer is observed."""
1140
+
1141
+ def __init__(
1142
+ self,
1143
+ model: Any,
1144
+ tokenizer: _CountingTokenizer,
1145
+ *,
1146
+ budget: Mapping[str, Any],
1147
+ expected_input_tokens: int,
1148
+ ) -> None:
1149
+ self._model = model
1150
+ self._tokenizer = tokenizer
1151
+ self._budget = budget
1152
+ self._expected_input_tokens = expected_input_tokens
1153
+ self.forward_calls = 0
1154
+
1155
+ def __call__(self, *args: Any, **kwargs: Any) -> Any:
1156
+ if self._tokenizer.input_tokens != self._expected_input_tokens:
1157
+ raise DriverError(
1158
+ "TOKEN_PREFLIGHT_MISMATCH",
1159
+ "RECOMP scorer token count changed after preflight",
1160
+ )
1161
+ _gate_context_budget(self._budget, input_tokens=self._tokenizer.input_tokens)
1162
+ self.forward_calls += 1
1163
+ if self.forward_calls > 1:
1164
+ raise DriverError("MODEL_CALL_BUDGET_EXCEEDED", "RECOMP scorer called the model twice")
1165
+ return self._model(*args, **kwargs)
1166
+
1167
+ def __getattr__(self, name: str) -> Any:
1168
+ return getattr(self._model, name)
1169
+
1170
+
1171
+ @dataclass(slots=True)
1172
+ class RECOMPDriver:
1173
+ checkout_path: Path
1174
+ checkpoint_path: Path
1175
+ checkpoint_manifest_path: Path
1176
+ device: str = "cpu"
1177
+ manifest_path: Path = DEFAULT_MANIFEST
1178
+ model_loader: ModelLoader | None = None
1179
+ allow_fixture_manifest: bool = False
1180
+ checkpoint_license_acknowledged: bool = False
1181
+ container_digest: str = ""
1182
+
1183
+ def __post_init__(self) -> None:
1184
+ if not _DEVICE.fullmatch(self.device):
1185
+ raise DriverError("CONFIGURATION_INVALID", "RECOMP device is invalid")
1186
+ if self.checkpoint_license_acknowledged is not True:
1187
+ raise DriverError(
1188
+ "CHECKPOINT_LICENSE_NOT_ACKNOWLEDGED",
1189
+ "RECOMP worker startup must acknowledge the NOASSERTION checkpoint license",
1190
+ )
1191
+ if self.container_digest and not _CONTAINER_DIGEST.fullmatch(self.container_digest):
1192
+ raise DriverError(
1193
+ "CONFIGURATION_INVALID", "RECOMP container digest must be sha256:<64 hex>"
1194
+ )
1195
+
1196
+ def fingerprint(self) -> Mapping[str, Any]:
1197
+ checkout = verify_checkout(
1198
+ self.checkout_path,
1199
+ manifest_path=self.manifest_path,
1200
+ allow_fixture_manifest=self.allow_fixture_manifest,
1201
+ )
1202
+ checkpoint = verify_checkpoint(self.checkpoint_path, self.checkpoint_manifest_path)
1203
+ runtime = _runtime_fingerprint(
1204
+ checkout.manifest,
1205
+ mode=checkpoint.mode,
1206
+ device=self.device,
1207
+ container_digest=self.container_digest,
1208
+ custom_loader=self.model_loader is not None,
1209
+ )
1210
+ return {
1211
+ "worker_id": DRIVER_ID,
1212
+ "upstream_commit": UPSTREAM_COMMIT,
1213
+ "compatibility_manifest_sha256": checkout.manifest_sha256,
1214
+ "asset_manifest_hash": checkpoint.manifest_sha256,
1215
+ "checkpoint_revision": checkpoint.checkpoint_revision,
1216
+ "tokenizer_revision": checkpoint.tokenizer_revision,
1217
+ "mode": checkpoint.mode,
1218
+ "execution_evidence": checkout.evidence_level,
1219
+ "checkpoint_license": "NOASSERTION",
1220
+ "checkpoint_license_acknowledged_at_startup": True,
1221
+ "checkpoint_tree_sha256": checkpoint.tree_sha256,
1222
+ "checkpoint_file_count": checkpoint.file_count,
1223
+ "runtime_fingerprint": runtime,
1224
+ "runtime_fingerprint_hash": _content_hash(runtime),
1225
+ }
1226
+
1227
+ def call(self, raw_request: Mapping[str, Any]) -> Mapping[str, Any]:
1228
+ request = parse_request(raw_request)
1229
+ checkout = verify_checkout(
1230
+ self.checkout_path,
1231
+ manifest_path=self.manifest_path,
1232
+ allow_fixture_manifest=self.allow_fixture_manifest,
1233
+ )
1234
+ checkpoint = verify_checkpoint(self.checkpoint_path, self.checkpoint_manifest_path)
1235
+ payload = request.payload
1236
+ if checkpoint.mode != request.mode:
1237
+ raise DriverError("DEPENDENCY_MISMATCH", "checkpoint mode does not match release")
1238
+ if (
1239
+ checkpoint.manifest_sha256 != payload["asset_manifest_hash"]
1240
+ or checkpoint.checkpoint_revision != payload["checkpoint_revision"]
1241
+ or checkpoint.tokenizer_revision != payload["tokenizer_revision"]
1242
+ ):
1243
+ raise DriverError("DEPENDENCY_MISMATCH", "checkpoint identity does not match request")
1244
+ output_required = (
1245
+ int(payload["controls"]["max_target_tokens"]) if request.mode == "abstractive" else 0
1246
+ )
1247
+ _gate_known_budget(request.remaining_budget, output_tokens_required=output_required)
1248
+ loader = self.model_loader
1249
+ if loader is None:
1250
+ _verify_dependencies(checkout.manifest, mode=request.mode, device=self.device)
1251
+ loader = _default_model_loader
1252
+ runtime_fingerprint = _runtime_fingerprint(
1253
+ checkout.manifest,
1254
+ mode=request.mode,
1255
+ device=self.device,
1256
+ container_digest=self.container_digest,
1257
+ custom_loader=self.model_loader is not None,
1258
+ )
1259
+ runtime_fingerprint_hash = _content_hash(runtime_fingerprint)
1260
+ try:
1261
+ model, tokenizer = loader(request.mode, checkpoint.root, self.device)
1262
+ except DriverError:
1263
+ raise
1264
+ except Exception as exc:
1265
+ raise DriverError(
1266
+ "CHECKPOINT_LOAD_FAILURE",
1267
+ f"RECOMP model loader failed: {type(exc).__name__}",
1268
+ ) from exc
1269
+ input_required, preflight_output_required = _preflight_input_tokens(request, tokenizer)
1270
+ if preflight_output_required != output_required:
1271
+ raise DriverError("WORKER_INTEGRITY_FAILURE", "RECOMP output reservation drifted")
1272
+ _gate_context_budget(request.remaining_budget, input_tokens=input_required)
1273
+ decision_token = _preflight_token(
1274
+ request,
1275
+ checkout=checkout,
1276
+ checkpoint=checkpoint,
1277
+ runtime_fingerprint_hash=runtime_fingerprint_hash,
1278
+ input_tokens_required=input_required,
1279
+ output_tokens_required=output_required,
1280
+ )
1281
+ common_payload = {
1282
+ "checkpoint_revision": checkpoint.checkpoint_revision,
1283
+ "tokenizer_revision": checkpoint.tokenizer_revision,
1284
+ "asset_manifest_hash": checkpoint.manifest_sha256,
1285
+ "checkpoint_tree_sha256": checkpoint.tree_sha256,
1286
+ "checkpoint_file_count": checkpoint.file_count,
1287
+ "checkpoint_license": "NOASSERTION",
1288
+ "checkpoint_license_acknowledged": True,
1289
+ "source_document_ids": list(payload["document_ids"]),
1290
+ "retriever_artifact_hash": payload["retriever_artifact_hash"],
1291
+ "input_artifact_hash": payload["input_artifact_hash"],
1292
+ "compatibility_manifest_sha256": checkout.manifest_sha256,
1293
+ "execution_evidence": checkout.evidence_level,
1294
+ "remaining_budget_hash": _content_hash(request.remaining_budget),
1295
+ "input_tokens_required": input_required,
1296
+ "output_tokens_required": output_required,
1297
+ "preflight_token": decision_token,
1298
+ "runtime_fingerprint": runtime_fingerprint,
1299
+ "runtime_fingerprint_hash": runtime_fingerprint_hash,
1300
+ }
1301
+ if request.phase == "preflight":
1302
+ return {
1303
+ "status": "ok",
1304
+ "request_id": request.request_id,
1305
+ "upstream_commit": UPSTREAM_COMMIT,
1306
+ "protocol_version": request.protocol_version,
1307
+ "payload": common_payload,
1308
+ "usage": _zero_usage(),
1309
+ }
1310
+ if request.preflight_token != decision_token:
1311
+ raise DriverError(
1312
+ "PREFLIGHT_TOKEN_MISMATCH",
1313
+ "RECOMP execute request does not match the frozen preflight",
1314
+ )
1315
+ if request.mode == "extractive":
1316
+ result_payload, usage = self._run_extractive(
1317
+ request,
1318
+ checkout,
1319
+ checkpoint,
1320
+ model,
1321
+ tokenizer,
1322
+ expected_input_tokens=input_required,
1323
+ budget=request.remaining_budget,
1324
+ )
1325
+ else:
1326
+ result_payload, usage = self._run_abstractive(
1327
+ request,
1328
+ checkout,
1329
+ checkpoint,
1330
+ model,
1331
+ tokenizer,
1332
+ expected_input_tokens=input_required,
1333
+ budget=request.remaining_budget,
1334
+ )
1335
+ return {
1336
+ "status": "ok",
1337
+ "request_id": request.request_id,
1338
+ "upstream_commit": UPSTREAM_COMMIT,
1339
+ "protocol_version": request.protocol_version,
1340
+ "payload": {
1341
+ **result_payload,
1342
+ **common_payload,
1343
+ },
1344
+ "usage": usage,
1345
+ }
1346
+
1347
+ def _run_extractive(
1348
+ self,
1349
+ request: ParsedRequest,
1350
+ checkout: Checkout,
1351
+ checkpoint: Checkpoint,
1352
+ model: Any,
1353
+ tokenizer: Any,
1354
+ *,
1355
+ expected_input_tokens: int,
1356
+ budget: Mapping[str, Any],
1357
+ ) -> tuple[Mapping[str, Any], Mapping[str, int]]:
1358
+ module = _load_extractive_module(checkout)
1359
+ payload = request.payload
1360
+ documents = payload["documents"]
1361
+ counting = _CountingTokenizer(tokenizer)
1362
+ gated_model = _ForwardBudgetGate(
1363
+ model,
1364
+ counting,
1365
+ budget=budget,
1366
+ expected_input_tokens=expected_input_tokens,
1367
+ )
1368
+ data_row = {
1369
+ "query": payload["query"],
1370
+ "retrieved_docs": [{"text": item["text"]} for item in documents],
1371
+ }
1372
+ discarded = _DiscardTextIO()
1373
+ try:
1374
+ with contextlib.redirect_stdout(discarded):
1375
+ scores = module.get_contriever_scores(
1376
+ gated_model,
1377
+ counting,
1378
+ data_row,
1379
+ self.device,
1380
+ top_k=-1,
1381
+ )
1382
+ except DriverError:
1383
+ raise
1384
+ except Exception as exc:
1385
+ raise DriverError(
1386
+ "UPSTREAM_RUNTIME_FAILURE",
1387
+ f"official RECOMP scorer failed: {type(exc).__name__}",
1388
+ consumed=True,
1389
+ usage=_usage(input_tokens=counting.input_tokens),
1390
+ ) from exc
1391
+ if counting.input_tokens != expected_input_tokens:
1392
+ raise DriverError(
1393
+ "TOKEN_PREFLIGHT_MISMATCH",
1394
+ "RECOMP scorer token count changed after preflight",
1395
+ consumed=gated_model.forward_calls > 0,
1396
+ usage=(
1397
+ _usage(input_tokens=counting.input_tokens)
1398
+ if gated_model.forward_calls > 0
1399
+ else None
1400
+ ),
1401
+ )
1402
+ _gate_context_budget(budget, input_tokens=counting.input_tokens)
1403
+ if gated_model.forward_calls != 1:
1404
+ raise DriverError(
1405
+ "USAGE_ACCOUNTING_MISMATCH",
1406
+ "RECOMP scorer must execute exactly one model forward",
1407
+ )
1408
+ if isinstance(scores, (str, bytes)) or not isinstance(scores, Sequence):
1409
+ raise DriverError("UPSTREAM_OUTPUT_INVALID", "RECOMP scorer returned non-sequence")
1410
+ if len(scores) != len(documents):
1411
+ raise DriverError(
1412
+ "SCORE_LENGTH_MISMATCH",
1413
+ "RECOMP scorer output length differs from fixed candidate units",
1414
+ consumed=True,
1415
+ usage=_usage(input_tokens=counting.input_tokens),
1416
+ )
1417
+ rows: list[dict[str, Any]] = []
1418
+ for index, (document, score) in enumerate(zip(documents, scores, strict=True)):
1419
+ if (
1420
+ isinstance(score, bool)
1421
+ or not isinstance(score, (int, float))
1422
+ or not math.isfinite(float(score))
1423
+ ):
1424
+ raise DriverError("UPSTREAM_OUTPUT_INVALID", "RECOMP scorer emitted invalid score")
1425
+ text = str(document["text"])
1426
+ rows.append(
1427
+ {
1428
+ "sentence_id": document["block_id"],
1429
+ "document_id": document["block_id"],
1430
+ "document_index": index,
1431
+ "sentence_index": 0,
1432
+ "char_start": 0,
1433
+ "char_end": len(text),
1434
+ "text": text,
1435
+ "score": float(score),
1436
+ }
1437
+ )
1438
+ return (
1439
+ {
1440
+ "sentence_splitter_revision": payload["sentence_splitter_revision"],
1441
+ "candidate_unit_kind": CANDIDATE_UNIT_KIND,
1442
+ "sentence_scores": rows,
1443
+ "scoring_api": "run_extractive_compressor.get_contriever_scores(top_k=-1)",
1444
+ },
1445
+ _usage(input_tokens=counting.input_tokens),
1446
+ )
1447
+
1448
+ def _run_abstractive(
1449
+ self,
1450
+ request: ParsedRequest,
1451
+ checkout: Checkout,
1452
+ checkpoint: Checkpoint,
1453
+ model: Any,
1454
+ tokenizer: Any,
1455
+ *,
1456
+ expected_input_tokens: int,
1457
+ budget: Mapping[str, Any],
1458
+ ) -> tuple[Mapping[str, Any], Mapping[str, int]]:
1459
+ del checkout, checkpoint
1460
+ payload = request.payload
1461
+ controls = payload["controls"]
1462
+ prompt = ABSTRACTIVE_INPUT_TEMPLATE.format(
1463
+ question=payload["query"],
1464
+ passages=payload["passages"],
1465
+ )
1466
+ counting = _CountingTokenizer(tokenizer)
1467
+ try:
1468
+ encoded = counting(
1469
+ [prompt],
1470
+ max_length=controls["max_source_tokens"],
1471
+ padding=False,
1472
+ truncation=True,
1473
+ return_tensors="pt",
1474
+ )
1475
+ to_device = getattr(encoded, "to", None)
1476
+ if callable(to_device):
1477
+ encoded = to_device(self.device)
1478
+ if counting.input_tokens != expected_input_tokens:
1479
+ raise DriverError(
1480
+ "TOKEN_PREFLIGHT_MISMATCH",
1481
+ "RECOMP generation token count changed after preflight",
1482
+ )
1483
+ _gate_context_budget(budget, input_tokens=counting.input_tokens)
1484
+ _gate_known_budget(
1485
+ budget,
1486
+ output_tokens_required=int(controls["max_target_tokens"]),
1487
+ )
1488
+ model_inputs = (
1489
+ dict(encoded)
1490
+ if isinstance(encoded, Mapping)
1491
+ else {
1492
+ "input_ids": encoded.input_ids,
1493
+ "attention_mask": encoded.attention_mask,
1494
+ }
1495
+ )
1496
+ outputs = model.generate(
1497
+ **model_inputs,
1498
+ max_length=controls["max_target_tokens"],
1499
+ num_beams=controls["num_beams"],
1500
+ )
1501
+ decoded = tokenizer.batch_decode(
1502
+ outputs,
1503
+ skip_special_tokens=True,
1504
+ clean_up_tokenization_spaces=True,
1505
+ )
1506
+ except DriverError:
1507
+ raise
1508
+ except Exception as exc:
1509
+ raise DriverError(
1510
+ "UPSTREAM_RUNTIME_FAILURE",
1511
+ f"official RECOMP generation path failed: {type(exc).__name__}",
1512
+ consumed=True,
1513
+ usage=_usage(input_tokens=counting.input_tokens),
1514
+ ) from exc
1515
+ if (
1516
+ not isinstance(decoded, Sequence)
1517
+ or isinstance(decoded, (str, bytes))
1518
+ or len(decoded) != 1
1519
+ ):
1520
+ raise DriverError("UPSTREAM_OUTPUT_INVALID", "RECOMP generation returned invalid batch")
1521
+ raw_generation = decoded[0]
1522
+ if not isinstance(raw_generation, str):
1523
+ raise DriverError("UPSTREAM_OUTPUT_INVALID", "RECOMP generation is not text")
1524
+ summary = raw_generation.strip()
1525
+ output_tokens = _output_token_count(outputs, getattr(tokenizer, "pad_token_id", None))
1526
+ return (
1527
+ {
1528
+ "input_template_hash": ABSTRACTIVE_INPUT_TEMPLATE_SHA256,
1529
+ "passages_hash": payload["passages_hash"],
1530
+ "prompt_hash": _sha256_bytes(prompt.encode("utf-8")),
1531
+ "applied": True,
1532
+ "summary": summary,
1533
+ "raw_generation": raw_generation,
1534
+ "selective_augmentation": not summary,
1535
+ "parse_status": "generated_empty" if not summary else "generated_text",
1536
+ "generation_api": "AutoModelForSeq2SeqLM.generate+batch_decode(strip)",
1537
+ },
1538
+ _usage(input_tokens=counting.input_tokens, output_tokens=output_tokens),
1539
+ )
1540
+
1541
+
1542
+ def _failure_response(exc: DriverError, *, request_id: str = "") -> Mapping[str, Any]:
1543
+ result: dict[str, Any] = {
1544
+ "status": "error",
1545
+ "failure_code": exc.code,
1546
+ "message": exc.public_message,
1547
+ "consumed": exc.consumed,
1548
+ }
1549
+ if request_id:
1550
+ result["request_id"] = request_id
1551
+ if exc.usage is not None:
1552
+ result["usage"] = exc.usage
1553
+ return result
1554
+
1555
+
1556
+ def _read_request() -> Any:
1557
+ raw = sys.stdin.buffer.readline(MAX_REQUEST_BYTES + 1)
1558
+ if len(raw) > MAX_REQUEST_BYTES:
1559
+ raise DriverError("REQUEST_TOO_LARGE", "RECOMP request exceeds byte limit")
1560
+ if not raw or sys.stdin.buffer.read(1):
1561
+ raise DriverError("INVALID_REQUEST", "RECOMP worker accepts exactly one JSON line")
1562
+ try:
1563
+ return json.loads(raw)
1564
+ except (UnicodeDecodeError, json.JSONDecodeError) as exc:
1565
+ raise DriverError("INVALID_REQUEST", "RECOMP request is not valid JSON") from exc
1566
+
1567
+
1568
+ def _parser() -> argparse.ArgumentParser:
1569
+ parser = argparse.ArgumentParser(description=__doc__)
1570
+ parser.add_argument("--checkout", type=Path, required=True)
1571
+ parser.add_argument("--checkpoint", type=Path)
1572
+ parser.add_argument("--checkpoint-manifest", type=Path)
1573
+ parser.add_argument("--device", default="cpu")
1574
+ parser.add_argument("--container-digest", default="")
1575
+ parser.add_argument(
1576
+ "--acknowledge-noassertion-checkpoint",
1577
+ action="store_true",
1578
+ help="explicitly acknowledge that mounted checkpoint assets have NOASSERTION license",
1579
+ )
1580
+ parser.add_argument("--smoke-import", choices=("extractive", "abstractive"))
1581
+ return parser
1582
+
1583
+
1584
+ def main(argv: Sequence[str] | None = None) -> int:
1585
+ args = _parser().parse_args(argv)
1586
+ request_id = ""
1587
+ try:
1588
+ checkout = verify_checkout(args.checkout)
1589
+ if args.smoke_import:
1590
+ if not _DEVICE.fullmatch(args.device):
1591
+ raise DriverError("CONFIGURATION_INVALID", "RECOMP device is invalid")
1592
+ if args.container_digest and not _CONTAINER_DIGEST.fullmatch(args.container_digest):
1593
+ raise DriverError(
1594
+ "CONFIGURATION_INVALID", "RECOMP container digest must be sha256:<64 hex>"
1595
+ )
1596
+ _verify_dependencies(checkout.manifest, mode=args.smoke_import, device=args.device)
1597
+ importlib.import_module("torch")
1598
+ importlib.import_module("tokenizers")
1599
+ importlib.import_module("transformers")
1600
+ if args.smoke_import == "extractive":
1601
+ _load_extractive_module(checkout)
1602
+ runtime = _runtime_fingerprint(
1603
+ checkout.manifest,
1604
+ mode=args.smoke_import,
1605
+ device=args.device,
1606
+ container_digest=args.container_digest,
1607
+ custom_loader=False,
1608
+ )
1609
+ response: Mapping[str, Any] = {
1610
+ "status": "ok",
1611
+ "upstream_commit": UPSTREAM_COMMIT,
1612
+ "mode": args.smoke_import,
1613
+ "execution_evidence": checkout.evidence_level,
1614
+ "compatibility_manifest_sha256": checkout.manifest_sha256,
1615
+ "runtime_fingerprint": runtime,
1616
+ "runtime_fingerprint_hash": _content_hash(runtime),
1617
+ }
1618
+ else:
1619
+ if args.checkpoint is None or args.checkpoint_manifest is None:
1620
+ raise DriverError(
1621
+ "CONFIGURATION_MISSING",
1622
+ "normal inference requires --checkpoint and --checkpoint-manifest",
1623
+ )
1624
+ if not args.acknowledge_noassertion_checkpoint:
1625
+ raise DriverError(
1626
+ "CHECKPOINT_LICENSE_NOT_ACKNOWLEDGED",
1627
+ "normal inference requires --acknowledge-noassertion-checkpoint",
1628
+ )
1629
+ raw_request = _read_request()
1630
+ if isinstance(raw_request, Mapping) and isinstance(raw_request.get("request_id"), str):
1631
+ request_id = str(raw_request["request_id"])
1632
+ response = RECOMPDriver(
1633
+ checkout_path=args.checkout,
1634
+ checkpoint_path=args.checkpoint,
1635
+ checkpoint_manifest_path=args.checkpoint_manifest,
1636
+ device=args.device,
1637
+ container_digest=args.container_digest,
1638
+ checkpoint_license_acknowledged=True,
1639
+ ).call(raw_request)
1640
+ except DriverError as exc:
1641
+ response = _failure_response(exc, request_id=request_id)
1642
+ except Exception as exc: # pragma: no cover - last-resort JSON containment
1643
+ response = _failure_response(
1644
+ DriverError("WORKER_INTERNAL_FAILURE", f"RECOMP failed closed: {type(exc).__name__}"),
1645
+ request_id=request_id,
1646
+ )
1647
+ sys.stdout.write(_canonical_json(response) + "\n")
1648
+ sys.stdout.flush()
1649
+ return 0
1650
+
1651
+
1652
+ if __name__ == "__main__":
1653
+ raise SystemExit(main())