Skip to content

feat(shell): show a turn's sub-agent spend in the child panel (v0.12.0) - #118

Merged
wangtai merged 2 commits into
mainfrom
develop
Nov 13, 2026
Merged

wangtai merged 2 commits into
mainfrom
develop

Conversation

@wangtai

@wangtai wangtai commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

问题

sidecar 从 R5 起就在 stream.done.usage 里报了子代理的花费(child*Tokens / children[] / unknownUsageChildren),但桌面端一个字段都没读。子代理看板每行只显示该子代理自己的 token,父代理的花费不在任何一行里——于是「这一轮花了多少、其中子代理占多少」在界面上无处可读。

改动

  • 新增 turn_usage SSE 事件:回合收尾时发一次、赶在 message_id 之前,与 turn_files 同槽同归档规则,因此会落到持久化消息上、刷新后水合。
  • 子代理看板下方多一行:本回合总计 + 子代理那一份及其占比。

两处容易读错的语义(都有测试钉住)

usage.totalTokens 是父代理自己的账单,childTotalTokens 是子代理那一部分,二者并列相加才是整轮成本——不是「总计与其中一部分」。第一版按后者实现,让每一轮都少报,写测试时对着 sidecar 源码才发现。

一个子代理都没报 token 的回合仍然渲染那一行,标 partial。否则偏小的数字会被读成完整账单,而一个确实派过活的回合看起来像没派。

刻意不做

不显示成本。costUsd 只估算父级模型,子代理可能跑在别的模型上,拿它当整轮成本会偏低。宁可只报 token,也不报一个错的钱。

验证

  • agent-shell/web 973 passed,agent-shell/ts 1646 passed,两边 tsc --noEmit 干净
  • 新增 turn-usage.test.ts(12)、TurnUsageLine.test.tsx(6)、router 两条(事件顺序 + 落库)
  • 版本 0.12.0:只动了 npm 包,PyPI 侧无接口变化(仍按 lockstep 同版本发布)

Made with Cursor

R6 follow-up. The sidecar has reported the children's share of a turn since
R5 (`child*Tokens`, `children[]`, `unknownUsageChildren`), but nothing in
the desktop UI read it. Each child row shows only THAT child's bill, so the
parent's own spend appeared nowhere and "what did this turn cost, and how
much of it was sub-agents" was unanswerable from the interface.

- `turn_usage` SSE event, emitted at turn end just before `message_id` —
  the same slot and archival rule as `turn_files`, so it lands on the
  persisted message and hydrates after a reload.
- The card now prints one line under the child board: the turn total and
  the sub-agents' slice with its share.

Two readings are easy to get wrong and are pinned by tests:

`usage.totalTokens` is the PARENT's own bill and `childTotalTokens` is the
children's; they are additive, not total-and-part-of-it. The first
implementation subtracted, which under-reported every turn. Writing the
test against the sidecar source caught it.

A turn whose children report no tokens at all (external ACP engines) still
renders the line, marked `partial` — otherwise the smaller number reads as
a complete bill, and a turn that delegated looks like one that didn't.

Cost is deliberately not shown: `costUsd` estimates the parent's model only,
and children may run on others, so presenting it as the turn's cost would
understate it.

Verified: web 973 passed, agent-shell ts 1646 passed; tsc clean in both.
Signed-off-by: Wang Tai <i@wangtai.me>
Signed-off-by: Wang Tai <i@wangtai.me>
@wangtai
wangtai merged commit f99fa96 into main Nov 13, 2026
43 checks passed
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.

1 participant