Skip to content

Add a Worker-side tracingApiKey option to the TypeScript OpenAI Agents plugin - #215

Merged
campoy merged 2 commits into
mainfrom
fix/openai-agents-typescript-tracing-key-dropped-across-boundary
Oct 30, 2026
Merged

campoy merged 2 commits into
mainfrom
fix/openai-agents-typescript-tracing-key-dropped-across-boundary

Conversation

@campoy

@campoy campoy commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

OpenAIAgentsPlugin has a new optional Worker-side option, tracingApiKey, that gives a tracing key to spans that cross a Temporal boundary without one:

new OpenAIAgentsPlugin({
  modelProvider,
  tracingApiKey: ({ traceId, traceName, workflowId, workflowType }) => keyFor(workflowId),
});
  • The plugin calls it for traces and spans that have no key of their own, and uses the result as their tracingApiKey. Returning undefined keeps the exporter's default key.
  • Activities: the Activity inbound interceptor calls it with the header's trace ID and name and the Activity's Workflow ID and type. withRestoredAgentsTraceContext sets the key on the rebuilt Trace, so spans made in the Activity (the model's generation span, a tool body's spans, temporal:executeActivity) take it.
  • Stateful MCP: the session Activity passes the main Worker's resolver to the interceptor of the dedicated per-run Worker it starts, so listTools() and callTool() spans take the key too. The resolver is held Worker-side, keyed by the Activity's context, and never goes into Activity arguments.
  • Nexus handlers: the same, with the trace ID and name. A handler does not know the calling Workflow, so the Workflow fields are not set.
  • Workflow spans: the tracing sink, which re-emits Workflow traces and spans to the Worker's processors, calls it with the trace ID, the Workflow ID and type, and the trace name for trace records. This covers a Workflow started inside a keyed client trace. Spans that already carry a key keep it: a run's tracing: { apiKey } stays on its Workflow spans when the run starts its own trace. Inside a client's trace the run joins that trace and the Agents SDK ignores the run's key, so those spans get the resolved key.
  • The resolver is synchronous, so the sink does not reorder span events. A throw fails the Activity or Nexus operation, and the sink's existing handler drops the Workflow item with a warning.
  • New exported types TracingApiKeyResolver and TracingApiKeyContext, and a "Tracing keys per run" part in the README's "Tracing" section.

API change: additive. Without tracingApiKey the plugin behaves as before.

Replay and rollback: no Workflow-side behavior changes. withRestoredAgentsTraceContext is shared with Workflow code, but Workflow code never passes the new argument; the resolver runs only in the Activity and Nexus interceptors (including the stateful MCP dedicated Worker's) and in the sink, on the Worker. The trace header, Activity inputs and History don't change, so histories replay on either version, and the previous release is a safe rollback.

Why?

See #214. The __openai_span header holds only the trace name and IDs, because it is stored in Event History and must not carry a credential. The receiving side rebuilt the trace without a key, so those spans went out with the Worker's default key: the Activity spans of a run with its own key, including the generation span that holds the prompt, and every Workflow and Activity span of a Workflow started inside a keyed client trace. At OpenAI's ingest the key selects the account, so those spans landed in another account without any warning.

Checklist

  1. Closes [Bug] openai-agents (TypeScript): a run's or client trace's tracing key does not reach spans made past a Temporal boundary #214

  2. How was this tested:

    • New test tracingApiKey gives spans that cross a Temporal boundary the key the Worker resolves in src/__tests__/test-openai-agents.ts. It runs one Workflow with its own key, the same Workflow with its own key inside a keyed client trace, and a stateful MCP Workflow inside that trace, with a model and an MCP server that open spans. It checks the key of the Activity span, the run's own Workflow span (outside a client trace), the client trace's Workflow span (the run's key is ignored there), the stateful MCP connect span on the main Worker and the listTools/callTool spans on the dedicated Worker, and the resolver's context.
    • New test Nexus handler spans get the key from tracingApiKey in src/__tests__/trace-interceptor.test.ts; no existing test covers the Worker's trace interceptors.
    • The option is new, so the tests can't compile against main. With the option kept but the two lines that apply the key turned off, both tests fail: Activity span of a run with its own key gets undefined instead of 'sk-resolved-run', and the Nexus span gets undefined. With only the sink line turned off, the Workflow span in a keyed client trace gets undefined instead of 'sk-resolved-trace'. With only the stateful MCP forwarding turned off, the dedicated Worker's listTools span gets undefined instead of 'sk-resolved-mcp'. With the change all pass.
    • Locally, in Docker (Node 24.22.0, pnpm 10.27.0, Linux arm64): make sync, make lint (8 existing warnings, none in files this PR touches), make test (209 tests), make build, node ../_shared/smoke.mjs and scripts/ci/check_conventions.py pass.
    • [Bug] openai-agents (TypeScript): a run's or client trace's tracing key does not reach spans made past a Temporal boundary #214's reproduction exits 1 on npm 1.24.0, on main at c664a34 and on this branch without the option. With resolve (the option set) it exits 1 on 1.24.0 and main, which ignore it, and 0 on this branch.
    • With the framework's own OpenAITracingExporter posting to a loopback ingest, a run with its own key and a keyed client trace both export every item, including the generation span with the prompt, with their key when the option is set. Without it, the output matches main. (This was run before the stateful MCP fix; the stateful MCP spans are covered by the test above.)
    • Replay: histories recorded on main and on this branch (with the option set) replay on both with Worker.runReplayHistories, 2 of 2 each. A control Workflow with one extra agent run fails with [TMPRL1100].

    CI covers the other Node versions and platforms.

  3. Any docs updates needed?
    The README part, included here.

Related: #185 reports the same mechanism in the Python plugin. Its draft fix #186 refuses a trace's own key at Temporal boundaries instead (breaking, gated), and names a Worker-side key resolver as a possible separate, additive feature. This PR takes the resolver route for TypeScript: a refusal would break the Workflow-side part of a run's own key, which works today.

Not covered:

  • Applications that don't set the option keep today's behavior.
  • The resolver context has no trace metadata or group ID, because the header doesn't carry them.
  • A run's tracing: { apiKey } inside a trace a Client started is ignored by the Agents SDK; the resolver decides those spans' key.

Open questions:

Draft while these questions are open.

…s plugin

A trace's export key is not carried across Temporal boundaries, because the
plugin's trace header is stored in Workflow history. So spans made in
Activities and Nexus handlers, and the Workflow spans of a trace started by a
Client with tracingApiKey, went out with the exporter's default key.

The new OpenAIAgentsPlugin option tracingApiKey resolves a key on the Worker
for those spans. The Activity and Nexus interceptors give the restored trace
the key, and the tracing sink gives it to Workflow traces and spans that have
no key of their own. A key a run sets with tracing: { apiKey } still applies
to its Workflow spans. Without the option nothing changes.
@campoy
campoy marked this pull request as ready for review October 29, 2026 19:26
@campoy
campoy requested a review from a team as a code owner October 29, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] openai-agents (TypeScript): a run's or client trace's tracing key does not reach spans made past a Temporal boundary

2 participants