From cdfbb9ac643a741ca9c91f3d3833b2b0cdb29e04 Mon Sep 17 00:00:00 2001 From: T3 Code Test Date: Fri, 31 Jul 2026 04:45:27 +0530 Subject: [PATCH] fix(orchestration-v2): address subagent observability review findings - Treat pending/waiting workflow agents as active in the session-registry write guard and wake-buffer pre-open, so concurrent active transitions are not dropped and wake task_started cannot force a false resume. - Preserve the first-recorded activation completion time on duplicate terminal frames (Claude in-place update and ACP rebuilt emits). - Let a blocked/waiting first snapshot of a new attempt reopen a settled workflow agent entry instead of silently dropping its status and usage. - Settle still-active workflow members when their coordinator terminalizes, and expect the Implementer completed in claude_workflow. Co-Authored-By: Claude Fable 5 --- .../orchestration-v2/Adapters/AcpAdapterV2.ts | 4 +- .../Adapters/ClaudeAdapterV2.ts | 59 +++++++++++++++---- .../fixtures/claude_workflow/output.ts | 2 +- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts index fcb9128eb5..735f4c24dc 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts @@ -2353,7 +2353,9 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV status: taskStatus, usage: null, startedAt: subagent.task.startedAt, - completedAt: settled ? now : null, + // Keep the activation aligned with the subagent record instead + // of re-stamping the completion time on rebuilt emits. + completedAt: settled ? (subagent.task.completedAt ?? now) : null, updatedAt: now, } satisfies OrchestrationV2SubagentActivation, }); diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index a91689fba0..705e0e6f0f 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -3035,7 +3035,9 @@ export function makeClaudeAdapterV2( ...existingSubagent.activation, status: input.status, usage: mergeCumulativeSubagentUsage(existingSubagent.activation.usage, input.usage), - completedAt: settled ? now : null, + // A duplicate terminal frame must not re-stamp the completion + // time the first terminal frame recorded. + completedAt: settled ? (existingSubagent.activation.completedAt ?? now) : null, updatedAt: now, } satisfies OrchestrationV2SubagentActivation); task.currentActivationId = settled ? null : activation.id; @@ -3071,9 +3073,13 @@ export function makeClaudeAdapterV2( // re-open must not clobber its result. yield* Ref.update(sessionSubagentsByTaskId, (current) => { const registered = current.get(input.taskId); - if ( + const registeredSettled = registered !== undefined && - registered.task.status !== "running" && + (registered.task.status === "completed" || + registered.task.status === "failed" || + registered.task.status === "cancelled"); + if ( + registeredSettled && activeStart && !(isReopen && registered === existingSubagent) ) { @@ -3804,7 +3810,12 @@ export function makeClaudeAdapterV2( const now = yield* DateTime.now; yield* Ref.update(sessionSubagentsByTaskId, (current) => { const registered = current.get(message.task_id); - if (registered === undefined || registered.task.status === "running") { + if ( + registered === undefined || + registered.task.status === "running" || + registered.task.status === "pending" || + registered.task.status === "waiting" + ) { return current; } const { progress: _staleProgress, ...priorTask } = registered.task; @@ -4186,7 +4197,8 @@ export function makeClaudeAdapterV2( const existingWorker = context.subagentsByTaskId.get(workerTaskId) ?? (yield* Ref.get(sessionSubagentsByTaskId)).get(workerTaskId); - const activeWorker = status === "pending" || status === "running"; + const activeWorker = + status === "pending" || status === "running" || status === "waiting"; const reopen = activeWorker && existingWorker !== undefined && @@ -4236,19 +4248,44 @@ export function makeClaudeAdapterV2( }); if (!wasBackgroundTask && !context.ignoredTaskIds.has(message.task_id)) { const usage = claudeSubagentUsage(message.usage); + const terminalStatus = + message.status === "completed" + ? ("completed" as const) + : message.status === "stopped" + ? ("cancelled" as const) + : ("failed" as const); yield* updateClaudeSubagentNode({ context, taskId: message.task_id, ...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }), result: message.summary, ...(usage === undefined ? {} : { usage }), - status: - message.status === "completed" - ? "completed" - : message.status === "stopped" - ? "cancelled" - : "failed", + status: terminalStatus, }); + // Workflow members only ever settle through workflow snapshots; + // once the coordinator terminalizes, any member left active + // would stay "running" forever, so settle it alongside. + const workerPrefix = `${message.task_id}:workflow:`; + const registeredWorkers = new Map([ + ...(yield* Ref.get(sessionSubagentsByTaskId)), + ...context.subagentsByTaskId, + ]); + for (const [workerTaskId, worker] of registeredWorkers) { + if (!workerTaskId.startsWith(workerPrefix)) continue; + const workerStatus = worker.task.status; + if ( + workerStatus !== "pending" && + workerStatus !== "running" && + workerStatus !== "waiting" + ) { + continue; + } + yield* updateClaudeSubagentNode({ + context, + taskId: workerTaskId, + status: terminalStatus, + }); + } } // Replay tombstone only needs to outlive buffering until this // drained/live classification runs; drop it so it cannot leak. diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/claude_workflow/output.ts b/apps/server/src/orchestration-v2/testkit/fixtures/claude_workflow/output.ts index 064ad572a4..075454b375 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/claude_workflow/output.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/claude_workflow/output.ts @@ -45,7 +45,7 @@ export function assertClaudeWorkflowOutput( ]), [ ["Researcher", "completed", 0, "claude-sonnet-4-6", 400], - ["Implementer", "running", 1, "claude-opus-4-1", 500], + ["Implementer", "completed", 1, "claude-opus-4-1", 500], ], ); // Membership must point at the coordinator that actually reached the