dsh-sweeper 1.5.15 → 1.5.17

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 CHANGED
@@ -278,6 +278,8 @@ node test/browser/check.mjs # 需要本机有 Chrome/Edge,或 CHROME=<p
278
278
  ## 边界与自保
279
279
 
280
280
  - **工具的遍历上限会说出来**:模型自己找文件时走到 400 个条目上限会收到一句「只看了前 400 个」,不会把「没扫完」当成「没有」。
281
+ - **清理大目录不会越来越慢**:一次清理每搬一个文件只追加一行 `journal.jsonl`(而不是重写整份清单),搬完再折成一份 `manifest.json`;批次跑到一半崩溃时,`journal.jsonl` 仍是完整的记录,列表和恢复都认得它。
282
+ - **缓存与构建目录也点名**:`node_modules`、`.pnpm-store`、`.gradle`、`.m2`、`.cargo`、`.turbo`、`.next`、`dist` 等 21 个目录不遍历,跳过了多少个会写在结果末尾(「N 个缓存或构建目录没有搜」),免得模型以为整棵树都看过了。
281
283
  - **不敢跟的链接、读不到的条目会被数出来**:`list_dir` / `find_files` 遇到符号链接(可能指向工作目录之外)和权限不足的条目时不再静默跳过,而是写「N 个链接未跟随」「N 个条目读不出来」;整目录读不进去时单独写「N 个目录读不出来」,不会把目录说成文件。
282
284
  - **有日志读不出来,就不给「无脑删」**:一份会话日志打不开时,它可能用过任何文件;这一轮所有原本能一键清理的条目都会降到「需要你判断」,并在理由里写明原因。
283
285
  - **看不到的路径会被点名**:权限不足或文件被占用的路径不能判断,会写成「另有 N 个路径读不到」,不会悄悄少列。
package/lib/fs-tools.js CHANGED
@@ -9,7 +9,15 @@ import fs from 'node:fs'
9
9
  import path from 'node:path'
10
10
  import { WIN, normKey, isInside, baseOf } from './paths.js'
11
11
 
12
- const SKIP = new Set(['node_modules', '.git', '.venv', 'venv', '__pycache__', '.next', 'dist', 'build', 'target'])
12
+ // Directories that are not worth walking: package-manager caches, build output,
13
+ // and tool state. A conversation did not produce them, and they are large enough
14
+ // to eat the whole walk budget.
15
+ const SKIP = new Set([
16
+ 'node_modules', '.git', '.venv', 'venv', '__pycache__',
17
+ '.next', 'dist', 'build', 'target', 'coverage', 'obj',
18
+ '.pnpm-store', '.yarn', '.gradle', '.m2', '.cargo',
19
+ '.turbo', '.parcel-cache', '.nuxt', '.cache', '.idea', '.vs',
20
+ ])
13
21
  const MAX_ENTRIES = 60
14
22
  const MAX_WALK = 400
15
23
 
@@ -159,6 +167,7 @@ export function makeFsTools(opts) {
159
167
  let links = 0
160
168
  let unreadable = 0
161
169
  let unreadableDirs = 0
170
+ let skippedDirs = 0
162
171
  const walk = (dir, level) => {
163
172
  if (level > depth) return
164
173
  if (walked > MAX_WALK) { capped = true; return }
@@ -172,7 +181,7 @@ export function makeFsTools(opts) {
172
181
  try { st = lstatOf(full) } catch { unreadable += 1; continue }
173
182
  if (st.isSymbolicLink()) { links += 1; continue }
174
183
  if (st.isDirectory()) {
175
- if (SKIP.has(name.toLowerCase())) continue
184
+ if (SKIP.has(name.toLowerCase())) { skippedDirs += 1; continue }
176
185
  walk(full, level + 1)
177
186
  continue
178
187
  }
@@ -193,6 +202,7 @@ export function makeFsTools(opts) {
193
202
  const skippedNote = [
194
203
  links ? '… ' + links + ' symbolic link' + (links === 1 ? '' : 's') + ' not followed' : '',
195
204
  unreadableDirs ? '… ' + unreadableDirs + ' director' + (unreadableDirs === 1 ? 'y' : 'ies') + ' could not be read' : '',
205
+ skippedDirs ? '… ' + skippedDirs + ' cache or build director' + (skippedDirs === 1 ? 'y was' : 'ies were') + ' not searched' : '',
196
206
  unreadable ? '… ' + unreadable + ' entr' + (unreadable === 1 ? 'y' : 'ies') + ' could not be read' : '',
197
207
  ].filter(Boolean).join('\n')
198
208
  const extra = cappedNote + (skippedNote ? '\n' + skippedNote : '')
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(join(root, name)) } catch {
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(join(root, name), { withFileTypes: true }) } catch { files = [] }
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(root, name, f.name))).size } catch { /* ignore */ } }
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 = Array.isArray(manifest.entries) ? manifest.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
- // Write after every move so a crash never loses the mapping.
229
- try { await writeManifest(dir, manifest) } catch { /* best effort */ }
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
- for (const entry of Array.isArray(manifest.entries) ? manifest.entries : []) {
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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-sweeper",
3
- "version": "1.5.15",
3
+ "version": "1.5.17",
4
4
  "type": "module",
5
5
  "description": "清理对话留下的临时文件与垃圾,释放磁盘空间;只报告,清理进回收站可恢复 (cleanup junk files conversations leave on disk, free up space)",
6
6
  "keywords": [