hammerkit 2.1.0-next.20 → 2.1.0-next.22
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,
|
|
@@ -55,6 +55,8 @@ const UNSCHEDULABLE_MS = 10000;
|
|
|
55
55
|
// how long a pod may take to start, its image pull included
|
|
56
56
|
const POD_START_MS = 5 * 60 * 1000;
|
|
57
57
|
const REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
|
|
58
|
+
// how long a pod of the files may take to be deleted, before the requests that wait for it go on
|
|
59
|
+
const POD_REMOVE_MS = 2 * 60 * 1000;
|
|
58
60
|
const FETCH_TIMEOUT_MS = 10 * 60 * 1000;
|
|
59
61
|
// an answer is read into memory up to this; a diff is up to 64 MiB, as JSON a bit more
|
|
60
62
|
const MAX_ANSWER_BYTES = 160 * 1024 * 1024;
|
|
@@ -110,6 +112,8 @@ class KubernetesSandboxStorage {
|
|
|
110
112
|
// one request at a time per sandbox, as a host has it: the objects `state` writes are
|
|
111
113
|
// pruned again in the pod, which must not meet another request that still needs them
|
|
112
114
|
this.queues = new Map();
|
|
115
|
+
// per sandbox, the pod that is being taken away: until it is gone no pod of the sandbox starts
|
|
116
|
+
this.leaving = new Map();
|
|
113
117
|
this.backend = config.backend;
|
|
114
118
|
const access = (0, kubernetes_backend_1.getClusterAccess)(config.backend, env);
|
|
115
119
|
this.instance =
|
|
@@ -311,7 +315,8 @@ class KubernetesSandboxStorage {
|
|
|
311
315
|
const names = getSandboxNames(key.id);
|
|
312
316
|
const namespace = this.namespaces.name(key.owner);
|
|
313
317
|
const pod = await this.usePod(key);
|
|
314
|
-
pod
|
|
318
|
+
// the pod counts this run only once the copy is over: until then the pod is a request
|
|
319
|
+
// under way, which a pod that makes way for another run waits for (makeWayForRun)
|
|
315
320
|
const leave = () => {
|
|
316
321
|
pod.runs--;
|
|
317
322
|
this.leavePod(key, pod);
|
|
@@ -325,12 +330,13 @@ class KubernetesSandboxStorage {
|
|
|
325
330
|
await (0, promises_1.mkdir)(into, { recursive: true });
|
|
326
331
|
await this.copyOut(key, ['tar', 'cf', '-', '-C', `${CLAIM_PATH}/${key.id}/files`, '.'], into,
|
|
327
332
|
// what the run plans with: files, directories and links, nothing outside of it
|
|
328
|
-
(_path, type) => ['File', 'OldFile', 'ContiguousFile', 'Directory', 'SymbolicLink'].includes(type), AbortSignal.timeout(FETCH_TIMEOUT_MS));
|
|
333
|
+
(_path, type) => ['File', 'OldFile', 'ContiguousFile', 'Directory', 'SymbolicLink'].includes(type), AbortSignal.timeout(FETCH_TIMEOUT_MS), pod);
|
|
329
334
|
}
|
|
330
335
|
catch (e) {
|
|
331
|
-
|
|
336
|
+
this.leavePod(key, pod);
|
|
332
337
|
throw e;
|
|
333
338
|
}
|
|
339
|
+
pod.runs++;
|
|
334
340
|
const watching = new AbortController();
|
|
335
341
|
void this.makeWayForRun(key, pod, watching.signal);
|
|
336
342
|
return {
|
|
@@ -376,16 +382,14 @@ class KubernetesSandboxStorage {
|
|
|
376
382
|
condition.reason === 'Unschedulable' &&
|
|
377
383
|
Date.now() - new Date(condition.lastTransitionTime ?? Date.now()).getTime() >= UNSCHEDULABLE_MS));
|
|
378
384
|
if (stuck && !signal.aborted && this.pods.get(key.id) === pod) {
|
|
379
|
-
// requests under way are answered first; the next one starts a pod next to the run's
|
|
380
|
-
this
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
pod.movedAside = true;
|
|
388
|
-
await this.deletePod(pod.namespace, pod.name, 'Orphan');
|
|
385
|
+
// requests under way are answered first; the next one starts a pod next to the run's,
|
|
386
|
+
// once this one is gone: it has the name of this one, and would be the one deleted
|
|
387
|
+
await this.retire(key.id, pod, () => pod.runs, async () => {
|
|
388
|
+
// what it owns stays for the run (the egress proxies of its tasks) and goes
|
|
389
|
+
// when the run released it
|
|
390
|
+
await this.deletePod(pod.namespace, pod.name, 'Orphan', pod.uid);
|
|
391
|
+
pod.movedAside = true;
|
|
392
|
+
});
|
|
389
393
|
return;
|
|
390
394
|
}
|
|
391
395
|
}
|
|
@@ -396,7 +400,9 @@ class KubernetesSandboxStorage {
|
|
|
396
400
|
}
|
|
397
401
|
// What a tar in the pod writes, extracted into `into`: the entries `keep` takes, up to
|
|
398
402
|
// the bytes a sandbox may take.
|
|
399
|
-
async copyOut(key, command, into, keep, signal
|
|
403
|
+
async copyOut(key, command, into, keep, signal,
|
|
404
|
+
// the pod a caller holds already: asking for one again would wait for a pod that goes
|
|
405
|
+
held) {
|
|
400
406
|
let bytes = 0;
|
|
401
407
|
const extract = (0, tar_1.extract)({
|
|
402
408
|
cwd: into,
|
|
@@ -416,7 +422,12 @@ class KubernetesSandboxStorage {
|
|
|
416
422
|
});
|
|
417
423
|
extracted.catch(() => undefined);
|
|
418
424
|
counted.pipe(extract);
|
|
419
|
-
|
|
425
|
+
if (held) {
|
|
426
|
+
await this.exec(held, command, null, counted, signal);
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
await this.withPod(key, (pod) => this.exec(pod, command, null, counted, signal));
|
|
430
|
+
}
|
|
420
431
|
counted.end();
|
|
421
432
|
await extracted;
|
|
422
433
|
}
|
|
@@ -569,6 +580,10 @@ class KubernetesSandboxStorage {
|
|
|
569
580
|
}
|
|
570
581
|
// The running pod of the sandbox's files, which stays until `leavePod`.
|
|
571
582
|
async usePod(key) {
|
|
583
|
+
// a pod that goes (stopPod, makeWayForRun) has the name the next one gets
|
|
584
|
+
while (this.leaving.has(key.id)) {
|
|
585
|
+
await this.leaving.get(key.id);
|
|
586
|
+
}
|
|
572
587
|
let pod = this.pods.get(key.id);
|
|
573
588
|
if (pod?.started !== undefined && pod.busy === 0 && Date.now() - pod.started > POD_RENEW_MS) {
|
|
574
589
|
// nothing uses it and its deadline nears: a new one, before a run needs it for long
|
|
@@ -620,34 +635,80 @@ class KubernetesSandboxStorage {
|
|
|
620
635
|
}
|
|
621
636
|
// Takes the pod of the sandbox's files down, once the requests under way are answered.
|
|
622
637
|
async stopPod(key, namespace = this.namespaces.name(key.owner)) {
|
|
623
|
-
|
|
638
|
+
// no await between the last look at `leaving` and `retire`, which sets it
|
|
639
|
+
while (this.leaving.has(key.id)) {
|
|
640
|
+
await this.leaving.get(key.id);
|
|
641
|
+
}
|
|
642
|
+
await this.retire(key.id, this.pods.get(key.id), () => 0, () => this.deletePod(namespace, getSandboxNames(key.id).pod));
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Takes `pod` (if there is one) out of use, waits until no more than `keep()` of its users are
|
|
646
|
+
* left (the requests under way are answered, the runs that hold it stay) and then runs
|
|
647
|
+
* `remove`, for at most POD_REMOVE_MS. A pod after it has the same name, so none starts
|
|
648
|
+
* (usePod) before this is over: it would take the pod that goes, or be deleted by its name.
|
|
649
|
+
*/
|
|
650
|
+
async retire(id, pod, keep, remove) {
|
|
624
651
|
if (pod) {
|
|
625
|
-
this.pods.
|
|
652
|
+
if (this.pods.get(id) === pod) {
|
|
653
|
+
this.pods.delete(id);
|
|
654
|
+
}
|
|
626
655
|
clearTimeout(pod.idle);
|
|
627
|
-
|
|
656
|
+
}
|
|
657
|
+
const gone = (async () => {
|
|
658
|
+
for (let i = 0; pod && pod.busy > keep() && i < REQUEST_TIMEOUT_MS / 100; i++) {
|
|
628
659
|
await (0, sleep_1.sleep)(100);
|
|
629
660
|
}
|
|
661
|
+
let timer;
|
|
662
|
+
try {
|
|
663
|
+
await Promise.race([
|
|
664
|
+
remove(),
|
|
665
|
+
new Promise((_resolve, reject) => {
|
|
666
|
+
timer = setTimeout(() => reject(new Error('the pod of the sandbox did not go in time')), POD_REMOVE_MS);
|
|
667
|
+
}),
|
|
668
|
+
]);
|
|
669
|
+
}
|
|
670
|
+
finally {
|
|
671
|
+
clearTimeout(timer);
|
|
672
|
+
}
|
|
673
|
+
})();
|
|
674
|
+
// a remove that is stuck must not hold the requests of the sandbox: `leaving` always settles
|
|
675
|
+
const settled = gone.catch(() => undefined);
|
|
676
|
+
this.leaving.set(id, settled);
|
|
677
|
+
try {
|
|
678
|
+
await gone;
|
|
679
|
+
}
|
|
680
|
+
finally {
|
|
681
|
+
await settled;
|
|
682
|
+
if (this.leaving.get(id) === settled) {
|
|
683
|
+
this.leaving.delete(id);
|
|
684
|
+
}
|
|
630
685
|
}
|
|
631
|
-
await this.deletePod(namespace, getSandboxNames(key.id).pod);
|
|
632
686
|
}
|
|
633
|
-
|
|
687
|
+
// Deletes a pod and waits until it is gone. With its `uid` only that pod goes: one that has
|
|
688
|
+
// the name now is another, which a late delete must not remove.
|
|
689
|
+
async deletePod(namespace, name, propagationPolicy, uid) {
|
|
634
690
|
try {
|
|
635
691
|
await this.instance.coreApi.deleteNamespacedPod({
|
|
636
692
|
name,
|
|
637
693
|
namespace,
|
|
638
694
|
gracePeriodSeconds: 0,
|
|
639
695
|
...(propagationPolicy ? { propagationPolicy } : {}),
|
|
696
|
+
...(uid ? { body: { preconditions: { uid } } } : {}),
|
|
640
697
|
});
|
|
641
698
|
}
|
|
642
699
|
catch (e) {
|
|
643
|
-
|
|
700
|
+
// 409: the precondition failed, the pod of that uid is gone
|
|
701
|
+
if ((0, apply_1.statusCodeOf)(e) === 404 || (uid && (0, apply_1.statusCodeOf)(e) === 409)) {
|
|
644
702
|
return;
|
|
645
703
|
}
|
|
646
704
|
throw e;
|
|
647
705
|
}
|
|
648
706
|
for (let attempt = 0; attempt < 120; attempt++) {
|
|
649
707
|
try {
|
|
650
|
-
await this.instance.coreApi.readNamespacedPod({ name, namespace });
|
|
708
|
+
const found = await this.instance.coreApi.readNamespacedPod({ name, namespace });
|
|
709
|
+
if (uid && found.metadata?.uid !== uid) {
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
651
712
|
}
|
|
652
713
|
catch (e) {
|
|
653
714
|
if ((0, apply_1.statusCodeOf)(e) === 404) {
|
package/package.json
CHANGED