Skip to content

fix: 清理多货币审计剩余问题并删除原始口径死代码 - #11

Open
Rain-0x01-39 wants to merge 14 commits into
leafliber:mainfrom
Rain-0x01-39:fix/multi-currency-audit
Open

fix: 清理多货币审计剩余问题并删除原始口径死代码#11
Rain-0x01-39 wants to merge 14 commits into
leafliber:mainfrom
Rain-0x01-39:fix/multi-currency-audit

Conversation

@Rain-0x01-39

Copy link
Copy Markdown
Contributor

背景

延续 #9 的多货币换算审计,本次清理剩余的符号/口径展示问题(M2/M4/L2/L3/L4)与原始计费货币口径的死代码(L1)。P0 金额错误(S1-S5)与 review 反馈(P1/P2)已在 #9 修复并合并。

修复

M2 — /budget 命令限额展示口径错配

全局维度按 budgets_cost_currency、override 按 cost_currency 将花费限额换算到主货币后再显示。此前用主货币符号配原始货币值(如主货币 CNY、限额 USD 10 显示 $10)。

M4 — 未定价模型告警文案硬编码 $0

改为跟随主货币符号。

L2/L3 — 默认参数 main_cur="$"(非法货币代码)

budget._override_usedweb_api._supplement_to_dict 默认值改为 "USD",避免漏传时把 $ 当未知货币导致不换算。

L4 — ai_diag 预算数据口径不完整

喂给 LLM 的预算数据 cost_limit 换算到主货币,并补充 cost_used/cost_ratio 与 cost 维度超限判断(此前 exceeded 只判 token)。

L1 — 删除原始口径死代码

  • budget._groups_cost(仅测试引用)及 3 个相关单测
  • analytics._row_cost(无调用方)与 compute_row_cost 死导入
  • cost.CostMixin.compute_cost(无调用方)
  • web_apicompute_cost_grouped/compute_row_cost 死导入

避免原始计费货币口径函数被后续误用;生产链路已统一走 *_in_main 版本。

验证

  • 186 个测试全部通过
  • ruff check / ruff format 全部通过

用 compute_cost_with_currency 取 (金额, 货币代码),分别固化 cost_amount /
currency_symbol。此前用 compute_cost_value 返回原始货币金额却一律标 "USD",
导致后续 _supplement_to_dict 回退路径把 CNY 数字当 USD 换算。
此前函数内部按各行原始计费货币金额直接累加(混合货币和),三处调用方
(budget._override_used user 分支、check_budget per_user_daily、
web_api.api_budgets override user 分支)又把结果当 USD 二次换算到主货币,
构成双重错误。

改为:函数签名加 main_cur / rates,内部逐行用 compute_cost_with_currency
取 (金额, 货币) 后 convert 到主货币累加;三个调用方去掉二次换算直接传入
main_cur / rates。_override_used 的 user 分支此前根本没换算,一并修正。
cmd_cost 此前用 compute_row_cost 把各行原始计费货币金额直接相加(存在
非 USD 定价条目时为混合货币和),却用主货币符号显示。改用
compute_row_cost_in_main 逐行换算到主货币后求和,与 schedule.py 日报、
budget.check_budget 口径一致。
S3: 全局 5 维 cost 限额(limits_cost)按 budgets_cost_currency 各自填的
原始货币值,而 used 已是主货币口径,_part 算 ratio/exceeded 时口径错配。
参照 budget.check_budget:426-435 换算到主货币得到 limits_cost_main,
_dim_entry 用它比较;响应保留 limits_cost 原始值供前端编辑框初始值,
新增 limits_cost_main 供展示/判定。

S4: override 的 cost_limit 按 cost_currency 原始货币填,_ratio 直接拿来
与主货币口径的 used_c_v 比较。参照 check_budget:494-497 先换算到主货币
再比较;cost_limit / cost_currency 保留原始值供编辑。
旧版 backfill_cost_amounts 用 compute_cost_value 固化的金额是原始计费货币,
却一律标 currency_symbol="USD",导致展示层把 CNY 数字当 USD 换算。
新增 fix_mislabeled_cost_currency:仅修正 currency_symbol 为定价条目实际
货币(不动 cost_amount,保持历史快照),main.py initialize 时执行。
比较时 used/limit 均已换算到主货币,返回结果却标 ov_cur,_format_message
按限额原币种符号显示主货币金额(如主货币 CNY 时显示 $80 / $72)。改为
返回 main_cur。
回退调用 compute_cost_value 返回原始计费货币金额,却直接作为主货币 cost
输出(主货币 USD 时 CNY 7.2 的记录显示 7.2 而非 1.0)。改用
compute_cost_in_main 即时换算到主货币,与 analytics 口径一致。
后端 /budgets 的 dimensions.cost / override current.cost 已是主货币金额,
前端此前仍按限额原币种符号展示原始 limits_cost / cost_limit,导致
主货币 CNY、限额 USD 10 时界面显示 $36 / $10。改为:

- GlobalDefaultsPanel 展示行用 dimensions.cost 的 used/limit(主货币值)
  + 缺省主货币符号;编辑框保留原始货币输入语义。
- OverrideRow 状态行用 current.cost.used/limit + 主货币符号。
- 后端 _ratio 补充 limit 字段(主货币口径),类型 OverrideCurrent.cost
  增加 limit;BudgetResponse 增加 limits_cost_main 字段。
- 同步更新构建产物 pages/dashboard(index.html / style.css / app.js)。
- M2: /budget 命令的全局维度与 override 花费限额按 budgets_cost_currency /
  cost_currency 换算到主货币后再显示(此前用主货币符号配原始货币值)。
- M4: 未定价模型告警文案硬编码 $0 改为跟随主货币符号。
- L2/L3: budget._override_used 与 web_api._supplement_to_dict 默认参数
  main_cur="$"(非法货币代码)改为 "USD"。
- L4: ai_diag 喂 LLM 的预算数据 cost_limit 换算到主货币口径,并补充
  cost_used / cost_ratio 与 cost 维度超限判断(此前 exceeded 只判 token)。
- 删除 budget._groups_cost(仅测试引用,生产已统一走 compute_cost_grouped_in_main)
 及相关 3 个单测。
- 删除 analytics._row_cost(无任何调用方)与 compute_row_cost 死导入。
- 删除 cost.CostMixin.compute_cost(async,无调用方)。
- 删除 web_api 内 compute_cost_grouped / compute_row_cost 死导入(同函数内
  实际使用 _in_main 版本)。

避免原始计费货币口径函数被后续误用。
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