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