diff --git a/apps/mobile/src/lib/threadActivity.test.ts b/apps/mobile/src/lib/threadActivity.test.ts index f5b4d0232a902..4572c650c3e0c 100644 --- a/apps/mobile/src/lib/threadActivity.test.ts +++ b/apps/mobile/src/lib/threadActivity.test.ts @@ -1593,6 +1593,48 @@ describe("subagent continued by a later turn", () => { "closing-2", ]); }); + + it("times a turn to its answer when a delegated task resumes after it", () => { + const resumed: OrchestrationV2TurnItem[] = [ + user("prompt-1", runId, 1_000_001, 0), + reasoning("reasoning-1", runId, 1_000_002, 1), + assistant("answer-1", runId, 1_000_003, 3), + { + ...item("resumed", runId, 1_000_004, 36), + completedAt: DateTime.makeUnsafe(at(40)), + updatedAt: DateTime.makeUnsafe(at(40)), + type: "subagent", + subagentId: NodeId.make("resumed"), + origin: "app_owned", + driver: ProviderDriverKind.make("codex"), + providerInstanceId: ProviderInstanceId.make("codex"), + childThreadId: sourceThreadId, + prompt: "Research the bug", + result: "Found it", + }, + { + ...item("finished", run2, 2_000_001, 50), + type: "notification", + source: { + kind: "delegated_task", + taskIds: [NodeId.make("resumed")], + childThreadId: sourceThreadId, + }, + outcome: "completed", + summary: 'Delegated task "Research the bug" finished', + }, + reasoning("reasoning-2", run2, 2_000_002, 51), + assistant("answer-2", run2, 2_000_003, 52), + ]; + const presented = deriveThreadFeedPresentation( + buildThreadFeed(resumed.map((entry, index) => projected(entry, index))), + { runId: run2, status: "completed", startedAt: at(50), completedAt: at(52) }, + new Set(), + ); + expect(presented.find((entry) => entry.id === `run-fold:${runId}`)).toMatchObject({ + label: "Worked for 3s", + }); + }); }); it("stops stranded thinking after a steer and follows the next thought or tool", () => { diff --git a/apps/mobile/src/lib/threadActivity.ts b/apps/mobile/src/lib/threadActivity.ts index 50c90a05106a2..d1bb36a66876e 100644 --- a/apps/mobile/src/lib/threadActivity.ts +++ b/apps/mobile/src/lib/threadActivity.ts @@ -1190,8 +1190,17 @@ function deriveThreadFeedRunFolds( ? group.entries.find((entry) => entry.id === terminalAssistantId) : null; const latestRunMatches = latestRun?.runId === runId; + // A card dates from its task's latest start, so a resumed task's card lands after its run ended. + const lastTimedEntry = + group.entries.findLast( + (entry) => + entry.type !== "activity-group" || + entry.activities[0]?.projectedItem.item.type !== "subagent", + ) ?? lastEntry; const lastEntryEnd = - lastEntry.type === "message" ? lastEntry.message.updatedAt : lastEntry.createdAt; + lastTimedEntry.type === "message" + ? lastTimedEntry.message.updatedAt + : lastTimedEntry.createdAt; const elapsedMs = latestRunMatches && latestRun.startedAt && latestRun.completedAt ? computeElapsedMs(latestRun.startedAt, latestRun.completedAt) diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts index ba0f508ebeecd..df2ab7790b72a 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts @@ -4535,6 +4535,123 @@ describe("subagent continued by a later turn", () => { "closing-2", ]); }); + + it("folds a delegated task resumed after the turn's answer into that turn", () => { + const resumed: OrchestrationV2TurnItem[] = [ + user("prompt-1", run1, 1_000_001, 0), + reasoning("reasoning-1", run1, 1_000_002, 1), + assistant("answer-1", run1, 1_000_003, 3), + { + ...base("resumed", run1, 1_000_004, 36), + completedAt: at(40), + updatedAt: at(40), + type: "subagent", + subagentId: NodeId.make("resumed"), + origin: "app_owned", + driver: ProviderDriverKind.make("codex"), + providerInstanceId: ProviderInstanceId.make("codex"), + childThreadId: ThreadId.make("child-thread"), + prompt: "Research the bug", + result: "Found it", + }, + { + ...base("finished", run2, 2_000_001, 50), + type: "notification", + source: { + kind: "delegated_task", + taskIds: [NodeId.make("resumed")], + childThreadId: ThreadId.make("child-thread"), + }, + outcome: "completed", + summary: 'Delegated task "Research the bug" finished', + }, + reasoning("reasoning-2", run2, 2_000_002, 51), + assistant("answer-2", run2, 2_000_003, 52), + ]; + const rows = deriveMessagesTimelineRows({ + timelineEntries: deriveTimelineEntriesFromVisibleTurnItems({ + visibleTurnItems: resumed.map((item) => ({ + position: item.ordinal, + visibility: "local", + sourceThreadId: threadId, + sourceItemId: item.id, + item, + })), + optimisticMessages: [], + }), + latestRun: { + runId: run2, + status: "completed", + startedAt: DateTime.formatIso(at(50)), + completedAt: DateTime.formatIso(at(52)), + }, + isWorking: false, + turnDiffSummaries: [], + supportsConversationRollback: false, + }); + expect( + rows.map((row) => (row.kind === "turn-fold" ? `${row.id}: ${row.label}` : row.id)), + ).toEqual([ + "prompt-1", + `turn-fold:${run1}: Worked for 3s`, + "answer-1", + "finished", + `turn-fold:${run2}: Worked for 2s`, + "answer-2", + ]); + }); + + it("times a steered turn's continuation to its answer when a delegated task resumes after it", () => { + const resumed: OrchestrationV2TurnItem[] = [ + user("prompt-1", run1, 1_000_001, 0), + reasoning("reasoning-1", run1, 1_000_002, 1), + { ...user("steer-1", run1, 1_000_003, 2), inputIntent: "steer" }, + reasoning("reasoning-steer", run1, 1_000_004, 3), + assistant("answer-1", run1, 1_000_005, 5), + { + ...base("resumed", run1, 1_000_006, 36), + completedAt: at(40), + updatedAt: at(40), + type: "subagent", + subagentId: NodeId.make("resumed"), + origin: "app_owned", + driver: ProviderDriverKind.make("codex"), + providerInstanceId: ProviderInstanceId.make("codex"), + childThreadId: ThreadId.make("child-thread"), + prompt: "Research the bug", + result: "Found it", + }, + user("prompt-2", run2, 2_000_001, 50), + assistant("answer-2", run2, 2_000_002, 52), + ]; + const rows = deriveMessagesTimelineRows({ + timelineEntries: deriveTimelineEntriesFromVisibleTurnItems({ + visibleTurnItems: resumed.map((item) => ({ + position: item.ordinal, + visibility: "local", + sourceThreadId: threadId, + sourceItemId: item.id, + item, + })), + optimisticMessages: [], + }), + latestRun: { + runId: run2, + status: "completed", + startedAt: DateTime.formatIso(at(50)), + completedAt: DateTime.formatIso(at(52)), + }, + isWorking: false, + turnDiffSummaries: [], + supportsConversationRollback: false, + }); + expect( + rows.flatMap((row) => (row.kind === "turn-fold" ? [`${row.id}: ${row.label}`] : [])), + ).toEqual([ + `turn-fold:${run1}: Worked for 5s`, + `turn-fold:${run1}:steer:steer-1: Continued for 3s`, + ]); + }); }); describe("shouldCollapseUserMessage", () => { diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index 4536e9a250386..004e4a919cd63 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -914,6 +914,11 @@ function timelineEntryEnd(entry: TimelineEntry): string { return entry.kind === "message" ? entry.message.updatedAt : entry.createdAt; } +// A card dates from its task's latest start, so a resumed task's card lands after its run ended. +function timelineEntryIsSubagentCard(entry: TimelineEntry): boolean { + return entry.kind === "event" && entry.projectedItem.item.type === "subagent"; +} + function timelineEntryIsCompaction(entry: TimelineEntry): boolean { return entry.kind === "work" && entry.entry.sourceActivityKind === "context-compaction"; } @@ -1129,9 +1134,10 @@ function deriveTurnFolds(input: { } const isCompaction = timelineEntryIsCompaction(entry); const isFoldableTrailingActivity = - entry.kind === "work" && - entry.entry.toolLifecycleStatus !== "inProgress" && - !workEntryDisplayIndicatesToolFailure(entry.entry); + timelineEntryIsSubagentCard(entry) || + (entry.kind === "work" && + entry.entry.toolLifecycleStatus !== "inProgress" && + !workEntryDisplayIndicatesToolFailure(entry.entry)); if (!isCompaction && index > terminalEntryIndex && !isFoldableTrailingActivity) { continue; } @@ -1166,7 +1172,9 @@ function deriveTurnFolds(input: { input.latestRun?.runId === runId && input.latestRun.status === "interrupted"; // A turn cut short by a steer leaves trailing work entries behind its // terminal message — take whichever ended last. - const lastEntryEnd = timelineEntryEnd(lastEntry); + const lastEntryEnd = timelineEntryEnd( + group.entries.findLast((entry) => !timelineEntryIsSubagentCard(entry)) ?? lastEntry, + ); const elapsedMs = input.latestRun?.runId === runId && input.latestRun.startedAt && input.latestRun.completedAt ? computeElapsedMs(input.latestRun.startedAt, input.latestRun.completedAt) @@ -1204,8 +1212,10 @@ function deriveTurnFolds(input: { if (anchorEntry === undefined || !hidesNonCompactionWork(entries)) continue; const segmentMs = computeElapsedMs( steer.createdAt, - entries.map(timelineEntryEnd).reduce(maxIsoTimestamp, null) ?? - steer.createdAt, + entries + .filter((entry) => !timelineEntryIsSubagentCard(entry)) + .map(timelineEntryEnd) + .reduce(maxIsoTimestamp, null) ?? steer.createdAt, ); foldsByAnchorEntryId.set(anchorEntry.id, { runId: RunId.make(`${runId}:steer:${steer.id}`),