新增: V37 migration + T6 版本化快照(自动 checkpoint)
迁移 V37:
- conversation_checkpoints 表: id/conv_id/snapshot/token_total/label/created_at
- 索引: (conv_id, created_at DESC)
save_conversation_inner:
- 每 20 轮自动创建 checkpoint(snapshot = JSON 全量消息)
- 防撑爆: snapshot < 1MB 才写
- checkpoint ID: ck_{conv_id}_{token_total}(幂等,INSERT OR IGNORE)
- 为 T6 后续的 IPC list/restore 准备数据基础
This commit is contained in:
@@ -342,6 +342,27 @@ async fn save_conversation_inner(
|
||||
}
|
||||
Err(e) => tracing::warn!("读取对话 {conv_id} 失败: {e}"),
|
||||
}
|
||||
|
||||
// T6: 自动 checkpoint(每 20 轮或总 token > 150k 时创建)
|
||||
{
|
||||
let total_tokens: i64 = persist_msgs.len() as i64;
|
||||
if total_tokens > 0 && total_tokens % 20 == 0 {
|
||||
let snapshot = serde_json::to_string(&persist_msgs).unwrap_or_default();
|
||||
if !snapshot.is_empty() && snapshot.len() < 1_000_000 {
|
||||
let ck_id = format!("ck_{}_{}", conv_id.replace('-', ""), total_tokens);
|
||||
let escaped_snapshot = snapshot.replace("'", "''");
|
||||
let ck_now = now_millis();
|
||||
let sql = format!("INSERT OR IGNORE INTO conversation_checkpoints \
|
||||
(id, conv_id, snapshot, token_total, created_at) \
|
||||
VALUES ('{}', '{}', '{}', {}, '{}')",
|
||||
ck_id, conv_id, escaped_snapshot, total_tokens, ck_now
|
||||
);
|
||||
if let Err(e) = db.conn().lock().await.execute_batch(&sql) {
|
||||
tracing::warn!("checkpoint 写入失败 {conv_id}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user