hammerkit 2.1.0 → 2.2.0-next.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 (97) hide show
  1. package/build.schema.json +126 -28
  2. package/dist/cli.js +5 -0
  3. package/dist/dashboard/assets/graph-canvas-D6GMgkoe.js +8 -0
  4. package/dist/dashboard/assets/index-BNq9j5Jo.js +23 -0
  5. package/dist/dashboard/assets/index-DP3_hKOe.css +2 -0
  6. package/dist/dashboard/index.html +2 -2
  7. package/dist/docker/item-network.js +6 -5
  8. package/dist/docker/remove-orphaned-containers.js +5 -4
  9. package/dist/environment/replace-env-variables.js +8 -5
  10. package/dist/environment/run-source-envs.js +2 -1
  11. package/dist/executer/archive-task-entry.js +1 -1
  12. package/dist/executer/check-readiness.js +8 -4
  13. package/dist/executer/docker-healthcheck.js +73 -0
  14. package/dist/executer/docker-service.js +21 -15
  15. package/dist/executer/event-cache.js +5 -1
  16. package/dist/executer/runner-commands.js +36 -0
  17. package/dist/file/get-file-context.js +4 -7
  18. package/dist/harness/connect.js +12 -8
  19. package/dist/harness/reconnect.js +141 -0
  20. package/dist/harness/sandbox-client.js +241 -33
  21. package/dist/harness/service-tools.js +11 -2
  22. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +62 -14
  23. package/dist/kubernetes/ensure-persistent-data.js +1 -1
  24. package/dist/kubernetes/follow-pod-logs.js +8 -5
  25. package/dist/kubernetes/kept-claims.js +38 -14
  26. package/dist/kubernetes/sandbox-runner.js +526 -156
  27. package/dist/kubernetes/store-kubernetes-data.js +18 -3
  28. package/dist/kubernetes/sync-changes.js +463 -0
  29. package/dist/kubernetes/volumes.js +23 -1
  30. package/dist/network/healthcheck-request.js +38 -0
  31. package/dist/optimizer/get-work-cache-stats.js +7 -0
  32. package/dist/optimizer/work-task-cache-description.js +5 -3
  33. package/dist/planner/image-build.js +5 -0
  34. package/dist/planner/service-definition.js +2 -1
  35. package/dist/planner/utils/append-work-service.js +12 -4
  36. package/dist/planner/utils/dependency-outputs.js +7 -6
  37. package/dist/planner/validate.js +41 -0
  38. package/dist/planner/work-healthcheck.js +23 -2
  39. package/dist/planner/work-item-status.js +12 -2
  40. package/dist/planner/work-runtime-aggregate.js +1 -0
  41. package/dist/planner/work-runtime-docker.js +33 -6
  42. package/dist/planner/work-runtime-kubernetes.js +313 -70
  43. package/dist/planner/work-runtime-local.js +8 -4
  44. package/dist/planner/work-secret.js +33 -11
  45. package/dist/planner/work-service-id.js +30 -6
  46. package/dist/planner/work-service.js +2 -2
  47. package/dist/program.js +19 -1
  48. package/dist/remote/annotations.js +8 -2
  49. package/dist/remote/bundle.js +6 -8
  50. package/dist/remote/device-login.js +4 -1
  51. package/dist/remote/remote-commands.js +16 -7
  52. package/dist/remote/remote-status.js +1 -1
  53. package/dist/remote/remote-work-commands.js +220 -68
  54. package/dist/remote/server-client.js +38 -40
  55. package/dist/schema/build-file-base-task-schema.js +16 -1
  56. package/dist/schema/build-file-image-build-schema.js +5 -8
  57. package/dist/schema/build-file-service-container-healthcheck.js +25 -7
  58. package/dist/schema/reference-parser.js +108 -43
  59. package/dist/schema/work-scope-parser.js +8 -0
  60. package/dist/secrets/prefetch-secrets.js +20 -1
  61. package/dist/server/artifacts-api.js +56 -0
  62. package/dist/server/borrowed-quota.js +216 -0
  63. package/dist/server/capacity.js +18 -6
  64. package/dist/server/definitions-api.js +62 -8
  65. package/dist/server/executor.js +117 -30
  66. package/dist/server/http.js +4 -1
  67. package/dist/server/keep-artifacts.js +230 -0
  68. package/dist/server/kubernetes-backend.js +292 -79
  69. package/dist/server/kubernetes-sandbox-storage.js +86 -24
  70. package/dist/server/overview.js +73 -9
  71. package/dist/server/paging.js +167 -0
  72. package/dist/server/plan.js +16 -1
  73. package/dist/server/run-access.js +24 -0
  74. package/dist/server/run-artifacts.js +171 -0
  75. package/dist/server/run-logs.js +25 -1
  76. package/dist/server/run-request.js +11 -0
  77. package/dist/server/run-source-environment.js +1 -1
  78. package/dist/server/run-worker.js +267 -33
  79. package/dist/server/runs.js +531 -105
  80. package/dist/server/sandbox-files-api.js +27 -12
  81. package/dist/server/sandbox-files.js +12 -10
  82. package/dist/server/sandbox-follow.js +54 -0
  83. package/dist/server/sandbox-pod.js +1 -1
  84. package/dist/server/sandbox-storage.js +38 -1
  85. package/dist/server/sandbox-sync.js +392 -0
  86. package/dist/server/sandboxes-api.js +116 -19
  87. package/dist/server/sandboxes.js +38 -3
  88. package/dist/server/server-config.js +73 -2
  89. package/dist/server/server.js +169 -38
  90. package/dist/server/task-order.js +56 -0
  91. package/dist/utils/project-path.js +20 -0
  92. package/dist/utils/tar-files.js +52 -0
  93. package/dist/utils/tar.js +37 -1
  94. package/package.json +1 -1
  95. package/dist/dashboard/assets/graph-canvas-DtJSpova.js +0 -8
  96. package/dist/dashboard/assets/index-BndChMep.css +0 -2
  97. package/dist/dashboard/assets/index-DRSwIRXd.js +0 -17
