pi-famulus 0.1.7 → 0.1.9

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.
@@ -1,5 +1,9 @@
1
- import { accessSync, constants, readFileSync, statSync } from "node:fs";
1
+ import { spawnSync } from "node:child_process";
2
+ import { accessSync, closeSync, constants, mkdirSync, openSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync, writeSync } from "node:fs";
2
3
  import { createRequire } from "node:module";
4
+ import { homedir } from "node:os";
5
+ import { dirname, join, resolve, sep } from "node:path";
6
+ import { fileURLToPath } from "node:url";
3
7
  import { getSystemErrorMap } from "node:util";
4
8
 
5
9
  // Filesystem failures mean this optional package is unusable, regardless of the syscall code.
@@ -11,8 +15,10 @@ const resolutionErrorCodes = new Set([
11
15
  "ERR_INVALID_MODULE_SPECIFIER", "ERR_UNSUPPORTED_RESOLVE_REQUEST",
12
16
  ]);
13
17
 
18
+ const modulePath = fileURLToPath(import.meta.url);
14
19
  const packageVersion = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
15
20
  const resolveInstalled = createRequire(import.meta.url).resolve;
21
+ const RESTORE_HINT = "not installed; run `npm install --include=optional pi-famulus`";
16
22
 
17
23
  /** The native optional packages are selected by npm's os/cpu constraints. */
