Skip to content

Automate browser based workflows with AI

Resolve Copilot credentials when the login page appears, not only at turn start (SKY-13200) (#7878)

Andrew Neilson3c201c6c7aa0
diff --git a/docs/api-reference/openapi.json b/docs/api-reference/openapi.json
index ea54b8b44..36e19c6bd 100644
--- a/docs/api-reference/openapi.json
+++ b/docs/api-reference/openapi.json
@@ -19187,6 +19187,21 @@
"+14155550123"
]
+ },
+ "metadata": {
+ "anyOf": [
+ {
+ "additionalProperties": {
+ "type": "string"
+ },
+ "type": "object"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Metadata",
+ "description": "Optional additional password credential metadata fields"
}
},
"type": "object",
@@ -24740,6 +24755,7 @@
"authenticator",
"email",
"text",
+ "passkey",
"none"
],
"title": "TotpType",
diff --git a/docs/developers/self-hosted/docker.mdx b/docs/developers/self-hosted/docker.mdx
index a4fa35bb3..0ba28a6af 100644
--- a/docs/developers/self-hosted/docker.mdx
+++ b/docs/developers/self-hosted/docker.mdx
@@ -305,8 +305,11 @@ Update the frontend environment to use your domain:
VITE_API_BASE_URL=https://api.your-domain.com/api/v1
VITE_WSS_BASE_URL=wss://api.your-domain.com/api/v1
VITE_ARTIFACT_API_BASE_URL=https://artifacts.your-domain.com
+ARTIFACT_ALLOWED_ORIGIN=https://your-domain.com
```
+`ARTIFACT_ALLOWED_ORIGIN` must match the browser-visible UI origin (`https://your-domain.com`), not the artifact server URL.
+
<Warning>
If exposing Skyvern to the internet, add authentication at the reverse proxy layer or use a VPN.
</Warning>
diff --git a/docs/developers/self-hosted/kubernetes.mdx b/docs/developers/self-hosted/kubernetes.mdx
index ce7a127a8..15eec7fd6 100644
--- a/docs/developers/self-hosted/kubernetes.mdx
+++ b/docs/developers/self-hosted/kubernetes.mdx
@@ -111,9 +111,12 @@ stringData:
VITE_API_BASE_URL: "http://skyvern.example.com/api/v1"
VITE_WSS_BASE_URL: "ws://skyvern.example.com/api/v1"
VITE_ARTIFACT_API_BASE_URL: "http://skyvern.example.com/artifacts"
+ ARTIFACT_ALLOWED_ORIGIN: "http://skyvern.example.com"
VITE_SKYVERN_API_KEY: "" # Leave empty for initial deploy
```
+`ARTIFACT_ALLOWED_ORIGIN` must match the browser-visible UI origin (`http://skyvern.example.com`), not the artifact server URL.
+
Replace `skyvern.example.com` with your actual domain.
### 4. Configure ingress
diff --git a/fern/openapi/skyvern_openapi.json b/fern/openapi/skyvern_openapi.json
index 515c267f6..9c36e36fc 100644
--- a/fern/openapi/skyvern_openapi.json
+++ b/fern/openapi/skyvern_openapi.json
@@ -23681,6 +23681,7 @@
"authenticator",
"email",
"text",
+ "passkey",
"none"
],
"title": "TotpType",
diff --git a/skyvern-frontend/src/api/types.ts b/skyvern-frontend/src/api/types.ts
index fef39b0e0..6385de392 100644
--- a/skyvern-frontend/src/api/types.ts
+++ b/skyvern-frontend/src/api/types.ts
@@ -1013,7 +1013,7 @@ export type BrowserProfileUsage = {
export type PasswordCredentialApiResponse = {
username: string;
- totp_type: "authenticator" | "email" | "text" | "none";
+ totp_type: "authenticator" | "email" | "text" | "passkey" | "none";
totp_identifier?: string | null;
};
@@ -1092,7 +1092,7 @@ export type PasswordCredential = {
username: string;
password: string;
totp: string | null;
- totp_type: "authenticator" | "email" | "text" | "none";
+ totp_type: "authenticator" | "email" | "text" | "passkey" | "none";
totp_identifier?: string | null;
};
diff --git a/skyvern-frontend/src/routes/credentials/CredentialAuthenticatorSupportContext.ts b/skyvern-frontend/src/routes/credentials/CredentialAuthenticatorSupportContext.ts
index 906bbf294..1d41b71e2 100644
--- a/skyvern-frontend/src/routes/credentials/CredentialAuthenticatorSupportContext.ts
+++ b/skyvern-frontend/src/routes/credentials/CredentialAuthenticatorSupportContext.ts
@@ -4,6 +4,44 @@ import {
useContext,
type ReactNode,
} from "react";
+import type { CredentialGetter } from "@/api/AxiosClient";
+import type { PasswordCredential } from "@/api/types";
+
+type CredentialAdditionalTwoFactorState = Record<
+ string,
+ string | number | boolean
+>;
+
+type CredentialAdditionalTwoFactorMethod = {
+ value: string;
+ requestType: PasswordCredential["totp_type"];
+ label: string;
+ icon?: ReactNode;
+ flagName?: string;
+ supportsInlineTest?: boolean;
+ removalConfirmation?: string;
+ initialState?: CredentialAdditionalTwoFactorState;
+ renderFields: (props: {
+ state: CredentialAdditionalTwoFactorState;
+ setState: (next: CredentialAdditionalTwoFactorState) => void;
+ disabled: boolean;
+ isEditMode: boolean;
+ configured: boolean;
+ validationErrorId?: string;
+ }) => ReactNode;
+ validate: (
+ state: CredentialAdditionalTwoFactorState,
+ context: { isEditMode: boolean; configured: boolean; enabled: boolean },
+ ) => string | null;
+ onSaved: (args: {
+ credentialId: string;
+ state: CredentialAdditionalTwoFactorState;
+ wasSelected: boolean;
+ previouslyConfigured: boolean;
+ enabled: boolean;
+ credentialGetter: CredentialGetter | null;
+ }) => Promise<void>;
+};
type CredentialEnterpriseAuthenticatorSupport = {
label: string;
@@ -24,6 +62,7 @@ type CredentialAuthenticatorQrCodeType = {
type CredentialAuthenticatorSupportCopy = {
enterpriseApps?: CredentialEnterpriseAuthenticatorSupport;
+ additionalTwoFactorMethods?: CredentialAdditionalTwoFactorMethod[];
};
const CredentialAuthenticatorSupportContext =
@@ -52,6 +91,8 @@ export {
useCredentialAuthenticatorSupport,
};
export type {
+ CredentialAdditionalTwoFactorMethod,
+ CredentialAdditionalTwoFactorState,
CredentialAuthenticatorSupportCopy,
CredentialAuthenticatorQrCodeType,
CredentialEnterpriseAuthenticatorSupport,
diff --git a/skyvern-frontend/src/routes/credentials/CredentialItem.test.tsx b/skyvern-frontend/src/routes/credentials/CredentialItem.test.tsx
index bc6a62e52..8e7780bb9 100644
--- a/skyvern-frontend/src/routes/credentials/CredentialItem.test.tsx
+++ b/skyvern-frontend/src/routes/credentials/CredentialItem.test.tsx
@@ -12,6 +12,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { getClient } from "@/api/AxiosClient";
import type { CredentialApiResponse } from "@/api/types";
import { copyText } from "@/util/copyText";
+import { CredentialAuthenticatorSupportProvider } from "./CredentialAuthenticatorSupportContext";
import { CredentialItem } from "./CredentialItem";
vi.mock("@/api/AxiosClient", () => ({ getClient: vi.fn() }));
@@ -62,6 +63,40 @@ afterEach(() => {
});
describe("CredentialItem TOTP code preview", () => {
+ it("uses the context label for an additional two-factor method", () => {
+ render(
+ <CredentialAuthenticatorSupportProvider
+ value={{
+ additionalTwoFactorMethods: [
+ {
+ value: "security_device",
+ requestType: "none",
+ label: "Security Device",
+ renderFields: () => null,
+ validate: () => null,
+ onSaved: vi.fn(),
+ },
+ ],
+ }}
+ >
+ <CredentialItem
+ credential={
+ makePasswordCredential({
+ credential: {
+ username: "[email protected]",
+ totp_type: "security_device" as never,
+ totp_identifier: null,
+ },
+ }) as unknown as CredentialApiResponse
+ }
+ />
+ </CredentialAuthenticatorSupportProvider>,
+ );
+
+ expect(screen.getByText("Security Device")).toBeTruthy();
+ expect(screen.queryByRole("button", { name: "Show 2FA code" })).toBeNull();
+ });
+
it("loads and copies the current authenticator code on demand", async () => {
mockedGetClient.mockResolvedValue({
get: vi.fn().mockResolvedValue({
diff --git a/skyvern-frontend/src/routes/credentials/CredentialItem.tsx b/skyvern-frontend/src/routes/credentials/CredentialItem.tsx
index 345482371..9ef01d83f 100644
--- a/skyvern-frontend/src/routes/credentials/CredentialItem.tsx
+++ b/skyvern-frontend/src/routes/credentials/CredentialItem.tsx
@@ -37,6 +37,7 @@ import { toast } from "@/components/ui/use-toast";
import { copyText } from "@/util/copyText";
import { cn } from "@/util/utils";
import { getCredentialErrorMessage } from "./authenticatorSaveError";
+import { useCredentialAuthenticatorSupport } from "./CredentialAuthenticatorSupportContext";
type Props = {
credential: CredentialApiResponse;
@@ -258,6 +259,8 @@ function CredentialItem({
hasSelection = false,
onSelect,
}: Props) {
+ const { additionalTwoFactorMethods = [] } =
+ useCredentialAuthenticatorSupport();
const [editModalOpen, setEditModalOpen] = useState(false);
const activeTest = useCredentialTestStore((s) =>
s.activeTest?.credentialId === credential.credential_id
@@ -292,8 +295,12 @@ function CredentialItem({
case "text":
return "Text Message";
case "none":
- default:
return "";
+ default:
+ return (
+ additionalTwoFactorMethods.find(({ value }) => value === totpType)
+ ?.label ?? ""
+ );
}
};
diff --git a/skyvern-frontend/src/routes/credentials/CredentialsModal.test.tsx b/skyvern-frontend/src/routes/credentials/CredentialsModal.test.tsx
index d6045f8ab..d14ab514b 100644
--- a/skyvern-frontend/src/routes/credentials/CredentialsModal.test.tsx
+++ b/skyvern-frontend/src/routes/credentials/CredentialsModal.test.tsx
@@ -14,6 +14,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { CredentialApiResponse } from "@/api/types";
+import {
+ CredentialAuthenticatorSupportProvider,
+ type CredentialAdditionalTwoFactorMethod,
+} from "./CredentialAuthenticatorSupportContext";
import { getAuthenticatorKeyError } from "./credentialTotpValidation";
import { CredentialsModal } from "./CredentialsModal";
import { CredentialModalTypes } from "./useCredentialModalState";
@@ -68,7 +72,11 @@ function axiosErrorWithDetail(detail: unknown): AxiosError {
return error;
}
-function renderPasswordCredentialsModal() {
+function renderPasswordCredentialsModal(
+ additionalTwoFactorMethods?: CredentialAdditionalTwoFactorMethod[],
+ onOpenChange = vi.fn(),
+ onCredentialCreated?: (id: string, name?: string) => void,
+) {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
@@ -77,13 +85,18 @@ function renderPasswordCredentialsModal() {
});
return render(
<QueryClientProvider client={queryClient}>
- <MemoryRouter>
- <CredentialsModal
- isOpen
- onOpenChange={vi.fn()}
- overrideType={CredentialModalTypes.PASSWORD}
- />
- </MemoryRouter>
+ <CredentialAuthenticatorSupportProvider
+ value={{ additionalTwoFactorMethods }}
+ >
+ <MemoryRouter>
+ <CredentialsModal
+ isOpen
+ onOpenChange={onOpenChange}
+ onCredentialCreated={onCredentialCreated}
+ overrideType={CredentialModalTypes.PASSWORD}
+ />
+ </MemoryRouter>
+ </CredentialAuthenticatorSupportProvider>
</QueryClientProvider>,
);
}
@@ -106,7 +119,10 @@ const editingPasswordCredential: CredentialApiResponse = {
proxy_session_id: null,
};
-function renderEditPasswordCredentialsModal() {
+function renderEditPasswordCredentialsModal(
+ credential = editingPasswordCredential,
+ additionalTwoFactorMethods?: CredentialAdditionalTwoFactorMethod[],
+) {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
@@ -115,15 +131,19 @@ function renderEditPasswordCredentialsModal() {
});
return render(
<QueryClientProvider client={queryClient}>
- <MemoryRouter>
- <CredentialsModal
- isOpen
- onOpenChange={vi.fn()}
- overrideType={CredentialModalTypes.PASSWORD}
- editingCredential={editingPasswordCredential}
- onStartBackgroundTest={vi.fn()}
- />
- </MemoryRouter>
+ <CredentialAuthenticatorSupportProvider
+ value={{ additionalTwoFactorMethods }}
+ >
+ <MemoryRouter>
+ <CredentialsModal
+ isOpen
+ onOpenChange={vi.fn()}
+ overrideType={CredentialModalTypes.PASSWORD}
+ editingCredential={credential}
+ onStartBackgroundTest={vi.fn()}
+ />
+ </MemoryRouter>
+ </CredentialAuthenticatorSupportProvider>
</QueryClientProvider>,
);
}
@@ -164,7 +184,7 @@ describe("getAuthenticatorKeyError", () => {
).toBeNull();
});
- it("does not validate the key for email, text, or disabled 2FA methods", () => {
+ it("does not validate the key for email, text, extension, or disabled 2FA methods", () => {
expect(
getAuthenticatorKeyError({ totp: "", totp_type: "email" }),
).toBeNull();
@@ -174,7 +194,280 @@ describe("getAuthenticatorKeyError", () => {
expect(
getAuthenticatorKeyError({ totp: "", totp_type: "none" }),
).toBeNull();
+ expect(
+ getAuthenticatorKeyError({ totp: "", totp_type: "security_device" }),
+ ).toBeNull();
+ });
+});
+
+describe("CredentialsModal additional two-factor methods", () => {
+ it("validates extension state and runs its post-save callback with the created id", async () => {
+ const onSaved = vi.fn().mockResolvedValue(undefined);
+ const additionalMethod: CredentialAdditionalTwoFactorMethod = {
+ value: "security_device",
+ requestType: "none",
+ label: "Security Device",
+ initialState: { deviceCode: "" },
+ renderFields: ({ state, setState }) => (
+ <label>
+ Device code
+ <input
+ aria-label="Device code"
+ value={String(state.deviceCode ?? "")}
+ onChange={(event) =>
+ setState({ ...state, deviceCode: event.target.value })
+ }
+ />
+ </label>
+ ),
+ validate: (state) =>
+ String(state.deviceCode ?? "").trim()
+ ? null
+ : "Device code is required.",
+ onSaved,
+ };
+ postMock.mockResolvedValueOnce({
+ data: { credential_id: "cred-security-device", name: "credentials" },
+ });
+ renderPasswordCredentialsModal([additionalMethod]);
+
+ await waitFor(() => {
+ expect(screen.getByDisplayValue("credentials")).toBeTruthy();
+ });
+ const usernameInput = Array.from(
+ document.querySelectorAll<HTMLInputElement>("input"),
+ ).find(
+ (input) =>
+ input.type === "text" && input.value === "" && input.placeholder === "",
+ );
+ expect(usernameInput).toBeTruthy();
+ fireEvent.change(usernameInput as HTMLInputElement, {
+ target: { value: "[email protected]" },
+ });
+ const passwordInput = document.querySelector('input[type="password"]');
+ expect(passwordInput).toBeTruthy();
+ fireEvent.change(passwordInput as HTMLInputElement, {
+ target: { value: "password" },
+ });
+
+ fireEvent.click(screen.getByText("Two-Factor Authentication"));
+ fireEvent.click(screen.getByText("Security Device"));
+
+ const saveButton = screen.getByRole("button", { name: "Save" });
+ expect((saveButton as HTMLButtonElement).disabled).toBe(true);
+ expect(screen.getByText("Device code is required.")).toBeTruthy();
+ fireEvent.change(screen.getByLabelText("Device code"), {
+ target: { value: "device-value" },
+ });
+ expect((saveButton as HTMLButtonElement).disabled).toBe(false);
+ fireEvent.click(saveButton);
+
+ await waitFor(() => {
+ expect(postMock).toHaveBeenCalledWith(
+ "/credentials",
+ expect.objectContaining({
+ credential: {
+ username: "[email protected]",
+ password: "password",
+ totp: null,
+ totp_type: "none",
+ totp_identifier: null,
+ },
+ }),
+ );
+ });
+ await waitFor(() => {
+ expect(onSaved).toHaveBeenCalledWith(
+ expect.objectContaining({
+ credentialId: "cred-security-device",
+ state: { deviceCode: "device-value" },
+ wasSelected: true,
+ previouslyConfigured: false,
+ }),
+ );
+ });
+ const request = postMock.mock.calls[0]?.[1] as {
+ credential: Record<string, unknown>;
+ };
+ expect(request.credential).not.toHaveProperty("security_device");
+ });
+
+ it("surfaces a post-save extension failure as a partial save", async () => {
+ const onOpenChange = vi.fn();
+ const additionalMethod: CredentialAdditionalTwoFactorMethod = {
+ value: "security_device",
+ requestType: "none",
+ label: "Security Device",
+ renderFields: () => null,
+ validate: () => null,
+ onSaved: vi
+ .fn()
+ .mockRejectedValue(axiosErrorWithDetail("Device setup failed.")),
+ };
+ postMock.mockResolvedValueOnce({
+ data: { credential_id: "cred-security-device", name: "credentials" },
+ });
+ renderPasswordCredentialsModal([additionalMethod], onOpenChange);
+
+ await waitFor(() => {
+ expect(screen.getByDisplayValue("credentials")).toBeTruthy();
+ });
+ const usernameInput = Array.from(
+ document.querySelectorAll<HTMLInputElement>("input"),
+ ).find(
+ (input) =>
+ input.type === "text" && input.value === "" && input.placeholder === "",
+ );
+ fireEvent.change(usernameInput as HTMLInputElement, {
+ target: { value: "[email protected]" },
+ });
+ fireEvent.change(document.querySelector('input[type="password"]')!, {
+ target: { value: "password" },
+ });
+ fireEvent.click(screen.getByText("Two-Factor Authentication"));
+ fireEvent.click(screen.getByText("Security Device"));
+ fireEvent.click(screen.getByRole("button", { name: "Save" }));
+
+ await waitFor(() => {
+ expect(toastMock).toHaveBeenCalledWith({
+ title: "Partial save",
+ description: "Device setup failed.",
+ variant: "destructive",
+ });
+ });
+ expect(onOpenChange).toHaveBeenCalledWith(false);
+ });
+
+ it("rolls back the created credential when the passkey material attach fails", async () => {
+ const onSaved = vi
+ .fn()
+ .mockRejectedValue(axiosErrorWithDetail("Passkey setup failed."));
+ const onCredentialCreated = vi.fn();
+ const additionalMethod: CredentialAdditionalTwoFactorMethod = {
+ value: "passkey",
+ requestType: "passkey",
+ label: "Passkey",
+ renderFields: () => null,
+ validate: () => null,
+ onSaved,
+ };
+ postMock.mockResolvedValueOnce({
+ data: { credential_id: "cred-passkey", name: "credentials" },
+ });
+ deleteMock.mockResolvedValueOnce({ data: {} });
+ renderPasswordCredentialsModal(
+ [additionalMethod],
+ vi.fn(),
+ onCredentialCreated,
+ );
+
+ await waitFor(() => {
+ expect(screen.getByDisplayValue("credentials")).toBeTruthy();
+ });
+ const usernameInput = Array.from(
+ document.querySelectorAll<HTMLInputElement>("input"),
+ ).find(
+ (input) =>
+ input.type === "text" && input.value === "" && input.placeholder === "",
+ );
+ fireEvent.change(usernameInput as HTMLInputElement, {
+ target: { value: "[email protected]" },
+ });
+ fireEvent.change(document.querySelector('input[type="password"]')!, {
+ target: { value: "password" },
+ });
+ fireEvent.click(screen.getByText("Two-Factor Authentication"));
+ fireEvent.click(screen.getByText("Passkey"));
+ fireEvent.click(screen.getByRole("button", { name: "Save" }));
+
+ await waitFor(() => {
+ expect(onSaved).toHaveBeenCalledWith(
+ expect.objectContaining({ credentialId: "cred-passkey" }),
+ );
+ });
+ await waitFor(() => {
+ expect(deleteMock).toHaveBeenCalledWith("/credentials/cred-passkey");
+ });
+ expect(onCredentialCreated).not.toHaveBeenCalled();
});
+
+ it("runs the configured method callback after turning two-factor off", async () => {
+ const onSaved = vi.fn().mockResolvedValue(undefined);
+ const additionalMethod: CredentialAdditionalTwoFactorMethod = {
+ value: "security_device",
+ requestType: "none",
+ label: "Security Device",
+ initialState: { deviceCode: "" },
+ renderFields: () => null,
+ validate: () => null,
+ onSaved,
+ };
+ postMock.mockResolvedValueOnce({
+ data: { credential_id: "real-cred-id", name: "Acme Login" },
+ });
+ renderEditPasswordCredentialsModal(
+ {
+ ...editingPasswordCredential,
+ credential: {
+ username: "[email protected]",
+ totp_type: "security_device",
+ totp_identifier: null,
+ },
+ } as unknown as CredentialApiResponse,
+ [additionalMethod],
+ );
+
+ fireEvent.click(screen.getAllByLabelText("Edit credential values")[0]!);
+ fireEvent.change(screen.getByPlaceholderText("••••••••"), {
+ target: { value: "password" },
+ });
+ // Collapsing the Two-Factor Authentication section turns 2FA off (no None tile).
+ fireEvent.click(screen.getByText("Two-Factor Authentication"));
+ fireEvent.click(screen.getByRole("button", { name: "Update" }));
+
+ // Removing a saved 2FA method now requires confirming an in-app dialog.
+ fireEvent.click(await screen.findByRole("button", { name: "Remove" }));
+
+ await waitFor(() => {
+ expect(postMock).toHaveBeenCalledWith(
+ "/credentials/real-cred-id/update",
+ expect.objectContaining({
+ credential: expect.objectContaining({ totp_type: "none" }),
+ }),
+ );
+ });
+ await waitFor(() => {
+ expect(onSaved).toHaveBeenCalledWith(
+ expect.objectContaining({
+ credentialId: "real-cred-id",
+ wasSelected: false,
+ previouslyConfigured: true,
+ }),
+ );
+ });
+ });
+
+ it.each(["Authenticator App", "Email", "Text Message"])(
+ "keeps the inline test affordance for %s",
+ (methodLabel) => {
+ renderPasswordCredentialsModal();
+
+ fireEvent.click(screen.getByText("Two-Factor Authentication"));
+ fireEvent.click(screen.getByText(methodLabel));
+
+ const inlineTestToggle = screen.getByLabelText(
+ "Save browser session for future logins",
+ ) as HTMLButtonElement;
+ expect(inlineTestToggle.disabled).toBe(false);
+ fireEvent.click(inlineTestToggle);
+ expect(screen.getByRole("button", { name: "Test" })).toBeTruthy();
+ expect(
+ screen.queryByText(
+ "Inline login testing is not available for this two-factor method.",
+ ),
+ ).toBeNull();
+ },
+ );
});
describe("CredentialsModal authenticator save errors", () => {
@@ -236,6 +529,12 @@ describe("CredentialsModal authenticator save errors", () => {
}),
);
});
+ const submittedCredential = postMock.mock.calls[0]?.[1] as {
+ credential: Record<string, unknown>;
+ };
+ expect(submittedCredential.credential).not.toHaveProperty(
+ "additionalTwoFactorState",
+ );
await waitFor(() => {
expect(screen.getByText(/push-approval app/)).toBeTruthy();
});
diff --git a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx
index af0755af6..ac7d348ab 100644
--- a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx
+++ b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx
@@ -4,13 +4,17 @@ import {
DialogContent,
DialogHeader,
DialogFooter,
+ DialogClose,
} from "@/components/ui/dialog";
import {
useCredentialModalState,
CredentialModalTypes,
type CredentialModalType,
} from "./useCredentialModalState";
-import { PasswordCredentialContent } from "./PasswordCredentialContent";
+import {
+ PasswordCredentialContent,
+ type PasswordCredentialContentValues,
+} from "./PasswordCredentialContent";
import { SecretCredentialContent } from "./SecretCredentialContent";
import { useState, useEffect, useCallback, useRef } from "react";
import { Button } from "@/components/ui/button";
@@ -31,6 +35,7 @@ import {
type TestCredentialStatusResponse,
type TestLoginResponse,
type ProxyLocation,
+ type PasswordCredential,
} from "@/api/types";
import { getClient } from "@/api/AxiosClient";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
@@ -75,16 +80,48 @@ import {
getCredentialErrorMessage,
type AuthenticatorSaveError,
} from "./authenticatorSaveError";
+import {
+ type CredentialAdditionalTwoFactorMethod,
+ type CredentialAdditionalTwoFactorState,
+ useCredentialAuthenticatorSupport,
+} from "./CredentialAuthenticatorSupportContext";
const PASSWORD_CREDENTIAL_INITIAL_VALUES = {
name: "",
username: "",
password: "",
totp: "",
- totp_type: "none" as "none" | "authenticator" | "email" | "text",
+ totp_type: "none",
totp_identifier: "",
};
+function createAdditionalTwoFactorStates(
+ methods: CredentialAdditionalTwoFactorMethod[],
+): Record<string, CredentialAdditionalTwoFactorState> {
+ return Object.fromEntries(
+ methods.map((method) => [method.value, { ...method.initialState }]),
+ );
+}
+
+function toPasswordCredentialTotpType(
+ value: string,
+ selectedAdditionalTwoFactorMethod?: CredentialAdditionalTwoFactorMethod,
+): PasswordCredential["totp_type"] {
+ if (selectedAdditionalTwoFactorMethod?.value === value) {
+ return selectedAdditionalTwoFactorMethod.requestType;
+ }
+
+ switch (value) {
+ case "authenticator":
+ case "email":
+ case "text":
+ case "none":
+ return value;
+ default:
+ return "none";
+ }
+}
+
function createCreditCardCredentialInitialValues(): CreditCardCredentialValues {
return {
name: "",
@@ -265,6 +302,8 @@ function CredentialsModal({
useCustomCredentialServiceConfig();
const hasCustomCredentialService = !!customCredentialServiceConfig;
const [vaultType, setVaultType] = useState<"default" | "custom">("default");
+ const { additionalTwoFactorMethods = [] } =
+ useCredentialAuthenticatorSupport();
const isEditMode = !!editingCredential;
@@ -278,6 +317,9 @@ function CredentialsModal({
const [passwordCredentialValues, setPasswordCredentialValues] = useState(
PASSWORD_CREDENTIAL_INITIAL_VALUES,
);
+ const [additionalTwoFactorStates, setAdditionalTwoFactorStates] = useState(
+ () => createAdditionalTwoFactorStates(additionalTwoFactorMethods),
+ );
const [creditCardCredentialValues, setCreditCardCredentialValues] = useState(
createCreditCardCredentialInitialValues,
);
@@ -292,9 +334,15 @@ function CredentialsModal({
// decoded QR value or pasted key. Cleared whenever the user edits the key.
const [authenticatorSaveError, setAuthenticatorSaveError] =
useState<AuthenticatorSaveError | null>(null);
+ // In-app confirmation shown before a save that would remove/replace a saved 2FA
+ // method. The ref lets the Remove button re-enter handleSave past this gate.
+ const [removalConfirmationMessage, setRemovalConfirmationMessage] = useState<
+ string | null
+ >(null);
+ const removalConfirmedRef = useRef(false);
const handlePasswordCredentialChange = useCallback(
- (next: typeof PASSWORD_CREDENTIAL_INITIAL_VALUES) => {
+ (next: PasswordCredentialContentValues) => {
setPasswordCredentialValues((prev) => {
if (next.totp !== prev.totp || next.totp_type !== prev.totp_type) {
setAuthenticatorSaveError(null);
@@ -305,6 +353,33 @@ function CredentialsModal({
[],
);
+ const editingPasswordCredentialData =
+ editingCredential && isPasswordCredential(editingCredential.credential)
+ ? editingCredential.credential
+ : undefined;
+ const configuredAdditionalTwoFactorMethod = editingPasswordCredentialData
+ ? additionalTwoFactorMethods.find(
+ ({ value }) => value === editingPasswordCredentialData.totp_type,
+ )
+ : undefined;
+ const selectedAdditionalTwoFactorMethod = additionalTwoFactorMethods.find(
+ ({ value }) => value === passwordCredentialValues.totp_type,
+ );
+ const supportsInlineTest =
+ selectedAdditionalTwoFactorMethod?.supportsInlineTest !== false;
+ const configuredAdditionalTwoFactorFlagEnabled = useFeatureFlag(
+ configuredAdditionalTwoFactorMethod?.flagName ?? "",
+ );
+ const selectedAdditionalTwoFactorFlagEnabled = useFeatureFlag(
+ selectedAdditionalTwoFactorMethod?.flagName ?? "",
+ );
+ const configuredAdditionalTwoFactorMethodEnabled =
+ !configuredAdditionalTwoFactorMethod?.flagName ||
+ configuredAdditionalTwoFactorFlagEnabled === true;
+ const selectedAdditionalTwoFactorMethodEnabled =
+ !selectedAdditionalTwoFactorMethod?.flagName ||
+ selectedAdditionalTwoFactorFlagEnabled === true;
+
const reportCredentialSaveError = useCallback(
(error: unknown, title = "Error"): string => {
const authError = getAuthenticatorSaveError(error);
@@ -432,6 +507,12 @@ function CredentialsModal({
proxyLocation: ProxyLocation | null;
proxySessionId?: string | null;
proxyPinChanged: boolean;
+ additionalTwoFactor?: {
+ selectedValue?: string;
+ configuredValue?: string;
+ enabledValues: Record<string, boolean>;
+ states: Record<string, CredentialAdditionalTwoFactorState>;
+ };
}>({
shouldTestAfterSave: false,
saveBrowserSessionIntent: false,
@@ -443,6 +524,47 @@ function CredentialsModal({
proxyPinChanged: false,
});
+ const runAdditionalTwoFactorSave = useCallback(
+ async (credentialId: string): Promise<boolean> => {
+ const snapshot = saveIntentRef.current.additionalTwoFactor;
+ if (!snapshot) {
+ return true;
+ }
+
+ const affectedValues = [
+ ...(snapshot.configuredValue ? [snapshot.configuredValue] : []),
+ ...(snapshot.selectedValue !== snapshot.configuredValue &&
+ snapshot.selectedValue
+ ? [snapshot.selectedValue]
+ : []),
+ ];
+
+ try {
+ for (const value of affectedValues) {
+ const method = additionalTwoFactorMethods.find(
+ (candidate) => candidate.value === value,
+ );
+ if (!method) {
+ continue;
+ }
+ await method.onSaved({
+ credentialId,
+ state: snapshot.states[value] ?? method.initialState ?? {},
+ wasSelected: snapshot.selectedValue === value,
+ previouslyConfigured: snapshot.configuredValue === value,
+ enabled: snapshot.enabledValues[value] ?? true,
+ credentialGetter,
+ });
+ }
+ return true;
+ } catch (error) {
+ reportCredentialSaveError(error, "Partial save");
+ return false;
+ }
+ },
+ [additionalTwoFactorMethods, credentialGetter, reportCredentialSaveError],
+ );
+
// Cleanup polling on unmount
useEffect(() => {
return () => {
@@ -566,15 +688,21 @@ function CredentialsModal({
setTestUrl(defaultTestUrl);
}
}
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- ref-guarded one-time init; reset and the setters are intentionally omitted so a background refetch can't re-run it
}, [isOpen, credentials, isEditMode, editingCredential, defaultTestUrl]);
function reset() {
setVaultType("default");
setPasswordCredentialValues(PASSWORD_CREDENTIAL_INITIAL_VALUES);
+ setAdditionalTwoFactorStates(
+ createAdditionalTwoFactorStates(additionalTwoFactorMethods),
+ );
setCreditCardCredentialValues(createCreditCardCredentialInitialValues());
setSecretCredentialValues(SECRET_CREDENTIAL_INITIAL_VALUES);
setEditingGroups({ name: false, values: false });
setAuthenticatorSaveError(null);
+ setRemovalConfirmationMessage(null);
+ removalConfirmedRef.current = false;
setBrowserProfileId(null);
setPinIp(false);
setTestAndSave(false);
@@ -745,7 +873,10 @@ function CredentialsModal({
username: passwordCredentialValues.username.trim(),
password: passwordCredentialValues.password.trim(),
totp: passwordCredentialValues.totp.trim() || null,
- totp_type: passwordCredentialValues.totp_type,
+ totp_type: toPasswordCredentialTotpType(
+ passwordCredentialValues.totp_type,
+ selectedAdditionalTwoFactorMethod,
+ ),
totp_identifier:
passwordCredentialValues.totp_identifier.trim() || null,
user_context: userContext.trim() || null,
@@ -791,6 +922,7 @@ function CredentialsModal({
credentialGetter,
testUrl,
passwordCredentialValues,
+ selectedAdditionalTwoFactorMethod,
userContext,
pollTestStatus,
getProxyPinPayload,
@@ -825,6 +957,33 @@ function CredentialsModal({
// Best-effort — credential was created, URL is just metadata
}
}
+ const additionalTwoFactorSaved = await runAdditionalTwoFactorSave(
+ data.credential_id,
+ );
+
+ if (!additionalTwoFactorSaved) {
+ // The 2FA type is already persisted; roll the credential back rather than strand a passkey with no material.
+ let rollbackSucceeded = true;
+ try {
+ const client = await getClient(credentialGetter);
+ await client.delete(`/credentials/${data.credential_id}`);
+ } catch {
+ rollbackSucceeded = false;
+ }
+ if (rollbackSucceeded) {
+ reset();
+ setIsOpen(false);
+ } else {
+ toast({
+ title: "Couldn't finish saving the credential",
+ description:
+ "The additional two-factor method couldn't be saved and the incomplete credential couldn't be removed automatically. Please delete it from the credentials list and try again.",
+ variant: "destructive",
+ });
+ }
+ return;
+ }
+
queryClient.invalidateQueries({
queryKey: ["credentials"],
});
@@ -922,14 +1081,24 @@ function CredentialsModal({
}
}
- reset();
- setIsOpen(false);
+ const additionalTwoFactorSaved = editingCredential?.credential_id
+ ? await runAdditionalTwoFactorSave(editingCredential.credential_id)
+ : true;
+
queryClient.invalidateQueries({
queryKey: ["credentials"],
});
- // The metadata PATCH failed and already surfaced a destructive toast —
- // don't follow it with a contradictory "Credential updated" success toast.
+ // The additional 2FA save owns the passkey material; if it failed, keep the modal open so the
+ // entered material stays available for correction instead of leaving a passkey type with no key.
+ if (!additionalTwoFactorSaved) {
+ return;
+ }
+
+ reset();
+ setIsOpen(false);
+
+ // A destructive "Partial save" toast already surfaced; don't contradict it with a success toast.
if (!metadataSaved) {
return;
}
@@ -1121,15 +1290,47 @@ function CredentialsModal({
if (authenticatorKeyError) {
return;
}
+ if (additionalTwoFactorError) {
+ toast({
+ title: "Error",
+ description: additionalTwoFactorError,
+ variant: "destructive",
+ });
+ return;
+ }
+ // Removing or replacing any saved 2FA method (authenticator/email/text/passkey) destroys its
+ // stored material on save; confirm first so an accidental collapse can't silently delete it.
+ const savedTotpType = editingPasswordCredentialData?.totp_type;
+ const removalConfirmation =
+ isEditMode &&
+ savedTotpType &&
+ savedTotpType !== "none" &&
+ savedTotpType !== passwordCredentialValues.totp_type
+ ? (configuredAdditionalTwoFactorMethod?.removalConfirmation ??
+ "Remove two-factor authentication? The saved 2FA settings will be permanently deleted.")
+ : null;
+ if (removalConfirmation && !removalConfirmedRef.current) {
+ setRemovalConfirmationMessage(removalConfirmation);
+ return;
+ }
+ removalConfirmedRef.current = false;
setAuthenticatorSaveError(null);
const hasCompletedInlineTest =
- testAndSave && testStatus === "completed" && !!testCredentialId;
+ supportsInlineTest &&
+ testAndSave &&
+ testStatus === "completed" &&
+ !!testCredentialId;
// Create mode: the temp credential created by the inline test already holds
// the entered secret and the saved browser profile, so rename it in place
// instead of creating a duplicate.
- if (!isEditMode && hasCompletedInlineTest && testCredentialId) {
+ if (
+ !isEditMode &&
+ hasCompletedInlineTest &&
+ testCredentialId &&
+ !selectedAdditionalTwoFactorMethod
+ ) {
const url = testUrl.trim();
const ctx = userContext.trim();
renameCredentialMutation.mutate({
@@ -1169,19 +1370,47 @@ function CredentialsModal({
testUrl.trim() !== (editingCredential?.tested_url ?? "");
saveIntentRef.current = {
shouldTestAfterSave:
+ supportsInlineTest &&
!browserMemoryEnabled &&
testAndSave &&
(testStatus !== "completed" ||
(isEditMode && hasCompletedInlineTest)) &&
testUrl.trim() !== "" &&
hasEditModeChanges,
- saveBrowserSessionIntent: testAndSave,
+ saveBrowserSessionIntent: supportsInlineTest && testAndSave,
testUrl: testUrl.trim(),
userContext: userContext.trim(),
name,
proxyLocation: proxyPinPayload.proxy_location,
proxySessionId: proxyPinPayload.proxy_session_id,
proxyPinChanged,
+ additionalTwoFactor:
+ selectedAdditionalTwoFactorMethod ||
+ configuredAdditionalTwoFactorMethod
+ ? {
+ selectedValue: selectedAdditionalTwoFactorMethod?.value,
+ configuredValue: configuredAdditionalTwoFactorMethod?.value,
+ enabledValues: {
+ ...(selectedAdditionalTwoFactorMethod
+ ? {
+ [selectedAdditionalTwoFactorMethod.value]:
+ selectedAdditionalTwoFactorMethodEnabled,
+ }
+ : {}),
+ ...(configuredAdditionalTwoFactorMethod
+ ? {
+ [configuredAdditionalTwoFactorMethod.value]:
+ configuredAdditionalTwoFactorMethodEnabled,
+ }
+ : {}),
+ },
+ states: Object.fromEntries(
+ Object.entries(additionalTwoFactorStates).map(
+ ([value, state]) => [value, { ...state }],
+ ),
+ ),
+ }
+ : undefined,
};
activeMutation.mutate({
@@ -1190,8 +1419,19 @@ function CredentialsModal({
credential: {
username,
password,
- totp: totp === "" ? null : totp,
- totp_type: passwordCredentialValues.totp_type,
+ totp: selectedAdditionalTwoFactorMethod || totp === "" ? null : totp,
+ // When newly selecting an additional method, stage "none" so the dedicated endpoint sets type
+ // and material atomically; a rejection then can't leave a 2FA type with no material behind.
+ // For an already-configured replacement, keep the type so the old material survives a failure.
+ totp_type:
+ selectedAdditionalTwoFactorMethod &&
+ selectedAdditionalTwoFactorMethod.value !==
+ configuredAdditionalTwoFactorMethod?.value
+ ? "none"
+ : toPasswordCredentialTotpType(
+ passwordCredentialValues.totp_type,
+ selectedAdditionalTwoFactorMethod,
+ ),
totp_identifier: totpIdentifier === "" ? null : totpIdentifier,
},
...(vaultType === "custom" ? { vault_type: "custom" } : {}),
@@ -1379,6 +1619,24 @@ function CredentialsModal({
const authenticatorKeyError = shouldValidateAuthenticatorKey
? getAuthenticatorKeyError(passwordCredentialValues)
: null;
+ const shouldValidateAdditionalTwoFactor =
+ type === CredentialModalTypes.PASSWORD &&
+ (!isEditMode || editingGroups.values);
+ const additionalTwoFactorError =
+ shouldValidateAdditionalTwoFactor && selectedAdditionalTwoFactorMethod
+ ? selectedAdditionalTwoFactorMethod.validate(
+ additionalTwoFactorStates[selectedAdditionalTwoFactorMethod.value] ??
+ selectedAdditionalTwoFactorMethod.initialState ??
+ {},
+ {
+ isEditMode,
+ configured:
+ configuredAdditionalTwoFactorMethod?.value ===
+ selectedAdditionalTwoFactorMethod.value,
+ enabled: selectedAdditionalTwoFactorMethodEnabled,
+ },
+ )
+ : null;
const proxyPinContent = (
<div className="space-y-3 border-t border-slate-700 pt-4">
@@ -1560,9 +1818,20 @@ function CredentialsModal({
onChange={handlePasswordCredentialChange}
url={testUrl}
onUrlChange={setTestUrl}
- urlRequired={testAndSave}
+ urlRequired={supportsInlineTest && testAndSave}
urlDisabled={isTestInProgress}
editMode={isEditMode}
+ configuredAdditionalTwoFactorMethod={
+ configuredAdditionalTwoFactorMethod?.value
+ }
+ additionalTwoFactorStates={additionalTwoFactorStates}
+ onAdditionalTwoFactorStateChange={(methodValue, next) =>
+ setAdditionalTwoFactorStates((previous) => ({
+ ...previous,
+ [methodValue]: next,
+ }))
+ }
+ additionalTwoFactorError={additionalTwoFactorError}
editingGroups={editingGroups}
onEnableEditName={handleEnableEditName}
onEnableEditValues={handleEnableEditValues}
@@ -1581,7 +1850,7 @@ function CredentialsModal({
onCheckedChange={(checked) =>
setTestAndSave(checked === true)
}
- disabled={isTestInProgress}
+ disabled={isTestInProgress || !supportsInlineTest}
/>
<Label
htmlFor="test-and-save"
@@ -1592,7 +1861,14 @@ function CredentialsModal({
<HelpTooltip content="Skyvern will log in using your credentials, verify success, and save the browser session. Future agent runs will skip the login form entirely because the saved session is already authenticated." />
</div>
- {testAndSave && (
+ {!supportsInlineTest && (
+ <p className="pl-7 text-sm text-muted-foreground">
+ Inline login testing is not available for this two-factor
+ method.
+ </p>
+ )}
+
+ {supportsInlineTest && testAndSave && (
<div className="space-y-1 pl-7">
<Label
htmlFor="user-context"
@@ -1614,7 +1890,7 @@ function CredentialsModal({
</div>
)}
- {isTestInProgress && (
+ {supportsInlineTest && isTestInProgress && (
<div className="space-y-1 pl-7">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<ReloadIcon className="size-4 animate-spin" />
@@ -1633,7 +1909,7 @@ function CredentialsModal({
)}
</div>
)}
- {testStatus === "completed" && (
+ {supportsInlineTest && testStatus === "completed" && (
<div className="flex items-center gap-2 pl-7 text-sm text-green-400">
<CheckCircledIcon className="size-4" />
<span>
@@ -1641,7 +1917,7 @@ function CredentialsModal({
</span>
</div>
)}
- {testStatus === "profile_failed" && (
+ {supportsInlineTest && testStatus === "profile_failed" && (
<div className="space-y-1 pl-7">
<div className="flex items-center gap-2 text-sm text-destructive">
<CrossCircledIcon className="size-4" />
@@ -1654,7 +1930,7 @@ function CredentialsModal({
)}
</div>
)}
- {testStatus === "failed" && (
+ {supportsInlineTest && testStatus === "failed" && (
<div className="space-y-1 pl-7">
<div className="flex items-center gap-2 text-sm text-destructive">
<CrossCircledIcon className="size-4" />
@@ -1737,6 +2013,9 @@ function CredentialsModal({
if (authenticatorKeyError) {
return;
}
+ if (additionalTwoFactorError) {
+ return;
+ }
// Set testing state immediately to avoid button flash
pollCancelledRef.current = false;
@@ -1785,6 +2064,7 @@ function CredentialsModal({
const showTestButton =
!browserMemoryEnabled &&
testAndSave &&
+ supportsInlineTest &&
type === CredentialModalTypes.PASSWORD &&
(!isEditMode || editingGroups.values);
@@ -1795,99 +2075,135 @@ function CredentialsModal({
passwordCredentialValues.username.trim() !== "" &&
passwordCredentialValues.password.trim() !== "" &&
!authenticatorKeyError &&
+ !additionalTwoFactorError &&
!isTestInProgress;
return (
- <Dialog
- open={isOpen}
- onOpenChange={(open) => {
- if (!open) {
- // Prevent in-flight poll responses from updating state after close
- pollCancelledRef.current = true;
- // Cancel any in-progress test before closing — use refs for latest IDs
- const credId = testCredentialIdRef.current;
- const wrId = testWorkflowRunIdRef.current;
- if (isTestInProgress && credId && wrId) {
- getClient(credentialGetter, "sans-api-v1")
- .then((client) =>
- client.post(`/credentials/${credId}/test/${wrId}/cancel`),
- )
- .catch(() => {
- // Best-effort cleanup
- });
- } else if (credId && !isTestInProgress) {
- // Test completed but user closed without saving — delete orphaned temp credential
- getClient(credentialGetter)
- .then((client) => client.delete(`/credentials/${credId}`))
- .catch(() => {});
- }
- testCredentialIdRef.current = null;
- testWorkflowRunIdRef.current = null;
- if (pollIntervalRef.current) {
- clearTimeout(pollIntervalRef.current);
- pollIntervalRef.current = null;
+ <>
+ <Dialog
+ open={isOpen}
+ onOpenChange={(open) => {
+ if (!open) {
+ // Prevent in-flight poll responses from updating state after close
+ pollCancelledRef.current = true;
+ // Cancel any in-progress test before closing — use refs for latest IDs
+ const credId = testCredentialIdRef.current;
+ const wrId = testWorkflowRunIdRef.current;
+ if (isTestInProgress && credId && wrId) {
+ getClient(credentialGetter, "sans-api-v1")
+ .then((client) =>
+ client.post(`/credentials/${credId}/test/${wrId}/cancel`),
+ )
+ .catch(() => {
+ // Best-effort cleanup
+ });
+ } else if (credId && !isTestInProgress) {
+ // Test completed but user closed without saving — delete orphaned temp credential
+ getClient(credentialGetter)
+ .then((client) => client.delete(`/credentials/${credId}`))
+ .catch(() => {});
+ }
+ testCredentialIdRef.current = null;
+ testWorkflowRunIdRef.current = null;
+ if (pollIntervalRef.current) {
+ clearTimeout(pollIntervalRef.current);
+ pollIntervalRef.current = null;
+ }
+ reset();
}
- reset();
- }
- setIsOpen(open);
- }}
- >
- <DialogContent className="max-h-[90vh] w-[700px] max-w-[700px] overflow-y-auto [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-2 [&::-webkit-scrollbar-thumb]:border-slate-100 [&::-webkit-scrollbar-thumb]:bg-slate-300 dark:[&::-webkit-scrollbar-thumb]:border-slate-800 dark:[&::-webkit-scrollbar-thumb]:bg-slate-600 [&::-webkit-scrollbar-track]:bg-slate-100 dark:[&::-webkit-scrollbar-track]:bg-slate-800 [&::-webkit-scrollbar]:w-2">
- <DialogHeader>
- <DialogTitle className="font-bold">
- {isEditMode ? "Edit Credential" : "Add Credential"}
- </DialogTitle>
- </DialogHeader>
- {isEditMode && editingGroups.values && (
- <Alert variant="warning">
- <ExclamationTriangleIcon className="size-4" />
- <AlertDescription>
- For security, saved values are never retrieved. Changing any field
- other than the credential name requires re-entering all fields,
- including passwords and 2FA settings.
- </AlertDescription>
- </Alert>
- )}
- {credentialContent}
- {/* The browser-memory IP checkbox only replaces this for PASSWORD
+ setIsOpen(open);
+ }}
+ >
+ <DialogContent className="max-h-[90vh] w-[700px] max-w-[700px] overflow-y-auto [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-2 [&::-webkit-scrollbar-thumb]:border-slate-100 [&::-webkit-scrollbar-thumb]:bg-slate-300 dark:[&::-webkit-scrollbar-thumb]:border-slate-800 dark:[&::-webkit-scrollbar-thumb]:bg-slate-600 [&::-webkit-scrollbar-track]:bg-slate-100 dark:[&::-webkit-scrollbar-track]:bg-slate-800 [&::-webkit-scrollbar]:w-2">
+ <DialogHeader>
+ <DialogTitle className="font-bold">
+ {isEditMode ? "Edit Credential" : "Add Credential"}
+ </DialogTitle>
+ </DialogHeader>
+ {isEditMode && editingGroups.values && (
+ <Alert variant="warning">
+ <ExclamationTriangleIcon className="size-4" />
+ <AlertDescription>
+ For security, saved values are never retrieved. Changing any
+ field other than the credential name requires re-entering all
+ fields, including passwords and 2FA settings.
+ </AlertDescription>
+ </Alert>
+ )}
+ {credentialContent}
+ {/* The browser-memory IP checkbox only replaces this for PASSWORD
credentials; card/secret keep the proxy-pin UI under the flag. */}
- {(!browserMemoryEnabled || type !== CredentialModalTypes.PASSWORD) &&
- proxyPinContent}
-
- <DialogFooter>
- <div className="flex w-full items-center justify-end gap-2">
- {showTestButton &&
- (isTestInProgress ? (
- <Button variant="destructive" onClick={cancelTest}>
- Cancel Test
- </Button>
- ) : (
- <Button
- variant="secondary"
- onClick={handleTest}
- disabled={!canTest}
- >
- {isTestComplete ? "Retest" : "Test"}
- </Button>
- ))}
+ {(!browserMemoryEnabled || type !== CredentialModalTypes.PASSWORD) &&
+ proxyPinContent}
+
+ <DialogFooter>
+ <div className="flex w-full items-center justify-end gap-2">
+ {showTestButton &&
+ (isTestInProgress ? (
+ <Button variant="destructive" onClick={cancelTest}>
+ Cancel Test
+ </Button>
+ ) : (
+ <Button
+ variant="secondary"
+ onClick={handleTest}
+ disabled={!canTest}
+ >
+ {isTestComplete ? "Retest" : "Test"}
+ </Button>
+ ))}
+ <Button
+ onClick={handleSave}
+ disabled={
+ activeMutation.isPending ||
+ renameCredentialMutation.isPending ||
+ isTestInProgress ||
+ Boolean(additionalTwoFactorError)
+ }
+ >
+ {activeMutation.isPending ||
+ renameCredentialMutation.isPending ? (
+ <ReloadIcon className="mr-2 size-4 animate-spin" />
+ ) : null}
+ {isEditMode ? "Update" : "Save"}
+ </Button>
+ </div>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ <Dialog
+ open={removalConfirmationMessage !== null}
+ onOpenChange={(open) => {
+ if (!open) {
+ setRemovalConfirmationMessage(null);
+ }
+ }}
+ >
+ <DialogContent onCloseAutoFocus={(e) => e.preventDefault()}>
+ <DialogHeader>
+ <DialogTitle>Remove two-factor authentication?</DialogTitle>
+ </DialogHeader>
+ <div className="text-sm text-slate-400">
+ {removalConfirmationMessage}
+ </div>
+ <DialogFooter>
+ <DialogClose asChild>
+ <Button variant="secondary">Cancel</Button>
+ </DialogClose>
<Button
- onClick={handleSave}
- disabled={
- activeMutation.isPending ||
- renameCredentialMutation.isPending ||
- isTestInProgress
- }
+ variant="destructive"
+ onClick={() => {
+ setRemovalConfirmationMessage(null);
+ removalConfirmedRef.current = true;
+ handleSave();
+ }}
>
- {activeMutation.isPending ||
- renameCredentialMutation.isPending ? (
- <ReloadIcon className="mr-2 size-4 animate-spin" />
- ) : null}
- {isEditMode ? "Update" : "Save"}
+ Remove
</Button>
- </div>
- </DialogFooter>
- </DialogContent>
- </Dialog>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ </>
);
}
diff --git a/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.test.tsx b/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.test.tsx
index 2b33fd126..746c14569 100644
--- a/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.test.tsx
+++ b/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.test.tsx
@@ -20,7 +20,7 @@ type Values = {
username: string;
password: string;
totp: string;
- totp_type: "authenticator" | "email" | "text" | "none";
+ totp_type: string;
totp_identifier: string;
};
@@ -72,6 +72,143 @@ const ENTERPRISE_APPS = {
"Scan the QR as usual - Skyvern detects these setup codes automatically.",
};
+describe("PasswordCredentialContent — additional two-factor methods", () => {
+ const additionalMethod = {
+ value: "security_device",
+ requestType: "none" as const,
+ label: "Security Device",
+ initialState: { deviceCode: "" },
+ renderFields: ({
+ state,
+ setState,
+ configured,
+ }: {
+ state: Record<string, string | number | boolean>;
+ setState: (next: Record<string, string | number | boolean>) => void;
+ disabled: boolean;
+ isEditMode: boolean;
+ configured: boolean;
+ }) => (
+ <>
+ <label>
+ Device code
+ <input
+ aria-label="Device code"
+ value={String(state.deviceCode ?? "")}
+ onChange={(event) =>
+ setState({ ...state, deviceCode: event.target.value })
+ }
+ />
+ </label>
+ <span>{configured ? "Configured" : "Not configured"}</span>
+ </>
+ ),
+ validate: () => null,
+ onSaved: vi.fn(),
+ };
+
+ it("renders methods supplied by the context as additional cards", () => {
+ render(
+ <CredentialAuthenticatorSupportProvider
+ value={{ additionalTwoFactorMethods: [additionalMethod] }}
+ >
+ <MemoryRouter>
+ <PasswordCredentialContent
+ values={INITIAL_VALUES}
+ onChange={vi.fn()}
+ additionalTwoFactorStates={{
+ security_device: additionalMethod.initialState,
+ }}
+ onAdditionalTwoFactorStateChange={vi.fn()}
+ />
+ </MemoryRouter>
+ </CredentialAuthenticatorSupportProvider>,
+ );
+
+ fireEvent.click(screen.getByText("Two-Factor Authentication"));
+ fireEvent.click(screen.getByText("Security Device"));
+
+ expect(screen.getByLabelText("Device code")).toBeTruthy();
+ expect(screen.getByText("Not configured")).toBeTruthy();
+ });
+
+ it("marks a response-matched method as configured in edit mode", () => {
+ render(
+ <CredentialAuthenticatorSupportProvider
+ value={{ additionalTwoFactorMethods: [additionalMethod] }}
+ >
+ <MemoryRouter>
+ <PasswordCredentialContent
+ values={{ ...INITIAL_VALUES, totp_type: "security_device" }}
+ onChange={vi.fn()}
+ editMode
+ editingGroups={{ name: false, values: true }}
+ configuredAdditionalTwoFactorMethod="security_device"
+ additionalTwoFactorStates={{
+ security_device: additionalMethod.initialState,
+ }}
+ onAdditionalTwoFactorStateChange={vi.fn()}
+ />
+ </MemoryRouter>
+ </CredentialAuthenticatorSupportProvider>,
+ );
+
+ expect(screen.getByText("Security Device")).toBeTruthy();
+ expect(screen.getByText("Configured")).toBeTruthy();
+ });
+
+ it("does not render an additional card with the default context", () => {
+ render(
+ <MemoryRouter>
+ <PasswordCredentialContent
+ values={INITIAL_VALUES}
+ onChange={vi.fn()}
+ additionalTwoFactorStates={{}}
+ onAdditionalTwoFactorStateChange={vi.fn()}
+ />
+ </MemoryRouter>,
+ );
+
+ fireEvent.click(screen.getByText("Two-Factor Authentication"));
+
+ expect(screen.queryByText("Security Device")).toBeNull();
+ });
+});
+
+describe("PasswordCredentialContent — built-in two-factor methods", () => {
+ it("exposes every method as a focusable pressed button and updates selection", () => {
+ render(
+ <MemoryRouter>
+ <PasswordCredentialContent values={INITIAL_VALUES} onChange={vi.fn()} />
+ </MemoryRouter>,
+ );
+
+ fireEvent.click(screen.getByText("Two-Factor Authentication"));
+
+ const methodNames = ["Authenticator App", "Email", "Text Message"];
+ const methodButtons = methodNames.map((name) =>
+ screen.getByRole("button", { name }),
+ );
+
+ for (const [index, button] of methodButtons.entries()) {
+ expect(button).toBeInstanceOf(HTMLButtonElement);
+ expect((button as HTMLButtonElement).type).toBe("button");
+ expect(button.tabIndex).toBe(0);
+ expect(button.getAttribute("aria-pressed")).toBe(
+ index === 0 ? "true" : "false",
+ );
+ }
+
+ const emailButton = methodButtons[1]!;
+ emailButton.focus();
+ expect(document.activeElement).toBe(emailButton);
+ fireEvent.click(emailButton);
+
+ expect(emailButton.getAttribute("aria-pressed")).toBe("true");
+ expect(methodButtons[0]?.getAttribute("aria-pressed")).toBe("false");
+ });
+});
+
// Mirrors CredentialsModal: starts the form at INITIAL_VALUES, then after mount
// applies the loaded credential via setPasswordCredentialValues — same flow
// that produced SKY-9864.
diff --git a/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.tsx b/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.tsx
index c6f6bc852..90cb7f981 100644
--- a/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.tsx
+++ b/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.tsx
@@ -10,6 +10,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Separator } from "@/components/ui/separator";
+import { useFeatureFlag } from "@/hooks/useFeatureFlag";
import { cn } from "@/util/utils";
import {
CheckIcon,
@@ -32,6 +33,8 @@ import {
} from "react";
import { Link } from "react-router-dom";
import {
+ type CredentialAdditionalTwoFactorMethod,
+ type CredentialAdditionalTwoFactorState,
type CredentialAuthenticatorQrCodeType,
useCredentialAuthenticatorSupport,
} from "./CredentialAuthenticatorSupportContext";
@@ -39,23 +42,18 @@ import { AuthenticatorAppLogo } from "./AuthenticatorAppLogo";
import { decodeQrCodeImage } from "./decodeQrCodeImage";
import { type AuthenticatorSaveError } from "./authenticatorSaveError";
+type PasswordCredentialContentValues = {
+ name: string;
+ username: string;
+ password: string;
+ totp: string;
+ totp_type: string;
+ totp_identifier: string;
+};
+
type Props = {
- values: {
- name: string;
- username: string;
- password: string;
- totp: string;
- totp_type: "authenticator" | "email" | "text" | "none";
- totp_identifier: string;
- };
- onChange: (values: {
- name: string;
- username: string;
- password: string;
- totp: string;
- totp_type: "authenticator" | "email" | "text" | "none";
- totp_identifier: string;
- }) => void;
+ values: PasswordCredentialContentValues;
+ onChange: (values: PasswordCredentialContentValues) => void;
/** Login page URL value — when onUrlChange is provided, a URL field is rendered after Name */
url?: string;
onUrlChange?: (url: string) => void;
@@ -68,6 +66,16 @@ type Props = {
/** Slot rendered right before the separator between Name/URL and Username/Password */
beforeCredentialFields?: React.ReactNode;
editMode?: boolean;
+ configuredAdditionalTwoFactorMethod?: string;
+ additionalTwoFactorStates?: Record<
+ string,
+ CredentialAdditionalTwoFactorState
+ >;
+ onAdditionalTwoFactorStateChange?: (
+ methodValue: string,
+ next: CredentialAdditionalTwoFactorState,
+ ) => void;
+ additionalTwoFactorError?: string | null;
editingGroups?: { name: boolean; values: boolean };
onEnableEditName?: () => void;
onEnableEditValues?: () => void;
@@ -105,6 +113,97 @@ function isStandardAuthenticatorValue(value: string): boolean {
return compact.length >= 16 && /^[A-Z2-7]+=*$/i.test(compact);
}
+function useAdditionalTwoFactorMethodEnabled(
+ method: CredentialAdditionalTwoFactorMethod,
+): boolean {
+ const flagEnabled = useFeatureFlag(method.flagName ?? "");
+ return method.flagName ? flagEnabled === true : true;
+}
+
+function AdditionalTwoFactorMethodCard({
+ method,
+ selected,
+ onSelect,
+}: {
+ method: CredentialAdditionalTwoFactorMethod;
+ selected: boolean;
+ onSelect: () => void;
+}) {
+ const enabled = useAdditionalTwoFactorMethodEnabled(method);
+ if (!enabled) {
+ return null;
+ }
+
+ return (
+ <button
+ type="button"
+ aria-pressed={selected}
+ className={cn(
+ "relative flex h-36 cursor-pointer items-center justify-center gap-2 rounded-lg border border-transparent bg-slate-elevation1 hover:bg-slate-elevation3",
+ selected &&
+ "border-blue-400 bg-slate-elevation3 ring-1 ring-blue-400/60",
+ )}
+ onClick={onSelect}
+ >
+ {selected && (
+ <span className="absolute right-3 top-3 flex size-5 items-center justify-center rounded-full bg-blue-500 text-white">
+ <CheckIcon className="size-3" />
+ </span>
+ )}
+ {method.icon}
+ <Label className="cursor-pointer text-center">{method.label}</Label>
+ </button>
+ );
+}
+
+function AdditionalTwoFactorMethodFields({
+ method,
+ state,
+ setState,
+ disabled,
+ isEditMode,
+ configured,
+ error,
+}: {
+ method: CredentialAdditionalTwoFactorMethod;
+ state: CredentialAdditionalTwoFactorState;
+ setState: (next: CredentialAdditionalTwoFactorState) => void;
+ disabled: boolean;
+ isEditMode: boolean;
+ configured: boolean;
+ error?: string | null;
+}) {
+ const enabled = useAdditionalTwoFactorMethodEnabled(method);
+ const validationErrorId = useId();
+ if (!enabled) {
+ return configured ? (
+ <div className="flex items-center gap-3 rounded-md border border-input bg-background px-3 py-2">
+ {method.icon}
+ <span className="text-sm font-medium">{method.label}</span>
+ <span className="text-xs text-muted-foreground">Configured</span>
+ </div>
+ ) : null;
+ }
+
+ return (
+ <div className="space-y-4">
+ {method.renderFields({
+ state,
+ setState,
+ disabled,
+ isEditMode,
+ configured,
+ validationErrorId: error ? validationErrorId : undefined,
+ })}
+ {error && (
+ <p id={validationErrorId} className="text-xs text-destructive">
+ {error}
+ </p>
+ )}
+ </div>
+ );
+}
+
function PasswordCredentialContent({
values,
onChange,
@@ -115,13 +214,18 @@ function PasswordCredentialContent({
afterUrl,
beforeCredentialFields,
editMode,
+ configuredAdditionalTwoFactorMethod,
+ additionalTwoFactorStates = {},
+ onAdditionalTwoFactorStateChange,
+ additionalTwoFactorError,
editingGroups,
onEnableEditName,
onEnableEditValues,
totpError,
authenticatorSaveError,
}: Props) {
- const { enterpriseApps } = useCredentialAuthenticatorSupport();
+ const { enterpriseApps, additionalTwoFactorMethods = [] } =
+ useCredentialAuthenticatorSupport();
const { name, username, password, totp, totp_type, totp_identifier } = values;
const enterpriseUpgradeError =
@@ -145,10 +249,8 @@ function PasswordCredentialContent({
const nameReadOnly = editMode && !editingGroups?.name;
const valuesReadOnly = editMode && !editingGroups?.values;
- const [totpMethod, setTotpMethod] = useState<
- "authenticator" | "email" | "text"
- >(
- totp_type === "email" || totp_type === "text" ? totp_type : "authenticator",
+ const [totpMethod, setTotpMethod] = useState<string>(
+ totp_type === "none" ? "authenticator" : totp_type,
);
const [totpAccordionValue, setTotpAccordionValue] = useState<string>("");
const [showPassword, setShowPassword] = useState(false);
@@ -253,10 +355,12 @@ function PasswordCredentialContent({
// Sync totpMethod and auto-expand accordion when totp_type prop changes
// (e.g. edit data arriving after mount)
useEffect(() => {
- setTotpMethod(
- totp_type === "email" || totp_type === "text"
- ? totp_type
- : "authenticator",
+ setTotpMethod((current) =>
+ totp_type === "none"
+ ? current === "none"
+ ? "none"
+ : "authenticator"
+ : totp_type,
);
if (totp_type && totp_type !== "none") {
setTotpAccordionValue("two-factor-authentication");
@@ -359,9 +463,7 @@ function PasswordCredentialContent({
// User explicitly switched the 2FA method. Apply method-specific identifier
// defaults here (rather than in a useEffect) so data-hydration setTotpMethod
// calls don't accidentally trigger them.
- const handleTotpMethodChange = (
- method: "authenticator" | "email" | "text",
- ) => {
+ const handleTotpMethodChange = (method: string) => {
onEnableEditValues?.();
const prevMethod = totpMethod;
setTotpMethod(method);
@@ -386,17 +488,32 @@ function PasswordCredentialContent({
updates.totp_identifier = "";
}
+ if (
+ method === "none" ||
+ additionalTwoFactorMethods.some(({ value }) => value === method)
+ ) {
+ updates.totp_identifier = "";
+ }
+
updateValues(updates);
};
const handleTotpAccordionValueChange = (value: string) => {
setTotpAccordionValue(value);
- if (
- value === "two-factor-authentication" &&
- totp_type === "none" &&
- !valuesReadOnly
- ) {
- handleTotpMethodChange(totpMethod);
+ if (valuesReadOnly) {
+ return;
+ }
+ if (value === "two-factor-authentication") {
+ // Opening the section activates a concrete method (there is no "None"
+ // tile); default to the authenticator app when nothing is selected yet.
+ if (totp_type === "none") {
+ handleTotpMethodChange(
+ totpMethod === "none" ? "authenticator" : totpMethod,
+ );
+ }
+ } else if (totp_type !== "none") {
+ // Collapsing the section is how the user turns two-factor off.
+ handleTotpMethodChange("none");
}
};
@@ -659,13 +776,15 @@ function PasswordCredentialContent({
<AccordionContent>
<div className="space-y-4">
<p className="text-sm text-slate-400">
- Set up Skyvern to automatically retrieve two-factor
- authentication codes.
+ Set up Skyvern to automatically complete two-factor
+ authentication.
</p>
- <div className="grid h-36 grid-cols-3 gap-4">
- <div
+ <div className="grid grid-cols-2 gap-4 sm:grid-cols-4">
+ <button
+ type="button"
+ aria-pressed={totpMethod === "authenticator"}
className={cn(
- "relative flex cursor-pointer items-center justify-center gap-2 rounded-lg border border-transparent bg-slate-elevation1 hover:bg-slate-elevation3",
+ "relative flex h-36 cursor-pointer items-center justify-center gap-2 rounded-lg border border-transparent bg-slate-elevation1 hover:bg-slate-elevation3",
{
"border-blue-400 bg-slate-elevation3 ring-1 ring-blue-400/60":
totpMethod === "authenticator",
@@ -682,10 +801,12 @@ function PasswordCredentialContent({
<Label className="cursor-pointer text-center">
Authenticator App
</Label>
- </div>
- <div
+ </button>
+ <button
+ type="button"
+ aria-pressed={totpMethod === "email"}
className={cn(
- "relative flex cursor-pointer items-center justify-center gap-2 rounded-lg border border-transparent bg-slate-elevation1 hover:bg-slate-elevation3",
+ "relative flex h-36 cursor-pointer items-center justify-center gap-2 rounded-lg border border-transparent bg-slate-elevation1 hover:bg-slate-elevation3",
{
"border-blue-400 bg-slate-elevation3 ring-1 ring-blue-400/60":
totpMethod === "email",
@@ -700,10 +821,12 @@ function PasswordCredentialContent({
)}
<EnvelopeClosedIcon className="h-6 w-6" />
<Label className="cursor-pointer text-center">Email</Label>
- </div>
- <div
+ </button>
+ <button
+ type="button"
+ aria-pressed={totpMethod === "text"}
className={cn(
- "relative flex cursor-pointer items-center justify-center gap-2 rounded-lg border border-transparent bg-slate-elevation1 hover:bg-slate-elevation3",
+ "relative flex h-36 cursor-pointer items-center justify-center gap-2 rounded-lg border border-transparent bg-slate-elevation1 hover:bg-slate-elevation3",
{
"border-blue-400 bg-slate-elevation3 ring-1 ring-blue-400/60":
totpMethod === "text",
@@ -720,8 +843,39 @@ function PasswordCredentialContent({
<Label className="cursor-pointer text-center">
Text Message
</Label>
- </div>
+ </button>
+ {additionalTwoFactorMethods.map((method) => (
+ <AdditionalTwoFactorMethodCard
+ key={method.value}
+ method={method}
+ selected={totpMethod === method.value}
+ onSelect={() => handleTotpMethodChange(method.value)}
+ />
+ ))}
</div>
+ {additionalTwoFactorMethods.map((method) =>
+ totpMethod === method.value ? (
+ <AdditionalTwoFactorMethodFields
+ key={method.value}
+ method={method}
+ state={
+ additionalTwoFactorStates[method.value] ??
+ method.initialState ??
+ {}
+ }
+ setState={(next) => {
+ onEnableEditValues?.();
+ onAdditionalTwoFactorStateChange?.(method.value, next);
+ }}
+ disabled={Boolean(valuesReadOnly)}
+ isEditMode={Boolean(editMode)}
+ configured={
+ configuredAdditionalTwoFactorMethod === method.value
+ }
+ error={additionalTwoFactorError}
+ />
+ ) : null,
+ )}
{(totpMethod === "text" || totpMethod === "email") && (
<>
<div className="space-y-2">
@@ -937,4 +1091,4 @@ function PasswordCredentialContent({
);
}
-export { PasswordCredentialContent };
+export { PasswordCredentialContent, type PasswordCredentialContentValues };
diff --git a/skyvern-frontend/src/routes/credentials/credentialTotpValidation.ts b/skyvern-frontend/src/routes/credentials/credentialTotpValidation.ts
index af4fabe80..7dfbadfe7 100644
--- a/skyvern-frontend/src/routes/credentials/credentialTotpValidation.ts
+++ b/skyvern-frontend/src/routes/credentials/credentialTotpValidation.ts
@@ -2,7 +2,7 @@ const AUTHENTICATOR_KEY_REQUIRED_MESSAGE = "Authenticator key is required.";
type AuthenticatorKeyValues = {
totp: string;
- totp_type: "authenticator" | "email" | "text" | "none";
+ totp_type: string;
};
function getAuthenticatorKeyError(
diff --git a/skyvern-frontend/src/routes/workflows/components/SheetTabCombobox.test.tsx b/skyvern-frontend/src/routes/workflows/components/SheetTabCombobox.test.tsx
new file mode 100644
index 000000000..29dc130b2
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/components/SheetTabCombobox.test.tsx
@@ -0,0 +1,160 @@
+// @vitest-environment jsdom
+
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import type { ComponentProps, ReactNode } from "react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { SheetTabCombobox } from "./SheetTabCombobox";
+
+const mocks = vi.hoisted(() => ({
+ capture: vi.fn(),
+ useCreateGoogleSheetTab: vi.fn(),
+ useGoogleSheetTabs: vi.fn(),
+}));
+
+vi.mock("@/components/WorkflowBlockInputTextarea", () => ({
+ WorkflowBlockInputTextarea: ({
+ value,
+ onChange,
+ onFocus,
+ placeholder,
+ }: {
+ value: string;
+ onChange: (value: string) => void;
+ onFocus?: () => void;
+ placeholder?: string;
+ }) => (
+ <textarea
+ value={value}
+ onChange={(event) => onChange(event.target.value)}
+ onFocus={onFocus}
+ placeholder={placeholder}
+ />
+ ),
+}));
+
+vi.mock("@/components/ui/popover", async () => {
+ const React = await vi.importActual<typeof import("react")>("react");
+ const OpenContext = React.createContext(false);
+
+ return {
+ Popover: ({ open, children }: { open?: boolean; children?: ReactNode }) => (
+ <OpenContext.Provider value={Boolean(open)}>
+ {children}
+ </OpenContext.Provider>
+ ),
+ PopoverAnchor: ({ children }: { children?: ReactNode }) => <>{children}</>,
+ PopoverContent: ({ children }: { children?: ReactNode }) => (
+ <div
+ data-testid="popover-content"
+ data-open={String(React.useContext(OpenContext))}
+ >
+ {children}
+ </div>
+ ),
+ };
+});
+
+vi.mock("@/hooks/useGoogleSheetTabs", () => ({
+ useGoogleSheetTabs: mocks.useGoogleSheetTabs,
+}));
+vi.mock("@/hooks/useCreateGoogleSheetTab", () => ({
+ useCreateGoogleSheetTab: mocks.useCreateGoogleSheetTab,
+}));
+vi.mock("@/hooks/useCurrentOrgId", () => ({
+ useCurrentOrgId: () => "org_test",
+}));
+vi.mock("posthog-js/react", () => ({
+ usePostHog: () => ({ capture: mocks.capture }),
+}));
+
+const spreadsheetUrl =
+ "https://docs.google.com/spreadsheets/d/spreadsheet_test_id_123/edit";
+
+function renderCombobox(
+ overrides: Partial<ComponentProps<typeof SheetTabCombobox>> = {},
+) {
+ const props: ComponentProps<typeof SheetTabCombobox> = {
+ nodeId: "node-1",
+ credentialId: "credential-1",
+ hasSelectedAccount: true,
+ spreadsheetUrl,
+ value: "",
+ placeholder: "Select a sheet",
+ allowCreate: false,
+ blockType: "google_sheets_read",
+ templateMode: false,
+ onTemplateModeChange: vi.fn(),
+ onChange: vi.fn(),
+ onSelect: vi.fn(),
+ ...overrides,
+ };
+
+ render(<SheetTabCombobox {...props} />);
+ return props;
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.useGoogleSheetTabs.mockReturnValue({
+ data: [
+ {
+ sheet_id: 1,
+ title: "Summary",
+ },
+ ],
+ error: null,
+ isFetching: false,
+ isLoading: false,
+ });
+ mocks.useCreateGoogleSheetTab.mockReturnValue({
+ isPending: false,
+ mutateAsync: vi.fn(),
+ });
+});
+
+afterEach(() => {
+ cleanup();
+});
+
+describe("SheetTabCombobox", () => {
+ it("opens the picker on focus with list content and the template footer", () => {
+ renderCombobox();
+
+ fireEvent.focus(screen.getByRole("textbox"));
+
+ expect(screen.getByTestId("popover-content").dataset.open).toBe("true");
+ expect(screen.getByText("Summary")).toBeTruthy();
+ expect(screen.getByText("Use template expression")).toBeTruthy();
+ });
+
+ it("requests template mode from the footer and closes the popover", () => {
+ const onChange = vi.fn();
+ const onTemplateModeChange = vi.fn();
+ renderCombobox({ onChange, onTemplateModeChange });
+ fireEvent.focus(screen.getByRole("textbox"));
+
+ fireEvent.click(screen.getByText("Use template expression"));
+
+ expect(onTemplateModeChange).toHaveBeenCalledWith(true);
+ expect(screen.getByTestId("popover-content").dataset.open).toBe("false");
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it("suppresses the picker in controlled template mode", () => {
+ renderCombobox({ templateMode: true });
+
+ expect(screen.getByRole("textbox").getAttribute("placeholder")).toBe(
+ "sheet_{{ current_index }}",
+ );
+ fireEvent.focus(screen.getByRole("textbox"));
+ expect(screen.getByTestId("popover-content").dataset.open).toBe("false");
+ });
+
+ it("suppresses the picker for template values when controlled mode is false", () => {
+ renderCombobox({ value: "{{ sheet_name }}", templateMode: false });
+ fireEvent.focus(screen.getByRole("textbox"));
+
+ expect(screen.getByTestId("popover-content").dataset.open).toBe("false");
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/components/SheetTabCombobox.tsx b/skyvern-frontend/src/routes/workflows/components/SheetTabCombobox.tsx
index 9cf2aed6b..a49fb508a 100644
--- a/skyvern-frontend/src/routes/workflows/components/SheetTabCombobox.tsx
+++ b/skyvern-frontend/src/routes/workflows/components/SheetTabCombobox.tsx
@@ -21,6 +21,7 @@ import {
type SheetsBlockType,
} from "@/util/sheetsTelemetry";
import { InlineCreateRow } from "./InlineCreateRow";
+import { TemplateExpressionRow } from "./TemplateExpressionRow";
type Props = {
nodeId: string;
@@ -31,6 +32,8 @@ type Props = {
placeholder?: string;
allowCreate: boolean;
blockType: SheetsBlockType;
+ templateMode: boolean;
+ onTemplateModeChange: (enabled: boolean) => void;
onChange: (value: string) => void;
onSelect: (tabName: string) => void;
};
@@ -44,6 +47,8 @@ function SheetTabCombobox({
placeholder,
allowCreate,
blockType,
+ templateMode,
+ onTemplateModeChange,
onChange,
onSelect,
}: Props) {
@@ -51,6 +56,8 @@ function SheetTabCombobox({
const anchorRef = useRef<HTMLDivElement>(null);
const postHog = usePostHog();
const orgId = useCurrentOrgId();
+ // Re-derived locally so the field renders template values correctly even if a caller forgets to OR them into templateMode.
+ const effectiveTemplateMode = templateMode || isTemplateExpression(value);
const isTypeable =
hasSelectedAccount &&
@@ -59,19 +66,27 @@ function SheetTabCombobox({
!isTemplateExpression(spreadsheetUrl) &&
extractSpreadsheetIdFromUrl(spreadsheetUrl) !== null &&
!isTemplateExpression(value);
+ const canPick = isTypeable && !effectiveTemplateMode;
const handleChange = (nextValue: string) => {
onChange(nextValue);
};
const handleFocus = () => {
- if (isTypeable) {
+ if (canPick) {
setIsOpen(true);
}
};
+ const handleUseTemplateExpression = () => {
+ setIsOpen(false);
+ onTemplateModeChange(true);
+ };
+
+ const popoverOpen = isOpen && canPick;
+
return (
- <Popover open={isOpen && isTypeable} onOpenChange={setIsOpen}>
+ <Popover open={popoverOpen} onOpenChange={setIsOpen}>
<PopoverAnchor asChild>
<div ref={anchorRef} className="relative">
<WorkflowBlockInputTextarea
@@ -79,7 +94,9 @@ function SheetTabCombobox({
value={value}
onChange={handleChange}
onFocus={handleFocus}
- placeholder={placeholder}
+ placeholder={
+ effectiveTemplateMode ? "sheet_{{ current_index }}" : placeholder
+ }
hideActions={!hasSelectedAccount}
className="nopan text-xs"
/>
@@ -112,6 +129,7 @@ function SheetTabCombobox({
setIsOpen(false);
}}
/>
+ <TemplateExpressionRow onClick={handleUseTemplateExpression} />
</PopoverContent>
</Popover>
);
diff --git a/skyvern-frontend/src/routes/workflows/components/SpreadsheetCombobox.test.tsx b/skyvern-frontend/src/routes/workflows/components/SpreadsheetCombobox.test.tsx
new file mode 100644
index 000000000..98d5d213e
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/components/SpreadsheetCombobox.test.tsx
@@ -0,0 +1,195 @@
+// @vitest-environment jsdom
+
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import type { ComponentProps, ReactNode } from "react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { SpreadsheetCombobox } from "./SpreadsheetCombobox";
+
+const mocks = vi.hoisted(() => ({
+ capture: vi.fn(),
+ useCreateGoogleSpreadsheet: vi.fn(),
+ useGoogleSpreadsheets: vi.fn(),
+}));
+
+vi.mock("@/components/WorkflowBlockInputTextarea", () => ({
+ WorkflowBlockInputTextarea: ({
+ value,
+ onChange,
+ onFocus,
+ placeholder,
+ }: {
+ value: string;
+ onChange: (value: string) => void;
+ onFocus?: () => void;
+ placeholder?: string;
+ }) => (
+ <textarea
+ value={value}
+ onChange={(event) => onChange(event.target.value)}
+ onFocus={onFocus}
+ placeholder={placeholder}
+ />
+ ),
+}));
+
+vi.mock("@/components/ui/popover", async () => {
+ const React = await vi.importActual<typeof import("react")>("react");
+ const OpenContext = React.createContext(false);
+
+ return {
+ Popover: ({ open, children }: { open?: boolean; children?: ReactNode }) => (
+ <OpenContext.Provider value={Boolean(open)}>
+ {children}
+ </OpenContext.Provider>
+ ),
+ PopoverAnchor: ({ children }: { children?: ReactNode }) => <>{children}</>,
+ PopoverContent: ({ children }: { children?: ReactNode }) => (
+ <div
+ data-testid="popover-content"
+ data-open={String(React.useContext(OpenContext))}
+ >
+ {children}
+ </div>
+ ),
+ };
+});
+
+vi.mock("@/hooks/useGoogleSpreadsheets", () => ({
+ useGoogleSpreadsheets: mocks.useGoogleSpreadsheets,
+}));
+vi.mock("@/hooks/useCreateGoogleSpreadsheet", () => ({
+ useCreateGoogleSpreadsheet: mocks.useCreateGoogleSpreadsheet,
+}));
+vi.mock("@/hooks/useCurrentOrgId", () => ({
+ useCurrentOrgId: () => "org_test",
+}));
+vi.mock("posthog-js/react", () => ({
+ usePostHog: () => ({ capture: mocks.capture }),
+}));
+
+const spreadsheetUrl =
+ "https://docs.google.com/spreadsheets/d/spreadsheet_test_id_123/edit";
+
+function renderCombobox(
+ overrides: Partial<ComponentProps<typeof SpreadsheetCombobox>> = {},
+) {
+ const props: ComponentProps<typeof SpreadsheetCombobox> = {
+ nodeId: "node-1",
+ credentialId: "credential-1",
+ hasSelectedAccount: true,
+ value: "",
+ displayName: null,
+ placeholder: "Select a spreadsheet",
+ allowCreate: false,
+ blockType: "google_sheets_write",
+ templateMode: false,
+ onTemplateModeChange: vi.fn(),
+ onChange: vi.fn(),
+ onSelect: vi.fn(),
+ ...overrides,
+ };
+
+ render(<SpreadsheetCombobox {...props} />);
+ return props;
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.useGoogleSpreadsheets.mockReturnValue({
+ data: {
+ pages: [
+ {
+ spreadsheets: [
+ {
+ id: "spreadsheet_test_id_123",
+ name: "Budget Plan",
+ modified_time: null,
+ },
+ ],
+ },
+ ],
+ },
+ error: null,
+ fetchNextPage: vi.fn(),
+ hasNextPage: false,
+ isFetchingNextPage: false,
+ isLoading: false,
+ });
+ mocks.useCreateGoogleSpreadsheet.mockReturnValue({
+ isPending: false,
+ mutateAsync: vi.fn(),
+ });
+});
+
+afterEach(() => {
+ cleanup();
+});
+
+describe("SpreadsheetCombobox", () => {
+ it("opens the picker on focus with list content and the template footer", () => {
+ renderCombobox();
+
+ fireEvent.focus(screen.getByRole("textbox"));
+
+ expect(screen.getByTestId("popover-content").dataset.open).toBe("true");
+ expect(screen.getByText("Budget Plan")).toBeTruthy();
+ expect(screen.getByText("Use template expression")).toBeTruthy();
+ });
+
+ it("requests template mode from the footer and closes the popover", () => {
+ const onChange = vi.fn();
+ const onTemplateModeChange = vi.fn();
+ renderCombobox({ onChange, onTemplateModeChange });
+ fireEvent.focus(screen.getByRole("textbox"));
+
+ fireEvent.click(screen.getByText("Use template expression"));
+
+ expect(onTemplateModeChange).toHaveBeenCalledWith(true);
+ expect(screen.getByTestId("popover-content").dataset.open).toBe("false");
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it("suppresses the picker and renders the raw value in controlled template mode", () => {
+ renderCombobox({
+ displayName: "Budget Plan",
+ value: spreadsheetUrl,
+ templateMode: true,
+ });
+
+ expect(screen.getByRole<HTMLTextAreaElement>("textbox").value).toBe(
+ spreadsheetUrl,
+ );
+ expect(screen.getByRole("textbox").getAttribute("placeholder")).toBe(
+ "{{ target_spreadsheet_url }}",
+ );
+
+ fireEvent.focus(screen.getByRole("textbox"));
+ expect(screen.getByTestId("popover-content").dataset.open).toBe("false");
+ });
+
+ it("suppresses the picker for template values when controlled mode is false", () => {
+ renderCombobox({ value: "{{ sheet_url }}", templateMode: false });
+ fireEvent.focus(screen.getByRole("textbox"));
+
+ expect(screen.getByTestId("popover-content").dataset.open).toBe("false");
+ });
+
+ it("never searches for a template expression", () => {
+ renderCombobox({
+ value: "{{ target_spreadsheet_url }}",
+ templateMode: false,
+ });
+
+ expect(mocks.useGoogleSpreadsheets).toHaveBeenCalled();
+ expect(
+ mocks.useGoogleSpreadsheets.mock.calls.some(
+ ([options]) =>
+ typeof options.query === "string" && options.query.includes("{{"),
+ ),
+ ).toBe(false);
+ expect(mocks.useGoogleSpreadsheets).toHaveBeenLastCalledWith(
+ expect.objectContaining({ query: "" }),
+ );
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/components/SpreadsheetCombobox.tsx b/skyvern-frontend/src/routes/workflows/components/SpreadsheetCombobox.tsx
index 04ff9d4f1..4774a667e 100644
--- a/skyvern-frontend/src/routes/workflows/components/SpreadsheetCombobox.tsx
+++ b/skyvern-frontend/src/routes/workflows/components/SpreadsheetCombobox.tsx
@@ -25,6 +25,7 @@ import {
type SheetsBlockType,
} from "@/util/sheetsTelemetry";
import { InlineCreateRow } from "./InlineCreateRow";
+import { TemplateExpressionRow } from "./TemplateExpressionRow";
type Selection = {
url: string;
@@ -41,6 +42,8 @@ type Props = {
placeholder?: string;
allowCreate: boolean;
blockType: SheetsBlockType;
+ templateMode: boolean;
+ onTemplateModeChange: (enabled: boolean) => void;
onChange: (value: string) => void;
onSelect: (selection: Selection) => void;
};
@@ -54,6 +57,8 @@ function SpreadsheetCombobox({
placeholder,
allowCreate,
blockType,
+ templateMode,
+ onTemplateModeChange,
onChange,
onSelect,
}: Props) {
@@ -61,7 +66,9 @@ function SpreadsheetCombobox({
const anchorRef = useRef<HTMLDivElement>(null);
const postHog = usePostHog();
const orgId = useCurrentOrgId();
- const renderedValue = displayName ?? value;
+ // Re-derived locally so the field renders template values correctly even if a caller forgets to OR them into templateMode.
+ const effectiveTemplateMode = templateMode || isTemplateExpression(value);
+ const renderedValue = effectiveTemplateMode ? value : (displayName ?? value);
// Skip the Drive search whenever we already have a resolved selection
// (displayName) or the input is a parseable URL/ID. Drive `q` searches by
// title, so URLs and post-selection titles only generate wasted requests.
@@ -70,24 +77,35 @@ function SpreadsheetCombobox({
? ""
: renderedValue;
const [debouncedQuery] = useDebounce(queryForSearch, 300);
+ // The debounced value can lag a just-cleared template by 300ms; a template
+ // is never a meaningful Drive title search, so drop it instead of querying.
+ const searchQuery = isTemplateExpression(debouncedQuery)
+ ? ""
+ : debouncedQuery;
const isTypeable =
hasSelectedAccount &&
Boolean(credentialId) &&
!isTemplateExpression(credentialId) &&
!isTemplateExpression(value);
+ const canPick = isTypeable && !effectiveTemplateMode;
const handleChange = (nextValue: string) => {
onChange(nextValue);
};
const handleFocus = () => {
- if (isTypeable) {
+ if (canPick) {
setIsOpen(true);
}
};
- const popoverOpen = isOpen && isTypeable;
+ const handleUseTemplateExpression = () => {
+ setIsOpen(false);
+ onTemplateModeChange(true);
+ };
+
+ const popoverOpen = isOpen && canPick;
useEffect(() => {
if (popoverOpen) {
postHog?.capture("sheets.spreadsheet.picker.opened", {
@@ -114,7 +132,7 @@ function SpreadsheetCombobox({
};
return (
- <Popover open={isOpen && isTypeable} onOpenChange={setIsOpen}>
+ <Popover open={popoverOpen} onOpenChange={setIsOpen}>
<PopoverAnchor asChild>
<div ref={anchorRef} className="relative">
<WorkflowBlockInputTextarea
@@ -122,7 +140,11 @@ function SpreadsheetCombobox({
value={renderedValue}
onChange={handleChange}
onFocus={handleFocus}
- placeholder={placeholder}
+ placeholder={
+ effectiveTemplateMode
+ ? "{{ target_spreadsheet_url }}"
+ : placeholder
+ }
hideActions={!hasSelectedAccount}
className="nopan text-xs"
/>
@@ -141,11 +163,12 @@ function SpreadsheetCombobox({
>
<SpreadsheetListPanel
credentialId={credentialId}
- query={debouncedQuery}
+ query={searchQuery}
allowCreate={allowCreate}
blockType={blockType}
onPick={handlePick}
/>
+ <TemplateExpressionRow onClick={handleUseTemplateExpression} />
</PopoverContent>
</Popover>
);
diff --git a/skyvern-frontend/src/routes/workflows/components/TemplateExpressionRow.test.tsx b/skyvern-frontend/src/routes/workflows/components/TemplateExpressionRow.test.tsx
new file mode 100644
index 000000000..74f05e800
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/components/TemplateExpressionRow.test.tsx
@@ -0,0 +1,25 @@
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { TemplateExpressionRow } from "./TemplateExpressionRow";
+
+afterEach(() => {
+ cleanup();
+});
+
+describe("TemplateExpressionRow", () => {
+ it("renders the template-expression action and handles clicks", () => {
+ const onClick = vi.fn();
+ render(<TemplateExpressionRow onClick={onClick} />);
+
+ expect(screen.getByText("{{}}")).toBeTruthy();
+ const button = screen
+ .getByText("Use template expression")
+ .closest("button");
+ expect(button).not.toBeNull();
+
+ fireEvent.click(button!);
+
+ expect(onClick).toHaveBeenCalledOnce();
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/components/TemplateExpressionRow.tsx b/skyvern-frontend/src/routes/workflows/components/TemplateExpressionRow.tsx
new file mode 100644
index 000000000..2e7b1dc1b
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/components/TemplateExpressionRow.tsx
@@ -0,0 +1,20 @@
+type Props = {
+ onClick: () => void;
+};
+
+function TemplateExpressionRow({ onClick }: Props) {
+ return (
+ <div className="border-t border-border">
+ <button
+ type="button"
+ onClick={onClick}
+ className="flex w-full items-center gap-2 px-3 py-2 text-left text-xs text-foreground hover:bg-muted dark:text-slate-200 dark:hover:bg-slate-700"
+ >
+ <span className="font-mono text-xs">{"{{}}"}</span>
+ <span>Use template expression</span>
+ </button>
+ </div>
+ );
+}
+
+export { TemplateExpressionRow };
diff --git a/skyvern-frontend/src/routes/workflows/components/TemplateModeToggle.test.tsx b/skyvern-frontend/src/routes/workflows/components/TemplateModeToggle.test.tsx
new file mode 100644
index 000000000..46dff1dbb
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/components/TemplateModeToggle.test.tsx
@@ -0,0 +1,54 @@
+// @vitest-environment jsdom
+
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { TemplateModeToggle } from "./TemplateModeToggle";
+
+afterEach(() => {
+ cleanup();
+});
+
+describe("TemplateModeToggle", () => {
+ it("uses the custom-value label when not pressed and fires onToggle", () => {
+ const onToggle = vi.fn();
+ render(
+ <TemplateModeToggle
+ pressed={false}
+ pickerTitle="Pick from your spreadsheets"
+ onToggle={onToggle}
+ />,
+ );
+
+ const toggle = screen.getByRole("button", {
+ name: "Enter a custom value",
+ });
+ expect(toggle.getAttribute("title")).toBe("Enter a custom value");
+ expect(toggle.getAttribute("aria-pressed")).toBe("false");
+
+ fireEvent.click(toggle);
+
+ expect(onToggle).toHaveBeenCalledWith(true);
+ });
+
+ it("uses the picker label when pressed", () => {
+ const onToggle = vi.fn();
+ render(
+ <TemplateModeToggle
+ pressed={true}
+ pickerTitle="Pick from your spreadsheets"
+ onToggle={onToggle}
+ />,
+ );
+
+ const toggle = screen.getByRole("button", {
+ name: "Pick from your spreadsheets",
+ });
+ expect(toggle.getAttribute("title")).toBe("Pick from your spreadsheets");
+ expect(toggle.getAttribute("aria-pressed")).toBe("true");
+
+ fireEvent.click(toggle);
+
+ expect(onToggle).toHaveBeenCalledWith(false);
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/components/TemplateModeToggle.tsx b/skyvern-frontend/src/routes/workflows/components/TemplateModeToggle.tsx
new file mode 100644
index 000000000..bca9779eb
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/components/TemplateModeToggle.tsx
@@ -0,0 +1,31 @@
+import { CodeIcon } from "@radix-ui/react-icons";
+
+import { cn } from "@/util/utils";
+
+type Props = {
+ pressed: boolean;
+ pickerTitle: string;
+ onToggle: (enabled: boolean) => void;
+};
+
+function TemplateModeToggle({ pressed, pickerTitle, onToggle }: Props) {
+ const label = pressed ? pickerTitle : "Enter a custom value";
+
+ return (
+ <button
+ type="button"
+ aria-label={label}
+ aria-pressed={pressed}
+ title={label}
+ className={cn(
+ "rounded p-1 text-muted-foreground transition-colors hover:text-foreground dark:hover:text-slate-200",
+ pressed && "bg-muted text-foreground dark:bg-slate-700",
+ )}
+ onClick={() => onToggle(!pressed)}
+ >
+ <CodeIcon className="size-4" />
+ </button>
+ );
+}
+
+export { TemplateModeToggle };
diff --git a/skyvern-frontend/src/routes/workflows/editor/helpContent.ts b/skyvern-frontend/src/routes/workflows/editor/helpContent.ts
index 85e034633..ef02a5e29 100644
--- a/skyvern-frontend/src/routes/workflows/editor/helpContent.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/helpContent.ts
@@ -107,9 +107,9 @@ export const helpTooltips = {
google_sheets_read: {
...baseHelpTooltipContent,
spreadsheetUrl:
- "Paste a Google Sheets URL, or click the table icon to pick from your connected account. Jinja templates work too.",
+ "Paste a Google Sheets URL or pick from your connected account. Click the </> button to pass in a custom value instead — for example {{ target_spreadsheet_url }}.",
sheetName:
- "Pick a tab via the layers icon once a spreadsheet is selected, or type the name directly.",
+ "Pick a tab once a spreadsheet is selected, or type the name directly. Click the </> button to pass in a custom value like sheet_{{ current_index }}.",
range: "A1 notation range to read (optional, defaults to all data)",
credentialId: "The credential ID for Google OAuth authentication",
hasHeaderRow:
@@ -130,11 +130,11 @@ export const helpTooltips = {
google_sheets_write: {
...baseHelpTooltipContent,
spreadsheetUrl:
- "The full URL of the Google Sheet to write to. Use the picker to browse your connected account.",
+ "The Google Sheet to write to. Pick from your connected account, or click the </> button to pass in a custom value instead — for example {{ target_spreadsheet_url }}.",
credentialId:
"The Google account used to authenticate with the spreadsheet.",
sheetName:
- "The sheet tab to write to. Use the picker to list tabs or create a new one.",
+ "The sheet tab to write to. Pick a tab or create a new one. Click the </> button to pass in a custom value like sheet_{{ current_index }}.",
writeMode:
"Append adds new rows below existing data. Update Range overwrites the exact cells in the range you specify.",
range:
diff --git a/skyvern-frontend/src/routes/workflows/editor/hooks/useTemplateMode.ts b/skyvern-frontend/src/routes/workflows/editor/hooks/useTemplateMode.ts
new file mode 100644
index 000000000..850bd1b64
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/hooks/useTemplateMode.ts
@@ -0,0 +1,37 @@
+import { useState } from "react";
+import { usePostHog } from "posthog-js/react";
+
+import { useCurrentOrgId } from "@/hooks/useCurrentOrgId";
+import { isTemplateExpression } from "@/util/googleSheetsUrl";
+
+type Options = {
+ value: string;
+ event:
+ | "sheets.spreadsheet.picker.template_mode_toggled"
+ | "sheets.tab.template_mode_toggled";
+ blockType: "google_sheets_read" | "google_sheets_write";
+ onClear: () => void;
+};
+
+function useTemplateMode({ value, event, blockType, onClear }: Options) {
+ const [templateMode, setTemplateMode] = useState(false);
+ const postHog = usePostHog();
+ const orgId = useCurrentOrgId();
+ const pressed = templateMode || isTemplateExpression(value);
+
+ const onChange = (enabled: boolean) => {
+ setTemplateMode(enabled);
+ if (enabled === false && isTemplateExpression(value)) {
+ onClear();
+ }
+ postHog?.capture(event, {
+ org_id: orgId,
+ block_type: blockType,
+ enabled,
+ });
+ };
+
+ return { pressed, onChange };
+}
+
+export { useTemplateMode };
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/GoogleSheetsReadNode/GoogleSheetsReadEditor.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/GoogleSheetsReadNode/GoogleSheetsReadEditor.tsx
index 92a45e0dc..e66e43b9c 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/GoogleSheetsReadNode/GoogleSheetsReadEditor.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/GoogleSheetsReadNode/GoogleSheetsReadEditor.tsx
@@ -20,6 +20,7 @@ import { useGoogleSpreadsheet } from "@/hooks/useGoogleSpreadsheet";
import { GoogleOAuthCredentialSelector } from "@/routes/workflows/components/GoogleOAuthCredentialSelector";
import { SheetTabCombobox } from "@/routes/workflows/components/SheetTabCombobox";
import { SpreadsheetCombobox } from "@/routes/workflows/components/SpreadsheetCombobox";
+import { TemplateModeToggle } from "@/routes/workflows/components/TemplateModeToggle";
import {
extractSpreadsheetIdFromUrl,
isTemplateExpression,
@@ -31,6 +32,7 @@ import {
type GoogleSheetsReadNode,
type GoogleSheetsReadNodeData,
} from "./types";
+import { useTemplateMode } from "../../hooks/useTemplateMode";
import { ParametersMultiSelect } from "../TaskNode/ParametersMultiSelect";
import { useUpdate } from "../../useUpdate";
import { getAvailableOutputParameterKeys } from "../../workflowEditorUtils";
@@ -43,7 +45,15 @@ function GoogleSheetsReadEditor({ blockId }: { blockId: string }) {
if (!nodeSlice || nodeSlice.type !== "googleSheetsRead") {
return null;
}
- return <GoogleSheetsReadEditorBody blockId={blockId} data={nodeSlice.data} />;
+ // Key by blockId: the sidebar reuses one GoogleSheetsReadEditorBody instance
+ // across same-type blocks; remounting resets local template-mode state.
+ return (
+ <GoogleSheetsReadEditorBody
+ key={blockId}
+ blockId={blockId}
+ data={nodeSlice.data}
+ />
+ );
}
function GoogleSheetsReadEditorBody({
@@ -68,6 +78,27 @@ function GoogleSheetsReadEditorBody({
const [spreadsheetDisplayName, setSpreadsheetDisplayName] = useState<
string | null
>(null);
+ const {
+ pressed: spreadsheetTemplateModePressed,
+ onChange: handleSpreadsheetTemplateModeChange,
+ } = useTemplateMode({
+ value: data.spreadsheetUrl,
+ event: "sheets.spreadsheet.picker.template_mode_toggled",
+ blockType: "google_sheets_read",
+ onClear: () => {
+ setSpreadsheetDisplayName(null);
+ update({ spreadsheetUrl: "" });
+ },
+ });
+ const {
+ pressed: sheetTemplateModePressed,
+ onChange: handleSheetTemplateModeChange,
+ } = useTemplateMode({
+ value: data.sheetName,
+ event: "sheets.tab.template_mode_toggled",
+ blockType: "google_sheets_read",
+ onClear: () => update({ sheetName: "" }),
+ });
const { credentials } = useGoogleOAuthCredentials();
const hasSelectedAccount =
@@ -112,15 +143,22 @@ function GoogleSheetsReadEditorBody({
</div>
<div className="space-y-2">
- <div className="flex gap-2">
- <Label className="text-xs text-tertiary-foreground">
- Spreadsheet
- </Label>
- <HelpTooltip
- content={
- helpTooltips["google_sheets_read"]?.["spreadsheetUrl"] ??
- "The spreadsheet to read. Type to search your Google Drive, or paste a spreadsheet URL."
- }
+ <div className="flex items-center justify-between gap-2">
+ <div className="flex items-center gap-2">
+ <Label className="text-xs text-tertiary-foreground">
+ Spreadsheet
+ </Label>
+ <HelpTooltip
+ content={
+ helpTooltips["google_sheets_read"]?.["spreadsheetUrl"] ??
+ "The spreadsheet to read. Type to search your Google Drive, or paste a spreadsheet URL."
+ }
+ />
+ </div>
+ <TemplateModeToggle
+ pressed={spreadsheetTemplateModePressed}
+ pickerTitle="Pick from your spreadsheets"
+ onToggle={handleSpreadsheetTemplateModeChange}
/>
</div>
<SpreadsheetCombobox
@@ -132,6 +170,8 @@ function GoogleSheetsReadEditorBody({
placeholder="Search or paste a spreadsheet URL"
allowCreate={false}
blockType="google_sheets_read"
+ templateMode={spreadsheetTemplateModePressed}
+ onTemplateModeChange={handleSpreadsheetTemplateModeChange}
onChange={(next) => {
setSpreadsheetDisplayName(null);
const oldId = extractSpreadsheetIdFromUrl(data.spreadsheetUrl);
@@ -162,15 +202,22 @@ function GoogleSheetsReadEditorBody({
<AccordionContent className="pl-6 pr-1 pt-4">
<div className="space-y-3">
<div className="space-y-2">
- <div className="flex gap-2">
- <Label className="text-xs text-tertiary-foreground">
- Sheet Name
- </Label>
- <HelpTooltip
- content={
- helpTooltips["google_sheets_read"]?.["sheetName"] ??
- "The sheet tab to read. Type to search tabs in the selected spreadsheet."
- }
+ <div className="flex items-center justify-between gap-2">
+ <div className="flex items-center gap-2">
+ <Label className="text-xs text-tertiary-foreground">
+ Sheet Name
+ </Label>
+ <HelpTooltip
+ content={
+ helpTooltips["google_sheets_read"]?.["sheetName"] ??
+ "The sheet tab to read. Type to search tabs in the selected spreadsheet."
+ }
+ />
+ </div>
+ <TemplateModeToggle
+ pressed={sheetTemplateModePressed}
+ pickerTitle="Pick from your sheet tabs"
+ onToggle={handleSheetTemplateModeChange}
/>
</div>
<SheetTabCombobox
@@ -182,6 +229,8 @@ function GoogleSheetsReadEditorBody({
placeholder="Sheet1"
allowCreate={false}
blockType="google_sheets_read"
+ templateMode={sheetTemplateModePressed}
+ onTemplateModeChange={handleSheetTemplateModeChange}
onChange={(next) => update({ sheetName: next })}
onSelect={(tabName) => update({ sheetName: tabName })}
/>
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/GoogleSheetsWriteNode/GoogleSheetsWriteEditor.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/GoogleSheetsWriteNode/GoogleSheetsWriteEditor.tsx
index 0a30031f5..41a43dc47 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/GoogleSheetsWriteNode/GoogleSheetsWriteEditor.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/GoogleSheetsWriteNode/GoogleSheetsWriteEditor.tsx
@@ -23,6 +23,7 @@ import { ColumnMappingEditor } from "@/routes/workflows/components/ColumnMapping
import { GoogleOAuthCredentialSelector } from "@/routes/workflows/components/GoogleOAuthCredentialSelector";
import { SheetTabCombobox } from "@/routes/workflows/components/SheetTabCombobox";
import { SpreadsheetCombobox } from "@/routes/workflows/components/SpreadsheetCombobox";
+import { TemplateModeToggle } from "@/routes/workflows/components/TemplateModeToggle";
import { parseColumnMapping } from "@/util/columnMappingSerialization";
import {
columnLettersToIndex,
@@ -38,6 +39,7 @@ import {
type GoogleSheetsWriteNode,
type GoogleSheetsWriteNodeData,
} from "./types";
+import { useTemplateMode } from "../../hooks/useTemplateMode";
import { ParametersMultiSelect } from "../TaskNode/ParametersMultiSelect";
import { useUpdate } from "../../useUpdate";
import { getAvailableOutputParameterKeys } from "../../workflowEditorUtils";
@@ -50,8 +52,14 @@ function GoogleSheetsWriteEditor({ blockId }: { blockId: string }) {
if (!nodeSlice || nodeSlice.type !== "googleSheetsWrite") {
return null;
}
+ // Key by blockId: the sidebar reuses one GoogleSheetsWriteEditorBody instance
+ // across same-type blocks; remounting resets local template-mode state.
return (
- <GoogleSheetsWriteEditorBody blockId={blockId} data={nodeSlice.data} />
+ <GoogleSheetsWriteEditorBody
+ key={blockId}
+ blockId={blockId}
+ data={nodeSlice.data}
+ />
);
}
@@ -77,15 +85,40 @@ function GoogleSheetsWriteEditorBody({
editable,
});
+ const [spreadsheetDisplayName, setSpreadsheetDisplayName] = useState<
+ string | null
+ >(null);
+ const {
+ pressed: spreadsheetTemplateModePressed,
+ onChange: handleSpreadsheetTemplateModeChange,
+ } = useTemplateMode({
+ value: data.spreadsheetUrl,
+ event: "sheets.spreadsheet.picker.template_mode_toggled",
+ blockType: "google_sheets_write",
+ onClear: () => {
+ setSpreadsheetDisplayName(null);
+ update({ spreadsheetUrl: "" });
+ },
+ });
+ const {
+ pressed: sheetTemplateModePressed,
+ onChange: handleSheetTemplateModeChange,
+ } = useTemplateMode({
+ value: data.sheetName,
+ event: "sheets.tab.template_mode_toggled",
+ blockType: "google_sheets_write",
+ onClear: () => update({ sheetName: "" }),
+ });
+
const headersQuery = useGoogleSheetHeaders({
credentialId: data.credentialId,
spreadsheetUrlOrId: data.spreadsheetUrl,
- sheetName: data.sheetName,
+ sheetName: sheetTemplateModePressed ? "" : data.sheetName,
});
const dimensionsQuery = useGoogleSheetDimensions({
credentialId: data.credentialId,
spreadsheetUrlOrId: data.spreadsheetUrl,
- sheetName: data.sheetName,
+ sheetName: sheetTemplateModePressed ? "" : data.sheetName,
});
const needsReconnect = isReconnectRequired(headersQuery.error);
@@ -107,10 +140,6 @@ function GoogleSheetsWriteEditorBody({
return out;
}, [dimensionsQuery.data, data.columnMapping]);
- const [spreadsheetDisplayName, setSpreadsheetDisplayName] = useState<
- string | null
- >(null);
-
const { credentials } = useGoogleOAuthCredentials();
const hasSelectedAccount =
isTemplateExpression(data.credentialId) ||
@@ -164,15 +193,22 @@ function GoogleSheetsWriteEditorBody({
</div>
<div className="space-y-2">
- <div className="flex gap-2">
- <Label className="text-xs text-tertiary-foreground">
- Spreadsheet
- </Label>
- <HelpTooltip
- content={
- helpTooltips["google_sheets_write"]?.["spreadsheetUrl"] ??
- "The spreadsheet to write to. Type to search your Google Drive, or paste a spreadsheet URL."
- }
+ <div className="flex items-center justify-between gap-2">
+ <div className="flex items-center gap-2">
+ <Label className="text-xs text-tertiary-foreground">
+ Spreadsheet
+ </Label>
+ <HelpTooltip
+ content={
+ helpTooltips["google_sheets_write"]?.["spreadsheetUrl"] ??
+ "The spreadsheet to write to. Type to search your Google Drive, or paste a spreadsheet URL."
+ }
+ />
+ </div>
+ <TemplateModeToggle
+ pressed={spreadsheetTemplateModePressed}
+ pickerTitle="Pick from your spreadsheets"
+ onToggle={handleSpreadsheetTemplateModeChange}
/>
</div>
<SpreadsheetCombobox
@@ -184,6 +220,8 @@ function GoogleSheetsWriteEditorBody({
placeholder="Search or paste a spreadsheet URL"
allowCreate={true}
blockType="google_sheets_write"
+ templateMode={spreadsheetTemplateModePressed}
+ onTemplateModeChange={handleSpreadsheetTemplateModeChange}
onChange={(next) => {
setSpreadsheetDisplayName(null);
const oldId = extractSpreadsheetIdFromUrl(data.spreadsheetUrl);
@@ -276,15 +314,22 @@ function GoogleSheetsWriteEditorBody({
</div>
<div className="space-y-2">
- <div className="flex gap-2">
- <Label className="text-xs text-tertiary-foreground">
- Sheet Name
- </Label>
- <HelpTooltip
- content={
- helpTooltips["google_sheets_write"]?.["sheetName"] ??
- "The sheet tab to write to. Type to search tabs in the selected spreadsheet."
- }
+ <div className="flex items-center justify-between gap-2">
+ <div className="flex items-center gap-2">
+ <Label className="text-xs text-tertiary-foreground">
+ Sheet Name
+ </Label>
+ <HelpTooltip
+ content={
+ helpTooltips["google_sheets_write"]?.["sheetName"] ??
+ "The sheet tab to write to. Type to search tabs in the selected spreadsheet."
+ }
+ />
+ </div>
+ <TemplateModeToggle
+ pressed={sheetTemplateModePressed}
+ pickerTitle="Pick from your sheet tabs"
+ onToggle={handleSheetTemplateModeChange}
/>
</div>
<SheetTabCombobox
@@ -296,6 +341,8 @@ function GoogleSheetsWriteEditorBody({
placeholder="Sheet1"
allowCreate={true}
blockType="google_sheets_write"
+ templateMode={sheetTemplateModePressed}
+ onTemplateModeChange={handleSheetTemplateModeChange}
onChange={(next) => update({ sheetName: next })}
onSelect={(tabName) => update({ sheetName: tabName })}
/>
diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/GoogleSheetsReadBlockForm.test.tsx b/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/GoogleSheetsReadBlockForm.test.tsx
index 9e4f443dd..29f72209e 100644
--- a/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/GoogleSheetsReadBlockForm.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/GoogleSheetsReadBlockForm.test.tsx
@@ -102,6 +102,8 @@ vi.mock("@/routes/workflows/components/SpreadsheetCombobox", () => ({
displayName: string | null;
placeholder?: string;
allowCreate: boolean;
+ templateMode: boolean;
+ onTemplateModeChange: (enabled: boolean) => void;
onChange: (value: string) => void;
onSelect: (selection: {
url: string;
@@ -114,6 +116,7 @@ vi.mock("@/routes/workflows/components/SpreadsheetCombobox", () => ({
data-value={props.value}
data-display-name={props.displayName ?? ""}
data-has-selected-account={String(props.hasSelectedAccount)}
+ data-template-mode={String(props.templateMode)}
>
<input
data-testid="spreadsheet-input"
@@ -130,6 +133,10 @@ vi.mock("@/routes/workflows/components/SpreadsheetCombobox", () => ({
})
}
/>
+ <button
+ data-testid="spreadsheet-template-mode-on"
+ onClick={() => props.onTemplateModeChange(true)}
+ />
</div>
),
}));
@@ -143,10 +150,16 @@ vi.mock("@/routes/workflows/components/SheetTabCombobox", () => ({
value: string;
placeholder?: string;
allowCreate: boolean;
+ templateMode: boolean;
+ onTemplateModeChange: (enabled: boolean) => void;
onChange: (value: string) => void;
onSelect: (tabName: string) => void;
}) => (
- <div data-testid="sheet-tab-combobox" data-value={props.value}>
+ <div
+ data-testid="sheet-tab-combobox"
+ data-value={props.value}
+ data-template-mode={String(props.templateMode)}
+ >
<input
data-testid="sheet-tab-input"
value={props.value}
@@ -156,6 +169,10 @@ vi.mock("@/routes/workflows/components/SheetTabCombobox", () => ({
data-testid="sheet-tab-select"
onClick={() => props.onSelect("Tab2")}
/>
+ <button
+ data-testid="sheet-template-mode-on"
+ onClick={() => props.onTemplateModeChange(true)}
+ />
</div>
),
}));
@@ -177,6 +194,14 @@ vi.mock("@/hooks/useGoogleSpreadsheet", () => ({
useGoogleSpreadsheet: () => ({ data: null }),
}));
+vi.mock("@/hooks/useCurrentOrgId", () => ({
+ useCurrentOrgId: () => "org_test",
+}));
+
+vi.mock("posthog-js/react", () => ({
+ usePostHog: () => ({ capture: vi.fn() }),
+}));
+
vi.mock("@/components/ui/switch", () => ({
Switch: (props: {
checked: boolean;
@@ -349,6 +374,66 @@ describe("GoogleSheetsReadBlockForm (SKY-9361)", () => {
});
});
+ test("switching blocks clears the selected spreadsheet display name", () => {
+ setGoogleSheetsReadNode("g1");
+ setGoogleSheetsReadNode("g2", {
+ spreadsheetUrl: "https://docs.google.com/spreadsheets/d/xyz456/edit",
+ });
+ const { rerender } = render(<GoogleSheetsReadBlockForm blockId="g1" />);
+
+ fireEvent.click(screen.getByTestId("spreadsheet-select"));
+ expect(screen.getByTestId("spreadsheet-combobox").dataset.displayName).toBe(
+ "My Sheet",
+ );
+
+ rerender(<GoogleSheetsReadBlockForm blockId="g2" />);
+
+ expect(screen.getByTestId("spreadsheet-combobox").dataset.displayName).toBe(
+ "",
+ );
+ expect(screen.getByTestId("spreadsheet-combobox").dataset.value).toBe(
+ "https://docs.google.com/spreadsheets/d/xyz456/edit",
+ );
+ });
+
+ test("repeated true requests keep spreadsheet template mode on", () => {
+ setGoogleSheetsReadNode("g1", {
+ spreadsheetUrl: "{{ target_spreadsheet_url }}",
+ });
+ render(<GoogleSheetsReadBlockForm blockId="g1" />);
+
+ const requestTemplateMode = screen.getByTestId(
+ "spreadsheet-template-mode-on",
+ );
+ fireEvent.click(requestTemplateMode);
+ fireEvent.click(requestTemplateMode);
+
+ expect(
+ screen
+ .getByRole("button", { name: "Pick from your spreadsheets" })
+ .getAttribute("aria-pressed"),
+ ).toBe("true");
+ expect(updateNodeData).not.toHaveBeenCalled();
+ });
+
+ test("repeated true requests keep sheet template mode on", () => {
+ setGoogleSheetsReadNode("g1", {
+ sheetName: "{{ target_sheet_name }}",
+ });
+ render(<GoogleSheetsReadBlockForm blockId="g1" />);
+
+ const requestTemplateMode = screen.getByTestId("sheet-template-mode-on");
+ fireEvent.click(requestTemplateMode);
+ fireEvent.click(requestTemplateMode);
+
+ expect(
+ screen
+ .getByRole("button", { name: "Pick from your sheet tabs" })
+ .getAttribute("aria-pressed"),
+ ).toBe("true");
+ expect(updateNodeData).not.toHaveBeenCalled();
+ });
+
test("selecting a sheet propagates sheetName", () => {
setGoogleSheetsReadNode("g1");
render(<GoogleSheetsReadBlockForm blockId="g1" />);
diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/GoogleSheetsWriteBlockForm.test.tsx b/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/GoogleSheetsWriteBlockForm.test.tsx
index 20ff13d58..07c951ed2 100644
--- a/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/GoogleSheetsWriteBlockForm.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/GoogleSheetsWriteBlockForm.test.tsx
@@ -17,6 +17,7 @@ const mockNodes = new Map<
{ id: string; type: string; data?: Record<string, unknown> } | undefined
>();
const updateNodeData = vi.fn();
+const capture = vi.fn();
vi.mock("@xyflow/react", async () => {
const actual =
@@ -102,6 +103,8 @@ vi.mock("@/routes/workflows/components/SpreadsheetCombobox", () => ({
displayName: string | null;
placeholder?: string;
allowCreate: boolean;
+ templateMode: boolean;
+ onTemplateModeChange: (enabled: boolean) => void;
onChange: (value: string) => void;
onSelect: (selection: {
url: string;
@@ -115,6 +118,7 @@ vi.mock("@/routes/workflows/components/SpreadsheetCombobox", () => ({
data-display-name={props.displayName ?? ""}
data-has-selected-account={String(props.hasSelectedAccount)}
data-allow-create={String(props.allowCreate)}
+ data-template-mode={String(props.templateMode)}
>
<input
data-testid="spreadsheet-input"
@@ -131,6 +135,10 @@ vi.mock("@/routes/workflows/components/SpreadsheetCombobox", () => ({
})
}
/>
+ <button
+ data-testid="spreadsheet-template-mode-on"
+ onClick={() => props.onTemplateModeChange(true)}
+ />
</div>
),
}));
@@ -144,10 +152,16 @@ vi.mock("@/routes/workflows/components/SheetTabCombobox", () => ({
value: string;
placeholder?: string;
allowCreate: boolean;
+ templateMode: boolean;
+ onTemplateModeChange: (enabled: boolean) => void;
onChange: (value: string) => void;
onSelect: (tabName: string) => void;
}) => (
- <div data-testid="sheet-tab-combobox" data-value={props.value}>
+ <div
+ data-testid="sheet-tab-combobox"
+ data-value={props.value}
+ data-template-mode={String(props.templateMode)}
+ >
<input
data-testid="sheet-tab-input"
value={props.value}
@@ -157,6 +171,10 @@ vi.mock("@/routes/workflows/components/SheetTabCombobox", () => ({
data-testid="sheet-tab-select"
onClick={() => props.onSelect("Tab2")}
/>
+ <button
+ data-testid="sheet-template-mode-on"
+ onClick={() => props.onTemplateModeChange(true)}
+ />
</div>
),
}));
@@ -203,6 +221,14 @@ vi.mock("@/hooks/useGoogleSheetDimensions", () => ({
useGoogleSheetDimensions: () => ({ data: null, isLoading: false }),
}));
+vi.mock("@/hooks/useCurrentOrgId", () => ({
+ useCurrentOrgId: () => "org_test",
+}));
+
+vi.mock("posthog-js/react", () => ({
+ usePostHog: () => ({ capture }),
+}));
+
vi.mock("@/components/ui/checkbox", () => ({
Checkbox: (props: {
checked: boolean;
@@ -241,6 +267,7 @@ beforeEach(() => {
vi.useFakeTimers();
mockNodes.clear();
updateNodeData.mockReset();
+ capture.mockReset();
usePendingCommitsStore.setState({ commits: {} });
useSidebarSaveStateStore.getState().reset();
});
@@ -355,6 +382,169 @@ describe("GoogleSheetsWriteBlockForm (SKY-9361)", () => {
});
});
+ test("switching blocks clears the selected spreadsheet display name", () => {
+ setGoogleSheetsWriteNode("g1");
+ setGoogleSheetsWriteNode("g2", {
+ spreadsheetUrl: "https://docs.google.com/spreadsheets/d/xyz456/edit",
+ });
+ const { rerender } = render(<GoogleSheetsWriteBlockForm blockId="g1" />);
+
+ fireEvent.click(screen.getByTestId("spreadsheet-select"));
+ expect(screen.getByTestId("spreadsheet-combobox").dataset.displayName).toBe(
+ "My Sheet",
+ );
+
+ rerender(<GoogleSheetsWriteBlockForm blockId="g2" />);
+
+ expect(screen.getByTestId("spreadsheet-combobox").dataset.displayName).toBe(
+ "",
+ );
+ expect(screen.getByTestId("spreadsheet-combobox").dataset.value).toBe(
+ "https://docs.google.com/spreadsheets/d/xyz456/edit",
+ );
+ });
+
+ test("toggles the spreadsheet field and captures telemetry", () => {
+ setGoogleSheetsWriteNode("g1");
+ render(<GoogleSheetsWriteBlockForm blockId="g1" />);
+
+ fireEvent.click(
+ screen.getAllByRole("button", { name: "Enter a custom value" })[0]!,
+ );
+
+ expect(
+ screen
+ .getByRole("button", { name: "Pick from your spreadsheets" })
+ .getAttribute("aria-pressed"),
+ ).toBe("true");
+ expect(
+ screen.getByTestId("spreadsheet-combobox").dataset.templateMode,
+ ).toBe("true");
+ expect(capture).toHaveBeenCalledWith(
+ "sheets.spreadsheet.picker.template_mode_toggled",
+ {
+ org_id: "org_test",
+ block_type: "google_sheets_write",
+ enabled: true,
+ },
+ );
+ });
+
+ test("clears a template spreadsheet value when returning to the picker", () => {
+ setGoogleSheetsWriteNode("g1", {
+ spreadsheetUrl: "{{ target_spreadsheet_url }}",
+ });
+ render(<GoogleSheetsWriteBlockForm blockId="g1" />);
+
+ fireEvent.click(
+ screen.getByRole("button", { name: "Pick from your spreadsheets" }),
+ );
+
+ expect(updateNodeData).toHaveBeenCalledWith("g1", {
+ spreadsheetUrl: "",
+ });
+ expect(capture).toHaveBeenCalledWith(
+ "sheets.spreadsheet.picker.template_mode_toggled",
+ {
+ org_id: "org_test",
+ block_type: "google_sheets_write",
+ enabled: false,
+ },
+ );
+ });
+
+ test("repeated true requests keep spreadsheet template mode on", () => {
+ setGoogleSheetsWriteNode("g1", {
+ spreadsheetUrl: "{{ target_spreadsheet_url }}",
+ });
+ render(<GoogleSheetsWriteBlockForm blockId="g1" />);
+
+ const requestTemplateMode = screen.getByTestId(
+ "spreadsheet-template-mode-on",
+ );
+ fireEvent.click(requestTemplateMode);
+ fireEvent.click(requestTemplateMode);
+
+ expect(
+ screen
+ .getByRole("button", { name: "Pick from your spreadsheets" })
+ .getAttribute("aria-pressed"),
+ ).toBe("true");
+ expect(updateNodeData).not.toHaveBeenCalled();
+ expect(capture).toHaveBeenLastCalledWith(
+ "sheets.spreadsheet.picker.template_mode_toggled",
+ {
+ org_id: "org_test",
+ block_type: "google_sheets_write",
+ enabled: true,
+ },
+ );
+ });
+
+ test("repeated true requests keep sheet template mode on", () => {
+ setGoogleSheetsWriteNode("g1", {
+ sheetName: "{{ target_sheet_name }}",
+ });
+ render(<GoogleSheetsWriteBlockForm blockId="g1" />);
+
+ const requestTemplateMode = screen.getByTestId("sheet-template-mode-on");
+ fireEvent.click(requestTemplateMode);
+ fireEvent.click(requestTemplateMode);
+
+ expect(
+ screen
+ .getByRole("button", { name: "Pick from your sheet tabs" })
+ .getAttribute("aria-pressed"),
+ ).toBe("true");
+ expect(updateNodeData).not.toHaveBeenCalled();
+ expect(capture).toHaveBeenLastCalledWith(
+ "sheets.tab.template_mode_toggled",
+ {
+ org_id: "org_test",
+ block_type: "google_sheets_write",
+ enabled: true,
+ },
+ );
+ });
+
+ test("switching blocks resets both local template modes", () => {
+ setGoogleSheetsWriteNode("g1");
+ setGoogleSheetsWriteNode("g2");
+ const { rerender } = render(<GoogleSheetsWriteBlockForm blockId="g1" />);
+
+ fireEvent.click(
+ screen.getAllByRole("button", { name: "Enter a custom value" })[0]!,
+ );
+ fireEvent.click(
+ screen.getByRole("button", { name: "Enter a custom value" }),
+ );
+
+ expect(
+ screen.getByRole("button", { name: "Pick from your spreadsheets" }),
+ ).toBeTruthy();
+ expect(
+ screen.getByRole("button", { name: "Pick from your sheet tabs" }),
+ ).toBeTruthy();
+
+ rerender(<GoogleSheetsWriteBlockForm blockId="g2" />);
+
+ expect(
+ screen.queryByRole("button", { name: "Pick from your spreadsheets" }),
+ ).toBeNull();
+ expect(
+ screen.queryByRole("button", { name: "Pick from your sheet tabs" }),
+ ).toBeNull();
+ expect(
+ screen.getAllByRole("button", { name: "Enter a custom value" }),
+ ).toHaveLength(2);
+ expect(
+ screen.getByTestId("spreadsheet-combobox").dataset.templateMode,
+ ).toBe("false");
+ expect(screen.getByTestId("sheet-tab-combobox").dataset.templateMode).toBe(
+ "false",
+ );
+ });
+
test("selecting a sheet propagates sheetName", () => {
setGoogleSheetsWriteNode("g1");
render(<GoogleSheetsWriteBlockForm blockId="g1" />);
diff --git a/skyvern/client/types/totp_type.py b/skyvern/client/types/totp_type.py
index 8d15e04ca..720e3af27 100644
--- a/skyvern/client/types/totp_type.py
+++ b/skyvern/client/types/totp_type.py
@@ -2,4 +2,4 @@
import typing
-TotpType = typing.Union[typing.Literal["authenticator", "email", "text", "none"], typing.Any]
+TotpType = typing.Union[typing.Literal["authenticator", "email", "text", "passkey", "none"], typing.Any]
diff --git a/skyvern/core/script_generations/script_skyvern_page.py b/skyvern/core/script_generations/script_skyvern_page.py
index 468b3af56..34f0bea84 100644
--- a/skyvern/core/script_generations/script_skyvern_page.py
+++ b/skyvern/core/script_generations/script_skyvern_page.py
@@ -178,7 +178,9 @@ class ScriptSkyvernPage(SkyvernPage):
browser_state = await cls._get_or_create_browser_state(browser_session_id=browser_session_id, url=url)
return await browser_state.scrape_website(
url="",
- cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(),
+ cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(
+ engine_selection=browser_state.engine_selection
+ ),
scrape_exclude=app.scrape_exclude,
max_screenshot_number=settings.MAX_NUM_SCREENSHOTS,
# DEPRECATED: visual bounding box overlays are no longer rendered during scraping.
@@ -695,7 +697,10 @@ class ScriptSkyvernPage(SkyvernPage):
if not working_page:
return
- skyvern_frame = await SkyvernFrame.create_instance(frame=working_page)
+ skyvern_frame = await SkyvernFrame.create_instance(
+ frame=working_page,
+ engine_selection=browser_state.engine_selection,
+ )
html = await skyvern_frame.get_content()
if html:
@@ -790,7 +795,10 @@ class ScriptSkyvernPage(SkyvernPage):
if not self.page:
return
- skyvern_frame = await SkyvernFrame.create_instance(frame=self.page)
+ skyvern_frame = await SkyvernFrame.create_instance(
+ frame=self.page,
+ engine_selection=self.engine_selection,
+ )
await skyvern_frame.wait_for_page_ready(
network_idle_timeout_ms=settings.PAGE_READY_NETWORK_IDLE_TIMEOUT_MS,
loading_indicator_timeout_ms=settings.PAGE_READY_LOADING_INDICATOR_TIMEOUT_MS,
@@ -821,7 +829,10 @@ class ScriptSkyvernPage(SkyvernPage):
# Inject domUtils.js and build the element tree to set unique_id attrs.
# Use a short timeout since this is best-effort; we don't want to hang for 60s.
- skyvern_frame = await SkyvernFrame.create_instance(frame=self.page)
+ skyvern_frame = await SkyvernFrame.create_instance(
+ frame=self.page,
+ engine_selection=self.engine_selection,
+ )
await skyvern_frame.build_tree_from_body(
frame_name="main.frame",
frame_index=0,
diff --git a/skyvern/core/script_generations/skyvern_page.py b/skyvern/core/script_generations/skyvern_page.py
index 196cc1df0..cdca3f073 100644
--- a/skyvern/core/script_generations/skyvern_page.py
+++ b/skyvern/core/script_generations/skyvern_page.py
@@ -2360,7 +2360,10 @@ class SkyvernPage(Page):
try:
from skyvern.webeye.utils.page import SkyvernFrame
- skyvern_frame = await SkyvernFrame.create_instance(frame=self.page)
+ skyvern_frame = await SkyvernFrame.create_instance(
+ frame=self.page,
+ engine_selection=self.engine_selection,
+ )
await skyvern_frame.wait_for_page_ready(
network_idle_timeout_ms=3000,
loading_indicator_timeout_ms=5000,
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index e329d909c..dfbd841e9 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -191,6 +191,7 @@ from skyvern.webeye.actions.parse_actions import (
parse_ui_tars_actions,
)
from skyvern.webeye.actions.responses import ActionResult, ActionSuccess
+from skyvern.webeye.browser_engine import BrowserEngineSelection
from skyvern.webeye.browser_state import BrowserState
from skyvern.webeye.cdp_download_interceptor import (
download_filename_from_suffix,
@@ -3440,7 +3441,10 @@ class ForgeAgent:
skyvern_frame: SkyvernFrame | None = None
try:
- skyvern_frame = await SkyvernFrame.create_instance(frame=working_page)
+ skyvern_frame = await SkyvernFrame.create_instance(
+ frame=working_page,
+ engine_selection=browser_state.engine_selection,
+ )
await skyvern_frame.safe_wait_for_animation_end(caller="post_action_artifact")
except Exception:
LOG.info("Failed to wait for animation end, ignore it", exc_info=True)
@@ -3533,7 +3537,10 @@ class ForgeAgent:
try:
if skyvern_frame is None:
- skyvern_frame = await SkyvernFrame.create_instance(frame=working_page)
+ skyvern_frame = await SkyvernFrame.create_instance(
+ frame=working_page,
+ engine_selection=browser_state.engine_selection,
+ )
html = await skyvern_frame.get_content()
_ctx = skyvern_context.current()
# Encode once to fix the html_bytes char-vs-byte mismatch and avoid a
@@ -3680,7 +3687,11 @@ class ForgeAgent:
return await browser_state.scrape_website(
url=task.url,
- cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(task=task, step=step),
+ cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(
+ task=task,
+ step=step,
+ engine_selection=browser_state.engine_selection,
+ ),
scrape_exclude=app.scrape_exclude,
max_screenshot_number=max_screenshot_number,
draw_boxes=draw_boxes,
@@ -3983,6 +3994,7 @@ class ForgeAgent:
slim_output: str | None = None,
has_data_extraction_goal: bool = False,
enable_new_planner_actions: bool = False,
+ extra_action_guidance: str | None = None,
) -> str:
"""
Build a short-but-unique cache variant identifier so extract-action prompts that
@@ -3992,6 +4004,9 @@ class ForgeAgent:
variant_parts: list[str] = []
if verification_code_check:
variant_parts.append("vc")
+ if extra_action_guidance:
+ digest = hashlib.sha256(extra_action_guidance.encode("utf-8"), usedforsecurity=False).hexdigest()[:8]
+ variant_parts.append(f"g{digest}")
if show_close_page_action:
variant_parts.append("cp")
if show_new_tab_action:
@@ -4332,6 +4347,7 @@ class ForgeAgent:
task.workflow_run_id if task.workflow_run_id else task.task_id,
properties={"task_url": task.url, "organization_id": task.organization_id},
)
+ extra_action_guidance = await app.AGENT_FUNCTION.get_extra_extract_action_guidance(task)
# Format-then-clear so a render failure can't drop the signal permanently;
# gate on extract-action template since other task types don't render it.
@@ -4357,6 +4373,7 @@ class ForgeAgent:
"error_code_mapping_str": error_code_mapping_str,
"local_datetime": local_datetime,
"verification_code_check": verification_code_check,
+ "extra_action_guidance": extra_action_guidance,
"complete_criterion": task.complete_criterion.strip() if task.complete_criterion else None,
"terminate_criterion": task.terminate_criterion.strip() if task.terminate_criterion else None,
"show_close_page_action": show_close_page_action,
@@ -4370,6 +4387,7 @@ class ForgeAgent:
}
cache_variant = self._build_extract_action_cache_variant(
verification_code_check=verification_code_check,
+ extra_action_guidance=extra_action_guidance,
show_close_page_action=show_close_page_action,
show_new_tab_action=show_new_tab_action,
show_switch_tab_action=show_switch_tab_action,
@@ -4482,6 +4500,7 @@ class ForgeAgent:
error_code_mapping_str=error_code_mapping_str,
local_datetime=local_datetime,
verification_code_check=verification_code_check,
+ extra_action_guidance=extra_action_guidance,
complete_criterion=task.complete_criterion.strip() if task.complete_criterion else None,
terminate_criterion=task.terminate_criterion.strip() if task.terminate_criterion else None,
show_close_page_action=show_close_page_action,
@@ -5742,6 +5761,7 @@ class ForgeAgent:
task=task,
step=step,
page=page,
+ engine_selection=browser_state.engine_selection,
)
failure_reason = f"Reached the maximum steps ({max_steps_per_run}). Possible failure reasons: {generated_failure_reason.reasoning}"
errors = [ReachMaxStepsError().model_dump()] + [
@@ -5845,6 +5865,7 @@ class ForgeAgent:
step=step,
page=page,
max_retries=max_retries_per_step,
+ engine_selection=browser_state.engine_selection if browser_state is not None else None,
)
# Only pass new errors — update_task() appends to existing errors in the DB
@@ -5924,6 +5945,7 @@ class ForgeAgent:
task: Task,
step: Step,
page: Page | None,
+ engine_selection: BrowserEngineSelection | None,
) -> MaxStepsReasonResponse:
steps_results = []
llm_errors: list[str] = []
@@ -5991,7 +6013,12 @@ class ForgeAgent:
screenshots: list[bytes] = []
if page is not None:
- screenshots = await SkyvernFrame.take_split_screenshots(page=page, url=page.url, scroll=scroll)
+ screenshots = await SkyvernFrame.take_split_screenshots(
+ page=page,
+ url=page.url,
+ scroll=scroll,
+ engine_selection=engine_selection,
+ )
prompt = prompt_engine.load_prompt(
"summarize-max-steps-reason",
@@ -6045,6 +6072,7 @@ class ForgeAgent:
step: Step,
page: Page | None,
max_retries: int,
+ engine_selection: BrowserEngineSelection | None,
) -> MaxStepsReasonResponse:
html = ""
screenshots: list[bytes] = []
@@ -6134,11 +6162,16 @@ class ForgeAgent:
)
if page is not None:
- skyvern_frame = await SkyvernFrame.create_instance(frame=page)
+ skyvern_frame = await SkyvernFrame.create_instance(frame=page, engine_selection=engine_selection)
html = truncate_page_html_for_summary(await skyvern_frame.get_content())
# scroll=False: one current-viewport shot (the failing region) is enough for a
# failure summary; avoids 10 full-page strips. SKY-10626.
- screenshots = await SkyvernFrame.take_split_screenshots(page=page, url=page.url, scroll=False)
+ screenshots = await SkyvernFrame.take_split_screenshots(
+ page=page,
+ url=page.url,
+ scroll=False,
+ engine_selection=engine_selection,
+ )
prompt = prompt_engine.load_prompt(
"summarize-max-retries-reason",
@@ -6456,6 +6489,7 @@ class ForgeAgent:
task=task,
step=step,
page=page,
+ engine_selection=browser_state.engine_selection if browser_state is not None else None,
)
failure_reason = f"Reached the maximum steps ({max_steps_per_run}). Possible failure reasons: {generated_failure_reason.reasoning}"
errors = [ReachMaxStepsError().model_dump()] + [
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py
index ede95b13f..6b9328310 100644
--- a/skyvern/forge/agent_functions.py
+++ b/skyvern/forge/agent_functions.py
@@ -6,7 +6,7 @@ import hashlib
import os
import time
from collections.abc import AsyncIterator
-from contextlib import AbstractAsyncContextManager, asynccontextmanager
+from contextlib import AbstractAsyncContextManager, asynccontextmanager, nullcontext
from dataclasses import dataclass, field
from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING, Any, Literal, TypedDict
@@ -39,6 +39,16 @@ from skyvern.forge.sdk.copilot.config import CopilotConfig, block_authoring_poli
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.db.agent_db import AgentDB
from skyvern.forge.sdk.models import Step, StepStatus
+from skyvern.forge.sdk.schemas.credentials import (
+ CreateCredentialRequest,
+ Credential,
+ CredentialItem,
+ CredentialType,
+ CredentialVaultType,
+ NonEmptyCreditCardCredential,
+ NonEmptyPasswordCredential,
+ SecretCredential,
+)
from skyvern.forge.sdk.schemas.organizations import Organization
from skyvern.forge.sdk.schemas.tasks import Task, TaskStatus
from skyvern.forge.sdk.services import (
@@ -56,7 +66,7 @@ from skyvern.schemas.workflows import BlockResult, FileStorageType, FileUploadDe
from skyvern.services.otp_gmail import GmailOTPVerificationContext
from skyvern.utils.url_validators import pinned_ip_client
from skyvern.webeye.actions.actions import Action
-from skyvern.webeye.browser_engine import BrowserEngineSelection, resolve_engine_selection_for_task
+from skyvern.webeye.browser_engine import UNSET_SELECTION, BrowserEngineSelection, resolve_engine_selection_for_task
from skyvern.webeye.browser_state import BrowserState
from skyvern.webeye.scraper.scraped_page import ELEMENT_NODE_ATTRIBUTES, CleanupElementTreeFunc, json_to_html
from skyvern.webeye.utils.dom import SkyvernElement
@@ -66,6 +76,7 @@ if TYPE_CHECKING:
from playwright.async_api import BrowserContext
from skyvern.forge.sdk.db.enums import WorkflowRunTriggerType
+ from skyvern.forge.sdk.services.credential.credential_vault_service import CredentialVaultService
from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext
from skyvern.forge.sdk.workflow.models.code_block_recorder import RecordingPage
from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun, WorkflowRunStatus
@@ -479,7 +490,7 @@ async def _check_svg_eligibility(
locator=locater,
frame=skyvern_frame.get_frame(),
static_element=element,
- engine_selection=_resolve_engine_selection(task),
+ engine_selection=skyvern_frame.engine_selection,
)
_, blocked = await skyvern_frame.get_blocking_element_id(
@@ -665,7 +676,7 @@ async def _convert_css_shape_to_string(
locator=locater,
frame=skyvern_frame.get_frame(),
static_element=element,
- engine_selection=_resolve_engine_selection(task),
+ engine_selection=skyvern_frame.engine_selection,
)
_, blocked = await skyvern_frame.get_blocking_element_id(await skyvern_element.get_element_handler())
@@ -692,7 +703,11 @@ async def _convert_css_shape_to_string(
LOG.debug("call LLM to convert css shape to string shape", element_id=element_id)
# A capture failure (FailedToTakeScreenshot) falls through to the outer handler below,
# which drops the element from future scrape passes and aborts the conversion.
- screenshot = await take_element_screenshot(locater, timeout=settings.BROWSER_ACTION_TIMEOUT_MS)
+ screenshot = await take_element_screenshot(
+ locater,
+ timeout=settings.BROWSER_ACTION_TIMEOUT_MS,
+ engine_selection=skyvern_frame.engine_selection,
+ )
prompt = prompt_engine.load_prompt("css-shape-convert")
# TODO: we don't retry the css shape conversion today
@@ -1161,6 +1176,13 @@ class AgentFunction:
async def setup_browser_context_extensions(self, browser_context: Any, **kwargs: Any) -> None:
"""Attach cloud-only listeners/route handlers to a fresh BrowserContext. OSS no-op."""
+ async def on_browser_context_acquired(
+ self,
+ browser_context: Any,
+ workflow_run_id: str | None,
+ ) -> None:
+ return
+
async def validate_step_execution(
self,
task: Task,
@@ -1216,6 +1238,62 @@ class AgentFunction:
) -> str | None:
return None
+ async def validate_credential_write(
+ self,
+ *,
+ organization_id: str,
+ credential_type: CredentialType,
+ credential: NonEmptyPasswordCredential | NonEmptyCreditCardCredential | SecretCredential,
+ existing_credential: Credential | None = None,
+ ) -> None:
+ return
+
+ async def prepare_credential_update(
+ self,
+ *,
+ credential_service: CredentialVaultService,
+ existing_credential: Credential,
+ data: CreateCredentialRequest,
+ ) -> CreateCredentialRequest:
+ return data
+
+ async def should_lock_credential_write(
+ self,
+ *,
+ credential: NonEmptyPasswordCredential | NonEmptyCreditCardCredential | SecretCredential | None,
+ existing_credential: Credential,
+ ) -> bool:
+ return False
+
+ def credential_write_lock(
+ self,
+ *,
+ organization_id: str,
+ credential_id: str,
+ ) -> AbstractAsyncContextManager[None]:
+ return nullcontext()
+
+ async def on_credential_item_orphaned(
+ self,
+ *,
+ organization_id: str,
+ item_id: str,
+ vault_type: CredentialVaultType,
+ ) -> None:
+ return
+
+ async def process_registered_credential_item(
+ self,
+ *,
+ workflow_run_id: str | None,
+ db_credential: Credential,
+ credential_item: CredentialItem,
+ ) -> CredentialItem:
+ return credential_item
+
+ async def get_extra_extract_action_guidance(self, task: Task) -> str | None:
+ return None
+
async def parse_enterprise_totp_secret_result(
self,
totp_secret: str,
@@ -1789,8 +1867,11 @@ class AgentFunction:
self,
task: Task | None = None,
step: Step | None = None,
+ engine_selection: BrowserEngineSelection | None = UNSET_SELECTION,
) -> CleanupElementTreeFunc:
MAX_ELEMENT_CNT = settings.SVG_MAX_PARSING_ELEMENT_CNT
+ if engine_selection is UNSET_SELECTION:
+ engine_selection = _resolve_engine_selection(task)
@traced(name="skyvern.agent.cleanup_element_tree")
async def cleanup_element_tree_func(frame: Page | Frame, url: str, element_tree: list[dict]) -> list[dict]:
@@ -1804,7 +1885,7 @@ class AgentFunction:
"""
context = skyvern_context.ensure_context()
# page won't be in the context.frame_index_map, so the index is going to be 0
- skyvern_frame = await SkyvernFrame.create_instance(frame=frame)
+ skyvern_frame = await SkyvernFrame.create_instance(frame=frame, engine_selection=engine_selection)
current_frame_index = context.frame_index_map.get(frame, 0)
queue = []
@@ -1829,7 +1910,10 @@ class AgentFunction:
new_frame = next(
(k for k, v in context.frame_index_map.items() if v == queue_ele.get("frame_index")), frame
)
- skyvern_frame = await SkyvernFrame.create_instance(frame=new_frame)
+ skyvern_frame = await SkyvernFrame.create_instance(
+ frame=new_frame,
+ engine_selection=engine_selection,
+ )
current_frame_index = queue_ele.get("frame_index", 0)
_remove_rect(queue_ele)
@@ -2247,3 +2331,13 @@ class AgentFunction:
) -> None:
"""Fired after a workflow run reaches a final status. Overrides must be best-effort and never raise."""
return None
+
+ async def on_workflow_run_terminal(
+ self,
+ *,
+ workflow_run_id: str,
+ organization_id: str,
+ status: WorkflowRunStatus,
+ ) -> None:
+ """Fired after a workflow run reaches a final status. Overrides must be best-effort and never raise."""
+ return None
diff --git a/skyvern/forge/api_app.py b/skyvern/forge/api_app.py
index d4cecb404..f7770811a 100644
--- a/skyvern/forge/api_app.py
+++ b/skyvern/forge/api_app.py
@@ -17,6 +17,8 @@ from skyvern.exceptions import require_server_extra_modules
require_server_extra_modules("skyvern.forge.api_app", ("fastapi", "starlette", "starlette_context"))
from fastapi import FastAPI, Response, status
+from fastapi.exception_handlers import request_validation_exception_handler
+from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import JSONResponse
@@ -125,6 +127,17 @@ def format_validation_errors(exc: ValidationError) -> str:
)
+def sanitize_request_validation_errors(exc: RequestValidationError) -> list[dict[str, Any]]:
+ return [
+ {
+ "loc": error["loc"],
+ "msg": error["msg"],
+ "type": error["type"],
+ }
+ for error in exc.errors()
+ ]
+
+
class ExecutionDatePlugin(Plugin):
key = "execution_date"
@@ -450,6 +463,20 @@ def create_api_app() -> FastAPI:
content={"detail": detail},
)
+ @fastapi_app.exception_handler(RequestValidationError)
+ async def handle_request_validation_error(request: Request, exc: RequestValidationError) -> Response:
+ # Only credential routes carry passkey/secret material worth stripping from 422 detail; every
+ # other route keeps FastAPI's default input/ctx to preserve debuggable validation errors.
+ path = request.url.path.rstrip("/")
+ credential_prefixes = ("/v1/credentials", "/api/v1/credentials")
+ if not any(path == prefix or path.startswith(f"{prefix}/") for prefix in credential_prefixes):
+ return await request_validation_exception_handler(request, exc)
+
+ return JSONResponse(
+ status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
+ content={"detail": sanitize_request_validation_errors(exc)},
+ )
+
@fastapi_app.exception_handler(ClientDisconnect)
async def handle_client_disconnect(request: Request, exc: ClientDisconnect) -> Response:
# Client closed the connection mid-request (e.g. while a route reads the body).
diff --git a/skyvern/forge/prompts/skyvern/extract-action-static.j2 b/skyvern/forge/prompts/skyvern/extract-action-static.j2
index b3c397251..310d23c55 100644
--- a/skyvern/forge/prompts/skyvern/extract-action-static.j2
+++ b/skyvern/forge/prompts/skyvern/extract-action-static.j2
@@ -64,6 +64,7 @@ Reply in JSON format with the following keys:
"should_enter_verification_code": bool, // Whether the user should proceed to enter the verification code.
"should_verify_by_magic_link": bool // Whether the page instructs the user to check their email for a magic link to verify the login.{% endif %}
}
-
+{% if extra_action_guidance %}{{ extra_action_guidance }}
+{% endif %}
Consider the action history from the last step{% if llm_screenshots_enabled %} and the screenshot{% endif %} together, if actions from the last step don't yield positive impact, try other actions or other action combinations.
Action history from previous steps: (note: even if the action history suggests goal is achieved, check{% if llm_screenshots_enabled %} the screenshot and{% endif %} the DOM elements to make sure the goal is achieved)
diff --git a/skyvern/forge/prompts/skyvern/extract-action.j2 b/skyvern/forge/prompts/skyvern/extract-action.j2
index e907880c6..b299aefb0 100644
--- a/skyvern/forge/prompts/skyvern/extract-action.j2
+++ b/skyvern/forge/prompts/skyvern/extract-action.j2
@@ -64,7 +64,8 @@ Reply in JSON format with the following keys:
"should_enter_verification_code": bool, // Whether the user should proceed to enter the verification code.
"should_verify_by_magic_link": bool // Whether the page instructs the user to check their email for a magic link to verify the login.{% endif %}
}
-
+{% if extra_action_guidance %}{{ extra_action_guidance }}
+{% endif %}
Consider the action history from the last step{% if llm_screenshots_enabled %} and the screenshot{% endif %} together, if actions from the last step don't yield positive impact, try other actions or other action combinations.
Action history from previous steps: (note: even if the action history suggests goal is achieved, check{% if llm_screenshots_enabled %} the screenshot and{% endif %} the DOM elements to make sure the goal is achieved)
{% if recent_dialog_messages_str %}Browser alert messages raised during the previous step (the agent auto-accepted each one). Use these as additional context: if a message indicates a validation error or that the previous action was rejected, change the value, its format, or the target element before retrying — do NOT retry the same INPUT_TEXT with the same text on the same field. Treat purely informational messages (e.g. "Saved successfully") as confirmation, not as a signal to change anything.
diff --git a/skyvern/forge/request_logging.py b/skyvern/forge/request_logging.py
index 1b36a322d..9cc104cab 100644
--- a/skyvern/forge/request_logging.py
+++ b/skyvern/forge/request_logging.py
@@ -33,6 +33,7 @@ _SENSITIVE_ENDPOINTS = {
"POST /v1/google/oauth/callback",
"POST /api/v1/google/oauth/callback",
}
+_SENSITIVE_ENDPOINT_PATTERNS = (re.compile(r"^(?:POST|PUT) /(?:api/)?v1/credentials(?:/.*)?$"),)
_MAX_BODY_LENGTH = 1000
_READ_METHODS = frozenset({"GET", "HEAD", "OPTIONS"})
_MAX_RESPONSE_READ_BYTES = 1024 * 1024 # 1 MB — skip logging bodies larger than this
@@ -92,8 +93,11 @@ def _client_ip_from_headers(headers: typing.Mapping[str, str]) -> str | None:
def _is_sensitive_endpoint(request: Request) -> bool:
- return f"{request.method.upper()} {request.url.path.rstrip('/')}" in _SENSITIVE_ENDPOINTS or (
- request.method.upper() == "POST" and _ACTION_LOG_ENDPOINT_RE.fullmatch(request.url.path) is not None
+ endpoint = f"{request.method.upper()} {request.url.path.rstrip('/')}"
+ return (
+ endpoint in _SENSITIVE_ENDPOINTS
+ or any(pattern.fullmatch(endpoint) for pattern in _SENSITIVE_ENDPOINT_PATTERNS)
+ or (request.method.upper() == "POST" and _ACTION_LOG_ENDPOINT_RE.fullmatch(request.url.path) is not None)
)
@@ -114,15 +118,16 @@ def _sanitize_body(request: Request, body: bytes, content_type: str | None) -> s
return text
-def _is_sensitive_key(key: str) -> bool:
- return key.lower() in _SENSITIVE_FIELDS
+def _is_sensitive_key(key: str, *, redact_metadata: bool = False) -> bool:
+ normalized = key.lower()
+ return normalized in _SENSITIVE_FIELDS or (redact_metadata and normalized == "metadata")
def _strip_artifact_url_query(value: str) -> str:
return _ARTIFACT_CONTENT_URL_QUERY_RE.sub(r"\1", value)
-def redact_sensitive_fields(obj: typing.Any, _depth: int = 0) -> typing.Any:
+def redact_sensitive_fields(obj: typing.Any, _depth: int = 0, *, redact_metadata: bool = False) -> typing.Any:
"""Redact sensitive fields and strip capability queries from artifact URLs.
Field names use exact-match (case-insensitive) rather than substring/regex
@@ -130,18 +135,35 @@ def redact_sensitive_fields(obj: typing.Any, _depth: int = 0) -> typing.Any:
``page_token`` which contain sensitive substrings but are not secrets.
"""
if isinstance(obj, str):
- return _strip_artifact_url_query(obj)
+ sanitized_value = _strip_artifact_url_query(obj)
+ if _depth > 20:
+ return sanitized_value
+ try:
+ parsed_value = json.loads(sanitized_value)
+ except (json.JSONDecodeError, TypeError):
+ return sanitized_value
+ if isinstance(parsed_value, (dict, list)):
+ redacted_value = redact_sensitive_fields(parsed_value, _depth + 1, redact_metadata=redact_metadata)
+ return json.dumps(redacted_value) if redacted_value != parsed_value else sanitized_value
+ return sanitized_value
if _depth > 20:
# Stop recursing but still redact sensitive keys at this level
if isinstance(obj, dict):
- return {k: _REDACTED if _is_sensitive_key(k) else v for k, v in obj.items()}
+ return {
+ k: _REDACTED if _is_sensitive_key(k, redact_metadata=redact_metadata) else v for k, v in obj.items()
+ }
return obj
if isinstance(obj, dict):
return {
- k: _REDACTED if _is_sensitive_key(k) else redact_sensitive_fields(v, _depth + 1) for k, v in obj.items()
+ k: (
+ _REDACTED
+ if _is_sensitive_key(k, redact_metadata=redact_metadata)
+ else redact_sensitive_fields(v, _depth + 1, redact_metadata=redact_metadata)
+ )
+ for k, v in obj.items()
}
if isinstance(obj, list):
- return [redact_sensitive_fields(item, _depth + 1) for item in obj]
+ return [redact_sensitive_fields(item, _depth + 1, redact_metadata=redact_metadata) for item in obj]
return obj
diff --git a/skyvern/forge/sdk/api/custom_credential_client.py b/skyvern/forge/sdk/api/custom_credential_client.py
index 87048a59b..16d1df165 100644
--- a/skyvern/forge/sdk/api/custom_credential_client.py
+++ b/skyvern/forge/sdk/api/custom_credential_client.py
@@ -1,8 +1,10 @@
from typing import Any
import structlog
+from pydantic import ValidationError
from skyvern.exceptions import HttpException
+from skyvern.forge.request_logging import redact_sensitive_fields
from skyvern.forge.sdk.core.aiohttp_helper import aiohttp_request
from skyvern.forge.sdk.schemas.credentials import (
CredentialItem,
@@ -11,6 +13,7 @@ from skyvern.forge.sdk.schemas.credentials import (
PasswordCredential,
SecretCredential,
)
+from skyvern.forge.sdk.services.credentials import safe_error_message
LOG = structlog.get_logger()
@@ -41,15 +44,18 @@ class CustomCredentialAPIClient:
) -> dict[str, Any]:
"""Convert Skyvern credential to API payload format."""
if isinstance(credential, PasswordCredential):
- return {
+ payload: dict[str, Any] = {
"type": "password",
"username": credential.username,
"password": credential.password,
"totp": credential.totp,
"totp_type": credential.totp_type,
}
+ if credential.metadata:
+ payload["metadata"] = credential.metadata
+ return payload
elif isinstance(credential, CreditCardCredential):
- payload: dict[str, Any] = {
+ payload = {
"type": "credit_card",
"card_holder_name": credential.card_holder_name,
"card_number": credential.card_number,
@@ -83,13 +89,14 @@ class CustomCredentialAPIClient:
@staticmethod
def _format_error_response(response_body: Any) -> str:
"""Extract a human-readable error message from an HTTP error response body."""
- if isinstance(response_body, dict):
+ redacted_body = redact_sensitive_fields(response_body, redact_metadata=True)
+ if isinstance(redacted_body, dict):
for key in ["message", "error", "detail", "error_message"]:
- if key in response_body and response_body[key]:
- return str(response_body[key])
- return str(response_body)
- if isinstance(response_body, str) and response_body.strip():
- return response_body.strip()
+ if key in redacted_body and redacted_body[key]:
+ return str(redacted_body[key])
+ return str(redacted_body)
+ if isinstance(redacted_body, str) and redacted_body.strip():
+ return redacted_body.strip()
return ""
def _api_response_to_credential(self, credential_data: dict[str, Any], name: str, item_id: str) -> CredentialItem:
@@ -107,6 +114,7 @@ class CustomCredentialAPIClient:
password=credential_data["password"],
totp=credential_data.get("totp"),
totp_type=credential_data.get("totp_type", "none"),
+ metadata=credential_data.get("metadata"),
)
return CredentialItem(
item_id=item_id,
@@ -162,7 +170,7 @@ class CustomCredentialAPIClient:
credential_type=CredentialType.SECRET,
)
else:
- raise ValueError(f"Unsupported credential type from API: {credential_type}")
+ raise ValueError("Unsupported credential type from API")
async def create_credential(
self, name: str, credential: PasswordCredential | CreditCardCredential | SecretCredential
@@ -212,7 +220,7 @@ class CustomCredentialAPIClient:
"Custom credential API returned error",
url=url,
status_code=status_code,
- response_body=response_body,
+ response_body=redact_sensitive_fields(response_body, redact_metadata=True),
)
raise HttpException(status_code, url, msg)
@@ -225,7 +233,7 @@ class CustomCredentialAPIClient:
LOG.error(
"Custom credential API response missing id field",
url=url,
- response=response_body,
+ response_body=redact_sensitive_fields(response_body, redact_metadata=True),
)
raise HttpException(500, url, "Invalid response format from custom credential API: missing 'id' field")
@@ -233,7 +241,6 @@ class CustomCredentialAPIClient:
"Successfully created credential via custom API",
url=url,
name=name,
- credential_id=credential_id,
)
return str(credential_id)
@@ -241,14 +248,15 @@ class CustomCredentialAPIClient:
except HttpException:
raise
except Exception as e:
+ safe_error = safe_error_message(e)
LOG.error(
"Failed to create credential via custom API",
url=url,
name=name,
- error=str(e),
- exc_info=True,
+ error=safe_error,
+ exc_info=not isinstance(e, ValidationError),
)
- raise HttpException(500, url, f"Failed to create credential via custom API: {e!s}") from e
+ raise HttpException(500, url, f"Failed to create credential via custom API: {safe_error}") from e
async def get_credential(self, credential_id: str, name: str) -> CredentialItem:
"""
@@ -289,7 +297,7 @@ class CustomCredentialAPIClient:
"Custom credential API returned error",
url=url,
status_code=status_code,
- response_body=response_body,
+ response_body=redact_sensitive_fields(response_body, redact_metadata=True),
)
raise HttpException(status_code, url, msg)
@@ -307,14 +315,15 @@ class CustomCredentialAPIClient:
except HttpException:
raise
except Exception as e:
+ safe_error = safe_error_message(e)
LOG.error(
"Failed to retrieve credential via custom API",
url=url,
credential_id=credential_id,
- error=str(e),
- exc_info=True,
+ error=safe_error,
+ exc_info=not isinstance(e, ValidationError),
)
- raise HttpException(500, url, f"Failed to retrieve credential via custom API: {e!s}") from e
+ raise HttpException(500, url, f"Failed to retrieve credential via custom API: {safe_error}") from e
async def delete_credential(self, credential_id: str) -> None:
"""
@@ -351,7 +360,7 @@ class CustomCredentialAPIClient:
"Custom credential API returned error on delete",
url=url,
status_code=status_code,
- response_body=response_body,
+ response_body=redact_sensitive_fields(response_body, redact_metadata=True),
)
raise HttpException(status_code, url, msg)
@@ -364,11 +373,12 @@ class CustomCredentialAPIClient:
except HttpException:
raise
except Exception as e:
+ safe_error = safe_error_message(e)
LOG.error(
"Failed to delete credential via custom API",
url=url,
credential_id=credential_id,
- error=str(e),
- exc_info=True,
+ error=safe_error,
+ exc_info=not isinstance(e, ValidationError),
)
- raise HttpException(500, url, f"Failed to delete credential via custom API: {e!s}") from e
+ raise HttpException(500, url, f"Failed to delete credential via custom API: {safe_error}") from e
diff --git a/skyvern/forge/sdk/api/real_azure.py b/skyvern/forge/sdk/api/real_azure.py
index ee93fcb2b..2506e8e53 100644
--- a/skyvern/forge/sdk/api/real_azure.py
+++ b/skyvern/forge/sdk/api/real_azure.py
@@ -56,7 +56,36 @@ class RealAsyncAzureVaultClient(AsyncAzureVaultClient):
async def create_or_update_secret(self, secret_name: str, secret_value: str, vault_name: str) -> str:
secret_client = await self._get_secret_client(vault_name)
try:
+ previous_versions: list[str] = []
+ try:
+ async for properties in secret_client.list_properties_of_secret_versions(secret_name):
+ if properties.enabled is not False and properties.version is not None:
+ previous_versions.append(properties.version)
+ except ResourceNotFoundError:
+ pass
+ except Exception:
+ # An identity permitted to set but not list secret versions raises an authorization
+ # error here. Disabling prior versions is best-effort, so log and continue rather than
+ # failing every write (and, via delete_credential, orphaning material after the row is gone).
+ LOG.warning(
+ "Could not enumerate prior Azure secret versions to disable; proceeding with the write",
+ secret_name=secret_name,
+ exc_info=True,
+ )
+
secret = await secret_client.set_secret(secret_name, secret_value)
+ # Azure Key Vault leaves every prior version enabled and readable by explicit version id; disable
+ # the ones that predate this write so a rotated/detached secret value can't be read back.
+ for version in previous_versions:
+ try:
+ await secret_client.update_secret_properties(secret_name, version, enabled=False)
+ except Exception:
+ LOG.warning(
+ "Failed to disable prior Azure secret version",
+ secret_name=secret_name,
+ version=version,
+ exc_info=True,
+ )
return secret.name
except Exception as e:
LOG.exception("Failed to create secret from Azure Key Vault.", secret_name=secret_name, error=e)
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index d901d26d0..40ccf9d84 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -1898,8 +1898,8 @@ def _make_agent_result(
pause_outcome = ctx.credential_pause_outcome
if pause_outcome:
pause_payload = {"outcome": pause_outcome}
- if pause_outcome == "connected" and ctx.request_policy and ctx.request_policy.resolved_credentials:
- pause_payload["credentialId"] = ctx.request_policy.resolved_credentials[-1].credential_id
+ if pause_outcome == "connected" and ctx.credential_pause_connected_credential_id:
+ pause_payload["credentialId"] = ctx.credential_pause_connected_credential_id
payload_updates["credentialPause"] = pause_payload
if ctx is not None and "verifiedSuccess" not in narrative_payload:
payload_updates["verifiedSuccess"] = bool(verified_goal_claim_authorized(ctx))
diff --git a/skyvern/forge/sdk/copilot/context.py b/skyvern/forge/sdk/copilot/context.py
index fd43819b0..6bab78de7 100644
--- a/skyvern/forge/sdk/copilot/context.py
+++ b/skyvern/forge/sdk/copilot/context.py
@@ -618,7 +618,8 @@ _MAX_APPROVED_CREDENTIALS = 20
def record_approved_credentials_in_global_llm_context(ctx: CopilotContext, raw_context: str | None) -> str | None:
"""Persist resolved credentials as durable cross-turn approval. Records only from
resolved_credentials, never discovered_credentials, so ADR-0002's run/draft split
- holds by construction.
+ holds by construction. A credential a live login page vouched for is left out: its
+ evidence is that page, which a later turn has not seen, so it must be re-earned there.
"""
policy = ctx.request_policy
if policy is None or not policy.resolved_credentials:
@@ -626,7 +627,7 @@ def record_approved_credentials_in_global_llm_context(ctx: CopilotContext, raw_c
sc = StructuredContext.from_json_str(raw_context)
existing_ids = {record.credential_id for record in sc.approved_credentials}
for credential in policy.resolved_credentials:
- if credential.credential_id in existing_ids:
+ if credential.credential_id in existing_ids or credential.credential_id in policy.live_page_admitted_urls:
continue
sc.approved_credentials.append(ApprovedCredential(credential_id=credential.credential_id))
existing_ids.add(credential.credential_id)
@@ -787,6 +788,7 @@ class CopilotContext(AgentContext):
credential_pause_used: bool = False
copilot_credential_pause_seconds: float = 0.0
credential_pause_outcome: str | None = None
+ credential_pause_connected_credential_id: str | None = None
# Tool tracking
consecutive_tool_tracker: list[str] = field(default_factory=list)
diff --git a/skyvern/forge/sdk/copilot/credential_pause.py b/skyvern/forge/sdk/copilot/credential_pause.py
index 0d31fe8d6..beb7d15d3 100644
--- a/skyvern/forge/sdk/copilot/credential_pause.py
+++ b/skyvern/forge/sdk/copilot/credential_pause.py
@@ -315,6 +315,7 @@ def _apply_connected_credential_to_policy(ctx: Any, policy: RequestPolicy, crede
untested draft instead of being forced back through update_and_run_blocks.
"""
ctx.test_after_update_done = False
+ ctx.credential_pause_connected_credential_id = credential.credential_id
policy.resolved_credentials.append(credential)
policy.allow_run_blocks = True
policy.clarification_reason = "none"
diff --git a/skyvern/forge/sdk/copilot/credential_resolution.py b/skyvern/forge/sdk/copilot/credential_resolution.py
new file mode 100644
index 000000000..46015c4a2
--- /dev/null
+++ b/skyvern/forge/sdk/copilot/credential_resolution.py
@@ -0,0 +1,142 @@
+"""Deterministic credential matching shared by every seam that needs credential authority.
+
+Turn start and the browser scout run the same tiers over different evidence: prompt text
+before a page exists, the live page URL once one does. Authority is therefore a function of
+the evidence at hand, not a set computed once at turn start.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Sequence
+from dataclasses import dataclass
+from typing import Literal
+from urllib.parse import parse_qsl, urlencode, urlparse
+
+from skyvern.forge import app
+from skyvern.forge.sdk.schemas.credentials import Credential, CredentialType
+
+CredentialUrlTier = Literal["url_exact", "url_path", "url_host"]
+CredentialResolutionTier = Literal["url_exact", "url_path", "url_host", "urlless_sole"]
+CredentialResolutionVerdict = Literal["resolved", "ambiguous", "unresolved"]
+
+_URL_TIERS: tuple[CredentialResolutionTier, ...] = ("url_exact", "url_path", "url_host")
+
+
+@dataclass(frozen=True)
+class CredentialResolution:
+ verdict: CredentialResolutionVerdict
+ candidates: tuple[Credential, ...] = ()
+ tier: CredentialResolutionTier | None = None
+
+ def contains(self, credential_id: str) -> bool:
+ return any(candidate.credential_id == credential_id for candidate in self.candidates)
+
+
+async def load_credentials(organization_id: str) -> list[Credential]:
+ page = 1
+ credentials: list[Credential] = []
+ while True:
+ items = await app.DATABASE.credentials.get_credentials(organization_id=organization_id, page=page, page_size=50)
+ credentials.extend(items)
+ if len(items) < 50:
+ return sorted(credentials, key=lambda c: getattr(c, "created_at", None) or "", reverse=True)
+ page += 1
+
+
+def url_parts(url: str) -> tuple[str, str, str] | None:
+ parsed = urlparse(url if "://" in url else f"https://{url}")
+ if not parsed.netloc:
+ return None
+ host = parsed.netloc.lower()
+ path = parsed.path.rstrip("/")
+ # Login pages can differ only by query (?tenant=a vs ?tenant=b), so the
+ # exact tier keys on the full URL with a param-order-insensitive query.
+ query = urlencode(sorted(parse_qsl(parsed.query, keep_blank_values=True)))
+ origin = f"{parsed.scheme.lower()}://{host}"
+ # A live sign-in URL carries ?next=/?state=, so the path tier is what a saved tested_url
+ # actually matches; the host tier alone would let any page on the host claim the credential.
+ without_query = f"{origin}{path}"
+ return (f"{without_query}?{query}" if query else without_query), without_query, origin
+
+
+def loggable_origin(url: str) -> str:
+ """The origin with any `user:pass@` dropped, for logs.
+
+ `url_parts` keys on the whole netloc so that `https://[email protected]/` cannot
+ match `real.example.com`; that same netloc would carry basic-auth credentials into a log line.
+ """
+ parsed = urlparse(url if "://" in url else f"https://{url}")
+ if not parsed.hostname:
+ return ""
+ try:
+ port = f":{parsed.port}" if parsed.port else ""
+ except ValueError:
+ port = ""
+ return f"{parsed.scheme.lower()}://{parsed.hostname}{port}"
+
+
+def deduplicate_credentials(credentials: list[Credential]) -> list[Credential]:
+ by_id: dict[str, Credential] = {}
+ for credential in credentials:
+ by_id.setdefault(credential.credential_id, credential)
+ return list(by_id.values())
+
+
+def _match_by_url_tiered(
+ credentials: list[Credential],
+ urls: list[str],
+ *,
+ tiers: Sequence[CredentialUrlTier],
+) -> tuple[CredentialResolutionTier | None, list[Credential]]:
+ indexed = [
+ (credential, parts)
+ for credential in credentials
+ if credential.tested_url and (parts := url_parts(credential.tested_url))
+ ]
+ requested = [parts for url in urls if (parts := url_parts(url))]
+ # Ranked in _URL_TIERS order whatever order the caller listed them in: an exact match must
+ # never lose to a looser one.
+ for tier in sorted(set(tiers), key=_URL_TIERS.index):
+ index = _URL_TIERS.index(tier)
+ matches = [
+ credential
+ for credential, parts in indexed
+ # A query on the *saved* URL is identity — a tenant, an account. Only the live page
+ # brings transient ?next=/?state=, so dropping the query is safe in that direction only.
+ if not (tier == "url_path" and parts[0] != parts[1])
+ and any(parts[index] == target[index] for target in requested)
+ ]
+ if matches:
+ return tier, deduplicate_credentials(matches)
+ return None, []
+
+
+def resolve_by_url(
+ credentials: list[Credential],
+ urls: list[str],
+ *,
+ tiers: Sequence[CredentialUrlTier],
+ password_only: bool = True,
+ allow_urlless_sole: bool = False,
+) -> CredentialResolution:
+ """Rank saved credentials against login-page URLs over ``tiers``, in `_URL_TIERS` order, then a
+ URL-less sole candidate. ``password_only`` keeps a card or secret credential carrying a
+ ``tested_url`` from auto-binding as a login; naming one explicitly is a different tier."""
+ pool = (
+ [credential for credential in credentials if credential.credential_type == CredentialType.PASSWORD]
+ if password_only
+ else list(credentials)
+ )
+ tier, matches = _match_by_url_tiered(pool, urls, tiers=tiers)
+ if not matches and allow_urlless_sole:
+ # A credential saved without a login URL is still a valid login candidate (SKY-12959);
+ # it ranks below every URL tier so a tested match always wins.
+ tier, matches = (
+ "urlless_sole",
+ deduplicate_credentials([credential for credential in pool if not credential.tested_url]),
+ )
+ if len(matches) == 1:
+ return CredentialResolution("resolved", tuple(matches), tier)
+ if matches:
+ return CredentialResolution("ambiguous", tuple(matches), tier)
+ return CredentialResolution("unresolved")
diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.py
index ade7fa895..2c7032119 100644
--- a/skyvern/forge/sdk/copilot/request_policy.py
+++ b/skyvern/forge/sdk/copilot/request_policy.py
@@ -6,9 +6,9 @@ import hashlib
import json
import re
import time
+from collections.abc import Awaitable, Callable
from dataclasses import dataclass, field, replace
from typing import Any, Literal, cast, get_args
-from urllib.parse import parse_qsl, urlencode, urlparse
import structlog
@@ -23,6 +23,13 @@ from skyvern.forge.sdk.copilot.context import (
prior_signin_email_from_context,
sanitize_global_llm_context_for_prompt,
)
+from skyvern.forge.sdk.copilot.credential_resolution import deduplicate_credentials as _deduplicate_credentials
+from skyvern.forge.sdk.copilot.credential_resolution import load_credentials as _load_credentials
+from skyvern.forge.sdk.copilot.credential_resolution import (
+ loggable_origin,
+ resolve_by_url,
+)
+from skyvern.forge.sdk.copilot.credential_resolution import url_parts as _url_parts
from skyvern.forge.sdk.copilot.llm_errors import is_retriable_llm_error
from skyvern.forge.sdk.copilot.output_utils import parse_final_response
from skyvern.forge.sdk.copilot.reached_download_target import REGISTERED_DOWNLOAD_REQUESTED_OUTPUT_PATHS
@@ -48,7 +55,7 @@ from skyvern.forge.sdk.copilot.workflow_credential_utils import (
workflow_credential_ids,
workflow_credential_origins,
)
-from skyvern.forge.sdk.schemas.credentials import Credential, CredentialType
+from skyvern.forge.sdk.schemas.credentials import Credential
from skyvern.forge.sdk.schemas.workflow_copilot import (
WorkflowCopilotChatHistoryMessage,
WorkflowCopilotChatSender,
@@ -571,6 +578,9 @@ class RequestPolicy:
completion_contract: str | None = None
completion_criteria: list[CompletionCriterion] = field(default_factory=list)
resolved_credentials: list[Credential] = field(default_factory=list)
+ # credential_id -> the login page URL that granted it, so each later fill can confirm the
+ # browser has not left that site and a redirect cannot inherit the grant.
+ live_page_admitted_urls: dict[str, str] = field(default_factory=dict)
# Approves persisting a bound credential, not running it: run authority stays
# scoped to resolved_credentials (ADR 0002).
discovered_credentials: list[Credential] = field(default_factory=list)
@@ -746,7 +756,7 @@ class RequestPolicy:
if self.resolved_credentials:
lines += [
"resolved_credentials:",
- *[f"- {_safe_label(credential)}" for credential in self.resolved_credentials],
+ *[f"- {credential_candidate_label(credential)}" for credential in self.resolved_credentials],
]
if self.invalid_credential_ids:
lines.append("invalid_credential_ids: " + ", ".join(f"`{cid}`" for cid in self.invalid_credential_ids))
@@ -4292,17 +4302,6 @@ async def _classify_request(
return policy
-async def _load_credentials(organization_id: str) -> list[Credential]:
- page = 1
- credentials: list[Credential] = []
- while True:
- items = await app.DATABASE.credentials.get_credentials(organization_id=organization_id, page=page, page_size=50)
- credentials.extend(items)
- if len(items) < 50:
- return sorted(credentials, key=lambda c: getattr(c, "created_at", None) or "", reverse=True)
- page += 1
-
-
def _quote_in_credential_context(user_message: str, quote_start: int) -> bool:
return bool(_CREDENTIAL_QUOTE_CONTEXT_RE.search(user_message[max(0, quote_start - 48) : quote_start]))
@@ -4312,7 +4311,9 @@ def _credential_reference_is_negated(user_message: str, reference_start: int) ->
context = user_message[clause_start + 1 : reference_start]
context = re.sub(r",\s*(?=credential\b)", " ", context, flags=re.I)
if context.rstrip().endswith(",") and re.match(
- r"\s*(?:credential\b|cred_|[`'\"])",
+ # Any wrapper the name arrives in — quotes, markdown emphasis, brackets — reads the same:
+ # "do not use the credential, **prod**" is one clause, not an appositive.
+ r"\s*(?:credential\b|cred_|[`'\"“”‘’*_([{<])",
user_message[reference_start:],
re.I,
):
@@ -4392,6 +4393,8 @@ def _credential_name_candidates(user_message: str) -> list[str]:
_CREDENTIAL_NAME_MIN_DISTINCTIVE_LENGTH = 4
+# Labels that mean the classifier read this turn as referring to a stored credential at all.
+_CREDENTIAL_REFERENCE_INPUT_KINDS = frozenset({"credential_id", "credential_name", "website_stored_credential"})
def _explicit_login_action_asserted(user_message: str) -> bool:
@@ -4405,25 +4408,54 @@ def _explicit_login_action_asserted(user_message: str) -> bool:
)
+def _wrapper_start(text: str, index: int) -> int:
+ """Step back over quotes, brackets or markdown emphasis wrapping a reference."""
+ while index > 0 and text[index - 1] in "`'\"“”‘’*_([{<":
+ index -= 1
+ return index
+
+
+def _name_stated_affirmatively(text: str, name: str) -> bool:
+ name = (name or "").strip()
+ if len(name) < _CREDENTIAL_NAME_MIN_DISTINCTIVE_LENGTH or name.lower() in _CREDENTIAL_REFERENCE_STOPWORDS:
+ return False
+ pattern = rf"(?<![A-Za-z0-9_.@:-]){re.escape(name)}(?![A-Za-z0-9_.@:-])"
+ for match in re.finditer(pattern, text, re.IGNORECASE):
+ # Negation reads the clause before the reference, and any wrapper around the name belongs to
+ # it: from inside the quotes, "credential, `prod`" no longer reads as an appositive.
+ start = _wrapper_start(text, match.start())
+ if not _credential_reference_is_negated(text, start) and not _credential_reference_is_unrelated_replacement(
+ text, start
+ ):
+ return True
+ return False
+
+
def _saved_credential_names_mentioned(user_message: str, credentials: list[Credential]) -> list[str]:
"""Saved names a message states outright, e.g. "use skyvern-datadog to login".
The saved names are known, so match on them rather than on the prose around them.
"""
text = _credential_authority_text(user_message or "")
- mentioned: list[str] = []
- for credential in credentials:
- name = (credential.name or "").strip()
- if len(name) < _CREDENTIAL_NAME_MIN_DISTINCTIVE_LENGTH or name.lower() in _CREDENTIAL_REFERENCE_STOPWORDS:
- continue
- for match in re.finditer(rf"(?<![A-Za-z0-9_.@:-]){re.escape(name)}(?![A-Za-z0-9_.@:-])", text, re.IGNORECASE):
- if _credential_reference_is_negated(text, match.start()) or _credential_reference_is_unrelated_replacement(
- text, match.start()
- ):
- continue
- mentioned.append(name)
- break
- return _clean_list(mentioned)
+ return _clean_list(
+ [
+ (credential.name or "").strip()
+ for credential in credentials
+ if _name_stated_affirmatively(text, credential.name or "")
+ ]
+ )
+
+
+def _classifier_ref_stated_in_message(policy: RequestPolicy, user_message: str) -> bool:
+ """Whether a credential-labelled turn's extracted name appears affirmatively in this message.
+
+ Which credential label the classifier chose must not decide whether a name the user wrote is
+ looked up by name (SKY-12917), but a turn it never called a credential request may not scan.
+ """
+ if policy.classifier_status == "fallback" or policy.credential_input_kind not in _CREDENTIAL_REFERENCE_INPUT_KINDS:
+ return False
+ text = _credential_authority_text(user_message or "")
+ return any(isinstance(ref, str) and _name_stated_affirmatively(text, ref) for ref in policy.credential_refs)
def _explicit_credential_ids(user_message: str) -> list[str]:
@@ -4462,14 +4494,7 @@ def _explicit_credential_ids(user_message: str) -> list[str]:
return _clean_list(explicit)
-def _deduplicate_credentials(credentials: list[Credential]) -> list[Credential]:
- by_id: dict[str, Credential] = {}
- for credential in credentials:
- by_id.setdefault(credential.credential_id, credential)
- return list(by_id.values())
-
-
-def _safe_label(credential: Credential) -> str:
+def credential_candidate_label(credential: Credential) -> str:
parts = [f"`{credential.credential_id}`", credential.name]
parts += [f"Login Page URL: {credential.tested_url}"] if credential.tested_url else []
return " - ".join(parts)
@@ -4488,48 +4513,17 @@ def _block(
if reason is not None:
policy.clarification_reason = reason
if candidates:
- question += "\n\nSafe matches:\n" + "\n".join(f"- {_safe_label(candidate)}" for candidate in candidates)
+ question += "\n\nSafe matches:\n" + "\n".join(
+ f"- {credential_candidate_label(candidate)}" for candidate in candidates
+ )
policy.clarification_question = question
-def _url_parts(url: str) -> tuple[str, str] | None:
- parsed = urlparse(url if "://" in url else f"https://{url}")
- if not parsed.netloc:
- return None
- host = parsed.netloc.lower()
- path = parsed.path.rstrip("/")
- # Login pages can differ only by query (?tenant=a vs ?tenant=b), so the
- # exact tier keys on the full URL with a param-order-insensitive query.
- query = urlencode(sorted(parse_qsl(parsed.query, keep_blank_values=True)))
- exact = f"{parsed.scheme.lower()}://{host}{path}"
- if query:
- exact = f"{exact}?{query}"
- return exact, f"{parsed.scheme.lower()}://{host}"
-
-
-def _match_by_url(
- credentials: list[Credential], urls: list[str], *, allow_host_fallback: bool = True
-) -> list[Credential]:
- indexed = [
- (credential, parts)
- for credential in credentials
- if credential.tested_url and (parts := _url_parts(credential.tested_url))
- ]
- requested = [parts for url in urls if (parts := _url_parts(url))]
- for index in range(2 if allow_host_fallback else 1):
- matches = [
- credential for credential, parts in indexed if any(parts[index] == target[index] for target in requested)
- ]
- if matches:
- return matches
- return []
-
-
def _login_url_candidates(policy: RequestPolicy, user_message: str) -> list[str]:
if policy.login_page_urls:
return _clean_list(policy.login_page_urls)
candidates = _clean_list([candidate.rstrip(".,;:!?") for candidate in URL_CANDIDATE_RE.findall(user_message)])
- candidate_hosts = {parts[1] for candidate in candidates if (parts := _url_parts(candidate))}
+ candidate_hosts = {parts[2] for candidate in candidates if (parts := _url_parts(candidate))}
if len(candidate_hosts) > 1:
return []
return candidates
@@ -4646,7 +4640,7 @@ def _login_credentials_unresolved(policy: RequestPolicy, user_message: str, work
def _signin_target_hosts(policy: RequestPolicy, user_message: str) -> list[str]:
- return [parts[1] for url in _login_url_candidates(policy, user_message) if (parts := _url_parts(url))]
+ return [parts[2] for url in _login_url_candidates(policy, user_message) if (parts := _url_parts(url))]
def _is_passwordless_email_signin(policy: RequestPolicy) -> bool:
@@ -4858,7 +4852,11 @@ async def _resolve_credentials(
# it, so a message asking for a login in its own words must still count. A classifier that failed
# outright is a different case: a degraded turn never enumerates the org's saved credentials.
login_phrase_asserted = _explicit_login_action_asserted(user_message)
- may_scan_saved_names = policy.login_intent or (policy.classifier_status != "fallback" and login_phrase_asserted)
+ may_scan_saved_names = (
+ policy.login_intent
+ or (policy.classifier_status != "fallback" and login_phrase_asserted)
+ or _classifier_ref_stated_in_message(policy, user_message)
+ )
name_candidates = _credential_name_candidates(user_message)
credentials: list[Credential] | None = None
# Only a request that already asserts a login or credential intent may be matched against the
@@ -4947,54 +4945,29 @@ async def _resolve_credentials(
if url_candidates:
if credentials is None:
credentials = await _load_credentials(organization_id)
- url_credentials = credentials
- if current_turn_login_authorized:
- # Card/secret credentials can carry a tested_url too; a
- # login turn must only ever auto-bind a password credential.
- url_credentials = [
- credential for credential in credentials if credential.credential_type == CredentialType.PASSWORD
- ]
- matches = _match_by_url(
- url_credentials,
+ resolution = resolve_by_url(
+ credentials,
url_candidates,
# A URL merely mentioned in prose is a weaker signal than a
# classifier-extracted login page; require an exact tested-URL hit.
- allow_host_fallback=allow_host_fallback,
+ tiers=("url_exact", "url_host") if allow_host_fallback else ("url_exact",),
+ password_only=current_turn_login_authorized,
+ allow_urlless_sole=current_turn_login_authorized,
)
- matches = _deduplicate_credentials(matches)
- if len(matches) == 1:
- policy.resolved_credentials = matches
+ if resolution.verdict == "resolved":
+ policy.resolved_credentials = list(resolution.candidates)
return
- if matches:
+ if resolution.verdict == "ambiguous":
# Found-but-unbound keeps the unresolved-login override from
# relabeling this disambiguation ask as a missing-credential turn.
- policy.discovered_credentials = matches
+ policy.discovered_credentials = list(resolution.candidates)
_block(
policy,
_AMBIGUOUS_URL_CREDENTIAL_QUESTION,
- matches,
+ list(resolution.candidates),
reason="credential_name_unresolved",
)
return
- if current_turn_login_authorized:
- # URL-matched password credentials rank first. If neither URL
- # tier matches, credentials saved without a tested URL remain
- # valid login candidates.
- urlless = _deduplicate_credentials(
- [credential for credential in url_credentials if not credential.tested_url]
- )
- if len(urlless) == 1:
- policy.resolved_credentials = urlless
- return
- if urlless:
- policy.discovered_credentials = urlless
- _block(
- policy,
- _AMBIGUOUS_URL_CREDENTIAL_QUESTION,
- urlless,
- reason="credential_name_unresolved",
- )
- return
if policy.credential_input_kind == "credential_name":
if policy.testing_intent == "skip_test" or defer_unresolved_credential_name:
@@ -5027,6 +5000,91 @@ async def _resolve_credentials(
)
+@dataclass(frozen=True)
+class LiveCredentialAdmission:
+ admitted: bool
+ steer: str | None = None
+ page_url: str | None = None
+
+
+async def admit_credential_for_live_page(
+ policy: RequestPolicy,
+ *,
+ organization_id: str,
+ credential_id: str,
+ page_url: str,
+ load_org_credentials: Callable[[], Awaitable[list[Credential]]] | None = None,
+) -> LiveCredentialAdmission:
+ """Grant run authority for the credential whose saved login URL matches the page the scout reached.
+
+ The discriminator is that server-computed match, never the model's claim: a credential the page
+ does not vouch for is refused, and so is one saved without a login URL, which is evidence about
+ no page at all.
+ """
+ if policy.raw_secret_detected or not policy.allow_run_blocks or not credential_id or not page_url:
+ return LiveCredentialAdmission(False)
+ if _is_passwordless_email_signin(policy):
+ # Turn-start resolution excludes this shape from every URL tier because a site that signs in
+ # by emailed link has no password question to put to the user, so admitting one here would
+ # fill a password the user said the account does not have.
+ LOG.info(
+ "copilot credential live-page admission",
+ outcome="passwordless_declined",
+ organization_id=organization_id,
+ credential_id=credential_id,
+ page_url=loggable_origin(page_url),
+ )
+ return LiveCredentialAdmission(False)
+
+ resolution = resolve_by_url(
+ await (load_org_credentials() if load_org_credentials else _load_credentials(organization_id)),
+ [page_url],
+ # The scout reached this page, so its own path is the evidence; a bare host match would let
+ # any other page on that host — a profile, an upload, a user-content route — claim the login.
+ tiers=("url_exact", "url_path"),
+ password_only=True,
+ )
+ # Being *one of* several page matches is not authority; only a sole match the page
+ # vouches for is, so an ambiguous set still asks even when it contains the requested id.
+ admitted = resolution.verdict == "resolved" and resolution.contains(credential_id)
+ LOG.info(
+ "copilot credential live-page admission",
+ outcome="admitted"
+ if admitted
+ else ("wrong_credential" if resolution.verdict == "resolved" else resolution.verdict),
+ organization_id=organization_id,
+ credential_id=credential_id,
+ page_url=loggable_origin(page_url),
+ tier=resolution.tier,
+ candidate_credential_ids=[candidate.credential_id for candidate in resolution.candidates],
+ )
+ if admitted:
+ policy.resolved_credentials = _deduplicate_credentials(
+ list(policy.resolved_credentials) + list(resolution.candidates)
+ )
+ for candidate in resolution.candidates:
+ policy.live_page_admitted_urls[candidate.credential_id] = page_url
+ return LiveCredentialAdmission(True, page_url=page_url)
+
+ if resolution.verdict == "ambiguous":
+ policy.discovered_credentials = _deduplicate_credentials(
+ list(policy.discovered_credentials) + list(resolution.candidates)
+ )
+ return LiveCredentialAdmission(
+ False,
+ steer=f"{_AMBIGUOUS_URL_CREDENTIAL_QUESTION} Ask the user which one to use, then fill it.",
+ )
+ if resolution.verdict == "resolved":
+ return LiveCredentialAdmission(
+ False,
+ steer=(
+ f"`{credential_id}` is not the saved credential for this login page. Ask the user which "
+ "saved credential to use here."
+ ),
+ )
+ return LiveCredentialAdmission(False)
+
+
def _is_login_credential_param(param: dict[str, Any]) -> bool:
raw = param.get("parameter_type")
if not isinstance(raw, str):
diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.py
index 6adfe94d6..187540fa8 100644
--- a/skyvern/forge/sdk/copilot/runtime.py
+++ b/skyvern/forge/sdk/copilot/runtime.py
@@ -38,6 +38,7 @@ from skyvern.forge.sdk.copilot.turn_origin import (
)
from skyvern.forge.sdk.copilot.verification_evidence import WorkflowVerificationEvidence
from skyvern.forge.sdk.core import skyvern_context
+from skyvern.forge.sdk.schemas.credentials import Credential
from skyvern.library.skyvern_browser import SkyvernBrowser
from skyvern.webeye.browser_state import BrowserState
@@ -509,6 +510,8 @@ class AgentContext:
# Which requires-live-scout fields (username/password, non-empty) each scouted credential
# carries; recorded at credential resolve time and rehydrated from FillCarry across turns.
scouted_credential_field_inventory_by_credential_id: dict[str, frozenset[str]] = field(default_factory=dict)
+ # Read once per turn: repeated fill attempts must not re-scan the org's credentials.
+ org_credentials_for_turn: list[Credential] | None = None
# Highest trajectory_index visible at the latest parsed evaluate observation and whether that page
# showed a password-type control; orders page evidence against post-fill submits across evictions.
last_scout_observation_trajectory_index: int | None = None
diff --git a/skyvern/forge/sdk/copilot/tools/__init__.py b/skyvern/forge/sdk/copilot/tools/__init__.py
index f7bf879f9..675638e5b 100644
--- a/skyvern/forge/sdk/copilot/tools/__init__.py
+++ b/skyvern/forge/sdk/copilot/tools/__init__.py
@@ -97,7 +97,8 @@ from .composition_capture import (
)
from .composition_capture import _normalized_inspect_url as _normalized_inspect_url
from .composition_capture import _same_inspect_target as _same_inspect_target
-from .credential_fill import _credential_fill_policy_error as _credential_fill_policy_error
+from .credential_fill import _credential_fill_authority_error as _credential_fill_authority_error
+from .credential_fill import _credential_fill_prerequisite_error as _credential_fill_prerequisite_error
from .credential_fill import (
_fill_credential_field_impl,
)
diff --git a/skyvern/forge/sdk/copilot/tools/credential_fill.py b/skyvern/forge/sdk/copilot/tools/credential_fill.py
index 1775c6c9a..16e52b251 100644
--- a/skyvern/forge/sdk/copilot/tools/credential_fill.py
+++ b/skyvern/forge/sdk/copilot/tools/credential_fill.py
@@ -7,15 +7,16 @@ import structlog
from skyvern.cli.core.session_manager import get_page
from skyvern.forge import app
from skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy
+from skyvern.forge.sdk.copilot.credential_resolution import load_credentials, url_parts
from skyvern.forge.sdk.copilot.loop_detection import record_tool_step_result_for_ctx
-from skyvern.forge.sdk.copilot.request_policy import RequestPolicy
+from skyvern.forge.sdk.copilot.request_policy import RequestPolicy, admit_credential_for_live_page
from skyvern.forge.sdk.copilot.runtime import AgentContext, ensure_browser_session, mcp_browser_context
from skyvern.forge.sdk.copilot.secret_scrub import (
REDACTED_SECRET_PLACEHOLDER,
register_secret_scrub_value,
scrub_secrets_from_text,
)
-from skyvern.forge.sdk.schemas.credentials import CredentialVaultType, PasswordCredential, TotpType
+from skyvern.forge.sdk.schemas.credentials import Credential, CredentialVaultType, PasswordCredential, TotpType
from skyvern.forge.sdk.services.credentials import generate_totp_code, normalize_totp_config
from .banned_blocks import _copilot_block_authoring_policy
@@ -68,7 +69,7 @@ def _scrub_secret_from_text(text: str, secret_value: str) -> str:
return text.replace(secret_value, REDACTED_SECRET_PLACEHOLDER)
-def _credential_fill_policy_error(copilot_ctx: AgentContext, credential_id: str) -> str | None:
+def _credential_fill_prerequisite_error(copilot_ctx: AgentContext, credential_id: str) -> str | None:
if _copilot_block_authoring_policy(copilot_ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
return (
"fill_credential_field is only available in code-only browser authoring mode. "
@@ -80,10 +81,26 @@ def _credential_fill_policy_error(copilot_ctx: AgentContext, credential_id: str)
"Saved-credential scouting is not authorized for this request. "
"Ask the user for the required credential or clarification before filling credential fields."
)
+ return None
+
+
+def _still_on_admitted_site(current_url: str | None, admitted_url: str) -> bool:
+ """Whether the browser is still on the origin whose login page granted this credential.
+
+ Compared at origin, not at the tier that granted it: a real sign-in walks email -> password ->
+ one-time code across several paths of the same site, and refusing those would refuse the login
+ the grant exists for. What it still stops is the secret following a redirect off the site.
+ """
+ admitted_parts = url_parts(admitted_url)
+ current_parts = url_parts(current_url or "")
+ return bool(current_parts and admitted_parts and current_parts[2] == admitted_parts[2])
+
+
+def _credential_fill_authority_error(copilot_ctx: AgentContext, credential_id: str) -> str | None:
+ policy = copilot_ctx.request_policy
resolved_ids = {
credential.credential_id
- for credential in policy.resolved_credentials
- if isinstance(getattr(credential, "credential_id", None), str)
+ for credential in (policy.resolved_credentials if isinstance(policy, RequestPolicy) else [])
}
if credential_id not in resolved_ids:
return (
@@ -95,6 +112,43 @@ def _credential_fill_policy_error(copilot_ctx: AgentContext, credential_id: str)
return None
+async def _credential_fill_gate_error(copilot_ctx: AgentContext, credential_id: str) -> tuple[str | None, str | None]:
+ """Refuse only after asking the live page whether it vouches for this credential.
+
+ Returns the refusal (if any) and the page URL that justified an admission, so the caller can
+ confirm the browser is still on that page before the secret is typed.
+ """
+ prerequisite_error = _credential_fill_prerequisite_error(copilot_ctx, credential_id)
+ if prerequisite_error:
+ return prerequisite_error, None
+ policy = copilot_ctx.request_policy
+ authority_error = _credential_fill_authority_error(copilot_ctx, credential_id)
+ if not authority_error:
+ # A grant the live page made stays bound to that page: re-checking only the call that
+ # made it would let the retry after a redirect inherit authority it never earned.
+ admitted = policy.live_page_admitted_urls.get(credential_id) if isinstance(policy, RequestPolicy) else None
+ return None, admitted
+
+ if not isinstance(policy, RequestPolicy):
+ return authority_error, None
+
+ async def load_once() -> list[Credential]:
+ if copilot_ctx.org_credentials_for_turn is None:
+ copilot_ctx.org_credentials_for_turn = await load_credentials(copilot_ctx.organization_id)
+ return copilot_ctx.org_credentials_for_turn
+
+ admission = await admit_credential_for_live_page(
+ policy,
+ organization_id=copilot_ctx.organization_id,
+ credential_id=credential_id,
+ page_url=await _live_working_page_url(copilot_ctx) or "",
+ load_org_credentials=load_once,
+ )
+ if admission.admitted:
+ return None, admission.page_url
+ return admission.steer or authority_error, None
+
+
async def _resolve_credential_fill_value(
copilot_ctx: AgentContext,
credential_id: str,
@@ -201,7 +255,7 @@ async def _fill_credential_field_impl(
return finish({"ok": False, "error": "fill_credential_field requires a CSS selector for the input field."})
if field not in _CREDENTIAL_FILL_FIELDS:
return finish({"ok": False, "error": "fill_credential_field `field` must be one of: username, password, totp."})
- policy_error = _credential_fill_policy_error(copilot_ctx, credential_id)
+ policy_error, admitted_page = await _credential_fill_gate_error(copilot_ctx, credential_id)
if policy_error:
LOG.info(
"copilot fill_credential_field rejected tool-side",
@@ -222,6 +276,19 @@ async def _fill_credential_field_impl(
try:
async with mcp_browser_context(copilot_ctx):
page, _ = await get_page(session_id=copilot_ctx.browser_session_id)
+ # Checked on the page about to be filled, with nothing awaited in between: the vault read
+ # and the evidence capture above both give a redirect room to move the browser.
+ if admitted_page and not _still_on_admitted_site(page.url, admitted_page):
+ return finish(
+ {
+ "ok": False,
+ "error": (
+ "The browser left the site whose login page authorized this credential "
+ "before it could be filled. Re-inspect the current page and fill again if "
+ "the sign-in is still in progress there."
+ ),
+ }
+ )
await page.fill(selector, value, mode="direct", timeout=_CREDENTIAL_FILL_TIMEOUT_MS)
except Exception as exc:
error_text = scrub_secrets_from_text(copilot_ctx, _scrub_secret_from_text(str(exc), value))
diff --git a/skyvern/forge/sdk/routes/credentials.py b/skyvern/forge/sdk/routes/credentials.py
index ecb877eb5..2fa9e7df4 100644
--- a/skyvern/forge/sdk/routes/credentials.py
+++ b/skyvern/forge/sdk/routes/credentials.py
@@ -33,6 +33,7 @@ exact threat the vault architecture is designed to prevent.
import asyncio
import json
import time
+from contextlib import AbstractAsyncContextManager, nullcontext
from dataclasses import dataclass
from typing import Annotated, Any, NoReturn
@@ -186,6 +187,7 @@ _SAVED_AUTHENTICATOR_SECRET_INVALID_DETAIL = (
"Saved authenticator key is invalid. Edit the credential and paste the raw setup key "
"or full otpauth:// URI from the website's 2FA setup screen."
)
+CREDENTIAL_WRITE_CONFLICT_DETAIL = "Credential was modified by another request. Please retry."
_TOTP_CODE_PREVIEW_CACHE_MAX_ENTRIES = 1024
@@ -695,6 +697,13 @@ async def create_credential(
organization_id=current_org.organization_id,
)
+ await app.AGENT_FUNCTION.validate_credential_write(
+ organization_id=current_org.organization_id,
+ credential_type=data.credential_type,
+ credential=data.credential,
+ existing_credential=None,
+ )
+
# Validate the profile link BEFORE provisioning, so an invalid/managed/cross-org/owned profile
# rejects with a 400 without leaving an orphan credential (and vault secret) behind.
if data.browser_profile_id is not None:
@@ -891,7 +900,18 @@ async def rename_credential(
proxy_session_id=data.proxy_session_id,
rotate_proxy_session_id=data.rotate_proxy_session_id,
)
- updated = await _update_credential_or_profile_conflict(**update_kwargs)
+ requires_write_lock = await app.AGENT_FUNCTION.should_lock_credential_write(
+ credential=None,
+ existing_credential=credential,
+ )
+ write_guard: AbstractAsyncContextManager[None] = nullcontext()
+ if requires_write_lock:
+ write_guard = app.AGENT_FUNCTION.credential_write_lock(
+ organization_id=current_org.organization_id,
+ credential_id=credential_id,
+ )
+ async with write_guard:
+ updated = await _update_credential_or_profile_conflict(**update_kwargs)
if not updated:
raise HTTPException(status_code=500, detail="Failed to update credential")
@@ -1746,6 +1766,90 @@ async def _create_browser_profile_after_workflow(
)
+async def _delete_replaced_credential_item(
+ *,
+ credential_service: CredentialVaultService,
+ organization_id: str,
+ item_id: str,
+ vault_type: CredentialVaultType,
+) -> None:
+ try:
+ deleted = await credential_service.post_delete_credential_item(item_id, organization_id)
+ except Exception as exc:
+ LOG.warning(
+ "Failed to delete replaced credential vault item",
+ organization_id=organization_id,
+ item_id=item_id,
+ vault_type=vault_type,
+ error_type=type(exc).__name__,
+ )
+ else:
+ if deleted:
+ return
+ LOG.warning(
+ "Credential vault provider rejected replaced item cleanup",
+ organization_id=organization_id,
+ item_id=item_id,
+ vault_type=vault_type,
+ )
+
+ try:
+ await app.AGENT_FUNCTION.on_credential_item_orphaned(
+ organization_id=organization_id,
+ item_id=item_id,
+ vault_type=vault_type,
+ )
+ except Exception as exc:
+ LOG.error(
+ "Failed to schedule replaced credential vault item cleanup",
+ organization_id=organization_id,
+ item_id=item_id,
+ vault_type=vault_type,
+ error_type=type(exc).__name__,
+ )
+
+
+async def _write_lock_decision_is_stale(*, sampled_credential: Credential, latest_credential: Credential) -> bool:
+ # Vault backends that update secrets in place keep item_id stable, so the lock decision
+ # itself has to be re-evaluated against the refreshed row rather than inferred from item_id.
+ requires_lock = await app.AGENT_FUNCTION.should_lock_credential_write(
+ credential=None,
+ existing_credential=latest_credential,
+ )
+ return latest_credential.item_id != sampled_credential.item_id or requires_lock
+
+
+async def _raise_if_superseded_by_concurrent_write(
+ *,
+ credential_service: CredentialVaultService,
+ credential_id: str,
+ organization_id: str,
+ written_item_id: str,
+ replaced_item_id: str,
+ vault_type: CredentialVaultType,
+) -> None:
+ latest_credential = await app.DATABASE.credentials.get_credential(
+ credential_id=credential_id, organization_id=organization_id
+ )
+ if latest_credential is not None and latest_credential.item_id == written_item_id:
+ return
+
+ LOG.warning(
+ "Credential update was superseded by a concurrent write",
+ credential_id=credential_id,
+ organization_id=organization_id,
+ written_item_id=written_item_id,
+ )
+ if written_item_id != replaced_item_id:
+ await _delete_replaced_credential_item(
+ credential_service=credential_service,
+ organization_id=organization_id,
+ item_id=written_item_id,
+ vault_type=vault_type,
+ )
+ raise HTTPException(status_code=409, detail=CREDENTIAL_WRITE_CONFLICT_DETAIL)
+
+
@legacy_base_router.put("/credentials/{credential_id}")
@legacy_base_router.put("/credentials/{credential_id}/", include_in_schema=False)
@base_router.post(
@@ -1799,10 +1903,15 @@ async def update_credential(
if not existing_credential:
raise HTTPException(status_code=404, detail=f"Credential not found, credential_id={credential_id}")
- if isinstance(data.credential, NonEmptyPasswordCredential):
- await _normalize_authenticator_totp_for_organization_or_raise(
- data.credential,
+ requires_write_lock = await app.AGENT_FUNCTION.should_lock_credential_write(
+ credential=data.credential,
+ existing_credential=existing_credential,
+ )
+ write_guard: AbstractAsyncContextManager[None] = nullcontext()
+ if requires_write_lock:
+ write_guard = app.AGENT_FUNCTION.credential_write_lock(
organization_id=current_org.organization_id,
+ credential_id=credential_id,
)
if data.browser_profile_id is not None:
@@ -1810,36 +1919,86 @@ async def update_credential(
data.browser_profile_id, current_org.organization_id, current_credential_id=credential_id
)
- vault_type = existing_credential.vault_type or CredentialVaultType.BITWARDEN
- credential_service = app.CREDENTIAL_VAULT_SERVICES.get(vault_type)
- if not credential_service:
- raise HTTPException(status_code=400, detail="Unsupported credential storage type")
+ async with write_guard:
+ if requires_write_lock:
+ existing_credential = await app.DATABASE.credentials.get_credential(
+ credential_id=credential_id, organization_id=current_org.organization_id
+ )
+ if not existing_credential:
+ raise HTTPException(status_code=404, detail=f"Credential not found, credential_id={credential_id}")
- old_item_id = existing_credential.item_id
+ if isinstance(data.credential, NonEmptyPasswordCredential):
+ await _normalize_authenticator_totp_for_organization_or_raise(
+ data.credential,
+ organization_id=current_org.organization_id,
+ )
- try:
- updated_credential = await credential_service.update_credential(
- credential=existing_credential,
- data=data,
- )
- except SkyvernHttpException as e:
- detail = (
- f"Custom credential service returned {e.error_message}"
- if e.error_message
- else f"Custom credential service returned HTTP {e.status_code}"
+ await app.AGENT_FUNCTION.validate_credential_write(
+ organization_id=current_org.organization_id,
+ credential_type=data.credential_type,
+ credential=data.credential,
+ existing_credential=existing_credential,
)
- raise HTTPException(status_code=502, detail=detail)
- # Schedule background cleanup of old vault item if the item_id changed
- if old_item_id != updated_credential.item_id:
- background_tasks.add_task(
- credential_service.post_delete_credential_item,
- old_item_id,
- existing_credential.organization_id,
+ vault_type = existing_credential.vault_type or CredentialVaultType.BITWARDEN
+ credential_service = app.CREDENTIAL_VAULT_SERVICES.get(vault_type)
+ if not credential_service:
+ raise HTTPException(status_code=400, detail="Unsupported credential storage type")
+
+ old_item_id = existing_credential.item_id
+
+ if not requires_write_lock:
+ latest_credential = await app.DATABASE.credentials.get_credential(
+ credential_id=credential_id, organization_id=current_org.organization_id
+ )
+ if latest_credential is None or (
+ await _write_lock_decision_is_stale(
+ sampled_credential=existing_credential,
+ latest_credential=latest_credential,
+ )
+ ):
+ raise HTTPException(status_code=409, detail=CREDENTIAL_WRITE_CONFLICT_DETAIL)
+
+ write_request = await app.AGENT_FUNCTION.prepare_credential_update(
+ credential_service=credential_service,
+ existing_credential=existing_credential,
+ data=data,
)
- if updated_credential.vault_type == CredentialVaultType.BITWARDEN:
- background_tasks.add_task(fetch_credential_item_background, updated_credential.item_id)
+ try:
+ updated_credential = await credential_service.update_credential(
+ credential=existing_credential,
+ data=write_request,
+ )
+ except SkyvernHttpException as e:
+ detail = (
+ f"Custom credential service returned {e.error_message}"
+ if e.error_message
+ else f"Custom credential service returned HTTP {e.status_code}"
+ )
+ raise HTTPException(status_code=502, detail=detail)
+
+ if not requires_write_lock:
+ await _raise_if_superseded_by_concurrent_write(
+ credential_service=credential_service,
+ credential_id=credential_id,
+ organization_id=current_org.organization_id,
+ written_item_id=updated_credential.item_id,
+ replaced_item_id=old_item_id,
+ vault_type=vault_type,
+ )
+
+ if old_item_id != updated_credential.item_id:
+ background_tasks.add_task(
+ _delete_replaced_credential_item,
+ credential_service=credential_service,
+ organization_id=existing_credential.organization_id,
+ item_id=old_item_id,
+ vault_type=vault_type,
+ )
+
+ if updated_credential.vault_type == CredentialVaultType.BITWARDEN:
+ background_tasks.add_task(fetch_credential_item_background, updated_credential.item_id)
_clear_cached_totp_code_preview(organization_id=current_org.organization_id, credential_id=credential_id)
@@ -1903,28 +2062,55 @@ async def delete_credential(
if not credential:
raise HTTPException(status_code=404, detail=f"Credential not found, credential_id={credential_id}")
- vault_type = credential.vault_type or CredentialVaultType.BITWARDEN
- credential_service = app.CREDENTIAL_VAULT_SERVICES.get(vault_type)
- if not credential_service:
- raise HTTPException(status_code=400, detail="Unsupported credential storage type")
-
- try:
- await credential_service.delete_credential(credential)
- except SkyvernHttpException as e:
- detail = (
- f"Custom credential service returned {e.error_message}"
- if e.error_message
- else f"Custom credential service returned HTTP {e.status_code}"
+ requires_write_lock = await app.AGENT_FUNCTION.should_lock_credential_write(
+ credential=None,
+ existing_credential=credential,
+ )
+ write_guard: AbstractAsyncContextManager[None] = nullcontext()
+ if requires_write_lock:
+ write_guard = app.AGENT_FUNCTION.credential_write_lock(
+ organization_id=current_org.organization_id,
+ credential_id=credential_id,
)
- raise HTTPException(status_code=502, detail=detail)
- # Schedule background cleanup if the service implements it
- if vault_type != CredentialVaultType.CUSTOM:
- background_tasks.add_task(
- credential_service.post_delete_credential_item,
- credential.item_id,
- credential.organization_id,
+ async with write_guard:
+ latest_credential = await app.DATABASE.credentials.get_credential(
+ credential_id=credential_id, organization_id=current_org.organization_id
)
+ if not latest_credential:
+ raise HTTPException(status_code=404, detail=f"Credential not found, credential_id={credential_id}")
+ if not requires_write_lock and (
+ await _write_lock_decision_is_stale(
+ sampled_credential=credential,
+ latest_credential=latest_credential,
+ )
+ ):
+ raise HTTPException(status_code=409, detail=CREDENTIAL_WRITE_CONFLICT_DETAIL)
+ credential = latest_credential
+
+ vault_type = credential.vault_type or CredentialVaultType.BITWARDEN
+ credential_service = app.CREDENTIAL_VAULT_SERVICES.get(vault_type)
+ if not credential_service:
+ raise HTTPException(status_code=400, detail="Unsupported credential storage type")
+
+ try:
+ await credential_service.delete_credential(credential)
+ except SkyvernHttpException as e:
+ detail = (
+ f"Custom credential service returned {e.error_message}"
+ if e.error_message
+ else f"Custom credential service returned HTTP {e.status_code}"
+ )
+ raise HTTPException(status_code=502, detail=detail)
+
+ if vault_type != CredentialVaultType.CUSTOM:
+ background_tasks.add_task(
+ _delete_replaced_credential_item,
+ credential_service=credential_service,
+ organization_id=credential.organization_id,
+ item_id=credential.item_id,
+ vault_type=vault_type,
+ )
# Reap the credential's living browser profile so deleting a credential doesn't silently orphan
# its cookie-bearing archive (unbounded S3 growth). Kept if a live owner still references it. The
diff --git a/skyvern/forge/sdk/schemas/credentials.py b/skyvern/forge/sdk/schemas/credentials.py
index e395cb7e3..e9d28ae82 100644
--- a/skyvern/forge/sdk/schemas/credentials.py
+++ b/skyvern/forge/sdk/schemas/credentials.py
@@ -33,6 +33,7 @@ class TotpType(StrEnum):
AUTHENTICATOR = "authenticator"
EMAIL = "email"
TEXT = "text"
+ PASSKEY = "passkey"
NONE = "none"
@@ -125,6 +126,16 @@ class PasswordCredential(BaseModel):
description="Identifier (email or phone number) used to fetch TOTP codes",
examples=["[email protected]", "+14155550123"],
)
+ metadata: dict[str, str] | None = Field(
+ default=None,
+ description="Optional additional password credential metadata fields",
+ )
+
+ @model_validator(mode="after")
+ def normalize_empty_optional_fields(self) -> Self:
+ if self.metadata == {}:
+ self.metadata = None
+ return self
class NonEmptyPasswordCredential(PasswordCredential):
diff --git a/skyvern/forge/sdk/services/bitwarden.py b/skyvern/forge/sdk/services/bitwarden.py
index 52b1fa461..017cc09be 100644
--- a/skyvern/forge/sdk/services/bitwarden.py
+++ b/skyvern/forge/sdk/services/bitwarden.py
@@ -90,6 +90,32 @@ def _build_bitwarden_custom_fields(credential: CreditCardCredential) -> list[dic
]
+def _build_bitwarden_login_custom_fields(credential: PasswordCredential) -> list[dict[str, str | int | None]]:
+ return [
+ {
+ "name": f"metadata_{key}",
+ "value": value,
+ "type": BITWARDEN_CUSTOM_FIELD_TYPE_HIDDEN,
+ "linkedId": None,
+ }
+ for key, value in (credential.metadata or {}).items()
+ if key and value
+ ]
+
+
+def _password_metadata_from_bitwarden_item(item: dict) -> dict[str, str] | None:
+ metadata = {
+ name.removeprefix("metadata_"): value
+ for field in item.get("fields") or []
+ if isinstance((name := field.get("name")), str)
+ and name.startswith("metadata_")
+ and name != "metadata_"
+ and isinstance((value := field.get("value")), str)
+ and value
+ }
+ return metadata or None
+
+
def _extract_credit_card_extra_custom_field_values(item: dict) -> dict[str, str]:
values: dict[str, str] = {}
for field in item.get("fields") or []:
@@ -150,6 +176,7 @@ def get_list_response_item_from_bitwarden_item(item: dict) -> CredentialItem:
username=login["username"] or "",
password=login["password"] or "",
totp=totp,
+ metadata=_password_metadata_from_bitwarden_item(item),
),
name=item["name"],
credential_type=CredentialType.PASSWORD,
@@ -1088,6 +1115,7 @@ class BitwardenService:
item_template["login"] = login_template
item_template["collectionIds"] = [collection_id]
item_template["organizationId"] = bw_organization_id
+ item_template["fields"] = _build_bitwarden_login_custom_fields(credential)
response = await aiohttp_post(f"{BITWARDEN_SERVER_BASE_URL}/object/item", data=item_template, timeout=120)
if not response or response.get("success") is False:
@@ -1369,6 +1397,7 @@ class BitwardenService:
username=login_item["username"] or "",
password=login_item["password"] or "",
totp=login_item["totp"],
+ metadata=_password_metadata_from_bitwarden_item(response["data"]),
),
)
elif response["data"]["type"] == BitwardenItemType.CREDIT_CARD:
diff --git a/skyvern/forge/sdk/services/credential/azure_credential_vault_service.py b/skyvern/forge/sdk/services/credential/azure_credential_vault_service.py
index 59b4b3c61..e5a3e27eb 100644
--- a/skyvern/forge/sdk/services/credential/azure_credential_vault_service.py
+++ b/skyvern/forge/sdk/services/credential/azure_credential_vault_service.py
@@ -28,6 +28,7 @@ class AzureCredentialVaultService(CredentialVaultService):
password: str
username: str
totp: str | None = None
+ metadata: dict[str, str] | None = None
class _CreditCardCredentialDataImage(BaseModel):
type: Literal["credit_card"]
@@ -73,7 +74,12 @@ class AzureCredentialVaultService(CredentialVaultService):
async def update_credential(self, credential: Credential, data: CreateCredentialRequest) -> Credential:
credential_data = data.credential
- if data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
+ if data.credential_type == CredentialType.PASSWORD and isinstance(credential_data, PasswordCredential):
+ credential_data = await self._preserve_omitted_password_metadata(
+ credential=credential,
+ updated_credential=credential_data,
+ )
+ elif data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
credential_data = await self._preserve_omitted_credit_card_fields(
credential=credential,
updated_credential=credential_data,
@@ -119,7 +125,7 @@ class AzureCredentialVaultService(CredentialVaultService):
secret_value="",
)
- async def post_delete_credential_item(self, item_id: str, _organization_id: str | None = None) -> None:
+ async def post_delete_credential_item(self, item_id: str, _organization_id: str | None = None) -> bool:
"""
Background task to delete the credential item from Azure Key Vault.
This allows the API to respond quickly while the deletion happens asynchronously.
@@ -136,13 +142,15 @@ class AzureCredentialVaultService(CredentialVaultService):
item_id=item_id,
vault_name=self._vault_name,
)
- except Exception as e:
- LOG.exception(
+ return True
+ except Exception as exc:
+ LOG.warning(
"Failed to delete credential item from Azure Key Vault in background",
item_id=item_id,
vault_name=self._vault_name,
- error=str(e),
+ error_type=type(exc).__name__,
)
+ return False
async def get_credential_item(self, db_credential: Credential) -> CredentialItem:
secret_json_str = await self._client.get_secret(secret_name=db_credential.item_id, vault_name=self._vault_name)
@@ -158,6 +166,7 @@ class AzureCredentialVaultService(CredentialVaultService):
password=data.password,
totp=data.totp,
totp_type=db_credential.totp_type,
+ metadata=data.metadata,
),
name=db_credential.name,
credential_type=CredentialType.PASSWORD,
@@ -201,6 +210,7 @@ class AzureCredentialVaultService(CredentialVaultService):
username=credential.username,
password=credential.password,
totp=credential.totp,
+ metadata=credential.metadata,
)
elif isinstance(credential, CreditCardCredential):
data = AzureCredentialVaultService._CreditCardCredentialDataImage(
@@ -245,6 +255,7 @@ class AzureCredentialVaultService(CredentialVaultService):
username=credential.username,
password=credential.password,
totp=credential.totp,
+ metadata=credential.metadata,
)
elif isinstance(credential, CreditCardCredential):
data = AzureCredentialVaultService._CreditCardCredentialDataImage(
diff --git a/skyvern/forge/sdk/services/credential/bitwarden_credential_service.py b/skyvern/forge/sdk/services/credential/bitwarden_credential_service.py
index 0efc7ef84..62f654ead 100644
--- a/skyvern/forge/sdk/services/credential/bitwarden_credential_service.py
+++ b/skyvern/forge/sdk/services/credential/bitwarden_credential_service.py
@@ -9,6 +9,7 @@ from skyvern.forge.sdk.schemas.credentials import (
CredentialType,
CredentialVaultType,
CreditCardCredential,
+ PasswordCredential,
)
from skyvern.forge.sdk.services.bitwarden import BitwardenService
from skyvern.forge.sdk.services.credential.credential_vault_service import CredentialVaultService
@@ -57,7 +58,12 @@ class BitwardenCredentialVaultService(CredentialVaultService):
raise HTTPException(status_code=404, detail="Credential account not found. It might have been deleted.")
credential_data = data.credential
- if data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
+ if data.credential_type == CredentialType.PASSWORD and isinstance(credential_data, PasswordCredential):
+ credential_data = await self._preserve_omitted_password_metadata(
+ credential=credential,
+ updated_credential=credential_data,
+ )
+ elif data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
credential_data = await self._preserve_omitted_credit_card_fields(
credential=credential,
updated_credential=credential_data,
@@ -77,21 +83,18 @@ class BitwardenCredentialVaultService(CredentialVaultService):
data=data,
item_id=new_item_id,
)
- except Exception:
+ except BaseException:
LOG.warning(
- "DB update failed, attempting to clean up new Bitwarden vault item",
+ "DB update failed; reclaiming the new Bitwarden vault item",
organization_id=credential.organization_id,
new_item_id=new_item_id,
)
- try:
- await BitwardenService.delete_credential_item(new_item_id)
- except Exception as cleanup_error:
- LOG.error(
- "Failed to clean up orphaned Bitwarden vault item",
- organization_id=credential.organization_id,
- new_item_id=new_item_id,
- error=str(cleanup_error),
- )
+ await self._reclaim_orphaned_vault_item(
+ delete=lambda: BitwardenService.delete_credential_item(new_item_id),
+ organization_id=credential.organization_id,
+ item_id=new_item_id,
+ vault_type=CredentialVaultType.BITWARDEN,
+ )
raise
return updated_credential
@@ -109,20 +112,21 @@ class BitwardenCredentialVaultService(CredentialVaultService):
await app.DATABASE.credentials.delete_credential(credential.credential_id, credential.organization_id)
await BitwardenService.delete_credential_item(credential.item_id)
- async def post_delete_credential_item(self, item_id: str, organization_id: str | None = None) -> None:
+ async def post_delete_credential_item(self, item_id: str, organization_id: str | None = None) -> bool:
try:
await BitwardenService.delete_credential_item(item_id)
LOG.info(
"Successfully deleted credential item from Bitwarden in background",
item_id=item_id,
)
- except Exception as e:
+ return True
+ except Exception as exc:
LOG.warning(
"Failed to delete credential item from Bitwarden in background",
item_id=item_id,
- error=str(e),
- exc_info=True,
+ error_type=type(exc).__name__,
)
+ return False
async def get_credential_item(self, db_credential: Credential) -> CredentialItem:
return await BitwardenService.get_credential_item(db_credential.item_id)
diff --git a/skyvern/forge/sdk/services/credential/credential_vault_service.py b/skyvern/forge/sdk/services/credential/credential_vault_service.py
index 47e91ad16..066c1b85a 100644
--- a/skyvern/forge/sdk/services/credential/credential_vault_service.py
+++ b/skyvern/forge/sdk/services/credential/credential_vault_service.py
@@ -1,5 +1,8 @@
+import asyncio
from abc import ABC, abstractmethod
-from typing import Any
+from typing import Any, Awaitable, Callable
+
+import structlog
from skyvern.forge import app
from skyvern.forge.sdk.schemas.credentials import (
@@ -10,8 +13,11 @@ from skyvern.forge.sdk.schemas.credentials import (
CredentialVaultType,
CreditCardBillingAddress,
CreditCardCredential,
+ PasswordCredential,
)
+LOG = structlog.get_logger()
+
class CredentialVaultService(ABC):
"""Abstract interface for credential vault services.
@@ -32,16 +38,57 @@ class CredentialVaultService(ABC):
async def delete_credential(self, credential: Credential) -> None:
"""Delete a credential from the vault and database."""
- async def post_delete_credential_item(self, item_id: str, organization_id: str | None = None) -> None:
+ async def post_delete_credential_item(self, item_id: str, organization_id: str | None = None) -> bool:
"""
Optional hook for scheduling background cleanup tasks after credential deletion.
Default implementation does nothing. Override in subclasses as needed.
"""
+ return True
@abstractmethod
async def get_credential_item(self, db_credential: Credential) -> CredentialItem:
"""Retrieve the full credential data from the vault."""
+ async def _reclaim_orphaned_vault_item(
+ self,
+ *,
+ delete: Callable[[], Awaitable[None]],
+ organization_id: str,
+ item_id: str,
+ vault_type: CredentialVaultType,
+ ) -> None:
+ """Reclaim a just-created vault item after the DB repoint failed or was cancelled.
+
+ Runs under shield so a CancelledError (write-lease renewal cancelling the owner task)
+ cannot orphan a secret-bearing item between create and repoint. Falls back to durable
+ cleanup so the reaper reclaims the item if the inline delete also fails.
+ """
+
+ async def _run() -> None:
+ try:
+ await delete()
+ return
+ except Exception:
+ LOG.error(
+ "Inline vault-item cleanup failed after DB repoint failure; enqueuing durable cleanup",
+ organization_id=organization_id,
+ item_id=item_id,
+ vault_type=vault_type,
+ exc_info=True,
+ )
+ await app.AGENT_FUNCTION.on_credential_item_orphaned(
+ organization_id=organization_id,
+ item_id=item_id,
+ vault_type=vault_type,
+ )
+
+ reclaim = asyncio.ensure_future(_run())
+ while not reclaim.done():
+ try:
+ await asyncio.shield(reclaim)
+ except asyncio.CancelledError:
+ continue
+
async def _preserve_omitted_credit_card_fields(
self,
credential: Credential,
@@ -76,6 +123,20 @@ class CredentialVaultService(ABC):
return updated_credential.model_copy(update=preserved_fields)
+ async def _preserve_omitted_password_metadata(
+ self,
+ credential: Credential,
+ updated_credential: PasswordCredential,
+ ) -> PasswordCredential:
+ if "metadata" in updated_credential.model_fields_set:
+ return updated_credential
+
+ existing_item = await self.get_credential_item(credential)
+ if not isinstance(existing_item.credential, PasswordCredential):
+ return updated_credential
+
+ return updated_credential.model_copy(update={"metadata": existing_item.credential.metadata})
+
@staticmethod
def _preserve_omitted_billing_address_fields(
existing_address: CreditCardBillingAddress,
diff --git a/skyvern/forge/sdk/services/credential/custom_credential_vault_service.py b/skyvern/forge/sdk/services/credential/custom_credential_vault_service.py
index a6cbd1931..e4d2a02f6 100644
--- a/skyvern/forge/sdk/services/credential/custom_credential_vault_service.py
+++ b/skyvern/forge/sdk/services/credential/custom_credential_vault_service.py
@@ -1,6 +1,7 @@
import json
import structlog
+from pydantic import ValidationError
from skyvern.exceptions import SkyvernException
from skyvern.forge import app
@@ -13,8 +14,10 @@ from skyvern.forge.sdk.schemas.credentials import (
CredentialType,
CredentialVaultType,
CreditCardCredential,
+ PasswordCredential,
)
from skyvern.forge.sdk.services.credential.credential_vault_service import CredentialVaultService
+from skyvern.forge.sdk.services.credentials import safe_error_message
LOG = structlog.get_logger()
@@ -174,7 +177,14 @@ class CustomCredentialVaultService(CredentialVaultService):
try:
client = await self._get_client_for_organization(credential.organization_id)
credential_data = data.credential
- if data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
+ if data.credential_type == CredentialType.PASSWORD and isinstance(credential_data, PasswordCredential):
+ credential_data = await self._preserve_omitted_password_metadata(
+ credential=credential,
+ updated_credential=credential_data,
+ )
+ elif data.credential_type == CredentialType.CREDIT_CARD and isinstance(
+ credential_data, CreditCardCredential
+ ):
credential_data = await self._preserve_omitted_credit_card_fields(
credential=credential,
updated_credential=credential_data,
@@ -193,21 +203,18 @@ class CustomCredentialVaultService(CredentialVaultService):
data=data,
item_id=new_item_id,
)
- except Exception:
+ except BaseException:
LOG.warning(
- "DB update failed, attempting to clean up new external credential",
+ "DB update failed; reclaiming the new external credential",
organization_id=credential.organization_id,
new_item_id=new_item_id,
)
- try:
- await client.delete_credential(new_item_id)
- except Exception as cleanup_error:
- LOG.error(
- "Failed to clean up orphaned external credential",
- organization_id=credential.organization_id,
- new_item_id=new_item_id,
- error=str(cleanup_error),
- )
+ await self._reclaim_orphaned_vault_item(
+ delete=lambda: client.delete_credential(new_item_id),
+ organization_id=credential.organization_id,
+ item_id=new_item_id,
+ vault_type=CredentialVaultType.CUSTOM,
+ )
raise
LOG.info(
@@ -230,7 +237,7 @@ class CustomCredentialVaultService(CredentialVaultService):
)
raise
- async def post_delete_credential_item(self, item_id: str, organization_id: str | None = None) -> None:
+ async def post_delete_credential_item(self, item_id: str, organization_id: str | None = None) -> bool:
"""
Background task to delete the old credential item from the custom vault
after an update or delete operation.
@@ -242,7 +249,7 @@ class CustomCredentialVaultService(CredentialVaultService):
item_id=item_id,
organization_id=organization_id,
)
- return
+ return False
if self._client is not None:
client = self._client
@@ -255,14 +262,15 @@ class CustomCredentialVaultService(CredentialVaultService):
organization_id=organization_id,
item_id=item_id,
)
- except Exception as e:
+ return True
+ except Exception as exc:
LOG.warning(
"Failed to delete credential item from custom vault in background",
organization_id=organization_id,
item_id=item_id,
- error=str(e),
- exc_info=True,
+ error_type=type(exc).__name__,
)
+ return False
async def delete_credential(self, credential: Credential) -> None:
"""
@@ -347,7 +355,7 @@ class CustomCredentialVaultService(CredentialVaultService):
organization_id=db_credential.organization_id,
credential_id=db_credential.credential_id,
item_id=db_credential.item_id,
- error=str(e),
- exc_info=True,
+ error=safe_error_message(e),
+ exc_info=not isinstance(e, ValidationError),
)
raise
diff --git a/skyvern/forge/sdk/services/credential/gcp_credential_vault_service.py b/skyvern/forge/sdk/services/credential/gcp_credential_vault_service.py
index 9209a5818..385e80442 100644
--- a/skyvern/forge/sdk/services/credential/gcp_credential_vault_service.py
+++ b/skyvern/forge/sdk/services/credential/gcp_credential_vault_service.py
@@ -30,6 +30,7 @@ class GcpCredentialVaultService(CredentialVaultService):
password: str
username: str
totp: str | None = None
+ metadata: dict[str, str] | None = None
class _CreditCardCredentialDataImage(BaseModel):
type: Literal["credit_card"]
@@ -83,7 +84,12 @@ class GcpCredentialVaultService(CredentialVaultService):
async def update_credential(self, credential: Credential, data: CreateCredentialRequest) -> Credential:
credential_data = data.credential
- if data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
+ if data.credential_type == CredentialType.PASSWORD and isinstance(credential_data, PasswordCredential):
+ credential_data = await self._preserve_omitted_password_metadata(
+ credential=credential,
+ updated_credential=credential_data,
+ )
+ elif data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
credential_data = await self._preserve_omitted_credit_card_fields(
credential=credential,
updated_credential=credential_data,
@@ -128,6 +134,21 @@ class GcpCredentialVaultService(CredentialVaultService):
await self._client.delete_secret(secret_id=credential.item_id, project_id=self._project_id)
await app.DATABASE.credentials.delete_credential(credential.credential_id, credential.organization_id)
+ async def post_delete_credential_item(self, item_id: str, _organization_id: str | None = None) -> bool:
+ # The reaper drops the pending record only when this returns True, so inheriting the base default
+ # (return True without deleting) would orphan the Secret Manager secret; delete_secret swallows NotFound.
+ try:
+ await self._client.delete_secret(secret_id=item_id, project_id=self._project_id)
+ return True
+ except Exception:
+ LOG.warning(
+ "Failed to delete GCP Secret Manager credential item in background cleanup",
+ item_id=item_id,
+ project_id=self._project_id,
+ exc_info=True,
+ )
+ return False
+
async def get_credential_item(self, db_credential: Credential) -> CredentialItem:
secret_json_str = await self._client.get_secret(secret_id=db_credential.item_id, project_id=self._project_id)
if secret_json_str is None:
@@ -142,6 +163,7 @@ class GcpCredentialVaultService(CredentialVaultService):
password=data.password,
totp=data.totp,
totp_type=db_credential.totp_type,
+ metadata=data.metadata,
),
name=db_credential.name,
credential_type=CredentialType.PASSWORD,
@@ -181,6 +203,7 @@ class GcpCredentialVaultService(CredentialVaultService):
username=credential.username,
password=credential.password,
totp=credential.totp,
+ metadata=credential.metadata,
)
elif isinstance(credential, CreditCardCredential):
return GcpCredentialVaultService._CreditCardCredentialDataImage(
diff --git a/skyvern/forge/sdk/services/credential/skyvern_credential_vault_service.py b/skyvern/forge/sdk/services/credential/skyvern_credential_vault_service.py
index 37c8961c9..484b3aa63 100644
--- a/skyvern/forge/sdk/services/credential/skyvern_credential_vault_service.py
+++ b/skyvern/forge/sdk/services/credential/skyvern_credential_vault_service.py
@@ -1,3 +1,4 @@
+import asyncio
import json
import os
import re
@@ -18,6 +19,7 @@ from skyvern.forge.sdk.schemas.credentials import (
CredentialType,
CredentialVaultType,
CreditCardCredential,
+ PasswordCredential,
)
from skyvern.forge.sdk.services.credential.credential_vault_service import CredentialVaultService
@@ -49,13 +51,23 @@ class SkyvernCredentialVaultService(CredentialVaultService):
item_id=item_id,
vault_type=CredentialVaultType.SKYVERN,
)
- except Exception:
- self._delete_item_file(item_id)
+ except BaseException:
+ await self._reclaim_orphaned_vault_item(
+ delete=lambda: asyncio.to_thread(self._unlink_item_file, item_id),
+ organization_id=organization_id,
+ item_id=item_id,
+ vault_type=CredentialVaultType.SKYVERN,
+ )
raise
async def update_credential(self, credential: Credential, data: CreateCredentialRequest) -> Credential:
credential_data = data.credential
- if data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
+ if data.credential_type == CredentialType.PASSWORD and isinstance(credential_data, PasswordCredential):
+ credential_data = await self._preserve_omitted_password_metadata(
+ credential=credential,
+ updated_credential=credential_data,
+ )
+ elif data.credential_type == CredentialType.CREDIT_CARD and isinstance(credential_data, CreditCardCredential):
credential_data = await self._preserve_omitted_credit_card_fields(
credential=credential,
updated_credential=credential_data,
@@ -76,16 +88,21 @@ class SkyvernCredentialVaultService(CredentialVaultService):
data=data,
item_id=new_item_id,
)
- except Exception:
- self._delete_item_file(new_item_id)
+ except BaseException:
+ await self._reclaim_orphaned_vault_item(
+ delete=lambda: asyncio.to_thread(self._unlink_item_file, new_item_id),
+ organization_id=credential.organization_id,
+ item_id=new_item_id,
+ vault_type=CredentialVaultType.SKYVERN,
+ )
raise
async def delete_credential(self, credential: Credential) -> None:
await app.DATABASE.credentials.delete_credential(credential.credential_id, credential.organization_id)
self._delete_item_file(credential.item_id)
- async def post_delete_credential_item(self, item_id: str, organization_id: str | None = None) -> None:
- self._delete_item_file(item_id)
+ async def post_delete_credential_item(self, item_id: str, organization_id: str | None = None) -> bool:
+ return self._delete_item_file(item_id)
async def get_credential_item(self, db_credential: Credential) -> CredentialItem:
item_path = self._item_path(db_credential.item_id)
@@ -168,13 +185,18 @@ class SkyvernCredentialVaultService(CredentialVaultService):
raise HTTPException(status_code=400, detail="Invalid credential vault item ID")
return self._vault_dir() / f"{item_id}.bin"
- def _delete_item_file(self, item_id: str) -> None:
+ def _unlink_item_file(self, item_id: str) -> None:
+ self._item_path(item_id).unlink(missing_ok=True)
+
+ def _delete_item_file(self, item_id: str) -> bool:
try:
- self._item_path(item_id).unlink(missing_ok=True)
+ self._unlink_item_file(item_id)
+ return True
except HTTPException:
raise
except Exception:
LOG.warning("Failed to delete local credential vault item", item_id=item_id, exc_info=True)
+ return False
@staticmethod
def _generate_item_id() -> str:
diff --git a/skyvern/forge/sdk/services/credentials.py b/skyvern/forge/sdk/services/credentials.py
index 46b265ca9..5175e03c5 100644
--- a/skyvern/forge/sdk/services/credentials.py
+++ b/skyvern/forge/sdk/services/credentials.py
@@ -5,6 +5,7 @@ from urllib.parse import SplitResult, parse_qsl, unquote, urlencode, urlsplit, u
import pyotp
import structlog
+from pydantic import ValidationError
LOG = structlog.get_logger()
@@ -31,6 +32,12 @@ _ONEPASSWORD_CREDENTIAL_ERROR_PATTERN = re.compile(
)
+def safe_error_message(error: Exception) -> str:
+ if isinstance(error, ValidationError):
+ return "; ".join(f"{entry['loc']}: {entry['msg']}" for entry in error.errors())
+ return str(error)
+
+
def extract_onepassword_upstream_5xx_status(message: str) -> int | None:
"""Return the upstream 5xx HTTP status embedded in a 1Password SDK error message, if any.
diff --git a/skyvern/forge/sdk/workflow/context_manager.py b/skyvern/forge/sdk/workflow/context_manager.py
index b1df64920..b7c0ae96f 100644
--- a/skyvern/forge/sdk/workflow/context_manager.py
+++ b/skyvern/forge/sdk/workflow/context_manager.py
@@ -693,6 +693,11 @@ class WorkflowRunContext:
raise CredentialVaultNotConfiguredError(vault_type=vault_type.value, credential_id=credential_id)
credential_item = await credential_service.get_credential_item(db_credential)
+ credential_item = await app.AGENT_FUNCTION.process_registered_credential_item(
+ workflow_run_id=self.workflow_run_id,
+ db_credential=db_credential,
+ credential_item=credential_item,
+ )
credential = credential_item.credential
credential_totp_identifier = db_credential.totp_identifier or getattr(credential, "totp_identifier", None)
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index e0ef8141b..b4acddc8d 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -7321,6 +7321,26 @@ class WorkflowService:
steps_updated=steps_updated,
)
+ def _schedule_workflow_run_terminal_hooks(
+ self,
+ *,
+ workflow_run_id: str,
+ workflow_id: str,
+ organization_id: str,
+ status: WorkflowRunStatus,
+ ) -> None:
+ hook_coroutines = (
+ app.AGENT_FUNCTION.on_workflow_run_completed(
+ organization_id=organization_id,
+ workflow_id=workflow_id,
+ status=status,
+ ),
+ )
+ for hook_coroutine in hook_coroutines:
+ task = asyncio.create_task(hook_coroutine)
+ self._background_tasks.add(task)
+ task.add_done_callback(self._background_tasks.discard)
+
async def _update_workflow_run_status(
self,
workflow_run_id: str,
@@ -7409,15 +7429,12 @@ class WorkflowService:
workflow_schedule_id=workflow_run.workflow_schedule_id,
)
await self._apply_completion_run_tags_best_effort(workflow_run)
- run_completed_task = asyncio.create_task(
- app.AGENT_FUNCTION.on_workflow_run_completed(
- organization_id=workflow_run.organization_id,
- workflow_id=workflow_run.workflow_id,
- status=status,
- ),
+ self._schedule_workflow_run_terminal_hooks(
+ workflow_run_id=workflow_run_id,
+ organization_id=workflow_run.organization_id,
+ workflow_id=workflow_run.workflow_id,
+ status=status,
)
- self._background_tasks.add(run_completed_task)
- run_completed_task.add_done_callback(self._background_tasks.discard)
# Best-effort fire-and-forget write-through to task_runs table.
# Runs off the hot path so workflow status transitions stay fast.
bg = asyncio.create_task(
@@ -7804,6 +7821,13 @@ class WorkflowService:
)
await self._apply_completion_run_tags_best_effort(updated)
+ self._schedule_workflow_run_terminal_hooks(
+ workflow_run_id=workflow_run_id,
+ organization_id=updated.organization_id,
+ workflow_id=updated.workflow_id,
+ status=WorkflowRunStatus.canceled,
+ )
+
bg = asyncio.create_task(
self._sync_task_run_from_workflow_run(updated, workflow_run_id, WorkflowRunStatus.canceled),
)
@@ -8873,6 +8897,12 @@ class WorkflowService:
schedule_credential_fallback_retry: bool = True,
) -> None:
analytics.capture("skyvern-oss-agent-workflow-status", {"status": workflow_run.status})
+ # Tear down passkey material in the worker, after the finally block, while the context is still alive.
+ await app.AGENT_FUNCTION.on_workflow_run_terminal(
+ workflow_run_id=workflow_run.workflow_run_id,
+ organization_id=workflow_run.organization_id,
+ status=workflow_run.status,
+ )
if browser_cleanup_result is None:
browser_cleanup_result = await self._clean_up_workflow_browser(
workflow_run=workflow_run,
diff --git a/skyvern/services/error_detection_service.py b/skyvern/services/error_detection_service.py
index afe917ecc..28d3aef79 100644
--- a/skyvern/services/error_detection_service.py
+++ b/skyvern/services/error_detection_service.py
@@ -117,7 +117,11 @@ async def _detect_errors_from_page(
)
scraped_page = await browser_state.scrape_website(
url=page.url,
- cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(task=task, step=step),
+ cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(
+ task=task,
+ step=step,
+ engine_selection=browser_state.engine_selection,
+ ),
take_screenshots=True,
draw_boxes=False,
)
diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.py
index 723961272..4c296f27e 100644
--- a/skyvern/services/task_v2_service.py
+++ b/skyvern/services/task_v2_service.py
@@ -166,7 +166,12 @@ async def _summarize_max_steps_failure_reason(
if page is None:
return "Failed to get the current browser page", None
- screenshots = await SkyvernFrame.take_split_screenshots(page=page, url=str(task_v2.url), draw_boxes=False)
+ screenshots = await SkyvernFrame.take_split_screenshots(
+ page=page,
+ url=str(task_v2.url),
+ draw_boxes=False,
+ engine_selection=browser_state.engine_selection,
+ )
run_blocks = await app.DATABASE.observer.get_workflow_run_blocks(
workflow_run_id=task_v2.workflow_run_id,
@@ -895,7 +900,9 @@ async def run_task_v2_helper(
try:
scraped_page = await browser_state.scrape_website(
url=url,
- cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(),
+ cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(
+ engine_selection=browser_state.engine_selection
+ ),
scrape_exclude=app.scrape_exclude,
)
if page is None:
@@ -1208,7 +1215,9 @@ async def run_task_v2_helper(
)
completion_scraped_page = await browser_state.scrape_website(
url=url,
- cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(),
+ cleanup_element_tree=app.AGENT_FUNCTION.cleanup_element_tree_factory(
+ engine_selection=browser_state.engine_selection
+ ),
scrape_exclude=app.scrape_exclude,
)
completion_screenshots = completion_scraped_page.screenshots
@@ -2389,7 +2398,11 @@ async def _persist_completion_tab_screenshots(browser_state: BrowserState, thoug
await page.bring_to_front()
except Exception:
LOG.debug("Failed to bring tab to front before completion screenshot", exc_info=True)
- screenshots = await SkyvernFrame.take_split_screenshots(page=page, scroll=False)
+ screenshots = await SkyvernFrame.take_split_screenshots(
+ page=page,
+ scroll=False,
+ engine_selection=browser_state.engine_selection,
+ )
except Exception:
LOG.warning("Failed to capture completion screenshot for an open tab", exc_info=True)
continue
diff --git a/skyvern/webeye/browser_engine.py b/skyvern/webeye/browser_engine.py
index dba3affe3..7736ee5e6 100644
--- a/skyvern/webeye/browser_engine.py
+++ b/skyvern/webeye/browser_engine.py
@@ -24,7 +24,7 @@ from __future__ import annotations
import importlib.metadata
from dataclasses import dataclass, field
-from typing import TYPE_CHECKING, Awaitable, Callable
+from typing import TYPE_CHECKING, Awaitable, Callable, cast
import structlog
from playwright.async_api import Error as _PlaywrightError
@@ -49,6 +49,7 @@ LOG = structlog.get_logger()
BrowserDriverStarter = Callable[[], Awaitable[Playwright]]
BrowserEngineErrorLoader = Callable[[], "tuple[type[BaseException], type[BaseException]]"]
BrowserEngineRichErrorLoader = Callable[[], "BrowserEngineRichErrorTypes"]
+UNSET_SELECTION = cast("BrowserEngineSelection | None", object())
STOCK_ENGINE_NAME = "playwright"
RUSTWRIGHT_ENGINE_NAME = "rustwright"
diff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.py
index 8ee631b69..6fd1fedd1 100644
--- a/skyvern/webeye/real_browser_manager.py
+++ b/skyvern/webeye/real_browser_manager.py
@@ -75,6 +75,16 @@ async def _rebind_pbs_download_dir(
)
+async def _on_browser_state_acquired(
+ browser_state: BrowserState,
+ workflow_run_id: str | None,
+) -> BrowserState:
+ browser_context = browser_state.browser_context
+ if browser_context is not None:
+ await app.AGENT_FUNCTION.on_browser_context_acquired(browser_context, workflow_run_id)
+ return browser_state
+
+
def _merge_proxy_session_headers(
extra_http_headers: dict[str, str] | None,
proxy_session_id: str | None,
@@ -442,7 +452,7 @@ class RealBrowserManager(BrowserManager):
) -> BrowserState:
browser_state = self.get_for_task(task_id=task.task_id, workflow_run_id=task.workflow_run_id)
if browser_state is not None:
- return browser_state
+ return await _on_browser_state_acquired(browser_state, task.workflow_run_id)
if browser_session_id:
LOG.info(
@@ -524,7 +534,7 @@ class RealBrowserManager(BrowserManager):
task_id=task.task_id,
organization_id=task.organization_id,
)
- return browser_state
+ return await _on_browser_state_acquired(browser_state, task.workflow_run_id)
async def get_or_create_for_workflow_run(
self,
@@ -545,7 +555,7 @@ class RealBrowserManager(BrowserManager):
browser_state = self.get_for_workflow_run(workflow_run_id=workflow_run_id)
if browser_state:
LOG.debug("Returning cached browser state for workflow run", workflow_run_id=workflow_run_id)
- return browser_state
+ return await _on_browser_state_acquired(browser_state, workflow_run_id)
# When an explicit browser_session_id is provided (e.g. from a workflow
# trigger block), skip the parent workflow lookup so the child uses the
@@ -577,7 +587,7 @@ class RealBrowserManager(BrowserManager):
workflow_run_id=workflow_run_id,
organization_id=workflow_run.organization_id,
)
- return browser_state
+ return await _on_browser_state_acquired(browser_state, workflow_run_id)
LOG.warning(
"Inherited parent browser state has no working page; creating a fresh browser",
workflow_run_id=workflow_run_id,
@@ -718,7 +728,7 @@ class RealBrowserManager(BrowserManager):
workflow_run_id=workflow_run.workflow_run_id,
organization_id=workflow_run.organization_id,
)
- return browser_state
+ return await _on_browser_state_acquired(browser_state, workflow_run_id)
def get_for_workflow_run(
self, workflow_run_id: str, parent_workflow_run_id: str | None = None
@@ -1029,9 +1039,11 @@ class RealBrowserManager(BrowserManager):
browser_session_id: str | None = None,
organization_id: str | None = None,
) -> BrowserState:
+ context = skyvern_context.current()
+ workflow_run_id = context.workflow_run_id if context else None
browser_state = self.get_for_script(script_id=script_id)
if browser_state:
- return browser_state
+ return await _on_browser_state_acquired(browser_state, workflow_run_id)
if browser_session_id:
# Fail closed: look the session up under its real organization_id (release's symmetric key).
@@ -1067,7 +1079,7 @@ class RealBrowserManager(BrowserManager):
script_id=script_id,
)
- return browser_state
+ return await _on_browser_state_acquired(browser_state, workflow_run_id)
async def cleanup_for_script(
self,
diff --git a/skyvern/webeye/real_browser_state.py b/skyvern/webeye/real_browser_state.py
index c1e00fb8c..ec1220575 100644
--- a/skyvern/webeye/real_browser_state.py
+++ b/skyvern/webeye/real_browser_state.py
@@ -329,7 +329,7 @@ class RealBrowserState(BrowserState):
async def validate_browser_context(self, page: Page) -> bool:
# validate the content
try:
- skyvern_frame = await SkyvernFrame.create_instance(frame=page)
+ skyvern_frame = await SkyvernFrame.create_instance(frame=page, engine_selection=self.engine_selection)
html = await skyvern_frame.get_content()
except Exception:
LOG.error(
@@ -764,6 +764,7 @@ class RealBrowserState(BrowserState):
page=page,
file_path=file_path,
mode=ScreenshotMode.LITE,
+ engine_selection=self.engine_selection,
)
@traced(name="skyvern.browser.post_action_screenshot")
@@ -778,4 +779,5 @@ class RealBrowserState(BrowserState):
file_path=file_path,
mode=ScreenshotMode.LITE,
scrolling_number=scrolling_number,
+ engine_selection=self.engine_selection,
)
diff --git a/skyvern/webeye/scraper/scraper.py b/skyvern/webeye/scraper/scraper.py
index 10a120483..b2f36e388 100644
--- a/skyvern/webeye/scraper/scraper.py
+++ b/skyvern/webeye/scraper/scraper.py
@@ -476,21 +476,29 @@ async def scrape_web_unsafe(
frame_count=len(meaningful_frames),
)
- skyvern_frame = await SkyvernFrame.create_instance(page)
+ skyvern_frame = await SkyvernFrame.create_instance(page, engine_selection=browser_state.engine_selection)
await _wait_for_scrape_ready(skyvern_frame)
if wait_seconds > 0:
LOG.info(f"Waiting for {wait_seconds} seconds before scraping the website.", wait_seconds=wait_seconds)
await asyncio.sleep(wait_seconds)
- elements, element_tree, destinations = await get_interactable_element_tree(page, scrape_exclude, must_included_tags)
+ elements, element_tree, destinations = await get_interactable_element_tree(
+ page,
+ scrape_exclude,
+ must_included_tags,
+ engine_selection=browser_state.engine_selection,
+ )
empty_page_retry = False
if not elements and not support_empty_page:
LOG.warning("No elements found on the page, wait and retry")
await empty_page_retry_wait()
empty_page_retry = True
elements, element_tree, destinations = await get_interactable_element_tree(
- page, scrape_exclude, must_included_tags
+ page,
+ scrape_exclude,
+ must_included_tags,
+ engine_selection=browser_state.engine_selection,
)
element_tree = await cleanup_element_tree(page, url, copy.deepcopy(element_tree))
@@ -563,6 +571,7 @@ async def scrape_web_unsafe(
draw_boxes=draw_boxes,
max_number=max_screenshot_number,
scroll=effective_scroll,
+ engine_selection=browser_state.engine_selection,
)
_ss_span.set_attribute("screenshot_count", len(screenshots))
_ss_span.set_attribute("screenshot_bytes", sum(len(s) for s in screenshots))
@@ -585,7 +594,7 @@ async def scrape_web_unsafe(
html = ""
window_dimension = None
try:
- skyvern_frame = await SkyvernFrame.create_instance(frame=page)
+ skyvern_frame = await SkyvernFrame.create_instance(frame=page, engine_selection=browser_state.engine_selection)
html = await skyvern_frame.get_content()
if page.viewport_size:
window_dimension = Resolution(width=page.viewport_size["width"], height=page.viewport_size["height"])
@@ -690,6 +699,7 @@ async def add_frame_interactable_elements(
element_tree: list[dict],
destinations: dict[str, dict],
must_included_tags: list[str] | None = None,
+ engine_selection: "BrowserEngineSelection | None" = None,
) -> tuple[list[dict], list[dict]]:
"""
Add the interactable element of the frame to the elements and element_tree.
@@ -716,7 +726,7 @@ async def add_frame_interactable_elements(
return elements, element_tree
try:
- skyvern_frame = await SkyvernFrame.create_instance(frame)
+ skyvern_frame = await SkyvernFrame.create_instance(frame, engine_selection=engine_selection)
await _wait_for_scrape_ready(skyvern_frame)
frame_elements, frame_element_tree, frame_destinations = await skyvern_frame.build_tree_from_body(
@@ -740,6 +750,7 @@ async def get_interactable_element_tree(
page: Page,
scrape_exclude: ScrapeExcludeFunc | None = None,
must_included_tags: list[str] | None = None,
+ engine_selection: "BrowserEngineSelection | None" = None,
) -> tuple[list[dict], list[dict], dict[str, dict]]:
"""
Get the element tree of the page, including all the elements that are interactable.
@@ -748,7 +759,7 @@ async def get_interactable_element_tree(
destination-facts sidecar stripped out of them at the SkyvernFrame boundary.
"""
# main page index is 0
- skyvern_page = await SkyvernFrame.create_instance(page)
+ skyvern_page = await SkyvernFrame.create_instance(page, engine_selection=engine_selection)
elements, element_tree, destinations = await skyvern_page.build_tree_from_body(
frame_name="main.frame", frame_index=0, must_included_tags=must_included_tags
)
@@ -775,6 +786,7 @@ async def get_interactable_element_tree(
element_tree,
destinations,
must_included_tags,
+ engine_selection,
)
# Placeholder nodes stand in for frames the filter skipped but wants the LLM to
diff --git a/tests/unit/force_stub_app.py b/tests/unit/force_stub_app.py
index 16584dfcd..c7a095fd6 100644
--- a/tests/unit/force_stub_app.py
+++ b/tests/unit/force_stub_app.py
@@ -1,3 +1,4 @@
+from contextlib import nullcontext
from unittest.mock import AsyncMock, MagicMock
from skyvern.forge import set_force_app_instance
@@ -41,6 +42,13 @@ def create_forge_stub_app() -> ForgeApp:
# Sync method returning a key or None — _LazyNamespace would auto-mock it as a truthy
# AsyncMock and hijack the TextPromptBlock llm_key. Match the OSS no-op.
fake_app_module.AGENT_FUNCTION.get_fallback_llm_key = MagicMock(return_value=None)
+ # Credential write-lock gating — _LazyNamespace would auto-mock these as truthy AsyncMocks,
+ # forcing the update/delete credential routes down the lock path and handing `async with` a
+ # coroutine instead of a context manager. Match the real OSS base no-ops (unlocked path).
+ fake_app_module.AGENT_FUNCTION.should_lock_credential_write = AsyncMock(return_value=False)
+ fake_app_module.AGENT_FUNCTION.credential_write_lock = MagicMock(return_value=nullcontext())
+ fake_app_module.AGENT_FUNCTION.validate_credential_write = AsyncMock(return_value=None)
+ fake_app_module.AGENT_FUNCTION.prepare_credential_update = AsyncMock(side_effect=lambda **kwargs: kwargs["data"])
# Grid-collection seam — _LazyNamespace would auto-mock this as a truthy AsyncMock whose
# awaited value is a non-None MagicMock, poisoning the extract-information prompt/cache key.
# Match the real OSS base no-op (None → no grid rows injected).
diff --git a/tests/unit/forge/test_agent_function_credential_write.py b/tests/unit/forge/test_agent_function_credential_write.py
new file mode 100644
index 000000000..2c58c4bb2
--- /dev/null
+++ b/tests/unit/forge/test_agent_function_credential_write.py
@@ -0,0 +1,24 @@
+import pytest
+
+from skyvern.forge.agent_functions import AgentFunction
+from skyvern.forge.sdk.schemas.credentials import CredentialType, SecretCredential
+
+
+async def test_default_credential_write_validation_is_a_noop() -> None:
+ result = await AgentFunction().validate_credential_write(
+ organization_id="org_test",
+ credential_type=CredentialType.SECRET,
+ credential=SecretCredential(secret_value="opaque-value"),
+ )
+
+ assert result is None
+
+
+async def test_default_credential_write_lock_is_a_noop() -> None:
+ async with AgentFunction().credential_write_lock(
+ organization_id="org_test",
+ credential_id="cred_test",
+ ):
+ pass
diff --git a/tests/unit/forge/test_svg_convert_cache_resilience.py b/tests/unit/forge/test_svg_convert_cache_resilience.py
index b3d8f7e49..984213418 100644
--- a/tests/unit/forge/test_svg_convert_cache_resilience.py
+++ b/tests/unit/forge/test_svg_convert_cache_resilience.py
@@ -5,7 +5,7 @@ from collections.abc import Generator
from datetime import timedelta
from types import SimpleNamespace
from typing import Any
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -98,6 +98,8 @@ class _FakeFrame:
class _FakeSkyvernFrame:
+ engine_selection = None
+
def get_frame(self) -> _FakeFrame:
return _FakeFrame()
@@ -105,6 +107,51 @@ class _FakeSkyvernFrame:
return None, False
[email protected]("selection", [MagicMock(), None])
+async def test_taskless_cleanup_threads_explicit_running_driver_selection(selection: object | None) -> None:
+ frame = MagicMock(url="https://example.com")
+
+ with (
+ patch.object(agent_functions, "_resolve_engine_selection") as resolve,
+ patch.object(agent_functions.settings, "SVG_MAX_PARSING_ELEMENT_CNT", 3000),
+ patch.object(agent_functions.settings, "ENABLE_CSS_SVG_PARSING", False),
+ patch.object(agent_functions, "app", SimpleNamespace(SVG_CSS_CONVERTER_LLM_API_HANDLER=None)),
+ patch.object(
+ agent_functions.SkyvernFrame, "create_instance", new=AsyncMock(return_value=MagicMock())
+ ) as create,
+ ):
+ cleanup = agent_functions.AgentFunction().cleanup_element_tree_factory(engine_selection=selection)
+ await cleanup(frame, frame.url, [])
+
+ resolve.assert_not_called()
+ create.assert_awaited_once_with(frame=frame, engine_selection=selection)
+
+
+async def test_cleanup_without_selection_resolves_once_and_reuses_identity() -> None:
+ selection = MagicMock()
+ task = MagicMock()
+ frame = MagicMock(url="https://example.com")
+
+ with (
+ patch.object(agent_functions, "_resolve_engine_selection", return_value=selection) as resolve,
+ patch.object(agent_functions.settings, "SVG_MAX_PARSING_ELEMENT_CNT", 3000),
+ patch.object(agent_functions.settings, "ENABLE_CSS_SVG_PARSING", False),
+ patch.object(agent_functions, "app", SimpleNamespace(SVG_CSS_CONVERTER_LLM_API_HANDLER=None)),
+ patch.object(
+ agent_functions.SkyvernFrame, "create_instance", new=AsyncMock(return_value=MagicMock())
+ ) as create,
+ ):
+ cleanup = agent_functions.AgentFunction().cleanup_element_tree_factory(task=task)
+ await cleanup(frame, frame.url, [])
+ await cleanup(frame, frame.url, [])
+
+ resolve.assert_called_once_with(task)
+ assert create.await_count == 2
+ assert all(call.kwargs["engine_selection"] is selection for call in create.await_args_list)
+
+
def test_svg_local_invalid_shape_cache_uses_short_ttl(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(agent_functions.time, "monotonic", lambda: 100.0)
@@ -401,20 +448,21 @@ async def test_svg_eligibility_taskless_path_stays_manager_free(monkeypatch: pyt
@pytest.mark.asyncio
-async def test_svg_eligibility_resolves_pinned_selection_when_task_present(monkeypatch: pytest.MonkeyPatch) -> None:
+async def test_svg_eligibility_uses_frame_selection_when_task_present(monkeypatch: pytest.MonkeyPatch) -> None:
captured: list[Any] = []
selection = SimpleNamespace()
- browser_manager = SimpleNamespace(
- get_for_task=lambda task_id, workflow_run_id: SimpleNamespace(engine_selection=selection)
- )
+ browser_manager = SimpleNamespace(get_for_task=MagicMock())
monkeypatch.setattr(agent_functions, "app", SimpleNamespace(BROWSER_MANAGER=browser_manager))
monkeypatch.setattr(agent_functions, "SkyvernElement", _capturing_skyvern_element(captured))
task = SimpleNamespace(task_id="tsk_1", workflow_run_id="wr_1")
+ skyvern_frame = _FakeSkyvernFrame()
+ skyvern_frame.engine_selection = selection
- eligible = await agent_functions._check_svg_eligibility(_FakeSkyvernFrame(), _svg_element(), task=task) # type: ignore[arg-type]
+ eligible = await agent_functions._check_svg_eligibility(skyvern_frame, _svg_element(), task=task) # type: ignore[arg-type]
assert eligible is True
assert captured == [selection]
+ browser_manager.get_for_task.assert_not_called()
@pytest.mark.asyncio
@@ -437,16 +485,14 @@ async def test_css_shape_convert_taskless_path_stays_manager_free(monkeypatch: p
@pytest.mark.asyncio
-async def test_css_shape_convert_resolves_pinned_selection_when_task_present(monkeypatch: pytest.MonkeyPatch) -> None:
+async def test_css_shape_convert_uses_frame_selection_when_task_present(monkeypatch: pytest.MonkeyPatch) -> None:
captured: list[Any] = []
selection = SimpleNamespace()
async def handler(**kwargs: Any) -> dict[str, Any]:
return {"shape": "calendar icon", "recognized": True}
- browser_manager = SimpleNamespace(
- get_for_task=lambda task_id, workflow_run_id: SimpleNamespace(engine_selection=selection)
- )
+ browser_manager = SimpleNamespace(get_for_task=MagicMock())
monkeypatch.setattr(
agent_functions,
"app",
@@ -456,7 +502,10 @@ async def test_css_shape_convert_resolves_pinned_selection_when_task_present(mon
)
monkeypatch.setattr(agent_functions, "SkyvernElement", _capturing_skyvern_element(captured))
task = SimpleNamespace(task_id="tsk_1", workflow_run_id="wr_1")
+ skyvern_frame = _FakeSkyvernFrame()
+ skyvern_frame.engine_selection = selection
- await agent_functions._convert_css_shape_to_string(_FakeSkyvernFrame(), _css_shape_element(), task=task) # type: ignore[arg-type]
+ await agent_functions._convert_css_shape_to_string(skyvern_frame, _css_shape_element(), task=task) # type: ignore[arg-type]
assert captured == [selection]
+ browser_manager.get_for_task.assert_not_called()
diff --git a/tests/unit/test_azure_credential_vault_service.py b/tests/unit/test_azure_credential_vault_service.py
new file mode 100644
index 000000000..6fdee3aa0
--- /dev/null
+++ b/tests/unit/test_azure_credential_vault_service.py
@@ -0,0 +1,38 @@
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from skyvern.forge.sdk.schemas.credentials import PasswordCredential, TotpType
+from skyvern.forge.sdk.services.credential.azure_credential_vault_service import AzureCredentialVaultService
+
+_PASSWORD_METADATA = {"tenant": "north"}
+
+
+async def test_azure_password_data_image_round_trips_metadata_and_loads_legacy() -> None:
+ stored_value = ""
+
+ async def store_secret(*, secret_value: str, **_: str) -> str:
+ nonlocal stored_value
+ stored_value = secret_value
+ return "secret-id"
+
+ client = AsyncMock()
+ client.create_or_update_secret = AsyncMock(side_effect=store_secret)
+ client.get_secret = AsyncMock(side_effect=lambda **_: stored_value)
+ service = AzureCredentialVaultService(client=client, vault_name="vault")
+ item_id = await service._create_azure_secret_item(
+ organization_id="org_test",
+ credential=PasswordCredential(username="[email protected]", password="pw", metadata=_PASSWORD_METADATA),
+ )
+ item = await service.get_credential_item(SimpleNamespace(item_id=item_id, totp_type=TotpType.NONE, name="Login"))
+
+ assert isinstance(item.credential, PasswordCredential)
+ assert item.credential.metadata == _PASSWORD_METADATA
+ stored_value = '{"type":"password","username":"user","password":"pw"}'
+ legacy_item = await service.get_credential_item(
+ SimpleNamespace(item_id="secret-id", totp_type=TotpType.NONE, name="Legacy Login")
+ )
+ assert isinstance(legacy_item.credential, PasswordCredential)
+ assert legacy_item.credential.metadata is None
diff --git a/tests/unit/test_bitwarden_credential_vault_service.py b/tests/unit/test_bitwarden_credential_vault_service.py
new file mode 100644
index 000000000..3dfed77a2
--- /dev/null
+++ b/tests/unit/test_bitwarden_credential_vault_service.py
@@ -0,0 +1,132 @@
+import asyncio
+from datetime import datetime
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from skyvern.forge import app
+from skyvern.forge.sdk.schemas.credentials import (
+ CreateCredentialRequest,
+ Credential,
+ CredentialType,
+ CredentialVaultType,
+ NonEmptyPasswordCredential,
+)
+from skyvern.forge.sdk.services.credential import bitwarden_credential_service as service_module
+from skyvern.forge.sdk.services.credential.bitwarden_credential_service import BitwardenCredentialVaultService
+
+
+def _credential() -> Credential:
+ return Credential(
+ credential_id="cred_test",
+ organization_id="org_test",
+ name="Login",
+ vault_type=CredentialVaultType.BITWARDEN,
+ item_id="item_old",
+ credential_type=CredentialType.PASSWORD,
+ username="user_test",
+ totp_type="none",
+ totp_identifier=None,
+ card_last4=None,
+ card_brand=None,
+ secret_label=None,
+ browser_profile_id=None,
+ tested_url=None,
+ user_context=None,
+ save_browser_session_intent=False,
+ folder_id=None,
+ created_at=datetime(2026, 1, 1),
+ modified_at=datetime(2026, 1, 1),
+ deleted_at=None,
+ )
+
+
+def _password_request() -> CreateCredentialRequest:
+ return CreateCredentialRequest(
+ name="Login Updated",
+ credential_type=CredentialType.PASSWORD,
+ credential=NonEmptyPasswordCredential(
+ username="user_test",
+ password="secret_test",
+ metadata={"key": "value"},
+ ),
+ )
+
+
+def _prepare_update(
+ monkeypatch: pytest.MonkeyPatch,
+ failure: BaseException,
+ *,
+ delete_failure: Exception | None = None,
+) -> tuple[BitwardenCredentialVaultService, AsyncMock]:
+ service = BitwardenCredentialVaultService()
+ monkeypatch.setattr(
+ app.DATABASE.credentials,
+ "get_organization_bitwarden_collection",
+ AsyncMock(return_value=SimpleNamespace(collection_id="collection_test")),
+ )
+ monkeypatch.setattr(
+ service_module.BitwardenService,
+ "create_credential_item",
+ AsyncMock(return_value="item_new"),
+ )
+ delete_item = AsyncMock(side_effect=delete_failure)
+ monkeypatch.setattr(service_module.BitwardenService, "delete_credential_item", delete_item)
+ monkeypatch.setattr(service, "_update_db_credential", AsyncMock(side_effect=failure))
+ return service, delete_item
+
+
+async def test_post_delete_credential_item_returns_provider_failure_status(monkeypatch: pytest.MonkeyPatch) -> None:
+ delete_item = AsyncMock(side_effect=RuntimeError("vault unavailable"))
+ monkeypatch.setattr(service_module.BitwardenService, "delete_credential_item", delete_item)
+
+ result = await BitwardenCredentialVaultService().post_delete_credential_item("item_old", "org_1")
+
+ assert result is False
+ delete_item.assert_awaited_once_with("item_old")
+
+
+ ("failure_type", "message"),
+ [
+ pytest.param(RuntimeError, "database unavailable", id="exception"),
+ pytest.param(asyncio.CancelledError, "repoint cancelled", id="cancelled"),
+ ],
+)
+async def test_update_credential_reclaims_new_item_when_repoint_fails(
+ monkeypatch: pytest.MonkeyPatch,
+ failure_type: type[BaseException],
+ message: str,
+) -> None:
+ service, delete_item = _prepare_update(monkeypatch, failure_type(message))
+
+ with pytest.raises(failure_type):
+ await service.update_credential(_credential(), _password_request())
+
+ delete_item.assert_awaited_with("item_new")
+
+
+async def test_update_credential_enqueues_orphan_when_cancelled_inline_cleanup_fails(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ service, delete_item = _prepare_update(
+ monkeypatch,
+ asyncio.CancelledError(),
+ delete_failure=RuntimeError("vault unavailable"),
+ )
+ orphaned = AsyncMock()
+ monkeypatch.setattr(app.AGENT_FUNCTION, "on_credential_item_orphaned", orphaned)
+
+ with pytest.raises(asyncio.CancelledError):
+ await service.update_credential(_credential(), _password_request())
+
+ delete_item.assert_awaited_with("item_new")
+ orphaned.assert_awaited_with(
+ organization_id="org_test",
+ item_id="item_new",
+ vault_type=CredentialVaultType.BITWARDEN,
+ )
diff --git a/tests/unit/test_bitwarden_login.py b/tests/unit/test_bitwarden_login.py
index b0f179371..ba4bfc160 100644
--- a/tests/unit/test_bitwarden_login.py
+++ b/tests/unit/test_bitwarden_login.py
@@ -1,6 +1,15 @@
+from unittest.mock import AsyncMock
+
import pytest
-from skyvern.forge.sdk.services.bitwarden import BitwardenService, RunCommandResult
+from skyvern.forge.sdk.schemas.credentials import PasswordCredential
+from skyvern.forge.sdk.services import bitwarden as bitwarden_module
+from skyvern.forge.sdk.services.bitwarden import (
+ BITWARDEN_CUSTOM_FIELD_TYPE_HIDDEN,
+ BitwardenService,
+ RunCommandResult,
+ get_list_response_item_from_bitwarden_item,
+)
@pytest.mark.asyncio
@@ -17,3 +26,46 @@ async def test_login_ignores_data_file_creation_notice_on_stderr(
monkeypatch.setattr(BitwardenService, "run_command", fake_run_command)
await BitwardenService.login("client-id", "client-secret", master_password="master-password")
+
+
+async def test_server_login_item_round_trips_metadata(monkeypatch: pytest.MonkeyPatch) -> None:
+ metadata = {"tenant": "north", "account_id": "acct_123"}
+ stored_item: dict = {}
+ get_json = AsyncMock(
+ side_effect=[
+ {"data": {"template": {}}},
+ {"data": {"template": {}}},
+ {"success": True, "data": stored_item},
+ ]
+ )
+ post = AsyncMock(return_value={"success": True, "data": {"id": "item-1"}})
+ monkeypatch.setattr(bitwarden_module, "aiohttp_get_json", get_json)
+ monkeypatch.setattr(bitwarden_module, "aiohttp_post", post)
+
+ item_id = await BitwardenService._create_login_item_using_server(
+ bw_organization_id="bw-org",
+ collection_id="collection-1",
+ name="Login",
+ credential=PasswordCredential(username="[email protected]", password="pw", totp="", metadata=metadata),
+ )
+ stored_item.update(post.await_args.kwargs["data"], id=item_id)
+ listed_item = get_list_response_item_from_bitwarden_item(stored_item)
+ fetched_item = await BitwardenService._get_credential_item_by_id_using_server(item_id)
+
+ assert stored_item["fields"] == [
+ {
+ "name": "metadata_tenant",
+ "value": "north",
+ "type": BITWARDEN_CUSTOM_FIELD_TYPE_HIDDEN,
+ "linkedId": None,
+ },
+ {
+ "name": "metadata_account_id",
+ "value": "acct_123",
+ "type": BITWARDEN_CUSTOM_FIELD_TYPE_HIDDEN,
+ "linkedId": None,
+ },
+ ]
+ assert listed_item.credential.metadata == metadata
+ assert fetched_item.credential.metadata == metadata
diff --git a/tests/unit/test_copilot_agent_blocker_renderer.py b/tests/unit/test_copilot_agent_blocker_renderer.py
index 5fe2a9274..93945c1eb 100644
--- a/tests/unit/test_copilot_agent_blocker_renderer.py
+++ b/tests/unit/test_copilot_agent_blocker_renderer.py
@@ -1008,3 +1008,18 @@ def test_reconcile_turn_end_keeps_cancelled_result_and_expires_grant() -> None:
assert result is original
assert ctx.turn_halt is None
assert ctx.output_contract_actuation_by_signature["sig_a"] == OutputContractAdvisoryState.EXPIRED
+
+
+def test_unapproved_credential_reference_asks_without_naming_the_credential_inventory() -> None:
+ """`discovered_credentials` holds everything `list_credentials` returned, not page matches,
+ so the reply must not present it as a match set."""
+ ctx = _ctx()
+ ctx.request_policy = RequestPolicy(
+ discovered_credentials=[SimpleNamespace(credential_id="cred_unrelated", name="hr portal", tested_url=None)]
+ )
+
+ result = _blocked_result(ctx, OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE)
+
+ assert "confirm which saved credential" in result.user_response
+ assert "cred_unrelated" not in result.user_response
+ assert "hr portal" not in result.user_response
diff --git a/tests/unit/test_copilot_context.py b/tests/unit/test_copilot_context.py
index 2f4c3a8d5..a649fb669 100644
--- a/tests/unit/test_copilot_context.py
+++ b/tests/unit/test_copilot_context.py
@@ -734,7 +734,11 @@ class TestCopilotContext:
def _policy_ctx(resolved: list[SimpleNamespace], credential_input_kind: str = "credential_name") -> SimpleNamespace:
return SimpleNamespace(
- request_policy=SimpleNamespace(resolved_credentials=resolved, credential_input_kind=credential_input_kind)
+ request_policy=SimpleNamespace(
+ resolved_credentials=resolved,
+ credential_input_kind=credential_input_kind,
+ live_page_admitted_urls={},
+ )
)
diff --git a/tests/unit/test_copilot_fill_credential_field.py b/tests/unit/test_copilot_fill_credential_field.py
index e9e988239..b82c88f92 100644
--- a/tests/unit/test_copilot_fill_credential_field.py
+++ b/tests/unit/test_copilot_fill_credential_field.py
@@ -11,7 +11,7 @@ import json
from contextlib import asynccontextmanager
from types import SimpleNamespace
from typing import Any, AsyncIterator
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, patch
import pytest
@@ -22,11 +22,12 @@ from skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy
from skyvern.forge.sdk.copilot.request_policy import RequestPolicy
from skyvern.forge.sdk.copilot.tools import credential_fill as credential_fill_module
from skyvern.forge.sdk.copilot.tools import scouting as scouting_module
-from skyvern.forge.sdk.schemas.credentials import CredentialVaultType, PasswordCredential, TotpType
+from skyvern.forge.sdk.schemas.credentials import CredentialType, CredentialVaultType, PasswordCredential, TotpType
_FAKE_PASSWORD = "fake-test-password-7x9"
_FAKE_USERNAME = "[email protected]"
_FAKE_TOTP_SEED = "JBSWY3DPEHPK3PXP"
+_FIXTURE_LOGIN_URL = "https://authenticationtest.com/simpleFormAuth/"
def _resolved_credential(credential_id: str = "cred_123") -> SimpleNamespace:
@@ -56,6 +57,7 @@ def _ctx(**overrides: Any) -> SimpleNamespace:
discovery_mcp_server=None,
secret_scrub_values=[],
scouted_credential_field_inventory_by_credential_id={},
+ org_credentials_for_turn=None,
)
for key, value in overrides.items():
setattr(ns, key, value)
@@ -65,21 +67,21 @@ def _ctx(**overrides: Any) -> SimpleNamespace:
class TestCredentialFillPolicyGate:
def test_rejects_outside_code_only_mode(self) -> None:
ctx = _ctx(block_authoring_policy=BlockAuthoringPolicy.STANDARD)
- error = tools_module._credential_fill_policy_error(ctx, "cred_123")
+ error = tools_module._credential_fill_prerequisite_error(ctx, "cred_123")
assert error is not None
assert "login" in error
def test_rejects_without_request_policy(self) -> None:
ctx = _ctx(request_policy=None)
- assert tools_module._credential_fill_policy_error(ctx, "cred_123") is not None
+ assert tools_module._credential_fill_prerequisite_error(ctx, "cred_123") is not None
def test_rejects_when_run_blocks_not_allowed(self) -> None:
ctx = _ctx(request_policy=_policy(allow_run_blocks=False))
- assert tools_module._credential_fill_policy_error(ctx, "cred_123") is not None
+ assert tools_module._credential_fill_prerequisite_error(ctx, "cred_123") is not None
def test_rejects_credential_outside_resolved_set(self) -> None:
ctx = _ctx()
- error = tools_module._credential_fill_policy_error(ctx, "cred_999")
+ error = tools_module._credential_fill_authority_error(ctx, "cred_999")
assert error is not None
assert "cred_999" in error
assert "resolved" in error
@@ -88,11 +90,11 @@ class TestCredentialFillPolicyGate:
policy = _policy()
policy.discovered_credentials = [_resolved_credential("cred_discovered")]
ctx = _ctx(request_policy=policy)
- assert tools_module._credential_fill_policy_error(ctx, "cred_discovered") is not None
+ assert tools_module._credential_fill_authority_error(ctx, "cred_discovered") is not None
def test_allows_resolved_credential_in_code_only_mode(self) -> None:
ctx = _ctx()
- assert tools_module._credential_fill_policy_error(ctx, "cred_123") is None
+ assert tools_module._credential_fill_authority_error(ctx, "cred_123") is None
class TestResolveCredentialFillValue:
@@ -305,7 +307,8 @@ class TestResolveCredentialFillValue:
class _FakePage:
- def __init__(self, fill_error: Exception | None = None) -> None:
+ def __init__(self, fill_error: Exception | None = None, url: str = _FIXTURE_LOGIN_URL) -> None:
+ self.url = url
self.fill_calls: list[tuple[Any, ...]] = []
self.fill_kwargs: list[dict[str, Any]] = []
self._fill_error = fill_error
@@ -491,3 +494,198 @@ class TestToolRegistration:
def test_tool_is_phase_gated_as_browser_primitive(self) -> None:
assert "fill_credential_field" in _BROWSER_PRIMITIVE_TOOLS
+
+
+def _org_credential(
+ credential_id: str,
+ name: str,
+ tested_url: str | None,
+ credential_type: CredentialType = CredentialType.PASSWORD,
+) -> SimpleNamespace:
+ return SimpleNamespace(
+ credential_id=credential_id, name=name, tested_url=tested_url, credential_type=credential_type
+ )
+
+
+class TestCredentialFillLivePageAdmission:
+ """The gate consults the login page the scout reached before refusing.
+
+ Every case starts from an empty resolved set — the state a prompt that never mentioned a
+ login leaves behind once the wall turns up mid-turn.
+ """
+
+ async def _gate(
+ self,
+ *,
+ credential_id: str,
+ page_url: str | None,
+ org_credentials: list[SimpleNamespace],
+ policy: RequestPolicy | None = None,
+ block_authoring_policy: BlockAuthoringPolicy = BlockAuthoringPolicy.CODE_ONLY_BROWSER,
+ ) -> tuple[str | None, RequestPolicy, AsyncMock]:
+ policy = policy if policy is not None else RequestPolicy()
+ ctx = _ctx(request_policy=policy, block_authoring_policy=block_authoring_policy)
+ load_mock = AsyncMock(return_value=org_credentials)
+ with (
+ patch("skyvern.forge.app.DATABASE.credentials.get_credentials", new=load_mock),
+ patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=page_url)),
+ ):
+ error, _ = await credential_fill_module._credential_fill_gate_error(ctx, credential_id)
+ return error, policy, load_mock
+
+ @pytest.mark.asyncio
+ async def test_page_matched_credential_passes_the_gate(self) -> None:
+ error, policy, _ = await self._gate(
+ credential_id="cred_analytics",
+ page_url="https://analytics.example.com/login?next=%2Fweb",
+ org_credentials=[_org_credential("cred_analytics", "analytics", "https://analytics.example.com/login")],
+ )
+
+ assert error is None
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_analytics"]
+
+ @pytest.mark.asyncio
+ async def test_gate_still_refuses_a_credential_the_page_does_not_vouch_for(self) -> None:
+ error, policy, _ = await self._gate(
+ credential_id="cred_unrelated",
+ page_url="https://analytics.example.com/login",
+ org_credentials=[_org_credential("cred_unrelated", "unrelated", "https://billing.example.com/login")],
+ )
+
+ assert error is not None
+ assert "cred_unrelated" in error
+ assert policy.resolved_credentials == []
+
+ @pytest.mark.asyncio
+ async def test_prerequisite_failures_never_consult_the_page(self) -> None:
+ error, _, load_mock = await self._gate(
+ credential_id="cred_analytics",
+ page_url="https://analytics.example.com/login",
+ org_credentials=[_org_credential("cred_analytics", "analytics", "https://analytics.example.com/login")],
+ block_authoring_policy=BlockAuthoringPolicy.STANDARD,
+ )
+
+ assert error is not None
+ load_mock.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_a_turn_without_run_authority_is_not_reopened_by_the_page(self) -> None:
+ error, policy, load_mock = await self._gate(
+ credential_id="cred_analytics",
+ page_url="https://analytics.example.com/login",
+ org_credentials=[_org_credential("cred_analytics", "analytics", "https://analytics.example.com/login")],
+ policy=RequestPolicy(allow_run_blocks=False),
+ )
+
+ assert error is not None
+ assert policy.resolved_credentials == []
+ load_mock.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_already_resolved_credential_short_circuits_without_a_page_read(self) -> None:
+ error, _, load_mock = await self._gate(
+ credential_id="cred_123",
+ page_url="https://analytics.example.com/login",
+ org_credentials=[],
+ policy=RequestPolicy(resolved_credentials=[_resolved_credential()]),
+ )
+
+ assert error is None
+ load_mock.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_page_matched_credential_reaches_the_vault_and_fills(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ """The end-to-end seam: admission has to unblock the actual fill, not just the gate."""
+ page = _FakePage()
+ _wire_impl(monkeypatch, page)
+ policy = RequestPolicy()
+ ctx = _ctx(request_policy=policy)
+
+ with patch(
+ "skyvern.forge.app.DATABASE.credentials.get_credentials",
+ new=AsyncMock(return_value=[_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]),
+ ):
+ result = await tools_module._fill_credential_field_impl(ctx, "#passwordInput", "cred_analytics", "password")
+
+ assert result["ok"] is True
+ assert page.fill_calls == [("#passwordInput", _FAKE_PASSWORD)]
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_analytics"]
+
+ @pytest.mark.asyncio
+ async def test_a_redirect_after_admission_stops_the_secret_reaching_the_new_page(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ """Vault reads sit between the page match and the fill, so the page is re-checked."""
+ page = _FakePage()
+ _wire_impl(monkeypatch, page)
+ policy = RequestPolicy()
+ ctx = _ctx(request_policy=policy)
+
+ async def redirect_then_capture(_ctx: Any) -> None:
+ page.url = "https://elsewhere.example.com/collect"
+
+ monkeypatch.setattr(credential_fill_module, "_capture_scout_source_url", redirect_then_capture)
+
+ with patch(
+ "skyvern.forge.app.DATABASE.credentials.get_credentials",
+ new=AsyncMock(return_value=[_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]),
+ ):
+ result = await tools_module._fill_credential_field_impl(ctx, "#passwordInput", "cred_analytics", "password")
+
+ assert result["ok"] is False
+ assert page.fill_calls == []
+
+ @pytest.mark.asyncio
+ async def test_the_second_fill_of_an_admitted_credential_is_still_page_checked(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ """Username then password is the ordinary flow, and the page can move in between."""
+ page = _FakePage()
+ _wire_impl(monkeypatch, page)
+ ctx = _ctx(request_policy=RequestPolicy())
+
+ with patch(
+ "skyvern.forge.app.DATABASE.credentials.get_credentials",
+ new=AsyncMock(return_value=[_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]),
+ ):
+ first = await tools_module._fill_credential_field_impl(ctx, "#user", "cred_analytics", "username")
+ page.url = "https://evil.example.com/harvest"
+ second = await tools_module._fill_credential_field_impl(ctx, "#pass", "cred_analytics", "password")
+
+ assert first["ok"] is True
+ assert second["ok"] is False
+ assert page.fill_calls == [("#user", _FAKE_PASSWORD)]
+
+ @pytest.mark.asyncio
+ async def test_a_refusable_call_never_reads_the_org_credentials(self) -> None:
+ """The refusals that need no lookup must not page the credential table in first."""
+ policy = RequestPolicy(login_intent=True, email_signin_intent=True, credential_input_kind="none")
+ error, _, load_mock = await self._gate(
+ credential_id="cred_analytics",
+ page_url="https://analytics.example.com/login",
+ org_credentials=[_org_credential("cred_analytics", "analytics", "https://analytics.example.com/login")],
+ policy=policy,
+ )
+
+ assert error is not None
+ load_mock.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_a_second_step_on_the_same_site_still_fills(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ """Real sign-ins walk email -> password -> one-time code across paths of the same site;
+ the live PostHog case moved to a 2FA path between the password and the code."""
+ page = _FakePage()
+ _wire_impl(monkeypatch, page)
+ ctx = _ctx(request_policy=RequestPolicy())
+
+ with patch(
+ "skyvern.forge.app.DATABASE.credentials.get_credentials",
+ new=AsyncMock(return_value=[_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]),
+ ):
+ first = await tools_module._fill_credential_field_impl(ctx, "#user", "cred_analytics", "username")
+ page.url = "https://authenticationtest.com/simpleFormAuth/verify?step=2fa"
+ second = await tools_module._fill_credential_field_impl(ctx, "#code", "cred_analytics", "totp")
+
+ assert first["ok"] is True
+ assert second["ok"] is True
+ assert len(page.fill_calls) == 2
diff --git a/tests/unit/test_copilot_request_policy_workflow_credentials.py b/tests/unit/test_copilot_request_policy_workflow_credentials.py
index 9543553b8..01b1f0741 100644
--- a/tests/unit/test_copilot_request_policy_workflow_credentials.py
+++ b/tests/unit/test_copilot_request_policy_workflow_credentials.py
@@ -6,6 +6,7 @@ from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
import pytest
+from structlog.testing import capture_logs
from skyvern.forge.sdk.copilot.context import (
CredentialCheck,
@@ -15,6 +16,7 @@ from skyvern.forge.sdk.copilot.context import (
record_signin_email_in_global_llm_context,
)
from skyvern.forge.sdk.copilot.credential_pause import credential_pause_reason
+from skyvern.forge.sdk.copilot.credential_resolution import resolve_by_url
from skyvern.forge.sdk.copilot.request_policy import (
_AMBIGUOUS_URL_CREDENTIAL_QUESTION,
_LOGIN_CREDENTIAL_QUESTION,
@@ -23,14 +25,17 @@ from skyvern.forge.sdk.copilot.request_policy import (
_STORED_CREDENTIAL_URL_QUESTION,
CREDENTIAL_DEFERRED_DRAFT_REASONS,
CREDENTIAL_PROMPT_CLARIFICATION_REASONS,
+ LiveCredentialAdmission,
RequestPolicy,
_can_defer_unresolved_credential_name_for_draft,
_classification_from_raw,
_clean_email_list,
_resolve_credentials,
_saved_credential_names_mentioned,
+ _seed_prior_approved_credentials,
_should_defer_repeated_unresolved_credential_question,
_workflow_credential_inputs_unbound,
+ admit_credential_for_live_page,
build_request_policy,
credential_prompt_reason,
)
@@ -2709,3 +2714,454 @@ def test_a_negated_saved_name_is_not_treated_as_a_reference() -> None:
credentials = [SimpleNamespace(name="skyvern-datadog")]
assert _saved_credential_names_mentioned("do not use skyvern-datadog", credentials) == []
+
+
+async def _admit(
+ policy: RequestPolicy,
+ *,
+ credential_id: str,
+ page_url: str,
+ org_credentials: list[SimpleNamespace],
+) -> tuple[LiveCredentialAdmission, AsyncMock]:
+ load_mock = AsyncMock(return_value=org_credentials)
+ with patch("skyvern.forge.app.DATABASE.credentials.get_credentials", new=load_mock):
+ admission = await admit_credential_for_live_page(
+ policy,
+ organization_id="o_test",
+ credential_id=credential_id,
+ page_url=page_url,
+ )
+ return admission, load_mock
+
+
+_LIVE_LOGIN_URL = "https://analytics.example.com/login?next=%2Fweb"
+_SAVED_LOGIN_URL = "https://analytics.example.com/login"
+
+
+class TestLivePageCredentialAdmission:
+ """A login wall the scout reached is authority evidence no turn-start classification carried.
+
+ The turn-start resolved set is empty in every case here, which is the state a bare-task
+ prompt ("go to <deep link> and output <metric>") leaves behind.
+ """
+
+ @pytest.mark.asyncio
+ async def test_admits_the_credential_the_live_login_page_vouches_for(self) -> None:
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ assert admission.admitted is True
+ assert admission.steer is None
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_analytics"]
+
+ @pytest.mark.asyncio
+ async def test_admission_is_additive_and_does_not_duplicate(self) -> None:
+ already = _cred("other", "cred_other", "https://billing.example.com/login")
+ policy = RequestPolicy(resolved_credentials=[already])
+ await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL), already],
+ )
+ await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL), already],
+ )
+
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_other", "cred_analytics"]
+
+ @pytest.mark.asyncio
+ async def test_declines_a_credential_the_live_page_does_not_vouch_for(self) -> None:
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_unrelated",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("unrelated", "cred_unrelated", "https://billing.example.com/login")],
+ )
+
+ assert admission.admitted is False
+ assert policy.resolved_credentials == []
+
+ @pytest.mark.asyncio
+ async def test_a_wrong_credential_is_refused_without_naming_the_right_one(self) -> None:
+ """The tool answers a model-authored id; naming the match would enumerate the org for it."""
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_unrelated",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[
+ _cred("analytics", "cred_analytics", _SAVED_LOGIN_URL),
+ _cred("unrelated", "cred_unrelated", "https://billing.example.com/login"),
+ ],
+ )
+
+ assert admission.admitted is False
+ assert admission.steer is not None
+ assert "cred_analytics" not in admission.steer
+ assert policy.resolved_credentials == []
+
+ @pytest.mark.asyncio
+ async def test_several_page_matches_ask_instead_of_binding(self) -> None:
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_one",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[
+ _cred("analytics one", "cred_one", _SAVED_LOGIN_URL),
+ _cred("analytics two", "cred_two", _SAVED_LOGIN_URL),
+ ],
+ )
+
+ assert admission.admitted is False
+ assert admission.steer is not None
+ assert _AMBIGUOUS_URL_CREDENTIAL_QUESTION in admission.steer
+ assert "cred_one" not in admission.steer
+ assert policy.resolved_credentials == []
+ assert [c.credential_id for c in policy.discovered_credentials] == ["cred_one", "cred_two"]
+
+ @pytest.mark.asyncio
+ async def test_a_credential_saved_without_a_login_url_is_evidence_about_no_page(self) -> None:
+ """The sole-URL-less tier is turn-start-only: reaching a login wall says a login is needed,
+ not which credential this page belongs to."""
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_urlless",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_urlless")],
+ )
+
+ assert admission.admitted is False
+ assert policy.resolved_credentials == []
+
+ @pytest.mark.asyncio
+ async def test_a_url_matched_credential_is_named_when_a_urlless_one_was_requested(self) -> None:
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_urlless",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[
+ _cred("tested", "cred_tested", _SAVED_LOGIN_URL),
+ _cred("urlless", "cred_urlless"),
+ ],
+ )
+
+ assert admission.admitted is False
+ assert admission.steer is not None
+ assert "cred_tested" not in admission.steer
+
+ @pytest.mark.asyncio
+ async def test_a_card_credential_never_admits_as_a_login(self) -> None:
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_card",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("card", "cred_card", _SAVED_LOGIN_URL, CredentialType.CREDIT_CARD)],
+ )
+
+ assert admission.admitted is False
+ assert policy.resolved_credentials == []
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "policy",
+ [
+ RequestPolicy(raw_secret_detected=True),
+ RequestPolicy(allow_run_blocks=False),
+ ],
+ )
+ async def test_a_turn_without_run_authority_never_reads_credentials(self, policy: RequestPolicy) -> None:
+ admission, load_mock = await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ assert admission.admitted is False
+ assert policy.resolved_credentials == []
+ load_mock.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_no_live_page_url_never_reads_credentials(self) -> None:
+ policy = RequestPolicy()
+ admission, load_mock = await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url="",
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ assert admission.admitted is False
+ load_mock.assert_not_awaited()
+
+
[email protected]("kind", ["website_stored_credential", "credential_name"])
+async def test_a_credential_labelled_turn_looks_a_written_name_up_by_name(kind: str) -> None:
+ """Which credential label the classifier chose must not decide whether a name the user wrote
+ is searched by name (SKY-12917). The message carries no login phrase and no quoted name, so
+ only the classifier-ref route can resolve it."""
+ policy = RequestPolicy(credential_input_kind=kind, credential_refs=["analytics-login"])
+ await _resolve_direct(
+ policy,
+ user_message="Use analytics-login here.",
+ org_credentials=[_cred("analytics-login", "cred_analytics")],
+ )
+
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_analytics"]
+ assert policy.clarification_question is None
+
+
[email protected]("kind", ["none", "placeholder"])
+async def test_a_turn_the_classifier_never_called_a_credential_request_does_not_scan(kind: str) -> None:
+ policy = RequestPolicy(credential_input_kind=kind, credential_refs=["analytics-login"])
+ load_mock, _ = await _resolve_direct(
+ policy,
+ user_message="Use analytics-login here.",
+ org_credentials=[_cred("analytics-login", "cred_analytics")],
+ get_credentials=AsyncMock(side_effect=AssertionError("a non-credential turn must not scan")),
+ )
+
+ assert policy.resolved_credentials == []
+ load_mock.assert_not_awaited()
+
+
+async def test_a_degraded_classifier_never_enumerates_saved_credentials(kind: str = "credential_name") -> None:
+ policy = RequestPolicy(
+ credential_input_kind=kind, credential_refs=["analytics-login"], classifier_status="fallback"
+ )
+ load_mock, _ = await _resolve_direct(
+ policy,
+ user_message="Use analytics-login here.",
+ org_credentials=[_cred("analytics-login", "cred_analytics")],
+ get_credentials=AsyncMock(side_effect=AssertionError("a degraded turn must not scan")),
+ )
+
+ assert policy.resolved_credentials == []
+ load_mock.assert_not_awaited()
+
+
+async def test_a_stale_ref_absent_from_the_message_still_authorizes_nothing() -> None:
+ policy = RequestPolicy(credential_input_kind="website_stored_credential", credential_refs=["analytics-login"])
+ load_mock, by_ids_mock = await _resolve_direct(
+ policy,
+ user_message="Continue with the workflow.",
+ org_credentials=[_cred("analytics-login", "cred_analytics")],
+ get_credentials=AsyncMock(side_effect=AssertionError("a stale ref must not scan credentials")),
+ )
+
+ assert policy.resolved_credentials == []
+ load_mock.assert_not_awaited()
+
+
+def _passwordless_email_policy() -> RequestPolicy:
+ """The shape `_is_passwordless_email_signin` recognises: sign-in by email, no credential referenced."""
+ return RequestPolicy(
+ login_intent=True,
+ email_signin_intent=True,
+ credential_input_kind="none",
+ )
+
+
+class TestPasswordlessEmailTurnMeetsLivePageAdmission:
+ """A passwordless-email turn must not be pulled into a password-credential ask (SKY-13030 / :2559).
+
+ Turn-start resolution excludes this shape from the URL tiers; these pin what the mid-turn
+ seam does with the same shape, which is the interaction the turn-start rule cannot cover.
+ """
+
+ @pytest.mark.asyncio
+ async def test_a_sole_host_match_is_neither_admitted_nor_asked_about(self) -> None:
+ policy = _passwordless_email_policy()
+ admission, load_mock = await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ assert admission.admitted is False
+ assert admission.steer is None
+ assert policy.resolved_credentials == []
+ assert policy.clarification_question is None
+ load_mock.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_several_host_matches_do_not_pull_the_turn_into_a_credential_ask(self) -> None:
+ policy = _passwordless_email_policy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_one",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[
+ _cred("analytics one", "cred_one", _SAVED_LOGIN_URL),
+ _cred("analytics two", "cred_two", _SAVED_LOGIN_URL),
+ ],
+ )
+
+ assert admission.admitted is False
+ assert admission.steer is None
+ assert policy.discovered_credentials == []
+ assert policy.requires_user_clarification is False
+
+
+async def test_a_live_page_admission_does_not_carry_to_the_next_turn() -> None:
+ """The evidence is a page the next turn has not seen, so the grant is re-earned there."""
+ policy = RequestPolicy()
+ await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+ carried_context = record_approved_credentials_in_global_llm_context(SimpleNamespace(request_policy=policy), None)
+
+ next_turn = RequestPolicy()
+ with patch(
+ "skyvern.forge.app.DATABASE.credentials.get_credentials_by_ids",
+ new=AsyncMock(return_value=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)]),
+ ):
+ await _seed_prior_approved_credentials(
+ next_turn,
+ organization_id="o_test",
+ global_llm_context=carried_context or "",
+ )
+
+ assert next_turn.resolved_credentials == []
+
+
+async def test_session_reuse_language_grants_no_authority_at_either_seam() -> None:
+ """ "Use the tab where I'm signed in" is session context, not stored-credential authority
+ (SKY-10131) — and the live-page seam must not convert it into authority either, because
+ the tool gate, not the wording, is what reaches admission."""
+ policy = RequestPolicy(login_intent=False, credential_input_kind="none")
+ load_mock, _ = await _resolve_direct(
+ policy,
+ user_message="Use the tab where I'm signed in at https://analytics.example.com/web and read the visitor count.",
+ org_credentials=[_cred("analytics", "cred_analytics", "https://analytics.example.com/login")],
+ )
+ assert policy.resolved_credentials == []
+ assert policy.clarification_question is None
+
+ blocked = RequestPolicy(allow_run_blocks=False)
+ admission, admit_load = await _admit(
+ blocked,
+ credential_id="cred_analytics",
+ page_url="https://analytics.example.com/login",
+ org_credentials=[_cred("analytics", "cred_analytics", "https://analytics.example.com/login")],
+ )
+ assert admission.admitted is False
+ admit_load.assert_not_awaited()
+
+
+async def test_the_passwordless_decline_is_named_in_the_admission_log() -> None:
+ """QA counts a run only when this exact value appears, so the string is a contract, not a label.
+
+ Captured through structlog rather than caplog: a sibling test module reconfigures structlog's
+ factory, which empties caplog here depending on test order.
+ """
+ policy = _passwordless_email_policy()
+ with capture_logs() as entries:
+ await _admit(
+ policy,
+ credential_id="cred_one",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[
+ _cred("analytics one", "cred_one", _SAVED_LOGIN_URL),
+ _cred("analytics two", "cred_two", _SAVED_LOGIN_URL),
+ ],
+ )
+
+ assert any(entry.get("outcome") == "passwordless_declined" for entry in entries)
+
+
+class TestLivePageTierBoundaries:
+ """The path tier exists for the live page's transient query, not to cross an identity."""
+
+ def test_an_exact_match_wins_however_the_caller_orders_the_tiers(self) -> None:
+ credentials = [
+ _cred("tenant a", "cred_exact", "https://analytics.example.com/login?tenant=a"),
+ _cred("no tenant", "cred_loose", "https://analytics.example.com/login"),
+ ]
+ page = ["https://analytics.example.com/login?tenant=a"]
+
+ for tiers in (("url_exact", "url_path"), ("url_path", "url_exact")):
+ resolution = resolve_by_url(credentials, page, tiers=tiers, password_only=True)
+ assert resolution.tier == "url_exact"
+ assert [c.credential_id for c in resolution.candidates] == ["cred_exact"]
+
+ @pytest.mark.asyncio
+ async def test_a_tenant_query_on_the_saved_url_must_match_exactly(self) -> None:
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_tenant_a",
+ page_url="https://analytics.example.com/login?tenant=b",
+ org_credentials=[_cred("tenant a", "cred_tenant_a", "https://analytics.example.com/login?tenant=a")],
+ )
+
+ assert admission.admitted is False
+ assert policy.resolved_credentials == []
+
+ @pytest.mark.asyncio
+ async def test_the_live_pages_transient_query_does_not_block_the_match(self) -> None:
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ assert admission.admitted is True
+
+ @pytest.mark.asyncio
+ async def test_another_page_on_the_same_host_is_not_the_login_page(self) -> None:
+ policy = RequestPolicy()
+ admission, _ = await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url="https://analytics.example.com/u/someone/uploaded-note",
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ assert admission.admitted is False
+ assert policy.resolved_credentials == []
+
+
+async def test_basic_auth_in_a_login_url_never_reaches_the_admission_log() -> None:
+ """A sign-in URL can carry `user:pass@`, and the matching key keeps it deliberately."""
+ policy = RequestPolicy()
+ with capture_logs() as entries:
+ await _admit(
+ policy,
+ credential_id="cred_analytics",
+ page_url="https://admin:[email protected]/login?code=abc",
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ logged = " ".join(str(entry) for entry in entries)
+ assert "hunter2" not in logged
+ assert "admin:" not in logged
+ assert "code=abc" not in logged
diff --git a/tests/unit/test_credential_api_response.py b/tests/unit/test_credential_api_response.py
index 46eec06f8..8853a9360 100644
--- a/tests/unit/test_credential_api_response.py
+++ b/tests/unit/test_credential_api_response.py
@@ -7,6 +7,7 @@ from skyvern.forge.sdk.schemas.credentials import (
Credential,
CredentialType,
CredentialVaultType,
+ PasswordCredential,
TotpType,
)
@@ -53,3 +54,9 @@ def test_convert_to_response_includes_totp_type() -> None:
credential = _make_credential(totp_type=TotpType.EMAIL)
response = _convert_to_response(credential)
assert response.credential.totp_type == TotpType.EMAIL
+
+
+def test_password_credential_normalizes_empty_metadata() -> None:
+ credential = PasswordCredential(username="[email protected]", password="pw", metadata={})
+
+ assert credential.metadata is None
diff --git a/tests/unit/test_credential_delete_reaps_profile.py b/tests/unit/test_credential_delete_reaps_profile.py
index 533d7e877..6820b83e2 100644
--- a/tests/unit/test_credential_delete_reaps_profile.py
+++ b/tests/unit/test_credential_delete_reaps_profile.py
@@ -41,7 +41,10 @@ def _setup_app(
),
STORAGE=SimpleNamespace(delete_browser_profile=storage_delete_profile),
CREDENTIAL_VAULT_SERVICES={credentials.CredentialVaultType.BITWARDEN: vault_service},
- AGENT_FUNCTION=SimpleNamespace(is_browser_memory_engine_enabled_for_org=AsyncMock(return_value=engine_enabled)),
+ AGENT_FUNCTION=SimpleNamespace(
+ is_browser_memory_engine_enabled_for_org=AsyncMock(return_value=engine_enabled),
+ should_lock_credential_write=AsyncMock(return_value=False),
+ ),
)
monkeypatch.setattr(credentials, "app", fake_app)
monkeypatch.setattr(credentials, "_clear_cached_totp_code_preview", lambda **kwargs: None)
diff --git a/tests/unit/test_credential_fallback.py b/tests/unit/test_credential_fallback.py
index 93f531b03..a09132bda 100644
--- a/tests/unit/test_credential_fallback.py
+++ b/tests/unit/test_credential_fallback.py
@@ -901,6 +901,7 @@ async def test_clean_up_workflow_schedules_credential_fallback_retry() -> None:
mock_app.ARTIFACT_MANAGER.wait_for_upload_aiotasks = AsyncMock()
mock_app.STORAGE.save_downloaded_files = AsyncMock()
mock_app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context = MagicMock()
+ mock_app.AGENT_FUNCTION.on_workflow_run_terminal = AsyncMock()
await service.clean_up_workflow(
workflow=workflow,
@@ -938,6 +939,7 @@ async def test_clean_up_workflow_preserves_body_success_without_retrying_finally
mock_app.ARTIFACT_MANAGER.wait_for_upload_aiotasks = AsyncMock()
mock_app.STORAGE.save_downloaded_files = AsyncMock()
mock_app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context = MagicMock()
+ mock_app.AGENT_FUNCTION.on_workflow_run_terminal = AsyncMock()
await service.clean_up_workflow(
workflow=workflow,
@@ -980,6 +982,7 @@ async def test_clean_up_workflow_schedules_retry_even_when_webhook_raises() -> N
mock_app.ARTIFACT_MANAGER.wait_for_upload_aiotasks = AsyncMock()
mock_app.STORAGE.save_downloaded_files = AsyncMock()
mock_app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context = MagicMock()
+ mock_app.AGENT_FUNCTION.on_workflow_run_terminal = AsyncMock()
with pytest.raises(RuntimeError):
await service.clean_up_workflow(
@@ -1017,6 +1020,7 @@ async def test_clean_up_workflow_schedules_retry_when_earlier_cleanup_step_raise
mock_app.ARTIFACT_MANAGER.wait_for_upload_aiotasks = AsyncMock(side_effect=RuntimeError("upload drain failed"))
mock_app.STORAGE.save_downloaded_files = AsyncMock()
mock_app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context = MagicMock()
+ mock_app.AGENT_FUNCTION.on_workflow_run_terminal = AsyncMock()
with pytest.raises(RuntimeError):
await service.clean_up_workflow(
diff --git a/tests/unit/test_credential_rotation.py b/tests/unit/test_credential_rotation.py
index 6e4eb5033..82781cdf7 100644
--- a/tests/unit/test_credential_rotation.py
+++ b/tests/unit/test_credential_rotation.py
@@ -1016,6 +1016,10 @@ async def test_runtime_credential_registration_proceeds_when_lane_is_safe(
return_value=SimpleNamespace(sequential_credential_id=stamped_credential_id)
)
mock_app.CREDENTIAL_VAULT_SERVICES.get.return_value = credential_service
+ # Mirror the OSS no-op hook: return the resolved item unchanged.
+ mock_app.AGENT_FUNCTION.process_registered_credential_item = AsyncMock(
+ side_effect=lambda *, workflow_run_id, db_credential, credential_item: credential_item
+ )
await context.register_credential_parameter_value(login, organization)
diff --git a/tests/unit/test_credential_update_params.py b/tests/unit/test_credential_update_params.py
index 6055e4e3d..3dde1afa7 100644
--- a/tests/unit/test_credential_update_params.py
+++ b/tests/unit/test_credential_update_params.py
@@ -404,6 +404,7 @@ async def test_route_pin_only_update_does_not_pass_browser_profile_id(monkeypatc
fake_app = SimpleNamespace(
DATABASE=SimpleNamespace(credentials=SimpleNamespace(get_credential=AsyncMock(return_value=existing))),
CREDENTIAL_VAULT_SERVICES={CredentialVaultType.BITWARDEN: vault_service},
+ AGENT_FUNCTION=AgentFunction(),
)
monkeypatch.setattr(credentials_routes, "app", fake_app)
monkeypatch.setattr(credentials_routes, "_update_credential_or_profile_conflict", conflict)
@@ -1551,7 +1552,10 @@ async def test_oss_route_allows_disabling_sequential_credential(monkeypatch: pyt
monkeypatch.setattr(
forge_app,
"AGENT_FUNCTION",
- SimpleNamespace(supports_sequential_credentials=lambda: False),
+ SimpleNamespace(
+ supports_sequential_credentials=lambda: False,
+ should_lock_credential_write=AsyncMock(return_value=False),
+ ),
)
response = await credentials_routes.rename_credential(
diff --git a/tests/unit/test_custom_credential_client.py b/tests/unit/test_custom_credential_client.py
index 1aacae0c9..180fa85e6 100644
--- a/tests/unit/test_custom_credential_client.py
+++ b/tests/unit/test_custom_credential_client.py
@@ -1,7 +1,24 @@
+import json
+from unittest.mock import AsyncMock
+
import pytest
+from pydantic import BaseModel, ValidationError
+from structlog.testing import capture_logs
+from skyvern.exceptions import HttpException
+from skyvern.forge.sdk.api import custom_credential_client as client_module
from skyvern.forge.sdk.api.custom_credential_client import CustomCredentialAPIClient
-from skyvern.forge.sdk.schemas.credentials import CredentialType, SecretCredential
+from skyvern.forge.sdk.schemas.credentials import (
+ CredentialType,
+ PasswordCredential,
+ SecretCredential,
+)
+from skyvern.forge.sdk.services import credentials as credential_helpers
+from skyvern.forge.sdk.services.credential import custom_credential_vault_service as vault_module
+
+
+class _IntegerPayload(BaseModel):
+ count: int
@pytest.fixture
@@ -73,3 +90,73 @@ def test_api_response_to_credential_secret_missing_required_field(client: Custom
with pytest.raises(ValueError, match="Missing required secret fields from API"):
client._api_response_to_credential(response, name="Broken Secret", item_id="cred_789")
+
+
[email protected]("metadata", [{"region": "us-east"}, None])
+def test_password_credential_metadata_payload_round_trip(
+ client: CustomCredentialAPIClient,
+ metadata: dict[str, str] | None,
+) -> None:
+ credential = PasswordCredential(username="[email protected]", password="pw", metadata=metadata)
+
+ payload = client._credential_to_api_payload(credential)
+ assert payload.get("metadata") == metadata
+ assert ("metadata" in payload) is (metadata is not None)
+ restored = client._api_response_to_credential(payload, name="Login", item_id="cred_password")
+ assert isinstance(restored.credential, PasswordCredential)
+ assert restored.credential.metadata == metadata
+
+
+def test_format_error_response_redacts_password(client: CustomCredentialAPIClient) -> None:
+ password = "password-material"
+
+ detail = client._format_error_response({"detail": {"password": password, "message": "invalid"}})
+
+ assert password not in detail
+
+
+async def test_missing_id_response_log_never_contains_echoed_private_value(
+ client: CustomCredentialAPIClient,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ private_value = "echoed-private-value"
+ response_body = {
+ "echo": json.dumps(
+ {
+ "private_key": private_value,
+ "username": "[email protected]",
+ }
+ )
+ }
+ monkeypatch.setattr(
+ client_module,
+ "aiohttp_request",
+ AsyncMock(return_value=(200, {}, response_body)),
+ )
+
+ with capture_logs() as logs, pytest.raises(HttpException) as exc_info:
+ await client.create_credential(
+ name="Login",
+ credential=PasswordCredential(username="[email protected]", password="pw"),
+ )
+
+ assert private_value not in json.dumps(logs)
+ assert private_value not in str(exc_info.value)
+
+
+def test_shared_credential_error_formatter_omits_pydantic_input() -> None:
+ submitted_value = "submitted-sensitive-value"
+
+ with pytest.raises(ValidationError) as exc_info:
+ _IntegerPayload(count=submitted_value) # type: ignore[arg-type]
+
+ assert hasattr(credential_helpers, "safe_error_message")
+ message = credential_helpers.safe_error_message(exc_info.value)
+ assert "count" in message
+ assert submitted_value not in message
+
+
+def test_custom_credential_components_share_error_formatter() -> None:
+ assert client_module.safe_error_message is credential_helpers.safe_error_message
+ assert vault_module.safe_error_message is credential_helpers.safe_error_message
diff --git a/tests/unit/test_custom_credential_vault_service.py b/tests/unit/test_custom_credential_vault_service.py
new file mode 100644
index 000000000..c5716b8e7
--- /dev/null
+++ b/tests/unit/test_custom_credential_vault_service.py
@@ -0,0 +1,111 @@
+import asyncio
+from datetime import datetime
+from unittest.mock import AsyncMock
+
+import pytest
+
+from skyvern.forge import app
+from skyvern.forge.sdk.api.custom_credential_client import CustomCredentialAPIClient
+from skyvern.forge.sdk.schemas.credentials import (
+ CreateCredentialRequest,
+ Credential,
+ CredentialType,
+ CredentialVaultType,
+ NonEmptyPasswordCredential,
+)
+from skyvern.forge.sdk.services.credential.custom_credential_vault_service import CustomCredentialVaultService
+
+
+def _credential() -> Credential:
+ return Credential(
+ credential_id="cred_test",
+ organization_id="org_test",
+ name="Login",
+ vault_type=CredentialVaultType.CUSTOM,
+ item_id="item_old",
+ credential_type=CredentialType.PASSWORD,
+ username="user_test",
+ totp_type="none",
+ totp_identifier=None,
+ card_last4=None,
+ card_brand=None,
+ secret_label=None,
+ browser_profile_id=None,
+ tested_url=None,
+ user_context=None,
+ save_browser_session_intent=False,
+ folder_id=None,
+ created_at=datetime(2026, 1, 1),
+ modified_at=datetime(2026, 1, 1),
+ deleted_at=None,
+ )
+
+
+def _password_request() -> CreateCredentialRequest:
+ return CreateCredentialRequest(
+ name="Login Updated",
+ credential_type=CredentialType.PASSWORD,
+ credential=NonEmptyPasswordCredential(
+ username="user_test",
+ password="secret_test",
+ metadata={"key": "value"},
+ ),
+ )
+
+
+def _prepare_update(
+ monkeypatch: pytest.MonkeyPatch,
+ failure: BaseException,
+ *,
+ delete_failure: Exception | None = None,
+) -> tuple[CustomCredentialVaultService, AsyncMock]:
+ client = AsyncMock(spec=CustomCredentialAPIClient)
+ client.create_credential.return_value = "item_new"
+ client.delete_credential.side_effect = delete_failure
+ service = CustomCredentialVaultService(client=client)
+ monkeypatch.setattr(service, "_update_db_credential", AsyncMock(side_effect=failure))
+ return service, client.delete_credential
+
+
+ ("failure_type", "message"),
+ [
+ pytest.param(RuntimeError, "database unavailable", id="exception"),
+ pytest.param(asyncio.CancelledError, "repoint cancelled", id="cancelled"),
+ ],
+)
+async def test_update_credential_reclaims_new_item_when_repoint_fails(
+ monkeypatch: pytest.MonkeyPatch,
+ failure_type: type[BaseException],
+ message: str,
+) -> None:
+ service, delete_credential = _prepare_update(monkeypatch, failure_type(message))
+
+ with pytest.raises(failure_type):
+ await service.update_credential(_credential(), _password_request())
+
+ delete_credential.assert_awaited_with("item_new")
+
+
+async def test_update_credential_enqueues_orphan_when_cancelled_inline_cleanup_fails(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ service, delete_credential = _prepare_update(
+ monkeypatch,
+ asyncio.CancelledError(),
+ delete_failure=RuntimeError("vault unavailable"),
+ )
+ orphaned = AsyncMock()
+ monkeypatch.setattr(app.AGENT_FUNCTION, "on_credential_item_orphaned", orphaned)
+
+ with pytest.raises(asyncio.CancelledError):
+ await service.update_credential(_credential(), _password_request())
+
+ delete_credential.assert_awaited_with("item_new")
+ orphaned.assert_awaited_with(
+ organization_id="org_test",
+ item_id="item_new",
+ vault_type=CredentialVaultType.CUSTOM,
+ )
diff --git a/tests/unit/test_destination_facts_capture.py b/tests/unit/test_destination_facts_capture.py
index 1b9b39724..ac00abbb2 100644
--- a/tests/unit/test_destination_facts_capture.py
+++ b/tests/unit/test_destination_facts_capture.py
@@ -278,7 +278,7 @@ class TestScrapeAssemblyCarriesTheSidecar:
[[element("e1", destination=ANCHOR_FACTS)], [element("e1", destination=ANCHOR_FACTS)]]
)
- async def fake_create_instance(_frame: object) -> SkyvernFrame:
+ async def fake_create_instance(_frame: object, engine_selection: object = None) -> SkyvernFrame:
return main_frame
monkeypatch.setattr(SkyvernFrame, "create_instance", fake_create_instance)
diff --git a/tests/unit/test_download_dir_rebind.py b/tests/unit/test_download_dir_rebind.py
index 41e4cdbc0..21f66c1fe 100644
--- a/tests/unit/test_download_dir_rebind.py
+++ b/tests/unit/test_download_dir_rebind.py
@@ -1061,6 +1061,7 @@ async def test_real_browser_manager_adoption_resolves_context_run_id() -> None:
patch("skyvern.webeye.real_browser_manager.rebind_download_dir", new_callable=AsyncMock) as mock_rebind,
patch("skyvern.webeye.real_browser_manager.skyvern_context.current", return_value=ctx),
):
+ mock_app.AGENT_FUNCTION.on_browser_context_acquired = AsyncMock()
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=browser_state)
result = await manager.get_or_create_for_workflow_run(workflow_run, browser_session_id="bs")
diff --git a/tests/unit/test_element_engine_selection_threading.py b/tests/unit/test_element_engine_selection_threading.py
index 7d456556d..d7280b23e 100644
--- a/tests/unit/test_element_engine_selection_threading.py
+++ b/tests/unit/test_element_engine_selection_threading.py
@@ -58,39 +58,53 @@ def _element_double() -> MagicMock:
@pytest.mark.asyncio
-async def test_svg_eligibility_threads_live_task_engine_selection(monkeypatch: pytest.MonkeyPatch) -> None:
- selection = _selection()
[email protected]("selection", [_selection(), None])
+async def test_svg_eligibility_reuses_frame_selection_after_browser_state_removal(
+ selection: BrowserEngineSelection | None,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
task = _task()
locator = MagicMock(count=AsyncMock(return_value=1), is_visible=AsyncMock(return_value=True))
frame = MagicMock(locator=MagicMock(return_value=locator))
skyvern_frame = MagicMock(
+ engine_selection=selection,
get_frame=MagicMock(return_value=frame),
get_blocking_element_id=AsyncMock(return_value=(None, False)),
)
constructor = MagicMock(return_value=_element_double())
- manager = MagicMock(get_for_task=MagicMock(return_value=SimpleNamespace(engine_selection=selection)))
+ manager = MagicMock(get_for_task=MagicMock(return_value=None))
+ resolver = MagicMock(return_value=None)
monkeypatch.setattr(agent_functions, "SkyvernElement", constructor)
monkeypatch.setattr(agent_functions.app, "BROWSER_MANAGER", manager)
+ monkeypatch.setattr(agent_functions, "_resolve_engine_selection", resolver)
assert await agent_functions._check_svg_eligibility(skyvern_frame, {"id": "svg-1", "tagName": "svg"}, task)
assert constructor.call_args.kwargs["engine_selection"] is selection
- manager.get_for_task.assert_called_once_with(task.task_id, workflow_run_id=task.workflow_run_id)
+ resolver.assert_not_called()
+ manager.get_for_task.assert_not_called()
@pytest.mark.asyncio
-async def test_css_shape_conversion_threads_live_task_engine_selection(monkeypatch: pytest.MonkeyPatch) -> None:
- selection = _selection()
[email protected]("selection", [_selection(), None])
+async def test_css_shape_conversion_reuses_frame_selection_after_browser_state_replacement(
+ selection: BrowserEngineSelection | None,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ replacement = _selection()
task = _task()
locator = MagicMock(count=AsyncMock(return_value=1))
frame = MagicMock(locator=MagicMock(return_value=locator))
skyvern_frame = MagicMock(
+ engine_selection=selection,
get_frame=MagicMock(return_value=frame),
get_blocking_element_id=AsyncMock(return_value=(None, True)),
)
constructor = MagicMock(return_value=_element_double())
- manager = MagicMock(get_for_task=MagicMock(return_value=SimpleNamespace(engine_selection=selection)))
+ manager = MagicMock(get_for_task=MagicMock(return_value=SimpleNamespace(engine_selection=replacement)))
+ resolver = MagicMock(return_value=replacement)
monkeypatch.setattr(agent_functions, "SkyvernElement", constructor)
+ monkeypatch.setattr(agent_functions, "_resolve_engine_selection", resolver)
monkeypatch.setattr(
agent_functions,
"app",
@@ -110,7 +124,8 @@ async def test_css_shape_conversion_threads_live_task_engine_selection(monkeypat
)
assert constructor.call_args.kwargs["engine_selection"] is selection
- manager.get_for_task.assert_called_once_with(task.task_id, workflow_run_id=task.workflow_run_id)
+ resolver.assert_not_called()
+ manager.get_for_task.assert_not_called()
@pytest.mark.asyncio
diff --git a/tests/unit/test_extract_action_open_tabs_context.py b/tests/unit/test_extract_action_open_tabs_context.py
index f78511a30..b79333873 100644
--- a/tests/unit/test_extract_action_open_tabs_context.py
+++ b/tests/unit/test_extract_action_open_tabs_context.py
@@ -134,6 +134,31 @@ class TestCacheVariant:
assert "vc" in result
assert "cp" in result
+ def test_guidance_text_has_a_stable_collision_resistant_tag(self) -> None:
+ first = ForgeAgent._build_extract_action_cache_variant(
+ verification_code_check=False,
+ show_close_page_action=False,
+ complete_criterion=None,
+ extra_action_guidance="Use verification method A.",
+ )
+ repeated = ForgeAgent._build_extract_action_cache_variant(
+ verification_code_check=False,
+ show_close_page_action=False,
+ complete_criterion=None,
+ extra_action_guidance="Use verification method A.",
+ )
+ second = ForgeAgent._build_extract_action_cache_variant(
+ verification_code_check=False,
+ show_close_page_action=False,
+ complete_criterion=None,
+ extra_action_guidance="Use verification method B.",
+ )
+
+ assert first == repeated
+ assert first.startswith("g")
+ assert len(first) == 9
+ assert first != second
+
class TestNewTabSwitchTabGate:
@pytest.mark.parametrize("template", ["extract-action", "extract-action-static"])
diff --git a/tests/unit/test_extract_action_prompt.py b/tests/unit/test_extract_action_prompt.py
index 82db6f8a2..f12c9751e 100644
--- a/tests/unit/test_extract_action_prompt.py
+++ b/tests/unit/test_extract_action_prompt.py
@@ -82,3 +82,15 @@ def test_static_template_is_prefix_when_flag_on(prompt_engine: PromptEngine) ->
full = prompt_engine.load_prompt("extract-action", **kwargs)
static = prompt_engine.load_prompt("extract-action-static", **kwargs)
assert full.startswith(static.rstrip())
+
+
[email protected]("template", ["extract-action", "extract-action-static"])
+def test_extra_action_guidance_renders_at_action_boundary(prompt_engine: PromptEngine, template: str) -> None:
+ guidance = "Use the organization-approved verification method when it is offered."
+
+ rendered = prompt_engine.load_prompt(
+ template,
+ **{**_EXTRACT_ACTION_KWARGS, "extra_action_guidance": guidance},
+ )
+
+ assert guidance in rendered
diff --git a/tests/unit/test_failure_summary_engine_selection.py b/tests/unit/test_failure_summary_engine_selection.py
new file mode 100644
index 000000000..57d10078d
--- /dev/null
+++ b/tests/unit/test_failure_summary_engine_selection.py
@@ -0,0 +1,102 @@
+from datetime import UTC
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from skyvern.forge import app
+from skyvern.forge.agent import ForgeAgent
+from skyvern.forge.sdk.core import skyvern_context
+from skyvern.webeye.utils.page import SkyvernFrame
+
+_METHODS = [
+ ("summary_failure_reason_for_max_steps", "LLM_API_HANDLER", {}),
+ ("summary_failure_reason_for_max_retries", "SECONDARY_LLM_API_HANDLER", {"max_retries": 1}),
+]
+
+
+def _task() -> MagicMock:
+ return MagicMock(
+ task_id="task-1",
+ workflow_run_id="run-1",
+ organization_id="org-1",
+ navigation_goal="goal",
+ navigation_payload=None,
+ error_code_mapping=None,
+ workflow_system_prompt=None,
+ )
+
+
+def _patch_frame_and_handler(monkeypatch: pytest.MonkeyPatch, handler_name: str) -> tuple[AsyncMock, AsyncMock]:
+ monkeypatch.setattr(app.DATABASE.tasks, "get_task_steps", AsyncMock(return_value=[]))
+ monkeypatch.setattr(skyvern_context, "ensure_context", lambda: SimpleNamespace(tz_info=UTC))
+ frame = MagicMock(get_content=AsyncMock(return_value="<html></html>"))
+ create_instance = AsyncMock(return_value=frame)
+ monkeypatch.setattr(SkyvernFrame, "create_instance", create_instance)
+ take_screenshots = AsyncMock(return_value=[])
+ monkeypatch.setattr(SkyvernFrame, "take_split_screenshots", take_screenshots)
+ monkeypatch.setattr(
+ app,
+ handler_name,
+ AsyncMock(return_value={"page_info": "", "reasoning": "summary", "errors": []}),
+ )
+ return create_instance, take_screenshots
+
+
[email protected](("method_name", "handler_name", "extra_kwargs"), _METHODS)
+async def test_failure_summary_threads_pinned_selection_without_reresolving(
+ monkeypatch: pytest.MonkeyPatch,
+ method_name: str,
+ handler_name: str,
+ extra_kwargs: dict[str, int],
+) -> None:
+ selection = MagicMock()
+ # A live manager that, if consulted at teardown, would hand back a DIFFERENT selection. The
+ # summary must thread the caller's already-pinned selection and never re-resolve, so this
+ # drift can't leak into the screenshots.
+ drifted_manager = MagicMock(get_for_task=MagicMock(return_value=MagicMock(engine_selection=MagicMock())))
+ monkeypatch.setattr(app, "BROWSER_MANAGER", drifted_manager)
+ create_instance, take_screenshots = _patch_frame_and_handler(monkeypatch, handler_name)
+
+ response = await getattr(ForgeAgent(), method_name)(
+ organization=MagicMock(organization_id="org-1"),
+ task=_task(),
+ step=MagicMock(),
+ page=MagicMock(url="https://example.test"),
+ engine_selection=selection,
+ **extra_kwargs,
+ )
+
+ assert response.reasoning == "summary"
+ drifted_manager.get_for_task.assert_not_called()
+ assert take_screenshots.await_args.kwargs["engine_selection"] is selection
+ if create_instance.await_args is not None:
+ assert create_instance.await_args.kwargs["engine_selection"] is selection
+
+
[email protected](("method_name", "handler_name", "extra_kwargs"), _METHODS)
+async def test_failure_summary_preserves_explicit_none_selection(
+ monkeypatch: pytest.MonkeyPatch,
+ method_name: str,
+ handler_name: str,
+ extra_kwargs: dict[str, int],
+) -> None:
+ manager = MagicMock(get_for_task=MagicMock(return_value=MagicMock(engine_selection=MagicMock())))
+ monkeypatch.setattr(app, "BROWSER_MANAGER", manager)
+ create_instance, take_screenshots = _patch_frame_and_handler(monkeypatch, handler_name)
+
+ await getattr(ForgeAgent(), method_name)(
+ organization=MagicMock(organization_id="org-1"),
+ task=_task(),
+ step=MagicMock(),
+ page=MagicMock(url="https://example.test"),
+ engine_selection=None,
+ **extra_kwargs,
+ )
+
+ manager.get_for_task.assert_not_called()
+ assert take_screenshots.await_args.kwargs["engine_selection"] is None
+ if create_instance.await_args is not None:
+ assert create_instance.await_args.kwargs["engine_selection"] is None
diff --git a/tests/unit/test_gcp_credential_vault_service.py b/tests/unit/test_gcp_credential_vault_service.py
index 4ef705939..3ed19da50 100644
--- a/tests/unit/test_gcp_credential_vault_service.py
+++ b/tests/unit/test_gcp_credential_vault_service.py
@@ -26,6 +26,9 @@ TEST_PROJECT = "test-project"
TEST_ORG = "o_123"
+_PASSWORD_METADATA = {"tenant": "north"}
+
+
def _service() -> tuple[GcpCredentialVaultService, AsyncMock]:
"""Return a service plus the mock vault client it delegates to.
@@ -50,7 +53,12 @@ class TestGcpSecretItemCreation:
item_id = await svc._create_gcp_secret_item(
organization_id=TEST_ORG,
- credential=PasswordCredential(username="[email protected]", password="pw", totp="JBSWY3DPEHPK3PXP"),
+ credential=PasswordCredential(
+ username="[email protected]",
+ password="pw",
+ totp="JBSWY3DPEHPK3PXP",
+ metadata=_PASSWORD_METADATA,
+ ),
)
assert item_id == "ret-id"
@@ -62,6 +70,7 @@ class TestGcpSecretItemCreation:
"password": "pw",
"username": "[email protected]",
"totp": "JBSWY3DPEHPK3PXP",
+ "metadata": _PASSWORD_METADATA,
}
async def test_create_credit_card_secret_item(self) -> None:
@@ -136,7 +145,15 @@ class TestGetCredentialItem:
async def test_get_password_credential(self) -> None:
svc, client = _service()
client.get_secret = AsyncMock(
- return_value=json.dumps({"type": "password", "username": "u", "password": "p", "totp": "T"})
+ return_value=json.dumps(
+ {
+ "type": "password",
+ "username": "u",
+ "password": "p",
+ "totp": "T",
+ "metadata": _PASSWORD_METADATA,
+ }
+ )
)
db_cred = _db_credential(item_id="sid", totp_type=TotpType.NONE, name="My Login")
@@ -148,6 +165,17 @@ class TestGetCredentialItem:
assert isinstance(item.credential, PasswordCredential)
assert item.credential.username == "u"
assert item.credential.password == "p"
+ assert item.credential.metadata == _PASSWORD_METADATA
+
+ async def test_get_legacy_password_credential_without_metadata(self) -> None:
+ svc, client = _service()
+ client.get_secret = AsyncMock(return_value='{"type":"password","username":"u","password":"p"}')
+ db_cred = _db_credential(item_id="sid", totp_type=TotpType.NONE, name="My Login")
+
+ item = await svc.get_credential_item(db_cred)
+
+ assert isinstance(item.credential, PasswordCredential)
+ assert item.credential.metadata is None
async def test_get_credit_card_credential(self) -> None:
svc, client = _service()
diff --git a/tests/unit/test_non_secret_credential_fields.py b/tests/unit/test_non_secret_credential_fields.py
index 6010d9480..c3490473c 100644
--- a/tests/unit/test_non_secret_credential_fields.py
+++ b/tests/unit/test_non_secret_credential_fields.py
@@ -44,6 +44,10 @@ async def _register(monkeypatch: pytest.MonkeyPatch) -> tuple[WorkflowRunContext
app = MagicMock()
app.DATABASE.credentials.get_credential = AsyncMock(return_value=db_credential)
app.CREDENTIAL_VAULT_SERVICES.get.return_value = vault
+ # Mirror the OSS no-op hook: return the resolved item unchanged.
+ app.AGENT_FUNCTION.process_registered_credential_item = AsyncMock(
+ side_effect=lambda *, workflow_run_id, db_credential, credential_item: credential_item
+ )
monkeypatch.setattr(context_manager_module, "app", app)
await context._register_credential_parameter_value(
diff --git a/tests/unit/test_onboarding_milestone_hooks.py b/tests/unit/test_onboarding_milestone_hooks.py
index 20e20f2d6..12241403c 100644
--- a/tests/unit/test_onboarding_milestone_hooks.py
+++ b/tests/unit/test_onboarding_milestone_hooks.py
@@ -310,6 +310,7 @@ class TestWorkflowRunCompleteHookFires:
mock_agent_fn.on_workflow_run_completed = AsyncMock()
mock_workflow_run = MagicMock()
+ mock_workflow_run.workflow_run_id = "wr_1"
mock_workflow_run.organization_id = "o_789"
mock_workflow_run.workflow_id = "wf_1"
mock_workflow_run.workflow_permanent_id = "wpid_1"
@@ -326,6 +327,7 @@ class TestWorkflowRunCompleteHookFires:
mock_db = MagicMock()
mock_db.workflow_runs.update_workflow_run = AsyncMock(return_value=mock_workflow_run)
+ mock_db.tags.apply_system_run_tag_changes = AsyncMock()
with (
patch("skyvern.forge.sdk.workflow.service.app") as mock_app,
@@ -345,13 +347,68 @@ class TestWorkflowRunCompleteHookFires:
workflow_run_id="wr_1",
status=mock_status,
)
- await asyncio.sleep(0)
+ while svc._background_tasks:
+ await asyncio.gather(*tuple(svc._background_tasks))
mock_agent_fn.on_workflow_run_completed.assert_awaited_once_with(
organization_id="o_789",
workflow_id="wf_1",
status=mock_status,
)
+ mock_agent_fn.on_workflow_run_terminal.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_cleanup_awaits_terminal_hook_before_browser_cleanup(self) -> None:
+ mock_agent_fn = MagicMock(spec=AgentFunction)
+ mock_agent_fn.on_workflow_run_terminal = AsyncMock()
+
+ mock_workflow = MagicMock()
+ mock_workflow_run = MagicMock()
+ mock_workflow_run.workflow_run_id = "wr_1"
+ mock_workflow_run.organization_id = "o_789"
+ mock_workflow_run.status = "completed"
+
+ browser_cleanup_result = MagicMock()
+ browser_cleanup_result.browser_state = None
+ browser_cleanup_result.tasks = []
+ browser_cleanup_result.all_workflow_task_ids = []
+ browser_cleanup_result.child_workflow_run_ids = []
+ browser_cleanup_result.close_browser_on_completion = True
+
+ async def clean_up_browser(**_: object) -> MagicMock:
+ mock_agent_fn.on_workflow_run_terminal.assert_awaited_once_with(
+ workflow_run_id="wr_1",
+ organization_id="o_789",
+ status="completed",
+ )
+ return browser_cleanup_result
+
+ with (
+ patch("skyvern.forge.sdk.workflow.service.app") as mock_app,
+ patch("skyvern.forge.sdk.workflow.service.analytics.capture"),
+ ):
+ mock_app.AGENT_FUNCTION = mock_agent_fn
+ mock_app.ARTIFACT_MANAGER.wait_for_upload_aiotasks = AsyncMock()
+ mock_app.STORAGE.save_downloaded_files = AsyncMock()
+ mock_app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context = MagicMock()
+
+ from skyvern.forge.sdk.workflow.service import WorkflowService
+
+ svc = WorkflowService.__new__(WorkflowService)
+ svc._clean_up_workflow_browser = AsyncMock(side_effect=clean_up_browser)
+ svc._schedule_credential_fallback_retry = MagicMock()
+
+ await svc.clean_up_workflow(
+ workflow=mock_workflow,
+ workflow_run=mock_workflow_run,
+ need_call_webhook=False,
+ )
+
+ mock_agent_fn.on_workflow_run_terminal.assert_awaited_once_with(
+ workflow_run_id="wr_1",
+ organization_id="o_789",
+ status="completed",
+ )
@pytest.mark.asyncio
async def test_run_non_final_does_not_fire_hook(self) -> None:
@@ -386,3 +443,4 @@ class TestWorkflowRunCompleteHookFires:
await asyncio.sleep(0)
mock_agent_fn.on_workflow_run_completed.assert_not_awaited()
+ mock_agent_fn.on_workflow_run_terminal.assert_not_awaited()
diff --git a/tests/unit/test_real_azure.py b/tests/unit/test_real_azure.py
new file mode 100644
index 000000000..dfa4351fe
--- /dev/null
+++ b/tests/unit/test_real_azure.py
@@ -0,0 +1,148 @@
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+from azure.core.exceptions import ClientAuthenticationError, ResourceNotFoundError
+
+from skyvern.forge.sdk.api.real_azure import RealAsyncAzureVaultClient
+
+
+class _AsyncVersionIterator:
+ def __init__(self, items: list[object]) -> None:
+ self._items = list(items)
+
+ def __aiter__(self) -> "_AsyncVersionIterator":
+ return self
+
+ async def __anext__(self) -> object:
+ if not self._items:
+ raise StopAsyncIteration
+ return self._items.pop(0)
+
+
+class _RaisingVersionIterator:
+ def __aiter__(self) -> "_RaisingVersionIterator":
+ return self
+
+ async def __anext__(self) -> object:
+ raise ResourceNotFoundError("no prior versions")
+
+
+class _UnauthorizedVersionIterator:
+ def __aiter__(self) -> "_UnauthorizedVersionIterator":
+ return self
+
+ async def __anext__(self) -> object:
+ raise ClientAuthenticationError("caller lacks secrets/list permission")
+
+
+def _secret_client(versions_iterator: object, *, disable_error: Exception | None = None) -> MagicMock:
+ secret_client = MagicMock()
+ secret_client.list_properties_of_secret_versions = MagicMock(return_value=versions_iterator)
+ secret_client.set_secret = AsyncMock(return_value=SimpleNamespace(name="my-secret"))
+ secret_client.update_secret_properties = AsyncMock(side_effect=disable_error)
+ secret_client.close = AsyncMock()
+ return secret_client
+
+
+def _vault_client(monkeypatch: pytest.MonkeyPatch, secret_client: MagicMock) -> RealAsyncAzureVaultClient:
+ client = RealAsyncAzureVaultClient(credential=AsyncMock())
+ monkeypatch.setattr(client, "_get_secret_client", AsyncMock(return_value=secret_client))
+ return client
+
+
+async def test_create_or_update_secret_disables_only_preexisting_enabled_versions(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ secret_client = _secret_client(
+ _AsyncVersionIterator(
+ [
+ SimpleNamespace(version="v-enabled", enabled=True),
+ SimpleNamespace(version="v-unset", enabled=None), # enabled is not False -> still readable
+ SimpleNamespace(version="v-disabled", enabled=False), # already disabled -> skip
+ SimpleNamespace(version=None, enabled=True), # no version id -> skip
+ ]
+ )
+ )
+ client = _vault_client(monkeypatch, secret_client)
+
+ result = await client.create_or_update_secret("my-secret", "new-value", "vault")
+
+ assert result == "my-secret"
+ secret_client.set_secret.assert_awaited_once_with("my-secret", "new-value")
+ disabled = sorted(sent.args[1] for sent in secret_client.update_secret_properties.await_args_list)
+ assert disabled == ["v-enabled", "v-unset"]
+ for sent in secret_client.update_secret_properties.await_args_list:
+ assert sent.kwargs == {"enabled": False}
+ secret_client.close.assert_awaited_once()
+
+
+async def test_create_or_update_secret_snapshots_versions_before_writing_new_one(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ call_order: list[str] = []
+ secret_client = _secret_client(_AsyncVersionIterator([SimpleNamespace(version="v-old", enabled=True)]))
+ secret_client.set_secret = AsyncMock(
+ side_effect=lambda *_: call_order.append("set") or SimpleNamespace(name="my-secret")
+ )
+ secret_client.update_secret_properties = AsyncMock(side_effect=lambda *a, **k: call_order.append("disable"))
+ secret_client.list_properties_of_secret_versions = MagicMock(
+ side_effect=lambda *_: (
+ call_order.append("list") or _AsyncVersionIterator([SimpleNamespace(version="v-old", enabled=True)])
+ )
+ )
+ client = _vault_client(monkeypatch, secret_client)
+
+ await client.create_or_update_secret("my-secret", "new-value", "vault")
+
+ # The new value must be written before any prior version is disabled, so a mid-write crash never
+ # leaves the secret with every version disabled and no readable value.
+ assert call_order == ["list", "set", "disable"]
+
+
+async def test_create_or_update_secret_survives_version_disable_failure(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ secret_client = _secret_client(
+ _AsyncVersionIterator([SimpleNamespace(version="v-old", enabled=True)]),
+ disable_error=RuntimeError("disable failed"),
+ )
+ client = _vault_client(monkeypatch, secret_client)
+
+ result = await client.create_or_update_secret("my-secret", "new-value", "vault")
+
+ assert result == "my-secret"
+ secret_client.set_secret.assert_awaited_once()
+ secret_client.close.assert_awaited_once()
+
+
+async def test_create_or_update_secret_handles_missing_prior_versions(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ secret_client = _secret_client(_RaisingVersionIterator())
+ client = _vault_client(monkeypatch, secret_client)
+
+ result = await client.create_or_update_secret("my-secret", "new-value", "vault")
+
+ assert result == "my-secret"
+ secret_client.update_secret_properties.assert_not_awaited()
+ secret_client.close.assert_awaited_once()
+
+
+async def test_create_or_update_secret_writes_when_version_listing_is_unauthorized(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ secret_client = _secret_client(_UnauthorizedVersionIterator())
+ client = _vault_client(monkeypatch, secret_client)
+
+ result = await client.create_or_update_secret("my-secret", "new-value", "vault")
+
+ assert result == "my-secret"
+ secret_client.set_secret.assert_awaited_once_with("my-secret", "new-value")
+ secret_client.update_secret_properties.assert_not_awaited()
+ secret_client.close.assert_awaited_once()
diff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.py
index c01fd436f..db3bdaf75 100644
--- a/tests/unit/test_real_browser_manager.py
+++ b/tests/unit/test_real_browser_manager.py
@@ -35,6 +35,10 @@ def make_workflow_run(
return wfr
+def configure_browser_context_acquired_hook(mock_app: MagicMock) -> None:
+ mock_app.AGENT_FUNCTION.on_browser_context_acquired = AsyncMock()
+
+
class _StopBeforeBrowserContext(Exception):
pass
@@ -76,6 +80,7 @@ async def test_pbs_workflow_run_cache_hit_on_second_call() -> None:
workflow_run = make_workflow_run("wfr_child")
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
result = await manager.get_or_create_for_workflow_run(
workflow_run=workflow_run,
url="https://example.com",
@@ -100,6 +105,7 @@ async def test_pbs_workflow_run_does_not_inherit_parent_browser() -> None:
pbs_state.get_or_create_page = AsyncMock()
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=pbs_state)
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -257,6 +263,7 @@ async def test_task_browser_inherits_session_proxy_when_no_browser_state() -> No
session = make_session(proxy_location=session_proxy)
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.AGENT_FUNCTION.merge_proxy_session_extra_http_headers.side_effect = _merge_cloud_proxy_session_headers
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=None)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_session = AsyncMock(return_value=session)
@@ -283,6 +290,7 @@ async def test_task_browser_inherits_session_proxy_pin_when_no_browser_state() -
session = make_session(proxy_location="RESIDENTIAL_ISP", proxy_session_id="abc1234567")
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.AGENT_FUNCTION.merge_proxy_session_extra_http_headers.side_effect = _merge_cloud_proxy_session_headers
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=None)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_session = AsyncMock(return_value=session)
@@ -311,6 +319,7 @@ async def test_task_browser_uses_task_proxy_when_session_has_no_proxy() -> None:
session = make_session(proxy_location=None)
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=None)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_session = AsyncMock(return_value=session)
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -339,6 +348,7 @@ async def test_workflow_run_browser_inherits_session_proxy_when_no_browser_state
session = make_session(proxy_location=session_proxy)
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=None)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_session = AsyncMock(return_value=session)
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -369,6 +379,7 @@ async def test_workflow_run_browser_inherits_session_proxy_pin_when_no_browser_s
session = make_session(proxy_location="RESIDENTIAL_ISP", proxy_session_id="abc1234567")
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.AGENT_FUNCTION.merge_proxy_session_extra_http_headers.side_effect = _merge_cloud_proxy_session_headers
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=None)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_session = AsyncMock(return_value=session)
@@ -403,6 +414,7 @@ async def test_workflow_run_browser_uses_workflow_proxy_when_session_has_no_prox
session = make_session(proxy_location=None)
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=None)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_session = AsyncMock(return_value=session)
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -717,6 +729,7 @@ async def test_pbs_adoption_rebinds_download_dir_to_run_id() -> None:
patch("skyvern.webeye.real_browser_manager.app") as mock_app,
patch("skyvern.webeye.real_browser_manager.rebind_download_dir", new_callable=AsyncMock) as mock_rebind,
):
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=pbs_state)
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -744,6 +757,7 @@ async def test_pbs_adoption_skips_rebind_when_no_browser() -> None:
patch("skyvern.webeye.real_browser_manager.app") as mock_app,
patch("skyvern.webeye.real_browser_manager.rebind_download_dir", new_callable=AsyncMock) as mock_rebind,
):
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=pbs_state)
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -818,6 +832,7 @@ async def test_pbs_navigate_evicts_and_retries_on_connection_closed_driver_error
workflow_run = make_workflow_run("wfr_pbs")
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(side_effect=[stale, fresh])
mock_app.PERSISTENT_SESSIONS_MANAGER.evict_cached_browser_state = AsyncMock()
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -855,6 +870,7 @@ async def test_pbs_navigate_does_not_retry_on_unrelated_error() -> None:
workflow_run = make_workflow_run("wfr_pbs")
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=stale)
mock_app.PERSISTENT_SESSIONS_MANAGER.evict_cached_browser_state = AsyncMock()
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -890,6 +906,7 @@ async def test_pbs_navigate_does_not_evict_on_page_only_close() -> None:
workflow_run = make_workflow_run("wfr_pbs")
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=stale)
mock_app.PERSISTENT_SESSIONS_MANAGER.evict_cached_browser_state = AsyncMock()
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -929,6 +946,7 @@ async def test_pbs_recovery_path_passes_expected_state_to_public_evict() -> None
patch("skyvern.webeye.real_browser_manager.app") as mock_app,
patch("skyvern.webeye.real_browser_manager.rebind_download_dir", new_callable=AsyncMock),
):
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(side_effect=[stale, fresh])
mock_app.PERSISTENT_SESSIONS_MANAGER.evict_cached_browser_state = AsyncMock()
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -969,6 +987,7 @@ async def test_pbs_recovery_path_rebinds_download_dir_on_fresh_browser() -> None
patch("skyvern.webeye.real_browser_manager.app") as mock_app,
patch("skyvern.webeye.real_browser_manager.rebind_download_dir", new_callable=AsyncMock) as mock_rebind,
):
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(side_effect=[stale, fresh])
mock_app.PERSISTENT_SESSIONS_MANAGER.evict_cached_browser_state = AsyncMock()
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -1006,6 +1025,7 @@ async def test_pbs_navigate_skips_recovery_when_manager_cannot_reconnect() -> No
workflow_run = make_workflow_run("wfr_pbs")
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=stale)
mock_app.PERSISTENT_SESSIONS_MANAGER.evict_cached_browser_state = AsyncMock()
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
@@ -1047,6 +1067,7 @@ async def test_pbs_recovery_falls_through_to_get_or_create_page_when_fresh_state
workflow_run = make_workflow_run("wfr_pbs")
with patch("skyvern.webeye.real_browser_manager.app") as mock_app:
+ configure_browser_context_acquired_hook(mock_app)
mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(side_effect=[stale, fresh])
mock_app.PERSISTENT_SESSIONS_MANAGER.evict_cached_browser_state = AsyncMock()
mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()
diff --git a/tests/unit/test_request_logging.py b/tests/unit/test_request_logging.py
index 03d0729c6..52775adcd 100644
--- a/tests/unit/test_request_logging.py
+++ b/tests/unit/test_request_logging.py
@@ -209,6 +209,61 @@ class TestRedactSensitiveFields:
result = redact_sensitive_fields(data)
assert result == data
+ def test_redacts_sensitive_fields_inside_serialized_json(self) -> None:
+ private_value = "serialized-private-value"
+ data = {
+ "payload": json.dumps(
+ {
+ "private_key": private_value,
+ "items": [{"status": "ok"}, {"api_key": "nested-key"}],
+ }
+ )
+ }
+
+ result = redact_sensitive_fields(data)
+ decoded = json.loads(result["payload"])
+
+ assert private_value not in result["payload"]
+ assert decoded == {
+ "private_key": _REDACTED,
+ "items": [{"status": "ok"}, {"api_key": _REDACTED}],
+ }
+
+ def test_metadata_preserved_by_default_and_redacted_only_when_requested(self) -> None:
+ payload = {"metadata": {"region": "north", "note": "opaque"}}
+
+ assert redact_sensitive_fields(payload) == payload
+ assert redact_sensitive_fields(payload, redact_metadata=True) == {"metadata": _REDACTED}
+
+ def test_is_sensitive_key_gates_metadata_behind_flag(self) -> None:
+ assert _is_sensitive_key("metadata") is False
+ assert _is_sensitive_key("Metadata", redact_metadata=True) is True
+ assert _is_sensitive_key("password") is True
+
+ def test_redact_metadata_flag_threads_into_nested_and_serialized_values(self) -> None:
+ nested = {"outer": {"metadata": {"k": "v"}, "keep": "me"}}
+ assert redact_sensitive_fields(nested, redact_metadata=True) == {
+ "outer": {"metadata": _REDACTED, "keep": "me"},
+ }
+
+ serialized = {"payload": json.dumps({"metadata": {"k": "v"}, "ok": 1})}
+ result = redact_sensitive_fields(serialized, redact_metadata=True)
+ assert json.loads(result["payload"]) == {"metadata": _REDACTED, "ok": 1}
+
+ def test_preserves_serialized_json_byte_for_byte_when_nothing_redacted(self) -> None:
+ # A non-secret JSON payload must round-trip unchanged: re-serializing would escape non-ASCII and
+ # re-space separators, corrupting downstream consumers that compare bytes.
+ original = '{"note":"Grüße","items":[1,2,3]}'
+ result = redact_sensitive_fields({"payload": original})
+ assert result["payload"] == original
+
+ @pytest.mark.parametrize("value", ["ordinary text", "123", "true", '"json string"'])
+ def test_preserves_non_object_strings(self, value: str) -> None:
+ assert redact_sensitive_fields({"message": value, "status": "ok"}) == {
+ "message": value,
+ "status": "ok",
+ }
+
def test_handles_non_dict_non_list(self) -> None:
assert redact_sensitive_fields("hello") == "hello"
assert redact_sensitive_fields(42) == 42
@@ -357,6 +412,11 @@ class TestSanitizeResponseBody:
result = _sanitize_response_body(request, '{"data": "value"}', "application/json")
assert result == _REDACTED
+ def test_malformed_json_credential_mutation_response_is_redacted(self) -> None:
+ request = _make_request("PUT", "/api/v1/credentials/cred_123/rotate")
+ result = _sanitize_response_body(request, '{"private_key":"secret"', "application/json")
+ assert result == _REDACTED
+
@pytest.mark.parametrize(
("method", "path"),
[
@@ -410,6 +470,28 @@ class TestSanitizeBody:
result = _sanitize_body(request, b'{"user": "alice"}', "application/json")
assert result == '{"user": "alice"}'
+ @pytest.mark.parametrize(
+ ("method", "path"),
+ [
+ ("POST", "/v1/credentials/cred_123/rotate"),
+ ("PUT", "/api/v1/credentials/cred_123"),
+ ],
+ )
+ def test_malformed_json_credential_mutation_request_is_redacted(self, method: str, path: str) -> None:
+ request = _make_request(method, path)
+ result = _sanitize_body(request, b'{"private_key":"secret"', "application/json")
+ assert result == _REDACTED
+
+ def test_malformed_json_unrelated_request_keeps_current_behavior(self) -> None:
+ request = _make_request("POST", "/v1/tasks")
+ result = _sanitize_body(request, b'{"private_key":"secret"', "application/json")
+ assert result == '{"private_key":"secret"'
+
+ def test_get_credential_path_is_logged_normally(self) -> None:
+ request = _make_request("GET", "/api/v1/credentials/cred_123")
+ result = _sanitize_body(request, b'{"status":"ok"}', "application/json")
+ assert result == '{"status":"ok"}'
+
@pytest.mark.parametrize(
("method", "path"),
[
diff --git a/tests/unit/test_skyvern_credential_vault_service.py b/tests/unit/test_skyvern_credential_vault_service.py
index dce65669c..13bb574db 100644
--- a/tests/unit/test_skyvern_credential_vault_service.py
+++ b/tests/unit/test_skyvern_credential_vault_service.py
@@ -1,5 +1,7 @@
+import asyncio
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
+from unittest.mock import AsyncMock, MagicMock
import pytest
from cryptography.fernet import Fernet
@@ -18,6 +20,8 @@ from skyvern.forge.sdk.services.credential.skyvern_credential_vault_service impo
SkyvernCredentialVaultService,
)
+_UNSET = object()
+
class _FakeCredentialRepository:
def __init__(self) -> None:
@@ -93,6 +97,11 @@ class _FailingUpdateCredentialRepository(_FakeCredentialRepository):
raise RuntimeError("database unavailable")
+class _CancelledUpdateCredentialRepository(_FakeCredentialRepository):
+ async def update_credential_vault_data(self, **kwargs: object) -> Credential:
+ raise asyncio.CancelledError()
+
+
class _CapturingCreateCredentialRepository(_FakeCredentialRepository):
def __init__(self) -> None:
super().__init__()
@@ -103,14 +112,27 @@ class _CapturingCreateCredentialRepository(_FakeCredentialRepository):
return await super().create_credential(**kwargs)
-def _password_request(name: str = "Login", password: str = "secret-password") -> CreateCredentialRequest:
+def _password_request(
+ name: str = "Login",
+ password: str = "secret-password",
+ metadata: dict[str, str] | None | object = _UNSET,
+) -> CreateCredentialRequest:
+ credential_data: dict[str, object] = {
+ "username": "[email protected]",
+ "password": password,
+ }
+ if metadata is not _UNSET:
+ credential_data["metadata"] = metadata
return CreateCredentialRequest(
name=name,
credential_type=CredentialType.PASSWORD,
- credential=NonEmptyPasswordCredential(username="[email protected]", password=password),
+ credential=NonEmptyPasswordCredential.model_validate(credential_data),
)
+_PASSWORD_METADATA = {"tenant": "north"}
+
+
@pytest.mark.asyncio
async def test_skyvern_credential_vault_round_trips_encrypted_password(tmp_path, monkeypatch) -> None:
monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_PATH", str(tmp_path))
@@ -140,6 +162,24 @@ async def test_skyvern_credential_vault_round_trips_encrypted_password(tmp_path,
assert not item_path.exists()
+async def test_skyvern_credential_vault_round_trips_password_metadata(tmp_path, monkeypatch) -> None:
+ monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_PATH", str(tmp_path))
+ monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_KEY", None)
+ monkeypatch.setattr(app.DATABASE, "credentials", _FakeCredentialRepository())
+
+ service = SkyvernCredentialVaultService()
+
+ item = await service.get_credential_item(
+ await service.create_credential(
+ "org_test",
+ _password_request(name="Metadata Login", metadata=_PASSWORD_METADATA),
+ )
+ )
+
+ assert item.credential.metadata == _PASSWORD_METADATA
+
+
@pytest.mark.asyncio
async def test_skyvern_credential_vault_update_creates_new_item_and_cleans_old(tmp_path, monkeypatch) -> None:
monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_PATH", str(tmp_path))
@@ -148,7 +188,7 @@ async def test_skyvern_credential_vault_update_creates_new_item_and_cleans_old(t
monkeypatch.setattr(app.DATABASE, "credentials", _FakeCredentialRepository())
service = SkyvernCredentialVaultService()
- request = _password_request(password="old-password")
+ request = _password_request(password="old-password", metadata=_PASSWORD_METADATA)
credential = await service.create_credential("org_test", request)
old_item_id = credential.item_id
@@ -164,6 +204,7 @@ async def test_skyvern_credential_vault_update_creates_new_item_and_cleans_old(t
item = await service.get_credential_item(updated)
assert item.name == "Login Updated"
assert item.credential.password == "new-password"
+ assert item.credential.metadata == _PASSWORD_METADATA
@pytest.mark.asyncio
@@ -200,6 +241,72 @@ async def test_skyvern_credential_vault_cleans_new_item_when_update_db_fails(tmp
assert list(tmp_path.glob("*.bin")) == [old_item_path]
+async def test_skyvern_credential_vault_cleans_new_item_when_update_db_is_cancelled(tmp_path, monkeypatch) -> None:
+ monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_PATH", str(tmp_path))
+ monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_KEY", None)
+ monkeypatch.setattr(app.DATABASE, "credentials", _CancelledUpdateCredentialRepository())
+
+ service = SkyvernCredentialVaultService()
+ credential = await service.create_credential("org_test", _password_request(password="old-password"))
+ old_item_path = tmp_path / f"{credential.item_id}.bin"
+
+ with pytest.raises(asyncio.CancelledError):
+ await service.update_credential(
+ credential,
+ _password_request(name="Login Updated", password="new-password"),
+ )
+
+ assert old_item_path.exists()
+ assert list(tmp_path.glob("*.bin")) == [old_item_path]
+
+
+async def test_skyvern_credential_vault_enqueues_durable_cleanup_when_inline_unlink_fails(
+ tmp_path, monkeypatch
+) -> None:
+ monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_PATH", str(tmp_path))
+ monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_KEY", None)
+ monkeypatch.setattr(app.DATABASE, "credentials", _CancelledUpdateCredentialRepository())
+
+ orphaned_hook = AsyncMock()
+ monkeypatch.setattr(app.AGENT_FUNCTION, "on_credential_item_orphaned", orphaned_hook)
+
+ service = SkyvernCredentialVaultService()
+ credential = await service.create_credential("org_test", _password_request(password="old-password"))
+ old_item_path = tmp_path / f"{credential.item_id}.bin"
+
+ unlink = MagicMock(side_effect=OSError("unlink blew up"))
+ monkeypatch.setattr(service, "_unlink_item_file", unlink)
+
+ with pytest.raises(asyncio.CancelledError):
+ await service.update_credential(
+ credential,
+ _password_request(name="Login Updated", password="new-password"),
+ )
+
+ orphaned_hook.assert_awaited_once()
+ _, kwargs = orphaned_hook.await_args
+ assert kwargs["organization_id"] == "org_test"
+ assert kwargs["vault_type"] == CredentialVaultType.SKYVERN
+ new_item_id = kwargs["item_id"]
+ assert new_item_id != credential.item_id
+ unlink.assert_called_once_with(new_item_id)
+ assert old_item_path.exists()
+ assert (tmp_path / f"{new_item_id}.bin").exists()
+
+
+async def test_skyvern_credential_vault_post_delete_reports_unlink_failure(tmp_path, monkeypatch) -> None:
+ monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_PATH", str(tmp_path))
+ monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_KEY", None)
+
+ service = SkyvernCredentialVaultService()
+ monkeypatch.setattr(service, "_unlink_item_file", MagicMock(side_effect=OSError("unlink blew up")))
+
+ assert await service.post_delete_credential_item("creditem_missing") is False
+
+
@pytest.mark.asyncio
async def test_skyvern_credential_vault_rejects_invalid_item_id(tmp_path, monkeypatch) -> None:
monkeypatch.setattr(settings, "LOCAL_CREDENTIAL_VAULT_PATH", str(tmp_path))
diff --git a/tests/unit/test_task_v2_per_tab_screenshots.py b/tests/unit/test_task_v2_per_tab_screenshots.py
index b5fddf747..23d8ce62a 100644
--- a/tests/unit/test_task_v2_per_tab_screenshots.py
+++ b/tests/unit/test_task_v2_per_tab_screenshots.py
@@ -24,6 +24,10 @@ def _fake_page(url: str = "https://example.com") -> AsyncMock:
return page
+def _fake_browser_state(list_valid_pages: AsyncMock, engine_selection: object | None = None) -> SimpleNamespace:
+ return SimpleNamespace(list_valid_pages=list_valid_pages, engine_selection=engine_selection)
+
+
@pytest.fixture
def artifact_manager(monkeypatch: pytest.MonkeyPatch) -> AsyncMock:
manager = SimpleNamespace(create_thought_artifact=AsyncMock(return_value="art_id"))
@@ -41,7 +45,7 @@ def stub_screenshot(monkeypatch: pytest.MonkeyPatch) -> AsyncMock:
@pytest.mark.asyncio
async def test_captures_one_artifact_per_open_tab(artifact_manager: AsyncMock) -> None:
pages = [_fake_page() for _ in range(3)]
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=pages))
+ browser_state = _fake_browser_state(AsyncMock(return_value=pages))
thought = _fake_thought()
captured = await _persist_completion_tab_screenshots(browser_state, thought)
@@ -59,7 +63,7 @@ async def test_captures_one_artifact_per_open_tab(artifact_manager: AsyncMock) -
async def test_enumerates_without_closing_tabs(artifact_manager: AsyncMock) -> None:
# The whole point of the feature is to prove tabs are still open, so enumeration must use
# max_pages=0 to avoid list_valid_pages' close-oldest behavior.
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=[_fake_page()]))
+ browser_state = _fake_browser_state(AsyncMock(return_value=[_fake_page()]))
await _persist_completion_tab_screenshots(browser_state, _fake_thought())
@@ -70,7 +74,7 @@ async def test_enumerates_without_closing_tabs(artifact_manager: AsyncMock) -> N
async def test_respects_max_cap(artifact_manager: AsyncMock, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(task_v2_service.settings, "MAX_COMPLETION_TAB_SCREENSHOTS_PER_TASK_V2", 2)
pages = [_fake_page() for _ in range(5)]
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=pages))
+ browser_state = _fake_browser_state(AsyncMock(return_value=pages))
captured = await _persist_completion_tab_screenshots(browser_state, _fake_thought())
@@ -80,7 +84,7 @@ async def test_respects_max_cap(artifact_manager: AsyncMock, monkeypatch: pytest
@pytest.mark.asyncio
async def test_enumeration_failure_returns_zero(artifact_manager: AsyncMock) -> None:
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(side_effect=RuntimeError("boom")))
+ browser_state = _fake_browser_state(AsyncMock(side_effect=RuntimeError("boom")))
captured = await _persist_completion_tab_screenshots(browser_state, _fake_thought())
@@ -93,7 +97,7 @@ async def test_single_tab_failure_does_not_abort_others(
artifact_manager: AsyncMock, stub_screenshot: AsyncMock
) -> None:
pages = [_fake_page() for _ in range(3)]
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=pages))
+ browser_state = _fake_browser_state(AsyncMock(return_value=pages))
stub_screenshot.side_effect = [[b"a"], RuntimeError("screenshot fail"), [b"c"]]
captured = await _persist_completion_tab_screenshots(browser_state, _fake_thought())
@@ -106,7 +110,7 @@ async def test_single_tab_failure_does_not_abort_others(
async def test_bring_to_front_failure_still_captures(artifact_manager: AsyncMock) -> None:
pages = [_fake_page(), _fake_page()]
pages[0].bring_to_front = AsyncMock(side_effect=RuntimeError("no front"))
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=pages))
+ browser_state = _fake_browser_state(AsyncMock(return_value=pages))
captured = await _persist_completion_tab_screenshots(browser_state, _fake_thought())
@@ -118,7 +122,7 @@ async def test_bring_to_front_failure_still_captures(artifact_manager: AsyncMock
@pytest.mark.asyncio
async def test_single_tab_is_skipped(artifact_manager: AsyncMock) -> None:
# The lone active tab is already persisted by the completion check; re-capturing it is waste.
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=[_fake_page()]))
+ browser_state = _fake_browser_state(AsyncMock(return_value=[_fake_page()]))
captured = await _persist_completion_tab_screenshots(browser_state, _fake_thought())
@@ -131,7 +135,7 @@ async def test_persist_failure_does_not_propagate(artifact_manager: AsyncMock) -
# Best-effort: a transient artifact/DB write failure must not flip an already-successful run.
artifact_manager.side_effect = RuntimeError("db down")
pages = [_fake_page(), _fake_page()]
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=pages))
+ browser_state = _fake_browser_state(AsyncMock(return_value=pages))
captured = await _persist_completion_tab_screenshots(browser_state, _fake_thought())
@@ -145,17 +149,21 @@ async def test_slow_tab_times_out_and_does_not_block_others(
) -> None:
monkeypatch.setattr(task_v2_service.settings, "BROWSER_SCREENSHOT_TIMEOUT_MS", 50)
slow_page, fast_page = _fake_page("https://slow"), _fake_page("https://fast")
+ pinned_selection = object()
+ seen_selections = []
- async def fake_take(page: object, scroll: bool = False) -> list[bytes]:
+ async def fake_take(page: object, scroll: bool = False, engine_selection: object | None = None) -> list[bytes]:
+ seen_selections.append(engine_selection)
if getattr(page, "url", "") == "https://slow":
await asyncio.sleep(5)
return [b"png-bytes"]
monkeypatch.setattr(task_v2_service.SkyvernFrame, "take_split_screenshots", fake_take)
- browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=[slow_page, fast_page]))
+ browser_state = _fake_browser_state(AsyncMock(return_value=[slow_page, fast_page]), pinned_selection)
captured = await _persist_completion_tab_screenshots(browser_state, _fake_thought())
# The hung tab is abandoned at the per-tab deadline; the healthy tab is still captured.
assert captured == 1
assert artifact_manager.await_count == 1
+ assert seen_selections and all(selection is pinned_selection for selection in seen_selections)
diff --git a/tests/unit/test_totp_identifier_fallback.py b/tests/unit/test_totp_identifier_fallback.py
index bc370d487..561a69b49 100644
--- a/tests/unit/test_totp_identifier_fallback.py
+++ b/tests/unit/test_totp_identifier_fallback.py
@@ -51,10 +51,14 @@ async def test_register_credential_parameter_uses_db_totp_identifier(monkeypatch
def __init__(self) -> None:
self.credentials = FakeCredentialRepo()
+ process_item = AsyncMock(side_effect=lambda **kwargs: kwargs["credential_item"])
fake_app = SimpleNamespace(
DATABASE=FakeDatabase(),
CREDENTIAL_VAULT_SERVICES={CredentialVaultType.BITWARDEN: FakeCredentialService()},
- AGENT_FUNCTION=SimpleNamespace(parse_enterprise_totp_secret=AsyncMock(return_value=None)),
+ AGENT_FUNCTION=SimpleNamespace(
+ parse_enterprise_totp_secret=AsyncMock(return_value=None),
+ process_registered_credential_item=process_item,
+ ),
)
monkeypatch.setattr(cm, "app", fake_app)
@@ -75,7 +79,10 @@ async def test_register_credential_parameter_uses_db_totp_identifier(monkeypatch
assert context.get_resolved_credential_parameter_id("credential_param") == "cred-1"
-async def _register_with_credential(monkeypatch: pytest.MonkeyPatch, credential: object) -> WorkflowRunContext:
+async def _register_with_credential(
+ monkeypatch: pytest.MonkeyPatch,
+ credential: object,
+) -> WorkflowRunContext:
db_credential = SimpleNamespace(
credential_id="cred-1",
organization_id="org-1",
@@ -100,10 +107,14 @@ async def _register_with_credential(monkeypatch: pytest.MonkeyPatch, credential:
def __init__(self) -> None:
self.credentials = FakeCredentialRepo()
+ process_item = AsyncMock(side_effect=lambda **kwargs: kwargs["credential_item"])
fake_app = SimpleNamespace(
DATABASE=FakeDatabase(),
CREDENTIAL_VAULT_SERVICES={CredentialVaultType.BITWARDEN: FakeCredentialService()},
- AGENT_FUNCTION=SimpleNamespace(parse_enterprise_totp_secret=AsyncMock(return_value=None)),
+ AGENT_FUNCTION=SimpleNamespace(
+ parse_enterprise_totp_secret=AsyncMock(return_value=None),
+ process_registered_credential_item=process_item,
+ ),
)
monkeypatch.setattr(cm, "app", fake_app)
@@ -198,6 +209,19 @@ async def test_register_credit_card_flattens_billing_fields(
assert context.secrets[values["metadata_customer_id"]] == "cus_123"
+async def test_register_password_flattens_metadata_like_credit_card(monkeypatch: pytest.MonkeyPatch) -> None:
+ credential = PasswordCredential(
+ username="[email protected]",
+ password="secret",
+ metadata={"customer_id": "cus_123"},
+ )
+ context = await _register_with_credential(monkeypatch, credential)
+
+ values = context.values["credential_param"]
+ assert context.secrets[values["metadata_customer_id"]] == "cus_123"
+
+
@pytest.mark.asyncio
async def test_register_credit_card_preserves_colliding_metadata_keys(
monkeypatch: pytest.MonkeyPatch,
diff --git a/tests/unit/test_validation_error_formatting.py b/tests/unit/test_validation_error_formatting.py
index 2bf547e14..dc9522a4a 100644
--- a/tests/unit/test_validation_error_formatting.py
+++ b/tests/unit/test_validation_error_formatting.py
@@ -1,10 +1,14 @@
from __future__ import annotations
+import json
from unittest.mock import MagicMock
+import pytest
+from fastapi.exceptions import RequestValidationError
from pydantic import BaseModel, ValidationError, field_validator
+from starlette.requests import Request
-from skyvern.forge.api_app import format_validation_errors
+from skyvern.forge.api_app import create_api_app, format_validation_errors
class _DummyModel(BaseModel):
@@ -126,3 +130,69 @@ class TestFormatValidationErrors:
result = format_validation_errors(mock_exc)
assert result == "email: field required"
assert "body" not in result
+
+
+async def test_request_validation_handler_does_not_echo_input() -> None:
+ secret_input = "submitted-private-key-material"
+ secret_context = "submitted-context-material"
+ exc = RequestValidationError(
+ [
+ {
+ "type": "value_error",
+ "loc": ("body", "credential", "secret_value"),
+ "msg": "Value error, invalid secret value",
+ "input": secret_input,
+ "ctx": {"error": ValueError(secret_context), "submitted": secret_context},
+ }
+ ]
+ )
+ app = create_api_app()
+
+ handler = app.exception_handlers[RequestValidationError]
+ request = MagicMock(spec=Request)
+ request.url.path = "/v1/credentials/cred_x/passkey"
+ response = await handler(request, exc)
+ response_body = json.loads(response.body)
+
+ assert response.status_code == 422
+ assert secret_input not in response.body.decode()
+ assert secret_context not in response.body.decode()
+ assert isinstance(response_body["detail"], list)
+ assert response_body == {
+ "detail": [
+ {
+ "loc": ["body", "credential", "secret_value"],
+ "msg": "Value error, invalid secret value",
+ "type": "value_error",
+ }
+ ]
+ }
+
+
+async def test_request_validation_handler_preserves_input_for_non_credential_routes() -> None:
+ echoed_input = "workflow-parameter-value"
+ exc = RequestValidationError(
+ [
+ {
+ "type": "less_than",
+ "loc": ("body", "parameters", "count"),
+ "msg": "Input should be less than 3",
+ "input": echoed_input,
+ "ctx": {"lt": 3},
+ }
+ ]
+ )
+ app = create_api_app()
+
+ handler = app.exception_handlers[RequestValidationError]
+ request = MagicMock(spec=Request)
+ request.url.path = "/v1/workflows"
+ response = await handler(request, exc)
+ response_body = json.loads(response.body)
+
+ assert response.status_code == 422
+ detail = response_body["detail"][0]
+ assert detail["input"] == echoed_input
+ assert detail["ctx"] == {"lt": 3}