@yolk_vat-y/dsh-project-memory 0.6.6 → 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 +106 -0
- package/README.md +22 -32
- package/README.zh-CN.md +23 -33
- package/package.json +2 -2
- package/src/audit.js +33 -66
- package/src/auto-inject.js +133 -228
- package/src/chunker.js +1 -1
- package/src/client/paging.js +22 -50
- package/src/client/slash.ts +12 -48
- package/src/enhancer.js +45 -92
- package/src/index-pipeline.js +14 -10
- package/src/index.js +43 -73
- package/src/insight-store.js +79 -126
- package/src/ops.js +90 -44
- package/src/project-profile.js +20 -15
- package/src/readiness.js +120 -124
- package/src/recall.js +53 -136
- package/src/skill.js +3 -1
- package/src/store.js +217 -215
- 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/fs.js +169 -207
- package/src/util/path-token.js +8 -3
- package/src/util/ru.js +3 -2
- package/src/util/search.js +15 -3
- package/src/vector-index.js +104 -108
- package/src/watch.js +45 -58
package/src/readiness.js
CHANGED
|
@@ -1,25 +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'
|
|
22
|
-
import { CYRILLIC_RANGE, RU_STOPWORDS, normalizeRuWord } from './util/ru.js'
|
|
12
|
+
import { CYRILLIC_RANGE, CYRILLIC_WORD_RE, RU_STOPWORDS, normalizeRuWord, stemRu } from './util/ru.js'
|
|
23
13
|
|
|
24
14
|
/**
|
|
25
15
|
* 动作词典:动作 id → 匹配模式。命令形态与人类意图词都归一成同一套 id,
|
|
@@ -40,15 +30,10 @@ export const ACTION_LEXICON = [
|
|
|
40
30
|
|
|
41
31
|
/**
|
|
42
32
|
* 允许**人类消息里的动作词**触发全文注入的 op 白名单。
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* 反过来,`删除 / 清理 / 渲染 / 基准 / 取网页 / 查询记忆` 这些词在人类消息里经常只是
|
|
49
|
-
* **提及**("删除后的悬空状态"、"清理一下 README 的措辞"、"这个渲染是错的"),
|
|
50
|
-
* 用它们做确定性全文注入实测代价是 1200 字符一条的无关 procedure。
|
|
51
|
-
* 这些动作仍可由**已观察到的工具调用**触发(`rm` 真的执行了、`soffice` 真的跑了)。
|
|
33
|
+
* 判据是"这条记忆必须在动作**之前**到达,晚了就没用,且动作不可逆":提交 / 发 npm 包 / 发版 /
|
|
34
|
+
* 推远端 / 部署 / 公开仓库。
|
|
35
|
+
* 反过来,`删除 / 清理 / 渲染 / 基准 / 取网页 / 查询记忆` 在人类消息里经常只是**提及**,用它们做
|
|
36
|
+
* 全文注入实测代价是 1200 字符一条的无关 procedure;这些动作仍可由**已观察到的工具调用**触发。
|
|
52
37
|
*/
|
|
53
38
|
export const PRE_ACTION_OPS = new Set(['npm-publish', 'release', 'git-commit', 'git-push', 'deploy', 'go-public'])
|
|
54
39
|
|
|
@@ -69,8 +54,7 @@ export function detectActions(text) {
|
|
|
69
54
|
|
|
70
55
|
/**
|
|
71
56
|
* 文本 → 路径 token(用于 trigger 的 `writes` / `guard.paths` 匹配)。
|
|
72
|
-
* 规则收在 `util/path-token.js
|
|
73
|
-
* (原先这里与 ops.js 各抄一份,ops 那份漏了点开头文件名且已漂移)。
|
|
57
|
+
* 规则收在 `util/path-token.js`,与 ops 抽写目标是**同一份实现**。
|
|
74
58
|
* @param {string} text - 工具调用参数或人类消息。
|
|
75
59
|
* @returns {string[]} 去重后的路径 token。
|
|
76
60
|
*/
|
|
@@ -80,17 +64,11 @@ export function extractPaths(text) {
|
|
|
80
64
|
|
|
81
65
|
/**
|
|
82
66
|
* 组装就绪上下文:人类意图 + 已观察动作。三处来源都做动作/路径归一,调用方只需给文本。
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* @param {string} [input.humanText] - 本轮人类消息(先发窗口)。
|
|
89
|
-
* @param {string} [input.actionText] - 旧调用方的工具调用文本(测试/老宿主;生产路径不再传)。
|
|
90
|
-
* @param {string[]} [input.ops] - 结构化工具调用归一出的动作(可信)。
|
|
91
|
-
* @param {string[]} [input.targets] - 结构化写目标(可信)。
|
|
92
|
-
* @param {string[]} [input.actions] - 调用方已归一化的动作 id(可选,会被并集)。
|
|
93
|
-
* @param {string[]} [input.paths] - 调用方已归一化的路径(可选,会被并集)。
|
|
67
|
+
* **证据来源决定准入**:observed(结构化工具调用)可以触发全文注入;人类消息里的动作词只允许
|
|
68
|
+
* 4 个"不可逆外部动作"触发全文(见 {@link PRE_ACTION_OPS}),其余只进提示通道。
|
|
69
|
+
* @param {object} input `humanText` 本轮人类消息;`actionText` 旧调用方的工具调用文本(测试/老宿主,
|
|
70
|
+
* 生产路径不再传);`ops`/`targets` 结构化工具调用归一出的动作与写目标(可信);
|
|
71
|
+
* `actions`/`paths` 调用方已归一化的动作 id 与路径(可选,会被并集)。
|
|
94
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[] }}
|
|
95
73
|
*/
|
|
96
74
|
export function buildReadinessContext(input = {}) {
|
|
@@ -163,7 +141,7 @@ const DERIVED_MAX_PATHS = 6
|
|
|
163
141
|
|
|
164
142
|
/**
|
|
165
143
|
* 从一条 insight 的正文确定性派生触发信号(零模型、可重放)。
|
|
166
|
-
* 结果只写进 `triggerDerived
|
|
144
|
+
* 结果只写进 `triggerDerived`,供提示通道使用;是否强制注入只由 authored `trigger` 决定。
|
|
167
145
|
* @param {object} ins - 归一化后的 insight 条目。
|
|
168
146
|
* @returns {{ keywords: string[], actions: string[], paths: string[] }}
|
|
169
147
|
*/
|
|
@@ -208,36 +186,36 @@ export function backfillDerivedTriggers(doc) {
|
|
|
208
186
|
|
|
209
187
|
/**
|
|
210
188
|
* 提示通道的查询文本:剔除 1–3 个字符的拉丁 token。
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
* 越权 lesson 顶到了提示位;"wsl" 又把一条 pnpm lesson 顶到了"改文件时间戳"的任务里)。
|
|
215
|
-
* 内容词(≥4 字符的拉丁标识符、CJK 词)不受影响;authored trigger 通道完全不走这里。
|
|
189
|
+
* `PR` / `CI` / `OS` / `WSL` / `npm` 这类缩写太短、歧义太大,一个巧合命中就能当上该层最高分
|
|
190
|
+
* (实测:人类消息里的 "PR" 把一条越权 lesson 顶到提示位,"wsl" 把 pnpm lesson 顶进改时间戳的任务)。
|
|
191
|
+
* 内容词不受影响;authored trigger 通道完全不走这里。
|
|
216
192
|
* @param {string} text - 人类消息或工具参数。
|
|
217
193
|
* @returns {string} 过滤后的查询文本。
|
|
218
194
|
*/
|
|
219
195
|
export function hintQueryText(text) {
|
|
220
196
|
return String(text || '')
|
|
221
197
|
.split(/\s+/)
|
|
222
|
-
// 路径形态的 token 原样保留(src/util/fs.js 里的 fs / js
|
|
223
|
-
// 只对独立词做缩写剔除。
|
|
198
|
+
// 路径形态的 token 原样保留(src/util/fs.js 里的 fs / js 是有效证据),只对独立词做缩写剔除。
|
|
224
199
|
.map((w) => (/[/.]/.test(w) ? w : w.replace(/\b[A-Za-z0-9_]{1,3}\b/g, ' ')))
|
|
225
200
|
.join(' ')
|
|
226
201
|
.replace(/\s+/g, ' ')
|
|
227
202
|
.trim()
|
|
228
203
|
}
|
|
229
204
|
|
|
230
|
-
/**
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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')
|
|
234
213
|
}
|
|
235
214
|
|
|
236
215
|
/**
|
|
237
216
|
* 剥离**引用内容**:代码块、行内代码、引号内的路径/文件名、裸路径。
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* 这个文件名让一条"做调研要先扫 curated 列表"的经验在改文件时间戳的任务里被注入。
|
|
217
|
+
* 人类消息里的文件名是**数据**,不是意图:实测"示例调研.pptx"这个文件名让一条调研经验在改文件
|
|
218
|
+
* 时间戳的任务里被注入。
|
|
241
219
|
* @param {string} text - 人类消息原文
|
|
242
220
|
* @returns {string} 只保留意图文字的版本
|
|
243
221
|
*/
|
|
@@ -248,11 +226,18 @@ export function intentText(text) {
|
|
|
248
226
|
.replace(/"[^"\n]*"/g, ' ')
|
|
249
227
|
.replace(/“[^”\n]*”/g, ' ')
|
|
250
228
|
.replace(/[A-Za-z]:\\[^\s"']*/g, ' ')
|
|
251
|
-
.
|
|
252
|
-
//
|
|
253
|
-
|
|
254
|
-
//
|
|
255
|
-
|
|
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
|
+
})
|
|
256
241
|
// 仓库里的裸文件名(README / CHANGELOG …)也是语料,不是意图
|
|
257
242
|
.replace(/\b(?:README|CHANGELOG|LICENSE|AGENTS|CONTRIBUTING|Dockerfile|Makefile)\b/g, ' ')
|
|
258
243
|
.replace(/\s+/g, ' ')
|
|
@@ -260,18 +245,15 @@ export function intentText(text) {
|
|
|
260
245
|
}
|
|
261
246
|
|
|
262
247
|
/**
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
* 规则来自实测:`rm` 命中 `dcterms`、`ppt` 命中 `pptx`、`ms` 命中任何含 ms 的词——短拉丁词
|
|
248
|
+
* 意图词是否值得作为触发信号(准入过滤):`rm` 命中 `dcterms`、`ppt` 命中 `pptx` 这类短拉丁词
|
|
266
249
|
* 是假阳性制造机;含 `_`/`.`/`/` 的是标识符或路径,属于语料平面,不是意图。
|
|
267
250
|
*/
|
|
268
251
|
export function isIntentWord(word) {
|
|
269
252
|
const s = String(word || '').trim()
|
|
270
253
|
if (!s) return false
|
|
271
254
|
if (CJK_RANGE.test(s)) return s.length >= 2
|
|
272
|
-
//
|
|
273
|
-
//
|
|
274
|
-
// 而俄语的三字母词几乎都是实词(虚词已经被 RU_STOPWORDS 挡掉),没有拉丁那种缩写歧义。
|
|
255
|
+
// 俄语:作者写**原形**,匹配时按 Snowball 词干比对(见 matchIntent);只要 ≥3 字符、不是虚词、
|
|
256
|
+
// 不含路径/标识符字符即可。三字母俄语词几乎都是实词,没有拉丁那种缩写歧义。
|
|
275
257
|
if (CYRILLIC_RANGE.test(s)) {
|
|
276
258
|
const w = normalizeRuWord(s)
|
|
277
259
|
return w.length >= 3 && !RU_STOPWORDS.has(w) && !/[_./\\*]/.test(w) && !/\s/.test(w)
|
|
@@ -293,42 +275,75 @@ export function isDroppableGlob(pattern) {
|
|
|
293
275
|
}
|
|
294
276
|
|
|
295
277
|
/**
|
|
296
|
-
* 意图词命中:CJK 用包含,拉丁用词边界(避免 `ppt` 命中 `pptx
|
|
278
|
+
* 意图词命中:CJK 用包含,拉丁用词边界(避免 `ppt` 命中 `pptx`),俄语先做 ё/е 归一 + 词边界
|
|
279
|
+
* 精确匹配,未命中再按 Snowball 词干比对词形。为什么俄语要词干:作者写原形(`миграция`),
|
|
280
|
+
* 人类消息里几乎总是变格(`миграции`),只做字面匹配会让俄语触发面基本失效;词干与检索侧同一
|
|
281
|
+
* 把尺子,`город` 不会命中 `городской`。
|
|
297
282
|
* @param {string} word
|
|
298
283
|
* @param {string} text - 已经是 {@link intentText} 处理过的意图文本
|
|
299
284
|
*/
|
|
300
285
|
export function matchIntent(word, text) {
|
|
301
|
-
const
|
|
286
|
+
const raw = String(word || '').trim()
|
|
302
287
|
const hay = String(text || '')
|
|
303
|
-
if (!
|
|
304
|
-
if (CJK_RANGE.test(
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
// 长度门槛与 isIntentWord 保持一致(≥3),不套用拉丁的 ≥4。
|
|
309
|
-
if (CYRILLIC_RANGE.test(w)) {
|
|
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)
|
|
310
293
|
if (w.length < 3) return false
|
|
311
|
-
|
|
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
|
|
312
305
|
}
|
|
313
|
-
|
|
306
|
+
const esc = raw.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
307
|
+
if (raw.length < 4) return false
|
|
314
308
|
return new RegExp(`(^|[^A-Za-z0-9_])${esc}([^A-Za-z0-9_]|$)`, 'i').test(hay)
|
|
315
309
|
}
|
|
316
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
|
+
*/
|
|
317
330
|
function matchAnyPath(pattern, targets) {
|
|
318
|
-
const p =
|
|
331
|
+
const p = normalizeMatchPath(pattern)
|
|
319
332
|
if (!p) return false
|
|
320
|
-
const
|
|
333
|
+
const full = globToRegExp(p)
|
|
334
|
+
const tail = globToRegExp(p, { suffix: true })
|
|
321
335
|
return (targets || []).some((cand) => {
|
|
322
|
-
const c =
|
|
336
|
+
const c = normalizeMatchPath(cand)
|
|
323
337
|
if (!c) return false
|
|
324
|
-
return
|
|
338
|
+
return full.test(c) || tail.test(c)
|
|
325
339
|
})
|
|
326
340
|
}
|
|
327
341
|
|
|
328
342
|
/** guard 是**收窄**条件:全部满足才放行;它自己不能触发任何东西。 */
|
|
329
|
-
function guardPass(guard, ctx) {
|
|
343
|
+
function guardPass(guard, ctx, writeTargets) {
|
|
330
344
|
if (!guard || typeof guard !== 'object') return true
|
|
331
|
-
|
|
345
|
+
// 路径面与 `when.writes` 用同一份事实:已观察到的写调用 ∪ 人类消息里的写意图。
|
|
346
|
+
const targets = writeTargets || ctx?.targets || []
|
|
332
347
|
if (Array.isArray(guard.paths) && guard.paths.length && !guard.paths.some((p) => matchAnyPath(p, targets))) return false
|
|
333
348
|
if (Array.isArray(guard.not_paths) && guard.not_paths.some((p) => matchAnyPath(p, targets))) return false
|
|
334
349
|
if (Array.isArray(guard.hosts) && guard.hosts.length && !guard.hosts.some((h) => (ctx?.hosts || []).includes(String(h)))) return false
|
|
@@ -342,11 +357,8 @@ function guardPass(guard, ctx) {
|
|
|
342
357
|
|
|
343
358
|
/**
|
|
344
359
|
* trigger 命中判定(确定性,无打分)——**准入化后的语义**。
|
|
345
|
-
*
|
|
346
|
-
* `when`
|
|
347
|
-
* `intents`(人类消息里剥离引用后的意图词)。`guard` 只能收窄。
|
|
348
|
-
* **没有 `when` 的条目不再触发任何东西**(降级为可发现 + 按需拉取)。
|
|
349
|
-
*
|
|
360
|
+
* `when` 是唯一触发面,三个成员按 OR:`ops` / `writes` / `intents`(人类消息剥离引用后的意图词);
|
|
361
|
+
* `guard` 只能收窄。**没有 `when` 的条目不再触发任何东西**(降级为可发现 + 按需拉取)。
|
|
350
362
|
* @param {object|null} trigger
|
|
351
363
|
* @param {object} ctx - {@link buildReadinessContext} 的结果
|
|
352
364
|
* @returns {string|null} 命中原因(`op:…` / `write:…` / `intent:…`),未命中为 null
|
|
@@ -355,14 +367,13 @@ export function matchTrigger(trigger, ctx) {
|
|
|
355
367
|
if (!trigger || typeof trigger !== 'object') return null
|
|
356
368
|
const when = trigger.when
|
|
357
369
|
if (!when || typeof when !== 'object') return null
|
|
358
|
-
|
|
370
|
+
// 写目标 = 已观察到的结构化写调用 ∪ 人类消息里提到的路径(动手前的写意图)。动作文本里的**读**
|
|
371
|
+
// 路径不算(`read README.md` 不是"即将写 README.md");guard 用同一份合集,否则窗口里永不放行。
|
|
372
|
+
const writeTargets = [...(ctx?.targets || []), ...(ctx?.humanPaths || [])]
|
|
373
|
+
if (!guardPass(trigger.guard, ctx, writeTargets)) return null
|
|
359
374
|
for (const op of when.ops || []) {
|
|
360
375
|
if (op && (ctx?.ops || []).includes(String(op))) return `op:${op}`
|
|
361
376
|
}
|
|
362
|
-
// 写目标 = 已观察到的结构化写调用 ∪ 人类消息里提到的路径(动手前的写意图)。
|
|
363
|
-
// DSH 没有工具执行前拦截钩子,动手前唯一能拿到的写意图就是人类消息里的路径;
|
|
364
|
-
// 但动作文本里的**读**路径不算(`read README.md` 不是"即将写 README.md")。
|
|
365
|
-
const writeTargets = [...(ctx?.targets || []), ...(ctx?.humanPaths || [])]
|
|
366
377
|
for (const w of when.writes || []) {
|
|
367
378
|
// 扩展名/泛名 glob 在这里被硬性忽略:`*.pptx` 这类条件只能撒谎,不能收窄。
|
|
368
379
|
if (!w || isDroppableGlob(w)) continue
|
|
@@ -378,12 +389,9 @@ export function matchTrigger(trigger, ctx) {
|
|
|
378
389
|
/**
|
|
379
390
|
* 旧 trigger → 新 schema(纯函数、幂等、不改原对象)。
|
|
380
391
|
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
* - `keywords` → `when.intents`,只留通过 {@link isIntentWord} 的
|
|
385
|
-
* - `scope` → 默认忽略(它的值不在项目画像 tag 空间里,实测把最相关的一条 procedure 判了死刑)
|
|
386
|
-
*
|
|
392
|
+
* 迁移:`actions` → `when.ops`(经 {@link opForLegacyAction},死值记进 `triggerNormalized.dropped`);
|
|
393
|
+
* 具体 `paths` → `when.writes`(扩展名/泛名 glob 丢弃);`keywords` → `when.intents`(只留通过
|
|
394
|
+
* {@link isIntentWord} 的);`scope` → 默认忽略(它的值不在项目画像 tag 空间里)。
|
|
387
395
|
* @param {object} it
|
|
388
396
|
* @param {{legacyScope?: 'ignore'|'filter'}} [opts]
|
|
389
397
|
*/
|
|
@@ -412,13 +420,11 @@ export function normalizeTrigger(it, opts = {}) {
|
|
|
412
420
|
}
|
|
413
421
|
ops.add(op)
|
|
414
422
|
}
|
|
415
|
-
// 具体路径只写进 `when.writes
|
|
416
|
-
//
|
|
417
|
-
// (实测:那样会让 D 场景一次多出 6 条假阳性)。
|
|
423
|
+
// 具体路径只写进 `when.writes`,**不要**再补 `file-write` 到 ops:when 成员取或,补进去等于让
|
|
424
|
+
// "写了任何文件"就命中,把路径条件短路掉(实测那样一次多出 6 条假阳性)。
|
|
418
425
|
const intents = []
|
|
419
426
|
for (const k of t.keywords || []) if (isIntentWord(k)) intents.push(String(k))
|
|
420
|
-
// 旧 `symbols`
|
|
421
|
-
// (README 承诺 keywords/symbols/actions/paths/scope 都会迁移)。与 keywords 同一把准入尺子。
|
|
427
|
+
// 旧 `symbols` 也走文本平面,与 keywords 同一把准入尺子(不归一等于静默丢掉作者写下的触发面)。
|
|
422
428
|
for (const s of t.symbols || []) if (isIntentWord(s)) intents.push(String(s))
|
|
423
429
|
const when = {}
|
|
424
430
|
if (ops.size) when.ops = [...ops].sort()
|
|
@@ -426,9 +432,8 @@ export function normalizeTrigger(it, opts = {}) {
|
|
|
426
432
|
if (intents.length) when.intents = [...new Set(intents)]
|
|
427
433
|
const out = { ...t, when }
|
|
428
434
|
const scopeIgnored = Array.isArray(t.scope) && t.scope.length > 0
|
|
429
|
-
// legacy `scope` → guard.tags **只在显式要求 filter
|
|
430
|
-
//
|
|
431
|
-
// scope 的条目因此永远不命中)。默认 ignore —— 与 `src/index.js` 的 schema 默认一致。
|
|
435
|
+
// legacy `scope` → guard.tags **只在显式要求 filter 时**才做:作者手写的值几乎落不进自动派生的
|
|
436
|
+
// 画像 tag 空间,当收窄条件用等于判死条目。默认 ignore——与 `src/index.js` 的 schema 默认一致。
|
|
432
437
|
if (scopeIgnored && opts.legacyScope === 'filter') out.guard = { ...(t.guard || {}), tags: t.scope }
|
|
433
438
|
return { ...it, trigger: out, triggerNormalized: { dropped, scopeIgnored } }
|
|
434
439
|
}
|
|
@@ -479,18 +484,12 @@ export function auditTriggers(items, opts = {}) {
|
|
|
479
484
|
}
|
|
480
485
|
|
|
481
486
|
/**
|
|
482
|
-
* IDF
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
* - `coverage`:在**语料能表示**的词里,条目覆盖了多少信息量(分母不含 df=0 的词——
|
|
489
|
-
* 自然语言查询总有语料没有的词,把它们算成未命中会让任何正常查询都趋零)。
|
|
490
|
-
* - `matched`:命中的词数。单个通用词("插件")也能拿到 coverage=1.00。
|
|
491
|
-
* - `supported` / `terms`:语料里有对应词的比例。长句子里只有一两个词能在语料中找到对应时,
|
|
492
|
-
* "覆盖率 1.00"是假象(实测:一句 19 个词的改时间戳请求,只与 WSL 笔记共享一个"文件",
|
|
493
|
-
* 却拿到 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 是假象,调用方据此让**整条通道沉默**。
|
|
494
493
|
* @returns {{coverage: number, matched: number, supported: number, terms: number}}
|
|
495
494
|
*/
|
|
496
495
|
export function idfCoverage(query, itemText, corpusTexts) {
|
|
@@ -499,11 +498,8 @@ export function idfCoverage(query, itemText, corpusTexts) {
|
|
|
499
498
|
|
|
500
499
|
/**
|
|
501
500
|
* 语料 → 词集合数组。**对一批候选循环评分时必须只建一次**。
|
|
502
|
-
*
|
|
503
|
-
*
|
|
504
|
-
* 候选各调一次,就是 C×C 次 tokenize —— 实测真实 store(72 条)单步 147 ms,提出循环外
|
|
505
|
-
* 降到 1.3 ms(109×),且 coverage 结果逐字段一致。热路径上每个 pre-step 都要跑一次,所以
|
|
506
|
-
* 这个差别是"每条模型消息"级别的。
|
|
501
|
+
* `idfCoverage` 每次都会重建整个语料;对 C 个候选各调一次就是 C×C 次 tokenize——实测真实 store
|
|
502
|
+
* 单步 147 ms,提出循环外降到 1.3 ms(109×),coverage 结果逐字段一致。
|
|
507
503
|
* @param {string[]} texts - 语料正文
|
|
508
504
|
* @returns {Set<string>[]} 与输入同序的词集合
|
|
509
505
|
*/
|
|
@@ -531,8 +527,8 @@ export function idfCoveragePrepared(query, itemText, corpusSets) {
|
|
|
531
527
|
for (const term of q) {
|
|
532
528
|
let df = 0
|
|
533
529
|
for (const doc of corpus) if (doc.has(term)) df++
|
|
534
|
-
// df=0
|
|
535
|
-
//
|
|
530
|
+
// df=0 的词不计入分母(对"选哪一条"没有分辨力);但 supported 记下多少词是语料无法表示的,
|
|
531
|
+
// 那是"这条查询整体离语料太远"的证据,交给调用方决定沉默。
|
|
536
532
|
if (df === 0) continue
|
|
537
533
|
supported++
|
|
538
534
|
const w = Math.log(1 + n / (1 + df))
|
|
@@ -557,7 +553,7 @@ export function relativeHits(scored, { ratioMin = 0.5 } = {}) {
|
|
|
557
553
|
if (!list.length) return []
|
|
558
554
|
const top = list[0].score
|
|
559
555
|
// 显式的 0 表示"关掉相对门槛"(只剩 score>0);未给/非法值才回退 0.5。
|
|
560
|
-
|
|
556
|
+
|
|
561
557
|
const ratio = typeof ratioMin === 'number' && Number.isFinite(ratioMin) ? ratioMin : 0.5
|
|
562
558
|
const floor = top * Math.max(0, Math.min(1, ratio))
|
|
563
559
|
return list.filter((r) => r.score >= floor)
|