ccswitchmulti icon indicating copy to clipboard operation
ccswitchmulti copied to clipboard

WebDAV/S3 同步下载失败:session_log_sync.file_path UNIQUE constraint failed

Open zhushihao opened this issue 3 months ago • 3 comments

问题描述

WebDAV/S3 双向同步的「下载」操作失败,报错:

数据库错误: 重写同步字段 session_log_sync.file_path 失败: UNIQUE constraint failed: session_log_sync.file_path

复现步骤

  1. 设备 A(macOS):配置 WebDAV 同步,执行「立即上传」
  2. 设备 B(Windows):配置同一 WebDAV 地址,执行「立即下载」
  3. 下载导入失败,报上述错误,远端配置无法拉取

根因分析

session_log_sync 表(记录 Codex 会话日志同步进度的设备本地状态表,file_path 为主键)未列入 SYNC_SKIP_TABLES / SYNC_PRESERVE_TABLES(src-tauri/src/database/backup.rs:76-91),因此参与跨设备同步导出/导入。

下载导入时 localize_imported_sync_snapshot 会调用 rewrite_text_columns_for_sync(backup.rs:232-311),对所有 TEXT 列做路径本地化重写(%HOME% 占位符 → 本机 home 路径),执行:

UPDATE "session_log_sync" SET "file_path" = ?1 WHERE rowid = ?2

当远端快照中 session_log_sync 存在多行、且本地化后 file_path 出现重复时(例如同一会话文件同时存在 %HOME% 开头与绝对路径开头两行,或跨平台路径归一化后重合),UPDATE 违反 file_path 主键唯一约束,整个导入事务失败。

建议修复

session_log_sync 是设备本地运行时状态(记录本机会话文件的扫描/同步进度),与 proxy_request_logs、usage_daily_rollups 同类,不应跨设备同步。建议将其加入:

const SYNC_SKIP_TABLES: &[&str] = &[
    // ...
    "session_log_sync",
];
const SYNC_PRESERVE_TABLES: &[&str] = &[
    // ...
    "session_log_sync",
];

zhushihao avatar Aug 22 '26 07:08 zhushihao

v3.19.2-15 核对:仍未修复 + 本地验证方案

最新版核对(v3.19.2-15, 2026-08-22 发布)

确认 v3.19.2-15 未修复:

  • src-tauri/src/database/backup.rs 的 SYNC_SKIP_TABLES / SYNC_PRESERVE_TABLES 仍不含 session_log_sync;
  • localize_imported_sync_snapshot → rewrite_text_columns_for_sync 仍无条件遍历所有表做路径本地化重写(UPDATE "session_log_sync" SET "file_path" = ? WHERE rowid = ?),跨平台路径归一化后 file_path 主键冲突,下载导入整体失败。

本地验证方案(已在 fix/28-reasoning 分支实测通过)

session_log_sync 是设备本地运行时状态(记录本机会话文件的扫描/同步断点),与 proxy_request_logs 同类,不应跨设备同步。两处改动:

1. 加入同步排除清单(src-tauri/src/database/backup.rs:76-91):

const SYNC_SKIP_TABLES: &[&str] = &[
    // ...
    "session_log_sync",
];
const SYNC_PRESERVE_TABLES: &[&str] = &[
    // ...
    "session_log_sync",
];

2. 路径重写跳过本地状态表(rewrite_text_columns_for_sync,backup.rs 约 260 行)——只加排除清单不够,因为 localize_imported_sync_snapshot 在导入时对所有表做路径重写,session_log_sync 只要在远端快照里有数据就会撞主键:

for table in tables {
    if SYNC_SKIP_TABLES.contains(&table.as_str())
        || SYNC_PRESERVE_TABLES.contains(&table.as_str())
    {
        continue;
    }
    // ...
}

实测结果

  • 修复前:Mac 上传 → Windows 下载 → 重写同步字段 session_log_sync.file_path 失败: UNIQUE constraint failed;
  • 修复后:Windows 下载成功,双端双向同步正常;session_log_sync 不再参与导出与导入路径改写。

zhushihao avatar Aug 22 '26 12:08 zhushihao

此 issue 已超过 60 天没有活动,已被标记为 stale。 如果此问题仍然存在,请回复以保持 issue 打开状态,否则将在 14 天后自动关闭。

This issue has been automatically marked as stale because it has not had activity for 60 days. Please reply to keep it open, otherwise it will be automatically closed in 14 days.

github-actions[bot] avatar Oct 22 '26 03:10 github-actions[bot]

此 issue 因长时间无活动已被自动关闭。如果问题仍然存在,欢迎重新打开。

This issue has been automatically closed due to inactivity. If the problem persists, feel free to reopen it.

github-actions[bot] avatar Nov 05 '26 05:11 github-actions[bot]