Skip to content

feat(stats): merge token statistics onto latest main - #950

Merged
AAswordman merged 32 commits into
mainfrom
fix/token-stats-922-review
Aug 13, 2026
Merged

feat(stats): merge token statistics onto latest main#950
AAswordman merged 32 commits into
mainfrom
fix/token-stats-922-review

Conversation

@AAswordman

Copy link
Copy Markdown
Owner

变更说明 / Description

将完整的 token 统计功能及其当前审查修复合入最新 main,保留原有统计分支的提交历史,并补齐数据库访问串行化与编译修正。

背景与基线 / Context and baseline

  • 源功能分支:feat/token-stats-922-base
  • 原始功能 PR:#922
  • 上一轮当前基线整合 PR:#928,已关闭,未直接合入 main
  • 本 PR 分支:fix/token-stats-922-review
  • 本 PR 基于:origin/main @ 6819ac4401c91bc6eecf1ee5691675960c03f3c6
  • 目标分支:main

本分支在 3d567320 上合入最新 origin/main,随后完成本次收尾修复。

改动范围 / Changes

  • 保留并合入 feat(stats): add token usage statistics, pricing management and backup/restore integration #922 的 token 统计、价格配置、统计页面、Room 存储和现有备份集成。
  • 统计数据访问、Room 数据库备份、Room 恢复、原始快照导入/导出统一使用 TokenUsageRepository 的数据库访问锁。
  • 删除重复的 RoomDatabaseBackupRestoreLock,避免不同锁之间产生竞态。
  • Room 恢复先解压到临时文件,再使用同文件系统的原子替换;不再先删除当前数据库。
  • 当当前数据库仍有 WAL/SHM 而备份缺少对应文件时,在替换前拒绝恢复,避免混合 SQLite 文件。
  • 修复 MemoryLibrary 调用 parseAnalysisResult 时遗留的旧参数,恢复 compileDebugKotlin 编译。
  • 清理本次变更涉及文件的末尾空行噪音。

不包含:统计数据单独导入/导出;现有聊天记录 JSON 导入/导出协议;统计产品口径和 UI 设计的额外变更。

兼容性与风险 / Compatibility and risks

  • 统计表与聊天表同在 app_database,每日/手动 Room 备份及原始快照会一起保存,统计无需单独导入。
  • Room 恢复要求 WAL/SHM 与当前数据库状态保持一致;不匹配的备份会在目标文件替换前失败,保留当前数据库。
  • 恢复操作期间,统计写入、查询和其他数据库备份会等待统一锁,避免打开或使用正在替换的 Room 实例。
  • 原子替换按数据库、WAL、SHM 文件分别执行;每个文件替换本身是原子的。

关联 Issue / Related issue

Supersedes #928 and carries forward the implementation from #922.

验证方式 / Verification

检查或命令:
- git fetch origin main
- git merge-base --is-ancestor origin/main HEAD
- git diff --check origin/main...HEAD
- ./gradlew :app:compileDebugKotlin

环境与变体:
- Windows 工作区
- branch: fix/token-stats-922-review
- debug Kotlin compilation

结果:
- origin/main 是当前分支祖先
- git diff --check 通过
- :app:compileDebugKotlin 成功(BUILD SUCCESSFUL)
- 未新增测试;遵循用户要求保持改动简单干净

证据 / Evidence

检查清单 / Checklist

  • 我已记录可复现验证和未运行项原因 / Reproducible verification and reasons for unrun checks
  • 我已确认 Candidate checks 覆盖改动范围,并会处理技术失败项 / Candidate checks covers the change scope and technical failures will be addressed
  • 最终 diff 无无关、临时、生成、二进制或敏感内容 / Final diff has no unrelated, temporary, generated, binary, or secret content
  • 已提供对应的回归、UI、文档/字符串或兼容性证据 / Relevant regression, UI, docs/strings, or compatibility evidence is provided

CATMIAOZHI and others added 30 commits August 10, 2026 05:08
(cherry picked from commit dbb03ee)
P1-1: wire backup/restore barriers into raw snapshot and Room managers; persist and consume the restore REPLACING marker across crashes so pre-restore spool is never replayed into a replaced database.
P1-2: fail-open statistics finalization - a persistence failure must never fail a successful model result; degraded tracking context when the stats DB is unavailable.
P1-3: run finalization on Dispatchers.IO so FutureTask.get and fsync never block the Main thread.
P1-4: bill Gemini thoughts on top of candidates and extract usageMetadata before early returns.

Adds BackupRestoreBarrierTest covering production manager wiring, marker consumption on startup, and corrupt-zip pre-commit rejection.
Preserve PR #922 authorship and review history on the dedicated token-statistics baseline branch. Follow-up redesign work continues on fix/token-stats-922-review before any main merge.
Replace the spool, baseline, recovery, cleanup, and quarantine implementation with structured Room usage records and model settings. Integrate provider usage capture, SQL-backed statistics, settings and UI updates, and normal backup/restore handling. Document the redesigned storage model and remove obsolete tests.
…eview

# Conflicts:
#	app/src/main/java/com/ai/assistance/operit/api/chat/library/MemoryLibrary.kt
#	app/src/main/java/com/ai/assistance/operit/data/backup/RawSnapshotBackupManager.kt
@AAswordman
AAswordman merged commit 9cb9afb into main Aug 13, 2026
1 check failed
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.

2 participants