@stigmer/runner 3.14.0 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (331) hide show
  1. package/dist/.build-fingerprint +1 -1
  2. package/dist/__test-utils__/execution-record-fixture.d.ts +47 -0
  3. package/dist/__test-utils__/execution-record-fixture.js +68 -0
  4. package/dist/__test-utils__/execution-record-fixture.js.map +1 -0
  5. package/dist/__test-utils__/harness-contract/types.d.ts +166 -0
  6. package/dist/__test-utils__/harness-contract/types.js +49 -0
  7. package/dist/__test-utils__/harness-contract/types.js.map +1 -0
  8. package/dist/__test-utils__/hermetic-activity.d.ts +268 -0
  9. package/dist/__test-utils__/hermetic-activity.js +385 -0
  10. package/dist/__test-utils__/hermetic-activity.js.map +1 -0
  11. package/dist/__test-utils__/mock-client.d.ts +13 -0
  12. package/dist/__test-utils__/mock-client.js +45 -0
  13. package/dist/__test-utils__/mock-client.js.map +1 -0
  14. package/dist/__test-utils__/mock-workspace.d.ts +15 -0
  15. package/dist/__test-utils__/mock-workspace.js +21 -0
  16. package/dist/__test-utils__/mock-workspace.js.map +1 -0
  17. package/dist/__test-utils__/model-registry-fixture.d.ts +56 -0
  18. package/dist/__test-utils__/model-registry-fixture.js +66 -0
  19. package/dist/__test-utils__/model-registry-fixture.js.map +1 -0
  20. package/dist/__test-utils__/turn-input-fixture.d.ts +60 -0
  21. package/dist/__test-utils__/turn-input-fixture.js +118 -0
  22. package/dist/__test-utils__/turn-input-fixture.js.map +1 -0
  23. package/dist/activities/execute-cursor/__test-utils__/contract-subject.d.ts +107 -0
  24. package/dist/activities/execute-cursor/__test-utils__/contract-subject.js +344 -0
  25. package/dist/activities/execute-cursor/__test-utils__/contract-subject.js.map +1 -0
  26. package/dist/activities/execute-cursor/__test-utils__/cursor-hook-harness.d.ts +20 -0
  27. package/dist/activities/execute-cursor/__test-utils__/cursor-hook-harness.js +58 -0
  28. package/dist/activities/execute-cursor/__test-utils__/cursor-hook-harness.js.map +1 -1
  29. package/dist/activities/execute-cursor/__test-utils__/gateway-substrate.js +2 -32
  30. package/dist/activities/execute-cursor/__test-utils__/gateway-substrate.js.map +1 -1
  31. package/dist/activities/execute-cursor/__test-utils__/hermetic-cursor.d.ts +148 -0
  32. package/dist/activities/execute-cursor/__test-utils__/hermetic-cursor.js +257 -0
  33. package/dist/activities/execute-cursor/__test-utils__/hermetic-cursor.js.map +1 -0
  34. package/dist/activities/execute-cursor/__test-utils__/scripted-agent.d.ts +183 -0
  35. package/dist/activities/execute-cursor/__test-utils__/scripted-agent.js +262 -0
  36. package/dist/activities/execute-cursor/__test-utils__/scripted-agent.js.map +1 -0
  37. package/dist/activities/execute-cursor/__test-utils__/scripted-sdk.d.ts +138 -0
  38. package/dist/activities/execute-cursor/__test-utils__/scripted-sdk.js +191 -0
  39. package/dist/activities/execute-cursor/__test-utils__/scripted-sdk.js.map +1 -0
  40. package/dist/activities/execute-cursor/adapter.d.ts +55 -0
  41. package/dist/activities/execute-cursor/adapter.js +119 -0
  42. package/dist/activities/execute-cursor/adapter.js.map +1 -0
  43. package/dist/activities/execute-cursor/approval-state.d.ts +2 -2
  44. package/dist/activities/execute-cursor/approval-state.js.map +1 -1
  45. package/dist/activities/execute-cursor/capture-flow.d.ts +15 -30
  46. package/dist/activities/execute-cursor/capture-flow.js +17 -26
  47. package/dist/activities/execute-cursor/capture-flow.js.map +1 -1
  48. package/dist/activities/execute-cursor/cursor-capabilities.d.ts +23 -0
  49. package/dist/activities/execute-cursor/cursor-capabilities.js +33 -0
  50. package/dist/activities/execute-cursor/cursor-capabilities.js.map +1 -0
  51. package/dist/activities/execute-cursor/cursor-mcp-config.d.ts +2 -2
  52. package/dist/activities/execute-cursor/cursor-mcp-config.js.map +1 -1
  53. package/dist/activities/execute-cursor/fetch-interceptor.d.ts +15 -25
  54. package/dist/activities/execute-cursor/fetch-interceptor.js +39 -33
  55. package/dist/activities/execute-cursor/fetch-interceptor.js.map +1 -1
  56. package/dist/activities/execute-cursor/http2-interceptor.d.ts +21 -17
  57. package/dist/activities/execute-cursor/http2-interceptor.js +38 -31
  58. package/dist/activities/execute-cursor/http2-interceptor.js.map +1 -1
  59. package/dist/activities/execute-cursor/message-translator.d.ts +28 -19
  60. package/dist/activities/execute-cursor/message-translator.js +76 -27
  61. package/dist/activities/execute-cursor/message-translator.js.map +1 -1
  62. package/dist/activities/execute-cursor/prompt-builder.d.ts +180 -11
  63. package/dist/activities/execute-cursor/prompt-builder.js +171 -0
  64. package/dist/activities/execute-cursor/prompt-builder.js.map +1 -1
  65. package/dist/activities/execute-cursor/rejection-capture.d.ts +3 -6
  66. package/dist/activities/execute-cursor/rejection-capture.js +5 -7
  67. package/dist/activities/execute-cursor/rejection-capture.js.map +1 -1
  68. package/dist/activities/execute-cursor/session-lifecycle.d.ts +1 -4
  69. package/dist/activities/execute-cursor/session-lifecycle.js.map +1 -1
  70. package/dist/activities/execute-cursor/turn-boundary.d.ts +1 -1
  71. package/dist/activities/execute-cursor/turn-settle.d.ts +49 -0
  72. package/dist/activities/execute-cursor/turn-settle.js +507 -0
  73. package/dist/activities/execute-cursor/turn-settle.js.map +1 -0
  74. package/dist/activities/execute-cursor/turn-setup.d.ts +173 -0
  75. package/dist/activities/execute-cursor/turn-setup.js +496 -0
  76. package/dist/activities/execute-cursor/turn-setup.js.map +1 -0
  77. package/dist/activities/execute-cursor/turn-stream.d.ts +41 -71
  78. package/dist/activities/execute-cursor/turn-stream.js +70 -135
  79. package/dist/activities/execute-cursor/turn-stream.js.map +1 -1
  80. package/dist/activities/execute-cursor/turn.d.ts +43 -0
  81. package/dist/activities/execute-cursor/turn.js +222 -0
  82. package/dist/activities/execute-cursor/turn.js.map +1 -0
  83. package/dist/activities/execute-cursor/usage-pricing.d.ts +38 -0
  84. package/dist/activities/execute-cursor/usage-pricing.js +50 -0
  85. package/dist/activities/execute-cursor/usage-pricing.js.map +1 -0
  86. package/dist/activities/execute-deep-agent/approval-file-change.d.ts +1 -1
  87. package/dist/activities/execute-deep-agent/approval-file-change.js +1 -1
  88. package/dist/activities/execute-deep-agent/environment.js +1 -1
  89. package/dist/activities/execute-deep-agent/environment.js.map +1 -1
  90. package/dist/activities/execute-deep-agent/setup.js +3 -2
  91. package/dist/activities/execute-deep-agent/setup.js.map +1 -1
  92. package/dist/activities/hydrate-workflow-execution.js +1 -1
  93. package/dist/activities/hydrate-workflow-execution.js.map +1 -1
  94. package/dist/config.d.ts +15 -0
  95. package/dist/config.js.map +1 -1
  96. package/dist/harness/capabilities.d.ts +87 -0
  97. package/dist/harness/capabilities.js +37 -0
  98. package/dist/harness/capabilities.js.map +1 -0
  99. package/dist/harness/persist-chokepoint.d.ts +74 -0
  100. package/dist/harness/persist-chokepoint.js +88 -0
  101. package/dist/harness/persist-chokepoint.js.map +1 -0
  102. package/dist/harness/registry.d.ts +106 -0
  103. package/dist/harness/registry.js +168 -0
  104. package/dist/harness/registry.js.map +1 -0
  105. package/dist/harness/run-turn.d.ts +53 -0
  106. package/dist/harness/run-turn.js +528 -0
  107. package/dist/harness/run-turn.js.map +1 -0
  108. package/dist/harness/stop-controller.d.ts +50 -0
  109. package/dist/harness/stop-controller.js +62 -0
  110. package/dist/harness/stop-controller.js.map +1 -0
  111. package/dist/harness/terminal-table.d.ts +141 -0
  112. package/dist/harness/terminal-table.js +239 -0
  113. package/dist/harness/terminal-table.js.map +1 -0
  114. package/dist/harness/turn-context.d.ts +523 -0
  115. package/dist/harness/turn-context.js +739 -0
  116. package/dist/harness/turn-context.js.map +1 -0
  117. package/dist/harness/types.d.ts +448 -0
  118. package/dist/harness/types.js +64 -0
  119. package/dist/harness/types.js.map +1 -0
  120. package/dist/harness/usage-accumulator.d.ts +68 -0
  121. package/dist/harness/usage-accumulator.js +101 -0
  122. package/dist/harness/usage-accumulator.js.map +1 -0
  123. package/dist/harness-adapters.d.ts +27 -0
  124. package/dist/harness-adapters.js +28 -0
  125. package/dist/harness-adapters.js.map +1 -0
  126. package/dist/main.js.map +1 -1
  127. package/dist/runner-manager.js +64 -48
  128. package/dist/runner-manager.js.map +1 -1
  129. package/dist/runner.d.ts +5 -4
  130. package/dist/runner.js +65 -64
  131. package/dist/runner.js.map +1 -1
  132. package/dist/shared/approval-policy.d.ts +1 -1
  133. package/dist/shared/approval-policy.js.map +1 -1
  134. package/dist/{activities/execute-cursor → shared}/attachment-resolver.d.ts +2 -2
  135. package/dist/{activities/execute-cursor → shared}/attachment-resolver.js +5 -5
  136. package/dist/shared/attachment-resolver.js.map +1 -0
  137. package/dist/{activities/execute-cursor → shared}/blueprint-resolver.d.ts +7 -5
  138. package/dist/{activities/execute-cursor → shared}/blueprint-resolver.js +3 -63
  139. package/dist/shared/blueprint-resolver.js.map +1 -0
  140. package/dist/shared/channel-attachment.d.ts +1 -1
  141. package/dist/shared/channel-attachment.js.map +1 -1
  142. package/dist/{activities/execute-cursor → shared}/cost-guard.d.ts +5 -1
  143. package/dist/{activities/execute-cursor → shared}/cost-guard.js +5 -1
  144. package/dist/shared/cost-guard.js.map +1 -0
  145. package/dist/{activities/execute-cursor → shared}/env-resolver.d.ts +1 -1
  146. package/dist/shared/env-resolver.js.map +1 -0
  147. package/dist/{activities/execute-cursor → shared}/exact-apply.d.ts +1 -1
  148. package/dist/{activities/execute-cursor → shared}/exact-apply.js +6 -6
  149. package/dist/shared/exact-apply.js.map +1 -0
  150. package/dist/shared/execution-context.d.ts +35 -0
  151. package/dist/shared/execution-context.js +38 -0
  152. package/dist/shared/execution-context.js.map +1 -0
  153. package/dist/{activities/execute-cursor → shared}/extract-structured-output.d.ts +5 -2
  154. package/dist/{activities/execute-cursor → shared}/extract-structured-output.js +9 -6
  155. package/dist/shared/extract-structured-output.js.map +1 -0
  156. package/dist/shared/filereview/capture.d.ts +1 -1
  157. package/dist/shared/filereview/capture.js +1 -1
  158. package/dist/shared/heartbeat.d.ts +13 -11
  159. package/dist/shared/heartbeat.js +14 -29
  160. package/dist/shared/heartbeat.js.map +1 -1
  161. package/dist/{activities/execute-cursor → shared}/persist-decision.d.ts +5 -2
  162. package/dist/{activities/execute-cursor → shared}/persist-decision.js +4 -1
  163. package/dist/shared/persist-decision.js.map +1 -0
  164. package/dist/{activities/execute-cursor → shared}/skill-resolver.d.ts +16 -3
  165. package/dist/{activities/execute-cursor → shared}/skill-resolver.js +12 -4
  166. package/dist/shared/skill-resolver.js.map +1 -0
  167. package/dist/shared/subagent-rows.d.ts +21 -0
  168. package/dist/shared/subagent-rows.js +34 -0
  169. package/dist/shared/subagent-rows.js.map +1 -0
  170. package/dist/shared/worker-shutdown.d.ts +43 -16
  171. package/dist/shared/worker-shutdown.js +28 -13
  172. package/dist/shared/worker-shutdown.js.map +1 -1
  173. package/dist/shared/workspace/platform-dir.d.ts +1 -1
  174. package/dist/shared/workspace/platform-dir.js +1 -1
  175. package/dist/shared/workspace/session-provision.d.ts +54 -0
  176. package/dist/{activities/execute-cursor/workspace-provision.js → shared/workspace/session-provision.js} +15 -12
  177. package/dist/shared/workspace/session-provision.js.map +1 -0
  178. package/package.json +4 -4
  179. package/src/__test-utils__/__tests__/harness-contract-self-check.test.ts +325 -0
  180. package/src/__test-utils__/config-fixture.ts +63 -0
  181. package/src/__test-utils__/execution-record-fixture.ts +104 -0
  182. package/src/__test-utils__/harness-boot-order-child.ts +58 -0
  183. package/src/__test-utils__/harness-contract/contract.ts +581 -0
  184. package/src/__test-utils__/harness-contract/recording-sink.ts +107 -0
  185. package/src/__test-utils__/harness-contract/runtime-contract.ts +666 -0
  186. package/src/__test-utils__/harness-contract/scripted-adapter.ts +349 -0
  187. package/src/__test-utils__/harness-contract/types.ts +176 -0
  188. package/src/__test-utils__/hermetic-activity.ts +502 -0
  189. package/src/__test-utils__/model-registry-fixture.ts +72 -0
  190. package/src/__test-utils__/module-specifiers.ts +82 -0
  191. package/src/__test-utils__/proto-helpers.ts +25 -0
  192. package/src/__test-utils__/turn-input-fixture.ts +155 -0
  193. package/src/__tests__/harness-boot-order.test.ts +77 -0
  194. package/src/__tests__/harness-contract.test.ts +27 -0
  195. package/src/__tests__/runner-manager.test.ts +18 -0
  196. package/src/activities/execute-cursor/__test-utils__/__tests__/scripted-double.test.ts +102 -0
  197. package/src/activities/execute-cursor/__test-utils__/contract-subject.ts +396 -0
  198. package/src/activities/execute-cursor/__test-utils__/cursor-hook-harness.ts +66 -0
  199. package/src/activities/execute-cursor/__test-utils__/gateway-substrate.ts +2 -43
  200. package/src/activities/execute-cursor/__test-utils__/hermetic-cursor.ts +342 -0
  201. package/src/activities/execute-cursor/__test-utils__/scripted-agent.ts +368 -0
  202. package/src/activities/execute-cursor/__test-utils__/scripted-sdk.ts +254 -0
  203. package/src/activities/execute-cursor/__tests__/adapter-is-temporal-free.test.ts +45 -0
  204. package/src/activities/execute-cursor/__tests__/approval-decisions-agree.test.ts +90 -0
  205. package/src/activities/execute-cursor/__tests__/build-prompt.test.ts +7 -8
  206. package/src/activities/execute-cursor/__tests__/capture-flow.test.ts +28 -1
  207. package/src/activities/execute-cursor/__tests__/deny-gate-exact-apply.test.ts +1 -1
  208. package/src/activities/execute-cursor/__tests__/fetch-interceptor.test.ts +64 -6
  209. package/src/activities/execute-cursor/__tests__/hermetic/deny-and-retry.test.ts +228 -0
  210. package/src/activities/execute-cursor/__tests__/hermetic/file-review-capture.test.ts +180 -0
  211. package/src/activities/execute-cursor/__tests__/hermetic/goldens/cost-cap.status.json +53 -0
  212. package/src/activities/execute-cursor/__tests__/hermetic/goldens/deny-and-retry.turn1.status.json +55 -0
  213. package/src/activities/execute-cursor/__tests__/hermetic/goldens/deny-and-retry.turn2.status.json +77 -0
  214. package/src/activities/execute-cursor/__tests__/hermetic/goldens/file-review-capture.status.json +126 -0
  215. package/src/activities/execute-cursor/__tests__/hermetic/goldens/pause.status.json +40 -0
  216. package/src/activities/execute-cursor/__tests__/hermetic/goldens/plain-turn.status.json +48 -0
  217. package/src/activities/execute-cursor/__tests__/hermetic/goldens/platform-stop.status.json +56 -0
  218. package/src/activities/execute-cursor/__tests__/hermetic/goldens/recovery-fresh-agent.status.json +53 -0
  219. package/src/activities/execute-cursor/__tests__/hermetic/goldens/recovery-transport-timeout.status.json +48 -0
  220. package/src/activities/execute-cursor/__tests__/hermetic/goldens/resolution-error.status.json +18 -0
  221. package/src/activities/execute-cursor/__tests__/hermetic/goldens/run-cancelled.status.json +36 -0
  222. package/src/activities/execute-cursor/__tests__/hermetic/goldens/run-error-non-retryable.status.json +37 -0
  223. package/src/activities/execute-cursor/__tests__/hermetic/goldens/sdk-error-at-create.status.json +38 -0
  224. package/src/activities/execute-cursor/__tests__/hermetic/goldens/stall.status.json +59 -0
  225. package/src/activities/execute-cursor/__tests__/hermetic/goldens/tool-call.status.json +68 -0
  226. package/src/activities/execute-cursor/__tests__/hermetic/goldens/unattributed-hook-block.status.json +79 -0
  227. package/src/activities/execute-cursor/__tests__/hermetic/goldens/worker-shutdown.status.json +42 -0
  228. package/src/activities/execute-cursor/__tests__/hermetic/harness-contract.test.ts +170 -0
  229. package/src/activities/execute-cursor/__tests__/hermetic/pause-vs-shutdown.test.ts +205 -0
  230. package/src/activities/execute-cursor/__tests__/hermetic/plain-turn.test.ts +171 -0
  231. package/src/activities/execute-cursor/__tests__/hermetic/recovery-fresh-agent.test.ts +156 -0
  232. package/src/activities/execute-cursor/__tests__/hermetic/run-wait-arms.test.ts +286 -0
  233. package/src/activities/execute-cursor/__tests__/hermetic/stream-self-stop-arms.test.ts +332 -0
  234. package/src/activities/execute-cursor/__tests__/hermetic/thrown-error-arms.test.ts +193 -0
  235. package/src/activities/execute-cursor/__tests__/hermetic/tool-call.test.ts +137 -0
  236. package/src/activities/execute-cursor/__tests__/hermetic/unattributed-hook-block.test.ts +185 -0
  237. package/src/activities/execute-cursor/__tests__/hermetic/workspace-lock-timeout.test.ts +151 -0
  238. package/src/activities/execute-cursor/__tests__/http2-interceptor.test.ts +65 -35
  239. package/src/activities/execute-cursor/__tests__/message-translator.test.ts +0 -40
  240. package/src/activities/execute-cursor/__tests__/same-identity-reproposal.test.ts +171 -0
  241. package/src/activities/execute-cursor/__tests__/turn-stream.test.ts +118 -243
  242. package/src/activities/execute-cursor/__tests__/usage-pricing.test.ts +84 -0
  243. package/src/activities/execute-cursor/adapter.ts +138 -0
  244. package/src/activities/execute-cursor/approval-state.ts +2 -2
  245. package/src/activities/execute-cursor/capture-flow.ts +18 -41
  246. package/src/activities/execute-cursor/cursor-capabilities.ts +35 -0
  247. package/src/activities/execute-cursor/cursor-mcp-config.ts +2 -2
  248. package/src/activities/execute-cursor/fetch-interceptor.ts +48 -45
  249. package/src/activities/execute-cursor/http2-interceptor.ts +47 -34
  250. package/src/activities/execute-cursor/message-translator.ts +90 -45
  251. package/src/activities/execute-cursor/prompt-builder.ts +327 -12
  252. package/src/activities/execute-cursor/rejection-capture.ts +5 -12
  253. package/src/activities/execute-cursor/session-lifecycle.ts +1 -5
  254. package/src/activities/execute-cursor/turn-boundary.ts +1 -1
  255. package/src/activities/execute-cursor/turn-settle.ts +601 -0
  256. package/src/activities/execute-cursor/turn-setup.ts +645 -0
  257. package/src/activities/execute-cursor/turn-stream.ts +84 -210
  258. package/src/activities/execute-cursor/turn.ts +240 -0
  259. package/src/activities/execute-cursor/usage-pricing.ts +67 -0
  260. package/src/activities/execute-deep-agent/approval-file-change.ts +1 -1
  261. package/src/activities/execute-deep-agent/environment.ts +1 -1
  262. package/src/activities/execute-deep-agent/setup.ts +3 -2
  263. package/src/activities/hydrate-workflow-execution.ts +1 -1
  264. package/src/config.ts +13 -0
  265. package/src/harness/__tests__/import-direction.test.ts +168 -0
  266. package/src/harness/__tests__/persist-chokepoint.test.ts +146 -0
  267. package/src/harness/__tests__/registry.test.ts +168 -0
  268. package/src/harness/__tests__/run-turn.test.ts +165 -0
  269. package/src/harness/__tests__/stop-controller.test.ts +49 -0
  270. package/src/harness/__tests__/turn-context.test.ts +178 -0
  271. package/src/harness/__tests__/usage-accumulator.test.ts +83 -0
  272. package/src/harness/capabilities.ts +92 -0
  273. package/src/harness/persist-chokepoint.ts +112 -0
  274. package/src/harness/registry.ts +197 -0
  275. package/src/harness/run-turn.ts +618 -0
  276. package/src/harness/stop-controller.ts +83 -0
  277. package/src/harness/terminal-table.ts +276 -0
  278. package/src/harness/turn-context.ts +1028 -0
  279. package/src/harness/types.ts +473 -0
  280. package/src/harness/usage-accumulator.ts +122 -0
  281. package/src/harness-adapters.ts +30 -0
  282. package/src/main.ts +1 -1
  283. package/src/runner-manager.ts +68 -61
  284. package/src/runner.ts +67 -78
  285. package/src/shared/__tests__/attachment-naming.test.ts +1 -1
  286. package/src/{activities/execute-cursor → shared}/__tests__/attachment-resolver.test.ts +6 -4
  287. package/src/{activities/execute-cursor → shared}/__tests__/cost-guard.test.ts +2 -1
  288. package/src/{activities/execute-cursor → shared}/__tests__/exact-apply.test.ts +3 -3
  289. package/src/{activities/execute-cursor → shared}/__tests__/extract-structured-output.test.ts +8 -8
  290. package/src/{activities/execute-cursor → shared}/__tests__/persist-decision.test.ts +4 -2
  291. package/src/{activities/execute-cursor → shared}/__tests__/skill-resolver.test.ts +1 -1
  292. package/src/shared/__tests__/subagent-rows.test.ts +34 -0
  293. package/src/shared/__tests__/worker-shutdown.test.ts +44 -39
  294. package/src/shared/approval-policy.ts +1 -1
  295. package/src/{activities/execute-cursor → shared}/attachment-resolver.ts +6 -6
  296. package/src/{activities/execute-cursor → shared}/blueprint-resolver.ts +7 -78
  297. package/src/shared/channel-attachment.ts +1 -1
  298. package/src/{activities/execute-cursor → shared}/cost-guard.ts +5 -1
  299. package/src/{activities/execute-cursor → shared}/env-resolver.ts +1 -1
  300. package/src/{activities/execute-cursor → shared}/exact-apply.ts +7 -7
  301. package/src/shared/execution-context.ts +45 -0
  302. package/src/{activities/execute-cursor → shared}/extract-structured-output.ts +10 -7
  303. package/src/shared/filereview/capture.ts +1 -1
  304. package/src/shared/heartbeat.ts +13 -33
  305. package/src/{activities/execute-cursor → shared}/persist-decision.ts +5 -2
  306. package/src/{activities/execute-cursor → shared}/skill-resolver.ts +20 -6
  307. package/src/shared/subagent-rows.ts +39 -0
  308. package/src/shared/worker-shutdown.ts +55 -25
  309. package/src/{activities/execute-cursor/__tests__/workspace-provision.test.ts → shared/workspace/__tests__/session-provision.test.ts} +25 -28
  310. package/src/shared/workspace/platform-dir.ts +1 -1
  311. package/src/{activities/execute-cursor/workspace-provision.ts → shared/workspace/session-provision.ts} +29 -16
  312. package/dist/activities/execute-cursor/attachment-resolver.js.map +0 -1
  313. package/dist/activities/execute-cursor/blueprint-resolver.js.map +0 -1
  314. package/dist/activities/execute-cursor/cost-guard.js.map +0 -1
  315. package/dist/activities/execute-cursor/env-resolver.js.map +0 -1
  316. package/dist/activities/execute-cursor/exact-apply.js.map +0 -1
  317. package/dist/activities/execute-cursor/extract-structured-output.js.map +0 -1
  318. package/dist/activities/execute-cursor/index.d.ts +0 -209
  319. package/dist/activities/execute-cursor/index.js +0 -2345
  320. package/dist/activities/execute-cursor/index.js.map +0 -1
  321. package/dist/activities/execute-cursor/persist-decision.js.map +0 -1
  322. package/dist/activities/execute-cursor/skill-resolver.js.map +0 -1
  323. package/dist/activities/execute-cursor/usage-accumulator.d.ts +0 -89
  324. package/dist/activities/execute-cursor/usage-accumulator.js +0 -121
  325. package/dist/activities/execute-cursor/usage-accumulator.js.map +0 -1
  326. package/dist/activities/execute-cursor/workspace-provision.d.ts +0 -42
  327. package/dist/activities/execute-cursor/workspace-provision.js.map +0 -1
  328. package/src/activities/execute-cursor/__tests__/usage-accumulator.test.ts +0 -164
  329. package/src/activities/execute-cursor/index.ts +0 -2876
  330. package/src/activities/execute-cursor/usage-accumulator.ts +0 -169
  331. /package/dist/{activities/execute-cursor → shared}/env-resolver.js +0 -0
