fix: 修复 agent 执行中断场景下 span 状态与输出丢失的 4 类问题 - #300
Merged
Conversation
1. invocation 初始化异常时 span 缺失 runner 业务属性且显示未知:
- trace_runner() 的 invocation_context 改为 Optional,初始化异常时传入
None(此时 InvocationContext 尚未构造),依赖它的 runner.name 在 None
时跳过,其余业务属性照常写入
- runner 初始化失败分支统一调用 trace_runner() 上报错误状态
2. call_llm 模型调用错误被记录为成功:
- 模型异常被 retry 层转成 LlmResponse(error_code=...) 后,trace 层未识别
error_code,导致 span 误标成功;现补充检查 llm_response.error_code
3. 外部取消(asyncio.CancelledError)时根 invocation span 仍显示成功且缺 partial:
- CancelledError 继承 BaseException 而非 Exception,漏过 except Exception;
现单独捕获 CancelledError,标记 span 为错误
- 累积已流式输出的 partial 文本,防止取消时已生成内容丢失
4. GeneratorExit 时 agent_run 无部分输出:
- 生成器关闭(aclose/break)触发的 GeneratorExit 分支现会累积并回填
partial 流式文本到 agent_action 中,避免已输出内容丢失
Contributor
AI Code Review发现的问题
|
raychen911
approved these changes
Aug 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
修复 agent 执行中断/异常场景下链路追踪(span)状态错误与输出丢失的 4 类问题。
改动
invocation 初始化异常时 span 显示 unknown:
trace_runner()的invocation_context改为 Optional,初始化异常路径传入 None(此时 InvocationContext 尚未构造出来),依赖它的runner.name在 None 时跳过,其余业务属性照常写入;runner 初始化失败分支统一调用trace_runner()上报错误状态。call_llm 模型调用错误被记录为成功:模型异常被 retry 层转成
LlmResponse(error_code=...)后 trace 层未识别 error_code,span 误标成功;现补充检查llm_response.error_code,标记 span 为失败。外部取消(asyncio.CancelledError)时根 span 仍显示成功且缺 partial:
CancelledError继承BaseException而非Exception,漏过except Exception;现单独捕获 CancelledError 标记 span 为错误,并累积已流式的 partial 文本,防止取消时已生成内容丢失。GeneratorExit 时 agent_run 无部分输出:生成器关闭(aclose/break)触发的 GeneratorExit 分支现会累积并回填 partial 流式文本到 agent_action 中,避免已输出内容丢失。
影响
trpc_agent_sdk下 3 个文件:runners.py/telemetry/_trace.py/agents/_base_agent.py