diff --git a/apps/docs/openapi-core.json b/apps/docs/openapi-core.json index 522584c295d..29d741589fb 100644 --- a/apps/docs/openapi-core.json +++ b/apps/docs/openapi-core.json @@ -638,6 +638,16 @@ "type": "string", "example": "e4f8d2b6-9a1c-4e3d-8b7f-5c0a2d9e6f13" } + }, + { + "name": "includeSnapshot", + "in": "query", + "required": false, + "description": "Whether to include `executionSnapshot`, which carries every block's output and can be large. Defaults to `true`.", + "schema": { + "type": "boolean", + "default": true + } } ], "responses": { @@ -1792,6 +1802,10 @@ "type": "string", "description": "The block ID where execution paused." }, + "blockName": { + "type": "string", + "description": "Name of the paused block as the run executed it. Absent for a point paused before names were recorded whose run had no deployment version." + }, "response": { "description": "Data returned by the block before pausing, including display data and form fields." }, @@ -1949,7 +1963,7 @@ "properties": { "executionSnapshot": { "type": "object", - "description": "Serialized execution state for resumption.", + "description": "Serialized execution state for resumption. Omitted when the resume path is read with `includeSnapshot=false`.", "properties": { "snapshot": { "type": "string", diff --git a/apps/docs/openapi-v2-workflows.json b/apps/docs/openapi-v2-workflows.json index 7918cdc1183..eb1136d7e0d 100644 --- a/apps/docs/openapi-v2-workflows.json +++ b/apps/docs/openapi-v2-workflows.json @@ -16140,7 +16140,7 @@ "type": "null" } ], - "description": "Name of the block in the deployment version the run executed, or null for a manual run or when the version no longer exists." + "description": "Name of the block as the run executed it. Null only for a point paused before names were recorded whose run was manual or whose deployment version no longer exists." }, "kind": { "type": "string", diff --git a/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/page.test.tsx b/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/page.test.tsx index 4fe6530e15c..26e919a3c93 100644 --- a/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/page.test.tsx +++ b/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/page.test.tsx @@ -64,7 +64,7 @@ describe('ResumeExecutionPageWrapper', () => { expect(mocks.authorize).toHaveBeenCalledWith({ principal: createSessionPrincipal(), - input: PAGE_PARAMS, + input: { ...PAGE_PARAMS, includeSnapshot: false }, }) expect(result.props).toMatchObject({ params: PAGE_PARAMS, diff --git a/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/page.tsx b/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/page.tsx index 4dc78759d15..ecefad05f73 100644 --- a/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/page.tsx +++ b/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/page.tsx @@ -53,7 +53,7 @@ export default async function ResumeExecutionPageWrapper({ userId: session.user.id, sessionId: session.session.id, }, - input: { workflowId, executionId }, + input: { workflowId, executionId, includeSnapshot: false }, }) } catch (error) { const classified = asOrchestrationError(error) diff --git a/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/resume-page-client.tsx b/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/resume-page-client.tsx index f5af1bcfcb6..47b7e055964 100644 --- a/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/resume-page-client.tsx +++ b/apps/sim/app/(interfaces)/resume/[workflowId]/[executionId]/resume-page-client.tsx @@ -106,22 +106,6 @@ function StatusBadge({ status }: { status: string }) { ) } -function getBlockNameFromSnapshot( - executionSnapshot: { snapshot?: string } | null | undefined, - blockId: string | undefined -): string | null { - if (!executionSnapshot?.snapshot || !blockId) return null - try { - const parsed = JSON.parse(executionSnapshot.snapshot) - const workflowState = parsed?.workflow - if (!workflowState?.blocks || !Array.isArray(workflowState.blocks)) return null - const block = workflowState.blocks.find((b: { id: string }) => b.id === blockId) - return block?.metadata?.name || null - } catch { - return null - } -} - const DISPLAY_VALUE_PREVIEW_MAX_CHARS = 5000 function truncateForPreview(text: string): { text: string; truncated: boolean } { @@ -697,13 +681,7 @@ export default function ResumeExecutionPage({ selectedStatus === 'queued' || (isHumanMode && hasInputFormat && (!isFormComplete || Object.keys(formErrors).length > 0)) - const getBlockName = (pause: PausePointWithQueue) => { - const pauseBlockId = pause.blockId || pause.triggerBlockId - return ( - getBlockNameFromSnapshot(executionDetail?.executionSnapshot, pauseBlockId) || - 'Human in the Loop' - ) - } + const getBlockName = (pause: PausePointWithQueue) => pause.blockName || 'Human in the Loop' if (loadingExecution) { return ( diff --git a/apps/sim/app/api/resume/[workflowId]/[executionId]/route.test.ts b/apps/sim/app/api/resume/[workflowId]/[executionId]/route.test.ts index 78a2faf1db9..021bd64abdc 100644 --- a/apps/sim/app/api/resume/[workflowId]/[executionId]/route.test.ts +++ b/apps/sim/app/api/resume/[workflowId]/[executionId]/route.test.ts @@ -121,4 +121,23 @@ describe('GET /api/resume/[workflowId]/[executionId]', () => { expect(response.status).toBe(403) expect(await response.json()).toEqual({ error: 'Insufficient workspace permissions' }) }) + + it('reads the execution snapshot unless the resume page opts out', async () => { + await GET( + createMockRequest( + 'GET', + undefined, + {}, + 'http://localhost/api/resume/workflow-1/execution-1?includeSnapshot=false' + ), + createRouteContext(params) + ) + for (const { call } of routeCases) await call() + + expect(mocks.execute.mock.calls.map(([call]) => call.input.includeSnapshot)).toEqual([ + false, + true, + true, + ]) + }) }) diff --git a/apps/sim/app/api/resume/[workflowId]/[executionId]/route.ts b/apps/sim/app/api/resume/[workflowId]/[executionId]/route.ts index 950c27ea42c..5f60313ec56 100644 --- a/apps/sim/app/api/resume/[workflowId]/[executionId]/route.ts +++ b/apps/sim/app/api/resume/[workflowId]/[executionId]/route.ts @@ -15,9 +15,10 @@ export const GET = defineInternalJsonRoute({ reason: 'Preserve existing authenticated resume-detail behavior', }), errorPolicy: internalWorkflowErrorPolicies.concealWorkflowAuthorization, - mapInput: ({ params }) => ({ + mapInput: ({ params, query }) => ({ workflowId: params.workflowId, executionId: params.executionId, + includeSnapshot: query.includeSnapshot, }), useCase: readPausedWorkflowExecution, responseHeaders: () => ({ 'Cache-Control': 'private, no-store' }), diff --git a/apps/sim/app/api/workflows/[id]/paused/[executionId]/route.ts b/apps/sim/app/api/workflows/[id]/paused/[executionId]/route.ts index 916e7c22e15..7b3c3da6382 100644 --- a/apps/sim/app/api/workflows/[id]/paused/[executionId]/route.ts +++ b/apps/sim/app/api/workflows/[id]/paused/[executionId]/route.ts @@ -18,6 +18,7 @@ export const GET = defineInternalJsonRoute({ mapInput: ({ params }) => ({ workflowId: params.id, executionId: params.executionId, + includeSnapshot: true, }), useCase: readPausedWorkflowExecution, responseHeaders: () => ({ 'Cache-Control': 'private, no-store' }), diff --git a/apps/sim/executor/execution/engine.test.ts b/apps/sim/executor/execution/engine.test.ts index 9bef693a364..2832281fd2a 100644 --- a/apps/sim/executor/execution/engine.test.ts +++ b/apps/sim/executor/execution/engine.test.ts @@ -413,7 +413,7 @@ describe('ExecutionEngine', () => { expect(nodeOrchestrator.executeNode).toHaveBeenCalledWith(context, 'both') }) - it('records paused block completion before returning paused result', async () => { + it('records paused block completion and the paused block name before returning paused result', async () => { const node = createMockNode('hitl', 'function') const dag = createMockDAG([node]) const context = createMockContext({ @@ -441,6 +441,9 @@ describe('ExecutionEngine', () => { const result = await engine.run('hitl') expect(result.status).toBe('paused') + expect(result.pausePoints).toEqual([ + expect.objectContaining({ contextId: 'pause-1', blockId: 'hitl', blockName: 'Block hitl' }), + ]) expect(nodeOrchestrator.handleNodeCompletion).toHaveBeenCalledWith( context, 'hitl', diff --git a/apps/sim/executor/execution/engine.ts b/apps/sim/executor/execution/engine.ts index 092c830e2a6..a2db926f047 100644 --- a/apps/sim/executor/execution/engine.ts +++ b/apps/sim/executor/execution/engine.ts @@ -657,6 +657,7 @@ export class ExecutionEngine { ...capturePauseDisplay(pause, registry), contextId: pause.contextId, blockId: pause.blockId, + blockName: this.dag.nodes.get(pause.blockId)?.block.metadata?.name, response: pause.response, registeredAt: pause.timestamp, resumeStatus: 'paused' as ResumeStatus, diff --git a/apps/sim/executor/types.ts b/apps/sim/executor/types.ts index 6c384a38191..f54c45e6528 100644 --- a/apps/sim/executor/types.ts +++ b/apps/sim/executor/types.ts @@ -81,6 +81,12 @@ export type ResumeStatus = 'paused' | 'resumed' | 'failed' | 'queued' | 'resumin export interface PausePoint { contextId: string blockId?: string + /** + * The paused block's name as the run executed it, recorded when the pause is captured. + * Points persisted before names were recorded lack it; readers name those from the run's + * deployment version instead of the execution snapshot. + */ + blockName?: string response: any registeredAt: string resumeStatus: ResumeStatus diff --git a/apps/sim/hooks/queries/resume-execution.ts b/apps/sim/hooks/queries/resume-execution.ts index 2930d9e5636..b5f1a6dbe6d 100644 --- a/apps/sim/hooks/queries/resume-execution.ts +++ b/apps/sim/hooks/queries/resume-execution.ts @@ -41,7 +41,6 @@ export interface ResumeQueueEntrySummary { export interface PausePointWithQueue { contextId: string - triggerBlockId?: string blockId?: string response: any registeredAt: string @@ -51,6 +50,7 @@ export interface PausePointWithQueue { resumeLinks?: ResumeLinks queuePosition?: number | null latestResumeEntry?: ResumeQueueEntrySummary | null + blockName?: string parallelScope?: any loopScope?: any pauseKind?: 'human' | 'time' @@ -73,7 +73,6 @@ export interface PausedExecutionSummary { } export interface PausedExecutionDetail extends PausedExecutionSummary { - executionSnapshot: any queue: ResumeQueueEntrySummary[] } @@ -118,6 +117,7 @@ export function useResumeExecutionDetail(workflowId: string, executionId: string queryFn: async ({ signal }): Promise => { const raw = await requestJson(resumeWorkflowExecutionContract, { params: { workflowId, executionId }, + query: { includeSnapshot: false }, signal, }) // double-cast-allowed: contract models pause points as z.record; the resume UI uses the richer PausedExecutionDetail interface diff --git a/apps/sim/lib/api/contracts/v2/workflows.ts b/apps/sim/lib/api/contracts/v2/workflows.ts index ce9558a9b7e..1bd14229d26 100644 --- a/apps/sim/lib/api/contracts/v2/workflows.ts +++ b/apps/sim/lib/api/contracts/v2/workflows.ts @@ -2173,7 +2173,7 @@ const v2WorkflowRunPausePointSchema = z .string() .nullable() .describe( - 'Name of the block in the deployment version the run executed, or null for a manual run or when the version no longer exists.' + 'Name of the block as the run executed it. Null only for a point paused before names were recorded whose run was manual or whose deployment version no longer exists.' ), kind: z .enum(['human', 'time']) diff --git a/apps/sim/lib/api/contracts/workflows.ts b/apps/sim/lib/api/contracts/workflows.ts index b5eabc856fa..326ee0b79a1 100644 --- a/apps/sim/lib/api/contracts/workflows.ts +++ b/apps/sim/lib/api/contracts/workflows.ts @@ -6,6 +6,7 @@ import { } from '@sim/workflow-types/workflow' import { z } from 'zod' import { + booleanQueryFlagSchema, privateSecretProvenanceBundleSchema, requiredFieldSchema, workflowIdSchema, @@ -442,6 +443,15 @@ export const resumeExecutionParamsSchema = z.object({ executionId: z.string().min(1), }) +/** + * `includeSnapshot=false` leaves out the execution snapshot, which carries every + * block's output and can run to many megabytes. The resume page asks for that; + * API callers keep the documented default of receiving it. + */ +const resumeExecutionQuerySchema = z.object({ + includeSnapshot: booleanQueryFlagSchema.optional().default(true), +}) + export const resumeExecutionContextParamsSchema = z.object({ workflowId: z.string().min(1), executionId: z.string().min(1), @@ -554,7 +564,8 @@ const pausedWorkflowExecutionsResponseSchema = z.object({ }) const pausedWorkflowExecutionDetailSchema = pausedWorkflowExecutionSummarySchema.extend({ - executionSnapshot: z.unknown(), + /** Present when the read asked for it; see {@link resumeExecutionQuerySchema}. */ + executionSnapshot: z.unknown().optional(), queue: z.array(z.record(z.string(), z.unknown())), }) @@ -959,6 +970,7 @@ export const resumeWorkflowExecutionContract = defineRouteContract({ method: 'GET', path: '/api/resume/[workflowId]/[executionId]', params: resumeExecutionParamsSchema, + query: resumeExecutionQuerySchema, response: { mode: 'json', schema: pausedWorkflowExecutionDetailSchema, diff --git a/apps/sim/lib/workflows/application/read-paused-workflow-execution.test.ts b/apps/sim/lib/workflows/application/read-paused-workflow-execution.test.ts index 749750f37b6..862e8766fba 100644 --- a/apps/sim/lib/workflows/application/read-paused-workflow-execution.test.ts +++ b/apps/sim/lib/workflows/application/read-paused-workflow-execution.test.ts @@ -72,7 +72,7 @@ describe('readPausedWorkflowExecution', () => { async (principal) => { const result = await readPausedWorkflowExecution.execute({ principal, - input: { workflowId: 'workflow-1', executionId: 'execution-1' }, + input: { workflowId: 'workflow-1', executionId: 'execution-1', includeSnapshot: true }, }) expect(result).toBe(detail) @@ -87,7 +87,7 @@ describe('readPausedWorkflowExecution', () => { it('finishes session authorization before loading paused execution detail', async () => { await readPausedWorkflowExecution.execute({ principal: allowedPrincipals[0], - input: { workflowId: 'workflow-1', executionId: 'execution-1' }, + input: { workflowId: 'workflow-1', executionId: 'execution-1', includeSnapshot: true }, }) expect(mockResolvePermission.mock.invocationCallOrder[0]).toBeLessThan( @@ -110,7 +110,7 @@ describe('readPausedWorkflowExecution', () => { await expect( readPausedWorkflowExecution.execute({ principal, - input: { workflowId: 'workflow-1', executionId: 'execution-1' }, + input: { workflowId: 'workflow-1', executionId: 'execution-1', includeSnapshot: true }, }) ).rejects.toMatchObject({ name: 'DelegatedServiceAuthorizationError' }) expect(mockResolveWorkflowContext).not.toHaveBeenCalled() @@ -128,7 +128,7 @@ describe('readPausedWorkflowExecution', () => { await expect( readPausedWorkflowExecution.execute({ principal, - input: { workflowId: 'workflow-1', executionId: 'execution-1' }, + input: { workflowId: 'workflow-1', executionId: 'execution-1', includeSnapshot: true }, }) ).rejects.toMatchObject({ name: 'PrincipalKindAuthorizationError' }) expect(mockResolveWorkflowContext).not.toHaveBeenCalled() @@ -142,7 +142,7 @@ describe('readPausedWorkflowExecution', () => { workspaceId: 'workspace-2', keyId: 'workspace-key-2', }), - input: { workflowId: 'workflow-1', executionId: 'execution-1' }, + input: { workflowId: 'workflow-1', executionId: 'execution-1', includeSnapshot: true }, }) ).rejects.toMatchObject({ code: 'forbidden' }) expect(mockGetPausedExecutionDetail).not.toHaveBeenCalled() @@ -154,7 +154,7 @@ describe('readPausedWorkflowExecution', () => { await expect( readPausedWorkflowExecution.execute({ principal: allowedPrincipals[0], - input: { workflowId: 'workflow-1', executionId: 'execution-1' }, + input: { workflowId: 'workflow-1', executionId: 'execution-1', includeSnapshot: true }, }) ).rejects.toMatchObject({ name: 'NoWorkspaceAccessError' }) expect(mockGetPausedExecutionDetail).not.toHaveBeenCalled() @@ -169,7 +169,7 @@ describe('readPausedWorkflowExecution', () => { await expect( readPausedWorkflowExecution.execute({ principal: allowedPrincipals[1], - input: { workflowId: 'workflow-1', executionId: 'execution-1' }, + input: { workflowId: 'workflow-1', executionId: 'execution-1', includeSnapshot: true }, }) ).rejects.toMatchObject({ name: 'PersonalApiKeysDisabledError' }) expect(mockGetPausedExecutionDetail).not.toHaveBeenCalled() diff --git a/apps/sim/lib/workflows/application/read-paused-workflow-execution.ts b/apps/sim/lib/workflows/application/read-paused-workflow-execution.ts index 958bfdea234..7fae5e83343 100644 --- a/apps/sim/lib/workflows/application/read-paused-workflow-execution.ts +++ b/apps/sim/lib/workflows/application/read-paused-workflow-execution.ts @@ -7,6 +7,11 @@ import { PauseResumeManager } from '@/lib/workflows/executor/human-in-the-loop-m export interface ReadPausedWorkflowExecutionInput { workflowId: string executionId: string + /** + * Also read the execution snapshot. It carries every block's output and can run to + * many megabytes, so only a caller that returns it asks for it. + */ + includeSnapshot: boolean } export const readPausedWorkflowExecution = defineAuthorizedWorkflowUseCase({ @@ -14,10 +19,10 @@ export const readPausedWorkflowExecution = defineAuthorizedWorkflowUseCase({ resolveContext: ({ input }: { input: ReadPausedWorkflowExecutionInput }) => resolveActiveWorkflowApplicationContext({ workflowId: input.workflowId }), async execute({ context, input }) { - const detail = await PauseResumeManager.getPausedExecutionDetail({ - workflowId: context.workflowId, - executionId: input.executionId, - }) + const target = { workflowId: context.workflowId, executionId: input.executionId } + const detail = input.includeSnapshot + ? await PauseResumeManager.getPausedExecutionDetail(target) + : await PauseResumeManager.getPausedExecutionView(target) if (!detail) throw new OrchestrationError('not_found', 'Paused execution not found') return detail }, diff --git a/apps/sim/lib/workflows/executor/execution-status.ts b/apps/sim/lib/workflows/executor/execution-status.ts index c6387f60d1e..9461c9e98ac 100644 --- a/apps/sim/lib/workflows/executor/execution-status.ts +++ b/apps/sim/lib/workflows/executor/execution-status.ts @@ -26,9 +26,9 @@ import { } from '@/lib/workflows/executor/execution-job-ids' import { normalizePausePoints } from '@/lib/workflows/executor/execution-state' import { PauseResumeManager } from '@/lib/workflows/executor/human-in-the-loop-manager' +import { namePausePoints, pauseBaseBlockId } from '@/lib/workflows/executor/pause-block-names' import { getAutomaticResumeWaitingMetadata } from '@/lib/workflows/executor/paused-execution-metadata' import type { PauseInputField, PausePoint, ResumeStatus } from '@/executor/types' -import { extractBaseBlockId } from '@/executor/utils/subflow-node-id-codec' /** * Reads a single execution's status resource — the log row, the paused-state @@ -657,47 +657,6 @@ export interface PausedRunPoint { queuePosition: number | null } -/** - * Names of `blockIds` in the deployment version the run executed, read inside the - * database so the version's state never leaves it. Empty for a run with no recorded - * version (a manual run) or whose version no longer exists. - */ -async function readRunBlockNames( - workflowId: string, - executionId: string, - blockIds: string[] -): Promise> { - if (blockIds.length === 0) return new Map() - const [row] = await db - .select({ - names: sql | null>`( - select jsonb_object_agg(b.key, b.value ->> 'name') - from json_each(${workflowDeploymentVersion.state} -> 'blocks') as b - where b.key in (${sql.join( - blockIds.map((id) => sql`${id}`), - sql`, ` - )}) - )`, - }) - .from(workflowExecutionLogs) - .innerJoin( - workflowDeploymentVersion, - eq(workflowDeploymentVersion.id, workflowExecutionLogs.deploymentVersionId) - ) - .where( - and( - eq(workflowExecutionLogs.executionId, executionId), - eq(workflowExecutionLogs.workflowId, workflowId) - ) - ) - .limit(1) - const names = new Map() - for (const [id, name] of Object.entries(row?.names ?? {})) { - if (typeof name === 'string' && name) names.set(id, name) - } - return names -} - /** * The pause points of a paused run, earliest registered first and capped at * {@link MAX_PAUSED_RUN_POINTS} (unresolved points kept first), or null when the run has no @@ -758,37 +717,34 @@ export async function readPausedRunPoints(input: { ) const mapped = PauseResumeManager.mapPausePoints(row.pausePoints, queuePositions) - const points = capPausePoints(input.only ? mapped.filter(input.only) : mapped) - const baseBlockId = (point: PausePoint) => extractBaseBlockId(point.blockId ?? point.contextId) - const names = await readRunBlockNames(workflowId, executionId, [ - ...new Set(points.map(baseBlockId)), - ]) - - return points.map((point) => { - const blockId = baseBlockId(point) - return { - contextId: point.contextId, - blockId, - blockName: names.get(blockId) ?? null, - kind: point.pauseKind === 'time' ? 'time' : 'human', - status: point.resumeStatus, - registeredAt: point.registeredAt ?? null, - resumeAt: point.resumeAt ?? null, - parallel: point.parallelScope - ? { - parallelId: point.parallelScope.parallelId, - branchIndex: point.parallelScope.branchIndex, - branchTotal: point.parallelScope.branchTotal ?? null, - } - : null, - loop: point.loopScope - ? { loopId: point.loopScope.loopId, iteration: point.loopScope.iteration } - : null, - inputFields: point.inputFields ?? null, - display: point.display ?? null, - queuePosition: point.queuePosition ?? null, - } - }) + const points = await namePausePoints( + workflowId, + executionId, + capPausePoints(input.only ? mapped.filter(input.only) : mapped) + ) + + return points.map((point) => ({ + contextId: point.contextId, + blockId: pauseBaseBlockId(point), + blockName: point.blockName ?? null, + kind: point.pauseKind === 'time' ? 'time' : 'human', + status: point.resumeStatus, + registeredAt: point.registeredAt ?? null, + resumeAt: point.resumeAt ?? null, + parallel: point.parallelScope + ? { + parallelId: point.parallelScope.parallelId, + branchIndex: point.parallelScope.branchIndex, + branchTotal: point.parallelScope.branchTotal ?? null, + } + : null, + loop: point.loopScope + ? { loopId: point.loopScope.loopId, iteration: point.loopScope.iteration } + : null, + inputFields: point.inputFields ?? null, + display: point.display ?? null, + queuePosition: point.queuePosition ?? null, + })) } /** diff --git a/apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts b/apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts index a413db2d390..8ae64fe6d77 100644 --- a/apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts +++ b/apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts @@ -48,6 +48,7 @@ import { LIVE_ONLY_EXECUTION_EVENT_TYPES, } from '@/lib/workflows/executor/execution-events' import { getWorkflowExecutionCopilotToolCallId } from '@/lib/workflows/executor/execution-state' +import { namePausePoints } from '@/lib/workflows/executor/pause-block-names' import { createPausedExecutionResumeMetadata, parsePausedExecutionResumeMetadata, @@ -426,11 +427,48 @@ type PausedExecutionSummaryRow = Pick< | 'expiresAt' > & { triggerIds: string[] | null | undefined } -interface PausedExecutionDetail extends PausedExecutionSummary { - executionSnapshot: SerializedSnapshot +/** + * The columns a paused execution is read for display with. The execution snapshot can + * run to many megabytes per row and Postgres detoasts all of it to read one key, so + * `triggerIds` come from the metadata a pause records them in, and the snapshot is + * touched only for a row persisted before metadata carried them. + */ +const pausedExecutionViewColumns = { + id: pausedExecutions.id, + workflowId: pausedExecutions.workflowId, + executionId: pausedExecutions.executionId, + status: pausedExecutions.status, + pausePoints: pausedExecutions.pausePoints, + metadata: pausedExecutions.metadata, + pausedAt: pausedExecutions.pausedAt, + updatedAt: pausedExecutions.updatedAt, + expiresAt: pausedExecutions.expiresAt, + triggerIds: sql< + string[] | null + >`coalesce(${pausedExecutions.metadata}->'triggerIds', ${pausedExecutions.executionSnapshot}->'triggerIds')`, +} + +interface PausedExecutionKey { + workflowId: string + executionId: string +} + +function pausedExecutionKeyFilter({ workflowId, executionId }: PausedExecutionKey) { + return and( + eq(pausedExecutions.workflowId, workflowId), + eq(pausedExecutions.executionId, executionId) + ) +} + +/** A paused execution with its resume queue, read without the execution snapshot. */ +interface PausedExecutionView extends PausedExecutionSummary { queue: ResumeQueueEntrySummary[] } +interface PausedExecutionDetail extends PausedExecutionView { + executionSnapshot: SerializedSnapshot +} + interface PauseContextDetail { execution: PausedExecutionSummary pausePoint: PausePointWithQueue @@ -611,6 +649,7 @@ export class PauseResumeManager { acc[point.contextId] = { contextId: point.contextId, blockId: PauseResumeManager.normalizePauseBlockId(point.blockId ?? point.contextId), + blockName: point.blockName, response: point.response, resumeStatus: point.resumeStatus, snapshotReady: point.snapshotReady, @@ -3504,24 +3543,8 @@ export class PauseResumeManager { } } - /** - * The summary reads only `triggerIds` from the execution snapshot, whose - * serialized `snapshot` string can run to megabytes per row, so that one key - * is projected instead of shipping and parsing the whole document. - */ const rows = await execDb - .select({ - id: pausedExecutions.id, - workflowId: pausedExecutions.workflowId, - executionId: pausedExecutions.executionId, - status: pausedExecutions.status, - pausePoints: pausedExecutions.pausePoints, - metadata: pausedExecutions.metadata, - pausedAt: pausedExecutions.pausedAt, - updatedAt: pausedExecutions.updatedAt, - expiresAt: pausedExecutions.expiresAt, - triggerIds: sql`${pausedExecutions.executionSnapshot}->'triggerIds'`, - }) + .select(pausedExecutionViewColumns) .from(pausedExecutions) .where(whereClause) .orderBy(desc(pausedExecutions.pausedAt)) @@ -3546,60 +3569,55 @@ export class PauseResumeManager { return rows[0] ?? null } - static async getPausedExecutionDetail(options: { - workflowId: string - executionId: string - }): Promise { - const { workflowId, executionId } = options + /** A paused execution's human pause points and resume queue, read without the execution snapshot. */ + static async getPausedExecutionView( + options: PausedExecutionKey + ): Promise { + const [row] = await execDb + .select(pausedExecutionViewColumns) + .from(pausedExecutions) + .where(pausedExecutionKeyFilter(options)) + .limit(1) + return row ? PauseResumeManager.buildPausedExecutionView(row) : null + } - const row = await execDb - .select() + /** {@link getPausedExecutionView} plus the execution snapshot a resume restores from. */ + static async getPausedExecutionDetail( + options: PausedExecutionKey + ): Promise { + const [row] = await execDb + .select({ + ...pausedExecutionViewColumns, + executionSnapshot: pausedExecutions.executionSnapshot, + }) .from(pausedExecutions) - .where( - and( - eq(pausedExecutions.workflowId, workflowId), - eq(pausedExecutions.executionId, executionId) - ) - ) + .where(pausedExecutionKeyFilter(options)) .limit(1) - .then((rows) => rows[0]) + if (!row) return null - if (!row) { - return null - } + const view = await PauseResumeManager.buildPausedExecutionView(row) + return view && { ...view, executionSnapshot: row.executionSnapshot as SerializedSnapshot } + } + private static async buildPausedExecutionView( + row: PausedExecutionSummaryRow & { pausePoints: unknown } + ): Promise { const queueEntries = await execDb .select() .from(resumeQueue) - .where(eq(resumeQueue.parentExecutionId, executionId)) + .where(eq(resumeQueue.parentExecutionId, row.executionId)) .orderBy(asc(resumeQueue.queuedAt)) - const normalizedQueue = queueEntries.map((entry) => - PauseResumeManager.normalizeQueueEntry(entry) - ) - const queuePositions = PauseResumeManager.computeQueuePositions(normalizedQueue) - const latestEntries = PauseResumeManager.computeLatestEntriesByContext(normalizedQueue) - - const pausePoints = PauseResumeManager.mapPausePoints( + const queue = queueEntries.map((entry) => PauseResumeManager.normalizeQueueEntry(entry)) + const humanPoints = PauseResumeManager.mapPausePoints( row.pausePoints, - queuePositions, - latestEntries + PauseResumeManager.computeQueuePositions(queue), + PauseResumeManager.computeLatestEntriesByContext(queue) ).filter((point) => point.pauseKind !== 'time') + if (humanPoints.length === 0) return null - if (pausePoints.length === 0) { - return null - } - - const executionSummary = PauseResumeManager.normalizePausedExecution( - { ...row, triggerIds: (row.executionSnapshot as SerializedSnapshot)?.triggerIds }, - pausePoints - ) - - return { - ...executionSummary, - executionSnapshot: row.executionSnapshot as SerializedSnapshot, - queue: normalizedQueue, - } + const pausePoints = await namePausePoints(row.workflowId, row.executionId, humanPoints) + return { ...PauseResumeManager.normalizePausedExecution(row, pausePoints), queue } } static async getPauseContextDetail(options: { @@ -3608,18 +3626,13 @@ export class PauseResumeManager { contextId: string }): Promise { const { workflowId, executionId, contextId } = options - const detail = await PauseResumeManager.getPausedExecutionDetail({ workflowId, executionId }) - - if (!detail) { - return null - } - - const pausePoint = detail.pausePoints.find((point) => point.contextId === contextId) - if (!pausePoint) { + const view = await PauseResumeManager.getPausedExecutionView({ workflowId, executionId }) + const pausePoint = view?.pausePoints.find((point) => point.contextId === contextId) + if (!view || !pausePoint) { return null } - const activeResumeEntry = detail.queue.find( + const activeResumeEntry = view.queue.find( (entry) => entry.contextId === contextId && (entry.status === 'claimed' || entry.status === 'pending') ) @@ -3628,9 +3641,9 @@ export class PauseResumeManager { // are already returned via `pausePoint` below; strip them from // `execution.pausePoints` so a large HITL payload isn't duplicated in full // within the same response. - const execution: PausedExecutionDetail = { - ...detail, - pausePoints: detail.pausePoints.map(({ display, inputFields, ...point }) => + const execution: PausedExecutionSummary = { + ...view, + pausePoints: view.pausePoints.map(({ display, inputFields, ...point }) => point.response ? { ...point, response: { ...point.response, data: undefined } } : point ), } @@ -3638,7 +3651,7 @@ export class PauseResumeManager { return { execution, pausePoint, - queue: detail.queue, + queue: view.queue, activeResumeEntry, } } @@ -3837,6 +3850,7 @@ export class PauseResumeManager { return { contextId: point.contextId, blockId, + blockName: typeof point.blockName === 'string' ? point.blockName : undefined, response: point.response, registeredAt: point.registeredAt, resumeStatus: point.resumeStatus || 'paused', diff --git a/apps/sim/lib/workflows/executor/pause-block-names.ts b/apps/sim/lib/workflows/executor/pause-block-names.ts new file mode 100644 index 00000000000..ea9bf3b2734 --- /dev/null +++ b/apps/sim/lib/workflows/executor/pause-block-names.ts @@ -0,0 +1,73 @@ +import { db } from '@sim/db' +import { workflowDeploymentVersion, workflowExecutionLogs } from '@sim/db/schema' +import { and, eq, sql } from 'drizzle-orm' +import type { PausePoint } from '@/executor/types' +import { stripCloneSuffixes } from '@/executor/utils/subflow-node-id-codec' + +type NameablePausePoint = Pick + +/** The workflow block a pause point waits on, without any parallel branch or subflow clone suffix. */ +export function pauseBaseBlockId(point: Pick): string { + return stripCloneSuffixes(point.blockId ?? point.contextId) +} + +/** + * Names of `blockIds` in the deployment version the run executed, read inside the + * database so the version's state never leaves it. Empty for a run with no recorded + * version (a manual run) or whose version no longer exists. + */ +async function readRunBlockNames( + workflowId: string, + executionId: string, + blockIds: string[] +): Promise> { + const [row] = await db + .select({ + names: sql | null>`( + select jsonb_object_agg(b.key, b.value ->> 'name') + from json_each(${workflowDeploymentVersion.state} -> 'blocks') as b + where b.key in (${sql.join( + blockIds.map((id) => sql`${id}`), + sql`, ` + )}) + )`, + }) + .from(workflowExecutionLogs) + .innerJoin( + workflowDeploymentVersion, + eq(workflowDeploymentVersion.id, workflowExecutionLogs.deploymentVersionId) + ) + .where( + and( + eq(workflowExecutionLogs.executionId, executionId), + eq(workflowExecutionLogs.workflowId, workflowId) + ) + ) + .limit(1) + const names = new Map() + for (const [id, name] of Object.entries(row?.names ?? {})) { + if (typeof name === 'string' && name) names.set(id, name) + } + return names +} + +/** + * Gives every pause point its block's name. A point carries the name it was captured + * with; a point persisted before names were recorded is named from the run's + * deployment version, and stays unnamed for a manual run. The execution snapshot is + * never read, and the version is queried only when some point lacks a name. + */ +export async function namePausePoints( + workflowId: string, + executionId: string, + points: T[] +): Promise { + const unnamed = [...new Set(points.filter((point) => !point.blockName).map(pauseBaseBlockId))] + if (unnamed.length === 0) return points + + const names = await readRunBlockNames(workflowId, executionId, unnamed) + return points.map((point) => { + const blockName = point.blockName || names.get(pauseBaseBlockId(point)) + return blockName ? { ...point, blockName } : point + }) +} diff --git a/apps/sim/lib/workflows/executor/pause-persistence.integration.ts b/apps/sim/lib/workflows/executor/pause-persistence.integration.ts index 352589ee73b..f51968d2ba9 100644 --- a/apps/sim/lib/workflows/executor/pause-persistence.integration.ts +++ b/apps/sim/lib/workflows/executor/pause-persistence.integration.ts @@ -6,6 +6,7 @@ import { db } from '@sim/db' import { pausedExecutions, + permissions, user, workflow, workflowDeploymentVersion, @@ -24,6 +25,7 @@ import { } from '@/lib/billing/core/attribution' import { LoggingSession } from '@/lib/logs/execution/logging-session' import type { WorkflowState } from '@/lib/logs/types' +import { readPausedWorkflowExecution } from '@/lib/workflows/application/read-paused-workflow-execution' import { getWorkflowExecutionStatus, readPausedRunPoints, @@ -154,6 +156,13 @@ beforeAll(async () => { ownerId: ids.owner, billedAccountUserId: ids.owner, }) + await db.insert(permissions).values({ + id: generateId(), + entityType: 'workspace', + entityId: ids.workspace, + userId: ids.owner, + permissionType: 'admin', + }) await db.insert(workflow).values({ id: ids.workflow, userId: ids.owner, @@ -175,6 +184,7 @@ afterAll(async () => { await db .delete(workflowExecutionSnapshots) .where(eq(workflowExecutionSnapshots.workflowId, ids.workflow)) + await db.delete(permissions).where(eq(permissions.entityId, ids.workspace)) await deleteWorkspaceFixture(db, eq(workspace.id, ids.workspace)) await db.delete(user).where(eq(user.id, ids.owner)) }) @@ -496,3 +506,81 @@ describe('readPausedRunPoints', () => { }) }) }) + +describe('resume page reads', () => { + const read = (executionId: string, includeSnapshot = false) => + readPausedWorkflowExecution.execute({ + principal: { kind: 'session', userId: ids.owner, sessionId: generateId() }, + input: { workflowId: ids.workflow, executionId, includeSnapshot }, + }) + + it('returns the name each point was captured with, on a manual run, without the snapshot', async () => { + const registry = registryWithResolvedSecret() + const executionId = await publishPausedRun( + [humanBranchPoint(0, registry), humanBranchPoint(1, registry)].map((point) => ({ + ...point, + blockName: 'Approval', + })) + ) + + const view = await read(executionId) + const context = await PauseResumeManager.getPauseContextDetail({ + workflowId: ids.workflow, + executionId, + contextId: BRANCH_IDS[1], + }) + + expect(view.pausePoints.map((point) => point.blockName)).toEqual(['Approval', 'Approval']) + expect(context?.pausePoint.blockName).toBe('Approval') + expect( + (await readPausedRunPoints({ workflowId: ids.workflow, executionId }))?.map( + (point) => point.blockName + ) + ).toEqual(['Approval', 'Approval']) + expect(JSON.stringify(view)).not.toContain(SNAPSHOT_SENTINEL) + expect(JSON.stringify(context)).not.toContain(SNAPSHOT_SENTINEL) + expect(JSON.stringify(await read(executionId, true))).toContain(SNAPSHOT_SENTINEL) + }) + + it('names a point persisted before names were recorded from the run deployment version', async () => { + const executionId = await publishPausedRun([humanBranchPoint(0, registryWithResolvedSecret())]) + const versionId = generateId() + await db.insert(workflowDeploymentVersion).values({ + id: versionId, + workflowId: ids.workflow, + version: 3, + state: { blocks: { [CONTEXT_ID]: { id: CONTEXT_ID, name: 'Deployed approval' } } }, + isActive: false, + }) + await db + .update(workflowExecutionLogs) + .set({ deploymentVersionId: versionId }) + .where(eq(workflowExecutionLogs.executionId, executionId)) + + const [point] = (await read(executionId)).pausePoints + expect(point.blockName).toBe('Deployed approval') + }) + + it('reads trigger ids from the pause metadata, and from the snapshot only for an older row', async () => { + const executionId = await publishPausedRun([humanBranchPoint(0, registryWithResolvedSecret())]) + const [row] = await db + .select({ metadata: pausedExecutions.metadata, seed: pausedExecutions.executionSnapshot }) + .from(pausedExecutions) + .where(eq(pausedExecutions.executionId, executionId)) + const seed = row.seed as { snapshot: string; triggerIds: string[] } + const setTriggerIds = (metadataIds: string[] | undefined) => + db + .update(pausedExecutions) + .set({ + metadata: { ...(row.metadata as Record), triggerIds: metadataIds }, + executionSnapshot: { ...seed, triggerIds: ['from-snapshot'] }, + }) + .where(eq(pausedExecutions.executionId, executionId)) + + await setTriggerIds(['from-metadata']) + expect((await read(executionId)).triggerIds).toEqual(['from-metadata']) + + await setTriggerIds(undefined) + expect((await read(executionId)).triggerIds).toEqual(['from-snapshot']) + }) +}) diff --git a/packages/testing/src/mocks/human-in-the-loop-manager.mock.ts b/packages/testing/src/mocks/human-in-the-loop-manager.mock.ts index 673a03290f3..22c1514ee21 100644 --- a/packages/testing/src/mocks/human-in-the-loop-manager.mock.ts +++ b/packages/testing/src/mocks/human-in-the-loop-manager.mock.ts @@ -58,6 +58,7 @@ const pauseResumeManagerMockFns = { mockGetPausedExecutionById: vi.fn(), mockGetPausedExecutionDetail: vi.fn(), mockGetPauseContextDetail: vi.fn(), + mockGetPausedExecutionView: vi.fn(), mockProcessQueuedResumes: vi.fn(), } @@ -221,6 +222,7 @@ export const humanInTheLoopManagerMock = { getPausedExecutionById: pauseResumeManagerMockFns.mockGetPausedExecutionById, getPausedExecutionDetail: pauseResumeManagerMockFns.mockGetPausedExecutionDetail, getPauseContextDetail: pauseResumeManagerMockFns.mockGetPauseContextDetail, + getPausedExecutionView: pauseResumeManagerMockFns.mockGetPausedExecutionView, processQueuedResumes: pauseResumeManagerMockFns.mockProcessQueuedResumes, }, } diff --git a/scripts/check-explicit-any.baseline.json b/scripts/check-explicit-any.baseline.json index bd53833d48e..ad88ebef161 100644 --- a/scripts/check-explicit-any.baseline.json +++ b/scripts/check-explicit-any.baseline.json @@ -181,7 +181,7 @@ "apps/sim/executor/variables/resolvers/workflow.test.ts": 1, "apps/sim/executor/variables/resolvers/workflow.ts": 9, "apps/sim/hooks/queries/folders.test.ts": 5, - "apps/sim/hooks/queries/resume-execution.ts": 6, + "apps/sim/hooks/queries/resume-execution.ts": 5, "apps/sim/hooks/use-collaborative-workflow.ts": 23, "apps/sim/hooks/use-execution-stream.ts": 10, "apps/sim/hooks/use-trigger-config-aggregation.ts": 2,