Repository navigation
fix(core): 见解记录文件加锁并原子写入,读坏的文件改名保留而不是当作空列表覆盖 - #1143
Merged
Merged
Conversation
ConfigStore::save 的「临时文件 + sync + 改名替换(0600)」提成 fs_atomic::write_private, lock_config 打开并锁住 <文件>.lock 的部分提成 fs_atomic::lock_beside,rename_replacing 原样移过去, 供见解记录文件复用(#1137)。行为不变,只是 lock 文件打不开时的报错统一为 failed to lock <文件>.lock。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
serve --insight 的引擎与 WebUI 标为已读、清空的子进程同时读改写 insights/<账号>.json 时, 一方可能读到另一方截断重写中的半个 JSON,当作空列表写回,丢掉全部历史记录。 - add、mark_read、clear 在 <文件>.lock 上加锁,锁内重新读文件、修改、写回 - 写入先写临时文件(0600、sync)再 rename_replacing 替换 - 读取区分没有文件、记录和无法解析的文件;无法解析的改名为 <文件>.corrupt-<时间> 保留并警告, split_legacy_records 并入旧文件时同样处理 - add、clear 返回 AppResult,加锁、读、写失败时报错,不再静默忽略 - cache clear-account 一并删除该账号被改名保留的副本 fixed #1137 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
做了什么
见解记录文件(
WEFLOW_HOME/insights/<账号>.json)原来不加锁、截断后原地重写,读坏时当作空列表:serve --insight的引擎在服务进程里add,WebUI 的「标为已读」「清空」在子进程里改同一个文件,交错时可能读到半个 JSON → 得到[]→ 写回只剩新记录,全部历史丢失。本 PR 照配置文件(#111)的做法修复:add、mark_read、clear在<账号>.json.lock上用File::lock()加锁,锁内重新读文件、修改、写回;mark_read只在确实改了时才写。list、get不加锁,依靠原子替换读到完整的文件。<文件>.<pid>-<n>.tmp,0600,sync_all),再用rename_replacing替换(Windows 上短暂重试)。<文件>.corrupt-<时间>保留,在 stderr(--json时为warning事件)提示,再从空开始写;split_legacy_records并入旧的总文件时也同样处理账号文件。RecordStore::add、clear返回AppResult,加锁、读、写失败时insight clear、insight mark-read、insight trigger报错(原来写失败被静默忽略);引擎的add失败经insight_generate的错误写进日志。新增报错文字failed to lock {}: {}、failed to set aside {}: {}已加进messages_zh的 CATALOG。cache clear-account一并删除该账号被改名保留的.corrupt-*副本(里面同样是该账号的记录)。提交分两个:
refactor(core):把ConfigStore::save的「临时文件 + sync + 改名(0600)」提成fs_atomic::write_private,lock_config打开并锁住.lock的部分提成fs_atomic::lock_beside,rename_replacing原样移进fs_atomic。配置文件的行为不变,只是.lock打不开时的报错统一为failed to lock <文件>.lock。fs_atomic::write_atomically(fix(core): 缓存文件先写临时文件再改名,中途取消或失败不再留下截断文件 #1133)不设 0600,所以另加write_private,不改它。fix(core):见解记录改用上面的函数,加锁、区分坏文件、补测试。怎么验证的
新增测试:
insight::records::tests::a_file_cut_short_is_set_aside_not_written_over:文件只剩半个 JSON 时,add不覆盖它,坏文件按原内容改名保留,新文件只有新记录。an_accounts_file_cut_short_is_set_aside_when_the_old_file_is_split:并入旧总文件时,坏的账号文件被改名保留。two_stores_taking_turns_lose_no_change:两个各自先load的 store(模拟引擎和mark-read子进程)交替add、互相mark_read对方的新记录,最后 10 条全在、全部已读(原来的实现在第一次mark_read就找不到记录)。stores_changing_the_file_at_once_lose_no_record:三个线程同时各add+mark_read15 次,最后 45 条全在、全部已读,目录里没有残留的临时文件。fs_atomic::tests::a_private_write_replaces_the_file_or_leaves_it_as_it_was:替换后为 0600(Unix,原文件 0644),目标是文件夹时失败且不留临时文件。命令(Windows 本机,另在 WSL 的 Linux 上跑了 clippy 和只在 Linux 编译的
insight_e2e):cargo fmt --all -- --checkcargo clippy -p weflow-core -p weflow-cli --all-targets -- -D warnings(Windows、Linux)cargo test -p weflow-core(含messages_zh的两个目录检查)cargo test -p weflow-core --test insight_e2e(Linux)用户可见的变化
insights/<账号>.json.corrupt-<时间>保留,并提示警告。insights/下多一个<账号>.json.lock(空文件)。insight clear、insight mark-read、insight trigger报错,不再静默成功。cache clear-account也删除该账号的.corrupt-*副本。Wiki 需要同步的页面(本 PR 不改 Wiki):AI 见解#记录(加锁、坏文件改名保留与警告)、缓存与配置(
insights/下的.lock和.corrupt-*、权限一行、cache clear-account删除的内容)。fixed #1137
🤖 Generated with Claude Code