Skip to content

Automate browser based workflows with AI

Keep copilot test-run action rows visible at the terminal turn (#7645)

Celal Zamanoğlua540d3b4f3c6
diff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.blockActions.test.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.blockActions.test.ts
index be7b5a5b6..35d42a377 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.blockActions.test.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.blockActions.test.ts
@@ -213,7 +213,11 @@ describe("applyNarrativeEvent — client_block_actions", () => {
expect(block.recordedActionsAt).toBe(1_000);
});
- it("carries recordedActions through a response event that hydrates from a real narrative_payload", () => {
+ it("restores the run-block id and carries recordedActions when the terminal payload omits workflowRunBlockId (real BE shape)", () => {
+ // The BE NarrativeBlock TypedDict has no workflowRunBlockId field, so a
+ // hydrated terminal block arrives with "". The terminal frame re-associates
+ // it with the live block by label, restoring the real id AND the recorded
+ // actions. Fails on the id-only reducer (which dropped both).
const withActions = reduce([
...oneBlockRunning,
blockActions({
@@ -239,7 +243,6 @@ describe("applyNarrativeEvent — client_block_actions", () => {
terminal: "response",
blocks: [
{
- workflowRunBlockId: "wrb_open_search",
label: "open_search",
blockType: "code",
state: "completed",
@@ -253,10 +256,220 @@ describe("applyNarrativeEvent — client_block_actions", () => {
};
const after = applyNarrativeEvent(withActions, responseEvent);
const block = after.blocks.find((b) => b.label === "open_search")!;
+ expect(block.workflowRunBlockId).toBe("wrb_open_search");
expect(block.recordedActions).toHaveLength(1);
expect(block.recordedActionsAt).toBe(1_000);
});
+ it("drops recorded actions at hydration when the label is ambiguous (loop iterations share it)", () => {
+ // Two run-block ids, same label — the payload can't tell them apart once it
+ // omits the id, so the safe outcome is today's drop, not mis-attribution.
+ const withActions = reduce([
+ turnStart(),
+ blockProgress({ block_label: "iterate", status: "running" }),
+ blockProgress({ block_label: "iterate", status: "completed" }),
+ ]);
+ // Second same-label block with a distinct id, plus its recorded actions.
+ const twoIds: TurnNarrativeState = {
+ ...withActions,
+ blocks: [
+ ...withActions.blocks,
+ {
+ ...withActions.blocks[0]!,
+ workflowRunBlockId: "wrb_iterate_2",
+ },
+ ],
+ };
+ const withBoth = applyNarrativeEvent(
+ twoIds,
+ blockActions({
+ blocks: [
+ {
+ workflowRunBlockId: "wrb_iterate",
+ actions: [recordedAction({ actionId: "a1" })],
+ },
+ {
+ workflowRunBlockId: "wrb_iterate_2",
+ actions: [recordedAction({ actionId: "a2" })],
+ },
+ ],
+ }),
+ );
+ const responseEvent: WorkflowCopilotStreamResponseUpdate = {
+ type: "response",
+ workflow_copilot_chat_id: "chat_1",
+ message: "Done",
+ response_time: "2026-06-10T00:01:00Z",
+ proposal_disposition: "no_proposal",
+ turn_id: "turn-1",
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ mode: "build",
+ terminal: "response",
+ blocks: [
+ {
+ label: "iterate",
+ blockType: "code",
+ state: "completed",
+ lastSeenIteration: 0,
+ activity: [],
+ startedAt: "2026-06-10T00:00:04Z",
+ endedAt: "2026-06-10T00:01:00Z",
+ },
+ {
+ label: "iterate",
+ blockType: "code",
+ state: "completed",
+ lastSeenIteration: 0,
+ activity: [],
+ startedAt: "2026-06-10T00:00:04Z",
+ endedAt: "2026-06-10T00:01:00Z",
+ },
+ ],
+ },
+ };
+ const after = applyNarrativeEvent(withBoth, responseEvent);
+ expect(after.blocks.every((b) => b.recordedActions === undefined)).toBe(
+ true,
+ );
+ });
+
+ it("does not mis-attribute when only the hydrated side duplicates a label", () => {
+ // One recorded source block with a unique label, but the terminal payload
+ // carries two blocks under that same label. Uniqueness must hold on BOTH
+ // sides, so the ambiguous hydrated pair drops rather than both copying the
+ // single source block's actions.
+ const withActions = reduce([
+ turnStart(),
+ blockProgress({ block_label: "extract", status: "running" }),
+ blockProgress({ block_label: "extract", status: "completed" }),
+ blockActions({
+ blocks: [
+ {
+ workflowRunBlockId: "wrb_extract",
+ actions: [recordedAction({ actionId: "a1" })],
+ },
+ ],
+ }),
+ ]);
+ const dupBlock = {
+ label: "extract",
+ blockType: "code",
+ state: "completed",
+ lastSeenIteration: 0,
+ activity: [],
+ startedAt: "2026-06-10T00:00:04Z",
+ endedAt: "2026-06-10T00:01:00Z",
+ };
+ const responseEvent: WorkflowCopilotStreamResponseUpdate = {
+ type: "response",
+ workflow_copilot_chat_id: "chat_1",
+ message: "Done",
+ response_time: "2026-06-10T00:01:00Z",
+ proposal_disposition: "no_proposal",
+ turn_id: "turn-1",
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ mode: "build",
+ terminal: "response",
+ blocks: [dupBlock, { ...dupBlock }],
+ },
+ };
+ const after = applyNarrativeEvent(withActions, responseEvent);
+ expect(after.blocks.every((b) => b.recordedActions === undefined)).toBe(
+ true,
+ );
+ });
+
+ const terminalResponse = (): WorkflowCopilotStreamResponseUpdate => ({
+ type: "response",
+ workflow_copilot_chat_id: "chat_1",
+ message: "Done",
+ response_time: "2026-06-10T00:01:00Z",
+ proposal_disposition: "no_proposal",
+ turn_id: "turn-1",
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ mode: "build",
+ terminal: "response",
+ blocks: [
+ {
+ label: "open_search",
+ blockType: "code",
+ state: "completed",
+ lastSeenIteration: 0,
+ activity: [],
+ startedAt: "2026-06-10T00:00:04Z",
+ endedAt: "2026-06-10T00:01:00Z",
+ },
+ ],
+ },
+ });
+
+ it("re-attaches a post-terminal fetch by the run-block id the terminal frame restored", () => {
+ // The recorded-actions fetch resolves AFTER the terminal frame froze the
+ // narrative. The terminal frame restored the real run-block id from the live
+ // block, so the late fetch matches by id (no label fallback needed).
+ const frozen = applyNarrativeEvent(
+ reduce(oneBlockRunning),
+ terminalResponse(),
+ );
+ expect(frozen.blocks[0]!.workflowRunBlockId).toBe("wrb_open_search");
+ const after = applyNarrativeEvent(
+ frozen,
+ blockActions({
+ blocks: [
+ {
+ workflowRunBlockId: "wrb_open_search",
+ actions: [recordedAction({ actionId: "a1" })],
+ },
+ ],
+ }),
+ );
+ const block = after.blocks.find((b) => b.label === "open_search")!;
+ expect(block.recordedActions).toHaveLength(1);
+ expect(block.recordedActions![0]!.actionId).toBe("a1");
+ });
+
+ it("does not graft a later run's actions onto a prior turn's frozen block with the same label (P1)", () => {
+ // A prior turn's frozen block keeps its restored id and its own actions. A
+ // new run reuses the label but has a different id, so its fetch matches by
+ // id and never touches the prior turn.
+ const frozen = applyNarrativeEvent(
+ reduce([
+ ...oneBlockRunning,
+ blockActions({
+ blocks: [
+ {
+ workflowRunBlockId: "wrb_open_search",
+ actions: [recordedAction({ actionId: "a1" })],
+ },
+ ],
+ }),
+ ]),
+ terminalResponse(),
+ );
+ expect(frozen.blocks[0]!.workflowRunBlockId).toBe("wrb_open_search");
+ const after = applyNarrativeEvent(
+ frozen,
+ blockActions({
+ blocks: [
+ {
+ workflowRunBlockId: "wrb_open_search_run2",
+ actions: [recordedAction({ actionId: "b1" })],
+ },
+ ],
+ }),
+ );
+ expect(after).toBe(frozen);
+ expect(after.blocks[0]!.recordedActions!.map((a) => a.actionId)).toEqual([
+ "a1",
+ ]);
+ });
+
it("staggers a second block's reveal start past the first block's own schedule total", () => {
const s = reduce([
...twoBlocksRunning,
diff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
index bb36617d9..3143de2a2 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
@@ -575,6 +575,21 @@ export function isBlockOk(
);
}
+// Labels that occur exactly once in the given set — the only labels safe to
+// key recorded actions on when the run-block id is missing (the terminal
+// narrative_payload drops workflowRunBlockId). Loop iterations reuse a label,
+// so an ambiguous label falls back to today's drop rather than mis-attributing.
+function uniqueLabelSet(labels: Array<string | undefined>): Set<string> {
+ const counts = new Map<string, number>();
+ for (const label of labels) {
+ if (!label) continue;
+ counts.set(label, (counts.get(label) ?? 0) + 1);
+ }
+ const unique = new Set<string>();
+ for (const [label, n] of counts) if (n === 1) unique.add(label);
+ return unique;
+}
+
export function applyNarrativeEvent(
prev: TurnNarrativeState,
event: NarrativeEvent,
@@ -728,6 +743,10 @@ export function applyNarrativeEvent(
// visibly wrong, re-merge matched actionIds instead of skipping them.
let carry = 0;
let changed = false;
+ // Match by run-block id only. A terminal frame restores each block's real
+ // id from the live blocks (see the `response` case), so a post-terminal
+ // fetch still matches by id — and a different run's ids never collide with
+ // this turn's, keeping late fetches from cross-contaminating prior turns.
const blocks = prev.blocks.map((b) => {
const match = event.blocks.find(
(entry) => entry.workflowRunBlockId === b.workflowRunBlockId,
@@ -840,24 +859,44 @@ export function applyNarrativeEvent(
case "response": {
const hydrated = hydrateNarrativeFromPayload(event.narrative_payload);
if (hydrated) {
- // The backend payload never carries recordedActions/recordedActionsAt
- // (client-only replay data); carry them over from the prior live
- // blocks so a fetch that resolved before this terminal frame
- // survives hydration instead of being silently dropped.
- const recordedById = new Map(
+ // The BE narrative_payload drops workflowRunBlockId and the client-only
+ // recordedActions. Re-associate each hydrated block with the live block
+ // of the same label to restore its real run-block id (and carry any
+ // recordedActions). Restoring the real id keeps this frozen turn keyed
+ // by id, so a later test run that reuses a label matches by id and can't
+ // graft its actions onto this turn. Unique labels only — loop iterations
+ // reuse a label and can't be told apart without the id.
+ const liveById = new Map(
prev.blocks
- .filter((b) => b.recordedActions !== undefined)
+ .filter((b) => b.workflowRunBlockId !== "")
.map((b) => [b.workflowRunBlockId, b] as const),
);
+ const uniqueLiveLabels = uniqueLabelSet(
+ prev.blocks.map((b) => b.label),
+ );
+ const uniqueHydratedLabels = uniqueLabelSet(
+ hydrated.blocks.map((b) => b.label),
+ );
+ const liveByLabel = new Map(
+ prev.blocks
+ .filter((b) => uniqueLiveLabels.has(b.label))
+ .map((b) => [b.label, b] as const),
+ );
const blocks = hydrated.blocks.map((b) => {
- const carried = recordedById.get(b.workflowRunBlockId);
- return carried
- ? {
- ...b,
- recordedActions: carried.recordedActions,
- recordedActionsAt: carried.recordedActionsAt,
- }
- : b;
+ const live =
+ (b.workflowRunBlockId !== ""
+ ? liveById.get(b.workflowRunBlockId)
+ : undefined) ??
+ (b.workflowRunBlockId === "" && uniqueHydratedLabels.has(b.label)
+ ? liveByLabel.get(b.label)
+ : undefined);
+ if (!live) return b;
+ return {
+ ...b,
+ workflowRunBlockId: live.workflowRunBlockId,
+ recordedActions: live.recordedActions,
+ recordedActionsAt: live.recordedActionsAt,
+ };
});
return {
...hydrated,