@@ -76,10 +76,14 @@ const searchSchema = zod_1.z.preprocess((body) => {
76
76
  * PUT /v1/sandboxes/<id>/files/<path> write bytes, If-Match: "<sha256>"
77
77
  * POST /v1/sandboxes/<id>/search ripgrep
78
78
  * POST /v1/sandboxes/<id>/edits { dryRun, ops }, all or nothing
79
- * GET /v1/sandboxes/<id>/changes?format=summary|diff|patch
79
+ * GET /v1/sandboxes/<id>/changes?format=summary|diff|patch (diff: &path=… for some files)
80
80
  *
81
81
  * Reading a file works at any time. Writes, and the reads that walk the files (tree, search,
82
82
  * changes), are `409` while a run holds the sandbox.
83
+ *
84
+ * Who may read the sandbox, its owner and who observes, was decided by `handleSandboxes`:
85
+ * reads go as its owner. Writes go as the caller, so the registry answers anybody but the
86
+ * owner `404`.
83
87
  */
84
88
  async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, path, req, res, url) {
85
89
  const who = `iss=${principal.iss} sub=${principal.sub}`;
@@ -99,7 +103,7 @@ async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, pa
99
103
  limit: intParam(url, 'limit'),
100
104
  cursor: url.searchParams.get('cursor') ?? undefined,
101
105
  };
102
- const tree = await sandboxes.read(principal, sandbox.id, (files) => files.tree(query));
106
+ const tree = await sandboxes.read(sandbox.owner, sandbox.id, (files) => files.tree(query));
103
107
  (0, http_1.send)(res, 200, tree);
104
108
  }
105
109
  else if (action === 'search') {
@@ -107,7 +111,7 @@ async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, pa
107
111
  return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'POST' });
