@struct-ai/sdk 0.3.0 → 0.3.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { context as otelContext, SpanKind, SpanStatusCode, trace, } from "@opentelemetry/api";
3
- import { getAgentSpan, getSessionId, popPendingToolCallId, pushPendingToolCalls, } from "../context.js";
3
+ import { getAgentSpan, getManualAgentSpan, getPendingToolCalls, getSessionId, } from "../context.js";
4
4
  import { safe } from "../core.js";
5
- import { ERROR_TYPE, EVENT_NAMES, EVENT_NAME, GEN_AI, LANGCHAIN_FINISH_REASON_MAP, ROLE_TO_EVENT_NAME, STRUCT, } from "../semconv.js";
5
+ import { ERROR_TYPE, EVENT_NAMES, EVENT_NAME, GEN_AI, LANGCHAIN, LANGCHAIN_FINISH_REASON_MAP, ROLE_TO_EVENT_NAME, STRUCT, } from "../semconv.js";
6
6
  import { safeJsonStringify, truncateAndSerialize, truncateParts, } from "../truncation.js";
7
7
  import { detectProvider, langchainMessageToRoleAndParts, langchainToInputMessages, langchainToOutputMessages, lastUserMessageParts, } from "./langchain-content.js";
8
8
  /**
@@ -23,29 +23,37 @@ import { detectProvider, langchainMessageToRoleAndParts, langchainToInputMessage
23
23
  *
24
24
  * - `gen_ai.conversation.id` is the OTel GenAI-spec conversation identifier
25
25
  * and Struct's UI grouping unit — one value = one entry in the sessions
26
- * list. For the top-level agent we auto-derive it from thread_id, so a
27
- * stable thread_id across multi-turn chats collapses into one session.
26
+ * list. It is one id per run: every span belonging to a top-level agent
27
+ * invocation AND any subagents it spawns shares the SAME
28
+ * `gen_ai.conversation.id`, so the whole call tree collapses into one
29
+ * session (OTel's `conversation.id` models "the thread", not "the
30
+ * agent").
28
31
  *
29
- * For SUBAGENTS (an agent invoked from inside another's tool body) we
30
- * deliberately assign a DIFFERENT conversation.id — either from the subagent's
31
- * own thread_id if supplied, or a fresh UUID. The resulting session is
32
- * linked back to the outer agent's session via the
33
- * `struct.agent.parent_session_id` span attribute (what powers "Spawned
34
- * by" / subagent navigation in the UI). Without this split the subagent's
35
- * spans would collapse into the outer session, burying delegation.
32
+ * SUBAGENTS (an agent invoked from inside another's tool body) therefore
33
+ * INHERIT the parent run's `gen_ai.conversation.id` rather than minting
34
+ * their own. Structural linkage — "this invoke_agent span was spawned by
35
+ * that one" — is carried separately via the `struct.agent.parent_session_id`
36
+ * span attribute (what powers "Spawned by" / subagent navigation in the
37
+ * UI), not by giving the subagent a different session id.
36
38
  *
37
- * LangChain quirk (handled automatically): when `agent.invoke(...)` runs
38
- * nested inside a parent call, LangChain's config-merge inherits the
39
- * parent's `metadata.thread_id` onto the child even if its config provided
40
- * a different one. We detect that by comparing against the nearest agent
41
- * ancestor's session; if they match, treat it as "inherited, not
42
- * user-intended" and assign a fresh UUID to the subagent.
39
+ * A subagent MAY still be started with its own explicit thread_id (e.g. a
40
+ * LangGraph checkpoint key distinct from the parent's). When that happens
41
+ * we preserve it as `struct.agent.thread_id` for observability, but it does
42
+ * NOT split the session — `gen_ai.conversation.id` still follows the
43
+ * parent, because the grouping unit is the run, not the checkpoint.
44
+ *
45
+ * Chat / tool / retriever spans NEVER fabricate a conversation id. If no
46
+ * ancestor run, metadata thread_id, or ambient session supplies one, the
47
+ * span (and any log events it emits) simply omits `gen_ai.conversation.id`
48
+ * — an orphan span with no session is preferable to a fake one that can
49
+ * never be joined to anything else.
43
50
  *
44
51
  * End-user guidance:
45
52
  * - Use thread_id per conversation; multi-turn chats reuse it.
46
- * - For a subagent call, pass a DIFFERENT thread_id (or omit it and let
47
- * LangGraph generate one). Each subagent then surfaces as its own
48
- * session in the UI, linked back via parent_session_id.
53
+ * - Subagents inherit the enclosing run's session automatically; you
54
+ * don't need to (and shouldn't) pass a different thread_id purely to
55
+ * separate them — use `struct.agent.parent_session_id` /
56
+ * "Spawned by" navigation in the UI instead.
49
57
  */
