@capybara-hq/kernel 0.1.0-rc.3 → 0.1.0-rc.30

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 (1201) hide show
  1. package/bootstrap.json +94 -5
  2. package/dist/admin/admin-agents.controller.d.ts +28 -0
  3. package/dist/admin/admin-agents.controller.js +79 -0
  4. package/dist/admin/admin-connectors.controller.d.ts +29 -0
  5. package/dist/admin/admin-connectors.controller.js +79 -0
  6. package/dist/admin/admin-items.schemas.d.ts +42 -0
  7. package/dist/admin/admin-items.schemas.js +48 -0
  8. package/dist/admin/admin.controller.d.ts +54 -82
  9. package/dist/admin/admin.controller.js +118 -83
  10. package/dist/admin/admin.module.d.ts +5 -1
  11. package/dist/admin/admin.module.js +56 -4
  12. package/dist/admin/admin.schemas.d.ts +87 -0
  13. package/dist/admin/admin.schemas.js +53 -0
  14. package/dist/admin/audit.controller.d.ts +14 -0
  15. package/dist/admin/audit.controller.js +42 -0
  16. package/dist/admin/audit.schemas.d.ts +8 -0
  17. package/dist/admin/audit.schemas.js +17 -0
  18. package/dist/admin/extensions.controller.d.ts +34 -3
  19. package/dist/admin/extensions.controller.js +66 -8
  20. package/dist/admin/extensions.schemas.d.ts +7 -0
  21. package/dist/admin/extensions.schemas.js +10 -0
  22. package/dist/admin/health.service.d.ts +104 -0
  23. package/dist/admin/health.service.js +250 -0
  24. package/dist/admin/limit-defaults.d.ts +20 -0
  25. package/dist/admin/limit-defaults.js +10 -0
  26. package/dist/admin/outbox-dead.controller.d.ts +23 -0
  27. package/dist/admin/outbox-dead.controller.js +62 -0
  28. package/dist/admin/outbox-dead.schemas.d.ts +8 -0
  29. package/dist/admin/outbox-dead.schemas.js +18 -0
  30. package/dist/admin/outbox-dead.service.d.ts +70 -0
  31. package/dist/admin/outbox-dead.service.js +190 -0
  32. package/dist/admin/overview.controller.d.ts +31 -0
  33. package/dist/admin/overview.controller.js +78 -0
  34. package/dist/admin/overview.schemas.d.ts +8 -0
  35. package/dist/admin/overview.schemas.js +13 -0
  36. package/dist/admin/overview.service.d.ts +147 -0
  37. package/dist/admin/overview.service.js +257 -0
  38. package/dist/admin/platform-audit.service.d.ts +41 -0
  39. package/dist/admin/platform-audit.service.js +70 -0
  40. package/dist/admin/setup-lock.d.ts +1 -1
  41. package/dist/admin/setup-lock.js +1 -1
  42. package/dist/admin/setup.controller.d.ts +1 -1
  43. package/dist/admin/setup.controller.js +1 -1
  44. package/dist/admin/setup.service.d.ts +4 -4
  45. package/dist/admin/setup.service.js +8 -8
  46. package/dist/admin/usage.service.d.ts +149 -0
  47. package/dist/admin/usage.service.js +321 -0
  48. package/dist/agents/admin-agents.d.ts +105 -0
  49. package/dist/agents/admin-agents.js +228 -0
  50. package/dist/agents/agent-lookup.d.ts +24 -0
  51. package/dist/agents/agent-lookup.js +36 -0
  52. package/dist/agents/agent-profile.service.d.ts +31 -0
  53. package/dist/agents/agent-profile.service.js +122 -0
  54. package/dist/agents/agents.controller.d.ts +53 -168
  55. package/dist/agents/agents.controller.js +163 -34
  56. package/dist/agents/agents.module.d.ts +6 -2
  57. package/dist/agents/agents.module.js +44 -7
  58. package/dist/agents/agents.schemas.d.ts +440 -0
  59. package/dist/agents/agents.schemas.js +126 -0
  60. package/dist/agents/agents.service.d.ts +130 -39
  61. package/dist/agents/agents.service.js +435 -136
  62. package/dist/agents/definition.d.ts +169 -23
  63. package/dist/agents/definition.js +310 -55
  64. package/dist/agents/from-template.d.ts +109 -0
  65. package/dist/agents/from-template.js +693 -0
  66. package/dist/agents/from-template.service.d.ts +72 -0
  67. package/dist/agents/from-template.service.js +178 -0
  68. package/dist/agents/instruction-files.d.ts +60 -0
  69. package/dist/agents/instruction-files.js +111 -0
  70. package/dist/agents/models.controller.d.ts +44 -6
  71. package/dist/agents/models.controller.js +84 -8
  72. package/dist/agents/org-templates.d.ts +186 -0
  73. package/dist/agents/org-templates.js +932 -0
  74. package/dist/agents/pending-connector-requests.d.ts +79 -0
  75. package/dist/agents/pending-connector-requests.js +215 -0
  76. package/dist/agents/pending-connectors.controller.d.ts +39 -0
  77. package/dist/agents/pending-connectors.controller.js +91 -0
  78. package/dist/agents/pending-connectors.service.d.ts +62 -0
  79. package/dist/agents/pending-connectors.service.js +282 -0
  80. package/dist/agents/profile.d.ts +114 -0
  81. package/dist/agents/profile.js +55 -0
  82. package/dist/agents/readiness.controller.d.ts +26 -0
  83. package/dist/agents/readiness.controller.js +56 -0
  84. package/dist/agents/templates.controller.d.ts +77 -5
  85. package/dist/agents/templates.controller.js +153 -7
  86. package/dist/agents/templates.d.ts +133 -23
  87. package/dist/agents/templates.js +490 -53
  88. package/dist/agents/templates.schemas.d.ts +20 -0
  89. package/dist/agents/templates.schemas.js +45 -0
  90. package/dist/app.js +2 -0
  91. package/dist/attribution/platform-attribution.d.ts +104 -0
  92. package/dist/attribution/platform-attribution.js +149 -0
  93. package/dist/background/background.controller.d.ts +38 -0
  94. package/dist/background/background.controller.js +88 -0
  95. package/dist/background/background.module.d.ts +10 -0
  96. package/dist/background/background.module.js +39 -0
  97. package/dist/background/background.schemas.d.ts +10 -0
  98. package/dist/background/background.schemas.js +16 -0
  99. package/dist/background/finish.d.ts +47 -0
  100. package/dist/background/finish.js +106 -0
  101. package/dist/background/launch-lock.d.ts +15 -0
  102. package/dist/background/launch-lock.js +40 -0
  103. package/dist/background/launch.d.ts +81 -0
  104. package/dist/background/launch.js +160 -0
  105. package/dist/background/poll.job.d.ts +56 -0
  106. package/dist/background/poll.job.js +195 -0
  107. package/dist/background/render.d.ts +23 -0
  108. package/dist/background/render.js +48 -0
  109. package/dist/background/session.d.ts +60 -0
  110. package/dist/background/session.js +102 -0
  111. package/dist/background/status.d.ts +35 -0
  112. package/dist/background/status.js +57 -0
  113. package/dist/background/stop.d.ts +66 -0
  114. package/dist/background/stop.js +224 -0
  115. package/dist/background/store.d.ts +175 -0
  116. package/dist/background/store.js +385 -0
  117. package/dist/background/store.module.d.ts +6 -0
  118. package/dist/background/store.module.js +22 -0
  119. package/dist/background/tasks.service.d.ts +109 -0
  120. package/dist/background/tasks.service.js +271 -0
  121. package/dist/background/wake-rules.d.ts +71 -0
  122. package/dist/background/wake-rules.js +124 -0
  123. package/dist/background/wake.d.ts +30 -0
  124. package/dist/background/wake.js +63 -0
  125. package/dist/catalog/admin-catalog.controller.d.ts +156 -55
  126. package/dist/catalog/admin-catalog.controller.js +313 -68
  127. package/dist/catalog/admin-catalog.schemas.d.ts +87 -0
  128. package/dist/catalog/admin-catalog.schemas.js +107 -0
  129. package/dist/catalog/appearance.schemas.d.ts +10 -0
  130. package/dist/catalog/appearance.schemas.js +20 -0
  131. package/dist/catalog/appearance.service.d.ts +105 -0
  132. package/dist/catalog/appearance.service.js +306 -0
  133. package/dist/catalog/catalog-content.d.ts +144 -35
  134. package/dist/catalog/catalog-content.js +122 -13
  135. package/dist/catalog/catalog-files.d.ts +42 -0
  136. package/dist/catalog/catalog-files.js +89 -0
  137. package/dist/catalog/catalog-import.module.d.ts +7 -0
  138. package/dist/catalog/catalog-import.module.js +59 -0
  139. package/dist/catalog/catalog-items.d.ts +122 -7
  140. package/dist/catalog/catalog-items.js +301 -13
  141. package/dist/catalog/catalog-scope.service.d.ts +330 -0
  142. package/dist/catalog/catalog-scope.service.js +1312 -0
  143. package/dist/catalog/catalog.module.d.ts +8 -9
  144. package/dist/catalog/catalog.module.js +55 -31
  145. package/dist/catalog/catalog.service.d.ts +129 -31
  146. package/dist/catalog/catalog.service.js +193 -105
  147. package/dist/catalog/claude/agents.js +2 -0
  148. package/dist/catalog/claude/report.d.ts +9 -2
  149. package/dist/catalog/claude/report.js +1 -0
  150. package/dist/catalog/claude/translate.js +4 -1
  151. package/dist/catalog/cli.d.ts +12 -1
  152. package/dist/catalog/cli.js +24 -1
  153. package/dist/catalog/component-promotion.d.ts +96 -0
  154. package/dist/catalog/component-promotion.js +394 -0
  155. package/dist/catalog/component-scope.controller.d.ts +102 -0
  156. package/dist/catalog/component-scope.controller.js +257 -0
  157. package/dist/catalog/component-scope.schemas.d.ts +118 -0
  158. package/dist/catalog/component-scope.schemas.js +97 -0
  159. package/dist/catalog/component-scope.service.d.ts +394 -0
  160. package/dist/catalog/component-scope.service.js +1133 -0
  161. package/dist/catalog/component-state.d.ts +219 -0
  162. package/dist/catalog/component-state.js +493 -0
  163. package/dist/catalog/git-source.d.ts +9 -1
  164. package/dist/catalog/git-source.js +29 -2
  165. package/dist/catalog/job-ids.d.ts +19 -1
  166. package/dist/catalog/job-ids.js +21 -1
  167. package/dist/catalog/marketplace-fetch.d.ts +1 -1
  168. package/dist/catalog/marketplace-icons.d.ts +64 -0
  169. package/dist/catalog/marketplace-icons.js +131 -0
  170. package/dist/catalog/marketplaces.controller.d.ts +50 -41
  171. package/dist/catalog/marketplaces.controller.js +130 -46
  172. package/dist/catalog/marketplaces.schemas.d.ts +48 -0
  173. package/dist/catalog/marketplaces.schemas.js +53 -0
  174. package/dist/catalog/marketplaces.service.d.ts +110 -17
  175. package/dist/catalog/marketplaces.service.js +339 -30
  176. package/dist/catalog/org-catalog.controller.d.ts +124 -62
  177. package/dist/catalog/org-catalog.controller.js +208 -93
  178. package/dist/catalog/org-catalog.schemas.d.ts +117 -0
  179. package/dist/catalog/org-catalog.schemas.js +141 -0
  180. package/dist/catalog/org-catalog.service.d.ts +72 -33
  181. package/dist/catalog/org-catalog.service.js +377 -158
  182. package/dist/catalog/org-extension-switch.d.ts +41 -0
  183. package/dist/catalog/org-extension-switch.js +115 -0
  184. package/dist/catalog/org-extension.controller.d.ts +133 -0
  185. package/dist/catalog/org-extension.controller.js +313 -0
  186. package/dist/catalog/org-extension.schemas.d.ts +15 -0
  187. package/dist/catalog/org-extension.schemas.js +22 -0
  188. package/dist/catalog/org-plugin-uploads.controller.d.ts +77 -0
  189. package/dist/catalog/org-plugin-uploads.controller.js +199 -0
  190. package/dist/catalog/org-plugin-uploads.d.ts +77 -0
  191. package/dist/catalog/org-plugin-uploads.js +134 -0
  192. package/dist/catalog/org-plugin-uploads.schemas.d.ts +19 -0
  193. package/dist/catalog/org-plugin-uploads.schemas.js +30 -0
  194. package/dist/catalog/owned-items.d.ts +181 -0
  195. package/dist/catalog/owned-items.js +790 -0
  196. package/dist/catalog/owned-plugin-cleanup.d.ts +47 -0
  197. package/dist/catalog/owned-plugin-cleanup.js +140 -0
  198. package/dist/catalog/ownership.d.ts +172 -0
  199. package/dist/catalog/ownership.js +326 -0
  200. package/dist/catalog/platform-components.d.ts +264 -0
  201. package/dist/catalog/platform-components.js +857 -0
  202. package/dist/catalog/platform-import-update.d.ts +50 -0
  203. package/dist/catalog/platform-import-update.js +116 -0
  204. package/dist/catalog/plugin-import.d.ts +289 -52
  205. package/dist/catalog/plugin-import.js +846 -143
  206. package/dist/catalog/plugin-install.d.ts +235 -51
  207. package/dist/catalog/plugin-install.js +915 -139
  208. package/dist/catalog/plugin-package.d.ts +50 -4
  209. package/dist/catalog/plugin-package.js +221 -8
  210. package/dist/catalog/plugin-upgrade.d.ts +325 -225
  211. package/dist/catalog/plugin-upgrade.js +1317 -1133
  212. package/dist/catalog/request-rules.d.ts +65 -0
  213. package/dist/catalog/request-rules.js +217 -0
  214. package/dist/catalog/requests.service.d.ts +250 -117
  215. package/dist/catalog/requests.service.js +700 -323
  216. package/dist/catalog/reserved-names.d.ts +8 -0
  217. package/dist/catalog/reserved-names.js +42 -0
  218. package/dist/catalog/scope-jobs.d.ts +59 -0
  219. package/dist/catalog/scope-jobs.js +65 -0
  220. package/dist/catalog/scope-orgs.d.ts +21 -0
  221. package/dist/catalog/scope-orgs.js +67 -0
  222. package/dist/catalog/scope-state.d.ts +35 -0
  223. package/dist/catalog/scope-state.js +87 -0
  224. package/dist/catalog/scope-sync-workers.d.ts +29 -0
  225. package/dist/catalog/scope-sync-workers.js +85 -0
  226. package/dist/catalog/scope-sync.d.ts +151 -0
  227. package/dist/catalog/scope-sync.js +568 -0
  228. package/dist/catalog/scope.d.ts +42 -0
  229. package/dist/catalog/scope.js +42 -0
  230. package/dist/catalog/unshare.d.ts +22 -0
  231. package/dist/catalog/unshare.js +119 -0
  232. package/dist/channels/actions.d.ts +5 -0
  233. package/dist/channels/actions.js +33 -7
  234. package/dist/channels/cards.d.ts +59 -9
  235. package/dist/channels/cards.js +173 -20
  236. package/dist/channels/channels.module.d.ts +13 -9
  237. package/dist/channels/channels.module.js +33 -11
  238. package/dist/channels/channels.schemas.d.ts +41 -0
  239. package/dist/channels/channels.schemas.js +40 -0
  240. package/dist/channels/connections.controller.d.ts +46 -37
  241. package/dist/channels/connections.controller.js +76 -34
  242. package/dist/channels/connections.service.d.ts +105 -13
  243. package/dist/channels/connections.service.js +277 -30
  244. package/dist/channels/deliver-job.d.ts +32 -0
  245. package/dist/channels/deliver-job.js +29 -0
  246. package/dist/channels/inbound.d.ts +12 -2
  247. package/dist/channels/inbound.js +89 -22
  248. package/dist/channels/jobs.d.ts +37 -20
  249. package/dist/channels/jobs.js +79 -27
  250. package/dist/channels/outbound.d.ts +40 -3
  251. package/dist/channels/outbound.js +187 -22
  252. package/dist/channels/phrases.d.ts +93 -2
  253. package/dist/channels/phrases.js +71 -1
  254. package/dist/channels/supervisor.d.ts +2 -0
  255. package/dist/channels/supervisor.js +4 -0
  256. package/dist/channels/threads.d.ts +21 -3
  257. package/dist/channels/threads.js +46 -3
  258. package/dist/channels/webhook.d.ts +21 -1
  259. package/dist/channels/webhook.js +39 -4
  260. package/dist/client/client-access.service.d.ts +102 -0
  261. package/dist/client/client-access.service.js +303 -0
  262. package/dist/client/client-builds.service.d.ts +109 -0
  263. package/dist/client/client-builds.service.js +229 -0
  264. package/dist/client/client-delivery.controller.d.ts +25 -0
  265. package/dist/client/client-delivery.controller.js +66 -0
  266. package/dist/client/client-files.d.ts +48 -0
  267. package/dist/client/client-files.js +136 -0
  268. package/dist/client/client-reports.cron.d.ts +13 -0
  269. package/dist/client/client-reports.cron.js +42 -0
  270. package/dist/client/client-reports.service.d.ts +30 -0
  271. package/dist/client/client-reports.service.js +102 -0
  272. package/dist/client/client-sign-in.service.d.ts +21 -0
  273. package/dist/client/client-sign-in.service.js +49 -0
  274. package/dist/client/client.controller.d.ts +49 -0
  275. package/dist/client/client.controller.js +129 -0
  276. package/dist/client/client.module.d.ts +8 -0
  277. package/dist/client/client.module.js +28 -0
  278. package/dist/client/client.schemas.d.ts +61 -0
  279. package/dist/client/client.schemas.js +27 -0
  280. package/dist/config/docs.js +5 -1
  281. package/dist/config/schema.d.ts +71 -11
  282. package/dist/config/schema.js +431 -60
  283. package/dist/conversations/access.d.ts +6 -4
  284. package/dist/conversations/access.js +29 -5
  285. package/dist/conversations/agent-tree.d.ts +62 -0
  286. package/dist/conversations/agent-tree.js +128 -0
  287. package/dist/conversations/attachments.service.d.ts +30 -0
  288. package/dist/conversations/attachments.service.js +130 -5
  289. package/dist/conversations/conversations.controller.d.ts +91 -44
  290. package/dist/conversations/conversations.controller.js +161 -82
  291. package/dist/conversations/conversations.module.d.ts +1 -1
  292. package/dist/conversations/conversations.module.js +10 -7
  293. package/dist/conversations/conversations.schemas.d.ts +60 -0
  294. package/dist/conversations/conversations.schemas.js +93 -0
  295. package/dist/conversations/conversations.service.d.ts +291 -23
  296. package/dist/conversations/conversations.service.js +479 -82
  297. package/dist/conversations/device-status.d.ts +24 -0
  298. package/dist/conversations/device-status.js +71 -0
  299. package/dist/conversations/event-log.d.ts +61 -18
  300. package/dist/conversations/event-log.js +135 -21
  301. package/dist/conversations/events.d.ts +518 -17
  302. package/dist/conversations/events.js +514 -32
  303. package/dist/conversations/message-context.d.ts +82 -0
  304. package/dist/conversations/message-context.js +149 -0
  305. package/dist/conversations/queue.d.ts +20 -4
  306. package/dist/conversations/queue.js +44 -5
  307. package/dist/conversations/queue.service.d.ts +20 -9
  308. package/dist/conversations/queue.service.js +88 -23
  309. package/dist/conversations/source.d.ts +10 -0
  310. package/dist/conversations/source.js +17 -0
  311. package/dist/conversations/titles.d.ts +33 -11
  312. package/dist/conversations/titles.js +76 -25
  313. package/dist/conversations/tree.d.ts +27 -0
  314. package/dist/conversations/tree.js +77 -0
  315. package/dist/ctx/create-ctx.d.ts +48 -5
  316. package/dist/ctx/create-ctx.js +174 -34
  317. package/dist/db/kernel-schema.d.ts +819 -33
  318. package/dist/db/pool.d.ts +5 -0
  319. package/dist/db/pool.js +3 -0
  320. package/dist/devices/access.d.ts +66 -0
  321. package/dist/devices/access.js +113 -0
  322. package/dist/devices/conversation-devices.d.ts +107 -0
  323. package/dist/devices/conversation-devices.js +315 -0
  324. package/dist/devices/devices.controller.d.ts +52 -0
  325. package/dist/devices/devices.controller.js +161 -0
  326. package/dist/devices/devices.module.d.ts +8 -0
  327. package/dist/devices/devices.module.js +34 -0
  328. package/dist/devices/devices.schemas.d.ts +11 -0
  329. package/dist/devices/devices.schemas.js +22 -0
  330. package/dist/devices/devices.service.d.ts +409 -0
  331. package/dist/devices/devices.service.js +1069 -0
  332. package/dist/devices/dto.d.ts +54 -0
  333. package/dist/devices/dto.js +58 -0
  334. package/dist/devices/label.d.ts +11 -0
  335. package/dist/devices/label.js +25 -0
  336. package/dist/devices/legacy-cleanup.job.d.ts +34 -0
  337. package/dist/devices/legacy-cleanup.job.js +141 -0
  338. package/dist/devices/public-suffix.d.ts +6 -0
  339. package/dist/devices/public-suffix.js +79 -0
  340. package/dist/errors.d.ts +281 -8
  341. package/dist/errors.js +281 -8
  342. package/dist/evals/evals.module.js +16 -3
  343. package/dist/evals/evals.schemas.d.ts +110 -0
  344. package/dist/evals/evals.schemas.js +107 -0
  345. package/dist/evals/jobs.d.ts +14 -6
  346. package/dist/evals/jobs.js +21 -19
  347. package/dist/evals/plan.d.ts +20 -15
  348. package/dist/evals/plan.js +29 -6
  349. package/dist/evals/reconstruct.d.ts +52 -23
  350. package/dist/evals/reconstruct.js +137 -43
  351. package/dist/evals/recorded-tools.d.ts +11 -1
  352. package/dist/evals/recorded-tools.js +39 -3
  353. package/dist/evals/run-replay.d.ts +28 -5
  354. package/dist/evals/run-replay.js +101 -18
  355. package/dist/evals/runs.controller.d.ts +2 -57
  356. package/dist/evals/runs.controller.js +1 -42
  357. package/dist/evals/runs.service.d.ts +13 -13
  358. package/dist/evals/runs.service.js +55 -62
  359. package/dist/evals/sets.controller.d.ts +2 -28
  360. package/dist/evals/sets.controller.js +1 -31
  361. package/dist/evals/sets.service.d.ts +10 -9
  362. package/dist/evals/sets.service.js +39 -19
  363. package/dist/evals/snapshot.d.ts +10 -3
  364. package/dist/evals/snapshot.js +19 -5
  365. package/dist/evals/step-replay.d.ts +10 -6
  366. package/dist/evals/step-replay.js +21 -9
  367. package/dist/evals/trajectory.d.ts +19 -3
  368. package/dist/evals/trajectory.js +14 -1
  369. package/dist/evals/trajectory.service.d.ts +8 -3
  370. package/dist/evals/trajectory.service.js +26 -14
  371. package/dist/evals/worker.d.ts +5 -5
  372. package/dist/evals/worker.js +11 -4
  373. package/dist/extensions/admin-ops.d.ts +81 -14
  374. package/dist/extensions/admin-ops.js +181 -47
  375. package/dist/extensions/admin-view.d.ts +57 -10
  376. package/dist/extensions/admin-view.js +249 -27
  377. package/dist/extensions/approvals.d.ts +12 -2
  378. package/dist/extensions/blobs.d.ts +14 -1
  379. package/dist/extensions/blobs.js +49 -5
  380. package/dist/extensions/bootstrap.d.ts +85 -10
  381. package/dist/extensions/bootstrap.js +215 -36
  382. package/dist/extensions/cli.d.ts +12 -0
  383. package/dist/extensions/cli.js +11 -2
  384. package/dist/extensions/config-hash.d.ts +10 -0
  385. package/dist/extensions/config-hash.js +38 -0
  386. package/dist/extensions/config-issues.d.ts +13 -1
  387. package/dist/extensions/config-issues.js +22 -5
  388. package/dist/extensions/config-json-schema.d.ts +43 -7
  389. package/dist/extensions/config-json-schema.js +124 -58
  390. package/dist/extensions/deployment-config-store.d.ts +6 -0
  391. package/dist/extensions/deployment-config.d.ts +94 -9
  392. package/dist/extensions/deployment-config.js +255 -34
  393. package/dist/extensions/display-name.d.ts +22 -0
  394. package/dist/extensions/display-name.js +77 -0
  395. package/dist/extensions/effective-config.d.ts +133 -0
  396. package/dist/extensions/effective-config.js +379 -0
  397. package/dist/extensions/extensions.module.js +34 -12
  398. package/dist/extensions/guide-places.d.ts +68 -0
  399. package/dist/extensions/guide-places.js +152 -0
  400. package/dist/extensions/hooks.d.ts +59 -7
  401. package/dist/extensions/hooks.js +97 -17
  402. package/dist/extensions/host.d.ts +105 -12
  403. package/dist/extensions/host.js +372 -87
  404. package/dist/extensions/installs.d.ts +30 -9
  405. package/dist/extensions/installs.js +57 -16
  406. package/dist/extensions/instance.d.ts +17 -2
  407. package/dist/extensions/instance.js +21 -0
  408. package/dist/extensions/ledger.d.ts +1 -1
  409. package/dist/extensions/lifecycle-hooks.d.ts +99 -0
  410. package/dist/extensions/lifecycle-hooks.js +273 -0
  411. package/dist/extensions/lifecycle.d.ts +6 -1
  412. package/dist/extensions/lifecycle.js +9 -2
  413. package/dist/extensions/locks.d.ts +12 -0
  414. package/dist/extensions/locks.js +26 -0
  415. package/dist/extensions/org-config.d.ts +110 -8
  416. package/dist/extensions/org-config.js +234 -47
  417. package/dist/extensions/org-levels.d.ts +37 -0
  418. package/dist/extensions/org-levels.js +71 -0
  419. package/dist/extensions/org-overrides.d.ts +151 -0
  420. package/dist/extensions/org-overrides.js +524 -0
  421. package/dist/extensions/package.d.ts +18 -4
  422. package/dist/extensions/package.js +57 -4
  423. package/dist/extensions/permission-set.d.ts +35 -5
  424. package/dist/extensions/permission-set.js +36 -5
  425. package/dist/extensions/permission-view.d.ts +112 -15
  426. package/dist/extensions/permission-view.js +40 -17
  427. package/dist/extensions/plugin-guides.d.ts +282 -0
  428. package/dist/extensions/plugin-guides.js +554 -0
  429. package/dist/extensions/plugin-icons.d.ts +54 -0
  430. package/dist/extensions/plugin-icons.js +125 -0
  431. package/dist/extensions/plugin-items.d.ts +21 -0
  432. package/dist/extensions/plugin-items.js +42 -0
  433. package/dist/extensions/point-defaults.controller.d.ts +28 -0
  434. package/dist/extensions/point-defaults.controller.js +69 -0
  435. package/dist/extensions/point-defaults.d.ts +100 -0
  436. package/dist/extensions/point-defaults.js +267 -0
  437. package/dist/extensions/point-defaults.schemas.d.ts +9 -0
  438. package/dist/extensions/point-defaults.schemas.js +14 -0
  439. package/dist/extensions/registry.d.ts +1 -1
  440. package/dist/extensions/registry.js +2 -0
  441. package/dist/extensions/resolve.d.ts +51 -1
  442. package/dist/extensions/resolve.js +85 -3
  443. package/dist/extensions/single-provider.d.ts +92 -0
  444. package/dist/extensions/single-provider.js +180 -0
  445. package/dist/extensions/sources.d.ts +13 -2
  446. package/dist/extensions/sources.js +2 -0
  447. package/dist/extensions/state.module.d.ts +9 -0
  448. package/dist/extensions/state.module.js +46 -0
  449. package/dist/extensions/unavailable.d.ts +50 -0
  450. package/dist/extensions/unavailable.js +115 -0
  451. package/dist/extensions/usage-subscribers.d.ts +43 -0
  452. package/dist/extensions/usage-subscribers.js +108 -0
  453. package/dist/files/previewers.controller.js +2 -3
  454. package/dist/files/previewers.d.ts +10 -3
  455. package/dist/files/previewers.js +40 -11
  456. package/dist/files/previewers.schemas.d.ts +10 -0
  457. package/dist/files/previewers.schemas.js +9 -0
  458. package/dist/git/gateway.d.ts +93 -0
  459. package/dist/git/gateway.js +750 -0
  460. package/dist/git/gateway.route.d.ts +16 -0
  461. package/dist/git/gateway.route.js +41 -0
  462. package/dist/git/git.module.d.ts +10 -0
  463. package/dist/git/git.module.js +31 -0
  464. package/dist/git/identity.d.ts +80 -0
  465. package/dist/git/identity.js +209 -0
  466. package/dist/git/pkt-line.d.ts +53 -0
  467. package/dist/git/pkt-line.js +146 -0
  468. package/dist/git/run-end.d.ts +29 -0
  469. package/dist/git/run-end.js +121 -0
  470. package/dist/git/sources.d.ts +42 -0
  471. package/dist/git/sources.js +134 -0
  472. package/dist/git/tickets.d.ts +69 -0
  473. package/dist/git/tickets.js +194 -0
  474. package/dist/http/api.module.js +86 -4
  475. package/dist/http/app.d.ts +17 -2
  476. package/dist/http/app.js +34 -5
  477. package/dist/http/gateway-routes.d.ts +7 -3
  478. package/dist/http/gateway-routes.js +6 -3
  479. package/dist/http/nest-logger.d.ts +6 -5
  480. package/dist/http/nest-logger.js +24 -10
  481. package/dist/http/plugin-dispatch.d.ts +4 -1
  482. package/dist/http/plugin-dispatch.js +14 -2
  483. package/dist/identity/accounts.d.ts +77 -31
  484. package/dist/identity/accounts.js +133 -52
  485. package/dist/identity/audit.d.ts +2 -1
  486. package/dist/identity/auth.controller.js +9 -5
  487. package/dist/identity/auth.guard.d.ts +13 -5
  488. package/dist/identity/auth.guard.js +35 -9
  489. package/dist/identity/cookies.d.ts +26 -5
  490. package/dist/identity/cookies.js +58 -6
  491. package/dist/identity/identity.module.d.ts +1 -1
  492. package/dist/identity/identity.module.js +7 -4
  493. package/dist/identity/initial-passwords.controller.d.ts +22 -0
  494. package/dist/identity/initial-passwords.controller.js +49 -0
  495. package/dist/identity/initial-passwords.d.ts +61 -0
  496. package/dist/identity/initial-passwords.job.d.ts +16 -0
  497. package/dist/identity/initial-passwords.job.js +46 -0
  498. package/dist/identity/initial-passwords.js +159 -0
  499. package/dist/identity/me.controller.d.ts +17 -1
  500. package/dist/identity/me.controller.js +45 -4
  501. package/dist/identity/me.schemas.d.ts +24 -0
  502. package/dist/identity/me.schemas.js +25 -0
  503. package/dist/identity/principal.d.ts +11 -3
  504. package/dist/identity/principal.js +11 -3
  505. package/dist/identity/sessions.d.ts +12 -0
  506. package/dist/identity/sessions.js +45 -0
  507. package/dist/identity/settings.d.ts +216 -12
  508. package/dist/identity/settings.js +420 -29
  509. package/dist/identity/{enterprise-admin.d.ts → super-admin.d.ts} +5 -5
  510. package/dist/identity/{enterprise-admin.js → super-admin.js} +24 -24
  511. package/dist/identity/write-actor.d.ts +11 -0
  512. package/dist/identity/write-actor.js +7 -0
  513. package/dist/inbox/inbox.module.d.ts +4 -1
  514. package/dist/inbox/inbox.module.js +9 -4
  515. package/dist/inbox/inbox.schemas.d.ts +8 -0
  516. package/dist/inbox/inbox.schemas.js +11 -0
  517. package/dist/inbox/inbox.service.d.ts +65 -4
  518. package/dist/inbox/inbox.service.js +82 -4
  519. package/dist/inbox/org-notices.d.ts +63 -0
  520. package/dist/inbox/org-notices.js +91 -0
  521. package/dist/inbox/platform-inbox.controller.d.ts +23 -0
  522. package/dist/inbox/platform-inbox.controller.js +58 -0
  523. package/dist/inbox/platform-inbox.service.d.ts +128 -0
  524. package/dist/inbox/platform-inbox.service.js +232 -0
  525. package/dist/inbox/platform-notices.d.ts +19 -0
  526. package/dist/inbox/platform-notices.js +9 -0
  527. package/dist/index.d.ts +7 -2
  528. package/dist/index.js +8 -1
  529. package/dist/infra/cli/kernel-cli.module.js +14 -1
  530. package/dist/infra/config/kernel-config.service.d.ts +7 -0
  531. package/dist/infra/config/kernel-config.service.js +70 -1
  532. package/dist/infra/database/database.module.js +1 -0
  533. package/dist/infra/database/xact-lock.d.ts +1 -1
  534. package/dist/infra/database/xact-lock.js +1 -1
  535. package/dist/infra/events/kernel-events.d.ts +28 -25
  536. package/dist/infra/lifecycle/process-guards.js +4 -0
  537. package/dist/infra/lock/lock.service.d.ts +8 -0
  538. package/dist/infra/lock/lock.service.js +27 -0
  539. package/dist/infra/logging/logging.module.d.ts +4 -2
  540. package/dist/infra/logging/logging.module.js +24 -6
  541. package/dist/infra/outbox/outbox-relay.module.d.ts +6 -0
  542. package/dist/infra/outbox/outbox-relay.module.js +18 -0
  543. package/dist/infra/outbox/outbox.dispatcher.d.ts +103 -0
  544. package/dist/infra/outbox/outbox.dispatcher.js +310 -0
  545. package/dist/infra/outbox/outbox.module.d.ts +24 -0
  546. package/dist/infra/outbox/outbox.module.js +50 -0
  547. package/dist/infra/outbox/outbox.relay.d.ts +31 -0
  548. package/dist/infra/outbox/outbox.relay.js +76 -0
  549. package/dist/infra/outbox/outbox.service.d.ts +57 -0
  550. package/dist/infra/outbox/outbox.service.js +165 -0
  551. package/dist/infra/outbox/registry.d.ts +296 -0
  552. package/dist/infra/outbox/registry.js +291 -0
  553. package/dist/infra/presence/presence.module.d.ts +6 -0
  554. package/dist/infra/presence/presence.module.js +22 -0
  555. package/dist/infra/presence/replica-presence.d.ts +87 -0
  556. package/dist/infra/presence/replica-presence.js +189 -0
  557. package/dist/infra/queue/queue-names.d.ts +8 -0
  558. package/dist/infra/queue/queue-names.js +17 -0
  559. package/dist/infra/queue/redis-connection.d.ts +6 -0
  560. package/dist/infra/queue/redis-connection.js +7 -0
  561. package/dist/infra/redis/redis.module.js +6 -2
  562. package/dist/infra/throttler/named-throttlers.d.ts +12 -0
  563. package/dist/infra/throttler/named-throttlers.js +67 -0
  564. package/dist/infra/throttler/throttler.guard.js +1 -1
  565. package/dist/infra/tokens.d.ts +6 -1
  566. package/dist/infra/tokens.js +6 -1
  567. package/dist/kernel.module.js +18 -0
  568. package/dist/lifecycle/kernel-lifecycle.d.ts +9 -3
  569. package/dist/lifecycle/kernel-lifecycle.js +48 -11
  570. package/dist/local-devices/device-auth.controller.d.ts +42 -0
  571. package/dist/local-devices/device-auth.controller.js +116 -0
  572. package/dist/local-devices/device-auth.d.ts +124 -0
  573. package/dist/local-devices/device-auth.js +410 -0
  574. package/dist/local-devices/device-auth.module.d.ts +7 -0
  575. package/dist/local-devices/device-auth.module.js +25 -0
  576. package/dist/local-devices/device-policy.service.d.ts +174 -0
  577. package/dist/local-devices/device-policy.service.js +470 -0
  578. package/dist/local-devices/device-routes.d.ts +36 -0
  579. package/dist/local-devices/device-routes.js +90 -0
  580. package/dist/local-devices/dto.d.ts +66 -0
  581. package/dist/local-devices/dto.js +44 -0
  582. package/dist/local-devices/keys.d.ts +25 -0
  583. package/dist/local-devices/keys.js +25 -0
  584. package/dist/local-devices/local-device-gateway.d.ts +136 -0
  585. package/dist/local-devices/local-device-gateway.js +570 -0
  586. package/dist/local-devices/local-device-provider.d.ts +116 -0
  587. package/dist/local-devices/local-device-provider.js +203 -0
  588. package/dist/local-devices/local-devices.module.d.ts +17 -0
  589. package/dist/local-devices/local-devices.module.js +60 -0
  590. package/dist/local-devices/machines.d.ts +163 -0
  591. package/dist/local-devices/machines.js +540 -0
  592. package/dist/local-devices/network-grants.d.ts +88 -0
  593. package/dist/local-devices/network-grants.js +268 -0
  594. package/dist/local-devices/policy-op-jobs.d.ts +15 -0
  595. package/dist/local-devices/policy-op-jobs.js +12 -0
  596. package/dist/local-devices/policy-summary.d.ts +98 -0
  597. package/dist/local-devices/policy-summary.js +151 -0
  598. package/dist/local-devices/relay.d.ts +100 -0
  599. package/dist/local-devices/relay.js +373 -0
  600. package/dist/local-devices/targets.d.ts +28 -0
  601. package/dist/local-devices/targets.js +73 -0
  602. package/dist/log.d.ts +13 -1
  603. package/dist/log.js +93 -17
  604. package/dist/models/embeddings.d.ts +82 -0
  605. package/dist/models/embeddings.js +319 -0
  606. package/dist/models/enablement.controller.d.ts +40 -0
  607. package/dist/models/enablement.controller.js +74 -0
  608. package/dist/models/enablement.d.ts +101 -0
  609. package/dist/models/enablement.js +264 -0
  610. package/dist/models/models.module.d.ts +5 -1
  611. package/dist/models/models.module.js +19 -4
  612. package/dist/models/org-models.schemas.d.ts +8 -0
  613. package/dist/models/org-models.schemas.js +22 -0
  614. package/dist/models/prices.d.ts +8 -6
  615. package/dist/models/prices.js +11 -9
  616. package/dist/models/purposes.controller.d.ts +6 -4
  617. package/dist/models/purposes.controller.js +13 -7
  618. package/dist/models/purposes.d.ts +79 -14
  619. package/dist/models/purposes.js +120 -34
  620. package/dist/orgs/directory-sync.d.ts +14 -5
  621. package/dist/orgs/directory-sync.js +42 -18
  622. package/dist/orgs/members.service.js +1 -1
  623. package/dist/orgs/offboarding.d.ts +54 -57
  624. package/dist/orgs/offboarding.js +161 -185
  625. package/dist/orgs/org-lock.d.ts +5 -0
  626. package/dist/orgs/org-lock.js +13 -0
  627. package/dist/orgs/org.guard.d.ts +10 -0
  628. package/dist/orgs/org.guard.js +43 -32
  629. package/dist/orgs/orgs.controller.d.ts +2 -5
  630. package/dist/orgs/orgs.controller.js +3 -6
  631. package/dist/orgs/orgs.module.js +10 -10
  632. package/dist/orgs/orgs.schemas.d.ts +8 -0
  633. package/dist/orgs/orgs.schemas.js +16 -0
  634. package/dist/orgs/orgs.service.d.ts +29 -8
  635. package/dist/orgs/orgs.service.js +57 -16
  636. package/dist/orgs/roles.d.ts +1 -1
  637. package/dist/orgs/roles.js +11 -1
  638. package/dist/orgs/settings.controller.d.ts +1 -1
  639. package/dist/orgs/settings.controller.js +4 -3
  640. package/dist/orgs/settings.service.d.ts +278 -31
  641. package/dist/orgs/settings.service.js +357 -47
  642. package/dist/overrides.d.ts +49 -4
  643. package/dist/overrides.js +28 -2
  644. package/dist/platform/actor.d.ts +48 -0
  645. package/dist/platform/actor.js +100 -0
  646. package/dist/platform/agent-docs.d.ts +18 -0
  647. package/dist/platform/agent-docs.js +59 -0
  648. package/dist/platform/availability.d.ts +14 -0
  649. package/dist/platform/availability.js +43 -0
  650. package/dist/platform/errors.d.ts +28 -0
  651. package/dist/platform/errors.js +65 -0
  652. package/dist/platform/exclusions.d.ts +11 -0
  653. package/dist/platform/exclusions.js +792 -0
  654. package/dist/platform/method.d.ts +6 -0
  655. package/dist/platform/method.js +15 -0
  656. package/dist/platform/operation.d.ts +169 -0
  657. package/dist/platform/operation.js +106 -0
  658. package/dist/platform/ops/accounts.ops.d.ts +32 -0
  659. package/dist/platform/ops/accounts.ops.js +291 -0
  660. package/dist/platform/ops/admin-items.ops.d.ts +37 -0
  661. package/dist/platform/ops/admin-items.ops.js +278 -0
  662. package/dist/platform/ops/admin.ops.d.ts +38 -0
  663. package/dist/platform/ops/admin.ops.js +546 -0
  664. package/dist/platform/ops/agent-skills.ops.d.ts +50 -0
  665. package/dist/platform/ops/agent-skills.ops.js +509 -0
  666. package/dist/platform/ops/agent-templates.ops.d.ts +17 -0
  667. package/dist/platform/ops/agent-templates.ops.js +272 -0
  668. package/dist/platform/ops/agents.ops.d.ts +37 -0
  669. package/dist/platform/ops/agents.ops.js +726 -0
  670. package/dist/platform/ops/approvals.ops.d.ts +30 -0
  671. package/dist/platform/ops/approvals.ops.js +116 -0
  672. package/dist/platform/ops/background.ops.d.ts +14 -0
  673. package/dist/platform/ops/background.ops.js +134 -0
  674. package/dist/platform/ops/catalog-admin.ops.d.ts +94 -0
  675. package/dist/platform/ops/catalog-admin.ops.js +710 -0
  676. package/dist/platform/ops/channels.ops.d.ts +16 -0
  677. package/dist/platform/ops/channels.ops.js +195 -0
  678. package/dist/platform/ops/components.ops.d.ts +46 -0
  679. package/dist/platform/ops/components.ops.js +854 -0
  680. package/dist/platform/ops/connectors.ops.d.ts +17 -0
  681. package/dist/platform/ops/connectors.ops.js +190 -0
  682. package/dist/platform/ops/conversations.ops.d.ts +30 -0
  683. package/dist/platform/ops/conversations.ops.js +504 -0
  684. package/dist/platform/ops/devices.ops.d.ts +26 -0
  685. package/dist/platform/ops/devices.ops.js +313 -0
  686. package/dist/platform/ops/errors.d.ts +16 -0
  687. package/dist/platform/ops/errors.js +30 -0
  688. package/dist/platform/ops/evals.ops.d.ts +28 -0
  689. package/dist/platform/ops/evals.ops.js +794 -0
  690. package/dist/platform/ops/extensions-admin.ops.d.ts +25 -0
  691. package/dist/platform/ops/extensions-admin.ops.js +372 -0
  692. package/dist/platform/ops/extensions.ops.d.ts +25 -0
  693. package/dist/platform/ops/extensions.ops.js +347 -0
  694. package/dist/platform/ops/inbox.ops.d.ts +25 -0
  695. package/dist/platform/ops/inbox.ops.js +384 -0
  696. package/dist/platform/ops/marketplaces.ops.d.ts +22 -0
  697. package/dist/platform/ops/marketplaces.ops.js +466 -0
  698. package/dist/platform/ops/models.ops.d.ts +25 -0
  699. package/dist/platform/ops/models.ops.js +232 -0
  700. package/dist/platform/ops/org-plugins.ops.d.ts +35 -0
  701. package/dist/platform/ops/org-plugins.ops.js +388 -0
  702. package/dist/platform/ops/org-settings.ops.d.ts +18 -0
  703. package/dist/platform/ops/org-settings.ops.js +134 -0
  704. package/dist/platform/ops/orgs.ops.d.ts +28 -0
  705. package/dist/platform/ops/orgs.ops.js +301 -0
  706. package/dist/platform/ops/other-orgs.d.ts +20 -0
  707. package/dist/platform/ops/other-orgs.js +72 -0
  708. package/dist/platform/ops/outbox.ops.d.ts +21 -0
  709. package/dist/platform/ops/outbox.ops.js +125 -0
  710. package/dist/platform/ops/paging.d.ts +14 -0
  711. package/dist/platform/ops/paging.js +20 -0
  712. package/dist/platform/ops/platform.ops.d.ts +39 -0
  713. package/dist/platform/ops/platform.ops.js +263 -0
  714. package/dist/platform/ops/plugins.ops.d.ts +35 -0
  715. package/dist/platform/ops/plugins.ops.js +476 -0
  716. package/dist/platform/ops/previewers.ops.d.ts +14 -0
  717. package/dist/platform/ops/previewers.ops.js +109 -0
  718. package/dist/platform/ops/requests.ops.d.ts +14 -0
  719. package/dist/platform/ops/requests.ops.js +192 -0
  720. package/dist/platform/ops/role.d.ts +4 -0
  721. package/dist/platform/ops/role.js +10 -0
  722. package/dist/platform/ops/schedules.ops.d.ts +21 -0
  723. package/dist/platform/ops/schedules.ops.js +509 -0
  724. package/dist/platform/ops/setup.ops.d.ts +24 -0
  725. package/dist/platform/ops/setup.ops.js +212 -0
  726. package/dist/platform/ops/skills.ops.d.ts +17 -0
  727. package/dist/platform/ops/skills.ops.js +160 -0
  728. package/dist/platform/ops/summary.d.ts +10 -0
  729. package/dist/platform/ops/summary.js +18 -0
  730. package/dist/platform/ops/themes.ops.d.ts +14 -0
  731. package/dist/platform/ops/themes.ops.js +47 -0
  732. package/dist/platform/ops/ui.ops.d.ts +8 -0
  733. package/dist/platform/ops/ui.ops.js +41 -0
  734. package/dist/platform/ops/unavailable.d.ts +23 -0
  735. package/dist/platform/ops/unavailable.js +118 -0
  736. package/dist/platform/parity.d.ts +33 -0
  737. package/dist/platform/parity.js +272 -0
  738. package/dist/platform/platform-ops.module.d.ts +45 -0
  739. package/dist/platform/platform-ops.module.js +150 -0
  740. package/dist/platform/platform.module.d.ts +8 -0
  741. package/dist/platform/platform.module.js +25 -0
  742. package/dist/platform/registry.d.ts +30 -0
  743. package/dist/platform/registry.js +111 -0
  744. package/dist/platform/route-schemas.d.ts +18 -0
  745. package/dist/platform/route-schemas.js +91 -0
  746. package/dist/roles/gateway-role.d.ts +18 -3
  747. package/dist/roles/gateway-role.js +33 -5
  748. package/dist/roles/gateway.module.d.ts +2 -1
  749. package/dist/roles/gateway.module.js +6 -3
  750. package/dist/roles/worker-role.d.ts +11 -4
  751. package/dist/roles/worker-role.js +24 -7
  752. package/dist/roles/worker.module.d.ts +1 -1
  753. package/dist/roles/worker.module.js +33 -7
  754. package/dist/runtime/compaction-progress.d.ts +62 -0
  755. package/dist/runtime/compaction-progress.js +82 -0
  756. package/dist/runtime/compaction-settings.service.d.ts +16 -0
  757. package/dist/runtime/compaction-settings.service.js +42 -0
  758. package/dist/runtime/compaction.service.d.ts +50 -0
  759. package/dist/runtime/compaction.service.js +75 -0
  760. package/dist/runtime/context/build.d.ts +18 -7
  761. package/dist/runtime/context/build.js +115 -21
  762. package/dist/runtime/context/claude-tools.d.ts +3 -1
  763. package/dist/runtime/context/claude-tools.js +8 -6
  764. package/dist/runtime/context/compaction.d.ts +90 -12
  765. package/dist/runtime/context/compaction.js +284 -23
  766. package/dist/runtime/context/device-paths.d.ts +82 -0
  767. package/dist/runtime/context/device-paths.js +165 -0
  768. package/dist/runtime/context/fit.d.ts +79 -0
  769. package/dist/runtime/context/fit.js +202 -0
  770. package/dist/runtime/context/plugin-context.d.ts +20 -0
  771. package/dist/runtime/context/plugin-context.js +30 -0
  772. package/dist/runtime/context/reminders.d.ts +2 -0
  773. package/dist/runtime/context/reminders.js +2 -0
  774. package/dist/runtime/context/run-context.d.ts +88 -0
  775. package/dist/runtime/context/run-context.js +296 -0
  776. package/dist/runtime/context/skills.d.ts +14 -1
  777. package/dist/runtime/context/skills.js +45 -7
  778. package/dist/runtime/context/tool-changes.d.ts +27 -0
  779. package/dist/runtime/context/tool-changes.js +60 -0
  780. package/dist/runtime/hook-calls.d.ts +8 -1
  781. package/dist/runtime/hook-calls.js +40 -7
  782. package/dist/runtime/langgraph-loop.d.ts +3 -1
  783. package/dist/runtime/langgraph-loop.js +52 -21
  784. package/dist/runtime/limits.d.ts +15 -0
  785. package/dist/runtime/limits.js +25 -0
  786. package/dist/runtime/loop.d.ts +50 -8
  787. package/dist/runtime/loop.js +2 -1
  788. package/dist/runtime/maintenance.d.ts +19 -5
  789. package/dist/runtime/maintenance.js +102 -10
  790. package/dist/runtime/model-call.d.ts +199 -24
  791. package/dist/runtime/model-call.js +854 -355
  792. package/dist/runtime/plugin-context.service.d.ts +75 -0
  793. package/dist/runtime/plugin-context.service.js +220 -0
  794. package/dist/runtime/providers.d.ts +149 -29
  795. package/dist/runtime/providers.js +332 -63
  796. package/dist/runtime/queue-lost-jobs.d.ts +24 -0
  797. package/dist/runtime/queue-lost-jobs.js +51 -0
  798. package/dist/runtime/run-context.service.d.ts +82 -0
  799. package/dist/runtime/run-context.service.js +270 -0
  800. package/dist/runtime/run-control.d.ts +62 -5
  801. package/dist/runtime/run-control.js +98 -12
  802. package/dist/runtime/run-drafts.d.ts +85 -0
  803. package/dist/runtime/run-drafts.js +230 -0
  804. package/dist/runtime/run-finish.d.ts +49 -12
  805. package/dist/runtime/run-finish.js +125 -14
  806. package/dist/runtime/run-jobs.d.ts +112 -15
  807. package/dist/runtime/run-jobs.js +224 -32
  808. package/dist/runtime/run-orphan-check.job.d.ts +145 -0
  809. package/dist/runtime/run-orphan-check.job.js +344 -0
  810. package/dist/runtime/run-processors.d.ts +16 -9
  811. package/dist/runtime/run-processors.js +1 -1
  812. package/dist/runtime/run-queue.d.ts +134 -49
  813. package/dist/runtime/run-queue.js +519 -228
  814. package/dist/runtime/run-status.d.ts +12 -0
  815. package/dist/runtime/run-status.js +21 -0
  816. package/dist/runtime/run-steps.d.ts +3 -2
  817. package/dist/runtime/run-steps.js +15 -11
  818. package/dist/runtime/run-worker.d.ts +63 -22
  819. package/dist/runtime/run-worker.js +406 -117
  820. package/dist/runtime/runtime.module.d.ts +2 -3
  821. package/dist/runtime/runtime.module.js +36 -9
  822. package/dist/runtime/step-log.d.ts +31 -7
  823. package/dist/runtime/step-log.js +49 -11
  824. package/dist/runtime/stream.controller.d.ts +24 -4
  825. package/dist/runtime/stream.controller.js +60 -9
  826. package/dist/runtime/stream.d.ts +4 -1
  827. package/dist/runtime/stream.js +42 -3
  828. package/dist/runtime/subagents/child.d.ts +55 -7
  829. package/dist/runtime/subagents/child.js +106 -8
  830. package/dist/runtime/subagents/definition.d.ts +54 -25
  831. package/dist/runtime/subagents/definition.js +164 -212
  832. package/dist/runtime/subagents/deliveries.d.ts +74 -0
  833. package/dist/runtime/subagents/deliveries.js +237 -0
  834. package/dist/runtime/subagents/dispatch.d.ts +121 -32
  835. package/dist/runtime/subagents/dispatch.js +221 -173
  836. package/dist/runtime/subagents/limits.d.ts +24 -5
  837. package/dist/runtime/subagents/limits.js +40 -6
  838. package/dist/runtime/subagents/messages.d.ts +99 -0
  839. package/dist/runtime/subagents/messages.js +336 -0
  840. package/dist/runtime/subagents/preamble.d.ts +5 -4
  841. package/dist/runtime/subagents/preamble.js +10 -7
  842. package/dist/runtime/subagents/progress.d.ts +8 -6
  843. package/dist/runtime/subagents/progress.js +11 -6
  844. package/dist/runtime/subagents/render.d.ts +30 -0
  845. package/dist/runtime/subagents/render.js +53 -0
  846. package/dist/runtime/subagents/roster.d.ts +24 -7
  847. package/dist/runtime/subagents/roster.js +47 -11
  848. package/dist/runtime/subagents/skill-fork.d.ts +6 -5
  849. package/dist/runtime/subagents/skill-fork.js +16 -9
  850. package/dist/runtime/subagents/status.d.ts +14 -0
  851. package/dist/runtime/subagents/status.js +23 -0
  852. package/dist/runtime/subagents/subagents.service.d.ts +7 -4
  853. package/dist/runtime/subagents/subagents.service.js +12 -7
  854. package/dist/runtime/subagents/tree-jobs.d.ts +61 -0
  855. package/dist/runtime/subagents/tree-jobs.js +86 -0
  856. package/dist/runtime/usage-report-jobs.d.ts +63 -0
  857. package/dist/runtime/usage-report-jobs.js +106 -0
  858. package/dist/runtime/usage-report.d.ts +48 -98
  859. package/dist/runtime/usage-report.js +142 -243
  860. package/dist/runtime/waits.d.ts +134 -165
  861. package/dist/runtime/waits.js +352 -706
  862. package/dist/sandbox/jobs.d.ts +50 -13
  863. package/dist/sandbox/jobs.js +64 -21
  864. package/dist/sandbox/leases.d.ts +37 -54
  865. package/dist/sandbox/leases.js +71 -115
  866. package/dist/sandbox/modes.d.ts +108 -0
  867. package/dist/sandbox/modes.js +126 -0
  868. package/dist/sandbox/platform-auto-mode.d.ts +63 -0
  869. package/dist/sandbox/platform-auto-mode.js +63 -0
  870. package/dist/sandbox/providers.d.ts +34 -7
  871. package/dist/sandbox/providers.js +54 -9
  872. package/dist/sandbox/review/classifier.d.ts +133 -0
  873. package/dist/sandbox/review/classifier.js +408 -0
  874. package/dist/sandbox/review/index.d.ts +13 -0
  875. package/dist/sandbox/review/index.js +10 -0
  876. package/dist/sandbox/review/review.service.d.ts +125 -0
  877. package/dist/sandbox/review/review.service.js +301 -0
  878. package/dist/sandbox/review/rules.d.ts +79 -0
  879. package/dist/sandbox/review/rules.js +4278 -0
  880. package/dist/sandbox/review/types.d.ts +85 -0
  881. package/dist/sandbox/review/types.js +12 -0
  882. package/dist/sandbox/sandbox-file-reader.d.ts +35 -0
  883. package/dist/sandbox/sandbox-file-reader.js +98 -0
  884. package/dist/sandbox/sandbox.module.d.ts +3 -2
  885. package/dist/sandbox/sandbox.module.js +21 -5
  886. package/dist/sandbox/sandbox.service.d.ts +463 -133
  887. package/dist/sandbox/sandbox.service.js +1255 -407
  888. package/dist/sandbox/unreachable.d.ts +30 -0
  889. package/dist/sandbox/unreachable.js +108 -0
  890. package/dist/schedules/calendar.d.ts +134 -0
  891. package/dist/schedules/calendar.js +410 -0
  892. package/dist/schedules/effective-settings.d.ts +16 -0
  893. package/dist/schedules/effective-settings.js +17 -0
  894. package/dist/schedules/errors.d.ts +8 -0
  895. package/dist/schedules/errors.js +8 -0
  896. package/dist/schedules/fire.d.ts +137 -0
  897. package/dist/schedules/fire.js +697 -0
  898. package/dist/schedules/grant-rules.d.ts +118 -0
  899. package/dist/schedules/grant-rules.js +184 -0
  900. package/dist/schedules/job-ids.d.ts +81 -0
  901. package/dist/schedules/job-ids.js +95 -0
  902. package/dist/schedules/pause.d.ts +9 -0
  903. package/dist/schedules/pause.js +33 -0
  904. package/dist/schedules/run-tracker.d.ts +76 -0
  905. package/dist/schedules/run-tracker.js +321 -0
  906. package/dist/schedules/scan.job.d.ts +45 -0
  907. package/dist/schedules/scan.job.js +189 -0
  908. package/dist/schedules/schedule-math.d.ts +73 -0
  909. package/dist/schedules/schedule-math.js +275 -0
  910. package/dist/schedules/scheduled-tasks.service.d.ts +362 -0
  911. package/dist/schedules/scheduled-tasks.service.js +1246 -0
  912. package/dist/schedules/schedules.controller.d.ts +72 -0
  913. package/dist/schedules/schedules.controller.js +256 -0
  914. package/dist/schedules/schedules.module.d.ts +16 -0
  915. package/dist/schedules/schedules.module.js +61 -0
  916. package/dist/schedules/schedules.schemas.d.ts +351 -0
  917. package/dist/schedules/schedules.schemas.js +213 -0
  918. package/dist/schedules/store.d.ts +86 -0
  919. package/dist/schedules/store.js +410 -0
  920. package/dist/schedules/store.module.d.ts +6 -0
  921. package/dist/schedules/store.module.js +21 -0
  922. package/dist/schedules/types.d.ts +110 -0
  923. package/dist/schedules/types.js +1 -0
  924. package/dist/schedules/unattended.d.ts +87 -0
  925. package/dist/schedules/unattended.js +184 -0
  926. package/dist/setup/kernel-checks.d.ts +91 -0
  927. package/dist/setup/kernel-checks.js +295 -0
  928. package/dist/setup/oauth-token-probe.d.ts +47 -0
  929. package/dist/setup/oauth-token-probe.js +112 -0
  930. package/dist/setup/redact-check.d.ts +18 -0
  931. package/dist/setup/redact-check.js +46 -0
  932. package/dist/setup/setup-check-registry.d.ts +86 -0
  933. package/dist/setup/setup-check-registry.js +75 -0
  934. package/dist/setup/setup-checks.controller.d.ts +28 -0
  935. package/dist/setup/setup-checks.controller.js +78 -0
  936. package/dist/setup/setup-checks.schemas.d.ts +38 -0
  937. package/dist/setup/setup-checks.schemas.js +43 -0
  938. package/dist/setup/setup-checks.service.d.ts +144 -0
  939. package/dist/setup/setup-checks.service.js +683 -0
  940. package/dist/setup/setup-guides.controller.d.ts +18 -0
  941. package/dist/setup/setup-guides.controller.js +48 -0
  942. package/dist/setup/setup-guides.schemas.d.ts +10 -0
  943. package/dist/setup/setup-guides.schemas.js +6 -0
  944. package/dist/setup/setup-guides.service.d.ts +126 -0
  945. package/dist/setup/setup-guides.service.js +315 -0
  946. package/dist/setup/setup.module.d.ts +8 -0
  947. package/dist/setup/setup.module.js +31 -0
  948. package/dist/skills/agent-skills.controller.d.ts +48 -0
  949. package/dist/skills/agent-skills.controller.js +131 -0
  950. package/dist/skills/agent-skills.service.d.ts +258 -0
  951. package/dist/skills/agent-skills.service.js +961 -0
  952. package/dist/skills/catalog.d.ts +80 -70
  953. package/dist/skills/catalog.js +136 -241
  954. package/dist/skills/changes.controller.d.ts +12 -22
  955. package/dist/skills/changes.controller.js +27 -56
  956. package/dist/skills/changes.service.d.ts +69 -115
  957. package/dist/skills/changes.service.js +249 -599
  958. package/dist/skills/conversation-catalog.d.ts +53 -0
  959. package/dist/skills/conversation-catalog.js +142 -0
  960. package/dist/skills/invocable.controller.d.ts +1 -1
  961. package/dist/skills/invocable.controller.js +2 -2
  962. package/dist/skills/invocation-keys.d.ts +1 -1
  963. package/dist/skills/invocation-keys.js +1 -1
  964. package/dist/skills/invoke.d.ts +4 -4
  965. package/dist/skills/invoke.js +16 -4
  966. package/dist/skills/package.d.ts +15 -1
  967. package/dist/skills/package.js +14 -1
  968. package/dist/skills/skills.controller.d.ts +35 -7
  969. package/dist/skills/skills.controller.js +64 -11
  970. package/dist/skills/skills.module.js +25 -3
  971. package/dist/skills/skills.schemas.d.ts +44 -0
  972. package/dist/skills/skills.schemas.js +68 -0
  973. package/dist/skills/skills.service.d.ts +158 -60
  974. package/dist/skills/skills.service.js +640 -162
  975. package/dist/skills/usable.d.ts +131 -0
  976. package/dist/skills/usable.js +522 -0
  977. package/dist/themes/render-css.d.ts +13 -0
  978. package/dist/themes/render-css.js +135 -0
  979. package/dist/themes/theme-delivery.service.d.ts +36 -0
  980. package/dist/themes/theme-delivery.service.js +116 -0
  981. package/dist/themes/theme-state.d.ts +116 -0
  982. package/dist/themes/theme-state.js +221 -0
  983. package/dist/themes/theme-usage.d.ts +61 -0
  984. package/dist/themes/theme-usage.js +98 -0
  985. package/dist/themes/themes-public.controller.d.ts +33 -0
  986. package/dist/themes/themes-public.controller.js +98 -0
  987. package/dist/themes/themes.controller.d.ts +13 -0
  988. package/dist/themes/themes.controller.js +38 -0
  989. package/dist/themes/themes.module.d.ts +3 -0
  990. package/dist/themes/themes.module.js +21 -0
  991. package/dist/themes/themes.schemas.d.ts +3 -0
  992. package/dist/themes/themes.schemas.js +2 -0
  993. package/dist/themes/themes.service.d.ts +50 -0
  994. package/dist/themes/themes.service.js +99 -0
  995. package/dist/time-zone.d.ts +5 -0
  996. package/dist/time-zone.js +19 -0
  997. package/dist/tools/admin-connectors.d.ts +69 -0
  998. package/dist/tools/admin-connectors.js +135 -0
  999. package/dist/tools/approvals.controller.d.ts +4 -2
  1000. package/dist/tools/approvals.controller.js +19 -5
  1001. package/dist/tools/approvals.schemas.d.ts +22 -0
  1002. package/dist/tools/approvals.schemas.js +17 -0
  1003. package/dist/tools/approvals.service.d.ts +479 -19
  1004. package/dist/tools/approvals.service.js +1472 -34
  1005. package/dist/tools/builtin-names.d.ts +78 -22
  1006. package/dist/tools/builtin-names.js +130 -28
  1007. package/dist/tools/builtins/agent.d.ts +23 -21
  1008. package/dist/tools/builtins/agent.js +62 -55
  1009. package/dist/tools/builtins/ask-user.js +3 -2
  1010. package/dist/tools/builtins/builtin-tools.d.ts +29 -3
  1011. package/dist/tools/builtins/builtin-tools.js +102 -11
  1012. package/dist/tools/builtins/current-time.d.ts +26 -0
  1013. package/dist/tools/builtins/current-time.js +99 -0
  1014. package/dist/tools/builtins/handback.d.ts +28 -0
  1015. package/dist/tools/builtins/handback.js +53 -0
  1016. package/dist/tools/builtins/platform.d.ts +51 -0
  1017. package/dist/tools/builtins/platform.js +451 -0
  1018. package/dist/tools/builtins/sandbox/bash.d.ts +3 -3
  1019. package/dist/tools/builtins/sandbox/bash.js +290 -18
  1020. package/dist/tools/builtins/sandbox/common.d.ts +162 -37
  1021. package/dist/tools/builtins/sandbox/common.js +273 -63
  1022. package/dist/tools/builtins/sandbox/deliver.js +9 -6
  1023. package/dist/tools/builtins/sandbox/device-code.d.ts +40 -0
  1024. package/dist/tools/builtins/sandbox/device-code.js +57 -0
  1025. package/dist/tools/builtins/sandbox/edit.js +48 -39
  1026. package/dist/tools/builtins/sandbox/file-state.d.ts +31 -1
  1027. package/dist/tools/builtins/sandbox/file-state.js +33 -3
  1028. package/dist/tools/builtins/sandbox/glob.js +16 -9
  1029. package/dist/tools/builtins/sandbox/grep.js +12 -9
  1030. package/dist/tools/builtins/sandbox/index.d.ts +9 -4
  1031. package/dist/tools/builtins/sandbox/index.js +15 -4
  1032. package/dist/tools/builtins/sandbox/read.js +12 -7
  1033. package/dist/tools/builtins/sandbox/task-list.d.ts +12 -0
  1034. package/dist/tools/builtins/sandbox/task-list.js +130 -0
  1035. package/dist/tools/builtins/sandbox/task-output.d.ts +110 -0
  1036. package/dist/tools/builtins/sandbox/task-output.js +401 -0
  1037. package/dist/tools/builtins/sandbox/task-stop.d.ts +10 -0
  1038. package/dist/tools/builtins/sandbox/task-stop.js +113 -0
  1039. package/dist/tools/builtins/sandbox/write.js +41 -33
  1040. package/dist/tools/builtins/schedules.d.ts +109 -0
  1041. package/dist/tools/builtins/schedules.js +947 -0
  1042. package/dist/tools/builtins/send-message.d.ts +56 -0
  1043. package/dist/tools/builtins/send-message.js +100 -0
  1044. package/dist/tools/builtins/skill-load.d.ts +27 -16
  1045. package/dist/tools/builtins/skill-load.js +97 -61
  1046. package/dist/tools/builtins/web/errors.d.ts +8 -0
  1047. package/dist/tools/builtins/web/errors.js +53 -0
  1048. package/dist/tools/builtins/web/extract.d.ts +60 -0
  1049. package/dist/tools/builtins/web/extract.js +268 -0
  1050. package/dist/tools/builtins/web/fetch.d.ts +55 -0
  1051. package/dist/tools/builtins/web/fetch.js +175 -0
  1052. package/dist/tools/builtins/web/index.d.ts +13 -0
  1053. package/dist/tools/builtins/web/index.js +12 -0
  1054. package/dist/tools/builtins/web/page-cache.d.ts +25 -0
  1055. package/dist/tools/builtins/web/page-cache.js +77 -0
  1056. package/dist/tools/builtins/web/search.d.ts +25 -0
  1057. package/dist/tools/builtins/web/search.js +210 -0
  1058. package/dist/tools/builtins/web/url.d.ts +17 -0
  1059. package/dist/tools/builtins/web/url.js +43 -0
  1060. package/dist/tools/connector-account-policies.d.ts +59 -0
  1061. package/dist/tools/connector-account-policies.js +173 -0
  1062. package/dist/tools/connectors.controller.d.ts +76 -9
  1063. package/dist/tools/connectors.controller.js +138 -17
  1064. package/dist/tools/connectors.schemas.d.ts +27 -0
  1065. package/dist/tools/connectors.schemas.js +34 -0
  1066. package/dist/tools/connectors.service.d.ts +147 -11
  1067. package/dist/tools/connectors.service.js +357 -30
  1068. package/dist/tools/credentials.d.ts +41 -0
  1069. package/dist/tools/credentials.js +150 -1
  1070. package/dist/tools/credentials.module.d.ts +7 -0
  1071. package/dist/tools/credentials.module.js +22 -0
  1072. package/dist/tools/device-op-resolver.d.ts +40 -0
  1073. package/dist/tools/device-op-resolver.js +121 -0
  1074. package/dist/tools/elicitation.d.ts +2 -0
  1075. package/dist/tools/elicitation.js +1 -1
  1076. package/dist/tools/extension-tools.controller.d.ts +14 -0
  1077. package/dist/tools/extension-tools.controller.js +37 -0
  1078. package/dist/tools/fixed-ask.d.ts +7 -0
  1079. package/dist/tools/fixed-ask.js +9 -0
  1080. package/dist/tools/git-asks.d.ts +113 -0
  1081. package/dist/tools/git-asks.js +174 -0
  1082. package/dist/tools/git-push-policy.d.ts +89 -0
  1083. package/dist/tools/git-push-policy.js +175 -0
  1084. package/dist/tools/info.d.ts +45 -4
  1085. package/dist/tools/info.js +67 -4
  1086. package/dist/tools/legacy-keys.js +0 -1
  1087. package/dist/tools/network-asks.d.ts +115 -0
  1088. package/dist/tools/network-asks.js +223 -0
  1089. package/dist/tools/oauth/flow-paths.d.ts +1 -1
  1090. package/dist/tools/oauth/flow-paths.js +1 -1
  1091. package/dist/tools/oauth/flow.js +1 -1
  1092. package/dist/tools/oauth/tokens.d.ts +54 -7
  1093. package/dist/tools/oauth/tokens.js +87 -17
  1094. package/dist/tools/org-tool-switches.d.ts +89 -0
  1095. package/dist/tools/org-tool-switches.js +201 -0
  1096. package/dist/tools/pipeline.d.ts +173 -51
  1097. package/dist/tools/pipeline.js +791 -388
  1098. package/dist/tools/policy.d.ts +46 -13
  1099. package/dist/tools/policy.js +187 -49
  1100. package/dist/tools/questions.service.d.ts +50 -9
  1101. package/dist/tools/questions.service.js +128 -12
  1102. package/dist/tools/registry.d.ts +207 -54
  1103. package/dist/tools/registry.js +312 -212
  1104. package/dist/tools/tools.module.js +51 -5
  1105. package/dist/tools/validate.js +1 -1
  1106. package/dist/ui/admin-ui.controller.d.ts +56 -0
  1107. package/dist/ui/admin-ui.controller.js +192 -0
  1108. package/dist/ui/arrangement.d.ts +65 -0
  1109. package/dist/ui/arrangement.js +171 -0
  1110. package/dist/ui/embed-token.d.ts +2 -2
  1111. package/dist/ui/embed-token.js +30 -5
  1112. package/dist/ui/registry.d.ts +42 -17
  1113. package/dist/ui/registry.js +46 -14
  1114. package/dist/ui/slots.controller.d.ts +11 -0
  1115. package/dist/ui/slots.controller.js +25 -0
  1116. package/dist/ui/ui-stream.d.ts +20 -3
  1117. package/dist/ui/ui-stream.js +25 -7
  1118. package/dist/ui/ui.schemas.d.ts +70 -0
  1119. package/dist/ui/ui.schemas.js +65 -0
  1120. package/dist/ui/views.controller.d.ts +35 -49
  1121. package/dist/ui/views.controller.js +48 -70
  1122. package/dist/ui/views.d.ts +112 -18
  1123. package/dist/ui/views.js +254 -47
  1124. package/package.json +6 -3
  1125. package/src/migrations/0064_account_last_org.sql +7 -0
  1126. package/src/migrations/0065_conversation_archive.sql +8 -0
  1127. package/src/migrations/0066_extension_display_name.sql +4 -0
  1128. package/src/migrations/0067_agent_profiles.sql +20 -0
  1129. package/src/migrations/0068_agent_avatar_uploads.sql +7 -0
  1130. package/src/migrations/0069_attachments_agent_idx.sql +3 -0
  1131. package/src/migrations/0070_background_tasks.sql +37 -0
  1132. package/src/migrations/0071_background_tasks_reserve.sql +6 -0
  1133. package/src/migrations/0072_background_tasks_stopping.sql +5 -0
  1134. package/src/migrations/0073_org_extension_point_defaults.sql +11 -0
  1135. package/src/migrations/0074_subagent_tree.sql +50 -0
  1136. package/src/migrations/0075_plugin_scope.sql +61 -0
  1137. package/src/migrations/0076_skills_created_by_nullable.sql +6 -0
  1138. package/src/migrations/0077_scope_requests.sql +52 -0
  1139. package/src/migrations/0078_scope_grant_switches.sql +18 -0
  1140. package/src/migrations/0079_scope_live_install_switches.sql +19 -0
  1141. package/src/migrations/0080_rename_enterprise_admin.sql +18 -0
  1142. package/src/migrations/0081_platform_components.sql +129 -0
  1143. package/src/migrations/0082_platform_migration.sql +33 -0
  1144. package/src/migrations/0083_plugin_scope_sync_failures.sql +15 -0
  1145. package/src/migrations/0084_org_owned_items.sql +85 -0
  1146. package/src/migrations/0085_agent_templates_phase4.sql +45 -0
  1147. package/src/migrations/0086_org_layering.sql +30 -0
  1148. package/src/migrations/0087_approval_summary.sql +6 -0
  1149. package/src/migrations/0088_environments.sql +80 -0
  1150. package/src/migrations/0089_model_kinds_usage.sql +38 -0
  1151. package/src/migrations/0090_initial_passwords.sql +27 -0
  1152. package/src/migrations/0091_account_time_zone.sql +4 -0
  1153. package/src/migrations/0092_tools_default_on.sql +27 -0
  1154. package/src/migrations/0093_connected_environments.sql +37 -0
  1155. package/src/migrations/0094_environment_paths_idx.sql +7 -0
  1156. package/src/migrations/0095_environment_files_resets.sql +5 -0
  1157. package/src/migrations/0096_environment_demoted_default.sql +18 -0
  1158. package/src/migrations/0097_extension_installs_enabled_at.sql +16 -0
  1159. package/src/migrations/0098_single_provider_running_first.sql +41 -0
  1160. package/src/migrations/0099_channel_connection_guide_ack.sql +6 -0
  1161. package/src/migrations/0100_catalog_files.sql +13 -0
  1162. package/src/migrations/0101_marketplace_icons.sql +32 -0
  1163. package/src/migrations/0102_themes.sql +64 -0
  1164. package/src/migrations/0103_ui_theme_setting.sql +7 -0
  1165. package/src/migrations/0104_connector_tool_account_policies.sql +16 -0
  1166. package/src/migrations/0105_devices_rename.sql +136 -0
  1167. package/src/migrations/0106_setup_guides_v2.sql +10 -0
  1168. package/src/migrations/0107_extension_client_builds.sql +50 -0
  1169. package/src/migrations/0108_question_skipped.sql +6 -0
  1170. package/src/migrations/0109_audit_platform_idx.sql +5 -0
  1171. package/src/migrations/0110_usage_time_idx.sql +8 -0
  1172. package/src/migrations/0111_agent_skill_sharing.sql +22 -0
  1173. package/src/migrations/0112_skill_share_requests.sql +4 -0
  1174. package/src/migrations/0113_scheduled_tasks.sql +108 -0
  1175. package/src/migrations/0114_network_asks.sql +23 -0
  1176. package/src/migrations/0115_network_requests.sql +28 -0
  1177. package/src/migrations/0116_skills_default_on.sql +37 -0
  1178. package/src/migrations/0117_sandbox_tiers.sql +11 -0
  1179. package/src/migrations/0118_device_policy_ops.sql +6 -0
  1180. package/src/migrations/0119_outbox.sql +22 -0
  1181. package/src/migrations/0120_drop_usage_report_sweep.sql +7 -0
  1182. package/src/migrations/0121_drop_platform_migration_runs.sql +12 -0
  1183. package/src/migrations/0122_drop_scheduled_task_runs_rechecked_at.sql +5 -0
  1184. package/src/migrations/0123_pgvector.sql +27 -0
  1185. package/src/migrations/0124_conversation_source.sql +36 -0
  1186. package/src/migrations/0125_conversation_tree_stopped_at.sql +5 -0
  1187. package/src/migrations/0126_git_gateway.sql +10 -0
  1188. package/src/migrations/0127_git_push_policy.sql +12 -0
  1189. package/src/migrations/0128_scheduled_task_plugin_disabled.sql +10 -0
  1190. package/src/migrations/0129_connector_plugin_disabled.sql +5 -0
  1191. package/src/migrations/0130_auto_mode.sql +48 -0
  1192. package/src/migrations/0131_device_mode.sql +9 -0
  1193. package/src/migrations/0132_compact_runs.sql +6 -0
  1194. package/dist/catalog/upgrade-targets.d.ts +0 -56
  1195. package/dist/catalog/upgrade-targets.js +0 -90
  1196. package/dist/conversations/sandbox-status.d.ts +0 -24
  1197. package/dist/conversations/sandbox-status.js +0 -52
  1198. package/dist/extensions/org-config.controller.d.ts +0 -24
  1199. package/dist/extensions/org-config.controller.js +0 -62
  1200. package/dist/tools/builtins/skill-write.d.ts +0 -59
  1201. package/dist/tools/builtins/skill-write.js +0 -244
