@yusukeshib/pi-babysit 0.6.31 → 0.6.33

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 CHANGED
@@ -161,7 +161,7 @@ the host.
161
161
  | `/babysit` | Arrow-key picker over all sessions. Renders an **inline snapshot** (no tmux): running **process** → current rendered screen + recent output + a copy-paste `babysit attach` take-over hint (detach `Ctrl-\ Ctrl-\`); running **subagent** → read-only progress (RPC stdin stays untouchable); finished → summary. Re-run `/babysit` to refresh |
162
162
  | `/babysit gc [days]` | Preview and confirm deletion of old Pi-session roots (default 14 days). Active leases, live supervisor/child PIDs, unknown states, the current root, and recent roots are retained; old empty roots are eligible once their lease is gone. Deletion uses a GC lock and atomic rename |
163
163
 
164
- The widget below the editor lists all running sessions, newest first,
164
+ The TUI-only widget below the editor lists all running sessions, newest first,
165
165
  including reusable idle agents. Summary counts use `RUNNING` / `IDLE`; rows show
166
166
  `PROCESS RUNNING`, `AGENT RUNNING`, or `AGENT IDLE`, total session age, remote host
167
167
  and unknown-status indicators when applicable, and a colored one-line description.
@@ -208,20 +208,14 @@ targeted pattern over a broad tail, and do not read a potentially large log file
208
208
  in full. Subagent crashes return structured errors plus the full log path, never
209
209
  a raw RPC JSON tail.
210
210
 
211
+ Periodic widget updates are disabled outside TUI mode, so headless RPC workers
212
+ do not fill their logs with status/age UI requests. Background completion
213
+ notifications and actionable warnings/errors remain enabled.
214
+
211
215
  Subagents run in RPC mode with normal Pi session persistence enabled, using
212
216
  Pi's configured session storage (by default `~/.pi/agent/sessions/`). Each worker
213
217
  has its own Pi session, separate from the parent and from babysit's output log.
214
218
 
215
- Subagent logs compact Pi's streaming `message_update` events before they are
216
- recorded. Pi repeats the complete growing assistant message and partial snapshot
217
- on every token; pi-babysit retains only the incremental delta. Compact RPC
218
- logging is the default and also removes duplicate payloads from `message_start`,
219
- `turn_end`, successful `tool_execution_end`, and `agent_end`, while preserving
220
- authoritative `message_end`, failures, responses, and errors. Parked-process
221
- state is materialized as a small boolean so completion detection is unchanged.
222
- Set `PI_BABYSIT_RPC_LOG_MODE=standard` only when legacy lifecycle payloads are
223
- needed for debugging. Live `/babysit` and attach views render either format.
224
-
225
219
  All shell commands, including `pwd` and Git, go through `babysit_run`. Bundle
226
220
  closely related tiny observations when doing so safely reduces tool turns. For
227
221
  parallel checks, do not issue sibling `foreground: true` calls: start them with
@@ -231,6 +225,32 @@ suite at the end. Background subagents must likewise be collected before the
231
225
  parent task finishes so their answer and nested usage are not lost.
232
226
  Set `PI_BABYSIT_ALLOW_BASH=1` only as an explicit emergency escape hatch.
233
227
 
228
+ ### Subagent log format
229
+
230
+ A subagent's babysit log is a subset of Pi's standard RPC event stream
231
+ (`rpc-stream-format.mjs`):
232
+
233
+ - The first line is `{"type":"pi_babysit_stream","version":1,"profile":"compact"}`
234
+ (`"standard"` with `PI_BABYSIT_RPC_LOG_MODE=standard`).
235
+ - Standard events keep their type and field meaning. The compact profile only
236
+ omits fields whose value is exactly reproducible from other lines; it never
237
+ reshapes a field. pi-babysit's own data uses separate `pi_babysit_*` events.
238
+ - `message_update` is delta-only, as in Pi >= 1.4 (older Pi's cumulative
239
+ `message` / `partial` snapshots are stripped).
240
+ - Compact omissions: `message.content` / `message.details` of a non-assistant
241
+ `message_start` that is immediately followed by an identical `message_end`;
242
+ `result.content` / `result.details` of a non-error `tool_execution_end` (they
243
+ equal the `toolResult` `message_end` with the same `toolCallId`, otherwise a
244
+ `pi_babysit_tool_execution_result` event carries the original); and
245
+ `turn_end.message` / `turn_end.toolResults` / `agent_end.messages` when they
246
+ equal the values derived from the run's `message_end` events.
247
+ - Every `agent_end` is preceded by `{"type":"pi_babysit_agent_end","parked":bool}`,
248
+ which completion detection uses.
249
+
250
+ `createRpcEventRestorer()` turns a log back into standard Pi events for
251
+ viewers. Live `/babysit` and attach views render either profile, as well as logs
252
+ written by older pi-babysit releases.
253
+
234
254
  ## Process ownership, interruption, shutdown, and reload
235
255
 
236
256
  An inline process wait (`foreground: true`, or process execution in a non-UI
@@ -326,7 +346,7 @@ session totals.
326
346
  | `PI_BABYSIT_QUICK_GRACE` | `2s` | interactive process grace before a still-running command is returned as background work; use `foreground: true` to wait explicitly |
327
347
  | `PI_BABYSIT_REAP_AFTER` | `120s` | idle grace before a finished subagent self-exits (`off`/`none`/`0` disables) |
328
348
  | `PI_BABYSIT_BUDGET_GRACE` | `90s` | grace after a subagent budget is exceeded before verified termination |
329
- | `PI_BABYSIT_RPC_LOG_MODE` | `compact` | `compact` removes duplicate RPC lifecycle payloads; `standard` opts into legacy payloads |
349
+ | `PI_BABYSIT_RPC_LOG_MODE` | `compact` | `compact` omits reproducible duplicate RPC payloads; `standard` records Pi's events unchanged (see Subagent log format) |
330
350
  | `PI_BABYSIT_RETENTION_DAYS` | `3` | at most once per day, remove safe terminal roots older than this (in a background child process, so session start never waits); set `0` to disable automatic retention |
331
351
  | `PI_BABYSIT_TAIL_MAX_BYTES` | `4000` | default cap for explicit log tails/screens returned by `babysit_check`; override per call with `maxBytes` |
332
352
  | `PI_BABYSIT_INLINE_OUTPUT_MAX_BYTES` | `8000` | cap for complete process output and aggregate multi-wait results |
package/index.ts CHANGED
@@ -1958,12 +1958,20 @@ function parseEventLine(progress: Progress, raw: string): void {
1958
1958
  case "agent_start":
1959
1959
  progress.agentStarts++;
1960
1960
  break;
1961
+ // The compact log precedes every agent_end with this marker and may omit
1962
+ // agent_end.messages (see rpc-stream-format.mjs).
1963
+ case "pi_babysit_agent_end":
1964
+ if (typeof event.parked === "boolean") progress.lastEndWasProcessWait = event.parked;
1965
+ break;
1961
1966
  case "agent_end": {
1962
1967
  progress.agentEnds++;
1963
- progress.lastEndWasProcessWait =
1964
- typeof event.piBabysitParked === "boolean"
1965
- ? event.piBabysitParked
1966
- : isParkedMessages(event.messages as ParkedMessage[] | undefined);
1968
+ if (typeof event.piBabysitParked === "boolean") {
1969
+ // Logs written by pi-babysit <= 0.6.32.
1970
+ progress.lastEndWasProcessWait = event.piBabysitParked;
1971
+ } else if (Array.isArray(event.messages)) {
1972
+ progress.lastEndWasProcessWait = isParkedMessages(event.messages as ParkedMessage[]);
1973
+ }
1974
+ // Otherwise keep the value from the preceding pi_babysit_agent_end marker.
1967
1975
  break;
1968
1976
  }
1969
1977
  case "agent_settled":
@@ -2346,9 +2354,9 @@ async function spawnSubagent(
2346
2354
  "--timeout",
2347
2355
  opts.timeout,
2348
2356
  ];
2349
- // Pretty-print the compact JSONL stream for humans who `attach`. The RPC
2350
- // proxy retains authoritative message_end/response/error events and reduces
2351
- // redundant lifecycle/tool payloads; parseEvents accepts both log formats.
2357
+ // Pretty-print the JSONL stream for humans who `attach`. The RPC proxy
2358
+ // writes a standard-event subset that omits only reproducible duplicate
2359
+ // payloads (rpc-stream-format.mjs); parseEvents accepts every log format.
2352
2360
  if (VIEW_CMD.trim()) bsArgs.push("--view-cmd", VIEW_CMD);
2353
2361
  if (opts.idleTimeout && opts.idleTimeout !== "none") {
2354
2362
  bsArgs.push("--idle-timeout", opts.idleTimeout);
@@ -3574,7 +3582,9 @@ export default function (pi: ExtensionAPI) {
3574
3582
 
3575
3583
  let sessionWidget: ReturnType<typeof createWidgetComponent> | undefined;
3576
3584
  const refreshWidget = async (ctx: ExtensionContext, snapshot?: BsSession[]) => {
3577
- if (!ctx.hasUI) return;
3585
+ // RPC hasUI is also true, but every setWidget call becomes a log record.
3586
+ // Keep the age/status ticker local to the TUI, not headless worker logs.
3587
+ if (!ctx.hasUI || ctx.mode !== "tui") return;
3578
3588
  const all = snapshot ?? (await listSessions()).sessions;
3579
3589
  const { active } = widgetSessionGroups(all, (id) => readMeta(id)?.startedAt ?? 0);
3580
3590
  const subs = active.filter((session) => kindOf(session.id) === "subagent");
@@ -3606,21 +3616,15 @@ export default function (pi: ExtensionAPI) {
3606
3616
  description: description ? theme.fg("warning", theme.bold(escapeCommandForDisplay(description))) : undefined,
3607
3617
  };
3608
3618
  });
3609
- if (ctx.mode === "tui") {
3610
- if (!sessionWidget) {
3611
- ctx.ui.setWidget("pi-babysit", (tui) => {
3612
- sessionWidget = createWidgetComponent(summaryLines, displays,
3613
- (id) => copyToClipboard(sessionCliCommand(id, "log")),
3614
- () => tui.requestRender());
3615
- return sessionWidget;
3616
- }, { placement: "belowEditor" });
3617
- } else {
3618
- sessionWidget.update(summaryLines, displays);
3619
- }
3619
+ if (!sessionWidget) {
3620
+ ctx.ui.setWidget("pi-babysit", (tui) => {
3621
+ sessionWidget = createWidgetComponent(summaryLines, displays,
3622
+ (id) => copyToClipboard(sessionCliCommand(id, "log")),
3623
+ () => tui.requestRender());
3624
+ return sessionWidget;
3625
+ }, { placement: "belowEditor" });
3620
3626
  } else {
3621
- ctx.ui.setWidget("pi-babysit", createWidgetComponent(summaryLines, displays).render(10_000), {
3622
- placement: "belowEditor",
3623
- });
3627
+ sessionWidget.update(summaryLines, displays);
3624
3628
  }
3625
3629
  };
3626
3630
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yusukeshib/pi-babysit",
3
- "version": "0.6.31",
3
+ "version": "0.6.33",
4
4
  "description": "Run any shell command and pi subagents under babysit, with context-safe captured output.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -24,6 +24,7 @@
24
24
  "self-reap.ts",
25
25
  "format-stream.mjs",
26
26
  "rpc-stream-proxy.mjs",
27
+ "rpc-stream-format.mjs",
27
28
  "search-log.mjs",
28
29
  "gc.mjs",
29
30
  "gc.d.mts",
@@ -0,0 +1,446 @@
1
+ /**
2
+ * rpc-stream-format.mjs — pi-babysit's subagent log format for `pi --mode rpc`.
3
+ *
4
+ * The log is a subset of Pi's standard RPC event stream:
5
+ *
6
+ * - The first line is a header:
7
+ * {"type":"pi_babysit_stream","version":1,"profile":"compact"|"standard"}
8
+ * - Standard events keep their type and field meaning. The compact profile only
9
+ * OMITS fields whose value is exactly reproducible from other lines; it never
10
+ * reshapes a field.
11
+ * - pi-babysit's own data is carried by separate `pi_babysit_*` events, never by
12
+ * extra fields on standard events. Standard consumers can ignore them.
13
+ *
14
+ * Compact profile v1 omissions (see README "Subagent log format"):
15
+ * - message_start of a non-assistant message: `message.content` and
16
+ * `message.details`, only when the next line is a message_end carrying an
17
+ * identical message.
18
+ * - tool_execution_end with `isError !== true`: `result.content` and
19
+ * `result.details`. They equal the toolResult message_end for the same
20
+ * toolCallId; otherwise a `pi_babysit_tool_execution_result` event carries the
21
+ * original values.
22
+ * - turn_end `message` / `toolResults` and agent_end `messages`: only when equal
23
+ * to the value derived from message_end events by createRunTracker.
24
+ *
25
+ * Every agent_end is preceded by {"type":"pi_babysit_agent_end","parked":bool}.
26
+ *
27
+ * createRpcEventRestorer() turns a compact log back into standard events.
28
+ */
29
+ import { createHash } from "node:crypto";
30
+
31
+ export const STREAM_HEADER_TYPE = "pi_babysit_stream";
32
+ export const STREAM_VERSION = 1;
33
+ export const AGENT_END_MARKER_TYPE = "pi_babysit_agent_end";
34
+ export const TOOL_RESULT_EVENT_TYPE = "pi_babysit_tool_execution_result";
35
+ const BABYSIT_EVENT_PREFIX = "pi_babysit_";
36
+
37
+ export function streamHeader(profile) {
38
+ return { type: STREAM_HEADER_TYPE, version: STREAM_VERSION, profile };
39
+ }
40
+
41
+ /** True when the agent run ended with a background process whose exit will resume it. */
42
+ export function isParkedMessages(messages) {
43
+ if (!Array.isArray(messages)) return false;
44
+ const terminalCounts = new Map();
45
+ const addTerminal = (id) => terminalCounts.set(id, (terminalCounts.get(id) ?? 0) + 1);
46
+
47
+ for (let index = messages.length - 1; index >= 0; index--) {
48
+ const message = messages[index];
49
+ if (message?.role !== "toolResult") continue;
50
+ if (message.toolName === "babysit_wait") {
51
+ const status = message.details?.status;
52
+ if (
53
+ status &&
54
+ typeof status === "object" &&
55
+ typeof status.id === "string" &&
56
+ typeof status.state === "string" &&
57
+ status.state !== "running"
58
+ ) {
59
+ addTerminal(status.id);
60
+ }
61
+ for (const result of message.details?.results ?? []) {
62
+ if (result.kind === "exited" && typeof result.id === "string") addTerminal(result.id);
63
+ }
64
+ const first = message.details?.first;
65
+ if (first?.kind === "exited" && typeof first.id === "string") addTerminal(first.id);
66
+ continue;
67
+ }
68
+ if (
69
+ message.toolName === "babysit_kill" &&
70
+ typeof message.details?.id === "string" &&
71
+ ["exited", "killed", "dead"].includes(String(message.details.status))
72
+ ) {
73
+ addTerminal(message.details.id);
74
+ continue;
75
+ }
76
+ if (message.toolName === "process") return true;
77
+ if (message.toolName !== "babysit_run") continue;
78
+ const text = Array.isArray(message.content)
79
+ ? message.content
80
+ .filter((part) => part?.type === "text" && typeof part.text === "string")
81
+ .map((part) => part.text)
82
+ .join("")
83
+ : typeof message.content === "string"
84
+ ? message.content
85
+ : "";
86
+ const textMatch = /^Process started \(id: ([^)]+)\)\. \[notify-on-exit\]\nLog: /.exec(text);
87
+ const structuredStart =
88
+ message.details?.kind === "process" && message.details.status === "started";
89
+ if (!structuredStart && !textMatch) continue;
90
+ const id = typeof message.details?.id === "string" ? message.details.id : textMatch?.[1];
91
+ if (!id) return true;
92
+ const count = terminalCounts.get(id) ?? 0;
93
+ if (count === 0) return true;
94
+ if (count === 1) terminalCounts.delete(id);
95
+ else terminalCounts.set(id, count - 1);
96
+ }
97
+ return false;
98
+ }
99
+
100
+ const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
101
+
102
+ /**
103
+ * Derives turn_end and agent_end payloads from message_end events. The
104
+ * compactor and the restorer must feed it the same event sequence; `project`
105
+ * maps each message to the stored value (a hash in the compactor, the message
106
+ * itself in the restorer).
107
+ */
108
+ export function createRunTracker(project) {
109
+ let run = null;
110
+ let turn = [];
111
+ return {
112
+ observe(event) {
113
+ switch (event?.type) {
114
+ case "agent_start":
115
+ run = [];
116
+ turn = [];
117
+ break;
118
+ case "turn_start":
119
+ turn = [];
120
+ break;
121
+ case "message_end":
122
+ if (isObject(event.message)) {
123
+ const value = project(event.message);
124
+ run?.push(value);
125
+ turn.push({ role: event.message.role, value });
126
+ }
127
+ break;
128
+ case "agent_end":
129
+ run = null;
130
+ turn = [];
131
+ break;
132
+ }
133
+ },
134
+ /** `{ message, toolResults }` for the current turn, or null when not derivable. */
135
+ turnEnd() {
136
+ let index = turn.length - 1;
137
+ while (index >= 0 && turn[index].role !== "assistant") index--;
138
+ if (index < 0) return null;
139
+ return {
140
+ message: turn[index].value,
141
+ toolResults: turn
142
+ .slice(index + 1)
143
+ .filter((entry) => entry.role === "toolResult")
144
+ .map((entry) => entry.value),
145
+ };
146
+ },
147
+ /** Messages of the current agent run, or null when no agent_start was observed. */
148
+ agentEndMessages() {
149
+ return run ? [...run] : null;
150
+ },
151
+ };
152
+ }
153
+
154
+ const hashOf = (value) => createHash("sha1").update(JSON.stringify(value) ?? "").digest("base64");
155
+ const sameList = (a, b) => a.length === b.length && a.every((value, index) => value === b[index]);
156
+
157
+ /** Pick only the keys present on `source`, preserving JSON absent-vs-present. */
158
+ function pickPresent(source, keys) {
159
+ const out = {};
160
+ for (const key of keys) if (isObject(source) && key in source && source[key] !== undefined) out[key] = source[key];
161
+ return out;
162
+ }
163
+
164
+ function withoutKeys(source, keys) {
165
+ const out = { ...source };
166
+ for (const key of keys) delete out[key];
167
+ return out;
168
+ }
169
+
170
+ const PAYLOAD_KEYS = ["content", "details"];
171
+
172
+ /**
173
+ * Old Pi releases repeat the cumulative assistant message on every
174
+ * message_update. Pi >= 1.4 writes delta-only updates, which is the standard
175
+ * wire form; normalize older streams to it.
176
+ */
177
+ function normalizeMessageUpdate(event) {
178
+ const { message: _cumulativeMessage, assistantMessageEvent, ...rest } = event;
179
+ if (!isObject(assistantMessageEvent)) return rest;
180
+ const { partial: _cumulativePartial, ...deltaEvent } = assistantMessageEvent;
181
+ return { ...rest, assistantMessageEvent: deltaEvent };
182
+ }
183
+
184
+ function eventTypeOf(line) {
185
+ return /^\{"type":"([^"\\]+)"/.exec(line)?.[1];
186
+ }
187
+
188
+ /**
189
+ * Stateful line transformer for the proxy. `push(line)` returns the lines to
190
+ * write (possibly none while a message_start is held); `end()` flushes.
191
+ */
192
+ export function createRpcLogCompactor({ mode = "compact" } = {}) {
193
+ const compact = mode === "compact";
194
+ let headerSent = false;
195
+ let heldStart = null; // { line, json, event }
196
+ const pendingResults = new Map(); // toolCallId -> JSON of original { content, details }
197
+ const tracker = createRunTracker(hashOf);
198
+
199
+ const correctionLine = (toolCallId, originalJson) =>
200
+ `{"type":${JSON.stringify(TOOL_RESULT_EVENT_TYPE)},"toolCallId":${JSON.stringify(toolCallId)},"result":${originalJson}}`;
201
+ const flushPendingResults = (out) => {
202
+ for (const [toolCallId, originalJson] of pendingResults) out.push(correctionLine(toolCallId, originalJson));
203
+ pendingResults.clear();
204
+ };
205
+ const releaseHeld = (out) => {
206
+ if (!heldStart) return;
207
+ out.push(heldStart.line);
208
+ heldStart = null;
209
+ };
210
+
211
+ function push(line) {
212
+ const out = [];
213
+ if (!headerSent) {
214
+ out.push(JSON.stringify(streamHeader(compact ? "compact" : "standard")));
215
+ headerSent = true;
216
+ }
217
+ if (!line.trimStart().startsWith("{")) {
218
+ releaseHeld(out);
219
+ out.push(line);
220
+ return out;
221
+ }
222
+ const type = eventTypeOf(line);
223
+ if (type !== "message_end") releaseHeld(out);
224
+ // Delta-only message_update lines are the bulk of the stream; skip the
225
+ // parse when there is nothing to strip. Inside JSON strings a quote is
226
+ // escaped, so these key patterns cannot match text.
227
+ if (type === "message_update" && !line.includes('"message":') && !line.includes('"partial":')) {
228
+ out.push(line);
229
+ return out;
230
+ }
231
+ if (type === undefined || (compact ? false : type !== "message_update")) {
232
+ out.push(line);
233
+ return out;
234
+ }
235
+
236
+ let event;
237
+ try {
238
+ event = JSON.parse(line);
239
+ } catch {
240
+ releaseHeld(out);
241
+ out.push(line);
242
+ return out;
243
+ }
244
+ if (event?.type === "message_update") {
245
+ out.push(JSON.stringify(normalizeMessageUpdate(event)));
246
+ return out;
247
+ }
248
+
249
+ switch (event?.type) {
250
+ case "message_start": {
251
+ const message = event.message;
252
+ if (isObject(message) && message.role !== "assistant") {
253
+ heldStart = { line, json: JSON.stringify(message), event };
254
+ return out;
255
+ }
256
+ out.push(line);
257
+ return out;
258
+ }
259
+ case "message_end": {
260
+ const message = event.message;
261
+ tracker.observe(event);
262
+ if (message?.role === "toolResult" && pendingResults.has(message.toolCallId)) {
263
+ const originalJson = pendingResults.get(message.toolCallId);
264
+ pendingResults.delete(message.toolCallId);
265
+ if (originalJson !== JSON.stringify(pickPresent(message, PAYLOAD_KEYS))) {
266
+ out.push(correctionLine(message.toolCallId, originalJson));
267
+ }
268
+ }
269
+ if (heldStart) {
270
+ if (heldStart.json === JSON.stringify(message)) {
271
+ const startMessage = withoutKeys(heldStart.event.message, PAYLOAD_KEYS);
272
+ out.push(JSON.stringify({ ...heldStart.event, message: startMessage }));
273
+ } else {
274
+ out.push(heldStart.line);
275
+ }
276
+ heldStart = null;
277
+ }
278
+ out.push(line);
279
+ return out;
280
+ }
281
+ case "tool_execution_end": {
282
+ if (event.isError === true || !isObject(event.result) || typeof event.toolCallId !== "string") {
283
+ out.push(line);
284
+ return out;
285
+ }
286
+ pendingResults.set(event.toolCallId, JSON.stringify(pickPresent(event.result, PAYLOAD_KEYS)));
287
+ out.push(JSON.stringify({ ...event, result: withoutKeys(event.result, PAYLOAD_KEYS) }));
288
+ return out;
289
+ }
290
+ case "turn_end": {
291
+ const derived = tracker.turnEnd();
292
+ let compacted = event;
293
+ if (derived && "message" in event && hashOf(event.message) === derived.message) {
294
+ compacted = withoutKeys(compacted, ["message"]);
295
+ }
296
+ if (
297
+ derived &&
298
+ Array.isArray(event.toolResults) &&
299
+ sameList(event.toolResults.map(hashOf), derived.toolResults)
300
+ ) {
301
+ compacted = withoutKeys(compacted, ["toolResults"]);
302
+ }
303
+ out.push(compacted === event ? line : JSON.stringify(compacted));
304
+ return out;
305
+ }
306
+ case "agent_end": {
307
+ flushPendingResults(out);
308
+ out.push(JSON.stringify({ type: AGENT_END_MARKER_TYPE, parked: isParkedMessages(event.messages) }));
309
+ const derived = tracker.agentEndMessages();
310
+ tracker.observe(event);
311
+ if (derived && Array.isArray(event.messages) && sameList(event.messages.map(hashOf), derived)) {
312
+ out.push(JSON.stringify(withoutKeys(event, ["messages"])));
313
+ } else {
314
+ out.push(line);
315
+ }
316
+ return out;
317
+ }
318
+ default:
319
+ tracker.observe(event);
320
+ out.push(line);
321
+ return out;
322
+ }
323
+ }
324
+
325
+ function end() {
326
+ const out = [];
327
+ releaseHeld(out);
328
+ flushPendingResults(out);
329
+ return out;
330
+ }
331
+
332
+ return { push, end };
333
+ }
334
+
335
+ /**
336
+ * Turns a pi-babysit log back into standard Pi RPC events. Feed parsed JSON
337
+ * events from the start of the log; `push` returns the restored events to
338
+ * consume now and `end` flushes anything still held.
339
+ *
340
+ * Restoration is exact for content. Ordering is preserved except that a
341
+ * compacted tool_execution_end is released together with the toolResult
342
+ * message for the same toolCallId.
343
+ */
344
+ export function createRpcEventRestorer() {
345
+ let compact = false;
346
+ let heldStart = null;
347
+ const pendingEnds = new Map(); // toolCallId -> compacted tool_execution_end
348
+ const tracker = createRunTracker((message) => message);
349
+
350
+ const releasePendingEnd = (out, toolCallId, payload) => {
351
+ const event = pendingEnds.get(toolCallId);
352
+ if (!event) return;
353
+ pendingEnds.delete(toolCallId);
354
+ out.push({ ...event, result: { ...event.result, ...payload } });
355
+ };
356
+ const releaseHeld = (out) => {
357
+ if (!heldStart) return;
358
+ out.push(heldStart);
359
+ heldStart = null;
360
+ };
361
+ const flushPendingEnds = (out) => {
362
+ for (const event of pendingEnds.values()) out.push(event);
363
+ pendingEnds.clear();
364
+ };
365
+
366
+ function push(event) {
367
+ const out = [];
368
+ const type = event?.type;
369
+ if (type === STREAM_HEADER_TYPE) {
370
+ compact = event.profile === "compact" && event.version === STREAM_VERSION;
371
+ return out;
372
+ }
373
+ if (typeof type === "string" && type.startsWith(BABYSIT_EVENT_PREFIX)) {
374
+ if (type === TOOL_RESULT_EVENT_TYPE && typeof event.toolCallId === "string") {
375
+ releasePendingEnd(out, event.toolCallId, pickPresent(event.result, PAYLOAD_KEYS));
376
+ }
377
+ return out;
378
+ }
379
+ if (!compact) return [event];
380
+ if (type !== "message_end") releaseHeld(out);
381
+
382
+ switch (type) {
383
+ case "message_start":
384
+ if (isObject(event.message) && event.message.role !== "assistant" && !("content" in event.message)) {
385
+ heldStart = event;
386
+ return out;
387
+ }
388
+ out.push(event);
389
+ return out;
390
+ case "message_end": {
391
+ tracker.observe(event);
392
+ const message = event.message;
393
+ if (message?.role === "toolResult") {
394
+ releasePendingEnd(out, message.toolCallId, pickPresent(message, PAYLOAD_KEYS));
395
+ }
396
+ if (heldStart) {
397
+ out.push({ ...heldStart, message: { ...heldStart.message, ...pickPresent(message, PAYLOAD_KEYS) } });
398
+ heldStart = null;
399
+ }
400
+ out.push(event);
401
+ return out;
402
+ }
403
+ case "tool_execution_end":
404
+ if (
405
+ event.isError !== true &&
406
+ typeof event.toolCallId === "string" &&
407
+ isObject(event.result) &&
408
+ !("content" in event.result) &&
409
+ !("details" in event.result)
410
+ ) {
411
+ pendingEnds.set(event.toolCallId, event);
412
+ return out;
413
+ }
414
+ out.push(event);
415
+ return out;
416
+ case "turn_end": {
417
+ const derived = tracker.turnEnd();
418
+ const restored = { ...event };
419
+ if (!("message" in event) && derived) restored.message = derived.message;
420
+ if (!("toolResults" in event) && derived) restored.toolResults = derived.toolResults;
421
+ out.push(restored);
422
+ return out;
423
+ }
424
+ case "agent_end": {
425
+ flushPendingEnds(out);
426
+ const derived = tracker.agentEndMessages();
427
+ tracker.observe(event);
428
+ out.push(!("messages" in event) && derived ? { ...event, messages: derived } : event);
429
+ return out;
430
+ }
431
+ default:
432
+ tracker.observe(event);
433
+ out.push(event);
434
+ return out;
435
+ }
436
+ }
437
+
438
+ function end() {
439
+ const out = [];
440
+ releaseHeld(out);
441
+ flushPendingEnds(out);
442
+ return out;
443
+ }
444
+
445
+ return { push, end };
446
+ }
@@ -2,165 +2,50 @@
2
2
  /**
3
3
  * Transparent stdin/stdout proxy for `pi --mode rpc`.
4
4
  *
5
- * Pi's `message_update` events repeat the entire growing assistant message and
6
- * `assistantMessageEvent.partial` on every token. Those cumulative snapshots
7
- * are useful only while streaming; `message_end` is the authoritative final
8
- * message. Persisting every snapshot makes babysit logs grow quadratically.
9
- *
10
- * This proxy forwards stdin byte-for-byte. `message_update` snapshots are
11
- * always compacted. Compact log mode is the default: redundant payloads on
12
- * lifecycle/tool events are removed while authoritative `message_end`,
13
- * response, and error events remain intact. Set PI_BABYSIT_RPC_LOG_MODE=standard
14
- * to opt back into the legacy lifecycle payloads.
5
+ * stdin is forwarded byte-for-byte. stdout is rewritten line by line into
6
+ * pi-babysit's subagent log format (see rpc-stream-format.mjs): a subset of
7
+ * Pi's standard RPC event stream that only omits exactly reproducible
8
+ * duplicate payloads, plus separate `pi_babysit_*` events. Compact is the
9
+ * default profile; PI_BABYSIT_RPC_LOG_MODE=standard keeps Pi's events intact.
15
10
  */
16
11
  import { spawn } from "node:child_process";
17
12
  import { StringDecoder } from "node:string_decoder";
18
13
  import { pathToFileURL } from "node:url";
14
+ import { createRpcLogCompactor } from "./rpc-stream-format.mjs";
19
15
 
20
- function isParkedMessages(messages) {
21
- if (!Array.isArray(messages)) return false;
22
- const terminalCounts = new Map();
23
- const addTerminal = (id) => terminalCounts.set(id, (terminalCounts.get(id) ?? 0) + 1);
24
-
25
- for (let index = messages.length - 1; index >= 0; index--) {
26
- const message = messages[index];
27
- if (message?.role !== "toolResult") continue;
28
- if (message.toolName === "babysit_wait") {
29
- const status = message.details?.status;
30
- if (
31
- status &&
32
- typeof status === "object" &&
33
- typeof status.id === "string" &&
34
- typeof status.state === "string" &&
35
- status.state !== "running"
36
- ) {
37
- addTerminal(status.id);
38
- }
39
- for (const result of message.details?.results ?? []) {
40
- if (result.kind === "exited" && typeof result.id === "string") addTerminal(result.id);
41
- }
42
- const first = message.details?.first;
43
- if (first?.kind === "exited" && typeof first.id === "string") addTerminal(first.id);
44
- continue;
45
- }
46
- if (
47
- message.toolName === "babysit_kill" &&
48
- typeof message.details?.id === "string" &&
49
- ["exited", "killed", "dead"].includes(String(message.details.status))
50
- ) {
51
- addTerminal(message.details.id);
52
- continue;
53
- }
54
- if (message.toolName === "process") return true;
55
- if (message.toolName !== "babysit_run") continue;
56
- const text = Array.isArray(message.content)
57
- ? message.content
58
- .filter((part) => part?.type === "text" && typeof part.text === "string")
59
- .map((part) => part.text)
60
- .join("")
61
- : typeof message.content === "string"
62
- ? message.content
63
- : "";
64
- const textMatch = /^Process started \(id: ([^)]+)\)\. \[notify-on-exit\]\nLog: /.exec(text);
65
- const structuredStart =
66
- message.details?.kind === "process" && message.details.status === "started";
67
- if (!structuredStart && !textMatch) continue;
68
- const id = typeof message.details?.id === "string" ? message.details.id : textMatch?.[1];
69
- if (!id) return true;
70
- const count = terminalCounts.get(id) ?? 0;
71
- if (count === 0) return true;
72
- if (count === 1) terminalCounts.delete(id);
73
- else terminalCounts.set(id, count - 1);
74
- }
75
- return false;
76
- }
77
-
78
- export function compactRpcLine(
79
- line,
80
- mode = process.env.PI_BABYSIT_RPC_LOG_MODE ?? "compact",
81
- ) {
82
- if (!line.trimStart().startsWith("{")) return line;
83
- // Pi >= 1.4 already writes delta-only message_update lines (no cumulative
84
- // `message` or `partial`). They are the bulk of a worker's stream, so skip
85
- // the parse/re-serialize round trip when there is nothing to strip. Inside
86
- // JSON strings a quote is escaped, so these key patterns cannot match text.
87
- if (
88
- line.startsWith('{"type":"message_update"') &&
89
- !line.includes('"message":') &&
90
- !line.includes('"partial":')
91
- ) {
92
- return line;
93
- }
94
-
95
- let event;
96
- try {
97
- event = JSON.parse(line);
98
- } catch {
99
- return line;
100
- }
101
-
102
- if (event?.type === "message_update") {
103
- const { message: _cumulativeMessage, assistantMessageEvent, ...rest } = event;
104
- if (!assistantMessageEvent || typeof assistantMessageEvent !== "object") {
105
- return JSON.stringify(rest);
106
- }
107
- const { partial: _cumulativePartial, ...deltaEvent } = assistantMessageEvent;
108
- return JSON.stringify({ ...rest, assistantMessageEvent: deltaEvent });
109
- }
110
- if (mode !== "compact") return line;
111
-
112
- switch (event?.type) {
113
- case "message_start": {
114
- const { message, ...rest } = event;
115
- return JSON.stringify({
116
- ...rest,
117
- message:
118
- message && typeof message === "object"
119
- ? { role: message.role, toolName: message.toolName, toolCallId: message.toolCallId }
120
- : message,
121
- });
122
- }
123
- case "turn_end": {
124
- const { message: _message, toolResults: _toolResults, ...rest } = event;
125
- return JSON.stringify(rest);
126
- }
127
- case "agent_end": {
128
- const { messages, ...rest } = event;
129
- return JSON.stringify({ ...rest, piBabysitParked: isParkedMessages(messages) });
130
- }
131
- case "tool_execution_end": {
132
- if (event.isError) return line;
133
- const { result: _result, ...rest } = event;
134
- return JSON.stringify(rest);
135
- }
136
- default:
137
- return line;
138
- }
16
+ export function rpcLogMode(value = process.env.PI_BABYSIT_RPC_LOG_MODE) {
17
+ return value === "standard" ? "standard" : "compact";
139
18
  }
140
19
 
141
20
  function transformRpcOutput(input, output) {
142
21
  return new Promise((resolve, reject) => {
143
22
  const decoder = new StringDecoder("utf8");
23
+ const compactor = createRpcLogCompactor({ mode: rpcLogMode() });
144
24
  let buffered = "";
145
25
 
146
- const write = (value) => {
147
- if (!output.write(value)) input.pause();
26
+ const write = (lines, terminated = true) => {
27
+ if (lines.length === 0) return;
28
+ if (!output.write(lines.join("\n") + (terminated ? "\n" : ""))) input.pause();
148
29
  };
149
30
  output.on("drain", () => input.resume());
150
31
  input.on("error", reject);
151
32
  input.on("data", (chunk) => {
152
33
  buffered += decoder.write(chunk);
34
+ const lines = [];
153
35
  for (;;) {
154
36
  const newline = buffered.indexOf("\n");
155
37
  if (newline < 0) break;
156
- const line = buffered.slice(0, newline);
38
+ lines.push(...compactor.push(buffered.slice(0, newline)));
157
39
  buffered = buffered.slice(newline + 1);
158
- write(`${compactRpcLine(line)}\n`);
159
40
  }
41
+ write(lines);
160
42
  });
161
43
  input.on("end", () => {
162
44
  buffered += decoder.end();
163
- if (buffered) write(compactRpcLine(buffered));
45
+ const lines = buffered ? compactor.push(buffered) : [];
46
+ lines.push(...compactor.end());
47
+ // Keep an unterminated final record unterminated.
48
+ write(lines, !buffered);
164
49
  if (output.writableNeedDrain) output.once("drain", resolve);
165
50
  else resolve();
166
51
  });