@kangyishuai/dsh-session-delete 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +121 -2
- package/README.zh-CN.md +122 -0
- package/cordis.patch.yml +3 -0
- package/lib/audit.js +93 -0
- package/lib/capability.js +104 -0
- package/lib/client.js +1146 -0
- package/lib/index.js +7 -0
- package/lib/journal.js +136 -0
- package/lib/locate.js +129 -0
- package/lib/operations.js +534 -0
- package/lib/paths.js +148 -0
- package/lib/plan.js +185 -0
- package/lib/refs.js +192 -0
- package/lib/service.js +166 -0
- package/lib/trash.js +246 -0
- package/locale/en.json +37 -0
- package/locale/zh.json +37 -0
- package/package.json +57 -3
package/lib/trash.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 回收站:原子移动 + meta.json + 恢复 + 彻底删除。
|
|
3
|
+
// 回收站目录固定放在数据根下(同卷,rename 才是 O(1)),陌生条目一律保留并告警。
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { cp, mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { isInside, requireInside, requireSessionId, trashRoot } from "./paths.js";
|
|
8
|
+
|
|
9
|
+
/** meta.json 的格式版本;新版本读旧条目,旧版本忽略新字段。 */
|
|
10
|
+
export const TRASH_SCHEMA = 1;
|
|
11
|
+
/** 回收站里由本插件创建的条目必须带这个标记,彻底删除前会核对。 */
|
|
12
|
+
export const TRASH_PLUGIN = "@kangyishuai/dsh-session-delete";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {object} TrashMetaInput
|
|
16
|
+
* @property {string | null} [title]
|
|
17
|
+
* @property {string | null} [cwd]
|
|
18
|
+
* @property {number} [bytes]
|
|
19
|
+
* @property {boolean} [live]
|
|
20
|
+
* @property {{ id: string, title: string | null }[]} [children]
|
|
21
|
+
* @property {string[]} [workspaceIds]
|
|
22
|
+
* @property {boolean | null} [pinned]
|
|
23
|
+
* @property {string} [trashedAt]
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const ENTRY_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @typedef {object} TrashMeta
|
|
30
|
+
* @property {number} schema
|
|
31
|
+
* @property {string} entryId
|
|
32
|
+
* @property {string} sessionId
|
|
33
|
+
* @property {string | null} title
|
|
34
|
+
* @property {string | null} cwd
|
|
35
|
+
* @property {string} trashedAt
|
|
36
|
+
* @property {number} bytes
|
|
37
|
+
* @property {boolean} live
|
|
38
|
+
* @property {{ id: string, title: string | null }[]} children
|
|
39
|
+
* @property {string[]} workspaceIds
|
|
40
|
+
* @property {boolean | null} pinned
|
|
41
|
+
* @property {string} originPath
|
|
42
|
+
* @property {string} plugin
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/** @param {string} sessionId @param {number} [now] @returns {string} */
|
|
46
|
+
export function makeEntryId(sessionId, now = Date.now()) {
|
|
47
|
+
const stamp = new Date(now).toISOString().replace(/[:.]/gu, "-");
|
|
48
|
+
const salt = randomUUID().slice(0, 4);
|
|
49
|
+
return `${stamp}-${salt}-${sessionId}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** @param {string} entryId @returns {string} */
|
|
53
|
+
export function requireEntryId(entryId) {
|
|
54
|
+
if (typeof entryId !== "string" || !ENTRY_ID.test(entryId)) {
|
|
55
|
+
throw Object.assign(new Error("invalid trash entry id"), {
|
|
56
|
+
code: "session-delete/invalid-entry",
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return entryId;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** @param {string} dataRoot @param {string} entryId @returns {string} */
|
|
63
|
+
export function entryDir(dataRoot, entryId) {
|
|
64
|
+
return join(trashRoot(dataRoot), requireEntryId(entryId));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @param {string} dataRoot @param {string} entryId @returns {string} */
|
|
68
|
+
export function entryDataDir(dataRoot, entryId) {
|
|
69
|
+
return join(entryDir(dataRoot, entryId), "data");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @param {string} dataRoot @param {string} entryId @returns {string} */
|
|
73
|
+
export function entryMetaPath(dataRoot, entryId) {
|
|
74
|
+
return join(entryDir(dataRoot, entryId), "meta.json");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 原子移动目录。同卷 rename;跨卷(EXDEV)退化为 cp → 校验 → 删源。
|
|
79
|
+
* @param {string} source
|
|
80
|
+
* @param {string} target
|
|
81
|
+
* @returns {Promise<void>}
|
|
82
|
+
*/
|
|
83
|
+
async function moveDirectory(source, target) {
|
|
84
|
+
try {
|
|
85
|
+
await rename(source, target);
|
|
86
|
+
return;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
const code = /** @type {any} */ (error)?.code;
|
|
89
|
+
if (code !== "EXDEV") throw error;
|
|
90
|
+
}
|
|
91
|
+
await cp(source, target, { recursive: true, errorOnExist: true, force: false });
|
|
92
|
+
const [sourceStat, targetStat] = await Promise.all([stat(source), stat(target)]);
|
|
93
|
+
if (!targetStat.isDirectory()) throw new Error("cross-device copy did not produce a directory");
|
|
94
|
+
void sourceStat;
|
|
95
|
+
await rm(source, { recursive: true, force: false });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 把会话目录移入回收站并写 meta.json。
|
|
100
|
+
* @param {object} options
|
|
101
|
+
* @param {string} options.dataRoot
|
|
102
|
+
* @param {string} options.sessionId
|
|
103
|
+
* @param {string} options.sourceDir 会话目录(调用方已校验在 sessions 根内)。
|
|
104
|
+
* @param {TrashMetaInput} options.meta
|
|
105
|
+
* @returns {Promise<{ entryId: string, meta: TrashMeta }>}
|
|
106
|
+
*/
|
|
107
|
+
export async function moveIntoTrash({ dataRoot, sessionId, sourceDir, meta }) {
|
|
108
|
+
requireSessionId(sessionId);
|
|
109
|
+
requireInside(dataRoot, sourceDir);
|
|
110
|
+
|
|
111
|
+
const entryId = makeEntryId(sessionId);
|
|
112
|
+
const root = trashRoot(dataRoot);
|
|
113
|
+
const target = join(root, entryId);
|
|
114
|
+
requireInside(dataRoot, target);
|
|
115
|
+
if (!isInside(root, target)) throw new Error("trash entry escaped the trash root");
|
|
116
|
+
|
|
117
|
+
await mkdir(root, { recursive: true });
|
|
118
|
+
await mkdir(target, { recursive: false });
|
|
119
|
+
const dataDir = join(target, "data");
|
|
120
|
+
await moveDirectory(sourceDir, dataDir);
|
|
121
|
+
|
|
122
|
+
/** @type {TrashMeta} */
|
|
123
|
+
const full = {
|
|
124
|
+
schema: TRASH_SCHEMA,
|
|
125
|
+
entryId,
|
|
126
|
+
sessionId,
|
|
127
|
+
title: meta.title ?? null,
|
|
128
|
+
cwd: meta.cwd ?? null,
|
|
129
|
+
trashedAt: meta.trashedAt ?? new Date().toISOString(),
|
|
130
|
+
bytes: meta.bytes ?? 0,
|
|
131
|
+
live: Boolean(meta.live),
|
|
132
|
+
children: meta.children ?? [],
|
|
133
|
+
workspaceIds: meta.workspaceIds ?? [],
|
|
134
|
+
pinned: meta.pinned ?? null,
|
|
135
|
+
originPath: sourceDir,
|
|
136
|
+
plugin: TRASH_PLUGIN,
|
|
137
|
+
};
|
|
138
|
+
await writeFile(entryMetaPath(dataRoot, entryId), JSON.stringify(full, null, 2), "utf8");
|
|
139
|
+
return { entryId, meta: full };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {string} dataRoot
|
|
144
|
+
* @param {string} entryId
|
|
145
|
+
* @returns {Promise<TrashMeta>}
|
|
146
|
+
*/
|
|
147
|
+
export async function readEntry(dataRoot, entryId) {
|
|
148
|
+
const raw = await readFile(entryMetaPath(dataRoot, entryId), "utf8");
|
|
149
|
+
const parsed = JSON.parse(raw);
|
|
150
|
+
if (parsed?.plugin !== TRASH_PLUGIN || typeof parsed?.sessionId !== "string") {
|
|
151
|
+
throw Object.assign(new Error("trash entry was not created by this plugin"), {
|
|
152
|
+
code: "session-delete/foreign-entry",
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return /** @type {TrashMeta} */ (parsed);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 列出回收站条目。没有 meta.json 的陌生目录原样保留并单独返回,绝不删除。
|
|
160
|
+
* @param {string} dataRoot
|
|
161
|
+
* @returns {Promise<{ entries: TrashMeta[], unknown: string[] }>}
|
|
162
|
+
*/
|
|
163
|
+
export async function listEntries(dataRoot) {
|
|
164
|
+
const root = trashRoot(dataRoot);
|
|
165
|
+
/** @type {string[]} */
|
|
166
|
+
let names = [];
|
|
167
|
+
try {
|
|
168
|
+
names = (await readdir(root, { withFileTypes: true }))
|
|
169
|
+
.filter(
|
|
170
|
+
(entry) => entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "journal",
|
|
171
|
+
)
|
|
172
|
+
.map((entry) => entry.name);
|
|
173
|
+
} catch {
|
|
174
|
+
return { entries: [], unknown: [] };
|
|
175
|
+
}
|
|
176
|
+
/** @type {TrashMeta[]} */
|
|
177
|
+
const entries = [];
|
|
178
|
+
/** @type {string[]} */
|
|
179
|
+
const unknown = [];
|
|
180
|
+
for (const name of names) {
|
|
181
|
+
try {
|
|
182
|
+
entries.push(await readEntry(dataRoot, name));
|
|
183
|
+
} catch {
|
|
184
|
+
unknown.push(name);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
entries.sort((a, b) => b.trashedAt.localeCompare(a.trashedAt));
|
|
188
|
+
return { entries, unknown };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 从回收站恢复:把 data/ 移回原路径,成功后再清理条目目录。
|
|
193
|
+
* @param {object} options
|
|
194
|
+
* @param {string} options.dataRoot
|
|
195
|
+
* @param {string} options.entryId
|
|
196
|
+
* @param {string} [options.destinationDir] 覆盖默认的原路径(测试/迁移用)。
|
|
197
|
+
* @returns {Promise<{ meta: TrashMeta, restoredTo: string }>}
|
|
198
|
+
*/
|
|
199
|
+
export async function restoreEntry({ dataRoot, entryId, destinationDir }) {
|
|
200
|
+
const meta = await readEntry(dataRoot, entryId);
|
|
201
|
+
const destination = destinationDir ?? meta.originPath;
|
|
202
|
+
requireInside(dataRoot, destination);
|
|
203
|
+
const dataDir = entryDataDir(dataRoot, entryId);
|
|
204
|
+
requireInside(dataRoot, dataDir);
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
await stat(destination);
|
|
208
|
+
throw Object.assign(new Error("the original session directory already exists"), {
|
|
209
|
+
code: "session-delete/restore-conflict",
|
|
210
|
+
});
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (/** @type {any} */ (error)?.code === "session-delete/restore-conflict") throw error;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
216
|
+
await moveDirectory(dataDir, destination);
|
|
217
|
+
|
|
218
|
+
const historyDir = join(trashRoot(dataRoot), ".history");
|
|
219
|
+
await mkdir(historyDir, { recursive: true });
|
|
220
|
+
await writeFile(
|
|
221
|
+
join(historyDir, `${entryId}.json`),
|
|
222
|
+
JSON.stringify(
|
|
223
|
+
{ ...meta, restoredAt: new Date().toISOString(), restoredTo: destination },
|
|
224
|
+
null,
|
|
225
|
+
2,
|
|
226
|
+
),
|
|
227
|
+
"utf8",
|
|
228
|
+
);
|
|
229
|
+
await rm(entryDir(dataRoot, entryId), { recursive: true, force: true });
|
|
230
|
+
return { meta, restoredTo: destination };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 彻底删除一个回收站条目(不可恢复)。
|
|
235
|
+
* @param {object} options
|
|
236
|
+
* @param {string} options.dataRoot
|
|
237
|
+
* @param {string} options.entryId
|
|
238
|
+
* @returns {Promise<{ bytes: number }>}
|
|
239
|
+
*/
|
|
240
|
+
export async function purgeEntry({ dataRoot, entryId }) {
|
|
241
|
+
const meta = await readEntry(dataRoot, entryId);
|
|
242
|
+
const dir = entryDir(dataRoot, entryId);
|
|
243
|
+
requireInside(dataRoot, dir);
|
|
244
|
+
await rm(dir, { recursive: true, force: false });
|
|
245
|
+
return { bytes: meta.bytes ?? 0 };
|
|
246
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"sessionDelete": {
|
|
3
|
+
"menu.delete": "Delete session…",
|
|
4
|
+
"menu.trash": "Trash…",
|
|
5
|
+
"confirm.title": "Delete this session?",
|
|
6
|
+
"confirm.loading": "Reading what will be removed…",
|
|
7
|
+
"confirm.desc": "The session moves to the local trash and can be restored.",
|
|
8
|
+
"confirm.size": "Session log is about {size}",
|
|
9
|
+
"confirm.live": "This session is still open; its work is stopped first.",
|
|
10
|
+
"confirm.children": "{n} subagent session(s) come along",
|
|
11
|
+
"confirm.refs": "Will clean: {list}",
|
|
12
|
+
"confirm.trashPath": "Trash location: {path}",
|
|
13
|
+
"confirm.blocked": "Cannot delete right now: {reason}",
|
|
14
|
+
"confirm.cancel": "Cancel",
|
|
15
|
+
"confirm.confirm": "Move to trash",
|
|
16
|
+
"confirm.pending": "Moving to trash…",
|
|
17
|
+
"confirm.done": "Moved to trash",
|
|
18
|
+
"confirm.undo": "Undo",
|
|
19
|
+
"confirm.failed": "Deletion failed",
|
|
20
|
+
"panel.title": "Session trash",
|
|
21
|
+
"panel.tabTrash": "Trash",
|
|
22
|
+
"panel.tabAudit": "Audit",
|
|
23
|
+
"panel.empty": "The trash is empty.",
|
|
24
|
+
"panel.auditEmpty": "No audit entries yet.",
|
|
25
|
+
"panel.restore": "Restore",
|
|
26
|
+
"panel.purge": "Delete for good",
|
|
27
|
+
"panel.purgeConfirm": "Delete for good? This cannot be undone",
|
|
28
|
+
"panel.size": "{size}",
|
|
29
|
+
"panel.trashedAt": "Trashed {time}",
|
|
30
|
+
"panel.unknown": "{n} unrecognized director(ies) kept",
|
|
31
|
+
"panel.pending": "{n} unfinished operation(s)",
|
|
32
|
+
"panel.readOnly": "Read-only: this DSH version is missing required APIs; deletion is disabled.",
|
|
33
|
+
"panel.close": "Close",
|
|
34
|
+
"toast.failed": "Operation failed: {message}",
|
|
35
|
+
"footer.trash": "Trash"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"sessionDelete": {
|
|
3
|
+
"menu.delete": "删除会话…",
|
|
4
|
+
"menu.trash": "回收站…",
|
|
5
|
+
"confirm.title": "删除这个会话?",
|
|
6
|
+
"confirm.loading": "正在读取影响面…",
|
|
7
|
+
"confirm.desc": "会话会被移入本机回收站,可以恢复。",
|
|
8
|
+
"confirm.size": "会话日志约 {size}",
|
|
9
|
+
"confirm.live": "该会话仍在本机打开,删除前会先停止它的工作。",
|
|
10
|
+
"confirm.children": "会一并带上 {n} 个子代理会话",
|
|
11
|
+
"confirm.refs": "将清理:{list}",
|
|
12
|
+
"confirm.trashPath": "回收站位置:{path}",
|
|
13
|
+
"confirm.blocked": "暂时无法删除:{reason}",
|
|
14
|
+
"confirm.cancel": "取消",
|
|
15
|
+
"confirm.confirm": "移入回收站",
|
|
16
|
+
"confirm.pending": "正在移入回收站…",
|
|
17
|
+
"confirm.done": "已移入回收站",
|
|
18
|
+
"confirm.undo": "撤销",
|
|
19
|
+
"confirm.failed": "删除失败",
|
|
20
|
+
"panel.title": "会话回收站",
|
|
21
|
+
"panel.tabTrash": "回收站",
|
|
22
|
+
"panel.tabAudit": "审计",
|
|
23
|
+
"panel.empty": "回收站是空的。",
|
|
24
|
+
"panel.auditEmpty": "还没有审计记录。",
|
|
25
|
+
"panel.restore": "恢复",
|
|
26
|
+
"panel.purge": "彻底删除",
|
|
27
|
+
"panel.purgeConfirm": "彻底删除?不可恢复",
|
|
28
|
+
"panel.size": "占用 {size}",
|
|
29
|
+
"panel.trashedAt": "删除于 {time}",
|
|
30
|
+
"panel.unknown": "{n} 个无法识别的目录已保留",
|
|
31
|
+
"panel.pending": "{n} 个未完成的操作",
|
|
32
|
+
"panel.readOnly": "只读模式:当前 DSH 版本缺少依赖接口,删除功能已禁用。",
|
|
33
|
+
"panel.close": "关闭",
|
|
34
|
+
"toast.failed": "操作失败:{message}",
|
|
35
|
+
"footer.trash": "回收站"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kangyishuai/dsh-session-delete",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Real session deletion for DSH: trash by default, plan before delete, audit log, residue receipt",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"dsh",
|
|
7
|
+
"dsh-plugin",
|
|
8
|
+
"deepseek-harness",
|
|
9
|
+
"session",
|
|
10
|
+
"delete",
|
|
11
|
+
"trash",
|
|
12
|
+
"audit",
|
|
13
|
+
"dry-run"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "nano (https://github.com/kangyishuai)",
|
|
17
|
+
"homepage": "https://github.com/kangyishuai/dsh-plugins/tree/main/packages/session-delete#readme",
|
|
18
|
+
"bugs": "https://github.com/kangyishuai/dsh-plugins/issues",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/kangyishuai/dsh-plugins.git",
|
|
22
|
+
"directory": "packages/session-delete"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "lib/index.js",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./lib/index.js",
|
|
28
|
+
"./client": "./lib/client.js",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"cordis.patch.yml",
|
|
33
|
+
"lib",
|
|
34
|
+
"locale",
|
|
35
|
+
"README.md",
|
|
36
|
+
"README.zh-CN.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"dsh": {
|
|
43
|
+
"bundle": {
|
|
44
|
+
"patch": "./cordis.patch.yml"
|
|
45
|
+
},
|
|
46
|
+
"client": {
|
|
47
|
+
"platform": "web",
|
|
48
|
+
"inject": [
|
|
49
|
+
"@deepseek-ai/dsh-client-ui-workspace",
|
|
50
|
+
"@deepseek-ai/dsh-client-ui-slots",
|
|
51
|
+
"@deepseek-ai/dsh-client-locale",
|
|
52
|
+
"@deepseek-ai/dsh-client-connection",
|
|
53
|
+
"@deepseek-ai/dsh-client-ui-primitives"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"test": "node --test"
|
|
59
|
+
}
|
|
6
60
|
}
|