Skip to content

Commit 2c8a9e0

Browse files
feat(grok): show Grok subagents in the Agents panel
Claude Task and Codex collab children already emit task.* events that the right-hand Agents panel folds into a live roster. Grok Build streams the same work over x.ai session notifications, but T3 dropped them, so Grok threads never listed child agents. Map subagent_spawned/progress/finished and workflow_updated onto the existing task.started/progress/completed path, with Claude/Codex linkage (parentAgentId + timelineBypass for workflow members; standalone agent() rows stay direct subagents). Made with Grok 4.6.
1 parent f6f2be3 commit 2c8a9e0

6 files changed

Lines changed: 1243 additions & 0 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const emitInterleavedAssistantToolCalls =
1919
const emitGenericToolPlaceholders = process.env.T3_ACP_EMIT_GENERIC_TOOL_PLACEHOLDERS === "1";
2020
const emitAskQuestion = process.env.T3_ACP_EMIT_ASK_QUESTION === "1";
2121
const emitXAiAskUserQuestion = process.env.T3_ACP_EMIT_XAI_ASK_USER_QUESTION === "1";
22+
const emitXAiSubagent = process.env.T3_ACP_EMIT_XAI_SUBAGENT === "1";
2223
const emitXAiExitPlanMode = process.env.T3_ACP_EMIT_XAI_EXIT_PLAN_MODE === "1";
2324
const emitXAiPlanMdWrite = process.env.T3_ACP_EMIT_XAI_PLAN_MD_WRITE === "1";
2425
const emitXAiPromptCompleteThenHang = process.env.T3_ACP_EMIT_XAI_PROMPT_COMPLETE_THEN_HANG === "1";
@@ -543,6 +544,44 @@ const program = Effect.gen(function* () {
543544
return yield* Effect.never;
544545
}
545546

547+
if (emitXAiSubagent) {
548+
writeJsonRpcNotification("_x.ai/session/update", {
549+
sessionId: requestedSessionId,
550+
update: {
551+
sessionUpdate: "subagent_spawned",
552+
subagent_id: "grok-subagent-1",
553+
subagent_type: "explore",
554+
description: "Search the codebase",
555+
model: "grok-4.6",
556+
},
557+
});
558+
writeJsonRpcNotification("_x.ai/session/update", {
559+
sessionId: requestedSessionId,
560+
update: {
561+
sessionUpdate: "subagent_progress",
562+
subagent_id: "grok-subagent-1",
563+
subagent_type: "explore",
564+
description: "Search the codebase",
565+
status: "running",
566+
last_tool_name: "grep",
567+
tokens_used: 80,
568+
},
569+
});
570+
writeJsonRpcNotification("_x.ai/session/update", {
571+
sessionId: requestedSessionId,
572+
update: {
573+
sessionUpdate: "subagent_finished",
574+
subagent_id: "grok-subagent-1",
575+
subagent_type: "explore",
576+
description: "Search the codebase",
577+
status: "completed",
578+
output: "Found 3 call sites.",
579+
tokens_used: 120,
580+
duration_ms: 400,
581+
},
582+
});
583+
}
584+
546585
if (emitXAiRateLimitThenHang) {
547586
writeJsonRpcNotification("_x.ai/session/prompt_complete", {
548587
sessionId: requestedSessionId,

‎apps/server/src/provider/Layers/GrokAdapter.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,4 +2085,57 @@ it.layer(grokAdapterTestLayer)("GrokAdapterLive", (it) => {
20852085
// hang until the suite timeout instead of failing here.
20862086
}).pipe(TestClock.withLive),
20872087
);
2088+
2089+
it.effect("maps Grok ACP subagent notifications onto the Agents panel task stream", () =>
2090+
Effect.gen(function* () {
2091+
const threadId = ThreadId.make("grok-subagent-panel");
2092+
const wrapperPath = yield* Effect.promise(() =>
2093+
makeMockGrokWrapper({ T3_ACP_EMIT_XAI_SUBAGENT: "1" }),
2094+
);
2095+
const adapter = yield* makeTestAdapter(wrapperPath);
2096+
const started =
2097+
yield* Deferred.make<Extract<ProviderRuntimeEvent, { type: "task.started" }>>();
2098+
const completed =
2099+
yield* Deferred.make<Extract<ProviderRuntimeEvent, { type: "task.completed" }>>();
2100+
const eventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) => {
2101+
if (String(event.threadId) !== String(threadId)) {
2102+
return Effect.void;
2103+
}
2104+
if (event.type === "task.started") {
2105+
return Deferred.succeed(started, event).pipe(Effect.ignore);
2106+
}
2107+
if (event.type === "task.completed") {
2108+
return Deferred.succeed(completed, event).pipe(Effect.ignore);
2109+
}
2110+
return Effect.void;
2111+
}).pipe(Effect.forkChild);
2112+
2113+
yield* adapter.startSession({
2114+
threadId,
2115+
provider: ProviderDriverKind.make("grok"),
2116+
cwd: process.cwd(),
2117+
runtimeMode: "full-access",
2118+
});
2119+
yield* adapter.sendTurn({
2120+
threadId,
2121+
input: "delegate the search",
2122+
attachments: [],
2123+
});
2124+
2125+
const startedEvent = yield* Deferred.await(started);
2126+
const completedEvent = yield* Deferred.await(completed);
2127+
assert.equal(startedEvent.payload.taskId, "grok-subagent-1");
2128+
assert.equal(startedEvent.payload.taskType, "subagent");
2129+
assert.equal(startedEvent.payload.role, "explore");
2130+
assert.equal(startedEvent.payload.title, "Search the codebase");
2131+
assert.equal(startedEvent.payload.timelineBypass, true);
2132+
assert.equal(startedEvent.raw?.method, "_x.ai/session/update");
2133+
assert.equal(completedEvent.payload.taskId, "grok-subagent-1");
2134+
assert.equal(completedEvent.payload.status, "completed");
2135+
assert.equal(completedEvent.payload.summary, "Found 3 call sites.");
2136+
2137+
yield* Fiber.interrupt(eventsFiber);
2138+
yield* adapter.stopSession(threadId);
2139+
}),
2140+
);
20882141
});

