Skip to content

Commit b59b56d

Browse files
committed
fix(choro): 收掉 review 指出的 12 处溯源漏判
claims - diff 路径要解 git 的 C 风格八进制转义:core.quotepath 默认开, 非 ASCII 文件名带引号加转义,不解就跟 source_path 对不上,这类 文件上的结论永远判不成 touched。 - diffSince 显式传 --no-renames:--unified=0 下 git 仍会自动开重命名 检测,输出 similarity/rename 而不产生 hunk,旧路径上的结论会被判成 untouched。原注释说「不传 -M 就按删旧+增新处理」是错的。 - overlaps 处理纯插入:oldCount === 0 时这个 hunk 只插行、不动旧行, 但插在结论行区间里就是改了这一页,按 [start, end) 判相交。 queries - 验证过却没记下基线(verified_at 有、verified_baseline 空)不再回退 全仓基线,直接判不了:回退等于把一次没留凭据的验证说成有凭据。 - clean 无条件排除 inconsistentPages:repoAtHead 短路时原先不查, 有语义段却没 claims 的页会被仓库恰好在 HEAD 上掩盖过去。 t0 - collectT1Loss 读页失败不再当「没有」:读不出来就无法确认那页有没有 语义段,进 unreadable 并让 guardT1Loss 拒绝刷新,避免自动刷新把读 不到的语义段连带 claims 一起清掉。 t1 - DSH_CLI_JS 显式指定但路径不存在时直接失败,不回退到其它候选:回退会 让人以为设置生效了。 - 回退分支的失败信息补上已探测的候选清单。 choro cli - --dsh-cli 转成绝对路径再赋给 DSH_CLI_JS,否则按 cwd 做 existsSync 会判错。 client - checkDrift 加请求代次,仓库重载后在途结果不再写 state。 - 面板显示 autoRefreshError:服务端一直回这个字段,面板从来没读, 自动刷新失败对用户是隐形的。它与 autoRefreshSkipped 是两个语义, 不合并。 - 汇总横幅把 inconsistentPages 算进不可判定,三方都 0 才说干净。 - git-unreadable 用专门的文案,不再复用 anchorStale。 测试 +8,附变异自证:去掉 --no-renames、overlaps 回退、t0 catch 回退、 queries 删 inconsistentPages 判定、t1 删显式短路,五处改动各被一条 测试杀掉。
1 parent 5555e31 commit b59b56d

14 files changed

Lines changed: 453 additions & 59 deletions

File tree

