@mastra/factory 0.28.0-alpha.8 → 0.28.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 (184) hide show
  1. package/README.md +61 -42
  2. package/dist/boards/board-writes.d.ts +3 -3
  3. package/dist/boards/board-writes.d.ts.map +1 -1
  4. package/dist/boards/board-writes.js +24 -20
  5. package/dist/boards/board-writes.js.map +1 -1
  6. package/dist/boards/project-boards.d.ts +20 -0
  7. package/dist/boards/project-boards.d.ts.map +1 -1
  8. package/dist/boards/project-boards.js +35 -11
  9. package/dist/boards/project-boards.js.map +1 -1
  10. package/dist/boards/registry.d.ts.map +1 -1
  11. package/dist/boards/registry.js +0 -3
  12. package/dist/boards/registry.js.map +1 -1
  13. package/dist/capabilities/version-control.d.ts +6 -1
  14. package/dist/capabilities/version-control.d.ts.map +1 -1
  15. package/dist/environment/build-runner.d.ts +13 -2
  16. package/dist/environment/build-runner.d.ts.map +1 -1
  17. package/dist/environment/build-runner.js +42 -0
  18. package/dist/environment/build-runner.js.map +1 -1
  19. package/dist/environment/build-schedule.js +1 -1
  20. package/dist/environment/build-schedule.js.map +1 -1
  21. package/dist/environment/build-workflow.d.ts +6 -4
  22. package/dist/environment/build-workflow.d.ts.map +1 -1
  23. package/dist/environment/build-workflow.js +70 -66
  24. package/dist/environment/build-workflow.js.map +1 -1
  25. package/dist/environment/build.d.ts +22 -7
  26. package/dist/environment/build.d.ts.map +1 -1
  27. package/dist/environment/build.js +73 -15
  28. package/dist/environment/build.js.map +1 -1
  29. package/dist/environment/heads.d.ts +1 -1
  30. package/dist/environment/heads.d.ts.map +1 -1
  31. package/dist/environment/heads.js +4 -1
  32. package/dist/environment/heads.js.map +1 -1
  33. package/dist/environment/repository-access.d.ts +13 -0
  34. package/dist/environment/repository-access.d.ts.map +1 -0
  35. package/dist/environment/repository-access.js +55 -0
  36. package/dist/environment/repository-access.js.map +1 -0
  37. package/dist/factory.d.ts +5 -2
  38. package/dist/factory.d.ts.map +1 -1
  39. package/dist/factory.js +32 -26
  40. package/dist/factory.js.map +1 -1
  41. package/dist/integrations/github/sandbox-release.d.ts +12 -0
  42. package/dist/integrations/github/sandbox-release.d.ts.map +1 -1
  43. package/dist/integrations/github/sandbox-release.js +16 -1
  44. package/dist/integrations/github/sandbox-release.js.map +1 -1
  45. package/dist/integrations/platform/github/event-worker.js.map +1 -1
  46. package/dist/integrations/platform/github/integration.d.ts.map +1 -1
  47. package/dist/integrations/platform/github/integration.js +137 -105
  48. package/dist/integrations/platform/github/integration.js.map +1 -1
  49. package/dist/integrations/platform/jira/integration.d.ts.map +1 -1
  50. package/dist/integrations/platform/jira/integration.js +5 -1
  51. package/dist/integrations/platform/jira/integration.js.map +1 -1
  52. package/dist/integrations/slack/integration.d.ts.map +1 -1
  53. package/dist/integrations/slack/integration.js +7 -4
  54. package/dist/integrations/slack/integration.js.map +1 -1
  55. package/dist/mcp/routes.d.ts.map +1 -1
  56. package/dist/mcp/routes.js +3 -5
  57. package/dist/mcp/routes.js.map +1 -1
  58. package/dist/routes/board-snapshot.d.ts +17 -1
  59. package/dist/routes/board-snapshot.d.ts.map +1 -1
  60. package/dist/routes/board-snapshot.js +54 -7
  61. package/dist/routes/board-snapshot.js.map +1 -1
  62. package/dist/routes/boards.d.ts +2 -3
  63. package/dist/routes/boards.d.ts.map +1 -1
  64. package/dist/routes/boards.js.map +1 -1
  65. package/dist/routes/contracts.d.ts +102 -0
  66. package/dist/routes/contracts.d.ts.map +1 -1
  67. package/dist/routes/contracts.js +96 -6
  68. package/dist/routes/contracts.js.map +1 -1
  69. package/dist/routes/projects.d.ts.map +1 -1
  70. package/dist/routes/projects.js +5 -0
  71. package/dist/routes/projects.js.map +1 -1
  72. package/dist/routes/sandbox-envs.d.ts +2 -0
  73. package/dist/routes/sandbox-envs.d.ts.map +1 -1
  74. package/dist/routes/sandbox-envs.js +3 -0
  75. package/dist/routes/sandbox-envs.js.map +1 -1
  76. package/dist/routes/source-control-settings.d.ts +2 -0
  77. package/dist/routes/source-control-settings.d.ts.map +1 -1
  78. package/dist/routes/source-control-settings.js +5 -0
  79. package/dist/routes/source-control-settings.js.map +1 -1
  80. package/dist/routes/surface.d.ts +2 -0
  81. package/dist/routes/surface.d.ts.map +1 -1
  82. package/dist/routes/surface.js +2 -1
  83. package/dist/routes/surface.js.map +1 -1
  84. package/dist/routes/work-item-activity.d.ts +11 -0
  85. package/dist/routes/work-item-activity.d.ts.map +1 -0
  86. package/dist/routes/work-item-activity.js +25 -0
  87. package/dist/routes/work-item-activity.js.map +1 -0
  88. package/dist/routes/work-item-selection.d.ts +18 -0
  89. package/dist/routes/work-item-selection.d.ts.map +1 -0
  90. package/dist/routes/work-item-selection.js +61 -0
  91. package/dist/routes/work-item-selection.js.map +1 -0
  92. package/dist/routes/work-item-starters.d.ts +29 -0
  93. package/dist/routes/work-item-starters.d.ts.map +1 -0
  94. package/dist/routes/work-item-starters.js +169 -0
  95. package/dist/routes/work-item-starters.js.map +1 -0
  96. package/dist/routes/work-items.d.ts.map +1 -1
  97. package/dist/routes/work-items.js +162 -15
  98. package/dist/routes/work-items.js.map +1 -1
  99. package/dist/rules/dispatch-errors.d.ts.map +1 -1
  100. package/dist/rules/dispatch-errors.js +4 -0
  101. package/dist/rules/dispatch-errors.js.map +1 -1
  102. package/dist/rules/dispatcher.d.ts +2 -1
  103. package/dist/rules/dispatcher.d.ts.map +1 -1
  104. package/dist/rules/dispatcher.js +84 -16
  105. package/dist/rules/dispatcher.js.map +1 -1
  106. package/dist/rules/processor.d.ts +2 -0
  107. package/dist/rules/processor.d.ts.map +1 -1
  108. package/dist/rules/processor.js +18 -4
  109. package/dist/rules/processor.js.map +1 -1
  110. package/dist/rules/transition-service.d.ts +20 -2
  111. package/dist/rules/transition-service.d.ts.map +1 -1
  112. package/dist/rules/transition-service.js +40 -18
  113. package/dist/rules/transition-service.js.map +1 -1
  114. package/dist/sandbox/context-sandboxes.d.ts +70 -0
  115. package/dist/sandbox/context-sandboxes.d.ts.map +1 -0
  116. package/dist/sandbox/context-sandboxes.js +120 -0
  117. package/dist/sandbox/context-sandboxes.js.map +1 -0
  118. package/dist/sandbox/session-retirement.d.ts +11 -4
  119. package/dist/sandbox/session-retirement.d.ts.map +1 -1
  120. package/dist/sandbox/session-retirement.js +39 -13
  121. package/dist/sandbox/session-retirement.js.map +1 -1
  122. package/dist/session/live-sessions.d.ts +1 -1
  123. package/dist/session/live-sessions.d.ts.map +1 -1
  124. package/dist/session/live-sessions.js +12 -2
  125. package/dist/session/live-sessions.js.map +1 -1
  126. package/dist/storage/domains/boards/base.d.ts +25 -5
  127. package/dist/storage/domains/boards/base.d.ts.map +1 -1
  128. package/dist/storage/domains/boards/base.js +57 -20
  129. package/dist/storage/domains/boards/base.js.map +1 -1
  130. package/dist/storage/domains/projects/base.d.ts +8 -2
  131. package/dist/storage/domains/projects/base.d.ts.map +1 -1
  132. package/dist/storage/domains/projects/base.js +21 -4
  133. package/dist/storage/domains/projects/base.js.map +1 -1
  134. package/dist/storage/domains/work-items/base.d.ts +47 -6
  135. package/dist/storage/domains/work-items/base.d.ts.map +1 -1
  136. package/dist/storage/domains/work-items/base.js +161 -17
  137. package/dist/storage/domains/work-items/base.js.map +1 -1
  138. package/dist/storage/domains/work-items/cards.d.ts +1 -0
  139. package/dist/storage/domains/work-items/cards.d.ts.map +1 -1
  140. package/dist/storage/domains/work-items/cards.js +32 -8
  141. package/dist/storage/domains/work-items/cards.js.map +1 -1
  142. package/dist/storage/domains/work-items/legacy-review.d.ts +19 -1
  143. package/dist/storage/domains/work-items/legacy-review.d.ts.map +1 -1
  144. package/dist/storage/domains/work-items/legacy-review.js +62 -3
  145. package/dist/storage/domains/work-items/legacy-review.js.map +1 -1
  146. package/dist/storage/domains/work-items/work-item-schema.d.ts +1 -0
  147. package/dist/storage/domains/work-items/work-item-schema.d.ts.map +1 -1
  148. package/dist/storage/domains/work-items/work-item-schema.js +46 -18
  149. package/dist/storage/domains/work-items/work-item-schema.js.map +1 -1
  150. package/dist/tools/annotations.d.ts.map +1 -1
  151. package/dist/tools/annotations.js +4 -0
  152. package/dist/tools/annotations.js.map +1 -1
  153. package/dist/tools/audit-events-tool.d.ts +29 -0
  154. package/dist/tools/audit-events-tool.d.ts.map +1 -0
  155. package/dist/tools/audit-events-tool.js +180 -0
  156. package/dist/tools/audit-events-tool.js.map +1 -0
  157. package/dist/tools/index.d.ts +2 -1
  158. package/dist/tools/index.d.ts.map +1 -1
  159. package/dist/tools/index.js +2 -0
  160. package/dist/tools/index.js.map +1 -1
  161. package/dist/tools/permissions.d.ts.map +1 -1
  162. package/dist/tools/permissions.js +1 -0
  163. package/dist/tools/permissions.js.map +1 -1
  164. package/dist/tools/sessions-state-processor.js +1 -1
  165. package/dist/tools/sessions-state-processor.js.map +1 -1
  166. package/dist/work-items/board-check.d.ts +11 -0
  167. package/dist/work-items/board-check.d.ts.map +1 -0
  168. package/dist/work-items/board-check.js +20 -0
  169. package/dist/work-items/board-check.js.map +1 -0
  170. package/dist/work-items/related-cards.d.ts +21 -0
  171. package/dist/work-items/related-cards.d.ts.map +1 -0
  172. package/dist/work-items/related-cards.js +45 -0
  173. package/dist/work-items/related-cards.js.map +1 -0
  174. package/dist/work-items/split-migration.d.ts.map +1 -1
  175. package/dist/work-items/split-migration.js +6 -2
  176. package/dist/work-items/split-migration.js.map +1 -1
  177. package/dist/workspace.d.ts +0 -1
  178. package/dist/workspace.d.ts.map +1 -1
  179. package/dist/workspace.js +49 -32
  180. package/dist/workspace.js.map +1 -1
  181. package/factory-skills/factory-build/SKILL.md +1 -1
  182. package/factory-skills/factory-self-review/SKILL.md +1 -1
  183. package/factory-skills/factory-triage/SKILL.md +4 -4
  184. package/package.json +16 -16
