hammerkit 2.1.0-next.20 → 2.1.0-next.21

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,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.request = exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.downloadRunArtifact = exports.getRunArtifacts = exports.getRun = exports.followRun = exports.getMoveTarget = exports.lookupChecks = exports.submitRun = exports.FINAL_RUN_STATES = exports.readTextUpTo = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.getServerConnection = exports.SandboxClientError = exports.MAX_ENDPOINT_BYTES = void 0;
4
+ const crypto_1 = require("crypto");
4
5
  const path_1 = require("path");
5
6
  const token_source_1 = require("../remote/token-source");
6
7
  const reconnect_1 = require("./reconnect");
@@ -94,6 +95,10 @@ class SandboxClient {
94
95
  this.connection = connection;
95
96
  this.sandbox = sandbox;
96
97
  this.build = build;
98
+ // What the agent last read of each file, or wrote, by its sha256. A write sends it as If-Match,
99
+ // so a write over a file that changed since is refused. A file this client never read is written
100
+ // as it is, and so is a new file: there is no hash to check.
101
+ this.seen = new Map();
97
102
  }
98
103
  get id() {
99
104
  return this.sandbox.id;
@@ -133,15 +138,28 @@ class SandboxClient {
133
138
  if (response.headers.get('x-hammerkit-type') === 'directory') {
134
139
  throw new SandboxClientError(`${path} is a directory`, 400);
135
140
  }
136
- return Buffer.from(await response.arrayBuffer());
141
+ const content = Buffer.from(await response.arrayBuffer());
142
+ this.remember(checkSandboxPath(path), sha256Of(content));
143
+ return content;
137
144
  }
138
- // not sent again after a lost answer: a write sent again would write over what the agent did since
145
+ // not sent again after a lost answer: a write sent again would write over what the agent did since.
146
+ // Refused with 412 when the file changed since the agent read it (see `seen`).
139
147
  async writeRaw(path, content) {
140
- return json(await call(this.connection, 'PUT', this.file(path), { raw: content, repeat: 'unsent' }));
148
+ const key = checkSandboxPath(path);
149
+ const written = await json(await call(this.connection, 'PUT', this.file(path), {
150
+ raw: content,
151
+ repeat: 'unsent',
152
+ ifMatch: this.seen.get(key),
153
+ }));
154
+ this.remember(key, sha256Of(content));
155
+ return written;
141
156
  }
142
157
  // numbered lines, like `cat -n`
143
158
  async read(path, offset, limit) {
144
- return json(await call(this.connection, 'GET', this.file(path), { query: { offset, limit } }));
159
+ const read = await json(await call(this.connection, 'GET', this.file(path), { query: { offset, limit } }));
160
+ // the hash is of the whole file, however many lines were asked for
161
+ this.remember(checkSandboxPath(path), read.sha256);
162
+ return read;
145
163
  }
146
164
  async tree(query) {
147
165
  return json(await call(this.connection, 'GET', `${this.base()}/tree`, { query: { ...query } }));
@@ -153,10 +171,21 @@ class SandboxClient {
153
171
  // dry run is sent again on a broken connection
154
172
  async edit(ops, dryRun = false) {
155
173
  const answer = await call(this.connection, 'POST', `${this.base()}/edits`, {
156
- body: { dryRun, ops },
174
+ body: { dryRun, ops: this.withHashes(ops) },
157
175
  repeat: dryRun ? 'always' : 'unsent',
158
176
  });
159
- return (await json(answer)).files;
177
+ const files = (await json(answer)).files;
178
+ // a dry run changes nothing, so what the agent read stays as it was
179
+ if (!dryRun) {
180
+ for (const file of files) {
181
+ const key = fileKey(file.path);
182
+ // a file the edit left without a hash is one the agent has to read again
183
+ if (key !== undefined) {
184
+ this.remember(key, file.change === 'deleted' ? undefined : file.sha256);
185
+ }
186
+ }
187
+ }
188
+ return files;
160
189
  }
161
190
  async changes() {
162
191
  return json(await call(this.connection, 'GET', `${this.base()}/changes`));
@@ -306,6 +335,41 @@ class SandboxClient {
306
335
  base() {
307
336
  return `/v1/sandboxes/${encodeURIComponent(this.id)}`;
308
337
  }
338
+ // The ops of an edit as they go to the server, with the paths the server checks. The first op on
339
+ // a file carries the hash the agent read of it. The server checks a file as the edit has changed
340
+ // it so far, so an op after one that changed its file, a move's destination included, or after a
341
+ // patch (which names files of its own, with no hash), carries none. An invalid path goes as it is.
342
+ withHashes(ops) {
343
+ const changed = new Set();
344
+ let patched = false;
345
+ return ops.map((op) => {
346
+ if (op.op === 'patch') {
347
+ patched = true;
348
+ return op;
349
+ }
350
+ const key = fileKey(op.path);
351
+ if (key === undefined) {
352
+ return op;
353
+ }
354
+ const hash = !patched && !changed.has(key) ? this.seen.get(key) : undefined;
355
+ changed.add(key);
356
+ const to = op.op === 'move' ? fileKey(op.to) : undefined;
357
+ if (to !== undefined) {
358
+ changed.add(to);
359
+ }
360
+ const sent = op.op === 'move' ? { ...op, path: key, to: to ?? op.to } : { ...op, path: key };
361
+ return hash === undefined || op.ifHash !== undefined ? sent : { ...sent, ifHash: hash };
362
+ });
363
+ }
364
+ // a hash the server gave that is not a sha256 is not one the agent read, so the file is read again
365
+ remember(key, sha256) {
366
+ if (sha256 !== undefined && SHA256.test(sha256)) {
367
+ this.seen.set(key, sha256);
368
+ }
369
+ else {
370
+ this.seen.delete(key);
371
+ }
372
+ }
309
373
  file(path) {
310
374
  const clean = checkSandboxPath(path);
311
375
  if (!clean) {
@@ -315,6 +379,20 @@ class SandboxClient {
315
379
  }
316
380
  }
317
381
  exports.SandboxClient = SandboxClient;
382
+ // The key of a file in the record of what the agent read: its clean path, or undefined for a path
383
+ // outside the sandbox, which the server refuses anyway
384
+ function fileKey(path) {
385
+ try {
386
+ return checkSandboxPath(path);
387
+ }
388
+ catch {
389
+ return undefined;
390
+ }
391
+ }
392
+ const SHA256 = /^[0-9a-f]{64}$/;
393
+ function sha256Of(content) {
394
+ return (0, crypto_1.createHash)('sha256').update(content).digest('hex');
395
+ }
318
396
  // The first lines of every shell command: it stops when the working directory is empty,
319
397
  // links included, without any tool but the shell itself.
320
398
  const EMPTY_MARKER = 'hammerkit: the shell sees no files';
@@ -700,6 +778,7 @@ async function request(connection, method, path, options = {}) {
700
778
  ...(token !== undefined ? { authorization: `Bearer ${token}` } : {}),
701
779
  ...(options.body !== undefined ? { 'content-type': 'application/json' } : {}),
702
780
  ...(options.raw !== undefined ? { 'content-type': 'application/octet-stream' } : {}),
781
+ ...(options.ifMatch !== undefined ? { 'if-match': `"${options.ifMatch}"` } : {}),
703
782
  },
704
783
  body: options.body !== undefined ? JSON.stringify(options.body) : options.raw,
705
784
  signal: stop ? AbortSignal.any([timeout, stop]) : timeout,
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.20",
4
+ "version": "2.1.0-next.21",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",