pi-famulus 0.2.3 → 0.2.4
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 +2 -2
- package/package.json +7 -7
- package/src/config.ts +4 -0
- package/src/format.ts +33 -5
- package/src/idle-turn-guard.ts +118 -0
- package/src/index.ts +7 -0
- package/src/intercom/intercom.ts +4 -47
- package/src/intercom/surface.ts +57 -0
- package/src/manager-client.ts +17 -3
- package/src/monitor-batching.ts +33 -9
- package/src/monitor.ts +175 -16
- package/src/notify.ts +42 -5
- package/src/prompts.generated.ts +25 -10
- package/src/task-tools.ts +34 -15
- package/src/tui/message-renderers.ts +3 -3
- package/src/wake.ts +14 -3
package/src/task-tools.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { Type } from "typebox";
|
|
5
5
|
import { realClock, type Clock } from "./clock";
|
|
6
6
|
import type { ExtensionContext, ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
7
|
-
import type { ManagerClient, TaskRecord } from "./manager-client";
|
|
7
|
+
import type { ManagerClient, StopResult, TaskRecord } from "./manager-client";
|
|
8
8
|
import { fnv1a64 } from "./config";
|
|
9
9
|
import { fill, PROMPTS } from "./prompts.generated";
|
|
10
10
|
import {
|
|
@@ -30,7 +30,7 @@ export interface TaskToolsDeps {
|
|
|
30
30
|
/** task_stop on a monitor: its leftover lines and exit should not wake the model. */
|
|
31
31
|
noteStopRequested?: (taskId: string) => (() => void) | void;
|
|
32
32
|
/** Live monitor remaining timeout and last output, from the in-session registry. */
|
|
33
|
-
getMonitorLive?: () => Iterable<
|
|
33
|
+
getMonitorLive?: () => Iterable<MonitorLive>;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
function requireClient(deps: TaskToolsDeps): Promise<ManagerClient> {
|
|
@@ -73,14 +73,22 @@ function formatDuration(startedAt: number, endedAt: number | null, now: number):
|
|
|
73
73
|
return `${Math.floor(ms / 60_000)}m${Math.round((ms % 60_000) / 1000)}s`;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
interface MonitorLive {
|
|
77
|
+
taskId: string;
|
|
78
|
+
remainingMs: number | null;
|
|
79
|
+
persistent: boolean;
|
|
80
|
+
lastOutputAt?: number;
|
|
81
|
+
wakeOn?: string;
|
|
82
|
+
stallAfterMs?: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function formatMonitorLive(live: MonitorLive, now: number): string {
|
|
80
86
|
const timeout = live.persistent ? "persistent" : `timeout in ${formatAge(now, now + (live.remainingMs ?? 0), now)}`;
|
|
81
87
|
const last =
|
|
82
88
|
live.lastOutputAt === undefined ? "no output yet" : `last output ${formatAge(live.lastOutputAt, undefined, now)} ago`;
|
|
83
|
-
|
|
89
|
+
const filter = live.wakeOn === undefined ? "" : `; wakes only on /${live.wakeOn}/`;
|
|
90
|
+
const stall = live.stallAfterMs === undefined ? "" : `; stall notice after ${formatAge(now, now + live.stallAfterMs, now)}`;
|
|
91
|
+
return ` ${timeout}; ${last}${filter}${stall}`;
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
function formatTaskLine(task: TaskRecord, now: number): string {
|
|
@@ -126,7 +134,7 @@ export function createTaskListTool(
|
|
|
126
134
|
const lines: string[] = [];
|
|
127
135
|
const seen = new Set<string>();
|
|
128
136
|
let running = false;
|
|
129
|
-
const monitorLive = new Map<string,
|
|
137
|
+
const monitorLive = new Map<string, MonitorLive>();
|
|
130
138
|
for (const live of deps.getMonitorLive?.() ?? []) monitorLive.set(live.taskId, live);
|
|
131
139
|
|
|
132
140
|
const pushItem = (item: WorkItem) => {
|
|
@@ -312,6 +320,12 @@ const taskStopParameters = Type.Object({
|
|
|
312
320
|
task_id: Type.String({ description: PROMPTS["tools.task_stop.param.task_id"] }),
|
|
313
321
|
});
|
|
314
322
|
|
|
323
|
+
function endedOutcome(ended: NonNullable<StopResult["alreadyEnded"]>): string {
|
|
324
|
+
if (ended.signal) return `${ended.status}, killed by ${ended.signal}`;
|
|
325
|
+
if (ended.exitCode !== null) return `${ended.status}, exit code ${ended.exitCode}`;
|
|
326
|
+
return ended.status;
|
|
327
|
+
}
|
|
328
|
+
|
|
315
329
|
export function createTaskStopTool(
|
|
316
330
|
deps: TaskToolsDeps,
|
|
317
331
|
): ToolDefinition<typeof taskStopParameters, { task_id: string; stopped: boolean }> {
|
|
@@ -335,8 +349,9 @@ export function createTaskStopTool(
|
|
|
335
349
|
}
|
|
336
350
|
const client = await requireClient(deps);
|
|
337
351
|
const undoNote = deps.noteStopRequested?.(params.task_id);
|
|
352
|
+
let result: StopResult | undefined;
|
|
338
353
|
try {
|
|
339
|
-
await client.stop(params.task_id, "tool");
|
|
354
|
+
result = await client.stop(params.task_id, "tool");
|
|
340
355
|
} catch (err) {
|
|
341
356
|
undoNote?.();
|
|
342
357
|
throw err;
|
|
@@ -344,13 +359,17 @@ export function createTaskStopTool(
|
|
|
344
359
|
// Stopping a task that already ended emits no new exit event.
|
|
345
360
|
await deps.syncWithManager?.();
|
|
346
361
|
}
|
|
362
|
+
const ended = result?.alreadyEnded;
|
|
363
|
+
if (ended) {
|
|
364
|
+
// Nothing was stopped, so its exit is not a stop the model asked for.
|
|
365
|
+
undoNote?.();
|
|
366
|
+
return {
|
|
367
|
+
content: [{ type: "text", text: fill("tools.task_stop.result.already-ended", { taskId: params.task_id, outcome: endedOutcome(ended) }) }],
|
|
368
|
+
details: { task_id: params.task_id, stopped: false },
|
|
369
|
+
};
|
|
370
|
+
}
|
|
347
371
|
return {
|
|
348
|
-
content: [
|
|
349
|
-
{
|
|
350
|
-
type: "text",
|
|
351
|
-
text: `Task ${params.task_id} stopped (SIGTERM; SIGKILL after 2s grace if needed).`,
|
|
352
|
-
},
|
|
353
|
-
],
|
|
372
|
+
content: [{ type: "text", text: fill("tools.task_stop.result.stopped", { taskId: params.task_id }) }],
|
|
354
373
|
details: { task_id: params.task_id, stopped: true },
|
|
355
374
|
};
|
|
356
375
|
},
|
|
@@ -119,14 +119,14 @@ function collapsedText(details: FamulusWake, theme: Theme): string {
|
|
|
119
119
|
return `${theme.fg("muted", "↑")} ${theme.fg("muted", "supervisor update")} ${details.message.slice(0, 100)}`;
|
|
120
120
|
case "monitor-session-end": {
|
|
121
121
|
const n = details.ids.length || details.monitors.length;
|
|
122
|
-
return `${theme.fg("warning", "✗")} ${theme.fg("muted", "session monitors")} ${n}
|
|
122
|
+
return `${theme.fg("warning", "✗")} ${theme.fg("muted", "session monitors")} ${n} teardown · ${details.reason}`;
|
|
123
123
|
}
|
|
124
124
|
case "running-monitors":
|
|
125
125
|
return `${theme.fg("accent", "›")} ${theme.fg("muted", "running")} ${details.monitors.length} monitor${details.monitors.length === 1 ? "" : "s"}`;
|
|
126
126
|
case "monitor-terminal": {
|
|
127
127
|
const n = details.ids.length || details.monitors.length;
|
|
128
128
|
const statuses = countStatuses(details.monitors.map((monitor) => monitor.status));
|
|
129
|
-
return `${theme.fg("warning", "✗")} ${theme.fg("muted", "monitors")} ${n}
|
|
129
|
+
return `${theme.fg("warning", "✗")} ${theme.fg("muted", "monitors")} ${n} lifecycle updates · ${statuses}`;
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
}
|
|
@@ -208,7 +208,7 @@ export function expandedWakeText(details: FamulusWake | undefined, content: stri
|
|
|
208
208
|
return [`Update from ${details.name} (${details.from})`, details.message].join("\n");
|
|
209
209
|
case "monitor-session-end":
|
|
210
210
|
return [
|
|
211
|
-
`Session
|
|
211
|
+
`Session monitor teardown (${details.reason}); process exits are not confirmed by this notice.`,
|
|
212
212
|
...details.monitors.map((monitor) =>
|
|
213
213
|
`${monitor.id} "${monitor.description}" elapsed ${formatDuration(monitor.elapsedMs)}; original output: task_output({task_id: "${monitor.id}"}) ${monitor.outputPath} while the log is retained`,
|
|
214
214
|
),
|
package/src/wake.ts
CHANGED
|
@@ -30,6 +30,7 @@ export interface TaskWake {
|
|
|
30
30
|
exitCode: number | null;
|
|
31
31
|
/** Signal name ("SIGTERM" | "SIGKILL"). Omitted when the exit had no signal. */
|
|
32
32
|
signal?: string;
|
|
33
|
+
endReason?: string;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
export type WakeChildStatus = "pending" | "running" | "completed" | "failed" | "interrupted";
|
|
@@ -105,6 +106,13 @@ export interface MonitorOutputInfo {
|
|
|
105
106
|
omittedChars?: number;
|
|
106
107
|
}
|
|
107
108
|
|
|
109
|
+
export interface MonitorExitFacts {
|
|
110
|
+
endReason?: string;
|
|
111
|
+
signal?: string;
|
|
112
|
+
exitCode?: number | null;
|
|
113
|
+
stopRequested?: boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
108
116
|
export type FamulusWake = FamulusWakeBody & WakeTiming;
|
|
109
117
|
|
|
110
118
|
type FamulusWakeBody =
|
|
@@ -118,6 +126,8 @@ type FamulusWakeBody =
|
|
|
118
126
|
eventCount?: number;
|
|
119
127
|
droppedLines?: number;
|
|
120
128
|
output?: MonitorOutputInfo;
|
|
129
|
+
exitFacts?: MonitorExitFacts;
|
|
130
|
+
exitContext?: string;
|
|
121
131
|
}
|
|
122
132
|
| {
|
|
123
133
|
kind: "subagent-handover";
|
|
@@ -175,7 +185,7 @@ type FamulusWakeBody =
|
|
|
175
185
|
}
|
|
176
186
|
| {
|
|
177
187
|
kind: "running-monitors";
|
|
178
|
-
monitors: { id: string; description: string; remaining: string }[];
|
|
188
|
+
monitors: { id: string; description: string; remaining: string; wakeOn?: string }[];
|
|
179
189
|
}
|
|
180
190
|
| {
|
|
181
191
|
kind: "monitor-terminal";
|
|
@@ -188,6 +198,7 @@ type FamulusWakeBody =
|
|
|
188
198
|
detail: string;
|
|
189
199
|
droppedLines?: number;
|
|
190
200
|
output?: MonitorOutputInfo;
|
|
201
|
+
exitFacts?: MonitorExitFacts;
|
|
191
202
|
}>;
|
|
192
203
|
};
|
|
193
204
|
|
|
@@ -546,7 +557,7 @@ function renderRunningMonitors(details: Extract<FamulusWake, { kind: "running-mo
|
|
|
546
557
|
];
|
|
547
558
|
for (const monitor of details.monitors) {
|
|
548
559
|
parts.push(
|
|
549
|
-
` <monitor id="${escapeXmlAttr(monitor.id)}" remaining="${escapeXmlAttr(monitor.remaining)}">${escapeXml(monitor.description)}</monitor>`,
|
|
560
|
+
` <monitor id="${escapeXmlAttr(monitor.id)}" remaining="${escapeXmlAttr(monitor.remaining)}"${monitor.wakeOn ? ` wake-on="${escapeXmlAttr(monitor.wakeOn)}"` : ""}>${escapeXml(monitor.description)}</monitor>`,
|
|
550
561
|
);
|
|
551
562
|
}
|
|
552
563
|
parts.push("</pi-famulus-wake>");
|
|
@@ -592,7 +603,7 @@ function renderMonitorTerminal(details: Extract<FamulusWake, { kind: "monitor-te
|
|
|
592
603
|
function renderMonitorSessionEnd(details: Extract<FamulusWake, { kind: "monitor-session-end" }>): string {
|
|
593
604
|
const parts = [
|
|
594
605
|
`<pi-famulus-wake kind="monitor-session-end" reason="${escapeXmlAttr(details.reason)}">`,
|
|
595
|
-
` <summary>${escapeXml(`
|
|
606
|
+
` <summary>${escapeXml(`Session monitor teardown (${details.reason}); process exits are not confirmed by this notice.`)}</summary>`,
|
|
596
607
|
];
|
|
597
608
|
for (const monitor of details.monitors) {
|
|
598
609
|
const pointer = `task_output({task_id: "${monitor.id}"}) ${monitor.outputPath} while the log is retained`;
|