@kangyishuai/dsh-session-delete 0.4.9 → 0.6.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 +46 -33
- package/README.zh-CN.md +39 -26
- package/lib/agent-tool.js +39 -3
- package/lib/audit.js +1 -1
- package/lib/client.js +2484 -800
- package/lib/operations.js +725 -152
- package/lib/plan.js +24 -0
- package/lib/preferences.js +90 -0
- package/lib/recovery.js +477 -0
- package/lib/refs.js +217 -6
- package/lib/restore.js +187 -0
- package/lib/service.js +165 -0
- package/lib/trash.js +157 -14
- package/lib/workspace-state.js +63 -0
- package/locale/en.json +91 -44
- package/locale/zh.json +92 -45
- package/package.json +4 -2
package/lib/refs.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// tier 决定它在 plan 里被标成「可自动处理」还是「未知,仅报告」。
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { trashRoot } from "./paths.js";
|
|
6
|
+
import { readPinnedFlag } from "./workspace-state.js";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @typedef {"api" | "internal" | "unknown"} RefTier
|
|
@@ -11,13 +12,19 @@ import { trashRoot } from "./paths.js";
|
|
|
11
12
|
* @property {string[]} [workspaceIds]
|
|
12
13
|
* @property {boolean | null} [pinned]
|
|
13
14
|
*
|
|
15
|
+
* restore 的返回值让适配器能区分「本来就不用恢复」和「恢复失败」;
|
|
16
|
+
* 返回 undefined 视作成功(向后兼容)。
|
|
17
|
+
* @typedef {object} RefRestoreResult
|
|
18
|
+
* @property {"ok" | "skipped" | "failed"} [status]
|
|
19
|
+
* @property {string} [detail]
|
|
20
|
+
*
|
|
14
21
|
* @typedef {object} RefAdapter
|
|
15
22
|
* @property {string} id
|
|
16
23
|
* @property {string} label
|
|
17
24
|
* @property {RefTier} tier
|
|
18
25
|
* @property {(sessionId: string) => Promise<RefSnapshot>} [snapshot]
|
|
19
26
|
* @property {(sessionId: string, snapshot: RefSnapshot) => Promise<void>} [remove]
|
|
20
|
-
* @property {(sessionId: string, snapshot: RefSnapshot) => Promise<void>} [restore]
|
|
27
|
+
* @property {(sessionId: string, snapshot: RefSnapshot) => Promise<void | RefRestoreResult>} [restore]
|
|
21
28
|
* @property {(sessionId: string) => Promise<{ id: string, label: string, status: "ok" | "failed" | "skipped", detail: string }>} [inspect]
|
|
22
29
|
*/
|
|
23
30
|
|
|
@@ -27,11 +34,13 @@ import { trashRoot } from "./paths.js";
|
|
|
27
34
|
*
|
|
28
35
|
* @param {import("./capability.js").Capabilities} caps
|
|
29
36
|
* @param {any} logger
|
|
37
|
+
* @param {{ dataRoot?: string }} [options] dataRoot 用于读取置顶集合(没有公开读接口)。
|
|
30
38
|
* @returns {RefAdapter[]}
|
|
31
39
|
*/
|
|
32
|
-
export function buildRefs(caps, logger) {
|
|
40
|
+
export function buildRefs(caps, logger, options = {}) {
|
|
33
41
|
const { workspace, storageDomain, sessions, agents } = caps.services;
|
|
34
42
|
const check = caps.checks;
|
|
43
|
+
const dataRoot = options.dataRoot ?? null;
|
|
35
44
|
|
|
36
45
|
/** @type {RefAdapter[]} */
|
|
37
46
|
const refs = [];
|
|
@@ -45,20 +54,65 @@ export function buildRefs(caps, logger) {
|
|
|
45
54
|
throw new Error("unarchiveSession unavailable");
|
|
46
55
|
await workspace.unarchiveSession(sessionId);
|
|
47
56
|
},
|
|
57
|
+
// 删除的第 1 步会 archive 会话(当作 agent/pre-step 闸门),恢复时把它解开。
|
|
58
|
+
// 归档标记本身不会被还原(ADR:恢复的期望是「会话回到列表」,见 AUD-SD-11 的取舍)。
|
|
59
|
+
//
|
|
60
|
+
// USIM-U3-02 修:宿主不提供该接口时**不能抛异常**。删除阶段同样是 unknown 档、
|
|
61
|
+
// 根本没归档过,恢复阶段再报「失败」只会让用户看到一条假的「1 处引用未通过」。
|
|
62
|
+
// 返回 skipped 才是事实:这一处没动过,也没有需要撤的。
|
|
63
|
+
|
|
64
|
+
// USIM-U3-02 / N6:解除归档失败意味着「会话回来了但仍可能被归档集合挡住」
|
|
65
|
+
// ——它同时是删除时的 agent/pre-step 闸门,所以必须给出下一步而不是原始英文。
|
|
66
|
+
restore: async (sessionId) => {
|
|
67
|
+
if (typeof workspace?.unarchiveSession !== "function") {
|
|
68
|
+
return { status: "skipped", detail: "当前宿主不提供 unarchiveSession,未做处理" };
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
await workspace.unarchiveSession(sessionId);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
return {
|
|
74
|
+
status: "failed",
|
|
75
|
+
detail:
|
|
76
|
+
`归档标记未能解除(${error instanceof Error ? error.message : error});` +
|
|
77
|
+
"会话可能仍在归档状态,可在侧栏的归档列表里手动取消归档",
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return { status: "ok" };
|
|
81
|
+
},
|
|
48
82
|
});
|
|
49
83
|
|
|
50
84
|
refs.push({
|
|
51
85
|
id: "workspace-pin",
|
|
52
86
|
label: "置顶标记",
|
|
53
87
|
tier: check["workspace.unpin"] ? "api" : "unknown",
|
|
88
|
+
// 置顶集合没有公开读接口:读 storages/workspace.json(只读兜底)。
|
|
89
|
+
// 读不到时返回 null = 状态未知,绝不能当成「未置顶」。
|
|
90
|
+
snapshot: async (sessionId) => {
|
|
91
|
+
if (typeof dataRoot !== "string") return { pinned: null };
|
|
92
|
+
const pinned = await readPinnedFlag(dataRoot, sessionId).catch(() => null);
|
|
93
|
+
return { pinned };
|
|
94
|
+
},
|
|
54
95
|
remove: async (sessionId) => {
|
|
55
96
|
if (typeof workspace?.unpinSession !== "function")
|
|
56
97
|
throw new Error("unpinSession unavailable");
|
|
57
98
|
await workspace.unpinSession(sessionId);
|
|
58
99
|
},
|
|
59
100
|
restore: async (sessionId, snapshot) => {
|
|
60
|
-
if (
|
|
61
|
-
if (typeof workspace?.pinSession
|
|
101
|
+
if (snapshot?.pinned !== true) return { status: "skipped", detail: "原本未置顶" };
|
|
102
|
+
if (typeof workspace?.pinSession !== "function") {
|
|
103
|
+
return { status: "skipped", detail: "当前宿主不提供 pinSession,未做处理" };
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
await workspace.pinSession(sessionId);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
return {
|
|
109
|
+
status: "failed",
|
|
110
|
+
detail:
|
|
111
|
+
`置顶没有恢复(${error instanceof Error ? error.message : error});` +
|
|
112
|
+
"可在会话右键菜单里手动置顶",
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return { status: "ok" };
|
|
62
116
|
},
|
|
63
117
|
});
|
|
64
118
|
|
|
@@ -85,12 +139,47 @@ export function buildRefs(caps, logger) {
|
|
|
85
139
|
}
|
|
86
140
|
},
|
|
87
141
|
restore: async (sessionId, snapshot) => {
|
|
88
|
-
|
|
142
|
+
/** @type {string[]} */
|
|
143
|
+
const missing = [];
|
|
144
|
+
/** @type {string[]} */
|
|
145
|
+
const failed = [];
|
|
146
|
+
const workspaceIds = snapshot?.workspaceIds ?? [];
|
|
147
|
+
if (workspaceIds.length === 0) {
|
|
148
|
+
return { status: "ok", detail: "原本不在任何工作区" };
|
|
149
|
+
}
|
|
150
|
+
for (const workspaceId of workspaceIds) {
|
|
89
151
|
const item = (workspace?.list?.() ?? []).find(
|
|
90
152
|
(/** @type {any} */ w) => String(w.id) === workspaceId,
|
|
91
153
|
);
|
|
92
|
-
if (item
|
|
154
|
+
if (!item) {
|
|
155
|
+
missing.push(workspaceId);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (typeof item.attachSession !== "function") {
|
|
159
|
+
failed.push(`${workspaceId}(宿主没有 attachSession)`);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
await item.attachSession(sessionId);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
failed.push(`${workspaceId}(${error instanceof Error ? error.message : error})`);
|
|
166
|
+
}
|
|
93
167
|
}
|
|
168
|
+
// 逐项说出是哪一处,并给下一步动作;不再 throw(throw 只会把整个引用列表
|
|
169
|
+
// 压成一句「挂回工作区失败」,用户仍然不知道点哪里修)。
|
|
170
|
+
if (failed.length > 0) {
|
|
171
|
+
return {
|
|
172
|
+
status: "failed",
|
|
173
|
+
detail: `未挂回工作区:${failed.join("、")};可在会话右键菜单「归属到项目…」里手动挂回`,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (missing.length > 0) {
|
|
177
|
+
return {
|
|
178
|
+
status: "skipped",
|
|
179
|
+
detail: `原工作区已不存在:${missing.join("、")};可在会话右键菜单「归属到项目…」里挂到别的工作区`,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return { status: "ok", detail: `已挂回 ${workspaceIds.length} 个工作区` };
|
|
94
183
|
},
|
|
95
184
|
inspect: async (sessionId) => {
|
|
96
185
|
if (typeof workspace?.list !== "function") {
|
|
@@ -173,6 +262,128 @@ export function buildRefs(caps, logger) {
|
|
|
173
262
|
return refs;
|
|
174
263
|
}
|
|
175
264
|
|
|
265
|
+
/**
|
|
266
|
+
* 记录所有引用的删除前状态(供恢复时还原)。失败的适配器只告警并跳过——
|
|
267
|
+
* 恢复阶段会把「缺快照」如实记成 skipped,而不是假装还原成功。
|
|
268
|
+
* @param {RefAdapter[]} refs
|
|
269
|
+
* @param {string} sessionId
|
|
270
|
+
* @param {any} logger
|
|
271
|
+
* @returns {Promise<Record<string, RefSnapshot>>}
|
|
272
|
+
*/
|
|
273
|
+
export async function snapshotAllRefs(refs, sessionId, logger) {
|
|
274
|
+
/** @type {Record<string, RefSnapshot>} */
|
|
275
|
+
const snapshots = {};
|
|
276
|
+
for (const ref of refs) {
|
|
277
|
+
if (typeof ref.snapshot !== "function") continue;
|
|
278
|
+
try {
|
|
279
|
+
snapshots[ref.id] = await ref.snapshot(sessionId);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
logger.warn(
|
|
282
|
+
`session-delete: snapshot ${ref.id} failed: ${error instanceof Error ? error.message : error}`,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return snapshots;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 摘掉所有还指向该会话的引用。幂等:恢复/补做会重复调用它。
|
|
291
|
+
* @param {RefAdapter[]} refs
|
|
292
|
+
* @param {string} sessionId
|
|
293
|
+
* @param {Record<string, RefSnapshot>} snapshots
|
|
294
|
+
* @returns {Promise<{ id: string, label: string, status: "ok" | "failed" | "skipped", detail?: string }[]>}
|
|
295
|
+
*/
|
|
296
|
+
export async function removeAllRefs(refs, sessionId, snapshots = {}) {
|
|
297
|
+
/** @type {{ id: string, label: string, status: "ok" | "failed" | "skipped", detail?: string }[]} */
|
|
298
|
+
const removed = [];
|
|
299
|
+
for (const ref of refs) {
|
|
300
|
+
if (typeof ref.remove !== "function" || ref.tier === "unknown") {
|
|
301
|
+
removed.push({
|
|
302
|
+
id: ref.id,
|
|
303
|
+
label: ref.label,
|
|
304
|
+
status: "skipped",
|
|
305
|
+
detail: "当前宿主不提供该接口",
|
|
306
|
+
});
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
await ref.remove(sessionId, snapshots[ref.id] ?? {});
|
|
311
|
+
removed.push({ id: ref.id, label: ref.label, status: "ok" });
|
|
312
|
+
} catch (error) {
|
|
313
|
+
removed.push({
|
|
314
|
+
id: ref.id,
|
|
315
|
+
label: ref.label,
|
|
316
|
+
status: "failed",
|
|
317
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return removed;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* 把引用恢复到删除前的状态;逐项记录结果,失败不中断其余项(会话文件已经回来了,
|
|
326
|
+
* 不能因为一个引用失败把恢复整体判死)。
|
|
327
|
+
*
|
|
328
|
+
* 三道闸门(USIM-U3-02):
|
|
329
|
+
* 1. `tier === "unknown"` 的引用**跳过**,不报失败 —— 删除阶段同样没动它(removeAllRefs
|
|
330
|
+
* 的同一判据),恢复阶段再报失败就是假警报;
|
|
331
|
+
* 2. 适配器自己在接口缺失时返回 `skipped`(见 refs.js 各 restore),不再抛异常;
|
|
332
|
+
* 3. 失败项的 detail 必须带「是哪一处 + 下一步动作」,直接进回执、审计和界面。
|
|
333
|
+
*
|
|
334
|
+
* @param {RefAdapter[]} refs
|
|
335
|
+
* @param {string} sessionId
|
|
336
|
+
* @param {RefSnapshot} snapshot
|
|
337
|
+
* @param {{ onlyIds?: string[], retried?: boolean }} [options] onlyIds 只重试这些引用(自动重试一次用)
|
|
338
|
+
* @returns {Promise<{ id: string, label: string, status: "ok" | "failed" | "skipped", detail?: string, retried?: boolean }[]>}
|
|
339
|
+
*/
|
|
340
|
+
export async function restoreAllRefs(refs, sessionId, snapshot, options = {}) {
|
|
341
|
+
const onlyIds = Array.isArray(options.onlyIds) ? new Set(options.onlyIds) : null;
|
|
342
|
+
/** @type {{ id: string, label: string, status: "ok" | "failed" | "skipped", detail?: string, retried?: boolean }[]} */
|
|
343
|
+
const results = [];
|
|
344
|
+
for (const ref of refs) {
|
|
345
|
+
if (onlyIds !== null && !onlyIds.has(ref.id)) continue;
|
|
346
|
+
if (typeof ref.restore !== "function") {
|
|
347
|
+
results.push({
|
|
348
|
+
id: ref.id,
|
|
349
|
+
label: ref.label,
|
|
350
|
+
status: "skipped",
|
|
351
|
+
detail: "该引用没有恢复动作",
|
|
352
|
+
...(options.retried === true ? { retried: true } : {}),
|
|
353
|
+
});
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (ref.tier === "unknown" && onlyIds === null) {
|
|
357
|
+
results.push({
|
|
358
|
+
id: ref.id,
|
|
359
|
+
label: ref.label,
|
|
360
|
+
status: "skipped",
|
|
361
|
+
detail: "当前宿主不提供该接口,未做处理",
|
|
362
|
+
});
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
try {
|
|
366
|
+
const outcome = await ref.restore(sessionId, snapshot);
|
|
367
|
+
results.push({
|
|
368
|
+
id: ref.id,
|
|
369
|
+
label: ref.label,
|
|
370
|
+
status: outcome?.status ?? "ok",
|
|
371
|
+
detail: outcome?.detail,
|
|
372
|
+
...(options.retried === true ? { retried: true } : {}),
|
|
373
|
+
});
|
|
374
|
+
} catch (error) {
|
|
375
|
+
results.push({
|
|
376
|
+
id: ref.id,
|
|
377
|
+
label: ref.label,
|
|
378
|
+
status: "failed",
|
|
379
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
380
|
+
...(options.retried === true ? { retried: true } : {}),
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return results;
|
|
385
|
+
}
|
|
386
|
+
|
|
176
387
|
/**
|
|
177
388
|
* 回收站自身的一致性检查(meta 与实际数据是否对得上)。
|
|
178
389
|
* @param {string} dataRoot
|
package/lib/restore.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 恢复之后的校验(AUD-SD-02):光把目录搬回来不算「恢复成功」——
|
|
3
|
+
// 还要确认会话目录真的在原处、工作区归属与置顶状态(如果原本有)真的回来了。
|
|
4
|
+
// 这些结论进回执与界面,失败必须说出来,而不是一律报成功。
|
|
5
|
+
import { stat } from "node:fs/promises";
|
|
6
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} RestoreCheck
|
|
10
|
+
* @property {string} id
|
|
11
|
+
* @property {string} label
|
|
12
|
+
* @property {"ok" | "failed" | "skipped"} status
|
|
13
|
+
* @property {string} detail
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {object} options
|
|
18
|
+
* @param {import("./refs.js").RefAdapter[]} options.refs
|
|
19
|
+
* @param {string} options.sessionId
|
|
20
|
+
* @param {string} options.restoredTo
|
|
21
|
+
* @param {{ workspaceIds?: string[], pinned?: boolean | null }} options.meta
|
|
22
|
+
* @returns {Promise<RestoreCheck[]>}
|
|
23
|
+
*/
|
|
24
|
+
export async function verifyRestored({ refs, sessionId, restoredTo, meta }) {
|
|
25
|
+
/** @type {RestoreCheck[]} */
|
|
26
|
+
const checks = [];
|
|
27
|
+
|
|
28
|
+
const dirOk = await stat(restoredTo)
|
|
29
|
+
.then((value) => value.isDirectory())
|
|
30
|
+
.catch(() => false);
|
|
31
|
+
checks.push({
|
|
32
|
+
id: "session-dir",
|
|
33
|
+
label: "会话目录",
|
|
34
|
+
status: dirOk ? "ok" : "failed",
|
|
35
|
+
detail: dirOk ? `已回到 ${restoredTo}` : `没有回到 ${restoredTo}`,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const workspaceIds = Array.isArray(meta.workspaceIds) ? meta.workspaceIds : [];
|
|
39
|
+
const slot = refs.find((ref) => ref.id === "workspace-slot");
|
|
40
|
+
if (workspaceIds.length === 0) {
|
|
41
|
+
checks.push({
|
|
42
|
+
id: "workspace-slot",
|
|
43
|
+
label: "工作区记账",
|
|
44
|
+
status: "skipped",
|
|
45
|
+
detail: "原本不在任何工作区",
|
|
46
|
+
});
|
|
47
|
+
} else if (typeof slot?.snapshot !== "function") {
|
|
48
|
+
checks.push({
|
|
49
|
+
id: "workspace-slot",
|
|
50
|
+
label: "工作区记账",
|
|
51
|
+
status: "skipped",
|
|
52
|
+
detail: "当前宿主无法读取工作区记账;可在会话右键菜单「归属到项目…」里手动挂回",
|
|
53
|
+
});
|
|
54
|
+
} else {
|
|
55
|
+
try {
|
|
56
|
+
const snapshot = await slot.snapshot(sessionId);
|
|
57
|
+
const present = new Set(snapshot?.workspaceIds ?? []);
|
|
58
|
+
const missing = workspaceIds.filter((workspaceId) => !present.has(workspaceId));
|
|
59
|
+
checks.push({
|
|
60
|
+
id: "workspace-slot",
|
|
61
|
+
label: "工作区记账",
|
|
62
|
+
status: missing.length === 0 ? "ok" : "failed",
|
|
63
|
+
detail:
|
|
64
|
+
missing.length === 0
|
|
65
|
+
? "已挂回原工作区"
|
|
66
|
+
: `未挂回:${missing.join("、")};可在会话右键菜单「归属到项目…」里手动挂回`,
|
|
67
|
+
});
|
|
68
|
+
} catch (error) {
|
|
69
|
+
checks.push({
|
|
70
|
+
id: "workspace-slot",
|
|
71
|
+
label: "工作区记账",
|
|
72
|
+
status: "skipped",
|
|
73
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const pin = refs.find((ref) => ref.id === "workspace-pin");
|
|
79
|
+
if (meta.pinned !== true) {
|
|
80
|
+
checks.push({
|
|
81
|
+
id: "workspace-pin",
|
|
82
|
+
label: "置顶标记",
|
|
83
|
+
status: "skipped",
|
|
84
|
+
detail: meta.pinned === false ? "原本未置顶" : "置顶状态未知",
|
|
85
|
+
});
|
|
86
|
+
} else if (typeof pin?.snapshot !== "function") {
|
|
87
|
+
checks.push({
|
|
88
|
+
id: "workspace-pin",
|
|
89
|
+
label: "置顶标记",
|
|
90
|
+
status: "skipped",
|
|
91
|
+
detail: "当前宿主无法读取置顶集合",
|
|
92
|
+
});
|
|
93
|
+
} else {
|
|
94
|
+
// 置顶集合只能读 storages/workspace.json,而宿主是写后落盘:刚 pin() 完就读可能还是旧值。
|
|
95
|
+
// 读两次(间隔一小会儿)再判定,避免把「尚未 flush」误报成「没恢复」。
|
|
96
|
+
/** @type {boolean | null} */
|
|
97
|
+
let pinnedState = false;
|
|
98
|
+
/** @type {string | null} */
|
|
99
|
+
let pinError = null;
|
|
100
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
101
|
+
if (attempt > 0) await delay(60);
|
|
102
|
+
try {
|
|
103
|
+
const snapshot = await pin.snapshot(sessionId);
|
|
104
|
+
if (snapshot?.pinned === true) {
|
|
105
|
+
pinnedState = true;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
pinnedState = snapshot?.pinned === null || snapshot?.pinned === undefined ? null : false;
|
|
109
|
+
if (pinnedState === null) break;
|
|
110
|
+
} catch (error) {
|
|
111
|
+
pinError = error instanceof Error ? error.message : String(error);
|
|
112
|
+
pinnedState = null;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
checks.push({
|
|
117
|
+
id: "workspace-pin",
|
|
118
|
+
label: "置顶标记",
|
|
119
|
+
status: pinnedState === true ? "ok" : pinnedState === null ? "skipped" : "failed",
|
|
120
|
+
detail:
|
|
121
|
+
pinnedState === true
|
|
122
|
+
? "已恢复置顶"
|
|
123
|
+
: pinnedState === null
|
|
124
|
+
? (pinError ?? "无法读取置顶集合(状态未知)")
|
|
125
|
+
: "置顶没有恢复",
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return checks;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @typedef {object} RestoreSummary
|
|
134
|
+
* @property {number} refs
|
|
135
|
+
* @property {number} failedRefs
|
|
136
|
+
* @property {number} failedChecks
|
|
137
|
+
* @property {boolean} partial
|
|
138
|
+
* @property {{ id: string, label: string, detail: string }[]} failures 逐项失败清单(回执、审计、界面共用)
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 一次恢复里「有没有没做到的」:引用失败或校验失败都算不完整。
|
|
143
|
+
* 失败清单带名字与 detail —— 「1 处引用未通过」这种计数对用户没有信息量(USIM-U3-05)。
|
|
144
|
+
* @param {{ id?: string, label?: string, status: string, detail?: string }[]} refResults
|
|
145
|
+
* @param {RestoreCheck[]} checks
|
|
146
|
+
* @returns {RestoreSummary}
|
|
147
|
+
*/
|
|
148
|
+
export function restoreSummary(refResults, checks) {
|
|
149
|
+
const failedRefs = refResults.filter((item) => item.status === "failed");
|
|
150
|
+
const failedChecks = checks.filter((item) => item.status === "failed");
|
|
151
|
+
/** @type {{ id: string, label: string, detail: string }[]} */
|
|
152
|
+
const failures = [];
|
|
153
|
+
for (const item of failedRefs) {
|
|
154
|
+
failures.push({
|
|
155
|
+
id: String(item.id ?? ""),
|
|
156
|
+
label: String(item.label ?? item.id ?? ""),
|
|
157
|
+
detail: String(item.detail ?? "恢复未成功,没有留下原因"),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
for (const item of failedChecks) {
|
|
161
|
+
failures.push({
|
|
162
|
+
id: item.id,
|
|
163
|
+
label: item.label,
|
|
164
|
+
detail: item.detail,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
refs: refResults.length,
|
|
169
|
+
failedRefs: failedRefs.length,
|
|
170
|
+
failedChecks: failedChecks.length,
|
|
171
|
+
partial: failedRefs.length > 0 || failedChecks.length > 0,
|
|
172
|
+
failures,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 给审计/日志用的一句话:全过就说全过,没过就**逐项列出名字与原因**。
|
|
178
|
+
* @param {RestoreSummary} summary
|
|
179
|
+
* @returns {string}
|
|
180
|
+
*/
|
|
181
|
+
export function describeRestoreSummary(summary) {
|
|
182
|
+
if (!summary.partial) {
|
|
183
|
+
return summary.refs > 0 ? `恢复完成,${summary.refs} 处引用与校验全部通过` : "恢复完成";
|
|
184
|
+
}
|
|
185
|
+
const parts = summary.failures.map((item) => `${item.label}(${item.detail})`);
|
|
186
|
+
return `恢复完成,但 ${summary.failedRefs} 处引用 / ${summary.failedChecks} 项校验未通过:${parts.join(";")}`;
|
|
187
|
+
}
|
package/lib/service.js
CHANGED
|
@@ -58,7 +58,94 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
58
58
|
this._ops = null;
|
|
59
59
|
/** @type {boolean} */
|
|
60
60
|
this.agentToolRegistered = false;
|
|
61
|
+
/** 启动对账只跑一次(宿主服务重建时 ctx.inject 可能重复触发)。 */
|
|
62
|
+
this.recoveryRequested = false;
|
|
63
|
+
/** 补跑一轮的开关:宿主晚挂载的服务在首轮对账时还探不到。 */
|
|
64
|
+
this.recoveryRetried = false;
|
|
61
65
|
this.installAgentTool();
|
|
66
|
+
this.installStartupRecovery();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 启动对账(AUD-SD-01):扫 journal 里昨天中断的操作,按落盘事实补做或回滚。
|
|
71
|
+
*
|
|
72
|
+
* 必须等宿主服务挂载后再建操作层——ops 是惰性创建的,过早创建会把能力探测结果
|
|
73
|
+
* 固化在「服务还没挂上」那一刻。恢复本身幂等,重复触发没有副作用。
|
|
74
|
+
*/
|
|
75
|
+
installStartupRecovery() {
|
|
76
|
+
const run = () => {
|
|
77
|
+
if (this.recoveryRequested) return;
|
|
78
|
+
this.recoveryRequested = true;
|
|
79
|
+
void this.ops
|
|
80
|
+
.recoverPending({ operator: "system" })
|
|
81
|
+
.then((/** @type {any} */ summary) => {
|
|
82
|
+
if (summary.examined > 0) {
|
|
83
|
+
this.ctx.logger?.info?.(
|
|
84
|
+
`session-delete: startup recovery examined ${summary.examined} record(s) ` +
|
|
85
|
+
`(${summary.completed} completed, ${summary.rolledBack} rolled back, ` +
|
|
86
|
+
`${summary.unresolved} unresolved, ${summary.failed} failed)`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
this.scheduleRecoveryRetry();
|
|
90
|
+
})
|
|
91
|
+
.catch((/** @type {any} */ error) => {
|
|
92
|
+
this.recoveryRequested = false;
|
|
93
|
+
this.ctx.logger?.warn?.(
|
|
94
|
+
"session-delete: startup recovery failed: " +
|
|
95
|
+
(error instanceof Error ? error.message : String(error)),
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
try {
|
|
100
|
+
if (typeof this.ctx?.inject === "function") {
|
|
101
|
+
this.ctx.inject(["sessionPersistence"], run);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
// 没有 ctx.inject 的老宿主:等宿主把服务挂上再跑(ops 的能力探测也才准)。
|
|
105
|
+
const timer = setTimeout(run, 2000);
|
|
106
|
+
/** @type {any} */ (timer).unref?.();
|
|
107
|
+
} catch (error) {
|
|
108
|
+
this.ctx?.logger?.warn?.(
|
|
109
|
+
"session-delete: cannot schedule startup recovery: " +
|
|
110
|
+
(error instanceof Error ? error.message : String(error)),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 启动对账补跑一轮(USIM-U3-02 的宿主侧根因):
|
|
117
|
+
* 桌面组合里 `workspaceRegistry` / `agents` 比 `sessionPersistence` 晚挂载,
|
|
118
|
+
* 首轮对账探不到它们,可能漏掉归档闸门与工作区收尾。对账本身幂等,补跑没有副作用。
|
|
119
|
+
*
|
|
120
|
+
* 覆盖范围(复审 N5):补跑只能救 **trash 现场**——「已移动但引用未摘」的记录会以
|
|
121
|
+
* state=failed 留在 journal 里,第二轮会重跑;而 restore 收尾在首轮就已经 finalize
|
|
122
|
+
* 条目,第二轮只会走「恢复此前已完成」的捷径。restore 的引用补做靠设置页
|
|
123
|
+
* `recheckRestore`(回执里的 expected 快照),不是这里。
|
|
124
|
+
*/
|
|
125
|
+
scheduleRecoveryRetry() {
|
|
126
|
+
if (this.recoveryRetried) return;
|
|
127
|
+
this.recoveryRetried = true;
|
|
128
|
+
const timer = setTimeout(() => {
|
|
129
|
+
void this.ops
|
|
130
|
+
.recoverPending({ operator: "system" })
|
|
131
|
+
.then((/** @type {any} */ summary) => {
|
|
132
|
+
if (summary.examined > 0) {
|
|
133
|
+
this.ctx.logger?.info?.(
|
|
134
|
+
`session-delete: recovery re-check examined ${summary.examined} record(s) ` +
|
|
135
|
+
`after host services settled (${summary.completed} completed, ` +
|
|
136
|
+
`${summary.rolledBack} rolled back, ${summary.unresolved} unresolved, ` +
|
|
137
|
+
`${summary.failed} failed)`,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
.catch((/** @type {any} */ error) => {
|
|
142
|
+
this.ctx.logger?.warn?.(
|
|
143
|
+
"session-delete: recovery re-check failed: " +
|
|
144
|
+
(error instanceof Error ? error.message : String(error)),
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
}, 2500);
|
|
148
|
+
/** @type {any} */ (timer).unref?.();
|
|
62
149
|
}
|
|
63
150
|
|
|
64
151
|
/**
|
|
@@ -120,6 +207,8 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
120
207
|
trash: (/** @type {string} */ sessionId, /** @type {any} */ request) =>
|
|
121
208
|
this.ops.trash(sessionId, request),
|
|
122
209
|
},
|
|
210
|
+
// AUD-SD-04:模型工具的拒绝(token / stale-plan / caller-session)也写审计。
|
|
211
|
+
onRefusal: (/** @type {any} */ event) => this.ops.recordRefusal(event),
|
|
123
212
|
});
|
|
124
213
|
tools.register(
|
|
125
214
|
defineTool({
|
|
@@ -183,6 +272,34 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
183
272
|
}
|
|
184
273
|
}
|
|
185
274
|
|
|
275
|
+
/**
|
|
276
|
+
* 界面偏好:确认框里「同时删除子会话」的初值。
|
|
277
|
+
* 存宿主侧而不是 localStorage —— Web 的 origin 带随机端口,换端口就丢。
|
|
278
|
+
*/
|
|
279
|
+
async getPrefs() {
|
|
280
|
+
try {
|
|
281
|
+
return await this.ops.getPrefs();
|
|
282
|
+
} catch (error) {
|
|
283
|
+
rethrow(error);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 合并写入界面偏好。
|
|
289
|
+
* @param {{ cascadeChildren?: boolean }} [request]
|
|
290
|
+
*/
|
|
291
|
+
async setPrefs(request) {
|
|
292
|
+
try {
|
|
293
|
+
return await this.ops.setPrefs({
|
|
294
|
+
...(typeof request?.cascadeChildren === "boolean"
|
|
295
|
+
? { cascadeChildren: request.cascadeChildren }
|
|
296
|
+
: {}),
|
|
297
|
+
});
|
|
298
|
+
} catch (error) {
|
|
299
|
+
rethrow(error);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
186
303
|
/**
|
|
187
304
|
* 只读:返回完整影响面(plan/dry-run 的数据源)。
|
|
188
305
|
* @param {{ sessionId: string }} request
|
|
@@ -195,6 +312,34 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
195
312
|
}
|
|
196
313
|
}
|
|
197
314
|
|
|
315
|
+
/**
|
|
316
|
+
* 工作区级影响面:一次返回该工作区全部会话的体积/活体/子会话与聚合 digest。
|
|
317
|
+
* @param {{ workspaceId: string }} request
|
|
318
|
+
*/
|
|
319
|
+
async planWorkspace(request) {
|
|
320
|
+
try {
|
|
321
|
+
return await this.ops.planWorkspace(request?.workspaceId);
|
|
322
|
+
} catch (error) {
|
|
323
|
+
rethrow(error);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 把整个工作区的会话逐条移入回收站(汇总部分失败)。
|
|
329
|
+
* @param {{ workspaceId: string, digest?: string, cascade?: boolean }} request
|
|
330
|
+
*/
|
|
331
|
+
async trashWorkspace(request) {
|
|
332
|
+
try {
|
|
333
|
+
return await this.ops.trashWorkspace(request?.workspaceId, {
|
|
334
|
+
digest: request?.digest,
|
|
335
|
+
cascade: request?.cascade === true,
|
|
336
|
+
operator: "ui",
|
|
337
|
+
});
|
|
338
|
+
} catch (error) {
|
|
339
|
+
rethrow(error);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
198
343
|
/**
|
|
199
344
|
* 把会话移入回收站。
|
|
200
345
|
* @param {{ sessionId: string, digest?: string, title?: string | null, cascade?: boolean }} request
|
|
@@ -232,6 +377,21 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
232
377
|
}
|
|
233
378
|
}
|
|
234
379
|
|
|
380
|
+
/**
|
|
381
|
+
* 重检一次已完成的恢复:重放失败的引用并重新校验(USIM-U3-05)。
|
|
382
|
+
* @param {{ sessionId?: string, receiptId?: string }} [request]
|
|
383
|
+
*/
|
|
384
|
+
async recheckRestore(request) {
|
|
385
|
+
try {
|
|
386
|
+
return await this.ops.recheckRestore({
|
|
387
|
+
sessionId: typeof request?.sessionId === "string" ? request.sessionId : undefined,
|
|
388
|
+
receiptId: typeof request?.receiptId === "string" ? request.receiptId : undefined,
|
|
389
|
+
});
|
|
390
|
+
} catch (error) {
|
|
391
|
+
rethrow(error);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
235
395
|
/**
|
|
236
396
|
* 彻底删除一个回收站条目。
|
|
237
397
|
* @param {{ entryId: string }} request
|
|
@@ -326,10 +486,15 @@ export class SessionDeleteService extends TypertRemoteService {
|
|
|
326
486
|
}
|
|
327
487
|
|
|
328
488
|
declareRemote(SessionDeleteService, "health");
|
|
489
|
+
declareRemote(SessionDeleteService, "getPrefs");
|
|
490
|
+
declareRemote(SessionDeleteService, "setPrefs");
|
|
329
491
|
declareRemote(SessionDeleteService, "plan");
|
|
492
|
+
declareRemote(SessionDeleteService, "planWorkspace");
|
|
493
|
+
declareRemote(SessionDeleteService, "trashWorkspace");
|
|
330
494
|
declareRemote(SessionDeleteService, "trash");
|
|
331
495
|
declareRemote(SessionDeleteService, "list");
|
|
332
496
|
declareRemote(SessionDeleteService, "restore");
|
|
497
|
+
declareRemote(SessionDeleteService, "recheckRestore");
|
|
333
498
|
declareRemote(SessionDeleteService, "purge");
|
|
334
499
|
declareRemote(SessionDeleteService, "audit");
|
|
335
500
|
declareRemote(SessionDeleteService, "restoreMany");
|