pi-deltabox 0.1.4 → 0.1.6
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 +14 -3
- package/dist/client.js +172 -11
- package/dist/commands.js +9 -7
- package/dist/extension.js +33 -10
- package/dist/tools.js +14 -10
- 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
|
|
|
@@ -186,6 +190,13 @@ use the resident executor's fixed 600-second deadline.
|
|
|
186
190
|
private modes, publication happens at commit.
|
|
187
191
|
5. Set `"transport": "serve"` or `"cli"` only when native loading is not
|
|
188
192
|
available. These transports preserve the durable on-disk protocol.
|
|
193
|
+
6. If Pi is killed or crashes without stopping its session, the next start
|
|
194
|
+
**recovers** it instead of starting fresh: the session keeps its pending
|
|
195
|
+
changes and its open review, and a notice tells you to settle them with
|
|
196
|
+
`/sandbox-review`, then `/sandbox-commit` or `/sandbox-abort`. Nothing is
|
|
197
|
+
committed or reverted on your behalf. This needs a deltabox-lite addon with
|
|
198
|
+
session ownership (native transport). If a session start fails, the next
|
|
199
|
+
turn, tool call, or command retries it.
|
|
189
200
|
|
|
190
201
|
## Performance benchmark
|
|
191
202
|
|
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;
|
|
@@ -75,6 +79,31 @@ export function sessionNotices(info) {
|
|
|
75
79
|
}
|
|
76
80
|
return notices;
|
|
77
81
|
}
|
|
82
|
+
export function recoveryNotices(recovery, project) {
|
|
83
|
+
if (!recovery)
|
|
84
|
+
return [];
|
|
85
|
+
const notices = [];
|
|
86
|
+
const next = recovery.pendingChanges === 0
|
|
87
|
+
? "It has no pending changes."
|
|
88
|
+
: recovery.reviewActive
|
|
89
|
+
? `It has ${recovery.pendingChanges} pending change(s); use /sandbox-review, then /sandbox-commit or /sandbox-abort.`
|
|
90
|
+
: `It has ${recovery.pendingChanges} pending change(s); use /sandbox-diff to inspect them.`;
|
|
91
|
+
notices.push({
|
|
92
|
+
code: "recovered",
|
|
93
|
+
level: "warning",
|
|
94
|
+
message: `Deltabox recovered session ${recovery.sessionId}, left running by a previous run that ended without stopping it. ${next}`,
|
|
95
|
+
});
|
|
96
|
+
if (recovery.otherOrphans.length > 0) {
|
|
97
|
+
notices.push({
|
|
98
|
+
code: "orphans",
|
|
99
|
+
level: "warning",
|
|
100
|
+
message: `${recovery.otherOrphans.length} other orphaned deltabox session(s) remain: ` +
|
|
101
|
+
`${recovery.otherOrphans.join(", ")}. Stop one with ` +
|
|
102
|
+
`\`deltabox session stop <id> --project ${project}\`.`,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return notices;
|
|
106
|
+
}
|
|
78
107
|
export class DeltaboxClient {
|
|
79
108
|
project;
|
|
80
109
|
proc = null;
|
|
@@ -83,6 +112,8 @@ export class DeltaboxClient {
|
|
|
83
112
|
nextId = 1;
|
|
84
113
|
sessionId = null;
|
|
85
114
|
sessionInfo = null;
|
|
115
|
+
starting = null;
|
|
116
|
+
recovery = null;
|
|
86
117
|
native = null;
|
|
87
118
|
bin;
|
|
88
119
|
transport;
|
|
@@ -156,6 +187,7 @@ export class DeltaboxClient {
|
|
|
156
187
|
rejectPending(message) {
|
|
157
188
|
for (const pending of this.pending.values()) {
|
|
158
189
|
clearTimeout(pending.timer);
|
|
190
|
+
pending.detach?.();
|
|
159
191
|
pending.reject(new Error(message));
|
|
160
192
|
}
|
|
161
193
|
this.pending.clear();
|
|
@@ -176,6 +208,7 @@ export class DeltaboxClient {
|
|
|
176
208
|
return;
|
|
177
209
|
this.pending.delete(id);
|
|
178
210
|
clearTimeout(pending.timer);
|
|
211
|
+
pending.detach?.();
|
|
179
212
|
if (message.error) {
|
|
180
213
|
const err = message.error;
|
|
181
214
|
pending.reject(new Error(err.message ?? "deltabox error"));
|
|
@@ -184,25 +217,42 @@ export class DeltaboxClient {
|
|
|
184
217
|
pending.resolve(message.result);
|
|
185
218
|
}
|
|
186
219
|
}
|
|
187
|
-
async request(method, params, timeoutMs = this.timeoutMs) {
|
|
220
|
+
async request(method, params, timeoutMs = this.timeoutMs, signal) {
|
|
188
221
|
if (this.transport !== "serve") {
|
|
189
222
|
throw new Error("JSON-RPC request requires serve transport");
|
|
190
223
|
}
|
|
224
|
+
if (signal?.aborted)
|
|
225
|
+
throw abortReason(signal);
|
|
191
226
|
if (!this.proc)
|
|
192
227
|
this.startServe();
|
|
193
228
|
const id = this.nextId++;
|
|
194
229
|
const body = JSON.stringify({ id, method, params });
|
|
195
230
|
return new Promise((resolve, reject) => {
|
|
196
|
-
const timer = setTimeout(() => {
|
|
231
|
+
const timer = timeoutMs === null ? undefined : setTimeout(() => {
|
|
197
232
|
this.pending.delete(id);
|
|
198
233
|
this.disposeServe("deltabox serve restarted after request timeout");
|
|
199
234
|
reject(new Error(`deltabox request timed out after ${timeoutMs}ms`));
|
|
200
235
|
}, timeoutMs);
|
|
201
|
-
|
|
236
|
+
// Aborting restarts the server: the serve protocol has no way to cancel
|
|
237
|
+
// one request, and a command that is still running holds the session.
|
|
238
|
+
const onAbort = () => {
|
|
239
|
+
this.pending.delete(id);
|
|
240
|
+
this.disposeServe("deltabox serve restarted after the request was aborted");
|
|
241
|
+
reject(abortReason(signal));
|
|
242
|
+
};
|
|
243
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
244
|
+
this.pending.set(id, {
|
|
245
|
+
resolve,
|
|
246
|
+
reject,
|
|
247
|
+
timer,
|
|
248
|
+
detach: () => signal?.removeEventListener("abort", onAbort),
|
|
249
|
+
});
|
|
202
250
|
this.proc.stdin.write(body + "\n");
|
|
203
251
|
});
|
|
204
252
|
}
|
|
205
|
-
async cli(args, input, timeoutMs = this.timeoutMs) {
|
|
253
|
+
async cli(args, input, timeoutMs = this.timeoutMs, signal) {
|
|
254
|
+
if (signal?.aborted)
|
|
255
|
+
throw abortReason(signal);
|
|
206
256
|
return new Promise((resolvePromise, reject) => {
|
|
207
257
|
const child = spawn(this.bin, args, {
|
|
208
258
|
cwd: this.project,
|
|
@@ -218,6 +268,7 @@ export class DeltaboxClient {
|
|
|
218
268
|
settled = true;
|
|
219
269
|
if (timer)
|
|
220
270
|
clearTimeout(timer);
|
|
271
|
+
signal?.removeEventListener("abort", onAbort);
|
|
221
272
|
if (error)
|
|
222
273
|
reject(error);
|
|
223
274
|
else
|
|
@@ -234,10 +285,18 @@ export class DeltaboxClient {
|
|
|
234
285
|
finish(new Error(detail || `deltabox exited with ${signal ?? code}`));
|
|
235
286
|
}
|
|
236
287
|
});
|
|
237
|
-
|
|
288
|
+
// Killing the CLI also ends the command it started.
|
|
289
|
+
const onAbort = () => {
|
|
238
290
|
child.kill("SIGKILL");
|
|
239
|
-
finish(
|
|
240
|
-
}
|
|
291
|
+
finish(abortReason(signal));
|
|
292
|
+
};
|
|
293
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
294
|
+
if (timeoutMs !== null) {
|
|
295
|
+
timer = setTimeout(() => {
|
|
296
|
+
child.kill("SIGKILL");
|
|
297
|
+
finish(new Error(`deltabox command timed out after ${timeoutMs}ms`));
|
|
298
|
+
}, timeoutMs);
|
|
299
|
+
}
|
|
241
300
|
child.stdin.end(input);
|
|
242
301
|
});
|
|
243
302
|
}
|
|
@@ -274,14 +333,74 @@ export class DeltaboxClient {
|
|
|
274
333
|
parse(output) {
|
|
275
334
|
return JSON.parse(output.stdout.toString("utf8"));
|
|
276
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* Starts the session, or returns it if already started. Concurrent callers
|
|
338
|
+
* share one attempt, and a failed attempt is not remembered: the next call
|
|
339
|
+
* tries again instead of repeating a stale error.
|
|
340
|
+
*/
|
|
277
341
|
async start() {
|
|
278
342
|
if (this.sessionId && this.sessionInfo)
|
|
279
343
|
return this.sessionInfo;
|
|
344
|
+
this.starting ??= this.startOnce().finally(() => {
|
|
345
|
+
this.starting = null;
|
|
346
|
+
});
|
|
347
|
+
return this.starting;
|
|
348
|
+
}
|
|
349
|
+
/** Notices about a session `start` adopted instead of starting fresh. */
|
|
350
|
+
recoveryNotices() {
|
|
351
|
+
return recoveryNotices(this.recovery, this.project);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Adopts the newest orphan matching this client's filesystem configuration,
|
|
355
|
+
* so a restarted host picks up where a killed one stopped. Returns null
|
|
356
|
+
* when there is none, or when the addon predates session ownership.
|
|
357
|
+
*/
|
|
358
|
+
async adoptOrphan(native) {
|
|
359
|
+
if (!native.orphanedSessions || !native.adoptSession)
|
|
360
|
+
return null;
|
|
361
|
+
const durability = this.filesystem.durability === "strict" ? "strict" : "process-crash";
|
|
362
|
+
const orphans = await native.orphanedSessions();
|
|
363
|
+
const candidates = [];
|
|
364
|
+
for (const id of orphans) {
|
|
365
|
+
try {
|
|
366
|
+
const info = await native.sessionStatus(id);
|
|
367
|
+
if (info.requested_fs_mode === this.filesystem.mode && info.durability === durability) {
|
|
368
|
+
candidates.push(info);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
catch {
|
|
372
|
+
// Unreadable orphans stay for the user to stop by hand.
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
candidates.sort((a, b) => b.started_at_ms - a.started_at_ms);
|
|
376
|
+
for (const candidate of candidates) {
|
|
377
|
+
let info;
|
|
378
|
+
try {
|
|
379
|
+
info = await native.adoptSession(candidate.session_id);
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
continue; // another host adopted it first
|
|
383
|
+
}
|
|
384
|
+
// A run killed mid-turn leaves its review open; keeping it lets the
|
|
385
|
+
// user settle the recovered work with the usual review commands.
|
|
386
|
+
const review = await native.reviewStatus(info.session_id).catch(() => null);
|
|
387
|
+
this.recovery = {
|
|
388
|
+
sessionId: info.session_id,
|
|
389
|
+
pendingChanges: info.pending_changes,
|
|
390
|
+
reviewActive: review !== null,
|
|
391
|
+
otherOrphans: orphans.filter((id) => id !== info.session_id),
|
|
392
|
+
};
|
|
393
|
+
return info;
|
|
394
|
+
}
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
async startOnce() {
|
|
280
398
|
const mode = this.filesystem.mode === "auto" ? null : this.filesystem.mode;
|
|
281
399
|
const policy = this.policy ? resolve(this.project, this.policy) : null;
|
|
282
400
|
let info;
|
|
283
401
|
if (this.transport === "native") {
|
|
284
|
-
|
|
402
|
+
const native = this.nativeRequired();
|
|
403
|
+
info = (await this.adoptOrphan(native)) ?? await native.startSession(policy ?? undefined, this.filesystem.durability, this.filesystem.groupCommitMs ?? undefined);
|
|
285
404
|
}
|
|
286
405
|
else if (this.transport === "serve") {
|
|
287
406
|
const info = (await this.request("session.start", {
|
|
@@ -335,19 +454,31 @@ export class DeltaboxClient {
|
|
|
335
454
|
this.dispose();
|
|
336
455
|
}
|
|
337
456
|
}
|
|
457
|
+
/**
|
|
458
|
+
* Run a command. Commands have no default time limit: they end when they
|
|
459
|
+
* exit, when `timeoutMs` elapses, or when `signal` aborts. Every transport
|
|
460
|
+
* kills the command's process tree on timeout or abort; the native transport
|
|
461
|
+
* needs an addon that provides `cancelExec`, and with an older one the
|
|
462
|
+
* command simply runs to completion.
|
|
463
|
+
*/
|
|
338
464
|
async exec(argv, opts) {
|
|
339
465
|
if (this.transport === "native") {
|
|
466
|
+
const native = this.nativeRequired();
|
|
340
467
|
const cwd = opts?.cwd ? this.projectPath(opts.cwd) : this.project;
|
|
341
468
|
const environment = opts?.env
|
|
342
469
|
? Object.entries(opts.env).map(([key, value]) => ({ key, value }))
|
|
343
470
|
: undefined;
|
|
344
|
-
|
|
471
|
+
const wantsCancel = opts?.timeoutMs !== undefined || opts?.signal !== undefined;
|
|
472
|
+
if (!wantsCancel || native.cancelExec === undefined) {
|
|
473
|
+
return native.exec(this.id(), argv, cwd, environment, opts?.label);
|
|
474
|
+
}
|
|
475
|
+
return this.execCancellable(native, argv, cwd, environment, opts);
|
|
345
476
|
}
|
|
346
477
|
if (this.transport === "serve") {
|
|
347
478
|
return (await this.request("session.exec", {
|
|
348
479
|
session_id: this.id(), argv, cwd: opts?.cwd ? this.projectPath(opts.cwd) : null,
|
|
349
480
|
env: opts?.env ? Object.entries(opts.env) : null, label: opts?.label ?? null,
|
|
350
|
-
}, opts?.timeoutMs ??
|
|
481
|
+
}, opts?.timeoutMs ?? null, opts?.signal));
|
|
351
482
|
}
|
|
352
483
|
const args = ["exec", this.id(), ...this.projectArgs()];
|
|
353
484
|
if (opts?.label)
|
|
@@ -357,7 +488,37 @@ export class DeltaboxClient {
|
|
|
357
488
|
for (const [key, value] of Object.entries(opts?.env ?? {}))
|
|
358
489
|
args.push("--env", `${key}=${value}`);
|
|
359
490
|
args.push("--", ...argv);
|
|
360
|
-
return this.parse(await this.cli(args, undefined, opts?.timeoutMs ??
|
|
491
|
+
return this.parse(await this.cli(args, undefined, opts?.timeoutMs ?? null, opts?.signal));
|
|
492
|
+
}
|
|
493
|
+
async execCancellable(native, argv, cwd, environment, opts) {
|
|
494
|
+
const { signal, timeoutMs } = opts;
|
|
495
|
+
if (signal?.aborted)
|
|
496
|
+
throw abortReason(signal);
|
|
497
|
+
const execId = randomUUID();
|
|
498
|
+
let reason;
|
|
499
|
+
const cancel = (error) => {
|
|
500
|
+
reason ??= error;
|
|
501
|
+
native.cancelExec(execId);
|
|
502
|
+
};
|
|
503
|
+
const timer = timeoutMs === undefined ? undefined : setTimeout(() => cancel(new Error(`deltabox command timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
504
|
+
const onAbort = () => cancel(abortReason(signal));
|
|
505
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
506
|
+
try {
|
|
507
|
+
const result = await native.exec(this.id(), argv, cwd, environment, opts.label, undefined, undefined, execId);
|
|
508
|
+
// An unsandboxed command killed by a cancel resolves with a non-zero
|
|
509
|
+
// result instead of rejecting; report why it stopped.
|
|
510
|
+
if (reason)
|
|
511
|
+
throw reason;
|
|
512
|
+
return result;
|
|
513
|
+
}
|
|
514
|
+
catch (error) {
|
|
515
|
+
throw reason ?? error;
|
|
516
|
+
}
|
|
517
|
+
finally {
|
|
518
|
+
if (timer)
|
|
519
|
+
clearTimeout(timer);
|
|
520
|
+
signal?.removeEventListener("abort", onAbort);
|
|
521
|
+
}
|
|
361
522
|
}
|
|
362
523
|
async readFile(path) {
|
|
363
524
|
path = this.projectPath(path);
|
package/dist/commands.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { renderDiff } from "./format.js";
|
|
2
|
-
export function registerCommands(pi,
|
|
2
|
+
export function registerCommands(pi,
|
|
3
|
+
/** Resolves the started client; `ctx` receives any start notices. */
|
|
4
|
+
getClient, getConfig, setMode) {
|
|
3
5
|
pi.registerCommand("sandbox-status", {
|
|
4
6
|
description: "Show deltabox-lite session status and pending change count",
|
|
5
7
|
handler: async (_args, ctx) => {
|
|
6
|
-
const client = getClient();
|
|
8
|
+
const client = await getClient(ctx);
|
|
7
9
|
try {
|
|
8
10
|
const status = await client.status();
|
|
9
11
|
ctx.ui.notify([
|
|
@@ -27,7 +29,7 @@ export function registerCommands(pi, getClient, getConfig, setMode) {
|
|
|
27
29
|
pi.registerCommand("sandbox-review", {
|
|
28
30
|
description: "Show the pending diff",
|
|
29
31
|
handler: async (_args, ctx) => {
|
|
30
|
-
const client = getClient();
|
|
32
|
+
const client = await getClient(ctx);
|
|
31
33
|
try {
|
|
32
34
|
const diff = await client.reviewDiff();
|
|
33
35
|
const text = renderDiff(diff);
|
|
@@ -41,7 +43,7 @@ export function registerCommands(pi, getClient, getConfig, setMode) {
|
|
|
41
43
|
pi.registerCommand("sandbox-commit", {
|
|
42
44
|
description: "Commit pending changes to the real project",
|
|
43
45
|
handler: async (_args, ctx) => {
|
|
44
|
-
const client = getClient();
|
|
46
|
+
const client = await getClient(ctx);
|
|
45
47
|
try {
|
|
46
48
|
const result = await client.reviewCommit();
|
|
47
49
|
ctx.ui.notify(`Committed ${result.applied} change(s) (${result.checkpoint_id.slice(0, 8)})`, "info");
|
|
@@ -54,7 +56,7 @@ export function registerCommands(pi, getClient, getConfig, setMode) {
|
|
|
54
56
|
pi.registerCommand("sandbox-abort", {
|
|
55
57
|
description: "Abort pending changes back to the head checkpoint",
|
|
56
58
|
handler: async (_args, ctx) => {
|
|
57
|
-
const client = getClient();
|
|
59
|
+
const client = await getClient(ctx);
|
|
58
60
|
try {
|
|
59
61
|
const result = await client.reviewAbort();
|
|
60
62
|
ctx.ui.notify(`Aborted, restored ${result.restored} change(s)`, "info");
|
|
@@ -68,7 +70,7 @@ export function registerCommands(pi, getClient, getConfig, setMode) {
|
|
|
68
70
|
description: "Keep pending changes and preserve the current rollback point",
|
|
69
71
|
handler: async (_args, ctx) => {
|
|
70
72
|
try {
|
|
71
|
-
const active = await getClient().reviewSkip();
|
|
73
|
+
const active = await (await getClient(ctx)).reviewSkip();
|
|
72
74
|
ctx.ui.notify(`Kept changes pending from ${active.checkpoint_id.slice(0, 8)}`, "info");
|
|
73
75
|
}
|
|
74
76
|
catch (error) {
|
|
@@ -79,7 +81,7 @@ export function registerCommands(pi, getClient, getConfig, setMode) {
|
|
|
79
81
|
pi.registerCommand("sandbox-diff", {
|
|
80
82
|
description: "Show the diff between two checkpoints (or base and head)",
|
|
81
83
|
handler: async (args, ctx) => {
|
|
82
|
-
const client = getClient();
|
|
84
|
+
const client = await getClient(ctx);
|
|
83
85
|
const parts = String(args ?? "")
|
|
84
86
|
.trim()
|
|
85
87
|
.split(/\s+/)
|
package/dist/extension.js
CHANGED
|
@@ -27,6 +27,33 @@ export function registerExtension(pi, dependencies = defaultDependencies) {
|
|
|
27
27
|
}
|
|
28
28
|
return client;
|
|
29
29
|
};
|
|
30
|
+
// The client whose start notices were shown; each client announces once.
|
|
31
|
+
let announced = null;
|
|
32
|
+
/**
|
|
33
|
+
* The started client. A failed start is not sticky: the client is dropped,
|
|
34
|
+
* and the next tool call, command, or turn builds a fresh one and retries.
|
|
35
|
+
*/
|
|
36
|
+
const ensureClient = async (ctx) => {
|
|
37
|
+
const current = getClient();
|
|
38
|
+
let info;
|
|
39
|
+
try {
|
|
40
|
+
info = await current.start();
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (client === current) {
|
|
44
|
+
current.dispose();
|
|
45
|
+
client = null;
|
|
46
|
+
}
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
if (ctx?.ui && announced !== current) {
|
|
50
|
+
announced = current;
|
|
51
|
+
for (const notice of [...current.recoveryNotices(), ...sessionNotices(info)]) {
|
|
52
|
+
ctx.ui.notify(notice.message, notice.level);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return current;
|
|
56
|
+
};
|
|
30
57
|
pi.on("tool_call", async (event) => {
|
|
31
58
|
if (process.platform === "win32" && config.isolation === "deltabox" && event.toolName === "bash") {
|
|
32
59
|
return { block: true, reason: "Git Bash is unavailable under Windows DeltaBox isolation. Use the powershell tool." };
|
|
@@ -36,14 +63,9 @@ export function registerExtension(pi, dependencies = defaultDependencies) {
|
|
|
36
63
|
config = dependencies.loadConfig(dependencies.cwd());
|
|
37
64
|
mode = config.review.mode;
|
|
38
65
|
try {
|
|
39
|
-
|
|
40
|
-
for (const notice of sessionNotices(info)) {
|
|
41
|
-
ctx.ui.notify(notice.message, notice.level);
|
|
42
|
-
}
|
|
66
|
+
await ensureClient(ctx);
|
|
43
67
|
}
|
|
44
68
|
catch (error) {
|
|
45
|
-
client?.dispose();
|
|
46
|
-
client = null;
|
|
47
69
|
ctx.ui.notify(`deltabox session start failed: ${String(error)}`, "error");
|
|
48
70
|
}
|
|
49
71
|
});
|
|
@@ -61,10 +83,11 @@ export function registerExtension(pi, dependencies = defaultDependencies) {
|
|
|
61
83
|
}
|
|
62
84
|
});
|
|
63
85
|
pi.on("before_agent_start", async (_event, ctx) => {
|
|
64
|
-
if (
|
|
86
|
+
if (mode !== "task")
|
|
65
87
|
return;
|
|
66
88
|
try {
|
|
67
|
-
|
|
89
|
+
// Also the retry point after a failed session start.
|
|
90
|
+
await (await ensureClient(ctx)).reviewBegin("task", "agent-turn");
|
|
68
91
|
}
|
|
69
92
|
catch (error) {
|
|
70
93
|
ctx.ui.notify(`deltabox review start failed: ${String(error)}`, "error");
|
|
@@ -87,10 +110,10 @@ export function registerExtension(pi, dependencies = defaultDependencies) {
|
|
|
87
110
|
ctx.ui.notify(`deltabox review failed: ${String(error)}`, "error");
|
|
88
111
|
}
|
|
89
112
|
});
|
|
90
|
-
for (const tool of makeTools(
|
|
113
|
+
for (const tool of makeTools(ensureClient, getConfig)) {
|
|
91
114
|
pi.registerTool(tool);
|
|
92
115
|
}
|
|
93
|
-
registerCommands(pi,
|
|
116
|
+
registerCommands(pi, ensureClient, getConfig, (next) => {
|
|
94
117
|
mode = next;
|
|
95
118
|
});
|
|
96
119
|
}
|
package/dist/tools.js
CHANGED
|
@@ -77,7 +77,9 @@ async function afterMutation(client, config, ctx, source) {
|
|
|
77
77
|
await reviewPending(client, (ctx.ui ?? {}), source);
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
|
-
export function makeTools(
|
|
80
|
+
export function makeTools(
|
|
81
|
+
/** Resolves the started client; `ctx` receives any start notices. */
|
|
82
|
+
getClient, getConfig) {
|
|
81
83
|
const readSchema = Type.Object({
|
|
82
84
|
path: Type.String({ description: "Path to the file to read" }),
|
|
83
85
|
offset: Type.Optional(Type.Number({ description: "1-indexed first line" })),
|
|
@@ -106,7 +108,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
106
108
|
parameters: readSchema,
|
|
107
109
|
async execute(_id, params, _signal, _onUpdate, ctx) {
|
|
108
110
|
const p = params;
|
|
109
|
-
const client = getClient();
|
|
111
|
+
const client = await getClient(ctx);
|
|
110
112
|
const absolute = toAbsolute(ctx, p.path);
|
|
111
113
|
const bytes = await client.readFile(absolute);
|
|
112
114
|
const text = bytes.toString("utf8");
|
|
@@ -125,7 +127,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
125
127
|
parameters: pathSchema,
|
|
126
128
|
async execute(_id, params, _signal, _onUpdate, ctx) {
|
|
127
129
|
const p = params;
|
|
128
|
-
const client = getClient();
|
|
130
|
+
const client = await getClient(ctx);
|
|
129
131
|
await beforeMutation(client, getConfig());
|
|
130
132
|
await client.writeFile(toAbsolute(ctx, p.path), p.content);
|
|
131
133
|
await afterMutation(client, getConfig(), ctx, "write");
|
|
@@ -139,7 +141,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
139
141
|
parameters: editSchema,
|
|
140
142
|
async execute(_id, params, _signal, _onUpdate, ctx) {
|
|
141
143
|
const p = params;
|
|
142
|
-
const client = getClient();
|
|
144
|
+
const client = await getClient(ctx);
|
|
143
145
|
const absolute = toAbsolute(ctx, p.path);
|
|
144
146
|
await beforeMutation(client, getConfig());
|
|
145
147
|
const content = (await client.readFile(absolute)).toString("utf8");
|
|
@@ -159,9 +161,9 @@ export function makeTools(getClient, getConfig) {
|
|
|
159
161
|
label: "bash (deltabox)",
|
|
160
162
|
description: "Run a shell command inside the deltabox-lite sandbox; filesystem changes are tracked and reviewable.",
|
|
161
163
|
parameters: bashSchema,
|
|
162
|
-
async execute(_id, params,
|
|
164
|
+
async execute(_id, params, signal, _onUpdate, ctx) {
|
|
163
165
|
const p = params;
|
|
164
|
-
const client = getClient();
|
|
166
|
+
const client = await getClient(ctx);
|
|
165
167
|
await beforeMutation(client, getConfig());
|
|
166
168
|
const timeoutMs = p.timeout !== undefined && p.timeout > 0
|
|
167
169
|
? p.timeout * 1_000
|
|
@@ -171,6 +173,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
171
173
|
result = await client.exec([bashExecutable(), "-c", p.command], {
|
|
172
174
|
label: "bash",
|
|
173
175
|
timeoutMs,
|
|
176
|
+
signal,
|
|
174
177
|
});
|
|
175
178
|
}
|
|
176
179
|
catch (error) {
|
|
@@ -191,7 +194,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
191
194
|
}),
|
|
192
195
|
async execute(_id, params, _signal, _onUpdate, ctx) {
|
|
193
196
|
const p = params;
|
|
194
|
-
const client = getClient();
|
|
197
|
+
const client = await getClient(ctx);
|
|
195
198
|
const target = toAbsolute(ctx, p.path ?? ".");
|
|
196
199
|
const result = await client.exec(process.platform === "win32" ? powershellArgv(`Get-ChildItem -LiteralPath ${powershellQuote(powershellTarget(ctx.cwd, target))} -File -Recurse | Select-String -Pattern ${powershellQuote(p.pattern)} | ForEach-Object { '{0}:{1}:{2}' -f $_.Path, $_.LineNumber, $_.Line }`) : [
|
|
197
200
|
bashExecutable(),
|
|
@@ -215,7 +218,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
215
218
|
}),
|
|
216
219
|
async execute(_id, params, _signal, _onUpdate, ctx) {
|
|
217
220
|
const p = params;
|
|
218
|
-
const client = getClient();
|
|
221
|
+
const client = await getClient(ctx);
|
|
219
222
|
const target = toAbsolute(ctx, p.path ?? ".");
|
|
220
223
|
const pattern = p.pattern ?? "*";
|
|
221
224
|
const result = await client.exec(process.platform === "win32" ? powershellArgv(`Get-ChildItem -LiteralPath ${powershellQuote(powershellTarget(ctx.cwd, target))} -Recurse -Force | Where-Object { $_.Name -like ${powershellQuote(pattern)} } | ForEach-Object { $_.FullName }`) : [
|
|
@@ -239,7 +242,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
239
242
|
}),
|
|
240
243
|
async execute(_id, params, _signal, _onUpdate, ctx) {
|
|
241
244
|
const p = params;
|
|
242
|
-
const client = getClient();
|
|
245
|
+
const client = await getClient(ctx);
|
|
243
246
|
const target = toAbsolute(ctx, p.path ?? ".");
|
|
244
247
|
const result = await client.exec(process.platform === "win32"
|
|
245
248
|
? powershellArgv(`Get-ChildItem -LiteralPath ${powershellQuote(powershellTarget(ctx.cwd, target))} -Force | Format-Table Mode, Length, Name -AutoSize | Out-String -Width 240`)
|
|
@@ -262,7 +265,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
262
265
|
if (signal?.aborted)
|
|
263
266
|
throw new Error("PowerShell command cancelled before execution");
|
|
264
267
|
const p = params;
|
|
265
|
-
const client = getClient();
|
|
268
|
+
const client = await getClient(ctx);
|
|
266
269
|
const config = getConfig();
|
|
267
270
|
const argv = powershellArgv(p.command);
|
|
268
271
|
await beforeMutation(client, config);
|
|
@@ -270,6 +273,7 @@ export function makeTools(getClient, getConfig) {
|
|
|
270
273
|
return execResult(normalizePowerShellResult(await client.exec(argv, {
|
|
271
274
|
label: "powershell",
|
|
272
275
|
timeoutMs: p.timeout !== undefined && p.timeout > 0 ? p.timeout * 1_000 : undefined,
|
|
276
|
+
signal,
|
|
273
277
|
})));
|
|
274
278
|
}
|
|
275
279
|
finally {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-deltabox",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
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.3.0",
|
|
32
|
+
"deltabox-lite-darwin-x64": "0.3.0",
|
|
33
|
+
"deltabox-lite-linux-arm64": "0.3.0",
|
|
34
|
+
"deltabox-lite-linux-x64": "0.3.0",
|
|
35
|
+
"deltabox-lite-windows-x64": "0.3.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@earendil-works/pi-coding-agent": "^0.79.3",
|