kld-sdd 2.6.21 → 2.7.3
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/bin/kld-sdd-init.js +3 -9
- package/lib/init.js +20 -57
- package/package.json +2 -2
- package/skywalk-sdd/index.cjs +13 -0
- package/skywalk-sdd/lib/git-identity.cjs +270 -0
- package/skywalk-sdd/lib/usage-contract.cjs +10 -79
- package/skywalk-sdd/lib/usage-reporter.cjs +210 -104
- package/skywalk-sdd/lib/user-config.cjs +21 -46
- package/skywalk-sdd/ontology/artifact-parser.cjs +1 -2
- package/templates/git-hooks/commit-msg +58 -15
- package/templates/git-hooks/hooks.config +19 -0
- package/templates/git-hooks/pre-commit +45 -11
- package/templates/git-hooks/pre-push +53 -11
- package/templates/git-hooks/pre-push-consistency-check.cjs +112 -2
- package/lib/device-auth-cli.js +0 -130
- package/lib/device-auth.js +0 -345
- package/lib/init-account-binding.js +0 -108
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
// kld-T04 — 使用事件 reporter(
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// kld-T04 — 使用事件 reporter(git 身份缓存化韧性版)
|
|
2
|
+
// 职责:git 身份取自 ~/.kld-sdd/git-identity.json 持久缓存(git 仅由 git-identity.cjs
|
|
3
|
+
// 后台刷新读取,慢 git 环境前台零调用);无身份当前事件入 pending 队列不跳过,
|
|
4
|
+
// 身份就绪后 flush 时注入身份补报;
|
|
5
|
+
// server 四级解析(config > kb-state.api > KLD_SDD_SERVER > 内置默认);
|
|
6
|
+
// 单一 3s deadline;pending JSONL 原子追加;100 条上限;1MB 淘汰;
|
|
7
|
+
// HTTP 状态分类(confirmed/retry/drop,401 归入 drop 不清任何本地状态);
|
|
4
8
|
// 并发安全(appendFileSync O_APPEND + 事件 id 差集提交);不改原业务退出码。
|
|
5
9
|
'use strict';
|
|
6
10
|
|
|
@@ -13,11 +17,12 @@ const {
|
|
|
13
17
|
classifyUsageSubmitResponse,
|
|
14
18
|
postJson,
|
|
15
19
|
mapCommandToUsageSkill,
|
|
20
|
+
DEFAULT_SERVER_URL,
|
|
16
21
|
} = require('./usage-contract.cjs');
|
|
17
22
|
const {
|
|
18
23
|
readUserConfig,
|
|
19
|
-
clearUserToken,
|
|
20
24
|
} = require('./user-config.cjs');
|
|
25
|
+
const gitIdentity = require('./git-identity.cjs');
|
|
21
26
|
|
|
22
27
|
const PENDING_FILE_NAME = 'pending-usage.jsonl';
|
|
23
28
|
const PENDING_MAX_BYTES = 1024 * 1024; // 1 MB
|
|
@@ -34,6 +39,73 @@ function pendingPathFor(homeDir) {
|
|
|
34
39
|
return path.join(home, '.kld-sdd', PENDING_FILE_NAME);
|
|
35
40
|
}
|
|
36
41
|
|
|
42
|
+
// git 身份进程内缓存:仅缓存成功读取;失败不缓存,下次重试
|
|
43
|
+
let gitIdentityCache = null;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 读取本机 git 身份。
|
|
47
|
+
* - 来源:~/.kld-sdd/git-identity.json 持久缓存(git-identity.cjs 维护,
|
|
48
|
+
* git 仅由 detached 后台刷新进程读取——慢 git 环境(7-34s 启动)前台零调用)
|
|
49
|
+
* - 进程内缓存:首次成功后复用;失败不缓存
|
|
50
|
+
* - 无缓存 / 缓存缺字段 / tombstone → 返回 null(调用方将当前事件入队待补报,
|
|
51
|
+
* 并已由 ensureIdentity 触发一次后台刷新)
|
|
52
|
+
*
|
|
53
|
+
* @param {string} [homeDir] — 测试注入 home;缺省走 KLD_SDD_HOME/os.homedir
|
|
54
|
+
* @returns {{userName:string, email:string}|null}
|
|
55
|
+
*/
|
|
56
|
+
function readGitIdentity(homeDir) {
|
|
57
|
+
if (gitIdentityCache) return gitIdentityCache;
|
|
58
|
+
const identity = gitIdentity.ensureIdentity({ homeDir });
|
|
59
|
+
if (!identity) return null;
|
|
60
|
+
gitIdentityCache = identity;
|
|
61
|
+
return gitIdentityCache;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 测试专用:重置进程内 git 身份缓存。
|
|
66
|
+
*/
|
|
67
|
+
function _resetGitIdentityCache() {
|
|
68
|
+
gitIdentityCache = null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 四级解析上报地址:config.server → 项目 kb-state.json api → env KLD_SDD_SERVER → 内置默认。
|
|
73
|
+
* 每级均经 normalizeServerUrl;结果永远非空(默认地址兜底)。
|
|
74
|
+
*
|
|
75
|
+
* @param {{homeDir?:string, projectRoot?:string, env?:object}} [options]
|
|
76
|
+
* @returns {string}
|
|
77
|
+
*/
|
|
78
|
+
function resolveReportServer(options = {}) {
|
|
79
|
+
const env = options.env || process.env;
|
|
80
|
+
const projectRoot = options.projectRoot || process.cwd();
|
|
81
|
+
// 1. 用户级 config.server
|
|
82
|
+
const config = readUserConfig(options.homeDir);
|
|
83
|
+
if (config && typeof config.server === 'string') {
|
|
84
|
+
const normalized = normalizeServerUrl(config.server);
|
|
85
|
+
if (normalized) return normalized;
|
|
86
|
+
}
|
|
87
|
+
// 2. 项目级 kb-state.json 的 api
|
|
88
|
+
try {
|
|
89
|
+
const kbStatePath = path.join(projectRoot, 'kb-state.json');
|
|
90
|
+
if (fs.existsSync(kbStatePath)) {
|
|
91
|
+
const kbState = JSON.parse(fs.readFileSync(kbStatePath, 'utf8'));
|
|
92
|
+
if (kbState && typeof kbState.api === 'string') {
|
|
93
|
+
const normalized = normalizeServerUrl(kbState.api);
|
|
94
|
+
if (normalized) return normalized;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
// kb-state 读取失败时静默,继续下一级
|
|
99
|
+
}
|
|
100
|
+
// 3. env KLD_SDD_SERVER
|
|
101
|
+
if (env && typeof env.KLD_SDD_SERVER === 'string') {
|
|
102
|
+
const normalized = normalizeServerUrl(env.KLD_SDD_SERVER);
|
|
103
|
+
if (normalized) return normalized;
|
|
104
|
+
}
|
|
105
|
+
// 4. 内置默认地址兜底
|
|
106
|
+
return normalizeServerUrl(DEFAULT_SERVER_URL);
|
|
107
|
+
}
|
|
108
|
+
|
|
37
109
|
/**
|
|
38
110
|
* 生成事件唯一 id(基于时间戳 + pid + 随机字节)。
|
|
39
111
|
* 用于 flush 提交时按 id 差集删除已确认行,避免 read-modify-write 覆盖并发 append。
|
|
@@ -174,21 +246,112 @@ function appendPending(event, filePath) {
|
|
|
174
246
|
}
|
|
175
247
|
|
|
176
248
|
/**
|
|
177
|
-
*
|
|
178
|
-
* @returns {Promise<'confirmed'|'
|
|
249
|
+
* 提交单条事件;发送前注入 git 身份;返回分类。
|
|
250
|
+
* @returns {Promise<'confirmed'|'retry'|'drop'>}
|
|
179
251
|
*/
|
|
180
|
-
async function submitOne(server,
|
|
181
|
-
// 提交时剔除内部 id
|
|
182
|
-
|
|
252
|
+
async function submitOne(server, identity, event, timeoutMs) {
|
|
253
|
+
// 提交时剔除内部 id(不上传给服务端),统一注入当前 git 身份
|
|
254
|
+
// (兼容 pending 队列里无身份字段的遗留事件)
|
|
255
|
+
const outgoing = Object.assign({}, event, {
|
|
256
|
+
userName: identity.userName,
|
|
257
|
+
email: identity.email,
|
|
258
|
+
});
|
|
183
259
|
delete outgoing.id;
|
|
184
260
|
const result = await postJson(
|
|
185
261
|
`${server}/api/v1/usage-events`,
|
|
186
262
|
outgoing,
|
|
187
|
-
{
|
|
263
|
+
{ timeoutMs },
|
|
188
264
|
);
|
|
189
265
|
return classifyUsageSubmitResponse(result.statusCode);
|
|
190
266
|
}
|
|
191
267
|
|
|
268
|
+
/**
|
|
269
|
+
* 共享冲刷执行体:读 pending → 逐条提交 → 按 id 差集原子提交队列变化。
|
|
270
|
+
* reportUsageOnce(冲刷后发当前事件)与 flushPendingUsage(只冲刷)共用。
|
|
271
|
+
*
|
|
272
|
+
* @returns {Promise<{submitted:number, confirmedIds:Set, droppedIds:Set}>}
|
|
273
|
+
*/
|
|
274
|
+
async function flushPendingQueue(server, identity, pendingFile, deadlineStart) {
|
|
275
|
+
const deadline = deadlineStart || (Date.now() + SHARED_DEADLINE_MS);
|
|
276
|
+
const confirmedIds = new Set(); // 已成功的事件 id
|
|
277
|
+
const droppedIds = new Set(); // 4xx 永久失败的事件 id
|
|
278
|
+
let submitted = 0;
|
|
279
|
+
|
|
280
|
+
const { events: pendingEvents } = readPending(pendingFile);
|
|
281
|
+
let flushed = 0;
|
|
282
|
+
for (let i = 0; i < pendingEvents.length; i++) {
|
|
283
|
+
const ev = pendingEvents[i];
|
|
284
|
+
const remaining = deadline - Date.now();
|
|
285
|
+
if (flushed >= FLUSH_MAX_COUNT || remaining <= 0) break;
|
|
286
|
+
|
|
287
|
+
const timeoutMs = Math.max(50, remaining);
|
|
288
|
+
let classification;
|
|
289
|
+
try {
|
|
290
|
+
classification = await submitOne(server, identity, ev, timeoutMs);
|
|
291
|
+
} catch {
|
|
292
|
+
classification = 'retry';
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (classification === 'confirmed') {
|
|
296
|
+
flushed += 1;
|
|
297
|
+
submitted += 1;
|
|
298
|
+
if (ev.id) confirmedIds.add(ev.id);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (classification === 'drop') {
|
|
302
|
+
// drop 丢弃该条(含 401),不 retained、不 break,继续下一条
|
|
303
|
+
if (ev.id) droppedIds.add(ev.id);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
// retry:网络/5xx/429/timeout,本次及后续保留
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// 原子提交队列变化(按 id 差集删除已确认 + 已丢弃)
|
|
311
|
+
// 关键:rewrite 前必须重新读文件,把并发 append 的新行合并进来
|
|
312
|
+
if (confirmedIds.size > 0 || droppedIds.size > 0) {
|
|
313
|
+
const { events: latestEvents } = readPending(pendingFile);
|
|
314
|
+
const retained = latestEvents.filter((e) => {
|
|
315
|
+
if (e.id && confirmedIds.has(e.id)) return false;
|
|
316
|
+
if (e.id && droppedIds.has(e.id)) return false;
|
|
317
|
+
return true;
|
|
318
|
+
});
|
|
319
|
+
writePendingAtomic(pendingFile, retained);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return { submitted, confirmedIds, droppedIds };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 只冲刷 pending 队列,不产生新事件(cmdEnd / git 身份就绪后的补报入口)。
|
|
327
|
+
* 无身份返回 skipped 零请求;任何失败均不抛。
|
|
328
|
+
*
|
|
329
|
+
* @param {object} [options]
|
|
330
|
+
* @param {string} [options.homeDir] — 测试注入
|
|
331
|
+
* @param {string} [options.projectRoot] — kb-state.json 查找目录,缺省 process.cwd()
|
|
332
|
+
* @returns {Promise<{submitted:number,enqueued:boolean,skipped:boolean}>}
|
|
333
|
+
*/
|
|
334
|
+
async function flushPendingUsage(options = {}) {
|
|
335
|
+
const result = { submitted: 0, enqueued: false, skipped: false };
|
|
336
|
+
try {
|
|
337
|
+
const identity = readGitIdentity(options.homeDir);
|
|
338
|
+
if (!identity) {
|
|
339
|
+
result.skipped = true;
|
|
340
|
+
return result;
|
|
341
|
+
}
|
|
342
|
+
const pendingFile = pendingPathFor(options.homeDir);
|
|
343
|
+
const { events: pendingEvents } = readPending(pendingFile);
|
|
344
|
+
if (pendingEvents.length === 0) return result;
|
|
345
|
+
const server = resolveReportServer({ homeDir: options.homeDir, projectRoot: options.projectRoot });
|
|
346
|
+
const flushed = await flushPendingQueue(server, identity, pendingFile);
|
|
347
|
+
result.submitted = flushed.submitted;
|
|
348
|
+
return result;
|
|
349
|
+
} catch {
|
|
350
|
+
// 兜底:永不抛到调用方
|
|
351
|
+
return result;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
192
355
|
/**
|
|
193
356
|
* 上报一次使用事件(主入口)。任何失败均不抛。
|
|
194
357
|
*
|
|
@@ -196,119 +359,68 @@ async function submitOne(server, token, event, timeoutMs) {
|
|
|
196
359
|
* @param {string} options.skill — 七值之一;非七值立即返回
|
|
197
360
|
* @param {string} [options.startedAt] — ISO8601;缺省取当前
|
|
198
361
|
* @param {string} [options.homeDir] — 测试注入
|
|
199
|
-
* @param {
|
|
200
|
-
* @returns {Promise<{submitted:number,enqueued:boolean,
|
|
362
|
+
* @param {string} [options.projectRoot] — kb-state.json 查找目录,缺省 process.cwd()
|
|
363
|
+
* @returns {Promise<{submitted:number,enqueued:boolean,skipped:boolean}>}
|
|
201
364
|
*/
|
|
202
365
|
async function reportUsageOnce(options = {}) {
|
|
203
|
-
const result = { submitted: 0, enqueued: false,
|
|
366
|
+
const result = { submitted: 0, enqueued: false, skipped: false };
|
|
204
367
|
const skill = mapCommandToUsageSkill(options.skill);
|
|
205
368
|
if (!skill) {
|
|
206
369
|
result.skipped = true;
|
|
207
370
|
return result;
|
|
208
371
|
}
|
|
209
|
-
|
|
372
|
+
|
|
373
|
+
const startedAt = typeof options.startedAt === 'string' && options.startedAt
|
|
374
|
+
? options.startedAt
|
|
375
|
+
: new Date().toISOString();
|
|
376
|
+
const currentEvent = { skill, startedAt, id: generateEventId() };
|
|
377
|
+
const pendingFile = pendingPathFor(options.homeDir);
|
|
210
378
|
|
|
211
379
|
try {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
380
|
+
// 无 git 身份:当前事件入队(无身份字段,与遗留事件同构),本次冲刷整体跳过、
|
|
381
|
+
// 队列原有内容保留;身份就绪后由后续 flush 注入身份补报(慢 git 环境不静默丢失)。
|
|
382
|
+
const identity = readGitIdentity(options.homeDir);
|
|
383
|
+
if (!identity) {
|
|
384
|
+
try {
|
|
385
|
+
appendPending(currentEvent, pendingFile);
|
|
386
|
+
result.enqueued = true;
|
|
387
|
+
} catch { /* append 失败静默,不改退出码 */ }
|
|
220
388
|
return result;
|
|
221
389
|
}
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
const startedAt = typeof options.startedAt === 'string' && options.startedAt
|
|
225
|
-
? options.startedAt
|
|
226
|
-
: new Date().toISOString();
|
|
227
|
-
const currentEvent = { skill, startedAt, id: generateEventId() };
|
|
390
|
+
const server = resolveReportServer({ homeDir: options.homeDir, projectRoot: options.projectRoot });
|
|
228
391
|
|
|
229
|
-
const pendingFile = pendingPathFor(options.homeDir);
|
|
230
|
-
const deadline = Date.now() + SHARED_DEADLINE_MS;
|
|
231
392
|
const MAX_TIMEOUT_SKEW_MS = 500; // 调度容差
|
|
393
|
+
// 共享 3s deadline:冲刷与当前事件共用同一预算
|
|
394
|
+
const deadline = Date.now() + SHARED_DEADLINE_MS;
|
|
232
395
|
|
|
233
|
-
// 1. 先冲刷 pending(最多 100
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const failedEarly =
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const ev = pendingEvents[i];
|
|
242
|
-
const remaining = deadline - Date.now();
|
|
243
|
-
if (flushed >= FLUSH_MAX_COUNT || remaining <= 0) break;
|
|
244
|
-
|
|
245
|
-
const timeoutMs = Math.max(50, remaining);
|
|
246
|
-
let classification;
|
|
247
|
-
try {
|
|
248
|
-
classification = await submitOne(server, token, ev, timeoutMs);
|
|
249
|
-
} catch {
|
|
250
|
-
classification = 'retry';
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (classification === 'confirmed') {
|
|
254
|
-
flushed += 1;
|
|
255
|
-
result.submitted += 1;
|
|
256
|
-
if (ev.id) confirmedIds.add(ev.id);
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
if (classification === 'unauthorized') {
|
|
260
|
-
// 401:原子清除 token;本次及后续不再请求
|
|
261
|
-
try {
|
|
262
|
-
if (clearUserToken(options.homeDir)) {
|
|
263
|
-
result.tokenCleared = true;
|
|
264
|
-
onNotify('检测到平台凭证已失效,请运行 `kld-sdd auth login` 重新绑定。');
|
|
265
|
-
}
|
|
266
|
-
} catch { /* ignore */ }
|
|
267
|
-
failedEarly.hit = true;
|
|
268
|
-
failedEarly.kind = 'unauthorized';
|
|
269
|
-
break;
|
|
270
|
-
}
|
|
271
|
-
if (classification === 'drop') {
|
|
272
|
-
// kld-fixback YELLOW#4: drop 丢弃该条,不 retained、不 break,继续下一条
|
|
273
|
-
if (ev.id) droppedIds.add(ev.id);
|
|
274
|
-
continue;
|
|
275
|
-
}
|
|
276
|
-
// retry:网络/5xx/429/timeout,本次及后续保留
|
|
277
|
-
failedEarly.hit = true;
|
|
278
|
-
failedEarly.kind = 'retry';
|
|
279
|
-
break;
|
|
280
|
-
}
|
|
396
|
+
// 1. 先冲刷 pending(最多 100 条;retry 提前终止时剩余保留)
|
|
397
|
+
const flushOutcome = await flushPendingQueue(server, identity, pendingFile, deadline);
|
|
398
|
+
result.submitted += flushOutcome.submitted;
|
|
399
|
+
// 冲刷提前失败判定:零提交、零 drop,且队列仍有残留(retry 语义)
|
|
400
|
+
const failedEarly = flushOutcome.submitted === 0
|
|
401
|
+
&& flushOutcome.confirmedIds.size === 0
|
|
402
|
+
&& flushOutcome.droppedIds.size === 0
|
|
403
|
+
&& readPending(pendingFile).events.length > 0;
|
|
281
404
|
|
|
282
405
|
// 2. 发当前事件(若仍有预算且未提前失败)
|
|
283
|
-
//
|
|
284
|
-
// 『网络/超时/429/5xx 将事件追加或保留在 pending 队列』一致),不得静默丢弃。
|
|
406
|
+
// flush 提前失败时当前事件必须入队,不得静默丢弃。
|
|
285
407
|
let currentOutcome = 'pending'; // 'confirmed' | 'enqueued' | 'dropped' | 'pending'
|
|
286
|
-
if (failedEarly
|
|
408
|
+
if (failedEarly) {
|
|
287
409
|
currentOutcome = 'enqueued';
|
|
288
410
|
} else {
|
|
289
411
|
const remaining = deadline - Date.now();
|
|
290
412
|
if (remaining > MAX_TIMEOUT_SKEW_MS) {
|
|
291
413
|
let classification;
|
|
292
414
|
try {
|
|
293
|
-
classification = await submitOne(server,
|
|
415
|
+
classification = await submitOne(server, identity, currentEvent, remaining);
|
|
294
416
|
} catch {
|
|
295
417
|
classification = 'retry';
|
|
296
418
|
}
|
|
297
419
|
if (classification === 'confirmed') {
|
|
298
420
|
result.submitted += 1;
|
|
299
421
|
currentOutcome = 'confirmed';
|
|
300
|
-
} else if (classification === 'unauthorized') {
|
|
301
|
-
try {
|
|
302
|
-
if (clearUserToken(options.homeDir)) {
|
|
303
|
-
result.tokenCleared = true;
|
|
304
|
-
onNotify('检测到平台凭证已失效,请运行 `kld-sdd auth login` 重新绑定。');
|
|
305
|
-
}
|
|
306
|
-
} catch { /* ignore */ }
|
|
307
|
-
currentOutcome = 'enqueued';
|
|
308
|
-
failedEarly.hit = true;
|
|
309
|
-
failedEarly.kind = 'unauthorized';
|
|
310
422
|
} else if (classification === 'drop') {
|
|
311
|
-
//
|
|
423
|
+
// 当前事件 drop(含 401)直接丢弃不入队,不清任何本地状态
|
|
312
424
|
currentOutcome = 'dropped';
|
|
313
425
|
} else {
|
|
314
426
|
currentOutcome = 'enqueued';
|
|
@@ -318,18 +430,8 @@ async function reportUsageOnce(options = {}) {
|
|
|
318
430
|
}
|
|
319
431
|
}
|
|
320
432
|
|
|
321
|
-
// 3.
|
|
322
|
-
// 关键:rewrite 前必须重新读文件,把并发 append 的新行合并进来
|
|
433
|
+
// 3. append 当前事件(若需要)
|
|
323
434
|
try {
|
|
324
|
-
if (confirmedIds.size > 0 || droppedIds.size > 0) {
|
|
325
|
-
const { events: latestEvents } = readPending(pendingFile);
|
|
326
|
-
const retained = latestEvents.filter((e) => {
|
|
327
|
-
if (e.id && confirmedIds.has(e.id)) return false;
|
|
328
|
-
if (e.id && droppedIds.has(e.id)) return false;
|
|
329
|
-
return true;
|
|
330
|
-
});
|
|
331
|
-
writePendingAtomic(pendingFile, retained);
|
|
332
|
-
}
|
|
333
435
|
if (currentOutcome === 'enqueued') {
|
|
334
436
|
appendPending(currentEvent, pendingFile);
|
|
335
437
|
result.enqueued = true;
|
|
@@ -345,8 +447,12 @@ async function reportUsageOnce(options = {}) {
|
|
|
345
447
|
|
|
346
448
|
module.exports = {
|
|
347
449
|
reportUsageOnce,
|
|
450
|
+
flushPendingUsage,
|
|
348
451
|
appendPending,
|
|
349
452
|
readPending,
|
|
453
|
+
readGitIdentity,
|
|
454
|
+
resolveReportServer,
|
|
455
|
+
_resetGitIdentityCache,
|
|
350
456
|
PENDING_FILE_NAME,
|
|
351
457
|
PENDING_MAX_BYTES,
|
|
352
458
|
FLUSH_MAX_COUNT,
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
// kld-T02 — ~/.kld-sdd/config.json 用户级配置管理
|
|
2
|
-
// 职责:读取、规范化、原子写、0600/Windows ACL
|
|
3
|
-
//
|
|
2
|
+
// 职责:读取、规范化、原子写、0600/Windows ACL。
|
|
3
|
+
// schema v2 = {version, server};使用统计按 git 身份上报,不再保存任何 token。
|
|
4
|
+
// 旧版含 token/userName/boundAt 的 config 读取时 sanitize 为 {version, server},回写即淘汰。
|
|
4
5
|
'use strict';
|
|
5
6
|
|
|
6
7
|
const fs = require('node:fs');
|
|
7
8
|
const os = require('node:os');
|
|
8
9
|
const path = require('node:path');
|
|
9
10
|
|
|
10
|
-
const USER_CONFIG_VERSION =
|
|
11
|
+
const USER_CONFIG_VERSION = 2;
|
|
11
12
|
const CONFIG_DIR_NAME = '.kld-sdd';
|
|
12
13
|
const CONFIG_FILE_NAME = 'config.json';
|
|
13
14
|
|
|
@@ -32,9 +33,23 @@ function getConfigPath(homeDir) {
|
|
|
32
33
|
return path.join(getConfigDir(homeDir), CONFIG_FILE_NAME);
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
/**
|
|
37
|
+
* 将任意已解析对象规范化为 v2 config:只保留 {version, server}。
|
|
38
|
+
* @param {object} parsed
|
|
39
|
+
* @returns {{version: number, server?: string}}
|
|
40
|
+
*/
|
|
41
|
+
function sanitizeConfig(parsed) {
|
|
42
|
+
const config = { version: USER_CONFIG_VERSION };
|
|
43
|
+
if (typeof parsed.server === 'string') {
|
|
44
|
+
config.server = parsed.server;
|
|
45
|
+
}
|
|
46
|
+
return config;
|
|
47
|
+
}
|
|
48
|
+
|
|
35
49
|
/**
|
|
36
50
|
* 读取用户配置。
|
|
37
51
|
* - 文件不存在 / 损坏 JSON / 非对象 → 返回 null(不抛)
|
|
52
|
+
* - 旧版多余字段(token/userName/boundAt 等)读取即忽略,不回传
|
|
38
53
|
* - 返回新引用;调用方修改不影响后续读取
|
|
39
54
|
*
|
|
40
55
|
* @param {string} [homeDir]
|
|
@@ -55,12 +70,7 @@ function readUserConfig(homeDir) {
|
|
|
55
70
|
return null;
|
|
56
71
|
}
|
|
57
72
|
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return null;
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
return JSON.parse(JSON.stringify(parsed));
|
|
61
|
-
} catch {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
73
|
+
return sanitizeConfig(parsed);
|
|
64
74
|
}
|
|
65
75
|
|
|
66
76
|
/**
|
|
@@ -83,6 +93,7 @@ function hardenPermissions(target, kind) {
|
|
|
83
93
|
|
|
84
94
|
/**
|
|
85
95
|
* 原子写入用户配置:先 temp 后 rename,目录与文件均设置权限。
|
|
96
|
+
* 只持久化 {version, server},多余字段写入即淘汰。
|
|
86
97
|
* @param {object} config — 必须含 version 字段
|
|
87
98
|
* @param {string} [homeDir]
|
|
88
99
|
* @throws {Error} config 非法 / 缺 version
|
|
@@ -100,7 +111,7 @@ function writeUserConfig(config, homeDir) {
|
|
|
100
111
|
hardenPermissions(dir, 'dir');
|
|
101
112
|
|
|
102
113
|
const tmp = path.join(dir, `.${CONFIG_FILE_NAME}.${process.pid}.${Date.now()}.tmp`);
|
|
103
|
-
const payload = JSON.stringify(config, null, 2);
|
|
114
|
+
const payload = JSON.stringify(sanitizeConfig(config), null, 2);
|
|
104
115
|
fs.writeFileSync(tmp, payload, { encoding: 'utf8', mode: 0o600 });
|
|
105
116
|
hardenPermissions(tmp, 'file');
|
|
106
117
|
try {
|
|
@@ -112,46 +123,10 @@ function writeUserConfig(config, homeDir) {
|
|
|
112
123
|
hardenPermissions(file, 'file');
|
|
113
124
|
}
|
|
114
125
|
|
|
115
|
-
/**
|
|
116
|
-
* 原子清除 token 字段,保留其他所有字段。
|
|
117
|
-
* - 文件不存在 / 损坏 / 已无 token → 幂等返回 false
|
|
118
|
-
* - 成功清除返回 true
|
|
119
|
-
*
|
|
120
|
-
* @param {string} [homeDir]
|
|
121
|
-
* @returns {boolean}
|
|
122
|
-
*/
|
|
123
|
-
function clearUserToken(homeDir) {
|
|
124
|
-
const file = getConfigPath(homeDir);
|
|
125
|
-
if (!fs.existsSync(file)) return false;
|
|
126
|
-
const current = readUserConfig(homeDir);
|
|
127
|
-
if (!current) return false;
|
|
128
|
-
if (!Object.prototype.hasOwnProperty.call(current, 'token')) return false;
|
|
129
|
-
delete current.token;
|
|
130
|
-
writeUserConfig(current, homeDir);
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* 敏感信息脱敏:任何非空字符串 token 一律返回 '***'。
|
|
136
|
-
* - 空字符串保持空字符串
|
|
137
|
-
* - 非字符串原样返回
|
|
138
|
-
* - 永不抛出
|
|
139
|
-
*
|
|
140
|
-
* @param {*} value
|
|
141
|
-
* @returns {*}
|
|
142
|
-
*/
|
|
143
|
-
function redactToken(value) {
|
|
144
|
-
if (typeof value !== 'string') return value;
|
|
145
|
-
if (value.length === 0) return '';
|
|
146
|
-
return '***';
|
|
147
|
-
}
|
|
148
|
-
|
|
149
126
|
module.exports = {
|
|
150
127
|
USER_CONFIG_VERSION,
|
|
151
128
|
getConfigDir,
|
|
152
129
|
getConfigPath,
|
|
153
130
|
readUserConfig,
|
|
154
131
|
writeUserConfig,
|
|
155
|
-
clearUserToken,
|
|
156
|
-
redactToken,
|
|
157
132
|
};
|
|
@@ -200,7 +200,7 @@ function identityFromValues(values = {}) {
|
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
function flushRequirementRef(requirementRefs, currentRef) {
|
|
203
|
-
if (!currentRef) return
|
|
203
|
+
if (!currentRef) return;
|
|
204
204
|
const { normalize, validate } = require('./external-key.cjs');
|
|
205
205
|
const system = currentRef.system;
|
|
206
206
|
const objectType = currentRef['object-type'] || currentRef.object_type;
|
|
@@ -224,7 +224,6 @@ function flushRequirementRef(requirementRefs, currentRef) {
|
|
|
224
224
|
}
|
|
225
225
|
requirementRefs.push(entry);
|
|
226
226
|
}
|
|
227
|
-
return null;
|
|
228
227
|
}
|
|
229
228
|
|
|
230
229
|
function parseStructuredFrontmatter(lines) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/bin/sh
|
|
2
2
|
# KLD SDD Commit-MSG Hook
|
|
3
|
-
#
|
|
3
|
+
# 在下方 SCRIPTS 列表中声明要加载的 .cjs 脚本
|
|
4
|
+
# 删除某行 = 禁用该插件
|
|
4
5
|
# marker: KLD SDD quality gate
|
|
5
6
|
|
|
6
7
|
# 从 hook 所在目录推算 git 根目录
|
|
@@ -8,39 +9,81 @@ hook_dir="$(cd "$(dirname "$0")" && pwd)"
|
|
|
8
9
|
git_dir="$(dirname "$hook_dir")"
|
|
9
10
|
git_root="$(dirname "$git_dir")"
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
hooks_dir=""
|
|
12
13
|
|
|
13
14
|
# 方法1: git config sdd.specPath(多仓布局:spec 是独立 clone)
|
|
14
15
|
spec_path=$(git config --local sdd.specPath 2>/dev/null)
|
|
15
|
-
if [ -n "$spec_path" ] && [ -
|
|
16
|
-
|
|
16
|
+
if [ -n "$spec_path" ] && [ -d "$spec_path/skywalk-sdd/git-hooks" ]; then
|
|
17
|
+
hooks_dir="$spec_path/skywalk-sdd/git-hooks"
|
|
17
18
|
fi
|
|
18
19
|
|
|
19
20
|
# 方法2: .sdd-spec-root(单仓 mono 布局:skywalk-sdd 在 spec 包裹包子目录内)
|
|
20
|
-
if [ -z "$
|
|
21
|
+
if [ -z "$hooks_dir" ] && [ -f "$git_root/.sdd-spec-root" ]; then
|
|
21
22
|
spec_rel=$(cat "$git_root/.sdd-spec-root" | head -1 | tr -d '[:space:]')
|
|
22
|
-
if [ -n "$spec_rel" ] && [ -
|
|
23
|
-
|
|
23
|
+
if [ -n "$spec_rel" ] && [ -d "$git_root/$spec_rel/skywalk-sdd/git-hooks" ]; then
|
|
24
|
+
hooks_dir="$git_root/$spec_rel/skywalk-sdd/git-hooks"
|
|
24
25
|
fi
|
|
25
26
|
fi
|
|
26
27
|
|
|
27
28
|
# 方法3: 从 git 根向上搜索 skywalk-sdd/git-hooks(spec 仓本身或父级)
|
|
28
|
-
if [ -z "$
|
|
29
|
+
if [ -z "$hooks_dir" ]; then
|
|
29
30
|
search_dir="$git_root"
|
|
30
31
|
while [ "$search_dir" != "/" ] && [ "$search_dir" != "" ]; do
|
|
31
|
-
if [ -
|
|
32
|
-
|
|
32
|
+
if [ -d "$search_dir/skywalk-sdd/git-hooks" ]; then
|
|
33
|
+
hooks_dir="$search_dir/skywalk-sdd/git-hooks"
|
|
33
34
|
break
|
|
34
35
|
fi
|
|
35
36
|
search_dir="$(dirname "$search_dir")"
|
|
36
37
|
done
|
|
37
38
|
fi
|
|
38
39
|
|
|
39
|
-
if [ -z "$
|
|
40
|
-
echo "[SDD] 未找到
|
|
40
|
+
if [ -z "$hooks_dir" ]; then
|
|
41
|
+
echo "[SDD] 未找到 spec 仓的 skywalk-sdd/git-hooks 目录,跳过 commit-msg"
|
|
41
42
|
exit 0
|
|
42
43
|
fi
|
|
43
44
|
|
|
44
|
-
#
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
# 从配置文件读取插件列表
|
|
46
|
+
config_file="$hooks_dir/hooks.config"
|
|
47
|
+
if [ ! -f "$config_file" ]; then
|
|
48
|
+
echo "[SDD] ⚠️ 配置文件不存在:hooks.config,使用默认插件"
|
|
49
|
+
SCRIPTS="commit-msg-sdd-trailer.cjs"
|
|
50
|
+
else
|
|
51
|
+
# 读取 [commit-msg] 段落的配置
|
|
52
|
+
in_section=0
|
|
53
|
+
SCRIPTS=""
|
|
54
|
+
while IFS= read -r line; do
|
|
55
|
+
line=$(printf '%s' "$line" | tr -d '\r')
|
|
56
|
+
case "$line" in
|
|
57
|
+
"[commit-msg]") in_section=1 ;;
|
|
58
|
+
"["*) in_section=0 ;;
|
|
59
|
+
"#"*|"") ;;
|
|
60
|
+
*)
|
|
61
|
+
if [ "$in_section" -eq 1 ]; then
|
|
62
|
+
SCRIPTS="$SCRIPTS $line"
|
|
63
|
+
fi
|
|
64
|
+
;;
|
|
65
|
+
esac
|
|
66
|
+
done < "$config_file"
|
|
67
|
+
fi
|
|
68
|
+
|
|
69
|
+
executed=0
|
|
70
|
+
failed=0
|
|
71
|
+
for script_name in $SCRIPTS; do
|
|
72
|
+
script="$hooks_dir/$script_name"
|
|
73
|
+
if [ ! -f "$script" ]; then
|
|
74
|
+
echo "[SDD] ⚠️ 插件不存在:$script_name"
|
|
75
|
+
continue
|
|
76
|
+
fi
|
|
77
|
+
executed=$((executed + 1))
|
|
78
|
+
echo "[SDD] 执行 commit-msg 插件:$script_name"
|
|
79
|
+
node "$script" "$1" --project="$git_root" || failed=$((failed + 1))
|
|
80
|
+
done
|
|
81
|
+
|
|
82
|
+
if [ "$executed" -eq 0 ]; then
|
|
83
|
+
echo "[SDD] 无 commit-msg 插件,跳过"
|
|
84
|
+
elif [ "$failed" -gt 0 ]; then
|
|
85
|
+
echo "[SDD] ❌ 共执行 $executed 个 commit-msg 插件,$failed 个失败"
|
|
86
|
+
exit 1
|
|
87
|
+
else
|
|
88
|
+
echo "[SDD] ✅ 共执行 $executed 个 commit-msg 插件,全部通过"
|
|
89
|
+
fi
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# KLD SDD Hook 插件配置文件
|
|
2
|
+
# 每行一个脚本名,空行和 # 开头的行会被忽略
|
|
3
|
+
# 注释某行 = 禁用该插件,取消注释 = 启用该插件
|
|
4
|
+
|
|
5
|
+
[commit-msg]
|
|
6
|
+
# 自动写入 Spec-Change / Spec-Revision Trailer 到 commit message
|
|
7
|
+
commit-msg-sdd-trailer.cjs
|
|
8
|
+
|
|
9
|
+
[pre-commit]
|
|
10
|
+
# 检查 tasks.md 中是否有未完成的归档任务
|
|
11
|
+
pre-commit-sdd-check.cjs
|
|
12
|
+
# 检查 Spec 一致性校验报告(置信度、过期、人工确认等)
|
|
13
|
+
pre-commit-consistency-check.cjs
|
|
14
|
+
|
|
15
|
+
[pre-push]
|
|
16
|
+
# 运行 doctor 完整诊断(配置、Trailer 协议、活动变更等)
|
|
17
|
+
pre-push-sdd-check.cjs
|
|
18
|
+
# 轻量检查一致性报告是否存在、是否过期
|
|
19
|
+
pre-push-consistency-check.cjs
|