@kangyishuai/dsh-casefile 0.8.1 → 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/lib/service.js CHANGED
@@ -77,6 +77,7 @@ function fail(error) {
77
77
  "coverage-not-met",
78
78
  "builder-unavailable",
79
79
  "auth-tier-blocked",
80
+ "artifact-too-large",
80
81
  ]);
81
82
  if (BUSINESS_CODES.has(code)) {
82
83
  throw new RemoteError("gateway/bad-request", message, {});
@@ -95,7 +96,11 @@ export class CasefileService extends TypertRemoteService {
95
96
  this.config = config ?? {};
96
97
  this.logger = ctx?.logger ?? console;
97
98
  this.dataRoot = resolveDataRoot(this.config);
98
- this.store = new CaseStore({ dataRoot: this.dataRoot, logger: this.logger });
99
+ this.store = new CaseStore({
100
+ dataRoot: this.dataRoot,
101
+ logger: this.logger,
102
+ maxArtifactBytes: this.config.maxArtifactBytes,
103
+ });
99
104
  /** 会话 → 最近打开的项目(工具省略 projectId 时用)。 @type {Map<string, string>} */
100
105
  this.currentProject = new Map();
101
106
  this.accessWait = null;
@@ -314,6 +319,8 @@ export class CasefileService extends TypertRemoteService {
314
319
  toolHandlers() {
315
320
  return {
316
321
  openProject: (input, exec) => this.actions.openProject(input, exec),
322
+ rememberProject: (exec, projectId) => this.actions.remember(exec, projectId),
323
+ lastOpenedProjectId: () => this.store.lastOpenedProjectId(),
317
324
  listProjects: () => this.actions.listProjects(),
318
325
  getProject: (projectId) => this.actions.detail(projectId),
319
326
  updateProject: (projectId, patch) => this.actions.updateProject(projectId, patch),
@@ -343,6 +350,9 @@ export class CasefileService extends TypertRemoteService {
343
350
  inspectApp: (input) => this.actions.inspectAppBundle(input),
344
351
  archiveProject: (projectId, options) => this.actions.archiveProject(projectId, options),
345
352
  dropProject: (projectId, options) => this.actions.dropProject(projectId, options),
353
+ listTrash: () => this.actions.listTrash(),
354
+ restoreTrash: (name, options) => this.actions.restoreTrash(name, options),
355
+ purgeTrash: (options) => this.actions.purgeTrash(options),
346
356
  runCycle: (projectId, input) => this.actions.runCycle(projectId, input),
347
357
  isBuilderSession: (sessionId) =>
348
358
  this.store.listBuilderSessions().has(String(sessionId ?? "")),
@@ -622,7 +632,46 @@ export class CasefileService extends TypertRemoteService {
622
632
  */
623
633
  async dropProject(projectId, options) {
624
634
  try {
625
- return await this.actions.dropProject(projectId, options ?? {});
635
+ // UI 通道(Remote)显式标 actor=user:工具通道在 tools.js 里标 agent(复审 A2)。
636
+ return await this.actions.dropProject(projectId, { ...(options ?? {}), actor: "user" });
637
+ } catch (error) {
638
+ fail(error);
639
+ }
640
+ }
641
+
642
+ /**
643
+ * 回收站列表(ADR 0002「影响」条)。
644
+ * @returns {Promise<Record<string, any>[]>}
645
+ */
646
+ async listTrash() {
647
+ try {
648
+ return await this.actions.listTrash();
649
+ } catch (error) {
650
+ fail(error);
651
+ }
652
+ }
653
+
654
+ /**
655
+ * 从回收站恢复一个条目。
656
+ * @param {string} name 回收站条目名
657
+ * @returns {Promise<Record<string, any>>}
658
+ */
659
+ async restoreTrashEntry(name) {
660
+ try {
661
+ return await this.actions.restoreTrash(name, { actor: "user" });
662
+ } catch (error) {
663
+ fail(error);
664
+ }
665
+ }
666
+
667
+ /**
668
+ * 清空回收站(显式硬删,需 confirm;支持 dryRun)。
669
+ * @param {{ names?: string[], confirm?: boolean, dryRun?: boolean }} [options]
670
+ * @returns {Promise<Record<string, any>>}
671
+ */
672
+ async purgeTrash(options) {
673
+ try {
674
+ return await this.actions.purgeTrash({ ...(options ?? {}), actor: "user" });
626
675
  } catch (error) {
627
676
  fail(error);
628
677
  }
@@ -905,6 +954,9 @@ declareRemote(CasefileService, "inspectApp");
905
954
  declareRemote(CasefileService, "provenanceInfo");
906
955
  declareRemote(CasefileService, "archiveProject");
907
956
  declareRemote(CasefileService, "dropProject");
957
+ declareRemote(CasefileService, "listTrash");
958
+ declareRemote(CasefileService, "restoreTrashEntry");
959
+ declareRemote(CasefileService, "purgeTrash");
908
960
  declareRemote(CasefileService, "awaitLogin");
909
961
  declareRemote(CasefileService, "runCycle");
910
962
  declareRemote(CasefileService, "closeProject");
package/lib/tools.js CHANGED
@@ -1,16 +1,22 @@
1
1
  // @ts-check
2
- // tools.js:模型可调用的 6 个 casefile 工具。
2
+ // tools.js:模型可调用的 12 个 casefile 工具。
3
3
  //
4
4
  // 设计(spec/02-architecture.md §5.2):
5
5
  // - 工具面控制在个位数;长尾(反编译、抓包、对比)走 REA 自己的工具;
6
6
  // - 统一输出 { ok, data | error{code,message,hint}, summary }:错误不抛异常,
7
7
  // 让模型读到稳定错误码并自行纠正;
8
8
  // - 每个工具都接受 projectId;省略时用「本会话最近打开的项目」(由 service 维护)。
9
+ //
10
+ // 工具面代价(AUD-CASEFILE-10):12 个工具的定义(description+parameters JSON)合计约 13.6 KB
11
+ // ≈ 3.4K tokens,常驻每个会话。
12
+ // 这是已知取舍而非免费:低频能力(case_access/case_app/case_ingest)保留独立工具是为了
13
+ // 每个工具的 schema 都能自解释;如果未来要压缩,优先把这三者并进一个 case_misc。
9
14
 
10
15
  import { describeIngestSchema } from "./core/ingest.js";
16
+ import { CLAIM_KINDS } from "./core/model.js";
11
17
  import { renderToolResult, TOOL_OUTPUT_SCHEMA } from "./tool-schema.js";
12
18
 
13
- /** 六个工具名(文档、测试、冒烟共用)。 */
19
+ /** 十二个工具名(文档、测试、冒烟共用)。 */
14
20
  export const TOOL_NAMES = /** @type {const} */ ({
15
21
  project: "case_project",
16
22
  evidence: "case_evidence",
@@ -42,12 +48,13 @@ export function toToolError(error) {
42
48
  const message = String(anyError?.message ?? error ?? "unknown error");
43
49
  /** @type {Record<string, string>} */
44
50
  const hints = {
45
- "project-not-found": "先调用 case_project 的 open 或 list 确认项目 id。",
51
+ "project-not-found": "先调用 case_project 的 open/list 确认项目 id,或显式传 projectId。",
46
52
  "project-exists": "换个标题,或直接 open 已有项目。",
47
53
  "invalid-input": "按参数 description 修正字段。",
48
54
  "read-only-newer": "项目文件由更新的 casefile 写入;升级插件后再试。",
49
55
  "no-data-root": "检查插件 Config 里的 dataRoot。",
50
56
  "bad-project-id": "项目 id 必须是小写 kebab-case。",
57
+ "artifact-too-large": "产物超过归档上限:先裁剪,或调大插件 Config 的 maxArtifactBytes。",
51
58
  };
52
59
  return { ok: false, error: { code, message, ...(hints[code] ? { hint: hints[code] } : {}) } };
53
60
  }
@@ -117,6 +124,20 @@ export function buildToolDefinitions(handlers, projectOf) {
117
124
  }
118
125
  };
119
126
 
127
+ /**
128
+ * 危险操作(drop / restore / purge)的审计来源:工具路径由模型发起,记 `agent` 并把会话 id
129
+ * 作为来源证据(复审 A2)。UI 路径走 Remote,在 service.js 里显式记 `user`。
130
+ * @param {any} exec
131
+ * @returns {{ actor: "agent", sessionId: string | null }}
132
+ */
133
+ const auditOrigin = (exec) => {
134
+ const id = exec?.agent?.id;
135
+ return {
136
+ actor: "agent",
137
+ sessionId: typeof id === "string" && id.trim() !== "" ? id.trim() : null,
138
+ };
139
+ };
140
+
120
141
  /**
121
142
  * @param {any} args
122
143
  * @param {any} exec
@@ -126,9 +147,18 @@ export function buildToolDefinitions(handlers, projectOf) {
126
147
  const explicit = typeof args.projectId === "string" && args.projectId !== "";
127
148
  const id = explicit ? args.projectId : projectOf(exec);
128
149
  if (typeof id !== "string" || id === "") {
129
- throw Object.assign(new Error("还没有指定项目:先 case_project open,或显式传 projectId"), {
130
- code: "project-not-found",
131
- });
150
+ const lastOpened =
151
+ typeof handlers.lastOpenedProjectId === "function" ? handlers.lastOpenedProjectId() : null;
152
+ throw Object.assign(
153
+ new Error(
154
+ `还没有指定项目:先 case_project open,或显式传 projectId${lastOpened ? `(上次打开的是 ${lastOpened})` : ""}`,
155
+ ),
156
+ { code: "project-not-found" },
157
+ );
158
+ }
159
+ // 任何显式 projectId 调用都建立/刷新会话指针:get 之后不带 projectId 也能继续写(AUD-CASEFILE-01)。
160
+ if (explicit && typeof handlers.rememberProject === "function") {
161
+ handlers.rememberProject(exec, id);
132
162
  }
133
163
  return id;
134
164
  };
@@ -137,7 +167,7 @@ export function buildToolDefinitions(handlers, projectOf) {
137
167
  {
138
168
  name: TOOL_NAMES.project,
139
169
  description:
140
- "casefile 项目:open 新建/打开调查目标,list 列出,get 看详情,update 改授权档/范围/状态。",
170
+ "casefile 项目:open 新建或打开已有项目,list 列出,get 看详情,update 改授权档/范围/状态;trash/restore/purge 管理回收站。drop 会移动整个项目目录:只能在用户明确同意后调用,不要自行 drop 项目(面板的「危险操作 → 移入回收站」是给用户自己点的路径)。",
141
171
  parameters: {
142
172
  type: "object",
143
173
  additionalProperties: false,
@@ -154,12 +184,20 @@ export function buildToolDefinitions(handlers, projectOf) {
154
184
  "overview",
155
185
  "archive",
156
186
  "drop",
187
+ "autonomy",
188
+ "bundle",
189
+ "trash",
190
+ "restore",
191
+ "purge",
157
192
  ],
158
193
  description: "要执行的动作",
159
194
  },
160
195
  projectId: PROJECT_ID,
161
196
  title: { type: "string", description: "open 时必填:项目标题" },
162
- id: { type: "string", description: "open 时可指定 kebab-case 项目 id" },
197
+ id: {
198
+ type: "string",
199
+ description: "open 时可指定 kebab-case 项目 id;id 命中已有项目时直接打开它",
200
+ },
163
201
  target: {
164
202
  type: "object",
165
203
  additionalProperties: false,
@@ -178,7 +216,8 @@ export function buildToolDefinitions(handlers, projectOf) {
178
216
  format: {
179
217
  type: "string",
180
218
  enum: ["md", "html", "both"],
181
- description: "report 时的输出格式:md(默认)/ html(单文件、可外发归档)/ both",
219
+ description:
220
+ "report 时的输出格式:md(默认)/ html(单文件、可外发归档;默认脱敏,传 redact:false 关闭)/ both",
182
221
  },
183
222
  level: {
184
223
  type: "string",
@@ -197,11 +236,26 @@ export function buildToolDefinitions(handlers, projectOf) {
197
236
  },
198
237
  redact: {
199
238
  type: "boolean",
200
- description: "bundle 时是否脱敏(默认 true)",
239
+ description: "bundle/report 时是否脱敏(默认 true)",
240
+ },
241
+ dryRun: {
242
+ type: "boolean",
243
+ description:
244
+ "drop 时只给影响面(项目数/字节/previewTrashPath;预览路径不是最终落点),purge 时给条目数/字节/trashRoot;都不实际执行",
245
+ },
246
+ trashName: {
247
+ type: "string",
248
+ description: "restore 时必填:回收站条目名(case_project trash 列出的 name)",
249
+ },
250
+ names: {
251
+ type: "array",
252
+ items: { type: "string" },
253
+ description: "purge 时可选:只清这些回收站条目;省略 = 全部认识的条目",
201
254
  },
202
255
  confirm: {
203
256
  type: "boolean",
204
- description: "drop 时必须传 true:把整个项目目录移进 trash(可手动移回)",
257
+ description:
258
+ "drop/purge 时必须传 true:drop 把整个项目目录移进 trash(可在回收站恢复);purge 硬删回收站条目(不可恢复)。drop 前必须先得到用户明确同意(用户原话要求丢弃该项目),不得自行决定。",
205
259
  },
206
260
  },
207
261
  required: ["action"],
@@ -226,7 +280,12 @@ export function buildToolDefinitions(handlers, projectOf) {
226
280
  },
227
281
  exec,
228
282
  );
229
- return { project, summary: `已打开项目 ${project.id}(${project.title})` };
283
+ return {
284
+ project,
285
+ summary: project.reopened
286
+ ? `已打开已有项目 ${project.id}(${project.title})`
287
+ : `已打开项目 ${project.id}(${project.title})`,
288
+ };
230
289
  }
231
290
  case "list": {
232
291
  const projects = await handlers.listProjects();
@@ -294,19 +353,51 @@ export function buildToolDefinitions(handlers, projectOf) {
294
353
  case "drop": {
295
354
  const dropped = await handlers.dropProject(projectId(args, exec), {
296
355
  confirm: args.confirm,
356
+ dryRun: args.dryRun,
357
+ ...auditOrigin(exec),
297
358
  });
298
359
  return {
299
- trashedTo: dropped.trashedTo,
300
- summary: `项目已移入 trash:${dropped.trashedTo}`,
360
+ ...dropped,
361
+ summary:
362
+ dropped.dryRun === true
363
+ ? dropped.summary
364
+ : `项目已移入 trash:${dropped.trashedTo}(可在回收站恢复)`,
365
+ };
366
+ }
367
+ case "trash": {
368
+ const entries = await handlers.listTrash();
369
+ return { trash: entries, summary: `回收站 ${entries.length} 个条目` };
370
+ }
371
+ case "restore": {
372
+ const name = typeof args.trashName === "string" ? args.trashName : "";
373
+ if (name === "") {
374
+ throw Object.assign(new Error("restore 需要 trashName(trash 列出的 name)"), {
375
+ code: "invalid-input",
376
+ });
377
+ }
378
+ const restored = await handlers.restoreTrash(name, auditOrigin(exec));
379
+ return {
380
+ ...restored,
381
+ summary: `已恢复项目 ${restored.project.id}`,
301
382
  };
302
383
  }
384
+ case "purge": {
385
+ const purged = await handlers.purgeTrash({
386
+ names: args.names,
387
+ confirm: args.confirm,
388
+ dryRun: args.dryRun,
389
+ ...auditOrigin(exec),
390
+ });
391
+ return purged;
392
+ }
303
393
  case "report": {
304
394
  const exported = await handlers.exportReport(projectId(args, exec), {
305
395
  format: args.format,
396
+ redact: args.redact !== false,
306
397
  });
307
398
  return {
308
399
  ...exported,
309
- summary: `报告已写入 ${exported.path}(${(exported.files ?? []).join(" / ")})`,
400
+ summary: `报告已写入 ${exported.path}(${(exported.files ?? []).join(" / ")},${exported.redacted === false ? "未脱敏" : "已脱敏"})`,
310
401
  };
311
402
  }
312
403
  default:
@@ -333,7 +424,12 @@ export function buildToolDefinitions(handlers, projectOf) {
333
424
  type: "object",
334
425
  additionalProperties: false,
335
426
  properties: {
336
- kind: { type: "string" },
427
+ kind: {
428
+ type: "string",
429
+ enum: [...CLAIM_KINDS],
430
+ description:
431
+ "证据种类(与落库枚举同源);叫不出名字的类型用 other,原始值会记进 kindRaw",
432
+ },
337
433
  sourceTool: {
338
434
  type: "string",
339
435
  description:
package/locale/en.json CHANGED
@@ -25,6 +25,16 @@
25
25
  "blocker.openUnknowns": "unresolved unknowns",
26
26
  "blocker.failedObligations": "failed obligations",
27
27
  "blocker.blockedObligations": "blocked obligations",
28
- "blocker.coverageBelow": "coverage below target"
28
+ "blocker.coverageBelow": "coverage below target",
29
+ "trash.toggle": "Trash",
30
+ "trash.title": "Trash (restorable)",
31
+ "trash.empty": "The trash is empty.",
32
+ "trash.meta": "{id} · {files} files · {bytes}",
33
+ "trash.restore": "Restore",
34
+ "trash.back": "Back to projects",
35
+ "trash.purge": "Empty trash",
36
+ "trash.purgePreview": "Permanently deletes {count} entries ({bytes}); {skipped} unknown entries are kept.",
37
+ "trash.purgeConfirm": "Empty it",
38
+ "trash.cancel": "Cancel"
29
39
  }
30
40
  }
package/locale/zh.json CHANGED
@@ -25,6 +25,16 @@
25
25
  "blocker.openUnknowns": "还有未解决未知项",
26
26
  "blocker.failedObligations": "有失败的义务",
27
27
  "blocker.blockedObligations": "有阻塞的义务",
28
- "blocker.coverageBelow": "覆盖率不足"
28
+ "blocker.coverageBelow": "覆盖率不足",
29
+ "trash.toggle": "回收站",
30
+ "trash.title": "回收站(可恢复)",
31
+ "trash.empty": "回收站是空的。",
32
+ "trash.meta": "{id} · {files} 个文件 · {bytes}",
33
+ "trash.restore": "恢复",
34
+ "trash.back": "返回项目列表",
35
+ "trash.purge": "清空回收站",
36
+ "trash.purgePreview": "将硬删 {count} 个条目({bytes},不可恢复);{skipped} 个来历不明的条目会保留。",
37
+ "trash.purgeConfirm": "确认清空",
38
+ "trash.cancel": "取消"
29
39
  }
30
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kangyishuai/dsh-casefile",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "Evidence-first reverse-engineering workbench on top of REA: observations become citable evidence, a handoff spec pack, and recomputable acceptance checks. No bridge, no engine.",
5
5
  "keywords": [
6
6
  "dsh",