Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import {
4 applyGrokSubagentUpdate,
5 applyGrokWorkflowUpdate,
6 emptyGrokSubagentTrackState,
7 grokWorkflowMemberTaskId,
8 parseXAiSubagentUpdate,
9 parseXAiWorkflowUpdated,
10} from "./GrokAcpSubagents.ts";
11
12describe("parseXAiSubagentUpdate", () => {
13 it("reads snake_case subagent_spawned envelopes", () => {
14 const parsed = parseXAiSubagentUpdate({
15 sessionId: "parent-session",
16 update: {
17 sessionUpdate: "subagent_spawned",
18 subagent_id: "child-1",
19 subagent_type: "explore",
20 description: "Search the codebase",
21 child_session_id: "child-session-1",
22 model: "grok-4.6",
23 },
24 });
25 expect(parsed).toEqual({
26 kind: "spawned",
27 subagentId: "child-1",
28 childSessionId: "child-session-1",
29 role: "explore",
30 description: "Search the codebase",
31 model: "grok-4.6",
32 status: undefined,
33 error: undefined,
34 tokensUsed: undefined,
35 durationMs: undefined,
36 turnCount: undefined,
37 toolCallCount: undefined,
38 lastToolName: undefined,
39 output: undefined,
40 });
41 });
42
43 it("accepts PascalCase tags and camelCase fields", () => {
44 const parsed = parseXAiSubagentUpdate({
45 sessionUpdate: "SubagentFinished",
46 subagentId: "child-2",
47 agentType: "plan",
48 status: "failed",
49 error: "depth limit",
50 tokensUsed: 12,
51 durationMs: 40,
52 toolCallCount: 3,
53 });
54 expect(parsed?.kind).toBe("finished");
55 expect(parsed?.subagentId).toBe("child-2");
56 expect(parsed?.role).toBe("plan");
57 expect(parsed?.error).toBe("depth limit");
58 expect(parsed?.tokensUsed).toBe(12);
59 expect(parsed?.toolCallCount).toBe(3);
60 });
61
62 it("ignores unrelated session extras", () => {
63 expect(
64 parseXAiSubagentUpdate({
65 update: { sessionUpdate: "auto_compact_started", tokens_used: 100 },
66 }),
67 ).toBeUndefined();
68 });
69});
70
71describe("applyGrokSubagentUpdate", () => {
72 it("maps spawn/progress/finish onto the shared child-task path", () => {
73 const spawned = applyGrokSubagentUpdate(emptyGrokSubagentTrackState(), {
74 kind: "spawned",
75 subagentId: "child-1",
76 childSessionId: "child-session",
77 role: "explore",
78 description: "Search the codebase",
79 model: "grok-4.6",
80 status: undefined,
81 error: undefined,
82 tokensUsed: undefined,
83 durationMs: undefined,
84 turnCount: undefined,
85 toolCallCount: undefined,
86 lastToolName: undefined,
87 output: undefined,
88 });
89 expect(spawned.events).toEqual([
90 {
91 type: "task.started",
92 payload: {
93 taskId: "child-1",
94 description: "Search the codebase",
95 title: "Search the codebase",
96 taskType: "subagent",
97 role: "explore",
98 model: "grok-4.6",
99 agentPath: "child-session",
100 timelineBypass: true,
101 },
102 },
103 ]);
104 expect(spawned.events[0]?.payload.parentAgentId).toBeUndefined();
105
106 const progressed = applyGrokSubagentUpdate(spawned.state, {
107 kind: "progress",
108 subagentId: "child-1",
109 childSessionId: "child-session",
110 role: "explore",
111 description: "Search the codebase",
112 model: "grok-4.6",
113 status: "running",
114 error: undefined,
115 tokensUsed: 80,
116 durationMs: 200,
117 turnCount: 1,
118 toolCallCount: 2,
119 lastToolName: "grep",
120 output: undefined,
121 });
122 expect(progressed.events).toHaveLength(1);
123 expect(progressed.events[0]).toMatchObject({
124 type: "task.progress",
125 payload: {
126 taskId: "child-1",
127 status: "running",
128 lastToolName: "grep",
129 typedUsage: { totalTokens: 80, durationMs: 200, toolUses: 2 },
130 },
131 });
132
133 const finished = applyGrokSubagentUpdate(progressed.state, {
134 kind: "finished",
135 subagentId: "child-1",
136 childSessionId: "child-session",
137 role: "explore",
138 description: "Search the codebase",
139 model: "grok-4.6",
140 status: "completed",
141 error: undefined,
142 tokensUsed: 120,
143 durationMs: 400,
144 turnCount: 2,
145 toolCallCount: 4,
146 lastToolName: undefined,
147 output: "Found 3 call sites.",
148 });
149 expect(finished.events).toEqual([
150 {
151 type: "task.completed",
152 payload: {
153 taskId: "child-1",
154 description: "Search the codebase",
155 title: "Search the codebase",
156 taskType: "subagent",
157 role: "explore",
158 model: "grok-4.6",
159 agentPath: "child-session",
160 timelineBypass: true,
161 status: "completed",
162 summary: "Found 3 call sites.",
163 typedUsage: { totalTokens: 120, durationMs: 400, toolUses: 4 },
164 },
165 },
166 ]);
167 });
168
169 it("does not treat the parent ACP session as a workflow coordinator", () => {
170 const parsed = parseXAiSubagentUpdate({
171 sessionId: "parent-session",
172 parent_session_id: "parent-session",
173 update: {
174 sessionUpdate: "subagent_spawned",
175 subagent_id: "child-1",
176 subagent_type: "explore",
177 },
178 });
179 expect(parsed).toBeDefined();
180 const applied = applyGrokSubagentUpdate(emptyGrokSubagentTrackState(), parsed!);
181 expect(applied.events[0]?.payload.parentAgentId).toBeUndefined();
182 expect(applied.events[0]?.payload.taskType).toBe("subagent");
183 });
184
185 it("completes a first-seen terminal subagent", () => {
186 const applied = applyGrokSubagentUpdate(emptyGrokSubagentTrackState(), {
187 kind: "finished",
188 subagentId: "late-1",
189 childSessionId: undefined,
190 role: "general-purpose",
191 description: undefined,
192 model: undefined,
193 status: "failed",
194 error: "cancelled by parent",
195 tokensUsed: 9,
196 durationMs: 30,
197 turnCount: undefined,
198 toolCallCount: undefined,
199 lastToolName: undefined,
200 output: undefined,
201 });
202 expect(applied.events.map((event) => event.type)).toEqual(["task.started", "task.completed"]);
203 expect(applied.events[1]?.payload).toMatchObject({
204 taskId: "late-1",
205 status: "failed",
206 summary: "cancelled by parent",
207 });
208 });
209
210 it("keeps spawn-time token count when a later tick only reports tools", () => {
211 const spawned = applyGrokSubagentUpdate(emptyGrokSubagentTrackState(), {
212 kind: "spawned",
213 subagentId: "child-1",
214 childSessionId: undefined,
215 role: "explore",
216 description: undefined,
217 model: undefined,
218 status: undefined,
219 error: undefined,
220 tokensUsed: 50,
221 durationMs: undefined,
222 turnCount: undefined,
223 toolCallCount: undefined,
224 lastToolName: undefined,
225 output: undefined,
226 });
227 const toolOnly = applyGrokSubagentUpdate(spawned.state, {
228 kind: "progress",
229 subagentId: "child-1",
230 childSessionId: undefined,
231 role: "explore",
232 description: undefined,
233 model: undefined,
234 status: "running",
235 error: undefined,
236 tokensUsed: undefined,
237 durationMs: 900,
238 turnCount: undefined,
239 toolCallCount: 7,
240 lastToolName: "bash",
241 output: undefined,
242 });
243 expect(toolOnly.events[0]?.payload.typedUsage).toEqual({
244 totalTokens: 50,
245 durationMs: 900,
246 toolUses: 7,
247 });
248 });
249});
250
251describe("parseXAiWorkflowUpdated", () => {
252 it("reads a workflow_updated envelope", () => {
253 const parsed = parseXAiWorkflowUpdated({
254 update: {
255 sessionUpdate: "workflow_updated",
256 run_id: "run-1",
257 name: "review",
258 objective: "Review the PR",
259 status: "active",
260 current_phase: "review",
261 phases: [{ title: "review", state: "running" }],
262 agents: [
263 {
264 agent_id: "agent-a",
265 label: "Reviewer",
266 phase: "review",
267 model: "grok-4.6",
268 state: "running",
269 tokens_used: 20,
270 duration_ms: 100,
271 },
272 ],
273 },
274 });
275 expect(parsed?.runId).toBe("run-1");
276 expect(parsed?.agents).toHaveLength(1);
277 expect(parsed?.agents[0]?.agentId).toBe("agent-a");
278 });
279});
280
281describe("applyGrokWorkflowUpdate", () => {
282 it("stamps members with parentAgentId, timelineBypass, and a stable slot", () => {
283 const applied = applyGrokWorkflowUpdate(emptyGrokSubagentTrackState(), {
284 runId: "run-1",
285 revision: 1,
286 name: "review",
287 objective: "Review the PR",
288 status: "active",
289 phases: [{ title: "review", state: "running" }],
290 currentPhase: "review",
291 agentBudget: 4,
292 agentsUsed: 1,
293 elapsedMs: 100,
294 activeAgents: 1,
295 currentAgentLabel: "Reviewer",
296 agents: [
297 {
298 agentId: "agent-a",
299 label: "Reviewer",
300 phase: "review",
301 model: "grok-4.6",
302 state: "running",
303 tokensUsed: 20,
304 durationMs: 100,
305 },
306 ],
307 pauseMessage: undefined,
308 resultSummary: undefined,
309 });
310
311 expect(applied.events[0]).toMatchObject({
312 type: "task.started",
313 payload: {
314 taskId: "run-1",
315 taskType: "local_workflow",
316 workflowName: "review",
317 },
318 });
319 const memberStart = applied.events.find(
320 (event) =>
321 event.type === "task.started" &&
322 event.payload.taskId === grokWorkflowMemberTaskId("run-1", "agent-a"),
323 );
324 expect(memberStart?.payload).toMatchObject({
325 parentAgentId: "run-1",
326 timelineBypass: true,
327 taskType: "subagent",
328 title: "Reviewer",
329 model: "grok-4.6",
330 });
331 });
332
333 it("skips unchanged member ticks", () => {
334 const first = applyGrokWorkflowUpdate(emptyGrokSubagentTrackState(), {
335 runId: "run-1",
336 revision: 1,
337 name: "review",
338 objective: "Review the PR",
339 status: "active",
340 phases: [],
341 currentPhase: undefined,
342 agentBudget: undefined,
343 agentsUsed: undefined,
344 elapsedMs: undefined,
345 activeAgents: undefined,
346 currentAgentLabel: undefined,
347 agents: [
348 {
349 agentId: "agent-a",
350 label: "Reviewer",
351 phase: undefined,
352 model: undefined,
353 state: "running",
354 tokensUsed: 10,
355 durationMs: 50,
356 },
357 ],
358 pauseMessage: undefined,
359 resultSummary: undefined,
360 });
361 const second = applyGrokWorkflowUpdate(first.state, {
362 runId: "run-1",
363 revision: 2,
364 name: "review",
365 objective: "Review the PR",
366 status: "active",
367 phases: [],
368 currentPhase: undefined,
369 agentBudget: undefined,
370 agentsUsed: undefined,
371 elapsedMs: undefined,
372 activeAgents: undefined,
373 currentAgentLabel: undefined,
374 agents: [
375 {
376 agentId: "agent-a",
377 label: "Reviewer",
378 phase: undefined,
379 model: undefined,
380 state: "running",
381 tokensUsed: 10,
382 durationMs: 50,
383 },
384 ],
385 pauseMessage: undefined,
386 resultSummary: undefined,
387 });
388 expect(
389 second.events.filter(
390 (event) => event.payload.taskId === grokWorkflowMemberTaskId("run-1", "agent-a"),
391 ),
392 ).toHaveLength(0);
393 });
394
395 it("emits member progress when wire state changes but mapped status does not", () => {
396 const member = {
397 agentId: "agent-a",
398 label: "Reviewer",
399 phase: undefined as string | undefined,
400 model: undefined as string | undefined,
401 tokensUsed: 10,
402 durationMs: 50,
403 };
404 const first = applyGrokWorkflowUpdate(emptyGrokSubagentTrackState(), {
405 runId: "run-1",
406 revision: 1,
407 name: "review",
408 objective: "Review the PR",
409 status: "active",
410 phases: [],
411 currentPhase: undefined,
412 agentBudget: undefined,
413 agentsUsed: undefined,
414 elapsedMs: undefined,
415 activeAgents: undefined,
416 currentAgentLabel: undefined,
417 agents: [{ ...member, state: "start" }],
418 pauseMessage: undefined,
419 resultSummary: undefined,
420 });
421 const second = applyGrokWorkflowUpdate(first.state, {
422 runId: "run-1",
423 revision: 2,
424 name: "review",
425 objective: "Review the PR",
426 status: "active",
427 phases: [],
428 currentPhase: undefined,
429 agentBudget: undefined,
430 agentsUsed: undefined,
431 elapsedMs: undefined,
432 activeAgents: undefined,
433 currentAgentLabel: undefined,
434 agents: [{ ...member, state: "running" }],
435 pauseMessage: undefined,
436 resultSummary: undefined,
437 });
438 const memberProgress = second.events.find(
439 (event) =>
440 event.type === "task.progress" &&
441 event.payload.taskId === grokWorkflowMemberTaskId("run-1", "agent-a"),
442 );
443 expect(memberProgress?.payload).toMatchObject({
444 status: "running",
445 summary: "running",
446 });
447 });
448});