docs(debt): ③ 的消歧键订正为 agent+workspace(收 pi 场景C);新增 ④ 迁移静默失效

① ③ 订正(pi `43d2c9dd` 造场景C 反驳我"只按 agent",实测成立):
     场景A 跨 agent + 不同 ws                            ⇒ 三键皆 1 行 ✓
     场景B 跨 agent + 同  ws                             ⇒ 按 ws 2 行 ✗ / 按 agent 1 行 ✓ / 两把 1 行 ✓
     场景C 同 agent + 同 id + 两 ws(未来态,PK 加 ws 后合法)⇒ 按 agent **2 行** ✗ / 两把 1 行 ✓
   ⇒ 正确键 = aps.agent_name = s.from_agent AND aps.workspace = s.workspace
   且 sessions.workspace 列已存在(sqliteAddColumns 补的); 生产: 有 platform_id 的 9 条
   ⇒ workspace 非空 9/9、与 aps 一致 6/6(另 3 条镜像无此 id)⇒ 不需新加数据
   ★ 我一度想用 ORDER BY (aps.agent_name=s.from_agent) DESC LIMIT 1 替代"谓词入 ON",
     场景E(本侧由 e2 接管、镜像同名 id 只剩 e1 那行)实测取到 e1(错),
     而谓词入 ON 得 NULL ⇒ 退回 from_agent=e2(对,合文档 :388-390)⇒ 我的排序键想法撤回

② ④ 新增(本回合最重的发现): 改 PK 在**已部署库上静默不生效**
   ① init_sqlite.sql:407 / init.sql:371 都是 CREATE TABLE IF NOT EXISTS ⇒ 对已存在的表整条跳过
   ② migrate.go:33-40 每次启动逐条重跑 init DDL(cmd/server/main.go:39/48);
      addMissingColumns(:365) 只补列、不碰约束 ⇒ 补不了 PK
   ③ 实测: 旧 PK 库重跑含新 PK 的 DDL ⇒ rc=0 无报错,sqlite_master 里 PK 仍是旧的,
      再插「同 id 不同 ws」第二行 ⇒ 仍报 1555
   ④ 测试库走 t.TempDir()+Migrate ⇒ 每次全新建表 ⇒ 新 PK 生效 ⇒ 测试全绿;
      且全仓无任何 schema/PK 断言(sqlite_master/table_info grep=0)
   ⇒ 「改完 DDL、测试全绿、生产没变」完全静默:
     DELETE/JOIN 都改对了,但 PK 没变 ⇒ 同 id 跨 ws INSERT 撞 1555 + 无 ON CONFLICT
     + defer tx.Rollback() ⇒ 整个事务回滚 ⇒ 心跳持续成功而镜像永不再更新
     —— 比现在的间歇擦除更糟
   ⇒ 治法: 显式重建表(建新表含新 PK→INSERT SELECT→DROP→RENAME)+ 一条断言实际 PK 的判据

校验: go test ./internal/repo/ -run Debt -count=1 ⇒ PASS(余额 27,新条目渲染正常)
This commit is contained in:
2026-09-26 02:09:21 +08:00
parent 1da6eca7d3
commit 2912be45a9

File diff suppressed because one or more lines are too long