Skip to content

Commit 468a4bf

Browse files
fix(grok): keep subagent liveness, tokens, and member progress honest
Subagent and workflow ACP ticks now stamp lastTurnActivityAtNanos before waking the watchdog, so a quiet parent stream is not cancelled while children are still working. Spawn-time token counts are kept in usageByTaskId so a later tools-or-duration tick cannot publish totalTokens 0. Workflow member fingerprints include the wire state so start-to-running still emits progress. Made with Grok 4.6.
1 parent 2c8a9e0 commit 468a4bf

3 files changed

Lines changed: 77 additions & 28 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,20 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
566566
return ctx && turnId !== undefined ? signalTurnLiveness(ctx, turnId) : Effect.void;
567567
};
568568

569+
const refreshGrokSessionTurnLiveness = (ctx: GrokSessionContext) =>
570+
Effect.gen(function* () {
571+
const turnId = resolveNotificationTurnId(ctx);
572+
if (turnId === undefined || ctx.livenessTurnId !== turnId) {
573+
return;
574+
}
575+
// Subagent/workflow ticks prove the provider is still working even
576+
// when the parent ACP stream is quiet. Stamp the watchdog clock
577+
// before signalling; a wake without a fresh timestamp cancels a
578+
// turn that has already sat near turnInactivityTimeoutMs.
579+
ctx.lastTurnActivityAtNanos = yield* Clock.monotonicTimeNanos;
580+
yield* signalTurnLiveness(ctx, turnId);
581+
});
582+
569583
const applyGrokSessionNotification = (
570584
ctx: GrokSessionContext,
571585
method: GrokSessionNotificationMethod,
@@ -582,7 +596,7 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
582596
payload: params,
583597
specs: applied.events,
584598
});
585-
yield* signalSessionTurnLiveness(ctx.threadId, resolveNotificationTurnId(ctx));
599+
yield* refreshGrokSessionTurnLiveness(ctx);
586600
return;
587601
}
588602
const subagent = parseXAiSubagentUpdate(params);
@@ -597,7 +611,7 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
597611
payload: params,
598612
specs: applied.events,
599613
});
600-
yield* signalSessionTurnLiveness(ctx.threadId, resolveNotificationTurnId(ctx));
614+
yield* refreshGrokSessionTurnLiveness(ctx);
601615
});
602616

603617
const resumeSessionTurnLiveness = Effect.fn("GrokAdapter.resumeSessionTurnLiveness")(function* (

‎apps/server/src/provider/acp/GrokAcpSubagents.test.ts‎

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ describe("applyGrokSubagentUpdate", () => {
207207
});
208208
});
209209

210-
it("keeps the last token count when a later tick only reports tools", () => {
210+
it("keeps spawn-time token count when a later tick only reports tools", () => {
211211
const spawned = applyGrokSubagentUpdate(emptyGrokSubagentTrackState(), {
212212
kind: "spawned",
213213
subagentId: "child-1",
@@ -224,23 +224,7 @@ describe("applyGrokSubagentUpdate", () => {
224224
lastToolName: undefined,
225225
output: undefined,
226226
});
227-
const progressed = 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: 50,
237-
durationMs: undefined,
238-
turnCount: undefined,
239-
toolCallCount: undefined,
240-
lastToolName: undefined,
241-
output: undefined,
242-
});
243-
const toolOnly = applyGrokSubagentUpdate(progressed.state, {
227+
const toolOnly = applyGrokSubagentUpdate(spawned.state, {
244228
kind: "progress",
245229
subagentId: "child-1",
246230
childSessionId: undefined,
@@ -407,4 +391,58 @@ describe("applyGrokWorkflowUpdate", () => {
407391
),
408392
).toHaveLength(0);
409393
});
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+
});
410448
});

‎apps/server/src/provider/acp/GrokAcpSubagents.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ export function grokWorkflowMemberTaskId(runId: string, agentId: string): string
325325
function memberFingerprint(agent: GrokWorkflowAgent, status: RuntimeTaskStatus): string {
326326
return [
327327
status,
328+
agent.state,
328329
agent.label,
329330
agent.model ?? "",
330331
agent.phase ?? "",
@@ -526,6 +527,10 @@ export function applyGrokSubagentUpdate(
526527
const usageByTaskId = new Map(state.usageByTaskId);
527528
const events: Array<GrokTaskEventSpec> = [];
528529
const title = subagentTitle(update);
530+
const typedUsage = mergeTypedUsageFromCounts(update, usageByTaskId.get(update.subagentId));
531+
if (typedUsage) {
532+
usageByTaskId.set(update.subagentId, typedUsage);
533+
}
529534
const linkage = {
530535
taskId: update.subagentId,
531536
description: title,
@@ -546,10 +551,6 @@ export function applyGrokSubagentUpdate(
546551
events.push({ type: "task.started", payload: linkage });
547552
}
548553
if (!completedSubagentIds.has(update.subagentId)) {
549-
const typedUsage = mergeTypedUsageFromCounts(update, usageByTaskId.get(update.subagentId));
550-
if (typedUsage) {
551-
usageByTaskId.set(update.subagentId, typedUsage);
552-
}
553554
events.push({
554555
type: "task.progress",
555556
payload: {
@@ -568,10 +569,6 @@ export function applyGrokSubagentUpdate(
568569
}
569570
completedSubagentIds.add(update.subagentId);
570571
const finished = update.status ?? "completed";
571-
const typedUsage = mergeTypedUsageFromCounts(update, usageByTaskId.get(update.subagentId));
572-
if (typedUsage) {
573-
usageByTaskId.set(update.subagentId, typedUsage);
574-
}
575572
events.push({
576573
type: "task.completed",
577574
payload: {

0 commit comments

Comments
 (0)