hammerkit 1.27.0-next.9 → 2.0.0-next.10

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 (141) hide show
  1. package/build.schema.json +176 -6
  2. package/dist/cache/backends/local-cache-backend.js +6 -0
  3. package/dist/cache/backends/registry/registry-client.js +1 -0
  4. package/dist/cache/cache-sync.js +1 -1
  5. package/dist/cache/explain.js +23 -4
  6. package/dist/cli.js +2 -0
  7. package/dist/dashboard/assets/graph-canvas-DLioOiRN.css +1 -0
  8. package/dist/dashboard/assets/graph-canvas-HmkW_-XJ.js +8 -0
  9. package/dist/dashboard/assets/index-gIBDC_m8.css +2 -0
  10. package/dist/dashboard/assets/index-vE22S5E_.js +17 -0
  11. package/dist/dashboard/index.html +15 -0
  12. package/dist/dashboard/logo.png +0 -0
  13. package/dist/docker/item-network.js +13 -5
  14. package/dist/executer/archive-task-entry.js +4 -2
  15. package/dist/executer/deadline.js +17 -3
  16. package/dist/executer/deferred-restore.js +36 -0
  17. package/dist/executer/docker-service.js +30 -8
  18. package/dist/executer/docker-task.js +24 -1
  19. package/dist/executer/execute-work-service.js +2 -0
  20. package/dist/executer/execute-work-task.js +14 -5
  21. package/dist/executer/execution-steps.js +25 -4
  22. package/dist/executer/local-task.js +3 -0
  23. package/dist/executer/reset-work-tree.js +4 -0
  24. package/dist/executer/scheduler/enqueue-next.js +63 -7
  25. package/dist/executer/scheduler/state-key.js +89 -15
  26. package/dist/executer/watch-loop.js +22 -6
  27. package/dist/file/get-file-context.js +5 -1
  28. package/dist/harness/connect.js +20 -6
  29. package/dist/harness/mcp-command.js +22 -6
  30. package/dist/harness/mcp.js +63 -114
  31. package/dist/harness/opencode.js +158 -50
  32. package/dist/harness/pi.js +29 -0
  33. package/dist/harness/sandbox-client.js +194 -23
  34. package/dist/harness/service-tools.js +141 -0
  35. package/dist/harness/task-tools.js +221 -0
  36. package/dist/kubernetes/apply.js +49 -1
  37. package/dist/kubernetes/await-running-state.js +241 -18
  38. package/dist/kubernetes/claim-layout.js +10 -1
  39. package/dist/kubernetes/cluster-state.js +245 -0
  40. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +142 -9
  41. package/dist/kubernetes/ensure-kubernetes-service-exists.js +2 -0
  42. package/dist/kubernetes/ensure-persistent-data.js +305 -12
  43. package/dist/kubernetes/follow-pod-logs.js +281 -0
  44. package/dist/kubernetes/job-placement.js +36 -12
  45. package/dist/kubernetes/kept-claims.js +16 -1
  46. package/dist/kubernetes/kubernetes-instance.js +1 -0
  47. package/dist/kubernetes/network-policy.js +6 -5
  48. package/dist/kubernetes/quota-wait.js +97 -0
  49. package/dist/kubernetes/remove-stale-items.js +46 -0
  50. package/dist/kubernetes/restore-kubernetes-data.js +35 -8
  51. package/dist/kubernetes/sandbox-runner.js +628 -0
  52. package/dist/kubernetes/service-rollouts.js +74 -0
  53. package/dist/kubernetes/store-kubernetes-data.js +18 -4
  54. package/dist/kubernetes/volumes.js +124 -12
  55. package/dist/main.js +2 -0
  56. package/dist/optimizer/calculate-checksum.js +49 -5
  57. package/dist/optimizer/check-key.js +94 -0
  58. package/dist/optimizer/check-keys.js +66 -0
  59. package/dist/optimizer/get-work-cache-stats.js +53 -27
  60. package/dist/optimizer/work-task-cache-description.js +49 -1
  61. package/dist/parser/read-build-file.js +8 -3
  62. package/dist/planner/service-definition.js +9 -4
  63. package/dist/planner/utils/append-work-dependencies.js +5 -3
  64. package/dist/planner/utils/append-work-service.js +18 -1
  65. package/dist/planner/utils/append-work-task.js +13 -3
  66. package/dist/planner/utils/get-work-runtime.js +6 -1
  67. package/dist/planner/utils/normalize-path.js +19 -1
  68. package/dist/planner/utils/parse-work-mount.js +7 -1
  69. package/dist/planner/utils/parse-work-port.js +4 -1
  70. package/dist/planner/utils/template-value.js +20 -4
  71. package/dist/planner/validate.js +22 -0
  72. package/dist/planner/work-item-executions.js +27 -0
  73. package/dist/planner/work-network.js +3 -1
  74. package/dist/planner/work-runtime-aggregate.js +32 -0
  75. package/dist/planner/work-runtime-kubernetes.js +204 -53
  76. package/dist/planner/work-task.js +6 -1
  77. package/dist/program.js +27 -9
  78. package/dist/remote/device-login.js +7 -2
  79. package/dist/remote/host-config.js +69 -3
  80. package/dist/remote/remote-commands.js +33 -7
  81. package/dist/remote/remote-status.js +112 -0
  82. package/dist/remote/remote-work-commands.js +571 -45
  83. package/dist/remote/server-client.js +15 -2
  84. package/dist/remote/token-source.js +64 -26
  85. package/dist/run-program.js +9 -0
  86. package/dist/schema/build-file-base-task-schema.js +3 -3
  87. package/dist/schema/build-file-container-service-schema.js +10 -0
  88. package/dist/schema/build-file-container-task-schema.js +16 -1
  89. package/dist/schema/build-file-include-schema.js +18 -4
  90. package/dist/schema/build-file-kubernetes-service-schema.js +2 -0
  91. package/dist/schema/build-file-task-schema.js +5 -1
  92. package/dist/schema/capabilities-schema.js +56 -0
  93. package/dist/schema/reference-parser.js +119 -11
  94. package/dist/schema/removed-properties.js +20 -0
  95. package/dist/schema/schema-parser.js +140 -43
  96. package/dist/schema/work-scope-parser.js +33 -10
  97. package/dist/secrets/command-secret-provider.js +3 -1
  98. package/dist/secrets/github-app-token-command.js +3 -1
  99. package/dist/secrets/secret-catalog.js +43 -19
  100. package/dist/server/accounts-api.js +4 -1
  101. package/dist/server/approvals-api.js +1 -1
  102. package/dist/server/capacity-format.js +36 -0
  103. package/dist/server/capacity.js +330 -0
  104. package/dist/server/checks-api.js +83 -0
  105. package/dist/server/checks.js +116 -0
  106. package/dist/server/dashboard.js +214 -0
  107. package/dist/server/definitions-api.js +66 -3
  108. package/dist/server/endpoints.js +803 -0
  109. package/dist/server/executor.js +216 -30
  110. package/dist/server/kubernetes-backend.js +415 -48
  111. package/dist/server/kubernetes-sandbox-storage.js +60 -6
  112. package/dist/server/oauth.js +16 -1
  113. package/dist/server/overview.js +340 -0
  114. package/dist/server/plan.js +186 -124
  115. package/dist/server/policy.js +42 -4
  116. package/dist/server/proposals-api.js +17 -8
  117. package/dist/server/repo-access.js +5 -0
  118. package/dist/server/run-logs.js +4 -0
  119. package/dist/server/run-plan.js +38 -4
  120. package/dist/server/run-request.js +58 -3
  121. package/dist/server/run-worker.js +307 -17
  122. package/dist/server/runs.js +1036 -42
  123. package/dist/server/sandbox-files.js +107 -16
  124. package/dist/server/sandbox-patch.js +43 -17
  125. package/dist/server/sandbox-pod.js +28 -0
  126. package/dist/server/sandbox-storage.js +23 -0
  127. package/dist/server/sandboxes.js +65 -23
  128. package/dist/server/server-config.js +250 -11
  129. package/dist/server/server.js +465 -33
  130. package/dist/server/service-runs.js +89 -0
  131. package/dist/server/sources.js +68 -11
  132. package/dist/server/timings.js +152 -0
  133. package/dist/server/wait-reason.js +21 -0
  134. package/dist/utils/buffer-context.js +30 -20
  135. package/dist/utils/limit.js +30 -0
  136. package/dist/utils/read-lines.js +65 -0
  137. package/dist/utils/tar.js +28 -1
  138. package/dist/utils/terminal-prompt.js +53 -0
  139. package/dist/utils/units.js +36 -1
  140. package/dist/utils/web-links.js +34 -0
  141. package/package.json +8 -2
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HostExecutor = exports.volumeScope = exports.cacheNamespace = exports.defaultWorker = void 0;
3
+ exports.HostExecutor = exports.removeServiceRunVolumes = exports.serviceRunScopes = exports.volumeScope = exports.cacheNamespace = exports.defaultWorker = void 0;
4
4
  const child_process_1 = require("child_process");
