@cnc-tools/storm 0.16.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. package/dist/bin/storm.js +54 -40
  2. package/dist/build-info.js +1 -1
  3. package/dist/chunks/{chunk-VL7WFTVQ.js → chunk-24UYAK4R.js} +465 -365
  4. package/dist/chunks/{chunk-DC2CDHNK.js → chunk-2JMWUKH3.js} +2074 -473
  5. package/dist/chunks/{chunk-FOZWA52V.js → chunk-3VXZONGE.js} +481 -376
  6. package/dist/chunks/{chunk-ZG3WQ4UF.js → chunk-55O4FCN4.js} +1 -1
  7. package/dist/chunks/{chunk-CN4EFA5J.js → chunk-72ZFEMAW.js} +1 -1
  8. package/dist/chunks/chunk-AS37D43E.js +2173 -0
  9. package/dist/chunks/chunk-CQAY6GLP.js +1260 -0
  10. package/dist/chunks/chunk-DZLU7BYK.js +340 -0
  11. package/dist/chunks/{chunk-R7XVBZOY.js → chunk-EEENLQHE.js} +59 -11
  12. package/dist/chunks/{chunk-TJNFVNKR.js → chunk-EI3XDCEI.js} +1 -1
  13. package/dist/chunks/{chunk-HVNK3I45.js → chunk-FDUY3BNF.js} +1 -1
  14. package/dist/chunks/{chunk-FCQUJDA4.js → chunk-GUCCYCIO.js} +4 -189
  15. package/dist/chunks/{chunk-OIY3DMF2.js → chunk-HBI4LEMH.js} +3 -3
  16. package/dist/chunks/{chunk-2MAD5NKK.js → chunk-K4MDQPLW.js} +1 -18
  17. package/dist/chunks/chunk-KK7PAWIZ.js +198 -0
  18. package/dist/chunks/{chunk-G3PHMXH5.js → chunk-KQ5FXRUD.js} +18 -1
  19. package/dist/chunks/{chunk-5VXBKPW6.js → chunk-LKXHGGGX.js} +1 -1
  20. package/dist/chunks/{chunk-P357TZAY.js → chunk-LOGHIUUB.js} +21 -18
  21. package/dist/chunks/{chunk-CK2GZEPK.js → chunk-OEPFINFT.js} +2 -2
  22. package/dist/chunks/{chunk-DWEAHX4T.js → chunk-OI4AP6Z7.js} +5 -5
  23. package/dist/chunks/{chunk-V4IKA3WI.js → chunk-PMBQWVTW.js} +26 -19
  24. package/dist/chunks/chunk-SZMYIIG2.js +88 -0
  25. package/dist/chunks/{chunk-5CCP36QQ.js → chunk-TXCOFC7N.js} +1 -1
  26. package/dist/chunks/{chunk-VUPZH7J2.js → chunk-UXD3TVWM.js} +1 -1
  27. package/dist/chunks/{chunk-3L52KNQA.js → chunk-WNW2QXQK.js} +8 -8
  28. package/dist/chunks/{chunk-AWBSONHB.js → chunk-XBDRVFEV.js} +55 -16
  29. package/dist/chunks/{live-app-WECQOQ4C.js → live-app-UY7BEDUN.js} +17 -13
  30. package/dist/cli.d.ts +28 -0
  31. package/dist/cli.js +64 -0
  32. package/dist/commands/exec/across-sandboxes-outcomes.d.ts +2 -2
  33. package/dist/commands/exec/across-sandboxes-outcomes.js +3 -3
  34. package/dist/commands/exec/before-run.d.ts +123 -0
  35. package/dist/commands/exec/before-run.js +105 -0
  36. package/dist/commands/exec/command.d.ts +67 -14
  37. package/dist/commands/exec/command.js +31 -95
  38. package/dist/commands/exec/driver-plan.d.ts +9 -4
  39. package/dist/commands/exec/driver-plan.js +8 -8
  40. package/dist/commands/exec/exec-words.js +1 -1
  41. package/dist/commands/exec/execute-run.d.ts +39 -0
  42. package/dist/commands/exec/execute-run.js +325 -0
  43. package/dist/commands/exec/live-view.d.ts +3 -3
  44. package/dist/commands/exec/live-view.js +16 -15
  45. package/dist/commands/exec/metadata.js +1 -1
  46. package/dist/commands/exec/mint-failed.d.ts +38 -0
  47. package/dist/commands/exec/mint-failed.js +27 -4
  48. package/dist/commands/exec/outcomes.d.ts +104 -22
  49. package/dist/commands/exec/outcomes.js +73 -26
  50. package/dist/commands/exec/plan-run.d.ts +58 -150
  51. package/dist/commands/exec/plan-run.js +239 -90
  52. package/dist/commands/exec/progress.d.ts +10 -6
  53. package/dist/commands/exec/progress.js +38 -25
  54. package/dist/commands/exec/run-bin.d.ts +11 -11
  55. package/dist/commands/exec/run-bin.js +9 -9
  56. package/dist/commands/exec/run-flags.d.ts +69 -0
  57. package/dist/commands/exec/run-flags.js +58 -0
  58. package/dist/commands/exec/run-metadata.js +4 -4
  59. package/dist/commands/exec/run-summary.d.ts +58 -0
  60. package/dist/commands/exec/run-summary.js +205 -0
  61. package/dist/commands/exec/runner-refusals.d.ts +82 -0
  62. package/dist/commands/exec/runner-refusals.js +71 -0
  63. package/dist/commands/exec/sandbox-progress.js +3 -0
  64. package/dist/commands/exec/stopped-outcome.d.ts +12 -0
  65. package/dist/commands/exec/stopped-outcome.js +77 -0
  66. package/dist/commands/exec/view.d.ts +20 -11
  67. package/dist/commands/exec/view.js +103 -47
  68. package/dist/commands/runs/facts.d.ts +8 -4
  69. package/dist/commands/runs/facts.js +19 -10
  70. package/dist/commands/runs/list/command.d.ts +16 -8
  71. package/dist/commands/runs/list/command.js +10 -10
  72. package/dist/commands/runs/list/outcomes.d.ts +8 -4
  73. package/dist/commands/runs/logs/command.js +10 -10
  74. package/dist/commands/runs/show/command.d.ts +16 -8
  75. package/dist/commands/runs/show/command.js +31 -29
  76. package/dist/commands/runs/show/metadata.js +1 -1
  77. package/dist/commands/runs/show/outcomes.d.ts +8 -4
  78. package/dist/commands/runs/show/outcomes.js +8 -6
  79. package/dist/commands/snapshots/access.d.ts +111 -0
  80. package/dist/commands/snapshots/access.js +96 -0
  81. package/dist/commands/snapshots/build/command.d.ts +376 -101
  82. package/dist/commands/snapshots/build/command.js +447 -149
  83. package/dist/commands/snapshots/build/metadata.d.ts +10 -0
  84. package/dist/commands/snapshots/build/metadata.js +31 -11
  85. package/dist/commands/snapshots/build/outcomes.d.ts +242 -47
  86. package/dist/commands/snapshots/build/outcomes.js +200 -36
  87. package/dist/commands/snapshots/list/command.d.ts +368 -27
  88. package/dist/commands/snapshots/list/command.js +298 -68
  89. package/dist/commands/snapshots/list/metadata.d.ts +16 -0
  90. package/dist/commands/snapshots/list/metadata.js +29 -6
  91. package/dist/commands/snapshots/list/outcomes.d.ts +247 -15
  92. package/dist/commands/snapshots/list/outcomes.js +223 -52
  93. package/dist/commands/snapshots/metadata.js +1 -1
  94. package/dist/commands/snapshots/prune/command.d.ts +430 -0
  95. package/dist/commands/snapshots/prune/command.js +161 -0
  96. package/dist/commands/snapshots/prune/metadata.d.ts +28 -0
  97. package/dist/commands/snapshots/prune/metadata.js +39 -0
  98. package/dist/commands/snapshots/prune/outcomes.d.ts +208 -0
  99. package/dist/commands/snapshots/prune/outcomes.js +78 -0
  100. package/dist/commands/suites/list/command.js +17 -17
  101. package/dist/commands/suites/list/outcomes.js +2 -2
  102. package/dist/commands/test/command.d.ts +67 -14
  103. package/dist/commands/test/command.js +49 -252
  104. package/dist/commands/test/outcomes.d.ts +33 -7
  105. package/dist/config/check-runner.d.ts +3 -3
  106. package/dist/config/check-runner.js +8 -7
  107. package/dist/config/credential-resolver.d.ts +50 -5
  108. package/dist/config/credential-resolver.js +51 -19
  109. package/dist/config/credential-settle.d.ts +6 -2
  110. package/dist/config/credential-settle.js +11 -5
  111. package/dist/config/index.d.ts +2 -2
  112. package/dist/config/index.js +1 -1
  113. package/dist/config/runner.d.ts +5 -5
  114. package/dist/config/runner.js +1 -1
  115. package/dist/config/schema.d.ts +2 -2
  116. package/dist/config/schema.js +1 -1
  117. package/dist/config-file/define.d.ts +1 -1
  118. package/dist/config-file/define.js +1 -1
  119. package/dist/config-file/runner.d.ts +5 -5
  120. package/dist/config-file/toolchain.d.ts +14 -4
  121. package/dist/config-file/toolchain.js +43 -7
  122. package/dist/context.d.ts +10 -6
  123. package/dist/context.js +8 -6
  124. package/dist/core/across-sandboxes.d.ts +7 -6
  125. package/dist/core/across-sandboxes.js +123 -152
  126. package/dist/core/coordinator.d.ts +166 -0
  127. package/dist/core/coordinator.js +233 -0
  128. package/dist/core/events.d.ts +2 -2
  129. package/dist/core/index.d.ts +4 -2
  130. package/dist/core/index.js +1 -1
  131. package/dist/core/outcomes.d.ts +148 -0
  132. package/dist/core/{sandbox-end.js → outcomes.js} +28 -4
  133. package/dist/core/resources.d.ts +31 -53
  134. package/dist/core/resources.js +32 -70
  135. package/dist/core/run-state.d.ts +7 -1
  136. package/dist/core/run-state.js +2 -0
  137. package/dist/core/run.d.ts +32 -236
  138. package/dist/core/run.js +107 -267
  139. package/dist/core/state-directory.d.ts +1 -1
  140. package/dist/core/state-directory.js +2 -2
  141. package/dist/credential/gateways.d.ts +15 -1
  142. package/dist/credential/index.d.ts +4 -3
  143. package/dist/credential/index.js +3 -2
  144. package/dist/credential/live/vercel-cli-gateway.d.ts +1 -1
  145. package/dist/credential/live/vercel-cli-gateway.js +1 -1
  146. package/dist/credential/live/vercel-oidc-token-gateway.d.ts +11 -0
  147. package/dist/credential/live/vercel-oidc-token-gateway.js +16 -0
  148. package/dist/credential/source.d.ts +73 -6
  149. package/dist/credential/source.js +55 -2
  150. package/dist/extension-commands.d.ts +1 -1
  151. package/dist/extension-commands.js +2 -2
  152. package/dist/extensions/testserver/commands/pool/create/command.d.ts +35 -2
  153. package/dist/extensions/testserver/commands/pool/create/command.js +7 -7
  154. package/dist/extensions/testserver/commands/pool/create/metadata.js +5 -5
  155. package/dist/extensions/testserver/commands/pool/create/outcomes.d.ts +17 -1
  156. package/dist/extensions/testserver/commands/pool/list/command.js +4 -4
  157. package/dist/extensions/testserver/credential-facts.d.ts +6 -2
  158. package/dist/extensions/testserver/credential-facts.js +29 -11
  159. package/dist/extensions/testserver/lifecycle.js +7 -1
  160. package/dist/extensions/testserver/limit.d.ts +3 -0
  161. package/dist/extensions/testserver/limit.js +3 -0
  162. package/dist/extensions/testserver/provision.d.ts +8 -1
  163. package/dist/extensions/testserver/provision.js +8 -3
  164. package/dist/extensions/testserver/refusals.d.ts +6 -1
  165. package/dist/extensions/testserver/refusals.js +16 -1
  166. package/dist/extensions/testserver/resource.d.ts +1 -1
  167. package/dist/extensions/testserver/resource.js +1 -1
  168. package/dist/extensions/testserver/result.d.ts +1 -1
  169. package/dist/extensions/testserver/result.js +9 -4
  170. package/dist/planning/driver-file-lists.d.ts +0 -15
  171. package/dist/planning/driver-file-lists.js +2 -21
  172. package/dist/planning/plan-only.d.ts +3 -14
  173. package/dist/planning/plan-only.js +11 -20
  174. package/dist/planning/run-kind-flags.js +2 -2
  175. package/dist/planning/run-plan.d.ts +45 -12
  176. package/dist/planning/run-plan.js +4 -3
  177. package/dist/planning/suites.d.ts +1 -1
  178. package/dist/planning/suites.js +2 -2
  179. package/dist/planning/test-plan.d.ts +9 -0
  180. package/dist/planning/test-plan.js +9 -0
  181. package/dist/run-directory/recording.js +3 -3
  182. package/dist/run-directory/run-record.d.ts +26 -11
  183. package/dist/run-directory/run-record.js +16 -13
  184. package/dist/runner/base.js +1 -1
  185. package/dist/runner/build-snapshots.d.ts +60 -29
  186. package/dist/runner/build-snapshots.js +136 -29
  187. package/dist/runner/context.d.ts +15 -7
  188. package/dist/runner/context.js +19 -6
  189. package/dist/runner/events.d.ts +16 -10
  190. package/dist/runner/gateways.d.ts +122 -23
  191. package/dist/runner/gateways.js +1 -1
  192. package/dist/runner/gh.d.ts +42 -0
  193. package/dist/runner/gh.js +58 -0
  194. package/dist/runner/index.d.ts +13 -8
  195. package/dist/runner/index.js +12 -6
  196. package/dist/runner/live/checkout-gateway.js +7 -0
  197. package/dist/runner/live/gh-output.d.ts +17 -0
  198. package/dist/runner/live/gh-output.js +37 -0
  199. package/dist/runner/live/gh-token-gateway.d.ts +13 -0
  200. package/dist/runner/live/gh-token-gateway.js +51 -0
  201. package/dist/runner/live/sandbox-errors.d.ts +2 -0
  202. package/dist/runner/live/sandbox-errors.js +4 -0
  203. package/dist/runner/live/sandbox-gateway.js +140 -4
  204. package/dist/runner/live/snapshot-index-gateway.d.ts +12 -0
  205. package/dist/runner/live/{snapshot-cache-gateway.js → snapshot-index-gateway.js} +11 -11
  206. package/dist/runner/live/snapshot-records.js +12 -12
  207. package/dist/runner/local-snapshot-index.d.ts +205 -0
  208. package/dist/runner/local-snapshot-index.js +210 -0
  209. package/dist/runner/network-policy.d.ts +6 -6
  210. package/dist/runner/network-policy.js +7 -7
  211. package/dist/runner/policy-writes.d.ts +1 -1
  212. package/dist/runner/policy-writes.js +1 -1
  213. package/dist/runner/prepare.d.ts +14 -14
  214. package/dist/runner/prepare.js +20 -11
  215. package/dist/runner/ready.d.ts +19 -11
  216. package/dist/runner/ready.js +104 -33
  217. package/dist/runner/sandboxed-command.d.ts +3 -3
  218. package/dist/runner/sandboxed-command.js +3 -3
  219. package/dist/runner/scope.d.ts +15 -8
  220. package/dist/runner/scope.js +16 -5
  221. package/dist/runner/secrets.d.ts +41 -18
  222. package/dist/runner/secrets.js +226 -30
  223. package/dist/runner/session.d.ts +8 -8
  224. package/dist/runner/session.js +6 -3
  225. package/dist/runner/shared-snapshot-entry.d.ts +92 -0
  226. package/dist/runner/shared-snapshot-entry.js +168 -0
  227. package/dist/runner/shared-snapshot-list.d.ts +81 -0
  228. package/dist/runner/shared-snapshot-list.js +155 -0
  229. package/dist/runner/shared-snapshot-lookup.d.ts +51 -0
  230. package/dist/runner/shared-snapshot-lookup.js +118 -0
  231. package/dist/runner/shared-snapshot-prune.d.ts +58 -0
  232. package/dist/runner/shared-snapshot-prune.js +133 -0
  233. package/dist/runner/snapshot-build.d.ts +42 -14
  234. package/dist/runner/snapshot-build.js +79 -47
  235. package/dist/runner/snapshot-choice.js +2 -1
  236. package/dist/runner/snapshot-inspect.d.ts +39 -40
  237. package/dist/runner/snapshot-inspect.js +52 -35
  238. package/dist/runner/snapshot-key.d.ts +32 -17
  239. package/dist/runner/snapshot-key.js +43 -18
  240. package/dist/runner/snapshot-source.d.ts +31 -0
  241. package/dist/runner/snapshot-source.js +122 -0
  242. package/dist/runner/steps.d.ts +1 -1
  243. package/dist/runner/steps.js +1 -1
  244. package/dist/runner/{recipe-check.d.ts → vercel-sandbox-check.d.ts} +4 -2
  245. package/dist/runner/{recipe-check.js → vercel-sandbox-check.js} +7 -2
  246. package/dist/runner/{recipe.d.ts → vercel-sandbox-options.d.ts} +59 -28
  247. package/dist/runner/{recipe.js → vercel-sandbox-options.js} +128 -84
  248. package/dist/runner/vercel-sandbox-secrets.d.ts +116 -0
  249. package/dist/runner/{recipe-secrets.js → vercel-sandbox-secrets.js} +159 -26
  250. package/dist/sdk/extension/credential-diagnosis.d.ts +3 -1
  251. package/dist/sdk/extension/credential-diagnosis.js +25 -5
  252. package/dist/sdk/extension/credential-source.d.ts +20 -3
  253. package/dist/sdk/extension/credential-source.js +4 -4
  254. package/dist/sdk/extension/index.d.ts +3 -2
  255. package/dist/sdk/extension/index.js +2 -1
  256. package/dist/sdk/extension/resource-definition.d.ts +1 -1
  257. package/dist/sdk/extension/secret-source.d.ts +1 -1
  258. package/dist/sdk/extension/secret-source.js +1 -1
  259. package/dist/sdk/extension/vercel-oidc-facts.d.ts +20 -0
  260. package/dist/sdk/extension/vercel-oidc-facts.js +20 -0
  261. package/dist/shared/cleanup.d.ts +3 -3
  262. package/dist/shared/cleanup.js +15 -5
  263. package/dist/shared/snapshot-text.d.ts +4 -4
  264. package/dist/shared/snapshot-text.js +11 -11
  265. package/dist/time/duration.js +1 -1
  266. package/package.json +5 -3
  267. package/skills/overview.md +4 -4
  268. package/skills/plan-runs.md +1 -1
  269. package/skills/read-run-output.md +6 -6
  270. package/dist/application.d.ts +0 -21
  271. package/dist/application.js +0 -30
  272. package/dist/chunks/chunk-7EENF73W.js +0 -782
  273. package/dist/chunks/chunk-Z5PBVG3Y.js +0 -2599
  274. package/dist/commands/exec/across-sandboxes-run.d.ts +0 -180
  275. package/dist/commands/exec/across-sandboxes-run.js +0 -339
  276. package/dist/commands/exec/plan-only.d.ts +0 -10
  277. package/dist/commands/exec/plan-only.js +0 -45
  278. package/dist/commands/exec/run-command.d.ts +0 -475
  279. package/dist/commands/exec/run-command.js +0 -603
  280. package/dist/commands/exec/run-planned.d.ts +0 -20
  281. package/dist/commands/exec/run-planned.js +0 -22
  282. package/dist/commands/exec/sandbox-view.d.ts +0 -19
  283. package/dist/commands/exec/sandbox-view.js +0 -72
  284. package/dist/commands/test/plan.d.ts +0 -92
  285. package/dist/commands/test/plan.js +0 -125
  286. package/dist/core/sandbox-end.d.ts +0 -64
  287. package/dist/runner/live/snapshot-cache-gateway.d.ts +0 -12
  288. package/dist/runner/recipe-secrets.d.ts +0 -67
  289. package/dist/runner/snapshot-cache.d.ts +0 -148
  290. package/dist/runner/snapshot-cache.js +0 -126
