Skip to content

feat(sight): add optimization tips and savings breakdown to token savings page#1007

Merged
jfeng18 merged 1 commit into
alibaba:mainfrom
husterL9:feature/sight/token-savings-observability
Jun 26, 2026
Merged

feat(sight): add optimization tips and savings breakdown to token savings page#1007
jfeng18 merged 1 commit into
alibaba:mainfrom
husterL9:feature/sight/token-savings-observability

Conversation

@husterL9

Copy link
Copy Markdown
Contributor

Description

为 Token Savings 页面增加优化建议面板节省排行面板,提升可操作性和可解释性。

后端 (handlers.rs)

  • OptimizationItemDto 新增 compression_ratio(压缩率)和 explanation(中文说明)
  • 新增 OptimizationTip 结构体(level/title/description)
  • get_token_savings 根据节省率、组件启用状态生成多级优化建议

前端 (TokenSavingsPage.tsx + apiClient.ts)

  • OptimizationTipsPanel:按 success/info/warning 分级展示优化建议
  • SavingsBreakdownPanel:跨会话 Top 5 节省排行,进度条可视化
  • DiffView 增强:explanation banner + before/after token 数量标注

测试 (TokenSavingsPage.test.tsx)

  • mock 数据同步新字段,新增 2 个测试用例

Related Issue

closes #1006

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 (578 passed, 0 failed)
  • Lock files are up to date (Cargo.lock)

Testing

  • cargo test: 578 passed, 0 failed, 28 ignored
  • 新增前端测试:OptimizationTipsPanel 渲染、SavingsBreakdownPanel 渲染

Additional Notes

前端以 embed 模式编译进二进制,已验证本地部署正常。

@husterL9 husterL9 requested a review from chengshuyi as a code owner June 18, 2026 02:51
@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.

@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!后端算好展示串 + severity 查找表 + ?? [] 防御,方向很对 👍 复核后:

建议处理:

  1. 评级死区:<5% 警告、≥15% 良好、≥30% 优秀,但 [5%,15%) 区间没有任何评级(handlers.rs:431/468/474),确认是否有意?若非,建议把警告区间扩到 <15%
  2. 阈值缺判别测试:测试用 3/25/50,边界 5/15/30 都没覆盖——把 >= 改成 > 测试也不会失败,建议补边界用例。
  3. 前端 banner 未覆盖:详情面板里 compression_ratio.toFixed() 在测试中从未展开渲染,API mock 若缺该字段会 crash;建议加一个"展开详情"的测试。

小点: compression_ratio 未 clamp,外部 stats.db 若 after>before 会显示负压缩率。

另:与 #985/#990 改同一批文件,合并需注意顺序与 rebase。

@husterL9 husterL9 force-pushed the feature/sight/token-savings-observability branch from 3bb756a to ff2ae1e Compare June 24, 2026 03:42
@husterL9

Copy link
Copy Markdown
Contributor Author

感谢 review! 已全部处理:

  1. 评级死区 ✅ — 警告阈值从 < 5% 扩大到 < 15%,现在区间划分为:<15% 警告 → [15%,30%) 良好 → ≥30% 优秀,无空档。

  2. 边界测试 ✅ — 新增 3 个边界用例:

    • test_tips_boundary_at_15_no_warning(精确 15.0% 不触发警告,触发「良好」)
    • test_tips_boundary_at_30_excellent(精确 30.0% 触发「优秀」)
    • test_tips_boundary_just_below_15_warning(14.9% 仍触发警告)
  3. 前端 banner 覆盖 ✅ — 在展开详情测试中增加了点击「详情」按钮后断言 75.0%压缩率 文本渲染。

  4. compression_ratio clamp ✅ — 使用 .clamp(0.0, 100.0) 防止 after > before 产生负值,新增 test_compute_compression_ratio_negative_clamped 验证。

