hammerkit 1.27.0-next.9 → 2.0.0-next.2
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 +176 -6
- package/dist/cache/backends/local-cache-backend.js +6 -0
- package/dist/cache/backends/registry/registry-client.js +1 -0
- package/dist/cache/cache-sync.js +1 -1
- package/dist/cache/explain.js +17 -0
- package/dist/cli.js +2 -0
- package/dist/dashboard/assets/index-CjnKm5-3.css +2 -0
- package/dist/dashboard/assets/index-zPXa4klZ.js +13 -0
- package/dist/dashboard/favicon.svg +1 -0
- package/dist/dashboard/index.html +15 -0
- package/dist/docker/item-network.js +13 -5
- package/dist/executer/archive-task-entry.js +4 -2
- package/dist/executer/deadline.js +17 -3
- package/dist/executer/deferred-restore.js +36 -0
- package/dist/executer/docker-service.js +30 -8
- package/dist/executer/docker-task.js +21 -1
- package/dist/executer/execute-work-service.js +2 -0
- package/dist/executer/execute-work-task.js +11 -4
- package/dist/executer/execution-steps.js +25 -4
- package/dist/executer/reset-work-tree.js +4 -0
- package/dist/executer/scheduler/enqueue-next.js +57 -6
- package/dist/executer/scheduler/state-key.js +42 -5
- package/dist/executer/watch-loop.js +6 -3
- package/dist/harness/connect.js +20 -6
- package/dist/harness/mcp-command.js +22 -6
- package/dist/harness/mcp.js +63 -114
- package/dist/harness/opencode.js +158 -50
- package/dist/harness/pi.js +29 -0
- package/dist/harness/sandbox-client.js +194 -23
- package/dist/harness/service-tools.js +141 -0
- package/dist/harness/task-tools.js +221 -0
- package/dist/kubernetes/apply.js +49 -1
- package/dist/kubernetes/await-running-state.js +172 -13
- package/dist/kubernetes/cluster-state.js +245 -0
- package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +133 -9
- package/dist/kubernetes/ensure-kubernetes-service-exists.js +2 -0
- package/dist/kubernetes/ensure-persistent-data.js +211 -6
- package/dist/kubernetes/follow-pod-logs.js +182 -0
- package/dist/kubernetes/job-placement.js +36 -12
- package/dist/kubernetes/kubernetes-instance.js +1 -0
- package/dist/kubernetes/network-policy.js +2 -1
- package/dist/kubernetes/remove-stale-items.js +46 -0
- package/dist/kubernetes/restore-kubernetes-data.js +9 -5
- package/dist/kubernetes/sandbox-runner.js +628 -0
- package/dist/kubernetes/service-rollouts.js +74 -0
- package/dist/kubernetes/store-kubernetes-data.js +5 -2
- package/dist/kubernetes/volumes.js +94 -9
- package/dist/main.js +2 -0
- package/dist/optimizer/calculate-checksum.js +3 -2
- package/dist/optimizer/check-key.js +94 -0
- package/dist/optimizer/check-keys.js +64 -0
- package/dist/optimizer/get-work-cache-stats.js +17 -3
- package/dist/optimizer/work-task-cache-description.js +49 -1
- package/dist/planner/service-definition.js +9 -4
- package/dist/planner/utils/append-work-dependencies.js +5 -3
- package/dist/planner/utils/append-work-service.js +18 -1
- package/dist/planner/utils/append-work-task.js +13 -3
- package/dist/planner/utils/get-work-runtime.js +6 -1
- package/dist/planner/utils/normalize-path.js +19 -1
- package/dist/planner/utils/parse-work-mount.js +7 -1
- package/dist/planner/utils/parse-work-port.js +4 -1
- package/dist/planner/validate.js +22 -0
- package/dist/planner/work-network.js +3 -1
- package/dist/planner/work-runtime-aggregate.js +32 -0
- package/dist/planner/work-runtime-kubernetes.js +136 -13
- package/dist/planner/work-task.js +6 -1
- package/dist/program.js +27 -9
- package/dist/remote/device-login.js +7 -2
- package/dist/remote/host-config.js +69 -3
- package/dist/remote/remote-commands.js +33 -7
- package/dist/remote/remote-status.js +112 -0
- package/dist/remote/remote-work-commands.js +562 -45
- package/dist/remote/server-client.js +15 -2
- package/dist/remote/token-source.js +64 -26
- package/dist/run-program.js +9 -0
- package/dist/schema/build-file-base-task-schema.js +3 -3
- package/dist/schema/build-file-container-service-schema.js +10 -0
- package/dist/schema/build-file-container-task-schema.js +16 -1
- package/dist/schema/build-file-include-schema.js +18 -4
- package/dist/schema/build-file-kubernetes-service-schema.js +2 -0
- package/dist/schema/build-file-task-schema.js +5 -1
- package/dist/schema/capabilities-schema.js +56 -0
- package/dist/schema/reference-parser.js +119 -11
- package/dist/schema/removed-properties.js +20 -0
- package/dist/schema/schema-parser.js +94 -41
- package/dist/schema/work-scope-parser.js +33 -10
- package/dist/secrets/command-secret-provider.js +3 -1
- package/dist/secrets/github-app-token-command.js +3 -1
- package/dist/secrets/secret-catalog.js +43 -19
- package/dist/server/accounts-api.js +4 -1
- package/dist/server/approvals-api.js +1 -1
- package/dist/server/capacity-format.js +36 -0
- package/dist/server/capacity.js +300 -0
- package/dist/server/checks-api.js +83 -0
- package/dist/server/checks.js +116 -0
- package/dist/server/dashboard.js +196 -0
- package/dist/server/definitions-api.js +66 -3
- package/dist/server/endpoints.js +794 -0
- package/dist/server/executor.js +164 -9
- package/dist/server/kubernetes-backend.js +415 -48
- package/dist/server/kubernetes-sandbox-storage.js +46 -2
- package/dist/server/overview.js +271 -0
- package/dist/server/plan.js +170 -121
- package/dist/server/policy.js +34 -2
- package/dist/server/proposals-api.js +17 -8
- package/dist/server/repo-access.js +5 -0
- package/dist/server/run-logs.js +4 -0
- package/dist/server/run-plan.js +38 -4
- package/dist/server/run-request.js +58 -3
- package/dist/server/run-worker.js +262 -13
- package/dist/server/runs.js +962 -36
- package/dist/server/sandbox-pod.js +28 -0
- package/dist/server/sandbox-storage.js +23 -0
- package/dist/server/sandboxes.js +8 -0
- package/dist/server/server-config.js +246 -10
- package/dist/server/server.js +429 -32
- package/dist/server/service-runs.js +82 -0
- package/dist/server/timings.js +132 -0
- package/dist/server/wait-reason.js +21 -0
- package/dist/utils/tar.js +28 -1
- package/dist/utils/terminal-prompt.js +53 -0
- package/package.json +8 -2
package/dist/harness/pi.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.hammerkitPiExtension = exports.createPiOperations = void 0;
|
|
|
4
4
|
const path_1 = require("path");
|
|
5
5
|
const connect_1 = require("./connect");
|
|
6
6
|
const format_1 = require("./format");
|
|
7
|
+
const service_tools_1 = require("./service-tools");
|
|
7
8
|
const sandbox_client_1 = require("./sandbox-client");
|
|
8
9
|
const IMAGE_TYPES = {
|
|
9
10
|
'.png': 'image/png',
|
|
@@ -178,6 +179,34 @@ function hammerkitPiExtension(library, settings = {}) {
|
|
|
178
179
|
return { content: [{ type: 'text', text: `pushed ${pushed.commit} to ${pushed.branch}` }], details: pushed };
|
|
179
180
|
},
|
|
180
181
|
});
|
|
182
|
+
for (const tool of service_tools_1.SERVICE_TOOLS) {
|
|
183
|
+
register({
|
|
184
|
+
name: tool.name,
|
|
185
|
+
label: tool.name,
|
|
186
|
+
description: tool.description,
|
|
187
|
+
promptSnippet: tool.title,
|
|
188
|
+
parameters: (0, service_tools_1.serviceToolSchema)(tool),
|
|
189
|
+
execute: async (_id, params, signal) => {
|
|
190
|
+
const text = await tool.call(await client(), params, signal ?? new AbortController().signal);
|
|
191
|
+
return { content: [{ type: 'text', text }], details: undefined };
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
register({
|
|
196
|
+
name: 'check',
|
|
197
|
+
label: 'check',
|
|
198
|
+
description: sandbox_client_1.CHECK_DESCRIPTION,
|
|
199
|
+
promptSnippet: 'Run a task as a check that CI verifies after the push',
|
|
200
|
+
parameters: {
|
|
201
|
+
type: 'object',
|
|
202
|
+
properties: { task: { type: 'string', description: 'The name of the task' } },
|
|
203
|
+
required: ['task'],
|
|
204
|
+
},
|
|
205
|
+
execute: async (_id, params, signal) => {
|
|
206
|
+
const text = await (0, sandbox_client_1.runCheck)(await client(), params.task, signal);
|
|
207
|
+
return { content: [{ type: 'text', text }], details: { task: params.task } };
|
|
208
|
+
},
|
|
209
|
+
});
|
|
181
210
|
pi.on('session_start', async (_event, ctx) => {
|
|
182
211
|
try {
|
|
183
212
|
const sandbox = await client();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.followRun = exports.submitRun = exports.FINAL_RUN_STATES = exports.oneAtATime = exports.SandboxClient = exports.SandboxClientError = void 0;
|
|
3
|
+
exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.getRun = exports.followRun = exports.getMoveTarget = exports.submitRun = exports.FINAL_RUN_STATES = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.SandboxClientError = void 0;
|
|
4
4
|
const path_1 = require("path");
|
|
5
5
|
const token_source_1 = require("../remote/token-source");
|
|
6
6
|
// a write carries up to 32 MiB
|
|
@@ -16,6 +16,48 @@ class SandboxClientError extends Error {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
exports.SandboxClientError = SandboxClientError;
|
|
19
|
+
// What the agent is told of a check, on each harness.
|
|
20
|
+
exports.CHECK_DESCRIPTION = 'Run a task as a check before you push, such as test or e2e: it runs on a copy of the files a push ' +
|
|
21
|
+
'would commit (what .gitignore leaves out is not there, so dependencies are installed by the tasks ' +
|
|
22
|
+
'the build file names), and when it passes, CI verifies it for that commit instead of running it. ' +
|
|
23
|
+
'Push without changing files afterwards, or the check no longer counts for the files that changed.';
|
|
24
|
+
// A check, run to its end, in a few lines: how it went and the last lines of a failure.
|
|
25
|
+
async function runCheck(client, task, signal) {
|
|
26
|
+
const ran = await client.runTask(task, { check: true, signal });
|
|
27
|
+
const result = ran.result;
|
|
28
|
+
const lines = [
|
|
29
|
+
result?.status === 'succeeded' || result?.status === 'cached'
|
|
30
|
+
? `check ${task} passed (run ${ran.run.id}${result.status === 'cached' ? ', from the cache' : ''}): CI can verify it for a push of these files`
|
|
31
|
+
: `check ${task} did not pass (run ${ran.run.id}: ${result?.status ?? ran.run.state}${result?.exitCode !== undefined ? `, exit code ${result.exitCode}` : ''})`,
|
|
32
|
+
];
|
|
33
|
+
const error = result?.error ?? ran.run.error;
|
|
34
|
+
if (error) {
|
|
35
|
+
lines.push(`error: ${error}`);
|
|
36
|
+
}
|
|
37
|
+
if (result && result.status !== 'succeeded' && result.status !== 'cached' && result.lastLog.length > 0) {
|
|
38
|
+
lines.push('last lines:', ...result.lastLog);
|
|
39
|
+
}
|
|
40
|
+
return lines.join('\n');
|
|
41
|
+
}
|
|
42
|
+
exports.runCheck = runCheck;
|
|
43
|
+
/**
|
|
44
|
+
* The tasks of a published version, without its services, as `GET /v1/definitions/<hash>`
|
|
45
|
+
* answers them.
|
|
46
|
+
*/
|
|
47
|
+
async function listPublishedTasks(connection, hash) {
|
|
48
|
+
const version = await json(await call(connection, 'GET', `/v1/definitions/${hash}`));
|
|
49
|
+
return version.tasks.map((name) => {
|
|
50
|
+
const plan = version.plan?.[name];
|
|
51
|
+
const args = version.arguments?.[name];
|
|
52
|
+
return {
|
|
53
|
+
name,
|
|
54
|
+
...(plan?.description ? { description: plan.description } : {}),
|
|
55
|
+
...(plan && plan.command !== undefined ? { command: plan.command } : {}),
|
|
56
|
+
...(args !== undefined ? { args } : {}),
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
exports.listPublishedTasks = listPublishedTasks;
|
|
19
61
|
/**
|
|
20
62
|
* A caller's sandbox on a hammerkit server, over its HTTP API: the raw read, write and
|
|
21
63
|
* stat that harnesses plug their own tools into, the typed endpoints for clients without
|
|
@@ -34,6 +76,10 @@ class SandboxClient {
|
|
|
34
76
|
get id() {
|
|
35
77
|
return this.sandbox.id;
|
|
36
78
|
}
|
|
79
|
+
// the tasks of the published version that run on the sandbox
|
|
80
|
+
tasks() {
|
|
81
|
+
return listPublishedTasks(this.connection, this.build.hash);
|
|
82
|
+
}
|
|
37
83
|
static async create(connection, repo, ref) {
|
|
38
84
|
return json(await call(connection, 'POST', '/v1/sandboxes', { body: { repo, ref } }));
|
|
39
85
|
}
|
|
@@ -115,12 +161,13 @@ class SandboxClient {
|
|
|
115
161
|
runOptions.args = options.args;
|
|
116
162
|
}
|
|
117
163
|
if (options.timeoutSeconds) {
|
|
118
|
-
|
|
164
|
+
// a cap: it also shortens a shell task that sets a longer timeout of its own
|
|
165
|
+
runOptions.maxTimeout = `${Math.max(1, Math.ceil(options.timeoutSeconds))}s`;
|
|
119
166
|
}
|
|
120
167
|
const submitted = await submitRun(this.connection, {
|
|
121
168
|
definition: this.build.hash,
|
|
122
169
|
tasks: [task],
|
|
123
|
-
source: { type: 'sandbox', id: this.id },
|
|
170
|
+
source: { type: 'sandbox', id: this.id, ...(options.check ? { check: true } : {}) },
|
|
124
171
|
options: runOptions,
|
|
125
172
|
});
|
|
126
173
|
const followed = await followRun(this.connection, submitted, {
|
|
@@ -138,11 +185,68 @@ class SandboxClient {
|
|
|
138
185
|
exitCode: exitCodeOf(result),
|
|
139
186
|
};
|
|
140
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Starts services of the published build file on the sandbox, or on a branch of its
|
|
190
|
+
* repository, and follows them until they are up: the run then holds their endpoints
|
|
191
|
+
* until its ttl ends or it is stopped. A signal cancels the run.
|
|
192
|
+
*/
|
|
193
|
+
async up(request, options = {}) {
|
|
194
|
+
if (options.signal?.aborted) {
|
|
195
|
+
throw new SandboxClientError('the run was cancelled');
|
|
196
|
+
}
|
|
197
|
+
const submitted = await submitRun(this.connection, {
|
|
198
|
+
definition: this.build.hash,
|
|
199
|
+
services: request.services,
|
|
200
|
+
endpoints: request.endpoints ?? [],
|
|
201
|
+
...(request.ttl ? { ttl: request.ttl } : {}),
|
|
202
|
+
...(request.sleepTtl ? { sleepTtl: request.sleepTtl } : {}),
|
|
203
|
+
...(request.name ? { name: request.name } : {}),
|
|
204
|
+
source: request.branch
|
|
205
|
+
? { type: 'ref', repo: this.sandbox.repo, ref: request.branch }
|
|
206
|
+
: { type: 'sandbox', id: this.id },
|
|
207
|
+
});
|
|
208
|
+
return followRun(this.connection, submitted, { signal: options.signal, untilUp: true });
|
|
209
|
+
}
|
|
210
|
+
// Stops a run: a service run takes its services and endpoints down.
|
|
211
|
+
async stop(id) {
|
|
212
|
+
return json(await call(this.connection, 'POST', `/v1/runs/${encodeURIComponent(id)}/cancel`));
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* A page of an endpoint of a service run, as the caller sees it: only at an address the
|
|
216
|
+
* server gave the run, so the token goes to the server and nowhere else.
|
|
217
|
+
*/
|
|
218
|
+
async fetchEndpoint(id, path, service, signal) {
|
|
219
|
+
const run = await this.getRun(id);
|
|
220
|
+
const endpoint = run.up?.endpoints.find((candidate) => service === undefined || candidate.service === service);
|
|
221
|
+
if (!endpoint) {
|
|
222
|
+
throw new SandboxClientError(run.up
|
|
223
|
+
? `${id} has no endpoint${service ? ` of ${service}` : ''}, it has ${run.up.endpoints.map((candidate) => `${candidate.service}:${candidate.port}`).join(', ') || 'none'}`
|
|
224
|
+
: `${id} is not a service run`);
|
|
225
|
+
}
|
|
226
|
+
const url = new URL(path.startsWith('/') ? path : `/${path}`, endpoint.url);
|
|
227
|
+
if (url.origin !== new URL(endpoint.url).origin) {
|
|
228
|
+
throw new SandboxClientError(`${path} is not a path of ${endpoint.url}`);
|
|
229
|
+
}
|
|
230
|
+
const response = await fetch(url, {
|
|
231
|
+
headers: { 'hammerkit-authorization': `Bearer ${await this.connection.token()}` },
|
|
232
|
+
redirect: 'manual',
|
|
233
|
+
signal: AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS), ...(signal ? [signal] : [])]),
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
url: url.toString(),
|
|
237
|
+
status: response.status,
|
|
238
|
+
type: response.headers.get('content-type') ?? '',
|
|
239
|
+
body: await response.text(),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
141
242
|
/**
|
|
142
243
|
* A shell command on the sandbox: the shell task of the build file with the command as
|
|
143
244
|
* its argument. `cwd` is a directory of the sandbox, `''` for its root.
|
|
144
245
|
*/
|
|
145
246
|
async exec(command, cwd, options = {}) {
|
|
247
|
+
if (!this.build.shell) {
|
|
248
|
+
throw new SandboxClientError('there is no shell on this sandbox (HAMMERKIT_SHELL is empty), run a task instead');
|
|
249
|
+
}
|
|
146
250
|
// on its own line: `cd dir && a; b` would run b in the root when dir is missing
|
|
147
251
|
const script = cwd && cwd !== '.' ? `cd -- ${quotePosix(cwd)} || exit 1\n${command}` : command;
|
|
148
252
|
// a shell task in a container without `mounts: [.]` runs in an empty directory: the
|
|
@@ -203,25 +307,47 @@ function oneAtATime() {
|
|
|
203
307
|
exports.oneAtATime = oneAtATime;
|
|
204
308
|
// a run that is over, its state does not change any more
|
|
205
309
|
exports.FINAL_RUN_STATES = ['succeeded', 'failed', 'cancelled', 'rejected'];
|
|
206
|
-
// Submits a run, as `POST /v1/runs` takes it, and answers it as the server queued it or
|
|
207
|
-
// holds it for an approval.
|
|
208
310
|
async function submitRun(connection, request) {
|
|
209
311
|
return json(await call(connection, 'POST', '/v1/runs', { body: request }));
|
|
210
312
|
}
|
|
211
313
|
exports.submitRun = submitRun;
|
|
212
314
|
/**
|
|
213
315
|
* Follows a run to its end: its log as it grows, and its state. A signal cancels the run
|
|
214
|
-
* on the server and fails the follow.
|
|
316
|
+
* on the server and fails the follow. A service run that moved to another commit is followed
|
|
317
|
+
* from where its log was until it is there; a signal leaves it be, it was up before.
|
|
215
318
|
*/
|
|
319
|
+
/** The commit a service run that moved was submitted to move to */
|
|
320
|
+
function getMoveTarget(submitted) {
|
|
321
|
+
if (!submitted.moved) {
|
|
322
|
+
return undefined;
|
|
323
|
+
}
|
|
324
|
+
return submitted.moving?.commit ?? (submitted.source.type === 'ref' ? submitted.source.commit : undefined);
|
|
325
|
+
}
|
|
326
|
+
exports.getMoveTarget = getMoveTarget;
|
|
327
|
+
// A service run is up; one that moved is where the move took it: up or asleep at the commit,
|
|
328
|
+
// or back where it was, once it does not move there anymore
|
|
329
|
+
function isThere(run, submitted) {
|
|
330
|
+
if (!submitted.moved) {
|
|
331
|
+
return run.state === 'up' && !run.moving;
|
|
332
|
+
}
|
|
333
|
+
if (run.moving?.commit === getMoveTarget(submitted)) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
return run.state === 'up' || run.state === 'asleep';
|
|
337
|
+
}
|
|
216
338
|
async function followRun(connection, submitted, options = {}) {
|
|
217
339
|
const path = `/v1/runs/${encodeURIComponent(submitted.id)}`;
|
|
218
340
|
let cancelling;
|
|
219
341
|
const cancel = () => {
|
|
220
|
-
|
|
342
|
+
if (!submitted.moved) {
|
|
343
|
+
cancelling ?? (cancelling = call(connection, 'POST', `${path}/cancel`).catch(() => undefined));
|
|
344
|
+
}
|
|
221
345
|
};
|
|
222
|
-
options.
|
|
346
|
+
if (!options.detach) {
|
|
347
|
+
options.signal?.addEventListener('abort', cancel, { once: true });
|
|
348
|
+
}
|
|
223
349
|
const lines = [];
|
|
224
|
-
let from = 1;
|
|
350
|
+
let from = submitted.moved?.logFrom ?? 1;
|
|
225
351
|
let truncated = false;
|
|
226
352
|
const add = (line) => {
|
|
227
353
|
lines.push(line);
|
|
@@ -244,37 +370,29 @@ async function followRun(connection, submitted, options = {}) {
|
|
|
244
370
|
let run = submitted;
|
|
245
371
|
let waitingSaid = false;
|
|
246
372
|
for (;;) {
|
|
373
|
+
if (options.signal?.aborted && options.detach) {
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
247
376
|
if (options.signal?.aborted) {
|
|
248
377
|
// the process may exit after this, so the cancel has to reach the server first
|
|
249
378
|
cancel();
|
|
250
379
|
let timer;
|
|
251
380
|
await Promise.race([cancelling, new Promise((resolve) => (timer = setTimeout(resolve, CANCEL_WAIT_MS)))]);
|
|
252
381
|
clearTimeout(timer);
|
|
253
|
-
throw new SandboxClientError('the run was cancelled');
|
|
382
|
+
throw new SandboxClientError(submitted.moved ? 'stopped following the run, it moves on without' : 'the run was cancelled');
|
|
254
383
|
}
|
|
255
384
|
if (run.state === 'awaiting-approval' && !waitingSaid) {
|
|
256
385
|
waitingSaid = true;
|
|
257
386
|
options.onWaiting?.(run);
|
|
258
387
|
}
|
|
259
388
|
await readLog();
|
|
260
|
-
if (exports.FINAL_RUN_STATES.includes(run.state)) {
|
|
389
|
+
if (exports.FINAL_RUN_STATES.includes(run.state) || (options.untilUp && isThere(run, submitted))) {
|
|
261
390
|
break;
|
|
262
391
|
}
|
|
263
392
|
run = await json(await call(connection, 'GET', path, { query: { wait: POLL_WAIT } }));
|
|
264
393
|
}
|
|
265
394
|
if (truncated) {
|
|
266
|
-
|
|
267
|
-
const tasks = options.task !== undefined ? [options.task] : Object.keys(run.results ?? {});
|
|
268
|
-
for (const task of tasks) {
|
|
269
|
-
const result = run.results?.[task];
|
|
270
|
-
if (!result) {
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
const said = options.task !== undefined ? 'the last lines were' : `the last lines of ${task} were`;
|
|
274
|
-
for (const text of [`… the server cut the log off at its limit, ${said}:`, ...result.lastLog]) {
|
|
275
|
-
add({ line: 0, task, stream: 'stdout', text });
|
|
276
|
-
}
|
|
277
|
-
}
|
|
395
|
+
truncatedEnd(run, options.task).forEach(add);
|
|
278
396
|
}
|
|
279
397
|
return { run, lines, truncated };
|
|
280
398
|
}
|
|
@@ -283,6 +401,59 @@ async function followRun(connection, submitted, options = {}) {
|
|
|
283
401
|
}
|
|
284
402
|
}
|
|
285
403
|
exports.followRun = followRun;
|
|
404
|
+
// The end of a log the server cut off at its limit: the last lines of each task, which its
|
|
405
|
+
// results keep.
|
|
406
|
+
function truncatedEnd(run, only) {
|
|
407
|
+
const lines = [];
|
|
408
|
+
const tasks = only !== undefined ? [only] : Object.keys(run.results ?? {});
|
|
409
|
+
for (const task of tasks) {
|
|
410
|
+
const result = run.results?.[task];
|
|
411
|
+
if (!result) {
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
const said = only !== undefined ? 'the last lines were' : `the last lines of ${task} were`;
|
|
415
|
+
for (const text of [`… the server cut the log off at its limit, ${said}:`, ...result.lastLog]) {
|
|
416
|
+
lines.push({ line: 0, task, stream: 'stdout', text });
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return lines;
|
|
420
|
+
}
|
|
421
|
+
// A run as the server keeps it, or null when it keeps none the caller may see.
|
|
422
|
+
async function getRun(connection, id) {
|
|
423
|
+
const response = await call(connection, 'GET', `/v1/runs/${encodeURIComponent(id)}`, { missing: true });
|
|
424
|
+
return response.status === 404 ? null : json(response);
|
|
425
|
+
}
|
|
426
|
+
exports.getRun = getRun;
|
|
427
|
+
/**
|
|
428
|
+
* Reads the log of a run as it is now, every line or the last `tail` of them, of one task or
|
|
429
|
+
* of all: also of a run that ended, as long as the server keeps it.
|
|
430
|
+
*/
|
|
431
|
+
async function readRunLog(connection, run, query, onLine) {
|
|
432
|
+
const path = `/v1/runs/${encodeURIComponent(run.id)}/logs`;
|
|
433
|
+
if (query.tail !== undefined) {
|
|
434
|
+
const page = await json(await call(connection, 'GET', path, { query: { task: query.task, tail: query.tail } }));
|
|
435
|
+
page.lines.forEach(onLine);
|
|
436
|
+
return { truncated: page.truncated };
|
|
437
|
+
}
|
|
438
|
+
let from = 1;
|
|
439
|
+
let truncated = false;
|
|
440
|
+
for (;;) {
|
|
441
|
+
const page = await json(await call(connection, 'GET', path, { query: { task: query.task, from, limit: 1000 } }));
|
|
442
|
+
truncated = page.truncated;
|
|
443
|
+
for (const line of page.lines) {
|
|
444
|
+
onLine(line);
|
|
445
|
+
from = line.line + 1;
|
|
446
|
+
}
|
|
447
|
+
if (!page.next) {
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (truncated) {
|
|
452
|
+
truncatedEnd(run, query.task).forEach(onLine);
|
|
453
|
+
}
|
|
454
|
+
return { truncated };
|
|
455
|
+
}
|
|
456
|
+
exports.readRunLog = readRunLog;
|
|
286
457
|
function exitCodeOf(result) {
|
|
287
458
|
if (!result) {
|
|
288
459
|
return null;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SERVICE_TOOLS = exports.describeServiceRun = exports.parseEndpoint = exports.serviceToolArgs = exports.serviceToolSchema = void 0;
|
|
4
|
+
// the most of a page an agent gets back
|
|
5
|
+
const MAX_PAGE_CHARS = 20000;
|
|
6
|
+
// The parameters as a JSON schema, for MCP, pi and opencode 2.
|
|
7
|
+
function serviceToolSchema(tool) {
|
|
8
|
+
return {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: Object.fromEntries(Object.entries(tool.parameters).map(([name, parameter]) => [
|
|
11
|
+
name,
|
|
12
|
+
parameter.type === 'string'
|
|
13
|
+
? { type: 'string', description: parameter.description }
|
|
14
|
+
: { type: 'array', items: { type: 'string' }, description: parameter.description },
|
|
15
|
+
])),
|
|
16
|
+
required: tool.required,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
exports.serviceToolSchema = serviceToolSchema;
|
|
20
|
+
// The parameters with the zod of opencode 1.
|
|
21
|
+
function serviceToolArgs(tool, z) {
|
|
22
|
+
return Object.fromEntries(Object.entries(tool.parameters).map(([name, parameter]) => {
|
|
23
|
+
const type = parameter.type === 'string' ? z.string() : z.array(z.string());
|
|
24
|
+
return [name, (tool.required.includes(name) ? type : type.optional()).describe(parameter.description)];
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
exports.serviceToolArgs = serviceToolArgs;
|
|
28
|
+
// `web:8080` as the server takes an endpoint
|
|
29
|
+
function parseEndpoint(value) {
|
|
30
|
+
const index = value.lastIndexOf(':');
|
|
31
|
+
const port = Number(value.slice(index + 1));
|
|
32
|
+
if (index <= 0 || !Number.isInteger(port) || port < 1 || port > 65535) {
|
|
33
|
+
throw new Error(`${value} is not an endpoint, write it as service:port, such as web:8080`);
|
|
34
|
+
}
|
|
35
|
+
return { service: value.slice(0, index), port };
|
|
36
|
+
}
|
|
37
|
+
exports.parseEndpoint = parseEndpoint;
|
|
38
|
+
// How a service run stands, for an agent: where its endpoints are and until when.
|
|
39
|
+
function describeServiceRun(run) {
|
|
40
|
+
const lines = [`run ${run.id}: ${run.state}${run.up ? ` (${run.up.name})` : ''}`];
|
|
41
|
+
if (run.state === 'up') {
|
|
42
|
+
lines.push(`up until ${run.expiresAt}, stop it with services_stop when it is no longer needed`);
|
|
43
|
+
if (run.up?.sleepTtl) {
|
|
44
|
+
lines.push('without a request for a while the services sleep, the next request wakes them');
|
|
45
|
+
}
|
|
46
|
+
for (const endpoint of run.up?.endpoints ?? []) {
|
|
47
|
+
lines.push(`${endpoint.service}:${endpoint.port} -> ${endpoint.url}`);
|
|
48
|
+
}
|
|
49
|
+
if ((run.up?.endpoints ?? []).length > 0) {
|
|
50
|
+
lines.push('a person opens these addresses in a browser and signs in; read a page yourself with endpoint_get');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (run.error) {
|
|
54
|
+
lines.push(`error: ${run.error}`);
|
|
55
|
+
}
|
|
56
|
+
for (const [task, result] of Object.entries(run.results ?? {})) {
|
|
57
|
+
if (result.status === 'failed') {
|
|
58
|
+
lines.push(`${task} failed${result.error ? `: ${result.error}` : ''}`, ...result.lastLog);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (run.state !== 'up') {
|
|
62
|
+
lines.push(`read the log with run_logs { "run": "${run.id}" }`);
|
|
63
|
+
}
|
|
64
|
+
return lines.join('\n');
|
|
65
|
+
}
|
|
66
|
+
exports.describeServiceRun = describeServiceRun;
|
|
67
|
+
exports.SERVICE_TOOLS = [
|
|
68
|
+
{
|
|
69
|
+
name: 'services_up',
|
|
70
|
+
title: 'Start services',
|
|
71
|
+
description: 'Start services of the published build file on the hammerkit server, with the files of the sandbox (or of ' +
|
|
72
|
+
'a branch), and keep them up for a person to look at: each endpoint (service:port) gets an address of its ' +
|
|
73
|
+
'own behind the sign-in of the server. Answers once they are up, with the addresses and until when they ' +
|
|
74
|
+
'stay. A second start with the same name replaces the first, so its addresses stay the same. A server on ' +
|
|
75
|
+
'Kubernetes starts services from a branch only: push the sandbox and pass branch.',
|
|
76
|
+
parameters: {
|
|
77
|
+
services: { type: 'string[]', description: 'The services to start, as the build file names them' },
|
|
78
|
+
endpoints: { type: 'string[]', description: 'The ports to publish, as service:port, such as web:8080' },
|
|
79
|
+
ttl: {
|
|
80
|
+
type: 'string',
|
|
81
|
+
description: 'How long they stay once ready, such as 2h, then they are removed; the server default when left out',
|
|
82
|
+
},
|
|
83
|
+
sleepTtl: {
|
|
84
|
+
type: 'string',
|
|
85
|
+
description: 'How long they stay up without a request, such as 30m, then they sleep until the next request wakes ' +
|
|
86
|
+
'them (from a branch only); the server default when left out',
|
|
87
|
+
},
|
|
88
|
+
name: {
|
|
89
|
+
type: 'string',
|
|
90
|
+
description: 'Lower case letters, digits and -; the addresses carry it. The branch when left out',
|
|
91
|
+
},
|
|
92
|
+
branch: {
|
|
93
|
+
type: 'string',
|
|
94
|
+
description: 'Start from this pushed branch of the repository instead of the files of the sandbox',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
required: ['services'],
|
|
98
|
+
async call(client, args, signal) {
|
|
99
|
+
const followed = await client.up({
|
|
100
|
+
services: args.services,
|
|
101
|
+
endpoints: (args.endpoints ?? []).map(parseEndpoint),
|
|
102
|
+
ttl: args.ttl || undefined,
|
|
103
|
+
sleepTtl: args.sleepTtl || undefined,
|
|
104
|
+
name: args.name || undefined,
|
|
105
|
+
branch: args.branch || undefined,
|
|
106
|
+
}, { signal });
|
|
107
|
+
return describeServiceRun(followed.run);
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: 'endpoint_get',
|
|
112
|
+
title: 'Read a page of an endpoint',
|
|
113
|
+
description: 'GET a path of an endpoint of a service run started with services_up, signed in as you, and answer its ' +
|
|
114
|
+
'status and body: to check that the app answers before a person opens it.',
|
|
115
|
+
parameters: {
|
|
116
|
+
run: { type: 'string', description: 'The id of the service run' },
|
|
117
|
+
path: { type: 'string', description: 'The path, such as / or /api/health' },
|
|
118
|
+
service: { type: 'string', description: 'The service of the endpoint, the first endpoint when left out' },
|
|
119
|
+
},
|
|
120
|
+
required: ['run', 'path'],
|
|
121
|
+
async call(client, args, signal) {
|
|
122
|
+
const page = await client.fetchEndpoint(args.run, args.path, args.service || undefined, signal);
|
|
123
|
+
const body = page.body.length > MAX_PAGE_CHARS
|
|
124
|
+
? `${page.body.slice(0, MAX_PAGE_CHARS)}\n… (${page.body.length} characters, the first ${MAX_PAGE_CHARS} shown)`
|
|
125
|
+
: page.body;
|
|
126
|
+
return [`GET ${page.url}: ${page.status}${page.type ? ` (${page.type})` : ''}`, '', body].join('\n');
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: 'services_stop',
|
|
131
|
+
title: 'Stop services',
|
|
132
|
+
description: 'Stop a service run started with services_up: its services and endpoints go.',
|
|
133
|
+
parameters: { run: { type: 'string', description: 'The id of the service run' } },
|
|
134
|
+
required: ['run'],
|
|
135
|
+
async call(client, args) {
|
|
136
|
+
const run = await client.stop(args.run);
|
|
137
|
+
return `run ${run.id}: ${run.state === 'up' ? 'stopping' : run.state}`;
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
];
|
|
141
|
+
//# sourceMappingURL=service-tools.js.map
|