dsh-context-kit 0.1.0 → 0.1.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.
@@ -1,286 +1,286 @@
1
- // sources.cjs — 索引的两个数据来源:
2
- // 1) 路径:read/write/edit/grep/glob 的 file_path / path 参数(只记路径,不记内容)
3
- // 2) artifact:token-guard 的 manifest.jsonl 尾部 N 行(id/tool/mode/visibleChars)
4
- //
5
- // 安全不变式:
6
- // - 任何命中脱敏表的路径整条丢弃(大小写不敏感,见 DENY_PATTERNS);
7
- // - 绝对路径做展示折叠:工作区外且属于某个用户目录时,只保留尾部两段(`…\Documents\x.md`),
8
- // 自家 home 折叠为 `~`,避免把用户名送进 system prompt(也省 token);
9
- // - 不读取文件内容,只读路径字符串与 manifest 的元数据;
10
- // - 任何异常返回空结果,不向上抛(失败开放)。
11
- const fs = require('node:fs')
12
- const os = require('node:os')
13
- const path = require('node:path')
14
-
15
- // 脱敏表:命中任一子串的路径整条丢弃。大小写不敏感。
16
- // 'credentials' 覆盖 .credentials / .git-credentials / .aws\credentials / credentials.json。
17
- const DENY_PATTERNS = [
18
- '.credentials',
19
- 'credentials',
20
- '.env',
21
- 'local-proxy',
22
- 'secrets',
23
- 'token',
24
- '.ssh',
25
- 'appdata',
26
- '.kube',
27
- 'kubeconfig',
28
- '.npmrc',
29
- 'id_rsa',
30
- 'cookies',
31
- ]
32
-
33
- // 只跟踪这几种"触碰文件"的工具;其它工具(pwsh/web 等)不贡献路径。
34
- const TRACKED_TOOLS = new Set(['read', 'write', 'edit', 'grep', 'glob'])
35
-
36
- // 参数里可能承载路径的键,按优先级排列。
37
- const PATH_KEYS = ['file_path', 'filePath', 'path', 'paths', 'files', 'file']
38
-
39
- const MAX_PATH_CHARS = 320
40
-
41
- // 某个用户目录(非工作区)绝对路径:只保留尾部两段
42
- const USER_DIR_RE = /[\\/]Users[\\/][^\\/]+[\\/]/i
43
-
44
- /** 路径是否命中脱敏规则(大小写不敏感)。 */
45
- function isDeniedPath(raw) {
46
- if (typeof raw !== 'string') return false
47
- const lower = raw.toLowerCase()
48
- return DENY_PATTERNS.some((needle) => lower.includes(needle))
49
- }
50
-
51
- /** abs 是否在 root 之内(root 为文件路径或目录)。 */
52
- function isInside(root, abs) {
53
- try {
54
- if (!root) return false
55
- const rel = path.relative(path.resolve(root), abs)
56
- return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel))
57
- } catch {
58
- return false
59
- }
60
- }
61
-
62
- /**
63
- * 展示用路径:工作区内原样;自家 home 折叠成 `~`;
64
- * 其它用户目录降级为尾部两段(避免泄露用户名)。
65
- */
66
- function displayPath(abs, options = {}) {
67
- const { workspaceRoot = null, homeDir = null } = options
68
- if (workspaceRoot && isInside(workspaceRoot, abs)) return abs
69
- if (homeDir && isInside(homeDir, abs)) {
70
- const rel = path.relative(homeDir, abs)
71
- return rel ? `~${path.sep}${rel}` : '~'
72
- }
73
- if (USER_DIR_RE.test(abs)) {
74
- const parts = abs.split(/[\\/]+/).filter(Boolean)
75
- const tail = parts.slice(-2).join('\\')
76
- return tail ? `…\\${tail}` : ''
77
- }
78
- return abs
79
- }
80
-
81
- /** 从工具名 + 参数里提取候选路径(未归一化、未脱敏)。 */
82
- function extractPaths(toolName, args) {
83
- if (!TRACKED_TOOLS.has(String(toolName ?? ''))) return []
84
- if (!args || typeof args !== 'object') return []
85
- const out = []
86
- for (const key of PATH_KEYS) {
87
- const value = args[key]
88
- if (typeof value === 'string') {
89
- out.push(value)
90
- } else if (Array.isArray(value)) {
91
- for (const item of value) {
92
- if (typeof item === 'string') out.push(item)
93
- else if (item && typeof item === 'object' && typeof item.path === 'string') out.push(item.path)
94
- }
95
- }
96
- }
97
- return out
98
- }
99
-
100
- /**
101
- * 归一化 + 脱敏 + 展示折叠。返回可展示的路径字符串,或 null(表示丢弃)。
102
- * cwd 用于把相对路径补全;workspaceRoot 用于判断"工作区内";homeDir 用于折叠用户名。
103
- */
104
- function normalizePath(raw, options = {}) {
105
- const { cwd = process.cwd(), workspaceRoot = null, homeDir = os.homedir(), maxChars = MAX_PATH_CHARS } = options
106
- try {
107
- if (typeof raw !== 'string') return null
108
- let p = raw.trim()
109
- if (!p) return null
110
- if (p.length > maxChars) return null
111
- if (/[\r\n\t]/.test(p)) return null
112
- if (/^[a-z][a-z0-9+.-]*:\/\//i.test(p)) return null // URL 不是文件路径(也挡住 URL 里的 token)
113
- if ((p.startsWith('"') && p.endsWith('"')) || (p.startsWith("'") && p.endsWith("'"))) p = p.slice(1, -1)
114
- const abs = path.isAbsolute(p) ? path.normalize(p) : path.resolve(cwd, p)
115
- const inWorkspace = workspaceRoot ? isInside(workspaceRoot, abs) : false
116
- // 工作区自身的路径前缀(例如工作区恰好位于 AppData\Local\Temp)不算泄露;
117
- // 但工作区内的相对部分命中敏感词仍整条丢弃。
118
- if (isDeniedPath(p) || isDeniedPath(abs)) {
119
- const rel = inWorkspace ? path.relative(path.resolve(workspaceRoot), abs) : null
120
- if (!inWorkspace || (rel !== null && isDeniedPath(rel))) return null
121
- }
122
- const shown = displayPath(abs, { workspaceRoot, homeDir })
123
- if (!shown) return null
124
- if (!inWorkspace && isDeniedPath(shown)) return null
125
- return shown
126
- } catch {
127
- return null
128
- }
129
- }
130
-
131
- /**
132
- * 读 JSONL 文件尾部最多 maxLines 行(不回扫整个大文件)。
133
- * 返回 { records: [], total, size, mtimeMs },records 为"最近在前"。
134
- * 文件不存在 / 读失败 → 空结果,绝不抛。
135
- */
136
- function readManifestTail(file, maxLines = 500, maxBytes = 256 * 1024) {
137
- const empty = { records: [], total: 0, size: 0, mtimeMs: 0 }
138
- try {
139
- const st = fs.statSync(file)
140
- if (!st.isFile() || st.size === 0) return { ...empty, size: st.size, mtimeMs: st.mtimeMs }
141
- const limitLines = Math.max(1, Math.min(2000, Number(maxLines) || 500))
142
- const bytes = Math.min(st.size, Math.max(4096, maxBytes))
143
- const start = st.size - bytes
144
- const fd = fs.openSync(file, 'r')
145
- let text
146
- try {
147
- const buf = Buffer.allocUnsafe(bytes)
148
- fs.readSync(fd, buf, 0, bytes, start)
149
- text = buf.toString('utf8')
150
- } finally {
151
- fs.closeSync(fd)
152
- }
153
- let lines = text.split(/\r?\n/)
154
- // 从中间截断时,首行可能是半行:丢掉
155
- if (start > 0) lines = lines.slice(1)
156
- lines = lines.filter((line) => line.trim())
157
- const records = []
158
- for (let i = lines.length - 1; i >= 0 && records.length < limitLines; i -= 1) {
159
- try {
160
- records.push(JSON.parse(lines[i]))
161
- } catch {
162
- // 单行坏了不影响其它行
163
- }
164
- }
165
- return { records, total: lines.length, size: st.size, mtimeMs: st.mtimeMs }
166
- } catch {
167
- return empty
168
- }
169
- }
170
-
171
- /** 人类可读体积:4.1K / 11.9K / 812。 */
172
- function formatSize(chars) {
173
- const n = Number(chars)
174
- if (!Number.isFinite(n) || n <= 0) return '—'
175
- if (n < 1024) return String(Math.round(n))
176
- return `${(n / 1024).toFixed(1)}K`
177
- }
178
-
179
- // token-guard 落盘的溢出文件形如 ...\token-guard\artifacts\<sha16>.txt。
180
- // 它是"原文副本"而不是工具的目标路径,当作目标展示会误导模型。
181
- const SPILL_ARTIFACT_RE = /[\\/]artifacts[\\/][0-9a-f]{6,64}\.txt$/i
182
-
183
- /** 从 manifest 记录里推断工具的目标路径;拿不到就返回空串。 */
184
- function inferTarget(record) {
185
- if (!record || typeof record !== 'object') return ''
186
- const candidates = [record.target, record.path, record.file_path, record.filePath]
187
- for (const candidate of candidates) {
188
- if (typeof candidate === 'string' && candidate.trim()) return candidate.trim()
189
- }
190
- // artifact 字段:只有它不是 token-guard 自己的溢出副本时才作为目标
191
- if (typeof record.artifact === 'string' && record.artifact.trim() && !SPILL_ARTIFACT_RE.test(record.artifact)) {
192
- return record.artifact.trim()
193
- }
194
- return ''
195
- }
196
-
197
- // 状态语义(D1,2026-11-14 评审):
198
- // mode:"unchanged" 表示这次只给了 99 字符的 `[未变 · 与 #N 相同]` 标记,
199
- // visibleChars 才是模型当时真正看到的大小(实测 12,882 → 99)。
200
- // 绝不能把它渲染成 "12.6K ok"——那会让模型以为原文已在上下文里。
201
- function deriveStatus(record, { visibleChars, originalChars, mode }) {
202
- if (record?.isError === true || record?.error) return 'err'
203
- const marker = visibleChars > 0 && originalChars > 0 && visibleChars < originalChars * 0.5
204
- // 截断/摘要:上下文里看到的是删节版 → trunc
205
- if (mode === 'capped' || mode === 'summary') return 'trunc'
206
- // 未变/差异:只有 visible 远小于原文时才说明"模型没见过原文" → need
207
- if (mode === 'unchanged' || mode === 'delta') {
208
- if (marker) return 'need'
209
- return mode === 'delta' ? 'Δ' : 'same'
210
- }
211
- // 其它 mode(或没有 mode):保守处理
212
- if (marker) return 'need'
213
- return 'ok'
214
- }
215
-
216
- /** manifest 记录 → 索引里的 artifact 行;不是合法记录返回 null。 */
217
- function summarizeArtifact(record) {
218
- try {
219
- if (!record || typeof record !== 'object') return null
220
- const rawId = String(record.id ?? record.artifactId ?? '').trim()
221
- if (!/^[0-9a-f]{6,64}$/i.test(rawId)) return null
222
- const tool = typeof record.tool === 'string' && record.tool ? record.tool : '?'
223
- const originalChars = Number(record.originalChars ?? record.chars ?? 0)
224
- const hasVisible = record.visibleChars !== undefined && record.visibleChars !== null
225
- const visibleParsed = hasVisible ? Number(record.visibleChars) : originalChars
226
- const visibleChars = Number.isFinite(visibleParsed) && visibleParsed >= 0 ? visibleParsed : originalChars
227
- const target = inferTarget(record)
228
- if (target && isDeniedPath(target)) return null
229
- const mode = typeof record.mode === 'string' ? record.mode : ''
230
- const status = deriveStatus(record, { visibleChars, originalChars, mode })
231
- const marker = visibleChars > 0 && originalChars > 0 && visibleChars < originalChars * 0.5
232
- const size = marker
233
- ? `${formatSize(visibleChars)} / full ${formatSize(originalChars)}`
234
- : formatSize(visibleChars > 0 ? visibleChars : originalChars)
235
- return {
236
- id: rawId.slice(0, 6),
237
- fullId: rawId.toLowerCase(),
238
- tool,
239
- target,
240
- size,
241
- status,
242
- mode,
243
- dup: 1,
244
- originalChars: Number.isFinite(originalChars) ? originalChars : 0,
245
- visibleChars: Number.isFinite(visibleChars) ? visibleChars : 0,
246
- }
247
- } catch {
248
- return null
249
- }
250
- }
251
-
252
- /** 批量转换,并按 id 去重(保留最近一条,重复次数进 dup 列,渲染成 `×N`)。 */
253
- function summarizeArtifacts(records, limit = Infinity) {
254
- const out = []
255
- const seen = new Map()
256
- if (!Array.isArray(records)) return out
257
- for (const record of records) {
258
- const item = summarizeArtifact(record)
259
- if (!item) continue
260
- const existing = seen.get(item.fullId)
261
- if (existing) {
262
- existing.dup += 1
263
- continue
264
- }
265
- if (out.length >= limit) continue
266
- seen.set(item.fullId, item)
267
- out.push(item)
268
- }
269
- return out
270
- }
271
-
272
- module.exports = {
273
- DENY_PATTERNS,
274
- TRACKED_TOOLS,
275
- extractPaths,
276
- isDeniedPath,
277
- isInside,
278
- displayPath,
279
- normalizePath,
280
- readManifestTail,
281
- formatSize,
282
- inferTarget,
283
- deriveStatus,
284
- summarizeArtifact,
285
- summarizeArtifacts,
286
- }
1
+ // sources.cjs — 索引的两个数据来源:
2
+ // 1) 路径:read/write/edit/grep/glob 的 file_path / path 参数(只记路径,不记内容)
3
+ // 2) artifact:token-guard 的 manifest.jsonl 尾部 N 行(id/tool/mode/visibleChars)
4
+ //
5
+ // 安全不变式:
6
+ // - 任何命中脱敏表的路径整条丢弃(大小写不敏感,见 DENY_PATTERNS);
7
+ // - 绝对路径做展示折叠:工作区外且属于某个用户目录时,只保留尾部两段(`…\Documents\x.md`),
8
+ // 自家 home 折叠为 `~`,避免把用户名送进 system prompt(也省 token);
9
+ // - 不读取文件内容,只读路径字符串与 manifest 的元数据;
10
+ // - 任何异常返回空结果,不向上抛(失败开放)。
11
+ const fs = require('node:fs')
12
+ const os = require('node:os')
13
+ const path = require('node:path')
14
+
15
+ // 脱敏表:命中任一子串的路径整条丢弃。大小写不敏感。
16
+ // 'credentials' 覆盖 .credentials / .git-credentials / .aws\credentials / credentials.json。
17
+ const DENY_PATTERNS = [
18
+ '.credentials',
19
+ 'credentials',
20
+ '.env',
21
+ 'local-proxy',
22
+ 'secrets',
23
+ 'token',
24
+ '.ssh',
25
+ 'appdata',
26
+ '.kube',
27
+ 'kubeconfig',
28
+ '.npmrc',
29
+ 'id_rsa',
30
+ 'cookies',
31
+ ]
32
+
33
+ // 只跟踪这几种"触碰文件"的工具;其它工具(pwsh/web 等)不贡献路径。
34
+ const TRACKED_TOOLS = new Set(['read', 'write', 'edit', 'grep', 'glob'])
35
+
36
+ // 参数里可能承载路径的键,按优先级排列。
37
+ const PATH_KEYS = ['file_path', 'filePath', 'path', 'paths', 'files', 'file']
38
+
39
+ const MAX_PATH_CHARS = 320
40
+
41
+ // 某个用户目录(非工作区)绝对路径:只保留尾部两段
42
+ const USER_DIR_RE = /[\\/]Users[\\/][^\\/]+[\\/]/i
43
+
44
+ /** 路径是否命中脱敏规则(大小写不敏感)。 */
45
+ function isDeniedPath(raw) {
46
+ if (typeof raw !== 'string') return false
47
+ const lower = raw.toLowerCase()
48
+ return DENY_PATTERNS.some((needle) => lower.includes(needle))
49
+ }
50
+
51
+ /** abs 是否在 root 之内(root 为文件路径或目录)。 */
52
+ function isInside(root, abs) {
53
+ try {
54
+ if (!root) return false
55
+ const rel = path.relative(path.resolve(root), abs)
56
+ return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel))
57
+ } catch {
58
+ return false
59
+ }
60
+ }
61
+
62
+ /**
63
+ * 展示用路径:工作区内原样;自家 home 折叠成 `~`;
64
+ * 其它用户目录降级为尾部两段(避免泄露用户名)。
65
+ */
66
+ function displayPath(abs, options = {}) {
67
+ const { workspaceRoot = null, homeDir = null } = options
68
+ if (workspaceRoot && isInside(workspaceRoot, abs)) return abs
69
+ if (homeDir && isInside(homeDir, abs)) {
70
+ const rel = path.relative(homeDir, abs)
71
+ return rel ? `~${path.sep}${rel}` : '~'
72
+ }
73
+ if (USER_DIR_RE.test(abs)) {
74
+ const parts = abs.split(/[\\/]+/).filter(Boolean)
75
+ const tail = parts.slice(-2).join('\\')
76
+ return tail ? `…\\${tail}` : ''
77
+ }
78
+ return abs
79
+ }
80
+
81
+ /** 从工具名 + 参数里提取候选路径(未归一化、未脱敏)。 */
82
+ function extractPaths(toolName, args) {
83
+ if (!TRACKED_TOOLS.has(String(toolName ?? ''))) return []
84
+ if (!args || typeof args !== 'object') return []
85
+ const out = []
86
+ for (const key of PATH_KEYS) {
87
+ const value = args[key]
88
+ if (typeof value === 'string') {
89
+ out.push(value)
90
+ } else if (Array.isArray(value)) {
91
+ for (const item of value) {
92
+ if (typeof item === 'string') out.push(item)
93
+ else if (item && typeof item === 'object' && typeof item.path === 'string') out.push(item.path)
94
+ }
95
+ }
96
+ }
97
+ return out
98
+ }
99
+
100
+ /**
101
+ * 归一化 + 脱敏 + 展示折叠。返回可展示的路径字符串,或 null(表示丢弃)。
102
+ * cwd 用于把相对路径补全;workspaceRoot 用于判断"工作区内";homeDir 用于折叠用户名。
103
+ */
104
+ function normalizePath(raw, options = {}) {
105
+ const { cwd = process.cwd(), workspaceRoot = null, homeDir = os.homedir(), maxChars = MAX_PATH_CHARS } = options
106
+ try {
107
+ if (typeof raw !== 'string') return null
108
+ let p = raw.trim()
109
+ if (!p) return null
110
+ if (p.length > maxChars) return null
111
+ if (/[\r\n\t]/.test(p)) return null
112
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(p)) return null // URL 不是文件路径(也挡住 URL 里的 token)
113
+ if ((p.startsWith('"') && p.endsWith('"')) || (p.startsWith("'") && p.endsWith("'"))) p = p.slice(1, -1)
114
+ const abs = path.isAbsolute(p) ? path.normalize(p) : path.resolve(cwd, p)
115
+ const inWorkspace = workspaceRoot ? isInside(workspaceRoot, abs) : false
116
+ // 工作区自身的路径前缀(例如工作区恰好位于 AppData\Local\Temp)不算泄露;
117
+ // 但工作区内的相对部分命中敏感词仍整条丢弃。
118
+ if (isDeniedPath(p) || isDeniedPath(abs)) {
119
+ const rel = inWorkspace ? path.relative(path.resolve(workspaceRoot), abs) : null
120
+ if (!inWorkspace || (rel !== null && isDeniedPath(rel))) return null
121
+ }
122
+ const shown = displayPath(abs, { workspaceRoot, homeDir })
123
+ if (!shown) return null
124
+ if (!inWorkspace && isDeniedPath(shown)) return null
125
+ return shown
126
+ } catch {
127
+ return null
128
+ }
129
+ }
130
+
131
+ /**
132
+ * 读 JSONL 文件尾部最多 maxLines 行(不回扫整个大文件)。
133
+ * 返回 { records: [], total, size, mtimeMs },records 为"最近在前"。
134
+ * 文件不存在 / 读失败 → 空结果,绝不抛。
135
+ */
136
+ function readManifestTail(file, maxLines = 500, maxBytes = 256 * 1024) {
137
+ const empty = { records: [], total: 0, size: 0, mtimeMs: 0 }
138
+ try {
139
+ const st = fs.statSync(file)
140
+ if (!st.isFile() || st.size === 0) return { ...empty, size: st.size, mtimeMs: st.mtimeMs }
141
+ const limitLines = Math.max(1, Math.min(2000, Number(maxLines) || 500))
142
+ const bytes = Math.min(st.size, Math.max(4096, maxBytes))
143
+ const start = st.size - bytes
144
+ const fd = fs.openSync(file, 'r')
145
+ let text
146
+ try {
147
+ const buf = Buffer.allocUnsafe(bytes)
148
+ fs.readSync(fd, buf, 0, bytes, start)
149
+ text = buf.toString('utf8')
150
+ } finally {
151
+ fs.closeSync(fd)
152
+ }
153
+ let lines = text.split(/\r?\n/)
154
+ // 从中间截断时,首行可能是半行:丢掉
155
+ if (start > 0) lines = lines.slice(1)
156
+ lines = lines.filter((line) => line.trim())
157
+ const records = []
158
+ for (let i = lines.length - 1; i >= 0 && records.length < limitLines; i -= 1) {
159
+ try {
160
+ records.push(JSON.parse(lines[i]))
161
+ } catch {
162
+ // 单行坏了不影响其它行
163
+ }
164
+ }
165
+ return { records, total: lines.length, size: st.size, mtimeMs: st.mtimeMs }
166
+ } catch {
167
+ return empty
168
+ }
169
+ }
170
+
171
+ /** 人类可读体积:4.1K / 11.9K / 812。 */
172
+ function formatSize(chars) {
173
+ const n = Number(chars)
174
+ if (!Number.isFinite(n) || n <= 0) return '—'
175
+ if (n < 1024) return String(Math.round(n))
176
+ return `${(n / 1024).toFixed(1)}K`
177
+ }
178
+
179
+ // token-guard 落盘的溢出文件形如 ...\token-guard\artifacts\<sha16>.txt。
180
+ // 它是"原文副本"而不是工具的目标路径,当作目标展示会误导模型。
181
+ const SPILL_ARTIFACT_RE = /[\\/]artifacts[\\/][0-9a-f]{6,64}\.txt$/i
182
+
183
+ /** 从 manifest 记录里推断工具的目标路径;拿不到就返回空串。 */
184
+ function inferTarget(record) {
185
+ if (!record || typeof record !== 'object') return ''
186
+ const candidates = [record.target, record.path, record.file_path, record.filePath]
187
+ for (const candidate of candidates) {
188
+ if (typeof candidate === 'string' && candidate.trim()) return candidate.trim()
189
+ }
190
+ // artifact 字段:只有它不是 token-guard 自己的溢出副本时才作为目标
191
+ if (typeof record.artifact === 'string' && record.artifact.trim() && !SPILL_ARTIFACT_RE.test(record.artifact)) {
192
+ return record.artifact.trim()
193
+ }
194
+ return ''
195
+ }
196
+
197
+ // 状态语义(D1,2026-11-14 评审):
198
+ // mode:"unchanged" 表示这次只给了 99 字符的 `[未变 · 与 #N 相同]` 标记,
199
+ // visibleChars 才是模型当时真正看到的大小(实测 12,882 → 99)。
200
+ // 绝不能把它渲染成 "12.6K ok"——那会让模型以为原文已在上下文里。
201
+ function deriveStatus(record, { visibleChars, originalChars, mode }) {
202
+ if (record?.isError === true || record?.error) return 'err'
203
+ const marker = visibleChars > 0 && originalChars > 0 && visibleChars < originalChars * 0.5
204
+ // 截断/摘要:上下文里看到的是删节版 → trunc
205
+ if (mode === 'capped' || mode === 'summary') return 'trunc'
206
+ // 未变/差异:只有 visible 远小于原文时才说明"模型没见过原文" → need
207
+ if (mode === 'unchanged' || mode === 'delta') {
208
+ if (marker) return 'need'
209
+ return mode === 'delta' ? 'Δ' : 'same'
210
+ }
211
+ // 其它 mode(或没有 mode):保守处理
212
+ if (marker) return 'need'
213
+ return 'ok'
214
+ }
215
+
216
+ /** manifest 记录 → 索引里的 artifact 行;不是合法记录返回 null。 */
217
+ function summarizeArtifact(record) {
218
+ try {
219
+ if (!record || typeof record !== 'object') return null
220
+ const rawId = String(record.id ?? record.artifactId ?? '').trim()
221
+ if (!/^[0-9a-f]{6,64}$/i.test(rawId)) return null
222
+ const tool = typeof record.tool === 'string' && record.tool ? record.tool : '?'
223
+ const originalChars = Number(record.originalChars ?? record.chars ?? 0)
224
+ const hasVisible = record.visibleChars !== undefined && record.visibleChars !== null
225
+ const visibleParsed = hasVisible ? Number(record.visibleChars) : originalChars
226
+ const visibleChars = Number.isFinite(visibleParsed) && visibleParsed >= 0 ? visibleParsed : originalChars
227
+ const target = inferTarget(record)
228
+ if (target && isDeniedPath(target)) return null
229
+ const mode = typeof record.mode === 'string' ? record.mode : ''
230
+ const status = deriveStatus(record, { visibleChars, originalChars, mode })
231
+ const marker = visibleChars > 0 && originalChars > 0 && visibleChars < originalChars * 0.5
232
+ const size = marker
233
+ ? `${formatSize(visibleChars)} / full ${formatSize(originalChars)}`
234
+ : formatSize(visibleChars > 0 ? visibleChars : originalChars)
235
+ return {
236
+ id: rawId.slice(0, 6),
237
+ fullId: rawId.toLowerCase(),
238
+ tool,
239
+ target,
240
+ size,
241
+ status,
242
+ mode,
243
+ dup: 1,
244
+ originalChars: Number.isFinite(originalChars) ? originalChars : 0,
245
+ visibleChars: Number.isFinite(visibleChars) ? visibleChars : 0,
246
+ }
247
+ } catch {
248
+ return null
249
+ }
250
+ }
251
+
252
+ /** 批量转换,并按 id 去重(保留最近一条,重复次数进 dup 列,渲染成 `×N`)。 */
253
+ function summarizeArtifacts(records, limit = Infinity) {
254
+ const out = []
255
+ const seen = new Map()
256
+ if (!Array.isArray(records)) return out
257
+ for (const record of records) {
258
+ const item = summarizeArtifact(record)
259
+ if (!item) continue
260
+ const existing = seen.get(item.fullId)
261
+ if (existing) {
262
+ existing.dup += 1
263
+ continue
264
+ }
265
+ if (out.length >= limit) continue
266
+ seen.set(item.fullId, item)
267
+ out.push(item)
268
+ }
269
+ return out
270
+ }
271
+
272
+ module.exports = {
273
+ DENY_PATTERNS,
274
+ TRACKED_TOOLS,
275
+ extractPaths,
276
+ isDeniedPath,
277
+ isInside,
278
+ displayPath,
279
+ normalizePath,
280
+ readManifestTail,
281
+ formatSize,
282
+ inferTarget,
283
+ deriveStatus,
284
+ summarizeArtifact,
285
+ summarizeArtifacts,
286
+ }