@@ -223,9 +223,27 @@ export async function createAgentInvocation(
223223 input . agentName ? childConversationId : invocationId
224224 } `;
225225 return await getSqlExecutor ( ) . withLock ( lockName , async ( ) => {
226+ const existingBinding = input . agentName
227+ ? await getAgentBinding ( {
228+ name : input . agentName ,
229+ parentConversationId : input . parentConversationId ,
230+ } )
231+ : undefined ;
232+ if (
233+ existingBinding &&
234+ input . reasoningLevel !== undefined &&
235+ input . reasoningLevel !== existingBinding . reasoningLevel
236+ ) {
237+ throw new Error (
238+ `Named agent binding policy changed for ${ input . agentName } ` ,
239+ ) ;
240+ }
241+ const effectiveInput = existingBinding ?. reasoningLevel
242+ ? { ...input , reasoningLevel : existingBinding . reasoningLevel }
243+ : input ;
226244 const existing = await getAgentInvocation ( invocationId ) ;
227245 if ( existing ) {
228- if ( ! sameCreateInput ( existing , input ) ) {
246+ if ( ! sameCreateInput ( existing , effectiveInput ) ) {
229247 throw new Error (
230248 `Agent invocation idempotency key was reused with different input for ${ invocationId } ` ,
231249 ) ;
@@ -249,7 +267,7 @@ export async function createAgentInvocation(
249267 createdAt : new Date ( nowMs ) ,
250268 name : input . agentName ,
251269 parentConversationId : input . parentConversationId ,
252- reasoningLevel : input . reasoningLevel ?? null ,
270+ reasoningLevel : effectiveInput . reasoningLevel ?? null ,
253271 updatedAt : new Date ( nowMs ) ,
254272 } )
255273 . onConflictDoNothing ( ) ;
@@ -262,7 +280,7 @@ export async function createAgentInvocation(
262280 `Named agent binding did not resolve to ${ childConversationId } ` ,
263281 ) ;
264282 }
265- if ( binding . reasoningLevel !== input . reasoningLevel ) {
283+ if ( binding . reasoningLevel !== effectiveInput . reasoningLevel ) {
266284 throw new Error (
267285 `Named agent binding policy changed for ${ input . agentName } ` ,
268286 ) ;
@@ -283,13 +301,13 @@ export async function createAgentInvocation(
283301 parentConversationId : input . parentConversationId ,
284302 childConversationId,
285303 agentName : input . agentName ?? null ,
286- input : input . input ,
287- actor : input . actor ,
288- credentialContext : input . credentialContext ?? null ,
289- source : input . source ,
290- destination : input . destination ,
291- destinationVisibility : input . destinationVisibility ?? null ,
292- reasoningLevel : input . reasoningLevel ?? null ,
304+ input : effectiveInput . input ,
305+ actor : effectiveInput . actor ,
306+ credentialContext : effectiveInput . credentialContext ?? null ,
307+ source : effectiveInput . source ,
308+ destination : effectiveInput . destination ,
309+ destinationVisibility : effectiveInput . destinationVisibility ?? null ,
310+ reasoningLevel : effectiveInput . reasoningLevel ?? null ,
293311 status : "pending" ,
294312 mailboxStatus : "pending" ,
295313 createdAt : new Date ( nowMs ) ,
@@ -298,7 +316,7 @@ export async function createAgentInvocation(
298316 } )
299317 . onConflictDoNothing ( ) ;
300318 const invocation = await getAgentInvocation ( invocationId ) ;
301- if ( ! invocation || ! sameCreateInput ( invocation , input ) ) {
319+ if ( ! invocation || ! sameCreateInput ( invocation , effectiveInput ) ) {
302320 throw new Error ( `Agent invocation creation raced for ${ invocationId } ` ) ;
303321 }
304322 return { invocation, status : "created" } ;
0 commit comments