The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 04:15 UTC
fix(server): take a delegated task's result from its newest turn item (#19058)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
hinescreative committed
Oct 18, 2026 at 14:29 UTC
48902dabe28de56a8b4ffcf657497957c484eb6d
2 files changed
+90
-12
apps/server/src/orchestration-v2/SubagentProjection.test.ts
+74
@@ -316,6 +316,80 @@ it("exposes the provider failure rather than a progress message from the failed
316
assert.isNull(result.messageId);
317
});
318
319
+it("reports the last assistant message when the provider finalizes them together", () => {
320
+ const { projection, run } = taskFixture();
321
+ // OpenCode stamps every assistant message of a turn with the turn's end time.
322
+ const finalizedAt = DateTime.makeUnsafe("2026-07-24T10:05:00.000Z");
323
+ const message = (id: string, text: string, ordinal: number, createdAt: string) => {
324
+ const artifacts = makeSubagentConversationArtifacts({
325
+ messageId: MessageId.make(id),
326
+ turnItemId: TurnItemId.make(id),
327
+ threadId: parentThreadId,
328
+ rootNodeId: NodeId.make("root"),
329
+ providerThreadId: null,
330
+ providerTurnId: null,
331
+ nativeItemRef: null,
332
+ role: "assistant",
333
+ text,
334
+ ordinal,
335
+ now: DateTime.makeUnsafe(createdAt),
336
+ });
337
+ return {
338
+ message: { ...artifacts.message, runId: run.id, updatedAt: finalizedAt },
339
+ turnItem: { ...artifacts.turnItem, runId: run.id },
340
+ };
341
+ };
342
+ const progress = message("progress", "Starting step 0.", 1, "2026-07-24T10:00:00.000Z");
343
+ const report = message("report", "All steps passed.", 2, "2026-07-24T10:04:00.000Z");
344
+ const result = subagentResultForRun(
345
+ {
346
+ ...projection,
347
+ messages: [progress.message, report.message],
348
+ turnItems: [progress.turnItem, report.turnItem],
349
+ },
350
+ run,
351
+ );
352
+ assert.equal(result.text, "All steps passed.");
353
+ assert.equal(result.messageId, report.message.id);
354
+});
355
+
356
+it("orders a task's assistant messages by turn item, not by timestamps", () => {
357
+ const { projection, run } = taskFixture();
358
+ const at = DateTime.makeUnsafe("2026-07-24T10:05:00.000Z");
359
+ const message = (id: string, text: string, ordinal: number) => {
360
+ const artifacts = makeSubagentConversationArtifacts({
361
+ messageId: MessageId.make(id),
362
+ turnItemId: TurnItemId.make(id),
363
+ threadId: parentThreadId,
364
+ rootNodeId: NodeId.make("root"),
365
+ providerThreadId: null,
366
+ providerTurnId: null,
367
+ nativeItemRef: null,
368
+ role: "assistant",
369
+ text,
370
+ ordinal,
371
+ now: at,
372
+ });
373
+ return {
374
+ message: { ...artifacts.message, runId: run.id },
375
+ turnItem: { ...artifacts.turnItem, runId: run.id },
376
+ };
377
+ };
378
+ const progress = message("progress", "Starting step 0.", 1);
379
+ const report = message("report", "All steps passed.", 2);
380
+ const result = subagentResultForRun(
381
+ {
382
+ ...projection,
383
+ messages: [progress.message, report.message],
384
+ turnItems: [report.turnItem, progress.turnItem],
385
+ },
386
+ run,
387
+ );
388
+ assert.equal(result.text, "All steps passed.");
389
+ assert.equal(result.messageId, report.message.id);
390
+ assert.equal(result.turnItemId, report.turnItem.id);
391
+});
392
+
393
it.effect("ends an orphaned native subagent's node when only its record already ended", () =>
394
Effect.gen(function* () {
395
const id = NodeId.make("node:orphaned-native-subagent");
packages/provider-core/src/server/subagentProjection.ts
+16
-12
@@ -172,18 +172,6 @@ export function subagentResultForRun(
172
readonly messageId: OrchestrationV2ConversationMessage["id"] | null;
173
readonly turnItemId: OrchestrationV2TurnItem["id"] | null;
174
} {
175
- const message =
176
- projection.messages
177
- .filter(
178
- (candidate) =>
179
- candidate.runId === run.id &&
180
- candidate.role === "assistant" &&
181
- candidate.text.trim().length > 0,
182
- )
183
- .toSorted(
184
- (left, right) =>
185
- DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
186
- )[0] ?? null;
175
const turnItem =
176
projection.turnItems
177
.filter(
@@ -195,6 +183,22 @@ export function subagentResultForRun(
183
candidate.text.trim().length > 0,
184
)
185
.toSorted((left, right) => right.ordinal - left.ordinal)[0] ?? null;
186
+ const messages = projection.messages.filter(
187
+ (candidate) =>
188
+ candidate.runId === run.id &&
189
+ candidate.role === "assistant" &&
190
+ candidate.text.trim().length > 0,
191
+ );
192
+ // Turn item order is authoritative: providers can finalize every message of a turn at once,
193
+ // so message timestamps can tie. They only order messages that have no turn item.
194
+ const message =
195
+ messages.find((candidate) => candidate.id === turnItem?.messageId) ??
196
+ messages.toSorted(
197
+ (left, right) =>
198
+ DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt) ||
199
+ DateTime.toEpochMillis(right.createdAt) - DateTime.toEpochMillis(left.createdAt),
200
+ )[0] ??
201
+ null;
202
const failure =
203
run.status === "failed"
204
? projection.turnItems