@@ -3,20 +3,20 @@
3
3
  * build (`runEnvironmentBuild`), step two polls the provider until the build
4
4
  * settles and pins the active template on the project row. Registered by
5
5
  * `MastraFactory.prepare()` only when the sandbox has `builds`, and driven by
6
- * the Build now route, the per-project cron schedule and the GitHub push
7
- * hand-off. Saving settings never starts it.
6
+ * the Build now route, the per-project cron schedule, the GitHub push
7
+ * hand-off and saved template inputs.
8
8
  */
9
9
  import { z } from 'zod';
10
10
  import type { EnvironmentBuildDeps, EnvironmentBuildOutcome } from './build.js';
11
11
  export declare const ENVIRONMENT_BUILD_WORKFLOW_ID = "factory-environment-build";
12
12
  export declare const DEFAULT_BUILD_POLL_MS = 15000;
13
- export declare const MAX_BUILD_WAIT_MS: number;
14
13
  export declare const environmentBuildInputSchema: z.ZodObject<{
15
14
  projectId: z.ZodString;
16
15
  trigger: z.ZodEnum<{
17
16
  manual: "manual";
18
17
  push: "push";
19
18
  schedule: "schedule";
19
+ settings: "settings";
20
20
  }>;
21
21
  }, z.core.$strip>;
