Skip to content

Automate browser based workflows with AI

Return focus to Try again when a lock closes the Copilot test confirmation (SKY-17340) (#8705)

Shuchang Zhengaca73294cdf5
diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
index 9e1859ead..971dae4f5 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
@@ -10,10 +10,12 @@ import { CopilotMarkdown } from "./CopilotMarkdown";
import {
ACTIVITY_KIND_GLYPH,
ACTIVITY_KIND_WORD,
+ ActivityLog,
ActivityRow as ActivityRowModel,
deriveActivityLog,
+ rowIndexOfToolCall,
} from "./copilotActivityLog";
-import { TURN_ROW_INSET } from "./cards/cardLayout";
+import { TURN_ROW_INSET, TURN_ROW_OUTSET } from "./cards/cardLayout";
import { showPhaseChecklist } from "./copilotPhases";
import { CodeWriteDiff } from "./workflowCopilotTypes";
import {
@@ -1058,9 +1060,11 @@ export function InstantAckPlaceholder() {
interface FActivityLogProps {
turn: TurnNarrativeState;
+ log: ActivityLog;
turnEnded: boolean;
onBlockSelect?: (label: string) => void;
interactionRef?: { current: string | null };
+ anchoredAfterRow?: ReadonlyMap<string, AnchoredTurnItem[]>;
}
// The kind gutter sits to the left of ActivityRow's own status column, so a
@@ -1461,13 +1465,15 @@ function FActivityLogRow({
function FActivityLog({
turn,
+ log,
turnEnded,
onBlockSelect,
interactionRef,
+ anchoredAfterRow,
}: FActivityLogProps) {
const outcomeReasonFallback = notConfirmedDisplayReason(turn);
const outcomeOwnerKey = outcomeNotConfirmedOwnerKey(turn);
- const { rows, focusIndex } = useMemo(() => deriveActivityLog(turn), [turn]);
+ const { rows, focusIndex } = log;
// Signed, not a bare id set: a click on the live row has to be able to mean
// "closed", or folding the active row would silently pin it open instead.
const [override, setOverride] = useState<ReadonlyMap<string, boolean>>(
@@ -1533,7 +1539,7 @@ function FActivityLog({
(candidate) =>
candidate.label === label && candidate.patch !== undefined,
);
- return (
+ return [
<div
key={row.id}
data-activity-row-id={row.id}
@@ -1555,19 +1561,43 @@ function FActivityLog({
outcomeReasonFallback={outcomeReasonFallback}
outcomeOwnerKey={outcomeOwnerKey}
/>
- </div>
- );
+ </div>,
+ ...(anchoredAfterRow?.get(row.id) ?? []).map((item) => (
+ <div key={item.key} className={`${TURN_ROW_OUTSET} py-0.5`}>
+ {item.node}
+ </div>
+ )),
+ ];
})}
</div>
);
}
+// Something that happened during a turn and renders where it happened: after the activity row
+// holding its tool call, or above the turn's reply when that row is not on screen.
+export interface AnchoredTurnItem {
+ key: string;
+ toolCallId: string | null;
+ node: React.ReactNode;
+}
+
+function AnchoredFallback({ items }: { items: AnchoredTurnItem[] }) {
+ return (
+ <>
+ {items.map((item) => (
+ <div key={item.key}>{item.node}</div>
+ ))}
+ </>
+ );
+}
+
interface DetailViewProps {
turn: TurnNarrativeState;
onBlockSelect?: (label: string) => void;
workingRowActive?: boolean;
activityInteractionRef?: { current: string | null };
beforeProse?: React.ReactNode;
+ anchored: AnchoredTurnItem[];
}
function DetailView({
@@ -1576,6 +1606,7 @@ function DetailView({
workingRowActive,
activityInteractionRef,
beforeProse,
+ anchored,
}: DetailViewProps) {
const collapsedOutcomeReason = notConfirmedDisplayReason(turn);
const outcomeOwnerKey = outcomeNotConfirmedOwnerKey(turn);
@@ -1593,18 +1624,44 @@ function DetailView({
const preBlockNarration = turn.designActivity.filter(
(e) => e.kind === "narration",
);
+ const log = useMemo(
+ () => (showChecklist ? deriveActivityLog(turn) : null),
+ [showChecklist, turn],
+ );
+ const { anchoredAfterRow, unanchored } = useMemo(() => {
+ const byRow = new Map<string, AnchoredTurnItem[]>();
+ const rest: AnchoredTurnItem[] = [];
+ // The log shows a placeholder instead of rows until a live turn has one.
+ const rows =
+ log && (turn.terminal !== null || log.rows.length > 0) ? log.rows : [];
+ for (const item of anchored) {
+ const index =
+ item.toolCallId === null
+ ? -1
+ : rowIndexOfToolCall(rows, item.toolCallId);
+ const row = rows[index];
+ if (row === undefined) {
+ rest.push(item);
+ } else {
+ byRow.set(row.id, [...(byRow.get(row.id) ?? []), item]);
+ }
+ }
+ return { anchoredAfterRow: byRow, unanchored: rest };
+ }, [anchored, log, turn.terminal]);
return (
<div className="flex flex-col gap-2.5">
<div className="flex flex-col gap-2.5">
- {showChecklist ? (
+ {log ? (
<div className={TURN_ROW_INSET}>
<FActivityLog
key={turn.turnId ?? ""}
turn={turn}
+ log={log}
turnEnded={turn.terminal !== null}
onBlockSelect={onBlockSelect}
interactionRef={activityInteractionRef}
+ anchoredAfterRow={anchoredAfterRow}
/>
</div>
) : showDesign ? (
@@ -1665,6 +1722,8 @@ function DetailView({
</div>
) : null}
+ <AnchoredFallback items={unanchored} />
+
{beforeProse}
{/* terminalProseTone's question branch without its evidence gate: an
@@ -1701,8 +1760,11 @@ interface NarrativeViewProps {
// What the user did mid-turn (answered a question). It happened after the turn's activity and
// before the reply, so it renders between them to keep the transcript chronological.
beforeProse?: React.ReactNode;
+ anchored?: AnchoredTurnItem[];
}
+const NO_ANCHORED_ITEMS: AnchoredTurnItem[] = [];
+
type TerminalProseTone = "answer" | "question";
function hasRecordedTerminalEvidence(turn: TurnNarrativeState): boolean {
@@ -1835,6 +1897,7 @@ export function NarrativeView({
onBlockSelect,
workingRowActive,
beforeProse,
+ anchored = NO_ANCHORED_ITEMS,
}: NarrativeViewProps) {
const proseTone = terminalProseTone(turn);
const proseText = humanizeJudgeText(terminalNarrativeText(turn));
@@ -1848,8 +1911,9 @@ export function NarrativeView({
arrivedAt={turn.endedAt}
/>
);
- return beforeProse ? (
+ return beforeProse || anchored.length > 0 ? (
<div className="flex flex-col gap-2.5">
+ <AnchoredFallback items={anchored} />
{beforeProse}
{prose}
</div>
@@ -1865,6 +1929,7 @@ export function NarrativeView({
workingRowActive={workingRowActive}
activityInteractionRef={activityInteractionRef}
beforeProse={beforeProse}
+ anchored={anchored}
/>
);
}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx
index 720cbcf5b..4e39474fb 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx
@@ -579,6 +579,115 @@ describe("WorkflowCopilotChat — activity log", () => {
});
});
+describe("WorkflowCopilotChat — credential receipt placement", () => {
+ it("keeps a mid-turn credential card after the row it was raised on, live and once the turn ends", async () => {
+ credentialsData.current = [
+ { credential_id: "cred-hn", name: "HN Login", tested_url: null },
+ ];
+ await streamScoutTurn();
+ const write = {
+ tool_name: "update_workflow",
+ display_label: "Updating workflow",
+ iteration: 1,
+ tool_call_id: "tc-2",
+ };
+ await act(async () => {
+ streamCalls[0]!.onMessage(
+ credentialFrame({ anchor_tool_call_id: "tc-1" }),
+ );
+ streamCalls[0]!.onMessage({
+ type: "credential_pause_resolved",
+ turn_id: "turn-1",
+ workflow_copilot_chat_id: "chat-1",
+ resume_token: "rt-abc",
+ outcome: "connected",
+ credential_id: "cred-hn",
+ name: "HN Login",
+ timestamp: new Date().toISOString(),
+ });
+ streamCalls[0]!.onMessage({
+ type: "tool_call",
+ tool_input: {},
+ ...write,
+ });
+ streamCalls[0]!.onMessage({
+ type: "tool_result",
+ success: true,
+ summary: "Saved the draft",
+ ...write,
+ });
+ });
+ const row = (id: string) =>
+ document.querySelector(`[data-activity-row-id="${id}"]`)!;
+ const precedes = (a: Node, b: Node) =>
+ Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING);
+ const liveReceipt = await screen.findByText("Credential 'HN Login' added");
+ expect(precedes(row("tc-1"), liveReceipt)).toBe(true);
+ expect(precedes(liveReceipt, row("tc-2"))).toBe(true);
+
+ await act(async () => {
+ streamCalls[0]!.onMessage({
+ type: "response",
+ workflow_copilot_chat_id: "chat-1",
+ message: "Signed in and saved the draft.",
+ updated_workflow: null,
+ response_time: "2026-07-13T00:00:06Z",
+ proposal_disposition: "no_proposal",
+ turn_id: "turn-1",
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ designStarted: true,
+ designEnded: true,
+ draft: { blockCount: 1, blockLabels: ["sign_in"] },
+ terminal: "response",
+ terminalMessage: "Signed in and saved the draft.",
+ narrativeSummary: "Signed in and saved the draft.",
+ startedAt: "2026-07-13T00:00:00Z",
+ endedAt: "2026-07-13T00:00:06Z",
+ designActivity: [
+ {
+ ...scoutResult(),
+ kind: "tool_result",
+ id: "tr-tc-1",
+ text: "Opened the sign-in page",
+ toolName: "navigate_browser",
+ },
+ {
+ kind: "tool_call",
+ id: "tc-tc-2",
+ text: "Updating workflow…",
+ toolName: "update_workflow",
+ iteration: 1,
+ },
+ {
+ kind: "tool_result",
+ id: "tr-tc-2",
+ text: "Saved the draft",
+ toolName: "update_workflow",
+ iteration: 1,
+ success: true,
+ },
+ ],
+ credentialPause: {
+ outcome: "connected",
+ credentialId: "cred-hn",
+ anchorToolCallId: "tc-1",
+ },
+ },
+ });
+ streamCalls[0]!.resolve();
+ });
+ const receipt = await screen.findByText("Credential 'HN Login' added");
+ expect(screen.getAllByText("Credential 'HN Login' added")).toHaveLength(1);
+ expect(precedes(row("tc-1"), receipt)).toBe(true);
+ expect(precedes(receipt, row("tc-2"))).toBe(true);
+ expect(
+ precedes(receipt, screen.getByText("Signed in and saved the draft.")),
+ ).toBe(true);
+ });
+});
+
describe("WorkflowCopilotChat — credential card wiring", () => {
it("sends the initial attachments once after the authoring hold clears", async () => {
useWorkflowYamlEditorStore.setState({ authoringInProgress: true });
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
index 8d691bf4f..a4cc1dba5 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
@@ -143,7 +143,12 @@ import {
resolveSendAction,
} from "./sendQueue";
import { shouldAutoApplyWorkflowResponse } from "./proposalDisposition";
-import { InstantAckPlaceholder, NarrativeView } from "./NarrativeView";
+import {
+ AnchoredTurnItem,
+ InstantAckPlaceholder,
+ NarrativeView,
+} from "./NarrativeView";
+import { credentialAnchorToolCallId } from "./turnCardPlacement";
import { CopilotMarkdown } from "./CopilotMarkdown";
import { FeedbackThumbs } from "@/components/feedback/FeedbackThumbs";
import {
@@ -8822,6 +8827,90 @@ export function WorkflowCopilotChat({
return -1;
}, [messages]);
+ // Each turn's closing plan, so a revision renders in the turn that wrote it and the one it
+ // replaced stays where it was, folded.
+ const turnPlans = useMemo(
+ () =>
+ messages.map((message) =>
+ message.sender === "ai" ? (message.narrative?.workPlan ?? null) : null,
+ ),
+ [messages],
+ );
+ const liveTurnPlan =
+ narrative.turnId !== null && narrative.terminal === null
+ ? (narrative.workPlan ?? null)
+ : null;
+ let lastTurnPlanIndex = -1;
+ turnPlans.forEach((plan, index) => {
+ if (plan) lastTurnPlanIndex = index;
+ });
+ const planBefore = (index: number): string[] | null => {
+ for (let i = index - 1; i >= 0; i--) {
+ const plan = turnPlans[i];
+ if (plan) return plan.items;
+ }
+ return null;
+ };
+
+ // The live turn places its plan and its credential card exactly where the finished turn will, so
+ // nothing moves when the turn ends.
+ const liveAnchored: AnchoredTurnItem[] = [];
+ if (liveTurnPlan) {
+ liveAnchored.push({
+ key: `plan-${liveTurnPlan.toolCallId}`,
+ toolCallId: liveTurnPlan.toolCallId,
+ node: (
+ <WorkPlanCard
+ items={liveTurnPlan.items}
+ previous={planBefore(messages.length)}
+ />
+ ),
+ });
+ }
+ if (
+ !isLoadingHistory &&
+ livePauseFrame &&
+ livePauseFrame.turn_id === narrative.turnId
+ ) {
+ liveAnchored.push({
+ key: "credential",
+ toolCallId: credentialAnchorToolCallId(
+ narrative,
+ livePauseFrame.anchor_tool_call_id,
+ ),
+ node: (
+ <CredentialCard
+ key={livePauseFrame.resume_token}
+ frame={liveFrameToCardFrame(livePauseFrame)}
+ mode="inline-pause"
+ reloadKey={credentialsReloadKey}
+ resolvedOutcome={pauseCardResolutions[livePauseFrame.resume_token]}
+ onUpdateCredential={(credential) =>
+ openCredentialModal(
+ livePauseFrame,
+ livePauseFrame.turn_id,
+ false,
+ credential,
+ )
+ }
+ // A picked credential (id + name from the fetched list) answers through the typed
+ // resume POST, which origin-binds; the Add-credential CTA (no id) opens the modal.
+ onConnect={(credentialId, name) =>
+ credentialId
+ ? void respondToCredentialPause(
+ livePauseFrame,
+ "connected",
+ credentialId,
+ name,
+ )
+ : openCredentialModal(livePauseFrame, livePauseFrame.turn_id)
+ }
+ onSkip={() => void respondToCredentialPause(livePauseFrame, "skip")}
+ />
+ ),
+ });
+ }
+
const autoBoundReceiptIndexes = useMemo(
() =>
selectAutoBoundReceiptIndexes(
@@ -9442,6 +9531,142 @@ export function WorkflowCopilotChat({
const showReviewGate =
shouldShowDiffCard(message.narrative) ||
(turnId !== null && turnId === pendingProposalTurnId);
+ const credentialCard = (() => {
+ if (isLoadingHistory || turnId === null) return null;
+ const credFrame = credentialCardFrameFor(message.narrative);
+ if (!credFrame) return null;
+ const localResolution = credentialResolutions[turnId];
+ // The persisted pause verdict outranks the optimistic click, so a pick the server
+ // did not admit never reads as connected; the click's name survives via pauseCardResolutions.
+ const resolvedOutcome =
+ historicalCredentialOutcome(
+ message.narrative,
+ pauseCardResolutions,
+ ) ?? localResolution;
+ // The actionable ask is only live on the tail message; a resolved receipt still
+ // renders on any message so a scrolled-back turn keeps its outcome. Without this,
+ // picking on a stale card would show a receipt with no backend call or continue.
+ // A stranded ask (its auto-continue failed) stays actionable off-tail for a retry.
+ if (
+ !resolvedOutcome &&
+ !isLastMessage &&
+ !strandedTerminalContinuations.has(turnId)
+ )
+ return null;
+ return (
+ <CredentialCard
+ frame={credFrame}
+ mode="terminal"
+ reloadKey={credentialsReloadKey}
+ resolvedOutcome={resolvedOutcome}
+ continued={Boolean(localResolution?.continued)}
+ // A picked credential (id + name from the fetched list) auto-continues by
+ // id; the Add-credential CTA (no id) opens the modal instead. A stranded ask
+ // (its prior continue failed) may continue too, though it is no longer the tail.
+ onConnect={(credentialId, name) => {
+ const canContinue =
+ isLastMessage ||
+ strandedTerminalContinuations.has(turnId);
+ return credentialId
+ ? continueAfterTerminalConnect(
+ turnId,
+ credentialId,
+ name ?? localResolution?.name,
+ canContinue,
+ )
+ : openCredentialModal(null, turnId, canContinue);
+ }}
+ onSkip={() => resolveTerminalCredential(turnId, "skip")}
+ />
+ );
+ })();
+ const autoBoundCard = (() => {
+ if (isLoadingHistory || turnId === null) return null;
+ const autoBound = autoBoundReceiptFor(message);
+ if (!autoBound || !autoBoundReceiptIndexes.has(index))
+ return null;
+ // The receipt renders on any message (scrollback-safe). Before a Change it shows
+ // the auto-bound credential with a Change picker; after one, the local resolution
+ // routes into CredentialCard's existing "Continuing with 'X'…" receipt.
+ const localResolution = credentialResolutions[turnId];
+ const canContinue =
+ isLastMessage ||
+ strandedTerminalContinuations.has(turnId);
+ return (
+ <CredentialCard
+ frame={{
+ type: "credential_required",
+ reason: "workflow_credential_inputs_unbound",
+ }}
+ mode="auto-bound"
+ autoBound={autoBound}
+ // Not while a turn is in flight: the shared continue path would still record
+ // an optimistic "connected" even though it suppresses the actual send.
+ canChange={canContinue && !isLoading}
+ reloadKey={credentialsReloadKey}
+ resolvedOutcome={localResolution}
+ continued={Boolean(localResolution?.continued)}
+ // Change re-enters the same terminal-continue path a terminal ask pick uses
+ // (send "Use the credential <id> — continue", or open the add modal). A silent
+ // bind never has a live pause, so never the typed credential-response path.
+ onConnect={(credentialId, name) =>
+ credentialId
+ ? continueAfterTerminalConnect(
+ turnId,
+ credentialId,
+ name ?? localResolution?.name,
+ canContinue,
+ )
+ : openCredentialModal(null, turnId, canContinue)
+ }
+ onSkip={() => {}}
+ />
+ );
+ })();
+ const credentialPause = message.narrative.credentialPause;
+ // A pause resolved mid-turn renders where it was raised. A terminal ask ended the
+ // turn, so it stays last and actionable.
+ const credentialCardMidTurn =
+ credentialPause !== null &&
+ credentialPause.outcome !== "declined";
+ const turnPlan = turnPlans[index];
+ const anchored: AnchoredTurnItem[] = [];
+ if (turnPlan) {
+ anchored.push({
+ key: `plan-${turnPlan.toolCallId}`,
+ toolCallId: turnPlan.toolCallId,
+ node: (
+ <WorkPlanCard
+ items={turnPlan.items}
+ previous={planBefore(index)}
+ current={
+ liveTurnPlan === null &&
+ index === lastTurnPlanIndex &&
+ !messages
+ .slice(index + 1)
+ .some((later) => later.sender === "user")
+ }
+ />
+ ),
+ });
+ }
+ if (credentialCard && credentialCardMidTurn) {
+ anchored.push({
+ key: "credential",
+ toolCallId: credentialAnchorToolCallId(
+ message.narrative,
+ credentialPause.anchorToolCallId,
+ ),
+ node: credentialCard,
+ });
+ }
+ if (autoBoundCard) {
+ anchored.push({
+ key: "credential-auto-bound",
+ toolCallId: credentialAnchorToolCallId(message.narrative),
+ node: autoBoundCard,
+ });
+ }
return (
<div
key={message.id}
@@ -9456,6 +9681,7 @@ export function WorkflowCopilotChat({
beforeProse={
turnQuestions.length > 0 ? turnQuestions : undefined
}
+ anchored={anchored}
/>
{message.narrative.outputFiles.length > 0 ? (
<div className="flex flex-wrap gap-2">
@@ -9588,106 +9814,7 @@ export function WorkflowCopilotChat({
}}
/>
) : null}
- {(() => {
- if (isLoadingHistory || turnId === null) return null;
- const credFrame = credentialCardFrameFor(
- message.narrative,
- );
- if (!credFrame) return null;
- const localResolution = credentialResolutions[turnId];
- // The persisted pause verdict outranks the optimistic click, so a pick the server
- // did not admit never reads as connected; the click's name survives via pauseCardResolutions.
- const resolvedOutcome =
- historicalCredentialOutcome(
- message.narrative,
- pauseCardResolutions,
- ) ?? localResolution;
- // The actionable ask is only live on the tail message; a resolved receipt still
- // renders on any message so a scrolled-back turn keeps its outcome. Without this,
- // picking on a stale card would show a receipt with no backend call or continue.
- // A stranded ask (its auto-continue failed) stays actionable off-tail for a retry.
- if (
- !resolvedOutcome &&
- !isLastMessage &&
- !strandedTerminalContinuations.has(turnId)
- )
- return null;
- return (
- <CredentialCard
- frame={credFrame}
- mode="terminal"
- reloadKey={credentialsReloadKey}
- resolvedOutcome={resolvedOutcome}
- continued={Boolean(localResolution?.continued)}
- // A picked credential (id + name from the fetched list) auto-continues by
- // id; the Add-credential CTA (no id) opens the modal instead. A stranded ask
- // (its prior continue failed) may continue too, though it is no longer the tail.
- onConnect={(credentialId, name) => {
- const canContinue =
- isLastMessage ||
- strandedTerminalContinuations.has(turnId);
- return credentialId
- ? continueAfterTerminalConnect(
- turnId,
- credentialId,
- name ?? localResolution?.name,
- canContinue,
- )
- : openCredentialModal(
- null,
- turnId,
- canContinue,
- );
- }}
- onSkip={() =>
- resolveTerminalCredential(turnId, "skip")
- }
- />
- );
- })()}
- {(() => {
- if (isLoadingHistory || turnId === null) return null;
- const autoBound = autoBoundReceiptFor(message);
- if (!autoBound || !autoBoundReceiptIndexes.has(index))
- return null;
- // The receipt renders on any message (scrollback-safe). Before a Change it shows
- // the auto-bound credential with a Change picker; after one, the local resolution
- // routes into CredentialCard's existing "Continuing with 'X'…" receipt.
- const localResolution = credentialResolutions[turnId];
- const canContinue =
- isLastMessage ||
- strandedTerminalContinuations.has(turnId);
- return (
- <CredentialCard
- frame={{
- type: "credential_required",
- reason: "workflow_credential_inputs_unbound",
- }}
- mode="auto-bound"
- autoBound={autoBound}
- // Not while a turn is in flight: the shared continue path would still record
- // an optimistic "connected" even though it suppresses the actual send.
- canChange={canContinue && !isLoading}
- reloadKey={credentialsReloadKey}
- resolvedOutcome={localResolution}
- continued={Boolean(localResolution?.continued)}
- // Change re-enters the same terminal-continue path a terminal ask pick uses
- // (send "Use the credential <id> — continue", or open the add modal). A silent
- // bind never has a live pause, so never the typed credential-response path.
- onConnect={(credentialId, name) =>
- credentialId
- ? continueAfterTerminalConnect(
- turnId,
- credentialId,
- name ?? localResolution?.name,
- canContinue,
- )
- : openCredentialModal(null, turnId, canContinue)
- }
- onSkip={() => {}}
- />
- );
- })()}
+ {credentialCardMidTurn ? null : credentialCard}
{message.narrative.terminal ? feedbackControl : null}
</div>
);
@@ -9883,46 +10010,8 @@ export function WorkflowCopilotChat({
turn={narrative}
onBlockSelect={onBlockSelect}
workingRowActive={showWorkingRow}
+ anchored={liveAnchored}
/>
- {!isLoadingHistory &&
- livePauseFrame &&
- livePauseFrame.turn_id === narrative.turnId ? (
- <CredentialCard
- key={livePauseFrame.resume_token}
- frame={liveFrameToCardFrame(livePauseFrame)}
- mode="inline-pause"
- reloadKey={credentialsReloadKey}
- resolvedOutcome={
- pauseCardResolutions[livePauseFrame.resume_token]
- }
- onUpdateCredential={(credential) =>
- openCredentialModal(
- livePauseFrame,
- livePauseFrame.turn_id,
- false,
- credential,
- )
- }
- // A picked credential (id + name from the fetched list) answers through the typed
- // resume POST, which origin-binds; the Add-credential CTA (no id) opens the modal.
- onConnect={(credentialId, name) =>
- credentialId
- ? void respondToCredentialPause(
- livePauseFrame,
- "connected",
- credentialId,
- name,
- )
- : openCredentialModal(
- livePauseFrame,
- livePauseFrame.turn_id,
- )
- }
- onSkip={() =>
- void respondToCredentialPause(livePauseFrame, "skip")
- }
- />
- ) : null}
</div>
)}
{questionInteractions
@@ -9933,7 +10022,10 @@ export function WorkflowCopilotChat({
),
)
.map(renderQuestionReceipt)}
- <WorkPlanCard items={workPlan} />
+ {/* Turns recorded before plans rode their own tool call carry only the chat's latest plan. */}
+ {lastTurnPlanIndex === -1 && liveTurnPlan === null ? (
+ <WorkPlanCard items={workPlan} />
+ ) : null}
{recordingSuggestionHost}
</div>
</div>
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.workPlan.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.workPlan.test.tsx
index 9a4fc108a..098ea47ff 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.workPlan.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.workPlan.test.tsx
@@ -280,3 +280,182 @@ describe("WorkflowCopilotChat — work plan liveness", () => {
expect(screen.getByText("reach the payment step")).toBeTruthy();
});
});
+
+describe("WorkflowCopilotChat — a plan renders in the turn that wrote it", () => {
+ const planTurn = (
+ turnId: string,
+ callId: string,
+ items: string[],
+ summary: string,
+ createdAt: string,
+ ) => ({
+ sender: "ai",
+ content: summary,
+ created_at: createdAt,
+ turn_outcome: { copilot_turn_id: turnId, terminal_reason: "completed" },
+ narrative_payload: {
+ turnId,
+ turnIndex: 0,
+ designStarted: true,
+ designEnded: true,
+ draft: { blockCount: 1, blockLabels: ["search"] },
+ blocks: [],
+ terminal: "response",
+ terminalMessage: summary,
+ narrativeSummary: summary,
+ designActivity: [
+ {
+ kind: "tool_call",
+ id: `tc-${callId}`,
+ text: "Updating its plan…",
+ toolName: "set_work_plan",
+ iteration: 0,
+ },
+ {
+ kind: "tool_result",
+ id: `tr-${callId}`,
+ text: "Updated its plan",
+ toolName: "set_work_plan",
+ iteration: 0,
+ success: true,
+ },
+ ],
+ workPlan: { toolCallId: callId, items },
+ },
+ });
+
+ it("places each revision after its own plan row, folds the one it replaced, and drops the chat-level card", async () => {
+ historyResponse.data = {
+ ...historyResponse.data,
+ workflow_copilot_chat_id: "chat-1",
+ work_plan: ["scout the search page", "pay with the saved card"],
+ chat_history: [
+ {
+ sender: "user",
+ content: "start",
+ created_at: "2026-09-04T00:00:00Z",
+ },
+ planTurn(
+ "turn-1",
+ "p1",
+ ["scout the search page", "reach the payment step"],
+ "Drafted the search.",
+ "2026-09-04T00:00:01Z",
+ ),
+ {
+ sender: "user",
+ content: "use the saved card",
+ created_at: "2026-09-04T00:00:02Z",
+ },
+ planTurn(
+ "turn-2",
+ "p2",
+ ["scout the search page", "pay with the saved card"],
+ "Switched to the saved card.",
+ "2026-09-04T00:00:03Z",
+ ),
+ ],
+ };
+
+ await renderChat();
+
+ const summary = await screen.findByText("Switched to the saved card.");
+ const revision = screen.getByText("pay with the saved card");
+ const precedes = (a: Node, b: Node) =>
+ Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING);
+ const planRow = document.querySelector('[data-activity-row-id="p2"]')!;
+ expect(precedes(planRow, revision)).toBe(true);
+ expect(precedes(revision, summary)).toBe(true);
+ expect(
+ screen.getAllByRole("group", { name: "Copilot's plan" }),
+ ).toHaveLength(2);
+ // The first plan folded when the user answered it; its step shows only as the revision's removal.
+ expect(screen.getAllByText("reach the payment step")).toHaveLength(1);
+ expect(screen.getByText("Plan updated")).toBeTruthy();
+ });
+
+ it("shows a live plan after its row and keeps it once the turn ends, even when the cap trimmed that row", async () => {
+ await renderChat();
+ await submitTurn("book a seat");
+ const plan = ["scout the search page", "reach the payment step"];
+ const call = (id: string, toolName: string) => ({
+ tool_name: toolName,
+ display_label: toolName,
+ iteration: 0,
+ tool_call_id: id,
+ });
+ await act(async () => {
+ streamCalls[0]!.onMessage({
+ type: "turn_start",
+ turn_id: "turn-1",
+ turn_index: 0,
+ mode: "build",
+ timestamp: "2026-09-04T00:00:00Z",
+ });
+ streamCalls[0]!.onMessage({ type: "design_start" });
+ for (const [id, toolName] of [
+ ["p1", "set_work_plan"],
+ ["w1", "update_workflow"],
+ ]) {
+ streamCalls[0]!.onMessage({
+ type: "tool_call",
+ tool_input: {},
+ ...call(id!, toolName!),
+ });
+ streamCalls[0]!.onMessage({
+ type: "tool_result",
+ success: true,
+ summary: "done",
+ work_plan: toolName === "set_work_plan" ? plan : null,
+ ...call(id!, toolName!),
+ });
+ }
+ });
+ const precedes = (a: Node, b: Node) =>
+ Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING);
+ const row = (id: string) =>
+ document.querySelector(`[data-activity-row-id="${id}"]`)!;
+ const livePlan = await screen.findByRole("group", {
+ name: "Copilot's plan",
+ });
+ expect(precedes(row("p1"), livePlan)).toBe(true);
+ expect(precedes(livePlan, row("w1"))).toBe(true);
+
+ await act(async () => {
+ streamCalls[0]!.onMessage({
+ ...terminalResponse(plan),
+ turn_id: "turn-1",
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ designStarted: true,
+ designEnded: true,
+ draft: { blockCount: 1, blockLabels: ["search"] },
+ blocks: [],
+ terminal: "response",
+ terminalMessage: "Saved a draft.",
+ narrativeSummary: "Saved a draft.",
+ designActivity: [
+ {
+ kind: "tool_result",
+ id: "tr-w1",
+ text: "Saved the draft",
+ toolName: "update_workflow",
+ iteration: 0,
+ success: true,
+ },
+ ],
+ workPlan: { toolCallId: "p1", items: plan },
+ },
+ });
+ streamCalls[0]!.resolve();
+ });
+ const summary = await screen.findByText("Saved a draft.");
+ const [finalPlan, ...extra] = screen.getAllByRole("group", {
+ name: "Copilot's plan",
+ });
+ expect(extra).toHaveLength(0);
+ expect(precedes(finalPlan!, summary)).toBe(true);
+ expect(screen.getByText("reach the payment step")).toBeTruthy();
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/CredentialCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/CredentialCard.tsx
index 3544ea8b2..61c4b4ea4 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/cards/CredentialCard.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/CredentialCard.tsx
@@ -31,6 +31,7 @@ import {
} from "@/routes/workflows/hooks/useCredentialQuery";
import {
+ AppliedCheck,
CardBody,
CardFooter,
CardHeader,
@@ -594,12 +595,7 @@ function ResolvedCredentialCard({
!
</span>
) : (
- <span
- aria-hidden="true"
- className="text-xs font-bold text-emerald-600 dark:text-emerald-400"
- >
- ✓
- </span>
+ <AppliedCheck />
)
}
title={title}
@@ -1017,14 +1013,7 @@ function AutoBoundReceipt({
return (
<CopilotCard>
<CardHeader
- icon={
- <span
- aria-hidden="true"
- className="text-xs font-bold text-emerald-600 dark:text-emerald-400"
- >
- ✓
- </span>
- }
+ icon={<AppliedCheck />}
title={<span title={name}>Using credential &apos;{name}&apos;</span>}
actions={change}
expanded={expanded}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.test.tsx
index 23c48fc91..98d38a772 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.test.tsx
@@ -400,6 +400,10 @@ describe("ReviewGateCard — Test end-to-end recourse", () => {
rerender(gate("reload"));
expect(screen.queryByRole("button", { name: "Run test" })).toBeNull();
+ // The row that opened it is locked now, so focus lands on the one live control.
+ expect(document.activeElement).toBe(
+ screen.getByRole("button", { name: "Try again" }),
+ );
const more = screen.getByRole("button", { name: "More actions" });
expect(more.hasAttribute("disabled")).toBe(true);
openMenu("More actions");
@@ -549,10 +553,7 @@ describe("ReviewGateCard — block label humanization", () => {
fireEvent.click(screen.getByRole("button", { expanded: false }));
const proposed = screen.getByTitle("extract_titles_v2");
expect(proposed.textContent).toBe("Extract Titles");
- // A legacy draft has no change classes, so its row carries no marker or change label.
- expect(proposed.parentElement?.previousElementSibling?.textContent).toBe(
- "",
- );
+ // A legacy draft has no change classes, so its row announces no change label.
expect(proposed.parentElement?.textContent).toBe("Extract Titles");
expect(screen.queryByText("Old Extract Step")).toBeNull();
});
@@ -610,13 +611,15 @@ describe("ReviewGateCard — recorded review projection", () => {
/>,
);
- const markerOf = (label: string) =>
- screen.getByTitle(label).parentElement?.previousElementSibling
- ?.textContent;
- expect(markerOf("added_export")).toBe("+");
- expect(markerOf("changed_query")).toBe("~");
- expect(markerOf("unchanged_login")).toBe("");
- expect(markerOf("removed_cleanup")).toBe("\u2212");
+ // The +/~/− glyphs are decoration; the change class a reader gets is the announced label.
+ const announced = (text: string) =>
+ screen.queryAllByText(
+ (_, el) => el?.textContent?.startsWith(text) ?? false,
+ ).length > 0;
+ expect(announced("Added: Added Export")).toBe(true);
+ expect(announced("Changed: Changed Query")).toBe(true);
+ expect(announced("Unchanged: Unchanged Login")).toBe(true);
+ expect(announced("Removed: Removed Cleanup")).toBe(true);
expect(screen.getByTitle("removed_cleanup").className).toContain(
"line-through",
);
diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx
index 3ee917cb3..924852714 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx
@@ -23,6 +23,7 @@ import {
ranCleanOnCurrentSource,
} from "../narrativeState";
import {
+ AppliedCheck,
CardBody,
CardFooter,
CardHeader,
@@ -423,12 +424,7 @@ function ResolvedReviewCard({
↺
</span>
) : applied ? (
- <span
- aria-hidden="true"
- className="text-xs font-bold text-emerald-600 dark:text-emerald-400"
- >
- ✓
- </span>
+ <AppliedCheck />
) : (
<MagicWandIcon
aria-hidden="true"
@@ -498,6 +494,7 @@ export function ReviewGateCard({
const runTestRef = useRef<HTMLButtonElement>(null);
const moreTriggerRef = useRef<HTMLButtonElement>(null);
const bodyTestRef = useRef<HTMLButtonElement>(null);
+ const retryRef = useRef<HTMLButtonElement>(null);
// The confirmation replaces the row that opened it, so focus is placed by hand both ways.
const restoreFocusRef = useRef(false);
const selectingTestRef = useRef(false);
@@ -506,7 +503,14 @@ export function ReviewGateCard({
runTestRef.current?.focus();
} else if (restoreFocusRef.current) {
restoreFocusRef.current = false;
- (moreTriggerRef.current ?? bodyTestRef.current)?.focus();
+ // Only when focus went down with the confirmation; a lock may land while it is elsewhere.
+ if (document.activeElement && document.activeElement !== document.body) {
+ return;
+ }
+ // Under a lock the row that opened the confirmation is disabled, so Try again takes focus.
+ [moreTriggerRef.current, bodyTestRef.current, retryRef.current]
+ .find((el) => el && !el.matches(":disabled"))
+ ?.focus();
}
}, [confirmingTest]);
const closeConfirmation = () => {
@@ -566,6 +570,7 @@ export function ReviewGateCard({
failure === "saved";
// A lock that lands mid-confirmation would strand it behind the disabled fieldset.
if (confirmingTest && actionsLocked) {
+ restoreFocusRef.current = true;
setConfirmingTest(false);
}
const showTestInBody = showActions && offerTestInBody && !confirmingTest;
@@ -728,6 +733,7 @@ export function ReviewGateCard({
size="sm"
variant="outline"
disabled={accepting}
+ ref={retryRef}
onClick={onRetry}
className="shrink-0"
>
diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/WorkPlanCard.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/WorkPlanCard.test.tsx
index 3687ee0b2..42a4523da 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/cards/WorkPlanCard.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/WorkPlanCard.test.tsx
@@ -1,12 +1,12 @@
// @vitest-environment jsdom
-import { cleanup, render, screen } from "@testing-library/react";
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { WorkPlanCard } from "./WorkPlanCard";
afterEach(cleanup);
describe("model-owned work plan", () => {
- it("renders each item exactly as the model wrote it", () => {
+ it("renders each item exactly as the model wrote it, in order", () => {
const items = [
"continue past result selection",
"reach the payment step",
@@ -15,6 +15,33 @@ describe("model-owned work plan", () => {
render(<WorkPlanCard items={items} />);
expect(
screen.getAllByRole("listitem").map((entry) => entry.textContent),
- ).toEqual(items.map((item, index) => `${index + 1}.${item}`));
+ ).toEqual(items.map((item, index) => `${index + 1}${item}`));
+ });
+
+ it("marks what a revision added and removed, and folds once it is no longer current", () => {
+ const { rerender } = render(
+ <WorkPlanCard
+ items={["scout the search page", "pay with the saved card"]}
+ previous={["scout the search page", "reach the payment step"]}
+ />,
+ );
+ expect(screen.getByText("Plan updated")).toBeTruthy();
+ expect(screen.getByText(/1 new · 1 removed/)).toBeTruthy();
+ const added = screen.getByText("pay with the saved card").closest("li")!;
+ expect(added.textContent).toContain("New");
+ expect(screen.getByText("reach the payment step").className).toContain(
+ "line-through",
+ );
+
+ rerender(
+ <WorkPlanCard
+ items={["scout the search page", "pay with the saved card"]}
+ previous={["scout the search page", "reach the payment step"]}
+ current={false}
+ />,
+ );
+ expect(screen.queryByText("pay with the saved card")).toBeNull();
+ fireEvent.click(screen.getByRole("button", { expanded: false }));
+ expect(screen.getByText("pay with the saved card")).toBeTruthy();
});
});
diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/WorkPlanCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/WorkPlanCard.tsx
index 600a37fc0..bfba2eb81 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/cards/WorkPlanCard.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/WorkPlanCard.tsx
@@ -1,36 +1,97 @@
import { ListBulletIcon } from "@radix-ui/react-icons";
+import { useEffect, useState } from "react";
+
+import { CardBody, CardHeader, CopilotCard, GutterRow } from "./cardChrome";
interface WorkPlanCardProps {
items: string[];
+ // The plan this one replaced. A step counts as new only when no earlier step reads exactly the
+ // same: a reworded step is not recognizable without guessing, so it shows as new plus removed.
+ previous?: string[] | null;
+ // The plan in force with nothing sent since. It arrives open and folds once the user moves on.
+ current?: boolean;
+}
+
+function stepCount(count: number): string {
+ return `${count} ${count === 1 ? "step" : "steps"}`;
}
-export function WorkPlanCard({ items }: WorkPlanCardProps) {
+export function WorkPlanCard({
+ items,
+ previous = null,
+ current = true,
+}: WorkPlanCardProps) {
+ const [expanded, setExpanded] = useState(current);
+ useEffect(() => {
+ if (!current) setExpanded(false);
+ }, [current]);
+
+ const earlier = new Set(previous ?? []);
+ const kept = new Set(items);
+ const added = previous === null ? [] : items.filter((i) => !earlier.has(i));
+ const removed = (previous ?? []).filter((item) => !kept.has(item));
+
if (items.length === 0) {
- return null;
+ if (removed.length === 0) return null;
+ return (
+ <CopilotCard>
+ <CardHeader
+ icon={
+ <ListBulletIcon className="h-3.5 w-3.5 text-muted-foreground" />
+ }
+ title="Plan cleared"
+ />
+ </CopilotCard>
+ );
}
+
+ const changes = [
+ added.length > 0 ? `${added.length} new` : null,
+ removed.length > 0 ? `${removed.length} removed` : null,
+ ].filter(Boolean);
return (
- <div
- role="group"
- aria-label="Copilot's plan"
- className="overflow-hidden rounded-[10px] border border-border bg-slate-elevation2"
- >
- <div className="flex items-center gap-2 px-3 pt-3 text-xs font-semibold text-foreground">
- <ListBulletIcon className="h-3.5 w-3.5" />
- Copilot&apos;s plan
+ <CopilotCard>
+ <div role="group" aria-label="Copilot's plan">
+ <CardHeader
+ icon={
+ <ListBulletIcon className="h-3.5 w-3.5 text-muted-foreground" />
+ }
+ title={changes.length > 0 ? "Plan updated" : "Plan"}
+ meta={[stepCount(items.length), ...changes].join(" · ")}
+ expanded={expanded}
+ onToggle={() => setExpanded((value) => !value)}
+ />
+ {expanded ? (
+ <CardBody>
+ <ol className="max-h-[60vh] overflow-y-auto">
+ {items.map((item, index) => (
+ <li key={`${index}-${item}`}>
+ <GutterRow marker={index + 1}>
+ <span className="whitespace-pre-wrap">{item}</span>
+ {added.includes(item) ? (
+ <span className="ml-2 whitespace-nowrap rounded-full bg-sky-500/10 px-1.5 py-0.5 text-[10px] font-medium text-sky-700 dark:text-sky-300">
+ New
+ </span>
+ ) : null}
+ </GutterRow>
+ </li>
+ ))}
+ </ol>
+ {removed.map((item) => (
+ <GutterRow
+ key={`removed-${item}`}
+ marker="−"
+ markerClass="text-destructive"
+ srLabel="Removed"
+ >
+ <span className="whitespace-pre-wrap text-muted-foreground line-through">
+ {item}
+ </span>
+ </GutterRow>
+ ))}
+ </CardBody>
+ ) : null}
</div>
- <ol className="max-h-[60vh] overflow-y-auto px-3 pb-3">
- {items.map((item, index) => (
- <li
- key={`${index}-${item}`}
- className="mt-2 flex gap-2 text-xs text-foreground"
- >
- <span className="shrink-0 text-muted-foreground">{index + 1}.</span>
- <span className="min-w-0 whitespace-pre-wrap break-words">
- {item}
- </span>
- </li>
- ))}
- </ol>
- </div>
+ </CopilotCard>
);
}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/cardChrome.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/cardChrome.tsx
index e6b14eed2..251ab01b0 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/cards/cardChrome.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/cardChrome.tsx
@@ -5,7 +5,7 @@ import { cn } from "@/util/utils";
import { ICON_COLUMN } from "./cardLayout";
-export type PillTone = "green" | "amber" | "sky" | "red";
+export type PillTone = "green" | "amber" | "sky";
const PILL_TONE_CLASSES: Record<PillTone, string> = {
green:
@@ -13,7 +13,6 @@ const PILL_TONE_CLASSES: Record<PillTone, string> = {
amber:
"border-amber-500/30 bg-amber-500/15 text-amber-700 dark:text-amber-300",
sky: "border-sky-500/30 bg-sky-500/15 text-sky-700 dark:text-sky-300",
- red: "border-red-500/30 bg-red-500/15 text-red-700 dark:text-red-300",
};
export function CardPill({
@@ -32,6 +31,17 @@ export function CardPill({
);
}
+export function AppliedCheck() {
+ return (
+ <span
+ aria-hidden="true"
+ className="text-xs font-bold text-emerald-600 dark:text-emerald-400"
+ >
+ ✓
+ </span>
+ );
+}
+
export function CopilotCard({
id,
className,
diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/cardLayout.ts b/skyvern-frontend/src/routes/workflows/copilot/cards/cardLayout.ts
index 963c331ca..938e0365c 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/cards/cardLayout.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/cardLayout.ts
@@ -1,6 +1,8 @@
// A card's 1px border plus px-3 puts its content 13px in, so the turn's other rows take the same
// inset and every glyph and text edge in a turn lines up. A margin, so it composes with row padding.
export const TURN_ROW_INSET = "mx-[13px]";
+// Undoes TURN_ROW_INSET for a card placed inside an inset row list, so the card keeps its full width.
+export const TURN_ROW_OUTSET = "-mx-[13px]";
export const ICON_COLUMN = "flex w-[18px] shrink-0 justify-center";
export const ACCEPT_BUTTON_CLASS =
diff --git a/skyvern-frontend/src/routes/workflows/copilot/copilotActivityLog.ts b/skyvern-frontend/src/routes/workflows/copilot/copilotActivityLog.ts
index 60ebd8037..ce725639d 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/copilotActivityLog.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/copilotActivityLog.ts
@@ -287,6 +287,20 @@ function rowContainsNarrationTime(
return narrationMs >= startedMs && narrationMs <= endedMs;
}
+// The row holding a tool call's activity, as a row entry or on a block the row ran. -1 when the call
+// has no row: it aged out past the activity cap, or its frames never reached this turn.
+export function rowIndexOfToolCall(
+ rows: ActivityRow[],
+ toolCallId: string,
+): number {
+ const ofCall = (entry: ActivityEntry) => toolCallIdOf(entry) === toolCallId;
+ return rows.findIndex(
+ (row) =>
+ row.entries.some(ofCall) ||
+ row.blocks.some((block) => block.activity.some(ofCall)),
+ );
+}
+
export function deriveActivityLog(turn: TurnNarrativeState): ActivityLog {
const rows: ActivityRow[] = [];
const narrations: { entry: ActivityEntry; precedingRow: number }[] = [];
diff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
index e0a97c2b9..71c808cf3 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
@@ -423,10 +423,16 @@ export interface TurnNarrativeState {
credentialPause: {
outcome: "connected" | "skipped" | "timeout" | "declined";
credentialId: string | null;
+ // The tool call whose row was newest when the card was raised. Absent on
+ // turns recorded before it was stamped.
+ anchorToolCallId?: string;
} | null;
// Silently auto-bound credential, from the credentialAutoBound narrative signal — rendered as a
// receipt with a Change affordance so a confident-but-wrong pick can be corrected after the fact.
credentialAutoBound: { credentialId: string; name: string } | null;
+ // The last plan a successful set_work_plan stored this turn, kept apart from the activity rows
+ // because their cap can trim the call's row in a long turn.
+ workPlan?: TurnWorkPlan | null;
connectedAccountChoices: ConnectedAccountChoice[];
outputFiles: DeliveredOutputFile[];
googleConnectionNotices: GoogleConnectionNotice[];
@@ -519,9 +525,13 @@ export function parseCredentialPause(
if (!value || typeof value !== "object") return null;
const o = value as Record<string, unknown>;
const outcome = o.outcome;
+ const anchorToolCallId =
+ typeof o.anchorToolCallId === "string" && o.anchorToolCallId
+ ? o.anchorToolCallId
+ : undefined;
// The user answered but nothing was bound, which the card already renders as a skip.
if (outcome === "not_admitted") {
- return { outcome: "skipped", credentialId: null };
+ return { outcome: "skipped", credentialId: null, anchorToolCallId };
}
if (
outcome !== "connected" &&
@@ -534,9 +544,30 @@ export function parseCredentialPause(
return {
outcome,
credentialId: typeof o.credentialId === "string" ? o.credentialId : null,
+ anchorToolCallId,
};
}
+export interface TurnWorkPlan {
+ toolCallId: string;
+ items: string[];
+}
+
+function parseWorkPlanItems(value: unknown): string[] | null {
+ return Array.isArray(value) && value.every((item) => typeof item === "string")
+ ? [...value]
+ : null;
+}
+
+function parseTurnWorkPlan(value: unknown): TurnWorkPlan | null {
+ if (!value || typeof value !== "object") return null;
+ const o = value as Record<string, unknown>;
+ const items = parseWorkPlanItems(o.items);
+ return typeof o.toolCallId === "string" && o.toolCallId && items
+ ? { toolCallId: o.toolCallId, items }
+ : null;
+}
+
export function parseCodeDiffs(value: unknown): CodeWriteDiff[] | undefined {
if (!Array.isArray(value)) return undefined;
const diffs: CodeWriteDiff[] = [];
@@ -1350,8 +1381,12 @@ export function applyNarrativeEvent(
}
case "tool_result": {
+ const planItems = parseWorkPlanItems(event.work_plan);
+ const workPlan = planItems
+ ? { toolCallId: event.tool_call_id, items: planItems }
+ : prev.workPlan;
const entry = buildActivityFromToolResult(event);
- if (!entry) return { ...prev };
+ if (!entry) return { ...prev, workPlan };
const { blocks, designActivity } = appendActivity(
prev.blocks,
prev.designActivity,
@@ -1361,6 +1396,7 @@ export function applyNarrativeEvent(
...prev,
blocks,
designActivity,
+ workPlan,
};
}
@@ -1791,6 +1827,7 @@ export function hydrateNarrativeFromPayload(
credentialPrompt: parseCredentialPrompt(payload.credentialPrompt),
credentialPause: parseCredentialPause(payload.credentialPause),
credentialAutoBound: parseCredentialAutoBound(payload.credentialAutoBound),
+ workPlan: parseTurnWorkPlan(payload.workPlan),
connectedAccountChoices: parseConnectedAccountChoices(
payload.connectedAccountChoices,
),
diff --git a/skyvern-frontend/src/routes/workflows/copilot/turnCardPlacement.ts b/skyvern-frontend/src/routes/workflows/copilot/turnCardPlacement.ts
new file mode 100644
index 000000000..87ee2d91e
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/copilot/turnCardPlacement.ts
@@ -0,0 +1,34 @@
+import {
+ ActivityEntry,
+ TurnNarrativeState,
+ toolCallIdOf,
+} from "./narrativeState";
+
+// A call made while a block runs is filed under that block, not the turn's own activity.
+function turnActivity(turn: TurnNarrativeState): ActivityEntry[] {
+ return [
+ ...turn.designActivity,
+ ...turn.blocks.flatMap((block) => block.activity),
+ ];
+}
+
+// Mirror of the backend's credential tool names (tools/__init__.py).
+const CREDENTIAL_TOOLS = new Set([
+ "request_credential",
+ "fill_credential_field",
+]);
+
+// Where a credential receipt renders: the call recorded with it, else the turn's first credential
+// tool call, else nowhere in the log (null), which places it above the turn's reply.
+export function credentialAnchorToolCallId(
+ turn: TurnNarrativeState,
+ recorded?: string | null,
+): string | null {
+ if (recorded) return recorded;
+ const entry = turnActivity(turn).find(
+ (candidate) =>
+ candidate.toolName !== undefined &&
+ CREDENTIAL_TOOLS.has(candidate.toolName),
+ );
+ return (entry && toolCallIdOf(entry)) ?? null;
+}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
index 13eae7821..2b2da5fab 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
@@ -422,6 +422,9 @@ export interface WorkflowCopilotCredentialRequiredUpdate {
credential_refs: string[];
timeout_seconds: number;
expires_at: string;
+ // The tool call whose activity row was newest when the pause was raised, so
+ // the card renders there. Absent against a backend that predates it.
+ anchor_tool_call_id?: string | null;
timestamp: string;
}
@@ -465,6 +468,8 @@ export interface WorkflowCopilotToolResultUpdate {
iteration: number;
tool_call_id: string;
code_diffs?: CodeWriteDiff[] | null;
+ // The plan a successful set_work_plan stored. Absent on every other tool.
+ work_plan?: string[] | null;
detail?: string | null;
timestamp?: string | null;
}
diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
index 32a44d938..60fa04c69 100644
--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
@@ -3451,16 +3451,9 @@ function Workspace({
);
const currentVersion: WorkflowVersion = {
...baseVersion,
- title: "Current",
workflow_definition: baseConversion.data.workflow_definition,
};
- // Construct fake WorkflowVersion for pending copilot suggestion
- const pendingVersion: WorkflowVersion = {
- ...pendingWorkflow,
- title: "Copilot Suggestion",
- };
-
// Approve / Reject settle through the chat so the server-side
// proposal is cleared (revision-checked) and the canvas updated
// exactly as the chat's own buttons do. A refused decision keeps the
@@ -3496,7 +3489,7 @@ function Workspace({
content: "history",
data: {
version1: currentVersion,
- version2: pendingVersion,
+ version2: pendingWorkflow,
showComparison: true,
mode: "copilot",
onCopilotReviewClose: handleCopilotReviewClose,
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/ActionNode/ActionNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/ActionNode/ActionNode.tsx
index 1f7d6d0dc..a3b7a6756 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/ActionNode/ActionNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/ActionNode/ActionNode.tsx
@@ -59,7 +59,6 @@ function ActionNode({ id, data, type }: NodeProps<ActionNode>) {
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockNode.tsx
index 1f8b52bb2..3774876dc 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockNode.tsx
@@ -55,7 +55,6 @@ function CodeBlockNode({ id, data }: NodeProps<CodeBlockNode>) {
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/ConditionalNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/ConditionalNode.tsx
index 02bf03431..3b80dfb7c 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/ConditionalNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/ConditionalNode.tsx
@@ -145,7 +145,6 @@ function ConditionalNodeComponent({ id, data }: NodeProps<ConditionalNode>) {
<div
className={cn(
"w-[30rem] rounded-lg bg-slate-elevation3 px-6 py-4 shadow-sm transition-shadow motion-reduce:transition-none",
- data.comparisonColor,
)}
>
<NodeHeader
@@ -188,7 +187,6 @@ function ConditionalNodeComponent({ id, data }: NodeProps<ConditionalNode>) {
className={cn(
"w-[30rem] space-y-4 rounded-lg bg-slate-elevation3 px-6 py-4 transition-shadow motion-reduce:transition-none",
open ? "shadow-md" : "shadow-sm",
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/ExtractionNode/ExtractionNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/ExtractionNode/ExtractionNode.tsx
index ab7b80cf3..5cf8ebd3b 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/ExtractionNode/ExtractionNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/ExtractionNode/ExtractionNode.tsx
@@ -59,7 +59,6 @@ function ExtractionNode({ id, data, type }: NodeProps<ExtractionNode>) {
"pointer-events-none bg-background": thisBlockIsPlaying,
"outline outline-2 outline-ring": thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/HumanInteractionNode/HumanInteractionNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/HumanInteractionNode/HumanInteractionNode.tsx
index 226ace467..b4746cdc9 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/HumanInteractionNode/HumanInteractionNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/HumanInteractionNode/HumanInteractionNode.tsx
@@ -57,7 +57,6 @@ function HumanInteractionNode({
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/LoginNode/LoginNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/LoginNode/LoginNode.tsx
index e6e78437c..20ea57f65 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/LoginNode/LoginNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/LoginNode/LoginNode.tsx
@@ -59,7 +59,6 @@ function LoginNode({ id, data, type }: NodeProps<LoginNode>) {
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/LoopNode/LoopNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/LoopNode/LoopNode.tsx
index 9fd61565d..a02a1671c 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/LoopNode/LoopNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/LoopNode/LoopNode.tsx
@@ -174,7 +174,6 @@ function LoopNode({ id, data }: NodeProps<LoopNode>) {
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
@@ -226,7 +225,6 @@ function LoopNode({ id, data }: NodeProps<LoopNode>) {
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/NavigationNode/NavigationNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/NavigationNode/NavigationNode.tsx
index b507357b1..309e610fe 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/NavigationNode/NavigationNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/NavigationNode/NavigationNode.tsx
@@ -63,7 +63,6 @@ function NavigationNode({ id, data, type }: NodeProps<NavigationNode>) {
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/StartNode/StartNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/StartNode/StartNode.tsx
index 1bf4e66ee..f8a327544 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/StartNode/StartNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/StartNode/StartNode.tsx
@@ -27,6 +27,11 @@ import { BuildModeOnly } from "../BuildModeOnly";
import { isLoopNode } from "../LoopNode/types";
import { WorkflowInputsSection } from "../../WorkflowInputs";
import { WorkflowSettingsEditor } from "./WorkflowSettingsEditor";
+import {
+ reviewStatusOf,
+ useReviewAnnotation,
+} from "../../review/reviewAnnotation";
+import { ReviewStartChanges, ReviewStatusChip } from "../../review/ReviewParts";
interface StartSettings {
webhookCallbackUrl: string;
@@ -54,6 +59,10 @@ function StartNode({ id, data, parentId }: NodeProps<StartNode>) {
const rerender = useRerender({ prefix: "accordion" });
const toggleScriptForNodeCallback = useToggleScriptForNodeCallback();
const isRecording = recordingStore.isRecording;
+ const reviewAnnotation = useReviewAnnotation();
+ const startReview =
+ reviewAnnotation?.kind === "start" ? reviewAnnotation : null;
+ const startReviewStatus = startReview ? reviewStatusOf(startReview) : null;
const parentNode = parentId ? reactFlowInstance.getNode(parentId) : null;
const isInsideConditional = parentNode?.type === "conditional";
@@ -181,8 +190,19 @@ function StartNode({ id, data, parentId }: NodeProps<StartNode>) {
)}
>
<div className="relative">
- <header className="mb-4 mt-2">Start</header>
+ <header className="relative mb-4 mt-2">
+ Start
+ {startReview?.showStatus &&
+ startReviewStatus &&
+ startReviewStatus !== "unchanged" ? (
+ <ReviewStatusChip
+ status={startReviewStatus}
+ className="absolute right-0 top-1/2 -translate-y-1/2"
+ />
+ ) : null}
+ </header>
<Separator />
+ {startReview ? <ReviewStartChanges review={startReview} /> : null}
<BuildModeOnly renderInReadOnlyComparison={false}>
<WorkflowInputsSection editable={data.editable} />
<Accordion
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/StartNode/types.ts b/skyvern-frontend/src/routes/workflows/editor/nodes/StartNode/types.ts
index d7732d72a..8354d1ef6 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/StartNode/types.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/StartNode/types.ts
@@ -5,6 +5,7 @@ import {
WorkflowModel,
WorkflowRetryPolicy,
} from "@/routes/workflows/types/workflowTypes";
+import type { StartReviewAnnotation } from "../../review/reviewAnnotation";
export type WorkflowStartNodeData = {
withWorkflowSettings: true;
@@ -39,6 +40,7 @@ export type WorkflowStartNodeData = {
errorCodeMapping: Record<string, string> | null;
label: "__start_block__";
showCode: boolean;
+ review?: StartReviewAnnotation;
};
export type OtherStartNodeData = {
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/TaskNode/TaskNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/TaskNode/TaskNode.tsx
index 8637187a1..56b7fef91 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/TaskNode/TaskNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/TaskNode/TaskNode.tsx
@@ -62,7 +62,6 @@ function TaskNode({ id, data, type }: NodeProps<TaskNode>) {
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/ValidationNode/ValidationNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/ValidationNode/ValidationNode.tsx
index 2ace0bab5..527d08130 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/ValidationNode/ValidationNode.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/ValidationNode/ValidationNode.tsx
@@ -59,7 +59,6 @@ function ValidationNode({ id, data, type }: NodeProps<ValidationNode>) {
"bg-background outline outline-2 outline-ring":
thisBlockIsTargetted,
},
- data.comparisonColor,
)}
>
<NodeHeader
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.test.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.test.tsx
index e385fa9be..864ba146c 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.test.tsx
@@ -39,6 +39,10 @@ import {
makeCollapseKey,
useNodeCollapseStore,
} from "../../collapse/useNodeCollapseStore";
+import {
+ ReviewAnnotationContext,
+ type BlockReviewAnnotation,
+} from "../../review/reviewAnnotation";
vi.mock("@/components/ui/use-toast", () => ({ toast: vi.fn() }));
@@ -78,6 +82,7 @@ function renderNodeHeader(
initialEntry = "/agents/wf-test/build",
initialNodes: AppNode[] = [],
scopeId: string | null = null,
+ review: BlockReviewAnnotation | null = null,
) {
const DebugWrapper = ({ children }: { children: React.ReactNode }) =>
debugStore ? (
@@ -104,15 +109,17 @@ function renderNodeHeader(
<WorkflowScopeContext.Provider
value={{ workflowId: scopeId, readOnly: false }}
>
- <NodeHeader
- blockLabel="block_1"
- editable
- nodeId="node-a"
- totpIdentifier={null}
- totpUrl={null}
- type="code"
- {...props}
- />
+ <ReviewAnnotationContext.Provider value={review}>
+ <NodeHeader
+ blockLabel="block_1"
+ editable
+ nodeId="node-a"
+ totpIdentifier={null}
+ totpUrl={null}
+ type="code"
+ {...props}
+ />
+ </ReviewAnnotationContext.Provider>
</WorkflowScopeContext.Provider>
</DebugWrapper>
</BlockActionContext.Provider>
@@ -312,3 +319,29 @@ test("a paused block run leaves per-block controls idle", () => {
expect(screen.getByRole("button", { name: "Run this block" })).toBeTruthy();
expect(screen.queryByRole("button", { name: "Stop this block" })).toBeNull();
});
+
+test("a changed block in a review shows its status and field changes", () => {
+ renderNodeHeader({ editable: false }, undefined, undefined, [], null, {
+ kind: "block",
+ status: "changed",
+ showStatus: true,
+ changes: [
+ {
+ key: "max_steps_per_run",
+ label: "Max steps",
+ before: "10",
+ after: "15",
+ },
+ { key: "url", label: "URL", before: null, after: "https://a.test" },
+ ],
+ });
+
+ expect(screen.getByText("Changed")).toBeTruthy();
+ expect(screen.getByText("Max steps")).toBeTruthy();
+ expect(screen.queryByText("URL")).toBeNull();
+
+ fireEvent.click(screen.getByRole("button", { name: "+1 more change" }));
+
+ expect(screen.getByText("URL")).toBeTruthy();
+ expect(screen.getByText("https://a.test")).toBeTruthy();
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx
index 743e94950..8197cc324 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx
@@ -75,6 +75,8 @@ import { useDebuggerLastRunValuesStore } from "@/store/DebuggerLastRunValuesStor
import { useBlockOutputStore } from "@/store/BlockOutputStore";
import { useDebugStore } from "@/store/useDebugStore";
import { useMissingStartUrlStore } from "@/store/MissingStartUrlStore";
+import { useReviewAnnotation } from "../../review/reviewAnnotation";
+import { ReviewBlockChanges, ReviewStatusChip } from "../../review/ReviewParts";
import {
RUN_APPEND_PANES,
withPanesOpen,
@@ -279,6 +281,9 @@ function NodeHeader({
const isCollapsible = collapsibleWorkflowBlockTypes.has(type);
const { closeWorkflowPanel } = useWorkflowPanelStore();
const workflowSettingsStore = useWorkflowSettingsStore();
+ const reviewAnnotation = useReviewAnnotation();
+ const blockReview =
+ reviewAnnotation?.kind === "block" ? reviewAnnotation : null;
const [label, setLabel] = useNodeLabelChangeHandler({
id: nodeId,
initialValue: blockLabel,
@@ -1032,7 +1037,10 @@ function NodeHeader({
useWorkflowTitleStore.getState().recordCopilotGraphEdit();
setLabel(value);
}}
- titleClassName="text-base"
+ titleClassName={cn("text-base", {
+ "text-muted-foreground line-through":
+ blockReview?.status === "removed",
+ })}
// A negative margin here would shrink this auto-width column's
// measured size and clip short values via max-w-full, so the
// padding is offset with relative/left (paint-only) instead.
@@ -1083,6 +1091,9 @@ function NodeHeader({
</div>
</div>
<div className="pointer-events-auto ml-auto flex items-center gap-2">
+ {blockReview?.showStatus && blockReview.status !== "unchanged" ? (
+ <ReviewStatusChip status={blockReview.status} />
+ ) : null}
{extraActions}
{thisBlockIsPlaying && (
<div className="ml-auto">
@@ -1193,6 +1204,8 @@ function NodeHeader({
</div>
</header>
+ {blockReview ? <ReviewBlockChanges review={blockReview} /> : null}
+
<BlockParametersDialog
open={showParamsDialog}
onOpenChange={setShowParamsDialog}
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/index.ts b/skyvern-frontend/src/routes/workflows/editor/nodes/index.ts
index 5d76acf8e..0a8691509 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/index.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/index.ts
@@ -71,6 +71,7 @@ import { DataExportNode as DataExportNodeComponent } from "./DataExportNode/Data
import { withSortableBlock } from "../sortable/withSortableBlock";
import { withCollapsible } from "../collapse/withCollapsible";
import { withSelectableBlock } from "../selection/withSelectableBlock";
+import { withReviewAnnotation } from "../review/withReviewAnnotation";
export type UtilityNode = StartNode | NodeAdderNode;
@@ -120,6 +121,8 @@ export type AppNode = UtilityNode | WorkflowBlockNode;
// Composition order is load-bearing:
// memo (outermost) - stable identity per node type for RF reconciliation
+// withReviewAnnotation - review canvases only; its outline sits outside
+// the selected ring
// withSortableBlock - registers `useSortable({ id })`; the inner tree
// must mount in both open and collapsed states so
// drag pickup works on either
@@ -128,7 +131,9 @@ export type AppNode = UtilityNode | WorkflowBlockNode;
// withCollapsible - leaf wrapper for body chrome
function wrapBlock<P extends NodeProps>(Component: ComponentType<P>) {
return memo(
- withSortableBlock(withSelectableBlock(withCollapsible(Component))),
+ withReviewAnnotation(
+ withSortableBlock(withSelectableBlock(withCollapsible(Component))),
+ ),
);
}
@@ -136,7 +141,9 @@ function wrapBlock<P extends NodeProps>(Component: ComponentType<P>) {
// child nodes, so collapsing the parent to a header-only card would leave
// children visually overflowing the card and break edge layout.
function wrapContainerBlock<P extends NodeProps>(Component: ComponentType<P>) {
- return memo(withSortableBlock(withSelectableBlock(Component)));
+ return memo(
+ withReviewAnnotation(withSortableBlock(withSelectableBlock(Component))),
+ );
}
export const nodeTypes = {
@@ -151,7 +158,7 @@ export const nodeTypes = {
fileUpload: wrapBlock(FileUploadNodeComponent),
download: wrapBlock(DownloadNodeComponent),
nodeAdder: memo(NodeAdderNodeComponent),
- start: memo(StartNodeComponent),
+ start: memo(withReviewAnnotation(StartNodeComponent)),
validation: wrapBlock(ValidationNodeComponent),
action: wrapBlock(ActionNodeComponent),
navigation: wrapBlock(NavigationNodeComponent),
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/types.ts b/skyvern-frontend/src/routes/workflows/editor/nodes/types.ts
index bc537766b..5eb0672d1 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/types.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/types.ts
@@ -3,6 +3,10 @@ import {
type WorkflowModel,
} from "../../types/workflowTypes";
import type { CodeBlockTitleSource } from "../../types/scriptTypes";
+import type {
+ BlockReviewAnnotation,
+ FoldReviewAnnotation,
+} from "../review/reviewAnnotation";
export type NodeBaseData = {
debuggable: boolean;
@@ -12,7 +16,7 @@ export type NodeBaseData = {
editable: boolean;
model: WorkflowModel | null;
showCode?: boolean;
- comparisonColor?: string;
+ review?: BlockReviewAnnotation | FoldReviewAnnotation;
ignoreWorkflowSystemPrompt?: boolean;
/**
* Optional metadata used for conditional branches.
diff --git a/skyvern-frontend/src/routes/workflows/editor/paneFit.test.ts b/skyvern-frontend/src/routes/workflows/editor/paneFit.test.ts
index 629964e03..815c769cf 100644
--- a/skyvern-frontend/src/routes/workflows/editor/paneFit.test.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/paneFit.test.ts
@@ -9,6 +9,7 @@ import {
PANE_RESIZE_EPSILON_PX,
paneRecenterViewport,
relayoutDriftCorrection,
+ revealNodeViewport,
START_ANCHOR_MARGIN_X_PX,
START_ANCHOR_MIN_ZOOM,
START_ANCHOR_TOP_PX,
@@ -416,3 +417,33 @@ describe("relayoutDriftCorrection", () => {
).toBeNull();
});
});
+
+describe("revealNodeViewport", () => {
+ const pane = { width: 800, height: 600 };
+
+ test("centers a node that fits the pane", () => {
+ const bounds = { x: 100, y: 300, width: 400, height: 200 };
+ const { x, y, zoom } = revealNodeViewport({ pane, bounds })!;
+ expect(zoom).toBe(1);
+ expect(bounds.x + bounds.width / 2 + x).toBeCloseTo(pane.width / 2);
+ expect(bounds.y + bounds.height / 2 + y).toBeCloseTo(pane.height / 2);
+ });
+
+ test("pins a node taller than the pane by its top so its header shows", () => {
+ const bounds = { x: 100, y: 2000, width: 400, height: 900 };
+ const { y } = revealNodeViewport({ pane, bounds })!;
+ const top = bounds.y + y;
+ expect(top).toBeGreaterThan(0);
+ expect(top).toBeLessThan(pane.height / 4);
+ });
+
+ test("degenerate panes and empty nodes return null", () => {
+ const bounds = { x: 0, y: 0, width: 400, height: 200 };
+ expect(
+ revealNodeViewport({ pane: { width: 0, height: 0 }, bounds }),
+ ).toBeNull();
+ expect(
+ revealNodeViewport({ pane, bounds: { ...bounds, width: 0, height: 0 } }),
+ ).toBeNull();
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/paneFit.ts b/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
index 72fa75fbf..d8f78775a 100644
--- a/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
@@ -183,6 +183,27 @@ export function centeredNodeViewport({
};
}
+const REVEAL_TOP_MARGIN_PX = 48;
+
+// Like centeredNodeViewport, but a node taller than the pane is pinned by its
+// top so its header, where the review status sits, stays in view.
+export function revealNodeViewport({
+ pane,
+ bounds,
+}: {
+ pane: Size;
+ bounds: Rect;
+}): Viewport | null {
+ const centered = centeredNodeViewport({ pane, bounds });
+ if (centered === null) {
+ return null;
+ }
+ if (bounds.height + REVEAL_TOP_MARGIN_PX * 2 <= pane.height) {
+ return centered;
+ }
+ return { ...centered, y: REVEAL_TOP_MARGIN_PX - bounds.y };
+}
+
export const END_ANCHOR_BOTTOM_PX = 24;
/**
diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.test.tsx b/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.test.tsx
index 315447638..b6ae70e82 100644
--- a/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.test.tsx
@@ -11,12 +11,13 @@ import { afterEach, describe, expect, test, vi } from "vitest";
import type { WorkflowVersion } from "../../hooks/useWorkflowVersionsQuery";
-// The panel mounts two read-only canvases; the review-action contract under
-// test lives entirely in the footer, so the canvas stack is stubbed out.
+// The review contract under test lives in the header, so the canvas stack is
+// stubbed out; the diff still runs on the real chain helpers.
vi.mock("../FlowRenderer", () => ({
FlowRenderer: () => <div data-testid="flow" />,
}));
-vi.mock("../workflowEditorUtils", () => ({
+vi.mock("../workflowEditorUtils", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../workflowEditorUtils")>()),
getElements: () => ({ nodes: [], edges: [] }),
}));
vi.mock("../nodes", () => ({}));
@@ -34,7 +35,7 @@ afterEach(() => {
});
describe("WorkflowComparisonPanel diff summary", () => {
- test("a first proposal against an empty Current reads as added, not identical", () => {
+ test("a first proposal against an empty Current reads as a new workflow", () => {
const block = {
label: "open_target",
block_type: "goto_url",
@@ -49,9 +50,10 @@ describe("WorkflowComparisonPanel diff summary", () => {
/>,
);
- expect(screen.getByText("Added (1)")).toBeTruthy();
- expect(screen.getByText("Identical (0)")).toBeTruthy();
- expect(screen.getByText("Modified (0)")).toBeTruthy();
+ expect(screen.getByText("New workflow")).toBeTruthy();
+ expect(screen.getByText("1 block")).toBeTruthy();
+ expect(screen.getByText(/This is a new workflow/)).toBeTruthy();
+ expect(screen.queryByText(/unchanged/)).toBeNull();
});
});
@@ -74,7 +76,7 @@ describe("WorkflowComparisonPanel copilot review actions", () => {
/>,
);
- const accept = screen.getByRole("button", { name: "Accept" });
+ const accept = screen.getByRole("button", { name: "Accept changes" });
const reject = screen.getByRole("button", { name: "Reject" });
await act(async () => {
diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.tsx b/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.tsx
index af1dc4a3f..bc465d049 100644
--- a/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.tsx
@@ -1,28 +1,57 @@
import { apiWorkflowToSettings } from "@/routes/workflows/editor/apiWorkflowToSettings";
-import { useCallback, useEffect, useMemo, useRef, useState } from "react";
+import {
+ useCallback,
+ useEffect,
+ useId,
+ useMemo,
+ useRef,
+ useState,
+ type MutableRefObject,
+} from "react";
import { Button } from "@/components/ui/button";
+import { Checkbox } from "@/components/ui/checkbox";
+import { Label } from "@/components/ui/label";
import { Separator } from "@/components/ui/separator";
-import { Cross2Icon } from "@radix-ui/react-icons";
+import {
+ ChevronDownIcon,
+ ChevronUpIcon,
+ Cross2Icon,
+} from "@radix-ui/react-icons";
import {
ReactFlowProvider,
useNodesState,
useEdgesState,
+ useReactFlow,
+ useStore,
+ getNodesBounds,
NodeChange,
EdgeChange,
+ type Edge,
} from "@xyflow/react";
import { WorkflowVersion } from "../../hooks/useWorkflowVersionsQuery";
import { WorkflowBlock } from "../../types/workflowTypes";
import { FlowRenderer } from "../FlowRenderer";
import { getElements } from "../workflowEditorUtils";
-import { AppNode } from "../nodes";
-import { areBlocksIdentical } from "../../util/compareBlocks";
-
-type BlockComparison = {
- leftBlock?: WorkflowBlock;
- rightBlock?: WorkflowBlock;
- status: "identical" | "modified" | "added" | "removed";
- identifier: string;
-};
+import {
+ PANE_FIT_DEBOUNCE_MS,
+ revealNodeViewport,
+ startAnchoredViewport,
+} from "../paneFit";
+import { AppNode, isWorkflowBlockNode } from "../nodes";
+import { isWorkflowStartNodeData } from "../nodes/StartNode/types";
+import type {
+ BlockReviewAnnotation,
+ FoldReviewAnnotation,
+ StartReviewAnnotation,
+} from "../review/reviewAnnotation";
+import { ReviewStatusChip } from "../review/ReviewParts";
+import {
+ diffWorkflowVersions,
+ foldUnchangedRuns,
+ type BlockReview,
+ type UnchangedFold,
+ type WorkflowReviewDiff,
+} from "./workflowReviewDiff";
type ComparisonMode = "history" | "copilot";
@@ -39,181 +68,145 @@ type Props = {
onExit?: () => void;
};
-// Mapping from WorkflowBlock.block_type to ReactFlow node.type
-const BLOCK_TYPE_TO_NODE_TYPE: Record<string, string> = {
- task: "task",
- task_v2: "navigation", // task_v2 blocks are displayed as navigation nodes with V2 engine
- validation: "validation",
- action: "action",
- navigation: "navigation",
- extraction: "extraction",
- login: "login",
- wait: "wait",
- terminate: "terminate",
- file_download: "fileDownload",
- code: "codeBlock",
- send_email: "sendEmail",
- text_prompt: "textPrompt",
- for_loop: "loop",
- while_loop: "loop",
- file_url_parser: "fileParser",
- pdf_parser: "pdfParser",
- download_to_s3: "download",
- upload_to_s3: "upload",
- file_upload: "fileUpload",
- goto_url: "url",
- http_request: "http_request",
- pdf_fill: "pdfFill",
- split_pdf: "splitPdf",
- email_inbox: "emailInbox",
- google_sheets_read: "googleSheetsRead",
- google_sheets_write: "googleSheetsWrite",
-};
-
-function getBlockIdentifier(block: WorkflowBlock): string {
- // Convert block_type to node type for consistent comparison
- const nodeType =
- BLOCK_TYPE_TO_NODE_TYPE[block.block_type] || block.block_type;
- return `${nodeType}:${block.label}`;
+type CanvasElements = { nodes: Array<AppNode>; edges: Array<Edge> };
+
+type BlockAnnotationLookup = (
+ label: string,
+) => BlockReviewAnnotation | FoldReviewAnnotation | undefined;
+
+function annotatedElements(
+ blocks: Array<WorkflowBlock>,
+ version: WorkflowVersion,
+ lookup: BlockAnnotationLookup,
+ start?: StartReviewAnnotation,
+): CanvasElements {
+ // Deep clone so the canvas never shares objects with the editor's state.
+ const { nodes, edges } = getElements(
+ JSON.parse(JSON.stringify(blocks)),
+ apiWorkflowToSettings(version),
+ false,
+ );
+ return {
+ edges,
+ nodes: nodes.map((node) => {
+ if (node.type === "start") {
+ return start && !node.parentId && isWorkflowStartNodeData(node.data)
+ ? ({ ...node, data: { ...node.data, review: start } } as AppNode)
+ : node;
+ }
+ if (!isWorkflowBlockNode(node)) return node;
+ const review = lookup(node.data.label);
+ return review
+ ? ({ ...node, data: { ...node.data, review } } as AppNode)
+ : node;
+ }),
+ };
}
-function compareWorkflowBlocks(
- blocks1: WorkflowBlock[],
- blocks2: WorkflowBlock[],
-): BlockComparison[] {
- const comparisons: BlockComparison[] = [];
- const processedBlocks = new Set<string>();
-
- // Create maps for quick lookup
- const blocks1Map = new Map<string, WorkflowBlock>();
- const blocks2Map = new Map<string, WorkflowBlock>();
-
- blocks1.forEach((block) => {
- const identifier = getBlockIdentifier(block);
- blocks1Map.set(identifier, block);
- });
-
- blocks2.forEach((block) => {
- const identifier = getBlockIdentifier(block);
- blocks2Map.set(identifier, block);
- });
-
- // Compare blocks that exist in the first version
- blocks1.forEach((block1) => {
- const identifier = getBlockIdentifier(block1);
- const block2 = blocks2Map.get(identifier);
- processedBlocks.add(identifier);
-
- if (block2) {
- // Block exists in both versions
- const isIdentical = areBlocksIdentical(block1, block2);
- comparisons.push({
- leftBlock: block1,
- rightBlock: block2,
- status: isIdentical ? "identical" : "modified",
- identifier,
- });
- } else {
- // Block was removed in version 2
- comparisons.push({
- leftBlock: block1,
- rightBlock: undefined,
- status: "removed",
- identifier,
- });
- }
- });
-
- // Check for blocks that were added in version 2
- blocks2.forEach((block2) => {
- const identifier = getBlockIdentifier(block2);
- if (!processedBlocks.has(identifier)) {
- comparisons.push({
- leftBlock: undefined,
- rightBlock: block2,
- status: "added",
- identifier,
- });
- }
- });
-
- return comparisons;
+function blockAnnotation(
+ review: BlockReview | undefined,
+ showStatus: boolean,
+ withChanges: boolean,
+): BlockReviewAnnotation | undefined {
+ if (!review) return undefined;
+ return {
+ kind: "block",
+ status: review.status,
+ changes: withChanges ? review.changes : [],
+ showStatus,
+ };
}
-function getWorkflowElements(version: WorkflowVersion) {
- const settings = apiWorkflowToSettings(version);
-
- // Deep clone the blocks to ensure complete isolation from main editor
- const blocks = JSON.parse(
- JSON.stringify(version.workflow_definition?.blocks || []),
- );
-
- return getElements(
- blocks,
- settings,
- false, // not editable in comparison view
- );
-}
+type AnchorControl = MutableRefObject<{
+ release: () => void;
+ rearm: () => void;
+ pane: () => DOMRect | undefined;
+} | null>;
-function WorkflowComparisonRenderer({
+function ReviewFlowCanvas({
+ elements,
version,
- blockColors,
+ anchorControlRef,
}: {
+ elements: CanvasElements;
version: WorkflowVersion;
- onSelectState?: (version: WorkflowVersion) => void;
- blockColors?: Map<string, string>;
+ // Lets the header stop or restart the start anchor and measure the pane.
+ anchorControlRef?: AnchorControl;
}) {
- // Memoize elements creation to prevent unnecessary re-renders
- const elements = useMemo(() => getWorkflowElements(version), [version]);
+ const reactFlow = useReactFlow<AppNode>();
+ const [nodes, setNodes, onNodesChange] = useNodesState(elements.nodes);
+ const [edges, setEdges, onEdgesChange] = useEdgesState(elements.edges);
+ const containerRef = useRef<HTMLDivElement>(null);
+ const userMovedRef = useRef(false);
+ const anchorTimerRef = useRef<number | null>(null);
- // Memoize the colored nodes to prevent re-computation
- const coloredNodes = useMemo(() => {
- if (!blockColors || blockColors.size === 0) {
- return elements.nodes;
+ // useNodesState only reads its initial argument; re-sync when the elements
+ // change (Hide unchanged, a fold opened).
+ useEffect(() => {
+ setNodes(elements.nodes);
+ setEdges(elements.edges);
+ }, [elements, setNodes, setEdges]);
+
+ // The review opens while its pane is still settling (the studio reveals the
+ // Editor pane in the same step), so a one-shot fit lands off-center. Keep the
+ // flow's start pinned to the top, re-applied after each layout pass and pane
+ // resize, until the reviewer pans, zooms, or jumps to a change.
+ const scheduleAnchor = useCallback(() => {
+ if (userMovedRef.current) return;
+ if (anchorTimerRef.current !== null) {
+ window.clearTimeout(anchorTimerRef.current);
}
+ anchorTimerRef.current = window.setTimeout(() => {
+ anchorTimerRef.current = null;
+ const pane = containerRef.current?.getBoundingClientRect();
+ const visible = reactFlow.getNodes().filter((node) => !node.hidden);
+ if (userMovedRef.current || !pane || visible.length === 0) return;
+ const viewport = startAnchoredViewport({
+ pane: { width: pane.width, height: pane.height },
+ bounds: getNodesBounds(visible),
+ });
+ if (viewport) void reactFlow.setViewport(viewport);
+ }, PANE_FIT_DEBOUNCE_MS);
+ }, [reactFlow]);
- // Apply comparison colors to block nodes
- return elements.nodes.map((node) => {
- // Check if this is a workflow block node (not start/nodeAdder)
- if (
- node.type !== "nodeAdder" &&
- node.type !== "start" &&
- node.data &&
- node.data.label
- ) {
- // This is a workflow block node - get its identifier and color
- const identifier = `${node.type}:${node.data.label}`;
- const color = blockColors.get(identifier);
-
- if (color) {
- return {
- ...node,
- data: {
- ...node.data,
- comparisonColor: color,
- },
- style: {
- ...node.style,
- backgroundColor: color,
- border: `2px solid ${color}`,
- },
- };
- }
- }
- return node;
- });
- }, [elements.nodes, blockColors]);
-
- const [nodes, setNodes, onNodesChange] = useNodesState(
- coloredNodes as AppNode[],
- );
- const [edges, setEdges, onEdgesChange] = useEdgesState(elements.edges);
+ useEffect(() => {
+ scheduleAnchor();
+ }, [nodes, scheduleAnchor]);
- // useNodesState only reads its initial argument; re-sync when colored nodes
- // change so "modified" highlights show up after blockColors settles.
useEffect(() => {
- setNodes(coloredNodes as AppNode[]);
- }, [coloredNodes, setNodes]);
+ const container = containerRef.current;
+ if (!container) return;
+ const release = () => {
+ userMovedRef.current = true;
+ };
+ if (anchorControlRef) {
+ anchorControlRef.current = {
+ release,
+ rearm: () => {
+ userMovedRef.current = false;
+ scheduleAnchor();
+ },
+ pane: () => containerRef.current?.getBoundingClientRect(),
+ };
+ }
+ const observer =
+ typeof ResizeObserver === "undefined"
+ ? null
+ : new ResizeObserver(() => scheduleAnchor());
+ observer?.observe(container);
+ container.addEventListener("wheel", release, { passive: true });
+ container.addEventListener("pointerdown", release);
+ return () => {
+ observer?.disconnect();
+ container.removeEventListener("wheel", release);
+ container.removeEventListener("pointerdown", release);
+ if (anchorControlRef) anchorControlRef.current = null;
+ if (anchorTimerRef.current !== null) {
+ window.clearTimeout(anchorTimerRef.current);
+ anchorTimerRef.current = null;
+ }
+ };
+ }, [anchorControlRef, scheduleAnchor]);
const handleNodesChange = useCallback(
(changes: NodeChange<AppNode>[]) => {
@@ -230,7 +223,10 @@ function WorkflowComparisonRenderer({
);
return (
- <div className="h-full w-full rounded-lg border bg-white">
+ <div
+ ref={containerRef}
+ className="h-full w-full overflow-hidden rounded-lg border bg-background"
+ >
<FlowRenderer
hideBackground={false}
readOnly
@@ -247,203 +243,380 @@ function WorkflowComparisonRenderer({
);
}
-function WorkflowComparisonPanel({
- version1,
- version2,
- onSelectState,
- mode = "history",
- onCopilotReviewClose,
- onExit,
-}: Props) {
- const comparisons = useMemo(() => {
- const blocks1 = version1?.workflow_definition?.blocks || [];
- const blocks2 = version2?.workflow_definition?.blocks || [];
- return compareWorkflowBlocks(blocks1, blocks2);
- }, [
- version1?.workflow_definition?.blocks,
- version2?.workflow_definition?.blocks,
- ]);
+function pluralize(count: number, word: string): string {
+ return `${count} ${word}${count === 1 ? "" : "s"}`;
+}
- // Approve / Reject await a server round trip; a second click before it
- // settles would race a concurrent apply/clear against the same revision.
- const settlingRef = useRef(false);
- const [isSettling, setIsSettling] = useState(false);
- const settleCopilotReview = useCallback(
- async (status: CopilotReviewStatus) => {
- if (!onCopilotReviewClose || settlingRef.current) return;
- settlingRef.current = true;
- setIsSettling(true);
- try {
- await onCopilotReviewClose(status);
- } finally {
- settlingRef.current = false;
- setIsSettling(false);
- }
- },
- [onCopilotReviewClose],
+function ReviewSummary({
+ diff,
+ lead,
+}: {
+ diff: WorkflowReviewDiff;
+ lead: string;
+}) {
+ const { counts, inputs, settings } = diff;
+ if (diff.isNewWorkflow) {
+ return (
+ <div className="flex flex-wrap items-center gap-2 text-sm">
+ <span className="text-muted-foreground">New workflow</span>
+ <ReviewStatusChip status="new" />
+ <span className="text-muted-foreground">
+ {pluralize(counts.new, "block")}
+ {inputs.length > 0 ? `, ${pluralize(inputs.length, "input")}` : ""}
+ </span>
+ </div>
+ );
+ }
+ const startParts = [
+ inputs.length > 0 ? pluralize(inputs.length, "input") : null,
+ settings.length > 0 ? pluralize(settings.length, "setting") : null,
+ ].filter(Boolean);
+ const hasBlockChanges = counts.new + counts.changed + counts.removed > 0;
+ return (
+ <div className="flex flex-wrap items-center gap-2 text-sm">
+ <span className="text-muted-foreground">{lead}</span>
+ {counts.new > 0 ? (
+ <ReviewStatusChip status="new" count={counts.new} />
+ ) : null}
+ {counts.changed > 0 ? (
+ <ReviewStatusChip status="changed" count={counts.changed} />
+ ) : null}
+ {counts.removed > 0 ? (
+ <ReviewStatusChip status="removed" count={counts.removed} />
+ ) : null}
+ {startParts.length > 0 ? (
+ <span className="text-muted-foreground">+ {startParts.join(", ")}</span>
+ ) : null}
+ {!hasBlockChanges && startParts.length === 0 ? (
+ <span className="text-muted-foreground">No changes</span>
+ ) : null}
+ <span className="text-muted-foreground">
+ · {counts.unchanged} unchanged
+ </span>
+ </div>
);
+}
- // ESC key handler for copilot mode - close without rejecting
- useEffect(() => {
- if (mode !== "copilot" || !onCopilotReviewClose) return;
-
- const handleKeyDown = (event: KeyboardEvent) => {
- if (event.key === "Escape") {
- void settleCopilotReview("close");
- }
- };
-
- window.addEventListener("keydown", handleKeyDown);
- return () => window.removeEventListener("keydown", handleKeyDown);
- }, [mode, onCopilotReviewClose, settleCopilotReview]);
-
- // ESC in history mode mirrors "Keep current version" when an exit is wired.
- useEffect(() => {
- if (mode !== "history" || !onExit) return;
-
- const handleKeyDown = (event: KeyboardEvent) => {
- if (event.key === "Escape") {
- onExit();
- }
- };
-
- window.addEventListener("keydown", handleKeyDown);
- return () => window.removeEventListener("keydown", handleKeyDown);
- }, [mode, onExit]);
+const START_TARGET = "__start_block__";
- // Statistics
- const stats = useMemo(
- () => ({
- identical: comparisons.filter((c) => c.status === "identical").length,
- modified: comparisons.filter((c) => c.status === "modified").length,
- added: comparisons.filter((c) => c.status === "added").length,
- removed: comparisons.filter((c) => c.status === "removed").length,
- }),
- [comparisons],
+function CopilotReview({
+ diff,
+ version,
+ isSettling,
+ canClose,
+ onSettle,
+}: {
+ diff: WorkflowReviewDiff;
+ version: WorkflowVersion;
+ isSettling: boolean;
+ canClose: boolean;
+ onSettle: (status: CopilotReviewStatus) => void;
+}) {
+ const reactFlow = useReactFlow<AppNode>();
+ const hideUnchangedId = useId();
+ const [hideUnchanged, setHideUnchanged] = useState(false);
+ const [expandedFolds, setExpandedFolds] = useState<ReadonlySet<string>>(
+ () => new Set(),
+ );
+ const [cursor, setCursor] = useState(-1);
+ const anchorControlRef: AnchorControl = useRef(null);
+ const showStatus = !diff.isNewWorkflow;
+ const finallyLabel = version.workflow_definition?.finally_block_label ?? null;
+
+ // Conditional levels never fold, so a workflow can have unchanged blocks and
+ // still nothing to hide.
+ const canFold = useMemo(
+ () =>
+ foldUnchangedRuns(diff.mergedBlocks, diff.merged, new Set(), finallyLabel)
+ .folds.size > 0,
+ [diff, finallyLabel],
);
- // Create color mapping for block identifiers
- const getComparisonColor = (
- status: "identical" | "modified" | "added" | "removed",
- ): string => {
- switch (status) {
- case "identical":
- return "#86efac"; // green-300
- case "modified":
- return "#facc15"; // yellow-400
- case "added":
- return "#60a5fa"; // blue-400
- case "removed":
- return "#f87171"; // red-400
- default:
- return "";
- }
+ const elements = useMemo(() => {
+ const { blocks, folds } = hideUnchanged
+ ? foldUnchangedRuns(
+ diff.mergedBlocks,
+ diff.merged,
+ expandedFolds,
+ finallyLabel,
+ )
+ : {
+ blocks: diff.mergedBlocks,
+ folds: new Map<string, UnchangedFold>(),
+ };
+ return annotatedElements(
+ blocks,
+ version,
+ (label) => {
+ const fold = folds.get(label);
+ if (fold) {
+ return {
+ kind: "fold",
+ count: fold.count,
+ onShow: () =>
+ setExpandedFolds((prev) => new Set(prev).add(fold.key)),
+ };
+ }
+ return blockAnnotation(diff.merged.get(label), showStatus, true);
+ },
+ {
+ kind: "start",
+ inputs: diff.inputs,
+ settings: diff.settings,
+ showStatus,
+ },
+ );
+ }, [diff, version, hideUnchanged, expandedFolds, finallyLabel, showStatus]);
+
+ // A change inside a conditional's inactive branch has no card to jump to,
+ // and the reviewer can switch branches, so visibility is read live.
+ const hiddenLabels = useStore((store) =>
+ store.nodes
+ .flatMap((node) =>
+ node.hidden && isWorkflowBlockNode(node as AppNode)
+ ? [(node as AppNode).data.label as string]
+ : [],
+ )
+ .join("\n"),
+ );
+ const targets = useMemo(() => {
+ const hidden = new Set(hiddenLabels.split("\n"));
+ return [
+ ...(diff.inputs.length + diff.settings.length > 0 ? [START_TARGET] : []),
+ ...diff.changeOrder.filter((label) => !hidden.has(label)),
+ ];
+ }, [diff, hiddenLabels]);
+ useEffect(() => {
+ setCursor(-1);
+ }, [targets]);
+
+ // React Flow's fitView queues a no-op node update that FlowRenderer's change
+ // filter drops, so the jump sets the viewport itself, as FlowRenderer does.
+ const focusChange = (index: number) => {
+ const target = targets[index];
+ if (target === undefined) return;
+ setCursor(index);
+ const node = reactFlow
+ .getNodes()
+ .find((candidate) =>
+ target === START_TARGET
+ ? candidate.type === "start" && !candidate.parentId
+ : isWorkflowBlockNode(candidate) && candidate.data.label === target,
+ );
+ anchorControlRef.current?.release();
+ const internal = node ? reactFlow.getInternalNode(node.id) : undefined;
+ const pane = anchorControlRef.current?.pane();
+ if (!internal || !pane) return;
+ const viewport = revealNodeViewport({
+ pane: { width: pane.width, height: pane.height },
+ bounds: {
+ ...internal.internals.positionAbsolute,
+ width: internal.measured.width ?? 0,
+ height: internal.measured.height ?? 0,
+ },
+ });
+ if (viewport) void reactFlow.setViewport(viewport, { duration: 300 });
};
- // Create memoized maps for each version's block colors
- const { version1BlockColors, version2BlockColors } = useMemo(() => {
- const v1Colors = new Map<string, string>();
- const v2Colors = new Map<string, string>();
-
- comparisons.forEach((comparison) => {
- const color = getComparisonColor(comparison.status);
+ return (
+ <div className="flex h-full w-full flex-col rounded-lg bg-slate-elevation2">
+ <div className="flex flex-shrink-0 flex-wrap items-center gap-x-6 gap-y-3 p-4">
+ <div className="min-w-0">
+ <div className="text-xs font-medium uppercase tracking-[0.12em] text-muted-foreground">
+ Copilot review
+ </div>
+ <h2 className="truncate text-lg font-semibold">{version.title}</h2>
+ </div>
+ <ReviewSummary diff={diff} lead="Copilot's changes" />
+ <div className="ml-auto flex flex-wrap items-center gap-3">
+ {targets.length > 0 && !diff.isNewWorkflow ? (
+ <div className="flex items-center gap-1">
+ <Button
+ variant="ghost"
+ size="icon"
+ className="size-8"
+ aria-label="Previous change"
+ title="Previous change"
+ onClick={() =>
+ focusChange(cursor <= 0 ? targets.length - 1 : cursor - 1)
+ }
+ >
+ <ChevronUpIcon className="size-4" />
+ </Button>
+ <span
+ aria-live="polite"
+ className="min-w-[5.5rem] text-center text-xs tabular-nums text-muted-foreground"
+ >
+ {cursor >= 0
+ ? `Change ${cursor + 1} of ${targets.length}`
+ : pluralize(targets.length, "change")}
+ </span>
+ <Button
+ variant="ghost"
+ size="icon"
+ className="size-8"
+ aria-label="Next change"
+ title="Next change"
+ onClick={() => focusChange((cursor + 1) % targets.length)}
+ >
+ <ChevronDownIcon className="size-4" />
+ </Button>
+ </div>
+ ) : null}
+ {canFold ? (
+ <div className="flex items-center gap-2">
+ <Checkbox
+ id={hideUnchangedId}
+ checked={hideUnchanged}
+ onCheckedChange={(checked) => {
+ setHideUnchanged(checked === true);
+ setExpandedFolds(new Set());
+ setCursor(-1);
+ anchorControlRef.current?.rearm();
+ }}
+ />
+ <Label
+ htmlFor={hideUnchangedId}
+ className="cursor-pointer text-sm font-normal"
+ >
+ Hide unchanged
+ </Label>
+ </div>
+ ) : null}
+ <Button
+ size="sm"
+ variant="secondary"
+ onClick={() => onSettle("reject")}
+ disabled={isSettling}
+ >
+ Reject
+ </Button>
+ {/* Matches the chat's Accept, so one decision looks the same in both places. */}
+ <Button
+ size="sm"
+ onClick={() => onSettle("approve")}
+ disabled={isSettling}
+ className="bg-success text-success-foreground hover:bg-success/90"
+ >
+ Accept changes
+ </Button>
+ {canClose ? (
+ <button
+ type="button"
+ onClick={() => onSettle("close")}
+ disabled={isSettling}
+ className="rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
+ title="Close (Esc)"
+ aria-label="Close review"
+ >
+ <Cross2Icon className="h-5 w-5" />
+ </button>
+ ) : null}
+ </div>
+ </div>
- // For version1 blocks
- if (comparison.leftBlock) {
- v1Colors.set(comparison.identifier, color);
- }
+ <Separator />
- // For version2 blocks
- if (comparison.rightBlock) {
- v2Colors.set(comparison.identifier, color);
- }
- });
+ <div className="relative flex-1 overflow-hidden p-4">
+ {diff.isNewWorkflow ? (
+ <div className="pointer-events-none absolute inset-x-0 top-8 z-10 flex justify-center px-8">
+ <div className="flex items-center gap-2 rounded-lg border bg-slate-elevation3 px-3 py-2 text-sm shadow-md">
+ <ReviewStatusChip status="new" />
+ <span>
+ This is a new workflow, so every block below is new. Nothing on
+ the canvas is replaced.
+ </span>
+ </div>
+ </div>
+ ) : null}
+ <ReviewFlowCanvas
+ elements={elements}
+ version={version}
+ anchorControlRef={anchorControlRef}
+ />
+ </div>
+ </div>
+ );
+}
- return {
- version1BlockColors: v1Colors,
- version2BlockColors: v2Colors,
- };
- }, [comparisons]);
+function HistoryComparison({
+ diff,
+ version1,
+ version2,
+ onSelectState,
+ onExit,
+}: {
+ diff: WorkflowReviewDiff;
+ version1: WorkflowVersion;
+ version2: WorkflowVersion;
+ onSelectState?: (version: WorkflowVersion) => void;
+ onExit?: () => void;
+}) {
+ const leftElements = useMemo(
+ () =>
+ annotatedElements(
+ version1.workflow_definition?.blocks ?? [],
+ version1,
+ (label) => blockAnnotation(diff.before.get(label), true, false),
+ ),
+ [diff, version1],
+ );
+ const rightElements = useMemo(
+ () =>
+ annotatedElements(
+ version2.workflow_definition?.blocks ?? [],
+ version2,
+ (label) => blockAnnotation(diff.after.get(label), true, true),
+ {
+ kind: "start",
+ inputs: diff.inputs,
+ settings: diff.settings,
+ showStatus: true,
+ },
+ ),
+ [diff, version2],
+ );
return (
<div className="flex h-full w-full flex-col rounded-lg bg-slate-elevation2">
- {/* Header */}
- <div className="relative flex-shrink-0 p-4 pb-3">
- {/* History-mode exit: keep the current version, drop the comparison. */}
- {mode === "history" && onExit && (
- <button
- type="button"
- onClick={onExit}
- title="Exit comparison (Esc)"
- className="absolute right-4 top-4 inline-flex h-7 items-center rounded-md border border-border px-2 text-xs font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
- >
- Keep current version
- </button>
- )}
- {/* Close button for copilot mode */}
- {mode === "copilot" && onCopilotReviewClose && (
- <button
- type="button"
- onClick={() => void settleCopilotReview("close")}
- disabled={isSettling}
- className="absolute right-4 top-4 rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground dark:hover:bg-slate-700 dark:hover:text-slate-200"
- title="Close (Esc)"
- >
- <Cross2Icon className="h-5 w-5" />
- </button>
+ <div className="flex-shrink-0 p-4 pb-3">
+ {onExit && (
+ <div className="mb-2 flex justify-end">
+ <button
+ type="button"
+ onClick={onExit}
+ title="Exit comparison (Esc)"
+ className="inline-flex h-7 items-center rounded-md border border-border px-2 text-xs font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
+ >
+ Keep current version
+ </button>
+ </div>
)}
- {/* 3x3 Grid Layout */}
<div className="grid grid-cols-3 gap-4">
- {/* Row 1: Workflow Names and Title */}
<h2 className="text-center text-xl font-semibold">
{version1.title}
</h2>
<h3 className="text-center text-lg font-medium text-muted-foreground">
- {mode === "copilot" ? "Copilot Review" : "Version Comparison"}
+ Version Comparison
</h3>
<h2 className="text-center text-xl font-semibold">
{version2.title}
</h2>
- {/* Row 2: Version Details and Statistics */}
- {mode === "history" ? (
- <div className="text-center text-sm text-muted-foreground">
- [Version {version1.version}] •{" "}
- {new Date(version1.modified_at).toLocaleDateString()}
- </div>
- ) : (
- <div></div>
- )}
- <div className="flex justify-center gap-3 text-sm">
- <div className="flex items-center gap-1">
- <div className="h-3 w-3 rounded-full bg-green-300"></div>
- <span>Identical ({stats.identical})</span>
- </div>
- <div className="flex items-center gap-1">
- <div className="h-3 w-3 rounded-full bg-yellow-400"></div>
- <span>Modified ({stats.modified})</span>
- </div>
- <div className="flex items-center gap-1">
- <div className="h-3 w-3 rounded-full bg-blue-400"></div>
- <span>Added ({stats.added})</span>
- </div>
- <div className="flex items-center gap-1">
- <div className="h-3 w-3 rounded-full bg-red-400"></div>
- <span>Removed ({stats.removed})</span>
- </div>
+ <div className="text-center text-sm text-muted-foreground">
+ [Version {version1.version}] •{" "}
+ {new Date(version1.modified_at).toLocaleDateString()}
+ </div>
+ <div className="flex justify-center">
+ <ReviewSummary diff={diff} lead="Changes" />
+ </div>
+ <div className="text-center text-sm text-muted-foreground">
+ [Version {version2.version}] •{" "}
+ {new Date(version2.modified_at).toLocaleDateString()}
</div>
- {mode === "history" ? (
- <div className="text-center text-sm text-muted-foreground">
- [Version {version2.version}] •{" "}
- {new Date(version2.modified_at).toLocaleDateString()}
- </div>
- ) : (
- <div></div>
- )}
- {/* Row 3: Action Buttons */}
<div className="flex justify-center">
- {mode === "history" && onSelectState && (
+ {onSelectState && (
<Button
size="sm"
onClick={() => onSelectState(version1)}
@@ -453,31 +626,9 @@ function WorkflowComparisonPanel({
</Button>
)}
</div>
- <div className="flex justify-center gap-2">
- {mode === "copilot" && (
- <>
- <Button
- size="sm"
- variant="destructive"
- onClick={() => void settleCopilotReview("reject")}
- disabled={isSettling}
- className="text-xs"
- >
- Reject
- </Button>
- <Button
- size="sm"
- onClick={() => void settleCopilotReview("approve")}
- disabled={isSettling}
- className="bg-green-600 text-xs hover:bg-green-700"
- >
- Accept
- </Button>
- </>
- )}
- </div>
+ <div />
<div className="flex justify-center">
- {mode === "history" && onSelectState && (
+ {onSelectState && (
<Button
size="sm"
onClick={() => onSelectState(version2)}
@@ -492,26 +643,20 @@ function WorkflowComparisonPanel({
<Separator />
- {/* Content - Two columns for comparison */}
<div className="flex-1 overflow-hidden p-4">
<div className="grid h-full grid-cols-2 gap-4">
- {/* Version 1 Column */}
<ReactFlowProvider>
- <WorkflowComparisonRenderer
+ <ReviewFlowCanvas
key={`k1-${version1.workflow_id}v${version1.version}`}
+ elements={leftElements}
version={version1}
- onSelectState={onSelectState}
- blockColors={version1BlockColors}
/>
</ReactFlowProvider>
-
- {/* Version 2 Column */}
<ReactFlowProvider>
- <WorkflowComparisonRenderer
+ <ReviewFlowCanvas
key={`k2-${version2.workflow_id}v${version2.version}`}
+ elements={rightElements}
version={version2}
- onSelectState={onSelectState}
- blockColors={version2BlockColors}
/>
</ReactFlowProvider>
</div>
@@ -520,4 +665,89 @@ function WorkflowComparisonPanel({
);
}
+function WorkflowComparisonPanel({
+ version1,
+ version2,
+ onSelectState,
+ mode = "history",
+ onCopilotReviewClose,
+ onExit,
+}: Props) {
+ const diff = useMemo(
+ () => diffWorkflowVersions(version1, version2),
+ [version1, version2],
+ );
+
+ // Approve / Reject await a server round trip; a second click before it
+ // settles would race a concurrent apply/clear against the same revision.
+ const settlingRef = useRef(false);
+ const [isSettling, setIsSettling] = useState(false);
+ const settleCopilotReview = useCallback(
+ async (status: CopilotReviewStatus) => {
+ if (!onCopilotReviewClose || settlingRef.current) return;
+ settlingRef.current = true;
+ setIsSettling(true);
+ try {
+ await onCopilotReviewClose(status);
+ } finally {
+ settlingRef.current = false;
+ setIsSettling(false);
+ }
+ },
+ [onCopilotReviewClose],
+ );
+
+ // ESC key handler for copilot mode - close without rejecting
+ useEffect(() => {
+ if (mode !== "copilot" || !onCopilotReviewClose) return;
+
+ const handleKeyDown = (event: KeyboardEvent) => {
+ if (event.key === "Escape") {
+ void settleCopilotReview("close");
+ }
+ };
+
+ window.addEventListener("keydown", handleKeyDown);
+ return () => window.removeEventListener("keydown", handleKeyDown);
+ }, [mode, onCopilotReviewClose, settleCopilotReview]);
+
+ // ESC in history mode mirrors "Keep current version" when an exit is wired.
+ useEffect(() => {
+ if (mode !== "history" || !onExit) return;
+
+ const handleKeyDown = (event: KeyboardEvent) => {
+ if (event.key === "Escape") {
+ onExit();
+ }
+ };
+
+ window.addEventListener("keydown", handleKeyDown);
+ return () => window.removeEventListener("keydown", handleKeyDown);
+ }, [mode, onExit]);
+
+ if (mode === "copilot") {
+ return (
+ <ReactFlowProvider>
+ <CopilotReview
+ diff={diff}
+ version={version2}
+ isSettling={isSettling}
+ canClose={Boolean(onCopilotReviewClose)}
+ onSettle={(status) => void settleCopilotReview(status)}
+ />
+ </ReactFlowProvider>
+ );
+ }
+
+ return (
+ <HistoryComparison
+ diff={diff}
+ version1={version1}
+ version2={version2}
+ onSelectState={onSelectState}
+ onExit={onExit}
+ />
+ );
+}
+
export { WorkflowComparisonPanel };
diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/workflowReviewDiff.test.ts b/skyvern-frontend/src/routes/workflows/editor/panels/workflowReviewDiff.test.ts
new file mode 100644
index 000000000..6f8dd03b6
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/panels/workflowReviewDiff.test.ts
@@ -0,0 +1,774 @@
+import { describe, expect, test } from "vitest";
+
+import type { WorkflowVersion } from "../../hooks/useWorkflowVersionsQuery";
+import type { WorkflowBlock } from "../../types/workflowTypes";
+import { apiWorkflowToSettings } from "../apiWorkflowToSettings";
+import { isWorkflowBlockNode, type AppNode } from "../nodes";
+import { getElements } from "../workflowEditorUtils";
+import {
+ diffWords,
+ diffWorkflowVersions,
+ foldUnchangedRuns,
+} from "./workflowReviewDiff";
+
+let idCounter = 0;
+
+// Every call mints fresh ids and timestamps, the way a rehydrated Copilot
+// proposal does for blocks whose content did not change.
+function block(
+ label: string,
+ block_type: string,
+ fields: Record<string, unknown> = {},
+): WorkflowBlock {
+ idCounter += 1;
+ return {
+ label,
+ block_type,
+ continue_on_failure: false,
+ model: null,
+ output_parameter: {
+ parameter_type: "output",
+ key: `${label}_output`,
+ output_parameter_id: `op_${idCounter}`,
+ workflow_id: `w_${idCounter}`,
+ created_at: `2026-09-0${(idCounter % 9) + 1}T00:00:00Z`,
+ modified_at: `2026-09-0${(idCounter % 9) + 1}T00:00:00Z`,
+ deleted_at: null,
+ description: null,
+ },
+ ...fields,
+ } as unknown as WorkflowBlock;
+}
+
+function version(
+ blocks: Array<WorkflowBlock>,
+ overrides: Partial<WorkflowVersion> = {},
+ parameters: Array<unknown> = [],
+): WorkflowVersion {
+ return {
+ title: "wf",
+ workflow_definition: { version: 2, parameters, blocks },
+ ...overrides,
+ } as unknown as WorkflowVersion;
+}
+
+// Reads run order off the rendered canvas, the way a reviewer sees it: the
+// top-level chain, or the chain inside the loop labelled `insideLoop`.
+function canvasOrder(
+ blocks: Array<WorkflowBlock>,
+ v: WorkflowVersion,
+ insideLoop?: string,
+) {
+ const { nodes, edges } = getElements(blocks, apiWorkflowToSettings(v), false);
+ const parentId = insideLoop
+ ? nodes.find(
+ (node) => isWorkflowBlockNode(node) && node.data.label === insideLoop,
+ )?.id
+ : undefined;
+ const level = nodes.filter((node) => node.parentId === parentId);
+ const byId = new Map(level.map((node) => [node.id, node]));
+ const order: Array<string> = [];
+ let cursor: AppNode | undefined = level.find((node) => node.type === "start");
+ const seen = new Set<string>();
+ while (cursor && !seen.has(cursor.id)) {
+ seen.add(cursor.id);
+ if (isWorkflowBlockNode(cursor)) order.push(cursor.data.label);
+ const edge = edges.find(
+ (e) => e.source === cursor!.id && byId.has(e.target),
+ );
+ cursor = edge ? byId.get(edge.target) : undefined;
+ }
+ return order;
+}
+
+function conditional(
+ label: string,
+ targets: Array<string>,
+ next: string | null,
+): WorkflowBlock {
+ return block(label, "conditional", {
+ next_block_label: next,
+ branch_conditions: targets.map((target, index) => ({
+ id: `${label}_${index}_${++idCounter}`,
+ criteria:
+ index === targets.length - 1
+ ? null
+ : {
+ criteria_type: "jinja2_template",
+ expression: `{{ n == ${index} }}`,
+ },
+ next_block_label: target,
+ description: null,
+ is_default: index === targets.length - 1,
+ })),
+ });
+}
+
+function step(label: string, next: string | null, goal = label) {
+ return block(label, "navigation", {
+ navigation_goal: goal,
+ next_block_label: next,
+ });
+}
+
+function branchesOf(v: WorkflowVersion) {
+ return (
+ v.workflow_definition.blocks[0] as unknown as {
+ branch_conditions: Array<{ is_default: boolean; criteria: unknown }>;
+ }
+ ).branch_conditions;
+}
+
+// Where each block lands on the canvas: hidden or not, and which conditional
+// branch holds it.
+function placement(blocks: Array<WorkflowBlock>, v: WorkflowVersion) {
+ const { nodes } = getElements(blocks, apiWorkflowToSettings(v), false);
+ return new Map(
+ nodes.flatMap((node) =>
+ isWorkflowBlockNode(node)
+ ? [
+ [
+ node.data.label,
+ {
+ hidden: Boolean(node.hidden),
+ branch:
+ (node.data as { conditionalBranchId?: string | null })
+ .conditionalBranchId ?? null,
+ },
+ ] as const,
+ ]
+ : [],
+ ),
+ );
+}
+
+function vendorInvoicesBefore() {
+ return [
+ block("open_vendor_portal", "goto_url", { url: "https://vendor.test" }),
+ block("sign_in", "login", { navigation_goal: "Sign in" }),
+ block("go_to_invoices", "navigation", {
+ navigation_goal: "Open the Billing tab and click Invoices.",
+ max_steps_per_run: 10,
+ }),
+ block("notify_finance", "send_email", {
+ subject: "Invoices downloaded",
+ recipients: ["[email protected]"],
+ file_attachments: [],
+ }),
+ block("upload_to_drive", "upload_to_s3", { path: "invoices" }),
+ ];
+}
+
+function vendorInvoicesAfter() {
+ return [
+ block("open_vendor_portal", "goto_url", { url: "https://vendor.test" }),
+ block("sign_in", "login", { navigation_goal: "Sign in" }),
+ block("go_to_invoices", "navigation", {
+ navigation_goal:
+ "Open the Billing tab, click Invoices, and filter to {{ billing_month }}.",
+ max_steps_per_run: 15,
+ }),
+ block("extract_invoice_rows", "extraction", {
+ data_extraction_goal: "Extract every invoice row.",
+ }),
+ block("each_invoice", "for_loop", {
+ loop_variable_reference: "extract_invoice_rows.output",
+ loop_blocks: [
+ block("download_invoice_pdf", "file_download", {
+ navigation_goal: "Click the PDF link.",
+ }),
+ ],
+ }),
+ block("upload_to_drive", "upload_to_s3", { path: "invoices" }),
+ ];
+}
+
+describe("diffWorkflowVersions", () => {
+ test("an edit renders one canvas with the removed block where it ran", () => {
+ const after = version(vendorInvoicesAfter());
+ const diff = diffWorkflowVersions(version(vendorInvoicesBefore()), after);
+
+ expect(canvasOrder(diff.mergedBlocks, after)).toEqual([
+ "open_vendor_portal",
+ "sign_in",
+ "go_to_invoices",
+ "notify_finance",
+ "extract_invoice_rows",
+ "each_invoice",
+ "upload_to_drive",
+ ]);
+ const status = (label: string) => diff.merged.get(label)?.status;
+ expect(status("open_vendor_portal")).toBe("unchanged");
+ expect(status("sign_in")).toBe("unchanged");
+ expect(status("go_to_invoices")).toBe("changed");
+ expect(status("extract_invoice_rows")).toBe("new");
+ expect(status("download_invoice_pdf")).toBe("new");
+ expect(status("notify_finance")).toBe("removed");
+ expect(diff.merged.get("go_to_invoices")?.changes).toEqual([
+ {
+ key: "navigation_goal",
+ label: "Navigation goal",
+ before: "Open the Billing tab and click Invoices.",
+ after:
+ "Open the Billing tab, click Invoices, and filter to {{ billing_month }}.",
+ },
+ {
+ key: "max_steps_per_run",
+ label: "Max steps",
+ before: "10",
+ after: "15",
+ },
+ ]);
+ expect(diff.counts).toEqual({
+ new: 3,
+ changed: 1,
+ removed: 1,
+ unchanged: 3,
+ });
+ expect(diff.changeOrder).toEqual([
+ "go_to_invoices",
+ "notify_finance",
+ "extract_invoice_rows",
+ "each_invoice",
+ "download_invoice_pdf",
+ ]);
+ expect(diff.isNewWorkflow).toBe(false);
+ });
+
+ test("a loop's children are compared one by one, removed ones included", () => {
+ const loop = (children: Array<WorkflowBlock>) =>
+ block("each_row", "for_loop", {
+ loop_variable_reference: "rows",
+ loop_blocks: children,
+ });
+ const openRow = () =>
+ block("open_row", "navigation", { navigation_goal: "Open row" });
+ const after = version([
+ loop([
+ openRow(),
+ block("save_row", "navigation", { navigation_goal: "Save it twice" }),
+ ]),
+ ]);
+ const diff = diffWorkflowVersions(
+ version([
+ loop([
+ openRow(),
+ block("check_row", "validation", { complete_criterion: "Row open" }),
+ block("save_row", "navigation", { navigation_goal: "Save it" }),
+ ]),
+ ]),
+ after,
+ );
+
+ expect(canvasOrder(diff.mergedBlocks, after, "each_row")).toEqual([
+ "open_row",
+ "check_row",
+ "save_row",
+ ]);
+ expect(diff.merged.get("each_row")).toMatchObject({
+ status: "unchanged",
+ containsChanges: true,
+ });
+ expect(diff.merged.get("open_row")?.status).toBe("unchanged");
+ expect(diff.merged.get("check_row")?.status).toBe("removed");
+ expect(diff.merged.get("save_row")?.status).toBe("changed");
+ expect(diff.changeOrder).toEqual(["check_row", "save_row"]);
+ });
+
+ test("removed first and last blocks stay at the ends, and a type change keeps both cards", () => {
+ const after = version([
+ block("fill_form", "navigation", { navigation_goal: "Fill" }),
+ block("summary", "extraction", { data_extraction_goal: "Read it" }),
+ ]);
+ const diff = diffWorkflowVersions(
+ version([
+ block("open_site", "goto_url", { url: "https://a.test" }),
+ block("fill_form", "navigation", { navigation_goal: "Fill" }),
+ block("summary", "text_prompt", { prompt: "Summarize" }),
+ ]),
+ after,
+ );
+
+ expect(canvasOrder(diff.mergedBlocks, after)).toEqual([
+ "open_site",
+ "fill_form",
+ "summary (removed)",
+ "summary",
+ ]);
+ expect(diff.merged.get("summary")?.status).toBe("new");
+
+ const unfinalized = version([step("a", "b"), step("b", null)]);
+ const finalized = version([
+ step("cleanup", null),
+ step("a", "b"),
+ step("b", null),
+ ]);
+ finalized.workflow_definition.finally_block_label = "cleanup";
+ const finallyRemoved = diffWorkflowVersions(finalized, unfinalized);
+ expect(canvasOrder(finallyRemoved.mergedBlocks, unfinalized)).toEqual([
+ "a",
+ "b",
+ "cleanup",
+ ]);
+ expect(diff.merged.get("summary (removed)")?.status).toBe("removed");
+ expect(diff.before.get("summary")?.status).toBe("removed");
+ });
+
+ test("run order, not storage order, places removed blocks", () => {
+ const after = version([step("c", null, "edited"), step("a", "c")]);
+ const removal = diffWorkflowVersions(
+ version([step("c", null), step("a", "b"), step("b", "c")]),
+ after,
+ );
+ expect(canvasOrder(removal.mergedBlocks, after)).toEqual(["a", "b", "c"]);
+ expect(removal.changeOrder).toEqual(["b", "c"]);
+ });
+
+ test("removed blocks around a conditional keep their branch and never hide kept blocks", () => {
+ const flattened = version([
+ step("a", "x"),
+ step("x", "z"),
+ step("z", "w", "edited"),
+ step("w", null),
+ ]);
+ const unwrap = diffWorkflowVersions(
+ version([
+ step("a", "c"),
+ conditional("c", ["x", "y"], "z"),
+ step("x", "z"),
+ step("y", "z"),
+ step("z", "w"),
+ step("w", null),
+ ]),
+ flattened,
+ );
+ expect(canvasOrder(unwrap.mergedBlocks, flattened)).toEqual([
+ "a",
+ "c",
+ "x",
+ "z",
+ "w",
+ ]);
+ const unwrapped = placement(unwrap.mergedBlocks, flattened);
+ expect(unwrapped.get("z")?.hidden).toBe(false);
+ expect(unwrapped.get("y")?.branch).not.toBeNull();
+
+ const inlined = version([
+ step("a", "x"),
+ step("x", "y"),
+ step("y", "z"),
+ step("z", null),
+ ]);
+ const stale = diffWorkflowVersions(
+ version([
+ step("a", "c"),
+ conditional("c", ["x", "y"], "z"),
+ step("x", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]),
+ inlined,
+ );
+ expect(canvasOrder(stale.mergedBlocks, inlined)).toEqual([
+ "a",
+ "c",
+ "x",
+ "y",
+ "z",
+ ]);
+
+ const trimmed = version([
+ conditional("c", ["x", "y"], "z"),
+ step("x", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]);
+ const tail = diffWorkflowVersions(
+ version([
+ conditional("c", ["x", "y"], "z"),
+ step("x", "r"),
+ step("r", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]),
+ trimmed,
+ );
+ const branches = placement(tail.mergedBlocks, trimmed);
+ expect(branches.get("r")?.branch).toBe(branches.get("x")?.branch);
+ expect(branches.get("r")?.hidden).toBe(false);
+ expect(tail.merged.get("c")?.status).toBe("unchanged");
+
+ const rejoined = version([
+ conditional("c", ["n", "y"], "z"),
+ step("n", "x"),
+ step("x", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]);
+ const merge = diffWorkflowVersions(
+ version([
+ conditional("c", ["x", "y"], "r"),
+ step("x", "r"),
+ step("y", "r"),
+ step("r", "z"),
+ step("z", null),
+ ]),
+ rejoined,
+ );
+ expect(canvasOrder(merge.mergedBlocks, rejoined)).toEqual(["c", "r", "z"]);
+ expect(placement(merge.mergedBlocks, rejoined).get("z")?.hidden).toBe(
+ false,
+ );
+ expect(merge.merged.get("c")?.status).toBe("unchanged");
+
+ const narrowed = version([
+ conditional("c", ["x", "y"], "z"),
+ step("x", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]);
+ const dropped = diffWorkflowVersions(
+ version([
+ conditional("c", ["x", "b", "y"], "z"),
+ step("x", "z"),
+ step("b", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]),
+ narrowed,
+ );
+ expect(canvasOrder(dropped.mergedBlocks, narrowed)).toEqual(["c", "z"]);
+ expect(
+ placement(dropped.mergedBlocks, narrowed).get("b")?.branch,
+ ).not.toBeNull();
+
+ const elseless = version([
+ conditional("c", ["x", "y"], "z"),
+ step("x", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]);
+ const lastBranch = branchesOf(elseless)[1]!;
+ lastBranch.is_default = false;
+ lastBranch.criteria = {
+ criteria_type: "jinja2_template",
+ expression: "{{ n == 1 }}",
+ };
+ const droppedElse = diffWorkflowVersions(
+ version([
+ conditional("c", ["x", "y", "e"], "z"),
+ step("x", "z"),
+ step("y", "z"),
+ step("e", "z"),
+ step("z", null),
+ ]),
+ elseless,
+ );
+ expect(canvasOrder(droppedElse.mergedBlocks, elseless)).toEqual(["c", "z"]);
+ expect(
+ placement(droppedElse.mergedBlocks, elseless).get("e")?.branch,
+ ).not.toBeNull();
+
+ const widened = version([
+ conditional("c", ["x", "w", "y"], "z"),
+ step("x", "z"),
+ step("w", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]);
+ branchesOf(widened)[2]!.criteria = {
+ criteria_type: "jinja2_template",
+ expression: "",
+ };
+ const elseReplaced = diffWorkflowVersions(
+ version([
+ conditional("c", ["x", "e"], "z"),
+ step("x", "z"),
+ step("e", "z"),
+ step("z", null),
+ ]),
+ widened,
+ );
+ expect(canvasOrder(elseReplaced.mergedBlocks, widened)).toEqual(["c", "z"]);
+
+ const branchesOnly = (targets: Array<string>) =>
+ version([
+ conditional("c", targets, "z"),
+ step("x", "z"),
+ step("y", "z"),
+ step("z", null),
+ ]);
+ const swap = diffWorkflowVersions(
+ branchesOnly(["x", "y"]),
+ branchesOnly(["y", "x"]),
+ );
+ expect(swap.merged.get("c")?.changes).toEqual([
+ {
+ key: "__branch_routes",
+ label: "Branch routes",
+ before: "Branch 1 → x, Else → y",
+ after: "Branch 1 → y, Else → x",
+ },
+ ]);
+ });
+
+ test("an edited input is reviewed on Start, not on each block that reads it", () => {
+ const input = (description: string) => ({
+ parameter_type: "workflow",
+ key: "vendor",
+ workflow_parameter_type: "string",
+ workflow_parameter_id: `wp_${++idCounter}`,
+ default_value: null,
+ description,
+ });
+ const reader = (description: string) =>
+ block("open_portal", "navigation", {
+ navigation_goal: "Open {{ vendor }}",
+ parameters: [input(description)],
+ });
+ const diff = diffWorkflowVersions(
+ version([reader("old")], {}, [input("old")]),
+ version([reader("new")], {}, [input("new")]),
+ );
+
+ expect(diff.merged.get("open_portal")?.status).toBe("unchanged");
+ expect(diff.inputs).toMatchObject([{ key: "vendor", status: "changed" }]);
+ });
+
+ test("input and setting changes are reported without serializer noise", () => {
+ const workflowInput = (
+ key: string,
+ extra: Record<string, unknown> = {},
+ ) => ({
+ parameter_type: "workflow",
+ key,
+ workflow_parameter_type: "string",
+ workflow_parameter_id: `wp_${key}_${++idCounter}`,
+ default_value: null,
+ description: null,
+ ...extra,
+ });
+ const diff = diffWorkflowVersions(
+ version(
+ [],
+ {
+ max_elapsed_time_minutes: 15,
+ extra_http_headers: { a: "1", b: "2" },
+ run_with: "agent",
+ code_version: null,
+ },
+ [workflowInput("vendor")],
+ ),
+ version(
+ [],
+ {
+ max_elapsed_time_minutes: 25,
+ extra_http_headers: { b: "2", a: "1" },
+ run_with: "agent",
+ code_version: 2,
+ },
+ [workflowInput("vendor"), workflowInput("billing_month")],
+ ),
+ );
+
+ expect(diff.inputs).toEqual([
+ { key: "billing_month", status: "new", detail: "string", changes: [] },
+ ]);
+ expect(diff.settings).toEqual([
+ {
+ key: "maxElapsedTimeMinutes",
+ label: "Max run time (minutes)",
+ before: "15",
+ after: "25",
+ },
+ ]);
+ });
+
+ test("secret values are hidden while their change still shows", () => {
+ const upload = (password: string, token: string) =>
+ block("upload_report", "file_upload", {
+ sftp_password: password,
+ headers: {
+ Authorization: `Bearer ${token}`,
+ "X-CSRF-Token": `csrf-${token}`,
+ Accept: "text/csv",
+ },
+ });
+ const diff = diffWorkflowVersions(
+ version([upload("old-pass", "old-token")]),
+ version([upload("new-pass", "new-token")]),
+ );
+
+ const changes = diff.merged.get("upload_report")?.changes ?? [];
+ expect(changes.map((change) => change.key).sort()).toEqual([
+ "headers",
+ "sftp_password",
+ ]);
+ const shown = JSON.stringify(changes);
+ for (const secret of ["old-pass", "new-pass", "old-token", "new-token"]) {
+ expect(shown).not.toContain(secret);
+ }
+ expect(shown).toContain("text/csv");
+
+ const tokenInput = (value: string) => ({
+ parameter_type: "workflow",
+ key: "api_token",
+ workflow_parameter_type: "string",
+ workflow_parameter_id: `wp_${++idCounter}`,
+ default_value: value,
+ description: null,
+ });
+ const settingsFor = (headers: unknown, token: string) =>
+ version(
+ [],
+ {
+ extra_http_headers: headers as unknown as Record<string, string>,
+ },
+ [tokenInput(token)],
+ );
+ const inputs = diffWorkflowVersions(
+ settingsFor('{"Authorization": "Bearer old-token"', "old-default"),
+ settingsFor('{"Authorization": "Bearer new-token"', "new-default"),
+ );
+ const startShown = JSON.stringify([...inputs.inputs, ...inputs.settings]);
+ expect(inputs.inputs).toMatchObject([
+ { key: "api_token", status: "changed" },
+ ]);
+ for (const secret of [
+ "old-default",
+ "new-default",
+ "old-token",
+ "new-token",
+ ]) {
+ expect(startShown).not.toContain(secret);
+ }
+
+ const sessionHeaders = (token: string) =>
+ settingsFor({ "X-Amz-Security-Token": token }, "same-default");
+ const cdp = diffWorkflowVersions(
+ sessionHeaders("old-session"),
+ sessionHeaders("new-session"),
+ );
+ expect(cdp.settings.map((change) => change.key)).toEqual([
+ "extraHttpHeaders",
+ ]);
+ const cdpShown = JSON.stringify(cdp.settings);
+ expect(cdpShown).not.toContain("old-session");
+ expect(cdpShown).not.toContain("new-session");
+ });
+
+ test("a title-only proposal is reported, not read as no changes", () => {
+ const diff = diffWorkflowVersions(
+ version([], { title: "Download invoices" }),
+ version([], { title: "Download vendor invoices" }),
+ );
+
+ expect(diff.settings).toEqual([
+ {
+ key: "title",
+ label: "Title",
+ before: "Download invoices",
+ after: "Download vendor invoices",
+ },
+ ]);
+ });
+
+ test("a brand-new workflow is flagged as new with every block new", () => {
+ const diff = diffWorkflowVersions(
+ version([]),
+ version([
+ block("open_store", "goto_url", { url: "https://shop.test" }),
+ block("find_order", "navigation", { navigation_goal: "Find it" }),
+ ]),
+ );
+
+ expect(diff.isNewWorkflow).toBe(true);
+ expect(diff.counts).toEqual({
+ new: 2,
+ changed: 0,
+ removed: 0,
+ unchanged: 0,
+ });
+ });
+});
+
+describe("foldUnchangedRuns", () => {
+ test("folds every run of unchanged blocks and keeps the chain intact", () => {
+ const labels = ["a", "b", "c", "d", "e", "f"];
+ const make = (edited: Array<string>) =>
+ labels.map((label) =>
+ block(label, "navigation", {
+ navigation_goal: edited.includes(label) ? "edited" : label,
+ }),
+ );
+ const after = version(make(["a", "e"]));
+ const diff = diffWorkflowVersions(version(make([])), after);
+
+ const folded = foldUnchangedRuns(
+ diff.mergedBlocks,
+ diff.merged,
+ new Set(),
+ null,
+ );
+ expect(canvasOrder(folded.blocks, after)).toEqual([
+ "a",
+ "__review_fold__b",
+ "e",
+ "__review_fold__f",
+ ]);
+ expect([...folded.folds.values()]).toEqual([
+ { key: "b", count: 3 },
+ { key: "f", count: 1 },
+ ]);
+
+ const expanded = foldUnchangedRuns(
+ diff.mergedBlocks,
+ diff.merged,
+ new Set(["b", "f"]),
+ null,
+ );
+ expect(canvasOrder(expanded.blocks, after)).toEqual(labels);
+
+ const stored = (goal: string) =>
+ version([
+ step("c", "d"),
+ step("a", "b"),
+ step("b", "c"),
+ step("d", null, goal),
+ ]);
+ const shuffled = stored("edited");
+ const reordered = diffWorkflowVersions(stored("d"), shuffled);
+ const foldedInRunOrder = foldUnchangedRuns(
+ reordered.mergedBlocks,
+ reordered.merged,
+ new Set(),
+ null,
+ );
+ expect(canvasOrder(foldedInRunOrder.blocks, shuffled)).toEqual([
+ "__review_fold__a",
+ "d",
+ ]);
+ expect([...foldedInRunOrder.folds.values()]).toEqual([
+ { key: "a", count: 3 },
+ ]);
+ });
+});
+
+describe("diffWords", () => {
+ test("a one-word edit to a long prompt highlights only that word", () => {
+ const words = Array.from({ length: 150 }, (_, index) => `word${index}`);
+ const before = words.join(" ");
+ const after = words
+ .map((word, index) => (index === 75 ? "edited" : word))
+ .join(" ");
+
+ const diff = diffWords(before, after);
+
+ expect(diff.before.filter((s) => s.changed).map((s) => s.text)).toEqual([
+ "word75",
+ ]);
+ expect(diff.after.filter((s) => s.changed).map((s) => s.text)).toEqual([
+ "edited",
+ ]);
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/workflowReviewDiff.ts b/skyvern-frontend/src/routes/workflows/editor/panels/workflowReviewDiff.ts
new file mode 100644
index 000000000..f2589192e
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/panels/workflowReviewDiff.ts
@@ -0,0 +1,1145 @@
+import { stableStringify } from "@/util/stableStringify";
+
+import type { WorkflowVersion } from "../../hooks/useWorkflowVersionsQuery";
+import {
+ isNestedLoopWorkflowBlock,
+ type Parameter,
+ type WorkflowBlock,
+ type WorkflowSettings,
+} from "../../types/workflowTypes";
+import {
+ areValuesEquivalent,
+ diffBlockFields,
+ diffRecordFields,
+ isEmptyValue,
+ shouldOmitForComparison,
+ type RawFieldChange,
+} from "../../util/compareBlocks";
+import { apiWorkflowToSettings } from "../apiWorkflowToSettings";
+import {
+ applySequentialDefaulting,
+ findChainRoot,
+} from "../workflowEditorUtils";
+
+export type ReviewStatus = "unchanged" | "new" | "changed" | "removed";
+
+export type ReviewFieldChange = {
+ key: string;
+ label: string;
+ before: string | null;
+ after: string | null;
+};
+
+export type BlockReview = {
+ status: ReviewStatus;
+ changes: Array<ReviewFieldChange>;
+ // A block nested under this one differs, so this block must stay visible.
+ containsChanges: boolean;
+};
+
+export type InputReview = {
+ key: string;
+ status: Exclude<ReviewStatus, "unchanged">;
+ detail: string | null;
+ changes: Array<ReviewFieldChange>;
+};
+
+export type WorkflowReviewDiff = {
+ // The proposal's blocks with removed ones threaded back in where they sat.
+ mergedBlocks: Array<WorkflowBlock>;
+ merged: Map<string, BlockReview>;
+ before: Map<string, BlockReview>;
+ after: Map<string, BlockReview>;
+ // Labels in mergedBlocks of every block that is not unchanged, in run order.
+ changeOrder: Array<string>;
+ inputs: Array<InputReview>;
+ settings: Array<ReviewFieldChange>;
+ counts: Record<ReviewStatus, number>;
+ isNewWorkflow: boolean;
+};
+
+// Matches the canvas node types so task_v2 and navigation blocks with the same
+// label are the same block.
+const BLOCK_TYPE_TO_NODE_TYPE: Record<string, string> = {
+ task: "task",
+ task_v2: "navigation",
+ validation: "validation",
+ action: "action",
+ navigation: "navigation",
+ extraction: "extraction",
+ login: "login",
+ wait: "wait",
+ terminate: "terminate",
+ file_download: "fileDownload",
+ code: "codeBlock",
+ send_email: "sendEmail",
+ text_prompt: "textPrompt",
+ for_loop: "loop",
+ while_loop: "loop",
+ file_url_parser: "fileParser",
+ pdf_parser: "pdfParser",
+ download_to_s3: "download",
+ upload_to_s3: "upload",
+ file_upload: "fileUpload",
+ goto_url: "url",
+ http_request: "http_request",
+ pdf_fill: "pdfFill",
+ split_pdf: "splitPdf",
+ email_inbox: "emailInbox",
+ google_sheets_read: "googleSheetsRead",
+ google_sheets_write: "googleSheetsWrite",
+};
+
+function blockIdentity(block: WorkflowBlock): string {
+ const nodeType =
+ BLOCK_TYPE_TO_NODE_TYPE[block.block_type] ?? block.block_type;
+ return `${nodeType}:${block.label}`;
+}
+
+const ACRONYMS = new Set([
+ "ai",
+ "cdp",
+ "css",
+ "http",
+ "id",
+ "ip",
+ "json",
+ "llm",
+ "pdf",
+ "sql",
+ "totp",
+ "url",
+]);
+
+const FIELD_LABEL_OVERRIDES: Record<string, string> = {
+ wait_sec: "Wait (seconds)",
+ loop_variable_reference: "Loop over",
+ max_steps_per_run: "Max steps",
+ block_type: "Block type",
+};
+
+function humanizeKey(key: string): string {
+ const override = FIELD_LABEL_OVERRIDES[key];
+ if (override) return override;
+ const words = key
+ .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
+ .split(/[_\s]+/)
+ .filter(Boolean)
+ .map((word) => word.toLowerCase())
+ .map((word) => (ACRONYMS.has(word) ? word.toUpperCase() : word));
+ const [first = "", ...rest] = words;
+ return [first.charAt(0).toUpperCase() + first.slice(1), ...rest].join(" ");
+}
+
+function formatValue(value: unknown): string | null {
+ if (isEmptyValue(value)) return null;
+ if (typeof value === "string") return value;
+ if (typeof value === "number") return String(value);
+ if (typeof value === "boolean") return value ? "Yes" : "No";
+ if (Array.isArray(value)) {
+ if (
+ value.every(
+ (item) => typeof item === "string" || typeof item === "number",
+ )
+ ) {
+ return value.join(", ");
+ }
+ if (
+ value.every(
+ (item) =>
+ item !== null &&
+ typeof item === "object" &&
+ typeof (item as { key?: unknown }).key === "string",
+ )
+ ) {
+ return value.map((item) => (item as { key: string }).key).join(", ");
+ }
+ }
+ if (
+ value !== null &&
+ typeof value === "object" &&
+ typeof (value as { model_name?: unknown }).model_name === "string"
+ ) {
+ return (value as { model_name: string }).model_name;
+ }
+ const stable = stableStringify(value, { omit: shouldOmitForComparison });
+ return stable === undefined
+ ? null
+ : JSON.stringify(JSON.parse(stable), null, 2);
+}
+
+// Secret fields render as password inputs in their editors, and the review
+// card is plain text, so their values never reach it; a change still shows.
+const SECRET_KEY =
+ /password|passphrase|secret|private_?key|account_?key|api[_-]?(key|token)|(access|auth|bearer|refresh|session)[_-]?token|^token$|authorization|cookie/i;
+const HIDDEN_VALUE = "•••••••• (hidden)";
+
+// Header maps take arbitrary names (X-CSRF-Token, X-Amz-Security-Token, ...),
+// so their values are hidden unless the name is known to carry no credential.
+const HEADERS_KEY = /headers$/i;
+const PUBLIC_HEADERS: ReadonlySet<string> = new Set([
+ "accept",
+ "accept-encoding",
+ "accept-language",
+ "cache-control",
+ "content-type",
+ "origin",
+ "referer",
+ "user-agent",
+]);
+
+function redactHeaders(value: object): Record<string, unknown> {
+ return Object.fromEntries(
+ Object.entries(value).map(([name, item]) => [
+ name,
+ PUBLIC_HEADERS.has(name.toLowerCase()) || isEmptyValue(item)
+ ? item
+ : HIDDEN_VALUE,
+ ]),
+ );
+}
+
+function redactSecrets(key: string, value: unknown): unknown {
+ if (isEmptyValue(value)) return value;
+ if (SECRET_KEY.test(key)) return HIDDEN_VALUE;
+ if (HEADERS_KEY.test(key) && typeof value === "object" && value !== null) {
+ return Array.isArray(value) ? HIDDEN_VALUE : redactHeaders(value);
+ }
+ if (Array.isArray(value)) return value.map((item) => redactSecrets("", item));
+ if (value !== null && typeof value === "object") {
+ return Object.fromEntries(
+ Object.entries(value).map(([nested, item]) => [
+ nested,
+ redactSecrets(nested, item),
+ ]),
+ );
+ }
+ return value;
+}
+
+function toReviewChange(
+ change: RawFieldChange,
+ label = humanizeKey(change.key),
+ hidden = false,
+): ReviewFieldChange {
+ const shown = (value: unknown) =>
+ formatValue(
+ hidden && !isEmptyValue(value)
+ ? HIDDEN_VALUE
+ : redactSecrets(change.key, value),
+ );
+ return {
+ key: change.key,
+ label,
+ before: shown(change.before),
+ after: shown(change.after),
+ };
+}
+
+function clone<T>(value: T): T {
+ return JSON.parse(JSON.stringify(value)) as T;
+}
+
+type ConditionalLike = {
+ branch_conditions?: Array<{
+ next_block_label?: string | null;
+ criteria?: unknown;
+ is_default?: boolean;
+ }>;
+};
+
+function branchesOf(block: WorkflowBlock) {
+ return (block as ConditionalLike).branch_conditions ?? [];
+}
+
+// An edge out of a block: its `next_block_label` (branch null) or the target
+// of one of its conditional branches.
+type Slot = { owner: string; branch: number | null };
+
+function slotTarget(block: WorkflowBlock, branch: number | null) {
+ return branch === null
+ ? (block.next_block_label ?? null)
+ : (branchesOf(block)[branch]?.next_block_label ?? null);
+}
+
+function setSlot(
+ block: WorkflowBlock,
+ branch: number | null,
+ target: string | null,
+): void {
+ if (branch === null) {
+ block.next_block_label = target;
+ return;
+ }
+ const condition = branchesOf(block)[branch];
+ if (condition) condition.next_block_label = target;
+}
+
+// Points every edge into `from` at `to`, so a block inserted before `from`
+// takes over its inbound edges, branch targets included.
+function repointReferences(
+ level: Array<WorkflowBlock>,
+ from: string,
+ to: string,
+): void {
+ for (const block of level) {
+ if (block.next_block_label === from) {
+ block.next_block_label = to;
+ }
+ for (const branch of (block as ConditionalLike).branch_conditions ?? []) {
+ if (branch.next_block_label === from) {
+ branch.next_block_label = to;
+ }
+ }
+ }
+}
+
+function chainTail(
+ level: Array<WorkflowBlock>,
+ finallyLabel: string | null,
+): WorkflowBlock | null {
+ const chain = level.filter((block) => block.label !== finallyLabel);
+ const byLabel = new Map(chain.map((block) => [block.label, block]));
+ const seen = new Set<string>();
+ let cursor = findChainRoot(chain);
+ let tail: WorkflowBlock | null = null;
+ while (cursor && !seen.has(cursor.label)) {
+ seen.add(cursor.label);
+ tail = cursor;
+ cursor = byLabel.get(cursor.next_block_label ?? "") ?? null;
+ }
+ return tail ?? chain[chain.length - 1] ?? null;
+}
+
+function collectLabels(blocks: Array<WorkflowBlock>, into: Set<string>): void {
+ for (const block of blocks) {
+ into.add(block.label);
+ if (isNestedLoopWorkflowBlock(block)) {
+ collectLabels(block.loop_blocks, into);
+ }
+ }
+}
+
+type DiffState = {
+ merged: Map<string, BlockReview>;
+ before: Map<string, BlockReview>;
+ after: Map<string, BlockReview>;
+ usedLabels: Set<string>;
+};
+
+function uniqueLabel(label: string, used: Set<string>): string {
+ let candidate = label;
+ for (let n = 1; used.has(candidate); n++) {
+ candidate = n === 1 ? `${label} (removed)` : `${label} (removed ${n})`;
+ }
+ used.add(candidate);
+ return candidate;
+}
+
+function recordSubtree(
+ blocks: Array<WorkflowBlock>,
+ status: "new" | "removed",
+ state: DiffState,
+): void {
+ for (const block of blocks) {
+ const review: BlockReview = {
+ status,
+ changes: [],
+ containsChanges: false,
+ };
+ state.merged.set(block.label, review);
+ if (status === "new") state.after.set(block.label, review);
+ if (isNestedLoopWorkflowBlock(block)) {
+ recordSubtree(block.loop_blocks, status, state);
+ }
+ }
+}
+
+// A removed block can share its label with a block Copilot added (a type
+// change), and the canvas needs unique labels, so a clashing one is renamed.
+function removedCopy(block: WorkflowBlock, state: DiffState): WorkflowBlock {
+ const copy = clone(block);
+ const relabel = (level: Array<WorkflowBlock>) => {
+ const renames = new Map<string, string>();
+ for (const item of level) {
+ const original = item.label;
+ state.before.set(original, {
+ status: "removed",
+ changes: [],
+ containsChanges: false,
+ });
+ item.label = uniqueLabel(original, state.usedLabels);
+ renames.set(original, item.label);
+ if (isNestedLoopWorkflowBlock(item)) relabel(item.loop_blocks);
+ }
+ for (const item of level) {
+ const next = item.next_block_label;
+ if (next && renames.has(next)) item.next_block_label = renames.get(next);
+ for (const branch of branchesOf(item)) {
+ const target = branch.next_block_label;
+ if (target && renames.has(target)) {
+ branch.next_block_label = renames.get(target);
+ }
+ }
+ }
+ };
+ relabel([copy]);
+ recordSubtree([copy], "removed", state);
+ return copy;
+}
+
+// The order the canvas draws a level in: the next_block_label chain, each
+// conditional's branches before its merge point, and the finally block last.
+// Array order need not match it.
+function runOrder(
+ level: Array<WorkflowBlock>,
+ finallyLabel: string | null,
+): Array<WorkflowBlock> {
+ const chain = level.filter((block) => block.label !== finallyLabel);
+ const byLabel = new Map(chain.map((block) => [block.label, block]));
+ const ordered: Array<WorkflowBlock> = [];
+ const seen = new Set<string>();
+ const walk = (start: string | null | undefined, stop: string | null) => {
+ let cursor = byLabel.get(start ?? "");
+ while (cursor && cursor.label !== stop && !seen.has(cursor.label)) {
+ seen.add(cursor.label);
+ ordered.push(cursor);
+ for (const branch of branchesOf(cursor)) {
+ walk(branch.next_block_label, cursor.next_block_label ?? null);
+ }
+ cursor = byLabel.get(cursor.next_block_label ?? "");
+ }
+ };
+ walk(findChainRoot(chain)?.label, null);
+ return [
+ ...ordered,
+ ...chain.filter((block) => !seen.has(block.label)),
+ ...level.filter((block) => block.label === finallyLabel),
+ ];
+}
+
+// The first edge into each block, in run order, so a merge point is entered
+// from its conditional rather than from the end of a branch.
+function firstInboundSlots(order: Array<WorkflowBlock>): Map<string, Slot> {
+ const inbound = new Map<string, Slot>();
+ for (const block of order) {
+ const slots: Array<number | null> = [null, ...branchesOf(block).keys()];
+ for (const branch of slots) {
+ const target = slotTarget(block, branch);
+ if (target && !inbound.has(target)) {
+ inbound.set(target, { owner: block.label, branch });
+ }
+ }
+ }
+ return inbound;
+}
+
+// Labels reached from a conditional's branches before they rejoin at `merge`.
+function branchMembers(
+ level: Array<WorkflowBlock>,
+ conditional: WorkflowBlock,
+ merge: string | null,
+): Set<string> {
+ const byLabel = new Map(level.map((block) => [block.label, block]));
+ const members = new Set<string>();
+ const pending = branchesOf(conditional).map((b) => b.next_block_label);
+ while (pending.length > 0) {
+ const label = pending.pop();
+ if (!label || label === merge || members.has(label)) continue;
+ const block = byLabel.get(label);
+ if (!block) continue;
+ members.add(label);
+ pending.push(
+ block.next_block_label,
+ ...branchesOf(block).map((b) => b.next_block_label),
+ );
+ }
+ return members;
+}
+
+// Before and after conditionals share a branch when their criteria match, or
+// by position when neither side added or dropped a branch.
+function matchingBranch(
+ prior: WorkflowBlock,
+ current: WorkflowBlock,
+ branch: number,
+): number | null {
+ const criteria = (block: WorkflowBlock, index: number) =>
+ stableStringify(
+ {
+ criteria: branchesOf(block)[index]?.criteria ?? null,
+ is_default: branchesOf(block)[index]?.is_default ?? false,
+ },
+ { omit: shouldOmitForComparison },
+ );
+ if (branchesOf(prior)[branch]?.is_default) {
+ const otherElse = branchesOf(current).findIndex((b) => b.is_default);
+ if (otherElse >= 0) return otherElse;
+ }
+ const wanted = criteria(prior, branch);
+ const byCriteria = branchesOf(current).findIndex(
+ (_, index) => criteria(current, index) === wanted,
+ );
+ if (byCriteria >= 0) return byCriteria;
+ return branchesOf(prior).length === branchesOf(current).length
+ ? branch
+ : null;
+}
+
+// Where a branch leads, read as the first block it reaches that both versions
+// share, so a block added at the head of a branch does not read as a re-route.
+function branchRoute(
+ level: Array<WorkflowBlock>,
+ start: string | null | undefined,
+ merge: string | null,
+ shared: (block: WorkflowBlock) => boolean,
+): string | null {
+ const byLabel = new Map(level.map((block) => [block.label, block]));
+ const seen = new Set<string>();
+ let cursor = byLabel.get(start ?? "");
+ while (cursor && cursor.label !== merge && !seen.has(cursor.label)) {
+ if (shared(cursor)) return cursor.label;
+ seen.add(cursor.label);
+ cursor = byLabel.get(cursor.next_block_label ?? "");
+ }
+ return null;
+}
+
+function describeRoutes(
+ block: WorkflowBlock,
+ routes: Array<string | null>,
+): string {
+ return routes
+ .map((route, index) => {
+ const name = branchesOf(block)[index]?.is_default
+ ? "Else"
+ : `Branch ${index + 1}`;
+ return `${name} → ${route ?? "end"}`;
+ })
+ .join(", ");
+}
+
+// The proposal dropped this branch, so the review canvas keeps a copy of it
+// (ahead of Else, or as Else when the proposal has none); the removed blocks
+// that ran there stay inside the conditional instead of reading as steps
+// after its merge point.
+function keepDroppedBranch(
+ prior: WorkflowBlock,
+ current: WorkflowBlock,
+ branch: number,
+): number | null {
+ const dropped = branchesOf(prior)[branch];
+ const conditions = (current as ConditionalLike).branch_conditions;
+ if (!dropped || !conditions) return null;
+ const elseIndex = conditions.findIndex((condition) => condition.is_default);
+ // A conditional allows one Else at most.
+ if (dropped.is_default && elseIndex >= 0) return null;
+ const copy = {
+ ...clone(dropped),
+ id: `${(dropped as { id?: string }).id ?? "branch"}-removed`,
+ next_block_label: null,
+ };
+ conditions.splice(elseIndex < 0 ? conditions.length : elseIndex, 0, copy);
+ return conditions.indexOf(copy);
+}
+
+function mergeLevel(
+ beforeLevel: Array<WorkflowBlock>,
+ afterLevel: Array<WorkflowBlock>,
+ finallyLabels: { before: string | null; after: string | null },
+ state: DiffState,
+): Array<WorkflowBlock> {
+ const finallyLabel = finallyLabels.after;
+ const beforeByIdentity = new Map(
+ beforeLevel.map((block) => [blockIdentity(block), block]),
+ );
+ const afterIdentities = new Set(afterLevel.map(blockIdentity));
+ const beforeOrder = runOrder(beforeLevel, finallyLabels.before);
+ const merged = afterLevel.map((block) => {
+ const prior = beforeByIdentity.get(blockIdentity(block));
+ if (!prior) {
+ recordSubtree([block], "new", state);
+ return block;
+ }
+ const changes = diffBlockFields(prior, block).map((change) =>
+ toReviewChange(change),
+ );
+ if (branchesOf(block).length > 0) {
+ const beforeRoutes = branchesOf(prior).map((branch) =>
+ branchRoute(
+ beforeLevel,
+ branch.next_block_label,
+ prior.next_block_label ?? null,
+ (candidate) => afterIdentities.has(blockIdentity(candidate)),
+ ),
+ );
+ const afterRoutes = branchesOf(block).map((branch) =>
+ branchRoute(
+ afterLevel,
+ branch.next_block_label,
+ block.next_block_label ?? null,
+ (candidate) => beforeByIdentity.has(blockIdentity(candidate)),
+ ),
+ );
+ const rerouted = afterRoutes.some((route, index) => {
+ const match = branchesOf(prior).findIndex(
+ (_, priorIndex) => matchingBranch(prior, block, priorIndex) === index,
+ );
+ return match < 0 || beforeRoutes[match] !== route;
+ });
+ if (rerouted) {
+ changes.push({
+ key: "__branch_routes",
+ label: "Branch routes",
+ before: describeRoutes(prior, beforeRoutes),
+ after: describeRoutes(block, afterRoutes),
+ });
+ }
+ }
+ const review: BlockReview = {
+ status: changes.length > 0 ? "changed" : "unchanged",
+ changes,
+ containsChanges: false,
+ };
+ state.merged.set(block.label, review);
+ state.before.set(prior.label, review);
+ state.after.set(block.label, review);
+ if (isNestedLoopWorkflowBlock(block) && isNestedLoopWorkflowBlock(prior)) {
+ block.loop_blocks = mergeLevel(
+ prior.loop_blocks,
+ block.loop_blocks,
+ { before: null, after: null },
+ state,
+ );
+ }
+ return block;
+ });
+
+ // Each removed block goes back on the edge that led into it: it takes that
+ // edge over from whatever the proposal points it at now.
+ const inbound = firstInboundSlots(beforeOrder);
+ const beforeRoot = beforeOrder[0]?.label;
+ const placed = new Map<string, WorkflowBlock>(
+ beforeLevel.flatMap((prior) => {
+ const kept = merged.find(
+ (block) => blockIdentity(block) === blockIdentity(prior),
+ );
+ return kept ? [[prior.label, kept] as const] : [];
+ }),
+ );
+ const removedCopies = new Set<WorkflowBlock>();
+ const placeAfter = (slot: Slot, removed: WorkflowBlock): boolean => {
+ const owner = placed.get(slot.owner);
+ const prior = beforeLevel.find((block) => block.label === slot.owner);
+ if (!owner || !prior) return false;
+ const branch =
+ slot.branch === null || removedCopies.has(owner)
+ ? slot.branch
+ : (matchingBranch(prior, owner, slot.branch) ??
+ keepDroppedBranch(prior, owner, slot.branch));
+ if (slot.branch !== null && branch === null) return false;
+ const target = slotTarget(owner, branch);
+ // Entering a conditional's merge point: its branches must rejoin at the
+ // removed block too, or they would run on past it.
+ if (branch === null && branchesOf(owner).length > 0) {
+ for (const label of branchMembers(merged, owner, target)) {
+ const member = merged.find((block) => block.label === label);
+ if (member && member.next_block_label === target) {
+ member.next_block_label = removed.label;
+ }
+ }
+ }
+ setSlot(owner, branch, removed.label);
+ removed.next_block_label = target;
+ merged.splice(merged.indexOf(owner) + 1, 0, removed);
+ return true;
+ };
+
+ for (const prior of beforeOrder) {
+ if (afterIdentities.has(blockIdentity(prior))) continue;
+ const removed = removedCopy(prior, state);
+ // Its old branch targets are stale; removed blocks from those branches
+ // attach again as they are placed.
+ branchesOf(removed).forEach((branch) => {
+ branch.next_block_label = null;
+ });
+ removedCopies.add(removed);
+ placed.set(prior.label, removed);
+ const slot = inbound.get(prior.label);
+ if (slot && placeAfter(slot, removed)) continue;
+ if (prior.label === beforeRoot && prior.label !== finallyLabels.before) {
+ const root = runOrder(merged, finallyLabel)[0];
+ removed.next_block_label =
+ root && root.label !== finallyLabel ? root.label : null;
+ merged.unshift(removed);
+ continue;
+ }
+ const tail = chainTail(merged, finallyLabel);
+ if (tail) tail.next_block_label = removed.label;
+ removed.next_block_label = null;
+ merged.push(removed);
+ }
+
+ return merged;
+}
+
+function markContainedChanges(
+ blocks: Array<WorkflowBlock>,
+ reviews: Map<string, BlockReview>,
+): boolean {
+ let any = false;
+ for (const block of blocks) {
+ const review = reviews.get(block.label);
+ let nested = false;
+ if (isNestedLoopWorkflowBlock(block)) {
+ nested = markContainedChanges(block.loop_blocks, reviews);
+ }
+ if (review) review.containsChanges = nested;
+ if (nested || (review && review.status !== "unchanged")) any = true;
+ }
+ return any;
+}
+
+function changeOrderOf(
+ blocks: Array<WorkflowBlock>,
+ reviews: Map<string, BlockReview>,
+ finallyLabel: string | null,
+): Array<string> {
+ return runOrder(blocks, finallyLabel).flatMap((block) => [
+ ...(reviews.get(block.label)?.status !== "unchanged" ? [block.label] : []),
+ ...(isNestedLoopWorkflowBlock(block)
+ ? changeOrderOf(block.loop_blocks, reviews, null)
+ : []),
+ ]);
+}
+
+function reviewableInputs(version: WorkflowVersion): Map<string, Parameter> {
+ return new Map(
+ (version.workflow_definition?.parameters ?? [])
+ .filter((parameter) => parameter.parameter_type !== "output")
+ .map((parameter) => [parameter.key, parameter]),
+ );
+}
+
+function inputDetail(parameter: Parameter): string | null {
+ if (parameter.parameter_type === "workflow") {
+ return parameter.workflow_parameter_type;
+ }
+ return humanizeKey(parameter.parameter_type);
+}
+
+const INPUT_IGNORED_KEYS: ReadonlySet<string> = new Set(["key"]);
+
+function diffInputs(
+ before: WorkflowVersion,
+ after: WorkflowVersion,
+): Array<InputReview> {
+ const beforeInputs = reviewableInputs(before);
+ const afterInputs = reviewableInputs(after);
+ const reviews: Array<InputReview> = [];
+ afterInputs.forEach((parameter, key) => {
+ const prior = beforeInputs.get(key);
+ if (!prior) {
+ reviews.push({
+ key,
+ status: "new",
+ detail: inputDetail(parameter),
+ changes: [],
+ });
+ return;
+ }
+ const changes = diffRecordFields(
+ prior as unknown as Record<string, unknown>,
+ parameter as unknown as Record<string, unknown>,
+ INPUT_IGNORED_KEYS,
+ ).map((change) => toReviewChange(change, undefined, SECRET_KEY.test(key)));
+ if (changes.length > 0) {
+ reviews.push({
+ key,
+ status: "changed",
+ detail: inputDetail(parameter),
+ changes,
+ });
+ }
+ });
+ beforeInputs.forEach((parameter, key) => {
+ if (!afterInputs.has(key)) {
+ reviews.push({
+ key,
+ status: "removed",
+ detail: inputDetail(parameter),
+ changes: [],
+ });
+ }
+ });
+ return reviews;
+}
+
+// Labels match the workflow settings editor, so a change reads the way the
+// setting is named where the user would edit it.
+const SETTING_LABELS: Record<keyof WorkflowSettings, string> = {
+ model: "Model",
+ workflowSystemPrompt: "Agent system prompt",
+ webhookCallbackUrl: "Webhook callback URL",
+ proxyLocation: "Proxy location",
+ browserType: "Browser type",
+ runWith: "Run with",
+ aiFallback: "AI fallback (cached scripts)",
+ scriptCacheKey: "Code key",
+ codeVersion: "Code version",
+ maskSecrets: "Mask secrets",
+ runSequentially: "Prevent overlapping runs",
+ sequentialKey: "Sequential key",
+ reuseBrowserSession: "Reuse browser session",
+ persistBrowserSession: "Save & reuse browser profile",
+ pinSavedSessionIp: "Keep same IP across runs",
+ browserProfileKey: "Browser profile key",
+ browserProfileId: "Starting browser profile",
+ extraHttpHeaders: "Extra HTTP headers",
+ cdpConnectHeaders: "CDP connect headers",
+ maxScreenshotScrolls: "Max screenshot scrolls",
+ maxElapsedTimeMinutes: "Max run time (minutes)",
+ finallyBlockLabel: "Execute on any outcome",
+ totpVerificationUrl: "2FA verification URL",
+ totpIdentifier: "2FA identifier",
+ adaptiveCaching: "Adaptive caching",
+ generateScriptOnTerminal: "Generate script on terminal",
+ errorCodeMapping: "Error code mapping",
+ retryPolicy: "Retry policy",
+};
+
+const JSON_STRING_SETTINGS: ReadonlySet<keyof WorkflowSettings> = new Set([
+ "extraHttpHeaders",
+ "cdpConnectHeaders",
+]);
+
+function comparableSettings(version: WorkflowVersion): Record<string, unknown> {
+ const settings: Record<string, unknown> = {
+ ...apiWorkflowToSettings(version),
+ };
+ for (const key of JSON_STRING_SETTINGS) {
+ const raw = settings[key];
+ if (typeof raw === "string") {
+ try {
+ settings[key] = JSON.parse(raw);
+ } catch {
+ // Unparseable headers compare as their raw text.
+ }
+ }
+ }
+ // The code version only applies when running with code; the pre-submit
+ // snapshot clears it otherwise while the server keeps its default.
+ if (settings.runWith !== "code") settings.codeVersion = null;
+ return settings;
+}
+
+const METADATA_LABELS = { title: "Title", description: "Description" };
+
+function diffSettings(
+ before: WorkflowVersion,
+ after: WorkflowVersion,
+): Array<ReviewFieldChange> {
+ // Accept applies a new title or description too, so it reviews with the
+ // settings rather than leaving the header to say "No changes".
+ const metadata = (["title", "description"] as const)
+ .filter((key) => !areValuesEquivalent(before[key], after[key]))
+ .map((key) =>
+ toReviewChange(
+ { key, before: before[key], after: after[key] },
+ METADATA_LABELS[key],
+ ),
+ );
+ const beforeSettings = comparableSettings(before);
+ const afterSettings = comparableSettings(after);
+ return [
+ ...metadata,
+ ...(Object.keys(SETTING_LABELS) as Array<keyof WorkflowSettings>)
+ .filter(
+ (key) => !areValuesEquivalent(beforeSettings[key], afterSettings[key]),
+ )
+ .map((key) =>
+ toReviewChange(
+ { key, before: beforeSettings[key], after: afterSettings[key] },
+ SETTING_LABELS[key],
+ // Headers that failed to parse stay raw text, out of reach of the
+ // per-header redaction.
+ JSON_STRING_SETTINGS.has(key) &&
+ (typeof beforeSettings[key] === "string" ||
+ typeof afterSettings[key] === "string"),
+ ),
+ ),
+ ];
+}
+
+function diffWorkflowVersions(
+ before: WorkflowVersion,
+ after: WorkflowVersion,
+): WorkflowReviewDiff {
+ const finallyLabel = after.workflow_definition?.finally_block_label ?? null;
+ const beforeFinallyLabel =
+ before.workflow_definition?.finally_block_label ?? null;
+ const beforeBlocks = applySequentialDefaulting(
+ clone(before.workflow_definition?.blocks ?? []),
+ beforeFinallyLabel,
+ );
+ const afterBlocks = applySequentialDefaulting(
+ clone(after.workflow_definition?.blocks ?? []),
+ finallyLabel,
+ );
+ const usedLabels = new Set<string>();
+ collectLabels(afterBlocks, usedLabels);
+ const state: DiffState = {
+ merged: new Map(),
+ before: new Map(),
+ after: new Map(),
+ usedLabels,
+ };
+ const mergedBlocks = mergeLevel(
+ beforeBlocks,
+ afterBlocks,
+ { before: beforeFinallyLabel, after: finallyLabel },
+ state,
+ );
+ markContainedChanges(mergedBlocks, state.merged);
+
+ const counts: Record<ReviewStatus, number> = {
+ unchanged: 0,
+ new: 0,
+ changed: 0,
+ removed: 0,
+ };
+ state.merged.forEach((review) => {
+ counts[review.status] += 1;
+ });
+
+ return {
+ mergedBlocks,
+ merged: state.merged,
+ before: state.before,
+ after: state.after,
+ changeOrder: changeOrderOf(mergedBlocks, state.merged, finallyLabel),
+ inputs: diffInputs(before, after),
+ settings: diffSettings(before, after),
+ counts,
+ isNewWorkflow: beforeBlocks.length === 0 && afterBlocks.length > 0,
+ };
+}
+
+export type UnchangedFold = {
+ key: string;
+ count: number;
+};
+
+const FOLD_LABEL_PREFIX = "__review_fold__";
+
+function isQuiet(
+ block: WorkflowBlock,
+ reviews: Map<string, BlockReview>,
+ finallyLabel: string | null,
+): boolean {
+ const review = reviews.get(block.label);
+ return (
+ block.label !== finallyLabel &&
+ review?.status === "unchanged" &&
+ !review.containsChanges
+ );
+}
+
+// The fold marker rides the canvas as a wait block, the simplest block the
+// renderer lays out; the review wrapper swaps its card for the marker.
+function foldPlaceholder(
+ key: string,
+ next: string | null | undefined,
+): WorkflowBlock {
+ return {
+ block_type: "wait",
+ label: `${FOLD_LABEL_PREFIX}${key}`,
+ wait_sec: 0,
+ continue_on_failure: false,
+ model: null,
+ next_block_label: next ?? null,
+ } as unknown as WorkflowBlock;
+}
+
+function foldLevel(
+ level: Array<WorkflowBlock>,
+ reviews: Map<string, BlockReview>,
+ expanded: ReadonlySet<string>,
+ finallyLabel: string | null,
+ folds: Map<string, UnchangedFold>,
+): Array<WorkflowBlock> {
+ const withChildren = level.map((block) =>
+ isNestedLoopWorkflowBlock(block)
+ ? ({
+ ...block,
+ loop_blocks: foldLevel(
+ block.loop_blocks,
+ reviews,
+ expanded,
+ null,
+ folds,
+ ),
+ } as WorkflowBlock)
+ : { ...block },
+ );
+ // Branch targets reference blocks by label across the level, so a fold here
+ // could orphan a branch.
+ if (withChildren.some((block) => block.block_type === "conditional")) {
+ return withChildren;
+ }
+
+ const runs: Array<Array<WorkflowBlock>> = [];
+ let run: Array<WorkflowBlock> = [];
+ for (const block of runOrder(withChildren, finallyLabel)) {
+ const extendsRun =
+ run.length > 0 && run[run.length - 1]!.next_block_label === block.label;
+ if (
+ isQuiet(block, reviews, finallyLabel) &&
+ (run.length === 0 || extendsRun)
+ ) {
+ run.push(block);
+ continue;
+ }
+ runs.push(run);
+ run = isQuiet(block, reviews, finallyLabel) ? [block] : [];
+ }
+ runs.push(run);
+
+ const placeholders = new Map<string, WorkflowBlock>();
+ const foldedLabels = new Set<string>();
+ for (const candidate of runs) {
+ const key = candidate[0]?.label;
+ // Any run folds, even a single block: one block card can outgrow the pane.
+ if (!key || expanded.has(key)) {
+ continue;
+ }
+ const last = candidate[candidate.length - 1]!;
+ const placeholder = foldPlaceholder(key, last.next_block_label);
+ placeholders.set(key, placeholder);
+ candidate.forEach((block) => foldedLabels.add(block.label));
+ folds.set(placeholder.label, { key, count: candidate.length });
+ }
+
+ const result: Array<WorkflowBlock> = [];
+ for (const block of withChildren) {
+ const placeholder = placeholders.get(block.label);
+ if (placeholder) result.push(placeholder);
+ if (!foldedLabels.has(block.label)) result.push(block);
+ }
+ placeholders.forEach((placeholder, key) =>
+ repointReferences(result, key, placeholder.label),
+ );
+ return result;
+}
+
+function foldUnchangedRuns(
+ blocks: Array<WorkflowBlock>,
+ reviews: Map<string, BlockReview>,
+ expanded: ReadonlySet<string>,
+ finallyLabel: string | null,
+): { blocks: Array<WorkflowBlock>; folds: Map<string, UnchangedFold> } {
+ const folds = new Map<string, UnchangedFold>();
+ return {
+ blocks: foldLevel(blocks, reviews, expanded, finallyLabel, folds),
+ folds,
+ };
+}
+
+export type TextSegment = { text: string; changed: boolean };
+
+// Past this many token pairs the LCS table gets expensive to build on every
+// render, and a rewrite that large reads better as a plain before/after anyway.
+const MAX_WORD_DIFF_CELLS = 40_000;
+
+function pushSegment(
+ segments: Array<TextSegment>,
+ text: string,
+ changed: boolean,
+) {
+ const last = segments[segments.length - 1];
+ if (last && last.changed === changed) {
+ last.text += text;
+ } else {
+ segments.push({ text, changed });
+ }
+}
+
+function diffWords(
+ before: string,
+ after: string,
+): { before: Array<TextSegment>; after: Array<TextSegment> } {
+ const tokenize = (text: string) =>
+ text.match(/\s+|[\p{L}\p{N}_]+|[^\s\p{L}\p{N}_]/gu) ?? [];
+ const a = tokenize(before);
+ const b = tokenize(after);
+ // Only the span between the shared start and end needs the LCS table, which
+ // keeps a one-clause edit to a long prompt well under the cap.
+ let prefix = 0;
+ while (prefix < a.length && prefix < b.length && a[prefix] === b[prefix]) {
+ prefix++;
+ }
+ let suffix = 0;
+ while (
+ suffix < a.length - prefix &&
+ suffix < b.length - prefix &&
+ a[a.length - 1 - suffix] === b[b.length - 1 - suffix]
+ ) {
+ suffix++;
+ }
+ const midA = a.slice(prefix, a.length - suffix);
+ const midB = b.slice(prefix, b.length - suffix);
+ const beforeSegments: Array<TextSegment> = [];
+ const afterSegments: Array<TextSegment> = [];
+ const shared = (tokens: Array<string>) => {
+ for (const token of tokens) {
+ pushSegment(beforeSegments, token, false);
+ pushSegment(afterSegments, token, false);
+ }
+ };
+ shared(a.slice(0, prefix));
+ if (midA.length * midB.length > MAX_WORD_DIFF_CELLS) {
+ if (midA.length > 0) pushSegment(beforeSegments, midA.join(""), true);
+ if (midB.length > 0) pushSegment(afterSegments, midB.join(""), true);
+ } else {
+ const lcs: Array<Array<number>> = Array.from(
+ { length: midA.length + 1 },
+ () => new Array<number>(midB.length + 1).fill(0),
+ );
+ for (let i = midA.length - 1; i >= 0; i--) {
+ for (let j = midB.length - 1; j >= 0; j--) {
+ lcs[i]![j] =
+ midA[i] === midB[j]
+ ? lcs[i + 1]![j + 1]! + 1
+ : Math.max(lcs[i + 1]![j]!, lcs[i]![j + 1]!);
+ }
+ }
+ let i = 0;
+ let j = 0;
+ while (i < midA.length && j < midB.length) {
+ if (midA[i] === midB[j]) {
+ pushSegment(beforeSegments, midA[i]!, false);
+ pushSegment(afterSegments, midB[j]!, false);
+ i++;
+ j++;
+ } else if (lcs[i + 1]![j]! >= lcs[i]![j + 1]!) {
+ pushSegment(beforeSegments, midA[i]!, true);
+ i++;
+ } else {
+ pushSegment(afterSegments, midB[j]!, true);
+ j++;
+ }
+ }
+ for (; i < midA.length; i++) pushSegment(beforeSegments, midA[i]!, true);
+ for (; j < midB.length; j++) pushSegment(afterSegments, midB[j]!, true);
+ }
+ shared(a.slice(a.length - suffix));
+ return { before: beforeSegments, after: afterSegments };
+}
+
+export {
+ blockIdentity,
+ diffWords,
+ diffWorkflowVersions,
+ foldUnchangedRuns,
+ humanizeKey,
+};
diff --git a/skyvern-frontend/src/routes/workflows/editor/review/ReviewParts.tsx b/skyvern-frontend/src/routes/workflows/editor/review/ReviewParts.tsx
new file mode 100644
index 000000000..4d2d059f4
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/review/ReviewParts.tsx
@@ -0,0 +1,266 @@
+import { useState } from "react";
+import { ArrowRightIcon } from "@radix-ui/react-icons";
+import { Handle, Position } from "@xyflow/react";
+
+import { cn } from "@/util/utils";
+
+import {
+ diffWords,
+ type ReviewFieldChange,
+ type ReviewStatus,
+ type TextSegment,
+} from "../panels/workflowReviewDiff";
+import {
+ REVIEW_STATUS_META,
+ type BlockReviewAnnotation,
+ type StartReviewAnnotation,
+} from "./reviewAnnotation";
+
+function ReviewStatusChip({
+ status,
+ count,
+ className,
+}: {
+ status: ReviewStatus;
+ count?: number;
+ className?: string;
+}) {
+ const meta = REVIEW_STATUS_META[status];
+ return (
+ <span
+ className={cn(
+ "inline-flex shrink-0 items-center gap-1 whitespace-nowrap rounded-full px-2 py-0.5 text-xs font-medium",
+ meta.chip,
+ className,
+ )}
+ >
+ <span aria-hidden className={cn("font-semibold", meta.glyphClass)}>
+ {meta.glyph}
+ </span>
+ {count === undefined ? meta.word : `${count} ${meta.word.toLowerCase()}`}
+ </span>
+ );
+}
+
+const SHORT_VALUE_LENGTH = 48;
+
+function isShortValue(value: string | null): boolean {
+ return (
+ value === null ||
+ (value.length <= SHORT_VALUE_LENGTH && !value.includes("\n"))
+ );
+}
+
+function NotSet() {
+ return <span className="italic text-muted-foreground">Not set</span>;
+}
+
+function DiffLine({
+ tone,
+ segments,
+}: {
+ tone: "before" | "after";
+ segments: Array<TextSegment>;
+}) {
+ const before = tone === "before";
+ return (
+ <div
+ className={cn(
+ "nowheel flex max-h-40 gap-2 overflow-auto rounded-md px-2 py-1 text-sm",
+ before ? "bg-destructive/10" : "bg-success/10",
+ )}
+ >
+ <span
+ aria-hidden
+ className={cn(
+ "select-none font-semibold",
+ before ? "text-destructive" : "text-success",
+ )}
+ >
+ {before ? "−" : "+"}
+ </span>
+ <span className="sr-only">{before ? "Before:" : "After:"}</span>
+ <span className="min-w-0 whitespace-pre-wrap break-words">
+ {segments.map((segment, index) => {
+ if (!segment.changed) return <span key={index}>{segment.text}</span>;
+ const Tag = before ? "del" : "ins";
+ return (
+ <Tag
+ key={index}
+ className={cn(
+ "rounded-sm no-underline",
+ before
+ ? "bg-destructive/25 line-through decoration-destructive"
+ : "bg-success/25 font-medium",
+ )}
+ >
+ {segment.text}
+ </Tag>
+ );
+ })}
+ </span>
+ </div>
+ );
+}
+
+function ReviewValueChange({ change }: { change: ReviewFieldChange }) {
+ const { before, after } = change;
+ if (isShortValue(before) && isShortValue(after)) {
+ return (
+ <div className="flex flex-wrap items-center gap-1.5 text-sm">
+ <span className="sr-only">Before:</span>
+ {before === null ? (
+ <NotSet />
+ ) : (
+ <span className="break-all rounded-sm bg-destructive/15 px-1 line-through decoration-destructive decoration-2">
+ {before}
+ </span>
+ )}
+ <ArrowRightIcon aria-hidden className="size-3.5 shrink-0" />
+ <span className="sr-only">After:</span>
+ {after === null ? (
+ <NotSet />
+ ) : (
+ <span className="break-all rounded-sm bg-success/15 px-1 font-medium">
+ {after}
+ </span>
+ )}
+ </div>
+ );
+ }
+ const words = diffWords(before ?? "", after ?? "");
+ return (
+ <div className="space-y-1">
+ {before === null ? null : (
+ <DiffLine tone="before" segments={words.before} />
+ )}
+ {after === null ? (
+ <div className="px-2 text-sm">
+ <NotSet />
+ </div>
+ ) : (
+ <DiffLine tone="after" segments={words.after} />
+ )}
+ </div>
+ );
+}
+
+function ReviewFieldRow({ change }: { change: ReviewFieldChange }) {
+ return (
+ <div className="space-y-1">
+ <div className="text-xs text-muted-foreground">{change.label}</div>
+ <ReviewValueChange change={change} />
+ </div>
+ );
+}
+
+function ReviewBlockChanges({ review }: { review: BlockReviewAnnotation }) {
+ const [expanded, setExpanded] = useState(false);
+ if (review.status !== "changed" || review.changes.length === 0) {
+ return null;
+ }
+ const visible = expanded ? review.changes : review.changes.slice(0, 1);
+ const hiddenCount = review.changes.length - visible.length;
+ return (
+ <div className="nodrag nopan cursor-default space-y-3 border-t border-border pt-3 text-left">
+ {visible.map((change) => (
+ <ReviewFieldRow key={change.key} change={change} />
+ ))}
+ {review.changes.length > 1 ? (
+ <button
+ type="button"
+ onClick={() => setExpanded((value) => !value)}
+ className="text-xs font-medium text-muted-foreground underline-offset-2 hover:text-foreground hover:underline focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
+ >
+ {expanded
+ ? "Show fewer changes"
+ : `+${hiddenCount} more ${hiddenCount === 1 ? "change" : "changes"}`}
+ </button>
+ ) : null}
+ </div>
+ );
+}
+
+function ReviewStartChanges({ review }: { review: StartReviewAnnotation }) {
+ if (review.inputs.length === 0 && review.settings.length === 0) {
+ return null;
+ }
+ return (
+ <div className="nodrag nopan mt-3 cursor-default space-y-3 text-left">
+ {review.inputs.map((input) => (
+ <div key={`input:${input.key}`} className="space-y-1">
+ <div className="flex flex-wrap items-center gap-2">
+ <span className="w-16 shrink-0 text-xs text-muted-foreground">
+ Input
+ </span>
+ {review.showStatus ? (
+ <ReviewStatusChip status={input.status} />
+ ) : null}
+ <code
+ className={cn("font-mono text-sm", {
+ "text-muted-foreground line-through":
+ input.status === "removed",
+ })}
+ >
+ {input.key}
+ </code>
+ {input.detail ? (
+ <span className="text-xs text-muted-foreground">
+ {input.detail}
+ </span>
+ ) : null}
+ </div>
+ {input.changes.length > 0 ? (
+ <div className="space-y-2 pl-[4.5rem]">
+ {input.changes.map((change) => (
+ <ReviewFieldRow key={change.key} change={change} />
+ ))}
+ </div>
+ ) : null}
+ </div>
+ ))}
+ {review.settings.map((setting) => (
+ <div key={`setting:${setting.key}`} className="space-y-1">
+ <div className="flex flex-wrap items-center gap-2">
+ <span className="text-xs text-muted-foreground">
+ {setting.label}
+ </span>
+ {review.showStatus ? <ReviewStatusChip status="changed" /> : null}
+ </div>
+ <ReviewValueChange change={setting} />
+ </div>
+ ))}
+ </div>
+ );
+}
+
+function ReviewFoldMarker({
+ count,
+ onShow,
+}: {
+ count: number;
+ onShow: () => void;
+}) {
+ return (
+ <div className="w-[30rem]">
+ <Handle type="target" position={Position.Top} className="opacity-0" />
+ <Handle type="source" position={Position.Bottom} className="opacity-0" />
+ <button
+ type="button"
+ onClick={onShow}
+ className="nodrag nopan flex w-full items-center justify-center gap-2 rounded-lg border border-dashed border-border bg-slate-elevation2 px-4 py-3 text-sm text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring dark:border-slate-600"
+ >
+ {count} unchanged {count === 1 ? "block" : "blocks"}
+ <span aria-hidden>·</span>
+ <span className="font-medium text-foreground">Show</span>
+ </button>
+ </div>
+ );
+}
+
+export {
+ ReviewBlockChanges,
+ ReviewFoldMarker,
+ ReviewStartChanges,
+ ReviewStatusChip,
+};
diff --git a/skyvern-frontend/src/routes/workflows/editor/review/reviewAnnotation.ts b/skyvern-frontend/src/routes/workflows/editor/review/reviewAnnotation.ts
new file mode 100644
index 000000000..862f4061c
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/review/reviewAnnotation.ts
@@ -0,0 +1,94 @@
+import { createContext, useContext } from "react";
+
+import type {
+ InputReview,
+ ReviewFieldChange,
+ ReviewStatus,
+} from "../panels/workflowReviewDiff";
+
+export type BlockReviewAnnotation = {
+ kind: "block";
+ status: ReviewStatus;
+ changes: Array<ReviewFieldChange>;
+ // False for a brand-new workflow: one note says everything is new instead.
+ showStatus: boolean;
+};
+
+export type StartReviewAnnotation = {
+ kind: "start";
+ inputs: Array<InputReview>;
+ settings: Array<ReviewFieldChange>;
+ showStatus: boolean;
+};
+
+export type FoldReviewAnnotation = {
+ kind: "fold";
+ count: number;
+ onShow: () => void;
+};
+
+export type NodeReviewAnnotation =
+ | BlockReviewAnnotation
+ | StartReviewAnnotation
+ | FoldReviewAnnotation;
+
+export type CardReviewAnnotation =
+ | BlockReviewAnnotation
+ | StartReviewAnnotation;
+
+export const ReviewAnnotationContext =
+ createContext<CardReviewAnnotation | null>(null);
+
+export function useReviewAnnotation(): CardReviewAnnotation | null {
+ return useContext(ReviewAnnotationContext);
+}
+
+export function reviewStatusOf(review: CardReviewAnnotation): ReviewStatus {
+ if (review.kind === "block") return review.status;
+ return review.inputs.length > 0 || review.settings.length > 0
+ ? "changed"
+ : "unchanged";
+}
+
+// One vocabulary for every review surface: a glyph and a word with each color,
+// so status never rests on color alone. Outline, tint, and glyph share one
+// semantic token per status.
+export const REVIEW_STATUS_META: Record<
+ ReviewStatus,
+ {
+ glyph: string;
+ word: string;
+ chip: string;
+ glyphClass: string;
+ outline: string;
+ }
+> = {
+ new: {
+ glyph: "+",
+ word: "New",
+ chip: "bg-success/20 text-foreground",
+ glyphClass: "text-success",
+ outline: "outline outline-2 outline-offset-4 outline-success",
+ },
+ changed: {
+ glyph: "~",
+ word: "Changed",
+ chip: "bg-warning/20 text-foreground",
+ glyphClass: "text-warning",
+ outline: "outline outline-2 outline-offset-4 outline-warning",
+ },
+ removed: {
+ glyph: "−",
+ word: "Removed",
+ chip: "bg-destructive/20 text-foreground",
+ glyphClass: "text-destructive",
+ outline: "outline-dashed outline-2 outline-offset-4 outline-destructive",
+ },
+ unchanged: {
+ glyph: "=",
+ word: "Unchanged",
+ chip: "bg-muted text-muted-foreground",
+ glyphClass: "",
+ outline: "",
+ },
+};
diff --git a/skyvern-frontend/src/routes/workflows/editor/review/withReviewAnnotation.tsx b/skyvern-frontend/src/routes/workflows/editor/review/withReviewAnnotation.tsx
new file mode 100644
index 000000000..704bc2e08
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/review/withReviewAnnotation.tsx
@@ -0,0 +1,47 @@
+import type { ComponentType } from "react";
+import type { NodeProps } from "@xyflow/react";
+
+import { cn } from "@/util/utils";
+
+import {
+ REVIEW_STATUS_META,
+ ReviewAnnotationContext,
+ reviewStatusOf,
+ type NodeReviewAnnotation,
+} from "./reviewAnnotation";
+import { ReviewFoldMarker } from "./ReviewParts";
+
+// Only review canvases set `data.review`; every other canvas renders the node
+// untouched. The outline wraps the whole node (a loop's children included) and
+// the header and change list read the annotation from context.
+export function withReviewAnnotation<P extends NodeProps>(
+ Component: ComponentType<P>,
+): ComponentType<P> {
+ function Reviewed(props: P) {
+ const review = (props.data as { review?: NodeReviewAnnotation }).review;
+ if (!review) {
+ return <Component {...props} />;
+ }
+ if (review.kind === "fold") {
+ return <ReviewFoldMarker count={review.count} onShow={review.onShow} />;
+ }
+ const status = reviewStatusOf(review);
+ return (
+ <ReviewAnnotationContext.Provider value={review}>
+ <div
+ data-review-status={status}
+ className={cn(
+ "rounded-lg",
+ review.showStatus && REVIEW_STATUS_META[status].outline,
+ )}
+ >
+ <Component {...props} />
+ </div>
+ </ReviewAnnotationContext.Provider>
+ );
+ }
+ Reviewed.displayName = `withReviewAnnotation(${
+ Component.displayName ?? Component.name ?? "Component"
+ })`;
+ return Reviewed;
+}
diff --git a/skyvern-frontend/src/routes/workflows/util/compareBlocks.test.ts b/skyvern-frontend/src/routes/workflows/util/compareBlocks.test.ts
index 14020fbdd..4a467bd33 100644
--- a/skyvern-frontend/src/routes/workflows/util/compareBlocks.test.ts
+++ b/skyvern-frontend/src/routes/workflows/util/compareBlocks.test.ts
@@ -1,7 +1,10 @@
import { describe, test, expect } from "vitest";
-import { areBlocksIdentical } from "./compareBlocks";
+import { diffBlockFields } from "./compareBlocks";
import { WorkflowBlock } from "../types/workflowTypes";
+const areBlocksIdentical = (a: WorkflowBlock, b: WorkflowBlock) =>
+ diffBlockFields(a, b).length === 0;
+
function makeOutputParameter(id: string) {
return {
parameter_type: "output" as const,
@@ -52,7 +55,7 @@ function makeTaskBlock(overrides: Record<string, unknown> = {}): WorkflowBlock {
} as unknown as WorkflowBlock;
}
-describe("areBlocksIdentical", () => {
+describe("diffBlockFields", () => {
test("identical blocks compare equal", () => {
expect(areBlocksIdentical(makeTaskBlock(), makeTaskBlock())).toBe(true);
});
diff --git a/skyvern-frontend/src/routes/workflows/util/compareBlocks.ts b/skyvern-frontend/src/routes/workflows/util/compareBlocks.ts
index 14bfe33d5..d2e321d95 100644
--- a/skyvern-frontend/src/routes/workflows/util/compareBlocks.ts
+++ b/skyvern-frontend/src/routes/workflows/util/compareBlocks.ts
@@ -17,18 +17,96 @@ const COMPARISON_OMIT_KEYS = new Set([
"id",
]);
+// Not block content: the label is the block's identity, `next_block_label`
+// follows from where the block sits, and loop children are compared as blocks
+// of their own.
+const STRUCTURAL_BLOCK_KEYS: ReadonlySet<string> = new Set([
+ "label",
+ "next_block_label",
+ "loop_blocks",
+]);
+
function shouldOmitForComparison(key: string): boolean {
return COMPARISON_OMIT_KEYS.has(key) || key.endsWith("_parameter_id");
}
-function areBlocksIdentical(
- block1: WorkflowBlock,
- block2: WorkflowBlock,
-): boolean {
- return (
- stableStringify(block1, { omit: shouldOmitForComparison }) ===
- stableStringify(block2, { omit: shouldOmitForComparison })
+type RawFieldChange = {
+ key: string;
+ before: unknown;
+ after: unknown;
+};
+
+function isEmptyValue(value: unknown): boolean {
+ if (value === null || value === undefined || value === "") return true;
+ if (Array.isArray(value)) return value.length === 0;
+ if (typeof value === "object") return Object.keys(value).length === 0;
+ return false;
+}
+
+function comparableValue(value: unknown): string | undefined {
+ // Absent, null, and empty all read as "not set" to a reviewer, and the two
+ // sides reach the panel through different serializers that disagree on them.
+ if (isEmptyValue(value)) return undefined;
+ return stableStringify(value, { omit: shouldOmitForComparison });
+}
+
+function areValuesEquivalent(before: unknown, after: unknown): boolean {
+ return comparableValue(before) === comparableValue(after);
+}
+
+function diffRecordFields(
+ before: Record<string, unknown>,
+ after: Record<string, unknown>,
+ ignoredKeys: ReadonlySet<string> = new Set(),
+): Array<RawFieldChange> {
+ const keys = [...new Set([...Object.keys(after), ...Object.keys(before)])];
+ return keys
+ .filter((key) => !ignoredKeys.has(key) && !shouldOmitForComparison(key))
+ .filter((key) => !areValuesEquivalent(before[key], after[key]))
+ .map((key) => ({ key, before: before[key], after: after[key] }));
+}
+
+// A block's parameters embed whole inputs, which are reviewed on their own, so
+// a block only differs in which inputs it reads. Branch targets are structure,
+// like `next_block_label`.
+function comparableBlock(block: WorkflowBlock): Record<string, unknown> {
+ const record = { ...block } as Record<string, unknown>;
+ if (Array.isArray(record.parameters)) {
+ record.parameters = record.parameters.map((parameter: unknown) =>
+ parameter !== null &&
+ typeof parameter === "object" &&
+ typeof (parameter as { key?: unknown }).key === "string"
+ ? (parameter as { key: string }).key
+ : parameter,
+ );
+ }
+ if (Array.isArray(record.branch_conditions)) {
+ record.branch_conditions = record.branch_conditions.map(
+ (branch: Record<string, unknown>) => ({
+ ...branch,
+ next_block_label: undefined,
+ }),
+ );
+ }
+ return record;
+}
+
+function diffBlockFields(
+ before: WorkflowBlock,
+ after: WorkflowBlock,
+): Array<RawFieldChange> {
+ return diffRecordFields(
+ comparableBlock(before),
+ comparableBlock(after),
+ STRUCTURAL_BLOCK_KEYS,
);
}
-export { areBlocksIdentical, shouldOmitForComparison };
+export {
+ areValuesEquivalent,
+ diffBlockFields,
+ diffRecordFields,
+ isEmptyValue,
+ shouldOmitForComparison,
+ type RawFieldChange,
+};
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index 9215e8dd5..eb06ed778 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -2158,6 +2158,8 @@ def _make_agent_result(
pause_payload = {"outcome": pause_outcome}
if pause_outcome == "connected" and ctx.credential_pause_connected_credential_id:
pause_payload["credentialId"] = ctx.credential_pause_connected_credential_id
+ if ctx.credential_pause_anchor_tool_call_id:
+ pause_payload["anchorToolCallId"] = ctx.credential_pause_anchor_tool_call_id
payload_updates["credentialPause"] = pause_payload
if ctx is not None and "googleConnectionNotices" not in narrative_payload and ctx.google_connection_notices:
payload_updates["googleConnectionNotices"] = [
@@ -2357,6 +2359,8 @@ def _build_narrative_payload(
"startedAt": ctx.turn_started_at,
"endedAt": ctx.turn_ended_at,
}
+ if narrator_state is not None and narrator_state.work_plan is not None:
+ payload["workPlan"] = narrator_state.work_plan
budget_state = _budget_expiry_state_with_staged_draft(ctx)
if budget_state.source is not None:
payload["budgetExpiry"] = {
diff --git a/skyvern/forge/sdk/copilot/context.py b/skyvern/forge/sdk/copilot/context.py
index 603d9a5ab..5ad0dda17 100644
--- a/skyvern/forge/sdk/copilot/context.py
+++ b/skyvern/forge/sdk/copilot/context.py
@@ -220,6 +220,11 @@ class NarrativeBudgetExpiry(TypedDict):
# Mirror of the FE TurnNarrativeState; camelCase keys match the wire shape.
+class NarrativeWorkPlan(TypedDict):
+ toolCallId: str
+ items: list[str]
+
+
class TurnNarrativePayload(TypedDict):
turnId: str | None
turnIndex: int
@@ -231,8 +236,9 @@ class TurnNarrativePayload(TypedDict):
questionInteractions: NotRequired[list[dict[str, Any]]]
# {"reason": <credential_prompt_reason() token>}, set when this turn surfaces a credential need.
credentialPrompt: NotRequired[dict[str, str]]
- # {"outcome": "connected"|"skipped"|"timeout", "credentialId": ...}, set when a mid-build
- # credential pause (credential_pause.py) resolved during this turn.
+ # {"outcome": "connected"|"skipped"|"timeout", "credentialId": ..., "anchorToolCallId": ...}, set
+ # when a mid-build credential pause (credential_pause.py) resolved during this turn. The anchor is
+ # the tool call whose row was newest when the card was raised.
credentialPause: NotRequired[dict[str, str]]
# {"credentialId": ..., "name": ...}, set when a credential was bound this turn without an ask
# (deterministic auto-bind); the FE renders it as a receipt with a Change affordance.
@@ -249,6 +255,9 @@ class TurnNarrativePayload(TypedDict):
narrativeSummary: str | None
priorBlockCount: int | None
designActivity: list[NarrativeActivityEntry]
+ # The last plan a successful set_work_plan stored this turn. Kept off designActivity, whose cap
+ # can trim the call's row in a long turn.
+ workPlan: NotRequired[NarrativeWorkPlan]
startedAt: str | None
endedAt: str | None
review: NotRequired[NarrativeReviewProjection]
@@ -1215,6 +1224,7 @@ class CopilotContext(AgentContext):
copilot_credential_pause_seconds: float = 0.0
credential_pause_outcome: str | None = None
credential_pause_connected_credential_id: str | None = None
+ credential_pause_anchor_tool_call_id: str | None = None
# Set while a ``request_credential`` ask is open, so tool calls issued alongside it in the same
# model response wait for the user's answer instead of racing it.
credential_pause_settled: asyncio.Event | None = None
diff --git a/skyvern/forge/sdk/copilot/credential_pause.py b/skyvern/forge/sdk/copilot/credential_pause.py
index 03a97a6bf..1b42b4a2a 100644
--- a/skyvern/forge/sdk/copilot/credential_pause.py
+++ b/skyvern/forge/sdk/copilot/credential_pause.py
@@ -561,6 +561,7 @@ async def _run_credential_pause(
update_credential_id: str | None = None,
admit_connected: Callable[[Credential], Awaitable[bool]] | None = None,
allow_second_ask: bool = False,
+ anchor_tool_call_id: str | None = None,
) -> CredentialPauseResolution | None:
"""Send the credential card and wait for the user's decision.
@@ -574,8 +575,12 @@ async def _run_credential_pause(
return None
# Latch before async checks so a declined transport cannot trigger another pause. Only the pick ask
# spends credential_pause_used; request_credential_pause latches the update ask.
+ # A pause no tool call raised (the end-of-turn ask) renders after the newest row instead.
+ if anchor_tool_call_id is None and ctx.narrator_state is not None:
+ anchor_tool_call_id = ctx.narrator_state.last_tool_call_id
if not update_ask:
ctx.credential_pause_used = True
+ ctx.credential_pause_anchor_tool_call_id = anchor_tool_call_id
def settle(outcome: str) -> None:
# An update card asks to fix a credential already chosen, so the turn keeps its pick card's state.
@@ -623,6 +628,7 @@ async def _run_credential_pause(
credential_refs=credential_refs,
timeout_seconds=timeout_seconds,
expires_at=expires_at,
+ anchor_tool_call_id=anchor_tool_call_id,
timestamp=now,
)
await cache.set(
@@ -761,6 +767,7 @@ async def request_credential_pause(
update_reason: Literal["credential_missing_totp", "credential_rejected_by_site"] = "credential_missing_totp",
admit_connected: Callable[[Credential], Awaitable[bool]] | None = None,
allow_second_ask: bool = False,
+ anchor_tool_call_id: str | None = None,
) -> CredentialPauseResolution | None:
"""Raise the card from the model's own ``request_credential`` call and wait, inline, for the
answer, so tool calls the model issued alongside it can await ``credential_pause_settled``."""
@@ -780,6 +787,7 @@ async def request_credential_pause(
update_credential_id=update_credential_id,
admit_connected=admit_connected,
allow_second_ask=allow_second_ask,
+ anchor_tool_call_id=anchor_tool_call_id,
)
if not update_ask:
ctx.credential_pause_reaskable_by_run = resolution is None or resolution.action != "connected"
diff --git a/skyvern/forge/sdk/copilot/narration.py b/skyvern/forge/sdk/copilot/narration.py
index 8934083bc..f67ae0196 100644
--- a/skyvern/forge/sdk/copilot/narration.py
+++ b/skyvern/forge/sdk/copilot/narration.py
@@ -39,7 +39,7 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (
)
if TYPE_CHECKING:
- from skyvern.forge.sdk.copilot.context import NarrativeActivityEntry
+ from skyvern.forge.sdk.copilot.context import NarrativeActivityEntry, NarrativeWorkPlan
from skyvern.forge.sdk.core.event_source_stream import EventSourceStream
LOG = structlog.get_logger()
@@ -315,10 +315,16 @@ class NarratorState:
run_tool_call_buckets: dict[str, str | None] = field(default_factory=dict)
# Per-turn (NarratorState lives one turn); collapses repeated code-repair progress to one entry.
emitted_progress_texts: set[str] = field(default_factory=set)
+ # The tool call behind the newest recorded tool row, whichever bucket it landed in. Something that
+ # happens between rows (a credential pause) anchors here so it renders where it happened.
+ last_tool_call_id: str | None = None
+ work_plan: NarrativeWorkPlan | None = None
def record_activity(self, entry: NarrativeActivityEntry | None) -> None:
if entry is None:
return
+ if entry.get("kind") in ("tool_call", "tool_result"):
+ self.last_tool_call_id = entry["id"][3:]
label = self._activity_bucket_label(entry)
if label is None:
self.design_activity.append(entry)
diff --git a/skyvern/forge/sdk/copilot/streaming_adapter.py b/skyvern/forge/sdk/copilot/streaming_adapter.py
index aaecd72f6..41bd37aee 100644
--- a/skyvern/forge/sdk/copilot/streaming_adapter.py
+++ b/skyvern/forge/sdk/copilot/streaming_adapter.py
@@ -439,6 +439,7 @@ async def stream_to_sse(
break
tool_result_ts = datetime.now(timezone.utc)
code_diffs = _drain_code_write_diffs(ctx, tool_name, call_id)
+ work_plan = _tool_result_work_plan(tool_name, parsed)
narrator_state.record_activity(
build_tool_result_activity(
tool_name,
@@ -451,6 +452,8 @@ async def stream_to_sse(
code_diffs=code_diffs,
)
)
+ if work_plan is not None:
+ narrator_state.work_plan = {"toolCallId": call_id, "items": work_plan}
if not client_gone:
await stream.send(
@@ -463,6 +466,7 @@ async def stream_to_sse(
iteration=iteration,
tool_call_id=call_id,
code_diffs=code_diffs,
+ work_plan=work_plan,
detail=detail,
workflow_run_id=_tool_result_workflow_run_id(tool_name, parsed),
executed_source_reference=_tool_result_executed_source_reference(tool_name, parsed),
@@ -574,6 +578,16 @@ def _tool_result_workflow_run_id(tool_name: str, parsed: dict[str, Any]) -> str
return run_id if isinstance(run_id, str) else None
+def _tool_result_work_plan(tool_name: str, parsed: dict[str, Any]) -> list[str] | None:
+ # A refused write echoes the plan still in force, which is not a new plan for this row.
+ if tool_name != "set_work_plan" or parsed.get("ok") is not True:
+ return None
+ items = parsed.get("items")
+ if not isinstance(items, list) or not all(isinstance(item, str) for item in items):
+ return None
+ return list(items)
+
+
def _tool_result_executed_source_reference(tool_name: str, parsed: dict[str, Any]) -> str | None:
if tool_name != "run_browser_code":
return None
@@ -602,6 +616,7 @@ async def flush_goal_satisfied_tool_result(stream: EventSourceStream, ctx: Copil
display_label = pending.display_label or tool_activity_display_label(pending.tool_name)
flush_ts = datetime.now(timezone.utc)
code_diffs = _drain_code_write_diffs(ctx, pending.tool_name, pending.call_id)
+ work_plan = _tool_result_work_plan(pending.tool_name, parsed)
narrator_state = ctx.narrator_state
if narrator_state is not None:
narrator_state.record_activity(
@@ -616,6 +631,8 @@ async def flush_goal_satisfied_tool_result(stream: EventSourceStream, ctx: Copil
code_diffs=code_diffs,
)
)
+ if work_plan is not None:
+ narrator_state.work_plan = {"toolCallId": pending.call_id, "items": work_plan}
if await stream.is_disconnected():
return
await stream.send(
@@ -628,6 +645,7 @@ async def flush_goal_satisfied_tool_result(stream: EventSourceStream, ctx: Copil
iteration=pending.iteration,
tool_call_id=pending.call_id,
code_diffs=code_diffs,
+ work_plan=work_plan,
detail=summarize_tool_result_detail(
parsed, tool_name=pending.tool_name, blocker_signal=blocker_signals, success=success
),
diff --git a/skyvern/forge/sdk/copilot/tools/__init__.py b/skyvern/forge/sdk/copilot/tools/__init__.py
index a287cc079..13d9866e3 100644
--- a/skyvern/forge/sdk/copilot/tools/__init__.py
+++ b/skyvern/forge/sdk/copilot/tools/__init__.py
@@ -981,7 +981,14 @@ async def request_credential_tool(
if authority_error:
result = {"ok": False, "error": authority_error}
else:
- result = await _request_credential(login_page_url, reason, copilot_ctx, credential_id, rejected_by_site)
+ result = await _request_credential(
+ login_page_url,
+ reason,
+ copilot_ctx,
+ credential_id,
+ rejected_by_site,
+ anchor_tool_call_id=_originating_call_id(ctx),
+ )
finally:
# A card on screen right now owns the gate; this call must not open it for one it never
# raised. Every other exit has to release, including a repeat ask in a later response.
diff --git a/skyvern/forge/sdk/copilot/tools/credential_fill.py b/skyvern/forge/sdk/copilot/tools/credential_fill.py
index ae13f7246..a2fdb72c4 100644
--- a/skyvern/forge/sdk/copilot/tools/credential_fill.py
+++ b/skyvern/forge/sdk/copilot/tools/credential_fill.py
@@ -589,6 +589,7 @@ async def _request_credential(
copilot_ctx: CopilotContext,
credential_id: str | None = None,
rejected_by_site: bool = False,
+ anchor_tool_call_id: str | None = None,
) -> dict[str, Any]:
policy = copilot_ctx.request_policy
if not isinstance(policy, RequestPolicy) or (policy.raw_secret_detected and not policy.raw_secret_redacted_draft):
@@ -692,6 +693,7 @@ async def _request_credential(
update_reason="credential_rejected_by_site" if site_rejected else "credential_missing_totp",
admit_connected=admit_connected,
allow_second_ask=handback,
+ anchor_tool_call_id=anchor_tool_call_id,
)
except BaseException:
if recovery is not None:
diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.py
index ffbf50626..173f0ae70 100644
--- a/skyvern/forge/sdk/schemas/workflow_copilot.py
+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py
@@ -755,6 +755,10 @@ class WorkflowCopilotToolResultUpdate(BaseModel):
None,
description="Per changed code block: its label, the +N/-M line delta, and a size-capped scrubbed patch",
)
+ work_plan: list[str] | None = Field(
+ None,
+ description="The plan a successful set_work_plan stored, as stored. None for every other tool",
+ )
detail: str | None = Field(
None,
description=(
@@ -952,6 +956,9 @@ class WorkflowCopilotCredentialRequiredUpdate(BaseModel):
credential_refs: list[str] = Field(default_factory=list, description="Credential IDs or names referenced")
timeout_seconds: int = Field(..., description="How long the backend will wait before degrading to terminal")
expires_at: datetime = Field(..., description="Server time after which the pause degrades to terminal")
+ anchor_tool_call_id: str | None = Field(
+ None, description="Tool call whose activity row was newest when the pause was raised"
+ )
timestamp: datetime = Field(..., description="Server timestamp")
diff --git a/tests/unit/test_copilot_credential_pause.py b/tests/unit/test_copilot_credential_pause.py
index 3181a5a7d..01d760a1e 100644
--- a/tests/unit/test_copilot_credential_pause.py
+++ b/tests/unit/test_copilot_credential_pause.py
@@ -33,6 +33,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from agents import RunConfig
+from agents.tool_context import ToolContext
from fastapi import HTTPException, status
from structlog.testing import capture_logs
@@ -83,6 +84,7 @@ from skyvern.forge.sdk.copilot.enforcement import (
run_with_enforcement,
)
from skyvern.forge.sdk.copilot.hooks import CopilotRunHooks
+from skyvern.forge.sdk.copilot.narration import NarratorState, build_tool_call_activity
from skyvern.forge.sdk.copilot.request_policy import (
RequestPolicy,
_seed_prior_approved_credentials,
@@ -381,6 +383,10 @@ async def test_connected_action_mutates_policy_and_resolves(monkeypatch: pytest.
ctx.workflow_copilot_chat_id = "chat-1"
ctx.last_run_skipped_unbound_credentials = True
ctx.request_policy = RequestPolicy()
+ ctx.narrator_state = NarratorState()
+ ctx.narrator_state.record_activity(
+ build_tool_call_activity("run_blocks_and_collect_debug", 0, "call-run", timestamp=datetime.now(timezone.utc))
+ )
cache = _FakeCache()
cache.store[credential_response_cache_key("org-1", "chat-1", "turn-1")] = encode_credential_response(
@@ -412,6 +418,8 @@ async def test_connected_action_mutates_policy_and_resolves(monkeypatch: pytest.
assert "cred_1" in resume_text
card, resolved = (call.args[0] for call in stream.send.await_args_list)
assert card.type == WorkflowCopilotStreamMessageType.CREDENTIAL_REQUIRED
+ # The card renders after the row that was newest when it was raised, live and after reload.
+ assert card.anchor_tool_call_id == ctx.credential_pause_anchor_tool_call_id == "call-run"
assert resolved.type == WorkflowCopilotStreamMessageType.CREDENTIAL_PAUSE_RESOLVED
assert (resolved.resume_token, resolved.outcome, resolved.credential_id, resolved.name) == (
card.resume_token,
@@ -2086,6 +2094,31 @@ async def test_a_run_tool_called_alongside_the_ask_waits_for_the_user_to_answer(
assert ctx.credential_pause_outcome == "connected"
+async def test_an_ask_issued_alongside_a_run_renders_after_its_own_row(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Sibling calls in one model response log their rows before any of them runs, so the newest row
+ when the ask starts is a sibling's rather than the ask's own."""
+ ctx = _tool_ctx(
+ monkeypatch,
+ _SlowAnswerCache(credential_response_cache_key("org-1", "chat-1", "turn-1"), 0.01, "connected"),
+ )
+ _stub_credential_lookup(monkeypatch, _make_credential())
+ ctx.narrator_state = NarratorState()
+ for tool_name, call_id in (("request_credential", "call-ask"), ("run_blocks_and_collect_debug", "call-run")):
+ ctx.narrator_state.record_activity(
+ build_tool_call_activity(tool_name, 0, call_id, timestamp=datetime.now(timezone.utc))
+ )
+ arguments = json.dumps({"login_page_url": "https://portal.example.com/login", "reason": "Needs a sign-in."})
+
+ await tools_module.request_credential_tool.on_invoke_tool(
+ ToolContext(context=ctx, tool_name="request_credential", tool_call_id="call-ask", tool_arguments=arguments),
+ arguments,
+ )
+
+ (card,) = _sent_cards(ctx)
+ assert card.anchor_tool_call_id == ctx.credential_pause_anchor_tool_call_id == "call-ask"
+
+
async def _announce_ask(ctx: CopilotContext) -> None:
response = SimpleNamespace(output=[SimpleNamespace(name="request_credential")])
await CopilotRunHooks(ctx).on_llm_end(SimpleNamespace(context=ctx), MagicMock(), response) # type: ignore[arg-type]
diff --git a/tests/unit/test_copilot_make_agent_result_backfill.py b/tests/unit/test_copilot_make_agent_result_backfill.py
index 9205cd3db..ef2068535 100644
--- a/tests/unit/test_copilot_make_agent_result_backfill.py
+++ b/tests/unit/test_copilot_make_agent_result_backfill.py
@@ -447,6 +447,40 @@ def test_credential_auto_bound_survives_narrative_payload_serialization() -> Non
assert dumped["credentialAutoBound"] == {"credentialId": "cred_x", "name": "Work login"}
+def test_backfill_stamps_where_the_credential_pause_was_raised() -> None:
+ ctx = _ctx()
+ ctx.credential_pause_outcome = "connected"
+ ctx.credential_pause_connected_credential_id = "cred_work"
+ ctx.credential_pause_anchor_tool_call_id = "call-run"
+ result = _result(ctx, turn_outcome=_outcome(ResponseKind.BUILD), narrative_payload=_payload())
+ assert result.narrative_payload is not None
+ assert result.narrative_payload["credentialPause"] == {
+ "outcome": "connected",
+ "credentialId": "cred_work",
+ "anchorToolCallId": "call-run",
+ }
+
+
+def test_a_turns_plan_and_pause_anchor_survive_narrative_payload_serialization() -> None:
+ # The wire model drops keys the TypedDicts do not declare, which would lose the turn's plan and
+ # the pause's position on reload while the live stream still showed them.
+ message = WorkflowCopilotChatMessage(
+ workflow_copilot_chat_message_id="m1",
+ workflow_copilot_chat_id="c1",
+ sender=WorkflowCopilotChatSender.AI,
+ content="done",
+ created_at=datetime(2026, 1, 1),
+ modified_at=datetime(2026, 1, 1),
+ narrative_payload=_payload(
+ workPlan={"toolCallId": "p1", "items": ["Open the admin page", "Create the user"]},
+ credentialPause={"outcome": "skipped", "anchorToolCallId": "p1"},
+ ),
+ )
+ dumped = message.model_dump()["narrative_payload"]
+ assert dumped["workPlan"] == {"toolCallId": "p1", "items": ["Open the admin page", "Create the user"]}
+ assert dumped["credentialPause"]["anchorToolCallId"] == "p1"
+
+
def test_backfill_omits_credential_prompt_when_no_signal_present() -> None:
result = _result(_ctx(), user_response="Done, the workflow is ready.", narrative_payload=_payload())
assert result.narrative_payload is not None
diff --git a/tests/unit/test_copilot_narrative_activity.py b/tests/unit/test_copilot_narrative_activity.py
index 2c8098a5f..71f5b3b77 100644
--- a/tests/unit/test_copilot_narrative_activity.py
+++ b/tests/unit/test_copilot_narrative_activity.py
@@ -245,10 +245,13 @@ def test_build_narrative_payload_serializes_block_and_design_activity() -> None:
build_tool_result_activity("run_blocks_and_collect_debug", "ran step_1", True, 1, "c1", timestamp=_TS)
]
}
+ state.work_plan = {"toolCallId": "p1", "items": ["Open the admin page"]}
ctx.narrator_state = state
payload = _build_narrative_payload(ctx, terminal="response", terminal_message="done", narrative_summary="summary")
+ assert payload["workPlan"] == {"toolCallId": "p1", "items": ["Open the admin page"]}
+
assert payload["designActivity"] == [
{
"kind": "narration",
diff --git a/tests/unit/test_copilot_streaming_adapter.py b/tests/unit/test_copilot_streaming_adapter.py
index 803c73824..a2ee26d35 100644
--- a/tests/unit/test_copilot_streaming_adapter.py
+++ b/tests/unit/test_copilot_streaming_adapter.py
@@ -21,7 +21,12 @@ from openai.types.responses.response_function_tool_call import ResponseFunctionT
from skyvern.forge.sdk.copilot import streaming_adapter as streaming_adapter_module
from skyvern.forge.sdk.copilot.context import CopilotContext, InFlightStreamToolCall
-from skyvern.forge.sdk.copilot.narration import NarratorState, TransitionKind, schedule_narration
+from skyvern.forge.sdk.copilot.narration import (
+ MAX_DESIGN_ACTIVITY_ENTRIES,
+ NarratorState,
+ TransitionKind,
+ schedule_narration,
+)
from skyvern.forge.sdk.copilot.streaming_adapter import (
_sanitize_input,
_update_enforcement_from_tool,
@@ -840,6 +845,59 @@ def test_tool_result_executed_source_reference_only_for_browser_code() -> None:
assert _tool_result_executed_source_reference("run_browser_code", {"executed_source_reference": 42}) is None
+async def test_a_stored_work_plan_outlives_its_row_being_capped_away() -> None:
+ plan = ["Open the admin page", "Create the user"]
+
+ def tool_output(call_id: str, payload: dict[str, Any]) -> RunItemStreamEvent:
+ out_item = MagicMock(spec=RunItem)
+ out_item.raw_item = {"call_id": call_id}
+ out_item.output = [{"type": "text", "text": json.dumps(payload)}]
+ return RunItemStreamEvent(name="tool_output", item=out_item)
+
+ # Enough later calls that the design activity cap trims the plan's own rows.
+ filler = [
+ event
+ for index in range(MAX_DESIGN_ACTIVITY_ENTRIES)
+ for event in (
+ _tool_called_event(f"f{index}", "evaluate"),
+ tool_output(f"f{index}", {"ok": True}),
+ )
+ ]
+ result = MagicMock()
+ result.stream_events = lambda: _stream_events_from(
+ _tool_called_event("p1", "set_work_plan"),
+ tool_output("p1", {"ok": True, "items": plan}),
+ # A refused write echoes the plan still in force; it is not a new plan.
+ _tool_called_event("p2", "set_work_plan"),
+ tool_output("p2", {"ok": False, "error": "too long", "items": ["something else"]}),
+ *filler,
+ )
+ result.cancel = MagicMock()
+
+ sent: list[Any] = []
+
+ async def _send(payload: Any) -> bool:
+ sent.append(payload)
+ return True
+
+ stream = MagicMock()
+ stream.is_disconnected = AsyncMock(return_value=False)
+ stream.send = _send
+ ctx = _new_ctx()
+
+ await stream_to_sse(result, stream, ctx)
+
+ plan_results = [
+ p
+ for p in sent
+ if getattr(p, "type", None) == WorkflowCopilotStreamMessageType.TOOL_RESULT and p.tool_name == "set_work_plan"
+ ]
+ assert [p.work_plan for p in plan_results] == [plan, None]
+ assert "tr-p1" not in {row["id"] for row in ctx.narrator_state.design_activity}
+ assert ctx.narrator_state.work_plan == {"toolCallId": "p1", "items": plan}
+
+
@pytest.mark.asyncio
async def test_a_stashed_write_diff_rides_one_result_and_no_later_foreign_one() -> None:
diffs = [{"label": "download_step", "added": 3, "removed": 1, "patch": "@@\n-old\n+new"}]