Skip to content

Commit fe5a9ce

Browse files
feat(db): route pause/resume and large-value metadata persistence to the exec pool
Coverage scan follow-up: the paused_executions / resume_queue / workflow_execution_logs transactions in human-in-the-loop-manager.ts and the large-value owner/reference registration writes on the execution path now use dbFor('exec'), matching the completion writes in the execution logger. All are self-contained; billing calls remain outside the moved transactions on the default client. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NNy9Fzpfc1FdHAzYyb6Ycy
1 parent 165a7bb commit fe5a9ce

3 files changed

Lines changed: 45 additions & 32 deletions

File tree

apps/sim/lib/execution/payloads/large-value-metadata.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,20 @@ const {
8383
}
8484
})
8585

86-
vi.mock('@sim/db', () => ({
87-
db: {
86+
vi.mock('@sim/db', () => {
87+
const db = {
8888
delete: mockDelete,
8989
execute: mockExecute,
9090
insert: mockInsert,
9191
select: mockSelect,
9292
transaction: mockTransaction,
93-
},
94-
}))
93+
}
94+
return {
95+
db,
96+
// Exec-pool client shares the instance so the seeded chains still apply.
97+
dbFor: () => db,
98+
}
99+
})
95100

96101
vi.mock('@sim/db/schema', () => ({
97102
executionLargeValueDependencies: {

apps/sim/lib/execution/payloads/large-value-metadata.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { db } from '@sim/db'
1+
import { db, dbFor } from '@sim/db'
22
import {
33
executionLargeValueDependencies,
44
executionLargeValueReferences,
@@ -188,7 +188,7 @@ export async function registerLargeValueOwner(
188188
return false
189189
}
190190

191-
await db.transaction(async (tx) => {
191+
await dbFor('exec').transaction(async (tx) => {
192192
await tx
193193
.insert(executionLargeValues)
194194
.values({
@@ -311,7 +311,8 @@ export async function addLargeValueReference(
311311
return
312312
}
313313

314-
const [existingRef] = await db
314+
const execDb = dbFor('exec')
315+
const [existingRef] = await execDb
315316
.select({ key: executionLargeValueReferences.key })
316317
.from(executionLargeValueReferences)
317318
.where(
@@ -328,7 +329,7 @@ export async function addLargeValueReference(
328329
return
329330
}
330331

331-
const existingRefs = await db
332+
const existingRefs = await execDb
332333
.select({ key: executionLargeValueReferences.key })
333334
.from(executionLargeValueReferences)
334335
.where(
@@ -346,7 +347,7 @@ export async function addLargeValueReference(
346347
)
347348
}
348349

349-
await db
350+
await execDb
350351
.insert(executionLargeValueReferences)
351352
.values({
352353
key: boundedKey,
@@ -365,7 +366,7 @@ export async function replaceLargeValueReferences(
365366
const referenceKeys = scope.workspaceId
366367
? collectLargeValueReferenceKeys(value, scope.workspaceId)
367368
: []
368-
await db.transaction(async (tx) => {
369+
await dbFor('exec').transaction(async (tx) => {
369370
await replaceLargeValueReferenceKeysWithClient(tx, scope, referenceKeys)
370371
})
371372
}

apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { db } from '@sim/db'
1+
import { dbFor } from '@sim/db'
22
import { pausedExecutions, resumeQueue, workflowExecutionLogs } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
44
import { toError } from '@sim/utils/errors'
@@ -54,6 +54,13 @@ import { hasExecutionResult } from '@/executor/utils/errors'
5454
import { filterOutputForLog } from '@/executor/utils/output-filter'
5555
import type { SerializedConnection } from '@/serializer/types'
5656

57+
/**
58+
* All paused-execution / resume-queue / execution-log persistence in this
59+
* module runs on the exec pool, mirroring the completion writes in
60+
* `lib/logs/execution/logger.ts`.
61+
*/
62+
const execDb = dbFor('exec')
63+
5764
const logger = createLogger('HumanInTheLoopManager')
5865
const RUN_BUFFER_UNAVAILABLE_ERROR = 'Run buffer temporarily unavailable'
5966
const TERMINAL_PUBLISH_ERROR = 'Run buffer terminal event publish failed'
@@ -373,7 +380,7 @@ export class PauseResumeManager {
373380
...resumeMetadata,
374381
}
375382

376-
await db.transaction(async (tx) => {
383+
await execDb.transaction(async (tx) => {
377384
const existing = await tx
378385
.select()
379386
.from(pausedExecutions)
@@ -489,7 +496,7 @@ export class PauseResumeManager {
489496
static async enqueueOrStartResume(args: EnqueueResumeArgs): Promise<EnqueueResumeResult> {
490497
const { executionId, workflowId, contextId, resumeInput, userId, allowedPauseKinds } = args
491498

492-
return await db.transaction(async (tx) => {
499+
return await execDb.transaction(async (tx) => {
493500
const pausedExecution = await tx
494501
.select()
495502
.from(pausedExecutions)
@@ -815,7 +822,7 @@ export class PauseResumeManager {
815822
} = args
816823
const parentExecutionId = pausedExecution.executionId
817824

818-
await db
825+
await execDb
819826
.update(workflowExecutionLogs)
820827
.set({ status: 'running' })
821828
.where(eq(workflowExecutionLogs.executionId, parentExecutionId))
@@ -1653,7 +1660,7 @@ export class PauseResumeManager {
16531660
const { resumeEntryId, pausedExecutionId, parentExecutionId, contextId } = args
16541661
const now = new Date()
16551662

1656-
await db.transaction(async (tx) => {
1663+
await execDb.transaction(async (tx) => {
16571664
await tx
16581665
.update(resumeQueue)
16591666
.set({ status: 'completed', completedAt: now, failureReason: null })
@@ -1720,7 +1727,7 @@ export class PauseResumeManager {
17201727
}): Promise<void> {
17211728
const now = new Date()
17221729

1723-
await db.transaction(async (tx) => {
1730+
await execDb.transaction(async (tx) => {
17241731
await tx
17251732
.update(resumeQueue)
17261733
.set({ status: 'failed', failureReason: args.failureReason, completedAt: now })
@@ -1752,7 +1759,7 @@ export class PauseResumeManager {
17521759
}): Promise<void> {
17531760
const now = new Date()
17541761

1755-
await db.transaction(async (tx) => {
1762+
await execDb.transaction(async (tx) => {
17561763
const pausedExecution = args.preserveForRetry
17571764
? await tx
17581765
.select({
@@ -1843,7 +1850,7 @@ export class PauseResumeManager {
18431850
}): Promise<void> {
18441851
const { pausedExecutionId, contextId, pauseBlockId, executionState } = args
18451852

1846-
const pausedExecution = await db
1853+
const pausedExecution = await execDb
18471854
.select()
18481855
.from(pausedExecutions)
18491856
.where(eq(pausedExecutions.id, pausedExecutionId))
@@ -1905,7 +1912,7 @@ export class PauseResumeManager {
19051912
? collectLargeValueReferenceKeys(snapshotReferenceValue, snapshotWorkspaceId)
19061913
: []
19071914

1908-
await db.transaction(async (tx) => {
1915+
await execDb.transaction(async (tx) => {
19091916
await tx
19101917
.update(pausedExecutions)
19111918
.set({
@@ -1937,7 +1944,7 @@ export class PauseResumeManager {
19371944
static async beginPausedCancellation(executionId: string, workflowId: string): Promise<boolean> {
19381945
const now = new Date()
19391946

1940-
return await db.transaction(async (tx) => {
1947+
return await execDb.transaction(async (tx) => {
19411948
const pausedExecution = await tx
19421949
.select({ id: pausedExecutions.id, status: pausedExecutions.status })
19431950
.from(pausedExecutions)
@@ -1992,7 +1999,7 @@ export class PauseResumeManager {
19921999
): Promise<boolean> {
19932000
const now = new Date()
19942001

1995-
return await db.transaction(async (tx) => {
2002+
return await execDb.transaction(async (tx) => {
19962003
const pausedExecution = await tx
19972004
.select({ id: pausedExecutions.id, status: pausedExecutions.status })
19982005
.from(pausedExecutions)
@@ -2033,7 +2040,7 @@ export class PauseResumeManager {
20332040
): Promise<boolean> {
20342041
const now = new Date()
20352042

2036-
return await db.transaction(async (tx) => {
2043+
return await execDb.transaction(async (tx) => {
20372044
const pausedExecution = await tx
20382045
.select({ id: pausedExecutions.id })
20392046
.from(pausedExecutions)
@@ -2077,7 +2084,7 @@ export class PauseResumeManager {
20772084
workflowId: string
20782085
): Promise<void> {
20792086
const now = new Date()
2080-
await db
2087+
await execDb
20812088
.update(pausedExecutions)
20822089
.set({
20832090
status: sql`CASE WHEN resumed_count > 0 THEN 'partially_resumed' ELSE 'paused' END`,
@@ -2097,7 +2104,7 @@ export class PauseResumeManager {
20972104
executionId: string,
20982105
workflowId: string
20992106
): Promise<'cancelling' | 'cancelled' | null> {
2100-
const activeResume = await db
2107+
const activeResume = await execDb
21012108
.select({ id: resumeQueue.id })
21022109
.from(resumeQueue)
21032110
.where(and(eq(resumeQueue.parentExecutionId, executionId), eq(resumeQueue.status, 'claimed')))
@@ -2108,7 +2115,7 @@ export class PauseResumeManager {
21082115
return null
21092116
}
21102117

2111-
const pausedExecution = await db
2118+
const pausedExecution = await execDb
21122119
.select({ status: pausedExecutions.status })
21132120
.from(pausedExecutions)
21142121
.where(
@@ -2135,7 +2142,7 @@ export class PauseResumeManager {
21352142
}): Promise<void> {
21362143
const now = new Date()
21372144

2138-
await db.transaction(async (tx) => {
2145+
await execDb.transaction(async (tx) => {
21392146
const pausedExecution = await tx
21402147
.select({
21412148
automaticResumeRetryCount: pausedExecutions.automaticResumeRetryCount,
@@ -2228,7 +2235,7 @@ export class PauseResumeManager {
22282235
pausedExecutionId: string
22292236
nextResumeAt: Date | null
22302237
}): Promise<void> {
2231-
await db
2238+
await execDb
22322239
.update(pausedExecutions)
22332240
.set({ nextResumeAt: args.nextResumeAt })
22342241
.where(
@@ -2260,7 +2267,7 @@ export class PauseResumeManager {
22602267
}
22612268
}
22622269

2263-
const rows = await db
2270+
const rows = await execDb
22642271
.select()
22652272
.from(pausedExecutions)
22662273
.where(whereClause)
@@ -2278,7 +2285,7 @@ export class PauseResumeManager {
22782285
static async getPausedExecutionById(
22792286
id: string
22802287
): Promise<typeof pausedExecutions.$inferSelect | null> {
2281-
const rows = await db
2288+
const rows = await execDb
22822289
.select()
22832290
.from(pausedExecutions)
22842291
.where(eq(pausedExecutions.id, id))
@@ -2292,7 +2299,7 @@ export class PauseResumeManager {
22922299
}): Promise<PausedExecutionDetail | null> {
22932300
const { workflowId, executionId } = options
22942301

2295-
const row = await db
2302+
const row = await execDb
22962303
.select()
22972304
.from(pausedExecutions)
22982305
.where(
@@ -2308,7 +2315,7 @@ export class PauseResumeManager {
23082315
return null
23092316
}
23102317

2311-
const queueEntries = await db
2318+
const queueEntries = await execDb
23122319
.select()
23132320
.from(resumeQueue)
23142321
.where(eq(resumeQueue.parentExecutionId, executionId))
@@ -2386,7 +2393,7 @@ export class PauseResumeManager {
23862393
} | null = null
23872394

23882395
while (!pendingEntry) {
2389-
const selection = await db.transaction(async (tx) => {
2396+
const selection = await execDb.transaction(async (tx) => {
23902397
const pausedExecution = await tx
23912398
.select()
23922399
.from(pausedExecutions)

0 commit comments

Comments
 (0)