-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathload.ts
More file actions
325 lines (302 loc) · 12.8 KB
/
Copy pathload.ts
File metadata and controls
325 lines (302 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { db, workflow, workflowBlocks, workflowEdges, workflowSubflows } from '@sim/db'
import { createLogger } from '@sim/logger'
import type { BlockRetryConfig, BlockState, Loop, Parallel } from '@sim/workflow-types/workflow'
import {
normalizeBlockRetryTries,
normalizeBlockRetryWaitMs,
normalizeWorkflowEdgeSourceHandle,
normalizeWorkflowEdgeTargetHandle,
SUBFLOW_TYPES,
} from '@sim/workflow-types/workflow'
import type { Edge } from '@xyflow/react'
import { and, eq, getTableColumns, isNull, sql } from 'drizzle-orm'
import { clampParallelBatchSize } from './subflow-helpers'
import type { DbOrTx, NormalizedWorkflowData } from './types'
const logger = createLogger('WorkflowPersistenceLoad')
/**
* Rebuilds a stored retry policy as a real {@link BlockRetryConfig} instead of
* asserting that the raw `jsonb` blob already is one.
*
* The column is written verbatim by writers that never validate its contents —
* the realtime batch-add and replace-state ops take untyped block records, and
* the admin/superuser import routes persist externally-authored workflow JSON —
* so a row can hold an out-of-range number, a missing field, or a non-boolean
* flag. Pinning the values here is the policy the feature declares (see
* `resolveBlockRetryConfig`) and applies it to every reader at once: the editor
* renders exactly the numbers execution will use, and the strict HTTP contract
* that serves this state can never reject a workflow it is meant to open.
*
* `enabled` is carried across rather than resolved, so the numbers a builder
* configured survive switching retry off and back on. That is why
* `resolveBlockRetryConfig` — which collapses a disabled policy to `null` —
* must not be used on this path.
*/
/**
* Stored subflow config coerced to the string the loaded shape declares.
*
* `config` is schemaless JSONB and the writers are looser than the readers:
* `?? ''` only replaces `null`/`undefined`, so an object or number written into
* `whileCondition` or `doWhileCondition` survived to a consumer whose schema
* asserts `z.string()`. On the v2 read that is a `500` on a workflow that opens
* fine in the editor. Non-strings are preserved as JSON rather than dropped, so
* nothing a caller wrote is lost.
*
* `forEachItems` and `distribution` deliberately do NOT go through this: both
* are declared `unknown[] | Record<string, unknown> | string` in
* `@sim/workflow-types` and published as that union by the v2 read schema, so a
* stored array is correct data rather than a shape to coerce.
*/
function storedConfigString(value: unknown): string {
if (typeof value === 'string') return value
if (value === null || value === undefined) return ''
try {
return JSON.stringify(value) ?? ''
} catch {
return ''
}
}
/** Stored subflow `nodes` filtered to the block-id strings the shape declares. */
function storedConfigNodes(value: unknown): string[] {
return Array.isArray(value)
? value.filter((node): node is string => typeof node === 'string')
: []
}
function normalizeStoredBlockRetry(stored: unknown): BlockRetryConfig | undefined {
if (stored == null || typeof stored !== 'object') return undefined
const retry = stored as Record<string, unknown>
return {
enabled: Boolean(retry.enabled),
maxTries: normalizeBlockRetryTries(retry.maxTries),
waitBetweenTriesMs: normalizeBlockRetryWaitMs(retry.waitBetweenTriesMs),
}
}
export interface RawNormalizedWorkflow extends NormalizedWorkflowData {
workspaceId: string
/**
* Each block row's `updated_at` rendered by Postgres as text, preserving the
* full microsecond precision. Kept as a string on purpose: Drizzle's `date`
* mode surfaces timestamps as JS `Date`s, which truncate to milliseconds, so
* a Date-based value can never be used for an exact compare-and-set against
* rows stamped by SQL `now()`/`defaultNow()`.
*/
blockUpdatedAtById: Record<string, string | null>
}
/**
* Load workflow state from normalized tables without running block migrations.
* Block migrations (credential rewrites, subblock ID migrations, canonical-mode
* backfill, tool sanitization) depend on the block/tool registry that lives in
* the Next app and should not be pulled into leaf services. Callers that want
* migrated state should wrap this with their own migration pipeline.
*
* Invariant: downstream migrations must not mutate `block.data.collection`,
* `block.data.whileCondition`, or `block.data.doWhileCondition`. Those fields
* are patched here from the subflow config on the pre-migration block, and
* callers re-sync only `loop.enabled`/`parallel.enabled` from the migrated
* block. If a future migration rewrites these data fields, the loop/parallel
* config on the returned object will silently diverge from the migrated block.
*/
export async function loadWorkflowFromNormalizedTablesRaw(
workflowId: string,
externalTx?: DbOrTx
): Promise<RawNormalizedWorkflow | null> {
try {
const tx = externalTx ?? db
const [blocks, edges, subflows, [workflowRow]] = await Promise.all([
tx
.select({
...getTableColumns(workflowBlocks),
updatedAtText: sql<string | null>`${workflowBlocks.updatedAt}::text`,
})
.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)),
tx
.select({ workspaceId: workflow.workspaceId })
.from(workflow)
.where(eq(workflow.id, workflowId))
.limit(1),
])
if (!workflowRow) return null
if (!workflowRow.workspaceId) {
throw new Error(`Workflow ${workflowId} has no workspace`)
}
const blocksMap: Record<string, BlockState> = {}
const blockUpdatedAtById: Record<string, string | null> = {}
blocks.forEach((block) => {
const blockData = (block.data ?? {}) as BlockState['data']
const assembled: BlockState = {
id: block.id,
type: block.type,
name: block.name,
position: {
x: Number(block.positionX),
y: Number(block.positionY),
},
enabled: block.enabled,
horizontalHandles: block.horizontalHandles,
advancedMode: block.advancedMode,
errorEnabled: block.errorEnabled,
retry: normalizeStoredBlockRetry(block.retry),
triggerMode: block.triggerMode,
height: Number(block.height),
subBlocks: (block.subBlocks as BlockState['subBlocks']) || {},
outputs: (block.outputs as BlockState['outputs']) || {},
data: blockData,
locked: block.locked,
}
blocksMap[block.id] = assembled
blockUpdatedAtById[block.id] = block.updatedAtText ?? null
})
const edgesArray: Edge[] = edges.map((edge) => ({
id: edge.id,
source: edge.sourceBlockId,
target: edge.targetBlockId,
sourceHandle: normalizeWorkflowEdgeSourceHandle(edge.sourceHandle) ?? undefined,
targetHandle: normalizeWorkflowEdgeTargetHandle(edge.targetHandle) ?? undefined,
type: 'default',
data: {},
}))
const loops: Record<string, Loop> = {}
const parallels: Record<string, Parallel> = {}
subflows.forEach((subflow) => {
const config = (subflow.config ?? {}) as Partial<Loop & Parallel>
if (subflow.type === SUBFLOW_TYPES.LOOP) {
const loopType =
(config as Loop).loopType === 'for' ||
(config as Loop).loopType === 'forEach' ||
(config as Loop).loopType === 'while' ||
(config as Loop).loopType === 'doWhile'
? (config as Loop).loopType
: 'for'
const loop: Loop = {
id: subflow.id,
nodes: storedConfigNodes((config as Loop).nodes),
iterations:
typeof (config as Loop).iterations === 'number' ? (config as Loop).iterations : 1,
loopType,
forEachItems: (config as Loop).forEachItems ?? '',
whileCondition: storedConfigString((config as Loop).whileCondition),
doWhileCondition: storedConfigString((config as Loop).doWhileCondition),
enabled: blocksMap[subflow.id]?.enabled ?? true,
}
loops[subflow.id] = loop
if (blocksMap[subflow.id]) {
const block = blocksMap[subflow.id]
blocksMap[subflow.id] = {
...block,
data: {
...block.data,
collection: loop.forEachItems ?? block.data?.collection ?? '',
whileCondition: loop.whileCondition ?? block.data?.whileCondition ?? '',
doWhileCondition: loop.doWhileCondition ?? block.data?.doWhileCondition ?? '',
},
}
}
} else if (subflow.type === SUBFLOW_TYPES.PARALLEL) {
const parallel: Parallel = {
id: subflow.id,
nodes: storedConfigNodes((config as Parallel).nodes),
count: typeof (config as Parallel).count === 'number' ? (config as Parallel).count : 5,
distribution: (config as Parallel).distribution ?? '',
parallelType:
(config as Parallel).parallelType === 'count' ||
(config as Parallel).parallelType === 'collection'
? (config as Parallel).parallelType
: 'count',
batchSize: clampParallelBatchSize((config as Parallel).batchSize),
enabled: blocksMap[subflow.id]?.enabled ?? true,
}
parallels[subflow.id] = parallel
if (blocksMap[subflow.id]) {
const block = blocksMap[subflow.id]
blocksMap[subflow.id] = {
...block,
data: {
...block.data,
count: parallel.count,
collection: parallel.distribution ?? block.data?.collection ?? '',
parallelType: parallel.parallelType,
batchSize: parallel.batchSize,
},
}
}
} else {
logger.warn(`Unknown subflow type: ${subflow.type} for subflow ${subflow.id}`)
}
})
return {
blocks: blocksMap,
edges: edgesArray,
loops,
parallels,
isFromNormalizedTables: true,
workspaceId: workflowRow.workspaceId,
blockUpdatedAtById,
}
} catch (error) {
logger.error(`Error loading workflow ${workflowId} from normalized tables:`, error)
return null
}
}
/**
* Optimistic-concurrency guard: matches a row's `updated_at` exactly against
* the microsecond-precision text value captured at read time.
*
* The comparison deliberately round-trips through text rather than a JS
* `Date`. Postgres stores `timestamp` columns with microsecond precision, but
* Drizzle's `date` mode truncates to milliseconds on read, so a Date-based
* equality guard can never match rows stamped by SQL `now()`/`defaultNow()` —
* the UPDATE silently no-ops forever and load-time migrations never persist.
* Casting the captured text back to `timestamp` restores an exact
* compare-and-set: a concurrent write stamps a new `updated_at` and the guard
* fails closed (skipped, logged, retried on the next load). Residual, inherent
* limit: a concurrent app write whose JS `new Date()` collides with the stored
* value at exact precision is indistinguishable; a version column would be
* required to close that fully.
*/
function updatedAtMatches(expectedText: string) {
return eq(workflowBlocks.updatedAt, sql`${expectedText}::timestamp`)
}
export async function persistMigratedBlocks(
workflowId: string,
originalBlocks: Record<string, BlockState>,
migratedBlocks: Record<string, BlockState>,
blockUpdatedAtById: Record<string, string | null> = {}
): Promise<void> {
try {
for (const [blockId, block] of Object.entries(migratedBlocks)) {
if (block !== originalBlocks[blockId]) {
const hasExpectedUpdatedAt = Object.hasOwn(blockUpdatedAtById, blockId)
const expectedUpdatedAt = blockUpdatedAtById[blockId]
const whereClause = hasExpectedUpdatedAt
? and(
eq(workflowBlocks.id, blockId),
eq(workflowBlocks.workflowId, workflowId),
expectedUpdatedAt === null
? isNull(workflowBlocks.updatedAt)
: updatedAtMatches(expectedUpdatedAt)
)
: and(eq(workflowBlocks.id, blockId), eq(workflowBlocks.workflowId, workflowId))
const persisted = await db
.update(workflowBlocks)
.set({
subBlocks: block.subBlocks,
data: block.data,
updatedAt: new Date(),
})
.where(whereClause)
.returning({ id: workflowBlocks.id })
if (persisted.length === 0) {
logger.warn('Skipped persisting block migration (row changed since read or missing)', {
workflowId,
blockId,
expectedUpdatedAt,
})
}
}
}
} catch (err) {
logger.warn('Failed to persist block migrations', { workflowId, error: err })
}
}