@kangyishuai/dsh-casefile 0.3.0 → 0.4.1

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/actions.js CHANGED
@@ -60,6 +60,7 @@ export class CasefileActionError extends Error {
60
60
  * currentProject?: Map<string, string>,
61
61
  * dataRoot?: string,
62
62
  * runner?: (command: string, options?: Record<string, any>) => Promise<Record<string, any>>,
63
+ * onWaitChange?: (state: Record<string, any> | null) => void,
63
64
  * sessions?: {
64
65
  * create(input: { cwd: string }): Promise<{ sessionId: string }>,
65
66
  * prompt(input: { sessionId: string, text: string }): Promise<void>,
@@ -72,6 +73,7 @@ export function createActions(options) {
72
73
  const currentProject = options.currentProject ?? new Map();
73
74
  const dataRoot = options.dataRoot ?? store.rootDir;
74
75
  const sessions = options.sessions;
76
+ const onWaitChange = options.onWaitChange;
75
77
  const checkers = createCheckers(
76
78
  /** @type {any} */ (options.runner ? { runner: options.runner } : {}),
77
79
  );
@@ -1142,6 +1144,7 @@ export function createActions(options) {
1142
1144
  */
1143
1145
  async awaitLogin(projectId, input) {
1144
1146
  const project = requireProject(projectId);
1147
+ const startedAt = nowIso();
1145
1148
  const endpoint = String(input?.endpoint ?? "").trim();
1146
1149
  if (endpoint === "") {
1147
1150
  throw new CasefileInputError(
@@ -1149,13 +1152,25 @@ export function createActions(options) {
1149
1152
  "endpoint 不能为空(形如 http://127.0.0.1:9223)",
1150
1153
  );
1151
1154
  }
1152
- const result = await awaitLogin({
1155
+ onWaitChange?.({
1156
+ active: true,
1157
+ projectId: project.id,
1153
1158
  endpoint,
1154
- urlPrefix: input?.urlPrefix,
1155
- readyJs: input?.readyJs,
1156
- pollMs: input?.pollMs,
1157
- timeoutMs: input?.timeoutMs,
1159
+ startedAt,
1160
+ hint: "请在弹出的浏览器窗口里完成登录;就绪后会自动继续。",
1158
1161
  });
1162
+ let result;
1163
+ try {
1164
+ result = await awaitLogin({
1165
+ endpoint,
1166
+ urlPrefix: input?.urlPrefix,
1167
+ readyJs: input?.readyJs,
1168
+ pollMs: input?.pollMs,
1169
+ timeoutMs: input?.timeoutMs,
1170
+ });
1171
+ } finally {
1172
+ onWaitChange?.(null);
1173
+ }
1159
1174
  store.audit(project.id, {
1160
1175
  actor: "agent",
1161
1176
  action: "access/await-login",
@@ -1167,6 +1182,42 @@ export function createActions(options) {
1167
1182
 
1168
1183
  // ---------- 覆盖率 / 关闭 / 报告(M3 收尾) ----------
1169
1184
 
1185
+ /**
1186
+ * 归档项目(非破坏性):误建或暂存的项目收起来,文件都在。
1187
+ * @param {string} projectId
1188
+ * @param {{ reason?: string }} [options]
1189
+ * @returns {Promise<Record<string, any>>}
1190
+ */
1191
+ async archiveProject(projectId, options = {}) {
1192
+ const project = store.archiveProject(projectId, options?.reason);
1193
+ store.audit(project.id, {
1194
+ actor: "agent",
1195
+ action: "project/archive",
1196
+ target: project.id,
1197
+ detail: { reason: String(options?.reason ?? "").slice(0, 200) },
1198
+ });
1199
+ return { project };
1200
+ },
1201
+
1202
+ /**
1203
+ * 丢弃项目:整个目录移进 dataRoot/trash(可手动移回)。
1204
+ * 破坏性,必须 confirm。
1205
+ * @param {string} projectId
1206
+ * @param {{ confirm?: boolean }} [options]
1207
+ * @returns {Promise<Record<string, any>>}
1208
+ */
1209
+ async dropProject(projectId, options = {}) {
1210
+ const project = requireProject(projectId);
1211
+ if (options?.confirm !== true) {
1212
+ throw new CasefileActionError(
1213
+ "confirm-required",
1214
+ `丢弃项目会把 projects/${project.id} 整个移进 trash(可手动移回)。确认请传 confirm: true。`,
1215
+ );
1216
+ }
1217
+ const result = store.dropProject(project.id);
1218
+ return { trashedTo: result.trashedTo, project: result.project };
1219
+ },
1220
+
1170
1221
  /**
1171
1222
  * 项目覆盖率与关闭闸门状态(只读预览,不写状态)。
1172
1223
  * @param {string} projectId
package/lib/client.js CHANGED
@@ -83,6 +83,9 @@ window.__ModuleLoader__.load({
83
83
  "blocker.failedObligations": "有失败的义务",
84
84
  "blocker.blockedObligations": "有阻塞的义务",
85
85
  "blocker.coverageBelow": "覆盖率不足",
86
+ "wait.title": "案卷:等待登录",
87
+ "wait.hint": "请在弹出的浏览器窗口里完成登录;就绪后会自动继续。",
88
+ "wait.elapsed": "已等待 {seconds}s",
86
89
  "blocker.implementationChanged": "实现已变化",
87
90
  "blocker.originalChanged": "原件已变化",
88
91
  "blocker.checkStale": "检查已过期",
@@ -172,6 +175,10 @@ window.__ModuleLoader__.load({
172
175
  "blocker.failedObligations": "failed obligations",
173
176
  "blocker.blockedObligations": "blocked obligations",
174
177
  "blocker.coverageBelow": "coverage below target",
178
+ "wait.title": "Casefile: waiting for sign-in",
179
+ "wait.hint":
180
+ "Finish signing in in the browser window; it continues automatically once ready.",
181
+ "wait.elapsed": "waiting {seconds}s",
175
182
  "blocker.implementationChanged": "implementation changed",
176
183
  "blocker.originalChanged": "original changed",
177
184
  "blocker.checkStale": "check is stale",
@@ -551,6 +558,81 @@ window.__ModuleLoader__.load({
551
558
  );
552
559
  }
553
560
 
561
+ /**
562
+ * 等待登录横幅:轮询 host 的 accessState。
563
+ * 只在"正在等"时出现,不阻塞任何操作——这是提醒,不是模态框。
564
+ */
565
+ /** @param {{ api: any, t: any }} props */
566
+ function LoginWaitOverlay(props) {
567
+ var ctx = props.api.ctx;
568
+ var t = props.t;
569
+ var state = useState(null);
570
+ var wait = state[0];
571
+ var setWait = state[1];
572
+ var tickState = useState(0);
573
+ var setTick = tickState[1];
574
+
575
+ useEffect(() => {
576
+ var alive = true;
577
+ var poll = () => {
578
+ call(ctx, "accessState")
579
+ .then((value) => {
580
+ if (alive) setWait(value && value.active === true ? value : null);
581
+ })
582
+ .catch(() => {
583
+ if (alive) setWait(null);
584
+ });
585
+ };
586
+ poll();
587
+ var timer = setInterval(() => {
588
+ poll();
589
+ setTick((/** @type {number} */ n) => n + 1);
590
+ }, 2000);
591
+ return () => {
592
+ alive = false;
593
+ clearInterval(timer);
594
+ };
595
+ }, [ctx]);
596
+
597
+ var seconds = 0;
598
+ var started = NaN;
599
+ if (!wait) return null;
600
+ if (typeof wait.startedAt === "string") {
601
+ started = Date.parse(wait.startedAt);
602
+ if (Number.isFinite(started))
603
+ seconds = Math.max(0, Math.round((Date.now() - started) / 1000));
604
+ }
605
+ return h(
606
+ "div",
607
+ {
608
+ className: "cf-loginwait",
609
+ "data-casefile-loginwait": wait.projectId || "1",
610
+ "data-casefile-loginwait-seconds": String(seconds),
611
+ style: {
612
+ position: "fixed",
613
+ right: 20,
614
+ bottom: 20,
615
+ zIndex: 40,
616
+ maxWidth: 380,
617
+ padding: "10px 14px",
618
+ borderRadius: 10,
619
+ background: "var(--dsw-alias-toast-bg)",
620
+ color: "var(--dsw-alias-toast-label)",
621
+ fontSize: 12,
622
+ lineHeight: 1.55,
623
+ boxShadow: "0 6px 24px rgba(0,0,0,.28)",
624
+ },
625
+ },
626
+ h("div", { style: { fontWeight: 600, marginBottom: 2 } }, text(t, "wait.title")),
627
+ h("div", null, wait.hint || text(t, "wait.hint")),
628
+ h(
629
+ "div",
630
+ { style: { marginTop: 4, opacity: 0.72 } },
631
+ text(t, "wait.elapsed", { seconds: seconds }),
632
+ ),
633
+ );
634
+ }
635
+
554
636
  // ---------- 主面板 ----------
555
637
 
556
638
  /**
@@ -1268,6 +1350,19 @@ window.__ModuleLoader__.load({
1268
1350
  ),
1269
1351
  "casefile: settings section",
1270
1352
  );
1353
+
1354
+ c.effect(
1355
+ () =>
1356
+ c.slots.inject("shell.overlay", () =>
1357
+ c.slots.register(
1358
+ { name: "shell.overlay", id: "casefile-loginwait", order: 880, locale: NS },
1359
+ function CasefileLoginWaitOverlay(/** @type {any} */ props) {
1360
+ return h(LoginWaitOverlay, Object.assign({}, props, { api: api, t: t }));
1361
+ },
1362
+ ),
1363
+ ),
1364
+ "casefile: login wait overlay",
1365
+ );
1271
1366
  };
1272
1367
 
1273
1368
  return module.exports;
package/lib/core/model.js CHANGED
@@ -71,6 +71,7 @@ export const PROJECT_STATES = /** @type {const} */ ([
71
71
  "reconstructing",
72
72
  "verifying",
73
73
  "closed",
74
+ "archived",
74
75
  ]);
75
76
 
76
77
  /** 项目 id:小写字母数字与连字符,1–64。 */
package/lib/core/store.js CHANGED
@@ -307,6 +307,48 @@ export class CaseStore {
307
307
  * @param {Record<string, any>} patch
308
308
  * @returns {Record<string, any>}
309
309
  */
310
+ /**
311
+ * 归档:状态改为 archived,文件保留。误建/待整理的项目用它收起来。
312
+ * @param {string} projectId
313
+ * @param {string} [reason]
314
+ * @returns {Record<string, any>}
315
+ */
316
+ archiveProject(projectId, reason) {
317
+ const project = this.getProject(projectId);
318
+ if (!project) throw new CaseStoreError("project-not-found", `项目不存在:${projectId}`);
319
+ const next = {
320
+ ...project,
321
+ state: "archived",
322
+ archivedAt: new Date().toISOString(),
323
+ archiveReason: reason == null ? "" : String(reason).slice(0, 500),
324
+ };
325
+ this.writeJson(join(this.projectDir(projectId), "project.json"), next);
326
+ return next;
327
+ }
328
+
329
+ /**
330
+ * 丢弃:把整个项目目录移进 `<dataRoot>/trash/`,可手动移回。
331
+ * 破坏性操作,必须显式 confirm。
332
+ * @param {string} projectId
333
+ * @returns {{ trashedTo: string, project: Record<string, any> }}
334
+ */
335
+ dropProject(projectId) {
336
+ const project = this.getProject(projectId);
337
+ if (!project) throw new CaseStoreError("project-not-found", `项目不存在:${projectId}`);
338
+ const trashRoot = join(this.dataRoot, "trash");
339
+ mkdirSync(trashRoot, { recursive: true });
340
+ const stamp = new Date().toISOString().replace(/[:.]/g, "-");
341
+ const target = join(trashRoot, `${stamp}-${projectId}`);
342
+ renameSync(this.projectDir(projectId), target);
343
+ return { trashedTo: target, project };
344
+ }
345
+
346
+ /**
347
+ * 局部更新项目字段。
348
+ * @param {string} projectId
349
+ * @param {Record<string, any>} patch
350
+ * @returns {Record<string, any>}
351
+ */
310
352
  updateProject(projectId, patch) {
311
353
  const project = this.getProject(projectId);
312
354
  if (!project) throw new CaseStoreError("project-not-found", `项目不存在:${projectId}`);
package/lib/service.js CHANGED
@@ -65,6 +65,7 @@ function fail(error) {
65
65
  "nothing-selected",
66
66
  "project-exists",
67
67
  "not-found",
68
+ "confirm-required",
68
69
  "project-not-found",
69
70
  "obligationTooTall",
70
71
  "coverage-not-met",
@@ -91,11 +92,8 @@ export class CasefileService extends TypertRemoteService {
91
92
  this.store = new CaseStore({ dataRoot: this.dataRoot, logger: this.logger });
92
93
  /** 会话 → 最近打开的项目(工具省略 projectId 时用)。 @type {Map<string, string>} */
93
94
  this.currentProject = new Map();
94
- this.actions = createActions({
95
- store: this.store,
96
- currentProject: this.currentProject,
97
- dataRoot: this.dataRoot,
98
- });
95
+ this.accessWait = null;
96
+ this.actions = createActions(this.actionOptions());
99
97
  this.installSessions();
100
98
  this.installTools();
101
99
  this.logger.info?.(`[casefile] loaded, dataRoot=${this.dataRoot}`);
@@ -120,15 +118,30 @@ export class CasefileService extends TypertRemoteService {
120
118
  /**
121
119
  * 拿到会话服务后重建 actions(builder 需要 create/prompt/wait)。
122
120
  */
121
+ /**
122
+ * createActions 的公共选项(构造函数与 installSessions 必须共用)。
123
+ * @returns {Parameters<typeof createActions>[0]}
124
+ */
125
+ actionOptions() {
126
+ return {
127
+ store: this.store,
128
+ currentProject: this.currentProject,
129
+ dataRoot: this.dataRoot,
130
+ onWaitChange: (/** @type {any} */ state) => {
131
+ this.accessWait = state;
132
+ },
133
+ };
134
+ }
135
+
123
136
  installSessions() {
124
137
  try {
125
138
  this.ctx.inject?.(/** @type {any} */ (["sessionController"]), (/** @type {any} */ scope) => {
126
139
  const controller = scope.sessionController;
127
140
  if (!controller) return;
141
+ // 注意:这里必须和构造函数用同一份选项——漏掉 onWaitChange 会让
142
+ // 等待登录状态永远不生效(实测踩过:await-login 期间 accessState 恒为 false)。
128
143
  this.actions = createActions({
129
- store: this.store,
130
- currentProject: this.currentProject,
131
- dataRoot: this.dataRoot,
144
+ ...this.actionOptions(),
132
145
  sessions: {
133
146
  create: async (input) => {
134
147
  const created = await controller.create({ cwd: input.cwd });
@@ -211,6 +224,9 @@ export class CasefileService extends TypertRemoteService {
211
224
  reopenObligation: (projectId, id, note) => this.actions.reopenObligation(projectId, id, note),
212
225
  overview: () => this.actions.overview(),
213
226
  awaitLogin: (projectId, input) => this.actions.awaitLogin(projectId, input),
227
+ accessState: () => this.accessState(),
228
+ archiveProject: (projectId, options) => this.actions.archiveProject(projectId, options),
229
+ dropProject: (projectId, options) => this.actions.dropProject(projectId, options),
214
230
  runCycle: (projectId, input) => this.actions.runCycle(projectId, input),
215
231
  isBuilderSession: (sessionId) =>
216
232
  this.store.listBuilderSessions().has(String(sessionId ?? "")),
@@ -460,6 +476,37 @@ export class CasefileService extends TypertRemoteService {
460
476
  }
461
477
  }
462
478
 
479
+ /** @returns {Promise<Record<string, any>>} */
480
+ async accessState() {
481
+ return this.accessWait == null ? { active: false } : { ...this.accessWait };
482
+ }
483
+
484
+ /**
485
+ * @param {string} projectId
486
+ * @param {Record<string, any>} [options]
487
+ * @returns {Promise<Record<string, any>>}
488
+ */
489
+ async archiveProject(projectId, options) {
490
+ try {
491
+ return await this.actions.archiveProject(projectId, options ?? {});
492
+ } catch (error) {
493
+ fail(error);
494
+ }
495
+ }
496
+
497
+ /**
498
+ * @param {string} projectId
499
+ * @param {Record<string, any>} [options]
500
+ * @returns {Promise<Record<string, any>>}
501
+ */
502
+ async dropProject(projectId, options) {
503
+ try {
504
+ return await this.actions.dropProject(projectId, options ?? {});
505
+ } catch (error) {
506
+ fail(error);
507
+ }
508
+ }
509
+
463
510
  /**
464
511
  * @param {string} projectId
465
512
  * @param {Record<string, any>} input
@@ -679,6 +726,9 @@ declareRemote(CasefileService, "listChecks");
679
726
  declareRemote(CasefileService, "closeObligation");
680
727
  declareRemote(CasefileService, "reopenObligation");
681
728
  declareRemote(CasefileService, "overview");
729
+ declareRemote(CasefileService, "accessState");
730
+ declareRemote(CasefileService, "archiveProject");
731
+ declareRemote(CasefileService, "dropProject");
682
732
  declareRemote(CasefileService, "awaitLogin");
683
733
  declareRemote(CasefileService, "runCycle");
684
734
  declareRemote(CasefileService, "closeProject");
package/lib/tools.js CHANGED
@@ -141,7 +141,17 @@ export function buildToolDefinitions(handlers, projectOf) {
141
141
  properties: {
142
142
  action: {
143
143
  type: "string",
144
- enum: ["open", "list", "get", "update", "close", "report", "overview"],
144
+ enum: [
145
+ "open",
146
+ "list",
147
+ "get",
148
+ "update",
149
+ "close",
150
+ "report",
151
+ "overview",
152
+ "archive",
153
+ "drop",
154
+ ],
145
155
  description: "要执行的动作",
146
156
  },
147
157
  projectId: PROJECT_ID,
@@ -161,6 +171,11 @@ export function buildToolDefinitions(handlers, projectOf) {
161
171
  scope: { type: "string", enum: ["all", "selected"] },
162
172
  patch: { type: "object", description: "update 时的字段(title/authTier/scope/state)" },
163
173
  minCoverage: { type: "number", description: "close 时的覆盖率下限(默认 1)" },
174
+ reason: { type: "string", description: "archive 时的原因(进审计)" },
175
+ confirm: {
176
+ type: "boolean",
177
+ description: "drop 时必须传 true:把整个项目目录移进 trash(可手动移回)",
178
+ },
164
179
  },
165
180
  required: ["action"],
166
181
  },
@@ -219,6 +234,21 @@ export function buildToolDefinitions(handlers, projectOf) {
219
234
  summary: `项目已关闭(覆盖率 ${(preview.coverage.ratio * 100).toFixed(0)}%)`,
220
235
  };
221
236
  }
237
+ case "archive": {
238
+ const archived = await handlers.archiveProject(projectId(args, exec), {
239
+ reason: args.reason,
240
+ });
241
+ return { project: archived.project, summary: `项目已归档:${archived.project.id}` };
242
+ }
243
+ case "drop": {
244
+ const dropped = await handlers.dropProject(projectId(args, exec), {
245
+ confirm: args.confirm,
246
+ });
247
+ return {
248
+ trashedTo: dropped.trashedTo,
249
+ summary: `项目已移入 trash:${dropped.trashedTo}`,
250
+ };
251
+ }
222
252
  case "report": {
223
253
  return handlers.exportReport(projectId(args, exec));
224
254
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kangyishuai/dsh-casefile",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
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",