Skip to content

OTel import: show which agent made each call (invoke_agent spans) - #1246

Open
Beehtrix wants to merge 14 commits into
Cygnux-Labs:mainfrom
Beehtrix:cursor/otel-invoke-agent-1798
Open

Beehtrix wants to merge 14 commits into
Cygnux-Labs:mainfrom
Beehtrix:cursor/otel-invoke-agent-1798

Conversation

@Beehtrix

@Beehtrix Beehtrix commented Dec 1, 2026 •

Copy link
Copy Markdown

What and why

Fixes #1240.

After maintainer approval (2026-12-01) of the records sketch: one run per trace, no placement at write time, agent tree built when records are read.

The v2 signer's OTLP import (POST /v1/traces) now records what each span says about its parent instead of opening agent runs or placing spans at write time:

  • Calls — first tool.call / model.exchange record carries ext.otel.parent (parent span id; omitted for a root call).
  • Agent spans — ext record {name: otel, kind: agent, agent, agent_id?, operation, parent_span_id} when the span arrives (span_id dedupe unchanged).
  • Framework spans — ext link record only when a recorded span already names that span as parent (not every unrecognised span: order-proof but adds noise to every web trace).
  • Reading — tracekit.otel_agents.build(records) walks parents (≤64 hops, memoised); calls whose chain ends at a missing span show as unseen until the agent span arrives; nesting capped at 32. tracekit view lists each agent and its call count from the same read path.
  • Verifier — ext.otel in EXT_CHECKS as informational otel import (Python + TypeScript, VERSIONS 1.10.0 row). Bundles still need verifier_min_version: 1.4.0 (the ext slot). No not interpreted on every import.
  • Removed — otel.unplaced, write-time parent_run/session for imports, MAX_AGENT_DEPTH placement, parent-walk on the writer thread.

Maintainer answers implemented:

  1. otel → EXT_CHECKS informational (not a per-bundle warning).
  2. Link spans only when something recorded hangs under them; rare early framework span → calls unseen (limits (1.9) row).
  3. verifier_min_version: 1.4.0 for imported runs kept.

AI assistance: written by an AI coding agent for @Beehtrix.

Verify and view evidence

Nested agent trace (orchestrator + researcher + fact-checker + writer + framework “running tools” span between researcher and tool — same shape as the Pydantic AI repro: orchestrator, two sub-agents, 7 tool/model spans):

pytest tests/test_signer_otel.py::TestAgents::test_the_bundle_of_a_trace_with_agents_verifies_and_the_verifier_reads_ext_otel — export --v2, then verify:

  • exit 0, no not interpreted warnings
  • [pass] otel import: N record(s) carry ext.otel (4 agent, 1 link, … call parent): the application's report of its span tree, not checked for truth
  • manifest verifier_min_version: 1.4.0, verifier_version: 1.10.0

View — tests/test_view_ui.py::test_an_imported_runs_review_lists_the_agents_its_application_reported_and_the_calls_in_none exercises the review otel panel (otelRows): e.g. <orchestrator> 5 calls, researcher 3 calls, writer 3 calls.

Full suite: tests/test_signer_otel.py 31 passed; tests/test_otel_agents.py; ext interpretation parity (test_an_ext_interpretation_adds_only_its_own_lines).

What changed vs the sketch (/tmp/sketch-1246.md)

Sketch This branch
ext.otel + informational verifier (maintainer preferred over not interpreted) VERSIONS 1.10.0 + otel import line (Python/TS)
Link only when a parent is already named; unseen for early framework span Same (signer/otel.py links(), limits (1.9))
verifier_min_version 1.4.0 Unchanged (needs() — ext slot only bumps critical)
otel.unplaced gone Removed in rewrite
Pydantic AI SimpleSpanProcessor + BatchSpanProcessor repro Covered by signer otel tests + prior repro comment on PR; nested fixture mirrors 11-call tree

