@cnc-tools/storm 0.7.0 → 0.8.1

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 (120) hide show
  1. package/dist/build-info.js +1 -1
  2. package/dist/commands/exec/cleanup.d.ts +3 -3
  3. package/dist/commands/exec/cleanup.js +1 -1
  4. package/dist/commands/exec/command.d.ts +771 -1
  5. package/dist/commands/exec/command.js +1 -1
  6. package/dist/commands/exec/live-view.js +3 -2
  7. package/dist/commands/exec/metadata.d.ts +1 -1
  8. package/dist/commands/exec/metadata.js +4 -0
  9. package/dist/commands/exec/mint-failed.d.ts +28 -0
  10. package/dist/commands/exec/outcomes.d.ts +476 -0
  11. package/dist/commands/exec/progress.js +3 -5
  12. package/dist/commands/exec/run-command.d.ts +2 -2
  13. package/dist/commands/exec/run-metadata.d.ts +1 -1
  14. package/dist/commands/exec/run-metadata.js +1 -1
  15. package/dist/commands/exec/tests-summary.d.ts +1 -1
  16. package/dist/commands/exec/tests-summary.js +1 -1
  17. package/dist/commands/runs/facts.d.ts +2 -2
  18. package/dist/commands/runs/facts.js +1 -1
  19. package/dist/commands/runs/list/command.d.ts +129 -1
  20. package/dist/commands/runs/list/command.js +1 -1
  21. package/dist/commands/runs/list/outcomes.d.ts +16 -0
  22. package/dist/commands/runs/list/outcomes.js +1 -1
  23. package/dist/commands/runs/logs/command.d.ts +263 -1
  24. package/dist/commands/runs/logs/command.js +1 -1
  25. package/dist/commands/runs/logs/outcomes.d.ts +105 -0
  26. package/dist/commands/runs/logs/outcomes.js +1 -1
  27. package/dist/commands/runs/show/command.d.ts +329 -1
  28. package/dist/commands/runs/show/command.js +1 -1
  29. package/dist/commands/runs/show/outcomes.d.ts +71 -0
  30. package/dist/commands/runs/show/outcomes.js +3 -3
  31. package/dist/commands/vitest/command.d.ts +886 -1
  32. package/dist/commands/vitest/command.js +1 -1
  33. package/dist/commands/vitest/metadata.d.ts +1 -1
  34. package/dist/commands/vitest/metadata.js +4 -0
  35. package/dist/commands/vitest/outcomes.d.ts +295 -0
  36. package/dist/config/errors.d.ts +73 -0
  37. package/dist/config/schema.d.ts +1 -1
  38. package/dist/config-file/define.d.ts +4 -3
  39. package/dist/config-file/define.js +1 -1
  40. package/dist/config-file/toolchain.js +1 -1
  41. package/dist/core/command-slots.d.ts +8 -2
  42. package/dist/core/command-slots.js +6 -2
  43. package/dist/core/index.d.ts +1 -1
  44. package/dist/credential/index.d.ts +1 -0
  45. package/dist/credential/index.js +1 -0
  46. package/dist/credential/settle.d.ts +20 -0
  47. package/dist/credential/target.d.ts +6 -0
  48. package/dist/credential/target.js +7 -0
  49. package/dist/resources/github/command.d.ts +54 -2
  50. package/dist/resources/github/config.js +1 -1
  51. package/dist/resources/github/index.js +1 -1
  52. package/dist/resources/github/resource.js +2 -2
  53. package/dist/resources/testserver/command.d.ts +208 -10
  54. package/dist/resources/testserver/command.js +7 -0
  55. package/dist/resources/testserver/commands/create/command.d.ts +810 -1
  56. package/dist/resources/testserver/commands/create/command.js +73 -23
  57. package/dist/resources/testserver/commands/create/metadata.js +6 -2
  58. package/dist/resources/testserver/commands/create/outcomes.d.ts +347 -0
  59. package/dist/resources/testserver/commands/create/outcomes.js +14 -1
  60. package/dist/resources/testserver/commands/list/command.d.ts +84 -1
  61. package/dist/resources/testserver/commands/list/command.js +1 -1
  62. package/dist/resources/testserver/commands/list/outcomes.d.ts +16 -0
  63. package/dist/resources/testserver/config.js +14 -4
  64. package/dist/resources/testserver/events.d.ts +11 -0
  65. package/dist/resources/testserver/index.js +8 -7
  66. package/dist/resources/testserver/lane.js +1 -0
  67. package/dist/resources/testserver/live/gateways.d.ts +2 -0
  68. package/dist/resources/testserver/live/gateways.js +16 -3
  69. package/dist/resources/testserver/pool/channels.d.ts +5 -2
  70. package/dist/resources/testserver/pool/channels.js +1 -1
  71. package/dist/resources/testserver/pool/create.d.ts +9 -2
  72. package/dist/resources/testserver/pool/create.js +8 -5
  73. package/dist/resources/testserver/pool/gateways.d.ts +6 -0
  74. package/dist/resources/testserver/pool/index.d.ts +1 -0
  75. package/dist/resources/testserver/pool/index.js +1 -0
  76. package/dist/resources/testserver/pool/live/pool-records.js +6 -2
  77. package/dist/resources/testserver/pool/pull-request.d.ts +96 -0
  78. package/dist/resources/testserver/pool/pull-request.js +118 -0
  79. package/dist/resources/testserver/pr-build-facts.d.ts +130 -0
  80. package/dist/resources/testserver/pr-build-facts.js +261 -0
  81. package/dist/resources/testserver/presentation.d.ts +2 -2
  82. package/dist/resources/testserver/presentation.js +13 -3
  83. package/dist/resources/testserver/provider.d.ts +16 -6
  84. package/dist/resources/testserver/provider.js +55 -16
  85. package/dist/resources/testserver/pull-request/gateways.d.ts +117 -0
  86. package/dist/resources/testserver/pull-request/gateways.js +5 -0
  87. package/dist/resources/testserver/pull-request/index.d.ts +5 -0
  88. package/dist/resources/testserver/pull-request/index.js +6 -0
  89. package/dist/resources/testserver/pull-request/live/github-rest.d.ts +28 -0
  90. package/dist/resources/testserver/pull-request/live/github-rest.js +114 -0
  91. package/dist/resources/testserver/pull-request/live/github-token-gateway.d.ts +12 -0
  92. package/dist/resources/testserver/pull-request/live/github-token-gateway.js +39 -0
  93. package/dist/resources/testserver/pull-request/live/pull-request-gateway.d.ts +10 -0
  94. package/dist/resources/testserver/pull-request/live/pull-request-gateway.js +27 -0
  95. package/dist/resources/testserver/pull-request/live/testserver-workflow-gateway.d.ts +21 -0
  96. package/dist/resources/testserver/pull-request/live/testserver-workflow-gateway.js +64 -0
  97. package/dist/resources/testserver/ran.d.ts +5 -0
  98. package/dist/resources/testserver/ran.js +19 -3
  99. package/dist/resources/testserver/resource.d.ts +5 -1
  100. package/dist/resources/testserver/resource.js +2 -2
  101. package/dist/resources/testserver/sandbox/build-reference.d.ts +38 -0
  102. package/dist/resources/testserver/sandbox/build-reference.js +79 -0
  103. package/dist/resources/testserver/sandbox/index.d.ts +1 -0
  104. package/dist/resources/testserver/sandbox/index.js +1 -0
  105. package/dist/resources.d.ts +265 -15
  106. package/dist/sdk/index.js +2 -2
  107. package/dist/story/command-line.d.ts +34 -0
  108. package/dist/story/command-line.js +100 -0
  109. package/dist/story/index.d.ts +1 -0
  110. package/dist/story/index.js +3 -2
  111. package/dist/vercel-cli/context.js +1 -1
  112. package/dist/vercel-cli/index.js +1 -1
  113. package/dist/vitest/config.js +2 -2
  114. package/dist/vitest/fixtures.d.ts +1 -1
  115. package/dist/vitest/fixtures.js +1 -1
  116. package/dist/vitest/index.js +3 -3
  117. package/dist/vitest/plan.js +1 -1
  118. package/package.json +2 -3
  119. package/skills/overview.md +2 -0
  120. package/skills/testserver-builds.md +82 -0
