Skip to content

armgit / Wake

Public Imported from GitHub
0

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

Rust

feat(search): rank recently active sessions a little higher

Corey Chiu37f7cae14b05
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4fb1bb9..caff97e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
- Fix: ⌘K / Ctrl+K results show the `@host` badge for sessions mirrored from a remote host
- Fix: Insights no longer counts a session twice when it is mirrored from more than one host
- Fix: Settings → Data includes the remote mirrors in the storage size
+- Update: search favours recently active sessions a little — an equally good match from last week now outranks one from last year, in ⌘K / Ctrl+K, `wake_search` and `wake-cli search`
- Fix: an agent's own Wake lookups (the `wake_*` tools, `wake-cli` commands) no longer show up as search hits in its session
- Update: search now matches session titles too, in ⌘K / Ctrl+K, `wake_search` and `wake-cli search` — title hits come first and open the session at its start
diff --git a/crates/wake-core/src/db.rs b/crates/wake-core/src/db.rs
index a3a964d..77bfafd 100644
--- a/crates/wake-core/src/db.rs
+++ b/crates/wake-core/src/db.rs
@@ -1498,6 +1498,14 @@ impl Store {
}
let degraded = segs.iter().any(|s| s.chars().count() < 3);
let limit = if f.limit > 0 { f.limit } else { 60 };
+ // 近因加权:bm25 是负数、越小越相关,乘上 (1 + W / (1 + 距今天数 / H)) 让
+ // 新会话的命中"更负"。W = 1、H = 30 天:今天的会话 ×2、一个月前 ×1.5、
+ // 一年前 ×1.08——同等文本相关性下新会话在前,文本相关性差一倍以上的老命中
+ // 仍然赢得过。按会话的 updated_at 而不是消息自己的 ts:排的是"该翻哪场
+ // 对话",同一会话内各命中的相对顺序不受影响。只用四则运算与 max(),不依赖
+ // SQLite 的数学扩展;`?` 是当前时间(毫秒)。降级的 LIKE 路径本来就按时间排
+ const RECENCY_BOOST: &str = "(1.0 + 1.0 / (1.0 + max(0, ? - s.updated_at) / 2592000000.0))";
+ let now = now_ms();
// 会话侧筛选(agent / 项目并集 / 时间下界):SQL 片段拼一次,参数按需重建
// ——正文与标题是两条查询,Box<dyn ToSql> 不能 clone
@@ -1548,7 +1556,7 @@ impl Store {
JOIN messages m ON m.id = messages_fts.rowid
JOIN sessions s ON s.key = m.session_key
WHERE messages_fts MATCH ?{filter_sql}
- ORDER BY bm25(messages_fts) LIMIT ?"
+ ORDER BY bm25(messages_fts) * {RECENCY_BOOST} LIMIT ?"
);
let mut stmt = conn.prepare_cached(&sql)?;
let mut all_args: Vec<Box<dyn rusqlite::ToSql>> = vec![
@@ -1557,6 +1565,7 @@ impl Store {
Box::new(match_expr.clone()),
];
all_args.extend(filter_args(f));
+ all_args.push(Box::new(now));
all_args.push(Box::new(limit));
let rows = stmt.query_map(
rusqlite::params_from_iter(all_args.iter().map(|b| b.as_ref())),
@@ -1628,7 +1637,7 @@ impl Store {
"SELECT t.key, highlight(titles_fts, 1, ?, ?)
FROM titles_fts t JOIN sessions s ON s.key = t.key
WHERE titles_fts MATCH ?{filter_sql}
- ORDER BY bm25(titles_fts) LIMIT ?"
+ ORDER BY bm25(titles_fts) * {RECENCY_BOOST} LIMIT ?"
);
let mut stmt = conn.prepare_cached(&sql)?;
let mut all_args: Vec<Box<dyn rusqlite::ToSql>> = vec![
@@ -1637,6 +1646,7 @@ impl Store {
Box::new(match_expr),
];
all_args.extend(filter_args(f));
+ all_args.push(Box::new(now));
all_args.push(Box::new(limit));
let rows = stmt.query_map(
rusqlite::params_from_iter(all_args.iter().map(|b| b.as_ref())),
diff --git a/crates/wake-core/tests/db_roundtrip.rs b/crates/wake-core/tests/db_roundtrip.rs
index 8f15557..c3524b5 100644
--- a/crates/wake-core/tests/db_roundtrip.rs
+++ b/crates/wake-core/tests/db_roundtrip.rs
@@ -1091,3 +1091,40 @@ fn insights_counts_a_session_mirrored_on_several_hosts_once() {
(1, 3, 300)
);
}
+
+/// 近因加权:同等文本相关性下最近活跃的会话排前面(bm25 同分时原本按写入顺序,
+/// 先写的老会话在前),标题命中与正文命中同一规则
+#[test]
+fn search_ranks_recently_active_sessions_higher() {
+ let (_dir, store) = temp_store();
+ let now = wake_core::db::now_ms();
+ let day = 86_400_000;
+ let mut old = meta("claude-code:old", "缓存策略讨论");
+ old.updated_at = now - 400 * day;
+ let mut fresh = meta("claude-code:fresh", "缓存策略讨论");
+ fresh.updated_at = now - day;
+ for m in [&old, &fresh] {
+ store
+ .write_session(
+ m,
+ m.updated_at,
+ &[unit(0, Role::User, "缓存失效要不要加 jitter")],
+ )
+ .unwrap();
+ }
+ let (hits, _) = store.search("缓存失效", &[], None, 10).unwrap();
+ assert_eq!(
+ hits.iter()
+ .map(|h| h.session.key.as_str())
+ .collect::<Vec<_>>(),
+ ["claude-code:fresh", "claude-code:old"]
+ );
+ let (hits, _) = store.search("缓存策略", &[], None, 10).unwrap();
+ assert!(hits.iter().all(|h| h.role == "title"));
+ assert_eq!(
+ hits.iter()
+ .map(|h| h.session.key.as_str())
+ .collect::<Vec<_>>(),
+ ["claude-code:fresh", "claude-code:old"]
+ );
+}
diff --git a/docs/mcp.md b/docs/mcp.md
index bfaedb6..c6c0d6f 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -141,7 +141,7 @@ Full-text search across every indexed session: titles, user prompts, assistant r
| `project`, `agents`, `since` | | | see above |
| `limit` | integer 1–30 | 10 | maximum sessions returned |
-Results are grouped by session, best matches first, with up to three snippets per session. Each snippet carries a reference of the form `wake://session/<key>#<seq>` that `wake_get_session` accepts directly. Archived sessions are included.
+Results are grouped by session, best matches first, with up to three snippets per session. Recently active sessions get a mild boost: an equally good match from last week outranks one from last year, but a clearly better match still wins regardless of age. Each snippet carries a reference of the form `wake://session/<key>#<seq>` that `wake_get_session` accepts directly. Archived sessions are included.
```
26 sessions match `二维码` (project /Users/me/Github/app) — showing 3, best matches first.