@@ -229,17 +229,21 @@ export class UpdateMetadataService extends BaseService {
229229 }
230230
231231 public async call (
232- environment : AuthenticatedEnvironment ,
233232 runId : string ,
234- body : UpdateMetadataRequestBody
233+ body : UpdateMetadataRequestBody ,
234+ environment ?: AuthenticatedEnvironment
235235 ) {
236236 const runIdType = runId . startsWith ( "run_" ) ? "friendly" : "internal" ;
237237
238238 const taskRun = await this . _prisma . taskRun . findFirst ( {
239- where : {
240- runtimeEnvironmentId : environment . id ,
241- ...( runIdType === "internal" ? { id : runId } : { friendlyId : runId } ) ,
242- } ,
239+ where : environment
240+ ? {
241+ runtimeEnvironmentId : environment . id ,
242+ ...( runIdType === "internal" ? { id : runId } : { friendlyId : runId } ) ,
243+ }
244+ : {
245+ ...( runIdType === "internal" ? { id : runId } : { friendlyId : runId } ) ,
246+ } ,
243247 select : {
244248 id : true ,
245249 status : true ,
@@ -351,6 +355,15 @@ export class UpdateMetadataService extends BaseService {
351355 } ) ;
352356
353357 if ( result . count === 0 ) {
358+ if ( this . flushLoggingEnabled ) {
359+ logger . debug (
360+ `[UpdateMetadataService][updateRunMetadataWithOperations] Optimistic lock failed for run ${ runId } ` ,
361+ {
362+ metadataVersion : run . metadataVersion ,
363+ }
364+ ) ;
365+ }
366+
354367 // If this was our last attempt, buffer the operations and return optimistically
355368 if ( attempts === MAX_RETRIES ) {
356369 this . #ingestRunOperations( runId , operations ) ;
@@ -363,6 +376,15 @@ export class UpdateMetadataService extends BaseService {
363376 continue ;
364377 }
365378
379+ if ( this . flushLoggingEnabled ) {
380+ logger . debug (
381+ `[UpdateMetadataService][updateRunMetadataWithOperations] Updated metadata for run ${ runId } ` ,
382+ {
383+ metadata : applyResults . newMetadata ,
384+ }
385+ ) ;
386+ }
387+
366388 // Success! Return the new metadata
367389 return applyResults . newMetadata ;
368390 }
@@ -383,10 +405,15 @@ export class UpdateMetadataService extends BaseService {
383405 metadataPacket . data !== "{}" ||
384406 ( existingMetadata . data && metadataPacket . data !== existingMetadata . data )
385407 ) {
386- logger . debug ( `Updating metadata directly for run` , {
387- metadata : metadataPacket . data ,
388- runId,
389- } ) ;
408+ if ( this . flushLoggingEnabled ) {
409+ logger . debug (
410+ `[UpdateMetadataService][updateRunMetadataDirectly] Updating metadata directly for run` ,
411+ {
412+ metadata : metadataPacket . data ,
413+ runId,
414+ }
415+ ) ;
416+ }
390417
391418 // Update the metadata without version check
392419 await this . _prisma . taskRun . update ( {
@@ -416,6 +443,13 @@ export class UpdateMetadataService extends BaseService {
416443 } ;
417444 } ) ;
418445
446+ if ( this . flushLoggingEnabled ) {
447+ logger . debug ( `[UpdateMetadataService] Ingesting operations for run` , {
448+ runId,
449+ bufferedOperations,
450+ } ) ;
451+ }
452+
419453 const existingBufferedOperations = this . _bufferedOperations . get ( runId ) ?? [ ] ;
420454
421455 this . _bufferedOperations . set ( runId , [ ...existingBufferedOperations , ...bufferedOperations ] ) ;
0 commit comments