fix(orchestration-v2): preserve subagent progress state

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Shivam SharmaandClaude Fable 5 authored and T3 Code Test committed 2026-08-14 15:04:11 +05:30
1 parent cdfbb9ac64
commit b819de73af
5 files changed
+71 -15

No files matched your search

@@ -3275,11 +3275,11 @@ export function makeClaudeAdapterV2(
status: input.status,
title: "Subagent progress",
startedAt: progressStartedAt,
completedAt: input.status === "running" ? null : now,
completedAt: settled ? now : null,
updatedAt: now,
type: "reasoning",
text: progress,
streaming: input.status === "running",
streaming: !settled,
},
});
}
@@ -3577,6 +3577,21 @@ describe("CodexAdapterV2 post-settle continuation", () => {
assert.equal(firstCompletion?.subagent.activationCount, 1);
assert.equal(firstCompletion?.subagent.usage?.totalTokens, 100);
assert.isNull(firstCompletion?.subagent.currentActivationId);
const firstActivationUpdates = harness
.subagentActivationUpdates()
.filter((event) => event.activation.ordinal === 1);
const firstActivation = firstActivationUpdates.at(-1)?.activation;
assert.equal(firstActivation?.ordinal, 1);
assert.equal(firstActivation?.status, "completed");
assert.equal(firstActivation?.usage?.totalTokens, 100);
assert.equal(
firstActivation?.completedAt,
firstActivationUpdates.at(-2)?.activation.completedAt,
);
assert.equal(
firstActivation?.updatedAt,
firstActivationUpdates.at(-2)?.activation.updatedAt,
);
assert.lengthOf(
new Set(
settledUpdates
@@ -3629,6 +3644,15 @@ describe("CodexAdapterV2 post-settle continuation", () => {
[RESUME_CHILD_TURN_1, RESUME_CHILD_TURN_2],
);
assert.equal(activations.at(-1)?.activation.status, "completed");
assert.equal(activations.at(-1)?.activation.usage?.totalTokens, 80);
assert.equal(
activations.at(-1)?.activation.completedAt,
activations.at(-2)?.activation.completedAt,
);
assert.equal(
activations.at(-1)?.activation.updatedAt,
activations.at(-2)?.activation.updatedAt,
);
assert.isFalse(yield* harness.hasPendingBackgroundWork);
assert.lengthOf(harness.terminalEvents(), 1);
assert.lengthOf(harness.continuationRequests, 0);
@@ -1517,6 +1517,9 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
const pendingRootTurns = yield* Ref.make(new Map<string, ProviderAdapterV2TurnInput>());
const turnWaiters = yield* Ref.make(new Map<string, Deferred.Deferred<void, never>>());
const subagentThreads = yield* Ref.make(new Map<string, CodexSubagentThreadContext>());
const subagentActivationsByNativeTurnId = yield* Ref.make(
new Map<string, OrchestrationV2SubagentActivation>(),
);
const pendingSubagentTurns = yield* Ref.make(
new Map<string, ReadonlyArray<PendingCodexSubagentTurnStarted>>(),
);
@@ -1958,6 +1961,9 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
updatedAt: turn.startedAt,
} satisfies OrchestrationV2SubagentActivation;
activeContext.subagentActivation = activation;
yield* Ref.update(subagentActivationsByNativeTurnId, (current) =>
new Map(current).set(turn.nativeTurnId, activation),
);
yield* Ref.update(activeTurns, (current) => {
const updated = new Map(current);
updated.set(turn.nativeTurnId, activeContext);
@@ -3380,30 +3386,36 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
});
const activeContext = (yield* Ref.get(activeTurns)).get(payload.turnId);
if (
activeContext === undefined ||
activeContext.subagent !== subagent ||
activeContext.subagentActivation === null
) {
const activation =
activeContext?.subagent === subagent
? activeContext.subagentActivation
: ((yield* Ref.get(subagentActivationsByNativeTurnId)).get(payload.turnId) ?? null);
if (activation === null) {
return;
}
const last = payload.tokenUsage.last;
const activation = {
...activeContext.subagentActivation,
usage: {
const now = yield* DateTime.now;
const updatedActivation = {
...activation,
usage: mergeCumulativeSubagentUsage(activation.usage, {
totalTokens: last.totalTokens,
inputTokens: last.inputTokens,
cachedInputTokens: last.cachedInputTokens,
outputTokens: last.outputTokens,
reasoningOutputTokens: last.reasoningOutputTokens,
},
updatedAt: yield* DateTime.now,
}),
updatedAt: activation.completedAt === null ? now : activation.updatedAt,
} satisfies OrchestrationV2SubagentActivation;
activeContext.subagentActivation = activation;
if (activeContext?.subagent === subagent) {
activeContext.subagentActivation = updatedActivation;
}
yield* Ref.update(subagentActivationsByNativeTurnId, (current) =>
new Map(current).set(payload.turnId, updatedActivation),
);
yield* emitProviderEvent({
type: "subagent_activation.updated",
driver: CODEX_PROVIDER,
activation,
activation: updatedActivation,
});
}).pipe(Effect.orDie),
);
@@ -3435,6 +3447,9 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
updatedAt: now,
} satisfies OrchestrationV2SubagentActivation;
activeContext.subagentActivation = activation;
yield* Ref.update(subagentActivationsByNativeTurnId, (current) =>
new Map(current).set(activeContext.nativeTurnId, activation),
);
yield* emitProviderEvent({
type: "subagent_activation.updated",
driver: CODEX_PROVIDER,
@@ -4392,6 +4407,9 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
updatedAt: input.completedAt,
} satisfies OrchestrationV2SubagentActivation;
input.context.subagentActivation = activation;
yield* Ref.update(subagentActivationsByNativeTurnId, (current) =>
new Map(current).set(input.nativeTurnId, activation),
);
yield* emitProviderEvent({
type: "subagent_activation.updated",
driver: CODEX_PROVIDER,
@@ -4,7 +4,7 @@
{"type": "emit_inbound", "label": "system", "frame": {"type": "system", "subtype": "init", "agents": [], "apiKeySource": "none", "claude_code_version": "2.1.183", "cwd": "/tmp/claude-replay-claude_workflow", "tools": [], "mcp_servers": [], "model": "claude-sonnet-4-6", "permissionMode": "bypassPermissions", "slash_commands": [], "output_style": "default", "skills": [], "plugins": [], "fast_mode_state": "off", "uuid": "af3bf4cc-1385-4e19-a5fa-fcbe2a0ca201", "session_id": "3c7f21a0-9d4e-4b21-8f60-000000000201"}}
{"type": "emit_inbound", "label": "assistant:commentary", "frame": {"type": "assistant", "message": {"model": "claude-sonnet-4-6", "id": "msg_01claudeworkflow1", "type": "message", "role": "assistant", "content": [{"type": "text", "text": "Starting the two-phase workflow."}], "stop_reason": null, "stop_sequence": null, "stop_details": null, "usage": {"input_tokens": 1, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0, "output_tokens": 1, "service_tier": "standard", "inference_geo": "not_available"}, "context_management": null}, "parent_tool_use_id": null, "session_id": "3c7f21a0-9d4e-4b21-8f60-000000000201", "uuid": "97898e28-ee4e-4fcf-80c3-5c790c502201"}}
{"type": "emit_inbound", "label": "task_started:local_workflow", "frame": {"type": "system", "subtype": "task_started", "task_id": "wf7ka2mq1", "tool_use_id": "toolu_01ClaudeWorkflowFixture01", "description": "Research and implement", "task_type": "local_workflow", "prompt": "Research the parser, then implement the fix.", "uuid": "4873c95b-1aa9-44a5-9783-dc56dac73201", "session_id": "3c7f21a0-9d4e-4b21-8f60-000000000201"}}
{"type": "emit_inbound", "label": "task_progress:phase-1", "frame": {"type": "system", "subtype": "task_progress", "task_id": "wf7ka2mq1", "tool_use_id": "toolu_01ClaudeWorkflowFixture01", "description": "Research phase running", "usage": {"input_tokens": 300, "output_tokens": 200, "total_tokens": 500, "tool_uses": 4}, "workflow_progress": [{"type": "workflow_phase", "index": 0, "title": "Research"}, {"type": "workflow_phase", "index": 1, "title": "Implement"}, {"type": "workflow_agent", "index": 0, "label": "Researcher", "state": "active", "phaseIndex": 0, "attempt": 0, "model": "claude-sonnet-4-6", "lastToolName": "Read", "tokens": 200, "toolCalls": 2}], "uuid": "4873c95b-1aa9-44a5-9783-dc56dac73202", "session_id": "3c7f21a0-9d4e-4b21-8f60-000000000201"}}
{"type": "emit_inbound", "label": "task_progress:phase-1", "frame": {"type": "system", "subtype": "task_progress", "task_id": "wf7ka2mq1", "tool_use_id": "toolu_01ClaudeWorkflowFixture01", "description": "Research phase running", "usage": {"input_tokens": 300, "output_tokens": 200, "total_tokens": 500, "tool_uses": 4}, "workflow_progress": [{"type": "workflow_phase", "index": 0, "title": "Research"}, {"type": "workflow_phase", "index": 1, "title": "Implement"}, {"type": "workflow_agent", "index": 0, "label": "Researcher", "state": "waiting", "phaseIndex": 0, "attempt": 0, "model": "claude-sonnet-4-6", "lastToolName": "Read", "tokens": 200, "toolCalls": 2}], "uuid": "4873c95b-1aa9-44a5-9783-dc56dac73202", "session_id": "3c7f21a0-9d4e-4b21-8f60-000000000201"}}
{"type": "emit_inbound", "label": "task_progress:phase-2", "frame": {"type": "system", "subtype": "task_progress", "task_id": "wf7ka2mq1", "tool_use_id": "toolu_01ClaudeWorkflowFixture01", "description": "Implement phase running", "usage": {"input_tokens": 700, "output_tokens": 500, "total_tokens": 1200, "tool_uses": 9}, "workflow_progress": [{"type": "workflow_phase", "index": 0, "title": "Research"}, {"type": "workflow_phase", "index": 1, "title": "Implement"}, {"type": "workflow_agent", "index": 0, "label": "Researcher", "state": "done", "phaseIndex": 0, "attempt": 0, "model": "claude-sonnet-4-6", "lastToolName": "Read", "tokens": 400, "toolCalls": 3}, {"type": "workflow_agent", "index": 1, "label": "Implementer", "state": "active", "phase_index": 1, "attempt": 0, "model": "claude-opus-4-1", "last_tool_name": "Edit", "tokens": 500, "tool_calls": 4}], "uuid": "4873c95b-1aa9-44a5-9783-dc56dac73203", "session_id": "3c7f21a0-9d4e-4b21-8f60-000000000201"}}
{"type": "emit_inbound", "label": "task_notification:local_workflow", "frame": {"type": "system", "subtype": "task_notification", "task_id": "wf7ka2mq1", "tool_use_id": "toolu_01ClaudeWorkflowFixture01", "status": "completed", "output_file": "", "summary": "Workflow finished both phases.", "usage": {"input_tokens": 900, "output_tokens": 600, "total_tokens": 1500, "tool_uses": 11}, "uuid": "4873c95b-1aa9-44a5-9783-dc56dac73204", "session_id": "3c7f21a0-9d4e-4b21-8f60-000000000201"}}
{"type": "emit_inbound", "label": "assistant:commentary", "frame": {"type": "assistant", "message": {"model": "claude-sonnet-4-6", "id": "msg_01claudeworkflow2", "type": "message", "role": "assistant", "content": [{"type": "text", "text": "claude workflow fixture complete"}], "stop_reason": null, "stop_sequence": null, "stop_details": null, "usage": {"input_tokens": 1, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0, "output_tokens": 1, "service_tier": "standard", "inference_geo": "not_available"}, "context_management": null}, "parent_tool_use_id": null, "session_id": "3c7f21a0-9d4e-4b21-8f60-000000000201", "uuid": "89be9e65-097b-47e8-849b-be723386b201"}}
@@ -72,6 +72,20 @@ export function assertClaudeWorkflowOutput(
0,
"a member without membership would render outside its workflow",
);
const waitingProgressItem = result.domainEvents.find(
(event) =>
event.type === "turn-item.updated" &&
event.payload.type === "reasoning" &&
event.payload.text === "Read" &&
event.payload.status === "waiting",
);
assert.isDefined(waitingProgressItem);
if (waitingProgressItem?.type === "turn-item.updated") {
assert.isNull(waitingProgressItem.payload.completedAt);
assert.isTrue(
waitingProgressItem.payload.type === "reasoning" && waitingProgressItem.payload.streaming,
);
}
// The coordinator's usage already covers its members, so the panel subtracts
// theirs from it. That only stays non-negative if the provider's totals are
// carried through unmodified.