@@ -4,6 +4,7 @@ import type {
44} from '@sim/auth/principal'
55import type { VerifiedInternalDelegation } from '@/lib/auth/internal'
66import { asOrchestrationError } from '@/lib/core/orchestration/types'
7+ import { withDatabaseReadRetry } from '@/lib/db/read-retry'
78import {
89 type ActiveWorkflowApplicationContext ,
910 resolveActiveWorkflowApplicationContext ,
@@ -25,7 +26,15 @@ export class InvalidInternalDelegationBindingError extends Error {
2526 }
2627}
2728
28- /** Binds signed executor claims to the workflow's canonical active workspace. */
29+ const CANONICAL_LOAD_RETRY = { label : 'internal delegation canonical load' } as const
30+
31+ /**
32+ * Binds signed executor claims to the workflow's canonical active workspace.
33+ *
34+ * Every tool call a workflow run delegates (Function, MCP, files, knowledge) binds here first, and
35+ * nothing above it retries. The canonical loads are independent reads, so a transient connection
36+ * failure such as a reset pooled socket is retried instead of failing the block.
37+ */
2938export async function bindInternalExecutorDelegation (
3039 claims : VerifiedInternalDelegation ,
3140 options : BindInternalExecutorDelegationOptions
@@ -43,19 +52,32 @@ export async function bindInternalExecutorDelegation(
4352 try {
4453 if ( claims . currentWorkflow ) {
4554 if ( ! claims . executionId ) throw new InvalidInternalDelegationBindingError ( )
46- const executionContext = await resolveActiveWorkflowExecutionApplicationContext ( {
47- runId : claims . executionId ,
48- assertedWorkflowId : claims . workflowId ,
49- } )
55+ const runId = claims . executionId
56+ const executionContext = await withDatabaseReadRetry (
57+ ( ) =>
58+ resolveActiveWorkflowExecutionApplicationContext ( {
59+ runId,
60+ assertedWorkflowId : claims . workflowId ,
61+ } ) ,
62+ CANONICAL_LOAD_RETRY
63+ )
5064 context = executionContext
5165 rootDeploymentVersionId = executionContext . deploymentVersionId
5266 } else if ( claims . executionId ) {
53- context = await resolveActiveWorkflowRunApplicationContext ( {
54- runId : claims . executionId ,
55- assertedWorkflowId : claims . workflowId ,
56- } )
67+ const runId = claims . executionId
68+ context = await withDatabaseReadRetry (
69+ ( ) =>
70+ resolveActiveWorkflowRunApplicationContext ( {
71+ runId,
72+ assertedWorkflowId : claims . workflowId ,
73+ } ) ,
74+ CANONICAL_LOAD_RETRY
75+ )
5776 } else {
58- context = await resolveActiveWorkflowApplicationContext ( { workflowId : claims . workflowId } )
77+ context = await withDatabaseReadRetry (
78+ ( ) => resolveActiveWorkflowApplicationContext ( { workflowId : claims . workflowId } ) ,
79+ CANONICAL_LOAD_RETRY
80+ )
5981 }
6082 } catch ( error ) {
6183 if ( asOrchestrationError ( error ) ?. code === 'not_found' ) {
@@ -74,18 +96,23 @@ export async function bindInternalExecutorDelegation(
7496 throw new InvalidInternalDelegationBindingError ( )
7597 }
7698 } else {
99+ const currentWorkflow = claims . currentWorkflow
100+ const workspaceId = context . workspaceId
77101 try {
78- const currentContext =
79- claims . currentWorkflow . mode === 'deployment'
80- ? await resolveActiveWorkflowDeploymentVersionApplicationContext ( {
81- workflowId : claims . currentWorkflow . workflowId ,
82- deploymentVersionId : claims . currentWorkflow . deploymentVersionId ,
83- assertedWorkspaceId : context . workspaceId ,
84- } )
85- : await resolveActiveWorkflowApplicationContext ( {
86- workflowId : claims . currentWorkflow . workflowId ,
87- assertedWorkspaceId : context . workspaceId ,
88- } )
102+ const currentContext = await withDatabaseReadRetry (
103+ ( ) =>
104+ currentWorkflow . mode === 'deployment'
105+ ? resolveActiveWorkflowDeploymentVersionApplicationContext ( {
106+ workflowId : currentWorkflow . workflowId ,
107+ deploymentVersionId : currentWorkflow . deploymentVersionId ,
108+ assertedWorkspaceId : workspaceId ,
109+ } )
110+ : resolveActiveWorkflowApplicationContext ( {
111+ workflowId : currentWorkflow . workflowId ,
112+ assertedWorkspaceId : workspaceId ,
113+ } ) ,
114+ CANONICAL_LOAD_RETRY
115+ )
89116 if ( currentContext . workspaceId !== context . workspaceId ) {
90117 throw new InvalidInternalDelegationBindingError ( )
91118 }
0 commit comments