fix(api): 全局强制 API 路由动态渲染,修复 build 阶段收集数据导致的崩溃

This commit is contained in:
kennethcheng 2026-05-03 15:34:01 +08:00
parent a06b993558
commit 917291ad5b

View File

@ -1,5 +1,14 @@
# Omniledger 架构与开发记忆 (Memory) # Omniledger 架构与开发记忆 (Memory)
## 执行 Task 95为 /api/cron/fetch-prices 及其他后端 API 注入 force-dynamic 声明,彻底阻断 Next.js 静态收集导致 Turbopack 克隆实例失败的构建期崩溃
- **架构红线**:所有涉及数据库写入或外部 API 调用的 Route Handler 必须显式声明 `export const dynamic = 'force-dynamic'``export const fetchCache = 'force-no-store'`,确保构建期不被 Next.js 静态预渲染引擎错误收集。
- **扫描结论**:已全面审计 `app/api/` 目录下全部 4 个对外提供服务的 route.ts 文件:
- `app/api/cron/fetch-prices/route.ts` ✓ 已声明
- `app/api/cron/fetch-rates/route.ts` ✓ 已声明
- `app/api/debug/snapshot/route.ts` ✓ 已声明
- `app/api/admin/rebuild-snapshots/route.ts` ✓ 已声明
- **重灾区验证**cron 目录下的 fetch-prices资产价格同步和 fetch-rates汇率同步均已打上动态标签构建期崩溃风险已清除。
## 修复 portfolio.ts 卖出交易时的平均成本分母 Bug将 totalBuyQuantity 替换为真实的当前 quantity彻底消除了频繁交易导致的本金虚高幽灵账目 (Task 88) ## 修复 portfolio.ts 卖出交易时的平均成本分母 Bug将 totalBuyQuantity 替换为真实的当前 quantity彻底消除了频繁交易导致的本金虚高幽灵账目 (Task 88)
- **根因分析**:在 `src/actions/portfolio.ts``getPortfolioPositions()` 函数中SELL 交易的平均成本计算使用了 `holding.totalBuyQuantity`(历史累计买入总量)作为分母,而非 `holding.quantity`(卖出前的实际真实持仓量)。当同一资产存在多次"买-卖-买-卖"循环时,`totalBuyQuantity` 会不断累加而不再下降,导致分母远大于真实持仓量,平均成本被严重稀释,卖出时扣减的 `totalBuyCostNative/Cny` 不足,最终造成 Dashboard 投入本金虚高(幽灵本金)。 - **根因分析**:在 `src/actions/portfolio.ts``getPortfolioPositions()` 函数中SELL 交易的平均成本计算使用了 `holding.totalBuyQuantity`(历史累计买入总量)作为分母,而非 `holding.quantity`(卖出前的实际真实持仓量)。当同一资产存在多次"买-卖-买-卖"循环时,`totalBuyQuantity` 会不断累加而不再下降,导致分母远大于真实持仓量,平均成本被严重稀释,卖出时扣减的 `totalBuyCostNative/Cny` 不足,最终造成 Dashboard 投入本金虚高(幽灵本金)。
- **架构红线**:计算移动平均成本时,绝对禁止使用 `holding.totalBuyQuantity` 作为分母!必须使用发生交易前的实际持仓量 `holding.quantity` - **架构红线**:计算移动平均成本时,绝对禁止使用 `holding.totalBuyQuantity` 作为分母!必须使用发生交易前的实际持仓量 `holding.quantity`