dsh-experience 0.0.0-stage → 1.0.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/lib/capture.js ADDED
@@ -0,0 +1,868 @@
1
+ /**
2
+ * dsh-experience / lib/capture.js — deterministic L1 experience capture.
3
+ *
4
+ * Pure functions only: no ctx, no fs, no network, no model calls. The caller
5
+ * (index.js) owns persistence and identity assignment, so everything here is
6
+ * directly testable with synthetic traces (tools/selftest.mjs).
7
+ */
8
+
9
+ const SECRET_PATTERNS = [
10
+ [/sk-[A-Za-z0-9_-]{16,}/g, '‹redacted›'],
11
+ [/ghp_[A-Za-z0-9]{20,}/g, '‹redacted›'],
12
+ [/AKIA[0-9A-Z]{16}/g, '‹redacted›'],
13
+ [/xox[baprs]-[A-Za-z0-9-]{10,}/g, '‹redacted›'],
14
+ [/Bearer\s+[A-Za-z0-9._-]+/gi, 'Bearer ‹redacted›'],
15
+ [/([A-Za-z0-9_\-]*(?:api[_-]?key|token|secret|password|passwd|authorization))(\s*[:=]\s*)\S+/gi, '$1$2‹redacted›'],
16
+ [/(?=[A-Za-z0-9+/]*[A-Z])(?=[A-Za-z0-9+/]*[a-z])(?=[A-Za-z0-9+/]*[0-9])[A-Za-z0-9+/]{40,}={0,2}/g, '‹redacted›'],
17
+ ]
18
+
19
+ const PATH_PATTERNS = [
20
+ [/[A-Za-z]:\\Users\\[^\\\s]+/gi, '~'],
21
+ [/[A-Za-z]:\\(?:[^\\\s]+\\){0,4}/g, '<path>/'],
22
+ [/\\\\[^\\\s]+\\/g, '<unc>/'],
23
+ ]
24
+
25
+ const KNOWN_CONFIG_FILES = new Set([
26
+ 'package.json', 'pyproject.toml', 'tsconfig.json', 'Cargo.toml', 'go.mod', 'requirements.txt',
27
+ ])
28
+
29
+ export const DEFAULT_CONFIG = {
30
+ dailyLimit: 10,
31
+ libraryLimit: 200,
32
+ mergeThreshold: 0.75,
33
+ scopeMustMatch: true,
34
+ captureBudgetMs: 20,
35
+ maxRecordChars: 2000,
36
+ maxCalls: 40,
37
+ autoPromote: false,
38
+ minDistinctSessions: 2,
39
+ stepsJaccard: 0.7,
40
+ allowAutoVerified: false,
41
+ minEffectiveSteps: 2,
42
+ recallThreshold: 0.15,
43
+ }
44
+
45
+ export function redact(text) {
46
+ let out = String(text == null ? '' : text)
47
+ for (const [pattern, replacement] of SECRET_PATTERNS) out = out.replace(pattern, replacement)
48
+ for (const [pattern, replacement] of PATH_PATTERNS) out = out.replace(pattern, replacement)
49
+ return out
50
+ }
51
+
52
+ export function clip(text, limit = 160) {
53
+ const source = redact(text).replace(/\s+/g, ' ').trim()
54
+ if (source.length <= limit) return source
55
+ return source.slice(0, Math.max(0, limit - 1)) + '…'
56
+ }
57
+
58
+ /** Truncate without redacting: features (scope, fingerprint) need the real path. */
59
+ export function clipRaw(text, limit = 160) {
60
+ const source = String(text == null ? '' : text).replace(/\s+/g, ' ').trim()
61
+ if (source.length <= limit) return source
62
+ return source.slice(0, Math.max(0, limit - 1)) + '…'
63
+ }
64
+
65
+ const WRITE_TOOL = /^(write|edit|apply|patch|create|replace)/i
66
+ const READ_TOOL = /^(read|view|open|cat)/i
67
+ const SEARCH_TOOL = /^(grep|glob|search|find|query)/i
68
+ const RUN_TOOL = /^(pwsh|bash|shell|exec|run|command|terminal)/i
69
+
70
+ function firstString(value) {
71
+ if (typeof value === 'string') return value
72
+ if (Array.isArray(value)) return value.map(firstString).find(Boolean) || ''
73
+ if (value && typeof value === 'object') {
74
+ for (const key of ['path', 'file', 'file_path', 'command', 'pattern', 'query', 'cmd']) {
75
+ const found = firstString(value[key])
76
+ if (found) return found
77
+ }
78
+ }
79
+ return ''
80
+ }
81
+
82
+ export function normalizePath(input) {
83
+ const raw = String(input || '').trim().replace(/^["'`]|["'`]$/g, '')
84
+ if (!raw) return ''
85
+ const segments = raw.split(/[\\/]+/).filter(Boolean)
86
+ const base = segments[segments.length - 1] || ''
87
+ if (KNOWN_CONFIG_FILES.has(base)) return base
88
+ const extMatch = base.match(/\.[A-Za-z0-9]+$/)
89
+ const ext = extMatch ? extMatch[0] : ''
90
+ const stem = ext ? base.slice(0, -ext.length) : base
91
+ const keepStem = /^(package|pyproject|index|main|mod)$/i.test(stem)
92
+ const name = keepStem ? stem : '<name>'
93
+ const parents = segments.slice(Math.max(0, segments.length - 2), segments.length - 1)
94
+ return [...parents, name + ext].join('/')
95
+ }
96
+
97
+ export function normalizeCommand(command) {
98
+ const raw = clip(command, 120)
99
+ if (!raw) return ''
100
+ if (/node\s+--test\b/.test(raw)) return 'node --test'
101
+ const nodeCall = raw.match(/node\s+([^\s|&;]+)/)
102
+ if (nodeCall) return `node ${normalizePath(nodeCall[1])}`
103
+ const parts = raw.split(/\s+/)
104
+ return parts.slice(0, 2).join(' ')
105
+ }
106
+
107
+ export function classify(call) {
108
+ const name = String((call && call.name) || '')
109
+ const hint = firstString(call && (call.hint || call.argsHint || call.args))
110
+ let kind = 'other'
111
+ if (WRITE_TOOL.test(name)) kind = 'write'
112
+ else if (READ_TOOL.test(name)) kind = 'read'
113
+ else if (SEARCH_TOOL.test(name)) kind = 'search'
114
+ else if (RUN_TOOL.test(name)) kind = 'run'
115
+ let target = ''
116
+ if (kind === 'run') target = normalizeCommand(hint)
117
+ else if (kind === 'write' || kind === 'read') target = normalizePath(hint)
118
+ else if (kind === 'search') target = clip(hint, 60)
119
+ else target = clip(hint, 40)
120
+ return { kind, target, name }
121
+ }
122
+
123
+ /**
124
+ * "cd somewhere; node test.js" says nothing about the recipe beyond the command
125
+ * that actually ran, so keep only the trailing command.
126
+ */
127
+ export function stripCdPrefix(command) {
128
+ const text = String(command || '').trim()
129
+ const parts = text.split(/\s*(?:;|&&|\|\|)\s*/).filter(Boolean)
130
+ if (parts.length <= 1) return text.replace(/;+$/, '')
131
+ const kept = parts.filter((part) => !/^cd\s/i.test(part) && !/^Set-Location\b/i.test(part))
132
+ return (kept.length ? kept.join('; ') : parts[parts.length - 1]).replace(/;+$/, '')
133
+ }
134
+
135
+ export function stepLabel(call) {
136
+ const { kind, target, name } = classify(call)
137
+ switch (kind) {
138
+ case 'write':
139
+ return target ? `写 ${target}` : '写文件'
140
+ case 'read':
141
+ return target ? `读 ${target.split('/').pop()}` : '读文件'
142
+ case 'run':
143
+ return target ? `跑 ${stripCdPrefix(target)}` : '执行命令'
144
+ case 'search':
145
+ return target ? `检索 ${target}` : '检索'
146
+ default:
147
+ return target ? `${name}(${target})` : name || '调用工具'
148
+ }
149
+ }
150
+
151
+ const EXPLORE_COMMAND = /\b(Get-ChildItem|Get-Content|Get-Item|Test-Path|Select-String|Select-Object|Measure-Object|gci|gc|dir|ls|cat|type|head|tail|find|rg|grep|where|which|stat)\b/i
152
+ const TEST_COMMAND = /\b(test|spec|jest|vitest|mocha|pytest|check|--test)\b/i
153
+
154
+ /** A successful run whose command looks like a test/verification command. */
155
+ export function isTestCall(call) {
156
+ if (!call) return false
157
+ if (classify(call).kind !== 'run') return false
158
+ const raw = firstString(call.hint || call.argsHint || call.args)
159
+ return TEST_COMMAND.test(raw)
160
+ }
161
+
162
+ /**
163
+ * How much a successful call contributes to the *recipe* (not the transcript):
164
+ * 4 = execution/test run, 3 = write/edit, 2 = other real commands, 0 = exploration.
165
+ * Exploration (listing a directory, reading a reference file, grepping) is what an
166
+ * agent does because it does not know the answer yet; it must not become the
167
+ * "core steps" of the experience.
168
+ */
169
+ export function stepPriority(call) {
170
+ const { kind } = classify(call)
171
+ if (kind === 'write') return 3
172
+ if (kind === 'run') {
173
+ const command = firstString(call && (call.hint || call.argsHint || call.args))
174
+ if (TEST_COMMAND.test(command)) return 4
175
+ if (EXPLORE_COMMAND.test(command)) return 0
176
+ return 2
177
+ }
178
+ return 0
179
+ }
180
+
181
+ /**
182
+ * A step label is only useful if a human could read it as an instruction.
183
+ * PowerShell scaffolding (variable assembly, New-Item/Test-Path probes, host
184
+ * output) is transcript noise: it describes how the agent measured the world,
185
+ * not what a second run should do.
186
+ */
187
+ const NOISE_PREFIX = /^(\$|New-Item|New-ItemProperty|New-Object|Test-Path|Get-ChildItem|Get-Item|Set-Location|Push-Location|Pop-Location|Write-Host|Write-Output|Write-Verbose|Clear-Host|Copy-Item|Remove-Item|Move-Item|Rename-Item|Start-Sleep|sleep|cd)\b/
188
+ const ASSIGNMENT = /^[A-Za-z_$][\w$]*\s*=[^=]/
189
+
190
+ export function isNoiseStep(label) {
191
+ const text = String(label == null ? '' : label).trim()
192
+ if (!text) return true
193
+ if (NOISE_PREFIX.test(text) || ASSIGNMENT.test(text)) return true
194
+ const body = text.replace(/^(写|读|跑|检索)\s+/, '')
195
+ if (body !== text && (NOISE_PREFIX.test(body) || ASSIGNMENT.test(body))) return true
196
+ // a bare token with no action word and no separator is a fragment
197
+ if (!/\s/.test(text)) return true
198
+ return false
199
+ }
200
+
201
+ /** Steps that carry the recipe: successful writes and test/verification runs. */
202
+ export function effectiveSteps(calls) {
203
+ let count = 0
204
+ for (const call of calls || []) {
205
+ if (!call || !call.ok) continue
206
+ if (stepPriority(call) < 3) continue
207
+ if (isNoiseStep(stepLabel(call))) continue
208
+ count++
209
+ }
210
+ return count
211
+ }
212
+
213
+ const ACTION_VERBS = /^(新增|写|跑通|跑|执行|修改|修复|重构|实现|添加|删除|更新|重命名|移动|替换|补|加|改为)/
214
+
215
+ /**
216
+ * Executions worth repeating. A run recipe is "how to invoke this thing", so a
217
+ * bare mkdir / dir listing / file read never counts: those describe discovery,
218
+ * not the recipe.
219
+ */
220
+ export function runSteps(calls) {
221
+ const kept = []
222
+ for (const call of calls || []) {
223
+ if (!call || !call.ok) continue
224
+ if (classify(call).kind !== 'run') continue
225
+ if (stepPriority(call) < 2) continue
226
+ if (isNoiseStep(stepLabel(call))) continue
227
+ kept.push(call)
228
+ }
229
+ return kept
230
+ }
231
+
232
+ /** The program a run call actually launched: "& \"C:\\...\\node.exe\" x.mjs" -> node. */
233
+ export function commandHeads(calls) {
234
+ const heads = []
235
+ for (const call of calls || []) {
236
+ if (!call || !call.ok) continue
237
+ if (classify(call).kind !== 'run') continue
238
+ if (isNoiseStep(stepLabel(call))) continue
239
+ const raw = firstString(call && (call.hint || call.argsHint || call.args)) || ''
240
+ const lead = raw.trim().replace(/^&\s*/, '')
241
+ const quoted = lead.match(/^("[^"]+"|'[^']+'|\S+)/)
242
+ const program = quoted ? quoted[0].replace(/^["']|["']$/g, '') : ''
243
+ const base = program.split(/[\\/]+/).filter(Boolean).pop() || ''
244
+ const head = base.replace(/\.exe$/i, '').toLowerCase()
245
+ if (head && /^[a-z][a-z0-9_.-]*$/.test(head) && !/^[a-z]:$/.test(head)) heads.push(head)
246
+ }
247
+ return [...new Set(heads)].sort()
248
+ }
249
+
250
+ /** The script a run recipe executes, as a bare name: "run build.mjs" -> build. */
251
+ export function runArtifactName(calls) {
252
+ for (const call of calls || []) {
253
+ if (!call || !call.ok) continue
254
+ if (classify(call).kind !== 'run') continue
255
+ const raw = firstString(call && (call.hint || call.argsHint || call.args)) || ''
256
+ const hit = raw.match(/[A-Za-z0-9_][A-Za-z0-9_.-]*\.(?:mjs|cjs|js|py|go|rs|sh|ps1|java)\b/i)
257
+ if (!hit) continue
258
+ const base = hit[0].split(/[\\/]/).pop()
259
+ return base.replace(/\.[^.]+$/, '')
260
+ }
261
+ return ''
262
+ }
263
+
264
+
265
+
266
+ /** The object of a title must name a concrete artifact, not only <placeholders>. */
267
+ export function actionableTitle(title) {
268
+ const text = String(title == null ? '' : title).trim()
269
+ if (!ACTION_VERBS.test(text)) return false
270
+ const object = text.replace(ACTION_VERBS, '').trim()
271
+ if (!object) return false
272
+ if (object.startsWith('$') || object.startsWith('-')) return false
273
+ // Drop <placeholders> first: what remains must still name something. A title
274
+ // like "写 src/<name>.js" collapses to "src/.js" — a directory and an
275
+ // extension with no artifact name — and is therefore rejected.
276
+ const stripped = object.replace(/<[^>]*>/g, '')
277
+ const base = stripped.split(/[\\/]+/).filter(Boolean).pop() || ''
278
+ const stem = base.replace(/\.[^.]+$/, '').replace(/[\s._-]+/g, '')
279
+ return /[A-Za-z0-9]{2,}/.test(stem)
280
+ }
281
+
282
+ /**
283
+ * G7 — refuses captures whose steps or title are not a reusable recipe.
284
+ * Runs after the G1..G6 chain; the caller writes trace.skipReason = reason.
285
+ */
286
+ export function gateStepQuality(trace, record, config, intent) {
287
+ const cfg = { ...DEFAULT_CONFIG, ...(config || {}) }
288
+ const calls = (trace && trace.calls) || []
289
+ // Two scales: a change is judged by its write/test steps, an invocation
290
+ // recipe by its repeatable executions. mkdir/read never enter either count.
291
+ const effective = intent === 'run' ? runSteps(calls).length : effectiveSteps(calls)
292
+ if (effective < cfg.minEffectiveSteps) {
293
+ const what = intent === 'run' ? '可复用执行(建目录/读文件不计)' : '(只有写操作与测试执行算核心步骤)'
294
+ return {
295
+ ok: false,
296
+ reason: `G7: 有效步骤 ${effective} < ${cfg.minEffectiveSteps}${intent === 'run' ? ' 次' : ''}${what}`,
297
+ effective,
298
+ runOnly: intent === 'run',
299
+ }
300
+ }
301
+ const title = String((record && record.title) || '')
302
+ if (!actionableTitle(title)) {
303
+ return { ok: false, reason: `G7: 标题「${title}」不含动作+对象,按命令碎片丢弃`, effective }
304
+ }
305
+ const when = String((record && record.when) || '')
306
+ if (/完成一项可复用的改动/.test(when)) {
307
+ return { ok: false, reason: 'G7: when 是兜底文案,说明任务类型未被识别', effective }
308
+ }
309
+ return { ok: true, effective }
310
+ }
311
+
312
+ export function summarizeCalls(calls) {
313
+ const steps = []
314
+ for (const call of calls || []) {
315
+ if (!call || !call.ok) continue
316
+ if (stepPriority(call) === 0) continue
317
+ const label = stepLabel(call)
318
+ if (!label) continue
319
+ if (isNoiseStep(label)) continue
320
+ if (steps[steps.length - 1] === label) continue
321
+ steps.push(label)
322
+ if (steps.length >= 8) break
323
+ }
324
+ return steps
325
+ }
326
+
327
+ /**
328
+ * What the turn actually produced, independent of how many steps it needed.
329
+ * A second run that only re-reads and re-runs the tests has the same fingerprint
330
+ * as the first run that scaffolded everything — that is the real "same task".
331
+ */
332
+ const EXT_KIND = {
333
+ code: ['js', 'mjs', 'cjs', 'ts', 'tsx', 'jsx', 'py', 'go', 'rs', 'java', 'cs', 'rb', 'php', 'sh', 'ps1', 'cmd', 'bat'],
334
+ image: ['png', 'jpg', 'jpeg', 'webp', 'gif', 'bmp', 'svg', 'tif', 'tiff', 'ico', 'heic'],
335
+ doc: ['md', 'txt', 'pdf', 'docx', 'doc', 'pptx', 'xlsx', 'csv', 'html', 'css', 'json', 'yml', 'yaml'],
336
+ binary: ['zip', 'gz', 'tgz', 'exe', 'dll', 'whl', 'jar'],
337
+ }
338
+
339
+ const KIND_BY_EXT = (() => {
340
+ const map = new Map()
341
+ for (const [kind, exts] of Object.entries(EXT_KIND)) for (const ext of exts) map.set(ext, kind)
342
+ return map
343
+ })()
344
+
345
+ export function artifactKindOf(target) {
346
+ const name = String(target || '').toLowerCase()
347
+ const match = name.match(/\.([a-z0-9]+)$/)
348
+ if (!match) return ''
349
+ return KIND_BY_EXT.get(match[1]) || ''
350
+ }
351
+
352
+ export function artifactKindsOf(targets) {
353
+ const kinds = new Set()
354
+ for (const target of targets || []) {
355
+ const kind = artifactKindOf(target)
356
+ if (kind) kinds.add(kind)
357
+ }
358
+ return [...kinds].sort()
359
+ }
360
+
361
+ /**
362
+ * Do these two runs write fundamentally different kinds of artifact?
363
+ * Only says "conflict" when both sides have evidence and the sets are disjoint.
364
+ */
365
+ export function artifactTypesConflict(left, right) {
366
+ const a = (left && left.fingerprint && left.fingerprint.kinds) || []
367
+ const b = (right && right.fingerprint && right.fingerprint.kinds) || []
368
+ if (!a.length || !b.length) return false
369
+ return !a.some((kind) => b.includes(kind))
370
+ }
371
+
372
+ export function outcomeFingerprint(calls) {
373
+ const list = calls || []
374
+ const artifactOf = (call) => {
375
+ const raw = firstString(call && (call.hint || call.argsHint || call.args)) || ''
376
+ const parts = raw.split(/[\\/]+/).filter(Boolean)
377
+ return (parts.length >= 2 ? parts.slice(-2).join('/') : parts[0] || '').toLowerCase()
378
+ }
379
+ const isCode = (target) => /\.(js|mjs|cjs|ts|tsx|jsx|py|go|rs|java|cs|rb|php)$/.test(target)
380
+ const writes = list.filter((call) => call && call.ok && classify(call).kind === 'write').map(artifactOf)
381
+ const reads = list.filter((call) => call && call.ok && classify(call).kind === 'read').map(artifactOf)
382
+ const testCalls = list.filter((call) => call && isTestCall(call))
383
+ const tests = testCalls.map((call) => classify(call).target)
384
+ // A run that failed and then went green still ends green; the failure itself is
385
+ // captured as a reverse example, not as a reason to refuse the merge.
386
+ const green = testCalls.length > 0 && testCalls[testCalls.length - 1].ok === true
387
+ const allArtifacts = writes.length ? writes : reads
388
+ const artifacts = allArtifacts.filter(isCode)
389
+ return {
390
+ targets: [...new Set(artifacts)].sort(),
391
+ commands: commandHeads(list),
392
+ kinds: artifactKindsOf(allArtifacts),
393
+ tests: [...new Set(tests)].sort(),
394
+ green,
395
+ verificationOnly: writes.length === 0,
396
+ }
397
+ }
398
+
399
+ /** Same project + same produced artifact + green tests => the same task, whatever the steps. */
400
+ export function fingerprintsMatch(left, right) {
401
+ const a = left && left.fingerprint
402
+ const b = right && right.fingerprint
403
+ if (!a || !b || !a.green || !b.green) return false
404
+ // Same name, different artifact type is not the same outcome.
405
+ if (artifactTypesConflict(left, right)) return false
406
+ const have = new Set(a.targets || [])
407
+ const want = new Set(b.targets || [])
408
+ if (!have.size || !want.size) return false
409
+ for (const target of have) if (want.has(target)) return true
410
+ return false
411
+ }
412
+
413
+ function errorCode(call) {
414
+ const text = String((call && (call.errorCode || call.error)) || '')
415
+ const known = text.match(/\b(EPERM|ENOENT|EACCES|ETIMEDOUT|EEXIST|EISDIR|TypeError|RangeError|SyntaxError|AssertionError)\b/)
416
+ if (known) return known[1]
417
+ const exit = text.match(/exit code (\d+)/i)
418
+ if (exit) return `exit ${exit[1]}`
419
+ return '错误'
420
+ }
421
+
422
+ /** The first line of the failure is what a second run actually needs to avoid. */
423
+ function errorDetail(call) {
424
+ const text = String((call && (call.errorCode || call.error)) || '').replace(/\s+/g, ' ').trim()
425
+ if (!text) return ''
426
+ const head = text.split(/(?<=\b(?:Error|EPERM|ENOENT|EACCES|EEXIST|EISDIR)\b)/)[0]
427
+ return clip(head || text, 80)
428
+ }
429
+
430
+ export function findRecoveries(calls) {
431
+ const list = calls || []
432
+ const recoveries = []
433
+ const seen = new Set()
434
+ for (let i = 0; i < list.length; i++) {
435
+ const failed = list[i]
436
+ if (!failed || failed.ok) continue
437
+ const failedLabel = stepLabel(failed)
438
+ for (let j = i + 1; j < list.length; j++) {
439
+ const ok = list[j]
440
+ if (!ok || !ok.ok) continue
441
+ const failedKind = classify(failed).kind
442
+ const okKind = classify(ok).kind
443
+ const sameKind = failedKind === okKind
444
+ // Compare the *raw* basename: normalization collapses `a.js` and `b.js`
445
+ // to the same `<name>.js`, which would fake a recovery between two
446
+ // unrelated files.
447
+ const rawBase = (call) => {
448
+ const raw = firstString(call && (call.hint || call.argsHint || call.args))
449
+ const segment = String(raw).split(/[\\/]+/).filter(Boolean).pop() || ''
450
+ return segment.replace(/\.[^.]+$/, '').toLowerCase()
451
+ }
452
+ const failedBase = rawBase(failed)
453
+ const okBase = rawBase(ok)
454
+ // A recovery must be *related* to the failure, otherwise any later success
455
+ // of the same tool kind would masquerade as one:
456
+ // - same kind + same raw basename (normal retry / file moved),
457
+ // - same command head for two runs (node --test -> node test/x.mjs).
458
+ const sameHead =
459
+ failedKind === 'run' &&
460
+ okKind === 'run' &&
461
+ classify(failed).target.split(/\s+/)[0] === classify(ok).target.split(/\s+/)[0]
462
+ const related = (sameKind && Boolean(failedBase) && failedBase === okBase) || sameHead
463
+ if (!related) continue
464
+ const line = `${failedLabel} 失败(${errorDetail(failed) || errorCode(failed)}),改用 ${stepLabel(ok)} 成功`
465
+ if (!seen.has(line)) {
466
+ seen.add(line)
467
+ recoveries.push(line)
468
+ }
469
+ break
470
+ }
471
+ if (recoveries.length >= 3) break
472
+ }
473
+ return recoveries
474
+ }
475
+
476
+ export function inferTaskType(calls, cwd) {
477
+ const haystack = (calls || [])
478
+ .map((call) => `${firstString(call && (call.hint || call.argsHint || call.args))} ${call && call.name ? call.name : ''}`)
479
+ .join(' ')
480
+ .toLowerCase()
481
+ const dir = String(cwd || '').toLowerCase()
482
+ if (/\.(tsx|jsx|vue|svelte)\b/.test(haystack)) return 'web/frontend'
483
+ if (/\.(ts)\b/.test(haystack)) return 'web/frontend'
484
+ if (/\.py\b|pyproject\.toml|requirements\.txt/.test(haystack)) return 'python/module'
485
+ if (/\.go\b|go\.mod/.test(haystack)) return 'go/module'
486
+ if (/\.rs\b|cargo\.toml/.test(haystack)) return 'rust/module'
487
+ if (/package\.json|\.m?js\b|node/.test(haystack) || /node/.test(dir)) return 'node-project/feature'
488
+ return 'workspace/general'
489
+ }
490
+
491
+ export function shouldCapture(trace, counts, config, intent) {
492
+ const cfg = { ...DEFAULT_CONFIG, ...(config || {}) }
493
+ const calls = (trace && trace.calls) || []
494
+ const successes = calls.filter((call) => call && call.ok)
495
+ if (successes.length < 2) return { ok: false, reason: 'G1: 成功工具调用 < 2' }
496
+
497
+ const names = calls.map((call) => String((call && call.name) || ''))
498
+ const query = String((trace && trace.query) || '').trim()
499
+ if (names.some((name) => name.startsWith('experience_')) || query.startsWith('/memory')) {
500
+ return { ok: false, reason: 'G2: 本 turn 是经验管理操作,不回灌' }
501
+ }
502
+
503
+ const writes = successes.filter((call) => classify(call).kind === 'write')
504
+ const recoveries = findRecoveries(calls)
505
+ // A verification-only turn (re-read the code, run the tests, all green) is valid
506
+ // independent evidence: it re-proves the recipe without a write operation.
507
+ const tests = successes.filter((call) => isTestCall(call))
508
+ const testFailures = calls.filter((call) => call && !call.ok && isTestCall(call))
509
+ const verifiedGreen = tests.length > 0 && testFailures.length === 0
510
+ // A run recipe is legitimate evidence on its own: nobody wrote anything, but
511
+ // the execution was repeated and is worth replaying.
512
+ const executions = intent === 'run' ? runSteps(calls) : []
513
+ const runOnly = intent === 'run' && !writes.length && !recoveries.length && !verifiedGreen
514
+ if (!writes.length && !recoveries.length && !verifiedGreen && !(runOnly && executions.length >= 2)) {
515
+ return { ok: false, reason: 'G3: 无成功写操作、无失败后成功,且不是全绿验证轨迹' }
516
+ }
517
+
518
+ if ((counts && counts.todayAdded) >= cfg.dailyLimit) {
519
+ return { ok: false, reason: `G5: 今日自动捕获已达上限 ${cfg.dailyLimit}` }
520
+ }
521
+ if ((counts && counts.autoTotal) >= cfg.libraryLimit) {
522
+ return { ok: false, reason: `G5: 自动经验库已达上限 ${cfg.libraryLimit}` }
523
+ }
524
+ return {
525
+ ok: true,
526
+ writes,
527
+ recoveries,
528
+ runOnly,
529
+ executions: executions.length,
530
+ verificationOnly: !writes.length && !recoveries.length,
531
+ }
532
+ }
533
+
534
+ function verbsOf(taskType) {
535
+ if (taskType === 'node-project/feature') return '新增函数/模块并配用例'
536
+ if (taskType === 'python/module') return '新增 Python 模块并配测试'
537
+ if (taskType === 'web/frontend') return '改前端组件'
538
+ if (taskType === 'go/module') return '新增 Go 包'
539
+ if (taskType === 'rust/module') return '新增 Rust 模块'
540
+ return '完成一项可复用的改动'
541
+ }
542
+
543
+ export function workspaceName(cwd) {
544
+ const segments = String(cwd || '').split(/[\\/]+/).filter(Boolean)
545
+ return segments[segments.length - 1] || ''
546
+ }
547
+
548
+ const GENERIC_DIRS = new Set([
549
+ 'users', 'home', 'desktop', 'documents', 'downloads', 'src', 'test', 'tests',
550
+ 'lib', 'node_modules', 'bin', 'tmp', 'temp', 'app', 'apps', 'workspace',
551
+ 'workspaces',
552
+ // Tool-chain directories: every recipe on this machine touches them, so they
553
+ // say nothing about *which project* a record belongs to.
554
+ 'node', 'python', 'python3', 'npm', 'pnpm', 'npx', 'yarn', 'git', 'pwsh',
555
+ 'deno', 'bun', 'runtime', 'runtimes', 'dependencies', 'dsh', 'windows',
556
+ // Home-directory placeholders: a user name is not a project name.
557
+ 'dev', 'developer', 'user', 'userprofile',
558
+ // Sandbox and CI roots: fixtures live here, they are not project names.
559
+ 'work', 'projects', 'repos',
560
+ ])
561
+
562
+ /**
563
+ * Project directories mentioned in the raw task text.
564
+ * "D:\\w\\acme-app\\src\\pad.js" -> ["acme-app"]; only the deepest non-generic segment is
565
+ * kept, so a user name or a workspace root can never make two projects look alike.
566
+ */
567
+ export function mentionedDirs(text, limit = 6) {
568
+ const found = new Set()
569
+ for (const match of String(text || '').matchAll(/(?:[A-Za-z]:)?(?:[\\/][A-Za-z0-9_.-]+)+/g)) {
570
+ // "node-project/feature" is a task type, not a directory.
571
+ if (/^[a-z][a-z-]*\/[a-z][a-z-]*$/.test(match[0])) continue
572
+ const parts = match[0].split(/[\\/]+/).filter(Boolean).filter((part) => !/^[A-Za-z]:$/.test(part))
573
+ // A lone "/feature" is the tail of "task-type/feature", not a path.
574
+ if (!/^[A-Za-z]:/.test(match[0]) && parts.length < 2) continue
575
+ if (/^[A-Za-z0-9_.-]+\.[A-Za-z0-9]+$/.test(parts[parts.length - 1] || '')) parts.pop()
576
+ const dirs = parts.map((part) => part.toLowerCase()).filter((part) => part.length >= 2 && !GENERIC_DIRS.has(part))
577
+ if (!dirs.length) continue
578
+ found.add(dirs[dirs.length - 1])
579
+ }
580
+ return [...found].slice(0, limit)
581
+ }
582
+
583
+ /** Idempotent: redacts a record written by an older build that had no redaction. */
584
+ export function scrubRecord(record) {
585
+ if (!record || typeof record !== 'object') return false
586
+ let changed = false
587
+ const fix = (value) => {
588
+ if (typeof value !== 'string') return value
589
+ const next = redact(value)
590
+ if (next !== value) changed = true
591
+ return next
592
+ }
593
+ for (const key of ['title', 'when', 'evidence']) {
594
+ if (typeof record[key] === 'string') record[key] = fix(record[key])
595
+ }
596
+ for (const key of ['steps', 'reverse_examples', 'pitfalls', 'keywords']) {
597
+ if (Array.isArray(record[key])) record[key] = record[key].map(fix)
598
+ }
599
+ return changed
600
+ }
601
+
602
+ /** Project slugs recorded before `scope` existed, e.g. keywords "acme-app", "acme-app/**". */
603
+ export function scopeOf(record) {
604
+ if (!record) return []
605
+ if (Array.isArray(record.scope) && record.scope.length) return record.scope.map((item) => String(item).toLowerCase())
606
+ const out = []
607
+ for (const word of record.keywords || []) {
608
+ const text = String(word)
609
+ if (/^[a-z][a-z-]*\/[a-z][a-z-]*$/.test(text)) continue // taskType, not a directory
610
+ const head = text.split(/[\\/*]/)[0].toLowerCase()
611
+ if (/^[a-z][a-z0-9]*(?:-[a-z0-9]+)+$/.test(head)) out.push(head)
612
+ }
613
+ return [...new Set(out)]
614
+ }
615
+
616
+ /** Two records describe different targets when both name a project dir and the names disagree. */
617
+ /** Scope tokens that actually name a project, not the tool-chain around it. */
618
+ export function projectScope(record) {
619
+ return scopeOf(record).filter((item) => item.length >= 2 && !GENERIC_DIRS.has(item))
620
+ }
621
+
622
+ export function scopeConflicts(existing, draft) {
623
+ const left = projectScope(existing)
624
+ const right = projectScope(draft)
625
+ if (!left.length || !right.length) return false
626
+ return !left.some((item) => right.includes(item))
627
+ }
628
+
629
+ /** Raw basename of a written artifact, extension stripped ("src/pad.js" -> "pad"). */
630
+ export function artifactName(call) {
631
+ const raw = firstString(call && (call.hint || call.argsHint || call.args)) || ''
632
+ const parts = raw.split(/[\\/]+/).filter(Boolean)
633
+ const base = (parts[parts.length - 1] || '').replace(/\.[^.]+$/, '')
634
+ return base.replace(/[^A-Za-z0-9_.-]/g, '').trim()
635
+ }
636
+
637
+ export function buildCandidate(trace, info, config) {
638
+ const cfg = { ...DEFAULT_CONFIG, ...(config || {}) }
639
+ const calls = ((trace && trace.calls) || []).slice(0, cfg.maxCalls)
640
+ const taskType = inferTaskType(calls, info && info.cwd)
641
+ const steps = summarizeCalls(calls)
642
+ const recoveries = findRecoveries(calls)
643
+ const writes = calls.filter((call) => call && call.ok && classify(call).kind === 'write')
644
+ const runs = calls.filter((call) => call && call.ok && classify(call).kind === 'run')
645
+
646
+ const runOnly = Boolean(info && info.runOnly) || (!writes.length && runs.length > 0)
647
+ const primary = writes.length ? classify(writes[0]).target : runs.length ? classify(runs[0]).target : ''
648
+ const scriptName = runOnly ? runArtifactName(calls) : ''
649
+ const verb = writes.length ? '写' : '跑通'
650
+ // Prefer a concrete artifact name (raw basename, before path normalization):
651
+ // "<path>/<name>.js" cannot name what was produced, "slugify.js" can.
652
+ const named = writes
653
+ .map((call) => artifactName(call))
654
+ .find((name) => /^[A-Za-z0-9_][A-Za-z0-9_.-]*$/.test(name) && /[A-Za-z0-9]{2,}/.test(name))
655
+ // Scope is computed before rendering so the title and the trigger can name the
656
+ // project. "新增 index" is unreadable in a review list; "新增 index(dsh-experience)"
657
+ // tells the human which project the recipe belongs to.
658
+ const scope = mentionedDirs(
659
+ [
660
+ ...(calls || []).map((call) => firstString(call && (call.hint || call.argsHint || call.args))),
661
+ trace && trace.query,
662
+ ].join(' '),
663
+ )
664
+ const project = projectScope({ scope })[0] || ''
665
+ const suffix = project ? `(${project})` : ''
666
+ const title = clip(
667
+ runOnly
668
+ ? scriptName
669
+ ? `跑 ${scriptName}${suffix}`
670
+ : primary
671
+ ? `跑 ${primary}`
672
+ : verbsOf(taskType)
673
+ : named
674
+ ? `新增 ${named}${suffix}`
675
+ : primary
676
+ ? `${verb} ${primary}`
677
+ : verbsOf(taskType),
678
+ 110,
679
+ )
680
+ const place = workspaceName(info && info.cwd)
681
+ const testRun = runs.find((call) => TEST_COMMAND.test(firstString(call && (call.hint || call.argsHint || call.args))))
682
+ const testCommand = testRun ? classify(testRun).target : ''
683
+ // The trigger line must discriminate between recipes, not repeat the task type
684
+ // ("在 desktop 里 新增函数/模块并配用例" fits every single candidate).
685
+ const when = clip(
686
+ project
687
+ ? `在 ${project} 里 ${verbsOf(taskType)}${testCommand ? `,验证 ${testCommand}` : ''}`
688
+ : testCommand
689
+ ? `当需要${verbsOf(taskType)}时,验证 ${testCommand}`
690
+ : place
691
+ ? `在 ${place} 里 ${verbsOf(taskType)}`
692
+ : `当需要${verbsOf(taskType)}时`,
693
+ 160,
694
+ )
695
+ const keywords = [...new Set([taskType, ...steps.map((step) => step.split(' ')[1] || '').filter(Boolean)])]
696
+ .filter(Boolean)
697
+ .slice(0, 12)
698
+ .map((word) => clip(word, 40))
699
+
700
+ const record = {
701
+ status: 'candidate',
702
+ platform: process.platform,
703
+ // Task shape as recorded, not as re-guessed from prose later: a write task and
704
+ // a "run this script" task must never merge even when they share every noun.
705
+ intent: runOnly ? 'run' : 'write',
706
+ // Scope detection needs the raw paths; every rendered field below is normalized.
707
+ scope,
708
+ fingerprint: outcomeFingerprint(calls),
709
+ title,
710
+ taskType,
711
+ when,
712
+ keywords,
713
+ steps,
714
+ reverse_examples: recoveries,
715
+ evidence: `${(info && info.sessionId) || '?'}:turn-${(trace && trace.turn) || '?'} · calls=${calls.length} errors=${calls.filter((call) => call && !call.ok).length}`,
716
+ source: { auto: true, agentId: (info && info.agentId) || 'unknown', at: new Date().toISOString(), origin: 'l1' },
717
+ verify: null,
718
+ confidence: 0.25,
719
+ uses: 0,
720
+ lastUsedAt: null,
721
+ occurrences: 1,
722
+ }
723
+
724
+ if (JSON.stringify(record).length > cfg.maxRecordChars) {
725
+ record.truncated = true
726
+ while (record.steps.length > 1 && JSON.stringify(record).length > cfg.maxRecordChars) record.steps.pop()
727
+ while (record.reverse_examples.length > 1 && JSON.stringify(record).length > cfg.maxRecordChars) {
728
+ record.reverse_examples.pop()
729
+ }
730
+ }
731
+ return record
732
+ }
733
+
734
+ export function platformOf(record) {
735
+ const value = record && record.platform
736
+ return typeof value === 'string' ? value : ''
737
+ }
738
+
739
+ /**
740
+ * "PATH has no node, use the bundled node.exe" is a fact on win32 and a lie on
741
+ * darwin. Unknown platform never vetoes: records written before v1.1 carry no
742
+ * tag, and every one of them was learned on this machine.
743
+ */
744
+ /**
745
+ * Old records carry no platform tag: they were learned before isolation existed.
746
+ * They stay usable, but must not outrank a record that was proven on this machine.
747
+ */
748
+ export function platformFactor(record) {
749
+ const tag = platformOf(record)
750
+ if (!tag) return 0.85
751
+ return tag === process.platform ? 1 : 0
752
+ }
753
+
754
+ export function platformConflicts(record) {
755
+ const tag = platformOf(record)
756
+ return Boolean(tag && tag !== process.platform)
757
+ }
758
+
759
+ export function mergeInto(existing, draft) {
760
+ existing.occurrences = (existing.occurrences || 1) + 1
761
+ existing.lastSeenAt = new Date().toISOString()
762
+ // Older records had no fingerprint; adopt this run's so the next occurrence matches.
763
+ if (!existing.fingerprint && draft && draft.fingerprint) existing.fingerprint = draft.fingerprint
764
+ const canonical = existing.reverse_examples || existing.pitfalls || []
765
+ const seen = new Set(canonical)
766
+ const added = []
767
+ for (const item of draft.reverse_examples || []) {
768
+ if (!seen.has(item)) {
769
+ seen.add(item)
770
+ added.push(item)
771
+ }
772
+ }
773
+ existing.reverse_examples = [...canonical, ...added].slice(0, 10)
774
+
775
+ const stepSeen = new Set(existing.steps || [])
776
+ const newSteps = []
777
+ for (const step of draft.steps || []) {
778
+ if (!stepSeen.has(step) && newSteps.length < 4) {
779
+ stepSeen.add(step)
780
+ newSteps.push(step)
781
+ }
782
+ }
783
+ if (newSteps.length) existing.steps = [...(existing.steps || []), ...newSteps].slice(0, 10)
784
+
785
+ if (draft.evidence && existing.evidence && !existing.evidence.includes(draft.evidence)) {
786
+ existing.evidence = clip(`${existing.evidence}; ${draft.evidence}`, 240)
787
+ }
788
+ return { added, occurrences: existing.occurrences, newSteps }
789
+ }
790
+
791
+ export function reviewTargets(experiences) {
792
+ return (experiences || []).filter((item) => item && (item.status === 'candidate' || item.status === 'auto-verified'))
793
+ }
794
+
795
+ /**
796
+ * G8: a recipe that was rejected as rule-conflicting must not be re-adopted.
797
+ * A new turn that reproduces the same fingerprint would otherwise create a fresh
798
+ * candidate and wait for the same human to reject it again.
799
+ */
800
+ export function gateSuperseded(draft, experiences) {
801
+ const hit = (experiences || []).find(
802
+ (item) => item && item.status === 'superseded' && fingerprintsMatch(item, draft),
803
+ )
804
+ if (!hit) return { ok: true }
805
+ const rule = (hit.supersede && (hit.supersede.rule || hit.supersede.reason)) || '规则冲突'
806
+ return { ok: false, hit, reason: `G8: 与已否决经验 ${hit.id} 指纹相同(${rule})→ 不再收录` }
807
+ }
808
+
809
+ // Forty candidates in one screen is not a decision anyone can make.
810
+ const REVIEW_LIMIT = 10
811
+
812
+ export function renderReview(experiences) {
813
+ const candidates = reviewTargets(experiences)
814
+ const rejected = (experiences || []).filter((item) => item && item.status === 'superseded')
815
+ if (!candidates.length) {
816
+ return rejected.length
817
+ ? `没有待确认的候选经验。\n已否决(规则冲突)${rejected.length} 条,不再收录、也不会召回。`
818
+ : '没有待确认的候选经验。'
819
+ }
820
+ const auto = candidates.filter((item) => item.source && item.source.auto).length
821
+ const lines = [`候选 ${candidates.length} 条(自动捕获 ${auto} / 手动 ${candidates.length - auto})|批准后即可跨会话召回`, '']
822
+ const shownList = candidates.slice(0, REVIEW_LIMIT)
823
+ shownList.forEach((item, index) => {
824
+ const flags = [item.source && item.source.auto ? 'auto' : 'manual']
825
+ lines.push(`#${index + 1} ${item.id} [${flags.join('/')}] ${item.title}`)
826
+ lines.push(` 触发条件: ${item.when || '-'}`)
827
+ const steps = item.steps || []
828
+ const shown = steps.slice(0, 3).map((step, i) => `${i + 1}) ${step}`).join(' ')
829
+ const more = steps.length > 3 ? ` …+${steps.length - 3}` : ''
830
+ lines.push(` 核心步骤: ${shown || '-'}${more}`)
831
+ const pitfalls = item.reverse_examples || item.pitfalls || []
832
+ lines.push(` 避坑点: ${pitfalls.length ? pitfalls.join(';') : '(无)'}`)
833
+ lines.push(` id=${item.id} occurrences=${item.occurrences || 1}`)
834
+ lines.push('')
835
+ })
836
+ if (rejected.length) {
837
+ lines.push(`已否决(规则冲突)${rejected.length} 条,不再召回,仅用于防止重复收录:`)
838
+ for (const item of rejected.slice(0, 5)) {
839
+ const why = (item.supersede && (item.supersede.rule || item.supersede.reason)) || '规则冲突'
840
+ lines.push(` ${item.id} ${item.title} · ${why}`)
841
+ }
842
+ lines.push('')
843
+ }
844
+ if (candidates.length > shownList.length) lines.push(`…另有 ${candidates.length - shownList.length} 条未显示(先处理上面这些再 review)`)
845
+ lines.push('批量: /memory review --approve-all')
846
+ lines.push('单条: /memory review approve 1 | /memory review reject 1')
847
+ return lines.join('\n')
848
+ }
849
+ /**
850
+ * Coerce a user-typed config value to the type of its default.
851
+ * "/memory config set autoPromote true" must store boolean true, not the string "true".
852
+ */
853
+ export function coerceConfigValue(key, raw, defaults = DEFAULT_CONFIG) {
854
+ if (!(key in defaults)) throw new Error('未知配置项 ' + key)
855
+ const fallback = defaults[key]
856
+ const text = String(raw).trim()
857
+ if (typeof fallback === 'boolean') {
858
+ if (/^(true|1|yes|on)$/i.test(text)) return true
859
+ if (/^(false|0|no|off)$/i.test(text)) return false
860
+ throw new Error(key + ' 是布尔值,接受 true/false')
861
+ }
862
+ if (typeof fallback === 'number') {
863
+ const num = Number(text)
864
+ if (!Number.isFinite(num)) throw new Error(key + " 是数字,收到 " + text)
865
+ return num
866
+ }
867
+ return text
868
+ }