@@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
33import {
44 buildFeedbackPayload ,
55 buildTracePayload ,
6+ langfuseTraceIdForRun ,
67 deriveLangfuseDeliveryState ,
78 isContentToolName ,
89 isPartialRedactToolName ,
@@ -371,9 +372,18 @@ describe('shouldFullyRedactToolPayload (fail-closed)', () => {
371372 } ) ;
372373} ) ;
373374
375+ describe ( 'langfuseTraceIdForRun' , ( ) => {
376+ it ( 'derives the Langfuse-compliant trace id deterministically from the Open Design run id' , ( ) => {
377+ expect ( langfuseTraceIdForRun ( 'run-1' ) ) . toBe (
378+ '4e65d3fbe8ad6535681b021b30785b12' ,
379+ ) ;
380+ } ) ;
381+ } ) ;
382+
374383describe ( 'buildTracePayload' , ( ) => {
375384 it ( 'emits a trace with nested agent + generation observations' , ( ) => {
376385 const batch = buildTracePayload ( makeCtx ( ) ) ;
386+ const traceId = langfuseTraceIdForRun ( 'run-1' ) ;
377387 const types = ( batch as Array < { type : string } > ) . map ( ( e ) => e . type ) ;
378388 expect ( types ) . toEqual ( [
379389 'trace-create' ,
@@ -386,9 +396,13 @@ describe('buildTracePayload', () => {
386396 const gen = bodyOf ( batch , 'generation-create' , 'llm' ) ;
387397 const bash = bodyOf ( batch , 'span-create' , 'tool:Bash' ) ;
388398 const write = bodyOf ( batch , 'span-create' , 'tool:Write' ) ;
399+ const trace = ( batch [ 0 ] as any ) . body ;
400+ expect ( trace . id ) . toBe ( traceId ) ;
401+ expect ( trace . metadata . run_id ) . toBe ( 'run-1' ) ;
402+ expect ( trace . metadata . langfuse_trace_id ) . toBe ( traceId ) ;
389403 expect ( span . id ) . toBe ( 'run-1-agent' ) ;
390- expect ( span . traceId ) . toBe ( 'run-1' ) ;
391- expect ( gen . traceId ) . toBe ( 'run-1' ) ;
404+ expect ( span . traceId ) . toBe ( traceId ) ;
405+ expect ( gen . traceId ) . toBe ( traceId ) ;
392406 expect ( gen . parentObservationId ) . toBe ( 'run-1-agent' ) ;
393407 expect ( bash . parentObservationId ) . toBe ( 'run-1-agent' ) ;
394408 expect ( bash . input ) . toBeUndefined ( ) ;
@@ -1278,7 +1292,10 @@ describe('buildTracePayload', () => {
12781292 ) ;
12791293 const metadata = ( batch [ 0 ] as any ) . body . metadata ;
12801294 expect ( metadata . error_code ) . toBe ( 'RATE_LIMITED' ) ;
1281- expect ( metadata . langfuse_trace_id ) . toBe ( 'run-rate-limit' ) ;
1295+ expect ( metadata . run_id ) . toBe ( 'run-rate-limit' ) ;
1296+ expect ( metadata . langfuse_trace_id ) . toBe (
1297+ langfuseTraceIdForRun ( 'run-rate-limit' ) ,
1298+ ) ;
12821299 expect ( metadata . langfuse_expected ) . toBe ( false ) ;
12831300 expect ( metadata . langfuse_delivery_status ) . toBe ( 'not_expected' ) ;
12841301 expect ( metadata . langfuse_drop_reason ) . toBe ( 'content_consent_off' ) ;
@@ -2528,8 +2545,9 @@ describe('buildFeedbackPayload', () => {
25282545 ) as Array < Record < string , any > > ;
25292546 expect ( batch ) . toHaveLength ( 3 ) ;
25302547 const ratingScore = batch [ 0 ] ! ;
2548+ const traceId = langfuseTraceIdForRun ( 'run-feedback-1' ) ;
25312549 expect ( ratingScore . type ) . toBe ( 'score-create' ) ;
2532- expect ( ratingScore . body . traceId ) . toBe ( 'run-feedback-1' ) ;
2550+ expect ( ratingScore . body . traceId ) . toBe ( traceId ) ;
25332551 expect ( ratingScore . body . name ) . toBe ( 'user_rating' ) ;
25342552 expect ( ratingScore . body . value ) . toBe ( - 1 ) ;
25352553 expect ( ratingScore . body . dataType ) . toBe ( 'NUMERIC' ) ;
@@ -2543,7 +2561,7 @@ describe('buildFeedbackPayload', () => {
25432561 expect ( reasonScore . body . name ) . toBe ( 'user_rating_reason' ) ;
25442562 expect ( reasonScore . body . dataType ) . toBe ( 'CATEGORICAL' ) ;
25452563 expect ( reasonScore . body . comment ) . toBe ( 'negative' ) ;
2546- expect ( reasonScore . body . traceId ) . toBe ( 'run-feedback-1' ) ;
2564+ expect ( reasonScore . body . traceId ) . toBe ( traceId ) ;
25472565 }
25482566 expect ( batch [ 1 ] ! . body . value ) . toBe ( 'missed_request' ) ;
25492567 expect ( batch [ 2 ] ! . body . value ) . toBe ( 'weak_visual' ) ;
@@ -2611,14 +2629,14 @@ describe('reportRunFeedback', () => {
26112629 'score' ,
26122630 ] ) ;
26132631 expect ( envelope . events [ 0 ] . data ) . toMatchObject ( {
2614- id : 'run-feedback-1 -rating' ,
2615- traceId : 'run-feedback-1 ' ,
2632+ id : '908d98293a13a7b7811f44a271aa3b5b -rating' ,
2633+ traceId : '908d98293a13a7b7811f44a271aa3b5b ' ,
26162634 name : 'user_rating' ,
26172635 value : 1 ,
26182636 } ) ;
26192637 expect ( envelope . events [ 1 ] . data ) . toMatchObject ( {
2620- id : 'run-feedback-1 -reason-matched_request' ,
2621- traceId : 'run-feedback-1 ' ,
2638+ id : '908d98293a13a7b7811f44a271aa3b5b -reason-matched_request' ,
2639+ traceId : '908d98293a13a7b7811f44a271aa3b5b ' ,
26222640 name : 'user_rating_reason' ,
26232641 value : 'matched_request' ,
26242642 } ) ;
0 commit comments