File tree Expand file tree Collapse file tree
cli/src/modules/instance-ai Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ export type { DomainAccessTracker } from './domain-access';
1717export {
1818 createInstanceAiTraceContext ,
1919 continueInstanceAiTraceContext ,
20+ releaseTraceClient ,
2021 withCurrentTraceSpan ,
2122} from './tracing/langsmith-tracing' ;
2223export { createInstanceAgent } from './agent/instance-agent' ;
Original file line number Diff line number Diff line change @@ -423,6 +423,15 @@ function mergeRunTreeInputs(
423423 } ;
424424}
425425
426+ /**
427+ * Unconditionally remove the cached LangSmith Client for a trace.
428+ * Call after run finalization (success or failure) so the Client and
429+ * its RunTree hierarchy can be garbage-collected.
430+ */
431+ export function releaseTraceClient ( traceId : string ) : void {
432+ traceClients . delete ( traceId ) ;
433+ }
434+
426435export function getTraceParentRun ( ) : RunTree | undefined {
427436 const overrideRun = traceParentOverrideStorage . getStore ( ) ?. current ;
428437 if ( overrideRun ) {
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ import {
3434 PlannedTaskCoordinator ,
3535 PlannedTaskStorage ,
3636 applyPlannedTaskPermissions ,
37+ releaseTraceClient ,
3738 resumeAgentRun ,
3839 RunStateRegistry ,
3940 startBuildWorkflowAgentTask ,
@@ -544,6 +545,8 @@ export class InstanceAiService {
544545 threadId : tracing . rootRun . metadata ?. thread_id ,
545546 error : getErrorMessage ( error ) ,
546547 } ) ;
548+ } finally {
549+ releaseTraceClient ( tracing . rootRun . traceId ) ;
547550 }
548551 }
549552
@@ -574,6 +577,7 @@ export class InstanceAiService {
574577 private deleteTraceContextsForThread ( threadId : string ) : void {
575578 for ( const [ runId , entry ] of this . traceContextsByRunId ) {
576579 if ( entry . threadId === threadId ) {
580+ releaseTraceClient ( entry . tracing . rootRun . traceId ) ;
577581 this . traceContextsByRunId . delete ( runId ) ;
578582 }
579583 }
@@ -609,6 +613,8 @@ export class InstanceAiService {
609613 traceRunId : traceContext . rootRun . id ,
610614 error : getErrorMessage ( error ) ,
611615 } ) ;
616+ } finally {
617+ releaseTraceClient ( traceContext . rootRun . traceId ) ;
612618 }
613619 }
614620
You can’t perform that action at this time.
0 commit comments