baa-conductor

git clone 

baa-conductor / bugs / archive
codex@macbookpro  ·  2026-03-31

BUG-035-conversation-links-null-unique-index.md

 1# BUG-035: conversation_links UNIQUE INDEX 不约束 NULL remote_conversation_id
 2
 3> 提交者:Claude
 4> 日期:2026-03-30
 5
 6## 现象
 7
 8Schema 中 `idx_conversation_links_remote` 定义为 `UNIQUE(platform, remote_conversation_id)` 9
10SQLite 中 NULL 值不参与 UNIQUE 约束(SQL 标准行为),因此同一平台可以存在多条 `remote_conversation_id = NULL` 的 link 而不触发冲突。
11
12## 触发路径
13
141. Gemini 页面的 conversation_id 提取失败(`extractGeminiConversationIdFromUrl` 返回 null)
152. `observeRenewalConversation` 写入 `conversation_links`,`remote_conversation_id = NULL`
163. 下次同一页面再次触发,又写入一条 `remote_conversation_id = NULL` 的新 link
174. 两条 link 都指向不同的 `local_conversation_id`
185. `resolveExistingConversationLink` 跳过 `remote_conversation_id` 查找,走到 `scoreConversationLink` 弱信号路径
196. 弱信号匹配可能选到错误的 link,导致消息关联到错误对话
20
21## 建议修复
22
23两种方案:
24
25A. 在应用层,`remote_conversation_id` 为 null 时生成一个合成 ID(如 `synthetic_{platform}_{pageUrl_hash}`),确保 UNIQUE 生效
26B. 加一个部分索引 `CREATE UNIQUE INDEX ... ON conversation_links(platform, client_id, page_url) WHERE remote_conversation_id IS NULL`,约束无 conversation_id 时的 link 唯一性
27
28## 优先级
29
30中。当前 Gemini 的 conversation_id 提取大部分情况能成功,但极端情况下会导致对话关联错乱。