dsh-git-push 0.0.0-stage → 2.8.4
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 +1482 -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 +262 -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 +191 -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 +662 -0
- package/lib/app/tools.js +40 -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 +207 -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 +131 -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 +144 -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 +470 -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 +167 -0
- package/lib/npm/registry.js +147 -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 +193 -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/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,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-git-push 规则总入口(核心)
|
|
3
|
+
*
|
|
4
|
+
* 铁律:加字段 = 加函数 + registerCompiler 一行注册;compileRule 主体永不修改。
|
|
5
|
+
* 判定方式:不强制 yml 写 kind,按字段自动指派编译函数(detect 条件)。
|
|
6
|
+
* 每个编译函数声明 dimensions(10 维度绑定,支持一字段多维度)。
|
|
7
|
+
*/
|
|
8
|
+
import { findHomoglyphs } from './homoglyph.js';
|
|
9
|
+
|
|
10
|
+
export const RULE_COMPILERS = [];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 注册一个规则编译函数。
|
|
14
|
+
* @param {string} kind kebab-case 规则类型(如 'max-depth')
|
|
15
|
+
* @param {(rule)=>boolean} detect 认领条件(规则条目满足即归本编译函数)
|
|
16
|
+
* @param {(rule, ctx)=>any} compile 编译函数,返回 { ok:true, rule } 或 { ok:false, error }
|
|
17
|
+
*/
|
|
18
|
+
export function registerCompiler(kind, detect, compile) {
|
|
19
|
+
RULE_COMPILERS.push({ kind, detect, compile });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 统一规则编译入口:遍历注册表,detect 命中 → compile。
|
|
24
|
+
* 显式 kind 字段优先;无 kind 则字段探测。
|
|
25
|
+
* @param {object} rule yml 规则条目
|
|
26
|
+
* @param {object} ctx { errors, label } 编译上下文
|
|
27
|
+
*/
|
|
28
|
+
export function compileRule(rule, ctx = {}) {
|
|
29
|
+
// G3 防再犯:kind/id 夹带同形字符(с→c/е→e/а→a 等)→ 匹配不上注册表,
|
|
30
|
+
// 静默失效零提示。入口显式拦截:检出同形 → 报错(不进入 detect/compile)。
|
|
31
|
+
for (const key of ['kind', 'id']) {
|
|
32
|
+
if (typeof rule?.[key] !== 'string') continue;
|
|
33
|
+
const hits = findHomoglyphs(rule[key]);
|
|
34
|
+
if (hits.length) {
|
|
35
|
+
const detail = hits.map((h) => `「${h.ch}」→${h.ascii}@${h.index}`).join(' ');
|
|
36
|
+
return { ok: false, error: `${key}=${rule[key]} 含同形字符(${detail}),须用纯 ASCII(可复制正确写法或人工重敲)` };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// 槽位归属透传——loader 给每条 yml 规则打了 __slot(来源规则包),
|
|
40
|
+
// 编译结果统一补 slot 字段,供审计结果按规则包聚合命中数
|
|
41
|
+
// (侧边栏规则包行的「拦截/警告/通过」= 该规则包实际命中的问题数)。
|
|
42
|
+
const slot = typeof rule?.__slot === 'string' ? rule.__slot : undefined;
|
|
43
|
+
const withSlot = (res) => {
|
|
44
|
+
if (res && res.ok && res.rule) {
|
|
45
|
+
if (slot && res.rule.slot === undefined) res.rule.slot = slot;
|
|
46
|
+
// 试点:external 透传(yml external: true 的规则编译后保留——
|
|
47
|
+
// groupByKind 据此内置停跑,由 audit-ext 统一动态入口执行)
|
|
48
|
+
if (res.rule.external === undefined && rule.external !== undefined) res.rule.external = rule.external;
|
|
49
|
+
}
|
|
50
|
+
return res;
|
|
51
|
+
};
|
|
52
|
+
if (rule?.kind) {
|
|
53
|
+
const byKind = RULE_COMPILERS.find((e) => e.kind === rule.kind);
|
|
54
|
+
if (byKind) return withSlot(byKind.compile(rule, ctx));
|
|
55
|
+
return { ok: false, error: `未知规则类型 kind=${rule.kind}(${rule?.id || rule?.name})` };
|
|
56
|
+
}
|
|
57
|
+
for (const entry of RULE_COMPILERS) {
|
|
58
|
+
if (entry.detect(rule)) return withSlot(entry.compile(rule, ctx));
|
|
59
|
+
}
|
|
60
|
+
return { ok: false, error: `无法识别规则类型: ${rule?.id || rule?.name || '?'}` };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 编译全部规则条目(阶段3 统一入口的循环形态)。 */
|
|
64
|
+
export function compileAllRules(rules, ctx = {}) {
|
|
65
|
+
const out = [];
|
|
66
|
+
for (const r of rules || []) {
|
|
67
|
+
const compiled = compileRule(r, ctx);
|
|
68
|
+
if (compiled.ok) out.push(withRuleScope(compiled.rule, r));
|
|
69
|
+
else ctx.errors?.push(compiled.error);
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 统一补齐规则作用域字段(exts + astConfirmKind)——。
|
|
76
|
+
*
|
|
77
|
+
* 背景(真实缺陷):`exts` 原本要求每个编译器自己归一化后塞进 `extra` 透传,
|
|
78
|
+
* 16 个编译器里 10 个漏了(credential-ref/[FUNC]/semantic/blacklist/npm-json 等)。
|
|
79
|
+
* 后果是 yml 里写了 `exts` 却被**静默忽略**——规则照旧跑遍所有文本文件,
|
|
80
|
+
* 产生误报且看不出原因(实例:comment 包的 blacklist 规则声明了只查代码/HTML,
|
|
81
|
+
* 仍把 .json 配置文件的流程约定报成 blocker,卡死提交门禁)。
|
|
82
|
+
*
|
|
83
|
+
* 放在这里而不是各编译器:作用域是规则的通用属性,与具体 kind 无关;
|
|
84
|
+
* 统一收口后新增编译器自动具备,不会再漏。已在编译器内显式归一化的保持不变。
|
|
85
|
+
*
|
|
86
|
+
* @param compiled 编译产物(顶层 exts 或 extra.exts 均可被 filterRulesByExt 读取)
|
|
87
|
+
* @param source 原始 yml 规则
|
|
88
|
+
*/
|
|
89
|
+
function withRuleScope(compiled, source) {
|
|
90
|
+
if (!compiled) return compiled;
|
|
91
|
+
let out = compiled;
|
|
92
|
+
const patch = (obj, key, value) => (out = out === obj ? { ...obj, [key]: value } : { ...out, [key]: value });
|
|
93
|
+
|
|
94
|
+
// ① 文件作用域 exts
|
|
95
|
+
if (!(Array.isArray(out.exts) && out.exts.length) && Array.isArray(source?.exts) && source.exts.length) {
|
|
96
|
+
patch(out, 'exts', source.exts.map((x) => String(x).toLowerCase().replace(/^\./, '')));
|
|
97
|
+
}
|
|
98
|
+
// ② 具名精筛 astConfirmKind —— 与 exts 同类缺陷:原本只有 regex 编译器透传,
|
|
99
|
+
// [FUNC]/blacklist/semantic 等 kind 声明了也被静默忽略(实例:secret-github-pat
|
|
100
|
+
// 编译成 [FUNC],声明的 placeholder-credential 精筛不生效,占位符仍被报泄漏)。
|
|
101
|
+
if (out.astConfirmKind === undefined && source?.astConfirmKind) {
|
|
102
|
+
patch(out, 'astConfirmKind', source.astConfirmKind);
|
|
103
|
+
}
|
|
104
|
+
// ③ 作用域规则 scope_rules —— 与上面 ①exts / ②astConfirmKind **同类缺陷**:
|
|
105
|
+
// loader 已把 yml 的 scope_rules 规范化为 source.scopeRules(lib/rule/loader.js),
|
|
106
|
+
// 但编译器重建规则对象时只透传了 exts/astConfirmKind,scopeRules 被丢弃
|
|
107
|
+
// ⇒ yml 里声明的 scope_rules 在生产中**静默失效**。
|
|
108
|
+
// 实测影响:startup/serialization 的 exempt(max-function-length / max-complexity 等)
|
|
109
|
+
// 与 DSH 客户端单文件的 downgrade **全都不生效**,看起来只有检查器里的兜底在起作用。
|
|
110
|
+
if (!(Array.isArray(out.scopeRules) && out.scopeRules.length)) {
|
|
111
|
+
const srcScope = source?.scopeRules || source?.scope_rules;
|
|
112
|
+
if (Array.isArray(srcScope) && srcScope.length) patch(out, 'scopeRules', srcScope);
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 审计作用域判断 · 机制层(方案 Step 3)
|
|
3
|
+
*
|
|
4
|
+
* 目标:让规则知道「这段代码在什么作用域/路径上」,从“看代码长什么样”升级到
|
|
5
|
+
* “理解代码在干什么”——误报率从 ~97%(io-risk 实测 2/65 真警告)往 ~15% 收敛。
|
|
6
|
+
*
|
|
7
|
+
* 本文件只做机制(声明 + 解析),不判断任何具体作用域:
|
|
8
|
+
* - normalizeScopeRules:编译期把规则 yml 的 scope_rules 规范成 rule.scopeRules 数组
|
|
9
|
+
* - resolveScopeAction:执行期按运行时填充的 scopeInfo(path/lexical/effective)取 action
|
|
10
|
+
*
|
|
11
|
+
* 作用域命名空间(规则 yml 里 scope 字段可用的值,多值逗号分隔,* 全匹配):
|
|
12
|
+
* - 执行路径:startup / request / background / test / unknown
|
|
13
|
+
* - 词法作用域:global / module / function / block / loop
|
|
14
|
+
* - 功能作用域(闭包双重作用域):public / private / callback
|
|
15
|
+
* action:exempt(豁免)/ block(阻断)/ warn(警告)/ downgrade(降级),缺省 warn。
|
|
16
|
+
* - exempt :命中即**跳过**该命中(不出现在报告里);
|
|
17
|
+
* - block :命中即升级为阻断(用于「这个作用域里这么做是硬错误」);
|
|
18
|
+
* - warn :保持规则自身分级(缺省行为);
|
|
19
|
+
* - downgrade :**仍出现在报告里**,但降为 info 且不扣分——用于「平台/框架约束导致不可避免」
|
|
20
|
+
* 的场景(如 DSH 要求客户端为单文件,该文件必然超长且不可拆分)。
|
|
21
|
+
*
|
|
22
|
+
* 与规则检查器约定(Step 4 起各规则填充):
|
|
23
|
+
* scopeInfo = { path: 'request'|'startup'|..., lexical: 'function'|'loop'|..., effective: 'public'|... }
|
|
24
|
+
* —— rule.scopeRules 命中任一维度值即返回对应 action;无 scope_rules 或未命中 → null(规则默认行为)。
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** 合法 action 白名单(缺省 warn——未知作用域保守提示,不豁免也不阻断)。 */
|
|
28
|
+
export const SCOPE_ACTIONS = ['exempt', 'block', 'warn', 'downgrade'];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 规范化规则 yml 的 scope_rules 字段(编译期,幂等)。
|
|
32
|
+
* @param {object} rule 编译后的规则对象(yml 字段已透传:rule.scope_rules)
|
|
33
|
+
* @returns {Array<{scope:string, action:string, reason:string, _i:number}>}
|
|
34
|
+
*/
|
|
35
|
+
export function normalizeScopeRules(rule) {
|
|
36
|
+
const raw = rule && (rule.scope_rules || rule.scopeRules);
|
|
37
|
+
if (!Array.isArray(raw)) return [];
|
|
38
|
+
return raw.map((x, i) => ({
|
|
39
|
+
scope: String((x && x.scope) || '').trim(),
|
|
40
|
+
action: SCOPE_ACTIONS.includes(x && x.action) ? x.action : 'warn',
|
|
41
|
+
reason: (x && x.reason) || '',
|
|
42
|
+
_i: i,
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 执行期:按运行时作用域信息取 action。
|
|
48
|
+
* @param {Array<{scope:string, action:string}>} scopeRules 规范化后的规则作用域规则(rule.scopeRules)
|
|
49
|
+
* @param {object} [scopeInfo] 运行时填充:{ path, lexical, effective }(值即作用域字符串,如 'request')
|
|
50
|
+
* @returns {'exempt'|'block'|'warn'|null} action;无规则/无命中 → null(规则默认行为)
|
|
51
|
+
*/
|
|
52
|
+
export function resolveScopeAction(scopeRules, scopeInfo = {}) {
|
|
53
|
+
if (!Array.isArray(scopeRules) || scopeRules.length === 0) return null;
|
|
54
|
+
for (const sr of scopeRules) {
|
|
55
|
+
const keys = String(sr.scope || '').split(',').map((s) => s.trim()).filter(Boolean);
|
|
56
|
+
if (!keys.length) continue;
|
|
57
|
+
if (keys.includes('*')) return sr.action;
|
|
58
|
+
const hit = keys.some((k) => {
|
|
59
|
+
for (const v of Object.values(scopeInfo)) {
|
|
60
|
+
if (v != null && String(v) === k) return true;
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
});
|
|
64
|
+
if (hit) return sr.action;
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档加分制检查器——文档维度从扣分制改纯加分制(0 分起、上限 10)。
|
|
3
|
+
*
|
|
4
|
+
* 原则(需求讨论 DeepSeekHarness/文档加分制.md + docs/方案-文档维度加分制.md):
|
|
5
|
+
* · 只奖励「做了」(结构信号 + 交叉验证),不判断「做得好」——内容质量机器判不了
|
|
6
|
+
* · 检查范围=**文档集**:README.md(约定入口)+ docs/ 下递归 .md(排除 node_modules 等)
|
|
7
|
+
* + 根级常见文档名白名单(CHANGELOG/INSTALL/GUIDE/UPGRADING/升级记录/快速开始…)
|
|
8
|
+
* · 版本一致性是唯一硬交叉验证:文档集**任一**版本号 == package.json version
|
|
9
|
+
* (README 里的 Node 18.0.0 等无关版本不会误加)
|
|
10
|
+
* · 多文档版本号互相矛盾(README=1.5.6 vs CHANGELOG=1.5.5):存在性加分照给,
|
|
11
|
+
* 矛盾只进 review(人工复核)**不扣分**——与 version/readme-changelog 口径错开
|
|
12
|
+
* 约束:结果**不进 findings**(不进扣分维度/门禁/问题计数),由 scoreQuality 单独加分。
|
|
13
|
+
*/
|
|
14
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
|
|
17
|
+
/** 单项文档加分项满分(每题 2.5 分,4 题合计 10 分)。 */
|
|
18
|
+
const DOCS_ITEM_SCORE = 2.5;
|
|
19
|
+
|
|
20
|
+
/** 根级常见文档名白名单(docs/ 之外的「项目文档」识别)。 */
|
|
21
|
+
const ROOT_DOC_NAMES = [
|
|
22
|
+
'CHANGELOG.md', 'INSTALL.md', 'GUIDE.md', 'UPGRADING.md',
|
|
23
|
+
'升级记录.md', '快速开始.md', '安装.md', 'change.log', 'CHANGELOG',
|
|
24
|
+
];
|
|
25
|
+
/** docs/ 遍历时跳过的目录(复用审计收集器语义,不把依赖/夹具文档当项目文档)。 */
|
|
26
|
+
const SKIP_DIRS = new Set(['node_modules', 'vendor', 'test', 'tests', 'dist', 'build', '.git', 'coverage']);
|
|
27
|
+
/** 版本号提取(语义与文档讨论一致:v?x.y.z,任一匹配即可)。 */
|
|
28
|
+
const VERSION_RE = /v?(\d+\.\d+\.\d+)/g;
|
|
29
|
+
/**
|
|
30
|
+
* 「版本记录语境」行:md 标题 / 表格版本列 / version:·版本: 前缀 / CHANGELOG 条目(- 开头)。
|
|
31
|
+
* 用于**不一致检测**——排除正文「Node 18.0.0 支持」这类环境版本(非项目版本口径,
|
|
32
|
+
* 若全提取会频繁误报不一致);加分(存在性)仍用全文提取的宽松集合。
|
|
33
|
+
*/
|
|
34
|
+
const VERSION_RECORD_LINE_RE = /^(#+\s.*v?\d+\.\d+\.\d+|.*\|[^|]*v?\d+\.\d+\.\d+[^|]*\|.*|.*\b(version|版本).{0,4}[::].*v?\d+\.\d+\.\d+.*|[-*]\s+v?\d+\.\d+\.\d+.*)/i;
|
|
35
|
+
/** 安装/启动命令结构信号。 */
|
|
36
|
+
const INSTALL_RE = /npm install|pnpm add|yarn add|docker run|npm run (start|dev|build|serve)/i;
|
|
37
|
+
/** 环境变量清单结构信号。 */
|
|
38
|
+
const ENV_RE = /\.env|process\.env|环境变量/i;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 收集项目文档集(README + docs/ 下递归 + 根级常见命名),去重、保序。
|
|
42
|
+
* @param {string} root 项目根
|
|
43
|
+
* @returns {string[]} 文档文件绝对路径
|
|
44
|
+
*/
|
|
45
|
+
export function collectDocFiles(root = '.') {
|
|
46
|
+
const out = [];
|
|
47
|
+
const push = (p) => { try { if (existsSync(p) && statSync(p).isFile()) out.push(p); } catch { /* 忽略 */ } };
|
|
48
|
+
// ① README 入口(大小写兼容)
|
|
49
|
+
push(join(root, 'README.md'));
|
|
50
|
+
push(join(root, 'readme.md'));
|
|
51
|
+
// ② docs/ 递归(排除 SKIP_DIRS)
|
|
52
|
+
const docs = join(root, 'docs');
|
|
53
|
+
if (existsSync(docs)) {
|
|
54
|
+
const walk = (dir) => {
|
|
55
|
+
let ents;
|
|
56
|
+
try { ents = readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
57
|
+
for (const ent of ents) {
|
|
58
|
+
if (SKIP_DIRS.has(ent.name)) continue;
|
|
59
|
+
const p = join(dir, ent.name);
|
|
60
|
+
if (ent.isDirectory()) walk(p);
|
|
61
|
+
else if (ent.name.endsWith('.md') || ent.name.endsWith('.markdown')) out.push(p);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
walk(docs);
|
|
65
|
+
}
|
|
66
|
+
// ③ 根级常见文档名
|
|
67
|
+
for (const n of ROOT_DOC_NAMES) push(join(root, n));
|
|
68
|
+
return [...new Set(out)];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 4 项加分项静态定义(配置 yml audit-rules-docs.yml 与这里一一对应,增删改两处)。 */
|
|
72
|
+
export const DOCS_SCORE_ITEMS = [
|
|
73
|
+
{ id: 'readme-exists', name: 'README 存在', score: DOCS_ITEM_SCORE },
|
|
74
|
+
{ id: 'version-consistent', name: '版本号与 package.json 一致', score: DOCS_ITEM_SCORE },
|
|
75
|
+
{ id: 'install-command', name: '有安装/启动命令', score: DOCS_ITEM_SCORE },
|
|
76
|
+
{ id: 'env-list', name: '有环境变量清单', score: DOCS_ITEM_SCORE },
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 对项目根跑文档加分检查。
|
|
81
|
+
* @param {string} root 项目根(审计目标目录;仓库根/快照根均适用)
|
|
82
|
+
* @returns {{hits: string[], review: string[], items: Array<{id,name,score,hit}>, files: string[], versions: string[]}}
|
|
83
|
+
*/
|
|
84
|
+
export function checkDocsScore(root = '.') { // dsh-skip-func-length dsh-skip-complexity: 文档加分判定器(四检查链 + 宿主版本比对多条件,职责单一;53 行/24 复杂度为结构必然)
|
|
85
|
+
const files = collectDocFiles(root);
|
|
86
|
+
// 预读为 Map<path, text>(文档文件数有限,一次性读取;供全部检查复用,
|
|
87
|
+
// 避免宿主比对循环内二次 readFileSync——io-risk 会按「循环内同步读」报中风险)
|
|
88
|
+
const textsByFile = new Map();
|
|
89
|
+
// dsh-skip-residue(io-risk:文档文件数有限,一次性启动读取,非热路径循环)
|
|
90
|
+
for (const f of files) { try { textsByFile.set(f, readFileSync(f, 'utf8')); } catch { /* 读失败跳过 */ } } // dsh-skip-residue(io-risk:文档数有限一次性读取)
|
|
91
|
+
const all = [...textsByFile.values()].join('\n');
|
|
92
|
+
const hits = [];
|
|
93
|
+
const review = [];
|
|
94
|
+
// ① README 存在(约定入口;docs/ 不能替代此项)
|
|
95
|
+
const hasReadme = existsSync(join(root, 'README.md')) || existsSync(join(root, 'readme.md'));
|
|
96
|
+
if (hasReadme) hits.push('readme-exists');
|
|
97
|
+
else if (files.length) review.push('README 缺失(仅有 docs/ 等文档)');
|
|
98
|
+
// ② 版本一致性(交叉验证,最硬):文档集任一版本号 == pkg.version
|
|
99
|
+
let pkgVersion = '';
|
|
100
|
+
try {
|
|
101
|
+
pkgVersion = String(JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')).version || '').trim();
|
|
102
|
+
} catch { /* 无 package.json / 解析失败:版本项跳过,其余照判 */ }
|
|
103
|
+
const versions = [...new Set([...all.matchAll(VERSION_RE)].map((m) => m[1]))];
|
|
104
|
+
if (pkgVersion && versions.includes(pkgVersion)) hits.push('version-consistent');
|
|
105
|
+
// ③ 安装/启动命令
|
|
106
|
+
if (INSTALL_RE.test(all)) hits.push('install-command');
|
|
107
|
+
// ④ 环境变量清单
|
|
108
|
+
if (ENV_RE.test(all)) hits.push('env-list');
|
|
109
|
+
// 多文档口径不一致(存在性加分照给,矛盾只提示不扣分;仅看版本记录语境,排除环境版本)。
|
|
110
|
+
// 修:原「记录语境版本数 >1 就报」误判——CHANGELOG 版本表天然含全部历史版本
|
|
111
|
+
// (几十条)必然 >1,永不消失。现改为:**只比对版本宿主文档**(README 的「(当前)」
|
|
112
|
+
// 标记行 + CHANGELOG/版本表 的最新条目),各宿主声明的当前版本互相矛盾才算不一致。
|
|
113
|
+
// 普通 docs 文档(方案/细则/示例)不参与——其中的版本号是历史引用或 IP/端口示例
|
|
114
|
+
// (如 `127.0.0.1`),不是当前版本声明。
|
|
115
|
+
if (pkgVersion) {
|
|
116
|
+
const hostVersions = new Map(); // 宿主文件 -> 声明的当前版本
|
|
117
|
+
for (const f of files) {
|
|
118
|
+
const isHost = /README\.md$/i.test(f) || /CHANGELOG|版本表|版本记录/i.test(f);
|
|
119
|
+
if (!isHost) continue;
|
|
120
|
+
const text = textsByFile.get(f);
|
|
121
|
+
if (!text) continue;
|
|
122
|
+
// README:优先「(当前)」标记行;CHANGELOG/版本表:取版本表(`| 版本 |`)首条数据行
|
|
123
|
+
const curLine = text.split('\n').find((l) => /\d+\.\d+\.\d+.*(当前)|(当前).*\d+\.\d+\.\d+/.test(l) && VERSION_RECORD_LINE_RE.test(l));
|
|
124
|
+
if (curLine) {
|
|
125
|
+
const vers = [...curLine.matchAll(VERSION_RE)].map((m) => m[1]);
|
|
126
|
+
if (vers.length) hostVersions.set(f, vers[0]);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const recs = [...new Set(
|
|
130
|
+
text.split('\n')
|
|
131
|
+
.filter((line) => VERSION_RECORD_LINE_RE.test(line))
|
|
132
|
+
.flatMap((line) => [...line.matchAll(VERSION_RE)].map((m) => m[1])),
|
|
133
|
+
)];
|
|
134
|
+
if (recs.length) hostVersions.set(f, maxVersion(recs));
|
|
135
|
+
}
|
|
136
|
+
const distinct = [...new Set(hostVersions.values())];
|
|
137
|
+
if (distinct.length > 1) {
|
|
138
|
+
const shown = [...hostVersions.entries()]
|
|
139
|
+
.map(([, v]) => (v === pkgVersion ? `${v}(=pkg)` : v))
|
|
140
|
+
.slice(0, 3).join(' / ');
|
|
141
|
+
review.push(`文档集版本号不一致(${shown}),请人工核对`);
|
|
142
|
+
} else if (!versions.length) {
|
|
143
|
+
review.push('文档集未发现版本号(README/CHANGELOG 等应有版本记录)');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
hits,
|
|
148
|
+
review,
|
|
149
|
+
files,
|
|
150
|
+
versions,
|
|
151
|
+
items: DOCS_SCORE_ITEMS.map((it) => ({ ...it, hit: hits.includes(it.id) })),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 取版本号数组里的最大版本(x.y.z 数值比较)。 */
|
|
156
|
+
function maxVersion(list) {
|
|
157
|
+
const cmp = (a, b) => {
|
|
158
|
+
const pa = a.split('.').map(Number);
|
|
159
|
+
const pb = b.split('.').map(Number);
|
|
160
|
+
for (let i = 0; i < 3; i++) {
|
|
161
|
+
if (pa[i] !== pb[i]) return pa[i] - pb[i];
|
|
162
|
+
}
|
|
163
|
+
return 0;
|
|
164
|
+
};
|
|
165
|
+
return [...list].sort(cmp).pop();
|
|
166
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-git-push 评分总入口
|
|
3
|
+
*
|
|
4
|
+
* 统一 10 维度加权评分:问题(dimensions) → 分维度计数 → 加权总分。
|
|
5
|
+
* 每个编译函数声明 dimensions 绑定(支持一字段多维度)。
|
|
6
|
+
* 权重沿用 10 维度表(合计 100)。
|
|
7
|
+
*
|
|
8
|
+
* 审计豁免见 .auditignore(本文件是配置数据表,魔数即参数值,不适用魔数类规则)。
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_WEIGHTS = {
|
|
11
|
+
'可读性': 15, '可维护性': 15, '健壮性': 15, '安全性': 18, '性能': 10,
|
|
12
|
+
'测试覆盖': 10, '可观测性': 5, '可部署性': 5, '文档': 4, '开发者体验': 3,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 每维度对数衰减系数 k(约定:扣分平缓化,线性改对数 + 5 档调 k)。
|
|
17
|
+
* 单维度评分:max(0.1, base - k * ln(1 + errorCount)),base=10。
|
|
18
|
+
* k 按维度重要性分 5 档:安全类衰减最快(k=1.5),文档/开发者体验最宽松(k=0.8)。
|
|
19
|
+
* 比旧版(安全 1.8 / 其余 1.0-1.5)整体调低 0.2-0.3——同样错误数下扣分更温和、不会一下子扣完。
|
|
20
|
+
* 注:10 个维度权重(DEFAULT_WEIGHTS)由侧边栏可调,k 是固定的 5 档衰减系数,两者独立。
|
|
21
|
+
*/
|
|
22
|
+
export const DIMENSION_K = {
|
|
23
|
+
'可读性': 1.3, '可维护性': 1.3, '健壮性': 1.3, '安全性': 1.5, '性能': 1.1,
|
|
24
|
+
'测试覆盖': 1.0, '可观测性': 1.0, '可部署性': 1.0, '文档': 0.8, '开发者体验': 0.8,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const DIMENSION_ORDER = Object.keys(DEFAULT_WEIGHTS);
|
|
28
|
+
|
|
29
|
+
/** 等级分界(score ≥ 阈值取该级;A 最优,E 最差)。 */
|
|
30
|
+
export const LEVEL_A_MIN = 85;
|
|
31
|
+
export const LEVEL_B_MIN = 70;
|
|
32
|
+
export const LEVEL_C_MIN = 55;
|
|
33
|
+
export const LEVEL_D_MIN = 40;
|
|
34
|
+
/**
|
|
35
|
+
* 审计评分保留的小数位数(展示与 level 档位判定共用)。
|
|
36
|
+
* 改这一处即改全局精度:整数分丢失维度间差异(79 与 79.4 同显 79),
|
|
37
|
+
* 三位小数便于对照「同一仓库改一处代码后分数动了多少」。
|
|
38
|
+
*/
|
|
39
|
+
export const SCORE_DECIMALS = 3;
|
|
40
|
+
/** 舍入因子 = 10^SCORE_DECIMALS:先乘后除,实现「保留 N 位小数」。 */
|
|
41
|
+
const SCORE_ROUND_FACTOR = 10 ** SCORE_DECIMALS;
|
|
42
|
+
/** 单维度得分下限:错误再多也保留微弱区分度(不塌陷成 0)。 */
|
|
43
|
+
export const DIMENSION_SCORE_FLOOR = 0.1;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 按 10 维度汇总问题计数。
|
|
47
|
+
* @param {Array} findings 统一问题对象数组(含 dimensions 字段)
|
|
48
|
+
* @returns {{[dim]: number}} 分维度计数
|
|
49
|
+
*/
|
|
50
|
+
export function countByDimension(findings = []) {
|
|
51
|
+
const counts = Object.fromEntries(DIMENSION_ORDER.map((d) => [d, 0]));
|
|
52
|
+
// 公平性修复(大项目小问题线性累加不公平——小问题不会同时出现):
|
|
53
|
+
// warning/info 类**每文件每规则每维度最多计 1 次**——一个文件 20 处短变量 = 该文件 1 个
|
|
54
|
+
// 「命名可读性」问题(修一处即覆盖全消),不随同规则条数线性累加;不同规则各计
|
|
55
|
+
// (同文件 20 个不同规则问题 = 20),与大项目文件数解耦。
|
|
56
|
+
// blocker(安全/凭据等硬问题)保持每次计 2——同文件多个凭据 = 多个致命风险,不合并。
|
|
57
|
+
const seenRule = new Map(); // `${rule}|${dim}` → Set(file);仅 warning/info 参与去重
|
|
58
|
+
for (const f of findings) {
|
|
59
|
+
// scoreImpact: 0 的纯提醒(语义占位提示:npm audit 人工核查 / a11y /
|
|
60
|
+
// 路径穿越人工确认)不拉低质量分——它们不是「代码质量缺陷」,只是需要人工确认的
|
|
61
|
+
// 提示项;照常展示但不计入评分维度计数(severity=notice 且声明 0 影响的都不计)。
|
|
62
|
+
if (f.scoreImpact === 0) continue;
|
|
63
|
+
const sev = f.severity === 'blocker' ? 2 : 1;
|
|
64
|
+
const dedup = f.severity !== 'blocker';
|
|
65
|
+
const key = f.file || '__nofile__';
|
|
66
|
+
const rulePrefix = `${f.rule || '?'}|`;
|
|
67
|
+
for (const dim of f.dimensions || []) {
|
|
68
|
+
if (!(dim in counts)) continue;
|
|
69
|
+
if (dedup) {
|
|
70
|
+
const sKey = rulePrefix + dim;
|
|
71
|
+
if (!seenRule.has(sKey)) seenRule.set(sKey, new Set());
|
|
72
|
+
const fileSet = seenRule.get(sKey);
|
|
73
|
+
if (fileSet.has(key)) continue;
|
|
74
|
+
fileSet.add(key);
|
|
75
|
+
}
|
|
76
|
+
counts[dim] += sev;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return counts;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 计算 10 维度评分与总分(0-100)。
|
|
84
|
+
*
|
|
85
|
+
* 公式(用户 2026-09-11 权威 + 2026-09-12 防零分塌陷升级):
|
|
86
|
+
* - dims[d] = 维度得分(0-10 原始分):max(0.1, base - k_d * ln(1 + errorCount_d))
|
|
87
|
+
* base=10;k_d 见 DIMENSION_K(5 档:安全 1.5 / 可读·可维护·健壮 1.3 / 性能 1.1 / 测试·可观测·可部署 1.0 / 文档·开发者体验 0.8)
|
|
88
|
+
* errorCount 按 countByDimension:warning 计 1 / blocker 计 2
|
|
89
|
+
* - score = Σ(dims[d] × w[d]) / Σw × 10(满分区间的百分比映射 × 100,数学等价)
|
|
90
|
+
* 防零分塌陷:对数衰减替代线性扣分——10 个错误和 100 个错误有明显分差(不都归 0),floor=0.1 保留微弱区分度。
|
|
91
|
+
* @param {Array} findings
|
|
92
|
+
* @param {object} [weights] 覆盖权重(默认 DEFAULT_WEIGHTS)
|
|
93
|
+
* @param {object} [context] { files?: number, scope?: string } —— files=0(空目录/0 变动)时
|
|
94
|
+
* 不评分(score:null + emptyResult:true),防止「没扫到任何文件却直接满分」。
|
|
95
|
+
* @returns {{dims: object, score: number|null, level: 'A'|'B'|'C'|'D'|'E'|null, weights: object, emptyResult?: boolean, emptyReason?: string}}
|
|
96
|
+
*/
|
|
97
|
+
export function scoreQuality(findings = [], weights = {}, context = {}) {
|
|
98
|
+
const { files = null, docsScore = null } = context;
|
|
99
|
+
// 0 文件(空目录 / diff 0 变动)不评分——findings 为空时对数衰减天然给满分,
|
|
100
|
+
// 但「没扫到任何文件」不等于「代码健康」,返回 null 明确区分(调用方展示「未扫描到内容」)。
|
|
101
|
+
if (files !== null && Number(files) === 0) {
|
|
102
|
+
return {
|
|
103
|
+
dims: {}, counts: {}, score: null, level: null,
|
|
104
|
+
weights: { ...DEFAULT_WEIGHTS, ...weights },
|
|
105
|
+
emptyResult: true,
|
|
106
|
+
emptyReason: '未扫描到任何文件(空目录或 0 个变动文件),未评分',
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const weightMap = { ...DEFAULT_WEIGHTS, ...weights };
|
|
110
|
+
const counts = countByDimension(findings);
|
|
111
|
+
const dims = {};
|
|
112
|
+
// 文档维度加分制(需求讨论 DeepSeekHarness/文档加分制.md)——
|
|
113
|
+
// 文档得分 = min(10, Σ命中加分项分值),0 分起、上限 = 其他扣分制维度满分;
|
|
114
|
+
// 分值来自 docsScore.items[].score(配置可调,缺省每项 2.5);
|
|
115
|
+
// docsScore 缺省(旧调用不传 context.docsScore)→ 回退扣分制,兼容。
|
|
116
|
+
const docsTotal = Array.isArray(docsScore?.items)
|
|
117
|
+
? docsScore.items.filter((i) => i.hit).reduce((a, i) => a + (Number(i.score) || 0), 0)
|
|
118
|
+
: NaN;
|
|
119
|
+
for (const dim of DIMENSION_ORDER) {
|
|
120
|
+
// 对数衰减(防零分塌陷):score = max(DIMENSION_SCORE_FLOOR, 10 - k*ln(1+count))。dims 存原始分(不乘权重)。
|
|
121
|
+
if (dim === '文档' && Number.isFinite(docsTotal)) {
|
|
122
|
+
dims[dim] = Math.min(10, docsTotal);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const k = DIMENSION_K[dim] ?? 1.5;
|
|
126
|
+
dims[dim] = Math.max(DIMENSION_SCORE_FLOOR, 10 - k * Math.log(1 + counts[dim]));
|
|
127
|
+
}
|
|
128
|
+
// 最终得分 = Σ(维度得分 × 权重) / Σ权重 × 10(Σ(10×w)/Σw×10 = 100 满分;floor 0.1 时全扣 ≈ 1 分)
|
|
129
|
+
// 保留**三位小数**(不再 Math.round 取整)——整数分丢失维度间差异(79 与 79.4 同显 79);
|
|
130
|
+
// 侧边栏/工具展示更精细,也便于对照「同一仓库改一处代码后分数动了多少」;level 档位用同一分值判定。
|
|
131
|
+
const totalWeight = Object.values(weightMap).reduce((a, b) => a + b, 0);
|
|
132
|
+
const weightedSum = DIMENSION_ORDER.reduce((a, d) => a + dims[d] * (weightMap[d] || 0), 0);
|
|
133
|
+
// 百分比映射:Σ(dims×w)/Σw 是 0~10 的分值,×10 归一到 0~100;再按 SCORE_DECIMALS 保留小数
|
|
134
|
+
const percentScale = 10;
|
|
135
|
+
const score = totalWeight > 0
|
|
136
|
+
? Math.round(((weightedSum / totalWeight) * percentScale) * SCORE_ROUND_FACTOR) / SCORE_ROUND_FACTOR
|
|
137
|
+
: 0;
|
|
138
|
+
const level = score >= LEVEL_A_MIN ? 'A' : score >= LEVEL_B_MIN ? 'B' : score >= LEVEL_C_MIN ? 'C' : score >= LEVEL_D_MIN ? 'D' : 'E';
|
|
139
|
+
return { dims, counts, score, level, weights: weightMap };
|
|
140
|
+
}
|