Test results (this push)

  • pytest tests/test_signer_otel.py tests/test_otel_agents.py -q — pass
  • pytest tests/test_bundle_v2.py -k ext_interpretation -q — pass
  • pytest tests/test_ts_verifier.py -k versions -q — pass (versions.json matches bundle_v2.VERSIONS)

Checklist

  • regression tests (otel agents, signer otel, view UI otel rows, verify otel import)
  • docs: docs/otel.md, docs/limits.md, docs/format-v2.md
  • commits SSH-signed (gpgsig); GitHub shows unknown_key until the signing key is on the Beehtrix account (same for prior commits on this branch)

Security invariants

  • agent never holds signing key / seq
  • otel tree is T3 application report; verifier states that explicitly and does not treat it as proof
  • gaps still signed; malformed ext.otel warns on verify
  • no verification code in bundles

Copilot AI lite review requested due to automatic review settings December 1, 2026 05:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@LoopGlitch26

Copy link
Copy Markdown
Member

@Beehtrix Thanks. The careful parts hold:

  • another identity's import of a run is refused;
  • parent_run and session never left the trace across 25 fuzzed seeds of splits, shuffles, restarts and re-sends;
  • cyclic and self-parenting spans terminate;
  • a trace with no agent span gives byte-identical records to main;
  • sub-agent runs export and verify.

But with real exporters the tree comes out wrong, silently. Exporters send a span when it ends, so an agent's calls reach the signer before the agent span does. With SimpleSpanProcessor (one span per request) all 5 sub-agent calls of the PR's nested() trace landed in the trace's run with no note, and with BatchSpanProcessor in short windows 4 of 5 did. The agent runs exist but hold no calls, and the trace's run is named after the service. The demo finished every span inside one export, so it didn't show this. A person would see the orchestrator holding everyone's calls, which is worse than main's flat run, since it makes a claim about who did what.

The root cause is placing each call at write time, which depends on arrival order. A design that doesn't:

  • keep every span of a trace in the trace's run, as main does;
  • record each call's span id and parent span id, plus each agent span (name, id, parent);
  • let the viewer build the agent tree when it reads the run, walking parents iteratively with a depth cap.