‎dsh-mneme/bin/dsh-mneme-choro.mjs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ async function main() {
146146
// --apply 才真开会话花钱。三重上限与降级表在 createT1 里。
147147
// --dsh-cli 走环境变量而不是新参数:探测层(resolveCliJs)本来就认 DSH_CLI_JS,
148148
// 再加一条平行的传参链只会让「两种说法哪个赢」变成新的坑。
149-
if (flags["dsh-cli"]) process.env.DSH_CLI_JS = flags["dsh-cli"];
149+
//
150+
// 必须 resolve 成绝对路径:resolveCliJs 用 existsSync 判存在,而 existsSync 是
151+
// 相对**当时进程 cwd** 解析的——T1 会 spawn 子进程到仓库目录去跑,相对路径进来
152+
// 就变成对着另一个目录找文件,原本存在的 cli 会被判成不存在。
153+
if (flags["dsh-cli"]) process.env.DSH_CLI_JS = resolve(flags["dsh-cli"]);
150154
const t1 = createT1({ store });
151155
const receipt = await t1.runT1({
152156
repoRoot: repoPath,

‎dsh-mneme/lib/client.js‎

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ window.__ModuleLoader__.load({
380380
// 做了会丢东西」,而不是「刷新失败」——失败是另一种情况,另一种文案。
381381
"wiki.autoRefresh.skipped": "自动刷新这次没有做:这仓库有 {n} 页已经富化过,重跑骨架会把语义段和结论一起清掉。要新快照请手动点「重建骨架」(会先告诉你丢哪几页)。",
382382
"wiki.autoRefresh.skippedGeneric": "自动刷新这次没有做:这仓库已有富化过的页,重跑骨架会把语义段和结论一起清掉。要新快照请手动点「重建骨架」。",
383+
"wiki.autoRefresh.failed": "自动刷新试过但失败了",
383384
"wiki.run.pathRequired": "先填仓库目录的绝对路径",
384385
"wiki.run.failed": "生成失败",
385386
"wiki.run.notAbsolute": "要给绝对路径(例如 D:\\code\\my-repo):相对路径会按服务端进程的当前目录解析,同一个请求换个启动位置就落到别的目录。",
@@ -1508,6 +1509,7 @@ window.__ModuleLoader__.load({
15081509
// because doing it would lose something", never as "refresh failed".
15091510
"wiki.autoRefresh.skipped": "Auto-refresh was skipped: {n} page(s) in this repository are already enriched, and rebuilding the skeleton would wipe their prose and claims. Click Rebuild skeleton yourself if you want a new snapshot (it tells you which pages it drops).",
15101511
"wiki.autoRefresh.skippedGeneric": "Auto-refresh was skipped: this repository already has enriched pages, and rebuilding the skeleton would wipe their prose and claims. Click Rebuild skeleton yourself if you want a new snapshot.",
1512+
"wiki.autoRefresh.failed": "Auto-refresh was attempted but failed",
15111513
"wiki.run.pathRequired": "Enter the repository's absolute path first",
15121514
"wiki.run.failed": "Generate failed",
15131515
"wiki.run.notAbsolute": "Give an absolute path (for example /home/me/code/my-repo): relative paths resolve against the server process's working directory, so the same request can land somewhere else depending on how the host was started.",
@@ -12653,13 +12655,22 @@ window.__ModuleLoader__.load({
1265312655
// autoRefreshNote:读页时服务端说「这次自动刷新我主动没做」。它不是错误,
1265412656
// 所以不塞进 error、也不进 toast(toast 2.5s 就没了,用户看不到原因)。
1265512657
autoRefreshNote: null,
12658+
// autoRefreshError:服务端**真的**试过刷新但失败了。与上面那条严格分开:
12659+
// 把「主动没做」和「做了没成」合成一句,面板就会把一次正确的拒绝说成故障,
12660+
// 也会把一次真故障说成没事(src/api.js 两个字段分开就是为了这个)。
12661+
autoRefreshError: null,
1265612662
// 证据复核(Grounded Claims)的两样状态:drift 是 /choro/claims 的
1265712663
// 回体,driftPhase 区分「还没查」「查着」「查出三种结局」。
1265812664
drift: null, driftPhase: "idle"
1265912665
});
1266012666
// T1 跑完要重读当前仓库的模块树,但轮询的闭包里拿不到最新的 state.repo:
1266112667
// 用 ref 记住加载成功的仓库 id,避免把整棵树塞进 effect 依赖里。
1266212668
const repoIdRef = useRef(null);
12669+
// 证据复核的请求代次:切仓库 / 重新 load 时 +1。checkDrift 发出前记下当时的
12670+
// 代次,回来时代次已经变了就直接丢弃结果——否则「在甲仓库上发的 diff,在切到
12671+
// 乙仓库之后才回来」会把甲仓库的「需要复核」写到乙仓库的页签下,而用户看到的
12672+
// 是一个根本不属于当前页面的判定(比不显示更糟)。
12673+
const driftGenRef = useRef(0);
1266312674
// apiFetch 是裸 fetch:非 2xx 不抛错,返回 Response。这里统一解 JSON 并把
1266412675
// 状态码带回来——409 disabled(用户没开)/ 503 unavailable(装配缺件)
1266512676
// 各有各的空态文案(#497 的语义分叉在这里接住)。
@@ -12708,7 +12719,9 @@ window.__ModuleLoader__.load({
1270812719
repoIdRef.current = loaded && loaded.id != null ? loaded.id : ref;
1270912720
// 切仓库时证据复核的旧结果必须清掉:它是按上一个仓库的 diff 算的,
1271012721
// 留着就会把甲仓库的「需要复核」挂在乙仓库的页签下。
12711-
setState((s) => ({ ...s, phase: "ready", repos, repo: loaded, modules: mod.data.modules ?? [], edges: mod.data.edges ?? [], page: null, selected: null, drift: null, driftPhase: "idle", autoRefreshNote: null }));
12722+
// 代次 +1 之后,任何在途的 checkDrift 结果都会被丢弃(见 driftGenRef)。
12723+
driftGenRef.current += 1;
12724+
setState((s) => ({ ...s, phase: "ready", repos, repo: loaded, modules: mod.data.modules ?? [], edges: mod.data.edges ?? [], page: null, selected: null, drift: null, driftPhase: "idle", autoRefreshNote: null, autoRefreshError: null }));
1271212725
} catch (err) {
1271312726
setState((s) => ({ ...s, phase: "error", error: String(err?.message ?? err) }));
1271412727
}
@@ -12721,16 +12734,19 @@ window.__ModuleLoader__.load({
1272112734
const checkDrift = useCallback(async () => {
1272212735
const ref = repoIdRef.current;
1272312736
if (ref == null) return;
12724-
setState((s) => ({ ...s, driftPhase: "loading" }));
12737+
const gen = driftGenRef.current;
12738+
const writeIfCurrent = (patch) => setState((s) => (driftGenRef.current === gen ? { ...s, ...patch } : s));
12739+
writeIfCurrent({ driftPhase: "loading" });
12740+
// 只写「还是同一次请求、还是同一个仓库」的结果。
1272512741
try {
1272612742
const { status, data } = await readJson(await apiFetch(`/api/dsh-mneme/choro/claims?repo=${encodeURIComponent(ref)}`));
1272712743
if (status !== 200 || data.error) {
12728-
setState((s) => ({ ...s, driftPhase: "failed", drift: { error: data.error ?? `claims ${status}` } }));
12744+
writeIfCurrent({ driftPhase: "failed", drift: { error: data.error ?? `claims ${status}` } });
1272912745
return;
1273012746
}
12731-
setState((s) => ({ ...s, driftPhase: "done", drift: data }));
12747+
writeIfCurrent({ driftPhase: "done", drift: data });
1273212748
} catch (err) {
12733-
setState((s) => ({ ...s, driftPhase: "failed", drift: { error: String(err?.message ?? err) } }));
12749+
writeIfCurrent({ driftPhase: "failed", drift: { error: String(err?.message ?? err) } });
1273412750
}
1273512751
}, []);
1273612752
const openPage = useCallback(async (slug) => {
@@ -12749,7 +12765,12 @@ window.__ModuleLoader__.load({
1274912765
? t("wiki.autoRefresh.skipped").replace("{n}", String(data.repo.enrichedPages))
1275012766
: (data.autoRefreshSkipNote || t("wiki.autoRefresh.skippedGeneric")))
1275112767
: null;
12752-
setState((s) => ({ ...s, page: data.page, autoRefreshNote: skipped }));
12768+
// 真失败要单独带上:刷新没成时页面还是旧的、徽章还亮着,不说出来
12769+
// 就是「点了、看起来正常、其实是上次的内容」。
12770+
const refreshError = typeof data.autoRefreshError === "string" && data.autoRefreshError
12771+
? data.autoRefreshError
12772+
: null;
12773+
setState((s) => ({ ...s, page: data.page, autoRefreshNote: skipped, autoRefreshError: refreshError }));
1275312774
} catch (err) {
1275412775
setState((s) => ({ ...s, page: { error: String(err?.message ?? err) } }));
1275512776
}
@@ -12787,6 +12808,7 @@ window.__ModuleLoader__.load({
1278712808
rebuilding: false,
1278812809
// 重建已经真的发生了,那条「自动刷新没做」的提示就该退场。
1278912810
autoRefreshNote: null,
12811+
autoRefreshError: null,
1279012812
rebuildNote: `${t("wiki.rebuild.done")}: ${r.moduleCount ?? "?"} ${t("wiki.rebuild.modules")} / ${r.pageCount ?? "?"} ${t("wiki.rebuild.pages")}${loss ? ` — ${loss}` : ""}`
1279112813
}));
1279212814
await load(repo.id);
@@ -13002,12 +13024,19 @@ window.__ModuleLoader__.load({
1300213024
const driftDone = state.driftPhase === "done" && drift && !drift.error;
1300313025
const driftClean = driftDone && drift.clean === true;
1300413026
const driftAnchorStale = driftDone && drift.anchorStale === true;
13027+
// 「git 读不到」与「基线提交没了」是两种完全不同的处境:前者是仓库不是 git 目录
13028+
// 或不可读(去修环境),后者是 force-push/浅克隆把起点截断了(去重建骨架)。
13029+
// 两句 i18n 早就写好了,只是原来没接上,一律说成后者。
13030+
const driftGitUnreadable = driftAnchorStale && drift.reason === "git-unreadable";
1300513031
const driftTotals = (driftDone && drift.totals) || null;
1300613032
const driftRisk = driftTotals ? driftTotals.atRiskClaims : 0;
13007-
// 「比不了」的页数(验证基线认不出 + 从来没记过)单独计数:它们一条都不判,
13008-
// 不能混进「0 条需要复核」里当成没事。
13033+
// 「比不了」的页数(验证基线认不出 + 从来没记过 + 有语义段却没 claims)单独计数:
13034+
// 它们一条都不判,不能混进「0 条需要复核」里当成没事。inconsistentPages 原来
13035+
// 完全没进这个汇总,于是「所有页都是残局」也会显示成「这次改动没碰到任何结论」。
1300913036
const driftUndecidable = driftTotals
13010-
? (driftTotals.baselineUnknownPages || 0) + (driftTotals.anchorStalePages || 0)
13037+
? (driftTotals.baselineUnknownPages || 0)
13038+
+ (driftTotals.anchorStalePages || 0)
13039+
+ (driftTotals.inconsistentPages || 0)
1301113040
: 0;
1301213041
// 提交号显示前 7 位就够认,长 sha 会把这一行挤爆(完整值仍在 title 里)。
1301313042
const shortSha = (sha) => String(sha ?? "").slice(0, 7);
@@ -13143,12 +13172,16 @@ window.__ModuleLoader__.load({
1314313172
// 是一个仍然成立的状态(库里还有富化页),不是一次性的动作回执。
1314413173
state.autoRefreshNote && h("div", { className: "mneme-wikidriftbar mneme-wikidriftbar--warn", role: "status" },
1314513174
state.autoRefreshNote),
13175+
// 「试过刷新但失败了」单独一条:它不是无事发生,也不是「主动没做」。
13176+
// 旧实现在这里整段丢字段,用户的感受就是「点了没反应」。
13177+
state.autoRefreshError && h("div", { className: "mneme-wikidriftbar mneme-wikidriftbar--warn", role: "status" },
13178+
`${t("wiki.autoRefresh.failed")}: ${state.autoRefreshError}`),
1314613179
// 证据复核回执条:三种结局三种说法,「比对失败」单独一条 --
1314713180
// 它既不是「没事」也不是「有风险」,混进任何一边都是撒谎。
1314813181
state.driftPhase === "failed" && h("div", { className: "mneme-wikidriftbar mneme-wikidriftbar--warn", role: "status" },
1314913182
`${t("wiki.drift.blocked")}: ${(state.drift && state.drift.error) || ""}`),
1315013183
driftDone && driftAnchorStale && h("div", { className: "mneme-wikidriftbar mneme-wikidriftbar--warn", role: "status" },
13151-
t("wiki.drift.anchorStale")),
13184+
driftGitUnreadable ? t("wiki.drift.gitUnreadable") : t("wiki.drift.anchorStale")),
1315213185
driftDone && !driftAnchorStale && driftClean && h("div", { className: "mneme-wikidriftbar", role: "status" },
1315313186
t("wiki.drift.clean")),
1315413187
driftDone && !driftAnchorStale && !driftClean && h("div", { className: "mneme-wikidriftbar", role: "status" },

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,36 @@
1212
// 只算「相对上次生成基线改了什么」,不判断代码本体——结论要留给读它的人。
1313
import { execFileSync } from "node:child_process";
1414

15+
// git 对「含非 ASCII / 控制字符」的路径会加双引号并按 C 风格转义(core.quotepath 默认
16+
// 就是开的),例如中文名会写成 "a/\344\270\255\346\226\207.js"。只脱引号不解转义的话,
17+
// 归一出来的字符串永远对不上 choro_claims.source_path,这些文件上的结论就再也判不成
18+
// touched —— **漏判**比误判更难发现,因为它看起来一切正常。
19+
const GIT_PATH_ESCAPES = { a: "\x07", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v", '"': '"', "\\": "\\" };
20+
21+
function unescapeQuotedGitPath(s) {
22+
if (!s.includes("\\")) return s;
23+
const bytes = [];
24+
const pushStr = (str) => { for (const b of Buffer.from(str, "utf8")) bytes.push(b); };
25+
for (let i = 0; i < s.length; i += 1) {
26+
const ch = s[i];
27+
if (ch !== "\\" || i === s.length - 1) { pushStr(ch); continue; }
28+
const octal = /^[0-7]{3}/.exec(s.slice(i + 1));
29+
if (octal) { bytes.push(parseInt(octal[0], 8) & 0xff); i += 3; continue; }
30+
const escaped = GIT_PATH_ESCAPES[s[i + 1]];
31+
if (escaped !== undefined) { pushStr(escaped); i += 1; continue; }
32+
pushStr(ch);
33+
}
34+
return Buffer.from(bytes).toString("utf8");
35+
}
36+
1537
// 路径归一:choro_claims.source_path 存的是仓库相对路径(正斜杠);git 侧也是正斜杠,
16-
// 但 `--- a/x` `+++ b/x` 带前缀,可能需要去掉。
38+
// 但 `--- a/x` `+++ b/x` 带前缀,可能需要去掉。转义只出现在被引号包起来的那种写法里,
39+
// 所以解转义严格跟着「这次是否脱过引号」走——不能对未加引号的路径无条件解,
40+
// 否则 Windows 风格的反斜杠会被当成转义序列吃掉。
1741
function normDiffPath(p) {
1842
let s = String(p ?? "").trim();
19-
if (s.startsWith('"') && s.endsWith('"')) s = s.slice(1, -1);
43+
const quoted = s.length >= 2 && s.startsWith('"') && s.endsWith('"');
44+
if (quoted) s = unescapeQuotedGitPath(s.slice(1, -1));
2045
if (s.startsWith("a/") || s.startsWith("b/")) s = s.slice(2);
2146
return s.replace(/\\/g, "/");
2247
}
@@ -28,8 +53,11 @@ function normDiffPath(p) {
2853
* 因为 hunk 头必须匹配完整形状。两个计数都要——旧侧计数是新旧行坐标的唯一来源,
2954
* claims 的行号记在**旧侧**(写 claim 时那一版就是 HEAD)。
3055
*
31-
* 不传 -M:重命名按「删旧 + 增新」处理,旧路径上的 claim 落进 deleted 是对的
32-
* (锚点跟着文件走了,得重新指),而新路径没有 claim 自然也不会误报。
56+
* 不传 -M 还不够:git 在 `--unified=0` 下**仍会**自动开重命名检测(实测输出
57+
* `similarity index 100% / rename from / rename to`,既不出 hunk 也不进 /dev/null),
58+
* 于是重命名后旧路径上的 claim 会被判成「没事」。必须显式 `--no-renames`,让重命名
59+
* 按「删旧 + 增新」处理:旧路径上的 claim 落进 deleted 是对的(锚点跟着文件走了,得
60+
* 重新指),新路径没有 claim 自然也不会误报。
3361
*
3462
* 返回 { files: Map<path, [{oldStart, oldCount, newStart, newCount}]>, deleted: string[] }。
3563
*/
@@ -76,7 +104,10 @@ export function diffSince(repoRoot, baseline, head) {
76104
try {
77105
const text = execFileSync(
78106
"git",
79-
["diff", "--unified=0", "--no-color", "--no-ext-diff", `${baseline}..${head}`],
107+
["diff", "--unified=0", "--no-color", "--no-ext-diff", "--no-renames", `${baseline}..${head}`],
108+
// --no-renames 是必需的,不是保险:git 会按配置自动开重命名检测(实测在
109+
// --unified=0 下也开),一旦识别成 rename 就既不产生 hunk、也不产生 /dev/null,
110+
// 旧路径上的 claim 会被静默判成 untouched。见本函数上方 parseDiffRanges 的说明。
80111
// stdio 显式接管 stderr:execFileSync 默认把子进程 stderr 直接漏到父进程,
81112
// 「不是 git 仓库」那条几十行的 usage 会污染面板日志,也会在测试输出里刷屏。
82113
{ cwd: repoRoot, encoding: "utf8", maxBuffer: 32 * 1024 * 1024, stdio: ["ignore", "pipe", "pipe"] }
@@ -124,7 +155,14 @@ export function assessClaims(claims, diff) {
124155
let shift = 0;
125156
for (const h of sorted) {
126157
const hOldEnd = h.oldCount === 0 ? h.oldStart : h.oldStart + h.oldCount - 1;
127-
const overlaps = h.oldCount > 0 && start <= hOldEnd && end >= h.oldStart;
158+
// 纯插入(`@@ -a,0 +c,d @@`,oldCount === 0)没有「旧行区间」,但它插在旧第 a 行
159+
// 之后、第 a+1 行之前。落进 claim 区间内部(a >= start 且 a < end)就是真碰到了,
160+
// 与「删除/替换」一样算 touched;插在 claim 首行之前只是把它推下去,仍算 shifted。
161+
// 老写法 `h.oldCount > 0 && ...` 让纯插入永远不算相交——多行结论被插进新行时,
162+
// 结论原文已被改变却报「没事」。
163+
const overlaps = h.oldCount > 0
164+
? start <= hOldEnd && end >= h.oldStart
165+
: h.oldStart >= start && h.oldStart < end;
128166
if (overlaps) return { ...claim, reason: "touched", shift };
129167
if (hOldEnd < start) shift += h.newCount - h.oldCount;
130168
}

0 commit comments

Comments
 (0)