dsh-sweeper 0.0.0-stage → 1.4.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 +348 -2
- package/cordis.patch.yml +3 -0
- package/icon.svg +5 -0
- package/lib/agent.js +190 -0
- package/lib/client.js +1639 -0
- package/lib/deep-agent.js +131 -0
- package/lib/deep.js +695 -0
- package/lib/fs-tools.js +198 -0
- package/lib/index.js +994 -0
- package/lib/paths.js +83 -0
- package/lib/review.js +336 -0
- package/lib/scan.js +520 -0
- package/lib/trash.js +345 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +72 -4
package/lib/trash.js
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-sweeper · reversible quarantine.
|
|
3
|
+
*
|
|
4
|
+
* Deleting means "move into the plugin's own trash directory", never unlink.
|
|
5
|
+
* Every batch is a directory with a manifest.json; restoring moves files back
|
|
6
|
+
* to their original absolute path and refuses to overwrite anything that
|
|
7
|
+
* reappeared in the meantime. Permanent removal only happens on an explicit
|
|
8
|
+
* purge of a batch.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { copyFile, lstat, mkdir, readdir, readFile, realpath, rename, rm, stat, unlink, writeFile } from 'node:fs/promises'
|
|
12
|
+
import { basename, dirname, join, resolve } from 'node:path'
|
|
13
|
+
import { homedir } from 'node:os'
|
|
14
|
+
|
|
15
|
+
export const MAX_BATCH = 5000
|
|
16
|
+
|
|
17
|
+
export function dshHome() {
|
|
18
|
+
const env = process.env.DSH_HOME
|
|
19
|
+
if (env && env.trim()) return resolve(env.trim())
|
|
20
|
+
return join(homedir(), '.dsh')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function trashRoot() {
|
|
24
|
+
return join(dshHome(), 'plugin-data', 'dsh-sweeper', 'trash')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
import { WIN, isAbsPath, normKey, joinPath, isInside } from './paths.js'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Everything that must never be quarantined, independent of what the scan says.
|
|
32
|
+
* Returns null when the path is acceptable, otherwise a short reason.
|
|
33
|
+
*/
|
|
34
|
+
export async function rejectionReason(p) {
|
|
35
|
+
if (typeof p !== 'string' || !p) return 'empty path'
|
|
36
|
+
const raw = p.trim()
|
|
37
|
+
if (!isAbsPath(raw)) return 'not an absolute path'
|
|
38
|
+
if (raw.includes('..')) return 'path contains ..'
|
|
39
|
+
if (raw.replace(/[.\\ ]+$/, '').length !== raw.replace(/[. ]+$/, '').length) return 'path ends with a dot or space'
|
|
40
|
+
const depth = raw.split(WIN ? /[\\/]+/ : /\/+/).filter(Boolean).length
|
|
41
|
+
if (depth < 3) return 'refuses to touch a drive root or top-level directory'
|
|
42
|
+
|
|
43
|
+
const n = normKey(raw)
|
|
44
|
+
for (const root of protectedRoots()) {
|
|
45
|
+
if (isInside(n, root)) return 'inside DSH state'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let real
|
|
49
|
+
try { real = await realpath(raw) } catch { return 'gone' }
|
|
50
|
+
if (normKey(real) !== n) return 'path crosses a link or junction'
|
|
51
|
+
try {
|
|
52
|
+
const st = await stat(real)
|
|
53
|
+
if (!st.isFile()) return 'not a regular file'
|
|
54
|
+
} catch { return 'gone' }
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function protectedRoots() {
|
|
59
|
+
return [dshHome(), join(homedir(), '.agents'), trashRoot()].map((x) => normKey(x))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Path sanity for a restore target coming out of an on-disk manifest. */
|
|
63
|
+
export function restoreTargetProblem(p) {
|
|
64
|
+
if (typeof p !== 'string' || !p) return 'empty path'
|
|
65
|
+
const raw = p.trim()
|
|
66
|
+
if (!isAbsPath(raw)) return 'not an absolute path'
|
|
67
|
+
if (raw.includes('..')) return 'path contains ..'
|
|
68
|
+
const n = normKey(raw)
|
|
69
|
+
for (const root of protectedRoots()) {
|
|
70
|
+
if (isInside(n, root)) return 'refuses to restore into DSH state'
|
|
71
|
+
}
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Move a file across volumes: rename first, fall back to copy+unlink. */
|
|
76
|
+
async function moveFile(src, dest) {
|
|
77
|
+
try {
|
|
78
|
+
await rename(src, dest)
|
|
79
|
+
return { mode: 'rename' }
|
|
80
|
+
} catch (err) {
|
|
81
|
+
const code = err && err.code
|
|
82
|
+
if (code !== 'EXDEV' && code !== 'EPERM' && code !== 'EACCES') throw err
|
|
83
|
+
}
|
|
84
|
+
await copyFile(src, dest)
|
|
85
|
+
try {
|
|
86
|
+
await unlink(src)
|
|
87
|
+
} catch (err) {
|
|
88
|
+
try { await unlink(dest) } catch { /* ignore */ }
|
|
89
|
+
throw err
|
|
90
|
+
}
|
|
91
|
+
return { mode: 'copy' }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function batchId() {
|
|
95
|
+
return Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 8)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const safeId = (id) => /^[a-z0-9]+-[a-z0-9]+$/i.test(String(id || ''))
|
|
99
|
+
|
|
100
|
+
// A bad request must not reach the client as a 500: retrying would never help,
|
|
101
|
+
// and the page would say "the service returned 500, try again".
|
|
102
|
+
const badRequest = (message) => Object.assign(new Error(message), { status: 400 })
|
|
103
|
+
|
|
104
|
+
const readManifest = async (dir) => JSON.parse(await readFile(join(dir, 'manifest.json'), 'utf8'))
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A manifest is the only record of where each file came from; a half-written
|
|
108
|
+
* one strands an entire batch. Write beside it and rename over it, so readers
|
|
109
|
+
* see either the previous manifest or the next one, never a torn file.
|
|
110
|
+
*/
|
|
111
|
+
async function writeManifest(dir, manifest) {
|
|
112
|
+
const final = join(dir, 'manifest.json')
|
|
113
|
+
const tmp = join(dir, 'manifest.json.tmp')
|
|
114
|
+
await writeFile(tmp, JSON.stringify(manifest, null, 2), 'utf8')
|
|
115
|
+
await rename(tmp, final)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function listBatches() {
|
|
119
|
+
const root = trashRoot()
|
|
120
|
+
let names = []
|
|
121
|
+
try { names = await readdir(root) } catch { return [] }
|
|
122
|
+
const out = []
|
|
123
|
+
for (const name of names) {
|
|
124
|
+
if (!safeId(name)) continue
|
|
125
|
+
let manifest
|
|
126
|
+
try { manifest = await readManifest(join(root, name)) } catch {
|
|
127
|
+
// The files are still on disk and still recoverable by hand; hiding the
|
|
128
|
+
// batch would make the quarantine look empty. Report what is there.
|
|
129
|
+
let files = []
|
|
130
|
+
try { files = await readdir(join(root, name), { withFileTypes: true }) } catch { files = [] }
|
|
131
|
+
const kept = files.filter((f) => f.isFile() && f.name !== 'manifest.json' && !f.name.endsWith('.tmp'))
|
|
132
|
+
let bytes = 0
|
|
133
|
+
for (const f of kept) { try { bytes += (await stat(join(root, name, f.name))).size } catch { /* ignore */ } }
|
|
134
|
+
out.push({ id: name, createdAt: 0, kind: 'quarantine', note: '', count: kept.length, bytes, damaged: true })
|
|
135
|
+
continue
|
|
136
|
+
}
|
|
137
|
+
const entries = Array.isArray(manifest.entries) ? manifest.entries : []
|
|
138
|
+
out.push({
|
|
139
|
+
id: name,
|
|
140
|
+
createdAt: manifest.createdAt || 0,
|
|
141
|
+
kind: manifest.kind || 'quarantine',
|
|
142
|
+
note: manifest.note || '',
|
|
143
|
+
count: entries.length,
|
|
144
|
+
bytes: entries.reduce((s, e) => s + (e.size || 0), 0),
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
out.sort((a, b) => b.createdAt - a.createdAt)
|
|
148
|
+
return out
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Move `paths` into a new trash batch.
|
|
153
|
+
* @param {{path:string,key:string,size:number,mtime:number,tier?:string,reasons?:string[]}[]} items
|
|
154
|
+
*/
|
|
155
|
+
export async function quarantine(items, meta = {}) {
|
|
156
|
+
if (!Array.isArray(items) || !items.length) throw badRequest('nothing selected')
|
|
157
|
+
if (items.length > MAX_BATCH) throw badRequest('too many items in one batch (max ' + MAX_BATCH + ')')
|
|
158
|
+
|
|
159
|
+
const id = batchId()
|
|
160
|
+
const dir = join(trashRoot(), id)
|
|
161
|
+
await mkdir(dir, { recursive: true })
|
|
162
|
+
|
|
163
|
+
const manifest = {
|
|
164
|
+
id,
|
|
165
|
+
createdAt: Date.now(),
|
|
166
|
+
kind: 'quarantine',
|
|
167
|
+
note: String(meta.note || '').slice(0, 200),
|
|
168
|
+
plugin: 'dsh-sweeper',
|
|
169
|
+
entries: [],
|
|
170
|
+
}
|
|
171
|
+
const failed = []
|
|
172
|
+
let index = 0
|
|
173
|
+
|
|
174
|
+
for (const item of items) {
|
|
175
|
+
const reason = await rejectionReason(item.path)
|
|
176
|
+
if (reason) { failed.push({ path: item.path, reason }); continue }
|
|
177
|
+
let st
|
|
178
|
+
try { st = await stat(item.path) } catch { failed.push({ path: item.path, reason: 'gone' }); continue }
|
|
179
|
+
if (item.size != null && st.size !== item.size) { failed.push({ path: item.path, reason: 'changed size since the scan' }); continue }
|
|
180
|
+
if (item.mtime != null && Math.abs(st.mtimeMs - item.mtime) > 1000) { failed.push({ path: item.path, reason: 'modified since the scan' }); continue }
|
|
181
|
+
|
|
182
|
+
index += 1
|
|
183
|
+
const rel = String(index).padStart(4, '0') + '-' + basename(item.path).replace(/[^\w.@-]+/g, '_').slice(0, 80)
|
|
184
|
+
const dest = join(dir, rel)
|
|
185
|
+
try {
|
|
186
|
+
await moveFile(item.path, dest)
|
|
187
|
+
} catch (err) {
|
|
188
|
+
failed.push({ path: item.path, reason: (err && err.code) || String(err) })
|
|
189
|
+
continue
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// The copy path can truncate silently on a full disk: trust but verify.
|
|
193
|
+
let moved = 0
|
|
194
|
+
try { moved = (await stat(dest)).size } catch { moved = -1 }
|
|
195
|
+
if (moved !== st.size) {
|
|
196
|
+
try { await moveFile(dest, item.path) } catch { /* ignore */ }
|
|
197
|
+
failed.push({ path: item.path, reason: 'copy was incomplete; put back' })
|
|
198
|
+
continue
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
manifest.entries.push({
|
|
202
|
+
key: item.key || null,
|
|
203
|
+
orig: item.path,
|
|
204
|
+
rel,
|
|
205
|
+
size: st.size,
|
|
206
|
+
mtime: st.mtimeMs,
|
|
207
|
+
tier: item.tier || null,
|
|
208
|
+
reasons: item.reasons || [],
|
|
209
|
+
at: Date.now(),
|
|
210
|
+
})
|
|
211
|
+
// Write after every move so a crash never loses the mapping.
|
|
212
|
+
try { await writeManifest(dir, manifest) } catch { /* best effort */ }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!manifest.entries.length) {
|
|
216
|
+
try { await rm(dir, { recursive: true, force: true }) } catch { /* ignore */ }
|
|
217
|
+
throw new Error('nothing could be moved: ' + (failed[0] ? failed[0].reason : 'unknown'))
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
batch: {
|
|
221
|
+
id,
|
|
222
|
+
createdAt: manifest.createdAt,
|
|
223
|
+
count: manifest.entries.length,
|
|
224
|
+
bytes: manifest.entries.reduce((s, e) => s + (e.size || 0), 0),
|
|
225
|
+
},
|
|
226
|
+
moved: manifest.entries.map((e) => e.key).filter(Boolean),
|
|
227
|
+
movedPaths: manifest.entries.map((e) => e.orig),
|
|
228
|
+
failed,
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Two operations on the same batch (restore vs purge) would race: purge removes
|
|
234
|
+
* the directory while restore is still moving files out of it, or restore writes
|
|
235
|
+
* the manifest after purge deleted it. One lock per batch id, in process.
|
|
236
|
+
*/
|
|
237
|
+
const busy = new Set()
|
|
238
|
+
function lockBatch(id) {
|
|
239
|
+
if (busy.has(id)) {
|
|
240
|
+
const err = new Error('batch is busy: ' + id + ' is being restored or purged right now')
|
|
241
|
+
err.status = 409
|
|
242
|
+
throw err
|
|
243
|
+
}
|
|
244
|
+
busy.add(id)
|
|
245
|
+
return () => busy.delete(id)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export async function restore(id) {
|
|
249
|
+
if (!safeId(id)) throw badRequest('bad batch id')
|
|
250
|
+
const unlock = lockBatch(id)
|
|
251
|
+
const dir = join(trashRoot(), id)
|
|
252
|
+
const restored = []
|
|
253
|
+
const failed = []
|
|
254
|
+
const keep = []
|
|
255
|
+
try {
|
|
256
|
+
const manifest = await readManifest(dir)
|
|
257
|
+
for (const entry of Array.isArray(manifest.entries) ? manifest.entries : []) {
|
|
258
|
+
const src = join(dir, String(entry.rel || ''))
|
|
259
|
+
if (!entry.rel || entry.rel.includes('..') || entry.rel.includes('/') || entry.rel.includes('\\')) {
|
|
260
|
+
failed.push({ path: entry.orig, reason: 'bad manifest entry' })
|
|
261
|
+
keep.push(entry)
|
|
262
|
+
continue
|
|
263
|
+
}
|
|
264
|
+
try { await stat(src) } catch {
|
|
265
|
+
failed.push({ path: entry.orig, reason: 'missing from trash' })
|
|
266
|
+
continue
|
|
267
|
+
}
|
|
268
|
+
const problem = restoreTargetProblem(entry.orig)
|
|
269
|
+
if (problem) {
|
|
270
|
+
failed.push({ path: entry.orig, reason: problem })
|
|
271
|
+
keep.push(entry)
|
|
272
|
+
continue
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
const st = await stat(entry.orig).catch(() => null)
|
|
276
|
+
if (st) { failed.push({ path: entry.orig, reason: 'a file already exists there' }); keep.push(entry); continue }
|
|
277
|
+
await mkdir(dirname(entry.orig), { recursive: true })
|
|
278
|
+
await moveFile(src, entry.orig)
|
|
279
|
+
restored.push(entry.orig)
|
|
280
|
+
} catch (err) {
|
|
281
|
+
failed.push({ path: entry.orig, reason: (err && err.code) || String(err) })
|
|
282
|
+
keep.push(entry)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (!keep.length) {
|
|
286
|
+
try { await rm(dir, { recursive: true, force: true }) } catch { /* ignore */ }
|
|
287
|
+
} else {
|
|
288
|
+
manifest.entries = keep
|
|
289
|
+
try { await writeManifest(dir, manifest) } catch { /* ignore */ }
|
|
290
|
+
}
|
|
291
|
+
return { id, restored: restored.length, restoredPaths: restored, failed, remaining: keep.length }
|
|
292
|
+
} finally {
|
|
293
|
+
unlock()
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Count the files a damaged batch still holds, so the page can state a number. */
|
|
298
|
+
async function countFiles(dir) {
|
|
299
|
+
let n = 0
|
|
300
|
+
let names = []
|
|
301
|
+
try { names = await readdir(dir) } catch { return 0 }
|
|
302
|
+
for (const name of names) {
|
|
303
|
+
if (name === 'manifest.json' || name === 'manifest.json.tmp') continue
|
|
304
|
+
const full = join(dir, name)
|
|
305
|
+
// lstat, not stat: a symlink inside a batch must not be followed and must
|
|
306
|
+
// not be counted as if it were one of the quarantined files.
|
|
307
|
+
const st = await lstat(full).catch(() => null)
|
|
308
|
+
if (!st) continue
|
|
309
|
+
if (st.isSymbolicLink()) continue
|
|
310
|
+
n += st.isDirectory() ? await countFiles(full) : st.isFile() ? 1 : 0
|
|
311
|
+
}
|
|
312
|
+
return n
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export async function purge(id) {
|
|
316
|
+
if (!safeId(id)) throw badRequest('bad batch id')
|
|
317
|
+
const unlock = lockBatch(id)
|
|
318
|
+
try {
|
|
319
|
+
const dir = join(trashRoot(), id)
|
|
320
|
+
let count = 0
|
|
321
|
+
let manifest = null
|
|
322
|
+
try { manifest = await readManifest(dir) } catch { manifest = null }
|
|
323
|
+
let existed = true
|
|
324
|
+
try { await stat(dir) } catch { existed = false }
|
|
325
|
+
// An unreadable manifest used to mark the batch as gone, so purging a
|
|
326
|
+
// damaged batch answered 404 "already gone" right after deleting its files.
|
|
327
|
+
if (manifest && Array.isArray(manifest.entries)) count = manifest.entries.length
|
|
328
|
+
else if (existed) count = await countFiles(dir)
|
|
329
|
+
try {
|
|
330
|
+
await rm(dir, { recursive: true, force: true })
|
|
331
|
+
} catch {
|
|
332
|
+
const err = new Error('a file in that batch is still open by another program')
|
|
333
|
+
err.status = 409
|
|
334
|
+
err.code = 'BATCH_IN_USE'
|
|
335
|
+
throw err
|
|
336
|
+
}
|
|
337
|
+
return { id, purged: existed, missing: !existed, count }
|
|
338
|
+
} finally {
|
|
339
|
+
unlock()
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export function describeBatch(id) {
|
|
344
|
+
return join(trashRoot(), String(id))
|
|
345
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,74 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sweeper",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "清理对话留下的临时文件与垃圾,释放磁盘空间;只报告,清理进回收站可恢复 (cleanup junk files conversations leave on disk, free up space)",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"deepseek",
|
|
8
|
+
"deepseek-harness",
|
|
9
|
+
"dsh",
|
|
10
|
+
"dsh-plugin",
|
|
11
|
+
"cleanup",
|
|
12
|
+
"garbage-collector",
|
|
13
|
+
"disk-space",
|
|
14
|
+
"audit",
|
|
15
|
+
"junk-files",
|
|
16
|
+
"temporary-files",
|
|
17
|
+
"cache",
|
|
18
|
+
"trash",
|
|
19
|
+
"free-up-space",
|
|
20
|
+
"清理内存",
|
|
21
|
+
"清理对话",
|
|
22
|
+
"清理垃圾",
|
|
23
|
+
"释放空间",
|
|
24
|
+
"回收站"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": "Dushiqi1987",
|
|
28
|
+
"homepage": "https://github.com/Dushiqi1987/dsh-sweeper#readme",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/Dushiqi1987/dsh-sweeper.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/Dushiqi1987/dsh-sweeper/issues"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"exports": {
|
|
40
|
+
".": "./lib/index.js",
|
|
41
|
+
"./client": "./lib/client.js",
|
|
42
|
+
"./locale/*.json": "./locale/*.json",
|
|
43
|
+
"./icon": "./icon.svg",
|
|
44
|
+
"./package.json": "./package.json",
|
|
45
|
+
"./cordis.patch.yml": "./cordis.patch.yml"
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"LICENSE",
|
|
49
|
+
"README.md",
|
|
50
|
+
"cordis.patch.yml",
|
|
51
|
+
"icon.svg",
|
|
52
|
+
"lib",
|
|
53
|
+
"locale"
|
|
54
|
+
],
|
|
55
|
+
"dsh": {
|
|
56
|
+
"bundle": {
|
|
57
|
+
"patch": "./cordis.patch.yml"
|
|
58
|
+
},
|
|
59
|
+
"client": {
|
|
60
|
+
"platform": "web",
|
|
61
|
+
"inject": [
|
|
62
|
+
"@deepseek-ai/dsh-client-ui-renderer",
|
|
63
|
+
"@deepseek-ai/dsh-client-locale"
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"guards": "node scripts/preflight.mjs",
|
|
69
|
+
"contract": "node test/contract.mjs",
|
|
70
|
+
"messages": "node test/messages.mjs",
|
|
71
|
+
"listing": "node test/market-listing.mjs",
|
|
72
|
+
"shots": "node test/screenshots.mjs"
|
|
73
|
+
}
|
|
74
|
+
}
|