@@ -57,4 +57,87 @@ export declare function command(cmd: CommandBuilder<TestserverCommandApp>): impo
57
57
  path: z.ZodString;
58
58
  message: z.ZodString;
59
59
  }, z.core.$strip>;
60
- }>, TestserverCommandApp, false>;
60
+ }, {
61
+ readonly poolUnreadable: {
62
+ readonly exitCode: 1;
63
+ readonly description: "storm couldn't read the pool.";
64
+ readonly schema: z.ZodObject<{
65
+ path: z.ZodString;
66
+ message: z.ZodString;
67
+ }, z.core.$strip>;
68
+ readonly render: ({ path, message }: {
69
+ path: string;
70
+ message: string;
71
+ }) => {
72
+ message: string;
73
+ rows: [string, string][];
74
+ };
75
+ };
76
+ }>, import("@vercel/clinkr").OutcomeOf<import("@vercel/clinkr").Outcomes<{
77
+ listed: z.ZodObject<{
78
+ poolPath: z.ZodString;
79
+ entries: z.ZodArray<z.ZodObject<{
80
+ testserverUrl: z.ZodString;
81
+ logViewerUrl: z.ZodString;
82
+ requestedBuild: z.ZodString;
83
+ resolvedBuild: z.ZodString;
84
+ createdAt: z.ZodString;
85
+ ttlMinutes: z.ZodNumber;
86
+ minutesLeft: z.ZodNumber;
87
+ lease: z.ZodObject<{
88
+ state: z.ZodEnum<{
89
+ free: "free";
90
+ leased: "leased";
91
+ stale: "stale";
92
+ }>;
93
+ pid: z.ZodNullable<z.ZodNumber>;
94
+ hostname: z.ZodNullable<z.ZodString>;
95
+ startedAt: z.ZodNullable<z.ZodString>;
96
+ holderAlive: z.ZodNullable<z.ZodBoolean>;
97
+ }, z.core.$strip>;
98
+ leaseGuard: z.ZodBoolean;
99
+ health: z.ZodObject<{
100
+ kind: z.ZodEnum<{
101
+ answers: "answers";
102
+ expired: "expired";
103
+ failed: "failed";
104
+ }>;
105
+ status: z.ZodNullable<z.ZodNumber>;
106
+ detail: z.ZodNullable<z.ZodString>;
107
+ }, z.core.$strip>;
108
+ }, z.core.$strip>>;
109
+ unreadable: z.ZodArray<z.ZodObject<{
110
+ file: z.ZodString;
111
+ reason: z.ZodString;
112
+ }, z.core.$strip>>;
113
+ channels: z.ZodArray<z.ZodObject<{
114
+ channel: z.ZodString;
115
+ build: z.ZodString;
116
+ learnedAt: z.ZodString;
117
+ ageMinutes: z.ZodNumber;
118
+ }, z.core.$strip>>;
119
+ }, z.core.$strip>;
120
+ }, {
121
+ listed: unknown;
122
+ }, {
123
+ poolUnreadable: z.ZodObject<{
124
+ path: z.ZodString;
125
+ message: z.ZodString;
126
+ }, z.core.$strip>;
127
+ }, {
128
+ readonly poolUnreadable: {
129
+ readonly exitCode: 1;
130
+ readonly description: "storm couldn't read the pool.";
131
+ readonly schema: z.ZodObject<{
132
+ path: z.ZodString;
133
+ message: z.ZodString;
134
+ }, z.core.$strip>;
135
+ readonly render: ({ path, message }: {
136
+ path: string;
137
+ message: string;
138
+ }) => {
139
+ message: string;
140
+ rows: [string, string][];
141
+ };
142
+ };
143
+ }>>, TestserverCommandApp, false>;
@@ -9,7 +9,7 @@ import { outcomes } from "./outcomes.js";
9
9
  export function command(cmd) {
10
10
  return cmd.define({
11
11
  requestSchema: z.object({}).strict(),
12
- outcomes,
12
+ response: outcomes,
13
13
  handler: async ({ app }) => {
14
14
  const inspected = await inspectPool(app.context);
15
15
  if (!inspected.ok)
@@ -51,4 +51,20 @@ export declare const outcomes: import("@vercel/clinkr").Outcomes<{
51
51
  path: z.ZodString;
52
52
  message: z.ZodString;
53
53
  }, z.core.$strip>;
54
+ }, {
55
+ readonly poolUnreadable: {
56
+ readonly exitCode: 1;
57
+ readonly description: "storm couldn't read the pool.";
58
+ readonly schema: z.ZodObject<{
59
+ path: z.ZodString;
60
+ message: z.ZodString;
61
+ }, z.core.$strip>;
62
+ readonly render: ({ path, message }: {
63
+ path: string;
64
+ message: string;
65
+ }) => {
66
+ message: string;
67
+ rows: [string, string][];
68
+ };
69
+ };
54
70
  }>;
@@ -2,10 +2,10 @@
2
2
  // and default of each option, and the checks across its options. `config` loads and merges against it, and `exec` and
3
3
  // `vitest` check their own `--testserver-<option>` flags with its rules;
4
4
  // the command half (`command.ts`) settles the resolved options into the run's request.
5
- import { checkBoolean, checkNonEmptyString, describeValue, optionKeyPath, readBooleanText, readTextAsIs, readWholeNumberText, wrongValue, } from "../../config/index.js";
5
+ import { checkBoolean, describeValue, optionKeyPath, readBooleanText, readTextAsIs, readWholeNumberText, wrongValue, } from "../../config/index.js";
6
6
  import { parseDuration, parseTtl } from "../../time/index.js";
7
7
  import { MAX_SANDBOXES, MIN_SANDBOXES } from "./options.js";
8
- import { DEFAULT_TTL_MINUTES } from "./sandbox/index.js";
8
+ import { buildReferenceText, DEFAULT_TTL_MINUTES, parseBuildReference } from "./sandbox/index.js";
9
9
  /** The resource's name, its key under `resources`. */
10
10
  const NAME = "testserverPool";
11
11
  function sandboxCount(value) {
@@ -13,6 +13,16 @@ function sandboxCount(value) {
13
13
  ? { ok: true, value }
14
14
  : wrongValue(`must be a whole number from ${MIN_SANDBOXES} to ${MAX_SANDBOXES}, not ${describeValue(value)}`);
15
15
  }
16
+ /**
17
+ * A build, a build channel or a PR build reference, settled: a PR build reference, `pr:<N>` or the vercel/api pull
18
+ * request's URL, becomes `pr:<N>`, so everything after the check sees only that form.
19
+ */
20
+ function checkBuild(value) {
21
+ if (typeof value !== "string" || value.length === 0)
22
+ return wrongValue(`must be a non-empty string, not ${describeValue(value)}`);
23
+ const parsed = parseBuildReference(value);
24
+ return parsed.ok ? { ok: true, value: buildReferenceText(parsed.reference) } : wrongValue(parsed.problem);
25
+ }
16
26
  function duration(value) {
17
27
  return typeof value === "string" && parseDuration(value) !== undefined
18
28
  ? { ok: true, value }
@@ -35,10 +45,10 @@ const KIND = "testserverPool";
35
45
  */
36
46
  export const testserverPoolConfig = {
37
47
  name: NAME,
38
- factory: { kind: KIND, call: "testserverPool({ … })", subpath: "@cnc-tools/storm/testserver" },
48
+ factory: { kind: KIND, call: "testserverPool({ … })", subpath: "@vercel/storm/testserver" },
39
49
  options: {
40
50
  sandboxes: { check: sandboxCount, readText: readWholeNumberText, default: MIN_SANDBOXES },
41
- build: { check: checkNonEmptyString, readText: readTextAsIs, default: "stable" },
51
+ build: { check: checkBuild, readText: readTextAsIs, default: "stable" },
42
52
  minTimeLeft: { check: duration, readText: readTextAsIs, default: "30m" },
43
53
  ttl: { check: ttl, readText: readTextAsIs, default: "5h" },
44
54
  noCreate: { check: checkBoolean, readText: readBooleanText, default: false },
@@ -8,6 +8,10 @@ import type { BuildChannel } from "./sandbox/index.ts";
8
8
  * entries on the channel as written.
9
9
  * - `old-channel-resolution`: under `noCreate`, a build channel resolved from a channel resolution `ageMinutes`
10
10
  * old, past the hour a run trusts one for.
11
+ * - `pr-build-resolved`: the `build` setting was the PR build reference `pr:<pr>`, and the run resolved it to `build`,
12
+ * a PR build made from the vercel/api pull request's head commit `headSha`, found on the newest matching pool entry
13
+ * (`from: "pool"`) or as the newest PR build from the runs of vercel/api's `Testserver` workflow (`from:
14
+ * "workflow"`); `merged` says whether the pull request has merged.
11
15
  * - `leasing`: the run lists the pool on `match` and health-checks its free Testserver sandboxes, to lease up to
12
16
  * `requested`.
13
17
  * - `leased`: the pool's usable Testserver sandboxes were leased, up to `requested`.
@@ -26,6 +30,13 @@ export type TestserverPoolEvent = {
26
30
  readonly channel: BuildChannel;
27
31
  readonly build: string;
28
32
  readonly ageMinutes: number;
33
+ } | {
34
+ readonly kind: "pr-build-resolved";
35
+ readonly pr: number;
36
+ readonly headSha: string;
37
+ readonly build: string;
38
+ readonly merged: boolean;
39
+ readonly from: "pool" | "workflow";
29
40
  } | {
30
41
  readonly kind: "leasing";
31
42
  readonly match: BuildMatch;
@@ -1,12 +1,13 @@
1
1
  // The Testserver pool resource's folder: the Testserver control plane and health check (`sandbox/`), the pool of
2
- // Testserver sandboxes (`pool/`), its kind and run half, its events and its lane, its resource config, its command
3
- // half with its settle, its cleanup and ran reports, its live gateways, how its errors and `pool create` report a
4
- // credential source's failures, its sandbox counts, and the Testserver sandboxes a run holds. Its provider value
2
+ // Testserver sandboxes (`pool/`), a vercel/api pull request's head commit and PR builds from GitHub and the host's
3
+ // GitHub token (`pull-request/`), its kind and run half, its events and its lane, its resource config, its command half
4
+ // with its settle, its cleanup and ran reports, its live gateways, how its errors and `pool create` report a credential
5
+ // source's failures, its sandbox counts, and the Testserver sandboxes a run holds. Its provider value
5
6
  // (`resource-provider.ts`) carries them to the registry (`src/resources.ts`), the one place outside this folder that
6
- // takes it; `@cnc-tools/storm/testserver` is `resource.ts`.
7
- // `sandbox/` and `pool/` keep their own `index.ts` for the files in this folder; everything outside it imports only
8
- // this file: the registry and `src/config-file/`'s type-only import. Its own commands, `pool create` and `pool list`, are
9
- // `commands/`, which the registry mounts. The rest of what it exports is for tests.
7
+ // takes it; `@vercel/storm/testserver` is `resource.ts`.
8
+ // `sandbox/`, `pool/` and `pull-request/` keep their own `index.ts` for the files in this folder; everything outside it
9
+ // imports only this file: the registry and `src/config-file/`'s type-only import. Its own commands, `pool create` and
10
+ // `pool list`, are `commands/`, which the registry mounts. The rest of what it exports is for tests.
10
11
  export { testserverPoolCleanupReport } from "./cleanup.js";
11
12
  export { testserverPoolConfig } from "./config.js";
12
13
  export { testserverPoolCanceledLines, testserverPoolCommand, testserverPoolPrepareErrors, testserverPoolRefusalErrors, } from "./command.js";
@@ -47,6 +47,7 @@ function applyTestserverEvent(testserver, event, at) {
47
47
  // Resolving the build is part of the lane, but changes nothing it shows.
48
48
  case "build-unresolved":
49
49
  case "old-channel-resolution":
50
+ case "pr-build-resolved":
50
51
  case "create-progress":
51
52
  return testserver;
52
53
  case "leasing":
@@ -2,5 +2,7 @@ import { type ResourceHost } from "../../../core/index.ts";
2
2
  import type { TestserverPoolKind } from "../provider.ts";
3
3
  /** The Testserver control plane's origin. */
4
4
  export declare const TESTSERVER_ORIGIN = "https://testserver.vercel.sh";
5
+ /** GitHub's REST API origin. */
6
+ export declare const GITHUB_API_ORIGIN = "https://api.github.com";
5
7
  /** The Testserver pool's gateways, live, for `host`. */
6
8
  export declare function liveTestserverPoolGateways(host: ResourceHost): TestserverPoolKind["gateways"];
@@ -1,13 +1,19 @@
1
1
  // The Testserver pool resource's live gateways, built from the host: the Testserver control plane at
2
- // `testserver.vercel.sh`, with a 3-minute create and a 10-second `GET /builds`; 5-second health checks; the pool
3
- // under the host's `XDG_STATE_HOME` or `HOME`, or, when the host names no place for it, a pool whose every operation
4
- // fails and says why; and the system timer, which a create's retries wait on.
2
+ // `testserver.vercel.sh`, with a 3-minute create and a 10-second `GET /builds`; 5-second
3
+ // health checks; the pool under the host's `XDG_STATE_HOME` or `HOME`, or, when the host names no place for it, a pool
4
+ // whose every operation fails and says why; the system timer, which a create's retries wait on; GitHub's REST API at
5
+ // `api.github.com`, with a 10-second call and a 30-second job log download, for a pull request's head commit and the
6
+ // PR builds vercel/api's `Testserver` workflow published for it; and the host's GitHub token, from its `GITHUB_TOKEN`,
7
+ // `GH_TOKEN` or `gh auth token`.
5
8
  import { resolveStateDirectory } from "../../../core/index.js";
6
9
  import { systemTimer } from "../../../time/index.js";
7
10
  import { livePoolStoreGateway, POOL_DIRECTORY_PATTERN, POOL_SUBPATH, unavailablePoolStoreGateway, } from "../pool/index.js";
11
+ import { liveGitHubTokenGateway, livePullRequestGateway, liveTestserverWorkflowGateway, } from "../pull-request/index.js";
8
12
  import { liveControlPlaneGateway, liveHealthCheckGateway } from "../sandbox/index.js";
9
13
  /** The Testserver control plane's origin. */
10
14
  export const TESTSERVER_ORIGIN = "https://testserver.vercel.sh";
15
+ /** GitHub's REST API origin. */
16
+ export const GITHUB_API_ORIGIN = "https://api.github.com";
11
17
  /** The Testserver pool's gateways, live, for `host`. */
12
18
  export function liveTestserverPoolGateways(host) {
13
19
  const directory = resolveStateDirectory(host.env, POOL_SUBPATH);
@@ -22,5 +28,12 @@ export function liveTestserverPoolGateways(host) {
22
28
  ? livePoolStoreGateway({ directory: directory.directory })
23
29
  : unavailablePoolStoreGateway(POOL_DIRECTORY_PATTERN, directory.reason),
24
30
  timer: systemTimer,
31
+ pullRequests: livePullRequestGateway({ origin: GITHUB_API_ORIGIN, timeoutMs: 10_000 }),
32
+ testserverWorkflow: liveTestserverWorkflowGateway({
33
+ origin: GITHUB_API_ORIGIN,
34
+ timeoutMs: 10_000,
35
+ logTimeoutMs: 30_000,
36
+ }),
37
+ githubToken: liveGitHubTokenGateway({ env: host.env }),
25
38
  };
26
39
  }
@@ -2,6 +2,7 @@ import type { CredentialFailure, CredentialSource } from "../../../credential/in
2
2
  import { type BuildChannel, type BuildsFailure, type ControlPlaneGateway } from "../sandbox/index.ts";
3
3
  import type { Clock } from "../../../time/index.ts";
4
4
  import type { PoolStoreGateway } from "./gateways.ts";
5
+ import type { PrBuildResolution } from "./pull-request.ts";
5
6
  /** How long a channel resolution is trusted without asking the Testserver control plane again. */
6
7
  export declare const CHANNEL_RESOLUTION_TRUST_MINUTES = 60;
7
8
  /**
@@ -30,6 +31,8 @@ export type UnresolvedChannelReason = {
30
31
  * - `control-plane`: a build channel the Testserver control plane resolved just now (`GET /builds`).
31
32
  * - `as-written`: a build channel that couldn't be resolved; pool entries are matched on it as their
32
33
  * requested build instead, and the caller says why.
34
+ * - `pull-request`: a PR build reference, resolved to the newest PR build made from the pull request's head commit
35
+ * (`resolvePrBuild`), never from a channel resolution.
33
36
  */
34
37
  export type BuildResolution = {
35
38
  readonly kind: "named";
@@ -52,7 +55,7 @@ export type BuildResolution = {
52
55
  readonly kind: "as-written";
53
56
  readonly channel: BuildChannel;
54
57
  readonly reason: UnresolvedChannelReason;
55
- };
58
+ } | PrBuildResolution;
56
59
  /** Which pool entries a run may lease: those on a resolved build, or, as a fallback, those requested as written. */
57
60
  export type BuildMatch = {
58
61
  readonly resolvedBuild: string;
@@ -60,7 +63,7 @@ export type BuildMatch = {
60
63
  readonly requestedBuild: string;
61
64
  };
62
65
  /**
63
- * Resolves `build`. A build named directly resolves to itself. A build channel uses its channel resolution
66
+ * Resolves `build`, a build or build channel; a PR build reference is `resolvePrBuild`'s. A build named directly resolves to itself. A build channel uses its channel resolution
64
67
  * when that is less than an hour old; otherwise it gets a credential from `credential` and asks the Testserver
65
68
  * control plane, and records what it learns. A failed record write is ignored: it costs only a later lookup.
66
69
  * Reading the channel resolutions failing counts as having none.
@@ -2,7 +2,7 @@ import { isBuildChannel, resolveChannel, } from "../sandbox/index.js";
2
2
  /** How long a channel resolution is trusted without asking the Testserver control plane again. */
3
3
  export const CHANNEL_RESOLUTION_TRUST_MINUTES = 60;
4
4
  /**
5
- * Resolves `build`. A build named directly resolves to itself. A build channel uses its channel resolution
5
+ * Resolves `build`, a build or build channel; a PR build reference is `resolvePrBuild`'s. A build named directly resolves to itself. A build channel uses its channel resolution
6
6
  * when that is less than an hour old; otherwise it gets a credential from `credential` and asks the Testserver
7
7
  * control plane, and records what it learns. A failed record write is ignored: it costs only a later lookup.
8
8
  * Reading the channel resolutions failing counts as having none.
@@ -25,10 +25,12 @@ export type PooledCreateOutcome = {
25
25
  };
26
26
  /**
27
27
  * Creates one Testserver sandbox on `build` living `ttlMinutes` (see `createSandbox`) and adds it to the pool, its
28
- * entry recording the TTL: free, or, with
28
+ * entry recording the TTL and `requestedBuild`, the build setting as written: for a build or build channel the same
29
+ * as `build`, for a PR build reference `pr:<N>` while `build` is the PR build it resolved to; for a PR build, also
30
+ * `headSha`, the head commit it was made from, by which a later resolution of `pr:<N>` finds it. Free, or, with
29
31
  * `lease`, leased to this process, the lease written before the entry so no other run sees it free. Its
30
32
  * `createdAt` is the clock when the create started, which underestimates the time it has left. The entry is
31
- * written first, as soon as the Testserver sandbox exists; then, on a build channel, the channel resolution
33
+ * written first, as soon as the Testserver sandbox exists; then, when `requestedBuild` is a build channel, the channel resolution
32
34
  * the create showed is recorded. A failed channel record is ignored: it costs only a later lookup.
33
35
  * `progress` hears the Testserver control plane's progress messages.
34
36
  *
@@ -42,7 +44,12 @@ export declare function createPooledSandbox(context: {
42
44
  readonly clock: Clock;
43
45
  }, request: {
44
46
  readonly credential: Credential;
47
+ /** The build the control plane creates on: a build or build channel. */
45
48
  readonly build: string;
49
+ /** What the build setting asked for, which the pool entry records as its requested build. */
50
+ readonly requestedBuild: string;
51
+ /** For a PR build reference, the head commit the PR build `build` was made from, which the pool entry records. */
52
+ readonly headSha?: string;
46
53
  readonly ttlMinutes: number;
47
54
  readonly lease: boolean;
48
55
  }, progress?: CreateProgress): Promise<PooledCreateOutcome>;
@@ -2,10 +2,12 @@ import { createSandbox, isBuildChannel, } from "../sandbox/index.js";
2
2
  import { sandboxHost } from "./host.js";
3
3
  /**
4
4
  * Creates one Testserver sandbox on `build` living `ttlMinutes` (see `createSandbox`) and adds it to the pool, its
5
- * entry recording the TTL: free, or, with
5
+ * entry recording the TTL and `requestedBuild`, the build setting as written: for a build or build channel the same
6
+ * as `build`, for a PR build reference `pr:<N>` while `build` is the PR build it resolved to; for a PR build, also
7
+ * `headSha`, the head commit it was made from, by which a later resolution of `pr:<N>` finds it. Free, or, with
6
8
  * `lease`, leased to this process, the lease written before the entry so no other run sees it free. Its
7
9
  * `createdAt` is the clock when the create started, which underestimates the time it has left. The entry is
8
- * written first, as soon as the Testserver sandbox exists; then, on a build channel, the channel resolution
10
+ * written first, as soon as the Testserver sandbox exists; then, when `requestedBuild` is a build channel, the channel resolution
9
11
  * the create showed is recorded. A failed channel record is ignored: it costs only a later lookup.
10
12
  * `progress` hears the Testserver control plane's progress messages.
11
13
  *
@@ -29,15 +31,16 @@ export async function createPooledSandbox(context, request, progress = {}) {
29
31
  logViewerUrl,
30
32
  createdAt: startedAt.toISOString(),
31
33
  ttlMinutes,
32
- requestedBuild: request.build,
34
+ requestedBuild: request.requestedBuild,
33
35
  resolvedBuild,
36
+ ...(request.headSha === undefined ? {} : { headSha: request.headSha }),
34
37
  };
35
38
  const leaseOptions = request.lease ? { lease: { startedAt: context.clock.now().toISOString() } } : {};
36
39
  const added = await context.pool.addEntry(entry, leaseOptions);
37
40
  if (!added.ok)
38
41
  return { kind: "pool-write-failed", entry, failure: added.failure };
39
- if (isBuildChannel(request.build))
40
- await context.pool.writeChannelResolution(request.build, {
42
+ if (isBuildChannel(request.requestedBuild))
43
+ await context.pool.writeChannelResolution(request.requestedBuild, {
41
44
  build: resolvedBuild,
42
45
  learnedAt: context.clock.now().toISOString(),
43
46
  });
@@ -12,6 +12,12 @@ export interface PoolEntry {
12
12
  readonly ttlMinutes: number;
13
13
  readonly requestedBuild: string;
14
14
  readonly resolvedBuild: string;
15
+ /**
16
+ * For a Testserver sandbox on a PR build, created for the PR build reference `requestedBuild`, the pull request's
17
+ * head commit that PR build was made from, 40 lowercase hex characters; absent otherwise, and on entries written
18
+ * before storm recorded it.
19
+ */
20
+ readonly headSha?: string;
15
21
  }
16
22
  /** A lease as its file records it: the process holding it, on which host, since when. */
17
23
  export interface LeaseRecord {
@@ -1,4 +1,5 @@
1
1
  export { type BuildMatch, buildMatch, type BuildResolution, resolveBuild, type UnresolvedChannelReason, } from "./channels.ts";
2
+ export { buildTime, PR_BUILD_LIFETIME_MS, type PrBuildContext, type PrBuildOutcome, type PrBuildRefusal, type PrBuildResolution, resolvePrBuild, } from "./pull-request.ts";
2
3
  export { createPooledSandbox, type PooledCreateOutcome } from "./create.ts";
3
4
  export { POOL_DIRECTORY_PATTERN, POOL_SUBPATH } from "./directory.ts";
4
5
  export type { ChannelResolution, DropOutcome, LeaseHolder, LeaseOutcome, LeaseRecord, ListedEntry, ObservedLease, PoolEntry, PoolListing, PoolStoreFailure, PoolStoreGateway, PoolStoreOperation, PoolStoreResult, ReclaimOutcome, ReleaseOutcome, UnreadableEntry, } from "./gateways.ts";
@@ -1,6 +1,7 @@
1
1
  // The Testserver folder's `pool/` barrel, for the other files in `resources/testserver/`. Code outside that folder
2
2
  // imports `resources/testserver/index.ts` instead.
3
3
  export { buildMatch, resolveBuild, } from "./channels.js";
4
+ export { buildTime, PR_BUILD_LIFETIME_MS, resolvePrBuild, } from "./pull-request.js";
4
5
  export { createPooledSandbox } from "./create.js";
5
6
  export { POOL_DIRECTORY_PATTERN, POOL_SUBPATH } from "./directory.js";
6
7
  export { sandboxHost } from "./host.js";
@@ -9,6 +9,10 @@ const entrySchema = z.object({
9
9
  ttlMinutes: z.number().int().positive(),
10
10
  requestedBuild: z.string().min(1),
11
11
  resolvedBuild: z.string().min(1),
12
+ headSha: z
13
+ .string()
14
+ .regex(/^[0-9a-f]{40}$/)
15
+ .optional(),
12
16
  });
13
17
  const leaseSchema = z.object({
14
18
  version: z.literal(1),
@@ -25,8 +29,8 @@ export function parseEntry(text) {
25
29
  const parsed = parseVersioned(text, entrySchema);
26
30
  if (!parsed.ok)
27
31
  return parsed;
28
- const { version: _version, ...entry } = parsed.value;
29
- return { ok: true, value: entry };
32
+ const { version: _version, headSha, ...entry } = parsed.value;
33
+ return { ok: true, value: { ...entry, ...(headSha === undefined ? {} : { headSha }) } };
30
34
  }
31
35
  /** The lease a lease file holds, or why it holds none. */
32
36
  export function parseLease(text) {
@@ -0,0 +1,96 @@
1
+ import type { GitHubFailure, GitHubTokenGateway, GitHubTokenOrigin, PullRequestGateway, TestserverWorkflowGateway } from "../pull-request/index.ts";
2
+ import type { Clock } from "../../../time/index.ts";
3
+ import type { PoolStoreGateway } from "./gateways.ts";
4
+ /** How long the Testserver control plane creates Testserver sandboxes from a PR build after it was published. */
5
+ export declare const PR_BUILD_LIFETIME_MS: number;
6
+ /**
7
+ * Why a PR build reference couldn't be resolved.
8
+ *
9
+ * - `pull-request-unreadable`: the host has no GitHub token (`no-token`), or GitHub didn't give the pull request's head
10
+ * commit; `tokenOrigin` says where the token came from, when there was one.
11
+ * - `pr-builds-unlisted`: GitHub didn't give the runs of vercel/api's `Testserver` workflow on the pull request, or the
12
+ * PR build one of them published.
13
+ * - `pr-build-not-ready`: no run for the pull request's head commit has published a PR build; `newest` is the newest PR
14
+ * build a run for an older head commit published, if any.
15
+ * - `pr-build-expired`: the newest PR build of the head commit is past its `expiresAt`.
16
+ */
17
+ export type PrBuildRefusal = {
18
+ readonly kind: "pull-request-unreadable";
19
+ readonly pr: number;
20
+ readonly failure: {
21
+ readonly kind: "no-token";
22
+ } | GitHubFailure;
23
+ readonly tokenOrigin?: GitHubTokenOrigin;
24
+ } | {
25
+ readonly kind: "pr-builds-unlisted";
26
+ readonly pr: number;
27
+ readonly failure: GitHubFailure;
28
+ } | {
29
+ readonly kind: "pr-build-not-ready";
30
+ readonly pr: number;
31
+ readonly headSha: string;
32
+ readonly newest: {
33
+ readonly build: string;
34
+ readonly headSha: string;
35
+ readonly createdAt: string;
36
+ } | undefined;
37
+ } | {
38
+ readonly kind: "pr-build-expired";
39
+ readonly pr: number;
40
+ readonly headSha: string;
41
+ readonly build: string;
42
+ readonly expiresAt: string;
43
+ };
44
+ /**
45
+ * A PR build reference resolved: pull request `pr`'s head commit, a PR build made from it, whether the pull request
46
+ * has merged, and where the PR build was found (`from`): `pool`, on the newest pool entry created for `pr:<pr>` on that
47
+ * head commit, or `workflow`, the newest PR build from the runs of vercel/api's `Testserver` workflow.
48
+ */
49
+ export interface PrBuildResolution {
50
+ readonly kind: "pull-request";
51
+ readonly pr: number;
52
+ readonly headSha: string;
53
+ readonly build: string;
54
+ readonly merged: boolean;
55
+ readonly from: "pool" | "workflow";
56
+ }
57
+ /** How resolving a PR build reference ended. */
58
+ export type PrBuildOutcome = {
59
+ readonly ok: true;
60
+ readonly resolution: PrBuildResolution;
61
+ } | {
62
+ readonly ok: false;
63
+ readonly refusal: PrBuildRefusal;
64
+ };
65
+ /** What resolving a PR build reference reads: GitHub, with the host's GitHub token, the pool, and the clock. */
66
+ export interface PrBuildContext {
67
+ readonly pool: PoolStoreGateway;
68
+ readonly pullRequests: PullRequestGateway;
69
+ readonly testserverWorkflow: TestserverWorkflowGateway;
70
+ readonly githubToken: GitHubTokenGateway;
71
+ readonly clock: Clock;
72
+ }
73
+ /**
74
+ * Resolves the PR build reference `pr:<pr>` to a PR build made from the pull request's current head commit: the PR
75
+ * build of the newest matching pool entry, or else the newest PR build the workflow published. In order, it:
76
+ *
77
+ * 1. finds a GitHub token on the host; with none it refuses (`pull-request-unreadable`, `no-token`);
78
+ * 2. reads the pull request's head commit and branch with it; a failure refuses (`pull-request-unreadable`);
79
+ * 3. takes the PR build of the newest pool entry created for `pr:<pr>` on that head commit, when that PR build hasn't
80
+ * expired (`from: "pool"`); this reads nothing more from GitHub;
81
+ * 4. otherwise lists the runs of vercel/api's `Testserver` workflow on that branch; a failure refuses (`pr-builds-unlisted`);
82
+ * 5. takes, of the runs for the head commit, newest first, the first whose `Bundle & Publish` job published a PR build
83
+ * of this pull request; reading one failing refuses (`pr-builds-unlisted`), and with none it refuses
84
+ * (`pr-build-not-ready`), naming the newest PR build of the up to 3 newest runs for older head commits;
85
+ * 6. refuses one published 7 days ago or more, by the time its name gives (`pr-build-expired`).
86
+ *
87
+ * GitHub's runs carry the head commit each was for, so the PR build is the head's however the workflow built it; a pool
88
+ * entry records the head commit its PR build was made from, so the pool's is the head's too. It needs no credential:
89
+ * the Testserver control plane isn't asked. The GitHub token goes only to GitHub. It reads and writes no channel
90
+ * resolution: a PR build reference's head commit is read on every run.
91
+ */
92
+ export declare function resolvePrBuild(context: PrBuildContext, request: {
93
+ readonly pr: number;
94
+ }): Promise<PrBuildOutcome>;
95
+ /** The UTC time a build's name starts with, `<YYYYMMDD>T<HHMMSS>Z__…`, as an ISO string; undefined without one. */
96
+ export declare function buildTime(name: string): string | undefined;