5
5
  const crypto_1 = require("crypto");
6
+ const checks_1 = require("./checks");
6
7
  const fs_1 = require("fs");
7
8
  const promises_1 = require("fs/promises");
8
9
  const path_1 = require("path");
@@ -11,6 +12,7 @@ const resolve_git_source_1 = require("../includes/resolve-git-source");
11
12
  const git_checkout_1 = require("./git-checkout");
12
13
  const run_plan_1 = require("./run-plan");
13
14
  const work_network_1 = require("../planner/work-network");
15
+ const execute_docker_1 = require("../executer/execute-docker");
14
16
  // `.git` as a file system may read it: any case, and a name Windows reads without its
15
17
  // trailing dots and spaces, or by its short name
16
18
  function isGitDirectory(segment) {
@@ -35,19 +37,74 @@ exports.defaultWorker = defaultWorker;
35
37
  // share one, those of an upload or a sandbox only their owner's. Somebody who can only
36
38
  // upload, or an agent on a sandbox, can not leave an entry for a run from a reviewed
37
39
  // repository to find.
40
+ //
41
+ // A check on a sandbox runs on a copy of the files a commit of them would hold, so it is a
42
+ // run of the repository like a ref run of that commit, and shares its cache: what its
43
+ // owner may write in the sandbox is not part of it. It leaves entries a run from main can
44
+ // find, keyed by the files it ran on, as a ref run of a branch does: so only a caller who
45
+ // may push a branch of the repository and run from it may check (server.ts).
46
+ //
47
+ // An approved run on a sandbox runs on a copy of the approved files too, but its owner
48
+ // may not use its accounts without the approval: it has a cache of its own, so what the
49
+ // owner's other runs built from files nobody approved (ignored ones) is not restored in it.
38
50
  function cacheNamespace(run) {
39
- const key = run.source.type === 'ref' ? `ref\0${run.source.repo}` : `${run.source.type}\0${run.owner.iss}\0${run.owner.sub}`;
40
- return `${run.source.type}-${(0, crypto_1.createHash)('sha256').update(key).digest('hex').slice(0, 24)}`;
51
+ const repository = run.source.type === 'ref' || (run.source.type === 'sandbox' && run.source.check);
52
+ const kind = repository
53
+ ? 'ref'
54
+ : run.source.type === 'sandbox' && (run.approvals?.length ?? 0) > 0
55
+ ? 'approved'
56
+ : run.source.type;
57
+ const key = repository
58
+ ? `ref\0${run.source.repo}`
59
+ : `${kind}\0${run.owner.iss}\0${run.owner.sub}`;
60
+ return `${kind}-${(0, crypto_1.createHash)('sha256').update(key).digest('hex').slice(0, 24)}`;
41
61
  }
42
62
  exports.cacheNamespace = cacheNamespace;
43
63
  // The docker volumes of a run's services are its caller's, within the cache's source trust:
44
64
  // two callers of one repository share a cache, a task is keyed by what it reads, but not the
45
- // data their services keep.
46
- function volumeScope(run) {
65
+ // data their services keep. A service run's are its own: they stay while it sleeps and go
66
+ // when it ends (removeServiceRunVolumes). They carry the server's key (see serviceRunScopes),
67
+ // so a server sweeps its own only, next to another one on the same docker.
68
+ function volumeScope(run, server = '') {
69
+ if (run.up) {
70
+ return `${serviceRunScopes(server)}${(0, crypto_1.createHash)('sha256').update(run.id).digest('hex').slice(0, 24)}`;
71
+ }
47
72
  const key = `${cacheNamespace(run)}\0${run.owner.iss}\0${run.owner.sub}`;
48
73
  return `${run.source.type}-${(0, crypto_1.createHash)('sha256').update(key).digest('hex').slice(0, 24)}`;
49
74
  }
50
75
  exports.volumeScope = volumeScope;
76
+ // the start of the volume scopes of the service runs of a server, by the directory its runs
77
+ // work in
78
+ function serviceRunScopes(server) {
79
+ return `up-${(0, crypto_1.createHash)('sha256').update(server).digest('hex').slice(0, 8)}-`;
80
+ }
81
+ exports.serviceRunScopes = serviceRunScopes;
82
+ // the volume of a service of a service run, with the scope of its run (see scopeServiceVolumes)
83
+ const SERVICE_RUN_VOLUME = /^hammerkit-(up-[0-9a-f]{8}-[0-9a-f]{24})-/;
84
+ /**
85
+ * Removes the docker volumes of the services of service runs whose scope `remove` names: of
86
+ * one run that ended, or at a start, of every run but those that sleep with their volumes.
87
+ * One a container still uses stays, and is said.
88
+ */
89
+ async function removeServiceRunVolumes(docker, remove, log) {
90
+ const listed = await docker.listVolumes({ filters: { name: ['hammerkit-up-'] } });
91
+ const removed = [];
92
+ for (const volume of listed.Volumes ?? []) {
93
+ const scope = SERVICE_RUN_VOLUME.exec(volume.Name)?.[1];
94
+ if (!scope || !remove(scope)) {
95
+ continue;
96
+ }
97
+ try {
98
+ await docker.getVolume(volume.Name).remove();
99
+ removed.push(volume.Name);
100
+ }
101
+ catch (e) {
102
+ log(`the volume ${volume.Name} could not be removed: ${e instanceof Error ? e.message : String(e)}`);
103
+ }
104
+ }
105
+ return removed;
106
+ }
107
+ exports.removeServiceRunVolumes = removeServiceRunVolumes;
51
108
  /**
52
109
  * Runs the tasks of a run on this machine with the engine of `hammerkit run`, in a
53
110
  * process of its own.
@@ -65,6 +122,56 @@ exports.volumeScope = volumeScope;
65
122
  class HostExecutor {
66
123
  constructor(options) {
67
124
  this.options = options;
125
+ // the directories of the executions that run now
126
+ this.directories = new Set();
127
+ }
128
+ // the docker daemon the runs' services are on, as their processes reach it
129
+ docker() {
130
+ return (0, execute_docker_1.createDockerClient)(this.options.planning.environment.processEnvs.DOCKER_HOST);
131
+ }
132
+ // the volumes of a service run's services, which its end removes
133
+ async removeVolumes(run, context) {
134
+ const scope = volumeScope(run, this.options.directory);
135
+ const say = (text) => {
136
+ this.options.log?.(`run=${run.id} ${text}`);
137
+ context?.emit({ type: 'log', task: 'hammerkit', stream: 'stderr', text });
138
+ };
139
+ try {
140
+ await removeServiceRunVolumes(this.docker(), (candidate) => candidate === scope, say);
141
+ }
142
+ catch (e) {
143
+ say(`the volumes of the services could not be removed: ${e instanceof Error ? e.message : String(e)}`);
144
+ }
145
+ }
146
+ /** A service run that slept with its volumes ended without waking: they go. */
147
+ async forget(run) {
148
+ if (run.up && !this.options.placement) {
149
+ await this.removeVolumes(run);
150
+ }
151
+ }
152
+ /**
153
+ * At the start of the server: the volumes of service runs go, but of those that sleep with
154
+ * them. A server without docker has none.
155
+ */
156
+ restored(runs) {
157
+ if (this.options.placement) {
158
+ return;
159
+ }
160
+ const own = serviceRunScopes(this.options.directory);
161
+ const kept = new Set(runs.filter((run) => run.up).map((run) => volumeScope(run, this.options.directory)));
162
+ const log = (line) => this.options.log?.(line);
163
+ removeServiceRunVolumes(this.docker(), (scope) => scope.startsWith(own) && !kept.has(scope), log)
164
+ .then((removed) => {
165
+ if (removed.length > 0) {
166
+ log(`removed ${removed.length} volumes of service runs from before the server started`);
167
+ }
168
+ })
169
+ .catch((e) => {
170
+ const code = e.code;
171
+ if (code !== 'ENOENT' && code !== 'ECONNREFUSED') {
172
+ log(`the volumes of earlier service runs could not be removed: ${e instanceof Error ? e.message : e}`);
173
+ }
174
+ });
68
175
  }
69
176
  async execute(run, context) {
70
177
  const { definitions, planning, repos } = this.options;
@@ -110,50 +217,97 @@ class HostExecutor {
110
217
  abort();
111
218
  }
112
219
  context.signal.addEventListener('abort', abort, { once: true });
113
- const root = (0, path_1.join)(this.options.directory, run.id);
220
+ // a service run that moves executes beside itself, each in a directory of its own
221
+ const root = this.directoryOf(run);
114
222
  let attachment;
115
223
  try {
116
- // where the process of the run starts: the checkout of a ref, or for a sandbox the
117
- // published files, so what the process and the secret providers it starts read of
118
- // their directory is never what a task wrote
119
- let cwd;
224
+ // The process of the run starts in the published files, which it reads its build files
225
+ // from, and its tasks work on other files (`work`): a checkout, a sandbox or a copy of
226
+ // one. The published files are a directory of the run no task mounts, so what the
227
+ // process parses for each task it runs, and what the secret providers it starts read
228
+ // of their directory, is what was published and never what a task wrote.
229
+ const published = (0, path_1.join)(root, 'bundle');
230
+ await (0, promises_1.mkdir)(published, { recursive: true });
231
+ await this.writeBundle(published, bundle.files);
120
232
  let input;
121
233
  let claim;
122
- // the files of the sandbox the tasks work on
234
+ // the files the tasks work on
123
235
  let work;
124
- if (source.type === 'ref') {
125
- cwd = (0, path_1.join)(root, 'source');
236
+ const sourceStarted = new Date();
237
+ // A check works on the files a push of the sandbox commits, copied out of it, and so
238
+ // does a run that was approved: what was approved is those files, and the sandbox has
239
+ // more (its ignored files, node_modules, outputs), which its owner and their runs write
240
+ // while the run waits and runs.
241
+ const copied = source.type === 'sandbox' && (source.check || (run.approvals?.length ?? 0) > 0);
242
+ // (`source.type` again: TypeScript narrows `source` by it, not by `copied`)
243
+ if (source.type === 'sandbox' && copied) {
244
+ const { registry, network } = sandboxes;
245
+ const what = source.check ? 'the check' : 'the approved run';
246
+ if (!source.check) {
247
+ // what was approved is what runs: the files may have changed since, by a run
248
+ // before this one on the sandbox (the copy checks it again, under the sandbox's lock)
249
+ const state = await registry.state(source.id, abortCtrl.signal);
250
+ if (state.tree !== source.tree) {
251
+ throw new Error(`the files of the sandbox ${source.id} changed since the run was approved, they are ${state.tree} now and were ${source.tree}, submit the run again`);
252
+ }
253
+ }
254
+ work = (0, path_1.join)(root, 'source');
255
+ try {
256
+ await registry.exportTree(source.id, source.tree, work, abortCtrl.signal);
257
+ }
258
+ catch (e) {
259
+ throw new Error(`the files of the sandbox ${source.id} could not be copied for ${what}, submit it again: ${e instanceof Error ? e.message : String(e)}`);
260
+ }
261
+ // a repository of its own, as a checkout of the commit is: the root of the project,
262
+ // which the paths of the tasks' keys are relative to, is the one of git
263
+ await (0, git_checkout_1.git)(['init', '--quiet', work], {}, abortCtrl.signal, `${what} of sandbox ${source.id}`, 'init');
264
+ context.emit({
265
+ type: 'log',
266
+ task: 'hammerkit',
267
+ stream: 'stdout',
268
+ text: `${source.check ? 'check' : 'approved run'} of sandbox ${source.id} of ${source.repo}: files ${source.tree}, fetched at ${source.commit}`,
269
+ });
270
+ // the tasks see the published build files in place of the sandbox's own
271
+ await this.writeBundle(work, bundle.files);
272
+ input = {
273
+ entry: (0, path_1.join)(published, version.entry),
274
+ directory: { read: published, work },
275
+ ...(planning.policy.network ? { network: (0, work_network_1.toNetworkSchema)(planning.policy.network) } : {}),
276
+ sandboxNetwork: network,
277
+ };
278
+ }
279
+ else if (source.type === 'ref') {
280
+ work = (0, path_1.join)(root, 'source');
126
281
  const { repo, ref, commit: pinned } = source;
127
282
  const credentials = await repos.credentials(repo);
128
283
  let commit;
129
284
  try {
130
- commit = await (0, git_checkout_1.checkoutRef)(repos.url(repo), ref, cwd, credentials.env, abortCtrl.signal, pinned);
285
+ // a woken service run starts the commit it started first, where the ref is now or not
286
+ const fetched = run.wakes ? pinned : ref;
287
+ commit = await (0, git_checkout_1.checkoutRef)(repos.url(repo), fetched, work, credentials.env, abortCtrl.signal, pinned);
131
288
  }
132
289
  catch (e) {
133
290
  throw repos.scrub(e, credentials.values);
134
291
  }
135
292
  context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `checked out ${repo} at ${commit}` });
136
- await this.writeBundle(cwd, bundle.files);
293
+ // the tasks see the published build files in place of the checkout's own
294
+ await this.writeBundle(work, bundle.files);
137
295
  // what the version was checked against at publish holds while it runs too
138
296
  input = {
139
- entry: (0, path_1.join)(cwd, version.entry),
297
+ entry: (0, path_1.join)(published, version.entry),
298
+ directory: { read: published, work },
140
299
  ...(planning.policy.network ? { network: (0, work_network_1.toNetworkSchema)(planning.policy.network) } : {}),
141
300
  };
142
301
  }
143
302
  else {
144
- // the published files are read from a directory of the run and work on the
145
- // sandbox, whose own build files are just files
303
+ // a run its caller may run without an approval works on the sandbox itself, whose
304
+ // own build files are just files
146
305
  const { registry, network } = sandboxes;
147
306
  // read before the run attaches: until then the walk of the last run's measure holds
148
307
  const state = await registry.state(source.id, abortCtrl.signal);
149
308
  attachment = await registry.attach(source.id, (0, path_1.join)(root, 'sandbox'));
150
309
  work = attachment.directory;
151
310
  claim = attachment.claim;
152
- // what was approved is what runs: the files may have changed since, by a run
153
- // before this one on the sandbox
154
- if (run.approvals?.length && state.tree !== source.tree) {
155
- throw new Error(`the files of the sandbox ${source.id} changed since the run was approved, they are ${state.tree} now and were ${source.tree}, submit the run again`);
156
- }
157
311
  if (claim && !this.options.placement) {
158
312
  throw new Error(`the sandbox ${source.id} is a claim on a cluster, this server runs tasks on its own docker`);
159
313
  }
@@ -163,10 +317,6 @@ class HostExecutor {
163
317
  stream: 'stdout',
164
318
  text: `sandbox ${source.id} of ${source.repo} at ${state.commit}, files ${state.tree}${state.clean ? '' : ' with changes'}`,
165
319
  });
166
- const published = (0, path_1.join)(root, 'bundle');
167
- await (0, promises_1.mkdir)(published, { recursive: true });
168
- await this.writeBundle(published, bundle.files);
169
- cwd = published;
170
320
  input = {
171
321
  entry: (0, path_1.join)(published, version.entry),
172
322
  directory: { read: published, work },
@@ -175,39 +325,68 @@ class HostExecutor {
175
325
  sandboxNetwork: network,
176
326
  };
177
327
  }
328
+ emitPhase(context, 'source', sourceStarted);
178
329
  const home = (0, path_1.join)(root, 'home');
179
330
  await (0, promises_1.mkdir)(home, { recursive: true });
180
331
  const cache = (0, path_1.join)(this.options.cache, cacheNamespace(run));
181
332
  await (0, promises_1.mkdir)(cache, { recursive: true });
182
333
  const placement = this.options.placement;
183
- const placed = placement ? await placement.prepare(run, context, { root: work ?? cwd, claim }) : undefined;
334
+ const namespaceStarted = new Date();
335
+ const placed = placement ? await placement.prepare(run, context, { root: work, claim }) : undefined;
336
+ if (placed) {
337
+ emitPhase(context, 'namespace', namespaceStarted);
338
+ }
184
339
  try {
185
340
  await this.runInProcess(worker, {
186
341
  ...input,
187
342
  tasks: run.tasks,
188
343
  options: run.options,
189
344
  accounts: run.accounts,
190
- volumeScope: volumeScope(run),
345
+ volumeScope: volumeScope(run, this.options.directory),
346
+ ...((0, checks_1.isCheckRun)(run) ? { check: true } : {}),
191
347
  catalog: planning.environment.hosted?.catalog,
348
+ ...(run.up
349
+ ? {
350
+ up: {
351
+ services: run.up.services,
352
+ endpoints: run.up.endpoints.map(({ service, port }) => ({ service, port })),
353
+ },
354
+ }
355
+ : {}),
192
356
  ...(placed ? { kubernetes: placed.kubernetes } : {}),
193
- }, cwd, { ...this.processEnvironment(home, cache), ...placed?.env }, context, abortCtrl.signal);
357
+ ...(!placed && this.options.capabilityRuntime ? { capabilityRuntime: this.options.capabilityRuntime } : {}),
358
+ }, published, { ...this.processEnvironment(home, cache), ...placed?.env }, context, abortCtrl.signal);
194
359
  }
195
360
  finally {
196
361
  if (placement && placed) {
197
362
  await placement.release(run, context);
198
363
  }
364
+ else if (run.up && !context.keep?.()) {
365
+ // the services are gone with the process, their volumes stay only while the run sleeps
366
+ await this.removeVolumes(run, context);
367
+ }
199
368
  }
200
369
  }
