@namzu/sdk 72.0.0 → 74.0.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 (394) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/README.md +34 -34
  3. package/dist/agents/examples/SupervisorAgent.d.ts.map +1 -1
  4. package/dist/agents/examples/SupervisorAgent.js +5 -0
  5. package/dist/agents/examples/SupervisorAgent.js.map +1 -1
  6. package/dist/authorization/protected-paths.d.ts +7 -0
  7. package/dist/authorization/protected-paths.d.ts.map +1 -1
  8. package/dist/authorization/protected-paths.js +11 -0
  9. package/dist/authorization/protected-paths.js.map +1 -1
  10. package/dist/authorization/readonly-commands.d.ts +23 -0
  11. package/dist/authorization/readonly-commands.d.ts.map +1 -1
  12. package/dist/authorization/readonly-commands.js +89 -3
  13. package/dist/authorization/readonly-commands.js.map +1 -1
  14. package/dist/authorization/shell-paths.d.ts +8 -0
  15. package/dist/authorization/shell-paths.d.ts.map +1 -1
  16. package/dist/authorization/shell-paths.js +125 -2
  17. package/dist/authorization/shell-paths.js.map +1 -1
  18. package/dist/bridge/acp/permission.d.ts +6 -0
  19. package/dist/bridge/acp/permission.d.ts.map +1 -1
  20. package/dist/bridge/acp/permission.js.map +1 -1
  21. package/dist/bridge/acp/server.d.ts.map +1 -1
  22. package/dist/bridge/acp/server.js +21 -3
  23. package/dist/bridge/acp/server.js.map +1 -1
  24. package/dist/compaction/context-window.d.ts.map +1 -1
  25. package/dist/compaction/context-window.js +11 -0
  26. package/dist/compaction/context-window.js.map +1 -1
  27. package/dist/compaction/manual.d.ts +16 -1
  28. package/dist/compaction/manual.d.ts.map +1 -1
  29. package/dist/compaction/manual.js +75 -13
  30. package/dist/compaction/manual.js.map +1 -1
  31. package/dist/compaction/plan.d.ts +2 -1
  32. package/dist/compaction/plan.d.ts.map +1 -1
  33. package/dist/compaction/plan.js +6 -5
  34. package/dist/compaction/plan.js.map +1 -1
  35. package/dist/compaction/precompute.d.ts +179 -0
  36. package/dist/compaction/precompute.d.ts.map +1 -0
  37. package/dist/compaction/precompute.js +274 -0
  38. package/dist/compaction/precompute.js.map +1 -0
  39. package/dist/compaction/retained-images.d.ts +16 -0
  40. package/dist/compaction/retained-images.d.ts.map +1 -0
  41. package/dist/compaction/retained-images.js +113 -0
  42. package/dist/compaction/retained-images.js.map +1 -0
  43. package/dist/compaction/token-estimate.d.ts +8 -3
  44. package/dist/compaction/token-estimate.d.ts.map +1 -1
  45. package/dist/compaction/token-estimate.js +76 -8
  46. package/dist/compaction/token-estimate.js.map +1 -1
  47. package/dist/compaction/tool-result-editing.d.ts.map +1 -1
  48. package/dist/compaction/tool-result-editing.js +15 -8
  49. package/dist/compaction/tool-result-editing.js.map +1 -1
  50. package/dist/config/runtime.d.ts +4 -0
  51. package/dist/config/runtime.d.ts.map +1 -1
  52. package/dist/config/runtime.js +17 -0
  53. package/dist/config/runtime.js.map +1 -1
  54. package/dist/constants/acp/index.d.ts +2 -0
  55. package/dist/constants/acp/index.d.ts.map +1 -1
  56. package/dist/constants/acp/index.js +2 -0
  57. package/dist/constants/acp/index.js.map +1 -1
  58. package/dist/manager/agent/lifecycle.d.ts.map +1 -1
  59. package/dist/manager/agent/lifecycle.js +8 -0
  60. package/dist/manager/agent/lifecycle.js.map +1 -1
  61. package/dist/manager/session/turn-recorder.d.ts +26 -0
  62. package/dist/manager/session/turn-recorder.d.ts.map +1 -1
  63. package/dist/manager/session/turn-recorder.js +72 -5
  64. package/dist/manager/session/turn-recorder.js.map +1 -1
  65. package/dist/peers/protocol.d.ts +9 -0
  66. package/dist/peers/protocol.d.ts.map +1 -1
  67. package/dist/peers/protocol.js +4 -0
  68. package/dist/peers/protocol.js.map +1 -1
  69. package/dist/peers/record.d.ts +6 -0
  70. package/dist/peers/record.d.ts.map +1 -1
  71. package/dist/peers/record.js +4 -0
  72. package/dist/peers/record.js.map +1 -1
  73. package/dist/plugin/index.d.ts +2 -2
  74. package/dist/plugin/index.d.ts.map +1 -1
  75. package/dist/plugin/index.js +1 -1
  76. package/dist/plugin/index.js.map +1 -1
  77. package/dist/plugin/shell-hook.d.ts +30 -0
  78. package/dist/plugin/shell-hook.d.ts.map +1 -1
  79. package/dist/plugin/shell-hook.js +35 -0
  80. package/dist/plugin/shell-hook.js.map +1 -1
  81. package/dist/pricing/catalogue.generated.d.ts.map +1 -1
  82. package/dist/pricing/catalogue.generated.js +178 -1
  83. package/dist/pricing/catalogue.generated.js.map +1 -1
  84. package/dist/provider/attribution.js +1 -1
  85. package/dist/provider/attribution.js.map +1 -1
  86. package/dist/public-runtime.d.ts +9 -5
  87. package/dist/public-runtime.d.ts.map +1 -1
  88. package/dist/public-runtime.js +12 -4
  89. package/dist/public-runtime.js.map +1 -1
  90. package/dist/public-tools.d.ts +1 -1
  91. package/dist/public-tools.d.ts.map +1 -1
  92. package/dist/public-tools.js +1 -1
  93. package/dist/public-tools.js.map +1 -1
  94. package/dist/public-types.d.ts +3 -2
  95. package/dist/public-types.d.ts.map +1 -1
  96. package/dist/rag/embedding.d.ts +5 -1
  97. package/dist/rag/embedding.d.ts.map +1 -1
  98. package/dist/rag/embedding.js +22 -7
  99. package/dist/rag/embedding.js.map +1 -1
  100. package/dist/registry/command/kernel-commands.d.ts +7 -1
  101. package/dist/registry/command/kernel-commands.d.ts.map +1 -1
  102. package/dist/registry/command/kernel-commands.js +40 -2
  103. package/dist/registry/command/kernel-commands.js.map +1 -1
  104. package/dist/registry/tool/execute.d.ts +8 -0
  105. package/dist/registry/tool/execute.d.ts.map +1 -1
  106. package/dist/registry/tool/execute.js +12 -0
  107. package/dist/registry/tool/execute.js.map +1 -1
  108. package/dist/runtime/query/abandon-turn.d.ts.map +1 -1
  109. package/dist/runtime/query/abandon-turn.js +9 -0
  110. package/dist/runtime/query/abandon-turn.js.map +1 -1
  111. package/dist/runtime/query/budget-nudge.d.ts +27 -0
  112. package/dist/runtime/query/budget-nudge.d.ts.map +1 -0
  113. package/dist/runtime/query/budget-nudge.js +66 -0
  114. package/dist/runtime/query/budget-nudge.js.map +1 -0
  115. package/dist/runtime/query/compact-session.d.ts +19 -3
  116. package/dist/runtime/query/compact-session.d.ts.map +1 -1
  117. package/dist/runtime/query/compact-session.js +180 -93
  118. package/dist/runtime/query/compact-session.js.map +1 -1
  119. package/dist/runtime/query/guard.d.ts +8 -0
  120. package/dist/runtime/query/guard.d.ts.map +1 -1
  121. package/dist/runtime/query/guard.js +20 -0
  122. package/dist/runtime/query/guard.js.map +1 -1
  123. package/dist/runtime/query/index.d.ts.map +1 -1
  124. package/dist/runtime/query/index.js +1 -0
  125. package/dist/runtime/query/index.js.map +1 -1
  126. package/dist/runtime/query/iteration/index.d.ts +2 -0
  127. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  128. package/dist/runtime/query/iteration/index.js +41 -1
  129. package/dist/runtime/query/iteration/index.js.map +1 -1
  130. package/dist/runtime/query/iteration/phases/compaction.d.ts +1 -0
  131. package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
  132. package/dist/runtime/query/iteration/phases/compaction.js +203 -45
  133. package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
  134. package/dist/runtime/query/iteration/phases/context.d.ts +2 -0
  135. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  136. package/dist/runtime/query/iteration/phases/context.js +1 -0
  137. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  138. package/dist/runtime/query/iteration/phases/precompute.d.ts +60 -0
  139. package/dist/runtime/query/iteration/phases/precompute.d.ts.map +1 -0
  140. package/dist/runtime/query/iteration/phases/precompute.js +240 -0
  141. package/dist/runtime/query/iteration/phases/precompute.js.map +1 -0
  142. package/dist/runtime/query/iteration/request-prefix.d.ts +21 -0
  143. package/dist/runtime/query/iteration/request-prefix.d.ts.map +1 -0
  144. package/dist/runtime/query/iteration/request-prefix.js +111 -0
  145. package/dist/runtime/query/iteration/request-prefix.js.map +1 -0
  146. package/dist/runtime/query/review-policy.d.ts +11 -3
  147. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  148. package/dist/runtime/query/review-policy.js +54 -3
  149. package/dist/runtime/query/review-policy.js.map +1 -1
  150. package/dist/runtime/query/turn/outcome-ledger.d.ts +14 -0
  151. package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
  152. package/dist/runtime/query/turn/outcome-ledger.js +28 -0
  153. package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
  154. package/dist/sandbox/exec-stdin.d.ts +21 -0
  155. package/dist/sandbox/exec-stdin.d.ts.map +1 -0
  156. package/dist/sandbox/exec-stdin.js +40 -0
  157. package/dist/sandbox/exec-stdin.js.map +1 -0
  158. package/dist/sandbox/file-walk.d.ts +12 -7
  159. package/dist/sandbox/file-walk.d.ts.map +1 -1
  160. package/dist/sandbox/file-walk.js +13 -4
  161. package/dist/sandbox/file-walk.js.map +1 -1
  162. package/dist/sandbox/ignore-matcher.d.ts +1 -0
  163. package/dist/sandbox/ignore-matcher.d.ts.map +1 -1
  164. package/dist/sandbox/ignore-matcher.js.map +1 -1
  165. package/dist/sandbox/ignore-source.js +1 -1
  166. package/dist/sandbox/index.d.ts +3 -2
  167. package/dist/sandbox/index.d.ts.map +1 -1
  168. package/dist/sandbox/index.js +2 -1
  169. package/dist/sandbox/index.js.map +1 -1
  170. package/dist/sandbox/provider/local.d.ts.map +1 -1
  171. package/dist/sandbox/provider/local.js +2 -0
  172. package/dist/sandbox/provider/local.js.map +1 -1
  173. package/dist/session/paths.d.ts +6 -0
  174. package/dist/session/paths.d.ts.map +1 -1
  175. package/dist/session/paths.js +7 -0
  176. package/dist/session/paths.js.map +1 -1
  177. package/dist/session-query/index.d.ts +1 -0
  178. package/dist/session-query/index.d.ts.map +1 -1
  179. package/dist/session-query/index.js +26 -15
  180. package/dist/session-query/index.js.map +1 -1
  181. package/dist/store/evidence/disk.js +3 -1
  182. package/dist/store/evidence/disk.js.map +1 -1
  183. package/dist/store/evidence/index-page.d.ts +1 -0
  184. package/dist/store/evidence/index-page.d.ts.map +1 -1
  185. package/dist/store/evidence/index-page.js +8 -0
  186. package/dist/store/evidence/index-page.js.map +1 -1
  187. package/dist/store/goal/gates.d.ts +53 -0
  188. package/dist/store/goal/gates.d.ts.map +1 -0
  189. package/dist/store/goal/gates.js +664 -0
  190. package/dist/store/goal/gates.js.map +1 -0
  191. package/dist/store/goal/index.d.ts +16 -1
  192. package/dist/store/goal/index.d.ts.map +1 -1
  193. package/dist/store/goal/index.js +74 -0
  194. package/dist/store/goal/index.js.map +1 -1
  195. package/dist/store/memory/operation-lock.d.ts.map +1 -1
  196. package/dist/store/memory/operation-lock.js +15 -1
  197. package/dist/store/memory/operation-lock.js.map +1 -1
  198. package/dist/store/session-log/core.d.ts.map +1 -1
  199. package/dist/store/session-log/core.js +23 -0
  200. package/dist/store/session-log/core.js.map +1 -1
  201. package/dist/tools/builtins/bash.js +1 -1
  202. package/dist/tools/builtins/bash.js.map +1 -1
  203. package/dist/tools/builtins/browser.d.ts.map +1 -1
  204. package/dist/tools/builtins/browser.js +2 -0
  205. package/dist/tools/builtins/browser.js.map +1 -1
  206. package/dist/tools/builtins/computer-use-image.d.ts +9 -0
  207. package/dist/tools/builtins/computer-use-image.d.ts.map +1 -1
  208. package/dist/tools/builtins/computer-use-image.js +21 -0
  209. package/dist/tools/builtins/computer-use-image.js.map +1 -1
  210. package/dist/tools/builtins/computer-use.d.ts +1 -9
  211. package/dist/tools/builtins/computer-use.d.ts.map +1 -1
  212. package/dist/tools/builtins/computer-use.js +2 -22
  213. package/dist/tools/builtins/computer-use.js.map +1 -1
  214. package/dist/tools/builtins/monitor.d.ts.map +1 -1
  215. package/dist/tools/builtins/monitor.js +1 -0
  216. package/dist/tools/builtins/monitor.js.map +1 -1
  217. package/dist/tools/builtins/search-tools.js +2 -2
  218. package/dist/tools/builtins/search-tools.js.map +1 -1
  219. package/dist/tools/builtins/sleep-guard.js +1 -1
  220. package/dist/tools/builtins/sleep-guard.js.map +1 -1
  221. package/dist/tools/builtins/wait-for-job.js +1 -1
  222. package/dist/tools/builtins/wait-for-job.js.map +1 -1
  223. package/dist/tools/coordinator/ask-user-question.d.ts.map +1 -1
  224. package/dist/tools/coordinator/ask-user-question.js +25 -0
  225. package/dist/tools/coordinator/ask-user-question.js.map +1 -1
  226. package/dist/tools/coordinator/index.d.ts.map +1 -1
  227. package/dist/tools/coordinator/index.js +3 -0
  228. package/dist/tools/coordinator/index.js.map +1 -1
  229. package/dist/tools/defineTool.d.ts +2 -0
  230. package/dist/tools/defineTool.d.ts.map +1 -1
  231. package/dist/tools/defineTool.js +3 -0
  232. package/dist/tools/defineTool.js.map +1 -1
  233. package/dist/tools/goal/confirmation.d.ts +40 -2
  234. package/dist/tools/goal/confirmation.d.ts.map +1 -1
  235. package/dist/tools/goal/confirmation.js +117 -0
  236. package/dist/tools/goal/confirmation.js.map +1 -1
  237. package/dist/tools/goal/index.d.ts.map +1 -1
  238. package/dist/tools/goal/index.js +14 -2
  239. package/dist/tools/goal/index.js.map +1 -1
  240. package/dist/tools/memory/save.d.ts.map +1 -1
  241. package/dist/tools/memory/save.js +1 -0
  242. package/dist/tools/memory/save.js.map +1 -1
  243. package/dist/tools/schedules/index.d.ts +1 -0
  244. package/dist/tools/schedules/index.d.ts.map +1 -1
  245. package/dist/tools/schedules/index.js +1 -0
  246. package/dist/tools/schedules/index.js.map +1 -1
  247. package/dist/tools/schedules/loop-tool.d.ts.map +1 -1
  248. package/dist/tools/schedules/loop-tool.js +1 -0
  249. package/dist/tools/schedules/loop-tool.js.map +1 -1
  250. package/dist/tools/schedules/present.d.ts.map +1 -1
  251. package/dist/tools/schedules/present.js +2 -0
  252. package/dist/tools/schedules/present.js.map +1 -1
  253. package/dist/tools/schedules/schedule-tool.d.ts.map +1 -1
  254. package/dist/tools/schedules/schedule-tool.js +79 -9
  255. package/dist/tools/schedules/schedule-tool.js.map +1 -1
  256. package/dist/tools/schedules/self-tool.d.ts +16 -0
  257. package/dist/tools/schedules/self-tool.d.ts.map +1 -0
  258. package/dist/tools/schedules/self-tool.js +140 -0
  259. package/dist/tools/schedules/self-tool.js.map +1 -0
  260. package/dist/tools/schedules/timer-tools.d.ts.map +1 -1
  261. package/dist/tools/schedules/timer-tools.js +1 -0
  262. package/dist/tools/schedules/timer-tools.js.map +1 -1
  263. package/dist/tools/schedules/types.d.ts +63 -0
  264. package/dist/tools/schedules/types.d.ts.map +1 -1
  265. package/dist/toolsets/manager.d.ts +14 -0
  266. package/dist/toolsets/manager.d.ts.map +1 -1
  267. package/dist/toolsets/manager.js +60 -7
  268. package/dist/toolsets/manager.js.map +1 -1
  269. package/dist/types/agent/supervisor.d.ts +10 -0
  270. package/dist/types/agent/supervisor.d.ts.map +1 -1
  271. package/dist/types/agent/task.d.ts +6 -0
  272. package/dist/types/agent/task.d.ts.map +1 -1
  273. package/dist/types/common/index.d.ts +20 -0
  274. package/dist/types/common/index.d.ts.map +1 -1
  275. package/dist/types/common/index.js +4 -1
  276. package/dist/types/common/index.js.map +1 -1
  277. package/dist/types/goal/index.d.ts +66 -0
  278. package/dist/types/goal/index.d.ts.map +1 -1
  279. package/dist/types/hitl/index.d.ts +11 -0
  280. package/dist/types/hitl/index.d.ts.map +1 -1
  281. package/dist/types/hitl/index.js.map +1 -1
  282. package/dist/types/rag/embedding.d.ts +4 -0
  283. package/dist/types/rag/embedding.d.ts.map +1 -1
  284. package/dist/types/sandbox/index.d.ts +19 -0
  285. package/dist/types/sandbox/index.d.ts.map +1 -1
  286. package/dist/types/sandbox/index.js.map +1 -1
  287. package/dist/types/session/events.d.ts +55 -0
  288. package/dist/types/session/events.d.ts.map +1 -1
  289. package/dist/types/session/events.js.map +1 -1
  290. package/dist/types/session/outcome.d.ts +12 -0
  291. package/dist/types/session/outcome.d.ts.map +1 -1
  292. package/dist/types/session/outcome.js.map +1 -1
  293. package/dist/types/session/records.d.ts +81 -1
  294. package/dist/types/session/records.d.ts.map +1 -1
  295. package/dist/types/session/records.js +33 -0
  296. package/dist/types/session/records.js.map +1 -1
  297. package/dist/types/tool/index.d.ts +7 -0
  298. package/dist/types/tool/index.d.ts.map +1 -1
  299. package/dist/types/tool/index.js.map +1 -1
  300. package/dist/utils/cost.d.ts +15 -0
  301. package/dist/utils/cost.d.ts.map +1 -1
  302. package/dist/utils/cost.js +24 -1
  303. package/dist/utils/cost.js.map +1 -1
  304. package/package.json +2 -2
  305. package/src/agents/examples/SupervisorAgent.ts +5 -0
  306. package/src/authorization/protected-paths.ts +11 -0
  307. package/src/authorization/readonly-commands.ts +85 -3
  308. package/src/authorization/shell-paths.ts +141 -3
  309. package/src/bridge/acp/permission.ts +6 -0
  310. package/src/bridge/acp/server.ts +20 -1
  311. package/src/compaction/context-window.ts +11 -0
  312. package/src/compaction/manual.ts +103 -17
  313. package/src/compaction/plan.ts +11 -5
  314. package/src/compaction/precompute.ts +390 -0
  315. package/src/compaction/retained-images.ts +130 -0
  316. package/src/compaction/token-estimate.ts +86 -8
  317. package/src/compaction/tool-result-editing.ts +20 -8
  318. package/src/config/runtime.ts +17 -0
  319. package/src/constants/acp/index.ts +2 -0
  320. package/src/manager/agent/lifecycle.ts +9 -0
  321. package/src/manager/session/turn-recorder.ts +92 -5
  322. package/src/peers/protocol.ts +4 -0
  323. package/src/peers/record.ts +4 -0
  324. package/src/plugin/index.ts +2 -0
  325. package/src/plugin/shell-hook.ts +60 -0
  326. package/src/pricing/catalogue.generated.ts +178 -1
  327. package/src/pricing/rates.source.json +155 -2
  328. package/src/provider/attribution.ts +1 -1
  329. package/src/public-runtime.ts +48 -0
  330. package/src/public-tools.ts +2 -0
  331. package/src/public-types.ts +8 -0
  332. package/src/rag/embedding.ts +23 -7
  333. package/src/registry/command/kernel-commands.ts +62 -3
  334. package/src/registry/tool/execute.ts +14 -0
  335. package/src/runtime/query/abandon-turn.ts +9 -0
  336. package/src/runtime/query/budget-nudge.ts +92 -0
  337. package/src/runtime/query/compact-session.ts +242 -87
  338. package/src/runtime/query/guard.ts +22 -0
  339. package/src/runtime/query/index.ts +1 -0
  340. package/src/runtime/query/iteration/index.ts +40 -1
  341. package/src/runtime/query/iteration/phases/compaction.ts +224 -46
  342. package/src/runtime/query/iteration/phases/context.ts +3 -0
  343. package/src/runtime/query/iteration/phases/precompute.ts +277 -0
  344. package/src/runtime/query/iteration/request-prefix.ts +161 -0
  345. package/src/runtime/query/review-policy.ts +79 -11
  346. package/src/runtime/query/turn/outcome-ledger.ts +34 -0
  347. package/src/sandbox/exec-stdin.ts +54 -0
  348. package/src/sandbox/file-walk.ts +25 -18
  349. package/src/sandbox/ignore-matcher.ts +1 -0
  350. package/src/sandbox/ignore-source.ts +1 -1
  351. package/src/sandbox/index.ts +8 -2
  352. package/src/sandbox/provider/local.ts +2 -0
  353. package/src/session/paths.ts +9 -0
  354. package/src/session-query/index.ts +23 -14
  355. package/src/store/evidence/disk.ts +3 -1
  356. package/src/store/evidence/index-page.ts +11 -0
  357. package/src/store/goal/gates.ts +738 -0
  358. package/src/store/goal/index.ts +109 -0
  359. package/src/store/memory/operation-lock.ts +19 -1
  360. package/src/store/session-log/core.ts +26 -0
  361. package/src/tools/builtins/bash.ts +1 -1
  362. package/src/tools/builtins/browser.ts +4 -0
  363. package/src/tools/builtins/computer-use-image.ts +20 -0
  364. package/src/tools/builtins/computer-use.ts +1 -20
  365. package/src/tools/builtins/monitor.ts +2 -0
  366. package/src/tools/builtins/search-tools.ts +2 -2
  367. package/src/tools/builtins/sleep-guard.ts +1 -1
  368. package/src/tools/builtins/wait-for-job.ts +1 -1
  369. package/src/tools/coordinator/ask-user-question.ts +37 -10
  370. package/src/tools/coordinator/index.ts +3 -0
  371. package/src/tools/defineTool.ts +5 -0
  372. package/src/tools/goal/confirmation.ts +171 -3
  373. package/src/tools/goal/index.ts +15 -3
  374. package/src/tools/memory/save.ts +2 -0
  375. package/src/tools/schedules/index.ts +1 -0
  376. package/src/tools/schedules/loop-tool.ts +2 -0
  377. package/src/tools/schedules/present.ts +2 -0
  378. package/src/tools/schedules/schedule-tool.ts +84 -9
  379. package/src/tools/schedules/self-tool.ts +152 -0
  380. package/src/tools/schedules/timer-tools.ts +2 -0
  381. package/src/tools/schedules/types.ts +62 -0
  382. package/src/toolsets/manager.ts +59 -8
  383. package/src/types/agent/supervisor.ts +11 -0
  384. package/src/types/agent/task.ts +7 -0
  385. package/src/types/common/index.ts +26 -1
  386. package/src/types/goal/index.ts +72 -0
  387. package/src/types/hitl/index.ts +11 -0
  388. package/src/types/rag/embedding.ts +7 -0
  389. package/src/types/sandbox/index.ts +19 -0
  390. package/src/types/session/events.ts +56 -0
  391. package/src/types/session/outcome.ts +16 -1
  392. package/src/types/session/records.ts +41 -0
  393. package/src/types/tool/index.ts +7 -0
  394. package/src/utils/cost.ts +44 -1
