@@ -629,7 +629,10 @@ class LiveKitTracing {
629629 const state = this . sessions . get ( row . session ) ;
630630 if ( ! row . owner ) {
631631 row . span . log ( endEvent ( row , attrs , events ) ) ;
632- if ( state ) correlate ( row , identities , state . relationships ) ;
632+ if ( state )
633+ state . relationships . batch ( row . span , ( ) =>
634+ correlate ( row , identities , state . relationships ) ,
635+ ) ;
633636 if ( state && row . name === "user_speaking" )
634637 row . span . log ( {
635638 metadata : {
@@ -1287,6 +1290,19 @@ class Relationships {
12871290 private targets = new Map < string , Span [ ] > ( ) ;
12881291 private waiting = new Map < string , Map < Span , Relationship > > ( ) ;
12891292 private links = new Map < Span , Map < string , Link > > ( ) ;
1293+ /** A source whose new links publish together, and whether it has any. */
1294+ private batched ?: { source : Span ; changed : boolean } ;
1295+
1296+ /** Publishes the links `fn` adds from `source` once, as one complete list. */
1297+ batch ( source : Span , fn : ( ) => void ) : void {
1298+ const batched = ( this . batched = { source, changed : false } ) ;
1299+ try {
1300+ fn ( ) ;
1301+ } finally {
1302+ this . batched = undefined ;
1303+ if ( batched . changed ) this . publish ( source ) ;
1304+ }
1305+ }
12901306
12911307 register ( id : string , target : Span ) : void {
12921308 const targets = this . targets . get ( id ) ?? [ ] ;
@@ -1337,9 +1353,15 @@ class Relationships {
13371353 attributes : { "braintrust.relationship.type" : relationship } ,
13381354 } ) ;
13391355 this . links . set ( source , links ) ;
1356+ if ( this . batched ?. source === source ) this . batched . changed = true ;
1357+ else this . publish ( source ) ;
1358+ }
1359+
1360+ private publish ( source : Span ) : void {
13401361 // Native rows have no top-level links field, and OTel ingestion does not keep
13411362 // links yet. Metadata carries them until links are first-class.
1342- source . log ( { metadata : { links : [ ...links . values ( ) ] } } ) ;
1363+ const links = [ ...( this . links . get ( source ) ?. values ( ) ?? [ ] ) ] ;
1364+ source . log ( { metadata : { links } } ) ;
13431365 }
13441366}
13451367
0 commit comments