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.
- package/build.schema.json +126 -28
- package/dist/cli.js +5 -0
- package/dist/dashboard/assets/graph-canvas-D6GMgkoe.js +8 -0
- package/dist/dashboard/assets/index-BNq9j5Jo.js +23 -0
- package/dist/dashboard/assets/index-DP3_hKOe.css +2 -0
- package/dist/dashboard/index.html +2 -2
- package/dist/docker/item-network.js +6 -5
- package/dist/docker/remove-orphaned-containers.js +5 -4
- package/dist/environment/replace-env-variables.js +8 -5
- package/dist/environment/run-source-envs.js +2 -1
- package/dist/executer/archive-task-entry.js +1 -1
- package/dist/executer/check-readiness.js +8 -4
- package/dist/executer/docker-healthcheck.js +73 -0
- package/dist/executer/docker-service.js +21 -15
- package/dist/executer/event-cache.js +5 -1
- package/dist/executer/runner-commands.js +36 -0
- package/dist/file/get-file-context.js +4 -7
- package/dist/harness/connect.js +12 -8
- package/dist/harness/reconnect.js +141 -0
- package/dist/harness/sandbox-client.js +241 -33
- package/dist/harness/service-tools.js +11 -2
- package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +62 -14
- package/dist/kubernetes/ensure-persistent-data.js +1 -1
- package/dist/kubernetes/follow-pod-logs.js +8 -5
- package/dist/kubernetes/kept-claims.js +38 -14
- package/dist/kubernetes/sandbox-runner.js +526 -156
- package/dist/kubernetes/store-kubernetes-data.js +18 -3
- package/dist/kubernetes/sync-changes.js +463 -0
- package/dist/kubernetes/volumes.js +23 -1
- package/dist/network/healthcheck-request.js +38 -0
- package/dist/optimizer/get-work-cache-stats.js +7 -0
- package/dist/optimizer/work-task-cache-description.js +5 -3
- package/dist/planner/image-build.js +5 -0
- package/dist/planner/service-definition.js +2 -1
- package/dist/planner/utils/append-work-service.js +12 -4
- package/dist/planner/utils/dependency-outputs.js +7 -6
- package/dist/planner/validate.js +41 -0
- package/dist/planner/work-healthcheck.js +23 -2
- package/dist/planner/work-item-status.js +12 -2
- package/dist/planner/work-runtime-aggregate.js +1 -0
- package/dist/planner/work-runtime-docker.js +33 -6
- package/dist/planner/work-runtime-kubernetes.js +313 -70
- package/dist/planner/work-runtime-local.js +8 -4
- package/dist/planner/work-secret.js +33 -11
- package/dist/planner/work-service-id.js +30 -6
- package/dist/planner/work-service.js +2 -2
- package/dist/program.js +19 -1
- package/dist/remote/annotations.js +8 -2
- package/dist/remote/bundle.js +6 -8
- package/dist/remote/device-login.js +4 -1
- package/dist/remote/remote-commands.js +16 -7
- package/dist/remote/remote-status.js +1 -1
- package/dist/remote/remote-work-commands.js +220 -68
- package/dist/remote/server-client.js +38 -40
- package/dist/schema/build-file-base-task-schema.js +16 -1
- package/dist/schema/build-file-image-build-schema.js +5 -8
- package/dist/schema/build-file-service-container-healthcheck.js +25 -7
- package/dist/schema/reference-parser.js +108 -43
- package/dist/schema/work-scope-parser.js +8 -0
- package/dist/secrets/prefetch-secrets.js +20 -1
- package/dist/server/artifacts-api.js +56 -0
- package/dist/server/borrowed-quota.js +216 -0
- package/dist/server/capacity.js +18 -6
- package/dist/server/definitions-api.js +62 -8
- package/dist/server/executor.js +117 -30
- package/dist/server/http.js +4 -1
- package/dist/server/keep-artifacts.js +230 -0
- package/dist/server/kubernetes-backend.js +292 -79
- package/dist/server/kubernetes-sandbox-storage.js +86 -24
- package/dist/server/overview.js +73 -9
- package/dist/server/paging.js +167 -0
- package/dist/server/plan.js +16 -1
- package/dist/server/run-access.js +24 -0
- package/dist/server/run-artifacts.js +171 -0
- package/dist/server/run-logs.js +25 -1
- package/dist/server/run-request.js +11 -0
- package/dist/server/run-source-environment.js +1 -1
- package/dist/server/run-worker.js +267 -33
- package/dist/server/runs.js +531 -105
- package/dist/server/sandbox-files-api.js +27 -12
- package/dist/server/sandbox-files.js +12 -10
- package/dist/server/sandbox-follow.js +54 -0
- package/dist/server/sandbox-pod.js +1 -1
- package/dist/server/sandbox-storage.js +38 -1
- package/dist/server/sandbox-sync.js +392 -0
- package/dist/server/sandboxes-api.js +116 -19
- package/dist/server/sandboxes.js +38 -3
- package/dist/server/server-config.js +73 -2
- package/dist/server/server.js +169 -38
- package/dist/server/task-order.js +56 -0
- package/dist/utils/project-path.js +20 -0
- package/dist/utils/tar-files.js +52 -0
- package/dist/utils/tar.js +37 -1
- package/package.json +1 -1
- package/dist/dashboard/assets/graph-canvas-DtJSpova.js +0 -8
- package/dist/dashboard/assets/index-BndChMep.css +0 -2
- package/dist/dashboard/assets/index-DRSwIRXd.js +0 -17
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArtifactStore = exports.refuseArtifacts = exports.LOCAL_TASKS_REFUSAL = exports.ARTIFACTS_MANIFEST = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const promises_1 = require("fs/promises");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const units_1 = require("../utils/units");
|
|
8
|
+
const plan_1 = require("./plan");
|
|
9
|
+
// What the process of a run kept of the files its tasks generated, written as
|
|
10
|
+
// `artifacts.json` beside the archives once it is done: the server serves only what it lists.
|
|
11
|
+
exports.ARTIFACTS_MANIFEST = 'artifacts.json';
|
|
12
|
+
// Why a host that runs local tasks as the user of the server keeps no outputs: such a task
|
|
13
|
+
// could write the store of them (see server.ts and HostExecutorOptions.localTasksOnHost).
|
|
14
|
+
exports.LOCAL_TASKS_REFUSAL = 'this server runs local tasks as its own user, which could write the outputs it keeps: it keeps none';
|
|
15
|
+
/**
|
|
16
|
+
* Why a run keeps none of the files its tasks generated, or null when it keeps them. Its
|
|
17
|
+
* outputs are handed out as they are, never masked: a task or service of the run that reads
|
|
18
|
+
* a secret or a variable of the server (but the plain ones) can write it into a file, its
|
|
19
|
+
* own or one a task after it reads and copies (an `.npmrc`, a docker `config.json`), as it
|
|
20
|
+
* could print it past the masking with an argument (task-arguments FR-008). So any of them
|
|
21
|
+
* leaves the whole run without artifacts.
|
|
22
|
+
*/
|
|
23
|
+
function refuseArtifacts(plan) {
|
|
24
|
+
for (const [name, item] of Object.entries(plan).sort(([a], [b]) => a.localeCompare(b))) {
|
|
25
|
+
const reads = (0, plan_1.getCredentialReads)(item);
|
|
26
|
+
if (reads.length > 0) {
|
|
27
|
+
return `${name} reads ${reads.join(', ')}: what it writes can reach the outputs of the run, which keeps none`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
exports.refuseArtifacts = refuseArtifacts;
|
|
33
|
+
// Run ids name the directories: nothing else is taken from the store's folder
|
|
34
|
+
const RUN_DIRECTORY = /^run_[A-Za-z0-9_-]+$/;
|
|
35
|
+
// where a run's process writes the archives before it moves them into place (keepArtifacts)
|
|
36
|
+
const STAGING = '.staging';
|
|
37
|
+
/**
|
|
38
|
+
* The outputs runs kept, a directory per run below `directory`, written by its process
|
|
39
|
+
* (keepArtifacts) or its executor. They stay `keep` from the end of their run, while the
|
|
40
|
+
* registry keeps the run, and the newest runs' only while all together take at most `total`.
|
|
41
|
+
* `localTasksOnHost`: a local task of this host runs as the user of the server and could write
|
|
42
|
+
* the store, so nothing in it is served, whatever it holds, and the store is emptied.
|
|
43
|
+
*/
|
|
44
|
+
class ArtifactStore {
|
|
45
|
+
constructor(directory, options) {
|
|
46
|
+
this.directory = directory;
|
|
47
|
+
this.options = options;
|
|
48
|
+
}
|
|
49
|
+
directoryOf(id) {
|
|
50
|
+
if (!RUN_DIRECTORY.test(id)) {
|
|
51
|
+
throw new Error(`${id} is not a run id`);
|
|
52
|
+
}
|
|
53
|
+
return (0, path_1.join)(this.directory, id);
|
|
54
|
+
}
|
|
55
|
+
// A run that ended; who may read it was checked already.
|
|
56
|
+
async read(run, now) {
|
|
57
|
+
// nothing is read from the store here: a run that ended before planning has no manifest of its own
|
|
58
|
+
if (this.options.localTasksOnHost) {
|
|
59
|
+
return { outputs: [], left: [], reason: exports.LOCAL_TASKS_REFUSAL };
|
|
60
|
+
}
|
|
61
|
+
if (run.up) {
|
|
62
|
+
return { outputs: [], left: [], reason: 'a service run keeps no outputs' };
|
|
63
|
+
}
|
|
64
|
+
const manifest = await this.manifest(run, now);
|
|
65
|
+
if (!manifest) {
|
|
66
|
+
return {
|
|
67
|
+
outputs: [],
|
|
68
|
+
left: [],
|
|
69
|
+
reason: `the server has no outputs of the run: it keeps what tasks export (export: true, or always) for ${(0, units_1.formatDuration)(this.options.keep)}, and ${(0, units_1.formatSize)(this.options.total)} of all runs, the newest`,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
outputs: manifest.outputs.map((output, id) => ({
|
|
74
|
+
id,
|
|
75
|
+
task: output.task,
|
|
76
|
+
path: output.path,
|
|
77
|
+
bytes: output.bytes,
|
|
78
|
+
})),
|
|
79
|
+
left: manifest.left,
|
|
80
|
+
...(manifest.refused ? { reason: manifest.refused } : { expiresAt: this.expiresAt(run).toISOString() }),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// The archive of one output: a gzipped tar of its paths in the project.
|
|
84
|
+
async open(run, id, now) {
|
|
85
|
+
if (this.options.localTasksOnHost) {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
const output = (await this.manifest(run, now))?.outputs[id];
|
|
89
|
+
if (!output || !/^\d+\.tgz$/.test(output.file)) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
const file = (0, path_1.join)(this.directoryOf(run.id), output.file);
|
|
93
|
+
const bytes = (await (0, promises_1.stat)(file).catch(() => undefined))?.size;
|
|
94
|
+
return bytes === undefined ? undefined : { bytes, stream: () => (0, fs_1.createReadStream)(file), read: () => (0, promises_1.readFile)(file) };
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Removes what is over its time, what runs the registry no longer has kept, and of the
|
|
98
|
+
* runs that ended the oldest while all of them take more than `total`. A run that has not
|
|
99
|
+
* ended may still write. What a run's process left in its staging folder after it ended (a
|
|
100
|
+
* crash while it kept the outputs) goes too. Answers the runs it removed the outputs of.
|
|
101
|
+
*/
|
|
102
|
+
async sweep(lookup, now) {
|
|
103
|
+
const names = await (0, promises_1.readdir)(this.directory).catch(() => []);
|
|
104
|
+
if (this.options.localTasksOnHost) {
|
|
105
|
+
// nothing a run keeps is served here, so nothing is kept: whatever is in the store goes
|
|
106
|
+
for (const name of names) {
|
|
107
|
+
await (0, promises_1.rm)((0, path_1.join)(this.directory, name), { recursive: true, force: true });
|
|
108
|
+
}
|
|
109
|
+
return names.filter((name) => RUN_DIRECTORY.test(name));
|
|
110
|
+
}
|
|
111
|
+
const removed = [];
|
|
112
|
+
const ended = [];
|
|
113
|
+
for (const id of names.filter((name) => RUN_DIRECTORY.test(name))) {
|
|
114
|
+
const run = lookup(id);
|
|
115
|
+
if (run && !run.finishedAt) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (!run || this.expiresAt(run).getTime() <= now) {
|
|
119
|
+
await this.remove(id);
|
|
120
|
+
removed.push(id);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
await (0, promises_1.rm)((0, path_1.join)(this.directoryOf(id), STAGING), { recursive: true, force: true });
|
|
124
|
+
// what the directory takes, as it is: a file the manifest does not name counts too
|
|
125
|
+
const bytes = await sizeOf(this.directoryOf(id));
|
|
126
|
+
ended.push({ id, finishedAt: Date.parse(run.finishedAt), bytes });
|
|
127
|
+
}
|
|
128
|
+
let total = ended.reduce((sum, run) => sum + run.bytes, 0);
|
|
129
|
+
for (const run of ended.sort((a, b) => a.finishedAt - b.finishedAt)) {
|
|
130
|
+
if (total <= this.options.total) {
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
await this.remove(run.id);
|
|
134
|
+
removed.push(run.id);
|
|
135
|
+
total -= run.bytes;
|
|
136
|
+
}
|
|
137
|
+
return removed;
|
|
138
|
+
}
|
|
139
|
+
expiresAt(run) {
|
|
140
|
+
return new Date(Date.parse(run.finishedAt ?? '') + this.options.keep);
|
|
141
|
+
}
|
|
142
|
+
// what the run kept, while it is kept
|
|
143
|
+
async manifest(run, now) {
|
|
144
|
+
if (!run.finishedAt || this.expiresAt(run).getTime() <= now) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
return this.readManifest(run.id);
|
|
148
|
+
}
|
|
149
|
+
async readManifest(id) {
|
|
150
|
+
try {
|
|
151
|
+
return JSON.parse(await (0, promises_1.readFile)((0, path_1.join)(this.directoryOf(id), exports.ARTIFACTS_MANIFEST), 'utf8'));
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async remove(id) {
|
|
158
|
+
await (0, promises_1.rm)(this.directoryOf(id), { recursive: true, force: true });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.ArtifactStore = ArtifactStore;
|
|
162
|
+
// The bytes of the files below a directory, each as it is on disk (a link counts as itself).
|
|
163
|
+
async function sizeOf(directory) {
|
|
164
|
+
let bytes = 0;
|
|
165
|
+
for (const entry of await (0, promises_1.readdir)(directory, { withFileTypes: true }).catch(() => [])) {
|
|
166
|
+
const path = (0, path_1.join)(directory, entry.name);
|
|
167
|
+
bytes += entry.isDirectory() ? await sizeOf(path) : (await (0, promises_1.lstat)(path).catch(() => undefined))?.size ?? 0;
|
|
168
|
+
}
|
|
169
|
+
return bytes;
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=run-artifacts.js.map
|
package/dist/server/run-logs.js
CHANGED
|
@@ -113,6 +113,23 @@ class LogIndex {
|
|
|
113
113
|
lines(task) {
|
|
114
114
|
return this.taskLines.get(task) ?? [];
|
|
115
115
|
}
|
|
116
|
+
// How many lines of the log, or of one task, the first `count` events hold
|
|
117
|
+
linesBefore(count, task) {
|
|
118
|
+
const lines = task !== undefined ? this.lines(task) : undefined;
|
|
119
|
+
let low = 0;
|
|
120
|
+
let high = lines ? lines.length : this.size;
|
|
121
|
+
// the lines are in the order of their events
|
|
122
|
+
while (low < high) {
|
|
123
|
+
const middle = (low + high) >> 1;
|
|
124
|
+
if (this.lineEvent[lines ? lines[middle] : middle] < count) {
|
|
125
|
+
low = middle + 1;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
high = middle;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return low;
|
|
132
|
+
}
|
|
116
133
|
text(events, line) {
|
|
117
134
|
const event = events[this.lineEvent[line]];
|
|
118
135
|
return { task: event.task, stream: event.stream, text: event.text.slice(this.lineStart[line], this.lineEnd[line]) };
|
|
@@ -145,9 +162,15 @@ class LogIndex {
|
|
|
145
162
|
}
|
|
146
163
|
exports.LogIndex = LogIndex;
|
|
147
164
|
// The lines of the log a query selects, see LogQuery.
|
|
148
|
-
|
|
165
|
+
// `lostAfter`: Run.lostAfter
|
|
166
|
+
function readLogs(events, index, query, truncated, lostAfter) {
|
|
149
167
|
const selected = query.task !== undefined ? index.lines(query.task) : undefined;
|
|
150
168
|
const total = selected ? selected.length : index.size;
|
|
169
|
+
const lost = lostAfter !== undefined
|
|
170
|
+
? index.linesBefore(lostAfter, query.task) + 1
|
|
171
|
+
: query.from !== undefined && query.from > total + 1
|
|
172
|
+
? total + 1
|
|
173
|
+
: undefined;
|
|
151
174
|
const at = (position) => (selected ? selected[position] : position);
|
|
152
175
|
const hits = query.grep !== undefined ? index.matches(events, query.grep, query.ignoreCase) : undefined;
|
|
153
176
|
const toLine = (position) => ({
|
|
@@ -215,6 +238,7 @@ function readLogs(events, index, query, truncated) {
|
|
|
215
238
|
total,
|
|
216
239
|
...(next !== undefined ? { next } : {}),
|
|
217
240
|
truncated,
|
|
241
|
+
...(lost !== undefined ? { lost } : {}),
|
|
218
242
|
};
|
|
219
243
|
}
|
|
220
244
|
exports.readLogs = readLogs;
|
|
@@ -134,6 +134,9 @@ exports.runRequestSchema = zod_1.z
|
|
|
134
134
|
.array(zod_1.z.object({ service: name, port: zod_1.z.number().int().min(1).max(65535) }).strict())
|
|
135
135
|
.max(exports.MAX_ENDPOINTS)
|
|
136
136
|
.default([]),
|
|
137
|
+
// the services that start again with the files of their sandbox after an edit of it; the
|
|
138
|
+
// others keep the files they started with (see ServiceRunSpec.restart)
|
|
139
|
+
restart: zod_1.z.array(name).max(20).optional(),
|
|
137
140
|
// paths of those endpoints that need no sign-in of the server, such as an app's own sign-in
|
|
138
141
|
// for an MCP client; the run they are written for must be the one the run gets (see
|
|
139
142
|
// refusePublicPaths), and the endpoint one the run publishes
|
|
@@ -171,6 +174,14 @@ exports.runRequestSchema = zod_1.z
|
|
|
171
174
|
request.sleepTtl === undefined &&
|
|
172
175
|
request.name === undefined &&
|
|
173
176
|
request.endpoints.length === 0), { message: 'ttl, sleepTtl, name and endpoints are for a run of services', path: ['services'] })
|
|
177
|
+
.refine((request) => (request.restart ?? []).every((service) => request.services.includes(service)), {
|
|
178
|
+
message: 'restart names services the run starts',
|
|
179
|
+
path: ['restart'],
|
|
180
|
+
})
|
|
181
|
+
.refine((request) => !request.restart?.length || request.source.type === 'sandbox', {
|
|
182
|
+
message: 'restart is for a service run from a sandbox, which changes with its files',
|
|
183
|
+
path: ['restart'],
|
|
184
|
+
})
|
|
174
185
|
.refine((request) => new Set(request.endpoints.map((endpoint) => `${endpoint.service}:${endpoint.port}`)).size ===
|
|
175
186
|
request.endpoints.length, { message: 'an endpoint is named twice', path: ['endpoints'] })
|
|
176
187
|
.refine((request) => request.publicPaths.length === 0 || request.services.length > 0, {
|
|
@@ -9,7 +9,7 @@ const COMMIT = /^[0-9a-f]{40}([0-9a-f]{24})?$/i;
|
|
|
9
9
|
// repository, and the branch or tag and the commit it was pinned to, or the repository and the
|
|
10
10
|
// sandbox. None of them is a credential, so a task that reads them still takes arguments and
|
|
11
11
|
// keeps its network (see PLAIN_SERVER_ENVS). A sandbox has no commit: its files may hold
|
|
12
|
-
// changes. A run from an upload has none of them.
|
|
12
|
+
// changes. A run from an upload has none of them. Each is a name of RUN_SOURCE_ENVS.
|
|
13
13
|
function runSourceEnvironment(source) {
|
|
14
14
|
if (source.type === 'ref') {
|
|
15
15
|
return {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.workerMain = exports.readInput = exports.taskCommands = exports.taskCauses = exports.describeFailures = exports.failedTasks = exports.runTasks = exports.getEndpointTargets = exports.publishEndpoints = exports.getRunNetwork = exports.workerInputSchema = void 0;
|
|
3
|
+
exports.workerMain = exports.readInput = exports.ChangeQueue = exports.applyChanges = exports.parseChanges = exports.syncLine = exports.syncServices = exports.taskCommands = exports.taskCauses = exports.describeFailures = exports.failedTasks = exports.runTasks = exports.relayStatus = exports.removeCommandsOfRun = exports.getRunnerCommands = exports.getLogEvent = exports.getEndpointTargets = exports.publishEndpoints = exports.getRunNetwork = exports.workerInputSchema = void 0;
|
|
4
4
|
const readline_1 = require("readline");
|
|
5
5
|
const zod_1 = require("zod");
|
|
6
6
|
const cli_1 = require("../cli");
|
|
@@ -28,6 +28,10 @@ const service_rollouts_1 = require("../kubernetes/service-rollouts");
|
|
|
28
28
|
const cluster_state_1 = require("../kubernetes/cluster-state");
|
|
29
29
|
const kubernetes_instance_1 = require("../kubernetes/kubernetes-instance");
|
|
30
30
|
const check_keys_1 = require("../optimizer/check-keys");
|
|
31
|
+
const units_2 = require("../utils/units");
|
|
32
|
+
const keep_artifacts_1 = require("./keep-artifacts");
|
|
33
|
+
const sandbox_runner_1 = require("../kubernetes/sandbox-runner");
|
|
34
|
+
const runner_commands_1 = require("../executer/runner-commands");
|
|
31
35
|
// one log line of a task is cut here, the registry caps how many events a run keeps
|
|
32
36
|
const MAX_LOG_TEXT = 8 * 1024;
|
|
33
37
|
/**
|
|
@@ -75,6 +79,7 @@ exports.workerInputSchema = zod_1.z
|
|
|
75
79
|
.object({
|
|
76
80
|
services: zod_1.z.array(zod_1.z.string()).min(1),
|
|
77
81
|
endpoints: zod_1.z.array(zod_1.z.object({ service: zod_1.z.string(), port: zod_1.z.number().int() }).strict()),
|
|
82
|
+
restart: zod_1.z.array(zod_1.z.string()).optional(),
|
|
78
83
|
})
|
|
79
84
|
.strict()
|
|
80
85
|
.optional(),
|
|
@@ -82,6 +87,32 @@ exports.workerInputSchema = zod_1.z
|
|
|
82
87
|
check: zod_1.z.boolean().optional(),
|
|
83
88
|
// what its tasks took in past runs, in ms by name, so the longest chain starts first
|
|
84
89
|
durations: zod_1.z.record(zod_1.z.number().nonnegative()).optional(),
|
|
90
|
+
// where the outputs the tasks export are kept for a download once the tasks ended, and
|
|
91
|
+
// what they may take together; left out for a run that keeps none (refuseArtifacts)
|
|
92
|
+
artifacts: zod_1.z
|
|
93
|
+
.object({ directory: zod_1.z.string().min(1), maxBytes: zod_1.z.number().int().positive() })
|
|
94
|
+
.strict()
|
|
95
|
+
.optional(),
|
|
96
|
+
// the run executed before a restart of the server: how far its log holds the output of
|
|
97
|
+
// each command in a sandbox's runner (RunContext.resumed)
|
|
98
|
+
resumed: zod_1.z
|
|
99
|
+
.object({
|
|
100
|
+
outputs: zod_1.z
|
|
101
|
+
.record(zod_1.z.string().regex(runner_commands_1.COMMAND_ID_PATTERN), zod_1.z
|
|
102
|
+
.object({
|
|
103
|
+
task: zod_1.z.string().max(1024),
|
|
104
|
+
step: zod_1.z.number().int().nonnegative(),
|
|
105
|
+
execution: zod_1.z.number().int().nonnegative(),
|
|
106
|
+
stdout: zod_1.z.number().int().nonnegative(),
|
|
107
|
+
stderr: zod_1.z.number().int().nonnegative(),
|
|
108
|
+
done: zod_1.z.literal(true).optional(),
|
|
109
|
+
cut: zod_1.z.literal(true).optional(),
|
|
110
|
+
})
|
|
111
|
+
.strict())
|
|
112
|
+
.refine((outputs) => Object.keys(outputs).length <= runner_commands_1.MAX_OUTPUT_COMMANDS, 'too many commands'),
|
|
113
|
+
})
|
|
114
|
+
.strict()
|
|
115
|
+
.optional(),
|
|
85
116
|
})
|
|
86
117
|
.strict();
|
|
87
118
|
// The ceiling of the run, combined here and not in the server: the lower of two allow lists
|
|
@@ -145,6 +176,72 @@ function getEndpointTargets(workTree, up, kubernetes) {
|
|
|
145
176
|
return targets;
|
|
146
177
|
}
|
|
147
178
|
exports.getEndpointTargets = getEndpointTargets;
|
|
179
|
+
// What the run says of a message of the engine: the output of its tasks and services, and
|
|
180
|
+
// what went wrong; where the log of a pod got to with a line of it (RunEventData)
|
|
181
|
+
function getLogEvent(message) {
|
|
182
|
+
if (message.type === 'console') {
|
|
183
|
+
return {
|
|
184
|
+
type: 'log',
|
|
185
|
+
task: message.context.name,
|
|
186
|
+
stream: message.console,
|
|
187
|
+
text: message.message.slice(0, MAX_LOG_TEXT),
|
|
188
|
+
...(message.cursor ? { cursor: message.cursor } : {}),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (message.level === 'error' || message.level === 'warn') {
|
|
192
|
+
return { type: 'log', task: message.context.name, stream: 'stderr', text: message.message.slice(0, MAX_LOG_TEXT) };
|
|
193
|
+
}
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
exports.getLogEvent = getLogEvent;
|
|
197
|
+
// What the commands of the run in a sandbox's runner know of it: the ones it started before a
|
|
198
|
+
// restart and how far its log held their output, and the `output` events that say how far it
|
|
199
|
+
// holds it now. They go out through `emit` after the log lines they count, so the registry
|
|
200
|
+
// never keeps an offset ahead of its log.
|
|
201
|
+
function getRunnerCommands(input, emit) {
|
|
202
|
+
return (0, runner_commands_1.createRunnerCommands)({
|
|
203
|
+
...(input.resumed ? { resumed: input.resumed.outputs } : {}),
|
|
204
|
+
reached: (command, record) => emit({ type: 'output', command, ...record }),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
exports.getRunnerCommands = getRunnerCommands;
|
|
208
|
+
// What the commands of a run that ended left in the sandbox goes: they are killed and their
|
|
209
|
+
// directory removed. Not when the server went (it stops, or died): the run resumes with the
|
|
210
|
+
// next one, which takes its commands up again. A server that stops kills this process before.
|
|
211
|
+
async function removeCommandsOfRun(input, commands, emit) {
|
|
212
|
+
const kubernetes = input.kubernetes;
|
|
213
|
+
const workspace = kubernetes?.server?.workspace;
|
|
214
|
+
if (!kubernetes || !workspace?.runner || commands.executions.size === 0) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
await (0, sandbox_runner_1.removeRunCommands)((0, kubernetes_instance_1.createKubernetesInstances)(kubernetes), kubernetes.namespace, workspace.label, kubernetes.server.run, {
|
|
219
|
+
clean: true,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
catch (e) {
|
|
223
|
+
emit({
|
|
224
|
+
type: 'log',
|
|
225
|
+
task: 'hammerkit',
|
|
226
|
+
stream: 'stderr',
|
|
227
|
+
text: `what the commands of the run left in the sandbox could not be removed: ${(0, log_1.getErrorMessage)(e)}`,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.removeCommandsOfRun = removeCommandsOfRun;
|
|
232
|
+
// What a task says in the process of a run, as events: the lines it prints (with where the log
|
|
233
|
+
// of its pod got to) and its warnings and errors, in the order they come. The `output` events
|
|
234
|
+
// of the commands in a runner are emitted after the lines they count, on the same way (see
|
|
235
|
+
// getRunnerCommands).
|
|
236
|
+
function relayStatus(status, emit) {
|
|
237
|
+
return status.on((message) => {
|
|
238
|
+
const event = getLogEvent(message);
|
|
239
|
+
if (event) {
|
|
240
|
+
emit(event);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
exports.relayStatus = relayStatus;
|
|
148
245
|
/**
|
|
149
246
|
* Executes the tasks of a run with the engine of `hammerkit run`. The caller says which
|
|
150
247
|
* process this is in: it reports what happens through `emit` and stops when `abortCtrl`
|
|
@@ -154,7 +251,12 @@ async function runTasks(input, cwd, processEnvs, abortCtrl,
|
|
|
154
251
|
// whether the run was stopped from outside; the engine aborts too when a task fails
|
|
155
252
|
cancelled, emit,
|
|
156
253
|
// a service run: its services are ready, at these addresses
|
|
157
|
-
up
|
|
254
|
+
up,
|
|
255
|
+
// its commands in a sandbox's runner
|
|
256
|
+
commands = getRunnerCommands(input, emit),
|
|
257
|
+
// a service run: hands the changed files of its sandbox to the services that are up while
|
|
258
|
+
// they are up (undefined once they are not)
|
|
259
|
+
register) {
|
|
158
260
|
const secrets = (0, redact_1.createSecretRegistry)();
|
|
159
261
|
const status = (0, work_item_status_1.statusConsole)((0, empty_writable_1.emptyWritable)(), secrets);
|
|
160
262
|
// the process starts in a directory of the server, the tasks work on the run's files
|
|
@@ -168,6 +270,7 @@ up) {
|
|
|
168
270
|
accounts: input.accounts,
|
|
169
271
|
kubernetes: input.kubernetes,
|
|
170
272
|
capabilityRuntime: input.capabilityRuntime,
|
|
273
|
+
runnerCommands: commands,
|
|
171
274
|
},
|
|
172
275
|
abortCtrl,
|
|
173
276
|
cwd: work,
|
|
@@ -179,24 +282,7 @@ up) {
|
|
|
179
282
|
stderr: (0, empty_writable_1.emptyWritable)(),
|
|
180
283
|
stdoutColumns: 120,
|
|
181
284
|
};
|
|
182
|
-
const listener = status
|
|
183
|
-
if (message.type === 'console') {
|
|
184
|
-
emit({
|
|
185
|
-
type: 'log',
|
|
186
|
-
task: message.context.name,
|
|
187
|
-
stream: message.console,
|
|
188
|
-
text: message.message.slice(0, MAX_LOG_TEXT),
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
else if (message.level === 'error' || message.level === 'warn') {
|
|
192
|
-
emit({
|
|
193
|
-
type: 'log',
|
|
194
|
-
task: message.context.name,
|
|
195
|
-
stream: 'stderr',
|
|
196
|
-
text: message.message.slice(0, MAX_LOG_TEXT),
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
});
|
|
285
|
+
const listener = relayStatus(status, emit);
|
|
200
286
|
try {
|
|
201
287
|
const options = input.options;
|
|
202
288
|
const execOptions = {
|
|
@@ -260,6 +346,10 @@ up) {
|
|
|
260
346
|
const execution = input.up
|
|
261
347
|
? cli.up({ ...execOptions, workers: cli.defaultWorkers(), daemon: false, wait: 'ready' })
|
|
262
348
|
: cli.exec({ ...execOptions, workers: cli.defaultWorkers() });
|
|
349
|
+
if (input.up) {
|
|
350
|
+
const up = input.up;
|
|
351
|
+
register?.((changes) => syncServices(workTree, up, changes));
|
|
352
|
+
}
|
|
263
353
|
const seen = new Map();
|
|
264
354
|
const failedServices = new Set();
|
|
265
355
|
let ready = false;
|
|
@@ -278,6 +368,9 @@ up) {
|
|
|
278
368
|
try {
|
|
279
369
|
const result = await execution.start();
|
|
280
370
|
report();
|
|
371
|
+
if (input.artifacts && !input.up) {
|
|
372
|
+
await keepRunArtifacts(workTree, environment, input.artifacts, work, cancelled, emit, (text) => secrets.redact(text));
|
|
373
|
+
}
|
|
281
374
|
// on a cluster the services stay in their Deployments once they are ready, until
|
|
282
375
|
// the namespace of the run goes
|
|
283
376
|
if (input.up && input.kubernetes && ready && result.success && !abortCtrl.signal.aborted) {
|
|
@@ -320,6 +413,9 @@ up) {
|
|
|
320
413
|
}
|
|
321
414
|
finally {
|
|
322
415
|
watch.close();
|
|
416
|
+
if (input.up) {
|
|
417
|
+
register?.(undefined);
|
|
418
|
+
}
|
|
323
419
|
if (input.up && input.kubernetes?.server?.serviceRun && !ready && service_rollouts_1.serviceRollouts.size > 0) {
|
|
324
420
|
// a move (or a start) whose services did not all get up: the run stays where it was
|
|
325
421
|
await service_rollouts_1.serviceRollouts.undo((0, kubernetes_instance_1.createKubernetesInstances)(input.kubernetes), (text) => emit({ type: 'log', task: 'hammerkit', stream: 'stderr', text }));
|
|
@@ -332,6 +428,24 @@ up) {
|
|
|
332
428
|
}
|
|
333
429
|
}
|
|
334
430
|
exports.runTasks = runTasks;
|
|
431
|
+
// The outputs the tasks export, kept for a download whether the run failed or not: when a
|
|
432
|
+
// test failed its screenshots matter most. Into the log goes what was kept and what was not;
|
|
433
|
+
// a run ends as its tasks did, whatever happened to its outputs.
|
|
434
|
+
async function keepRunArtifacts(workTree, environment, artifacts, root, cancelled, emit, redact) {
|
|
435
|
+
const say = (stream, text) => emit({ type: 'log', task: 'hammerkit', stream, text: redact(text).slice(0, MAX_LOG_TEXT) });
|
|
436
|
+
try {
|
|
437
|
+
const kept = await (0, keep_artifacts_1.keepArtifacts)(workTree, environment, { ...artifacts, root, cancelled, redact });
|
|
438
|
+
for (const output of kept.outputs) {
|
|
439
|
+
say('stdout', `kept ${output.path} of ${output.task} for a download (${(0, units_2.formatSize)(output.bytes)})`);
|
|
440
|
+
}
|
|
441
|
+
for (const output of kept.left) {
|
|
442
|
+
say('stderr', `did not keep ${output.path} of ${output.task}: ${output.reason}`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
catch (e) {
|
|
446
|
+
say('stderr', `the outputs of the run could not be kept: ${(0, log_1.getErrorMessage)(e)}`);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
335
449
|
// What the cluster said of a run that failed, into its log: the server removes its pods
|
|
336
450
|
// once it ended.
|
|
337
451
|
async function reportClusterState(kubernetes, workTree, ownNamespace, emit, redact) {
|
|
@@ -497,11 +611,106 @@ redact) {
|
|
|
497
611
|
}
|
|
498
612
|
}
|
|
499
613
|
}
|
|
614
|
+
/**
|
|
615
|
+
* Hands the files of a sandbox that changed to the services of a service run that are up:
|
|
616
|
+
* each one that takes files (its runtime says so) syncs them, and starts again when the run
|
|
617
|
+
* lists it in `restart`. The other services are not touched.
|
|
618
|
+
*/
|
|
619
|
+
async function syncServices(workTree, up, changes) {
|
|
620
|
+
// one service that fails does not hold back the others: they are all tried, and the failures
|
|
621
|
+
// are said together
|
|
622
|
+
const failed = [];
|
|
623
|
+
for (const service of Object.values(workTree.services)) {
|
|
624
|
+
if (!up.services.includes(service.name)) {
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
try {
|
|
628
|
+
await service.runtime.syncFiles?.({ ...changes, restart: (up.restart ?? []).includes(service.name) });
|
|
629
|
+
}
|
|
630
|
+
catch (e) {
|
|
631
|
+
failed.push(`${service.name}: ${(0, log_1.getErrorMessage)(e)}`);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (failed.length > 0) {
|
|
635
|
+
throw new Error(`the changed files were not synced to ${failed.join('; ')}`);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
exports.syncServices = syncServices;
|
|
639
|
+
// The line `sync <json>` the server writes for a batch of changes: FileChanges, with its archive
|
|
640
|
+
// in base64
|
|
641
|
+
exports.syncLine = zod_1.z
|
|
642
|
+
.object({
|
|
643
|
+
archive: zod_1.z.string(),
|
|
644
|
+
written: zod_1.z.array(zod_1.z.string()),
|
|
645
|
+
deleted: zod_1.z.array(zod_1.z.string()),
|
|
646
|
+
restart: zod_1.z.boolean(),
|
|
647
|
+
})
|
|
648
|
+
.strict();
|
|
649
|
+
// The changes of a sync line, or undefined when the line does not match
|
|
650
|
+
function parseChanges(request) {
|
|
651
|
+
let json;
|
|
652
|
+
try {
|
|
653
|
+
json = JSON.parse(request);
|
|
654
|
+
}
|
|
655
|
+
catch {
|
|
656
|
+
return undefined;
|
|
657
|
+
}
|
|
658
|
+
const parsed = exports.syncLine.safeParse(json);
|
|
659
|
+
if (!parsed.success) {
|
|
660
|
+
return undefined;
|
|
661
|
+
}
|
|
662
|
+
return { ...parsed.data, archive: Buffer.from(parsed.data.archive, 'base64') };
|
|
663
|
+
}
|
|
664
|
+
exports.parseChanges = parseChanges;
|
|
665
|
+
/**
|
|
666
|
+
* Applies one batch of changes to the services that are up (`sync`). A line that does not parse
|
|
667
|
+
* is logged, and nothing else is done.
|
|
668
|
+
*/
|
|
669
|
+
async function applyChanges(request, sync, log,
|
|
670
|
+
// told when the batch was not applied: the server then sends the full set again
|
|
671
|
+
failed = () => undefined) {
|
|
672
|
+
const changes = parseChanges(request);
|
|
673
|
+
if (!changes) {
|
|
674
|
+
log('the changed files of the sandbox could not be read');
|
|
675
|
+
failed();
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
if (!sync) {
|
|
679
|
+
log('no service of the run is up to take the changed files of the sandbox');
|
|
680
|
+
failed();
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
try {
|
|
684
|
+
await sync(changes);
|
|
685
|
+
}
|
|
686
|
+
catch (e) {
|
|
687
|
+
log(`the changed files of the sandbox could not be synced: ${(0, log_1.getErrorMessage)(e)}`);
|
|
688
|
+
failed();
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
exports.applyChanges = applyChanges;
|
|
692
|
+
/**
|
|
693
|
+
* The batches of changes of a run, applied one after another in the order they came. A batch
|
|
694
|
+
* that fails does not stop the ones after it.
|
|
695
|
+
*/
|
|
696
|
+
class ChangeQueue {
|
|
697
|
+
constructor(apply, fail) {
|
|
698
|
+
this.apply = apply;
|
|
699
|
+
this.fail = fail;
|
|
700
|
+
this.last = Promise.resolve();
|
|
701
|
+
}
|
|
702
|
+
push(request) {
|
|
703
|
+
this.last = this.last.then(() => this.apply(request)).catch((e) => this.fail(e));
|
|
704
|
+
return this.last;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
exports.ChangeQueue = ChangeQueue;
|
|
500
708
|
// The run is the first line of the standard input. The server keeps it open and writes
|
|
501
709
|
// `cancel` on it to stop the run: that works the same on every platform, where a signal
|
|
502
|
-
// on Windows ends the process without letting it stop its tasks.
|
|
710
|
+
// on Windows ends the process without letting it stop its tasks. It writes `sync <json>` on
|
|
711
|
+
// it for the files of a sandbox that changed (FileChanges), which `sync` takes.
|
|
503
712
|
// `onEnd` is told when the input ends before the run does: the server is gone.
|
|
504
|
-
function readInput(stdin, cancel) {
|
|
713
|
+
function readInput(stdin, cancel, sync = () => undefined) {
|
|
505
714
|
const lines = (0, readline_1.createInterface)({ input: stdin, crlfDelay: Infinity });
|
|
506
715
|
let first;
|
|
507
716
|
let closing = false;
|
|
@@ -516,6 +725,9 @@ function readInput(stdin, cancel) {
|
|
|
516
725
|
else if (line.trim() === 'cancel') {
|
|
517
726
|
cancel();
|
|
518
727
|
}
|
|
728
|
+
else if (line.startsWith('sync ')) {
|
|
729
|
+
sync(line.slice('sync '.length));
|
|
730
|
+
}
|
|
519
731
|
});
|
|
520
732
|
lines.on('close', () => {
|
|
521
733
|
resolve(first ?? '');
|
|
@@ -555,7 +767,13 @@ async function workerMain(stdin, stdout, processEnvs, cwd, abortCtrl) {
|
|
|
555
767
|
};
|
|
556
768
|
const signals = ['SIGINT', 'SIGTERM', 'SIGHUP'];
|
|
557
769
|
signals.forEach((signal) => process.on(signal, stop));
|
|
558
|
-
|
|
770
|
+
// the changes of the sandbox, one batch after the other, for the services that are up
|
|
771
|
+
let sync;
|
|
772
|
+
const logLine = (text) => say({ kind: 'event', event: { type: 'log', task: 'hammerkit', stream: 'stderr', text } });
|
|
773
|
+
const batches = new ChangeQueue((request) => applyChanges(request, sync, logLine, () => say({ kind: 'sync-failed' })), (e) => logLine(`the changed files of the sandbox could not be synced: ${(0, log_1.getErrorMessage)(e)}`));
|
|
774
|
+
const reader = readInput(stdin, stop, (request) => {
|
|
775
|
+
void batches.push(request);
|
|
776
|
+
});
|
|
559
777
|
let input;
|
|
560
778
|
try {
|
|
561
779
|
input = exports.workerInputSchema.parse(JSON.parse(await reader.input));
|
|
@@ -566,17 +784,33 @@ async function workerMain(stdin, stdout, processEnvs, cwd, abortCtrl) {
|
|
|
566
784
|
say({ kind: 'result', ok: false, error: `the run could not be read: ${e instanceof Error ? e.message : e}` });
|
|
567
785
|
return;
|
|
568
786
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
787
|
+
// the input ends before the run does: the server is gone
|
|
788
|
+
let gone = false;
|
|
789
|
+
reader.onEnd(() => {
|
|
790
|
+
gone = true;
|
|
791
|
+
if (input.up) {
|
|
792
|
+
// services stay until they are stopped: without the server that would stop them, at
|
|
793
|
+
// their ttl or on a cancel, nothing would
|
|
794
|
+
stop();
|
|
795
|
+
}
|
|
796
|
+
});
|
|
797
|
+
const emit = (event) => say({ kind: 'event', event });
|
|
798
|
+
const commands = getRunnerCommands(input, emit);
|
|
574
799
|
try {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
800
|
+
let result;
|
|
801
|
+
try {
|
|
802
|
+
await runTasks(input, cwd, processEnvs, abortCtrl, () => cancelled, emit, (targets) => say({ kind: 'up', targets }), commands, (handler) => {
|
|
803
|
+
sync = handler;
|
|
804
|
+
});
|
|
805
|
+
result = { kind: 'result', ok: true };
|
|
806
|
+
}
|
|
807
|
+
catch (e) {
|
|
808
|
+
result = { kind: 'result', ok: false, error: e instanceof Error ? e.message : String(e) };
|
|
809
|
+
}
|
|
810
|
+
if (!gone) {
|
|
811
|
+
await removeCommandsOfRun(input, commands, emit);
|
|
812
|
+
}
|
|
813
|
+
say(result);
|
|
580
814
|
}
|
|
581
815
|
finally {
|
|
582
816
|
reader.close();
|