Skip to content

Commit 07e668b

Browse files
committed
fix(langgraph): guard createReactAgent suppression flag against tool-wrap throws
1 parent 90d1118 commit 07e668b

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

  • packages/server-utils/src/integrations/tracing-channel

packages/server-utils/src/integrations/tracing-channel/langgraph.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,20 @@ const _langGraphChannelIntegration = ((options: LangGraphOptions = {}) => {
8989
CHANNELS.LANGGRAPH_CREATE_REACT_AGENT,
9090
);
9191
reactAgentChannel.start.subscribe(message => {
92-
const { arguments: args } = message as CreateReactAgentChannelContext;
93-
const params = getFirstArgObject(args);
94-
if (params && Array.isArray(params.tools) && params.tools.length > 0) {
95-
wrapToolsWithSpans(params.tools, resolvedOptions, extractAgentNameFromParams(args) ?? undefined);
96-
}
97-
// Set only after tool wrapping so a throw there can't leave the flag stuck on and permanently
98-
// suppress `create_agent` spans. The flag must be on before the body runs its internal compile.
92+
// `createReactAgent` runs synchronously and compiles a `StateGraph` internally, so the flag
93+
// must be on for the duration and off by `end`. It's set here (never in a branch that can
94+
// throw) and cleared in both `end` and `error`, so it can neither stick on across calls nor
95+
// stay off during this call's nested compile. Tool wrapping is guarded for the same reason.
9996
insideCreateReactAgent = true;
97+
try {
98+
const { arguments: args } = message as CreateReactAgentChannelContext;
99+
const params = getFirstArgObject(args);
100+
if (params && Array.isArray(params.tools) && params.tools.length > 0) {
101+
wrapToolsWithSpans(params.tools, resolvedOptions, extractAgentNameFromParams(args) ?? undefined);
102+
}
103+
} catch (error) {
104+
DEBUG_BUILD && debug.error('[orchestrion:langgraph] failed to wrap createReactAgent tools', error);
105+
}
100106
});
101107
reactAgentChannel.end.subscribe(message => {
102108
insideCreateReactAgent = false;

0 commit comments

Comments
 (0)