OTel: Subagent chat spans after write_agent lose gen_ai.usage.* attributes and report parent model name

未关闭
#5,622 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python

调研方向

Start by tracing how OpenTelemetry spans are created for background subagent turns and how write_agent resumes or steers them; the issue does not name relevant files or tests. Compare span context and model selection with usage-record correlation before and after write_agent. Done means resumed subagent chat spans retain their own invoke_agent parent, model name, and token usage attributes across follow-up turns.

由索引模型根据 Issue 内容生成。

描述

area:agents
Describe the bug

When a background subagent is launched via task(..., mode: "background", model: "<subagent-model>"), its initial OpenTelemetry spans export normally under an invoke_agent <agent_type> child span with the subagent's gen_ai.request.model and gen_ai.usage.{input_tokens,output_tokens} attributes populated.

However, as soon as the parent session sends a message to that background subagent via write_agent (either waking an idle subagent for a multi-turn follow-up or steering a running subagent), all subsequent OTel chat spans produced by the subagent exhibit two telemetry regressions:

  1. Wrong model and parent span attribution: Once the initial invoke_agent <agent_type> span closes, no new invoke_agent span is opened for the write_agent turn. Subsequent subagent chat and execute_tool spans attach directly to the parent session's span, and the subagent's chat spans are named chat <parent-model> with gen_ai.request.model = "<parent-model>" instead of <subagent-model> (even though gen_ai.input.messages / gen_ai.output.messages on those spans belong to the subagent).
  2. Missing token usage attributes (gen_ai.usage.*): Although events.jsonl still writes complete session.usage_record entries for the subagent (usage.accounting.sourceSessionId = "<subagent-session-id>", usage.model = "<subagent-model>", inputTokens, outputTokens, cacheReadTokens), the OTel exporter fails to attach usage metrics to those post-write_agent chat spans — gen_ai.usage.input_tokens and gen_ai.usage.output_tokens are completely absent on every subsequent subagent chat span. Downstream OTel collectors / Prometheus spanmetrics pipelines therefore drop 100% of subagent token consumption after write_agent.
Affected version

1.0.115-7 (GitHub Copilot App / copilot-agent)

Steps to reproduce the behavior
  1. Enable OpenTelemetry trace export (OTEL_EXPORTER_OTLP_ENDPOINT / OTLP receiver such as Grafana Alloy + Tempo).
  2. Start a parent session running <parent-model> (e.g. gemini-4-neon).
  3. Have the parent launch a background subagent running a different <subagent-model> (e.g. task(agent_type: "general-purpose", mode: "background", model: "gemini-4-hydrogen", ...)).
  4. Observe in Tempo that initial subagent LLM calls appear under invoke_agent general-purpose as chat gemini-4-hydrogen with gen_ai.request.model = "gemini-4-hydrogen" and gen_ai.usage.input_tokens / gen_ai.usage.output_tokens populated.
  5. Have the parent send a follow-up or steering message via write_agent(agent_id: "<subagent-id>", message: "...") (reproduces both when the subagent is idle between turns and when running).
  6. Inspect the newly exported OTel chat spans for the subagent's subsequent turns in Tempo:
    • Span name is chat gemini-4-neon (gen_ai.request.model = "gemini-4-neon") instead of gemini-4-hydrogen.
    • gen_ai.usage.input_tokens and gen_ai.usage.output_tokens are missing (null), while events.jsonl and local assistant_usage_events still record the subagent's gemini-4-hydrogen token usage normally.
Expected behavior

After write_agent wakes or steers a background subagent:

  • Subsequent subagent turns should be scoped under a subagent invoke_agent span (preserving trace hierarchy) and report the subagent's actual gen_ai.request.model (chat <subagent-model>).
  • session.usage_record events for sourceSessionId = "<subagent-session-id>" should continue binding to the corresponding subagent chat spans so gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, and cache token attributes remain populated across multi-turn write_agent interactions.
Additional context
  • Surface: GitHub Copilot App (service.name = "github-copilot-app", otel.scope.name = "github.copilot", otel.scope.version = "1.0.115-7")
  • OS / Arch: macOS (ARM64)
  • Root-cause clue from events.jsonl vs Tempo: After write_agent, subagent assistant.turn_start events still reference the original parentToolCallId from the initial task call (whose invoke_agent span has already closed), while session.usage_record carries usage.accounting.sourceSessionId = "<subagent-native-session-id>" and assistant.message carries apiCallId. The OTel span tracker appears to fall back to the parent session's active span/model context and misses the usage-record correlation for subagent turns resumed via write_agent.
主要语言
Python
星标
11.5k
派生
2k
平均合并
6 小时 34 分钟
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

github/copilot-cli 的其他 Issue

查看 github/copilot-cli 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。