@@ -0,0 +1,738 @@
1
+ import type {
2
+ GoalGateEntry,
3
+ GoalGateResult,
4
+ GoalGates,
5
+ GoalGatesSpec,
6
+ } from '../../types/goal/index.js'
7
+
8
+ /** Most gates one goal may carry. */
9
+ export const GOAL_GATE_MAX_ENTRIES = 8
10
+ /** Most words in one gate's command. */
11
+ export const GOAL_GATE_MAX_ARGS = 64
12
+ /** Longest single word of a gate's command, in characters. */
13
+ export const GOAL_GATE_MAX_ARG_CHARS = 2048
14
+ /** Longest folder path a gate may name, in characters. */
15
+ export const GOAL_GATE_MAX_CWD_CHARS = 512
16
+ /** Longest name a gate may carry, in characters. */
17
+ export const GOAL_GATE_MAX_NAME_CHARS = 120
18
+ /** Longest output tail kept for a gate result, in characters. */
19
+ export const GOAL_GATE_MAX_TAIL_CHARS = 2000
20
+ /** Longest a gate may run, in milliseconds (one hour). */
21
+ export const GOAL_GATE_MAX_TIMEOUT_MS = 3_600_000
22
+ /** Shortest a gate may be given, in milliseconds. */
23
+ export const GOAL_GATE_MIN_TIMEOUT_MS = 1_000
24
+ /** How long a gate may run when its entry says nothing, in milliseconds (five minutes). */
25
+ export const GOAL_GATE_DEFAULT_TIMEOUT_MS = 300_000
26
+ /** Rounds in a row that may end with a failing gate before the goal stops, when nothing says otherwise. */
27
+ export const GOAL_GATE_DEFAULT_MAX_FAILING_ROUNDS = 3
28
+ /** The block code of a goal stopped because its gates kept failing. */
29
+ export const GOAL_GATES_BLOCK_CODE = 'gates-failing'
30
+
31
+ // Controls (C0 and C1), and the marks that hide, reorder or isolate text (zero-width, line and
32
+ // paragraph separators, direction marks and isolates, the byte order mark, the soft hyphen): a
33
+ // command or a name carrying one can read as something it is not when the list is shown.
34
+ const CONTROL_CHARACTER =
35
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what is refused.
36
+ /[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u200b-\u200f\u2028-\u202e\u2060-\u206f\ufeff]/u
37
+
38
+ /** Characters that need quoting for a person to read a word as one word. */
39
+ const PLAIN_WORD = /^[A-Za-z0-9_@%+=:,./-]+$/u
40
+
41
+ /** A command as one line for a person to read. Never run: the host runs the list. */
42
+ export function goalGateCommandText(command: readonly string[]): string {
43
+ return command
44
+ .map((word) => (PLAIN_WORD.test(word) ? word : `'${word.replace(/'/gu, "'\\''")}'`))
45
+ .join(' ')
46
+ }
47
+
48
+ /** Whether a gate's result is a pass. */
49
+ export function goalGatePassed(result: Pick<GoalGateResult, 'status'>): boolean {
50
+ return result.status === 'passed'
51
+ }
52
+
53
+ /** The names of the gates that did not pass. */
54
+ export function goalGateFailedNames(results: readonly GoalGateResult[]): string[] {
55
+ return results.filter((result) => !goalGatePassed(result)).map((result) => result.name)
56
+ }
57
+
58
+ /**
59
+ * Reads one gate entry, or says what is wrong with it. The command is a list
60
+ * of words; an empty word, a NUL or a control character in any word, a list
61
+ * that is too long and a program that is only a shell given a line to run are
62
+ * refused here so no host sees them.
63
+ */
64
+ export function parseGoalGateEntry(
65
+ value: unknown,
66
+ ):
67
+ | { readonly ok: true; readonly entry: GoalGateEntry }
68
+ | { readonly ok: false; readonly problem: string } {
69
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
70
+ return { ok: false, problem: 'a gate must be a mapping with a command' }
71
+ }
72
+ const raw = value as Record<string, unknown>
73
+ for (const key of Object.keys(raw)) {
74
+ if (key !== 'command' && key !== 'name' && key !== 'cwd' && key !== 'timeoutMs') {
75
+ return { ok: false, problem: `${key} is not a gate field (command, name, cwd, timeoutMs)` }
76
+ }
77
+ }
78
+ const command = raw.command
79
+ if (!Array.isArray(command) || command.length === 0) {
80
+ return {
81
+ ok: false,
82
+ problem: 'command must be a list of words, the program first (never one line for a shell)',
83
+ }
84
+ }
85
+ if (command.length > GOAL_GATE_MAX_ARGS) {
86
+ return { ok: false, problem: `command may have at most ${GOAL_GATE_MAX_ARGS} words` }
87
+ }
88
+ const words: string[] = []
89
+ for (const word of command) {
90
+ if (typeof word !== 'string')
91
+ return { ok: false, problem: 'every word of command must be text' }
92
+ if (word.length > GOAL_GATE_MAX_ARG_CHARS) {
93
+ return {
94
+ ok: false,
95
+ problem: `a word of command is longer than ${GOAL_GATE_MAX_ARG_CHARS} characters`,
96
+ }
97
+ }
98
+ if (CONTROL_CHARACTER.test(word)) {
99
+ return { ok: false, problem: 'a word of command holds a control character' }
100
+ }
101
+ words.push(word)
102
+ }
103
+ const program = words[0] as string
104
+ if (program.trim() === '' || program !== program.trim()) {
105
+ return { ok: false, problem: 'the program (first word of command) must be a name or a path' }
106
+ }
107
+ const shellLine = shellLineProblem(words)
108
+ if (shellLine !== undefined) return { ok: false, problem: shellLine }
109
+
110
+ let cwd: string | undefined
111
+ if (raw.cwd !== undefined) {
112
+ if (typeof raw.cwd !== 'string' || raw.cwd.trim() === '') {
113
+ return { ok: false, problem: 'cwd must be a folder inside the project, relative to it' }
114
+ }
115
+ const problem = cwdProblem(raw.cwd)
116
+ if (problem !== undefined) return { ok: false, problem }
117
+ cwd = raw.cwd
118
+ }
119
+ let timeoutMs: number | undefined
120
+ if (raw.timeoutMs !== undefined) {
121
+ if (
122
+ typeof raw.timeoutMs !== 'number' ||
123
+ !Number.isSafeInteger(raw.timeoutMs) ||
124
+ raw.timeoutMs < GOAL_GATE_MIN_TIMEOUT_MS ||
125
+ raw.timeoutMs > GOAL_GATE_MAX_TIMEOUT_MS
126
+ ) {
127
+ return {
128
+ ok: false,
129
+ problem: `timeoutMs must be a whole number from ${GOAL_GATE_MIN_TIMEOUT_MS} to ${GOAL_GATE_MAX_TIMEOUT_MS}`,
130
+ }
131
+ }
132
+ timeoutMs = raw.timeoutMs
133
+ }
134
+ let name = goalGateCommandText(words)
135
+ if (raw.name !== undefined) {
136
+ if (
137
+ typeof raw.name !== 'string' ||
138
+ raw.name.trim() === '' ||
139
+ CONTROL_CHARACTER.test(raw.name)
140
+ ) {
141
+ return { ok: false, problem: 'name must be a short line of text' }
142
+ }
143
+ name = raw.name.trim()
144
+ }
145
+ if (name.length > GOAL_GATE_MAX_NAME_CHARS)
146
+ name = `${name.slice(0, GOAL_GATE_MAX_NAME_CHARS - 1)}…`
147
+ return {
148
+ ok: true,
149
+ entry: {
150
+ name,
151
+ command: words,
152
+ ...(cwd !== undefined ? { cwd } : {}),
153
+ ...(timeoutMs !== undefined ? { timeoutMs } : {}),
154
+ },
155
+ }
156
+ }
157
+
158
+ /** Shells: a line to run is given with a flag, whichever way the flag is written. */
159
+ const SHELL_PROGRAMS = new Set([
160
+ 'sh',
161
+ 'bash',
162
+ 'zsh',
163
+ 'dash',
164
+ 'ash',
165
+ 'ksh',
166
+ 'mksh',
167
+ 'pdksh',
168
+ 'yash',
169
+ 'rbash',
170
+ 'rksh',
171
+ 'rzsh',
172
+ 'csh',
173
+ 'tcsh',
174
+ 'fish',
175
+ 'nu',
176
+ 'nushell',
177
+ 'elvish',
178
+ 'xonsh',
179
+ 'rc',
180
+ 'cmd',
181
+ 'powershell',
182
+ 'powershell_ise',
183
+ 'pwsh',
184
+ 'pwsh-preview',
185
+ ])
186
+
187
+ /** Programs that hand their arguments to a shell as one line, whatever the flags. */
188
+ const LINE_PROGRAMS = new Set(['wsl', 'ssh', 'mosh', 'rsh', 'forfiles', 'mshta'])
189
+
190
+ /** Programs that run a script or an expression written in their own arguments. */
191
+ interface CodeRunner {
192
+ /** Letters that, anywhere in a single-dash word (`-e`, `-pe`, `-cprint(1)`), say code follows. */
193
+ readonly cluster?: string
194
+ /** Long flags (`--eval`, with or without `=`). */
195
+ readonly long?: readonly string[]
196
+ /** Bare words that are subcommands taking code (`deno eval`). */
197
+ readonly words?: readonly string[]
198
+ /**
199
+ * Only the flags before the first plain word are the program's own (a package
200
+ * runner's later `-c` belongs to the tool it runs). Flags that take a value are
201
+ * skipped with it.
202
+ */
203
+ readonly leadingOnly?: boolean
204
+ }
205
+
206
+ /** Subcommands of the package runners that still read the runner's own flags after them. */
207
+ const RUNNER_SUBCOMMANDS = new Set(['exec', 'x', 'dlx', 'run', 'run-script'])
208
+
209
+ /** Flags of the package runners that take a value, so the value is not taken for the subcommand. */
210
+ const RUNNER_VALUE_FLAGS = new Set([
211
+ '-C',
212
+ '-F',
213
+ '-w',
214
+ '-p',
215
+ '--dir',
216
+ '--filter',
217
+ '--prefix',
218
+ '--workspace',
219
+ '--cwd',
220
+ '--package',
221
+ '--registry',
222
+ '--userconfig',
223
+ '--loglevel',
224
+ '--use-node-version',
225
+ ])
226
+
227
+ const CODE_RUNNERS: Readonly<Record<string, CodeRunner>> = {
228
+ python: { cluster: 'c' },
229
+ pypy: { cluster: 'c' },
230
+ py: { cluster: 'c' },
231
+ node: { cluster: 'ep', long: ['eval', 'print'] },
232
+ nodejs: { cluster: 'ep', long: ['eval', 'print'] },
233
+ tsx: { cluster: 'ep', long: ['eval', 'print'] },
234
+ 'ts-node': { cluster: 'ep', long: ['eval', 'print'] },
235
+ 'ts-node-esm': { cluster: 'ep', long: ['eval', 'print'] },
236
+ 'babel-node': { cluster: 'ep', long: ['eval', 'print'] },
237
+ coffee: { cluster: 'ec' },
238
+ bun: { cluster: 'ep', long: ['eval', 'print'], words: ['repl'] },
239
+ deno: { cluster: 'e', long: ['eval', 'print'], words: ['eval', 'repl'] },
240
+ ruby: { cluster: 'e' },
241
+ jruby: { cluster: 'e' },
242
+ perl: { cluster: 'eE' },
243
+ raku: { cluster: 'e' },
244
+ php: { cluster: 'rRBE', long: ['run'] },
245
+ lua: { cluster: 'e' },
246
+ luajit: { cluster: 'e' },
247
+ osascript: { cluster: 'e' },
248
+ rscript: { cluster: 'e', long: ['expression'] },
249
+ r: { cluster: 'e', long: ['expression'] },
250
+ julia: { cluster: 'eEp', long: ['eval', 'print'] },
251
+ groovy: { cluster: 'e' },
252
+ scala: { cluster: 'e' },
253
+ clojure: { cluster: 'e' },
254
+ clj: { cluster: 'e' },
255
+ elixir: { cluster: 'e', long: ['eval'] },
256
+ erl: { cluster: 'e' },
257
+ racket: { cluster: 'e', long: ['eval'] },
258
+ ghc: { cluster: 'e' },
259
+ ghci: { cluster: 'e' },
260
+ jshell: { cluster: 'c' },
261
+ guile: { cluster: 'ce' },
262
+ tclsh: { cluster: 'c' },
263
+ expect: { cluster: 'c' },
264
+ psql: { cluster: 'c', long: ['command'] },
265
+ mysql: { cluster: 'e', long: ['execute'] },
266
+ mariadb: { cluster: 'e', long: ['execute'] },
267
+ mongosh: { long: ['eval'] },
268
+ mongo: { long: ['eval'] },
269
+ gdb: { cluster: 'e', long: ['eval-command', 'ex', 'iex', 'init-eval-command'] },
270
+ sqlite: {},
271
+ tar: { cluster: 'I', long: ['to-command', 'checkpoint-action', 'use-compress-program'] },
272
+ rsync: { cluster: 'e', long: ['rsh'] },
273
+ su: { cluster: 'c', long: ['command'] },
274
+ runuser: { cluster: 'c', long: ['command'] },
275
+ sg: { cluster: 'c' },
276
+ script: { cluster: 'c', long: ['command'] },
277
+ sudo: { cluster: 'si', long: ['shell', 'login'] },
278
+ doas: { cluster: 's' },
279
+ vim: { cluster: 'c', long: ['cmd'] },
280
+ nvim: { cluster: 'c', long: ['cmd'] },
281
+ vi: { cluster: 'c', long: ['cmd'] },
282
+ ex: { cluster: 'c', long: ['cmd'] },
283
+ emacs: { cluster: 'ef', long: ['eval', 'funcall', 'execute'] },
284
+ emacsclient: { cluster: 'e', long: ['eval'] },
285
+ npx: { cluster: 'c', long: ['call', 'shell-mode'], leadingOnly: true },
286
+ npm: { cluster: 'c', long: ['call', 'shell-mode'], leadingOnly: true },
287
+ pnpm: { cluster: 'c', long: ['call', 'shell-mode'], leadingOnly: true },
288
+ yarn: { cluster: 'c', long: ['call', 'shell-mode'], leadingOnly: true },
289
+ }
290
+
291
+ /**
292
+ * Programs whose first plain word is a program text (awk, sed): refused unless
293
+ * the program comes from a file (`-f`) and no inline program is added.
294
+ */
295
+ const PROGRAM_TEXT_RUNNERS = new Set(['awk', 'gawk', 'mawk', 'nawk', 'sed', 'gsed'])
296
+
297
+ /** Python's modules that run a string given to them. */
298
+ const PYTHON_CODE_MODULES = new Set(['timeit', 'code', 'codeop', 'runpy', 'idlelib'])
299
+
300
+ /** The program's own name: no folder, lowercase, no Windows extension, no version (`python3.12`). */
301
+ function programName(program: string): string {
302
+ // Windows reads `cmd.`, `cmd.exe ` and `cmd.exe:stream` as cmd.exe.
303
+ const base = (program.trim().split(/[\\/]/u).pop() ?? program)
304
+ .toLowerCase()
305
+ .replace(/:.*$/u, '')
306
+ .replace(/[. ]+$/u, '')
307
+ const noExtension = base.replace(/\.(exe|cmd|bat|com|ps1)$/u, '')
308
+ const noVersion = noExtension.replace(/[-_.]?\d+(?:\.\d+)*[a-z]?$/u, '')
309
+ return noVersion === '' ? noExtension : noVersion
310
+ }
311
+
312
+ /** A flag as PowerShell reads it: any dash or slash, lowercase, without `:value`. */
313
+ function powershellFlag(word: string): string {
314
+ return word
315
+ .trim()
316
+ .toLowerCase()
317
+ .replace(/^[-/–—―]+/u, '')
318
+ .split(/[:=]/u)[0] as string
319
+ }
320
+
321
+ function powershellRunsLine(words: readonly string[]): boolean {
322
+ return words.some((word) => {
323
+ if (!/^[-/–—―]/u.test(word.trim())) return false
324
+ const flag = powershellFlag(word)
325
+ if (flag === '') return false
326
+ // PowerShell takes any unique start of a flag name: -c, -com, -enc, -e, -ec, -cwa.
327
+ return (
328
+ 'command'.startsWith(flag) ||
329
+ 'encodedcommand'.startsWith(flag) ||
330
+ 'commandwithargs'.startsWith(flag) ||
331
+ flag === 'ec' ||
332
+ flag === 'cwa'
333
+ )
334
+ })
335
+ }
336
+
337
+ /**
338
+ * Programs that run a command given through one of their own flags or
339
+ * subcommands (`git rebase -x`, `nix-shell --run`, `tmux run-shell`).
340
+ */
341
+ interface CommandFlags {
342
+ /** Long flags, with or without `=`. */
343
+ readonly long?: readonly string[]
344
+ /** Single-dash flags that may carry their value attached (`-xcmd`). */
345
+ readonly prefix?: readonly string[]
346
+ /** Bare words (a subcommand, a verb) that take a command, compared in lower case. */
347
+ readonly words?: readonly string[]
348
+ /** Words that follow one another in this order, other words between them allowed. */
349
+ readonly sequences?: readonly (readonly string[])[]
350
+ }
351
+
352
+ const COMMAND_FLAG_PROGRAMS: Readonly<Record<string, CommandFlags>> = {
353
+ 'nix-shell': { long: ['run', 'command'], prefix: ['-c'] },
354
+ nix: { long: ['command'], prefix: ['-c'] },
355
+ nodemon: { long: ['exec'], prefix: ['-x'] },
356
+ lldb: {
357
+ long: [
358
+ 'one-line',
359
+ 'one-line-before-file',
360
+ 'one-line-on-crash',
361
+ 'source',
362
+ 'source-before-file',
363
+ 'source-on-crash',
364
+ ],
365
+ prefix: ['-o', '-O', '-s', '-S', '-k', '-K'],
366
+ },
367
+ tmux: {
368
+ words: [
369
+ 'run-shell',
370
+ 'run',
371
+ 'if-shell',
372
+ 'if',
373
+ 'pipe-pane',
374
+ 'pipep',
375
+ 'new-session',
376
+ 'new',
377
+ 'new-window',
378
+ 'neww',
379
+ 'split-window',
380
+ 'splitw',
381
+ 'respawn-pane',
382
+ 'respawnp',
383
+ 'respawn-window',
384
+ 'respawnw',
385
+ 'display-popup',
386
+ 'popup',
387
+ ],
388
+ },
389
+ rc: { prefix: ['-c'] },
390
+ sbcl: { long: ['eval'] },
391
+ clisp: { prefix: ['-x'] },
392
+ csi: { prefix: ['-e', '-p'], long: ['eval', 'print'] },
393
+ runghc: { prefix: ['-e'] },
394
+ runhaskell: { prefix: ['-e'] },
395
+ crystal: { words: ['eval'] },
396
+ 'redis-cli': { words: ['eval', 'evalsha', 'eval_ro', 'evalsha_ro', 'fcall', 'fcall_ro'] },
397
+ fzf: { long: ['preview', 'bind', 'with-shell'] },
398
+ zip: { long: ['unzip-command'], prefix: ['-TT'] },
399
+ find: { words: ['-exec', '-execdir', '-ok', '-okdir'] },
400
+ rg: { long: ['pre', 'hostname-bin'] },
401
+ ripgrep: { long: ['pre', 'hostname-bin'] },
402
+ }
403
+
404
+ function commandFlagRuns(rule: CommandFlags, words: readonly string[]): boolean {
405
+ const trimmed = words.map((word) => word.trim())
406
+ const lowered = trimmed.map((word) => word.toLowerCase())
407
+ for (const word of trimmed) {
408
+ if (rule.long?.some((long) => word === `--${long}` || word.startsWith(`--${long}=`)) === true) {
409
+ return true
410
+ }
411
+ if (
412
+ rule.prefix?.some((flag) => word.startsWith(flag) && !word.startsWith('--')) === true &&
413
+ /^-[^-]/u.test(word)
414
+ ) {
415
+ return true
416
+ }
417
+ }
418
+ if (rule.words?.some((word) => lowered.includes(word.toLowerCase())) === true) return true
419
+ return (
420
+ rule.sequences?.some((sequence) => {
421
+ let from = 0
422
+ return sequence.every((part) => {
423
+ const at = lowered.indexOf(part, from)
424
+ if (at < 0) return false
425
+ from = at + 1
426
+ return true
427
+ })
428
+ }) === true
429
+ )
430
+ }
431
+
432
+ /** Modules of `ansible -m` that run a command on the target. */
433
+ const ANSIBLE_COMMAND_MODULES = new Set([
434
+ 'shell',
435
+ 'command',
436
+ 'raw',
437
+ 'script',
438
+ 'expect',
439
+ 'win_shell',
440
+ 'win_command',
441
+ ])
442
+
443
+ function ansibleRunsCommand(words: readonly string[]): boolean {
444
+ const moduleOf = (value: string | undefined): boolean => {
445
+ if (value === undefined) return false
446
+ const last = value.trim().toLowerCase().split('.').pop() ?? ''
447
+ return ANSIBLE_COMMAND_MODULES.has(last)
448
+ }
449
+ for (let index = 0; index < words.length; index += 1) {
450
+ const word = (words[index] as string).trim()
451
+ if (word === '-m' || word === '--module-name') {
452
+ if (moduleOf(words[index + 1])) return true
453
+ } else if (word.startsWith('--module-name=')) {
454
+ if (moduleOf(word.slice('--module-name='.length))) return true
455
+ } else if (/^-m[^-]/u.test(word) && moduleOf(word.slice(2))) {
456
+ return true
457
+ }
458
+ }
459
+ return false
460
+ }
461
+
462
+ const GIT_VALUE_FLAGS = new Set([
463
+ '-C',
464
+ '--git-dir',
465
+ '--work-tree',
466
+ '--namespace',
467
+ '--super-prefix',
468
+ '--attr-source',
469
+ ])
470
+
471
+ /** The subcommand of a git line: the first plain word after git's own flags. */
472
+ function gitSubcommand(words: readonly string[]): string | undefined {
473
+ for (let index = 0; index < words.length; index += 1) {
474
+ const word = (words[index] as string).trim()
475
+ if (GIT_VALUE_FLAGS.has(word)) {
476
+ index += 1
477
+ continue
478
+ }
479
+ if (!word.startsWith('-')) return word.toLowerCase()
480
+ }
481
+ return undefined
482
+ }
483
+
484
+ /**
485
+ * git subcommands and flags that run a command: `rebase -x`, `bisect run`,
486
+ * `submodule foreach`, `filter-branch --*-filter`, `difftool -x`, `grep -O`,
487
+ * `--upload-pack`, `--receive-pack`, `archive --exec`, `clone -u`.
488
+ */
489
+ function gitRunsCommand(words: readonly string[]): boolean {
490
+ const trimmed = words.map((word) => word.trim())
491
+ if (
492
+ trimmed.some(
493
+ (word) =>
494
+ /^--(upload-pack|receive-pack|exec|extcmd|open-files-in-pager)(=|$)/u.test(word) ||
495
+ /^--[a-z]+(?:-[a-z]+)*-filter(=|$)/u.test(word),
496
+ )
497
+ ) {
498
+ return true
499
+ }
500
+ const sub = gitSubcommand(trimmed)
501
+ const rest = trimmed.slice(trimmed.findIndex((word) => word.toLowerCase() === sub) + 1)
502
+ if (sub === 'bisect' && rest.some((word) => word.toLowerCase() === 'run')) return true
503
+ if (sub === 'submodule' && rest.some((word) => word.toLowerCase() === 'foreach')) return true
504
+ if (sub === 'rebase' && rest.some((word) => /^-[A-Za-z]*x/u.test(word))) return true
505
+ if (sub === 'difftool' && rest.some((word) => /^-[A-Za-z]*x/u.test(word))) return true
506
+ if (sub === 'grep' && rest.some((word) => /^-[A-Za-z]*O/u.test(word))) return true
507
+ if (sub === 'clone' && rest.some((word) => /^-[A-Za-z]*u/u.test(word))) return true
508
+ return false
509
+ }
510
+
511
+ /** `cmd` runs the rest of its line behind /c, /k or /r, however the switches are joined (`/d/s/c`). */
512
+ function cmdRunsLine(words: readonly string[]): boolean {
513
+ return words.some((word) => /^\/(?:[a-z:][^/]*\/)*[ckr]/iu.test(word.trim()))
514
+ }
515
+
516
+ function shellRunsLine(name: string, words: readonly string[]): boolean {
517
+ if (name === 'powershell' || name === 'pwsh') return powershellRunsLine(words)
518
+ if (name === 'cmd') return cmdRunsLine(words)
519
+ return words.some((word) => {
520
+ const flag = word.trim()
521
+ // Any single-dash cluster that holds c: -c, -lc, -xec, -euxfc, -cprint.
522
+ if (/^-[A-Za-z0-9]*c/u.test(flag)) return true
523
+ // fish and nushell spell it out.
524
+ return /^--(command|commands)(=|$)/u.test(flag)
525
+ })
526
+ }
527
+
528
+ function clusterHas(word: string, letters: string): boolean {
529
+ const match = /^-([A-Za-z0-9]+)/u.exec(word.trim())
530
+ if (match === null) return false
531
+ return [...(match[1] as string)].some((letter) => letters.includes(letter))
532
+ }
533
+
534
+ function runnerRunsCode(name: string, runner: CodeRunner, words: readonly string[]): boolean {
535
+ // sqlite3 runs a shell line from a dot command given as an argument.
536
+ if (
537
+ name === 'sqlite' &&
538
+ words.some(
539
+ (word) =>
540
+ /^\s*\.(shell|system|import|output|once|excel|www)\b/u.test(word) ||
541
+ /^\s*\.read\s+["']?[|!]/u.test(word),
542
+ )
543
+ ) {
544
+ return true
545
+ }
546
+ let afterModuleFlag = false
547
+ for (let index = 0; index < words.length; index += 1) {
548
+ const word = (words[index] as string).trim()
549
+ if (runner.leadingOnly === true) {
550
+ if (RUNNER_VALUE_FLAGS.has(word)) {
551
+ index += 1
552
+ continue
553
+ }
554
+ // A subcommand that takes the runner's own flags goes on; any other word is the tool's.
555
+ if (!word.startsWith('-') && !RUNNER_SUBCOMMANDS.has(word)) return false
556
+ }
557
+ if (runner.cluster !== undefined && clusterHas(word, runner.cluster)) return true
558
+ if (
559
+ runner.long?.some((long) => word === `--${long}` || word.startsWith(`--${long}=`)) === true
560
+ ) {
561
+ return true
562
+ }
563
+ if (runner.words?.includes(word) === true) return true
564
+ // An inline module is code too: node --import data:text/javascript,… , deno run https://…
565
+ // The same behind an `=` (--import=data:…, --require=data:…, --loader=https://…).
566
+ if (
567
+ (name === 'node' || name === 'nodejs' || name === 'bun' || name === 'deno') &&
568
+ /^(data:|https?:\/\/)/iu.test(word.replace(/^--?[A-Za-z][A-Za-z-]*=/u, ''))
569
+ ) {
570
+ return true
571
+ }
572
+ if (name === 'python' || name === 'pypy' || name === 'py') {
573
+ if (afterModuleFlag && PYTHON_CODE_MODULES.has(word)) return true
574
+ afterModuleFlag = /^-[A-Za-z]*m$/u.test(word)
575
+ }
576
+ }
577
+ return false
578
+ }
579
+
580
+ function programTextRuns(words: readonly string[]): boolean {
581
+ let fromFile = false
582
+ for (const raw of words) {
583
+ const word = raw.trim()
584
+ if (/^-[A-Za-z0-9]*[eE]/u.test(word) || /^--(source|expression|exec)(=|$)/u.test(word)) {
585
+ return true
586
+ }
587
+ if (/^-[A-Za-z0-9]*f/u.test(word) || /^--file(=|$)/u.test(word)) fromFile = true
588
+ }
589
+ return !fromFile
590
+ }
591
+
592
+ /** `git -c name=value` sets configuration, and `alias.x=!line` or `core.sshCommand` runs a line. */
593
+ function gitSetsConfiguration(words: readonly string[]): boolean {
594
+ for (let index = 0; index < words.length; index += 1) {
595
+ const word = (words[index] as string).trim()
596
+ if (/^-c/u.test(word) || /^--config-env(=|$)/u.test(word) || word === '--exec-path') return true
597
+ if (GIT_VALUE_FLAGS.has(word)) {
598
+ index += 1
599
+ continue
600
+ }
601
+ if (!word.startsWith('-')) return false
602
+ }
603
+ return false
604
+ }
605
+
606
+ /** Variables that make a program load or run something else (a preload, a startup script, a pager or ssh command). */
607
+ const RISKY_ENVIRONMENT =
608
+ /^(?:LD_.*|DYLD_.*|BASH_ENV|ENV|SHELLOPTS|BASHOPTS|PS4|PROMPT_COMMAND|IFS|PATH|PATHEXT|COMSPEC|PYTHON(?:STARTUP|HOME|PATH)|NODE_(?:OPTIONS|PATH)|PERL5?(?:OPT|LIB)|RUBY(?:OPT|LIB)|GIT_(?:SSH|SSH_COMMAND|PAGER|EXTERNAL_DIFF|ASKPASS|EDITOR|SEQUENCE_EDITOR|PROXY_COMMAND|EXEC_PATH|CONFIG.*)|SSH_ASKPASS|PAGER|EDITOR|VISUAL|LESSOPEN|LESSCLOSE|MANPAGER)$/u
609
+
610
+ const SHELL_LINE_MESSAGE =
611
+ 'a shell given a line to run (sh -c, cmd /c, powershell -Command) is not allowed: write the program and its arguments as separate words'
612
+ const INLINE_CODE_MESSAGE =
613
+ 'a program given code or a command line to run (python -c, node -e, perl -e, su -c, …) is not allowed: put the code in a file in the project and name the file'
614
+
615
+ /**
616
+ * A command that hands a shell, or an interpreter, a line or a piece of code to
617
+ * run is the string-for-a-shell this list exists to avoid: `sh -c "…"`,
618
+ * `bash -euxfc "…"`, `cmd /r …`, `powershell -Command …`, `python -c …`,
619
+ * `node -e …`, and the same behind `env`, `nice`, `timeout`, `xargs` or a path.
620
+ * Refused by name and by flag in every position, so a repository's file cannot
621
+ * smuggle one in as a list. A script file given to a shell or an interpreter
622
+ * (`sh ./check.sh`, `node check.js`) is the project's own file and passes.
623
+ *
624
+ * This catches the common shell and inline-code forms, with a list of programs
625
+ * that run a command through a flag or a subcommand. It is a mistake-catcher
626
+ * and not a boundary: a list can still start any program, and a program can
627
+ * read a flag by an abbreviation or a spelling this list does not know. The
628
+ * boundary is the person's approval of the exact list when it comes from a
629
+ * project's checked-in file (see the CLI).
630
+ */
631
+ function shellLineProblem(words: readonly string[]): string | undefined {
632
+ // Every position, not only the first: a wrapper (`env`, `nice -n 5`, `sudo -u x`, `xargs`,
633
+ // `docker run image`) puts the program anywhere behind its own options.
634
+ for (let index = 0; index < words.length; index += 1) {
635
+ const name = programName(words[index] as string)
636
+ const rest = words.slice(index + 1)
637
+ // A short Windows name (POWERS~1.EXE) is the same program under a name this list cannot read.
638
+ if (/~\d+\.(exe|cmd|bat|com)$/iu.test((words[index] as string).trim())) {
639
+ return 'a short Windows name (like POWERS~1.EXE) hides which program runs: write its full name'
640
+ }
641
+ if (LINE_PROGRAMS.has(name) && rest.length > 0) {
642
+ return `${name} runs its arguments as a line for a shell: name the program as separate words, not through ${name}`
643
+ }
644
+ // `env -S 'sh -c …'` splits one word into a command and runs it.
645
+ if (name === 'env' && rest.some((word) => /^(-[A-Za-z]*S|--split-string)/u.test(word.trim()))) {
646
+ return 'env -S runs one word as a command line: write the program and its arguments as separate words'
647
+ }
648
+ // `env NAME=value program`: a variable that makes the program run something else.
649
+ if (name === 'env') {
650
+ const risky = rest.find((word) => {
651
+ const assignment = /^([A-Za-z_][A-Za-z0-9_]*)=/u.exec(word.trim())
652
+ return assignment !== null && RISKY_ENVIRONMENT.test(assignment[1] as string)
653
+ })
654
+ if (risky !== undefined) {
655
+ return `${risky.split('=')[0]} changes which program or line runs: set it in the project, not in the gate`
656
+ }
657
+ }
658
+ if (SHELL_PROGRAMS.has(name) && shellRunsLine(name, rest)) return SHELL_LINE_MESSAGE
659
+ const runner = CODE_RUNNERS[name]
660
+ if (runner !== undefined && runnerRunsCode(name, runner, rest)) return INLINE_CODE_MESSAGE
661
+ if (PROGRAM_TEXT_RUNNERS.has(name) && rest.length > 0 && programTextRuns(rest)) {
662
+ return `${name} given its program as text is not allowed: put the program in a file and name it with -f`
663
+ }
664
+ if (name === 'git' && gitSetsConfiguration(rest)) {
665
+ return 'git -c sets configuration that can run a command: set it in a config file instead'
666
+ }
667
+ if (name === 'git' && gitRunsCommand(rest)) {
668
+ return 'a git flag or subcommand that runs a command (rebase -x, bisect run, submodule foreach, --upload-pack, filter-branch --*-filter) is not allowed: run the commands as separate gates'
669
+ }
670
+ const flagRule = COMMAND_FLAG_PROGRAMS[name]
671
+ if (flagRule !== undefined && commandFlagRuns(flagRule, rest)) {
672
+ return `${name} given a command to run through one of its flags or subcommands is not allowed: name that command as the gate instead`
673
+ }
674
+ if (name === 'ansible' && ansibleRunsCommand(rest)) {
675
+ return 'ansible -m shell (or command, raw, script) runs a command line: name the program as separate words'
676
+ }
677
+ }
678
+ return undefined
679
+ }
680
+
681
+ /** A folder a gate names must stay inside the project: relative, no `..`, no drive or root. */
682
+ function cwdProblem(cwd: string): string | undefined {
683
+ if (cwd.length > GOAL_GATE_MAX_CWD_CHARS) {
684
+ return `cwd is longer than ${GOAL_GATE_MAX_CWD_CHARS} characters`
685
+ }
686
+ if (CONTROL_CHARACTER.test(cwd)) return 'cwd holds a control character'
687
+ if (/^([A-Za-z]:|[\\/]|~)/u.test(cwd) || cwd.startsWith('\\\\')) {
688
+ return 'cwd must be inside the project, relative to it (no absolute path, drive or ~)'
689
+ }
690
+ if (cwd.split(/[\\/]+/u).some((part) => part === '..')) {
691
+ return 'cwd must stay inside the project (no ..)'
692
+ }
693
+ return undefined
694
+ }
695
+
696
+ /**
697
+ * The gates a goal is created with, checked: at most {@link GOAL_GATE_MAX_ENTRIES}
698
+ * entries, each valid, and a failing-round cap of at least 1. Throws a
699
+ * `TypeError` naming the first problem. `undefined` for no gates.
700
+ */
701
+ export function goalGatesFrom(spec: GoalGatesSpec | undefined): GoalGates | undefined {
702
+ if (spec === undefined || spec.entries.length === 0) return undefined
703
+ if (spec.entries.length > GOAL_GATE_MAX_ENTRIES) {
704
+ throw new TypeError(`a goal may carry at most ${GOAL_GATE_MAX_ENTRIES} gates.`)
705
+ }
706
+ const entries: GoalGateEntry[] = []
707
+ const names = new Set<string>()
708
+ for (const [index, raw] of spec.entries.entries()) {
709
+ const parsed = parseGoalGateEntry(raw)
710
+ if (!parsed.ok) throw new TypeError(`goal gate ${index + 1}: ${parsed.problem}.`)
711
+ // A result is found by its gate's name, so two gates may not share one.
712
+ if (names.has(parsed.entry.name)) {
713
+ throw new TypeError(`goal gate ${index + 1}: the name ${parsed.entry.name} is used twice.`)
714
+ }
715
+ names.add(parsed.entry.name)
716
+ entries.push(parsed.entry)
717
+ }
718
+ const cap = spec.maxFailingRounds ?? GOAL_GATE_DEFAULT_MAX_FAILING_ROUNDS
719
+ if (!Number.isSafeInteger(cap) || cap < 1) {
720
+ throw new TypeError('goal gate maxFailingRounds must be a whole number of at least 1.')
721
+ }
722
+ return { entries, maxFailingRounds: cap, failingRounds: 0, results: [] }
723
+ }
724
+
725
+ /** The end of a text, bounded, with the cut marked. */
726
+ export function goalGateTail(text: string, max = GOAL_GATE_MAX_TAIL_CHARS): string {
727
+ const trimmed = text.replace(/\s+$/u, '')
728
+ return trimmed.length > max ? `…${trimmed.slice(trimmed.length - (max - 1))}` : trimmed
729
+ }
730
+
731
+ /** The sentence for a goal stopped by its gates. */
732
+ export function goalGatesBlockSentence(
733
+ gates: Pick<GoalGates, 'maxFailingRounds'>,
734
+ names: readonly string[],
735
+ ): string {
736
+ const rounds = gates.maxFailingRounds
737
+ return `${rounds} round${rounds === 1 ? '' : 's'} in a row ended with a completion gate failing: ${names.join('; ')}.`
738
+ }