pi-famulus 0.2.5 → 0.2.7
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/package.json +7 -7
- package/src/manager-client.ts +1 -1
- package/src/monitor.ts +15 -0
- package/src/subagent/hosted-session.ts +167 -28
- package/src/subagent/tool.ts +37 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-famulus",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Keep your main agent's context clean: subagents on cheaper models, long bash in the background, and monitors that wake the agent. One Rust daemon runs the shells.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
"pi-famulus": "./bin/pi-famulus.js"
|
|
55
55
|
},
|
|
56
56
|
"optionalDependencies": {
|
|
57
|
-
"pi-famulus-linux-x64": "0.2.
|
|
58
|
-
"pi-famulus-linux-arm64": "0.2.
|
|
59
|
-
"pi-famulus-darwin-x64": "0.2.
|
|
60
|
-
"pi-famulus-darwin-arm64": "0.2.
|
|
61
|
-
"pi-famulus-win-x64": "0.2.
|
|
62
|
-
"pi-famulus-win-arm64": "0.2.
|
|
57
|
+
"pi-famulus-linux-x64": "0.2.7",
|
|
58
|
+
"pi-famulus-linux-arm64": "0.2.7",
|
|
59
|
+
"pi-famulus-darwin-x64": "0.2.7",
|
|
60
|
+
"pi-famulus-darwin-arm64": "0.2.7",
|
|
61
|
+
"pi-famulus-win-x64": "0.2.7",
|
|
62
|
+
"pi-famulus-win-arm64": "0.2.7"
|
|
63
63
|
},
|
|
64
64
|
"pi": {
|
|
65
65
|
"extensions": [
|
package/src/manager-client.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { famulusPaths } from "./config";
|
|
|
18
18
|
import { realClock, type Clock, type ClockTimer } from "./clock";
|
|
19
19
|
|
|
20
20
|
const MAX_FRAME_BYTES = 4 * 1024 * 1024; // 4 MiB (§3.3)
|
|
21
|
-
const EXTENSION_VERSION = "0.2.
|
|
21
|
+
const EXTENSION_VERSION = "0.2.7";
|
|
22
22
|
export { EXTENSION_VERSION };
|
|
23
23
|
// 6: hosted subagents. 7: the extension's messaging bridge (`intercom`
|
|
24
24
|
// request plus `intercom`/`intercom_reset` events).
|
package/src/monitor.ts
CHANGED
|
@@ -375,6 +375,21 @@ export class MonitorRegistry {
|
|
|
375
375
|
}
|
|
376
376
|
} else if (deliver.byteLength > 0) {
|
|
377
377
|
// Legacy manager or unavailable replay metadata: fail open, never hash in JS.
|
|
378
|
+
// Raw framing accounts for every COMPLETE line through `entry.cursor`. The
|
|
379
|
+
// manager re-sends records at a cursor it already wrote (`replay_at` in
|
|
380
|
+
// daemon.rs), so a line delivered raw must count as seen or a burst that
|
|
381
|
+
// arrived unframed (post-upgrade MonitorState rebuild) repeats its last
|
|
382
|
+
// line. Complete lines only: a trailing fragment is still framed by the
|
|
383
|
+
// manager's own finish/EOF line record at the same cursor, and under
|
|
384
|
+
// wake_on that fragment has not been admitted yet.
|
|
385
|
+
const lastNewline = deliver.lastIndexOf(0x0a);
|
|
386
|
+
if (lastNewline >= 0) {
|
|
387
|
+
const completed = entry.cursor - deliver.byteLength + lastNewline + 1;
|
|
388
|
+
if (completed > entry.recordCursor) {
|
|
389
|
+
entry.recordCursor = completed;
|
|
390
|
+
entry.lastRecordKind = "line";
|
|
391
|
+
}
|
|
392
|
+
}
|
|
378
393
|
entry.output.flush();
|
|
379
394
|
const cursorStart = Math.max(previousCursor, entry.cursor - deliver.byteLength);
|
|
380
395
|
const text = deliver.toString("utf8");
|
|
@@ -34,6 +34,33 @@ interface HostingView {
|
|
|
34
34
|
sessionId?: string;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* One prompt whose turn the parent supervises.
|
|
39
|
+
*
|
|
40
|
+
* A prompt is not the same as its RPC acknowledgement: the daemon's cold
|
|
41
|
+
* `get_state` handshake for a respawned parked child reports `idle` before the
|
|
42
|
+
* prompt has run, and events may arrive before the RPC response. Completion is
|
|
43
|
+
* therefore fenced by this turn's own `agent_start`/`agent_settled` lifecycle,
|
|
44
|
+
* never by transport readiness.
|
|
45
|
+
*/
|
|
46
|
+
interface ActiveTurn {
|
|
47
|
+
/** Resolves when this prompt's run lifecycle ends (or it was handled). */
|
|
48
|
+
done: Promise<void>;
|
|
49
|
+
resolve: () => void;
|
|
50
|
+
/** The daemon's RPC response for the dispatch was processed. */
|
|
51
|
+
accepted: boolean;
|
|
52
|
+
/** `disposition` from that response (`started` | `queued` | `handled`). */
|
|
53
|
+
disposition: string | undefined;
|
|
54
|
+
/** A run for this turn is active (observed `agent_start`, or joined live). */
|
|
55
|
+
sawStart: boolean;
|
|
56
|
+
/** An `agent_settled` was observed and no newer run started since. */
|
|
57
|
+
settled: boolean;
|
|
58
|
+
/** The hosted process reported failure before the turn ended. */
|
|
59
|
+
processFailed: boolean;
|
|
60
|
+
/** The prompt was steered into a run already in flight (out-of-band work). */
|
|
61
|
+
joinedRun: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
37
64
|
export function createHostedSessionFn(deps: HostedSessionDeps): CreateSessionFn {
|
|
38
65
|
return async (req) => {
|
|
39
66
|
const client = deps.getClient();
|
|
@@ -104,7 +131,8 @@ class HostedRpcSession implements ChildSessionAdapter {
|
|
|
104
131
|
private lastFailure: { stopReason: "error" | "aborted"; errorMessage?: string } | undefined;
|
|
105
132
|
private turns: { role: string; text: string }[] = [];
|
|
106
133
|
private usage: ChildTokenUsage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
|
|
107
|
-
|
|
134
|
+
/** The prompt turn awaiting its own completion; null between turns. */
|
|
135
|
+
private activeTurn: ActiveTurn | null = null;
|
|
108
136
|
private hostingView: HostingView;
|
|
109
137
|
private disposed = false;
|
|
110
138
|
|
|
@@ -131,10 +159,28 @@ class HostedRpcSession implements ChildSessionAdapter {
|
|
|
131
159
|
}
|
|
132
160
|
|
|
133
161
|
async prompt(text: string): Promise<void> {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
162
|
+
// A run already in flight is not ours: a user/UI attach or a sibling drove
|
|
163
|
+
// it. Steering delivers this prompt into that run, and its settle is this
|
|
164
|
+
// turn's completion — returning early would report the child finished while
|
|
165
|
+
// the run is still executing.
|
|
166
|
+
const joinedRun = this.streaming;
|
|
167
|
+
// A new prompt supersedes any turn still armed (defensive: the runner
|
|
168
|
+
// serializes generations, so this should not happen).
|
|
169
|
+
if (this.activeTurn) this.finishTurn(this.activeTurn);
|
|
170
|
+
const turn = this.beginTurn(joinedRun);
|
|
171
|
+
try {
|
|
172
|
+
const response = await this.client.agentInput(
|
|
173
|
+
this.childId,
|
|
174
|
+
joinedRun ? "prompt_steer" : "prompt",
|
|
175
|
+
text,
|
|
176
|
+
"parent",
|
|
177
|
+
);
|
|
178
|
+
this.acceptPrompt(turn, response);
|
|
179
|
+
} catch (err) {
|
|
180
|
+
this.finishTurn(turn);
|
|
181
|
+
throw err;
|
|
182
|
+
}
|
|
183
|
+
await turn.done;
|
|
138
184
|
}
|
|
139
185
|
|
|
140
186
|
async steer(text: string): Promise<void> {
|
|
@@ -146,10 +192,15 @@ class HostedRpcSession implements ChildSessionAdapter {
|
|
|
146
192
|
}
|
|
147
193
|
|
|
148
194
|
async abort(): Promise<void> {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
195
|
+
try {
|
|
196
|
+
await this.client.agentInput(this.childId, "abort", "", "parent");
|
|
197
|
+
} finally {
|
|
198
|
+
// The abort response is sent after the child became idle, so the turn can
|
|
199
|
+
// end now; its `agent_settled` is already drained or no longer relevant.
|
|
200
|
+
// A failed abort (dead child) must not leave the turn supervised forever.
|
|
201
|
+
this.streaming = false;
|
|
202
|
+
if (this.activeTurn) this.finishTurn(this.activeTurn);
|
|
203
|
+
}
|
|
153
204
|
}
|
|
154
205
|
|
|
155
206
|
async park(): Promise<void> {
|
|
@@ -158,8 +209,9 @@ class HostedRpcSession implements ChildSessionAdapter {
|
|
|
158
209
|
}
|
|
159
210
|
|
|
160
211
|
async waitForIdle(): Promise<void> {
|
|
161
|
-
|
|
162
|
-
|
|
212
|
+
const turn = this.activeTurn;
|
|
213
|
+
if (!turn || !this.streaming) return;
|
|
214
|
+
await turn.done;
|
|
163
215
|
}
|
|
164
216
|
|
|
165
217
|
getLastAssistantText(): string | undefined {
|
|
@@ -191,14 +243,71 @@ class HostedRpcSession implements ChildSessionAdapter {
|
|
|
191
243
|
if (this.disposed) return;
|
|
192
244
|
this.disposed = true;
|
|
193
245
|
this.unsubscribe();
|
|
194
|
-
this.
|
|
195
|
-
this.settle = null;
|
|
246
|
+
if (this.activeTurn) this.finishTurn(this.activeTurn);
|
|
196
247
|
}
|
|
197
248
|
|
|
198
|
-
private
|
|
199
|
-
|
|
200
|
-
|
|
249
|
+
private beginTurn(joinedRun: boolean): ActiveTurn {
|
|
250
|
+
let resolve!: () => void;
|
|
251
|
+
const done = new Promise<void>((res) => {
|
|
252
|
+
resolve = res;
|
|
201
253
|
});
|
|
254
|
+
// Failure/result state is turn-scoped: the previous turn's abort error must
|
|
255
|
+
// not describe this one.
|
|
256
|
+
this.lastFailure = undefined;
|
|
257
|
+
const turn: ActiveTurn = {
|
|
258
|
+
done,
|
|
259
|
+
resolve,
|
|
260
|
+
accepted: false,
|
|
261
|
+
disposition: undefined,
|
|
262
|
+
sawStart: joinedRun,
|
|
263
|
+
settled: false,
|
|
264
|
+
processFailed: false,
|
|
265
|
+
joinedRun,
|
|
266
|
+
};
|
|
267
|
+
this.activeTurn = turn;
|
|
268
|
+
return turn;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Apply the prompt RPC's disposition, then wait or finish accordingly. */
|
|
272
|
+
private acceptPrompt(turn: ActiveTurn, response: Record<string, unknown>): void {
|
|
273
|
+
if (this.activeTurn !== turn) return;
|
|
274
|
+
turn.accepted = true;
|
|
275
|
+
turn.disposition = typeof response.disposition === "string" ? response.disposition : undefined;
|
|
276
|
+
if (turn.disposition === "handled" && !turn.joinedRun) {
|
|
277
|
+
// An extension command or input handler consumed the prompt: no run
|
|
278
|
+
// starts for it, so there is nothing to wait for. A handled prompt that
|
|
279
|
+
// was steered into a live run still waits for that run to end.
|
|
280
|
+
this.streaming = false;
|
|
281
|
+
this.finishTurn(turn);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (turn.disposition === "queued") {
|
|
285
|
+
// The prompt joined a run that was already streaming (our view said
|
|
286
|
+
// idle). That run's `agent_settled` is this turn's completion.
|
|
287
|
+
turn.sawStart = true;
|
|
288
|
+
}
|
|
289
|
+
if (turn.processFailed) {
|
|
290
|
+
this.lastFailure = { stopReason: "error", errorMessage: "hosted child process failed" };
|
|
291
|
+
this.finishTurn(turn);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
this.maybeFinishTurn(turn);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private maybeFinishTurn(turn: ActiveTurn): void {
|
|
298
|
+
if (this.activeTurn !== turn || !turn.accepted || !turn.settled) return;
|
|
299
|
+
// `started` (or a legacy response without a disposition) must have shown a
|
|
300
|
+
// run start after this turn was armed: a stale `agent_settled` from the
|
|
301
|
+
// previous generation is not this prompt's completion.
|
|
302
|
+
if (turn.disposition !== "queued" && !turn.sawStart) return;
|
|
303
|
+
this.finishTurn(turn);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private finishTurn(turn: ActiveTurn): void {
|
|
307
|
+
if (this.activeTurn !== turn) return;
|
|
308
|
+
this.activeTurn = null;
|
|
309
|
+
this.streaming = false;
|
|
310
|
+
turn.resolve();
|
|
202
311
|
}
|
|
203
312
|
|
|
204
313
|
private onManagerEvent(event: ManagerEvent & Record<string, unknown>): void {
|
|
@@ -213,13 +322,28 @@ class HostedRpcSession implements ChildSessionAdapter {
|
|
|
213
322
|
sessionId: typeof event.session_id === "string" ? event.session_id : this.hostingView.sessionId,
|
|
214
323
|
};
|
|
215
324
|
if (event.activity === "running") this.streaming = true;
|
|
216
|
-
if (event.activity === "idle"
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
325
|
+
if (event.activity === "idle") {
|
|
326
|
+
// Transport/process readiness, not turn completion: a respawned parked
|
|
327
|
+
// child reports idle from its cold get_state handshake before the
|
|
328
|
+
// resumed prompt has run. Completion comes from `agent_settled`.
|
|
329
|
+
this.streaming = false;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (event.activity === "failed") {
|
|
220
333
|
this.streaming = false;
|
|
221
|
-
|
|
222
|
-
|
|
334
|
+
// Explicit process failure is a real terminal signal even before the
|
|
335
|
+
// run starts (the daemon filters stale generations). Before the RPC
|
|
336
|
+
// response the failure is held on the turn and applied on acceptance,
|
|
337
|
+
// so an accepted prompt cannot wait forever for a settle that will
|
|
338
|
+
// never come.
|
|
339
|
+
const turn = this.activeTurn;
|
|
340
|
+
if (turn) {
|
|
341
|
+
turn.processFailed = true;
|
|
342
|
+
if (turn.accepted) {
|
|
343
|
+
this.lastFailure = { stopReason: "error", errorMessage: "hosted child process failed" };
|
|
344
|
+
this.finishTurn(turn);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
223
347
|
}
|
|
224
348
|
return;
|
|
225
349
|
}
|
|
@@ -239,13 +363,28 @@ class HostedRpcSession implements ChildSessionAdapter {
|
|
|
239
363
|
isError?: boolean;
|
|
240
364
|
};
|
|
241
365
|
const type = record.type ?? "";
|
|
242
|
-
if (type === "agent_start")
|
|
366
|
+
if (type === "agent_start") {
|
|
367
|
+
this.streaming = true;
|
|
368
|
+
const turn = this.activeTurn;
|
|
369
|
+
if (turn) {
|
|
370
|
+
turn.sawStart = true;
|
|
371
|
+
// A run that starts after a settle means the record was not terminal
|
|
372
|
+
// for this prompt (Pi is still working), so it cannot close the turn.
|
|
373
|
+
turn.settled = false;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
243
376
|
if (type === "agent_settled") {
|
|
244
377
|
this.streaming = false;
|
|
245
|
-
this.
|
|
246
|
-
|
|
378
|
+
const turn = this.activeTurn;
|
|
379
|
+
if (turn) {
|
|
380
|
+
turn.settled = true;
|
|
381
|
+
this.maybeFinishTurn(turn);
|
|
382
|
+
}
|
|
247
383
|
}
|
|
248
|
-
|
|
384
|
+
// Content bookkeeping belongs only to work the parent is supervising: a
|
|
385
|
+
// late record from a drained generation must not rewrite this turn.
|
|
386
|
+
const owned = this.streaming || this.activeTurn?.sawStart === true;
|
|
387
|
+
if (owned && type === "message_end" && record.message?.role === "assistant") {
|
|
249
388
|
const text = (record.message.content ?? []).map((part) => part.text ?? "").join("");
|
|
250
389
|
if (text) {
|
|
251
390
|
this.lastText = text;
|
|
@@ -271,11 +410,11 @@ class HostedRpcSession implements ChildSessionAdapter {
|
|
|
271
410
|
this.usage.cacheWrite += num(usage.cacheWrite);
|
|
272
411
|
}
|
|
273
412
|
}
|
|
274
|
-
if (type === "message_end" && record.message?.role === "user") {
|
|
413
|
+
if (owned && type === "message_end" && record.message?.role === "user") {
|
|
275
414
|
const text = (record.message.content ?? []).map((part) => part.text ?? "").join("");
|
|
276
415
|
if (text) this.turns.push({ role: "user", text });
|
|
277
416
|
}
|
|
278
|
-
if (type === "tool_execution_end") {
|
|
417
|
+
if (owned && type === "tool_execution_end") {
|
|
279
418
|
const name = record.toolName ?? "tool";
|
|
280
419
|
const text = record.args === undefined ? name : `${name} ${JSON.stringify(record.args)}`;
|
|
281
420
|
this.turns.push({
|
package/src/subagent/tool.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { realClock, type Clock, type ClockTimer } from "../clock";
|
|
|
21
21
|
|
|
22
22
|
import type { NotifyCenter } from "../notify";
|
|
23
23
|
import { statusGlyph, toolComponent } from "../tui/tool-component";
|
|
24
|
+
import { truncateToWidth } from "../tui/pi-tui-load";
|
|
24
25
|
import { scrubTty } from "../tui/conversation-view";
|
|
25
26
|
import type { WorkIndex } from "../work-index";
|
|
26
27
|
import { fill, PROMPTS } from "../prompts.generated";
|
|
@@ -732,6 +733,42 @@ export function createSubagentTool(
|
|
|
732
733
|
promptSnippet: PROMPTS["tools.subagent.snippet"],
|
|
733
734
|
promptGuidelines: [...PROMPTS["tools.subagent.rules"]],
|
|
734
735
|
parameters: subagentParameters,
|
|
736
|
+
renderCall(args, theme, context) {
|
|
737
|
+
const summary: string[] = [];
|
|
738
|
+
if (typeof args?.action === "string" && /^[a-z]+$/.test(args.action)) summary.push(args.action);
|
|
739
|
+
if (Array.isArray(args?.tasks)) summary.push(`${args.tasks.length} ${args.tasks.length === 1 ? "task" : "tasks"}`);
|
|
740
|
+
if (Array.isArray(args?.chain)) summary.push(`chain · ${args.chain.length} ${args.chain.length === 1 ? "step" : "steps"}`);
|
|
741
|
+
if (typeof args?.concurrency === "number") summary.push(`concurrency ${args.concurrency}`);
|
|
742
|
+
|
|
743
|
+
const state = context.state as { callHeader?: { piExpanded: boolean; expanded: boolean } };
|
|
744
|
+
// When Pi changes its expansion state (e.g. Ctrl+O), drop the local override.
|
|
745
|
+
if (state.callHeader?.piExpanded !== context.expanded) {
|
|
746
|
+
state.callHeader = { piExpanded: context.expanded, expanded: context.expanded };
|
|
747
|
+
}
|
|
748
|
+
const callHeader = state.callHeader;
|
|
749
|
+
const expanded = callHeader.expanded;
|
|
750
|
+
const title = `${theme.fg("muted", expanded ? "▾" : "▸")} ${theme.fg("toolTitle", theme.bold("subagent"))}`;
|
|
751
|
+
const header = [title, ...summary.map((part) => theme.fg("muted", part))].join(" · ");
|
|
752
|
+
const body = expanded ? toolComponent(
|
|
753
|
+
// JSON quotes C0 controls; escape DEL and C1 controls too before sending to the terminal.
|
|
754
|
+
JSON.stringify(args ?? {}, null, 2).replace(/[\u007f-\u009f]/g, (ch) => `\\u${ch.charCodeAt(0).toString(16).padStart(4, "0")}`)
|
|
755
|
+
.split("\n").map((line) => theme.fg("muted", line)),
|
|
756
|
+
) : undefined;
|
|
757
|
+
return {
|
|
758
|
+
render(width) {
|
|
759
|
+
return [truncateToWidth(header, width), ...(body?.render(width) ?? [])];
|
|
760
|
+
},
|
|
761
|
+
invalidate() {},
|
|
762
|
+
handleMouse(event) {
|
|
763
|
+
if (event.type !== "click" || event.button !== "left") return undefined;
|
|
764
|
+
// Pi's default click is gated on having a result. Consume the click
|
|
765
|
+
// here so it toggles only the arguments, even while the call is running.
|
|
766
|
+
callHeader.expanded = !expanded;
|
|
767
|
+
context.invalidate();
|
|
768
|
+
return { handled: true };
|
|
769
|
+
},
|
|
770
|
+
};
|
|
771
|
+
},
|
|
735
772
|
renderResult(result, { expanded }, theme, context) {
|
|
736
773
|
const details = result.details as { run_id?: string; status?: string } | undefined;
|
|
737
774
|
const record = details?.run_id ? deps.getRegistry()?.get(details.run_id) : undefined;
|