fix: propagate backfilled cwd/source_file across devices (#12) - #37
Merged
Conversation
Cross-device project stats lost Codex (and other cwd-dependent) projects on the device running `aiusage serve`: synced records carried no cwd and no source_file, so /api/projects skipped them entirely. Root cause: the cwd/source_file backfills enriched local records but did not bump `updated_at`. Cross-device propagation only re-uploads records where `updated_at > synced_at`, and the remote merge only overwrites when the incoming updatedAt is strictly newer — so the enriched fields never reached peers. backfillCodexModels already bumps updated_at; the cwd and hermes source_file backfills were the inconsistent siblings. - Extract the cwd backfill into backfillCwd and bump updated_at (matching backfillCodexModels); also bump in the per-file cwd write and in backfillHermesSourceFiles. - Add migration v12 to repair users who already ran the buggy v1.5.0–v1.5.7 backfill: their cwd is already populated locally, so the corrected `WHERE cwd = ''` backfill no longer matches them — the migration re-marks enriched records as changed to force one re-upload. It is required, not optional, for existing installs. - Tests for both the backfill bump and the v12 repair.
juliantanx
force-pushed
the
fix/issue12-backfill-sync-propagation
branch
from
June 26, 2026 07:03
13b61e1 to
aa50beb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #12
问题
在运行
aiusage serve的设备上,跨设备的项目统计看不到 Codex(以及其他依赖 cwd 的)项目:同步过来的记录既没有cwd也没有source_file,导致/api/projects把它们整条跳过。根本原因
这不是路径推断问题,而是回填的同步传播 bug:
cwd回填与backfillHermesSourceFiles更新了记录,但没有 bumpupdated_at。updated_at > synced_at的记录,且远端mergeRecords只在updatedAt严格更新时才覆盖 → 被回填的字段永远传不到其他设备。/api/projects用if (!row.source_file) continue丢弃空 source_file 行;codex 的源路径里没有项目信息,于是项目从列表中消失。backfillCodexModels本就正确 bump 了updated_at,它是正确参照——另外两个回填是漏写 bump 的兄弟 bug。bug 版自 v1.5.0 起已发布。已升级用户本地
cwd早已填上,所以修正后的WHERE cwd = ''回填已无法匹配它们 —— 修复存量数据必须配合一次性迁移,而非可选项。改动
updated_at:抽取backfillCwd(对齐backfillCodexModels),并在 per-file cwd 写入与backfillHermesSourceFiles中一并 bump。:session:source_file)的本地记录重新标记为已变更,强制下次同步重传一次完整 wire 格式。记录 id 按设备归属,bumpupdated_at只会重传本设备自己的行(last-write-wins 安全)。数据流验证(全 5 链路)
updated_atgetUnsyncedRecords重新选中uploadFile→mergeRecords覆盖远端 ndjson(bump 后 updatedAt 严格更大)pull→insertSyncedRecord的ON CONFLICT DO UPDATE ... WHERE excluded.updated_at > synced_records.updated_at更新旧行source_file/cwd/api/projects(特定设备)直接查synced_records→ 从 cwd 提取 project两种故障模式都被覆盖:被
getUnsyncedRecords漏掉(已 synced),以及被选中但mergeRecords因 updatedAt 相等不覆盖 —— 两者都靠 bump 解决。注意
source_file/cwd。Test plan
tests/commands/backfill-sync-propagation.test.ts— 回填后记录被重新标记为待同步tests/db/migration-v12.test.ts— v12 只 bump enriched 记录,跳过synced/与无 cwd 记录parse/serve(跑 v12)→ 同步 → host A 拉取后可见 Codex 项目