@@ -1,55 +1,219 @@
1
- // The values of the recipe's credentials and unsafe secret environment variables: read from each entry's secret source
2
- // on this machine as the run settles, turned into the sandbox credentials the Sandbox firewall adds to the requests of
1
+ // The values of the runner's credentials and unsafe secret environment variables: read from each entry's source on
2
+ // this machine as the run settles, turned into the sandbox credentials the Sandbox firewall adds to the requests of
3
3
  // everything the run does in a sandbox (the steps and the command, for `credentials`) or passed into the command's
4
4
  // environment alone (for `unsafeSecretEnvVars`), and hidden as `<token>` in what the run prints and writes (`README.md`).
5
5
  import { describeSecretSource, readSecretSource, readSecretSourceValue, SECRET_MIN_LENGTH, secretSourceId, } from "../sdk/extension/index.js";
6
+ import { frameGhToken, ghEnvRedactionForms, resolveGhFormat, resolveGhHost } from "./gh.js";
6
7
  export { SECRET_MIN_LENGTH };
8
+ /** Machine-readable problem codes for `readCredentials` (`credentials_invalid`). */
9
+ export const CREDENTIAL_PROBLEM_KINDS = [
10
+ "missing",
11
+ "multiline",
12
+ "short",
13
+ "gh-not-installed",
14
+ "gh-not-authenticated",
15
+ "gh-timed-out",
16
+ "gh-failed",
17
+ "gh-preframed",
18
+ ];
7
19
  /**
8
- * The value of each credential and unsafe secret environment variable `recipe` declares that `env` sets, whatever a
9
- * run would say of it: for a command that hides the values in what it prints but needs none, such as `storm snapshots
10
- * list` or `storm snapshots build`. A short or multiline value is kept, since hiding it there is safer than showing it.
20
+ * The value of each env-backed credential and unsafe secret environment variable `options` declares that `env` sets,
21
+ * whatever a run would say of it: for commands and pure helpers that hide set environment values without doing file
22
+ * I/O or starting subprocesses, such as `storm snapshots list`, `storm snapshots build` (before reading credentials),
23
+ * and `storm test`'s pre-run line. A short or multiline value is kept, since hiding it there is safer than showing it.
11
24
  */
