Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-12202): correct block-run terminal copy and turn-overlap dedup (#7291)
Celal Zamanoğlu5b9ff64d8a93
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsxindex 60a82f5c1..4a3e9b4ce 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx@@ -704,6 +704,10 @@ export function WorkflowCopilotChat({// workflow_run_id — a run's evaluating and final verdict frames share an// id, so this stops the same run from being fetched twice.const fetchedActionRunIds = useRef<Set<string>>(new Set());+ // Run ids the copilot claimed via run_outcome — the turn narrates these+ // itself, so useRunLifecycleAnnouncements suppresses their lifecycle lines by+ // identity (an unrelated run seen in the same window must still be narrated).+ const turnOwnedRunIds = useRef<Set<string>>(new Set());useEffect(() => {workflowCopilotChatIdRef.current = workflowCopilotChatId;}, [workflowCopilotChatId]);@@ -760,7 +764,11 @@ export function WorkflowCopilotChat({}, []);useRunLifecycleAnnouncements({workflowRunId: docked && copilotUxV1Enabled ? workflowRunId : undefined,- turnInFlightRef: inFlightRef,+ // isLoading here, not inFlightRef: this hook needs a value React re-runs+ // its effect on when the turn ends, which a ref can't do. The one-render+ // lag that matters for the double-submit guard above doesn't matter here.+ turnInFlight: isLoading,+ turnOwnedRunIds,announce: announceRunLifecycle,});// Recorded actions arrive well after block_progress (they're persisted in@@ -1931,6 +1939,15 @@ export function WorkflowCopilotChat({return false;case "run_outcome":applyStoredNarrativeEvent(payload);+ if (payload.workflow_run_id) {+ const owned = turnOwnedRunIds.current;+ owned.add(payload.workflow_run_id);+ while (owned.size > MAX_TURN_SNAPSHOTS) {+ const oldest = owned.values().next().value;+ if (oldest === undefined) break;+ owned.delete(oldest);+ }+ }maybeFetchRecordedActions(payload);return false;case "turn_start": {diff --git a/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.test.tsxindex e6aad8fa5..f30c93296 100644--- a/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.test.tsx@@ -43,17 +43,27 @@ function renderLifecycle(initial: {inFlight?: boolean;search?: string;}) {- const turnInFlightRef = { current: initial.inFlight ?? false };const announce = vi.fn();+ const turnOwnedRunIds = { current: new Set<string>() };const view = renderHook(- ({ workflowRunId }: { workflowRunId: string | undefined }) =>+ ({+ workflowRunId,+ turnInFlight,+ }: {+ workflowRunId: string | undefined;+ turnInFlight: boolean;+ }) =>useRunLifecycleAnnouncements({workflowRunId,- turnInFlightRef,+ turnInFlight,+ turnOwnedRunIds,announce,}),{- initialProps: { workflowRunId: initial.workflowRunId },+ initialProps: {+ workflowRunId: initial.workflowRunId,+ turnInFlight: initial.inFlight ?? false,+ },wrapper: ({ children }) => (<MemoryRouter initialEntries={[`/?${initial.search ?? ""}`]}>{children}@@ -61,7 +71,7 @@ function renderLifecycle(initial: {),},);- return { ...view, announce, turnInFlightRef };+ return { ...view, announce, turnOwnedRunIds };}beforeEach(() => {@@ -80,7 +90,7 @@ describe("useRunLifecycleAnnouncements", () => {content: "Run started — watching it now.",});- rerender({ workflowRunId: "wr_1" });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });expect(announce).toHaveBeenCalledTimes(1);});@@ -97,7 +107,7 @@ describe("useRunLifecycleAnnouncements", () => {outputs: { extracted_information: [1, 2, 3, 4, 5] },}),});- rerender({ workflowRunId: "wr_1" });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });expect(announce).toHaveBeenCalledTimes(2);expect(announce.mock.calls[1]![0]).toMatchObject({@@ -118,7 +128,7 @@ describe("useRunLifecycleAnnouncements", () => {outputs: { extracted_information: { rows: [1, 2, 3] } },}),});- rerender({ workflowRunId: "wr_1" });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });expect(announce.mock.calls[1]![0].content).toContain("extracted 3 item(s)");});@@ -134,7 +144,7 @@ describe("useRunLifecycleAnnouncements", () => {outputs: { extracted_information: { a: [1], b: [2] } },}),});- rerender({ workflowRunId: "wr_1" });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });expect(announce.mock.calls[1]![0].content).toBe("Run completed in 0:10. Want to review or change anything?",@@ -148,7 +158,7 @@ describe("useRunLifecycleAnnouncements", () => {const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });expect(announce).not.toHaveBeenCalled();- rerender({ workflowRunId: "wr_1" });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });expect(announce).not.toHaveBeenCalled();});@@ -166,7 +176,7 @@ describe("useRunLifecycleAnnouncements", () => {failure_reason: longReason,}),});- rerender({ workflowRunId: "wr_1" });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });expect(announce).toHaveBeenCalledTimes(2);const message = announce.mock.calls[1]![0].content as string;@@ -184,7 +194,7 @@ describe("useRunLifecycleAnnouncements", () => {workflowRunQueryMock.mockReturnValue({data: makeRun({ status: Status.Canceled }),});- rerender({ workflowRunId: "wr_1" });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });expect(announce).toHaveBeenCalledTimes(2);expect(announce.mock.calls[1]![0].content).toBe("Run canceled.");@@ -201,11 +211,35 @@ describe("useRunLifecycleAnnouncements", () => {// Studio focus moves to wr_b, but keepPreviousData is still serving wr_a's// payload as a placeholder while the new query is in flight — the hook// input changed, the payload's own workflow_run_id did not.- rerender({ workflowRunId: "wr_b" });+ rerender({ workflowRunId: "wr_b", turnInFlight: false });expect(announce).not.toHaveBeenCalled();});- it("stays permanently silent for a run first observed while a copilot turn is in flight", () => {+ it("stays silent for the copilot's own run (claimed via run_outcome), even once it finishes and the turn ends", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender, turnOwnedRunIds } = renderLifecycle({+ workflowRunId: "wr_1",+ inFlight: true,+ });+ expect(announce).not.toHaveBeenCalled();++ // The turn claims wr_1 as its own build/test run (run_outcome carried its+ // id) — from here it is silenced by identity, not by timing.+ turnOwnedRunIds.current.add("wr_1");++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Completed }),+ });+ rerender({ workflowRunId: "wr_1", turnInFlight: true });+ expect(announce).not.toHaveBeenCalled();++ // Turn ends after the run already finished inside it — the copilot already+ // narrated it, so it stays fully silent.+ rerender({ workflowRunId: "wr_1", turnInFlight: false });+ expect(announce).not.toHaveBeenCalled();+ });++ it("REGRESSION: an unrelated run seen mid-turn gets its lines once the turn ends, instead of being silenced forever", () => {workflowRunQueryMock.mockReturnValue({ data: makeRun() });const { announce, rerender } = renderLifecycle({workflowRunId: "wr_1",@@ -213,11 +247,110 @@ describe("useRunLifecycleAnnouncements", () => {});expect(announce).not.toHaveBeenCalled();+ // Turn ends while the unrelated run is still going — it's now a fresh+ // observation and gets a start line.+ rerender({ workflowRunId: "wr_1", turnInFlight: false });+ expect(announce).toHaveBeenCalledTimes(1);+ expect(announce.mock.calls[0]![0].content).toBe(+ "Run started — watching it now.",+ );+workflowRunQueryMock.mockReturnValue({data: makeRun({ status: Status.Completed }),});- rerender({ workflowRunId: "wr_1" });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });+ expect(announce).toHaveBeenCalledTimes(2);+ expect(announce.mock.calls[1]![0].content).toContain("Run completed in");+ });++ it("REGRESSION: a run's terminal message isn't delayed by a later, unrelated turn", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });+ expect(announce).toHaveBeenCalledTimes(1);++ // An unrelated copilot turn starts while wr_1 is still running.+ rerender({ workflowRunId: "wr_1", turnInFlight: true });+ expect(announce).toHaveBeenCalledTimes(1);++ // wr_1 finishes while that unrelated turn is still in flight — its+ // terminal line must not wait for the unrelated turn to end.+ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Completed }),+ });+ rerender({ workflowRunId: "wr_1", turnInFlight: true });+ expect(announce).toHaveBeenCalledTimes(2);+ expect(announce.mock.calls[1]![0].content).toContain("Run completed in");+ });++ it("REGRESSION: the copilot's own run stays silent by identity even when the post-turn poll is stale", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender, turnOwnedRunIds } = renderLifecycle({+ workflowRunId: "wr_1",+ inFlight: true,+ });+ expect(announce).not.toHaveBeenCalled();++ // run_outcome claimed wr_1 as the turn's own run before the turn ended.+ turnOwnedRunIds.current.add("wr_1");++ // Turn ends, but the query hasn't repolled — data is still the pre-turn-end+ // "running" snapshot. Identity silences it regardless of poll timing.+ rerender({ workflowRunId: "wr_1", turnInFlight: false });expect(announce).not.toHaveBeenCalled();++ // A fresh poll shows the run already completed — still the turn's own run,+ // still fully silent.+ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Completed }),+ });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });+ expect(announce).not.toHaveBeenCalled();+ });++ it("REGRESSION: an unrelated run finishing in the post-turn poll gap is narrated, not silenced (turn never claimed it)", () => {+ // First seen running mid-turn; the turn never claims it via run_outcome, so+ // it is an unrelated, manually-started run — not the copilot's own.+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({+ workflowRunId: "wr_1",+ inFlight: true,+ });+ expect(announce).not.toHaveBeenCalled();++ // The turn ends and only then does a fresh poll arrive — by which point the+ // run has already completed (it finished inside the ~5s post-turn poll gap).+ // A pure-timing dedup silenced this; keying on identity narrates its end.+ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Completed }),+ });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });++ expect(announce).toHaveBeenCalledTimes(1);+ expect(announce.mock.calls[0]![0]).toMatchObject({+ id: "run-lifecycle-wr_1-terminal",+ content: "Run completed in 0:10. Want to review or change anything?",+ });+ });++ it("REGRESSION: block-run terminal copy says 'Block run completed', not 'Run completed'", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({+ workflowRunId: "wr_1",+ search: "bl=block_1",+ });+ expect(announce.mock.calls[0]![0].content).toBe(+ "Block run started — watching it now.",+ );++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Completed }),+ });+ rerender({ workflowRunId: "wr_1", turnInFlight: false });++ expect(announce).toHaveBeenCalledTimes(2);+ expect(announce.mock.calls[1]![0].content).toBe(+ "Block run completed in 0:10. Want to review or change anything?",+ );});it("does nothing when workflowRunId is undefined, and disables the query instead of letting it fall back to the route", () => {diff --git a/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.ts b/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.tsindex d82800c7e..2b8cb6f73 100644--- a/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.ts@@ -74,22 +74,24 @@ function buildStartMessage(function buildTerminalMessage(id: string,data: WorkflowRunStatusApiResponse,+ isBlockRun: boolean,): RunLifecycleMessage {+ const runLabel = isBlockRun ? "Block run" : "Run";const dur = formatElapsedSeconds(elapsedMs(data.started_at ?? data.created_at, data.finished_at),);let content: string;if (data.status === Status.Canceled) {- content = "Run canceled.";+ content = `${runLabel} canceled.`;} else if (statusIsAFailureType({ status: data.status })) {const reason = data.failure_reason? ` — ${truncate(data.failure_reason, FAILURE_REASON_MAX_CHARS)}`: "";- content = `Run ${failureVerb(data.status)} after ${dur}${reason}. Ask me to diagnose and fix it.`;+ content = `${runLabel} ${failureVerb(data.status)} after ${dur}${reason}. Ask me to diagnose and fix it.`;} else {const count = extractedCount(data.outputs);const extracted = count ? ` — extracted ${count} item(s)` : "";- content = `Run completed in ${dur}${extracted}. Want to review or change anything?`;+ content = `${runLabel} completed in ${dur}${extracted}. Want to review or change anything?`;}return {id: `run-lifecycle-${id}-terminal`,@@ -99,7 +101,7 @@ function buildTerminalMessage(};}-type SeenEntry = { start: boolean; terminal: boolean };+type SeenEntry = { start: boolean; terminal: boolean; wasRunning: boolean };/*** Watches the studio's focused run and appends presentational lifecycle lines@@ -108,11 +110,16 @@ type SeenEntry = { start: boolean; terminal: boolean };*/export function useRunLifecycleAnnouncements({workflowRunId,- turnInFlightRef,+ turnInFlight,+ turnOwnedRunIds,announce,}: {workflowRunId: string | undefined;- turnInFlightRef: { current: boolean };+ turnInFlight: boolean;+ // Run ids the copilot claimed via run_outcome this session — the turn already+ // narrates these through its own SSE stream, so we suppress them by identity+ // rather than by the timing of when they were first seen.+ turnOwnedRunIds: { current: Set<string> };announce: (message: RunLifecycleMessage) => void;}): void {// enabled: false (not just an omitted workflowRunId) stops useWorkflowRunQuery@@ -135,19 +142,53 @@ export function useRunLifecycleAnnouncements({const id = data.workflow_run_id;let entry = seen.current.get(id);if (!entry) {- const finalized = statusIsFinalized({ status: data.status });- const silent = finalized || turnInFlightRef.current;- entry = { start: silent, terminal: silent };+ entry = { start: false, terminal: false, wasRunning: false };seen.current.set(id, entry);- if (!silent) {- entry.start = true;- announce(buildStartMessage(id, data, isBlockRun));+ }+ // The copilot's own build/test run narrates itself through the turn's SSE+ // stream, so identity — not timing — is what silences it: a run whose id+ // the turn claimed via run_outcome stays fully silent, forever.+ if (turnOwnedRunIds.current.has(id)) {+ entry.start = true;+ entry.terminal = true;+ return;+ }+ const finalized = statusIsFinalized({ status: data.status });+ // A not-yet-claimed run seen mid-turn may still turn out to be the turn's+ // own (its run_outcome hasn't landed yet), so hold its lines until the turn+ // ends instead of leaking a start line the turn would also narrate. Record+ // that it was live so a completion observed right after the turn ends is+ // announced as a real terminal, not silently absorbed as an+ // already-finished first sight (which is what a genuinely unrelated run+ // finishing inside the post-turn poll gap would otherwise look like).+ if (!entry.start && turnInFlight) {+ if (!finalized) {+ entry.wasRunning = true;}return;}- if (!entry.terminal && statusIsFinalized({ status: data.status })) {+ if (!entry.start) {+ entry.start = true;+ if (finalized) {+ entry.terminal = true;+ if (entry.wasRunning) {+ announce(buildTerminalMessage(id, data, isBlockRun));+ }+ return;+ }+ announce(buildStartMessage(id, data, isBlockRun));+ return;+ }+ if (!entry.terminal && finalized) {entry.terminal = true;- announce(buildTerminalMessage(id, data));+ announce(buildTerminalMessage(id, data, isBlockRun));}- }, [workflowRunId, data, announce, isBlockRun, turnInFlightRef]);+ }, [+ workflowRunId,+ data,+ announce,+ isBlockRun,+ turnInFlight,+ turnOwnedRunIds,+ ]);}