@matrixfu/skill-set-manager 0.0.0-stage → 1.1.2
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.
- package/client.js +1618 -0
- package/cordis.patch.yml +5 -0
- package/icon.svg +7 -0
- package/index.js +1091 -0
- package/lib/core/atomic.js +16 -0
- package/lib/core/backup.js +123 -0
- package/lib/core/binding.js +22 -0
- package/lib/core/commandLine.js +31 -0
- package/lib/core/dataPlane.js +81 -0
- package/lib/core/diagnose.js +167 -0
- package/lib/core/effective.js +237 -0
- package/lib/core/giterr.js +22 -0
- package/lib/core/github.js +256 -0
- package/lib/core/gitignore.js +35 -0
- package/lib/core/gitnet.js +112 -0
- package/lib/core/lifecycle.js +88 -0
- package/lib/core/linkx.js +92 -0
- package/lib/core/managed.js +24 -0
- package/lib/core/manifest.js +169 -0
- package/lib/core/migrate.js +145 -0
- package/lib/core/opgate.js +62 -0
- package/lib/core/paths.js +52 -0
- package/lib/core/pending.js +210 -0
- package/lib/core/plan.js +33 -0
- package/lib/core/projectRoot.js +20 -0
- package/lib/core/providerSkills.js +26 -0
- package/lib/core/provision.js +32 -0
- package/lib/core/resident.js +98 -0
- package/lib/core/resolver.js +136 -0
- package/lib/core/search.js +28 -0
- package/lib/core/sets.js +250 -0
- package/lib/core/source.js +64 -0
- package/lib/core/storage.js +87 -0
- package/lib/core/translateExec.js +122 -0
- package/lib/core/translations.js +62 -0
- package/lib/core/updates.js +181 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +63 -4
- package/README.md +0 -3
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** 原子文本写:同目录 tmp+rename,杜绝半写文件;core 落盘统一走这里。 */
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
let seq = 0;
|
|
6
|
+
export function writeTextAtomic(file, text, { fs: fsImpl = fs } = {}) {
|
|
7
|
+
fsImpl.mkdirSync(path.dirname(file), { recursive: true });
|
|
8
|
+
const tmp = `${file}.tmp-${process.pid}-${Date.now()}-${seq++}`;
|
|
9
|
+
try {
|
|
10
|
+
fsImpl.writeFileSync(tmp, text);
|
|
11
|
+
fsImpl.renameSync(tmp, file);
|
|
12
|
+
} catch (e) {
|
|
13
|
+
try { fsImpl.rmSync(tmp, { force: true }); } catch {}
|
|
14
|
+
throw e;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 备份与回收站(§8.8):`~/.dsh/skill-library/.backups/<name>/<ts>/`(库目录点开头
|
|
3
|
+
* 天然不被 libraryList 列出);快照先写 `.tmp` 再 rename 发布——半成品永不成为恢复
|
|
4
|
+
* 源(Review Focus #3);保留 = 每技能最近 10 份或 30 天先到为准。
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import crypto from 'node:crypto';
|
|
9
|
+
import { copyDirTree } from './github.js';
|
|
10
|
+
import { walkFiles } from './manifest.js';
|
|
11
|
+
|
|
12
|
+
const DAYS = 30 * 24 * 3600 * 1000;
|
|
13
|
+
const KEEP = 10;
|
|
14
|
+
|
|
15
|
+
function backupsRoot(roots) {
|
|
16
|
+
return path.join(roots.libraryRoot, '.backups');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 整技能快照(含 .manifest.json);返回已发布目录。 */
|
|
20
|
+
export function snapshot(roots, name, { fs: fsImpl = fs } = {}) {
|
|
21
|
+
const src = path.join(roots.libraryRoot, name);
|
|
22
|
+
fsImpl.statSync(src); // 不存在 → 直接 throw(调用方归一)
|
|
23
|
+
const ts = new Date().toISOString().replace(/[:.]/g, '-');
|
|
24
|
+
const pub = path.join(backupsRoot(roots), name, ts);
|
|
25
|
+
const tmp = `${pub}.tmp`;
|
|
26
|
+
fsImpl.mkdirSync(path.dirname(pub), { recursive: true });
|
|
27
|
+
try {
|
|
28
|
+
fsImpl.rmSync(tmp, { recursive: true, force: true });
|
|
29
|
+
copyDirTree(src, tmp, fsImpl);
|
|
30
|
+
// 快照自记「盘上内容摘要」:manifest.files 对 skip 文件是上游基线哈希而非盘上值,
|
|
31
|
+
// 不能用作恢复校验基准(活体冒烟教训)——.snapshot.json 才是内容口径。
|
|
32
|
+
fsImpl.writeFileSync(path.join(tmp, '.snapshot.json'), JSON.stringify({ ts, files: walkFiles(src, { fs: fsImpl }) }, null, 2) + '\n');
|
|
33
|
+
fsImpl.renameSync(tmp, pub);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
try { fsImpl.rmSync(tmp, { recursive: true, force: true }); } catch {}
|
|
36
|
+
throw e;
|
|
37
|
+
}
|
|
38
|
+
prune(roots, name, { fs: fsImpl });
|
|
39
|
+
return pub;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 保留策略:按 mtime 降序,超出 10 份或超 30 天者删除。 */
|
|
43
|
+
export function prune(roots, name, { fs: fsImpl = fs, now = Date.now() } = {}) {
|
|
44
|
+
const root = path.join(backupsRoot(roots), name);
|
|
45
|
+
let names;
|
|
46
|
+
try { names = fsImpl.readdirSync(root); } catch { return; }
|
|
47
|
+
// 崩溃残留的 <ts>.tmp 半成品:顺带回收(进程活着时 catch 已清,这里补跨进程残留,review n6)
|
|
48
|
+
for (const n of names) {
|
|
49
|
+
if (!n.endsWith('.tmp')) continue;
|
|
50
|
+
try { fsImpl.rmSync(path.join(root, n), { recursive: true, force: true }); } catch {}
|
|
51
|
+
}
|
|
52
|
+
const dirs = names
|
|
53
|
+
.filter((n) => !n.endsWith('.tmp'))
|
|
54
|
+
.map((n) => {
|
|
55
|
+
const dir = path.join(root, n);
|
|
56
|
+
let mtime = now;
|
|
57
|
+
try { mtime = fsImpl.statSync(dir).mtimeMs; } catch {}
|
|
58
|
+
return { dir, n, mtime };
|
|
59
|
+
})
|
|
60
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
61
|
+
dirs.forEach((d, i) => {
|
|
62
|
+
if (i < KEEP && now - d.mtime <= DAYS) return;
|
|
63
|
+
try { fsImpl.rmSync(d.dir, { recursive: true, force: true }); } catch {}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 已发布快照列表(新→旧),ts=mtime。 */
|
|
68
|
+
export function listBackups(roots, name, { fs: fsImpl = fs } = {}) {
|
|
69
|
+
const root = path.join(backupsRoot(roots), name);
|
|
70
|
+
let names;
|
|
71
|
+
try { names = fsImpl.readdirSync(root); } catch { return []; }
|
|
72
|
+
return names
|
|
73
|
+
.filter((n) => !n.endsWith('.tmp'))
|
|
74
|
+
.map((n) => {
|
|
75
|
+
const dir = path.join(root, n);
|
|
76
|
+
let fileCount = 0; let mtime = 0;
|
|
77
|
+
try {
|
|
78
|
+
mtime = fsImpl.statSync(dir).mtimeMs;
|
|
79
|
+
fileCount = countFiles(dir, fsImpl);
|
|
80
|
+
} catch { return null; }
|
|
81
|
+
return { dir, ts: new Date(mtime).toISOString(), fileCount };
|
|
82
|
+
})
|
|
83
|
+
.filter(Boolean)
|
|
84
|
+
.sort((a, b) => (a.ts < b.ts ? 1 : -1));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function countFiles(dir, fsImpl) {
|
|
88
|
+
let n = 0;
|
|
89
|
+
for (const ent of fsImpl.readdirSync(dir, { withFileTypes: true })) {
|
|
90
|
+
if (ent.isDirectory()) n += countFiles(path.join(dir, ent.name), fsImpl);
|
|
91
|
+
else n += 1;
|
|
92
|
+
}
|
|
93
|
+
return n;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 恢复 = 快照整体替换库体 → 以快照 manifest 逐文件哈希复核(§17#10 判定)。 */
|
|
97
|
+
export function restore(roots, name, backupDir, { fs: fsImpl = fs } = {}) {
|
|
98
|
+
const dst = path.join(roots.libraryRoot, name);
|
|
99
|
+
fsImpl.statSync(backupDir);
|
|
100
|
+
// 覆盖式恢复先对现存库体自动快照(Review Focus #2:overwrite 必先备份,恢复不得静默吞掉当前内容)
|
|
101
|
+
let had = false;
|
|
102
|
+
try { fsImpl.statSync(dst); had = true; } catch {}
|
|
103
|
+
if (had) snapshot(roots, name, { fs: fsImpl });
|
|
104
|
+
fsImpl.rmSync(dst, { recursive: true, force: true });
|
|
105
|
+
copyDirTree(backupDir, dst, fsImpl);
|
|
106
|
+
// 校验基准优先 .snapshot.json(盘上内容口径);旧快照无此文件时退回 manifest(内容≈manifest 时代)。
|
|
107
|
+
let basis = null;
|
|
108
|
+
for (const f of ['.snapshot.json', '.manifest.json']) {
|
|
109
|
+
try { basis = JSON.parse(fsImpl.readFileSync(path.join(dst, f), 'utf8')); break; } catch {}
|
|
110
|
+
}
|
|
111
|
+
let verified = false;
|
|
112
|
+
if (basis && basis.files) {
|
|
113
|
+
verified = true;
|
|
114
|
+
for (const [p, h] of Object.entries(basis.files)) {
|
|
115
|
+
try {
|
|
116
|
+
const buf = fsImpl.readFileSync(path.join(dst, p));
|
|
117
|
+
const hex = crypto.createHash('sha256').update(buf).digest('hex');
|
|
118
|
+
if (`sha256:${hex}` !== h) { verified = false; break; }
|
|
119
|
+
} catch { verified = false; break; }
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return { verified };
|
|
123
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** 项目绑定文件(§11):<projectRoot>/.dsh/skill-set-binding.json。 */
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { writeTextAtomic } from './atomic.js';
|
|
5
|
+
|
|
6
|
+
export const bindingFile = (projectRoot) => path.join(projectRoot, '.dsh', 'skill-set-binding.json');
|
|
7
|
+
|
|
8
|
+
export function readBinding(projectRoot, { fs: fsImpl = fs } = {}) {
|
|
9
|
+
let raw;
|
|
10
|
+
try { raw = fsImpl.readFileSync(bindingFile(projectRoot), 'utf8'); } catch (e) {
|
|
11
|
+
if (e.code === 'ENOENT') return null;
|
|
12
|
+
throw new Error(`${bindingFile(projectRoot)}: 解析失败(文件被手改坏?请人工修复): ${e.message}`);
|
|
13
|
+
}
|
|
14
|
+
const doc = JSON.parse(raw);
|
|
15
|
+
return { setSlug: doc.setSlug ?? null, updatedAt: doc.updatedAt ?? null };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function writeBinding(projectRoot, setSlug, { fs: fsImpl = fs } = {}) {
|
|
19
|
+
const doc = { setSlug, updatedAt: new Date().toISOString() };
|
|
20
|
+
writeTextAtomic(bindingFile(projectRoot), JSON.stringify(doc, null, 2) + '\n', { fs: fsImpl });
|
|
21
|
+
return doc;
|
|
22
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/skill-set` 键盘流子命令解析(§12 P2):纯函数、可测、无业务判断(§14 薄包装)。
|
|
3
|
+
* 空参 = `show`(最高频:当前项目根 + 当前集 + 生效清单);
|
|
4
|
+
* `--yes` / `-y` = rootDrift 的显式二次确认旗标(§6.5 键盘流等价物),任意位置均可剥离;
|
|
5
|
+
* 未知子命令原样带回 `action`,由处理器渲染帮助文本。
|
|
6
|
+
*/
|
|
7
|
+
export function parseSkillSetLine(raw) {
|
|
8
|
+
const tokens = String(raw ?? '').trim().split(/\s+/).filter(Boolean);
|
|
9
|
+
let yes = false;
|
|
10
|
+
const rest = [];
|
|
11
|
+
for (const t of tokens) {
|
|
12
|
+
if (t === '--yes' || t === '-y') yes = true;
|
|
13
|
+
else rest.push(t);
|
|
14
|
+
}
|
|
15
|
+
// 子命令后的全部非旗标 token 拼成 arg:显示名可含空格(`use 我的 前端 集`),多余 token 不该被静默吞掉(评审 M1)
|
|
16
|
+
return { action: rest[0] ?? 'show', arg: rest.slice(1).join(' ') || null, yes };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `skill_set` 工具入参解析与校验(§12 P2:模型侧只有 list / show / use 三个动作)。
|
|
21
|
+
* 校验在解析层做(模型给的 args 不可信),业务判断仍在 remote 方法里(§14)。
|
|
22
|
+
*/
|
|
23
|
+
export const TOOL_ACTIONS = ['list', 'show', 'use'];
|
|
24
|
+
export function parseToolArgs(args) {
|
|
25
|
+
const action = String(args?.action ?? '').trim();
|
|
26
|
+
if (!TOOL_ACTIONS.includes(action))
|
|
27
|
+
throw new Error(`action 必须是 ${TOOL_ACTIONS.join(' / ')},收到 "${args?.action ?? ''}"`);
|
|
28
|
+
const slug = args?.slug == null ? null : (String(args.slug).trim() || null);
|
|
29
|
+
if (action === 'use' && !slug) throw new Error('action=use 需要 slug(技能集 slug 或显示名)');
|
|
30
|
+
return { action, slug };
|
|
31
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P3 数据面核心(设计 §6.1/§6.4/§6.6):翻译缓存 store 读写、库视图对账、外来常驻清单、翻译回写。
|
|
3
|
+
* store = storageDomain 句柄(get/put)由 host 注入;本模块不碰 ctx,纯逻辑可 CASES 覆盖。
|
|
4
|
+
*/
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { readDescription, reconcile, setTranslated, CACHE_KEY } from './translations.js';
|
|
8
|
+
import { runBatch, parseSingleReply } from './translateExec.js';
|
|
9
|
+
import { loadResident } from './sets.js';
|
|
10
|
+
|
|
11
|
+
const ENTRY_KEY = 'entries';
|
|
12
|
+
|
|
13
|
+
export async function loadCache(store) { return (await store.get(CACHE_KEY, ENTRY_KEY)) ?? {}; }
|
|
14
|
+
export async function saveCache(store, cache) { await store.put(CACHE_KEY, ENTRY_KEY, cache); }
|
|
15
|
+
|
|
16
|
+
/** 现扫库 → readDescription → reconcile(新名/失效/GC)→ 落盘;缓存永不成为真相源。 */
|
|
17
|
+
export async function reconcileLibrary(roots, store, { fs: fsImpl = fs } = {}) {
|
|
18
|
+
const cache = await loadCache(store);
|
|
19
|
+
let names;
|
|
20
|
+
try { names = fsImpl.readdirSync(roots.libraryRoot); } catch { names = []; }
|
|
21
|
+
const entries = [];
|
|
22
|
+
for (const n of names) {
|
|
23
|
+
if (n.startsWith('.')) continue;
|
|
24
|
+
const { text, hash } = readDescription(path.join(roots.libraryRoot, n), { fs: fsImpl });
|
|
25
|
+
if (text) entries.push({ name: n, hash, en: text });
|
|
26
|
+
}
|
|
27
|
+
const r = reconcile(cache, entries);
|
|
28
|
+
await saveCache(store, r.cache);
|
|
29
|
+
return r;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** libraryList 视图钩子:autoTranslate 关(默认)时 onPending 完全不触发 = 零出网。 */
|
|
33
|
+
export async function libraryView(roots, store, { autoTranslate = false, onPending = null, fs: fsImpl = fs } = {}) {
|
|
34
|
+
const r = await reconcileLibrary(roots, store, { fs: fsImpl });
|
|
35
|
+
if (autoTranslate && r.pending.length && onPending) onPending(r.pending);
|
|
36
|
+
return r;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 外来常驻(设计 §6.4):常驻区里「不在名单且含 SKILL.md」的条目(弹窗灰行判定源之一)。 */
|
|
40
|
+
export function residentForeign(roots, { fs: fsImpl = fs, residentNames = null } = {}) {
|
|
41
|
+
const listed = new Set(residentNames ?? loadResident(roots).skills);
|
|
42
|
+
let names;
|
|
43
|
+
try { names = fsImpl.readdirSync(roots.userAgentsRoot); } catch { names = []; }
|
|
44
|
+
const out = [];
|
|
45
|
+
for (const n of names) {
|
|
46
|
+
if (n.startsWith('.') || listed.has(n)) continue;
|
|
47
|
+
try { if (fsImpl.statSync(path.join(roots.userAgentsRoot, n, 'SKILL.md')).isFile()) out.push(n); } catch {}
|
|
48
|
+
}
|
|
49
|
+
return out.sort();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 翻译执行(逐条粒度):names → items(en 快照优先,缺失重读盘)→ runBatch →
|
|
54
|
+
* done 剥编号回写 ok、failed 回写 failed(带 srcHash,同哈希不自愈)→ 落盘。
|
|
55
|
+
*/
|
|
56
|
+
export async function translateRunCore({ roots, store, names, callLlm, callMt, concurrency, limit, fs: fsImpl = fs }) {
|
|
57
|
+
const cache = await loadCache(store);
|
|
58
|
+
const items = [];
|
|
59
|
+
for (const name of Array.isArray(names) ? names : []) {
|
|
60
|
+
let en = cache[name]?.en ?? null;
|
|
61
|
+
let srcHash = cache[name]?.srcHash ?? null;
|
|
62
|
+
if (!en) { const d = readDescription(path.join(roots.libraryRoot, name), { fs: fsImpl }); en = d.text; srcHash = d.hash; }
|
|
63
|
+
if (en) items.push({ name, en, srcHash });
|
|
64
|
+
}
|
|
65
|
+
// 提速(2026-10-26 用户反馈「特别慢」):5 条拼一个 prompt(runBatch chunk),错行自动逐条回退
|
|
66
|
+
const res = await runBatch({ items, callLlm, callMt, concurrency, limit, chunk: 5 });
|
|
67
|
+
const byName = new Map(items.map((i) => [i.name, i]));
|
|
68
|
+
// M1(终审 2026-10-26):整批 LLM 调用是分钟级,期间 reconcileLibrary(每次 libraryList)可能已推表——
|
|
69
|
+
// 批尾必须重读最新表再逐条 merge 本批结果,绝不整表覆盖批首快照。
|
|
70
|
+
let next = await loadCache(store);
|
|
71
|
+
for (const d of res.done) {
|
|
72
|
+
const it = byName.get(d.name);
|
|
73
|
+
next = setTranslated(next, d.name, { srcHash: it.srcHash, en: it.en, zh: parseSingleReply(d.zh), status: 'ok' });
|
|
74
|
+
}
|
|
75
|
+
for (const fl of res.failed) {
|
|
76
|
+
const it = byName.get(fl.name);
|
|
77
|
+
next = setTranslated(next, fl.name, { srcHash: it?.srcHash ?? null, en: it?.en ?? null, zh: null, status: 'failed' });
|
|
78
|
+
}
|
|
79
|
+
await saveCache(store, next);
|
|
80
|
+
return { done: res.done, failed: res.failed, skipped: res.skipped, cache: next };
|
|
81
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 诊断(§9):默认只读报告,按六类分组,每条 {level, kind, what, where, fix}
|
|
3
|
+
* (fix = 可复制修复动作)。只有两类可自动修复(回收失效链接 / 按集重建),
|
|
4
|
+
* 其余只给建议不动文件。只扫一层(§9.5)。
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import crypto from 'node:crypto';
|
|
9
|
+
import { readBinding } from './binding.js';
|
|
10
|
+
import { readManaged } from './managed.js';
|
|
11
|
+
import { listSets, loadSet, loadResident } from './sets.js';
|
|
12
|
+
import { entryKind } from './linkx.js';
|
|
13
|
+
import { parseFrontmatter } from './resolver.js';
|
|
14
|
+
import { SKILL_NAME_RE, projectSkillsDir } from './paths.js';
|
|
15
|
+
import { walkFiles } from './manifest.js';
|
|
16
|
+
|
|
17
|
+
const row = (level, kind, what, where, fix) => ({ level, kind, what, where, fix });
|
|
18
|
+
const dirExists = (p, fsImpl) => { try { return fsImpl.statSync(p).isDirectory(); } catch { return false; } };
|
|
19
|
+
const oneLevel = (dir, fsImpl) => { try { return fsImpl.readdirSync(dir).filter((n) => !n.startsWith('.')); } catch { return []; } };
|
|
20
|
+
function digestOf(dir, fsImpl) {
|
|
21
|
+
const files = walkFiles(dir, { fs: fsImpl });
|
|
22
|
+
return 'sha256:' + crypto.createHash('sha256').update(
|
|
23
|
+
Object.entries(files).sort().map(([k, v]) => `${k}:${v}`).join('\n'),
|
|
24
|
+
).digest('hex');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function diagnose(roots, { projectRoot = null, cwd = process.cwd(), workspaceScopeInstalled = false, providerEntries } = {}, { fs: fsImpl = fs } = {}) {
|
|
28
|
+
const groups = { partition: [], copies: [], dangling: [], deadLinks: [], structure: [], env: [] };
|
|
29
|
+
const zones = [roots.libraryRoot, roots.userDshRoot, roots.userAgentsRoot, ...roots.customDirs];
|
|
30
|
+
const skillsDirOf = (pr) => projectSkillsDir(pr);
|
|
31
|
+
// 集名口径(用户报告 2026-10-26:面板改名后诊断仍打 slug):显示名优先,集文件缺失(冻结绑定)回落 slug
|
|
32
|
+
const setLabel = (slug) => (slug ? (loadSet(roots, slug)?.name || slug) : slug);
|
|
33
|
+
|
|
34
|
+
// §9.1 分区(信息行:解释「为什么生效清单比技能集大」)
|
|
35
|
+
const managed = (projectRoot ? readManaged(projectRoot, { fs: fsImpl }) : null) ?? { entries: {} };
|
|
36
|
+
if (projectRoot) {
|
|
37
|
+
const binding = readBinding(projectRoot, { fs: fsImpl });
|
|
38
|
+
groups.partition.push(row('info', 'partition-managed',
|
|
39
|
+
`受管技能(技能集 ${binding?.setSlug ? `"${setLabel(binding.setSlug)}"` : '(未绑定)'} 提供,插件按托管清单接管,切换即改写):${Object.keys(managed.entries || {}).join(', ') || '(无)'}`,
|
|
40
|
+
skillsDirOf(projectRoot), '无需修复:分区仅解释清单构成'));
|
|
41
|
+
const resident = loadResident(roots);
|
|
42
|
+
groups.partition.push(row('info', 'partition-resident',
|
|
43
|
+
`全局常驻(不受管控,所有技能集自动包含,故生效清单可能比技能集大):${(resident.skills || []).join(', ') || '(无)'}`,
|
|
44
|
+
roots.dshHome, '如需减少:面板「全局常驻」取消勾选'));
|
|
45
|
+
// §9.1 第三分区(用户疑问 2026-10-26):插件/Preset 注入的技能不在生效区盘上,插件无法经目录
|
|
46
|
+
// 感知,由调用方经平台 skills 服务注入 providerEntries;null = 已尝试但服务不可用(如实报,
|
|
47
|
+
// 不当空集);undefined = 调用方未提供该口径(旧口径,不新增行,向后兼容)。
|
|
48
|
+
if (providerEntries !== undefined) {
|
|
49
|
+
if (providerEntries === null)
|
|
50
|
+
groups.partition.push(row('info', 'partition-provider-unavailable',
|
|
51
|
+
'插件/Preset 提供(不受技能集管控):本次不可枚举(skills 服务不可用)', 'profile', '无需修复:重启 DSH 后重跑诊断'));
|
|
52
|
+
else
|
|
53
|
+
groups.partition.push(row('info', 'partition-provider',
|
|
54
|
+
`插件/Preset 提供(不受技能集管控,切换技能集不移除):${providerEntries.map((e) => `${e.name}(${e.provider})`).join(', ') || '(无)'}`,
|
|
55
|
+
'profile', '停用对应插件 bundle / profile patch 行即移除;同名文件系统技能可覆盖'));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// §9.2 副本漂移
|
|
60
|
+
if (projectRoot) {
|
|
61
|
+
for (const [name, e] of Object.entries(managed.entries || {})) {
|
|
62
|
+
if (!e.copied) continue;
|
|
63
|
+
const lib = path.join(roots.libraryRoot, name);
|
|
64
|
+
const entry = path.join(skillsDirOf(projectRoot), name);
|
|
65
|
+
if (!dirExists(lib, fsImpl) || !dirExists(entry, fsImpl)) continue; // 缺失由 dangling/deadLinks 报
|
|
66
|
+
if (digestOf(lib, fsImpl) !== digestOf(entry, fsImpl))
|
|
67
|
+
groups.copies.push(row('warn', 'copy-drift', `副本技能 "${name}" 与库内容漂移(副本不随库更新)`, entry, `恢复一致:移除条目 "${name}" 后重新生成`));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 已知技能名(全部扫描区一层)
|
|
72
|
+
const known = new Set();
|
|
73
|
+
for (const z of zones) for (const n of oneLevel(z, fsImpl)) if (dirExists(path.join(z, n), fsImpl)) known.add(n);
|
|
74
|
+
|
|
75
|
+
// §9.3 悬空引用
|
|
76
|
+
for (const s of listSets(roots)) {
|
|
77
|
+
const set = loadSet(roots, s.slug);
|
|
78
|
+
for (const n of set?.skills || []) if (!known.has(n))
|
|
79
|
+
groups.dangling.push(row('warn', 'dangling', `技能集 "${set?.name || s.slug}" 引用不存在的技能 "${n}"`, roots.setsDir, `从该集移除 "${n}" 或补齐技能体`));
|
|
80
|
+
}
|
|
81
|
+
for (const n of loadResident(roots).skills || []) if (!known.has(n))
|
|
82
|
+
groups.dangling.push(row('warn', 'dangling', `常驻名单引用不存在的技能 "${n}"`, path.join(roots.setsDir, '_resident.json'), `取消常驻 "${n}" 或补齐技能体`));
|
|
83
|
+
// 台账在、盘上条目没了(外删/死链):broken-entry——可经「按当前集重建」修复(review M1 建议)
|
|
84
|
+
if (projectRoot) {
|
|
85
|
+
for (const [name, e] of Object.entries(managed.entries || {})) {
|
|
86
|
+
const pDir = path.join(skillsDirOf(projectRoot), name);
|
|
87
|
+
const pFile = path.join(skillsDirOf(projectRoot), `${name}.md`);
|
|
88
|
+
const onDisk = entryKind(e?.flat ? pFile : pDir, { fs: fsImpl }) !== 'missing'
|
|
89
|
+
|| entryKind(e?.flat ? pDir : pFile, { fs: fsImpl }) !== 'missing';
|
|
90
|
+
if (!onDisk) groups.dangling.push(row('warn', 'broken-entry', `托管条目 "${name}" 已被外部删除(台账仍在)`, e?.flat ? pFile : pDir, '点「按当前集重建」重新生成'));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// §9.4 失效链接(项目内 link 条目指向不存在的本体)。
|
|
95
|
+
// 判定口径(§10.1.2):readdir Dirent 把 junction 报成 isSymbolicLink——据此认「是链」,
|
|
96
|
+
// 再 stat 判死活(stat 跟随目标);不可用 entryKind(死链 stat 失败会被归为 missing)。
|
|
97
|
+
if (projectRoot) {
|
|
98
|
+
let ents;
|
|
99
|
+
try { ents = fsImpl.readdirSync(skillsDirOf(projectRoot), { withFileTypes: true }); } catch { ents = []; }
|
|
100
|
+
for (const ent of ents) {
|
|
101
|
+
if (ent.name.startsWith('.') || !ent.isSymbolicLink()) continue;
|
|
102
|
+
const p = path.join(skillsDirOf(projectRoot), ent.name);
|
|
103
|
+
try { fsImpl.statSync(p); } catch { groups.deadLinks.push(row('error', 'dead-link', `条目 "${ent.name}" 指向已不存在的本体`, p, '点「回收失效链接」或移除该条目')); }
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// §9.5 结构:只扫一层。自家常驻链(links 台账记账的全局生效区条目)不是独立副本,
|
|
108
|
+
// 跳过防「重复技能名」误报(活体误报:常驻 clarify/plan 与库本体同名)。
|
|
109
|
+
const ownLinks = new Set();
|
|
110
|
+
{
|
|
111
|
+
const links = loadResident(roots).links ?? {};
|
|
112
|
+
for (const dir of Object.keys(links)) for (const n of Object.keys(links[dir] ?? {})) ownLinks.add(path.resolve(dir, n));
|
|
113
|
+
}
|
|
114
|
+
const seen = new Map();
|
|
115
|
+
for (const z of zones) {
|
|
116
|
+
for (const n of oneLevel(z, fsImpl)) {
|
|
117
|
+
const d = path.join(z, n);
|
|
118
|
+
if (ownLinks.has(path.resolve(d))) continue;
|
|
119
|
+
if (!dirExists(d, fsImpl)) continue;
|
|
120
|
+
let skillMd = null;
|
|
121
|
+
try { skillMd = fsImpl.readFileSync(path.join(d, 'SKILL.md'), 'utf8'); } catch {}
|
|
122
|
+
if (skillMd === null) { groups.structure.push(row('warn', 'no-skillmd', `目录 "${n}" 缺 SKILL.md(不会被加载)`, d, '补 SKILL.md 或移出扫描目录')); continue; }
|
|
123
|
+
if (!SKILL_NAME_RE.test(n)) groups.structure.push(row('warn', 'kebab', `技能名 "${n}" 非 kebab-case(平台加载不稳定;改名=卸载+重装)`, d, `重命名为 kebab-case 等价名`));
|
|
124
|
+
const fm = parseFrontmatter(skillMd);
|
|
125
|
+
if (!fm || !fm.name || !fm.description) groups.structure.push(row('warn', 'frontmatter', `技能 "${n}" frontmatter 缺 name/description`, d, '补全 frontmatter 两字段'));
|
|
126
|
+
else if (fm.name !== n) groups.structure.push(row('warn', 'name-mismatch', `技能 "${n}" 的 frontmatter.name="${fm.name}" 与目录名不一致`, d, '统一目录名与 name'));
|
|
127
|
+
const key = (fm && fm.name) || n;
|
|
128
|
+
if (!seen.has(key)) seen.set(key, []);
|
|
129
|
+
seen.get(key).push(d);
|
|
130
|
+
for (const sub of oneLevel(d, fsImpl)) {
|
|
131
|
+
const sd = path.join(d, sub);
|
|
132
|
+
if (!dirExists(sd, fsImpl)) continue;
|
|
133
|
+
try { fsImpl.statSync(path.join(sd, 'SKILL.md')); groups.structure.push(row('warn', 'nested', `技能 "${n}" 内嵌套 SKILL.md("${sub}/",不会被平台发现)`, sd, `把 "${sub}" 移到扫描目录顶层`)); } catch {}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (const [name, dirs] of seen) if (dirs.length > 1)
|
|
138
|
+
groups.structure.push(row('warn', 'duplicate', `重复技能名 "${name}"(同名冲突平台只 warn 不报错,需人工消歧)`, dirs.join(' | '), '保留一处,另一处改名或移出扫描区'));
|
|
139
|
+
|
|
140
|
+
// §9.6 环境
|
|
141
|
+
if (projectRoot) {
|
|
142
|
+
if (cwd && path.resolve(cwd) !== path.resolve(projectRoot))
|
|
143
|
+
groups.env.push(row('warn', 'root-drift', `根漂移:会话目录与项目根不一致,写入落在 ${projectRoot}`, projectRoot, '确认写入位置后继续;或把会话开在仓库根'));
|
|
144
|
+
let up = path.dirname(path.resolve(projectRoot));
|
|
145
|
+
for (let i = 0; i < 5 && up && up !== path.dirname(up); i++, up = path.dirname(up)) {
|
|
146
|
+
try { fsImpl.statSync(path.join(up, '.dsh', 'skill-set-binding.json')); groups.env.push(row('warn', 'parent-binding', `上层目录 ${up} 另有技能集绑定(潜在双真相源)`, up, '只在一层绑定,移除其一')); break; } catch {}
|
|
147
|
+
}
|
|
148
|
+
// flat 复制条目盘上是 `<name>.md`——按台账口径换算,不得裸名误报(review m7)
|
|
149
|
+
const mine = new Set(Object.entries(managed.entries || {}).map(([n, e]) => (e && e.flat ? `${n}.md` : n)));
|
|
150
|
+
for (const n of oneLevel(skillsDirOf(projectRoot), fsImpl))
|
|
151
|
+
if (!mine.has(n))
|
|
152
|
+
groups.env.push(row('warn', 'foreign', `外来条目 "${n}"(不在托管清单:不接管、切换不覆盖)`, path.join(skillsDirOf(projectRoot), n), `需要接管:移走 "${n}" 后重新生成`));
|
|
153
|
+
}
|
|
154
|
+
for (const c of roots.customDirs)
|
|
155
|
+
if (path.resolve(c) === path.resolve(roots.libraryRoot))
|
|
156
|
+
groups.env.push(row('warn', 'custom-covers-library', `custom(300) 目录 "${c}" 恰为技能库路径——库会被直接扫进生效区(破坏 §4.2)`, c, '从 Config.customSkillDirs 移除该目录'));
|
|
157
|
+
if (workspaceScopeInstalled)
|
|
158
|
+
groups.env.push(row('warn', 'dual-truth', '已装 dsh-workspace-scope:其生效区切换与本插件存在双真相源风险', 'profile', '二选一:禁用其技能相关面或停用本插件'));
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
groups,
|
|
162
|
+
fixable: {
|
|
163
|
+
reclaimDeadLinks: { count: groups.deadLinks.length, action: '回收失效链接:按托管清单 removeEntry 后按当前集重新生成' },
|
|
164
|
+
rebuild: { action: '按当前技能集重建生效清单(对当前集重新绑定)' },
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|