@moonquake2004/dsh-doctor 0.4.6 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dsh-doctor.mjs +93 -61
  2. package/package.json +1 -1
package/dsh-doctor.mjs CHANGED
@@ -148,7 +148,7 @@ function pickNewestSessionLib(libs) {
148
148
  function sessionTableFrom(lib) {
149
149
  try {
150
150
  const s = readFileSync(lib, 'utf8');
151
- const m = /const KNOWN_SESSION_EVENT_TYPES = new Set\(\[(.*?)\]\);/.exec(s);
151
+ const m = /const KNOWN_SESSION_EVENT_TYPES = new Set\(\[([\s\S]*?)\]\);/.exec(s);
152
152
  if (!m) return null;
153
153
  const items = [...m[1].matchAll(/"([^"]+)"/g)].map((x) => x[1]);
154
154
  return items.length ? new Set(items) : null;
@@ -493,6 +493,20 @@ function checkProfile(name) {
493
493
  }
494
494
  // P3 insert name 可解析性
495
495
  const req = (() => { try { return createRequire(join(dir, '_anchor.js')); } catch { return null; } })();
496
+ // 判据(#1719 taltara 的正确性陷阱,2026-09-12 实测命中我们):**不能以 require.resolve 为准** ——
497
+ // ESM-only 包(exports 只声明 import 条件)会让 require.resolve 抛 ERR_PACKAGE_PATH_NOT_EXPORTED,
498
+ // 而 loader 能正常 import;用它判定会把健康 profile 报成 FAIL(比没有 doctor 更糟)。
499
+ // 改为**存在性判据**:node_modules/<name>/package.json 存在即可(scoped 取两段),
500
+ // 并向上找一层以覆盖 profile 根安装(~/.dsh/profiles/node_modules)。require.resolve 仅作正向加分。
501
+ const nmRoots = [join(dir, 'node_modules'), join(dirname(dir), 'node_modules')];
502
+ const pkgPresent = (n) => {
503
+ if (n.startsWith('cordis:')) return true; // 宿主内置
504
+ if (n.startsWith('.') || n.startsWith('/') || n.startsWith('~')) return true; // 相对/绝对路径
505
+ const seg = String(n).split('/');
506
+ const pkgName = n.startsWith('@') ? seg.slice(0, 2).join('/') : seg[0]; // scoped 需两段
507
+ if (!pkgName) return true;
508
+ return nmRoots.some((root) => existsSync(join(root, pkgName, 'package.json')));
509
+ };
496
510
  const bad = [];
497
511
  for (const n of userNames) {
498
512
  if (n.startsWith('@local/') || n.startsWith('@liustack/')) {
@@ -503,9 +517,9 @@ function checkProfile(name) {
503
517
  continue;
504
518
  }
505
519
  }
506
- let ok = false;
507
- try { if (req) { req.resolve(n); ok = true; } } catch { ok = false; }
508
- if (!ok) bad.push(n);
520
+ let resolved = false;
521
+ try { if (req) { req.resolve(n); resolved = true; } } catch { resolved = false; }
522
+ if (!resolved && !pkgPresent(n)) bad.push(n);
509
523
  }
510
524
  if (bad.length) report('profile', 'P3', false, `用户 patch 中不可解析的 name(#1197/#880): ${bad.join(', ')}`, `dsh plugin --profile ${name} add <包> 或修复 file: 依赖`);
511
525
  else report('profile', 'P3', true, '用户 patch insert 均可解析', undefined);
@@ -1026,25 +1040,77 @@ function packageNamedExports(pkgDir) {
1026
1040
  }
1027
1041
  }
1028
1042
 
1043
+ /* ---- 会话日志定位(世代感知,规范见 dsh-security/docs/session-shape-v3.md §1) ----
1044
+ * 命名:^session(?:\.v([1-9][0-9]*))?\.jsonl(\.zstd)?$(与 dsh-session-format 的
1045
+ * CANONICAL_LOG_FILENAME 一致);v0 = 无 .vN 的旧名,vN = 当前世代(本机为 v3)。
1046
+ * 规则:① 同一会话目录内取**最高世代**;② 跨目录按 **mtime** 取最新;
1047
+ * ③ 忽略 session.lock(不匹配正则);④ 目录名任意(含 _no-cwd / 无 session- 前缀);
1048
+ * ⑤ 同世代同时存在 .zstd 与裸 .jsonl 时**优先 .zstd**(与旧实现
1049
+ * `existsSync(zstd) ? zstd : plain` 一致;真实 store 遇到两种编码并存会抛
1050
+ * encodingMismatch,这里只是让诊断仍能看到日志)。
1051
+ * 旧实现只认 session.jsonl[.zstd],v3 上线后每次 S 检查都在分析 2 天前的旧世代日志。 */
1052
+ const SESSION_LOG_RE = /^session(?:\.v([1-9][0-9]*))?\.jsonl(\.zstd)?$/;
1053
+
1054
+ /** 解析一个文件名 → { gen, zstd };非会话日志(含 session.lock)→ null。 */
1055
+ function parseSessionLogName(name) {
1056
+ const m = SESSION_LOG_RE.exec(name);
1057
+ if (!m) return null;
1058
+ return { gen: m[1] === undefined ? 0 : Number(m[1]), zstd: m[2] === '.zstd' };
1059
+ }
1060
+
1061
+ /** 单个会话目录 → 该目录的权威日志(最高世代;同世代 .zstd 优先);无 → null。 */
1062
+ function pickSessionLogIn(dir) {
1063
+ let names;
1064
+ try { names = readdirSync(dir); } catch { return null; }
1065
+ let best = null;
1066
+ for (const n of names) {
1067
+ const g = parseSessionLogName(n);
1068
+ if (!g) continue;
1069
+ if (!best || g.gen > best.gen || (g.gen === best.gen && g.zstd && !best.zstd)) best = { ...g, f: join(dir, n) };
1070
+ }
1071
+ return best ? { f: best.f, gen: best.gen } : null;
1072
+ }
1073
+
1074
+ /** 会话库全量定位:三层 sessions/<project>/<session-id>/session*.jsonl*。
1075
+ * loose=true 时额外接受两层散文件 sessions/<project>/session*.jsonl*——这是
1076
+ * "取最新会话"(S 检查默认目标 / 安全层 SR/SS)旧有的兼容行为;S11/S12 的全库扫描
1077
+ * 旧实现只走三层,保持 loose=false,避免把散文件误当会话(那会引入误报)。
1078
+ * 返回 [{ f, gen, m }](m = mtimeMs,按 mtime 降序)。 */
1079
+ function listSessionLogs(root, { loose = false } = {}) {
1080
+ const out = [];
1081
+ let users;
1082
+ try { users = readdirSync(root, { withFileTypes: true }); } catch { return out; }
1083
+ const add = (hit) => {
1084
+ try { out.push({ f: hit.f, gen: hit.gen, m: statSync(hit.f).mtimeMs }); } catch { /* race */ }
1085
+ };
1086
+ for (const u of users) {
1087
+ if (!u.isDirectory()) continue; // root 下的散文件不在旧实现语义内,不扩权
1088
+ const sd = join(root, u.name);
1089
+ // 两层散文件(sessions/<user>/session.jsonl[.zstd])
1090
+ if (loose) { const l = pickSessionLogIn(sd); if (l) add(l); }
1091
+ // 三层:sessions/<user>/<session-id>/session*.jsonl*
1092
+ let subs;
1093
+ try { subs = readdirSync(sd, { withFileTypes: true }); } catch { continue; }
1094
+ for (const s of subs) {
1095
+ if (!s.isDirectory()) continue;
1096
+ const hit = pickSessionLogIn(join(sd, s.name));
1097
+ if (hit) add(hit);
1098
+ }
1099
+ }
1100
+ out.sort((a, b) => b.m - a.m);
1101
+ return out;
1102
+ }
1103
+
1104
+ /** 最新会话日志路径(世代感知;含两层散文件兼容;无 → null)。 */
1105
+ function latestSessionLog(root = join(HOME, 'sessions')) {
1106
+ const logs = listSessionLogs(root, { loose: true });
1107
+ return logs.length ? logs[0].f : null;
1108
+ }
1109
+
1029
1110
  /* ================= session ================= */
1030
1111
  function checkSession(targetPath) {
1031
1112
  if (!wants('session')) return;
1032
- const target = targetPath || (() => {
1033
- let best = null, bestM = -1;
1034
- const root = join(HOME, 'sessions');
1035
- if (!existsSync(root)) return null;
1036
- for (const u of readdirSync(root)) {
1037
- const sd = join(root, u);
1038
- if (!existsSync(sd)) continue;
1039
- for (const s of readdirSync(sd)) {
1040
- const f = existsSync(join(sd, s, 'session.jsonl.zstd')) ? join(sd, s, 'session.jsonl.zstd') : join(sd, s, 'session.jsonl');
1041
- if (!existsSync(f)) continue;
1042
- const m = statSync(f).mtimeMs;
1043
- if (m > bestM) { bestM = m; best = f; }
1044
- }
1045
- }
1046
- return best;
1047
- })();
1113
+ const target = targetPath || latestSessionLog();
1048
1114
  if (!target || !existsSync(target)) { report('session', 'S0', true, '无会话日志,跳过单会话检查(可用 --session <path> 指定)', undefined); return; }
1049
1115
  let text;
1050
1116
  try {
@@ -1322,15 +1388,8 @@ function scanMigrationRefusals() {
1322
1388
  }
1323
1389
  const root = join(HOME, 'sessions');
1324
1390
  if (!existsSync(root)) { reportSkip('session', 'S12', '无会话目录,迁移拒载预检不适用', undefined); return; }
1325
- const files = [];
1326
- for (const u of readdirSync(root)) {
1327
- const sd = join(root, u);
1328
- if (!existsSync(sd)) continue;
1329
- for (const s of readdirSync(sd)) {
1330
- const f = existsSync(join(sd, s, 'session.jsonl.zstd')) ? join(sd, s, 'session.jsonl.zstd') : join(sd, s, 'session.jsonl');
1331
- if (existsSync(f)) files.push(f);
1332
- }
1333
- }
1391
+ // 世代感知:每个会话目录只取权威世代(v3 优先于 v0),与 store 的会话列表对齐
1392
+ const files = listSessionLogs(root).map((x) => x.f);
1334
1393
  if (files.length === 0) { reportSkip('session', 'S12', '未发现会话日志', undefined); return; }
1335
1394
  const refused = [];
1336
1395
  let viaChain = 0;
@@ -1365,15 +1424,8 @@ function scanAllSessions() {
1365
1424
  if (!wants('session')) return;
1366
1425
  const root = join(HOME, 'sessions');
1367
1426
  if (!existsSync(root)) { report('session', 'S11', true, '无会话目录,跳过全会话扫描', undefined); return; }
1368
- const files = [];
1369
- for (const u of readdirSync(root)) {
1370
- const sd = join(root, u);
1371
- if (!existsSync(sd)) continue;
1372
- for (const s of readdirSync(sd)) {
1373
- const f = existsSync(join(sd, s, 'session.jsonl.zstd')) ? join(sd, s, 'session.jsonl.zstd') : join(sd, s, 'session.jsonl');
1374
- if (existsSync(f)) files.push(f);
1375
- }
1376
- }
1427
+ // 世代感知:每个会话目录只取权威世代(v3 优先于 v0),与 store 的会话列表对齐
1428
+ const files = listSessionLogs(root).map((x) => x.f);
1377
1429
  if (files.length === 0) { report('session', 'S11', true, '未发现会话日志', undefined); return; }
1378
1430
  const corrupt = []; const oversized = []; const clean = [];
1379
1431
  let totalDS = 0; let totalEvents = 0;
@@ -1863,31 +1915,11 @@ async function run() {
1863
1915
  }
1864
1916
  } catch { /* 配置损坏不影响检查 */ }
1865
1917
  // 获取最新会话文件(供 SR*/SS* 检查使用)
1866
- // 复审修复:对齐 S11 的两层布局 sessions/<user>/<session>/session.jsonl[.zstd],
1867
- // 按 mtime 取最新;旧实现只扫顶层 *.jsonl,真实部署下永远返回 null → 运行时层全跳过
1918
+ // 世代感知定位(与 S11 同一规则):三层 sessions/<user>/<session>/session*.jsonl*,
1919
+ // 目录内取最高世代(v0/v3 并存时取 v3),跨目录按 mtime 取最新,忽略 session.lock
1868
1920
  const findLatestSession = () => {
1869
1921
  if (sessionArg) return sessionArg;
1870
- try {
1871
- const root = join(HOME, 'sessions');
1872
- const candidates = [];
1873
- for (const u of readdirSync(root)) {
1874
- const sd = join(root, u);
1875
- let subs = [];
1876
- try { subs = readdirSync(sd); } catch { continue; }
1877
- for (const s of subs) {
1878
- const zstdPath = join(sd, s, 'session.jsonl.zstd');
1879
- const plainPath = join(sd, s, 'session.jsonl');
1880
- const f = existsSync(zstdPath) ? zstdPath : (existsSync(plainPath) ? plainPath : null);
1881
- if (!f) continue;
1882
- try { candidates.push({ f, m: statSync(f).mtimeMs }); } catch { /* race */ }
1883
- }
1884
- // 兼容直接放在用户目录下的散文件
1885
- const loose = join(sd, 'session.jsonl');
1886
- if (existsSync(loose)) { try { candidates.push({ f: loose, m: statSync(loose).mtimeMs }); } catch { /* race */ } }
1887
- }
1888
- candidates.sort((a, b) => b.m - a.m);
1889
- return candidates.length ? candidates[0].f : null;
1890
- } catch { return null; }
1922
+ try { return latestSessionLog(); } catch { return null; }
1891
1923
  };
1892
1924
 
1893
1925
  const { results: secResults, exitCode: secExit, summary: secSummary } = await registry.runAll(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moonquake2004/dsh-doctor",
3
- "version": "0.4.6",
3
+ "version": "0.4.8",
4
4
  "description": "Offline diagnostic for DeepSeek Harness — 28 built-in + 5 catalog checks across env/profile/session (Layer A checks-as-data), self-update (Layer B), and a semi-automatic LLM observer (Layer C, --observe); Doctor panel in web UI settings.",
5
5
  "main": "lib/index.js",
6
6
  "files": [