-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathsubtasks.test.ts
More file actions
1087 lines (961 loc) · 37.6 KB
/
Copy pathsubtasks.test.ts
File metadata and controls
1087 lines (961 loc) · 37.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
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
759
760
761
762
763
764
765
766
767
768
769
770
771
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
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
import * as assert from "assert"
import { RooCodeEventName, type ClineMessage } from "@roo-code/types"
import { setDefaultSuiteTimeout } from "./test-utils"
import { sleep, waitFor, waitUntilCompleted } from "./utils"
import {
SCHED_COMPLETED_PROMPT,
SCHED_COMPLETED_RESULT,
SCHED_STANDALONE_FOLLOWUP_ANSWER,
SCHED_STANDALONE_PROMPT,
SUBTASK_ABANDON_CHILD_FOLLOWUP_ANSWER,
SUBTASK_ABANDON_PARENT_PROMPT,
SUBTASK_API_HANG_CHILD_MARKER,
SUBTASK_API_HANG_CHILD_RESULT,
SUBTASK_API_HANG_PARENT_MARKER,
SUBTASK_API_HANG_PARENT_PROMPT,
SUBTASK_API_HANG_PARENT_RESULT,
SUBTASK_API_HANG_RESUME_MESSAGE,
SUBTASK_CHILD_FOLLOWUP_ANSWER,
SUBTASK_FAST_CHILD_RESULT,
SUBTASK_FAST_PARENT_PROMPT,
SUBTASK_INTERRUPT_CHILD_FOLLOWUP_ANSWER,
SUBTASK_INTERRUPT_PARENT_PROMPT,
SUBTASK_INTERRUPT_PARENT_RESULT,
SUBTASK_PARENT_PROMPT,
SUBTASK_XPROFILE_DIFFERENT_CHILD_RESULT,
SUBTASK_XPROFILE_PARENT_PROMPT,
SUBTASK_XPROFILE_PARENT_RESULT,
SUBTASK_XPROFILE_SAME_CHILD_RESULT,
} from "../fixtures/subtasks"
type AimockMessageContent = string | Array<{ type?: string; text?: string }>
type AimockJournalEntry = {
body?: {
messages?: Array<{
role?: string
content?: AimockMessageContent
}>
}
}
const messageContentText = (content?: AimockMessageContent) => {
if (typeof content === "string") {
return content
}
return content?.map((part) => part.text ?? "").join("") ?? ""
}
const waitForAimockRequestContaining = async (expectedText: string, excludeText?: string) => {
const aimockUrl = process.env.AIMOCK_URL
assert.ok(aimockUrl, "AIMOCK_URL must be set for aimock journal assertions")
await waitFor(async () => {
const response = await fetch(`${aimockUrl}/__aimock/journal`)
const entries = (await response.json()) as AimockJournalEntry[]
return entries.some((entry) => {
const messages = entry.body?.messages
if (!messages) return false
const entryText = messages.map((m) => messageContentText(m.content)).join("")
if (excludeText && entryText.includes(excludeText)) return false
return messages.some(
(message) => message.role === "user" && messageContentText(message.content).includes(expectedText),
)
})
})
}
suite("Roo Code Subtasks", function () {
setDefaultSuiteTimeout(this)
test("child completing on its first response returns to parent", async () => {
const api = globalThis.api
const says: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
try {
const parentTaskId = await waitUntilCompleted({
api,
start: () =>
api.startNewTask({
configuration: {
mode: "ask",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_FAST_PARENT_PROMPT,
}),
})
assert.ok(
Object.entries(says).some(
([taskId, messages]) =>
taskId !== parentTaskId &&
messages.some(
({ say, text }) =>
say === "completion_result" && text?.trim() === SUBTASK_FAST_CHILD_RESULT,
),
),
"Immediately-completing child should emit its expected result",
)
assert.strictEqual(
says[parentTaskId]
?.filter(({ say }) => say === "completion_result")
.map(({ text }) => text?.trim())
.find((text): text is string => !!text),
"Fast parent resumed",
"Parent should resume after the child completes on its first response",
)
} finally {
api.off(RooCodeEventName.Message, messageHandler)
while (api.getCurrentTaskStack().length > 0) {
await api.clearCurrentTask()
}
await sleep(1_500)
}
})
// Smoke: child completing normally must resume the parent task.
test("child task returns to parent after normal completion", async () => {
const api = globalThis.api
const asks: Record<string, ClineMessage[]> = {}
const says: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "ask") {
asks[taskId] = asks[taskId] || []
asks[taskId].push(message)
}
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
try {
const parentTaskId = await api.startNewTask({
configuration: {
mode: "ask",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_PARENT_PROMPT,
})
// Wait for child to spawn.
let childTaskId: string | undefined
await waitFor(() => {
const stack = api.getCurrentTaskStack()
const current = stack[stack.length - 1]
if (current && current !== parentTaskId) {
childTaskId = current
return true
}
return false
})
// Wait for the child's followup question, then answer so it can complete.
// Register the completion listener before sending the answer to avoid a race.
await waitFor(() => asks[childTaskId!]?.some(({ ask }) => ask === "followup") ?? false)
await waitUntilCompleted({
api,
start: async () => {
await api.sendMessage(SUBTASK_CHILD_FOLLOWUP_ANSWER)
return parentTaskId
},
})
const parentCompletionText = says[parentTaskId]
?.filter(({ say }) => say === "completion_result")
.map(({ text }) => text?.trim())
.find((t): t is string => !!t)
assert.strictEqual(
parentCompletionText,
"Parent task resumed",
"Parent should complete with the expected result after child returns",
)
} finally {
api.off(RooCodeEventName.Message, messageHandler)
// Drain the stack so partially-completed tasks don't leak into the next test.
// On the happy path the parent is already gone; on failure both tasks may still be active.
if (api.getCurrentTaskStack().length > 0) {
await api.clearCurrentTask()
}
if (api.getCurrentTaskStack().length > 0) {
await api.clearCurrentTask()
}
await waitFor(() => api.getCurrentTaskStack().length === 0).catch(() => {})
}
})
test("delegated child completion persists parent and child history state", async () => {
const api = globalThis.api
const asks: Record<string, ClineMessage[]> = {}
const says: Record<string, ClineMessage[]> = {}
let delegationCompletedParentId: string | undefined
let delegationCompletedChildId: string | undefined
let delegationCompletedSummary: string | undefined
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "ask") {
asks[taskId] = asks[taskId] || []
asks[taskId].push(message)
}
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
const delegationCompletedHandler = (parentId: string, childId: string, summary: string) => {
delegationCompletedParentId = parentId
delegationCompletedChildId = childId
delegationCompletedSummary = summary
}
api.on(RooCodeEventName.Message, messageHandler)
api.on(RooCodeEventName.TaskDelegationCompleted, delegationCompletedHandler)
try {
const parentTaskId = await api.startNewTask({
configuration: {
mode: "ask",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_PARENT_PROMPT,
})
let childTaskId: string | undefined
await waitFor(() => {
const stack = api.getCurrentTaskStack()
const current = stack[stack.length - 1]
if (current && current !== parentTaskId) {
childTaskId = current
return true
}
return false
})
await waitFor(() => asks[childTaskId!]?.some(({ ask }) => ask === "followup") ?? false)
// Send the answer, then wait for TaskDelegationCompleted. That event fires after
// atomicUpdatePair writes the persisted history but before the parent is re-created,
// so it is the right gate for history assertions. waitUntilCompleted alone is not
// sufficient because the resumed parent runs into a mock 404 and never emits
// TaskCompleted in the test environment.
await api.sendMessage(SUBTASK_CHILD_FOLLOWUP_ANSWER)
await waitFor(() => delegationCompletedParentId !== undefined)
assert.strictEqual(
delegationCompletedParentId,
parentTaskId,
"TaskDelegationCompleted should fire for parent",
)
assert.strictEqual(delegationCompletedChildId, childTaskId, "TaskDelegationCompleted should fire for child")
assert.strictEqual(delegationCompletedSummary, "9", "TaskDelegationCompleted summary should be '9'")
const parent = await api.getTaskHistoryItem(parentTaskId)
assert.ok(parent, "Parent history item should exist")
assert.ok(
parent.status === "active" || parent.status === "completed",
`Parent status should be 'active' or 'completed' after child completes (got '${parent.status}')`,
)
assert.strictEqual(parent.awaitingChildId, undefined, "Parent awaitingChildId should be cleared")
assert.strictEqual(parent.delegatedToId, undefined, "Parent delegatedToId should be cleared")
assert.strictEqual(parent.completedByChildId, childTaskId, "Parent completedByChildId should be the child")
assert.strictEqual(parent.completionResultSummary, "9", "Parent completionResultSummary should be '9'")
assert.ok(parent.childIds?.includes(childTaskId!), "Parent childIds should include the child")
const child = await api.getTaskHistoryItem(childTaskId!)
assert.ok(child, "Child history item should exist")
assert.strictEqual(child.status, "completed", "Child status should be 'completed'")
assert.strictEqual(child.parentTaskId, parentTaskId, "Child parentTaskId should point to parent")
assert.strictEqual(child.completionResultSummary, "9", "Child completionResultSummary should be '9'")
} finally {
api.off(RooCodeEventName.Message, messageHandler)
api.off(RooCodeEventName.TaskDelegationCompleted, delegationCompletedHandler)
// TaskDelegationCompleted fires before createTaskWithHistoryItem completes,
// so the reopened parent may not be on the stack yet. Wait for it to appear
// before draining, otherwise the next test sees a late-rehydrated stray task.
if (delegationCompletedParentId) {
await waitFor(
() =>
api.getCurrentTaskStack().length > 0 ||
api.getCurrentTaskStack().includes(delegationCompletedParentId!),
).catch(() => {})
}
while (api.getCurrentTaskStack().length > 0) {
await api.clearCurrentTask()
}
await waitFor(() => api.getCurrentTaskStack().length === 0).catch(() => {})
}
})
// Race mitigation: skipDelegationRepair prevents removeClineFromStack from
// auto-resuming the parent when the child is cancelled (Race 2).
test("parent stays paused after subtask cancellation", async () => {
const api = globalThis.api
const asks: Record<string, ClineMessage[]> = {}
const messages: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "ask") {
asks[taskId] = asks[taskId] || []
asks[taskId].push(message)
}
if (message.type === "say" && message.partial === false) {
messages[taskId] = messages[taskId] || []
messages[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
try {
const parentTaskId = await api.startNewTask({
configuration: {
mode: "ask",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_PARENT_PROMPT,
})
let spawnedTaskId: string | undefined
await waitFor(() => {
const stack = api.getCurrentTaskStack()
const current = stack[stack.length - 1]
if (current && current !== parentTaskId) {
spawnedTaskId = current
return true
}
return false
})
await waitFor(
() => asks[spawnedTaskId!]?.some(({ type, ask }) => type === "ask" && ask === "followup") ?? false,
)
await api.cancelCurrentTask()
// Gate on the async settle before asserting the parent never resumed: a spurious
// resume would be an async downstream effect of the cancellation, so a synchronous
// check right after cancelCurrentTask() proves nothing.
await waitFor(() => api.getCurrentTaskStack().at(-1) === spawnedTaskId)
await waitFor(
() => asks[spawnedTaskId!]?.some(({ type, ask }) => type === "ask" && ask === "resume_task") ?? false,
)
assert.ok(
messages[parentTaskId]?.find(({ type, text }) => type === "say" && text === "Parent task resumed") ===
undefined,
"Parent task should not have resumed after subtask cancellation",
)
assert.strictEqual(
messages[parentTaskId]?.find(({ say }) => say === "completion_result"),
undefined,
"Parent must not have completed after subtask cancellation",
)
await api.clearCurrentTask()
// The parent task is still in the stack; drain it so it doesn't leak into the next test.
await api.clearCurrentTask()
await waitFor(() => api.getCurrentTaskStack().length === 0)
} finally {
api.off(RooCodeEventName.Message, messageHandler)
}
})
// Race mitigation: runDelegationTransition lock + cancelledDelegationChildIds guard
// ensures cancelTask() wins over a concurrent reopenParentFromDelegation() (Race 3).
// Before issue #560 was fixed, a cancelled child would have its parent link severed on cancel, so
// it would complete in-place without reopening the parent. The correct behavior (post-fix) is that
// the cancelled child is marked "interrupted", and when it resumes and completes it reopens the parent.
test("cancelled child completes and reopens parent", async () => {
const api = globalThis.api
const asks: Record<string, ClineMessage[]> = {}
const says: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "ask") {
asks[taskId] = asks[taskId] || []
asks[taskId].push(message)
}
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
try {
const parentTaskId = await api.startNewTask({
configuration: {
mode: "ask",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_PARENT_PROMPT,
})
let spawnedTaskId: string | undefined
await waitFor(() => {
const stack = api.getCurrentTaskStack()
const current = stack[stack.length - 1]
if (current && current !== parentTaskId) {
spawnedTaskId = current
return true
}
return false
})
await waitFor(
() => asks[spawnedTaskId!]?.some(({ type, ask }) => type === "ask" && ask === "followup") ?? false,
)
await waitFor(async () => (await api.getTaskApiConversationHistoryLength(spawnedTaskId!)) > 0)
const cancelledChildTaskId = spawnedTaskId!
await api.cancelCurrentTask()
await waitFor(() => api.getCurrentTaskStack().at(-1) === cancelledChildTaskId)
await waitFor(
() =>
asks[cancelledChildTaskId]?.some(({ type, ask }) => type === "ask" && ask === "resume_task") ??
false,
)
// Resume the child — it should complete and reopen the parent (fix for #560)
const completedTaskId = await waitUntilCompleted({
api,
start: async () => {
await api.sendMessage(SUBTASK_CHILD_FOLLOWUP_ANSWER)
return parentTaskId
},
})
assert.strictEqual(
completedTaskId,
parentTaskId,
"Parent task should complete after interrupted child reports back",
)
assert.strictEqual(
says[parentTaskId]?.find(({ say }) => say === "completion_result")?.text?.trim(),
"Parent task resumed",
"Parent task should complete with its expected result",
)
} finally {
api.off(RooCodeEventName.Message, messageHandler)
}
})
// Issue #566: a child interrupted while its provider request is still pending
// must keep its parent link when manually resumed and completed.
test("API-hung interrupted child resumes and returns to parent", async () => {
const api = globalThis.api
const asks: Record<string, ClineMessage[]> = {}
const says: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "ask") {
asks[taskId] = asks[taskId] || []
asks[taskId].push(message)
}
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
try {
const parentTaskId = await api.startNewTask({
configuration: {
mode: "ask",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_API_HANG_PARENT_PROMPT,
})
let childTaskId: string | undefined
await waitFor(() => {
const stack = api.getCurrentTaskStack()
const current = stack[stack.length - 1]
if (current && current !== parentTaskId) {
childTaskId = current
return true
}
return false
})
await waitForAimockRequestContaining(SUBTASK_API_HANG_CHILD_MARKER, SUBTASK_API_HANG_PARENT_MARKER)
await api.cancelCurrentTask()
await waitFor(() => api.getCurrentTaskStack().at(-1) === childTaskId)
await waitFor(
() => asks[childTaskId!]?.some(({ type, ask }) => type === "ask" && ask === "resume_task") ?? false,
)
const interruptedChild = await api.getTaskHistoryItem(childTaskId!)
assert.strictEqual(interruptedChild?.status, "interrupted", "Child should be interrupted after manual stop")
assert.strictEqual(
interruptedChild?.parentTaskId,
parentTaskId,
"Interrupted child should retain its parent link before resume",
)
const completedParentTaskId = await waitUntilCompleted({
api,
start: async () => {
await api.sendMessage(SUBTASK_API_HANG_RESUME_MESSAGE)
return parentTaskId
},
})
assert.strictEqual(
completedParentTaskId,
parentTaskId,
"Parent task should complete after API-hung child resumes and reports back",
)
assert.strictEqual(
says[childTaskId!]
?.filter(({ say }) => say === "completion_result")
.map(({ text }) => text?.trim())
.find((text): text is string => !!text),
SUBTASK_API_HANG_CHILD_RESULT,
"Child should complete with its expected result after resume",
)
assert.strictEqual(
says[parentTaskId]
?.filter(({ say }) => say === "completion_result")
.map(({ text }) => text?.trim())
.find((text): text is string => !!text),
SUBTASK_API_HANG_PARENT_RESULT,
"Parent should resume and complete with its expected result",
)
const parent = await api.getTaskHistoryItem(parentTaskId)
assert.notStrictEqual(parent?.status, "delegated", "Parent history should not remain delegated")
assert.strictEqual(parent?.awaitingChildId, undefined, "Parent awaitingChildId should be cleared")
assert.strictEqual(parent?.completedByChildId, childTaskId, "Parent should record completed child")
const child = await api.getTaskHistoryItem(childTaskId!)
assert.strictEqual(child?.status, "completed", "Child history should be completed")
assert.strictEqual(child?.parentTaskId, parentTaskId, "Completed child should still point to parent")
} finally {
api.off(RooCodeEventName.Message, messageHandler)
while (api.getCurrentTaskStack().length > 0) {
await api.clearCurrentTask()
}
await waitFor(() => api.getCurrentTaskStack().length === 0).catch(() => {})
}
})
test("same-profile child returns before a different-profile child", async () => {
const api = globalThis.api
const says: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
const aimockUrl = process.env.AIMOCK_URL
const parentProfile = {
apiProvider: "openrouter" as const,
openRouterApiKey: "mock-key",
openRouterModelId: "openai/gpt-4.1",
rateLimitSeconds: 0,
...(aimockUrl && { openRouterBaseUrl: `${aimockUrl}/v1` }),
}
const childProfile = {
...parentProfile,
openRouterModelId: "openai/gpt-4.1-mini",
}
const priorModeApiConfigs = api.getConfiguration().modeApiConfigs ?? {}
const parentProfileId = await api.upsertProfile("subtask-parent-profile", parentProfile, true)
const childProfileId = await api.upsertProfile("subtask-child-profile", childProfile, false)
await api.setConfiguration({
modeApiConfigs: {
code: parentProfileId!,
ask: childProfileId!,
},
})
try {
let parentTaskId: string
try {
parentTaskId = await waitUntilCompleted({
api,
start: () =>
api.startNewTask({
configuration: {
mode: "code",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_XPROFILE_PARENT_PROMPT,
}),
})
} catch (error) {
const messageSummary = Object.fromEntries(
Object.entries(says).map(([taskId, messages]) => [
taskId,
messages.map(({ say, text }) => ({ say, text: text?.slice(0, 200) })),
]),
)
throw new Error(
`Sequential cross-profile subtasks did not complete. Stack: ${JSON.stringify(api.getCurrentTaskStack())}; ` +
`messages: ${JSON.stringify(messageSummary)}`,
{ cause: error },
)
}
const sameProfileChildId = Object.entries(says).find(
([taskId, messages]) =>
taskId !== parentTaskId &&
messages.some(
({ say, text }) =>
say === "completion_result" && text?.trim() === SUBTASK_XPROFILE_SAME_CHILD_RESULT,
),
)?.[0]
const differentProfileChildId = Object.entries(says).find(
([taskId, messages]) =>
taskId !== parentTaskId &&
messages.some(
({ say, text }) =>
say === "completion_result" && text?.trim() === SUBTASK_XPROFILE_DIFFERENT_CHILD_RESULT,
),
)?.[0]
assert.ok(sameProfileChildId, "Same-profile child should return to the parent")
assert.ok(differentProfileChildId, "Different-profile child should return to the parent")
assert.notStrictEqual(
sameProfileChildId,
differentProfileChildId,
"Parent should delegate to two distinct child tasks",
)
assert.strictEqual(
says[parentTaskId]
?.filter(({ say }) => say === "completion_result")
.map(({ text }) => text?.trim())
.find((text): text is string => !!text),
SUBTASK_XPROFILE_PARENT_RESULT,
"Parent should resume after both sequential children complete",
)
} finally {
api.off(RooCodeEventName.Message, messageHandler)
await api.setConfiguration({ modeApiConfigs: priorModeApiConfigs })
await api.deleteProfile("subtask-child-profile").catch(() => {})
await api.deleteProfile("subtask-parent-profile").catch(() => {})
while (api.getCurrentTaskStack().length > 0) {
await api.clearCurrentTask()
}
}
})
// Issue #560: interrupted child resumes and reports back to parent.
// Before the fix, cancelTask() severed the parent link, so the resumed child
// fell through to "Start New Task" instead of delegating back to the parent.
test("interrupted child resumes and reports back to parent", async () => {
const api = globalThis.api
const asks: Record<string, ClineMessage[]> = {}
const says: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "ask") {
asks[taskId] = asks[taskId] || []
asks[taskId].push(message)
}
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
try {
const parentTaskId = await api.startNewTask({
configuration: {
mode: "ask",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_INTERRUPT_PARENT_PROMPT,
})
// Wait for child to spawn
let childTaskId: string | undefined
await waitFor(() => {
const stack = api.getCurrentTaskStack()
const current = stack[stack.length - 1]
if (current && current !== parentTaskId) {
childTaskId = current
return true
}
return false
})
// Wait for the child's followup question
await waitFor(() => asks[childTaskId!]?.some(({ ask }) => ask === "followup") ?? false)
await waitFor(async () => (await api.getTaskApiConversationHistoryLength(childTaskId!)) > 0)
// Cancel the child — it should be marked "interrupted", parent stays "delegated"
await api.cancelCurrentTask()
// Child should be back on the stack (rehydrated as interrupted)
await waitFor(() => api.getCurrentTaskStack().at(-1) === childTaskId)
await waitFor(
() => asks[childTaskId!]?.some(({ type, ask }) => type === "ask" && ask === "resume_task") ?? false,
)
// Parent must not have resumed yet
assert.strictEqual(
says[parentTaskId]?.find(({ say }) => say === "completion_result"),
undefined,
"Parent must not have resumed while child is interrupted",
)
// Resume the child and answer the followup — child should complete and reopen parent.
const completedParentTaskId = await waitUntilCompleted({
api,
start: async () => {
await api.sendMessage(SUBTASK_INTERRUPT_CHILD_FOLLOWUP_ANSWER)
return parentTaskId
},
})
assert.strictEqual(
completedParentTaskId,
parentTaskId,
"Parent task should be the one that completes after interrupted child reports back",
)
assert.strictEqual(
says[parentTaskId]
?.filter(({ say }) => say === "completion_result")
.map(({ text }) => text?.trim())
.find((text) => text === SUBTASK_INTERRUPT_PARENT_RESULT),
SUBTASK_INTERRUPT_PARENT_RESULT,
"Parent should complete with expected result after interrupted child reports back",
)
} finally {
api.off(RooCodeEventName.Message, messageHandler)
while (api.getCurrentTaskStack().length > 0) {
await api.clearCurrentTask()
}
await waitFor(() => api.getCurrentTaskStack().length === 0).catch(() => {})
}
})
// Issue #559: explicit "Abandon subtask" action. Unlike cancellation alone (which leaves
// the child "interrupted" and the parent "delegated" so the child can still resume and
// report back), abandoning severs the link outright: the parent goes back to "active" and
// the child's parentTaskId/rootTaskId are cleared so a later resume can never reattach it.
test("abandoning an interrupted subtask severs the parent-child link", async () => {
const api = globalThis.api
const asks: Record<string, ClineMessage[]> = {}
const says: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "ask") {
asks[taskId] = asks[taskId] || []
asks[taskId].push(message)
}
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
try {
const parentTaskId = await api.startNewTask({
configuration: {
mode: "ask",
alwaysAllowModeSwitch: true,
alwaysAllowSubtasks: true,
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SUBTASK_ABANDON_PARENT_PROMPT,
})
let childTaskId: string | undefined
await waitFor(() => {
const stack = api.getCurrentTaskStack()
const current = stack[stack.length - 1]
if (current && current !== parentTaskId) {
childTaskId = current
return true
}
return false
})
await waitFor(() => asks[childTaskId!]?.some(({ ask }) => ask === "followup") ?? false)
await waitFor(async () => (await api.getTaskApiConversationHistoryLength(childTaskId!)) > 0)
// Cancel the child — marked "interrupted", parent stays "delegated".
await api.cancelCurrentTask()
await waitFor(() => api.getCurrentTaskStack().at(-1) === childTaskId)
await waitFor(
() => asks[childTaskId!]?.some(({ type, ask }) => type === "ask" && ask === "resume_task") ?? false,
)
const interruptedChild = await api.getTaskHistoryItem(childTaskId!)
assert.strictEqual(interruptedChild?.status, "interrupted", "Child should be marked interrupted")
const delegatedParent = await api.getTaskHistoryItem(parentTaskId)
assert.strictEqual(delegatedParent?.status, "delegated", "Parent should still be delegated before abandon")
assert.strictEqual(
delegatedParent?.awaitingChildId,
childTaskId,
"Parent should await the interrupted child",
)
// The interrupted child is the live/open task at this point (cancelTask rehydrates
// it onto the stack). Abandon must close that live instance before severing the
// persisted link — otherwise a later save on the still-open child would rebuild
// parentTaskId/rootTaskId from its live (readonly) fields and silently reattach it.
const abandoned = await api.abandonSubtask(childTaskId!)
assert.strictEqual(abandoned, true, "abandonSubtask should report the link was severed")
await waitFor(() => api.getCurrentTaskStack().at(-1) !== childTaskId)
const parentAfterAbandon = await api.getTaskHistoryItem(parentTaskId)
assert.strictEqual(parentAfterAbandon?.status, "active", "Parent should return to active after abandon")
assert.strictEqual(
parentAfterAbandon?.awaitingChildId,
undefined,
"Parent awaitingChildId should be cleared",
)
assert.strictEqual(parentAfterAbandon?.delegatedToId, undefined, "Parent delegatedToId should be cleared")
const childAfterAbandon = await api.getTaskHistoryItem(childTaskId!)
// The child's own status is left untouched (VALID_TRANSITIONS only allows interrupted → completed);
// only its parent/root links are cleared so it can never reattach to the parent again.
assert.strictEqual(childAfterAbandon?.status, "interrupted", "Child status stays interrupted")
assert.strictEqual(childAfterAbandon?.parentTaskId, undefined, "Child parentTaskId should be cleared")
assert.strictEqual(childAfterAbandon?.rootTaskId, undefined, "Child rootTaskId should be cleared")
// A second abandon call is a no-op since the parent is no longer delegated to this child.
const secondAbandon = await api.abandonSubtask(childTaskId!)
assert.strictEqual(secondAbandon, false, "Second abandonSubtask call should be a no-op")
// Resume and complete the abandoned child — it must NOT reopen or reattach to the
// parent. Before the abandon fix, a subsequent save on the still-live child could
// silently rewrite its persisted parentTaskId back to the parent; this proves the
// link stays severed all the way through a real resume/save/complete cycle.
// api.resumeTask() re-instantiates the child from history, which re-raises its own
// "resume_task" ask; answering it with the follow-up answer (same pattern the sibling
// "cancelled child completes and reopens parent" test above uses) both resumes the
// task and supplies the answer the re-asked follow-up question is waiting for.
// asks[childTaskId] already holds the earlier resume_task ask from the pre-abandon
// cancellation, so the wait below must look for a NEW one, not just any occurrence.
const askCountBeforeResume = asks[childTaskId!]?.length ?? 0
await api.resumeTask(childTaskId!)
await waitFor(() =>
(asks[childTaskId!] ?? [])
.slice(askCountBeforeResume)
.some(({ type, ask }) => type === "ask" && ask === "resume_task"),
)
const completedChildTaskId = await waitUntilCompleted({
api,
start: async () => {
await api.sendMessage(SUBTASK_ABANDON_CHILD_FOLLOWUP_ANSWER)
return childTaskId!
},
})
assert.strictEqual(
completedChildTaskId,
childTaskId,
"The abandoned child itself should be the task that completes, not the parent",
)
assert.strictEqual(
says[parentTaskId]?.find(({ say }) => say === "completion_result"),
undefined,
"Parent must never complete/reopen after its abandoned child resumes and completes",
)
const parentAfterChildCompletes = await api.getTaskHistoryItem(parentTaskId)
assert.strictEqual(
parentAfterChildCompletes?.status,
"active",
"Parent status must remain untouched by the abandoned child's completion",
)
assert.strictEqual(
parentAfterChildCompletes?.awaitingChildId,
undefined,
"Parent must not start awaiting the abandoned child again",
)
const childAfterCompletion = await api.getTaskHistoryItem(childTaskId!)
assert.strictEqual(
childAfterCompletion?.parentTaskId,
undefined,
"Child parentTaskId must still be cleared after it completes on its own — " +
"proves the live-instance save did not resurrect the old link",
)
} finally {
api.off(RooCodeEventName.Message, messageHandler)
while (api.getCurrentTaskStack().length > 0) {
await api.clearCurrentTask()
}
await waitFor(() => api.getCurrentTaskStack().length === 0).catch(() => {})
}
})
// TaskScheduler regression: resumeTask on a completed task must show resume_completed_task ask.
// Before the CodeRabbit fix, createTaskWithHistoryItem bypassed the scheduler and called
// Task.run() via the constructor's startTask: true default, causing run() to call startTask()
// (clearing history) instead of resumeTaskFromHistory().
test("resumeTask on a completed task presents resume_completed_task ask", async () => {
const api = globalThis.api
const asks: Record<string, ClineMessage[]> = {}
const says: Record<string, ClineMessage[]> = {}
const messageHandler = ({ taskId, message }: { taskId: string; message: ClineMessage }) => {
if (message.type === "ask") {
asks[taskId] = asks[taskId] || []
asks[taskId].push(message)
}
if (message.type === "say" && message.partial === false) {
says[taskId] = says[taskId] || []
says[taskId].push(message)
}
}
api.on(RooCodeEventName.Message, messageHandler)
try {
// Run a task to completion.
const taskId = await waitUntilCompleted({
api,
start: () =>
api.startNewTask({
configuration: {
mode: "ask",
autoApprovalEnabled: true,
enableCheckpoints: false,
},
text: SCHED_COMPLETED_PROMPT,
}),
})
assert.strictEqual(
says[taskId]?.find(({ say }) => say === "completion_result")?.text?.trim(),
SCHED_COMPLETED_RESULT,
"Task should complete with expected result",
)
// Re-open it via resumeTask — should hit resumeTaskFromHistory(), showing resume_completed_task.
await api.resumeTask(taskId)
await waitFor(
() => asks[taskId]?.some(({ type, ask }) => type === "ask" && ask === "resume_completed_task") ?? false,