@yolk_vat-y/dsh-project-memory 0.5.14 → 0.5.15
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/CHANGELOG.md +115 -0
- package/README.md +77 -108
- package/README.zh-CN.md +71 -100
- package/package.json +3 -1
- package/scripts/file-peak.mjs +356 -0
- package/scripts/tool-schema-size.mjs +131 -0
- package/src/doc-pipeline.js +3 -6
- package/src/enhancer.js +105 -41
- package/src/index.js +4 -1
- package/src/ops.js +11 -13
- package/src/parsers/pdfjs-parser.js +0 -30
- package/src/readiness.js +5 -12
- package/src/store.js +48 -7
- package/src/symbols.js +2 -1
- package/src/tools/forget.js +1 -1
- package/src/tools/index-doc.js +1 -1
- package/src/tools/index-repo.js +19 -5
- package/src/tools/lesson-tools.js +19 -16
- package/src/tools/query-memory.js +1 -1
- package/src/tools/remember.js +1 -1
- package/src/tools/stats.js +1 -1
- package/src/tools/task-tools.js +6 -5
- package/src/tools/watch-repo.js +1 -1
- package/src/util/entry-id.js +32 -0
- package/src/util/fs.js +18 -2
- package/src/util/path-token.js +40 -0
- package/src/watch.js +15 -3
package/src/enhancer.js
CHANGED
|
@@ -3,6 +3,7 @@ import { readFileSync } from 'node:fs'
|
|
|
3
3
|
import { join } from 'node:path'
|
|
4
4
|
import { createRequire } from 'node:module'
|
|
5
5
|
import { oneLineDeclaration } from './util/text.js'
|
|
6
|
+
import { entryIdPrefix } from './util/entry-id.js'
|
|
6
7
|
|
|
7
8
|
const require = createRequire(import.meta.url)
|
|
8
9
|
|
|
@@ -95,8 +96,22 @@ const PRIORITY = {
|
|
|
95
96
|
BACKLOG: 3
|
|
96
97
|
}
|
|
97
98
|
|
|
99
|
+
/**
|
|
100
|
+
* TS 增强队列的上界。
|
|
101
|
+
*
|
|
102
|
+
* 为什么需要它(0.5.15 加固):`index_repo` 对**全部** TS/JS 一次性入队,而队列此前
|
|
103
|
+
* 没有任何 cap。配合下面的"出队才解析",队列里每条只剩元数据(≈ 几百字节),默认
|
|
104
|
+
* `maxScanFiles`(20000)量级下上限约几 MB —— 这个数取成与扫描上限同量级,正常仓库够不到,
|
|
105
|
+
* 它只是防御"被抬高的 maxScanFiles / 异常调用方"的兜底阀。
|
|
106
|
+
*/
|
|
107
|
+
const ENHANCE_QUEUE_MAX = 20000
|
|
108
|
+
|
|
98
109
|
const enhanceQueue = []
|
|
110
|
+
let enhanceQueueMax = ENHANCE_QUEUE_MAX
|
|
99
111
|
let processing = false
|
|
112
|
+
/** `dropped` 计数两类丢弃:被挤掉的旧任务与被拒的新任务;`parsed` 是"出队后才解析"的证据。 */
|
|
113
|
+
const enhanceStats = { dropped: 0, parsed: 0 }
|
|
114
|
+
let droppedWarned = false
|
|
100
115
|
|
|
101
116
|
function getCacheKey(content) {
|
|
102
117
|
const hash = createHash('sha256').update(content).digest('hex').slice(0, 16)
|
|
@@ -337,6 +352,20 @@ export function deepParseWithTS(filePath, content) {
|
|
|
337
352
|
return symbols
|
|
338
353
|
}
|
|
339
354
|
|
|
355
|
+
/**
|
|
356
|
+
* 入队一个 TS 增强任务。两条不变式(0.5.15 加固):
|
|
357
|
+
*
|
|
358
|
+
* 1. **任务体在出队时才跑**。入队只读一次文件算 `cacheKey`(16 字节哈希),正文与解析结果
|
|
359
|
+
* 都不留在队列里;真正读正文 + `deepParseWithTS` + 提交发生在 `runEnhance()`。
|
|
360
|
+
* 旧实现在入队时就同步跑到第一个 `await`,于是 `index_repo` 对整棵 TS 树一次性入队 =
|
|
361
|
+
* 同时持有**所有**文件的正文与符号数组 —— 长驻 `dsh web` 的 RSS 单调爬升就是这个形状。
|
|
362
|
+
* 2. **有界**。队列长度不超过 `enhanceQueueMax`,超了先挤队尾(最不紧急的),挤不动就丢新
|
|
363
|
+
* 来的那条;被丢的任务 promise 立刻 resolve(调用方都是 fire-and-forget),丢弃计数由
|
|
364
|
+
* `enhanceQueueStats()` 暴露、并由 `index_repo` 写进报告 —— 不静默。
|
|
365
|
+
*
|
|
366
|
+
* 去重语义与旧实现一致(同 relPath 同内容复用,内容变了换掉)。已经开始跑的任务不在队列里,
|
|
367
|
+
* 因此不受替换/挤掉影响。
|
|
368
|
+
*/
|
|
340
369
|
export function enqueueEnhance(store, relPath, filePath, priority = PRIORITY.BATCH, config, root) {
|
|
341
370
|
if (!ts) return Promise.resolve()
|
|
342
371
|
|
|
@@ -352,59 +381,92 @@ export function enqueueEnhance(store, relPath, filePath, priority = PRIORITY.BAT
|
|
|
352
381
|
}
|
|
353
382
|
const cacheKey = getCacheKey(content)
|
|
354
383
|
|
|
355
|
-
//
|
|
384
|
+
// 去重语义与旧实现一致:同 relPath 同内容 → 复用队列里那条 promise;内容变了 → 换掉它。
|
|
356
385
|
const existingIdx = enhanceQueue.findIndex(q => q.relPath === relPath)
|
|
357
386
|
if (existingIdx >= 0) {
|
|
358
387
|
const existing = enhanceQueue[existingIdx]
|
|
359
388
|
if (existing.cacheKey === cacheKey) {
|
|
360
|
-
// Same content, just update priority if higher
|
|
361
389
|
if (priority < existing.priority) {
|
|
362
|
-
|
|
390
|
+
existing.priority = priority
|
|
363
391
|
enhanceQueue.sort((a, b) => a.priority - b.priority)
|
|
364
392
|
}
|
|
365
393
|
return existing.promise
|
|
366
|
-
} else {
|
|
367
|
-
// Content changed - replace the task
|
|
368
|
-
enhanceQueue[existingIdx] = { relPath, filePath, priority, cacheKey, promise: null }
|
|
369
|
-
// Will create new promise below
|
|
370
394
|
}
|
|
395
|
+
// 内容变了:那条还没开始跑(跑起来的早已出队)→ 换掉,并让它的 promise 立刻 resolve,
|
|
396
|
+
// 否则调用方 await 的那个 promise 永远悬着。
|
|
397
|
+
existing.resolve()
|
|
398
|
+
enhanceQueue.splice(existingIdx, 1)
|
|
371
399
|
}
|
|
372
400
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
entry.promise = (async () => {
|
|
390
|
-
try {
|
|
391
|
-
const content = readFileSync(filePath, 'utf8')
|
|
392
|
-
const enhanced = deepParseWithTS(filePath, content)
|
|
393
|
-
if (enhanced?.length) {
|
|
394
|
-
await store.commit(fn => applyEnhancedSymbols(fn, relPath, enhanced))
|
|
395
|
-
}
|
|
396
|
-
} catch (err) {
|
|
397
|
-
console.warn(`[dsh-project-memory] enhance failed for ${relPath}: ${err.message}`)
|
|
398
|
-
} finally {
|
|
399
|
-
// 只摘自己那一条:同 relPath 的新任务可能已经把它替换掉了(那种情况这里是 -1)。
|
|
400
|
-
const idx = enhanceQueue.indexOf(entry)
|
|
401
|
-
if (idx >= 0) enhanceQueue.splice(idx, 1)
|
|
401
|
+
const task = { store, relPath, filePath, priority, cacheKey, promise: null, resolve: null }
|
|
402
|
+
task.promise = new Promise((resolve) => { task.resolve = resolve })
|
|
403
|
+
|
|
404
|
+
if (enhanceQueue.length >= enhanceQueueMax) {
|
|
405
|
+
// 满了:优先挤掉队尾(`priority` 数字最大 = 最不紧急)的那条;挤不动(对方更紧急)
|
|
406
|
+
// 就丢掉新来的这条。两条路都计数 + 一次告警,绝不静默。
|
|
407
|
+
const last = enhanceQueue[enhanceQueue.length - 1]
|
|
408
|
+
if (last && last.priority > priority) {
|
|
409
|
+
enhanceQueue.pop()
|
|
410
|
+
last.resolve()
|
|
411
|
+
warnDropped()
|
|
412
|
+
} else {
|
|
413
|
+
warnDropped()
|
|
414
|
+
task.resolve()
|
|
415
|
+
return task.promise
|
|
402
416
|
}
|
|
403
|
-
}
|
|
404
|
-
enhanceQueue.sort((a, b) => a.priority - b.priority)
|
|
417
|
+
}
|
|
405
418
|
|
|
419
|
+
enhanceQueue.push(task)
|
|
420
|
+
enhanceQueue.sort((a, b) => a.priority - b.priority)
|
|
406
421
|
scheduleProcess()
|
|
407
|
-
return
|
|
422
|
+
return task.promise
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** 只打一次:这是**降级说明**(有些文件这轮没有 L2 增强),不是每轮都刷屏的故障。 */
|
|
426
|
+
function warnDropped() {
|
|
427
|
+
enhanceStats.dropped++
|
|
428
|
+
if (droppedWarned) return
|
|
429
|
+
droppedWarned = true
|
|
430
|
+
console.warn(
|
|
431
|
+
`[dsh-project-memory] TS 增强队列已满(上限 ${enhanceQueueMax}):优先级最低的任务被丢弃。` +
|
|
432
|
+
'这些文件仍有 L1 正则符号,只是没有 L2 类型增强;下次索引 / 改动会重新入队。',
|
|
433
|
+
)
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* 队列现状。`index_repo` 用它把"这轮丢了多少"写进报告(不静默),测试用它断言
|
|
438
|
+
* 「入队同步阶段只解析 1 个」与上界。
|
|
439
|
+
*/
|
|
440
|
+
export function enhanceQueueStats() {
|
|
441
|
+
return { queued: enhanceQueue.length, max: enhanceQueueMax, dropped: enhanceStats.dropped, parsed: enhanceStats.parsed }
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** 仅供测试:临时收窄队列上限(返回恢复函数),免得测试真去排两万条。 */
|
|
445
|
+
export function _setEnhanceQueueMaxForTest(n) {
|
|
446
|
+
const previous = enhanceQueueMax
|
|
447
|
+
enhanceQueueMax = Number.isFinite(n) && n > 0 ? n : ENHANCE_QUEUE_MAX
|
|
448
|
+
return () => { enhanceQueueMax = previous }
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** 出队之后才真正干活:此刻才读正文、才解析(正文与解析结果只活到这一步结束)。 */
|
|
452
|
+
async function runEnhance(task) {
|
|
453
|
+
let content
|
|
454
|
+
try {
|
|
455
|
+
content = readFileSync(task.filePath, 'utf8')
|
|
456
|
+
} catch {
|
|
457
|
+
return // 同入队路径:文件没了不是故障,安静跳过
|
|
458
|
+
}
|
|
459
|
+
enhanceStats.parsed++
|
|
460
|
+
try {
|
|
461
|
+
const enhanced = deepParseWithTS(task.filePath, content)
|
|
462
|
+
if (enhanced?.length) {
|
|
463
|
+
await task.store.commit(fn => applyEnhancedSymbols(fn, task.relPath, enhanced))
|
|
464
|
+
}
|
|
465
|
+
} catch (err) {
|
|
466
|
+
console.warn(`[dsh-project-memory] enhance failed for ${task.relPath}: ${err.message}`)
|
|
467
|
+
} finally {
|
|
468
|
+
task.resolve()
|
|
469
|
+
}
|
|
408
470
|
}
|
|
409
471
|
|
|
410
472
|
function scheduleProcess() {
|
|
@@ -415,9 +477,11 @@ function scheduleProcess() {
|
|
|
415
477
|
while (enhanceQueue.length > 0) {
|
|
416
478
|
const task = enhanceQueue.shift()
|
|
417
479
|
try {
|
|
418
|
-
await task
|
|
480
|
+
await runEnhance(task)
|
|
419
481
|
} catch (err) {
|
|
482
|
+
// runEnhance 内部已兜底;这里再兜一层,保证调度循环不会因单条任务死掉。
|
|
420
483
|
console.warn(`[dsh-project-memory] enhance task failed: ${err.message}`)
|
|
484
|
+
task.resolve()
|
|
421
485
|
}
|
|
422
486
|
await new Promise(r => setImmediate(r))
|
|
423
487
|
}
|
|
@@ -457,7 +521,7 @@ function applyEnhancedSymbols(fn, relPath, enhanced) {
|
|
|
457
521
|
const newEntries = []
|
|
458
522
|
for (const s of enhanced) {
|
|
459
523
|
if (existingKeys.has(`${s.name}#${s.line}`)) continue
|
|
460
|
-
const base = `${relPath
|
|
524
|
+
const base = `${entryIdPrefix(relPath)}#${s.line}`
|
|
461
525
|
let id = base
|
|
462
526
|
if (usedIds.has(id)) id = `${base}-${s.kind}`
|
|
463
527
|
for (let n = 2; usedIds.has(id); n++) id = `${base}-${s.kind}-${n}`
|
package/src/index.js
CHANGED
|
@@ -24,7 +24,10 @@ export const Config = Schema.object({
|
|
|
24
24
|
memoryDir: Schema.string().default('.dsh-project-memory'),
|
|
25
25
|
chunkChars: Schema.number().default(3000),
|
|
26
26
|
maxChunksPerFile: Schema.number().default(40),
|
|
27
|
-
|
|
27
|
+
// 单文件内存闸门,不是磁盘设置:索引一个文件要先把它整个物化,峰值随文件大小线性走。
|
|
28
|
+
// 实测边际系数(峰值 RSS / 磁盘 MB,见 bench/file-peak.mjs 与 README「单文件内存预算」):
|
|
29
|
+
// 典型源码 ~10×、符号密集代码 ~21×、文本型 PDF ~19×。10 把单文件增量压在 ~+300 MB。
|
|
30
|
+
maxFileSizeMb: Schema.number().default(10),
|
|
28
31
|
maxOutputChars: Schema.number().default(8000),
|
|
29
32
|
maxPdfPages: Schema.number().default(1000),
|
|
30
33
|
llmQueryExpansion: Schema.boolean().default(false),
|
package/src/ops.js
CHANGED
|
@@ -6,15 +6,10 @@
|
|
|
6
6
|
//
|
|
7
7
|
// 分工:ops.js 只负责"把调用读成什么动作",不负责决定注入什么(那是 readiness/auto-inject)。
|
|
8
8
|
// 刻意不 import readiness:readiness 要 import 本模块的 op 闭集,反向依赖会造成循环。
|
|
9
|
+
// 路径 token 的规则因此收在 util/path-token.js —— 它谁也不依赖,两个模块都能 import
|
|
10
|
+
// (原先这里就地抄了一份,注释还写着"与 readiness.extractPaths 同规则",实际已漂移)。
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
const PATH_TOKEN = /(?:[A-Za-z0-9_.@-]+\/)*[A-Za-z0-9_.@-]+\.[A-Za-z][A-Za-z0-9]{0,7}\b/g
|
|
12
|
-
|
|
13
|
-
function extractPaths(text) {
|
|
14
|
-
const s = String(text || '')
|
|
15
|
-
if (!s) return []
|
|
16
|
-
return [...new Set([...s.matchAll(PATH_TOKEN)].map((m) => m[0]))]
|
|
17
|
-
}
|
|
12
|
+
import { extractPathTokens } from './util/path-token.js'
|
|
18
13
|
|
|
19
14
|
/** op 闭集。新增一个 op 必须同时想清楚:它是动作,不是话题。 */
|
|
20
15
|
export const OP_IDS = [
|
|
@@ -133,7 +128,7 @@ export function classifyShellCommand(command) {
|
|
|
133
128
|
}
|
|
134
129
|
}
|
|
135
130
|
if (!ops.length) ops.push('shell-run')
|
|
136
|
-
const targets = SHELL_WRITE_RE.test(cmd) ?
|
|
131
|
+
const targets = SHELL_WRITE_RE.test(cmd) ? extractPathTokens(cmd) : []
|
|
137
132
|
const hosts = WSL_HINT_RE.test(cmd) || /\b(powershell|pwsh|cmd)\.exe\b/i.test(cmd) ? ['wsl'] : []
|
|
138
133
|
return { ops, targets, hosts }
|
|
139
134
|
}
|
|
@@ -152,7 +147,7 @@ export function classifyToolCall(name, argsText) {
|
|
|
152
147
|
if (FETCH_TOOLS.has(tool)) return { ops: ['fetch-web'], targets: [], hosts: [] }
|
|
153
148
|
if (QUERY_TOOLS.has(tool)) return { ops: ['query-memory'], targets: [], hosts: [] }
|
|
154
149
|
if (WRITE_TOOLS.has(tool)) {
|
|
155
|
-
const targets = path ? [path, ...
|
|
150
|
+
const targets = path ? [path, ...extractPathTokens(path)] : []
|
|
156
151
|
const hosts = targets.some((t) => WSL_HINT_RE.test(t)) ? ['wsl'] : []
|
|
157
152
|
return { ops: ['file-write'], targets: [...new Set(targets)], hosts }
|
|
158
153
|
}
|
|
@@ -191,9 +186,12 @@ export function activityFromText(text) {
|
|
|
191
186
|
// 先按"工具调用"解析(`edit {"file_path":"..."}`):结构化参数比正则猜命令可靠得多。
|
|
192
187
|
const call = /^\s*([A-Za-z_][\w-]*)\s+(\{[\s\S]*\})\s*$/.exec(line)
|
|
193
188
|
const r = call ? classifyToolCall(call[1], call[2]) : classifyShellCommand(line)
|
|
194
|
-
// 纯文本行里没有 shell 动作时不要退化成 shell-run
|
|
195
|
-
|
|
196
|
-
|
|
189
|
+
// 纯文本行里没有 shell 动作时不要退化成 shell-run(那是噪音源)。但**只丢这个 op**:
|
|
190
|
+
// 行里的写目标/主机是具体事实(要靠路径 token、/mnt/ 这类签名才抽得出,泛泛的散文产不出来),
|
|
191
|
+
// 一起丢掉会让 `bash {"command":"echo x >> .gitignore"}` 这种 shell-run-only 的写意图
|
|
192
|
+
// 到不了 when.writes —— 实测它连结构化工具调用行的写目标都会被静默吞掉。
|
|
193
|
+
const noiseOp = r.ops.length === 1 && r.ops[0] === 'shell-run' && !/\b\w+\s+-/.test(line)
|
|
194
|
+
if (!noiseOp) for (const op of r.ops) ops.add(op)
|
|
197
195
|
for (const t of r.targets) targets.add(t)
|
|
198
196
|
for (const h of r.hosts) hosts.add(h)
|
|
199
197
|
}
|
|
@@ -107,36 +107,6 @@ export async function parsePdf(filePath, { pages = null, maxPages = 1000, backen
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
export async function parsePdfInfo(filePath, maxPages = 1000) {
|
|
111
|
-
const data = new Uint8Array(await readFile(filePath))
|
|
112
|
-
const { getDocument } = await loadPdfjs()
|
|
113
|
-
const loadingTask = getDocument({ data, ...PDFJS_OPTIONS })
|
|
114
|
-
try {
|
|
115
|
-
const doc = await loadingTask.promise
|
|
116
|
-
if (doc.numPages > maxPages) {
|
|
117
|
-
throw new Error(`PDF has ${doc.numPages} pages, over the maxPages limit of ${maxPages}`)
|
|
118
|
-
}
|
|
119
|
-
let meta = {}
|
|
120
|
-
try {
|
|
121
|
-
meta = await doc.getMetadata()
|
|
122
|
-
} catch {
|
|
123
|
-
// metadata is optional
|
|
124
|
-
}
|
|
125
|
-
const info = meta.info || {}
|
|
126
|
-
return {
|
|
127
|
-
pageCount: doc.numPages,
|
|
128
|
-
title: info.Title ?? null,
|
|
129
|
-
author: info.Author ?? null,
|
|
130
|
-
subject: info.Subject ?? null,
|
|
131
|
-
created: info.CreationDate ?? null,
|
|
132
|
-
modified: info.ModDate ?? null,
|
|
133
|
-
encrypted: doc.isEncrypted,
|
|
134
|
-
}
|
|
135
|
-
} finally {
|
|
136
|
-
await loadingTask.destroy()
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
110
|
export function configurePdfjsWorker(workerSrc) {
|
|
141
111
|
configuredWorkerSrc = workerSrc
|
|
142
112
|
if (pdfjsPromise) {
|
package/src/readiness.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { FALLBACK_OPS, WEAK_OPS, activityFromText, opForLegacyAction } from './ops.js'
|
|
20
|
+
import { extractPathTokens } from './util/path-token.js'
|
|
20
21
|
import { CJK_RANGE, tokenize } from './util/search.js'
|
|
21
22
|
|
|
22
23
|
/**
|
|
@@ -35,11 +36,6 @@ export const ACTION_LEXICON = [
|
|
|
35
36
|
['deploy', [/\bdeploy\b/i, /部署/]],
|
|
36
37
|
]
|
|
37
38
|
|
|
38
|
-
/** 带扩展名的路径 token(README.md、src/util/fs.js …)。 */
|
|
39
|
-
const PATH_TOKEN = /(?:[A-Za-z0-9_.@-]+\/)*[A-Za-z0-9_.@-]+\.[A-Za-z][A-Za-z0-9]{0,7}\b/g
|
|
40
|
-
/** 点开头的裸文件名(.gitignore、.npmrc …)。 */
|
|
41
|
-
const DOTFILE_TOKEN = /(?:^|[\s"'`(])(\.[A-Za-z0-9_-]{2,})/g
|
|
42
|
-
|
|
43
39
|
/**
|
|
44
40
|
* 文本 → 动作 id 列表(确定性、去重、词典序稳定)。
|
|
45
41
|
* @param {string} text - 人类消息或工具调用参数。
|
|
@@ -56,17 +52,14 @@ export function detectActions(text) {
|
|
|
56
52
|
}
|
|
57
53
|
|
|
58
54
|
/**
|
|
59
|
-
* 文本 → 路径 token(用于 trigger
|
|
55
|
+
* 文本 → 路径 token(用于 trigger 的 `writes` / `guard.paths` 匹配)。
|
|
56
|
+
* 规则收在 `util/path-token.js`:与 ops 抽写目标是**同一份实现**
|
|
57
|
+
* (原先这里与 ops.js 各抄一份,ops 那份漏了点开头文件名且已漂移)。
|
|
60
58
|
* @param {string} text - 工具调用参数或人类消息。
|
|
61
59
|
* @returns {string[]} 去重后的路径 token。
|
|
62
60
|
*/
|
|
63
61
|
export function extractPaths(text) {
|
|
64
|
-
|
|
65
|
-
if (!s) return []
|
|
66
|
-
const out = new Set()
|
|
67
|
-
for (const m of s.matchAll(PATH_TOKEN)) out.add(m[0])
|
|
68
|
-
for (const m of s.matchAll(DOTFILE_TOKEN)) out.add(m[1])
|
|
69
|
-
return [...out]
|
|
62
|
+
return extractPathTokens(text)
|
|
70
63
|
}
|
|
71
64
|
|
|
72
65
|
/**
|
package/src/store.js
CHANGED
|
@@ -153,19 +153,60 @@ function isRecord(value) {
|
|
|
153
153
|
* 实测两者在一个真实大仓库 store 里合计约 245MB(链接 222.8MB + searchText 22.7MB)。
|
|
154
154
|
*/
|
|
155
155
|
const PERSISTED_DERIVED = ['linkedSymbols', 'searchText', 'typeSig']
|
|
156
|
+
const PERSISTED_DERIVED_SET = new Set(PERSISTED_DERIVED)
|
|
156
157
|
|
|
157
|
-
/**
|
|
158
|
+
/** 有没有派生字段要剥。没有就走零分配路径(绝大多数条目、以及所有新写的分片)。 */
|
|
159
|
+
function hasPersistedDerived(entry) {
|
|
160
|
+
for (const key of PERSISTED_DERIVED) if (key in entry) return true
|
|
161
|
+
return false
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 按**非派生键重建**一个对象。
|
|
166
|
+
*
|
|
167
|
+
* 不用 `delete`:删除属性会把 entry 推进 V8 的字典模式,之后每个属性都变成哈希表项。
|
|
168
|
+
* 本仓库真实条目形状实测 —— `delete` 后 **1196 B/条**,重建后 **436 B/条**(20k 条 / --expose-gc)。
|
|
169
|
+
* 加载路径上这是常驻内存(`storeCache` 里所有 store 都算),写盘路径上同款浪费。
|
|
170
|
+
*
|
|
171
|
+
* `undefined` 值的键也不复制:`typeSig: undefined` 是 enhancer 的实现细节(见 src/enhancer.js),
|
|
172
|
+
* JSON 里本来就不存在,留在内存对象里只会占一个属性槽。
|
|
173
|
+
*/
|
|
174
|
+
function rebuildWithoutDerived(entry) {
|
|
175
|
+
const out = {}
|
|
176
|
+
for (const key of Object.keys(entry)) {
|
|
177
|
+
if (PERSISTED_DERIVED_SET.has(key)) continue
|
|
178
|
+
const value = entry[key]
|
|
179
|
+
if (value === undefined) continue
|
|
180
|
+
// JSON.parse 可以把 `__proto__` 变成自有属性,而 `out[key] = value` 会走去改原型 —— 显式定义。
|
|
181
|
+
if (key === '__proto__') Object.defineProperty(out, key, { value, enumerable: true, writable: true, configurable: true })
|
|
182
|
+
else out[key] = value
|
|
183
|
+
}
|
|
184
|
+
return out
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 剥掉派生字段(加载路径用)。没有派生字段时**原对象返回**(零分配热路径)。
|
|
189
|
+
* @returns {object} 不含派生字段的条目(可能是同一个对象)
|
|
190
|
+
*/
|
|
158
191
|
function stripPersistedDerived(entry) {
|
|
159
192
|
if (!entry || typeof entry !== 'object') return entry
|
|
160
|
-
|
|
161
|
-
return entry
|
|
193
|
+
if (!hasPersistedDerived(entry)) return entry
|
|
194
|
+
return rebuildWithoutDerived(entry)
|
|
162
195
|
}
|
|
163
196
|
|
|
164
|
-
/**
|
|
197
|
+
/**
|
|
198
|
+
* 返回不含派生字段的条目(写盘路径用,不能动内存里的对象)。
|
|
199
|
+
* 同样重建而不是 spread+delete —— 两个路径的字典模式代价是一样的。
|
|
200
|
+
*/
|
|
165
201
|
function withoutPersistedDerived(entry) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return
|
|
202
|
+
if (!entry || typeof entry !== 'object') return entry
|
|
203
|
+
if (!hasPersistedDerived(entry)) return entry
|
|
204
|
+
return rebuildWithoutDerived(entry)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** 仅供测试:加载/写盘路径的剥离实现(内存形状由测试用 --expose-gc 度量)。 */
|
|
208
|
+
export function _stripPersistedDerivedForTest(entry) {
|
|
209
|
+
return stripPersistedDerived(entry)
|
|
169
210
|
}
|
|
170
211
|
|
|
171
212
|
function loadJson(filePath, fallback) {
|
package/src/symbols.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs'
|
|
2
2
|
import { oneLineDeclaration } from './util/text.js'
|
|
3
|
+
import { entryIdPrefix } from './util/entry-id.js'
|
|
3
4
|
|
|
4
5
|
const JS_LIKE = new Set(['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts', '.jsx', '.tsx'])
|
|
5
6
|
const PYTHON = new Set(['.py'])
|
|
@@ -460,7 +461,7 @@ function buildSymbol(matched, relPath, rawLine, lineNo) {
|
|
|
460
461
|
const identity = `${prefix}${typeSig}${overloadPart} — ${relPath}:${lineNo}`
|
|
461
462
|
|
|
462
463
|
return {
|
|
463
|
-
id: `${
|
|
464
|
+
id: `${entryIdPrefix(relPath)}#${lineNo}`,
|
|
464
465
|
sourcePath: relPath,
|
|
465
466
|
sourceLine: lineNo,
|
|
466
467
|
type: 'symbol',
|
package/src/tools/forget.js
CHANGED
package/src/tools/index-doc.js
CHANGED
|
@@ -20,7 +20,7 @@ export function indexDocTool(ctx, config) {
|
|
|
20
20
|
},
|
|
21
21
|
root: {
|
|
22
22
|
type: 'string',
|
|
23
|
-
description: '
|
|
23
|
+
description: '项目根目录(store 存放处),默认会话的项目根。',
|
|
24
24
|
},
|
|
25
25
|
},
|
|
26
26
|
output: {
|
package/src/tools/index-repo.js
CHANGED
|
@@ -4,7 +4,7 @@ import { statSync } from 'node:fs'
|
|
|
4
4
|
import { assertIndexRoot, assertSafeRoot, memoryDirName, memoryRootFor, relativePath, scanLimits, storeKey, walkDir } from '../util/fs.js'
|
|
5
5
|
import { DROP, OVERSIZE, UNCHANGED, commitFileUpdates, fileKind, planFileIndex, toFileUpdate } from '../index-pipeline.js'
|
|
6
6
|
import { ProjectMemoryStore } from '../store.js'
|
|
7
|
-
import { onFileIndexed, isTypeScriptFile } from '../enhancer.js'
|
|
7
|
+
import { onFileIndexed, isTypeScriptFile, enhanceQueueStats } from '../enhancer.js'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* 每批提交的文件数。旧实现把整棵树的 updates 攒到最后一次性 commit:一次全量扫描的
|
|
@@ -38,7 +38,7 @@ export async function indexRepository(ctx, config, root, { reindex = false, allo
|
|
|
38
38
|
batch = []
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
const { files, truncated, skipped: nestedRoots } = walkDir(root, {
|
|
41
|
+
const { files, truncated, capped, skipped: nestedRoots, unreadable } = walkDir(root, {
|
|
42
42
|
...scanLimits(config),
|
|
43
43
|
nestedStoreName: memoryDirName(config),
|
|
44
44
|
})
|
|
@@ -78,7 +78,8 @@ export async function indexRepository(ctx, config, root, { reindex = false, allo
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
// 收尾:写完最后一批 + 清理本轮未见到的旧条目。
|
|
81
|
-
// 截断时**不做 unseen
|
|
81
|
+
// 截断时**不做 unseen 清理**:没扫到的文件不等于被删了(`truncated` 同时覆盖"读不到的
|
|
82
|
+
// 目录"——那种情况下清理会静默删掉派生索引,见 src/util/fs.js walkDir)。
|
|
82
83
|
const { removed } = commitFileUpdates(store, {
|
|
83
84
|
updates: batch,
|
|
84
85
|
unseen: truncated ? null : seen,
|
|
@@ -88,7 +89,13 @@ export async function indexRepository(ctx, config, root, { reindex = false, allo
|
|
|
88
89
|
`Indexed project: ${root}\n` +
|
|
89
90
|
`docs indexed: ${indexed}, code symbols updated: ${updated}, unchanged skipped: ${skipped}, removed: ${removed}\n` +
|
|
90
91
|
`memory store: ${stats.files} files, ${stats.entries} entries, ${stats.experience} experience notes`
|
|
91
|
-
if (
|
|
92
|
+
if (unreadable.length) {
|
|
93
|
+
const rels = unreadable.map((p) => storeKey(relativePath(root, p)))
|
|
94
|
+
report +=
|
|
95
|
+
`\ncould not read ${unreadable.length} director${unreadable.length === 1 ? 'y' : 'ies'} (permissions?): ${rels.join(', ')}` +
|
|
96
|
+
'\n(scan incomplete → nothing is treated as deleted: previously indexed files under them are kept as-is; fix permissions and re-run)'
|
|
97
|
+
}
|
|
98
|
+
if (capped) {
|
|
92
99
|
const { maxFiles, maxDepth } = scanLimits(config)
|
|
93
100
|
report +=
|
|
94
101
|
`\nscan truncated at the safety limit (maxFiles=${maxFiles}, maxDepth=${maxDepth}): only part of the tree was indexed.` +
|
|
@@ -106,9 +113,16 @@ export async function indexRepository(ctx, config, root, { reindex = false, allo
|
|
|
106
113
|
}
|
|
107
114
|
|
|
108
115
|
// Trigger TS enhancement for code files (TS/JS only), now that the base entries are on disk.
|
|
116
|
+
const dropsBefore = enhanceQueueStats().dropped
|
|
109
117
|
for (const [rel, filePath] of tsFiles) {
|
|
110
118
|
onFileIndexed(store, rel, filePath, config, root)
|
|
111
119
|
}
|
|
120
|
+
const dropped = enhanceQueueStats().dropped - dropsBefore
|
|
121
|
+
if (dropped) {
|
|
122
|
+
report +=
|
|
123
|
+
`\n${dropped} file(s) skipped TS enhancement (queue cap reached, lowest priority dropped first)` +
|
|
124
|
+
': their L1 regex symbols are unaffected; they are re-queued on the next index or change.'
|
|
125
|
+
}
|
|
112
126
|
|
|
113
127
|
return report
|
|
114
128
|
}
|
|
@@ -125,7 +139,7 @@ export function indexRepoTool(ctx, config) {
|
|
|
125
139
|
root: {
|
|
126
140
|
type: 'string',
|
|
127
141
|
required: true,
|
|
128
|
-
description: '
|
|
142
|
+
description: '要索引的项目根目录(绝对路径)。',
|
|
129
143
|
},
|
|
130
144
|
reindex: {
|
|
131
145
|
type: 'boolean',
|
|
@@ -8,18 +8,19 @@ import { INSIGHT_KINDS, INSIGHT_SCOPES } from '../insight-store.js'
|
|
|
8
8
|
import { sessionIdOf } from '../util/task-view.js'
|
|
9
9
|
|
|
10
10
|
export function lessonTool(config) {
|
|
11
|
-
|
|
11
|
+
// 描述预算:这两段以前同时出现在「工具描述」和「参数描述」里,等于付两遍钱。
|
|
12
|
+
// 现在只在参数上写一次 —— 语义一条没少,只是不再重复。
|
|
13
|
+
const kindDesc = 'lesson(踩坑/纠偏, 用 pattern+fix) | decision(选型, choice+reason) | procedure(步骤, steps) | experience(problem→solution, v0.4 兼容);默认 lesson。'
|
|
12
14
|
const scopeDesc =
|
|
13
|
-
'
|
|
15
|
+
'task(任务私有,随任务归档) | project(项目资产) | global(个人能力库,跨项目);默认 project。' +
|
|
14
16
|
'解析顺序:显式 scope > task_id > 当前绑定任务 > project。'
|
|
15
17
|
return defineTool({
|
|
16
18
|
name: 'save_lesson',
|
|
17
19
|
description:
|
|
18
|
-
'
|
|
19
|
-
'
|
|
20
|
-
'
|
|
21
|
-
'
|
|
22
|
-
'when the same insight is hit by 2+ tasks it auto-promotes task -> project, 3+ tasks project -> global. ' + kindDesc + ' ' + scopeDesc,
|
|
20
|
+
'把一条教训 / 决策 / 流程写进分层记忆(单条 insight)。新的经验优先用它,而不是 remember。' +
|
|
21
|
+
'在踩到坑、被纠正、做了值得记住的取舍时调用;日常琐事不要写。' +
|
|
22
|
+
'近重复按词重叠 ≥0.7 合并、0.65~0.7 强化;同一 insight 被 2+ 任务命中自动 task→project、3+ →global。' +
|
|
23
|
+
'kind / scope / trigger 的语义见对应参数。',
|
|
23
24
|
parameters: {
|
|
24
25
|
title: { type: 'string', description: 'One-line title (lesson: the failure pattern; decision: the topic; procedure: the name).' },
|
|
25
26
|
kind: { type: 'string', enum: INSIGHT_KINDS, description: kindDesc },
|
|
@@ -54,23 +55,25 @@ export function lessonTool(config) {
|
|
|
54
55
|
},
|
|
55
56
|
description: '收窄条件:全部满足才注入。',
|
|
56
57
|
},
|
|
57
|
-
prevents: { type: 'string', description: '
|
|
58
|
-
|
|
58
|
+
prevents: { type: 'string', description: '准入条件:不知道这条,这一步会做错什么;写不出来就不该自动注入。' },
|
|
59
|
+
// 旧字段:只为兼容历史条目与旧调用而保留 schema 形状,描述不再占 prompt 预算。
|
|
60
|
+
// 语义(keywords→intents 与召回排序、actions→when.ops、paths→when.writes、scope→画像 tag)
|
|
61
|
+
// 见 README「save_lesson」与 autoContext.legacyScope。
|
|
62
|
+
keywords: { type: 'array', items: { type: 'string' } },
|
|
59
63
|
symbols: { type: 'array', items: { type: 'string' } },
|
|
60
|
-
actions: { type: 'array', items: { type: 'string' }
|
|
61
|
-
paths: { type: 'array', items: { type: 'string' }
|
|
62
|
-
scope: { type: 'array', items: { type: 'string' }
|
|
64
|
+
actions: { type: 'array', items: { type: 'string' } },
|
|
65
|
+
paths: { type: 'array', items: { type: 'string' } },
|
|
66
|
+
scope: { type: 'array', items: { type: 'string' } },
|
|
63
67
|
},
|
|
64
68
|
description:
|
|
65
|
-
'authored trigger
|
|
66
|
-
'
|
|
67
|
-
'没有 when 的条目不会自动推送,只出现在记忆目录里供按需拉取。',
|
|
69
|
+
'authored trigger:命中即在动手前确定性注入。when 是唯一触发面(ops / writes / intents 取或),' +
|
|
70
|
+
'guard 只能收窄,没有 when 的条目不会自动推送。',
|
|
68
71
|
},
|
|
69
72
|
task_id: { type: 'string', description: '目标任务 id(scope 缺省时优先于绑定任务)' },
|
|
70
73
|
files: { type: 'array', items: { type: 'string' }, description: '关联文件(项目相对路径)' },
|
|
71
74
|
symbols: { type: 'array', items: { type: 'string' }, description: '关联符号' },
|
|
72
75
|
confidence: { type: 'number', description: '置信 0..1(默认 0.8)' },
|
|
73
|
-
root: { type: 'string', description: '
|
|
76
|
+
root: { type: 'string', description: '项目根目录,默认当前工作目录。' },
|
|
74
77
|
},
|
|
75
78
|
output: { schema: { type: 'string' }, render: (_a, v) => [{ type: 'text', text: v }] },
|
|
76
79
|
async execute(args, exec) {
|
|
@@ -46,7 +46,7 @@ export function queryMemoryTool(ctx, config) {
|
|
|
46
46
|
},
|
|
47
47
|
root: {
|
|
48
48
|
type: 'string',
|
|
49
|
-
description: '
|
|
49
|
+
description: '搜索用的项目根目录,默认当前工作目录。',
|
|
50
50
|
},
|
|
51
51
|
type: {
|
|
52
52
|
type: 'string',
|
package/src/tools/remember.js
CHANGED
package/src/tools/stats.js
CHANGED