dsh-sweeper 1.5.16 → 1.5.18
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 +2 -0
- package/lib/client.js +6 -3
- package/lib/review.js +5 -2
- package/lib/scan.js +6 -0
- package/lib/trash.js +54 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -278,6 +278,8 @@ node test/browser/check.mjs # 需要本机有 Chrome/Edge,或 CHROME=<p
|
|
|
278
278
|
## 边界与自保
|
|
279
279
|
|
|
280
280
|
- **工具的遍历上限会说出来**:模型自己找文件时走到 400 个条目上限会收到一句「只看了前 400 个」,不会把「没扫完」当成「没有」。
|
|
281
|
+
- **清单外文件太多时会说「至少」**:模型复核的清单外文件最多收集 160 个、提交 80 个;命中上限时页面写「清单外文件至少 N 个……磁盘上还有更多没有列出来」,提示词里也会告诉模型这份清单不完整。
|
|
282
|
+
- **清理大目录不会越来越慢**:一次清理每搬一个文件只追加一行 `journal.jsonl`(而不是重写整份清单),搬完再折成一份 `manifest.json`;批次跑到一半崩溃时,`journal.jsonl` 仍是完整的记录,列表和恢复都认得它。
|
|
281
283
|
- **缓存与构建目录也点名**:`node_modules`、`.pnpm-store`、`.gradle`、`.m2`、`.cargo`、`.turbo`、`.next`、`dist` 等 21 个目录不遍历,跳过了多少个会写在结果末尾(「N 个缓存或构建目录没有搜」),免得模型以为整棵树都看过了。
|
|
282
284
|
- **不敢跟的链接、读不到的条目会被数出来**:`list_dir` / `find_files` 遇到符号链接(可能指向工作目录之外)和权限不足的条目时不再静默跳过,而是写「N 个链接未跟随」「N 个条目读不出来」;整目录读不进去时单独写「N 个目录读不出来」,不会把目录说成文件。
|
|
283
285
|
- **有日志读不出来,就不给「无脑删」**:一份会话日志打不开时,它可能用过任何文件;这一轮所有原本能一键清理的条目都会降到「需要你判断」,并在理由里写明原因。
|
package/lib/client.js
CHANGED
|
@@ -700,12 +700,15 @@ const STYLE_ID = 'dsh-sweeper-style'
|
|
|
700
700
|
h(Stat, { label: '模型', value: review.model && review.model.model ? review.model.model : '—', sub: (review.model && review.model.provider ? review.model.provider + ' · ' : '') + '耗时 ' + fmtDuration(review.ms) }),
|
|
701
701
|
h(Stat, { label: '本次看过', value: fmtNum(review.reviewed) + ' 条', sub: review.skipped
|
|
702
702
|
? '报告共 ' + fmtNum(review.reviewed + review.skipped) + ' 条,只发送了前 ' + fmtNum(review.reviewed) + ' 条'
|
|
703
|
-
: (extra.hints
|
|
703
|
+
: (extra.hints
|
|
704
|
+
? '另看 ' + fmtNum(extra.hints) + ' 个清单外文件' + (review.hintsCapped ? '(磁盘上还有更多没列)' : '')
|
|
705
|
+
: '编号全部有效') }),
|
|
704
706
|
),
|
|
705
707
|
|
|
706
|
-
review.hintsSkipped
|
|
708
|
+
review.hintsSkipped || review.hintsCapped
|
|
707
709
|
? h('div', { style: { fontSize: 11.5, color: T.faint, marginTop: 6 } },
|
|
708
|
-
'
|
|
710
|
+
'清单外文件' + (review.hintsCapped ? '至少 ' : '共 ') + fmtNum(review.hintsTotal) + ' 个,只提交了前 ' + fmtNum(extra.hints) + ' 个'
|
|
711
|
+
+ (review.hintsCapped ? ';磁盘上还有更多没有列出来。' : '。'))
|
|
709
712
|
: null,
|
|
710
713
|
|
|
711
714
|
moved.length
|
package/lib/review.js
CHANGED
|
@@ -187,7 +187,9 @@ export async function review(ctx, report, opts = {}) {
|
|
|
187
187
|
? ['A 段 · 候选清单(这些已经被规则筛过一轮):', JSON.stringify(payload), '']
|
|
188
188
|
: ['A 段 · 这次没有候选清单,只回答 B 段。', '']),
|
|
189
189
|
...(hints.length
|
|
190
|
-
? ['B 段 ·
|
|
190
|
+
? ['B 段 · 清单外观察到的文件(在已知临时目录里、但任何会话日志都没提到过;只根据路径和闲置天数判断)'
|
|
191
|
+
+ (report.hintsTruncated ? '(清单外文件很多,这里只列了前 ' + extra.length + ' 个;磁盘上还有更多没有列出来)' : '')
|
|
192
|
+
+ ':', JSON.stringify(extra), '']
|
|
191
193
|
: ['B 段 · 这次没有清单外文件。', '']),
|
|
192
194
|
'请按格式逐行输出判断。',
|
|
193
195
|
].join('\n')
|
|
@@ -234,7 +236,8 @@ export async function review(ctx, report, opts = {}) {
|
|
|
234
236
|
verdict: translated.verdict,
|
|
235
237
|
proposals: translated.proposals,
|
|
236
238
|
hints: hints.length,
|
|
237
|
-
hintsTotal: allHints.length,
|
|
239
|
+
hintsTotal: allHints.length,
|
|
240
|
+
hintsCapped: !!(report && report.hintsTruncated),
|
|
238
241
|
hintsSkipped: allHints.length - hints.length,
|
|
239
242
|
unknownIds: translated.unknown,
|
|
240
243
|
reviewed: items.length,
|
package/lib/scan.js
CHANGED
|
@@ -214,6 +214,9 @@ const HINT_SKIP = new Set(['node_modules', '.git', '.venv', 'venv', 'dist', 'bui
|
|
|
214
214
|
function collectHints(roots, artifacts, cutoff, now) {
|
|
215
215
|
const out = []
|
|
216
216
|
const seen = new Set(artifacts.keys())
|
|
217
|
+
// Stopping at a cap without saying so turns "the first 160 files" into
|
|
218
|
+
// "there are 160 files". Callers read the flag and say what they know.
|
|
219
|
+
let truncated = roots.length > 40
|
|
217
220
|
const queue = roots.slice(0, 40).map((r) => ({ dir: r, depth: 0 }))
|
|
218
221
|
while (queue.length && out.length < HINT_LIMIT) {
|
|
219
222
|
const { dir, depth } = queue.shift()
|
|
@@ -244,7 +247,9 @@ function collectHints(roots, artifacts, cutoff, now) {
|
|
|
244
247
|
})
|
|
245
248
|
}
|
|
246
249
|
}
|
|
250
|
+
if (out.length >= HINT_LIMIT) truncated = true
|
|
247
251
|
out.sort((a, b) => b.size - a.size)
|
|
252
|
+
Object.defineProperty(out, 'truncated', { value: truncated, enumerable: false })
|
|
248
253
|
return out
|
|
249
254
|
}
|
|
250
255
|
|
|
@@ -564,6 +569,7 @@ export async function scan(ctx, opts = {}) {
|
|
|
564
569
|
unreadable,
|
|
565
570
|
items: capped,
|
|
566
571
|
hints,
|
|
572
|
+
hintsTruncated: hints.truncated === true,
|
|
567
573
|
truncated: items.length > capped.length,
|
|
568
574
|
}
|
|
569
575
|
}
|
package/lib/trash.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* purge of a batch.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import { copyFile, lstat, mkdir, readdir, readFile, realpath, rename, rm, stat, unlink, writeFile } from 'node:fs/promises'
|
|
11
|
+
import { appendFile, copyFile, lstat, mkdir, readdir, readFile, realpath, rename, rm, stat, unlink, writeFile } from 'node:fs/promises'
|
|
12
12
|
import { basename, dirname, join, resolve } from 'node:path'
|
|
13
13
|
import { homedir } from 'node:os'
|
|
14
14
|
|
|
@@ -120,6 +120,39 @@ const badRequest = (message) => Object.assign(new Error(message), { status: 400
|
|
|
120
120
|
|
|
121
121
|
const readManifest = async (dir) => JSON.parse(await readFile(join(dir, 'manifest.json'), 'utf8'))
|
|
122
122
|
|
|
123
|
+
const JOURNAL = 'journal.jsonl'
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* One line per moved file, appended as it lands. Appending is O(1), while
|
|
127
|
+
* rewriting the whole manifest after every file is O(n²): a 5,000-file batch
|
|
128
|
+
* would write gigabytes and take minutes. The journal is folded into
|
|
129
|
+
* manifest.json when the batch finishes; if the process dies first, the
|
|
130
|
+
* journal is still a complete record of what moved where.
|
|
131
|
+
*/
|
|
132
|
+
async function appendJournal(dir, entry) {
|
|
133
|
+
await appendFile(join(dir, JOURNAL), JSON.stringify(entry) + '\n', 'utf8')
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function readJournal(dir) {
|
|
137
|
+
let text = ''
|
|
138
|
+
try { text = await readFile(join(dir, JOURNAL), 'utf8') } catch { return [] }
|
|
139
|
+
const out = []
|
|
140
|
+
for (const line of text.split('\n')) {
|
|
141
|
+
if (!line.trim()) continue
|
|
142
|
+
try {
|
|
143
|
+
const entry = JSON.parse(line)
|
|
144
|
+
if (entry && typeof entry.rel === 'string' && entry.rel) out.push(entry)
|
|
145
|
+
} catch { /* a torn last line is expected after a crash; skip it */ }
|
|
146
|
+
}
|
|
147
|
+
return out
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Manifest entries, falling back to the journal of a batch that never folded. */
|
|
151
|
+
async function entriesOf(dir, manifest) {
|
|
152
|
+
const entries = Array.isArray(manifest.entries) ? manifest.entries : []
|
|
153
|
+
return entries.length ? entries : readJournal(dir)
|
|
154
|
+
}
|
|
155
|
+
|
|
123
156
|
/**
|
|
124
157
|
* A manifest is the only record of where each file came from; a half-written
|
|
125
158
|
* one strands an entire batch. Write beside it and rename over it, so readers
|
|
@@ -139,19 +172,20 @@ export async function listBatches() {
|
|
|
139
172
|
const out = []
|
|
140
173
|
for (const name of names) {
|
|
141
174
|
if (!safeId(name)) continue
|
|
175
|
+
const dir = join(root, name)
|
|
142
176
|
let manifest
|
|
143
|
-
try { manifest = await readManifest(
|
|
177
|
+
try { manifest = await readManifest(dir) } catch {
|
|
144
178
|
// The files are still on disk and still recoverable by hand; hiding the
|
|
145
179
|
// batch would make the quarantine look empty. Report what is there.
|
|
146
180
|
let files = []
|
|
147
|
-
try { files = await readdir(
|
|
148
|
-
const kept = files.filter((f) => f.isFile() && f.name !== 'manifest.json' && !f.name.endsWith('.tmp'))
|
|
181
|
+
try { files = await readdir(dir, { withFileTypes: true }) } catch { files = [] }
|
|
182
|
+
const kept = files.filter((f) => f.isFile() && f.name !== 'manifest.json' && f.name !== JOURNAL && !f.name.endsWith('.tmp'))
|
|
149
183
|
let bytes = 0
|
|
150
|
-
for (const f of kept) { try { bytes += (await stat(join(
|
|
184
|
+
for (const f of kept) { try { bytes += (await stat(join(dir, f.name))).size } catch { /* ignore */ } }
|
|
151
185
|
out.push({ id: name, createdAt: 0, kind: 'quarantine', note: '', count: kept.length, bytes, damaged: true })
|
|
152
186
|
continue
|
|
153
187
|
}
|
|
154
|
-
const entries =
|
|
188
|
+
const entries = await entriesOf(dir, manifest)
|
|
155
189
|
out.push({
|
|
156
190
|
id: name,
|
|
157
191
|
createdAt: manifest.createdAt || 0,
|
|
@@ -187,6 +221,8 @@ export async function quarantine(items, meta = {}) {
|
|
|
187
221
|
}
|
|
188
222
|
const failed = []
|
|
189
223
|
let index = 0
|
|
224
|
+
// Written once so readers know the batch exists; entries arrive via the journal.
|
|
225
|
+
try { await writeManifest(dir, manifest) } catch { /* ignore */ }
|
|
190
226
|
|
|
191
227
|
for (const item of items) {
|
|
192
228
|
const reason = await rejectionReason(item.path)
|
|
@@ -225,14 +261,21 @@ export async function quarantine(items, meta = {}) {
|
|
|
225
261
|
reasons: item.reasons || [],
|
|
226
262
|
at: Date.now(),
|
|
227
263
|
})
|
|
228
|
-
//
|
|
229
|
-
|
|
264
|
+
// Append, don't rewrite: a crash must not lose the mapping, and the mapping
|
|
265
|
+
// must not cost O(n²) writes to keep.
|
|
266
|
+
try { await appendJournal(dir, manifest.entries[manifest.entries.length - 1]) } catch { /* best effort */ }
|
|
230
267
|
}
|
|
231
268
|
|
|
232
269
|
if (!manifest.entries.length) {
|
|
233
270
|
try { await rm(dir, { recursive: true, force: true }) } catch { /* ignore */ }
|
|
234
271
|
throw new Error('nothing could be moved: ' + (failed[0] ? failed[0].reason : 'unknown'))
|
|
235
272
|
}
|
|
273
|
+
// Fold the journal into the manifest so the batch is one readable file again.
|
|
274
|
+
try {
|
|
275
|
+
await writeManifest(dir, manifest)
|
|
276
|
+
await unlink(join(dir, JOURNAL))
|
|
277
|
+
} catch { /* the journal is still a valid record */ }
|
|
278
|
+
|
|
236
279
|
return {
|
|
237
280
|
batch: {
|
|
238
281
|
id,
|
|
@@ -271,7 +314,8 @@ export async function restore(id) {
|
|
|
271
314
|
const keep = []
|
|
272
315
|
try {
|
|
273
316
|
const manifest = await readManifest(dir)
|
|
274
|
-
|
|
317
|
+
manifest.entries = await entriesOf(dir, manifest)
|
|
318
|
+
for (const entry of manifest.entries) {
|
|
275
319
|
const src = join(dir, String(entry.rel || ''))
|
|
276
320
|
if (!entry.rel || entry.rel.includes('..') || entry.rel.includes('/') || entry.rel.includes('\\')) {
|
|
277
321
|
failed.push({ path: entry.orig, reason: 'bad manifest entry' })
|
|
@@ -317,7 +361,7 @@ async function countFiles(dir) {
|
|
|
317
361
|
let names = []
|
|
318
362
|
try { names = await readdir(dir) } catch { return 0 }
|
|
319
363
|
for (const name of names) {
|
|
320
|
-
if (name === 'manifest.json' || name === 'manifest.json.tmp') continue
|
|
364
|
+
if (name === 'manifest.json' || name === 'manifest.json.tmp' || name === JOURNAL) continue
|
|
321
365
|
const full = join(dir, name)
|
|
322
366
|
// lstat, not stat: a symlink inside a batch must not be followed and must
|
|
323
367
|
// not be counted as if it were one of the quarantined files.
|