后端测试 17 passed / 0 failed。关于与 #985/#990 的 rebase,合入时我会注意顺序处理冲突。

@husterL9 husterL9 force-pushed the feature/sight/token-savings-observability branch 2 times, most recently from 4fc4c67 to 676cb5b Compare June 24, 2026 07:02

@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 work on optimization tips — the generate_optimization_tips logic and test coverage look well thought out.

One issue: the backend additions (get_token_savings, compute_compression_ratio, build_explanation, generate_optimization_tips etc.) are in handlers.rs, but mod.rs routes /api/token-savings to token_savings.rs. The new endpoint in handlers.rs is never registered, so none of this backend code will be reached at runtime.

The frontend references item.compression_ratio.toFixed(1) and item.explanation, but the actually-served DTO from token_savings.rs does not include these fields — this will cause a runtime error.

Could you take a look?

@husterL9 husterL9 force-pushed the feature/sight/token-savings-observability branch from 676cb5b to 8f76176 Compare June 25, 2026 03:23
@husterL9

Copy link
Copy Markdown
Contributor Author

感谢指出!已修复路由 vs 实际端点不一致的问题:

根因mod.rs 注册的是 token_savings::get_token_savings,但我把新逻辑写到了 handlers.rs → 死代码,运行时永远到不了。

修复方案

  1. compression_ratioexplanationoptimization_tips 全部迁入 token_savings.rs(实际服务的端点)
  2. OptimizationItemDto 新增 compression_ratio: f64explanation: String 字段
  3. TokenSavingsResponse 新增 optimization_tips: Vec<OptimizationTip>
  4. get_session_savings 也同步加入了 compression_ratio 和 explanation
  5. 删除 handlers.rs 中全部重复的 Token Savings 死代码(约 440 行)
  6. 所有 17 个单元测试迁入 token_savings.rs 的 tests 模块

验证结果token_savings::tests 24 passed,handlers::tests 6 passed,编译 0 warnings。

@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.

LGTM overall — optimization tips and compression breakdown are useful additions.

One readability issue:

build_explanation uses Unicode escapes for Chinese text (e.g. \u{5de5}\u{5177} instead of "工具"). This makes the format strings nearly impossible to read or maintain. Please replace with Chinese string literals directly.

Also: this PR has merge conflicts with main (likely from #1099 baseline_tokens). Please rebase onto latest main before merge.

@husterL9 husterL9 force-pushed the feature/sight/token-savings-observability branch 2 times, most recently from 98e89c3 to f3d3283 Compare June 25, 2026 08:07
@husterL9 husterL9 requested a review from jfeng18 June 25, 2026 08:20
@husterL9 husterL9 force-pushed the feature/sight/token-savings-observability branch 2 times, most recently from 9c6e791 to bf536a7 Compare June 25, 2026 09:01

@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.

TokenSavingsPage.tsx:178 — 重构 DiffView 时旧条件渲染 {item.optimization_reason && (...)} 的开头被移除,但闭合 )} 残留。当前文件 <div 开标签 16 个、</div> 闭标签 15 个,第 157 行的 overflow 容器未正确关闭,TSX 无法编译。

另外第 157-177 行新增的两列对比与第 199-213 行原有 fallback 分支内容近乎相同,确认下是否需要去重。

其余 LGTM,后端逻辑已正确落入 token_savings.rs,阈值和边界测试覆盖完整。

@husterL9 husterL9 force-pushed the feature/sight/token-savings-observability branch from bf536a7 to efd12dd Compare June 26, 2026 06:05
@husterL9 husterL9 force-pushed the feature/sight/token-savings-observability branch from efd12dd to e11b587 Compare June 26, 2026 06:29
@jfeng18 jfeng18 self-requested a review June 26, 2026 06:57
@jfeng18 jfeng18 merged commit c5b8d6a into alibaba:main Jun 26, 2026
13 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 optimization tips and savings breakdown to token savings page

3 participants