harnessix 0.10.0b3__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 (653) hide show
  1. harnessix/__init__.py +3 -0
  2. harnessix/__main__.py +5 -0
  3. harnessix/agent/__init__.py +1 -0
  4. harnessix/agent/approvals.py +365 -0
  5. harnessix/agent/argument_normalization.py +143 -0
  6. harnessix/agent/attempt_accounting.py +60 -0
  7. harnessix/agent/batch_patching.py +191 -0
  8. harnessix/agent/billing.py +47 -0
  9. harnessix/agent/call_repair.py +212 -0
  10. harnessix/agent/cancellation.py +63 -0
  11. harnessix/agent/compaction_input_budget.py +130 -0
  12. harnessix/agent/compaction_reducer.py +275 -0
  13. harnessix/agent/completion_guard.py +177 -0
  14. harnessix/agent/errors.py +116 -0
  15. harnessix/agent/event_compatibility.py +200 -0
  16. harnessix/agent/execution.py +47 -0
  17. harnessix/agent/ids.py +13 -0
  18. harnessix/agent/input_publication.py +28 -0
  19. harnessix/agent/item_reducer.py +442 -0
  20. harnessix/agent/lifecycle.py +224 -0
  21. harnessix/agent/model_history_runtime.py +373 -0
  22. harnessix/agent/model_input_budget.py +275 -0
  23. harnessix/agent/model_text.py +151 -0
  24. harnessix/agent/models.py +900 -0
  25. harnessix/agent/patching.py +103 -0
  26. harnessix/agent/planning.py +289 -0
  27. harnessix/agent/ports.py +211 -0
  28. harnessix/agent/provider_stop.py +51 -0
  29. harnessix/agent/publication.py +225 -0
  30. harnessix/agent/question_events.py +43 -0
  31. harnessix/agent/reducer.py +225 -0
  32. harnessix/agent/reducer_support.py +196 -0
  33. harnessix/agent/runtime.py +3473 -0
  34. harnessix/agent/runtime_configuration.py +86 -0
  35. harnessix/agent/runtime_recovery.py +225 -0
  36. harnessix/agent/runtime_thread_lock.py +61 -0
  37. harnessix/agent/telemetry.py +318 -0
  38. harnessix/agent/tool_call_groups.py +41 -0
  39. harnessix/agent/tool_rejections.py +100 -0
  40. harnessix/agent/truncation_recovery.py +79 -0
  41. harnessix/agent/trusted_action_contracts.py +83 -0
  42. harnessix/agent/trusted_action_reducer.py +46 -0
  43. harnessix/agent/trusted_action_runtime.py +250 -0
  44. harnessix/agent/trusted_action_session.py +307 -0
  45. harnessix/agent/turn_budget_compaction.py +252 -0
  46. harnessix/agent/turn_budget_hint.py +27 -0
  47. harnessix/agent/turn_reducer.py +384 -0
  48. harnessix/agent/usage.py +150 -0
  49. harnessix/agent_cli.py +497 -0
  50. harnessix/app_server/__init__.py +13 -0
  51. harnessix/app_server/artifacts.py +68 -0
  52. harnessix/app_server/command_runtime.py +110 -0
  53. harnessix/app_server/frame_publication.py +118 -0
  54. harnessix/app_server/handshake.py +96 -0
  55. harnessix/app_server/query_runtime.py +118 -0
  56. harnessix/app_server/server.py +299 -0
  57. harnessix/app_server/service.py +487 -0
  58. harnessix/app_server/stdio.py +268 -0
  59. harnessix/artifacts/__init__.py +1 -0
  60. harnessix/artifacts/action_output_store.py +413 -0
  61. harnessix/artifacts/action_review_store.py +202 -0
  62. harnessix/artifacts/batch_diff.py +255 -0
  63. harnessix/artifacts/batch_verify.py +178 -0
  64. harnessix/artifacts/binary_projection.py +28 -0
  65. harnessix/artifacts/contracts.py +137 -0
  66. harnessix/artifacts/persistence.py +73 -0
  67. harnessix/artifacts/ports.py +69 -0
  68. harnessix/artifacts/publication.py +131 -0
  69. harnessix/artifacts/reference.py +145 -0
  70. harnessix/artifacts/sqlite.py +552 -0
  71. harnessix/candidates/__init__.py +1 -0
  72. harnessix/candidates/judge.py +94 -0
  73. harnessix/candidates/selection.py +146 -0
  74. harnessix/candidates/verification.py +208 -0
  75. harnessix/cli.py +126 -0
  76. harnessix/context/__init__.py +78 -0
  77. harnessix/context/compaction.py +459 -0
  78. harnessix/context/compaction_contracts.py +115 -0
  79. harnessix/context/compaction_ledger_contracts.py +163 -0
  80. harnessix/context/compaction_projection.py +45 -0
  81. harnessix/context/compaction_runtime_contracts.py +25 -0
  82. harnessix/context/compaction_window.py +175 -0
  83. harnessix/context/contracts.py +401 -0
  84. harnessix/context/engine.py +234 -0
  85. harnessix/context/ports.py +18 -0
  86. harnessix/context/read_workspace.py +84 -0
  87. harnessix/context/sources.py +839 -0
  88. harnessix/context/tool_result_contracts.py +214 -0
  89. harnessix/context/tool_result_view.py +488 -0
  90. harnessix/context/working_set_view.py +143 -0
  91. harnessix/delivery/__init__.py +1 -0
  92. harnessix/delivery/action_transaction_planning.py +53 -0
  93. harnessix/delivery/contracts.py +314 -0
  94. harnessix/delivery/diff.py +41 -0
  95. harnessix/delivery/diff_content.py +231 -0
  96. harnessix/delivery/file_modes.py +18 -0
  97. harnessix/delivery/filesystem.py +528 -0
  98. harnessix/delivery/git.py +902 -0
  99. harnessix/delivery/git_authentication_control.py +201 -0
  100. harnessix/delivery/git_checkpoint.py +132 -0
  101. harnessix/delivery/git_command.py +222 -0
  102. harnessix/delivery/git_contracts.py +497 -0
  103. harnessix/delivery/git_identity.py +58 -0
  104. harnessix/delivery/git_inventory_contracts.py +576 -0
  105. harnessix/delivery/git_inventory_materials.py +199 -0
  106. harnessix/delivery/git_inventory_wire.py +323 -0
  107. harnessix/delivery/git_material_cas.py +158 -0
  108. harnessix/delivery/git_material_failure.py +262 -0
  109. harnessix/delivery/git_material_input_contracts.py +459 -0
  110. harnessix/delivery/git_material_native.py +435 -0
  111. harnessix/delivery/git_material_native_windows.py +404 -0
  112. harnessix/delivery/git_material_trace2_profile.py +374 -0
  113. harnessix/delivery/git_material_worker.py +354 -0
  114. harnessix/delivery/git_object_material.py +147 -0
  115. harnessix/delivery/git_object_references.py +189 -0
  116. harnessix/delivery/git_push.py +609 -0
  117. harnessix/delivery/git_repository_recipe.py +246 -0
  118. harnessix/delivery/git_source.py +56 -0
  119. harnessix/delivery/git_store.py +438 -0
  120. harnessix/delivery/git_store_genesis.py +103 -0
  121. harnessix/delivery/git_store_schema.py +98 -0
  122. harnessix/delivery/git_store_schema_v2.py +239 -0
  123. harnessix/delivery/git_tree_closure.py +220 -0
  124. harnessix/delivery/git_tree_diff.py +93 -0
  125. harnessix/delivery/git_tree_projection.py +450 -0
  126. harnessix/delivery/git_workspace_snapshot.py +27 -0
  127. harnessix/delivery/path_case.py +156 -0
  128. harnessix/delivery/planner.py +316 -0
  129. harnessix/delivery/planner_v2.py +93 -0
  130. harnessix/delivery/preserved_mode_contracts.py +182 -0
  131. harnessix/delivery/review_jsonl.py +37 -0
  132. harnessix/delivery/rollback_action.py +296 -0
  133. harnessix/delivery/store.py +526 -0
  134. harnessix/delivery/transaction_action_executor.py +153 -0
  135. harnessix/delivery/trusted_action.py +614 -0
  136. harnessix/delivery/trusted_action_contracts.py +379 -0
  137. harnessix/delivery/windows_filesystem.py +244 -0
  138. harnessix/delivery/windows_io.py +29 -0
  139. harnessix/delivery/windows_metadata.py +95 -0
  140. harnessix/delivery/workspace_cas_io.py +80 -0
  141. harnessix/delivery/workspace_patch_compact.py +75 -0
  142. harnessix/delivery/workspace_patch_errors.py +81 -0
  143. harnessix/delivery/workspace_record_codec.py +334 -0
  144. harnessix/delivery/workspace_record_contracts.py +46 -0
  145. harnessix/delivery/workspace_record_v3_contracts.py +18 -0
  146. harnessix/delivery/workspace_store_schema.py +104 -0
  147. harnessix/delivery/workspace_v2_contracts.py +82 -0
  148. harnessix/domain/__init__.py +1 -0
  149. harnessix/domain/artifact_pagination.py +25 -0
  150. harnessix/domain/errors.py +15 -0
  151. harnessix/domain/file_lock.py +31 -0
  152. harnessix/domain/models.py +161 -0
  153. harnessix/domain/public_output_schema.py +255 -0
  154. harnessix/domain/review_text.py +90 -0
  155. harnessix/evals/__init__.py +318 -0
  156. harnessix/evals/_historical_check.py +131 -0
  157. harnessix/evals/campaign.py +177 -0
  158. harnessix/evals/campaign_cli.py +84 -0
  159. harnessix/evals/campaign_contracts.py +260 -0
  160. harnessix/evals/campaign_execution.py +425 -0
  161. harnessix/evals/campaign_execution_contracts.py +161 -0
  162. harnessix/evals/catalog.py +142 -0
  163. harnessix/evals/checks.py +210 -0
  164. harnessix/evals/cli_config.py +33 -0
  165. harnessix/evals/compaction.py +86 -0
  166. harnessix/evals/compaction_contracts.py +131 -0
  167. harnessix/evals/contracts.py +464 -0
  168. harnessix/evals/delivery.py +937 -0
  169. harnessix/evals/delivery_contracts.py +241 -0
  170. harnessix/evals/execution_fs.py +67 -0
  171. harnessix/evals/git_evidence.py +66 -0
  172. harnessix/evals/grader.py +511 -0
  173. harnessix/evals/materializer.py +391 -0
  174. harnessix/evals/provider_suite_cli.py +149 -0
  175. harnessix/evals/provider_suite_contracts.py +197 -0
  176. harnessix/evals/provider_suite_evidence.py +199 -0
  177. harnessix/evals/provider_suite_execution.py +222 -0
  178. harnessix/evals/report.py +383 -0
  179. harnessix/evals/run_state.py +97 -0
  180. harnessix/evals/runner.py +669 -0
  181. harnessix/evals/suite.py +231 -0
  182. harnessix/evals/suite_contracts.py +398 -0
  183. harnessix/evals/suite_execution.py +578 -0
  184. harnessix/evals/suite_execution_contracts.py +178 -0
  185. harnessix/evals/task_pack.py +229 -0
  186. harnessix/evals/task_pack_contracts.py +418 -0
  187. harnessix/evals/task_pack_execution.py +473 -0
  188. harnessix/evals/task_pack_materializer.py +510 -0
  189. harnessix/evals/task_pack_observations.py +125 -0
  190. harnessix/evals/task_pack_publication.py +264 -0
  191. harnessix/evals/task_pack_suite.py +179 -0
  192. harnessix/evals/task_pack_trial.py +631 -0
  193. harnessix/evals/taskpacks/engineering-v1/archives/agents-utils-benchmark.tar +0 -0
  194. harnessix/evals/taskpacks/engineering-v1/archives/langchain-utils-benchmark.tar +0 -0
  195. harnessix/evals/taskpacks/engineering-v1/archives/opencode-utils-benchmark.tar +0 -0
  196. harnessix/evals/taskpacks/engineering-v1/manifest.json +715 -0
  197. harnessix/evals/taskpacks/engineering-v2/archives/agents-utils-benchmark.tar +0 -0
  198. harnessix/evals/taskpacks/engineering-v2/archives/langchain-utils-benchmark.tar +0 -0
  199. harnessix/evals/taskpacks/engineering-v2/archives/opencode-utils-benchmark.tar +0 -0
  200. harnessix/evals/taskpacks/engineering-v2/manifest.json +715 -0
  201. harnessix/evals/taskpacks/v1/archives/javascript-slug.tar +0 -0
  202. harnessix/evals/taskpacks/v1/archives/python-mathbox.tar +0 -0
  203. harnessix/evals/taskpacks/v1/manifest.json +187 -0
  204. harnessix/exec_cli.py +476 -0
  205. harnessix/execution/__init__.py +1 -0
  206. harnessix/execution/contracts.py +242 -0
  207. harnessix/execution/planner.py +346 -0
  208. harnessix/execution/public_tool_contracts.py +176 -0
  209. harnessix/execution/store.py +281 -0
  210. harnessix/execution/versioned_contracts.py +13 -0
  211. harnessix/file_lock.py +7 -0
  212. harnessix/hooks/__init__.py +39 -0
  213. harnessix/hooks/contracts.py +497 -0
  214. harnessix/hooks/runtime.py +330 -0
  215. harnessix/hooks/store.py +406 -0
  216. harnessix/licensing.py +20 -0
  217. harnessix/mcp/__init__.py +49 -0
  218. harnessix/mcp/actions.py +267 -0
  219. harnessix/mcp/contracts.py +173 -0
  220. harnessix/mcp/host_target.py +87 -0
  221. harnessix/mcp/runtime.py +619 -0
  222. harnessix/mcp/schema.py +181 -0
  223. harnessix/mcp/server.py +258 -0
  224. harnessix/mcp/startup.py +59 -0
  225. harnessix/mcp/store.py +384 -0
  226. harnessix/mcp/target_protocol.py +35 -0
  227. harnessix/models/__init__.py +1 -0
  228. harnessix/models/_anthropic_http.py +72 -0
  229. harnessix/models/_anthropic_mapping.py +127 -0
  230. harnessix/models/_anthropic_stream.py +285 -0
  231. harnessix/models/_billing.py +27 -0
  232. harnessix/models/_bounded_http.py +136 -0
  233. harnessix/models/_chat_errors.py +123 -0
  234. harnessix/models/_chat_input_anchor.py +83 -0
  235. harnessix/models/_chat_mapping.py +180 -0
  236. harnessix/models/_chat_stream.py +325 -0
  237. harnessix/models/_history.py +196 -0
  238. harnessix/models/_json.py +69 -0
  239. harnessix/models/_provider_io.py +80 -0
  240. harnessix/models/_tool_schema.py +63 -0
  241. harnessix/models/anthropic.py +233 -0
  242. harnessix/models/billing.py +40 -0
  243. harnessix/models/config.py +199 -0
  244. harnessix/models/contracts.py +175 -0
  245. harnessix/models/costs.py +480 -0
  246. harnessix/models/openai_chat.py +347 -0
  247. harnessix/models/pricing.py +114 -0
  248. harnessix/models/proxy.py +78 -0
  249. harnessix/models/scripted.py +60 -0
  250. harnessix/models/tool_result_text.py +264 -0
  251. harnessix/observability/__init__.py +43 -0
  252. harnessix/observability/core.py +130 -0
  253. harnessix/observability/logging.py +78 -0
  254. harnessix/observability/opentelemetry.py +171 -0
  255. harnessix/patches/__init__.py +1 -0
  256. harnessix/patches/agent_bridge.py +355 -0
  257. harnessix/patches/batch_agent_bridge.py +468 -0
  258. harnessix/patches/batch_approval_contracts.py +88 -0
  259. harnessix/patches/batch_bridge_contracts.py +101 -0
  260. harnessix/patches/batch_contracts.py +60 -0
  261. harnessix/patches/batch_execution.py +159 -0
  262. harnessix/patches/batch_ledger.py +169 -0
  263. harnessix/patches/batch_run_contracts.py +94 -0
  264. harnessix/patches/batch_run_migrations.py +21 -0
  265. harnessix/patches/batch_runs.py +94 -0
  266. harnessix/patches/batches.py +82 -0
  267. harnessix/patches/bridge_contracts.py +57 -0
  268. harnessix/patches/contracts.py +107 -0
  269. harnessix/patches/diff.py +82 -0
  270. harnessix/patches/diff_contracts.py +92 -0
  271. harnessix/patches/diff_document.py +122 -0
  272. harnessix/patches/diff_document_contracts.py +164 -0
  273. harnessix/patches/ledger.py +218 -0
  274. harnessix/patches/ledger_migrations.py +26 -0
  275. harnessix/patches/managed.py +599 -0
  276. harnessix/patches/managed_batches.py +155 -0
  277. harnessix/patches/managed_contracts.py +66 -0
  278. harnessix/patches/managed_io.py +160 -0
  279. harnessix/patches/planner.py +229 -0
  280. harnessix/processes/__init__.py +1 -0
  281. harnessix/processes/bridge_contracts.py +125 -0
  282. harnessix/processes/capture.py +84 -0
  283. harnessix/processes/contracts.py +92 -0
  284. harnessix/processes/git_observation.py +79 -0
  285. harnessix/processes/git_read_windows.py +372 -0
  286. harnessix/processes/output_artifact.py +200 -0
  287. harnessix/processes/owner_output.py +167 -0
  288. harnessix/processes/owner_protocol.py +213 -0
  289. harnessix/processes/owner_receipt.py +415 -0
  290. harnessix/processes/posix_owner.py +548 -0
  291. harnessix/processes/public_output.py +219 -0
  292. harnessix/processes/receipt_projection.py +88 -0
  293. harnessix/processes/runtime.py +335 -0
  294. harnessix/processes/state_directory.py +41 -0
  295. harnessix/processes/supervision_contracts.py +264 -0
  296. harnessix/processes/supervision_planner.py +264 -0
  297. harnessix/processes/supervision_store.py +250 -0
  298. harnessix/processes/supervisor.py +728 -0
  299. harnessix/processes/supervisor_capabilities.py +116 -0
  300. harnessix/processes/test_contracts.py +44 -0
  301. harnessix/processes/trusted_output.py +514 -0
  302. harnessix/processes/windows_conpty.py +432 -0
  303. harnessix/processes/windows_job.py +177 -0
  304. harnessix/processes/windows_owner.py +474 -0
  305. harnessix/processes/windows_owner_observation.py +21 -0
  306. harnessix/processes/windows_receipt.py +101 -0
  307. harnessix/product_config/__init__.py +56 -0
  308. harnessix/product_config/action_catalog.py +146 -0
  309. harnessix/product_config/action_codec.py +122 -0
  310. harnessix/product_config/action_composition.py +628 -0
  311. harnessix/product_config/action_contracts.py +610 -0
  312. harnessix/product_config/action_diagnostics.py +173 -0
  313. harnessix/product_config/action_owner.py +40 -0
  314. harnessix/product_config/action_recovery.py +148 -0
  315. harnessix/product_config/action_runtime.py +502 -0
  316. harnessix/product_config/action_runtime_types.py +16 -0
  317. harnessix/product_config/action_store.py +522 -0
  318. harnessix/product_config/agent_context.py +557 -0
  319. harnessix/product_config/cli.py +126 -0
  320. harnessix/product_config/codec.py +181 -0
  321. harnessix/product_config/coding_closure_budget.py +48 -0
  322. harnessix/product_config/coding_scratch.py +18 -0
  323. harnessix/product_config/coding_test_outcomes.py +72 -0
  324. harnessix/product_config/coding_tool_view.py +895 -0
  325. harnessix/product_config/context_budget.py +106 -0
  326. harnessix/product_config/contracts.py +602 -0
  327. harnessix/product_config/endpoint_probe.py +155 -0
  328. harnessix/product_config/errors.py +5 -0
  329. harnessix/product_config/eval_action.py +210 -0
  330. harnessix/product_config/eval_process.py +331 -0
  331. harnessix/product_config/extension_composition.py +69 -0
  332. harnessix/product_config/git_approval_history_projection.py +472 -0
  333. harnessix/product_config/git_approval_history_proof.py +201 -0
  334. harnessix/product_config/git_baseline.py +355 -0
  335. harnessix/product_config/git_baseline_contracts.py +70 -0
  336. harnessix/product_config/git_business_associations.py +310 -0
  337. harnessix/product_config/git_business_snapshot.py +48 -0
  338. harnessix/product_config/git_business_snapshot_contracts.py +102 -0
  339. harnessix/product_config/git_business_snapshot_wire.py +211 -0
  340. harnessix/product_config/git_checkpoint_materials.py +287 -0
  341. harnessix/product_config/git_checkpoint_preparation.py +599 -0
  342. harnessix/product_config/git_checkpoint_scope.py +355 -0
  343. harnessix/product_config/git_decision_link_contracts.py +200 -0
  344. harnessix/product_config/git_decision_link_ledger.py +188 -0
  345. harnessix/product_config/git_decision_link_rows.py +138 -0
  346. harnessix/product_config/git_decision_link_sources.py +184 -0
  347. harnessix/product_config/git_decision_link_wire.py +78 -0
  348. harnessix/product_config/git_decision_recovery.py +252 -0
  349. harnessix/product_config/git_delivery_core_store.py +222 -0
  350. harnessix/product_config/git_delivery_observed_contracts.py +137 -0
  351. harnessix/product_config/git_delivery_observed_wire.py +41 -0
  352. harnessix/product_config/git_delivery_plan_contracts.py +293 -0
  353. harnessix/product_config/git_delivery_plan_materials.py +142 -0
  354. harnessix/product_config/git_delivery_plan_snapshot.py +180 -0
  355. harnessix/product_config/git_delivery_plan_validation.py +106 -0
  356. harnessix/product_config/git_delivery_plan_wire.py +188 -0
  357. harnessix/product_config/git_delivery_process.py +595 -0
  358. harnessix/product_config/git_delivery_review.py +330 -0
  359. harnessix/product_config/git_delivery_review_codec.py +209 -0
  360. harnessix/product_config/git_delivery_review_contracts.py +80 -0
  361. harnessix/product_config/git_delivery_review_host.py +275 -0
  362. harnessix/product_config/git_delivery_route_core.py +81 -0
  363. harnessix/product_config/git_delivery_source.py +464 -0
  364. harnessix/product_config/git_material_process.py +481 -0
  365. harnessix/product_config/git_native_control.py +114 -0
  366. harnessix/product_config/git_parent_contracts.py +25 -0
  367. harnessix/product_config/git_prefix_catalog.py +103 -0
  368. harnessix/product_config/git_prefix_reader.py +120 -0
  369. harnessix/product_config/git_prefix_records.py +154 -0
  370. harnessix/product_config/git_prefix_rows.py +113 -0
  371. harnessix/product_config/git_prefix_sql.py +290 -0
  372. harnessix/product_config/git_prefix_writer.py +319 -0
  373. harnessix/product_config/git_prepared_approval_history.py +364 -0
  374. harnessix/product_config/git_prepared_link_connection.py +274 -0
  375. harnessix/product_config/git_prepared_link_contracts.py +73 -0
  376. harnessix/product_config/git_prepared_link_ledger.py +424 -0
  377. harnessix/product_config/git_prepared_link_observation.py +175 -0
  378. harnessix/product_config/git_prepared_link_proof.py +326 -0
  379. harnessix/product_config/git_prepared_link_rows.py +99 -0
  380. harnessix/product_config/git_prepared_link_wire.py +76 -0
  381. harnessix/product_config/git_prepared_native_identity.py +151 -0
  382. harnessix/product_config/git_prepared_runtime_thread.py +274 -0
  383. harnessix/product_config/git_process_host.py +170 -0
  384. harnessix/product_config/git_repository_observation.py +128 -0
  385. harnessix/product_config/git_review_identity.py +182 -0
  386. harnessix/product_config/git_user_authority.py +136 -0
  387. harnessix/product_config/git_user_observation.py +591 -0
  388. harnessix/product_config/git_user_observation_contracts.py +83 -0
  389. harnessix/product_config/git_user_observation_paths.py +141 -0
  390. harnessix/product_config/git_user_source_files.py +350 -0
  391. harnessix/product_config/git_user_source_scope.py +90 -0
  392. harnessix/product_config/host_process_action.py +254 -0
  393. harnessix/product_config/mcp_composition.py +163 -0
  394. harnessix/product_config/mcp_config.py +216 -0
  395. harnessix/product_config/mcp_status.py +108 -0
  396. harnessix/product_config/mcp_trust.py +60 -0
  397. harnessix/product_config/migration.py +200 -0
  398. harnessix/product_config/native_process.py +608 -0
  399. harnessix/product_config/native_sandbox.py +269 -0
  400. harnessix/product_config/native_shells.py +65 -0
  401. harnessix/product_config/preflight.py +123 -0
  402. harnessix/product_config/preflight_actions.py +218 -0
  403. harnessix/product_config/preflight_configuration.py +337 -0
  404. harnessix/product_config/preflight_environment.py +316 -0
  405. harnessix/product_config/preflight_support.py +83 -0
  406. harnessix/product_config/private_json.py +22 -0
  407. harnessix/product_config/process_action.py +766 -0
  408. harnessix/product_config/process_command.py +137 -0
  409. harnessix/product_config/process_profile.py +382 -0
  410. harnessix/product_config/product_contracts.py +197 -0
  411. harnessix/product_config/runtime.py +457 -0
  412. harnessix/product_config/sandbox_approval.py +46 -0
  413. harnessix/product_config/server.py +524 -0
  414. harnessix/product_config/session_key.py +55 -0
  415. harnessix/product_config/session_key_codec.py +40 -0
  416. harnessix/product_config/session_key_dpapi.py +64 -0
  417. harnessix/product_config/session_key_posix.py +221 -0
  418. harnessix/product_config/session_key_store.py +27 -0
  419. harnessix/product_config/session_key_windows.py +104 -0
  420. harnessix/product_config/session_key_windows_files.py +185 -0
  421. harnessix/product_config/session_key_windows_security.py +29 -0
  422. harnessix/product_config/skill_composition.py +120 -0
  423. harnessix/product_config/skill_diagnostics.py +62 -0
  424. harnessix/product_config/state_backup.py +420 -0
  425. harnessix/product_config/state_backup_cli.py +99 -0
  426. harnessix/product_config/state_backup_contracts.py +122 -0
  427. harnessix/product_config/state_backup_files.py +494 -0
  428. harnessix/product_config/state_backup_records.py +328 -0
  429. harnessix/product_config/state_backup_v2_contracts.py +125 -0
  430. harnessix/product_config/state_backup_v2_validation.py +179 -0
  431. harnessix/product_config/state_backup_validation.py +291 -0
  432. harnessix/product_config/state_backup_windows.py +54 -0
  433. harnessix/product_config/state_owner.py +154 -0
  434. harnessix/product_config/state_owner_posix.py +67 -0
  435. harnessix/product_config/state_owner_windows.py +71 -0
  436. harnessix/product_config/state_restore.py +145 -0
  437. harnessix/product_config/state_restore_contracts.py +84 -0
  438. harnessix/product_config/state_restore_flow.py +174 -0
  439. harnessix/product_config/state_restore_journal.py +160 -0
  440. harnessix/product_config/state_restore_prepare.py +130 -0
  441. harnessix/product_config/store.py +502 -0
  442. harnessix/product_config/task_dependencies.py +238 -0
  443. harnessix/product_config/wizard.py +481 -0
  444. harnessix/product_config/workspace_patch_review.py +163 -0
  445. harnessix/product_config/workspace_patch_source.py +138 -0
  446. harnessix/product_config/workspace_patch_source_contracts.py +80 -0
  447. harnessix/product_config/workspace_rollback.py +89 -0
  448. harnessix/product_config/zero_config.py +115 -0
  449. harnessix/product_ui/__init__.py +122 -0
  450. harnessix/product_ui/app.py +240 -0
  451. harnessix/product_ui/approval_automation.py +75 -0
  452. harnessix/product_ui/cli.py +600 -0
  453. harnessix/product_ui/contracts.py +134 -0
  454. harnessix/product_ui/controller.py +591 -0
  455. harnessix/product_ui/cost_estimate.py +93 -0
  456. harnessix/product_ui/data_cli.py +275 -0
  457. harnessix/product_ui/diff_view.py +45 -0
  458. harnessix/product_ui/error_help.py +147 -0
  459. harnessix/product_ui/errors.py +13 -0
  460. harnessix/product_ui/git_detection.py +36 -0
  461. harnessix/product_ui/interaction_presenter.py +85 -0
  462. harnessix/product_ui/interaction_screens.py +287 -0
  463. harnessix/product_ui/interaction_service.py +316 -0
  464. harnessix/product_ui/interactions.py +395 -0
  465. harnessix/product_ui/main_view.py +164 -0
  466. harnessix/product_ui/mcp_cli.py +94 -0
  467. harnessix/product_ui/preflight_text.py +95 -0
  468. harnessix/product_ui/projection.py +267 -0
  469. harnessix/product_ui/rendering.py +124 -0
  470. harnessix/product_ui/session.py +345 -0
  471. harnessix/product_ui/skills_cli.py +22 -0
  472. harnessix/product_ui/state_file.py +226 -0
  473. harnessix/product_ui/state_store.py +180 -0
  474. harnessix/product_ui/thread_selection.py +55 -0
  475. harnessix/product_ui/tool_summary.py +105 -0
  476. harnessix/product_ui/turn_budget.py +36 -0
  477. harnessix/product_ui/workspace_trust.py +95 -0
  478. harnessix/protocol/__init__.py +94 -0
  479. harnessix/protocol/artifact_limits.py +11 -0
  480. harnessix/protocol/codec.py +94 -0
  481. harnessix/protocol/compatibility.py +30 -0
  482. harnessix/protocol/contracts.py +656 -0
  483. harnessix/protocol/projection.py +352 -0
  484. harnessix/protocol/requests.py +253 -0
  485. harnessix/sandbox/__init__.py +1 -0
  486. harnessix/sandbox/bubblewrap.py +222 -0
  487. harnessix/sandbox/capabilities.py +346 -0
  488. harnessix/sandbox/container.py +629 -0
  489. harnessix/sandbox/contracts.py +305 -0
  490. harnessix/sandbox/egress.py +297 -0
  491. harnessix/sandbox/native_contracts.py +199 -0
  492. harnessix/sandbox/native_policy.py +415 -0
  493. harnessix/sandbox/native_probe.py +187 -0
  494. harnessix/sandbox/network.py +149 -0
  495. harnessix/sandbox/network_isolation.py +82 -0
  496. harnessix/sandbox/planner.py +102 -0
  497. harnessix/sandbox/process_runtime.py +289 -0
  498. harnessix/sandbox/seatbelt.py +106 -0
  499. harnessix/sandbox/store.py +108 -0
  500. harnessix/sandbox/workspace_snapshot.py +51 -0
  501. harnessix/sdk/__init__.py +17 -0
  502. harnessix/sdk/agent_client.py +406 -0
  503. harnessix/sdk/errors.py +22 -0
  504. harnessix/sdk/request.py +82 -0
  505. harnessix/sdk/response.py +119 -0
  506. harnessix/sdk/subprocess.py +349 -0
  507. harnessix/secrets/__init__.py +1 -0
  508. harnessix/secrets/guard.py +79 -0
  509. harnessix/secrets/provider.py +160 -0
  510. harnessix/secrets/publication.py +364 -0
  511. harnessix/secrets/redaction.py +92 -0
  512. harnessix/secrets/text_publication.py +70 -0
  513. harnessix/session/__init__.py +1 -0
  514. harnessix/session/capacity.py +277 -0
  515. harnessix/session/errors.py +31 -0
  516. harnessix/session/event_body_refs.py +16 -0
  517. harnessix/session/git_prefix_contracts.py +82 -0
  518. harnessix/session/git_prefix_publication.py +97 -0
  519. harnessix/session/git_publication_contracts.py +81 -0
  520. harnessix/session/maintenance.py +231 -0
  521. harnessix/session/maintenance_backup.py +209 -0
  522. harnessix/session/maintenance_contracts.py +154 -0
  523. harnessix/session/maintenance_execution.py +235 -0
  524. harnessix/session/maintenance_io.py +64 -0
  525. harnessix/session/maintenance_planning.py +222 -0
  526. harnessix/session/maintenance_records.py +248 -0
  527. harnessix/session/maintenance_restore.py +77 -0
  528. harnessix/session/migrations/0001_initial.sql +13 -0
  529. harnessix/session/migrations/0002_projection_version.sql +1 -0
  530. harnessix/session/migrations/0003_semantic_contract.sql +2 -0
  531. harnessix/session/migrations/0004_model_attempts.sql +2 -0
  532. harnessix/session/migrations/0005_response_billing.sql +2 -0
  533. harnessix/session/migrations/0006_artifacts.sql +16 -0
  534. harnessix/session/migrations/0007_managed_patch.sql +3 -0
  535. harnessix/session/migrations/0008_managed_patch_batch.sql +3 -0
  536. harnessix/session/migrations/0009_batch_diff_artifacts.sql +24 -0
  537. harnessix/session/migrations/0010_agent_process_projection.sql +2 -0
  538. harnessix/session/migrations/0011_process_output_artifacts.sql +23 -0
  539. harnessix/session/migrations/0012_context_inspection.sql +2 -0
  540. harnessix/session/migrations/0013_context_sources.sql +2 -0
  541. harnessix/session/migrations/0014_context_source_consistency.sql +2 -0
  542. harnessix/session/migrations/0015_tool_result_model_view.sql +2 -0
  543. harnessix/session/migrations/0016_compaction_attempt_ledger.sql +2 -0
  544. harnessix/session/migrations/0017_compaction_window.sql +2 -0
  545. harnessix/session/migrations/0018_thread_lifecycle.sql +2 -0
  546. harnessix/session/migrations/0019_turn_retry.sql +2 -0
  547. harnessix/session/migrations/0020_protocol_requests.sql +15 -0
  548. harnessix/session/migrations/0021_deferred_turn_execution.sql +2 -0
  549. harnessix/session/migrations/0022_interactive_turns.sql +2 -0
  550. harnessix/session/migrations/0023_trusted_action_gateway.sql +2 -0
  551. harnessix/session/migrations/0024_trusted_action_review_artifacts.sql +25 -0
  552. harnessix/session/migrations/0025_trusted_action_output_artifacts.sql +26 -0
  553. harnessix/session/migrations/0026_store_maintenance.sql +30 -0
  554. harnessix/session/migrations/0027_thread_list_lookup.sql +4 -0
  555. harnessix/session/migrations/0028_artifact_publication_proof.sql +7 -0
  556. harnessix/session/migrations/0029_authenticated_session_history.sql +14 -0
  557. harnessix/session/migrations/0030_authenticated_artifact_body.sql +3 -0
  558. harnessix/session/migrations/0031_unknown_tool_rejection.sql +2 -0
  559. harnessix/session/migrations/0032_tool_result_working_set.sql +2 -0
  560. harnessix/session/migrations/__init__.py +1 -0
  561. harnessix/session/ports.py +45 -0
  562. harnessix/session/publication_seal.py +254 -0
  563. harnessix/session/sqlite.py +622 -0
  564. harnessix/session/sqlite_append.py +95 -0
  565. harnessix/session/sqlite_history.py +173 -0
  566. harnessix/session/sqlite_publication.py +373 -0
  567. harnessix/session/store_publication.py +438 -0
  568. harnessix/session/verified_prefix_cache.py +60 -0
  569. harnessix/skills/__init__.py +42 -0
  570. harnessix/skills/actions.py +386 -0
  571. harnessix/skills/contracts.py +253 -0
  572. harnessix/skills/prompt.py +38 -0
  573. harnessix/skills/runtime.py +644 -0
  574. harnessix/skills/store.py +276 -0
  575. harnessix/smoke/__init__.py +1 -0
  576. harnessix/smoke/cli.py +72 -0
  577. harnessix/smoke/contracts.py +133 -0
  578. harnessix/smoke/runner.py +237 -0
  579. harnessix/sqlite_readonly.py +18 -0
  580. harnessix/tools/__init__.py +1 -0
  581. harnessix/tools/argument_feedback.py +420 -0
  582. harnessix/tools/argument_repair.py +214 -0
  583. harnessix/tools/contracts.py +289 -0
  584. harnessix/tools/definition_view.py +275 -0
  585. harnessix/tools/files.py +318 -0
  586. harnessix/tools/git.py +533 -0
  587. harnessix/tools/git_contracts.py +98 -0
  588. harnessix/tools/grep_matcher.py +93 -0
  589. harnessix/tools/patterns.py +46 -0
  590. harnessix/tools/read_backend.py +65 -0
  591. harnessix/tools/read_catalog.py +358 -0
  592. harnessix/tools/runtime.py +504 -0
  593. harnessix/tools/search.py +582 -0
  594. harnessix/tools/search_contracts.py +279 -0
  595. harnessix/tools/windows_file_read.py +215 -0
  596. harnessix/tools/windows_read.py +92 -0
  597. harnessix/tools/windows_read_port.py +174 -0
  598. harnessix/tools/windows_search.py +425 -0
  599. harnessix/tools/workspace.py +295 -0
  600. harnessix/trusted_actions/__init__.py +37 -0
  601. harnessix/trusted_actions/agent_gateway.py +135 -0
  602. harnessix/trusted_actions/agent_gateway_invocation.py +63 -0
  603. harnessix/trusted_actions/agent_gateway_output.py +437 -0
  604. harnessix/trusted_actions/agent_gateway_support.py +608 -0
  605. harnessix/trusted_actions/agent_preplanning.py +119 -0
  606. harnessix/trusted_actions/builtin_success.py +125 -0
  607. harnessix/trusted_actions/composite_gateway.py +93 -0
  608. harnessix/trusted_actions/contracts.py +336 -0
  609. harnessix/trusted_actions/legacy_projection.py +103 -0
  610. harnessix/trusted_actions/operation_router.py +243 -0
  611. harnessix/trusted_actions/operation_store.py +322 -0
  612. harnessix/trusted_actions/outcome_validation.py +99 -0
  613. harnessix/trusted_actions/output_budget.py +129 -0
  614. harnessix/trusted_actions/ownership_store.py +150 -0
  615. harnessix/trusted_actions/planning.py +400 -0
  616. harnessix/trusted_actions/policy.py +135 -0
  617. harnessix/trusted_actions/preparation_rejection.py +177 -0
  618. harnessix/trusted_actions/prepared_resolution.py +115 -0
  619. harnessix/trusted_actions/public_errors.py +198 -0
  620. harnessix/trusted_actions/public_outcomes.py +484 -0
  621. harnessix/trusted_actions/recovery_contracts.py +101 -0
  622. harnessix/trusted_actions/router.py +496 -0
  623. harnessix/trusted_actions/store.py +490 -0
  624. harnessix/trusted_actions/transition_store.py +143 -0
  625. harnessix/trusted_actions/versioned_contracts.py +20 -0
  626. harnessix/workspace/__init__.py +1 -0
  627. harnessix/workspace/blob_read_control.py +55 -0
  628. harnessix/workspace/contracts.py +104 -0
  629. harnessix/workspace/git_windows_binding.py +98 -0
  630. harnessix/workspace/leases.py +160 -0
  631. harnessix/workspace/native_observation_io.py +71 -0
  632. harnessix/workspace/parent_closure_codec.py +286 -0
  633. harnessix/workspace/parent_closure_contracts.py +93 -0
  634. harnessix/workspace/parent_closure_paths.py +106 -0
  635. harnessix/workspace/parent_closure_wire.py +41 -0
  636. harnessix/workspace/paths.py +59 -0
  637. harnessix/workspace/snapshot.py +455 -0
  638. harnessix/workspace/snapshot_capture.py +267 -0
  639. harnessix/workspace/snapshot_contracts.py +73 -0
  640. harnessix/workspace/snapshot_fields.py +80 -0
  641. harnessix/workspace/snapshot_ports.py +73 -0
  642. harnessix/workspace/snapshot_v2.py +134 -0
  643. harnessix/workspace/snapshot_verification.py +45 -0
  644. harnessix/workspace/terminal_read_control.py +112 -0
  645. harnessix/workspace/windows.py +478 -0
  646. harnessix/workspace/windows_file_io.py +223 -0
  647. harnessix/workspace/windows_private_directory.py +55 -0
  648. harnessix/workspace/windows_private_security.py +217 -0
  649. harnessix-0.10.0b3.dist-info/METADATA +117 -0
  650. harnessix-0.10.0b3.dist-info/RECORD +653 -0
  651. harnessix-0.10.0b3.dist-info/WHEEL +4 -0
  652. harnessix-0.10.0b3.dist-info/entry_points.txt +2 -0
  653. harnessix-0.10.0b3.dist-info/licenses/LICENSE +235 -0
