dsh-git-push 2.12.3 → 2.13.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/lib/app/audit-api.js +1 -1
- package/lib/app/command-registry.js +1 -1
- package/lib/app/tool-call.js +1 -1
- package/lib/audit/history.js +1 -1
- package/lib/audit/orchestrate.js +59 -1
- package/lib/cli/commands-audit.mjs +1 -1
- package/lib/core/exec.js +109 -1
- package/lib/git/clone-history-mode.js +25 -4
- package/lib/score/index.js +63 -4
- package/lib/self/index.js +1 -1
- package/package.json +1 -1
package/lib/app/audit-api.js
CHANGED
|
@@ -117,7 +117,7 @@ export async function runAuditApi(repo, params = {}, cfg = {}) {
|
|
|
117
117
|
|
|
118
118
|
const { summarize } = await import('../audit/index.js');
|
|
119
119
|
const summary = summarize(auditResult.findings);
|
|
120
|
-
const quality = scoreQuality(auditResult.findings, params.weights || {}, { files: auditResult.files });
|
|
120
|
+
const quality = scoreQuality(auditResult.findings, params.weights || {}, { files: auditResult.files, dimFileCounts: auditResult.dimFileCounts });
|
|
121
121
|
// slot 归属映射(groupBy=slot 用):与 auditFiles 同源加载规则构建映射——
|
|
122
122
|
// 审计返回不带 compiled,这里按相同 slots/disabledSlots 重新编译一次。
|
|
123
123
|
let ruleSlotMap = new Map();
|
|
@@ -77,7 +77,7 @@ export const TOOL_REGISTRY = [
|
|
|
77
77
|
],
|
|
78
78
|
},
|
|
79
79
|
{
|
|
80
|
-
name: 'git_clone', cli: 'clone', description: '从 GitHub clone 仓库(Git Data API,不直连 github.com)。target=owner/repo 或完整 URL、dest=目标目录(缺省 workspaceRoot,已存在非空拒绝防覆盖)、branch=分支、history
|
|
80
|
+
name: 'git_clone', cli: 'clone', description: '从 GitHub clone 仓库(Git Data API,不直连 github.com)。target=owner/repo 或完整 URL、dest=目标目录(缺省 workspaceRoot,已存在非空拒绝防覆盖)、branch=分支、history=重建真实提交历史(可推回远端;默认开,传 false 退回整树快照)',
|
|
81
81
|
params: [
|
|
82
82
|
{ flag: 'target', type: 'string', positional: 0, required: true, desc: 'owner/repo 或完整 URL' },
|
|
83
83
|
{ flag: 'dest', type: 'string', desc: '目标目录(缺省 workspaceRoot)' },
|
package/lib/app/tool-call.js
CHANGED
|
@@ -514,7 +514,7 @@ async function runStandardAuditTool(args, env, cfg, log, weights) {
|
|
|
514
514
|
: await auditWithScope(repo, auditOpts);
|
|
515
515
|
const summary = summarize(auditResult.findings);
|
|
516
516
|
// 0 文件(空目录/diff 0 变动)不评分——scoreQuality 返回 emptyResult 防满分
|
|
517
|
-
const quality = scoreQuality(auditResult.findings, weights, { files: auditResult.files });
|
|
517
|
+
const quality = scoreQuality(auditResult.findings, weights, { files: auditResult.files, dimFileCounts: auditResult.dimFileCounts });
|
|
518
518
|
// 审计后直接输出豁免类型统计 + 聚合类型 + 拦截列表(blocker 详情不用查 API)
|
|
519
519
|
const exemptStats = exemptStatsOf(auditResult.findings);
|
|
520
520
|
const blocked = auditResult.findings
|
package/lib/audit/history.js
CHANGED
|
@@ -72,7 +72,7 @@ export async function auditHistoryCommit(repoPath, commit, opts = {}, weights =
|
|
|
72
72
|
writeFileSync(tarPath, arch.stdout);
|
|
73
73
|
execFileSync('tar', ['-x', '-C', tmpDir, '-f', tarPath], { stdio: 'ignore' });
|
|
74
74
|
const historyAudit = await auditFull(tmpDir, opts);
|
|
75
|
-
const quality = scoreQuality(historyAudit.findings || [], weights, { files: historyAudit.files, docsScore: historyAudit.docsScore });
|
|
75
|
+
const quality = scoreQuality(historyAudit.findings || [], weights, { files: historyAudit.files, docsScore: historyAudit.docsScore, dimFileCounts: historyAudit.dimFileCounts });
|
|
76
76
|
return {
|
|
77
77
|
summary: historyAudit.summary || {},
|
|
78
78
|
quality,
|
package/lib/audit/orchestrate.js
CHANGED
|
@@ -10,7 +10,7 @@ import { compileAllRules } from '../rule/registry.js';
|
|
|
10
10
|
import { auditFile } from './audit-file.js';
|
|
11
11
|
import { grammarForExt, parseText, makeSyncConfirmer } from '../ast/treesitter.js';
|
|
12
12
|
import { detectRepoJsYamlImport } from './file-context.js';
|
|
13
|
-
import { decorateTestExemptHint, makeFinding, summarize, normalizeExemptHints } from './finding.js';
|
|
13
|
+
import { decorateTestExemptHint, makeFinding, summarize, normalizeExemptHints, CODE_EXTS } from './finding.js';
|
|
14
14
|
import { pickRepoLevelAnchorFile, repoLevelSemanticRuleIds } from './repo-level.js';
|
|
15
15
|
import { checkDocsScore } from '../score/docs-score.js';
|
|
16
16
|
import { runAuditExt } from './ext-runner.js';
|
|
@@ -140,6 +140,61 @@ async function buildTreeConfirmer(relPath, text, grouped) {
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
const EMPTY_DIM_SET = new Set();
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 每维度「可作用文件数」——动态权重的分母。
|
|
147
|
+
*
|
|
148
|
+
* 为什么不用总文件数:总文件数里混着大量**该维度规则根本作用不到的数据文件**。
|
|
149
|
+
* 实测一个模型资源仓库:进入审计管道的 3086 个文本文件里 2701 个是清单 json,
|
|
150
|
+
* js/mjs 只有 319 个;可读性规则声明 exts=js/mjs/ts…,却按 3086 摊薄权重 ⇒
|
|
151
|
+
* 代码问题被数据资产数量稀释,json 越多的仓库分反而越好看(口径失真)。
|
|
152
|
+
*
|
|
153
|
+
* 口径与 filterRulesByExt 同一条 ext 判定(取 basename 最后一个点之后、小写)。
|
|
154
|
+
* 声明了 exts 的规则按 exts 匹配;**未声明 exts 的规则按代码文件计**(CODE_EXTS)——
|
|
155
|
+
* 这批规则绝大多数是 token/AST/semantic 类(复杂度、变量名、魔法数、eval、button 绑定…),
|
|
156
|
+
* 实际只会作用代码文件,不限扩展名只是「没声明」而非「要查数据文件」;
|
|
157
|
+
* 凭据扫描同理按代码文件(密钥藏在数据资产里的风险由逐文件 credential 判定覆盖,
|
|
158
|
+
* 不该反过来让 json 数量摊薄代码维度的分母)。
|
|
159
|
+
* include_paths/内容初筛不参与(那两层裁剪的是命中机会,不影响「能作用的文件面」)。
|
|
160
|
+
* 空规则集/编译失败返回 {} ⇒ 调用方回退总文件数(旧行为,向后兼容)。
|
|
161
|
+
*/
|
|
162
|
+
export function computeDimFileCounts(grouped, normFiles) {
|
|
163
|
+
const commonDims = new Set(); // 有不限 exts 的规则 → 作用全部文件
|
|
164
|
+
const extDims = new Map(); // ext → Set(dim)
|
|
165
|
+
const seen = new Set();
|
|
166
|
+
for (const rules of Object.values(grouped || {})) {
|
|
167
|
+
if (!Array.isArray(rules)) continue;
|
|
168
|
+
for (const r of rules) {
|
|
169
|
+
if (!r || !r.id || seen.has(r.id)) continue;
|
|
170
|
+
seen.add(r.id);
|
|
171
|
+
const dims = r.dimensions || [];
|
|
172
|
+
if (!dims.length) continue;
|
|
173
|
+
const exts = (Array.isArray(r.exts) && r.exts.length)
|
|
174
|
+
? r.exts : (Array.isArray(r.extra?.exts) ? r.extra.exts : null);
|
|
175
|
+
if (!exts || !exts.length) { for (const d of dims) commonDims.add(d); continue; }
|
|
176
|
+
for (const e of exts) {
|
|
177
|
+
const k = String(e).toLowerCase().replace(/^\./, '');
|
|
178
|
+
if (!extDims.has(k)) extDims.set(k, new Set());
|
|
179
|
+
const set = extDims.get(k);
|
|
180
|
+
for (const d of dims) set.add(d);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const counts = {};
|
|
185
|
+
for (const f of normFiles) {
|
|
186
|
+
const base = String(f.path || '').split(/[\\/]/).pop() || '';
|
|
187
|
+
const ext = base.includes('.') ? base.split('.').pop().toLowerCase() : '';
|
|
188
|
+
const set = extDims.get(ext) || EMPTY_DIM_SET;
|
|
189
|
+
// 未声明 exts 的规则按代码文件计(见函数头说明);并集避免同维度双重 +1
|
|
190
|
+
const union = CODE_EXTS.has(ext) && commonDims.size
|
|
191
|
+
? new Set([...commonDims, ...set])
|
|
192
|
+
: set;
|
|
193
|
+
for (const d of union) counts[d] = (counts[d] || 0) + 1;
|
|
194
|
+
}
|
|
195
|
+
return counts;
|
|
196
|
+
}
|
|
197
|
+
|
|
143
198
|
async function auditFiles(repoPath, files, opts = {}) {
|
|
144
199
|
// 归一化:full 收集项用 path,changed 用 rel → 统一取 path
|
|
145
200
|
const normFiles = files.map((f) => ({ path: f.path ?? f.rel, full: f.full, status: f.status }));
|
|
@@ -196,6 +251,8 @@ async function auditFiles(repoPath, files, opts = {}) {
|
|
|
196
251
|
&& !/^testing\//i.test(String(r?.id || '') || ''));
|
|
197
252
|
}
|
|
198
253
|
}
|
|
254
|
+
// 动态权重分母(维度可作用文件数,见 computeDimFileCounts)——在所有规则过滤完成后统计
|
|
255
|
+
const dimFileCounts = computeDimFileCounts(grouped, normFiles);
|
|
199
256
|
const findings = [];
|
|
200
257
|
if (opts.truncated) {
|
|
201
258
|
findings.push(makeFinding({
|
|
@@ -308,6 +365,7 @@ async function auditFiles(repoPath, files, opts = {}) {
|
|
|
308
365
|
return {
|
|
309
366
|
ok: true, scope: opts.scope || 'full', repo: repoPath, findings, summary: summarize(findings), slotStats, files: normFiles.length,
|
|
310
367
|
docsScore,
|
|
368
|
+
dimFileCounts,
|
|
311
369
|
analysisCoverage,
|
|
312
370
|
// 层级聚合 YAML(summary → 拦截级别 → 目录 → 文件 → 规则明细)
|
|
313
371
|
yaml: findingsToYaml(findings),
|
|
@@ -101,7 +101,7 @@ async function runStandardAudit(root, flags) {
|
|
|
101
101
|
opts.ownership = probeRepoOwnership(root); // 与插件工具同参:隐私规则只对自有仓库生效
|
|
102
102
|
const weights = pluginEqualWeights(cfg, flags);
|
|
103
103
|
const auditResult = full ? await auditFull(root, opts) : await auditWithScope(root, opts);
|
|
104
|
-
const quality = scoreQuality(auditResult.findings, weights, { files: auditResult.files });
|
|
104
|
+
const quality = scoreQuality(auditResult.findings, weights, { files: auditResult.files, dimFileCounts: auditResult.dimFileCounts });
|
|
105
105
|
if (flags.json) {
|
|
106
106
|
console.log(JSON.stringify({ ok: true, repo: root, scope: auditResult.scope, summary: auditResult.summary, quality, findings: auditResult.findings, files: auditResult.files, analysisCoverage: auditResult.analysisCoverage || null, yaml: auditResult.yaml }, null, 2));
|
|
107
107
|
return;
|
package/lib/core/exec.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* 所有 git 调用都经过此模块,便于统一超时与错误规整,避免各调用点各写一份。
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { execFile, execFileSync, spawnSync } from 'node:child_process';
|
|
9
|
+
import { execFile, execFileSync, spawn, spawnSync } from 'node:child_process';
|
|
10
10
|
import { existsSync, statSync } from 'node:fs';
|
|
11
11
|
import { delimiter as pathDelimiter, join } from 'node:path';
|
|
12
12
|
|
|
@@ -162,3 +162,111 @@ export function runGitAsync(args, { cwd = '', timeoutMs = 120_000, env = {}, noM
|
|
|
162
162
|
});
|
|
163
163
|
});
|
|
164
164
|
}
|
|
165
|
+
|
|
166
|
+
/* ─────────────────── 流式执行:按「无进展」判停摆 ─────────────────── */
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 流式 git 执行:**只判「有没有进展」,不设固定总时长**。
|
|
170
|
+
*
|
|
171
|
+
* 为什么需要:`runGit` 是 execFileSync + 死超时(默认 120s)。对大仓库的 `git fetch`
|
|
172
|
+
* 来说这必然半途而废 —— 实测 6.9GB / 7374 文件的仓库,fetch 要跑十几分钟以上,
|
|
173
|
+
* 但它其实一直在正常下载(`--progress` 的百分比行持续刷新)。真正该防的不是"慢",
|
|
174
|
+
* 是"卡住不动":所以这里改成**只要有输出就把看门狗往后推**,
|
|
175
|
+
* 连续 `stallLimit` 完全没有输出才判定停摆并结束进程。
|
|
176
|
+
*
|
|
177
|
+
* 与 runGit 的关系:语义一致(同前置参数、同镜像注入、同 DSH_GIT_ENFORCE_PASS 门禁),
|
|
178
|
+
* 区别只在于**超时判据**与"必须流式读 stderr 才能看见进度"。
|
|
179
|
+
*
|
|
180
|
+
* @param {string[]} args git 参数(自动前置 GIT_GLOBAL_ARGS)
|
|
181
|
+
* @param {{cwd?:string, env?:object, noMirror?:boolean, stallMs?:number,
|
|
182
|
+
* onProgress?:(line:string)=>void}} [o]
|
|
183
|
+
* stallMs:连续多少毫秒无任何输出算停摆(未传则读 DSH_GIT_FETCH_STALL_MS,再缺省 300000 = 5 分钟)
|
|
184
|
+
* @returns {Promise<{ok:boolean, stdout:string, stderr:string, stalled:boolean, ms:number}>}
|
|
185
|
+
* `stalled=true` 表示是按"停摆"结束的(不是 git 自己报错)——
|
|
186
|
+
* 调用方据此提示"可重试续传",别把它当成仓库/网络错误。
|
|
187
|
+
*/
|
|
188
|
+
export function runGitStream(args, { cwd = '', env = {}, noMirror = false, stallMs, onProgress } = {}) {
|
|
189
|
+
const base = GIT_GLOBAL_ARGS;
|
|
190
|
+
const bin = resolveGitBin();
|
|
191
|
+
if (!bin) return Promise.resolve({ ok: false, stdout: '', stderr: GIT_MISSING_MSG, stalled: false, ms: 0 });
|
|
192
|
+
const mirrorEnv = {};
|
|
193
|
+
if (!noMirror && GIT_READ_VERBS.has(String(args[0] || ''))) mirrorGitConfigEnv(mirrorEnv, { mode: 'read' });
|
|
194
|
+
// 停摆阈值优先级:显式传参 > 环境变量 > 默认 5 分钟。
|
|
195
|
+
// 默认值依据「fetch 带 --progress、正常下载会持续刷行」估计;若现场遇到长时间静默
|
|
196
|
+
// 但仍在工作的阶段(大 pack 解压/校验无进度行),可用 DSH_GIT_FETCH_STALL_MS 放宽,
|
|
197
|
+
// 不必改代码重发版——这类阈值只能在真实大仓库上才调得准。
|
|
198
|
+
const groupKill = process.platform !== 'win32';
|
|
199
|
+
const envStall = Number(process.env.DSH_GIT_FETCH_STALL_MS);
|
|
200
|
+
const stallLimit = Number(stallMs) > 0 ? Number(stallMs) : (envStall > 0 ? envStall : 300_000);
|
|
201
|
+
const startedAt = Date.now();
|
|
202
|
+
return new Promise((resolve) => {
|
|
203
|
+
let child;
|
|
204
|
+
try {
|
|
205
|
+
child = spawn(bin, cwd ? base.concat(['-C', cwd], args) : base.concat(args), {
|
|
206
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
207
|
+
// detached:让 git 自成进程组。git fetch 会再派生 git-remote-https 这类传输子进程,
|
|
208
|
+
// 只结束直接子进程时孙进程可能仍占着连接、把下载拖在后台;按进程组结束才真停下。
|
|
209
|
+
// Windows 无进程组语义,退回单进程结束。
|
|
210
|
+
detached: groupKill,
|
|
211
|
+
env: { ...process.env, ...mirrorEnv, DSH_GIT_ENFORCE_PASS: '1', ...env },
|
|
212
|
+
});
|
|
213
|
+
} catch (e) {
|
|
214
|
+
resolve({ ok: false, stdout: '', stderr: String(e?.message || e), stalled: false, ms: 0 });
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
let stdoutBuf = '';
|
|
218
|
+
let stderrTail = '';
|
|
219
|
+
let lastActivity = Date.now();
|
|
220
|
+
let stalled = false;
|
|
221
|
+
let settled = false;
|
|
222
|
+
const feed = (chunk, isErr) => {
|
|
223
|
+
lastActivity = Date.now();
|
|
224
|
+
const text = String(chunk);
|
|
225
|
+
if (!isErr) { stdoutBuf = (stdoutBuf + text).slice(-1024 * 1024); return; }
|
|
226
|
+
// 进度行用 \r 覆盖刷新:按换行/回车切开逐段回调,末段落进 tail 供失败时示错
|
|
227
|
+
stderrTail = (stderrTail + text).slice(-8192);
|
|
228
|
+
if (typeof onProgress === 'function') {
|
|
229
|
+
for (const line of text.split(/[\r\n]+/)) {
|
|
230
|
+
const t = line.trim();
|
|
231
|
+
if (!t) continue;
|
|
232
|
+
// 回调由调用方提供(进度推送/日志),它抛错会变成 data 事件的未捕获异常
|
|
233
|
+
// ⇒ 常驻宿主进程被一个进度回调带走。进度只是观测信息,绝不能因它中断传输。
|
|
234
|
+
try { onProgress(t); } catch { /* 进度回调失败不影响执行 */ }
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
child.stdout && child.stdout.on('data', (c) => feed(c, false));
|
|
239
|
+
child.stderr && child.stderr.on('data', (c) => feed(c, true));
|
|
240
|
+
// 看门狗只量"距上次输出多久";没有总时长上限(慢≠异常,卡住才是)
|
|
241
|
+
const tickMs = Math.min(5000, Math.max(1000, Math.floor(stallLimit / 4)));
|
|
242
|
+
// 结束整个进程组(含 git 派生的传输子进程);组的创建见 spawn 的 detached
|
|
243
|
+
const signal = (sig) => {
|
|
244
|
+
try {
|
|
245
|
+
if (groupKill && child.pid) process.kill(-child.pid, sig);
|
|
246
|
+
else child.kill(sig);
|
|
247
|
+
} catch {
|
|
248
|
+
try { child.kill(sig); } catch { /* 已退出 */ }
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
const watchdog = setInterval(() => {
|
|
252
|
+
if (Date.now() - lastActivity <= stallLimit) return;
|
|
253
|
+
stalled = true;
|
|
254
|
+
signal('SIGTERM');
|
|
255
|
+
// 宽限 3 秒仍未退出则强杀,避免残留子进程挂住调用方的 job
|
|
256
|
+
const killer = setTimeout(() => { signal('SIGKILL'); }, 3000);
|
|
257
|
+
if (killer && typeof killer.unref === 'function') killer.unref();
|
|
258
|
+
}, tickMs);
|
|
259
|
+
const finish = (code, errMsg) => {
|
|
260
|
+
if (settled) return;
|
|
261
|
+
settled = true;
|
|
262
|
+
clearInterval(watchdog);
|
|
263
|
+
const ok = !stalled && code === 0;
|
|
264
|
+
const errText = stalled
|
|
265
|
+
? `连续 ${Math.round(stallLimit / 1000)}s 无任何进展,已结束(可重试续传,已下载部分不丢)`
|
|
266
|
+
: String(errMsg || stderrTail.trim() || `git 退出码 ${code}`);
|
|
267
|
+
resolve({ ok, stdout: ok ? stdoutBuf.trim() : '', stderr: ok ? '' : errText, stalled, ms: Date.now() - startedAt });
|
|
268
|
+
};
|
|
269
|
+
child.on('error', (e) => finish(null, String(e?.message || e)));
|
|
270
|
+
child.on('close', (code) => finish(code, ''));
|
|
271
|
+
});
|
|
272
|
+
}
|
|
@@ -15,7 +15,7 @@ import { githubFetch } from './api.js';
|
|
|
15
15
|
import { fetchCommitChain } from './clone-history.js';
|
|
16
16
|
import { replayHistory } from './clone-replay.js';
|
|
17
17
|
import { createReplayIo } from './clone-replay-io.js';
|
|
18
|
-
import { runGit } from './exec.js';
|
|
18
|
+
import { runGit, runGitStream } from './exec.js';
|
|
19
19
|
import { initRepoCompat } from './init-compat.js';
|
|
20
20
|
import { resolveOwnerVisibility, accessPlan, sshGitUrl } from './visibility.js';
|
|
21
21
|
import { resolveSshKeys } from './credentials.js';
|
|
@@ -159,9 +159,30 @@ export async function cloneWithGitFetch({
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
// 显式 refspec:同时写入 refs/remotes/origin/<branch>,后续可据此建本地分支并设上游
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
// fetch 走**流式执行 + 停摆判据**(runGitStream),不用 runGit 的死超时:
|
|
163
|
+
// 大仓库 fetch 要跑十几分钟且一直在正常下载(--progress 行持续刷新),
|
|
164
|
+
// 固定 120s 必然半途而废;这里只在"连续无任何输出"时才结束(默认 5 分钟,
|
|
165
|
+
// 测试可用 deps.fetchStallMs 覆盖)。已下载的对象都在 .git 里,重试即续传。
|
|
166
|
+
const streamGit = deps.runGitStream || runGitStream;
|
|
167
|
+
const fetchArgs = ['fetch', '--progress', '--no-tags', ...(n ? [`--depth=${n}`] : []), 'origin', `+refs/heads/${branch}:refs/remotes/origin/${branch}`];
|
|
168
|
+
let lastProgress = '';
|
|
169
|
+
const fetched = await streamGit(fetchArgs, {
|
|
170
|
+
cwd: repoPath,
|
|
171
|
+
env: sshEnv,
|
|
172
|
+
noMirror: !plan.useMirror,
|
|
173
|
+
stallMs: Number(deps.fetchStallMs) > 0 ? Number(deps.fetchStallMs) : 300_000,
|
|
174
|
+
onProgress: (line) => { const t = String(line).trim(); if (t) lastProgress = t.slice(0, 160); },
|
|
175
|
+
});
|
|
176
|
+
steps.push({ cmd: fetchArgs.join(' '), ok: !!fetched?.ok, ms: fetched?.ms, stalled: !!fetched?.stalled, ...(fetched?.ok && lastProgress ? { lastProgress } : {}) });
|
|
177
|
+
if (!fetched?.ok) {
|
|
178
|
+
// 停摆与真失败必须区分:停摆是"没进展、可续传",不是仓库/网络错误。
|
|
179
|
+
// "可重试续传"这句在消费方兜底——上游 stderr 已带则去重,未带也保证提示到位,
|
|
180
|
+
// 避免换了流式实现后调用方看不到"已下载部分不丢"这个关键信息。
|
|
181
|
+
const detail = String(fetched?.stderr || '').trim() || '连续无任何进展';
|
|
182
|
+
return fail(fetched?.stalled
|
|
183
|
+
? `git fetch 停摆:${detail}${/续传/.test(detail) ? '' : '(可重试续传,已下载部分不丢)'}`
|
|
184
|
+
: `git fetch 失败:${detail === '连续无任何进展' ? '未知' : detail}`);
|
|
185
|
+
}
|
|
165
186
|
|
|
166
187
|
const rev = git(['rev-parse', `refs/remotes/origin/${branch}`], gitOpts);
|
|
167
188
|
const sha = String(rev?.stdout || '').trim();
|
package/lib/score/index.js
CHANGED
|
@@ -79,6 +79,30 @@ export function countByDimension(findings = []) {
|
|
|
79
79
|
return counts;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* 每维度**命中文件数**(file 去重)——动态权重的分子。
|
|
84
|
+
*
|
|
85
|
+
* 与 countByDimension 同口径:`scoreImpact === 0` 的纯提示不计(它们本就不拉低分数);
|
|
86
|
+
* blocker 与 warning 在这里都只算「该文件命中过这个维度」一次——动态权重衡量的是
|
|
87
|
+
* **问题覆盖面**,不是问题条数(条数已由 countByDimension 的对数衰减承担)。
|
|
88
|
+
* 无文件的仓库级 finding(如 folder/locale)记为 1 个「文件」,避免整维度被算成 0 命中。
|
|
89
|
+
*/
|
|
90
|
+
export function countHitFilesByDimension(findings = []) {
|
|
91
|
+
const sets = new Map();
|
|
92
|
+
for (const f of findings) {
|
|
93
|
+
if (f.scoreImpact === 0) continue;
|
|
94
|
+
const fileKey = f.file || '__repo__';
|
|
95
|
+
for (const dim of f.dimensions || []) {
|
|
96
|
+
if (!(dim in DEFAULT_WEIGHTS)) continue;
|
|
97
|
+
if (!sets.has(dim)) sets.set(dim, new Set());
|
|
98
|
+
sets.get(dim).add(fileKey);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const out = Object.fromEntries(DIMENSION_ORDER.map((d) => [d, 0]));
|
|
102
|
+
for (const [dim, set] of sets) out[dim] = set.size;
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
82
106
|
/**
|
|
83
107
|
* 计算 10 维度评分与总分(0-100)。
|
|
84
108
|
*
|
|
@@ -95,7 +119,7 @@ export function countByDimension(findings = []) {
|
|
|
95
119
|
* @returns {{dims: object, score: number|null, level: 'A'|'B'|'C'|'D'|'E'|null, weights: object, emptyResult?: boolean, emptyReason?: string}}
|
|
96
120
|
*/
|
|
97
121
|
export function scoreQuality(findings = [], weights = {}, context = {}) {
|
|
98
|
-
const { files = null, docsScore = null } = context;
|
|
122
|
+
const { files = null, docsScore = null, dimFileCounts = null } = context;
|
|
99
123
|
// 0 文件(空目录 / diff 0 变动)不评分——findings 为空时对数衰减天然给满分,
|
|
100
124
|
// 但「没扫到任何文件」不等于「代码健康」,返回 null 明确区分(调用方展示「未扫描到内容」)。
|
|
101
125
|
if (files !== null && Number(files) === 0) {
|
|
@@ -128,13 +152,48 @@ export function scoreQuality(findings = [], weights = {}, context = {}) {
|
|
|
128
152
|
// 最终得分 = Σ(维度得分 × 权重) / Σ权重 × 10(Σ(10×w)/Σw×10 = 100 满分;floor 0.1 时全扣 ≈ 1 分)
|
|
129
153
|
// 保留**三位小数**(不再 Math.round 取整)——整数分丢失维度间差异(79 与 79.4 同显 79);
|
|
130
154
|
// 侧边栏/工具展示更精细,也便于对照「同一仓库改一处代码后分数动了多少」;level 档位用同一分值判定。
|
|
131
|
-
|
|
132
|
-
|
|
155
|
+
// ── 动态权重(2026-10-30):实际权重 = 设定权重 × 动态权重,动态权重 = 命中文件数 / 总文件数 ──
|
|
156
|
+
// 语义:某维度只在少数文件出问题时,它的实际权重按「问题覆盖面」缩小(扣分更轻);
|
|
157
|
+
// 问题铺满全仓时动态权重=1,退化为纯静态权重(与历史分数一致)。
|
|
158
|
+
// 三条口径(已与需求方对齐):
|
|
159
|
+
// ① 分母用 Σ实际权重(而非固定 100)——否则「全绿」会因有效权重全为 0 而算出 0 分;
|
|
160
|
+
// ② **无命中的维度不缩放**(用设定权重):该维度没问题,必须保持满分贡献,
|
|
161
|
+
// 否则满分会被自己的 0 权重拖成 0 分;
|
|
162
|
+
// ③ 文档维度也参与:它的「命中文件数」取 docsScore.files(仓库里实际存在的文档文件),
|
|
163
|
+
// 因为它本身是加分制、没有「命中问题的文件」概念;无文档文件信息时回退不缩放。
|
|
164
|
+
// 总文件数缺失(files=null,旧调用方)⇒ 全部不缩放,行为与改造前逐位等价。
|
|
165
|
+
// 分母优先用**该维度可作用文件数**(dimFileCounts,由审计编排层按规则 exts 声明统计):
|
|
166
|
+
// 数据文件占比高的仓库里,用总文件数会把代码问题被数据资产数量摊薄(实测某资源仓库
|
|
167
|
+
// 3086 个文本文件中 2701 个是清单 json,可读性权重被稀释到 1/10)。
|
|
168
|
+
// 缺 dimFileCounts(旧调用方/独立调用)→ 回退总文件数,行为与上一版一致。
|
|
169
|
+
const denomOf = (dim) => {
|
|
170
|
+
const perDim = Number(dimFileCounts?.[dim]) > 0 ? Number(dimFileCounts[dim]) : 0;
|
|
171
|
+
return perDim || (Number(files) > 0 ? Number(files) : 0);
|
|
172
|
+
};
|
|
173
|
+
const anyDenom = DIMENSION_ORDER.some((d) => denomOf(d) > 0);
|
|
174
|
+
const hitFiles = anyDenom ? countHitFilesByDimension(findings) : null;
|
|
175
|
+
const dynamicRatio = (dim) => {
|
|
176
|
+
const total = denomOf(dim);
|
|
177
|
+
if (!total) return null; // 该维度既无分母也无总文件数 → 不启用动态权重
|
|
178
|
+
if (dim === '文档') {
|
|
179
|
+
const docFiles = Array.isArray(docsScore?.files) ? docsScore.files.length : 0;
|
|
180
|
+
return docFiles > 0 ? Math.min(1, docFiles / total) : null;
|
|
181
|
+
}
|
|
182
|
+
const hf = hitFiles[dim] || 0;
|
|
183
|
+
return hf > 0 ? Math.min(1, hf / total) : null; // 无命中 → 不缩放
|
|
184
|
+
};
|
|
185
|
+
const dynamicWeights = {};
|
|
186
|
+
for (const d of DIMENSION_ORDER) {
|
|
187
|
+
const ratio = dynamicRatio(d);
|
|
188
|
+
dynamicWeights[d] = (weightMap[d] || 0) * (ratio === null ? 1 : ratio);
|
|
189
|
+
}
|
|
190
|
+
const totalWeight = Object.values(dynamicWeights).reduce((a, b) => a + b, 0);
|
|
191
|
+
const weightedSum = DIMENSION_ORDER.reduce((a, d) => a + dims[d] * (dynamicWeights[d] || 0), 0);
|
|
133
192
|
// 百分比映射:Σ(dims×w)/Σw 是 0~10 的分值,×10 归一到 0~100;再按 SCORE_DECIMALS 保留小数
|
|
134
193
|
const percentScale = 10;
|
|
135
194
|
const score = totalWeight > 0
|
|
136
195
|
? Math.round(((weightedSum / totalWeight) * percentScale) * SCORE_ROUND_FACTOR) / SCORE_ROUND_FACTOR
|
|
137
196
|
: 0;
|
|
138
197
|
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 };
|
|
198
|
+
return { dims, counts, score, level, weights: weightMap, dynamicWeights };
|
|
140
199
|
}
|
package/lib/self/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
export const VERSION = '2.
|
|
12
|
+
export const VERSION = '2.13.2'; // 2.13.2:动态权重分母改按「该维度可作用文件数」——总文件数里混着数据资产(实测 ModelStage 管道内 374 文件中清单 json 占比极高),可读性规则声明 exts=js/mjs/ts… 却按总文件数摊薄 ⇒ 代码问题被数据数量稀释。新增 computeDimFileCounts(编排层按规则 exts 声明统计每维度分母,与 filterRulesByExt 同一取 ext 口径;未声明 exts 的规则按代码文件 CODE_EXTS 计——那批规则实际只作用代码);scoreQuality 优先用维度分母、缺失回退总文件数(向后兼容)。修复过程自证两处:未声明规则误按全文件计导致分母虚高、同一文件同维度双重 +1 导致 741>374——均有测试钉住。历史:2.13.1:大仓库 fetch 按无进展判停摆。2.13.0:动态权重。
|
|
13
13
|
// 实测后果:该目录有 package.json ⇒ npm 去打包整棵 DSH 树、输出 2.1MB > spawnSync 默认 maxBuffer 1MB ⇒ ENOBUFS(npm_publish 的体检与真发全部失败);
|
|
14
14
|
// 该目录没有 package.json 时报 ENOENT。现 publish 的两个调用点显式传 cwd=目标包目录,并给 runNpm 加 16MB maxBuffer。
|
|
15
15
|
// 2.8.3:文案修正——「零改动 / 无新提交」明确为**正常成功路径**(注入用法、工具描述、README、skill 同批改),并修 job 输出自相矛盾(cleanSkipCommit 路径补 committed:false,不再显示「已提交,未推送」)
|