Skip to content

test(sdk): cover agent name cleanup for spans after an @agent - #4777

Open
doronkopit5 wants to merge 1 commit into
mainfrom
test/sdk-agent-name-scope-4378
Open

doronkopit5 wants to merge 1 commit into
mainfrom
test/sdk-agent-name-scope-4378

Conversation

@doronkopit5

@doronkopit5 doronkopit5 commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

What

Test-only PR. It closes #4378, which #4405 already fixed (released in traceloop-sdk 0.62.2): _setup_span now keeps the token returned by set_agent_name(), and _detach_tokens detaches it on every exit path.

These tests pin the reporter's cases so the leak can't come back. All of them are in packages/traceloop-sdk/tests/test_agent_name_scope.py:

  • The issue's reproduction, sync and async: a sibling @task after an @agent inside a @workflow keeps traceloop.workflow.name and gets no gen_ai.agent.name. The agent's own child span still carries the agent name.
  • Bare agent: a standalone @task after a completed @agent gets no agent name, and the context agent_name is cleared.
  • Agent raises, sync and async: the user's exception propagates unchanged, the agent span is ERROR, and the agent name is detached before the next span.
  • Generator agent closed early: close() after one item ends the agent span and clears the context.
  • The existing generator exhaustion and aclosing tests now also assert that the agent span has ended.

No production code changes. All 18 tests in the file pass on main, and ruff and mypy are clean.

Out of scope: the refactor proposed in the issue (one attach that carries the agent name in the span context). It isn't needed for the fix.

Closes #4378

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Added regression coverage for synchronous and asynchronous generator cleanup, including exhausted and explicitly closed generators.
    • Added checks that agent names do not leak into later tasks or spans, and that agent spans record errors when exceptions occur.

Adds regression tests for #4378, which #4405 fixed: a @task that runs
after an @agent (inside a @workflow or standalone, sync and async) gets
no gen_ai.agent.name, and the agent name is detached when the agent
raises or a generator agent is closed early. The generator tests also
assert the agent span has ended.

Closes #4378

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

This change adds regression tests for agent-name context cleanup after generator exits, completed agents, and exception exits. The tests also check workflow-name retention and agent span completion or error status.

Changes: Agent Name Scope Tests

Layer / File(s) Summary
Generator cleanup
packages/traceloop-sdk/tests/test_agent_name_scope.py
Tests check that synchronous and asynchronous generator spans finish and agent names do not leak after exhaustion or explicit closure.
Subsequent task scope
packages/traceloop-sdk/tests/test_agent_name_scope.py
Tests check that tasks after an agent do not inherit its name, while tasks inside a workflow retain the workflow name.
Exception cleanup
packages/traceloop-sdk/tests/test_agent_name_scope.py
Tests check that synchronous and asynchronous agent errors propagate, mark spans as errored, and clear the agent name.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other · Severity of issue fixed: Medium

Merge Risk: Minimal · up to a4e5a

This change only adds regression tests for agent-name context cleanup and does not alter runtime behavior. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "test(sdk): cover agent name cleanup for spans after an @agent" matches the main change. The PR adds regression tests in test_agent_name_scope.py for clearing the agent name after an @agent …
Linked Issues check Passed Issue #4378 requires that agent metadata apply only to the agent invocation and its descendants, that the prior context be restored on exit, and that regression tests cover a completed bare agent foll…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes only packages/traceloop-sdk/tests/test_agent_name_scope.py. No production code changes. All added tests target the leak described in #4378 (sibling a…
Full details: Title check

Explanation

The title "test(sdk): cover agent name cleanup for spans after an @agent" matches the main change. The PR adds regression tests in test_agent_name_scope.py for clearing the agent name after an @agent exits, covering sibling tasks, standalone tasks, exceptions, and generator closure. The PR makes no production-code changes, and the title states that it adds tests. The title is specific and concise.

Full details: Linked Issues check

Explanation

Issue #4378 requires that agent metadata apply only to the agent invocation and its descendants, that the prior context be restored on exit, and that regression tests cover a completed bare agent followed by a task and an agent followed by a sibling task in a workflow. At the reviewed head, _setup_span in packages/traceloop-sdk/traceloop/sdk/decorators/base.py keeps the name_token returned by set_agent_name(), and _detach_tokens detaches it on every exit path (sync and async, success, exception, and generator close). The PR adds tests in test_agent_name_scope.py for: the workflow sibling case (sync and async), the standalone task after a bare agent, agent exceptions (sync and async), and early generator close. It also adds end_time assertions to existing generator tests. These tests cover the required regression cases. The issue's proposed production change (adding the agent name to the span context before attach) is a suggested implementation, not an expected behavior. The existing token-based fix satisfies the expected behavior, so its absence in this PR does not fail the requirement. Test execution was not re-run; the pass result relies on the diff and the head source.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes only packages/traceloop-sdk/tests/test_agent_name_scope.py. No production code changes. All added tests target the leak described in #4378 (sibling and standalone tasks, exception exits, early generator close). The added end_time assertions in existing generator tests verify span termination on the same cleanup path. No unrelated changes were found.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bc209b41-b219-4182-821a-3ac02f4dfe79
Commits

Reviewing files that changed from the base of the PR and between b870d8a and a4e5ab0.

Files selected for processing: 1

  • packages/traceloop-sdk/tests/test_agent_name_scope.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review.

@doronkopit5
doronkopit5 marked this pull request as ready for review December 1, 2026 20:24
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: @agent leaks gen_ai.agent.name into subsequent spans

1 participant