Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Keep Copilot in scope when earlier turns drift, and add account management to its scope (SKY-17731) (#8814)
Andrew Neilsonddff08470e80
diff --git a/skyvern-frontend/src/routes/workflows/copilot/QueuedMessageStrip.tsx b/skyvern-frontend/src/routes/workflows/copilot/QueuedMessageStrip.tsxindex 2c4149fb5..c3ccb0bd8 100644--- a/skyvern-frontend/src/routes/workflows/copilot/QueuedMessageStrip.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/QueuedMessageStrip.tsx@@ -1,4 +1,9 @@-import { Cross2Icon, FileIcon, Pencil1Icon } from "@radix-ui/react-icons";+import {+ Cross2Icon,+ FileIcon,+ PaperPlaneIcon,+ Pencil1Icon,+} from "@radix-ui/react-icons";import type { ReactNode } from "react";import { CornerDownRightIcon } from "@/components/icons/CornerDownRightIcon";@@ -13,6 +18,8 @@ type Props = {// Omitted for a product action's receipt, which has no words of the user's to edit.onEdit?: () => void;onRemove: () => void;+ // Present while the running turn can take the message now instead of after it ends.+ onSendNow?: () => void;};export function QueuedMessageStrip({@@ -20,6 +27,7 @@ export function QueuedMessageStrip({attachments,onEdit,onRemove,+ onSendNow,}: Props) {return (<div@@ -43,6 +51,23 @@ export function QueuedMessageStrip({</span></span>) : null}+ {onSendNow ? (+ <TooltipProvider>+ <ControlTooltip+ content="Copilot reads it at its next step instead of after this turn"+ side="top"+ >+ <button+ type="button"+ onClick={onSendNow}+ className="flex h-6 shrink-0 items-center gap-1 rounded px-1.5 text-[12px] text-foreground hover:bg-slate-elevation3 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"+ >+ <PaperPlaneIcon aria-hidden className="h-3 w-3" />+ Send now+ </button>+ </ControlTooltip>+ </TooltipProvider>+ ) : null}<TooltipProvider><span className="flex shrink-0 opacity-55 transition-opacity group-focus-within/queued:opacity-100 group-hover/queued:opacity-100">{onEdit ? (diff --git a/skyvern-frontend/src/routes/workflows/copilot/SteerReceipt.tsx b/skyvern-frontend/src/routes/workflows/copilot/SteerReceipt.tsxnew file mode 100644index 000000000..0c3dea5a9--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/SteerReceipt.tsx@@ -0,0 +1,18 @@+type Props = {+ text: string;+ // True until the running turn hands the message to the model.+ sending: boolean;+};++export function SteerReceipt({ text, sending }: Props) {+ return (+ <div data-testid="copilot-steer-receipt" className="flex justify-end">+ <div className="max-w-[85%] rounded-xl border border-white/5 bg-slate-elevation4 px-3.5 py-2.5 text-[13.5px] leading-[1.5] text-foreground">+ <p className="whitespace-pre-wrap [overflow-wrap:anywhere]">{text}</p>+ <p role="status" className="mt-1 text-[11px] text-muted-foreground">+ {sending ? "Sending now…" : "Sent while Copilot was working"}+ </p>+ </div>+ </div>+ );+}diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsxindex 48ac44672..f1c1a3fc7 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx@@ -3879,3 +3879,115 @@ describe("WorkflowCopilotChat — delivered output files", () => {expect(screen.queryByRole("button", { name: "quarterly.xlsx" })).toBeNull();});});++describe("WorkflowCopilotChat — send now delivers a queued message into the running turn", () => {+ const steerPosts = () =>+ cancelPost.mock.calls.filter(+ ([path]) => (path as string) === "/workflow/copilot/steer",+ ) as unknown as [string, Record<string, string>][];++ async function queueDuringTurn(text: string) {+ await renderChat();+ await submit("build the workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ const call = streamCalls[0]!;+ await act(async () => {+ call.onMessage({ ...turnStart(), workflow_copilot_chat_id: "chat-1" });+ });+ await submit(text);+ return call;+ }++ async function sendNow() {+ await act(async () => {+ fireEvent.click(queuedStrip().getByRole("button", { name: "Send now" }));+ });+ }++ it("posts to the running turn and shows the message where the model received it", async () => {+ const call = await queueDuringTurn("also grab the page title");+ await sendNow();++ const [, body] = steerPosts()[0]!;+ expect(body).toMatchObject({+ workflow_copilot_chat_id: "chat-1",+ cancel_token: (call.body as unknown as { cancel_token: string })+ .cancel_token,+ message: "also grab the page title",+ });+ expect(screen.queryByTestId("copilot-queued-message")).toBeNull();+ expect(screen.getByText("Sending now…")).toBeTruthy();++ await act(async () => {+ call.onMessage({+ type: "steer_delivered",+ turn_id: "turn-1",+ steer_messages: [+ {+ steer_id: body.steer_id,+ text: "also grab the page title",+ created_at: "2026-05-25T00:00:01Z",+ delivered_at: "2026-05-25T00:00:02Z",+ },+ ],+ });+ });+ expect(screen.getByText("Sent while Copilot was working")).toBeTruthy();++ await completeOldestStream("Built it with the title.");+ await act(async () => {});++ expect(postStreaming).toHaveBeenCalledTimes(1);+ expect(screen.getAllByTestId("copilot-steer-receipt")).toHaveLength(1);+ expect(screen.getByText("also grab the page title")).toBeTruthy();+ });++ it("sends the message as the next turn when the running turn ends before receiving it", async () => {+ await queueDuringTurn("also grab the page title");+ await sendNow();+ expect(steerPosts()).toHaveLength(1);++ await completeOldestStream("Built it.");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));++ expect(streamCalls[1]?.body.message).toBe("also grab the page title");+ expect(screen.queryAllByTestId("copilot-steer-receipt")).toHaveLength(0);+ });++ it("sends a message once when its request fails without a response but the turn still receives it", async () => {+ const call = await queueDuringTurn("also grab the page title");+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await sendNow();+ const [, body] = steerPosts()[0]!;++ await act(async () => {+ call.onMessage({+ type: "steer_delivered",+ turn_id: "turn-1",+ steer_messages: [+ {+ steer_id: body.steer_id,+ text: "also grab the page title",+ created_at: "2026-05-25T00:00:01Z",+ delivered_at: "2026-05-25T00:00:02Z",+ },+ ],+ });+ });+ await completeOldestStream("Built it with the title.");+ await act(async () => {});++ expect(postStreaming).toHaveBeenCalledTimes(1);+ });++ it("hands an undelivered message back to the composer on stop", async () => {+ await queueDuringTurn("also grab the page title");+ await sendNow();++ await act(async () => useCopilotActionStore.getState().requestCancel());+ await completeOldestStream("stopped");++ expect(textarea().value).toBe("also grab the page title");+ expect(postStreaming).toHaveBeenCalledTimes(1);+ });+});diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsxindex 8625f005c..b6be207c9 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx@@ -138,6 +138,9 @@ import {WorkflowCopilotQuestionRequired,WorkflowCopilotQuestionResolved,CopilotProductAction,+ CopilotSteerMessage,+ WorkflowCopilotSteerDeliveredUpdate,+ WorkflowCopilotSteerRequest,} from "./workflowCopilotTypes";import { WorkflowCopilotHistory } from "./WorkflowCopilotHistory";import { AutoAcceptChip } from "./AutoAcceptChip";@@ -172,6 +175,7 @@ import {type CopilotComposerStatus,} from "./CopilotWorkingStatus";import { QueuedMessageStrip } from "./QueuedMessageStrip";+import { SteerReceipt } from "./SteerReceipt";import {RecordingRefinementProgressCard,type RecordingRefinementStatus,@@ -959,6 +963,25 @@ type QueuedPrompt = {recording?: RecordingSnapshot;};+// "Send now" delivers words only: audio, files and recording state ride on a turn's own request.+function canSendQueuedPromptNow(prompt: QueuedPrompt): boolean {+ return (+ prompt.origin === "typed" &&+ prompt.reason === "working" &&+ !prompt.audioBlob &&+ !prompt.attachments?.length &&+ !prompt.recording?.recording_in_progress+ );+}++// A message sent into the running turn that its loop has not handed to the model yet.+type PendingSteer = {+ steerId: string;+ text: string;+ cancelToken: string;+ sentAt: string;+};+type SendOptions = {selectedConnectedAccountId?: string;queuedMessageId?: string;@@ -992,7 +1015,8 @@ type WorkflowCopilotSsePayload =| WorkflowCopilotCredentialRequiredUpdate| WorkflowCopilotCredentialPauseResolvedUpdate| WorkflowCopilotQuestionRequired- | WorkflowCopilotQuestionResolved;+ | WorkflowCopilotQuestionResolved+ | WorkflowCopilotSteerDeliveredUpdate;// The live pause frame is a structural superset of the card's frame; only// reason needs narrowing (the card tolerates unknown tokens either way).@@ -1980,6 +2004,10 @@ export function WorkflowCopilotChat({} | null>(null);const pendingMessageId = useRef<string | null>(null);const pendingCancelToken = useRef<string | null>(null);+ const pendingSteersRef = useRef<PendingSteer[]>([]);+ // The running turn's chat, which a brand-new chat learns from turn_start rather than at the end.+ const turnChatIdRef = useRef<string | null>(null);+ const [pendingSteers, setPendingSteers] = useState<PendingSteer[]>([]);// Read by cancelSend, which must not re-create on every render of the arming signal.const turnObservablyRunningRef = useRef(false);const stopArmTimer = useRef<ReturnType<typeof setTimeout> | null>(null);@@ -2260,6 +2288,8 @@ export function WorkflowCopilotChat({streamingAbortController.current?.abort();streamingAbortController.current = null;inFlightRef.current = false;+ pendingSteersRef.current = [];+ setPendingSteers([]);canonicalRecoveryAbort.current?.abort();canonicalRecoveryAbort.current = null;canonicalRecoveryInFlight.current = false;@@ -3217,6 +3247,8 @@ export function WorkflowCopilotChat({streamingAbortController.current?.abort();streamingAbortController.current = null;inFlightRef.current = false;+ pendingSteersRef.current = [];+ setPendingSteers([]);setIsLoading(false);setRecoveredPauseFrames([]);setQuestionInteractions([]);@@ -6648,6 +6680,98 @@ export function WorkflowCopilotChat({[adjustTextareaHeight, returnFilesToTray, updateQueuedPrompt],);+ // Removes and returns the matching steers, so whichever of delivery, refusal or the turn's end+ // reaches a steer first is the only one that acts on it.+ const takePendingSteers = useCallback(+ (match: (steer: PendingSteer) => boolean) => {+ const taken = pendingSteersRef.current.filter(match);+ if (taken.length > 0) {+ pendingSteersRef.current = pendingSteersRef.current.filter(+ (steer) => !match(steer),+ );+ setPendingSteers(pendingSteersRef.current);+ }+ return taken;+ },+ [],+ );++ const restoreSteersToComposer = useCallback((steers: PendingSteer[]) => {+ if (steers.length === 0) return;+ const restore = (current: string) =>+ appendQueuedText(steers.map((steer) => steer.text).join("\n"), current);+ if (answeringQuestionRef.current) setPromptHeldForQuestion(restore);+ else setInputValue(restore);+ }, []);++ // A steer the turn never received drains as the next turn, ahead of anything typed since.+ const requeueSteers = useCallback(+ (steers: PendingSteer[]) => {+ if (steers.length === 0) return;+ const text = steers.map((steer) => steer.text).join("\n");+ const queued = queuedPromptRef.current;+ if (queued === null) {+ updateQueuedPrompt({+ origin: "typed",+ id: crypto.randomUUID(),+ content: text,+ reason: "working",+ });+ } else if (queued.origin === "typed") {+ updateQueuedPrompt({+ ...queued,+ content: appendQueuedText(text, queued.content),+ });+ } else {+ restoreSteersToComposer(steers);+ }+ },+ [restoreSteersToComposer, updateQueuedPrompt],+ );++ const sendQueuedPromptNow = useCallback(async () => {+ const prompt = queuedPromptRef.current;+ const cancelToken = pendingCancelToken.current;+ const chatId = turnChatIdRef.current;+ if (!prompt || !canSendQueuedPromptNow(prompt) || !cancelToken || !chatId)+ return;+ const steer: PendingSteer = {+ steerId: crypto.randomUUID(),+ text: prompt.content,+ cancelToken,+ sentAt: new Date().toISOString(),+ };+ updateQueuedPrompt(null);+ pendingSteersRef.current = [...pendingSteersRef.current, steer];+ setPendingSteers(pendingSteersRef.current);+ try {+ const client = await getClient(credentialGetter, "sans-api-v1");+ await client.post("/workflow/copilot/steer", {+ workflow_copilot_chat_id: chatId,+ cancel_token: cancelToken,+ steer_id: steer.steerId,+ message: steer.text,+ } satisfies WorkflowCopilotSteerRequest);+ } catch (error) {+ const status = (error as { response?: { status?: number } })?.response+ ?.status;+ // Without a response the server may have recorded it, so delivery or the turn's end settles it.+ if (status === undefined) return;+ const refused = takePendingSteers(+ (pending) => pending.steerId === steer.steerId,+ );+ requeueSteers(refused);+ // 409 means the turn ended or is full, so the message simply goes out as the next turn.+ if (refused.length > 0 && status !== 409) {+ toast({+ variant: "destructive",+ title: "Couldn't send now",+ description: "Your message is queued for when Copilot finishes.",+ });+ }+ }+ }, [credentialGetter, requeueSteers, takePendingSteers, updateQueuedPrompt]);+const cancelSend = useCallback(async (source: WorkflowCopilotCancelSource,@@ -6661,6 +6785,7 @@ export function WorkflowCopilotChat({// the composer synchronously, before isLoading flips and the drain effect// would otherwise send it as a fresh turn.restoreQueuedPromptToComposer();+ restoreSteersToComposer(takePendingSteers(() => true));if (requireArmed && !turnObservablyRunningRef.current) return;@@ -6759,6 +6884,8 @@ export function WorkflowCopilotChat({isCopilotTurnCurrent,logCopilotRequestFailure,restoreQueuedPromptToComposer,+ restoreSteersToComposer,+ takePendingSteers,],);@@ -7628,6 +7755,7 @@ export function WorkflowCopilotChat({const cancelToken = crypto.randomUUID();pendingCancelToken.current = cancelToken;+ turnChatIdRef.current = null;try {if (workflowPermanentId)sessionStorage.setItem(@@ -8682,6 +8810,9 @@ export function WorkflowCopilotChat({}// A new turn can't carry the prior turn's dead resume_token.setLivePauseFrame(null);+ turnChatIdRef.current =+ payload.workflow_copilot_chat_id ??+ workflowCopilotChatIdRef.current;// Move the pre-submit canvas snapshot into the per-turn// map keyed by the BE-assigned turn_id; cap the map so a// long-running chat does not retain every turn's snapshot.@@ -8716,6 +8847,14 @@ export function WorkflowCopilotChat({case "codegen_progress":applyStoredNarrativeEvent(payload);return false;+ case "steer_delivered": {+ const delivered = new Set(+ payload.steer_messages.map((item) => item.steer_id),+ );+ takePendingSteers((steer) => delivered.has(steer.steerId));+ applyStoredNarrativeEvent(payload);+ return false;+ }case "workflow_draft": {// The draft frame summarizes the whole draft; the newest block// is the one being worked on.@@ -8886,6 +9025,9 @@ export function WorkflowCopilotChat({queryKey: ["workflowSchedules", workflowPermanentId],});const current = isCopilotOwnerCurrent(reservation);+ const undelivered = takePendingSteers(+ (steer) => steer.cancelToken === cancelToken,+ );const armRecovery = shouldArmRecovery();const retainReservation =armRecovery && pendingCanonicalRecovery.current !== null;@@ -8917,6 +9059,10 @@ export function WorkflowCopilotChat({// an error/thrown path already did, but a cancel or abort reaches only// here. A no-op once a genuine success cleared the ref above.rollbackPendingTerminalContinuation();+ // Steers this turn never received. A severed stream may still deliver them server-side,+ // so those go back to the composer rather than out again on their own.+ if (armRecovery) restoreSteersToComposer(undelivered);+ else requeueSteers(undelivered);setIsLoading(false);// Backstop: a turn that ends without a terminal run_outcome (thrown// stream) would otherwise leave a live poll running past the run.@@ -8940,6 +9086,9 @@ export function WorkflowCopilotChat({[acceptUnresolved,applyStoredNarrativeEvent,+ takePendingSteers,+ requeueSteers,+ restoreSteersToComposer,captureCopilotEvent,createCanonicalRecovery,onWorkflowPersisted,@@ -9887,6 +10036,16 @@ export function WorkflowCopilotChat({const showQueuedStrip = Boolean(queuedPrompt && !messages.some((message) => message.id === queuedPrompt.id),);+ // A turn parked on a question or credential card cannot read the message until the user answers.+ const canSendNow = Boolean(+ queuedPrompt &&+ canSendQueuedPromptNow(queuedPrompt) &&+ turnObservablyRunning &&+ !isStopping &&+ !hasPendingQuestion &&+ livePauseFrame === null &&+ turnChatIdRef.current,+ );const showsStopGlyph =isStopping || (turnObservablyRunning && !hasComposerText);const authoringBlocksComposerAction = authoringInProgress && !showsStopGlyph;@@ -9949,11 +10108,25 @@ export function WorkflowCopilotChat({});const liveTurnShown =narrative.turnId !== null && narrative.terminal === null;+ // A sent message renders where the model received it; a pending one sits at the turn's newest step.+ const anchoredSteer = (steer: CopilotSteerMessage): AnchoredTurnItem => ({+ key: `steer-${steer.steer_id}`,+ toolCallId: null,+ at: steer.delivered_at,+ node: <SteerReceipt text={steer.text} sending={false} />,+ });if (liveTurnShown) {liveAnchored.push(...questionInteractions.filter((item) => item.turn_id === narrative.turnId).map(anchoredQuestion),+ ...narrative.steerMessages.map(anchoredSteer),+ ...pendingSteers.map((steer) => ({+ key: `steer-${steer.steerId}`,+ toolCallId: null,+ at: steer.sentAt,+ node: <SteerReceipt text={steer.text} sending />,+ })),);}// Straight to the answer path rather than through handleSend, whose authoring and YAML-commit@@ -10419,7 +10592,10 @@ export function WorkflowCopilotChat({node: autoBoundCard,});}- anchored.push(...turnInteractions.map(anchoredQuestion));+ anchored.push(+ ...turnInteractions.map(anchoredQuestion),+ ...message.narrative.steerMessages.map(anchoredSteer),+ );return (<divkey={message.id}@@ -11047,6 +11223,9 @@ export function WorkflowCopilotChat({: () => restoreQueuedPromptToComposer()}onRemove={() => restoreQueuedPromptToComposer({ keepText: false })}+ onSendNow={+ canSendNow ? () => void sendQueuedPromptNow() : undefined+ }/>) : null}<divdiff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.tsindex 6c4260f6e..d2caa70b9 100644--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts@@ -9,6 +9,7 @@ import {BudgetExpiryOutcome,ConnectedAccountChoice,CopilotResponseType,+ CopilotSteerMessage,DeliveredOutputFile,ProposalDisposition,RunOutcomeRole,@@ -20,6 +21,7 @@ import {WorkflowCopilotRunOutcomeUpdate,WorkflowCopilotStreamErrorUpdate,WorkflowCopilotStreamResponseUpdate,+ WorkflowCopilotSteerDeliveredUpdate,WorkflowCopilotToolCallUpdate,CodeWriteDiff,WorkflowCopilotToolResultUpdate,@@ -120,6 +122,7 @@ export type NarrativeEvent =| WorkflowCopilotToolCallUpdate| WorkflowCopilotToolResultUpdate| WorkflowCopilotCodegenProgressUpdate+ | WorkflowCopilotSteerDeliveredUpdate| CopilotBlockActionsEvent;// Block lifecycle states as observed via block_progress. The bubble groups@@ -455,6 +458,8 @@ export interface TurnNarrativeState {review: ReviewProjection | null;turnFacts: TurnFacts | null;budgetExpiry: BudgetExpiryState | null;+ // Messages the user sent into this turn, in the order the model received them.+ steerMessages: CopilotSteerMessage[];}export interface GoogleConnectionNotice {@@ -495,6 +500,7 @@ export const EMPTY_NARRATIVE: TurnNarrativeState = Object.freeze({review: null,turnFacts: null,budgetExpiry: null,+ steerMessages: [],}) as TurnNarrativeState;// Caps to keep long-running narrations from unbounded growth (and to keep@@ -1522,6 +1528,19 @@ export function applyNarrativeEvent(};}+ case "steer_delivered": {+ const known = new Set(prev.steerMessages.map((item) => item.steer_id));+ return {+ ...prev,+ steerMessages: [+ ...prev.steerMessages,+ ...event.steer_messages.filter((item) => !known.has(item.steer_id)),+ ],+ // A delivery can abort the model call those drafting frames described.+ codegenProgress: null,+ };+ }+case "tool_result": {const planItems = parseStringList(event.work_plan);const workPlan = planItems@@ -1598,6 +1617,10 @@ export function applyNarrativeEvent(return {...hydrated,blocks,+ steerMessages:+ hydrated.steerMessages.length > 0+ ? hydrated.steerMessages+ : prev.steerMessages,responseType: event.response_type ?? hydrated.responseType,cancelled: event.cancelled ?? hydrated.cancelled,proposalDisposition:@@ -1989,9 +2012,22 @@ export function hydrateNarrativeFromPayload(review: parseReviewProjection(payload.review),turnFacts,budgetExpiry,+ steerMessages: parseSteerMessages(payload.steerMessages),};}+function parseSteerMessages(value: unknown): CopilotSteerMessage[] {+ if (!Array.isArray(value)) return [];+ return value.filter(+ (item): item is CopilotSteerMessage =>+ typeof item === "object" &&+ item !== null &&+ typeof item.steer_id === "string" &&+ typeof item.text === "string" &&+ typeof item.delivered_at === "string",+ );+}+// History rows persisted before narrative_payload carried responseKind still// have the adjacent persisted turn_outcome; graft its response_kind so// clarify/refuse/recover history corrects retroactively. Build-kind rows usediff --git a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.tsindex 16c34f620..e4453f9fb 100644--- a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts@@ -181,6 +181,27 @@ export interface WorkflowCopilotCancelRequest {source: WorkflowCopilotCancelSource;}+export interface WorkflowCopilotSteerRequest {+ workflow_copilot_chat_id: string;+ cancel_token: string;+ steer_id: string;+ message: string;+}++// A message sent into a running turn, as the server screened and stored it.+export interface CopilotSteerMessage {+ steer_id: string;+ text: string;+ created_at: string;+ delivered_at: string | null;+}++export interface WorkflowCopilotSteerDeliveredUpdate {+ type: "steer_delivered";+ turn_id: string;+ steer_messages: CopilotSteerMessage[];+}+export type WorkflowCopilotMessageFeedbackRating = "up" | "down";export interface WorkflowCopilotMessageFeedback {@@ -318,7 +339,8 @@ export type WorkflowCopilotStreamMessageType =| "codegen_progress"| "title_update"| "credential_required"- | "question_required";+ | "question_required"+ | "steer_delivered";export interface WorkflowCopilotProcessingUpdate {type: "processing_update";@@ -365,6 +387,7 @@ export interface WorkflowCopilotTurnStartUpdate {// edit-vs-build chip; the snap-back source is captured client-side at// submit time so unsaved local canvas edits survive.prior_block_count?: number | null;+ workflow_copilot_chat_id?: string | null;}export interface WorkflowCopilotDesignStartUpdate {diff --git a/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2 b/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2index 5ee757d3d..4092ebf6e 100644--- a/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2+++ b/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2@@ -4,11 +4,13 @@ You are an assistant who helps users of the Skyvern platform build, test, and de{{ security_rules }}{% endif %}-Users move freely between three kinds of help, and any message may switch between them:-- Answering questions — about Skyvern, about their workflows, about what a run did and why. Answer directly from what you know and what your tools can look up.-- Building, editing, and testing workflows — the reusable deliverable a user can re-run themselves. Users call a workflow an agent (the product does too), so "make an agent that…" asks for a workflow and says nothing about which block family its steps use.-- Driving a browser — navigating, inspecting, or extracting on request, without necessarily saving a workflow.-Follow the user's intent for which kind a message is; do not convert one into another without being asked.+Users move freely between four kinds of help, and any message may switch between them:+- Answering questions — about Skyvern, about their agents, about what a run did and why. Answer directly from what you know and what your tools can look up.+- Building, editing, and testing agents — reusable automations of tasks on the web that the user can re-run themselves. The product and its users call these agents; tools and schemas call them workflows. Call them agents when you talk to the user. "Make an agent that…" says nothing about which block family its steps use.+- Driving a browser — navigating, inspecting, or extracting on request, without necessarily saving an agent. When the user wants that done again — later, on a schedule, or for other inputs — that is a request for an agent, whatever words they use.+- Managing their Skyvern account — saved credentials, browser profiles, schedules, connected accounts, and usage.+Anything else is outside what you do, even when it is phrased as an agent or a browser task: say so and offer the closest of these instead.+Follow the user's intent for which kind a message is; do not convert one into another unless the request calls for it.Your progress and explanation channel is `user_facing_reason` on existing action calls. Include a short user-facing sentence with the ordinary arguments for substantive inspection, authoring and test actions, explaining what that action is intended to accomplish in product language without secrets. Describe intent, not an outcome before results. Null or absence is accepted when no useful explanation is available.Blocks already in the workflow stay as-is unless the user asks you to change them. If you think one should change, ask before changing it.diff --git a/skyvern/forge/prompts/skyvern/workflow-copilot-user.j2 b/skyvern/forge/prompts/skyvern/workflow-copilot-user.j2index 43e5d391d..39c538c7b 100644--- a/skyvern/forge/prompts/skyvern/workflow-copilot-user.j2+++ b/skyvern/forge/prompts/skyvern/workflow-copilot-user.j2@@ -132,3 +132,6 @@ The user says:```{{ user_message }}```+{% if scope_check is defined and scope_check %}+{{ scope_check }}+{% endif %}diff --git a/skyvern/forge/request_logging.py b/skyvern/forge/request_logging.pyindex 7c3118b63..20e1dc9db 100644--- a/skyvern/forge/request_logging.py+++ b/skyvern/forge/request_logging.py@@ -55,6 +55,7 @@ _SENSITIVE_ENDPOINTS = {# body stays opaque; the route persists only its canonical redacted form."POST /v1/workflow/copilot/chat-post","POST /v1/workflow/copilot/question-response",+ "POST /v1/workflow/copilot/steer","POST /v1/workflow/copilot/credential-response","POST /v1/workflow/copilot/convert-yaml-to-blocks",# Payment-provider events carry customer billing details (name, email, address, card metadata).diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.pyindex 7a9588428..35f520b16 100644--- a/skyvern/forge/sdk/copilot/agent.py+++ b/skyvern/forge/sdk/copilot/agent.py@@ -299,6 +299,7 @@ from skyvern.forge.sdk.schemas.persistent_browser_sessions import is_final_statufrom skyvern.forge.sdk.schemas.workflow_copilot import (TURN_OPENER_SENDERS,CopilotAttachedFile,+ CopilotSteerMessage,CopilotVideoEvidenceArtifact,WorkflowCopilotChatHistoryMessage,chat_history_role,@@ -640,6 +641,14 @@ def _format_chat_history(chat_history: list[WorkflowCopilotChatHistoryMessage])lines.append(f"ask_user result: {json.dumps(interaction.tool_result())}")else:lines.append(f"ask_user request: {interaction.model_dump_json()}")+ steers = msg.narrative_payload.get("steerMessages", []) if msg.narrative_payload is not None else []+ for raw in steers:+ try:+ steer = CopilotSteerMessage.model_validate(raw)+ except ValidationError:+ continue+ if steer.delivered_at is not None:+ lines.append(f"user (sent while you were working): {steer.text}")lines.append(f"{role}: {msg.content}")historical_facts = _historical_turn_facts_projection(msg.narrative_payload) if role == "ai" else Noneif historical_facts is not None:@@ -1543,6 +1552,7 @@ def _build_user_context(user_goal_summary: str = "",untrusted_evidence: str = "",attached_files_summary: str = "",+ scope_check: str = "",) -> str:"""Render untrusted context into the user message with code fencing.@@ -1568,6 +1578,7 @@ def _build_user_context(user_goal_summary=escape_code_fences(redact_raw_secrets_for_prompt(user_goal_summary or "")),untrusted_evidence=escape_code_fences(redact_raw_secrets_for_structured_prompt(untrusted_evidence or "")),attached_files_summary=escape_code_fences(redact_raw_secrets_for_prompt(attached_files_summary or "")),+ scope_check=scope_check,)@@ -5767,6 +5778,7 @@ async def _run_copilot_turn_impl(user_goal_summary=user_goal_summary,untrusted_evidence=untrusted_evidence or "",attached_files_summary=attached_files_summary,+ scope_check=copilot_config.scope_check,)initial_input: str | list[dict[str, Any]] = user_messageif video_attachment_message is not None or direct_test_handoff is not None:diff --git a/skyvern/forge/sdk/copilot/config.py b/skyvern/forge/sdk/copilot/config.pyindex 641090c2e..59afa05a0 100644--- a/skyvern/forge/sdk/copilot/config.py+++ b/skyvern/forge/sdk/copilot/config.py@@ -116,6 +116,9 @@ class CopilotConfig:max_turns: int = DEFAULT_MAX_TURNStoken_budget: int = field(default_factory=_default_token_budget)security_rules: str = ""+ # Rendered after the user's message on every turn: from there it holds even when earlier turns or the+ # saved workflow went along with an out-of-scope request, which the same text in the system prompt does not.+ scope_check: str = ""enforcement_nudges: dict[str, str] = field(default_factory=_default_enforcement_nudges)fallback_llm_key: str | None = field(default_factory=_default_fallback_llm_key)# Wire and persisted-metadata spelling of authoring_capability; read it only through thatdiff --git a/skyvern/forge/sdk/copilot/context.py b/skyvern/forge/sdk/copilot/context.pyindex 0b3fc05a9..394e1e932 100644--- a/skyvern/forge/sdk/copilot/context.py+++ b/skyvern/forge/sdk/copilot/context.py@@ -265,6 +265,7 @@ class TurnNarrativePayload(TypedDict):# TurnOutcome.response_kind value: "answer" | "build" | "clarify" | "diagnose" | "refuse" | "recover".responseKind: NotRequired[str]questionInteractions: NotRequired[list[dict[str, Any]]]+ steerMessages: NotRequired[list[dict[str, Any]]]# {"reason": <credential_prompt_reason() token>}, set when this turn surfaces a credential need.credentialPrompt: NotRequired[dict[str, str]]# {"outcome": "connected"|"skipped"|"timeout", "credentialId": ..., "anchorToolCallId": ...}, set@@ -1199,6 +1200,7 @@ class CopilotContext(AgentContext):workflow_copilot_chat_id: str | None = Nonecopilot_cancel_token: str | None = None+ handled_steer_ids: set[str] = field(default_factory=set)copilot_question_pause_seconds: float = 0.0human_input_wait: HumanInputWait = field(default_factory=HumanInputWait)eval_capture_case_id: str | None = Nonediff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.pyindex dc1c334aa..ec56e1faa 100644--- a/skyvern/forge/sdk/copilot/enforcement.py+++ b/skyvern/forge/sdk/copilot/enforcement.py@@ -94,6 +94,7 @@ from skyvern.forge.sdk.copilot.runtime import (AgentContext,)from skyvern.forge.sdk.copilot.screenshot_utils import ScreenshotActionRelation, ScreenshotEntry+from skyvern.forge.sdk.copilot.steer import take_steer_input, watch_for_steerfrom skyvern.forge.sdk.copilot.terminal_predicates import (artifact_health_blocked,outcome_criteria_evaluated,@@ -1309,6 +1310,8 @@ async def _run_streamed_with_deadline(runner_kwargs: dict[str, Any],start_time: float,iteration: int,+ *,+ watch_steer: bool = False,) -> Any:"""Run ``Runner.run_streamed`` + ``stream_to_sse`` under the hard watchdog.@@ -1337,12 +1340,17 @@ async def _run_streamed_with_deadline(result.cancel(mode="after_turn")ctx.check_model_work_deadline = None if ctx.budget_expiry_state.drain_active else check_model_work_deadline+ steer_watcher = asyncio.create_task(watch_for_steer(result, ctx)) if watch_steer else Nonetry:try:async with asyncio.timeout(remaining) as deadline:ctx.model_stream_deadline = deadlineawait streaming_adapter.stream_to_sse(result, tracked_stream, ctx)finally:+ if steer_watcher is not None:+ steer_watcher.cancel()+ await asyncio.wait({steer_watcher})+ ctx.model_call_in_flight = Falsectx.model_stream_deadline = Nonectx.check_model_work_deadline = None# A request_credential call the SDK rejected before its handler ran leaves the gate@@ -2021,6 +2029,7 @@ async def run_with_enforcement(current_runner_kwargs,start_time,iteration,+ watch_steer=True,)except asyncio.CancelledError:_record_copilot_cancellation(ctx, start_time, iteration)@@ -2076,6 +2085,7 @@ async def run_with_enforcement(current_runner_kwargs,start_time,iteration,+ watch_steer=True,)except asyncio.CancelledError:_record_copilot_cancellation(ctx, start_time, iteration)@@ -2115,6 +2125,14 @@ async def run_with_enforcement("deadline",)+ steer_input = await take_steer_input(ctx)+ if steer_input:+ current_input = (+ steer_input if session is not None else _prune_input_list(result.to_input_list()) + steer_input+ )+ iteration += 1+ continue+# The post-run screenshot drain must follow the enforcement check:# without a nudge, re-invoking with just the screenshot would replace# the agent's already-final REPLY with one synthesized from a singlediff --git a/skyvern/forge/sdk/copilot/hooks.py b/skyvern/forge/sdk/copilot/hooks.pyindex ef8f5a91f..8a74df524 100644--- a/skyvern/forge/sdk/copilot/hooks.py+++ b/skyvern/forge/sdk/copilot/hooks.py@@ -103,6 +103,8 @@ class CopilotRunHooks(RunHooksBase):system_prompt: str | None,input_items: list[TResponseInputItem],) -> None:+ self._ctx.model_call_in_flight = True+ self._ctx.model_call_streamed_tool_call = Falsetry:self._ctx.model_calls_this_turn += 1if self._ctx.eval_mode == "browser_ablation" and isinstance(system_prompt, str):@@ -114,6 +116,7 @@ class CopilotRunHooks(RunHooksBase):)async def on_llm_end(self, context: RunContextWrapper, agent: Agent, response: ModelResponse) -> None:+ self._ctx.model_call_in_flight = Falseif self._ctx.check_model_work_deadline is not None:self._ctx.check_model_work_deadline()try:diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.pyindex ab2e7a653..d4f7ae9ad 100644--- a/skyvern/forge/sdk/copilot/request_policy.py+++ b/skyvern/forge/sdk/copilot/request_policy.py@@ -62,6 +62,7 @@ from skyvern.forge.sdk.schemas.credentials import Credential, TotpTypefrom skyvern.forge.sdk.schemas.google_oauth import STATE_ACTIVEfrom skyvern.forge.sdk.schemas.workflow_copilot import (TURN_OPENER_SENDERS,+ CopilotSteerMessage,WorkflowCopilotChatHistoryMessage,WorkflowCopilotChatSender,)@@ -889,7 +890,13 @@ class QuestionResponseSiteURLSource:kind: Literal["question_response"] = field(default="question_response", init=False)-SiteURLSource = UserMessageSiteURLSource | QuestionResponseSiteURLSource+@dataclass(frozen=True)+class SteerMessageSiteURLSource:+ steer_id: str+ kind: Literal["steer_message"] = field(default="steer_message", init=False)+++SiteURLSource = UserMessageSiteURLSource | QuestionResponseSiteURLSource | SteerMessageSiteURLSource@dataclass(frozen=True)@@ -1002,6 +1009,14 @@ class RequestPolicy:def project_question_response_sites(self, interaction: QuestionInteraction) -> None:project_question_response_sites(self, interaction)+ def project_steer_message(self, steer: CopilotSteerMessage) -> None:+ # A delivered steer is more of the current user turn, so exact credential citations read it too.+ self.canonical_user_message = f"{self.canonical_user_message}\n{steer.text}".strip()+ if steer.raw_secret_detected:+ self.apply_raw_secret_redacted_draft()+ return+ _project_user_provided_sites(self, _steer_message_url_texts(steer), reset=False)+@propertydef raw_secret_redacted_draft(self) -> bool:return self.raw_secret_detected and self.raw_secret_handling == "redacted_draft"@@ -4176,6 +4191,19 @@ def _accepted_question_response_url_texts(interaction: QuestionInteraction) -> lreturn [_SiteURLText(text=text, source=source) for text in texts]+def _steer_message_url_texts(steer: CopilotSteerMessage) -> list[_SiteURLText]:+ if steer.raw_secret_detected or steer.delivered_at is None:+ return []+ return [_SiteURLText(text=steer.text, source=SteerMessageSiteURLSource(steer_id=steer.steer_id))]+++def _persisted_steer_message_url_texts(raw_steer: dict[str, Any]) -> list[_SiteURLText]:+ try:+ return _steer_message_url_texts(CopilotSteerMessage.model_validate(raw_steer))+ except ValidationError:+ return []++def _persisted_question_response_url_texts(raw_interaction: dict[str, Any]) -> list[_SiteURLText]:try:interaction = QuestionInteraction.model_validate(raw_interaction)@@ -4231,10 +4259,10 @@ def _ground_user_provided_sites(user_message: str,full_chat_history: Sequence[WorkflowCopilotChatHistoryMessage],) -> None:- """Rebuild the URL facts the person supplied in USER rows and accepted question responses.+ """Rebuild the URL facts the person supplied in USER rows, accepted question responses and delivered steers.Linking a later site name back to a URL is the agent's job. This projection only verifies URLs- in the two structured user-authored text surfaces; prompts, choices, rendered history, and+ in the three structured user-authored text surfaces; prompts, choices, rendered history, andPRODUCT or AI prose never become credential-origin authority."""url_texts: list[_SiteURLText] = []@@ -4252,6 +4280,8 @@ def _ground_user_provided_sites(continuefor raw_interaction in message.narrative_payload.get("questionInteractions", []):url_texts.extend(_persisted_question_response_url_texts(raw_interaction))+ for raw_steer in message.narrative_payload.get("steerMessages", []):+ url_texts.extend(_persisted_steer_message_url_texts(raw_steer))if user_message:url_texts.append(_SiteURLText(diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.pyindex ef464fc81..f650e8409 100644--- a/skyvern/forge/sdk/copilot/runtime.py+++ b/skyvern/forge/sdk/copilot/runtime.py@@ -463,6 +463,12 @@ class AgentContext:# The deadline the current model stream runs under, published by the enforcement loop so a tool# that parks on a user decision can suspend it instead of being cancelled mid-question.model_stream_deadline: asyncio.Timeout | None = None+ # True from on_llm_start to on_llm_end: no tool of that response has started, so aborting the+ # stream then discards only the call itself.+ model_call_in_flight: bool = False+ # The SDK streams each tool call to the chat before its response completes, so aborting after one+ # would leave a call on screen that never runs.+ model_call_streamed_tool_call: bool = False# Build-test dispatch calls in the model response now executing, armed before any of them runs.# The SDK schedules them concurrently, so more than one means a sibling may hold this session.build_test_tool_calls_in_model_response: int = 0diff --git a/skyvern/forge/sdk/copilot/session_factory.py b/skyvern/forge/sdk/copilot/session_factory.pyindex 79cb4f513..e8b6ea6e0 100644--- a/skyvern/forge/sdk/copilot/session_factory.py+++ b/skyvern/forge/sdk/copilot/session_factory.py@@ -141,9 +141,9 @@ def copilot_session_input_callback(payloads in the middle region.Keeps the original goal (first item) at full fidelity and preserves the- last ``RECENT_REAL_TURNS`` real user turns — in production every injected- copilot message is synthetic, so the whole post-goal history is the middle- region. Within it, older ``function_call_output`` / ``function_call`` items+ last ``RECENT_REAL_TURNS`` real user turns — besides the goal, only messages+ the user sent into the running turn are real, so the post-goal history is+ usually all middle region. Within it, older ``function_call_output`` / ``function_call`` itemsare compacted using the same ``KEEP_RECENT_TOOL_OUTPUTS`` rule that``enforcement._prune_input_list`` uses in the non-session path, and thenewest screenshot survives unless a newer one rides in recent/new items.diff --git a/skyvern/forge/sdk/copilot/steer.py b/skyvern/forge/sdk/copilot/steer.pynew file mode 100644index 000000000..cb8ad8b61--- /dev/null+++ b/skyvern/forge/sdk/copilot/steer.py@@ -0,0 +1,130 @@+"""Deliver messages the user sends into a running Copilot turn at the next model-call boundary."""++from __future__ import annotations++import asyncio+from datetime import timedelta+from typing import TYPE_CHECKING, Any++import structlog++from skyvern.forge import app+from skyvern.forge.sdk.copilot.context import CopilotContext++if TYPE_CHECKING:+ from agents.result import RunResultStreaming++LOG = structlog.get_logger()++STEER_DOORBELL_TTL = timedelta(minutes=30)+STEER_POLL_SECONDS = 1.0+++def copilot_steer_key(organization_id: str, cancel_token: str) -> str:+ return f"copilot_steer:{organization_id}:{cancel_token}"+++def _steer_target(ctx: object) -> tuple[CopilotContext, str, str] | None:+ if not isinstance(ctx, CopilotContext) or not ctx.workflow_copilot_chat_id or not ctx.copilot_cancel_token:+ return None+ return ctx, ctx.workflow_copilot_chat_id, ctx.copilot_cancel_token+++async def _unhandled_doorbell(ctx: CopilotContext, cancel_token: str) -> str | None:+ try:+ steer_id = await app.CACHE.get(copilot_steer_key(ctx.organization_id, cancel_token))+ except Exception:+ LOG.debug("Copilot steer doorbell read failed", exc_info=True)+ return None+ if isinstance(steer_id, bytes):+ steer_id = steer_id.decode()+ if not steer_id or steer_id in ctx.handled_steer_ids:+ return None+ return steer_id+++async def watch_for_steer(result: RunResultStreaming, ctx: object) -> None:+ """End the stream once the user sends a message: abort an in-flight model call, or let running tools finish.++ An in-flight call has nothing in the session yet, so aborting it loses only that call, while a tool's+ output must reach the model.+ """+ target = _steer_target(ctx)+ if target is None:+ return+ copilot_ctx, chat_id, cancel_token = target+ model_calls_before_run = copilot_ctx.model_calls_this_turn+ requested = False+ while True:+ await asyncio.sleep(STEER_POLL_SECONDS)+ if not requested:+ steer_id = await _unhandled_doorbell(copilot_ctx, cancel_token)+ if steer_id is None:+ continue+ try:+ waiting = await app.DATABASE.workflow_params.undelivered_copilot_steer_ids(+ copilot_ctx.organization_id, chat_id, copilot_ctx.turn_id+ )+ except Exception:+ LOG.debug("Copilot steer record read failed; will retry", exc_info=True)+ continue+ # The doorbell is keyed by cancel token, so it can name a message this turn never recorded.+ if steer_id not in waiting:+ copilot_ctx.handled_steer_ids.add(steer_id)+ continue+ requested = True+ LOG.info(+ "copilot_steer_requested",+ steer_id=steer_id,+ turn_id=copilot_ctx.turn_id,+ model_call_in_flight=copilot_ctx.model_call_in_flight,+ )+ # The SDK saves a run's input only when its first model call starts, so stopping earlier drops it.+ if copilot_ctx.model_calls_this_turn == model_calls_before_run:+ continue+ # A model call can start after the soft stop, so keep checking until the stream ends.+ if copilot_ctx.model_call_in_flight and not copilot_ctx.model_call_streamed_tool_call:+ # The aborted call never saw the frame it claimed, so the next call claims it again.+ copilot_ctx.pending_frame_lease = None+ result.cancel(mode="immediate")+ return+ result.cancel(mode="after_turn")+++async def take_steer_input(ctx: object) -> list[dict[str, Any]]:+ """Mark the turn's undelivered messages delivered and return them as the model's next user input.++ Only a doorbell this turn has not handled reaches the database, so a turn nobody steers never touches it.+ """+ target = _steer_target(ctx)+ if target is None:+ return []+ copilot_ctx, chat_id, cancel_token = target+ doorbell = await _unhandled_doorbell(copilot_ctx, cancel_token)+ if doorbell is None:+ return []+ steers = await app.DATABASE.workflow_params.take_copilot_steer_messages(+ copilot_ctx.organization_id, chat_id, copilot_ctx.turn_id+ )+ copilot_ctx.handled_steer_ids.add(doorbell)+ if not steers:+ return []+ for steer in steers:+ copilot_ctx.handled_steer_ids.add(steer.steer_id)+ if copilot_ctx.request_policy is not None:+ copilot_ctx.request_policy.project_steer_message(steer)+ if copilot_ctx.request_policy is not None and any(steer.raw_secret_detected for steer in steers):+ copilot_ctx.allow_untested_workflow_draft = copilot_ctx.request_policy.raw_secret_redacted_draft+ LOG.info(+ "copilot_steer_delivered",+ turn_id=copilot_ctx.turn_id,+ steer_ids=[steer.steer_id for steer in steers],+ )+ await copilot_ctx.stream.send(+ {+ "type": "steer_delivered",+ "turn_id": copilot_ctx.turn_id,+ "steer_messages": [steer.model_dump(mode="json") for steer in steers],+ }+ )+ return [{"role": "user", "content": steer.text} for steer in steers]diff --git a/skyvern/forge/sdk/copilot/streaming_adapter.py b/skyvern/forge/sdk/copilot/streaming_adapter.pyindex 8a15e023d..73500fad8 100644--- a/skyvern/forge/sdk/copilot/streaming_adapter.py+++ b/skyvern/forge/sdk/copilot/streaming_adapter.py@@ -331,6 +331,7 @@ async def stream_to_sse(LOG.warning("copilot_narrative_design_start_emit_failed", error=str(emit_err))if event.name == "tool_called":+ ctx.model_call_streamed_tool_call = Trueraw = event.item.raw_itemcall_id = _get_raw_field(raw, "call_id") or _get_raw_field(raw, "id") or ""tool_name = _get_raw_field(raw, "name") or "unknown"@@ -900,6 +901,7 @@ async def emit_turn_start(stream: EventSourceStream, ctx: CopilotContext) -> Nonturn_index=ctx.turn_index,timestamp=now,prior_block_count=ctx.prior_block_count,+ workflow_copilot_chat_id=ctx.workflow_copilot_chat_id,))diff --git a/skyvern/forge/sdk/copilot/tools/credential_fill.py b/skyvern/forge/sdk/copilot/tools/credential_fill.pyindex 72a0ecfc4..19a8dcf4e 100644--- a/skyvern/forge/sdk/copilot/tools/credential_fill.py+++ b/skyvern/forge/sdk/copilot/tools/credential_fill.py@@ -44,6 +44,7 @@ from skyvern.forge.sdk.copilot.request_policy import (QuestionResponseSiteURLSource,RequestPolicy,SiteURLSource,+ SteerMessageSiteURLSource,UserMessageSiteURLSource,admit_credential_for_live_page,loggable_origin,@@ -469,6 +470,8 @@ def _log_fill_grant(source_fields = {"source_kind": source.kind, "source_user_message": source.message_index}elif isinstance(source, QuestionResponseSiteURLSource):source_fields = {"source_kind": source.kind, "source_interaction_id": source.interaction_id}+ elif isinstance(source, SteerMessageSiteURLSource):+ source_fields = {"source_kind": source.kind, "source_steer_id": source.steer_id}LOG.info("copilot credential fill grant",route=route,diff --git a/skyvern/forge/sdk/db/repositories/workflow_parameters.py b/skyvern/forge/sdk/db/repositories/workflow_parameters.pyindex 2714376ca..feee1b65b 100644--- a/skyvern/forge/sdk/db/repositories/workflow_parameters.py+++ b/skyvern/forge/sdk/db/repositories/workflow_parameters.py@@ -69,10 +69,12 @@ from skyvern.forge.sdk.schemas.copilot_turn_outcome import TurnOutcomefrom skyvern.forge.sdk.schemas.task_generations import TaskGenerationfrom skyvern.forge.sdk.schemas.workflow_copilot import (COPILOT_PROPOSAL_METADATA_KEY,+ MAX_STEER_MESSAGES_PER_TURN,CopilotAttachedFile,CopilotCandidateDisposition,CopilotPendingTurn,CopilotProposalMetadata,+ CopilotSteerMessage,CopilotVideoEvidenceArtifact,NonAdoptableCriteriaSet,WorkflowCopilotChat,@@ -1374,6 +1376,80 @@ class WorkflowParametersRepository(BaseRepository):await session.commit()return changed+ @db_operation("record_copilot_steer_message", expected_errors=(ValueError,))+ async def record_copilot_steer_message(+ self,+ organization_id: str,+ chat_id: str,+ cancel_token: str,+ steer_id: str,+ steer: CopilotSteerMessage | None = None,+ ) -> CopilotSteerMessage | None:+ """Validate the running turn before external screening (``steer`` None), then recheck and record.++ An already-recorded ``steer_id`` returns the stored message, so a retry never records twice.+ """+ async with self.Session() as session:+ chat = await self._locked_question_chat(session, organization_id, chat_id)+ entry = next(+ (+ candidate+ for candidate in map(CopilotPendingTurn.model_validate, (chat.pending_turns or {}).values())+ if candidate.cancel_token == cancel_token and candidate.recovering_at is None+ ),+ None,+ )+ if entry is None:+ raise ValueError("The Copilot turn has ended")+ existing = next((item for item in entry.steer_messages if item.steer_id == steer_id), None)+ if existing is not None:+ return existing+ if len(entry.steer_messages) >= MAX_STEER_MESSAGES_PER_TURN:+ raise ValueError("This Copilot turn cannot take more messages")+ if steer is None:+ return None+ entry.steer_messages.append(steer)+ entry.steer_messages.sort(key=lambda item: item.created_at)+ self._store_question_turn(chat, entry)+ await session.commit()+ return steer++ @db_operation("undelivered_copilot_steer_ids")+ async def undelivered_copilot_steer_ids(self, organization_id: str, chat_id: str, turn_id: str) -> set[str]:+ async with self.Session() as session:+ pending_turns = (+ await session.scalars(+ select(WorkflowCopilotChatModel.pending_turns)+ .where(WorkflowCopilotChatModel.organization_id == organization_id)+ .where(WorkflowCopilotChatModel.workflow_copilot_chat_id == chat_id)+ )+ ).first()+ raw = (pending_turns or {}).get(turn_id)+ if raw is None:+ return set()+ entry = CopilotPendingTurn.model_validate(raw)+ return {item.steer_id for item in entry.steer_messages if item.delivered_at is None}++ @db_operation("take_copilot_steer_messages")+ async def take_copilot_steer_messages(+ self, organization_id: str, chat_id: str, turn_id: str+ ) -> list[CopilotSteerMessage]:+ async with self.Session() as session:+ chat = await self._locked_question_chat(session, organization_id, chat_id)+ raw = (chat.pending_turns or {}).get(turn_id)+ if raw is None:+ return []+ entry = CopilotPendingTurn.model_validate(raw)+ undelivered = [item for item in entry.steer_messages if item.delivered_at is None]+ if not undelivered:+ return []+ delivered_at = datetime.now(timezone.utc)+ for item in undelivered:+ item.delivered_at = delivered_at+ self._store_question_turn(chat, entry)+ await session.commit()+ return undelivered+@db_operation("claim_pending_copilot_turn")async def claim_pending_copilot_turn(self,diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex b9fd3ab47..8c396c0c5 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -86,6 +86,7 @@ from skyvern.forge.sdk.copilot.repair_origin_run import RepairOriginRefusal, resfrom skyvern.forge.sdk.copilot.request_policy import _screen_raw_secret_safetyfrom skyvern.forge.sdk.copilot.review_gate import parse_execution_receipts, serialize_execution_receiptsfrom skyvern.forge.sdk.copilot.runtime import close_browser_session_quietly+from skyvern.forge.sdk.copilot.steer import STEER_DOORBELL_TTL, copilot_steer_keyfrom skyvern.forge.sdk.copilot.tools.workflow_update import (_validated_pending_workflow_proposal,private_workflow_settings_from_proposal,@@ -145,6 +146,7 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (CopilotPendingTurn,CopilotProposalMetadata,CopilotProposalRunFacts,+ CopilotSteerMessage,CopilotVideoEvidenceArtifact,WorkflowCopilotApplyProposedWorkflowRequest,WorkflowCopilotAudioUploadResponse,@@ -169,6 +171,7 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (WorkflowCopilotMessageFeedbackResponse,WorkflowCopilotProcessingUpdate,WorkflowCopilotQuestionResponseRequest,+ WorkflowCopilotSteerRequest,WorkflowCopilotStreamErrorUpdate,WorkflowCopilotStreamMessageType,WorkflowCopilotStreamResponseUpdate,@@ -1286,6 +1289,13 @@ async def _persist_turn_messages(narrative_payload["questionInteractions"] = [item.model_dump(mode="json") for item in pending.question_interactions]+ delivered_steers = [+ item.model_dump(mode="json") for item in pending.steer_messages if item.delivered_at is not None+ ]+ if delivered_steers:+ if narrative_payload is None:+ narrative_payload = _make_error_narrative_payload(turn_id, None, assistant_content)+ narrative_payload["steerMessages"] = delivered_steersif turn_outcome is not None:turn_outcome = turn_outcome.model_copy(@@ -4055,6 +4065,57 @@ async def workflow_copilot_question_response(return await resolve()+@base_router.post("/workflow/copilot/steer", include_in_schema=False, response_model=CopilotSteerMessage)+async def workflow_copilot_steer(+ steer_request: WorkflowCopilotSteerRequest,+ organization: Organization = Depends(org_auth_service.get_current_org),+) -> CopilotSteerMessage:+ """Send a message into a running turn; its loop hands the message to the model at the next model call."""+ # Stamped before screening: screens run concurrently, so their finish order is not the send order.+ received_at = datetime.now(UTC)+ chat = await app.DATABASE.workflow_params.get_workflow_copilot_chat_by_id(+ organization.organization_id, steer_request.workflow_copilot_chat_id+ )+ if chat is None:+ raise HTTPException(status_code=404, detail="Unknown Copilot chat")++ async def record(steer: CopilotSteerMessage | None = None) -> CopilotSteerMessage | None:+ try:+ return await app.DATABASE.workflow_params.record_copilot_steer_message(+ organization.organization_id,+ chat.workflow_copilot_chat_id,+ steer_request.cancel_token,+ steer_request.steer_id,+ steer,+ )+ except ValueError as exc:+ raise HTTPException(status_code=409, detail=str(exc)) from exc++ recorded = await record()+ if recorded is None:+ handler = await resolve_raw_secret_safety_handler(chat.workflow_permanent_id, organization.organization_id)+ safety = await _screen_raw_secret_safety(+ steer_request.message, handler, organization_id=organization.organization_id+ )+ if safety.status == "blocked":+ raise HTTPException(status_code=503, detail="The safety screen is unavailable. Please retry your message.")+ screened = CopilotSteerMessage(+ steer_id=steer_request.steer_id,+ text=safety.canonical_user_message,+ raw_secret_detected=safety.status == "detected",+ created_at=received_at,+ )+ recorded = await record(screened) or screened+ # Without the doorbell the turn never reads the message; the client then sends it as the next turn.+ with contained_effect("copilot steer doorbell", workflow_copilot_chat_id=chat.workflow_copilot_chat_id):+ await app.CACHE.set(+ copilot_steer_key(organization.organization_id, steer_request.cancel_token),+ recorded.steer_id,+ ex=STEER_DOORBELL_TTL,+ )+ return recorded++@base_router.post("/workflow/copilot/message-feedback", include_in_schema=False)async def workflow_copilot_message_feedback(feedback_request: WorkflowCopilotMessageFeedbackRequest,diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.pyindex 5154ee9c0..bbff618f9 100644--- a/skyvern/forge/sdk/schemas/workflow_copilot.py+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py@@ -130,6 +130,20 @@ def copilot_proposal_metadata(value: object) -> CopilotProposalMetadata | None:return None+MAX_STEER_MESSAGES_PER_TURN = 20+++class CopilotSteerMessage(BaseModel):+ """A message the user sent into a running turn, stored as screened text only."""++ steer_id: str+ text: str+ raw_secret_detected: bool = Field(default=False, exclude_if=lambda detected: not detected)+ created_at: datetime+ # None until the running loop hands the message to the model.+ delivered_at: datetime | None = None++class CopilotPendingTurn(BaseModel):"""Durable write-ahead marker for one in-flight copilot turn.@@ -162,6 +176,7 @@ class CopilotPendingTurn(BaseModel):# Build-test runs this turn ended so it could take the chat's browser. Copilot owns this list;# the run row's failure_reason carries the same fact but a later finalizer can overwrite it.superseded_build_test_run_ids: list[str] = Field(default_factory=list)+ steer_messages: list[CopilotSteerMessage] = Field(default_factory=list)class WorkflowCopilotChat(BaseModel):@@ -526,6 +541,13 @@ class WorkflowCopilotCancelRequest(BaseModel):)+class WorkflowCopilotSteerRequest(BaseModel):+ workflow_copilot_chat_id: str+ cancel_token: str = Field(..., description="The cancel_token sent on the running turn's /chat-post request")+ steer_id: str = Field(..., max_length=64, description="Client-generated id; a retry with the same id is a no-op")+ message: Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]++class WorkflowCopilotQuestionResponseRequest(QuestionResponse):workflow_copilot_chat_id: strinteraction_id: str@@ -936,6 +958,9 @@ class WorkflowCopilotTurnStartUpdate(BaseModel):None,description="Block count of the canonical workflow at turn entry; drives the FE edit-vs-build chip.",)+ workflow_copilot_chat_id: str | None = Field(+ None, description="The chat this turn runs in, known to a client that opened the chat with this turn"+ )class WorkflowCopilotDesignStartUpdate(BaseModel):diff --git a/skyvern/forge/sdk/services/route_authorization.py b/skyvern/forge/sdk/services/route_authorization.pyindex 44008a75d..6fb690bde 100644--- a/skyvern/forge/sdk/services/route_authorization.py+++ b/skyvern/forge/sdk/services/route_authorization.py@@ -260,6 +260,7 @@ ROUTE_ACTIONS: Mapping[str, str] = {"skyvern.forge.sdk.routes.workflow_copilot.workflow_copilot_disable_auto_accept": "workflow_copilot.manage","skyvern.forge.sdk.routes.workflow_copilot.workflow_copilot_message_feedback": "workflow_copilot.manage","skyvern.forge.sdk.routes.workflow_copilot.workflow_copilot_question_response": "workflow_copilot.manage",+ "skyvern.forge.sdk.routes.workflow_copilot.workflow_copilot_steer": "workflow.update","skyvern.forge.sdk.routes.workflow_copilot.workflow_copilot_suggest_goal": "workflow_copilot.suggest","skyvern.forge.sdk.routes.workflow_run_groups.cancel_workflow_run_group": "workflow_run.cancel","skyvern.forge.sdk.routes.workflow_run_groups.get_workflow_run_group": "workflow_run.read",diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex 77215e8cc..c4599132b 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -214,6 +214,14 @@ def test_build_user_context_preserves_structured_evidence_after_redacting_secretassert "a002" in context+def test_build_user_context_renders_scope_check_after_the_user_message() -> None:+ context = agent_module._build_user_context(+ "", "user history", "", "", "make me a game", scope_check="SCOPE-SENTINEL"+ )++ assert context.rstrip().endswith("make me a game\n```\n\nSCOPE-SENTINEL")++_COVERED_DRAFT_YAML = """title: Draftworkflow_definition:parameters: []diff --git a/tests/unit/test_copilot_ask_user.py b/tests/unit/test_copilot_ask_user.pyindex e5b4b81c3..7b3632c38 100644--- a/tests/unit/test_copilot_ask_user.py+++ b/tests/unit/test_copilot_ask_user.py@@ -163,6 +163,7 @@ async def setup_question_chat(sqlite_engine, monkeypatch):api.add_api_route("/reply", routes.workflow_copilot_question_response, methods=["POST"])api.add_api_route("/history", routes.workflow_copilot_chat_history, methods=["GET"])api.add_api_route("/cancel", routes.workflow_copilot_cancel, methods=["POST"], status_code=204)+ api.add_api_route("/steer", routes.workflow_copilot_steer, methods=["POST"])client = AsyncClient(transport=ASGITransport(app=api), base_url="http://fixture")frames = asyncio.Queue()ctx = CopilotContext(diff --git a/tests/unit/test_copilot_steer.py b/tests/unit/test_copilot_steer.pynew file mode 100644index 000000000..f59f23d3e--- /dev/null+++ b/tests/unit/test_copilot_steer.py@@ -0,0 +1,434 @@+import asyncio+from types import SimpleNamespace+from typing import Any+from unittest.mock import MagicMock++import pytest+from agents import Agent, Model, RunConfig, SQLiteSession, function_tool+from agents.items import ModelResponse+from openai.types.responses import (+ Response,+ ResponseCompletedEvent,+ ResponseFunctionToolCall,+ ResponseOutputItemDoneEvent,+ ResponseOutputMessage,+ ResponseOutputText,+)++from skyvern.forge import app+from skyvern.forge.sdk.cache.local import LocalCache+from skyvern.forge.sdk.copilot.agent import _format_chat_history+from skyvern.forge.sdk.copilot.enforcement import run_with_enforcement+from skyvern.forge.sdk.copilot.hooks import CopilotRunHooks+from skyvern.forge.sdk.copilot.request_policy import (+ RequestPolicy,+ SteerMessageSiteURLSource,+ _ground_user_provided_sites,+)+from skyvern.forge.sdk.routes.workflow_copilot import _persist_turn_messages+from skyvern.forge.sdk.schemas.workflow_copilot import WorkflowCopilotChatHistoryResponse, WorkflowCopilotChatSender+from tests.unit.test_copilot_ask_user import setup_question_chat++STEER_TEXT = "Also extract the title from https://portal.example.com/home"+++class DoorbellCache(LocalCache):+ """Signals the first read that finds a message waiting, so a test can act after the loop saw it."""++ def __init__(self) -> None:+ super().__init__()+ self.rang = asyncio.Event()++ async def get(self, key: str) -> Any:+ value = await super().get(key)+ if value and key.startswith("copilot_steer:"):+ self.rang.set()+ return value+++def _completed(index: int, output: list[Any]) -> ResponseCompletedEvent:+ return ResponseCompletedEvent(+ sequence_number=0,+ type="response.completed",+ response=Response(+ id=f"resp_{index}",+ created_at=0.0,+ model="steer-test",+ object="response",+ output=output,+ parallel_tool_calls=True,+ tool_choice="auto",+ tools=[],+ status="completed",+ ),+ )+++def _reply(text: str) -> ResponseOutputMessage:+ return ResponseOutputMessage(+ id="msg_reply",+ type="message",+ role="assistant",+ status="completed",+ content=[ResponseOutputText(type="output_text", text=text, annotations=[])],+ )+++def _signal_when_steer_confirmed(monkeypatch: pytest.MonkeyPatch, repo: Any) -> asyncio.Event:+ """Set once the watcher has confirmed a waiting message; it then stops the stream before awaiting again."""+ confirmed = asyncio.Event()+ undelivered = repo.undelivered_copilot_steer_ids++ async def undelivered_then_signal(*args: str) -> set[str]:+ waiting = await undelivered(*args)+ if waiting:+ confirmed.set()+ return waiting++ monkeypatch.setattr(repo, "undelivered_copilot_steer_ids", undelivered_then_signal)+ return confirmed+++async def _consume_sdk_stream(result: Any, *_args: Any) -> None:+ async for _event in result.stream_events():+ pass++[email protected]("boundary", ["model_call", "tool"])+async def test_send_now_joins_the_running_turn_and_its_saved_history(sqlite_engine, monkeypatch, boundary):+ repo, client, ctx, frames = await setup_question_chat(sqlite_engine, monkeypatch)+ monkeypatch.setattr(app, "CACHE", LocalCache())+ monkeypatch.setattr("skyvern.forge.sdk.copilot.steer.STEER_POLL_SECONDS", 0.01)+ monkeypatch.setattr("skyvern.forge.sdk.copilot.streaming_adapter.stream_to_sse", _consume_sdk_stream)+ steer_confirmed = _signal_when_steer_confirmed(monkeypatch, repo)+ ctx.request_policy = RequestPolicy()+ model_inputs: list[list[Any]] = []+ aborted_calls: list[int] = []+ working = asyncio.Event()+ release_tool = asyncio.Event()++ @function_tool+ async def inspect_page() -> str:+ working.set()+ await release_tool.wait()+ return "page inspected"++ class SteerModel(Model):+ async def get_response(self, *args: Any, **kwargs: Any) -> ModelResponse:+ raise AssertionError("The production runner must stream")++ async def stream_response(self, *args: Any, **kwargs: Any):+ model_inputs.append(kwargs["input"] if "input" in kwargs else args[1])+ call = len(model_inputs)+ if call == 1 and boundary == "model_call":+ working.set()+ try:+ await asyncio.Event().wait()+ except asyncio.CancelledError:+ aborted_calls.append(call)+ raise+ if call == 1:+ output: list[Any] = [+ ResponseFunctionToolCall(+ type="function_call", name="inspect_page", call_id="call_inspect", arguments="{}"+ )+ ]+ else:+ output = [_reply("Built it, and it extracts the title.")]+ yield _completed(call, output)++ session = SQLiteSession("steer")+ async with client:+ turn = asyncio.create_task(+ run_with_enforcement(+ agent=Agent(name="steer-test", model=SteerModel(), tools=[inspect_page]),+ initial_input="Build the workflow",+ ctx=ctx,+ stream=MagicMock(),+ session=session,+ hooks=CopilotRunHooks(ctx),+ run_config=RunConfig(tracing_disabled=True),+ )+ )+ try:+ await asyncio.wait_for(working.wait(), 5)+ body = {+ "workflow_copilot_chat_id": ctx.workflow_copilot_chat_id,+ "cancel_token": "stop",+ "steer_id": "steer-1",+ "message": STEER_TEXT,+ }+ sent = await client.post("/steer", json=body)+ assert sent.status_code == 200, sent.text+ if boundary == "tool":+ await asyncio.wait_for(steer_confirmed.wait(), 5)+ release_tool.set()+ result = await asyncio.wait_for(turn, 5)++ assert result.final_output == "Built it, and it extracts the title."+ assert len(model_inputs) == 2+ next_input = model_inputs[1]+ assert next_input[-1] == {"role": "user", "content": STEER_TEXT}+ if boundary == "model_call":+ assert aborted_calls == [1]+ assert not any(item.get("type") == "function_call_output" for item in next_input)+ else:+ assert aborted_calls == []+ outputs = [item for item in next_input if item.get("type") == "function_call_output"]+ assert [item["output"] for item in outputs] == ["page inspected"]+ delivered = [frame for frame in frames._queue if frame.get("type") == "steer_delivered"]+ assert [message["steer_id"] for frame in delivered for message in frame["steer_messages"]] == ["steer-1"]+ assert ctx.request_policy.user_site_url_sources["https://portal.example.com/home"] == (+ SteerMessageSiteURLSource(steer_id="steer-1")+ )+ assert ctx.request_policy.canonical_user_message.endswith(STEER_TEXT)++ chat = await repo.get_workflow_copilot_chat_by_id("org", ctx.workflow_copilot_chat_id)+ await _persist_turn_messages(+ chat=chat,+ turn_id="turn",+ user_message="Build the workflow",+ audio_artifact_id=None,+ user_row_already_persisted=True,+ sender=WorkflowCopilotChatSender.USER,+ assistant_content="Built it, and it extracts the title.",+ global_llm_context=None,+ turn_outcome=None,+ narrative_payload=None,+ )+ history = await client.get("/history", params={"workflow_copilot_chat_id": ctx.workflow_copilot_chat_id})+ loaded = WorkflowCopilotChatHistoryResponse.model_validate(history.json())+ assert [message.sender for message in loaded.chat_history] == [+ WorkflowCopilotChatSender.USER,+ WorkflowCopilotChatSender.AI,+ ]+ saved = loaded.chat_history[-1].narrative_payload["steerMessages"]+ assert [(item["steer_id"], item["text"]) for item in saved] == [("steer-1", STEER_TEXT)]+ assert saved[0]["delivered_at"] is not None+ assert f"user (sent while you were working): {STEER_TEXT}" in _format_chat_history(loaded.chat_history)+ next_turn_policy = RequestPolicy()+ _ground_user_provided_sites(next_turn_policy, "Add pagination", loaded.chat_history)+ assert next_turn_policy.user_site_url_sources["https://portal.example.com/home"] == (+ SteerMessageSiteURLSource(steer_id="steer-1")+ )++ late = await client.post("/steer", json={**body, "steer_id": "steer-2"})+ assert late.status_code == 409+ finally:+ release_tool.set()+ if not turn.done():+ turn.cancel()+ await asyncio.gather(turn, return_exceptions=True)+ session.close()+++class _RecordingStream:+ def __init__(self) -> None:+ self.frames: list[Any] = []++ async def send(self, data: Any) -> bool:+ self.frames.append(data)+ return True++ async def is_disconnected(self) -> bool:+ return False+++async def test_send_now_waits_out_a_response_whose_tool_call_the_chat_already_shows(sqlite_engine, monkeypatch):+ repo, client, ctx, _ = await setup_question_chat(sqlite_engine, monkeypatch)+ monkeypatch.setattr(app, "CACHE", LocalCache())+ monkeypatch.setattr("skyvern.forge.sdk.copilot.steer.STEER_POLL_SECONDS", 0.01)+ steer_confirmed = _signal_when_steer_confirmed(monkeypatch, repo)+ model_inputs: list[list[Any]] = []+ aborted_calls: list[int] = []+ tool_call_streamed = asyncio.Event()+ release_model = asyncio.Event()+ call = ResponseFunctionToolCall(type="function_call", name="inspect_page", call_id="call_inspect", arguments="{}")++ @function_tool+ async def inspect_page() -> str:+ return "page inspected"++ class StreamingToolCallModel(Model):+ async def get_response(self, *args: Any, **kwargs: Any) -> ModelResponse:+ raise AssertionError("The production runner must stream")++ async def stream_response(self, *args: Any, **kwargs: Any):+ model_inputs.append(kwargs["input"] if "input" in kwargs else args[1])+ index = len(model_inputs)+ if index == 1:+ yield ResponseOutputItemDoneEvent(+ item=call, output_index=0, sequence_number=0, type="response.output_item.done"+ )+ tool_call_streamed.set()+ try:+ await release_model.wait()+ except asyncio.CancelledError:+ aborted_calls.append(index)+ raise+ yield _completed(index, [call])+ return+ yield _completed(index, [_reply("Built it, and it extracts the title.")])++ stream = _RecordingStream()+ session = SQLiteSession("streamed-tool-call")+ async with client:+ turn = asyncio.create_task(+ run_with_enforcement(+ agent=Agent(name="steer-test", model=StreamingToolCallModel(), tools=[inspect_page]),+ initial_input="Build the workflow",+ ctx=ctx,+ stream=stream,+ session=session,+ hooks=CopilotRunHooks(ctx),+ run_config=RunConfig(tracing_disabled=True),+ )+ )+ try:+ await asyncio.wait_for(tool_call_streamed.wait(), 5)+ sent = await client.post(+ "/steer",+ json={+ "workflow_copilot_chat_id": ctx.workflow_copilot_chat_id,+ "cancel_token": "stop",+ "steer_id": "steer-1",+ "message": STEER_TEXT,+ },+ )+ assert sent.status_code == 200, sent.text+ await asyncio.wait_for(steer_confirmed.wait(), 5)+ release_model.set()+ result = await asyncio.wait_for(turn, 5)++ assert aborted_calls == []+ assert result.final_output == "Built it, and it extracts the title."+ next_input = model_inputs[1]+ assert [item["output"] for item in next_input if item.get("type") == "function_call_output"] == [+ "page inspected"+ ]+ assert next_input[-1] == {"role": "user", "content": STEER_TEXT}+ finally:+ release_model.set()+ if not turn.done():+ turn.cancel()+ await asyncio.gather(turn, return_exceptions=True)+ session.close()+++async def test_steer_endpoint_screens_once_and_binds_only_to_the_running_turn(sqlite_engine, monkeypatch):+ from skyvern.forge.sdk.routes import workflow_copilot as routes++ repo, client, ctx, _ = await setup_question_chat(sqlite_engine, monkeypatch)+ monkeypatch.setattr(app, "CACHE", LocalCache())+ secret = "sk-proj-" + "aB3dE5fG7hJ9kL2mN4pQ6rS8tU0vW1xY" * 3+ body = {+ "workflow_copilot_chat_id": ctx.workflow_copilot_chat_id,+ "cancel_token": "stop",+ "steer_id": "steer-1",+ "message": f"Use the key {secret} for the API step",+ }+ async with client:+ foreign = await client.post("/steer", json=body, headers={"test-org": "foreign"})+ assert foreign.status_code == 404+ other_turn = await client.post("/steer", json={**body, "cancel_token": "other"})+ assert other_turn.status_code == 409+ routes.resolve_raw_secret_safety_handler.assert_not_awaited()++ accepted = await client.post("/steer", json=body)+ assert accepted.status_code == 200, accepted.text+ assert secret not in accepted.text+ retried = await client.post("/steer", json={**body, "message": "A different retry body"})+ assert retried.json() == accepted.json()+ routes.resolve_raw_secret_safety_handler.assert_awaited_once()++ chat = await repo.get_workflow_copilot_chat_by_id("org", ctx.workflow_copilot_chat_id)+ stored = chat.pending_turns["turn"].steer_messages+ assert [item.steer_id for item in stored] == ["steer-1"]+ assert secret not in stored[0].text+ assert await app.CACHE.get("copilot_steer:org:stop") == "steer-1"++ monkeypatch.setattr("skyvern.forge.sdk.db.repositories.workflow_parameters.MAX_STEER_MESSAGES_PER_TURN", 1)+ over_cap = await client.post("/steer", json={**body, "steer_id": "steer-2"})+ assert over_cap.status_code == 409+ routes.resolve_raw_secret_safety_handler.assert_awaited_once()+++async def test_doorbell_naming_a_message_this_turn_never_recorded_does_not_interrupt_it(sqlite_engine, monkeypatch):+ _, _, ctx, _ = await setup_question_chat(sqlite_engine, monkeypatch)+ cache = DoorbellCache()+ monkeypatch.setattr(app, "CACHE", cache)+ monkeypatch.setattr("skyvern.forge.sdk.copilot.steer.STEER_POLL_SECONDS", 0.01)+ monkeypatch.setattr("skyvern.forge.sdk.copilot.streaming_adapter.stream_to_sse", _consume_sdk_stream)+ await cache.set("copilot_steer:org:stop", "steer-from-an-earlier-turn")+ release_model = asyncio.Event()+ aborted_calls: list[int] = []++ class SlowModel(Model):+ async def get_response(self, *args: Any, **kwargs: Any) -> ModelResponse:+ raise AssertionError("The production runner must stream")++ async def stream_response(self, *args: Any, **kwargs: Any):+ try:+ await release_model.wait()+ except asyncio.CancelledError:+ aborted_calls.append(1)+ raise+ yield _completed(1, [_reply("Done.")])++ session = SQLiteSession("stale-doorbell")+ turn = asyncio.create_task(+ run_with_enforcement(+ agent=Agent(name="steer-test", model=SlowModel(), tools=[]),+ initial_input="Build the workflow",+ ctx=ctx,+ stream=MagicMock(),+ session=session,+ hooks=CopilotRunHooks(ctx),+ run_config=RunConfig(tracing_disabled=True),+ )+ )+ try:+ await asyncio.wait_for(cache.rang.wait(), 5)+ await asyncio.sleep(0.2)+ release_model.set()+ result = await asyncio.wait_for(turn, 5)+ assert result.final_output == "Done."+ assert aborted_calls == []+ finally:+ release_model.set()+ if not turn.done():+ turn.cancel()+ await asyncio.gather(turn, return_exceptions=True)+ session.close()+++async def test_messages_keep_their_send_order_when_screens_finish_out_of_order(sqlite_engine, monkeypatch):+ from skyvern.forge.sdk.routes import workflow_copilot as routes++ repo, client, ctx, _ = await setup_question_chat(sqlite_engine, monkeypatch)+ monkeypatch.setattr(app, "CACHE", LocalCache())+ screened = {"first": asyncio.Event(), "second": asyncio.Event()}++ async def screen_when_released(text: str, *_args: object, **_kwargs: object) -> SimpleNamespace:+ await screened[text].wait()+ return SimpleNamespace(status="clean", canonical_user_message=text)++ monkeypatch.setattr(routes, "_screen_raw_secret_safety", screen_when_released)+ body = {"workflow_copilot_chat_id": ctx.workflow_copilot_chat_id, "cancel_token": "stop"}+ async with client:+ first = asyncio.create_task(client.post("/steer", json={**body, "steer_id": "steer-1", "message": "first"}))+ await asyncio.sleep(0.05)+ second = asyncio.create_task(client.post("/steer", json={**body, "steer_id": "steer-2", "message": "second"}))+ await asyncio.sleep(0.05)+ screened["second"].set()+ assert (await asyncio.wait_for(second, 5)).status_code == 200+ screened["first"].set()+ assert (await asyncio.wait_for(first, 5)).status_code == 200++ steers = await repo.take_copilot_steer_messages("org", ctx.workflow_copilot_chat_id, "turn")+ assert [steer.text for steer in steers] == ["first", "second"]diff --git a/tests/unit/test_request_logging.py b/tests/unit/test_request_logging.pyindex c409af5b4..85cf695e0 100644--- a/tests/unit/test_request_logging.py+++ b/tests/unit/test_request_logging.py@@ -584,6 +584,7 @@ class TestSanitizeBody:["/v1/workflow/copilot/chat-post","/v1/workflow/copilot/question-response",+ "/v1/workflow/copilot/steer","/v1/workflow/copilot/credential-response","/v1/workflow/copilot/convert-yaml-to-blocks",],