Skip to content

feat(sight): add strategy breakdown to token savings page#985

Merged
jfeng18 merged 2 commits into
alibaba:mainfrom
husterL9:feature/sight/token-savings-strategy
Jun 24, 2026
Merged

feat(sight): add strategy breakdown to token savings page#985
jfeng18 merged 2 commits into
alibaba:mainfrom
husterL9:feature/sight/token-savings-strategy

Conversation

@husterL9

Copy link
Copy Markdown
Contributor

Description

Add per-strategy visualization and breakdown to the token savings dashboard, enabling users to see which optimization strategies (response compression, schema compression, command rewriting, TOON encoding) contribute most to their token savings.

Changes:

  • Frontend: Add strategy pie chart to savings summary card; add strategy column with colored badges and hover tooltips to optimization items table
  • Backend: Extend /api/token-savings response with strategy_breakdown array; add session-scoped /api/token-savings/session/{id} endpoint
  • Tests: Update AtifViewerPage and TokenSavingsPage tests

Related Issue

closes #984

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional change)
  • Performance improvement
  • CI/CD or build changes

Scope

  • sight (agentsight)

Checklist

  • I have read the Contributing Guide
  • My code follows the project's code style
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the documentation accordingly
  • For sight: cargo test pass
  • Lock files are up to date (Cargo.lock)

Testing

  • cargo test passes (2 passed, 28 ignored, 0 failed)
  • Manual verification: token savings page displays strategy pie chart and strategy column correctly with test data

@husterL9 husterL9 requested a review from chengshuyi as a code owner June 17, 2026 08:52
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


linyizhou seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions github-actions Bot added the component:sight src/agentsight/ label Jun 17, 2026
@husterL9 husterL9 force-pushed the feature/sight/token-savings-strategy branch 4 times, most recently from f5230c1 to acff014 Compare June 18, 2026 03:30

@jfeng18 jfeng18 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢 @husterL9 这个策略拆分!特别赞这次补齐了后端 Rust 测试 👍 复核后几个建议:

建议处理:

  1. 节省率口径不一致:get_session_savingscompounded/(actual+compounded)(handlers.rs:872),而列表页 get_token_savingssaved/actual(:1182)——同一 session 在 AtifViewer 卡片与列表页会显示不同的百分比,建议统一口径。
  2. 饼图重复切片:strategy_breakdown 按原始 operation 聚合(:703)、用 label 展示(:724),多个未知 operation 会生成多个都叫"其他优化"的切片;建议按 strategy label/id 聚合。
  3. 全表扫描:get_session_savingslist_sessions_for_savings(0, i64::MAX) 取全部 session 再 .find 单条,每次打开 AtifViewer 都会全表聚合;建议加按 session_id 的单条查询。

小点: compress-toon/compress-schema 归入 tool/mcp 桶的依据建议加注释说明。

另:本 PR 与 #990/#1007 改了同一批文件(handlers.rs / TokenSavingsPage.tsx),合并需注意顺序与 rebase;三者共往 handlers.rs 加了约千行,建议考虑抽出 server/token_savings.rs 收口。

- Add per-strategy pie chart (compress-response, compress-schema,
  rewrite-command, compress-toon) to savings summary card
- Add strategy column with colored badges and hover tooltips to
  optimization items table
- Extend /api/token-savings response with strategy_breakdown array
- Add session-scoped token savings endpoint
- Enhance ATIF viewer page with improvements
- Update related tests
@husterL9 husterL9 force-pushed the feature/sight/token-savings-strategy branch from 21ab2b4 to 275bd1d Compare June 24, 2026 03:30
@husterL9

Copy link
Copy Markdown
Contributor Author

感谢 review,已全部修复并 force push:

  1. 节省率口径统一get_session_savings 改为 compounded / total_tokens,与列表页一致
  2. 饼图重复切片 — 按 strategy_label 聚合,未知 operation 合并为单个「其他优化」切片
  3. 全表扫描 — 新增 get_session_for_savings(session_id) 方法,按 session_id 直接 GROUP BY 查询
  4. 分类注释 — 补充了 compress-toon / compress-schema 归桶的说明
  5. 模块拆分 — 提取 server/token_savings.rs(~800 行),handlers.rs 瘦身

PTAL 🙏

@jfeng18 jfeng18 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the solid work on strategy breakdown! Two items to address:

P1 — Test env pollution

token_savings.rs L668/702/740/773: set_var("HOME", ...) is never restored after each test. Since ENV_MUTEX only guards within this module, other tests in the same binary (e.g. genai::default_path(), tokenless::default_stats_path()) may observe the stale value under parallel execution, causing intermittent CI failures.

P2 — Pie chart colors always fall back to grey

StrategyBreakdown.strategy is populated with the Chinese label (e.g. "Schema 压缩") via grand_strategy_map key (L377), but the frontend STRATEGY_CONFIG is keyed by operation name (compress-schema). The lookup STRATEGY_CONFIG[b.strategy] never matches, so all pie slices render in the default #9ca3af grey.

@husterL9 husterL9 force-pushed the feature/sight/token-savings-strategy branch from 275bd1d to 93baa36 Compare June 24, 2026 04:02
@husterL9 husterL9 requested a review from jfeng18 June 24, 2026 06:04
- Fix savings rate formula inconsistency: unify get_session_savings
  to use compounded/total_tokens (same as get_token_savings list page)
- Fix duplicate pie chart slices: aggregate strategy_breakdown by
  label instead of raw operation, so unknown ops merge into one slice
- Fix full table scan: add get_session_for_savings() method that
  queries by session_id directly instead of list_all + .find
- Add classification comments explaining compress-toon/compress-schema
  bucket assignment rationale
- Extract token_savings.rs module from handlers.rs (~800 lines) to
  reduce file size and improve maintainability
@husterL9 husterL9 force-pushed the feature/sight/token-savings-strategy branch from 93baa36 to 0bf87e5 Compare June 24, 2026 06:09

@jfeng18 jfeng18 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both issues addressed. HOME env var is now properly saved/restored in all 4 test functions, and StrategyBreakdown.strategy correctly uses the operation key for known ops. LGTM.

@jfeng18 jfeng18 merged commit 364d8bb into alibaba:main Jun 24, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:sight src/agentsight/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[sight] feat: add strategy breakdown to token savings dashboard

3 participants