diff --git a/apps/realtime/src/database/block-lock.integration.ts b/apps/realtime/src/database/block-lock.integration.ts new file mode 100644 index 00000000000..d39ae4a1fbb --- /dev/null +++ b/apps/realtime/src/database/block-lock.integration.ts @@ -0,0 +1,299 @@ +/** Real PostgreSQL coverage: realtime batch-add, subflow updates and replace-state never change what a block lock protects. */ +import { mkdir, writeFile } from 'node:fs/promises' +import { dirname, resolve } from 'node:path' +import { db } from '@sim/db' +import { + user, + workflow, + workflowBlocks, + workflowEdges, + workflowSubflows, + workspace, +} from '@sim/db/schema' +import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' +import { + BLOCKS_OPERATIONS, + OPERATION_TARGETS, + SUBFLOW_OPERATIONS, + WORKFLOW_OPERATIONS, +} from '@sim/realtime-protocol/constants' +import { getErrorMessage } from '@sim/utils/errors' +import { generateId } from '@sim/utils/id' +import { eq } from 'drizzle-orm' +import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest' +import { persistWorkflowOperation } from '@/database/operations' + +const userId = generateId() +const workspaceId = generateId() +const workflowId = generateId() +const ids = { + locked: generateId(), + container: generateId(), + child: generateId(), + free: generateId(), + edgeIntoLocked: generateId(), + unlockedParent: generateId(), + lockedChild: generateId(), +} +const lockedLoopConfig = { id: ids.container, nodes: [ids.child], iterations: 3, loopType: 'for' } +const checks: { name: string; status: 'passed' | 'failed'; durationMs: number; error?: string }[] = + [] + +/** Registers a test and records its status and duration in the suite report. */ +function check(name: string, run: () => Promise) { + it(name, async () => { + const started = performance.now() + try { + await run() + checks.push({ name, status: 'passed', durationMs: performance.now() - started }) + } catch (error) { + checks.push({ + name, + status: 'failed', + durationMs: performance.now() - started, + error: getErrorMessage(error), + }) + throw error + } + }) +} + +function blockRow( + id: string, + name: string, + extra: Partial = {} +) { + return { + id, + workflowId, + type: 'function', + name, + positionX: '0', + positionY: '0', + ...extra, + } +} + +function payloadBlock(id: string, name: string, extra: Record = {}) { + return { id, type: 'function', name, position: { x: 10, y: 10 }, subBlocks: {}, ...extra } +} + +function persist(target: string, operation: string, payload: unknown) { + return persistWorkflowOperation(workflowId, { + target, + operation, + payload, + timestamp: Date.now(), + userId, + }) +} + +async function readBlocks() { + const rows = await db + .select() + .from(workflowBlocks) + .where(eq(workflowBlocks.workflowId, workflowId)) + return Object.fromEntries(rows.map((row) => [row.id, row])) +} + +async function readSubflows() { + return db + .select({ id: workflowSubflows.id, config: workflowSubflows.config }) + .from(workflowSubflows) + .where(eq(workflowSubflows.workflowId, workflowId)) +} + +describe('block locks against PostgreSQL', () => { + beforeAll(async () => { + const now = new Date() + await db.insert(user).values({ + id: userId, + name: 'Lock fixture', + email: `${userId}@block-lock.test`, + emailVerified: true, + createdAt: now, + updatedAt: now, + }) + await insertWorkspaceFixture(db, { + id: workspaceId, + name: 'Lock target', + ownerId: userId, + billedAccountUserId: userId, + }) + await db.insert(workflow).values({ + id: workflowId, + userId, + workspaceId, + name: 'Locked blocks', + lastSynced: now, + createdAt: now, + updatedAt: now, + }) + }) + + beforeEach(async () => { + await db.delete(workflowSubflows).where(eq(workflowSubflows.workflowId, workflowId)) + await db.delete(workflowBlocks).where(eq(workflowBlocks.workflowId, workflowId)) + await db.insert(workflowBlocks).values([ + blockRow(ids.locked, 'Locked', { locked: true }), + blockRow(ids.container, 'Locked loop', { type: 'loop', locked: true }), + blockRow(ids.child, 'Child', { data: { parentId: ids.container, extent: 'parent' } }), + blockRow(ids.free, 'Free'), + blockRow(ids.unlockedParent, 'Unlocked loop', { type: 'loop' }), + blockRow(ids.lockedChild, 'Locked child', { + locked: true, + data: { parentId: ids.unlockedParent, extent: 'parent' }, + }), + ]) + await db.insert(workflowEdges).values({ + id: ids.edgeIntoLocked, + workflowId, + sourceBlockId: ids.free, + targetBlockId: ids.locked, + }) + await db.insert(workflowSubflows).values({ + id: ids.container, + workflowId, + type: 'loop', + config: lockedLoopConfig, + }) + }) + + afterAll(async () => { + const reportPath = + process.env.REALTIME_BLOCK_LOCK_REPORT_PATH ?? + resolve('test-results/realtime-block-lock.json') + await mkdir(dirname(reportPath), { recursive: true }) + await writeFile(reportPath, JSON.stringify({ checks }, null, 2)) + await deleteWorkspaceFixture(db, eq(workspace.ownerId, userId)) + await db.delete(user).where(eq(user.id, userId)) + }) + + check('batch-add-blocks cannot unlock or overwrite a locked block', async () => { + const result = await persist(OPERATION_TARGETS.BLOCKS, BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS, { + blocks: [ + payloadBlock(ids.locked, 'Hijacked', { locked: false }), + payloadBlock(ids.child, 'Hijacked child'), + ], + edges: [], + loops: {}, + parallels: {}, + }) + + const blocks = await readBlocks() + expect(blocks[ids.locked]).toMatchObject({ locked: true, name: 'Locked' }) + expect(blocks[ids.child]).toMatchObject({ name: 'Child' }) + expect(result).toEqual({ reconcile: true }) + }) + + check('batch-add-blocks and subflow updates cannot rewrite a locked loop config', async () => { + const result = await persist(OPERATION_TARGETS.BLOCKS, BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS, { + blocks: [payloadBlock(generateId(), 'Throwaway')], + edges: [], + loops: { [ids.container]: { ...lockedLoopConfig, iterations: 99 } }, + parallels: {}, + }) + const updated = await persist(OPERATION_TARGETS.SUBFLOW, SUBFLOW_OPERATIONS.UPDATE, { + id: ids.container, + type: 'loop', + config: { iterations: 42 }, + }) + + expect(await readSubflows()).toEqual([{ id: ids.container, config: lockedLoopConfig }]) + expect(result).toEqual({ reconcile: true }) + expect(updated).toEqual({ reconcile: true }) + }) + + check('an add that touches no lock keeps collaborators on the payload', async () => { + const result = await persist(OPERATION_TARGETS.BLOCKS, BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS, { + blocks: [payloadBlock(generateId(), 'Plain')], + edges: [], + loops: {}, + parallels: {}, + }) + + expect(result).toEqual({ reconcile: false }) + }) + + check('an add whose only rejected part is an edge into a locked block re-syncs', async () => { + const result = await persist(OPERATION_TARGETS.BLOCKS, BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS, { + blocks: [], + edges: [{ id: generateId(), source: ids.free, target: ids.locked, sourceHandle: 'source' }], + loops: {}, + parallels: {}, + }) + + expect(result).toEqual({ reconcile: true }) + }) + + check( + 'batch-add-blocks never takes the lock flag from the client nor nests under a locked ancestor', + async () => { + const lockedNew = generateId() + const nestedNew = generateId() + const nestedChild = generateId() + await persist(OPERATION_TARGETS.BLOCKS, BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS, { + blocks: [ + payloadBlock(lockedNew, 'Self-locked', { locked: true }), + payloadBlock(nestedNew, 'Nested loop', { type: 'loop', data: { parentId: ids.child } }), + payloadBlock(nestedChild, 'Nested child', { data: { parentId: nestedNew } }), + ], + edges: [], + loops: { [nestedNew]: { id: nestedNew, nodes: [nestedChild], iterations: 2 } }, + parallels: {}, + }) + + const blocks = await readBlocks() + expect(blocks[lockedNew]).toMatchObject({ locked: false }) + expect(blocks[nestedNew]).toBeUndefined() + expect(blocks[nestedChild]).toBeUndefined() + expect(await readSubflows()).toEqual([{ id: ids.container, config: lockedLoopConfig }]) + } + ) + + check( + 'replace-state keeps protected blocks, their incoming edges and loop config as stored, and drops what a lock or a removed parent no longer allows', + async () => { + const added = generateId() + const nestedNew = generateId() + const result = await persist(OPERATION_TARGETS.WORKFLOW, WORKFLOW_OPERATIONS.REPLACE_STATE, { + state: { + blocks: { + [ids.locked]: payloadBlock(ids.locked, 'Hijacked', { locked: false }), + [ids.free]: payloadBlock(ids.free, 'Free renamed', { locked: true }), + [added]: payloadBlock(added, 'Added'), + [nestedNew]: payloadBlock(nestedNew, 'Nested', { + data: { parentId: ids.container, extent: 'parent' }, + }), + }, + edges: [ + { id: generateId(), source: ids.free, target: nestedNew }, + // Retargets the protected edge under its own id. + { id: ids.edgeIntoLocked, source: ids.free, target: added }, + ], + loops: { [ids.container]: { ...lockedLoopConfig, iterations: 99 } }, + parallels: {}, + }, + }) + + const blocks = await readBlocks() + expect(blocks[ids.locked]).toMatchObject({ locked: true, name: 'Locked' }) + expect(blocks[ids.container]).toMatchObject({ locked: true, name: 'Locked loop' }) + expect(blocks[ids.child]).toMatchObject({ name: 'Child' }) + expect(blocks[ids.free]).toMatchObject({ locked: false, name: 'Free renamed' }) + expect(blocks[added]).toMatchObject({ locked: false }) + expect(blocks[nestedNew]).toBeUndefined() + // The replacement removed its unlocked parent, so the locked child goes with it. + expect(blocks[ids.lockedChild]).toBeUndefined() + + const edges = await db + .select({ id: workflowEdges.id, target: workflowEdges.targetBlockId }) + .from(workflowEdges) + .where(eq(workflowEdges.workflowId, workflowId)) + expect(edges).toEqual([{ id: ids.edgeIntoLocked, target: ids.locked }]) + + expect(await readSubflows()).toEqual([{ id: ids.container, config: lockedLoopConfig }]) + expect(result).toEqual({ reconcile: true }) + } + ) +}) diff --git a/apps/realtime/src/database/operations.test.ts b/apps/realtime/src/database/operations.test.ts index 4d295ed3931..2ee3c38926d 100644 --- a/apps/realtime/src/database/operations.test.ts +++ b/apps/realtime/src/database/operations.test.ts @@ -84,7 +84,7 @@ describe('search replacement persistence', () => { }, ] - await expect(replaceTools(stored)).resolves.toBeUndefined() + await expect(replaceTools(stored)).resolves.toEqual({ reconcile: false }) expect(mockSet).toHaveBeenLastCalledWith( expect.objectContaining({ subBlocks: { tools: { id: 'tools', type: 'tool-input', value: replacement } }, @@ -138,7 +138,7 @@ describe('subblock update with canonical modes persistence', () => { } it('writes the subblock value and replaces canonical modes in one block update', async () => { - await expect(updateTools({})).resolves.toBeUndefined() + await expect(updateTools({})).resolves.toEqual({ reconcile: false }) expect(mockSet).toHaveBeenCalledTimes(2) expect(mockSet).toHaveBeenLastCalledWith( diff --git a/apps/realtime/src/database/operations.ts b/apps/realtime/src/database/operations.ts index 7dff7dffe72..5c94e3c6676 100644 --- a/apps/realtime/src/database/operations.ts +++ b/apps/realtime/src/database/operations.ts @@ -34,9 +34,11 @@ import { getWorkflowEdgeScopeDropReason, isKnownWorkflowTriggerBlock, isWorkflowAnnotationOnlyBlockType, + isWorkflowBlockAncestorLocked, isWorkflowBlockProtected, normalizeWorkflowEdgeSourceHandle, normalizeWorkflowEdgeTargetHandle, + preserveProtectedWorkflowBlocks, } from '@sim/workflow-types/workflow' import { and, eq, inArray, isNull, or, type SQL, sql } from 'drizzle-orm' import type { PgInsertValue } from 'drizzle-orm/pg-core' @@ -297,6 +299,15 @@ interface DbBlockRef { data: unknown } +/** Every block of the workflow with the fields the lock checks read, keyed by id. */ +async function loadLockBlocks(tx: DbOrTx, workflowId: string): Promise> { + const rows = await tx + .select({ id: workflowBlocks.id, locked: workflowBlocks.locked, data: workflowBlocks.data }) + .from(workflowBlocks) + .where(eq(workflowBlocks.workflowId, workflowId)) + return Object.fromEntries(rows.map((row) => [row.id, row])) +} + /** * Finds all descendant block IDs of a container (recursive). * Works with raw DB block arrays. @@ -412,8 +423,21 @@ export async function getWorkflowState(workflowId: string) { } } -export async function persistWorkflowOperation(workflowId: string, operation: any) { +/** What a persisted operation means for the clients that applied its payload optimistically. */ +export interface PersistedWorkflowOperation { + /** + * True when the stored graph kept lock-protected state the payload tried to change, so the + * payload no longer describes what was saved and every client must re-sync from the server. + */ + reconcile: boolean +} + +export async function persistWorkflowOperation( + workflowId: string, + operation: any +): Promise { const startTime = Date.now() + let reconcile = false try { const { operation: op, target, payload, timestamp, userId } = operation @@ -450,7 +474,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an await handleBlockOperationTx(tx, workflowId, op, payload) break case OPERATION_TARGETS.BLOCKS: - await handleBlocksOperationTx(tx, workflowId, op, payload) + reconcile = (await handleBlocksOperationTx(tx, workflowId, op, payload)) === true break case OPERATION_TARGETS.EDGE: await handleEdgeOperationTx(tx, workflowId, op, payload) @@ -459,7 +483,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an await handleEdgesOperationTx(tx, workflowId, op, payload) break case OPERATION_TARGETS.SUBFLOW: - await handleSubflowOperationTx(tx, workflowId, op, payload) + reconcile = await handleSubflowOperationTx(tx, workflowId, op, payload) break case OPERATION_TARGETS.SUBBLOCK: await handleSubblockOperationTx(tx, workflowId, op, payload) @@ -468,7 +492,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an await handleVariableOperationTx(tx, workflowId, op, payload) break case OPERATION_TARGETS.WORKFLOW: - await handleWorkflowOperationTx(tx, workflowId, op, payload) + reconcile = await handleWorkflowOperationTx(tx, workflowId, op, payload) break default: throw new Error(`Unknown operation target: ${target}`) @@ -494,6 +518,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an workflowId: `${workflowId.substring(0, 8)}...`, }) } + return { reconcile } } catch (error) { const duration = Date.now() - startTime logger.error( @@ -948,33 +973,38 @@ async function handleBlocksOperationTx( parallelCount: Object.keys(parallels || {}).length, }) - if (blocks && blocks.length > 0) { - const existingBlocks = await tx - .select({ id: workflowBlocks.id, locked: workflowBlocks.locked }) - .from(workflowBlocks) - .where(eq(workflowBlocks.workflowId, workflowId)) + const lockBlocks = await loadLockBlocks(tx, workflowId) + const isProtected = (id: string) => isWorkflowBlockProtected(id, lockBlocks) - type ExistingBlockRecord = (typeof existingBlocks)[number] - const lockedParentIds = new Set( - existingBlocks - .filter((b: ExistingBlockRecord) => b.locked) - .map((b: ExistingBlockRecord) => b.id) - ) + // Lock state is admin-only, so an add never writes the lock flag, overwrites a protected + // block or its loop/parallel config, nor places a block inside a locked container. + let reconcile = false + const rejectedBlockIds = new Set() - const allowedBlocks = (blocks as Array>).filter((block) => { - const parentId = (block.data as Record | null)?.parentId as - | string - | undefined - if (parentId && lockedParentIds.has(parentId)) { - logger.info(`Skipping block ${block.id} - parent ${parentId} is locked`) - return false - } - return true - }) + if (blocks && blocks.length > 0) { + const incoming = blocks as Array> + // Added blocks land unlocked, so one is refused exactly when it is protected now or + // would sit under a locked ancestor once the whole batch lands. + const landed: Record = { ...lockBlocks } + for (const block of incoming) { + const id = block.id as string + if (!isProtected(id)) landed[id] = { id, locked: false, data: block.data } + } + for (const block of incoming) { + const id = block.id as string + if (isProtected(id) || isWorkflowBlockAncestorLocked(id, landed)) rejectedBlockIds.add(id) + } + const allowedBlocks = incoming.filter((block) => !rejectedBlockIds.has(block.id as string)) + if (rejectedBlockIds.size > 0) { + logger.info('Skipping blocks that are locked or inside a locked container', { + count: rejectedBlockIds.size, + }) + } + reconcile = rejectedBlockIds.size > 0 || incoming.some((block) => block.locked) if (allowedBlocks.length === 0) { - logger.info('All blocks filtered out due to locked parents, skipping add') - break + logger.info('All blocks filtered out due to locks, skipping add') + return reconcile } const blockValues = allowedBlocks.map((block: Record) => { @@ -1001,7 +1031,6 @@ async function handleBlocksOperationTx( errorEnabled: (block.errorEnabled as boolean) ?? false, retry: (block.retry as Record | undefined) ?? null, height: String((block.height as number) || 0), - locked: (block.locked as boolean) ?? false, } }) @@ -1016,7 +1045,6 @@ async function handleBlocksOperationTx( triggerMode: sql`excluded.trigger_mode`, errorEnabled: sql`excluded.error_enabled`, retry: sql`excluded.retry`, - locked: sql`excluded.locked`, height: sql`excluded.height`, subBlocks: sql`excluded.sub_blocks`, outputs: sql`excluded.outputs`, @@ -1098,6 +1126,7 @@ async function handleBlocksOperationTx( droppedCyclic, }) } + if (droppedCounts['protected target block']) reconcile = true if (safeEdges.length > 0) { const edgeValues = safeEdges.map((edge) => ({ @@ -1113,36 +1142,37 @@ async function handleBlocksOperationTx( } } - if (loops && Object.keys(loops).length > 0) { - const loopValues = Object.entries(loops).map(([id, loop]) => ({ + const subflowEntries = [ + ...Object.entries(loops || {}).map(([id, config]) => ({ id, type: 'loop', config })), + ...Object.entries(parallels || {}).map(([id, config]) => ({ id, + type: 'parallel', + config, + })), + ] + const subflowValues = subflowEntries + .filter((subflow) => !isProtected(subflow.id) && !rejectedBlockIds.has(subflow.id)) + .map((subflow) => ({ + ...subflow, workflowId, - type: 'loop', - config: loop as Record, + config: subflow.config as Record, })) - - await upsertWorkflowRows(tx, workflowSubflows, workflowId, loopValues, SUBFLOW_UPSERT_SET) + if (subflowValues.length < subflowEntries.length) { + logger.info('Skipping loop/parallel config of locked containers') + reconcile = true } - - if (parallels && Object.keys(parallels).length > 0) { - const parallelValues = Object.entries(parallels).map(([id, parallel]) => ({ - id, - workflowId, - type: 'parallel', - config: parallel as Record, - })) - + if (subflowValues.length > 0) { await upsertWorkflowRows( tx, workflowSubflows, workflowId, - parallelValues, + subflowValues, SUBFLOW_UPSERT_SET ) } logger.info(`Successfully batch added blocks to workflow ${workflowId}`) - break + return reconcile } case BLOCKS_OPERATIONS.BATCH_REMOVE_BLOCKS: { @@ -1807,13 +1837,20 @@ async function handleSubflowOperationTx( workflowId: string, operation: string, payload: any -) { +): Promise { switch (operation) { case SUBFLOW_OPERATIONS.UPDATE: { if (!payload.id || !payload.config) { throw new Error('Missing required fields for update subflow operation') } + if (isWorkflowBlockProtected(payload.id, await loadLockBlocks(tx, workflowId))) { + logger.info( + `Skipping update of subflow ${payload.id} - it is locked or in a locked container` + ) + return true + } + logger.debug(`Updating subflow ${payload.id} with config:`, payload.config) // Read-modify-write merge so partial config payloads never wipe other fields @@ -1925,7 +1962,7 @@ async function handleSubflowOperationTx( .where(and(eq(workflowBlocks.id, payload.id), eq(workflowBlocks.workflowId, workflowId))) } - break + return false } default: @@ -2126,13 +2163,13 @@ async function handleVariableOperationTx( } } -/** Replaces the complete workflow state. */ +/** Replaces the complete workflow state; resolves whether clients must re-sync from the server. */ async function handleWorkflowOperationTx( tx: any, workflowId: string, operation: string, payload: any -) { +): Promise { switch (operation) { case WORKFLOW_OPERATIONS.REPLACE_STATE: { if (!payload.state) { @@ -2148,44 +2185,67 @@ async function handleWorkflowOperationTx( parallelCount: Object.keys(parallels || {}).length, }) + type BlockRow = typeof workflowBlocks.$inferInsert + const [storedBlocks, storedEdges, storedSubflows]: [ + BlockRow[], + (typeof workflowEdges.$inferSelect)[], + (typeof workflowSubflows.$inferSelect)[], + ] = await Promise.all([ + tx.select().from(workflowBlocks).where(eq(workflowBlocks.workflowId, workflowId)), + tx.select().from(workflowEdges).where(eq(workflowEdges.workflowId, workflowId)), + tx.select().from(workflowSubflows).where(eq(workflowSubflows.workflowId, workflowId)), + ]) + + const incomingBlocks: BlockRow[] = Object.values(blocks || {}).map((block: any) => ({ + id: block.id, + workflowId, + type: block.type, + name: block.name, + positionX: String(block.position.x), + positionY: String(block.position.y), + errorEnabled: block.errorEnabled ?? false, + retry: block.retry ?? null, + data: block.data || {}, + subBlocks: block.subBlocks || {}, + outputs: block.outputs || {}, + enabled: block.enabled ?? true, + horizontalHandles: block.horizontalHandles ?? true, + advancedMode: block.advancedMode ?? false, + triggerMode: block.triggerMode ?? false, + height: String(block.height || 0), + })) + const incomingEdges: EdgeAddCandidate[] = ( + (edges || []) as Array> + ).map((edge) => ({ + id: edge.id as string, + source: edge.source as string, + target: edge.target as string, + sourceHandle: (edge.sourceHandle as string | null) ?? null, + targetHandle: (edge.targetHandle as string | null) ?? null, + })) + + const merged = preserveProtectedWorkflowBlocks( + { + blocks: Object.fromEntries(storedBlocks.map((row) => [row.id, row])), + edges: storedEdges.map((row) => ({ id: row.id, ...toEdgeHandles(row) })), + }, + { + blocks: Object.fromEntries(incomingBlocks.map((row) => [row.id, row])), + edges: incomingEdges, + } + ) + await tx.delete(workflowBlocks).where(eq(workflowBlocks.workflowId, workflowId)) await tx.delete(workflowSubflows).where(eq(workflowSubflows.workflowId, workflowId)) - if (blocks && Object.keys(blocks).length > 0) { - const blockValues = Object.values(blocks).map((block: any) => ({ - id: block.id, - workflowId, - type: block.type, - name: block.name, - positionX: block.position.x, - positionY: block.position.y, - errorEnabled: block.errorEnabled ?? false, - retry: block.retry ?? null, - data: block.data || {}, - subBlocks: block.subBlocks || {}, - outputs: block.outputs || {}, - enabled: block.enabled ?? true, - horizontalHandles: block.horizontalHandles ?? true, - advancedMode: block.advancedMode ?? false, - triggerMode: block.triggerMode ?? false, - height: block.height || 0, - locked: block.locked ?? false, - })) - + const blockValues = Object.values(merged.blocks) + if (blockValues.length > 0) { await tx.insert(workflowBlocks).values(blockValues) } - if (edges && edges.length > 0) { - const canonicalEdges = (edges as Array>).map((edge) => - canonicalizeEdgeAddCandidate({ - id: edge.id as string, - source: edge.source as string, - target: edge.target as string, - sourceHandle: (edge.sourceHandle as string | null) ?? null, - targetHandle: (edge.targetHandle as string | null) ?? null, - }) - ) + if (merged.edges.length > 0) { + const canonicalEdges = merged.edges.map(canonicalizeEdgeAddCandidate) const uniqueEdges = filterUniqueWorkflowEdges(canonicalEdges, []) const edgeValues = uniqueEdges.map((edge) => ({ id: edge.id, @@ -2196,8 +2256,8 @@ async function handleWorkflowOperationTx( targetHandle: edge.targetHandle ?? null, })) - if (uniqueEdges.length < edges.length) { - logger.info(`Dropped ${edges.length - uniqueEdges.length} duplicate edge(s)`, { + if (uniqueEdges.length < merged.edges.length) { + logger.info(`Dropped ${merged.edges.length - uniqueEdges.length} duplicate edge(s)`, { operation: WORKFLOW_OPERATIONS.REPLACE_STATE, }) } @@ -2207,30 +2267,41 @@ async function handleWorkflowOperationTx( } } - if (loops && Object.keys(loops).length > 0) { - const loopValues = Object.entries(loops).map(([id, loop]: [string, any]) => ({ + const incomingSubflows = [ + ...Object.entries(loops || {}).map(([id, config]) => ({ id, workflowId, type: 'loop', - config: loop, - })) - - await tx.insert(workflowSubflows).values(loopValues) - } - - if (parallels && Object.keys(parallels).length > 0) { - const parallelValues = Object.entries(parallels).map(([id, parallel]: [string, any]) => ({ + config, + })), + ...Object.entries(parallels || {}).map(([id, config]) => ({ id, workflowId, type: 'parallel', - config: parallel, - })) - - await tx.insert(workflowSubflows).values(parallelValues) + config, + })), + ] + const subflowValues = Object.values({ + ...Object.fromEntries( + incomingSubflows.filter(({ id }) => merged.blocks[id]).map((row) => [row.id, row]) + ), + ...Object.fromEntries( + storedSubflows.filter(({ id }) => merged.keptBlockIds.has(id)).map((row) => [row.id, row]) + ), + }) + if (subflowValues.length > 0) { + await tx.insert(workflowSubflows).values(subflowValues) } logger.info(`Successfully replaced workflow state for ${workflowId}`) - break + // A graph holding locked blocks may have been kept over the payload; comparing the + // stored rows against it field by field is not worth the precision, so re-sync. + return ( + storedBlocks.some((row) => row.locked) || + Object.values((blocks || {}) as Record).some( + (block) => block.locked + ) + ) } default: diff --git a/apps/realtime/src/handlers/operations.test.ts b/apps/realtime/src/handlers/operations.test.ts index afed46f0636..abd24a37206 100644 --- a/apps/realtime/src/handlers/operations.test.ts +++ b/apps/realtime/src/handlers/operations.test.ts @@ -93,7 +93,7 @@ function setup(id: string, role: string) { describe('workflow operation ACL', () => { beforeEach(() => { mockAssertMutable.mockResolvedValue(undefined) - mockPersist.mockResolvedValue(undefined) + mockPersist.mockResolvedValue({ reconcile: false }) }) describe('read-only member', () => { diff --git a/apps/realtime/src/handlers/operations.ts b/apps/realtime/src/handlers/operations.ts index d3d603887db..26d92c3126f 100644 --- a/apps/realtime/src/handlers/operations.ts +++ b/apps/realtime/src/handlers/operations.ts @@ -9,6 +9,7 @@ import { type VariableOperation, WORKFLOW_OPERATIONS, } from '@sim/realtime-protocol/constants' +import type { WorkflowUpdatedBroadcast } from '@sim/realtime-protocol/events' import { ROOM_TYPES } from '@sim/realtime-protocol/rooms' import { WorkflowOperationSchema } from '@sim/realtime-protocol/schemas' import { getErrorMessage } from '@sim/utils/errors' @@ -21,6 +22,29 @@ import { type IRoomManager, type UserSession, workflowRoom as wf } from '@/rooms const logger = createLogger('OperationsHandlers') +/** + * Relays a persisted operation to the room. When persistence kept lock-protected state over + * the payload, every client, the sender included, is told to reload the stored graph instead + * of applying a payload that no longer describes what was saved. + */ +function relayPersistedOperation( + socket: AuthenticatedSocket, + roomManager: IRoomManager, + workflowId: string, + reconcile: boolean, + broadcast: object +) { + if (reconcile) { + roomManager.emitToRoom(wf(workflowId), 'workflow-updated', { + workflowId, + message: 'Locked blocks were kept as stored', + timestamp: Date.now(), + }) + return + } + socket.to(workflowId).emit('workflow-operation', broadcast) +} + export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager: IRoomManager) { socket.on('workflow-operation', async (data) => { const emitOperationError = ( @@ -314,7 +338,7 @@ export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager target === OPERATION_TARGETS.WORKFLOW && operation === WORKFLOW_OPERATIONS.REPLACE_STATE ) { - await persistWorkflowOperation(workflowId, { + const { reconcile } = await persistWorkflowOperation(workflowId, { operation, target, payload, @@ -324,7 +348,7 @@ export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager await roomManager.updateRoomLastModified(wf(workflowId)) - const broadcastData = { + relayPersistedOperation(socket, roomManager, workflowId, reconcile, { operation, target, payload, @@ -332,13 +356,8 @@ export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager senderId: socket.id, userId: session.userId, userName: session.userName, - metadata: { - workflowId, - operationId: generateId(), - }, - } - - socket.to(workflowId).emit('workflow-operation', broadcastData) + metadata: { workflowId, operationId: generateId() }, + }) if (operationId) { socket.emit('operation-confirmed', { @@ -351,7 +370,7 @@ export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager } if (target === OPERATION_TARGETS.BLOCKS && operation === BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS) { - await persistWorkflowOperation(workflowId, { + const { reconcile } = await persistWorkflowOperation(workflowId, { operation, target, payload, @@ -361,7 +380,7 @@ export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager await roomManager.updateRoomLastModified(wf(workflowId)) - socket.to(workflowId).emit('workflow-operation', { + relayPersistedOperation(socket, roomManager, workflowId, reconcile, { operation, target, payload, @@ -566,7 +585,7 @@ export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager } // For non-position operations, persist first then broadcast - await persistWorkflowOperation(workflowId, { + const { reconcile } = await persistWorkflowOperation(workflowId, { operation, target, payload, @@ -576,7 +595,7 @@ export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager await roomManager.updateRoomLastModified(wf(workflowId)) - const broadcastData = { + relayPersistedOperation(socket, roomManager, workflowId, reconcile, { operation, target, payload, @@ -584,13 +603,8 @@ export function setupOperationsHandlers(socket: AuthenticatedSocket, roomManager senderId: socket.id, userId: session.userId, userName: session.userName, - metadata: { - workflowId, - operationId: generateId(), - }, - } - - socket.to(workflowId).emit('workflow-operation', broadcastData) + metadata: { workflowId, operationId: generateId() }, + }) if (operationId) { socket.emit('operation-confirmed', { diff --git a/apps/sim/lib/workflows/persistence/block-lock.integration.ts b/apps/sim/lib/workflows/persistence/block-lock.integration.ts new file mode 100644 index 00000000000..9b78aa526e5 --- /dev/null +++ b/apps/sim/lib/workflows/persistence/block-lock.integration.ts @@ -0,0 +1,142 @@ +/** Real PostgreSQL coverage: a write-role full-state replace never changes what a block lock protects. */ +import { mkdir, writeFile } from 'node:fs/promises' +import { dirname, resolve } from 'node:path' +import { db } from '@sim/db' +import { user, workflowBlocks, workflowEdges, workspace } from '@sim/db/schema' +import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' +import { getErrorMessage } from '@sim/utils/errors' +import { generateId } from '@sim/utils/id' +import type { BlockState, WorkflowState } from '@sim/workflow-types/workflow' +import { eq } from 'drizzle-orm' +import { afterAll, beforeAll, describe, expect, it } from 'vitest' +import { createWorkflowWithState } from '@/lib/workflows/orchestration/workflow-lifecycle' +import { replaceWorkflowNormalizedState } from '@/lib/workflows/persistence/replace-normalized-state' + +const userId = generateId() +const workspaceId = generateId() +const checks: { name: string; status: 'passed' | 'failed'; durationMs: number; error?: string }[] = + [] + +/** Registers a test and records its status and duration in the suite report. */ +function check(name: string, run: () => Promise) { + it(name, async () => { + const started = performance.now() + try { + await run() + checks.push({ name, status: 'passed', durationMs: performance.now() - started }) + } catch (error) { + checks.push({ + name, + status: 'failed', + durationMs: performance.now() - started, + error: getErrorMessage(error), + }) + throw error + } + }) +} + +function block(id: string, name: string, extra: Partial = {}): BlockState { + return { + id, + type: 'function', + name, + position: { x: 0, y: 0 }, + subBlocks: {}, + outputs: {}, + enabled: true, + ...extra, + } +} + +describe('replaceWorkflowNormalizedState block locks against PostgreSQL', () => { + beforeAll(async () => { + const now = new Date() + await db.insert(user).values({ + id: userId, + name: 'Lock fixture', + email: `${userId}@block-lock.test`, + emailVerified: true, + createdAt: now, + updatedAt: now, + }) + await insertWorkspaceFixture(db, { + id: workspaceId, + name: 'Lock target', + ownerId: userId, + billedAccountUserId: userId, + }) + }) + + afterAll(async () => { + const reportPath = + process.env.WORKFLOW_BLOCK_LOCK_REPORT_PATH ?? + resolve('test-results/workflow-block-lock.json') + await mkdir(dirname(reportPath), { recursive: true }) + await writeFile(reportPath, JSON.stringify({ checks }, null, 2)) + await deleteWorkspaceFixture(db, eq(workspace.ownerId, userId)) + await db.delete(user).where(eq(user.id, userId)) + }) + + check( + 'keeps locked blocks and their incoming edges as stored, and writes the rest unlocked', + async () => { + const workflowId = generateId() + const lockedId = generateId() + const freeId = generateId() + const addedId = generateId() + const edgeId = generateId() + const seeded: WorkflowState = { + blocks: { + [lockedId]: block(lockedId, 'Locked', { locked: true }), + [freeId]: block(freeId, 'Free'), + }, + edges: [{ id: edgeId, source: freeId, target: lockedId }], + loops: {}, + parallels: {}, + } + const created = await createWorkflowWithState({ + id: workflowId, + userId, + workspaceId, + folderId: null, + name: 'Locked blocks', + state: seeded, + governance: { workspaceId: null, subjectUserId: null }, + }) + expect(created).toMatchObject({ success: true }) + + const { state } = await replaceWorkflowNormalizedState({ + workflowId, + workspaceId: null, + attributedUserId: userId, + subjectUserId: null, + state: { + blocks: { + // A record key that differs from the block's id must not slip past the lock. + decoy: block(lockedId, 'Hijacked', { locked: false }), + [freeId]: block(freeId, 'Free renamed', { locked: true }), + [addedId]: block(addedId, 'Added', { locked: true }), + }, + edges: [], + }, + }) + + const rows = await db + .select() + .from(workflowBlocks) + .where(eq(workflowBlocks.workflowId, workflowId)) + const byId = Object.fromEntries(rows.map((row) => [row.id, row])) + expect(byId[lockedId]).toMatchObject({ locked: true, name: 'Locked' }) + expect(byId[freeId]).toMatchObject({ locked: false, name: 'Free renamed' }) + expect(byId[addedId]).toMatchObject({ locked: false }) + expect(state.blocks[lockedId]).toMatchObject({ locked: true, name: 'Locked' }) + + const edges = await db + .select({ id: workflowEdges.id }) + .from(workflowEdges) + .where(eq(workflowEdges.workflowId, workflowId)) + expect(edges).toEqual([{ id: edgeId }]) + } + ) +}) diff --git a/apps/sim/lib/workflows/persistence/replace-normalized-state.test.ts b/apps/sim/lib/workflows/persistence/replace-normalized-state.test.ts index fc5d853f935..517f5cbd634 100644 --- a/apps/sim/lib/workflows/persistence/replace-normalized-state.test.ts +++ b/apps/sim/lib/workflows/persistence/replace-normalized-state.test.ts @@ -10,6 +10,11 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' const mocks = vi.hoisted(() => ({ prepare: vi.fn(), extractCustomTools: vi.fn(), + loadStoredGraph: vi.fn(), +})) + +vi.mock('@sim/workflow-persistence/load', () => ({ + loadWorkflowFromNormalizedTablesRaw: mocks.loadStoredGraph, })) vi.mock('@/lib/workflows/persistence/prepare-state', () => ({ @@ -85,6 +90,7 @@ describe('replaceWorkflowNormalizedState', () => { mocks.prepare.mockReturnValue({ state: PREPARED, warnings: [] }) mockSave.mockResolvedValue({ success: true }) mocks.extractCustomTools.mockResolvedValue({ saved: 0, errors: [] }) + mocks.loadStoredGraph.mockResolvedValue({ blocks: {}, edges: [], loops: {}, parallels: {} }) }) /** @@ -101,7 +107,7 @@ describe('replaceWorkflowNormalizedState', () => { const result = await replaceWorkflowNormalizedState(input()) expect(mocks.prepare).toHaveBeenCalledWith({ - blocks: { 'block-1': BLOCK }, + blocks: { 'block-1': { ...BLOCK, locked: false } }, edges: [], }) expect(mockSave).toHaveBeenCalledWith( diff --git a/apps/sim/lib/workflows/persistence/replace-normalized-state.ts b/apps/sim/lib/workflows/persistence/replace-normalized-state.ts index 4e1d13cf9a6..be70287957a 100644 --- a/apps/sim/lib/workflows/persistence/replace-normalized-state.ts +++ b/apps/sim/lib/workflows/persistence/replace-normalized-state.ts @@ -2,6 +2,8 @@ import { db } from '@sim/db' import { workflow, workflowBlocks, workflowEdges, workflowSubflows } from '@sim/db/schema' import { createLogger } from '@sim/logger' import { getPostgresConstraintName, getPostgresErrorCode } from '@sim/utils/errors' +import { loadWorkflowFromNormalizedTablesRaw } from '@sim/workflow-persistence/load' +import { preserveProtectedWorkflowBlocks } from '@sim/workflow-types/workflow' import { and, eq, inArray, isNull, ne } from 'drizzle-orm' import { OrchestrationError } from '@/lib/core/orchestration/types' import type { DbOrTx } from '@/lib/db/types' @@ -288,10 +290,13 @@ export async function replaceWorkflowNormalizedState( } const resolved = typeof state === 'function' ? await state(tx) : state - const prepared = prepareWorkflowStateForPersistence({ - blocks: resolved.blocks, - edges: resolved.edges, - }) + const stored = await loadWorkflowFromNormalizedTablesRaw(workflowId, tx) + if (!stored) { + throw new Error('Failed to read the stored workflow graph') + } + // Block locks are admin-only and this write is not, so the stored lock state wins. + const { blocks, edges } = preserveProtectedWorkflowBlocks(stored, resolved) + const prepared = prepareWorkflowStateForPersistence({ blocks, edges }) preparedState = prepared.state warnings = prepared.warnings workflowState = { diff --git a/packages/workflow-types/src/workflow.ts b/packages/workflow-types/src/workflow.ts index 742e4e7c52b..d4ff87a1b08 100644 --- a/packages/workflow-types/src/workflow.ts +++ b/packages/workflow-types/src/workflow.ts @@ -244,6 +244,84 @@ export function isWorkflowBlockProtected( return Boolean(block.locked || isWorkflowBlockAncestorLocked(blockId, blocks)) } +interface WorkflowLockGraph { + blocks: Record + edges: E[] +} + +/** + * Merges a caller-supplied full-graph replacement into the stored graph without + * letting it change anything the block lock protects. + * + * Locking is admin-only (`batch-toggle-locked`) while a full replace needs only + * `write`, so the lock flag is never taken from `incoming`: + * - a block protected in `stored` (locked, or inside a locked container) is kept + * exactly as stored, even when `incoming` changes or omits it, and so are the + * edges into it, matching the edge rules that refuse adding or removing them; + * - a protected block whose unlocked ancestor `incoming` removes goes with that + * ancestor, as it does when the ancestor is deleted directly; + * - every other block is written unlocked; + * - an incoming block that would land inside a locked container is dropped, + * together with the edges that touch it. + * + * Blocks and edges are keyed by their own `id`, never the record key, because + * that is the value persistence writes. + */ +export function preserveProtectedWorkflowBlocks< + B extends WorkflowLockBlock & { id: string }, + E extends WorkflowEdgeEndpoints & { id: string }, +>( + stored: WorkflowLockGraph, + incoming: WorkflowLockGraph +): WorkflowLockGraph & { keptBlockIds: Set } { + const protectedBlockIds = new Set( + Object.keys(stored.blocks).filter((id) => isWorkflowBlockProtected(id, stored.blocks)) + ) + + const blocks: Record = {} + for (const block of Object.values(incoming.blocks)) { + if (!protectedBlockIds.has(block.id)) blocks[block.id] = { ...block, locked: false } + } + + const hasRemovedAncestor = (id: string) => { + const visited = new Set() + let parentId = getWorkflowBlockParentId(stored.blocks[id]) + while (parentId && !visited.has(parentId)) { + visited.add(parentId) + if (!protectedBlockIds.has(parentId) && !blocks[parentId]) return true + parentId = getWorkflowBlockParentId(stored.blocks[parentId]) + } + return false + } + const keptBlockIds = new Set([...protectedBlockIds].filter((id) => !hasRemovedAncestor(id))) + for (const id of keptBlockIds) blocks[id] = stored.blocks[id] + + const removedBlockIds = new Set([ + ...[...protectedBlockIds].filter((id) => !keptBlockIds.has(id)), + ...Object.keys(blocks).filter( + (id) => !keptBlockIds.has(id) && isWorkflowBlockAncestorLocked(id, blocks) + ), + ]) + for (const id of removedBlockIds) delete blocks[id] + + const keptEdges = stored.edges.filter( + (edge) => keptBlockIds.has(edge.target) && blocks[edge.source] + ) + const keptEdgeIds = new Set(keptEdges.map((edge) => edge.id)) + const edges = [ + ...incoming.edges.filter( + (edge) => + !keptEdgeIds.has(edge.id) && + !protectedBlockIds.has(edge.target) && + !removedBlockIds.has(edge.source) && + !removedBlockIds.has(edge.target) + ), + ...keptEdges, + ] + + return { blocks, edges, keptBlockIds } +} + export interface WorkflowEdgeEndpoints { source: string target: string diff --git a/scripts/check-explicit-any.baseline.json b/scripts/check-explicit-any.baseline.json index d8dab07f579..e721dedb6aa 100644 --- a/scripts/check-explicit-any.baseline.json +++ b/scripts/check-explicit-any.baseline.json @@ -1,6 +1,6 @@ { "explicitAny": { - "apps/realtime/src/database/operations.ts": 29, + "apps/realtime/src/database/operations.ts": 27, "apps/realtime/src/handlers/subblocks.ts": 2, "apps/realtime/src/handlers/variables.ts": 2, "apps/sim/app/(auth)/login/login-form.tsx": 2,