@cnc-tools/storm 0.16.0 → 0.17.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 (281) hide show
  1. package/dist/application.d.ts +22 -13
  2. package/dist/application.js +21 -10
  3. package/dist/bin/run.d.ts +18 -0
  4. package/dist/bin/run.js +31 -0
  5. package/dist/bin/storm.js +42 -26
  6. package/dist/build-info.js +1 -1
  7. package/dist/chunks/{chunk-5CCP36QQ.js → chunk-2RNDPDTJ.js} +2 -2
  8. package/dist/chunks/chunk-2VP4GRJM.js +1219 -0
  9. package/dist/chunks/{chunk-P357TZAY.js → chunk-4O5C2XGJ.js} +18 -15
  10. package/dist/chunks/{chunk-VL7WFTVQ.js → chunk-4WAMTUWZ.js} +419 -340
  11. package/dist/chunks/{chunk-OIY3DMF2.js → chunk-6UHIJXZG.js} +3 -3
  12. package/dist/chunks/{chunk-CN4EFA5J.js → chunk-72ZFEMAW.js} +1 -1
  13. package/dist/chunks/{chunk-CK2GZEPK.js → chunk-7DZE67QM.js} +2 -2
  14. package/dist/chunks/{chunk-VUPZH7J2.js → chunk-BCBKYOHW.js} +1 -1
  15. package/dist/chunks/{chunk-2MAD5NKK.js → chunk-CKGZA25H.js} +1 -18
  16. package/dist/chunks/{chunk-ZG3WQ4UF.js → chunk-D27KILIA.js} +1 -1
  17. package/dist/chunks/{chunk-V4IKA3WI.js → chunk-DADEYJDW.js} +24 -17
  18. package/dist/chunks/{chunk-R7XVBZOY.js → chunk-EEENLQHE.js} +59 -11
  19. package/dist/chunks/chunk-F4MOYHRY.js +198 -0
  20. package/dist/chunks/{chunk-HVNK3I45.js → chunk-FNBB6V4R.js} +1 -1
  21. package/dist/chunks/{chunk-G3PHMXH5.js → chunk-GRT3UIBV.js} +18 -1
  22. package/dist/chunks/chunk-HP3CURSZ.js +88 -0
  23. package/dist/chunks/{chunk-DWEAHX4T.js → chunk-HWIQ2S7K.js} +5 -5
  24. package/dist/chunks/chunk-JN3YY6ZH.js +2166 -0
  25. package/dist/chunks/{chunk-TJNFVNKR.js → chunk-LKTZ5OXS.js} +1 -1
  26. package/dist/chunks/{chunk-3L52KNQA.js → chunk-M7EKRGVD.js} +8 -8
  27. package/dist/chunks/chunk-MR77L2GK.js +340 -0
  28. package/dist/chunks/{chunk-DC2CDHNK.js → chunk-RNAS7JHE.js} +1438 -445
  29. package/dist/chunks/{chunk-FCQUJDA4.js → chunk-RWIXDDCK.js} +4 -189
  30. package/dist/chunks/{chunk-5VXBKPW6.js → chunk-WV3DP6NR.js} +1 -1
  31. package/dist/chunks/{chunk-AWBSONHB.js → chunk-XBDRVFEV.js} +55 -16
  32. package/dist/chunks/{chunk-FOZWA52V.js → chunk-YEPFSLQR.js} +480 -375
  33. package/dist/chunks/{live-app-WECQOQ4C.js → live-app-R76JQXZL.js} +15 -13
  34. package/dist/commands/exec/across-sandboxes-outcomes.d.ts +1 -1
  35. package/dist/commands/exec/across-sandboxes-outcomes.js +2 -2
  36. package/dist/commands/exec/before-run.d.ts +123 -0
  37. package/dist/commands/exec/before-run.js +105 -0
  38. package/dist/commands/exec/command.d.ts +31 -2
  39. package/dist/commands/exec/command.js +31 -95
  40. package/dist/commands/exec/driver-plan.d.ts +9 -4
  41. package/dist/commands/exec/driver-plan.js +8 -8
  42. package/dist/commands/exec/exec-words.js +1 -1
  43. package/dist/commands/exec/execute-run.d.ts +39 -0
  44. package/dist/commands/exec/execute-run.js +325 -0
  45. package/dist/commands/exec/live-view.d.ts +3 -3
  46. package/dist/commands/exec/live-view.js +16 -15
  47. package/dist/commands/exec/metadata.js +1 -1
  48. package/dist/commands/exec/mint-failed.d.ts +38 -0
  49. package/dist/commands/exec/mint-failed.js +27 -4
  50. package/dist/commands/exec/outcomes.d.ts +34 -8
  51. package/dist/commands/exec/outcomes.js +30 -23
  52. package/dist/commands/exec/plan-run.d.ts +58 -150
  53. package/dist/commands/exec/plan-run.js +232 -88
  54. package/dist/commands/exec/progress.d.ts +10 -6
  55. package/dist/commands/exec/progress.js +38 -25
  56. package/dist/commands/exec/run-bin.d.ts +11 -11
  57. package/dist/commands/exec/run-bin.js +9 -9
  58. package/dist/commands/exec/run-flags.d.ts +69 -0
  59. package/dist/commands/exec/run-flags.js +58 -0
  60. package/dist/commands/exec/run-metadata.js +4 -4
  61. package/dist/commands/exec/run-summary.d.ts +58 -0
  62. package/dist/commands/exec/run-summary.js +205 -0
  63. package/dist/commands/exec/runner-refusals.d.ts +82 -0
  64. package/dist/commands/exec/runner-refusals.js +71 -0
  65. package/dist/commands/exec/sandbox-progress.js +3 -0
  66. package/dist/commands/exec/stopped-outcome.d.ts +12 -0
  67. package/dist/commands/exec/stopped-outcome.js +77 -0
  68. package/dist/commands/exec/view.d.ts +20 -11
  69. package/dist/commands/exec/view.js +103 -47
  70. package/dist/commands/runs/facts.d.ts +7 -3
  71. package/dist/commands/runs/facts.js +17 -8
  72. package/dist/commands/runs/list/command.d.ts +14 -6
  73. package/dist/commands/runs/list/command.js +10 -10
  74. package/dist/commands/runs/list/outcomes.d.ts +7 -3
  75. package/dist/commands/runs/logs/command.js +10 -10
  76. package/dist/commands/runs/show/command.d.ts +14 -6
  77. package/dist/commands/runs/show/command.js +30 -28
  78. package/dist/commands/runs/show/outcomes.d.ts +7 -3
  79. package/dist/commands/runs/show/outcomes.js +7 -5
  80. package/dist/commands/snapshots/access.d.ts +111 -0
  81. package/dist/commands/snapshots/access.js +96 -0
  82. package/dist/commands/snapshots/build/command.d.ts +360 -93
  83. package/dist/commands/snapshots/build/command.js +446 -150
  84. package/dist/commands/snapshots/build/metadata.d.ts +10 -0
  85. package/dist/commands/snapshots/build/metadata.js +31 -11
  86. package/dist/commands/snapshots/build/outcomes.d.ts +234 -43
  87. package/dist/commands/snapshots/build/outcomes.js +200 -36
  88. package/dist/commands/snapshots/list/command.d.ts +366 -25
  89. package/dist/commands/snapshots/list/command.js +296 -66
  90. package/dist/commands/snapshots/list/metadata.d.ts +16 -0
  91. package/dist/commands/snapshots/list/metadata.js +29 -6
  92. package/dist/commands/snapshots/list/outcomes.d.ts +245 -13
  93. package/dist/commands/snapshots/list/outcomes.js +221 -50
  94. package/dist/commands/snapshots/metadata.js +1 -1
  95. package/dist/commands/snapshots/prune/command.d.ts +430 -0
  96. package/dist/commands/snapshots/prune/command.js +161 -0
  97. package/dist/commands/snapshots/prune/metadata.d.ts +28 -0
  98. package/dist/commands/snapshots/prune/metadata.js +39 -0
  99. package/dist/commands/snapshots/prune/outcomes.d.ts +208 -0
  100. package/dist/commands/snapshots/prune/outcomes.js +78 -0
  101. package/dist/commands/suites/list/command.js +17 -17
  102. package/dist/commands/suites/list/outcomes.js +2 -2
  103. package/dist/commands/test/command.d.ts +31 -2
  104. package/dist/commands/test/command.js +49 -252
  105. package/dist/commands/test/outcomes.d.ts +15 -1
  106. package/dist/config/check-runner.d.ts +3 -3
  107. package/dist/config/check-runner.js +8 -7
  108. package/dist/config/credential-resolver.d.ts +50 -5
  109. package/dist/config/credential-resolver.js +51 -19
  110. package/dist/config/credential-settle.d.ts +6 -2
  111. package/dist/config/credential-settle.js +11 -5
  112. package/dist/config/index.d.ts +2 -2
  113. package/dist/config/index.js +1 -1
  114. package/dist/config/runner.d.ts +5 -5
  115. package/dist/config/runner.js +1 -1
  116. package/dist/config/schema.d.ts +2 -2
  117. package/dist/config/schema.js +1 -1
  118. package/dist/config-file/define.d.ts +1 -1
  119. package/dist/config-file/define.js +1 -1
  120. package/dist/config-file/runner.d.ts +5 -5
  121. package/dist/config-file/toolchain.d.ts +1 -1
  122. package/dist/config-file/toolchain.js +2 -2
  123. package/dist/context.d.ts +8 -6
  124. package/dist/context.js +7 -6
  125. package/dist/core/across-sandboxes.d.ts +7 -6
  126. package/dist/core/across-sandboxes.js +123 -152
  127. package/dist/core/coordinator.d.ts +166 -0
  128. package/dist/core/coordinator.js +233 -0
  129. package/dist/core/events.d.ts +2 -2
  130. package/dist/core/index.d.ts +4 -2
  131. package/dist/core/index.js +1 -1
  132. package/dist/core/outcomes.d.ts +148 -0
  133. package/dist/core/{sandbox-end.js → outcomes.js} +28 -4
  134. package/dist/core/resources.d.ts +31 -53
  135. package/dist/core/resources.js +32 -70
  136. package/dist/core/run-state.d.ts +7 -1
  137. package/dist/core/run-state.js +2 -0
  138. package/dist/core/run.d.ts +32 -236
  139. package/dist/core/run.js +106 -266
  140. package/dist/core/state-directory.d.ts +1 -1
  141. package/dist/core/state-directory.js +2 -2
  142. package/dist/credential/gateways.d.ts +15 -1
  143. package/dist/credential/index.d.ts +4 -3
  144. package/dist/credential/index.js +3 -2
  145. package/dist/credential/live/vercel-cli-gateway.d.ts +1 -1
  146. package/dist/credential/live/vercel-cli-gateway.js +1 -1
  147. package/dist/credential/live/vercel-oidc-token-gateway.d.ts +11 -0
  148. package/dist/credential/live/vercel-oidc-token-gateway.js +16 -0
  149. package/dist/credential/source.d.ts +73 -6
  150. package/dist/credential/source.js +55 -2
  151. package/dist/extension-commands.d.ts +6 -1
  152. package/dist/extension-commands.js +15 -1
  153. package/dist/extensions/testserver/commands/pool/create/command.d.ts +35 -2
  154. package/dist/extensions/testserver/commands/pool/create/command.js +7 -7
  155. package/dist/extensions/testserver/commands/pool/create/metadata.js +5 -5
  156. package/dist/extensions/testserver/commands/pool/create/outcomes.d.ts +17 -1
  157. package/dist/extensions/testserver/commands/pool/list/command.js +4 -4
  158. package/dist/extensions/testserver/credential-facts.d.ts +6 -2
  159. package/dist/extensions/testserver/credential-facts.js +29 -11
  160. package/dist/extensions/testserver/lifecycle.js +7 -1
  161. package/dist/extensions/testserver/limit.d.ts +3 -0
  162. package/dist/extensions/testserver/limit.js +3 -0
  163. package/dist/extensions/testserver/provision.d.ts +8 -1
  164. package/dist/extensions/testserver/provision.js +8 -3
  165. package/dist/extensions/testserver/refusals.d.ts +6 -1
  166. package/dist/extensions/testserver/refusals.js +16 -1
  167. package/dist/extensions/testserver/resource.d.ts +1 -1
  168. package/dist/extensions/testserver/resource.js +1 -1
  169. package/dist/extensions/testserver/result.d.ts +1 -1
  170. package/dist/extensions/testserver/result.js +9 -4
  171. package/dist/planning/driver-file-lists.d.ts +0 -15
  172. package/dist/planning/driver-file-lists.js +2 -21
  173. package/dist/planning/plan-only.d.ts +3 -14
  174. package/dist/planning/plan-only.js +11 -20
  175. package/dist/planning/run-kind-flags.js +2 -2
  176. package/dist/planning/run-plan.d.ts +45 -12
  177. package/dist/planning/run-plan.js +4 -3
  178. package/dist/planning/suites.d.ts +1 -1
  179. package/dist/planning/suites.js +2 -2
  180. package/dist/planning/test-plan.d.ts +9 -0
  181. package/dist/planning/test-plan.js +9 -0
  182. package/dist/run-directory/recording.js +2 -2
  183. package/dist/run-directory/run-record.d.ts +21 -6
  184. package/dist/run-directory/run-record.js +12 -9
  185. package/dist/runner/base.js +1 -1
  186. package/dist/runner/build-snapshots.d.ts +55 -29
  187. package/dist/runner/build-snapshots.js +130 -29
  188. package/dist/runner/context.d.ts +15 -7
  189. package/dist/runner/context.js +19 -6
  190. package/dist/runner/events.d.ts +16 -10
  191. package/dist/runner/gateways.d.ts +87 -21
  192. package/dist/runner/gateways.js +1 -1
  193. package/dist/runner/index.d.ts +11 -7
  194. package/dist/runner/index.js +9 -4
  195. package/dist/runner/live/sandbox-errors.d.ts +2 -0
  196. package/dist/runner/live/sandbox-errors.js +4 -0
  197. package/dist/runner/live/sandbox-gateway.js +129 -3
  198. package/dist/runner/live/snapshot-index-gateway.d.ts +12 -0
  199. package/dist/runner/live/{snapshot-cache-gateway.js → snapshot-index-gateway.js} +11 -11
  200. package/dist/runner/live/snapshot-records.js +6 -7
  201. package/dist/runner/local-snapshot-index.d.ts +199 -0
  202. package/dist/runner/local-snapshot-index.js +211 -0
  203. package/dist/runner/network-policy.d.ts +6 -6
  204. package/dist/runner/network-policy.js +7 -7
  205. package/dist/runner/policy-writes.d.ts +1 -1
  206. package/dist/runner/policy-writes.js +1 -1
  207. package/dist/runner/prepare.d.ts +14 -14
  208. package/dist/runner/prepare.js +20 -11
  209. package/dist/runner/ready.d.ts +18 -10
  210. package/dist/runner/ready.js +104 -33
  211. package/dist/runner/sandboxed-command.d.ts +3 -3
  212. package/dist/runner/sandboxed-command.js +3 -3
  213. package/dist/runner/scope.d.ts +10 -6
  214. package/dist/runner/scope.js +10 -3
  215. package/dist/runner/secrets.d.ts +13 -13
  216. package/dist/runner/secrets.js +18 -15
  217. package/dist/runner/session.d.ts +8 -8
  218. package/dist/runner/session.js +3 -3
  219. package/dist/runner/shared-snapshot-entry.d.ts +92 -0
  220. package/dist/runner/shared-snapshot-entry.js +168 -0
  221. package/dist/runner/shared-snapshot-list.d.ts +81 -0
  222. package/dist/runner/shared-snapshot-list.js +155 -0
  223. package/dist/runner/shared-snapshot-lookup.d.ts +51 -0
  224. package/dist/runner/shared-snapshot-lookup.js +118 -0
  225. package/dist/runner/shared-snapshot-prune.d.ts +58 -0
  226. package/dist/runner/shared-snapshot-prune.js +133 -0
  227. package/dist/runner/snapshot-build.d.ts +42 -14
  228. package/dist/runner/snapshot-build.js +32 -29
  229. package/dist/runner/snapshot-choice.js +1 -1
  230. package/dist/runner/snapshot-inspect.d.ts +39 -40
  231. package/dist/runner/snapshot-inspect.js +55 -35
  232. package/dist/runner/snapshot-key.d.ts +13 -12
  233. package/dist/runner/snapshot-key.js +13 -12
  234. package/dist/runner/steps.d.ts +1 -1
  235. package/dist/runner/steps.js +1 -1
  236. package/dist/runner/{recipe-check.d.ts → vercel-sandbox-check.d.ts} +2 -2
  237. package/dist/runner/{recipe-check.js → vercel-sandbox-check.js} +3 -2
  238. package/dist/runner/{recipe.d.ts → vercel-sandbox-options.d.ts} +24 -23
  239. package/dist/runner/{recipe.js → vercel-sandbox-options.js} +70 -69
  240. package/dist/runner/{recipe-secrets.d.ts → vercel-sandbox-secrets.d.ts} +6 -6
  241. package/dist/runner/{recipe-secrets.js → vercel-sandbox-secrets.js} +20 -20
  242. package/dist/sdk/extension/commands.d.ts +8 -3
  243. package/dist/sdk/extension/commands.js +3 -1
  244. package/dist/sdk/extension/credential-diagnosis.d.ts +3 -1
  245. package/dist/sdk/extension/credential-diagnosis.js +25 -5
  246. package/dist/sdk/extension/credential-source.d.ts +20 -3
  247. package/dist/sdk/extension/credential-source.js +4 -4
  248. package/dist/sdk/extension/index.d.ts +3 -2
  249. package/dist/sdk/extension/index.js +2 -1
  250. package/dist/sdk/extension/resource-definition.d.ts +1 -1
  251. package/dist/sdk/extension/secret-source.d.ts +1 -1
  252. package/dist/sdk/extension/secret-source.js +1 -1
  253. package/dist/sdk/extension/vercel-oidc-facts.d.ts +20 -0
  254. package/dist/sdk/extension/vercel-oidc-facts.js +20 -0
  255. package/dist/shared/cleanup.d.ts +3 -3
  256. package/dist/shared/cleanup.js +15 -5
  257. package/dist/shared/snapshot-text.d.ts +4 -4
  258. package/dist/shared/snapshot-text.js +11 -11
  259. package/dist/time/duration.js +1 -1
  260. package/package.json +3 -2
  261. package/skills/overview.md +4 -4
  262. package/skills/plan-runs.md +1 -1
  263. package/skills/read-run-output.md +6 -6
  264. package/dist/chunks/chunk-7EENF73W.js +0 -782
  265. package/dist/chunks/chunk-Z5PBVG3Y.js +0 -2599
  266. package/dist/commands/exec/across-sandboxes-run.d.ts +0 -180
  267. package/dist/commands/exec/across-sandboxes-run.js +0 -339
  268. package/dist/commands/exec/plan-only.d.ts +0 -10
  269. package/dist/commands/exec/plan-only.js +0 -45
  270. package/dist/commands/exec/run-command.d.ts +0 -475
  271. package/dist/commands/exec/run-command.js +0 -603
  272. package/dist/commands/exec/run-planned.d.ts +0 -20
  273. package/dist/commands/exec/run-planned.js +0 -22
  274. package/dist/commands/exec/sandbox-view.d.ts +0 -19
  275. package/dist/commands/exec/sandbox-view.js +0 -72
  276. package/dist/commands/test/plan.d.ts +0 -92
  277. package/dist/commands/test/plan.js +0 -125
  278. package/dist/core/sandbox-end.d.ts +0 -64
  279. package/dist/runner/live/snapshot-cache-gateway.d.ts +0 -12
  280. package/dist/runner/snapshot-cache.d.ts +0 -148
  281. package/dist/runner/snapshot-cache.js +0 -126
