Skip to content

Commit 9737ceb

Browse files
committed
fix(choro): 身份优先——路径回退核验身份、身份唯一索引、URL 端口与 scp 分隔符分开(PR #679 六条评审)
1 parent 1511e5b commit 9737ceb

13 files changed

Lines changed: 386 additions & 86 deletions

File tree

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## [Unreleased]
44

5-
- **仓库地志的身份与位置分家:`portable_key` + 每机位置表,双系统 / 双机共享同一个 memory.db 时地志第一次可以跨端复用(#581)**:mneme 把「库共享」做到了(`memoryDir` + WAL + busy_timeout),chorography 是唯一一处因为记了本机绝对路径而破坏这个前提的功能——Windows 端登记的行,macOS 端拼出来的路径不存在;再跑一次 T0 又多出第二行,从此不带 `repo` 参数的调用命中 repo-ambiguous;T1 的逐字符严格匹配更意味着**每端都必须先各自跑一次 T0**。修法按 issue 的最小方案落地,三个概念各就各位:**身份** = `choro_repos.portable_key`(新列,幂等迁移),取 `remote.origin.url` 归一化——协议头 / 尾斜杠 / 尾 `.git` / 大小写剥平,scp 形态(`git@host:path`)的冒号换 `/`,**userinfo 整体剥掉**(token 不进键:泄进库里是一回事,令牌轮换后同一仓库算出不同键是另一回事);无 remote 退化到**根提交哈希**(`git:<hash>`,跨副本恒定且与「碰巧同名的别的仓库」天然区分——issue 提的「退化为仓库名」不能用,目录名与 package name 重合率太高,第二次登记会吃掉别人的行,实现期在测试夹具上当场撞到)。**位置** = 新表 `choro_repo_paths(repo_key, host_id, root_path, last_seen_at)`,`host_id = os.hostname()`,T0/T1 登记时 upsert。**读侧**(`queries.js` 的 `localRootPath`)按当前 host 取本机路径、取不到回退身份行的 root_path——单机库两个值恒等、行为逐字节不变;本机没登记过时落进既有的 page-missing / unreadable 失败口径,不会拿别的机器的路径读出错误内容。**T1 的登记校验**从「root_path 逐字符相同」改成「本机路径命中**或**身份已登记」——同一仓库在另一台机器登记过,本机直接认领那一行富化,消灭「每端先各跑一次 T0」;认领同时落本机位置行。写路径(`/choro/run`、`/choro/t1` 后台任务、`/choro` 命令的过期判定)同样改吃本机根。**两个刻意的取舍**:① 同键登记收敛到同一行,但 root_path 的 UPDATE 前先查目标值是否被别的行占着(被占保留原值、位置走位置表)——升格期已跨端登记出的重复行**不合并**(页面与 claims 挂在 repo_id 上,机器间合并行等于改写归属),强行改会撞 `idx_choro_repos_root` 直接抛;② 升格存量行的键留 NULL,由它自己的机器重跑 T0/T1 时补上,不猜。已知边界:kb 导入的 choro_page source 是导入当时的绝对路径快照,跨端复用时会失配,另账处理。回归测试 +10(URL 归一化形态表含凭据剥离 / 无 remote 退化链与恒定性 / 旧库补列补表且重开幂等 / 位置表同键同机覆写 / 同键跨端登记收敛同一行且 created_at 不动 / 升格期 root 被占不强改 / 读侧三 host 各解析各的 / **T1 跨端认领端到端**:克隆检出直接富化身份行并登记本机位置),另把四个「同库登记两个相同夹具」的既有用例显式区分开(无 remote 键是根提交哈希,同秒提交的相同内容会同键收敛——这本身就是退化链设计的防误并性质)。变异自证 3 例(删 `upsertChoroRepoPath`/`choroRepoPathFor` 的 return 出口 → store 用例 3 红;读侧 `localRootPath` 去掉位置行解析 → queries 用例红;T1 去掉 byKey 认领 → 端到端用例红),恢复后全绿。
5+
- **仓库地志的身份与位置分家:`portable_key` + 每机位置表,双系统 / 双机共享同一个 memory.db 时地志第一次可以跨端复用(#581)**:mneme 把「库共享」做到了(`memoryDir` + WAL + busy_timeout),chorography 是唯一一处因为记了本机绝对路径而破坏这个前提的功能——Windows 端登记的行,macOS 端拼出来的路径不存在;再跑一次 T0 又多出第二行,从此不带 `repo` 参数的调用命中 repo-ambiguous;T1 的逐字符严格匹配更意味着**每端都必须先各自跑一次 T0**。修法按 issue 的最小方案落地,三个概念各就各位:**身份** = `choro_repos.portable_key`(新列,幂等迁移),取 `remote.origin.url` 归一化——协议头 / 尾斜杠 / 尾 `.git` / 大小写剥平,scp 形态(`git@host:path`)的冒号换 `/`,**userinfo 整体剥掉**(token 不进键:泄进库里是一回事,令牌轮换后同一仓库算出不同键是另一回事);无 remote 退化到**根提交哈希**(`git:<hash>`,跨副本恒定且与「碰巧同名的别的仓库」天然区分——issue 提的「退化为仓库名」不能用,目录名与 package name 重合率太高,第二次登记会吃掉别人的行,实现期在测试夹具上当场撞到)。**位置** = 新表 `choro_repo_paths(repo_key, host_id, root_path, last_seen_at)`,`host_id = os.hostname()`,T0/T1 登记时 upsert。**读侧**(`queries.js` 的 `localRootPath`)按当前 host 取本机路径、取不到回退身份行的 root_path——单机库两个值恒等、行为逐字节不变;本机没登记过时落进既有的 page-missing / unreadable 失败口径,不会拿别的机器的路径读出错误内容。**T1 的登记校验**从「root_path 逐字符相同」改成「本机路径命中**或**身份已登记」——同一仓库在另一台机器登记过,本机直接认领那一行富化,消灭「每端先各跑一次 T0」;认领同时落本机位置行。写路径(`/choro/run`、`/choro/t1` 后台任务、`/choro` 命令的过期判定)同样改吃本机根。**两个刻意的取舍**:① 同键登记收敛到同一行,但 root_path 的 UPDATE 前先查目标值是否被别的行占着(被占保留原值、位置走位置表)——升格期已跨端登记出的重复行**不合并**(页面与 claims 挂在 repo_id 上,机器间合并行等于改写归属),强行改会撞 `idx_choro_repos_root` 直接抛;② 升格存量行的键留 NULL,由它自己的机器重跑 T0/T1 时补上,不猜。已知边界:kb 导入的 choro_page source 是导入当时的绝对路径快照,跨端复用时会失配,另账处理。回归测试 +10(URL 归一化形态表含凭据剥离 / 无 remote 退化链与恒定性 / 旧库补列补表且重开幂等 / 位置表同键同机覆写 / 同键跨端登记收敛同一行且 created_at 不动 / 升格期 root 被占不强改 / 读侧三 host 各解析各的 / **T1 跨端认领端到端**:克隆检出直接富化身份行并登记本机位置),另把四个「同库登记两个相同夹具」的既有用例显式区分开(无 remote 键是根提交哈希,同秒提交的相同内容会同键收敛——这本身就是退化链设计的防误并性质)。变异自证 3 例(删 `upsertChoroRepoPath`/`choroRepoPathFor` 的 return 出口 → store 用例 3 红;读侧 `localRootPath` 去掉位置行解析 → queries 用例红;T1 去掉 byKey 认领 → 端到端用例红),恢复后全绿。**评审补丁(PR #679,六条 Major 全部成立)**:① **路径回退要核验身份**——两台主机的同一条绝对路径可能各自检出不同仓库,`upsertChoroRepo` / T1 的登记校验、读侧 `localRootPath` 都改成「身份优先,路径回退只接受无键存量行或键一致的行」;核验用 `portableKeyFor`(一次 git 读取,按「键+路径」记忆化),不符时读侧返回空串并按 `page-missing` / 不可读上报,绝不 `join("", rel)` 去读进程 CWD;`claimDrift` 的 `hasT1` 同样加了空根守卫。② **身份唯一**:`portable_key` 加部分唯一索引(`WHERE portable_key IS NOT NULL`),插入撞唯一键时回读同一身份行改走更新——并发登记不再产生重复行;索引建失败(存量重复键)退成非唯一索引,不让插件起不来。③ **URL 解析分清端口与 scp 分隔符**:带协议头时 authority 的冒号是端口(默认端口 ssh 22 / https 443 / git 9418 剥掉,其余保留),只有 scp 形态的冒号才是路径分隔符;④ **userinfo 只在 authority 段内剥**,仓库路径里的 `@`(`repo@stable`)不再把主机与路径一起切掉。⑤ `repoByCwd` 的最长前缀改为比较候选自身路径长度(原实现拿身份行里那条外地路径比长度,内层本机路径永远赢不了)。新增回归 5 条(端口 vs 路径段 / 路径里的 @ / 同路径另一身份拒绝且原行不变 / 回退目录身份不符报 page-missing / 前缀比较用候选长度),变异自证 6 例逐条变红。
66

77
## [0.14.0-beta.3] - 2026-11-12
88
- **品牌视觉换新:README 头图与插件列表图标**。三处 README(根、`dsh-mneme/`、`mcp/`)顶部改用 `images/mneme-banner-hero.webp`(1672×941,282 KiB)——原图 2.1 MiB 的 PNG 会让 `images/` 目录体积翻倍。根目录用相对路径,两个子目录用 raw 绝对地址:npm 详情页不解析相对图片路径,`../images/...` 在 npmjs.com 上必然裂图。

‎dsh-mneme/lib/store.js‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 || !/UNIQUE constraint failed: choro_repos\.portable_key/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

‎dsh-mneme/lib/wiki/portable.js‎

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,45 @@ export function hostId() {
1919
* remote URL → 归一化身份串。目标形态:`host/path`(全小写)。
2020
* - 协议头(https:// / ssh:// / git://)剥掉;
2121
* - 尾部 `/` 与 `.git` 剥掉;
22-
* - scp 形态 `git@host:path` 的冒号换成 `/`——判据是冒号在最后一个 `@` 之后、
23-
* 第一个 `/` 之前;https 的 userinfo(`user:token@host`)冒号在 `@` 之前,
24-
* **不能动**:token 换成 `/` 既毁匹配、还会把凭据的形状带进键里;
25-
* - userinfo(含 token)整体剥掉:凭据不进键——泄进库里是一回事,令牌轮换
26-
* 后同一仓库算出不同键是另一回事,两个都不可接受。
22+
* - **URL 与 scp 分开解析**(PR #679 评审):带 `://` 的形态里 authority 的冒号
23+
* 是**端口**(`ssh://host:2222/team/repo` 的 2222 不是路径段),只有 scp 形态
24+
* (`git@host:path`,无协议头)的冒号才是路径分隔符。混着处理会把
25+
* `ssh://host/2222/team/repo` 与 `ssh://host:2222/team/repo` 算成同一个身份。
26+
* 默认端口(ssh 22 / https 443 / git 9418)剥掉,其余端口保留——不同端口的
27+
* 两个服务本来就可能承载不同仓库;
28+
* - userinfo(含 token)**只在 authority 段内**剥掉:凭据不进键(泄进库里是
29+
* 一回事,令牌轮换后同一仓库算出不同键是另一回事),但仓库路径里的 `@`
30+
* (`team/repo@stable`)不是 userinfo,不能拿全串的 lastIndexOf 去切。
2731
*/
2832
export function normalizeGitUrl(rawUrl) {
29-
let s = String(rawUrl ?? "").trim();
30-
if (!s) return null;
31-
s = s.replace(/^[a-z][a-z0-9+.-]*:\/\//i, "");
32-
s = s.replace(/\/+$/, "");
33-
s = s.replace(/\.git$/i, "");
34-
const firstSlash = s.indexOf("/");
35-
const authority = firstSlash === -1 ? s : s.slice(0, firstSlash);
36-
const at = authority.lastIndexOf("@");
37-
const colon = authority.indexOf(":", at + 1);
38-
if (colon !== -1) {
39-
s = authority.slice(0, colon) + "/" + authority.slice(colon + 1)
40-
+ (firstSlash === -1 ? "" : s.slice(firstSlash));
33+
const raw = String(rawUrl ?? "").trim();
34+
if (!raw) return null;
35+
const schemeMatch = /^([a-z][a-z0-9+.-]*):\/\//i.exec(raw);
36+
if (schemeMatch) {
37+
const scheme = schemeMatch[1].toLowerCase();
38+
let s = raw.slice(schemeMatch[0].length).replace(/\/+$/, "").replace(/\.git$/i, "");
39+
const slash = s.indexOf("/");
40+
let authority = slash === -1 ? s : s.slice(0, slash);
41+
const path = slash === -1 ? "" : s.slice(slash);
42+
const at = authority.lastIndexOf("@");
43+
if (at !== -1) authority = authority.slice(at + 1);
44+
const portMatch = /:(\d+)$/.exec(authority);
45+
if (portMatch) {
46+
const defaultPort = { ssh: "22", "git+ssh": "22", https: "443", http: "80", git: "9418" }[scheme];
47+
if (portMatch[1] === defaultPort) authority = authority.slice(0, portMatch.index);
48+
}
49+
const out = (authority + path).toLowerCase();
50+
return out || null;
4151
}
42-
const at2 = s.lastIndexOf("@");
43-
const hostStart = at2 === -1 ? 0 : at2 + 1;
44-
const out = s.slice(hostStart).toLowerCase();
52+
// scp 形态:`[user@]host:path`;没有协议头,冒号是路径分隔符。
53+
let s = raw.replace(/\/+$/, "").replace(/\.git$/i, "");
54+
const at = s.indexOf("@");
55+
if (at !== -1) s = s.slice(at + 1);
56+
const colon = s.indexOf(":");
57+
if (colon === -1) return s.toLowerCase() || null;
58+
const host = s.slice(0, colon);
59+
const path = s.slice(colon + 1);
60+
const out = `${host}/${path}`.toLowerCase();
4561
return out || null;
4662
}
4763

0 commit comments

Comments
 (0)