Skip to content

Commit 1610d39

Browse files
t3dotggclaude
andcommitted
Address cursor bot review: session-gate, transcript tail, mobile noise
- workflow-logic: settle in-flight agents to "error" when terminalizing a run whose session died, so a stopped chip never sits above pulsing "running" agents; drop the dead hasStartedActivity field (snapshot-only runs render intentionally after history trims) - ChatView: gate workflow liveness on derivePhase so interrupted sessions also terminalize runs, matching disconnected-session UX - WorkflowPanel: fetch the transcript once more when a run leaves "running" so lines appended after the last poll tick are not lost - mobile: suppress workflow-owned task.progress/task.completed rows in the work log, mirroring desktop (mobile has no workflow card yet) Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent e5b721e commit 1610d39

5 files changed

Lines changed: 123 additions & 15 deletions

File tree

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,68 @@ function resolvePendingUserInputAnswer(
229229
return normalizeDraftAnswer(draft?.selectedOptionLabel);
230230
}
231231

232+
/**
233+
* Task ids owned by a workflow run. Mirrors the desktop derivation in
234+
* apps/web/src/workflow-logic.ts (collectWorkflowTaskIds) — mobile has no
235+
* workflow card yet, so all rows for those tasks are suppressed rather than
236+
* rendered as per-tick noise.
237+
*/
238+
function collectWorkflowTaskIds(
239+
activities: ReadonlyArray<OrchestrationThreadActivity>,
240+
): Set<string> {
241+
const taskIds = new Set<string>();
242+
for (const activity of activities) {
243+
const payload = activity.payload as Record<string, unknown> | null | undefined;
244+
const taskId =
245+
payload && typeof payload === "object" && typeof payload["taskId"] === "string"
246+
? payload["taskId"]
247+
: undefined;
248+
if (!taskId) continue;
249+
const workflowName =
250+
payload && typeof payload === "object" ? payload["workflowName"] : undefined;
251+
const taskType = payload && typeof payload === "object" ? payload["taskType"] : undefined;
252+
if (
253+
activity.kind === "task.workflow-updated" ||
254+
activity.kind === "task.workflow-meta" ||
255+
(activity.kind === "task.started" &&
256+
(taskType === "local_workflow" || typeof workflowName === "string"))
257+
) {
258+
taskIds.add(taskId);
259+
}
260+
}
261+
return taskIds;
262+
}
263+
264+
function activityBelongsToWorkflow(
265+
activity: OrchestrationThreadActivity,
266+
workflowTaskIds: ReadonlySet<string>,
267+
): boolean {
268+
const payload = activity.payload as Record<string, unknown> | null | undefined;
269+
const taskId = payload && typeof payload === "object" ? payload["taskId"] : undefined;
270+
return typeof taskId === "string" && workflowTaskIds.has(taskId);
271+
}
272+
232273
function deriveWorkLogEntries(
233274
activities: ReadonlyArray<OrchestrationThreadActivity>,
234275
): DerivedWorkLogEntry[] {
235276
const ordered = Arr.sort(activities, activityOrder);
277+
const workflowTaskIds = collectWorkflowTaskIds(activities);
236278
const entries: DerivedWorkLogEntry[] = [];
237279
for (const activity of ordered) {
238280
if (activity.kind === "tool.started") continue;
239281
if (activity.kind === "task.started") continue;
240282
// Workflow snapshot/meta activities back the desktop workflow card; on
241-
// mobile they would render as ever-mutating raw rows, so skip them.
283+
// mobile they would render as ever-mutating raw rows, so skip them —
284+
// along with the per-tick task rows the workflow owns.
242285
if (activity.kind === "task.workflow-updated") continue;
243286
if (activity.kind === "task.workflow-meta") continue;
287+
if (
288+
(activity.kind === "task.progress" || activity.kind === "task.completed") &&
289+
workflowTaskIds.size > 0 &&
290+
activityBelongsToWorkflow(activity, workflowTaskIds)
291+
) {
292+
continue;
293+
}
244294
if (activity.kind === "context-window.updated") continue;
245295
if (activity.summary === "Checkpoint captured") continue;
246296
if (isPlanBoundaryToolActivity(activity)) continue;

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,11 +1730,9 @@ function ChatViewContent(props: ChatViewProps) {
17301730
const phase = derivePhase(activeThread?.session ?? null);
17311731
const threadActivities = activeThread?.activities ?? EMPTY_ACTIVITIES;
17321732
const workLogEntries = useMemo(() => deriveWorkLogEntries(threadActivities), [threadActivities]);
1733-
const workflowSessionActive =
1734-
activeThread?.session !== null &&
1735-
activeThread?.session !== undefined &&
1736-
activeThread.session.status !== "stopped" &&
1737-
activeThread.session.status !== "error";
1733+
// Mirrors derivePhase: interrupted/stopped/error sessions are all
1734+
// disconnected, and a workflow cannot still be running under any of them.
1735+
const workflowSessionActive = phase !== "disconnected";
17381736
const workflowRuns = useMemo(
17391737
() => deriveWorkflowRuns(threadActivities, { sessionActive: workflowSessionActive }),
17401738
[threadActivities, workflowSessionActive],

‎apps/web/src/components/workflow/WorkflowPanel.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,12 @@ function AgentTranscriptView({
425425
};
426426
}, [loadMore]);
427427

428-
// Keep polling for new lines while the run is live.
428+
// Keep polling for new lines while the run is live; when the run settles
429+
// (or the row opens on an already-terminal run), fetch once more so lines
430+
// appended after the last poll tick are not lost.
429431
useEffect(() => {
430432
if (runStatus !== "running") {
433+
void loadMore();
431434
return;
432435
}
433436
const id = setInterval(() => {

‎apps/web/src/workflow-logic.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,40 @@ describe("deriveWorkflowRuns", () => {
256256
expect(runs).toEqual([]);
257257
});
258258

259+
it("terminalizes a still-running run and settles in-flight agents when the session is gone", () => {
260+
const runs = deriveWorkflowRuns(
261+
[
262+
workflowStartedActivity("task-1"),
263+
workflowUpdatedActivity("task-1", [
264+
{ type: "workflow_agent", index: 0, state: "done" },
265+
{ type: "workflow_agent", index: 1, state: "start", startedAt: 1000 },
266+
{ type: "workflow_agent", index: 2, state: "start" },
267+
]),
268+
],
269+
{ sessionActive: false },
270+
);
271+
const run = runs[0];
272+
expect(run?.status).toBe("stopped");
273+
const agents = run?.phases.flatMap((phase) => phase.agents) ?? [];
274+
expect(agents.map((agent) => agent.status)).toEqual(["done", "error", "error"]);
275+
expect(agents[1]?.error).toBe("Interrupted before completion");
276+
expect(run?.agentCounts).toEqual({ total: 3, queued: 0, running: 0, done: 1, error: 2 });
277+
});
278+
279+
it("keeps a running run untouched while the session is active", () => {
280+
const runs = deriveWorkflowRuns(
281+
[
282+
workflowStartedActivity("task-1"),
283+
workflowUpdatedActivity("task-1", [
284+
{ type: "workflow_agent", index: 0, state: "start", startedAt: 1000 },
285+
]),
286+
],
287+
{ sessionActive: true },
288+
);
289+
expect(runs[0]?.status).toBe("running");
290+
expect(runs[0]?.phases.flatMap((phase) => phase.agents)[0]?.status).toBe("running");
291+
});
292+
259293
it("detects remote runs from session handles", () => {
260294
const runs = deriveWorkflowRuns([
261295
workflowStartedActivity("task-remote"),

‎apps/web/src/workflow-logic.ts‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,7 @@ export function groupWorkflowAgentsByPhase(parsed: {
270270
.toSorted((a, b) => a.index - b.index);
271271
}
272272

273-
interface MutableWorkflowRun extends WorkflowRun {
274-
hasStartedActivity: boolean;
275-
}
273+
type MutableWorkflowRun = WorkflowRun;
276274

277275
function isWorkflowTaskStartedPayload(payload: Record<string, unknown>): boolean {
278276
return payload.taskType === "local_workflow" || asString(payload.workflowName) !== undefined;
@@ -326,7 +324,6 @@ export function deriveWorkflowRuns(
326324
phases: [],
327325
logs: [],
328326
agentCounts: { total: 0, queued: 0, running: 0, done: 0, error: 0 },
329-
hasStartedActivity: false,
330327
};
331328
runs.set(taskId, run);
332329
return run;
@@ -349,7 +346,6 @@ export function deriveWorkflowRuns(
349346
}
350347
const run = ensureRun(taskId, activity);
351348
run.revision += 1;
352-
run.hasStartedActivity = true;
353349
run.createdAt = activity.createdAt;
354350
run.turnId = activity.turnId;
355351
const name = asString(payload.workflowName);
@@ -440,16 +436,43 @@ export function deriveWorkflowRuns(
440436

441437
// A workflow cannot outlive its provider session: when the session is gone
442438
// and no task_notification ever arrived (crash, interrupt, app restart),
443-
// surface the run as stopped instead of running forever.
439+
// surface the run as stopped instead of running forever. Runs derived only
440+
// from snapshot/meta activities (no task.started — e.g. after a checkpoint
441+
// revert trimmed it) are kept intentionally: partial history still renders.
444442
const sessionActive = options?.sessionActive ?? true;
445443
return [...runs.values()]
446-
.map(({ hasStartedActivity: _hasStartedActivity, ...run }) => run)
447444
.map((run) =>
448-
run.status === "running" && !sessionActive ? { ...run, status: "stopped" as const } : run,
445+
run.status === "running" && !sessionActive ? terminalizeInterruptedRun(run) : run,
449446
)
450447
.toSorted((a, b) => a.createdAt.localeCompare(b.createdAt) || a.taskId.localeCompare(b.taskId));
451448
}
452449

450+
/**
451+
* Settle a run whose session died before a terminal task notification:
452+
* the run becomes "stopped" and its in-flight agents settle to "error" so
453+
* nothing keeps rendering (or polling) as live work.
454+
*/
455+
function terminalizeInterruptedRun(run: WorkflowRun): WorkflowRun {
456+
const settleAgent = (agent: WorkflowRunAgent): WorkflowRunAgent =>
457+
agent.status === "running" || agent.status === "queued"
458+
? { ...agent, status: "error", error: agent.error ?? "Interrupted before completion" }
459+
: agent;
460+
const phases = run.phases.map((phase) => ({ ...phase, agents: phase.agents.map(settleAgent) }));
461+
const agents = phases.flatMap((phase) => phase.agents);
462+
return {
463+
...run,
464+
status: "stopped",
465+
phases,
466+
agentCounts: {
467+
total: agents.length,
468+
queued: agents.filter((agent) => agent.status === "queued").length,
469+
running: agents.filter((agent) => agent.status === "running").length,
470+
done: agents.filter((agent) => agent.status === "done").length,
471+
error: agents.filter((agent) => agent.status === "error").length,
472+
},
473+
};
474+
}
475+
453476
export function isRemoteWorkflowRun(run: WorkflowRun): boolean {
454477
return run.handles?.taskType === "remote_agent" || run.handles?.sessionUrl !== undefined;
455478
}

0 commit comments

Comments
 (0)