@yolk_vat-y/dsh-project-memory 0.6.8 → 0.6.10
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 +59 -0
- package/README.md +11 -9
- package/README.zh-CN.md +11 -9
- package/client/client.js +411 -14
- package/client/client.js.map +1 -1
- package/cordis.patch.yml +4 -0
- package/package.json +4 -4
- package/skills/project-memory/SKILL.md +5 -1
- package/skills/project-memory/scripts/pm-digest.mjs +67 -5
- package/src/auto-inject.js +10 -5
- package/src/client/SettingsCard.module.css +185 -0
- package/src/client/client.ts +4 -1
- package/src/client/locales.ts +31 -0
- package/src/client/settings-card.tsx +346 -0
- package/src/commands/insight-actions.js +29 -7
- package/src/commands/tasks.js +1 -1
- package/src/enhancer.js +7 -3
- package/src/index-failures.js +46 -0
- package/src/index.js +36 -11
- package/src/insight-store.js +10 -3
- package/src/lazy.js +25 -5
- package/src/ops.js +13 -0
- package/src/parsers/pdfjs-parser.js +33 -4
- package/src/recall.js +2 -1
- package/src/setup/taskbridge.js +18 -5
- package/src/store.js +190 -78
- package/src/tools/index-doc.js +13 -3
- package/src/tools/index-repo.js +13 -4
- package/src/tools/lesson-tools.js +4 -2
- package/src/tools/query-memory.js +20 -8
- package/src/tools/stats.js +8 -0
- package/src/tools/task-tools.js +4 -0
- package/src/tools/watch-repo.js +14 -8
- package/src/util/config.js +65 -0
- package/src/util/fs.js +121 -17
- package/src/util/search.js +17 -13
- package/src/watch.js +92 -10
package/src/watch.js
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs'
|
|
1
|
+
import { existsSync, statSync } from 'node:fs'
|
|
2
2
|
import { stat } from 'node:fs/promises'
|
|
3
3
|
import path from 'node:path'
|
|
4
4
|
import { listProjectFiles, memoryDirName, memoryRootFor, relativePath, rootRejection, scanLimits, storeKey } from './util/fs.js'
|
|
5
5
|
import { OVERSIZE, UNCHANGED, commitFileUpdates, fileKind, planFileIndex, toFileUpdate } from './index-pipeline.js'
|
|
6
6
|
import { ProjectMemoryStore } from './store.js'
|
|
7
7
|
import { onFileChanged, isTypeScriptFile } from './enhancer.js'
|
|
8
|
+
import { cfgValue } from './util/config.js'
|
|
9
|
+
import { clearIndexFailure, recordIndexFailure } from './index-failures.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 把 `watch` / `watchInterval` 的**当前值**落到轮询器上。
|
|
13
|
+
*
|
|
14
|
+
* 这两个键是 volatile 字段:设置页保存后宿主会发 `loader/volatile-update`,插件不重启,
|
|
15
|
+
* 由这里按新值重排定时器。stop() 对没启动过的实例是空操作;start() 会按新间隔重算
|
|
16
|
+
* base/backoff/active 三个节拍。抽成函数是为了让"关掉后没有定时器、打开后按新间隔起"
|
|
17
|
+
* 这条契约可以被单测直接钉住(否则它只存在于 apply 的闭包里)。
|
|
18
|
+
* @param {WatchManager} watchManager 轮询器
|
|
19
|
+
* @param {object} config 插件 Config(volatile 字段是 ref)
|
|
20
|
+
* @returns {boolean} 当前是否在轮询
|
|
21
|
+
*/
|
|
22
|
+
export function applyWatchSettings(watchManager, config) {
|
|
23
|
+
watchManager.stop()
|
|
24
|
+
if (cfgValue(config, 'watch') === false) return false
|
|
25
|
+
watchManager.start(Number(cfgValue(config, 'watchInterval')) * 1000)
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
8
28
|
|
|
9
29
|
/**
|
|
10
30
|
* 有界并发 map。同步 `statSync` 循环在 Windows(尤其 WSL 挂 `/mnt/c`)会把事件循环卡住:
|
|
@@ -30,6 +50,12 @@ export class WatchManager {
|
|
|
30
50
|
this.ctx = ctx
|
|
31
51
|
this.config = config
|
|
32
52
|
this.roots = new Map()
|
|
53
|
+
/**
|
|
54
|
+
* `storeKey(root)` → `roots` 里实际使用的写法。Windows/macOS 上大小写不同的路径是同一
|
|
55
|
+
* 物理目录:没有这层索引,同一个项目会被登记两次(每轮扫两遍),而 `watch_repo({watch:false})`
|
|
56
|
+
* 只删得掉其中一条。
|
|
57
|
+
*/
|
|
58
|
+
this._rootsByKey = new Map()
|
|
33
59
|
this.timer = null
|
|
34
60
|
this._polling = false
|
|
35
61
|
this._truncationWarned = new Set()
|
|
@@ -48,7 +74,7 @@ export class WatchManager {
|
|
|
48
74
|
let best = null
|
|
49
75
|
for (const root of this.roots.keys()) {
|
|
50
76
|
const rel = path.relative(root, absPath)
|
|
51
|
-
if (rel.startsWith(
|
|
77
|
+
if (rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) continue
|
|
52
78
|
if (!best || root.length > best.length) best = root
|
|
53
79
|
}
|
|
54
80
|
return best
|
|
@@ -76,7 +102,12 @@ export class WatchManager {
|
|
|
76
102
|
dropped++
|
|
77
103
|
continue
|
|
78
104
|
}
|
|
79
|
-
|
|
105
|
+
// 文件路径(历史遗留 / 手改 watch.json)也摘掉:addRoot 现在只接受目录。
|
|
106
|
+
// hasRoot 按大小写归一判定:磁盘上可能是另一种写法的同一条。
|
|
107
|
+
if (!this.addRoot(root) && !this.hasRoot(root)) {
|
|
108
|
+
store.removeWatch(root)
|
|
109
|
+
dropped++
|
|
110
|
+
}
|
|
80
111
|
}
|
|
81
112
|
if (dropped) store.save()
|
|
82
113
|
}
|
|
@@ -85,9 +116,18 @@ export class WatchManager {
|
|
|
85
116
|
addRoot(root) {
|
|
86
117
|
// 根目录不存在就拒绝:否则每轮 poll 的 commit → save → mkdirSync 会把它重新「创建」出来。
|
|
87
118
|
if (!existsSync(root)) return false
|
|
119
|
+
// 文件不是可轮询的根:readdir/listProjectFiles 会每轮失败,持久化后还会在启动时反复自愈。
|
|
120
|
+
try {
|
|
121
|
+
if (!statSync(root).isDirectory()) return false
|
|
122
|
+
} catch {
|
|
123
|
+
return false
|
|
124
|
+
}
|
|
88
125
|
// 文件系统根 / 家目录 / 系统前缀 / roots.deny / roots.exclude 不整体监听(子目录允许),
|
|
89
126
|
// 除非配置显式放行(仅内置名单可放行)。
|
|
90
127
|
if (rootRejection(root, this.config)) return false
|
|
128
|
+
// 同一物理目录(大小写不敏感平台)不重复登记。
|
|
129
|
+
const key = storeKey(root)
|
|
130
|
+
if (this._rootsByKey.has(key)) return false
|
|
91
131
|
if (!this.roots.has(root)) {
|
|
92
132
|
// **不在这里 load()**:`restorePersisted()` 会对整条 watchlist 逐个 addRoot,急切读盘等于
|
|
93
133
|
// 「watch 过多少个根,启动就同步读多少个 store 的全部 shard」。store 到 pollRoot 需要时才取。
|
|
@@ -96,11 +136,17 @@ export class WatchManager {
|
|
|
96
136
|
// rel → 上次已上报的错误。坏文件每轮都会重试,逐轮打印会刷屏。
|
|
97
137
|
failures: new Map(),
|
|
98
138
|
})
|
|
139
|
+
this._rootsByKey.set(key, root)
|
|
99
140
|
return true
|
|
100
141
|
}
|
|
101
142
|
return false
|
|
102
143
|
}
|
|
103
144
|
|
|
145
|
+
/** 这个目录是否已在监视(大小写不敏感平台按归一后的路径判)。 */
|
|
146
|
+
hasRoot(root) {
|
|
147
|
+
return this._rootsByKey.has(storeKey(root))
|
|
148
|
+
}
|
|
149
|
+
|
|
104
150
|
/**
|
|
105
151
|
* 每个根**按需**取 store(命中 `storeCache` 即 O(1))。本类不长期持有实例:否则逐出对 watch
|
|
106
152
|
* 根不省内存,真被逐出后 `load()` 还会再建一份,同一 root 出现两份互不可见的状态。内存上界
|
|
@@ -111,12 +157,17 @@ export class WatchManager {
|
|
|
111
157
|
}
|
|
112
158
|
|
|
113
159
|
removeRoot(root) {
|
|
160
|
+
const key = storeKey(root)
|
|
161
|
+
const actual = this._rootsByKey.get(key)
|
|
162
|
+
if (actual !== undefined) {
|
|
163
|
+
this._rootsByKey.delete(key)
|
|
164
|
+
return this.roots.delete(actual)
|
|
165
|
+
}
|
|
166
|
+
// 兜底:roots 可能被外部直接写过(诊断/测试),按原写法再试一次。
|
|
114
167
|
return this.roots.delete(root)
|
|
115
168
|
}
|
|
116
169
|
|
|
117
170
|
start(intervalMs = 30000) {
|
|
118
|
-
// 轮询进行中 `timer` 会短暂为 null;此时再 start 会排第二个定时器。等这一轮结束由 `_tick()` 自己续。
|
|
119
|
-
if (this.timer || this._polling) return
|
|
120
171
|
// 非法值(NaN/undefined)会让定时器退化成 1ms 轮询打满事件循环,故回退默认 30s。
|
|
121
172
|
const raw = Number(intervalMs)
|
|
122
173
|
this._baseInterval = Number.isFinite(raw) ? Math.max(raw, 1000) : 30000
|
|
@@ -127,7 +178,13 @@ export class WatchManager {
|
|
|
127
178
|
// 不是"永不扫描"——外部编辑器改文件、或回合一直不结束(崩溃/长任务)时索引仍会自愈。
|
|
128
179
|
this._activeInterval = Math.max(this._baseInterval * 10, 300000)
|
|
129
180
|
this._interval = this._baseInterval
|
|
181
|
+
// 复位必须在 guard 之前:设置页保存走 stop() → start(),而 stop() 会把 _stopped 置 true。
|
|
182
|
+
// 如果轮询正在进行(_polling=true),这里的提前返回会留下 _stopped=true,那一轮 _tick() 收尾时
|
|
183
|
+
// 看到"已停止"就不再排下一轮——watch 永久停摆,且 applyWatchSettings() 还返回 true。
|
|
184
|
+
// 在飞的一轮结束时 _tick() 会按新的 _baseInterval/_activeInterval 续上。
|
|
130
185
|
this._stopped = false
|
|
186
|
+
// 轮询进行中 `timer` 会短暂为 null;此时再 start 会排第二个定时器。等这一轮结束由 `_tick()` 自己续。
|
|
187
|
+
if (this.timer || this._polling) return
|
|
131
188
|
this._schedule()
|
|
132
189
|
}
|
|
133
190
|
|
|
@@ -154,7 +211,13 @@ export class WatchManager {
|
|
|
154
211
|
if (next) {
|
|
155
212
|
this._interval = this._activeInterval
|
|
156
213
|
this._schedule(this._activeInterval)
|
|
157
|
-
} else
|
|
214
|
+
} else if (!this._polling) {
|
|
215
|
+
// 回合结束:立刻补扫,然后回到常规节拍。
|
|
216
|
+
// 如果已经有一轮 poll 在跑,不要在这里再起一轮:`poll()` 的 reentry guard 会把它当成
|
|
217
|
+
// "本轮无变化"(返回 false),`_tick()` 于是按无变化把间隔翻倍;在飞那轮真正结束时
|
|
218
|
+
// 又因为定时器已存在而降不回 base——回合结束的补扫被推迟到退避间隔之后。
|
|
219
|
+
void this._tick()
|
|
220
|
+
}
|
|
158
221
|
}
|
|
159
222
|
|
|
160
223
|
_schedule(interval = this._interval) {
|
|
@@ -207,6 +270,10 @@ export class WatchManager {
|
|
|
207
270
|
}
|
|
208
271
|
|
|
209
272
|
async pollRoot(root, state) {
|
|
273
|
+
// 根可能在运行期被删除/rename。这里**不能**继续走到 storeFor() → save():
|
|
274
|
+
// save() 会把 !formatWritten 当脏并 mkdirSync,把删掉的根重新创建成一个空目录。
|
|
275
|
+
// 暂时读不到的挂载点同样跳过本轮;它回来后会继续按原逻辑扫描。
|
|
276
|
+
if (!existsSync(root)) return false
|
|
210
277
|
const seen = new Set()
|
|
211
278
|
const updates = []
|
|
212
279
|
// rel → 本轮采集的 mtime:size。提交成功后才落进快照;CAS 失败的条目下轮重试。
|
|
@@ -286,16 +353,27 @@ export class WatchManager {
|
|
|
286
353
|
state.failures.set(rel, err.message)
|
|
287
354
|
console.error(`[dsh-project-memory] re-index failed for ${rel}: ${err.message}`)
|
|
288
355
|
}
|
|
356
|
+
// 日志只打一次是防刷屏;账本每轮都刷新时间,index_repo / memory_stats 才看得见
|
|
357
|
+
// "这个文件现在仍然没进语料",而不是一条陈年记录。
|
|
358
|
+
recordIndexFailure(root, rel, err.message)
|
|
289
359
|
continue
|
|
290
360
|
}
|
|
291
361
|
state.failures.delete(rel)
|
|
292
|
-
|
|
293
|
-
|
|
362
|
+
clearIndexFailure(root, rel)
|
|
363
|
+
// 内容未变也要落快照:否则 mtime:size 快路径永远命中不了,每次 poll 都要重读全仓。
|
|
364
|
+
if (plan.key === UNCHANGED) {
|
|
294
365
|
// 内容没变、只有 mtime 变了:把新 mtime 写回条目,让下一轮(及其它入口)的快路径能命中。
|
|
295
|
-
if (
|
|
366
|
+
if (typeof plan.mtimeMs === 'number') store.refreshMtime(rel, plan.mtimeMs)
|
|
296
367
|
state.snapshot[rel] = sig
|
|
297
368
|
continue
|
|
298
369
|
}
|
|
370
|
+
if (plan.key === OVERSIZE) {
|
|
371
|
+
// 体积超限必须把旧条目摘掉(与 index_repo 同一语义):只跳过快照会让
|
|
372
|
+
// 查询继续命中一个按配置已经不该索引的文件。
|
|
373
|
+
signatures.set(rel, sig)
|
|
374
|
+
updates.push({ rel, drop: true })
|
|
375
|
+
continue
|
|
376
|
+
}
|
|
299
377
|
|
|
300
378
|
signatures.set(rel, sig)
|
|
301
379
|
updates.push(toFileUpdate(rel, plan, record))
|
|
@@ -312,7 +390,11 @@ export class WatchManager {
|
|
|
312
390
|
// 扫全了才剪快照:删掉的文件不能留着,否则它以同样的 mtime:size 回来会被当成未变。
|
|
313
391
|
if (!truncated) {
|
|
314
392
|
for (const rel of Object.keys(state.snapshot)) if (!seen.has(rel)) delete state.snapshot[rel]
|
|
315
|
-
for (const rel of state.failures.keys())
|
|
393
|
+
for (const rel of [...state.failures.keys()]) {
|
|
394
|
+
if (seen.has(rel)) continue
|
|
395
|
+
state.failures.delete(rel)
|
|
396
|
+
clearIndexFailure(root, rel)
|
|
397
|
+
}
|
|
316
398
|
}
|
|
317
399
|
const failed = new Set(stale)
|
|
318
400
|
let applied = 0
|