50
58
  export class StructCallbackHandler {
51
59
  sdk;
@@ -70,6 +78,54 @@ export class StructCallbackHandler {
70
78
  raiseError = false;
71
79
  runs = new Map();
72
80
  internalLogger;
81
+ /**
82
+ * Index of live `execute_tool` spans by their `langgraph_checkpoint_ns`,
83
+ * keyed to an ARRAY of currently-live spans rather than a single span.
84
+ *
85
+ * The re-parenting trick this supports: LangGraph stamps a namespace
86
+ * (`tools:<uuid>`) on a tool-call branch AND the same value on the
87
+ * sub-agent graph that tool triggers, so a sub-agent's chain-start (whose
88
+ * `parentRunId` points at the parent GRAPH — a sibling of the tool in the
89
+ * run tree, not the tool itself) can look itself up by ns and re-parent
90
+ * under its triggering tool call instead of landing as a sibling.
91
+ *
92
+ * That assumption — "ns is unique even across parallel same-named tool
93
+ * calls" — is FALSE on every `@langchain/langgraph` release checked, up
94
+ * to and including the latest published as of this writing (`1.4.7`, and
95
+ * confirmed byte-identical on `main` at HEAD): `ToolNode.run()`
96
+ * (`0.2.x`: `dist/prebuilt/tool_node.js`; `1.x`: moved to
97
+ * `libs/langgraph-core/src/prebuilt/tool_node.ts`) invokes every parallel
98
+ * `tool_call` within one step (`Promise.all(...map(call => this.runTool(
99
+ * call, config, input)))`) with the exact SAME `config` object reference
100
+ * — no per-call config cloning, no call-index-derived namespace — so
101
+ * `config.metadata.langgraph_checkpoint_ns` collides across parallel
102
+ * same-named tool calls in that step on every version, not just 0.2.x.
103
+ * (One opt-in exception: routing tool calls through LangGraph `Send`
104
+ * objects, added ~PR #1498, gets Pregel's normal per-task namespacing —
105
+ * but `createReactAgent`/`ToolNode`'s default path does not use it.) If a
106
+ * future release fixes this, update this comment with the version and
107
+ * consider re-enabling direct pairing unconditionally on it. (Python's
108
+ * `langgraph.prebuilt.tool_node.ToolNode._afunc` does not have this
109
+ * problem: it calls `get_config_list(config, len(tool_calls))` to build a
110
+ * genuinely per-call config before fanning out.)
111
+ *
112
+ * An array-per-ns (instead of one span per ns) makes the collision
113
+ * DETECTABLE instead of silently last-write-wins: at consume time
114
+ * (`handleChainStart`'s sub-agent lookup), exactly one live candidate for
115
+ * the ns is an unambiguous pair (re-parent as before); zero or more than
116
+ * one live candidates means we cannot tell which tool call the sub-agent
117
+ * actually belongs to, so we deliberately do NOT re-parent — the
118
+ * sub-agent falls back to the normal `resolveParent` chain (renders as a
119
+ * graph sibling: degraded, but never wrong). Cross-wiring two unrelated
120
+ * spans is worse than under-wiring one. Entries are evicted (their
121
+ * specific span removed, not the whole ns bucket) in
122
+ * `handleToolEnd`/`handleToolError` so the map never grows unboundedly and
123
+ * so a tool call that finishes frees its slot for ambiguity resolution.
124
+ * Parity: python `_tool_spans_by_ns` / `_checkpoint_ns` (python's
125
+ * `langgraph` >= 1.2.0 does not need this same guard, since its ns values
126
+ * never collide — see above).
127
+ */
128
+ checkpointNsToolSpans = new Map();
73
129
  constructor(sdk, tracer, logger) {
74
130
  this.sdk = sdk;
75
131
  this.tracer = tracer;
@@ -82,7 +138,7 @@ export class StructCallbackHandler {
82
138
  // LangGraph nodes): don't create a span, but record the runId so
83
139
  // downstream children with parentRunId pointing here can still find
84
140
  // their effective parent span.
85
- if (!isAgentChain(chain, runType, runName)) {
141
+ if (!isAgentChain(chain, runType, runName, metadata)) {
86
142
  // Skipped chains inherit session from parent (so chat/tool spans
87
143
  // created under them stay tied to the enclosing agent's session).
88
144
  const sessionId = this.resolveSessionId(parentRunId, metadata);
@@ -92,49 +148,171 @@ export class StructCallbackHandler {
92
148
  effectiveParentSpan: parentSpan,
93
149
  sessionId,
94
150
  nearestAgentSessionId: this.inheritedAgentSessionId(parentRunId),
151
+ nearestAgentSpan: this.inheritedAgentSpan(parentRunId),
152
+ ...this.resolveQueueOwnership(parentRunId),
95
153
  kind: "skipped-chain",
96
154
  });
97
155
  return;
98
156
  }
157
+ // Manual struct.agent() wins over the framework's chain — twin
158
+ // suppression (ownership: manual > framework > provider).
159
+ //
160
+ // Generalized check (0.3.14; was: only a TOP-LEVEL chain, gated on
161
+ // `if (!parentRunId)`): suppress THIS agent chain as a
162
+ // "suppressed-twin" whenever a manual struct.agent() scope is live
163
+ // (`getManualAgentSpan()`) AND the run's EFFECTIVE parent span —
164
+ // resolved exactly like every other span's parent, via
165
+ // `resolveParent`, which already walks THROUGH intervening
166
+ // skipped-chains (RunnableSequence, ChannelWrite, Branch, a
167
+ // `prompt | graph` composition, ...) — resolves to that manual span
168
+ // itself. That condition means this run is the FIRST real agent chain
169
+ // reachable from the manual root through ONLY skipped chains, with no
170
+ // real framework `invoke_agent` in between.
171
+ //
172
+ // This SUBSUMES the old top-level-only case: a genuine top-level
173
+ // chain's effective parent is `resolveParent`'s no-known-parent
174
+ // fallback, `getAgentSpan()` — which core.ts's `agent()` seeds to the
175
+ // SAME span object as `manualAgentSpan`, so it equals `manual`
176
+ // whenever a manual scope is live. It ALSO now catches what the old
177
+ // `!parentRunId` gate missed: `sdk.agent(() => sequence.invoke(...))`
178
+ // wrapping `RunnableSequence -> CompiledStateGraph` (or any skipped
179
+ // chain wrapping the real graph). The sequence is registered as a
180
+ // `skipped-chain` whose `effectiveParentSpan` already resolves to the
181
+ // manual span; the graph's chain-start DOES carry a `parentRunId` (the
182
+ // sequence's) — which used to bypass this check entirely and fall
183
+ // through to a REAL `invoke_agent`, emitting a duplicate — but now
184
+ // resolves ITS OWN effective parent (via the same `resolveParent` every
185
+ // other span uses) to the manual span too, and gets suppressed exactly
186
+ // like a top-level twin would.
187
+ //
188
+ // A genuine nested sub-agent (agent -> tool -> sub-agent, or a
189
+ // sub-agent spawned under a REAL framework `invoke_agent`) instead
190
+ // resolves its effective parent to that real tool/agent SPAN — never
191
+ // the manual span — so `parentSpan !== manual` and it is correctly NOT
192
+ // suppressed, emitting its own `invoke_agent` as before.
193
+ //
194
+ // Record the run pointing at the manual span so descendants parent
195
+ // under it — but emit NO twin invoke_agent span, and NEVER end the
196
+ // manual span from chain callbacks (span: undefined guards that via
197
+ // the `if (!r || !r.span) return;` early-returns in
198
+ // handleChainEnd/handleChainError).
199
+ //
200
+ // Python parity note: `struct-sdk-python`'s `on_chain_start`
201
+ // (langchain.py:681-698) gates the identical suppression on
202
+ // `parent_key is None` — i.e. Python has this SAME top-level-only
203
+ // limitation today; it is not something this TS fix introduces or
204
+ // widens. This fix intentionally puts TS ahead of Python on the
205
+ // RunnableSequence/skipped-chain-wrapped-graph scenario until a
206
+ // matching Python fix lands — filed as a follow-up rather than silently
207
+ // diverging.
208
+ const manual = getManualAgentSpan();
209
+ const { parentSpan, parentSpanIsAgent: parentSpanIsAgentChain } = this.resolveParent(parentRunId);
210
+ void parentSpanIsAgentChain;
211
+ const parentAgentSessionId = this.inheritedAgentSessionId(parentRunId);
212
+ if (manual && parentSpan === manual) {
213
+ // Self-audit round 5 (FIX C): `getSessionId()` alone is the AMBIENT
214
+ // session — `undefined` for a SESSION-LESS manual `struct.agent()`
215
+ // (no explicit/enclosing sessionId; core.ts never fabricates one for
216
+ // its OWN span). Python still gives the suppressed-twin's
217
+ // descendants a COHERENT conversation.id in that case: it resolves
218
+ // `session_id` via `_resolve_agent_session_id` (metadata thread_id,
219
+ // then ambient session, then a fresh UUID) BEFORE checking manual
220
+ // ownership, then falls back to it — `_current_session_id.get(None)
221
+ // or session_id` (langchain.py:685-696) — rather than leaving the
222
+ // suppressed-twin subtree session-less just because the manual scope
223
+ // itself chose not to fabricate one. Mirror that fallback exactly:
224
+ // ambient session first, else the same resolution a real invoke_agent
225
+ // for THIS run would use — `resolveAgentSessionId(metadata,
226
+ // parentAgentSessionId)`, identical to the real-agent path below, so
227
+ // a suppressed twin reached through skipped chains resolves exactly
228
+ // like the real invoke_agent it stands in for would (for a genuinely
229
+ // top-level twin, parentAgentSessionId is undefined, same as before).
230
+ const session = getSessionId() ?? this.resolveAgentSessionId(metadata, parentAgentSessionId);
231
+ this.runs.set(runId, {
232
+ span: undefined,
233
+ effectiveParentSpan: manual,
234
+ sessionId: session,
235
+ nearestAgentSessionId: session,
236
+ nearestAgentSpan: manual,
237
+ // Queue ownership generalizes the same way as everything else here:
238
+ // `resolveQueueOwnership` already inherits verbatim from a
239
+ // registered parent (the skipped chain(s) in between) when the live
240
+ // manual span matches what the parent captured, and only
241
+ // re-captures `getPendingToolCalls()` fresh when there's no
242
+ // resolvable parent (the genuinely-top-level case, matching the old
243
+ // hardcoded direct-capture behavior) or a NEW nested manual scope
244
+ // began (FIX F). No behavior change for the top-level twin; correct
245
+ // inheritance for the new skipped-chain-reached twin.
246
+ ...this.resolveQueueOwnership(parentRunId),
247
+ kind: "suppressed-twin",
248
+ });
249
+ return;
250
+ }
99
251
  const agentName = runName ??
100
252
  extractClassName(chain) ??
101
253
  (typeof inputs === "object" && inputs
102
254
  ? inputs.name
103
255
  : undefined) ??
104
256
  "agent";
105
- // Agent-start: each agent invocation gets its OWN gen_ai.conversation.id.
106
- // Prefer config.configurable.thread_id, then a fresh UUID. Never inherit
107
- // from the parent agent — subagents should appear as separate sessions
108
- // in the UI, linked via ``struct.agent.parent_session_id``.
257
+ // Agent-start: every agent invocation shares ONE gen_ai.conversation.id
258
+ // with the run it belongs to — subagents INHERIT the parent agent's
259
+ // session rather than minting their own (OTel: conversation.id models
260
+ // the thread, not the agent). Only when there's no parent session at
261
+ // all do we fall back to metadata.thread_id, the ambient session, or
262
+ // (as a last resort, so agent spans always have a coherent id) a fresh
263
+ // UUID.
109
264
  //
110
265
  // Convention (documented publicly):
111
266
  // * thread_id is a LangGraph checkpoint identifier. Multiple turns
112
267
  // of one conversation reuse a thread_id → we group them into one
113
268
  // session.
114
- // * Subagents should be given a distinct thread_id (or none at all,
115
- // letting LangGraph generate one). That way a subagent surfaces
116
- // as its own entry in the sessions list.
269
+ // * Subagents don't need a distinct thread_id to appear distinctly —
270
+ // they surface via ``struct.agent.parent_session_id`` linkage
271
+ // ("Spawned by" navigation) while staying in the same session.
117
272
  //
118
- // LangChain quirk (handled below): when an invoke runs nested inside
119
- // a parent's call (e.g. the tool body invokes a subagent), LangChain
120
- // inherits the parent's ``metadata.thread_id`` into the child run's
121
- // metadata — even when the child config supplied its own. Treat that
122
- // inheritance as "no thread_id" and assign a fresh UUID.
273
+ // A subagent MAY still supply its own thread_id (e.g. a distinct
274
+ // LangGraph checkpoint key). We preserve that as ``struct.agent.thread_id``
275
+ // below for observability, but it does not override the inherited
276
+ // conversation.id.
123
277
  //
124
278
  // For the struct.agent.parent_session_id attribute, we need the
125
279
  // NEAREST agent ancestor's session — not just the immediate parent
126
280
  // run, which might be a tool span. That's what
127
- // ``inheritedAgentSessionId`` walks.
128
- const parentAgentSessionId = this.inheritedAgentSessionId(parentRunId);
281
+ // ``inheritedAgentSessionId`` walks. Both `parentAgentSessionId` and
282
+ // `parentSpan` were already resolved above (twin-suppression check),
283
+ // and are reused verbatim here — the run tree hasn't changed between
284
+ // the two reads within this single synchronous callback.
129
285
  const sessionId = this.resolveAgentSessionId(metadata, parentAgentSessionId);
130
286
  if (process.env.STRUCT_SDK_DEBUG === "1") {
131
287
  // eslint-disable-next-line no-console
132
288
  console.error("[struct-sdk] agent-start", agentName, "metadata.thread_id=", metadata?.thread_id, "parent.agent.session=", parentAgentSessionId, "→ resolved session=", sessionId);
133
289
  }
134
- const { parentSpan, parentSpanIsAgent } = this.resolveParent(parentRunId);
135
- void parentSpanIsAgent;
136
- const parentCtx = parentSpan
137
- ? trace.setSpan(otelContext.active(), parentSpan)
290
+ // LangChain agent-as-tool: a sub-agent graph runs as a SIBLING of its
291
+ // triggering execute_tool (parentRunId points at the parent graph, not
292
+ // the tool), so normal resolution would emit this invoke_agent as a
293
+ // sibling. LangGraph stamps the tool branch and its sub-agent with the
294
+ // same `langgraph_checkpoint_ns`, so on versions where that ns really is
295
+ // unique per tool call we re-parent under the matching live execute_tool
296
+ // span to nest natively (the UI's direct-tool-child path).
297
+ //
298
+ // But on every `@langchain/langgraph` release checked (0.2.x through the
299
+ // latest published, 1.4.7 — see the doc comment on
300
+ // `checkpointNsToolSpans`) the ns is NOT unique across parallel
301
+ // same-named tool calls in one step — so we only ever act on an
302
+ // UNAMBIGUOUS match: exactly one live tool span currently registered
303
+ // under this ns. Zero candidates (no tool ever registered this ns, or it
304
+ // already ended) or two-or-more candidates (a collision — we cannot tell
305
+ // which tool call this sub-agent belongs to) both fall back to the
306
+ // normal resolved parent instead of guessing. Cross-wiring two unrelated
307
+ // spans is a worse outcome than under-wiring one to a sibling position.
308
+ // Parity: python `delegating_tool_span` in `on_chain_start` (python's
309
+ // `langgraph` does not need this ambiguity guard — see above).
310
+ const ns = checkpointNs(metadata);
311
+ const nsCandidates = ns ? this.checkpointNsToolSpans.get(ns) : undefined;
312
+ const nsToolSpan = nsCandidates?.length === 1 ? nsCandidates[0] : undefined;
313
+ const effectiveParent = nsToolSpan ?? parentSpan;
314
+ const parentCtx = effectiveParent
315
+ ? trace.setSpan(otelContext.active(), effectiveParent)
138
316
  : otelContext.active();
139
317
  // Span creation can fail (custom tracer / broken context). Skip
140
318
  // run-state insertion when it does — end/error callbacks already
@@ -156,7 +334,16 @@ export class StructCallbackHandler {
156
334
  // session. LangChain doesn't surface a stable agent-id to us, so we
157
335
  // leave the attribute unset. Callers who use struct.agent() directly
158
336
  // can pass their own agentId via the scope options.
159
- startedSpan.setAttribute(GEN_AI.CONVERSATION_ID, sessionId);
337
+ if (sessionId)
338
+ startedSpan.setAttribute(GEN_AI.CONVERSATION_ID, sessionId);
339
+ // A subagent may supply its own local thread_id distinct from the
340
+ // inherited conversation.id (e.g. its own LangGraph checkpoint key).
341
+ // Preserve it for observability WITHOUT letting it split the
342
+ // session — struct.agent.thread_id is non-grouping metadata.
343
+ const localThread = metadataThreadId(metadata);
344
+ if (localThread && localThread !== sessionId) {
345
+ startedSpan.setAttribute(STRUCT.AGENT_THREAD_ID, localThread);
346
+ }
160
347
  // Link subagents back to the parent agent's session. Use the nearest
161
348
  // agent ancestor (NOT the immediate parent run) — a subagent spawned
162
349
  // from inside a tool has the tool as its immediate parent, but we want
@@ -189,6 +376,12 @@ export class StructCallbackHandler {
189
376
  sessionId,
190
377
  // This agent IS itself the nearest agent ancestor for everything nested inside.
191
378
  nearestAgentSessionId: sessionId,
379
+ nearestAgentSpan: startedSpan,
380
+ // Unlike nearestAgentSessionId/nearestAgentSpan, this INHERITS rather
381
+ // than owns — a nested subagent (bare mode) shares its ancestor's
382
+ // queue so a tool call fired from deep inside a subagent's own
383
+ // tool-call cycle still resolves against the same run-tree queue.
384
+ ...this.resolveQueueOwnership(parentRunId),
192
385
  kind: "chain",
193
386
  });
194
387
  };
@@ -239,7 +432,8 @@ export class StructCallbackHandler {
239
432
  startedSpan.setAttribute(GEN_AI.OPERATION_NAME, "chat");
240
433
  startedSpan.setAttribute(GEN_AI.PROVIDER_NAME, provider);
241
434
  startedSpan.setAttribute(GEN_AI.REQUEST_MODEL, String(model));
242
- startedSpan.setAttribute(GEN_AI.CONVERSATION_ID, sessionId);
435
+ if (sessionId)
436
+ startedSpan.setAttribute(GEN_AI.CONVERSATION_ID, sessionId);
243
437
  setRequestAttrsFromInvocation(startedSpan, extraParams?.invocation_params);
244
438
  const flat = messages.flat();
245
439
  if (flat.length > 0) {
@@ -250,8 +444,13 @@ export class StructCallbackHandler {
250
444
  if (this.sdk.emitSpanContent) {
251
445
  startedSpan.setAttribute(GEN_AI.INPUT_MESSAGES, langchainToInputMessages(flat));
252
446
  }
253
- // Propagate the most recent user message onto the nearest agent span.
254
- const ancestorAgentSpan = this.resolveParent(parentRunId).parentSpan ?? getAgentSpan();
447
+ // Propagate the most recent user message onto the nearest AGENT
448
+ // span — never a tool/chat span. `resolveParent(...).parentSpan`
449
+ // is the immediate parent (which may be a tool span when the chat
450
+ // fires from inside a tool body), so we must use the cached
451
+ // nearest-agent-ancestor lookup instead. Parity: python
452
+ // `_find_agent_ancestor` used at `on_chat_model_start` (:905-907).
453
+ const ancestorAgentSpan = this.inheritedAgentSpan(parentRunId);
255
454
  if (ancestorAgentSpan)
256
455
  this.propagateUserPrompt(ancestorAgentSpan, flat);
257
456
  }
@@ -261,6 +460,13 @@ export class StructCallbackHandler {
261
460
  effectiveParentSpan: startedSpan,
262
461
  sessionId,
263
462
  nearestAgentSessionId: this.inheritedAgentSessionId(parentRunId),
463
+ nearestAgentSpan: this.inheritedAgentSpan(parentRunId),
464
+ // No `parentRunId` here means a graph-less bare chat flow (e.g. a
465
+ // bound-tools chat model invoked directly, no AgentExecutor/graph
466
+ // wrapping it) — this run IS the top level, so it gets a fresh queue
467
+ // when bare, exactly like handleChainStart's top-level case.
468
+ ...this.resolveQueueOwnership(parentRunId),
469
+ forcedToolName: extractForcedToolName(extraParams?.invocation_params),
264
470
  kind: "llm",
265
471
  });
266
472
  };
@@ -295,17 +501,42 @@ export class StructCallbackHandler {
295
501
  if (!Array.isArray(toolCalls) || toolCalls.length === 0)
296
502
  return;
297
503
  const pairs = [];
504
+ let forcedToolConsumed = false;
298
505
  for (const tc of toolCalls) {
299
506
  if (tc && typeof tc === "object") {
300
507
  const n = tc.name;
301
508
  const id = tc.id;
302
509
  if (typeof n === "string" && typeof id === "string" && n && id) {
510
+ // A forced tool_choice (structured output extraction) is not a
511
+ // real tool call the agent should autofill via @struct.tool() —
512
+ // exclude it from the pending queue. Parity: python forced-tool
513
+ // exclusion in `on_llm_end` (langchain.py:998-1027).
514
+ if (n === r.forcedToolName) {
515
+ forcedToolConsumed = true;
516
+ continue;
517
+ }
303
518
  pairs.push([n, id]);
304
519
  }
305
520
  }
306
521
  }
307
- if (pairs.length > 0)
308
- pushPendingToolCalls(pairs);
522
+ if (pairs.length > 0 && r.toolCallQueue) {
523
+ // Push directly onto the RunState-PINNED queue OBJECT (decided
524
+ // once at the run-tree root and inherited by reference — see
525
+ // RunState's `toolCallQueue` doc comment), NOT via the
526
+ // ALS-reading `pushPendingToolCalls` helper: this run's callback
527
+ // may fire in a lost/foreign ALS frame, and reading/writing the
528
+ // pinned object directly sidesteps that entirely (in manual mode
529
+ // this object IS the same one context.ts's ALS-scoped helpers
530
+ // read, since it was captured via `getPendingToolCalls()` at the
531
+ // run-tree root — see `resolveQueueOwnership`).
532
+ const q = r.toolCallQueue;
533
+ for (const [name, id] of pairs) {
534
+ (q[name] ??= []).push(id);
535
+ }
536
+ }
537
+ if (forcedToolConsumed) {
538
+ span.setAttribute(GEN_AI.OUTPUT_TYPE, "json");
539
+ }
309
540
  }, "langchain.handleLLMEnd.record_pending_tool_calls", this.internalLogger);
310
541
  safe(() => span.setStatus({ code: SpanStatusCode.OK }), "langchain.handleLLMEnd.set_status", this.internalLogger);
311
542
  safe(() => span.end(), "langchain.handleLLMEnd.span_end", this.internalLogger);
@@ -337,33 +568,85 @@ export class StructCallbackHandler {
337
568
  return;
338
569
  const startedSpan = span;
339
570
  const sessionId = this.resolveSessionId(parentRunId, metadata);
571
+ const queueOwnership = this.resolveQueueOwnership(parentRunId);
340
572
  safe(() => {
341
573
  startedSpan.setAttribute(GEN_AI.OPERATION_NAME, "execute_tool");
342
574
  startedSpan.setAttribute(GEN_AI.PROVIDER_NAME, "langchain");
343
575
  startedSpan.setAttribute(GEN_AI.TOOL_NAME, String(toolName));
344
- // Tool call id from metadata (LangChain passes it there for ToolCall inputs)
345
- // or fallback to the pending queue populated by the LLM's tool_calls.
576
+ // Tool call id from metadata (LangChain passes it there for ToolCall
577
+ // inputs) or fallback to the pending queue populated by the LLM's
578
+ // tool_calls — popped directly off THIS run's PINNED queue OBJECT
579
+ // (`queueOwnership.toolCallQueue`, inherited from the run-tree root —
580
+ // see RunState's `toolCallQueue` doc comment), NOT via the
581
+ // ALS-reading `popPendingToolCallId` helper: this callback may fire
582
+ // in a lost/foreign ALS frame, and popping from the pinned object
583
+ // directly sidesteps that entirely (in manual mode this object IS
584
+ // the same one context.ts's ALS-scoped helpers read/write, since it
585
+ // was captured via `getPendingToolCalls()` at the run-tree root).
346
586
  const callId = metadata?.tool_call_id ??
347
587
  extractToolCallIdFromInput(input) ??
348
- popPendingToolCallId(String(toolName));
588
+ popFromQueue(queueOwnership.toolCallQueue, String(toolName));
349
589
  if (callId)
350
590
  startedSpan.setAttribute(GEN_AI.TOOL_CALL_ID, callId);
351
- startedSpan.setAttribute(GEN_AI.CONVERSATION_ID, sessionId);
591
+ if (sessionId)
592
+ startedSpan.setAttribute(GEN_AI.CONVERSATION_ID, sessionId);
352
593
  if (this.sdk.captureContent && input !== undefined) {
353
594
  startedSpan.setAttribute(GEN_AI.TOOL_CALL_ARGUMENTS, safeJsonStringify(input).slice(0, 8192));
354
595
  }
355
596
  }, "langchain.handleToolStart.set_attrs", this.internalLogger);
597
+ const tool_ns = checkpointNs(metadata);
356
598
  this.runs.set(runId, {
357
599
  span: startedSpan,
358
600
  effectiveParentSpan: startedSpan,
359
601
  sessionId,
360
602
  nearestAgentSessionId: this.inheritedAgentSessionId(parentRunId),
603
+ nearestAgentSpan: this.inheritedAgentSpan(parentRunId),
604
+ // Carried forward (not re-resolved) so a bare sub-agent spawned from
605
+ // inside this tool call inherits the SAME run-tree queue and pinned
606
+ // ownership.
607
+ ...queueOwnership,
608
+ checkpointNs: tool_ns,
361
609
  kind: "tool",
362
610
  });
611
+ // Index by checkpoint ns so a sub-agent graph triggered inside this tool
612
+ // (sharing this exact ns) can re-parent its invoke_agent span under us —
613
+ // PUSH onto the ns's candidate list rather than overwrite it, since
614
+ // `@langchain/langgraph@0.2.x` can hand two parallel same-named tool
615
+ // calls the identical ns (see `checkpointNsToolSpans`'s doc comment).
616
+ // Consume-side (`handleChainStart`) only acts when exactly one candidate
617
+ // is live for the ns; a 2+ list here is exactly the collision case it's
618
+ // built to detect.
619
+ if (tool_ns !== undefined) {
620
+ const bucket = this.checkpointNsToolSpans.get(tool_ns);
621
+ if (bucket)
622
+ bucket.push(startedSpan);
623
+ else
624
+ this.checkpointNsToolSpans.set(tool_ns, [startedSpan]);
625
+ }
363
626
  };
627
+ /**
628
+ * Remove one specific span from its checkpoint-ns bucket (not the whole
629
+ * bucket) — a sibling parallel tool call under the same colliding ns may
630
+ * still be live and must keep its own candidate-list membership. Deletes
631
+ * the ns key entirely once its bucket is empty so the map never grows
632
+ * unboundedly.
633
+ */
634
+ evictCheckpointNsSpan(ns, span) {
635
+ if (ns === undefined || span === undefined)
636
+ return;
637
+ const bucket = this.checkpointNsToolSpans.get(ns);
638
+ if (!bucket)
639
+ return;
640
+ const idx = bucket.indexOf(span);
641
+ if (idx !== -1)
642
+ bucket.splice(idx, 1);
643
+ if (bucket.length === 0)
644
+ this.checkpointNsToolSpans.delete(ns);
645
+ }
364
646
  handleToolEnd = (output, runId) => {
365
647
  const r = this.runs.get(runId);
366
648
  this.runs.delete(runId);
649
+ this.evictCheckpointNsSpan(r?.checkpointNs, r?.span);
367
650
  if (!r || !r.span)
368
651
  return;
369
652
  const span = r.span;
@@ -378,6 +661,7 @@ export class StructCallbackHandler {
378
661
  handleToolError = (err, runId) => {
379
662
  const r = this.runs.get(runId);
380
663
  this.runs.delete(runId);
664
+ this.evictCheckpointNsSpan(r?.checkpointNs, r?.span);
381
665
  if (!r || !r.span)
382
666
  return;
383
667
  const span = r.span;
@@ -406,7 +690,8 @@ export class StructCallbackHandler {
406
690
  startedSpan.setAttribute(GEN_AI.OPERATION_NAME, "retrieval");
407
691
  startedSpan.setAttribute(GEN_AI.PROVIDER_NAME, "langchain");
408
692
  startedSpan.setAttribute(GEN_AI.DATA_SOURCE_ID, String(name));
409
- startedSpan.setAttribute(GEN_AI.CONVERSATION_ID, sessionId);
693
+ if (sessionId)
694
+ startedSpan.setAttribute(GEN_AI.CONVERSATION_ID, sessionId);
410
695
  if (this.sdk.captureContent && typeof query === "string") {
411
696
  startedSpan.setAttribute(GEN_AI.RETRIEVAL_QUERY_TEXT, query.slice(0, 4096));
412
697
  }
@@ -416,6 +701,8 @@ export class StructCallbackHandler {
416
701
  effectiveParentSpan: startedSpan,
417
702
  sessionId,
418
703
  nearestAgentSessionId: this.inheritedAgentSessionId(parentRunId),
704
+ nearestAgentSpan: this.inheritedAgentSpan(parentRunId),
705
+ ...this.resolveQueueOwnership(parentRunId),
419
706
  kind: "retriever",
420
707
  });
421
708
  };
@@ -451,7 +738,7 @@ export class StructCallbackHandler {
451
738
  // filtered chains (RunnableSequence, ChannelWrite, ...).
452
739
  return {
453
740
  parentSpan: p.effectiveParentSpan,
454
- parentSpanIsAgent: p.kind === "chain",
741
+ parentSpanIsAgent: p.kind === "chain" || p.kind === "suppressed-twin",
455
742
  };
456
743
  }
457
744
  }
@@ -459,7 +746,11 @@ export class StructCallbackHandler {
459
746
  }
460
747
  /**
461
748
  * Conversation-id resolution for chat/tool/retriever spans — INHERIT from
462
- * the parent agent so everything rolls up under one gen_ai.conversation.id.
749
+ * the parent run so everything rolls up under one gen_ai.conversation.id.
750
+ * Returns `undefined` (never fabricates a UUID) when no ancestor run,
751
+ * metadata thread_id, or ambient session supplies one — an orphan span
752
+ * with no conversation.id is preferable to a fake one that can never be
753
+ * joined to anything else (parity: python `_resolve_session_id`).
463
754
  */
464
755
  resolveSessionId(parentRunId, metadata) {
465
756
  if (parentRunId) {
@@ -470,10 +761,7 @@ export class StructCallbackHandler {
470
761
  const threadId = metadataThreadId(metadata);
471
762
  if (threadId)
472
763
  return threadId;
473
- const ambient = getSessionId();
474
- if (ambient)
475
- return ambient;
476
- return randomUUID();
764
+ return getSessionId();
477
765
  }
478
766
  /**
479
767
  * Walk the run tree to find the nearest ``invoke_agent`` ancestor's
@@ -493,29 +781,129 @@ export class StructCallbackHandler {
493
781
  return this.runs.get(parentRunId)?.nearestAgentSessionId;
494
782
  }
495
783
  /**
496
- * Conversation-id resolution for AGENT spans — each agent invocation gets
497
- * its own gen_ai.conversation.id. We deliberately do NOT inherit from the
498
- * parent run, so subagents surface as separate entries in the sessions
499
- * list. Parent linkage is preserved via `struct.agent.parent_session_id`.
784
+ * Nearest ``invoke_agent`` ancestor SPAN — O(1) lookup, mirrors
785
+ * ``inheritedAgentSessionId`` for the span pointer itself. Cached at every
786
+ * RunState creation so descendants (and prompt propagation) can reach the
787
+ * ancestor without walking a parent chain we don't retain.
788
+ *
789
+ * Unlike ``inheritedAgentSessionId``, a missing ``parentRunId`` falls back
790
+ * to the ambient `getAgentSpan()` (matching `resolveParent`'s top-level
791
+ * fallback) rather than `undefined` — a run with no LangChain parent may
792
+ * still be nested inside a manually-created `struct.agent()` span.
793
+ * Parity: python `_inherited_agent_span`.
794
+ */
795
+ inheritedAgentSpan(parentRunId) {
796
+ if (!parentRunId)
797
+ return getAgentSpan();
798
+ return this.runs.get(parentRunId)?.nearestAgentSpan ?? getAgentSpan();
799
+ }
800
+ /**
801
+ * Resolve the pending-tool-call queue OBJECT a run should push/pop
802
+ * against for `gen_ai.tool.call.id` autofill, per `RunState.toolCallQueue`'s
803
+ * doc comment.
804
+ *
805
+ * PINNED AT THE RUN-TREE ROOT, INHERITED DOWN AS THE SAME OBJECT
806
+ * REFERENCE — this is the crux of the design, and it is the OBJECT that
807
+ * must be pinned, not merely a manual-vs-bare boolean. Whenever a parent
808
+ * run is known (`parentRunId` resolves to a live `RunState`), we copy its
809
+ * `toolCallQueue` reference VERBATIM rather than re-deriving anything from
810
+ * the live ALS frame. Only a run with no resolvable parent (a genuine
811
+ * root, or an orphaned `parentRunId` whose `RunState` was never
812
+ * registered) consults the live `getManualAgentSpan()` signal — and even
813
+ * then, only ONCE, at that run's creation.
814
+ *
815
+ * Why the OBJECT, not just a boolean: a descendant run's OWN callback can
816
+ * fire in a lost or foreign ALS frame — the LangChain FRAMEWORK layer
817
+ * (`langchain.ts`'s `BaseChatModel.generate`/`.stream` patch) opens its
818
+ * own short-lived ALS scope (`runWithContext({ suppressGenAi: true },
819
+ * fn)`) around each chat completion, entirely independent of any customer
820
+ * `struct.agent()` call; more generally, LangGraph's internal scheduling
821
+ * can run a node's callback outside the async context that originally
822
+ * held the manual scope. A design that pins only a manual-vs-bare
823
+ * ownership BOOLEAN but still routes the actual push (`handleLLMEnd`) and
824
+ * pop (`handleToolStart`) through the ALS-reading
825
+ * `pushPendingToolCalls`/`popPendingToolCallId` helpers (context.ts) is
826
+ * still broken: those helpers read whatever `pendingToolCalls` object is
827
+ * on the CURRENT frame, so a push in one lost/foreign frame and a pop in a
828
+ * DIFFERENT lost/foreign frame silently disagree, even though both frames
829
+ * correctly resolved the same boolean. Operating directly on the
830
+ * RunState-pinned `toolCallQueue` object sidesteps the ALS frame entirely
831
+ * for both operations: whatever object the root observed is what the
832
+ * whole run tree pushes into and pops from, regardless of ALS frame state
833
+ * at either callback.
500
834
  *
501
- * LangChain inherits `metadata.thread_id` from the parent invoke's config
502
- * when a nested invoke runs inside it (even if the child config supplied
503
- * its own). We detect that inheritance by comparing against the parent's
504
- * resolved session and ignore the inherited value.
835
+ * Three cases:
836
+ * 1. `parentRunId` resolves to a registered parent `RunState` — inherit
837
+ * `toolCallQueue` from it verbatim (decided once at the tree root) —
838
+ * UNLESS a NEW manual scope began mid-tree (see FIX F below).
839
+ * 2. No parent (root, or an orphaned `parentRunId`) AND a MANUAL
840
+ * `struct.agent()` scope is live right now (`getManualAgentSpan()`
841
+ * set) — capture `getPendingToolCalls()`, the SAME object
842
+ * `struct.agent()` seeded onto the ALS store and that manual
843
+ * `struct.tool()` calls pop from via `popPendingToolCallId`
844
+ * (context.ts). Captured IN-FRAME here (this callback necessarily
845
+ * runs inside the live manual scope), so the RunState-pinned
846
+ * reference and the ALS store's `pendingToolCalls` stay identical.
847
+ * 3. No parent AND no manual scope — this run IS the top of a bare run
848
+ * tree; mint a fresh queue `{}`.
849
+ *
850
+ * Self-audit round 5 (FIX F): case 1's "inherit verbatim" is only correct
851
+ * when the parent's queue was captured under the SAME manual ownership
852
+ * that is live right now. A NESTED manual `struct.agent()` — opened from
853
+ * inside a tool body, whose own LangChain graph LangChain threads as a
854
+ * CHILD of the outer run tree, parented on a REAL tool span rather than
855
+ * reaching the manual span through skipped chains (so it never reaches
856
+ * the suppressed-twin branch in `handleChainStart`) — would otherwise
857
+ * silently inherit the OUTER run's
858
+ * queue object, even though the CURRENT ambient `getManualAgentSpan()` is
859
+ * the INNER agent's own (different) span and the inner agent's own
860
+ * `pendingToolCalls` (a fresh object seeded by that inner `struct.agent()`
861
+ * call) is what its descendants actually push/pop against. Detect this by
862
+ * comparing the live `getManualAgentSpan()` against the PARENT run's
863
+ * `queueManualSpan` (the manual span that owned the parent's queue): if
864
+ * they differ and the live one is truthy, a new manual scope has begun —
865
+ * re-capture `getPendingToolCalls()` fresh, exactly like case 2. When they
866
+ * match (including both `undefined`, i.e. bare on both sides — preserves
867
+ * the F2 frame-independence fix, where a merely lost/foreign ALS frame
868
+ * reads as `undefined` and must NOT trigger a spurious re-capture),
869
+ * inherit verbatim as before.
870
+ */
871
+ resolveQueueOwnership(parentRunId) {
872
+ const manual = getManualAgentSpan();
873
+ if (parentRunId) {
874
+ const parent = this.runs.get(parentRunId);
875
+ if (parent) {
876
+ if (manual && manual !== parent.queueManualSpan) {
877
+ return { toolCallQueue: getPendingToolCalls() ?? {}, queueManualSpan: manual };
878
+ }
879
+ return { toolCallQueue: parent.toolCallQueue, queueManualSpan: parent.queueManualSpan };
880
+ }
881
+ }
882
+ return {
883
+ toolCallQueue: manual ? (getPendingToolCalls() ?? {}) : {},
884
+ queueManualSpan: manual,
885
+ };
886
+ }
887
+ /**
888
+ * Conversation-id resolution for AGENT spans — INHERIT from the parent
889
+ * run first, so a subagent shares its outer agent's gen_ai.conversation.id
890
+ * (one id per run; parity: python `_resolve_agent_session_id`). Only when
891
+ * there's no parent session do we fall back to metadata.thread_id, the
892
+ * ambient session, or — as a last resort, since agent spans should always
893
+ * have a coherent id — a fresh UUID. A subagent's own divergent thread_id
894
+ * (if any) is preserved separately as `struct.agent.thread_id`; see the
895
+ * caller in `handleChainStart`.
505
896
  */
506
897
  resolveAgentSessionId(metadata, parentSessionId) {
898
+ if (parentSessionId)
899
+ return parentSessionId; // inherit — one id per run
507
900
  const threadId = metadataThreadId(metadata);
508
- if (threadId) {
509
- if (parentSessionId && threadId === parentSessionId) {
510
- // Inherited from parent — treat as unset, assign fresh.
511
- return randomUUID();
512
- }
901
+ if (threadId)
513
902
  return threadId;
514
- }
515
903
  const ambient = getSessionId();
516
904
  if (ambient)
517
905
  return ambient;
518
- return randomUUID();
906
+ return randomUUID(); // agents always get a coherent id
519
907
  }
520
908
  propagateUserPrompt(parentSpan, messages) {
521
909
  try {
@@ -550,6 +938,77 @@ const AGENT_CLASSES = new Set([
550
938
  "Pregel",
551
939
  "LangGraph",
552
940
  ]);
941
+ /**
942
+ * LangChain/LangGraph fires chain-start for every internal Runnable. In
943
+ * Python, ``serialized`` is usually ``None`` for these, so we filter on
944
+ * run_name via a denylist. Matches LangSmith's promotion heuristic.
945
+ * Parity: python `_INTERNAL_RUN_NAMES` (langchain.py:267-306).
946
+ */
947
+ const INTERNAL_RUN_NAMES = new Set([
948
+ // Runnable wiring/plumbing
949
+ "RunnableSequence",
950
+ "RunnableLambda",
951
+ "RunnablePassthrough",
952
+ "RunnableParallel",
953
+ "RunnableBinding",
954
+ "RunnableMap",
955
+ "RunnableAssign",
956
+ "RunnableBranch",
957
+ "RunnableWithFallbacks",
958
+ "RunnableEach",
959
+ "RunnablePick",
960
+ "RunnableGenerator",
961
+ // Prompt templates
962
+ "Prompt",
963
+ "ChatPromptTemplate",
964
+ "PromptTemplate",
965
+ // langchain.agents (1.x) / legacy create_react_agent internal node names
966
+ "agent",
967
+ "tools",
968
+ "call_model",
969
+ "should_continue",
970
+ "__start__",
971
+ "__end__",
972
+ // Output parsers — invoked as Runnables but not agents. LangChain's
973
+ // ``langchain.agents.create_agent`` with ``ToolStrategy`` (or fallback
974
+ // from ``ProviderStrategy`` on models without native structured output)
975
+ // invokes these as a separate step and they fire on_chain_start.
976
+ "PydanticToolsParser",
977
+ "PydanticOutputParser",
978
+ "JsonOutputParser",
979
+ "JsonOutputToolsParser",
980
+ "JsonOutputKeyToolsParser",
981
+ "StrOutputParser",
982
+ "OutputParser",
983
+ "BaseOutputParser",
984
+ "OpenAIToolsAgentOutputParser",
985
+ "OpenAIFunctionsAgentOutputParser",
986
+ // TS-ecosystem addition beyond the Python `_INTERNAL_RUN_NAMES` list: this
987
+ // class only exists in @langchain/anthropic (JS), not in Python LangChain,
988
+ // so the byte-identical port doesn't cover it. `ChatAnthropic
989
+ // .withStructuredOutput(schema)` (without `include_raw`) internally chains
990
+ // through @langchain/anthropic's AnthropicToolsOutputParser
991
+ // (dist/output_parsers.js), which fires on_chain_start with this run name.
992
+ // Do not remove this during a cross-SDK denylist diff against Python.
993
+ "AnthropicToolsOutputParser",
994
+ // TS-ecosystem addition beyond Python's `_INTERNAL_RUN_NAMES`: needed for
995
+ // @langchain/langgraph 0.2.x internal sub-chains. `createReactAgent`'s
996
+ // prompt-application step (dist/prebuilt/react_agent_executor.js,
997
+ // `PROMPT_RUNNABLE_NAME = "prompt"`) is wired via
998
+ // `RunnableLambda.from(...).withConfig({ runName: "prompt" })` and invoked
999
+ // as a step INSIDE the "agent" Pregel node — so its own
1000
+ // `metadata.langgraph_node` is stamped with the PARENT node's name (e.g.
1001
+ // "agent"), not "prompt" itself, and the self-match rule (3 above) can't
1002
+ // suppress it. Confirmed live: this fires on_chain_start once per model
1003
+ // turn on installed @langchain/langgraph@0.2.74, producing spurious
1004
+ // `invoke_agent prompt` spans (live-parity REPORT.md). Python's
1005
+ // currently-resolved langgraph (1.2.0, via `create_agent`) doesn't hit
1006
+ // this — a LangGraph major-version confound, not something struct-sdk
1007
+ // controls. Do not remove this during a cross-SDK denylist diff against
1008
+ // Python.
1009
+ "prompt",
1010
+ ]);
1011
+ const INTERNAL_RUN_NAME_PREFIXES = ["ChannelWrite<", "Branch<", "RunnableSequence<"];
553
1012
  /**
554
1013
  * Threading-id metadata keys, in resolution order.
555
1014
  *
@@ -576,11 +1035,67 @@ function metadataThreadId(metadata) {
576
1035
  }
577
1036
  return undefined;
578
1037
  }
579
- function isAgentChain(chain, runType, _runName) {
1038
+ /**
1039
+ * LangGraph stamps a unique `langgraph_checkpoint_ns` (`tools:<uuid>`) on
1040
+ * each tool-call branch, and the SAME value on the sub-agent graph that the
1041
+ * tool triggers — even across parallel same-named tool calls. Used to
1042
+ * re-parent a sub-agent's `invoke_agent` span under its triggering
1043
+ * `execute_tool` span. Returns the namespace, or `undefined` if absent.
1044
+ * Parity: python `_checkpoint_ns` (langchain.py:325-341).
1045
+ */
1046
+ function checkpointNs(metadata) {
1047
+ if (!metadata || typeof metadata !== "object")
1048
+ return undefined;
1049
+ const ns = metadata["langgraph_checkpoint_ns"];
1050
+ return typeof ns === "string" && ns.length > 0 ? ns : undefined;
1051
+ }
1052
+ /**
1053
+ * Only promote user-meaningful chains to `invoke_agent` spans.
1054
+ *
1055
+ * Decision order (parity: python `_is_agent_chain`, langchain.py:360-407):
1056
+ *
1057
+ * 1. Explicit `runType === "agent"` (legacy AgentExecutor) → agent.
1058
+ * 2. `chain` class identifies a Pregel/CompiledStateGraph → agent.
1059
+ * 3. `metadata.langgraph_node === runName` → INTERNAL Pregel node (every
1060
+ * internal step of a `create_agent` Pregel fires chain-start with
1061
+ * metadata.langgraph_node set to its node name; for real top-level
1062
+ * agents or sub-agents the names differ or langgraph_node is absent).
1063
+ * 4. Known LangChain plumbing run names (denylist) → not agent.
1064
+ * 5. Otherwise, if there's a runName → agent (user-named chain).
1065
+ */
1066
+ function isAgentChain(chain, runType, runName, metadata) {
580
1067
  if (runType === "agent")
581
1068
  return true;
582
1069
  const cls = extractClassName(chain);
583
- return !!cls && AGENT_CLASSES.has(cls);
1070
+ if (cls && AGENT_CLASSES.has(cls))
1071
+ return true;
1072
+ // Internal Pregel node detection: LangGraph populates metadata with
1073
+ // `langgraph_node` (and `langgraph_step`) on every internal node
1074
+ // callback. The run_name of an internal node matches its langgraph_node;
1075
+ // for the top-level Pregel invocation, langgraph_node is absent; for a
1076
+ // sub-agent invoked from a tool body, langgraph_node may be set BUT
1077
+ // contains the *parent's* node name (e.g. "tools"), which differs from
1078
+ // the sub-agent's own run_name. So equality is the discriminator.
1079
+ if (metadata && runName) {
1080
+ const lgNode = metadata["langgraph_node"];
1081
+ if (typeof lgNode === "string" && lgNode && lgNode === runName)
1082
+ return false;
1083
+ }
1084
+ if (runName) {
1085
+ // LangChain names parametrized runnables `Base<...>` — e.g.
1086
+ // `RunnableParallel<raw>` and `RunnableAssign<parsed,parsing_error>`
1087
+ // from `with_structured_output(include_raw=True)`. Strip the `<...>`
1088
+ // so the base class matches the denylist instead of falling through to
1089
+ // the user-named-chain promotion below (a real production phantom-agent
1090
+ // bug found via a customer's structured-output topology).
1091
+ const baseName = runName.split("<", 1)[0];
1092
+ if (INTERNAL_RUN_NAMES.has(baseName))
1093
+ return false;
1094
+ if (INTERNAL_RUN_NAME_PREFIXES.some((p) => runName.startsWith(p)))
1095
+ return false;
1096
+ return true; // user-named chain → promoted
1097
+ }
1098
+ return false;
584
1099
  }
585
1100
  function extractClassName(obj) {
586
1101
  if (!obj)
@@ -605,6 +1120,47 @@ function extractParam(obj, key) {
605
1120
  const v = obj[key];
606
1121
  return typeof v === "string" && v.length > 0 ? v : undefined;
607
1122
  }
1123
+ /**
1124
+ * Extract the tool name the model was forced to call via `tool_choice`, if
1125
+ * any. Handles both provider shapes LangChain forwards in
1126
+ * `invocation_params.tool_choice`:
1127
+ * - Anthropic: `{type:"tool", name}`
1128
+ * - OpenAI: `{type:"function", function:{name}}`
1129
+ * `invocation` is unknown-typed (LangChain's `extraParams.invocation_params`
1130
+ * is untyped provider passthrough) — every layer is defensively checked.
1131
+ * Parity: python `_forced_tool_name` (langchain.py:410-432).
1132
+ */
1133
+ function extractForcedToolName(invocation) {
1134
+ if (!invocation || typeof invocation !== "object")
1135
+ return undefined;
1136
+ const tc = invocation["tool_choice"];
1137
+ if (!tc || typeof tc !== "object")
1138
+ return undefined;
1139
+ const t = tc;
1140
+ if (t.type === "tool" && typeof t.name === "string")
1141
+ return t.name;
1142
+ if (t.type === "function") {
1143
+ const fn = t.function;
1144
+ if (fn && typeof fn === "object" && typeof fn.name === "string") {
1145
+ return fn.name;
1146
+ }
1147
+ }
1148
+ return undefined;
1149
+ }
1150
+ /**
1151
+ * `@langchain/anthropic` (JS) keys whose value is a negative sentinel
1152
+ * meaning "not set by the caller" rather than a real request parameter.
1153
+ * `dist/chat_models.js` defaults BOTH `topK` and `topP` to `-1` and always
1154
+ * includes them in `invocationParams()` as `top_k`/`top_p` — unlike
1155
+ * Python's `ChatAnthropic`, which never forwards `top_k`/`top_p` unless the
1156
+ * caller genuinely set them. Confirmed live: TS was stamping
1157
+ * `gen_ai.request.top_k = -1` on every chat span (live-parity REPORT.md).
1158
+ * Scoped tightly to these two keys — do NOT extend blanket-negative
1159
+ * filtering to `frequencyPenalty`/`presencePenalty`, which are legitimately
1160
+ * negative in OpenAI's real range (-2.0..2.0). JS-ecosystem sentinel;
1161
+ * python parity: absent-when-unset.
1162
+ */
1163
+ const NEGATIVE_SENTINEL_KEYS = new Set(["topK", "top_k", "topP", "top_p"]);
608
1164
  function setRequestAttrsFromInvocation(span, invocation) {
609
1165
  if (!invocation || typeof invocation !== "object")
610
1166
  return;
@@ -622,8 +1178,11 @@ function setRequestAttrsFromInvocation(span, invocation) {
622
1178
  ];
623
1179
  for (const [src, dst] of mapping) {
624
1180
  const v = m[src];
625
- if (typeof v === "number")
626
- span.setAttribute(dst, v);
1181
+ if (typeof v !== "number")
1182
+ continue;
1183
+ if (NEGATIVE_SENTINEL_KEYS.has(src) && v < 0)
1184
+ continue;
1185
+ span.setAttribute(dst, v);
627
1186
  }
628
1187
  const stop = m.stop ?? m.stopSequences ?? m.stop_sequences;
629
1188
  if (Array.isArray(stop) && stop.length > 0) {
@@ -660,11 +1219,24 @@ function setLlmResponseAttrs(span, sdk, logger, message, provider, sessionId) {
660
1219
  const mapped = LANGCHAIN_FINISH_REASON_MAP[finish] ?? finish;
661
1220
  span.setAttribute(GEN_AI.RESPONSE_FINISH_REASONS, [mapped]);
662
1221
  }
663
- const respId = (typeof m.id === "string" && m.id) ||
664
- (typeof respMeta.id === "string" && respMeta.id) ||
665
- null;
666
- if (respId)
1222
+ // Prefer the provider message id (`msg_...` / `chatcmpl-...`) from
1223
+ // response_metadata over LangChain's own run id. ChatAnthropic (and most
1224
+ // LangChain chat model adapters) place the real API-level id in
1225
+ // `response_metadata.id` while `message.id` carries a LangChain-internal
1226
+ // run id. gen_ai.response.id is used as the duplicate-detection
1227
+ // fingerprint downstream, so the provider id must take priority. The
1228
+ // LangChain run id is preserved under `langchain.run.id` (only when it
1229
+ // diverges) so consumers can still join back to LangChain/LangSmith run
1230
+ // data. Parity: python `_set_llm_response_attrs` (langchain.py:1560-1576).
1231
+ const providerId = typeof respMeta.id === "string" ? respMeta.id : undefined;
1232
+ const lcRunId = typeof m.id === "string" ? m.id : undefined;
1233
+ const respId = providerId ?? lcRunId ?? null;
1234
+ if (respId) {
667
1235
  span.setAttribute(GEN_AI.RESPONSE_ID, respId);
1236
+ if (lcRunId && lcRunId !== respId) {
1237
+ span.setAttribute(LANGCHAIN.RUN_ID, lcRunId);
1238
+ }
1239
+ }
668
1240
  if (sdk.emitEvents && logger) {
669
1241
  emitChoiceEvent(logger, message, provider ?? "langchain", sessionId, span);
670
1242
  }
@@ -695,9 +1267,9 @@ function emitMessageEvents(logger, messages, provider, sessionId, span) {
695
1267
  attributes: {
696
1268
  [EVENT_NAME]: eventName,
697
1269
  body: payload,
698
- [GEN_AI.SYSTEM]: provider,
1270
+ [GEN_AI.PROVIDER_NAME]: provider,
699
1271
  [GEN_AI.MESSAGE_INDEX]: i,
700
- [GEN_AI.CONVERSATION_ID]: sessionId,
1272
+ ...(sessionId ? { [GEN_AI.CONVERSATION_ID]: sessionId } : {}),
701
1273
  },
702
1274
  });
703
1275
  }
@@ -722,8 +1294,8 @@ function emitChoiceEvent(logger, message, provider, sessionId, span) {
722
1294
  attributes: {
723
1295
  [EVENT_NAME]: EVENT_NAMES.CHOICE,
724
1296
  body: payload,
725
- [GEN_AI.SYSTEM]: provider,
726
- [GEN_AI.CONVERSATION_ID]: sessionId,
1297
+ [GEN_AI.PROVIDER_NAME]: provider,
1298
+ ...(sessionId ? { [GEN_AI.CONVERSATION_ID]: sessionId } : {}),
727
1299
  },
728
1300
  });
729
1301
  }
@@ -743,6 +1315,23 @@ function extractToolCallIdFromInput(input) {
743
1315
  }
744
1316
  return undefined;
745
1317
  }
1318
+ /**
1319
+ * Pop the first pending tool_use id matching `name` from a RunState-pinned
1320
+ * (run-tree-scoped, not ALS-scoped) queue object. FIFO semantics identical
1321
+ * to `context.ts`'s `popPendingToolCallId` — see `RunState.toolCallQueue`.
1322
+ * Used for BOTH manual and bare ownership: in manual mode the queue object
1323
+ * passed in is the same one `getPendingToolCalls()` (context.ts) reads, so
1324
+ * popping here and popping via that ALS-scoped helper observe the same
1325
+ * state.
1326
+ */
1327
+ function popFromQueue(queue, name) {
1328
+ if (!queue)
1329
+ return undefined;
1330
+ const ids = queue[name];
1331
+ if (!ids || ids.length === 0)
1332
+ return undefined;
1333
+ return ids.shift();
1334
+ }
746
1335
  function recordError(span, err) {
747
1336
  const errorType = err instanceof Error ? err.constructor.name : typeof err;
748
1337
  const message = err instanceof Error ? err.message : String(err);