@yolk_vat-y/dsh-project-memory 0.6.9 → 0.6.11
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 +65 -0
- package/README.md +5 -5
- package/README.zh-CN.md +5 -5
- package/client/client.js +8 -3
- package/client/client.js.map +1 -1
- package/package.json +9 -9
- package/skills/project-memory/SKILL.md +5 -1
- package/skills/project-memory/scripts/pm-digest.mjs +67 -5
- package/src/auto-inject.js +14 -5
- package/src/client/MemoryView.tsx +8 -4
- package/src/commands/insight-actions.js +49 -7
- package/src/commands/tasks.js +1 -1
- package/src/enhancer.js +4 -2
- package/src/insight-store.js +55 -19
- package/src/lazy.js +19 -3
- package/src/ops.js +87 -4
- package/src/recall.js +2 -1
- package/src/setup/taskbridge.js +43 -9
- package/src/store.js +72 -12
- package/src/tools/forget.js +5 -5
- package/src/tools/index-doc.js +2 -2
- package/src/tools/index-repo.js +2 -2
- package/src/tools/lesson-tools.js +28 -25
- package/src/tools/query-memory.js +6 -5
- package/src/tools/remember.js +5 -5
- package/src/tools/stats.js +1 -1
- package/src/tools/task-tools.js +12 -8
- package/src/tools/watch-repo.js +10 -6
- package/src/util/fs.js +64 -8
- package/src/util/patch.js +28 -0
- package/src/util/search.js +28 -14
- package/src/util/task-view.js +3 -1
- package/src/util/text.js +2 -0
- package/src/watch.js +25 -6
package/src/tools/task-tools.js
CHANGED
|
@@ -19,9 +19,9 @@ export function listTasksTool(config) {
|
|
|
19
19
|
return defineTool({
|
|
20
20
|
name: 'list_tasks',
|
|
21
21
|
description:
|
|
22
|
-
'
|
|
22
|
+
'List this project\'s task records (archived included). Look here first in a new or resumed session, then select_task to pick one.',
|
|
23
23
|
parameters: {
|
|
24
|
-
root: { type: 'string', description: '
|
|
24
|
+
root: { type: 'string', description: 'project root (default: cwd)' },
|
|
25
25
|
},
|
|
26
26
|
output: { schema: { type: 'string' }, render: (_a, v) => [{ type: 'text', text: v }] },
|
|
27
27
|
async execute(args, exec) {
|
|
@@ -44,12 +44,12 @@ export function selectTaskTool(config, host) {
|
|
|
44
44
|
return defineTool({
|
|
45
45
|
name: 'select_task',
|
|
46
46
|
description:
|
|
47
|
-
'
|
|
48
|
-
+ '
|
|
47
|
+
'Bind the session to a task (its todo list and files read afterwards sync into it). Exact taskId, or an exact title match'
|
|
48
|
+
+ '(creates one if absent); returns the task card used to rebuild the checklist.',
|
|
49
49
|
parameters: {
|
|
50
|
-
taskId: { type: 'string', description: '
|
|
51
|
-
title: { type: 'string', description: '
|
|
52
|
-
root: { type: 'string', description: '
|
|
50
|
+
taskId: { type: 'string', description: 'exact task id' },
|
|
51
|
+
title: { type: 'string', description: 'exact title; creates if absent' },
|
|
52
|
+
root: { type: 'string', description: 'project root (default: cwd)' },
|
|
53
53
|
},
|
|
54
54
|
output: { schema: { type: 'string' }, render: (_a, v) => [{ type: 'text', text: v }] },
|
|
55
55
|
async execute(args, exec) {
|
|
@@ -170,7 +170,7 @@ export function archiveTaskTool(config, host) {
|
|
|
170
170
|
description: 'Archive a task (restore by selecting it again).',
|
|
171
171
|
parameters: {
|
|
172
172
|
taskId: { type: 'string', required: true, description: 'task id' },
|
|
173
|
-
root: { type: 'string', description: '
|
|
173
|
+
root: { type: 'string', description: 'project root (default: cwd)' },
|
|
174
174
|
},
|
|
175
175
|
output: { schema: { type: 'string' }, render: (_a, v) => [{ type: 'text', text: v }] },
|
|
176
176
|
async execute(args, exec) {
|
|
@@ -182,6 +182,10 @@ export function archiveTaskTool(config, host) {
|
|
|
182
182
|
if (task.archived) return truncate(JSON.stringify({ success: false, error: 'Already archived' }), config.maxOutputChars)
|
|
183
183
|
task.archived = true
|
|
184
184
|
task.updatedAt = new Date().toISOString()
|
|
185
|
+
// 归档 = 停止续做:绑定必须在同一事务里清掉,否则本会话(以及别的会话)下一次
|
|
186
|
+
// 注入仍会拿到这张归档任务卡,task 级 insights 也继续可见;下一次 load 的
|
|
187
|
+
// _pruneBindings() 会自愈,但用户已经在中间几步看到了错误状态。
|
|
188
|
+
store.removeBindingsForTask(task.id)
|
|
185
189
|
store.markTasksDirty()
|
|
186
190
|
store.save()
|
|
187
191
|
// 反思钩子:归档即收割最终教训(默认关)
|
package/src/tools/watch-repo.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
2
3
|
import path from 'node:path'
|
|
3
4
|
import { assertIndexRoot, assertSafeRoot, memoryRootFor, resolveIndexRoot, rootRejection, sessionMemoryRootOrNull, storeKey } from '../util/fs.js'
|
|
4
5
|
import { ProjectMemoryStore } from '../store.js'
|
|
5
6
|
import { cfgValue } from '../util/config.js'
|
|
7
|
+
import { normalizeWatchIntervalMs } from '../watch.js'
|
|
6
8
|
|
|
7
9
|
export function watchRepoTool(watchManager, config) {
|
|
8
10
|
return defineTool({
|
|
@@ -13,11 +15,11 @@ export function watchRepoTool(watchManager, config) {
|
|
|
13
15
|
root: {
|
|
14
16
|
type: 'string',
|
|
15
17
|
required: true,
|
|
16
|
-
description: '
|
|
18
|
+
description: 'project root (relative: session cwd)',
|
|
17
19
|
},
|
|
18
20
|
watch: {
|
|
19
21
|
type: 'boolean',
|
|
20
|
-
description: 'false =
|
|
22
|
+
description: 'false = stop watching',
|
|
21
23
|
},
|
|
22
24
|
},
|
|
23
25
|
output: {
|
|
@@ -60,7 +62,9 @@ export function watchRepoTool(watchManager, config) {
|
|
|
60
62
|
// (这正是要修的场景),而写它会在家目录里造出 .dsh-project-memory。那份
|
|
61
63
|
// watch.json 现在也没有任何启动路径会读取(restorePersisted 只认会话 cwd),
|
|
62
64
|
// 留着即可;会话侧的镜像条目照常摘掉。
|
|
63
|
-
|
|
65
|
+
// 根可能早已被删除:这时**不能** load/commit,否则 save() 的 mkdirSync 会把
|
|
66
|
+
// watch_repo 刚停掉的路径重新创建出来。watch.json 的清理只对仍存在的根有意义。
|
|
67
|
+
if (existsSync(root) && !rootRejection(root, config)) {
|
|
64
68
|
new ProjectMemoryStore(memoryDir).load().commit((s) => s.removeWatch(root))
|
|
65
69
|
}
|
|
66
70
|
await mirrorSessionWatchlist(false)
|
|
@@ -69,9 +73,9 @@ export function watchRepoTool(watchManager, config) {
|
|
|
69
73
|
new ProjectMemoryStore(memoryDir).load().commit((s) => s.addWatch(root))
|
|
70
74
|
await mirrorSessionWatchlist(true)
|
|
71
75
|
watchManager.addRoot(root)
|
|
72
|
-
const
|
|
73
|
-
watchManager.start(
|
|
74
|
-
return `Watching ${root} (interval ${
|
|
76
|
+
const intervalMs = normalizeWatchIntervalMs(Number(cfgValue(config, 'watchInterval')) * 1000)
|
|
77
|
+
watchManager.start(intervalMs)
|
|
78
|
+
return `Watching ${root} (interval ${intervalMs / 1000}s). Docs/code changes will be re-indexed silently.`
|
|
75
79
|
},
|
|
76
80
|
})
|
|
77
81
|
}
|
package/src/util/fs.js
CHANGED
|
@@ -254,11 +254,15 @@ export function acquireDirLock(dir, { timeoutMs = 2000, staleMs = 10000, hardSta
|
|
|
254
254
|
}
|
|
255
255
|
if (lockIsStale(state, { staleMs, hardLimit, maxHoldMs })) {
|
|
256
256
|
// 只删"刚刚读到的那个内容":两次读之间锁可能已被别人抢到并重写。
|
|
257
|
+
// 回收失败(只读目录、Windows 杀毒/索引器长期占用)不能无条件 continue:那会变成
|
|
258
|
+
// 无 deadline、无等待的同步自旋,timeoutMs 被完全忽略,save()/commit()/退出 flush 卡死。
|
|
259
|
+
if (Date.now() >= deadline) return null
|
|
257
260
|
try {
|
|
258
261
|
if (readFileSync(file, 'utf8') === state.text) unlinkWithRetry(file)
|
|
259
262
|
} catch {
|
|
260
263
|
// 已被别处回收,或杀毒瞬时占用;下一轮继续判定
|
|
261
264
|
}
|
|
265
|
+
Atomics.wait(sleeper, 0, 0, 10)
|
|
262
266
|
continue
|
|
263
267
|
}
|
|
264
268
|
if (Date.now() >= deadline) return null
|
|
@@ -610,12 +614,16 @@ export function listProjectFiles(root, opts = {}) {
|
|
|
610
614
|
// 配置/状态/密钥(`.env`、`.ssh`),非 git 目录没有"被跟踪=作者同意"这层信号。
|
|
611
615
|
// 真要索引某个隐藏文件,走显式 `index_doc`(它不受扫描清单限制)。
|
|
612
616
|
if (segs.some((s) => s.startsWith('.'))) continue
|
|
613
|
-
|
|
617
|
+
// 与 walkDir 同口径:`DEFAULT_IGNORE` 只作用于**目录段**,根目录下一个叫 `build`
|
|
618
|
+
// 的普通文件仍然要收(此前对文件名也判,`git init` 前后语料会变)。
|
|
619
|
+
if (segs.slice(0, -1).some((s) => DEFAULT_IGNORE.has(s))) continue
|
|
614
620
|
const abs = path.join(root, rel)
|
|
615
621
|
// 与 walkDir 同一判据:exclude 是子树级前缀。必须在 maxDepth 之前——被排除文件不该把
|
|
616
622
|
// 扫描标成 truncated(否则本轮不清理"未见到的旧条目")。
|
|
617
623
|
if (policy && excludedEntryForAbs(abs, policy)) continue
|
|
618
|
-
|
|
624
|
+
// 文件的“目录深度”是 segs.length - 1;walkDir 的 maxDepth 也按目录层数计,
|
|
625
|
+
// 此前用 segs.length 会让 git 分支少收浅一层,并在同一棵树上报不同的 capped。
|
|
626
|
+
if (segs.length - 1 > maxDepth) {
|
|
619
627
|
capped = true
|
|
620
628
|
continue
|
|
621
629
|
}
|
|
@@ -742,6 +750,13 @@ export function normalizeCitationFiles(files, root) {
|
|
|
742
750
|
const value = raw.trim().replace(/\\/g, '/')
|
|
743
751
|
if (!value) continue
|
|
744
752
|
const looksAbsolute = value.startsWith('/') || /^[A-Za-z]:\//.test(value)
|
|
753
|
+
// Windows 盘符路径在 POSIX 上 `path.isAbsolute()` 是 false:不能把它当项目内相对路径
|
|
754
|
+
// 落成 `C:/...`(那会生成一个永远不存在的引用)。真正的 POSIX 绝对路径仍走下面的
|
|
755
|
+
// base/relative 判定,项目外的照常丢弃。
|
|
756
|
+
if (looksAbsolute && !path.isAbsolute(value)) {
|
|
757
|
+
dropped.push(raw)
|
|
758
|
+
continue
|
|
759
|
+
}
|
|
745
760
|
if (base === null && looksAbsolute) {
|
|
746
761
|
dropped.push(raw)
|
|
747
762
|
continue
|
|
@@ -771,16 +786,57 @@ export function storeKey(rel, platform = process.platform) {
|
|
|
771
786
|
return platform === 'win32' || platform === 'darwin' ? rel.toLowerCase() : rel
|
|
772
787
|
}
|
|
773
788
|
|
|
774
|
-
export function memoryRootFor(indexRoot, memoryDir) {
|
|
775
|
-
return path.join(indexRoot, memoryDir)
|
|
776
|
-
}
|
|
777
|
-
|
|
778
789
|
/** store 目录名(配置项 `memoryDir`;调用方可能来自工具参数,缺省时兜底成默认值)。 */
|
|
779
790
|
export const DEFAULT_MEMORY_DIR = '.dsh-project-memory'
|
|
780
791
|
|
|
792
|
+
/** 已经告过警的 `memoryDir` 值:同一个坏值在进程里只吵一次。 */
|
|
793
|
+
const warnedMemoryDir = new Set()
|
|
794
|
+
|
|
795
|
+
function defaultMemoryDirWarning(value) {
|
|
796
|
+
if (warnedMemoryDir.has(value)) return
|
|
797
|
+
warnedMemoryDir.add(value)
|
|
798
|
+
console.warn(
|
|
799
|
+
`[dsh-project-memory] memoryDir ${JSON.stringify(value)} must stay inside the project root; `
|
|
800
|
+
+ `using ${DEFAULT_MEMORY_DIR} instead.`,
|
|
801
|
+
)
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* `memoryDir` → 真正使用的目录名(读写两侧唯一的归一入口)。
|
|
806
|
+
*
|
|
807
|
+
* 合法值 = **项目根内的一段相对路径**。会让 store 落到根外、或落到根上(`save()` 的自我忽略
|
|
808
|
+
* 随后把整个仓库从 git 视角隐藏)的形态一律回落到默认目录并告警一次:空/空白、`.`、`..`、
|
|
809
|
+
* 含 `..` 段(`a/../b`)、绝对路径(POSIX `/x`、Windows `C:\\x`)。嵌套目录(`a/b`)仍允许——
|
|
810
|
+
* 它仍在根内。空串的兜底在这里收口,`memoryRootFor()` 与 `memoryDirName()` 不再各写一遍。
|
|
811
|
+
*/
|
|
812
|
+
export function safeMemoryDirName(raw, warn = defaultMemoryDirWarning) {
|
|
813
|
+
const value = typeof raw === 'string' ? raw.trim() : ''
|
|
814
|
+
if (!value) return DEFAULT_MEMORY_DIR
|
|
815
|
+
if (
|
|
816
|
+
value === '.' || value === '..'
|
|
817
|
+
|| path.isAbsolute(value)
|
|
818
|
+
|| WINDOWS_ABSOLUTE_PATH.test(value)
|
|
819
|
+
|| value.split(/[\\/]+/).includes('..')
|
|
820
|
+
) {
|
|
821
|
+
warn(value)
|
|
822
|
+
return DEFAULT_MEMORY_DIR
|
|
823
|
+
}
|
|
824
|
+
return value
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* 项目根 → store 目录。
|
|
829
|
+
*
|
|
830
|
+
* 空串/空白必须回落到默认目录:`path.join(root, '')` 等于项目根本身,而 `save()` 的自我忽略
|
|
831
|
+
* 会在那里写一个内容为 `*` 的 `.gitignore`(把整个仓库从 git 的视角隐藏),`format.json` /
|
|
832
|
+
* `shards/` 也会污染源码树。`.`, `..` 与绝对路径同理。
|
|
833
|
+
*/
|
|
834
|
+
export function memoryRootFor(indexRoot, memoryDir) {
|
|
835
|
+
return path.join(indexRoot, safeMemoryDirName(memoryDir))
|
|
836
|
+
}
|
|
837
|
+
|
|
781
838
|
export function memoryDirName(config) {
|
|
782
|
-
|
|
783
|
-
return typeof raw === 'string' && raw.trim() ? raw : DEFAULT_MEMORY_DIR
|
|
839
|
+
return safeMemoryDirName(config?.memoryDir)
|
|
784
840
|
}
|
|
785
841
|
|
|
786
842
|
/**
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codex `apply_patch` 正文里的目标路径。
|
|
3
|
+
*
|
|
4
|
+
* 这个工具没有 `file_path` / `path` 参数,路径只出现在补丁正文的文件头里:
|
|
5
|
+
* *** Add File: src/a.ts / *** Update File: src/b.ts / *** Delete File: src/c.ts
|
|
6
|
+
* *** Move to: src/d.ts(跟在 Update File 之后,是移动后的新路径)
|
|
7
|
+
* 正文里的增删行都带 `+` / `-` / 空格前缀,所以行首的 `*** …` 只可能是文件头。
|
|
8
|
+
*
|
|
9
|
+
* 只做机械提取:不校验补丁语法、不判断文件是否存在——调用方拿到的路径仍要走
|
|
10
|
+
* 自己的根归一化(项目外会被丢弃)。
|
|
11
|
+
*
|
|
12
|
+
* @param {string} input - `apply_patch` 的 `input` 参数(整段补丁文本)
|
|
13
|
+
* @returns {string[]} 按出现顺序去重后的路径(原样,未解析为绝对路径)
|
|
14
|
+
*/
|
|
15
|
+
export function patchFilePaths(input) {
|
|
16
|
+
if (typeof input !== 'string' || input === '') return []
|
|
17
|
+
const paths = []
|
|
18
|
+
const seen = new Set()
|
|
19
|
+
for (const line of input.split(/\r?\n/)) {
|
|
20
|
+
const matched = /^\*\*\* (?:Add|Update|Delete) File:\s*(.+)$/.exec(line) || /^\*\*\* Move to:\s*(.+)$/.exec(line)
|
|
21
|
+
if (!matched) continue
|
|
22
|
+
const raw = matched[1].trim().replace(/^(['"`])(.*)\1$/, '$2').trim()
|
|
23
|
+
if (!raw || seen.has(raw)) continue
|
|
24
|
+
seen.add(raw)
|
|
25
|
+
paths.push(raw)
|
|
26
|
+
}
|
|
27
|
+
return paths
|
|
28
|
+
}
|
package/src/util/search.js
CHANGED
|
@@ -3,6 +3,18 @@ import { CYRILLIC_WORD_RE, RU_STOPWORDS, normalizeRuWord, stemRu } from './ru.js
|
|
|
3
3
|
export const CJK_RANGE =
|
|
4
4
|
/[\u3400-\u9fff\uf900-\ufaff\u3040-\u309f\u30a0-\u30ff\uac00-\ud7af]/
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* 一段连续的 CJK 字符。分词时用它一次性取出所有段,取代"逐字符 test 再累积"的状态机。
|
|
8
|
+
*
|
|
9
|
+
* 从 `CJK_RANGE` 派生而不是另抄一份字符类:「什么算 CJK」全仓库只该有一个定义,
|
|
10
|
+
* 改字符范围时不会有两处不同步。`u` 让字符类按码点匹配——当前字符集全在 BMP,
|
|
11
|
+
* 输出与逐字符判断一致;将来若要纳入增补平面(U+20000 起),段内的二元组切分
|
|
12
|
+
* 必须一并改成按码点,`test/tokenizer-equivalence.test.mjs` 里有一条哨兵用例会在那时失败。
|
|
13
|
+
*
|
|
14
|
+
* 输出与逐字符实现逐字节一致,由同一条差分测试守住。
|
|
15
|
+
*/
|
|
16
|
+
const CJK_RUN_RE = new RegExp(`${CJK_RANGE.source}+`, 'gu')
|
|
17
|
+
|
|
6
18
|
/**
|
|
7
19
|
* 分词契约的版本号。改 tokenizeRaw 的**输出集合**(新增书写系统、词干、停用词)就要 +1:
|
|
8
20
|
* 落盘的 doc `terms` 是它的产物,旧版本写的 terms 里没有新词项,必须让
|
|
@@ -64,15 +76,6 @@ export function tokenizeRaw(text, { stem = true } = {}) {
|
|
|
64
76
|
if (!text) return []
|
|
65
77
|
const lower = text.toLowerCase()
|
|
66
78
|
const tokens = []
|
|
67
|
-
let cjkRun = ''
|
|
68
|
-
const flushCjk = () => {
|
|
69
|
-
if (cjkRun.length === 1) {
|
|
70
|
-
tokens.push(cjkRun)
|
|
71
|
-
} else {
|
|
72
|
-
for (let i = 0; i < cjkRun.length - 1; i++) tokens.push(cjkRun.slice(i, i + 2))
|
|
73
|
-
}
|
|
74
|
-
cjkRun = ''
|
|
75
|
-
}
|
|
76
79
|
const latin = lower.match(/[a-z0-9_]+/g) || []
|
|
77
80
|
for (const tok of latin) tokens.push(tok)
|
|
78
81
|
// 西里尔(只做了俄语):切词 → 去虚词 → Snowball 词干。
|
|
@@ -85,11 +88,12 @@ export function tokenizeRaw(text, { stem = true } = {}) {
|
|
|
85
88
|
const token = word.length <= 3 || !stem ? word : stemRu(word)
|
|
86
89
|
if (token.length >= 2) tokens.push(token)
|
|
87
90
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
+
// CJK:按「连续段」结算 —— 段长 1 产单字,段长 ≥2 产全部相邻二元组。
|
|
92
|
+
// 旧实现是逐字符累积 + 遇非 CJK 结算,两者输出逐字节一致(差分测试见上)。
|
|
93
|
+
for (const run of lower.match(CJK_RUN_RE) || []) {
|
|
94
|
+
if (run.length === 1) tokens.push(run)
|
|
95
|
+
else for (let i = 0; i < run.length - 1; i++) tokens.push(run.slice(i, i + 2))
|
|
91
96
|
}
|
|
92
|
-
flushCjk()
|
|
93
97
|
return tokens
|
|
94
98
|
}
|
|
95
99
|
|
|
@@ -211,12 +215,22 @@ export function rankEntriesMergedScored(entries, queries, limit = 8) {
|
|
|
211
215
|
// (没有 problem/solution 参数),独立打分器会把那种条目打成 0 分且无处可查。现由
|
|
212
216
|
// `recall.js` 用与洞察层相同的 `insightToEntry` 适配器打分,本函数随能力一起删除。
|
|
213
217
|
|
|
218
|
+
const LATIN_TERM_RE = /^[a-z0-9_]+$/
|
|
219
|
+
const WORD_CHAR_RE = /[a-z0-9_]/
|
|
220
|
+
|
|
214
221
|
function countOccurrences(text, token) {
|
|
215
222
|
if (!token) return 0
|
|
216
223
|
let count = 0
|
|
217
224
|
let pos = 0
|
|
225
|
+
// 拉丁词项必须按词边界计数:`searchText` 不是 token 化的,用裸 indexOf 会让 `cat` 命中
|
|
226
|
+
// "concatenate"、`log` 命中 "dialog",与按 token 统计的 IDF 不同源(假阳性 + 分数不可比)。
|
|
227
|
+
// CJK bigram / 西里尔词干没有 ASCII 词边界,保持子串计数。
|
|
228
|
+
const bounded = LATIN_TERM_RE.test(token)
|
|
218
229
|
while ((pos = text.indexOf(token, pos)) !== -1) {
|
|
219
|
-
|
|
230
|
+
if (!bounded
|
|
231
|
+
|| (!WORD_CHAR_RE.test(text[pos - 1] || '') && !WORD_CHAR_RE.test(text[pos + token.length] || ''))) {
|
|
232
|
+
count++
|
|
233
|
+
}
|
|
220
234
|
pos += token.length
|
|
221
235
|
}
|
|
222
236
|
return count
|
package/src/util/task-view.js
CHANGED
|
@@ -51,7 +51,9 @@ export function stepProgress(task) {
|
|
|
51
51
|
|
|
52
52
|
/** 相对时间(中文):面板、list_tasks、/tasks 三处文案共用同一套阈值。 */
|
|
53
53
|
export function timeAgo(iso) {
|
|
54
|
-
const
|
|
54
|
+
const t = Date.parse(iso ?? '')
|
|
55
|
+
if (!Number.isFinite(t)) return '未知'
|
|
56
|
+
const diff = Date.now() - t
|
|
55
57
|
const m = Math.floor(diff / 60000)
|
|
56
58
|
if (m < 1) return '刚刚'
|
|
57
59
|
if (m < 60) return `${m}分钟前`
|
package/src/util/text.js
CHANGED
|
@@ -14,6 +14,8 @@ export function textOf(message) {
|
|
|
14
14
|
|
|
15
15
|
export function truncate(text, maxChars) {
|
|
16
16
|
if (typeof text !== 'string') return String(text)
|
|
17
|
+
// 非有限值(缺配置 / 测试桩)原样返回,而不是输出 “...[truncated at undefined chars]”。
|
|
18
|
+
if (!Number.isFinite(maxChars)) return text
|
|
17
19
|
if (text.length <= maxChars) return text
|
|
18
20
|
return text.slice(0, maxChars) + `\n\n...[truncated at ${maxChars} chars]`
|
|
19
21
|
}
|
package/src/watch.js
CHANGED
|
@@ -22,10 +22,19 @@ import { clearIndexFailure, recordIndexFailure } from './index-failures.js'
|
|
|
22
22
|
export function applyWatchSettings(watchManager, config) {
|
|
23
23
|
watchManager.stop()
|
|
24
24
|
if (cfgValue(config, 'watch') === false) return false
|
|
25
|
-
watchManager.start(Number(cfgValue(config, 'watchInterval')) * 1000)
|
|
25
|
+
watchManager.start(normalizeWatchIntervalMs(Number(cfgValue(config, 'watchInterval')) * 1000))
|
|
26
26
|
return true
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* 把配置里的毫秒值归一成**实际会用的**轮询间隔:非法值回落 30s,合法值下限 1s。
|
|
31
|
+
* 工具回报与 `start()` 必须读同一份,否则 `watchInterval: 0` 会回报 "0s"、实际 1s。
|
|
32
|
+
*/
|
|
33
|
+
export function normalizeWatchIntervalMs(intervalMs) {
|
|
34
|
+
const raw = Number(intervalMs)
|
|
35
|
+
return Number.isFinite(raw) ? Math.max(raw, 1000) : 30000
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
/**
|
|
30
39
|
* 有界并发 map。同步 `statSync` 循环在 Windows(尤其 WSL 挂 `/mnt/c`)会把事件循环卡住:
|
|
31
40
|
* 400 文件实测 ~0.9s、12k 文件按同一比例是几十秒,期间面板与工具调用全部停摆。
|
|
@@ -169,8 +178,7 @@ export class WatchManager {
|
|
|
169
178
|
|
|
170
179
|
start(intervalMs = 30000) {
|
|
171
180
|
// 非法值(NaN/undefined)会让定时器退化成 1ms 轮询打满事件循环,故回退默认 30s。
|
|
172
|
-
|
|
173
|
-
this._baseInterval = Number.isFinite(raw) ? Math.max(raw, 1000) : 30000
|
|
181
|
+
this._baseInterval = normalizeWatchIntervalMs(intervalMs)
|
|
174
182
|
// 空闲退避:连续没有变化的轮次把间隔翻倍,最长 2 分钟一轮;任何变化立刻回到 base。
|
|
175
183
|
// 轮询是 O(树) 的 walkDir + 逐文件 stat(本仓库一轮实测 58–87ms),常驻 15s 一轮都在磨 I/O。
|
|
176
184
|
this._maxInterval = Math.max(this._baseInterval, Math.min(this._baseInterval * 8, 120000))
|
|
@@ -270,6 +278,10 @@ export class WatchManager {
|
|
|
270
278
|
}
|
|
271
279
|
|
|
272
280
|
async pollRoot(root, state) {
|
|
281
|
+
// 根可能在运行期被删除/rename。这里**不能**继续走到 storeFor() → save():
|
|
282
|
+
// save() 会把 !formatWritten 当脏并 mkdirSync,把删掉的根重新创建成一个空目录。
|
|
283
|
+
// 暂时读不到的挂载点同样跳过本轮;它回来后会继续按原逻辑扫描。
|
|
284
|
+
if (!existsSync(root)) return false
|
|
273
285
|
const seen = new Set()
|
|
274
286
|
const updates = []
|
|
275
287
|
// rel → 本轮采集的 mtime:size。提交成功后才落进快照;CAS 失败的条目下轮重试。
|
|
@@ -356,13 +368,20 @@ export class WatchManager {
|
|
|
356
368
|
}
|
|
357
369
|
state.failures.delete(rel)
|
|
358
370
|
clearIndexFailure(root, rel)
|
|
359
|
-
//
|
|
360
|
-
if (plan.key === UNCHANGED
|
|
371
|
+
// 内容未变也要落快照:否则 mtime:size 快路径永远命中不了,每次 poll 都要重读全仓。
|
|
372
|
+
if (plan.key === UNCHANGED) {
|
|
361
373
|
// 内容没变、只有 mtime 变了:把新 mtime 写回条目,让下一轮(及其它入口)的快路径能命中。
|
|
362
|
-
if (
|
|
374
|
+
if (typeof plan.mtimeMs === 'number') store.refreshMtime(rel, plan.mtimeMs)
|
|
363
375
|
state.snapshot[rel] = sig
|
|
364
376
|
continue
|
|
365
377
|
}
|
|
378
|
+
if (plan.key === OVERSIZE) {
|
|
379
|
+
// 体积超限必须把旧条目摘掉(与 index_repo 同一语义):只跳过快照会让
|
|
380
|
+
// 查询继续命中一个按配置已经不该索引的文件。
|
|
381
|
+
signatures.set(rel, sig)
|
|
382
|
+
updates.push({ rel, drop: true })
|
|
383
|
+
continue
|
|
384
|
+
}
|
|
366
385
|
|
|
367
386
|
signatures.set(rel, sig)
|
|
368
387
|
updates.push(toFileUpdate(rel, plan, record))
|