Skip to content

feat(agent): accurate agent_code detection + per-channel agentId for stats#467

Merged
PeterGuy326 merged 1 commit into
mainfrom
feat/agent-channel-detect
Jun 16, 2026
Merged

feat(agent): accurate agent_code detection + per-channel agentId for stats#467
PeterGuy326 merged 1 commit into
mainfrom
feat/agent-channel-detect

Conversation

@PeterGuy326

@PeterGuy326 PeterGuy326 commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

背景

网关侧反馈 agent_code 在日志里没有上报。根因:CLI 仅在 host 注入 DINGTALK_DWS_AGENTCODE(约 99.98% 为空)时才发该字段。本 PR 在 CLI 侧让 dws 准确识别市面各 agent host + 每实例可统计,无需网关分配 agentId。

agent_code 探测(覆盖 agent 生态)

置信度阶梯,每条签名均在真机/官方文档实测,不臆测;识别不了一律 custom:

  • T0 显式声明 DINGTALK_DWS_AGENTCODE(任意 agent 通用,推荐)
  • T1 各 agent 自动注入的 env 签名:claudecode(CLAUDECODE)、codex(CODEX_SANDBOX,防伪过滤)、openclaw(OPENCLAW_BUNDLE_ROOT)、hermes(HERMES_HOME)
  • T2 VSCODE_BRAND 值 —— 一条规则覆盖整个 VS Code fork 家族(Qoder/Cursor/VSCode/Windsurf/Trae/Kiro/未来 fork)
  • T3 macOS __CFBundleIdentifier 映射(qoder/cursor/vscode/workbuddy)
  • T4 custom 兜底

CLI 侧上报的 header(本 PR 已实现并验证)

dws 每次 MCP 请求会带上:

header 含义
x-dingtalk-dws-agent-code agent 渠道 claudecode / qoder / custom
x-dws-agent-instance-id 机器×渠道实例 id dwsa_c8oildTg5d0O
x-dws-agent-id 机器级 id(老字段,兼容) 504ddd36-...
X-Cli-Version dws 版本,区分新老客户端 1.0.xx

🔧 待网关侧配合(@李晓军,非本 PR,但数据落地必须做)

CLI 把上面 header 发到 mcp-gw 是必要但不充分的;网关侧不接,这些 header 会被 strip、数仓拿不到。需要两步:

  1. 加进透传白名单:把 x-dingtalk-dws-agent-codex-dws-agent-instance-idx-dws-agent-idX-Cli-Version 加入网关 a2a/routes.py_build_upstream_headers 放行名单(否则未知 header 被丢弃——与 list-direct 踩过同一个坑)。
  2. 落日志字段 + 投数仓:在流控日志 / 执行结果日志里新增 agent_codeagent_instance_idcli_version 字段,随现有 SLS → MaxCompute 投递一起进数仓。数仓侧即可 group by agent_code 出渠道分布、按 agent_instance_id 拆实例、用 cli_version 区分新老(老版本无 agent_code,单独成桶不报错)。

⚠️ 可信边界(重要,勿误用)

agent_code 和两个 id(x-dws-agent-id / x-dws-agent-instance-id)都是客户端自报、可伪造(改 ~/.dws/identity.json 或改 header 即可),dwsa_ 前缀不代表安全 ID。

  • 仅用于统计 / 观测(无人有动机谎报,聚合可信,本 PR 即此用途)。
  • 禁止用于认证 / 授权 / 限流 / 计费 / 吊销 —— 那需网关签发、绑定 clientId/PAT/OAuth、可校验可吊销的权威 id,真正防伪锚点是已有鉴权 token。另一条更重的路径,本 PR 不含。

agentId / 兼容性

identity.json v2(machineId + 按 agent_code 派生 dwsa_<base62(sha256(machineId|agent_code))>),v1 透明迁移;x-dws-agent-id 保持机器级(与 v1 连续),新增字段为增量,老版本缺字段=老/unknown 不报错。

验证

go build ./... / go vet 通过;auth + app 全包回归通过;探测各阶梯单测(precedence、未知 bundle→custom、噪声不判定);真机签名矩阵端到端验证(claudecode/codex/openclaw/hermes/qoder/cursor/workbuddy/vscode/windsurf/trae 命中,未知/噪声→custom)。完整对接说明见 docs/agent-code.md

@PeterGuy326 PeterGuy326 force-pushed the feat/agent-channel-detect branch from 0df80d9 to d9c9a13 Compare June 16, 2026 03:50
…stats

Tag each MCP request with which agent host is driving dws (agent_code) and a
per-(machine × agent_code) instance id, so usage can be sliced by channel and
instance in the data warehouse. Root cause it fixes: agent_code was only sent
when the host injected DINGTALK_DWS_AGENTCODE (~99.98% empty), so the gateway
logged none.

Detection ladder (every signature observed on a real host / official docs, not
guessed; unknown -> custom):
  T0 explicit DINGTALK_DWS_AGENTCODE
  T1 verified env signatures: claudecode (CLAUDECODE), codex (CODEX_SANDBOX),
     openclaw (OPENCLAW_BUNDLE_ROOT), hermes (HERMES_HOME)
  T2 VSCODE_BRAND value (covers the whole VS Code fork family)
  T3 macOS __CFBundleIdentifier map (qoder/cursor/vscode/workbuddy)
  T4 custom fallback

identity.json v2 (machineId + per-agent_code agents map), deterministic
dwsa_<base62> derivation, transparent v1 migration. Backward-compatible wiring:
x-dws-agent-id stays machine-level; new x-dws-agent-instance-id carries the
per-channel id; X-Cli-Version emitted so old/new clients are distinguishable.

Trust boundary (docs/agent-code.md): agent_code and the ids are self-reported
and spoofable — fit for statistics ONLY, never for auth/limit/billing.

Includes unit tests for every tier and the integration doc.
@PeterGuy326 PeterGuy326 force-pushed the feat/agent-channel-detect branch from 994bfe5 to 0aed349 Compare June 16, 2026 09:32
@PeterGuy326 PeterGuy326 merged commit ece91bf into main Jun 16, 2026
6 checks passed
@PeterGuy326 PeterGuy326 deleted the feat/agent-channel-detect branch June 16, 2026 09:58
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.

2 participants