12
- export function declaredSecretValues(recipe, env) {
13
- if (recipe === undefined)
25
+ export function declaredSecretValues(options, env) {
26
+ if (options === undefined)
14
27
  return [];
15
28
  const seen = new Set();
16
29
  const values = [];
17
- for (const { source } of [...recipe.credentials, ...recipe.unsafeSecretEnvVars]) {
18
- const id = secretSourceId(source);
19
- if (seen.has(id))
20
- continue;
21
- seen.add(id);
30
+ const add = (value) => {
31
+ if (value.length === 0 || seen.has(value))
32
+ return;
33
+ seen.add(value);
34
+ values.push(value);
35
+ };
36
+ for (const { source } of options.credentials) {
37
+ switch (source.kind) {
38
+ case "env": {
39
+ const value = readSecretSourceValue(source, env);
40
+ if (value !== undefined)
41
+ add(value);
42
+ break;
43
+ }
44
+ case "gh": {
45
+ if (source.env !== undefined) {
46
+ const raw = ownNonEmpty(env, source.env);
47
+ if (raw !== undefined) {
48
+ for (const form of ghEnvRedactionForms(raw))
49
+ add(form);
50
+ }
51
+ }
52
+ break;
53
+ }
54
+ }
55
+ }
56
+ for (const { source } of options.unsafeSecretEnvVars) {
22
57
  const value = readSecretSourceValue(source, env);
23
58
  if (value !== undefined)
24
- values.push(value);
59
+ add(value);
25
60
  }
26
61
  return values;
27
62
  }
28
63
  /**
29
- * Reads each of `recipe`'s credentials' values from `env`, the environment of the machine the run starts on, through
30
- * `readSecretSource`: each `{ kind: "env", name }` source holds the whole header value, such as `Bearer …`. It answers
31
- * the sandbox credentials the recipe's steps and the command run under, one per declared credential, in the recipe's
32
- * order, each with its domain, header, and `requests` from the recipe. A source's problem is the first that holds of
33
- * `missing`, `multiline`, and `short`, and a source two credentials share is read and reported once in first-seen
34
- * order. A value is used as it is, never trimmed.
64
+ * Reads each of `options`'s credentials' values on this machine:
65
+ * - `{ kind: "env", name }`: reads the whole header value from launcher `env` through `readSecretSource`.
66
+ * - `{ kind: "gh", env?, host?, format? }`: uses non-empty launcher `env[source.env]` when configured, otherwise
67
+ * reads a token through `context.ghTokens` (at most once per host per call) and frames it as `Basic` or `Bearer`.
68
+ *
69
+ * Returns normalized sandbox credentials in the runner's order plus every secret form to redact as `<token>`, or each
70
+ * problem in first-seen order.
35
71
  */
36
- export function readCredentials(recipe, env) {
37
- const read = readUniqueSources(recipe.credentials, env, ({ domain, header, requests }, value) => ({
38
- domain,
39
- header,
40
- value,
41
- requests,
42
- }));
43
- return read.ok ? { ok: true, credentials: read.items } : read;
72
+ export async function readCredentials(options, env, context) {
73
+ const envCache = new Map();
74
+ const overrideEnvFailures = new Set();
75
+ const ghCache = new Map();
76
+ const ghFailureSeen = new Set();
77
+ const problems = [];
78
+ const credentials = [];
79
+ const secretSet = new Set();
80
+ const addSecrets = (forms) => {
81
+ for (const form of forms) {
82
+ if (form.length > 0)
83
+ secretSet.add(form);
84
+ }
85
+ };
86
+ for (const entry of options.credentials) {
87
+ const { domain, header, source, requests } = entry;
88
+ switch (source.kind) {
89
+ case "env": {
90
+ const id = secretSourceId(source);
91
+ let read = envCache.get(id);
92
+ if (read === undefined) {
93
+ read = readSecretSource(source, env);
94
+ envCache.set(id, read);
95
+ if (!read.ok) {
96
+ problems.push(envVariableProblem(describeSecretSource(source), read.problem));
97
+ }
98
+ }
99
+ if (read.ok) {
100
+ credentials.push({ domain, header, value: read.value, requests });
101
+ addSecrets([read.value]);
102
+ }
103
+ break;
104
+ }
105
+ case "gh": {
106
+ const host = resolveGhHost(source);
107
+ const format = resolveGhFormat(domain, source);
108
+ const envOverride = source.env === undefined ? undefined : ownNonEmpty(env, source.env);
109
+ if (envOverride !== undefined && source.env !== undefined) {
110
+ const framed = frameGhToken(envOverride, format);
111
+ if (!framed.ok) {
112
+ const key = `env:${source.env}:${framed.problem}`;
113
+ if (!overrideEnvFailures.has(key)) {
114
+ overrideEnvFailures.add(key);
115
+ if (framed.problem === "gh-preframed") {
116
+ problems.push({
117
+ name: source.env,
118
+ problem: "gh-preframed",
119
+ detail: `${source.env} must hold a raw token, not a pre-framed Authorization header`,
120
+ hint: `Set ${source.env} to the raw token: storm frames it as ${format === "basic" ? "Basic" : "Bearer"} automatically.`,
121
+ });
122
+ }
123
+ else {
124
+ problems.push(envVariableProblem(source.env, framed.problem));
125
+ }
126
+ }
127
+ }
128
+ else {
129
+ credentials.push({ domain, header, value: framed.headerValue, requests });
130
+ addSecrets(framed.secrets);
131
+ }
132
+ break;
133
+ }
134
+ let ghRead = ghCache.get(host);
135
+ if (ghRead === undefined) {
136
+ ghRead = await context.ghTokens.readToken({ host });
137
+ ghCache.set(host, ghRead);
138
+ }
139
+ const label = `gh (${host})`;
140
+ if (!ghRead.ok) {
141
+ const failureKey = `gh:${host}:${source.env ?? ""}`;
142
+ if (!ghFailureSeen.has(failureKey)) {
143
+ ghFailureSeen.add(failureKey);
144
+ const { failure } = ghRead;
145
+ const problemKind = failure.kind === "not-installed"
146
+ ? "gh-not-installed"
147
+ : failure.kind === "not-authenticated"
148
+ ? "gh-not-authenticated"
149
+ : failure.kind === "timed-out"
150
+ ? "gh-timed-out"
151
+ : "gh-failed";
152
+ const baseHint = failure.kind === "not-installed"
153
+ ? "Install the GitHub CLI (gh) and run `gh auth login`"
154
+ : failure.kind === "not-authenticated"
155
+ ? `Run \`gh auth login --hostname ${host}\``
156
+ : `Check that \`gh auth token --hostname ${host}\` succeeds on this machine`;
157
+ problems.push({
158
+ name: label,
159
+ problem: problemKind,
160
+ detail: failure.message,
161
+ hint: withEnvFallback(baseHint, source.env),
162
+ });
163
+ }
164
+ break;
165
+ }
166
+ const framed = frameGhToken(ghRead.token, format);
167
+ if (!framed.ok) {
168
+ const failureKey = `gh-token:${host}:${framed.problem}`;
169
+ if (!ghFailureSeen.has(failureKey)) {
170
+ ghFailureSeen.add(failureKey);
171
+ if (framed.problem === "gh-preframed") {
172
+ problems.push({
173
+ name: label,
174
+ problem: "gh-preframed",
175
+ detail: `gh auth token --hostname ${host} returned a pre-framed Authorization header instead of a raw token`,
176
+ hint: withEnvFallback(`Re-authenticate with \`gh auth login --hostname ${host}\``, source.env),
177
+ });
178
+ }
179
+ else if (framed.problem === "multiline") {
180
+ problems.push({
181
+ name: label,
182
+ problem: "multiline",
183
+ detail: `gh auth token --hostname ${host} returned a value with a carriage return or a line feed`,
184
+ hint: withEnvFallback(`Re-authenticate with \`gh auth login --hostname ${host}\``, source.env),
185
+ });
186
+ }
187
+ else {
188
+ problems.push({
189
+ name: label,
190
+ problem: "short",
191
+ detail: `gh auth token --hostname ${host} returned fewer than ${SECRET_MIN_LENGTH} characters`,
192
+ hint: withEnvFallback(`Re-authenticate with \`gh auth login --hostname ${host}\``, source.env),
193
+ });
194
+ }
195
+ }
196
+ break;
197
+ }
198
+ credentials.push({ domain, header, value: framed.headerValue, requests });
199
+ addSecrets(framed.secrets);
200
+ break;
201
+ }
202
+ }
203
+ }
204
+ return problems.length === 0 ? { ok: true, credentials, secrets: [...secretSet] } : { ok: false, problems };
44
205
  }
45
206
  /**
46
- * Reads each of `recipe`'s unsafe secret environment variables (`runner.unsafeSecretEnvVars`) from `env`, the
207
+ * Reads each of `options`'s unsafe secret environment variables (`runner.unsafeSecretEnvVars`) from `env`, the
47
208
  * environment of the machine the run starts on, through `readSecretSource`, and maps each value to `entry.name` for
48
209
  * the command's environment. A source's problem is the first that holds of `missing`, `multiline`, and `short`, and a
49
210
  * source two entries share is read and reported once in first-seen order. A value is used as it is, never trimmed.
50
211
  */
51
- export function readUnsafeSecretEnvVars(recipe, env) {
52
- const read = readUniqueSources(recipe.unsafeSecretEnvVars, env, ({ name }, value) => [name, value]);
212
+ export function readUnsafeSecretEnvVars(options, env) {
213
+ const read = readUniqueSources(options.unsafeSecretEnvVars, env, ({ name }, value) => [
214
+ name,
215
+ value,
216
+ ]);
53
217
  return read.ok ? { ok: true, values: Object.fromEntries(read.items) } : read;
54
218
  }
55
219
  /**
@@ -74,3 +238,35 @@ function readUniqueSources(entries, env, map) {
74
238
  }
75
239
  return variables.length === 0 ? { ok: true, items } : { ok: false, variables };
76
240
  }
241
+ function envVariableProblem(name, problem) {
242
+ switch (problem) {
243
+ case "missing":
244
+ return {
245
+ name,
246
+ problem,
247
+ detail: `${name} is not set`,
248
+ hint: `Set ${name} in this machine's environment.`,
249
+ };
250
+ case "multiline":
251
+ return {
252
+ name,
253
+ problem,
254
+ detail: `${name} holds a carriage return or a line feed`,
255
+ hint: `Set ${name} to a value on one line: storm hides each value in output a line at a time, and doesn't trim one.`,
256
+ };
257
+ case "short":
258
+ return {
259
+ name,
260
+ problem,
261
+ detail: `${name} holds fewer than ${SECRET_MIN_LENGTH} characters`,
262
+ hint: `Set ${name} to the whole secret.`,
263
+ };
264
+ }
265
+ }
266
+ function withEnvFallback(base, envVar) {
267
+ return envVar === undefined ? `${base}.` : `${base}, or set ${envVar} in this machine's environment.`;
268
+ }
269
+ function ownNonEmpty(env, name) {
270
+ const value = Object.hasOwn(env, name) ? env[name] : undefined;
271
+ return value === undefined || value.length === 0 ? undefined : value;
272
+ }
@@ -12,12 +12,12 @@ import type { UnsafeSecretEnvVarMap } from "./secrets.ts";
12
12
  export interface RunnerSessionRequest {
13
13
  /** The runner checked on this machine (`prepareRunner`). */
14
14
  readonly prepared: PreparedRunner;
15
- /** The team and project the run's credential is minted for: the runner sandbox runs there. */
16
- readonly target: MintTarget;
15
+ /** The team and project the run's credential is minted for, or null when using `vercel-oidc` without `--scope`/`--project`. */
16
+ readonly target: MintTarget | null;
17
17
  /** The local user name, for each sandbox's `user` tag. */
18
18
  readonly user: string;
19
19
  /**
20
- * The recipe's credentials, with their values read from this machine's environment as the run started
20
+ * The runner's credentials, with their values read from this machine's environment as the run started
21
21
  * (`readCredentials`): the firewall adds every one to the requests of everything the run does in a sandbox—a build's
22
22
  * toolchain and prebuild steps, and the install, build, and command. No value enters a sandbox, and storm writes none
23
23
  * to the run directory, the snapshot cache, or `run.json`.
@@ -51,9 +51,9 @@ export interface RunnerSessionCleanup {
51
51
  readonly startCleanup: () => Promise<void>;
52
52
  readonly release: (runner: RunnerFenceOutcome) => Promise<void>;
53
53
  }
54
- /** What closing a session's sandbox scope left, and the team and project its sandboxes are in: the run's target. */
54
+ /** What closing a session's sandbox scope left, and the team and project its sandboxes are in when known. */
55
55
  export interface RunnerCleanupFacts extends RunnerScopeFacts {
56
- readonly target: MintTarget;
56
+ readonly target: MintTarget | null;
57
57
  }
58
58
  /**
59
59
  * One run's runner session, open from before the run mints. Its stages run in this order: `ready`, then `runCommand` in
@@ -64,7 +64,7 @@ export interface RunnerCleanupFacts extends RunnerScopeFacts {
64
64
  export interface RunnerSession {
65
65
  /**
66
66
  * Readies the runner sandbox (`readyRunner`): reuses the cached runner snapshot it picks or builds a base snapshot,
67
- * starts the runner sandbox from it, uploads and applies the run's changes, grants it the recipe's credentials, and
67
+ * starts the runner sandbox from it, uploads and applies the run's changes, grants it the runner's credentials, and
68
68
  * runs the install and build. With a snapshot the run already chose, it picks and builds none (`readyFromSnapshot`).
69
69
  * The credentials stay in place for the command; when the run ends before the command, the scope's fence revokes
70
70
  * them. `signal` stops it. Called once.
@@ -74,9 +74,9 @@ export interface RunnerSession {
74
74
  * Runs the command in the runner sandbox `ready` readied (`runCommandInSandbox`); canceling the run kills it. It
75
75
  * throws when `ready` hasn't answered `ready`, a mistake in the caller.
76
76
  *
77
- * The recipe's credentials, which `ready` granted, are still in place as the command starts. With `credentials`, the
77
+ * The runner's credentials, which `ready` granted, are still in place as the command starts. With `credentials`, the
78
78
  * sandbox credentials the run's resources give the command, each with its own `requests`, the sandbox scope first
79
- * grants the recipe's and the resources' together, in one write (`RunnerSandboxScope.grantCredentials`); with none,
79
+ * grants the runner's and the resources' together, in one write (`RunnerSandboxScope.grantCredentials`); with none,
80
80
  * it writes no policy before the command. A failed grant means the command doesn't run: it answers `failed` with
81
81
  * `network-policy-failed` (`operation: "inject"`, `around: "command"`), or `killed` when the signal stopped it. Once
82
82
  * the command has ended, or didn't run, the scope revokes every credential in the runner sandbox when anything was
@@ -1,5 +1,5 @@
1
1
  // A runner session: owns a run's sandbox scope from before minting through cleanup, readying the runner sandbox,
2
- // running the command with recipe and resource credentials, and running cleanup in order (`startCleanup`,
2
+ // running the command with runner and resource credentials, and running cleanup in order (`startCleanup`,
3
3
  // `scope.fence`, resource `release`, `scope.close`). See `README.md` ("Runner session, readying, and command").
4
4
  import { runnerNetworkPolicy } from "./network-policy.js";
5
5
  import { readyFromSnapshot, readyRunner, } from "./ready.js";
@@ -18,11 +18,14 @@ export function openRunnerSession(context, request, control, progress) {
18
18
  const hasUnsafeSecretEnvVars = Object.keys(unsafeSecretEnvVars).length > 0;
19
19
  if (hasUnsafeSecretEnvVars && request.keepMs > 0)
20
20
  throw new Error("a runner session with unsafeSecretEnvVars can't keep its runner sandbox");
21
+ if (request.prepared.options.managedCredentials.length > 0 && request.keepMs > 0)
22
+ throw new Error("a runner session with managedCredentials can't keep its runner sandbox");
21
23
  let commandRunning = false;
22
24
  // When the run is canceled, the scope reports `stopping` and revokes every credential at once.
23
25
  const scope = runnerSandboxScope(context, progress, {
24
26
  keepMs: request.keepMs,
25
- basePolicy: runnerNetworkPolicy(request.prepared.recipe),
27
+ basePolicy: runnerNetworkPolicy(request.prepared.options),
28
+ managedCredentials: request.prepared.options.managedCredentials,
26
29
  signal,
27
30
  commandRunning: () => commandRunning,
28
31
  });
@@ -79,7 +82,7 @@ export function openRunnerSession(context, request, control, progress) {
79
82
  ...(command.testEvents === undefined ? {} : { testEvents: command.testEvents }),
80
83
  }, { kill: signal }, progress);
81
84
  const resourceCredentials = command.credentials ?? [];
82
- // `ready` answered `ready`, so it granted the recipe's credentials, when there are any.
85
+ // `ready` answered `ready`, so it granted the runner's credentials, when there are any.
83
86
  const granted = request.credentials.length > 0 || resourceCredentials.length > 0;
84
87
  try {
85
88
  if (resourceCredentials.length > 0) {
@@ -0,0 +1,92 @@
1
+ import type { NamedSandboxInfo, RunnerSnapshotEntry, SandboxAccess, SnapshotInfo, SnapshotLookup } from "./gateways.ts";
2
+ import type { CheckedVercelSandboxOptions } from "./vercel-sandbox-options.ts";
3
+ /** The name prefix of every named sandbox that serves as a shared snapshot index entry. */
4
+ export declare const SHARED_SNAPSHOT_SANDBOX_PREFIX = "storm-snap-";
5
+ /** Maximum number of concurrent Sandbox API or Git calls when scanning shared snapshot index entries. */
6
+ export declare const INDEX_CONCURRENCY = 8;
7
+ /** What identifies one bucket of shared runner snapshots in a Vercel project. */
8
+ export interface SharedSnapshotBucketInput {
9
+ readonly teamId: string;
10
+ readonly projectId: string;
11
+ readonly region: string;
12
+ readonly image: string;
13
+ readonly runnerName: string;
14
+ readonly snapshotKey: string;
15
+ }
16
+ /**
17
+ * The 16-hex-character bucket identifier for `input`: the first 16 hex characters of sha256 over its team ID, project
18
+ * ID, region, image, runner name, and snapshot key, joined with NUL.
19
+ */
20
+ export declare function sharedSnapshotBucket(input: SharedSnapshotBucketInput): string;
21
+ /** The name prefix shared by every named sandbox in `bucket`. */
22
+ export declare function sharedSnapshotSandboxPrefix(bucket: string): string;
23
+ /** The named sandbox name for a shared snapshot at commit `base` in `bucket`. */
24
+ export declare function sharedSnapshotSandboxName(bucket: string, base: string): string;
25
+ /** The bucket and base commit SHA parsed from a shared snapshot sandbox name. */
26
+ export interface ParsedSharedSnapshotName {
27
+ readonly bucket: string;
28
+ readonly base: string;
29
+ }
30
+ /** Parses `name` as `storm-snap-<16-hex-bucket>-<40-hex-base>`, or null when it does not match. */
31
+ export declare function parseSharedSnapshotSandboxName(name: string): ParsedSharedSnapshotName | null;
32
+ /**
33
+ * The verified state of one shared snapshot index entry:
34
+ *
35
+ * - `ready`: the sandbox is stopped and its snapshot exists with status `created` and more than an hour left.
36
+ * - `expires-soon`: the sandbox is stopped and its snapshot exists with status `created` and has not expired, but has
37
+ * an hour or less left (so a run will not start from it).
38
+ * - `unknown-expiry`: the sandbox is stopped and its snapshot exists with status `created`, but neither the snapshot
39
+ * nor the sandbox's `expires-at` tag carries a valid expiry timestamp (so runs do not start from it and pruning
40
+ * skips it).
41
+ * - `building`: a build still owns the sandbox (`pending`, `running`, `snapshotting`, or `stopping`).
42
+ * - `expired`: the sandbox is stopped and its snapshot exists with status `created`, but has expired.
43
+ * - `missing`: the sandbox is stopped with a snapshot ID, but the snapshot is not found or not `created` (`deleted` or
44
+ * `failed`).
45
+ * - `no-snapshot`: the sandbox is stopped without a `currentSnapshotId`.
46
+ * - `failed`: the build sandbox ended in terminal `failed` or `aborted` status.
47
+ */
48
+ export type SharedSnapshotStatus = "ready" | "expires-soon" | "unknown-expiry" | "building" | "expired" | "missing" | "no-snapshot" | "failed";
49
+ /**
50
+ * What can be determined from a named sandbox row before calling `getSnapshot`: either a final status (`building`,
51
+ * `failed`, or `no-snapshot`), or `needs-snapshot` with the `snapshotId` to verify.
52
+ */
53
+ export type UnverifiedSharedSandboxState = {
54
+ readonly kind: "settled";
55
+ readonly status: "building" | "failed" | "no-snapshot";
56
+ } | {
57
+ readonly kind: "needs-snapshot";
58
+ readonly snapshotId: string;
59
+ };
60
+ /** Classifies `row` from its own sandbox status and `currentSnapshotId`, without calling `getSnapshot`. */
61
+ export declare function classifyUnverifiedSharedSandbox(row: NamedSandboxInfo): UnverifiedSharedSandboxState;
62
+ /** The canonical classification of a stopped named sandbox after looking up its snapshot via `getSnapshot`. */
63
+ export interface VerifiedSharedSnapshotState {
64
+ readonly status: "ready" | "expires-soon" | "unknown-expiry" | "expired" | "missing";
65
+ readonly snapshotId: string;
66
+ readonly snapshotStatus: string | undefined;
67
+ readonly createdAt: string;
68
+ readonly expiresAt: string | undefined;
69
+ }
70
+ /**
71
+ * Classifies a stopped named sandbox `row` (holding `currentSnapshotId`) given its `getSnapshot` `lookup` at `now`.
72
+ * Expiry always falls back from `snapshot.expiresAt` to `row.tags["expires-at"]`, so lookup, listing, and pruning
73
+ * agree on every entry.
74
+ */
75
+ export declare function classifyVerifiedSharedSnapshot(row: NamedSandboxInfo & {
76
+ readonly currentSnapshotId: string;
77
+ }, lookup: SnapshotLookup, now: Date): VerifiedSharedSnapshotState;
78
+ /**
79
+ * The local snapshot index entry a shared snapshot index entry stands for: the named sandbox `sandbox`, kept stopped at
80
+ * `base`, and its snapshot `snapshot`. Null unless the canonical classifier marks it `ready` (status `created`, known
81
+ * expiry from `snapshot.expiresAt ?? sandbox.tags["expires-at"]`, and more than an hour left).
82
+ */
83
+ export declare function usableSharedEntry(access: SandboxAccess, runner: Pick<CheckedVercelSandboxOptions, "name" | "region" | "image">, snapshotKey: string, base: string, sandbox: NamedSandboxInfo, snapshot: SnapshotInfo, now: Date): RunnerSnapshotEntry | null;
84
+ /**
85
+ * Runs `task` for indexes `0` to `count - 1`, at most `limit` at a time, in index order. Before starting each task it
86
+ * asks `proceed`; once that says no, no further task starts, and the ones in flight still finish.
87
+ *
88
+ * Its twin is the `runLimited` of an extension folder's `limit.ts`, with the same body. The layering leaves them no
89
+ * shared home: an extension folder imports storm only through the extension API, and the runner names no resource and
90
+ * imports no extension. Change both together.
91
+ */
92
+ export declare function runLimited<T>(count: number, limit: number, proceed: () => boolean, task: (index: number) => Promise<T>): Promise<readonly (T | undefined)[]>;
@@ -0,0 +1,168 @@
1
+ // Shared snapshot index naming, bucketing, concurrency, and canonical entry classification. Each bucket hashes
2
+ // the team, project, region, image, runner name, and snapshot key into 16 hex characters, and each entry's named
3
+ // sandbox is `storm-snap-<bucket>-<base>`. Lookup, listing, publishing, and pruning share one status and expiry
4
+ // classifier so they never disagree on whether an entry is usable or expired.
5
+ import { createHash } from "node:crypto";
6
+ import { SNAPSHOT_MIN_REMAINING_MS } from "./local-snapshot-index.js";
7
+ /** The name prefix of every named sandbox that serves as a shared snapshot index entry. */
8
+ export const SHARED_SNAPSHOT_SANDBOX_PREFIX = "storm-snap-";
9
+ const SHARED_SNAPSHOT_NAME_PATTERN = /^storm-snap-([0-9a-f]{16})-([0-9a-f]{40})$/;
10
+ /** Maximum number of concurrent Sandbox API or Git calls when scanning shared snapshot index entries. */
11
+ export const INDEX_CONCURRENCY = 8;
12
+ /**
13
+ * Whether `status` means a build still owns the named sandbox (active or in progress). Exhaustive over
14
+ * `NamedSandboxStatus`: `stopped` is a completed entry, and `failed`/`aborted` are dead entries eligible for pruning
15
+ * or replacement.
16
+ */
17
+ function isBuildingSandboxStatus(status) {
18
+ switch (status) {
19
+ case "pending":
20
+ case "running":
21
+ case "snapshotting":
22
+ case "stopping":
23
+ return true;
24
+ case "stopped":
25
+ case "failed":
26
+ case "aborted":
27
+ return false;
28
+ }
29
+ }
30
+ /** Whether `row` holds a non-empty `currentSnapshotId`. */
31
+ function hasSnapshotId(row) {
32
+ return row.currentSnapshotId !== undefined && row.currentSnapshotId !== "";
33
+ }
34
+ /**
35
+ * The 16-hex-character bucket identifier for `input`: the first 16 hex characters of sha256 over its team ID, project
36
+ * ID, region, image, runner name, and snapshot key, joined with NUL.
37
+ */
38
+ export function sharedSnapshotBucket(input) {
39
+ return createHash("sha256")
40
+ .update([input.teamId, input.projectId, input.region, input.image, input.runnerName, input.snapshotKey].join("\0"))
41
+ .digest("hex")
42
+ .slice(0, 16);
43
+ }
44
+ /** The name prefix shared by every named sandbox in `bucket`. */
45
+ export function sharedSnapshotSandboxPrefix(bucket) {
46
+ return `${SHARED_SNAPSHOT_SANDBOX_PREFIX}${bucket}-`;
47
+ }
48
+ /** The named sandbox name for a shared snapshot at commit `base` in `bucket`. */
49
+ export function sharedSnapshotSandboxName(bucket, base) {
50
+ return `${SHARED_SNAPSHOT_SANDBOX_PREFIX}${bucket}-${base}`;
51
+ }
52
+ /** Parses `name` as `storm-snap-<16-hex-bucket>-<40-hex-base>`, or null when it does not match. */
53
+ export function parseSharedSnapshotSandboxName(name) {
54
+ const match = SHARED_SNAPSHOT_NAME_PATTERN.exec(name);
55
+ if (match === null)
56
+ return null;
57
+ const [, bucket, base] = match;
58
+ if (bucket === undefined || base === undefined)
59
+ return null;
60
+ return { bucket, base };
61
+ }
62
+ /** Classifies `row` from its own sandbox status and `currentSnapshotId`, without calling `getSnapshot`. */
63
+ export function classifyUnverifiedSharedSandbox(row) {
64
+ if (isBuildingSandboxStatus(row.status))
65
+ return { kind: "settled", status: "building" };
66
+ if (row.status === "failed" || row.status === "aborted")
67
+ return { kind: "settled", status: "failed" };
68
+ if (!hasSnapshotId(row))
69
+ return { kind: "settled", status: "no-snapshot" };
70
+ return { kind: "needs-snapshot", snapshotId: row.currentSnapshotId };
71
+ }
72
+ /**
73
+ * Classifies a stopped named sandbox `row` (holding `currentSnapshotId`) given its `getSnapshot` `lookup` at `now`.
74
+ * Expiry always falls back from `snapshot.expiresAt` to `row.tags["expires-at"]`, so lookup, listing, and pruning
75
+ * agree on every entry.
76
+ */
77
+ export function classifyVerifiedSharedSnapshot(row, lookup, now) {
78
+ if (lookup.kind === "not-found") {
79
+ return {
80
+ status: "missing",
81
+ snapshotId: row.currentSnapshotId,
82
+ snapshotStatus: undefined,
83
+ createdAt: row.createdAt,
84
+ expiresAt: row.tags["expires-at"],
85
+ };
86
+ }
87
+ const { snapshot } = lookup;
88
+ const expiresAt = snapshot.expiresAt ?? row.tags["expires-at"];
89
+ if (snapshot.status !== "created") {
90
+ return {
91
+ status: "missing",
92
+ snapshotId: snapshot.snapshotId,
93
+ snapshotStatus: snapshot.status,
94
+ createdAt: snapshot.createdAt,
95
+ expiresAt,
96
+ };
97
+ }
98
+ const expiresAtMs = expiresAt === undefined ? Number.NaN : Date.parse(expiresAt);
99
+ if (Number.isNaN(expiresAtMs)) {
100
+ return {
101
+ status: "unknown-expiry",
102
+ snapshotId: snapshot.snapshotId,
103
+ snapshotStatus: snapshot.status,
104
+ createdAt: snapshot.createdAt,
105
+ expiresAt,
106
+ };
107
+ }
108
+ const nowMs = now.getTime();
109
+ const status = expiresAtMs <= nowMs ? "expired" : expiresAtMs - nowMs > SNAPSHOT_MIN_REMAINING_MS ? "ready" : "expires-soon";
110
+ return {
111
+ status,
112
+ snapshotId: snapshot.snapshotId,
113
+ snapshotStatus: snapshot.status,
114
+ createdAt: snapshot.createdAt,
115
+ expiresAt,
116
+ };
117
+ }
118
+ /**
119
+ * The local snapshot index entry a shared snapshot index entry stands for: the named sandbox `sandbox`, kept stopped at
120
+ * `base`, and its snapshot `snapshot`. Null unless the canonical classifier marks it `ready` (status `created`, known
121
+ * expiry from `snapshot.expiresAt ?? sandbox.tags["expires-at"]`, and more than an hour left).
122
+ */
123
+ export function usableSharedEntry(access, runner, snapshotKey, base, sandbox, snapshot, now) {
124
+ const verified = classifyVerifiedSharedSnapshot({ ...sandbox, currentSnapshotId: snapshot.snapshotId }, { kind: "found", snapshot }, now);
125
+ if (verified.status !== "ready" || verified.expiresAt === undefined)
126
+ return null;
127
+ return {
128
+ teamId: access.teamId,
129
+ projectId: access.projectId,
130
+ region: runner.region,
131
+ image: runner.image,
132
+ runnerName: runner.name,
133
+ snapshotKey,
134
+ base,
135
+ snapshotId: verified.snapshotId,
136
+ createdAt: verified.createdAt,
137
+ expiresAt: verified.expiresAt,
138
+ };
139
+ }
140
+ /**
141
+ * Runs `task` for indexes `0` to `count - 1`, at most `limit` at a time, in index order. Before starting each task it
142
+ * asks `proceed`; once that says no, no further task starts, and the ones in flight still finish.
143
+ *
144
+ * Its twin is the `runLimited` of an extension folder's `limit.ts`, with the same body. The layering leaves them no
145
+ * shared home: an extension folder imports storm only through the extension API, and the runner names no resource and
146
+ * imports no extension. Change both together.
147
+ */
148
+ export async function runLimited(count, limit, proceed, task) {
149
+ const results = Array.from({ length: count }, () => undefined);
150
+ let failure;
151
+ let next = 0;
152
+ const worker = async () => {
153
+ while (failure === undefined && next < count && proceed()) {
154
+ const index = next;
155
+ next += 1;
156
+ try {
157
+ results[index] = await task(index);
158
+ }
159
+ catch (error) {
160
+ failure ??= { error };
161
+ }
162
+ }
163
+ };
164
+ await Promise.all(Array.from({ length: Math.min(limit, count) }, worker));
165
+ if (failure !== undefined)
166
+ throw failure.error;
167
+ return results;
168
+ }