dsh-git-push 0.0.0-stage → 2.8.6
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/.auditignore +30 -0
- package/.gitignore +39 -0
- package/LICENSE +21 -0
- package/README.md +1486 -2
- package/cli.mjs +360 -0
- package/cordis.patch.yml +14 -0
- package/lib/ARCHITECTURE.md +34 -0
- package/lib/BUGFIX-NOTES-2026-09-14.md +278 -0
- package/lib/app/apply.js +275 -0
- package/lib/app/audit-api.js +158 -0
- package/lib/app/command-registry.js +266 -0
- package/lib/app/constants.js +16 -0
- package/lib/app/handlers/account.js +166 -0
- package/lib/app/handlers/clone.js +168 -0
- package/lib/app/handlers/http-constants.js +9 -0
- package/lib/app/handlers/identity-rewrite.js +94 -0
- package/lib/app/handlers/meta.js +175 -0
- package/lib/app/handlers/npm.js +169 -0
- package/lib/app/handlers/repo-actions.js +143 -0
- package/lib/app/handlers/repos.js +231 -0
- package/lib/app/handlers/settings.js +62 -0
- package/lib/app/host-doc-checks.js +25 -0
- package/lib/app/http-handlers.js +131 -0
- package/lib/app/index.js +30 -0
- package/lib/app/inject-text.js +192 -0
- package/lib/app/scan-root.js +23 -0
- package/lib/app/schema.js +150 -0
- package/lib/app/settings-bridge.js +167 -0
- package/lib/app/slash-commands.js +395 -0
- package/lib/app/slot-stats.js +35 -0
- package/lib/app/tool-call.js +672 -0
- package/lib/app/tools.js +34 -0
- package/lib/ast/brace.js +105 -0
- package/lib/ast/callgraph.js +135 -0
- package/lib/ast/code-lines.js +114 -0
- package/lib/ast/consts.js +12 -0
- package/lib/ast/control-flow.js +461 -0
- package/lib/ast/credential.js +199 -0
- package/lib/ast/dataflow.js +274 -0
- package/lib/ast/dup-code.js +165 -0
- package/lib/ast/index.js +36 -0
- package/lib/ast/io-risk-const.js +95 -0
- package/lib/ast/io-risk-fn.js +208 -0
- package/lib/ast/io-risk-loop.js +308 -0
- package/lib/ast/io-risk.js +314 -0
- package/lib/ast/lang.js +156 -0
- package/lib/ast/line-count.js +52 -0
- package/lib/ast/magic-number.js +272 -0
- package/lib/ast/naming.js +284 -0
- package/lib/ast/perf.js +472 -0
- package/lib/ast/scope.js +241 -0
- package/lib/ast/shell.js +131 -0
- package/lib/ast/size.js +378 -0
- package/lib/ast/symbol-index.js +399 -0
- package/lib/ast/tokenizer.js +341 -0
- package/lib/audit/analysis-coverage.js +98 -0
- package/lib/audit/audit-file.js +182 -0
- package/lib/audit/checks.js +17 -0
- package/lib/audit/client-entry.js +93 -0
- package/lib/audit/collector.js +477 -0
- package/lib/audit/ext-runner.js +80 -0
- package/lib/audit/file-context.js +138 -0
- package/lib/audit/finding.js +50 -0
- package/lib/audit/gitignore-match.js +113 -0
- package/lib/audit/glob.js +63 -0
- package/lib/audit/history-report.js +100 -0
- package/lib/audit/history.js +126 -0
- package/lib/audit/ignore-blind.js +101 -0
- package/lib/audit/index.js +33 -0
- package/lib/audit/orchestrate.js +476 -0
- package/lib/audit/public.js +24 -0
- package/lib/audit/repo-level.js +30 -0
- package/lib/audit/report-yaml.js +86 -0
- package/lib/audit/slot.js +65 -0
- package/lib/audit-defaults.js +18 -0
- package/lib/audit-rules/audit-rules-comment.yml +247 -0
- package/lib/audit-rules/audit-rules-cpp.yml +22 -0
- package/lib/audit-rules/audit-rules-docs.yml +32 -0
- package/lib/audit-rules/audit-rules-dsh.yml +162 -0
- package/lib/audit-rules/audit-rules-filehealth.yml +42 -0
- package/lib/audit-rules/audit-rules-folder.yml +99 -0
- package/lib/audit-rules/audit-rules-frontend.yml +335 -0
- package/lib/audit-rules/audit-rules-go.yml +22 -0
- package/lib/audit-rules/audit-rules-i18n.yml +45 -0
- package/lib/audit-rules/audit-rules-java.yml +105 -0
- package/lib/audit-rules/audit-rules-nodejs.yml +647 -0
- package/lib/audit-rules/audit-rules-npm.yml +187 -0
- package/lib/audit-rules/audit-rules-paths.yml +92 -0
- package/lib/audit-rules/audit-rules-performance.yml +215 -0
- package/lib/audit-rules/audit-rules-php.yml +22 -0
- package/lib/audit-rules/audit-rules-private.yml +24 -0
- package/lib/audit-rules/audit-rules-python.yml +106 -0
- package/lib/audit-rules/audit-rules-robustness.yml +27 -0
- package/lib/audit-rules/audit-rules-rust.yml +22 -0
- package/lib/audit-rules/audit-rules-structure.yml +159 -0
- package/lib/audit-rules/audit-rules-swift.yml +22 -0
- package/lib/audit-rules/audit-rules-template.yml +108 -0
- package/lib/checks/button-bind.js +276 -0
- package/lib/checks/common.js +170 -0
- package/lib/checks/credential-file.js +43 -0
- package/lib/checks/dataflow.js +40 -0
- package/lib/checks/dispatch.js +112 -0
- package/lib/checks/dup-code.js +69 -0
- package/lib/checks/dup-const.js +232 -0
- package/lib/checks/file-health.js +150 -0
- package/lib/checks/filter.js +137 -0
- package/lib/checks/folder.js +173 -0
- package/lib/checks/hardcoded-path.js +98 -0
- package/lib/checks/index.js +46 -0
- package/lib/checks/io.js +66 -0
- package/lib/checks/magic-number.js +42 -0
- package/lib/checks/npm-json.js +273 -0
- package/lib/checks/private.js +63 -0
- package/lib/checks/regex.js +256 -0
- package/lib/checks/semantic.js +134 -0
- package/lib/checks/structural.js +274 -0
- package/lib/cli/commands-account.mjs +107 -0
- package/lib/cli/commands-audit.mjs +144 -0
- package/lib/cli/commands-doc.mjs +127 -0
- package/lib/cli/commands-vcs.mjs +175 -0
- package/lib/client/index.js +162 -0
- package/lib/client-parts/00-loader.js +33 -0
- package/lib/client-parts/01-const.js +207 -0
- package/lib/client-parts/02-utils.js +62 -0
- package/lib/client-parts/03-icons.js +12 -0
- package/lib/client-parts/10-account.js +83 -0
- package/lib/client-parts/11-browser.js +95 -0
- package/lib/client-parts/20-repo.js +374 -0
- package/lib/client-parts/21-audit.js +301 -0
- package/lib/client-parts/22-settings.js +146 -0
- package/lib/client-parts/23-npm.js +383 -0
- package/lib/client-parts/30-page.js +56 -0
- package/lib/client-parts/40-controller.js +1092 -0
- package/lib/client-parts/41-actions.js +279 -0
- package/lib/client-parts/99-apply.js +72 -0
- package/lib/client.js +3195 -0
- package/lib/commit-push.js +148 -0
- package/lib/context/index.js +363 -0
- package/lib/core/atomic-json.js +98 -0
- package/lib/core/config.js +40 -0
- package/lib/core/credentials.js +274 -0
- package/lib/core/endpoints.js +109 -0
- package/lib/core/exec.js +162 -0
- package/lib/exempt/index.js +292 -0
- package/lib/fs/edit-after-read.js +103 -0
- package/lib/fsx.js +87 -0
- package/lib/git/account-status.js +140 -0
- package/lib/git/account.js +233 -0
- package/lib/git/api.js +136 -0
- package/lib/git/atomic-json.js +6 -0
- package/lib/git/browse.js +38 -0
- package/lib/git/clone-download.js +397 -0
- package/lib/git/clone-history-mode.js +143 -0
- package/lib/git/clone-history.js +175 -0
- package/lib/git/clone-jobs.js +250 -0
- package/lib/git/clone-replay-io.js +172 -0
- package/lib/git/clone-replay.js +275 -0
- package/lib/git/clone.js +445 -0
- package/lib/git/cloud.js +46 -0
- package/lib/git/commit-message.js +141 -0
- package/lib/git/config.js +6 -0
- package/lib/git/cred-env.js +88 -0
- package/lib/git/credentials.js +6 -0
- package/lib/git/endpoints.js +6 -0
- package/lib/git/exec.js +6 -0
- package/lib/git/identity-rewrite.js +297 -0
- package/lib/git/identity.js +150 -0
- package/lib/git/ignore.js +69 -0
- package/lib/git/index.js +41 -0
- package/lib/git/module-splitter.js +47 -0
- package/lib/git/post-push.js +101 -0
- package/lib/git/push-diagnose.js +142 -0
- package/lib/git/push.js +476 -0
- package/lib/git/remote-resolve.js +146 -0
- package/lib/git/remote.js +128 -0
- package/lib/git/repo-index.js +512 -0
- package/lib/git/repos.js +235 -0
- package/lib/git/scan-runner.js +159 -0
- package/lib/git/sensitive.js +134 -0
- package/lib/git/transport.js +430 -0
- package/lib/git/visibility.js +35 -0
- package/lib/git/wrapped-git.js +68 -0
- package/lib/http/index.js +170 -0
- package/lib/index.js +24 -0
- package/lib/link-check/index.js +187 -0
- package/lib/npm/account.js +64 -0
- package/lib/npm/install.js +211 -0
- package/lib/npm/npmrc.js +56 -0
- package/lib/npm/publish.js +171 -0
- package/lib/npm/registry.js +168 -0
- package/lib/plugin/auto-detect.js +92 -0
- package/lib/plugin/auto-push.js +154 -0
- package/lib/plugin/index.js +250 -0
- package/lib/readme-templates/readme.yml +38 -0
- package/lib/rule/compilers/credential.js +62 -0
- package/lib/rule/compilers/dataflow.js +38 -0
- package/lib/rule/compilers/file-health.js +75 -0
- package/lib/rule/compilers/frontend.js +86 -0
- package/lib/rule/compilers/func.js +35 -0
- package/lib/rule/compilers/helpers.js +71 -0
- package/lib/rule/compilers/index.js +29 -0
- package/lib/rule/compilers/link-semantic.js +45 -0
- package/lib/rule/compilers/list-kinds.js +12 -0
- package/lib/rule/compilers/numeric-kinds.js +66 -0
- package/lib/rule/compilers/regex.js +78 -0
- package/lib/rule/compilers/structure.js +80 -0
- package/lib/rule/compilers.js +17 -0
- package/lib/rule/homoglyph.js +45 -0
- package/lib/rule/loader.js +270 -0
- package/lib/rule/registry.js +115 -0
- package/lib/rule/scope.js +67 -0
- package/lib/score/docs-score.js +166 -0
- package/lib/score/index.js +140 -0
- package/lib/self/index.js +196 -0
- package/lib/skip-dirs.js +64 -0
- package/lib/tool-probes.json +50 -0
- package/lib/user-requirements.json +14 -0
- package/lib/vendor/js-yaml/LICENSE +21 -0
- package/lib/vendor/js-yaml/js-yaml.mjs +3073 -0
- package/package.json +95 -4
- package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
- package/scripts/audit-ext/_example-readme-present.mjs +34 -0
- package/scripts/audit-ext/variable-min-length.mjs +102 -0
- package/scripts/audit-runner.mjs +35 -0
- package/scripts/audit-runtime-check.mjs +129 -0
- package/scripts/browser-page-probe.mjs +271 -0
- package/scripts/build-client.mjs +119 -0
- package/scripts/check.mjs +88 -0
- package/scripts/clean-date-comments.py +102 -0
- package/scripts/cpu-sample.mjs +188 -0
- package/scripts/doc-drift.mjs +200 -0
- package/scripts/doc-func.mjs +212 -0
- package/scripts/doc-tree.mjs +456 -0
- package/scripts/doc-version.mjs +220 -0
- package/scripts/frontend-preview-check.mjs +124 -0
- package/scripts/hot-path-probe-cjs.cjs +71 -0
- package/scripts/hot-path-probe-fs-shim.mjs +28 -0
- package/scripts/hot-path-probe-fsp-shim.mjs +31 -0
- package/scripts/hot-path-probe.mjs +153 -0
- package/scripts/module-splitter.py +800 -0
- package/scripts/platform-scan.mjs +168 -0
- package/scripts/preview-gen.mjs +489 -0
- package/scripts/preview-local-runner.mjs +55 -0
- package/scripts/preview-server-direct.mjs +116 -0
- package/scripts/preview-server.mjs +305 -0
- package/scripts/preview-slot-data.mjs +123 -0
- package/scripts/probe-recheck.mjs +47 -0
- package/scripts/readme-gen.mjs +268 -0
- package/scripts/rename-locator.mjs +97 -0
- package/scripts/rule-switch.mjs +97 -0
- package/scripts/rules-solo-audit.mjs +173 -0
- package/scripts/scan-file-io.mjs +713 -0
- package/scripts/scan-hardcoded-paths.mjs +125 -0
- package/scripts/scan-repos.mjs +167 -0
- package/scripts/scan-version.mjs +122 -0
- package/scripts/scrub-user-wording.mjs +420 -0
- package/scripts/symbol-index.mjs +85 -0
- package/scripts/sync-plugin-install.mjs +223 -0
- package/scripts/sync-plugin.mjs +279 -0
- package/scripts/verify-prestep.mjs +100 -0
- package/skills/dev/audit-rule-authoring.md +58 -0
- package/skills/dev/dsh-git-push.md +323 -0
- package/skills/dev/git-push-live-fix.md +45 -0
- package/skills/dsh-repo-index.md +35 -0
- package/skills/guide/guide-audit.md +167 -0
- package/skills/guide/guide-cli-scripts.md +104 -0
- package/skills/guide/guide-clone-repos.md +173 -0
- package/skills/guide/guide-commit-push.md +156 -0
- package/skills/guide/guide-inject-ui.md +166 -0
- package/skills/guide/guide-slash-completion.md +118 -0
- package/skills/workflow/README.md +21 -0
- package/skills/workflow/git-commit-discipline.md +68 -0
- package/skills/workflow/git-project-startup.md +85 -0
- package/skills/workflow/git-rebuild-process.md +57 -0
- package/skills/workflow/github-operations.md +91 -0
- package/skills/workflow/readme-sync-git-md.md +28 -0
- package/skills/workflow/task-completion-report.md +73 -0
- package/skills/workflow/tool-json-add-ask.md +35 -0
- package/skills/workflow/versioning-rule.md +211 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件部署形态与 HMR 自检(sync-plugin 的安装位置逻辑,2026-10-09 拆出)
|
|
3
|
+
*
|
|
4
|
+
* 背景(来源:开发者文档《DSH 插件免重启热重载 —— 打通方法与两个必要条件》):
|
|
5
|
+
* 服务端插件代码要**免重启热重载**,必须同时满足两条,缺一不可:
|
|
6
|
+
* ① hmr 插件要显式配 `root`(base bundle 默认 `root: []` = 「module roots are opt-in」,一个模块目录都不 watch);
|
|
7
|
+
* ② 插件本体要放在 `<profile>/local-plugins/`,**不能只放 node_modules**
|
|
8
|
+
* (hmr 的 ignored 默认含 `node_modules/**`,chokidar 遇到该目录整棵剪枝——
|
|
9
|
+
* 想「只放行 node_modules 下某个插件」的否定写法也无效)。
|
|
10
|
+
* 另外 `config.base` 是相对 **hmr 自己的包位置**解析的(不是相对 profile),
|
|
11
|
+
* 所以只写 `root: ['local-plugins']` 会解析到 `node_modules/@deepseek-ai/dsh-hmr/local-plugins` —— 必须同时给 base。
|
|
12
|
+
*
|
|
13
|
+
* 本模块只做「看清楚 + 给方案 + 按需执行」:
|
|
14
|
+
* inspectProfiles 列 profile 与插件双副本形态
|
|
15
|
+
* readHmrConfig 读该 profile 的 cordis.patch.yml 里 id: hmr 的 base/root
|
|
16
|
+
* judgeHmr 判定「这个插件改代码是否需要重启」
|
|
17
|
+
* planInstall 目标形态与现状的差异清单(默认只报告)
|
|
18
|
+
* applyInstall 按 on/off 执行安装形态动作(用改动本体的 sync 步骤落真实文件)
|
|
19
|
+
* depHints package.json 依赖声明是否还指向 node_modules / 绝对 link:
|
|
20
|
+
*/
|
|
21
|
+
import { existsSync, readdirSync, lstatSync, readlinkSync, unlinkSync, symlinkSync, mkdirSync, readFileSync } from 'node:fs';
|
|
22
|
+
import { join, resolve, sep } from 'node:path';
|
|
23
|
+
|
|
24
|
+
/** DSH 数据根(含 .dsh 的那一层)。兼容两种入参形态:
|
|
25
|
+
* A) DSH_HOME 本尊:`…/0.2.0-rc.2` → profiles 在 `<h>/.dsh/profiles`
|
|
26
|
+
* B) 已含 .dsh 的路径:`…/0.2.0-rc.2/.dsh` → profiles 在 `<h>/profiles`
|
|
27
|
+
* 两种都探测不到时按 A 返回(保持既有调用方行为),由上层报「探测为空」。 */
|
|
28
|
+
export function dshRootOf(home = process.env.DSH_HOME || '') {
|
|
29
|
+
const h = String(home || '').trim();
|
|
30
|
+
if (!h) return '';
|
|
31
|
+
if (existsSync(join(h, '.dsh', 'profiles'))) return join(h, '.dsh');
|
|
32
|
+
if (existsSync(join(h, 'profiles'))) return h;
|
|
33
|
+
return join(h, '.dsh');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** profiles 目录。 */
|
|
37
|
+
export function profilesDirOf(home = process.env.DSH_HOME || '') {
|
|
38
|
+
const root = dshRootOf(home);
|
|
39
|
+
return root ? join(root, 'profiles') : '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 列各 profile 及其中 local-plugins 的概况(供「多 profile 要显式指定」的提示)。 */
|
|
43
|
+
export function inspectProfiles(home = process.env.DSH_HOME || '') {
|
|
44
|
+
const dir = profilesDirOf(home);
|
|
45
|
+
const out = [];
|
|
46
|
+
if (!dir || !existsSync(dir)) return out;
|
|
47
|
+
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
48
|
+
if (!e.isDirectory()) continue;
|
|
49
|
+
const pd = join(dir, e.name);
|
|
50
|
+
const localDir = join(pd, 'local-plugins');
|
|
51
|
+
let localPlugins = [];
|
|
52
|
+
try { localPlugins = readdirSync(localDir, { withFileTypes: true }).filter((x) => x.isDirectory() || x.isSymbolicLink()).map((x) => x.name); } catch { localPlugins = []; }
|
|
53
|
+
out.push({ name: e.name, dir: pd, localDir, localPlugins });
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 读 profile 的 hmr 配置(cordis.patch.yml 的 `- id: hmr` 块)。
|
|
60
|
+
* 文本扫描而非引 YAML 依赖:只需要 base / root 两项,且该文件由插件管理器生成、结构稳定。
|
|
61
|
+
*/
|
|
62
|
+
export function readHmrConfig(profileDir, home = process.env.DSH_HOME || '') {
|
|
63
|
+
const baseDir = profileDir || join(profilesDirOf(home), 'web');
|
|
64
|
+
const f = join(baseDir, 'cordis.patch.yml');
|
|
65
|
+
if (!existsSync(f)) return { found: false, profileDir: baseDir, base: '', root: [], reason: '该 profile 没有 cordis.patch.yml' };
|
|
66
|
+
let text = '';
|
|
67
|
+
try { text = readFileSync(f, 'utf8'); } catch { return { found: false, profileDir: baseDir, base: '', root: [], reason: 'cordis.patch.yml 读失败' }; }
|
|
68
|
+
const i = text.search(/^-\s*id:\s*hmr\s*$/m);
|
|
69
|
+
if (i < 0) {
|
|
70
|
+
return { found: false, profileDir: baseDir, base: '', root: [], reason: '未配置 id: hmr —— hmr 默认 root:[] 只重载 profile 配置,不 watch 任何模块目录' };
|
|
71
|
+
}
|
|
72
|
+
const rest = text.slice(i);
|
|
73
|
+
const nextIdx = rest.slice(1).search(/^-\s/m);
|
|
74
|
+
const block = nextIdx < 0 ? rest : rest.slice(0, nextIdx + 1);
|
|
75
|
+
const base = ((block.match(/^[ \t]*base:[ \t]*(\S+)[ \t]*$/m) || [])[1] || '');
|
|
76
|
+
// ⚠️ 这里不能用 \s*(\s 含换行,会把 root: 后的换行也吃掉,导致再要求 \n 时匹配失败)——只吃行内空白。
|
|
77
|
+
const rootBlock = ((block.match(/^[ \t]*root:[ \t]*\r?\n((?:[ \t]+-[ \t]*\S+[ \t]*\r?\n?)+)/m) || [])[1] || '');
|
|
78
|
+
const root = [...rootBlock.matchAll(/-[ \t]*(\S+)/g)].map((m) => m[1]);
|
|
79
|
+
return { found: true, profileDir: baseDir, base, root, reason: '' };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 判定「该插件改代码是否需要重启」。
|
|
84
|
+
* @param {{hmr: object, profileDir: string, pluginName: string}} p
|
|
85
|
+
* @returns {{hot: boolean, reason: string, dir?: string}}
|
|
86
|
+
*/
|
|
87
|
+
export function judgeHmr({ hmr, profileDir, pluginName }) {
|
|
88
|
+
if (!pluginName) return { hot: false, reason: '未给出插件名' };
|
|
89
|
+
if (!hmr || !hmr.found) return { hot: false, reason: (hmr && hmr.reason) || '未读到 hmr 配置(默认不 watch 模块目录)' };
|
|
90
|
+
const base = hmr.base || profileDir || '';
|
|
91
|
+
if (!hmr.root.length) {
|
|
92
|
+
return { hot: false, reason: 'hmr.root 为空 —— module roots are opt-in:配了 base 也必须显式列出要 watch 的目录' };
|
|
93
|
+
}
|
|
94
|
+
const roots = hmr.root.map((r) => resolve(base, r));
|
|
95
|
+
const localDir = join(base, 'local-plugins', pluginName);
|
|
96
|
+
const nmDir = join(base, 'node_modules', pluginName);
|
|
97
|
+
const inRoot = (p) => roots.some((r) => p === r || p.startsWith(r + sep));
|
|
98
|
+
// 判据必须**先确认真实存在**:只看路径前缀会把「名字恰好落在 root 下、其实没装」也判成可热重载(单测抓到过)。
|
|
99
|
+
if (existsSync(localDir) && inRoot(localDir)) {
|
|
100
|
+
return { hot: true, reason: `命中所配 root(${hmr.root.join(' / ')})且不经 node_modules ⇒ 改代码可免重启热重载`, dir: localDir };
|
|
101
|
+
}
|
|
102
|
+
if (existsSync(nmDir)) {
|
|
103
|
+
return { hot: false, reason: `插件只落在 node_modules(${nmDir})—— hmr 的默认 ignored 会整棵剪枝该目录(否定写法同样无效),需按目标形态挪到 local-plugins` };
|
|
104
|
+
}
|
|
105
|
+
if (!existsSync(localDir)) {
|
|
106
|
+
return { hot: false, reason: `未在 ${localDir} 找到该插件(看起来没装到该 profile)` };
|
|
107
|
+
}
|
|
108
|
+
return { hot: false, reason: `插件在 local-plugins,但该目录不在所配 root 覆盖范围内(root=${hmr.root.join(',')})` };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** lstat 包装:不存在返回 null(符号链接本身用 lstat 判断)。 */
|
|
112
|
+
function lstatOrNull(p) {
|
|
113
|
+
try { return lstatSync(p); } catch { return null; }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 目标形态(对照文档 §3.2)与现状的差异清单。默认只报告,不执行。
|
|
118
|
+
* 目标:`<profile>/local-plugins/<插件>` 为**真实目录**;`<profile>/node_modules/<插件>` 为**相对软链**指回去。
|
|
119
|
+
* @returns {Array<{kind: string, path: string, desc: string}>}
|
|
120
|
+
*/
|
|
121
|
+
export function planInstall({ profileDir, pluginName }) {
|
|
122
|
+
const actions = [];
|
|
123
|
+
if (!profileDir || !pluginName) return actions;
|
|
124
|
+
const local = join(profileDir, 'local-plugins', pluginName);
|
|
125
|
+
const nm = join(profileDir, 'node_modules', pluginName);
|
|
126
|
+
const ls = lstatOrNull(local);
|
|
127
|
+
if (ls && ls.isSymbolicLink()) {
|
|
128
|
+
actions.push({ kind: 'delink-local', path: local, desc: 'local-plugins 下是软链:真实加载源应是**真实目录**(执行时会先删链再落一份真实副本)' });
|
|
129
|
+
} else if (!ls) {
|
|
130
|
+
actions.push({ kind: 'create-local', path: local, desc: 'local-plugins 下缺该插件:需先同步一份真实目录(HMR 的覆盖点与真实加载源都在这里)' });
|
|
131
|
+
}
|
|
132
|
+
const ns = lstatOrNull(nm);
|
|
133
|
+
if (ns && ns.isSymbolicLink()) {
|
|
134
|
+
let t = '';
|
|
135
|
+
try { t = readlinkSync(nm); } catch { t = ''; }
|
|
136
|
+
if (!t.startsWith('.')) {
|
|
137
|
+
actions.push({ kind: 'relink-nm', path: nm, desc: `node_modules 下是绝对软链(→ ${t}):应改相对链 ../local-plugins/${pluginName}(换机/换卷仍有效)` });
|
|
138
|
+
}
|
|
139
|
+
} else if (!ns) {
|
|
140
|
+
actions.push({ kind: 'link-nm', path: nm, desc: `node_modules 下缺软链:应建相对链 ../local-plugins/${pluginName}` });
|
|
141
|
+
} else {
|
|
142
|
+
actions.push({ kind: 'dir-nm', path: nm, desc: 'node_modules 下是真实目录:按目标形态应让真实源落在 local-plugins、这里只留相对软链(本工具不自动搬移,避免动到既有数据)' });
|
|
143
|
+
}
|
|
144
|
+
return actions;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 执行安装形态动作(仅在调用方传入 on=true 时写盘)。
|
|
149
|
+
* delink-local:删软链(真实内容由后续同步步骤落进来)
|
|
150
|
+
* create-local:建目录(同上)
|
|
151
|
+
* relink-nm / link-nm:替换/新建相对软链
|
|
152
|
+
* dir-nm:只报告(搬移真实目录有数据风险,交给人工或既有搬迁流程)
|
|
153
|
+
* @returns {{done: string[], skipped: string[]}}
|
|
154
|
+
*/
|
|
155
|
+
export function applyInstall(actions, { on = false } = {}) {
|
|
156
|
+
const done = [];
|
|
157
|
+
const skipped = [];
|
|
158
|
+
for (const a of actions) {
|
|
159
|
+
if (!on || a.kind === 'dir-nm') { skipped.push(a.kind); continue; }
|
|
160
|
+
try {
|
|
161
|
+
if (a.kind === 'delink-local') {
|
|
162
|
+
unlinkSync(a.path); // 只删软链本身,不触及它指向的任何内容
|
|
163
|
+
mkdirSync(a.path, { recursive: true });
|
|
164
|
+
} else if (a.kind === 'create-local') {
|
|
165
|
+
mkdirSync(a.path, { recursive: true });
|
|
166
|
+
} else if (a.kind === 'relink-nm') {
|
|
167
|
+
unlinkSync(a.path);
|
|
168
|
+
symlinkSync(join('..', 'local-plugins', a.path.split(sep).pop()), a.path);
|
|
169
|
+
} else if (a.kind === 'link-nm') {
|
|
170
|
+
symlinkSync(join('..', 'local-plugins', a.path.split(sep).pop()), a.path);
|
|
171
|
+
}
|
|
172
|
+
done.push(a.kind);
|
|
173
|
+
} catch (e) {
|
|
174
|
+
skipped.push(`${a.kind}(${String(e && e.message).slice(0, 60)})`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return { done, skipped };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 依赖声明提示:profile 的 package.json 里该插件若写成 `link:<绝对路径>` 或 `file:node_modules/...`,
|
|
182
|
+
* 应改为 `file:local-plugins/<插件>`(否则 profile 解析到的不是真实加载源)。
|
|
183
|
+
* @returns {string[]} 提示行(空数组 = 无需改)
|
|
184
|
+
*/
|
|
185
|
+
export function depHints(profileDir, pluginName) {
|
|
186
|
+
const out = [];
|
|
187
|
+
if (!profileDir || !pluginName) return out;
|
|
188
|
+
const f = join(profileDir, 'package.json');
|
|
189
|
+
if (!existsSync(f)) return out;
|
|
190
|
+
let deps = {};
|
|
191
|
+
try { deps = { ...(JSON.parse(readFileSync(f, 'utf8')).dependencies || {}) }; } catch { return out; }
|
|
192
|
+
const val = deps[pluginName];
|
|
193
|
+
if (typeof val !== 'string') return out;
|
|
194
|
+
const want = `file:local-plugins/${pluginName}`;
|
|
195
|
+
if (val !== want) {
|
|
196
|
+
out.push(`依赖声明应改为 "${pluginName}": "${want}"(当前 "${val}")`);
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 从多 profile 里选出「该插件的正确部署 profile」。
|
|
203
|
+
*
|
|
204
|
+
* 为什么需要:自动探测原先取首个命中,而本机多个 profile 都可能残留同名副本(旧副本/测试 profile),
|
|
205
|
+
* 选中错的 profile 会同步到错的地方、并把「未配 hmr」等结论也判错(dry-run 实测踩到过)。
|
|
206
|
+
* 判据按可靠性排序:① 插件在 local-plugins(真实加载源)② 只在 node_modules(历史 link 产物)。
|
|
207
|
+
*
|
|
208
|
+
* @returns {{dir: string, profile?: string, reason: string, ambiguous?: boolean, candidates?: string[]}}
|
|
209
|
+
*/
|
|
210
|
+
export function pickProfile(profiles = [], pluginName = '') {
|
|
211
|
+
if (!pluginName) return { dir: '', reason: '未给出插件名' };
|
|
212
|
+
const withLocal = profiles.filter((p) => existsSync(join(p.localDir, pluginName)));
|
|
213
|
+
if (withLocal.length === 1) return { dir: withLocal[0].dir, profile: withLocal[0].name, reason: '命中 local-plugins(真实加载源)' };
|
|
214
|
+
if (withLocal.length > 1) {
|
|
215
|
+
return { dir: '', reason: '多个 profile 的 local-plugins 下都有该插件,请用 --profile 指定', ambiguous: true, candidates: withLocal.map((p) => p.name) };
|
|
216
|
+
}
|
|
217
|
+
const withNm = profiles.filter((p) => existsSync(join(p.dir, 'node_modules', pluginName)));
|
|
218
|
+
if (withNm.length === 1) return { dir: withNm[0].dir, profile: withNm[0].name, reason: '只在 node_modules 命中(历史 link 产物,建议按目标形态迁到 local-plugins)' };
|
|
219
|
+
if (withNm.length > 1) {
|
|
220
|
+
return { dir: '', reason: '多个 profile 的 node_modules 下都有该插件,请用 --profile 指定', ambiguous: true, candidates: withNm.map((p) => p.name) };
|
|
221
|
+
}
|
|
222
|
+
return { dir: '', reason: `没有任何 profile 装了这个插件(共 ${profiles.length} 个 profile)` };
|
|
223
|
+
}
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 双副本同步脚本(1.0.0)
|
|
3
|
+
*
|
|
4
|
+
* 源 = 本仓库(脚本所在插件仓库根)
|
|
5
|
+
* 目标 = DSH 插件目录(.dsh/profiles/<profile>/node_modules/<插件名>)
|
|
6
|
+
*
|
|
7
|
+
* 默认 **dryRun**(只打印将要同步的差异,不写入);`--write` 才真同步。
|
|
8
|
+
* 同步内容:lib/** skills/** scripts/** cli.mjs package.json cordis.patch.yml README.md
|
|
9
|
+
* 排除:.git node_modules docs/WORKBOARD*(开发看板不随插件发布)test/**
|
|
10
|
+
*
|
|
11
|
+
* 用法:
|
|
12
|
+
* node scripts/sync-plugin.mjs # dry-run(默认)
|
|
13
|
+
* node scripts/sync-plugin.mjs --write # 真同步
|
|
14
|
+
* node scripts/sync-plugin.mjs --target <目录> # 指定目标(默认自动探测)
|
|
15
|
+
* node scripts/sync-plugin.mjs --source <目录> # 指定源插件仓库(默认本仓库);
|
|
16
|
+
* # 目标未显式给时按源目录名自动探测双副本
|
|
17
|
+
*/
|
|
18
|
+
import { existsSync, readdirSync, statSync, readFileSync } from 'node:fs';
|
|
19
|
+
import { mkdir, readFile, chmod } from 'node:fs/promises';
|
|
20
|
+
// 目标目录常在工作区(CIFS 网络挂载):copyFileSync 在 CIFS 内会 EPERM
|
|
21
|
+
// (尝试 SMB 服务端复制),必须用带读写回退的 copyFileCompat。
|
|
22
|
+
import { copyFileCompat } from '../lib/fsx.js';
|
|
23
|
+
import { readdir, stat, access } from 'node:fs/promises';
|
|
24
|
+
import { join, relative, dirname, basename } from 'node:path';
|
|
25
|
+
import { fileURLToPath } from 'node:url';
|
|
26
|
+
// 安装形态与免重启热重载自检(2026-10-09 拆出,见 scripts/sync-plugin-install.mjs 顶部说明)
|
|
27
|
+
import { dshRootOf, profilesDirOf, inspectProfiles, readHmrConfig, judgeHmr, planInstall, applyInstall, depHints, pickProfile } from './sync-plugin-install.mjs';
|
|
28
|
+
|
|
29
|
+
export const SOURCE_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
30
|
+
|
|
31
|
+
/** 随插件发布的顶层条目(通用候选:按源目录存在性过滤,兼容任意 DSH 插件)。
|
|
32
|
+
* 候选含 lib/skills/scripts/assets/tools/cli.mjs/package.json/cordis.patch.yml/
|
|
33
|
+
* README.md/.auditignore/.gitignore——源里没有的条目自动跳过。 */
|
|
34
|
+
export const SYNC_ENTRIES = ['lib', 'skills', 'scripts', 'assets', 'tools', 'cli.mjs', 'package.json', 'cordis.patch.yml', 'README.md', 'LICENSE', '.auditignore', '.gitignore'];
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* npm 打包的**硬性排除项**(npm-packlist 默认忽略集,与项目自定义无关)。
|
|
38
|
+
*
|
|
39
|
+
* 【设计】同步清单的唯一权威 = 源项目 package.json 的 `files` 字段(npm 发布语义):
|
|
40
|
+
* 「要发布什么,就写进 files」;同步工具**不维护任何自定义排除表**。
|
|
41
|
+
*
|
|
42
|
+
* 为什么删掉旧的自定义排除(2026-10-07 事故):旧实现在 `files` 白名单之外另有一套
|
|
43
|
+
* 子串排除 `['.git','node_modules','WORKBOARD','test','.tmp','.bak','.trash']`,
|
|
44
|
+
* 判定写成 `rel.split('/').includes(x) || rel.includes(x)`——`rel.includes('test')`
|
|
45
|
+
* 把随插件发布的正式模块 `lib/shared/test-hooks.js` 当成 test 目录排掉了。后果:
|
|
46
|
+
* 同步报「成功」、安装副本却少一个文件,两个版本之间 lib/ 永远对不齐(只有逐文件
|
|
47
|
+
* diff 副本才发现,宿主运行看不出)。教训:**发布内容只由 package.json 决定**,
|
|
48
|
+
* `files` 白名单与自定义排除表是两套真相,必然分叉——排除只保留 npm 自己的硬规则。
|
|
49
|
+
*/
|
|
50
|
+
export const NPM_ALWAYS_IGNORE_SEGMENTS = ['.git', 'node_modules', '.DS_Store', '.npmrc', 'package-lock.json'];
|
|
51
|
+
/** npm 硬性忽略的后缀型条目(编辑器交换/备份文件)。 */
|
|
52
|
+
export const NPM_ALWAYS_IGNORE_SUFFIX = ['.orig', '.swp'];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 该相对路径是否命中 npm 硬性排除项(目录遍历与单测共用同一判定,避免规则再次分叉)。
|
|
56
|
+
* @param {string} rel 相对插件根的路径(POSIX 分隔符)
|
|
57
|
+
* @returns {boolean} true = npm 不会打包它,同步也不复制
|
|
58
|
+
*/
|
|
59
|
+
export function isNpmIgnored(rel) {
|
|
60
|
+
const segments = String(rel).split('/');
|
|
61
|
+
return NPM_ALWAYS_IGNORE_SEGMENTS.some((x) => segments.includes(x))
|
|
62
|
+
|| NPM_ALWAYS_IGNORE_SUFFIX.some((x) => String(rel).endsWith(x));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 异步探测路径是否存在(node:fs/promises 不提供 exists)。 */
|
|
66
|
+
async function exists(p) {
|
|
67
|
+
try { await access(p); return true; } catch { return false; }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 随插件发布的顶层条目 = 源项目 package.json 的 `files` 白名单(每个插件自行声明
|
|
72
|
+
* 发布内容,与 npm 打包语义一致);另强制包含 package.json 本身(部署副本加载必需)。
|
|
73
|
+
* files 缺失或读取失败时兜底保守最小集 ['lib', 'cordis.patch.yml']。
|
|
74
|
+
* @param {string} root 源仓库根
|
|
75
|
+
* @returns {string[]} 应同步的顶层条目
|
|
76
|
+
*/
|
|
77
|
+
export async function packageFilesOf(root) {
|
|
78
|
+
let files = [];
|
|
79
|
+
try {
|
|
80
|
+
const pkg = JSON.parse(await readFile(join(root, 'package.json'), 'utf8'));
|
|
81
|
+
if (Array.isArray(pkg.files) && pkg.files.length > 0) files = pkg.files;
|
|
82
|
+
} catch {
|
|
83
|
+
// 读失败 → 走兜底最小集
|
|
84
|
+
}
|
|
85
|
+
if (files.length === 0) {
|
|
86
|
+
// npm 语义:未声明 `files` = 发布「除默认忽略项以外的一切」;安装副本**不照做**
|
|
87
|
+
// (那会把 test/、docs/、开发归档一起灌进宿主副本),退化为候选白名单并提示补声明。
|
|
88
|
+
console.warn('[sync-plugin] ⚠️ package.json 未声明 files —— 按保守白名单同步;建议按 npm 规范补上 files 字段');
|
|
89
|
+
files = SYNC_ENTRIES;
|
|
90
|
+
}
|
|
91
|
+
// package.json 永远随包(npm 语义:始终包含)——部署副本靠它识别插件与版本。
|
|
92
|
+
return Array.from(new Set([...files, 'package.json']));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 递归列出源目录下应同步的文件(相对路径)。
|
|
97
|
+
* @param {string} root 源根
|
|
98
|
+
* @returns {string[]} 相对路径列表
|
|
99
|
+
*/
|
|
100
|
+
export async function listSyncFiles(root = SOURCE_ROOT) {
|
|
101
|
+
const out = [];
|
|
102
|
+
// 递归遍历目录:改异步以免在大目录上逐项阻塞事件循环
|
|
103
|
+
// (readdir 带 withFileTypes,目录项类型由一次调用带回,无需再逐个 stat)
|
|
104
|
+
const walk = async (dir) => {
|
|
105
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
106
|
+
const full = join(dir, entry.name);
|
|
107
|
+
const rel = relative(root, full);
|
|
108
|
+
if (isNpmIgnored(rel)) continue;
|
|
109
|
+
if (entry.isDirectory()) await walk(full);
|
|
110
|
+
else out.push(rel);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
for (const entryName of await packageFilesOf(root)) {
|
|
114
|
+
const full = join(root, entryName);
|
|
115
|
+
if (!(await exists(full))) continue;
|
|
116
|
+
if ((await stat(full)).isDirectory()) await walk(full);
|
|
117
|
+
else out.push(entryName);
|
|
118
|
+
}
|
|
119
|
+
return out.sort();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 探测 DSH 插件目录。
|
|
124
|
+
*
|
|
125
|
+
* **两个位置都要同步**(最大教训:只同步 node_modules 导致改动看不到):
|
|
126
|
+
* 1) `<profile>/local-plugins/<插件名>` ← **真实加载源**(profile 的 package.json
|
|
127
|
+
* 写的是 `"<插件名>": "file:./local-plugins/<插件名>"`,DSH 加载这里)
|
|
128
|
+
* 2) `<profile>/node_modules/<插件名>` ← npm link 产物(部分运行路径会解析到这里)
|
|
129
|
+
*
|
|
130
|
+
* 历史上 UI 改动「刷新看不到」,根因就是只 rsync 了 node_modules。
|
|
131
|
+
* @param {string} [home] DSH_HOME(默认从环境变量推断)
|
|
132
|
+
* @param {string} [pluginName] 插件名
|
|
133
|
+
* @returns {string[]} 命中的目标目录(local-plugins 在前)
|
|
134
|
+
*/
|
|
135
|
+
export function detectTargets(home = process.env.DSH_HOME || '', pluginName = '', profileName = '') {
|
|
136
|
+
const hits = [];
|
|
137
|
+
if (!home || !pluginName) return hits;
|
|
138
|
+
// 2026-10-09 入参归一:DSH 自己的 DSH_HOME **含** `.dsh`,而本函数原先一律按「不含 .dsh」拼接
|
|
139
|
+
// ⇒ 直接传 $DSH_HOME 会拼成 `…/.dsh/.dsh/profiles`、探测为空(开发者文档 §5.1 记录的坑)。
|
|
140
|
+
// 现按实存目录判定两种形态;多 profile 时可用 profileName 只看指定 profile(§5.2:自动取首个会选错)。
|
|
141
|
+
const profiles = profilesDirOf(home);
|
|
142
|
+
if (!profiles || !existsSync(profiles)) return hits;
|
|
143
|
+
for (const profile of readdirSync(profiles, { withFileTypes: true })) {
|
|
144
|
+
if (!profile.isDirectory()) continue;
|
|
145
|
+
if (profileName && profile.name !== profileName) continue;
|
|
146
|
+
const base = join(profiles, profile.name);
|
|
147
|
+
// 真实加载源优先(package.json 的 file: 指向这里)
|
|
148
|
+
const local = join(base, 'local-plugins', pluginName);
|
|
149
|
+
if (existsSync(local)) hits.push(local);
|
|
150
|
+
// npm link 产物(保留:部分解析路径走这里)
|
|
151
|
+
const nm = join(base, 'node_modules', pluginName);
|
|
152
|
+
if (existsSync(nm)) hits.push(nm);
|
|
153
|
+
}
|
|
154
|
+
return hits;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 同步(dry-run 或真写入)。
|
|
159
|
+
* @param {object} p { source, target, write }
|
|
160
|
+
* @returns {{ok: boolean, written: number, skipped: number, files: string[], error?: string}}
|
|
161
|
+
*/
|
|
162
|
+
/**
|
|
163
|
+
* 两文件内容是否相同;目标不存在返回 false。
|
|
164
|
+
* 读失败(权限/编码)按「不相同」处理 —— 交给后续复制去覆盖并上报真实错误,
|
|
165
|
+
* 避免在这里吞掉问题。
|
|
166
|
+
*/
|
|
167
|
+
async function fileContentEqual(from, to) {
|
|
168
|
+
try {
|
|
169
|
+
// 异步函数内同步 existsSync 改 fs.promises.access(消除阻塞事件循环)
|
|
170
|
+
try { await access(to); } catch { return false; }
|
|
171
|
+
const [a, b] = await Promise.all([readFile(from, 'utf8'), readFile(to, 'utf8')]);
|
|
172
|
+
return a === b;
|
|
173
|
+
} catch {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export async function syncPlugin({ source = SOURCE_ROOT, target = '', write = false } = {}) {
|
|
179
|
+
if (!target) return { ok: false, written: 0, skipped: 0, files: [], error: '未指定目标目录(用 --target 或配置 DSH_HOME)' };
|
|
180
|
+
const files = await listSyncFiles(source);
|
|
181
|
+
let written = 0;
|
|
182
|
+
let skipped = 0;
|
|
183
|
+
const failures = [];
|
|
184
|
+
for (const rel of files) {
|
|
185
|
+
const from = join(source, rel);
|
|
186
|
+
const to = join(target, rel);
|
|
187
|
+
// 内容不同才写(幂等)
|
|
188
|
+
// 逐文件比较内容(幂等):改 await 后单次遍历不再阻塞事件循环
|
|
189
|
+
const same = await fileContentEqual(from, to);
|
|
190
|
+
if (same) { skipped++; continue; }
|
|
191
|
+
if (write) {
|
|
192
|
+
await mkdir(dirname(to), { recursive: true });
|
|
193
|
+
const r = copyFileCompat(from, to);
|
|
194
|
+
if (!r.ok) {
|
|
195
|
+
// 单个文件失败不再抛出中断整个同步:记录后继续,最后统一上报。
|
|
196
|
+
failures.push({ file: rel, error: r.error });
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
// cli.mjs 是 bin(git-sluice)入口,需可执行权限(shebang 执行)。
|
|
200
|
+
// copyFile 不带原文件 mode,同步后恢复 +x(幂等 chmod;失败不阻塞)。
|
|
201
|
+
if (rel === 'cli.mjs' || rel.endsWith('/cli.mjs')) {
|
|
202
|
+
try { await chmod(to, 0o755); } catch { /* 权限恢复失败不强拦 */ }
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
written++;
|
|
206
|
+
}
|
|
207
|
+
return { ok: failures.length === 0, written, skipped, files, failures };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** CLI 入口。支持 --source <目录>(缺省=本仓库)、--target <目录>(缺省=自动探测)、
|
|
211
|
+
* --profile <名>(多 profile 时显式指定)、--write(真写)、--fix(按目标形态修正安装位置,含 --write 语义)。 */
|
|
212
|
+
export async function main(argv = process.argv.slice(2)) {
|
|
213
|
+
const fix = argv.includes('--fix');
|
|
214
|
+
const write = argv.includes('--write') || fix;
|
|
215
|
+
const si = argv.indexOf('--source');
|
|
216
|
+
const source = si >= 0 ? argv[si + 1] : SOURCE_ROOT;
|
|
217
|
+
const ti = argv.indexOf('--target');
|
|
218
|
+
const pi = argv.indexOf('--profile');
|
|
219
|
+
const profileName = pi >= 0 ? argv[pi + 1] : '';
|
|
220
|
+
const home = process.env.DSH_HOME || '';
|
|
221
|
+
const pluginName = basename(source);
|
|
222
|
+
// 2026-10-09:按 profile 探测(多 profile 时自动取首个会选错——开发者文档 §5.2 已记录该坑)
|
|
223
|
+
const profiles = inspectProfiles(home);
|
|
224
|
+
const picked = profileName
|
|
225
|
+
? { dir: (profiles.find((p) => p.name === profileName) || {}).dir || '', profile: profileName, reason: '按 --profile 指定' }
|
|
226
|
+
: pickProfile(profiles, pluginName);
|
|
227
|
+
if (!ti && picked.ambiguous) {
|
|
228
|
+
console.log(`"${pluginName}" 在多个 profile 都有副本:${(picked.candidates || []).join(' / ')}`);
|
|
229
|
+
console.log('请显式指定:--profile <名>(或 --target <目录>)');
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
const hits = detectTargets(home, pluginName, profileName || picked.profile || '');
|
|
233
|
+
const target = ti >= 0 ? argv[ti + 1] : (hits[0] || '');
|
|
234
|
+
const pd = picked.dir || (profiles.find((p) => p.dir === dirname(dirname(target))) || {}).dir || '';
|
|
235
|
+
if (!ti && !target) {
|
|
236
|
+
console.log(`未探测到 "${pluginName}" 的部署副本:${picked.reason}`);
|
|
237
|
+
if (profiles.length) console.log(` 共 ${profiles.length} 个 profile:${profiles.map((p) => p.name).join(' / ')}`);
|
|
238
|
+
console.log('请显式指定:--profile <名>(或 --target <目录>)');
|
|
239
|
+
return 1;
|
|
240
|
+
}
|
|
241
|
+
const r = await syncPlugin({ source, target, write });
|
|
242
|
+
if (!r.ok) {
|
|
243
|
+
console.log(`同步未执行:${r.error}`);
|
|
244
|
+
console.log(`可用目标(自动探测):${hits.join(' | ') || '(无)'}`);
|
|
245
|
+
return 1;
|
|
246
|
+
}
|
|
247
|
+
console.log(`双副本同步${write ? '' : '(dry-run,加 --write 才写)'}`);
|
|
248
|
+
console.log(` 源:${source}`);
|
|
249
|
+
console.log(` 目标:${target}`);
|
|
250
|
+
console.log(` 待写 ${r.written} 个文件,已一致 ${r.skipped} 个`);
|
|
251
|
+
for (const f of r.files.slice(0, 10)) console.log(` - ${f}`);
|
|
252
|
+
if (r.files.length > 10) console.log(` … 共 ${r.files.length} 个`);
|
|
253
|
+
// ── 安装形态 + 免重启热重载自检 + 依赖声明(2026-10-09:把开发者文档 §3.1/§3.2/§5 的检查内置)──
|
|
254
|
+
if (pd) {
|
|
255
|
+
const hmr = readHmrConfig(pd);
|
|
256
|
+
const verdict = judgeHmr({ hmr, profileDir: pd, pluginName });
|
|
257
|
+
console.log(` profile:${pd}`);
|
|
258
|
+
console.log(` 免重启热重载:${verdict.hot ? '✅ 可(改代码后同步即生效)' : `❌ 不可 —— ${verdict.reason}`}`);
|
|
259
|
+
const actions = planInstall({ profileDir: pd, pluginName });
|
|
260
|
+
if (actions.length) {
|
|
261
|
+
console.log(' 安装形态待办(目标:local-plugins 真实目录 + node_modules 相对软链):');
|
|
262
|
+
for (const a of actions) console.log(` · [${a.kind}] ${a.desc}`);
|
|
263
|
+
if (fix) {
|
|
264
|
+
const res = applyInstall(actions, { on: true });
|
|
265
|
+
console.log(` 已执行:${res.done.join(', ') || '(无)'}${res.skipped.length ? `|跳过:${res.skipped.join(', ')}` : ''}`);
|
|
266
|
+
} else {
|
|
267
|
+
console.log(' (加 --fix 才执行:删软链/建相对链,真实文件由上面的同步步骤落盘)');
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
for (const h of depHints(pd, pluginName)) console.log(` ⚠️ ${h}`);
|
|
271
|
+
}
|
|
272
|
+
return 0;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// 顶层 await:main 已异步,未 await 的话 rejection 会成为 unhandled rejection
|
|
276
|
+
// (进程静默退出,退出码不对),故显式 await 并回传退出码。
|
|
277
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
278
|
+
process.exitCode = await main();
|
|
279
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 上下文注入自检脚本(配合 1.5.2 工具探测改上下文注入)
|
|
3
|
+
*
|
|
4
|
+
* 真实调用 apply() 并触发 agent/pre-step,验证环境信息按上下文注入:
|
|
5
|
+
* - 工作区跑:验证接线正确 + 无 DSH 依赖时降级不崩(@deepseek-ai/dsh-llm 解析不到 → 注入跳过)
|
|
6
|
+
* - 安装副本跑(profiles/.../node_modules/dsh-git-push):完整验证注入 user 消息
|
|
7
|
+
* (工作区目录 + 工具安装路径(实测)+ skill 总入口,source 标记 plugin instructions)
|
|
8
|
+
*
|
|
9
|
+
* 用法:node scripts/verify-prestep.mjs
|
|
10
|
+
* 退出码:0 = 接线与(可注入时)注入全部符合预期;1 = 断言失败
|
|
11
|
+
*/
|
|
12
|
+
import { apply } from '../lib/app/apply.js';
|
|
13
|
+
import { setDefineToolOverride } from '../lib/plugin/index.js';
|
|
14
|
+
|
|
15
|
+
/** 被验证的宿主事件名(pre-step:上下文注入发生在 agent 回合前)。 */
|
|
16
|
+
const PRE_STEP_EVENT = 'agent/pre-step';
|
|
17
|
+
|
|
18
|
+
/** 简易断言计数(不引测试框架)。 */
|
|
19
|
+
const results = [];
|
|
20
|
+
function check(name, ok) { results.push([name, !!ok]); if (!ok) console.log(' ❌', name); }
|
|
21
|
+
|
|
22
|
+
// 1) defineTool 喂 mock(工作区无 DSH 依赖;安装副本同样可喂)
|
|
23
|
+
setDefineToolOverride((spec) => spec);
|
|
24
|
+
|
|
25
|
+
// 2) mock DSH ctx:tools/systemPrompt/webServer 服务 + agent/pre-step 事件捕获
|
|
26
|
+
const sections = [];
|
|
27
|
+
const events = {};
|
|
28
|
+
const registered = [];
|
|
29
|
+
const ctx = {
|
|
30
|
+
get: (k) => (k === 'log' ? { info: () => {}, warn: (...a) => console.log(' [warn]', ...a) } : undefined),
|
|
31
|
+
inject: (keys, fn) => {
|
|
32
|
+
if (keys[0] === 'tools') fn({ get: (k) => (k === 'tools' ? { register: (t) => registered.push(t) } : undefined) });
|
|
33
|
+
if (keys[0] === 'systemPrompt') fn({ get: (k) => (k === 'systemPrompt' ? { section: (s) => sections.push(s) } : undefined) });
|
|
34
|
+
if (keys[0] === 'webServer') fn({ get: (k) => (k === 'webServer' ? { register: () => {} } : undefined) });
|
|
35
|
+
},
|
|
36
|
+
on: (name, handler) => { events[name] = handler; },
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// 3) 真实调用 apply
|
|
40
|
+
await apply(ctx, { workspaceRoot: process.cwd() });
|
|
41
|
+
setDefineToolOverride(null);
|
|
42
|
+
|
|
43
|
+
console.log('== 注册统计 ==');
|
|
44
|
+
console.log('systemPrompt sections:', sections.map((s) => s.name).join(', ') || '(空)');
|
|
45
|
+
console.log('agent/pre-step 接线:', typeof events[PRE_STEP_EVENT] === 'function' ? '✅ 是' : '❌ 否');
|
|
46
|
+
console.log('工具注册数:', registered.length);
|
|
47
|
+
|
|
48
|
+
// 环境段不应再注册为 systemPrompt section(迁出)
|
|
49
|
+
check('systemPrompt 不含环境段(dsh-git-push-env 已迁出)', !sections.some((s) => s.name === 'dsh-git-push-env'));
|
|
50
|
+
check('systemPrompt 保留功能用法段', sections.some((s) => s.name === 'dsh-git-push-usage'));
|
|
51
|
+
check('agent/pre-step 已接线', typeof events[PRE_STEP_EVENT] === 'function');
|
|
52
|
+
check('工具已注册', registered.length >= 8);
|
|
53
|
+
|
|
54
|
+
if (typeof events[PRE_STEP_EVENT] !== 'function') {
|
|
55
|
+
console.log('\n❌ 未接线,退出(exit 1)');
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 4) 构造「首次 step」decision 并触发 pre-step
|
|
60
|
+
const decision = {
|
|
61
|
+
kind: 'next',
|
|
62
|
+
messages: [{ role: 'user', content: [{ type: 'text', text: '(原有消息)' }] }],
|
|
63
|
+
};
|
|
64
|
+
const agent = { id: 'verify-agent-1', session: { id: 'verify-session' } };
|
|
65
|
+
const first = await events[PRE_STEP_EVENT]({ agent, signal: undefined }, async () => decision);
|
|
66
|
+
const count1 = first.messages.length;
|
|
67
|
+
const injected = count1 > 1 ? first.messages[count1 - 1] : null;
|
|
68
|
+
|
|
69
|
+
console.log('\n== 首次触发 ==');
|
|
70
|
+
check('首次触发追加 1 条注入消息', count1 === 2);
|
|
71
|
+
if (injected) {
|
|
72
|
+
check('注入消息 role=user', injected.role === 'user');
|
|
73
|
+
check('注入消息 source 标记(plugin/dsh-git-push/instructions)',
|
|
74
|
+
injected.source?.kind === 'plugin' && injected.source?.plugin === 'dsh-git-push' && injected.source?.form === 'instructions');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// 5) 再次触发同一 agent:WeakSet 防重复,不应再追加
|
|
78
|
+
const second = await events[PRE_STEP_EVENT]({ agent, signal: undefined }, async () => decision);
|
|
79
|
+
check('二次触发不重复注入(回到原 1 条)', second.messages.length === 1);
|
|
80
|
+
|
|
81
|
+
// 6) 注入正文内容校验(注入成功时)
|
|
82
|
+
if (injected) {
|
|
83
|
+
const text = injected.content?.[0]?.text || '';
|
|
84
|
+
console.log('\n== 注入上下文正文 ==\n----------------------------------------');
|
|
85
|
+
console.log(text);
|
|
86
|
+
console.log('----------------------------------------');
|
|
87
|
+
check('含工作区根目录', /工作区根目录/.test(text));
|
|
88
|
+
check('含当前工作目录 cwd', /当前工作目录 cwd/.test(text));
|
|
89
|
+
check('含工具安装路径', /工具:/.test(text));
|
|
90
|
+
check('含 git 实测路径', /git=/.test(text));
|
|
91
|
+
check('含 skill 总入口', /skills/.test(text));
|
|
92
|
+
} else {
|
|
93
|
+
console.log('\n(未注入:当前运行环境无 @deepseek-ai/dsh-llm 依赖,属预期降级;')
|
|
94
|
+
console.log(' 在安装副本 profiles/.../node_modules/dsh-git-push 下运行可完整验证注入)');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 7) 汇总
|
|
98
|
+
const failed = results.filter(([, ok]) => !ok).length;
|
|
99
|
+
console.log(`\n== 结果:${results.length - failed}/${results.length} 通过 ==`);
|
|
100
|
+
process.exit(failed ? 1 : 0);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: audit-rule-authoring
|
|
3
|
+
description: 给 dsh-git-push 审计引擎新增规则的要点与坑:allow/deny 两种精筛语义、allow 语义下必须自查注释与字符串、判据收窄前先在自己仓库量命中量、用真实样本当回归测试、诚实标注代理特征。处理「新增审计规则」「性能规则」「规则误报太多」「astConfirmKind」类场景时加载。
|
|
4
|
+
whenToUse: 给本插件(或同类规则引擎)新增/调整审计规则时;规则误报或漏报排查时。
|
|
5
|
+
generatedBy: deepseek-v4-flash(2026-10-08)
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# 审计规则编写要点(dsh-git-push)
|
|
9
|
+
|
|
10
|
+
> **先记住我**:任何会话开始前,先读用户档案 `ai-work-archive/skills/remember-me.md`(优先级最高)。
|
|
11
|
+
> 本 skill 为**插件相关**,权威位置:插件项目 `skills/dev/`。
|
|
12
|
+
|
|
13
|
+
## 一、规则形态(沿用既有架构,不另造机制)
|
|
14
|
+
|
|
15
|
+
| 层 | 文件 | 职责 |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
规则声明 | `lib/audit-rules/audit-rules-<槽位>.yml` | id / category / severity / exts / patterns / mitigation |
|
|
18
|
+
AST 精筛 | `lib/ast/<模块>.js` | 提供「命中行号集合」的判定函数(如 `loopBodyLines`) |
|
|
19
|
+
引擎派发 | `lib/checks/regex.js` | 按 `rule.astConfirmKind` 取精筛集合 |
|
|
20
|
+
取值出口 | `lib/checks/common.js` | 统一导出精筛集合取值函数(约定:都从这里取) |
|
|
21
|
+
|
|
22
|
+
## 二、两种精筛语义(关键)
|
|
23
|
+
|
|
24
|
+
| 语义 | 含义 | 典型用法 |
|
|
25
|
+
|---|---|---|
|
|
26
|
+
`deny` | 集合内的行**豁免** | 受控小文件读取不算 memory-bomb |
|
|
27
|
+
`allow` | **只有**集合内的行才报 | token 级短名、循环体内的命中 |
|
|
28
|
+
|
|
29
|
+
## 三、坑(都踩过)
|
|
30
|
+
|
|
31
|
+
1. **allow 语义会跳过引擎的通用 `codeFilter`** → 注释/字符串里的示例**不会被自动过滤**。
|
|
32
|
+
实测:规则说明注释里写了 `Array.from(x.values())` 示例(恰在 `for` 循环体内),被自己的规则命中。
|
|
33
|
+
→ 用 `tokenize()` 做 **token 级排除**:同行存在 `comment`/`str`/`tmpl` 且文本包含命中串则不算命中。
|
|
34
|
+
2. **判据先宽后窄必须量化**:第一版规则在我们仓库炸出 **218 处**(`log/push/emit/readFile` 这类
|
|
35
|
+
重复调用是有意为之)→ 收窄到「查询/解析类函数名」后 **4 处**。
|
|
36
|
+
→ 纪律:**新规则提交前先在自己仓库跑一遍数命中量**,量级不合理先收窄。
|
|
37
|
+
3. **edit 锚点行会被自己吃掉**:把「函数注释头」当锚点替换 → 留下孤儿注释 → `SyntaxError`。
|
|
38
|
+
→ 凡 edit 后立刻 `node --check`;「在某行前插入」时锚点行必须原样出现在新内容里。
|
|
39
|
+
4. **不要用「同一函数内占比高」证明「某几行是元凶」** → 改完复测才作数(见 `perf-diagnosis-method`)。
|
|
40
|
+
|
|
41
|
+
## 四、测试写法
|
|
42
|
+
|
|
43
|
+
- **用真实样本当回归测试**:把外部真实项目里的问题片段(如 `symbol-table.ts` 的循环内物化结构)
|
|
44
|
+
写成 fixture 字符串,断言「必须命中」;再配「循环外/实参不同/注释里」的负样本断言「不报」。
|
|
45
|
+
- 端到端再补一条:`checkRegexRules({ file, text, rules: [<规则对象>] })` 断言命中数与规则 id
|
|
46
|
+
(规则对象用 `subPatterns: [{ regex, message }]` 形态,`patterns: [{regex}]` 会被引擎当 RegExp 用而报错)。
|
|
47
|
+
|
|
48
|
+
## 五、诚实标注
|
|
49
|
+
|
|
50
|
+
规则是**代理特征**,不是判决器:
|
|
51
|
+
- 「循环内全量物化」能静态判 → 可作规则;
|
|
52
|
+
- 「入参部分重叠的重复调用」「函数是否真的贵」→ 静态判不出来,只能靠运行时探针;
|
|
53
|
+
- 结论里要写清「本规则抓什么、抓不到什么」,避免使用者误以为规则=全知。
|
|
54
|
+
|
|
55
|
+
## 配套
|
|
56
|
+
|
|
57
|
+
- `perf-diagnosis-method`(性能定位与等价性验证)、`diagnostic-to-code-rule`(诊断转规则)、
|
|
58
|
+
- `code-detailed-comments`(注释写为什么)、`test-design-norms`(测试路径与隔离规范)。
|