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/fs-tools.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// Read-only filesystem tools for the model loop.
|
|
2
|
+
//
|
|
3
|
+
// Everything here is a read: directory listing, one stat, and a bounded walk.
|
|
4
|
+
// No writes, no rename, no deletes, no shell. Paths are clamped to the roots a
|
|
5
|
+
// single conversation owns, and DSH's own state is refused even inside those
|
|
6
|
+
// roots, so a model cannot be talked into listing ~/.dsh by a session log that
|
|
7
|
+
// happens to contain that path.
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import { WIN, normKey, isInside, baseOf } from './paths.js'
|
|
11
|
+
|
|
12
|
+
const SKIP = new Set(['node_modules', '.git', '.venv', 'venv', '__pycache__', '.next', 'dist', 'build', 'target'])
|
|
13
|
+
const MAX_ENTRIES = 60
|
|
14
|
+
const MAX_WALK = 400
|
|
15
|
+
|
|
16
|
+
function human(bytes) {
|
|
17
|
+
const n = Number(bytes) || 0
|
|
18
|
+
if (n >= 1024 * 1024 * 1024) return (n / 1024 / 1024 / 1024).toFixed(1) + ' GB'
|
|
19
|
+
if (n >= 1024 * 1024) return (n / 1024 / 1024).toFixed(1) + ' MB'
|
|
20
|
+
if (n >= 1024) return (n / 1024).toFixed(1) + ' KB'
|
|
21
|
+
return n + ' B'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function day(ms) {
|
|
25
|
+
const n = Number(ms)
|
|
26
|
+
if (!Number.isFinite(n) || n <= 0) return 'unknown'
|
|
27
|
+
return new Date(n).toISOString().slice(0, 10)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Build the tool set for one conversation.
|
|
32
|
+
*
|
|
33
|
+
* @param {object} opts
|
|
34
|
+
* @param {string[]} opts.roots - absolute directories this conversation may look at.
|
|
35
|
+
* @param {string[]} opts.deny - absolute directories that are never listed (DSH state).
|
|
36
|
+
*/
|
|
37
|
+
export function makeFsTools(opts) {
|
|
38
|
+
const roots = (opts.roots || []).filter((root) => typeof root === 'string' && root.length > 0).map((root) => normKey(root))
|
|
39
|
+
const deny = (opts.deny || []).filter((root) => typeof root === 'string' && root.length > 0).map((root) => normKey(root))
|
|
40
|
+
|
|
41
|
+
/** Resolve and authorise one path, or explain why not. */
|
|
42
|
+
function allow(raw) {
|
|
43
|
+
const text = String(raw || '').trim()
|
|
44
|
+
if (text === '') return { error: 'path is required' }
|
|
45
|
+
if (/[\n\r\u0000]/.test(text)) return { error: 'path contains control characters' }
|
|
46
|
+
let real
|
|
47
|
+
try {
|
|
48
|
+
real = fs.realpathSync.native ? fs.realpathSync.native(text) : fs.realpathSync(text)
|
|
49
|
+
} catch {
|
|
50
|
+
return { error: 'no such file or directory: ' + text }
|
|
51
|
+
}
|
|
52
|
+
const key = normKey(real)
|
|
53
|
+
if (!key) return { error: 'not an absolute path: ' + text }
|
|
54
|
+
for (const bad of deny) if (isInside(key, bad)) return { error: 'refused: that path belongs to DSH itself' }
|
|
55
|
+
if (roots.length > 0 && !roots.some((root) => isInside(key, root) || key === root)) {
|
|
56
|
+
return { error: 'refused: that path is outside the directories this conversation wrote to' }
|
|
57
|
+
}
|
|
58
|
+
return { key, real }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const listDir = {
|
|
62
|
+
name: 'list_dir',
|
|
63
|
+
description: 'List one directory: names, sizes, and last-modified dates, largest first. Use this to see what a run left behind before deciding what is dead.',
|
|
64
|
+
parameters: {
|
|
65
|
+
type: 'object',
|
|
66
|
+
properties: {
|
|
67
|
+
path: { type: 'string', description: 'Absolute directory path.' },
|
|
68
|
+
limit: { type: 'integer', description: 'How many entries to show, 1-60.' },
|
|
69
|
+
},
|
|
70
|
+
required: ['path'],
|
|
71
|
+
additionalProperties: false,
|
|
72
|
+
},
|
|
73
|
+
run(input) {
|
|
74
|
+
const ok = allow(input && input.path)
|
|
75
|
+
if (ok.error) throw new Error(ok.error)
|
|
76
|
+
const st = fs.lstatSync(ok.real)
|
|
77
|
+
if (!st.isDirectory()) throw new Error('not a directory: ' + input.path)
|
|
78
|
+
const limit = Math.max(1, Math.min(MAX_ENTRIES, Number(input.limit) || MAX_ENTRIES))
|
|
79
|
+
const rows = []
|
|
80
|
+
for (const name of fs.readdirSync(ok.real)) {
|
|
81
|
+
let item
|
|
82
|
+
try { item = fs.lstatSync(path.join(ok.real, name)) } catch { continue }
|
|
83
|
+
if (item.isSymbolicLink()) continue
|
|
84
|
+
rows.push({ name, size: item.isDirectory() ? -1 : item.size, dir: item.isDirectory(), mtime: item.mtimeMs })
|
|
85
|
+
}
|
|
86
|
+
rows.sort((a, b) => (b.size - a.size) || a.name.localeCompare(b.name))
|
|
87
|
+
const total = rows.length
|
|
88
|
+
const shown = rows.slice(0, limit)
|
|
89
|
+
const lines = shown.map((row) => (row.dir ? ' dir ' : human(row.size).padStart(9)) + ' ' + day(row.mtime) + ' ' + row.name)
|
|
90
|
+
return ok.real + '\n' + lines.join('\n') + (total > shown.length ? '\n… ' + (total - shown.length) + ' more entries not shown' : '')
|
|
91
|
+
},
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const statPath = {
|
|
95
|
+
name: 'stat_path',
|
|
96
|
+
description: 'Report one path: whether it is a file or directory, its size, and when it was last modified.',
|
|
97
|
+
parameters: {
|
|
98
|
+
type: 'object',
|
|
99
|
+
properties: { path: { type: 'string', description: 'Absolute path.' } },
|
|
100
|
+
required: ['path'],
|
|
101
|
+
additionalProperties: false,
|
|
102
|
+
},
|
|
103
|
+
run(input) {
|
|
104
|
+
const ok = allow(input && input.path)
|
|
105
|
+
if (ok.error) throw new Error(ok.error)
|
|
106
|
+
const st = fs.lstatSync(ok.real)
|
|
107
|
+
return [
|
|
108
|
+
'path: ' + ok.real,
|
|
109
|
+
'kind: ' + (st.isDirectory() ? 'directory' : st.isFile() ? 'file' : 'other'),
|
|
110
|
+
'size: ' + (st.isDirectory() ? '(directory)' : human(st.size)),
|
|
111
|
+
'modified: ' + day(st.mtimeMs),
|
|
112
|
+
].join('\n')
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const findFiles = {
|
|
117
|
+
name: 'find_files',
|
|
118
|
+
description: 'Walk a directory tree and list files matching a name pattern and/or a minimum size. Use minBytes to hunt large leftovers (e.g. 52428800 for 50 MB).',
|
|
119
|
+
parameters: {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: {
|
|
122
|
+
path: { type: 'string', description: 'Absolute directory to start from.' },
|
|
123
|
+
pattern: { type: 'string', description: 'Case-insensitive substring of the file name, or a wildcard like *.bin.' },
|
|
124
|
+
minBytes: { type: 'integer', description: 'Only report files at least this large.' },
|
|
125
|
+
depth: { type: 'integer', description: 'How deep to walk, 1-6. Default 3.' },
|
|
126
|
+
limit: { type: 'integer', description: 'How many files to show, 1-60.' },
|
|
127
|
+
},
|
|
128
|
+
required: ['path'],
|
|
129
|
+
additionalProperties: false,
|
|
130
|
+
},
|
|
131
|
+
run(input) {
|
|
132
|
+
const ok = allow(input && input.path)
|
|
133
|
+
if (ok.error) throw new Error(ok.error)
|
|
134
|
+
if (!fs.lstatSync(ok.real).isDirectory()) throw new Error('not a directory: ' + input.path)
|
|
135
|
+
const depth = Math.max(1, Math.min(6, Number(input.depth) || 3))
|
|
136
|
+
const limit = Math.max(1, Math.min(MAX_ENTRIES, Number(input.limit) || MAX_ENTRIES))
|
|
137
|
+
const minBytes = Math.max(0, Number(input.minBytes) || 0)
|
|
138
|
+
const pattern = String((input && input.pattern) || '').trim()
|
|
139
|
+
const wildcard = pattern.includes('*') ? new RegExp('^' + pattern.split('*').map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('.*') + '$', 'i') : null
|
|
140
|
+
const hit = (name) => {
|
|
141
|
+
if (!pattern) return true
|
|
142
|
+
return wildcard ? wildcard.test(name) : name.toLowerCase().includes(pattern.toLowerCase())
|
|
143
|
+
}
|
|
144
|
+
const found = []
|
|
145
|
+
let walked = 0
|
|
146
|
+
const walk = (dir, level) => {
|
|
147
|
+
if (level > depth || walked > MAX_WALK) return
|
|
148
|
+
let names
|
|
149
|
+
try { names = fs.readdirSync(dir) } catch { return }
|
|
150
|
+
for (const name of names) {
|
|
151
|
+
walked += 1
|
|
152
|
+
if (walked > MAX_WALK) return
|
|
153
|
+
const full = path.join(dir, name)
|
|
154
|
+
let st
|
|
155
|
+
try { st = fs.lstatSync(full) } catch { continue }
|
|
156
|
+
if (st.isSymbolicLink()) continue
|
|
157
|
+
if (st.isDirectory()) {
|
|
158
|
+
if (SKIP.has(name.toLowerCase())) continue
|
|
159
|
+
walk(full, level + 1)
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
if (!st.isFile()) continue
|
|
163
|
+
if (st.size < minBytes) continue
|
|
164
|
+
if (!hit(name)) continue
|
|
165
|
+
found.push({ full, size: st.size, mtime: st.mtimeMs })
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
walk(ok.real, 1)
|
|
169
|
+
found.sort((a, b) => b.size - a.size)
|
|
170
|
+
const shown = found.slice(0, limit)
|
|
171
|
+
if (shown.length === 0) return ok.real + '\nno file matched'
|
|
172
|
+
return ok.real + '\n' + shown.map((row) => human(row.size).padStart(9) + ' ' + day(row.mtime) + ' ' + row.full).join('\n')
|
|
173
|
+
+ (found.length > shown.length ? '\n… ' + (found.length - shown.length) + ' more matches not shown' : '')
|
|
174
|
+
},
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return [listDir, statPath, findFiles]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The directories a conversation may look at: its workspace plus scratch subtrees. */
|
|
181
|
+
export function rootsFor(cwd, extra) {
|
|
182
|
+
const out = []
|
|
183
|
+
const add = (value) => {
|
|
184
|
+
const text = String(value || '').trim()
|
|
185
|
+
if (text === '') return
|
|
186
|
+
if (!out.some((seen) => normKey(seen) === normKey(text))) out.push(text)
|
|
187
|
+
}
|
|
188
|
+
add(cwd)
|
|
189
|
+
for (const value of extra || []) add(value)
|
|
190
|
+
return out
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Short label for a path, used in traces so a log line stays one line. */
|
|
194
|
+
export function labelOf(target) {
|
|
195
|
+
return baseOf(String(target || ''))
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export const _internal = { human, day, WIN }
|