1- import { db } from '@sim/db'
1+ import { dbFor } from '@sim/db'
22import { pausedExecutions , resumeQueue , workflowExecutionLogs } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
44import { toError } from '@sim/utils/errors'
@@ -54,6 +54,13 @@ import { hasExecutionResult } from '@/executor/utils/errors'
5454import { filterOutputForLog } from '@/executor/utils/output-filter'
5555import 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+
5764const logger = createLogger ( 'HumanInTheLoopManager' )
5865const RUN_BUFFER_UNAVAILABLE_ERROR = 'Run buffer temporarily unavailable'
5966const 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