@@ -0,0 +1,396 @@
1
+ /**
2
+ * The Cursor harness as a `HarnessContractSubject` — the REAL `createCursorAdapter()`
3
+ * driven through the harness contract kit, with the kit's engine-neutral
4
+ * scenario vocabulary translated onto S0's scripted `@cursor/sdk` double.
5
+ *
6
+ * The sibling of `gateway-substrate.ts` (the Cursor side of the approval
7
+ * contract kit): the kit owns the invariants and the runtime stand-in, this
8
+ * module owns the ENGINE — what the next turn of a session's agent will do —
9
+ * and reports what the double observed. The adapter under test is the
10
+ * production one, untouched; it imports no `@temporalio/*` (the fence test
11
+ * pins that), which is what lets the kit run it outside any activity
12
+ * context.
13
+ *
14
+ * ── The engine is per session ───────────────────────────────────────────────
15
+ * One scripted agent per session, minted when the kit first arranges a turn
16
+ * for it and handed out by the double's `Agent.create` for THAT session (keyed
17
+ * by the SDK's `platform.workspaceRef`, computed the way the adapter computes
18
+ * it), then parked by the real session cache across turns exactly as in a live
19
+ * worker. Arranging a turn REPLACES the agent's next script, so a turn the
20
+ * adapter interrupts before its `send()` leaves no stale script for the
21
+ * session's next turn. Every session also gets the workspace directory the
22
+ * runtime's provisioner would give it (`<workspaceRootDir>/sessions/<id>`), so
23
+ * both halves of the kit gate in the same directory shape; the hook's
24
+ * active-turn pointer is per workspace, so concurrent sessions cannot repoint
25
+ * each other's hook.
26
+ *
27
+ * ── The translation, one rule per word ──────────────────────────────────────
28
+ * - `say(text)` → an `assistant` event. A first send opens with the
29
+ * `system/init` event; a resumed send does not (the
30
+ * `deny-and-retry` turn-2 script is the precedent).
31
+ * - `propose(id, a)` → the STREAM tool call (`edit` / `shell` / `delete`, the
32
+ * kits' shared translation in `cursor-hook-harness.ts`),
33
+ * an `effect` that runs the REAL preToolUse hook the
34
+ * adapter installed against the session's workspace,
35
+ * and the call's completion event. `executionCount(id)`
36
+ * is the hook's `allow` answers for `id` — the SDK
37
+ * executes a tool iff the hook allows it, so this is the
38
+ * safety-critical observable. WHETHER the call is
39
+ * emitted is decided from the kit's view and this
40
+ * subject's own memory, as the model and the SDK would
41
+ * decide it (see {@link CursorContractSubject.proposeSteps}).
42
+ * - `read(id, path)` → the `read` tool call, the REAL hook (which allows it —
43
+ * a read is never gated) and the completion; counted as
44
+ * run. The discrete event the adapter's loop flushes a
45
+ * persist on.
46
+ * - `usage(delta)` → a `turn-ended` delta; the adapter prices it.
47
+ * - `hang` → an `effect` parked on the run's own `cancelled`
48
+ * status, which the adapter's stop-signal listener
49
+ * (`turn-stream.ts` `cancelOnStop`) produces. Never a
50
+ * timer. Resolves `whenHanging`.
51
+ * - `fail(msg, s)` → `engine`: the run ends `{ status: "error" }` and
52
+ * `run.wait()` carries the message; `internal`: an
53
+ * `effect` throws mid-stream (the adapter's catch
54
+ * classifies it). `actionable` is refused as a test bug:
55
+ * only the unattributed-hook-block path produces that
56
+ * surface, and the kit's invariants do not ask for it.
57
+ * - `cancelled` → an `effect` calling `run.cancel()` from the SDK side
58
+ * (the `run-cancelled` golden's shape).
59
+ * Every script that does not end in `fail`, `cancelled` or `hang` closes with
60
+ * `finished({ result: <last say> })` so `run.wait()` has an answer.
61
+ *
62
+ * ── Two modelling decisions (S2 M4, Q-M4-4) ─────────────────────────────────
63
+ * A `propose` for an id this subject has already seen the hook ALLOW emits
64
+ * nothing: the real SDK does not re-run a completed tool on resume — the parked
65
+ * agent's conversation already holds the tool's result. A `propose` whose id
66
+ * carries a SKIP or REJECT decision emits nothing either: the resume prompt
67
+ * tells the model the user declined the action and an obedient model moves
68
+ * on. Both are what "the engine re-reaches the same call" and "the turn
69
+ * continues" MEAN for a deny-and-retry harness; neither hides anything from
70
+ * the safety observable, because a fresh identical command from a
71
+ * disobedient model IS a new gated act and Cursor re-gates it — the subject
72
+ * file proves that with {@link CursorContractSubject.forceReissue}.
73
+ *
74
+ * The completion event a `propose` emits is scripted from the answer the hook
75
+ * is EXPECTED to give (deny for an undecided proposal, allow for an approved
76
+ * re-issue); the hook's actual answer is recorded at effect time and any
77
+ * disagreement is kept in {@link CursorContractSubject.hookDisagreements} for
78
+ * the subject file to assert empty — a hook that allows an undecided action
79
+ * would show up there AND in `executionCount`, never be papered over.
80
+ */
81
+
82
+ import { mkdirSync } from "node:fs";
83
+ import { join } from "node:path";
84
+ import { ApprovalAction, ServiceTier, ThinkingMode } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/enum_pb";
85
+
86
+ import type { Config } from "../../../config.js";
87
+ import type { HarnessAdapter } from "../../../harness/types.js";
88
+ import type { ProposedAction } from "../../../__test-utils__/approval-contract/types.js";
89
+ import type {
90
+ EngineView,
91
+ HarnessContractSubject,
92
+ ScenarioStep,
93
+ SubjectInputOverrides,
94
+ TurnScenario,
95
+ } from "../../../__test-utils__/harness-contract/types.js";
96
+ import type { HermeticEnvironment } from "../../../__test-utils__/hermetic-activity.js";
97
+ import { createCursorAdapter } from "../adapter.js";
98
+ import { resolvePlatformOptions } from "../session-lifecycle.js";
99
+ import { hookInputFor, streamArgsFor, STREAM_NAME, type GatedActionKind } from "./cursor-hook-harness.js";
100
+ import { FIXTURE, SDK_CATALOG, hermeticCursorConfig, runWorkspaceHook } from "./hermetic-cursor.js";
101
+ import { ScriptedCursorAgent, sdkEvents, step, type ScriptStep, type ScriptedRun } from "./scripted-agent.js";
102
+ import { ScriptedCursorSdk, bindScriptedSdk } from "./scripted-sdk.js";
103
+
104
+ /** The user's message every kit turn asks; the prompt builder needs one and the kit has none to give. */
105
+ const KIT_USER_MESSAGE = "Do what the scenario says.";
106
+
107
+ /** What the double records the SDK "returning" for an allowed tool call. */
108
+ const ALLOWED_RESULT = "ok (kit)";
109
+
110
+ /** The error the SDK surfaces on a hook-denied call, as the goldens script it. */
111
+ const HOOK_BLOCKED_RESULT = "Blocked by hook";
112
+
113
+ /** What this subject remembers about one proposal id across the turns of an execution. */
114
+ interface ProposalMemory {
115
+ /** How many times the call has been emitted; the first emission uses the kit's id, a re-issue a fresh one. */
116
+ emitted: number;
117
+ /** How many times the hook allowed it — the execution count. */
118
+ allowed: number;
119
+ }
120
+
121
+ export interface CursorContractSubject extends HarnessContractSubject {
122
+ readonly adapter: HarnessAdapter;
123
+ /** The SDK double the adapter is talking to, for the subject file's own observations. */
124
+ readonly sdk: ScriptedCursorSdk;
125
+ /** The scripted agent minted for a session, once the kit has arranged a turn for it. */
126
+ agentFor(sessionId: string): ScriptedCursorAgent | undefined;
127
+ /**
128
+ * Make the NEXT `propose` of `toolCallId` emit its call whatever the
129
+ * decision or the memory says — the disobedient model, for the observation
130
+ * the kit's vocabulary cannot express.
131
+ */
132
+ forceReissue(toolCallId: string): void;
133
+ /** Every proposal at which the hook answered other than the script expected; the subject file asserts it stays empty. */
134
+ readonly hookDisagreements: readonly string[];
135
+ }
136
+
137
+ /**
138
+ * Build the subject over one hermetic environment (the temp `HOME` the gate's
139
+ * per-session HITL artifacts land under, the temp workspace root). The
140
+ * environment is the caller's to create before and dispose after; the SDK
141
+ * double is bound for the module here, so a file holds one subject.
142
+ */
143
+ export function createCursorContractSubject(env: HermeticEnvironment): CursorContractSubject {
144
+ return new CursorSubject(env);
145
+ }
146
+
147
+ class CursorSubject implements CursorContractSubject {
148
+ readonly name = "cursor";
149
+ readonly harness = "cursor" as const;
150
+ readonly adapter: HarnessAdapter;
151
+ readonly config: Config;
152
+ readonly sdk: ScriptedCursorSdk;
153
+ readonly hookDisagreements: string[] = [];
154
+
155
+ private readonly agents = new Map<string, ScriptedCursorAgent>();
156
+ private readonly agentsByWorkspaceRef = new Map<string, ScriptedCursorAgent>();
157
+ private readonly proposals = new Map<string, ProposalMemory>();
158
+ private readonly forcedReissues = new Set<string>();
159
+ private readonly hangWaiters: Array<() => void> = [];
160
+ private mintCounter = 0;
161
+
162
+ constructor(private readonly env: HermeticEnvironment) {
163
+ this.config = hermeticCursorConfig(env);
164
+ this.adapter = createCursorAdapter();
165
+ this.sdk = new ScriptedCursorSdk({
166
+ agents: [],
167
+ catalog: SDK_CATALOG,
168
+ agentForWorkspaceRef: (ref) => this.agentsByWorkspaceRef.get(ref),
169
+ });
170
+ bindScriptedSdk(this.sdk);
171
+ }
172
+
173
+ // ── HarnessContractSubject ────────────────────────────────────────────────
174
+
175
+ arrange(turn: TurnScenario, view: EngineView): void {
176
+ const agent = this.agents.get(view.sessionId) ?? this.mintAgent(view.sessionId);
177
+ mkdirSync(this.workspaceRootOf(view.sessionId), { recursive: true });
178
+ agent.arrangeNextTurn(this.translate(turn, view, agent));
179
+ }
180
+
181
+ inputOverrides(view: EngineView): SubjectInputOverrides {
182
+ return {
183
+ // The adapter validates the name against its catalog and prices against
184
+ // the registry fixture; both know exactly this model.
185
+ model: { requested: FIXTURE.model, serviceTier: ServiceTier.STANDARD, thinkingMode: ThinkingMode.DISABLED },
186
+ workspaceDir: this.workspaceRootOf(view.sessionId),
187
+ message: KIT_USER_MESSAGE,
188
+ };
189
+ }
190
+
191
+ whenHanging(): Promise<void> {
192
+ return new Promise((resolve) => this.hangWaiters.push(resolve));
193
+ }
194
+
195
+ executionCount(toolCallId: string): number {
196
+ return this.proposals.get(toolCallId)?.allowed ?? 0;
197
+ }
198
+
199
+ // ── The subject file's observations ──────────────────────────────────────
200
+
201
+ agentFor(sessionId: string): ScriptedCursorAgent | undefined {
202
+ return this.agents.get(sessionId);
203
+ }
204
+
205
+ forceReissue(toolCallId: string): void {
206
+ this.forcedReissues.add(toolCallId);
207
+ }
208
+
209
+ // ── The engine ────────────────────────────────────────────────────────────
210
+
211
+ /** The directory the runtime's provisioner gives a session with no workspace entries (`session-root.ts`). */
212
+ private workspaceRootOf(sessionId: string): string {
213
+ return join(this.env.workspaceRootDir, "sessions", sessionId);
214
+ }
215
+
216
+ private mintAgent(sessionId: string): ScriptedCursorAgent {
217
+ const agent = new ScriptedCursorAgent({ agentId: `agent-kit-${++this.mintCounter}`, turns: [] });
218
+ this.agents.set(sessionId, agent);
219
+ // The adapter asks the SDK to create an agent for the session under this
220
+ // ref; the double answers with this agent.
221
+ const { workspaceRef } = resolvePlatformOptions(sessionId, this.config.workspaceRootDir);
222
+ this.agentsByWorkspaceRef.set(workspaceRef, agent);
223
+ return agent;
224
+ }
225
+
226
+ private translate(turn: TurnScenario, view: EngineView, agent: ScriptedCursorAgent): ScriptStep[] {
227
+ const ev = sdkEvents(agent.agentId, agent.nextRunId);
228
+ const script: ScriptStep[] = [];
229
+ if (agent.sends.length === 0) script.push(step.event(ev.init()));
230
+
231
+ let lastSay = "";
232
+ let ended = false;
233
+ for (const s of turn) {
234
+ switch (s.kind) {
235
+ case "say":
236
+ script.push(step.event(ev.assistant(s.text)));
237
+ lastSay = s.text;
238
+ break;
239
+ case "propose":
240
+ script.push(...this.proposeSteps(s.toolCallId, s.action, view, ev));
241
+ break;
242
+ case "read":
243
+ script.push(...this.readSteps(s.toolCallId, s.path, view, ev));
244
+ break;
245
+ case "usage":
246
+ script.push(
247
+ step.turnEnded({
248
+ inputTokens: s.delta.inputTokens,
249
+ outputTokens: s.delta.outputTokens,
250
+ cacheReadTokens: s.delta.cacheReadTokens,
251
+ cacheWriteTokens: s.delta.cacheWriteTokens,
252
+ }),
253
+ );
254
+ break;
255
+ case "hang":
256
+ script.push(step.effect("kit: hang until the adapter cancels the run", ({ run }) => this.park(run)));
257
+ ended = true;
258
+ break;
259
+ case "fail":
260
+ switch (s.surface) {
261
+ case "engine":
262
+ script.push(step.errored({ result: s.message }));
263
+ break;
264
+ case "internal":
265
+ script.push(
266
+ step.effect("kit: the transport breaks mid-stream", () => {
267
+ throw new Error(s.message);
268
+ }),
269
+ );
270
+ break;
271
+ case "actionable":
272
+ throw new Error(`${this.name}: fail(..., "actionable") is not producible by this subject (only the unattributed hook block reaches that surface); test bug`);
273
+ default: {
274
+ const exhaustive: never = s.surface;
275
+ throw new Error(`${this.name}: unknown failure surface ${String(exhaustive)}`);
276
+ }
277
+ }
278
+ ended = true;
279
+ break;
280
+ case "cancelled":
281
+ script.push(step.effect("kit: the SDK cancels its own run", ({ run }) => run.cancel()));
282
+ ended = true;
283
+ break;
284
+ default: {
285
+ const exhaustive: never = s;
286
+ throw new Error(`${this.name}: unknown scenario step ${JSON.stringify(exhaustive)}`);
287
+ }
288
+ }
289
+ }
290
+ if (!ended) script.push(step.finished({ result: lastSay, model: { id: FIXTURE.model, params: [] } }));
291
+ return script;
292
+ }
293
+
294
+ /**
295
+ * Whether the model emits the gated call this turn, and what the hook is
296
+ * expected to answer — one rule per case (see the header). `undefined`
297
+ * means "nothing is emitted".
298
+ */
299
+ private emissionFor(toolCallId: string, view: EngineView): { readonly expectAllow: boolean } | undefined {
300
+ if (this.forcedReissues.delete(toolCallId)) {
301
+ // The disobedient model: emitted regardless; the hook decides on its own
302
+ // (a grant exists only for an APPROVE), so what to expect is the grant.
303
+ return { expectAllow: view.approvalDecisions.get(toolCallId) === ApprovalAction.APPROVE };
304
+ }
305
+ if ((this.proposals.get(toolCallId)?.allowed ?? 0) > 0) return undefined;
306
+ const decision = view.approvalDecisions.get(toolCallId) ?? ApprovalAction.UNSPECIFIED;
307
+ switch (decision) {
308
+ case ApprovalAction.UNSPECIFIED:
309
+ return { expectAllow: false };
310
+ case ApprovalAction.APPROVE:
311
+ case ApprovalAction.APPROVE_ALL:
312
+ return { expectAllow: true };
313
+ case ApprovalAction.SKIP:
314
+ case ApprovalAction.REJECT:
315
+ return undefined;
316
+ default: {
317
+ const exhaustive: never = decision;
318
+ throw new Error(`${this.name}: unknown approval action ${String(exhaustive)}`);
319
+ }
320
+ }
321
+ }
322
+
323
+ private proposeSteps(toolCallId: string, action: ProposedAction, view: EngineView, ev: ReturnType<typeof sdkEvents>): ScriptStep[] {
324
+ const emission = this.emissionFor(toolCallId, view);
325
+ if (!emission) return [];
326
+ if (action.kind === "read" || action.kind === "mcp") {
327
+ throw new Error(`${this.name}: propose(${action.kind}) is not a gated built-in on this harness; test bug`);
328
+ }
329
+ const gated: ProposedAction & { kind: GatedActionKind } = { ...action, kind: action.kind };
330
+ const memory = this.proposals.get(toolCallId) ?? { emitted: 0, allowed: 0 };
331
+ this.proposals.set(toolCallId, memory);
332
+ // The first emission carries the kit's id (the WAITING row the kit looks
333
+ // up); a re-issue carries a fresh call id, as the resumed SDK agent does,
334
+ // and the adapter reconciles it onto the committed row by identity.
335
+ const callId = memory.emitted === 0 ? toolCallId : `${toolCallId}#${memory.emitted}`;
336
+ memory.emitted += 1;
337
+ const name = STREAM_NAME[gated.kind];
338
+ const args = streamArgsFor(gated);
339
+ const workspaceRoot = this.workspaceRootOf(view.sessionId);
340
+ return [
341
+ step.event(ev.toolCall(callId, name, "running", args)),
342
+ step.effect(`kit: the real hook judges ${name} ${gated.resource}`, () => {
343
+ const { permission } = runWorkspaceHook(workspaceRoot, hookInputFor(gated));
344
+ if (permission === "allow") memory.allowed += 1;
345
+ if ((permission === "allow") !== emission.expectAllow) {
346
+ this.hookDisagreements.push(`${toolCallId}: expected ${emission.expectAllow ? "allow" : "deny"}, hook answered ${permission}`);
347
+ }
348
+ }),
349
+ emission.expectAllow
350
+ ? step.event(ev.toolCall(callId, name, "completed", args, ALLOWED_RESULT))
351
+ : step.event(ev.toolCall(callId, name, "error", args, HOOK_BLOCKED_RESULT)),
352
+ ];
353
+ }
354
+
355
+ /**
356
+ * An ungated read: the stream tool call, the REAL hook (which allows every
357
+ * read — the gate's baked-in category set has no arm for it; asserted
358
+ * through the disagreement record like every other hook answer), and the
359
+ * completion with contents. The SDK executes what the hook allows, so the
360
+ * read counts as run.
361
+ */
362
+ private readSteps(toolCallId: string, path: string, view: EngineView, ev: ReturnType<typeof sdkEvents>): ScriptStep[] {
363
+ const memory = this.proposals.get(toolCallId) ?? { emitted: 0, allowed: 0 };
364
+ this.proposals.set(toolCallId, memory);
365
+ memory.emitted += 1;
366
+ const args = { path };
367
+ const workspaceRoot = this.workspaceRootOf(view.sessionId);
368
+ return [
369
+ step.event(ev.toolCall(toolCallId, "read", "running", args)),
370
+ step.effect(`kit: the real hook judges read ${path}`, () => {
371
+ const { permission } = runWorkspaceHook(workspaceRoot, hookInputFor({ kind: "read", resource: path }));
372
+ if (permission === "allow") memory.allowed += 1;
373
+ else this.hookDisagreements.push(`${toolCallId}: expected allow for a read, hook answered ${permission}`);
374
+ }),
375
+ step.event(ev.toolCall(toolCallId, "read", "completed", args, `contents of ${path}`)),
376
+ ];
377
+ }
378
+
379
+ /**
380
+ * Park until the adapter cancels the run — the real shape of "the engine
381
+ * makes no progress until told to stop": the adapter's stop-signal listener
382
+ * calls `run.cancel()`, the double's status flips, and its stream breaks at
383
+ * the next step.
384
+ */
385
+ private park(run: ScriptedRun): Promise<void> {
386
+ for (const waiter of this.hangWaiters.splice(0)) waiter();
387
+ if (run.status === "cancelled") return Promise.resolve();
388
+ return new Promise((resolve) => {
389
+ const unsubscribe = run.onDidChangeStatus((status) => {
390
+ if (status !== "cancelled") return;
391
+ unsubscribe();
392
+ resolve();
393
+ });
394
+ });
395
+ }
396
+ }
@@ -25,6 +25,7 @@ import { generateHookScript } from "../hook-script.js";
25
25
  import { buildApprovalState, type ApprovalGrant, type McpToolPolicyEntry } from "../approval-state.js";
