Skip to content

Automate browser based workflows with AI

Editing a code block's Goal keeps your text and offers to update the block to match (SKY-16395) (#8711)

Andrew Neilson14d990ed6dad
diff --git a/docs/api-reference/openapi.json b/docs/api-reference/openapi.json
index e19256679..e1c975fd0 100644
--- a/docs/api-reference/openapi.json
+++ b/docs/api-reference/openapi.json
@@ -11483,6 +11483,28 @@
}
],
"title": "Data Schema"
+ },
+ "user_owned_goal": {
+ "anyOf": [
+ {
+ "type": "boolean"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "User Owned Goal"
+ },
+ "goal_needs_regeneration": {
+ "anyOf": [
+ {
+ "type": "boolean"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Goal Needs Regeneration"
}
},
"type": "object",
@@ -11710,6 +11732,30 @@
],
"title": "Data Schema",
"description": "JSON schema of the object this block's return produces; keys match the return keys; null when the block returns nothing"
+ },
+ "user_owned_goal": {
+ "anyOf": [
+ {
+ "type": "boolean"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "User Owned Goal",
+ "description": "True when a person wrote this block's Goal, so copilot regenerations keep their text. Set by the editor or the workflow API; a value the copilot submits is ignored in favour of the stored one"
+ },
+ "goal_needs_regeneration": {
+ "anyOf": [
+ {
+ "type": "boolean"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Goal Needs Regeneration",
+ "description": "True when a person edited the Goal and the code has not been rebuilt from it yet. Set by the editor or the workflow API; a value the copilot submits is ignored in favour of the stored one"
}
},
"type": "object",
diff --git a/skyvern-frontend/src/hooks/useDeferredLockedEdit.ts b/skyvern-frontend/src/hooks/useDeferredLockedEdit.ts
index 392acf575..befa35c7a 100644
--- a/skyvern-frontend/src/hooks/useDeferredLockedEdit.ts
+++ b/skyvern-frontend/src/hooks/useDeferredLockedEdit.ts
@@ -25,6 +25,17 @@ export function registerBufferedEditorFlusher(flush: () => void): () => void {
};
}
+const bufferedEditListeners = new Set<() => void>();
+
+// A buffered edit lands a debounce after the keystroke that caused it; listeners
+// hear it just before it is applied, so the canvas can still read it as the user's.
+export function subscribeToBufferedEdits(listener: () => void): () => void {
+ bufferedEditListeners.add(listener);
+ return () => {
+ bufferedEditListeners.delete(listener);
+ };
+}
+
export function flushBufferedEditorEdits() {
if (isEditorMutationLocked()) return;
// Save-data callbacks are registered by effects after the graph renders.
@@ -115,6 +126,7 @@ export function useDeferredLockedEdit({
if (isMutationLocked()) return;
pendingRef.current = null;
if (deferKey !== undefined) deferredEdits.delete(deferKey);
+ for (const listener of bufferedEditListeners) listener();
onChangeRef.current?.(newValue);
}, debounceMs);
diff --git a/skyvern-frontend/src/routes/workflows/copilot/PendingGoalChangesCard.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/PendingGoalChangesCard.test.tsx
new file mode 100644
index 000000000..aef6d6e23
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/copilot/PendingGoalChangesCard.test.tsx
@@ -0,0 +1,98 @@
+// @vitest-environment jsdom
+
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, test, vi } from "vitest";
+
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
+
+import { PendingGoalChangesCard } from "./PendingGoalChangesCard";
+
+afterEach(() => {
+ cleanup();
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [],
+ pendingBuild: null,
+ generatingBlockLabel: null,
+ queuedBuilds: [],
+ undoGoalChange: () => {},
+ });
+});
+
+describe("PendingGoalChangesCard", () => {
+ test("renders nothing when no Goal change is pending", () => {
+ render(<PendingGoalChangesCard />);
+
+ expect(
+ screen.queryByRole("region", { name: "Goal changes not applied" }),
+ ).toBeNull();
+ });
+
+ test("shows the old and new Goal and applies the new one on request", () => {
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [
+ {
+ label: "find_provider",
+ goal: "Return every provider's phone number",
+ previousGoal: "Return the first provider's address",
+ },
+ ],
+ });
+ render(<PendingGoalChangesCard />);
+
+ expect(
+ screen.getByText("Return the first provider's address"),
+ ).toBeTruthy();
+ expect(
+ screen.getByText("Return every provider's phone number"),
+ ).toBeTruthy();
+
+ fireEvent.click(screen.getByRole("button", { name: "Apply new Goal" }));
+
+ expect(useCopilotActionStore.getState().pendingBuild).toEqual({
+ blockLabel: "find_provider",
+ prompt: "Return every provider's phone number",
+ applyingGoalChange: true,
+ });
+ });
+
+ test("applies several Goal changes one after another", () => {
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [
+ { label: "first", goal: "Goal one", previousGoal: "Old one" },
+ { label: "second", goal: "Goal two", previousGoal: "Old two" },
+ ],
+ });
+ render(<PendingGoalChangesCard />);
+
+ fireEvent.click(screen.getByRole("button", { name: "Apply new Goals" }));
+
+ const state = useCopilotActionStore.getState();
+ expect(state.pendingBuild?.blockLabel).toBe("first");
+ expect(state.queuedBuilds.map((queued) => queued.blockLabel)).toEqual([
+ "second",
+ ]);
+ expect(screen.getByText("Applying the new Goal…")).toBeTruthy();
+ expect(
+ screen.queryByRole("button", { name: "Apply new Goals" }),
+ ).toBeNull();
+ });
+
+ test("Undo asks the canvas to restore that block's old Goal", () => {
+ const undoGoalChange = vi.fn();
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [
+ { label: "find_provider", goal: "New", previousGoal: "Old" },
+ ],
+ undoGoalChange,
+ });
+ render(<PendingGoalChangesCard />);
+
+ fireEvent.click(
+ screen.getByRole("button", {
+ name: "Undo the Goal change on find_provider",
+ }),
+ );
+
+ expect(undoGoalChange).toHaveBeenCalledWith("find_provider");
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/copilot/PendingGoalChangesCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/PendingGoalChangesCard.tsx
new file mode 100644
index 000000000..27c245134
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/copilot/PendingGoalChangesCard.tsx
@@ -0,0 +1,84 @@
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
+
+export function PendingGoalChangesCard() {
+ const changes = useCopilotActionStore((state) => state.pendingGoalChanges);
+ const generatingBlockLabel = useCopilotActionStore(
+ (state) => state.generatingBlockLabel,
+ );
+ const queuedBuilds = useCopilotActionStore((state) => state.queuedBuilds);
+ const applyPendingGoalChanges = useCopilotActionStore(
+ (state) => state.applyPendingGoalChanges,
+ );
+ const undoGoalChange = useCopilotActionStore((state) => state.undoGoalChange);
+
+ if (changes.length === 0) {
+ return null;
+ }
+ const applying = changes.some(
+ (change) =>
+ change.label === generatingBlockLabel ||
+ queuedBuilds.some((queued) => queued.blockLabel === change.label),
+ );
+ const single = changes.length === 1;
+
+ return (
+ <section
+ aria-label="Goal changes not applied"
+ className="mb-2 space-y-2 rounded-md border border-amber-500/40 bg-amber-500/10 p-2 text-xs"
+ >
+ <p className="font-medium text-foreground">
+ {single
+ ? `Goal changed on “${changes[0]!.label}”`
+ : `Goals changed on ${changes.length} blocks`}
+ </p>
+ <ul className="space-y-2">
+ {changes.map((change) => (
+ <li key={change.label} className="space-y-1">
+ {single ? null : (
+ <p className="font-medium text-foreground">{change.label}</p>
+ )}
+ {change.previousGoal === null ? null : (
+ <p
+ className="line-clamp-3 text-muted-foreground"
+ title={change.previousGoal}
+ >
+ <span aria-label="Old Goal">− </span>
+ {change.previousGoal}
+ </p>
+ )}
+ <p className="line-clamp-3 text-foreground" title={change.goal}>
+ <span aria-label="New Goal">+ </span>
+ {change.goal}
+ </p>
+ {change.previousGoal === null || applying ? null : (
+ <button
+ type="button"
+ aria-label={`Undo the Goal change on ${change.label}`}
+ onClick={() => undoGoalChange(change.label)}
+ className="rounded-md border border-border bg-slate-elevation1 px-2 py-0.5 text-xs text-foreground hover:bg-slate-elevation2 dark:text-slate-200"
+ >
+ Undo
+ </button>
+ )}
+ </li>
+ ))}
+ </ul>
+ <p className="text-muted-foreground">
+ {applying
+ ? "Applying the new Goal…"
+ : single
+ ? "This block still does what its old Goal said."
+ : "These blocks still do what their old Goals said."}
+ </p>
+ {applying ? null : (
+ <button
+ type="button"
+ onClick={applyPendingGoalChanges}
+ className="rounded-md bg-primary px-2 py-0.5 text-xs text-primary-foreground hover:bg-primary/90"
+ >
+ {single ? "Apply new Goal" : "Apply new Goals"}
+ </button>
+ )}
+ </section>
+ );
+}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
index 030a1153d..48ac44672 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
@@ -807,7 +807,7 @@ describe("WorkflowCopilotChat — keep the chat live during a turn", () => {
fireEvent.click(screen.getByRole("button", { name: "Cancel question" }));
});
- expect(screen.getByText(prompt)).toBeTruthy();
+ expect(await screen.findByText(prompt)).toBeTruthy();
saveData.workflow.workflow_id = workflowId;
});
@@ -2677,6 +2677,27 @@ describe("WorkflowCopilotChat — keep the chat live during a turn", () => {
);
});
+ it("a build still running when the chat unmounts does not hold up the next one", async () => {
+ const view = await renderChat();
+ await act(async () => {
+ useCopilotActionStore
+ .getState()
+ .requestBuild({ blockLabel: "open_page", prompt: "open the page" });
+ });
+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));
+
+ view.unmount();
+ useCopilotActionStore.getState().requestBuild({
+ blockLabel: "read_account",
+ prompt: "Return the balance",
+ applyingGoalChange: true,
+ });
+
+ expect(useCopilotActionStore.getState().pendingBuild?.blockLabel).toBe(
+ "read_account",
+ );
+ });
+
it("does not arm a block-build target when its generate no-ops behind a queued prompt", async () => {
await renderChat();
await submit("first message");
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
index a4cc1dba5..b761912a5 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
@@ -132,6 +132,7 @@ import {
} from "./workflowCopilotTypes";
import { WorkflowCopilotHistory } from "./WorkflowCopilotHistory";
import { AutoAcceptChip } from "./AutoAcceptChip";
+import { PendingGoalChangesCard } from "./PendingGoalChangesCard";
import { SelectedBlockChip } from "./SelectedBlockChip";
import { readSelectedBlockLabel } from "./selectedBlockLabel";
import { selectAutoBoundReceiptIndexes } from "./autoBoundReceiptIndexes";
@@ -8384,9 +8385,10 @@ export function WorkflowCopilotChat({
if (!pendingBlockBuild) {
return;
}
- blockBuildMessageRef.current =
- `Rebuild the "${pendingBlockBuild.blockLabel}" code block so it accomplishes ` +
- `this goal, and update its code and steps accordingly: ${pendingBlockBuild.prompt}`;
+ blockBuildMessageRef.current = pendingBlockBuild.applyingGoalChange
+ ? `Update "${pendingBlockBuild.blockLabel}" to match its new Goal: ${pendingBlockBuild.prompt}`
+ : `Rebuild the "${pendingBlockBuild.blockLabel}" code block so it accomplishes ` +
+ `this goal, and update its code and steps accordingly: ${pendingBlockBuild.prompt}`;
blockBuildTargetLabelRef.current = pendingBlockBuild.blockLabel;
setBlockBuildArmNonce((nonce) => nonce + 1);
clearPendingBlockBuild();
@@ -8416,6 +8418,18 @@ export function WorkflowCopilotChat({
});
}, [blockBuildArmNonce, acceptUnresolved, handleSend, finishBlockGenerating]);
+ // The build state is global and only a mounted chat finishes a build, so one left running here
+ // would hold every later Apply in the queue behind it.
+ useEffect(
+ () => () =>
+ useCopilotActionStore.setState({
+ pendingBuild: null,
+ generatingBlockLabel: null,
+ queuedBuilds: [],
+ }),
+ [],
+ );
+
const blockGenLoadingRef = useRef(isLoading);
useEffect(() => {
if (
@@ -10175,6 +10189,7 @@ export function WorkflowCopilotChat({
{inputStatusText}
</div>
) : null}
+ <PendingGoalChangesCard />
<SelectedBlockChip />
{hasPendingQuestion &&
(attachments.length > 0 ||
diff --git a/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx b/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
index b91a67960..1902c4602 100644
--- a/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
@@ -15,7 +15,10 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import { useOnChange } from "@/hooks/useOnChange";
-import { flushBufferedEditorEdits } from "@/hooks/useDeferredLockedEdit";
+import {
+ flushBufferedEditorEdits,
+ subscribeToBufferedEdits,
+} from "@/hooks/useDeferredLockedEdit";
import { cn } from "@/util/utils";
import { useShouldNotifyWhenClosingTab } from "@/hooks/useShouldNotifyWhenClosingTab";
import { BlockActionContext } from "@/store/BlockActionContext";
@@ -117,6 +120,7 @@ import {
START_ANCHOR_MIN_ZOOM,
} from "./paneFit";
import { useBlockerExit } from "./useBlockerExit";
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
import { WorkflowScopeContext } from "./WorkflowScopeContext";
import { FitViewControl } from "./controls/FitViewControl";
import { FlowJumpControls } from "./controls/FlowJumpControls";
@@ -771,14 +775,26 @@ function FlowRenderer({
setGetSaveDataRef.current = workflowChangesStore.setGetSaveData;
const saveWorkflow = useWorkflowSave({ status: "published" });
useShouldNotifyWhenClosingTab(!readOnly && workflowChangesStore.hasChanges);
+ const pendingGoalChanges = useCopilotActionStore(
+ (state) => state.pendingGoalChanges,
+ );
const blocker = useBlocker(({ currentLocation, nextLocation }) => {
+ if (readOnly || nextLocation.pathname === currentLocation.pathname) {
+ return false;
+ }
+ // A Goal change can be saved without being applied (API writes, or a proposal accepted for
+ // another block), so Run has to ask even when nothing is unsaved.
return (
- !readOnly &&
- workflowChangesStore.hasChanges &&
- nextLocation.pathname !== currentLocation.pathname
+ workflowChangesStore.hasChanges ||
+ (pendingGoalChanges.length > 0 &&
+ nextLocation.pathname ===
+ `/agents/${workflow.workflow_permanent_id}/run`)
);
});
const blockerExit = useBlockerExit(blocker);
+ const applyPendingGoalChanges = useCopilotActionStore(
+ (state) => state.applyPendingGoalChanges,
+ );
// Studio-only: list what changed inside the leave/run unsaved-changes modal.
// Memoized on the blocked state so it runs once when the modal opens (the
@@ -1143,11 +1159,13 @@ function FlowRenderer({
document.addEventListener("pointerup", onPointerUp, true);
document.addEventListener("pointercancel", onPointerUp, true);
document.addEventListener("keydown", onKeyDown, true);
+ const unsubscribeBufferedEdits = subscribeToBufferedEdits(markGesture);
return () => {
document.removeEventListener("pointerdown", onPointerDown, true);
document.removeEventListener("pointerup", onPointerUp, true);
document.removeEventListener("pointercancel", onPointerUp, true);
document.removeEventListener("keydown", onKeyDown, true);
+ unsubscribeBufferedEdits();
};
}, [embedded, readOnly]);
@@ -2407,10 +2425,23 @@ function FlowRenderer({
>
<DialogContent>
<DialogHeader>
- <DialogTitle>Unsaved Changes</DialogTitle>
+ <DialogTitle>
+ {pendingGoalChanges.length === 0
+ ? "Unsaved Changes"
+ : pendingGoalChanges.length === 1
+ ? "New Goal not applied"
+ : "New Goals not applied"}
+ </DialogTitle>
<DialogDescription>
- Your workflow has unsaved changes. Do you want to save them
- before leaving?
+ {pendingGoalChanges.length > 0
+ ? `${pendingGoalChanges
+ .map((change) => `“${change.label}”`)
+ .join(", ")} ${
+ pendingGoalChanges.length === 1
+ ? "has a new Goal it doesn't follow yet. Apply it"
+ : "have new Goals they don't follow yet. Apply them"
+ } first, or continue with the last saved version.`
+ : "Your workflow has unsaved changes. Do you want to save them before leaving?"}
</DialogDescription>
</DialogHeader>
<WorkflowChangesList changes={unsavedChangeSummary} />
@@ -2426,21 +2457,34 @@ function FlowRenderer({
>
Continue without saving
</Button>
- <Button
- onClick={() => {
- handleSave().then((ok) => {
- if (ok) {
- blockerExit.proceed();
- }
- });
- }}
- disabled={workflowChangesStore.saveIsPending}
- >
- {workflowChangesStore.saveIsPending && (
- <ReloadIcon className="mr-2 h-4 w-4 animate-spin" />
- )}
- Save changes
- </Button>
+ {pendingGoalChanges.length > 0 ? (
+ <Button
+ onClick={() => {
+ blockerExit.reset();
+ applyPendingGoalChanges();
+ }}
+ >
+ {pendingGoalChanges.length === 1
+ ? "Apply new Goal"
+ : "Apply new Goals"}
+ </Button>
+ ) : (
+ <Button
+ onClick={() => {
+ handleSave().then((ok) => {
+ if (ok) {
+ blockerExit.proceed();
+ }
+ });
+ }}
+ disabled={workflowChangesStore.saveIsPending}
+ >
+ {workflowChangesStore.saveIsPending && (
+ <ReloadIcon className="mr-2 h-4 w-4 animate-spin" />
+ )}
+ Save changes
+ </Button>
+ )}
</DialogFooter>
</DialogContent>
</Dialog>
diff --git a/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesDialog.tsx b/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesDialog.tsx
new file mode 100644
index 000000000..fba129c9d
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesDialog.tsx
@@ -0,0 +1,89 @@
+import { flushSync } from "react-dom";
+
+import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogClose,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
+
+type Props = {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ onSave: () => void;
+};
+
+/** Asked on Save when a block's new Goal isn't applied yet: apply it, or undo it and save the rest. */
+export function PendingGoalChangesDialog({
+ open,
+ onOpenChange,
+ onSave,
+}: Props) {
+ const changes = useCopilotActionStore((state) => state.pendingGoalChanges);
+ const applyPendingGoalChanges = useCopilotActionStore(
+ (state) => state.applyPendingGoalChanges,
+ );
+ const undoGoalChange = useCopilotActionStore((state) => state.undoGoalChange);
+
+ const single = changes.length === 1;
+ const names = changes.map((change) => `“${change.label}”`).join(", ");
+ const canUndoAll =
+ changes.length > 0 &&
+ changes.every((change) => change.previousGoal !== null);
+
+ return (
+ <Dialog open={open} onOpenChange={onOpenChange}>
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>
+ {single ? "New Goal not applied" : "New Goals not applied"}
+ </DialogTitle>
+ <DialogDescription>
+ {single
+ ? `${names} has a new Goal it doesn't follow yet.`
+ : `${names} have new Goals they don't follow yet.`}{" "}
+ Apply {single ? "it" : "them"} before saving, or undo the Goal{" "}
+ {single ? "change" : "changes"} and save everything else.
+ </DialogDescription>
+ </DialogHeader>
+ <DialogFooter>
+ <DialogClose asChild>
+ <Button variant="secondary">Cancel</Button>
+ </DialogClose>
+ {canUndoAll ? (
+ <Button
+ variant="secondary"
+ onClick={() => {
+ // The canvas must render the undo before the save reads it, or the save carries the new Goal.
+ flushSync(() => {
+ for (const change of changes) {
+ undoGoalChange(change.label);
+ }
+ });
+ onOpenChange(false);
+ onSave();
+ }}
+ >
+ {single
+ ? "Undo Goal change and save"
+ : "Undo Goal changes and save"}
+ </Button>
+ ) : null}
+ <Button
+ onClick={() => {
+ applyPendingGoalChanges();
+ onOpenChange(false);
+ }}
+ >
+ {single ? "Apply new Goal" : "Apply new Goals"}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesPublisher.test.tsx b/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesPublisher.test.tsx
new file mode 100644
index 000000000..5ff017ea9
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesPublisher.test.tsx
@@ -0,0 +1,96 @@
+// @vitest-environment jsdom
+
+import {
+ act,
+ cleanup,
+ fireEvent,
+ render,
+ screen,
+} from "@testing-library/react";
+import { ReactFlowProvider } from "@xyflow/react";
+import { afterEach, expect, test, vi } from "vitest";
+
+import { flushBufferedEditorEdits } from "@/hooks/useDeferredLockedEdit";
+
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
+
+import type { AppNode } from "./nodes";
+import { codeBlockNodeDefaultData } from "./nodes/CodeBlockNode/types";
+import { PendingGoalChangesDialog } from "./PendingGoalChangesDialog";
+import { PendingGoalChangesPublisher } from "./PendingGoalChangesPublisher";
+
+afterEach(cleanup);
+
+const editedBlock = {
+ id: "cb1",
+ type: "codeBlock",
+ position: { x: 0, y: 0 },
+ data: {
+ ...codeBlockNodeDefaultData,
+ label: "find_provider",
+ prompt: "Return every provider's phone number",
+ userOwnedGoal: true,
+ goalNeedsRegeneration: true,
+ goalBeforeEdit: {
+ prompt: "Return the first provider's address",
+ userOwnedGoal: null,
+ goalNeedsRegeneration: null,
+ },
+ },
+} as AppNode;
+
+test("publishes a pending Goal change and undoes it in the canvas", () => {
+ render(
+ <ReactFlowProvider defaultNodes={[editedBlock]}>
+ <PendingGoalChangesPublisher />
+ </ReactFlowProvider>,
+ );
+
+ expect(useCopilotActionStore.getState().pendingGoalChanges).toEqual([
+ {
+ label: "find_provider",
+ goal: "Return every provider's phone number",
+ previousGoal: "Return the first provider's address",
+ },
+ ]);
+
+ act(() => {
+ useCopilotActionStore.getState().undoGoalChange("find_provider");
+ });
+
+ expect(useCopilotActionStore.getState().pendingGoalChanges).toEqual([]);
+});
+
+test("undo and save saves after the canvas has dropped the new Goal", () => {
+ const consoleError = vi.spyOn(console, "error");
+ const onOpenChange = vi.fn();
+ let changesAtSave: number | undefined;
+ const save = vi.fn(() => {
+ changesAtSave = useCopilotActionStore.getState().pendingGoalChanges.length;
+ // The real save forces buffered edits through with flushSync before it writes.
+ flushBufferedEditorEdits();
+ });
+ render(
+ <ReactFlowProvider defaultNodes={[editedBlock]}>
+ <PendingGoalChangesPublisher />
+ <PendingGoalChangesDialog
+ open
+ onOpenChange={onOpenChange}
+ onSave={save}
+ />
+ </ReactFlowProvider>,
+ );
+
+ fireEvent.click(
+ screen.getByRole("button", { name: "Undo Goal change and save" }),
+ );
+
+ expect(save).toHaveBeenCalledOnce();
+ expect(changesAtSave).toBe(0);
+ expect(onOpenChange).toHaveBeenCalledWith(false);
+ expect(consoleError).not.toHaveBeenCalledWith(
+ expect.stringContaining("flushSync was called from inside a lifecycle"),
+ expect.anything(),
+ );
+ consoleError.mockRestore();
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesPublisher.tsx b/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesPublisher.tsx
new file mode 100644
index 000000000..efd34a982
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/PendingGoalChangesPublisher.tsx
@@ -0,0 +1,61 @@
+import { useNodes, useReactFlow } from "@xyflow/react";
+import { useCallback, useEffect } from "react";
+
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
+import { useWorkflowTitleStore } from "@/store/WorkflowTitleStore";
+import { refuseMutationDuringYamlCommit } from "@/store/WorkflowYamlEditorStore";
+
+import { isWorkflowBlockNode, type AppNode } from "./nodes";
+import {
+ goalChangeUndoPatch,
+ pendingGoalChangesOf,
+} from "./workflowEditorUtils";
+
+export function PendingGoalChangesPublisher() {
+ const nodes = useNodes<AppNode>();
+ const { getNodes, updateNodeData } = useReactFlow<AppNode>();
+ const setPendingGoalChanges = useCopilotActionStore(
+ (state) => state.setPendingGoalChanges,
+ );
+ const setUndoGoalChange = useCopilotActionStore(
+ (state) => state.setUndoGoalChange,
+ );
+
+ const undoGoalChange = useCallback(
+ (label: string) => {
+ if (refuseMutationDuringYamlCommit()) {
+ return;
+ }
+ const node = getNodes().find(
+ (candidate) =>
+ isWorkflowBlockNode(candidate) &&
+ candidate.type === "codeBlock" &&
+ candidate.data.label === label,
+ );
+ if (!node || node.type !== "codeBlock") {
+ return;
+ }
+ const patch = goalChangeUndoPatch(node.data);
+ if (patch) {
+ useWorkflowTitleStore.getState().recordCopilotGraphEdit();
+ updateNodeData(node.id, patch);
+ }
+ },
+ [getNodes, updateNodeData],
+ );
+
+ useEffect(() => {
+ setPendingGoalChanges(pendingGoalChangesOf(nodes));
+ }, [nodes, setPendingGoalChanges]);
+ useEffect(() => {
+ setUndoGoalChange(undoGoalChange);
+ }, [undoGoalChange, setUndoGoalChange]);
+ useEffect(
+ () => () => {
+ setPendingGoalChanges([]);
+ setUndoGoalChange(() => {});
+ },
+ [setPendingGoalChanges, setUndoGoalChange],
+ );
+ return null;
+}
diff --git a/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.test.tsx b/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.test.tsx
index afccd6009..beb344784 100644
--- a/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.test.tsx
@@ -37,6 +37,7 @@ import { summarizeWorkflowChanges, snapshotOf } from "./workflowChangesSummary";
import { TooltipProvider } from "@/components/ui/tooltip";
import { ProxySelector } from "@/components/ProxySelector";
import { TitleSection } from "../studio/StudioTopBar";
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
import { useWorkflowSnapshotStore } from "@/store/WorkflowSnapshotStore";
import type { WorkflowCopilotChatHistoryResponse } from "../copilot/workflowCopilotTypes";
import { Status } from "@/api/types";
@@ -2017,7 +2018,7 @@ describe("save failures stop navigation and block runs", () => {
cleanup();
client.clear();
});
- test("saves a corrected required prompt through the navigation blocker before its debounce fires", async () => {
+ function renderEmptyPromptNavigationWorkspace() {
useWorkflowYamlEditorStore.setState(
useWorkflowYamlEditorStore.getInitialState(),
);
@@ -2110,6 +2111,38 @@ describe("save failures stop navigation and block runs", () => {
</MemoryRouter>
</QueryClientProvider>,
);
+ return { view, client };
+ }
+
+ test("a first text edit after load lights the unsaved-changes dot", () => {
+ useWorkflowSnapshotStore.getState().clearSnapshot();
+ const { view, client } = renderEmptyPromptNavigationWorkspace();
+ try {
+ const prompt = view.container.querySelector<HTMLTextAreaElement>(
+ 'textarea[name="navigationGoal"]',
+ );
+ vi.useFakeTimers();
+ fireEvent.keyDown(prompt!, { key: "O" });
+ fireEvent.change(prompt!, {
+ target: { value: "Open the dashboard" },
+ });
+ act(() => {
+ vi.advanceTimersByTime(300);
+ });
+ act(() => {
+ vi.advanceTimersByTime(250);
+ });
+ expect(useWorkflowSnapshotStore.getState().contentDirty).toBe(true);
+ } finally {
+ view.unmount();
+ client.clear();
+ vi.useRealTimers();
+ clearDeferredEdits();
+ }
+ });
+
+ test("saves a corrected required prompt through the navigation blocker before its debounce fires", async () => {
+ const { view, client } = renderEmptyPromptNavigationWorkspace();
try {
const prompt = view.container.querySelector<HTMLTextAreaElement>(
'textarea[name="navigationGoal"]',
@@ -2211,6 +2244,91 @@ describe("save failures stop navigation and block runs", () => {
client.clear();
},
);
+ test("Run asks about a saved Goal that is not applied yet; leaving the editor does not", async () => {
+ vi.stubGlobal(
+ "ResizeObserver",
+ class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ },
+ );
+ useWorkflowYamlEditorStore.setState(
+ useWorkflowYamlEditorStore.getInitialState(),
+ );
+ useWorkflowHasChangesStore.setState(
+ useWorkflowHasChangesStore.getInitialState(),
+ );
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [
+ { label: "read_account", goal: "New", previousGoal: null },
+ ],
+ });
+ integration.realNavigation = true;
+ const workflow = {
+ ...(liveWorkflow as WorkflowApiResponse),
+ workflow_permanent_id: "wpid_live",
+ };
+ workflowQueryMock.mockReturnValue({ data: workflow, isLoading: false });
+ const client = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ const router = createMemoryRouter(
+ [
+ {
+ path: "/agents/wpid_live/edit",
+ element: (
+ <ReactFlowProvider>
+ <DebugStoreContext.Provider
+ value={{ isDebugMode: false, blockRunsEnabled: false }}
+ >
+ <Link to="/agents/wpid_live/run">Run</Link>
+ <Link to="/away">Leave editor</Link>
+ <FlowRenderer
+ nodes={[]}
+ edges={[]}
+ setNodes={vi.fn()}
+ setEdges={vi.fn()}
+ onNodesChange={vi.fn()}
+ onEdgesChange={vi.fn()}
+ initialTitle="Live agent"
+ workflow={workflow}
+ />
+ </DebugStoreContext.Provider>
+ </ReactFlowProvider>
+ ),
+ },
+ { path: "/agents/wpid_live/run", element: <p>Run page</p> },
+ { path: "/away", element: <p>Away</p> },
+ ],
+ { initialEntries: ["/agents/wpid_live/edit"] },
+ );
+ try {
+ render(
+ <QueryClientProvider client={client}>
+ <RouterProvider router={router} />
+ </QueryClientProvider>,
+ );
+
+ fireEvent.click(screen.getByRole("link", { name: "Run" }));
+ expect(await screen.findByText("New Goal not applied")).toBeTruthy();
+ expect(router.state.location.pathname).toBe("/agents/wpid_live/edit");
+
+ fireEvent.click(
+ screen.getByRole("button", { name: "Continue without saving" }),
+ );
+ expect(await screen.findByText("Run page")).toBeTruthy();
+
+ await act(() => router.navigate("/agents/wpid_live/edit"));
+ fireEvent.click(screen.getByRole("link", { name: "Leave editor" }));
+ expect(await screen.findByText("Away")).toBeTruthy();
+ } finally {
+ cleanup();
+ client.clear();
+ integration.realNavigation = false;
+ useCopilotActionStore.setState({ pendingGoalChanges: [] });
+ }
+ });
test.each([new SaveRefusedError(), new SaveStaleError()])(
"does not start a block run after %s",
async (error) => {
diff --git a/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx b/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
index 9870188a7..5625e0164 100644
--- a/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
@@ -1,5 +1,7 @@
import { apiWorkflowToSettings } from "@/routes/workflows/editor/apiWorkflowToSettings";
import { ReactFlowProvider } from "@xyflow/react";
+
+import { PendingGoalChangesPublisher } from "./PendingGoalChangesPublisher";
import { useWorkflowPermanentId } from "@/routes/workflows/WorkflowPermanentIdContext";
import { useStudioRunId } from "../studio/useStudioRunId";
import { useWorkflowQuery } from "../hooks/useWorkflowQuery";
@@ -117,6 +119,7 @@ function WorkflowEditor() {
) : null}
<div className="relative flex min-h-0 flex-1">
<ReactFlowProvider>
+ <PendingGoalChangesPublisher />
{studioEnabled ? (
<StudioShell
key={workflowPermanentId}
diff --git a/skyvern-frontend/src/routes/workflows/editor/WorkflowHeader.tsx b/skyvern-frontend/src/routes/workflows/editor/WorkflowHeader.tsx
index 80ae98ab3..a71e21fec 100644
--- a/skyvern-frontend/src/routes/workflows/editor/WorkflowHeader.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/WorkflowHeader.tsx
@@ -7,7 +7,7 @@ import {
PlayIcon,
ReloadIcon,
} from "@radix-ui/react-icons";
-import { type ReactNode } from "react";
+import { type ReactNode, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useWorkflowPermanentId } from "@/routes/workflows/WorkflowPermanentIdContext";
@@ -45,6 +45,8 @@ import { EditorOverflowMenu } from "./header/EditorOverflowMenu";
import { InputsCountBadge } from "./WorkflowInputs";
import { useIsGeneratingCode } from "./hooks/useIsGeneratingCode";
import { SaveFailedError, useSaveWorkflow } from "./hooks/useSaveWorkflow";
+import { PendingGoalChangesDialog } from "./PendingGoalChangesDialog";
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
import { useToggleCodeView } from "./hooks/useToggleCodeView";
import { useWorkflowHeaderCollapseStore } from "./useWorkflowHeaderCollapseStore";
import { WorkflowHeaderCollapseTab } from "./WorkflowHeaderCollapseTab";
@@ -108,6 +110,22 @@ function SaveButton() {
const isRecording = useRecordingStore().isRecording;
const isGlobalWorkflow = useIsGlobalWorkflow();
const onSave = useSaveWorkflow();
+ const pendingGoalChangeCount = useCopilotActionStore(
+ (state) => state.pendingGoalChanges.length,
+ );
+ const [goalDialogOpen, setGoalDialogOpen] = useState(false);
+ const save = () => {
+ void onSave().catch((error: unknown) => {
+ if (
+ error instanceof SaveRefusedError ||
+ error instanceof SaveStaleError ||
+ error instanceof SaveFailedError
+ ) {
+ return;
+ }
+ console.error("Failed to save workflow:", error);
+ });
+ };
return (
<TooltipProvider>
@@ -119,16 +137,11 @@ function SaveButton() {
className="size-10 min-w-[2.5rem]"
disabled={isGlobalWorkflow || isRecording}
onClick={() => {
- void onSave().catch((error: unknown) => {
- if (
- error instanceof SaveRefusedError ||
- error instanceof SaveStaleError ||
- error instanceof SaveFailedError
- ) {
- return;
- }
- console.error("Failed to save workflow:", error);
- });
+ if (pendingGoalChangeCount > 0) {
+ setGoalDialogOpen(true);
+ return;
+ }
+ save();
}}
>
{saving ? (
@@ -140,6 +153,11 @@ function SaveButton() {
</TooltipTrigger>
<TooltipContent>Save</TooltipContent>
</Tooltip>
+ <PendingGoalChangesDialog
+ open={goalDialogOpen}
+ onOpenChange={setGoalDialogOpen}
+ onSave={save}
+ />
</TooltipProvider>
);
}
diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
index 60fa04c69..9df07d866 100644
--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
@@ -195,6 +195,7 @@ import {
getWorkflowBlocks,
getWorkflowErrors,
upgradeWorkflowDefinitionToVersionTwo,
+ withGoalUndoRecordsFrom,
} from "./workflowEditorUtils";
import { replayPersistedCollapseVisibility } from "./collapse/applyDescendantCollapseVisibility";
import { useNodeCollapseStore } from "./collapse/useNodeCollapseStore";
@@ -2056,8 +2057,11 @@ function Workspace({
[workflowData.workflow_permanent_id]:
workflowData.workflow_definition.parameters,
}));
- updateNodes(
- replayPersistedCollapseVisibility(elements.nodes, wpid, collapsedSet),
+ updateNodes((current) =>
+ withGoalUndoRecordsFrom(
+ current,
+ replayPersistedCollapseVisibility(elements.nodes, wpid, collapsedSet),
+ ),
);
updateEdges(elements.edges);
useWorkflowParametersStore
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockEditor.test.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockEditor.test.tsx
index 373095fdc..9a6449ae3 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockEditor.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockEditor.test.tsx
@@ -41,6 +41,8 @@ const baseData: CodeBlockNodeData = {
prompt: null,
steps: null,
dataSchema: "null",
+ userOwnedGoal: null,
+ goalNeedsRegeneration: null,
model: null,
};
@@ -108,13 +110,16 @@ vi.mock("@/components/WorkflowBlockInputTextarea", () => ({
WorkflowBlockInputTextarea: ({
value,
onChange,
+ disabled,
}: {
value?: string;
onChange: (value: string) => void;
+ disabled?: boolean;
}) => (
<textarea
data-testid="block-input-textarea"
value={value ?? ""}
+ disabled={disabled}
onChange={(event) => onChange(event.target.value)}
/>
),
@@ -124,14 +129,20 @@ vi.mock("@/routes/workflows/components/CodeEditor", () => ({
CodeEditor: ({
readOnly,
extraExtensions,
+ value,
+ onChange,
}: {
readOnly?: boolean;
extraExtensions?: Array<unknown>;
+ value?: string;
+ onChange?: (value: string) => void;
}) => (
- <div
+ <textarea
data-testid="code-editor"
data-readonly={String(Boolean(readOnly))}
data-extension-count={String(extraExtensions?.length ?? 0)}
+ value={value ?? ""}
+ onChange={(event) => onChange?.(event.target.value)}
/>
),
}));
@@ -147,6 +158,13 @@ beforeEach(() => {
});
afterEach(cleanup);
+afterEach(() => {
+ useCopilotActionStore.setState({
+ pendingBuild: null,
+ generatingBlockLabel: null,
+ queuedBuilds: [],
+ });
+});
describe("save-time error code mapping validation", () => {
const createCodeBlock = (
@@ -532,7 +550,20 @@ describe("CodeBlockEditor for a code-first block", () => {
]);
});
- test("persists goal edits to the node data", () => {
+ const pendingGoalData = {
+ ...baseData,
+ ...codeFirstData,
+ prompt: "Open {{ link }}",
+ userOwnedGoal: true,
+ goalNeedsRegeneration: true,
+ goalBeforeEdit: {
+ prompt: "Open {{ url }}",
+ userOwnedGoal: null,
+ goalNeedsRegeneration: null,
+ },
+ };
+
+ test("editing the goal records the goal it replaced and starts nothing", () => {
node.data = { ...baseData, ...codeFirstData };
renderEditor();
@@ -541,6 +572,206 @@ describe("CodeBlockEditor for a code-first block", () => {
expect(updateNodeData).toHaveBeenCalledWith("cb1", {
prompt: "Open {{ link }}",
+ userOwnedGoal: true,
+ goalNeedsRegeneration: true,
+ goalBeforeEdit: {
+ prompt: "Open {{ url }}",
+ userOwnedGoal: null,
+ goalNeedsRegeneration: null,
+ },
+ });
+ expect(useCopilotActionStore.getState().pendingBuild).toBeNull();
+ });
+
+ test("a further edit keeps the goal the first edit replaced", () => {
+ node.data = pendingGoalData;
+ renderEditor();
+
+ const [goalTextarea] = screen.getAllByTestId("block-input-textarea");
+ fireEvent.change(goalTextarea!, { target: { value: "Open {{ other }}" } });
+
+ expect(updateNodeData).toHaveBeenCalledWith("cb1", {
+ prompt: "Open {{ other }}",
+ userOwnedGoal: true,
+ goalNeedsRegeneration: true,
+ });
+ });
+
+ test("typing the old goal back undoes the change", () => {
+ node.data = pendingGoalData;
+ renderEditor();
+
+ const [goalTextarea] = screen.getAllByTestId("block-input-textarea");
+ fireEvent.change(goalTextarea!, { target: { value: "Open {{ url }}" } });
+
+ expect(updateNodeData).toHaveBeenCalledWith("cb1", {
+ prompt: "Open {{ url }}",
+ userOwnedGoal: null,
+ goalNeedsRegeneration: null,
+ goalBeforeEdit: null,
+ });
+ });
+
+ test("a pending goal change shows the banner, and Apply asks the copilot to follow it", () => {
+ node.data = pendingGoalData;
+ renderEditor();
+
+ expect(screen.getByTestId("goal-change-banner").textContent).toContain(
+ "Goal changed — not applied yet",
+ );
+ expect(
+ screen.queryByRole("button", { name: "Regenerate block" }),
+ ).toBeNull();
+
+ fireEvent.click(screen.getByRole("button", { name: "Apply new Goal" }));
+
+ expect(useCopilotActionStore.getState().pendingBuild).toEqual({
+ blockLabel: "code_block",
+ prompt: "Open {{ link }}",
+ applyingGoalChange: true,
+ });
+ });
+
+ test("Undo restores the goal the change replaced", () => {
+ node.data = pendingGoalData;
+ renderEditor();
+
+ fireEvent.click(screen.getByRole("button", { name: "Undo" }));
+
+ expect(updateNodeData).toHaveBeenCalledWith("cb1", {
+ prompt: "Open {{ url }}",
+ userOwnedGoal: null,
+ goalNeedsRegeneration: null,
+ goalBeforeEdit: null,
+ });
+ });
+
+ test("a pending change with no record of the old goal offers Apply but no Undo", () => {
+ node.data = { ...pendingGoalData, goalBeforeEdit: undefined };
+ renderEditor();
+
+ expect(screen.getByRole("button", { name: "Apply new Goal" })).toBeTruthy();
+ expect(screen.queryByRole("button", { name: "Undo" })).toBeNull();
+ });
+
+ test("Apply while another block is generating waits its turn", () => {
+ node.data = pendingGoalData;
+ useCopilotActionStore.setState({ generatingBlockLabel: "other" });
+ renderEditor();
+
+ fireEvent.click(screen.getByRole("button", { name: "Apply new Goal" }));
+
+ expect(useCopilotActionStore.getState().pendingBuild).toBeNull();
+ expect(screen.getByTestId("goal-change-banner").textContent).toContain(
+ "Waiting to apply the new Goal",
+ );
+
+ act(() => {
+ useCopilotActionStore.getState().finishGenerating();
+ });
+
+ expect(useCopilotActionStore.getState().pendingBuild).toEqual({
+ blockLabel: "code_block",
+ prompt: "Open {{ link }}",
+ applyingGoalChange: true,
+ });
+ });
+
+ test.each([
+ ["applying", { generatingBlockLabel: "code_block" }],
+ [
+ "waiting to apply",
+ {
+ generatingBlockLabel: "other",
+ queuedBuilds: [{ blockLabel: "code_block", prompt: "Open {{ link }}" }],
+ },
+ ],
+ ])("the goal cannot be edited while the block is %s", (_, state) => {
+ node.data = pendingGoalData;
+ useCopilotActionStore.setState(state);
+ renderEditor();
+
+ const [goalTextarea] = screen.getAllByTestId("block-input-textarea");
+ expect((goalTextarea as HTMLTextAreaElement).disabled).toBe(true);
+ });
+
+ test("writes nothing when the goal text did not change", () => {
+ node.data = { ...baseData, ...codeFirstData };
+ renderEditor();
+
+ const [goalTextarea] = screen.getAllByTestId("block-input-textarea");
+ fireEvent.change(goalTextarea!, { target: { value: "Open {{ url }}" } });
+
+ expect(updateNodeData).not.toHaveBeenCalled();
+ });
+
+ test("clearing the goal hands ownership back", () => {
+ node.data = pendingGoalData;
+ renderEditor();
+
+ const [goalTextarea] = screen.getAllByTestId("block-input-textarea");
+ fireEvent.change(goalTextarea!, { target: { value: " " } });
+
+ expect(updateNodeData).toHaveBeenCalledWith("cb1", {
+ prompt: " ",
+ userOwnedGoal: false,
+ goalNeedsRegeneration: false,
+ goalBeforeEdit: null,
+ });
+ });
+
+ test("clearing a goal the person never owned writes no ownership fields", () => {
+ node.data = { ...baseData, ...codeFirstData };
+ renderEditor();
+
+ const [goalTextarea] = screen.getAllByTestId("block-input-textarea");
+ fireEvent.change(goalTextarea!, { target: { value: "" } });
+
+ expect(updateNodeData).toHaveBeenCalledWith("cb1", { prompt: "" });
+ });
+
+ test("with no goal change pending there is no banner and the build button is back", () => {
+ node.data = {
+ ...baseData,
+ ...codeFirstData,
+ userOwnedGoal: true,
+ goalNeedsRegeneration: false,
+ };
+ renderEditor();
+
+ expect(screen.queryByTestId("goal-change-banner")).toBeNull();
+ expect(
+ screen.getByRole("button", { name: "Regenerate block" }),
+ ).toBeTruthy();
+ });
+
+ test("editing the code clears the pending goal change", () => {
+ node.data = pendingGoalData;
+ renderEditor();
+ switchToCode();
+
+ fireEvent.change(screen.getByTestId("code-editor"), {
+ target: { value: "await page.goto(url)" },
+ });
+
+ expect(updateNodeData).toHaveBeenCalledWith("cb1", {
+ code: "await page.goto(url)",
+ goalNeedsRegeneration: false,
+ goalBeforeEdit: null,
+ });
+ });
+
+ test("editing the code of a block with no pending change writes only the code", () => {
+ node.data = { ...baseData, ...codeFirstData };
+ renderEditor();
+ switchToCode();
+
+ fireEvent.change(screen.getByTestId("code-editor"), {
+ target: { value: "await page.goto(url)" },
+ });
+
+ expect(updateNodeData).toHaveBeenCalledWith("cb1", {
+ code: "await page.goto(url)",
});
});
@@ -780,3 +1011,28 @@ describe("CodeBlockEditor generate gating", () => {
expect(button.disabled).toBe(true);
});
});
+
+describe("code block goal ownership serialization", () => {
+ test("round-trips the Goal ownership fields through the saved workflow blocks", () => {
+ const [saved] = getWorkflowBlocks(
+ [
+ {
+ id: "code-1",
+ type: "codeBlock" as const,
+ position: { x: 0, y: 0 },
+ data: {
+ ...baseData,
+ userOwnedGoal: true,
+ goalNeedsRegeneration: true,
+ },
+ },
+ ],
+ [],
+ );
+
+ expect(saved).toMatchObject({
+ user_owned_goal: true,
+ goal_needs_regeneration: true,
+ });
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockEditor.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockEditor.tsx
index 9e79a88c2..7874a29e0 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockEditor.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/CodeBlockEditor.tsx
@@ -15,6 +15,10 @@ import { ErrorCodeMappingEditor } from "@/routes/workflows/editor/ErrorCodeMappi
import { pythonSyntaxExtensions } from "@/routes/workflows/editor/pythonSyntaxLinter";
import { useWorkflowScopeReadOnly } from "@/routes/workflows/editor/WorkflowScopeContext";
import type { CodeBlockStep } from "@/routes/workflows/types/workflowTypes";
+import {
+ goalChangeIsPending,
+ goalChangeUndoPatch,
+} from "@/routes/workflows/editor/workflowEditorUtils";
import { getCodeStepPlainText } from "@/routes/workflows/workflowBlockUtils";
import { useCopilotActionStore } from "@/store/useCopilotActionStore";
import {
@@ -116,6 +120,12 @@ function CodeBlockEditorBody({
!scopeReadOnly &&
!mutationLocked;
const hasGenerated = steps.length > 0;
+ const isQueued = useCopilotActionStore((state) =>
+ state.queuedBuilds.some((queued) => queued.blockLabel === data.label),
+ );
+ const goalChangePending = goalChangeIsPending(data);
+ const canEditGoalChange = editable && !scopeReadOnly && !mutationLocked;
+ const undoPatch = goalChangeUndoPatch(data);
const workflowStartNode = nodes
.filter(isStartNode)
.find((candidate) => "errorCodeMapping" in candidate.data);
@@ -153,33 +163,71 @@ function CodeBlockEditorBody({
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label className="text-xs text-tertiary-foreground">Goal</Label>
- <button
- type="button"
- disabled={!canGenerate}
- aria-label={hasGenerated ? "Regenerate block" : "Generate block"}
- onClick={() =>
- requestBuild({ blockLabel: data.label, prompt: data.prompt ?? "" })
- }
- className={cn(
- "nodrag nopan flex items-center gap-1 rounded-md border border-border bg-slate-elevation1 px-2 py-0.5 text-xs text-foreground dark:text-slate-200",
- canGenerate
- ? "hover:bg-slate-elevation2"
- : "cursor-not-allowed opacity-50",
- )}
- >
- <MagicWandIcon className="size-3" />
- {isGenerating
- ? "Generating…"
- : hasGenerated
- ? "Regenerate"
- : "Generate"}
- </button>
+ {goalChangePending ? null : (
+ <button
+ type="button"
+ disabled={!canGenerate}
+ aria-label={hasGenerated ? "Regenerate block" : "Generate block"}
+ onClick={() =>
+ requestBuild({
+ blockLabel: data.label,
+ prompt: data.prompt ?? "",
+ })
+ }
+ className={cn(
+ "nodrag nopan flex items-center gap-1 rounded-md border border-border bg-slate-elevation1 px-2 py-0.5 text-xs text-foreground dark:text-slate-200",
+ canGenerate
+ ? "hover:bg-slate-elevation2"
+ : "cursor-not-allowed opacity-50",
+ )}
+ >
+ <MagicWandIcon className="size-3" />
+ {isGenerating
+ ? "Generating…"
+ : hasGenerated
+ ? "Regenerate"
+ : "Generate"}
+ </button>
+ )}
</div>
<WorkflowBlockInputTextarea
name="prompt"
nodeId={blockId}
- onChange={(value) => update({ prompt: value })}
+ onChange={(value) => {
+ if (value === (data.prompt ?? "")) {
+ return;
+ }
+ if (undoPatch && value === (data.goalBeforeEdit?.prompt ?? "")) {
+ update(undoPatch);
+ return;
+ }
+ if (value.trim().length > 0) {
+ update({
+ prompt: value,
+ userOwnedGoal: true,
+ goalNeedsRegeneration: true,
+ ...(!goalChangePending && {
+ goalBeforeEdit: {
+ prompt: data.prompt,
+ userOwnedGoal: data.userOwnedGoal,
+ goalNeedsRegeneration: data.goalNeedsRegeneration,
+ },
+ }),
+ });
+ return;
+ }
+ update({
+ prompt: value,
+ ...(data.userOwnedGoal === true && { userOwnedGoal: false }),
+ ...(goalChangePending && {
+ goalNeedsRegeneration: false,
+ goalBeforeEdit: null,
+ }),
+ });
+ }}
value={data.prompt ?? ""}
+ // The build in flight carries the Goal it was started with; a later edit would be lost to it.
+ disabled={isGenerating || isQueued}
className="nopan text-xs"
/>
</div>
@@ -209,7 +257,16 @@ function CodeBlockEditorBody({
value={data.code}
readOnly={scopeReadOnly}
onChange={(value) => {
- update({ code: value });
+ if (value === data.code) {
+ return;
+ }
+ update({
+ code: value,
+ ...(goalChangePending && {
+ goalNeedsRegeneration: false,
+ goalBeforeEdit: null,
+ }),
+ });
}}
className="nopan"
fontSize={10}
@@ -396,26 +453,86 @@ function CodeBlockEditorBody({
</div>
) : null;
+ const goalChangeBanner = goalChangePending ? (
+ <div
+ role="status"
+ data-testid="goal-change-banner"
+ className="space-y-2 rounded-md border border-amber-500/40 bg-amber-500/10 p-2 text-xs"
+ >
+ <p className="font-medium text-foreground">
+ {isGenerating
+ ? "Applying the new Goal…"
+ : isQueued
+ ? "Waiting to apply the new Goal…"
+ : "Goal changed — not applied yet"}
+ </p>
+ {isGenerating || isQueued ? null : (
+ <>
+ <p className="text-muted-foreground">
+ This block still does what its old Goal said.
+ </p>
+ <div className="flex items-center gap-2">
+ <button
+ type="button"
+ disabled={!canEditGoalChange}
+ onClick={() =>
+ requestBuild({
+ blockLabel: data.label,
+ prompt: data.prompt ?? "",
+ applyingGoalChange: true,
+ })
+ }
+ className="nodrag nopan rounded-md bg-primary px-2 py-0.5 text-xs text-primary-foreground hover:bg-primary/90 disabled:cursor-not-allowed disabled:opacity-50"
+ >
+ Apply new Goal
+ </button>
+ {undoPatch ? (
+ <button
+ type="button"
+ disabled={!canEditGoalChange}
+ onClick={() => update(undoPatch)}
+ className="nodrag nopan rounded-md border border-border bg-slate-elevation1 px-2 py-0.5 text-xs text-foreground hover:bg-slate-elevation2 disabled:cursor-not-allowed disabled:opacity-50 dark:text-slate-200"
+ >
+ Undo
+ </button>
+ ) : null}
+ </div>
+ </>
+ )}
+ </div>
+ ) : null;
+
return (
<div data-testid="code-block-block-form" className="space-y-4">
<div className="flex items-center justify-between gap-2">
<Label className="text-xs text-tertiary-foreground">View</Label>
<CodeBlockViewToggle value={view} onChange={setView} />
</div>
+ {goalChangeBanner}
{view === "plain" ? (
<>
{goalField}
{dataSchemaField}
- <CodeBlockPlainCard
- steps={steps}
- generating={isGenerating}
- onStop={requestCancel}
- />
+ <div
+ className={cn(goalChangePending && !isGenerating && "opacity-50")}
+ >
+ <CodeBlockPlainCard
+ steps={steps}
+ generating={isGenerating}
+ onStop={requestCancel}
+ />
+ </div>
{errorCodeMappingField}
</>
) : (
<>
- {stepLineList}
+ {stepLineList && (
+ <div
+ className={cn(goalChangePending && !isGenerating && "opacity-50")}
+ >
+ {stepLineList}
+ </div>
+ )}
{inputsField}
<div className="space-y-2">
<Label className="text-xs text-tertiary-foreground">
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/types.ts b/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/types.ts
index 6074ff941..8fd2c4fd5 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/types.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/CodeBlockNode/types.ts
@@ -12,6 +12,16 @@ export type CodeBlockNodeData = NodeBaseData & {
prompt: string | null;
steps: Array<CodeBlockStep> | null;
dataSchema: string;
+ userOwnedGoal: boolean | null;
+ goalNeedsRegeneration: boolean | null;
+ // Editor-only: the Goal as it was before the pending change, so Undo can restore it. Never saved.
+ goalBeforeEdit?: GoalBeforeEdit | null;
+};
+
+export type GoalBeforeEdit = {
+ prompt: string | null;
+ userOwnedGoal: boolean | null;
+ goalNeedsRegeneration: boolean | null;
};
export type CodeBlockNode = Node<CodeBlockNodeData, "codeBlock">;
@@ -38,5 +48,7 @@ export const codeBlockNodeDefaultData: CodeBlockNodeData = {
prompt: "",
steps: null,
dataSchema: "null",
+ userOwnedGoal: null,
+ goalNeedsRegeneration: null,
model: null,
} as const;
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 8197cc324..38854847d 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx
@@ -108,7 +108,7 @@ import { MicroDropdown } from "./MicroDropdown";
import { BlockParametersDialog } from "./BlockParametersDialog";
import type { AppNode } from "..";
import {
- getWorkflowErrors,
+ blockRunErrors,
isMissingRequiredStartUrl,
} from "../../workflowEditorUtils";
import { getNodeBrowserUrlError } from "../../browserBlockUrl";
@@ -412,9 +412,7 @@ function NodeHeader({
// one. Filter relies on the implicit contract that every validator
// formats errors as `${label}: ${message}` - if that ever drifts,
// errors for this block would silently slip past this gate.
- const allErrors = getWorkflowErrors(reactFlow.getNodes());
- const labelPrefix = `${blockLabel}:`;
- const blockErrors = allErrors.filter((e) => e.startsWith(labelPrefix));
+ const blockErrors = blockRunErrors(reactFlow.getNodes(), blockLabel);
if (
isMissingRequiredStartUrl(
reactFlow.getNodes(),
diff --git a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.test.ts b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.test.ts
new file mode 100644
index 000000000..75d6503b2
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.test.ts
@@ -0,0 +1,118 @@
+import { describe, expect, test } from "vitest";
+
+import type { AppNode } from "./nodes";
+import {
+ blockRunErrors,
+ getWorkflowErrors,
+ pendingGoalChangesOf,
+ withGoalUndoRecordsFrom,
+} from "./workflowEditorUtils";
+
+function codeBlock(goalNeedsRegeneration: boolean | null): AppNode {
+ return {
+ id: "cb1",
+ type: "codeBlock",
+ position: { x: 0, y: 0 },
+ data: {
+ debuggable: true,
+ editable: true,
+ label: "lookup_invoice",
+ code: "print(1)",
+ continueOnFailure: false,
+ parameterKeys: [],
+ errorCodeMapping: "null",
+ prompt: "Download last month's invoice",
+ steps: null,
+ dataSchema: "null",
+ userOwnedGoal: true,
+ goalNeedsRegeneration,
+ model: null,
+ },
+ } as AppNode;
+}
+
+describe("getWorkflowErrors", () => {
+ test("refuses to save a code block whose edited Goal has not been rebuilt into code", () => {
+ const errors = getWorkflowErrors([codeBlock(true)]);
+
+ expect(errors).toHaveLength(1);
+ expect(errors[0]).toContain("lookup_invoice");
+ });
+
+ test("a rebuild flag on a Goal no person owns blocks nothing, as on the backend", () => {
+ const apiAuthored = {
+ ...codeBlock(true),
+ data: { ...codeBlock(true).data, userOwnedGoal: null },
+ } as AppNode;
+
+ expect(getWorkflowErrors([apiAuthored])).toEqual([]);
+ });
+
+ test("saves once the code has been rebuilt from the Goal", () => {
+ expect(getWorkflowErrors([codeBlock(false)])).toEqual([]);
+ expect(getWorkflowErrors([codeBlock(null)])).toEqual([]);
+ });
+});
+
+describe("withGoalUndoRecordsFrom", () => {
+ const edited = {
+ ...codeBlock(true),
+ data: {
+ ...codeBlock(true).data,
+ goalBeforeEdit: {
+ prompt: "Download the latest statement",
+ userOwnedGoal: null,
+ goalNeedsRegeneration: null,
+ },
+ },
+ } as AppNode;
+
+ test("a draft that keeps the unapplied Goal keeps its Undo", () => {
+ const rebuilt = withGoalUndoRecordsFrom([edited], [codeBlock(true)]);
+
+ expect(pendingGoalChangesOf(rebuilt)).toEqual([
+ {
+ label: "lookup_invoice",
+ goal: "Download last month's invoice",
+ previousGoal: "Download the latest statement",
+ },
+ ]);
+ });
+
+ test("a draft that applied the Goal, or changed its text, drops the Undo", () => {
+ const applied = withGoalUndoRecordsFrom([edited], [codeBlock(false)]);
+ const retyped = withGoalUndoRecordsFrom(
+ [edited],
+ [
+ {
+ ...codeBlock(true),
+ data: { ...codeBlock(true).data, prompt: "Something else" },
+ } as AppNode,
+ ],
+ );
+
+ expect(pendingGoalChangesOf(applied)).toEqual([]);
+ expect(pendingGoalChangesOf(retyped)[0]?.previousGoal).toBeNull();
+ });
+});
+
+describe("blockRunErrors", () => {
+ const otherBlock = {
+ ...codeBlock(null),
+ id: "cb2",
+ data: { ...codeBlock(null).data, label: "send_report" },
+ } as AppNode;
+
+ test("a pending Goal on another block stops a single-block run", () => {
+ const errors = blockRunErrors([codeBlock(true), otherBlock], "send_report");
+
+ expect(errors).toHaveLength(1);
+ expect(errors[0]).toContain("lookup_invoice");
+ });
+
+ test("with no pending Goal, only the run block's own errors stop it", () => {
+ expect(
+ blockRunErrors([codeBlock(false), otherBlock], "send_report"),
+ ).toEqual([]);
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts
index e1fad8796..9d68a408f 100644
--- a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts
@@ -87,7 +87,11 @@ import {
import { EMAIL_BLOCK_SENDER, REACT_FLOW_EDGE_Z_INDEX } from "./constants";
import { ParametersState } from "./types";
import { AppNode, isWorkflowBlockNode, WorkflowBlockNode } from "./nodes";
-import { codeBlockNodeDefaultData } from "./nodes/CodeBlockNode/types";
+import {
+ codeBlockNodeDefaultData,
+ type CodeBlockNodeData,
+} from "./nodes/CodeBlockNode/types";
+import type { PendingGoalChange } from "@/store/useCopilotActionStore";
import { dataExportNodeDefaultData } from "./nodes/DataExportNode/types";
import { downloadNodeDefaultData } from "./nodes/DownloadNode/types";
import {
@@ -1027,6 +1031,8 @@ function convertToNode(
: typeof block.data_schema === "string"
? block.data_schema
: JSON.stringify(block.data_schema, null, 2),
+ userOwnedGoal: block.user_owned_goal ?? null,
+ goalNeedsRegeneration: block.goal_needs_regeneration ?? null,
},
};
}
@@ -3438,6 +3444,8 @@ function getWorkflowBlock(
prompt: node.data.prompt,
steps: node.data.steps,
data_schema: JSONSafeOrStringAllowArrays(node.data.dataSchema),
+ user_owned_goal: node.data.userOwnedGoal,
+ goal_needs_regeneration: node.data.goalNeedsRegeneration,
};
}
case "dataExport": {
@@ -4983,6 +4991,8 @@ function convertBlocksToBlockYAML(
prompt: block.prompt,
steps: block.steps,
data_schema: block.data_schema,
+ user_owned_goal: block.user_owned_goal,
+ goal_needs_regeneration: block.goal_needs_regeneration,
};
return blockYaml;
}
@@ -5294,8 +5304,99 @@ function convert(workflow: WorkflowApiResponse): WorkflowCreateYAMLRequest {
};
}
+// The backend counts the flag only on a Goal a person owns, so the editor must too.
+function goalChangeIsPending(data: CodeBlockNodeData): boolean {
+ return data.userOwnedGoal === true && data.goalNeedsRegeneration === true;
+}
+
+function pendingGoalChangesOf(nodes: Array<AppNode>): Array<PendingGoalChange> {
+ const changes: Array<PendingGoalChange> = [];
+ for (const node of nodes) {
+ if (
+ isWorkflowBlockNode(node) &&
+ node.type === "codeBlock" &&
+ goalChangeIsPending(node.data)
+ ) {
+ changes.push({
+ label: node.data.label,
+ goal: node.data.prompt ?? "",
+ previousGoal: node.data.goalBeforeEdit
+ ? (node.data.goalBeforeEdit.prompt ?? "")
+ : null,
+ });
+ }
+ }
+ return changes;
+}
+
+function goalChangeUndoPatch(
+ data: CodeBlockNodeData,
+): Partial<CodeBlockNodeData> | null {
+ if (!goalChangeIsPending(data) || !data.goalBeforeEdit) {
+ return null;
+ }
+ return { ...data.goalBeforeEdit, goalBeforeEdit: null };
+}
+
+// The undo record is editor-only, so a graph rebuilt from saved form drops it; keep it on a block
+// that still holds the same unapplied Goal.
+function withGoalUndoRecordsFrom(
+ previous: Array<AppNode>,
+ next: Array<AppNode>,
+): Array<AppNode> {
+ const records = new Map<string, CodeBlockNodeData>();
+ for (const node of previous) {
+ if (
+ isWorkflowBlockNode(node) &&
+ node.type === "codeBlock" &&
+ node.data.goalBeforeEdit
+ ) {
+ records.set(node.data.label, node.data);
+ }
+ }
+ if (records.size === 0) {
+ return next;
+ }
+ return next.map((node) => {
+ if (!isWorkflowBlockNode(node) || node.type !== "codeBlock") {
+ return node;
+ }
+ const before = records.get(node.data.label);
+ if (
+ !before ||
+ !goalChangeIsPending(node.data) ||
+ node.data.prompt !== before.prompt
+ ) {
+ return node;
+ }
+ return {
+ ...node,
+ data: { ...node.data, goalBeforeEdit: before.goalBeforeEdit },
+ };
+ });
+}
+
+function pendingGoalErrors(nodes: Array<AppNode>): Array<string> {
+ return pendingGoalChangesOf(nodes).map(
+ ({ label }) =>
+ `${label}: its new Goal isn't applied yet. Apply it or undo the change before saving.`,
+ );
+}
+
+// A block run saves the whole workflow first, so a pending Goal on any block stops it; every other
+// error stops only the block it names.
+function blockRunErrors(
+ nodes: Array<AppNode>,
+ blockLabel: string,
+): Array<string> {
+ const pendingGoals = new Set(pendingGoalErrors(nodes));
+ return getWorkflowErrors(nodes).filter(
+ (error) => error.startsWith(`${blockLabel}:`) || pendingGoals.has(error),
+ );
+}
+
function getWorkflowErrors(nodes: Array<AppNode>): Array<string> {
- const errors: Array<string> = [];
+ const errors: Array<string> = [...pendingGoalErrors(nodes)];
const workflowBlockNodes = nodes.filter(isWorkflowBlockNode);
if (
@@ -5744,6 +5845,11 @@ export {
getUpdatedNodesAfterLabelUpdateForParameterKeys,
getUpdatedParametersAfterLabelUpdateForSourceParameterKey,
getWorkflowBlocks,
+ goalChangeIsPending,
+ goalChangeUndoPatch,
+ blockRunErrors,
+ withGoalUndoRecordsFrom,
+ pendingGoalChangesOf,
getWorkflowErrors,
isNodeInsideForLoop,
getParentLoopSkipsOnFail,
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.test.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.test.tsx
index de4fb292f..f40da6991 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.test.tsx
@@ -23,6 +23,7 @@ import {
import { ProxyLocation, Status } from "@/api/types";
import { TooltipProvider } from "@/components/ui/tooltip";
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
import {
clearDeferredEdits,
deferredEdits,
@@ -543,6 +544,97 @@ describe("SaveButton confirmation gating", () => {
});
});
+describe("SaveButton with a new Goal that isn't applied yet", () => {
+ const clean = () => saveData([block("a", { url: "x" })]);
+
+ beforeEach(() => {
+ const data = clean();
+ useWorkflowHasChangesStore.setState({
+ getSaveData: () => data,
+ saveIsPending: false,
+ });
+ useWorkflowSnapshotStore.setState({
+ snapshot: snapshotOf(data),
+ contentDirty: false,
+ userHasEdited: false,
+ });
+ });
+
+ afterEach(() => {
+ useWorkflowSnapshotStore.getState().clearSnapshot();
+ useWorkflowHasChangesStore.setState({
+ getSaveData: () => null,
+ saveIsPending: false,
+ });
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [],
+ pendingBuild: null,
+ generatingBlockLabel: null,
+ queuedBuilds: [],
+ undoGoalChange: () => {},
+ });
+ });
+
+ test("asks instead of saving, and Apply asks the copilot to follow the new Goal", () => {
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [{ label: "a", goal: "New", previousGoal: "Old" }],
+ });
+
+ renderSaveButton();
+ fireEvent.click(screen.getByRole("button", { name: "Save workflow" }));
+
+ expect(screen.getByText("New Goal not applied")).toBeTruthy();
+ expect(saveWorkflowSpy).not.toHaveBeenCalled();
+
+ fireEvent.click(screen.getByRole("button", { name: "Apply new Goal" }));
+
+ expect(useCopilotActionStore.getState().pendingBuild).toEqual({
+ blockLabel: "a",
+ prompt: "New",
+ applyingGoalChange: true,
+ });
+ expect(saveWorkflowSpy).not.toHaveBeenCalled();
+ });
+
+ test("Undo and save saves the draft as the undo left it", () => {
+ const afterUndo = useWorkflowHasChangesStore.getState().getSaveData;
+ const edited = saveData([block("a", { url: "edited" })]);
+ useWorkflowHasChangesStore.setState({ getSaveData: () => edited });
+ const undoGoalChange = vi.fn(() => {
+ useWorkflowHasChangesStore.setState({ getSaveData: afterUndo });
+ useCopilotActionStore.getState().setPendingGoalChanges([]);
+ });
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [{ label: "a", goal: "New", previousGoal: "Old" }],
+ undoGoalChange,
+ });
+
+ renderSaveButton();
+ fireEvent.click(screen.getByRole("button", { name: "Save workflow" }));
+ fireEvent.click(
+ screen.getByRole("button", { name: "Undo Goal change and save" }),
+ );
+
+ expect(undoGoalChange).toHaveBeenCalledWith("a");
+ expect(saveWorkflowSpy).toHaveBeenCalledTimes(1);
+ expect(screen.queryByText("Saving Changes")).toBeNull();
+ });
+
+ test("offers no undo when the old Goal is unknown", () => {
+ useCopilotActionStore.setState({
+ pendingGoalChanges: [{ label: "a", goal: "New", previousGoal: null }],
+ });
+
+ renderSaveButton();
+ fireEvent.click(screen.getByRole("button", { name: "Save workflow" }));
+
+ expect(screen.getByRole("button", { name: "Apply new Goal" })).toBeTruthy();
+ expect(
+ screen.queryByRole("button", { name: "Undo Goal change and save" }),
+ ).toBeNull();
+ });
+});
+
describe("TitleSection title link + edit affordance", () => {
beforeEach(() => {
clearDeferredEdits();
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx
index a9b9cfc7c..24baaf6df 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx
@@ -57,6 +57,8 @@ import {
summarizeWorkflowChanges,
} from "../editor/workflowChangesSummary";
import { useSaveWorkflow } from "../editor/hooks/useSaveWorkflow";
+import { PendingGoalChangesDialog } from "../editor/PendingGoalChangesDialog";
+import { useCopilotActionStore } from "@/store/useCopilotActionStore";
import { useToggleHistoryPanel } from "../editor/hooks/useToggleHistoryPanel";
import { useDeferredTitleEdit } from "../hooks/useDeferredTitleEdit";
import { useIsGlobalWorkflow } from "../hooks/useIsGlobalWorkflow";
@@ -133,6 +135,42 @@ export function SaveButton() {
const isRecording = useRecordingStore((s) => s.isRecording);
const onSave = useSaveWorkflow();
const [confirmOpen, setConfirmOpen] = useState(false);
+ const pendingGoalChangeCount = useCopilotActionStore(
+ (state) => state.pendingGoalChanges.length,
+ );
+ const [goalDialogOpen, setGoalDialogOpen] = useState(false);
+ const saveOrConfirm = () => {
+ // Recompute dirtiness synchronously from the same source as the
+ // summary (incl. the YAML draft). contentDirty is debounced and
+ // canvas-only, so gating the confirmation on it would skip it for a
+ // YAML edit or an edit-then-save inside the debounce window.
+ // Read the stores, not this render's values: the Goal dialog undoes and saves in one click.
+ let dirty = false;
+ try {
+ const saveData = useWorkflowHasChangesStore.getState().getSaveData();
+ dirty = saveData
+ ? isDraftDirty(saveData, useWorkflowSnapshotStore.getState().snapshot)
+ : false;
+ } catch (error) {
+ console.error("Failed to check workflow changes", error);
+ }
+ // onSave rejects on a failed save (already toasted by its onError);
+ // swallow so it isn't an unhandled rejection.
+ // A hold means the baseline itself may be stale, so even a draft that matches it
+ // goes through the confirmation rather than saving straight over the newer change.
+ if (dirty || saveBlockedReason) {
+ setConfirmOpen(true);
+ } else {
+ void onSave().catch((error: unknown) => {
+ if (
+ error instanceof SaveRefusedError ||
+ error instanceof SaveStaleError
+ ) {
+ setConfirmOpen(false);
+ }
+ });
+ }
+ };
// Compute once when the confirm dialog opens; the canvas is behind the modal
// and can't be edited while it's up, so the summary stays valid.
@@ -167,33 +205,11 @@ export function SaveButton() {
className="relative h-8 w-8 text-muted-foreground"
disabled={isRecording}
onClick={() => {
- // Recompute dirtiness synchronously from the same source as the
- // summary (incl. the YAML draft). contentDirty is debounced and
- // canvas-only, so gating the confirmation on it would skip it for a
- // YAML edit or an edit-then-save inside the debounce window.
- let dirty = false;
- try {
- const saveData = getSaveData();
- dirty = saveData ? isDraftDirty(saveData, snapshot) : false;
- } catch (error) {
- console.error("Failed to check workflow changes", error);
- }
- // onSave rejects on a failed save (already toasted by its onError);
- // swallow so it isn't an unhandled rejection.
- // A hold means the baseline itself may be stale, so even a draft that matches it
- // goes through the confirmation rather than saving straight over the newer change.
- if (dirty || saveBlockedReason) {
- setConfirmOpen(true);
- } else {
- void onSave().catch((error: unknown) => {
- if (
- error instanceof SaveRefusedError ||
- error instanceof SaveStaleError
- ) {
- setConfirmOpen(false);
- }
- });
+ if (pendingGoalChangeCount > 0) {
+ setGoalDialogOpen(true);
+ return;
}
+ saveOrConfirm();
}}
aria-label={
saveBlockedReason
@@ -216,6 +232,11 @@ export function SaveButton() {
)}
</Button>
</ControlTooltip>
+ <PendingGoalChangesDialog
+ open={goalDialogOpen}
+ onOpenChange={setGoalDialogOpen}
+ onSave={saveOrConfirm}
+ />
<Dialog open={confirmOpen} onOpenChange={setConfirmOpen}>
<DialogContent>
<DialogHeader>
diff --git a/skyvern-frontend/src/routes/workflows/types/workflowTypes.ts b/skyvern-frontend/src/routes/workflows/types/workflowTypes.ts
index ef844ee7f..3367cadd2 100644
--- a/skyvern-frontend/src/routes/workflows/types/workflowTypes.ts
+++ b/skyvern-frontend/src/routes/workflows/types/workflowTypes.ts
@@ -418,6 +418,8 @@ export type CodeBlock = WorkflowBlockBase & {
prompt?: string | null;
steps?: Array<CodeBlockStep> | null;
data_schema?: CodeBlockDataSchema;
+ user_owned_goal?: boolean | null;
+ goal_needs_regeneration?: boolean | null;
};
export type TextPromptBlock = WorkflowBlockBase & {
diff --git a/skyvern-frontend/src/routes/workflows/types/workflowYamlTypes.ts b/skyvern-frontend/src/routes/workflows/types/workflowYamlTypes.ts
index 1267fb73a..1f4823753 100644
--- a/skyvern-frontend/src/routes/workflows/types/workflowYamlTypes.ts
+++ b/skyvern-frontend/src/routes/workflows/types/workflowYamlTypes.ts
@@ -386,6 +386,8 @@ export type CodeBlockYAML = BlockYAMLBase & {
prompt?: string | null;
steps?: Array<CodeBlockStep> | null;
data_schema?: CodeBlockDataSchema;
+ user_owned_goal?: boolean | null;
+ goal_needs_regeneration?: boolean | null;
};
export type TextPromptBlockYAML = BlockYAMLBase & {
diff --git a/skyvern-frontend/src/store/useCopilotActionStore.ts b/skyvern-frontend/src/store/useCopilotActionStore.ts
index cb327d24a..d024d6bda 100644
--- a/skyvern-frontend/src/store/useCopilotActionStore.ts
+++ b/skyvern-frontend/src/store/useCopilotActionStore.ts
@@ -3,6 +3,15 @@ import { create } from "zustand";
export type CopilotBlockBuildRequest = {
blockLabel: string;
prompt: string;
+ // The person changed this block's Goal and is asking for the block to follow it.
+ applyingGoalChange?: boolean;
+};
+
+export type PendingGoalChange = {
+ label: string;
+ goal: string;
+ // Null when the editor never saw the Goal before the change, so it cannot be undone here.
+ previousGoal: string | null;
};
interface CopilotActionStore {
@@ -10,26 +19,82 @@ interface CopilotActionStore {
pendingBuild: CopilotBlockBuildRequest | null;
// Label of the block currently generating, so the block can show a local busy state.
generatingBlockLabel: string | null;
+ // Requests made while another block was generating; each starts when the one before it finishes.
+ queuedBuilds: Array<CopilotBlockBuildRequest>;
// Bumped when the user stops an in-flight block generation.
cancelNonce: number;
+ // Blocks whose Goal a person changed and that have not been updated to match it yet. Published
+ // from inside the canvas so surfaces outside it (the chat, the top bar) can read it.
+ pendingGoalChanges: Array<PendingGoalChange>;
+ undoGoalChange: (label: string) => void;
+ setPendingGoalChanges: (changes: Array<PendingGoalChange>) => void;
+ setUndoGoalChange: (undo: (label: string) => void) => void;
requestBuild: (request: CopilotBlockBuildRequest) => void;
+ applyPendingGoalChanges: () => void;
clearPendingBuild: () => void;
finishGenerating: () => void;
requestCancel: () => void;
}
-export const useCopilotActionStore = create<CopilotActionStore>((set) => ({
+const noUndo = () => {};
+
+export const useCopilotActionStore = create<CopilotActionStore>((set, get) => ({
pendingBuild: null,
generatingBlockLabel: null,
+ queuedBuilds: [],
cancelNonce: 0,
+ pendingGoalChanges: [],
+ undoGoalChange: noUndo,
+ setPendingGoalChanges: (changes) =>
+ set((state) =>
+ JSON.stringify(state.pendingGoalChanges) === JSON.stringify(changes)
+ ? state
+ : { pendingGoalChanges: changes },
+ ),
+ setUndoGoalChange: (undo) => set({ undoGoalChange: undo }),
requestBuild: (request) =>
- set({ pendingBuild: request, generatingBlockLabel: request.blockLabel }),
+ set((state) => {
+ if (state.generatingBlockLabel == null) {
+ return {
+ pendingBuild: request,
+ generatingBlockLabel: request.blockLabel,
+ };
+ }
+ const alreadyWaiting =
+ state.generatingBlockLabel === request.blockLabel ||
+ state.queuedBuilds.some(
+ (queued) => queued.blockLabel === request.blockLabel,
+ );
+ return alreadyWaiting
+ ? state
+ : { queuedBuilds: [...state.queuedBuilds, request] };
+ }),
+ applyPendingGoalChanges: () => {
+ for (const change of get().pendingGoalChanges) {
+ get().requestBuild({
+ blockLabel: change.label,
+ prompt: change.goal,
+ applyingGoalChange: true,
+ });
+ }
+ },
clearPendingBuild: () => set({ pendingBuild: null }),
- finishGenerating: () => set({ generatingBlockLabel: null }),
+ finishGenerating: () =>
+ set((state) => {
+ const [next, ...rest] = state.queuedBuilds;
+ return next
+ ? {
+ pendingBuild: next,
+ generatingBlockLabel: next.blockLabel,
+ queuedBuilds: rest,
+ }
+ : { generatingBlockLabel: null };
+ }),
requestCancel: () =>
set((state) => ({
pendingBuild: null,
generatingBlockLabel: null,
+ queuedBuilds: [],
cancelNonce: state.cancelNonce + 1,
})),
}));
diff --git a/skyvern/forge/prompts/skyvern/workflow-copilot-user.j2 b/skyvern/forge/prompts/skyvern/workflow-copilot-user.j2
index 7ddf355be..43e5d391d 100644
--- a/skyvern/forge/prompts/skyvern/workflow-copilot-user.j2
+++ b/skyvern/forge/prompts/skyvern/workflow-copilot-user.j2
@@ -28,6 +28,14 @@ USER WORKFLOW CHANGES SINCE LAST COPILOT TURN:
```
{% endif %}
+{% if user_goal_summary is defined and user_goal_summary %}
+USER-WRITTEN GOALS NOT APPLIED YET:
+
+```
+{{ user_goal_summary }}
+```
+{% endif %}
+
{% if runnable_draft_summary is defined and runnable_draft_summary %}
RUNNABLE UNCOMMITTED DRAFT (not on the canvas):
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index eb06ed778..1117d0a12 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -81,6 +81,7 @@ from skyvern.forge.sdk.copilot.build_test_outcome import (
from skyvern.forge.sdk.copilot.cache_envelope import CacheableSystemInstructions
from skyvern.forge.sdk.copilot.code_block_steps import (
bind_referenced_parameters_in_yaml,
+ carry_user_owned_goals_in_yaml,
derive_code_block_steps_in_yaml,
)
from skyvern.forge.sdk.copilot.completion_criteria_store import (
@@ -1537,6 +1538,7 @@ def _build_user_context(
request_policy_summary: str = "",
user_workflow_change_summary: str = "",
runnable_draft_summary: str = "",
+ user_goal_summary: str = "",
untrusted_evidence: str = "",
attached_files_summary: str = "",
) -> str:
@@ -1561,6 +1563,7 @@ def _build_user_context(
user_message=escape_code_fences(redact_raw_secrets_for_prompt(user_message)),
user_workflow_change_summary=escape_code_fences(user_workflow_change_summary or ""),
runnable_draft_summary=escape_code_fences(runnable_draft_summary or ""),
+ user_goal_summary=escape_code_fences(redact_raw_secrets_for_prompt(user_goal_summary or "")),
untrusted_evidence=escape_code_fences(redact_raw_secrets_for_structured_prompt(untrusted_evidence or "")),
attached_files_summary=escape_code_fences(redact_raw_secrets_for_prompt(attached_files_summary or "")),
)
@@ -2741,6 +2744,31 @@ _RAW_SECRET_LEAK_REFUSAL = (
f"credential ID beginning with cred_. {RAW_SECRET_REFUSAL_SENTINEL}."
)
_SAVED_DRAFT_OUTPUT_POLICY_SUFFIX = "I only blocked the chat reply; the workflow draft is still saved."
+
+
+def _user_owned_goal_notes(kept: list[str], dropped: list[str]) -> list[str]:
+ """Person-facing notes for a staged write that kept or lost a Goal the person wrote; a label the
+ leak heuristic would flag is left out rather than letting the note become a reject sentence."""
+ notes: list[str] = []
+ if kept:
+ note = f"I kept the Goal you wrote for {', '.join(kept)}."
+ notes.append(note if not contains_internal_machinery_leak(note) else "I kept the Goal you wrote.")
+ if dropped:
+ many = len(dropped) > 1
+ note = (
+ f"{', '.join(dropped)} carried {'Goals' if many else 'a Goal'} you wrote and "
+ f"{'are' if many else 'is'} no longer in the workflow, so "
+ f"{'those Goals are' if many else 'that Goal is'} gone."
+ )
+ notes.append(
+ note
+ if not contains_internal_machinery_leak(note)
+ else f"{'Blocks' if many else 'A block'} carrying a Goal you wrote {'are' if many else 'is'} no longer "
+ f"in the workflow, so {'those Goals are' if many else 'that Goal is'} gone."
+ )
+ return notes
+
+
_INLINE_REJECT_NOTE_FALLBACK = (
"This draft didn't pass validation against the live page, so I haven't saved it. "
"I'll revise it before proposing again."
@@ -3846,6 +3874,14 @@ async def _translate_to_agent_result(
workflow_yaml = default_data_write_continue_on_failure(
workflow_yaml, ctx.last_workflow_yaml or ctx.workflow_yaml
)
+ # Runs ahead of redaction so the stored Goal bytes it copies in pass the scrub seam; the
+ # inline action carries no artifact metadata, so nothing here can attribute a rebuild.
+ goal_carry = carry_user_owned_goals_in_yaml(
+ workflow_yaml, prior_yaml=ctx.last_workflow_yaml or ctx.workflow_yaml, rebuilt_labels=()
+ )
+ workflow_yaml = goal_carry.workflow_yaml
+ for goal_note in _user_owned_goal_notes(goal_carry.kept, goal_carry.dropped):
+ user_response = f"{user_response}\n\n(Note: {goal_note})"
# Same seam as the update_workflow tool: redact before the row is written and before
# the draft becomes the anchor, so both are the same string.
workflow_yaml = redact_credentials_in_workflow_yaml(
@@ -5624,12 +5660,15 @@ async def _run_copilot_turn_impl(
user_workflow_change_summary = ""
runnable_draft_summary = ""
+ user_goal_summary = ""
attached_files_summary = ""
if isinstance(ctx.turn_context_packet, TurnContextPacket):
if ctx.turn_context_packet.workflow_change_context is not None:
user_workflow_change_summary = ctx.turn_context_packet.workflow_change_context.rendered_summary
if ctx.turn_context_packet.runnable_draft_context is not None:
runnable_draft_summary = ctx.turn_context_packet.runnable_draft_context.rendered_summary
+ if ctx.turn_context_packet.user_goal_context is not None:
+ user_goal_summary = ctx.turn_context_packet.user_goal_context.rendered_summary
if ctx.turn_context_packet.attached_file_context is not None:
attached_files_summary = ctx.turn_context_packet.attached_file_context.render_prompt_block()
@@ -5673,6 +5712,7 @@ async def _run_copilot_turn_impl(
user_message=agent_user_message,
user_workflow_change_summary=user_workflow_change_summary,
runnable_draft_summary=runnable_draft_summary,
+ user_goal_summary=user_goal_summary,
untrusted_evidence=untrusted_evidence or "",
attached_files_summary=attached_files_summary,
)
diff --git a/skyvern/forge/sdk/copilot/code_block_steps.py b/skyvern/forge/sdk/copilot/code_block_steps.py
index 6ac5ad81e..90847eb15 100644
--- a/skyvern/forge/sdk/copilot/code_block_steps.py
+++ b/skyvern/forge/sdk/copilot/code_block_steps.py
@@ -4,14 +4,16 @@ import ast
import re
import textwrap
from dataclasses import dataclass, replace
-from typing import Any, Iterator
+from typing import Any, Collection, Iterator
from urllib.parse import urlsplit
import structlog
import yaml
+from skyvern.forge.sdk.copilot.block_type_aliases import normalize_copilot_block_type_alias
from skyvern.forge.sdk.copilot.code_block_synthesis import _RECORDING_REQUIRED_ACTION_TYPES, _RESERVED_PARAM_NAMES
from skyvern.utils.templating import mask_jinja_control_blocks, strip_jinja_control_blocks
+from skyvern.utils.yaml_loader import dump_workflow_yaml, safe_load_no_dates
LOG = structlog.get_logger()
@@ -540,10 +542,16 @@ def derive_code_block_steps(code: str) -> list[dict[str, Any]]:
]
+def is_code_block_type(block_type: object) -> bool:
+ """True for every spelling the schema later canonicalizes to ``code``, so a pre-normalization
+ pass cannot be skipped by an alias."""
+ return isinstance(block_type, str) and normalize_copilot_block_type_alias(block_type) == "code"
+
+
def _iter_code_block_dicts(node: Any) -> Iterator[dict[str, Any]]:
"""Yield every code-block dict anywhere in the workflow structure (handles nested loop_blocks)."""
if isinstance(node, dict):
- if node.get("block_type") == "code" and isinstance(node.get("code"), str):
+ if is_code_block_type(node.get("block_type")) and isinstance(node.get("code"), str):
yield node
for value in node.values():
yield from _iter_code_block_dicts(value)
@@ -555,7 +563,7 @@ def _iter_code_block_dicts(node: Any) -> Iterator[dict[str, Any]]:
def derive_code_block_steps_in_yaml(workflow_yaml: str) -> str:
"""Return workflow_yaml with each code block's `steps` rebuilt from its `code`, discarding any it carried."""
try:
- data = yaml.safe_load(workflow_yaml)
+ data = safe_load_no_dates(workflow_yaml)
except yaml.YAMLError:
return workflow_yaml
if not isinstance(data, (dict, list)):
@@ -580,14 +588,14 @@ def fill_code_block_error_code_mappings_in_yaml(workflow_yaml: str, *, prior_yam
are deliberate values and therefore remain untouched.
"""
try:
- data = yaml.safe_load(workflow_yaml)
+ data = safe_load_no_dates(workflow_yaml)
except yaml.YAMLError:
return workflow_yaml
if not isinstance(data, (dict, list)) or not prior_yaml:
return workflow_yaml
try:
- prior_data = yaml.safe_load(prior_yaml)
+ prior_data = safe_load_no_dates(prior_yaml)
except yaml.YAMLError:
return workflow_yaml
if not isinstance(prior_data, (dict, list)):
@@ -652,7 +660,7 @@ def bind_referenced_parameters_in_yaml(workflow_yaml: str) -> str:
declares can be added, so this cannot invent a binding.
"""
try:
- data = yaml.safe_load(workflow_yaml)
+ data = safe_load_no_dates(workflow_yaml)
except yaml.YAMLError:
return workflow_yaml
if not isinstance(data, (dict, list)):
@@ -680,3 +688,134 @@ def bind_referenced_parameters_in_yaml(workflow_yaml: str) -> str:
if not changed:
return workflow_yaml
return yaml.safe_dump(data, sort_keys=False)
+
+
+def _user_owned_goal_labels(workflow_yaml: str, *, awaiting_rebuild: bool) -> list[str]:
+ try:
+ data = safe_load_no_dates(workflow_yaml)
+ except yaml.YAMLError:
+ return []
+ if not isinstance(data, (dict, list)):
+ return []
+ labels: list[str] = []
+ for block in _iter_code_block_dicts(data):
+ label = block.get("label")
+ if not isinstance(label, str) or not label or block.get("user_owned_goal") is not True:
+ continue
+ if awaiting_rebuild and block.get("goal_needs_regeneration") is not True:
+ continue
+ labels.append(label)
+ return labels
+
+
+_USER_OWNED_GOAL_FIELDS = ("user_owned_goal", "goal_needs_regeneration")
+
+
+def code_block_labels_with_user_owned_goal(workflow_yaml: str) -> list[str]:
+ return _user_owned_goal_labels(workflow_yaml, awaiting_rebuild=False)
+
+
+def code_block_labels_awaiting_goal_rebuild(workflow_yaml: str) -> list[str]:
+ return _user_owned_goal_labels(workflow_yaml, awaiting_rebuild=True)
+
+
+@dataclass(frozen=True)
+class UserOwnedGoalCarry:
+ """The candidate workflow with user-owned Goals restored, and what the carry kept or dropped."""
+
+ workflow_yaml: str
+ kept: list[str]
+ dropped: list[str]
+
+ @property
+ def kept_message(self) -> str | None:
+ if not self.kept:
+ return None
+ return (
+ "The user wrote these blocks' Goals, so the stored Goal text was kept and the submitted "
+ "prompt was discarded for them."
+ )
+
+ @property
+ def dropped_message(self) -> str | None:
+ if not self.dropped:
+ return None
+ return (
+ "These blocks carried a Goal the user wrote and are absent from the accepted workflow, so "
+ "their Goal is gone: " + ", ".join(self.dropped) + "."
+ )
+
+
+def carry_user_owned_goals_in_yaml(
+ workflow_yaml: str,
+ *,
+ prior_yaml: str | None = None,
+ rebuilt_labels: Collection[str] = (),
+) -> UserOwnedGoalCarry:
+ """Ownership is read from prior_yaml alone, so a label the prior does not mark user-owned has both
+ fields stripped; goal_needs_regeneration clears only for labels in rebuilt_labels."""
+ try:
+ data = safe_load_no_dates(workflow_yaml)
+ prior_data = safe_load_no_dates(prior_yaml) if prior_yaml else {}
+ except yaml.YAMLError:
+ return UserOwnedGoalCarry(workflow_yaml, [], [])
+ if not isinstance(prior_data, dict) or not isinstance(data, dict):
+ return UserOwnedGoalCarry(workflow_yaml, [], [])
+
+ owned_prior_blocks: dict[str, dict[str, Any]] = {}
+ for prior_block in _iter_code_block_dicts(prior_data):
+ prior_label = prior_block.get("label")
+ if isinstance(prior_label, str) and prior_block.get("user_owned_goal") is True:
+ owned_prior_blocks[prior_label] = prior_block
+
+ rebuilt = set(rebuilt_labels)
+ changed = False
+ kept: list[str] = []
+ submitted_labels: set[str] = set()
+ for block in _iter_code_block_dicts(data):
+ label = block.get("label")
+ if isinstance(label, str):
+ submitted_labels.add(label)
+ owner = owned_prior_blocks.get(label) if isinstance(label, str) else None
+ if owner is None or not isinstance(label, str):
+ for field in _USER_OWNED_GOAL_FIELDS:
+ if block.get(field):
+ del block[field]
+ changed = True
+ continue
+ owned_prompt = owner.get("prompt")
+ restored: dict[str, Any] = {
+ "user_owned_goal": True,
+ "goal_needs_regeneration": owner.get("goal_needs_regeneration") is True and label not in rebuilt,
+ }
+ if isinstance(owned_prompt, str):
+ restored["prompt"] = owned_prompt
+ if block.get("prompt") != owned_prompt:
+ kept.append(label)
+ for field, value in restored.items():
+ if block.get(field) != value:
+ block[field] = value
+ changed = True
+
+ dropped = sorted(
+ label
+ for label, owned_block in owned_prior_blocks.items()
+ if label not in submitted_labels and isinstance(owned_block.get("prompt"), str)
+ )
+ if dropped:
+ LOG.info("copilot submission omitted user-owned goal blocks", dropped_labels=dropped)
+
+ carried_yaml = dump_workflow_yaml(data) if changed else workflow_yaml
+ return UserOwnedGoalCarry(carried_yaml, sorted(kept), dropped)
+
+
+def user_owned_goal_carry_disclosure(carry: UserOwnedGoalCarry) -> dict[str, str | list[str]]:
+ """What the model must be told about Goals it submitted that the carry overrode or lost."""
+ disclosure: dict[str, str | list[str]] = {}
+ if carry.kept_message is not None:
+ disclosure["stored_goal_kept"] = carry.kept
+ disclosure["stored_goal_kept_message"] = carry.kept_message
+ if carry.dropped_message is not None:
+ disclosure["stored_goal_dropped"] = carry.dropped
+ disclosure["stored_goal_dropped_message"] = carry.dropped_message
+ return disclosure
diff --git a/skyvern/forge/sdk/copilot/review_gate.py b/skyvern/forge/sdk/copilot/review_gate.py
index a79873c2c..9e1cb22ba 100644
--- a/skyvern/forge/sdk/copilot/review_gate.py
+++ b/skyvern/forge/sdk/copilot/review_gate.py
@@ -195,11 +195,18 @@ DESTINATION_ADAPTERS: dict[str, DestinationAdapter] = {
}
+_CODE_BLOCK_NON_IDENTITY_KEYS = frozenset({"steps", "user_owned_goal", "goal_needs_regeneration"})
+
+
def _without_code_steps(value: Any) -> Any:
- """Drop code-derived `steps` at every nesting depth; they are a projection of `code`, not execution identity."""
+ """Drop code-derived `steps` and the Goal-ownership facts at every nesting depth; neither is execution identity."""
if isinstance(value, dict):
is_code = _block_type(value) == BlockType.CODE.value
- return {key: _without_code_steps(item) for key, item in value.items() if not (is_code and key == "steps")}
+ return {
+ key: _without_code_steps(item)
+ for key, item in value.items()
+ if not (is_code and key in _CODE_BLOCK_NON_IDENTITY_KEYS)
+ }
if isinstance(value, list):
return [_without_code_steps(item) for item in value]
return value
diff --git a/skyvern/forge/sdk/copilot/tools/__init__.py b/skyvern/forge/sdk/copilot/tools/__init__.py
index e7cbd6cfe..516f40458 100644
--- a/skyvern/forge/sdk/copilot/tools/__init__.py
+++ b/skyvern/forge/sdk/copilot/tools/__init__.py
@@ -413,6 +413,7 @@ async def _persist_block_scoped_edit(
originating_call_id: str | None = None,
code_artifact_metadata: list[CodeArtifactMetadata] | None = None,
block_observation_refs: list[BlockObservationRef] | None = None,
+ rebuilt_block_labels: list[str] | None = None,
) -> str:
"""Send a server-composed workflow through the normal persistence path.
@@ -427,6 +428,8 @@ async def _persist_block_scoped_edit(
if block_observation_refs is not None:
params["block_observation_refs"] = normalize_block_observation_refs(block_observation_refs)
params["raw_block_observation_refs"] = block_observation_refs
+ if rebuilt_block_labels:
+ params["_rebuilt_block_labels"] = rebuilt_block_labels
with copilot_span(tool_name, data={"yaml_length": len(workflow_yaml)}):
result = await _update_workflow(params, copilot_ctx, originating_call_id=originating_call_id)
_record_workflow_update_result(copilot_ctx, result, prior_definition)
@@ -501,7 +504,12 @@ async def edit_block_tool(
)
return json.dumps(sanitize_tool_result_for_llm("edit_block", result))
return await _persist_block_scoped_edit(
- copilot_ctx, "edit_block", workflow_yaml, arguments, originating_call_id=_originating_call_id(ctx)
+ copilot_ctx,
+ "edit_block",
+ workflow_yaml,
+ arguments,
+ originating_call_id=_originating_call_id(ctx),
+ rebuilt_block_labels=[label] if replacement_code is not None or "code" in (fields or {}) else None,
)
@@ -649,6 +657,7 @@ async def edit_block_and_run_tool(
"workflow_yaml": workflow_yaml,
"_preserve_code_block_associations": True,
"_expected_exact_code_by_label": {label: resolution.source},
+ "_rebuilt_block_labels": [label],
},
copilot_ctx,
allow_missing_credentials=skip_run_after_update,
@@ -665,7 +674,11 @@ async def edit_block_and_run_tool(
prior_definition = await _get_prior_workflow_definition(copilot_ctx)
with copilot_span("edit_block_and_run.update", data={"yaml_length": len(workflow_yaml)}):
update_result = await _update_workflow(
- {"workflow_yaml": workflow_yaml, "_preserve_code_block_associations": True},
+ {
+ "workflow_yaml": workflow_yaml,
+ "_preserve_code_block_associations": True,
+ "_rebuilt_block_labels": [label],
+ },
copilot_ctx,
allow_missing_credentials=skip_run_after_update,
originating_call_id=_originating_call_id(ctx),
@@ -1346,6 +1359,7 @@ def _promote_executed_sources(
promoted[label] = resolution.source
update_params["workflow_yaml"] = workflow_yaml
update_params["_expected_exact_code_by_label"] = promoted
+ update_params["_rebuilt_block_labels"] = sorted(promoted)
return None
diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.py
index 29722f701..f3209680c 100644
--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py
+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py
@@ -47,7 +47,11 @@ from skyvern.forge.sdk.copilot.code_block_preflight import (
scanner_advisory_diagnostics,
)
from skyvern.forge.sdk.copilot.code_block_security import CodeBlockSecurityError, author_time_code_security_errors
-from skyvern.forge.sdk.copilot.code_block_steps import bind_referenced_parameters_in_yaml
+from skyvern.forge.sdk.copilot.code_block_steps import (
+ bind_referenced_parameters_in_yaml,
+ carry_user_owned_goals_in_yaml,
+ user_owned_goal_carry_disclosure,
+)
from skyvern.forge.sdk.copilot.code_block_synthesis import wrapped_code_ast as _wrapped_code_ast
from skyvern.forge.sdk.copilot.code_write_diff import CodeWriteDiff, build_code_write_diffs
from skyvern.forge.sdk.copilot.completion_verification import grade_definition_criteria
@@ -1785,6 +1789,20 @@ def _path_segments(path: str) -> list[tuple[str, bool]]:
return segments
+def code_artifact_metadata_block_labels(raw_metadata: object) -> set[str]:
+ """Block labels the submission claims to have authored a code artifact for, read from the raw rows:
+ a row normalization later drops for an unrelated field still names the block the model rebuilt."""
+ labels: set[str] = set()
+ for raw_item in _code_artifact_metadata_items(raw_metadata):
+ item = _raw_metadata_item_mapping(raw_item)
+ if item is None:
+ continue
+ label = str(item.get("block_label") or "").strip()
+ if label:
+ labels.add(label)
+ return labels
+
+
def _metadata_item_for_block_label(raw_metadata: object, block_label: str) -> Mapping[str, Any] | None:
for raw_item in _code_artifact_metadata_items(raw_metadata):
item = _raw_metadata_item_mapping(raw_item)
@@ -3878,6 +3896,11 @@ def carry_author_time_findings(update_result: dict[str, Any], result: dict[str,
"stored_code_rewritten",
"dropped_prior_blocks",
"block_type_changes",
+ "stored_goal_kept",
+ "stored_goal_kept_message",
+ "stored_goal_dropped",
+ "stored_goal_dropped_message",
+ "google_connection_resolution",
"persistence",
"persistence_message",
"google_sheet_tab_resolution",
@@ -4499,6 +4522,15 @@ async def _update_workflow(
# reach the resolver already replaced by the placeholder.
workflow_yaml, google_connection_resolution = await canonicalize_named_google_sheet_bindings(workflow_yaml, ctx)
workflow_yaml, google_sheet_tab_resolution = await resolve_google_sheet_tabs_from_gid(workflow_yaml, ctx)
+ # Ahead of redaction: this copies stored Goal bytes into the candidate, so they have to
+ # pass the scrub seam like any other submitted text.
+ goal_carry = carry_user_owned_goals_in_yaml(
+ workflow_yaml,
+ prior_yaml=prior_yaml,
+ rebuilt_labels=code_artifact_metadata_block_labels(ctx.submitted_code_artifact_metadata_snapshot)
+ | set(params.get("_rebuilt_block_labels") or ()),
+ )
+ workflow_yaml = goal_carry.workflow_yaml
# Ahead of both persistence and the context assignment below, so the row, the draft the
# model reads back, and the bytes apply_block_edit anchors against are one string. Scrubbing
# the payload alone would leave the model reading redacted code and anchoring on raw code.
@@ -4741,6 +4773,7 @@ async def _update_workflow(
data["dropped_prior_blocks"] = dropped_prior_blocks
if block_type_changes:
data["block_type_changes"] = block_type_changes
+ data.update(user_owned_goal_carry_disclosure(goal_carry))
if stored_code or stored_code_withheld:
LOG.info(
"copilot write returned stored code",
diff --git a/skyvern/forge/sdk/copilot/turn_context.py b/skyvern/forge/sdk/copilot/turn_context.py
index 312647e50..ad2a79d0f 100644
--- a/skyvern/forge/sdk/copilot/turn_context.py
+++ b/skyvern/forge/sdk/copilot/turn_context.py
@@ -6,6 +6,10 @@ import structlog
import yaml
from pydantic import BaseModel, ConfigDict, Field
+from skyvern.forge.sdk.copilot.code_block_steps import (
+ code_block_labels_awaiting_goal_rebuild,
+ code_block_labels_with_user_owned_goal,
+)
from skyvern.forge.sdk.copilot.request_policy import (
RequestPolicy,
build_transcript_context,
@@ -62,6 +66,11 @@ class RunnableDraftContext(BaseModel):
block_labels: list[str] = Field(default_factory=list)
+class UserGoalContext(BaseModel):
+ rendered_summary: str
+ block_labels: list[str] = Field(default_factory=list)
+
+
class TranscriptContext(BaseModel):
earliest_user_turn: str
latest_prior_user_turn: str
@@ -115,6 +124,7 @@ class TurnContextPacket(BaseModel):
proposal_context: ProposalContext | None = None
workflow_change_context: WorkflowChangeContext | None = None
runnable_draft_context: RunnableDraftContext | None = None
+ user_goal_context: UserGoalContext | None = None
transcript_context: TranscriptContext
run_context: RunContext | None = None
credential_context: CredentialContext | None = None
@@ -127,6 +137,7 @@ class TurnContextPacket(BaseModel):
"proposal_context",
"workflow_change_context",
"runnable_draft_context",
+ "user_goal_context",
"run_context",
"credential_context",
"attached_file_context",
@@ -288,6 +299,7 @@ class TurnContextAssembler:
)
runnable_draft_context = self._runnable_draft_context(inputs)
+ user_goal_context = self._user_goal_context(inputs)
if inputs.prior_run_packet:
run_context = RunContext(packet=inputs.prior_run_packet)
@@ -305,6 +317,7 @@ class TurnContextAssembler:
proposal_context=proposal_context,
workflow_change_context=workflow_change_context,
runnable_draft_context=runnable_draft_context,
+ user_goal_context=user_goal_context,
transcript_context=transcript_context,
run_context=run_context,
credential_context=credential_context,
@@ -318,6 +331,20 @@ class TurnContextAssembler:
)
return packet
+ def _user_goal_context(self, inputs: TurnContextInputs) -> UserGoalContext | None:
+ labels = code_block_labels_awaiting_goal_rebuild(inputs.workflow_yaml)
+ if not labels:
+ return None
+ owned_labels = code_block_labels_with_user_owned_goal(inputs.workflow_yaml)
+ summary = (
+ "The user changed the Goal on these code blocks and has not applied it yet: "
+ f"{', '.join(labels)}. Their Goal text is what the block is for; the saved code still does what the "
+ "previous Goal said. The user applies a new Goal from the block, which sends its own request. "
+ f"A Goal you submit for any user-owned block ({', '.join(owned_labels)}) is discarded; the Goal "
+ "field on the block is where the user edits it."
+ )
+ return UserGoalContext(rendered_summary=summary, block_labels=labels)
+
def _runnable_draft_context(self, inputs: TurnContextInputs) -> RunnableDraftContext | None:
if not inputs.request_policy.allow_run_blocks:
return None
diff --git a/skyvern/forge/sdk/copilot/workflow_yaml.py b/skyvern/forge/sdk/copilot/workflow_yaml.py
index fd6f626e6..519430b42 100644
--- a/skyvern/forge/sdk/copilot/workflow_yaml.py
+++ b/skyvern/forge/sdk/copilot/workflow_yaml.py
@@ -19,6 +19,7 @@ from skyvern.forge.sdk.copilot.block_type_aliases import normalize_copilot_block
from skyvern.forge.sdk.copilot.code_block_steps import (
bind_referenced_parameters_in_yaml,
derive_code_block_steps_in_yaml,
+ is_code_block_type,
)
from skyvern.forge.sdk.copilot.workflow_block_traversal import (
WorkflowBlockLocation,
@@ -242,7 +243,7 @@ def runner_code_block_associations(
for location in workflow_block_locations(parsed):
block = location.block
label = block.get("label")
- if block.get("block_type") == "code" and isinstance(label, str) and label:
+ if is_code_block_type(block.get("block_type")) and isinstance(label, str) and label:
associations[label] = (
prior_associations[label] if preserve_existing and label in prior_associations else f"cba_{uuid4().hex}"
)
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.py
index dcaee0eff..e01961cc5 100644
--- a/skyvern/forge/sdk/workflow/models/block.py
+++ b/skyvern/forge/sdk/workflow/models/block.py
@@ -6505,6 +6505,8 @@ class CodeBlock(Block):
prompt: str | None = None
steps: list[CodeBlockStep] | None = None
data_schema: dict[str, Any] | list | str | None = None
+ user_owned_goal: bool | None = None
+ goal_needs_regeneration: bool | None = None
BLOCKED_ATTRS: ClassVar[frozenset[str]] = CODE_BLOCK_BLOCKED_ATTRS
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 6f5772cd2..198f62cb5 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -1459,8 +1459,8 @@ def _get_workflow_definition_core_data(workflow_definition: WorkflowDefinition)
"version",
"model",
]
- # `steps` is a plain-language annotation, not execution input, so editing it must not bust the cached script.
- code_block_annotation_fields = ("steps",)
+ # Plain-language annotations, not execution input, so editing them must not bust the cached script.
+ code_block_annotation_fields = ("steps", "user_owned_goal", "goal_needs_regeneration")
# Use BFS to recursively remove fields from all nested objects
diff --git a/skyvern/forge/sdk/workflow/workflow_definition_converter.py b/skyvern/forge/sdk/workflow/workflow_definition_converter.py
index 1c34823b3..1689f3478 100644
--- a/skyvern/forge/sdk/workflow/workflow_definition_converter.py
+++ b/skyvern/forge/sdk/workflow/workflow_definition_converter.py
@@ -546,6 +546,8 @@ def block_yaml_to_block(
prompt=block_yaml.prompt,
steps=[CodeBlockStep(**step) for step in derive_code_block_steps(block_yaml.code)] or None,
data_schema=block_yaml.data_schema,
+ user_owned_goal=block_yaml.user_owned_goal,
+ goal_needs_regeneration=block_yaml.goal_needs_regeneration,
)
elif block_yaml.block_type == BlockType.TEXT_PROMPT:
return TextPromptBlock(
diff --git a/skyvern/schemas/workflows.py b/skyvern/schemas/workflows.py
index 1b65af041..3be75297d 100644
--- a/skyvern/schemas/workflows.py
+++ b/skyvern/schemas/workflows.py
@@ -1097,6 +1097,14 @@ class CodeBlockYAML(BlockYAML):
default=None,
description="JSON schema of the object this block's return produces; keys match the return keys; null when the block returns nothing",
)
+ user_owned_goal: bool | None = Field(
+ default=None,
+ description="True when a person wrote this block's Goal, so copilot regenerations keep their text. Set by the editor or the workflow API; a value the copilot submits is ignored in favour of the stored one",
+ )
+ goal_needs_regeneration: bool | None = Field(
+ default=None,
+ description="True when a person edited the Goal and the code has not been rebuilt from it yet. Set by the editor or the workflow API; a value the copilot submits is ignored in favour of the stored one",
+ )
@model_validator(mode="before")
@classmethod
diff --git a/tests/unit/copilot/test_review_gate.py b/tests/unit/copilot/test_review_gate.py
index 800d92d4b..9a88d217c 100644
--- a/tests/unit/copilot/test_review_gate.py
+++ b/tests/unit/copilot/test_review_gate.py
@@ -880,3 +880,13 @@ def test_editing_one_of_two_blocks_leaves_the_untouched_block_current() -> None:
("sign_in", "current_source"),
("read_metric", "different_source"),
]
+
+
+def test_tested_block_fingerprint_ignores_the_goal_ownership_facts() -> None:
+ hand_edited = _top_level(_READ_PRICE_BLOCK + " user_owned_goal: true\n goal_needs_regeneration: false\n")
+ resubmitted = _top_level(_READ_PRICE_BLOCK)
+
+ assert workflow_block_fingerprints(hand_edited) == workflow_block_fingerprints(resubmitted)
+ projection = build_review_projection(hand_edited, resubmitted, workflow_block_fingerprints(hand_edited))
+ assert projection is not None
+ assert [(block["change"], block["coverage"]) for block in projection["blocks"]] == [("unchanged", "current_source")]
diff --git a/tests/unit/test_code_block_steps.py b/tests/unit/test_code_block_steps.py
index 8a7bb2ed3..df5609649 100644
--- a/tests/unit/test_code_block_steps.py
+++ b/tests/unit/test_code_block_steps.py
@@ -1,5 +1,6 @@
import textwrap
import timeit
+from typing import Any
import pytest
import yaml
@@ -7,8 +8,12 @@ import yaml
from skyvern.forge.sdk.copilot.code_block_steps import (
analyze_code_actions,
bind_referenced_parameters_in_yaml,
+ carry_user_owned_goals_in_yaml,
+ code_block_labels_awaiting_goal_rebuild,
+ code_block_labels_with_user_owned_goal,
derive_code_block_steps,
derive_code_block_steps_in_yaml,
+ user_owned_goal_carry_disclosure,
)
from skyvern.forge.sdk.copilot.code_block_synthesis import synthesize_code_block
from skyvern.webeye.actions.action_types import ActionType
@@ -706,6 +711,293 @@ def test_dom_reads_separated_by_an_action_are_distinct_steps():
assert [s["action_type"] for s in steps] == ["extract", "click", "extract"]
+def _goal_yaml(
+ *,
+ prompt: str,
+ code: str,
+ user_owned_goal: bool | None = None,
+ goal_needs_regeneration: bool | None = None,
+ label: str = "login",
+) -> str:
+ block: dict[str, str | bool] = {"block_type": "code", "label": label, "prompt": prompt, "code": code}
+ if user_owned_goal is not None:
+ block["user_owned_goal"] = user_owned_goal
+ if goal_needs_regeneration is not None:
+ block["goal_needs_regeneration"] = goal_needs_regeneration
+ return yaml.safe_dump({"workflow_definition": {"blocks": [block]}}, sort_keys=False)
+
+
+def _goal_block(workflow_yaml: str) -> dict[str, Any]:
+ return yaml.safe_load(workflow_yaml)["workflow_definition"]["blocks"][0]
+
+
+def test_stored_user_owned_goal_replaces_the_submitted_prompt():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ submitted = _goal_yaml(prompt="Sign in to the portal", code="await page.click('#invoice')")
+
+ carry = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior)
+ block = _goal_block(carry.workflow_yaml)
+
+ assert block["prompt"] == "Download last month's invoice"
+ assert block["code"] == "await page.click('#invoice')"
+ assert block["user_owned_goal"] is True
+ assert carry.kept == ["login"]
+
+
+def test_a_model_owned_block_keeps_the_submitted_prompt_even_when_it_differs():
+ prior = _goal_yaml(prompt="Sign in", code="await page.goto(url)")
+ submitted = _goal_yaml(prompt="Sign in and open the dashboard", code="await page.goto(url)")
+
+ carry = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior)
+ block = _goal_block(carry.workflow_yaml)
+
+ assert block["prompt"] == "Sign in and open the dashboard"
+ assert "user_owned_goal" not in block
+ assert carry.kept == []
+
+
+def test_a_submission_cannot_mint_the_ownership_fact_without_a_stored_one():
+ prior = _goal_yaml(prompt="Sign in", code="await page.goto(url)")
+ submitted = _goal_yaml(
+ prompt="Whatever the model wants",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+
+ block = _goal_block(carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior).workflow_yaml)
+
+ assert block["prompt"] == "Whatever the model wants"
+ assert "user_owned_goal" not in block
+ assert "goal_needs_regeneration" not in block
+
+
+def test_a_submission_cannot_mint_the_ownership_fact_when_there_is_no_prior_at_all():
+ submitted = _goal_yaml(
+ prompt="Rebuild this block and run it",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+
+ carried = carry_user_owned_goals_in_yaml(submitted, prior_yaml=None).workflow_yaml
+
+ assert "user_owned_goal" not in _goal_block(carried)
+ assert "goal_needs_regeneration" not in _goal_block(carried)
+ assert code_block_labels_awaiting_goal_rebuild(carried) == []
+ assert code_block_labels_with_user_owned_goal(carried) == []
+
+
+def test_no_prior_yaml_leaves_a_submission_without_the_fields_byte_for_byte():
+ submitted = _goal_yaml(prompt="Sign in", code="await page.goto(url)")
+
+ assert carry_user_owned_goals_in_yaml(submitted, prior_yaml=None).workflow_yaml == submitted
+
+
+def test_resubmitting_identical_code_leaves_the_block_awaiting_a_rebuild():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ submitted = _goal_yaml(prompt="Sign in", code="await page.goto(url)")
+
+ carried = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior).workflow_yaml
+
+ assert _goal_block(carried)["goal_needs_regeneration"] is True
+ assert code_block_labels_awaiting_goal_rebuild(carried) == ["login"]
+
+
+def test_an_unattributed_code_edit_does_not_cancel_the_pending_rebuild():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ submitted = _goal_yaml(prompt="Sign in", code="await page.goto(url) # tidied")
+
+ carried = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior).workflow_yaml
+
+ assert _goal_block(carried)["goal_needs_regeneration"] is True
+ assert code_block_labels_awaiting_goal_rebuild(carried) == ["login"]
+
+
+def test_a_rebuild_of_the_block_clears_the_pending_rebuild_and_keeps_ownership():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ submitted = _goal_yaml(prompt="Sign in", code="await page.get_by_role('link', name='Invoice').click()")
+
+ carried = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior, rebuilt_labels={"login"}).workflow_yaml
+ block = _goal_block(carried)
+
+ assert block["goal_needs_regeneration"] is False
+ assert block["user_owned_goal"] is True
+ assert block["prompt"] == "Download last month's invoice"
+ assert code_block_labels_awaiting_goal_rebuild(carried) == []
+
+
+def test_a_rebuild_that_reproduces_the_same_code_still_clears_the_pending_rebuild():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ submitted = _goal_yaml(prompt="Sign in", code="await page.goto(url)")
+
+ carried = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior, rebuilt_labels={"login"}).workflow_yaml
+
+ assert _goal_block(carried)["goal_needs_regeneration"] is False
+ assert code_block_labels_awaiting_goal_rebuild(carried) == []
+
+
+def test_a_differently_cased_block_type_cannot_dodge_the_carry():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ submitted = _goal_yaml(
+ prompt="Whatever the model wants",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ ).replace("block_type: code", "block_type: CODE ")
+
+ block = _goal_block(carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior).workflow_yaml)
+
+ assert block["prompt"] == "Download last month's invoice"
+ assert block["goal_needs_regeneration"] is True
+
+
+def test_an_impossible_date_scalar_in_the_workflow_does_not_break_the_carry_or_the_readers():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ submitted = _goal_yaml(prompt="Sign in", code="await page.goto(url)") + "parameters:\n- default_value: 2025-02-30\n"
+
+ carry = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior + "parameters:\n- default_value: 2025-02-30\n")
+
+ assert _goal_block(carry.workflow_yaml)["prompt"] == "Download last month's invoice"
+ assert code_block_labels_awaiting_goal_rebuild(carry.workflow_yaml) == ["login"]
+ assert code_block_labels_with_user_owned_goal(carry.workflow_yaml) == ["login"]
+
+
+def test_a_stored_goal_that_is_not_text_never_overwrites_the_submitted_prompt():
+ prior = yaml.safe_dump(
+ {
+ "workflow_definition": {
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "login",
+ "prompt": None,
+ "code": "await page.goto(url)",
+ "user_owned_goal": True,
+ }
+ ]
+ }
+ },
+ sort_keys=False,
+ )
+ submitted = _goal_yaml(prompt="Sign in to the portal", code="await page.goto(url)")
+
+ carry = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior)
+
+ assert _goal_block(carry.workflow_yaml)["prompt"] == "Sign in to the portal"
+ assert carry.kept == []
+
+
+def test_a_dropped_user_owned_block_is_disclosed_by_label_without_its_goal_text():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ )
+ submitted = _goal_yaml(prompt="Export the report", code="await page.goto(url)", label="export")
+
+ carry = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior)
+
+ assert carry.dropped == ["login"]
+ disclosure = user_owned_goal_carry_disclosure(carry)
+ assert disclosure["stored_goal_dropped"] == ["login"]
+ assert "Download last month's invoice" not in str(disclosure)
+
+
+def test_a_new_label_the_stored_workflow_does_not_have_is_model_owned():
+ prior = _goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ submitted = _goal_yaml(prompt="Export the report", code="await page.goto(url)", label="export")
+
+ block = _goal_block(carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior).workflow_yaml)
+
+ assert block["prompt"] == "Export the report"
+ assert "user_owned_goal" not in block
+
+
+def test_an_unparseable_prior_yaml_leaves_the_submission_and_its_fields_untouched():
+ submitted = _goal_yaml(
+ prompt="Sign in",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+
+ assert carry_user_owned_goals_in_yaml(submitted, prior_yaml="title: [unclosed").workflow_yaml == submitted
+
+
+def test_a_block_marked_stale_without_ownership_is_not_awaiting_a_rebuild():
+ orphan_flag = _goal_yaml(prompt="Sign in", code="await page.goto(url)", goal_needs_regeneration=True)
+
+ assert code_block_labels_awaiting_goal_rebuild(orphan_flag) == []
+ assert code_block_labels_with_user_owned_goal(orphan_flag) == []
+
+
+def _nested_goal_yaml(*, prompt: str, code: str, user_owned_goal: bool | None = None) -> str:
+ inner: dict[str, str | bool] = {"block_type": "code", "label": "inner_login", "prompt": prompt, "code": code}
+ if user_owned_goal is not None:
+ inner["user_owned_goal"] = user_owned_goal
+ inner["goal_needs_regeneration"] = user_owned_goal
+ loop = {"block_type": "for_loop", "label": "each_row", "loop_blocks": [inner]}
+ return yaml.safe_dump({"workflow_definition": {"blocks": [loop]}}, sort_keys=False)
+
+
+def test_a_user_owned_goal_inside_a_loop_block_is_carried_named_and_disclosed():
+ prior = _nested_goal_yaml(
+ prompt="Download last month's invoice",
+ code="await page.goto(url)",
+ user_owned_goal=True,
+ )
+ submitted = _nested_goal_yaml(prompt="Sign in to the portal", code="await page.goto(url)")
+
+ carry = carry_user_owned_goals_in_yaml(submitted, prior_yaml=prior)
+ inner = yaml.safe_load(carry.workflow_yaml)["workflow_definition"]["blocks"][0]["loop_blocks"][0]
+
+ assert inner["prompt"] == "Download last month's invoice"
+ assert inner["user_owned_goal"] is True
+ assert code_block_labels_awaiting_goal_rebuild(carry.workflow_yaml) == ["inner_login"]
+ assert code_block_labels_with_user_owned_goal(carry.workflow_yaml) == ["inner_login"]
+ assert carry.kept == ["inner_login"]
+
+
def test_control_flow_reads_do_not_fabricate_an_extraction_step():
# Visibility checks and counts gate control flow; they are not data extraction
# and must not invent an extract step.
diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.py
index 77776ca6d..786bbba88 100644
--- a/tests/unit/test_copilot_agent_helpers.py
+++ b/tests/unit/test_copilot_agent_helpers.py
@@ -3634,6 +3634,22 @@ workflow_definition:
)
+def test_a_goal_note_whose_label_looks_like_a_run_id_is_not_swapped_for_a_reject_sentence() -> None:
+ notes = agent_module._user_owned_goal_notes(["wr_lookup"], ["pbs_export", "wr_totals"])
+
+ assert len(notes) == 2
+ assert not any(agent_module.contains_internal_machinery_leak(note) for note in notes)
+ assert not any("haven't saved" in note for note in notes)
+ assert "Goal" in notes[0] and "gone" in notes[1]
+
+
+def test_a_goal_note_names_the_block_when_the_label_is_ordinary() -> None:
+ kept_note, dropped_note = agent_module._user_owned_goal_notes(["get_invoice"], ["export", "login"])
+
+ assert "get_invoice" in kept_note
+ assert "export, login" in dropped_note
+
+
class TestTranslateToAgentResultGating:
"""Covers the three SKY-9143 invariants that live in _translate_to_agent_result."""
@@ -3904,6 +3920,205 @@ class TestTranslateToAgentResultGating:
assert agent_result.workflow_yaml is None
assert agent_result.response_type == "REPLACE_WORKFLOW"
+ def test_inline_replace_workflow_keeps_a_user_written_goal_and_cannot_clear_its_rebuild(self, monkeypatch) -> None:
+ converted: list[str] = []
+
+ async def _process(**kwargs) -> SimpleNamespace:
+ converted.append(str(kwargs["workflow_yaml"]))
+ return SimpleNamespace(name="replacement", extra_http_headers=None, cdp_connect_headers=None)
+
+ monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._process_workflow_yaml", _process)
+
+ human_goal = "Download last month's invoice as a PDF"
+ prior_yaml = yaml.safe_dump(
+ {
+ "workflow_definition": {
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "get_invoice",
+ "prompt": human_goal,
+ "user_owned_goal": True,
+ "goal_needs_regeneration": True,
+ "code": "await page.goto(url)\n",
+ }
+ ]
+ }
+ },
+ sort_keys=False,
+ )
+ submitted_yaml = yaml.safe_dump(
+ {
+ "workflow_definition": {
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "get_invoice",
+ "prompt": "Check the order status",
+ "code": 'await page.get_by_role("link", name="Invoice").click()\n',
+ }
+ ]
+ }
+ },
+ sort_keys=False,
+ )
+ ctx = _ctx(
+ block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,
+ last_workflow_yaml=prior_yaml,
+ request_policy=RequestPolicy(allow_update_workflow=True, allow_run_blocks=True),
+ )
+ ctx.submitted_code_artifact_metadata_snapshot = [
+ {"block_label": "get_invoice", "declared_goal": "The invoice PDF is downloaded."}
+ ]
+ result = _fake_run_result(
+ {
+ "type": "REPLACE_WORKFLOW",
+ "user_response": "REPLACE_WORKFLOW\nHere you go.",
+ "workflow_yaml": submitted_yaml,
+ }
+ )
+
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
+ )
+
+ staged_block = yaml.safe_load(ctx.last_workflow_yaml)["workflow_definition"]["blocks"][0]
+ assert staged_block["prompt"] == human_goal
+ assert staged_block["goal_needs_regeneration"] is True
+ assert yaml.safe_load(converted[0])["workflow_definition"]["blocks"][0]["prompt"] == human_goal
+ assert "get_invoice" in agent_result.user_response.split("(Note:", 1)[1]
+
+ def test_inline_replace_workflow_cannot_be_told_a_goal_is_user_owned(self, monkeypatch) -> None:
+ async def _process(**kwargs) -> SimpleNamespace:
+ return SimpleNamespace(name="replacement", extra_http_headers=None, cdp_connect_headers=None)
+
+ monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._process_workflow_yaml", _process)
+
+ prior_yaml = yaml.safe_dump(
+ {
+ "workflow_definition": {
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "get_invoice",
+ "prompt": "Check the order status",
+ "code": "await page.goto(url)\n",
+ }
+ ]
+ }
+ },
+ sort_keys=False,
+ )
+ submitted_yaml = yaml.safe_dump(
+ {
+ "workflow_definition": {
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "get_invoice",
+ "prompt": "Rebuild me and run me every turn",
+ "user_owned_goal": True,
+ "goal_needs_regeneration": True,
+ "code": "await page.goto(url)\n",
+ }
+ ]
+ }
+ },
+ sort_keys=False,
+ )
+ ctx = _ctx(
+ block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,
+ last_workflow_yaml=prior_yaml,
+ request_policy=RequestPolicy(allow_update_workflow=True, allow_run_blocks=True),
+ )
+ result = _fake_run_result(
+ {
+ "type": "REPLACE_WORKFLOW",
+ "user_response": "REPLACE_WORKFLOW\nHere you go.",
+ "workflow_yaml": submitted_yaml,
+ }
+ )
+
+ asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
+ )
+
+ staged_block = yaml.safe_load(ctx.last_workflow_yaml)["workflow_definition"]["blocks"][0]
+ assert staged_block["prompt"] == "Rebuild me and run me every turn"
+ assert "user_owned_goal" not in staged_block
+ assert "goal_needs_regeneration" not in staged_block
+
+ def test_inline_replace_workflow_redacts_a_scrub_value_carried_from_a_stored_goal(self, monkeypatch) -> None:
+ converted: list[str] = []
+
+ async def _process(**kwargs) -> SimpleNamespace:
+ converted.append(str(kwargs["workflow_yaml"]))
+ return SimpleNamespace(name="replacement", extra_http_headers=None, cdp_connect_headers=None)
+
+ monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._process_workflow_yaml", _process)
+
+ secret = "live-portal-password-9182"
+ prior_yaml = yaml.safe_dump(
+ {
+ "workflow_definition": {
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "get_invoice",
+ "prompt": f"Sign in with {secret} and download the invoice",
+ "user_owned_goal": True,
+ "goal_needs_regeneration": True,
+ "code": "await page.goto(url)\n",
+ }
+ ]
+ }
+ },
+ sort_keys=False,
+ )
+ submitted_yaml = yaml.safe_dump(
+ {
+ "workflow_definition": {
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "get_invoice",
+ "prompt": "Check the order status",
+ "code": 'await page.get_by_role("link", name="Invoice").click()\n',
+ }
+ ]
+ }
+ },
+ sort_keys=False,
+ )
+ ctx = _ctx(
+ block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,
+ last_workflow_yaml=prior_yaml,
+ request_policy=RequestPolicy(allow_update_workflow=True, allow_run_blocks=True),
+ secret_scrub_values=[secret],
+ )
+ result = _fake_run_result(
+ {
+ "type": "REPLACE_WORKFLOW",
+ "user_response": "REPLACE_WORKFLOW\nHere you go.",
+ "workflow_yaml": submitted_yaml,
+ }
+ )
+
+ asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
+ )
+
+ assert secret not in ctx.last_workflow_yaml
+ assert converted and secret not in converted[0]
+ staged_block = yaml.safe_load(ctx.last_workflow_yaml)["workflow_definition"]["blocks"][0]
+ assert "[REDACTED_SECRET]" in staged_block["prompt"]
+
def test_interrupted_draft_inline_replace_persists_before_emission(self, monkeypatch) -> None:
prior = SimpleNamespace(name="prior")
replacement = SimpleNamespace(name="replacement")
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index f18fc6c35..e9613fafd 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -11,7 +11,7 @@ import asyncio
import json
import textwrap
from types import SimpleNamespace
-from typing import NoReturn
+from typing import Any, NoReturn
from unittest.mock import AsyncMock
import pytest
@@ -19,6 +19,7 @@ import pytest
from skyvern.forge import app
from skyvern.forge.agent_functions import AgentFunction
from skyvern.forge.sdk.copilot import tools as tools_module
+from skyvern.forge.sdk.copilot.browser_code_contract import ExecutedBrowserCodeSourceResolution
from skyvern.forge.sdk.copilot.code_block_preflight import CodeBlockScanFinding
from skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy
from skyvern.forge.sdk.copilot.context import CopilotContext
@@ -74,9 +75,21 @@ def _ctx(
return ctx
-def _code_yaml(code: str, *, label: str = "submit_search", prompt: str | None = "The page shows the result.") -> str:
+def _code_yaml(
+ code: str,
+ *,
+ label: str = "submit_search",
+ prompt: str | None = "The page shows the result.",
+ user_owned_goal: bool | None = None,
+ goal_needs_regeneration: bool | None = None,
+) -> str:
indented = "\n".join(f" {line}" for line in textwrap.dedent(code).strip().splitlines())
prompt_line = f" prompt: {json.dumps(prompt, ensure_ascii=False)}\n" if prompt is not None else ""
+ owned_lines = ""
+ if user_owned_goal is not None:
+ owned_lines += f" user_owned_goal: {str(user_owned_goal).lower()}\n"
+ if goal_needs_regeneration is not None:
+ owned_lines += f" goal_needs_regeneration: {str(goal_needs_regeneration).lower()}\n"
return (
"title: Search\n"
"workflow_definition:\n"
@@ -84,6 +97,7 @@ def _code_yaml(code: str, *, label: str = "submit_search", prompt: str | None =
" - block_type: code\n"
f" label: {label}\n"
f"{prompt_line}"
+ f"{owned_lines}"
" code: |\n"
f"{indented}\n"
)
@@ -272,6 +286,324 @@ async def test_accept_path_does_not_synthesize_an_omitted_goal_prompt(monkeypatc
assert "prompt" not in persisted_block
+def _user_owned_goal_yaml(*, prompt: str, code: str, needs_regeneration: bool) -> str:
+ return _code_yaml(code, prompt=prompt, user_owned_goal=True, goal_needs_regeneration=needs_regeneration)
+
+
+async def test_accept_path_keeps_the_user_written_goal_in_the_staged_yaml(monkeypatch: pytest.MonkeyPatch) -> None:
+ human_goal = "Download last month's invoice as a PDF"
+ prior = _user_owned_goal_yaml(
+ prompt=human_goal,
+ code='return {"output": {"status": "pending"}}',
+ needs_regeneration=True,
+ )
+ _stub_successful_update(monkeypatch)
+ submitted = _code_yaml(_REBUILT_CODE, prompt="Check the order status")
+ ctx = _ctx(prior)
+
+ result = await _update_workflow(
+ {"workflow_yaml": submitted, "code_artifact_metadata": [_rebuild_artifact_row()]},
+ ctx,
+ allow_missing_credentials=True,
+ )
+
+ assert result["ok"] is True
+ staged_block = workflow_blocks(parse_workflow_yaml(ctx.staged_workflow_yaml))[0]
+ assert staged_block["prompt"] == human_goal
+ assert staged_block["code"].strip() == _REBUILT_CODE
+ assert staged_block["user_owned_goal"] is True
+ assert staged_block["goal_needs_regeneration"] is False
+ assert result["data"]["stored_goal_kept"] == ["submit_search"]
+
+
+_REBUILT_CODE = 'await page.get_by_role("link", name="Invoice").click()\nreturn {"records": [{"number": "123"}]}'
+
+
+def _rebuild_artifact_row(label: str = "submit_search") -> dict[str, object]:
+ return {
+ "block_label": label,
+ "artifact_id": f"code_artifact:{label}",
+ "declared_goal": "The invoice PDF is downloaded.",
+ "claimed_outcomes": [
+ {
+ "id": "claim:invoice",
+ "scope": "outcome",
+ "text": "The invoice number is read",
+ "status": "observed_not_verified",
+ "depends_on": ["dependency:page"],
+ "covered_criteria": ["criterion:invoice"],
+ "goal_value_paths": ["records[].number"],
+ "observation_refs": ["obs1"],
+ }
+ ],
+ "page_dependencies": [
+ {"id": "dependency:page", "scope": "page", "status": "observed_not_verified", "observation_refs": ["obs1"]}
+ ],
+ "completion_criteria": [{"id": "criterion:invoice", "text": "The invoice is found", "level": "terminal"}],
+ "terminal_verifier_expectations": [
+ {
+ "id": "expectation:invoice",
+ "text": "The invoice number is returned",
+ "criteria_ids": ["criterion:invoice"],
+ "goal_value_paths": ["records[].number"],
+ }
+ ],
+ "observation_refs": [
+ {
+ "observation_ref": "obs1",
+ "dependency_id": "dependency:page",
+ "status": "observed_not_verified",
+ "source_tool": "scout_interaction",
+ }
+ ],
+ }
+
+
+async def test_a_metadata_row_that_names_the_block_clears_the_rebuild_even_when_normalization_drops_it(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ prior = _user_owned_goal_yaml(
+ prompt="Download last month's invoice as a PDF",
+ code='return {"output": {"status": "pending"}}',
+ needs_regeneration=True,
+ )
+ _stub_successful_update(monkeypatch)
+ submitted = _code_yaml('await page.get_by_role("link", name="Invoice").click()', prompt="Check the order status")
+ ctx = _ctx(prior)
+ bare_row = {"block_label": "submit_search", "declared_goal": "The invoice PDF is downloaded."}
+
+ result = await _update_workflow(
+ {"workflow_yaml": submitted, "code_artifact_metadata": [bare_row]},
+ ctx,
+ allow_missing_credentials=True,
+ )
+
+ assert result["ok"] is True
+ assert "submit_search" not in (ctx.code_artifact_metadata or {})
+ assert workflow_blocks(parse_workflow_yaml(ctx.staged_workflow_yaml))[0]["goal_needs_regeneration"] is False
+
+
+async def test_accept_path_leaves_the_rebuild_pending_when_no_artifact_claims_the_block(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ human_goal = "Download last month's invoice as a PDF"
+ prior = _user_owned_goal_yaml(
+ prompt=human_goal,
+ code='return {"output": {"status": "pending"}}',
+ needs_regeneration=True,
+ )
+ _stub_successful_update(monkeypatch)
+ submitted = _code_yaml('await page.get_by_role("link", name="Invoice").click()', prompt="Check the order status")
+ ctx = _ctx(prior)
+
+ result = await _update_workflow(
+ {"workflow_yaml": submitted, "code_artifact_metadata": []},
+ ctx,
+ allow_missing_credentials=True,
+ )
+
+ assert result["ok"] is True
+ staged_block = workflow_blocks(parse_workflow_yaml(ctx.staged_workflow_yaml))[0]
+ assert staged_block["prompt"] == human_goal
+ assert staged_block["goal_needs_regeneration"] is True
+
+
+async def test_a_block_scoped_code_edit_clears_the_rebuild_without_an_artifact_row(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ human_goal = "Download last month's invoice as a PDF"
+ prior = _user_owned_goal_yaml(
+ prompt=human_goal,
+ code='return {"output": {"status": "pending"}}',
+ needs_regeneration=True,
+ )
+ _stub_successful_update(monkeypatch)
+ submitted = _code_yaml('await page.get_by_role("link", name="Invoice").click()', prompt="Check the order status")
+ ctx = _ctx(prior)
+
+ result = await _update_workflow(
+ {
+ "workflow_yaml": submitted,
+ "_preserve_code_block_associations": True,
+ "_rebuilt_block_labels": ["submit_search"],
+ },
+ ctx,
+ allow_missing_credentials=True,
+ )
+
+ assert result["ok"] is True
+ staged_block = workflow_blocks(parse_workflow_yaml(ctx.staged_workflow_yaml))[0]
+ assert staged_block["prompt"] == human_goal
+ assert staged_block["goal_needs_regeneration"] is False
+
+
+async def _edit_block(ctx: CopilotContext, **edit: object) -> dict[str, object]:
+ raw = await tools_module.edit_block_tool.on_invoke_tool(
+ SimpleNamespace(context=ctx, tool_name="edit_block"),
+ json.dumps({"label": "submit_search", **edit}),
+ )
+ return json.loads(raw)
+
+
+def _staged_rebuild_flag(ctx: CopilotContext) -> bool:
+ return bool(workflow_blocks(parse_workflow_yaml(ctx.staged_workflow_yaml))[0]["goal_needs_regeneration"])
+
+
+async def test_edit_block_clears_the_rebuild_only_when_it_replaces_the_code(monkeypatch: pytest.MonkeyPatch) -> None:
+ prior = _user_owned_goal_yaml(
+ prompt="Download last month's invoice as a PDF",
+ code='return {"output": {"status": "pending"}}',
+ needs_regeneration=True,
+ )
+ _stub_successful_update(monkeypatch)
+ monkeypatch.setattr(tools_module, "_get_prior_workflow_definition", AsyncMock(return_value=None))
+
+ ctx = _ctx(prior)
+ result = await _edit_block(ctx, fields={"continue_on_failure": True})
+ assert result["ok"] is True, result
+ assert _staged_rebuild_flag(ctx) is True
+
+ ctx = _ctx(prior)
+ result = await _edit_block(ctx, expected_code='"pending"', replacement_code='"done"')
+ assert result["ok"] is True, result
+ assert _staged_rebuild_flag(ctx) is False
+
+ ctx = _ctx(prior)
+ result = await _edit_block(ctx, fields={"code": 'return {"output": {"status": "done"}}'})
+ assert result["ok"] is True, result
+ assert _staged_rebuild_flag(ctx) is False
+
+
+async def test_promoting_an_executed_source_clears_the_rebuild_for_that_block(monkeypatch: pytest.MonkeyPatch) -> None:
+ prior = _user_owned_goal_yaml(
+ prompt="Download last month's invoice as a PDF",
+ code='return {"output": {"status": "pending"}}',
+ needs_regeneration=True,
+ )
+ _stub_successful_update(monkeypatch)
+ executed = 'await page.get_by_role("link", name="Invoice").click()'
+ monkeypatch.setattr(
+ tools_module,
+ "resolve_executed_browser_code_source",
+ lambda _ctx, _reference: ExecutedBrowserCodeSourceResolution(status="valid", source=executed),
+ )
+ ctx = _ctx(prior)
+ update_params: dict[str, Any] = {"workflow_yaml": _code_yaml("", prompt="Check the order status")}
+
+ assert tools_module._promote_executed_sources(ctx, update_params, {"submit_search": "ref"}) is None
+ result = await _update_workflow(update_params, ctx, allow_missing_credentials=True)
+
+ assert result["ok"] is True, result
+ assert _single_code(ctx.staged_workflow_yaml).strip() == executed
+ assert _staged_rebuild_flag(ctx) is False
+
+
+async def test_a_goal_the_copilot_cannot_build_from_is_never_refused_at_author_time(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ unbuildable_goal = "Open the Quarterly Rebates tab that this site does not have and export it"
+ persisted: list[str] = []
+ _stub_successful_update(monkeypatch, persisted)
+ submitted = _code_yaml('return {"output": {"status": "done"}}', prompt=unbuildable_goal)
+ ctx = _ctx()
+
+ result = await _update_workflow(
+ {"workflow_yaml": submitted, "code_artifact_metadata": []},
+ ctx,
+ allow_missing_credentials=True,
+ )
+
+ assert result["ok"] is True
+ assert "error" not in result
+ assert persisted
+ assert workflow_blocks(parse_workflow_yaml(ctx.staged_workflow_yaml))[0]["prompt"] == unbuildable_goal
+
+
+async def test_accept_path_leaves_a_model_owned_goal_alone_when_a_prior_goal_differs(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ prior = _code_yaml('return {"output": {"status": "pending"}}', prompt="Check the order status")
+ _stub_successful_update(monkeypatch)
+ submitted = _code_yaml('return {"output": {"status": "done"}}', prompt="Report the final order status")
+ ctx = _ctx(prior)
+
+ result = await _update_workflow(
+ {"workflow_yaml": submitted, "code_artifact_metadata": []},
+ ctx,
+ allow_missing_credentials=True,
+ )
+
+ assert result["ok"] is True
+ assert ctx.staged_workflow_yaml == submitted
+ assert "stored_goal_kept" not in result["data"]
+
+
+async def test_a_scrub_value_inside_a_stored_goal_never_reaches_the_persisted_yaml(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ persisted: list[str] = []
+ _stub_successful_update(monkeypatch, persisted)
+ secret = "live-portal-password-9182"
+ prior = _user_owned_goal_yaml(
+ prompt=f"Sign in with {secret} and download the invoice",
+ code='return {"output": {"status": "pending"}}',
+ needs_regeneration=True,
+ )
+ ctx = _ctx(prior)
+ register_secret_scrub_value(ctx, secret)
+ submitted = _code_yaml(
+ 'await page.get_by_role("link", name="Invoice").click()',
+ prompt="Check the order status",
+ )
+
+ result = await _update_workflow(
+ {"workflow_yaml": submitted, "code_artifact_metadata": []},
+ ctx,
+ allow_missing_credentials=True,
+ )
+
+ assert result["ok"] is True
+ assert secret not in ctx.staged_workflow_yaml
+ assert persisted and secret not in persisted[0]
+ staged_block = workflow_blocks(parse_workflow_yaml(ctx.staged_workflow_yaml))[0]
+ assert REDACTED_SECRET_PLACEHOLDER in staged_block["prompt"]
+
+
+async def test_a_renamed_block_reports_the_user_owned_goal_it_dropped(monkeypatch: pytest.MonkeyPatch) -> None:
+ prior = _user_owned_goal_yaml(
+ prompt="Download last month's invoice as a PDF",
+ code='return {"output": {"status": "pending"}}',
+ needs_regeneration=True,
+ )
+ _stub_successful_update(monkeypatch)
+ submitted = _code_yaml(
+ 'return {"output": {"status": "done"}}',
+ label="fetch_invoice",
+ prompt="Fetch the invoice",
+ )
+ ctx = _ctx(prior)
+
+ result = await _update_workflow(
+ {"workflow_yaml": submitted, "code_artifact_metadata": []},
+ ctx,
+ allow_missing_credentials=True,
+ )
+
+ assert result["ok"] is True
+ assert result["data"]["stored_goal_dropped"] == ["submit_search"]
+ assert "submit_search" in result["data"]["stored_goal_dropped_message"]
+
+
@pytest.mark.asyncio
async def test_requested_output_contract_does_not_rewrite_model_code(monkeypatch: pytest.MonkeyPatch) -> None:
_stub_successful_update(monkeypatch)
diff --git a/tests/unit/test_copilot_turn_context.py b/tests/unit/test_copilot_turn_context.py
index 1bab1f93e..aa73f7c47 100644
--- a/tests/unit/test_copilot_turn_context.py
+++ b/tests/unit/test_copilot_turn_context.py
@@ -508,6 +508,72 @@ def test_a_filename_shaped_like_a_credential_is_redacted_while_ordinary_names_su
assert "[REDACTED_SECRET].csv (file_id: file_7)" in rendered
+def _goal_workflow_yaml(*, goal_needs_regeneration: bool | None) -> str:
+ stale = (
+ ""
+ if goal_needs_regeneration is None
+ else f" goal_needs_regeneration: {str(goal_needs_regeneration).lower()}\n"
+ )
+ return (
+ "workflow_definition:\n"
+ " blocks:\n"
+ " - block_type: code\n"
+ " label: get_invoice\n"
+ " prompt: Download last month's invoice\n"
+ " user_owned_goal: true\n"
+ f"{stale}"
+ " code: |\n"
+ " await page.goto(url)\n"
+ )
+
+
+def test_a_block_awaiting_a_goal_rebuild_reaches_the_prompt_as_a_fact() -> None:
+ packet = TurnContextAssembler().assemble(
+ TurnContextInputs(
+ request_policy=RequestPolicy(),
+ user_message="how long does this take to run?",
+ workflow_yaml=_goal_workflow_yaml(goal_needs_regeneration=True),
+ )
+ )
+
+ assert packet.user_goal_context is not None
+ assert packet.user_goal_context.block_labels == ["get_invoice"]
+
+ rendered = _build_user_context(
+ workflow_yaml="workflow_definition:\n blocks: []",
+ chat_history_text="",
+ global_llm_context="",
+ debug_run_info_text="",
+ user_message="how long does this take to run?",
+ user_goal_summary=packet.user_goal_context.rendered_summary,
+ )
+
+ assert "USER-WRITTEN GOALS NOT APPLIED YET" in rendered
+ assert "get_invoice" in rendered
+
+
+def test_a_user_owned_goal_already_rebuilt_carries_no_prompt_section() -> None:
+ packet = TurnContextAssembler().assemble(
+ TurnContextInputs(
+ request_policy=RequestPolicy(),
+ user_message="how long does this take to run?",
+ workflow_yaml=_goal_workflow_yaml(goal_needs_regeneration=False),
+ )
+ )
+
+ assert packet.user_goal_context is None
+
+ rendered = _build_user_context(
+ workflow_yaml="workflow_definition:\n blocks: []",
+ chat_history_text="",
+ global_llm_context="",
+ debug_run_info_text="",
+ user_message="how long does this take to run?",
+ )
+
+ assert "USER-WRITTEN GOALS NOT APPLIED YET" not in rendered
+
+
def test_a_secret_in_an_attachment_filename_never_reaches_the_prompt() -> None:
"""The filename is user-chosen text that bypasses the message safety screen, and it is replayed
on every later turn — so the prompt boundary has to redact it like every other value."""
diff --git a/tests/unit/test_copilot_workflow_update.py b/tests/unit/test_copilot_workflow_update.py
index eb2aaf10e..8af2fb954 100644
--- a/tests/unit/test_copilot_workflow_update.py
+++ b/tests/unit/test_copilot_workflow_update.py
@@ -17,6 +17,7 @@ from skyvern.exceptions import WorkflowNotFound
from skyvern.forge import app
from skyvern.forge.sdk.copilot import tools as tools_module
from skyvern.forge.sdk.copilot import workflow_yaml as workflow_yaml_module
+from skyvern.forge.sdk.copilot.code_block_steps import UserOwnedGoalCarry, user_owned_goal_carry_disclosure
from skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy
from skyvern.forge.sdk.copilot.context import CopilotContext
from skyvern.forge.sdk.copilot.output_utils import sanitize_tool_result_for_llm
@@ -359,6 +360,25 @@ def test_combined_tool_result_carries_sheet_tab_resolution() -> None:
assert carried["data"]["google_sheet_tab_resolution"] == [fact]
+def test_combined_tool_result_carries_every_submission_rewrite_disclosure() -> None:
+ goal_disclosure = user_owned_goal_carry_disclosure(UserOwnedGoalCarry("", ["get_invoice"], ["old_label"]))
+ assert set(goal_disclosure) >= {"stored_goal_kept", "stored_goal_dropped"}
+ update_result: dict[str, Any] = {
+ "ok": True,
+ "data": {
+ "stored_code_rewritten": ["login"],
+ **goal_disclosure,
+ "google_connection_resolution": [{"label": "write_row", "resolved_to": "cred_1"}],
+ },
+ }
+ run_result: dict[str, Any] = {"ok": True, "data": {"message": "Ran 1 block."}}
+
+ carried = carry_author_time_findings(update_result, run_result)["data"]
+
+ for key, value in update_result["data"].items():
+ assert carried[key] == value
+
+
_SAVE_CLAIM_PHRASES = ("updated successfully", "has been saved", "saved the workflow", "workflow was saved")
diff --git a/tests/unit/test_workflow_copilot_workflow_state.py b/tests/unit/test_workflow_copilot_workflow_state.py
index b2a20f34f..8838101f8 100644
--- a/tests/unit/test_workflow_copilot_workflow_state.py
+++ b/tests/unit/test_workflow_copilot_workflow_state.py
@@ -18,7 +18,7 @@ from skyvern.forge.sdk.routes.workflow_copilot import (
_prior_copilot_workflow_yaml,
)
from skyvern.forge.sdk.schemas.workflow_copilot import WorkflowCopilotChatRequest
-from skyvern.forge.sdk.workflow.models.block import FileDownloadBlock
+from skyvern.forge.sdk.workflow.models.block import CodeBlock, FileDownloadBlock
from skyvern.forge.sdk.workflow.models.parameter import OutputParameter, WorkflowParameter, WorkflowParameterType
from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowDefinition
from skyvern.schemas.proxy_location import GeoTarget
@@ -230,6 +230,39 @@ def _code_block_yaml(*, manifest: object = ...) -> str:
return yaml.safe_dump({"workflow_definition": {"parameters": [], "blocks": [block]}}, sort_keys=False)
+def test_a_saved_user_owned_goal_reaches_the_copilot_yaml_the_next_turn_reads() -> None:
+ now = datetime.now(timezone.utc)
+ workflow = Workflow(
+ workflow_id="w_saved",
+ organization_id="o_test",
+ title="Saved workflow",
+ workflow_permanent_id="wpid_test",
+ version=1,
+ is_saved_task=False,
+ created_at=now,
+ modified_at=now,
+ workflow_definition=WorkflowDefinition(
+ parameters=[],
+ blocks=[
+ CodeBlock(
+ label="get_invoice",
+ output_parameter=_output_parameter(now),
+ code="await page.goto(url)\n",
+ prompt="Download last month's invoice",
+ user_owned_goal=True,
+ goal_needs_regeneration=True,
+ )
+ ],
+ ),
+ )
+
+ block = yaml.safe_load(workflow_to_copilot_yaml(workflow))["workflow_definition"]["blocks"][0]
+
+ assert block["prompt"] == "Download last month's invoice"
+ assert block["user_owned_goal"] is True
+ assert block["goal_needs_regeneration"] is True
+
+
def test_code_block_regeneration_preserves_omitted_manifest_by_label() -> None:
prior = _code_block_yaml(manifest={"ACCOUNT_LOCKED": "Account is locked"})