@@ -1390,6 +1390,16 @@ export function createStore(path) {
13901390 // 自己的机器上重跑 T0/T1 时补上,不猜(remote URL 拿不到就是拿不到)。
13911391 // choro_repo_paths 建表走 SCHEMA 的 CREATE TABLE IF NOT EXISTS,老库重开自动补。
13921392 addColumn ( "choro_repos" , "portable_key" , "ALTER TABLE choro_repos ADD COLUMN portable_key TEXT" ) ;
1393+ // 身份唯一(PR #679 评审):同一 portable_key 只许一行,否则并发登记会各自插入、
1394+ // 之后任意取其中一行。部分索引(键非空才约束)保证升格前的 NULL 行不受影响。
1395+ // 建索引可能因存量重复键失败(本功能尚未发布,概率趋零,但不能因此让插件起不来)
1396+ // ——失败时退成非唯一索引,upsert 的「插入撞唯一键→回读再更新」分支只在索引建成
1397+ // 时生效,退化的库上并发仍可能重复,这是启动可用性与约束强度之间的取舍。
1398+ try {
1399+ db . exec ( "CREATE UNIQUE INDEX IF NOT EXISTS idx_choro_repos_portable ON choro_repos(portable_key) WHERE portable_key IS NOT NULL;" ) ;
1400+ } catch {
1401+ db . exec ( "CREATE INDEX IF NOT EXISTS idx_choro_repos_portable ON choro_repos(portable_key);" ) ;
1402+ }
13931403
13941404 // v0.8.0 A1(issue #17)scope 隔离存储层:四列全部可空、不带 DEFAULT——存量行
13951405 // 零重写,读侧把 NULL 视为未标注(= 全局可见)。是否落值由 scopeEnabled 门控
@@ -5482,7 +5492,20 @@ export function createStore(path) {
54825492 let existing = portableKey
54835493 ? ( db . prepare ( "SELECT * FROM choro_repos WHERE portable_key = ?" ) . get ( portableKey ) ?? undefined )
54845494 : undefined ;
5485- if ( ! existing ) existing = choroRepoByRoot ( rootPath ) ;
5495+ if ( ! existing ) {
5496+ // 路径回退必须校验身份(PR #679 评审):两台主机的同一条绝对路径可能各自
5497+ // 检出不同仓库。若该路径已属另一身份的行,拿它复用会把对方的名字、基线、
5498+ // 键一起改掉,随后 T0 还会用本仓库的图替换对方的模块与 claims。无键的存量
5499+ // 行(升格前登记)仍允许按路径认领——那时候键还不存在,路径就是唯一线索。
5500+ const byRoot = choroRepoByRoot ( rootPath ) ;
5501+ if ( byRoot && portableKey && byRoot . portable_key != null && byRoot . portable_key !== portableKey ) {
5502+ throw new Error (
5503+ `choro repo path conflict: ${ rootPath } is already registered as ${ byRoot . portable_key } ; ` +
5504+ `refusing to re-register it as ${ portableKey } `
5505+ ) ;
5506+ }
5507+ existing = byRoot ;
5508+ }
54865509 if ( existing ) {
54875510 const rootTaken = db . prepare ( "SELECT id FROM choro_repos WHERE root_path = ? AND id != ?" )
54885511 . get ( rootPath , existing . id ) ;
@@ -5496,10 +5519,25 @@ export function createStore(path) {
54965519 }
54975520 return choroRepoByRoot ( nextRoot ) ;
54985521 }
5499- db . prepare (
5500- `INSERT INTO choro_repos (root_path, name, plan_fingerprint, git_baseline, created_at, updated_at, portable_key)
5501- VALUES (?, ?, ?, ?, ?, ?, ?)`
5502- ) . run ( rootPath , name , planFingerprint , gitBaseline , updatedAt , updatedAt , portableKey ) ;
5522+ try {
5523+ db . prepare (
5524+ `INSERT INTO choro_repos (root_path, name, plan_fingerprint, git_baseline, created_at, updated_at, portable_key)
5525+ VALUES (?, ?, ?, ?, ?, ?, ?)`
5526+ ) . run ( rootPath , name , planFingerprint , gitBaseline , updatedAt , updatedAt , portableKey ) ;
5527+ } catch ( err ) {
5528+ // 并发登记(PR #679 评审):两个进程可能都先查到「无记录」再各自插入。
5529+ // portable_key 的唯一索引会让后到者失败,此时重新按身份取行并走更新,
5530+ // 而不是把第二个写入变成用户可见的失败。
5531+ const message = String ( err ?. message ?? err ) ;
5532+ if ( ! portableKey || ! / U N I Q U E c o n s t r a i n t f a i l e d : c h o r o _ r e p o s \. p o r t a b l e _ k e y / i. test ( message ) ) throw err ;
5533+ const raced = db . prepare ( "SELECT * FROM choro_repos WHERE portable_key = ?" ) . get ( portableKey ) ;
5534+ if ( ! raced ) throw err ;
5535+ db . prepare (
5536+ `UPDATE choro_repos SET root_path = ?, name = ?, plan_fingerprint = ?, git_baseline = ?, updated_at = ?
5537+ WHERE id = ?`
5538+ ) . run ( rootPath , name , planFingerprint , gitBaseline , updatedAt , raced . id ) ;
5539+ return choroRepoByRoot ( rootPath ) ;
5540+ }
55035541 return choroRepoByRoot ( rootPath ) ;
55045542 }
55055543
0 commit comments