26
26
  import type { MergedToolPolicy, ApprovalCategory } from "../approval-policy.js";
27
27
  import { readCasObservations, type CasObservations } from "../cas-observations.js";
28
+ import type { ProposedAction } from "../../../__test-utils__/approval-contract/types.js";
28
29
 
29
30
  /**
30
31
  * Whether `bash` is available on this machine. Hook tests must be skipped where
@@ -231,3 +232,68 @@ export const hookMcp = (name: string, input: Record<string, unknown> = {}) => ({
231
232
  command: "npx -y srv mcp",
232
233
  hook_event_name: "beforeMCPExecution",
233
234
  });
235
+
236
+ // ── The contract kits' abstract action, in both of Cursor's taxonomies ──────
237
+ //
238
+ // Both kits (`approval-contract`, `harness-contract`) speak of a side effect as
239
+ // a taxonomy-free `ProposedAction`; Cursor sees the same act twice, as the
240
+ // STREAM tool call the SDK emits (`edit` / `shell` / `delete`, lowercase) and
241
+ // as the HOOK payload the preToolUse script judges (`Write` / `Shell` /
242
+ // `Delete`). One translation here, so every Cursor-side substrate and subject
243
+ // exercises the same category collapse that lets a stream-minted grant match a
244
+ // hook-named call.
245
+
246
+ /** Build the real preToolUse hook-input payload for an abstract action. */
247
+ export function hookInputFor(action: ProposedAction): object {
248
+ switch (action.kind) {
249
+ case "write":
250
+ return hookWrite(action.resource, action.content ?? "x");
251
+ case "shell":
252
+ return hookShell(action.resource);
253
+ case "delete":
254
+ return hookDelete(action.resource);
255
+ case "read":
256
+ return hookRead(action.resource);
257
+ case "mcp":
258
+ return hookMcp(action.mcpToolName ?? "mcp_tool");
259
+ default: {
260
+ const exhaustive: never = action.kind;
261
+ throw new Error(`hookInputFor: unknown action kind ${String(exhaustive)}`);
262
+ }
263
+ }
264
+ }
265
+
266
+ /** The gated built-in categories: the only action kinds a grant is ever minted for. */
267
+ export type GatedActionKind = "write" | "shell" | "delete";
268
+
269
+ /**
270
+ * Stream-side (SDK) tool name per gated category — deliberately the OTHER
271
+ * taxonomy from the hook input, so a grant minted from the stream matches a
272
+ * hook-named call only via the canonical category, not the raw name.
273
+ */
274
+ export const STREAM_NAME: Record<GatedActionKind, string> = {
275
+ write: "edit",
276
+ shell: "shell",
277
+ delete: "delete",
278
+ };
279
+
280
+ /**
281
+ * The stream-side args of a gated action, mirroring {@link hookInputFor}: a
282
+ * write carries whole-file content (so its digest is content-exact and matches
283
+ * the hook's), a delete carries only a path, and a shell carries its command
284
+ * (the salient is already exact). The shapes the goldens' `edit` events use.
285
+ */
286
+ export function streamArgsFor(action: ProposedAction & { kind: GatedActionKind }): Record<string, unknown> {
287
+ switch (action.kind) {
288
+ case "shell":
289
+ return { command: action.resource };
290
+ case "delete":
291
+ return { path: action.resource };
292
+ case "write":
293
+ return { path: action.resource, content: action.content ?? "x" };
294
+ default: {
295
+ const exhaustive: never = action.kind;
296
+ throw new Error(`streamArgsFor: unknown gated kind ${String(exhaustive)}`);
297
+ }
298
+ }
299
+ }
@@ -15,15 +15,7 @@
15
15
  * (`enforcesExactResource: true`) and implements `authorizeAfterGrant`.