‎apps/server/src/provider/Layers/GrokAdapter.ts‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ProviderDriverKind,
1010
ProviderInstanceId,
1111
RuntimeRequestId,
12+
RuntimeTaskId,
1213
type ThreadId,
1314
TurnId,
1415
} from "@t3tools/contracts";
@@ -67,6 +68,18 @@ import {
6768
normalizeGrokReasoningEffort,
6869
resolveGrokAcpBaseModelId,
6970
} from "../acp/GrokAcpSupport.ts";
71+
import {
72+
applyGrokSubagentUpdate,
73+
applyGrokWorkflowUpdate,
74+
emptyGrokSubagentTrackState,
75+
GROK_SESSION_NOTIFICATION_METHODS,
76+
parseXAiSubagentUpdate,
77+
parseXAiWorkflowUpdated,
78+
XAiSessionNotification,
79+
type GrokSessionNotificationMethod,
80+
type GrokSubagentTrackState,
81+
type GrokTaskEventSpec,
82+
} from "../acp/GrokAcpSubagents.ts";
7083
import {
7184
extractGrokPlanMarkdownFromToolCallData,
7285
extractXAiAskUserQuestions,
@@ -162,6 +175,14 @@ interface GrokSessionContext {
162175
promptResponsesReady: number;
163176
currentModelId: string | undefined;
164177
currentReasoningEffort: string | undefined;
178+
/**
179+
* Grok `agent()` / workflow ACP extras mapped onto task.* so the Agents
180+
* panel can list them the same way Claude Task and Codex collab children
181+
* already do.
182+
*/
183+
workflowTrack: GrokSubagentTrackState;
184+
/** First-seen spawn turn per task, reused after the parent turn settles. */
185+
readonly taskSpawnTurnIds: Map<string, TurnId>;
165186
stopped: boolean;
166187
}
167188

@@ -399,6 +420,45 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
399420
const offerRuntimeEvent = (event: ProviderRuntimeEvent) =>
400421
PubSub.publish(runtimeEventPubSub, event).pipe(Effect.asVoid);
401422

423+
const emitGrokTaskSpecs = (input: {
424+
readonly ctx: GrokSessionContext;
425+
readonly method: string;
426+
readonly payload: unknown;
427+
readonly specs: ReadonlyArray<GrokTaskEventSpec>;
428+
}) =>
429+
Effect.forEach(
430+
input.specs,
431+
(spec) =>
432+
Effect.gen(function* () {
433+
const taskIdValue = spec.payload.taskId;
434+
if (typeof taskIdValue !== "string" || taskIdValue.length === 0) {
435+
return;
436+
}
437+
const taskId = RuntimeTaskId.make(taskIdValue);
438+
let turnId = input.ctx.taskSpawnTurnIds.get(taskIdValue);
439+
if (turnId === undefined) {
440+
turnId = resolveNotificationTurnId(input.ctx);
441+
if (turnId !== undefined) {
442+
input.ctx.taskSpawnTurnIds.set(taskIdValue, turnId);
443+
}
444+
}
445+
yield* offerRuntimeEvent({
446+
type: spec.type,
447+
...(yield* makeEventStamp()),
448+
provider: PROVIDER,
449+
threadId: input.ctx.threadId,
450+
...(turnId !== undefined ? { turnId } : {}),
451+
payload: { ...spec.payload, taskId },
452+
raw: {
453+
source: "acp.grok.extension",
454+
method: input.method,
455+
payload: input.payload,
456+
},
457+
} as ProviderRuntimeEvent);
458+
}),
459+
{ discard: true },
460+
);
461+
402462
const getThreadSemaphore = (threadId: string) =>
403463
SynchronizedRef.modifyEffect(threadLocksRef, (current) => {
404464
const existing: Option.Option<Semaphore.Semaphore> = Option.fromNullishOr(
@@ -506,6 +566,40 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
506566
return ctx && turnId !== undefined ? signalTurnLiveness(ctx, turnId) : Effect.void;
507567
};
508568

569+
const applyGrokSessionNotification = (
570+
ctx: GrokSessionContext,
571+
method: GrokSessionNotificationMethod,
572+
params: unknown,
573+
) =>
574+
Effect.gen(function* () {
575+
const workflow = parseXAiWorkflowUpdated(params);
576+
if (workflow) {
577+
const applied = applyGrokWorkflowUpdate(ctx.workflowTrack, workflow);
578+
ctx.workflowTrack = applied.state;
579+
yield* emitGrokTaskSpecs({
580+
ctx,
581+
method,
582+
payload: params,
583+
specs: applied.events,
584+
});
585+
yield* signalSessionTurnLiveness(ctx.threadId, resolveNotificationTurnId(ctx));
586+
return;
587+
}
588+
const subagent = parseXAiSubagentUpdate(params);
589+
if (!subagent) {
590+
return;
591+
}
592+
const applied = applyGrokSubagentUpdate(ctx.workflowTrack, subagent);
593+
ctx.workflowTrack = applied.state;
594+
yield* emitGrokTaskSpecs({
595+
ctx,
596+
method,
597+
payload: params,
598+
specs: applied.events,
599+
});
600+
yield* signalSessionTurnLiveness(ctx.threadId, resolveNotificationTurnId(ctx));
601+
});
602+
509603
const resumeSessionTurnLiveness = Effect.fn("GrokAdapter.resumeSessionTurnLiveness")(function* (
510604
threadId: ThreadId,
511605
turnId: TurnId | undefined,
@@ -1018,6 +1112,12 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
10181112
}),
10191113
),
10201114
);
1115+
const pendingSessionNotifications: Array<{
1116+
readonly method: GrokSessionNotificationMethod;
1117+
readonly params: unknown;
1118+
}> = [];
1119+
let sessionNotificationsReady = false;
1120+
const sessionNotificationLock = yield* Semaphore.make(1);
10211121
const started = yield* Effect.gen(function* () {
10221122
yield* Effect.forEach(
10231123
["x.ai/ask_user_question", "_x.ai/ask_user_question"] as const,
@@ -1121,6 +1221,34 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
11211221
),
11221222
{ discard: true },
11231223
);
1224+
// Grok Build streams agent() / workflow runs over a private ACP
1225+
// channel. Claude maps Task/workflow_progress onto task.*; Codex
1226+
// maps collabAgent/* the same way. Keep that seam: parse here,
1227+
// emit canonical events, never a third Agents UI.
1228+
yield* Effect.forEach(
1229+
GROK_SESSION_NOTIFICATION_METHODS,
1230+
(method) =>
1231+
acp.handleExtNotification(method, XAiSessionNotification, (params) =>
1232+
mapAcpCallbackFailure(
1233+
Effect.gen(function* () {
1234+
yield* logNative(input.threadId, method, params);
1235+
if (!sessionNotificationsReady) {
1236+
pendingSessionNotifications.push({ method, params });
1237+
return;
1238+
}
1239+
const liveCtx = sessions.get(input.threadId);
1240+
if (!liveCtx) {
1241+
pendingSessionNotifications.push({ method, params });
1242+
return;
1243+
}
1244+
yield* sessionNotificationLock.withPermit(
1245+
applyGrokSessionNotification(liveCtx, method, params),
1246+
);
1247+
}),
1248+
),
1249+
),
1250+
{ discard: true },
1251+
);
11241252
yield* acp.handleRequestPermission((params) =>
11251253
mapAcpCallbackFailure(
11261254
Effect.gen(function* () {
@@ -1290,6 +1418,8 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
12901418
requestedStartReasoningEffort !== undefined
12911419
? normalizeGrokReasoningEffort(requestedStartReasoningEffort)
12921420
: currentStartReasoningEffort,
1421+
workflowTrack: emptyGrokSubagentTrackState(),
1422+
taskSpawnTurnIds: new Map(),
12931423
stopped: false,
12941424
};
12951425

@@ -1433,6 +1563,15 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
14331563
sessions.set(input.threadId, ctx);
14341564
yield* runTurnLivenessWatchdog(ctx).pipe(Effect.forkIn(ctx.scope), Effect.asVoid);
14351565
sessionScopeTransferred = true;
1566+
yield* sessionNotificationLock.withPermit(
1567+
Effect.gen(function* () {
1568+
const batch = pendingSessionNotifications.splice(0);
1569+
sessionNotificationsReady = true;
1570+
yield* Effect.forEach(batch, (pending) =>
1571+
applyGrokSessionNotification(ctx, pending.method, pending.params),
1572+
);
1573+
}),
1574+
);
14361575

14371576
yield* offerRuntimeEvent({
14381577
type: "session.started",

0 commit comments

Comments
 (0)