@@ -0,0 +1,4278 @@
1
+ /** Operators the tokenizer recognizes, longest first (`&>` and `>&` are different). */
2
+ const OPERATORS = [
3
+ ["&&", "&&"],
4
+ ["||", "||"],
5
+ ["&>>", ">>"],
6
+ ["&>", ">"],
7
+ [">>", ">>"],
8
+ ["<<-", "<<"],
9
+ ["<<", "<<"],
10
+ [">&", ">&"],
11
+ ["<&", "<&"],
12
+ [">|", ">"],
13
+ ["|&", "|"],
14
+ [";;", ";"],
15
+ [";", ";"],
16
+ ["|", "|"],
17
+ ["&", "&"],
18
+ [">", ">"],
19
+ ["<", "<"],
20
+ ];
21
+ const WORD_END = new Set([" ", "\t", "\n", "\r", "|", "&", ";", "<", ">", "(", ")"]);
22
+ const isDigit = (c) => c >= "0" && c <= "9";
23
+ const isAlphaNum = (c) => /[A-Za-z0-9]/.test(c);
24
+ /** The public parser: the parsed command, or null when the tokenizer is not confident. */
25
+ export function parseCommand(command) {
26
+ const parsed = parseDetailed(command);
27
+ return parsed.ok ? { commands: parsed.value } : null;
28
+ }
29
+ function parseDetailed(command) {
30
+ const raw = parseRaw(command, 0);
31
+ if (!raw.ok)
32
+ return raw;
33
+ const unwrapped = unwrapOneLevel(raw.value);
34
+ if (!unwrapped.ok)
35
+ return unwrapped;
36
+ const dynamic = rejectDynamicPaths(unwrapped.value);
37
+ if (!dynamic.ok)
38
+ return dynamic;
39
+ const obfuscated = rejectObfuscation(unwrapped.value);
40
+ if (!obfuscated.ok)
41
+ return obfuscated;
42
+ return { ok: true, value: unwrapped.value };
43
+ }
44
+ const MAX_SUB_DEPTH = 3;
45
+ function parseRaw(command, depth) {
46
+ if (depth > MAX_SUB_DEPTH)
47
+ return { ok: false, unsure: "nested command substitution" };
48
+ const tokenized = tokenize(command, depth);
49
+ if (!tokenized.ok)
50
+ return tokenized;
51
+ return buildCommands(tokenized.value);
52
+ }
53
+ function tokenize(text, depth) {
54
+ const tokens = [];
55
+ let unsure = null;
56
+ let i = 0;
57
+ while (i < text.length) {
58
+ const c = text[i];
59
+ if (c === " " || c === "\t") {
60
+ i += 1;
61
+ continue;
62
+ }
63
+ if (c === "\r") {
64
+ i += text[i + 1] === "\n" ? 2 : 1;
65
+ tokens.push({ kind: "op", op: "\n" });
66
+ continue;
67
+ }
68
+ if (c === "\n") {
69
+ tokens.push({ kind: "op", op: "\n" });
70
+ i += 1;
71
+ continue;
72
+ }
73
+ if (c === "#") {
74
+ while (i < text.length && text[i] !== "\n")
75
+ i += 1;
76
+ continue;
77
+ }
78
+ if (isDigit(c)) {
79
+ // `2>`, `2>>`, `2>&1`: drop the fd number, let the redirection be read below.
80
+ let j = i;
81
+ while (j < text.length && isDigit(text[j]))
82
+ j += 1;
83
+ if (text[j] === ">" || text[j] === "<") {
84
+ i = j;
85
+ continue;
86
+ }
87
+ }
88
+ const operator = matchOperator(text, i);
89
+ if (operator !== null) {
90
+ if (operator.text === "<<" || operator.text === "<<-") {
91
+ unsure ??= "heredoc";
92
+ tokens.push({ kind: "op", op: ";" });
93
+ }
94
+ else if ((operator.op === "<" || operator.op === ">") &&
95
+ text[i + operator.text.length] === "(") {
96
+ // `cat <(env)` / `tee >(…)` is a process substitution: its command output feeds this one.
97
+ // The tokenizer does not model it, and reading it as a redirect to a file named `env` would
98
+ // let a credential producer print without review.
99
+ unsure ??= "process substitution";
100
+ tokens.push({ kind: "op", op: ";" });
101
+ }
102
+ else {
103
+ tokens.push({ kind: "op", op: operator.op });
104
+ }
105
+ i += operator.text.length;
106
+ continue;
107
+ }
108
+ const word = readWord(text, i, depth);
109
+ if (!word.ok) {
110
+ unsure ??= word.unsure;
111
+ break;
112
+ }
113
+ if (word.value === null) {
114
+ // A stray `(`, `)`, `{` or `}`: a group boundary, not a word.
115
+ i += 1;
116
+ continue;
117
+ }
118
+ tokens.push(word.value.token);
119
+ i = word.value.end;
120
+ }
121
+ if (unsure !== null)
122
+ return { ok: false, unsure };
123
+ return { ok: true, value: tokens };
124
+ }
125
+ function matchOperator(text, at) {
126
+ for (const [needle, op] of OPERATORS) {
127
+ if (text.startsWith(needle, at))
128
+ return { text: needle, op };
129
+ }
130
+ return null;
131
+ }
132
+ function readWord(text, start, depth) {
133
+ let i = start;
134
+ let value = "";
135
+ let dynamic = false;
136
+ const subs = [];
137
+ let started = false;
138
+ while (i < text.length) {
139
+ const c = text[i];
140
+ if (WORD_END.has(c))
141
+ break;
142
+ started = true;
143
+ if (c === "'") {
144
+ const close = text.indexOf("'", i + 1);
145
+ if (close < 0)
146
+ return { ok: false, unsure: "unterminated single quote" };
147
+ value += text.slice(i + 1, close);
148
+ i = close + 1;
149
+ continue;
150
+ }
151
+ if (c === '"') {
152
+ const quoted = readDoubleQuoted(text, i + 1, depth);
153
+ if (!quoted.ok)
154
+ return quoted;
155
+ value += quoted.value.text;
156
+ dynamic ||= quoted.value.dynamic;
157
+ subs.push(...quoted.value.subs);
158
+ i = quoted.value.end;
159
+ continue;
160
+ }
161
+ if (c === "$" && text[i + 1] === "'") {
162
+ const ansi = readAnsiCQuoted(text, i + 2);
163
+ if (!ansi.ok)
164
+ return ansi;
165
+ value += ansi.value.text;
166
+ i = ansi.value.end;
167
+ continue;
168
+ }
169
+ if (c === "$" && text[i + 1] === "(") {
170
+ const sub = readSubstitution(text, i, depth);
171
+ if (!sub.ok)
172
+ return sub;
173
+ subs.push(sub.value.commands);
174
+ value += `$(${sub.value.source})`;
175
+ dynamic = true;
176
+ i = sub.value.end;
177
+ continue;
178
+ }
179
+ if (c === "`") {
180
+ const sub = readBacktick(text, i, depth);
181
+ if (!sub.ok)
182
+ return sub;
183
+ subs.push(sub.value.commands);
184
+ value += `$(${sub.value.source})`;
185
+ dynamic = true;
186
+ i = sub.value.end;
187
+ continue;
188
+ }
189
+ if (c === "\\") {
190
+ const next = text[i + 1];
191
+ if (next === undefined)
192
+ return { ok: false, unsure: "trailing backslash" };
193
+ if (next === "\n") {
194
+ i += 2;
195
+ continue;
196
+ }
197
+ if (isAlphaNum(next))
198
+ return { ok: false, unsure: "backslash obfuscation" };
199
+ value += next;
200
+ i += 2;
201
+ continue;
202
+ }
203
+ if (c === "$" && (text[i + 1] === "{" || isAlphaNum(text[i + 1] ?? ""))) {
204
+ dynamic = true;
205
+ value += c;
206
+ i += 1;
207
+ continue;
208
+ }
209
+ value += c;
210
+ i += 1;
211
+ }
212
+ if (!started)
213
+ return { ok: true, value: null };
214
+ return { ok: true, value: { token: { kind: "word", value, dynamic, subs }, end: i } };
215
+ }
216
+ function readDoubleQuoted(text, start, depth) {
217
+ let i = start;
218
+ let out = "";
219
+ let dynamic = false;
220
+ const subs = [];
221
+ while (i < text.length) {
222
+ const c = text[i];
223
+ if (c === '"')
224
+ return { ok: true, value: { text: out, dynamic, subs, end: i + 1 } };
225
+ if (c === "\\") {
226
+ const next = text[i + 1];
227
+ if (next === undefined)
228
+ return { ok: false, unsure: "unterminated double quote" };
229
+ if (next === "\n") {
230
+ i += 2;
231
+ continue;
232
+ }
233
+ if (next === "$" || next === "`" || next === '"' || next === "\\") {
234
+ out += next;
235
+ i += 2;
236
+ continue;
237
+ }
238
+ out += `\\${next}`;
239
+ i += 2;
240
+ continue;
241
+ }
242
+ if (c === "$" && text[i + 1] === "(") {
243
+ const sub = readSubstitution(text, i, depth);
244
+ if (!sub.ok)
245
+ return sub;
246
+ subs.push(sub.value.commands);
247
+ out += `$(${sub.value.source})`;
248
+ dynamic = true;
249
+ i = sub.value.end;
250
+ continue;
251
+ }
252
+ if (c === "`") {
253
+ const sub = readBacktick(text, i, depth);
254
+ if (!sub.ok)
255
+ return sub;
256
+ subs.push(sub.value.commands);
257
+ out += `$(${sub.value.source})`;
258
+ dynamic = true;
259
+ i = sub.value.end;
260
+ continue;
261
+ }
262
+ if (c === "$" && (text[i + 1] === "{" || isAlphaNum(text[i + 1] ?? "")))
263
+ dynamic = true;
264
+ out += c;
265
+ i += 1;
266
+ }
267
+ return { ok: false, unsure: "unterminated double quote" };
268
+ }
269
+ function readAnsiCQuoted(text, start) {
270
+ let i = start;
271
+ let out = "";
272
+ while (i < text.length) {
273
+ const c = text[i];
274
+ if (c === "'")
275
+ return { ok: true, value: { text: out, end: i + 1 } };
276
+ if (c === "\\") {
277
+ const next = text[i + 1];
278
+ if (next === undefined)
279
+ return { ok: false, unsure: "unterminated ANSI-C quote" };
280
+ if (next === "n")
281
+ out += "\n";
282
+ else if (next === "t")
283
+ out += "\t";
284
+ else if (next === "r")
285
+ out += "\r";
286
+ else if (next === "\\")
287
+ out += "\\";
288
+ else if (next === "'")
289
+ out += "'";
290
+ else if (next === '"')
291
+ out += '"';
292
+ else
293
+ return { ok: false, unsure: "ANSI-C escape obfuscation" };
294
+ i += 2;
295
+ continue;
296
+ }
297
+ out += c;
298
+ i += 1;
299
+ }
300
+ return { ok: false, unsure: "unterminated ANSI-C quote" };
301
+ }
302
+ function readSubstitution(text, at, depth) {
303
+ // `at` points at `$`, `text[at + 1] === "("`.
304
+ let i = at + 1;
305
+ let nested = 0;
306
+ let quote = null;
307
+ while (i < text.length) {
308
+ const c = text[i];
309
+ if (quote === "'") {
310
+ if (c === "'")
311
+ quote = null;
312
+ i += 1;
313
+ continue;
314
+ }
315
+ if (quote === '"') {
316
+ if (c === "\\") {
317
+ i += 2;
318
+ continue;
319
+ }
320
+ if (c === '"')
321
+ quote = null;
322
+ i += 1;
323
+ continue;
324
+ }
325
+ if (c === "\\") {
326
+ i += 2;
327
+ continue;
328
+ }
329
+ if (c === "'" || c === '"') {
330
+ quote = c;
331
+ i += 1;
332
+ continue;
333
+ }
334
+ if (c === "(") {
335
+ nested += 1;
336
+ i += 1;
337
+ continue;
338
+ }
339
+ if (c === ")") {
340
+ nested -= 1;
341
+ if (nested === 0) {
342
+ const inner = text.slice(at + 2, i);
343
+ const parsed = parseRaw(inner, depth + 1);
344
+ if (!parsed.ok)
345
+ return { ok: false, unsure: `unparseable command substitution: ${parsed.unsure}` };
346
+ return { ok: true, value: { commands: parsed.value, end: i + 1, source: inner } };
347
+ }
348
+ i += 1;
349
+ continue;
350
+ }
351
+ i += 1;
352
+ }
353
+ return { ok: false, unsure: "unterminated command substitution" };
354
+ }
355
+ function readBacktick(text, at, depth) {
356
+ let i = at + 1;
357
+ while (i < text.length) {
358
+ const c = text[i];
359
+ if (c === "\\") {
360
+ i += 2;
361
+ continue;
362
+ }
363
+ if (c === "`") {
364
+ const inner = text.slice(at + 1, i);
365
+ const parsed = parseRaw(inner, depth + 1);
366
+ if (!parsed.ok)
367
+ return { ok: false, unsure: `unparseable command substitution: ${parsed.unsure}` };
368
+ return { ok: true, value: { commands: parsed.value, end: i + 1, source: inner } };
369
+ }
370
+ i += 1;
371
+ }
372
+ return { ok: false, unsure: "unterminated command substitution" };
373
+ }
374
+ function buildCommands(tokens) {
375
+ const commands = [];
376
+ let argv = [];
377
+ let redirects = [];
378
+ let subs = [];
379
+ let pending = null;
380
+ let pendingDup = null;
381
+ let pipeline = 0;
382
+ const flush = () => {
383
+ for (const sub of subs)
384
+ sub.pipeline = pipeline;
385
+ commands.push(...subs);
386
+ if (argv.length > 0 || redirects.length > 0)
387
+ commands.push({ argv, redirects, pipeline });
388
+ argv = [];
389
+ redirects = [];
390
+ subs = [];
391
+ pending = null;
392
+ pendingDup = null;
393
+ };
394
+ for (const token of tokens) {
395
+ if (token.kind === "op") {
396
+ if (token.op === ">" || token.op === ">>" || token.op === "<") {
397
+ pending = { op: token.op };
398
+ continue;
399
+ }
400
+ if (token.op === ">&" || token.op === "<&") {
401
+ pendingDup = token.op;
402
+ continue;
403
+ }
404
+ flush();
405
+ if (token.op !== "|")
406
+ pipeline += 1;
407
+ continue;
408
+ }
409
+ if (pendingDup !== null) {
410
+ // `2>&1` / `>&-` duplicate or close a descriptor; `echo x >& file` is `&> file`, a write.
411
+ pendingDup = null;
412
+ if (/^\d+$/.test(token.value) || token.value === "-")
413
+ continue;
414
+ redirects.push({ op: ">", target: token.value });
415
+ continue;
416
+ }
417
+ if (pending !== null) {
418
+ redirects.push({ op: pending.op, target: token.value });
419
+ pending = null;
420
+ continue;
421
+ }
422
+ argv.push(token.value);
423
+ for (const group of token.subs)
424
+ subs.push(...group);
425
+ }
426
+ if (pending !== null)
427
+ return { ok: false, unsure: "redirection without a target" };
428
+ flush();
429
+ return { ok: true, value: commands };
430
+ }
431
+ /* -------------------------------------------------------------------------------------------- *
432
+ * Wrappers and invocations
433
+ * -------------------------------------------------------------------------------------------- */
434
+ /** Shell reserved words: a command line with one is a control structure the parser does not model. */
435
+ const RESERVED_WORDS = new Set([
436
+ "if",
437
+ "then",
438
+ "else",
439
+ "elif",
440
+ "fi",
441
+ "for",
442
+ "while",
443
+ "until",
444
+ "do",
445
+ "done",
446
+ "case",
447
+ "esac",
448
+ "select",
449
+ "function",
450
+ "{",
451
+ "}",
452
+ "[[",
453
+ "]]",
454
+ "!",
455
+ ]);
456
+ const SHELLS = new Set(["bash", "sh", "zsh", "dash", "ksh", "fish"]);
457
+ const ENV_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
458
+ /** Neutral prefixes that run another command: they are stripped to find the real program. */
459
+ function stripNeutral(argv) {
460
+ let rest = argv.slice();
461
+ for (;;) {
462
+ while (rest.length > 0 && ENV_ASSIGNMENT.test(rest[0]))
463
+ rest = rest.slice(1);
464
+ const head = rest[0];
465
+ if (head === undefined)
466
+ return null;
467
+ const program = baseName(head);
468
+ if (program === "env") {
469
+ const after = skipEnv(rest.slice(1));
470
+ if (after.length === 0)
471
+ return { program: "env", raw: "env", args: [], argv: ["env"] };
472
+ rest = after;
473
+ continue;
474
+ }
475
+ if (["nohup", "command", "builtin", "noglob"].includes(program)) {
476
+ rest = rest.slice(1);
477
+ continue;
478
+ }
479
+ if (program === "nice" || program === "stdbuf") {
480
+ rest = skipLeadingOptions(rest.slice(1), [
481
+ "-n",
482
+ "-t",
483
+ "-s",
484
+ "-o",
485
+ "-e",
486
+ "-d",
487
+ "-q",
488
+ "-i",
489
+ "-p",
490
+ "-c",
491
+ ]);
492
+ continue;
493
+ }
494
+ if (program === "timeout") {
495
+ rest = skipTimeout(rest.slice(1));
496
+ continue;
497
+ }
498
+ if (program === "xargs") {
499
+ rest = skipXargs(rest.slice(1));
500
+ continue;
501
+ }
502
+ // `git -C dir clone ...`: the subcommand follows the global options.
503
+ if (program === "git") {
504
+ const git = gitSubcommandArgs(rest.slice(1));
505
+ return git.dir === undefined
506
+ ? { program, raw: head, args: git.args, argv: rest }
507
+ : { program, raw: head, args: git.args, argv: rest, dir: git.dir };
508
+ }
509
+ return { program, raw: head, args: rest.slice(1), argv: rest };
510
+ }
511
+ }
512
+ /** Git's global options before the subcommand, with their values removed. */
513
+ function gitSubcommandArgs(args) {
514
+ let i = 0;
515
+ let dir;
516
+ const chdir = (value) => {
517
+ dir = dir === undefined || value.startsWith("/") ? value : `${dir}/${value}`;
518
+ };
519
+ while (i < args.length) {
520
+ const a = args[i];
521
+ if (a === "-C") {
522
+ const value = args[i + 1];
523
+ if (value === undefined)
524
+ return { args: [] };
525
+ chdir(value);
526
+ i += 2;
527
+ continue;
528
+ }
529
+ if (a.startsWith("-C") && a.length > 2) {
530
+ chdir(a.slice(2));
531
+ i += 1;
532
+ continue;
533
+ }
534
+ if (["-c", "--git-dir", "--work-tree", "--namespace", "--exec-path", "--config-env"].includes(a)) {
535
+ i += 2;
536
+ continue;
537
+ }
538
+ if (a.startsWith("--git-dir=") ||
539
+ a.startsWith("--work-tree=") ||
540
+ a.startsWith("--namespace=") ||
541
+ a.startsWith("--exec-path=") ||
542
+ a.startsWith("--config-env=") ||
543
+ (a.startsWith("-c") && a.length > 2)) {
544
+ i += 1;
545
+ continue;
546
+ }
547
+ if (a.startsWith("-")) {
548
+ i += 1;
549
+ continue;
550
+ }
551
+ break;
552
+ }
553
+ const rest = args.slice(i);
554
+ return dir === undefined ? { args: rest } : { args: rest, dir };
555
+ }
556
+ function skipEnv(args) {
557
+ let i = 0;
558
+ while (i < args.length) {
559
+ const a = args[i];
560
+ if (ENV_ASSIGNMENT.test(a) ||
561
+ a === "-i" ||
562
+ a === "--ignore-environment" ||
563
+ a === "-0" ||
564
+ a === "--null") {
565
+ i += 1;
566
+ continue;
567
+ }
568
+ if (a === "-u" || a === "--unset") {
569
+ i += 2;
570
+ continue;
571
+ }
572
+ if (a.startsWith("--unset=") || (a.startsWith("-u") && a.length > 2)) {
573
+ i += 1;
574
+ continue;
575
+ }
576
+ if (a === "--")
577
+ i += 1;
578
+ break;
579
+ }
580
+ return args.slice(i);
581
+ }
582
+ function skipLeadingOptions(args, optionsWithValue) {
583
+ let i = 0;
584
+ while (i < args.length) {
585
+ const a = args[i];
586
+ if (a === "--") {
587
+ i += 1;
588
+ break;
589
+ }
590
+ if (!a.startsWith("-"))
591
+ break;
592
+ if (optionsWithValue.includes(a)) {
593
+ i += 2;
594
+ continue;
595
+ }
596
+ i += 1;
597
+ }
598
+ return args.slice(i);
599
+ }
600
+ function skipTimeout(args) {
601
+ const rest = skipLeadingOptions(args, ["-s", "--signal", "-k", "--kill-after"]);
602
+ return rest.length > 0 ? rest.slice(1) : [];
603
+ }
604
+ const XARGS_OPTIONS_WITH_VALUE = new Set([
605
+ "-n",
606
+ "--max-args",
607
+ "-I",
608
+ "--replace",
609
+ "-P",
610
+ "--max-procs",
611
+ "-L",
612
+ "--max-lines",
613
+ "-s",
614
+ "--max-chars",
615
+ "-d",
616
+ "--delimiter",
617
+ "-E",
618
+ "--eof",
619
+ "-a",
620
+ "--arg-file",
621
+ "-e",
622
+ "--eof-str",
623
+ ]);
624
+ function skipXargs(args) {
625
+ let i = 0;
626
+ while (i < args.length) {
627
+ const a = args[i];
628
+ if (a === "--") {
629
+ i += 1;
630
+ break;
631
+ }
632
+ if (!a.startsWith("-"))
633
+ break;
634
+ i += XARGS_OPTIONS_WITH_VALUE.has(a) ? 2 : 1;
635
+ }
636
+ return args.slice(i);
637
+ }
638
+ function baseName(token) {
639
+ const slash = token.lastIndexOf("/");
640
+ return slash < 0 ? token : token.slice(slash + 1);
641
+ }
642
+ /** Every command an invocation runs: the outer program plus the one behind `sudo` / `doas`. */
643
+ function invocationsOf(command) {
644
+ const stripped = stripNeutral(command.argv);
645
+ if (stripped === null)
646
+ return [];
647
+ const out = [stripped];
648
+ if (["sudo", "doas", "pkexec"].includes(stripped.program)) {
649
+ const inner = stripNeutral(skipSudoOptions(stripped.args));
650
+ if (inner !== null)
651
+ out.push(inner);
652
+ }
653
+ return out;
654
+ }
655
+ function skipSudoOptions(args) {
656
+ let i = 0;
657
+ while (i < args.length) {
658
+ const a = args[i];
659
+ if (a === "--") {
660
+ i += 1;
661
+ break;
662
+ }
663
+ if (!a.startsWith("-"))
664
+ break;
665
+ if ([
666
+ "-u",
667
+ "--user",
668
+ "-g",
669
+ "--group",
670
+ "-p",
671
+ "--prompt",
672
+ "-c",
673
+ "--close-from",
674
+ "-r",
675
+ "-t",
676
+ "--other-user",
677
+ "-U",
678
+ "-h",
679
+ ].includes(a)) {
680
+ i += 2;
681
+ continue;
682
+ }
683
+ i += 1;
684
+ }
685
+ return args.slice(i);
686
+ }
687
+ /** The wrapper command's script (`bash -c ...`), or null when the command is not a wrapper. */
688
+ function wrapperScript(inv) {
689
+ if (inv.program === "eval")
690
+ return { kind: "eval", script: inv.args.join(" ") };
691
+ if (inv.program === "su") {
692
+ const c = inv.args.indexOf("-c");
693
+ if (c >= 0 && inv.args[c + 1] !== undefined)
694
+ return { kind: "su", script: inv.args[c + 1] };
695
+ return null;
696
+ }
697
+ if (!SHELLS.has(inv.program))
698
+ return null;
699
+ for (let i = 0; i < inv.args.length; i += 1) {
700
+ const a = inv.args[i];
701
+ if (/^-[A-Za-z]*c[A-Za-z]*$/.test(a) && inv.args[i + 1] !== undefined) {
702
+ return { kind: "shell", script: inv.args[i + 1] };
703
+ }
704
+ if (a === "--command" && inv.args[i + 1] !== undefined) {
705
+ return { kind: "shell", script: inv.args[i + 1] };
706
+ }
707
+ }
708
+ return null;
709
+ }
710
+ function unwrapOneLevel(commands) {
711
+ const out = [];
712
+ let wrapperBlock = 0;
713
+ for (const command of commands) {
714
+ const inv = stripNeutral(command.argv);
715
+ let wrapper = inv === null ? null : wrapperScript(inv);
716
+ if (wrapper === null && inv !== null && ["sudo", "doas", "pkexec"].includes(inv.program)) {
717
+ // `sudo bash -c '...'`: the wrapper is behind the privilege command.
718
+ const inner = stripNeutral(skipSudoOptions(inv.args));
719
+ if (inner !== null)
720
+ wrapper = wrapperScript(inner);
721
+ }
722
+ if (wrapper === null) {
723
+ out.push(command);
724
+ continue;
725
+ }
726
+ if (isDynamicScript(wrapper.script)) {
727
+ return { ok: false, unsure: "a shell wrapper runs a dynamic script" };
728
+ }
729
+ const inner = parseRaw(wrapper.script, 1);
730
+ if (!inner.ok)
731
+ return { ok: false, unsure: `a shell wrapper's script is unparseable: ${inner.unsure}` };
732
+ for (const nested of inner.value) {
733
+ const nestedInv = stripNeutral(nested.argv);
734
+ if (nestedInv !== null && wrapperScript(nestedInv) !== null) {
735
+ return { ok: false, unsure: "nested shell wrapper" };
736
+ }
737
+ }
738
+ // Keep the script's own pipes apart from the rest of the line (a unique block per wrapper),
739
+ // and remember that the wrapper's stdout feeds its outer pipeline.
740
+ const block = wrapperBlock;
741
+ wrapperBlock += 1;
742
+ for (const nested of inner.value) {
743
+ nested.pipeline = block * 1_000_000 + nested.pipeline;
744
+ nested.flow = command.pipeline;
745
+ nested.block = block;
746
+ }
747
+ if (inv !== null && ["su", "sudo", "doas", "pkexec"].includes(inv.program)) {
748
+ // Keep the privilege command itself so the `ask.sudo` rule still sees it.
749
+ out.push({ argv: [inv.program], redirects: [], pipeline: command.pipeline });
750
+ }
751
+ if (inner.value.length === 0) {
752
+ out.push({ ...command, argv: command.argv.slice(0, 1) });
753
+ continue;
754
+ }
755
+ const first = inner.value[0];
756
+ first.redirects = [...first.redirects, ...command.redirects];
757
+ for (const nested of inner.value) {
758
+ out.push(nested.wrapper === undefined ? { ...nested, wrapper: wrapper.kind } : nested);
759
+ }
760
+ }
761
+ return { ok: true, value: out };
762
+ }
763
+ function isDynamicScript(script) {
764
+ return script.includes("$(") || script.includes("`") || /\$\{?[A-Za-z_]/.test(script);
765
+ }
766
+ function homeOf(workspace) {
767
+ const parts = workspace.split("/").filter((p) => p.length > 0);
768
+ if ((parts[0] === "home" || parts[0] === "Users") && parts.length >= 2)
769
+ return `/${parts[0]}/${parts[1]}`;
770
+ if (parts[0] === "root")
771
+ return "/root";
772
+ return null;
773
+ }
774
+ function resolveFacts(facts) {
775
+ const workspace = normalizePath(facts.workspace);
776
+ const cwd = facts.cwd === undefined ? workspace : normalizePath(facts.cwd);
777
+ return { workspace, cwd, home: homeOf(workspace) };
778
+ }
779
+ function normalizePath(path) {
780
+ const absolute = path.startsWith("/");
781
+ const parts = [];
782
+ for (const part of path.split("/")) {
783
+ if (part === "" || part === ".")
784
+ continue;
785
+ if (part === "..") {
786
+ if (parts.length > 0 && parts[parts.length - 1] !== "..")
787
+ parts.pop();
788
+ else if (!absolute)
789
+ parts.push("..");
790
+ continue;
791
+ }
792
+ parts.push(part);
793
+ }
794
+ return `${absolute ? "/" : ""}${parts.join("/")}` || "/";
795
+ }
796
+ const NO_VARS = new Map();
797
+ /** Replace `$X` / `${X}` from the tracked table; null when a variable has no known value. */
798
+ function substituteVariables(value, vars) {
799
+ let unresolved = false;
800
+ const out = value.replace(/\$(?:\{([A-Za-z_][A-Za-z0-9_]*)\}|([A-Za-z_][A-Za-z0-9_]*))/g, (_match, braced, bare) => {
801
+ const name = braced ?? bare ?? "";
802
+ const known = vars.get(name);
803
+ if (known === undefined || known === null) {
804
+ unresolved = true;
805
+ return "";
806
+ }
807
+ return known;
808
+ });
809
+ return unresolved ? null : out;
810
+ }
811
+ /** `~` / `$HOME` / tracked variables expanded and relative paths made absolute; null if unknown. */
812
+ function expandPath(path, facts, cwd = facts.cwd, vars = NO_VARS) {
813
+ let p = path;
814
+ if (p === "~" || p.startsWith("~/")) {
815
+ if (facts.home !== null)
816
+ p = facts.home + p.slice(1);
817
+ }
818
+ else {
819
+ const homeRef = /^\$(?:\{HOME\}|HOME)(?=\/|$)/.exec(p);
820
+ if (homeRef !== null) {
821
+ const rest = p.slice(homeRef[0].length);
822
+ // Without a known home, keep the path in `~` form so the secret/persistence checks match it
823
+ // symbolically (`$HOME/.ssh/...` is the same file as `~/.ssh/...`).
824
+ p = facts.home === null ? `~${rest}` : facts.home + rest;
825
+ }
826
+ }
827
+ const substituted = substituteVariables(p, vars);
828
+ if (substituted === null)
829
+ return null;
830
+ p = substituted;
831
+ // An unresolved variable anywhere in the path (including command substitution) cannot be
832
+ // resolved: never treat `$HOME/$(echo .ssh)/id_rsa` as a harmless absolute path.
833
+ if (/[$`]/.test(p))
834
+ return null;
835
+ if (p.startsWith("/") || p.startsWith("~"))
836
+ return normalizePath(p);
837
+ return normalizePath(`${cwd}/${p}`);
838
+ }
839
+ function inside(child, root) {
840
+ if (child === root)
841
+ return true;
842
+ if (root === "/")
843
+ return child.startsWith("/");
844
+ return child.startsWith(`${root}/`);
845
+ }
846
+ function isAncestor(ancestor, child) {
847
+ if (ancestor === child)
848
+ return false;
849
+ if (ancestor === "/")
850
+ return child.startsWith("/");
851
+ return child.startsWith(`${ancestor}/`);
852
+ }
853
+ function tildeForm(path, facts) {
854
+ if (facts.home !== null && inside(path, facts.home)) {
855
+ return path === facts.home ? "~" : `~${path.slice(facts.home.length)}`;
856
+ }
857
+ return path;
858
+ }
859
+ /** Case-folded spelling for the secret-path judgement. */
860
+ const foldCase = (value) => value.toLowerCase();
861
+ /**
862
+ * `tildeForm` for the secret-path judgement. macOS's default file system is case-insensitive, so
863
+ * `/USERS/me/.SSH` names the same file as `/Users/me/.ssh`; folding the comparison only widens what
864
+ * is judged secret. Workspace containment keeps the exact spelling.
865
+ */
866
+ function tildeFormFolded(path, facts) {
867
+ if (facts.home === null)
868
+ return path;
869
+ const home = foldCase(facts.home);
870
+ const folded = foldCase(path);
871
+ if (folded === home)
872
+ return "~";
873
+ if (folded.startsWith(`${home}/`))
874
+ return `~${path.slice(facts.home.length)}`;
875
+ return path;
876
+ }
877
+ const TMP_ROOTS = ["/tmp", "/private/tmp", "/var/tmp", "/var/folders", "/private/var/folders"];
878
+ const tmpRootOf = (path) => TMP_ROOTS.some((root) => inside(path, root));
879
+ const SECRET_HOME_PATHS = [
880
+ "~/.ssh",
881
+ "~/.aws",
882
+ "~/.config/gcloud",
883
+ "~/.azure",
884
+ "~/.kube/config",
885
+ "~/.docker/config.json",
886
+ "~/.netrc",
887
+ "~/.git-credentials",
888
+ "~/.config/gh/hosts.yml",
889
+ ];
890
+ /**
891
+ * Home directories that hold credential material. A shell pattern that can expand inside one is a
892
+ * secret read even when it does not spell a secret name (`~/.ssh` followed by a wildcard, or
893
+ * `~/.config/gc*`), and `~/.docker` is included because `config.json` is its credential file.
894
+ */
895
+ const SECRET_HOME_DIRS = [
896
+ "~/.ssh",
897
+ "~/.aws",
898
+ "~/.azure",
899
+ "~/.kube",
900
+ "~/.docker",
901
+ "~/.config/gh",
902
+ "~/.config/gcloud",
903
+ ];
904
+ /**
905
+ * A path naming a process's environment (and its glob form): a secret dump. Unanchored on purpose:
906
+ * `/proc/<pid>/root` is a symlink to `/`, so `/proc/self/root/proc/self/environ` names the same
907
+ * file. Any path whose tail is a `/proc/.../environ` component is treated as that file (an ordinary
908
+ * directory named `proc` is unlikely enough to over-ask harmlessly).
909
+ */
910
+ const PROC_ENVIRON = /\/proc\/(?:self|thread-self|[^/]+)(?:\/task\/[^/]+)?\/environ[^/]*$/i;
911
+ /** Shell pathname metacharacters: a segment carrying one is a pattern the shell expands first. */
912
+ const SHELL_PATTERN = /[*?[\]{}]/;
913
+ const hasPattern = (segment) => SHELL_PATTERN.test(segment);
914
+ /** Expand a segment's top-level brace groups into the strings they could become. */
915
+ function braceAlternatives(pattern) {
916
+ const open = pattern.indexOf("{");
917
+ if (open < 0)
918
+ return [pattern];
919
+ const close = pattern.indexOf("}", open + 1);
920
+ if (close < 0)
921
+ return [pattern];
922
+ const body = pattern.slice(open + 1, close);
923
+ if (body.includes(".."))
924
+ return [pattern]; // `{1..9}` is a sequence, not a list
925
+ const parts = body.split(",");
926
+ if (parts.length < 2)
927
+ return [pattern];
928
+ return parts.flatMap((part) => braceAlternatives(`${pattern.slice(0, open)}${part}${pattern.slice(close + 1)}`));
929
+ }
930
+ /** A shell segment pattern as a regex source. A shape it cannot read matches anything. */
931
+ function segmentPatternSource(pattern) {
932
+ let out = "";
933
+ for (let i = 0; i < pattern.length; i += 1) {
934
+ const c = pattern[i];
935
+ if (c === "*") {
936
+ out += ".*";
937
+ continue;
938
+ }
939
+ if (c === "?") {
940
+ out += ".";
941
+ continue;
942
+ }
943
+ if (c === "[") {
944
+ const close = pattern.indexOf("]", i + 1);
945
+ if (close < 0)
946
+ return ".*";
947
+ const body = pattern.slice(i + 1, close).replace(/\\/g, "\\\\");
948
+ out += `[${body.startsWith("!") ? `^${body.slice(1)}` : body}]`;
949
+ i = close;
950
+ continue;
951
+ }
952
+ if (c === "{") {
953
+ const close = pattern.indexOf("}", i + 1);
954
+ if (close < 0)
955
+ return ".*";
956
+ const body = pattern.slice(i + 1, close);
957
+ if (body.includes(".."))
958
+ return ".*";
959
+ out += `(?:${body.split(",").map(segmentPatternSource).join("|")})`;
960
+ i = close;
961
+ continue;
962
+ }
963
+ // A lone closer, or an ordinary character: a pattern shape is ambiguous, so match anything.
964
+ if (c === "}" || c === "]") {
965
+ out += ".*";
966
+ continue;
967
+ }
968
+ out += c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
969
+ }
970
+ return out;
971
+ }
972
+ /**
973
+ * Whether the pattern can start with a literal dot. A pattern that cannot never matches a dotfile
974
+ * (`~/*` excludes `.ssh`); expanding the braces first is what tells `{,.}ssh` apart from `*`.
975
+ */
976
+ function patternFitsDotfile(pattern) {
977
+ if (pattern.startsWith("."))
978
+ return true;
979
+ const alternatives = braceAlternatives(pattern);
980
+ if (alternatives.length > 64)
981
+ return true;
982
+ return alternatives.some((alternative) => alternative.startsWith("."));
983
+ }
984
+ /**
985
+ * Whether the shell pattern segment can expand to `literal`. Conservative: an unreadable shape and
986
+ * an expansion too large to enumerate match, so the caller asks instead of allowing.
987
+ */
988
+ function segmentMatches(pattern, literal) {
989
+ if (!hasPattern(pattern))
990
+ return foldCase(pattern) === foldCase(literal);
991
+ if (literal.startsWith(".") && !patternFitsDotfile(pattern))
992
+ return false;
993
+ try {
994
+ // Folded on purpose: a case-insensitive file system makes `.SSH` and `.ssh` the same directory.
995
+ return new RegExp(`^${segmentPatternSource(pattern)}$`, "iu").test(literal);
996
+ }
997
+ catch {
998
+ return true;
999
+ }
1000
+ }
1001
+ /**
1002
+ * Home roots whose next segment names a user's home directory (`/home/<user>`, `/Users/<user>`).
1003
+ * A shell pattern there (`/home/*`, `/home/{dev,other}`) expands to a real home directory.
1004
+ */
1005
+ const HOME_ROOTS = ["home", "Users"];
1006
+ /**
1007
+ * Whether a home-relative path (the part after `~/`) can name secret material. A segment may itself
1008
+ * carry a shell pattern, which is judged by where it can expand: inside a home secret directory
1009
+ * (`.ssh`, `.aws`, `.config/gh`, …), or a dotfile directly in the home directory (`.netr?`). A
1010
+ * single literal dotfile also counts, so the suffix of a home-directory glob is judged the same way
1011
+ * as the `~/…` path it stands for (`/home/<user>/.netrc` is `~/.netrc`, and so is `/Users/<user>`).
1012
+ */
1013
+ function isSecretHomePath(segments) {
1014
+ for (const dir of SECRET_HOME_DIRS) {
1015
+ const secret = dir.slice(2).split("/");
1016
+ if (segments.length >= secret.length &&
1017
+ secret.every((part, i) => segmentMatches(segments[i], part))) {
1018
+ return true;
1019
+ }
1020
+ }
1021
+ const basename = segments[0];
1022
+ return segments.length === 1 && patternFitsDotfile(basename);
1023
+ }
1024
+ /**
1025
+ * Whether a shell path *pattern* can name a secret. The shell expands a pattern before the command
1026
+ * runs, so the value is unknown; it is judged by where it can expand, never by what it spells:
1027
+ *
1028
+ * - under `/proc/<pid>[/task/<tid>]`, including a pattern in the pid position: every process
1029
+ * directory holds that process's environment (`/proc/self` plus a wildcard, `*environ`, or a
1030
+ * process-id glob with any file name);
1031
+ * - inside a home secret directory (`~/.ssh`, `~/.aws`, `~/.config/gc*`, `~/.kube` — each followed
1032
+ * by a wildcard);
1033
+ * - a dotfile directly in the home directory (`~/.netr?`, `~/.git-credential?`, `.???*`);
1034
+ * - with a pattern in the home-directory position itself (`/home/<user>/.netrc`,
1035
+ * `/Users/<user>/.ssh/id_rsa`), which expands to a real home directory: what follows the pattern
1036
+ * is judged as a `~/…` path;
1037
+ * - with a pattern in the home root segment above it (`/ho<glob>/<user>/.netrc`,
1038
+ * `/{home,Users}/<user>/.ssh`) or in the `/root` home segment itself (`/roo?/.netrc`), which
1039
+ * expands to the same home directory, so the segments behind it are judged as a `~/…` path;
1040
+ * - with a pattern standing in for the `/proc` segment itself (`/pr<glob>c/self/environ`), which
1041
+ * expands to the process environment named behind it.
1042
+ *
1043
+ * Any other pattern (`build/*`, `src/*.ts`, `/tmp/*`, `node_modules/*`, `/home/<user>/README.md`) is
1044
+ * an ordinary glob and stays allowed.
1045
+ */
1046
+ function isSecretPattern(path, facts) {
1047
+ const absolute = path.split("/").filter((segment) => segment.length > 0);
1048
+ // The literal form is matched first (`PROC_ENVIRON`); here a pattern anywhere below `/proc`
1049
+ // counts, since a glob over pids or file names picks a process file the shell only names later.
1050
+ for (let i = 0; i < absolute.length; i += 1) {
1051
+ const segment = absolute[i];
1052
+ if (foldCase(segment) === "proc") {
1053
+ if (absolute.length > i + 2 && absolute.slice(i + 1).some(hasPattern))
1054
+ return true;
1055
+ continue;
1056
+ }
1057
+ // A pattern standing in for the root segment itself (`/pr*c/self/environ`): only the root can
1058
+ // be the real `/proc`, and it is a process environment only when the tail names `environ`.
1059
+ if (i !== 0 || !hasPattern(segment) || !segmentMatches(segment, "proc"))
1060
+ continue;
1061
+ const tail = absolute.slice(i + 1);
1062
+ const last = tail[tail.length - 1];
1063
+ if (tail.length >= 2 && last !== undefined && segmentMatches(last, "environ"))
1064
+ return true;
1065
+ }
1066
+ const t = tildeFormFolded(path, facts);
1067
+ if (t.startsWith("~/"))
1068
+ return isSecretHomePath(t.slice(2).split("/"));
1069
+ // `/home/<pattern>/…` never becomes `~/…`: the user segment is the pattern, so judge only the
1070
+ // literal suffix behind it.
1071
+ const [root, user, ...suffix] = absolute;
1072
+ if (root !== undefined &&
1073
+ HOME_ROOTS.some((literal) => segmentMatches(root, literal)) &&
1074
+ user !== undefined &&
1075
+ hasPattern(user) &&
1076
+ suffix.length > 0) {
1077
+ return isSecretHomePath(suffix);
1078
+ }
1079
+ // A pattern in the home root segment itself (`/ho*/dev`, `/Use*/me`, `/{home,Users}`) expands to
1080
+ // a real home root the same way; the user position and the literal suffix behind it are a `~/…`
1081
+ // path, so `/ho*/dev/.netrc` is `~/.netrc` and `/Use*/me/.ssh` is `~/.ssh`.
1082
+ if (root !== undefined && hasPattern(root)) {
1083
+ // A root pattern can expand to several home roots at once (`/*` matches `home`, `Users` and
1084
+ // `root`), so each interpretation is judged: a false result in one must not hide a hit in the
1085
+ // other. `/ho*/…` and `/*/…` are a hit when the user position and suffix are a secret path.
1086
+ const homeHit = HOME_ROOTS.some((literal) => segmentMatches(root, literal)) &&
1087
+ user !== undefined &&
1088
+ suffix.length > 0 &&
1089
+ isSecretHomePath(suffix);
1090
+ // `/root` is a home directory with no user segment under it: the remainder is the path itself,
1091
+ // so `/roo?/.netrc` is `~/.netrc` and `/ro?t/.ssh` is `~/.ssh`. It is the agent's home only when
1092
+ // the workspace is under `/root`; from another home, `/root/…` is another user's and stays
1093
+ // allowed (as the literal `/root/.netrc` does), so it must not turn a `/Users/me` workspace's
1094
+ // `/*/.ssh/id_rsa` into a hit.
1095
+ const rootHit = (facts.home === null || facts.home === "/root") &&
1096
+ user !== undefined &&
1097
+ segmentMatches(root, "root") &&
1098
+ isSecretHomePath([user, ...suffix]);
1099
+ if (homeHit || rootHit)
1100
+ return true;
1101
+ }
1102
+ return false;
1103
+ }
1104
+ function isSecretPath(path, facts) {
1105
+ if (PROC_ENVIRON.test(path))
1106
+ return true;
1107
+ // A glob or brace is what the shell expands before the command runs: the path is a secret when
1108
+ // it can expand to one, so `cat ~/.netr?` is the same read as `cat ~/.netrc`.
1109
+ if (hasPattern(path) && isSecretPattern(path, facts))
1110
+ return true;
1111
+ const t = foldCase(tildeFormFolded(path, facts));
1112
+ if (SECRET_HOME_PATHS.some((secret) => t === secret || t.startsWith(`${secret}/`)))
1113
+ return true;
1114
+ const base = baseName(path).toLowerCase();
1115
+ return base.startsWith(".env") && !inside(path, facts.workspace);
1116
+ }
1117
+ function isPersistencePath(path, facts) {
1118
+ const t = tildeForm(path, facts);
1119
+ if (t === "~/.gitconfig" || t.startsWith("~/.gitconfig."))
1120
+ return true;
1121
+ if (/^~\/\.(bashrc|bash_profile|bash_login|profile|zshrc|zshenv|zprofile|zlogin|tcshrc|cshrc|kshrc)$/.test(t)) {
1122
+ return true;
1123
+ }
1124
+ if (t === "~/.config/fish/config.fish" || t.startsWith("~/.config/fish/conf.d/"))
1125
+ return true;
1126
+ if (t === "~/.ssh" || t.startsWith("~/.ssh/"))
1127
+ return true;
1128
+ if (t.startsWith("~/Library/LaunchAgents") || t.startsWith("~/Library/LaunchDaemons"))
1129
+ return true;
1130
+ if (t.startsWith("/Library/LaunchAgents") || t.startsWith("/Library/LaunchDaemons"))
1131
+ return true;
1132
+ if ([
1133
+ "/etc/profile",
1134
+ "/etc/bashrc",
1135
+ "/etc/bash.bashrc",
1136
+ "/etc/zshrc",
1137
+ "/etc/zshenv",
1138
+ "/etc/paths",
1139
+ ].includes(t) ||
1140
+ t.startsWith("/etc/profile.d/")) {
1141
+ return true;
1142
+ }
1143
+ if (t === "/etc/crontab" || t.startsWith("/var/spool/cron") || t.startsWith("/etc/cron."))
1144
+ return true;
1145
+ if (t.startsWith("~/.config/systemd"))
1146
+ return true;
1147
+ return isGitHook(path);
1148
+ }
1149
+ const DEVICE_DIRS = [
1150
+ "~/.config/capybara-device",
1151
+ "~/.capybara-device",
1152
+ "~/.local/share/capybara-device",
1153
+ "~/Library/Application Support/capybara-device",
1154
+ "/etc/capybara-device",
1155
+ "/var/lib/capybara-device",
1156
+ ];
1157
+ function isDevicePath(path, facts) {
1158
+ const t = tildeForm(path, facts);
1159
+ if (DEVICE_DIRS.some((dir) => t === dir || t.startsWith(`${dir}/`)))
1160
+ return true;
1161
+ // The device binary, its unit file and its launch agent anywhere on disk.
1162
+ return isDeviceIdentifier(baseName(t));
1163
+ }
1164
+ const isGitHook = (path) => path.includes("/.git/hooks/") || path.endsWith("/.git/hooks");
1165
+ function looksLikePath(token) {
1166
+ if (token.length === 0 || token === "-" || token.startsWith("-"))
1167
+ return false;
1168
+ return (token.startsWith("/") ||
1169
+ token.startsWith("~") ||
1170
+ token.startsWith(".") ||
1171
+ token.startsWith("$HOME") ||
1172
+ token.includes("/"));
1173
+ }
1174
+ /** A whole-word variable reference (`$KUBECONFIG`, `${AWS_CONFIG_FILE}`): a dynamic path value. */
1175
+ const BARE_VARIABLE = /^\$(?:\{[A-Za-z_][A-Za-z0-9_]*\}|[A-Za-z_][A-Za-z0-9_]*)$/;
1176
+ /** Curl strips one matching surrounding `"…"` pair from an `@file` / `<file` / upload value. */
1177
+ function stripQuotePair(value) {
1178
+ return value.length >= 2 && value.startsWith('"') && value.endsWith('"')
1179
+ ? value.slice(1, -1)
1180
+ : value;
1181
+ }
1182
+ /** A path argument as written: plain, `@file` (curl), `file=@path`, or `--flag=value`. */
1183
+ function pathCandidates(token) {
1184
+ const out = [];
1185
+ const add = (value) => {
1186
+ const fromAt = value.startsWith("@") || value.includes("@");
1187
+ let candidate = value.startsWith("@") ? value.slice(1) : value;
1188
+ // `-F file=@path`, `-d@path`: the file is after the first `@`.
1189
+ const at = candidate.indexOf("@");
1190
+ if (at >= 0)
1191
+ candidate = candidate.slice(at + 1);
1192
+ // A nested pair of quotes around the value (`@"/home/dev/.ssh/id_rsa"`) is not part of the
1193
+ // path: curl strips it, and a quoted path is what a reviewer must treat as the file it names.
1194
+ candidate = stripQuotePair(candidate);
1195
+ // An `@file` is a file even without a slash (`@$FILE` names one just as `@./file` does).
1196
+ if ((fromAt || looksLikePath(candidate)) &&
1197
+ candidate.length > 0 &&
1198
+ candidate !== "-" &&
1199
+ !out.includes(candidate)) {
1200
+ out.push(candidate);
1201
+ }
1202
+ };
1203
+ if (token.startsWith("@") && token.length > 1)
1204
+ add(token.slice(1));
1205
+ // Attached short-option values that name files: `-d@file`, `-Tfile`, `-Ffile=@path`, `-ofile`,
1206
+ // `awk -fprog`, `sed -fscript`.
1207
+ const attached = /^-(?:d|f|T|F|o|D|O)(.+)$/.exec(token);
1208
+ if (attached !== null && attached[1] !== undefined)
1209
+ add(attached[1]);
1210
+ const eq = token.indexOf("=");
1211
+ if (eq > 0)
1212
+ add(token.slice(eq + 1));
1213
+ const literal = stripQuotePair(token);
1214
+ if (looksLikePath(literal))
1215
+ out.push(literal);
1216
+ // A whole-word variable names whatever path it holds (`cat $KUBECONFIG`): dynamic, not a literal
1217
+ // word. It is carried as a candidate so the rules can treat an unresolved value as uncertain.
1218
+ if (BARE_VARIABLE.test(token) && !out.includes(token))
1219
+ out.push(token);
1220
+ return out;
1221
+ }
1222
+ /** Secret-looking path components: a dynamic word containing one is never a confident read. */
1223
+ const SECRET_LOOKING = /(^|\/)(\.ssh|\.aws|\.config\/gcloud|\.azure|\.kube|\.docker|\.netrc|\.git-credentials|\.env)/;
1224
+ /**
1225
+ * Whether a word is a concrete file path worth resolving statically for a read. Ordinary shell
1226
+ * patterns (`grep "src/.*$" src/`), command substitutions (`cat $(pwd)/package.json`) and URLs are
1227
+ * not: their unresolved text must not turn routine reads into `unsure`. A `$HOME`-leading word or
1228
+ * one that names a secret component still is.
1229
+ */
1230
+ function isConcretePathWord(candidate) {
1231
+ // A URL is not a local file path; `rg "http://$host/api"` stays a read.
1232
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(candidate) || /^https?:/i.test(candidate))
1233
+ return false;
1234
+ if (candidate.startsWith("/") || candidate.startsWith("~"))
1235
+ return true;
1236
+ if (candidate.startsWith("./") || candidate.startsWith("../"))
1237
+ return true;
1238
+ if (/^\$(?:\{HOME\}|HOME)(?=\/|$)/.test(candidate))
1239
+ return true;
1240
+ // A whole-word variable is a path word whose value is dynamic (`cat $KUBECONFIG`); when it cannot
1241
+ // be resolved the read is uncertain, never a confident allow.
1242
+ if (BARE_VARIABLE.test(candidate))
1243
+ return true;
1244
+ if (SECRET_LOOKING.test(candidate))
1245
+ return true;
1246
+ // A path-like word with a simple variable is concrete: if the variable is unresolved, the read
1247
+ // falls to `unsure` rather than `allow` (`cat $X/config`).
1248
+ if (/\$\{?[A-Za-z_][A-Za-z0-9_]*\}?/.test(candidate) &&
1249
+ (candidate.includes("/") || candidate.startsWith("."))) {
1250
+ return true;
1251
+ }
1252
+ if (/[*?[\]{}|&;<>()$`\\'"\s]/.test(candidate))
1253
+ return false;
1254
+ return candidate.includes("/") || /\.[A-Za-z0-9]{1,8}$/.test(candidate);
1255
+ }
1256
+ function cwdOf(command, facts) {
1257
+ return command.cwd ?? facts.cwd;
1258
+ }
1259
+ function secretTokens(command, facts) {
1260
+ const found = new Set();
1261
+ for (const token of command.argv) {
1262
+ for (const candidate of pathCandidates(token)) {
1263
+ const resolved = expandPath(candidate, facts, cwdOf(command, facts), command.vars);
1264
+ if (resolved !== null && isSecretPath(resolved, facts))
1265
+ found.add(resolved);
1266
+ }
1267
+ }
1268
+ for (const redirect of command.redirects) {
1269
+ const resolved = expandPath(redirect.target, facts, cwdOf(command, facts), command.vars);
1270
+ if (resolved !== null && isSecretPath(resolved, facts))
1271
+ found.add(resolved);
1272
+ }
1273
+ return [...found];
1274
+ }
1275
+ function persistenceTokens(command, facts) {
1276
+ const found = new Set();
1277
+ for (const token of command.argv) {
1278
+ for (const candidate of pathCandidates(token)) {
1279
+ const resolved = expandPath(candidate, facts, cwdOf(command, facts), command.vars);
1280
+ if (resolved !== null && isPersistencePath(resolved, facts))
1281
+ found.add(resolved);
1282
+ }
1283
+ }
1284
+ for (const redirect of command.redirects) {
1285
+ const resolved = expandPath(redirect.target, facts, cwdOf(command, facts), command.vars);
1286
+ if (resolved !== null && isPersistencePath(resolved, facts))
1287
+ found.add(resolved);
1288
+ }
1289
+ return [...found];
1290
+ }
1291
+ function deviceTokens(command, facts) {
1292
+ const found = new Set();
1293
+ for (const token of command.argv) {
1294
+ for (const candidate of pathCandidates(token)) {
1295
+ const resolved = expandPath(candidate, facts, cwdOf(command, facts), command.vars);
1296
+ if (resolved !== null && isDevicePath(resolved, facts))
1297
+ found.add(resolved);
1298
+ }
1299
+ }
1300
+ for (const redirect of command.redirects) {
1301
+ const resolved = expandPath(redirect.target, facts, cwdOf(command, facts), command.vars);
1302
+ if (resolved !== null && isDevicePath(resolved, facts))
1303
+ found.add(resolved);
1304
+ }
1305
+ return [...found];
1306
+ }
1307
+ /* -------------------------------------------------------------------------------------------- *
1308
+ * Command classification helpers
1309
+ * -------------------------------------------------------------------------------------------- */
1310
+ const READ_PROGRAMS = new Set([
1311
+ "cat",
1312
+ "head",
1313
+ "tail",
1314
+ "less",
1315
+ "more",
1316
+ "bat",
1317
+ "grep",
1318
+ "egrep",
1319
+ "fgrep",
1320
+ "rg",
1321
+ "ag",
1322
+ "ack",
1323
+ "sed",
1324
+ "awk",
1325
+ "gawk",
1326
+ "ls",
1327
+ "ll",
1328
+ "tree",
1329
+ "find",
1330
+ "stat",
1331
+ "file",
1332
+ "wc",
1333
+ "sort",
1334
+ "uniq",
1335
+ "cut",
1336
+ "tr",
1337
+ "diff",
1338
+ "cmp",
1339
+ "md5",
1340
+ "md5sum",
1341
+ "shasum",
1342
+ "sha256sum",
1343
+ "strings",
1344
+ "od",
1345
+ "xxd",
1346
+ "hexdump",
1347
+ "jq",
1348
+ "yq",
1349
+ "column",
1350
+ "nl",
1351
+ "pwd",
1352
+ "printenv",
1353
+ "which",
1354
+ "whereis",
1355
+ "type",
1356
+ "date",
1357
+ "echo",
1358
+ "printf",
1359
+ "basename",
1360
+ "dirname",
1361
+ "realpath",
1362
+ "readlink",
1363
+ "du",
1364
+ "df",
1365
+ "id",
1366
+ "whoami",
1367
+ "uname",
1368
+ "hostname",
1369
+ "env",
1370
+ "ps",
1371
+ "lsof",
1372
+ "netstat",
1373
+ "ss",
1374
+ "free",
1375
+ "uptime",
1376
+ "top",
1377
+ "htop",
1378
+ "kill",
1379
+ "pgrep",
1380
+ "pidof",
1381
+ "launchctl",
1382
+ "crontab",
1383
+ ]);
1384
+ const DEV_PROGRAMS = new Set([
1385
+ "node",
1386
+ "nodejs",
1387
+ "deno",
1388
+ "bun",
1389
+ "npm",
1390
+ "npx",
1391
+ "pnpm",
1392
+ "yarn",
1393
+ "pip",
1394
+ "pip3",
1395
+ "uv",
1396
+ "poetry",
1397
+ "pipenv",
1398
+ "cargo",
1399
+ "rustc",
1400
+ "rustup",
1401
+ "go",
1402
+ "gofmt",
1403
+ "make",
1404
+ "cmake",
1405
+ "ninja",
1406
+ "bazel",
1407
+ "gradle",
1408
+ "gradlew",
1409
+ "mvn",
1410
+ "mvnw",
1411
+ "tsc",
1412
+ "vitest",
1413
+ "jest",
1414
+ "mocha",
1415
+ "biome",
1416
+ "eslint",
1417
+ "prettier",
1418
+ "ruff",
1419
+ "black",
1420
+ "isort",
1421
+ "mypy",
1422
+ "pyright",
1423
+ "pylint",
1424
+ "pytest",
1425
+ "tox",
1426
+ "nox",
1427
+ "python",
1428
+ "python3",
1429
+ "ruby",
1430
+ "bundle",
1431
+ "rake",
1432
+ "composer",
1433
+ "php",
1434
+ "dotnet",
1435
+ "swift",
1436
+ "swiftc",
1437
+ "clang",
1438
+ "clang++",
1439
+ "gcc",
1440
+ "g++",
1441
+ "cc",
1442
+ "javac",
1443
+ "java",
1444
+ "kotlin",
1445
+ "kotlinc",
1446
+ "scala",
1447
+ "sbt",
1448
+ "mix",
1449
+ "elixir",
1450
+ "erl",
1451
+ "Rscript",
1452
+ "julia",
1453
+ "perl",
1454
+ "lua",
1455
+ "base64",
1456
+ "basenc",
1457
+ "source",
1458
+ ".",
1459
+ "next",
1460
+ "vite",
1461
+ "nuxt",
1462
+ "ng",
1463
+ "flask",
1464
+ "rails",
1465
+ "cd",
1466
+ "pushd",
1467
+ "popd",
1468
+ "export",
1469
+ "set",
1470
+ "unset",
1471
+ "alias",
1472
+ "unalias",
1473
+ "umask",
1474
+ "wait",
1475
+ "true",
1476
+ "false",
1477
+ "test",
1478
+ "[",
1479
+ ":",
1480
+ "sleep",
1481
+ "seq",
1482
+ ]);
1483
+ const SHELL_PROGRAMS = new Set(["bash", "sh", "zsh", "dash", "ksh", "fish"]);
1484
+ const SAFE_SERVE_PROGRAMS = new Set([
1485
+ "open",
1486
+ "xdg-open",
1487
+ "sensible-browser",
1488
+ "google-chrome",
1489
+ "chromium",
1490
+ "chromium-browser",
1491
+ "firefox",
1492
+ "safari",
1493
+ "http-server",
1494
+ "serve",
1495
+ "nodemon",
1496
+ "uvicorn",
1497
+ "gunicorn",
1498
+ "hypercorn",
1499
+ "daphne",
1500
+ ]);
1501
+ const NETWORK_PROGRAMS = new Set([
1502
+ "curl",
1503
+ "wget",
1504
+ "nc",
1505
+ "netcat",
1506
+ "ncat",
1507
+ "ssh",
1508
+ "scp",
1509
+ "sftp",
1510
+ "rsync",
1511
+ "ftp",
1512
+ "lftp",
1513
+ "telnet",
1514
+ "socat",
1515
+ "openssl",
1516
+ "dig",
1517
+ "nslookup",
1518
+ "host",
1519
+ "ping",
1520
+ "ping6",
1521
+ "traceroute",
1522
+ "tracepath",
1523
+ ]);
1524
+ const INSTALLERS = new Set([
1525
+ "brew",
1526
+ "port",
1527
+ "snap",
1528
+ "flatpak",
1529
+ "apt",
1530
+ "apt-get",
1531
+ "dnf",
1532
+ "yum",
1533
+ "zypper",
1534
+ "apk",
1535
+ "pacman",
1536
+ ]);
1537
+ const KEYCHAIN_DUMP = /^(find-.*password|dump-keychain)$/;
1538
+ const LAUNCHCTL_CHANGES = [
1539
+ "load",
1540
+ "unload",
1541
+ "submit",
1542
+ "bootstrap",
1543
+ "bootout",
1544
+ "enable",
1545
+ "disable",
1546
+ "kickstart",
1547
+ "start",
1548
+ "stop",
1549
+ "remove",
1550
+ ];
1551
+ function hasArg(args, ...values) {
1552
+ return args.some((a) => values.includes(a) || values.some((v) => a.startsWith(`${v}=`)));
1553
+ }
1554
+ function hasInPlace(args) {
1555
+ return args.some((a) => a === "--in-place" ||
1556
+ a.startsWith("--in-place=") ||
1557
+ a === "-i" ||
1558
+ (a.startsWith("-i") && !a.startsWith("--")));
1559
+ }
1560
+ function nonOptionArgs(args) {
1561
+ const out = [];
1562
+ for (let i = 0; i < args.length; i += 1) {
1563
+ const a = args[i];
1564
+ if (a === "--") {
1565
+ out.push(...args.slice(i + 1));
1566
+ break;
1567
+ }
1568
+ if (!a.startsWith("-"))
1569
+ out.push(a);
1570
+ }
1571
+ return out;
1572
+ }
1573
+ /** Options of cp/mv/install/ln/git-clone that consume the next word as their value. */
1574
+ const VALUE_OPTIONS = new Set([
1575
+ "-t",
1576
+ "--target-directory",
1577
+ "-S",
1578
+ "--suffix",
1579
+ "-m",
1580
+ "--mode",
1581
+ "-o",
1582
+ "--owner",
1583
+ "-g",
1584
+ "--group",
1585
+ "--depth",
1586
+ "--branch",
1587
+ "-b",
1588
+ "--origin",
1589
+ "--upload-pack",
1590
+ "-u",
1591
+ "--reference",
1592
+ "--separate-git-dir",
1593
+ "--config",
1594
+ "-c",
1595
+ "--filter",
1596
+ "--jobs",
1597
+ "--template",
1598
+ "--shallow-since",
1599
+ "--shallow-exclude",
1600
+ "--revision",
1601
+ ]);
1602
+ /**
1603
+ * The positional operands of an argument list, skipping options and the values of
1604
+ * {@link VALUE_OPTIONS} (`cp -t /etc/profile.d evil.sh` has one operand, `evil.sh`).
1605
+ */
1606
+ function operands(args, valueOptions = VALUE_OPTIONS) {
1607
+ const out = [];
1608
+ let afterDashDash = false;
1609
+ for (let i = 0; i < args.length; i += 1) {
1610
+ const a = args[i];
1611
+ if (afterDashDash) {
1612
+ out.push(a);
1613
+ continue;
1614
+ }
1615
+ if (a === "--") {
1616
+ afterDashDash = true;
1617
+ continue;
1618
+ }
1619
+ if (a.startsWith("--")) {
1620
+ // `--target-directory=/etc` carries its value inline; `--depth 1` does not.
1621
+ if (!a.includes("=") && valueOptions.has(a))
1622
+ i += 1;
1623
+ continue;
1624
+ }
1625
+ if (a.startsWith("-") && a.length > 1) {
1626
+ // GNU clustering: in `-rt DIR` / `-Dt DIR` the value-taking letter is last and the next word
1627
+ // is its value; `-tDIR` carries the value inline.
1628
+ const letters = a.slice(1);
1629
+ const index = [...letters].findIndex((letter) => valueOptions.has(`-${letter}`));
1630
+ if (index >= 0 && index === letters.length - 1)
1631
+ i += 1;
1632
+ continue;
1633
+ }
1634
+ out.push(a);
1635
+ }
1636
+ return out;
1637
+ }
1638
+ /** `cp -t DIR` / `cp -rt DIR` / `cp --target-directory=DIR`: a copy-like command's destination. */
1639
+ function targetDirectory(args) {
1640
+ for (let i = 0; i < args.length; i += 1) {
1641
+ const a = args[i];
1642
+ if (a === "--target-directory" && args[i + 1] !== undefined)
1643
+ return args[i + 1];
1644
+ if (a.startsWith("--target-directory="))
1645
+ return a.slice("--target-directory=".length);
1646
+ // `-t DIR`, `-rt DIR`, `-Dt DIR` (GNU clustering: the value-taking `t` is last).
1647
+ if (/^-[A-Za-z]*t$/.test(a) && args[i + 1] !== undefined)
1648
+ return args[i + 1];
1649
+ // `-tDIR`, `-rtDIR`: the value is attached.
1650
+ const attached = /^-[A-Za-z]*t(.+)$/.exec(a);
1651
+ if (attached !== null)
1652
+ return attached[1];
1653
+ }
1654
+ return null;
1655
+ }
1656
+ function venvBinary(raw) {
1657
+ return /(^|\/)\.?venv\/bin\//.test(raw) || /(^|\/)env\/bin\//.test(raw);
1658
+ }
1659
+ function globalInstallOf(program, args, venv) {
1660
+ if (program === "npm" || program === "pnpm" || program === "yarn" || program === "bun") {
1661
+ const global = args.includes("-g") || args.includes("--global") || args.includes("global");
1662
+ if (!global)
1663
+ return false;
1664
+ const sub = args.find((a) => !a.startsWith("-"));
1665
+ if (program === "yarn" && sub === "global" && args.includes("add"))
1666
+ return true;
1667
+ return ["install", "i", "add", "upgrade", "update", "link"].includes(sub ?? "");
1668
+ }
1669
+ if (program === "pip" || program === "pip3") {
1670
+ if (!["install", "uninstall", "remove"].includes(args[0] ?? ""))
1671
+ return false;
1672
+ return !venv;
1673
+ }
1674
+ if (program === "python" || program === "python3") {
1675
+ const m = args.indexOf("-m");
1676
+ const module = m >= 0 ? args[m + 1] : undefined;
1677
+ if (module !== "pip" && module !== "pip3")
1678
+ return false;
1679
+ return !venv;
1680
+ }
1681
+ if (program === "uv")
1682
+ return args.includes("pip") && args.includes("install") && args.includes("--system");
1683
+ if (program === "cargo")
1684
+ return args[0] === "install";
1685
+ if (program === "go")
1686
+ return args[0] === "install";
1687
+ if (INSTALLERS.has(program)) {
1688
+ const sub = args.find((a) => !a.startsWith("-"));
1689
+ return ["install", "reinstall", "upgrade", "update", "remove", "uninstall", "add"].includes(sub ?? "");
1690
+ }
1691
+ return false;
1692
+ }
1693
+ function commandLineActivatesVenv(commands) {
1694
+ return commands.some((command) => command.argv.some((token) => /(^|\/)\.?venv\/bin\/activate$/.test(token) || /\/bin\/activate$/.test(token)));
1695
+ }
1696
+ function gitPushKind(args) {
1697
+ if (hasArg(args, "--delete", "--prune") || args.includes("-d"))
1698
+ return "remote-destroy";
1699
+ let forced = false;
1700
+ const positional = [];
1701
+ for (const a of args) {
1702
+ if (a === "--force" ||
1703
+ a === "-f" ||
1704
+ a === "--force-with-lease" ||
1705
+ a.startsWith("--force-with-lease=") ||
1706
+ a === "--force-if-includes" ||
1707
+ a === "--mirror" ||
1708
+ a === "--all") {
1709
+ forced = true;
1710
+ continue;
1711
+ }
1712
+ if (a.startsWith("-"))
1713
+ continue;
1714
+ positional.push(a);
1715
+ }
1716
+ const refs = positional.slice(1);
1717
+ for (const ref of refs) {
1718
+ if (ref.startsWith(":"))
1719
+ return "remote-destroy";
1720
+ if (ref.startsWith("+"))
1721
+ forced = true;
1722
+ }
1723
+ for (const ref of refs) {
1724
+ const destination = ref.includes(":") ? ref.slice(ref.indexOf(":") + 1) : ref;
1725
+ const branch = destination
1726
+ .replace(/^\+/, "")
1727
+ .replace(/^refs\/heads\//, "")
1728
+ .replace(/^refs\/remotes\/[^/]+\//, "");
1729
+ if (branch === "main" || branch === "master" || branch.startsWith("release/"))
1730
+ return "protected";
1731
+ }
1732
+ return forced ? "force" : null;
1733
+ }
1734
+ function ghIsSafe(args) {
1735
+ const cleaned = args.filter((a) => !a.startsWith("-"));
1736
+ const sub = cleaned[0];
1737
+ const action = cleaned[1];
1738
+ if (sub === undefined || sub === "api" || sub === "search")
1739
+ return true;
1740
+ if (sub === "auth")
1741
+ return ["login", "logout", "status", "switch", "setup-git", "token"].includes(action ?? "");
1742
+ if (sub === "pr") {
1743
+ return [
1744
+ "create",
1745
+ "edit",
1746
+ "comment",
1747
+ "view",
1748
+ "list",
1749
+ "diff",
1750
+ "checks",
1751
+ "status",
1752
+ "ready",
1753
+ "review",
1754
+ ].includes(action ?? "");
1755
+ }
1756
+ if (sub === "issue")
1757
+ return ["view", "list", "status"].includes(action ?? "");
1758
+ if (sub === "repo")
1759
+ return ["view", "list", "clone"].includes(action ?? "");
1760
+ if (sub === "release")
1761
+ return ["view", "list", "download"].includes(action ?? "");
1762
+ if (sub === "run")
1763
+ return ["view", "list", "watch", "download"].includes(action ?? "");
1764
+ if (sub === "workflow")
1765
+ return ["view", "list", "run"].includes(action ?? "");
1766
+ if (sub === "secret" || sub === "variable")
1767
+ return action === "list";
1768
+ if (sub === "config")
1769
+ return ["get", "list"].includes(action ?? "");
1770
+ if (["label", "gist", "extension", "cache", "ruleset", "org", "project"].includes(sub)) {
1771
+ return ["list", "view"].includes(action ?? "");
1772
+ }
1773
+ return false;
1774
+ }
1775
+ function hostValue(args, flag) {
1776
+ for (let i = 0; i < args.length; i += 1) {
1777
+ const a = args[i];
1778
+ if (a === flag)
1779
+ return args[i + 1] ?? null;
1780
+ if (a.startsWith(`${flag}=`))
1781
+ return a.slice(flag.length + 1);
1782
+ }
1783
+ return null;
1784
+ }
1785
+ /** docker/pflag short options that take a value (used to walk a short-option cluster). */
1786
+ const DOCKER_SHORT_VALUE_LETTERS = new Set([
1787
+ "a",
1788
+ "c",
1789
+ "e",
1790
+ "h",
1791
+ "l",
1792
+ "m",
1793
+ "p",
1794
+ "u",
1795
+ "v",
1796
+ "w",
1797
+ ]);
1798
+ /**
1799
+ * The value `-X` carries in a docker short-option cluster: the rest of the cluster (`-vHOST`,
1800
+ * `-itvHOST`, `-v=HOST`) or `""` when the value is the next word (`-v HOST`). `undefined` when the
1801
+ * cluster has no `-X` (its first value-taking letter belongs to another option, e.g. `-eFOO=v`).
1802
+ */
1803
+ function dockerShortValue(arg, letter) {
1804
+ const chars = arg.slice(1);
1805
+ for (let i = 0; i < chars.length; i += 1) {
1806
+ const c = chars[i];
1807
+ if (!DOCKER_SHORT_VALUE_LETTERS.has(c))
1808
+ continue;
1809
+ if (c !== letter)
1810
+ return undefined;
1811
+ return chars.slice(i + 1).replace(/^=/, "");
1812
+ }
1813
+ return undefined;
1814
+ }
1815
+ function volumeHostPaths(args) {
1816
+ const out = [];
1817
+ for (let i = 0; i < args.length; i += 1) {
1818
+ const a = args[i];
1819
+ let spec = null;
1820
+ let isMount = false;
1821
+ if (a === "-v" || a === "--volume")
1822
+ spec = args[i + 1] ?? null;
1823
+ else if (a.startsWith("--volume="))
1824
+ spec = a.slice("--volume=".length);
1825
+ else if (a === "--mount") {
1826
+ spec = args[i + 1] ?? null;
1827
+ isMount = true;
1828
+ }
1829
+ else if (a.startsWith("--mount=")) {
1830
+ spec = a.slice("--mount=".length);
1831
+ isMount = true;
1832
+ }
1833
+ else if (a.startsWith("-") && !a.startsWith("--")) {
1834
+ // `-vHOST`, `-v=HOST` and clusters (`-itvHOST:CTR`); an empty value means the next word.
1835
+ const short = dockerShortValue(a, "v");
1836
+ if (short !== undefined)
1837
+ spec = short.length > 0 ? short : (args[i + 1] ?? null);
1838
+ }
1839
+ if (spec === null)
1840
+ continue;
1841
+ if (isMount) {
1842
+ const fields = spec.split(",");
1843
+ const type = fields.find((f) => f.startsWith("type="))?.slice(5);
1844
+ if (type !== undefined && type !== "bind")
1845
+ continue;
1846
+ const source = fields.find((f) => f.startsWith("source=") || f.startsWith("src="));
1847
+ if (source !== undefined)
1848
+ out.push(source.slice(source.indexOf("=") + 1));
1849
+ continue;
1850
+ }
1851
+ const host = spec.split(":")[0] ?? "";
1852
+ // A named volume (`mydata:/data`) has no host path. Absolute, `~`-, `.`- and `$`-rooted
1853
+ // sources are host paths; a variable is resolved by the caller (`$HOME/.ssh`, `${HOME}/.ssh`).
1854
+ out.push(host.startsWith("/") || host.startsWith("~") || host.startsWith(".") || host.startsWith("$")
1855
+ ? host
1856
+ : null);
1857
+ }
1858
+ return out;
1859
+ }
1860
+ function dockerPrivileged(args, facts, cwd, vars = NO_VARS) {
1861
+ if (args.some((a) => a === "--privileged" || a.startsWith("--privileged="))) {
1862
+ return "runs a container with --privileged";
1863
+ }
1864
+ if (["--pid", "--ipc", "--userns"].some((flag) => hostValue(args, flag) === "host")) {
1865
+ return "runs a container in the host's namespace (--pid/--ipc/--userns host)";
1866
+ }
1867
+ if (hostValue(args, "--net") === "host" || hostValue(args, "--network") === "host") {
1868
+ return "runs a container on the host's network (--net host)";
1869
+ }
1870
+ if (hasArg(args, "--device"))
1871
+ return "passes a host device into a container (--device)";
1872
+ for (const volume of volumeHostPaths(args)) {
1873
+ if (volume === null)
1874
+ continue;
1875
+ // `$(pwd)` is the run's own directory (a run-time substitution, but a known one); `$PWD` is
1876
+ // already in the variable table. Everything else must resolve statically.
1877
+ const ownDirectory = /^\$\(\s*pwd\s*\)(?=\/|$)/;
1878
+ const normalized = ownDirectory.test(volume) ? volume.replace(ownDirectory, cwd) : volume;
1879
+ const resolved = expandPath(normalized, facts, cwd, vars);
1880
+ // A host path that cannot be resolved (`-v $SECRET_DIR:/data`) may point outside the
1881
+ // workspace, so it is never treated as a harmless mount.
1882
+ if (resolved === null) {
1883
+ return `mounts a host path that cannot be resolved (${volume})`;
1884
+ }
1885
+ if (!inside(resolved, facts.workspace)) {
1886
+ return `mounts a host path outside the workspace (${volume})`;
1887
+ }
1888
+ }
1889
+ return null;
1890
+ }
1891
+ function isSecurityToolDisable(inv) {
1892
+ const { program, args } = inv;
1893
+ if (program === "pfctl" && hasArg(args, "-d", "-F", "-f", "-e"))
1894
+ return true;
1895
+ if (program === "spctl" && hasArg(args, "--master-disable", "--global-disable"))
1896
+ return true;
1897
+ if (program === "csrutil" && args.includes("disable"))
1898
+ return true;
1899
+ if ((program === "iptables" || program === "ip6tables") &&
1900
+ hasArg(args, "-F", "--flush", "-X", "--delete-chain")) {
1901
+ return true;
1902
+ }
1903
+ if (program === "nft" && args.join(" ").includes("flush ruleset"))
1904
+ return true;
1905
+ if (program === "ufw" && args.includes("disable"))
1906
+ return true;
1907
+ if (program === "setenforce" && args.includes("0"))
1908
+ return true;
1909
+ return false;
1910
+ }
1911
+ /** The device's own program, unit and launch-agent names. */
1912
+ const DEVICE_IDENTIFIER = /(^|\/)(capybara-device(\.service|\.plist)?|top\.capybara\.device(\.service|\.plist)?)$/;
1913
+ const isDeviceIdentifier = (value) => DEVICE_IDENTIFIER.test(value);
1914
+ /** Service subcommands that change state; status/show/list/is-active are reads. */
1915
+ const SERVICE_EDIT_ACTIONS = new Set([
1916
+ "stop",
1917
+ "start",
1918
+ "restart",
1919
+ "reload",
1920
+ "daemon-reload",
1921
+ "enable",
1922
+ "disable",
1923
+ "mask",
1924
+ "unmask",
1925
+ "kill",
1926
+ "load",
1927
+ "unload",
1928
+ "bootstrap",
1929
+ "bootout",
1930
+ "submit",
1931
+ "kickstart",
1932
+ // `systemctl edit UNIT` writes a drop-in; `launchctl remove LABEL` (deprecated) unloads it.
1933
+ "edit",
1934
+ "remove",
1935
+ ]);
1936
+ function isDeviceServiceCommand(inv) {
1937
+ if (!["launchctl", "systemctl", "service", "pkill", "killall", "kill"].includes(inv.program)) {
1938
+ return false;
1939
+ }
1940
+ // Match the device's own program or launch agent, not a substring of an unrelated name.
1941
+ if (!inv.args.some((arg) => isDeviceIdentifier(arg)))
1942
+ return false;
1943
+ if (["pkill", "killall", "kill"].includes(inv.program))
1944
+ return true;
1945
+ const positional = inv.args.filter((arg) => !arg.startsWith("-"));
1946
+ // `service NAME ACTION`: the action is the second operand.
1947
+ const action = inv.program === "service" ? positional[1] : positional[0];
1948
+ return action !== undefined && SERVICE_EDIT_ACTIONS.has(action);
1949
+ }
1950
+ /* -------------------------------------------------------------------------------------------- *
1951
+ * Write targets
1952
+ * -------------------------------------------------------------------------------------------- */
1953
+ const WRITE_PROGRAMS = new Set([
1954
+ "rm",
1955
+ "rmdir",
1956
+ "unlink",
1957
+ "shred",
1958
+ "mkdir",
1959
+ "touch",
1960
+ "truncate",
1961
+ "tee",
1962
+ "chmod",
1963
+ "chown",
1964
+ "chgrp",
1965
+ "mv",
1966
+ "cp",
1967
+ "install",
1968
+ "ln",
1969
+ "dd",
1970
+ "sed",
1971
+ "perl",
1972
+ ]);
1973
+ /** The programs some rule understands; an unknown program is never certified by a redirect alone. */
1974
+ const KNOWN_PROGRAMS = new Set([
1975
+ ...READ_PROGRAMS,
1976
+ ...DEV_PROGRAMS,
1977
+ ...SHELL_PROGRAMS,
1978
+ ...NETWORK_PROGRAMS,
1979
+ ...SAFE_SERVE_PROGRAMS,
1980
+ ...WRITE_PROGRAMS,
1981
+ "git",
1982
+ "gh",
1983
+ "docker",
1984
+ "kill",
1985
+ "pkill",
1986
+ "killall",
1987
+ "launchctl",
1988
+ "crontab",
1989
+ "security",
1990
+ "find",
1991
+ "tar",
1992
+ "unzip",
1993
+ "zip",
1994
+ "gzip",
1995
+ "gunzip",
1996
+ "vim",
1997
+ "nano",
1998
+ ]);
1999
+ function isKnownCommand(command) {
2000
+ return eachInvocation([command], (inv) => KNOWN_PROGRAMS.has(inv.program)).length > 0;
2001
+ }
2002
+ /** tar short options that consume an argument: the rest of the cluster, or the next word. */
2003
+ const TAR_VALUE_LETTERS = new Set([
2004
+ "b",
2005
+ "C",
2006
+ "f",
2007
+ "F",
2008
+ "g",
2009
+ "H",
2010
+ "I",
2011
+ "K",
2012
+ "L",
2013
+ "N",
2014
+ "T",
2015
+ "V",
2016
+ "X",
2017
+ ]);
2018
+ function writeTargetsOf(inv) {
2019
+ const { program, args } = inv;
2020
+ const positional = nonOptionArgs(args);
2021
+ switch (program) {
2022
+ case "rm":
2023
+ case "rmdir":
2024
+ case "unlink":
2025
+ case "shred":
2026
+ case "mkdir":
2027
+ case "touch":
2028
+ case "truncate":
2029
+ case "tee":
2030
+ return positional;
2031
+ case "chmod":
2032
+ case "chown":
2033
+ case "chgrp":
2034
+ return positional.slice(1);
2035
+ case "mv": {
2036
+ // The sources are deleted as well as the destination being written.
2037
+ const destination = targetDirectory(args);
2038
+ const sources = operands(args);
2039
+ return destination === null ? sources : [...sources, destination];
2040
+ }
2041
+ case "cp":
2042
+ case "install":
2043
+ case "ln": {
2044
+ const destination = targetDirectory(args);
2045
+ if (destination !== null)
2046
+ return [destination];
2047
+ const ops = operands(args);
2048
+ return ops.length > 0 ? [ops[ops.length - 1]] : [];
2049
+ }
2050
+ case "dd":
2051
+ return args.filter((a) => a.startsWith("of=")).map((a) => a.slice(3));
2052
+ case "sed":
2053
+ case "perl":
2054
+ return hasInPlace(args) ? positional.slice(1) : [];
2055
+ case "curl": {
2056
+ const out = [];
2057
+ for (let i = 0; i < args.length; i += 1) {
2058
+ const a = args[i];
2059
+ if (["-o", "--output", "-D", "--dump-header", "--output-dir"].includes(a) &&
2060
+ args[i + 1] !== undefined) {
2061
+ if (args[i + 1] !== "-")
2062
+ out.push(args[i + 1]);
2063
+ }
2064
+ else if (a.startsWith("--output=") ||
2065
+ a.startsWith("--dump-header=") ||
2066
+ a.startsWith("--output-dir=")) {
2067
+ out.push(a.slice(a.indexOf("=") + 1));
2068
+ }
2069
+ }
2070
+ return out;
2071
+ }
2072
+ case "wget": {
2073
+ const out = [];
2074
+ for (let i = 0; i < args.length; i += 1) {
2075
+ const a = args[i];
2076
+ if (["-O", "--output-document", "-P", "--directory-prefix"].includes(a) &&
2077
+ args[i + 1] !== undefined) {
2078
+ if (args[i + 1] !== "-")
2079
+ out.push(args[i + 1]);
2080
+ }
2081
+ else if (a.startsWith("--output-document=") || a.startsWith("--directory-prefix=")) {
2082
+ out.push(a.slice(a.indexOf("=") + 1));
2083
+ }
2084
+ }
2085
+ return out;
2086
+ }
2087
+ case "scp":
2088
+ case "sftp":
2089
+ case "rsync": {
2090
+ const last = positional[positional.length - 1];
2091
+ // `host:path` and `rsync://...` are remote: the command writes there, not here.
2092
+ return last !== undefined && !isRemoteSpec(last) ? [last] : [];
2093
+ }
2094
+ case "tar": {
2095
+ // `-C DIR` extracts into DIR; `-cf ARCHIVE` writes ARCHIVE. `-C` on create only selects
2096
+ // the files read, so it is not a write target there.
2097
+ const targets = [];
2098
+ let directory = null;
2099
+ let extract = false;
2100
+ let create = false;
2101
+ for (let i = 0; i < args.length; i += 1) {
2102
+ const a = args[i];
2103
+ if (a === "-C" || a === "--directory") {
2104
+ directory = args[i + 1] ?? null;
2105
+ i += 1;
2106
+ continue;
2107
+ }
2108
+ if (a.startsWith("--")) {
2109
+ // GNU/bsdtar accept any unambiguous prefix of `--directory` (`--dir`, `--direc`,
2110
+ // `--d`); it takes the next word or an `=`-attached value.
2111
+ const eq = a.indexOf("=");
2112
+ const name = a.slice(2, eq < 0 ? undefined : eq);
2113
+ if (name.length > 0 && "directory".startsWith(name)) {
2114
+ if (eq >= 0) {
2115
+ directory = a.slice(eq + 1);
2116
+ }
2117
+ else {
2118
+ directory = args[i + 1] ?? null;
2119
+ i += 1;
2120
+ }
2121
+ continue;
2122
+ }
2123
+ }
2124
+ if (a === "--extract" || a === "--get")
2125
+ extract = true;
2126
+ if (a === "--create")
2127
+ create = true;
2128
+ if (a === "-f" || a === "--file") {
2129
+ const value = args[i + 1];
2130
+ if (value !== undefined && create)
2131
+ targets.push(value);
2132
+ i += 1;
2133
+ continue;
2134
+ }
2135
+ if (a.startsWith("--file=")) {
2136
+ if (create)
2137
+ targets.push(a.slice("--file=".length));
2138
+ continue;
2139
+ }
2140
+ if (a.startsWith("-") && !a.startsWith("--")) {
2141
+ // Short options cluster (`-xvf`, `-xC/etc`, `-xvC DIR`). A letter that takes an argument
2142
+ // consumes the rest of the cluster, or the next word when it is last (`-xC/etc` and
2143
+ // `-xC /etc` both put `/etc` in DIR). Later letters are then part of that value, which is
2144
+ // why `-xvfC DIR` makes the archive `C` and does not extract into `DIR`.
2145
+ const cluster = a.slice(1);
2146
+ for (let j = 0; j < cluster.length; j += 1) {
2147
+ const letter = cluster[j];
2148
+ if (letter === "x") {
2149
+ extract = true;
2150
+ continue;
2151
+ }
2152
+ if (letter === "c") {
2153
+ create = true;
2154
+ continue;
2155
+ }
2156
+ if (!TAR_VALUE_LETTERS.has(letter))
2157
+ continue;
2158
+ const attachedValue = cluster.slice(j + 1);
2159
+ let value;
2160
+ if (attachedValue.length > 0) {
2161
+ value = attachedValue;
2162
+ }
2163
+ else {
2164
+ value = args[i + 1];
2165
+ if (value !== undefined)
2166
+ i += 1;
2167
+ }
2168
+ if (letter === "C")
2169
+ directory = value ?? null;
2170
+ if (letter === "f" && create && value !== undefined)
2171
+ targets.push(value);
2172
+ break;
2173
+ }
2174
+ }
2175
+ }
2176
+ if (extract)
2177
+ targets.push(directory ?? ".");
2178
+ return targets;
2179
+ }
2180
+ case "unzip": {
2181
+ // `-d/etc` is attached, `-d /etc` is separate; both name the extraction directory.
2182
+ const dirs = [];
2183
+ for (let i = 0; i < args.length; i += 1) {
2184
+ const a = args[i];
2185
+ if (a === "-d") {
2186
+ if (args[i + 1] !== undefined)
2187
+ dirs.push(args[i + 1]);
2188
+ i += 1;
2189
+ continue;
2190
+ }
2191
+ if (/^-[A-Za-z]*d$/.test(a) && args[i + 1] !== undefined) {
2192
+ dirs.push(args[i + 1]);
2193
+ i += 1;
2194
+ continue;
2195
+ }
2196
+ const attached = /^-[A-Za-z]*d(.+)$/.exec(a);
2197
+ if (attached !== null && attached[1] !== undefined)
2198
+ dirs.push(attached[1]);
2199
+ }
2200
+ return dirs.length > 0 ? dirs : ["."];
2201
+ }
2202
+ case "git": {
2203
+ if (args[0] !== "clone")
2204
+ return [];
2205
+ // `git clone [opts] <url> [dest]`: option values are skipped, so `--depth 1` does not shift
2206
+ // the destination into the URL's place.
2207
+ const pos = operands(args.slice(1));
2208
+ // `git -C DIR clone <url>` writes `<DIR>/<repo>` when no destination is given; a relative
2209
+ // destination is relative to `-C`, not to the run's cwd.
2210
+ const destination = (dest) => {
2211
+ if (dest.startsWith("/") || dest.startsWith("~") || inv.dir === undefined)
2212
+ return dest;
2213
+ return `${inv.dir.replace(/\/$/, "")}/${dest}`;
2214
+ };
2215
+ if (pos.length >= 2)
2216
+ return [destination(pos[1])];
2217
+ if (inv.dir === undefined)
2218
+ return []; // the default destination is inside cwd (the workspace)
2219
+ const repo = pos[0];
2220
+ const name = repo === undefined ? undefined : repoName(repo);
2221
+ return name === undefined ? [] : [destination(name)];
2222
+ }
2223
+ default:
2224
+ return [];
2225
+ }
2226
+ }
2227
+ function isIgnoredDevice(path) {
2228
+ return (path === "-" ||
2229
+ ["/dev/null", "/dev/stdout", "/dev/stderr", "/dev/tty", "/dev/zero", "/dev/random"].includes(path));
2230
+ }
2231
+ /** `host:path`, `user@host:path` or a URL: scp/sftp/rsync write to the remote, not locally. */
2232
+ function isRemoteSpec(path) {
2233
+ if (path.startsWith("/") ||
2234
+ path.startsWith("./") ||
2235
+ path.startsWith("../") ||
2236
+ path.startsWith("~"))
2237
+ return false;
2238
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(path))
2239
+ return true;
2240
+ return /^[^/]*:/.test(path);
2241
+ }
2242
+ /** The directory `git clone <url>` creates by default (`https://host/x/y.git` -> `y`). */
2243
+ function repoName(url) {
2244
+ const cleaned = url.replace(/\/+$/, "");
2245
+ const tail = cleaned.slice(cleaned.lastIndexOf("/") + 1);
2246
+ const name = tail.slice(tail.lastIndexOf(":") + 1).replace(/\.git$/, "");
2247
+ return name.length > 0 ? name : undefined;
2248
+ }
2249
+ function writeTargets(command) {
2250
+ const out = [];
2251
+ for (const redirect of command.redirects) {
2252
+ if (redirect.op !== "<" && !isIgnoredDevice(redirect.target))
2253
+ out.push(redirect.target);
2254
+ }
2255
+ for (const inv of invocationsOf(command)) {
2256
+ for (const target of writeTargetsOf(inv)) {
2257
+ if (!isIgnoredDevice(target))
2258
+ out.push(target);
2259
+ }
2260
+ }
2261
+ return out;
2262
+ }
2263
+ /** Every command an invocation of which satisfies `visit`; used to mark commands as covered. */
2264
+ function eachInvocation(commands, visit) {
2265
+ const matched = [];
2266
+ for (const command of commands) {
2267
+ for (const inv of invocationsOf(command)) {
2268
+ if (visit(inv, command)) {
2269
+ matched.push(command);
2270
+ break;
2271
+ }
2272
+ }
2273
+ }
2274
+ return matched;
2275
+ }
2276
+ function isSecretOwner(program, paths, facts) {
2277
+ for (const path of paths) {
2278
+ const t = tildeForm(path, facts);
2279
+ // Only ssh's own use of ~/.ssh is its credential; scp / sftp / rsync treat those files as
2280
+ // ordinary sources (a local copy is printed-then-copied and asks; a remote one is sent).
2281
+ if ((t === "~/.ssh" || t.startsWith("~/.ssh/")) &&
2282
+ ["git", "ssh", "ssh-add"].includes(program)) {
2283
+ return true;
2284
+ }
2285
+ if ((t === "~/.gitconfig" || t === "~/.gitconfig." || t === "~/.git-credentials") &&
2286
+ program === "git")
2287
+ return true;
2288
+ if (t === "~/.config/gh/hosts.yml" && program === "gh")
2289
+ return true;
2290
+ if (t.startsWith("~/.aws") && program === "aws")
2291
+ return true;
2292
+ if (t.startsWith("~/.config/gcloud") && program === "gcloud")
2293
+ return true;
2294
+ if (t.startsWith("~/.azure") && program === "az")
2295
+ return true;
2296
+ if (t === "~/.kube/config" && program === "kubectl")
2297
+ return true;
2298
+ if (t === "~/.docker/config.json" && program === "docker")
2299
+ return true;
2300
+ }
2301
+ return false;
2302
+ }
2303
+ /** A command that reads a secret path rather than using it as its own tool's credential. */
2304
+ /** A path word that cannot be resolved but names (or is rooted at) a secret location. */
2305
+ function isUnresolvedSecretWord(candidate, command, ctx) {
2306
+ if (expandPath(candidate, ctx.facts, cwdOf(command, ctx.facts), command.vars) !== null) {
2307
+ return false;
2308
+ }
2309
+ // A bare variable can hold the path of a credential or config file (`$KUBECONFIG`,
2310
+ // `$AWS_CONFIG_FILE`, `$DOTENV`); its unresolved value is treated as possibly secret. A variable
2311
+ // the line itself assigned (even with an unresolved value) is a shell-local, not evidence of a
2312
+ // credential file, so it stays `unsure` rather than asking.
2313
+ const bare = BARE_VARIABLE.exec(candidate);
2314
+ if (bare !== null) {
2315
+ const name = candidate.replace(/^\$\{?/, "").replace(/\}$/, "");
2316
+ return command.vars === undefined || !command.vars.has(name);
2317
+ }
2318
+ // `/proc/$PID/environ` names the same environment dump as the literal path.
2319
+ if (foldCase(candidate).startsWith("/proc/") && /\/environ[^/]*$/i.test(candidate))
2320
+ return true;
2321
+ return SECRET_LOOKING.test(candidate) || /^(?:\$HOME|\$\{HOME\})(?:\/|$)/.test(candidate);
2322
+ }
2323
+ function commandHasUnresolvedSecret(command, ctx) {
2324
+ // A write target is not read or printed; its own rule judges it (`ask.outside-write`). A value in
2325
+ // a known non-path position (`git commit -m $MSG`, a grep pattern) is text, not a file.
2326
+ const written = new Set(writeTargets(command));
2327
+ const nonPath = nonPathTokens(command);
2328
+ const tokens = [...command.argv, ...command.redirects.map((redirect) => redirect.target)];
2329
+ return tokens.some((token) => {
2330
+ if (written.has(token) || consumeNonPath(nonPath, token))
2331
+ return false;
2332
+ return pathCandidates(token).some((candidate) => isUnresolvedSecretWord(candidate, command, ctx));
2333
+ });
2334
+ }
2335
+ /** Whether a short-option argument (`-pe`, `-x`) carries the letter `letter`. */
2336
+ function shortOptionHas(args, letter) {
2337
+ return args.some((a) => /^-[A-Za-z]+$/.test(a) && a.includes(letter));
2338
+ }
2339
+ /** Options of `ps` that take a value, so the next word is not a BSD-style letter cluster. */
2340
+ const PS_VALUE_OPTIONS = new Set([
2341
+ "-C",
2342
+ "-G",
2343
+ "-g",
2344
+ "-p",
2345
+ "-q",
2346
+ "-s",
2347
+ "-t",
2348
+ "-U",
2349
+ "-u",
2350
+ "-O",
2351
+ "-o",
2352
+ "--Group",
2353
+ "--User",
2354
+ "--user",
2355
+ "--group",
2356
+ "--pid",
2357
+ "--ppid",
2358
+ "--quick-pid",
2359
+ "--session",
2360
+ "--sid",
2361
+ "--tty",
2362
+ "--format",
2363
+ "--sort",
2364
+ "--cols",
2365
+ "--columns",
2366
+ "--width",
2367
+ "--lines",
2368
+ "--rows",
2369
+ ]);
2370
+ /**
2371
+ * Whether a `ps` invocation prints each process's environment.
2372
+ *
2373
+ * macOS: a bare BSD-style cluster containing `e` (`ps eww`, `ps auxe`, `ps axe`) or `E`
2374
+ * (`ps axE`) displays it, and a dashed `-E` does too (`ps -E`).
2375
+ *
2376
+ * Linux procps: any `x` selector switches to the BSD reading, where a dashed or bare `e` means the
2377
+ * environment (`ps -xe`, `ps -ex`, `ps -xef`, `ps -auxe`, `ps -xe -u user`), and `e` may sit in a
2378
+ * different argument from `x` (`ps -e -x`, `ps -x -e`). Without `x`, UNIX-syntax `-e` (`ps -e`,
2379
+ * `ps -ef`, `ps -eww`) only selects every process and stays a plain listing. This over-asks
2380
+ * `ps -e -x` on macOS (where `-e` is `-A`), which one prompt absorbs; never a false denial.
2381
+ */
2382
+ function psShowsEnvironment(args) {
2383
+ let hasX = false; // an `x` selector in a dashed or a bare BSD-style cluster
2384
+ let dashedE = false; // a lower-case `e` in a dashed cluster (`-e`, `-xe`, `-ex`)
2385
+ let dashedUpperE = false; // an upper-case `E` in a dashed cluster (`ps -E`, `ps -AE`)
2386
+ let bareE = false; // `e` or `E` in a bare BSD-style cluster (`eww`, `axE`, `auxe`)
2387
+ for (let i = 0; i < args.length; i += 1) {
2388
+ const a = args[i];
2389
+ if (a === "--")
2390
+ break;
2391
+ if (PS_VALUE_OPTIONS.has(a)) {
2392
+ i += 1; // the option's value is not a BSD-style letter cluster (`ps -u user -C editor`)
2393
+ continue;
2394
+ }
2395
+ if (a.startsWith("--"))
2396
+ continue;
2397
+ if (a.startsWith("-")) {
2398
+ const letters = a.slice(1);
2399
+ if (letters.includes("x"))
2400
+ hasX = true;
2401
+ if (letters.includes("e"))
2402
+ dashedE = true;
2403
+ if (letters.includes("E"))
2404
+ dashedUpperE = true;
2405
+ continue;
2406
+ }
2407
+ if (a.includes("x"))
2408
+ hasX = true;
2409
+ if (a.includes("e") || a.includes("E"))
2410
+ bareE = true;
2411
+ }
2412
+ // A bare cluster prints the environment by itself; a dashed `E` always does; a dashed `e` only
2413
+ // on the Linux BSD path, whose marker is an `x` selector anywhere in the same invocation.
2414
+ return bareE || dashedUpperE || (dashedE && hasX);
2415
+ }
2416
+ /** `gh auth status` with `-t` / `--show-token` prints the login token, like `gh auth token`. */
2417
+ function ghStatusShowsToken(args) {
2418
+ const statusAt = args.indexOf("status");
2419
+ if (statusAt < 0)
2420
+ return false;
2421
+ return args
2422
+ .slice(statusAt + 1)
2423
+ .some((a) => a === "--show-token" ||
2424
+ a.startsWith("--show-token=") ||
2425
+ (/^-[A-Za-z]+(?:=.*)?$/.test(a) && (a.split("=")[0] ?? "").includes("t")));
2426
+ }
2427
+ /**
2428
+ * Whether an invocation prints credentials to its stdout: the environment (`env`, `set`,
2429
+ * `export -p`, `declare -x`, `printenv`, every `ps` environment display, `launchctl getenv`) or a
2430
+ * stored credential (`gh auth token`, `gh auth status --show-token`, `git credential fill`). Plain
2431
+ * shell reads allow these programs, yet their output is the owner's exported cloud keys and tokens
2432
+ * (auto mode) or a stored login. Printing one asks; piping it into a network command is a secret
2433
+ * exfiltration (see `readsSecret`).
2434
+ *
2435
+ * Only the printing forms count: `env VAR=1 cmd` never reaches here (the parser unwraps it),
2436
+ * `export VAR=1` / `set -e` / `declare -x VAR=1` / `ps aux` / `ps -ef` / `launchctl list` print
2437
+ * nothing sensitive, and `printenv` in an argument position of another program is that program's
2438
+ * argument, not a producer.
2439
+ */
2440
+ function credentialProducer(inv) {
2441
+ const { program, args } = inv;
2442
+ if (program === "env")
2443
+ return args.length === 0;
2444
+ if (program === "printenv")
2445
+ return true;
2446
+ if (program === "set")
2447
+ return args.length === 0;
2448
+ if (program === "export")
2449
+ return args.length === 0 || shortOptionHas(args, "p");
2450
+ if (program === "declare" || program === "typeset") {
2451
+ // `-p` prints the named values; a bare `declare` / `declare -x` lists every variable. `-f` /
2452
+ // `-F` list functions, which hold no values, and `declare -x VAR=1` only sets one.
2453
+ if (shortOptionHas(args, "p"))
2454
+ return true;
2455
+ if (shortOptionHas(args, "f") || shortOptionHas(args, "F"))
2456
+ return false;
2457
+ return nonOptionArgs(args).length === 0;
2458
+ }
2459
+ if (program === "ps")
2460
+ return psShowsEnvironment(args);
2461
+ if (program === "launchctl")
2462
+ return nonOptionArgs(args)[0] === "getenv";
2463
+ if (program === "gh") {
2464
+ const words = args.filter((a) => !a.startsWith("-"));
2465
+ if (words[0] !== "auth")
2466
+ return false;
2467
+ if (words[1] === "token")
2468
+ return true;
2469
+ return words[1] === "status" && ghStatusShowsToken(args);
2470
+ }
2471
+ if (program === "git") {
2472
+ // `git credential fill` and every `git credential-<helper> get` print the stored password.
2473
+ return ((args[0] === "credential" || (args[0] ?? "").startsWith("credential-")) &&
2474
+ ["fill", "get"].includes(args[1] ?? ""));
2475
+ }
2476
+ return false;
2477
+ }
2478
+ function readsSecret(command, ctx) {
2479
+ if (eachInvocation([command], (inv) => credentialProducer(inv)).length > 0)
2480
+ return true;
2481
+ const tokens = secretTokens(command, ctx.facts);
2482
+ if (tokens.length > 0) {
2483
+ const owner = eachInvocation([command], (inv) => isSecretOwner(inv.program, tokens, ctx.facts));
2484
+ if (owner.length === 0)
2485
+ return true;
2486
+ }
2487
+ if (commandHasUnresolvedSecret(command, ctx))
2488
+ return true;
2489
+ return (eachInvocation([command], (inv) => inv.program === "security" && inv.args.some((a) => KEYCHAIN_DUMP.test(a))).length > 0);
2490
+ }
2491
+ /**
2492
+ * Whether a secret read *earlier* in the line reaches the command at `index` through a pipe: the
2493
+ * commands share a pipeline, or the earlier one is inside a wrapper whose stdout feeds it.
2494
+ */
2495
+ function secretFlowsInto(ctx, index) {
2496
+ const target = ctx.commands[index];
2497
+ if (target === undefined)
2498
+ return false;
2499
+ const targetFlow = target.flow ?? target.pipeline;
2500
+ for (let i = 0; i < index; i += 1) {
2501
+ const other = ctx.commands[i];
2502
+ if (!readsSecret(other, ctx))
2503
+ continue;
2504
+ // The same pipe inside the line or a wrapper.
2505
+ if (other.pipeline === target.pipeline)
2506
+ return true;
2507
+ // Output of an earlier command/pipeline feeding the target's wrapper stdin. Commands inside
2508
+ // the target's own wrapper share `flow` too, but their own pipes are `pipeline`; only a
2509
+ // command from another (or no) wrapper feeds this one.
2510
+ if ((other.flow ?? other.pipeline) === targetFlow && other.block !== target.block)
2511
+ return true;
2512
+ }
2513
+ return false;
2514
+ }
2515
+ /** Flags whose value names a credential file the network tool uses (it is not sent as data). */
2516
+ function isCredentialFlag(program, flag) {
2517
+ if (["ssh", "scp", "sftp"].includes(program) &&
2518
+ ["-i", "--identity", "-F", "--ssh-config"].includes(flag)) {
2519
+ return true;
2520
+ }
2521
+ if (program === "rsync" && ["--password-file", "-e", "--rsh"].includes(flag))
2522
+ return true;
2523
+ if (program === "curl" &&
2524
+ ["--netrc-file", "--config", "--cacert", "--cert", "--key"].includes(flag)) {
2525
+ return true;
2526
+ }
2527
+ if (program === "wget" && ["--netrc-file", "--ca-certificate", "--certificate"].includes(flag)) {
2528
+ return true;
2529
+ }
2530
+ return false;
2531
+ }
2532
+ /** Flags whose value is a local file uploaded or posted as data. */
2533
+ const FILE_SEND_FLAGS = new Set([
2534
+ "-T",
2535
+ "--upload-file",
2536
+ "--post-file",
2537
+ "--body-file",
2538
+ ]);
2539
+ /** Flags whose `@file` / `name=@file` value is a local file; their bare value is data. */
2540
+ const DATA_SEND_FLAGS = new Set([
2541
+ "-d",
2542
+ "--data",
2543
+ "--data-binary",
2544
+ "--data-raw",
2545
+ "--data-urlencode",
2546
+ "-F",
2547
+ "--form",
2548
+ // `--json @file` reads the request body from a local file, like `-d @file`.
2549
+ "--json",
2550
+ ]);
2551
+ const isUrlLike = (value) => /^[a-z][a-z0-9+.-]*:\/\//i.test(value) || /^https?:/i.test(value);
2552
+ /**
2553
+ * The local files a send value names: `@file`, `name=@file`, curl's form `name=<file`, or a file
2554
+ * flag's bare value. Form parameters (`;type=...`) follow the file name. Only curl strips a
2555
+ * matching surrounding quote pair from such a value (`@"/path"`); wget and scp do not.
2556
+ */
2557
+ function sendPathCandidates(value, bareIsFile, stripQuotes = false) {
2558
+ // `@` and curl form's `<` both mark a value read from a local file.
2559
+ const marker = value.search(/[@<]/);
2560
+ let candidate = marker >= 0 ? (value.slice(marker + 1).split(";")[0] ?? "") : bareIsFile ? value : "";
2561
+ if (stripQuotes)
2562
+ candidate = stripQuotePair(candidate);
2563
+ if (candidate.length === 0 || candidate === "-")
2564
+ return [];
2565
+ if (isUrlLike(candidate) || isRemoteSpec(candidate))
2566
+ return [];
2567
+ if (marker < 0 && !looksLikePath(candidate) && !/\$\{?[A-Za-z_]/.test(candidate))
2568
+ return [];
2569
+ return [candidate];
2570
+ }
2571
+ /**
2572
+ * Whether an unresolved send value can plausibly be a secret. A URL, a remote path or a path
2573
+ * rooted at the run's cwd (`$(pwd)/...`, `$PWD/...`) cannot; `$HOME`, absolute paths, `../`,
2574
+ * secret-looking components and unknown variables can.
2575
+ */
2576
+ function unresolvedSendMatters(candidate) {
2577
+ if (isUrlLike(candidate) || isRemoteSpec(candidate))
2578
+ return false;
2579
+ if (/^(?:\$\(\s*pwd\s*\)|\$\{?PWD\}?)(?:\/|$)/.test(candidate))
2580
+ return false;
2581
+ if (/^(?:\$HOME|\$\{HOME\})(?:\/|$)/.test(candidate))
2582
+ return true;
2583
+ if (SECRET_LOOKING.test(candidate))
2584
+ return true;
2585
+ if (candidate.startsWith("/") || candidate.startsWith("../"))
2586
+ return true;
2587
+ return /\$\{?[A-Za-z_][A-Za-z0-9_]*\}?/.test(candidate);
2588
+ }
2589
+ /** A secret path a network command sends (as data), not one it uses as a credential. */
2590
+ function networkSendsSecret(inv, command, ctx) {
2591
+ const sendsPath = (candidate) => {
2592
+ const resolved = expandPath(candidate, ctx.facts, cwdOf(command, ctx.facts), command.vars);
2593
+ if (resolved !== null)
2594
+ return isSecretPath(resolved, ctx.facts);
2595
+ return unresolvedSendMatters(candidate);
2596
+ };
2597
+ const sends = (flag, value) => {
2598
+ const bareIsFile = FILE_SEND_FLAGS.has(flag);
2599
+ if (!bareIsFile && !DATA_SEND_FLAGS.has(flag))
2600
+ return false;
2601
+ return sendPathCandidates(value, bareIsFile, inv.program === "curl").some(sendsPath);
2602
+ };
2603
+ if (["scp", "sftp", "rsync"].includes(inv.program)) {
2604
+ // The local sources are what the command sends; the last operand is the remote destination.
2605
+ // Without a remote last operand (`scp KEY /tmp/`, `rsync -a DIR /tmp/x/`) the command copies
2606
+ // locally: the secret never leaves the machine, so only the secret-read rules apply.
2607
+ const sources = [];
2608
+ for (let i = 0; i < inv.args.length; i += 1) {
2609
+ const a = inv.args[i];
2610
+ if (isCredentialFlag(inv.program, a)) {
2611
+ i += 1;
2612
+ continue;
2613
+ }
2614
+ if (a.startsWith("-"))
2615
+ continue;
2616
+ sources.push(a);
2617
+ }
2618
+ const destination = sources[sources.length - 1];
2619
+ if (destination === undefined || !isRemoteSpec(destination))
2620
+ return false;
2621
+ sources.pop();
2622
+ return sources.some((source) => sendPathCandidates(source, true).some(sendsPath));
2623
+ }
2624
+ const { args } = inv;
2625
+ for (let i = 0; i < args.length; i += 1) {
2626
+ const a = args[i];
2627
+ const eq = a.indexOf("=");
2628
+ const flag = eq > 0 ? a.slice(0, eq) : a;
2629
+ if (isCredentialFlag(inv.program, flag)) {
2630
+ if (!a.includes("="))
2631
+ i += 1;
2632
+ continue;
2633
+ }
2634
+ if (eq > 0 && sends(flag, a.slice(eq + 1)))
2635
+ return true;
2636
+ if (args[i + 1] !== undefined && sends(a, args[i + 1])) {
2637
+ i += 1;
2638
+ return true;
2639
+ }
2640
+ const attached = /^-(?:d|T|F)(.+)$/.exec(a);
2641
+ if (attached !== null && attached[1] !== undefined && sends(a.slice(0, 2), attached[1])) {
2642
+ return true;
2643
+ }
2644
+ }
2645
+ return false;
2646
+ }
2647
+ /** Secret files a command uses as its own tool's credential (`curl --netrc-file`, `ssh -i`). */
2648
+ function credentialSecretTokens(command, ctx) {
2649
+ const out = new Set();
2650
+ for (const inv of invocationsOf(command)) {
2651
+ for (let i = 0; i < inv.args.length; i += 1) {
2652
+ const a = inv.args[i];
2653
+ const eq = a.indexOf("=");
2654
+ const flag = eq > 0 ? a.slice(0, eq) : a;
2655
+ if (!isCredentialFlag(inv.program, flag))
2656
+ continue;
2657
+ const value = eq > 0 ? a.slice(eq + 1) : inv.args[i + 1];
2658
+ if (value === undefined)
2659
+ continue;
2660
+ const resolved = expandPath(value, ctx.facts, cwdOf(command, ctx.facts), command.vars);
2661
+ if (resolved !== null)
2662
+ out.add(resolved);
2663
+ }
2664
+ }
2665
+ return out;
2666
+ }
2667
+ /** docker options that take a value, so its positional operands can be found. */
2668
+ const DOCKER_VALUE_OPTIONS = new Set([
2669
+ "-a",
2670
+ "--attach",
2671
+ "-c",
2672
+ "--cpu-shares",
2673
+ "--cidfile",
2674
+ "--cpuset-cpus",
2675
+ "--device",
2676
+ "-e",
2677
+ "--env",
2678
+ "--env-file",
2679
+ "--entrypoint",
2680
+ "-f",
2681
+ "--file",
2682
+ "-h",
2683
+ "--hostname",
2684
+ "-l",
2685
+ "--label",
2686
+ "--label-file",
2687
+ "--log-driver",
2688
+ "--log-opt",
2689
+ "-m",
2690
+ "--memory",
2691
+ "--mount",
2692
+ "--name",
2693
+ "--net",
2694
+ "--net-alias",
2695
+ "--network",
2696
+ "-p",
2697
+ "--publish",
2698
+ "--platform",
2699
+ "--restart",
2700
+ "--runtime",
2701
+ "--security-opt",
2702
+ "--stop-signal",
2703
+ "--stop-timeout",
2704
+ "--target",
2705
+ "-t",
2706
+ "--tag",
2707
+ "--tmpfs",
2708
+ "-u",
2709
+ "--user",
2710
+ "-v",
2711
+ "--volume",
2712
+ "--volumes-from",
2713
+ "-w",
2714
+ "--workdir",
2715
+ ]);
2716
+ /** sed options that take a value (so its script operand can be found). */
2717
+ const SED_VALUE_OPTIONS = new Set(["-e", "--expression", "-f", "--file"]);
2718
+ /** awk options that take a value (so its program operand can be found). */
2719
+ const AWK_VALUE_OPTIONS = new Set(["-f", "--file", "-v", "-F"]);
2720
+ /** grep options that take a value (so its pattern operand can be found). */
2721
+ const GREP_VALUE_OPTIONS = new Set([
2722
+ "-e",
2723
+ "--regexp",
2724
+ "-f",
2725
+ "--file",
2726
+ "-m",
2727
+ "--max-count",
2728
+ "-A",
2729
+ "--after-context",
2730
+ "-B",
2731
+ "--before-context",
2732
+ "-C",
2733
+ "--context",
2734
+ "-d",
2735
+ "--directories",
2736
+ "-D",
2737
+ "--devices",
2738
+ "--include",
2739
+ "--exclude",
2740
+ "--exclude-dir",
2741
+ ]);
2742
+ /**
2743
+ * Raw argument values in a clearly non-path position of a known program: a commit message, a
2744
+ * branch/ref name, a script name, a search pattern, a header, a data payload or a URL. An
2745
+ * unresolved variable there is ordinary text, so it must not turn a routine command into ask or
2746
+ * unsure. A value that names a file (`@file`, `<file`, sed/awk `-f FILE`) is never listed. Values
2747
+ * are counted, so the same word in a file position (`git commit -m $X -F $X`) is still checked.
2748
+ */
2749
+ function nonPathArguments(inv) {
2750
+ const out = new Map();
2751
+ const { program, args } = inv;
2752
+ const add = (value) => {
2753
+ if (value === undefined)
2754
+ return;
2755
+ if (value.includes("@") || value.includes("<"))
2756
+ return;
2757
+ out.set(value, (out.get(value) ?? 0) + 1);
2758
+ };
2759
+ // `-x VALUE`, `-xVALUE`, `--long VALUE`, `--long=VALUE`.
2760
+ const values = (flags) => {
2761
+ for (let i = 0; i < args.length; i += 1) {
2762
+ const a = args[i];
2763
+ for (const flag of flags) {
2764
+ if (a === flag) {
2765
+ add(args[i + 1]);
2766
+ }
2767
+ else if (flag.startsWith("--") && a.startsWith(`${flag}=`)) {
2768
+ add(a.slice(flag.length + 1));
2769
+ }
2770
+ else if (!flag.startsWith("--") && a.startsWith(flag) && a.length > flag.length) {
2771
+ add(a.slice(flag.length));
2772
+ }
2773
+ }
2774
+ }
2775
+ };
2776
+ const positional = () => args.filter((a) => !a.startsWith("-"));
2777
+ // An attached short `-fVALUE` (sed/awk/grep read a file) is not a script/pattern operand.
2778
+ const hasAttachedFile = (letter) => args.some((a) => a.startsWith(`-${letter}`) && !a.startsWith("--") && a.length > 2);
2779
+ if (program === "git") {
2780
+ values(["-m", "--message", "-b", "--branch", "--author"]);
2781
+ // `-c NAME=VALUE` / `--config NAME=VALUE`: a config value, not a local file path.
2782
+ for (let i = 0; i < inv.argv.length; i += 1) {
2783
+ const a = inv.argv[i];
2784
+ if ((a === "-c" || a === "--config") && inv.argv[i + 1] !== undefined)
2785
+ add(inv.argv[i + 1]);
2786
+ else if (a.startsWith("--config="))
2787
+ add(a.slice("--config=".length));
2788
+ else if (a.startsWith("-c") && a.length > 2)
2789
+ add(a.slice(2));
2790
+ }
2791
+ const sub = positional()[0];
2792
+ // Refs and remotes of these subcommands are not local file paths.
2793
+ if ([
2794
+ "checkout",
2795
+ "switch",
2796
+ "branch",
2797
+ "push",
2798
+ "pull",
2799
+ "fetch",
2800
+ "merge",
2801
+ "rebase",
2802
+ "tag",
2803
+ "remote",
2804
+ "cherry-pick",
2805
+ "revert",
2806
+ "reset",
2807
+ ].includes(sub ?? "")) {
2808
+ for (const p of positional().slice(1))
2809
+ add(p);
2810
+ }
2811
+ // `git clone <repo> [dir]`: the repository is a URL or a remote spec.
2812
+ if (sub === "clone")
2813
+ add(operands(args.slice(1))[0]);
2814
+ return out;
2815
+ }
2816
+ if (["npm", "pnpm", "yarn", "bun"].includes(program)) {
2817
+ const pos = positional();
2818
+ const run = pos.indexOf("run") >= 0 ? pos.indexOf("run") : pos.indexOf("run-script");
2819
+ if (run >= 0)
2820
+ add(pos[run + 1]);
2821
+ return out;
2822
+ }
2823
+ if (["grep", "egrep", "fgrep", "rg", "ag", "ack"].includes(program)) {
2824
+ const explicit = args.some((a) => ["-e", "--regexp", "-f", "--file"].includes(a) ||
2825
+ a.startsWith("--regexp=") ||
2826
+ a.startsWith("--file="));
2827
+ values(["-e", "--regexp"]);
2828
+ if (!explicit && !hasAttachedFile("f"))
2829
+ add(operands(args, GREP_VALUE_OPTIONS)[0]);
2830
+ return out;
2831
+ }
2832
+ if (program === "sed") {
2833
+ const explicit = args.some((a) => a === "-e" ||
2834
+ a === "--expression" ||
2835
+ a === "-f" ||
2836
+ a === "--file" ||
2837
+ a.startsWith("--expression=") ||
2838
+ a.startsWith("--file="));
2839
+ values(["-e", "--expression"]);
2840
+ if (!explicit && !hasAttachedFile("f"))
2841
+ add(operands(args, SED_VALUE_OPTIONS)[0]);
2842
+ return out;
2843
+ }
2844
+ if (program === "awk" || program === "gawk") {
2845
+ const explicit = args.some((a) => a === "-f" || a === "--file" || a.startsWith("--file="));
2846
+ if (!explicit && !hasAttachedFile("f"))
2847
+ add(operands(args, AWK_VALUE_OPTIONS)[0]);
2848
+ return out;
2849
+ }
2850
+ if (program === "curl" || program === "wget") {
2851
+ // The last operand is the URL; an earlier one is an option value (`-o FILE`, `-T FILE`).
2852
+ const pos = positional();
2853
+ add(pos[pos.length - 1]);
2854
+ values([
2855
+ "-H",
2856
+ "--header",
2857
+ "-X",
2858
+ "--request",
2859
+ "-A",
2860
+ "--user-agent",
2861
+ "-e",
2862
+ "--referer",
2863
+ "-u",
2864
+ "--user",
2865
+ "-b",
2866
+ "--cookie",
2867
+ "--url",
2868
+ "-w",
2869
+ "--write-out",
2870
+ "--post-data",
2871
+ "--body-data",
2872
+ "--method",
2873
+ ]);
2874
+ values([
2875
+ "-d",
2876
+ "--data",
2877
+ "--data-binary",
2878
+ "--data-raw",
2879
+ "--data-urlencode",
2880
+ "-F",
2881
+ "--form",
2882
+ "--json",
2883
+ ]);
2884
+ return out;
2885
+ }
2886
+ if (program === "gh") {
2887
+ // `--body-file` and other file flags are not listed: they name a file gh reads.
2888
+ values([
2889
+ "-t",
2890
+ "--title",
2891
+ "-b",
2892
+ "--body",
2893
+ "-H",
2894
+ "--head",
2895
+ "-B",
2896
+ "--base",
2897
+ "-R",
2898
+ "--repo",
2899
+ "-l",
2900
+ "--label",
2901
+ "-a",
2902
+ "--assignee",
2903
+ "--reviewer",
2904
+ "-m",
2905
+ "--milestone",
2906
+ ]);
2907
+ return out;
2908
+ }
2909
+ if (program === "docker") {
2910
+ // docker's positional operands are images, container commands and arguments, not local paths.
2911
+ for (const p of operands(args, DOCKER_VALUE_OPTIONS))
2912
+ add(p);
2913
+ values([
2914
+ "-t",
2915
+ "--tag",
2916
+ "--name",
2917
+ "-e",
2918
+ "--env",
2919
+ "-l",
2920
+ "--label",
2921
+ "-p",
2922
+ "--publish",
2923
+ "-u",
2924
+ "--user",
2925
+ "-w",
2926
+ "--workdir",
2927
+ "--network",
2928
+ "--net",
2929
+ "--platform",
2930
+ "--entrypoint",
2931
+ "--restart",
2932
+ ]);
2933
+ return out;
2934
+ }
2935
+ if (program === "echo" || program === "printf") {
2936
+ for (const a of args)
2937
+ add(a);
2938
+ }
2939
+ return out;
2940
+ }
2941
+ /** Every non-path argument value of a command, with how many times it may be subtracted. */
2942
+ function nonPathTokens(command) {
2943
+ const out = new Map();
2944
+ for (const inv of invocationsOf(command)) {
2945
+ for (const [value, count] of nonPathArguments(inv)) {
2946
+ out.set(value, (out.get(value) ?? 0) + count);
2947
+ }
2948
+ }
2949
+ return out;
2950
+ }
2951
+ /** Subtract one allowlisted occurrence of `token`; false when it has none left. */
2952
+ function consumeNonPath(allowed, token) {
2953
+ const count = allowed.get(token) ?? 0;
2954
+ if (count <= 0)
2955
+ return false;
2956
+ allowed.set(token, count - 1);
2957
+ return true;
2958
+ }
2959
+ /* -------------------------------------------------------------------------------------------- *
2960
+ * Git commands that run arbitrary programs
2961
+ * -------------------------------------------------------------------------------------------- */
2962
+ const SHELL_METACHARS = /[|;&<>$`]/;
2963
+ /** The value is a single plain program from `allowed` (options allowed, no shell syntax). */
2964
+ function plainProgram(value, allowed) {
2965
+ const trimmed = value.trim();
2966
+ if (trimmed.length === 0)
2967
+ return false;
2968
+ if (SHELL_METACHARS.test(trimmed))
2969
+ return false;
2970
+ const first = baseName(trimmed.split(/\s+/)[0] ?? "");
2971
+ if (!allowed.has(first))
2972
+ return false;
2973
+ // A sub-program named later (`ssh -o ProxyCommand=...`) makes the value an execution surface.
2974
+ if (/(?:^|\s)(?:curl|wget|nc|ncat|netcat|socat|telnet|base64|xxd|eval)(?:\s|$)/.test(trimmed)) {
2975
+ return false;
2976
+ }
2977
+ return !trimmed.includes("/dev/tcp");
2978
+ }
2979
+ const SSH_PROGRAMS = new Set(["ssh"]);
2980
+ const PAGER_PROGRAMS = new Set(["cat", "less", "more", "most", "delta"]);
2981
+ const EDITOR_PROGRAMS = new Set([
2982
+ "vi",
2983
+ "vim",
2984
+ "nvim",
2985
+ "nano",
2986
+ "emacs",
2987
+ "code",
2988
+ "ed",
2989
+ ]);
2990
+ const GIT_ENV_SSH = new Set(["GIT_SSH_COMMAND", "GIT_SSH"]);
2991
+ const GIT_ENV_ALWAYS = new Set([
2992
+ "GIT_ASKPASS",
2993
+ "SSH_ASKPASS",
2994
+ "GIT_CONFIG_PARAMETERS",
2995
+ "GIT_EXTERNAL_DIFF",
2996
+ ]);
2997
+ const GIT_ENV_PAGER = new Set(["PAGER", "GIT_PAGER"]);
2998
+ const GIT_ENV_EDITOR = new Set([
2999
+ "GIT_EDITOR",
3000
+ "GIT_SEQUENCE_EDITOR",
3001
+ "EDITOR",
3002
+ "VISUAL",
3003
+ ]);
3004
+ /** An environment assignment whose value is a program git runs on its behalf. */
3005
+ function gitEnvIsDangerous(token) {
3006
+ const match = ASSIGNMENT.exec(token);
3007
+ if (match === null)
3008
+ return false;
3009
+ const name = match[1];
3010
+ const value = match[2] ?? "";
3011
+ if (GIT_ENV_SSH.has(name))
3012
+ return !plainProgram(value, SSH_PROGRAMS);
3013
+ if (GIT_ENV_PAGER.has(name))
3014
+ return !plainProgram(value, PAGER_PROGRAMS);
3015
+ if (GIT_ENV_EDITOR.has(name))
3016
+ return !plainProgram(value, EDITOR_PROGRAMS);
3017
+ if (GIT_ENV_ALWAYS.has(name))
3018
+ return true;
3019
+ return (name === "GIT_CONFIG_COUNT" ||
3020
+ name.startsWith("GIT_CONFIG_KEY_") ||
3021
+ name.startsWith("GIT_CONFIG_VALUE_"));
3022
+ }
3023
+ /** A config setting that names a program git runs (or an alias that is a shell command). */
3024
+ function gitConfigIsDangerous(key, value) {
3025
+ const k = key.toLowerCase();
3026
+ if (k.startsWith("alias."))
3027
+ return value.trimStart().startsWith("!");
3028
+ if (k === "core.sshcommand")
3029
+ return !plainProgram(value, SSH_PROGRAMS);
3030
+ if (k === "core.pager")
3031
+ return !plainProgram(value, PAGER_PROGRAMS);
3032
+ if (k === "core.editor" || k === "sequence.editor") {
3033
+ return !plainProgram(value, EDITOR_PROGRAMS);
3034
+ }
3035
+ if (k === "core.askpass" || k === "diff.external" || /^http\..*\.cmd$/.test(k))
3036
+ return true;
3037
+ return false;
3038
+ }
3039
+ /**
3040
+ * Whether a git command makes git run a program the review did not see: an executing `-c` config
3041
+ * value (`core.sshCommand`, `core.pager`, `diff.external`, `http.*.cmd`, `alias.*=!…`), a config
3042
+ * write of one, or an environment override (`GIT_SSH_COMMAND`, `GIT_ASKPASS`, `PAGER`, editors).
3043
+ * A value that is a plain ssh/pager/editor command is the tool's own transport and stays allowed.
3044
+ */
3045
+ function gitRunsArbitraryProgram(command) {
3046
+ let isGit = false;
3047
+ for (const inv of invocationsOf(command)) {
3048
+ if (inv.program !== "git")
3049
+ continue;
3050
+ isGit = true;
3051
+ for (let i = 0; i < inv.argv.length; i += 1) {
3052
+ const a = inv.argv[i];
3053
+ let setting;
3054
+ if ((a === "-c" || a === "--config") && inv.argv[i + 1] !== undefined) {
3055
+ setting = inv.argv[i + 1];
3056
+ }
3057
+ else if (a.startsWith("--config=")) {
3058
+ setting = a.slice("--config=".length);
3059
+ }
3060
+ else if (a.startsWith("-c") && a.length > 2) {
3061
+ setting = a.slice(2);
3062
+ }
3063
+ if (setting === undefined)
3064
+ continue;
3065
+ const eq = setting.indexOf("=");
3066
+ if (eq <= 0)
3067
+ continue;
3068
+ if (gitConfigIsDangerous(setting.slice(0, eq), setting.slice(eq + 1)))
3069
+ return true;
3070
+ }
3071
+ if (inv.args[0] === "config") {
3072
+ const configOperands = inv.args.slice(1).filter((a) => !a.startsWith("-"));
3073
+ const key = configOperands[0];
3074
+ const value = configOperands[1];
3075
+ if (key !== undefined && value !== undefined && gitConfigIsDangerous(key, value))
3076
+ return true;
3077
+ }
3078
+ }
3079
+ if (!isGit)
3080
+ return false;
3081
+ // Only the environment prefix (before the git program) can override git's subprocesses; a
3082
+ // `PAGER=...` value in `git commit -m PAGER=...` is data.
3083
+ const gitIndex = command.argv.findIndex((token) => baseName(token) === "git");
3084
+ const prefix = gitIndex < 0 ? [] : command.argv.slice(0, gitIndex);
3085
+ return prefix.some((token) => gitEnvIsDangerous(token));
3086
+ }
3087
+ /* -------------------------------------------------------------------------------------------- *
3088
+ * Fail-closed floor for dynamic arguments
3089
+ * -------------------------------------------------------------------------------------------- */
3090
+ /** Programs whose non-option arguments name files (so a dynamic value there is a path). */
3091
+ const FILE_READERS = new Set([
3092
+ "cat",
3093
+ "head",
3094
+ "tail",
3095
+ "less",
3096
+ "more",
3097
+ "bat",
3098
+ "grep",
3099
+ "egrep",
3100
+ "fgrep",
3101
+ "rg",
3102
+ "ag",
3103
+ "ack",
3104
+ "sed",
3105
+ "awk",
3106
+ "gawk",
3107
+ "find",
3108
+ "stat",
3109
+ "file",
3110
+ "wc",
3111
+ "sort",
3112
+ "uniq",
3113
+ "cut",
3114
+ "tr",
3115
+ "diff",
3116
+ "cmp",
3117
+ "md5",
3118
+ "md5sum",
3119
+ "shasum",
3120
+ "sha256sum",
3121
+ "strings",
3122
+ "od",
3123
+ "xxd",
3124
+ "hexdump",
3125
+ "jq",
3126
+ "yq",
3127
+ "column",
3128
+ "nl",
3129
+ "realpath",
3130
+ "readlink",
3131
+ "du",
3132
+ "ls",
3133
+ "tree",
3134
+ "unzip",
3135
+ "zip",
3136
+ "gzip",
3137
+ "gunzip",
3138
+ "tar",
3139
+ ]);
3140
+ const FILE_ARGUMENT_PROGRAMS = new Set([...FILE_READERS, ...WRITE_PROGRAMS]);
3141
+ /** A value that, after `$(pwd)` / `$PWD`, has no unresolved part left to fear. */
3142
+ function knownPathRoot(candidate, cwd) {
3143
+ const staticized = candidate.replace(/\$\(\s*pwd\s*\)/g, cwd).replace(/\$\{?PWD\}?/g, cwd);
3144
+ return !/[$`]/.test(staticized);
3145
+ }
3146
+ /** A dynamic value that names a path: a bare variable, a separator, a relative root or a suffix. */
3147
+ function isPathLikeDynamic(candidate) {
3148
+ if (isUrlLike(candidate) || isRemoteSpec(candidate))
3149
+ return false;
3150
+ if (BARE_VARIABLE.test(candidate))
3151
+ return true;
3152
+ if (candidate.includes("/") || candidate.startsWith("~") || candidate.startsWith(".")) {
3153
+ return true;
3154
+ }
3155
+ return /\.[A-Za-z0-9]{1,8}$/.test(candidate);
3156
+ }
3157
+ /** Long options whose `--name=VALUE` value names a local file to read or write. */
3158
+ const FILE_LONG_OPTIONS = new Set([
3159
+ "--file",
3160
+ "--output",
3161
+ "--upload-file",
3162
+ "--post-file",
3163
+ "--body-file",
3164
+ "--directory",
3165
+ "--input",
3166
+ ]);
3167
+ /**
3168
+ * The generic fail-closed floor: a value with a variable, substitution, backtick or quote that
3169
+ * cannot be resolved is never allowed in a path position. A command whose program reads or writes
3170
+ * files has every non-option value in a file position; any other command needs a path-like value.
3171
+ */
3172
+ function dynamicArgumentAsks(command, ctx) {
3173
+ const nonPath = nonPathTokens(command);
3174
+ const written = new Set(writeTargets(command));
3175
+ const filePosition = invocationsOf(command).some((inv) => FILE_ARGUMENT_PROGRAMS.has(inv.program));
3176
+ const cwd = cwdOf(command, ctx.facts);
3177
+ const tokens = [...command.argv, ...command.redirects.map((redirect) => redirect.target)];
3178
+ for (const token of tokens) {
3179
+ if (!/[$`"']/.test(token))
3180
+ continue;
3181
+ if (written.has(token) || consumeNonPath(nonPath, token))
3182
+ continue;
3183
+ // A leading environment assignment is a variable value, not a file argument.
3184
+ if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(token))
3185
+ continue;
3186
+ // `pathCandidates` drops dynamic words that are not path-shaped (`$A$B`, `$(date).txt`); in a
3187
+ // file position the word itself is still a path. An option carries its value after `=` or, for
3188
+ // the file flags (`-f` sed/awk/grep, `-T`, `-o`, `-O`, `-D`), attached to the letter.
3189
+ const candidates = pathCandidates(token);
3190
+ let probes;
3191
+ if (candidates.length > 0) {
3192
+ probes = candidates;
3193
+ }
3194
+ else if (token.startsWith("-")) {
3195
+ const eq = token.indexOf("=");
3196
+ const longName = eq > 0 ? token.slice(0, eq) : token;
3197
+ const attached = /^-[fToOD](.+)$/.exec(token);
3198
+ const value = eq > 0 && FILE_LONG_OPTIONS.has(longName) ? token.slice(eq + 1) : attached?.[1];
3199
+ probes =
3200
+ value !== undefined && value.length > 0 && !nonPath.has(value) && !written.has(value)
3201
+ ? [value]
3202
+ : [];
3203
+ }
3204
+ else {
3205
+ probes = [token];
3206
+ }
3207
+ for (const candidate of probes) {
3208
+ if (expandPath(candidate, ctx.facts, cwd, command.vars) !== null)
3209
+ continue;
3210
+ if (isUrlLike(candidate) || isRemoteSpec(candidate))
3211
+ continue;
3212
+ if (knownPathRoot(candidate, cwd))
3213
+ continue;
3214
+ if (filePosition || isPathLikeDynamic(candidate))
3215
+ return true;
3216
+ }
3217
+ }
3218
+ return false;
3219
+ }
3220
+ function isServeInvocation(inv) {
3221
+ const { program, args } = inv;
3222
+ if (program === "python" || program === "python3") {
3223
+ return args.includes("-m") && args.some((a) => a === "http.server" || a === "SimpleHTTPServer");
3224
+ }
3225
+ if (program === "php")
3226
+ return args.includes("-S");
3227
+ if (program === "flask")
3228
+ return args[0] === "run";
3229
+ if (program === "rails")
3230
+ return args[0] === "server" || args[0] === "s";
3231
+ if (SAFE_SERVE_PROGRAMS.has(program))
3232
+ return true;
3233
+ if (["next", "vite", "nuxt", "ng"].includes(program)) {
3234
+ const sub = nonOptionArgs(args)[0];
3235
+ return sub === undefined || ["dev", "start", "serve", "preview", "watch"].includes(sub);
3236
+ }
3237
+ if (["npm", "pnpm", "yarn", "bun"].includes(program)) {
3238
+ const sub = args.find((a) => !a.startsWith("-"));
3239
+ const script = sub === "run" ? args[args.indexOf("run") + 1] : sub;
3240
+ return script === "dev" || script === "start" || script === "serve" || script === "preview";
3241
+ }
3242
+ return false;
3243
+ }
3244
+ function writesOutside(inv, command, ctx) {
3245
+ for (const target of writeTargetsOf(inv)) {
3246
+ const resolved = expandPath(target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3247
+ if (resolved !== null && !inside(resolved, ctx.facts.workspace) && !tmpRootOf(resolved))
3248
+ return true;
3249
+ }
3250
+ for (const redirect of command.redirects) {
3251
+ if (redirect.op === "<")
3252
+ continue;
3253
+ const resolved = expandPath(redirect.target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3254
+ if (resolved !== null && !inside(resolved, ctx.facts.workspace) && !tmpRootOf(resolved))
3255
+ return true;
3256
+ }
3257
+ return false;
3258
+ }
3259
+ function rmIsRecursiveForce(args) {
3260
+ let recursive = false;
3261
+ let force = false;
3262
+ for (const a of args) {
3263
+ if (a === "--recursive")
3264
+ recursive = true;
3265
+ else if (a === "--force")
3266
+ force = true;
3267
+ else if (/^-[A-Za-z]+$/.test(a)) {
3268
+ if (a.includes("r") || a.includes("R"))
3269
+ recursive = true;
3270
+ if (a.includes("f"))
3271
+ force = true;
3272
+ }
3273
+ }
3274
+ return recursive && force;
3275
+ }
3276
+ /** The part of a path before its first glob segment: `rm -rf /home/*` deletes `/home`. */
3277
+ function globBase(path) {
3278
+ const parts = path.split("/");
3279
+ const index = parts.findIndex((part) => part.includes("*") || part.includes("?"));
3280
+ if (index < 0)
3281
+ return path;
3282
+ return parts.slice(0, index).join("/") || "/";
3283
+ }
3284
+ function isDangerousDelete(resolved, ctx) {
3285
+ const base = globBase(resolved);
3286
+ const t = tildeForm(base, ctx.facts);
3287
+ if (t === "/" || t === "~" || t === "~/")
3288
+ return true;
3289
+ return isAncestor(base, ctx.facts.workspace);
3290
+ }
3291
+ function modifiesDevice(command, ctx) {
3292
+ // Only a write redirect that really lands on a device path counts (`2>/dev/null`, a listing to
3293
+ // /tmp and a read of the device files are not edits).
3294
+ const redirectsDevice = command.redirects.some((redirect) => {
3295
+ if (redirect.op === "<" || isIgnoredDevice(redirect.target))
3296
+ return false;
3297
+ const resolved = expandPath(redirect.target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3298
+ return resolved !== null && isDevicePath(resolved, ctx.facts);
3299
+ });
3300
+ if (redirectsDevice)
3301
+ return true;
3302
+ return (eachInvocation([command], (inv) => [
3303
+ "rm",
3304
+ "mv",
3305
+ "cp",
3306
+ "chmod",
3307
+ "chown",
3308
+ "truncate",
3309
+ "tee",
3310
+ "sed",
3311
+ "dd",
3312
+ "ln",
3313
+ "touch",
3314
+ "install",
3315
+ "shred",
3316
+ "unlink",
3317
+ "vi",
3318
+ "vim",
3319
+ "nano",
3320
+ "emacs",
3321
+ "code",
3322
+ "ed",
3323
+ ].includes(inv.program)).length > 0);
3324
+ }
3325
+ const RULES = [
3326
+ {
3327
+ id: "allow.read",
3328
+ outcome: "allow",
3329
+ check: (ctx) => {
3330
+ const matched = eachInvocation(ctx.commands, (inv, command) => {
3331
+ if (!READ_PROGRAMS.has(inv.program))
3332
+ return false;
3333
+ if (inv.program === "sed" && hasInPlace(inv.args))
3334
+ return false;
3335
+ if (inv.program === "kill" && !inv.args.includes("-l"))
3336
+ return false;
3337
+ if (credentialProducer(inv))
3338
+ return false;
3339
+ if (inv.program === "crontab" &&
3340
+ (!inv.args.includes("-l") || nonOptionArgs(inv.args).length > 0))
3341
+ return false;
3342
+ if (inv.program === "launchctl") {
3343
+ const action = nonOptionArgs(inv.args)[0];
3344
+ if (action !== undefined && LAUNCHCTL_CHANGES.includes(action))
3345
+ return false;
3346
+ }
3347
+ // `find -delete` / `-exec` / `-fprint` is not a read: its targets are dynamic.
3348
+ if (inv.program === "find" &&
3349
+ inv.args.some((a) => [
3350
+ "-delete",
3351
+ "-exec",
3352
+ "-execdir",
3353
+ "-ok",
3354
+ "-okdir",
3355
+ "-fprint",
3356
+ "-fprintf",
3357
+ "-fls",
3358
+ ].includes(a))) {
3359
+ return false;
3360
+ }
3361
+ // A path-like word or redirection target with an unresolved variable is not a confident
3362
+ // read; the line falls to `unsure` instead of `allow`. A value in a known non-path position
3363
+ // (`git commit -m $MSG`, a grep pattern) does not count.
3364
+ const nonPath = nonPathTokens(command);
3365
+ const unresolved = [
3366
+ ...inv.argv,
3367
+ ...command.redirects.map((redirect) => redirect.target),
3368
+ ].some((token) => {
3369
+ if (consumeNonPath(nonPath, token))
3370
+ return false;
3371
+ return pathCandidates(token).some((candidate) => isConcretePathWord(candidate) &&
3372
+ expandPath(candidate, ctx.facts, cwdOf(command, ctx.facts), command.vars) === null);
3373
+ });
3374
+ if (unresolved)
3375
+ return false;
3376
+ // Reading a secret path is not "reading anything except the secret paths".
3377
+ return (secretTokens({ argv: inv.argv, redirects: command.redirects, pipeline: command.pipeline }, ctx.facts).length === 0);
3378
+ });
3379
+ return matched.length > 0
3380
+ ? { reason: "Reads files; reading is allowed.", commands: matched }
3381
+ : null;
3382
+ },
3383
+ },
3384
+ {
3385
+ id: "allow.dev",
3386
+ outcome: "allow",
3387
+ check: (ctx) => {
3388
+ const matched = eachInvocation(ctx.commands, (inv) => {
3389
+ if (credentialProducer(inv))
3390
+ return false;
3391
+ if (globalInstallOf(inv.program, inv.args, ctx.venv || venvBinary(inv.raw)))
3392
+ return false;
3393
+ const local = inv.raw.startsWith("./") || inv.raw.startsWith("../");
3394
+ const isDev = DEV_PROGRAMS.has(inv.program) ||
3395
+ (SHELL_PROGRAMS.has(inv.program) && wrapperScript(inv) === null) ||
3396
+ venvBinary(inv.raw) ||
3397
+ local;
3398
+ if (!isDev)
3399
+ return false;
3400
+ // Starting a server is its own rule.
3401
+ return !isServeInvocation(inv);
3402
+ });
3403
+ return matched.length > 0
3404
+ ? {
3405
+ reason: "Builds, tests or formats the project; development commands are allowed.",
3406
+ commands: matched,
3407
+ }
3408
+ : null;
3409
+ },
3410
+ },
3411
+ {
3412
+ id: "allow.git",
3413
+ outcome: "allow",
3414
+ check: (ctx) => {
3415
+ const matched = eachInvocation(ctx.commands, (inv, command) => {
3416
+ if (inv.program !== "git")
3417
+ return false;
3418
+ // Printing a stored credential (`git credential fill`) is not a plain git command.
3419
+ if (credentialProducer(inv))
3420
+ return false;
3421
+ // A config value or environment override that makes git run another program is not a
3422
+ // plain git command (`ask.git-exec`).
3423
+ if (gitRunsArbitraryProgram(command))
3424
+ return false;
3425
+ if (inv.args[0] === "push" && gitPushKind(inv.args.slice(1)) !== null)
3426
+ return false;
3427
+ if (inv.args[0] === "config" && hasArg(inv.args.slice(1), "--global", "--system"))
3428
+ return false;
3429
+ return true;
3430
+ });
3431
+ return matched.length > 0
3432
+ ? {
3433
+ reason: "A git command that does not push forcibly or rewrite a remote; this is allowed.",
3434
+ commands: matched,
3435
+ }
3436
+ : null;
3437
+ },
3438
+ },
3439
+ {
3440
+ id: "allow.gh",
3441
+ outcome: "allow",
3442
+ check: (ctx) => {
3443
+ const matched = eachInvocation(ctx.commands, (inv) => {
3444
+ if (inv.program !== "gh")
3445
+ return false;
3446
+ // `gh auth token` prints the login token; it is not an ordinary gh read.
3447
+ if (credentialProducer(inv))
3448
+ return false;
3449
+ const cleaned = inv.args.filter((a) => !a.startsWith("-"));
3450
+ if (cleaned[0] === "pr" && cleaned[1] === "merge")
3451
+ return false;
3452
+ if (cleaned[0] === "repo" && cleaned[1] === "delete")
3453
+ return false;
3454
+ if (cleaned[0] === "release" && cleaned[1] === "delete")
3455
+ return false;
3456
+ return ghIsSafe(inv.args);
3457
+ });
3458
+ return matched.length > 0
3459
+ ? {
3460
+ reason: "A GitHub command that only reads or opens a pull request; this is allowed.",
3461
+ commands: matched,
3462
+ }
3463
+ : null;
3464
+ },
3465
+ },
3466
+ {
3467
+ id: "allow.network",
3468
+ outcome: "allow",
3469
+ check: (ctx) => {
3470
+ const matched = [];
3471
+ for (let index = 0; index < ctx.commands.length; index += 1) {
3472
+ const command = ctx.commands[index];
3473
+ const allowed = invocationsOf(command).some((inv) => {
3474
+ if (!NETWORK_PROGRAMS.has(inv.program))
3475
+ return false;
3476
+ if (networkSendsSecret(inv, command, ctx))
3477
+ return false;
3478
+ if (writesOutside(inv, command, ctx))
3479
+ return false;
3480
+ // `nc host < ~/.ssh/id_rsa`: the command sends what it reads from a secret.
3481
+ const readsSecretRedirect = command.redirects.some((redirect) => {
3482
+ if (redirect.op !== "<")
3483
+ return false;
3484
+ const resolved = expandPath(redirect.target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3485
+ return resolved === null || isSecretPath(resolved, ctx.facts);
3486
+ });
3487
+ if (readsSecretRedirect)
3488
+ return false;
3489
+ // Only a secret read *before* this command can flow into it.
3490
+ return !secretFlowsInto(ctx, index);
3491
+ });
3492
+ if (allowed)
3493
+ matched.push(command);
3494
+ }
3495
+ return matched.length > 0
3496
+ ? {
3497
+ reason: "Uses the network; network access is allowed when no secret or outside write is involved.",
3498
+ commands: matched,
3499
+ }
3500
+ : null;
3501
+ },
3502
+ },
3503
+ {
3504
+ id: "allow.serve",
3505
+ outcome: "allow",
3506
+ check: (ctx) => {
3507
+ const matched = eachInvocation(ctx.commands, (inv) => isServeInvocation(inv));
3508
+ return matched.length > 0
3509
+ ? { reason: "Starts a server or opens a browser; this is allowed.", commands: matched }
3510
+ : null;
3511
+ },
3512
+ },
3513
+ {
3514
+ id: "allow.docker",
3515
+ outcome: "allow",
3516
+ check: (ctx) => {
3517
+ const matched = eachInvocation(ctx.commands, (inv, command) => inv.program === "docker" &&
3518
+ dockerPrivileged(inv.args, ctx.facts, cwdOf(command, ctx.facts), command.vars) === null);
3519
+ return matched.length > 0
3520
+ ? { reason: "A Docker command without host access; this is allowed.", commands: matched }
3521
+ : null;
3522
+ },
3523
+ },
3524
+ {
3525
+ id: "allow.workspace-write",
3526
+ outcome: "allow",
3527
+ check: (ctx) => {
3528
+ const matched = [];
3529
+ for (const command of ctx.commands) {
3530
+ if (writeTargets(command).some((target) => {
3531
+ const resolved = expandPath(target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3532
+ return (resolved !== null && inside(resolved, ctx.facts.workspace) && !isGitHook(resolved));
3533
+ })) {
3534
+ matched.push(command);
3535
+ }
3536
+ }
3537
+ return matched.length > 0
3538
+ ? {
3539
+ reason: "Writes inside the workspace; this is allowed.",
3540
+ commands: matched,
3541
+ covered: matched.filter(isKnownCommand),
3542
+ }
3543
+ : null;
3544
+ },
3545
+ },
3546
+ {
3547
+ id: "allow.tmp-write",
3548
+ outcome: "allow",
3549
+ check: (ctx) => {
3550
+ const matched = [];
3551
+ for (const command of ctx.commands) {
3552
+ if (writeTargets(command).some((target) => {
3553
+ const resolved = expandPath(target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3554
+ return resolved !== null && tmpRootOf(resolved);
3555
+ })) {
3556
+ matched.push(command);
3557
+ }
3558
+ }
3559
+ return matched.length > 0
3560
+ ? {
3561
+ reason: "Writes to the device's temporary files; this is allowed.",
3562
+ commands: matched,
3563
+ covered: matched.filter(isKnownCommand),
3564
+ }
3565
+ : null;
3566
+ },
3567
+ },
3568
+ {
3569
+ id: "allow.own-kill",
3570
+ outcome: "allow",
3571
+ check: (ctx) => {
3572
+ const matched = eachInvocation(ctx.commands, (inv) => {
3573
+ if (inv.program !== "kill")
3574
+ return false;
3575
+ const targets = nonOptionArgs(inv.args);
3576
+ if (targets.length === 0)
3577
+ return false;
3578
+ return targets.every((t) => t === "$$" || t === "$!" || /^%[A-Za-z0-9_-]+$/.test(t));
3579
+ });
3580
+ return matched.length > 0
3581
+ ? {
3582
+ reason: "Stops a process or job the device started; this is allowed.",
3583
+ commands: matched,
3584
+ }
3585
+ : null;
3586
+ },
3587
+ },
3588
+ {
3589
+ id: "ask.outside-write",
3590
+ outcome: "ask",
3591
+ check: (ctx) => {
3592
+ const matched = [];
3593
+ let why = null;
3594
+ for (const command of ctx.commands) {
3595
+ for (const target of writeTargets(command)) {
3596
+ const resolved = expandPath(target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3597
+ if (resolved !== null && (inside(resolved, ctx.facts.workspace) || tmpRootOf(resolved)))
3598
+ continue;
3599
+ if (resolved !== null &&
3600
+ (isGitHook(resolved) ||
3601
+ isPersistencePath(resolved, ctx.facts) ||
3602
+ isDevicePath(resolved, ctx.facts)))
3603
+ continue;
3604
+ if (resolved !== null && isDangerousDelete(resolved, ctx))
3605
+ continue;
3606
+ matched.push(command);
3607
+ why ??=
3608
+ resolved === null
3609
+ ? "Writes to a path that cannot be resolved statically; this needs approval."
3610
+ : `Writes or deletes ${target} outside the workspace and temporary files; this needs approval.`;
3611
+ }
3612
+ }
3613
+ return why === null
3614
+ ? null
3615
+ : { reason: why, commands: matched, covered: matched.filter(isKnownCommand) };
3616
+ },
3617
+ },
3618
+ {
3619
+ id: "ask.force-push",
3620
+ outcome: "ask",
3621
+ check: (ctx) => {
3622
+ let why = null;
3623
+ const matched = eachInvocation(ctx.commands, (inv) => {
3624
+ if (inv.program === "git" && inv.args[0] === "push") {
3625
+ const kind = gitPushKind(inv.args.slice(1));
3626
+ if (kind === "force") {
3627
+ why ??= "Force-pushes to a remote; this needs approval.";
3628
+ return true;
3629
+ }
3630
+ if (kind === "protected") {
3631
+ why ??=
3632
+ "Pushes to a protected branch (main, master or release/*); this needs approval.";
3633
+ return true;
3634
+ }
3635
+ }
3636
+ if (inv.program === "gh" && inv.args[0] === "pr" && inv.args[1] === "merge") {
3637
+ why ??= "Merges a pull request; this needs approval.";
3638
+ return true;
3639
+ }
3640
+ return false;
3641
+ });
3642
+ return why === null ? null : { reason: why, commands: matched };
3643
+ },
3644
+ },
3645
+ {
3646
+ id: "ask.remote-destroy",
3647
+ outcome: "ask",
3648
+ check: (ctx) => {
3649
+ let why = null;
3650
+ const matched = eachInvocation(ctx.commands, (inv) => {
3651
+ if (inv.program === "git" &&
3652
+ inv.args[0] === "push" &&
3653
+ gitPushKind(inv.args.slice(1)) === "remote-destroy") {
3654
+ why ??= "Deletes remote branches or tags; this needs approval.";
3655
+ return true;
3656
+ }
3657
+ if (inv.program === "gh" && inv.args[0] === "repo" && inv.args[1] === "delete") {
3658
+ why ??= "Deletes a GitHub repository; this needs approval.";
3659
+ return true;
3660
+ }
3661
+ if (inv.program === "gh" && inv.args[0] === "release" && inv.args[1] === "delete") {
3662
+ why ??= "Deletes a GitHub release; this needs approval.";
3663
+ return true;
3664
+ }
3665
+ return false;
3666
+ });
3667
+ return why === null ? null : { reason: why, commands: matched };
3668
+ },
3669
+ },
3670
+ {
3671
+ id: "ask.rc-files",
3672
+ outcome: "ask",
3673
+ check: (ctx) => {
3674
+ let why = null;
3675
+ const matched = eachInvocation(ctx.commands, (inv) => {
3676
+ if (inv.program === "crontab" &&
3677
+ (!inv.args.includes("-l") || nonOptionArgs(inv.args).length > 0)) {
3678
+ why ??= "Changes crontab; this needs approval.";
3679
+ return true;
3680
+ }
3681
+ if (inv.program === "launchctl") {
3682
+ const action = nonOptionArgs(inv.args)[0];
3683
+ if (action !== undefined && LAUNCHCTL_CHANGES.includes(action)) {
3684
+ why ??= "Changes a launch agent; this needs approval.";
3685
+ return true;
3686
+ }
3687
+ }
3688
+ if (inv.program === "ssh-keygen") {
3689
+ why ??= "Changes ~/.ssh; this needs approval.";
3690
+ return true;
3691
+ }
3692
+ if (inv.program === "git" &&
3693
+ inv.args[0] === "config" &&
3694
+ hasArg(inv.args.slice(1), "--global", "--system")) {
3695
+ why ??= "Changes ~/.gitconfig; this needs approval.";
3696
+ return true;
3697
+ }
3698
+ return false;
3699
+ });
3700
+ for (const command of ctx.commands) {
3701
+ if (commandHasUnresolvedSecret(command, ctx)) {
3702
+ matched.push(command);
3703
+ why ??=
3704
+ "Changes or prints a shell rc file, SSH configuration or another secret/persistence location; this needs approval.";
3705
+ }
3706
+ const touched = [
3707
+ ...persistenceTokens(command, ctx.facts),
3708
+ ...secretTokens(command, ctx.facts),
3709
+ ];
3710
+ if (touched.length === 0)
3711
+ continue;
3712
+ const ownersOnly = eachInvocation([command], (inv) => isSecretOwner(inv.program, touched, ctx.facts))
3713
+ .length > 0;
3714
+ const credentials = credentialSecretTokens(command, ctx);
3715
+ if (!ownersOnly && !touched.every((path) => credentials.has(path))) {
3716
+ matched.push(command);
3717
+ why ??=
3718
+ "Changes or prints a shell rc file, SSH configuration or another secret/persistence location; this needs approval.";
3719
+ }
3720
+ }
3721
+ const keychain = eachInvocation(ctx.commands, (inv) => inv.program === "security" && inv.args.some((a) => KEYCHAIN_DUMP.test(a)));
3722
+ if (keychain.length > 0) {
3723
+ matched.push(...keychain);
3724
+ why ??= "Prints a macOS keychain item; this needs approval.";
3725
+ }
3726
+ return why === null ? null : { reason: why, commands: matched };
3727
+ },
3728
+ },
3729
+ {
3730
+ id: "ask.credential-print",
3731
+ outcome: "ask",
3732
+ check: (ctx) => {
3733
+ const matched = eachInvocation(ctx.commands, (inv) => credentialProducer(inv));
3734
+ return matched.length > 0
3735
+ ? {
3736
+ reason: "Prints environment variables or a stored credential (`env`, `printenv`, `set`, `export -p`, `gh auth token`, `gh auth status --show-token`, `ps e`, `launchctl getenv`, `git credential fill`); the output reaches the model, so this needs approval.",
3737
+ commands: matched,
3738
+ }
3739
+ : null;
3740
+ },
3741
+ },
3742
+ {
3743
+ id: "ask.sudo",
3744
+ outcome: "ask",
3745
+ check: (ctx) => {
3746
+ const matched = eachInvocation(ctx.commands, (inv) => ["sudo", "su", "doas", "pkexec"].includes(inv.program));
3747
+ return matched.length > 0
3748
+ ? {
3749
+ reason: "Runs a command with elevated privileges; this needs approval.",
3750
+ commands: matched,
3751
+ }
3752
+ : null;
3753
+ },
3754
+ },
3755
+ {
3756
+ id: "ask.global-install",
3757
+ outcome: "ask",
3758
+ check: (ctx) => {
3759
+ const matched = eachInvocation(ctx.commands, (inv) => globalInstallOf(inv.program, inv.args, ctx.venv || venvBinary(inv.raw)));
3760
+ return matched.length > 0
3761
+ ? {
3762
+ reason: "Installs a package outside the project; this needs approval.",
3763
+ commands: matched,
3764
+ }
3765
+ : null;
3766
+ },
3767
+ },
3768
+ {
3769
+ id: "ask.docker-privileged",
3770
+ outcome: "ask",
3771
+ check: (ctx) => {
3772
+ let why = null;
3773
+ const matched = eachInvocation(ctx.commands, (inv, command) => {
3774
+ if (inv.program !== "docker")
3775
+ return false;
3776
+ const reason = dockerPrivileged(inv.args, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3777
+ if (reason === null)
3778
+ return false;
3779
+ why ??= `Docker ${reason}; this needs approval.`;
3780
+ return true;
3781
+ });
3782
+ return why === null ? null : { reason: why, commands: matched };
3783
+ },
3784
+ },
3785
+ {
3786
+ id: "ask.kill-other",
3787
+ outcome: "ask",
3788
+ check: (ctx) => {
3789
+ const matched = eachInvocation(ctx.commands, (inv) => {
3790
+ if (inv.program === "pkill" || inv.program === "killall")
3791
+ return true;
3792
+ if (inv.program !== "kill")
3793
+ return false;
3794
+ const targets = nonOptionArgs(inv.args).filter((a) => !a.startsWith("-"));
3795
+ if (targets.length === 0)
3796
+ return false;
3797
+ return !targets.every((t) => t === "$$" || t === "$!" || /^%[A-Za-z0-9_-]+$/.test(t));
3798
+ });
3799
+ return matched.length > 0
3800
+ ? {
3801
+ reason: "Stops a process the device did not start; this needs approval.",
3802
+ commands: matched,
3803
+ }
3804
+ : null;
3805
+ },
3806
+ },
3807
+ {
3808
+ id: "ask.git-exec",
3809
+ outcome: "ask",
3810
+ check: (ctx) => {
3811
+ const matched = ctx.commands.filter((command) => gitRunsArbitraryProgram(command));
3812
+ return matched.length > 0
3813
+ ? {
3814
+ reason: "Makes git run another program through a config value, config write or environment override; this needs approval.",
3815
+ commands: matched,
3816
+ }
3817
+ : null;
3818
+ },
3819
+ },
3820
+ {
3821
+ id: "ask.dynamic-arg",
3822
+ outcome: "ask",
3823
+ check: (ctx) => {
3824
+ const matched = ctx.commands.filter((command) => dynamicArgumentAsks(command, ctx));
3825
+ return matched.length > 0
3826
+ ? {
3827
+ reason: "Uses a variable, substitution or quoted value as a file path that cannot be resolved statically; this needs approval.",
3828
+ commands: matched,
3829
+ }
3830
+ : null;
3831
+ },
3832
+ },
3833
+ {
3834
+ id: "deny.secret-exfil",
3835
+ outcome: "deny",
3836
+ check: (ctx) => {
3837
+ const direct = eachInvocation(ctx.commands, (inv, command) => {
3838
+ if (!NETWORK_PROGRAMS.has(inv.program))
3839
+ return false;
3840
+ if (networkSendsSecret(inv, command, ctx))
3841
+ return true;
3842
+ return command.redirects.some((redirect) => {
3843
+ if (redirect.op !== "<")
3844
+ return false;
3845
+ const resolved = expandPath(redirect.target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3846
+ return resolved === null || isSecretPath(resolved, ctx.facts);
3847
+ });
3848
+ });
3849
+ if (direct.length > 0) {
3850
+ return {
3851
+ reason: "Sends a secret file (an SSH key, cloud credentials or a .env) to a network command.",
3852
+ commands: direct,
3853
+ };
3854
+ }
3855
+ const matched = [];
3856
+ for (let index = 0; index < ctx.commands.length; index += 1) {
3857
+ const command = ctx.commands[index];
3858
+ const network = eachInvocation([command], (inv) => NETWORK_PROGRAMS.has(inv.program)).length > 0;
3859
+ if (!network)
3860
+ continue;
3861
+ // Only a secret read earlier in the line can be piped into this command.
3862
+ if (!secretFlowsInto(ctx, index))
3863
+ continue;
3864
+ matched.push(command);
3865
+ for (let source = 0; source < index; source += 1) {
3866
+ const other = ctx.commands[source];
3867
+ if (readsSecret(other, ctx))
3868
+ matched.push(other);
3869
+ }
3870
+ }
3871
+ return matched.length > 0
3872
+ ? {
3873
+ reason: "Pipes a secret file (an SSH key, cloud credentials or a .env) into a network command.",
3874
+ commands: matched,
3875
+ }
3876
+ : null;
3877
+ },
3878
+ },
3879
+ {
3880
+ id: "deny.root-delete",
3881
+ outcome: "deny",
3882
+ check: (ctx) => {
3883
+ const matched = eachInvocation(ctx.commands, (inv, command) => {
3884
+ if (inv.program !== "rm" || !rmIsRecursiveForce(inv.args))
3885
+ return false;
3886
+ for (const target of nonOptionArgs(inv.args)) {
3887
+ const resolved = expandPath(target, ctx.facts, cwdOf(command, ctx.facts), command.vars);
3888
+ if (resolved !== null && isDangerousDelete(resolved, ctx))
3889
+ return true;
3890
+ }
3891
+ return false;
3892
+ });
3893
+ return matched.length > 0
3894
+ ? {
3895
+ reason: "Deletes /, the home directory or a parent of the workspace with rm -rf.",
3896
+ commands: matched,
3897
+ }
3898
+ : null;
3899
+ },
3900
+ },
3901
+ {
3902
+ id: "deny.tooling-edit",
3903
+ outcome: "deny",
3904
+ check: (ctx) => {
3905
+ const matched = [];
3906
+ for (const command of ctx.commands) {
3907
+ if (deviceTokens(command, ctx.facts).length > 0 && modifiesDevice(command, ctx))
3908
+ matched.push(command);
3909
+ }
3910
+ matched.push(...eachInvocation(ctx.commands, (inv) => isSecurityToolDisable(inv) || isDeviceServiceCommand(inv)));
3911
+ return matched.length > 0
3912
+ ? {
3913
+ reason: "Edits Capybara's own device files or the device directory's marker, or disables an OS security tool.",
3914
+ commands: matched,
3915
+ }
3916
+ : null;
3917
+ },
3918
+ },
3919
+ {
3920
+ id: "unsure.unparsed",
3921
+ outcome: "unsure",
3922
+ check: (ctx) => ctx.unsure === null
3923
+ ? null
3924
+ : {
3925
+ reason: `The command cannot be parsed with confidence (${ctx.unsure}); it needs review.`,
3926
+ commands: [],
3927
+ },
3928
+ },
3929
+ ];
3930
+ /* -------------------------------------------------------------------------------------------- *
3931
+ * Cwd tracking and obfuscation checks
3932
+ * -------------------------------------------------------------------------------------------- */
3933
+ /**
3934
+ * Walk the chain and give each command its effective directory (a `cd` earlier in the line moves
3935
+ * the commands after it). A `cd` outside the workspace and tmp is `unsure`: the relative paths
3936
+ * after it cannot be checked against the workspace.
3937
+ */
3938
+ function assignCwds(commands, facts) {
3939
+ let cwd = facts.cwd;
3940
+ for (const command of commands) {
3941
+ command.cwd = cwd;
3942
+ const inv = stripNeutral(command.argv);
3943
+ if (inv === null)
3944
+ continue;
3945
+ if (inv.program === "pushd" || inv.program === "popd") {
3946
+ return { ok: false, unsure: "pushd/popd changes the directory" };
3947
+ }
3948
+ if (inv.program !== "cd")
3949
+ continue;
3950
+ const target = nonOptionArgs(inv.args)[0];
3951
+ if (target === undefined)
3952
+ return { ok: false, unsure: "cd without a target (the home directory)" };
3953
+ const resolved = expandPath(target, facts, cwd);
3954
+ if (resolved === null)
3955
+ return { ok: false, unsure: "cd into a directory that cannot be resolved" };
3956
+ if (!inside(resolved, facts.workspace) && !tmpRootOf(resolved)) {
3957
+ return { ok: false, unsure: "cd outside the workspace" };
3958
+ }
3959
+ cwd = resolved;
3960
+ }
3961
+ return { ok: true, value: commands };
3962
+ }
3963
+ const ASSIGNMENT = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/;
3964
+ /** A simple assignment value (`X=$HOME/.kube`), expanded with the variables seen so far. */
3965
+ function assignmentValue(raw, facts, vars) {
3966
+ let p = raw;
3967
+ if (p === "~" || p.startsWith("~/")) {
3968
+ if (facts.home !== null)
3969
+ p = facts.home + p.slice(1);
3970
+ }
3971
+ else {
3972
+ const homeRef = /^\$(?:\{HOME\}|HOME)(?=\/|$)/.exec(p);
3973
+ if (homeRef !== null) {
3974
+ const rest = p.slice(homeRef[0].length);
3975
+ p = facts.home === null ? `~${rest}` : facts.home + rest;
3976
+ }
3977
+ }
3978
+ const substituted = substituteVariables(p, vars);
3979
+ if (substituted === null || /[$`]/.test(substituted))
3980
+ return null;
3981
+ return substituted;
3982
+ }
3983
+ /**
3984
+ * Track simple assignments for the rule pass.
3985
+ *
3986
+ * What persists to later commands is only what a real shell persists: a standalone assignment
3987
+ * statement (`X=v`) and `export/declare/typeset X=v`. A leading `X=v` environment prefix on a
3988
+ * command (`X=/tmp cmd ...`) neither changes the shell's variables nor the expansions of that same
3989
+ * command: the shell expands the command words before it runs the command, and the assignment only
3990
+ * reaches that command's environment. So a variable a prefix shadows is unresolved for this
3991
+ * command's expansions (fail closed instead of trusting the prefix value), and no prefix is ever
3992
+ * written to the persistent table. Each command snapshots the table visible to it (with `PWD`).
3993
+ */
3994
+ function assignVariables(commands, facts) {
3995
+ const vars = new Map();
3996
+ for (const command of commands) {
3997
+ const cwd = command.cwd ?? facts.cwd;
3998
+ vars.set("PWD", cwd);
3999
+ const standalone = command.argv.length > 0 && command.argv.every((t) => ASSIGNMENT.test(t));
4000
+ if (standalone) {
4001
+ for (const token of command.argv) {
4002
+ const match = ASSIGNMENT.exec(token);
4003
+ if (match === null)
4004
+ continue;
4005
+ vars.set(match[1], assignmentValue(match[2] ?? "", facts, vars));
4006
+ }
4007
+ }
4008
+ const inv = stripNeutral(command.argv);
4009
+ if (inv !== null && ["export", "declare", "typeset"].includes(inv.program)) {
4010
+ for (const arg of inv.args) {
4011
+ const match = ASSIGNMENT.exec(arg);
4012
+ if (match === null)
4013
+ continue;
4014
+ vars.set(match[1], assignmentValue(match[2] ?? "", facts, vars));
4015
+ }
4016
+ }
4017
+ // Leading `X=v` prefixes of this command: unresolved here, not persisted.
4018
+ const snapshot = new Map(vars);
4019
+ for (const token of command.argv) {
4020
+ const match = ASSIGNMENT.exec(token);
4021
+ if (match === null)
4022
+ break;
4023
+ snapshot.set(match[1], null);
4024
+ }
4025
+ command.vars = snapshot;
4026
+ }
4027
+ }
4028
+ function rejectObfuscation(commands) {
4029
+ const byPipeline = new Map();
4030
+ for (const command of commands) {
4031
+ const list = byPipeline.get(command.pipeline) ?? [];
4032
+ list.push(command);
4033
+ byPipeline.set(command.pipeline, list);
4034
+ }
4035
+ for (const list of byPipeline.values()) {
4036
+ for (let i = 0; i < list.length; i += 1) {
4037
+ const inv = stripNeutral(list[i].argv);
4038
+ if (inv === null || !["base64", "base32", "xxd", "uudecode"].includes(inv.program))
4039
+ continue;
4040
+ for (let j = i + 1; j < list.length; j += 1) {
4041
+ const later = stripNeutral(list[j].argv);
4042
+ if (later === null)
4043
+ continue;
4044
+ if (SHELL_PROGRAMS.has(later.program) ||
4045
+ ["python", "python3", "node", "perl", "ruby", "php", "eval"].includes(later.program)) {
4046
+ return { ok: false, unsure: "base64 fed into an interpreter" };
4047
+ }
4048
+ }
4049
+ }
4050
+ // A pipeline into an interpreter (`curl ... | bash`): the fed text cannot be inspected.
4051
+ if (list.length >= 2) {
4052
+ const first = stripNeutral(list[0].argv);
4053
+ const last = stripNeutral(list[list.length - 1].argv);
4054
+ if (first !== null && last !== null) {
4055
+ const interpreter = SHELL_PROGRAMS.has(last.program) ||
4056
+ ["python", "python3", "node", "perl", "ruby", "php", "eval"].includes(last.program);
4057
+ if (interpreter && first.program !== last.program) {
4058
+ return { ok: false, unsure: "a pipeline into an interpreter" };
4059
+ }
4060
+ }
4061
+ }
4062
+ }
4063
+ return { ok: true, value: commands };
4064
+ }
4065
+ /** Prefixes that are never stripped: the command behind them is not trusted by prefix. */
4066
+ const NEVER_PREFIX = new Set(["watch", "setsid", "flock", "ionice"]);
4067
+ function rejectDynamicPaths(commands) {
4068
+ const writePrograms = [
4069
+ "rm",
4070
+ "mv",
4071
+ "cp",
4072
+ "install",
4073
+ "ln",
4074
+ "touch",
4075
+ "truncate",
4076
+ "tee",
4077
+ "mkdir",
4078
+ "rmdir",
4079
+ "chmod",
4080
+ "chown",
4081
+ "shred",
4082
+ ];
4083
+ for (const command of commands) {
4084
+ for (const redirect of command.redirects) {
4085
+ if (isUnresolvedDynamic(redirect.target))
4086
+ return { ok: false, unsure: "dynamic redirection target" };
4087
+ }
4088
+ const inv = stripNeutral(command.argv);
4089
+ if (inv === null)
4090
+ continue;
4091
+ if (RESERVED_WORDS.has(inv.program))
4092
+ return { ok: false, unsure: "a shell control structure" };
4093
+ if (isUnresolvedDynamic(inv.raw))
4094
+ return { ok: false, unsure: "dynamic command" };
4095
+ const outer = command.argv[0] === undefined ? "" : baseName(command.argv[0]);
4096
+ if (NEVER_PREFIX.has(outer)) {
4097
+ return { ok: false, unsure: `the ${outer} prefix is never trusted` };
4098
+ }
4099
+ if (outer === "xargs" &&
4100
+ writePrograms.includes(inv.program) &&
4101
+ nonOptionArgs(inv.args).length === 0) {
4102
+ return { ok: false, unsure: "xargs takes the paths from stdin" };
4103
+ }
4104
+ if (writePrograms.includes(inv.program)) {
4105
+ for (const target of nonOptionArgs(inv.args)) {
4106
+ if (isUnresolvedDynamic(target))
4107
+ return { ok: false, unsure: "dynamic path argument" };
4108
+ }
4109
+ }
4110
+ if (inv.program === "dd") {
4111
+ for (const a of inv.args) {
4112
+ if (a.startsWith("of=") && isUnresolvedDynamic(a.slice(3))) {
4113
+ return { ok: false, unsure: "dynamic path argument" };
4114
+ }
4115
+ }
4116
+ }
4117
+ }
4118
+ return { ok: true, value: commands };
4119
+ }
4120
+ function isUnresolvedDynamic(token) {
4121
+ if (token.includes("{}"))
4122
+ return true; // xargs' placeholder: the real path comes from stdin
4123
+ const withoutHome = token
4124
+ .replace(/\$\{HOME\}/g, "")
4125
+ .replace(/\$HOME/g, "")
4126
+ .replace(/~/g, "");
4127
+ return /[$`]/.test(withoutHome);
4128
+ }
4129
+ /* -------------------------------------------------------------------------------------------- *
4130
+ * Public entry points
4131
+ * -------------------------------------------------------------------------------------------- */
4132
+ const RANKS = { allow: 0, unsure: 1, ask: 2, deny: 3 };
4133
+ function strictest(hits) {
4134
+ let outcome = "allow";
4135
+ for (const hit of hits)
4136
+ if (RANKS[hit.outcome] > RANKS[outcome])
4137
+ outcome = hit.outcome;
4138
+ return outcome;
4139
+ }
4140
+ /**
4141
+ * The rule table over a command line: the strictest outcome and every hit, in rule order. A parse
4142
+ * the tokenizer cannot make confidently yields only `unsure.unparsed`; a command no rule covers
4143
+ * adds `unsure.uncovered`.
4144
+ */
4145
+ export function reviewRules(command, facts) {
4146
+ const resolved = resolveFacts(facts);
4147
+ const parsed = parseDetailed(command);
4148
+ let commands = [];
4149
+ let unsure = parsed.ok ? null : parsed.unsure;
4150
+ if (parsed.ok) {
4151
+ const withCwds = assignCwds(parsed.value, resolved);
4152
+ if (withCwds.ok) {
4153
+ commands = withCwds.value;
4154
+ assignVariables(commands, resolved);
4155
+ }
4156
+ else
4157
+ unsure = withCwds.unsure;
4158
+ }
4159
+ const ctx = {
4160
+ facts: resolved,
4161
+ commands,
4162
+ unsure,
4163
+ venv: commandLineActivatesVenv(commands),
4164
+ };
4165
+ const hits = [];
4166
+ const covered = new Set();
4167
+ for (const rule of RULES) {
4168
+ const match = rule.check(ctx);
4169
+ if (match === null)
4170
+ continue;
4171
+ hits.push({ id: rule.id, outcome: rule.outcome, reason: match.reason });
4172
+ for (const command of match.covered ?? match.commands)
4173
+ covered.add(command);
4174
+ }
4175
+ const uncovered = commands.filter((command) => command.argv.length > 0 && !covered.has(command));
4176
+ if (uncovered.length > 0) {
4177
+ hits.push({
4178
+ id: "unsure.uncovered",
4179
+ outcome: "unsure",
4180
+ reason: "No rule covers every command in this line; it needs review.",
4181
+ });
4182
+ }
4183
+ if (hits.length === 0) {
4184
+ hits.push({
4185
+ id: "unsure.uncovered",
4186
+ outcome: "unsure",
4187
+ reason: "No rule covers this command; it needs review.",
4188
+ });
4189
+ }
4190
+ return { outcome: strictest(hits), hits };
4191
+ }
4192
+ /**
4193
+ * The same table for a file-tool write: workspace and tmp writes pass; the device's own files are
4194
+ * denied; git hooks, shell rc files and secret files ask; anything else outside asks to write
4195
+ * outside.
4196
+ */
4197
+ export function reviewWrite(path, facts) {
4198
+ const resolved = resolveFacts(facts);
4199
+ const absolute = expandPath(path, resolved);
4200
+ if (absolute === null) {
4201
+ return {
4202
+ outcome: "unsure",
4203
+ hits: [
4204
+ {
4205
+ id: "unsure.uncovered",
4206
+ outcome: "unsure",
4207
+ reason: "The path cannot be resolved statically; it needs review.",
4208
+ },
4209
+ ],
4210
+ };
4211
+ }
4212
+ if (isDevicePath(absolute, resolved)) {
4213
+ return {
4214
+ outcome: "deny",
4215
+ hits: [
4216
+ {
4217
+ id: "deny.tooling-edit",
4218
+ outcome: "deny",
4219
+ reason: "Edits Capybara's own device files or the device directory's marker.",
4220
+ },
4221
+ ],
4222
+ };
4223
+ }
4224
+ if (isGitHook(absolute)) {
4225
+ return {
4226
+ outcome: "ask",
4227
+ hits: [
4228
+ { id: "ask.rc-files", outcome: "ask", reason: "Changes a git hook; this needs approval." },
4229
+ ],
4230
+ };
4231
+ }
4232
+ if (inside(absolute, resolved.workspace)) {
4233
+ return {
4234
+ outcome: "allow",
4235
+ hits: [
4236
+ {
4237
+ id: "allow.workspace-write",
4238
+ outcome: "allow",
4239
+ reason: "Writes inside the workspace; this is allowed.",
4240
+ },
4241
+ ],
4242
+ };
4243
+ }
4244
+ if (tmpRootOf(absolute)) {
4245
+ return {
4246
+ outcome: "allow",
4247
+ hits: [
4248
+ {
4249
+ id: "allow.tmp-write",
4250
+ outcome: "allow",
4251
+ reason: "Writes to the device's temporary files; this is allowed.",
4252
+ },
4253
+ ],
4254
+ };
4255
+ }
4256
+ if (isPersistencePath(absolute, resolved) || isSecretPath(absolute, resolved)) {
4257
+ return {
4258
+ outcome: "ask",
4259
+ hits: [
4260
+ {
4261
+ id: "ask.rc-files",
4262
+ outcome: "ask",
4263
+ reason: "Changes a shell rc file, SSH configuration or another secret/persistence location; this needs approval.",
4264
+ },
4265
+ ],
4266
+ };
4267
+ }
4268
+ return {
4269
+ outcome: "ask",
4270
+ hits: [
4271
+ {
4272
+ id: "ask.outside-write",
4273
+ outcome: "ask",
4274
+ reason: `Writes ${path} outside the workspace and temporary files; this needs approval.`,
4275
+ },
4276
+ ],
4277
+ };
4278
+ }