-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathsubtasks.test.ts
More file actions
192 lines (166 loc) · 5.81 KB
/
Copy pathsubtasks.test.ts
File metadata and controls
192 lines (166 loc) · 5.81 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
import * as assert from "assert"
import { RooCodeEventName, type ClineMessage } from "@roo-code/types"
import { setDefaultSuiteTimeout } from "./test-utils"
import { waitFor, waitUntilCompleted } from "./utils"
import { SUBTASK_CHILD_FOLLOWUP_ANSWER, SUBTASK_PARENT_PROMPT } from "../fixtures/subtasks"
suite("Roo Code Subtasks", function () {
setDefaultSuiteTimeout(this)
// 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()
assert.ok(
messages[parentTaskId]?.find(({ type, text }) => type === "say" && text === "Parent task resumed") ===
undefined,
"Parent task should not have resumed after subtask cancellation",
)
await waitFor(() => api.getCurrentTaskStack().at(-1) === spawnedTaskId)
await waitFor(
() => asks[spawnedTaskId!]?.some(({ type, ask }) => type === "ask" && ask === "resume_task") ?? false,
)
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).
test("cancelled child completes in-place and does not reopen parent", 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)
}
}
const findCompletionText = (taskId: string) =>
messages[taskId]
?.filter(
(message) =>
message.type === "say" && (message.say === "completion_result" || message.say === "text"),
)
.map((message) => message.text?.trim())
.find((text): text is string => !!text)
const findErrorText = (taskId: string) =>
messages[taskId]
?.filter((message) => message.type === "say" && message.say === "error")
.map((message) => message.text?.trim())
.find((text): text is string => !!text)
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,
)
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,
)
const resumedChildTaskId = await waitUntilCompleted({
api,
start: async () => {
await api.sendMessage(SUBTASK_CHILD_FOLLOWUP_ANSWER)
return cancelledChildTaskId
},
})
assert.strictEqual(
resumedChildTaskId,
cancelledChildTaskId,
"Cancelled child task should be resumed in place",
)
assert.strictEqual(
findErrorText(resumedChildTaskId),
undefined,
"Resumed child task should not emit an error",
)
assert.strictEqual(
findCompletionText(resumedChildTaskId),
"9",
"Resumed child task should complete with `9`",
)
assert.strictEqual(
api.getCurrentTaskStack().at(-1),
cancelledChildTaskId,
"Cancelled child task should remain the active completed task",
)
assert.ok(
messages[parentTaskId]?.find(({ type, text }) => type === "say" && text === "Parent task resumed") ===
undefined,
"Parent task should not have resumed after the cancelled child completed",
)
await api.clearCurrentTask()
} finally {
api.off(RooCodeEventName.Message, messageHandler)
}
})
})