Order stops mattering, and four other problems go with it:

  • a re-sent span is written a second time once its agent run is final (the siblings dedupe reads spans, which final_run drops);
  • the parent walk costs spans x depth on the writer thread (10,000 spans in a chain: 7.1 s against main's 1.25 s, with other RPCs waiting 6.7 s);
  • agent runs slip past the open_runs quota (one request made 10,000 runs);
  • nesting is unbounded (the viewer's treeOrder overflows the stack at 5,000 levels).

This changes #1240's "each its own run"; the goal is showing who made each call, and I'll update the issue to match. Could you sketch the records first (fields, and where the parent ids go: an ext field, or fields the v2 format already has) before rewriting? Design first saves you a round.

Smaller, for the rewrite:

  • our own verifiers print WARN not interpreted for otel.unplaced: add it to EXT_CHECKS as informational, or drop the note;
  • the replay change at pipeline.py:873 has no test (switching it off fails nothing);
  • MAX_AGENT_DEPTH's comment says deeper agents go under the trace's run, but the cap only breaks cycles;
  • format-v2.md §3 still says parent_run is the run whose token the caller presents;
  • the limits row should be marked (1.9), not (1.4).

@Beehtrix

Beehtrix commented Dec 1, 2026

Copy link
Copy Markdown
Author

Thanks for the careful review. I reproduced the wrong tree before designing anything: Pydantic AI (an orchestrator and two sub-agents, offline models, 11 calls) through the OpenTelemetry SDK's SimpleSpanProcessor, and again through BatchSpanProcessor with 3-span batches, into a real tracekit signer serve at this PR's head. In both, all 6 sub-agent calls land in the orchestrator's run, and the two agent runs hold nothing but an otel.unplaced note. Agreed on the cause. Here is the records sketch; I prototyped it first, so the shapes below are from real records, not a guess.

Shape. One run per trace, as on main. Nothing is placed at write time and no agent run is registered (no parent_run or session for imports). The signer records what each span says about its parent, and the tree is built when the records are read. All records stay source: import, tier T3, with the span in the existing envelope span_id; the v2 schema does not change.

Records

  1. A call's first record (tool.call, model.exchange) carries ext: {"otel": {"parent": "<parent span id>"}}, left out for a root span. v2 has no closed field for a parent span (run.registered.parent_run is per run), so it goes in ext.
  2. An agent span is one ext record {name: "otel", kind: "agent", agent, agent_id?, operation: "invoke_agent"|"create_agent", parent_span_id}, written whenever the span arrives. The dedupe key stays span_id:type, so a re-sent span writes nothing.
  3. A span the signer records no call of is one ext record {name: "otel", kind: "link", parent_span_id}, but only when a recorded span hangs under it (the PR's own nested() has one, a "running tools" span between an agent and its tool). The run keeps the set of parent ids its records name (rebuilt on replay, kept in snapshots), so the link is written even when the framework span arrives a request after its calls.

Reading. tracekit/otel_agents.py build(records) gives each call its nearest agent by walking parents iteratively (at most 64 hops, memoised). A call whose chain ends at a span never recorded is unseen, shown under the trace and said so; when the agent span arrives, the same read puts it in place, with no record changed. A create_agent span counts only when a call or agent is under it. Loops are cut, and nesting is shown to depth 32. The OTLP export (tracekit export --otel) reads it in the prototype and keeps the application's span ids and parents, so a collector sees the same tree the app sent; the viewer's run list will read it the same way.

Before and after (same run, SimpleSpanProcessor, one span per request in end order)

before   run otel:2ce6c1f  agent=orchestrator    11 calls: its own 5 and the sub-agents' 6
           run ..researcher  only a note        run ..writer  only a note
after    run otel:09d3735  20 records, 3 of them agent records
           agent orchestrator (5 calls)
             agent researcher (3 calls)    agent writer (3 calls)

Batch windows give the same "after". tracekit verify passes on the exported bundle.

Your four side problems

  • Re-sent span after its run is final: nothing to place, and a final run refuses the request as it does today.
  • Parent walk on the writer thread: gone. A chain of 10,000 nested tool spans in one request wrote in 4.7 s here, against 6.2 s on main and 35 s on this PR (one run each, same VM).
  • open_runs: one run per trace, whatever the number of agent spans (10,000 nested agent spans: 1 run, 3.8 s).
  • Nesting: the reader caps it at 32.

Costs, so you can veto

  • Every imported call now carries an ext, so a bundle of an imported run needs a verifier from 1.4.0 (its manifest says verifier_min_version: 1.4.0; main's said 1.0.0 for such a run). Older verifiers report UNVERIFIABLE, not FAILED.
  • Verifiers print not interpreted for ext.otel (one line per bundle). I would rather add otel to EXT_CHECKS as informational (Python and TypeScript, one new versions row) than leave the warning on every import; say if you prefer that, or the warning left alone.
  • A link is written only for a span that something recorded hangs under, so a framework span that reaches the signer before every span under it is not linked, and its calls show unseen. A span ends after what is under it, so exporters send it later; recording every unrecognised span instead is order-proof but adds records to every web trace. Tell me if you want that.
  • otel.unplaced, MAX_AGENT_DEPTH, the agent-run replay change in pipeline.py and the format-v2.md §3 parent_run edit all go away (the replay keeps only the set of parent ids); the limits row will say (1.9).

Plan if this works for you: tests for calls in batch 1 with the agent span in batch 2, the same across a restart, shuffled and one-span-per-request orders, a framework span between agent and call, loops, depth, and the export, then tracekit view listing each agent with its calls. I will build it on a separate branch of my fork and will not touch this PR until you answer.

@LoopGlitch26

Copy link
Copy Markdown
Member

@Beehtrix Thanks, this is the right design, and reproducing the wrong tree first was the right start. Please go ahead.

On your three questions:

  1. otel in EXT_CHECKS as informational (Python and TypeScript, one new versions row): yes. A not interpreted warning on every import teaches people to ignore warnings.
  2. Link only spans that something recorded hangs under: yes, as you propose. A span ends after what is under it, so the usual order links it. For the rare order where the framework span arrives first, show its calls as unseen and say so in a limits row rather than add records to every web trace.
  3. verifier_min_version: 1.4.0 for an imported run: fine. An older verifier says UNVERIFIABLE, which is true.

Your test plan covers what we'd ask. Please also cover tracekit verify on an exported bundle with agent records, and show tracekit view listing each agent with its calls in the PR, on the Pydantic AI run you reproduced. Whether you update this PR or open a new one in its place is up to you; if new, close this one with a link.

@Beehtrix

Beehtrix commented Dec 1, 2026

Copy link
Copy Markdown
Author

Thanks — proceeding on this PR with your three answers (otel informational, link only recorded parent spans, verifier_min_version 1.4.0). Will include tracekit verify on an exported bundle with agent records and a tracekit view listing for the Pydantic AI repro.

Copilot AI lite review requested due to automatic review settings December 1, 2026 21:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Give each OpenTelemetry agent span (invoke_agent, or create_agent with calls
under it) its own T3 import run, linked to the enclosing agent's run by
parent_run. Model and tool spans are recorded in the run of their nearest
enclosing agent span, whatever order they arrive in. A span whose agent has
not arrived is recorded in the trace's run after a signed ext note
otel.unplaced. A trace with no agent span is one run, as before.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
Describe agent runs, the application-reported tree, span placement and the
split-batch limit in otel.md and limits.md, and the otel.unplaced ext note in
format-v2.md.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
…retried orphans

Review findings on the agent-run import:
- process a request depth first (each agent span, then what is in its run), so
  parallel agents are not all open at once under open_runs
- cap the registration recursion for loops of agent spans
- a run.registered of an agent span carries its span_id, so a nested agent that
  arrives after its root in an earlier batch is placed without a false note
- a retried span is also looked for in the trace's agent runs
- an exported sub-agent names its parent run's span as parentSpanId

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
…te condition

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
Greptile review findings: the retried-span lookup scanned every run per trace, and
duplicate agent spans in one request multiplied their children in the tree
walk. The agent runs are indexed by trace on first use, and a request's
duplicate spans are collapsed before placement.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
… the first is

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
…t run by run

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
The signer will record parent span ids and agent spans instead of placing
calls into agent runs at write time. The tree is assembled when the
records are read: each call's nearest agent is found by walking parents
iteratively (hop and depth caps), and create_agent spans count only when
something hangs under them.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
Exporters send a span when it ends, so placing calls into agent runs at
write time put every call in the root agent's run with real processors.
Keep one run per trace, write each call's parent span id and each agent
span as non-critical ext, and record a skipped framework span as a link
when a recorded span hangs under it (the run's refs set, rebuilt on
replay). The tree is built when the records are read.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
review() adds an otel summary of the agent tree, and the run list renders
those agents as nested rows under the run instead of as separate runs.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
Replace the write-time placement / otel.unplaced design with parent and
agent records in the trace's run, the read-time tree, and a (1.9) limits
row for a chain that ends at a span the signer never received.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
Add VERSIONS 1.10.0 and EXT_CHECKS for otel in Python and TypeScript so
imported OTLP bundles no longer warn not interpreted on every call.
Update limits and format-v2; nested agent export still needs verifier 1.4.0.
@Beehtrix
Beehtrix force-pushed the cursor/otel-invoke-agent-1798 branch from 425b4c9 to b15c97f Compare December 1, 2026 21:41
Copilot AI lite review requested due to automatic review settings December 1, 2026 21:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@LoopGlitch26

Copy link
Copy Markdown
Member

@Beehtrix Thanks, the design holds where it matters most. We ran 24 real tracekit signer serve runs in a container:

  • A real Pydantic AI app (real OTel SDK, protobuf exporter) read the same tree in all 15 orders we tried: live Simple and Batch processors, one span per request, shuffled, agent spans in a later request, a SIGTERM or SIGKILL restart between batches, and under an HTTP root span.
  • nested() matched in every order except the documented one, where the framework span arrives before its call.
  • Nothing from the first version is left, and the writer stayed bounded: 100,000 sibling spans, with the slowest request at 0.15 s.

Before merge:

  1. CI will be red on three tests.
    • test_view_ui ApproveThenRetry::test_a_runs_held_count_is_its_reviews_held_calls and FinalQA::test_a_runs_list_says_1_gap_and_1_call: renderRuns now calls otelRows, which their page_js lists don't name.
    • test_ts_verifier Copies::test_the_verifier_is_the_package_version: Python reads 1.10.0 and TypeScript READS is still 1.9.0.
  2. The version: 1.9.0 hasn't shipped yet, so please put otel in the existing 1.9.0 row (Python and versions.json) and say 1.9 in the docs. That also fixes the READS test.
  3. A normal web app's import gets a WARN on a valid bundle. Under a POST /chat root span, the root becomes a link record with parent_span_id: null, and both verifiers require a span id (v2.py:365, index.ts:848; format-v2.md says required). Please allow a null parent for a link, or don't write a link for a root span. Add the Pydantic AI-under-HTTP-root case as a test with no warning in either verifier.
  4. export --otel doesn't keep the app's parents, which docs/otel.md promises. The sub-agents' spans move from their execute_tool parents to the orchestrator, the HTTP root is dropped, and with the writer's agent span missing, execute_tool write is exported under the orchestrator (signer/otel.py ~138-164). A call must never be exported under an agent its spans don't name. Please keep the real parents, or say plainly that it exports only the agent tree, and never place an unseen call under another agent. Test with nested() minus the writer's agent span.
  5. One lone surrogate fails the whole request. \ud800 in an agent span's gen_ai.agent.name raises CanonicalizationError in the writer, the connection closes with no response, and nothing in the request is written. On main the same request is written. Please clean names of lone surrogates before they reach the writer, with a test on an agent span and on a tool span.
  6. A forged span can take a real agent's calls. An agent span sent first with the researcher's span id and the name "impostor" makes the real span a silent "duplicate": the exporter is told nothing, and the calls show under "impostor". When a span id arrives again with a different kind or agent name, please don't drop it quietly. Record the conflict, or reject it with a message the exporter sees, and test the impostor case.
  7. build() depends on record order for chains over 64 hops. 10,000 tool spans sent deepest-first leave 9,500 lost, while top-first places all 10,000 (above() memoises partial walks, otel_agents.py ~74-92). Please make the read tree order-free, with a test of a 100-call chain sent top-down and bottom-up. The agent-depth loop's s in trail on a list is O(n²): a set fixes it.
  8. The verifiers disagree on name length. TypeScript counts agent.length in UTF-16 units (index.ts:839) and Python counts code points, so 100 emoji pass in Python and WARN in TypeScript. Please add a test that both verifiers read ext.otel alike, on a real import and a malformed one. Today a Python verifier that accepts any ext.otel, or a TypeScript one without checkOtel, passes every test.

Smaller:

  • In the viewer, a run with no agent span says "N calls whose agent's span has not arrived". For an app with no agents that's not true: "whose parent span was not received" would be.
  • An otel: run an importer registers over RPC has no refs, so a later OTLP import into it raises KeyError 'refs' (service.py ~3684).
  • b15c97f has no Signed-off-by, so DCO will fail.
  • Please show tracekit view listing each agent with its calls on the real Pydantic AI run in the PR, rather than a unit test.

This branch has not been deployed

No deployments
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.

OpenTelemetry import: show which agent made each call (invoke_agent spans)

3 participants