Skip to content

armgit / Wake

Public Imported from GitHub
0

All your AI agent sessions in one place — browse, search, resume. Rust + GPUI.

Rust

fix(scanner): only let the failed copy's own version yield to the fallback

Corey Chiu0a3628edec31
diff --git a/crates/wake-core/src/db.rs b/crates/wake-core/src/db.rs
index 4eb5a2e..fab1489 100644
--- a/crates/wake-core/src/db.rs
+++ b/crates/wake-core/src/db.rs
@@ -372,18 +372,20 @@ impl Store {
/// 先比副本所属实例的 `dedup_rank`(`rank_of` 按 file_path 给出,小者胜),
/// 同级再比 mtime 新者、平局路径字典序——两条路径尺子不一,会话就会在两份
/// 副本之间摇摆(2026-09-15 Codex review)。`supersedes` 是刚解析失败的那份
- /// 副本的路径(scanner 回退分支传入):库里这条 key 的行若正是它(quick 阶段
- /// 为它写的占位,或早先入库、后来损坏的转录),它已不是竞争者,本次写入直接
- /// 接管——判定与写入必须同一事务,先查再删再写会让 watcher 并发写入的第三份
- /// 副本被误删;按 file_mtime=0 推断占位也不行,真实文件同样可能给 0
- /// (Codex review 第二、三轮)。返回 false = 本次是败方副本,一字未写
+ /// 副本(路径 + 枚举时看到的 mtime,scanner 回退分支传入):库里这条 key 的行
+ /// 若正是它——同路径,且 file_mtime 不晚于枚举时看到的 mtime(quick 阶段占位
+ /// 的 0、早先入库后来损坏的旧版本都算)——它已不是竞争者,本次写入直接接管;
+ /// 期间 watcher 已把修好的同路径文件成功入库(mtime 更新)则不让位,照常裁决。判定与写入必须
+ /// 同一事务,先查再删再写会让并发写入的第三份副本被误删;单凭 file_mtime=0
+ /// 推断占位也不行,真实文件同样可能给 0(Codex review 二、三、四轮)。
+ /// 返回 false = 本次是败方副本,一字未写
pub fn write_session_guarded(
&self,
meta: &SessionMeta,
file_mtime: i64,
units: &[IndexUnit],
rank_of: &dyn Fn(&str) -> u8,
- supersedes: Option<&str>,
+ supersedes: Option<(&str, i64)>,
) -> Result<bool> {
let mut conn = self.write.lock().unwrap();
let tx = conn.transaction()?;
@@ -395,7 +397,12 @@ impl Store {
)
.optional()?;
if let Some((cur_path, cur_mtime)) = cur {
- let yields = supersedes.is_some_and(|failed| failed == cur_path);
+ // 让位只认"库里还是我们解析失败的那个版本或更旧的":同路径且行上的
+ // file_mtime ≤ 枚举时看到的 mtime(quick 占位的 0 自然包含;早先入库、
+ // 后来损坏的转录是"更旧");期间 watcher 已把修好的同路径文件成功入库
+ //(mtime 更新)就不让位,照常裁决(Codex review 第四轮)
+ let yields = supersedes
+ .is_some_and(|(failed, seen_mtime)| failed == cur_path && cur_mtime <= seen_mtime);
if cur_path != meta.file_path && !yields {
let (cur_rank, new_rank) = (rank_of(&cur_path), rank_of(&meta.file_path));
let loses = cur_rank < new_rank
diff --git a/crates/wake-core/src/scanner.rs b/crates/wake-core/src/scanner.rs
index c92d7e1..e93c56d 100644
--- a/crates/wake-core/src/scanner.rs
+++ b/crates/wake-core/src/scanner.rs
@@ -433,7 +433,7 @@ fn run_scan_inner(
fb.mtime_ms,
&parsed.units,
&rank_of(adapters, meta.agent),
- Some(&item.r.file_path),
+ Some((item.r.file_path.as_str(), item.r.mtime_ms)),
) {
Ok(written) => item_written = written,
Err(e) => eprintln!(
diff --git a/crates/wake-core/tests/db_roundtrip.rs b/crates/wake-core/tests/db_roundtrip.rs
index 5cd1638..6761c08 100644
--- a/crates/wake-core/tests/db_roundtrip.rs
+++ b/crates/wake-core/tests/db_roundtrip.rs
@@ -704,12 +704,19 @@ fn guarded_write_respects_winner() {
"rank 靠后的较新副本不得反超"
);
- // supersedes:库里的行正是刚解析失败的那份副本时它让位,回退副本直接接管
- //(判定与写入同一事务);库里若已被并发写成另一份副本,照常裁决、不误删
- //(2026-09-15 Codex review 第三轮)
+ // supersedes:库里的行正是刚解析失败的那份副本(同路径、同枚举 mtime)时
+ // 它让位,回退副本直接接管(判定与写入同一事务);同路径但库里版本更新
+ //(mtime 更大)= watcher 已把修好的文件成功入库,不让位;库里若已被并发写成
+ // 另一份副本,照常裁决、不误删(2026-09-15 Codex review 三、四轮)
+ assert!(
+ !store
+ .write_session_guarded(&ide, 12, &[], &rank_of, Some(("/cli/h.jsonl", 4)))
+ .unwrap(),
+ "同路径但库里版本更新(mtime 更大)不让位"
+ );
assert!(
store
- .write_session_guarded(&ide, 12, &[], &rank_of, Some("/cli/h.jsonl"))
+ .write_session_guarded(&ide, 12, &[], &rank_of, Some(("/cli/h.jsonl", 5)))
.unwrap(),
"失效的胜者行让位给回退副本"
);
@@ -722,7 +729,7 @@ fn guarded_write_respects_winner() {
third.file_path = "/store2/state.vscdb#h".into();
assert!(
!store
- .write_session_guarded(&third, 3, &[], &rank_of, Some("/cli/h.jsonl"))
+ .write_session_guarded(&third, 3, &[], &rank_of, Some(("/cli/h.jsonl", 5)))
.unwrap(),
"库里已是别的副本时 supersedes 不生效,按位次与 mtime 照常裁决"
);
@@ -730,6 +737,23 @@ fn guarded_write_respects_winner() {
store.get_session("cursor:h").unwrap().unwrap().file_path,
"/store/state.vscdb#h"
);
+
+ // quick 阶段为失败的胜者写的占位行(file_mtime=0)同样让位
+ let mut placeholder = meta("cursor:p", "占位");
+ placeholder.file_path = "/cli/p.jsonl".into();
+ store.write_meta_only(&[(placeholder, 0)]).unwrap();
+ let mut p_ide = meta("cursor:p", "IDE 副本");
+ p_ide.file_path = "/store/state.vscdb#p".into();
+ assert!(
+ store
+ .write_session_guarded(&p_ide, 12, &[], &rank_of, Some(("/cli/p.jsonl", 5)))
+ .unwrap(),
+ "占位行让位给回退副本"
+ );
+ assert_eq!(
+ store.get_session("cursor:p").unwrap().unwrap().file_path,
+ "/store/state.vscdb#p"
+ );
}
/// Insights 统计口径:archived=0、prompts 只数主线 user、daily/hourly 按