1- import { describe , expect , test } from "bun:test" ;
21import type { ToolCall , ToolResult } from "@intx/types/runtime" ;
2+ import { afterEach , describe , expect , test } from "bun:test" ;
33import type { Telemetry } from "./index.js" ;
4+ import { generationSampleRate } from "./index.js" ;
45import type { TurnContext } from "../session/hooks.js" ;
56import {
67 aggregateToolCalls ,
@@ -12,11 +13,17 @@ import {
1213 secondsFromMs ,
1314 turnTraceId ,
1415} from "./ai-observability.js" ;
16+ import { getCurrentTurnTraceId , resetFeedbackStateForTests } from "./feedback.js" ;
1517
1618const SUBAGENT_TOOL_NAME = "task" ;
1719const SESSION_ID = "0199-parent-session" ;
1820
21+ afterEach ( ( ) => {
22+ resetFeedbackStateForTests ( ) ;
23+ } ) ;
24+
1925function fakeTelemetry ( ) : {
26+
2027 telemetry : Telemetry ;
2128 captured : { event : string ; properties : Record < string , unknown > } [ ] ;
2229} {
@@ -210,6 +217,21 @@ describe("createTurnObserver", () => {
210217 expect ( captured [ 1 ] ?. properties . $ai_provider ) . toBe ( "codex" ) ;
211218 expect ( captured [ 1 ] ?. properties . $ai_model ) . toBe ( "model-y" ) ;
212219 } ) ;
220+
221+ test ( "onTurnStarted notes the in-flight turn for subagent parent_trace_id" , ( ) => {
222+ const { telemetry } = fakeTelemetry ( ) ;
223+ const observer = createTurnObserver ( {
224+ telemetry : ( ) => telemetry ,
225+ getSessionId : ( ) => SESSION_ID ,
226+ getSource : ( ) => ( { provider : "openai-compatible" , model : "model-x" } ) ,
227+ subagentToolName : SUBAGENT_TOOL_NAME ,
228+ } ) ;
229+
230+ observer . onTurnStarted ( { turnIndex : 2 } ) ;
231+ expect ( getCurrentTurnTraceId ( ) ) . toBe ( `${ SESSION_ID } :turn:2` ) ;
232+ observer . onTurnComplete ( fakeTurnContext ( { turnIndex : 2 , toolCalls : [ ] , toolResults : [ ] } ) ) ;
233+ expect ( getCurrentTurnTraceId ( ) ) . toBeUndefined ( ) ;
234+ } ) ;
213235} ) ;
214236
215237describe ( "emitAiObservability" , ( ) => {
@@ -378,6 +400,13 @@ describe("emitAiObservability", () => {
378400 expect ( captured ) . toHaveLength ( 1 ) ;
379401 expect ( captured [ 0 ] ?. event ) . toBe ( "$ai_generation" ) ;
380402 } ) ;
403+
404+ test ( "empty CORBITS_TELEMETRY_GENERATION_SAMPLE_RATE is treated as unset (1.0)" , ( ) => {
405+ expect ( generationSampleRate ( { CORBITS_TELEMETRY_GENERATION_SAMPLE_RATE : "" } ) ) . toBe ( 1 ) ;
406+ expect ( generationSampleRate ( { CORBITS_TELEMETRY_GENERATION_SAMPLE_RATE : " " } ) ) . toBe ( 1 ) ;
407+ expect ( generationSampleRate ( { } ) ) . toBe ( 1 ) ;
408+ expect ( generationSampleRate ( { CORBITS_TELEMETRY_GENERATION_SAMPLE_RATE : "0" } ) ) . toBe ( 0 ) ;
409+ } ) ;
381410} ) ;
382411
383412describe ( "emitAiTurnFailure" , ( ) => {
0 commit comments