@davidorex/pi-behavior-monitors 0.1.4 → 0.3.0

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/dist/index.js ADDED
@@ -0,0 +1,1456 @@
1
+ /**
2
+ * Behavior monitors for pi — watches agent activity, classifies against
3
+ * pattern libraries, steers corrections, and writes structured findings
4
+ * to JSON files for downstream consumption.
5
+ *
6
+ * Monitor definitions are JSON files (.monitor.json) with typed blocks:
7
+ * classify (LLM side-channel), patterns (JSON library), actions (steer + write).
8
+ * Patterns and instructions are JSON arrays conforming to schemas.
9
+ */
10
+ import { execSync } from "node:child_process";
11
+ import * as fs from "node:fs";
12
+ import * as os from "node:os";
13
+ import * as path from "node:path";
14
+ import { fileURLToPath } from "node:url";
15
+ import { complete } from "@mariozechner/pi-ai";
16
+ import { getAgentDir } from "@mariozechner/pi-coding-agent";
17
+ import { Box, Text } from "@mariozechner/pi-tui";
18
+ import { Type } from "@sinclair/typebox";
19
+ import nunjucks from "nunjucks";
20
+ const EXTENSION_DIR = path.dirname(fileURLToPath(import.meta.url));
21
+ const EXAMPLES_DIR = path.join(EXTENSION_DIR, "examples");
22
+ export const COLLECTOR_DESCRIPTORS = [
23
+ { name: "user_text", description: "Most recent user message text" },
24
+ { name: "assistant_text", description: "Most recent assistant message text" },
25
+ { name: "tool_results", description: "Tool results with tool name and error status", limits: "Last 5, truncated 2000 chars" },
26
+ { name: "tool_calls", description: "Tool calls and results interleaved", limits: "Last 20, truncated 2000 chars" },
27
+ { name: "custom_messages", description: "Custom extension messages since last user message" },
28
+ { name: "project_vision", description: ".project/project.json vision, core_value, name" },
29
+ { name: "project_conventions", description: ".project/conformance-reference.json principle names" },
30
+ { name: "git_status", description: "Output of git status --porcelain", limits: "5s timeout" },
31
+ ];
32
+ export const WHEN_CONDITIONS = [
33
+ { name: "always", description: "Fire every time the event occurs", parameterized: false },
34
+ { name: "has_tool_results", description: "Fire only if tool results present since last user message", parameterized: false },
35
+ { name: "has_file_writes", description: "Fire only if write or edit tool called since last user message", parameterized: false },
36
+ { name: "has_bash", description: "Fire only if bash tool called since last user message", parameterized: false },
37
+ { name: "every(N)", description: "Fire every Nth activation (counter resets when user text changes)", parameterized: true },
38
+ { name: "tool(name)", description: "Fire only if specific named tool called since last user message", parameterized: true },
39
+ ];
40
+ export const VERDICT_TYPES = ["clean", "flag", "new"];
41
+ export const SCOPE_TARGETS = ["main", "subagent", "all", "workflow"];
42
+ export const VALID_EVENTS = new Set(["message_end", "turn_end", "agent_end", "command"]);
43
+ function isValidEvent(event) {
44
+ return VALID_EVENTS.has(event);
45
+ }
46
+ // =============================================================================
47
+ // Discovery
48
+ // =============================================================================
49
+ function discoverMonitors() {
50
+ const dirs = [];
51
+ // project-local
52
+ let cwd = process.cwd();
53
+ while (true) {
54
+ const candidate = path.join(cwd, ".pi", "monitors");
55
+ if (isDir(candidate)) {
56
+ dirs.push(candidate);
57
+ break;
58
+ }
59
+ const parent = path.dirname(cwd);
60
+ if (parent === cwd)
61
+ break;
62
+ cwd = parent;
63
+ }
64
+ // global
65
+ const globalDir = path.join(getAgentDir(), "monitors");
66
+ if (isDir(globalDir))
67
+ dirs.push(globalDir);
68
+ const seen = new Map();
69
+ for (const dir of dirs) {
70
+ for (const file of listMonitorFiles(dir)) {
71
+ const monitor = parseMonitorJson(path.join(dir, file), dir);
72
+ if (monitor && !seen.has(monitor.name)) {
73
+ seen.set(monitor.name, monitor);
74
+ }
75
+ }
76
+ }
77
+ return Array.from(seen.values());
78
+ }
79
+ function isDir(p) {
80
+ try {
81
+ return fs.statSync(p).isDirectory();
82
+ }
83
+ catch {
84
+ return false;
85
+ }
86
+ }
87
+ function listMonitorFiles(dir) {
88
+ try {
89
+ return fs.readdirSync(dir).filter((f) => f.endsWith(".monitor.json"));
90
+ }
91
+ catch {
92
+ return [];
93
+ }
94
+ }
95
+ function parseMonitorJson(filePath, dir) {
96
+ let raw;
97
+ try {
98
+ raw = fs.readFileSync(filePath, "utf-8");
99
+ }
100
+ catch {
101
+ return null;
102
+ }
103
+ let spec;
104
+ try {
105
+ spec = JSON.parse(raw);
106
+ }
107
+ catch {
108
+ console.error(`[monitors] Failed to parse ${filePath}`);
109
+ return null;
110
+ }
111
+ const name = spec.name;
112
+ if (!name)
113
+ return null;
114
+ const event = String(spec.event ?? "message_end");
115
+ if (!isValidEvent(event)) {
116
+ console.error(`[${name}] Invalid event: ${event}. Must be one of: ${[...VALID_EVENTS].join(", ")}`);
117
+ return null;
118
+ }
119
+ const classify = spec.classify;
120
+ if (!classify?.prompt && !classify?.promptTemplate) {
121
+ console.error(`[${name}] Missing classify.prompt or classify.promptTemplate`);
122
+ return null;
123
+ }
124
+ const patternsSpec = spec.patterns;
125
+ if (!patternsSpec?.path) {
126
+ console.error(`[${name}] Missing patterns.path`);
127
+ return null;
128
+ }
129
+ const scope = spec.scope;
130
+ const instructions = spec.instructions;
131
+ const actions = spec.actions;
132
+ return {
133
+ name,
134
+ description: String(spec.description ?? ""),
135
+ event: event,
136
+ when: String(spec.when ?? "always"),
137
+ scope: scope ?? { target: "main" },
138
+ classify: {
139
+ model: classify.model ?? "claude-sonnet-4-20250514",
140
+ context: Array.isArray(classify.context) ? classify.context : ["tool_results", "assistant_text"],
141
+ excludes: Array.isArray(classify.excludes) ? classify.excludes : [],
142
+ prompt: classify.prompt ?? "",
143
+ promptTemplate: typeof classify.promptTemplate === "string" ? classify.promptTemplate : undefined,
144
+ },
145
+ patterns: {
146
+ path: patternsSpec.path,
147
+ learn: patternsSpec.learn !== false,
148
+ },
149
+ instructions: {
150
+ path: instructions?.path ?? `${name}.instructions.json`,
151
+ },
152
+ actions: actions ?? {},
153
+ ceiling: Number(spec.ceiling) || 5,
154
+ escalate: spec.escalate === "dismiss" ? "dismiss" : "ask",
155
+ dir,
156
+ resolvedPatternsPath: path.resolve(dir, patternsSpec.path),
157
+ resolvedInstructionsPath: path.resolve(dir, instructions?.path ?? `${name}.instructions.json`),
158
+ // runtime state
159
+ activationCount: 0,
160
+ whileCount: 0,
161
+ lastUserText: "",
162
+ dismissed: false,
163
+ };
164
+ }
165
+ // =============================================================================
166
+ // Example seeding
167
+ // =============================================================================
168
+ function resolveProjectMonitorsDir() {
169
+ let cwd = process.cwd();
170
+ while (true) {
171
+ const piDir = path.join(cwd, ".pi");
172
+ if (isDir(piDir))
173
+ return path.join(piDir, "monitors");
174
+ const parent = path.dirname(cwd);
175
+ if (parent === cwd)
176
+ break;
177
+ cwd = parent;
178
+ }
179
+ return path.join(process.cwd(), ".pi", "monitors");
180
+ }
181
+ function seedExamples() {
182
+ if (discoverMonitors().length > 0)
183
+ return 0;
184
+ if (!isDir(EXAMPLES_DIR))
185
+ return 0;
186
+ const targetDir = resolveProjectMonitorsDir();
187
+ fs.mkdirSync(targetDir, { recursive: true });
188
+ if (listMonitorFiles(targetDir).length > 0)
189
+ return 0;
190
+ const files = fs.readdirSync(EXAMPLES_DIR).filter((f) => f.endsWith(".json"));
191
+ let copied = 0;
192
+ for (const file of files) {
193
+ const dest = path.join(targetDir, file);
194
+ if (!fs.existsSync(dest)) {
195
+ fs.copyFileSync(path.join(EXAMPLES_DIR, file), dest);
196
+ copied++;
197
+ }
198
+ }
199
+ return copied;
200
+ }
201
+ // =============================================================================
202
+ // Context collection
203
+ // =============================================================================
204
+ const TRUNCATE = 2000;
205
+ function extractText(parts) {
206
+ return parts
207
+ .filter((b) => b.type === "text")
208
+ .map((b) => b.text)
209
+ .join("");
210
+ }
211
+ function extractUserText(parts) {
212
+ if (typeof parts === "string")
213
+ return parts;
214
+ if (!Array.isArray(parts))
215
+ return "";
216
+ return parts
217
+ .filter((b) => b.type === "text")
218
+ .map((b) => b.text)
219
+ .join("");
220
+ }
221
+ function trunc(text) {
222
+ return text.length <= TRUNCATE ? text : `${text.slice(0, TRUNCATE)} [TRUNCATED]`;
223
+ }
224
+ function isMessageEntry(entry) {
225
+ return entry.type === "message";
226
+ }
227
+ function collectUserText(branch) {
228
+ let foundAssistant = false;
229
+ for (let i = branch.length - 1; i >= 0; i--) {
230
+ const entry = branch[i];
231
+ if (!isMessageEntry(entry))
232
+ continue;
233
+ if (!foundAssistant) {
234
+ if (entry.message.role === "assistant")
235
+ foundAssistant = true;
236
+ continue;
237
+ }
238
+ if (entry.message.role === "user")
239
+ return extractUserText(entry.message.content);
240
+ }
241
+ return "";
242
+ }
243
+ function collectAssistantText(branch) {
244
+ for (let i = branch.length - 1; i >= 0; i--) {
245
+ const entry = branch[i];
246
+ if (isMessageEntry(entry) && entry.message.role === "assistant") {
247
+ return extractText(entry.message.content);
248
+ }
249
+ }
250
+ return "";
251
+ }
252
+ function collectToolResults(branch, limit = 5) {
253
+ const results = [];
254
+ for (let i = branch.length - 1; i >= 0 && results.length < limit; i--) {
255
+ const entry = branch[i];
256
+ if (!isMessageEntry(entry) || entry.message.role !== "toolResult")
257
+ continue;
258
+ const text = extractUserText(entry.message.content);
259
+ if (text)
260
+ results.push(`---\n[${entry.message.toolName}${entry.message.isError ? " ERROR" : ""}] ${trunc(text)}\n---`);
261
+ }
262
+ return results.reverse().join("\n");
263
+ }
264
+ function collectToolCalls(branch, limit = 20) {
265
+ const calls = [];
266
+ for (let i = branch.length - 1; i >= 0 && calls.length < limit; i--) {
267
+ const entry = branch[i];
268
+ if (!isMessageEntry(entry))
269
+ continue;
270
+ const msg = entry.message;
271
+ if (msg.role === "assistant") {
272
+ for (const part of msg.content) {
273
+ if (part.type === "toolCall") {
274
+ calls.push(`[call ${part.name}] ${trunc(JSON.stringify(part.arguments ?? {}))}`);
275
+ }
276
+ }
277
+ }
278
+ if (msg.role === "toolResult") {
279
+ calls.push(`[result ${msg.toolName}${msg.isError ? " ERROR" : ""}] ${trunc(extractUserText(msg.content))}`);
280
+ }
281
+ }
282
+ return calls.reverse().join("\n");
283
+ }
284
+ function collectCustomMessages(branch) {
285
+ const msgs = [];
286
+ for (let i = branch.length - 1; i >= 0; i--) {
287
+ const entry = branch[i];
288
+ if (!isMessageEntry(entry))
289
+ continue;
290
+ if (entry.message.role === "user")
291
+ break;
292
+ const msg = entry.message;
293
+ if (msg.customType) {
294
+ msgs.unshift(`[${msg.customType}] ${msg.content ?? ""}`);
295
+ }
296
+ }
297
+ return msgs.join("\n");
298
+ }
299
+ function collectProjectVision(_branch) {
300
+ try {
301
+ const projectPath = path.join(process.cwd(), ".project", "project.json");
302
+ const raw = JSON.parse(fs.readFileSync(projectPath, "utf-8"));
303
+ const parts = [];
304
+ if (raw.vision)
305
+ parts.push(`Vision: ${raw.vision}`);
306
+ if (raw.core_value)
307
+ parts.push(`Core value: ${raw.core_value}`);
308
+ if (raw.name)
309
+ parts.push(`Project: ${raw.name}`);
310
+ return parts.join("\n");
311
+ }
312
+ catch {
313
+ return "";
314
+ }
315
+ }
316
+ function collectProjectConventions(_branch) {
317
+ try {
318
+ const confPath = path.join(process.cwd(), ".project", "conformance-reference.json");
319
+ const raw = JSON.parse(fs.readFileSync(confPath, "utf-8"));
320
+ if (Array.isArray(raw.items)) {
321
+ return raw.items.map((item) => `- ${item.name ?? item.id}`).join("\n");
322
+ }
323
+ return "";
324
+ }
325
+ catch {
326
+ return "";
327
+ }
328
+ }
329
+ function collectGitStatus(_branch) {
330
+ try {
331
+ return execSync("git status --porcelain", { cwd: process.cwd(), encoding: "utf-8", timeout: 5000 }).trim();
332
+ }
333
+ catch {
334
+ return "";
335
+ }
336
+ }
337
+ const collectors = {
338
+ user_text: collectUserText,
339
+ assistant_text: collectAssistantText,
340
+ tool_results: collectToolResults,
341
+ tool_calls: collectToolCalls,
342
+ custom_messages: collectCustomMessages,
343
+ project_vision: collectProjectVision,
344
+ project_conventions: collectProjectConventions,
345
+ git_status: collectGitStatus,
346
+ };
347
+ /** Collector names derived from the runtime registry — used for consistency testing. */
348
+ export const COLLECTOR_NAMES = Object.keys(collectors);
349
+ function hasToolResults(branch) {
350
+ for (let i = branch.length - 1; i >= 0; i--) {
351
+ const entry = branch[i];
352
+ if (!isMessageEntry(entry))
353
+ continue;
354
+ if (entry.message.role === "user")
355
+ break;
356
+ if (entry.message.role === "toolResult")
357
+ return true;
358
+ }
359
+ return false;
360
+ }
361
+ function hasToolNamed(branch, name) {
362
+ for (let i = branch.length - 1; i >= 0; i--) {
363
+ const entry = branch[i];
364
+ if (!isMessageEntry(entry))
365
+ continue;
366
+ if (entry.message.role === "user")
367
+ break;
368
+ if (entry.message.role === "assistant") {
369
+ for (const part of entry.message.content) {
370
+ if (part.type === "toolCall" && part.name === name)
371
+ return true;
372
+ }
373
+ }
374
+ }
375
+ return false;
376
+ }
377
+ // =============================================================================
378
+ // When evaluation
379
+ // =============================================================================
380
+ function evaluateWhen(monitor, branch) {
381
+ const w = monitor.when;
382
+ if (w === "always")
383
+ return true;
384
+ if (w === "has_tool_results")
385
+ return hasToolResults(branch);
386
+ if (w === "has_file_writes")
387
+ return hasToolNamed(branch, "write") || hasToolNamed(branch, "edit");
388
+ if (w === "has_bash")
389
+ return hasToolNamed(branch, "bash");
390
+ const everyMatch = w.match(/^every\((\d+)\)$/);
391
+ if (everyMatch) {
392
+ const n = parseInt(everyMatch[1]);
393
+ const userText = collectUserText(branch);
394
+ if (userText !== monitor.lastUserText) {
395
+ monitor.activationCount = 0;
396
+ monitor.lastUserText = userText;
397
+ }
398
+ monitor.activationCount++;
399
+ if (monitor.activationCount >= n) {
400
+ monitor.activationCount = 0;
401
+ return true;
402
+ }
403
+ return false;
404
+ }
405
+ const toolMatch = w.match(/^tool\((\w+)\)$/);
406
+ if (toolMatch)
407
+ return hasToolNamed(branch, toolMatch[1]);
408
+ return true;
409
+ }
410
+ // =============================================================================
411
+ // Template rendering (JSON patterns → text for LLM prompt)
412
+ // =============================================================================
413
+ function loadPatterns(monitor) {
414
+ try {
415
+ const raw = fs.readFileSync(monitor.resolvedPatternsPath, "utf-8");
416
+ return JSON.parse(raw);
417
+ }
418
+ catch {
419
+ return [];
420
+ }
421
+ }
422
+ function formatPatternsForPrompt(patterns) {
423
+ return patterns.map((p, i) => `${i + 1}. [${p.severity ?? "warning"}] ${p.description}`).join("\n");
424
+ }
425
+ function loadInstructions(monitor) {
426
+ try {
427
+ const raw = fs.readFileSync(monitor.resolvedInstructionsPath, "utf-8");
428
+ return JSON.parse(raw);
429
+ }
430
+ catch {
431
+ return [];
432
+ }
433
+ }
434
+ function saveInstructions(monitor, instructions) {
435
+ const tmpPath = `${monitor.resolvedInstructionsPath}.${process.pid}.tmp`;
436
+ try {
437
+ fs.writeFileSync(tmpPath, JSON.stringify(instructions, null, 2) + "\n");
438
+ fs.renameSync(tmpPath, monitor.resolvedInstructionsPath);
439
+ return null;
440
+ }
441
+ catch (err) {
442
+ try {
443
+ fs.unlinkSync(tmpPath);
444
+ }
445
+ catch {
446
+ /* cleanup */
447
+ }
448
+ return err instanceof Error ? err.message : String(err);
449
+ }
450
+ }
451
+ export function parseMonitorsArgs(args, knownNames) {
452
+ const trimmed = args.trim();
453
+ if (!trimmed)
454
+ return { type: "list" };
455
+ const tokens = trimmed.split(/\s+/);
456
+ const first = tokens[0];
457
+ // global commands (only if not a monitor name)
458
+ if (!knownNames.has(first)) {
459
+ if (first === "on")
460
+ return { type: "on" };
461
+ if (first === "off")
462
+ return { type: "off" };
463
+ return { type: "error", message: `Unknown monitor: ${first}\nAvailable: ${[...knownNames].join(", ")}` };
464
+ }
465
+ const name = first;
466
+ if (tokens.length === 1)
467
+ return { type: "inspect", name };
468
+ const verb = tokens[1];
469
+ if (verb === "rules") {
470
+ if (tokens.length === 2)
471
+ return { type: "rules-list", name };
472
+ const action = tokens[2];
473
+ if (action === "add") {
474
+ const text = tokens.slice(3).join(" ");
475
+ if (!text)
476
+ return { type: "error", message: "Usage: /monitors <name> rules add <text>" };
477
+ return { type: "rules-add", name, text };
478
+ }
479
+ if (action === "remove") {
480
+ const n = parseInt(tokens[3]);
481
+ if (isNaN(n) || n < 1)
482
+ return { type: "error", message: "Usage: /monitors <name> rules remove <number>" };
483
+ return { type: "rules-remove", name, index: n };
484
+ }
485
+ if (action === "replace") {
486
+ const n = parseInt(tokens[3]);
487
+ const text = tokens.slice(4).join(" ");
488
+ if (isNaN(n) || n < 1 || !text)
489
+ return { type: "error", message: "Usage: /monitors <name> rules replace <number> <text>" };
490
+ return { type: "rules-replace", name, index: n, text };
491
+ }
492
+ return { type: "error", message: `Unknown rules action: ${action}\nAvailable: add, remove, replace` };
493
+ }
494
+ if (verb === "patterns")
495
+ return { type: "patterns-list", name };
496
+ if (verb === "dismiss")
497
+ return { type: "dismiss", name };
498
+ if (verb === "reset")
499
+ return { type: "reset", name };
500
+ return { type: "error", message: `Unknown subcommand: ${verb}\nAvailable: rules, patterns, dismiss, reset` };
501
+ }
502
+ function handleList(monitors, ctx, enabled) {
503
+ const header = enabled ? "monitors: ON" : "monitors: OFF (all monitoring paused)";
504
+ const lines = monitors.map((m) => {
505
+ const state = m.dismissed ? "dismissed" : m.whileCount > 0 ? `engaged (${m.whileCount}/${m.ceiling})` : "idle";
506
+ const scope = m.scope.target !== "main" ? ` [scope:${m.scope.target}]` : "";
507
+ return ` ${m.name} [${m.event}${m.when !== "always" ? `, when: ${m.when}` : ""}]${scope} — ${state}`;
508
+ });
509
+ ctx.ui.notify(`${header}\n${lines.join("\n")}`, "info");
510
+ }
511
+ function handleInspect(monitor, ctx) {
512
+ const rules = loadInstructions(monitor);
513
+ const patterns = loadPatterns(monitor);
514
+ const state = monitor.dismissed
515
+ ? "dismissed"
516
+ : monitor.whileCount > 0
517
+ ? `engaged (${monitor.whileCount}/${monitor.ceiling})`
518
+ : "idle";
519
+ const lines = [
520
+ `[${monitor.name}] ${monitor.description}`,
521
+ `event: ${monitor.event}, when: ${monitor.when}, scope: ${monitor.scope.target}`,
522
+ `state: ${state}, ceiling: ${monitor.ceiling}, escalate: ${monitor.escalate}`,
523
+ `rules: ${rules.length}, patterns: ${patterns.length}`,
524
+ ];
525
+ ctx.ui.notify(lines.join("\n"), "info");
526
+ }
527
+ function handleRulesList(monitor, ctx) {
528
+ const rules = loadInstructions(monitor);
529
+ if (rules.length === 0) {
530
+ ctx.ui.notify(`[${monitor.name}] (no rules)`, "info");
531
+ return;
532
+ }
533
+ const lines = rules.map((r, i) => `${i + 1}. ${r.text}`);
534
+ ctx.ui.notify(`[${monitor.name}] rules:\n${lines.join("\n")}`, "info");
535
+ }
536
+ function handleRulesAdd(monitor, ctx, text) {
537
+ const rules = loadInstructions(monitor);
538
+ rules.push({ text, added_at: new Date().toISOString() });
539
+ const err = saveInstructions(monitor, rules);
540
+ if (err) {
541
+ ctx.ui.notify(`[${monitor.name}] Failed to save: ${err}`, "error");
542
+ }
543
+ else {
544
+ ctx.ui.notify(`[${monitor.name}] Rule added: ${text}`, "info");
545
+ }
546
+ }
547
+ function handleRulesRemove(monitor, ctx, index) {
548
+ const rules = loadInstructions(monitor);
549
+ if (index < 1 || index > rules.length) {
550
+ ctx.ui.notify(`[${monitor.name}] Invalid index ${index}. Have ${rules.length} rules.`, "error");
551
+ return;
552
+ }
553
+ const removed = rules.splice(index - 1, 1)[0];
554
+ const err = saveInstructions(monitor, rules);
555
+ if (err) {
556
+ ctx.ui.notify(`[${monitor.name}] Failed to save: ${err}`, "error");
557
+ }
558
+ else {
559
+ ctx.ui.notify(`[${monitor.name}] Removed rule ${index}: ${removed.text}`, "info");
560
+ }
561
+ }
562
+ function handleRulesReplace(monitor, ctx, index, text) {
563
+ const rules = loadInstructions(monitor);
564
+ if (index < 1 || index > rules.length) {
565
+ ctx.ui.notify(`[${monitor.name}] Invalid index ${index}. Have ${rules.length} rules.`, "error");
566
+ return;
567
+ }
568
+ const old = rules[index - 1].text;
569
+ rules[index - 1] = { text, added_at: new Date().toISOString() };
570
+ const err = saveInstructions(monitor, rules);
571
+ if (err) {
572
+ ctx.ui.notify(`[${monitor.name}] Failed to save: ${err}`, "error");
573
+ }
574
+ else {
575
+ ctx.ui.notify(`[${monitor.name}] Replaced rule ${index}:\n was: ${old}\n now: ${text}`, "info");
576
+ }
577
+ }
578
+ function handlePatternsList(monitor, ctx) {
579
+ const patterns = loadPatterns(monitor);
580
+ if (patterns.length === 0) {
581
+ ctx.ui.notify(`[${monitor.name}] (no patterns — monitor will not classify)`, "info");
582
+ return;
583
+ }
584
+ const lines = patterns.map((p, i) => {
585
+ const source = p.source ? ` (${p.source})` : "";
586
+ return `${i + 1}. [${p.severity ?? "warning"}] ${p.description}${source}`;
587
+ });
588
+ ctx.ui.notify(`[${monitor.name}] patterns:\n${lines.join("\n")}`, "info");
589
+ }
590
+ function formatInstructionsForPrompt(instructions) {
591
+ if (instructions.length === 0)
592
+ return "";
593
+ const lines = instructions.map((i) => `- ${i.text}`).join("\n");
594
+ return `\nOperating instructions from the user (follow these strictly):\n${lines}\n`;
595
+ }
596
+ /**
597
+ * Create a Nunjucks environment for monitor prompt templates.
598
+ * Three-tier search: project monitors dir > user monitors dir > package examples.
599
+ */
600
+ function createMonitorTemplateEnv() {
601
+ const projectDir = resolveProjectMonitorsDir();
602
+ const userDir = path.join(os.homedir(), ".pi", "agent", "monitors");
603
+ const searchPaths = [];
604
+ if (isDir(projectDir))
605
+ searchPaths.push(projectDir);
606
+ if (isDir(userDir))
607
+ searchPaths.push(userDir);
608
+ if (isDir(EXAMPLES_DIR))
609
+ searchPaths.push(EXAMPLES_DIR);
610
+ const loader = searchPaths.length > 0 ? new nunjucks.FileSystemLoader(searchPaths) : undefined;
611
+ return new nunjucks.Environment(loader, {
612
+ autoescape: false,
613
+ throwOnUndefined: false,
614
+ });
615
+ }
616
+ /** Module-level template environment, initialized in extension entry point. */
617
+ let monitorTemplateEnv;
618
+ function renderClassifyPrompt(monitor, branch) {
619
+ const patterns = loadPatterns(monitor);
620
+ if (patterns.length === 0)
621
+ return null;
622
+ const instructions = loadInstructions(monitor);
623
+ const collected = {};
624
+ for (const key of monitor.classify.context) {
625
+ const fn = collectors[key];
626
+ if (fn)
627
+ collected[key] = fn(branch);
628
+ else
629
+ collected[key] = ""; // unknown collectors produce empty string (graceful degradation)
630
+ }
631
+ const context = {
632
+ patterns: formatPatternsForPrompt(patterns),
633
+ instructions: formatInstructionsForPrompt(instructions),
634
+ iteration: monitor.whileCount,
635
+ ...collected,
636
+ };
637
+ if (monitor.classify.promptTemplate && monitorTemplateEnv) {
638
+ // Nunjucks template file
639
+ try {
640
+ return monitorTemplateEnv.render(monitor.classify.promptTemplate, context);
641
+ }
642
+ catch (err) {
643
+ const msg = err instanceof Error ? err.message : String(err);
644
+ console.error(`[${monitor.name}] Template render failed (${monitor.classify.promptTemplate}): ${msg}`);
645
+ // Fall through to inline prompt if available
646
+ if (!monitor.classify.prompt)
647
+ return null;
648
+ }
649
+ }
650
+ // Fallback: inline string with {placeholder} replacement
651
+ if (!monitor.classify.prompt)
652
+ return null;
653
+ return monitor.classify.prompt.replace(/\{(\w+)\}/g, (match, key) => {
654
+ return String(context[key] ?? match);
655
+ });
656
+ }
657
+ // =============================================================================
658
+ // Classification
659
+ // =============================================================================
660
+ export function parseVerdict(raw) {
661
+ const text = raw.trim();
662
+ if (text.startsWith("CLEAN"))
663
+ return { verdict: "clean" };
664
+ if (text.startsWith("NEW:")) {
665
+ const rest = text.slice(4);
666
+ const pipe = rest.indexOf("|");
667
+ if (pipe !== -1)
668
+ return { verdict: "new", newPattern: rest.slice(0, pipe).trim(), description: rest.slice(pipe + 1).trim() };
669
+ return { verdict: "new", newPattern: rest.trim(), description: rest.trim() };
670
+ }
671
+ if (text.startsWith("FLAG:"))
672
+ return { verdict: "flag", description: text.slice(5).trim() };
673
+ return { verdict: "clean" };
674
+ }
675
+ export function parseModelSpec(spec) {
676
+ const slashIndex = spec.indexOf("/");
677
+ if (slashIndex !== -1) {
678
+ return { provider: spec.slice(0, slashIndex), modelId: spec.slice(slashIndex + 1) };
679
+ }
680
+ return { provider: "anthropic", modelId: spec };
681
+ }
682
+ async function classifyPrompt(ctx, monitor, prompt, signal) {
683
+ const { provider, modelId } = parseModelSpec(monitor.classify.model);
684
+ const model = ctx.modelRegistry.find(provider, modelId);
685
+ if (!model)
686
+ throw new Error(`Model ${monitor.classify.model} not found`);
687
+ const apiKey = await ctx.modelRegistry.getApiKey(model);
688
+ if (!apiKey)
689
+ throw new Error(`No API key for ${monitor.classify.model}`);
690
+ const response = await complete(model, { messages: [{ role: "user", content: [{ type: "text", text: prompt }], timestamp: Date.now() }] }, { apiKey, maxTokens: 150, signal });
691
+ return parseVerdict(extractText(response.content));
692
+ }
693
+ // =============================================================================
694
+ // Pattern learning (JSON)
695
+ // =============================================================================
696
+ function learnPattern(monitor, description) {
697
+ const patterns = loadPatterns(monitor);
698
+ const id = description
699
+ .toLowerCase()
700
+ .replace(/[^a-z0-9]+/g, "-")
701
+ .slice(0, 60);
702
+ // dedup by description
703
+ if (patterns.some((p) => p.description === description))
704
+ return;
705
+ patterns.push({
706
+ id,
707
+ description,
708
+ severity: "warning",
709
+ source: "learned",
710
+ learned_at: new Date().toISOString(),
711
+ });
712
+ const tmpPath = `${monitor.resolvedPatternsPath}.${process.pid}.tmp`;
713
+ try {
714
+ fs.writeFileSync(tmpPath, JSON.stringify(patterns, null, 2) + "\n");
715
+ fs.renameSync(tmpPath, monitor.resolvedPatternsPath);
716
+ }
717
+ catch (err) {
718
+ try {
719
+ fs.unlinkSync(tmpPath);
720
+ }
721
+ catch {
722
+ /* cleanup */
723
+ }
724
+ console.error(`[${monitor.name}] Failed to write pattern: ${err instanceof Error ? err.message : err}`);
725
+ }
726
+ }
727
+ // =============================================================================
728
+ // Action execution — write findings to JSON files
729
+ // =============================================================================
730
+ export function generateFindingId(monitorName, _description) {
731
+ return `${monitorName}-${Date.now().toString(36)}`;
732
+ }
733
+ function executeWriteAction(monitor, action, result) {
734
+ if (!action.write)
735
+ return;
736
+ const writeCfg = action.write;
737
+ const filePath = path.isAbsolute(writeCfg.path) ? writeCfg.path : path.resolve(process.cwd(), writeCfg.path);
738
+ // Build the entry from template, substituting placeholders
739
+ const findingId = generateFindingId(monitor.name, result.description ?? "unknown");
740
+ const entry = {};
741
+ for (const [key, tmpl] of Object.entries(writeCfg.template)) {
742
+ entry[key] = String(tmpl)
743
+ .replace(/\{finding_id\}/g, findingId)
744
+ .replace(/\{description\}/g, result.description ?? "Issue detected")
745
+ .replace(/\{severity\}/g, "warning")
746
+ .replace(/\{monitor_name\}/g, monitor.name)
747
+ .replace(/\{timestamp\}/g, new Date().toISOString());
748
+ }
749
+ // Read existing file or create structure
750
+ let data = {};
751
+ try {
752
+ data = JSON.parse(fs.readFileSync(filePath, "utf-8"));
753
+ }
754
+ catch {
755
+ // file doesn't exist or is invalid — create fresh
756
+ }
757
+ const arrayField = writeCfg.array_field;
758
+ if (!Array.isArray(data[arrayField])) {
759
+ data[arrayField] = [];
760
+ }
761
+ const arr = data[arrayField];
762
+ if (writeCfg.merge === "upsert") {
763
+ const idx = arr.findIndex((item) => item.id === entry.id);
764
+ if (idx !== -1) {
765
+ arr[idx] = entry;
766
+ }
767
+ else {
768
+ arr.push(entry);
769
+ }
770
+ }
771
+ else {
772
+ arr.push(entry);
773
+ }
774
+ const tmpPath = `${filePath}.${process.pid}.tmp`;
775
+ try {
776
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
777
+ fs.writeFileSync(tmpPath, JSON.stringify(data, null, 2) + "\n");
778
+ fs.renameSync(tmpPath, filePath);
779
+ }
780
+ catch (err) {
781
+ try {
782
+ fs.unlinkSync(tmpPath);
783
+ }
784
+ catch {
785
+ /* cleanup */
786
+ }
787
+ console.error(`[${monitor.name}] Failed to write to ${filePath}: ${err instanceof Error ? err.message : err}`);
788
+ }
789
+ }
790
+ // =============================================================================
791
+ // Activation
792
+ // =============================================================================
793
+ let monitorsEnabled = true;
794
+ async function activate(monitor, pi, ctx, branch, steeredThisTurn, updateStatus, pendingAgentEndSteers) {
795
+ if (!monitorsEnabled)
796
+ return;
797
+ if (monitor.dismissed)
798
+ return;
799
+ // check excludes
800
+ for (const ex of monitor.classify.excludes) {
801
+ if (steeredThisTurn.has(ex))
802
+ return;
803
+ }
804
+ if (!evaluateWhen(monitor, branch))
805
+ return;
806
+ // dedup: skip if user text unchanged since last classification
807
+ const currentUserText = collectUserText(branch);
808
+ if (currentUserText && currentUserText === monitor.lastUserText)
809
+ return;
810
+ // ceiling check
811
+ if (monitor.whileCount >= monitor.ceiling) {
812
+ await escalate(monitor, pi, ctx);
813
+ updateStatus();
814
+ return;
815
+ }
816
+ const prompt = renderClassifyPrompt(monitor, branch);
817
+ if (!prompt)
818
+ return;
819
+ // create an abort controller so classification can be cancelled if the user aborts
820
+ const abortController = new AbortController();
821
+ const onAbort = () => abortController.abort();
822
+ const unsubAbort = pi.events.on("monitors:abort", onAbort);
823
+ let result;
824
+ try {
825
+ result = await classifyPrompt(ctx, monitor, prompt, abortController.signal);
826
+ }
827
+ catch (e) {
828
+ if (abortController.signal.aborted)
829
+ return;
830
+ const message = e instanceof Error ? e.message : String(e);
831
+ if (ctx.hasUI) {
832
+ ctx.ui.notify(`[${monitor.name}] Classification failed: ${message}`, "error");
833
+ }
834
+ else {
835
+ console.error(`[${monitor.name}] Classification failed: ${message}`);
836
+ }
837
+ return;
838
+ }
839
+ finally {
840
+ unsubAbort();
841
+ }
842
+ // mark this user text as classified
843
+ monitor.lastUserText = currentUserText;
844
+ if (result.verdict === "clean") {
845
+ const cleanAction = monitor.actions.on_clean;
846
+ if (cleanAction) {
847
+ executeWriteAction(monitor, cleanAction, result);
848
+ }
849
+ monitor.whileCount = 0;
850
+ updateStatus();
851
+ return;
852
+ }
853
+ // Determine which action to execute
854
+ const action = result.verdict === "new" ? monitor.actions.on_new : monitor.actions.on_flag;
855
+ if (!action)
856
+ return;
857
+ // Learn new pattern
858
+ if (result.verdict === "new" && result.newPattern && action.learn_pattern) {
859
+ learnPattern(monitor, result.newPattern);
860
+ }
861
+ // Execute write action (findings to JSON file)
862
+ executeWriteAction(monitor, action, result);
863
+ // Steer (inject message into conversation) — only for main scope
864
+ if (action.steer && monitor.scope.target === "main") {
865
+ const description = result.description ?? "Issue detected";
866
+ const annotation = result.verdict === "new" ? " — new pattern learned" : "";
867
+ const details = {
868
+ monitorName: monitor.name,
869
+ verdict: result.verdict,
870
+ description,
871
+ steer: action.steer,
872
+ whileCount: monitor.whileCount + 1,
873
+ ceiling: monitor.ceiling,
874
+ };
875
+ const content = `[${monitor.name}] ${description}${annotation}. ${action.steer}`;
876
+ if (monitor.event === "agent_end" || monitor.event === "command") {
877
+ // Already post-loop or command context: deliver immediately
878
+ pi.sendMessage({ customType: "monitor-steer", content, display: true, details }, { deliverAs: "steer", triggerTurn: true });
879
+ }
880
+ else {
881
+ // message_end / turn_end: buffer for drain at agent_end
882
+ // (pi's async event queue means these handlers run after the agent loop
883
+ // has already checked getSteeringMessages — direct sendMessage misses
884
+ // the window and the steer arrives one response late)
885
+ pendingAgentEndSteers.push({ monitor, details, content });
886
+ }
887
+ }
888
+ monitor.whileCount++;
889
+ steeredThisTurn.add(monitor.name);
890
+ updateStatus();
891
+ }
892
+ async function escalate(monitor, pi, ctx) {
893
+ if (monitor.escalate === "dismiss") {
894
+ monitor.dismissed = true;
895
+ monitor.whileCount = 0;
896
+ return;
897
+ }
898
+ // In headless mode there is no way to prompt the user, so auto-dismiss
899
+ // to avoid an infinite classify-reset cycle that can never be resolved.
900
+ if (!ctx.hasUI) {
901
+ monitor.dismissed = true;
902
+ monitor.whileCount = 0;
903
+ return;
904
+ }
905
+ if (ctx.hasUI) {
906
+ const choice = await ctx.ui.confirm(`[${monitor.name}] Steered ${monitor.ceiling} times`, "Continue steering, or dismiss this monitor for the session?");
907
+ if (!choice) {
908
+ monitor.dismissed = true;
909
+ monitor.whileCount = 0;
910
+ return;
911
+ }
912
+ }
913
+ monitor.whileCount = 0;
914
+ }
915
+ // =============================================================================
916
+ // Extension entry point
917
+ // =============================================================================
918
+ export default function (pi) {
919
+ const seeded = seedExamples();
920
+ const monitors = discoverMonitors();
921
+ if (monitors.length === 0)
922
+ return;
923
+ // Initialize Nunjucks template environment for monitor prompt templates
924
+ monitorTemplateEnv = createMonitorTemplateEnv();
925
+ let statusCtx;
926
+ function updateStatus() {
927
+ if (!statusCtx?.hasUI)
928
+ return;
929
+ const theme = statusCtx.ui.theme;
930
+ if (!monitorsEnabled) {
931
+ statusCtx.ui.setStatus("monitors", `${theme.fg("dim", "monitors:")}${theme.fg("warning", "OFF")}`);
932
+ return;
933
+ }
934
+ const engaged = monitors.filter((m) => m.whileCount > 0 && !m.dismissed);
935
+ const dismissed = monitors.filter((m) => m.dismissed);
936
+ if (engaged.length === 0 && dismissed.length === 0) {
937
+ const count = theme.fg("dim", `${monitors.length}`);
938
+ statusCtx.ui.setStatus("monitors", `${theme.fg("dim", "monitors:")}${count}`);
939
+ return;
940
+ }
941
+ const parts = [];
942
+ for (const m of engaged) {
943
+ parts.push(theme.fg("warning", `${m.name}(${m.whileCount}/${m.ceiling})`));
944
+ }
945
+ if (dismissed.length > 0) {
946
+ parts.push(theme.fg("dim", `${dismissed.length} dismissed`));
947
+ }
948
+ statusCtx.ui.setStatus("monitors", `${theme.fg("dim", "monitors:")}${parts.join(" ")}`);
949
+ }
950
+ pi.on("session_start", async (_event, ctx) => {
951
+ try {
952
+ statusCtx = ctx;
953
+ if (seeded > 0 && ctx.hasUI) {
954
+ const dir = resolveProjectMonitorsDir();
955
+ ctx.ui.notify(`Seeded ${seeded} example monitor files into ${dir}\nEdit or delete them to customize.`, "info");
956
+ }
957
+ updateStatus();
958
+ }
959
+ catch {
960
+ /* startup errors should not block session */
961
+ }
962
+ });
963
+ pi.on("session_switch", async (_event, ctx) => {
964
+ statusCtx = ctx;
965
+ for (const m of monitors) {
966
+ m.whileCount = 0;
967
+ m.dismissed = false;
968
+ m.lastUserText = "";
969
+ m.activationCount = 0;
970
+ }
971
+ monitorsEnabled = true;
972
+ pendingAgentEndSteers = [];
973
+ updateStatus();
974
+ });
975
+ // ── Tool: monitors-status ──────────────────────────────────────────────
976
+ pi.registerTool({
977
+ name: "monitors-status",
978
+ label: "Monitors Status",
979
+ description: "List all behavior monitors with their current state.",
980
+ promptSnippet: "List all behavior monitors with their current state",
981
+ parameters: Type.Object({}),
982
+ async execute(_toolCallId, _params, _signal, _onUpdate, _ctx) {
983
+ const status = monitors.map((m) => ({
984
+ name: m.name,
985
+ description: m.description,
986
+ event: m.event,
987
+ when: m.when,
988
+ enabled: monitorsEnabled,
989
+ dismissed: m.dismissed,
990
+ whileCount: m.whileCount,
991
+ ceiling: m.ceiling,
992
+ }));
993
+ return {
994
+ details: undefined,
995
+ content: [{ type: "text", text: JSON.stringify(status, null, 2) }],
996
+ };
997
+ },
998
+ });
999
+ // ── Tool: monitors-inspect ─────────────────────────────────────────────
1000
+ pi.registerTool({
1001
+ name: "monitors-inspect",
1002
+ label: "Monitors Inspect",
1003
+ description: "Inspect a monitor — config, state, pattern count, rule count.",
1004
+ promptSnippet: "Inspect a monitor — config, state, pattern count, rule count",
1005
+ parameters: Type.Object({
1006
+ monitor: Type.String({ description: "Monitor name" }),
1007
+ }),
1008
+ async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
1009
+ const monitor = monitors.find((m) => m.name === params.monitor);
1010
+ if (!monitor)
1011
+ throw new Error(`Unknown monitor: ${params.monitor}`);
1012
+ const patterns = loadPatterns(monitor);
1013
+ const instructions = loadInstructions(monitor);
1014
+ const state = monitor.dismissed
1015
+ ? "dismissed"
1016
+ : monitor.whileCount > 0
1017
+ ? `engaged (${monitor.whileCount}/${monitor.ceiling})`
1018
+ : "idle";
1019
+ const info = {
1020
+ name: monitor.name,
1021
+ description: monitor.description,
1022
+ event: monitor.event,
1023
+ when: monitor.when,
1024
+ scope: monitor.scope,
1025
+ classify: {
1026
+ model: monitor.classify.model,
1027
+ context: monitor.classify.context,
1028
+ excludes: monitor.classify.excludes,
1029
+ },
1030
+ patterns: { path: monitor.patterns.path, learn: monitor.patterns.learn, count: patterns.length },
1031
+ instructions: { path: monitor.instructions.path, count: instructions.length },
1032
+ actions: monitor.actions,
1033
+ ceiling: monitor.ceiling,
1034
+ escalate: monitor.escalate,
1035
+ state,
1036
+ enabled: monitorsEnabled,
1037
+ dismissed: monitor.dismissed,
1038
+ whileCount: monitor.whileCount,
1039
+ };
1040
+ return {
1041
+ details: undefined,
1042
+ content: [{ type: "text", text: JSON.stringify(info, null, 2) }],
1043
+ };
1044
+ },
1045
+ });
1046
+ // ── Tool: monitors-control ─────────────────────────────────────────────
1047
+ pi.registerTool({
1048
+ name: "monitors-control",
1049
+ label: "Monitors Control",
1050
+ description: "Control monitors — enable, disable, dismiss, or reset.",
1051
+ promptSnippet: "Control monitors — enable, disable, dismiss, or reset",
1052
+ parameters: Type.Object({
1053
+ action: Type.Union([Type.Literal("on"), Type.Literal("off"), Type.Literal("dismiss"), Type.Literal("reset")]),
1054
+ monitor: Type.Optional(Type.String({ description: "Monitor name (required for dismiss/reset)" })),
1055
+ }),
1056
+ async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
1057
+ if (params.action === "on") {
1058
+ monitorsEnabled = true;
1059
+ updateStatus();
1060
+ return {
1061
+ details: undefined,
1062
+ content: [{ type: "text", text: "Monitors enabled" }],
1063
+ };
1064
+ }
1065
+ if (params.action === "off") {
1066
+ monitorsEnabled = false;
1067
+ updateStatus();
1068
+ return {
1069
+ details: undefined,
1070
+ content: [{ type: "text", text: "All monitors paused for this session" }],
1071
+ };
1072
+ }
1073
+ if (params.action === "dismiss") {
1074
+ if (!params.monitor)
1075
+ throw new Error("Monitor name required for dismiss");
1076
+ const monitor = monitors.find((m) => m.name === params.monitor);
1077
+ if (!monitor)
1078
+ throw new Error(`Unknown monitor: ${params.monitor}`);
1079
+ monitor.dismissed = true;
1080
+ updateStatus();
1081
+ return {
1082
+ details: undefined,
1083
+ content: [{ type: "text", text: `[${monitor.name}] Dismissed for this session` }],
1084
+ };
1085
+ }
1086
+ // reset
1087
+ if (!params.monitor)
1088
+ throw new Error("Monitor name required for reset");
1089
+ const monitor = monitors.find((m) => m.name === params.monitor);
1090
+ if (!monitor)
1091
+ throw new Error(`Unknown monitor: ${params.monitor}`);
1092
+ monitor.dismissed = false;
1093
+ monitor.whileCount = 0;
1094
+ updateStatus();
1095
+ return {
1096
+ details: undefined,
1097
+ content: [{ type: "text", text: `[${monitor.name}] Reset — dismissed=false, whileCount=0` }],
1098
+ };
1099
+ },
1100
+ });
1101
+ // ── Tool: monitors-rules ───────────────────────────────────────────────
1102
+ pi.registerTool({
1103
+ name: "monitors-rules",
1104
+ label: "Monitors Rules",
1105
+ description: "Manage monitor rules — list, add, remove, or replace calibration rules.",
1106
+ promptSnippet: "Manage monitor rules — list, add, remove, or replace calibration rules",
1107
+ parameters: Type.Object({
1108
+ monitor: Type.String({ description: "Monitor name" }),
1109
+ action: Type.Union([Type.Literal("list"), Type.Literal("add"), Type.Literal("remove"), Type.Literal("replace")]),
1110
+ text: Type.Optional(Type.String({ description: "Rule text (for add/replace)" })),
1111
+ index: Type.Optional(Type.Number({ description: "Rule index, 1-based (for remove/replace)" })),
1112
+ }),
1113
+ async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
1114
+ const monitor = monitors.find((m) => m.name === params.monitor);
1115
+ if (!monitor)
1116
+ throw new Error(`Unknown monitor: ${params.monitor}`);
1117
+ if (params.action === "list") {
1118
+ const rules = loadInstructions(monitor);
1119
+ return {
1120
+ details: undefined,
1121
+ content: [{ type: "text", text: JSON.stringify(rules, null, 2) }],
1122
+ };
1123
+ }
1124
+ if (params.action === "add") {
1125
+ if (!params.text)
1126
+ throw new Error("text parameter required for add");
1127
+ const rules = loadInstructions(monitor);
1128
+ rules.push({ text: params.text, added_at: new Date().toISOString() });
1129
+ const err = saveInstructions(monitor, rules);
1130
+ if (err)
1131
+ throw new Error(`Failed to save rules: ${err}`);
1132
+ return {
1133
+ details: undefined,
1134
+ content: [{ type: "text", text: `Rule added to [${monitor.name}]: ${params.text}` }],
1135
+ };
1136
+ }
1137
+ if (params.action === "remove") {
1138
+ if (params.index === undefined)
1139
+ throw new Error("index parameter required for remove");
1140
+ const rules = loadInstructions(monitor);
1141
+ if (params.index < 1 || params.index > rules.length) {
1142
+ throw new Error(`Invalid index ${params.index}. Have ${rules.length} rules.`);
1143
+ }
1144
+ const removed = rules.splice(params.index - 1, 1)[0];
1145
+ const err = saveInstructions(monitor, rules);
1146
+ if (err)
1147
+ throw new Error(`Failed to save rules: ${err}`);
1148
+ return {
1149
+ details: undefined,
1150
+ content: [{ type: "text", text: `Removed rule ${params.index} from [${monitor.name}]: ${removed.text}` }],
1151
+ };
1152
+ }
1153
+ // replace
1154
+ if (params.index === undefined)
1155
+ throw new Error("index parameter required for replace");
1156
+ if (!params.text)
1157
+ throw new Error("text parameter required for replace");
1158
+ const rules = loadInstructions(monitor);
1159
+ if (params.index < 1 || params.index > rules.length) {
1160
+ throw new Error(`Invalid index ${params.index}. Have ${rules.length} rules.`);
1161
+ }
1162
+ const old = rules[params.index - 1].text;
1163
+ rules[params.index - 1] = { text: params.text, added_at: new Date().toISOString() };
1164
+ const err = saveInstructions(monitor, rules);
1165
+ if (err)
1166
+ throw new Error(`Failed to save rules: ${err}`);
1167
+ return {
1168
+ details: undefined,
1169
+ content: [
1170
+ {
1171
+ type: "text",
1172
+ text: `Replaced rule ${params.index} in [${monitor.name}]:\n was: ${old}\n now: ${params.text}`,
1173
+ },
1174
+ ],
1175
+ };
1176
+ },
1177
+ });
1178
+ // ── Tool: monitors-patterns ────────────────────────────────────────────
1179
+ pi.registerTool({
1180
+ name: "monitors-patterns",
1181
+ label: "Monitors Patterns",
1182
+ description: "List patterns for a behavior monitor.",
1183
+ promptSnippet: "List patterns for a behavior monitor",
1184
+ parameters: Type.Object({
1185
+ monitor: Type.String({ description: "Monitor name" }),
1186
+ }),
1187
+ async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
1188
+ const monitor = monitors.find((m) => m.name === params.monitor);
1189
+ if (!monitor)
1190
+ throw new Error(`Unknown monitor: ${params.monitor}`);
1191
+ const patterns = loadPatterns(monitor);
1192
+ return {
1193
+ details: undefined,
1194
+ content: [{ type: "text", text: JSON.stringify(patterns, null, 2) }],
1195
+ };
1196
+ },
1197
+ });
1198
+ // --- message renderer ---
1199
+ pi.registerMessageRenderer("monitor-steer", (message, { expanded }, theme) => {
1200
+ const details = message.details;
1201
+ if (!details) {
1202
+ const box = new Box(1, 1, (t) => theme.bg("customMessageBg", t));
1203
+ box.addChild(new Text(String(message.content), 0, 0));
1204
+ return box;
1205
+ }
1206
+ const verdictColor = details.verdict === "new" ? "warning" : "error";
1207
+ const prefix = theme.fg(verdictColor, `[${details.monitorName}]`);
1208
+ const desc = ` ${details.description}`;
1209
+ const counter = theme.fg("dim", ` (${details.whileCount}/${details.ceiling})`);
1210
+ let text = `${prefix}${desc}${counter}`;
1211
+ if (details.verdict === "new") {
1212
+ text += theme.fg("dim", " — new pattern learned");
1213
+ }
1214
+ text += `\n${theme.fg("muted", details.steer)}`;
1215
+ if (expanded) {
1216
+ text += `\n${theme.fg("dim", `verdict: ${details.verdict}`)}`;
1217
+ }
1218
+ const box = new Box(1, 1, (t) => theme.bg("customMessageBg", t));
1219
+ box.addChild(new Text(text, 0, 0));
1220
+ return box;
1221
+ });
1222
+ // --- abort support + buffered steer drain ---
1223
+ pi.on("agent_end", async () => {
1224
+ pi.events.emit("monitors:abort", undefined);
1225
+ // Drain buffered steers from message_end/turn_end monitors.
1226
+ // The _agentEventQueue guarantees this runs AFTER all turn_end/message_end
1227
+ // handlers complete (sequential promise chain), so the buffer is populated.
1228
+ // Deliver only the first — the corrected response will re-trigger monitors
1229
+ // if additional issues remain.
1230
+ if (pendingAgentEndSteers.length > 0) {
1231
+ const first = pendingAgentEndSteers[0];
1232
+ pendingAgentEndSteers = [];
1233
+ pi.sendMessage({ customType: "monitor-steer", content: first.content, display: true, details: first.details }, { deliverAs: "steer", triggerTurn: true });
1234
+ }
1235
+ });
1236
+ // --- buffered steers for message_end/turn_end monitors ---
1237
+ // These monitors classify during the agent loop but can't inject steers in time
1238
+ // (pi's async event queue means extension handlers run after the agent loop checks
1239
+ // getSteeringMessages). Buffer steers here, drain at agent_end.
1240
+ let pendingAgentEndSteers = [];
1241
+ // --- per-turn exclusion tracking ---
1242
+ let steeredThisTurn = new Set();
1243
+ pi.on("turn_start", () => {
1244
+ steeredThisTurn = new Set();
1245
+ });
1246
+ // group monitors by validated event
1247
+ const byEvent = new Map();
1248
+ for (const m of monitors) {
1249
+ const list = byEvent.get(m.event) ?? [];
1250
+ list.push(m);
1251
+ byEvent.set(m.event, list);
1252
+ }
1253
+ // wire event handlers
1254
+ for (const [event, group] of byEvent) {
1255
+ if (event === "command") {
1256
+ for (const m of group) {
1257
+ pi.registerCommand(m.name, {
1258
+ description: m.description || `Run ${m.name} monitor`,
1259
+ handler: async (_args, ctx) => {
1260
+ const branch = ctx.sessionManager.getBranch();
1261
+ await activate(m, pi, ctx, branch, steeredThisTurn, updateStatus, pendingAgentEndSteers);
1262
+ },
1263
+ });
1264
+ }
1265
+ }
1266
+ else if (event === "message_end") {
1267
+ pi.on("message_end", async (ev, ctx) => {
1268
+ if (ev.message.role !== "assistant")
1269
+ return;
1270
+ const branch = ctx.sessionManager.getBranch();
1271
+ for (const m of group) {
1272
+ await activate(m, pi, ctx, branch, steeredThisTurn, updateStatus, pendingAgentEndSteers);
1273
+ }
1274
+ });
1275
+ }
1276
+ else if (event === "turn_end") {
1277
+ pi.on("turn_end", async (_ev, ctx) => {
1278
+ const branch = ctx.sessionManager.getBranch();
1279
+ for (const m of group) {
1280
+ await activate(m, pi, ctx, branch, steeredThisTurn, updateStatus, pendingAgentEndSteers);
1281
+ }
1282
+ });
1283
+ }
1284
+ else if (event === "agent_end") {
1285
+ pi.on("agent_end", async (_ev, ctx) => {
1286
+ const branch = ctx.sessionManager.getBranch();
1287
+ for (const m of group) {
1288
+ await activate(m, pi, ctx, branch, steeredThisTurn, updateStatus, pendingAgentEndSteers);
1289
+ }
1290
+ });
1291
+ }
1292
+ }
1293
+ // /monitors command — unified management interface
1294
+ const monitorNames = new Set(monitors.map((m) => m.name));
1295
+ const monitorsByName = new Map(monitors.map((m) => [m.name, m]));
1296
+ const monitorVerbs = ["rules", "patterns", "dismiss", "reset"];
1297
+ const rulesActions = ["add", "remove", "replace"];
1298
+ pi.registerCommand("monitors", {
1299
+ description: "Manage behavior monitors",
1300
+ getArgumentCompletions(argumentPrefix) {
1301
+ const tokens = argumentPrefix.split(/\s+/);
1302
+ const last = tokens[tokens.length - 1];
1303
+ // Level 0: no complete token yet — show global commands + monitor names
1304
+ if (tokens.length <= 1) {
1305
+ const items = [
1306
+ { value: "on", label: "on", description: "Enable all monitoring" },
1307
+ { value: "off", label: "off", description: "Pause all monitoring" },
1308
+ ...Array.from(monitorNames).map((n) => ({
1309
+ value: n,
1310
+ label: n,
1311
+ description: `${monitorsByName.get(n)?.description ?? ""} → rules|patterns|dismiss|reset`,
1312
+ })),
1313
+ ];
1314
+ return items.filter((i) => i.value.startsWith(last));
1315
+ }
1316
+ const name = tokens[0];
1317
+ // Level 1: monitor name entered — show verbs
1318
+ if (monitorNames.has(name) && tokens.length === 2) {
1319
+ return monitorVerbs
1320
+ .map((v) => ({ value: `${name} ${v}`, label: v, description: "" }))
1321
+ .filter((i) => i.label.startsWith(last));
1322
+ }
1323
+ // Level 2: monitor name + "rules" — show actions
1324
+ if (monitorNames.has(name) && tokens[1] === "rules" && tokens.length === 3) {
1325
+ return rulesActions
1326
+ .map((a) => ({ value: `${name} rules ${a}`, label: a, description: "" }))
1327
+ .filter((i) => i.label.startsWith(last));
1328
+ }
1329
+ return null;
1330
+ },
1331
+ handler: async (args, ctx) => {
1332
+ const cmd = parseMonitorsArgs(args, monitorNames);
1333
+ if (cmd.type === "error") {
1334
+ ctx.ui.notify(cmd.message, "warning");
1335
+ return;
1336
+ }
1337
+ if (cmd.type === "list") {
1338
+ if (!ctx.hasUI) {
1339
+ handleList(monitors, ctx, monitorsEnabled);
1340
+ return;
1341
+ }
1342
+ const options = [
1343
+ `on — Enable all monitoring`,
1344
+ `off — Pause all monitoring`,
1345
+ ...monitors.map((m) => {
1346
+ const state = m.dismissed
1347
+ ? "dismissed"
1348
+ : m.whileCount > 0
1349
+ ? `engaged (${m.whileCount}/${m.ceiling})`
1350
+ : "idle";
1351
+ return `${m.name} — ${m.description} [${state}]`;
1352
+ }),
1353
+ ];
1354
+ const selected = await ctx.ui.select("Monitors", options);
1355
+ if (!selected)
1356
+ return;
1357
+ const selectedName = selected.split(" ")[0];
1358
+ if (selectedName === "on") {
1359
+ monitorsEnabled = true;
1360
+ updateStatus();
1361
+ ctx.ui.notify("Monitors enabled", "info");
1362
+ }
1363
+ else if (selectedName === "off") {
1364
+ monitorsEnabled = false;
1365
+ updateStatus();
1366
+ ctx.ui.notify("All monitors paused for this session", "info");
1367
+ }
1368
+ else {
1369
+ const monitor = monitorsByName.get(selectedName);
1370
+ if (!monitor)
1371
+ return;
1372
+ const verbOptions = [
1373
+ `inspect — Show monitor state and config`,
1374
+ `rules — List and manage rules`,
1375
+ `patterns — List known patterns`,
1376
+ `dismiss — Silence for this session`,
1377
+ `reset — Reset state and un-dismiss`,
1378
+ ];
1379
+ const verb = await ctx.ui.select(`[${monitor.name}]`, verbOptions);
1380
+ if (!verb)
1381
+ return;
1382
+ const verbName = verb.split(" ")[0];
1383
+ if (verbName === "inspect")
1384
+ handleInspect(monitor, ctx);
1385
+ else if (verbName === "rules")
1386
+ handleRulesList(monitor, ctx);
1387
+ else if (verbName === "patterns")
1388
+ handlePatternsList(monitor, ctx);
1389
+ else if (verbName === "dismiss") {
1390
+ monitor.dismissed = true;
1391
+ monitor.whileCount = 0;
1392
+ updateStatus();
1393
+ ctx.ui.notify(`[${monitor.name}] Dismissed for this session`, "info");
1394
+ }
1395
+ else if (verbName === "reset") {
1396
+ monitor.dismissed = false;
1397
+ monitor.whileCount = 0;
1398
+ updateStatus();
1399
+ ctx.ui.notify(`[${monitor.name}] Reset`, "info");
1400
+ }
1401
+ }
1402
+ return;
1403
+ }
1404
+ if (cmd.type === "on") {
1405
+ monitorsEnabled = true;
1406
+ updateStatus();
1407
+ ctx.ui.notify("Monitors enabled", "info");
1408
+ return;
1409
+ }
1410
+ if (cmd.type === "off") {
1411
+ monitorsEnabled = false;
1412
+ updateStatus();
1413
+ ctx.ui.notify("All monitors paused for this session", "info");
1414
+ return;
1415
+ }
1416
+ const monitor = monitorsByName.get(cmd.name);
1417
+ if (!monitor) {
1418
+ ctx.ui.notify(`Unknown monitor: ${cmd.name}`, "warning");
1419
+ return;
1420
+ }
1421
+ switch (cmd.type) {
1422
+ case "inspect":
1423
+ handleInspect(monitor, ctx);
1424
+ break;
1425
+ case "rules-list":
1426
+ handleRulesList(monitor, ctx);
1427
+ break;
1428
+ case "rules-add":
1429
+ handleRulesAdd(monitor, ctx, cmd.text);
1430
+ break;
1431
+ case "rules-remove":
1432
+ handleRulesRemove(monitor, ctx, cmd.index);
1433
+ break;
1434
+ case "rules-replace":
1435
+ handleRulesReplace(monitor, ctx, cmd.index, cmd.text);
1436
+ break;
1437
+ case "patterns-list":
1438
+ handlePatternsList(monitor, ctx);
1439
+ break;
1440
+ case "dismiss":
1441
+ monitor.dismissed = true;
1442
+ monitor.whileCount = 0;
1443
+ updateStatus();
1444
+ ctx.ui.notify(`[${monitor.name}] Dismissed for this session`, "info");
1445
+ break;
1446
+ case "reset":
1447
+ monitor.dismissed = false;
1448
+ monitor.whileCount = 0;
1449
+ updateStatus();
1450
+ ctx.ui.notify(`[${monitor.name}] Reset`, "info");
1451
+ break;
1452
+ }
1453
+ },
1454
+ });
1455
+ }
1456
+ //# sourceMappingURL=index.js.map