22
22
  export declare const environmentBuildOutputSchema: z.ZodObject<{
@@ -42,10 +42,12 @@ export interface EnvironmentBuildWorkflowOptions {
42
42
  maxWaitMs?: number;
43
43
  /** Called with the first step's answer so a caller can return it without waiting for the poll. */
44
44
  onStart?: (runId: string, outcome: EnvironmentBuildOutcome) => void;
45
+ /** Called once a run is over, whoever started it (a route, the scheduler or a hand-off). */
46
+ onSettled?: (projectId: string, result: EnvironmentBuildWorkflowResult) => void;
45
47
  }
46
48
  export declare function createEnvironmentBuildWorkflow(deps: EnvironmentBuildDeps, options?: EnvironmentBuildWorkflowOptions): import("@mastra/core/workflows").Workflow<import("@mastra/core/workflows").DefaultEngineType, import("@mastra/core/workflows").Step<string, unknown, unknown, unknown, unknown, unknown, any, unknown>[], "factory-environment-build", unknown, {
47
49
  projectId: string;
48
- trigger: "manual" | "push" | "schedule";
50
+ trigger: "manual" | "push" | "schedule" | "settings";
49
51
  }, {
50
52
  outcome: "failed" | "skipped" | "started" | "unavailable";
51
53
  buildId?: string | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"build-workflow.d.ts","sourceRoot":"","sources":["../../src/environment/build-workflow.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,CAAC,EAAE,MA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8B,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,4BAA4B,CAAC,CAAC;AAE1F,MAAM,WAAW,+BAA+B;IAC9C,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,kGAAkG;IAClG,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,uBAAuB,KAAK,IAAI,CAAC;IACpE,4FAA4F;IAC5F,SAAS,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B,KAAK,IAAI,CAAC;CACjF;AAED,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,oBAAoB,EAC1B,OAAO,GAAE,+BAAoC;;;;;;;;;;;;;;;eAmG9C"}
@@ -9,12 +9,11 @@ import { createStep, createWorkflow } from "@mastra/core/workflows";
9
9
  * build (`runEnvironmentBuild`), step two polls the provider until the build
10
10
  * settles and pins the active template on the project row. Registered by
11
11
  * `MastraFactory.prepare()` only when the sandbox has `builds`, and driven by
12
- * the Build now route, the per-project cron schedule and the GitHub push
13
- * hand-off. Saving settings never starts it.
12
+ * the Build now route, the per-project cron schedule, the GitHub push
13
+ * hand-off and saved template inputs.
14
14
  */
15
15
  const ENVIRONMENT_BUILD_WORKFLOW_ID = "factory-environment-build";
16
16
  const DEFAULT_BUILD_POLL_MS = 15e3;
17
- const MAX_BUILD_WAIT_MS = 18e5;
18
17
  const environmentBuildInputSchema = z.object({
19
18
  projectId: z.string().min(1),
20
19
  trigger: z.enum(ENVIRONMENT_BUILD_TRIGGERS)
@@ -67,75 +66,80 @@ function createEnvironmentBuildWorkflow(deps, options = {}) {
67
66
  };
68
67
  }
69
68
  });
70
- const awaitBuild = createStep({
71
- id: "await-build",
72
- inputSchema: startOutputSchema,
73
- outputSchema: environmentBuildOutputSchema,
74
- execute: async ({ inputData }) => {
75
- const { projectId, heads, ...result } = inputData;
76
- if (result.outcome !== "started" || !result.buildId || !heads) return result;
77
- const builds = deps.sandbox?.builds;
78
- const project = await deps.projects.getById({ id: projectId });
79
- if (!builds || !deps.sourceControl || !project) return {
80
- ...result,
81
- status: "unknown"
82
- };
83
- const environment = await resolveProjectEnvironment(deps.sourceControl.storage, project);
84
- const { ctx } = await environmentBuildContextWithEnv(project, environment, heads, deps.sourceControl.versionControl, deps.sandboxEnvs);
85
- const startedAt = now().getTime();
86
- try {
87
- for (;;) {
88
- const build = await builds.get(ctx, environment.settings, result.buildId);
89
- if (build.status === "ready") {
90
- const templateId = build.templateId ?? result.templateId ?? result.buildId;
91
- const pinned = await deps.projects.pinActiveTemplate({
92
- id: project.id,
93
- buildId: result.buildId,
94
- templateId,
95
- heads
96
- });
97
- deps.logger?.info(pinned ? "environment build ready" : "environment build ready but superseded", {
98
- factoryProjectId: project.id,
99
- templateId
100
- });
101
- return {
102
- ...result,
103
- templateId,
104
- status: "ready"
105
- };
106
- }
107
- if (build.status === "failed" || build.status === "unknown") {
108
- deps.logger?.info("environment build did not complete", {
109
- factoryProjectId: project.id,
110
- buildId: result.buildId,
111
- status: build.status,
112
- ...build.error ? { reason: maskSandboxEnvValues(redactCredentials(build.error), ctx.buildEnv) } : {}
113
- });
114
- return {
115
- ...result,
116
- status: build.status,
117
- ...build.error ? { reason: maskSandboxEnvValues(redactCredentials(build.error), ctx.buildEnv) } : {}
118
- };
119
- }
120
- if (now().getTime() - startedAt >= maxWaitMs) return {
69
+ const pollUntilSettled = async (inputData) => {
70
+ const { projectId, heads, ...result } = inputData;
71
+ if (result.outcome !== "started" || !result.buildId || !heads) return result;
72
+ const builds = deps.sandbox?.builds;
73
+ const project = await deps.projects.getById({ id: projectId });
74
+ if (!builds || !deps.sourceControl || !project) return {
75
+ ...result,
76
+ status: "unknown"
77
+ };
78
+ const environment = await resolveProjectEnvironment(deps.sourceControl.storage, project);
79
+ const { ctx } = await environmentBuildContextWithEnv(project, environment, heads, deps.sourceControl.versionControl, deps.sandboxEnvs);
80
+ const startedAt = now().getTime();
81
+ try {
82
+ for (;;) {
83
+ const build = await builds.get(ctx, environment.settings, result.buildId);
84
+ if (build.status === "ready") {
85
+ const templateId = build.templateId ?? result.templateId ?? result.buildId;
86
+ const pinned = await deps.projects.pinActiveTemplate({
87
+ id: project.id,
88
+ buildId: result.buildId,
89
+ templateId,
90
+ heads
91
+ });
92
+ deps.logger?.info(pinned ? "environment build ready" : "environment build ready but superseded", {
93
+ factoryProjectId: project.id,
94
+ templateId
95
+ });
96
+ return {
97
+ ...result,
98
+ templateId,
99
+ status: "ready"
100
+ };
101
+ }
102
+ if (build.status === "failed" || build.status === "unknown") {
103
+ deps.logger?.info("environment build did not complete", {
104
+ factoryProjectId: project.id,
105
+ buildId: result.buildId,
106
+ status: build.status,
107
+ ...build.error ? { reason: maskSandboxEnvValues(redactCredentials(build.error), ctx.buildEnv) } : {}
108
+ });
109
+ return {
121
110
  ...result,
122
- status: "unknown",
123
- reason: "Build wait timed out."
111
+ status: build.status,
112
+ ...build.error ? { reason: maskSandboxEnvValues(redactCredentials(build.error), ctx.buildEnv) } : {}
124
113
  };
125
- await sleep(pollMs);
126
114
  }
127
- } catch (error) {
128
- const reason = maskSandboxEnvValues(errorReason(error), ctx.buildEnv);
129
- deps.logger?.info("environment build status read failed", {
130
- factoryProjectId: project.id,
131
- reason
132
- });
133
- return {
115
+ if (now().getTime() - startedAt >= maxWaitMs) return {
134
116
  ...result,
135
117
  status: "unknown",
136
- reason
118
+ reason: "Build wait timed out."
137
119
  };
120
+ await sleep(pollMs);
138
121
  }
122
+ } catch (error) {
123
+ const reason = maskSandboxEnvValues(errorReason(error), ctx.buildEnv);
124
+ deps.logger?.info("environment build status read failed", {
125
+ factoryProjectId: project.id,
126
+ reason
127
+ });
128
+ return {
129
+ ...result,
130
+ status: "unknown",
131
+ reason
132
+ };
133
+ }
134
+ };
135
+ const awaitBuild = createStep({
136
+ id: "await-build",
137
+ inputSchema: startOutputSchema,
138
+ outputSchema: environmentBuildOutputSchema,
139
+ execute: async ({ inputData }) => {
140
+ const settled = await pollUntilSettled(inputData);
141
+ options.onSettled?.(inputData.projectId, settled);
142
+ return settled;
139
143
  }
140
144
  });
141
145
  return createWorkflow({
@@ -144,6 +148,6 @@ function createEnvironmentBuildWorkflow(deps, options = {}) {
144
148
  outputSchema: environmentBuildOutputSchema
145
149
  }).then(start).then(awaitBuild).commit();
146
150
  }
147
- export { DEFAULT_BUILD_POLL_MS, ENVIRONMENT_BUILD_WORKFLOW_ID, MAX_BUILD_WAIT_MS, createEnvironmentBuildWorkflow, environmentBuildInputSchema, environmentBuildOutputSchema };
151
+ export { DEFAULT_BUILD_POLL_MS, ENVIRONMENT_BUILD_WORKFLOW_ID, createEnvironmentBuildWorkflow, environmentBuildInputSchema, environmentBuildOutputSchema };
148
152
 
149
153
  //# sourceMappingURL=build-workflow.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-workflow.js","names":[],"sources":["../../src/environment/build-workflow.ts"],"sourcesContent":["/**\n * The `factory-environment-build` workflow: step one decides and starts a\n * build (`runEnvironmentBuild`), step two polls the provider until the build\n * settles and pins the active template on the project row. Registered by\n * `MastraFactory.prepare()` only when the sandbox has `builds`, and driven by\n * the Build now route, the per-project cron schedule and the GitHub push\n * hand-off. Saving settings never starts it.\n */\n\nimport { createStep, createWorkflow } from '@mastra/core/workflows';\nimport { z } from 'zod';\nimport { maskSandboxEnvValues } from '../sandbox/sandbox-env.js';\nimport { resolveProjectEnvironment } from '../workspace.js';\nimport {\n ENVIRONMENT_BUILD_TRIGGERS,\n environmentBuildContextWithEnv,\n errorReason,\n redactCredentials,\n runEnvironmentBuild,\n} from './build.js';\nimport type { EnvironmentBuildDeps, EnvironmentBuildOutcome } from './build.js';\n\nexport const ENVIRONMENT_BUILD_WORKFLOW_ID = 'factory-environment-build';\nexport const DEFAULT_BUILD_POLL_MS = 15_000;\nexport const MAX_BUILD_WAIT_MS = 30 * 60_000;\n\nexport const environmentBuildInputSchema = z.object({\n projectId: z.string().min(1),\n trigger: z.enum(ENVIRONMENT_BUILD_TRIGGERS),\n});\n\nconst startOutputSchema = z.object({\n projectId: z.string(),\n outcome: z.enum(['started', 'skipped', 'unavailable', 'failed']),\n buildId: z.string().optional(),\n templateId: z.string().optional(),\n reason: z.string().optional(),\n heads: z.record(z.string(), z.string()).optional(),\n});\n\nexport const environmentBuildOutputSchema = z.object({\n outcome: z.enum(['started', 'skipped', 'unavailable', 'failed']),\n buildId: z.string().optional(),\n templateId: z.string().optional(),\n reason: z.string().optional(),\n /** Where the started build ended: `ready`, `failed`, or `unknown` when the provider lost it or the wait ran out. */\n status: z.enum(['ready', 'failed', 'unknown']).optional(),\n});\n\nexport type EnvironmentBuildWorkflowResult = z.infer<typeof environmentBuildOutputSchema>;\n\nexport interface EnvironmentBuildWorkflowOptions {\n sleep?: (ms: number) => Promise<void>;\n pollMs?: number;\n maxWaitMs?: number;\n /** Called with the first step's answer so a caller can return it without waiting for the poll. */\n onStart?: (runId: string, outcome: EnvironmentBuildOutcome) => void;\n}\n\nexport function createEnvironmentBuildWorkflow(\n deps: EnvironmentBuildDeps,\n options: EnvironmentBuildWorkflowOptions = {},\n) {\n const sleep = options.sleep ?? (ms => new Promise<void>(resolve => setTimeout(resolve, ms)));\n const pollMs = options.pollMs ?? DEFAULT_BUILD_POLL_MS;\n const maxWaitMs = options.maxWaitMs ?? MAX_BUILD_WAIT_MS;\n const now = deps.now ?? (() => new Date());\n\n const start = createStep({\n id: 'start',\n inputSchema: environmentBuildInputSchema,\n outputSchema: startOutputSchema,\n execute: async ({ inputData, runId }) => {\n const outcome = await runEnvironmentBuild(deps, inputData);\n options.onStart?.(runId, outcome);\n return { projectId: inputData.projectId, ...outcome };\n },\n });\n\n const awaitBuild = createStep({\n id: 'await-build',\n inputSchema: startOutputSchema,\n outputSchema: environmentBuildOutputSchema,\n execute: async ({ inputData }): Promise<EnvironmentBuildWorkflowResult> => {\n const { projectId, heads, ...result } = inputData;\n if (result.outcome !== 'started' || !result.buildId || !heads) return result;\n const builds = deps.sandbox?.builds;\n const project = await deps.projects.getById({ id: projectId });\n if (!builds || !deps.sourceControl || !project) return { ...result, status: 'unknown' };\n const environment = await resolveProjectEnvironment(deps.sourceControl.storage, project);\n const { ctx } = await environmentBuildContextWithEnv(\n project,\n environment,\n heads,\n deps.sourceControl.versionControl,\n deps.sandboxEnvs,\n );\n const startedAt = now().getTime();\n try {\n for (;;) {\n const build = await builds.get(ctx, environment.settings, result.buildId);\n if (build.status === 'ready') {\n const templateId = build.templateId ?? result.templateId ?? result.buildId;\n // A newer run may have replaced `last_build_id` while this one\n // polled; the pin is skipped then so the stale image never wins.\n const pinned = await deps.projects.pinActiveTemplate({\n id: project.id,\n buildId: result.buildId,\n templateId,\n heads,\n });\n deps.logger?.info(pinned ? 'environment build ready' : 'environment build ready but superseded', {\n factoryProjectId: project.id,\n templateId,\n });\n return { ...result, templateId, status: 'ready' };\n }\n if (build.status === 'failed' || build.status === 'unknown') {\n deps.logger?.info('environment build did not complete', {\n factoryProjectId: project.id,\n buildId: result.buildId,\n status: build.status,\n ...(build.error ? { reason: maskSandboxEnvValues(redactCredentials(build.error), ctx.buildEnv) } : {}),\n });\n return {\n ...result,\n status: build.status,\n ...(build.error ? { reason: maskSandboxEnvValues(redactCredentials(build.error), ctx.buildEnv) } : {}),\n };\n }\n if (now().getTime() - startedAt >= maxWaitMs) {\n return { ...result, status: 'unknown', reason: 'Build wait timed out.' };\n }\n await sleep(pollMs);\n }\n } catch (error) {\n const reason = maskSandboxEnvValues(errorReason(error), ctx.buildEnv);\n deps.logger?.info('environment build status read failed', { factoryProjectId: project.id, reason });\n return { ...result, status: 'unknown', reason };\n }\n },\n });\n\n return createWorkflow({\n id: ENVIRONMENT_BUILD_WORKFLOW_ID,\n inputSchema: environmentBuildInputSchema,\n outputSchema: environmentBuildOutputSchema,\n })\n .then(start)\n .then(awaitBuild)\n .commit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,QAAQ,MAAM;OACd,GAAI,MAAM,QAAQ,EAAE,QAAQ,qBAAqB,kBAAkB,MAAM,KAAK,GAAG,IAAI,QAAQ,EAAE,IAAI,CAAC;MACtG,CAAC;MACD,OAAO;OACL,GAAG;OACH,QAAQ,MAAM;OACd,GAAI,MAAM,QAAQ,EAAE,QAAQ,qBAAqB,kBAAkB,MAAM,KAAK,GAAG,IAAI,QAAQ,EAAE,IAAI,CAAC;MACtG;KACF;KACA,IAAI,IAAI,CAAC,CAAC,QAAQ,IAAI,aAAa,WACjC,OAAO;MAAE,GAAG;MAAQ,QAAQ;MAAW,QAAQ;KAAwB;KAEzE,MAAM,MAAM,MAAM;IACpB;GACF,SAAS,OAAO;IACd,MAAM,SAAS,qBAAqB,YAAY,KAAK,GAAG,IAAI,QAAQ;IACpE,KAAK,QAAQ,KAAK,wCAAwC;KAAE,kBAAkB,QAAQ;KAAI;IAAO,CAAC;IAClG,OAAO;KAAE,GAAG;KAAQ,QAAQ;KAAW;IAAO;GAChD;EACF;CACF,CAAC;CAED,OAAO,eAAe;EACpB,IAAI;EACJ,aAAa;EACb,cAAc;CAChB,CAAC,CAAC,CACC,KAAK,KAAK,CAAC,CACX,KAAK,UAAU,CAAC,CAChB,OAAO;AACZ"}
1
+ {"version":3,"file":"build-workflow.js","names":[],"sources":["../../src/environment/build-workflow.ts"],"sourcesContent":["/**\n * The `factory-environment-build` workflow: step one decides and starts a\n * build (`runEnvironmentBuild`), step two polls the provider until the build\n * settles and pins the active template on the project row. Registered by\n * `MastraFactory.prepare()` only when the sandbox has `builds`, and driven by\n * the Build now route, the per-project cron schedule, the GitHub push\n * hand-off and saved template inputs.\n */\n\nimport { createStep, createWorkflow } from '@mastra/core/workflows';\nimport { z } from 'zod';\nimport { maskSandboxEnvValues } from '../sandbox/sandbox-env.js';\nimport { resolveProjectEnvironment } from '../workspace.js';\nimport {\n ENVIRONMENT_BUILD_TRIGGERS,\n environmentBuildContextWithEnv,\n errorReason,\n MAX_BUILD_WAIT_MS,\n redactCredentials,\n runEnvironmentBuild,\n} from './build.js';\nimport type { EnvironmentBuildDeps, EnvironmentBuildOutcome } from './build.js';\n\nexport const ENVIRONMENT_BUILD_WORKFLOW_ID = 'factory-environment-build';\nexport const DEFAULT_BUILD_POLL_MS = 15_000;\n\nexport const environmentBuildInputSchema = z.object({\n projectId: z.string().min(1),\n trigger: z.enum(ENVIRONMENT_BUILD_TRIGGERS),\n});\n\nconst startOutputSchema = z.object({\n projectId: z.string(),\n outcome: z.enum(['started', 'skipped', 'unavailable', 'failed']),\n buildId: z.string().optional(),\n templateId: z.string().optional(),\n reason: z.string().optional(),\n heads: z.record(z.string(), z.string()).optional(),\n});\n\nexport const environmentBuildOutputSchema = z.object({\n outcome: z.enum(['started', 'skipped', 'unavailable', 'failed']),\n buildId: z.string().optional(),\n templateId: z.string().optional(),\n reason: z.string().optional(),\n /** Where the started build ended: `ready`, `failed`, or `unknown` when the provider lost it or the wait ran out. */\n status: z.enum(['ready', 'failed', 'unknown']).optional(),\n});\n\nexport type EnvironmentBuildWorkflowResult = z.infer<typeof environmentBuildOutputSchema>;\n\nexport interface EnvironmentBuildWorkflowOptions {\n sleep?: (ms: number) => Promise<void>;\n pollMs?: number;\n maxWaitMs?: number;\n /** Called with the first step's answer so a caller can return it without waiting for the poll. */\n onStart?: (runId: string, outcome: EnvironmentBuildOutcome) => void;\n /** Called once a run is over, whoever started it (a route, the scheduler or a hand-off). */\n onSettled?: (projectId: string, result: EnvironmentBuildWorkflowResult) => void;\n}\n\nexport function createEnvironmentBuildWorkflow(\n deps: EnvironmentBuildDeps,\n options: EnvironmentBuildWorkflowOptions = {},\n) {\n const sleep = options.sleep ?? (ms => new Promise<void>(resolve => setTimeout(resolve, ms)));\n const pollMs = options.pollMs ?? DEFAULT_BUILD_POLL_MS;\n const maxWaitMs = options.maxWaitMs ?? MAX_BUILD_WAIT_MS;\n const now = deps.now ?? (() => new Date());\n\n const start = createStep({\n id: 'start',\n inputSchema: environmentBuildInputSchema,\n outputSchema: startOutputSchema,\n execute: async ({ inputData, runId }) => {\n const outcome = await runEnvironmentBuild(deps, inputData);\n options.onStart?.(runId, outcome);\n return { projectId: inputData.projectId, ...outcome };\n },\n });\n\n // Polls a started build until it settles and pins its template.\n const pollUntilSettled = async (\n inputData: z.infer<typeof startOutputSchema>,\n ): Promise<EnvironmentBuildWorkflowResult> => {\n const { projectId, heads, ...result } = inputData;\n if (result.outcome !== 'started' || !result.buildId || !heads) return result;\n const builds = deps.sandbox?.builds;\n const project = await deps.projects.getById({ id: projectId });\n if (!builds || !deps.sourceControl || !project) return { ...result, status: 'unknown' };\n const environment = await resolveProjectEnvironment(deps.sourceControl.storage, project);\n const { ctx } = await environmentBuildContextWithEnv(\n project,\n environment,\n heads,\n deps.sourceControl.versionControl,\n deps.sandboxEnvs,\n );\n const startedAt = now().getTime();\n try {\n for (;;) {\n const build = await builds.get(ctx, environment.settings, result.buildId);\n if (build.status === 'ready') {\n const templateId = build.templateId ?? result.templateId ?? result.buildId;\n // A newer run may have replaced `last_build_id` while this one\n // polled; the pin is skipped then so the stale image never wins.\n const pinned = await deps.projects.pinActiveTemplate({\n id: project.id,\n buildId: result.buildId,\n templateId,\n heads,\n });\n deps.logger?.info(pinned ? 'environment build ready' : 'environment build ready but superseded', {\n factoryProjectId: project.id,\n templateId,\n });\n return { ...result, templateId, status: 'ready' };\n }\n if (build.status === 'failed' || build.status === 'unknown') {\n deps.logger?.info('environment build did not complete', {\n factoryProjectId: project.id,\n buildId: result.buildId,\n status: build.status,\n ...(build.error ? { reason: maskSandboxEnvValues(redactCredentials(build.error), ctx.buildEnv) } : {}),\n });\n return {\n ...result,\n status: build.status,\n ...(build.error ? { reason: maskSandboxEnvValues(redactCredentials(build.error), ctx.buildEnv) } : {}),\n };\n }\n if (now().getTime() - startedAt >= maxWaitMs) {\n return { ...result, status: 'unknown', reason: 'Build wait timed out.' };\n }\n await sleep(pollMs);\n }\n } catch (error) {\n const reason = maskSandboxEnvValues(errorReason(error), ctx.buildEnv);\n deps.logger?.info('environment build status read failed', { factoryProjectId: project.id, reason });\n return { ...result, status: 'unknown', reason };\n }\n };\n\n const awaitBuild = createStep({\n id: 'await-build',\n inputSchema: startOutputSchema,\n outputSchema: environmentBuildOutputSchema,\n execute: async ({ inputData }): Promise<EnvironmentBuildWorkflowResult> => {\n const settled = await pollUntilSettled(inputData);\n options.onSettled?.(inputData.projectId, settled);\n return settled;\n },\n });\n\n return createWorkflow({\n id: ENVIRONMENT_BUILD_WORKFLOW_ID,\n inputSchema: environmentBuildInputSchema,\n outputSchema: environmentBuildOutputSchema,\n })\n .then(start)\n .then(awaitBuild)\n .commit();\n}\n"],"mappings":";;;;;;;;;;;;;;AAuBA,MAAa,gCAAgC;AAC7C,MAAa,wBAAwB;AAErC,MAAa,8BAA8B,EAAE,OAAO;CAClD,WAAW,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC;CAC3B,SAAS,EAAE,KAAK,0BAA0B;AAC5C,CAAC;AAED,MAAM,oBAAoB,EAAE,OAAO;CACjC,WAAW,EAAE,OAAO;CACpB,SAAS,EAAE,KAAK;EAAC;EAAW;EAAW;EAAe;CAAQ,CAAC;CAC/D,SAAS,EAAE,OAAO,CAAC,CAAC,SAAS;CAC7B,YAAY,EAAE,OAAO,CAAC,CAAC,SAAS;CAChC,QAAQ,EAAE,OAAO,CAAC,CAAC,SAAS;CAC5B,OAAO,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,SAAS;AACnD,CAAC;AAED,MAAa,+BAA+B,EAAE,OAAO;CACnD,SAAS,EAAE,KAAK;EAAC;EAAW;EAAW;EAAe;CAAQ,CAAC;CAC/D,SAAS,EAAE,OAAO,CAAC,CAAC,SAAS;CAC7B,YAAY,EAAE,OAAO,CAAC,CAAC,SAAS;CAChC,QAAQ,EAAE,OAAO,CAAC,CAAC,SAAS;;CAE5B,QAAQ,EAAE,KAAK;EAAC;EAAS;EAAU;CAAS,CAAC,CAAC,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,KAAK,CAAC,CACX,KAAK,UAAU,CAAC,CAChB,OAAO;AACZ"}
@@ -13,11 +13,12 @@ import type { SandboxEnvStore } from '../storage/domains/sandbox-envs/base.js';
13
13
  import type { SourceControlStorageHandle } from '../storage/domains/source-control/base.js';
14
14
  import type { SessionEnvironment } from '../workspace.js';
15
15
  import type { EnvironmentHeads } from './heads.js';
16
- export declare const ENVIRONMENT_BUILD_TRIGGERS: readonly ['manual', 'schedule', 'push'];
16
+ /** `settings`: a saved change to the environment; builds only when the template inputs changed. */
17
+ export declare const ENVIRONMENT_BUILD_TRIGGERS: readonly ['manual', 'schedule', 'push', 'settings'];
17
18
  export type EnvironmentBuildTrigger = (typeof ENVIRONMENT_BUILD_TRIGGERS)[number];
18
19
  export interface EnvironmentBuildSourceControl {
19
20
  storage: Pick<SourceControlStorageHandle, 'projectRepositories' | 'repositories'>;
20
- versionControl: Pick<VersionControl, 'getRepositoryAccess'>;
21
+ versionControl: Pick<VersionControl, 'getRepositoryAccess' | 'getRepositoriesAccess'>;
21
22
  }
22
23
  export interface EnvironmentBuildDeps {
23
24
  /** The factory's normalized sandbox; builds are unavailable without its `builds`. */
@@ -40,7 +41,7 @@ export interface EnvironmentBuildOutcome {
40
41
  outcome: 'started' | 'skipped' | 'unavailable' | 'failed';
41
42
  buildId?: string;
42
43
  templateId?: string;
43
- /** Why nothing started: `debounced`, `starting` (another run is starting a build), `in_progress`, `unchanged`, `unresolved` (a lapsed run may have started a build the provider cannot list), `no_environment`, `no_builds`, `no_source_control`; why a run failed: `not_recorded`, `superseded`, or an error. */
44
+ /** Why nothing started: `debounced`, `starting` (another run is starting a build), `in_progress`, `unchanged`, `unresolved` (a lapsed run may have started a build the provider cannot list), `no_template` (a `settings` run before the first build), `no_environment`, `no_builds`, `no_source_control`; why a run failed: `not_recorded`, `superseded`, or an error. */
44
45
  reason?: string;
45
46
  /** The heads the started build pins; the workflow stores them once the build is ready. */
46
47
  heads?: EnvironmentHeads;
@@ -60,13 +61,13 @@ export declare function errorReason(error: unknown): string;
60
61
  * session id, with heads served from `heads` so the provider never resolves
61
62
  * them itself.
62
63
  */
63
- export declare function environmentBuildContext(project: Pick<FactoryProject, 'id' | 'orgId' | 'name'>, environment: SessionEnvironment, heads: EnvironmentHeads, versionControl: Pick<VersionControl, 'getRepositoryAccess'>): FactorySandboxContext;
64
+ export declare function environmentBuildContext(project: Pick<FactoryProject, 'id' | 'orgId' | 'name'>, environment: SessionEnvironment, heads: EnvironmentHeads, versionControl: Pick<VersionControl, 'getRepositoryAccess' | 'getRepositoriesAccess'>): FactorySandboxContext;
64
65
  /**
65
66
  * {@link environmentBuildContext} with the Factory's Build variables, plus the
66
67
  * build revision for {@link environmentBuildInputs}. Every build path (start,
67
68
  * status read, poll) goes through this so all of them name the same template.
68
69
  */
69
- export declare function environmentBuildContextWithEnv(project: Pick<FactoryProject, 'id' | 'orgId' | 'name'>, environment: SessionEnvironment, heads: EnvironmentHeads, versionControl: Pick<VersionControl, 'getRepositoryAccess'>, sandboxEnvs: Pick<SandboxEnvStore, 'resolve'> | undefined): Promise<{
70
+ export declare function environmentBuildContextWithEnv(project: Pick<FactoryProject, 'id' | 'orgId' | 'name'>, environment: SessionEnvironment, heads: EnvironmentHeads, versionControl: Pick<VersionControl, 'getRepositoryAccess' | 'getRepositoriesAccess'>, sandboxEnvs: Pick<SandboxEnvStore, 'resolve'> | undefined): Promise<{
70
71
  ctx: FactorySandboxContext;
71
72
  buildEnvRevision: string | undefined;
72
73
  }>;
@@ -91,6 +92,15 @@ export declare function resolveEnvironmentHeads(sourceControl: EnvironmentBuildS
91
92
  * its run died or stalled.
92
93
  */
93
94
  export declare const BUILD_START_LEASE_MS: number;
95
+ /** How long the build workflow polls a started build before it gives up on it. */
96
+ export declare const MAX_BUILD_WAIT_MS: number;
97
+ /**
98
+ * How long after a lapsed lease a provider that cannot list builds keeps every
99
+ * trigger from building. The lapsed run started its build, if it did, before
100
+ * its lease expired, so by now that build has outlived the workflow's own wait
101
+ * and is treated as failed.
102
+ */
103
+ export declare const UNRESOLVED_BUILD_START_TIMEOUT_MS: number;
94
104
  /**
95
105
  * Start a build for `projectId` when `trigger` calls for one. Every trigger
96
106
  * takes the project's build-start lease before reading build state and renews
@@ -101,13 +111,18 @@ export declare const BUILD_START_LEASE_MS: number;
101
111
  * build is ready and neither a head nor any other template input changed;
102
112
  * `manual` otherwise always builds. A `push` that would build then takes the
103
113
  * leading-edge debounce claim, so only a push that starts a build opens the
104
- * window. Saving settings never builds.
114
+ * window. `settings` (a saved environment change) builds only once the
115
+ * project has a template and the template inputs differ from the last
116
+ * build's, whatever the heads; it skips as `no_template` before the first
117
+ * build, and saving the same inputs twice skips as `unchanged`.
105
118
  *
106
119
  * Taking over a lapsed lease leaves the start uncertain: the lapsed run may
107
120
  * have started a build it never recorded. The mark persists until resolved.
108
121
  * While it stands, a provider that lists builds is asked for a running one
109
122
  * (and every trigger skips as `in_progress` on it); with a provider that
110
- * cannot list, every trigger skips as `unresolved`.
123
+ * cannot list, every trigger skips as `unresolved` until
124
+ * `UNRESOLVED_BUILD_START_TIMEOUT_MS` after the lapse, when the run gives
125
+ * up on that build as failed, clears the mark and carries on.
111
126
  *
112
127
  * The result is written as the lease is released, and only while this run
113
128
  * still holds it. A failed `builds.start` clears the last build id, so a later
@@ -1 +1 @@
1
- {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../../src/environment/build.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AACpF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEzE,OAAO,KAAK,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAClG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yCAAyC,CAAC;AAC/E,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,2CAA2C,CAAC;AAE5F,OAAO,KAAK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,CAAC;AAKnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,mBAAmB,CACvC,IAAI,EAAE,oBAAoB,EAC1B,KAAK,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,uBAAuB,CAAA;CAAE,GAC7D,OAAO,CAAC,uBAAuB,CAAC,CAyOlC"}
@@ -1,6 +1,7 @@
1
1
  import { maskSandboxEnvValues, resolveSandboxEnv, sandboxBuildContext } from "../sandbox/sandbox-env.js";
2
2
  import { environmentSandboxContext, resolveProjectEnvironment } from "../workspace.js";
3
3
  import { headsChanged, recordedHeadResolver, resolveCurrentHeads } from "./heads.js";
4
+ import { groupedRepositoryAccess } from "./repository-access.js";
4
5
  import { createHash, randomUUID } from "crypto";
5
6
  // src/environment/build.ts
6
7
  /**
@@ -11,10 +12,12 @@ import { createHash, randomUUID } from "crypto";
11
12
  * (`build-workflow.ts`), so a request that only needs the start answer never
12
13
  * waits on the provider.
13
14
  */
15
+ /** `settings`: a saved change to the environment; builds only when the template inputs changed. */
14
16
  const ENVIRONMENT_BUILD_TRIGGERS = [
15
17
  "manual",
16
18
  "schedule",
17
- "push"
19
+ "push",
20
+ "settings"
18
21
  ];
19
22
  /** The synthetic session id a project's build runs under; never a session. */
20
23
  function environmentBuildSessionId(projectId) {
@@ -38,13 +41,14 @@ function errorReason(error) {
38
41
  * them itself.
39
42
  */
40
43
  function environmentBuildContext(project, environment, heads, versionControl) {
44
+ const repositoryAccess = groupedRepositoryAccess(versionControl, project.orgId, environment.repos.map((repo) => repo.repositoryId));
41
45
  return {
42
46
  sessionId: environmentBuildSessionId(project.id),
43
47
  factoryId: project.id,
44
48
  factoryName: project.name,
45
49
  ...environmentSandboxContext(environment, {
46
50
  orgId: project.orgId,
47
- getRepositoryAccess: (args) => versionControl.getRepositoryAccess(args),
51
+ getRepositoryAccess: (args) => repositoryAccess(args.repositoryId),
48
52
  resolveHead: recordedHeadResolver(heads)
49
53
  })
50
54
  };
@@ -55,11 +59,17 @@ function environmentBuildContext(project, environment, heads, versionControl) {
55
59
  * status read, poll) goes through this so all of them name the same template.
56
60
  */
57
61
  async function environmentBuildContextWithEnv(project, environment, heads, versionControl, sandboxEnvs) {
62
+ return withBuildEnv(project, environment, heads, versionControl, await environmentBuildEnv(project, sandboxEnvs));
63
+ }
64
+ /** The Factory's Build variables as a build context hands them to the provider. */
65
+ async function environmentBuildEnv(project, sandboxEnvs) {
58
66
  const env = sandboxEnvs ? await resolveSandboxEnv(sandboxEnvs, {
59
67
  orgId: project.orgId,
60
68
  projectId: project.id
61
69
  }) : void 0;
62
- const build = sandboxBuildContext(env);
70
+ return sandboxBuildContext(env);
71
+ }
72
+ function withBuildEnv(project, environment, heads, versionControl, build) {
63
73
  return {
64
74
  ctx: {
65
75
  ...environmentBuildContext(project, environment, heads, versionControl),
@@ -111,6 +121,15 @@ async function resolveEnvironmentHeads(sourceControl, project, environment) {
111
121
  * its run died or stalled.
112
122
  */
113
123
  const BUILD_START_LEASE_MS = 6e5;
124
+ /** How long the build workflow polls a started build before it gives up on it. */
125
+ const MAX_BUILD_WAIT_MS = 18e5;
126
+ /**
127
+ * How long after a lapsed lease a provider that cannot list builds keeps every
128
+ * trigger from building. The lapsed run started its build, if it did, before
129
+ * its lease expired, so by now that build has outlived the workflow's own wait
130
+ * and is treated as failed.
131
+ */
132
+ const UNRESOLVED_BUILD_START_TIMEOUT_MS = MAX_BUILD_WAIT_MS;
114
133
  /** Tries at recording a started build before the run leaves its lease to lapse. */
115
134
  const RECORD_ATTEMPTS = 5;
116
135
  /**
@@ -123,13 +142,18 @@ const RECORD_ATTEMPTS = 5;
123
142
  * build is ready and neither a head nor any other template input changed;
124
143
  * `manual` otherwise always builds. A `push` that would build then takes the
125
144
  * leading-edge debounce claim, so only a push that starts a build opens the
126
- * window. Saving settings never builds.
145
+ * window. `settings` (a saved environment change) builds only once the
146
+ * project has a template and the template inputs differ from the last
147
+ * build's, whatever the heads; it skips as `no_template` before the first
148
+ * build, and saving the same inputs twice skips as `unchanged`.
127
149
  *
128
150
  * Taking over a lapsed lease leaves the start uncertain: the lapsed run may
129
151
  * have started a build it never recorded. The mark persists until resolved.
130
152
  * While it stands, a provider that lists builds is asked for a running one
131
153
  * (and every trigger skips as `in_progress` on it); with a provider that
132
- * cannot list, every trigger skips as `unresolved`.
154
+ * cannot list, every trigger skips as `unresolved` until
155
+ * `UNRESOLVED_BUILD_START_TIMEOUT_MS` after the lapse, when the run gives
156
+ * up on that build as failed, clears the mark and carries on.
133
157
  *
134
158
  * The result is written as the lease is released, and only while this run
135
159
  * still holds it. A failed `builds.start` clears the last build id, so a later
@@ -162,6 +186,23 @@ async function runEnvironmentBuild(deps, input) {
162
186
  outcome: "skipped",
163
187
  reason: "debounced"
164
188
  };
189
+ const readInputs = async (project) => {
190
+ const environment = await resolveProjectEnvironment(sourceControl.storage, project);
191
+ const buildEnv = await environmentBuildEnv(project, deps.sandboxEnvs);
192
+ return {
193
+ environment,
194
+ buildEnv,
195
+ buildInputs: environmentBuildInputs(environment, buildEnv.buildEnvRevision)
196
+ };
197
+ };
198
+ const settingsSkip = (project, read) => input.trigger !== "settings" ? void 0 : read.environment.repos.length === 0 ? "no_environment" : !project.activeTemplateId && !project.lastBuildId ? "no_template" : project.lastBuildInputs === read.buildInputs ? "unchanged" : void 0;
199
+ if (input.trigger === "settings") {
200
+ const reason = settingsSkip(initial, await readInputs(initial));
201
+ if (reason) return {
202
+ outcome: "skipped",
203
+ reason
204
+ };
205
+ }
165
206
  const token = randomUUID();
166
207
  const lease = {
167
208
  id: initial.id,
@@ -216,7 +257,8 @@ async function runEnvironmentBuild(deps, input) {
216
257
  let buildInputs;
217
258
  try {
218
259
  project = await deps.projects.getById({ id: initial.id }) ?? initial;
219
- environment = await resolveProjectEnvironment(sourceControl.storage, project);
260
+ const read = await readInputs(project);
261
+ ({environment, buildInputs} = read);
220
262
  if (environment.repos.length === 0) {
221
263
  await release();
222
264
  return {
@@ -224,10 +266,17 @@ async function runEnvironmentBuild(deps, input) {
224
266
  reason: "no_environment"
225
267
  };
226
268
  }
269
+ const skip = settingsSkip(project, read);
270
+ if (skip) {
271
+ await release();
272
+ return {
273
+ outcome: "skipped",
274
+ reason: skip
275
+ };
276
+ }
277
+ const buildEnv = read.buildEnv;
227
278
  heads = await resolveEnvironmentHeads(sourceControl, project, environment);
228
- const built = await environmentBuildContextWithEnv(project, environment, heads, sourceControl.versionControl, deps.sandboxEnvs);
229
- ctx = built.ctx;
230
- buildInputs = environmentBuildInputs(environment, built.buildEnvRevision);
279
+ ctx = withBuildEnv(project, environment, heads, sourceControl.versionControl, buildEnv).ctx;
231
280
  if (claim.uncertain) {
232
281
  if (builds.list) {
233
282
  const running = (await builds.list(ctx, environment.settings)).find((build) => build.status === "pending" || build.status === "building");
@@ -241,11 +290,20 @@ async function runEnvironmentBuild(deps, input) {
241
290
  }
242
291
  resolved = true;
243
292
  } else {
244
- await release();
245
- return {
246
- outcome: "skipped",
247
- reason: "unresolved"
248
- };
293
+ const since = project.buildStartUncertainSince ?? now();
294
+ if (now().getTime() - since.getTime() < 18e5) {
295
+ await release();
296
+ return {
297
+ outcome: "skipped",
298
+ reason: "unresolved"
299
+ };
300
+ }
301
+ resolved = true;
302
+ deps.logger?.info("environment build gave up on an unrecorded build start", {
303
+ factoryProjectId: project.id,
304
+ trigger: input.trigger,
305
+ uncertainSince: since.toISOString()
306
+ });
249
307
  }
250
308
  }
251
309
  if (project.lastBuildId) {
@@ -365,6 +423,6 @@ async function runEnvironmentBuild(deps, input) {
365
423
  clearInterval(renewal);
366
424
  }
367
425
  }
368
- export { BUILD_START_LEASE_MS, ENVIRONMENT_BUILD_TRIGGERS, environmentBuildContext, environmentBuildContextWithEnv, environmentBuildInputs, environmentBuildSessionId, errorReason, redactCredentials, resolveEnvironmentHeads, runEnvironmentBuild };
426
+ export { BUILD_START_LEASE_MS, ENVIRONMENT_BUILD_TRIGGERS, MAX_BUILD_WAIT_MS, UNRESOLVED_BUILD_START_TIMEOUT_MS, environmentBuildContext, environmentBuildContextWithEnv, environmentBuildInputs, environmentBuildSessionId, errorReason, redactCredentials, resolveEnvironmentHeads, runEnvironmentBuild };
369
427
 
370
428
  //# sourceMappingURL=build.js.map