-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbgrun.ts
More file actions
522 lines (460 loc) · 14.2 KB
/
bgrun.ts
File metadata and controls
522 lines (460 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/**
* Background Task Runner Extension
*
* Run and manage background tasks using tmux. Each project gets a single tmux
* session with one window per task. Window names are auto-derived from commands.
*
* Commands:
* /bgrun <command> — start a background task
* /bgtasks — open task manager dialog (list, view output, kill)
*
* Tool:
* bgrun — LLM can start/list/capture/kill background tasks
*
* Emits `bgrun:stats` event for status-bar consumption.
* Tasks are session-scoped: killed on /new. On shutdown, a timed confirm
* (5s, default=No) asks whether to kill remaining tasks.
*/
import { StringEnum } from "@mariozechner/pi-ai";
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
import { Key, matchesKey, Text, truncateToWidth } from "@mariozechner/pi-tui";
import { Type } from "typebox";
import {
type TmuxConfig,
buildTmuxConfig,
capturePane,
createWindow,
deriveTaskName,
formatDuration,
killSession,
killWindow,
listWindowNames,
listWindowState,
uniqueName,
} from "../lib/tmux.ts";
// Re-export for backward compatibility (tests, status-bar, etc.)
export { COMPOUND_SUBCOMMANDS, deriveTaskName, formatDuration, sanitizeName, uniqueName } from "../lib/tmux.ts";
/** Tracked background task metadata. */
export interface BgTask {
name: string;
command: string;
startedAt: number;
/** Whether this task is a subagent (shown with [AGENT] badge). */
isAgent?: boolean;
}
export default function bgrunExtension(pi: ExtensionAPI) {
const tasks = new Map<string, BgTask>();
let config: TmuxConfig = { socketPath: "", sessionName: "" };
/** Bind exec for convenience. */
const exec = pi.exec.bind(pi);
/** Resolve tmux config from cwd. */
function initTmux(cwd: string): void {
config = buildTmuxConfig(cwd, "bgrun");
}
/** Emit stats event for status-bar. */
function emitStats(): void {
pi.events.emit("bgrun:stats", { running: tasks.size });
}
/** Remove tasks whose tmux windows no longer exist or whose panes have exited. */
async function pruneDead(): Promise<void> {
const windowState = await listWindowState(exec, config);
let pruned = false;
for (const name of tasks.keys()) {
if (!windowState.has(name)) {
tasks.delete(name);
pruned = true;
} else if (windowState.get(name)) {
await killWindow(exec, config, name);
tasks.delete(name);
pruned = true;
}
}
if (pruned) emitStats();
}
/** Start a new background task. Returns the task metadata. */
async function startTask(command: string): Promise<BgTask> {
const rawName = deriveTaskName(command);
const name = uniqueName(rawName, tasks);
await createWindow(exec, config, name, command);
const task: BgTask = { name, command, startedAt: Date.now() };
tasks.set(name, task);
emitStats();
return task;
}
/** Capture last N lines from a task's tmux pane. */
async function captureTaskOutput(taskName: string, lines = 200): Promise<string> {
return capturePane(exec, config, taskName, lines);
}
/** Kill a specific task's tmux window. */
async function killTask(taskName: string): Promise<boolean> {
await killWindow(exec, config, taskName);
const removed = tasks.delete(taskName);
emitStats();
return removed;
}
/** Kill all tasks and the tmux session. */
async function killAll(): Promise<void> {
await killSession(exec, config);
tasks.clear();
emitStats();
}
/** Rebuild task list by scanning existing tmux windows. */
async function syncFromTmux(): Promise<void> {
tasks.clear();
const names = await listWindowNames(exec, config);
const now = Date.now();
for (const name of names) {
const isAgent = name.startsWith("agent:");
tasks.set(name, { name, command: "(reconnected)", startedAt: now, isAgent });
}
emitStats();
}
// --- Session lifecycle ---
pi.on("session_start", async (event, ctx) => {
// Fresh session (new conversation) — drop any background tasks
// that were attached to the previous session.
if (event.reason === "new") {
await killAll();
}
initTmux(ctx.cwd);
await syncFromTmux();
});
pi.on("session_shutdown", async (_event, ctx) => {
if (tasks.size === 0) return;
if (!ctx.hasUI) {
await killAll();
return;
}
const confirmed = await ctx.ui.confirm(
"Background Tasks",
`Kill ${tasks.size} background task(s)?`,
{ timeout: 5000 },
);
if (confirmed) {
await killAll();
}
});
// --- /bgrun command ---
pi.registerCommand("bgrun", {
description: "Start a background task: /bgrun <command>",
handler: async (args, ctx) => {
const command = args?.trim();
if (!command) {
ctx.ui.notify("Usage: /bgrun <command>", "warning");
return;
}
const task = await startTask(command);
ctx.ui.notify(
`Started: ${task.name}\n` +
`Monitor: tmux -S "${config.socketPath}" attach -t ${config.sessionName}:${task.name}`,
"info",
);
},
});
// --- /bgtasks command (task manager dialog) ---
pi.registerCommand("bgtasks", {
description: "Open background task manager",
handler: async (_args, ctx) => {
await pruneDead();
if (tasks.size === 0) {
ctx.ui.notify("No background tasks running", "info");
return;
}
await showTaskManager(ctx);
},
});
/** Show the interactive task manager TUI. */
async function showTaskManager(ctx: ExtensionContext): Promise<void> {
type View = "list" | "detail";
await ctx.ui.custom<void>((tui, theme, _kb, done) => {
let view: View = "list";
let selectedIndex = 0;
let capturedOutput = "";
let capturedTaskName = "";
let scrollOffset = 0;
let cachedLines: string[] | undefined;
function taskList(): BgTask[] {
return [...tasks.values()];
}
function refresh(): void {
cachedLines = undefined;
tui.requestRender();
}
function handleInput(data: string): void {
if (view === "detail") {
handleDetailInput(data);
} else {
handleListInput(data);
}
}
function handleListInput(data: string): void {
const list = taskList();
if (matchesKey(data, Key.escape) || data === "q") {
done(undefined);
return;
}
if (matchesKey(data, Key.up) && selectedIndex > 0) {
selectedIndex--;
refresh();
return;
}
if (matchesKey(data, Key.down) && selectedIndex < list.length - 1) {
selectedIndex++;
refresh();
return;
}
if (matchesKey(data, Key.enter)) {
const task = list[selectedIndex];
if (task) {
capturedTaskName = task.name;
captureTaskOutput(task.name, 200).then((out) => {
capturedOutput = out;
scrollOffset = 0;
view = "detail";
refresh();
});
}
return;
}
if (data === "k" || matchesKey(data, Key.delete)) {
const task = list[selectedIndex];
if (task) {
killTask(task.name).then(() => {
if (selectedIndex >= taskList().length) {
selectedIndex = Math.max(0, taskList().length - 1);
}
if (taskList().length === 0) {
done(undefined);
} else {
refresh();
}
});
}
return;
}
if (data === "K") {
killAll().then(() => done(undefined));
return;
}
}
function handleDetailInput(data: string): void {
if (matchesKey(data, Key.escape) || data === "q") {
view = "list";
refresh();
return;
}
if (matchesKey(data, Key.up)) {
scrollOffset = Math.max(0, scrollOffset - 1);
refresh();
return;
}
if (matchesKey(data, Key.down)) {
scrollOffset++;
refresh();
return;
}
if (data === "r") {
captureTaskOutput(capturedTaskName, 200).then((out) => {
capturedOutput = out;
refresh();
});
return;
}
}
function renderList(width: number): string[] {
const lines: string[] = [];
const add = (s: string) => lines.push(truncateToWidth(s, width));
const list = taskList();
add(theme.fg("accent", "─".repeat(width)));
add(theme.fg("accent", theme.bold(" ⚙ Background Tasks")) +
theme.fg("dim", ` (${list.length})`));
add("");
for (let i = 0; i < list.length; i++) {
const task = list[i]!;
const selected = i === selectedIndex;
const prefix = selected ? theme.fg("accent", " ▸ ") : " ";
const elapsed = formatDuration(Date.now() - task.startedAt);
const icon = theme.fg("success", "●");
const badge = task.isAgent ? theme.fg("warning", " [AGENT]") : "";
const nameStr = selected
? theme.fg("accent", task.name) + badge
: theme.fg("text", task.name) + badge;
const cmdStr = theme.fg("muted", task.command);
const timeStr = theme.fg("dim", elapsed);
add(`${prefix}${icon} ${nameStr} ${timeStr}`);
add(` ${cmdStr}`);
if (i < list.length - 1) add("");
}
add("");
add(theme.fg("dim", " ↑↓ navigate • Enter view output • k kill • K kill all • q/Esc close"));
add(theme.fg("accent", "─".repeat(width)));
return lines;
}
function renderDetail(width: number): string[] {
const lines: string[] = [];
const add = (s: string) => lines.push(truncateToWidth(s, width));
add(theme.fg("accent", "─".repeat(width)));
add(theme.fg("accent", theme.bold(` 📋 ${capturedTaskName}`)) +
theme.fg("dim", " (output)"));
add(theme.fg("accent", "─".repeat(width)));
const outputLines = capturedOutput.split("\n");
const maxVisible = Math.max(5, (tui as any).height
? (tui as any).height - 6
: 30);
const clampedOffset = Math.min(
scrollOffset,
Math.max(0, outputLines.length - maxVisible),
);
const visible = outputLines.slice(clampedOffset, clampedOffset + maxVisible);
for (const line of visible) {
add(` ${theme.fg("text", line)}`);
}
if (outputLines.length > maxVisible) {
add("");
add(theme.fg("dim",
` Lines ${clampedOffset + 1}-${clampedOffset + visible.length} of ${outputLines.length}`));
}
add("");
add(theme.fg("dim", " ↑↓ scroll • r refresh • q/Esc back"));
add(theme.fg("accent", "─".repeat(width)));
return lines;
}
function render(width: number): string[] {
if (cachedLines) return cachedLines;
cachedLines = view === "list"
? renderList(width)
: renderDetail(width);
return cachedLines;
}
return {
render,
invalidate: () => { cachedLines = undefined; },
handleInput,
};
});
}
// --- bgrun tool (for LLM) ---
const BgRunParams = Type.Object({
action: StringEnum(["start", "list", "capture", "kill"] as const, {
description: "Action to perform on background tasks",
}),
command: Type.Optional(Type.String({
description: "Shell command to run (required for 'start')",
})),
task_id: Type.Optional(Type.String({
description: "Task name/id (required for 'capture' and 'kill')",
})),
});
pi.registerTool({
name: "bgrun",
label: "Background Run",
description:
"Run and manage background tasks via tmux. " +
"Use 'start' to launch a command, 'list' to see all tasks, " +
"'capture' to get a task's output, 'kill' to stop a task. " +
"Use bgrun (not bash) for long-running or background processes: dev servers, watchers, builds, test suites — anything that doesn't terminate quickly.",
parameters: BgRunParams,
async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
switch (params.action) {
case "start": {
if (!params.command) {
throw new Error("'command' is required for 'start' action");
}
const task = await startTask(params.command);
const monitorCmd = `tmux -S "${config.socketPath}" attach -t ${config.sessionName}:${task.name}`;
return {
content: [{
type: "text" as const,
text: `Started background task: ${task.name}\nCommand: ${task.command}\nMonitor: ${monitorCmd}`,
}],
details: { task, monitorCmd },
};
}
case "list": {
await pruneDead();
const list = [...tasks.values()].map((t) => ({
name: t.name,
command: t.command,
uptime: formatDuration(Date.now() - t.startedAt),
}));
return {
content: [{
type: "text" as const,
text: list.length === 0
? "No background tasks running"
: `${list.length} task(s):\n${list.map((t) => ` ${t.name}: ${t.command} (${t.uptime})`).join("\n")}`,
}],
details: { tasks: list },
};
}
case "capture": {
if (!params.task_id) {
throw new Error("'task_id' is required for 'capture' action");
}
if (!tasks.has(params.task_id)) {
throw new Error(`Task '${params.task_id}' not found`);
}
const output = await captureTaskOutput(params.task_id);
return {
content: [{
type: "text" as const,
text: `Output of ${params.task_id}:\n${output}`,
}],
details: { task_id: params.task_id, output },
};
}
case "kill": {
if (!params.task_id) {
throw new Error("'task_id' is required for 'kill' action");
}
const existed = await killTask(params.task_id);
return {
content: [{
type: "text" as const,
text: existed
? `Killed task: ${params.task_id}`
: `Task '${params.task_id}' not found (may have already exited)`,
}],
details: { task_id: params.task_id, existed },
};
}
default:
throw new Error(`Unknown action: ${params.action}`);
}
},
renderCall(args, theme) {
let text = theme.fg("toolTitle", theme.bold("bgrun "));
text += theme.fg("accent", args.action ?? "");
if (args.command) {
text += " " + theme.fg("muted", args.command);
}
if (args.task_id) {
text += " " + theme.fg("dim", `[${args.task_id}]`);
}
return new Text(text, 0, 0);
},
renderResult(result, _options, theme) {
const details = result.details as Record<string, unknown> | undefined;
const text = result.content[0];
const raw = text?.type === "text" ? text.text : "";
if (details?.task) {
const task = details.task as BgTask;
return new Text(
theme.fg("success", "✓ ") +
theme.fg("accent", task.name) +
theme.fg("dim", ` — ${task.command}`),
0, 0,
);
}
if (details?.output) {
const preview = String(details.output).split("\n").slice(-5).join("\n");
return new Text(
theme.fg("success", `✓ ${details.task_id}\n`) +
theme.fg("dim", preview),
0, 0,
);
}
return new Text(theme.fg("success", "✓ ") + theme.fg("text", raw), 0, 0);
},
});
}