@ferris1225/pi-subagents 6.0.2 → 6.0.4
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/CHANGELOG.md +23 -0
- package/README.md +71 -191
- package/index.ts +2 -12
- package/package.json +7 -7
- package/src/configuration/config.ts +2 -6
- package/src/configuration/role-migration.ts +3 -14
- package/src/configuration/schema.ts +10 -0
- package/src/configuration/setup.ts +75 -36
- package/src/configuration/ui.ts +355 -53
- package/src/delegation/agents.ts +0 -1
- package/src/delegation/builtin-agents.ts +6 -27
- package/src/delegation/dispatch.ts +29 -120
- package/src/delegation/prompt.ts +16 -48
- package/src/execution/spawn.ts +2 -2
- package/src/lifecycle/cancellation.ts +4 -8
- package/src/lifecycle/durable.ts +0 -18
- package/src/lifecycle/runtime.ts +7 -56
- package/src/lifecycle/thread-lifecycle.ts +5 -33
- package/src/lifecycle/thread-restore.ts +0 -3
- package/src/lifecycle/thread-shared.ts +1 -11
- package/src/lifecycle/tools.ts +12 -97
- package/src/presentation/monitor.ts +727 -758
- package/src/presentation/widget.ts +36 -134
- package/src/delegation/ownership.ts +0 -98
- package/src/delegation/phase-scope.ts +0 -186
- package/src/workflow/acceptance.ts +0 -67
- package/src/workflow/durable.ts +0 -228
- package/src/workflow/engine.ts +0 -816
- package/src/workflow/plan.ts +0 -409
- package/src/workflow/state.ts +0 -55
- package/src/workflow/templates.ts +0 -156
- package/src/workflow/tool.ts +0 -254
|
@@ -1,758 +1,727 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sub-agent monitor: a module-level singleton store that tracks subagent runs
|
|
3
|
-
* for the current turn.
|
|
4
|
-
*
|
|
5
|
-
* The store notifies wait/status consumers on every mutation. Each run carries
|
|
6
|
-
* timing information plus a concise activity string ("thinking",
|
|
7
|
-
* "read src/index.ts", ...). Settled rows stay until the next beginTurn so the
|
|
8
|
-
* footer can count them beside live siblings; the widget ignores them. Tool
|
|
9
|
-
* results are the durable user-facing record.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { stripVTControlCharacters } from "node:util";
|
|
13
|
-
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
14
|
-
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
15
|
-
import { emptyUsage, type UsageStats } from "../execution/run-control.ts";
|
|
16
|
-
import type {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
return
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (
|
|
278
|
-
if (
|
|
279
|
-
return
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
return
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
case "
|
|
426
|
-
target
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
case "
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
);
|
|
481
|
-
this.notify();
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
this.
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
this.
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
if (
|
|
567
|
-
run.
|
|
568
|
-
if (
|
|
569
|
-
this.notify();
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
/** Record
|
|
573
|
-
|
|
574
|
-
const run = this.find(id);
|
|
575
|
-
if (!run) return;
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
this.
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
const run = this.
|
|
633
|
-
|
|
634
|
-
run
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
this.subscribers
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
case "done":
|
|
729
|
-
return theme.fg("success", "✓");
|
|
730
|
-
case "failed":
|
|
731
|
-
return theme.fg("error", "✗");
|
|
732
|
-
default:
|
|
733
|
-
return theme.fg("dim", "○");
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
/** User-facing lifecycle label used by tool/status rendering. */
|
|
738
|
-
export function statusLabel(status: RunStatus): string {
|
|
739
|
-
switch (status) {
|
|
740
|
-
case "queued":
|
|
741
|
-
return "queued";
|
|
742
|
-
case "running":
|
|
743
|
-
return "running";
|
|
744
|
-
case "interrupting":
|
|
745
|
-
return "interrupting";
|
|
746
|
-
case "parked":
|
|
747
|
-
return "parked";
|
|
748
|
-
case "done":
|
|
749
|
-
return "done";
|
|
750
|
-
case "failed":
|
|
751
|
-
return "stopped";
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
/** Startup label for runs that have not emitted activity yet. */
|
|
756
|
-
export function runWaitLabel(run: Pick<RunView, "status" | "waitReason">): string | undefined {
|
|
757
|
-
return run.status === "queued" ? "starting" : undefined;
|
|
758
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Sub-agent monitor: a module-level singleton store that tracks subagent runs
|
|
3
|
+
* for the current turn.
|
|
4
|
+
*
|
|
5
|
+
* The store notifies wait/status consumers on every mutation. Each run carries
|
|
6
|
+
* timing information plus a concise activity string ("thinking",
|
|
7
|
+
* "read src/index.ts", ...). Settled rows stay until the next beginTurn so the
|
|
8
|
+
* footer can count them beside live siblings; the widget ignores them. Tool
|
|
9
|
+
* results are the durable user-facing record.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { stripVTControlCharacters } from "node:util";
|
|
13
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
15
|
+
import { emptyUsage, type UsageStats } from "../execution/run-control.ts";
|
|
16
|
+
import type { IsolationMode, WorktreeFinalizationStatus } from "../isolation/worktree.ts";
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Types
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
export type RunStatus = "queued" | "running" | "interrupting" | "parked" | "done" | "failed";
|
|
23
|
+
|
|
24
|
+
/** A queued run is preparing its child process; execution has no capacity wait. */
|
|
25
|
+
export type RunWaitReason = "starting";
|
|
26
|
+
|
|
27
|
+
export function isRunActiveStatus(status: RunStatus): boolean {
|
|
28
|
+
return status === "queued" || status === "running" || status === "interrupting";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Durable integration projection of a worktree-isolated run: pending before
|
|
32
|
+
* settlement, finalizing while the patch is applied/cleaned up, then the
|
|
33
|
+
* terminal WorktreeFinalizationStatus. */
|
|
34
|
+
export type RunIntegrationStatus = "pending" | "finalizing" | WorktreeFinalizationStatus;
|
|
35
|
+
|
|
36
|
+
export interface RunView {
|
|
37
|
+
id: number;
|
|
38
|
+
agent: string;
|
|
39
|
+
task: string;
|
|
40
|
+
/** Short content label derived from the task (paths/symbols), shown next to
|
|
41
|
+
* the agent name so concurrent same-agent runs are told apart by what they
|
|
42
|
+
* are doing, not just by their run id. */
|
|
43
|
+
label?: string;
|
|
44
|
+
model?: string;
|
|
45
|
+
/** Selected model ref when the run handed off to current main. */
|
|
46
|
+
modelFallbackFrom?: string;
|
|
47
|
+
/** Effective thinking strength this run was launched with (setup override or role default). */
|
|
48
|
+
thinking?: string;
|
|
49
|
+
isolation?: IsolationMode;
|
|
50
|
+
integrationStatus?: RunIntegrationStatus;
|
|
51
|
+
worktreeId?: string;
|
|
52
|
+
status: RunStatus;
|
|
53
|
+
/** What a queued run is actually waiting for; see RunWaitReason. */
|
|
54
|
+
waitReason?: RunWaitReason;
|
|
55
|
+
usage: UsageStats;
|
|
56
|
+
/** Concise current activity ("thinking", "read src/index.ts"); last writer wins. */
|
|
57
|
+
activity?: string;
|
|
58
|
+
/** Newest-last rolling window of recent activity lines, so a progress query
|
|
59
|
+
* answers "what has it been doing" without reading child artifacts. */
|
|
60
|
+
recentActivity?: string[];
|
|
61
|
+
/** Epoch ms when this logical run first started executing. */
|
|
62
|
+
startedAt?: number;
|
|
63
|
+
/** Epoch ms when the current active segment started. */
|
|
64
|
+
activeSince?: number;
|
|
65
|
+
/** Cumulative active execution time from closed segments; parked time is excluded. */
|
|
66
|
+
elapsedMs: number;
|
|
67
|
+
/** Epoch ms when the latest active segment stopped. */
|
|
68
|
+
endedAt?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface RunChainMeta {
|
|
72
|
+
isolation?: IsolationMode;
|
|
73
|
+
worktreeId?: string;
|
|
74
|
+
/** Initial startup state before the child emits activity. */
|
|
75
|
+
waitReason?: RunWaitReason;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// Formatting helpers
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
const TASK_SUMMARY_MAX = 80;
|
|
83
|
+
const TASK_SUMMARY_ELLIPSIS = "…";
|
|
84
|
+
/** Columns reserved at the END of a truncated summary so the distinguishing
|
|
85
|
+
* keywords (paths, symbols, ...) survive; the head gets the rest. */
|
|
86
|
+
const TASK_SUMMARY_TAIL_MAX = 28;
|
|
87
|
+
/** Tail share of a non-default maxWidth (narrow summaries keep a usable tail). */
|
|
88
|
+
const TASK_SUMMARY_TAIL_SHARE = 0.35;
|
|
89
|
+
const TASK_SUMMARY_TAIL_MIN = 8;
|
|
90
|
+
const TASK_SUMMARY_KEY_SEP = " · ";
|
|
91
|
+
/** kebab/snake words that are task boilerplate, never distinguishing signal. */
|
|
92
|
+
const KEY_FRAGMENT_STOPWORDS = new Set([
|
|
93
|
+
"self-contained",
|
|
94
|
+
"read-only",
|
|
95
|
+
"write-only",
|
|
96
|
+
"auto-fix",
|
|
97
|
+
"re-review",
|
|
98
|
+
"one-line",
|
|
99
|
+
"pre-commit",
|
|
100
|
+
]);
|
|
101
|
+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
102
|
+
|
|
103
|
+
interface KeyFragment {
|
|
104
|
+
text: string;
|
|
105
|
+
index: number;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Pull the most distinguishing fragments out of a task: file paths, quoted
|
|
110
|
+
* phrases, camelCase/PascalCase symbols and kebab/snake compounds. Sorted by
|
|
111
|
+
* first occurrence and deduped (a path covers its own sub-fragments). These
|
|
112
|
+
* are what make parallel tasks of the same agent look different.
|
|
113
|
+
*/
|
|
114
|
+
function extractKeyFragments(text: string): string[] {
|
|
115
|
+
const fragments: KeyFragment[] = [];
|
|
116
|
+
const add = (re: RegExp, group = 0): void => {
|
|
117
|
+
for (const m of text.matchAll(re)) {
|
|
118
|
+
const g = m[group];
|
|
119
|
+
if (g === undefined) continue;
|
|
120
|
+
fragments.push({ text: g, index: m.index ?? 0 });
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
// Quoted phrases first (highest signal).
|
|
124
|
+
add(/["'`]([^"'`]{4,60})["'`]/g, 1);
|
|
125
|
+
// Paths with a known extension (src/index.ts, build/out.js.map).
|
|
126
|
+
add(/(?<![A-Za-z0-9_.-])[A-Za-z0-9_.-]+\.[A-Za-z0-9]{1,5}(?![A-Za-z0-9_.-])/g);
|
|
127
|
+
// Paths with a slash but no extension (src/components, .github/workflows).
|
|
128
|
+
add(/(?<![A-Za-z0-9_.-])[A-Za-z0-9_.-]+(?:[\\/][A-Za-z0-9_.-]+)+(?![\\/])/g);
|
|
129
|
+
// camelCase / PascalCase identifiers (function or type names).
|
|
130
|
+
add(/\b[a-z][a-zA-Z0-9]*[A-Z][a-zA-Z0-9]*\b/g);
|
|
131
|
+
// snake_case / kebab-case compound words.
|
|
132
|
+
add(/\b[a-z][a-z0-9]+[-_][a-z0-9][a-z0-9_-]*\b/g);
|
|
133
|
+
|
|
134
|
+
fragments.sort((a, b) => a.index - b.index);
|
|
135
|
+
const seen = new Set<string>();
|
|
136
|
+
const out: string[] = [];
|
|
137
|
+
for (const f of fragments) {
|
|
138
|
+
const t = f.text.trim();
|
|
139
|
+
if (t.length < 4 || KEY_FRAGMENT_STOPWORDS.has(t)) continue;
|
|
140
|
+
if (seen.has(t)) continue;
|
|
141
|
+
// A longer fragment (the full path) covers its own sub-fragments.
|
|
142
|
+
if (out.some((o) => o.includes(t) || t.includes(o))) continue;
|
|
143
|
+
seen.add(t);
|
|
144
|
+
out.push(t);
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function takeGraphemes(segments: string[], maxWidth: number): string {
|
|
150
|
+
let width = 0;
|
|
151
|
+
const out: string[] = [];
|
|
152
|
+
for (const segment of segments) {
|
|
153
|
+
const segmentWidth = visibleWidth(segment);
|
|
154
|
+
if (width + segmentWidth > maxWidth) break;
|
|
155
|
+
out.push(segment);
|
|
156
|
+
width += segmentWidth;
|
|
157
|
+
}
|
|
158
|
+
return out.join("");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function tailGraphemes(segments: string[], maxWidth: number): string {
|
|
162
|
+
let width = 0;
|
|
163
|
+
const tail: string[] = [];
|
|
164
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
165
|
+
const segmentWidth = visibleWidth(segments[i]);
|
|
166
|
+
if (width + segmentWidth > maxWidth) break;
|
|
167
|
+
tail.unshift(segments[i]);
|
|
168
|
+
width += segmentWidth;
|
|
169
|
+
}
|
|
170
|
+
return tail.join("");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* One-line task preview, capped by `maxWidth` display columns (default 80).
|
|
175
|
+
* `keysOnly` (default): extracted key fragments (paths, quoted phrases,
|
|
176
|
+
* symbols) are shown bare — the agent name is already displayed next to the
|
|
177
|
+
* task line, so templated prose ("scout: trace how ...") adds nothing.
|
|
178
|
+
* `keysOnly: false` keeps the prose as `head…tail` (used for completion
|
|
179
|
+
* messages, where the Task line is the reader's only context).
|
|
180
|
+
* Grapheme-safe — CJK, ZWJ emoji and combining sequences are never split.
|
|
181
|
+
*/
|
|
182
|
+
export function formatTaskSummary(task: string, maxWidth: number = TASK_SUMMARY_MAX, keysOnly = true): string {
|
|
183
|
+
if (maxWidth <= 0) return "";
|
|
184
|
+
const oneLine = stripVTControlCharacters(task).replace(/\s+/g, " ").trim();
|
|
185
|
+
if (visibleWidth(oneLine) <= maxWidth) return oneLine;
|
|
186
|
+
|
|
187
|
+
const segments = [...graphemeSegmenter.segment(oneLine)].map((s) => s.segment);
|
|
188
|
+
const ellipsisWidth = visibleWidth(TASK_SUMMARY_ELLIPSIS);
|
|
189
|
+
|
|
190
|
+
if (keysOnly) {
|
|
191
|
+
const fragments = extractKeyFragments(oneLine);
|
|
192
|
+
if (fragments.length > 0) {
|
|
193
|
+
const keyMax = maxWidth - 1;
|
|
194
|
+
let keys = "";
|
|
195
|
+
for (const fragment of fragments) {
|
|
196
|
+
const piece = keys ? `${TASK_SUMMARY_KEY_SEP}${fragment}` : fragment;
|
|
197
|
+
const total = keys + piece;
|
|
198
|
+
if (visibleWidth(total) > keyMax) {
|
|
199
|
+
// Budget exhausted: keep what fits, unless nothing fits yet.
|
|
200
|
+
if (!keys) {
|
|
201
|
+
// A single over-long fragment keeps its tail
|
|
202
|
+
// (extension/symbol) and is prefixed with the ellipsis.
|
|
203
|
+
const fragmentSegments = [...graphemeSegmenter.segment(piece)].map((s) => s.segment);
|
|
204
|
+
keys = `${TASK_SUMMARY_ELLIPSIS}${tailGraphemes(fragmentSegments, keyMax - ellipsisWidth)}`;
|
|
205
|
+
}
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
keys = total;
|
|
209
|
+
}
|
|
210
|
+
return keys;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// No distinctive fragments (or prose mode): fall back to head…tail.
|
|
215
|
+
const tailMax = Math.max(
|
|
216
|
+
TASK_SUMMARY_TAIL_MIN,
|
|
217
|
+
Math.min(TASK_SUMMARY_TAIL_MAX, Math.round(maxWidth * TASK_SUMMARY_TAIL_SHARE)),
|
|
218
|
+
);
|
|
219
|
+
const headMax = maxWidth - ellipsisWidth - tailMax;
|
|
220
|
+
if (headMax <= 0) {
|
|
221
|
+
return `${TASK_SUMMARY_ELLIPSIS}${tailGraphemes(segments, maxWidth - ellipsisWidth)}`;
|
|
222
|
+
}
|
|
223
|
+
return `${takeGraphemes(segments, headMax)}${TASK_SUMMARY_ELLIPSIS}${tailGraphemes(segments, tailMax)}`;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Max display width of a run's content label. */
|
|
227
|
+
const RUN_LABEL_MAX = 32;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Short content label for a run, derived from its task: the single most
|
|
231
|
+
* distinguishing fragment so concurrent same-agent runs are told apart by WHAT
|
|
232
|
+
* they do, not just their run id. A file/directory path outranks other
|
|
233
|
+
* fragment kinds (a kebab-case word like "edge-case" never beats
|
|
234
|
+
* "src/config.ts"); a long path keeps its tail (the filename is the
|
|
235
|
+
* recognisable part); a task with no recognizable fragment falls back to a
|
|
236
|
+
* head slice of its prose. Grapheme-safe.
|
|
237
|
+
*/
|
|
238
|
+
export function runLabel(task: string): string {
|
|
239
|
+
const oneLine = stripVTControlCharacters(task).replace(/\s+/g, " ").trim();
|
|
240
|
+
const fragments = extractKeyFragments(oneLine);
|
|
241
|
+
const fragment = fragments.find((candidate) => /[\\/]|\.[A-Za-z0-9]{1,5}$/.test(candidate)) ?? fragments[0];
|
|
242
|
+
const src = fragment ?? oneLine;
|
|
243
|
+
if (visibleWidth(src) <= RUN_LABEL_MAX) return src;
|
|
244
|
+
const chars = [...graphemeSegmenter.segment(src)].map((s) => s.segment);
|
|
245
|
+
// A path/symbol fragment keeps its tail (filename/symbol is recognisable);
|
|
246
|
+
// a prose fallback keeps its head.
|
|
247
|
+
return fragment
|
|
248
|
+
? `${TASK_SUMMARY_ELLIPSIS}${tailGraphemes(chars, RUN_LABEL_MAX - 1)}`
|
|
249
|
+
: `${takeGraphemes(chars, RUN_LABEL_MAX - 1)}${TASK_SUMMARY_ELLIPSIS}`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Narrow an already-extracted run label to a smaller budget, keeping its
|
|
253
|
+
* tail: runLabel tail-weights path fragments because the filename is the
|
|
254
|
+
* recognisable part, and a second squeeze must not trade that tail away.
|
|
255
|
+
* Grapheme-safe. */
|
|
256
|
+
export function shrinkRunLabel(text: string, maxWidth: number): string {
|
|
257
|
+
if (maxWidth <= 0) return "";
|
|
258
|
+
if (visibleWidth(text) <= maxWidth) return text;
|
|
259
|
+
const chars = [...graphemeSegmenter.segment(text)].map((s) => s.segment);
|
|
260
|
+
return `${TASK_SUMMARY_ELLIPSIS}${tailGraphemes(chars, maxWidth - 1)}`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function formatTokens(count: number): string {
|
|
264
|
+
if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M`;
|
|
265
|
+
if (count >= 1_000) return `${(count / 1_000).toFixed(1)}k`;
|
|
266
|
+
return String(count);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Token flow in the pi-footer vocabulary: ↑input ↓output, R cache-read,
|
|
270
|
+
* W cache-write; zero components are omitted. No cost — callers place it as
|
|
271
|
+
* its own droppable part. */
|
|
272
|
+
export function formatUsageTokens(usage: UsageStats | undefined): string | undefined {
|
|
273
|
+
if (!usage) return undefined;
|
|
274
|
+
const parts: string[] = [];
|
|
275
|
+
if (usage.input) parts.push(`↑${formatTokens(usage.input)}`);
|
|
276
|
+
if (usage.output) parts.push(`↓${formatTokens(usage.output)}`);
|
|
277
|
+
if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
|
|
278
|
+
if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
|
|
279
|
+
return parts.length > 0 ? parts.join(" ") : undefined;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function formatUsageCompact(usage: UsageStats): string {
|
|
283
|
+
return [formatUsageTokens(usage), usageCostPart(usage)].filter(Boolean).join(" ");
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Cost as its own droppable part: `$0.0421`, undefined when nothing accrued. */
|
|
287
|
+
function usageCostPart(usage: UsageStats | undefined): string | undefined {
|
|
288
|
+
return usage?.cost ? `$${usage.cost.toFixed(4)}` : undefined;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Aggregate usage across several runs (chain steps or a completion group). */
|
|
292
|
+
export function sumUsage(parts: readonly UsageStats[]): UsageStats {
|
|
293
|
+
const total: UsageStats = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 };
|
|
294
|
+
for (const part of parts) {
|
|
295
|
+
total.input += part.input;
|
|
296
|
+
total.output += part.output;
|
|
297
|
+
total.cacheRead += part.cacheRead;
|
|
298
|
+
total.cacheWrite += part.cacheWrite;
|
|
299
|
+
total.cost += part.cost;
|
|
300
|
+
total.contextTokens += part.contextTokens;
|
|
301
|
+
total.turns += part.turns;
|
|
302
|
+
}
|
|
303
|
+
return total;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function formatDuration(ms: number): string {
|
|
307
|
+
const totalSeconds = Math.max(0, Math.floor(ms / 1000));
|
|
308
|
+
if (totalSeconds < 60) return `${totalSeconds}s`;
|
|
309
|
+
const minutes = Math.floor(totalSeconds / 60);
|
|
310
|
+
const seconds = totalSeconds % 60;
|
|
311
|
+
// Seconds stay visible at every magnitude: a long-running chain is judged
|
|
312
|
+
// by whether it is still moving, and "5s ago" is exactly that signal.
|
|
313
|
+
if (minutes < 60) return `${minutes}m${String(seconds).padStart(2, "0")}s`;
|
|
314
|
+
const hours = Math.floor(minutes / 60);
|
|
315
|
+
return `${hours}h${String(minutes % 60).padStart(2, "0")}m${String(seconds).padStart(2, "0")}s`;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function elapsedMilliseconds(run: RunView, now: number = Date.now()): number {
|
|
319
|
+
let elapsed = run.elapsedMs ?? 0;
|
|
320
|
+
if (run.activeSince !== undefined) elapsed += Math.max(0, now - run.activeSince);
|
|
321
|
+
// Keep formatting tolerant of older/synthetic RunView values that predate
|
|
322
|
+
// segmented timing and carry only startedAt/endedAt.
|
|
323
|
+
if (elapsed === 0 && run.startedAt !== undefined && run.activeSince === undefined) {
|
|
324
|
+
elapsed = Math.max(0, (run.endedAt ?? now) - run.startedAt);
|
|
325
|
+
}
|
|
326
|
+
return elapsed;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function formatElapsed(run: RunView, now: number = Date.now()): string {
|
|
330
|
+
if (run.startedAt === undefined && run.elapsedMs <= 0) return "";
|
|
331
|
+
return formatDuration(elapsedMilliseconds(run, now));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Max length of the argument target inside a formatted activity line. */
|
|
335
|
+
const ACTIVITY_TARGET_MAX = 60;
|
|
336
|
+
|
|
337
|
+
const REDACTED = "<redacted>";
|
|
338
|
+
|
|
339
|
+
/** Remove credentials embedded in otherwise ordinary activity strings such as
|
|
340
|
+
* shell commands and HTTP headers. */
|
|
341
|
+
function redactSensitiveText(value: string): string {
|
|
342
|
+
let text = stripVTControlCharacters(value);
|
|
343
|
+
text = text.replace(
|
|
344
|
+
/(\bauthorization\s*:\s*(?:bearer|basic)\s+)([^\s'"`;,]+)/giu,
|
|
345
|
+
`$1${REDACTED}`,
|
|
346
|
+
);
|
|
347
|
+
text = text.replace(
|
|
348
|
+
/(\bbearer\s+)([A-Za-z0-9._~+/=-]{6,})/giu,
|
|
349
|
+
`$1${REDACTED}`,
|
|
350
|
+
);
|
|
351
|
+
text = text.replace(
|
|
352
|
+
/(\b(?:api[-_]?key|apikey|access[-_]?token|refresh[-_]?token|token|password|passwd|secret|credential|cookie)\b\s*(?:=|:)\s*)(?:"[^"]*"|'[^']*'|[^\s;&,]+)/giu,
|
|
353
|
+
`$1${REDACTED}`,
|
|
354
|
+
);
|
|
355
|
+
text = text.replace(
|
|
356
|
+
/((?:--?(?:api[-_]?key|access[-_]?token|token|password|secret|credential))\s+)(?:"[^"]*"|'[^']*'|\S+)/giu,
|
|
357
|
+
`$1${REDACTED}`,
|
|
358
|
+
);
|
|
359
|
+
return text.replace(
|
|
360
|
+
/\b(?:sk-[A-Za-z0-9_-]{8,}|gh[pousr]_[A-Za-z0-9]{8,}|AKIA[A-Z0-9]{16})\b/gu,
|
|
361
|
+
REDACTED,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** Monitor activity is returned to the parent model and rendered in the terminal,
|
|
366
|
+
* so treat every live string as untrusted before it reaches store state. */
|
|
367
|
+
function sanitizeActivityText(value: string): string {
|
|
368
|
+
return redactSensitiveText(value).replace(/\s+/g, " ").trim();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function shortTarget(value: unknown): string {
|
|
372
|
+
if (typeof value !== "string") return "";
|
|
373
|
+
const oneLine = sanitizeActivityText(value);
|
|
374
|
+
// Slice by code point so emoji / CJK-ext never leave a lone surrogate.
|
|
375
|
+
const chars = [...oneLine];
|
|
376
|
+
return chars.length > ACTIVITY_TARGET_MAX ? `${chars.slice(0, ACTIVITY_TARGET_MAX - 1).join("")}…` : oneLine;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** A code-mode script's first meaningful line: the earliest line that is not
|
|
380
|
+
* blank and not a `//` comment, shortened like any other activity target. */
|
|
381
|
+
function scriptLine(value: unknown): string {
|
|
382
|
+
if (typeof value !== "string") return "";
|
|
383
|
+
const line = value
|
|
384
|
+
.split("\n")
|
|
385
|
+
.map((part) => part.trim())
|
|
386
|
+
.find((part) => part.length > 0 && !part.startsWith("//"));
|
|
387
|
+
return line ? shortTarget(line) : "";
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Newest-last rolling window of what a run has been doing, surfaced to the
|
|
391
|
+
* parent model as a progress trail. */
|
|
392
|
+
const RECENT_ACTIVITY_MAX = 6;
|
|
393
|
+
|
|
394
|
+
/** Concise "what is it doing" text for a tool call: the tool name plus its single
|
|
395
|
+
* most telling argument (path, command, pattern, ...) — never a raw JSON blob. */
|
|
396
|
+
export function formatToolActivity(toolName: string, args: unknown): string {
|
|
397
|
+
const a = (typeof args === "object" && args !== null ? args : {}) as Record<string, unknown>;
|
|
398
|
+
const pick = (...keys: string[]): string => {
|
|
399
|
+
for (const key of keys) {
|
|
400
|
+
const s = shortTarget(a[key]);
|
|
401
|
+
if (s) return s;
|
|
402
|
+
}
|
|
403
|
+
return "";
|
|
404
|
+
};
|
|
405
|
+
let target: string;
|
|
406
|
+
switch (toolName) {
|
|
407
|
+
// Pi ships one shell tool per platform flavor, all with a `command`
|
|
408
|
+
// parameter: a Windows parent that swapped bash for powershell must still
|
|
409
|
+
// show the command it is running, not a bare tool name.
|
|
410
|
+
case "bash":
|
|
411
|
+
case "powershell":
|
|
412
|
+
case "shell":
|
|
413
|
+
target = pick("command");
|
|
414
|
+
break;
|
|
415
|
+
case "read":
|
|
416
|
+
case "edit":
|
|
417
|
+
case "write":
|
|
418
|
+
case "ls":
|
|
419
|
+
target = pick("path", "file", "filePath");
|
|
420
|
+
break;
|
|
421
|
+
case "grep":
|
|
422
|
+
case "find":
|
|
423
|
+
target = pick("pattern", "query", "path");
|
|
424
|
+
break;
|
|
425
|
+
case "codemode":
|
|
426
|
+
// A script is source, not a target: name its first meaningful line so the
|
|
427
|
+
// trail shows what the child is doing instead of a code blob.
|
|
428
|
+
target = scriptLine(a.code);
|
|
429
|
+
break;
|
|
430
|
+
case "subagent":
|
|
431
|
+
target = pick("agent", "task");
|
|
432
|
+
break;
|
|
433
|
+
default:
|
|
434
|
+
target = pick("path", "command", "query", "pattern", "url", "file", "task");
|
|
435
|
+
}
|
|
436
|
+
const safeToolName = sanitizeActivityText(toolName) || "tool";
|
|
437
|
+
return target ? `${safeToolName} ${target}` : safeToolName;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// ---------------------------------------------------------------------------
|
|
441
|
+
// MonitorStore
|
|
442
|
+
// ---------------------------------------------------------------------------
|
|
443
|
+
|
|
444
|
+
export class MonitorStore {
|
|
445
|
+
private runs: RunView[] = [];
|
|
446
|
+
private nextId = 1;
|
|
447
|
+
private subscribers = new Set<() => void>();
|
|
448
|
+
|
|
449
|
+
beginTurn(): void {
|
|
450
|
+
// Clear finished runs from a previous turn, but keep active and parked
|
|
451
|
+
// threads so concurrent work is not wiped between parent turns.
|
|
452
|
+
this.runs = this.runs.filter(
|
|
453
|
+
(r) => isRunActiveStatus(r.status) || r.status === "parked",
|
|
454
|
+
);
|
|
455
|
+
this.notify();
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Reserve a stable id for a durable result that must remain independently
|
|
459
|
+
* addressable without appearing as a live monitor row. */
|
|
460
|
+
reserveRunId(): number {
|
|
461
|
+
return this.nextId++;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** Keep newly allocated ids above a restored id so reload-restored threads
|
|
465
|
+
* never collide with runs started in the current process. */
|
|
466
|
+
ensureNextIdAbove(id: number): void {
|
|
467
|
+
if (id >= this.nextId) this.nextId = id + 1;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** Re-register a durable thread restored from a previous process. The row
|
|
471
|
+
* keeps its stable id and historical elapsed time. */
|
|
472
|
+
restoreRun(view: Pick<RunView, "id" | "agent" | "task" | "status"> & Partial<RunView>): void {
|
|
473
|
+
if (this.find(view.id)) return;
|
|
474
|
+
this.runs.push({
|
|
475
|
+
label: runLabel(view.task),
|
|
476
|
+
usage: emptyUsage(),
|
|
477
|
+
elapsedMs: 0,
|
|
478
|
+
...view,
|
|
479
|
+
});
|
|
480
|
+
this.ensureNextIdAbove(view.id);
|
|
481
|
+
this.notify();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
addRun(agent: string, task: string, model?: string, thinking?: string, meta?: RunChainMeta): number {
|
|
485
|
+
const id = this.reserveRunId();
|
|
486
|
+
this.runs.push({
|
|
487
|
+
id,
|
|
488
|
+
agent,
|
|
489
|
+
task,
|
|
490
|
+
label: runLabel(task),
|
|
491
|
+
model,
|
|
492
|
+
thinking,
|
|
493
|
+
status: "queued",
|
|
494
|
+
waitReason: meta?.waitReason ?? "starting",
|
|
495
|
+
usage: emptyUsage(),
|
|
496
|
+
elapsedMs: 0,
|
|
497
|
+
...(meta?.isolation ? { isolation: meta.isolation, integrationStatus: meta.isolation === "worktree" ? "pending" : undefined, ...(meta.worktreeId ? { worktreeId: meta.worktreeId } : {}) } : {}),
|
|
498
|
+
});
|
|
499
|
+
this.notify();
|
|
500
|
+
return id;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
setStatus(id: number, status: RunStatus): void {
|
|
504
|
+
const run = this.find(id);
|
|
505
|
+
if (!run) return;
|
|
506
|
+
const previousStatus = run.status;
|
|
507
|
+
const wasExecuting = previousStatus === "running" || previousStatus === "interrupting";
|
|
508
|
+
const isExecuting = status === "running" || status === "interrupting";
|
|
509
|
+
const now = Date.now();
|
|
510
|
+
run.status = status;
|
|
511
|
+
if (status !== "queued") run.waitReason = undefined;
|
|
512
|
+
if (isExecuting && !wasExecuting) {
|
|
513
|
+
run.startedAt ??= now;
|
|
514
|
+
run.activeSince = now;
|
|
515
|
+
run.endedAt = undefined;
|
|
516
|
+
} else if (!isExecuting && wasExecuting && run.activeSince !== undefined) {
|
|
517
|
+
run.elapsedMs += Math.max(0, now - run.activeSince);
|
|
518
|
+
run.activeSince = undefined;
|
|
519
|
+
}
|
|
520
|
+
if ((status === "parked" || status === "done" || status === "failed") && run.endedAt === undefined) {
|
|
521
|
+
run.endedAt = now;
|
|
522
|
+
}
|
|
523
|
+
this.notify();
|
|
524
|
+
}
|
|
525
|
+
/** Record what a still-queued run is actually waiting for, at the exact
|
|
526
|
+
* transition owned by the caller (lane wait begins, child process starts). */
|
|
527
|
+
setWaitReason(id: number, waitReason: RunWaitReason): void {
|
|
528
|
+
const run = this.find(id);
|
|
529
|
+
if (!run || run.status !== "queued" || run.waitReason === waitReason) return;
|
|
530
|
+
run.waitReason = waitReason;
|
|
531
|
+
this.notify();
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
setUsage(id: number, usage: UsageStats, model?: string): void {
|
|
535
|
+
const run = this.find(id);
|
|
536
|
+
if (!run) return;
|
|
537
|
+
run.usage = { ...usage };
|
|
538
|
+
if (model) run.model = model;
|
|
539
|
+
this.notify();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** Record the final actual model and selected-to-main transition. */
|
|
543
|
+
setModel(id: number, model?: string, fallbackFrom?: string): void {
|
|
544
|
+
const run = this.find(id);
|
|
545
|
+
if (!run) return;
|
|
546
|
+
if (model) run.model = model;
|
|
547
|
+
run.modelFallbackFrom = fallbackFrom;
|
|
548
|
+
this.notify();
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** Record the capability-clamped thinking level for the active model. */
|
|
552
|
+
setThinking(id: number, thinking?: string): void {
|
|
553
|
+
const run = this.find(id);
|
|
554
|
+
if (!run || !thinking) return;
|
|
555
|
+
run.thinking = thinking;
|
|
556
|
+
this.notify();
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** Update the run's current one-line activity (what it is doing now). */
|
|
560
|
+
setActivity(id: number, text: string): void {
|
|
561
|
+
const run = this.find(id);
|
|
562
|
+
if (!run) return;
|
|
563
|
+
const safe = sanitizeActivityText(text) || undefined;
|
|
564
|
+
// Every streamed thinking/text delta reports the same activity; skip the
|
|
565
|
+
// notification (and the widget re-render it triggers) when nothing changed.
|
|
566
|
+
if (run.activity === safe) return;
|
|
567
|
+
run.activity = safe;
|
|
568
|
+
if (safe) this.pushRecentActivity(run, safe);
|
|
569
|
+
this.notify();
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** Record a tool starting and update the run's visible activity. */
|
|
573
|
+
recordToolStart(id: number, toolName: string, activity: string): void {
|
|
574
|
+
const run = this.find(id);
|
|
575
|
+
if (!run) return;
|
|
576
|
+
const safeToolName = sanitizeActivityText(toolName) || "tool";
|
|
577
|
+
const safeActivity = sanitizeActivityText(activity) || safeToolName;
|
|
578
|
+
run.activity = safeActivity;
|
|
579
|
+
this.pushRecentActivity(run, safeActivity);
|
|
580
|
+
this.notify();
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** Record a failed tool; successful completions keep their last activity
|
|
584
|
+
* until the next model event supplies a more useful description. */
|
|
585
|
+
recordToolEnd(id: number, toolName: string, isError: boolean): void {
|
|
586
|
+
const run = this.find(id);
|
|
587
|
+
if (!run) return;
|
|
588
|
+
if (isError) {
|
|
589
|
+
const failed = `✗ ${sanitizeActivityText(toolName) || "tool"} failed`;
|
|
590
|
+
run.activity = failed;
|
|
591
|
+
this.pushRecentActivity(run, failed);
|
|
592
|
+
}
|
|
593
|
+
this.notify();
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
setIsolation(
|
|
597
|
+
id: number,
|
|
598
|
+
isolation: IsolationMode,
|
|
599
|
+
integrationStatus?: RunIntegrationStatus,
|
|
600
|
+
worktreeId?: string,
|
|
601
|
+
): void {
|
|
602
|
+
const run = this.find(id);
|
|
603
|
+
if (!run) return;
|
|
604
|
+
run.isolation = isolation;
|
|
605
|
+
run.integrationStatus = integrationStatus;
|
|
606
|
+
if (worktreeId) run.worktreeId = worktreeId;
|
|
607
|
+
this.notify();
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
getElapsedMs(id: number, now: number = Date.now()): number | undefined {
|
|
611
|
+
const run = this.find(id);
|
|
612
|
+
return run ? elapsedMilliseconds(run, now) : undefined;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** Look up a run by id without removing it. */
|
|
616
|
+
findRun(id: number): RunView | undefined {
|
|
617
|
+
return this.find(id);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** Remove all runs (used on session shutdown so stale state never leaks
|
|
621
|
+
* into the next session). Does not reset the id counter so in-flight
|
|
622
|
+
* finishRun calls from the old session remain safe no-ops. */
|
|
623
|
+
clear(): void {
|
|
624
|
+
this.runs = [];
|
|
625
|
+
this.notify();
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/** Remove a run after publication. Returns the removed run. */
|
|
629
|
+
removeRun(id: number): RunView | undefined {
|
|
630
|
+
const index = this.runs.findIndex((r) => r.id === id);
|
|
631
|
+
if (index === -1) return undefined;
|
|
632
|
+
const [run] = this.runs.splice(index, 1);
|
|
633
|
+
this.notify();
|
|
634
|
+
return run;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
getRuns(): RunView[] {
|
|
638
|
+
return this.runs;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
subscribe(cb: () => void): () => void {
|
|
642
|
+
this.subscribers.add(cb);
|
|
643
|
+
return () => {
|
|
644
|
+
this.subscribers.delete(cb);
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
summarize(run: RunView): string {
|
|
649
|
+
const usage = formatUsageCompact(run.usage);
|
|
650
|
+
const parts = [run.agent];
|
|
651
|
+
if (run.model) parts.push(run.model);
|
|
652
|
+
if (run.thinking) parts.push(`thinking ${run.thinking}`);
|
|
653
|
+
if (run.isolation === "worktree") parts.push(`worktree ${run.integrationStatus ?? "active"}`);
|
|
654
|
+
if (usage) parts.push(usage);
|
|
655
|
+
const elapsed = formatElapsed(run);
|
|
656
|
+
if (elapsed) parts.push(elapsed);
|
|
657
|
+
return parts.join(" · ");
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
private find(id: number): RunView | undefined {
|
|
661
|
+
return this.runs.find((r) => r.id === id);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/** Newest-last rolling activity window; consecutive duplicates collapse. */
|
|
665
|
+
private pushRecentActivity(run: RunView, text: string): void {
|
|
666
|
+
const recent = run.recentActivity ?? [];
|
|
667
|
+
if (recent[recent.length - 1] !== text) recent.push(text);
|
|
668
|
+
if (recent.length > RECENT_ACTIVITY_MAX) recent.splice(0, recent.length - RECENT_ACTIVITY_MAX);
|
|
669
|
+
run.recentActivity = recent;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
private notify(): void {
|
|
673
|
+
for (const cb of this.subscribers) {
|
|
674
|
+
try {
|
|
675
|
+
cb();
|
|
676
|
+
} catch {
|
|
677
|
+
/* subscriber errors must not break the store */
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
export const monitor = new MonitorStore();
|
|
684
|
+
|
|
685
|
+
// ---------------------------------------------------------------------------
|
|
686
|
+
// Status icons
|
|
687
|
+
// ---------------------------------------------------------------------------
|
|
688
|
+
|
|
689
|
+
export function statusIcon(status: RunStatus, theme: Theme): string {
|
|
690
|
+
switch (status) {
|
|
691
|
+
case "running":
|
|
692
|
+
return theme.fg("accent", "●");
|
|
693
|
+
case "interrupting":
|
|
694
|
+
return theme.fg("warning", "◐");
|
|
695
|
+
case "parked":
|
|
696
|
+
return theme.fg("dim", "■");
|
|
697
|
+
case "done":
|
|
698
|
+
return theme.fg("success", "✓");
|
|
699
|
+
case "failed":
|
|
700
|
+
return theme.fg("error", "✗");
|
|
701
|
+
default:
|
|
702
|
+
return theme.fg("dim", "○");
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/** User-facing lifecycle label used by tool/status rendering. */
|
|
707
|
+
export function statusLabel(status: RunStatus): string {
|
|
708
|
+
switch (status) {
|
|
709
|
+
case "queued":
|
|
710
|
+
return "queued";
|
|
711
|
+
case "running":
|
|
712
|
+
return "running";
|
|
713
|
+
case "interrupting":
|
|
714
|
+
return "interrupting";
|
|
715
|
+
case "parked":
|
|
716
|
+
return "parked";
|
|
717
|
+
case "done":
|
|
718
|
+
return "done";
|
|
719
|
+
case "failed":
|
|
720
|
+
return "stopped";
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/** Startup label for runs that have not emitted activity yet. */
|
|
725
|
+
export function runWaitLabel(run: Pick<RunView, "status" | "waitReason">): string | undefined {
|
|
726
|
+
return run.status === "queued" ? "starting" : undefined;
|
|
727
|
+
}
|