@kangyishuai/dsh-casefile 0.8.0 → 0.9.0
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/README.md +95 -36
- package/README.zh-CN.md +96 -36
- package/lib/actions.js +270 -36
- package/lib/client.js +815 -102
- package/lib/core/brief.js +58 -12
- package/lib/core/ingest-craft.js +2 -2
- package/lib/core/ingest.js +10 -4
- package/lib/core/model.js +17 -1
- package/lib/core/report.js +8 -1
- package/lib/core/store.js +371 -15
- package/lib/service.js +54 -2
- package/lib/tools.js +112 -16
- package/locale/en.json +11 -1
- package/locale/zh.json +11 -1
- package/package.json +1 -1
package/lib/actions.js
CHANGED
|
@@ -64,6 +64,23 @@ export class CasefileActionError extends Error {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* 危险操作审计里的「谁发起的」(复审 A2 / docs N8)。
|
|
69
|
+
*
|
|
70
|
+
* `case_project` 工具由模型发起、回收站 UI 走 Remote,两条路最终都进同一个 action;以前全部
|
|
71
|
+
* 硬编码 `actor: "user"`,事后无法回答「这次 drop 是我点的,还是模型自己决定的」。现在两个入口
|
|
72
|
+
* 各自显式传 `actor`(工具 → `agent`,UI → `user`),没传就记 `unknown`——宁可如实标不确定,
|
|
73
|
+
* 也不猜成用户。工具路径还能带上会话 id 作为来源证据。
|
|
74
|
+
* @param {{ actor?: unknown, sessionId?: unknown } | null | undefined} [options]
|
|
75
|
+
* @returns {{ actor: "user" | "agent" | "unknown", sessionId: string | null }}
|
|
76
|
+
*/
|
|
77
|
+
export function auditOriginOf(options) {
|
|
78
|
+
const actor = options?.actor === "agent" || options?.actor === "user" ? options.actor : "unknown";
|
|
79
|
+
const raw = options?.sessionId;
|
|
80
|
+
const sessionId = typeof raw === "string" && raw.trim() !== "" ? raw.trim().slice(0, 200) : null;
|
|
81
|
+
return { actor, sessionId };
|
|
82
|
+
}
|
|
83
|
+
|
|
67
84
|
/**
|
|
68
85
|
* 造一组动作。
|
|
69
86
|
* @param {{
|
|
@@ -88,6 +105,36 @@ export function createActions(options) {
|
|
|
88
105
|
const onWaitChange = options.onWaitChange;
|
|
89
106
|
const runner = options.runner ?? null;
|
|
90
107
|
const provenance = options.provenance ?? null;
|
|
108
|
+
|
|
109
|
+
// 会话 → 项目的指针落盘(AUD-CASEFILE-01):重启后同一个会话仍能 recall。
|
|
110
|
+
// 只存「会话 → 项目 id」的轻量索引;项目数据本身仍在 projects/<id>/。
|
|
111
|
+
const pointerFile = join(dataRoot, "session-pointers.json");
|
|
112
|
+
const MAX_POINTERS = 200;
|
|
113
|
+
let pointersLoaded = false;
|
|
114
|
+
/** 懒加载:第一次 recall/remember 时才读盘,避免每个会话启动都摸文件。 */
|
|
115
|
+
const loadPointers = () => {
|
|
116
|
+
if (pointersLoaded) return;
|
|
117
|
+
pointersLoaded = true;
|
|
118
|
+
const value = store.readJson(pointerFile);
|
|
119
|
+
const pointers = value?.pointers;
|
|
120
|
+
if (pointers == null || typeof pointers !== "object") return;
|
|
121
|
+
for (const [sessionId, projectId] of Object.entries(pointers)) {
|
|
122
|
+
if (typeof sessionId === "string" && typeof projectId === "string") {
|
|
123
|
+
currentProject.set(sessionId, projectId);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const persistPointers = () => {
|
|
128
|
+
/** @type {Record<string, string>} */
|
|
129
|
+
const pointers = {};
|
|
130
|
+
for (const [sessionId, projectId] of currentProject) pointers[sessionId] = projectId;
|
|
131
|
+
// Map 尾部是最近使用过的会话(remember 会先 delete 再 set);超上限时丢最久没用的。
|
|
132
|
+
const keys = Object.keys(pointers);
|
|
133
|
+
for (const key of keys.slice(0, Math.max(0, keys.length - MAX_POINTERS))) {
|
|
134
|
+
delete pointers[key];
|
|
135
|
+
}
|
|
136
|
+
store.writeJson(pointerFile, { version: 1, updatedAt: nowIso(), pointers });
|
|
137
|
+
};
|
|
91
138
|
// checker 需要固定形状的 runner;外层 runner 更宽松,这里做一次归一。
|
|
92
139
|
const checkerRunner =
|
|
93
140
|
runner == null
|
|
@@ -151,21 +198,43 @@ export function createActions(options) {
|
|
|
151
198
|
// ---------- 会话记忆 ----------
|
|
152
199
|
|
|
153
200
|
/**
|
|
201
|
+
* 记住「本会话当前项目」,并落盘(重启后仍可 recall)。
|
|
202
|
+
* 任何拿到显式 projectId 的工具调用都会走到这里,不再只依赖 open。
|
|
154
203
|
* @param {any} exec
|
|
155
204
|
* @param {string} projectId
|
|
156
205
|
*/
|
|
157
206
|
remember(exec, projectId) {
|
|
158
207
|
const sessionId = sessionOf(exec);
|
|
159
|
-
|
|
208
|
+
const id = String(projectId ?? "");
|
|
209
|
+
if (sessionId === undefined || id === "") return;
|
|
210
|
+
// 先删后设:让刚用过的会话排到 Map 尾部,淘汰时先丢最久没用的(LRU,不是 FIFO)。
|
|
211
|
+
currentProject.delete(sessionId);
|
|
212
|
+
currentProject.set(sessionId, id);
|
|
213
|
+
persistPointers();
|
|
160
214
|
},
|
|
161
215
|
|
|
162
216
|
/**
|
|
217
|
+
* 取「本会话当前项目」;项目已不存在时自清理悬空指针。
|
|
163
218
|
* @param {any} exec
|
|
164
219
|
* @returns {string | undefined}
|
|
165
220
|
*/
|
|
166
221
|
recall(exec) {
|
|
222
|
+
loadPointers();
|
|
167
223
|
const sessionId = sessionOf(exec);
|
|
168
|
-
|
|
224
|
+
if (sessionId === undefined) return undefined;
|
|
225
|
+
const id = currentProject.get(sessionId);
|
|
226
|
+
if (typeof id !== "string" || id === "") return undefined;
|
|
227
|
+
try {
|
|
228
|
+
if (!store.getProject(id)) {
|
|
229
|
+
currentProject.delete(sessionId);
|
|
230
|
+
persistPointers();
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
} catch {
|
|
234
|
+
// 读不了(高版本文件 / 非法 id)时保持指针,让下一步动作自己报稳定错误码。
|
|
235
|
+
return id;
|
|
236
|
+
}
|
|
237
|
+
return id;
|
|
169
238
|
},
|
|
170
239
|
|
|
171
240
|
// ---------- 项目 ----------
|
|
@@ -179,11 +248,23 @@ export function createActions(options) {
|
|
|
179
248
|
},
|
|
180
249
|
|
|
181
250
|
/**
|
|
251
|
+
* 打开项目:显式 id 命中已有项目时直接打开它(不再报 project-exists),
|
|
252
|
+
* 否则新建。两种情况都会建立「本会话当前项目」指针。
|
|
182
253
|
* @param {Record<string, any>} input
|
|
183
254
|
* @param {any} [exec]
|
|
184
255
|
* @returns {Promise<Record<string, any>>}
|
|
185
256
|
*/
|
|
186
257
|
async openProject(input, exec) {
|
|
258
|
+
const wantedId = input?.id == null ? null : String(input.id);
|
|
259
|
+
if (wantedId != null && wantedId !== "") {
|
|
260
|
+
const existing = store.getProject(wantedId);
|
|
261
|
+
if (existing) {
|
|
262
|
+
store.markLastOpened(existing.id);
|
|
263
|
+
if (exec) this.remember(exec, existing.id);
|
|
264
|
+
// 不重写 project.json:打开已有项目不应改它的标题/授权档/工具锚点。
|
|
265
|
+
return { ...existing, reopened: true };
|
|
266
|
+
}
|
|
267
|
+
}
|
|
187
268
|
const project = store.createProject(input ?? {});
|
|
188
269
|
// 把"当时用的哪套工具"锚进项目:REA 版本 + 工具面哈希。
|
|
189
270
|
const probe = typeof provenance === "function" ? provenance() : null;
|
|
@@ -193,7 +274,8 @@ export function createActions(options) {
|
|
|
193
274
|
capabilityHash: probe.capabilityHash,
|
|
194
275
|
})
|
|
195
276
|
: project;
|
|
196
|
-
|
|
277
|
+
store.markLastOpened(anchored.id);
|
|
278
|
+
if (exec) this.remember(exec, anchored.id);
|
|
197
279
|
return anchored;
|
|
198
280
|
},
|
|
199
281
|
|
|
@@ -203,6 +285,8 @@ export function createActions(options) {
|
|
|
203
285
|
*/
|
|
204
286
|
async detail(projectId) {
|
|
205
287
|
const project = requireProject(projectId);
|
|
288
|
+
// UI 选中项目走的也是这条:记「最近打开」,供缺失 projectId 时的提示使用。
|
|
289
|
+
store.markLastOpened(project.id);
|
|
206
290
|
const data = store.readAll(project.id);
|
|
207
291
|
return {
|
|
208
292
|
project,
|
|
@@ -268,19 +352,19 @@ export function createActions(options) {
|
|
|
268
352
|
*/
|
|
269
353
|
async addEvidence(projectId, input, exec) {
|
|
270
354
|
const project = requireProject(projectId);
|
|
271
|
-
// 产物接管:/tmp 里的东西会被清理,清了 sha256 就无法复算,项目只能停在 investigating。
|
|
272
|
-
const archived =
|
|
273
|
-
typeof input?.artifactPath === "string" && input.artifactPath.trim() !== ""
|
|
274
|
-
? store.archiveArtifact(project.id, input.artifactPath)
|
|
275
|
-
: null;
|
|
276
355
|
const record = normalizeEvidence({
|
|
277
356
|
...input,
|
|
278
357
|
// 外部摄入时,同一份报告可以同时是多条证据的出处:允许直接传入已归档对象。
|
|
279
|
-
artifact:
|
|
358
|
+
artifact: input?.artifact ?? null,
|
|
280
359
|
sessionId: input?.sessionId ?? sessionOf(exec) ?? null,
|
|
281
360
|
});
|
|
361
|
+
// 先校验后归档:其余字段校验失败时不应该先把文件复制进项目目录(AUD-CASEFILE-06 孤儿产物)。
|
|
362
|
+
// 产物接管:/tmp 里的东西会被清理,清了 sha256 就无法复算,项目只能停在 investigating。
|
|
363
|
+
if (typeof input?.artifactPath === "string" && input.artifactPath.trim() !== "") {
|
|
364
|
+
record.artifact = store.archiveArtifact(project.id, input.artifactPath);
|
|
365
|
+
}
|
|
282
366
|
store.appendRecord(project.id, "evidence", record);
|
|
283
|
-
store.touch(project.id);
|
|
367
|
+
await store.touch(project.id);
|
|
284
368
|
store.audit(project.id, {
|
|
285
369
|
actor: "agent",
|
|
286
370
|
action: "evidence/add",
|
|
@@ -328,7 +412,7 @@ export function createActions(options) {
|
|
|
328
412
|
const project = requireProject(projectId);
|
|
329
413
|
const record = normalizeClaim(input);
|
|
330
414
|
store.appendRecord(project.id, "claims", record);
|
|
331
|
-
store.touch(project.id);
|
|
415
|
+
await store.touch(project.id);
|
|
332
416
|
store.audit(project.id, {
|
|
333
417
|
actor: "agent",
|
|
334
418
|
action: "claim/add",
|
|
@@ -394,7 +478,7 @@ export function createActions(options) {
|
|
|
394
478
|
if (updated.length === 0) {
|
|
395
479
|
throw new CaseStoreError("not-found", `没有匹配的结论:${[...wanted].join("、")}`);
|
|
396
480
|
}
|
|
397
|
-
store.touch(project.id);
|
|
481
|
+
await store.touch(project.id);
|
|
398
482
|
store.audit(project.id, {
|
|
399
483
|
actor: "agent",
|
|
400
484
|
action: "claim/select",
|
|
@@ -415,7 +499,7 @@ export function createActions(options) {
|
|
|
415
499
|
const project = requireProject(projectId);
|
|
416
500
|
const record = normalizeFeature(input);
|
|
417
501
|
store.appendRecord(project.id, "features", record);
|
|
418
|
-
store.touch(project.id);
|
|
502
|
+
await store.touch(project.id);
|
|
419
503
|
store.audit(project.id, {
|
|
420
504
|
actor: "agent",
|
|
421
505
|
action: "feature/add",
|
|
@@ -443,7 +527,7 @@ export function createActions(options) {
|
|
|
443
527
|
const project = requireProject(projectId);
|
|
444
528
|
const record = normalizeUnknown(input);
|
|
445
529
|
store.appendRecord(project.id, "unknowns", record);
|
|
446
|
-
store.touch(project.id);
|
|
530
|
+
await store.touch(project.id);
|
|
447
531
|
store.audit(project.id, {
|
|
448
532
|
actor: "agent",
|
|
449
533
|
action: "unknown/add",
|
|
@@ -480,7 +564,7 @@ export function createActions(options) {
|
|
|
480
564
|
resolvedAt: nowIso(),
|
|
481
565
|
};
|
|
482
566
|
store.appendRecord(project.id, "unknowns", next);
|
|
483
|
-
store.touch(project.id);
|
|
567
|
+
await store.touch(project.id);
|
|
484
568
|
store.audit(project.id, { actor: "agent", action: "unknown/resolve", target: next.id });
|
|
485
569
|
return next;
|
|
486
570
|
},
|
|
@@ -496,7 +580,7 @@ export function createActions(options) {
|
|
|
496
580
|
const project = requireProject(projectId);
|
|
497
581
|
const record = normalizeObligation(input);
|
|
498
582
|
store.appendRecord(project.id, "obligations", record);
|
|
499
|
-
store.touch(project.id);
|
|
583
|
+
await store.touch(project.id);
|
|
500
584
|
store.audit(project.id, {
|
|
501
585
|
actor: "agent",
|
|
502
586
|
action: "obligation/add",
|
|
@@ -545,7 +629,7 @@ export function createActions(options) {
|
|
|
545
629
|
at: nowIso(),
|
|
546
630
|
};
|
|
547
631
|
store.appendRecord(project.id, "checks", record);
|
|
548
|
-
store.touch(project.id);
|
|
632
|
+
await store.touch(project.id);
|
|
549
633
|
store.audit(project.id, {
|
|
550
634
|
actor: "agent",
|
|
551
635
|
action: "check/add",
|
|
@@ -632,7 +716,7 @@ export function createActions(options) {
|
|
|
632
716
|
version: Number(obligation.version ?? 1) + 1,
|
|
633
717
|
};
|
|
634
718
|
store.appendRecord(project.id, "obligations", next);
|
|
635
|
-
store.touch(project.id);
|
|
719
|
+
await store.touch(project.id);
|
|
636
720
|
store.audit(project.id, {
|
|
637
721
|
actor: "agent",
|
|
638
722
|
action: "obligation/close",
|
|
@@ -661,7 +745,7 @@ export function createActions(options) {
|
|
|
661
745
|
version: Number(obligation.version ?? 1) + 1,
|
|
662
746
|
};
|
|
663
747
|
store.appendRecord(project.id, "obligations", next);
|
|
664
|
-
store.touch(project.id);
|
|
748
|
+
await store.touch(project.id);
|
|
665
749
|
store.audit(project.id, { actor: "agent", action: "obligation/reopen", target: next.id });
|
|
666
750
|
return next;
|
|
667
751
|
},
|
|
@@ -715,7 +799,7 @@ export function createActions(options) {
|
|
|
715
799
|
detail: JSON.stringify(result.detail ?? {}),
|
|
716
800
|
});
|
|
717
801
|
store.appendRecord(project.id, "builds", build);
|
|
718
|
-
store.touch(project.id);
|
|
802
|
+
await store.touch(project.id);
|
|
719
803
|
store.audit(project.id, {
|
|
720
804
|
actor: "agent",
|
|
721
805
|
action: "build/run",
|
|
@@ -791,7 +875,7 @@ export function createActions(options) {
|
|
|
791
875
|
at: checkedAt,
|
|
792
876
|
};
|
|
793
877
|
store.appendRecord(project.id, "checks", record);
|
|
794
|
-
store.touch(project.id);
|
|
878
|
+
await store.touch(project.id);
|
|
795
879
|
store.audit(project.id, {
|
|
796
880
|
actor: "agent",
|
|
797
881
|
action: "check/run",
|
|
@@ -838,7 +922,7 @@ export function createActions(options) {
|
|
|
838
922
|
detail: { reasons: entry.reasons },
|
|
839
923
|
});
|
|
840
924
|
}
|
|
841
|
-
if (reopened.length > 0) store.touch(project.id);
|
|
925
|
+
if (reopened.length > 0) await store.touch(project.id);
|
|
842
926
|
return reopened;
|
|
843
927
|
},
|
|
844
928
|
|
|
@@ -1074,7 +1158,7 @@ export function createActions(options) {
|
|
|
1074
1158
|
reason: input?.reason,
|
|
1075
1159
|
});
|
|
1076
1160
|
store.appendRecord(project.id, "attestations", attestation);
|
|
1077
|
-
store.touch(project.id);
|
|
1161
|
+
await store.touch(project.id);
|
|
1078
1162
|
store.audit(project.id, {
|
|
1079
1163
|
actor: "agent",
|
|
1080
1164
|
action: "builder/start",
|
|
@@ -1515,22 +1599,158 @@ export function createActions(options) {
|
|
|
1515
1599
|
},
|
|
1516
1600
|
|
|
1517
1601
|
/**
|
|
1518
|
-
* 丢弃项目:整个目录移进 dataRoot/trash
|
|
1519
|
-
*
|
|
1602
|
+
* 丢弃项目:整个目录移进 dataRoot/trash(可恢复)。
|
|
1603
|
+
* ADR 0002:默认进回收站、必须显式 confirm、支持 dry-run 且给出影响面、写数据根审计。
|
|
1520
1604
|
* @param {string} projectId
|
|
1521
|
-
* @param {{ confirm?: boolean
|
|
1605
|
+
* @param {{ confirm?: boolean, dryRun?: boolean, actor?: unknown,
|
|
1606
|
+
* sessionId?: unknown }} [options]
|
|
1522
1607
|
* @returns {Promise<Record<string, any>>}
|
|
1523
1608
|
*/
|
|
1524
1609
|
async dropProject(projectId, options = {}) {
|
|
1525
1610
|
const project = requireProject(projectId);
|
|
1611
|
+
const impact = store.projectImpact(project.id);
|
|
1612
|
+
// 预览路径每次取当前时间戳,**不是**最终落点:真正 drop 时会重新生成目录名(F5)。
|
|
1613
|
+
const previewTrashPath = store.expectedTrashPath(project.id);
|
|
1614
|
+
if (options?.dryRun === true) {
|
|
1615
|
+
return {
|
|
1616
|
+
dryRun: true,
|
|
1617
|
+
projectId: project.id,
|
|
1618
|
+
projects: 1,
|
|
1619
|
+
bytes: impact.bytes,
|
|
1620
|
+
files: impact.files,
|
|
1621
|
+
trashRoot: store.trashRoot,
|
|
1622
|
+
previewTrashPath,
|
|
1623
|
+
recoverable: true,
|
|
1624
|
+
summary: `将把项目 ${project.id}(${impact.files} 个文件 / ${impact.bytes} 字节)移进 ${store.trashRoot}/(预览路径 ${previewTrashPath};实际落点会带新的时间戳,可在回收站恢复)`,
|
|
1625
|
+
};
|
|
1626
|
+
}
|
|
1526
1627
|
if (options?.confirm !== true) {
|
|
1527
1628
|
throw new CasefileActionError(
|
|
1528
1629
|
"confirm-required",
|
|
1529
|
-
`丢弃项目会把 projects/${project.id}
|
|
1630
|
+
`丢弃项目会把 projects/${project.id}(${impact.files} 个文件 / ${impact.bytes} 字节)整个移进 trash(可在回收站恢复,不会硬删)。确认请传 confirm: true。`,
|
|
1530
1631
|
);
|
|
1531
1632
|
}
|
|
1532
1633
|
const result = store.dropProject(project.id);
|
|
1533
|
-
|
|
1634
|
+
const origin = auditOriginOf(options);
|
|
1635
|
+
store.auditRoot({
|
|
1636
|
+
actor: origin.actor,
|
|
1637
|
+
action: "project/drop",
|
|
1638
|
+
target: project.id,
|
|
1639
|
+
sessionId: origin.sessionId,
|
|
1640
|
+
detail: {
|
|
1641
|
+
trashedTo: result.trashedTo,
|
|
1642
|
+
bytes: impact.bytes,
|
|
1643
|
+
files: impact.files,
|
|
1644
|
+
recoverable: true,
|
|
1645
|
+
},
|
|
1646
|
+
});
|
|
1647
|
+
return {
|
|
1648
|
+
trashedTo: result.trashedTo,
|
|
1649
|
+
project: result.project,
|
|
1650
|
+
bytes: impact.bytes,
|
|
1651
|
+
files: impact.files,
|
|
1652
|
+
};
|
|
1653
|
+
},
|
|
1654
|
+
|
|
1655
|
+
/**
|
|
1656
|
+
* 列回收站(ADR 0002「影响」条:回收站要能查看/清空)。
|
|
1657
|
+
* @returns {Promise<Record<string, any>[]>}
|
|
1658
|
+
*/
|
|
1659
|
+
async listTrash() {
|
|
1660
|
+
return store.scanTrash().entries;
|
|
1661
|
+
},
|
|
1662
|
+
|
|
1663
|
+
/**
|
|
1664
|
+
* 从回收站恢复一个项目。
|
|
1665
|
+
* @param {string} name
|
|
1666
|
+
* @param {{ actor?: unknown, sessionId?: unknown }} [options] 来源(复审 A2)
|
|
1667
|
+
* @returns {Promise<Record<string, any>>}
|
|
1668
|
+
*/
|
|
1669
|
+
async restoreTrash(name, options = {}) {
|
|
1670
|
+
const origin = auditOriginOf(options);
|
|
1671
|
+
/** @type {{ project: Record<string, any>, restoredTo: string }} */
|
|
1672
|
+
let result;
|
|
1673
|
+
try {
|
|
1674
|
+
result = store.restoreTrash(name);
|
|
1675
|
+
} catch (error) {
|
|
1676
|
+
// 坏条目/不一致条目会被拒绝且**留在回收站**;失败也留痕,别让审计链断在这里(F3)。
|
|
1677
|
+
// 审计本身写不进去(只读/写满的数据根)时只告警:不能把 not-found 顶成 EACCES(N3)。
|
|
1678
|
+
try {
|
|
1679
|
+
store.auditRoot({
|
|
1680
|
+
actor: origin.actor,
|
|
1681
|
+
action: "project/restore-failed",
|
|
1682
|
+
target: String(name ?? ""),
|
|
1683
|
+
sessionId: origin.sessionId,
|
|
1684
|
+
detail: {
|
|
1685
|
+
code: String(/** @type {any} */ (error)?.code ?? "unknown"),
|
|
1686
|
+
message: String(/** @type {any} */ (error)?.message ?? error).slice(0, 300),
|
|
1687
|
+
},
|
|
1688
|
+
});
|
|
1689
|
+
} catch (auditError) {
|
|
1690
|
+
store.logger.warn?.(`casefile: 恢复失败审计写入失败:${String(auditError)}`);
|
|
1691
|
+
}
|
|
1692
|
+
throw error;
|
|
1693
|
+
}
|
|
1694
|
+
store.auditRoot({
|
|
1695
|
+
actor: origin.actor,
|
|
1696
|
+
action: "project/restore",
|
|
1697
|
+
target: String(name ?? ""),
|
|
1698
|
+
sessionId: origin.sessionId,
|
|
1699
|
+
detail: { projectId: result.project.id, restoredTo: result.restoredTo },
|
|
1700
|
+
});
|
|
1701
|
+
return {
|
|
1702
|
+
project: result.project,
|
|
1703
|
+
restoredTo: result.restoredTo,
|
|
1704
|
+
summary: `已恢复项目 ${result.project.id}`,
|
|
1705
|
+
};
|
|
1706
|
+
},
|
|
1707
|
+
|
|
1708
|
+
/**
|
|
1709
|
+
* 清空回收站(显式硬删):必须 confirm;dry-run 只给影响面;来历不明的条目保留。
|
|
1710
|
+
* @param {{ names?: string[], confirm?: boolean, dryRun?: boolean,
|
|
1711
|
+
* actor?: unknown, sessionId?: unknown }} [options]
|
|
1712
|
+
* @returns {Promise<Record<string, any>>}
|
|
1713
|
+
*/
|
|
1714
|
+
async purgeTrash(options = {}) {
|
|
1715
|
+
const scan = store.scanTrash();
|
|
1716
|
+
const names = Array.isArray(options?.names) ? options.names.map(String) : null;
|
|
1717
|
+
const selected =
|
|
1718
|
+
names == null ? scan.entries : scan.entries.filter((entry) => names.includes(entry.name));
|
|
1719
|
+
if (names != null && selected.length === 0) {
|
|
1720
|
+
throw new CaseStoreError("not-found", `回收站里没有这些条目:${names.join("、")}`);
|
|
1721
|
+
}
|
|
1722
|
+
const bytes = selected.reduce((sum, entry) => sum + (Number(entry.bytes) || 0), 0);
|
|
1723
|
+
/** @type {Record<string, any>} */
|
|
1724
|
+
const impact = {
|
|
1725
|
+
count: selected.length,
|
|
1726
|
+
bytes,
|
|
1727
|
+
names: selected.map((entry) => entry.name),
|
|
1728
|
+
skipped: scan.unknown.length,
|
|
1729
|
+
trashRoot: store.trashRoot,
|
|
1730
|
+
recoverable: false,
|
|
1731
|
+
};
|
|
1732
|
+
if (options?.dryRun === true) return { dryRun: true, ...impact };
|
|
1733
|
+
if (options?.confirm !== true) {
|
|
1734
|
+
throw new CasefileActionError(
|
|
1735
|
+
"confirm-required",
|
|
1736
|
+
`清空回收站会硬删 ${impact.count} 个条目(${impact.bytes} 字节,不可恢复);来历不明的条目会保留。确认请传 confirm: true。`,
|
|
1737
|
+
);
|
|
1738
|
+
}
|
|
1739
|
+
const result = store.purgeTrash(names ?? undefined);
|
|
1740
|
+
const origin = auditOriginOf(options);
|
|
1741
|
+
store.auditRoot({
|
|
1742
|
+
actor: origin.actor,
|
|
1743
|
+
action: "trash/purge",
|
|
1744
|
+
target: result.names.join(","),
|
|
1745
|
+
sessionId: origin.sessionId,
|
|
1746
|
+
detail: { purged: result.purged, bytes: result.bytes, skipped: result.skipped },
|
|
1747
|
+
});
|
|
1748
|
+
return {
|
|
1749
|
+
...result,
|
|
1750
|
+
...impact,
|
|
1751
|
+
count: result.purged,
|
|
1752
|
+
summary: `已清空 ${result.purged} 个回收站条目(${result.bytes} 字节,不可恢复)`,
|
|
1753
|
+
};
|
|
1534
1754
|
},
|
|
1535
1755
|
|
|
1536
1756
|
/**
|
|
@@ -1587,15 +1807,12 @@ export function createActions(options) {
|
|
|
1587
1807
|
return { project: next, coverage: preview.coverage };
|
|
1588
1808
|
},
|
|
1589
1809
|
|
|
1590
|
-
/**
|
|
1591
|
-
* 导出验收报告(md + json)。
|
|
1592
|
-
* @param {string} projectId
|
|
1593
|
-
* @returns {Promise<Record<string, any>>}
|
|
1594
|
-
*/
|
|
1595
1810
|
/**
|
|
1596
1811
|
* 导出报告。
|
|
1812
|
+
* 与 brief/bundle 共用脱敏(redactPack),默认 true:报告自称"可外发归档",
|
|
1813
|
+
* 就不能把本机绝对路径与目标标识原样带出去(AUD-CASEFILE-04)。
|
|
1597
1814
|
* @param {string} projectId
|
|
1598
|
-
* @param {{ format?: "md" | "html" | "both" }} [options]
|
|
1815
|
+
* @param {{ format?: "md" | "html" | "both", redact?: boolean, redactOptions?: Record<string, any> }} [options]
|
|
1599
1816
|
* @returns {Promise<Record<string, any>>}
|
|
1600
1817
|
*/
|
|
1601
1818
|
async exportReport(projectId, options = {}) {
|
|
@@ -1603,6 +1820,7 @@ export function createActions(options) {
|
|
|
1603
1820
|
if (!["md", "html", "both"].includes(format)) {
|
|
1604
1821
|
throw new CasefileInputError("invalid-input", `format 只能是 md / html / both:${format}`);
|
|
1605
1822
|
}
|
|
1823
|
+
const redacted = options?.redact !== false;
|
|
1606
1824
|
const project = requireProject(projectId);
|
|
1607
1825
|
const data = store.readAll(project.id);
|
|
1608
1826
|
const current = latestBuild(data.builds);
|
|
@@ -1612,7 +1830,21 @@ export function createActions(options) {
|
|
|
1612
1830
|
checks: data.checks,
|
|
1613
1831
|
openObligations: coverage.open,
|
|
1614
1832
|
});
|
|
1615
|
-
const
|
|
1833
|
+
const built = buildReport({
|
|
1834
|
+
project,
|
|
1835
|
+
data,
|
|
1836
|
+
coverage,
|
|
1837
|
+
stop,
|
|
1838
|
+
redaction: { redacted },
|
|
1839
|
+
});
|
|
1840
|
+
const report = redacted
|
|
1841
|
+
? redactPack(built, {
|
|
1842
|
+
home: process.env.HOME ?? process.env.USERPROFILE,
|
|
1843
|
+
cwd: process.cwd(),
|
|
1844
|
+
extraPaths: [dataRoot],
|
|
1845
|
+
...(options?.redactOptions ?? {}),
|
|
1846
|
+
})
|
|
1847
|
+
: built;
|
|
1616
1848
|
const stamp = String(report.generatedAt).replace(/[:.]/g, "-");
|
|
1617
1849
|
const dir = join(store.projectDir(project.id), "reports", stamp);
|
|
1618
1850
|
mkdirSync(dir, { recursive: true });
|
|
@@ -1637,13 +1869,15 @@ export function createActions(options) {
|
|
|
1637
1869
|
detail: {
|
|
1638
1870
|
coverage: coverage.ratio.toFixed(2),
|
|
1639
1871
|
digest: shortDigest(current?.implementationDigest),
|
|
1872
|
+
redacted,
|
|
1640
1873
|
},
|
|
1641
1874
|
});
|
|
1642
1875
|
return {
|
|
1643
1876
|
path: dir,
|
|
1644
1877
|
files,
|
|
1645
1878
|
coverage,
|
|
1646
|
-
|
|
1879
|
+
redacted,
|
|
1880
|
+
summary: `报告已写入 ${dir}(覆盖率 ${(coverage.ratio * 100).toFixed(0)}%,${redacted ? "已脱敏" : "未脱敏"})`,
|
|
1647
1881
|
};
|
|
1648
1882
|
},
|
|
1649
1883
|
|