16
16
  */
17
17
 
18
- import {
19
- setupCursorHookHarness,
20
- hasBash,
21
- hookWrite,
22
- hookShell,
23
- hookDelete,
24
- hookRead,
25
- hookMcp,
26
- } from "./cursor-hook-harness.js";
18
+ import { setupCursorHookHarness, hasBash, hookInputFor, streamArgsFor, STREAM_NAME } from "./cursor-hook-harness.js";
27
19
  import { toolIdentity, type ApprovalGrant } from "../approval-state.js";
28
20
  import { contentDigest } from "../../../shared/file-tools.js";
29
21
  import type { ApprovalCategory } from "../approval-policy.js";
@@ -34,31 +26,6 @@ import type {
34
26
  ProposedAction,
35
27
  } from "../../../__test-utils__/approval-contract/types.js";
36
28
 
37
- /** Build the real preToolUse hook-input payload for an abstract action. */
38
- function hookInputFor(action: ProposedAction): object {
39
- switch (action.kind) {
40
- case "write":
41
- return hookWrite(action.resource, action.content ?? "x");
42
- case "shell":
43
- return hookShell(action.resource);
44
- case "delete":
45
- return hookDelete(action.resource);
46
- case "read":
47
- return hookRead(action.resource);
48
- case "mcp":
49
- return hookMcp(action.mcpToolName ?? "mcp_tool");
50
- }
51
- }
52
-
53
- // Stream-side (SDK) tool name per gated category — deliberately the OTHER
54
- // taxonomy from the hook input, so a grant minted here matches a hook-named call
55
- // only via the canonical category, not the raw name.
56
- const STREAM_NAME: Record<"write" | "shell" | "delete", string> = {
57
- write: "edit",
58
- shell: "shell",
59
- delete: "delete",
60
- };
61
-
62
29
  /**
63
30
  * Mint the approval grant for an action using its stream-side identity. Only the
64
31
  * gated built-in categories are ever granted in the contract (read is never
@@ -69,15 +36,7 @@ function grantFor(action: ProposedAction): ApprovalGrant {
69
36
  throw new Error(`grantFor: ${action.kind} actions are not granted in the contract`);
70
37
  }
71
38
  const streamName = STREAM_NAME[action.kind];
72
- // Mirror hookInputFor: a write carries whole-file content (so its digest is
73
- // content-exact and matches the hook's), a delete carries only a path (no
74
- // content), and a shell carries its command (the salient is already exact).
75
- const args: Record<string, unknown> =
76
- action.kind === "shell"
77
- ? { command: action.resource }
78
- : action.kind === "delete"
79
- ? { path: action.resource }
80
- : { path: action.resource, content: action.content ?? "x" };
39
+ const args = streamArgsFor({ ...action, kind: action.kind });
81
40
  const identity = toolIdentity(streamName, "", args);
82
41
  return {
83
42
  toolName: streamName,