108
112
  }
109
113
  const query = parse(searchSchema, await (0, http_1.readJson)(req));
110
- (0, http_1.send)(res, 200, await sandboxes.read(principal, sandbox.id, (files) => files.search(query)));
114
+ (0, http_1.send)(res, 200, await sandboxes.read(sandbox.owner, sandbox.id, (files) => files.search(query)));
111
115
  }
112
116
  else if (action === 'edits') {
113
117
  if (method !== 'POST') {
@@ -124,7 +128,7 @@ async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, pa
124
128
  if (method !== 'GET') {
125
129
  return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET' });
126
130
  }
127
- await handleChanges(sandboxes, principal, sandbox, res, url);
131
+ await handleChanges(sandboxes, sandbox, res, url);
128
132
  }
129
133
  }
130
134
  catch (e) {
@@ -140,9 +144,10 @@ async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, pa
140
144
  exports.handleSandboxFiles = handleSandboxFiles;
141
145
  async function handleFile(sandboxes, log, principal, sandbox, path, req, res, url) {
142
146
  const method = req.method;
143
- const files = sandboxes.files(principal, sandbox.id);
147
+ // reading, as its owner
148
+ const files = () => sandboxes.files(sandbox.owner, sandbox.id);
144
149
  if (method === 'HEAD') {
145
- const stat = await files.stat(path);
150
+ const stat = await files().stat(path);
146
151
  if (!stat) {
147
152
  res.writeHead(404, { 'cache-control': 'no-store' });
148
153
  res.end();
@@ -153,11 +158,11 @@ async function handleFile(sandboxes, log, principal, sandbox, path, req, res, ur
153
158
  }
154
159
  else if (method === 'GET') {
155
160
  if (url.searchParams.has('stat')) {
156
- const stat = await files.stat(path);
161
+ const stat = await files().stat(path);
157
162
  return stat ? (0, http_1.send)(res, 200, stat) : (0, http_1.send)(res, 404, { error: `${path} does not exist` });
158
163
  }
159
164
  if (url.searchParams.has('raw')) {
160
- const raw = await files.raw(path);
165
+ const raw = await files().raw(path);
161
166
  const headers = { ...statHeaders(raw.stat), 'content-type': 'application/octet-stream' };
162
167
  if (raw.content) {
163
168
  res.writeHead(200, { ...headers, etag: `"${raw.sha256}"` });
@@ -179,7 +184,7 @@ async function handleFile(sandboxes, log, principal, sandbox, path, req, res, ur
179
184
  });
180
185
  return;
181
186
  }
182
- (0, http_1.send)(res, 200, await files.read(path, { offset: intParam(url, 'offset'), limit: intParam(url, 'limit') }));
187
+ (0, http_1.send)(res, 200, await files().read(path, { offset: intParam(url, 'offset'), limit: intParam(url, 'limit') }));
183
188
  }
184
189
  else if (method === 'PUT') {
185
190
  const content = await readBytes(req, sandbox_files_1.MAX_FILE_BYTES);
@@ -198,10 +203,12 @@ async function handleFile(sandboxes, log, principal, sandbox, path, req, res, ur
198
203
  (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET, HEAD, PUT' });
199
204
  }
200
205
  }
201
- async function handleChanges(sandboxes, principal, sandbox, res, url) {
206
+ // the most files one diff may name
207
+ const MAX_DIFF_PATHS = 100;
208
+ async function handleChanges(sandboxes, sandbox, res, url) {
202
209
  const format = url.searchParams.get('format') ?? 'summary';
203
210
  if (format === 'summary') {
204
- const files = await sandboxes.read(principal, sandbox.id, (files) => files.changes());
211
+ const files = await sandboxes.read(sandbox.owner, sandbox.id, (files) => files.changes());
205
212
  return (0, http_1.send)(res, 200, { commit: sandbox.commit, files });
206
213
  }
207
214
  if (format !== 'diff' && format !== 'patch') {
@@ -212,7 +219,15 @@ async function handleChanges(sandboxes, principal, sandbox, res, url) {
212
219
  email: textParam(url, 'email') ?? 'hammerkit@localhost',
213
220
  message: textParam(url, 'message', 4000, true) ?? `Changes of sandbox ${sandbox.id}`,
214
221
  };
215
- const text = await sandboxes.read(principal, sandbox.id, (files) => files.diff(format, author));
222
+ // the diff of some files only, as a list of changes opens one; a rename names both its paths
223
+ const paths = url.searchParams.getAll('path');
224
+ if (paths.length > 0 && format !== 'diff') {
225
+ return (0, http_1.send)(res, 400, { error: 'path: only a diff takes paths, a patch is whole' });
226
+ }
227
+ if (paths.length > MAX_DIFF_PATHS || paths.some((path) => path === '' || path.length > 4096 || path.includes('\0'))) {
228
+ return (0, http_1.send)(res, 400, { error: `path: at most ${MAX_DIFF_PATHS}, each a path of the sandbox` });
229
+ }
230
+ const text = await sandboxes.read(sandbox.owner, sandbox.id, (files) => files.diff(format, author, paths.length > 0 ? paths : undefined));
216
231
  res.writeHead(200, { 'content-type': 'text/x-diff; charset=utf-8', 'cache-control': 'no-store' });
217
232
  res.end(text);
218
233
  }
@@ -4,12 +4,14 @@ exports.HostSandboxFiles = exports.isRunState = exports.RUN_STATE_DIRECTORY = ex
4
4
  const child_process_1 = require("child_process");
5
5
  const crypto_1 = require("crypto");
6
6
  const fs_1 = require("fs");
7
+ const project_path_1 = require("../utils/project-path");
7
8
  const promises_1 = require("fs/promises");
8
9
  const os_1 = require("os");
9
10
  const path_1 = require("path");
10
11
  const minimatch_1 = require("minimatch");
11
12
  const source_manifest_1 = require("../remote/source-manifest");
12
13
  const sandbox_patch_1 = require("./sandbox-patch");
14
+ const runner_commands_1 = require("../executer/runner-commands");
13
15
  // The largest file the API reads into memory, edits or writes: the limit of an upload.
14
16
  exports.MAX_FILE_BYTES = source_manifest_1.MAX_SOURCE_FILE_BYTES;
15
17
  // what one typed read answers at most
@@ -54,13 +56,7 @@ exports.SandboxFileError = SandboxFileError;
54
56
  * deny list of uploads names (`.git/`, `.env`, keys).
55
57
  */
56
58
  function checkPath(path) {
57
- if (path.length === 0 ||
58
- path.length > 4096 ||
59
- path.startsWith('/') ||
60
- path.includes('\\') ||
61
- path.includes('\0') ||
62
- /^[A-Za-z]:/.test(path) ||
63
- path.split('/').some((part) => part === '' || part === '.' || part === '..')) {
59
+ if (!(0, project_path_1.isRelativePath)(path, 4096)) {
64
60
  throw new SandboxFileError(400, `${JSON.stringify(path.slice(0, 200))} is not a path in the sandbox: relative, / between names, no . or ..`);
65
61
  }
66
62
  const denied = deniedPath(path);
@@ -71,8 +67,13 @@ function checkPath(path) {
71
67
  }
72
68
  exports.checkPath = checkPath;
73
69
  // The deny list of uploads, with the last name taken as a directory too, so `.git` itself
74
- // is as denied as what is in it.
70
+ // is as denied as what is in it. The output of the commands of runs in the sandbox's runner
71
+ // is denied too: it is the runs' to read (their logs), not a file of the sandbox, and a file
72
+ // written there could pass for how a command ended.
75
73
  function deniedPath(path) {
74
+ if (path === runner_commands_1.RUNNER_COMMANDS_DIRECTORY || path.startsWith(`${runner_commands_1.RUNNER_COMMANDS_DIRECTORY}/`)) {
75
+ return 'the commands of runs keep their output there';
76
+ }
76
77
  return (0, source_manifest_1.deniedSourcePath)(path) ?? (0, source_manifest_1.deniedSourcePath)(`${path}/-`);
77
78
  }
78
79
  exports.deniedPath = deniedPath;
@@ -822,10 +823,11 @@ class HostSandboxFiles {
822
823
  return files;
823
824
  });
824
825
  }
825
- async diff(format, author) {
826
+ async diff(format, author, paths) {
826
827
  return this.withFilesIndex(async (run) => {
827
828
  if (format === 'diff') {
828
- return run(['diff', '--cached', '-M', 'HEAD'], 'read', { maxBuffer: this.maxChangesBytes });
829
+ const only = paths && paths.length > 0 ? ['--', ...paths.map((path) => `:(literal)${path}`)] : [];
830
+ return run(['diff', '--cached', '-M', 'HEAD', ...only], 'read', { maxBuffer: this.maxChangesBytes });
829
831
  }
830
832
  // a commit of the changes on top of the commit the sandbox is at, which no branch
831
833
  // points to, written out as `git format-patch` writes it
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SandboxFollow = void 0;
4
+ const log_1 = require("../log");
5
+ // How long a sandbox must stay unchanged before its service runs follow it, and how long
6
+ // they may wait while its files keep changing.
7
+ const DELAY_MS = 1000;
8
+ const MAX_WAIT_MS = 5000;
9
+ /**
10
+ * Batches the changes of each sandbox: a burst of edits (or of runs that wrote) makes one
11
+ * call of `follow` once the sandbox was quiet for `delay`, and at the latest `maxWait` after its
12
+ * first change, so a sandbox that never stops changing is still followed. Each sandbox has
13
+ * its own batch.
14
+ */
15
+ class SandboxFollow {
16
+ constructor(options) {
17
+ this.options = options;
18
+ // per sandbox, the timer of its batch and when the batch began
19
+ this.batches = new Map();
20
+ this.closed = false;
21
+ this.delay = options.delay ?? DELAY_MS;
22
+ this.maxWait = options.maxWait ?? MAX_WAIT_MS;
23
+ }
24
+ changed(sandbox) {
25
+ if (this.closed) {
26
+ return;
27
+ }
28
+ const now = Date.now();
29
+ const batch = this.batches.get(sandbox);
30
+ const since = batch?.since ?? now;
31
+ clearTimeout(batch?.timer);
32
+ const wait = Math.min(this.delay, since + this.maxWait - now);
33
+ const timer = setTimeout(() => this.run(sandbox), wait);
34
+ this.batches.set(sandbox, { timer, since });
35
+ }
36
+ close() {
37
+ this.closed = true;
38
+ for (const { timer } of this.batches.values()) {
39
+ clearTimeout(timer);
40
+ }
41
+ this.batches.clear();
42
+ }
43
+ async run(sandbox) {
44
+ this.batches.delete(sandbox);
45
+ try {
46
+ await this.options.follow(sandbox);
47
+ }
48
+ catch (e) {
49
+ this.options.log?.(`follow ${sandbox}: ${(0, log_1.getErrorMessage)(e)}`);
50
+ }
51
+ }
52
+ }
53
+ exports.SandboxFollow = SandboxFollow;
54
+ //# sourceMappingURL=sandbox-follow.js.map
@@ -80,7 +80,7 @@ async function answer(storage, directory, request, out) {
80
80
  case 'changes':
81
81
  return send(await files().changes());
82
82
  case 'diff':
83
- return send(await files().diff(request.format, request.author));
83
+ return send(await files().diff(request.format, request.author, request.paths));
84
84
  case 'commit':
85
85
  return send((await storage.commit(key, request.author, AbortSignal.timeout(10 * 60000))) ?? null);
86
86
  case 'move':
@@ -24,6 +24,11 @@ class SandboxPushFailed extends Error {
24
24
  exports.SandboxPushFailed = SandboxPushFailed;
25
25
  // the commit a sandbox pushed last, in its own git data
26
26
  const PUSHED_REF = 'refs/hammerkit/pushed';
27
+ // the directories a path is in, from the top: `a/b/c` is in `a` and `a/b`
28
+ function ancestors(path) {
29
+ const parts = path.split('/');
30
+ return parts.slice(0, -1).map((_, i) => parts.slice(0, i + 1).join('/'));
31
+ }
27
32
  /**
28
33
  * Sandboxes as directories below `directory`, one per sandbox: `files` is the checkout the
29
34
  * tasks work on, `git` its git data. The git data is kept outside of the files on purpose:
@@ -182,9 +187,41 @@ class HostSandboxStorage {
182
187
  this.walked.delete(id);
183
188
  // the branch by its full name: a tag of the same name is never taken
184
189
  await this.git(id, ['fetch', '--quiet', '--depth', '1', '--', fetch.url, `refs/heads/${fetch.ref}`], fetch.env, fetch.signal, 'fetch');
185
- await this.git(id, ['checkout', '--quiet', ...(force ? ['--force'] : []), 'FETCH_HEAD'], fetch.env, fetch.signal, 'check out');
190
+ try {
191
+ // a fetch does not let git write over an ignored file of the sandbox: git takes it for
192
+ // expendable, and the state of the sandbox does not count it as a change. Only a create
193
+ // passes force, into an empty work tree, which has no ignored file to write over.
194
+ await this.git(id, ['checkout', '--quiet', ...(force ? ['--force'] : ['--no-overwrite-ignore']), 'FETCH_HEAD'], fetch.env, fetch.signal, 'check out');
195
+ }
196
+ catch (e) {
197
+ const ignored = force ? undefined : await this.ignoredInTheWay(id, fetch);
198
+ if (ignored === undefined) {
199
+ throw e;
200
+ }
201
+ throw new sandbox_files_1.SandboxFileError(409, `${JSON.stringify(ignored)} is ignored in the sandbox and ${fetch.ref} has something in its place: ` +
202
+ 'a fetch would overwrite it, move or delete it first');
203
+ }
186
204
  return (await this.git(id, ['rev-parse', 'HEAD'], fetch.env, fetch.signal, 'read')).trim();
187
205
  }
206
+ // The first ignored file of the sandbox that the ref has in its place: at its path, above it
207
+ // (the ref has a file where the sandbox has a directory of ignored files) or below it (the ref
208
+ // has files in a directory where the sandbox has an ignored file). A checkout over one of those
209
+ // would write over it. Read only once a checkout was refused.
210
+ async ignoredInTheWay(id, fetch) {
211
+ // a listing that fails names no file, so git's own error stands
212
+ const list = (args) => this.git(id, args, fetch.env, fetch.signal, 'read', {
213
+ // a sandbox with node_modules lists many files
214
+ maxBuffer: 64 * 1024 * 1024,
215
+ }).then((out) => out.split('\0'), () => []);
216
+ const ignored = await list(['ls-files', '-z', '--others', '--ignored', '--exclude-standard']);
217
+ if (ignored.every((path) => path === '')) {
218
+ return undefined;
219
+ }
220
+ const tracked = await list(['ls-tree', '-r', '-z', '--name-only', 'FETCH_HEAD']);
221
+ const files = new Set(tracked);
222
+ const directories = new Set(tracked.flatMap(ancestors));
223
+ return ignored.find((path) => path !== '' && (files.has(path) || directories.has(path) || ancestors(path).some((dir) => files.has(dir))));
224
+ }
188
225
  // One hold of the sandbox from the commit to the push: a prune in between would remove the
189
226
  // commit. A ref keeps the commit until the sandbox is moved to it, also when the push
190
227
  // failed late and the remote has it after all.