pi-deltabox 0.1.4 → 0.1.5
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/README.md +7 -3
- package/dist/client.js +84 -10
- package/dist/tools.js +3 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -155,9 +155,13 @@ notifications.
|
|
|
155
155
|
| `tool` | Prompt after every mutating tool call |
|
|
156
156
|
| `risky` | Prompt after `bash`, `write`, and `edit` |
|
|
157
157
|
|
|
158
|
-
`timeoutMs` bounds CLI and JSON-RPC requests
|
|
159
|
-
|
|
160
|
-
|
|
158
|
+
`timeoutMs` bounds CLI and JSON-RPC requests other than commands. Commands have
|
|
159
|
+
no default time limit: they run until they exit, until the `timeout` supplied to
|
|
160
|
+
Pi's bash tool elapses, or until Pi aborts the tool call, and then the command's
|
|
161
|
+
process tree is killed. On the native transport this needs a `deltabox-lite`
|
|
162
|
+
addon that provides `cancelExec`; with an older addon the command is not
|
|
163
|
+
interrupted. Descendants that start their own session or process group are not
|
|
164
|
+
reached.
|
|
161
165
|
|
|
162
166
|
## Commands
|
|
163
167
|
|
package/dist/client.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import { realpathSync } from "node:fs";
|
|
3
4
|
import { mkdir, readFile as readFileBytes, writeFile as writeFileBytes, } from "node:fs/promises";
|
|
4
5
|
import { createRequire } from "node:module";
|
|
@@ -41,6 +42,9 @@ function loadNativeBinding(project, nativeAddon) {
|
|
|
41
42
|
}
|
|
42
43
|
return require(path);
|
|
43
44
|
}
|
|
45
|
+
function abortReason(signal) {
|
|
46
|
+
return signal.reason instanceof Error ? signal.reason : new Error("deltabox command aborted");
|
|
47
|
+
}
|
|
44
48
|
export function resolveDeltaboxBin(project, bin) {
|
|
45
49
|
if (isAbsolute(bin) || !bin.includes("/"))
|
|
46
50
|
return bin;
|
|
@@ -156,6 +160,7 @@ export class DeltaboxClient {
|
|
|
156
160
|
rejectPending(message) {
|
|
157
161
|
for (const pending of this.pending.values()) {
|
|
158
162
|
clearTimeout(pending.timer);
|
|
163
|
+
pending.detach?.();
|
|
159
164
|
pending.reject(new Error(message));
|
|
160
165
|
}
|
|
161
166
|
this.pending.clear();
|
|
@@ -176,6 +181,7 @@ export class DeltaboxClient {
|
|
|
176
181
|
return;
|
|
177
182
|
this.pending.delete(id);
|
|
178
183
|
clearTimeout(pending.timer);
|
|
184
|
+
pending.detach?.();
|
|
179
185
|
if (message.error) {
|
|
180
186
|
const err = message.error;
|
|
181
187
|
pending.reject(new Error(err.message ?? "deltabox error"));
|
|
@@ -184,25 +190,42 @@ export class DeltaboxClient {
|
|
|
184
190
|
pending.resolve(message.result);
|
|
185
191
|
}
|
|
186
192
|
}
|
|
187
|
-
async request(method, params, timeoutMs = this.timeoutMs) {
|
|
193
|
+
async request(method, params, timeoutMs = this.timeoutMs, signal) {
|
|
188
194
|
if (this.transport !== "serve") {
|
|
189
195
|
throw new Error("JSON-RPC request requires serve transport");
|
|
190
196
|
}
|
|
197
|
+
if (signal?.aborted)
|
|
198
|
+
throw abortReason(signal);
|
|
191
199
|
if (!this.proc)
|
|
192
200
|
this.startServe();
|
|
193
201
|
const id = this.nextId++;
|
|
194
202
|
const body = JSON.stringify({ id, method, params });
|
|
195
203
|
return new Promise((resolve, reject) => {
|
|
196
|
-
const timer = setTimeout(() => {
|
|
204
|
+
const timer = timeoutMs === null ? undefined : setTimeout(() => {
|
|
197
205
|
this.pending.delete(id);
|
|
198
206
|
this.disposeServe("deltabox serve restarted after request timeout");
|
|
199
207
|
reject(new Error(`deltabox request timed out after ${timeoutMs}ms`));
|
|
200
208
|
}, timeoutMs);
|
|
201
|
-
|
|
209
|
+
// Aborting restarts the server: the serve protocol has no way to cancel
|
|
210
|
+
// one request, and a command that is still running holds the session.
|
|
211
|
+
const onAbort = () => {
|
|
212
|
+
this.pending.delete(id);
|
|
213
|
+
this.disposeServe("deltabox serve restarted after the request was aborted");
|
|
214
|
+
reject(abortReason(signal));
|
|
215
|
+
};
|
|
216
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
217
|
+
this.pending.set(id, {
|
|
218
|
+
resolve,
|
|
219
|
+
reject,
|
|
220
|
+
timer,
|
|
221
|
+
detach: () => signal?.removeEventListener("abort", onAbort),
|
|
222
|
+
});
|
|
202
223
|
this.proc.stdin.write(body + "\n");
|
|
203
224
|
});
|
|
204
225
|
}
|
|
205
|
-
async cli(args, input, timeoutMs = this.timeoutMs) {
|
|
226
|
+
async cli(args, input, timeoutMs = this.timeoutMs, signal) {
|
|
227
|
+
if (signal?.aborted)
|
|
228
|
+
throw abortReason(signal);
|
|
206
229
|
return new Promise((resolvePromise, reject) => {
|
|
207
230
|
const child = spawn(this.bin, args, {
|
|
208
231
|
cwd: this.project,
|
|
@@ -218,6 +241,7 @@ export class DeltaboxClient {
|
|
|
218
241
|
settled = true;
|
|
219
242
|
if (timer)
|
|
220
243
|
clearTimeout(timer);
|
|
244
|
+
signal?.removeEventListener("abort", onAbort);
|
|
221
245
|
if (error)
|
|
222
246
|
reject(error);
|
|
223
247
|
else
|
|
@@ -234,10 +258,18 @@ export class DeltaboxClient {
|
|
|
234
258
|
finish(new Error(detail || `deltabox exited with ${signal ?? code}`));
|
|
235
259
|
}
|
|
236
260
|
});
|
|
237
|
-
|
|
261
|
+
// Killing the CLI also ends the command it started.
|
|
262
|
+
const onAbort = () => {
|
|
238
263
|
child.kill("SIGKILL");
|
|
239
|
-
finish(
|
|
240
|
-
}
|
|
264
|
+
finish(abortReason(signal));
|
|
265
|
+
};
|
|
266
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
267
|
+
if (timeoutMs !== null) {
|
|
268
|
+
timer = setTimeout(() => {
|
|
269
|
+
child.kill("SIGKILL");
|
|
270
|
+
finish(new Error(`deltabox command timed out after ${timeoutMs}ms`));
|
|
271
|
+
}, timeoutMs);
|
|
272
|
+
}
|
|
241
273
|
child.stdin.end(input);
|
|
242
274
|
});
|
|
243
275
|
}
|
|
@@ -335,19 +367,31 @@ export class DeltaboxClient {
|
|
|
335
367
|
this.dispose();
|
|
336
368
|
}
|
|
337
369
|
}
|
|
370
|
+
/**
|
|
371
|
+
* Run a command. Commands have no default time limit: they end when they
|
|
372
|
+
* exit, when `timeoutMs` elapses, or when `signal` aborts. Every transport
|
|
373
|
+
* kills the command's process tree on timeout or abort; the native transport
|
|
374
|
+
* needs an addon that provides `cancelExec`, and with an older one the
|
|
375
|
+
* command simply runs to completion.
|
|
376
|
+
*/
|
|
338
377
|
async exec(argv, opts) {
|
|
339
378
|
if (this.transport === "native") {
|
|
379
|
+
const native = this.nativeRequired();
|
|
340
380
|
const cwd = opts?.cwd ? this.projectPath(opts.cwd) : this.project;
|
|
341
381
|
const environment = opts?.env
|
|
342
382
|
? Object.entries(opts.env).map(([key, value]) => ({ key, value }))
|
|
343
383
|
: undefined;
|
|
344
|
-
|
|
384
|
+
const wantsCancel = opts?.timeoutMs !== undefined || opts?.signal !== undefined;
|
|
385
|
+
if (!wantsCancel || native.cancelExec === undefined) {
|
|
386
|
+
return native.exec(this.id(), argv, cwd, environment, opts?.label);
|
|
387
|
+
}
|
|
388
|
+
return this.execCancellable(native, argv, cwd, environment, opts);
|
|
345
389
|
}
|
|
346
390
|
if (this.transport === "serve") {
|
|
347
391
|
return (await this.request("session.exec", {
|
|
348
392
|
session_id: this.id(), argv, cwd: opts?.cwd ? this.projectPath(opts.cwd) : null,
|
|
349
393
|
env: opts?.env ? Object.entries(opts.env) : null, label: opts?.label ?? null,
|
|
350
|
-
}, opts?.timeoutMs ??
|
|
394
|
+
}, opts?.timeoutMs ?? null, opts?.signal));
|
|
351
395
|
}
|
|
352
396
|
const args = ["exec", this.id(), ...this.projectArgs()];
|
|
353
397
|
if (opts?.label)
|
|
@@ -357,7 +401,37 @@ export class DeltaboxClient {
|
|
|
357
401
|
for (const [key, value] of Object.entries(opts?.env ?? {}))
|
|
358
402
|
args.push("--env", `${key}=${value}`);
|
|
359
403
|
args.push("--", ...argv);
|
|
360
|
-
return this.parse(await this.cli(args, undefined, opts?.timeoutMs ??
|
|
404
|
+
return this.parse(await this.cli(args, undefined, opts?.timeoutMs ?? null, opts?.signal));
|
|
405
|
+
}
|
|
406
|
+
async execCancellable(native, argv, cwd, environment, opts) {
|
|
407
|
+
const { signal, timeoutMs } = opts;
|
|
408
|
+
if (signal?.aborted)
|
|
409
|
+
throw abortReason(signal);
|
|
410
|
+
const execId = randomUUID();
|
|
411
|
+
let reason;
|
|
412
|
+
const cancel = (error) => {
|
|
413
|
+
reason ??= error;
|
|
414
|
+
native.cancelExec(execId);
|
|
415
|
+
};
|
|
416
|
+
const timer = timeoutMs === undefined ? undefined : setTimeout(() => cancel(new Error(`deltabox command timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
417
|
+
const onAbort = () => cancel(abortReason(signal));
|
|
418
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
419
|
+
try {
|
|
420
|
+
const result = await native.exec(this.id(), argv, cwd, environment, opts.label, undefined, undefined, execId);
|
|
421
|
+
// An unsandboxed command killed by a cancel resolves with a non-zero
|
|
422
|
+
// result instead of rejecting; report why it stopped.
|
|
423
|
+
if (reason)
|
|
424
|
+
throw reason;
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
catch (error) {
|
|
428
|
+
throw reason ?? error;
|
|
429
|
+
}
|
|
430
|
+
finally {
|
|
431
|
+
if (timer)
|
|
432
|
+
clearTimeout(timer);
|
|
433
|
+
signal?.removeEventListener("abort", onAbort);
|
|
434
|
+
}
|
|
361
435
|
}
|
|
362
436
|
async readFile(path) {
|
|
363
437
|
path = this.projectPath(path);
|
package/dist/tools.js
CHANGED
|
@@ -159,7 +159,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
159
159
|
label: "bash (deltabox)",
|
|
160
160
|
description: "Run a shell command inside the deltabox-lite sandbox; filesystem changes are tracked and reviewable.",
|
|
161
161
|
parameters: bashSchema,
|
|
162
|
-
async execute(_id, params,
|
|
162
|
+
async execute(_id, params, signal, _onUpdate, ctx) {
|
|
163
163
|
const p = params;
|
|
164
164
|
const client = getClient();
|
|
165
165
|
await beforeMutation(client, getConfig());
|
|
@@ -171,6 +171,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
171
171
|
result = await client.exec([bashExecutable(), "-c", p.command], {
|
|
172
172
|
label: "bash",
|
|
173
173
|
timeoutMs,
|
|
174
|
+
signal,
|
|
174
175
|
});
|
|
175
176
|
}
|
|
176
177
|
catch (error) {
|
|
@@ -270,6 +271,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
270
271
|
return execResult(normalizePowerShellResult(await client.exec(argv, {
|
|
271
272
|
label: "powershell",
|
|
272
273
|
timeoutMs: p.timeout !== undefined && p.timeout > 0 ? p.timeout * 1_000 : undefined,
|
|
274
|
+
signal,
|
|
273
275
|
})));
|
|
274
276
|
}
|
|
275
277
|
finally {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-deltabox",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Pi extension: agent-native sandbox sessions with diff/commit/abort, powered by deltabox-lite",
|
|
6
6
|
"type": "module",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"typebox": "^1.3.10"
|
|
29
29
|
},
|
|
30
30
|
"optionalDependencies": {
|
|
31
|
-
"deltabox-lite-darwin-arm64": "0.
|
|
32
|
-
"deltabox-lite-darwin-x64": "0.
|
|
33
|
-
"deltabox-lite-linux-arm64": "0.
|
|
34
|
-
"deltabox-lite-linux-x64": "0.
|
|
35
|
-
"deltabox-lite-windows-x64": "0.
|
|
31
|
+
"deltabox-lite-darwin-arm64": "0.2.0",
|
|
32
|
+
"deltabox-lite-darwin-x64": "0.2.0",
|
|
33
|
+
"deltabox-lite-linux-arm64": "0.2.0",
|
|
34
|
+
"deltabox-lite-linux-x64": "0.2.0",
|
|
35
|
+
"deltabox-lite-windows-x64": "0.2.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@earendil-works/pi-coding-agent": "^0.79.3",
|