chore(cost-insights): stop all writes to the cost_insight_* tables - #4996
Conversation
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryRe-reviewed at HEAD Review Notes
Files Reviewed (52 files)
Previous Review Summaries (2 snapshots, latest commit 382e24a)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 382e24a)Status: No Issues Found | Recommendation: Merge Executive SummaryRe-reviewed at HEAD Review Notes
Files Reviewed (51 files)
Previous review (commit 88325c9)Status: No Issues Found | Recommendation: Merge Executive SummaryVerified the complete removal of all Review Notes
Files Reviewed (51 files)
Reviewed by kimi-k3 · Input: 159.4K · Output: 12.7K · Cached: 1.5M Review guidance: REVIEW.md from base branch |
Second step of the Cost Insights shutdown. Nothing reads or writes the cost_insight_* tables after this; a follow-up drops the tables, enums, and the CostInsightEventSnapshot schema. Rehomes a critical unrelated job first. runCostInsightHourlySweep was the only production caller of processPendingDailyUsageRollupRepairs, which is the only writer of microdollar_usage_daily. That table feeds Kilo Pass spend totals and both public leaderboard routes, and processUsage enqueues a repair signal for every usage row with a non-zero cost. Deleting the sweep as-is would have frozen those reads and grown microdollar_usage_daily_repairs without bound. The work now has its own cron at /api/cron/usage-daily-rollup-repairs, taking over the 5 * * * * slot with the same limit of 20 and its own summary and Sentry telemetry. Removed writes: - both Cost Insights cron routes and vercel.json registrations - six captureCostInsightSpend calls, five of them awaited inside billing transactions, so spend recording no longer depends on Cost Insights - five scheduleCostInsightEvaluationAfterSpend calls - the owner-hour advisory locks and rollup-repair enqueue in processUsage - the operator scripts db/cost-insights-rollups and db/cost-insights-source-indexes Removed now-unreachable code: all of apps/web/src/lib/cost-insights and packages/db/src/cost-insights-rollups, plus the @kilocode/db export entry and the dead getExaCostInsightFeatureKey helper. Relocated spend-writer-audit.test.ts to lib/ai-gateway. It enforces a repo-wide allowlist for direct microdollars_used increments and has nothing to do with Cost Insights, so deleting the directory would have silently dropped that invariant. Two incidental behaviour changes, both previously implicit: - a Cost Insights capture failure no longer rolls back coding plan activation, coding plan renewal, KiloClaw enrollment, KiloClaw renewal, or Exa usage recording - recordExaUsage no longer throws for a nonexistent user. The mandatory capture was doubling as an owner-existence guard; exa_usage_log has no foreign key and deductFromBalance matches zero rows. The Exa proxy only calls it after authentication. The discontinued-notice routes stay in place.
The mandatory Cost Insights capture was the only thing that rolled back recordExaUsage when the personal balance update matched no user row. Neither exa_usage_log nor exa_monthly_usage has a user foreign key, so without it a charged request could commit charged_to_balance and the charged monthly counter without ever debiting kilocode_users. Makes the guard explicit in deductFromBalance, matching the organization branch which already throws when its row disappears.
382e24a to
6bcb361
Compare
Second of three PRs shutting down Cost Insights, following #4993. After this, nothing reads or writes the
cost_insight_*tables. PR3 drops the tables, enums, andCostInsightEventSnapshotSchema.A critical unrelated job had to be rehomed first
runCostInsightHourlySweepwas the only production caller ofprocessPendingDailyUsageRollupRepairs, which is the only writer ofmicrodollar_usage_daily. That table is read bykilo-pass-router.ts:1112(Kilo Pass spend totals) and both public leaderboard routes, andprocessUsage.tsenqueues a repair signal for every usage row with a non-zero cost.Deleting the sweep as-is would have silently frozen Kilo Pass totals and the leaderboards while growing
microdollar_usage_daily_repairsby one row per billable request forever. A critical non-Cost-Insights job was nested inside the Cost Insights sweep.It now has its own route,
/api/cron/usage-daily-rollup-repairs, taking over the5 * * * *slot with the same limit of 20 plus its own summary and Sentry telemetry. Throughput is unchanged: a claim coalesces all pending signals for one owner and UTC day, so the limit bounds distinct owner-days rebuilt per run rather than requests.Writes removed
vercel.jsonregistrations.captureCostInsightSpendcalls —processUsage,exa-usage,coding-plans/index,coding-plans/billing-lifecycle-cron,kiloclaw/credit-billing, andservices/kiloclaw-billing/src/lifecycle. Five were awaited inside billing transactions.scheduleCostInsightEvaluationAfterSpendcalls.processUsagebilling transaction.db/cost-insights-rollups(wrote rollups and coverage) anddb/cost-insights-source-indexes.Dead code removed
All of
apps/web/src/lib/cost-insights/andpackages/db/src/cost-insights-rollups.ts, plus the@kilocode/dbexport entry and the now-deadgetExaCostInsightFeatureKeyhelper inexa-paths.ts.spend-writer-audit.test.tswas relocated tolib/ai-gateway/rather than deleted. It enforces a repo-wide allowlist requiring every directmicrodollars_usedincrement to be explicitly classified, which has nothing to do with Cost Insights — deleting the directory would have silently dropped that invariant.Two incidental behaviour changes
Both were previously implicit consequences of the capture being mandatory:
recordExaUsageno longer throws for a nonexistent user. The mandatory capture was doubling as an owner-existence guard:exa_usage_log.kilo_user_idhas no foreign key anddeductFromBalancesilently matches zero rows. The Exa proxy only calls this after authentication, so there is no practical exposure, but the guard is genuinely gone.Retained for PR3
packages/db/src/schema.tstable definitions, theCostInsight*enums inschema-types.ts,CostInsightEventSnapshotSchemaand its test, the migration history, and the discontinued-notice routes from #4993. The two indexes formerly managed bydb/cost-insights-source-indexeswill be dropped in PR3's migration.Verification
scripts/typecheck-all.shandscripts/lint-all.shacross all workspaces: 0 errors, 0 warnings.pnpm format:checkclean, andscript/check-md-table-padding.tspasses.apps/webjest suite: 697 suites, 8,913 passed, 3 skipped.services/kiloclaw-billingvitest: 6 files, 184 passed.maxDuration.