hammerkit 2.1.0-next.17 → 2.1.0-next.18

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getHarnessEnvironment = exports.connectSandbox = exports.connectServer = exports.hasShell = exports.harnessOptionsFromEnv = exports.DEFAULT_SHELL_TASK = void 0;
3
+ exports.getHarnessEnvironment = exports.reuseSandbox = exports.connectSandbox = exports.connectServer = exports.hasShell = exports.harnessOptionsFromEnv = exports.DEFAULT_SHELL_TASK = void 0;
4
4
  const get_file_context_1 = require("../file/get-file-context");
5
5
  const log_1 = require("../log");
6
6
  const work_item_status_1 = require("../planner/work-item-status");
@@ -90,6 +90,7 @@ async function reuseSandbox(connection, repo, ref) {
90
90
  }
91
91
  return undefined;
92
92
  }
93
+ exports.reuseSandbox = reuseSandbox;
93
94
  // What hammerkit needs of its environment to read the host config and the sign-in, for a
94
95
  // harness that loads it as a plugin rather than through the command line.
95
96
  function getHarnessEnvironment(cwd = process.cwd()) {
@@ -111,11 +111,13 @@ class SandboxClient {
111
111
  static async list(connection) {
112
112
  return (await json(await call(connection, 'GET', '/v1/sandboxes'))).sandboxes;
113
113
  }
114
- // checks out the ref again, what changed in the sandbox is gone: the same however often it comes
114
+ // checks out the ref again, and the server refuses that over changes of the sandbox. Not sent
115
+ // again after a lost answer: the server may have moved the sandbox already, and a second
116
+ // fetch would move it again, past what the agent did since.
115
117
  static async fetch(connection, id, ref) {
116
118
  return json(await call(connection, 'POST', `/v1/sandboxes/${encodeURIComponent(id)}/fetch`, {
117
119
  body: { ref },
118
- repeat: 'always',
120
+ repeat: 'unsent',
119
121
  }));
120
122
  }
121
123
  async stat(path) {
@@ -133,8 +135,9 @@ class SandboxClient {
133
135
  }
134
136
  return Buffer.from(await response.arrayBuffer());
135
137
  }
138
+ // not sent again after a lost answer: a write sent again would write over what the agent did since
136
139
  async writeRaw(path, content) {
137
- return json(await call(this.connection, 'PUT', this.file(path), { raw: content }));
140
+ return json(await call(this.connection, 'PUT', this.file(path), { raw: content, repeat: 'unsent' }));
138
141
  }
139
142
  // numbered lines, like `cat -n`
140
143
  async read(path, offset, limit) {
@@ -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.
@@ -9,6 +9,7 @@ const git_checkout_1 = require("./git-checkout");
9
9
  const oauth_1 = require("./oauth");
10
10
  const runs_1 = require("./runs");
11
11
  const repo_1 = require("./repo");
12
+ const sandbox_files_1 = require("./sandbox-files");
12
13
  const sandbox_storage_1 = require("./sandbox-storage");
13
14
  // How long creating a sandbox or moving it to another commit may take
14
15
  const FETCH_TIMEOUT_MS = 10 * 60 * 1000;
@@ -261,6 +262,10 @@ class SandboxRegistry {
261
262
  });
262
263
  }
263
264
  catch (e) {
265
+ // a file of the sandbox in the way of the ref, which a checkout would overwrite
266
+ if (e instanceof sandbox_files_1.SandboxFileError && e.status === 409) {
267
+ throw new SandboxError(409, repos.scrub(e, credentials.values).message);
268
+ }
264
269
  throw new SandboxError(502, repos.scrub(e, credentials.values).message);
265
270
  }
266
271
  sandbox.ref = ref;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hammerkit",
3
3
  "description": "Containerized build tool with incremental caching that runs the same build file locally, in Docker and on Kubernetes.",
4
- "version": "2.1.0-next.17",
4
+ "version": "2.1.0-next.18",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",