harnessix/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """Harnessix Code产品包;公共客户端只从``harnessix.sdk``导入。"""
2
+
3
+ __all__: list[str] = []
harnessix/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ """提供 `python -m harnessix` 命令入口。"""
2
+
3
+ from harnessix.cli import main
4
+
5
+ main()
@@ -0,0 +1 @@
1
+ """供应商无关的 Agent Runtime Kernel。"""
@@ -0,0 +1,365 @@
1
+ """持久Agent状态机:校验审批请求指纹、有效期与决策绑定。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import json
7
+ from datetime import timedelta
8
+ from uuid import UUID, uuid5
9
+
10
+ from pydantic import ValidationError
11
+
12
+ from harnessix.agent.errors import KernelError
13
+ from harnessix.agent.models import (
14
+ ApprovalContent,
15
+ ApprovalRequestContent,
16
+ Item,
17
+ PatchApprovalRequestContent,
18
+ PatchBatchApprovalRequestContent,
19
+ ProcessApprovalRequestContent,
20
+ Thread,
21
+ ToolCallContent,
22
+ ToolResultContent,
23
+ TrustedActionApprovalRequestContent,
24
+ Turn,
25
+ )
26
+ from harnessix.domain.models import (
27
+ ApprovalDecision,
28
+ ApprovalOutcome,
29
+ EffectClass,
30
+ ToolDescriptor,
31
+ utc_now,
32
+ )
33
+ from harnessix.patches.batch_bridge_contracts import ManagedPatchBatchCallPlan
34
+ from harnessix.patches.batch_contracts import PatchBatchProposal
35
+ from harnessix.patches.bridge_contracts import ManagedPatchCallPlan
36
+ from harnessix.patches.contracts import PatchProposal
37
+ from harnessix.processes.bridge_contracts import PROCESS_AGENT_FRONTENDS, AgentProcessCallPlan
38
+ from harnessix.processes.contracts import ProcessRequest
39
+ from harnessix.processes.test_contracts import RunTestsInput
40
+ from harnessix.tools.workspace import digest
41
+
42
+ READ_ONLY_POLICY_VERSION = "kernel-read-only/v1"
43
+ _TRUSTED_ACTION_NAMESPACE = UUID("f5cc2cc0-5b13-4f37-bd6e-7b3111b10c03")
44
+
45
+
46
+ def _fingerprint(value: object) -> str:
47
+ return hashlib.sha256(
48
+ json.dumps(
49
+ value, sort_keys=True, separators=(",", ":"), ensure_ascii=False, allow_nan=False
50
+ ).encode()
51
+ ).hexdigest()
52
+
53
+
54
+ def tool_fingerprint(definition: ToolDescriptor) -> str:
55
+ return _fingerprint(definition.model_dump(mode="json"))
56
+
57
+
58
+ def request_fingerprint(
59
+ thread: Thread,
60
+ turn: Turn,
61
+ call: ToolCallContent,
62
+ *,
63
+ policy_version: str = READ_ONLY_POLICY_VERSION,
64
+ ) -> str:
65
+ """绑定当前只读契约;不是 Workspace 内容哈希或未来的 OS 授权凭证。"""
66
+ return execution_fingerprint(
67
+ thread.thread_id, turn.turn_id, thread.workspace, call, policy_version=policy_version
68
+ )
69
+
70
+
71
+ def execution_fingerprint(
72
+ thread_id: UUID,
73
+ turn_id: UUID,
74
+ workspace: str,
75
+ call: ToolCallContent,
76
+ *,
77
+ policy_version: str = READ_ONLY_POLICY_VERSION,
78
+ ) -> str:
79
+ """审批和进程内作用域共享同一摘要格式,不引入第二套权限。"""
80
+ return _fingerprint(
81
+ {
82
+ "policy": policy_version,
83
+ "thread_id": str(thread_id),
84
+ "turn_id": str(turn_id),
85
+ "workspace": workspace,
86
+ "call": call.model_dump(mode="json"),
87
+ }
88
+ )
89
+
90
+
91
+ def trusted_action_invocation_id(
92
+ thread_id: UUID,
93
+ turn_id: UUID,
94
+ call: ToolCallContent,
95
+ ) -> UUID:
96
+ """从持久调用身份导出稳定Plan ID,重启不得产生第二个副作用计划。"""
97
+
98
+ if call.tool_fingerprint is None:
99
+ raise ValueError("Trusted Action调用缺少Tool Fingerprint")
100
+ return uuid5(
101
+ _TRUSTED_ACTION_NAMESPACE,
102
+ f"{thread_id}:{turn_id}:{call.call_id}:{call.tool_fingerprint}",
103
+ )
104
+
105
+
106
+ def trusted_action_request_fingerprint(
107
+ thread: Thread,
108
+ turn: Turn,
109
+ call: ToolCallContent,
110
+ *,
111
+ plan_id: UUID,
112
+ plan_fingerprint: str,
113
+ execution_fingerprint: str,
114
+ policy_id: str,
115
+ policy_version: str,
116
+ presentation: str,
117
+ diff_sha256: str | None,
118
+ ) -> str:
119
+ """绑定Session归属、完整调用、Router计划及可见Review证据。"""
120
+
121
+ return _fingerprint(
122
+ {
123
+ "spec_version": "harnessix.agent-trusted-action-approval/v1",
124
+ "thread_id": str(thread.thread_id),
125
+ "turn_id": str(turn.turn_id),
126
+ "workspace": thread.workspace,
127
+ "call": call.model_dump(mode="json"),
128
+ "plan_id": str(plan_id),
129
+ "plan_fingerprint": plan_fingerprint,
130
+ "execution_fingerprint": execution_fingerprint,
131
+ "policy_id": policy_id,
132
+ "policy_version": policy_version,
133
+ "presentation": presentation,
134
+ "diff_sha256": diff_sha256,
135
+ }
136
+ )
137
+
138
+
139
+ def approval_for(turn: Turn, call: ToolCallContent) -> Item | None:
140
+ return next(
141
+ (
142
+ item
143
+ for item in turn.items
144
+ if isinstance(item.content, ApprovalContent) and item.content.call_id == call.call_id
145
+ ),
146
+ None,
147
+ )
148
+
149
+
150
+ def approval_action_id(turn: Turn, call: ToolCallContent) -> UUID | None:
151
+ """返回统一Action或旧Process审批绑定的稳定执行身份。"""
152
+
153
+ item = approval_for(turn, call)
154
+ if item is None:
155
+ return None
156
+ if isinstance(item.content, TrustedActionApprovalRequestContent):
157
+ return item.content.plan_id
158
+ if isinstance(item.content, ProcessApprovalRequestContent):
159
+ return item.content.plan.action_id
160
+ return None
161
+
162
+
163
+ # 宿主以系统身份作出的决定(如本机沙箱内命令的自动批准,ADR 0113)使用该前缀。
164
+ SYSTEM_APPROVAL_ACTOR_PREFIX = "harnessix:"
165
+
166
+
167
+ def approval_was_decided(content: ApprovalContent, decision: ApprovalDecision) -> bool:
168
+ """同语义决定幂等返回;冲突重放失败关闭。
169
+
170
+ 系统身份已作出决定后,客户端可能在看到审批请求的瞬间再回复一次(自动批准模式或人工)。
171
+ 结论相同时视为同一决定幂等返回;结论相反仍失败关闭。
172
+ """
173
+
174
+ recorded = content.decision
175
+ if recorded is None:
176
+ return False
177
+ if recorded.actor.startswith(SYSTEM_APPROVAL_ACTOR_PREFIX) and (
178
+ recorded.outcome == decision.outcome
179
+ ):
180
+ return True
181
+ if (recorded.outcome, recorded.actor, recorded.reason) != (
182
+ decision.outcome,
183
+ decision.actor,
184
+ decision.reason,
185
+ ):
186
+ raise KernelError("approval_conflict", "审批已绑定其他决定")
187
+ return True
188
+
189
+
190
+ def approval_type_matches(call: ToolCallContent, content: ApprovalContent) -> bool:
191
+ """校验审批投影是否适用于当前Tool前端及效果类别。"""
192
+
193
+ if not call.requires_approval:
194
+ return False
195
+ if isinstance(content, TrustedActionApprovalRequestContent):
196
+ return True
197
+ if isinstance(content, ApprovalRequestContent):
198
+ return call.effect_class is EffectClass.READ_ONLY
199
+ if call.effect_class is not EffectClass.NON_IDEMPOTENT_WRITE:
200
+ return False
201
+ if isinstance(content, ProcessApprovalRequestContent):
202
+ return call.tool in PROCESS_AGENT_FRONTENDS
203
+ expected_tool = (
204
+ "apply_patch_batch"
205
+ if isinstance(content, PatchBatchApprovalRequestContent)
206
+ else "apply_patch"
207
+ )
208
+ return call.tool == expected_tool
209
+
210
+
211
+ def validate_patch_plan(
212
+ thread: Thread, turn: Turn, call: ToolCallContent, plan: ManagedPatchCallPlan
213
+ ) -> bool:
214
+ """只校验事件归属和提案一致性;磁盘事实由受管桥接核对。"""
215
+ try:
216
+ checked = ManagedPatchCallPlan.model_validate_json(plan.model_dump_json())
217
+ proposal = PatchProposal.model_validate_json(json.dumps(call.arguments, allow_nan=False))
218
+ except (ValidationError, ValueError, TypeError):
219
+ return False
220
+ return (
221
+ call.tool == "apply_patch"
222
+ and call.effect_class == EffectClass.NON_IDEMPOTENT_WRITE
223
+ and call.requires_approval
224
+ and call.tool_fingerprint is not None
225
+ and (checked.thread_id, checked.turn_id, checked.call_id)
226
+ == (thread.thread_id, turn.turn_id, call.call_id)
227
+ and checked.call_fingerprint == request_fingerprint(thread, turn, call)
228
+ and checked.manifest.proposal_sha256 == _fingerprint(proposal.model_dump(mode="json"))
229
+ )
230
+
231
+
232
+ def validate_batch_plan(
233
+ thread: Thread, turn: Turn, call: ToolCallContent, plan: ManagedPatchBatchCallPlan
234
+ ) -> bool:
235
+ try:
236
+ checked = ManagedPatchBatchCallPlan.model_validate_json(plan.model_dump_json())
237
+ proposal = PatchBatchProposal.model_validate_json(
238
+ json.dumps(call.arguments, allow_nan=False)
239
+ )
240
+ except (ValidationError, ValueError, TypeError):
241
+ return False
242
+ return (
243
+ call.tool == "apply_patch_batch"
244
+ and call.effect_class == EffectClass.NON_IDEMPOTENT_WRITE
245
+ and call.requires_approval
246
+ and call.tool_fingerprint is not None
247
+ and (checked.thread_id, checked.turn_id, checked.call_id)
248
+ == (thread.thread_id, turn.turn_id, call.call_id)
249
+ and checked.call_fingerprint == request_fingerprint(thread, turn, call)
250
+ and checked.backend.manifest.proposal_sha256
251
+ == _fingerprint(proposal.model_dump(mode="json"))
252
+ )
253
+
254
+
255
+ def validate_process_plan(
256
+ thread: Thread, turn: Turn, call: ToolCallContent, plan: AgentProcessCallPlan
257
+ ) -> bool:
258
+ """校验Session调用归属和公开命令摘要;Action事实由宿主桥接另行核对。"""
259
+ try:
260
+ checked = AgentProcessCallPlan.model_validate_json(plan.model_dump_json())
261
+ except (ValidationError, ValueError, TypeError):
262
+ return False
263
+ common = (
264
+ call.tool in PROCESS_AGENT_FRONTENDS
265
+ and call.effect_class == EffectClass.NON_IDEMPOTENT_WRITE
266
+ and call.requires_approval
267
+ and call.tool_fingerprint is not None
268
+ and (checked.thread_id, checked.turn_id, checked.call_id)
269
+ == (thread.thread_id, turn.turn_id, call.call_id)
270
+ and checked.workspace == thread.workspace
271
+ and checked.call_fingerprint == request_fingerprint(thread, turn, call)
272
+ )
273
+ if not common:
274
+ return False
275
+ if call.tool == "run_tests":
276
+ try:
277
+ RunTestsInput.model_validate_json(json.dumps(call.arguments, allow_nan=False))
278
+ except (ValidationError, ValueError, TypeError):
279
+ return False
280
+ return True
281
+ try:
282
+ process = ProcessRequest.model_validate_json(json.dumps(call.arguments, allow_nan=False))
283
+ except (ValidationError, ValueError, TypeError):
284
+ return False
285
+ return (
286
+ checked.action_tool_version == call.tool_version
287
+ and checked.program == process.program
288
+ and checked.arguments_sha256 == digest(process.arguments)
289
+ and checked.timeout_seconds == process.timeout_seconds
290
+ )
291
+
292
+
293
+ def approval_matches(
294
+ thread: Thread,
295
+ turn: Turn,
296
+ call: ToolCallContent,
297
+ content: ApprovalContent,
298
+ ) -> bool:
299
+ if isinstance(content, TrustedActionApprovalRequestContent):
300
+ try:
301
+ expected_plan_id = trusted_action_invocation_id(thread.thread_id, turn.turn_id, call)
302
+ except ValueError:
303
+ return False
304
+ return (
305
+ call.requires_approval
306
+ and content.plan_id == expected_plan_id
307
+ and content.request_fingerprint
308
+ == trusted_action_request_fingerprint(
309
+ thread,
310
+ turn,
311
+ call,
312
+ plan_id=content.plan_id,
313
+ plan_fingerprint=content.plan_fingerprint,
314
+ execution_fingerprint=content.execution_fingerprint,
315
+ policy_id=content.policy_id,
316
+ policy_version=content.policy_version,
317
+ presentation=content.presentation,
318
+ diff_sha256=(
319
+ content.diff_artifact.sha256 if content.diff_artifact is not None else None
320
+ ),
321
+ )
322
+ )
323
+ if isinstance(content, ProcessApprovalRequestContent):
324
+ return (
325
+ validate_process_plan(thread, turn, call, content.plan)
326
+ and content.request_fingerprint == content.plan.approval_fingerprint
327
+ )
328
+ if isinstance(content, PatchBatchApprovalRequestContent):
329
+ return (
330
+ validate_batch_plan(thread, turn, call, content.plan)
331
+ and content.request_fingerprint == content.plan.approval_fingerprint
332
+ )
333
+ if isinstance(content, PatchApprovalRequestContent):
334
+ return (
335
+ validate_patch_plan(thread, turn, call, content.plan)
336
+ and content.request_fingerprint == content.plan.approval_fingerprint
337
+ )
338
+ return content.request_fingerprint == request_fingerprint(
339
+ thread, turn, call, policy_version=content.policy_version
340
+ )
341
+
342
+
343
+ def remaining_seconds(turn: Turn) -> float:
344
+ # 持久墙钟截止时间:暂停、离线和重启均不刷新原 Turn 的预算。
345
+ return (
346
+ turn.created_at + timedelta(seconds=turn.budget.timeout_seconds) - utc_now()
347
+ ).total_seconds()
348
+
349
+
350
+ def with_rejection_reason[ResultT](result: ResultT, approval: Item | None) -> ResultT:
351
+ """用户拒绝并填写原因时附在错误信息后,模型据此调整做法,而不是只知道被拒绝。"""
352
+
353
+ if not isinstance(result, ToolResultContent) or approval is None:
354
+ return result
355
+ assert isinstance(approval.content, ApprovalContent)
356
+ decision, error = approval.content.decision, result.error
357
+ if (
358
+ decision is None
359
+ or decision.outcome != ApprovalOutcome.REJECTED
360
+ or not decision.reason
361
+ or error is None
362
+ ):
363
+ return result
364
+ message = f"{error.message}。用户说明:{decision.reason}"[:2000]
365
+ return result.model_copy(update={"error": error.model_copy(update={"message": message})})
@@ -0,0 +1,143 @@
1
+ """按已公布input_schema还原被模型字符串化的JSON容器参数;不放宽任何校验。
2
+
3
+ 部分模型会把数组/对象参数序列化为JSON字符串(如``"files": "[...]"``),偶尔还遗漏末尾的
4
+ 闭合括号。仅当Schema在该位置明确声明为array或object、且字符串可严格解析(必要时仅补齐末尾
5
+ 缺失的闭合括号)为同类型容器时才还原;其余值原样保留,由工具原有的严格校验照旧拒绝。
6
+ 还原后的规范参数才进入审批、审计与执行。还原先于提案阶段的结构修复(call_repair):
7
+ 修复只认真实的数组与对象,字符串化的``files``会让它看不到外层字段与默认mode。
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from collections.abc import Mapping
13
+ from typing import Any
14
+
15
+ from pydantic import JsonValue
16
+
17
+ from harnessix.domain.models import ToolDescriptor
18
+ from harnessix.models._json import strict_json
19
+ from harnessix.models.contracts import ToolCallCompleted, ToolCallRejected
20
+
21
+ _MAX_DEPTH = 16
22
+
23
+
24
+ def normalize_proposal(
25
+ proposal: ToolCallCompleted | ToolCallRejected, definitions: Mapping[str, ToolDescriptor]
26
+ ) -> ToolCallCompleted | ToolCallRejected:
27
+ """按提案所调工具的公布Schema还原参数;未注册工具与拒绝提案原样返回。"""
28
+ if not isinstance(proposal, ToolCallCompleted):
29
+ return proposal
30
+ definition = definitions.get(proposal.tool)
31
+ if definition is None:
32
+ return proposal
33
+ normalized = normalize_stringified_containers(proposal.arguments, definition.input_schema)
34
+ if normalized == proposal.arguments:
35
+ return proposal
36
+ return proposal.model_copy(update={"arguments": normalized})
37
+
38
+
39
+ def normalize_stringified_containers(
40
+ arguments: dict[str, JsonValue], schema: Mapping[str, Any]
41
+ ) -> dict[str, JsonValue]:
42
+ normalized = _normalize(arguments, schema, schema, 0)
43
+ assert isinstance(normalized, dict)
44
+ return normalized
45
+
46
+
47
+ def _normalize(
48
+ value: JsonValue, schema: Mapping[str, Any], root: Mapping[str, Any], depth: int
49
+ ) -> JsonValue:
50
+ node = _resolve(schema, root)
51
+ if depth > _MAX_DEPTH or node is None:
52
+ return value
53
+ expected = _container_type(node)
54
+ if isinstance(value, str) and expected is not None:
55
+ value = _decoded(value, expected)
56
+ if isinstance(value, dict):
57
+ properties = node.get("properties")
58
+ if not isinstance(properties, Mapping):
59
+ return value
60
+ return {
61
+ key: _normalize(item, properties[key], root, depth + 1)
62
+ if isinstance(properties.get(key), Mapping)
63
+ else item
64
+ for key, item in value.items()
65
+ }
66
+ if isinstance(value, list):
67
+ items = node.get("items")
68
+ if not isinstance(items, Mapping):
69
+ return value
70
+ return [_normalize(item, items, root, depth + 1) for item in value]
71
+ return value
72
+
73
+
74
+ def _resolve(node: Mapping[str, Any], root: Mapping[str, Any]) -> Mapping[str, Any] | None:
75
+ for _ in range(_MAX_DEPTH):
76
+ reference = node.get("$ref")
77
+ if reference is None:
78
+ return node
79
+ if not isinstance(reference, str) or not reference.startswith("#/$defs/"):
80
+ return None
81
+ definitions = root.get("$defs")
82
+ target = (
83
+ definitions.get(reference.removeprefix("#/$defs/"))
84
+ if isinstance(definitions, Mapping)
85
+ else None
86
+ )
87
+ if not isinstance(target, Mapping):
88
+ return None
89
+ node = target
90
+ return None
91
+
92
+
93
+ def _container_type(node: Mapping[str, Any]) -> type | None:
94
+ declared = node.get("type")
95
+ if declared == "array":
96
+ return list
97
+ if declared == "object":
98
+ return dict
99
+ return None
100
+
101
+
102
+ def _decoded(text: str, expected: type) -> JsonValue:
103
+ candidate = text.strip()
104
+ for attempt in (candidate, _with_missing_closers(candidate)):
105
+ if attempt is None:
106
+ continue
107
+ try:
108
+ parsed = strict_json(attempt)
109
+ except ValueError:
110
+ continue
111
+ if isinstance(parsed, expected):
112
+ return parsed
113
+ return text
114
+
115
+
116
+ def _with_missing_closers(text: str) -> str | None:
117
+ """只补齐末尾缺失的外层闭合括号:文本须以已闭合的内层容器结尾(`]`或`}`)。
118
+
119
+ 不改写任何已有字符;值中途截断、未闭合字符串、多余闭合或其他语法错误一律放弃,
120
+ 交由严格校验拒绝。
121
+ """
122
+ if not text.endswith(("]", "}")):
123
+ return None
124
+ stack: list[str] = []
125
+ in_string = escaped = False
126
+ for character in text:
127
+ if in_string:
128
+ if escaped:
129
+ escaped = False
130
+ elif character == "\\":
131
+ escaped = True
132
+ elif character == '"':
133
+ in_string = False
134
+ elif character == '"':
135
+ in_string = True
136
+ elif character in "[{":
137
+ stack.append("]" if character == "[" else "}")
138
+ elif character in "]}":
139
+ if not stack or stack.pop() != character:
140
+ return None
141
+ if in_string or not stack:
142
+ return None
143
+ return text + "".join(reversed(stack))
@@ -0,0 +1,60 @@
1
+ """持久Agent状态机:根据Provider观测事实结算模型尝试用量。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ from datetime import datetime
6
+
7
+ from harnessix.agent.errors import KernelError
8
+ from harnessix.agent.usage import ModelAttempt, ModelAttemptFinished, ModelUsageObserved
9
+
10
+
11
+ def settle_observation(
12
+ attempt: ModelAttempt,
13
+ payload: ModelUsageObserved | ModelAttemptFinished,
14
+ occurred_at: datetime,
15
+ ) -> tuple[ModelAttempt, int, int]:
16
+ """共享累计差额规则;用途、状态阶段、预算和身份唯一性由调用方守卫。"""
17
+ if attempt.attempt_id != payload.attempt_id or attempt.status != "running":
18
+ raise KernelError("invalid_event", "模型尝试尚未开始或已结算")
19
+ if isinstance(payload, ModelUsageObserved):
20
+ try:
21
+ payload.usage.validate_successor(attempt.usage)
22
+ billing = payload.billing if payload.billing is not None else attempt.billing
23
+ billing.validate_successor(attempt.billing)
24
+ billing.validate_usage(payload.usage)
25
+ except ValueError:
26
+ raise KernelError("invalid_event", "模型用量或计费元数据冲突") from None
27
+ for field in ("actual_model", "response_id"):
28
+ before, after = getattr(attempt, field), getattr(payload, field)
29
+ if before is not None and after is not None and before != after:
30
+ raise KernelError("invalid_event", "尝试响应身份发生变化")
31
+ input_delta = (payload.usage.input_tokens or 0) - (attempt.usage.input_tokens or 0)
32
+ output_delta = (payload.usage.output_tokens or 0) - (attempt.usage.output_tokens or 0)
33
+ return (
34
+ attempt.model_copy(
35
+ update={
36
+ "usage": payload.usage,
37
+ "billing": billing,
38
+ "actual_model": payload.actual_model or attempt.actual_model,
39
+ "response_id": payload.response_id or attempt.response_id,
40
+ }
41
+ ),
42
+ input_delta,
43
+ output_delta,
44
+ )
45
+ if payload.outcome == "completed":
46
+ if attempt.usage.completeness != "complete":
47
+ raise KernelError("invalid_event", "成功尝试需要完整用量")
48
+ if attempt.actual_model is None or attempt.response_id is None:
49
+ raise KernelError("invalid_event", "成功尝试缺少响应身份")
50
+ return (
51
+ attempt.model_copy(
52
+ update={
53
+ "status": payload.outcome,
54
+ "error": payload.error,
55
+ "finished_at": occurred_at,
56
+ }
57
+ ),
58
+ 0,
59
+ 0,
60
+ )