201
370
  finally {
202
371
  context.signal.removeEventListener('abort', abort);
203
372
  attachment?.release?.();
204
373
  await this.removeRunDirectory(run, root, context);
374
+ this.directories.delete(root);
205
375
  if (source.type === 'sandbox') {
206
376
  // what the run wrote counts against the owner's limits, from their next run or create
207
377
  sandboxes?.registry.measure(source.id).catch(() => undefined);
208
378
  }
209
379
  }
210
380
  }
381
+ directoryOf(run) {
382
+ for (let execution = 1;; execution++) {
383
+ const root = (0, path_1.join)(this.options.directory, execution === 1 ? run.id : `${run.id}-${execution}`);
384
+ if (!this.directories.has(root)) {
385
+ this.directories.add(root);
386
+ return root;
387
+ }
388
+ }
389
+ }
211
390
  // A task of the run may still hold a file of its directory for a moment after the run
212
391
  // ended, which Windows refuses to remove. A directory that stays is reported, to the run
213
392
  // by its error code and to the operator with its path; it does not change how the run ended.
@@ -282,6 +461,9 @@ class HostExecutor {
282
461
  if (message.kind === 'event') {
283
462
  context.emit(message.event);
284
463
  }
464
+ else if (message.kind === 'up') {
465
+ context.up?.(message.targets);
466
+ }
285
467
  else if (message.kind === 'result') {
286
468
  result = message;
287
469
  // nothing left to tell the process
@@ -349,4 +531,8 @@ class HostExecutor {
349
531
  }
350
532
  }
351
533
  exports.HostExecutor = HostExecutor;
534
+ // A step of a run before its tasks, for its timeline (RunPhase)
535
+ function emitPhase(context, phase, startedAt) {
536
+ context.emit({ type: 'phase', phase, startedAt: startedAt.toISOString(), finishedAt: new Date().toISOString() });
537
+ }
352
538
  //# sourceMappingURL=executor.js.map