diff --git a/apps/mobile/src/lib/threadActivity.ts b/apps/mobile/src/lib/threadActivity.ts index 1c1582ef3e..2a2437a958 100644 --- a/apps/mobile/src/lib/threadActivity.ts +++ b/apps/mobile/src/lib/threadActivity.ts @@ -229,18 +229,68 @@ function resolvePendingUserInputAnswer( return normalizeDraftAnswer(draft?.selectedOptionLabel); } +/** + * Task ids owned by a workflow run. Mirrors the desktop derivation in + * apps/web/src/workflow-logic.ts (collectWorkflowTaskIds) — mobile has no + * workflow card yet, so all rows for those tasks are suppressed rather than + * rendered as per-tick noise. + */ +function collectWorkflowTaskIds( + activities: ReadonlyArray, +): Set { + const taskIds = new Set(); + for (const activity of activities) { + const payload = activity.payload as Record | null | undefined; + const taskId = + payload && typeof payload === "object" && typeof payload["taskId"] === "string" + ? payload["taskId"] + : undefined; + if (!taskId) continue; + const workflowName = + payload && typeof payload === "object" ? payload["workflowName"] : undefined; + const taskType = payload && typeof payload === "object" ? payload["taskType"] : undefined; + if ( + activity.kind === "task.workflow-updated" || + activity.kind === "task.workflow-meta" || + (activity.kind === "task.started" && + (taskType === "local_workflow" || typeof workflowName === "string")) + ) { + taskIds.add(taskId); + } + } + return taskIds; +} + +function activityBelongsToWorkflow( + activity: OrchestrationThreadActivity, + workflowTaskIds: ReadonlySet, +): boolean { + const payload = activity.payload as Record | null | undefined; + const taskId = payload && typeof payload === "object" ? payload["taskId"] : undefined; + return typeof taskId === "string" && workflowTaskIds.has(taskId); +} + function deriveWorkLogEntries( activities: ReadonlyArray, ): DerivedWorkLogEntry[] { const ordered = Arr.sort(activities, activityOrder); + const workflowTaskIds = collectWorkflowTaskIds(activities); const entries: DerivedWorkLogEntry[] = []; for (const activity of ordered) { if (activity.kind === "tool.started") continue; if (activity.kind === "task.started") continue; // Workflow snapshot/meta activities back the desktop workflow card; on - // mobile they would render as ever-mutating raw rows, so skip them. + // mobile they would render as ever-mutating raw rows, so skip them — + // along with the per-tick task rows the workflow owns. if (activity.kind === "task.workflow-updated") continue; if (activity.kind === "task.workflow-meta") continue; + if ( + (activity.kind === "task.progress" || activity.kind === "task.completed") && + workflowTaskIds.size > 0 && + activityBelongsToWorkflow(activity, workflowTaskIds) + ) { + continue; + } if (activity.kind === "context-window.updated") continue; if (activity.summary === "Checkpoint captured") continue; if (isPlanBoundaryToolActivity(activity)) continue; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 4bc0c53840..b77ccc3848 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -1730,11 +1730,9 @@ function ChatViewContent(props: ChatViewProps) { const phase = derivePhase(activeThread?.session ?? null); const threadActivities = activeThread?.activities ?? EMPTY_ACTIVITIES; const workLogEntries = useMemo(() => deriveWorkLogEntries(threadActivities), [threadActivities]); - const workflowSessionActive = - activeThread?.session !== null && - activeThread?.session !== undefined && - activeThread.session.status !== "stopped" && - activeThread.session.status !== "error"; + // Mirrors derivePhase: interrupted/stopped/error sessions are all + // disconnected, and a workflow cannot still be running under any of them. + const workflowSessionActive = phase !== "disconnected"; const workflowRuns = useMemo( () => deriveWorkflowRuns(threadActivities, { sessionActive: workflowSessionActive }), [threadActivities, workflowSessionActive], diff --git a/apps/web/src/components/workflow/WorkflowPanel.tsx b/apps/web/src/components/workflow/WorkflowPanel.tsx index e191399373..3a5aded319 100644 --- a/apps/web/src/components/workflow/WorkflowPanel.tsx +++ b/apps/web/src/components/workflow/WorkflowPanel.tsx @@ -425,9 +425,12 @@ function AgentTranscriptView({ }; }, [loadMore]); - // Keep polling for new lines while the run is live. + // Keep polling for new lines while the run is live; when the run settles + // (or the row opens on an already-terminal run), fetch once more so lines + // appended after the last poll tick are not lost. useEffect(() => { if (runStatus !== "running") { + void loadMore(); return; } const id = setInterval(() => { diff --git a/apps/web/src/workflow-logic.test.ts b/apps/web/src/workflow-logic.test.ts index ecbee19088..006677293a 100644 --- a/apps/web/src/workflow-logic.test.ts +++ b/apps/web/src/workflow-logic.test.ts @@ -256,6 +256,40 @@ describe("deriveWorkflowRuns", () => { expect(runs).toEqual([]); }); + it("terminalizes a still-running run and settles in-flight agents when the session is gone", () => { + const runs = deriveWorkflowRuns( + [ + workflowStartedActivity("task-1"), + workflowUpdatedActivity("task-1", [ + { type: "workflow_agent", index: 0, state: "done" }, + { type: "workflow_agent", index: 1, state: "start", startedAt: 1000 }, + { type: "workflow_agent", index: 2, state: "start" }, + ]), + ], + { sessionActive: false }, + ); + const run = runs[0]; + expect(run?.status).toBe("stopped"); + const agents = run?.phases.flatMap((phase) => phase.agents) ?? []; + expect(agents.map((agent) => agent.status)).toEqual(["done", "error", "error"]); + expect(agents[1]?.error).toBe("Interrupted before completion"); + expect(run?.agentCounts).toEqual({ total: 3, queued: 0, running: 0, done: 1, error: 2 }); + }); + + it("keeps a running run untouched while the session is active", () => { + const runs = deriveWorkflowRuns( + [ + workflowStartedActivity("task-1"), + workflowUpdatedActivity("task-1", [ + { type: "workflow_agent", index: 0, state: "start", startedAt: 1000 }, + ]), + ], + { sessionActive: true }, + ); + expect(runs[0]?.status).toBe("running"); + expect(runs[0]?.phases.flatMap((phase) => phase.agents)[0]?.status).toBe("running"); + }); + it("detects remote runs from session handles", () => { const runs = deriveWorkflowRuns([ workflowStartedActivity("task-remote"), diff --git a/apps/web/src/workflow-logic.ts b/apps/web/src/workflow-logic.ts index 1744f9b283..8e6cb90dee 100644 --- a/apps/web/src/workflow-logic.ts +++ b/apps/web/src/workflow-logic.ts @@ -270,9 +270,7 @@ export function groupWorkflowAgentsByPhase(parsed: { .toSorted((a, b) => a.index - b.index); } -interface MutableWorkflowRun extends WorkflowRun { - hasStartedActivity: boolean; -} +type MutableWorkflowRun = WorkflowRun; function isWorkflowTaskStartedPayload(payload: Record): boolean { return payload.taskType === "local_workflow" || asString(payload.workflowName) !== undefined; @@ -326,7 +324,6 @@ export function deriveWorkflowRuns( phases: [], logs: [], agentCounts: { total: 0, queued: 0, running: 0, done: 0, error: 0 }, - hasStartedActivity: false, }; runs.set(taskId, run); return run; @@ -349,7 +346,6 @@ export function deriveWorkflowRuns( } const run = ensureRun(taskId, activity); run.revision += 1; - run.hasStartedActivity = true; run.createdAt = activity.createdAt; run.turnId = activity.turnId; const name = asString(payload.workflowName); @@ -440,16 +436,43 @@ export function deriveWorkflowRuns( // A workflow cannot outlive its provider session: when the session is gone // and no task_notification ever arrived (crash, interrupt, app restart), - // surface the run as stopped instead of running forever. + // surface the run as stopped instead of running forever. Runs derived only + // from snapshot/meta activities (no task.started — e.g. after a checkpoint + // revert trimmed it) are kept intentionally: partial history still renders. const sessionActive = options?.sessionActive ?? true; return [...runs.values()] - .map(({ hasStartedActivity: _hasStartedActivity, ...run }) => run) .map((run) => - run.status === "running" && !sessionActive ? { ...run, status: "stopped" as const } : run, + run.status === "running" && !sessionActive ? terminalizeInterruptedRun(run) : run, ) .toSorted((a, b) => a.createdAt.localeCompare(b.createdAt) || a.taskId.localeCompare(b.taskId)); } +/** + * Settle a run whose session died before a terminal task notification: + * the run becomes "stopped" and its in-flight agents settle to "error" so + * nothing keeps rendering (or polling) as live work. + */ +function terminalizeInterruptedRun(run: WorkflowRun): WorkflowRun { + const settleAgent = (agent: WorkflowRunAgent): WorkflowRunAgent => + agent.status === "running" || agent.status === "queued" + ? { ...agent, status: "error", error: agent.error ?? "Interrupted before completion" } + : agent; + const phases = run.phases.map((phase) => ({ ...phase, agents: phase.agents.map(settleAgent) })); + const agents = phases.flatMap((phase) => phase.agents); + return { + ...run, + status: "stopped", + phases, + agentCounts: { + total: agents.length, + queued: agents.filter((agent) => agent.status === "queued").length, + running: agents.filter((agent) => agent.status === "running").length, + done: agents.filter((agent) => agent.status === "done").length, + error: agents.filter((agent) => agent.status === "error").length, + }, + }; +} + export function isRemoteWorkflowRun(run: WorkflowRun): boolean { return run.handles?.taskType === "remote_agent" || run.handles?.sessionUrl !== undefined; }