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
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Agentic audit for one conversation.
|
|
2
|
+
//
|
|
3
|
+
// The single-prompt version could only judge the paths the scanner had already
|
|
4
|
+
// collected, which by design means small scratch files: a 900 MB checkpoint or
|
|
5
|
+
// a rendered video usually leaves no write/edit call behind, so it never made
|
|
6
|
+
// the list, and "no candidates" read as "nothing to clean". This module lets
|
|
7
|
+
// the model look instead: a bounded tool loop walks the conversation's own
|
|
8
|
+
// workspace, sees real sizes, and names what it believes is dead.
|
|
9
|
+
//
|
|
10
|
+
// Safety is unchanged in kind: the tools are read-only, scoped to that
|
|
11
|
+
// conversation's workspace, and everything the model names is re-checked on
|
|
12
|
+
// the server (exists, not a link, not tracked, not DSH state) before it can
|
|
13
|
+
// appear on the page. Anything large or outside a scratch pattern is forced
|
|
14
|
+
// into the "decide yourself" tier, never the one-click one.
|
|
15
|
+
import { makeFsTools, rootsFor } from './fs-tools.js'
|
|
16
|
+
import { runAgentLoop } from './agent.js'
|
|
17
|
+
import { normKey, isInside } from './paths.js'
|
|
18
|
+
|
|
19
|
+
const SYSTEM = [
|
|
20
|
+
'You audit one finished coding session and decide which files it left behind are now dead.',
|
|
21
|
+
'',
|
|
22
|
+
'You get a summary of the conversation and a numbered list of files it wrote. You may also',
|
|
23
|
+
'look around its workspace with the read-only tools: list_dir, stat_path, find_files.',
|
|
24
|
+
'Use them when the listed files are too small to matter or when you suspect a big artifact',
|
|
25
|
+
'(model checkpoints, rendered video, datasets, caches) that never appeared in a write call.',
|
|
26
|
+
'',
|
|
27
|
+
'How to look, in this order:',
|
|
28
|
+
' 1. list_dir the workspace root. Big artifacts are usually visible there.',
|
|
29
|
+
' 2. call find_files once with minBytes = 10485760 and depth = 3, so a large',
|
|
30
|
+
' file that never showed up in a write call still gets a chance to be seen.',
|
|
31
|
+
' 3. open one level deeper only where the sizes look suspicious.',
|
|
32
|
+
'',
|
|
33
|
+
'Rules:',
|
|
34
|
+
'- Only name a path you have seen in the list or through a tool call. Never invent a path.',
|
|
35
|
+
'- Never name a conversation log, an attachment, anything under the DSH state directory,',
|
|
36
|
+
' a git-tracked file, source code, configuration, documentation, or tests.',
|
|
37
|
+
'- Prefer keep over delete when the summary suggests the user still needs the file.',
|
|
38
|
+
'- Sizes matter: a large file deserves an explicit reason, because deleting it is expensive.',
|
|
39
|
+
'- Many copies of the same big file (build outputs, several dated release folders) are a',
|
|
40
|
+
' strong signal; say which ones and why.',
|
|
41
|
+
'',
|
|
42
|
+
'Answer EXACTLY with one line per file and nothing else:',
|
|
43
|
+
' S <path or iN> | <one-line reason> file is clearly disposable',
|
|
44
|
+
' O <path or iN> | <one-line reason> probably disposable, user should decide',
|
|
45
|
+
' K <path or iN> | <one-line reason> keep it',
|
|
46
|
+
].join('\n')
|
|
47
|
+
|
|
48
|
+
const MAX_LINES = 40
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Ask the model to audit one conversation with tools available.
|
|
52
|
+
*
|
|
53
|
+
* @param {object} ctx - host context.
|
|
54
|
+
* @param {object} info - describeSession() output.
|
|
55
|
+
* @param {string} digest - buildDigest() text.
|
|
56
|
+
* @param {object} opts - { route, timeoutMs, signal, maxTurns, maxCalls, onCall }.
|
|
57
|
+
* @returns {Promise<{verdict: object, turns: number, calls: number, ms: number, trace: Array}>}
|
|
58
|
+
*/
|
|
59
|
+
export async function auditWithTools(ctx, info, digest, opts = {}) {
|
|
60
|
+
const roots = rootsFor(info.cwd, opts.extraRoots)
|
|
61
|
+
const tools = makeFsTools({ roots, deny: opts.deny || [] })
|
|
62
|
+
const started = Date.now()
|
|
63
|
+
const goal = [
|
|
64
|
+
digest,
|
|
65
|
+
'',
|
|
66
|
+
'Workspace of this conversation: ' + (info.cwd || '(unknown)'),
|
|
67
|
+
'',
|
|
68
|
+
'Decide now. Use the tools first if you need to see sizes or look deeper, then answer with',
|
|
69
|
+
'the S/O/K lines only. Anything larger than a few megabytes deserves an explicit reason.',
|
|
70
|
+
].join('\n')
|
|
71
|
+
const result = await runAgentLoop(ctx, {
|
|
72
|
+
system: SYSTEM,
|
|
73
|
+
goal,
|
|
74
|
+
route: opts.route,
|
|
75
|
+
tools,
|
|
76
|
+
maxTurns: opts.maxTurns,
|
|
77
|
+
maxCalls: opts.maxCalls,
|
|
78
|
+
timeoutMs: opts.timeoutMs,
|
|
79
|
+
signal: opts.signal,
|
|
80
|
+
onCall: opts.onCall,
|
|
81
|
+
})
|
|
82
|
+
return {
|
|
83
|
+
verdict: parseVerdict(result.text),
|
|
84
|
+
turns: result.turns,
|
|
85
|
+
calls: result.calls,
|
|
86
|
+
ms: Date.now() - started,
|
|
87
|
+
trace: result.trace,
|
|
88
|
+
text: result.text,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Parse the S/O/K lines. A path after the letter may be an id (`i3`) or an
|
|
94
|
+
* absolute path the model discovered; the caller decides which of those it can
|
|
95
|
+
* accept.
|
|
96
|
+
*/
|
|
97
|
+
export function parseVerdict(text) {
|
|
98
|
+
const out = { sweep: [], optional: [], keep: [], garbage: [] }
|
|
99
|
+
const lines = String(text || '').split(/\r?\n/)
|
|
100
|
+
for (const rawLine of lines) {
|
|
101
|
+
const line = rawLine.replace(/^\s*(?:[-*•]|\d+[.)])\s*/, '').trim()
|
|
102
|
+
if (line === '') continue
|
|
103
|
+
const match = /^([SOK])\s+(.+?)\s*(?:\|\s*(.*))?$/i.exec(line)
|
|
104
|
+
if (!match) { out.garbage.push(line.slice(0, 120)); continue }
|
|
105
|
+
const bucket = match[1].toUpperCase()
|
|
106
|
+
let target = match[2].trim().replace(/^[`'"]+|[`'"]+$/g, '').replace(/[.,;]+$/, '')
|
|
107
|
+
const reason = (match[3] || '').trim().slice(0, 160) || 'model judgement'
|
|
108
|
+
if (target === '') { out.garbage.push(line.slice(0, 120)); continue }
|
|
109
|
+
const entry = { id: target, reason }
|
|
110
|
+
if (bucket === 'S') out.sweep.push(entry)
|
|
111
|
+
else if (bucket === 'O') out.optional.push(entry)
|
|
112
|
+
else out.keep.push(entry)
|
|
113
|
+
}
|
|
114
|
+
for (const key of ['sweep', 'optional', 'keep']) out[key] = out[key].slice(0, MAX_LINES)
|
|
115
|
+
return out
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** True when the model answered with a path rather than a list id. */
|
|
119
|
+
export function looksLikePath(target) {
|
|
120
|
+
return /^[A-Za-z]:[\\/]/.test(String(target || '')) || String(target || '').startsWith('/')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Keep only the paths that live inside the conversation's own workspace. */
|
|
124
|
+
export function insideWorkspace(target, cwd) {
|
|
125
|
+
if (!looksLikePath(target) || !cwd) return false
|
|
126
|
+
const key = normKey(target)
|
|
127
|
+
const root = normKey(cwd)
|
|
128
|
+
return !!key && !!root && (key === root || isInside(key, root))
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export const _internal = { SYSTEM, parseVerdict }
|