Skip to content

Commit b0c7f03

Browse files
committed
fix(workflow-code): compare edges as a set in the row-locked layout-only recheck
The edges read has no ORDER BY, so the evaluation read and the row-locked re-read can return the same edges in different orders (an in-place row rewrite, or a seq scan vs. an index scan on workflow_id + source/target). The layout-only recheck compared the arrays with isEqual and answered a spurious 409 for an unchanged draft. Sort both sides by edge id before comparing. Also give the legacy ~n secret test a dotted loser name: the get's own migration write-back rewrites the two rows one at a time, and an apply that read between them assigned the colliding keys the other way, moving the fingerprint.
1 parent 97b583a commit b0c7f03

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎apps/sim/lib/workflows/application/apply-workflow-source.integration.ts‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
workflow,
2222
workflowBlocks,
2323
workflowDeploymentVersion,
24+
workflowEdges,
2425
workflowSchedule,
2526
workflowSourceMeta,
2627
workspace,
@@ -407,10 +408,11 @@ check('a legacy ~n key block keeps its stored secret through get → apply uncha
407408
})
408409
const winnerId = generateId()
409410
const loserId = generateId()
411+
// Stored blocks load in heap order; only a dotted name loses its key in every order.
410412
const saved = await saveWorkflowToNormalizedTables(workflowId, {
411413
blocks: {
412-
[winnerId]: crawler(winnerId, 'Crawl 1', 'sk-winner-secret'),
413-
[loserId]: crawler(loserId, 'crawl1', 'sk-loser-secret'),
414+
[winnerId]: crawler(winnerId, 'crawl1', 'sk-winner-secret'),
415+
[loserId]: crawler(loserId, 'Crawl.1', 'sk-loser-secret'),
414416
},
415417
edges: [],
416418
loops: {},
@@ -651,6 +653,60 @@ check(
651653
}
652654
)
653655

656+
check(
657+
'an edge row rewritten unchanged while the apply waits on the row lock is no change',
658+
async () => {
659+
const seeded = await seedWorkflow()
660+
const otherId = generateId()
661+
await db.insert(workflowBlocks).values({
662+
id: otherId,
663+
workflowId: seeded.workflowId,
664+
type: 'function',
665+
name: 'Other',
666+
positionX: '0',
667+
positionY: '240',
668+
subBlocks: { code: { id: 'code', type: 'code', value: 'return 0' } },
669+
})
670+
await db.insert(workflowEdges).values({
671+
id: generateId(),
672+
workflowId: seeded.workflowId,
673+
sourceBlockId: seeded.startId,
674+
targetBlockId: otherId,
675+
})
676+
const source = await getSource(seeded.workflowId)
677+
const [firstRead] = await db
678+
.select({ id: workflowEdges.id })
679+
.from(workflowEdges)
680+
.where(eq(workflowEdges.workflowId, seeded.workflowId))
681+
let outcome: Promise<unknown> = Promise.resolve()
682+
let settled = false
683+
684+
await db.transaction(async (tx) => {
685+
const [self] = await tx.execute<{ pid: number }>(sql`SELECT pg_backend_pid() AS pid`)
686+
await tx
687+
.select({ id: workflow.id })
688+
.from(workflow)
689+
.where(eq(workflow.id, seeded.workflowId))
690+
.for('update')
691+
outcome = runOutsideTransactionContext(() =>
692+
apply(source, edit(source.files, SCORE_CODE, 'return 8')).finally(() => {
693+
settled = true
694+
})
695+
)
696+
await waitForBlocked(Number(self?.pid), () => settled)
697+
// An in-place rewrite moves the row to the end of the heap, so the edges read reorders.
698+
await tx
699+
.update(workflowEdges)
700+
.set({ targetHandle: sql`${workflowEdges.targetHandle}` })
701+
.where(eq(workflowEdges.id, firstRead.id))
702+
})
703+
704+
await expect(outcome).resolves.toMatchObject({ applied: true })
705+
const after = await snapshot(seeded.workflowId)
706+
expect(after.state?.blocks[seeded.scoreId]?.subBlocks.code?.value).toBe('return 8')
707+
}
708+
)
709+
654710
check('a block added to a loop stores the loop resized by the targeted layout', async () => {
655711
const workflowId = generateId()
656712
const ids = { start: generateId(), loop: generateId(), inner: generateId() }

‎apps/sim/lib/workflows/application/source-commit.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { db } from '@sim/db'
22
import { workflow as workflowTable } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
44
import { omit } from '@sim/utils/object'
5+
import { compareStrings } from '@sim/utils/string'
56
import {
67
type BlockCatalog,
78
canonicalize,
@@ -153,10 +154,12 @@ function withoutLayout(block: BlockState): Omit<BlockState, (typeof BLOCK_LAYOUT
153154
return block.data ? { ...rest, data: omit(block.data, [...BLOCK_LAYOUT_DATA_FIELDS]) } : rest
154155
}
155156

157+
/** `source` without canvas geometry, its edges in id order: the edges read has no ORDER BY. */
156158
function withoutBlockLayout(source: StoredSource) {
157159
const blocks: Record<string, ReturnType<typeof withoutLayout>> = {}
158160
for (const [id, block] of Object.entries(source.state.blocks)) blocks[id] = withoutLayout(block)
159-
return { ...source, state: { ...source.state, blocks } }
161+
const edges = [...source.state.edges].sort((a, b) => compareStrings(a.id, b.id))
162+
return { ...source, state: { ...source.state, blocks, edges } }
160163
}
161164

162165
/**

0 commit comments

Comments
 (0)