@ferris1225/pi-subagents 0.32.2 → 1.0.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/README.md +35 -16
- package/package.json +2 -2
- package/src/announcements.ts +59 -0
- package/src/dispatch.ts +1833 -1878
- package/src/fixloop.ts +1 -1
- package/src/format.ts +2 -1
- package/src/index.ts +4 -8
- package/src/models.ts +23 -35
- package/src/monitor.ts +29 -88
- package/src/rpc-run.ts +1 -26
- package/src/runtime.ts +284 -285
- package/src/setup.ts +4 -4
- package/src/spawn.ts +1 -6
- package/src/tools.ts +730 -748
- package/src/trajectory.ts +16 -207
- package/src/inspector-panel.ts +0 -363
- package/src/inspector.ts +0 -369
- package/src/widget.ts +0 -195
package/src/dispatch.ts
CHANGED
|
@@ -1,1878 +1,1833 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The `subagent` tool: dispatches explore/worker/reviewer agents as isolated pi
|
|
3
|
-
* child processes, single or parallel. Owns the dispatch pipeline: config load,
|
|
4
|
-
* per-agent model-pool resolution, per-run
|
|
5
|
-
* (REVIEW_FAIL → worker → re-review), and completion delivery.
|
|
6
|
-
*
|
|
7
|
-
* Vision: a task flagged `vision: true` uses the configured vision model as an
|
|
8
|
-
* explicit primary, then the agent's configured backup and the current
|
|
9
|
-
* main-window model. Stale refs remain in the pool and fail normally at runtime.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { StringEnum } from "@earendil-works/pi-ai";
|
|
13
|
-
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
14
|
-
import { Text } from "@earendil-works/pi-tui";
|
|
15
|
-
import { existsSync } from "node:fs";
|
|
16
|
-
import { realpath, rm } from "node:fs/promises";
|
|
17
|
-
import { resolve } from "node:path";
|
|
18
|
-
import { Type } from "typebox";
|
|
19
|
-
import { discoverAgents, type AgentConfig } from "./agents.ts";
|
|
20
|
-
import {
|
|
21
|
-
completionTriggersTurn,
|
|
22
|
-
type CompletionMessageItem,
|
|
23
|
-
} from "./completion.ts";
|
|
24
|
-
import { loadConfig, type SubagentsConfig } from "./config.ts";
|
|
25
|
-
import {
|
|
26
|
-
dispatchFailedResult,
|
|
27
|
-
failedStartResult,
|
|
28
|
-
formatCompletionBlock,
|
|
29
|
-
formatUsage,
|
|
30
|
-
modelLevelTakeoverNote,
|
|
31
|
-
queuedResult,
|
|
32
|
-
} from "./format.ts";
|
|
33
|
-
import {
|
|
34
|
-
buildFixTaskBrief,
|
|
35
|
-
buildReReviewBrief,
|
|
36
|
-
formatChainSummary,
|
|
37
|
-
shouldTriggerFixLoop,
|
|
38
|
-
summarizeChainResult,
|
|
39
|
-
type ChainStep,
|
|
40
|
-
} from "./fixloop.ts";
|
|
41
|
-
import { currentModelRef, resolveAgentModelPool } from "./models.ts";
|
|
42
|
-
import {
|
|
43
|
-
formatTaskSummary,
|
|
44
|
-
formatToolActivity,
|
|
45
|
-
monitor,
|
|
46
|
-
statusIcon,
|
|
47
|
-
type RunChainMeta,
|
|
48
|
-
} from "./monitor.ts";
|
|
49
|
-
import type { SubagentRuntime, SubagentThread, ThreadState } from "./runtime.ts";
|
|
50
|
-
import { persistRecoveryRecords, recoveryRecordFromFinalization } from "./recovery.ts";
|
|
51
|
-
import { forkRetainedSession } from "./session-fork.ts";
|
|
52
|
-
import {
|
|
53
|
-
buildFallbackResumeReason,
|
|
54
|
-
buildResumePrompt,
|
|
55
|
-
RpcRunControl,
|
|
56
|
-
getResultOutput,
|
|
57
|
-
isFailedResult,
|
|
58
|
-
isModelLevelFailure,
|
|
59
|
-
reviewVerdict,
|
|
60
|
-
runSingleAgentWithModelFallback,
|
|
61
|
-
type SingleResult,
|
|
62
|
-
type SubagentDetails,
|
|
63
|
-
type SubagentLiveEvent,
|
|
64
|
-
|
|
65
|
-
} from "./
|
|
66
|
-
import {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
type
|
|
71
|
-
type
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const
|
|
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
|
-
if (agent.name === "
|
|
138
|
-
if (agent.
|
|
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
|
-
const
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
"Use subagent with agent '
|
|
223
|
-
"Use subagent with agent '
|
|
224
|
-
"
|
|
225
|
-
"
|
|
226
|
-
"
|
|
227
|
-
"
|
|
228
|
-
"
|
|
229
|
-
"
|
|
230
|
-
"When a
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
//
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
monitor.
|
|
249
|
-
|
|
250
|
-
if (
|
|
251
|
-
if (opts?.
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
//
|
|
258
|
-
//
|
|
259
|
-
//
|
|
260
|
-
//
|
|
261
|
-
//
|
|
262
|
-
//
|
|
263
|
-
//
|
|
264
|
-
//
|
|
265
|
-
//
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
(
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
//
|
|
274
|
-
//
|
|
275
|
-
//
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
-
if (
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
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
|
-
task
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
);
|
|
446
|
-
|
|
447
|
-
result
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
runtime.registerRunResult(runId,
|
|
465
|
-
return { runId, result };
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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
|
-
if (!
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
parentThreadAtStart.
|
|
556
|
-
|
|
557
|
-
parentThreadAtStart.
|
|
558
|
-
parentThreadAtStart.
|
|
559
|
-
parentThreadAtStart.
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
chain
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
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
|
-
|
|
633
|
-
|
|
634
|
-
|
|
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
|
-
runtime.
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
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
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
const
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
cwd
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
const
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
const
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
}
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
(
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
)
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
}
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
}
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
thread.
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
runtime.
|
|
1389
|
-
!
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
await
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
thread.
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
return;
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
}
|
|
1662
|
-
|
|
1663
|
-
if (ownsSettlement())
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
}
|
|
1783
|
-
const
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
}
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
}
|
|
1835
|
-
if (args.tasks.length > 4) text += `\n ${theme.fg("dim", `… +${args.tasks.length - 4} more`)}`;
|
|
1836
|
-
return new Text(text, 0, 0);
|
|
1837
|
-
}
|
|
1838
|
-
const task: string = args.task ?? "";
|
|
1839
|
-
const preview = formatTaskSummary(task, 60);
|
|
1840
|
-
const isolation = args.isolation === "worktree" ? " [worktree]" : "";
|
|
1841
|
-
return new Text(
|
|
1842
|
-
`${theme.fg("toolTitle", theme.bold("subagent "))}${theme.fg("accent", args.agent ?? "?")}${theme.fg("dim", isolation)} ${theme.fg("dim", preview)}`,
|
|
1843
|
-
0,
|
|
1844
|
-
0,
|
|
1845
|
-
);
|
|
1846
|
-
},
|
|
1847
|
-
|
|
1848
|
-
renderResult(result, _options, theme) {
|
|
1849
|
-
const details = result.details as SubagentDetails | undefined;
|
|
1850
|
-
if (!details || details.results.length === 0) return new Text(theme.fg("dim", "(no output)"), 0, 0);
|
|
1851
|
-
|
|
1852
|
-
if (details.mode === "single") {
|
|
1853
|
-
const r = details.results[0];
|
|
1854
|
-
const pending = r.exitCode === -1;
|
|
1855
|
-
const icon = statusIcon(pending ? "running" : isFailedResult(r) ? "failed" : "done", theme);
|
|
1856
|
-
const usage = formatUsage(r.usage);
|
|
1857
|
-
const model = `${r.model ?? "?"}${r.modelFallbackFrom ? ` (pool fallback from ${r.modelFallbackFrom})` : ""}`;
|
|
1858
|
-
const isolation = r.isolation === "worktree" ? ` · worktree ${r.integrationStatus ?? "active"}` : "";
|
|
1859
|
-
const line = `${theme.fg("toolTitle", theme.bold("subagent "))}${icon} ${theme.fg("accent", r.agent)} ${theme.fg("dim", `· ${model}${r.thinking ? ` · thinking ${r.thinking}` : ""}${isolation}${pending ? " · background" : ""}${usage ? ` · ${usage}` : ""}`)}`;
|
|
1860
|
-
return new Text(line, 0, 0);
|
|
1861
|
-
}
|
|
1862
|
-
|
|
1863
|
-
// Parallel mode: header + one compact line per agent
|
|
1864
|
-
const lines: string[] = [
|
|
1865
|
-
`${theme.fg("toolTitle", theme.bold("subagent "))}${theme.fg("accent", `parallel (${details.results.length})`)}`,
|
|
1866
|
-
];
|
|
1867
|
-
for (const r of details.results) {
|
|
1868
|
-
const pending = r.exitCode === -1;
|
|
1869
|
-
const icon = statusIcon(pending ? "running" : isFailedResult(r) ? "failed" : "done", theme);
|
|
1870
|
-
const usage = formatUsage(r.usage);
|
|
1871
|
-
const model = `${r.model ?? "?"}${r.modelFallbackFrom ? ` (pool fallback from ${r.modelFallbackFrom})` : ""}`;
|
|
1872
|
-
const isolation = r.isolation === "worktree" ? ` · worktree ${r.integrationStatus ?? "active"}` : "";
|
|
1873
|
-
lines.push(` ${icon} ${theme.fg("accent", r.agent)} ${theme.fg("dim", `· ${model}${r.thinking ? ` · thinking ${r.thinking}` : ""}${isolation}${pending ? " · background" : ""}${usage ? ` · ${usage}` : ""}`)}`);
|
|
1874
|
-
}
|
|
1875
|
-
return new Text(lines.join("\n"), 0, 0);
|
|
1876
|
-
},
|
|
1877
|
-
});
|
|
1878
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The `subagent` tool: dispatches explore/worker/reviewer agents as isolated pi
|
|
3
|
+
* child processes, single or parallel. Owns the dispatch pipeline: config load,
|
|
4
|
+
* per-agent model-pool resolution, per-run status tracking, the auto-fix chain
|
|
5
|
+
* (REVIEW_FAIL → worker → re-review), and completion delivery.
|
|
6
|
+
*
|
|
7
|
+
* Vision: a task flagged `vision: true` uses the configured vision model as an
|
|
8
|
+
* explicit primary, then the agent's configured backup and the current
|
|
9
|
+
* main-window model. Stale refs remain in the pool and fail normally at runtime.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
13
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
15
|
+
import { existsSync } from "node:fs";
|
|
16
|
+
import { realpath, rm } from "node:fs/promises";
|
|
17
|
+
import { resolve } from "node:path";
|
|
18
|
+
import { Type } from "typebox";
|
|
19
|
+
import { discoverAgents, type AgentConfig } from "./agents.ts";
|
|
20
|
+
import {
|
|
21
|
+
completionTriggersTurn,
|
|
22
|
+
type CompletionMessageItem,
|
|
23
|
+
} from "./completion.ts";
|
|
24
|
+
import { loadConfig, type SubagentsConfig } from "./config.ts";
|
|
25
|
+
import {
|
|
26
|
+
dispatchFailedResult,
|
|
27
|
+
failedStartResult,
|
|
28
|
+
formatCompletionBlock,
|
|
29
|
+
formatUsage,
|
|
30
|
+
modelLevelTakeoverNote,
|
|
31
|
+
queuedResult,
|
|
32
|
+
} from "./format.ts";
|
|
33
|
+
import {
|
|
34
|
+
buildFixTaskBrief,
|
|
35
|
+
buildReReviewBrief,
|
|
36
|
+
formatChainSummary,
|
|
37
|
+
shouldTriggerFixLoop,
|
|
38
|
+
summarizeChainResult,
|
|
39
|
+
type ChainStep,
|
|
40
|
+
} from "./fixloop.ts";
|
|
41
|
+
import { currentModelRef, resolveAgentModelPool } from "./models.ts";
|
|
42
|
+
import {
|
|
43
|
+
formatTaskSummary,
|
|
44
|
+
formatToolActivity,
|
|
45
|
+
monitor,
|
|
46
|
+
statusIcon,
|
|
47
|
+
type RunChainMeta,
|
|
48
|
+
} from "./monitor.ts";
|
|
49
|
+
import type { SubagentRuntime, SubagentThread, ThreadState } from "./runtime.ts";
|
|
50
|
+
import { persistRecoveryRecords, recoveryRecordFromFinalization } from "./recovery.ts";
|
|
51
|
+
import { forkRetainedSession } from "./session-fork.ts";
|
|
52
|
+
import {
|
|
53
|
+
buildFallbackResumeReason,
|
|
54
|
+
buildResumePrompt,
|
|
55
|
+
RpcRunControl,
|
|
56
|
+
getResultOutput,
|
|
57
|
+
isFailedResult,
|
|
58
|
+
isModelLevelFailure,
|
|
59
|
+
reviewVerdict,
|
|
60
|
+
runSingleAgentWithModelFallback,
|
|
61
|
+
type SingleResult,
|
|
62
|
+
type SubagentDetails,
|
|
63
|
+
type SubagentLiveEvent,
|
|
64
|
+
} from "./spawn.ts";
|
|
65
|
+
import { trajectoryStore, summarizeToolArgs } from "./trajectory.ts";
|
|
66
|
+
import {
|
|
67
|
+
createWorktreeIsolation,
|
|
68
|
+
resolveWorktreeTarget,
|
|
69
|
+
type IsolationMode,
|
|
70
|
+
type WorktreeFinalization,
|
|
71
|
+
type WorktreeIsolation,
|
|
72
|
+
} from "./worktree.ts";
|
|
73
|
+
|
|
74
|
+
const NON_BLANK_TASK_OPTIONS = { minLength: 1, pattern: "\\S" } as const;
|
|
75
|
+
export const FORK_CONTINUATION_PROMPT =
|
|
76
|
+
"Continue from the retained context above. Review the prior work, then take the most useful next step toward completing the existing objective without repeating completed work.";
|
|
77
|
+
export const WORKTREE_ISOLATION_INSTRUCTIONS =
|
|
78
|
+
"You are running in a temporary detached Git worktree. Work only in the current cwd; do not create another worktree or manually copy/apply changes to the original checkout. The parent dispatcher will integrate your tracked, deleted, and untracked changes when this thread finally settles.";
|
|
79
|
+
|
|
80
|
+
export function buildWorktreeTaskPrompt(task: string): string {
|
|
81
|
+
return `${WORKTREE_ISOLATION_INSTRUCTIONS}\n\nTask: ${task}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function withWorktreeSystemPrompt(agent: AgentConfig): AgentConfig {
|
|
85
|
+
return {
|
|
86
|
+
...agent,
|
|
87
|
+
systemPrompt: `${agent.systemPrompt.trimEnd()}\n\n${WORKTREE_ISOLATION_INSTRUCTIONS}`.trim(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface DispatchEnvironment {
|
|
92
|
+
ctx: ExtensionContext;
|
|
93
|
+
config: SubagentsConfig;
|
|
94
|
+
agents: AgentConfig[];
|
|
95
|
+
sessionRef?: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const VISION_DESCRIPTION =
|
|
99
|
+
"Set true when the task may require viewing images (screenshots, mockups, designs) — the configured vision model becomes primary, followed by the agent backup and current main-window model";
|
|
100
|
+
|
|
101
|
+
const ISOLATION_DESCRIPTION =
|
|
102
|
+
"Filesystem isolation: shared uses the caller's working tree; worktree creates a detached temporary Git worktree (write-capable agents only)";
|
|
103
|
+
|
|
104
|
+
const IsolationSchema = Type.Optional(
|
|
105
|
+
StringEnum(["shared", "worktree"] as const, { description: ISOLATION_DESCRIPTION }),
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const TaskItem = Type.Object({
|
|
109
|
+
agent: Type.String({ description: "Name of the agent to invoke" }),
|
|
110
|
+
task: Type.String({
|
|
111
|
+
...NON_BLANK_TASK_OPTIONS,
|
|
112
|
+
description: "Self-contained task to delegate (the agent has no memory of this conversation)",
|
|
113
|
+
}),
|
|
114
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
|
|
115
|
+
vision: Type.Optional(Type.Boolean({ description: VISION_DESCRIPTION })),
|
|
116
|
+
isolation: IsolationSchema,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const SubagentParams = Type.Object({
|
|
120
|
+
agent: Type.Optional(Type.String({ description: "Name of the agent to invoke (single mode)" })),
|
|
121
|
+
task: Type.Optional(
|
|
122
|
+
Type.String({ ...NON_BLANK_TASK_OPTIONS, description: "Self-contained task to delegate (single mode)" }),
|
|
123
|
+
),
|
|
124
|
+
tasks: Type.Optional(Type.Array(TaskItem, { description: "Array of {agent, task} for parallel execution" })),
|
|
125
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process (single mode)" })),
|
|
126
|
+
vision: Type.Optional(Type.Boolean({ description: VISION_DESCRIPTION })),
|
|
127
|
+
isolation: IsolationSchema,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
export function defaultIsolationMode(mode: "single" | "parallel", agentName: string, requested?: IsolationMode): IsolationMode {
|
|
131
|
+
if (requested) return requested;
|
|
132
|
+
return mode === "parallel" && agentName === "worker" ? "worktree" : "shared";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function isWorktreeCapableAgent(agent: AgentConfig): boolean {
|
|
136
|
+
if (agent.name === "explore" || agent.name === "reviewer") return false;
|
|
137
|
+
if (agent.name === "worker") return true;
|
|
138
|
+
if (!agent.tools) return true;
|
|
139
|
+
return agent.tools.includes("edit") || agent.tools.includes("write");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const autoFixRootTails = new Map<string, Promise<void>>();
|
|
143
|
+
|
|
144
|
+
async function canonicalAutoFixRoot(cwd: string): Promise<string> {
|
|
145
|
+
try {
|
|
146
|
+
return (await resolveWorktreeTarget(cwd)).originalRoot;
|
|
147
|
+
} catch {
|
|
148
|
+
try {
|
|
149
|
+
return await realpath(resolve(cwd));
|
|
150
|
+
} catch {
|
|
151
|
+
return resolve(cwd);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Keep the complete worker→review loop exclusive for one canonical repository.
|
|
157
|
+
* Child processes have independent file-mutation queues, so queue concurrency
|
|
158
|
+
* alone cannot make shared-checkout edits safe. */
|
|
159
|
+
function serializeAutoFixChain(
|
|
160
|
+
cwd: string,
|
|
161
|
+
task: (signal: AbortSignal) => Promise<void>,
|
|
162
|
+
): (signal: AbortSignal) => Promise<void> {
|
|
163
|
+
return async (signal) => {
|
|
164
|
+
if (signal.aborted) return;
|
|
165
|
+
const root = await canonicalAutoFixRoot(cwd);
|
|
166
|
+
const key = process.platform === "win32" ? root.toLowerCase() : root;
|
|
167
|
+
const previous = autoFixRootTails.get(key) ?? Promise.resolve();
|
|
168
|
+
let release!: () => void;
|
|
169
|
+
const gate = new Promise<void>((resolveGate) => {
|
|
170
|
+
release = resolveGate;
|
|
171
|
+
});
|
|
172
|
+
const tail = previous.catch(() => undefined).then(() => gate);
|
|
173
|
+
autoFixRootTails.set(key, tail);
|
|
174
|
+
await previous.catch(() => undefined);
|
|
175
|
+
try {
|
|
176
|
+
if (!signal.aborted) await task(signal);
|
|
177
|
+
} finally {
|
|
178
|
+
release();
|
|
179
|
+
if (autoFixRootTails.get(key) === tail) autoFixRootTails.delete(key);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function resolveDispatchModelPool(
|
|
185
|
+
agent: AgentConfig,
|
|
186
|
+
config: SubagentsConfig,
|
|
187
|
+
mainRef: string | undefined,
|
|
188
|
+
vision: boolean,
|
|
189
|
+
): { agent: AgentConfig; fallbackModelRefs: string[] } {
|
|
190
|
+
const pool = resolveAgentModelPool({
|
|
191
|
+
primaryRef: vision ? config.visionModel : config.agentModels[agent.name],
|
|
192
|
+
backupRef: config.agentBackupModels[agent.name],
|
|
193
|
+
mainRef,
|
|
194
|
+
declaredDefaultRef: agent.model,
|
|
195
|
+
});
|
|
196
|
+
return {
|
|
197
|
+
agent: { ...agent, model: pool.primaryRef },
|
|
198
|
+
fallbackModelRefs: pool.fallbackModelRefs,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function registerSubagentTool(pi: ExtensionAPI, runtime: SubagentRuntime): void {
|
|
203
|
+
pi.registerTool({
|
|
204
|
+
name: "subagent",
|
|
205
|
+
label: "Subagent",
|
|
206
|
+
description: [
|
|
207
|
+
"Delegate a discrete, self-contained task to a specialized sub-agent running in an ISOLATED context window.",
|
|
208
|
+
"Agents: explore (read-only codebase recon), worker (implement/fix/refactor/test, full tools), reviewer (adversarial pre-commit review, read-only).",
|
|
209
|
+
"Modes: single ({agent, task}) or parallel ({tasks: [{agent, task}, ...]}).",
|
|
210
|
+
"Isolation: single tasks default to shared; parallel worker tasks default to detached Git worktrees unless isolation: shared is explicit. explore/reviewer cannot use worktree isolation.",
|
|
211
|
+
"Use subagent_control to steer, retarget, park, resume, or fork a thread by its stable run id.",
|
|
212
|
+
"It starts agents in the background and immediately returns control to the main window; completion messages automatically wake the main agent to continue.",
|
|
213
|
+
"Each agent has no memory of this conversation — brief it fully (goal, exact paths, constraints, expected output).",
|
|
214
|
+
"Results arrive as wake-up messages automatically — you do NOT need to wait. If you must get a result in-turn, subagent_wait is a non-blocking lookup by default (pass timeoutMs to block).",
|
|
215
|
+
"Vision: set vision: true when the task may require viewing images (screenshots, mockups, design files — e.g. frontend work) — the configured vision model is primary, followed by that agent's backup and the current main-window model.",
|
|
216
|
+
].join(" "),
|
|
217
|
+
promptSnippet:
|
|
218
|
+
"Start background subagents: explore (read-only search), worker (implement), reviewer (adversarial review); completion automatically resumes the main agent. Simple tasks: use direct tools, not subagents.",
|
|
219
|
+
promptGuidelines: [
|
|
220
|
+
"Delegate only when an isolated context genuinely pays: broad exploration, a self-contained implementation, or a review gate. Handle simple lookups and one-line edits inline with direct tools — never spawn a sub-agent for them.",
|
|
221
|
+
"Use subagent with agent 'explore' for broad or open-ended code search before large changes; a targeted 'where is X' is a direct grep/read.",
|
|
222
|
+
"Use subagent with agent 'worker' for a self-contained implementation task worth a separate context; it plans internally.",
|
|
223
|
+
"Use subagent with agent 'reviewer' for a fresh read-only review before reporting work done or committing.",
|
|
224
|
+
"subagent launches work in the background and ends the current turn; when a result arrives, the main agent is automatically resumed with it.",
|
|
225
|
+
"Run independent tasks in parallel by passing a tasks array to subagent; parallel worker items default to isolation: worktree so their edits are integrated independently. Pass isolation: shared only when workers intentionally need the caller's live uncommitted tree.",
|
|
226
|
+
"Use isolation: worktree only for worker/write-capable agents and only inside a Git repository with a committed HEAD; setup or integration failures never silently fall back to shared.",
|
|
227
|
+
"NEVER sleep or poll, and do NOT call subagent_wait to hold the turn — subagent ends the turn immediately and the result arrives as a message that wakes you automatically (even mid-turn). Ending your turn is the default and the only correct way to wait.",
|
|
228
|
+
"If you must keep the turn for a result, call subagent_wait with an explicit timeoutMs (non-blocking by default) — never bash sleep/timeout to wait for a sub-agent.",
|
|
229
|
+
"When a delegated task may require viewing images (frontend screenshots, mockups, design comparisons), pass vision: true and give the sub-agent the exact image paths — it reads them with its read tool. The configured vision model becomes primary; model-level failures continue through the agent's backup pool and current main-window model.",
|
|
230
|
+
"When a sub-agent result arrives it is already shown to the user — do NOT restate, paraphrase, or summarize it; reply with only your own conclusion or next action (often just one line), since duplicating the result wastes tokens for nothing.",
|
|
231
|
+
],
|
|
232
|
+
parameters: SubagentParams,
|
|
233
|
+
|
|
234
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
235
|
+
monitor.beginTurn();
|
|
236
|
+
const config = await loadConfig(runtime.configPath);
|
|
237
|
+
// Pick up concurrency changes from /subagents-setup without a restart.
|
|
238
|
+
runtime.backgroundQueue.setConcurrency(config.maxConcurrency);
|
|
239
|
+
|
|
240
|
+
// Finished runs leave the active monitor immediately. Their final findings
|
|
241
|
+
// are sent as a custom message that starts a follow-up turn.
|
|
242
|
+
const finishRun = (
|
|
243
|
+
runId: number,
|
|
244
|
+
status: "done" | "failed",
|
|
245
|
+
opts?: { silent?: boolean; retain?: boolean },
|
|
246
|
+
): void => {
|
|
247
|
+
monitor.setStatus(runId, status); // stamps endedAt for the elapsed time
|
|
248
|
+
const run = opts?.retain ? monitor.findRun(runId) : monitor.removeRun(runId);
|
|
249
|
+
if (!run) return; // already finished — stay idempotent
|
|
250
|
+
if (opts?.retain) monitor.setRetained(runId, true);
|
|
251
|
+
if (opts?.silent || !runtime.sessionActive) return;
|
|
252
|
+
const icon = status === "done" ? "✓" : "✗";
|
|
253
|
+
ctx.ui.notify(`${icon} ${monitor.summarize(run)}`, status === "done" ? "info" : "error");
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// Live sub-agent activity → concise one-line status ("thinking",
|
|
257
|
+
// "read src/index.ts", ...), never a raw args blob. In parallel, every
|
|
258
|
+
// live event is appended to the thread's append-only trajectory (status,
|
|
259
|
+
// model-candidate changes, usage, tool starts/ends with a redacted
|
|
260
|
+
// args summary). The live handler only updates monitor state; finishing
|
|
261
|
+
// (removeRun + notify) is
|
|
262
|
+
// owned by the queue task / launchInLoop. That keeps a startup retry —
|
|
263
|
+
// which fires a transient "failed" status before relaunching — from
|
|
264
|
+
// ripping the row out early, and lets the queue task decide between
|
|
265
|
+
// delivering a reviewer's result and starting an auto-fix chain.
|
|
266
|
+
const makeLiveHandler =
|
|
267
|
+
(runId: number, threadId?: number, generation?: number) =>
|
|
268
|
+
(e: SubagentLiveEvent): void => {
|
|
269
|
+
if (generation !== undefined && runtime.threads.get(runId)?.generation !== generation) return;
|
|
270
|
+
switch (e.kind) {
|
|
271
|
+
case "status":
|
|
272
|
+
// Only update monitor status here. Finishing (removeRun + notify) is
|
|
273
|
+
// owned by the queue task / launchInLoop so that a startup retry — which
|
|
274
|
+
// fires a transient "failed" status before relaunching the child — never
|
|
275
|
+
// rips the row out from under the retry or emits a premature "✗" toast.
|
|
276
|
+
monitor.setStatus(runId, e.status);
|
|
277
|
+
break;
|
|
278
|
+
case "model":
|
|
279
|
+
monitor.setModel(runId, e.model, e.fallbackFrom);
|
|
280
|
+
break;
|
|
281
|
+
case "usage":
|
|
282
|
+
monitor.setUsage(runId, e.usage, e.model);
|
|
283
|
+
break;
|
|
284
|
+
case "tool_start":
|
|
285
|
+
monitor.recordToolStart(runId, e.toolName, formatToolActivity(e.toolName, e.args));
|
|
286
|
+
break;
|
|
287
|
+
case "tool_end":
|
|
288
|
+
monitor.recordToolEnd(runId, e.toolName, e.isError);
|
|
289
|
+
break;
|
|
290
|
+
case "thinking":
|
|
291
|
+
monitor.setActivity(runId, "thinking");
|
|
292
|
+
break;
|
|
293
|
+
case "text":
|
|
294
|
+
// A text delta is model output, not a filesystem write.
|
|
295
|
+
monitor.setActivity(runId, "responding");
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
if (threadId !== undefined) {
|
|
299
|
+
const trajectory = trajectoryStore.get(threadId).trajectory;
|
|
300
|
+
switch (e.kind) {
|
|
301
|
+
case "status":
|
|
302
|
+
trajectory.append({ kind: "status", status: e.status });
|
|
303
|
+
break;
|
|
304
|
+
case "model":
|
|
305
|
+
trajectory.append({ kind: "candidate", model: e.model, fallbackFrom: e.fallbackFrom });
|
|
306
|
+
break;
|
|
307
|
+
case "usage":
|
|
308
|
+
trajectory.append({ kind: "usage", usage: { ...e.usage }, model: e.model });
|
|
309
|
+
break;
|
|
310
|
+
case "tool_start":
|
|
311
|
+
trajectory.append({
|
|
312
|
+
kind: "tool_start",
|
|
313
|
+
tool: e.toolName,
|
|
314
|
+
toolCallId: e.toolCallId,
|
|
315
|
+
summary: summarizeToolArgs(e.args),
|
|
316
|
+
});
|
|
317
|
+
break;
|
|
318
|
+
case "tool_end":
|
|
319
|
+
trajectory.append({ kind: "tool_end", tool: e.toolName, toolCallId: e.toolCallId, isError: e.isError });
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const discovery = discoverAgents(ctx.cwd, {
|
|
326
|
+
scope: config.agentScope,
|
|
327
|
+
enabledNames: config.enabledAgents,
|
|
328
|
+
projectTrusted: ctx.isProjectTrusted?.() === true,
|
|
329
|
+
});
|
|
330
|
+
const sessionRef = currentModelRef(ctx);
|
|
331
|
+
const agents = discovery.agents;
|
|
332
|
+
|
|
333
|
+
const hasTasks = (params.tasks?.length ?? 0) > 0;
|
|
334
|
+
const hasSingle = Boolean(params.agent) && params.task !== undefined;
|
|
335
|
+
|
|
336
|
+
const makeDetails =
|
|
337
|
+
(mode: "single" | "parallel", background = false) =>
|
|
338
|
+
(results: SingleResult[]): SubagentDetails => ({ mode, results, background });
|
|
339
|
+
|
|
340
|
+
const catalog = agents.map((a) => a.name).join(", ") || "none";
|
|
341
|
+
|
|
342
|
+
if (Number(hasTasks) + Number(hasSingle) !== 1) {
|
|
343
|
+
return {
|
|
344
|
+
content: [
|
|
345
|
+
{
|
|
346
|
+
type: "text",
|
|
347
|
+
text: `Invalid parameters. Provide exactly one mode: single {agent, task} or parallel {tasks: [...]}. Enabled agents: ${catalog}.`,
|
|
348
|
+
},
|
|
349
|
+
],
|
|
350
|
+
details: makeDetails("single")([]),
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (hasTasks) {
|
|
355
|
+
const blankTaskIndex = params.tasks?.findIndex(({ task }) => task.trim().length === 0) ?? -1;
|
|
356
|
+
if (blankTaskIndex !== -1) {
|
|
357
|
+
return {
|
|
358
|
+
content: [
|
|
359
|
+
{
|
|
360
|
+
type: "text",
|
|
361
|
+
text: `Invalid parameters. tasks[${blankTaskIndex}].task must contain at least one non-whitespace character. No background tasks were started. Enabled agents: ${catalog}.`,
|
|
362
|
+
},
|
|
363
|
+
],
|
|
364
|
+
details: makeDetails("parallel")([]),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
} else if (params.task?.trim().length === 0) {
|
|
368
|
+
return {
|
|
369
|
+
content: [
|
|
370
|
+
{
|
|
371
|
+
type: "text",
|
|
372
|
+
text: `Invalid parameters. task must contain at least one non-whitespace character. Enabled agents: ${catalog}.`,
|
|
373
|
+
},
|
|
374
|
+
],
|
|
375
|
+
details: makeDetails("single")([]),
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Dispatch one agent inside an auto-fix chain: tracked in monitor state with a
|
|
381
|
+
* groupId/relationLabel, but NOT delivered through the completion flow — the
|
|
382
|
+
* chain owner assembles and delivers the whole group at the end.
|
|
383
|
+
*/
|
|
384
|
+
const launchInLoop = async (
|
|
385
|
+
agentName: string,
|
|
386
|
+
task: string,
|
|
387
|
+
executionCwd: string,
|
|
388
|
+
signal: AbortSignal,
|
|
389
|
+
meta: RunChainMeta,
|
|
390
|
+
vision = false,
|
|
391
|
+
): Promise<{ runId?: number; result: SingleResult }> => {
|
|
392
|
+
const agent = agents.find((candidate) => candidate.name === agentName);
|
|
393
|
+
if (!agent) return { result: failedStartResult(agentName, task, `Unknown agent: "${agentName}".`) };
|
|
394
|
+
// Vision chains keep the vision override as each round's primary while
|
|
395
|
+
// retaining that worker/reviewer's own configured backup pool.
|
|
396
|
+
const pool = resolveDispatchModelPool(agent, config, sessionRef, vision);
|
|
397
|
+
const thinkingLevel = config.agentThinkingLevels[agent.name] ?? agent.thinking ?? config.thinkingLevel;
|
|
398
|
+
const runId = monitor.addRun(agent.name, task, pool.agent.model, thinkingLevel, meta);
|
|
399
|
+
// Chain rounds keep their own lifecycle trajectory.
|
|
400
|
+
const chainState = trajectoryStore.get(runId);
|
|
401
|
+
chainState.trajectory.append({
|
|
402
|
+
kind: "dispatch",
|
|
403
|
+
agent: agent.name,
|
|
404
|
+
task,
|
|
405
|
+
model: pool.agent.model,
|
|
406
|
+
thinking: thinkingLevel,
|
|
407
|
+
pool: pool.fallbackModelRefs,
|
|
408
|
+
vision,
|
|
409
|
+
isolation: "shared",
|
|
410
|
+
originalCwd: executionCwd,
|
|
411
|
+
isolationCwd: executionCwd,
|
|
412
|
+
});
|
|
413
|
+
const onLive = makeLiveHandler(runId, runId);
|
|
414
|
+
try {
|
|
415
|
+
const result = await runSingleAgentWithModelFallback(
|
|
416
|
+
{
|
|
417
|
+
defaultCwd: executionCwd,
|
|
418
|
+
cwd: executionCwd,
|
|
419
|
+
agent: pool.agent,
|
|
420
|
+
agentName,
|
|
421
|
+
task,
|
|
422
|
+
thinkingLevel,
|
|
423
|
+
signal,
|
|
424
|
+
onLive,
|
|
425
|
+
makeDetails: makeDetails("single", true),
|
|
426
|
+
idleTimeoutMs: config.idleTimeoutSec * 1000,
|
|
427
|
+
},
|
|
428
|
+
pool.fallbackModelRefs,
|
|
429
|
+
);
|
|
430
|
+
result.runId = runId;
|
|
431
|
+
result.isolation = "shared";
|
|
432
|
+
result.originalCwd = executionCwd;
|
|
433
|
+
result.isolationCwd = executionCwd;
|
|
434
|
+
runtime.retainSession(result);
|
|
435
|
+
monitor.setModel(runId, result.model, result.modelFallbackFrom);
|
|
436
|
+
chainState.trajectory.append({
|
|
437
|
+
kind: "settled",
|
|
438
|
+
status: isFailedResult(result) ? "failed" : "done",
|
|
439
|
+
model: result.model,
|
|
440
|
+
});
|
|
441
|
+
// Keep the finished round in status state while the chain is
|
|
442
|
+
// still running, with a one-line summary of what it did; the whole
|
|
443
|
+
// group is dropped when the chain resolves (see removeChainGroup).
|
|
444
|
+
monitor.setSummary(runId, summarizeChainResult(result));
|
|
445
|
+
finishRun(runId, isFailedResult(result) ? "failed" : "done", { retain: true });
|
|
446
|
+
runtime.registerRunResult(runId, result);
|
|
447
|
+
return { runId, result };
|
|
448
|
+
} catch (error) {
|
|
449
|
+
finishRun(runId, "failed", { retain: true });
|
|
450
|
+
chainState.trajectory.append({ kind: "settled", status: "failed", model: pool.agent.model });
|
|
451
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
452
|
+
const crashed: SingleResult = {
|
|
453
|
+
...queuedResult(pool.agent, task, thinkingLevel),
|
|
454
|
+
runId,
|
|
455
|
+
isolation: "shared",
|
|
456
|
+
originalCwd: executionCwd,
|
|
457
|
+
isolationCwd: executionCwd,
|
|
458
|
+
exitCode: 1,
|
|
459
|
+
stderr: errorMessage,
|
|
460
|
+
stopReason: signal.aborted ? "aborted" : "error",
|
|
461
|
+
errorMessage,
|
|
462
|
+
dispatchFailed: true,
|
|
463
|
+
};
|
|
464
|
+
runtime.registerRunResult(runId, crashed);
|
|
465
|
+
return { runId, result: crashed };
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Run the auto-fix chain in the background: worker (briefed with the review's
|
|
471
|
+
* findings) → reviewer re-review, up to maxFixRounds times. The main agent is
|
|
472
|
+
* not woken mid-loop; the full chain is delivered as one group at the end.
|
|
473
|
+
* Failures short-circuit: a crashed worker skips its re-review and delivers.
|
|
474
|
+
* The triggering reviewer stays in monitor state until the chain resolves.
|
|
475
|
+
*/
|
|
476
|
+
/** Drop every monitor row belonging to an auto-fix chain; the retained
|
|
477
|
+
* parent is removed separately (it does not carry the groupId). */
|
|
478
|
+
const removeChainGroup = (groupId: string): void => {
|
|
479
|
+
for (const run of [...monitor.getRuns()]) {
|
|
480
|
+
if (run.groupId === groupId) monitor.removeRun(run.id);
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const startFixLoop = (
|
|
485
|
+
initialReviewerResult: SingleResult,
|
|
486
|
+
parentGroupId: string,
|
|
487
|
+
parentRunId: number,
|
|
488
|
+
executionCwd: string,
|
|
489
|
+
vision = false,
|
|
490
|
+
): void => {
|
|
491
|
+
const parentThreadAtStart = runtime.threads.get(parentRunId);
|
|
492
|
+
if (!parentThreadAtStart) return;
|
|
493
|
+
const parentGeneration = parentThreadAtStart.generation;
|
|
494
|
+
const parentControl = parentThreadAtStart.control;
|
|
495
|
+
let fixController: AbortController | undefined;
|
|
496
|
+
const ownsParent = (): boolean => {
|
|
497
|
+
const current = runtime.threads.get(parentRunId);
|
|
498
|
+
return fixController !== undefined &&
|
|
499
|
+
current === parentThreadAtStart &&
|
|
500
|
+
current.generation === parentGeneration &&
|
|
501
|
+
current.control === parentControl &&
|
|
502
|
+
current.queueController === fixController &&
|
|
503
|
+
runtime.runControllers.get(parentRunId) === fixController;
|
|
504
|
+
};
|
|
505
|
+
const clearOwnedController = (): void => {
|
|
506
|
+
if (!fixController) return;
|
|
507
|
+
if (runtime.runControllers.get(parentRunId) === fixController) {
|
|
508
|
+
runtime.runControllers.delete(parentRunId);
|
|
509
|
+
}
|
|
510
|
+
const current = runtime.threads.get(parentRunId);
|
|
511
|
+
if (current === parentThreadAtStart && current.queueController === fixController) {
|
|
512
|
+
current.queueController = undefined;
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
fixController = runtime.backgroundQueue.enqueue(
|
|
516
|
+
serializeAutoFixChain(executionCwd, async (signal) => {
|
|
517
|
+
const chain: ChainStep[] = [
|
|
518
|
+
{ runId: parentRunId, result: initialReviewerResult, relation: "initial review" },
|
|
519
|
+
];
|
|
520
|
+
let lastReviewer = initialReviewerResult;
|
|
521
|
+
for (let round = 1; round <= config.maxFixRounds; round++) {
|
|
522
|
+
if (!runtime.sessionActive) break;
|
|
523
|
+
const fixBrief = buildFixTaskBrief(lastReviewer, round, config.maxFixRounds);
|
|
524
|
+
const workerStep = await launchInLoop("worker", fixBrief, executionCwd, signal, {
|
|
525
|
+
groupId: parentGroupId,
|
|
526
|
+
relationLabel: `fix round ${round}`,
|
|
527
|
+
}, vision);
|
|
528
|
+
// Preserve the newest sub-step before checking chain ownership. A
|
|
529
|
+
// destructive stop invalidates ownsParent() while this child is
|
|
530
|
+
// aborting, and its partial output must become the parent's stopped
|
|
531
|
+
// result instead of falling back to the old triggering review.
|
|
532
|
+
if (
|
|
533
|
+
runtime.threads.get(parentRunId) === parentThreadAtStart &&
|
|
534
|
+
parentThreadAtStart.generation === parentGeneration
|
|
535
|
+
) {
|
|
536
|
+
parentThreadAtStart.lastResult = workerStep.result;
|
|
537
|
+
parentThreadAtStart.agentName = workerStep.result.agent;
|
|
538
|
+
parentThreadAtStart.task = workerStep.result.task;
|
|
539
|
+
parentThreadAtStart.sessionId = workerStep.result.sessionId;
|
|
540
|
+
parentThreadAtStart.sessionDir = workerStep.result.sessionDir;
|
|
541
|
+
runtime.retainSession(workerStep.result);
|
|
542
|
+
}
|
|
543
|
+
if (!ownsParent()) return;
|
|
544
|
+
chain.push({ ...workerStep, relation: `fix round ${round}` });
|
|
545
|
+
if (!runtime.sessionActive || isFailedResult(workerStep.result)) break;
|
|
546
|
+
const reReviewBrief = buildReReviewBrief(lastReviewer, round);
|
|
547
|
+
const reviewStep = await launchInLoop("reviewer", reReviewBrief, executionCwd, signal, {
|
|
548
|
+
groupId: parentGroupId,
|
|
549
|
+
relationLabel: `re-review round ${round}`,
|
|
550
|
+
}, vision);
|
|
551
|
+
if (
|
|
552
|
+
runtime.threads.get(parentRunId) === parentThreadAtStart &&
|
|
553
|
+
parentThreadAtStart.generation === parentGeneration
|
|
554
|
+
) {
|
|
555
|
+
parentThreadAtStart.lastResult = reviewStep.result;
|
|
556
|
+
parentThreadAtStart.agentName = reviewStep.result.agent;
|
|
557
|
+
parentThreadAtStart.task = reviewStep.result.task;
|
|
558
|
+
parentThreadAtStart.sessionId = reviewStep.result.sessionId;
|
|
559
|
+
parentThreadAtStart.sessionDir = reviewStep.result.sessionDir;
|
|
560
|
+
runtime.retainSession(reviewStep.result);
|
|
561
|
+
}
|
|
562
|
+
if (!ownsParent()) return;
|
|
563
|
+
chain.push({ ...reviewStep, relation: `re-review round ${round}` });
|
|
564
|
+
lastReviewer = reviewStep.result;
|
|
565
|
+
// A crashed re-review must stop the chain like a crashed worker: its
|
|
566
|
+
// output (if any) is not a verdict, and feeding it to the next fix
|
|
567
|
+
// round would brief the worker from garbage.
|
|
568
|
+
if (!runtime.sessionActive || isFailedResult(reviewStep.result)) break;
|
|
569
|
+
if (reviewVerdict(getResultOutput(reviewStep.result)) === "pass") break;
|
|
570
|
+
}
|
|
571
|
+
// Every parent mutation is guarded by the exact generation, control, and
|
|
572
|
+
// queue controller that started this chain. A parked/resumed generation or
|
|
573
|
+
// destructive stop must make this old orchestration a no-op.
|
|
574
|
+
if (!ownsParent()) return;
|
|
575
|
+
const controlledParent = parentThreadAtStart;
|
|
576
|
+
if (controlledParent.retired || controlledParent.state === "stopped") {
|
|
577
|
+
clearOwnedController();
|
|
578
|
+
removeChainGroup(parentGroupId);
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
// Parking an auto-fix chain aborts its in-flight child but preserves the
|
|
582
|
+
// parent's retained checkpoint and suppresses an aborted chain delivery.
|
|
583
|
+
if (controlledParent.state === "parked") {
|
|
584
|
+
clearOwnedController();
|
|
585
|
+
removeChainGroup(parentGroupId);
|
|
586
|
+
monitor.setRetained(parentRunId, false);
|
|
587
|
+
monitor.setStatus(parentRunId, "parked");
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
// The chain is done (success, exhaustion, or abort): drop the retained
|
|
591
|
+
// parent row and its retained round rows, then deliver one condensed
|
|
592
|
+
// summary. Register the parent's final state (the last chain result)
|
|
593
|
+
// before removal so subagent_wait can resolve it.
|
|
594
|
+
const last = chain[chain.length - 1];
|
|
595
|
+
runtime.registerRunResult(parentRunId, last.result);
|
|
596
|
+
removeChainGroup(parentGroupId);
|
|
597
|
+
monitor.removeRun(parentRunId);
|
|
598
|
+
runtime.retainSession(last.result);
|
|
599
|
+
const parentThread = parentThreadAtStart;
|
|
600
|
+
parentThread.agentName = last.result.agent;
|
|
601
|
+
parentThread.task = last.result.task;
|
|
602
|
+
parentThread.sessionId = last.result.sessionId;
|
|
603
|
+
parentThread.sessionDir = last.result.sessionDir;
|
|
604
|
+
parentThread.state = isFailedResult(last.result) ? "failed" : "completed";
|
|
605
|
+
// The chain outcome settles the parent thread's trajectory: the
|
|
606
|
+
// last chain step is its final state.
|
|
607
|
+
const parentTrajectory = trajectoryStore.get(parentRunId);
|
|
608
|
+
parentTrajectory.trajectory.append({
|
|
609
|
+
kind: "settled",
|
|
610
|
+
status: parentThread.state === "failed" ? "failed" : "done",
|
|
611
|
+
model: last.result.model,
|
|
612
|
+
});
|
|
613
|
+
if (!runtime.sessionActive) {
|
|
614
|
+
clearOwnedController();
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
// One compact message instead of every round's raw output: the summary
|
|
618
|
+
// lines cover each step (verdict + what changed/found), and the final
|
|
619
|
+
// step's full report is appended only when its detail is actionable
|
|
620
|
+
// (a FAIL verdict, a crash, or a model-level failure the main agent
|
|
621
|
+
// must take over). Everything else stays one `subagent_status #id`
|
|
622
|
+
// call away.
|
|
623
|
+
let block = formatChainSummary(chain);
|
|
624
|
+
if (isFailedResult(last.result) && isModelLevelFailure(last.result)) {
|
|
625
|
+
block = `${block}\n\n${formatCompletionBlock(last.result, config.maxResultLines, executionCwd)}\n\n${modelLevelTakeoverNote(last.result, { runId: parentRunId })}`;
|
|
626
|
+
} else if (isFailedResult(last.result) || reviewVerdict(getResultOutput(last.result)) === "fail") {
|
|
627
|
+
block = `${block}\n\n${formatCompletionBlock(last.result, config.maxResultLines, executionCwd)}`;
|
|
628
|
+
}
|
|
629
|
+
runtime.sendCompletionGroup([
|
|
630
|
+
{
|
|
631
|
+
agent: `auto-fix chain (${last.result.agent})`,
|
|
632
|
+
block,
|
|
633
|
+
triggerTurn: true,
|
|
634
|
+
},
|
|
635
|
+
]);
|
|
636
|
+
runtime.completionBatcher.flush();
|
|
637
|
+
clearOwnedController();
|
|
638
|
+
}),
|
|
639
|
+
() => {
|
|
640
|
+
if (!ownsParent()) return;
|
|
641
|
+
const controlledParent = parentThreadAtStart;
|
|
642
|
+
clearOwnedController();
|
|
643
|
+
removeChainGroup(parentGroupId);
|
|
644
|
+
if (controlledParent.state === "parked") {
|
|
645
|
+
monitor.setRetained(parentRunId, false);
|
|
646
|
+
monitor.setStatus(parentRunId, "parked");
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
if (!controlledParent.retired) monitor.removeRun(parentRunId);
|
|
650
|
+
},
|
|
651
|
+
(error) => {
|
|
652
|
+
// A crash inside the chain orchestration (failed runs are caught by
|
|
653
|
+
// launchInLoop and delivered as part of the chain) must not vanish, but
|
|
654
|
+
// an obsolete generation/controller must never publish it.
|
|
655
|
+
if (!ownsParent()) return;
|
|
656
|
+
if (parentThreadAtStart.retired || parentThreadAtStart.state === "stopped") {
|
|
657
|
+
clearOwnedController();
|
|
658
|
+
removeChainGroup(parentGroupId);
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
runtime.registerRunResult(parentRunId, initialReviewerResult);
|
|
662
|
+
removeChainGroup(parentGroupId);
|
|
663
|
+
monitor.removeRun(parentRunId);
|
|
664
|
+
if (!runtime.sessionActive) {
|
|
665
|
+
clearOwnedController();
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
669
|
+
try {
|
|
670
|
+
ctx.ui.notify(`✗ auto-fix chain dispatch failed: ${errorMessage}`, "error");
|
|
671
|
+
// Keep the triggering review's findings: the chain crashed before any
|
|
672
|
+
// fix round ran, and the main agent needs the review to act on it.
|
|
673
|
+
runtime.sendCompletionGroup([
|
|
674
|
+
{
|
|
675
|
+
agent: initialReviewerResult.agent,
|
|
676
|
+
block: `${formatCompletionBlock(initialReviewerResult, config.maxResultLines, executionCwd)}\n\nAuto-fix chain crashed before completion: ${errorMessage}. The planned fix rounds did not run; the review above is the triggering reviewer's full output.`,
|
|
677
|
+
triggerTurn: true,
|
|
678
|
+
},
|
|
679
|
+
]);
|
|
680
|
+
runtime.completionBatcher.flush();
|
|
681
|
+
} catch {
|
|
682
|
+
/* a second delivery failure must not throw through the queue */
|
|
683
|
+
} finally {
|
|
684
|
+
clearOwnedController();
|
|
685
|
+
}
|
|
686
|
+
},
|
|
687
|
+
);
|
|
688
|
+
runtime.runControllers.set(parentRunId, fixController);
|
|
689
|
+
parentThreadAtStart.queueController = fixController;
|
|
690
|
+
const priorCompletion = parentThreadAtStart.generationCompletion;
|
|
691
|
+
parentThreadAtStart.generationCompletion = Promise.all([
|
|
692
|
+
priorCompletion,
|
|
693
|
+
runtime.backgroundQueue.waitForTask(fixController),
|
|
694
|
+
]).then(() => undefined);
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
interface SessionSeed {
|
|
698
|
+
sessionId?: string;
|
|
699
|
+
sessionDir?: string;
|
|
700
|
+
prompt?: string;
|
|
701
|
+
worktree?: WorktreeIsolation;
|
|
702
|
+
forkedFromRunId?: number;
|
|
703
|
+
forkObjective?: string;
|
|
704
|
+
modelPool?: string[];
|
|
705
|
+
thinkingLevel?: SubagentThread["thinkingLevel"];
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
interface ResumeReservation {
|
|
709
|
+
version: number;
|
|
710
|
+
generation: number;
|
|
711
|
+
sessionId?: string;
|
|
712
|
+
sessionDir?: string;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
const ownsResumeReservation = (
|
|
716
|
+
thread: SubagentThread,
|
|
717
|
+
reservation: ResumeReservation,
|
|
718
|
+
): boolean =>
|
|
719
|
+
runtime.sessionActive &&
|
|
720
|
+
runtime.threads.get(thread.id) === thread &&
|
|
721
|
+
!thread.retired &&
|
|
722
|
+
thread.lifecycleOperation === "resume" &&
|
|
723
|
+
thread.lifecycleVersion === reservation.version &&
|
|
724
|
+
thread.generation === reservation.generation &&
|
|
725
|
+
thread.sessionId === reservation.sessionId &&
|
|
726
|
+
thread.sessionDir === reservation.sessionDir;
|
|
727
|
+
|
|
728
|
+
const beginPreflight = (): (() => void) => {
|
|
729
|
+
let resolvePreflight!: () => void;
|
|
730
|
+
const preflight = new Promise<void>((resolve) => {
|
|
731
|
+
resolvePreflight = resolve;
|
|
732
|
+
});
|
|
733
|
+
runtime.preflightOperations.add(preflight);
|
|
734
|
+
return () => {
|
|
735
|
+
runtime.preflightOperations.delete(preflight);
|
|
736
|
+
resolvePreflight();
|
|
737
|
+
};
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
const startBackground = async (
|
|
741
|
+
agentName: string,
|
|
742
|
+
task: string,
|
|
743
|
+
cwd: string | undefined,
|
|
744
|
+
vision = false,
|
|
745
|
+
isolation: IsolationMode = "shared",
|
|
746
|
+
existingThread?: SubagentThread,
|
|
747
|
+
newObjectiveOnResume = false,
|
|
748
|
+
environment?: DispatchEnvironment,
|
|
749
|
+
seed?: SessionSeed,
|
|
750
|
+
resumeReservation?: ResumeReservation,
|
|
751
|
+
): Promise<SingleResult> => {
|
|
752
|
+
if (!runtime.sessionActive) {
|
|
753
|
+
return failedStartResult(agentName, task, "Parent session shut down before this subagent generation could start.");
|
|
754
|
+
}
|
|
755
|
+
if (existingThread && (!resumeReservation || !ownsResumeReservation(existingThread, resumeReservation))) {
|
|
756
|
+
return failedStartResult(agentName, task, `Run #${existingThread.id} changed while resume was preparing; no new generation was started.`);
|
|
757
|
+
}
|
|
758
|
+
const runCtx = environment?.ctx ?? ctx;
|
|
759
|
+
const runConfig = environment?.config ?? config;
|
|
760
|
+
const runAgents = environment?.agents ?? agents;
|
|
761
|
+
const runSessionRef = environment?.sessionRef ?? sessionRef;
|
|
762
|
+
const agent = runAgents.find((candidate) => candidate.name === agentName);
|
|
763
|
+
if (!agent) return failedStartResult(agentName, task, `Unknown agent: "${agentName}".`);
|
|
764
|
+
if (isolation === "worktree" && !isWorktreeCapableAgent(agent)) {
|
|
765
|
+
return {
|
|
766
|
+
...failedStartResult(agentName, task, `Agent "${agentName}" is read-only; worktree isolation is available only to worker/write-capable agents.`),
|
|
767
|
+
isolation,
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
const originalCwd = resolve(cwd ?? runCtx.cwd);
|
|
772
|
+
const previousWorktree = existingThread?.worktree;
|
|
773
|
+
let worktree = seed?.worktree ?? previousWorktree;
|
|
774
|
+
if (isolation === "worktree") {
|
|
775
|
+
if (worktree && worktree.state !== "active") {
|
|
776
|
+
return {
|
|
777
|
+
...failedStartResult(agentName, task, `Run #${existingThread?.id ?? "?"} has no active continuation worktree.`),
|
|
778
|
+
isolation,
|
|
779
|
+
originalCwd,
|
|
780
|
+
integrationStatus: worktree.state === "finalizing" ? "pending" : worktree.state,
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
if (!worktree) {
|
|
784
|
+
try {
|
|
785
|
+
worktree = await createWorktreeIsolation(originalCwd);
|
|
786
|
+
} catch (error) {
|
|
787
|
+
return {
|
|
788
|
+
...failedStartResult(agentName, task, error instanceof Error ? error.message : String(error)),
|
|
789
|
+
isolation,
|
|
790
|
+
originalCwd,
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
const executionCwd = worktree?.cwd ?? originalCwd;
|
|
796
|
+
const resolvedPool = resolveDispatchModelPool(agent, runConfig, runSessionRef, vision);
|
|
797
|
+
const inheritedPool = seed?.modelPool?.filter((ref) => ref.trim().length > 0) ?? [];
|
|
798
|
+
const rawPool = inheritedPool.length > 0
|
|
799
|
+
? {
|
|
800
|
+
agent: { ...agent, model: inheritedPool[0] },
|
|
801
|
+
fallbackModelRefs: inheritedPool.slice(1),
|
|
802
|
+
}
|
|
803
|
+
: resolvedPool;
|
|
804
|
+
// Isolation is a persistent system-level invariant, not a one-shot task
|
|
805
|
+
// prefix: queued retargets, live retargets, resumes, and model fallbacks
|
|
806
|
+
// all keep the same worktree boundary.
|
|
807
|
+
const pool = isolation === "worktree"
|
|
808
|
+
? { ...rawPool, agent: withWorktreeSystemPrompt(rawPool.agent) }
|
|
809
|
+
: rawPool;
|
|
810
|
+
const thinkingLevel = seed?.thinkingLevel ?? runConfig.agentThinkingLevels[agent.name] ?? agent.thinking ?? runConfig.thinkingLevel;
|
|
811
|
+
const modelPool = [pool.agent.model, ...pool.fallbackModelRefs].filter((ref): ref is string => Boolean(ref));
|
|
812
|
+
const priorTask = existingThread?.task;
|
|
813
|
+
const priorSessionId = seed?.sessionId ?? existingThread?.sessionId;
|
|
814
|
+
const priorSessionDir = seed?.sessionDir ?? existingThread?.sessionDir;
|
|
815
|
+
if (existingThread && resumeReservation && !ownsResumeReservation(existingThread, resumeReservation)) {
|
|
816
|
+
return failedStartResult(agentName, task, `Run #${existingThread.id} changed while resume was preparing; no new generation was started.`);
|
|
817
|
+
}
|
|
818
|
+
const runId = existingThread?.id ?? monitor.addRun(agent.name, task, pool.agent.model, thinkingLevel, {
|
|
819
|
+
isolation,
|
|
820
|
+
...(seed?.forkedFromRunId !== undefined ? { forkedFromRunId: seed.forkedFromRunId } : {}),
|
|
821
|
+
});
|
|
822
|
+
const generation = (existingThread?.generation ?? 0) + 1;
|
|
823
|
+
const pending: SingleResult = {
|
|
824
|
+
...queuedResult(pool.agent, task, thinkingLevel),
|
|
825
|
+
runId,
|
|
826
|
+
isolation,
|
|
827
|
+
originalCwd,
|
|
828
|
+
isolationCwd: executionCwd,
|
|
829
|
+
...(isolation === "worktree" ? { integrationStatus: "pending" as const } : {}),
|
|
830
|
+
...(seed?.sessionId && seed.sessionDir
|
|
831
|
+
? { sessionId: seed.sessionId, sessionDir: seed.sessionDir, resumed: true }
|
|
832
|
+
: {}),
|
|
833
|
+
...(seed?.forkedFromRunId !== undefined ? { forkedFromRunId: seed.forkedFromRunId } : {}),
|
|
834
|
+
};
|
|
835
|
+
if (existingThread) {
|
|
836
|
+
monitor.restartRun(runId, agent.name, task, pool.agent.model, thinkingLevel, isolation);
|
|
837
|
+
runtime.settledRuns.delete(runId);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
let thread!: SubagentThread;
|
|
841
|
+
const control = new RpcRunControl(task, generation, (phase) => {
|
|
842
|
+
if (runtime.threads.get(runId)?.generation !== generation || phase === "settled") return;
|
|
843
|
+
// Orchestration transitions are part of the trajectory (retrying →
|
|
844
|
+
// retry event, park/stop → terminal control events).
|
|
845
|
+
const trajectory = trajectoryStore.get(runId).trajectory;
|
|
846
|
+
if (phase === "retrying") trajectory.append({ kind: "retry", reason: "retrying" });
|
|
847
|
+
else if (phase === "parked") trajectory.append({ kind: "park" });
|
|
848
|
+
else if (phase === "stopped") trajectory.append({ kind: "stop", reason: control.getStopMessage() });
|
|
849
|
+
const state: ThreadState =
|
|
850
|
+
phase === "queued" || phase === "starting"
|
|
851
|
+
? "queued"
|
|
852
|
+
: phase === "steering"
|
|
853
|
+
? "steering"
|
|
854
|
+
: phase === "interrupting"
|
|
855
|
+
? "interrupting"
|
|
856
|
+
: phase === "parked"
|
|
857
|
+
? "parked"
|
|
858
|
+
: phase === "stopped"
|
|
859
|
+
? "stopped"
|
|
860
|
+
: "running";
|
|
861
|
+
thread.state = state;
|
|
862
|
+
if (state === "queued") monitor.setStatus(runId, "queued");
|
|
863
|
+
else if (state === "steering") monitor.setStatus(runId, "steering");
|
|
864
|
+
else if (state === "interrupting") monitor.setStatus(runId, "interrupting");
|
|
865
|
+
else if (state === "parked") monitor.setStatus(runId, "parked");
|
|
866
|
+
else if (state === "running") monitor.setStatus(runId, "running");
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
// Restart bumps the generation while preserving append-only history.
|
|
870
|
+
const trajectoryState = trajectoryStore.get(runId);
|
|
871
|
+
if (existingThread) {
|
|
872
|
+
trajectoryState.trajectory.restart();
|
|
873
|
+
trajectoryState.trajectory.append({
|
|
874
|
+
kind: "resume",
|
|
875
|
+
objective: newObjectiveOnResume ? task : undefined,
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
if (seed?.forkedFromRunId !== undefined) {
|
|
879
|
+
trajectoryState.trajectory.append({
|
|
880
|
+
kind: "fork",
|
|
881
|
+
sourceRunId: seed.forkedFromRunId,
|
|
882
|
+
childRunId: runId,
|
|
883
|
+
objective: seed.forkObjective,
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
trajectoryState.trajectory.append({
|
|
887
|
+
kind: "dispatch",
|
|
888
|
+
agent: agent.name,
|
|
889
|
+
task,
|
|
890
|
+
model: pool.agent.model,
|
|
891
|
+
thinking: thinkingLevel,
|
|
892
|
+
pool: pool.fallbackModelRefs,
|
|
893
|
+
vision,
|
|
894
|
+
resumed: existingThread !== undefined || seed !== undefined,
|
|
895
|
+
isolation,
|
|
896
|
+
originalCwd,
|
|
897
|
+
isolationCwd: executionCwd,
|
|
898
|
+
});
|
|
899
|
+
if (worktree && worktree !== previousWorktree) {
|
|
900
|
+
trajectoryState.trajectory.append({
|
|
901
|
+
kind: "worktree",
|
|
902
|
+
status: "created",
|
|
903
|
+
originalCwd,
|
|
904
|
+
isolationCwd: executionCwd,
|
|
905
|
+
worktreePath: worktree.worktreePath,
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
if (existingThread) {
|
|
910
|
+
thread = existingThread;
|
|
911
|
+
thread.generation = generation;
|
|
912
|
+
thread.agentName = agent.name;
|
|
913
|
+
thread.task = task;
|
|
914
|
+
thread.cwd = originalCwd;
|
|
915
|
+
thread.executionCwd = executionCwd;
|
|
916
|
+
thread.vision = vision;
|
|
917
|
+
thread.modelPool = modelPool;
|
|
918
|
+
thread.thinkingLevel = thinkingLevel;
|
|
919
|
+
thread.isolation = isolation;
|
|
920
|
+
thread.worktree = worktree;
|
|
921
|
+
thread.state = "queued";
|
|
922
|
+
thread.control = control;
|
|
923
|
+
// A newly admitted generation owns no output yet. Keeping the prior
|
|
924
|
+
// generation here would make a queued stop publish stale task,
|
|
925
|
+
// session metadata as this generation's partial.
|
|
926
|
+
thread.lastResult = undefined;
|
|
927
|
+
if (seed?.sessionId && seed.sessionDir) {
|
|
928
|
+
thread.sessionId = seed.sessionId;
|
|
929
|
+
thread.sessionDir = seed.sessionDir;
|
|
930
|
+
}
|
|
931
|
+
thread.retireOnSettle = false;
|
|
932
|
+
thread.isolationFailureNotified = false;
|
|
933
|
+
} else {
|
|
934
|
+
thread = {
|
|
935
|
+
id: runId,
|
|
936
|
+
generation,
|
|
937
|
+
agentName: agent.name,
|
|
938
|
+
task,
|
|
939
|
+
cwd: originalCwd,
|
|
940
|
+
executionCwd,
|
|
941
|
+
vision,
|
|
942
|
+
modelPool,
|
|
943
|
+
thinkingLevel,
|
|
944
|
+
isolation,
|
|
945
|
+
worktree,
|
|
946
|
+
state: "queued",
|
|
947
|
+
control,
|
|
948
|
+
generationCompletion: Promise.resolve(),
|
|
949
|
+
lifecycleVersion: 0,
|
|
950
|
+
sessionId: seed?.sessionId,
|
|
951
|
+
sessionDir: seed?.sessionDir,
|
|
952
|
+
forkedFromRunId: seed?.forkedFromRunId,
|
|
953
|
+
forkChildRunIds: [],
|
|
954
|
+
park: async () => {
|
|
955
|
+
throw new Error("Thread park was not initialized.");
|
|
956
|
+
},
|
|
957
|
+
resume: async () => failedStartResult(agent.name, task, "Thread resume was not initialized."),
|
|
958
|
+
fork: async () => failedStartResult(agent.name, task, "Thread fork was not initialized."),
|
|
959
|
+
finalizeIsolation: async () => undefined,
|
|
960
|
+
};
|
|
961
|
+
runtime.threads.set(runId, thread);
|
|
962
|
+
}
|
|
963
|
+
thread.notifyIsolationFailure = (finalization) => {
|
|
964
|
+
const paths = [finalization.worktreePath, finalization.patchPath].filter(Boolean).join(" · ");
|
|
965
|
+
runCtx.ui.notify(
|
|
966
|
+
`✗ worker worktree ${finalization.integrated ? "cleanup" : "integration"} failed${paths ? ` · retained ${paths}` : ""}: ${finalization.error ?? "unknown Git integration error"}`,
|
|
967
|
+
"error",
|
|
968
|
+
);
|
|
969
|
+
};
|
|
970
|
+
thread.finalizeIsolation = async (
|
|
971
|
+
expectedGeneration: number,
|
|
972
|
+
result?: SingleResult,
|
|
973
|
+
): Promise<WorktreeFinalization | undefined> => {
|
|
974
|
+
if (thread.isolation !== "worktree" || !thread.worktree) return undefined;
|
|
975
|
+
if (thread.generation !== expectedGeneration) return undefined;
|
|
976
|
+
const finalization = await thread.worktree.finalize();
|
|
977
|
+
monitor.setIsolation(runId, "worktree", finalization.status);
|
|
978
|
+
trajectoryState.trajectory.append({
|
|
979
|
+
kind: "worktree",
|
|
980
|
+
status: finalization.status,
|
|
981
|
+
originalCwd: thread.cwd,
|
|
982
|
+
isolationCwd: thread.executionCwd,
|
|
983
|
+
worktreePath: finalization.worktreePath,
|
|
984
|
+
patchPath: finalization.patchPath,
|
|
985
|
+
integrated: finalization.integrated,
|
|
986
|
+
error: finalization.error,
|
|
987
|
+
});
|
|
988
|
+
if (result) {
|
|
989
|
+
result.runId = runId;
|
|
990
|
+
result.isolation = "worktree";
|
|
991
|
+
result.originalCwd = thread.cwd;
|
|
992
|
+
result.isolationCwd = thread.executionCwd;
|
|
993
|
+
result.integrationStatus = finalization.status;
|
|
994
|
+
result.integrationApplied = finalization.integrated;
|
|
995
|
+
result.integrationError = finalization.error;
|
|
996
|
+
result.integrationWorktreePath = finalization.worktreePath;
|
|
997
|
+
result.integrationPatchPath = finalization.patchPath;
|
|
998
|
+
result.forkedFromRunId = thread.forkedFromRunId;
|
|
999
|
+
result.forkChildRunIds = [...thread.forkChildRunIds];
|
|
1000
|
+
if (finalization.status === "retained") {
|
|
1001
|
+
const retained = [
|
|
1002
|
+
finalization.worktreePath ? `worktree ${finalization.worktreePath}` : undefined,
|
|
1003
|
+
finalization.patchPath ? `patch ${finalization.patchPath}` : undefined,
|
|
1004
|
+
].filter(Boolean).join(", ");
|
|
1005
|
+
const integrationMessage = finalization.integrated
|
|
1006
|
+
? `Worktree changes were applied, but cleanup failed${retained ? `; retained ${retained}` : ""}: ${finalization.error ?? "unknown Git cleanup error"}`
|
|
1007
|
+
: `Worktree integration failed${retained ? `; retained ${retained}` : ""}: ${finalization.error ?? "unknown Git integration error"}`;
|
|
1008
|
+
result.exitCode = 1;
|
|
1009
|
+
result.stopReason = "error";
|
|
1010
|
+
result.errorMessage = result.errorMessage
|
|
1011
|
+
? `${result.errorMessage}\n${integrationMessage}`
|
|
1012
|
+
: integrationMessage;
|
|
1013
|
+
result.stderr = result.stderr ? `${result.stderr.trimEnd()}\n${integrationMessage}` : integrationMessage;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
if (finalization.status === "retained") {
|
|
1017
|
+
runtime.retainWorktreeArtifacts(finalization);
|
|
1018
|
+
if (!thread.isolationFailureNotified) {
|
|
1019
|
+
thread.isolationFailureNotified = true;
|
|
1020
|
+
try {
|
|
1021
|
+
thread.notifyIsolationFailure?.(finalization);
|
|
1022
|
+
} catch {
|
|
1023
|
+
/* notification failures do not hide retained artifacts */
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
return finalization;
|
|
1028
|
+
};
|
|
1029
|
+
|
|
1030
|
+
const cleanupTrackedSessionDir = async (sessionDir: string, action: string): Promise<void> => {
|
|
1031
|
+
try {
|
|
1032
|
+
await rm(sessionDir, { recursive: true, force: true });
|
|
1033
|
+
runtime.sessionDirs.delete(sessionDir);
|
|
1034
|
+
} catch (error) {
|
|
1035
|
+
// Keep ownership so shutdown can retry; losing the path here leaks a
|
|
1036
|
+
// cloned session containing retained model context on Windows locks.
|
|
1037
|
+
try {
|
|
1038
|
+
runCtx.ui.notify(
|
|
1039
|
+
`✗ ${action}; retained ${sessionDir} for shutdown cleanup: ${error instanceof Error ? error.message : String(error)}`,
|
|
1040
|
+
"error",
|
|
1041
|
+
);
|
|
1042
|
+
} catch {
|
|
1043
|
+
/* cleanup ownership remains tracked even if the UI is unavailable */
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
};
|
|
1047
|
+
|
|
1048
|
+
const discardUnusedWorktree = async (candidate: WorktreeIsolation | undefined): Promise<void> => {
|
|
1049
|
+
if (!candidate) return;
|
|
1050
|
+
try {
|
|
1051
|
+
if (candidate.discard) {
|
|
1052
|
+
await candidate.discard();
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
// Compatibility for externally supplied/test handles. Production handles
|
|
1056
|
+
// expose discard(), so this fallback never integrates a seeded worktree.
|
|
1057
|
+
if (candidate.state === "active") await candidate.finalize();
|
|
1058
|
+
} catch (error) {
|
|
1059
|
+
const retainedPath = existsSync(candidate.worktreePath)
|
|
1060
|
+
? candidate.worktreePath
|
|
1061
|
+
: existsSync(candidate.tempDir)
|
|
1062
|
+
? candidate.tempDir
|
|
1063
|
+
: undefined;
|
|
1064
|
+
const finalization: WorktreeFinalization = {
|
|
1065
|
+
status: "retained",
|
|
1066
|
+
integrated: false,
|
|
1067
|
+
hadChanges: false,
|
|
1068
|
+
...(retainedPath ? { worktreePath: retainedPath } : {}),
|
|
1069
|
+
...(existsSync(candidate.patchPath) ? { patchPath: candidate.patchPath } : {}),
|
|
1070
|
+
error: `Discarding unused continuation failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1071
|
+
};
|
|
1072
|
+
runtime.retainWorktreeArtifacts(finalization);
|
|
1073
|
+
await persistRecoveryRecords(runtime.configPath, [
|
|
1074
|
+
recoveryRecordFromFinalization(runId, finalization),
|
|
1075
|
+
]).catch(() => undefined);
|
|
1076
|
+
try {
|
|
1077
|
+
thread.notifyIsolationFailure?.(finalization);
|
|
1078
|
+
} catch {
|
|
1079
|
+
/* parent UI may already be shutting down */
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
const createContinuationWorktree = async (
|
|
1085
|
+
source: WorktreeIsolation,
|
|
1086
|
+
seedIsIntegrated: boolean,
|
|
1087
|
+
): Promise<WorktreeIsolation> => {
|
|
1088
|
+
if (source.state === "finalizing") {
|
|
1089
|
+
throw new Error(`Run #${runId}'s worktree is still finalizing.`);
|
|
1090
|
+
}
|
|
1091
|
+
const seedCheckpoint = await source.snapshotCheckpoint();
|
|
1092
|
+
return createWorktreeIsolation(thread.cwd, {
|
|
1093
|
+
seedCheckpoint,
|
|
1094
|
+
seedIsIntegrated,
|
|
1095
|
+
});
|
|
1096
|
+
};
|
|
1097
|
+
|
|
1098
|
+
thread.park = async (): Promise<"queued" | "active"> => {
|
|
1099
|
+
if (thread.retired) throw new Error(`Run #${runId} was retired by subagent_stop.`);
|
|
1100
|
+
if (thread.lifecycleOperation) throw new Error(`Run #${runId} is already handling ${thread.lifecycleOperation}.`);
|
|
1101
|
+
if (thread.state === "parked") return "active";
|
|
1102
|
+
const phase = thread.control.getPhase();
|
|
1103
|
+
const queued = thread.state === "queued" && phase === "queued";
|
|
1104
|
+
if (
|
|
1105
|
+
!queued &&
|
|
1106
|
+
((phase === "settled" && thread.state !== "running") ||
|
|
1107
|
+
!["starting", "running", "steering", "interrupting", "retrying", "settled"].includes(phase))
|
|
1108
|
+
) {
|
|
1109
|
+
throw new Error(`Run #${runId} is ${thread.state}; only active work can be parked.`);
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
const version = ++thread.lifecycleVersion;
|
|
1113
|
+
const generation = thread.generation;
|
|
1114
|
+
const completion = thread.generationCompletion;
|
|
1115
|
+
const controller = thread.queueController;
|
|
1116
|
+
thread.lifecycleOperation = "park";
|
|
1117
|
+
try {
|
|
1118
|
+
if (queued) {
|
|
1119
|
+
thread.control.parkPending();
|
|
1120
|
+
runtime.backgroundQueue.cancel(controller);
|
|
1121
|
+
} else {
|
|
1122
|
+
await thread.control.park();
|
|
1123
|
+
// Auto-fix orchestration has no live RPC attempt once its parent
|
|
1124
|
+
// review settled, so cancel its queue owner explicitly.
|
|
1125
|
+
if (phase === "settled") runtime.backgroundQueue.cancel(controller);
|
|
1126
|
+
}
|
|
1127
|
+
await completion;
|
|
1128
|
+
if (
|
|
1129
|
+
thread.generation !== generation ||
|
|
1130
|
+
thread.lifecycleVersion !== version ||
|
|
1131
|
+
thread.lifecycleOperation !== "park"
|
|
1132
|
+
) {
|
|
1133
|
+
throw new Error(`Run #${runId} changed while parking.`);
|
|
1134
|
+
}
|
|
1135
|
+
thread.state = "parked";
|
|
1136
|
+
thread.queueController = undefined;
|
|
1137
|
+
runtime.runControllers.delete(runId);
|
|
1138
|
+
monitor.setStatus(runId, "parked");
|
|
1139
|
+
return queued ? "queued" : "active";
|
|
1140
|
+
} finally {
|
|
1141
|
+
if (thread.lifecycleVersion === version && thread.lifecycleOperation === "park") {
|
|
1142
|
+
thread.lifecycleOperation = undefined;
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
|
|
1147
|
+
thread.resume = async (objective?: string, resumeCtx?: ExtensionContext): Promise<SingleResult> => {
|
|
1148
|
+
const requestedObjective = objective?.trim();
|
|
1149
|
+
if (!runtime.sessionActive || runtime.threads.get(runId) !== thread) {
|
|
1150
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} belongs to a parent session that has shut down.`);
|
|
1151
|
+
}
|
|
1152
|
+
if (objective !== undefined && !requestedObjective) {
|
|
1153
|
+
return failedStartResult(thread.agentName, thread.task, "resume objective must be non-blank when provided.");
|
|
1154
|
+
}
|
|
1155
|
+
if (thread.retired) return failedStartResult(thread.agentName, thread.task, `Run #${runId} was retired by subagent_stop.`);
|
|
1156
|
+
if (thread.lifecycleOperation) {
|
|
1157
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} is already ${thread.lifecycleOperation === "resume" ? "resuming" : "being forked"}.`);
|
|
1158
|
+
}
|
|
1159
|
+
if (!["parked", "completed", "failed"].includes(thread.state)) {
|
|
1160
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} is ${thread.state}; it must be parked or settled before resume.`);
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
// Lifecycle CAS: claim synchronously before the first await, then cancel
|
|
1164
|
+
// and fully quiesce any superseded queue/process before cloning or
|
|
1165
|
+
// reusing its session. A second resume/fork sees this claim immediately.
|
|
1166
|
+
const previousState = thread.state;
|
|
1167
|
+
const previousSessionId = thread.sessionId;
|
|
1168
|
+
const previousSessionDir = thread.sessionDir;
|
|
1169
|
+
const previousExecutionCwd = thread.executionCwd;
|
|
1170
|
+
const reservation: ResumeReservation = {
|
|
1171
|
+
version: ++thread.lifecycleVersion,
|
|
1172
|
+
generation: thread.generation,
|
|
1173
|
+
sessionId: previousSessionId,
|
|
1174
|
+
sessionDir: previousSessionDir,
|
|
1175
|
+
};
|
|
1176
|
+
thread.lifecycleOperation = "resume";
|
|
1177
|
+
thread.state = "resuming";
|
|
1178
|
+
const finishPreflight = beginPreflight();
|
|
1179
|
+
const supersededController = thread.queueController;
|
|
1180
|
+
runtime.backgroundQueue.cancel(supersededController);
|
|
1181
|
+
runtime.runControllers.delete(runId);
|
|
1182
|
+
|
|
1183
|
+
let continuationWorktree: WorktreeIsolation | undefined;
|
|
1184
|
+
let clonedSession: Awaited<ReturnType<typeof forkRetainedSession>> | undefined;
|
|
1185
|
+
try {
|
|
1186
|
+
await thread.generationCompletion;
|
|
1187
|
+
if (!ownsResumeReservation(thread, reservation)) {
|
|
1188
|
+
return failedStartResult(
|
|
1189
|
+
thread.agentName,
|
|
1190
|
+
thread.task,
|
|
1191
|
+
thread.retired
|
|
1192
|
+
? `Run #${runId} was retired by subagent_stop; no new generation was started.`
|
|
1193
|
+
: `Run #${runId} changed while resume was preparing; no new generation was started.`,
|
|
1194
|
+
);
|
|
1195
|
+
}
|
|
1196
|
+
thread.state = "resuming";
|
|
1197
|
+
const currentCtx = resumeCtx ?? runCtx;
|
|
1198
|
+
let seed: SessionSeed | undefined;
|
|
1199
|
+
if (thread.isolation === "worktree" && thread.worktree?.state !== "active") {
|
|
1200
|
+
if (!thread.worktree) throw new Error(`Run #${runId} has no isolated worktree checkpoint.`);
|
|
1201
|
+
const seedAlreadyIntegrated =
|
|
1202
|
+
thread.worktree.state === "integrated" ||
|
|
1203
|
+
thread.worktree.state === "no_changes" ||
|
|
1204
|
+
thread.lastResult?.integrationApplied === true;
|
|
1205
|
+
continuationWorktree = await createContinuationWorktree(thread.worktree, seedAlreadyIntegrated);
|
|
1206
|
+
if (!ownsResumeReservation(thread, reservation)) {
|
|
1207
|
+
throw new Error(`Run #${runId} changed while its continuation worktree was being created.`);
|
|
1208
|
+
}
|
|
1209
|
+
seed = { worktree: continuationWorktree };
|
|
1210
|
+
if (previousSessionId && previousSessionDir) {
|
|
1211
|
+
clonedSession = await forkRetainedSession({
|
|
1212
|
+
cwd: previousExecutionCwd,
|
|
1213
|
+
targetCwd: continuationWorktree.cwd,
|
|
1214
|
+
sessionDir: previousSessionDir,
|
|
1215
|
+
sessionId: previousSessionId,
|
|
1216
|
+
});
|
|
1217
|
+
runtime.sessionDirs.add(clonedSession.sessionDir);
|
|
1218
|
+
if (!ownsResumeReservation(thread, reservation)) {
|
|
1219
|
+
throw new Error(`Run #${runId} changed while its retained session was being cloned.`);
|
|
1220
|
+
}
|
|
1221
|
+
seed.sessionId = clonedSession.sessionId;
|
|
1222
|
+
seed.sessionDir = clonedSession.sessionDir;
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
const currentConfig = await loadConfig(runtime.configPath);
|
|
1227
|
+
if (!ownsResumeReservation(thread, reservation)) {
|
|
1228
|
+
throw new Error(`Run #${runId} changed while resume configuration was loading.`);
|
|
1229
|
+
}
|
|
1230
|
+
runtime.backgroundQueue.setConcurrency(currentConfig.maxConcurrency);
|
|
1231
|
+
const currentAgents = discoverAgents(currentCtx.cwd, {
|
|
1232
|
+
scope: currentConfig.agentScope,
|
|
1233
|
+
enabledNames: currentConfig.enabledAgents,
|
|
1234
|
+
projectTrusted: currentCtx.isProjectTrusted?.() === true,
|
|
1235
|
+
}).agents;
|
|
1236
|
+
const nextTask = requestedObjective ?? thread.task;
|
|
1237
|
+
const pending = await startBackground(
|
|
1238
|
+
thread.agentName,
|
|
1239
|
+
nextTask,
|
|
1240
|
+
thread.cwd,
|
|
1241
|
+
thread.vision,
|
|
1242
|
+
thread.isolation,
|
|
1243
|
+
thread,
|
|
1244
|
+
objective !== undefined,
|
|
1245
|
+
{
|
|
1246
|
+
ctx: currentCtx,
|
|
1247
|
+
config: currentConfig,
|
|
1248
|
+
agents: currentAgents,
|
|
1249
|
+
sessionRef: currentModelRef(currentCtx),
|
|
1250
|
+
},
|
|
1251
|
+
seed,
|
|
1252
|
+
reservation,
|
|
1253
|
+
);
|
|
1254
|
+
if (pending.exitCode !== -1) {
|
|
1255
|
+
if (clonedSession) {
|
|
1256
|
+
await cleanupTrackedSessionDir(
|
|
1257
|
+
clonedSession.sessionDir,
|
|
1258
|
+
`Could not discard failed resume session clone for run #${runId}`,
|
|
1259
|
+
);
|
|
1260
|
+
}
|
|
1261
|
+
await discardUnusedWorktree(continuationWorktree);
|
|
1262
|
+
if (ownsResumeReservation(thread, reservation)) thread.state = previousState;
|
|
1263
|
+
return pending;
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
// The cloned branch replaces the removed-worktree session for this
|
|
1267
|
+
// logical id. Keep an undeletable old dir in runtime cleanup if needed.
|
|
1268
|
+
if (clonedSession && previousSessionDir && previousSessionDir !== clonedSession.sessionDir) {
|
|
1269
|
+
try {
|
|
1270
|
+
await rm(previousSessionDir, { recursive: true, force: true });
|
|
1271
|
+
runtime.sessionDirs.delete(previousSessionDir);
|
|
1272
|
+
} catch {
|
|
1273
|
+
/* shutdown retries cleanup of the old retained branch */
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
return pending;
|
|
1277
|
+
} catch (error) {
|
|
1278
|
+
if (clonedSession) {
|
|
1279
|
+
await cleanupTrackedSessionDir(
|
|
1280
|
+
clonedSession.sessionDir,
|
|
1281
|
+
`Could not discard interrupted resume session clone for run #${runId}`,
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
await discardUnusedWorktree(continuationWorktree);
|
|
1285
|
+
if (ownsResumeReservation(thread, reservation)) {
|
|
1286
|
+
thread.state = previousState;
|
|
1287
|
+
thread.sessionId = previousSessionId;
|
|
1288
|
+
thread.sessionDir = previousSessionDir;
|
|
1289
|
+
thread.executionCwd = previousExecutionCwd;
|
|
1290
|
+
}
|
|
1291
|
+
return failedStartResult(
|
|
1292
|
+
thread.agentName,
|
|
1293
|
+
requestedObjective ?? thread.task,
|
|
1294
|
+
`Could not resume run #${runId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1295
|
+
);
|
|
1296
|
+
} finally {
|
|
1297
|
+
finishPreflight();
|
|
1298
|
+
if (
|
|
1299
|
+
thread.lifecycleOperation === "resume" &&
|
|
1300
|
+
thread.lifecycleVersion === reservation.version
|
|
1301
|
+
) {
|
|
1302
|
+
thread.lifecycleOperation = undefined;
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
|
|
1307
|
+
thread.fork = async (objective?: string, forkCtx?: ExtensionContext): Promise<SingleResult> => {
|
|
1308
|
+
const forkObjective = objective?.trim();
|
|
1309
|
+
if (!runtime.sessionActive || runtime.threads.get(runId) !== thread) {
|
|
1310
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} belongs to a parent session that has shut down.`);
|
|
1311
|
+
}
|
|
1312
|
+
if (objective !== undefined && !forkObjective) {
|
|
1313
|
+
return failedStartResult(thread.agentName, thread.task, "fork objective must be non-blank when provided.");
|
|
1314
|
+
}
|
|
1315
|
+
if (thread.retired || thread.state === "stopped") {
|
|
1316
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} was retired by subagent_stop and cannot be forked.`);
|
|
1317
|
+
}
|
|
1318
|
+
if (thread.lifecycleOperation) {
|
|
1319
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} is already ${thread.lifecycleOperation === "resume" ? "resuming" : "being forked"}.`);
|
|
1320
|
+
}
|
|
1321
|
+
if (thread.state === "queued" && !thread.sessionId) {
|
|
1322
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} is queued and has no retained session to fork.`);
|
|
1323
|
+
}
|
|
1324
|
+
if (["queued", "running", "steering", "interrupting"].includes(thread.state)) {
|
|
1325
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} is active; park it first with subagent_control { action: "park", id: ${runId} }, then fork the stable session.`);
|
|
1326
|
+
}
|
|
1327
|
+
if (!["parked", "completed", "failed"].includes(thread.state)) {
|
|
1328
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} is ${thread.state} and has no forkable retained checkpoint.`);
|
|
1329
|
+
}
|
|
1330
|
+
if (!thread.sessionId || !thread.sessionDir) {
|
|
1331
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} has no retained session to fork (it may have been parked before starting).`);
|
|
1332
|
+
}
|
|
1333
|
+
if (thread.isolation === "worktree") {
|
|
1334
|
+
const worktreeState = thread.worktree?.state;
|
|
1335
|
+
const seedIntegrated =
|
|
1336
|
+
worktreeState === "integrated" ||
|
|
1337
|
+
worktreeState === "no_changes" ||
|
|
1338
|
+
thread.lastResult?.integrationApplied === true;
|
|
1339
|
+
if (!seedIntegrated) {
|
|
1340
|
+
return failedStartResult(
|
|
1341
|
+
thread.agentName,
|
|
1342
|
+
thread.task,
|
|
1343
|
+
`Run #${runId}'s isolated checkpoint has not been integrated. Resume and settle it before forking so its seed is applied exactly once.`,
|
|
1344
|
+
);
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
// Same lifecycle CAS as resume: a concurrent resume/fork cannot consume
|
|
1349
|
+
// or clone this session while the branch copy is in progress.
|
|
1350
|
+
const forkVersion = ++thread.lifecycleVersion;
|
|
1351
|
+
const forkGeneration = thread.generation;
|
|
1352
|
+
const forkSessionId = thread.sessionId;
|
|
1353
|
+
const forkSessionDir = thread.sessionDir;
|
|
1354
|
+
const ownsFork = (): boolean =>
|
|
1355
|
+
runtime.sessionActive &&
|
|
1356
|
+
runtime.threads.get(runId) === thread &&
|
|
1357
|
+
!thread.retired &&
|
|
1358
|
+
thread.lifecycleOperation === "fork" &&
|
|
1359
|
+
thread.lifecycleVersion === forkVersion &&
|
|
1360
|
+
thread.generation === forkGeneration &&
|
|
1361
|
+
thread.sessionId === forkSessionId &&
|
|
1362
|
+
thread.sessionDir === forkSessionDir;
|
|
1363
|
+
thread.lifecycleOperation = "fork";
|
|
1364
|
+
const finishPreflight = beginPreflight();
|
|
1365
|
+
let childWorktree: WorktreeIsolation | undefined;
|
|
1366
|
+
let forkedSession: Awaited<ReturnType<typeof forkRetainedSession>> | undefined;
|
|
1367
|
+
try {
|
|
1368
|
+
await thread.generationCompletion;
|
|
1369
|
+
if (!ownsFork()) {
|
|
1370
|
+
return failedStartResult(thread.agentName, thread.task, `Run #${runId} changed while fork was preparing; no child was started.`);
|
|
1371
|
+
}
|
|
1372
|
+
const currentCtx = forkCtx ?? runCtx;
|
|
1373
|
+
if (thread.isolation === "worktree") {
|
|
1374
|
+
if (!thread.worktree) throw new Error(`Run #${runId} has no isolated worktree checkpoint.`);
|
|
1375
|
+
const seedAlreadyIntegrated =
|
|
1376
|
+
thread.worktree.state === "integrated" ||
|
|
1377
|
+
thread.worktree.state === "no_changes" ||
|
|
1378
|
+
thread.lastResult?.integrationApplied === true;
|
|
1379
|
+
childWorktree = await createContinuationWorktree(thread.worktree, seedAlreadyIntegrated);
|
|
1380
|
+
if (!ownsFork()) throw new Error(`Run #${runId} changed while its fork worktree was being created.`);
|
|
1381
|
+
}
|
|
1382
|
+
forkedSession = await forkRetainedSession({
|
|
1383
|
+
cwd: thread.executionCwd,
|
|
1384
|
+
targetCwd: childWorktree?.cwd ?? thread.cwd,
|
|
1385
|
+
sessionDir: thread.sessionDir,
|
|
1386
|
+
sessionId: thread.sessionId,
|
|
1387
|
+
});
|
|
1388
|
+
runtime.sessionDirs.add(forkedSession.sessionDir);
|
|
1389
|
+
if (!ownsFork()) throw new Error(`Run #${runId} changed while its retained session was being forked.`);
|
|
1390
|
+
const currentConfig = await loadConfig(runtime.configPath);
|
|
1391
|
+
if (!ownsFork()) throw new Error(`Run #${runId} changed while fork configuration was loading.`);
|
|
1392
|
+
runtime.backgroundQueue.setConcurrency(currentConfig.maxConcurrency);
|
|
1393
|
+
const currentAgents = discoverAgents(currentCtx.cwd, {
|
|
1394
|
+
scope: currentConfig.agentScope,
|
|
1395
|
+
enabledNames: currentConfig.enabledAgents,
|
|
1396
|
+
projectTrusted: currentCtx.isProjectTrusted?.() === true,
|
|
1397
|
+
}).agents;
|
|
1398
|
+
if (!ownsFork()) throw new Error(`Run #${runId} changed while fork was preparing; no child was started.`);
|
|
1399
|
+
const childTask = forkObjective ?? thread.task;
|
|
1400
|
+
const child = await startBackground(
|
|
1401
|
+
thread.agentName,
|
|
1402
|
+
childTask,
|
|
1403
|
+
thread.cwd,
|
|
1404
|
+
thread.vision,
|
|
1405
|
+
thread.isolation,
|
|
1406
|
+
undefined,
|
|
1407
|
+
false,
|
|
1408
|
+
{
|
|
1409
|
+
ctx: currentCtx,
|
|
1410
|
+
config: currentConfig,
|
|
1411
|
+
agents: currentAgents,
|
|
1412
|
+
sessionRef: currentModelRef(currentCtx),
|
|
1413
|
+
},
|
|
1414
|
+
{
|
|
1415
|
+
sessionId: forkedSession.sessionId,
|
|
1416
|
+
sessionDir: forkedSession.sessionDir,
|
|
1417
|
+
prompt: forkObjective ?? FORK_CONTINUATION_PROMPT,
|
|
1418
|
+
worktree: childWorktree,
|
|
1419
|
+
forkedFromRunId: runId,
|
|
1420
|
+
forkObjective,
|
|
1421
|
+
modelPool: [...thread.modelPool],
|
|
1422
|
+
thinkingLevel: thread.thinkingLevel,
|
|
1423
|
+
},
|
|
1424
|
+
);
|
|
1425
|
+
if (child.exitCode !== -1 || child.runId === undefined) {
|
|
1426
|
+
await cleanupTrackedSessionDir(
|
|
1427
|
+
forkedSession.sessionDir,
|
|
1428
|
+
`Could not discard failed fork session clone for run #${runId}`,
|
|
1429
|
+
);
|
|
1430
|
+
await discardUnusedWorktree(childWorktree);
|
|
1431
|
+
return child;
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
// Once the independent child is enqueued it remains valid even if the
|
|
1435
|
+
// source is retired; just skip source-side relationship mutation.
|
|
1436
|
+
if (!ownsFork()) return child;
|
|
1437
|
+
const childRunId = child.runId;
|
|
1438
|
+
if (!thread.forkChildRunIds.includes(childRunId)) thread.forkChildRunIds.push(childRunId);
|
|
1439
|
+
const childThread = runtime.threads.get(childRunId);
|
|
1440
|
+
if (childThread) childThread.forkedFromRunId = runId;
|
|
1441
|
+
monitor.setForkRelation(runId, childRunId);
|
|
1442
|
+
trajectoryStore.get(runId).trajectory.append({
|
|
1443
|
+
kind: "fork",
|
|
1444
|
+
sourceRunId: runId,
|
|
1445
|
+
childRunId,
|
|
1446
|
+
objective: forkObjective,
|
|
1447
|
+
});
|
|
1448
|
+
const sourceResult = runtime.settledRuns.get(runId) ?? thread.lastResult;
|
|
1449
|
+
if (sourceResult) sourceResult.forkChildRunIds = [...thread.forkChildRunIds];
|
|
1450
|
+
return child;
|
|
1451
|
+
} catch (error) {
|
|
1452
|
+
if (forkedSession) {
|
|
1453
|
+
await cleanupTrackedSessionDir(
|
|
1454
|
+
forkedSession.sessionDir,
|
|
1455
|
+
`Could not discard interrupted fork session clone for run #${runId}`,
|
|
1456
|
+
);
|
|
1457
|
+
}
|
|
1458
|
+
await discardUnusedWorktree(childWorktree);
|
|
1459
|
+
return failedStartResult(
|
|
1460
|
+
thread.agentName,
|
|
1461
|
+
forkObjective ?? thread.task,
|
|
1462
|
+
`Could not fork retained session for run #${runId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1463
|
+
);
|
|
1464
|
+
} finally {
|
|
1465
|
+
finishPreflight();
|
|
1466
|
+
if (thread.lifecycleVersion === forkVersion && thread.lifecycleOperation === "fork") {
|
|
1467
|
+
thread.lifecycleOperation = undefined;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
};
|
|
1471
|
+
|
|
1472
|
+
const onLive = makeLiveHandler(runId, runId, generation);
|
|
1473
|
+
const queueController = runtime.backgroundQueue.enqueue(
|
|
1474
|
+
async (backgroundSignal) => {
|
|
1475
|
+
if (runtime.threads.get(runId)?.generation !== generation) return;
|
|
1476
|
+
let result: SingleResult;
|
|
1477
|
+
try {
|
|
1478
|
+
result = await runSingleAgentWithModelFallback(
|
|
1479
|
+
{
|
|
1480
|
+
defaultCwd: executionCwd,
|
|
1481
|
+
agent: pool.agent,
|
|
1482
|
+
agentName,
|
|
1483
|
+
task,
|
|
1484
|
+
cwd: executionCwd,
|
|
1485
|
+
thinkingLevel,
|
|
1486
|
+
signal: backgroundSignal,
|
|
1487
|
+
onLive,
|
|
1488
|
+
control,
|
|
1489
|
+
makeDetails: makeDetails("single", true),
|
|
1490
|
+
idleTimeoutMs: runConfig.idleTimeoutSec * 1000,
|
|
1491
|
+
...(priorSessionId && priorSessionDir
|
|
1492
|
+
? {
|
|
1493
|
+
sessionId: priorSessionId,
|
|
1494
|
+
sessionDir: priorSessionDir,
|
|
1495
|
+
stdinText: seed?.prompt ?? (newObjectiveOnResume
|
|
1496
|
+
? task
|
|
1497
|
+
: buildResumePrompt(priorTask ?? task, buildFallbackResumeReason())),
|
|
1498
|
+
}
|
|
1499
|
+
: {}),
|
|
1500
|
+
},
|
|
1501
|
+
pool.fallbackModelRefs,
|
|
1502
|
+
);
|
|
1503
|
+
} catch (error) {
|
|
1504
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1505
|
+
result = {
|
|
1506
|
+
...pending,
|
|
1507
|
+
task: control.getObjective(),
|
|
1508
|
+
exitCode: 1,
|
|
1509
|
+
stderr: errorMessage,
|
|
1510
|
+
stopReason: backgroundSignal.aborted ? "aborted" : "error",
|
|
1511
|
+
errorMessage,
|
|
1512
|
+
dispatchFailed: true,
|
|
1513
|
+
};
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
// A stale process/generation may finish after a park/resume race. It owns
|
|
1517
|
+
// no monitor mutation, result registration, or completion delivery.
|
|
1518
|
+
if (runtime.threads.get(runId)?.generation !== generation) return;
|
|
1519
|
+
result.runId = runId;
|
|
1520
|
+
result.isolation = isolation;
|
|
1521
|
+
result.originalCwd = originalCwd;
|
|
1522
|
+
result.isolationCwd = executionCwd;
|
|
1523
|
+
result.forkedFromRunId = thread.forkedFromRunId;
|
|
1524
|
+
result.forkChildRunIds = [...thread.forkChildRunIds];
|
|
1525
|
+
thread.queueController = undefined;
|
|
1526
|
+
runtime.runControllers.delete(runId);
|
|
1527
|
+
thread.task = result.task;
|
|
1528
|
+
thread.sessionId = result.sessionId;
|
|
1529
|
+
thread.sessionDir = result.sessionDir;
|
|
1530
|
+
thread.lastResult = result;
|
|
1531
|
+
runtime.retainSession(result);
|
|
1532
|
+
monitor.setModel(runId, result.model, result.modelFallbackFrom);
|
|
1533
|
+
|
|
1534
|
+
// Destructive stop owns publication once it has synchronously claimed
|
|
1535
|
+
// the lifecycle. Leave the partial result/session on the thread; the
|
|
1536
|
+
// stop path waits for this queue task, finalizes isolation, and emits
|
|
1537
|
+
// exactly one aborted result.
|
|
1538
|
+
if (thread.lifecycleOperation === "stop") return;
|
|
1539
|
+
|
|
1540
|
+
if (result.parked) {
|
|
1541
|
+
thread.state = "parked";
|
|
1542
|
+
monitor.setStatus(runId, "parked");
|
|
1543
|
+
runtime.settledRuns.delete(runId);
|
|
1544
|
+
return;
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
if (thread.retireOnSettle) runtime.retireThreadSession(thread);
|
|
1548
|
+
const wantsFixLoop = shouldTriggerFixLoop(result, runConfig);
|
|
1549
|
+
if (wantsFixLoop && isolation === "shared" && runtime.sessionActive) {
|
|
1550
|
+
thread.state = "running";
|
|
1551
|
+
finishRun(runId, "done", { silent: true, retain: true });
|
|
1552
|
+
monitor.setAnnotation(runId, "auto-fix chain running");
|
|
1553
|
+
startFixLoop(result, `fix-${runId}`, runId, thread.executionCwd, vision);
|
|
1554
|
+
return;
|
|
1555
|
+
}
|
|
1556
|
+
// Claim terminal settlement synchronously before the first slow await.
|
|
1557
|
+
// Park therefore either wins while RPC is still active, or is rejected
|
|
1558
|
+
// once settlement owns the generation. Destructive stop may supersede
|
|
1559
|
+
// this reservation; publication is revalidated after Git finalization.
|
|
1560
|
+
const settlementVersion = ++thread.lifecycleVersion;
|
|
1561
|
+
thread.lifecycleOperation = "settle";
|
|
1562
|
+
const ownsSettlement = (): boolean =>
|
|
1563
|
+
runtime.threads.get(runId) === thread &&
|
|
1564
|
+
thread.generation === generation &&
|
|
1565
|
+
thread.lifecycleVersion === settlementVersion &&
|
|
1566
|
+
thread.lifecycleOperation === "settle" &&
|
|
1567
|
+
!thread.retired;
|
|
1568
|
+
try {
|
|
1569
|
+
// Worktree isolation is rejected for reviewers, the only role that can
|
|
1570
|
+
// trigger auto-fix. Keep that invariant explicit: an isolated result is
|
|
1571
|
+
// finalized once here and can never start a chain that would integrate
|
|
1572
|
+
// the same worktree early.
|
|
1573
|
+
await thread.finalizeIsolation(generation, result);
|
|
1574
|
+
if (!ownsSettlement()) return;
|
|
1575
|
+
|
|
1576
|
+
const failed = isFailedResult(result);
|
|
1577
|
+
thread.state = failed ? "failed" : "completed";
|
|
1578
|
+
// Stamp the terminal monitor state before projecting it. This gives every
|
|
1579
|
+
// path a fixed endedAt even when the row is removed immediately.
|
|
1580
|
+
monitor.setStatus(runId, failed ? "failed" : "done");
|
|
1581
|
+
trajectoryState.trajectory.append({
|
|
1582
|
+
kind: "settled",
|
|
1583
|
+
status: failed ? "failed" : "done",
|
|
1584
|
+
model: result.model,
|
|
1585
|
+
isolation,
|
|
1586
|
+
...(result.integrationStatus && result.integrationStatus !== "pending"
|
|
1587
|
+
? { integrationStatus: result.integrationStatus }
|
|
1588
|
+
: {}),
|
|
1589
|
+
});
|
|
1590
|
+
if (!runtime.sessionActive || !ownsSettlement()) return;
|
|
1591
|
+
|
|
1592
|
+
const modelLevel = failed && isModelLevelFailure(result);
|
|
1593
|
+
const dispatchFailed = result.dispatchFailed === true;
|
|
1594
|
+
finishRun(runId, failed ? "failed" : "done", modelLevel || dispatchFailed ? { silent: true } : undefined);
|
|
1595
|
+
runtime.registerRunResult(runId, result);
|
|
1596
|
+
const completion: CompletionMessageItem = {
|
|
1597
|
+
agent: result.agent,
|
|
1598
|
+
block: modelLevel
|
|
1599
|
+
? `${formatCompletionBlock(result, runConfig.maxResultLines, runCtx.cwd)}\n\n${modelLevelTakeoverNote(result, { runId })}`
|
|
1600
|
+
: formatCompletionBlock(result, runConfig.maxResultLines, runCtx.cwd),
|
|
1601
|
+
triggerTurn: completionTriggersTurn(result, runConfig.notifyOnReviewPass),
|
|
1602
|
+
};
|
|
1603
|
+
if (modelLevel) {
|
|
1604
|
+
runCtx.ui.notify(`✗ ${result.agent} dispatch failed: model unavailable or broken — task handed to the main window`, "error");
|
|
1605
|
+
} else if (dispatchFailed) {
|
|
1606
|
+
runCtx.ui.notify(`✗ ${result.agent} dispatch failed: ${result.errorMessage ?? "dispatch crashed"}`, "error");
|
|
1607
|
+
}
|
|
1608
|
+
if (failed) {
|
|
1609
|
+
runtime.sendCompletionGroup([completion]);
|
|
1610
|
+
runtime.completionBatcher.flush();
|
|
1611
|
+
} else {
|
|
1612
|
+
runtime.completionBatcher.push(completion);
|
|
1613
|
+
}
|
|
1614
|
+
} finally {
|
|
1615
|
+
if (ownsSettlement()) thread.lifecycleOperation = undefined;
|
|
1616
|
+
}
|
|
1617
|
+
},
|
|
1618
|
+
() => {
|
|
1619
|
+
if (runtime.threads.get(runId)?.generation !== generation) return;
|
|
1620
|
+
// Queued park/stop owns publication and may still be finalizing an
|
|
1621
|
+
// isolated worktree. Do not expose a terminal monitor/trajectory state
|
|
1622
|
+
// before that owner records the checkpoint or aborted result.
|
|
1623
|
+
if (thread.lifecycleOperation === "park" || thread.lifecycleOperation === "stop") return;
|
|
1624
|
+
runtime.runControllers.delete(runId);
|
|
1625
|
+
thread.queueController = undefined;
|
|
1626
|
+
if (thread.state === "parked") {
|
|
1627
|
+
monitor.setStatus(runId, "parked");
|
|
1628
|
+
return;
|
|
1629
|
+
}
|
|
1630
|
+
thread.state = "stopped";
|
|
1631
|
+
monitor.setStatus(runId, "failed");
|
|
1632
|
+
trajectoryState.trajectory.append({ kind: "settled", status: "stopped", model: monitor.findRun(runId)?.model, isolation });
|
|
1633
|
+
if (!runtime.sessionActive) {
|
|
1634
|
+
monitor.removeRun(runId);
|
|
1635
|
+
return;
|
|
1636
|
+
}
|
|
1637
|
+
finishRun(runId, "failed");
|
|
1638
|
+
},
|
|
1639
|
+
async (error) => {
|
|
1640
|
+
if (runtime.threads.get(runId)?.generation !== generation) return;
|
|
1641
|
+
// Queue-level crashes use the same settlement reservation as ordinary
|
|
1642
|
+
// results. A concurrent destructive stop may supersede it while slow
|
|
1643
|
+
// worktree finalization is running, in which case stop publishes once.
|
|
1644
|
+
if (thread.lifecycleOperation === "stop") return;
|
|
1645
|
+
const settlementVersion = ++thread.lifecycleVersion;
|
|
1646
|
+
thread.lifecycleOperation = "settle";
|
|
1647
|
+
const ownsSettlement = (): boolean =>
|
|
1648
|
+
runtime.threads.get(runId) === thread &&
|
|
1649
|
+
thread.generation === generation &&
|
|
1650
|
+
thread.lifecycleVersion === settlementVersion &&
|
|
1651
|
+
thread.lifecycleOperation === "settle" &&
|
|
1652
|
+
!thread.retired;
|
|
1653
|
+
try {
|
|
1654
|
+
const crashed: SingleResult = {
|
|
1655
|
+
...dispatchFailedResult(pool.agent, control.getObjective(), error, thinkingLevel),
|
|
1656
|
+
runId,
|
|
1657
|
+
isolation,
|
|
1658
|
+
originalCwd,
|
|
1659
|
+
isolationCwd: executionCwd,
|
|
1660
|
+
forkedFromRunId: thread.forkedFromRunId,
|
|
1661
|
+
};
|
|
1662
|
+
await thread.finalizeIsolation(generation, crashed);
|
|
1663
|
+
if (!ownsSettlement()) return;
|
|
1664
|
+
thread.state = "failed";
|
|
1665
|
+
monitor.setStatus(runId, "failed");
|
|
1666
|
+
trajectoryState.trajectory.append({
|
|
1667
|
+
kind: "settled",
|
|
1668
|
+
status: "failed",
|
|
1669
|
+
model: crashed.model,
|
|
1670
|
+
isolation,
|
|
1671
|
+
...(crashed.integrationStatus && crashed.integrationStatus !== "pending"
|
|
1672
|
+
? { integrationStatus: crashed.integrationStatus }
|
|
1673
|
+
: {}),
|
|
1674
|
+
});
|
|
1675
|
+
finishRun(runId, "failed", { silent: true });
|
|
1676
|
+
runtime.registerRunResult(runId, crashed);
|
|
1677
|
+
runtime.runControllers.delete(runId);
|
|
1678
|
+
thread.queueController = undefined;
|
|
1679
|
+
if (!runtime.sessionActive || !ownsSettlement()) return;
|
|
1680
|
+
try {
|
|
1681
|
+
runCtx.ui.notify(`✗ ${agent.name} dispatch failed: ${crashed.errorMessage}`, "error");
|
|
1682
|
+
runtime.sendCompletionGroup([
|
|
1683
|
+
{
|
|
1684
|
+
agent: agent.name,
|
|
1685
|
+
block: formatCompletionBlock(crashed, runConfig.maxResultLines, runCtx.cwd),
|
|
1686
|
+
triggerTurn: true,
|
|
1687
|
+
},
|
|
1688
|
+
]);
|
|
1689
|
+
runtime.completionBatcher.flush();
|
|
1690
|
+
} catch {
|
|
1691
|
+
/* a second delivery failure must not throw through the queue */
|
|
1692
|
+
}
|
|
1693
|
+
} finally {
|
|
1694
|
+
if (ownsSettlement()) thread.lifecycleOperation = undefined;
|
|
1695
|
+
}
|
|
1696
|
+
},
|
|
1697
|
+
);
|
|
1698
|
+
thread.queueController = queueController;
|
|
1699
|
+
thread.generationCompletion = runtime.backgroundQueue.waitForTask(queueController);
|
|
1700
|
+
runtime.runControllers.set(runId, queueController);
|
|
1701
|
+
return pending;
|
|
1702
|
+
};
|
|
1703
|
+
|
|
1704
|
+
// Sub-agents intentionally detach from the foreground turn. This makes the
|
|
1705
|
+
// editor available immediately; completion messages later wake the main agent.
|
|
1706
|
+
if (params.tasks && params.tasks.length > 0) {
|
|
1707
|
+
if (params.tasks.length > config.maxConcurrency) {
|
|
1708
|
+
return {
|
|
1709
|
+
content: [
|
|
1710
|
+
{
|
|
1711
|
+
type: "text",
|
|
1712
|
+
text: `Too many parallel tasks (${params.tasks.length}). Max is ${config.maxConcurrency} (configurable via /subagents-setup).`,
|
|
1713
|
+
},
|
|
1714
|
+
],
|
|
1715
|
+
details: makeDetails("parallel", true)([]),
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
const results: SingleResult[] = [];
|
|
1720
|
+
// Preserve caller order (and deterministic completion batching) while
|
|
1721
|
+
// preparing each isolated filesystem before its queue entry can start.
|
|
1722
|
+
for (const item of params.tasks) {
|
|
1723
|
+
results.push(await startBackground(
|
|
1724
|
+
item.agent,
|
|
1725
|
+
item.task,
|
|
1726
|
+
item.cwd,
|
|
1727
|
+
item.vision === true,
|
|
1728
|
+
defaultIsolationMode("parallel", item.agent, item.isolation as IsolationMode | undefined),
|
|
1729
|
+
));
|
|
1730
|
+
}
|
|
1731
|
+
const startedRuns = results.filter((result) => result.exitCode === -1);
|
|
1732
|
+
const started = startedRuns.length;
|
|
1733
|
+
const startedRefs = startedRuns.map((result) =>
|
|
1734
|
+
result.runId === undefined ? result.agent : `#${result.runId} ${result.agent}`,
|
|
1735
|
+
);
|
|
1736
|
+
const failureLines = results.flatMap((result, index) => {
|
|
1737
|
+
if (result.exitCode === -1) return [];
|
|
1738
|
+
const reason = getResultOutput(result).trim() || "unknown startup failure";
|
|
1739
|
+
return [
|
|
1740
|
+
`- tasks[${index}] (${params.tasks![index]!.agent}) failed to start: ${reason.replace(/\n/g, "\n ")}`,
|
|
1741
|
+
];
|
|
1742
|
+
});
|
|
1743
|
+
if (started === 0) {
|
|
1744
|
+
// Pi marks custom-tool failures only when execute throws; returning an
|
|
1745
|
+
// `isError` property is still a successful AgentToolResult.
|
|
1746
|
+
throw new Error(`No background subagents were started.\n${failureLines.join("\n")}`);
|
|
1747
|
+
}
|
|
1748
|
+
const text = [
|
|
1749
|
+
`Started ${started} background subagent${started === 1 ? "" : "s"}: ${startedRefs.join(", ")}. Results will automatically resume the main agent when ready.`,
|
|
1750
|
+
...(failureLines.length > 0
|
|
1751
|
+
? [`${failureLines.length} task${failureLines.length === 1 ? "" : "s"} failed before launch:`, ...failureLines]
|
|
1752
|
+
: []),
|
|
1753
|
+
].join("\n");
|
|
1754
|
+
return {
|
|
1755
|
+
content: [{ type: "text", text }],
|
|
1756
|
+
details: makeDetails("parallel", true)(results),
|
|
1757
|
+
terminate: true,
|
|
1758
|
+
};
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
const result = await startBackground(
|
|
1762
|
+
params.agent as string,
|
|
1763
|
+
params.task as string,
|
|
1764
|
+
params.cwd,
|
|
1765
|
+
params.vision === true,
|
|
1766
|
+
defaultIsolationMode("single", params.agent as string, params.isolation as IsolationMode | undefined),
|
|
1767
|
+
);
|
|
1768
|
+
if (result.exitCode !== -1) {
|
|
1769
|
+
throw new Error(getResultOutput(result));
|
|
1770
|
+
}
|
|
1771
|
+
const runRef = result.runId === undefined ? result.agent : `#${result.runId} ${result.agent}`;
|
|
1772
|
+
return {
|
|
1773
|
+
content: [{ type: "text", text: `Started ${runRef} in the background. Its result will automatically resume the main agent when ready.` }],
|
|
1774
|
+
details: makeDetails("single", true)([result]),
|
|
1775
|
+
terminate: true,
|
|
1776
|
+
};
|
|
1777
|
+
|
|
1778
|
+
},
|
|
1779
|
+
|
|
1780
|
+
renderCall(args, theme) {
|
|
1781
|
+
if (args.tasks && args.tasks.length > 0) {
|
|
1782
|
+
let text = `${theme.fg("toolTitle", theme.bold("subagent "))}${theme.fg("accent", `parallel (${args.tasks.length})`)}`;
|
|
1783
|
+
for (const t of args.tasks.slice(0, 4)) {
|
|
1784
|
+
const preview = formatTaskSummary(t.task, 48);
|
|
1785
|
+
const isolation = defaultIsolationMode("parallel", t.agent, t.isolation) === "worktree" ? " [worktree]" : "";
|
|
1786
|
+
text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", isolation)} ${theme.fg("dim", preview)}`;
|
|
1787
|
+
}
|
|
1788
|
+
if (args.tasks.length > 4) text += `\n ${theme.fg("dim", `… +${args.tasks.length - 4} more`)}`;
|
|
1789
|
+
return new Text(text, 0, 0);
|
|
1790
|
+
}
|
|
1791
|
+
const task: string = args.task ?? "";
|
|
1792
|
+
const preview = formatTaskSummary(task, 60);
|
|
1793
|
+
const isolation = args.isolation === "worktree" ? " [worktree]" : "";
|
|
1794
|
+
return new Text(
|
|
1795
|
+
`${theme.fg("toolTitle", theme.bold("subagent "))}${theme.fg("accent", args.agent ?? "?")}${theme.fg("dim", isolation)} ${theme.fg("dim", preview)}`,
|
|
1796
|
+
0,
|
|
1797
|
+
0,
|
|
1798
|
+
);
|
|
1799
|
+
},
|
|
1800
|
+
|
|
1801
|
+
renderResult(result, _options, theme) {
|
|
1802
|
+
const details = result.details as SubagentDetails | undefined;
|
|
1803
|
+
if (!details || details.results.length === 0) return new Text(theme.fg("dim", "(no output)"), 0, 0);
|
|
1804
|
+
|
|
1805
|
+
if (details.mode === "single") {
|
|
1806
|
+
const r = details.results[0];
|
|
1807
|
+
const pending = r.exitCode === -1;
|
|
1808
|
+
const icon = statusIcon(pending ? "running" : isFailedResult(r) ? "failed" : "done", theme);
|
|
1809
|
+
const usage = formatUsage(r.usage);
|
|
1810
|
+
const model = `${r.model ?? "?"}${r.modelFallbackFrom ? ` (pool fallback from ${r.modelFallbackFrom})` : ""}`;
|
|
1811
|
+
const isolation = r.isolation === "worktree" ? ` · worktree ${r.integrationStatus ?? "active"}` : "";
|
|
1812
|
+
const runId = r.runId === undefined ? "" : `${theme.fg("dim", `#${r.runId}`)} `;
|
|
1813
|
+
const line = `${theme.fg("toolTitle", theme.bold("subagent "))}${icon} ${runId}${theme.fg("accent", r.agent)} ${theme.fg("dim", `· ${model}${r.thinking ? ` · thinking ${r.thinking}` : ""}${isolation}${pending ? " · background" : ""}${usage ? ` · ${usage}` : ""}`)}`;
|
|
1814
|
+
return new Text(line, 0, 0);
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
// Parallel mode: header + one compact line per agent
|
|
1818
|
+
const lines: string[] = [
|
|
1819
|
+
`${theme.fg("toolTitle", theme.bold("subagent "))}${theme.fg("accent", `parallel (${details.results.length})`)}`,
|
|
1820
|
+
];
|
|
1821
|
+
for (const r of details.results) {
|
|
1822
|
+
const pending = r.exitCode === -1;
|
|
1823
|
+
const icon = statusIcon(pending ? "running" : isFailedResult(r) ? "failed" : "done", theme);
|
|
1824
|
+
const usage = formatUsage(r.usage);
|
|
1825
|
+
const model = `${r.model ?? "?"}${r.modelFallbackFrom ? ` (pool fallback from ${r.modelFallbackFrom})` : ""}`;
|
|
1826
|
+
const isolation = r.isolation === "worktree" ? ` · worktree ${r.integrationStatus ?? "active"}` : "";
|
|
1827
|
+
const runId = r.runId === undefined ? "" : `${theme.fg("dim", `#${r.runId}`)} `;
|
|
1828
|
+
lines.push(` ${icon} ${runId}${theme.fg("accent", r.agent)} ${theme.fg("dim", `· ${model}${r.thinking ? ` · thinking ${r.thinking}` : ""}${isolation}${pending ? " · background" : ""}${usage ? ` · ${usage}` : ""}`)}`);
|
|
1829
|
+
}
|
|
1830
|
+
return new Text(lines.join("\n"), 0, 0);
|
|
1831
|
+
},
|
|
1832
|
+
});
|
|
1833
|
+
}
|