TypeScript
fix(server): take a delegated task's result from its newest turn item (#19058)
hinescreative48902dabe28d
diff --git a/apps/server/src/orchestration-v2/SubagentProjection.test.ts b/apps/server/src/orchestration-v2/SubagentProjection.test.tsindex 65f3b917fca..7524f250ba5 100644--- a/apps/server/src/orchestration-v2/SubagentProjection.test.ts+++ b/apps/server/src/orchestration-v2/SubagentProjection.test.ts@@ -316,6 +316,80 @@ it("exposes the provider failure rather than a progress message from the failedassert.isNull(result.messageId);});+it("reports the last assistant message when the provider finalizes them together", () => {+ const { projection, run } = taskFixture();+ // OpenCode stamps every assistant message of a turn with the turn's end time.+ const finalizedAt = DateTime.makeUnsafe("2026-07-24T10:05:00.000Z");+ const message = (id: string, text: string, ordinal: number, createdAt: string) => {+ const artifacts = makeSubagentConversationArtifacts({+ messageId: MessageId.make(id),+ turnItemId: TurnItemId.make(id),+ threadId: parentThreadId,+ rootNodeId: NodeId.make("root"),+ providerThreadId: null,+ providerTurnId: null,+ nativeItemRef: null,+ role: "assistant",+ text,+ ordinal,+ now: DateTime.makeUnsafe(createdAt),+ });+ return {+ message: { ...artifacts.message, runId: run.id, updatedAt: finalizedAt },+ turnItem: { ...artifacts.turnItem, runId: run.id },+ };+ };+ const progress = message("progress", "Starting step 0.", 1, "2026-07-24T10:00:00.000Z");+ const report = message("report", "All steps passed.", 2, "2026-07-24T10:04:00.000Z");+ const result = subagentResultForRun(+ {+ ...projection,+ messages: [progress.message, report.message],+ turnItems: [progress.turnItem, report.turnItem],+ },+ run,+ );+ assert.equal(result.text, "All steps passed.");+ assert.equal(result.messageId, report.message.id);+});++it("orders a task's assistant messages by turn item, not by timestamps", () => {+ const { projection, run } = taskFixture();+ const at = DateTime.makeUnsafe("2026-07-24T10:05:00.000Z");+ const message = (id: string, text: string, ordinal: number) => {+ const artifacts = makeSubagentConversationArtifacts({+ messageId: MessageId.make(id),+ turnItemId: TurnItemId.make(id),+ threadId: parentThreadId,+ rootNodeId: NodeId.make("root"),+ providerThreadId: null,+ providerTurnId: null,+ nativeItemRef: null,+ role: "assistant",+ text,+ ordinal,+ now: at,+ });+ return {+ message: { ...artifacts.message, runId: run.id },+ turnItem: { ...artifacts.turnItem, runId: run.id },+ };+ };+ const progress = message("progress", "Starting step 0.", 1);+ const report = message("report", "All steps passed.", 2);+ const result = subagentResultForRun(+ {+ ...projection,+ messages: [progress.message, report.message],+ turnItems: [report.turnItem, progress.turnItem],+ },+ run,+ );+ assert.equal(result.text, "All steps passed.");+ assert.equal(result.messageId, report.message.id);+ assert.equal(result.turnItemId, report.turnItem.id);+});+it.effect("ends an orphaned native subagent's node when only its record already ended", () =>Effect.gen(function* () {const id = NodeId.make("node:orphaned-native-subagent");diff --git a/packages/provider-core/src/server/subagentProjection.ts b/packages/provider-core/src/server/subagentProjection.tsindex 0212eb766f5..cd46d5b0301 100644--- a/packages/provider-core/src/server/subagentProjection.ts+++ b/packages/provider-core/src/server/subagentProjection.ts@@ -172,18 +172,6 @@ export function subagentResultForRun(readonly messageId: OrchestrationV2ConversationMessage["id"] | null;readonly turnItemId: OrchestrationV2TurnItem["id"] | null;} {- const message =- projection.messages- .filter(- (candidate) =>- candidate.runId === run.id &&- candidate.role === "assistant" &&- candidate.text.trim().length > 0,- )- .toSorted(- (left, right) =>- DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),- )[0] ?? null;const turnItem =projection.turnItems.filter(@@ -195,6 +183,22 @@ export function subagentResultForRun(candidate.text.trim().length > 0,).toSorted((left, right) => right.ordinal - left.ordinal)[0] ?? null;+ const messages = projection.messages.filter(+ (candidate) =>+ candidate.runId === run.id &&+ candidate.role === "assistant" &&+ candidate.text.trim().length > 0,+ );+ // Turn item order is authoritative: providers can finalize every message of a turn at once,+ // so message timestamps can tie. They only order messages that have no turn item.+ const message =+ messages.find((candidate) => candidate.id === turnItem?.messageId) ??+ messages.toSorted(+ (left, right) =>+ DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt) ||+ DateTime.toEpochMillis(right.createdAt) - DateTime.toEpochMillis(left.createdAt),+ )[0] ??+ null;const failure =run.status === "failed"? projection.turnItems