@@ -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 runtime: 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, runtime, 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" | "runtime">, 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, runtime, 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, runtime, 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.runtime, 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
+ runtime: runner.runtime,
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
+ }
@@ -0,0 +1,81 @@
1
+ import type { CheckoutFailure, NamedSandboxStatus, SandboxAccess } from "./gateways.ts";
2
+ import { type SharedSnapshotStatus } from "./shared-snapshot-entry.ts";
3
+ import type { SharedSnapshotLookupContext } from "./shared-snapshot-lookup.ts";
4
+ import type { InspectedSnapshot } from "./snapshot-inspect.ts";
5
+ import type { CheckedVercelSandboxOptions } from "./vercel-sandbox-options.ts";
6
+ /** What `listSharedSnapshots` queries in the shared snapshot index. */
7
+ export interface SharedSnapshotListRequest {
8
+ readonly access: SandboxAccess;
9
+ readonly runner: Pick<CheckedVercelSandboxOptions, "name" | "region" | "runtime">;
10
+ readonly snapshotKey: string;
11
+ }
12
+ /** One entry returned by `listSharedSnapshots`, verified against the Sandbox API when stopped with a snapshot ID. */
13
+ export interface SharedSnapshotIndexEntry {
14
+ readonly sandboxName: string;
15
+ readonly base: string;
16
+ readonly sandboxStatus: NamedSandboxStatus;
17
+ /** When the named sandbox was created, which ranks entries as `lookupSharedSnapshot` does. */
18
+ readonly sandboxCreatedAt: string;
19
+ readonly status: SharedSnapshotStatus;
20
+ readonly snapshotId: string | undefined;
21
+ readonly snapshotStatus: string | undefined;
22
+ readonly createdAt: string;
23
+ readonly expiresAt: string | undefined;
24
+ readonly expired: boolean;
25
+ }
26
+ /** How `listSharedSnapshots` ended. */
27
+ export type SharedSnapshotListResult = {
28
+ readonly kind: "ok";
29
+ readonly bucket: string;
30
+ readonly entries: readonly SharedSnapshotIndexEntry[];
31
+ } | {
32
+ readonly kind: "failed";
33
+ readonly message: string;
34
+ } | {
35
+ readonly kind: "aborted";
36
+ };
37
+ /**
38
+ * Lists every valid `storm-snap-<bucket>-<base>` entry in `request`'s bucket and verifies stopped entries that hold a
39
+ * `currentSnapshotId` via `getSnapshot` with bounded concurrency.
40
+ */
41
+ export declare function listSharedSnapshots(context: Pick<SharedSnapshotLookupContext, "runnerSandboxes" | "clock">, request: SharedSnapshotListRequest, signal?: AbortSignal): Promise<SharedSnapshotListResult>;
42
+ /** One entry of the shared snapshot index, with what the pick across both indexes says of it. */
43
+ export interface SharedSnapshotSelection {
44
+ readonly entry: SharedSnapshotIndexEntry;
45
+ /** Whether a run here, for the listed team and project, starts from it. */
46
+ readonly picked: boolean;
47
+ /** How many commits HEAD has that its base lacks, when it is ready and at or behind the merge-base; else null. */
48
+ readonly builtBehind: number | null;
49
+ }
50
+ /**
51
+ * How `pickSnapshotAcrossIndexes` ended: the local snapshot index's snapshots, with the team and project's pick
52
+ * un-marked when a shared entry beats it, and each shared entry with whether a run here starts from it.
53
+ */
54
+ export type SnapshotAcrossIndexesResult = {
55
+ readonly kind: "ok";
56
+ readonly local: readonly InspectedSnapshot[];
57
+ readonly shared: readonly SharedSnapshotSelection[];
58
+ } | {
59
+ readonly kind: "checkout-failed";
60
+ readonly failure: CheckoutFailure;
61
+ };
62
+ /**
63
+ * Decides which snapshot a run at `request.mergeBase` for `request.owner` starts from, across the local snapshot
64
+ * index's inspected snapshots (`request.local`) and the shared snapshot index's entries (`request.entries`), with the
65
+ * rule `chooseSnapshot` follows: the closest `ready` shared entry at the merge-base or an ancestor of it (ranked by
66
+ * `compareSnapshotCandidates` on the sandbox's `createdAt`, as `lookupSharedSnapshot` ranks before it reads a
67
+ * snapshot), which wins only when it beats the local pick (`sharedBeatsLocal`). Every other team and project's local
68
+ * pick stands. Git is asked once per ancestor base, for its distance behind the merge-base.
69
+ */
70
+ export declare function pickSnapshotAcrossIndexes(context: Pick<SharedSnapshotLookupContext, "checkout">, request: {
71
+ readonly repoRoot: string;
72
+ readonly mergeBase: string;
73
+ /** How many commits HEAD has that the merge-base lacks. */
74
+ readonly commitsAfterBase: number;
75
+ readonly owner: {
76
+ readonly teamId: string;
77
+ readonly projectId: string;
78
+ };
79
+ readonly local: readonly InspectedSnapshot[];
80
+ readonly entries: readonly SharedSnapshotIndexEntry[];
81
+ }): Promise<SnapshotAcrossIndexesResult>;
@@ -0,0 +1,155 @@
1
+ // Listing and selecting entries from the shared snapshot index for `storm snapshots list --shared`.
2
+ import { compareSnapshotCandidates, sharedBeatsLocal } from "./local-snapshot-index.js";
3
+ import { classifyUnverifiedSharedSandbox, classifyVerifiedSharedSnapshot, INDEX_CONCURRENCY, parseSharedSnapshotSandboxName, runLimited, sharedSnapshotBucket, sharedSnapshotSandboxPrefix, } from "./shared-snapshot-entry.js";
4
+ /**
5
+ * Lists every valid `storm-snap-<bucket>-<base>` entry in `request`'s bucket and verifies stopped entries that hold a
6
+ * `currentSnapshotId` via `getSnapshot` with bounded concurrency.
7
+ */
8
+ export async function listSharedSnapshots(context, request, signal) {
9
+ const { runnerSandboxes, clock } = context;
10
+ const { access, runner, snapshotKey } = request;
11
+ const bucket = sharedSnapshotBucket({
12
+ teamId: access.teamId,
13
+ projectId: access.projectId,
14
+ region: runner.region,
15
+ runtime: runner.runtime,
16
+ runnerName: runner.name,
17
+ snapshotKey,
18
+ });
19
+ const listed = await runnerSandboxes.listNamedSandboxes(access, { namePrefix: sharedSnapshotSandboxPrefix(bucket) }, signal);
20
+ if (!listed.ok) {
21
+ return listed.failure.kind === "aborted"
22
+ ? { kind: "aborted" }
23
+ : { kind: "failed", message: listed.failure.message };
24
+ }
25
+ const now = clock.now();
26
+ const matched = [];
27
+ for (const row of listed.value) {
28
+ const parsed = parseSharedSnapshotSandboxName(row.name);
29
+ if (parsed === null || parsed.bucket !== bucket)
30
+ continue;
31
+ matched.push({ row, base: parsed.base });
32
+ }
33
+ let stop = false;
34
+ const outcomes = await runLimited(matched.length, INDEX_CONCURRENCY, () => !stop && signal?.aborted !== true, async (index) => {
35
+ const { row, base } = matched[index];
36
+ const unverified = classifyUnverifiedSharedSandbox(row);
37
+ if (unverified.kind === "settled") {
38
+ return {
39
+ kind: "entry",
40
+ entry: indexEntryOf(row, base, {
41
+ status: unverified.status,
42
+ snapshotId: unverified.status === "no-snapshot" ? undefined : row.currentSnapshotId,
43
+ snapshotStatus: undefined,
44
+ createdAt: row.createdAt,
45
+ expiresAt: row.tags["expires-at"],
46
+ expired: false,
47
+ }),
48
+ };
49
+ }
50
+ const verified = await runnerSandboxes.getSnapshot(access, { snapshotId: unverified.snapshotId }, signal);
51
+ if (!verified.ok) {
52
+ stop = true;
53
+ return verified.failure.kind === "aborted"
54
+ ? { kind: "aborted" }
55
+ : { kind: "failed", message: verified.failure.message };
56
+ }
57
+ const classified = classifyVerifiedSharedSnapshot({ ...row, currentSnapshotId: unverified.snapshotId }, verified.value, now);
58
+ return {
59
+ kind: "entry",
60
+ entry: indexEntryOf(row, base, {
61
+ status: classified.status,
62
+ snapshotId: classified.snapshotId,
63
+ snapshotStatus: classified.snapshotStatus,
64
+ createdAt: classified.createdAt,
65
+ expiresAt: classified.expiresAt,
66
+ expired: classified.status === "expired",
67
+ }),
68
+ };
69
+ });
70
+ if (signal?.aborted === true)
71
+ return { kind: "aborted" };
72
+ const entries = [];
73
+ for (const outcome of outcomes) {
74
+ if (outcome === undefined)
75
+ continue;
76
+ if (outcome.kind === "aborted")
77
+ return { kind: "aborted" };
78
+ if (outcome.kind === "failed")
79
+ return outcome;
80
+ entries.push(outcome.entry);
81
+ }
82
+ return { kind: "ok", bucket, entries };
83
+ }
84
+ function indexEntryOf(row, base, fields) {
85
+ return {
86
+ sandboxName: row.name,
87
+ base,
88
+ sandboxStatus: row.status,
89
+ sandboxCreatedAt: row.createdAt,
90
+ ...fields,
91
+ };
92
+ }
93
+ /**
94
+ * Decides which snapshot a run at `request.mergeBase` for `request.owner` starts from, across the local snapshot
95
+ * index's inspected snapshots (`request.local`) and the shared snapshot index's entries (`request.entries`), with the
96
+ * rule `chooseSnapshot` follows: the closest `ready` shared entry at the merge-base or an ancestor of it (ranked by
97
+ * `compareSnapshotCandidates` on the sandbox's `createdAt`, as `lookupSharedSnapshot` ranks before it reads a
98
+ * snapshot), which wins only when it beats the local pick (`sharedBeatsLocal`). Every other team and project's local
99
+ * pick stands. Git is asked once per ancestor base, for its distance behind the merge-base.
100
+ */
101
+ export async function pickSnapshotAcrossIndexes(context, request) {
102
+ const { checkout } = context;
103
+ const { repoRoot, mergeBase, commitsAfterBase, owner, local, entries } = request;
104
+ const owned = (snapshot) => snapshot.entry.teamId === owner.teamId && snapshot.entry.projectId === owner.projectId;
105
+ const localPick = local.find((snapshot) => snapshot.picked && owned(snapshot));
106
+ const behindMergeBaseByBase = new Map([[mergeBase, 0]]);
107
+ const qualifying = [];
108
+ for (const entry of entries) {
109
+ if (entry.status !== "ready")
110
+ continue;
111
+ let commitsBehindMergeBase = behindMergeBaseByBase.get(entry.base);
112
+ if (commitsBehindMergeBase === undefined) {
113
+ const ancestor = await checkout.contains({ repoRoot, ancestor: entry.base, descendant: mergeBase });
114
+ if (!ancestor.ok)
115
+ return { kind: "checkout-failed", failure: ancestor.failure };
116
+ commitsBehindMergeBase = null;
117
+ if (ancestor.value) {
118
+ const counted = await checkout.commitCount({ repoRoot, from: entry.base, to: mergeBase });
119
+ if (!counted.ok)
120
+ return { kind: "checkout-failed", failure: counted.failure };
121
+ commitsBehindMergeBase = counted.value;
122
+ }
123
+ behindMergeBaseByBase.set(entry.base, commitsBehindMergeBase);
124
+ }
125
+ if (commitsBehindMergeBase !== null)
126
+ qualifying.push({ entry, commitsBehindMergeBase });
127
+ }
128
+ const best = qualifying.toSorted((a, b) => compareSnapshotCandidates({
129
+ commitsBehindMergeBase: a.commitsBehindMergeBase,
130
+ createdAt: a.entry.sandboxCreatedAt,
131
+ tieBreaker: a.entry.sandboxName,
132
+ }, {
133
+ commitsBehindMergeBase: b.commitsBehindMergeBase,
134
+ createdAt: b.entry.sandboxCreatedAt,
135
+ tieBreaker: b.entry.sandboxName,
136
+ }))[0];
137
+ const sharedPick = best !== undefined && sharedBeatsLocal(best.commitsBehindMergeBase, localPick?.commitsBehindMergeBase ?? undefined)
138
+ ? best.entry
139
+ : undefined;
140
+ const readyBehind = new Map(qualifying.map(({ entry, commitsBehindMergeBase }) => [entry, commitsBehindMergeBase]));
141
+ return {
142
+ kind: "ok",
143
+ local: sharedPick === undefined || localPick === undefined
144
+ ? local
145
+ : local.map((snapshot) => (snapshot === localPick ? { ...snapshot, picked: false } : snapshot)),
146
+ shared: entries.map((entry) => {
147
+ const commitsBehindMergeBase = readyBehind.get(entry);
148
+ return {
149
+ entry,
150
+ picked: entry === sharedPick,
151
+ builtBehind: commitsBehindMergeBase === undefined ? null : commitsAfterBase + commitsBehindMergeBase,
152
+ };
153
+ }),
154
+ };
155
+ }
@@ -0,0 +1,51 @@
1
+ import type { Clock } from "../time/index.ts";
2
+ import type { CheckoutGateway, RunnerSandboxGateway, RunnerSnapshotEntry, SandboxAccess } from "./gateways.ts";
3
+ import type { CheckedVercelSandboxOptions } from "./vercel-sandbox-options.ts";
4
+ /** Gateways and seams used to look up, list, or prune shared snapshots. */
5
+ export interface SharedSnapshotLookupContext {
6
+ readonly runnerSandboxes: RunnerSandboxGateway;
7
+ readonly checkout: CheckoutGateway;
8
+ readonly clock: Clock;
9
+ }
10
+ /** What `lookupSharedSnapshot` searches for in the shared snapshot index. */
11
+ export interface SharedSnapshotLookupRequest {
12
+ readonly access: SandboxAccess;
13
+ readonly repoRoot: string;
14
+ readonly runner: Pick<CheckedVercelSandboxOptions, "name" | "region" | "runtime">;
15
+ readonly snapshotKey: string;
16
+ readonly mergeBase: string;
17
+ /**
18
+ * When set, the commit distance behind `mergeBase` of the local snapshot index's candidate: only shared snapshots that
19
+ * beat it (`sharedBeatsLocal`) qualify. When no ancestor can beat it, the bucket listing is skipped after the exact
20
+ * check misses.
21
+ */
22
+ readonly maxCommitsBehindMergeBase?: number;
23
+ }
24
+ /**
25
+ * What `lookupSharedSnapshot` found:
26
+ *
27
+ * - `hit`: a verified, unexpired snapshot with time left at the merge-base (`commitsBehindMergeBase === 0`) or its
28
+ * closest qualifying ancestor.
29
+ * - `miss`: no usable shared snapshot was found in the bucket.
30
+ * - `unavailable`: a Sandbox API call failed, so the caller can degrade gracefully.
31
+ * - `aborted`: the caller's signal aborted the lookup.
32
+ */
33
+ export type SharedSnapshotLookupResult = {
34
+ readonly kind: "hit";
35
+ readonly entry: RunnerSnapshotEntry;
36
+ readonly commitsBehindMergeBase: number;
37
+ readonly sandboxName: string;
38
+ } | {
39
+ readonly kind: "miss";
40
+ } | {
41
+ readonly kind: "unavailable";
42
+ readonly message: string;
43
+ } | {
44
+ readonly kind: "aborted";
45
+ };
46
+ /**
47
+ * Looks up a usable shared snapshot for `request`: first checks the exact named sandbox for `request.mergeBase`
48
+ * without listing, and falls back to listing the bucket and selecting the closest verified ancestor of
49
+ * `request.mergeBase` (tie-breaking by newest `createdAt`, then ascending `name`).
50
+ */
51
+ export declare function lookupSharedSnapshot(context: SharedSnapshotLookupContext, request: SharedSnapshotLookupRequest, signal?: AbortSignal): Promise<SharedSnapshotLookupResult>;
@@ -0,0 +1,118 @@
1
+ // Live run lookup against the shared snapshot index: checks the exact merge-base named sandbox first without
2
+ // listing, then falls back to scanning the bucket for the closest verified ancestor of the merge-base.
3
+ import { compareSnapshotCandidates, sharedBeatsLocal } from "./local-snapshot-index.js";
4
+ import { classifyUnverifiedSharedSandbox, INDEX_CONCURRENCY, parseSharedSnapshotSandboxName, runLimited, sharedSnapshotBucket, sharedSnapshotSandboxName, sharedSnapshotSandboxPrefix, usableSharedEntry, } from "./shared-snapshot-entry.js";
5
+ /**
6
+ * Looks up a usable shared snapshot for `request`: first checks the exact named sandbox for `request.mergeBase`
7
+ * without listing, and falls back to listing the bucket and selecting the closest verified ancestor of
8
+ * `request.mergeBase` (tie-breaking by newest `createdAt`, then ascending `name`).
9
+ */
10
+ export async function lookupSharedSnapshot(context, request, signal) {
11
+ const { runnerSandboxes, checkout, clock } = context;
12
+ const { access, repoRoot, runner, snapshotKey, mergeBase, maxCommitsBehindMergeBase } = request;
13
+ // Even an entry at the merge-base can't beat the caller's candidate.
14
+ if (!sharedBeatsLocal(0, maxCommitsBehindMergeBase))
15
+ return { kind: "miss" };
16
+ const bucket = sharedSnapshotBucket({
17
+ teamId: access.teamId,
18
+ projectId: access.projectId,
19
+ region: runner.region,
20
+ runtime: runner.runtime,
21
+ runnerName: runner.name,
22
+ snapshotKey,
23
+ });
24
+ // Step 1: fast exact check for `mergeBase` without calling `listNamedSandboxes`.
25
+ const exactName = sharedSnapshotSandboxName(bucket, mergeBase);
26
+ const exact = await runnerSandboxes.getNamedSandbox(access, { name: exactName }, signal);
27
+ if (!exact.ok) {
28
+ return unavailable(exact.failure);
29
+ }
30
+ if (exact.value.kind === "found") {
31
+ const { sandbox } = exact.value;
32
+ const unverified = classifyUnverifiedSharedSandbox(sandbox);
33
+ if (unverified.kind === "needs-snapshot") {
34
+ const verified = await runnerSandboxes.getSnapshot(access, { snapshotId: unverified.snapshotId }, signal);
35
+ if (!verified.ok) {
36
+ return unavailable(verified.failure);
37
+ }
38
+ if (verified.value.kind === "found") {
39
+ const entry = usableSharedEntry(access, runner, snapshotKey, mergeBase, sandbox, verified.value.snapshot, clock.now());
40
+ if (entry !== null) {
41
+ return { kind: "hit", entry, commitsBehindMergeBase: 0, sandboxName: sandbox.name };
42
+ }
43
+ }
44
+ }
45
+ }
46
+ // Only the exact match above can beat the caller's candidate when no ancestor, 1 commit behind or more, can.
47
+ if (!sharedBeatsLocal(1, maxCommitsBehindMergeBase))
48
+ return { kind: "miss" };
49
+ // Step 2: ancestor scan across the bucket.
50
+ const listed = await runnerSandboxes.listNamedSandboxes(access, { namePrefix: sharedSnapshotSandboxPrefix(bucket) }, signal);
51
+ if (!listed.ok) {
52
+ return unavailable(listed.failure);
53
+ }
54
+ const stoppedRows = [];
55
+ for (const row of listed.value) {
56
+ const unverified = classifyUnverifiedSharedSandbox(row);
57
+ if (unverified.kind !== "needs-snapshot")
58
+ continue;
59
+ const parsed = parseSharedSnapshotSandboxName(row.name);
60
+ if (parsed === null || parsed.bucket !== bucket || parsed.base === mergeBase)
61
+ continue;
62
+ stoppedRows.push({ row, base: parsed.base, snapshotId: unverified.snapshotId });
63
+ }
64
+ const checked = await runLimited(stoppedRows.length, INDEX_CONCURRENCY, () => signal?.aborted !== true, async (index) => {
65
+ const item = stoppedRows[index];
66
+ const ancestor = await checkout.contains({ repoRoot, ancestor: item.base, descendant: mergeBase });
67
+ if (!ancestor.ok || !ancestor.value)
68
+ return null;
69
+ if (signal?.aborted === true)
70
+ return null;
71
+ const counted = await checkout.commitCount({ repoRoot, from: item.base, to: mergeBase });
72
+ if (!counted.ok)
73
+ return null;
74
+ if (!sharedBeatsLocal(counted.value, maxCommitsBehindMergeBase))
75
+ return null;
76
+ return {
77
+ row: item.row,
78
+ base: item.base,
79
+ snapshotId: item.snapshotId,
80
+ commitsBehindMergeBase: counted.value,
81
+ };
82
+ });
83
+ if (signal?.aborted === true)
84
+ return { kind: "aborted" };
85
+ const candidates = checked.filter((candidate) => candidate != null);
86
+ candidates.sort((a, b) => compareSnapshotCandidates({
87
+ commitsBehindMergeBase: a.commitsBehindMergeBase,
88
+ createdAt: a.row.createdAt,
89
+ tieBreaker: a.row.name,
90
+ }, {
91
+ commitsBehindMergeBase: b.commitsBehindMergeBase,
92
+ createdAt: b.row.createdAt,
93
+ tieBreaker: b.row.name,
94
+ }));
95
+ // Verify candidates sequentially in closest-first order so we stop on the first usable snapshot.
96
+ for (const candidate of candidates) {
97
+ const verified = await runnerSandboxes.getSnapshot(access, { snapshotId: candidate.snapshotId }, signal);
98
+ if (!verified.ok) {
99
+ return unavailable(verified.failure);
100
+ }
101
+ if (verified.value.kind !== "found")
102
+ continue;
103
+ const entry = usableSharedEntry(access, runner, snapshotKey, candidate.base, candidate.row, verified.value.snapshot, clock.now());
104
+ if (entry !== null) {
105
+ return {
106
+ kind: "hit",
107
+ entry,
108
+ commitsBehindMergeBase: candidate.commitsBehindMergeBase,
109
+ sandboxName: candidate.row.name,
110
+ };
111
+ }
112
+ }
113
+ return { kind: "miss" };
114
+ }
115
+ /** How the lookup ends on a failed Sandbox call: aborted for the signal, and `unavailable` otherwise. */
116
+ function unavailable(failure) {
117
+ return failure.kind === "aborted" ? { kind: "aborted" } : { kind: "unavailable", message: failure.message };
118
+ }