pi-background-tasks 0.1.0

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.
@@ -0,0 +1,1305 @@
1
+ import { spawn, type ChildProcess } from "node:child_process";
2
+ import { randomBytes } from "node:crypto";
3
+ import { createWriteStream, existsSync, statSync, type WriteStream } from "node:fs";
4
+ import { mkdir, open, writeFile } from "node:fs/promises";
5
+ import { join } from "node:path";
6
+ import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext, Theme, ToolRenderResultOptions } from "@earendil-works/pi-coding-agent";
7
+ import { DEFAULT_MAX_BYTES, formatSize } from "@earendil-works/pi-coding-agent";
8
+ import { matchesKey, Text, truncateToWidth, visibleWidth, type Component, type TUI } from "@earendil-works/pi-tui";
9
+ import { Type } from "typebox";
10
+
11
+ /**
12
+ * Project-local Pi background task manager.
13
+ *
14
+ * Scope:
15
+ * - Explicit background shell jobs only: /bg and bg_run spawn commands directly.
16
+ * - No Ctrl+B support for backgrounding an already-running built-in bash tool.
17
+ * - No detached/restart reattachment: live child processes belong to this Pi
18
+ * extension runtime and are killed on session shutdown/reload.
19
+ */
20
+
21
+ type TaskStatus = "running" | "completed" | "failed" | "killed";
22
+ type KillKind = "user" | "timeout" | "output_cap" | "shutdown";
23
+
24
+ type BgTaskSnapshot = {
25
+ id: string;
26
+ command: string;
27
+ description?: string;
28
+ status: TaskStatus;
29
+ outputPath: string;
30
+ cwd: string;
31
+ startTime: number;
32
+ endTime?: number;
33
+ exitCode?: number | null;
34
+ signal?: string | null;
35
+ pid?: number;
36
+ bytesWritten: number;
37
+ error?: string;
38
+ notified: boolean;
39
+ notifyOnCompletion: boolean;
40
+ triggerOnCompletion: boolean;
41
+ timeoutSeconds?: number;
42
+ };
43
+
44
+ type BgTask = BgTaskSnapshot & {
45
+ outputAbsPath: string;
46
+ metadataAbsPath: string;
47
+ child?: ChildProcess;
48
+ stream?: WriteStream;
49
+ timeoutHandle?: NodeJS.Timeout;
50
+ killKind?: KillKind;
51
+ killSignalSent?: boolean;
52
+ capExceeded?: boolean;
53
+ finalized?: boolean;
54
+ waiters: Array<() => void>;
55
+ };
56
+
57
+ type BgRunDetails = {
58
+ task: BgTaskSnapshot;
59
+ };
60
+
61
+ type BgStatusDetails = {
62
+ tasks: BgTaskSnapshot[];
63
+ };
64
+
65
+ type BgLogsDetails = {
66
+ task: BgTaskSnapshot;
67
+ path: string;
68
+ bytesRead: number;
69
+ truncated: boolean;
70
+ tail: boolean;
71
+ };
72
+
73
+ type BgKillDetails = {
74
+ task: BgTaskSnapshot;
75
+ message: string;
76
+ };
77
+
78
+ const DEFAULT_LOG_BYTES = Math.min(DEFAULT_MAX_BYTES, 50 * 1024);
79
+ const MAX_LOG_BYTES = Math.min(DEFAULT_MAX_BYTES, 50 * 1024);
80
+ const MAX_OUTPUT_BYTES = Number(process.env.PI_BG_MAX_OUTPUT_BYTES ?? 20 * 1024 * 1024);
81
+ const KILL_GRACE_MS = 3000;
82
+ const STOP_WAIT_MS = KILL_GRACE_MS + 1500;
83
+ const MAX_RECENT_TASKS = 100;
84
+ const STATUS_INTERVAL_MS = 1000;
85
+ const COMMAND_PREVIEW_CHARS = 90;
86
+ const DETAIL_TAIL_BYTES = 8 * 1024;
87
+ const LIST_VISIBLE_ROWS = 14;
88
+ const DETAIL_VISIBLE_OUTPUT_LINES = 12;
89
+ const LIGHT_BLUE_BG = "\x1b[48;2;183;223;255m";
90
+ const LIGHT_BLUE_FG = "\x1b[38;2;11;70;110m";
91
+ const LIGHT_BLUE_BORDER = "\x1b[38;2;83;160;215m";
92
+ const ANSI_RESET = "\x1b[0m";
93
+
94
+ function sanitizePathSegment(value: string): string {
95
+ const sanitized = value.replace(/[^a-zA-Z0-9_.-]+/g, "-").replace(/^-+|-+$/g, "");
96
+ return sanitized || "session";
97
+ }
98
+
99
+ function stripMatchingQuotes(value: string): string {
100
+ const trimmed = value.trim();
101
+ if (trimmed.length >= 2) {
102
+ const first = trimmed[0];
103
+ const last = trimmed[trimmed.length - 1];
104
+ if ((first === '"' && last === '"') || (first === "'" && last === "'")) {
105
+ return trimmed.slice(1, -1);
106
+ }
107
+ }
108
+ return trimmed;
109
+ }
110
+
111
+ function truncateChars(value: string, maxChars: number): string {
112
+ if (value.length <= maxChars) return value;
113
+ return `${value.slice(0, Math.max(0, maxChars - 1))}…`;
114
+ }
115
+
116
+ function formatDuration(ms: number): string {
117
+ if (ms < 1000) return `${ms}ms`;
118
+ const seconds = Math.floor(ms / 1000);
119
+ if (seconds < 60) return `${seconds}s`;
120
+ const minutes = Math.floor(seconds / 60);
121
+ const remSeconds = seconds % 60;
122
+ if (minutes < 60) return `${minutes}m${remSeconds ? `${remSeconds}s` : ""}`;
123
+ const hours = Math.floor(minutes / 60);
124
+ const remMinutes = minutes % 60;
125
+ return `${hours}h${remMinutes ? `${remMinutes}m` : ""}`;
126
+ }
127
+
128
+ function formatTime(timestamp: number): string {
129
+ return new Date(timestamp).toLocaleTimeString();
130
+ }
131
+
132
+ function padAnsi(value: string, width: number): string {
133
+ return value + " ".repeat(Math.max(0, width - visibleWidth(value)));
134
+ }
135
+
136
+ function lightBlue(value: string): string {
137
+ return `${LIGHT_BLUE_BG}${LIGHT_BLUE_FG}${value}${ANSI_RESET}`;
138
+ }
139
+
140
+ function blueBorder(value: string): string {
141
+ return `${LIGHT_BLUE_BORDER}${value}${ANSI_RESET}`;
142
+ }
143
+
144
+ function statusLabel(status: TaskStatus): string {
145
+ if (status === "completed") return "done";
146
+ if (status === "failed") return "error";
147
+ if (status === "killed") return "stopped";
148
+ return "running";
149
+ }
150
+
151
+ function statusColor(theme: Theme, status: TaskStatus, text = statusLabel(status)): string {
152
+ if (status === "completed") return theme.fg("success", text);
153
+ if (status === "failed") return theme.fg("error", text);
154
+ if (status === "killed") return theme.fg("warning", text);
155
+ return theme.fg("accent", text);
156
+ }
157
+
158
+ function sortTasksForUi(tasks: BgTask[]): BgTask[] {
159
+ const rank = (task: BgTask) => (task.status === "running" ? 0 : task.status === "failed" ? 1 : task.status === "killed" ? 2 : 3);
160
+ return [...tasks].sort((a, b) => {
161
+ const rankDiff = rank(a) - rank(b);
162
+ if (rankDiff !== 0) return rankDiff;
163
+ return (b.endTime ?? b.startTime) - (a.endTime ?? a.startTime);
164
+ });
165
+ }
166
+
167
+ function shellInvocation(command: string): { shell: string; args: string[] } {
168
+ if (process.platform === "win32") {
169
+ return { shell: process.env.ComSpec || "cmd.exe", args: ["/d", "/s", "/c", command] };
170
+ }
171
+ return { shell: process.env.SHELL || "/bin/sh", args: ["-c", command] };
172
+ }
173
+
174
+ function normalizeMaxBytes(value: unknown, fallback = DEFAULT_LOG_BYTES): number {
175
+ const raw = typeof value === "number" && Number.isFinite(value) ? Math.floor(value) : fallback;
176
+ return Math.max(1, Math.min(MAX_LOG_BYTES, raw));
177
+ }
178
+
179
+ function snapshot(task: BgTask): BgTaskSnapshot {
180
+ return {
181
+ id: task.id,
182
+ command: task.command,
183
+ description: task.description,
184
+ status: task.status,
185
+ outputPath: task.outputPath,
186
+ cwd: task.cwd,
187
+ startTime: task.startTime,
188
+ endTime: task.endTime,
189
+ exitCode: task.exitCode,
190
+ signal: task.signal,
191
+ pid: task.pid,
192
+ bytesWritten: task.bytesWritten,
193
+ error: task.error,
194
+ notified: task.notified,
195
+ notifyOnCompletion: task.notifyOnCompletion,
196
+ triggerOnCompletion: task.triggerOnCompletion,
197
+ timeoutSeconds: task.timeoutSeconds,
198
+ };
199
+ }
200
+
201
+ function textContent(text: string) {
202
+ return [{ type: "text" as const, text }];
203
+ }
204
+
205
+ function taskAge(task: BgTask, now = Date.now()): string {
206
+ return formatDuration((task.endTime ?? now) - task.startTime);
207
+ }
208
+
209
+ function formatTaskLine(task: BgTask, now = Date.now()): string {
210
+ const statusIcon =
211
+ task.status === "running" ? "▶" : task.status === "completed" ? "✓" : task.status === "killed" ? "■" : "✗";
212
+ const code = task.exitCode !== undefined ? ` exit=${task.exitCode}` : "";
213
+ const pid = task.pid ? ` pid=${task.pid}` : "";
214
+ const error = task.error ? ` error=${truncateChars(task.error, 80)}` : "";
215
+ const label = task.description || task.command;
216
+ return `${statusIcon} ${task.id} ${task.status} ${taskAge(task, now)}${code}${pid} — ${truncateChars(label, COMMAND_PREVIEW_CHARS)}${error}\n output: ${task.outputPath}`;
217
+ }
218
+
219
+ function formatTaskList(tasks: BgTask[], now = Date.now()): string {
220
+ if (tasks.length === 0) return "No background tasks in this Pi extension runtime.";
221
+ const running = tasks.filter((task) => task.status === "running");
222
+ const finished = tasks
223
+ .filter((task) => task.status !== "running")
224
+ .sort((a, b) => (b.endTime ?? b.startTime) - (a.endTime ?? a.startTime))
225
+ .slice(0, 20);
226
+ const ordered = [...running.sort((a, b) => a.startTime - b.startTime), ...finished];
227
+ return ordered.map((task) => formatTaskLine(task, now)).join("\n");
228
+ }
229
+
230
+ function formatSnapshotList(tasks: BgTaskSnapshot[], now = Date.now()): string {
231
+ if (tasks.length === 0) return "No background tasks in this Pi extension runtime.";
232
+ return tasks
233
+ .map((task) => {
234
+ const statusIcon =
235
+ task.status === "running" ? "▶" : task.status === "completed" ? "✓" : task.status === "killed" ? "■" : "✗";
236
+ const age = formatDuration((task.endTime ?? now) - task.startTime);
237
+ const code = task.exitCode !== undefined ? ` exit=${task.exitCode}` : "";
238
+ const pid = task.pid ? ` pid=${task.pid}` : "";
239
+ const error = task.error ? ` error=${truncateChars(task.error, 80)}` : "";
240
+ const label = task.description || task.command;
241
+ return `${statusIcon} ${task.id} ${task.status} ${age}${code}${pid} — ${truncateChars(label, COMMAND_PREVIEW_CHARS)}${error}\n output: ${task.outputPath}`;
242
+ })
243
+ .join("\n");
244
+ }
245
+
246
+ async function boundedRead(filePath: string, maxBytes: number, tail: boolean): Promise<{ content: string; truncated: boolean; bytesRead: number; totalBytes: number }> {
247
+ const stat = statSync(filePath);
248
+ const totalBytes = stat.size;
249
+ const bytesToRead = Math.min(totalBytes, maxBytes);
250
+ if (bytesToRead === 0) {
251
+ return { content: "", truncated: false, bytesRead: 0, totalBytes };
252
+ }
253
+
254
+ const file = await open(filePath, "r");
255
+ try {
256
+ const buffer = Buffer.alloc(bytesToRead);
257
+ const position = tail ? Math.max(0, totalBytes - bytesToRead) : 0;
258
+ const { bytesRead } = await file.read(buffer, 0, bytesToRead, position);
259
+ return {
260
+ content: buffer.subarray(0, bytesRead).toString("utf8"),
261
+ truncated: totalBytes > bytesRead,
262
+ bytesRead,
263
+ totalBytes,
264
+ };
265
+ } finally {
266
+ await file.close();
267
+ }
268
+ }
269
+
270
+ function renderPlainResult(result: { content?: Array<{ type: string; text?: string }> }, _options: ToolRenderResultOptions, _theme: any) {
271
+ const first = result.content?.find((part) => part.type === "text");
272
+ return new Text(first?.text ?? "", 0, 0);
273
+ }
274
+
275
+ type TaskManagerResult = "closed" | "killed";
276
+
277
+ type TaskManagerOptions = {
278
+ initialTaskId?: string;
279
+ getTasks: () => BgTask[];
280
+ stopTask: (task: BgTask) => Promise<void>;
281
+ markSeen: (taskId: string) => void;
282
+ isSeen: (taskId: string) => boolean;
283
+ };
284
+
285
+ class BackgroundTasksManager implements Component {
286
+ private mode: "list" | "detail" = "list";
287
+ private selectedIndex = 0;
288
+ private listScroll = 0;
289
+ private detailTaskId: string | undefined;
290
+ private showHistory = false;
291
+ private tailText = "";
292
+ private tailBytesRead = 0;
293
+ private tailTotalBytes = 0;
294
+ private tailTruncated = false;
295
+ private tailError: string | undefined;
296
+ private actionMessage: string | undefined;
297
+ private refreshTimer: NodeJS.Timeout;
298
+
299
+ constructor(
300
+ private readonly tui: Pick<TUI, "requestRender">,
301
+ private readonly theme: Theme,
302
+ private readonly done: (result: TaskManagerResult) => void,
303
+ private readonly options: TaskManagerOptions,
304
+ ) {
305
+ if (options.initialTaskId) {
306
+ this.detailTaskId = options.initialTaskId;
307
+ this.mode = "detail";
308
+ this.options.markSeen(options.initialTaskId);
309
+ void this.refreshTail();
310
+ }
311
+ this.refreshTimer = setInterval(() => {
312
+ if (this.mode === "detail") void this.refreshTail();
313
+ this.tui.requestRender();
314
+ }, STATUS_INTERVAL_MS);
315
+ }
316
+
317
+ dispose(): void {
318
+ clearInterval(this.refreshTimer);
319
+ }
320
+
321
+ invalidate(): void {}
322
+
323
+ handleInput(data: string): void {
324
+ this.actionMessage = undefined;
325
+ if (matchesKey(data, "escape") || data === "q" || data === "Q") {
326
+ if (this.mode === "detail" && this.currentTasks().length > 1) {
327
+ this.mode = "list";
328
+ this.detailTaskId = undefined;
329
+ this.tui.requestRender();
330
+ return;
331
+ }
332
+ this.done("closed");
333
+ return;
334
+ }
335
+
336
+ if (this.mode === "detail") {
337
+ this.handleDetailInput(data);
338
+ return;
339
+ }
340
+ this.handleListInput(data);
341
+ }
342
+
343
+ render(width: number): string[] {
344
+ const boxWidth = Math.max(48, Math.min(width, 96));
345
+ return this.mode === "detail" ? this.renderDetail(boxWidth) : this.renderList(boxWidth);
346
+ }
347
+
348
+ private handleListInput(data: string): void {
349
+ const tasks = this.currentTasks();
350
+ if (tasks.length === 0) {
351
+ if (matchesKey(data, "return")) this.done("closed");
352
+ return;
353
+ }
354
+ if (matchesKey(data, "up")) {
355
+ this.selectedIndex = Math.max(0, this.selectedIndex - 1);
356
+ this.ensureSelectionVisible();
357
+ this.tui.requestRender();
358
+ return;
359
+ }
360
+ if (matchesKey(data, "down")) {
361
+ this.selectedIndex = Math.min(tasks.length - 1, this.selectedIndex + 1);
362
+ this.ensureSelectionVisible();
363
+ this.tui.requestRender();
364
+ return;
365
+ }
366
+ if (matchesKey(data, "pageUp")) {
367
+ this.selectedIndex = Math.max(0, this.selectedIndex - LIST_VISIBLE_ROWS);
368
+ this.ensureSelectionVisible();
369
+ this.tui.requestRender();
370
+ return;
371
+ }
372
+ if (matchesKey(data, "pageDown")) {
373
+ this.selectedIndex = Math.min(tasks.length - 1, this.selectedIndex + LIST_VISIBLE_ROWS);
374
+ this.ensureSelectionVisible();
375
+ this.tui.requestRender();
376
+ return;
377
+ }
378
+ if (matchesKey(data, "return") || matchesKey(data, "right")) {
379
+ const task = tasks[this.selectedIndex];
380
+ if (!task) return;
381
+ this.openDetail(task.id);
382
+ return;
383
+ }
384
+ if (data === "x" || data === "X") {
385
+ const task = tasks[this.selectedIndex];
386
+ if (task) void this.killTaskFromUi(task);
387
+ return;
388
+ }
389
+ if (data === "h" || data === "H") {
390
+ this.showHistory = !this.showHistory;
391
+ this.selectedIndex = 0;
392
+ this.listScroll = 0;
393
+ this.tui.requestRender();
394
+ }
395
+ }
396
+
397
+ private handleDetailInput(data: string): void {
398
+ const task = this.detailTask();
399
+ if (matchesKey(data, "left")) {
400
+ this.mode = "list";
401
+ this.detailTaskId = undefined;
402
+ this.tui.requestRender();
403
+ return;
404
+ }
405
+ if (data === "r" || data === "R") {
406
+ void this.refreshTail();
407
+ return;
408
+ }
409
+ if ((data === "x" || data === "X") && task) {
410
+ void this.killTaskFromUi(task);
411
+ }
412
+ }
413
+
414
+ private currentTasks(): BgTask[] {
415
+ const allTasks = this.options.getTasks();
416
+ const visible = this.showHistory ? allTasks : allTasks.filter((task) => task.status === "running");
417
+ return sortTasksForUi(visible);
418
+ }
419
+
420
+ private detailTask(): BgTask | undefined {
421
+ return this.options.getTasks().find((task) => task.id === this.detailTaskId);
422
+ }
423
+
424
+ private openDetail(taskId: string): void {
425
+ this.detailTaskId = taskId;
426
+ this.mode = "detail";
427
+ this.tailText = "";
428
+ this.tailError = undefined;
429
+ this.options.markSeen(taskId);
430
+ void this.refreshTail();
431
+ this.tui.requestRender();
432
+ }
433
+
434
+ private ensureSelectionVisible(): void {
435
+ if (this.selectedIndex < this.listScroll) this.listScroll = this.selectedIndex;
436
+ if (this.selectedIndex >= this.listScroll + LIST_VISIBLE_ROWS) this.listScroll = this.selectedIndex - LIST_VISIBLE_ROWS + 1;
437
+ }
438
+
439
+ private async killTaskFromUi(task: BgTask): Promise<void> {
440
+ if (task.status !== "running") {
441
+ this.actionMessage = `Task ${task.id} is ${task.status}; nothing to stop.`;
442
+ this.tui.requestRender();
443
+ return;
444
+ }
445
+ this.actionMessage = `Stopping ${task.id}…`;
446
+ this.tui.requestRender();
447
+ try {
448
+ await this.options.stopTask(task);
449
+ this.actionMessage = `Stopped ${task.id}.`;
450
+ this.done("killed");
451
+ } catch (error) {
452
+ this.actionMessage = `Stop failed: ${error instanceof Error ? error.message : String(error)}`;
453
+ }
454
+ this.tui.requestRender();
455
+ }
456
+
457
+ private async refreshTail(): Promise<void> {
458
+ const task = this.detailTask();
459
+ if (!task) return;
460
+ try {
461
+ if (!existsSync(task.outputAbsPath)) {
462
+ this.tailText = "";
463
+ this.tailBytesRead = 0;
464
+ this.tailTotalBytes = 0;
465
+ this.tailTruncated = false;
466
+ this.tailError = `Output file not found: ${task.outputPath}`;
467
+ return;
468
+ }
469
+ const read = await boundedRead(task.outputAbsPath, DETAIL_TAIL_BYTES, true);
470
+ this.tailText = read.content;
471
+ this.tailBytesRead = read.bytesRead;
472
+ this.tailTotalBytes = read.totalBytes;
473
+ this.tailTruncated = read.truncated;
474
+ this.tailError = undefined;
475
+ } catch (error) {
476
+ this.tailError = `Output read failed: ${error instanceof Error ? error.message : String(error)}`;
477
+ }
478
+ this.tui.requestRender();
479
+ }
480
+
481
+ private frame(title: string, subtitle: string, body: string[], footer: string, width: number): string[] {
482
+ const inner = width - 2;
483
+ const top = blueBorder(`╭${"─".repeat(inner)}╮`);
484
+ const bottom = blueBorder(`╰${"─".repeat(inner)}╯`);
485
+ const row = (content = "") => `${blueBorder("│")}${padAnsi(truncateToWidth(content, inner), inner)}${blueBorder("│")}`;
486
+ const header = lightBlue(padAnsi(` ${title}`, inner));
487
+ const subtitleLine = subtitle ? lightBlue(padAnsi(` ${subtitle}`, inner)) : lightBlue(" ".repeat(inner));
488
+ const lines = [top, row(header), row(subtitleLine), row()];
489
+ for (const line of body) lines.push(row(line));
490
+ lines.push(row());
491
+ lines.push(row(footer));
492
+ lines.push(bottom);
493
+ return lines;
494
+ }
495
+
496
+ private renderList(width: number): string[] {
497
+ const tasks = this.currentTasks();
498
+ if (this.selectedIndex >= tasks.length) this.selectedIndex = Math.max(0, tasks.length - 1);
499
+ this.ensureSelectionVisible();
500
+
501
+ const allTasks = this.options.getTasks();
502
+ const allRunning = allTasks.filter((task) => task.status === "running").length;
503
+ const historyCount = allTasks.length - allRunning;
504
+ const unread = allTasks.filter((task) => task.status !== "running" && !this.options.isSeen(task.id)).length;
505
+ const subtitle = this.showHistory
506
+ ? `${allRunning} active · ${historyCount} history${unread ? ` · ${unread} unread` : ""}`
507
+ : allRunning > 0
508
+ ? `${allRunning} active shell${allRunning === 1 ? "" : "s"}`
509
+ : "No active shells";
510
+
511
+ const body: string[] = [];
512
+ if (tasks.length === 0) {
513
+ const message = this.showHistory
514
+ ? " No background tasks in this session."
515
+ : historyCount > 0
516
+ ? " No running background tasks. Press h to show recent history."
517
+ : " No running background tasks.";
518
+ body.push(this.theme.fg("dim", message));
519
+ } else {
520
+ const maxLabelWidth = Math.max(16, width - 42);
521
+ const visible = tasks.slice(this.listScroll, this.listScroll + LIST_VISIBLE_ROWS);
522
+ for (let i = 0; i < visible.length; i++) {
523
+ const task = visible[i]!;
524
+ const index = this.listScroll + i;
525
+ const selected = index === this.selectedIndex;
526
+ const pointer = selected ? "›" : " ";
527
+ const unreadMark = task.status !== "running" && !this.options.isSeen(task.id) ? this.theme.fg("warning", "● ") : " ";
528
+ const label = truncateChars(task.description || task.command, maxLabelWidth);
529
+ const status = statusColor(this.theme, task.status, statusLabel(task.status));
530
+ const runtime = taskAge(task);
531
+ const size = formatSize(task.bytesWritten);
532
+ let row = ` ${pointer} ${unreadMark}${this.theme.fg("accent", task.id)} ${label} ${this.theme.fg("dim", "·")} ${status} ${this.theme.fg("dim", `${runtime} ${size}`)}`;
533
+ if (selected) row = lightBlue(padAnsi(truncateToWidth(row, width - 4), width - 4));
534
+ body.push(row);
535
+ }
536
+ if (tasks.length > LIST_VISIBLE_ROWS) {
537
+ body.push(this.theme.fg("dim", ` Showing ${this.listScroll + 1}-${Math.min(tasks.length, this.listScroll + LIST_VISIBLE_ROWS)} of ${tasks.length}`));
538
+ }
539
+ }
540
+ if (this.actionMessage) body.push(this.theme.fg("warning", ` ${this.actionMessage}`));
541
+ return this.frame(
542
+ "Background tasks",
543
+ subtitle,
544
+ body,
545
+ ` ${this.theme.fg("dim", `↑/↓ select · Enter inspect · x stop · h ${this.showHistory ? "hide" : "show"} history · Esc close`)}`,
546
+ width,
547
+ );
548
+ }
549
+
550
+ private renderDetail(width: number): string[] {
551
+ const task = this.detailTask();
552
+ if (!task) {
553
+ this.mode = "list";
554
+ return this.renderList(width);
555
+ }
556
+ const status = statusColor(this.theme, task.status);
557
+ const exit = task.exitCode !== undefined ? ` exit=${task.exitCode}` : "";
558
+ const body: string[] = [
559
+ ` ${this.theme.fg("toolTitle", "Status:")} ${status}${this.theme.fg("dim", exit)}`,
560
+ ` ${this.theme.fg("toolTitle", "Runtime:")} ${taskAge(task)}${task.pid ? this.theme.fg("dim", ` · pid ${task.pid}`) : ""}`,
561
+ ` ${this.theme.fg("toolTitle", "Started:")} ${formatTime(task.startTime)}${task.endTime ? this.theme.fg("dim", ` · ended ${formatTime(task.endTime)}`) : ""}`,
562
+ ` ${this.theme.fg("toolTitle", "Output:")} ${this.theme.fg("accent", task.outputPath)}`,
563
+ ` ${this.theme.fg("toolTitle", "Command:")} ${truncateToWidth(task.command, width - 13)}`,
564
+ ];
565
+ if (task.error) body.push(` ${this.theme.fg("error", `Error: ${task.error}`)}`);
566
+ body.push("", ` ${this.theme.fg("toolTitle", "Output tail:")}`);
567
+ body.push(...this.renderOutputBox(width - 4));
568
+ if (this.actionMessage) body.push(this.theme.fg("warning", ` ${this.actionMessage}`));
569
+ const subtitle = `${task.id} · ${task.status === "running" ? "live tail refreshes every second" : "final output"}`;
570
+ const footer = ` ${this.theme.fg("dim", "← back · r refresh · x stop running task · Esc close")}`;
571
+ return this.frame("Shell details", subtitle, body, footer, width);
572
+ }
573
+
574
+ private renderOutputBox(width: number): string[] {
575
+ const inner = Math.max(20, width - 2);
576
+ const top = ` ${blueBorder(`╭${"─".repeat(inner)}╮`)}`;
577
+ const bottom = ` ${blueBorder(`╰${"─".repeat(inner)}╯`)}`;
578
+ const row = (content = "") => ` ${blueBorder("│")}${padAnsi(truncateToWidth(content, inner), inner)}${blueBorder("│")}`;
579
+ const lines = [top];
580
+ if (this.tailError) {
581
+ lines.push(row(this.theme.fg("error", this.tailError)));
582
+ } else if (!this.tailText) {
583
+ lines.push(row(this.theme.fg("dim", "No output yet")));
584
+ } else {
585
+ const outputLines = this.tailText.replace(/\r/g, "").split("\n").filter((line, index, array) => line.length > 0 || index < array.length - 1);
586
+ const visible = outputLines.slice(-DETAIL_VISIBLE_OUTPUT_LINES);
587
+ for (const line of visible) lines.push(row(this.theme.fg("toolOutput", line)));
588
+ }
589
+ while (lines.length < DETAIL_VISIBLE_OUTPUT_LINES + 1) lines.push(row());
590
+ lines.push(bottom);
591
+ const suffix = this.tailTruncated ? ` of ${formatSize(this.tailTotalBytes)}` : "";
592
+ lines.push(` ${this.theme.fg("dim", `Showing tail ${formatSize(this.tailBytesRead)}${suffix}`)}`);
593
+ return lines;
594
+ }
595
+ }
596
+
597
+ export default function backgroundTasksExtension(pi: ExtensionAPI): void {
598
+ const tasks = new Map<string, BgTask>();
599
+ let runtimeDirAbs: string | undefined;
600
+ let runtimeDirDisplay: string | undefined;
601
+ let currentCtx: ExtensionContext | undefined;
602
+ let shuttingDown = false;
603
+ let statusInterval: NodeJS.Timeout | undefined;
604
+ const seenTaskIds = new Set<string>();
605
+
606
+ function makeTaskId(): string {
607
+ for (let attempt = 0; attempt < 20; attempt++) {
608
+ const id = `b${randomBytes(4).toString("hex")}`;
609
+ if (!tasks.has(id)) return id;
610
+ }
611
+ throw new Error("Could not generate a unique background task ID after 20 attempts");
612
+ }
613
+
614
+ async function ensureRuntimeDir(ctx: ExtensionContext): Promise<{ abs: string; display: string }> {
615
+ if (runtimeDirAbs && runtimeDirDisplay) return { abs: runtimeDirAbs, display: runtimeDirDisplay };
616
+ const sessionId = sanitizePathSegment(ctx.sessionManager.getSessionId?.() || "session");
617
+ const runId = `${sessionId}-${process.pid}`;
618
+ runtimeDirAbs = join(ctx.cwd, ".pi", "tasks", runId);
619
+ runtimeDirDisplay = join(".pi", "tasks", runId);
620
+ await mkdir(runtimeDirAbs, { recursive: true });
621
+ return { abs: runtimeDirAbs, display: runtimeDirDisplay };
622
+ }
623
+
624
+ async function writeMetadata(task: BgTask): Promise<void> {
625
+ await writeFile(task.metadataAbsPath, `${JSON.stringify(snapshot(task), null, 2)}\n`, "utf8");
626
+ }
627
+
628
+ function updateUi(ctx = currentCtx): void {
629
+ if (shuttingDown || !ctx) return;
630
+ try {
631
+ if (!ctx.hasUI) return;
632
+ const running = [...tasks.values()].filter((task) => task.status === "running");
633
+ ctx.ui.setWidget("background-tasks", undefined);
634
+ if (running.length === 0) {
635
+ ctx.ui.setStatus("background-tasks", undefined);
636
+ return;
637
+ }
638
+
639
+ const label = ` bg ${running.length} running · /tasks `;
640
+ ctx.ui.setStatus("background-tasks", lightBlue(label));
641
+ } catch (error) {
642
+ console.error(`[background-tasks] UI update failed: ${error instanceof Error ? error.message : String(error)}`);
643
+ currentCtx = undefined;
644
+ }
645
+ }
646
+
647
+ function appendToOutput(task: BgTask, data: Buffer | string): void {
648
+ if (!task.stream || task.stream.destroyed) return;
649
+ const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data, "utf8");
650
+ if (buffer.length === 0) return;
651
+
652
+ const nextBytes = task.bytesWritten + buffer.length;
653
+ if (nextBytes <= MAX_OUTPUT_BYTES) {
654
+ task.stream.write(buffer);
655
+ task.bytesWritten = nextBytes;
656
+ return;
657
+ }
658
+
659
+ const remaining = Math.max(0, MAX_OUTPUT_BYTES - task.bytesWritten);
660
+ if (remaining > 0) {
661
+ task.stream.write(buffer.subarray(0, remaining));
662
+ task.bytesWritten += remaining;
663
+ }
664
+
665
+ if (!task.capExceeded) {
666
+ task.capExceeded = true;
667
+ task.error = `Output exceeded cap of ${formatSize(MAX_OUTPUT_BYTES)}; terminating task`;
668
+ const notice = `\n\n[background task error: ${task.error}]\n`;
669
+ task.stream.write(notice);
670
+ task.bytesWritten += Buffer.byteLength(notice, "utf8");
671
+ task.killKind = "output_cap";
672
+ try {
673
+ requestKill(task, "SIGTERM");
674
+ } catch (error) {
675
+ task.error = `${task.error}; kill failed: ${error instanceof Error ? error.message : String(error)}`;
676
+ void finalizeTask(task, "failed", null, undefined, task.error);
677
+ }
678
+ }
679
+ }
680
+
681
+ function requestKill(task: BgTask, signal: NodeJS.Signals = "SIGTERM"): void {
682
+ if (task.status !== "running") {
683
+ throw new Error(`Task ${task.id} is ${task.status}, not running`);
684
+ }
685
+ if (!task.child) {
686
+ throw new Error(`Task ${task.id} has no child process handle`);
687
+ }
688
+ if (!task.pid) {
689
+ throw new Error(`Task ${task.id} has no process id`);
690
+ }
691
+ if (task.killSignalSent && signal === "SIGTERM") return;
692
+
693
+ const errors: string[] = [];
694
+ let killed = false;
695
+
696
+ if (process.platform !== "win32") {
697
+ try {
698
+ process.kill(-task.pid, signal);
699
+ killed = true;
700
+ } catch (error) {
701
+ errors.push(`process group kill failed: ${error instanceof Error ? error.message : String(error)}`);
702
+ }
703
+ }
704
+
705
+ if (!killed) {
706
+ try {
707
+ task.child.kill(signal);
708
+ killed = true;
709
+ } catch (error) {
710
+ errors.push(`child kill failed: ${error instanceof Error ? error.message : String(error)}`);
711
+ }
712
+ }
713
+
714
+ if (!killed) {
715
+ throw new Error(`Could not kill task ${task.id}: ${errors.join("; ")}`);
716
+ }
717
+
718
+ task.killSignalSent = true;
719
+ setTimeout(() => {
720
+ if (task.status !== "running") return;
721
+ try {
722
+ requestKill(task, "SIGKILL");
723
+ } catch (error) {
724
+ task.error = `SIGKILL failed: ${error instanceof Error ? error.message : String(error)}`;
725
+ void writeMetadata(task).catch((metadataError) => {
726
+ console.error(`[background-tasks] failed to write metadata for ${task.id}:`, metadataError);
727
+ });
728
+ }
729
+ }, KILL_GRACE_MS).unref?.();
730
+ }
731
+
732
+ function waitForEnd(task: BgTask, timeoutMs: number): Promise<boolean> {
733
+ if (task.status !== "running") return Promise.resolve(true);
734
+ return new Promise((resolve) => {
735
+ const timeout = setTimeout(() => {
736
+ const idx = task.waiters.indexOf(done);
737
+ if (idx >= 0) task.waiters.splice(idx, 1);
738
+ resolve(false);
739
+ }, timeoutMs);
740
+ const done = () => {
741
+ clearTimeout(timeout);
742
+ resolve(true);
743
+ };
744
+ task.waiters.push(done);
745
+ });
746
+ }
747
+
748
+ async function stopTask(task: BgTask, kind: KillKind, reason?: string): Promise<BgTask> {
749
+ if (task.status !== "running") {
750
+ throw new Error(`Task ${task.id} is ${task.status}, not running`);
751
+ }
752
+ task.killKind = kind;
753
+ if (reason) task.error = reason;
754
+ requestKill(task, "SIGTERM");
755
+ const stopped = await waitForEnd(task, STOP_WAIT_MS);
756
+ if (!stopped) {
757
+ throw new Error(`Task ${task.id} did not exit within ${formatDuration(STOP_WAIT_MS)} after SIGTERM/SIGKILL`);
758
+ }
759
+ return task;
760
+ }
761
+
762
+ async function notifyCompletion(task: BgTask): Promise<void> {
763
+ if (!task.notifyOnCompletion || task.notified || shuttingDown) return;
764
+ task.notified = true;
765
+ const exit = task.exitCode === undefined ? "" : `\n <exit-code>${task.exitCode}</exit-code>`;
766
+ const error = task.error ? `\n <error>${escapeXml(task.error)}</error>` : "";
767
+ const content = [
768
+ "<background-task-notification>",
769
+ ` <task-id>${task.id}</task-id>`,
770
+ ` <status>${task.status}</status>`,
771
+ exit,
772
+ error,
773
+ ` <output-file>${escapeXml(task.outputPath)}</output-file>`,
774
+ ` <summary>${escapeXml(`Background command ${JSON.stringify(task.description || task.command)} ${task.status}`)}</summary>`,
775
+ "</background-task-notification>",
776
+ ]
777
+ .filter(Boolean)
778
+ .join("\n");
779
+
780
+ try {
781
+ pi.sendMessage(
782
+ {
783
+ customType: "background-task-notification",
784
+ content,
785
+ display: true,
786
+ details: snapshot(task),
787
+ },
788
+ { deliverAs: "followUp", triggerTurn: task.triggerOnCompletion },
789
+ );
790
+ } catch (error) {
791
+ task.notified = false;
792
+ throw new Error(`Failed to send background task notification for ${task.id}: ${error instanceof Error ? error.message : String(error)}`);
793
+ }
794
+ }
795
+
796
+ function escapeXml(value: string): string {
797
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
798
+ }
799
+
800
+ async function finalizeTask(task: BgTask, status: TaskStatus, exitCode: number | null, signal?: string | null, error?: string): Promise<void> {
801
+ if (task.finalized) return;
802
+ task.finalized = true;
803
+ if (task.timeoutHandle) clearTimeout(task.timeoutHandle);
804
+ task.status = status;
805
+ task.exitCode = exitCode;
806
+ task.signal = signal ?? null;
807
+ task.endTime = Date.now();
808
+ if (error) task.error = error;
809
+ if (task.stream && !task.stream.destroyed) task.stream.end();
810
+
811
+ for (const waiter of task.waiters.splice(0)) waiter();
812
+
813
+ try {
814
+ await writeMetadata(task);
815
+ } catch (metadataError) {
816
+ console.error(`[background-tasks] failed to write metadata for ${task.id}:`, metadataError);
817
+ }
818
+
819
+ updateUi();
820
+ try {
821
+ await notifyCompletion(task);
822
+ } catch (notificationError) {
823
+ console.error(`[background-tasks] notification failed for ${task.id}:`, notificationError);
824
+ }
825
+ try {
826
+ await writeMetadata(task);
827
+ } catch (metadataError) {
828
+ console.error(`[background-tasks] failed to update notification metadata for ${task.id}:`, metadataError);
829
+ }
830
+ pruneOldTasks();
831
+ }
832
+
833
+ function pruneOldTasks(): void {
834
+ if (tasks.size <= MAX_RECENT_TASKS) return;
835
+ const removable = [...tasks.values()]
836
+ .filter((task) => task.status !== "running")
837
+ .sort((a, b) => (a.endTime ?? a.startTime) - (b.endTime ?? b.startTime));
838
+ while (tasks.size > MAX_RECENT_TASKS && removable.length > 0) {
839
+ const task = removable.shift();
840
+ if (task) tasks.delete(task.id);
841
+ }
842
+ }
843
+
844
+ async function startTask(
845
+ ctx: ExtensionContext,
846
+ command: string,
847
+ options: { description?: string; timeoutSeconds?: number; notifyOnCompletion?: boolean; triggerOnCompletion?: boolean } = {},
848
+ ): Promise<BgTask> {
849
+ const normalizedCommand = stripMatchingQuotes(command);
850
+ if (!normalizedCommand) throw new Error("Background command is empty");
851
+ if (shuttingDown) throw new Error("Cannot start a background task while Pi is shutting down");
852
+
853
+ currentCtx = ctx;
854
+ const dir = await ensureRuntimeDir(ctx);
855
+ const id = makeTaskId();
856
+ const outputAbsPath = join(dir.abs, `${id}.output`);
857
+ const metadataAbsPath = join(dir.abs, `${id}.json`);
858
+ const outputPath = join(dir.display, `${id}.output`);
859
+ const timeoutSeconds =
860
+ typeof options.timeoutSeconds === "number" && Number.isFinite(options.timeoutSeconds) && options.timeoutSeconds > 0
861
+ ? Math.floor(options.timeoutSeconds)
862
+ : undefined;
863
+
864
+ const task: BgTask = {
865
+ id,
866
+ command: normalizedCommand,
867
+ description: options.description?.trim() || undefined,
868
+ status: "running",
869
+ outputPath,
870
+ outputAbsPath,
871
+ metadataAbsPath,
872
+ cwd: ctx.cwd,
873
+ startTime: Date.now(),
874
+ exitCode: undefined,
875
+ pid: undefined,
876
+ bytesWritten: 0,
877
+ notified: false,
878
+ notifyOnCompletion: options.notifyOnCompletion ?? true,
879
+ triggerOnCompletion: options.triggerOnCompletion ?? false,
880
+ timeoutSeconds,
881
+ waiters: [],
882
+ };
883
+ tasks.set(id, task);
884
+
885
+ const stream = createWriteStream(outputAbsPath, { flags: "a", encoding: "utf8" });
886
+ task.stream = stream;
887
+ stream.on("error", (error) => {
888
+ task.error = `Output file write failed: ${error.message}`;
889
+ if (task.status === "running") {
890
+ task.killKind = "output_cap";
891
+ try {
892
+ requestKill(task, "SIGTERM");
893
+ } catch (killError) {
894
+ void finalizeTask(
895
+ task,
896
+ "failed",
897
+ null,
898
+ undefined,
899
+ `${task.error}; kill failed: ${killError instanceof Error ? killError.message : String(killError)}`,
900
+ );
901
+ }
902
+ }
903
+ });
904
+
905
+ try {
906
+ const invocation = shellInvocation(normalizedCommand);
907
+ const child = spawn(invocation.shell, invocation.args, {
908
+ cwd: ctx.cwd,
909
+ detached: process.platform !== "win32",
910
+ stdio: ["ignore", "pipe", "pipe"],
911
+ env: process.env,
912
+ windowsHide: true,
913
+ });
914
+
915
+ task.child = child;
916
+ task.pid = child.pid;
917
+
918
+ child.stdout?.on("data", (data) => appendToOutput(task, data));
919
+ child.stderr?.on("data", (data) => appendToOutput(task, data));
920
+
921
+ child.on("error", (error) => {
922
+ appendToOutput(task, `\n[background task spawn error: ${error.message}]\n`);
923
+ void finalizeTask(task, "failed", null, undefined, error.message);
924
+ });
925
+
926
+ child.on("close", (code, signalName) => {
927
+ let status: TaskStatus;
928
+ let error: string | undefined;
929
+ if (task.killKind === "user" || task.killKind === "shutdown") {
930
+ status = "killed";
931
+ } else if (task.killKind === "timeout") {
932
+ status = "failed";
933
+ error = task.error || `Timed out after ${task.timeoutSeconds}s`;
934
+ } else if (task.killKind === "output_cap") {
935
+ status = "failed";
936
+ error = task.error || `Output exceeded cap of ${formatSize(MAX_OUTPUT_BYTES)}`;
937
+ } else if ((code ?? 0) === 0) {
938
+ status = "completed";
939
+ } else {
940
+ status = "failed";
941
+ error = `Exited with code ${code ?? "null"}${signalName ? ` (${signalName})` : ""}`;
942
+ }
943
+ void finalizeTask(task, status, code, signalName, error);
944
+ });
945
+
946
+ if (timeoutSeconds) {
947
+ task.timeoutHandle = setTimeout(() => {
948
+ if (task.status !== "running") return;
949
+ task.killKind = "timeout";
950
+ task.error = `Timed out after ${timeoutSeconds}s`;
951
+ appendToOutput(task, `\n[background task timeout: ${task.error}]\n`);
952
+ try {
953
+ requestKill(task, "SIGTERM");
954
+ } catch (error) {
955
+ void finalizeTask(task, "failed", null, undefined, `${task.error}; kill failed: ${error instanceof Error ? error.message : String(error)}`);
956
+ }
957
+ }, timeoutSeconds * 1000);
958
+ }
959
+
960
+ await writeMetadata(task);
961
+ updateUi(ctx);
962
+ return task;
963
+ } catch (error) {
964
+ const message = error instanceof Error ? error.message : String(error);
965
+ appendToOutput(task, `\n[background task spawn exception: ${message}]\n`);
966
+ await finalizeTask(task, "failed", null, undefined, message);
967
+ throw new Error(`Failed to start background task: ${message}`);
968
+ }
969
+ }
970
+
971
+ function resolveTask(idOrPrefix: string): BgTask {
972
+ const id = idOrPrefix.trim();
973
+ if (!id) throw new Error("Task ID is required");
974
+ const exact = tasks.get(id);
975
+ if (exact) return exact;
976
+ const matches = [...tasks.values()].filter((task) => task.id.startsWith(id));
977
+ if (matches.length === 1) return matches[0];
978
+ if (matches.length > 1) throw new Error(`Ambiguous task ID prefix "${id}": ${matches.map((task) => task.id).join(", ")}`);
979
+ throw new Error(`Unknown background task ID: ${id}`);
980
+ }
981
+
982
+ async function getTaskLogs(task: BgTask, maxBytes: number, tail: boolean): Promise<{ text: string; details: BgLogsDetails }> {
983
+ if (!existsSync(task.outputAbsPath)) {
984
+ throw new Error(`Output file does not exist for ${task.id}: ${task.outputPath}`);
985
+ }
986
+ const read = await boundedRead(task.outputAbsPath, maxBytes, tail);
987
+ const direction = tail ? "tail" : "head";
988
+ let text = read.content || "(no output yet)";
989
+ if (read.truncated) {
990
+ const omitted = read.totalBytes - read.bytesRead;
991
+ const notice = `\n\n[Showing ${direction} ${formatSize(read.bytesRead)} of ${formatSize(read.totalBytes)}; ${formatSize(omitted)} omitted. Full output: ${task.outputPath}]`;
992
+ text = tail ? `${notice}\n\n${text}` : `${text}${notice}`;
993
+ } else {
994
+ text += `\n\n[Full output: ${task.outputPath}]`;
995
+ }
996
+ return {
997
+ text,
998
+ details: {
999
+ task: snapshot(task),
1000
+ path: task.outputPath,
1001
+ bytesRead: read.bytesRead,
1002
+ truncated: read.truncated,
1003
+ tail,
1004
+ },
1005
+ };
1006
+ }
1007
+
1008
+ async function openTaskManager(ctx: ExtensionCommandContext | ExtensionContext, initialTaskId?: string): Promise<void> {
1009
+ currentCtx = ctx;
1010
+ if (!ctx.hasUI) {
1011
+ ctx.ui.notify("Background task manager requires an interactive Pi UI. Use /jobs, /logs, or the bg_status/bg_logs tools in non-interactive mode.", "error");
1012
+ return;
1013
+ }
1014
+ await ctx.ui.custom<TaskManagerResult>(
1015
+ (tui, theme, _keybindings, done) =>
1016
+ new BackgroundTasksManager(tui, theme, done, {
1017
+ initialTaskId,
1018
+ getTasks: () => [...tasks.values()],
1019
+ stopTask: async (task) => {
1020
+ await stopTask(task, "user");
1021
+ updateUi(ctx);
1022
+ },
1023
+ markSeen: (taskId) => {
1024
+ seenTaskIds.add(taskId);
1025
+ updateUi(ctx);
1026
+ },
1027
+ isSeen: (taskId) => seenTaskIds.has(taskId),
1028
+ }),
1029
+ {
1030
+ overlay: true,
1031
+ overlayOptions: { anchor: "center", width: "86%", minWidth: 64, maxHeight: "85%", margin: 1 },
1032
+ },
1033
+ );
1034
+ updateUi(ctx);
1035
+ }
1036
+
1037
+ pi.registerMessageRenderer<BgTaskSnapshot>("background-task-notification", (message, _options, theme) => {
1038
+ const task = message.details;
1039
+ const status = task?.status ?? "completed";
1040
+ const color = status === "completed" ? "success" : status === "failed" ? "error" : status === "killed" ? "warning" : "accent";
1041
+ const id = task?.id ?? "background task";
1042
+ const output = task?.outputPath ? `\n${theme.fg("dim", `Output: ${task.outputPath}`)}` : "";
1043
+ const error = task?.error ? `\n${theme.fg("error", task.error)}` : "";
1044
+ return new Text(`${theme.fg(color as any, `[bg ${status}]`)} ${theme.fg("accent", id)}${output}${error}`, 0, 0);
1045
+ });
1046
+
1047
+ pi.on("session_start", async (_event, ctx) => {
1048
+ shuttingDown = false;
1049
+ currentCtx = ctx;
1050
+ await ensureRuntimeDir(ctx);
1051
+ updateUi(ctx);
1052
+ if (statusInterval) clearInterval(statusInterval);
1053
+ statusInterval = setInterval(() => updateUi(), STATUS_INTERVAL_MS);
1054
+ });
1055
+
1056
+ pi.on("session_shutdown", async (_event, ctx) => {
1057
+ shuttingDown = true;
1058
+ currentCtx = undefined;
1059
+ if (statusInterval) {
1060
+ clearInterval(statusInterval);
1061
+ statusInterval = undefined;
1062
+ }
1063
+ const running = [...tasks.values()].filter((task) => task.status === "running");
1064
+ if (running.length === 0) return;
1065
+
1066
+ const failures: string[] = [];
1067
+ await Promise.all(
1068
+ running.map(async (task) => {
1069
+ try {
1070
+ await stopTask(task, "shutdown", "Killed during Pi session shutdown/reload");
1071
+ } catch (error) {
1072
+ const message = `${task.id}: ${error instanceof Error ? error.message : String(error)}`;
1073
+ failures.push(message);
1074
+ console.error(`[background-tasks] shutdown cleanup failed for ${message}`);
1075
+ }
1076
+ }),
1077
+ );
1078
+ if (failures.length > 0 && ctx.hasUI) {
1079
+ ctx.ui.notify(`Background task cleanup failed:\n${failures.join("\n")}`, "error");
1080
+ }
1081
+ });
1082
+
1083
+ pi.registerCommand("bg", {
1084
+ description: "Start a shell command as a tracked background task: /bg <command>",
1085
+ handler: async (args, ctx) => {
1086
+ try {
1087
+ const task = await startTask(ctx, args, { notifyOnCompletion: true, triggerOnCompletion: false });
1088
+ ctx.ui.notify(`Started ${task.id}\nOutput: ${task.outputPath}\nCommand: ${task.command}`, "info");
1089
+ } catch (error) {
1090
+ ctx.ui.notify(`Background task failed to start: ${error instanceof Error ? error.message : String(error)}`, "error");
1091
+ }
1092
+ },
1093
+ });
1094
+
1095
+ pi.registerCommand("tasks", {
1096
+ description: "Open the Claude-like background task manager UI",
1097
+ handler: async (args, ctx) => {
1098
+ const taskId = args.trim() || undefined;
1099
+ await openTaskManager(ctx, taskId);
1100
+ },
1101
+ });
1102
+
1103
+ pi.registerCommand("bg-tasks", {
1104
+ description: "Open the background task manager UI",
1105
+ handler: async (args, ctx) => {
1106
+ const taskId = args.trim() || undefined;
1107
+ await openTaskManager(ctx, taskId);
1108
+ },
1109
+ });
1110
+
1111
+ pi.registerShortcut("shift+down" as any, {
1112
+ description: "Open background task manager",
1113
+ handler: async (ctx) => {
1114
+ await openTaskManager(ctx);
1115
+ },
1116
+ });
1117
+
1118
+ pi.registerCommand("jobs", {
1119
+ description: "List running and recent background tasks",
1120
+ handler: async (_args, ctx) => {
1121
+ currentCtx = ctx;
1122
+ ctx.ui.notify(formatTaskList([...tasks.values()]), "info");
1123
+ updateUi(ctx);
1124
+ },
1125
+ });
1126
+
1127
+ pi.registerCommand("logs", {
1128
+ description: "Show bounded output from a background task: /logs <id> [maxBytes]",
1129
+ getArgumentCompletions: (prefix) => {
1130
+ const matches = [...tasks.values()]
1131
+ .filter((task) => task.id.startsWith(prefix.trim()))
1132
+ .slice(0, 20)
1133
+ .map((task) => ({ value: task.id, label: task.id, description: `${task.status} — ${truncateChars(task.command, 60)}` }));
1134
+ return matches.length > 0 ? matches : null;
1135
+ },
1136
+ handler: async (args, ctx) => {
1137
+ try {
1138
+ currentCtx = ctx;
1139
+ const [id, bytes] = args.trim().split(/\s+/, 2);
1140
+ const task = resolveTask(id || "");
1141
+ const maxBytes = normalizeMaxBytes(Number(bytes));
1142
+ const logs = await getTaskLogs(task, maxBytes, true);
1143
+ ctx.ui.notify(logs.text, "info");
1144
+ } catch (error) {
1145
+ ctx.ui.notify(`Background logs error: ${error instanceof Error ? error.message : String(error)}`, "error");
1146
+ }
1147
+ },
1148
+ });
1149
+
1150
+ pi.registerCommand("kill", {
1151
+ description: "Stop a running background task: /kill <id>",
1152
+ getArgumentCompletions: (prefix) => {
1153
+ const matches = [...tasks.values()]
1154
+ .filter((task) => task.status === "running" && task.id.startsWith(prefix.trim()))
1155
+ .slice(0, 20)
1156
+ .map((task) => ({ value: task.id, label: task.id, description: truncateChars(task.command, 70) }));
1157
+ return matches.length > 0 ? matches : null;
1158
+ },
1159
+ handler: async (args, ctx) => {
1160
+ try {
1161
+ currentCtx = ctx;
1162
+ const task = resolveTask(args.trim());
1163
+ await stopTask(task, "user");
1164
+ ctx.ui.notify(`Killed ${task.id}. Output: ${task.outputPath}`, "info");
1165
+ updateUi(ctx);
1166
+ } catch (error) {
1167
+ ctx.ui.notify(`Background kill error: ${error instanceof Error ? error.message : String(error)}`, "error");
1168
+ }
1169
+ },
1170
+ });
1171
+
1172
+ const BgRunParams = Type.Object({
1173
+ command: Type.String({ description: "Shell command to start in the background" }),
1174
+ description: Type.Optional(Type.String({ description: "Short human-readable task description" })),
1175
+ timeoutSeconds: Type.Optional(Type.Number({ description: "Optional timeout; task is failed and killed when exceeded" })),
1176
+ notifyOnCompletion: Type.Optional(Type.Boolean({ description: "Whether to show a completion notification. Default: true." })),
1177
+ triggerOnCompletion: Type.Optional(Type.Boolean({ description: "Whether completion should trigger a follow-up agent turn. Default: true for bg_run." })),
1178
+ });
1179
+
1180
+ pi.registerTool<typeof BgRunParams, BgRunDetails>({
1181
+ name: "bg_run",
1182
+ label: "Background Run",
1183
+ description: `Start a long-running shell command in the background and return immediately with a task ID and output path. Output is written to .pi/tasks and model-visible logs are bounded to ${formatSize(MAX_LOG_BYTES)}.`,
1184
+ promptSnippet: "Start long-running shell commands in the background and return a task ID plus output file path",
1185
+ promptGuidelines: [
1186
+ "Use bg_run instead of bash for commands expected to run for a long time, such as test suites, dev servers, watchers, builds, or sleeps.",
1187
+ "After bg_run, use bg_status and bg_logs to inspect progress; do not assume the background task completed until status says completed, failed, or killed.",
1188
+ "When a <background-task-notification> appears, react to it: inspect bg_status/bg_logs as needed, then report completion, failure, or next steps to the user.",
1189
+ ],
1190
+ parameters: BgRunParams,
1191
+ async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
1192
+ const task = await startTask(ctx, params.command, {
1193
+ description: params.description,
1194
+ timeoutSeconds: params.timeoutSeconds,
1195
+ notifyOnCompletion: params.notifyOnCompletion ?? true,
1196
+ triggerOnCompletion: params.triggerOnCompletion ?? true,
1197
+ });
1198
+ return {
1199
+ content: textContent(`Started background task ${task.id}\nStatus: ${task.status}\nPID: ${task.pid ?? "unknown"}\nOutput: ${task.outputPath}`),
1200
+ details: { task: snapshot(task) },
1201
+ };
1202
+ },
1203
+ renderCall(args, theme) {
1204
+ return new Text(`${theme.fg("toolTitle", theme.bold("bg_run "))}${theme.fg("muted", truncateChars(args.description || args.command || "", 90))}`, 0, 0);
1205
+ },
1206
+ renderResult(result, _options, theme) {
1207
+ const task = result.details?.task;
1208
+ if (!task) return renderPlainResult(result, _options, theme);
1209
+ return new Text(`${theme.fg("success", "✓ started")} ${theme.fg("accent", task.id)}\n${theme.fg("dim", `Output: ${task.outputPath}`)}`, 0, 0);
1210
+ },
1211
+ });
1212
+
1213
+ const BgStatusParams = Type.Object({
1214
+ taskId: Type.Optional(Type.String({ description: "Optional task ID or unambiguous prefix. If omitted, all running/recent tasks are returned." })),
1215
+ });
1216
+
1217
+ pi.registerTool<typeof BgStatusParams, BgStatusDetails>({
1218
+ name: "bg_status",
1219
+ label: "Background Status",
1220
+ description: "Inspect one background task or list all running/recent background tasks.",
1221
+ promptSnippet: "Inspect status for one or all background tasks",
1222
+ promptGuidelines: ["Use bg_status before bg_logs when you need to know whether a background task is still running or has finished."],
1223
+ parameters: BgStatusParams,
1224
+ async execute(_toolCallId, params) {
1225
+ const selected = params.taskId ? [resolveTask(params.taskId)] : [...tasks.values()];
1226
+ const snapshots = selected.map(snapshot);
1227
+ return {
1228
+ content: textContent(formatSnapshotList(snapshots)),
1229
+ details: { tasks: snapshots },
1230
+ };
1231
+ },
1232
+ renderCall(args, theme) {
1233
+ return new Text(`${theme.fg("toolTitle", theme.bold("bg_status"))}${args.taskId ? ` ${theme.fg("accent", args.taskId)}` : ""}`, 0, 0);
1234
+ },
1235
+ renderResult: renderPlainResult,
1236
+ });
1237
+
1238
+ const BgLogsParams = Type.Object({
1239
+ taskId: Type.String({ description: "Task ID or unambiguous prefix" }),
1240
+ maxBytes: Type.Optional(Type.Number({ description: `Maximum bytes to return, capped at ${formatSize(MAX_LOG_BYTES)}. Default: ${formatSize(DEFAULT_LOG_BYTES)}.` })),
1241
+ tail: Type.Optional(Type.Boolean({ description: "Read the tail of the log when true, head when false. Default: true." })),
1242
+ });
1243
+
1244
+ pi.registerTool<typeof BgLogsParams, BgLogsDetails>({
1245
+ name: "bg_logs",
1246
+ label: "Background Logs",
1247
+ description: `Read bounded output from a background task. Output is capped at ${formatSize(MAX_LOG_BYTES)} for model safety and points to the full output file when truncated.`,
1248
+ promptSnippet: "Read bounded output from a background task log",
1249
+ promptGuidelines: ["Use bg_logs with a modest maxBytes value to inspect background task progress without flooding context."],
1250
+ parameters: BgLogsParams,
1251
+ async execute(_toolCallId, params) {
1252
+ const task = resolveTask(params.taskId);
1253
+ const logs = await getTaskLogs(task, normalizeMaxBytes(params.maxBytes), params.tail ?? true);
1254
+ return {
1255
+ content: textContent(logs.text),
1256
+ details: logs.details,
1257
+ };
1258
+ },
1259
+ renderCall(args, theme) {
1260
+ return new Text(`${theme.fg("toolTitle", theme.bold("bg_logs "))}${theme.fg("accent", args.taskId)}`, 0, 0);
1261
+ },
1262
+ renderResult(result, { expanded }, theme) {
1263
+ const details = result.details;
1264
+ if (!details) return renderPlainResult(result, { expanded, isPartial: false }, theme);
1265
+ let text = `${theme.fg("accent", details.task.id)} ${theme.fg("muted", details.tail ? "tail" : "head")} ${formatSize(details.bytesRead)}`;
1266
+ if (details.truncated) text += theme.fg("warning", " (truncated)");
1267
+ text += `\n${theme.fg("dim", `Full output: ${details.path}`)}`;
1268
+ if (expanded) {
1269
+ const content = result.content?.[0];
1270
+ if (content?.type === "text") text += `\n${theme.fg("toolOutput", content.text.split("\n").slice(0, 30).join("\n"))}`;
1271
+ }
1272
+ return new Text(text, 0, 0);
1273
+ },
1274
+ });
1275
+
1276
+ const BgKillParams = Type.Object({
1277
+ taskId: Type.String({ description: "Task ID or unambiguous prefix to stop" }),
1278
+ });
1279
+
1280
+ pi.registerTool<typeof BgKillParams, BgKillDetails>({
1281
+ name: "bg_kill",
1282
+ label: "Background Kill",
1283
+ description: "Stop a running background task by ID. Fails loudly if the task is unknown or already finished.",
1284
+ promptSnippet: "Stop a running background task by ID",
1285
+ promptGuidelines: ["Use bg_kill when the user asks to stop a background task or when a bg_run command is no longer needed."],
1286
+ parameters: BgKillParams,
1287
+ async execute(_toolCallId, params) {
1288
+ const task = resolveTask(params.taskId);
1289
+ await stopTask(task, "user");
1290
+ const message = `Killed background task ${task.id}. Output: ${task.outputPath}`;
1291
+ return {
1292
+ content: textContent(message),
1293
+ details: { task: snapshot(task), message },
1294
+ };
1295
+ },
1296
+ renderCall(args, theme) {
1297
+ return new Text(`${theme.fg("toolTitle", theme.bold("bg_kill "))}${theme.fg("accent", args.taskId)}`, 0, 0);
1298
+ },
1299
+ renderResult(result, _options, theme) {
1300
+ const task = result.details?.task;
1301
+ if (!task) return renderPlainResult(result, _options, theme);
1302
+ return new Text(`${theme.fg("warning", "■ killed")} ${theme.fg("accent", task.id)}\n${theme.fg("dim", `Output: ${task.outputPath}`)}`, 0, 0);
1303
+ },
1304
+ });
1305
+ }