dsh-sweeper 0.0.0-stage → 1.4.1
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 +358 -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/paths.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path rules that work on every OS the desktop app runs on.
|
|
3
|
+
*
|
|
4
|
+
* Nothing here is machine specific: roots always come from the environment
|
|
5
|
+
* (DSH_HOME / home directory / temp dir) or from paths found in session logs,
|
|
6
|
+
* so the same bundle behaves the same on a fresh computer.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const WIN = process.platform === 'win32'
|
|
10
|
+
|
|
11
|
+
const WIN_DRIVE = /^[A-Za-z]:[\\/]/
|
|
12
|
+
const WIN_UNC = /^\\\\[^\\/]+[\\/][^\\/]+/
|
|
13
|
+
const POSIX_ABS = /^\//
|
|
14
|
+
|
|
15
|
+
/** Absolute path on the current platform, plus Windows shapes when running on Windows. */
|
|
16
|
+
export function isAbsPath(p) {
|
|
17
|
+
if (typeof p !== 'string' || !p) return false
|
|
18
|
+
if (WIN) return WIN_DRIVE.test(p) || WIN_UNC.test(p)
|
|
19
|
+
return POSIX_ABS.test(p)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Comparison key for a path: one separator, no trailing dots/spaces, and
|
|
24
|
+
* lower case only where the filesystem is case insensitive.
|
|
25
|
+
*/
|
|
26
|
+
export function normKey(p) {
|
|
27
|
+
if (typeof p !== 'string' || !p) return null
|
|
28
|
+
let s = p.trim()
|
|
29
|
+
if (WIN) s = s.replace(/\//g, '\\').replace(/\\{2,}/g, '\\')
|
|
30
|
+
else s = s.replace(/\/{2,}/g, '/')
|
|
31
|
+
if (!isAbsPath(s)) return null
|
|
32
|
+
s = s.replace(/[. ]+$/, '')
|
|
33
|
+
// A trailing separator means the same directory: D:\a\b\ and D:\a\b must
|
|
34
|
+
// compare equal, or one of them sneaks past a boundary check.
|
|
35
|
+
const root = WIN ? (WIN_DRIVE.test(s) ? s.slice(0, 3) : '') : '/'
|
|
36
|
+
const rest = s.slice(root.length).replace(/[\\/]+$/, '')
|
|
37
|
+
s = root + rest
|
|
38
|
+
return WIN ? s.toLowerCase() : s
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Same separators as normKey but preserves case (for showing paths to people). */
|
|
42
|
+
export function tidyPath(p) {
|
|
43
|
+
const s = String(p || '').trim()
|
|
44
|
+
if (!s) return s
|
|
45
|
+
return WIN ? s.replace(/\//g, '\\') : s.replace(/\/{2,}/g, '/')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Everything before the last separator. */
|
|
49
|
+
export function dirOf(p) {
|
|
50
|
+
const s = tidyPath(p)
|
|
51
|
+
const i = WIN ? s.lastIndexOf('\\') : s.lastIndexOf('/')
|
|
52
|
+
return i > 0 ? s.slice(0, i) : s
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** File name without its directory. */
|
|
56
|
+
export function baseOf(p) {
|
|
57
|
+
const s = tidyPath(p)
|
|
58
|
+
const i = WIN ? s.lastIndexOf('\\') : s.lastIndexOf('/')
|
|
59
|
+
return i >= 0 ? s.slice(i + 1) : s
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Join with the platform separator without doubling it. */
|
|
63
|
+
export function joinPath(dir, name) {
|
|
64
|
+
const d = tidyPath(dir).replace(WIN ? /\\+$/ : /\/+$/, '')
|
|
65
|
+
return d + (WIN ? '\\' : '/') + name
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Number of segments below the filesystem root (C:\a\b -> 2, /a/b -> 2). */
|
|
69
|
+
export function depthOf(p) {
|
|
70
|
+
const s = tidyPath(p)
|
|
71
|
+
const root = WIN ? (WIN_DRIVE.test(s) ? s.slice(0, 3) : '') : '/'
|
|
72
|
+
const rest = s.slice(root.length)
|
|
73
|
+
return rest.split(/[\\/]+/).filter(Boolean).length
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** True when `child` sits inside `parent` (or is the same path). */
|
|
77
|
+
export function isInside(child, parent) {
|
|
78
|
+
const a = normKey(child)
|
|
79
|
+
const b = normKey(parent)
|
|
80
|
+
if (!a || !b) return false
|
|
81
|
+
if (a === b) return true
|
|
82
|
+
return a.startsWith(b.endsWith(WIN ? '\\' : '/') ? b : b + (WIN ? '\\' : '/'))
|
|
83
|
+
}
|
package/lib/review.js
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-sweeper · model review (dynamic pass).
|
|
3
|
+
*
|
|
4
|
+
* The scan is deterministic; this module adds one optional opinion on top of it.
|
|
5
|
+
* It sends the candidate list to the host's configured model and asks for a
|
|
6
|
+
* line-oriented verdict. Nothing here touches the filesystem: the caller decides,
|
|
7
|
+
* and only `tier === 'safe'` items may be acted on automatically.
|
|
8
|
+
*
|
|
9
|
+
* Safety model (assume the model can be wrong or adversarial):
|
|
10
|
+
* - the model only ever sees ids from the current report;
|
|
11
|
+
* - ids are re-validated against that report;
|
|
12
|
+
* - a model verdict of `delete` on an `unsure` item is downgraded to a question;
|
|
13
|
+
* - the prompt forbids inventing paths, commands, or deletions outside the list.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { randomUUID } from 'node:crypto'
|
|
17
|
+
import { normKey } from './paths.js'
|
|
18
|
+
|
|
19
|
+
const SYSTEM = `你是 DSH 插件 dsh-sweeper 的「文件复核员」。唯一任务:在给定的候选清单里,判断哪些是可以安全删除的临时垃圾。
|
|
20
|
+
|
|
21
|
+
硬性约束(违反任意一条都算失败):
|
|
22
|
+
1. 只能使用候选清单里出现的 id,禁止提到清单外的路径、目录、文件名、通配符或命令。
|
|
23
|
+
2. 禁止输出 shell / PowerShell / Python 代码,也不要建议"执行删除命令"。插件自己负责把文件移进回收站。
|
|
24
|
+
3. 绝对不能建议删除:会话记录、聊天历史、附件、.dsh 目录内的任何内容、被 git 跟踪的文件、用户文档/图片/数据、正式源码。
|
|
25
|
+
4. 除了下列格式的行,不要输出任何其它文字(不要解释、不要标题、不要 markdown 代码块)。
|
|
26
|
+
|
|
27
|
+
清单分两段:
|
|
28
|
+
A 段「候选」的 id 形如 i12,用下面三种回答:
|
|
29
|
+
D <id> ← 可以安全删除
|
|
30
|
+
K <id> ← 保留
|
|
31
|
+
Q <id> <问题> ← 拿不准,用一句简短中文问用户
|
|
32
|
+
|
|
33
|
+
B 段「清单外观察到的文件」的 id 形如 x7,等你主动判断要不要清理:
|
|
34
|
+
A <id> ← 可以无脑清理(明确的构建/临时/缓存产物,删了不会有任何损失)
|
|
35
|
+
S <id> ← 可选清理(像是垃圾,但用户可能还要,让用户决定)
|
|
36
|
+
K <id> ← 保留(源码、配置、数据、文档,或看不出是垃圾)
|
|
37
|
+
|
|
38
|
+
B 段判断要更保守:只根据路径、文件名、大小、闲置天数判断;拿不准就 K,不要为了让清单变长而瞎猜。绝对不要给出 B 段之外的任何 id。
|
|
39
|
+
|
|
40
|
+
判断标准:
|
|
41
|
+
- 一次性临时产物(.scratch/、tmp/、__pycache__、*.bak、调试脚本 shot-*.mjs、repro-*.py 这类)→ D
|
|
42
|
+
- 看起来属于某个项目的正式源码、配置、文档、数据集 → K
|
|
43
|
+
- 删除后用户可能后悔、或信息不足以判断 → Q`
|
|
44
|
+
|
|
45
|
+
// A stand-in conversation id: some providers (opencode-go) require a session
|
|
46
|
+
// header for routing, but this plugin never reads or writes a real session log.
|
|
47
|
+
const REVIEW_SESSION_ID = randomUUID()
|
|
48
|
+
|
|
49
|
+
const MAX_ITEMS = 100
|
|
50
|
+
const MAX_HINTS = 80
|
|
51
|
+
const MAX_TOKENS = 4000
|
|
52
|
+
const TIMEOUT_MS = 120_000
|
|
53
|
+
|
|
54
|
+
function stripFence(text) {
|
|
55
|
+
const s = String(text || '').trim()
|
|
56
|
+
const m = s.match(/```(?:[a-z]*)\s*([\s\S]*?)```/i)
|
|
57
|
+
return (m ? m[1] : s).trim()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Line protocol: `D <id>` / `K <id>` / `Q <id> <question>`. Tolerates bullets and numbering. */
|
|
61
|
+
function parseLines(text) {
|
|
62
|
+
const out = { delete: [], keep: [], ask: [], sweep: [], optional: [], note: '' }
|
|
63
|
+
let matched = 0
|
|
64
|
+
let ignored = 0
|
|
65
|
+
for (const rawLine of String(text || '').split(/\r?\n/)) {
|
|
66
|
+
const line = rawLine.trim().replace(/^[-*•]\s*/, '').replace(/^\d+[.)]\s*/, '').trim()
|
|
67
|
+
if (!line) continue
|
|
68
|
+
const m = line.match(/^([DKOQASdkoqas])\s*[::]?\s+(\S+)\s*(.*)$/)
|
|
69
|
+
if (!m) { ignored++; continue }
|
|
70
|
+
matched++
|
|
71
|
+
const mark = m[1].toUpperCase()
|
|
72
|
+
const id = m[2]
|
|
73
|
+
const rest = (m[3] || '').trim()
|
|
74
|
+
if (mark === 'D') out.delete.push(id)
|
|
75
|
+
else if (mark === 'A') out.sweep.push(id)
|
|
76
|
+
else if (mark === 'S') out.optional.push(id)
|
|
77
|
+
else if (mark === 'K') out.keep.push(id)
|
|
78
|
+
else out.ask.push({ id, question: rest.slice(0, 300) })
|
|
79
|
+
}
|
|
80
|
+
return { verdict: out, matched, ignored }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function parseJSONish(text) {
|
|
84
|
+
const raw = stripFence(text)
|
|
85
|
+
const start = raw.indexOf('{')
|
|
86
|
+
const end = raw.lastIndexOf('}')
|
|
87
|
+
if (start < 0 || end <= start) return null
|
|
88
|
+
try {
|
|
89
|
+
const parsed = JSON.parse(raw.slice(start, end + 1))
|
|
90
|
+
const ids = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === 'string').map((x) => x.trim()) : [])
|
|
91
|
+
return {
|
|
92
|
+
delete: ids(parsed.delete),
|
|
93
|
+
sweep: ids(parsed.sweep),
|
|
94
|
+
optional: ids(parsed.optional),
|
|
95
|
+
keep: ids(parsed.keep),
|
|
96
|
+
ask: Array.isArray(parsed.ask)
|
|
97
|
+
? parsed.ask.filter((a) => a && typeof a === 'object' && typeof a.id === 'string')
|
|
98
|
+
.map((a) => ({ id: String(a.id).trim(), question: String(a.question || '').slice(0, 300) }))
|
|
99
|
+
: [],
|
|
100
|
+
note: typeof parsed.note === 'string' ? parsed.note.slice(0, 500) : '',
|
|
101
|
+
}
|
|
102
|
+
} catch { return null }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function parseVerdict(text) {
|
|
106
|
+
if (!String(text || '').trim()) throw new Error('model returned an empty answer')
|
|
107
|
+
const lines = parseLines(text)
|
|
108
|
+
if (lines.matched > 0) return { ...lines.verdict, matched: lines.matched, ignored: lines.ignored }
|
|
109
|
+
const json = parseJSONish(text)
|
|
110
|
+
if (json) return { ...json, matched: json.delete.length + json.keep.length + json.ask.length, ignored: 0 }
|
|
111
|
+
throw new Error('model answer has no decisions (first 200 chars: ' + String(text).trim().slice(0, 200) + ')')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function resolveRoute(ctx, override) {
|
|
115
|
+
const pick = (v) => (typeof v === 'string' && v.trim() ? v.trim() : '')
|
|
116
|
+
if (pick(override && override.provider) && pick(override && override.model)) {
|
|
117
|
+
return { provider: pick(override.provider), model: pick(override.model) }
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const svc = typeof ctx.get === 'function' ? ctx.get('agentDefaultModel') : null
|
|
121
|
+
const cur = svc && typeof svc.currentSelection === 'function' ? svc.currentSelection() : null
|
|
122
|
+
if (cur && pick(cur.provider) && pick(cur.model)) {
|
|
123
|
+
return { provider: pick(cur.provider), model: pick(cur.model) }
|
|
124
|
+
}
|
|
125
|
+
} catch { /* fall through */ }
|
|
126
|
+
throw new Error('no model route: configure provider/model in the plugin row or set a default model')
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function collectText(chunk, acc) {
|
|
130
|
+
if (!chunk || typeof chunk !== 'object') return
|
|
131
|
+
acc.types[chunk.type] = (acc.types[chunk.type] || 0) + 1
|
|
132
|
+
if (chunk.type === 'text-delta' && typeof chunk.text === 'string') { acc.deltas += chunk.text; return }
|
|
133
|
+
if (chunk.type === 'block-end' && chunk.block && chunk.block.type === 'text' && typeof chunk.block.text === 'string') {
|
|
134
|
+
acc.blocks += chunk.block.text
|
|
135
|
+
}
|
|
136
|
+
if (chunk.type === 'finish' && acc.finish === null) acc.finish = JSON.stringify(chunk).slice(0, 400)
|
|
137
|
+
if (chunk.type === 'error' && acc.error === null) acc.error = JSON.stringify(chunk).slice(0, 400)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Ask the configured model to triage the current candidate list.
|
|
142
|
+
* @param {object} ctx host context (needs `llm`, optionally `agentDefaultModel`).
|
|
143
|
+
* @param {object} report the current scan report.
|
|
144
|
+
* @param {{model?: {provider?: string, model?: string}, timeoutMs?: number}} opts
|
|
145
|
+
*/
|
|
146
|
+
export async function review(ctx, report, opts = {}) {
|
|
147
|
+
const llm = typeof ctx.get === 'function' ? ctx.get('llm') : null
|
|
148
|
+
if (!llm || typeof llm.prepareCall !== 'function') throw new Error('the host llm service is not available')
|
|
149
|
+
const route = resolveRoute(ctx, opts.model)
|
|
150
|
+
const items = (report.items || []).slice(0, MAX_ITEMS)
|
|
151
|
+
if (!items.length) {
|
|
152
|
+
return { model: route, verdict: { delete: [], keep: [], ask: [], note: '', matched: 0, ignored: 0 }, reviewed: 0, skipped: 0, ms: 0 }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const payload = items.map((it, i) => ({
|
|
156
|
+
id: 'i' + (i + 1),
|
|
157
|
+
p: it.path,
|
|
158
|
+
t: it.tier,
|
|
159
|
+
k: it.kind,
|
|
160
|
+
b: it.size,
|
|
161
|
+
d: it.ageDays,
|
|
162
|
+
u: it.uses,
|
|
163
|
+
f: !!it.failed,
|
|
164
|
+
}))
|
|
165
|
+
const hints = (report.hints || []).slice(0, MAX_HINTS)
|
|
166
|
+
const extra = hints.map((h, i) => ({
|
|
167
|
+
id: 'x' + (i + 1),
|
|
168
|
+
p: h.path,
|
|
169
|
+
b: h.size,
|
|
170
|
+
d: h.ageDays,
|
|
171
|
+
}))
|
|
172
|
+
const user = [
|
|
173
|
+
'字段含义:id=编号(原样复制),p=路径,t=档位(safe=无脑删 / unsure=需确认),k=来源,b=字节数,d=闲置天数,u=产生后被工具使用次数,f=产生它那次调用是否失败。',
|
|
174
|
+
'',
|
|
175
|
+
'A 段 · 候选清单(这些已经被规则筛过一轮):',
|
|
176
|
+
JSON.stringify(payload),
|
|
177
|
+
'',
|
|
178
|
+
'B 段 · 清单外观察到的文件(在已知临时目录里、但任何会话日志都没提到过;只根据路径和闲置天数判断):',
|
|
179
|
+
JSON.stringify(extra),
|
|
180
|
+
'',
|
|
181
|
+
'请按格式逐行输出判断。',
|
|
182
|
+
].join('\n')
|
|
183
|
+
|
|
184
|
+
const started = Date.now()
|
|
185
|
+
const controller = new AbortController()
|
|
186
|
+
const timeoutMs = Number(opts.timeoutMs) > 0 ? Number(opts.timeoutMs) : TIMEOUT_MS
|
|
187
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
|
188
|
+
const acc = { deltas: '', blocks: '', types: {}, finish: null, error: null }
|
|
189
|
+
try {
|
|
190
|
+
const call = await llm.prepareCall({ ...route, maxTokens: MAX_TOKENS }, controller.signal, (controls) => controls)
|
|
191
|
+
const messages = [{
|
|
192
|
+
id: randomUUID(),
|
|
193
|
+
role: 'user',
|
|
194
|
+
content: [{ type: 'text', text: user }],
|
|
195
|
+
source: { kind: 'dsh-sweeper' },
|
|
196
|
+
}]
|
|
197
|
+
for await (const chunk of call.stream({
|
|
198
|
+
...call.config,
|
|
199
|
+
messages,
|
|
200
|
+
system: SYSTEM,
|
|
201
|
+
sessionId: REVIEW_SESSION_ID,
|
|
202
|
+
purpose: 'sweeper-review',
|
|
203
|
+
signal: controller.signal,
|
|
204
|
+
})) collectText(chunk, acc)
|
|
205
|
+
} catch (err) {
|
|
206
|
+
if (controller.signal.aborted) throw new Error('model call timed out after ' + Math.round(timeoutMs / 1000) + 's')
|
|
207
|
+
throw err
|
|
208
|
+
} finally {
|
|
209
|
+
clearTimeout(timer)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const text = acc.deltas || acc.blocks
|
|
213
|
+
const trace = { types: acc.types, finish: acc.finish, error: acc.error, chars: text.length }
|
|
214
|
+
let verdict
|
|
215
|
+
try { verdict = parseVerdict(text) } catch (err) {
|
|
216
|
+
const wrapped = new Error((err && err.message) || String(err))
|
|
217
|
+
wrapped.trace = trace
|
|
218
|
+
throw wrapped
|
|
219
|
+
}
|
|
220
|
+
const translated = translateVerdict(verdict, items, hints)
|
|
221
|
+
return {
|
|
222
|
+
model: route,
|
|
223
|
+
verdict: translated.verdict,
|
|
224
|
+
proposals: translated.proposals,
|
|
225
|
+
hints: hints.length,
|
|
226
|
+
unknownIds: translated.unknown,
|
|
227
|
+
reviewed: items.length,
|
|
228
|
+
skipped: (report.items || []).length - items.length,
|
|
229
|
+
ms: Date.now() - started,
|
|
230
|
+
answerChars: text.length,
|
|
231
|
+
raw: String(text || '').slice(0, 6000),
|
|
232
|
+
trace,
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function normPath(p) {
|
|
237
|
+
return normKey(p)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Map whatever the model wrote back onto report keys.
|
|
242
|
+
* Accepts a short label (i3), the key itself, or a case/separator variant of the path.
|
|
243
|
+
*/
|
|
244
|
+
function translateVerdict(verdict, items, hints = []) {
|
|
245
|
+
const hintByLabel = new Map(hints.map((h, i) => ['x' + (i + 1), h]))
|
|
246
|
+
const hintByKey = new Map(hints.map((h) => [h.key, h]))
|
|
247
|
+
const hintOf = (label) => {
|
|
248
|
+
if (typeof label !== 'string') return null
|
|
249
|
+
const raw = label.trim().toLowerCase()
|
|
250
|
+
if (!raw) return null
|
|
251
|
+
if (hintByLabel.has(raw)) return hintByLabel.get(raw)
|
|
252
|
+
const n = normPath(raw)
|
|
253
|
+
if (n && hintByKey.has(n)) return hintByKey.get(n)
|
|
254
|
+
return null
|
|
255
|
+
}
|
|
256
|
+
const proposals = { sweep: [], optional: [], unknown: [] }
|
|
257
|
+
const seenProposal = new Set()
|
|
258
|
+
const propose = (bucket, label) => {
|
|
259
|
+
const h = hintOf(label)
|
|
260
|
+
if (!h) { proposals.unknown.push(String(label).slice(0, 200)); return }
|
|
261
|
+
if (seenProposal.has(h.key)) return
|
|
262
|
+
seenProposal.add(h.key)
|
|
263
|
+
proposals[bucket].push(h)
|
|
264
|
+
}
|
|
265
|
+
for (const l of verdict.sweep || []) propose('sweep', l)
|
|
266
|
+
for (const l of verdict.optional || []) propose('optional', l)
|
|
267
|
+
const byLabel = new Map(items.map((it, i) => ['i' + (i + 1), it.key]))
|
|
268
|
+
const byKey = new Map(items.map((it) => [it.key, it.key]))
|
|
269
|
+
const byPath = new Map()
|
|
270
|
+
for (const it of items) {
|
|
271
|
+
const n = normPath(it.path)
|
|
272
|
+
if (n) byPath.set(n, it.key)
|
|
273
|
+
}
|
|
274
|
+
const unknown = []
|
|
275
|
+
const keyOf = (label) => {
|
|
276
|
+
if (typeof label !== 'string') return null
|
|
277
|
+
const raw = label.trim()
|
|
278
|
+
if (!raw) return null
|
|
279
|
+
if (byLabel.has(raw)) return byLabel.get(raw)
|
|
280
|
+
const lower = raw.toLowerCase()
|
|
281
|
+
if (byLabel.has(lower)) return byLabel.get(lower)
|
|
282
|
+
if (byKey.has(raw)) return raw
|
|
283
|
+
const n = normPath(raw)
|
|
284
|
+
if (n && byPath.has(n)) return byPath.get(n)
|
|
285
|
+
if (byKey.has(n)) return n
|
|
286
|
+
unknown.push(raw.slice(0, 200))
|
|
287
|
+
return null
|
|
288
|
+
}
|
|
289
|
+
const out = { delete: [], keep: [], ask: [], note: verdict.note || '' }
|
|
290
|
+
for (const l of verdict.delete || []) { const k = keyOf(l); if (k) out.delete.push(k) }
|
|
291
|
+
for (const l of verdict.keep || []) { const k = keyOf(l); if (k) out.keep.push(k) }
|
|
292
|
+
for (const a of verdict.ask || []) {
|
|
293
|
+
const k = keyOf(a.id)
|
|
294
|
+
if (k) out.ask.push({ id: k, question: a.question || '' })
|
|
295
|
+
}
|
|
296
|
+
return { verdict: out, unknown, proposals }
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Turn a raw verdict into actions that are legal for this report. */
|
|
300
|
+
export function applyVerdict(report, verdict) {
|
|
301
|
+
const byKey = new Map((report.items || []).map((it) => [it.key, it]))
|
|
302
|
+
const auto = []
|
|
303
|
+
const asks = []
|
|
304
|
+
const kept = []
|
|
305
|
+
const unknown = []
|
|
306
|
+
const seen = new Set()
|
|
307
|
+
const once = (k) => (seen.has(k) ? false : (seen.add(k), true))
|
|
308
|
+
|
|
309
|
+
for (const key of verdict.delete || []) {
|
|
310
|
+
const it = byKey.get(key)
|
|
311
|
+
if (!it) { unknown.push(key); continue }
|
|
312
|
+
if (!once(key)) continue
|
|
313
|
+
if (it.tier === 'safe') auto.push(it)
|
|
314
|
+
else {
|
|
315
|
+
asks.push({
|
|
316
|
+
key,
|
|
317
|
+
path: it.path,
|
|
318
|
+
tier: it.tier,
|
|
319
|
+
size: it.size,
|
|
320
|
+
question: '模型认为可以删,但它属于「不确定」档:' + ((it.reasons || []).join(';') || '没有更多信息'),
|
|
321
|
+
})
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
for (const a of verdict.ask || []) {
|
|
325
|
+
const it = byKey.get(a.id)
|
|
326
|
+
if (!it) { unknown.push(a.id); continue }
|
|
327
|
+
if (!once(a.id)) continue
|
|
328
|
+
asks.push({ key: a.id, path: it.path, tier: it.tier, size: it.size, question: a.question || '这个还留着吗?' })
|
|
329
|
+
}
|
|
330
|
+
for (const key of verdict.keep || []) {
|
|
331
|
+
if (!byKey.has(key)) { unknown.push(key); continue }
|
|
332
|
+
if (!once(key)) continue
|
|
333
|
+
kept.push(key)
|
|
334
|
+
}
|
|
335
|
+
return { auto, asks, kept, unknown }
|
|
336
|
+
}
|