@yolk_vat-y/dsh-project-memory 0.6.5 → 0.6.7
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 +194 -0
- package/README.md +24 -21
- package/README.zh-CN.md +25 -23
- package/package.json +6 -4
- package/scripts/bench-edits.mjs +87 -0
- package/skills/project-memory/SKILL.md +72 -0
- package/skills/project-memory/scripts/pm-digest.mjs +138 -0
- package/src/audit.js +35 -62
- package/src/auto-inject.js +171 -220
- package/src/chunker.js +1 -1
- package/src/client/paging.js +22 -50
- package/src/client/slash.ts +12 -48
- package/src/doc-index.js +7 -5
- package/src/doc-pipeline.js +2 -0
- package/src/enhancer.js +45 -92
- package/src/index-pipeline.js +14 -10
- package/src/index.js +51 -73
- package/src/insight-store.js +132 -129
- package/src/ops.js +290 -54
- package/src/project-profile.js +20 -15
- package/src/readiness.js +153 -106
- package/src/recall.js +55 -134
- package/src/skill.js +86 -0
- package/src/store.js +311 -214
- package/src/tools/index-doc.js +5 -1
- package/src/tools/index-repo.js +2 -1
- package/src/tools/lesson-tools.js +2 -2
- package/src/util/flush.js +62 -0
- package/src/util/fs.js +346 -191
- package/src/util/path-token.js +8 -3
- package/src/util/persist.js +27 -0
- package/src/util/ru.js +202 -0
- package/src/util/search.js +33 -1
- package/src/vector-index.js +104 -104
- package/src/watch.js +74 -61
package/src/readiness.js
CHANGED
|
@@ -1,24 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 就绪引擎(push
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* - **authored trigger = 确定性注入**:作者写下的 keywords / symbols / actions / paths 命中即在
|
|
7
|
-
* 动手前全文注入,优先级 1。可审计(返回 `why`),不参与任何相似度打分。
|
|
8
|
-
* - **统计信号 = 提示态**:没有 trigger 的条目只能作为截断提示进入,优先级 2,且判据是
|
|
9
|
-
* **层内相对阈值**(相对最高分),不是长度敏感的魔法常量。
|
|
10
|
-
*
|
|
11
|
-
* 两个窗口(忠于宿主契约:DSH 没有"工具执行前拦截"钩子,唯一能加上下文的缝是 agent/pre-step):
|
|
12
|
-
* - 先发窗口:人类消息里的意图词("提交/发布/公开")→ 动作 id 在模型规划动作之前就命中;
|
|
13
|
-
* - 反应窗口:已观察到的 tool/call 参数(`git commit`、`npm publish`、改动的路径)走同一个匹配器。
|
|
14
|
-
*
|
|
15
|
-
* 纯函数、零依赖、不调用模型。
|
|
2
|
+
* 就绪引擎(push 侧):recall 回答"记忆里有什么相关",readiness 回答"动手之前必须知道什么"。
|
|
3
|
+
* 交付契约:authored trigger = 确定性全文注入(优先级 1,可审计 `why`);统计信号 = 截断提示
|
|
4
|
+
* (优先级 2,层内相对阈值)。两个窗口靠 agent/pre-step(DSH 没有工具执行前钩子):人类意图词先发,
|
|
5
|
+
* 已观察到的 tool/call 走同一匹配器。纯函数、零依赖、不调用模型。
|
|
16
6
|
* @module dsh-project-memory/readiness
|
|
17
7
|
*/
|
|
18
8
|
|
|
19
9
|
import { FALLBACK_OPS, WEAK_OPS, activityFromText, opForLegacyAction } from './ops.js'
|
|
20
10
|
import { extractPathTokens } from './util/path-token.js'
|
|
21
11
|
import { CJK_RANGE, tokenize } from './util/search.js'
|
|
12
|
+
import { CYRILLIC_RANGE, CYRILLIC_WORD_RE, RU_STOPWORDS, normalizeRuWord, stemRu } from './util/ru.js'
|
|
22
13
|
|
|
23
14
|
/**
|
|
24
15
|
* 动作词典:动作 id → 匹配模式。命令形态与人类意图词都归一成同一套 id,
|
|
@@ -29,13 +20,23 @@ export const ACTION_LEXICON = [
|
|
|
29
20
|
['git-commit', [/git\s+commit\b/i, /提交/]],
|
|
30
21
|
['git-push', [/git\s+push\b/i, /推送/]],
|
|
31
22
|
['npm-publish', [/npm\s+publish\b/i, /发包/, /发布\s*到\s*npm/i]],
|
|
32
|
-
|
|
23
|
+
// 英文 `release` 只在带版本号时才算动作:`release notes` 是文档任务,不是发版。
|
|
24
|
+
['release', [/git\s+tag\b/i, /发版/, /发布版本/, /\brelease\s+v?\d/i]],
|
|
33
25
|
['go-public', [/公开/, /开源/, /对外/, /上线/]],
|
|
34
26
|
['delete', [/rm\s+-rf\b/, /git\s+rm\b/, /删除/, /清理/]],
|
|
35
27
|
['migrate', [/migrate\b/i, /迁移/]],
|
|
36
28
|
['deploy', [/\bdeploy\b/i, /部署/]],
|
|
37
29
|
]
|
|
38
30
|
|
|
31
|
+
/**
|
|
32
|
+
* 允许**人类消息里的动作词**触发全文注入的 op 白名单。
|
|
33
|
+
* 判据是"这条记忆必须在动作**之前**到达,晚了就没用,且动作不可逆":提交 / 发 npm 包 / 发版 /
|
|
34
|
+
* 推远端 / 部署 / 公开仓库。
|
|
35
|
+
* 反过来,`删除 / 清理 / 渲染 / 基准 / 取网页 / 查询记忆` 在人类消息里经常只是**提及**,用它们做
|
|
36
|
+
* 全文注入实测代价是 1200 字符一条的无关 procedure;这些动作仍可由**已观察到的工具调用**触发。
|
|
37
|
+
*/
|
|
38
|
+
export const PRE_ACTION_OPS = new Set(['npm-publish', 'release', 'git-commit', 'git-push', 'deploy', 'go-public'])
|
|
39
|
+
|
|
39
40
|
/**
|
|
40
41
|
* 文本 → 动作 id 列表(确定性、去重、词典序稳定)。
|
|
41
42
|
* @param {string} text - 人类消息或工具调用参数。
|
|
@@ -53,8 +54,7 @@ export function detectActions(text) {
|
|
|
53
54
|
|
|
54
55
|
/**
|
|
55
56
|
* 文本 → 路径 token(用于 trigger 的 `writes` / `guard.paths` 匹配)。
|
|
56
|
-
* 规则收在 `util/path-token.js
|
|
57
|
-
* (原先这里与 ops.js 各抄一份,ops 那份漏了点开头文件名且已漂移)。
|
|
57
|
+
* 规则收在 `util/path-token.js`,与 ops 抽写目标是**同一份实现**。
|
|
58
58
|
* @param {string} text - 工具调用参数或人类消息。
|
|
59
59
|
* @returns {string[]} 去重后的路径 token。
|
|
60
60
|
*/
|
|
@@ -64,17 +64,21 @@ export function extractPaths(text) {
|
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
66
|
* 组装就绪上下文:人类意图 + 已观察动作。三处来源都做动作/路径归一,调用方只需给文本。
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
69
|
-
* @param {
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @returns {{ humanText: string, actionText: string, actions: string[], paths: string[] }}
|
|
67
|
+
* **证据来源决定准入**:observed(结构化工具调用)可以触发全文注入;人类消息里的动作词只允许
|
|
68
|
+
* 4 个"不可逆外部动作"触发全文(见 {@link PRE_ACTION_OPS}),其余只进提示通道。
|
|
69
|
+
* @param {object} input `humanText` 本轮人类消息;`actionText` 旧调用方的工具调用文本(测试/老宿主,
|
|
70
|
+
* 生产路径不再传);`ops`/`targets` 结构化工具调用归一出的动作与写目标(可信);
|
|
71
|
+
* `actions`/`paths` 调用方已归一化的动作 id 与路径(可选,会被并集)。
|
|
72
|
+
* @returns {{ humanText: string, actionText: string, actions: string[], paths: string[], humanPaths: string[], ops: string[], opsObserved: string[], intentOps: string[], targets: string[], hosts: string[], intent: string, tags: string[] }}
|
|
73
73
|
*/
|
|
74
74
|
export function buildReadinessContext(input = {}) {
|
|
75
75
|
const humanText = String(input.humanText ?? input.query ?? '')
|
|
76
76
|
const actionText = String(input.actionText || '')
|
|
77
|
-
|
|
77
|
+
// `input.actions` 是调用方**已归一化**的动作(测试/老宿主),当作已观察证据;
|
|
78
|
+
// 只有从 humanText 里现猜出来的词才受 PRE_ACTION_OPS 约束。
|
|
79
|
+
const declaredActions = new Set(input.actions || [])
|
|
80
|
+
const proseActions = detectActions(humanText)
|
|
81
|
+
const actions = new Set([...declaredActions, ...proseActions])
|
|
78
82
|
// 人类消息里提到的路径 = **动手前的写意图**("帮我改 src/x.js");动作文本里的路径不区分读写,
|
|
79
83
|
// 读一个文件不该算写目标(否则 `read README.md` 会命中 writes:['README.md'])。
|
|
80
84
|
// 两者对外仍合并成 `paths`(兼容既有语义),写通道单看 humanPaths。
|
|
@@ -87,11 +91,20 @@ export function buildReadinessContext(input = {}) {
|
|
|
87
91
|
const paths = new Set([...humanPaths, ...extractPaths(actionText)])
|
|
88
92
|
// 动作平面(S1):结构化调用优先;没有结构化调用时对 actionText 跑一遍 shell 规则兜底。
|
|
89
93
|
const fromText = activityFromText(actionText)
|
|
90
|
-
const
|
|
91
|
-
for (const a of
|
|
94
|
+
const opsObserved = new Set([...(input.ops || []), ...fromText.ops])
|
|
95
|
+
for (const a of declaredActions) {
|
|
96
|
+
const op = opForLegacyAction(a)
|
|
97
|
+
if (op) opsObserved.add(op)
|
|
98
|
+
}
|
|
99
|
+
// 人类消息里的动作词:`删除/清理/渲染/基准/取网页` 在散文里太容易是**提及**
|
|
100
|
+
// (实测 "删除后的悬空状态"、"清理一下 README 的措辞" 都命中过 file-delete,
|
|
101
|
+
// 后者注入了一条 1200 字符的恢复 procedure)。只有没法等的不可逆外部动作才允许先发。
|
|
102
|
+
const intentOps = new Set()
|
|
103
|
+
for (const a of proseActions) {
|
|
92
104
|
const op = opForLegacyAction(a)
|
|
93
|
-
if (op)
|
|
105
|
+
if (op) intentOps.add(op)
|
|
94
106
|
}
|
|
107
|
+
const ops = new Set([...opsObserved, ...[...intentOps].filter((op) => PRE_ACTION_OPS.has(op))])
|
|
95
108
|
const targets = new Set([...(input.targets || []), ...fromText.targets])
|
|
96
109
|
const hosts = new Set([...(input.hosts || []), ...fromText.hosts])
|
|
97
110
|
return {
|
|
@@ -101,6 +114,8 @@ export function buildReadinessContext(input = {}) {
|
|
|
101
114
|
paths: [...paths],
|
|
102
115
|
humanPaths: [...humanPaths],
|
|
103
116
|
ops: [...ops],
|
|
117
|
+
opsObserved: [...opsObserved],
|
|
118
|
+
intentOps: [...intentOps],
|
|
104
119
|
targets: [...targets],
|
|
105
120
|
hosts: [...hosts],
|
|
106
121
|
intent: intentText(humanText),
|
|
@@ -126,7 +141,7 @@ const DERIVED_MAX_PATHS = 6
|
|
|
126
141
|
|
|
127
142
|
/**
|
|
128
143
|
* 从一条 insight 的正文确定性派生触发信号(零模型、可重放)。
|
|
129
|
-
* 结果只写进 `triggerDerived
|
|
144
|
+
* 结果只写进 `triggerDerived`,供提示通道使用;是否强制注入只由 authored `trigger` 决定。
|
|
130
145
|
* @param {object} ins - 归一化后的 insight 条目。
|
|
131
146
|
* @returns {{ keywords: string[], actions: string[], paths: string[] }}
|
|
132
147
|
*/
|
|
@@ -171,36 +186,36 @@ export function backfillDerivedTriggers(doc) {
|
|
|
171
186
|
|
|
172
187
|
/**
|
|
173
188
|
* 提示通道的查询文本:剔除 1–3 个字符的拉丁 token。
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* 越权 lesson 顶到了提示位;"wsl" 又把一条 pnpm lesson 顶到了"改文件时间戳"的任务里)。
|
|
178
|
-
* 内容词(≥4 字符的拉丁标识符、CJK 词)不受影响;authored trigger 通道完全不走这里。
|
|
189
|
+
* `PR` / `CI` / `OS` / `WSL` / `npm` 这类缩写太短、歧义太大,一个巧合命中就能当上该层最高分
|
|
190
|
+
* (实测:人类消息里的 "PR" 把一条越权 lesson 顶到提示位,"wsl" 把 pnpm lesson 顶进改时间戳的任务)。
|
|
191
|
+
* 内容词不受影响;authored trigger 通道完全不走这里。
|
|
179
192
|
* @param {string} text - 人类消息或工具参数。
|
|
180
193
|
* @returns {string} 过滤后的查询文本。
|
|
181
194
|
*/
|
|
182
195
|
export function hintQueryText(text) {
|
|
183
196
|
return String(text || '')
|
|
184
197
|
.split(/\s+/)
|
|
185
|
-
// 路径形态的 token 原样保留(src/util/fs.js 里的 fs / js
|
|
186
|
-
// 只对独立词做缩写剔除。
|
|
198
|
+
// 路径形态的 token 原样保留(src/util/fs.js 里的 fs / js 是有效证据),只对独立词做缩写剔除。
|
|
187
199
|
.map((w) => (/[/.]/.test(w) ? w : w.replace(/\b[A-Za-z0-9_]{1,3}\b/g, ' ')))
|
|
188
200
|
.join(' ')
|
|
189
201
|
.replace(/\s+/g, ' ')
|
|
190
202
|
.trim()
|
|
191
203
|
}
|
|
192
204
|
|
|
193
|
-
/**
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
205
|
+
/**
|
|
206
|
+
* glob(只支持 `*`)→ 正则。`?` 不是通配符,必须按字面量转义,否则 `^?` 是非法正则。
|
|
207
|
+
* `suffix: true` 时锚在**路径分段边界**上(`(?:^|/)…$`):写目标是项目相对路径,而候选常常是
|
|
208
|
+
* 绝对路径(`D:\proj\src\a.ts`),两者只差一个前缀。一次编译、两次 `test()`,不做按段切分的循环。
|
|
209
|
+
*/
|
|
210
|
+
function globToRegExp(pattern, { suffix = false } = {}) {
|
|
211
|
+
const escaped = String(pattern).replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\?/g, '\\?').replace(/\*/g, '.*')
|
|
212
|
+
return new RegExp(`${suffix ? '(?:^|/)' : '^'}${escaped}$`, 'i')
|
|
197
213
|
}
|
|
198
214
|
|
|
199
215
|
/**
|
|
200
216
|
* 剥离**引用内容**:代码块、行内代码、引号内的路径/文件名、裸路径。
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* 这个文件名让一条"做调研要先扫 curated 列表"的经验在改文件时间戳的任务里被注入。
|
|
217
|
+
* 人类消息里的文件名是**数据**,不是意图:实测"示例调研.pptx"这个文件名让一条调研经验在改文件
|
|
218
|
+
* 时间戳的任务里被注入。
|
|
204
219
|
* @param {string} text - 人类消息原文
|
|
205
220
|
* @returns {string} 只保留意图文字的版本
|
|
206
221
|
*/
|
|
@@ -211,11 +226,18 @@ export function intentText(text) {
|
|
|
211
226
|
.replace(/"[^"\n]*"/g, ' ')
|
|
212
227
|
.replace(/“[^”\n]*”/g, ' ')
|
|
213
228
|
.replace(/[A-Za-z]:\\[^\s"']*/g, ' ')
|
|
214
|
-
.
|
|
215
|
-
//
|
|
216
|
-
|
|
217
|
-
//
|
|
218
|
-
|
|
229
|
+
// 相对路径两侧的分隔符都算语料:`src/util/a.ts` 与 `src\util\a.ts` 都要剥掉,
|
|
230
|
+
// 否则 Windows 写法会在意图文本里留下 `src\util` 这类碎片,把 `a` 当意图词。
|
|
231
|
+
.replace(/(?:[A-Za-z0-9_.@-]+[\\/])+[A-Za-z0-9_.@-]+/g, ' ')
|
|
232
|
+
// 带扩展名的**非 ASCII 文件名**(`示例调研.pptx`)也要剥:ASCII 裸名(notes.md)保持原语义不动。
|
|
233
|
+
// 按 token 逐个找 `.ext`,只切"token 开头 → 扩展名结尾";`\S*` 两段匹配在长中文串上指数回溯(4000 字 12s)。
|
|
234
|
+
.replace(/\S+/g, (tok) => {
|
|
235
|
+
const re = /\.[A-Za-z][A-Za-z0-9]{0,7}\b/g
|
|
236
|
+
for (let m = re.exec(tok); m; m = re.exec(tok)) {
|
|
237
|
+
if (/[^\x00-\x7F]/.test(tok.slice(0, m.index))) return tok.slice(m.index + m[0].length)
|
|
238
|
+
}
|
|
239
|
+
return tok
|
|
240
|
+
})
|
|
219
241
|
// 仓库里的裸文件名(README / CHANGELOG …)也是语料,不是意图
|
|
220
242
|
.replace(/\b(?:README|CHANGELOG|LICENSE|AGENTS|CONTRIBUTING|Dockerfile|Makefile)\b/g, ' ')
|
|
221
243
|
.replace(/\s+/g, ' ')
|
|
@@ -223,15 +245,19 @@ export function intentText(text) {
|
|
|
223
245
|
}
|
|
224
246
|
|
|
225
247
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
* 规则来自实测:`rm` 命中 `dcterms`、`ppt` 命中 `pptx`、`ms` 命中任何含 ms 的词——短拉丁词
|
|
248
|
+
* 意图词是否值得作为触发信号(准入过滤):`rm` 命中 `dcterms`、`ppt` 命中 `pptx` 这类短拉丁词
|
|
229
249
|
* 是假阳性制造机;含 `_`/`.`/`/` 的是标识符或路径,属于语料平面,不是意图。
|
|
230
250
|
*/
|
|
231
251
|
export function isIntentWord(word) {
|
|
232
252
|
const s = String(word || '').trim()
|
|
233
253
|
if (!s) return false
|
|
234
254
|
if (CJK_RANGE.test(s)) return s.length >= 2
|
|
255
|
+
// 俄语:作者写**原形**,匹配时按 Snowball 词干比对(见 matchIntent);只要 ≥3 字符、不是虚词、
|
|
256
|
+
// 不含路径/标识符字符即可。三字母俄语词几乎都是实词,没有拉丁那种缩写歧义。
|
|
257
|
+
if (CYRILLIC_RANGE.test(s)) {
|
|
258
|
+
const w = normalizeRuWord(s)
|
|
259
|
+
return w.length >= 3 && !RU_STOPWORDS.has(w) && !/[_./\\*]/.test(w) && !/\s/.test(w)
|
|
260
|
+
}
|
|
235
261
|
if (s.length < 5) return false
|
|
236
262
|
if (/[_./\\*]/.test(s)) return false
|
|
237
263
|
if (/\s/.test(s)) return s.length >= 8
|
|
@@ -249,35 +275,75 @@ export function isDroppableGlob(pattern) {
|
|
|
249
275
|
}
|
|
250
276
|
|
|
251
277
|
/**
|
|
252
|
-
* 意图词命中:CJK 用包含,拉丁用词边界(避免 `ppt` 命中 `pptx
|
|
278
|
+
* 意图词命中:CJK 用包含,拉丁用词边界(避免 `ppt` 命中 `pptx`),俄语先做 ё/е 归一 + 词边界
|
|
279
|
+
* 精确匹配,未命中再按 Snowball 词干比对词形。为什么俄语要词干:作者写原形(`миграция`),
|
|
280
|
+
* 人类消息里几乎总是变格(`миграции`),只做字面匹配会让俄语触发面基本失效;词干与检索侧同一
|
|
281
|
+
* 把尺子,`город` 不会命中 `городской`。
|
|
253
282
|
* @param {string} word
|
|
254
283
|
* @param {string} text - 已经是 {@link intentText} 处理过的意图文本
|
|
255
284
|
*/
|
|
256
285
|
export function matchIntent(word, text) {
|
|
257
|
-
const
|
|
286
|
+
const raw = String(word || '').trim()
|
|
258
287
|
const hay = String(text || '')
|
|
259
|
-
if (!
|
|
260
|
-
if (CJK_RANGE.test(
|
|
261
|
-
if (
|
|
262
|
-
|
|
288
|
+
if (!raw || !hay) return false
|
|
289
|
+
if (CJK_RANGE.test(raw)) return raw.length >= 2 && hay.includes(raw)
|
|
290
|
+
if (CYRILLIC_RANGE.test(raw)) {
|
|
291
|
+
// 两侧都归一:作者写 `ёлка`、消息里打 `елка`(或反过来)必须命中。
|
|
292
|
+
const w = normalizeRuWord(raw)
|
|
293
|
+
if (w.length < 3) return false
|
|
294
|
+
const normalizedHay = normalizeRuWord(hay)
|
|
295
|
+
const esc = w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
296
|
+
// Unicode 词边界:`[^A-Za-z0-9_]` 会把俄文词内部的命中当成边界(`город`/`городской`);归一后已全小写。
|
|
297
|
+
if (new RegExp(`(^|[^\\p{L}\\p{N}_])${esc}(?![\\p{L}\\p{N}_])`, 'u').test(normalizedHay)) return true
|
|
298
|
+
const stem = stemRu(w)
|
|
299
|
+
if (stem.length < 2) return false
|
|
300
|
+
for (const candidate of normalizedHay.match(CYRILLIC_WORD_RE) || []) {
|
|
301
|
+
if (candidate.length < 3) continue
|
|
302
|
+
if (stemRu(candidate) === stem) return true
|
|
303
|
+
}
|
|
304
|
+
return false
|
|
305
|
+
}
|
|
306
|
+
const esc = raw.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
307
|
+
if (raw.length < 4) return false
|
|
263
308
|
return new RegExp(`(^|[^A-Za-z0-9_])${esc}([^A-Za-z0-9_]|$)`, 'i').test(hay)
|
|
264
309
|
}
|
|
265
310
|
|
|
311
|
+
/**
|
|
312
|
+
* 路径槽位的归一:反斜杠 → `/`、去掉 `./` 前缀。
|
|
313
|
+
* Windows 上模型给的候选几乎都是 `D:\proj\src\a.ts` 或 `src\a.ts`,而 `when.writes` 里存的是
|
|
314
|
+
* 项目相对、`/` 分隔的路径(写入期由 `normalizeCitationFiles()` 归好)。不归一这两侧,
|
|
315
|
+
* 带目录的 writes 判据在 Windows 上永远不会命中——只命中裸文件名(basename 兜底)。
|
|
316
|
+
* @param {unknown} value
|
|
317
|
+
* @returns {string}
|
|
318
|
+
*/
|
|
319
|
+
function normalizeMatchPath(value) {
|
|
320
|
+
return String(value || '').replace(/\\/g, '/').replace(/^\.\//, '')
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* 写目标匹配:`pattern` 是项目相对路径(可含 `*`),候选来自工具参数或人类消息,可能是绝对路径。
|
|
325
|
+
* 两档判据——整串相等,或"从某个路径分段边界起的后缀"相等(`D:/proj/src/a.ts` 的后缀
|
|
326
|
+
* `src/a.ts`)。于是"项目相对判据 vs 绝对候选"在 Windows 与 POSIX 上口径一致,而 `notlib/a.ts`
|
|
327
|
+
* 不会被 `lib/a.ts` 命中(边界是分段,不是子串)。正则自带 `i`,正好对上 Windows/macOS
|
|
328
|
+
* 大小写不敏感的文件系统语义。
|
|
329
|
+
*/
|
|
266
330
|
function matchAnyPath(pattern, targets) {
|
|
267
|
-
const p =
|
|
331
|
+
const p = normalizeMatchPath(pattern)
|
|
268
332
|
if (!p) return false
|
|
269
|
-
const
|
|
333
|
+
const full = globToRegExp(p)
|
|
334
|
+
const tail = globToRegExp(p, { suffix: true })
|
|
270
335
|
return (targets || []).some((cand) => {
|
|
271
|
-
const c =
|
|
336
|
+
const c = normalizeMatchPath(cand)
|
|
272
337
|
if (!c) return false
|
|
273
|
-
return
|
|
338
|
+
return full.test(c) || tail.test(c)
|
|
274
339
|
})
|
|
275
340
|
}
|
|
276
341
|
|
|
277
342
|
/** guard 是**收窄**条件:全部满足才放行;它自己不能触发任何东西。 */
|
|
278
|
-
function guardPass(guard, ctx) {
|
|
343
|
+
function guardPass(guard, ctx, writeTargets) {
|
|
279
344
|
if (!guard || typeof guard !== 'object') return true
|
|
280
|
-
|
|
345
|
+
// 路径面与 `when.writes` 用同一份事实:已观察到的写调用 ∪ 人类消息里的写意图。
|
|
346
|
+
const targets = writeTargets || ctx?.targets || []
|
|
281
347
|
if (Array.isArray(guard.paths) && guard.paths.length && !guard.paths.some((p) => matchAnyPath(p, targets))) return false
|
|
282
348
|
if (Array.isArray(guard.not_paths) && guard.not_paths.some((p) => matchAnyPath(p, targets))) return false
|
|
283
349
|
if (Array.isArray(guard.hosts) && guard.hosts.length && !guard.hosts.some((h) => (ctx?.hosts || []).includes(String(h)))) return false
|
|
@@ -291,11 +357,8 @@ function guardPass(guard, ctx) {
|
|
|
291
357
|
|
|
292
358
|
/**
|
|
293
359
|
* trigger 命中判定(确定性,无打分)——**准入化后的语义**。
|
|
294
|
-
*
|
|
295
|
-
* `when`
|
|
296
|
-
* `intents`(人类消息里剥离引用后的意图词)。`guard` 只能收窄。
|
|
297
|
-
* **没有 `when` 的条目不再触发任何东西**(降级为可发现 + 按需拉取)。
|
|
298
|
-
*
|
|
360
|
+
* `when` 是唯一触发面,三个成员按 OR:`ops` / `writes` / `intents`(人类消息剥离引用后的意图词);
|
|
361
|
+
* `guard` 只能收窄。**没有 `when` 的条目不再触发任何东西**(降级为可发现 + 按需拉取)。
|
|
299
362
|
* @param {object|null} trigger
|
|
300
363
|
* @param {object} ctx - {@link buildReadinessContext} 的结果
|
|
301
364
|
* @returns {string|null} 命中原因(`op:…` / `write:…` / `intent:…`),未命中为 null
|
|
@@ -304,14 +367,13 @@ export function matchTrigger(trigger, ctx) {
|
|
|
304
367
|
if (!trigger || typeof trigger !== 'object') return null
|
|
305
368
|
const when = trigger.when
|
|
306
369
|
if (!when || typeof when !== 'object') return null
|
|
307
|
-
|
|
370
|
+
// 写目标 = 已观察到的结构化写调用 ∪ 人类消息里提到的路径(动手前的写意图)。动作文本里的**读**
|
|
371
|
+
// 路径不算(`read README.md` 不是"即将写 README.md");guard 用同一份合集,否则窗口里永不放行。
|
|
372
|
+
const writeTargets = [...(ctx?.targets || []), ...(ctx?.humanPaths || [])]
|
|
373
|
+
if (!guardPass(trigger.guard, ctx, writeTargets)) return null
|
|
308
374
|
for (const op of when.ops || []) {
|
|
309
375
|
if (op && (ctx?.ops || []).includes(String(op))) return `op:${op}`
|
|
310
376
|
}
|
|
311
|
-
// 写目标 = 已观察到的结构化写调用 ∪ 人类消息里提到的路径(动手前的写意图)。
|
|
312
|
-
// DSH 没有工具执行前拦截钩子,动手前唯一能拿到的写意图就是人类消息里的路径;
|
|
313
|
-
// 但动作文本里的**读**路径不算(`read README.md` 不是"即将写 README.md")。
|
|
314
|
-
const writeTargets = [...(ctx?.targets || []), ...(ctx?.humanPaths || [])]
|
|
315
377
|
for (const w of when.writes || []) {
|
|
316
378
|
// 扩展名/泛名 glob 在这里被硬性忽略:`*.pptx` 这类条件只能撒谎,不能收窄。
|
|
317
379
|
if (!w || isDroppableGlob(w)) continue
|
|
@@ -327,12 +389,9 @@ export function matchTrigger(trigger, ctx) {
|
|
|
327
389
|
/**
|
|
328
390
|
* 旧 trigger → 新 schema(纯函数、幂等、不改原对象)。
|
|
329
391
|
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* - `keywords` → `when.intents`,只留通过 {@link isIntentWord} 的
|
|
334
|
-
* - `scope` → 默认忽略(它的值不在项目画像 tag 空间里,实测把最相关的一条 procedure 判了死刑)
|
|
335
|
-
*
|
|
392
|
+
* 迁移:`actions` → `when.ops`(经 {@link opForLegacyAction},死值记进 `triggerNormalized.dropped`);
|
|
393
|
+
* 具体 `paths` → `when.writes`(扩展名/泛名 glob 丢弃);`keywords` → `when.intents`(只留通过
|
|
394
|
+
* {@link isIntentWord} 的);`scope` → 默认忽略(它的值不在项目画像 tag 空间里)。
|
|
336
395
|
* @param {object} it
|
|
337
396
|
* @param {{legacyScope?: 'ignore'|'filter'}} [opts]
|
|
338
397
|
*/
|
|
@@ -361,13 +420,11 @@ export function normalizeTrigger(it, opts = {}) {
|
|
|
361
420
|
}
|
|
362
421
|
ops.add(op)
|
|
363
422
|
}
|
|
364
|
-
// 具体路径只写进 `when.writes
|
|
365
|
-
//
|
|
366
|
-
// (实测:那样会让 D 场景一次多出 6 条假阳性)。
|
|
423
|
+
// 具体路径只写进 `when.writes`,**不要**再补 `file-write` 到 ops:when 成员取或,补进去等于让
|
|
424
|
+
// "写了任何文件"就命中,把路径条件短路掉(实测那样一次多出 6 条假阳性)。
|
|
367
425
|
const intents = []
|
|
368
426
|
for (const k of t.keywords || []) if (isIntentWord(k)) intents.push(String(k))
|
|
369
|
-
// 旧 `symbols`
|
|
370
|
-
// (README 承诺 keywords/symbols/actions/paths/scope 都会迁移)。与 keywords 同一把准入尺子。
|
|
427
|
+
// 旧 `symbols` 也走文本平面,与 keywords 同一把准入尺子(不归一等于静默丢掉作者写下的触发面)。
|
|
371
428
|
for (const s of t.symbols || []) if (isIntentWord(s)) intents.push(String(s))
|
|
372
429
|
const when = {}
|
|
373
430
|
if (ops.size) when.ops = [...ops].sort()
|
|
@@ -375,9 +432,8 @@ export function normalizeTrigger(it, opts = {}) {
|
|
|
375
432
|
if (intents.length) when.intents = [...new Set(intents)]
|
|
376
433
|
const out = { ...t, when }
|
|
377
434
|
const scopeIgnored = Array.isArray(t.scope) && t.scope.length > 0
|
|
378
|
-
// legacy `scope` → guard.tags **只在显式要求 filter
|
|
379
|
-
//
|
|
380
|
-
// scope 的条目因此永远不命中)。默认 ignore —— 与 `src/index.js` 的 schema 默认一致。
|
|
435
|
+
// legacy `scope` → guard.tags **只在显式要求 filter 时**才做:作者手写的值几乎落不进自动派生的
|
|
436
|
+
// 画像 tag 空间,当收窄条件用等于判死条目。默认 ignore——与 `src/index.js` 的 schema 默认一致。
|
|
381
437
|
if (scopeIgnored && opts.legacyScope === 'filter') out.guard = { ...(t.guard || {}), tags: t.scope }
|
|
382
438
|
return { ...it, trigger: out, triggerNormalized: { dropped, scopeIgnored } }
|
|
383
439
|
}
|
|
@@ -428,18 +484,12 @@ export function auditTriggers(items, opts = {}) {
|
|
|
428
484
|
}
|
|
429
485
|
|
|
430
486
|
/**
|
|
431
|
-
* IDF
|
|
432
|
-
*
|
|
433
|
-
*
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
* - `coverage`:在**语料能表示**的词里,条目覆盖了多少信息量(分母不含 df=0 的词——
|
|
438
|
-
* 自然语言查询总有语料没有的词,把它们算成未命中会让任何正常查询都趋零)。
|
|
439
|
-
* - `matched`:命中的词数。单个通用词("插件")也能拿到 coverage=1.00。
|
|
440
|
-
* - `supported` / `terms`:语料里有对应词的比例。长句子里只有一两个词能在语料中找到对应时,
|
|
441
|
-
* "覆盖率 1.00"是假象(实测:一句 19 个词的改时间戳请求,只与 WSL 笔记共享一个"文件",
|
|
442
|
-
* 却拿到 cov=1.00),调用方据此让**整条通道沉默**。
|
|
487
|
+
* IDF 加权覆盖率(**绝对**门槛):条目覆盖了查询里多少**信息量**,而不是多少个 token。
|
|
488
|
+
* 为什么需要它:`relativeHits` 只看层内最高分的比例,而最高分本身可能就是噪声(实测
|
|
489
|
+
* `relative:1.00` 出现在和查询毫无关系的条目上);归一在 [0,1]、有真零点才能设下限。
|
|
490
|
+
* 返回四个量要一起看:`coverage` 在语料能表示的词里覆盖的信息量(df=0 的词不进分母);
|
|
491
|
+
* `matched` 命中词数(单个通用词也能拿 coverage=1.00);`supported`/`terms` 语料有对应词的比例,
|
|
492
|
+
* 长句只有一两个词命中时 cov=1.00 是假象,调用方据此让**整条通道沉默**。
|
|
443
493
|
* @returns {{coverage: number, matched: number, supported: number, terms: number}}
|
|
444
494
|
*/
|
|
445
495
|
export function idfCoverage(query, itemText, corpusTexts) {
|
|
@@ -448,11 +498,8 @@ export function idfCoverage(query, itemText, corpusTexts) {
|
|
|
448
498
|
|
|
449
499
|
/**
|
|
450
500
|
* 语料 → 词集合数组。**对一批候选循环评分时必须只建一次**。
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
* 候选各调一次,就是 C×C 次 tokenize —— 实测真实 store(72 条)单步 147 ms,提出循环外
|
|
454
|
-
* 降到 1.3 ms(109×),且 coverage 结果逐字段一致。热路径上每个 pre-step 都要跑一次,所以
|
|
455
|
-
* 这个差别是"每条模型消息"级别的。
|
|
501
|
+
* `idfCoverage` 每次都会重建整个语料;对 C 个候选各调一次就是 C×C 次 tokenize——实测真实 store
|
|
502
|
+
* 单步 147 ms,提出循环外降到 1.3 ms(109×),coverage 结果逐字段一致。
|
|
456
503
|
* @param {string[]} texts - 语料正文
|
|
457
504
|
* @returns {Set<string>[]} 与输入同序的词集合
|
|
458
505
|
*/
|
|
@@ -480,8 +527,8 @@ export function idfCoveragePrepared(query, itemText, corpusSets) {
|
|
|
480
527
|
for (const term of q) {
|
|
481
528
|
let df = 0
|
|
482
529
|
for (const doc of corpus) if (doc.has(term)) df++
|
|
483
|
-
// df=0
|
|
484
|
-
//
|
|
530
|
+
// df=0 的词不计入分母(对"选哪一条"没有分辨力);但 supported 记下多少词是语料无法表示的,
|
|
531
|
+
// 那是"这条查询整体离语料太远"的证据,交给调用方决定沉默。
|
|
485
532
|
if (df === 0) continue
|
|
486
533
|
supported++
|
|
487
534
|
const w = Math.log(1 + n / (1 + df))
|
|
@@ -506,7 +553,7 @@ export function relativeHits(scored, { ratioMin = 0.5 } = {}) {
|
|
|
506
553
|
if (!list.length) return []
|
|
507
554
|
const top = list[0].score
|
|
508
555
|
// 显式的 0 表示"关掉相对门槛"(只剩 score>0);未给/非法值才回退 0.5。
|
|
509
|
-
|
|
556
|
+
|
|
510
557
|
const ratio = typeof ratioMin === 'number' && Number.isFinite(ratioMin) ? ratioMin : 0.5
|
|
511
558
|
const floor = top * Math.max(0, Math.min(1, ratio))
|
|
512
559
|
return list.filter((r) => r.score >= floor)
|