Skip to content

Automate browser based workflows with AI

Shallow-fetch history for changed-file CI jobs (#8854)

Aaron Pereza1ccbcd84bdf
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.blockActions.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.blockActions.test.tsx
index 38bf6e538..914b2b56b 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.blockActions.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.blockActions.test.tsx
@@ -118,12 +118,34 @@ vi.mock("react-router-dom", async (importOriginal) => {
};
});
-// Unrelated to this file's tests; the real hook needs a QueryClientProvider
-// this harness doesn't set up.
+// The real hook needs a QueryClientProvider this harness doesn't set up.
+const { focusedRun, reopenEditor } = vi.hoisted(() => ({
+ focusedRun: { current: undefined as Record<string, unknown> | undefined },
+ reopenEditor: vi.fn(),
+}));
vi.mock("../hooks/useWorkflowRunQuery", () => ({
- useWorkflowRunQuery: () => ({ data: undefined }),
+ useWorkflowRunQuery: ({ workflowRunId }: { workflowRunId?: string }) => ({
+ data: workflowRunId ? focusedRun.current : undefined,
+ }),
}));
+vi.mock(
+ "@/routes/workflows/studio/StudioPaneDefaultsContext",
+ async (importOriginal) => {
+ const actual =
+ await importOriginal<
+ typeof import("@/routes/workflows/studio/StudioPaneDefaultsContext")
+ >();
+ return {
+ ...actual,
+ useStudioPaneDefaults: () => ({
+ ...actual.useStudioPaneDefaults(),
+ reopenEditor,
+ }),
+ };
+ },
+);
+
vi.mock("@/routes/workflows/editor/recording/RecordingPanel", () => ({
RecordingPanel: () => <div data-testid="recording-chapter" />,
}));
@@ -176,7 +198,11 @@ const BOOLEAN_FLAGS: Record<string, boolean> = {
CODE_BLOCK_ACCESS: false,
};
-type ChatProps = { docked?: boolean; portalTarget?: HTMLElement | null };
+type ChatProps = {
+ docked?: boolean;
+ portalTarget?: HTMLElement | null;
+ workflowRunId?: string | null;
+};
function chatUi(props: ChatProps = {}) {
return (
@@ -239,6 +265,8 @@ const runStartedFrame = (overrides: Partial<Record<string, unknown>> = {}) => ({
beforeEach(() => {
useRecordingStore.getState().reset();
switchStudioRun.mockClear();
+ reopenEditor.mockClear();
+ focusedRun.current = undefined;
HTMLElement.prototype.scrollIntoView = vi.fn();
HTMLElement.prototype.scrollTo = vi.fn();
streamCalls.length = 0;
@@ -627,6 +655,69 @@ describe("WorkflowCopilotChat — studio run focus", () => {
});
});
+describe("WorkflowCopilotChat — Editor returns after the turn's run", () => {
+ it("reopens Editor once when the focused run's terminal outcome lands", async () => {
+ await renderChat({ ...makeDockedProps(), workflowRunId: "wr_1" });
+ await submit("build a workflow");
+ streamCalls[0]!.onMessage(runStartedFrame());
+ streamCalls[0]!.onMessage(runOutcomeFrame());
+ expect(reopenEditor).not.toHaveBeenCalled();
+
+ streamCalls[0]!.onMessage(runOutcomeFrame({ verdict: "demonstrated" }));
+ streamCalls[0]!.onMessage(runOutcomeFrame({ verdict: "demonstrated" }));
+ expect(reopenEditor).toHaveBeenCalledTimes(1);
+ });
+
+ it("reopens Editor once the focus lands when the run ended before it did", async () => {
+ const props = makeDockedProps();
+ const view = await renderChat(props);
+ await submit("build a workflow");
+ streamCalls[0]!.onMessage(runStartedFrame());
+ streamCalls[0]!.onMessage(runOutcomeFrame({ verdict: "demonstrated" }));
+ expect(reopenEditor).not.toHaveBeenCalled();
+ view.rerender(chatUi({ ...props, workflowRunId: "wr_1" }));
+ expect(reopenEditor).toHaveBeenCalledTimes(1);
+ });
+
+ it("leaves the panes alone when the user has switched to another run", async () => {
+ const props = { ...makeDockedProps(), workflowRunId: "wr_1" };
+ const view = await renderChat(props);
+ await submit("build a workflow");
+ streamCalls[0]!.onMessage(runStartedFrame());
+ view.rerender(chatUi({ ...props, workflowRunId: "wr_2" }));
+ streamCalls[0]!.onMessage(runOutcomeFrame({ verdict: "demonstrated" }));
+ focusedRun.current = { workflow_run_id: "wr_1", status: "completed" };
+ view.rerender(chatUi({ ...props, workflowRunId: "wr_1" }));
+ expect(reopenEditor).not.toHaveBeenCalled();
+ });
+
+ it("does not reopen Editor when the user returns to a run that ended while they were away", async () => {
+ const props = { ...makeDockedProps(), workflowRunId: "wr_1" };
+ const view = await renderChat(props);
+ await submit("build a workflow");
+ streamCalls[0]!.onMessage(runStartedFrame());
+ view.rerender(chatUi({ ...props, workflowRunId: "wr_2" }));
+ await act(async () => {
+ streamCalls[0]!.reject(new Error("network"));
+ });
+ focusedRun.current = { workflow_run_id: "wr_1", status: "completed" };
+ view.rerender(chatUi({ ...props, workflowRunId: "wr_1" }));
+ await act(async () => {});
+ expect(reopenEditor).not.toHaveBeenCalled();
+ });
+
+ it("reopens Editor from the run status when the stream is severed first", async () => {
+ await renderChat({ ...makeDockedProps(), workflowRunId: "wr_1" });
+ await submit("build a workflow");
+ streamCalls[0]!.onMessage(runStartedFrame());
+ focusedRun.current = { workflow_run_id: "wr_1", status: "completed" };
+ await act(async () => {
+ streamCalls[0]!.reject(new Error("network"));
+ });
+ await waitFor(() => expect(reopenEditor).toHaveBeenCalledTimes(1));
+ });
+});
+
describe("WorkflowCopilotChat — build follow", () => {
const focusBlock = vi.fn();
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx
index f1561a4ed..54729a0a2 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx
@@ -59,6 +59,7 @@ const {
modalEditingCredentialId,
modalDefaultTotpType,
toastFn,
+ realPopover,
} = vi.hoisted(() => {
const calls: StreamCall[] = [];
const streaming = vi.fn(
@@ -139,6 +140,7 @@ const {
modalEditingCredentialId: { current: undefined as string | undefined },
modalDefaultTotpType: { current: undefined as string | undefined },
toastFn: vi.fn(),
+ realPopover: { current: false },
};
});
@@ -160,35 +162,43 @@ vi.mock("@/components/ui/use-toast", () => ({ toast: toastFn }));
// Unlike the card unit test's always-render stub, this one honors `open` and wires the trigger —
// WorkflowCopilotHistory also renders a Popover whose (closed) content pulls react-query, so an
// unconditional PopoverContent would force-mount it and crash with "No QueryClient".
-vi.mock("@/components/ui/popover", async () => {
+// `realPopover` swaps in the Radix popover for a test that needs its Escape handling.
+vi.mock("@/components/ui/popover", async (importOriginal) => {
const React = await import("react");
+ const actual =
+ await importOriginal<typeof import("@/components/ui/popover")>();
const OpenCtx = React.createContext<{
open: boolean;
setOpen: (value: boolean) => void;
}>({ open: false, setOpen: () => {} });
return {
- Popover: ({
- open,
- onOpenChange,
- children,
- }: {
- open?: boolean;
- onOpenChange?: (value: boolean) => void;
- children?: ReactNode;
- }) => (
- <OpenCtx.Provider
- value={{ open: Boolean(open), setOpen: onOpenChange ?? (() => {}) }}
- >
- {children}
- </OpenCtx.Provider>
- ),
- PopoverTrigger: ({ children }: { children?: ReactNode }) => {
+ Popover: (props: ComponentProps<typeof actual.Popover>) =>
+ realPopover.current ? (
+ <actual.Popover {...props} />
+ ) : (
+ <OpenCtx.Provider
+ value={{
+ open: Boolean(props.open),
+ setOpen: props.onOpenChange ?? (() => {}),
+ }}
+ >
+ {props.children}
+ </OpenCtx.Provider>
+ ),
+ PopoverTrigger: (props: ComponentProps<typeof actual.PopoverTrigger>) => {
const { open, setOpen } = React.useContext(OpenCtx);
- return <div onClick={() => setOpen(!open)}>{children}</div>;
+ return realPopover.current ? (
+ <actual.PopoverTrigger {...props} />
+ ) : (
+ <div onClick={() => setOpen(!open)}>{props.children}</div>
+ );
},
- PopoverContent: ({ children }: { children?: ReactNode }) => {
+ PopoverContent: (props: ComponentProps<typeof actual.PopoverContent>) => {
const { open } = React.useContext(OpenCtx);
- return open ? <div>{children}</div> : null;
+ if (realPopover.current) {
+ return <actual.PopoverContent {...props} />;
+ }
+ return open ? <div>{props.children}</div> : null;
},
};
});
@@ -528,6 +538,7 @@ beforeEach(() => {
toastFn.mockClear();
credentialsData.current = [];
credsFail.current = false;
+ realPopover.current = false;
modalOverrideType.current = undefined;
modalDefaultTestUrl.current = undefined;
modalEditingCredentialId.current = undefined;
@@ -2279,6 +2290,38 @@ describe("WorkflowCopilotChat — credential card wiring", () => {
expect(postStreaming).toHaveBeenCalledTimes(1);
});
+ it("Escape closes the open login picker without stopping the turn", async () => {
+ realPopover.current = true;
+ credentialsData.current = [
+ { credential_id: "cred-hn", name: "HN Login", tested_url: null },
+ ];
+ await renderChat();
+ await submit("build me a workflow");
+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));
+ await act(async () => {
+ streamCalls[0]!.onMessage(turnStart());
+ streamCalls[0]!.onMessage(credentialFrame());
+ });
+ await act(async () => {
+ fireEvent.click(await screen.findByRole("combobox"));
+ });
+
+ await act(async () => {
+ fireEvent.keyDown(screen.getByPlaceholderText("Search credentials..."), {
+ key: "Escape",
+ });
+ });
+
+ await waitFor(() =>
+ expect(screen.queryByPlaceholderText("Search credentials...")).toBeNull(),
+ );
+ expect(sansApiPost).not.toHaveBeenCalledWith(
+ "/workflow/copilot/cancel",
+ expect.anything(),
+ expect.anything(),
+ );
+ });
+
it("shows the full org credential list on a pause ask (not just the frame's candidates) and answers via the typed POST", async () => {
credentialsData.current = [
{ credential_id: "cred-abc", name: "abc", tested_url: null },
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
index 5ff95b55b..c494fb068 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
@@ -183,6 +183,8 @@ import {
type RecordingRefinementStatus,
} from "./RecordingRefinementProgressCard";
import { useRunLifecycleAnnouncements } from "./useRunLifecycleAnnouncements";
+import { useWorkflowRunQuery } from "../hooks/useWorkflowRunQuery";
+import { runIsLogicallyFinal } from "@/routes/workflows/workflowRun/runRetryState";
import { useHistoryLoad } from "./useHistoryLoad";
import { ConfirmCard, shouldShowConfirmCard } from "./cards/ConfirmCard";
import {
@@ -263,6 +265,7 @@ import {
liveSearch,
} from "@/routes/workflows/studio/liveSearch";
import { useStudioPanes } from "@/routes/workflows/studio/useStudioPanes";
+import { useStudioPaneDefaults } from "@/routes/workflows/studio/StudioPaneDefaultsContext";
import { useRecordingStore } from "@/store/useRecordingStore";
import { useRecordingRefinementEvidenceStore } from "@/store/RecordingRefinementEvidenceStore";
import { captureRecordBrowser } from "@/util/recordBrowserTelemetry";
@@ -2280,6 +2283,7 @@ export function WorkflowCopilotChat({
// Focusing the turn's run is the copilot acting for the user, not a
// navigation they asked for, so it must not add a Back step.
const { resolveLivePanes } = useStudioPanes();
+ const { reopenEditor } = useStudioPaneDefaults();
const switchStudioRun = useSwitchStudioRun({
replace: true,
systemFocus: true,
@@ -2557,15 +2561,61 @@ export function WorkflowCopilotChat({
actionPollRef.current.forEach((timer) => clearInterval(timer));
actionPollRef.current.clear();
}, []);
+ const releasedTurnRunId = useRef<string | null>(null);
+ const arrivedTurnRunId = useRef<string | null>(null);
+ const pendingReleaseRunId = useRef<string | null>(null);
+ const studioRunIdRef = useRef(workflowRunId);
+ studioRunIdRef.current = workflowRunId;
const focusTurnRun = useCallback(
(runId: string | null | undefined) => {
if (!docked || !runId) return;
if (focusedTurnRunId.current === runId) return;
focusedTurnRunId.current = runId;
+ if (studioRunIdRef.current === runId) arrivedTurnRunId.current = runId;
switchStudioRun(runId);
},
[docked, switchStudioRun],
);
+ // A narrow stage drops Editor for the run's Browser; give it back once the
+ // run this turn focused is over, once per run, and only while the studio
+ // shows that run. An end that beats the focus navigation waits for it; once
+ // the user leaves the run the release is spent, so returning later never
+ // reopens Editor.
+ const releaseTurnRun = useCallback(
+ (runId: string) => {
+ if (
+ focusedTurnRunId.current !== runId ||
+ releasedTurnRunId.current === runId
+ )
+ return;
+ if (studioRunIdRef.current !== runId) {
+ pendingReleaseRunId.current = runId;
+ return;
+ }
+ releasedTurnRunId.current = runId;
+ reopenEditor();
+ },
+ [reopenEditor],
+ );
+ useEffect(() => {
+ const focused = focusedTurnRunId.current;
+ if (!focused) return;
+ if (workflowRunId === focused) {
+ arrivedTurnRunId.current = focused;
+ if (pendingReleaseRunId.current === focused) releaseTurnRun(focused);
+ } else if (arrivedTurnRunId.current === focused) {
+ releasedTurnRunId.current = focused;
+ }
+ }, [workflowRunId, releaseTurnRun]);
+ // The polled run status also ends it, for a stream severed before run_outcome.
+ const { data: focusedRunData } = useWorkflowRunQuery({
+ workflowRunId: docked ? (workflowRunId ?? undefined) : undefined,
+ });
+ useEffect(() => {
+ if (focusedRunData && runIsLogicallyFinal(focusedRunData)) {
+ releaseTurnRun(focusedRunData.workflow_run_id);
+ }
+ }, [focusedRunData, releaseTurnRun]);
useEffect(() => {
const onPointerDown = (event: PointerEvent) => {
if (!buildFollowEngaged.current) return;
@@ -8988,6 +9038,7 @@ export function WorkflowCopilotChat({
// Terminal verdict: one convergent fetch, then stop polling.
void fetchRecordedActions(payload.workflow_run_id);
finalizeRecordedActionsPoll(payload.workflow_run_id);
+ releaseTurnRun(payload.workflow_run_id);
}
}
return false;
@@ -9349,6 +9400,7 @@ export function WorkflowCopilotChat({
recordingFocusOpen,
pendingAttachments,
focusTurnRun,
+ releaseTurnRun,
followBuildLabel,
inputValue,
questionInteractions,
diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/CredentialCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/CredentialCard.tsx
index c91323040..e0be00b30 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/cards/CredentialCard.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/CredentialCard.tsx
@@ -395,7 +395,12 @@ function CredentialPicker({
<ChevronDownIcon className="size-4 shrink-0 opacity-60" />
</Button>
</PopoverTrigger>
- <PopoverContent className={`${contentClassName} p-0`} align="start">
+ <PopoverContent
+ className={`${contentClassName} p-0`}
+ align="start"
+ // The chat stops the running turn on any Escape that reaches the window.
+ onEscapeKeyDown={(event) => event.stopPropagation()}
+ >
<Command shouldFilter={false}>
<CommandInput
placeholder="Search credentials..."
diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
index 678f83a2f..0fb8966fc 100644
--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
@@ -243,12 +243,17 @@ import {
type WorkflowUpdateOptions,
} from "../copilot/WorkflowCopilotChat";
import { useStudioRunId } from "../studio/useStudioRunId";
+import { SYSTEM_RUN_FOCUS_PARAM } from "../studio/panes";
import { copilotRunId } from "./copilotRunId";
import {
shouldOpenCopilotPaneForHandoff,
useDiscoverCopilotPromptRecovery,
withoutDiscoverViaParam,
} from "../discoverCopilotHandoff";
+import {
+ initialEditorAutoOpenState,
+ shouldAutoOpenEditor,
+} from "./editorAutoOpen";
import { useStudioShellContext } from "../studio/StudioShellContext";
import { StudioShellPanelPortal } from "../studio/StudioShellPanelPortal";
import { useRecordingLauncherStore } from "@/store/useRecordingLauncherStore";
@@ -461,13 +466,29 @@ function CopyText({ className, text }: { className?: string; text: string }) {
// eslint-disable-next-line react-refresh/only-export-components -- Exercise the production apply callback without mounting the entire workspace.
export function useWorkspaceCopilotUpdate({
applyWorkflowUpdate,
+ embedded = false,
+ initialBlockCount = 0,
}: {
applyWorkflowUpdate: (
workflow: WorkflowVersion,
options: WorkflowUpdateOptions & { userDriven: boolean },
) => boolean | void;
+ embedded?: boolean;
+ initialBlockCount?: number;
}) {
const logging = useLogging();
+ const { reopenEditor } = useStudioPaneDefaults();
+ // The studio shell remounts Workspace per workflow, so this arms only for an
+ // agent that had no blocks when it opened.
+ const editorAutoOpenStateRef = useRef(
+ initialEditorAutoOpenState(initialBlockCount),
+ );
+ // A run the user opened owns the stage; Copilot's own run focus does not.
+ const [searchParams] = useSearchParams();
+ const studioRunId = useStudioRunId();
+ const inspectingUserRunRef = useRef(false);
+ inspectingUserRunRef.current =
+ Boolean(studioRunId) && !searchParams.has(SYSTEM_RUN_FOCUS_PARAM);
return (workflowData: WorkflowVersion, options?: WorkflowUpdateOptions) => {
try {
// All Copilot-driven applies are user edits (mid-turn draft, accept,
@@ -478,6 +499,17 @@ export function useWorkspaceCopilotUpdate({
false
)
throw new Error("The editor refused the Copilot update");
+ const { fire, nextState } = shouldAutoOpenEditor(
+ editorAutoOpenStateRef.current,
+ {
+ embedded,
+ applied: options?.applied,
+ midTurnDraft: options?.midTurnDraft,
+ blockCount: workflowData.workflow_definition.blocks.length,
+ },
+ );
+ editorAutoOpenStateRef.current = nextState;
+ if (fire && !inspectingUserRunRef.current) reopenEditor();
} catch (error) {
console.error("Failed to parse and apply agent", error, workflowData);
logging.error("Copilot agent apply failed", {
@@ -2213,6 +2245,8 @@ function Workspace({
const handleCopilotWorkflowUpdate = useWorkspaceCopilotUpdate({
applyWorkflowUpdate,
+ embedded,
+ initialBlockCount: workflow.workflow_definition.blocks.length,
});
const reportStudioPaneCrash = (
pane: string,
diff --git a/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.test.ts b/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.test.ts
index ceaf56516..1c092e552 100644
--- a/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.test.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.test.ts
@@ -29,14 +29,15 @@ describe("shouldAutoOpenEditor", () => {
expect(result.nextState).toEqual({ armed: false });
});
- it("does not fire for a draft (applied undefined)", () => {
+ it("fires on the first mid-turn draft that lands blocks in studio", () => {
const result = shouldAutoOpenEditor(armed, {
embedded: true,
applied: undefined,
+ midTurnDraft: true,
blockCount: 2,
});
- expect(result.fire).toBe(false);
- expect(result.nextState).toEqual(armed);
+ expect(result.fire).toBe(true);
+ expect(result.nextState).toEqual(disarmed);
});
it("does not fire for a snap-back (applied false)", () => {
diff --git a/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.ts b/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.ts
index ee2ab3c1e..abe7da0f0 100644
--- a/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.ts
@@ -1,8 +1,7 @@
// One-shot arm/fire decision for auto-opening the Editor pane when Copilot's
-// first applied build lands blocks on a previously-empty agent (SKY-11763).
-// Drafts/snap-backs (`applied` false/undefined) never fire it, and once it
-// fires it stays disarmed for the rest of the state's lifetime — closing the
-// pane afterward has no bearing since this state has no notion of pane-open.
+// first build or mid-turn draft lands blocks on a previously-empty agent.
+// Snap-backs never fire it, and once it fires it stays disarmed for the rest
+// of the state's lifetime.
export type EditorAutoOpenState = {
armed: boolean;
};
@@ -18,13 +17,14 @@ export function shouldAutoOpenEditor(
update: {
embedded: boolean;
applied: boolean | undefined;
+ midTurnDraft?: boolean;
blockCount: number;
},
): { fire: boolean; nextState: EditorAutoOpenState } {
const fire =
state.armed &&
update.embedded &&
- Boolean(update.applied) &&
+ Boolean(update.applied || update.midTurnDraft) &&
update.blockCount > 0;
return { fire, nextState: fire ? { armed: false } : state };
}
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.test.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.test.tsx
index e58a7299c..1e7f5a895 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.test.tsx
@@ -14,8 +14,11 @@ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { useMountEffect } from "@/hooks/useMountEffect";
import { useStudioFirstRunStore } from "@/store/StudioFirstRunStore";
import { useManualSignInStore } from "@/store/useManualSignInStore";
+import { useRecordingStore } from "@/store/useRecordingStore";
import { shouldOpenCopilotPaneForHandoff } from "../discoverCopilotHandoff";
+import { useWorkspaceCopilotUpdate } from "../editor/Workspace";
+import type { WorkflowVersion } from "../hooks/useWorkflowVersionsQuery";
import { StudioPaneDefaultsProvider } from "./StudioPaneDefaults";
import { useStudioPaneDefaults } from "./StudioPaneDefaultsContext";
import { useSwitchStudioRun } from "./runSwitchNavigation";
@@ -36,6 +39,7 @@ function PanesProbe() {
resolveLivePanes,
togglePane,
openPane,
+ closePane,
setOpenPanes,
setPanesOrder,
} = useStudioPanes();
@@ -51,10 +55,15 @@ function PanesProbe() {
</output>
<button onClick={() => togglePane("overview")}>toggle-overview</button>
<button onClick={() => togglePane("copilot")}>toggle-copilot</button>
+ <button onClick={() => togglePane("editor")}>toggle-editor</button>
+ <button onClick={() => closePane("editor")}>close-editor</button>
<button onClick={() => setPanesOrder(["editor", "copilot"])}>
reorder-copilot
</button>
<button onClick={() => setOpenPanes(["editor"])}>show-editor-only</button>
+ <button onClick={() => setOpenPanes(["browser"], { byUser: true })}>
+ user-shows-browser-only
+ </button>
<button onClick={() => openPane("editor")}>open-editor</button>
<button onClick={() => openPane("browser")}>open-browser</button>
</div>
@@ -541,9 +550,91 @@ describe("Copilot manual sign-in", () => {
// Mirrors the Copilot chat focusing the test run its turn just started.
function CopilotRunProbe() {
const switchRun = useSwitchStudioRun({ replace: true, systemFocus: true });
- return <button onClick={() => switchRun("wr_1")}>copilot-run-started</button>;
+ const userSwitchRun = useSwitchStudioRun();
+ const { reopenEditor } = useStudioPaneDefaults();
+ return (
+ <>
+ <button onClick={() => switchRun("wr_1")}>copilot-run-started</button>
+ <button onClick={() => userSwitchRun("wr_2")}>user-opens-run</button>
+ <button onClick={() => reopenEditor()}>copilot-run-ended</button>
+ </>
+ );
+}
+
+// The Workspace's Copilot update entry, on an agent that opened with no blocks.
+function CopilotDraftProbe() {
+ const onWorkflowUpdate = useWorkspaceCopilotUpdate({
+ applyWorkflowUpdate: () => true,
+ embedded: true,
+ initialBlockCount: 0,
+ });
+ const draft = (blocks: number) =>
+ ({
+ workflow_permanent_id: "wpid_1",
+ workflow_definition: {
+ blocks: Array.from({ length: blocks }, () => ({})),
+ },
+ }) as unknown as WorkflowVersion;
+ return (
+ <>
+ <button
+ onClick={() => onWorkflowUpdate(draft(0), { midTurnDraft: true })}
+ >
+ empty-draft-lands
+ </button>
+ <button
+ onClick={() => onWorkflowUpdate(draft(2), { midTurnDraft: true })}
+ >
+ draft-lands
+ </button>
+ </>
+ );
}
+describe("first Copilot draft on a blank agent", () => {
+ test("opens Editor once the draft has blocks", () => {
+ renderStudio({
+ path: "/workflows/wpid_1/studio?via=discover",
+ hasBlocks: false,
+ children: <CopilotDraftProbe />,
+ });
+ expect(panesText()).toBe("copilot,browser");
+ fireEvent.click(screen.getByText("empty-draft-lands"));
+ expect(panesText()).toBe("copilot,browser");
+ fireEvent.click(screen.getByText("draft-lands"));
+ expect(panesText()).toBe("copilot,browser,editor");
+ });
+
+ test("leaves a run the user is inspecting alone when the draft lands", () => {
+ renderStudio({
+ path: "/workflows/wpid_1/studio?via=discover",
+ hasBlocks: false,
+ children: (
+ <>
+ <CopilotRunProbe />
+ <CopilotDraftProbe />
+ </>
+ ),
+ });
+ fireEvent.click(screen.getByText("user-opens-run"));
+ expect(panesText()).toBe("overview,browser");
+ fireEvent.click(screen.getByText("draft-lands"));
+ expect(panesText()).toBe("overview,browser");
+ });
+
+ test("fires once, so a later draft leaves a user's close alone", () => {
+ renderStudio({
+ path: "/workflows/wpid_1/studio?via=discover",
+ hasBlocks: false,
+ children: <CopilotDraftProbe />,
+ });
+ fireEvent.click(screen.getByText("draft-lands"));
+ fireEvent.click(screen.getByText("close-editor"));
+ fireEvent.click(screen.getByText("draft-lands"));
+ expect(panesText()).toBe("copilot,browser");
+ });
+});
+
describe("Copilot test run", () => {
test("opens the Browser pane on the Edit layout", () => {
renderStudio({ children: <CopilotRunProbe /> });
@@ -565,6 +656,96 @@ describe("Copilot test run", () => {
expect(panesText()).toBe("copilot,editor,browser");
});
+ test("reopens Editor in its slot when the run ends on a narrow stage", () => {
+ renderStudio({ stageWidth: 800, children: <CopilotRunProbe /> });
+ fireEvent.click(screen.getByText("copilot-run-started"));
+ expect(panesText()).toBe("copilot,browser");
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("copilot,editor");
+ expect(toastMock).not.toHaveBeenCalled();
+ });
+
+ test("opens Editor on a blank agent when the run ends", () => {
+ renderStudio({
+ path: "/workflows/wpid_1/studio?via=discover",
+ hasBlocks: false,
+ children: <CopilotRunProbe />,
+ });
+ expect(panesText()).toBe("copilot,browser");
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("copilot,browser,editor");
+ });
+
+ test("shows Editor on a stage too narrow for Copilot beside it", () => {
+ renderStudio({
+ path: "/workflows/wpid_1/studio?via=discover",
+ hasBlocks: false,
+ stageWidth: 400,
+ children: <CopilotRunProbe />,
+ });
+ expect(panesText()).toBe("copilot");
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("editor");
+ });
+
+ test("leaves a Browser-only layout the user chose mid-run alone", () => {
+ renderStudio({ stageWidth: 800, children: <CopilotRunProbe /> });
+ fireEvent.click(screen.getByText("copilot-run-started"));
+ expect(panesText()).toBe("copilot,browser");
+ fireEvent.click(screen.getByText("user-shows-browser-only"));
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("browser");
+ });
+
+ test("leaves Editor closed when the user closed it during the session", () => {
+ renderStudio({ children: <CopilotRunProbe /> });
+ fireEvent.click(screen.getByText("copilot-run-started"));
+ fireEvent.click(screen.getByText("toggle-editor"));
+ expect(panesText()).toBe("copilot,browser");
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("copilot,browser");
+ });
+
+ test("leaves Editor closed after the user closes it with the pane's close button", () => {
+ renderStudio({ children: <CopilotRunProbe /> });
+ fireEvent.click(screen.getByText("close-editor"));
+ fireEvent.click(screen.getByText("copilot-run-started"));
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("copilot,browser");
+ });
+
+ test("reopens Editor again once anything has reopened it since the user's close", () => {
+ renderStudio({ stageWidth: 800, children: <CopilotRunProbe /> });
+ fireEvent.click(screen.getByText("toggle-editor"));
+ fireEvent.click(screen.getByText("open-editor"));
+ fireEvent.click(screen.getByText("copilot-run-started"));
+ expect(panesText()).toBe("copilot,browser");
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("copilot,editor");
+ });
+
+ test("does not reopen Editor while a recording is capturing", () => {
+ renderStudio({ stageWidth: 800, children: <CopilotRunProbe /> });
+ fireEvent.click(screen.getByText("copilot-run-started"));
+ useRecordingStore.setState({ isRecording: true });
+ try {
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("copilot,browser");
+ } finally {
+ useRecordingStore.setState({ isRecording: false });
+ }
+ });
+
+ test("keeps a user's Editor close across a run they switch to", () => {
+ renderStudio({ children: <CopilotRunProbe /> });
+ fireEvent.click(screen.getByText("toggle-editor"));
+ fireEvent.click(screen.getByText("copilot-run-started"));
+ fireEvent.click(screen.getByText("user-opens-run"));
+ expect(panesText()).toBe("overview,browser");
+ fireEvent.click(screen.getByText("copilot-run-ended"));
+ expect(panesText()).toBe("overview,browser");
+ });
+
test("leaves a cramped layout with Browser already open alone", () => {
renderStudio({ stageWidth: 800, children: <CopilotRunProbe /> });
fireEvent.click(screen.getByText("open-browser"));
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.tsx
index e1a1c29d5..6efd4819f 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.tsx
@@ -13,6 +13,7 @@ import { useFirstParam } from "@/hooks/useFirstParam";
import { useMountEffect } from "@/hooks/useMountEffect";
import { useStudioFirstRunStore } from "@/store/StudioFirstRunStore";
import { useManualSignInStore } from "@/store/useManualSignInStore";
+import { useRecordingStore } from "@/store/useRecordingStore";
import { sanitizePaneWidth, type PaneWidths } from "@/store/paneWidths";
import {
@@ -36,6 +37,15 @@ import {
} from "./StudioPaneDefaultsContext";
import { useStudioWorkflowDeletedAt } from "./StudioShellContext";
+// Which panes a reopened Editor keeps on a narrow stage once no run holds the
+// Browser, first to last; Editor leads so a one-pane stage still shows it.
+const REOPEN_EDITOR_PANE_RANK: Record<StudioPaneId, number> = {
+ editor: 0,
+ copilot: 1,
+ overview: 2,
+ browser: 3,
+};
+
// Create markers nothing reads after mount; discover, template and record are
// stripped later by the flows that consume them.
const CREATE_ONLY_VIA = ["blank", "sidebar", "onboarding_template"];
@@ -82,6 +92,9 @@ export function StudioPaneDefaultsProvider({
const locationKeyRef = useRef(location.key);
const measuredEntryRef = useRef<number | null>(null);
const wroteEntryRef = useRef<number | null>(null);
+ // The user closed Editor themselves during this visit; run switches and
+ // other new entries keep it.
+ const editorClosedByUserRef = useRef(false);
const keyForSearch = useCallback(
(search: string) => {
@@ -162,6 +175,7 @@ export function StudioPaneDefaultsProvider({
paneWidths: {},
entryId: state.entryId + 1,
};
+ if (panes.includes("editor")) editorClosedByUserRef.current = false;
}
setState(current);
}
@@ -182,6 +196,7 @@ export function StudioPaneDefaultsProvider({
slots: readonly StudioPaneId[],
stageWidth: number,
) => StudioPaneId[] | PanesCutFrom,
+ options?: { byUser?: boolean; layout?: boolean },
) => {
const previous = latestRef.current;
const result = compute(
@@ -197,6 +212,14 @@ export function StudioPaneDefaultsProvider({
? panesWithoutDeletedBlocked(computed)
: computed;
wroteEntryRef.current = previous.entryId;
+ if (panes.includes("editor")) {
+ editorClosedByUserRef.current = false;
+ } else if (
+ options?.byUser &&
+ (options.layout || previous.panes.includes("editor"))
+ ) {
+ editorClosedByUserRef.current = true;
+ }
if (!panesListEqual(previous.panes, panes)) {
latestRef.current = withPanes(previous, panes, arrangement);
setState(latestRef.current);
@@ -219,6 +242,27 @@ export function StudioPaneDefaultsProvider({
},
[workflowDeleted],
);
+ const reopenEditor = useCallback(() => {
+ // Capture takes Editor away on purpose (recordingPaneLifecycle).
+ if (
+ editorClosedByUserRef.current ||
+ useRecordingStore.getState().isRecording
+ )
+ return;
+ updatePanes((panes, slots, stageWidth) => {
+ if (panes.includes("editor")) return panes;
+ const opened = withPaneOpen(panes, "editor", slots);
+ if (stageWidth <= 0 || panesFitWidth(opened, stageWidth)) return opened;
+ const priority = [...opened].sort(
+ (a, b) => REOPEN_EDITOR_PANE_RANK[a] - REOPEN_EDITOR_PANE_RANK[b],
+ );
+ const kept = fitPanesToWidth(priority, stageWidth);
+ return {
+ panes: opened.filter((id) => kept.includes(id)),
+ arrangement: opened,
+ };
+ });
+ }, [updatePanes]);
// A layout effect subscribes before the Copilot chat's passive effects write
// the store, and before a previous agent's chat clears it on unmount.
useLayoutEffect(
@@ -289,6 +333,7 @@ export function StudioPaneDefaultsProvider({
entryId: current.entryId,
getPanes,
updatePanes,
+ reopenEditor,
registerStageElement,
setPaneWidths,
resetPaneWidths,
@@ -300,6 +345,7 @@ export function StudioPaneDefaultsProvider({
current.entryId,
getPanes,
updatePanes,
+ reopenEditor,
registerStageElement,
setPaneWidths,
resetPaneWidths,
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaultsContext.ts b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaultsContext.ts
index 7b7058182..af91b696f 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaultsContext.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaultsContext.ts
@@ -22,7 +22,12 @@ export type StudioPaneDefaultsValue = {
slots: readonly StudioPaneId[],
stageWidth: number,
) => StudioPaneId[] | PanesCutFrom,
+ // layout: the user picked the whole pane set, so leaving Editor out of it
+ // counts as closing Editor even when it was already closed.
+ options?: { byUser?: boolean; layout?: boolean },
) => void;
+ // Opens Editor unless the user closed it during this visit.
+ reopenEditor: () => void;
setPaneWidths: (widths: PaneWidths) => void;
resetPaneWidths: () => void;
preserveNextEntry: (
@@ -42,6 +47,7 @@ export const StudioPaneDefaultsContext = createContext<StudioPaneDefaultsValue>(
entryId: 0,
getPanes: () => DEFAULT_STUDIO_PANES,
updatePanes: noop,
+ reopenEditor: noop,
setPaneWidths: noop,
resetPaneWidths: noop,
preserveNextEntry: noop,
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx
index 8f87a0568..43e7a48a0 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx
@@ -588,7 +588,7 @@ export function StudioTopBar() {
// editor-only layout on entry (an explicit override, like the full-run
// reset). Exiting doesn't restore the previous set — reopen as needed.
const openVersionHistory = () => {
- setOpenPanes(["editor"]);
+ setOpenPanes(["editor"], { byUser: true });
toggleHistoryPanel();
};
return (
diff --git a/skyvern-frontend/src/routes/workflows/studio/runview/RunView.tsx b/skyvern-frontend/src/routes/workflows/studio/runview/RunView.tsx
index b9980805f..478320c16 100644
--- a/skyvern-frontend/src/routes/workflows/studio/runview/RunView.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/runview/RunView.tsx
@@ -1080,7 +1080,7 @@ export function RunView({
onThoughtSelect={(thought) => pinFrame(thought.thought_id)}
onViewScreenshot={(workflowRunBlockId) => {
if (embedded) {
- setOpenPanes(["browser"]);
+ setOpenPanes(["browser"], { byUser: true });
} else {
openPane("browser");
}
diff --git a/skyvern-frontend/src/routes/workflows/studio/useStudioPanes.ts b/skyvern-frontend/src/routes/workflows/studio/useStudioPanes.ts
index 9b87dba87..51441831d 100644
--- a/skyvern-frontend/src/routes/workflows/studio/useStudioPanes.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/useStudioPanes.ts
@@ -65,7 +65,7 @@ export function useStudioPanes() {
current: StudioPaneId[],
slots: readonly StudioPaneId[],
) => StudioPaneId[],
- options?: ApplyPanesOptions,
+ options?: ApplyPanesOptions & { byUser?: boolean; layout?: boolean },
) => {
restoreExpandedPane?.();
updatePanes((current, slots) => {
@@ -81,7 +81,7 @@ export function useStudioPanes() {
});
}
return next;
- });
+ }, options);
if (
!options ||
(!("state" in options) && options.selectedBlockLabel === undefined)
@@ -124,7 +124,9 @@ export function useStudioPanes() {
const togglePane = useCallback(
(id: StudioPaneId) =>
- applyPanes((current, slots) => togglePaneIn(current, id, slots)),
+ applyPanes((current, slots) => togglePaneIn(current, id, slots), {
+ byUser: true,
+ }),
[applyPanes],
);
const openPane = useCallback(
@@ -133,11 +135,13 @@ export function useStudioPanes() {
[applyPanes],
);
const closePane = useCallback(
- (id: StudioPaneId) => applyPanes((current) => withPaneClosed(current, id)),
+ (id: StudioPaneId) =>
+ applyPanes((current) => withPaneClosed(current, id), { byUser: true }),
[applyPanes],
);
const setOpenPanes = useCallback(
- (panes: readonly StudioPaneId[]) => applyPanes(() => [...panes]),
+ (panes: readonly StudioPaneId[], options?: { byUser?: boolean }) =>
+ applyPanes(() => [...panes], { ...options, layout: true }),
[applyPanes],
);
const setPanesOrder = useCallback(
diff --git a/skyvern/forge/sdk/copilot/credential_pause.py b/skyvern/forge/sdk/copilot/credential_pause.py
index b42feceb0..9f6443f38 100644
--- a/skyvern/forge/sdk/copilot/credential_pause.py
+++ b/skyvern/forge/sdk/copilot/credential_pause.py
@@ -44,6 +44,7 @@ from skyvern.forge.sdk.copilot.credential_resolution import loggable_origin, saf
from skyvern.forge.sdk.copilot.diagnosis_repair_contract import DiagnosisFailureType, RepairNextAction
from skyvern.forge.sdk.copilot.human_input_wait import pause_human_input
from skyvern.forge.sdk.copilot.request_policy import RequestPolicy
+from skyvern.forge.sdk.credential_site_policy import same_site
from skyvern.forge.sdk.schemas.credentials import Credential
from skyvern.forge.sdk.schemas.workflow_copilot import (
CredentialPauseResolvedOutcome,
@@ -1031,7 +1032,10 @@ async def _run_credential_pause(
if isinstance(policy, RequestPolicy):
# Bind an answer to the URLs on this card, never an earlier unanswered ask.
policy.credential_ask_login_page_urls = list(login_page_urls)
- credential_refs = list(policy.credential_refs) if isinstance(policy, RequestPolicy) else []
+ # A recovery card pins nothing: its ask follows a refusal of the credential already in hand.
+ credential_refs = (
+ list(policy.credential_refs) if isinstance(policy, RequestPolicy) and admit_connected is None else []
+ )
# The same record the missing-origin fill refusal reads, so the card offers what that refusal promised.
named = policy.current_turn_named_credential_ids if isinstance(policy, RequestPolicy) else set()
plain_pick_ask = update_credential_id is None and registration is None and admit_connected is None
@@ -1040,6 +1044,25 @@ async def _run_credential_pause(
ask_origins = [parts[-1] if (parts := url_parts(url)) is not None else None for url in login_page_urls]
one_site = None not in ask_origins and len(set(ask_origins)) == 1
named_credential_id = next(iter(named)) if len(named) == 1 and plain_pick_ask and one_site else None
+ if named_credential_id is not None and isinstance(policy, RequestPolicy):
+ # A login already saved or confirmed for another site is left to the picker, so one click never rebinds it.
+ known_urls = [
+ policy.live_page_admitted_urls.get(named_credential_id),
+ policy.carried_credential_urls.get(named_credential_id),
+ ] + [
+ credential.tested_url
+ for credential in policy.resolved_credentials
+ if credential.credential_id == named_credential_id
+ ]
+ if any(url and (parts := url_parts(url)) is not None and parts[-1] != ask_origins[0] for url in known_urls):
+ named_credential_id = None
+ if named_credential_id is not None:
+ from skyvern.forge.sdk.copilot.tools.credential_fill import _read_vault_named_sites # circular import
+
+ # The vault entry's sites grant whole-site, so they are compared by site; an unreadable entry offers nothing.
+ vault_sites = await _read_vault_named_sites(ctx, named_credential_id)
+ if vault_sites is None or (vault_sites and not any(same_site(login_page_urls[0], uri) for uri in vault_sites)):
+ named_credential_id = None
timeout_seconds = copilot_config.credential_pause_timeout_seconds
now = datetime.now(timezone.utc)
diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.py
index 5ef167a39..0071e054d 100644
--- a/skyvern/forge/sdk/copilot/request_policy.py
+++ b/skyvern/forge/sdk/copilot/request_policy.py
@@ -963,6 +963,9 @@ class RequestPolicy:
origin_recovery_kept_named_credential_ids: set[str] = field(default_factory=set)
# Explicit user approvals hydrated from the existing trusted structured chat context.
prior_approved_credential_ids: set[str] = field(default_factory=set)
+ # The site each approval or proposal carried in from an earlier turn recorded. Naming the credential
+ # again drops it from live_page_admitted_urls, so the pick card reads it here.
+ carried_credential_urls: dict[str, str] = field(default_factory=dict)
# Sites the user themselves provided anywhere in this chat, one URL per origin. A credential may
# only be released onto one of these (or a vault/tested match); a site only a model produced is
# never eligible.
@@ -4313,6 +4316,7 @@ async def _seed_prior_approved_credentials(
for record in StructuredContext.from_json_str(global_llm_context).approved_credentials:
if record.credential_id in approved_ids and record.admitted_url:
policy.live_page_admitted_urls.setdefault(record.credential_id, record.admitted_url)
+ policy.carried_credential_urls.setdefault(record.credential_id, record.admitted_url)
policy.seeded_proposal_credential_ids.add(record.credential_id)
missing_ids = sorted(approved_ids - {credential.credential_id for credential in policy.resolved_credentials})
if not missing_ids:
@@ -4356,6 +4360,7 @@ async def _seed_proposed_credential(
# Restoring the recorded origin keeps the hydrated id page-vouched, so the turn-end recorder
# still skips it rather than promoting a one-turn carry to durable approval.
policy.live_page_admitted_urls.setdefault(seed.credential_id, seed.admitted_url)
+ policy.carried_credential_urls.setdefault(seed.credential_id, seed.admitted_url)
policy.seeded_proposal_credential_ids.add(seed.credential_id)
# The server bound this and the user did not name it, so it enters through the auto-bound record:
# a write to current_turn_named_credential_ids would refuse the user's own later answer.
diff --git a/tests/unit/test_copilot_credential_pause.py b/tests/unit/test_copilot_credential_pause.py
index 7e377f98f..e3ce353ec 100644
--- a/tests/unit/test_copilot_credential_pause.py
+++ b/tests/unit/test_copilot_credential_pause.py
@@ -57,6 +57,7 @@ from skyvern.forge.sdk.copilot.context import (
AgentResult,
ApprovedCredential,
CopilotContext,
+ ProposedCredential,
StructuredContext,
record_approved_credentials_in_global_llm_context,
)
@@ -95,6 +96,7 @@ from skyvern.forge.sdk.copilot.narration import NarratorState, build_tool_call_a
from skyvern.forge.sdk.copilot.request_policy import (
RequestPolicy,
_seed_prior_approved_credentials,
+ _seed_proposed_credential,
)
from skyvern.forge.sdk.copilot.runtime import CredentialOriginRecovery, CredentialOriginRecoveryState
from skyvern.forge.sdk.copilot.tools import credential_fill as credential_fill_module
@@ -2442,6 +2444,7 @@ async def test_a_pick_card_carries_the_one_login_the_user_named_and_an_update_ca
ctx = _tool_ctx(monkeypatch, cache)
wire_credential_vault(monkeypatch, PasswordCredential(username="u", password="p", totp=None))
ctx.request_policy.current_turn_named_credential_ids = {"cred_1"}
+ ctx.vault_login_uris_by_credential_id["cred_1"] = []
ctx.stream.send = AsyncMock(side_effect=_answer_each_card(cache, [("connected", "cred_1"), ("skip", None)]))
await _call_ask_tool(ctx)
@@ -2450,6 +2453,72 @@ async def test_a_pick_card_carries_the_one_login_the_user_named_and_an_update_ca
assert [card.named_credential_id for card in _sent_cards(ctx)] == ["cred_1", None]
+ ("saved_url", "confirmed_url", "vault_sites", "offered"),
+ [
+ ("https://other.example.com/login", None, [], None),
+ (None, "https://other.example.com/login", [], None),
+ (None, None, ["https://other.example.org"], None),
+ (None, None, None, None),
+ ("https://portal.example.com/account", "https://portal.example.com/login", ["example.com"], "cred_1"),
+ ],
+ ids=[
+ "saved_for_another_site",
+ "confirmed_for_another_site",
+ "vault_names_another_site",
+ "vault_unreadable",
+ "same_site",
+ ],
+)
+async def test_a_named_login_known_for_another_site_is_left_to_the_picker(
+ monkeypatch: pytest.MonkeyPatch,
+ saved_url: str | None,
+ confirmed_url: str | None,
+ vault_sites: list[str] | None,
+ offered: str | None,
+) -> None:
+ ctx = _tool_ctx(monkeypatch, _answered_cache("skip"))
+ if vault_sites is not None:
+ ctx.vault_login_uris_by_credential_id["cred_1"] = vault_sites
+ ctx.request_policy.current_turn_named_credential_ids = {"cred_1"}
+ ctx.request_policy.resolved_credentials = [_make_credential().model_copy(update={"tested_url": saved_url})]
+ if confirmed_url:
+ ctx.request_policy.live_page_admitted_urls["cred_1"] = confirmed_url
+
+ await _ask(ctx, "https://portal.example.com/login")
+
+ assert [card.named_credential_id for card in _sent_cards(ctx)] == [offered]
+
+
[email protected]("carried_as", ["approval", "proposal"])
+async def test_a_login_carried_in_for_another_site_is_left_to_the_picker_when_named_again(
+ monkeypatch: pytest.MonkeyPatch, carried_as: str
+) -> None:
+ ctx = _tool_ctx(monkeypatch, _answered_cache("skip"))
+ ctx.vault_login_uris_by_credential_id["cred_1"] = []
+ policy = ctx.request_policy
+ policy.resolved_credentials = [_make_credential()]
+ _stub_credential_lookup(monkeypatch, _make_credential())
+ earlier = StructuredContext()
+ other_site = "https://other.example.com/login"
+ if carried_as == "approval":
+ earlier.approved_credentials = [ApprovedCredential(credential_id="cred_1", admitted_url=other_site)]
+ seed = _seed_prior_approved_credentials
+ else:
+ earlier.proposed_credential = ProposedCredential(credential_id="cred_1", admitted_url=other_site)
+ seed = _seed_proposed_credential
+ await seed(policy, organization_id="org-1", global_llm_context=earlier.to_json_str())
+ # What resolving a re-named carried credential does before any card is built.
+ policy.live_page_admitted_urls.pop("cred_1")
+ policy.current_turn_named_credential_ids = {"cred_1"}
+
+ await _ask(ctx, "https://portal.example.com/login")
+
+ assert [card.named_credential_id for card in _sent_cards(ctx)] == [None]
+
+
@pytest.mark.asyncio
async def test_an_update_ask_leaves_the_pick_budget_for_a_later_card(monkeypatch: pytest.MonkeyPatch) -> None:
cache = _FakeCache()
@@ -2666,10 +2735,12 @@ async def test_origin_recovery_admits_no_credential_it_cannot_place_on_the_provi
monkeypatch: pytest.MonkeyPatch, connected: Credential, vault_row: AsyncMock | None
) -> None:
ctx = _recovery_ctx(monkeypatch, _answered_cache("connected", connected.credential_id))
+ ctx.request_policy.credential_refs = ["cred_service"]
_stub_credential_lookup(monkeypatch, connected, vault_row)
result = await _ask(ctx, f"{_IDP_ORIGIN}/login")
+ assert [card.credential_refs for card in _sent_cards(ctx)] == [[]]
assert connected.credential_id not in ctx.request_policy.live_page_admitted_urls
assert ctx.credential_pause_outcome == "not_admitted"
_assert_declined(ctx, result, "connected_other_site")