18
24
  export function nativePackageName(platform, arch) {
@@ -23,20 +29,263 @@ export function nativePackageName(platform, arch) {
23
29
  return `pi-famulus-${packageOs}-${arch}`;
24
30
  }
25
31
 
32
+ /**
33
+ * Cache for the exact platform package. `pi update --extensions` reinstalls
34
+ * with npm, and `omit=optional` (user or project npmrc) deletes optional
35
+ * dependencies during that install. The cache is outside the famulus runtime
36
+ * home and outside pi's package tree, so the next update does not remove it.
37
+ */
38
+ export function nativeCacheDir({
39
+ version = packageVersion,
40
+ platform = process.platform,
41
+ arch = process.arch,
42
+ env = process.env,
43
+ home = homedir(),
44
+ } = {}) {
45
+ const base = platform === "win32"
46
+ ? join(env.LOCALAPPDATA || join(home, "AppData", "Local"), "pi-famulus", "native")
47
+ : join(env.XDG_CACHE_HOME || join(home, ".cache"), "pi-famulus", "native");
48
+ return join(base, version, `${platform}-${arch}`);
49
+ }
50
+
51
+ function isIgnoredResolveError(error) {
52
+ return error instanceof SyntaxError || systemErrorCodes.has(error?.code) || resolutionErrorCodes.has(error?.code);
53
+ }
54
+
55
+ function executableFile(path) {
56
+ try {
57
+ const stat = statSync(path);
58
+ if (!stat.isFile() || stat.size <= 0) return false;
59
+ accessSync(path, constants.X_OK);
60
+ return true;
61
+ } catch (error) {
62
+ if (isIgnoredResolveError(error)) return false;
63
+ throw error;
64
+ }
65
+ }
66
+
67
+ /** Usable exact-version binary inside one package directory, or null. */
68
+ function binaryInPackage(dir, name) {
69
+ let metadata;
70
+ try {
71
+ metadata = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
72
+ } catch (error) {
73
+ if (isIgnoredResolveError(error)) return null;
74
+ throw error;
75
+ }
76
+ if (!metadata || metadata.name !== name || metadata.version !== packageVersion) return null;
77
+ const exported = metadata.exports?.["./bin/pi-famulus"];
78
+ const rel = typeof exported === "string" ? exported : metadata.main;
79
+ if (typeof rel !== "string" || !rel.startsWith("./") || rel.split("/").includes("..")) return null;
80
+ const binary = join(dir, rel);
81
+ const root = dir.endsWith(sep) ? dir : dir + sep;
82
+ if (!binary.startsWith(root)) return null;
83
+ return executableFile(binary) ? binary : null;
84
+ }
85
+
86
+ function walkNodeModules(startFile, stopAt) {
87
+ const dirs = [];
88
+ let dir = dirname(startFile);
89
+ const seen = new Set();
90
+ const boundary = stopAt ? resolve(stopAt) : null;
91
+ while (!seen.has(dir)) {
92
+ seen.add(dir);
93
+ dirs.push(join(dir, "node_modules"));
94
+ if (boundary && (dir === boundary || !dir.startsWith(boundary.endsWith(sep) ? boundary : boundary + sep))) break;
95
+ const parent = dirname(dir);
96
+ if (parent === dir) break;
97
+ dir = parent;
98
+ }
99
+ return dirs;
100
+ }
101
+
102
+ function legacyResolve(name, resolve) {
103
+ const metadata = JSON.parse(readFileSync(resolve(`${name}/package.json`), "utf8"));
104
+ if (!metadata || metadata.name !== name || metadata.version !== packageVersion) return null;
105
+ const binary = resolve(`${name}/bin/pi-famulus`);
106
+ const stat = statSync(binary);
107
+ if (!stat.isFile() || stat.size <= 0) return null;
108
+ accessSync(binary, constants.X_OK);
109
+ return binary;
110
+ }
111
+
112
+ function searchNative({ platform = process.platform, arch = process.arch, from, stopAt, cacheDir } = {}) {
113
+ const name = nativePackageName(platform, arch);
114
+ if (!name) return { name: null, path: null, detail: `unsupported ${platform}/${arch}` };
115
+ let start = from ?? modulePath;
116
+ if (!from) {
117
+ try { start = realpathSync(modulePath); } catch { /* keep the URL path */ }
118
+ }
119
+ const seen = new Set();
120
+ for (const nodeModules of walkNodeModules(start, stopAt)) {
121
+ const dir = join(nodeModules, name);
122
+ if (seen.has(dir)) continue;
123
+ seen.add(dir);
124
+ const binary = binaryInPackage(dir, name);
125
+ if (binary) return { name, path: binary, detail: "" };
126
+ }
127
+ // NODE_PATH and other resolver roots. An explicit `from` is a hermetic walk.
128
+ if (!from) {
129
+ try {
130
+ const resolved = dirname(resolveInstalled(`${name}/package.json`));
131
+ if (!seen.has(resolved)) {
132
+ const binary = binaryInPackage(resolved, name);
133
+ if (binary) return { name, path: binary, detail: "" };
134
+ }
135
+ } catch (error) {
136
+ if (!isIgnoredResolveError(error)) throw error;
137
+ }
138
+ }
139
+ if (cacheDir !== false) {
140
+ const dir = join(cacheDir ?? nativeCacheDir({ platform, arch }), "node_modules", name);
141
+ const binary = binaryInPackage(dir, name);
142
+ if (binary) return { name, path: binary, detail: "" };
143
+ }
144
+ return { name, path: null, detail: RESTORE_HINT };
145
+ }
146
+
26
147
  /** Resolve the exact-version optional package without loading native executable bytes. */
27
- export function resolveNativeManagerPath({ platform = process.platform, arch = process.arch, resolve = resolveInstalled } = {}) {
148
+ export function resolveNativeManagerPath({ platform = process.platform, arch = process.arch, resolve, from, stopAt, cacheDir } = {}) {
28
149
  const name = nativePackageName(platform, arch);
29
150
  if (!name) return null;
151
+ // Injected resolvers stay hermetic: tests use them to simulate one install tree.
152
+ if (resolve) {
153
+ try {
154
+ return legacyResolve(name, resolve);
155
+ } catch (error) {
156
+ if (isIgnoredResolveError(error)) return null;
157
+ throw error;
158
+ }
159
+ }
160
+ return searchNative({ platform, arch, from, stopAt, cacheDir }).path;
161
+ }
162
+
163
+ /** `npm <package> <path>` or why that exact platform package was not usable. */
164
+ export function describeNativeLookup(options) {
165
+ const name = nativePackageName(options?.platform ?? process.platform, options?.arch ?? process.arch);
166
+ if (!name) return `npm native manager (unsupported ${options?.platform ?? process.platform}/${options?.arch ?? process.arch})`;
167
+ if (options?.resolve) {
168
+ const path = resolveNativeManagerPath(options);
169
+ return path ? `npm ${name} ${path}` : `npm ${name} (missing or unusable, ignored)`;
170
+ }
171
+ const found = searchNative(options);
172
+ return found.path ? `npm ${name} ${found.path}` : `npm ${name} (${found.detail})`;
173
+ }
174
+
175
+ function pidAlive(pid) {
30
176
  try {
31
- const metadata = JSON.parse(readFileSync(resolve(`${name}/package.json`), "utf8"));
32
- if (!metadata || metadata.name !== name || metadata.version !== packageVersion) return null;
33
- const binary = resolve(`${name}/bin/pi-famulus`);
34
- const stat = statSync(binary);
35
- if (!stat.isFile() || stat.size <= 0) return null;
36
- accessSync(binary, constants.X_OK);
37
- return binary;
177
+ process.kill(pid, 0);
178
+ return true;
38
179
  } catch (error) {
39
- if (error instanceof SyntaxError || systemErrorCodes.has(error?.code) || resolutionErrorCodes.has(error?.code)) return null;
40
- throw error;
180
+ return error?.code === "EPERM";
181
+ }
182
+ }
183
+
184
+ function sleepMs(ms) {
185
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
186
+ }
187
+
188
+ /** A lock left by a dead process, or held far longer than one npm install, can be replaced. */
189
+ function installLockIsStale(lockPath) {
190
+ try {
191
+ const age = Date.now() - statSync(lockPath).mtimeMs;
192
+ const pid = Number(readFileSync(lockPath, "utf8").trim());
193
+ if (Number.isInteger(pid) && pid > 0) return !pidAlive(pid) || age > 150_000;
194
+ return age > 5_000;
195
+ } catch {
196
+ return true;
197
+ }
198
+ }
199
+
200
+ /**
201
+ * One npm install per cache directory. Another startup either waits until that
202
+ * install publishes a binary or takes over a stale lock. Returns the open lock,
203
+ * or null when waiting itself produced a usable binary.
204
+ */
205
+ function acquireInstallLock(dir, lookup) {
206
+ mkdirSync(dir, { recursive: true });
207
+ const lockPath = `${dir}.install.lock`;
208
+ const deadline = Date.now() + 130_000;
209
+ while (Date.now() <= deadline) {
210
+ try {
211
+ const fd = openSync(lockPath, "wx");
212
+ try { writeSync(fd, String(process.pid)); } catch { /* the fd is still the lock */ }
213
+ return { fd, lockPath };
214
+ } catch (error) {
215
+ if (error?.code !== "EEXIST") throw error;
216
+ const ready = resolveNativeManagerPath(lookup);
217
+ if (ready) return null;
218
+ if (installLockIsStale(lockPath)) {
219
+ rmSync(lockPath, { force: true });
220
+ continue;
221
+ }
222
+ sleepMs(200);
223
+ }
224
+ }
225
+ return null;
226
+ }
227
+
228
+ function releaseInstallLock(lock) {
229
+ if (!lock) return;
230
+ try { closeSync(lock.fd); } catch { /* already closed */ }
231
+ rmSync(lock.lockPath, { force: true });
232
+ }
233
+
234
+ /**
235
+ * Install the exact platform package into the versioned cache when the
236
+ * optional dependency is missing. No-ops when a usable binary is already
237
+ * resolvable. `PI_FAMULUS_NO_NATIVE_FETCH=1`, `PI_OFFLINE=1`, and Vitest skip
238
+ * the install. Directory, manifest, and npm failures return null so startup
239
+ * degrades instead of throwing.
240
+ */
241
+ export function ensureNativeManagerInstalled({
242
+ platform = process.platform,
243
+ arch = process.arch,
244
+ cacheDir,
245
+ env = process.env,
246
+ npmCommand,
247
+ spawn = spawnSync,
248
+ from,
249
+ stopAt,
250
+ } = {}) {
251
+ const lookup = { platform, arch, cacheDir, from, stopAt };
252
+ try {
253
+ const existing = resolveNativeManagerPath(lookup);
254
+ if (existing) return existing;
255
+ if (!nativePackageName(platform, arch)) return null;
256
+ if (env.PI_FAMULUS_NO_NATIVE_FETCH === "1" || env.PI_OFFLINE === "1" || env.VITEST) return null;
257
+ if (cacheDir === false) return null;
258
+ const dir = cacheDir ?? nativeCacheDir({ platform, arch, env });
259
+ const lockedLookup = { ...lookup, cacheDir: dir };
260
+ const lock = acquireInstallLock(dir, lockedLookup);
261
+ if (!lock) return resolveNativeManagerPath(lockedLookup);
262
+ try {
263
+ const manifest = join(dir, "package.json");
264
+ try {
265
+ readFileSync(manifest);
266
+ } catch {
267
+ writeFileSync(manifest, `${JSON.stringify({ private: true, name: "pi-famulus-native-cache", version: "0.0.0" })}\n`);
268
+ }
269
+ const command = npmCommand ?? (platform === "win32" ? "npm.cmd" : "npm");
270
+ const name = nativePackageName(platform, arch);
271
+ let result;
272
+ try {
273
+ result = spawn(command, [
274
+ "install", `${name}@${packageVersion}`,
275
+ "--prefix", dir,
276
+ "--ignore-scripts", "--no-fund", "--no-audit", "--loglevel=error",
277
+ ], { timeout: 120_000, windowsHide: true, encoding: "utf8" });
278
+ } catch {
279
+ return resolveNativeManagerPath(lockedLookup);
280
+ }
281
+ const found = resolveNativeManagerPath(lockedLookup);
282
+ if (found) return found;
283
+ if (!result || result.error || result.status !== 0) return null;
284
+ return null;
285
+ } finally {
286
+ releaseInstallLock(lock);
287
+ }
288
+ } catch {
289
+ return null;
41
290
  }
42
291
  }
package/src/notify.ts CHANGED
@@ -14,10 +14,12 @@
14
14
  * ever delivered once.
15
15
  */
16
16
  import { formatMonitorEvent, formatTaskNotification, type TaskExitInfo } from "./format";
17
- import { FAMULUS_WAKE_CUSTOM_TYPE, stampWakeAsOf, wakeIds, type WakeItem } from "./wake";
17
+ import { FAMULUS_WAKE_CUSTOM_TYPE, stampWakeAsOf, wakeIds, type MonitorOutputInfo, type WakeItem } from "./wake";
18
18
  import { realClock, type Clock, type ClockTimer } from "./clock";
19
19
 
20
20
 
21
+ type MonitorDeliveryAck = (visiblePreview?: string) => void;
22
+
21
23
  export interface NotifyMessage {
22
24
  customType: string;
23
25
  content: string;
@@ -42,6 +44,64 @@ export interface NotifyCenterDeps {
42
44
  }
43
45
 
44
46
 
47
+ // Reserve 100 of the 3000 preview characters for the event-count prefix and
48
+ // omission marker. No raw pending text grows with the number/size of events.
49
+ const MONITOR_PREVIEW_CHARS = 2900;
50
+ const MONITOR_PREVIEW_HALF = MONITOR_PREVIEW_CHARS / 2;
51
+ interface MonitorPreview {
52
+ head: string;
53
+ tail: string;
54
+ omittedChars: number;
55
+ }
56
+
57
+ function clipUtf16Start(text: string, max: number): string {
58
+ if (text.length <= max) return text;
59
+ const out = text.slice(0, max);
60
+ const last = out.charCodeAt(out.length - 1);
61
+ return last >= 0xd800 && last <= 0xdbff ? out.slice(0, -1) : out;
62
+ }
63
+
64
+ function clipUtf16End(text: string, max: number): string {
65
+ if (text.length <= max) return text;
66
+ const out = text.slice(-max);
67
+ const first = out.charCodeAt(0);
68
+ return first >= 0xdc00 && first <= 0xdfff ? out.slice(1) : out;
69
+ }
70
+
71
+ function appendMonitorPreview(previous: MonitorPreview | undefined, event: string): MonitorPreview {
72
+ const total = previous
73
+ ? previous.head.length + previous.tail.length + previous.omittedChars + 1 + event.length
74
+ : event.length;
75
+ if (total <= MONITOR_PREVIEW_CHARS) {
76
+ return { head: previous ? `${previous.head}\n${event}` : event, tail: "", omittedChars: 0 };
77
+ }
78
+ const head = previous
79
+ ? clipUtf16Start(`${previous.head}\n${clipUtf16Start(event, MONITOR_PREVIEW_HALF)}`, MONITOR_PREVIEW_HALF)
80
+ : clipUtf16Start(event, MONITOR_PREVIEW_HALF);
81
+ const tail = event.length >= MONITOR_PREVIEW_HALF
82
+ ? clipUtf16End(event, MONITOR_PREVIEW_HALF)
83
+ : clipUtf16End(`${previous?.tail || previous?.head || ""}\n${event}`, MONITOR_PREVIEW_HALF);
84
+ return { head, tail, omittedChars: total - head.length - tail.length };
85
+ }
86
+
87
+ function mergeMonitorOutput(previous: MonitorOutputInfo | undefined, next: MonitorOutputInfo | undefined): MonitorOutputInfo | undefined {
88
+ if (!previous && !next) return undefined;
89
+ const output = { ...previous };
90
+ for (const key of ["firstObservedAt", "cursorStart"] as const) {
91
+ const value = next?.[key];
92
+ if (value !== undefined) output[key] = Math.min(previous?.[key] ?? value, value);
93
+ }
94
+ for (const key of ["lastObservedAt", "cursorEnd"] as const) {
95
+ const value = next?.[key];
96
+ if (value !== undefined) output[key] = Math.max(previous?.[key] ?? value, value);
97
+ }
98
+ for (const key of ["suppressedCount", "omittedChars"] as const) {
99
+ if (previous?.[key] !== undefined || next?.[key] !== undefined) output[key] = (previous?.[key] ?? 0) + (next?.[key] ?? 0);
100
+ }
101
+ if (previous?.truncated !== undefined || next?.truncated !== undefined) output.truncated = !!previous?.truncated || !!next?.truncated;
102
+ return output;
103
+ }
104
+
45
105
  export class NotifyCenter {
46
106
  private readonly deps: NotifyCenterDeps;
47
107
  private readonly batchMs: number;
@@ -49,14 +109,14 @@ export class NotifyCenter {
49
109
  private pendingExits: TaskExitInfo[] = [];
50
110
  private readonly pendingMonitors = new Map<
51
111
  string,
52
- { description: string; eventCount: number; lastEvent: string; droppedLines: number; quiet: boolean }
112
+ { description: string; eventCount: number; preview: MonitorPreview; droppedLines: number; quiet: boolean; output?: MonitorOutputInfo; onDelivered?: MonitorDeliveryAck }
53
113
  >();
54
114
  private readonly seen = new Set<string>();
55
115
  private timer: ClockTimer | null = null;
56
116
  private disposed = false;
57
117
  /** A wake started or steered a run that has not settled yet. */
58
118
  private runPending = false;
59
- private held: NotifyMessage[] = [];
119
+ private held: { message: NotifyMessage; onDelivered?: MonitorDeliveryAck; sourcePreview?: string }[] = [];
60
120
 
61
121
  constructor(deps: NotifyCenterDeps) {
62
122
  this.deps = deps;
@@ -94,13 +154,17 @@ export class NotifyCenter {
94
154
  if (wake?.kind === "monitor" && typeof wake.id === "string") this.flushMonitor(wake.id, opts.quietEvents);
95
155
  if (!opts.passive) {
96
156
  this.deliver(message);
97
- } else if (this.runPending || !this.deps.isIdle()) {
98
- this.held.push(message);
99
157
  } else {
100
- this.deliver(message, true);
158
+ this.deliverPassive(message);
101
159
  }
102
160
  }
103
161
 
162
+ private deliverPassive(message: NotifyMessage, onDelivered?: MonitorDeliveryAck, sourcePreview?: string): void {
163
+ message = stampWakeAsOf(message, this.clock.now());
164
+ if (this.runPending || !this.deps.isIdle()) this.held.push({ message, onDelivered, sourcePreview });
165
+ else this.deliver(message, true, onDelivered, sourcePreview);
166
+ }
167
+
104
168
  /**
105
169
  * The agent settled: the run any earlier wake started is over. Flush
106
170
  * coalesced monitor output, then, unless that started another run, the
@@ -111,7 +175,7 @@ export class NotifyCenter {
111
175
  this.runPending = false;
112
176
  this.flushMonitorEvents();
113
177
  if (this.runPending) return;
114
- for (const message of this.held.splice(0)) this.deliver(message, true);
178
+ for (const { message, onDelivered, sourcePreview } of this.held.splice(0)) this.deliver(message, true, onDelivered, sourcePreview);
115
179
  }
116
180
 
117
181
  /**
@@ -119,21 +183,30 @@ export class NotifyCenter {
119
183
  * monitor. `quiet`: output of a monitor the model stopped; it is only
120
184
  * coalesced, and its summary is delivered without a turn (with the exit
121
185
  * notice, or at settle), pending events of that monitor included.
186
+ * `onDelivered` receives surviving source lines after sendMessage returns, not
187
+ * enqueue; generated headings/omission markers are never acknowledgement
188
+ * evidence. Callers must reuse one stable callback per monitor: retaining one
189
+ * callback bounds pending acknowledgements without silently dropping any.
122
190
  */
123
- notifyMonitorEvent(description: string, taskId: string, event: string, droppedLines = 0, opts: { quiet?: boolean } = {}): void {
191
+ notifyMonitorEvent(description: string, taskId: string, event: string, droppedLines = 0, opts: { quiet?: boolean; onDelivered?: MonitorDeliveryAck; output?: MonitorOutputInfo } = {}): void {
124
192
  if (this.disposed) return;
125
193
  const pending = this.pendingMonitors.get(taskId);
194
+ if (pending?.onDelivered && opts.onDelivered && pending.onDelivered !== opts.onDelivered) {
195
+ throw new Error(`Monitor ${taskId} requires a stable onDelivered callback while pending`);
196
+ }
126
197
  if (this.deps.isIdle() && !pending && !opts.quiet) {
127
- const wake = formatMonitorEvent(description, taskId, event, undefined, { droppedLines });
128
- this.deliver({ customType: wake.customType, content: wake.content, details: wake.details });
198
+ const wake = formatMonitorEvent(description, taskId, event, undefined, { droppedLines, output: opts.output });
199
+ this.deliver({ customType: wake.customType, content: wake.content, details: wake.details }, false, opts.onDelivered);
129
200
  return;
130
201
  }
131
202
  this.pendingMonitors.set(taskId, {
132
203
  description,
133
204
  eventCount: (pending?.eventCount ?? 0) + 1,
134
- lastEvent: event,
205
+ preview: appendMonitorPreview(pending?.preview, event),
135
206
  droppedLines: (pending?.droppedLines ?? 0) + droppedLines,
136
207
  quiet: (pending?.quiet ?? false) || opts.quiet === true,
208
+ output: mergeMonitorOutput(pending?.output, opts.output),
209
+ onDelivered: pending?.onDelivered ?? opts.onDelivered,
137
210
  });
138
211
  if (this.deps.isIdle() && !opts.quiet) this.flushMonitorEvents();
139
212
  }
@@ -149,16 +222,25 @@ export class NotifyCenter {
149
222
  const item = this.pendingMonitors.get(taskId);
150
223
  if (!item) return;
151
224
  this.pendingMonitors.delete(taskId);
152
- const summary = item.eventCount > 1
153
- ? `${item.eventCount} events · last: ${item.lastEvent}`
154
- : item.lastEvent;
225
+ const { head, tail, omittedChars } = item.preview;
226
+ const preview = omittedChars > 0 ? `${head}\n[... ${omittedChars} characters omitted ...]\n${tail}` : head;
227
+ const summary = item.eventCount > 1 ? `${item.eventCount} events\n${preview}` : preview;
155
228
  const wake = formatMonitorEvent(item.description, taskId, summary, undefined, {
156
229
  eventCount: item.eventCount,
157
230
  droppedLines: item.droppedLines,
231
+ output: mergeMonitorOutput(item.output, omittedChars > 0 ? { truncated: true, omittedChars } : undefined),
158
232
  });
159
233
  const message = { customType: wake.customType, content: wake.content, details: wake.details };
160
- if (quiet || item.quiet) this.notify(message, { passive: true });
161
- else this.deliver(message);
234
+ // Exclude generated text and boundary fragments: a clipped middle source
235
+ // line named e.g. "30 events" was not seen just because our heading says it.
236
+ const sourcePreview = omittedChars > 0
237
+ ? [
238
+ head.slice(0, Math.max(0, head.lastIndexOf("\n"))),
239
+ tail.includes("\n") ? tail.slice(tail.indexOf("\n") + 1) : "",
240
+ ].filter(Boolean).join("\n")
241
+ : head;
242
+ if (quiet || item.quiet) this.deliverPassive(message, item.onDelivered, sourcePreview);
243
+ else this.deliver(message, false, item.onDelivered, sourcePreview);
162
244
  }
163
245
 
164
246
  /** Flush any pending task exit notifications now. */
@@ -204,7 +286,7 @@ export class NotifyCenter {
204
286
  });
205
287
  }
206
288
 
207
- private deliver(message: NotifyMessage, passive = false): void {
289
+ private deliver(message: NotifyMessage, passive = false, onDelivered?: MonitorDeliveryAck, sourcePreview?: string): void {
208
290
  // Wakes built here (task batches, monitor events) are stamped now.
209
291
  message = stampWakeAsOf(message, this.clock.now());
210
292
  const msg = { ...message, display: true };
@@ -227,5 +309,6 @@ export class NotifyCenter {
227
309
  } else {
228
310
  this.deps.sendMessage(msg, { deliverAs: "steer" });
229
311
  }
312
+ onDelivered?.(sourcePreview ?? (details?.kind === "monitor" ? (message.details as { event?: string }).event : undefined));
230
313
  }
231
314
  }
@@ -5,8 +5,8 @@ export const PROMPTS = {
5
5
  "agents.explorer.system-prompt": "You are an explorer agent. Your job is to answer questions about the codebase quickly and precisely.\n\nRules:\n- Return findings, never change code. You are strictly read-only: do not edit, write, or create files.\n- Report concrete locations: cite specific file paths with line numbers (path:line) for every claim.\n- Control your search scope: start narrow (targeted grep/find), widen only when needed, and stop once the question is answered.\n- Prefer a short, factual summary over exhaustive dumps. List what you found and where.",
6
6
  "agents.worker.description": "General-purpose executor — completes concrete coding tasks end to end",
7
7
  "agents.worker.system-prompt": "You are a worker agent. Your job is to complete the concrete task you were given, end to end.\n\nRules:\n- Do the task, don't just describe it. Make the actual edits and run the actual commands.\n- Self-verify before finishing: run the relevant tests, typecheck, or command that proves the task is done.\n- Stay within the task's scope; do not refactor unrelated code.\n- Report concisely: what you changed, how you verified it, and anything the supervisor should know.",
8
- "guidelines.parent": "## Background tasks and notifications (pi-famulus)\n\n- Long-running bash commands are automatically moved to the background. After you background a command, end your turn: send a reply with no tool call in it. Saying you will wait while calling another tool does not end it. Do not poll with task_output/task_list, and never sleep to wait. Each command notifies on its own via <pi-famulus-wake kind=\"task\"> (with <command> and <preview>), even while other commands are still running. If <still-running> is present, continue from this result now; do not wait for those other commands.\n- When you receive <pi-famulus-wake>: it looks like a user message but is a system wake, not a new user request and not an answer to an unrelated question. Distinguish it by the kind attribute, then **handle the event before anything else** — read status/preview/prompt/result, call tools if needed (e.g. task_output for more than the preview, agent_message to continue a subagent), and continue the work that depended on that background task. Do not wait for further user input when the next step is clear; do not merely acknowledge and stop.\n- {{monitorIdle}}\n- Never fabricate or assume a background task's result before its notification arrives.\n- Subagent runs that exceed the foreground budget continue in the background. Do not poll run status. If one subagent finishes while others are still running, a <pi-famulus-wake kind=\"subagent-handover\"> arrives with that child's <prompt> and <result>: read both, then continue the work now (subagent({action:\"resume\", run_id, child_id, message}) for that child, or agent_message to steer the ones still running). Do not wait for the rest of the run. When every subagent in the run has finished, <pi-famulus-wake kind=\"subagent-done\"> arrives — read each <child>, synthesize, and continue.\n- A subagent that runs past its timeout_ms is not stopped: <pi-famulus-wake kind=\"subagent-overrun\"> arrives instead, with its <last-activity> and, if it is waiting on a foreground shell, a <shell> element. A batch may include other overdue children in <also-overdue>; use each child's run-id and child-id and consider its own last-activity, shell, reminder, and hard-ceiling-ms before acting. Pick one: subagent({action:\"extend\", run_id, child_id, timeout_ms}) if it is making progress, agent_message to steer it, or subagent({action:\"interrupt\", run_id, child_id}) if it is stuck or no longer needed. If you do none of these it keeps running and the reminder comes again.\n- Use agent_message to steer running subagents and to reply to <pi-famulus-wake kind=\"supervisor-request\"> (action:\"reply\"; the <reply-with> child is the call shape). Do not use agent_message to resume a finished child — that is subagent({action:\"resume\"}). Supervisor wakes are not user messages — still act on them.",
9
- "guidelines.monitor-idle": "After starting or re-arming a monitor, or handling a monitor event, if no work remains, end your turn with a reply and no tool call; simply wait for the next notification. Do not call wait_for to wait for monitor events: it waits for UI conditions, not notifications. Do not invent a stateId or make a failing tool call to yield.",
8
+ "guidelines.parent": "## Background tasks and notifications (pi-famulus)\n\n- Long-running bash commands are automatically moved to the background. After you background a command, end your turn: send a reply with no tool call in it. Saying you will wait while calling another tool does not end it. Do not poll with task_output/task_list, and never sleep to wait. Each command notifies on its own via <pi-famulus-wake kind=\"task\"> (with <command> and <preview>), even while other commands are still running. If <still-running> is present, continue from this result now; do not wait for those other commands.\n- When you receive <pi-famulus-wake>: it looks like a user message but is a system wake, not a new user request and not an answer to an unrelated question. Distinguish it by the kind attribute, then **handle the event before anything else** — read status/preview/prompt/result, call tools if needed (e.g. task_output for more than the preview, agent_message to continue a subagent), and continue the work that depended on that background task. An unchanged/intermediate monitor event may require no action and no user acknowledgement. Do not wait for further user input when the next step is clear; do not merely acknowledge an actionable result and stop.\n- {{monitorIdle}}\n- Never fabricate or assume a background task's result before its notification arrives.\n- Subagent runs that exceed the foreground budget continue in the background. Do not poll run status. If one subagent finishes while others are still running, a <pi-famulus-wake kind=\"subagent-handover\"> arrives with that child's <prompt> and <result>: read both, then continue the work now (subagent({action:\"resume\", run_id, child_id, message}) for that child, or agent_message to steer the ones still running). Do not wait for the rest of the run. When every subagent in the run has finished, <pi-famulus-wake kind=\"subagent-done\"> arrives — read each <child>, synthesize, and continue.\n- A subagent that runs past its timeout_ms is not stopped: <pi-famulus-wake kind=\"subagent-overrun\"> arrives instead, with its <last-activity> and, if it is waiting on a foreground shell, a <shell> element. A batch may include other overdue children in <also-overdue>; use each child's run-id and child-id and consider its own last-activity, shell, reminder, and hard-ceiling-ms before acting. Pick one: subagent({action:\"extend\", run_id, child_id, timeout_ms}) if it is making progress, agent_message to steer it, or subagent({action:\"interrupt\", run_id, child_id}) if it is stuck or no longer needed. If you do none of these it keeps running and the reminder comes again.\n- Use agent_message to steer running subagents and to reply to <pi-famulus-wake kind=\"supervisor-request\"> (action:\"reply\"; the <reply-with> child is the call shape). Do not use agent_message to resume a finished child — that is subagent({action:\"resume\"}). Supervisor wakes are not user messages — still act on them.",
9
+ "guidelines.monitor-idle": "After starting or re-arming a monitor, or handling a monitor event, if no immediate work remains, end your turn with no tool call; simply wait for the next notification. Handling an event does not require a user-visible reply. Do not repeat unchanged/intermediate status or narrate your decision to stay quiet; notify the user when their requested condition is met, their input is needed, or a failure prevents further progress. Read original output with task_output only when details are needed (including paging a truncated preview), never to poll or wait. Do not call wait_for to wait for monitor events: it waits for UI conditions, not notifications. Do not invent a stateId or make a failing tool call to yield.",
10
10
  "guidelines.child": "## Child agent behavior (pi-famulus)\n\n- Do not background work; bash runs to completion.\n- When blocked, use contact_supervisor with need_decision; use progress_update for status.\n- Use agent_message only to communicate with siblings in this same run.\n- Your final message is your result.",
11
11
  "tools.agent_message.description": "Message your subagents. \"send\" steers or queues a running child. Sending to a finished child errors (it does not resume) and points at subagent({ action: \"resume\", run_id, child_id, message }). \"reply\" answers a child's pending decision request; \"broadcast\" steers every running child of a run (to = run_id); \"list\" shows children, pending decision requests, and recent traffic.",
12
12
  "tools.agent_message.snippet": "Send/reply/broadcast messages to subagents",
@@ -34,14 +34,16 @@ export const PROMPTS = {
34
34
  "tools.child.contact_supervisor.snippet": "Ask the parent agent for a decision or report progress",
35
35
  "tools.child.contact_supervisor.param.reason": "\"need_decision\" blocks until the supervisor (parent agent) replies; \"progress_update\" is fire-and-forget.",
36
36
  "tools.child.contact_supervisor.param.message": "Message for the supervisor.",
37
- "tools.monitor.description": "Start a background monitor process whose stdout lines are injected back to you as <pi-famulus-wake kind=\"monitor\"> messages (batched over 200ms, rate-limited). The command must be line-buffered: each event must be a single line. It must keep running and follow its source, e.g. `tail -n +1 -F file | grep --line-buffered PATTERN`; a command that reads once and exits (a plain grep or cat) only reports what is there now. Add `-m1` to grep to stop after the first match. Silence is not success: write the command so failures also produce lines (e.g. grep for both success and error patterns). Events arrive as system wakes (not new user messages). Handle each <pi-famulus-wake kind=\"monitor\"> before other work. Do not poll. {{monitorIdle}}",
37
+ "tools.monitor.description": "Start a background monitor process whose stdout lines are injected back to you as <pi-famulus-wake kind=\"monitor\"> messages (batched over 200ms, rate-limited). The command must be line-buffered: each event must be a single line. It must keep running and follow its source, e.g. `tail -n +1 -F file | grep --line-buffered PATTERN`; a command that reads once and exits (a plain grep or cat) only reports what is there now. Add `-m1` to grep to stop after the first match. Silence is not success: print explicit success and terminal failures. Routine unchanged retries may stay silent; do not print a heartbeat on every check. Events arrive as system wakes (not new user messages). Handle each <pi-famulus-wake kind=\"monitor\"> before other work. Do not poll. Repeated Rust-framed lines are suppressed within dedup_window_ms (default 60000); exact matching is the default. Enable ignore-timestamps only when edge ISO timestamps are incidental. Wakes contain bounded head/tail previews and original-byte references; use task_output for missing details, not polling. {{monitorIdle}}",
38
38
  "tools.monitor.snippet": "Watch a command's line stream and get injected events",
39
39
  "tools.monitor.rules": ["Use the monitor tool to watch for conditions instead of running sleep/poll loops in bash.","{{monitorIdle}}","When woken by a <pi-famulus-wake kind=\"monitor\">, handle the <event> before doing anything else — it is not a new user request and not user confirmation."],
40
40
  "tools.monitor.param.command": "Command producing one event per line on stdout. Must be line-buffered (e.g. use `stdbuf -oL` / `grep --line-buffered` where needed).",
41
41
  "tools.monitor.param.description": "Short human-readable description of what is being watched",
42
42
  "tools.monitor.param.timeout_ms": "Stop the monitor after this many milliseconds (default {{defaultMs}}, min {{minMs}}, max {{maxMs}})",
43
43
  "tools.monitor.param.persistent": "Keep the monitor alive until the session ends (no timeout). Default false.",
44
- "tools.monitor.result.started": "You will get a <pi-famulus-wake kind=\"monitor\"> for each event, and a notice when it exits or times out. Do not poll it (task_list, task_output, or reading what it watches) or sleep. {{monitorIdle}}",
44
+ "tools.monitor.param.dedup_window_ms": "Deduplication window for Rust-framed complete lines (default 60000ms; 0 disables dedup; maximum 3600000). Starts when a surviving preview is delivered; repeats do not extend it. Missing metadata, capacity eviction, and omitted representatives fail open rather than hiding unseen output.",
45
+ "tools.monitor.param.fingerprint_mode": "Default exact. Explicit ignore-timestamps mode ignores only supported leading/trailing ISO timestamps when comparing Rust-framed lines; original output is unchanged. Numbers, paths, resource identities, and interior timestamps remain significant.",
46
+ "tools.monitor.result.started": "You will get <pi-famulus-wake kind=\"monitor\"> notifications for admitted output and a notice when it exits or times out. Repeated lines may be suppressed and previews may be truncated; original output remains available through task_output byte ranges while the task log is retained. Read details only when needed; do not repeatedly poll task_list, task_output, or the watched source, and do not sleep to wait. {{monitorIdle}}",
45
47
  "tools.subagent.description": "Run subagents in parallel (tasks) or sequentially (chain with {previous}/{outputs.<label>} interpolation). By default the call waits up to a foreground budget (default 45s); longer runs continue in the background. Each child that finishes while others are still running wakes you with <pi-famulus-wake kind=\"subagent-handover\"> (its prompt and result). The whole run wakes you with <pi-famulus-wake kind=\"subagent-done\">. Never poll or sleep to wait. A subagent still running past its timeout_ms is not stopped; it wakes you with <pi-famulus-wake kind=\"subagent-overrun\"> so you can extend, steer, or interrupt it. Use action=list/get/status/interrupt/resume/steer/extend to manage existing runs.",
46
48
  "tools.subagent.snippet": "Fan out subagents in parallel or sequence them in a chain",
47
49
  "tools.subagent.rules": ["When a <pi-famulus-wake kind=\"subagent-handover\"> arrives, read <prompt> and <result> immediately and continue: subagent({action:\"resume\", run_id, child_id, message}) for that child, or agent_message to steer children that are still running. Do not wait for the rest of the run.","Subagent runs that exceed the foreground budget continue in the background; you are notified per finished child and again when the run completes — do not poll.","A failed subagent does not fail the whole run; inspect per-subagent sections in the result.","<pi-famulus-wake> is a system wake, not a user reply. kind=subagent-handover is one child; kind=subagent-done is the whole run."],
@@ -72,17 +74,18 @@ export const PROMPTS = {
72
74
  "tools.task_list.snippet": "List background shell/monitor tasks and subagents",
73
75
  "tools.task_list.param.all": "Also include finished work (default: running only). Always limited to this session.",
74
76
  "tools.task_list.hint.running": "Running work wakes you with a <pi-famulus-wake> when it finishes. Reply to the user now with no tool call instead of checking again.",
75
- "tools.task_output.description": "Read output of a background task. Without a cursor, returns the tail of the output plus the current file pointer; pass the returned next_cursor as cursor for incremental reads. Not a way to wait: a running task wakes you when it finishes, so reply with no tool call instead of calling this again.",
77
+ "tools.task_output.description": "Read original output of a background task, including a monitor's truncated or deduplicated output. Without a cursor, returns the tail plus the current file pointer; pass next_cursor as cursor to page forward. Use small max_bytes ranges for details. Reading a different range is allowed; while a task is running, recently read ranges may be rejected if the output size is unchanged. This is not a way to wait: when no further details are needed, end your turn with no tool call and resume on the next notification.",
76
78
  "tools.task_output.snippet": "Read background task output",
77
79
  "tools.task_output.param.task_id": "Task id, e.g. sh_a1b2c3d4",
78
80
  "tools.task_output.param.cursor": "Byte offset to read from (for incremental reads). Omit to read the tail of the output.",
79
- "tools.task_output.param.max_bytes": "Maximum bytes to return (default 65536)",
80
- "tools.task_output.hint.still-running": "The task is still running. Reply to the user now with no tool call; a <pi-famulus-wake> arrives when it finishes.",
81
+ "tools.task_output.param.max_bytes": "Maximum bytes to return (default 65536, capped at 1048576); use a smaller range to avoid flooding context. Zero selects the default.",
82
+ "tools.task_output.hint.still-running": "The task is still running. Read another byte range only if details are needed; otherwise end your turn with no tool call. Do not repeatedly check for progress; a <pi-famulus-wake> arrives when it finishes.",
81
83
  "tools.task_output.error.no-new-output": "No new output from {{taskId}} since your last read. {{hint}}",
82
84
  "tools.task_stop.description": "Stop a running background task (SIGTERM to the process group, SIGKILL after a 2s grace).",
83
85
  "tools.task_stop.snippet": "Stop a background task",
84
86
  "tools.task_stop.param.task_id": "Task id to stop, e.g. sh_a1b2c3d4",
85
87
  "wakes.lead-in": "System wake — not a new user message. Handle this <pi-famulus-wake> before other work.",
88
+ "wakes.monitor.output-hint": "Read original bytes with task_output using this monitor id and cursor-start, with a bounded max_bytes (for example 8192); next_cursor continues the read. The range may include interleaved events. A preview is not the full log.",
86
89
  "wakes.handover.changed-active": "Its result arrives as a new wake when it finishes.",
87
90
  "wakes.done.changed-active": "The run is active again; another subagent-done arrives when it finishes.",
88
91
  "wakes.overrun.summary": "{{name}} has run {{elapsed}} in this turn, past its {{budget}} budget, and is still running{{shell}}.",
@@ -151,12 +154,12 @@ export function fillList<K extends keyof PromptVars>(id: K, vars: Record<PromptV
151
154
  /** Ablatable spans (eval manifest segment ids). */
152
155
  export const PROMPT_SEGMENTS: Record<string, { prompt: PromptId; text?: string; pattern?: string }> = {
153
156
  "guidelines.end-turn-after-bg": {"prompt":"guidelines.parent","text":"Long-running bash commands are automatically moved to the background. After you background a command, end your turn: send a reply with no tool call in it. Saying you will wait while calling another tool does not end it. Do not poll with task_output/task_list, and never sleep to wait. Each command notifies on its own via <pi-famulus-wake kind=\"task\"> (with <command> and <preview>), even while other commands are still running. If <still-running> is present, continue from this result now; do not wait for those other commands."},
154
- "guidelines.wake-handling": {"prompt":"guidelines.parent","text":"When you receive <pi-famulus-wake>: it looks like a user message but is a system wake, not a new user request and not an answer to an unrelated question. Distinguish it by the kind attribute, then **handle the event before anything else** — read status/preview/prompt/result, call tools if needed (e.g. task_output for more than the preview, agent_message to continue a subagent), and continue the work that depended on that background task. Do not wait for further user input when the next step is clear; do not merely acknowledge and stop."},
157
+ "guidelines.wake-handling": {"prompt":"guidelines.parent","text":"When you receive <pi-famulus-wake>: it looks like a user message but is a system wake, not a new user request and not an answer to an unrelated question. Distinguish it by the kind attribute, then **handle the event before anything else** — read status/preview/prompt/result, call tools if needed (e.g. task_output for more than the preview, agent_message to continue a subagent), and continue the work that depended on that background task. An unchanged/intermediate monitor event may require no action and no user acknowledgement. Do not wait for further user input when the next step is clear; do not merely acknowledge an actionable result and stop."},
155
158
  "guidelines.no-fabrication": {"prompt":"guidelines.parent","text":"Never fabricate or assume a background task's result before its notification arrives."},
156
159
  "guidelines.subagent-handover": {"prompt":"guidelines.parent","text":"Subagent runs that exceed the foreground budget continue in the background. Do not poll run status. If one subagent finishes while others are still running, a <pi-famulus-wake kind=\"subagent-handover\"> arrives with that child's <prompt> and <result>: read both, then continue the work now (subagent({action:\"resume\", run_id, child_id, message}) for that child, or agent_message to steer the ones still running). Do not wait for the rest of the run. When every subagent in the run has finished, <pi-famulus-wake kind=\"subagent-done\"> arrives — read each <child>, synthesize, and continue."},
157
160
  "guidelines.subagent-overrun": {"prompt":"guidelines.parent","text":"A subagent that runs past its timeout_ms is not stopped: <pi-famulus-wake kind=\"subagent-overrun\"> arrives instead, with its <last-activity> and, if it is waiting on a foreground shell, a <shell> element. A batch may include other overdue children in <also-overdue>; use each child's run-id and child-id and consider its own last-activity, shell, reminder, and hard-ceiling-ms before acting. Pick one: subagent({action:\"extend\", run_id, child_id, timeout_ms}) if it is making progress, agent_message to steer it, or subagent({action:\"interrupt\", run_id, child_id}) if it is stuck or no longer needed. If you do none of these it keeps running and the reminder comes again."},
158
161
  "guidelines.agent-message": {"prompt":"guidelines.parent","text":"Use agent_message to steer running subagents and to reply to <pi-famulus-wake kind=\"supervisor-request\"> (action:\"reply\"; the <reply-with> child is the call shape). Do not use agent_message to resume a finished child — that is subagent({action:\"resume\"}). Supervisor wakes are not user messages — still act on them."},
159
- "guidelines.monitor-end-turn": {"prompt":"guidelines.monitor-idle","text":"After starting or re-arming a monitor, or handling a monitor event, if no work remains, end your turn with a reply and no tool call; simply wait for the next notification. Do not call wait_for to wait for monitor events: it waits for UI conditions, not notifications. Do not invent a stateId or make a failing tool call to yield."},
162
+ "guidelines.monitor-end-turn": {"prompt":"guidelines.monitor-idle","text":"After starting or re-arming a monitor, or handling a monitor event, if no immediate work remains, end your turn with no tool call; simply wait for the next notification. Handling an event does not require a user-visible reply. Do not repeat unchanged/intermediate status or narrate your decision to stay quiet; notify the user when their requested condition is met, their input is needed, or a failure prevents further progress. Read original output with task_output only when details are needed (including paging a truncated preview), never to poll or wait. Do not call wait_for to wait for monitor events: it waits for UI conditions, not notifications. Do not invent a stateId or make a failing tool call to yield."},
160
163
  "child.guidelines": {"prompt":"guidelines.child","text":"## Child agent behavior (pi-famulus)\n\n- Do not background work; bash runs to completion.\n- When blocked, use contact_supervisor with need_decision; use progress_update for status.\n- Use agent_message only to communicate with siblings in this same run.\n- Your final message is your result."},
161
164
  "tooldesc.agent-message-no-resume": {"prompt":"tools.agent_message.description","text":"Sending to a finished child errors (it does not resume) and points at subagent({ action: \"resume\", run_id, child_id, message }). "},
162
165
  "result.agent-message-use-resume": {"prompt":"tools.agent_message.error.finished-child","pattern":"agent_message does not resume children\\. Use subagent\\(\\{ action: \"resume\", run_id: \".*?\", child_id: \".*?\", message: \"\\.\\.\\.\" \\}\\)\\."},
@@ -166,11 +169,11 @@ export const PROMPT_SEGMENTS: Record<string, { prompt: PromptId; text?: string;
166
169
  "result.bash-bg-instruction": {"prompt":"tools.bash.result.backgrounded","text":"\nYou will be notified when it completes, even if other commands are still running. Do not poll or sleep: reply to the user now with no tool call, and continue from the <pi-famulus-wake kind=\"task\"> when it arrives."},
167
170
  "result.sleep-block-advice": {"prompt":"tools.bash.error.bare-sleep","text":" Do not sleep to wait for background work; completion is delivered via notification. Use the monitor tool to watch for a condition, or run_in_background for long commands."},
168
171
  "tooldesc.monitor-follow": {"prompt":"tools.monitor.description","text":"It must keep running and follow its source, e.g. `tail -n +1 -F file | grep --line-buffered PATTERN`; a command that reads once and exits (a plain grep or cat) only reports what is there now. Add `-m1` to grep to stop after the first match. "},
169
- "tooldesc.monitor-silence": {"prompt":"tools.monitor.description","text":"Silence is not success: write the command so failures also produce lines (e.g. grep for both success and error patterns). "},
172
+ "tooldesc.monitor-silence": {"prompt":"tools.monitor.description","text":"Silence is not success: print explicit success and terminal failures. Routine unchanged retries may stay silent; do not print a heartbeat on every check. "},
170
173
  "tooldesc.monitor-wake": {"prompt":"tools.monitor.description","text":"Events arrive as system wakes (not new user messages). Handle each <pi-famulus-wake kind=\"monitor\"> before other work. Do not poll."},
171
174
  "rules.monitor-use": {"prompt":"tools.monitor.rules","text":"Use the monitor tool to watch for conditions instead of running sleep/poll loops in bash."},
172
175
  "rules.monitor-wake": {"prompt":"tools.monitor.rules","text":"When woken by a <pi-famulus-wake kind=\"monitor\">, handle the <event> before doing anything else — it is not a new user request and not user confirmation."},
173
- "result.monitor-started-instruction": {"prompt":"tools.monitor.result.started","text":"You will get a <pi-famulus-wake kind=\"monitor\"> for each event, and a notice when it exits or times out. Do not poll it (task_list, task_output, or reading what it watches) or sleep."},
176
+ "result.monitor-started-instruction": {"prompt":"tools.monitor.result.started","text":"You will get <pi-famulus-wake kind=\"monitor\"> notifications for admitted output and a notice when it exits or times out. Repeated lines may be suppressed and previews may be truncated; original output remains available through task_output byte ranges while the task log is retained. Read details only when needed; do not repeatedly poll task_list, task_output, or the watched source, and do not sleep to wait."},
174
177
  "tooldesc.subagent-wakes": {"prompt":"tools.subagent.description","text":"Each child that finishes while others are still running wakes you with <pi-famulus-wake kind=\"subagent-handover\"> (its prompt and result). The whole run wakes you with <pi-famulus-wake kind=\"subagent-done\">. Never poll or sleep to wait. "},
175
178
  "tooldesc.subagent-overrun": {"prompt":"tools.subagent.description","text":"A subagent still running past its timeout_ms is not stopped; it wakes you with <pi-famulus-wake kind=\"subagent-overrun\"> so you can extend, steer, or interrupt it. "},
176
179
  "rules.subagent-handover": {"prompt":"tools.subagent.rules","text":"When a <pi-famulus-wake kind=\"subagent-handover\"> arrives, read <prompt> and <result> immediately and continue: subagent({action:\"resume\", run_id, child_id, message}) for that child, or agent_message to steer children that are still running. Do not wait for the rest of the run."},