Skip to content

Commit 77e9c58

Browse files
committed
feat(inject): 注入条数的查询自适应——确定少注入、模糊维持上限(issue #239 第 5 项)
- 新增 injectUncertaintyAdaptive(默认关)。开启后:确定性强的话题把注入条数收缩到 一半(下限 1),模糊话题(回指/时间线索,或极短查询)维持 maxInjectedItems 上限 ——只做**单向收缩**,绝不越过用户配置的上限。 - 依据:不确定性驱动的读路径(模糊多召回、确定少召回);但「注入相关却带偏生成」 的内容可能比不注入更糟(两处独立出处),故宁可少注入也不新增该风险面。 - 判据只看查询本身、零额外检索(先探针检索等于白付一次 fuseRecall);拿不到查询 时维持现状不猜。 - 纯函数 needsBroadRecall / adaptiveInjectBudget 落在 src/search/adaptive.js(与既有 的查询自适应向量阈值同源),可单测。 - 测试:新增 test/adaptive-inject-budget.test.js 5 例(判据、预算边界、默认关行为 不变、开启后确定/模糊分档、无会话日志退化);变异测试把开关钉死 → 1 例转红。 - api 旗标锁 58 → 59;README 补一行;CHANGELOG 记入 [Unreleased]。
1 parent e1d8aab commit 77e9c58

12 files changed

Lines changed: 198 additions & 6 deletions

File tree

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- **蒸馏的成本感知级联与有界检查点(issue #239,第一批)**:两个 opt-in 旋钮,默认 `0` = 行为逐字节不变。① `summarizeMinWindowChars`——**蒸馏前的零 LLM 预判**:窗口内可蒸馏文本不足阈值时直接跳过 LLM 调用(纯规则判定、无模型参与;被挡下的窗口照常消费游标,否则每个 `turn/end` 都会重评同一段短文本),skip 原因写进 `llm_audit_logs`(`status='skipped'` / `error_message='window-too-small'`)——此前只有最小间隔一档留痕,「这一轮为什么没蒸馏」基本不可观测;② `summarizeMaxRunsPerSession`——每会话最多发起多少次蒸馏,只统计**真正发起过** LLM 调用的 run(被预判挡下的窗口不占额度),aborted 调用按既有口径回滚,游标刻意不消费(预算恢复或重启后仍能蒸馏到该窗口)。两键在 `config.js` schema 与 `settings.js` 整数白名单成对落位,面板可调;第 4 项(错峰队列)与第 5 项(注入侧不确定性召回)留后续批次。
1212

13+
- **注入条数的查询自适应(issue #239 第 5 项)**:`injectUncertaintyAdaptive`(默认关)。开启后:确定性强的话题把注入条数收缩到一半(下限 1),模糊话题(回指/时间线索,或极短查询)维持 `maxInjectedItems` 上限——**只做单向收缩**,绝不越过用户配置的上限。依据是不确定性驱动的读路径(模糊多召回、确定少召回),但「注入相关却带偏生成」的内容可能比不注入更糟(两处独立出处),所以宁可少注入也不新增这个风险面。判据只看查询本身、零额外检索(先探针检索等于白付一次 `fuseRecall`);拿不到查询时维持现状不猜。纯函数 `needsBroadRecall` / `adaptiveInjectBudget` 落在 `src/search/adaptive.js`(与既有的查询自适应向量阈值同源),可单测。
14+
1315
## 🐛 修复
1416

1517
- **睡眠冲突/模式阶段的输出预算可配(issue #257)**:`src/dream/sleep.js` 冲突消解与模式发现两处 `maxTokens: 2048` 硬编码提为 `sleepMaxTokens`(默认 8192,schema + 整数白名单成对落位,面板可调)。实测依据(报告者 llama.cpp 环境):默认档 24 对裁决需 2097 token,恰好压在 2048 边界(53 次运行 48 败 5 胜的「间歇性失败」指纹);`sleepActionSet: full` 六分支实测需 6967(3.4 倍越界)——该档位自 #126 引入起从未跑通过。流式计费按实际用量,调大不增加成本。

‎dsh-mneme/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ dsh web
426426
| `distillRateLimitRetries` | `3` | 命中 429 限流时的指数退避重试次数(0-10) |
427427
| `distillRateLimitBaseDelayMs` | `1000` | 429 退避基准延迟(ms):1s→2s→4s… |
428428
| `maxInjectedItems` | `5` | 最多注入几条记忆 |
429+
| `injectUncertaintyAdaptive` | `false` | 注入条数的查询自适应:确定性强的话题把条数收缩到一半(下限 1),模糊话题(回指/时间线索,或极短查询)维持 `maxInjectedItems` 上限;**只做单向收缩**,判据只看查询本身、不做额外检索(#239 第 5 项) |
429430
| `injectRotationTurns` | `0` | 注入位跨轮轮换:同一条记忆在最近 N 个查询轮次注入过后本轮不再优先(新鲜优先、不足回填,槽位数不变;会话边界自动重置;`0` = 关闭保持现状) |
430431
| `injectContentMaxChars` | `300` | 注入单条正文截断上限(60-4000,原硬编码 300,#164①/#225):截断尾部带上限/原长/全文 `memory_get` 指引;块预算 `Math.max(1500, 上限+600)` 随上限放大 |
431432
| `importanceThreshold` | `3` | 注入的最低重要性(1-5) |

‎dsh-mneme/lib/config.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export const Config = z.object({
5858
// 默认 300 = 与既有行为一致。
5959
injectContentMaxChars: z.natural().min(60).max(4000).default(300),
6060
importanceThreshold: z.natural().min(1).max(5).default(3),
61+
// Issue #239(第 5 项):注入条数的查询自适应(默认关)。确定性强的话题收缩注入
62+
// 条数(减半、下限 1),模糊话题(回指/时间线索,或极短查询)维持
63+
// maxInjectedItems 上限——只做**单向收缩**,绝不越过用户配置的上限;判据只看
64+
// 查询本身,不做额外检索(先探针检索等于白付一次 fuseRecall)。
65+
injectUncertaintyAdaptive: z.boolean().default(false),
6166
// 编码记忆蒸馏(codingRetrospect,opt-in,默认关)。开启时,turn/end 蒸馏
6267
// 额外提取三类编码专属记忆:rejected_solution(被否决方案)/ pitfall(踩坑)/
6368
// constraint(工程约束)。蒸馏上下文为整轮完整对话(用户输入 → 助手思考/回答

‎dsh-mneme/lib/inject.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createScopeResolver } from "./scope.js";
22
import { createHotMemory } from "./hot-memory.js";
33
import { STR, langOf } from "./lang.js";
4+
import { adaptiveInjectBudget } from "./search/adaptive.js";
45

56
// Best-effort extraction of the current user's latest message text from the
67
// live session, for semantic-first injection (Bug4). The system-prompt
@@ -92,7 +93,7 @@ function extractRounds(ctx, maxRounds) {
9293

9394
export function createInjector(ctx, service, settings, config) {
9495
const language = langOf(config);
95-
const maxItems = config.maxInjectedItems ?? 5;
96+
const baseMaxItems = config.maxInjectedItems ?? 5;
9697
const threshold = config.importanceThreshold ?? 3;
9798
// Issue #205:注入位跨轮轮换。rotationTurns = 最近 N 个「不同用户查询」轮次
9899
// 注入过的记忆本轮不再优先(0 = 关闭,保持既有行为)。历史按会话维护——
@@ -270,6 +271,12 @@ export function createInjector(ctx, service, settings, config) {
270271
const rotate = recentInjectedIds(sessionId, query);
271272
// rotateWindow 随集合一起传入:候选池按 maxItems×(N+1) 扩容——窗口要的
272273
// 牌比既有 maxItems×2 池多时,轮换才有新鲜牌可换(#205 补测)。
274+
// Issue #239(第 5 项):注入条数的查询自适应(默认关)。确定性强的话题收缩
275+
// 条数、模糊话题维持上限——单向收缩,绝不越过 maxInjectedItems;判据只看查询
276+
// 本身,不做额外检索(先探针检索等于白付一次 fuseRecall)。
277+
const maxItems = config.injectUncertaintyAdaptive === true
278+
? adaptiveInjectBudget(query, baseMaxItems)
279+
: baseMaxItems;
273280
const candidates = service.injectCandidates({ query, queryVector, maxItems, threshold, scope, rotate, rotateWindow: rotationTurns });
274281
recordInjection(sessionId, query, candidates);
275282
// Hot memory (v0.5.0 1.3) leads the single memory block: the agent

‎dsh-mneme/lib/search/adaptive.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,41 @@ export function adaptiveThreshold(query, candidates = []) {
2020
if (scores.length >= 5 && scores[0] - scores[4] > 0.3) return 0.5;
2121
return 0.65;
2222
}
23+
24+
// Issue #239(第 5 项):注入条数的查询自适应(去 always-on 化的一半)。
25+
// 依据:不确定性强的话题多召回、确定性的少召回(Oblivion 的不确定性驱动读路径);
26+
// 但**注入相关却带偏生成**的内容可能比不注入更糟(2607.24010 与 2604.02280 两处
27+
// 独立出处),所以这里只做**单向收缩**:模糊 → 维持用户配置的上限,确定 → 收缩,
28+
// 绝不越过上限。判据只看查询本身——不做额外检索(2607.24010:触发侧成本别忽略,
29+
// 先探针检索等于白付一次 fuseRecall)。
30+
const RECALL_CUES = [
31+
// 中文回指 / 时间线索:出现即说明用户要的是「记忆里的东西」,当场算不出来
32+
"上次", "之前", "以前", "那天", "那次", "刚才", "刚刚", "后来", "很早",
33+
"还记得", "记不记得", "我们讨论", "我们聊", "我说过", "你提过", "提到过",
34+
"最近", "上次说", "之前说",
35+
// 英文同类线索
36+
"last time", "previously", "earlier", "remember", "we discussed",
37+
"i mentioned", "you said", "that thing", "remind me"
38+
];
39+
40+
/**
41+
* 这段查询是否需要「宽召回」。命中回指/时间线索,或短到几乎匹配任何东西
42+
* (与 adaptiveThreshold 对 <5 字符的判断同源)。纯函数、零成本。
43+
* 拿不到查询时不猜——返回 true 维持现状。
44+
*/
45+
export function needsBroadRecall(query) {
46+
const q = String(query ?? "").trim().toLowerCase();
47+
if (q === "") return true;
48+
if (q.length < 5) return true;
49+
return RECALL_CUES.some((cue) => q.includes(cue));
50+
}
51+
52+
/**
53+
* 注入条数的自适应预算:模糊 → 维持配置上限;确定 → 收缩到一半(下限 1)。
54+
* `maxItems <= 1` 时原样返回(含 0 = 不注入,flag 打开也不该把 0 变成 1)。
55+
*/
56+
export function adaptiveInjectBudget(query, maxItems) {
57+
const cap = Number.isFinite(maxItems) && maxItems > 0 ? Math.floor(maxItems) : maxItems;
58+
if (!Number.isFinite(cap) || cap <= 1) return cap;
59+
return needsBroadRecall(query) ? cap : Math.max(1, Math.ceil(cap / 2));
60+
}

‎dsh-mneme/lib/settings.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ const FEATURE_FLAG_BOOLEANS = [
4343
"autoInject",
4444
"autoSummarize",
4545
"hotMemoryEnabled",
46+
// Issue #239 第 5 项:注入条数的查询自适应(确定性强则收缩注入条数,默认关)。
47+
"injectUncertaintyAdaptive",
4648
"entityExtractionEnabled",
4749
// Issue #219:图召回轴——查询命中实体名时把挂联记忆并入检索融合池
4850
// (默认关;依赖实体抽取产出,lightMode 强制关闭)。

‎dsh-mneme/src/config.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export const Config = z.object({
5858
// 默认 300 = 与既有行为一致。
5959
injectContentMaxChars: z.natural().min(60).max(4000).default(300),
6060
importanceThreshold: z.natural().min(1).max(5).default(3),
61+
// Issue #239(第 5 项):注入条数的查询自适应(默认关)。确定性强的话题收缩注入
62+
// 条数(减半、下限 1),模糊话题(回指/时间线索,或极短查询)维持
63+
// maxInjectedItems 上限——只做**单向收缩**,绝不越过用户配置的上限;判据只看
64+
// 查询本身,不做额外检索(先探针检索等于白付一次 fuseRecall)。
65+
injectUncertaintyAdaptive: z.boolean().default(false),
6166
// 编码记忆蒸馏(codingRetrospect,opt-in,默认关)。开启时,turn/end 蒸馏
6267
// 额外提取三类编码专属记忆:rejected_solution(被否决方案)/ pitfall(踩坑)/
6368
// constraint(工程约束)。蒸馏上下文为整轮完整对话(用户输入 → 助手思考/回答

‎dsh-mneme/src/inject.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createScopeResolver } from "./scope.js";
22
import { createHotMemory } from "./hot-memory.js";
33
import { STR, langOf } from "./lang.js";
4+
import { adaptiveInjectBudget } from "./search/adaptive.js";
45

56
// Best-effort extraction of the current user's latest message text from the
67
// live session, for semantic-first injection (Bug4). The system-prompt
@@ -92,7 +93,7 @@ function extractRounds(ctx, maxRounds) {
9293

9394
export function createInjector(ctx, service, settings, config) {
9495
const language = langOf(config);
95-
const maxItems = config.maxInjectedItems ?? 5;
96+
const baseMaxItems = config.maxInjectedItems ?? 5;
9697
const threshold = config.importanceThreshold ?? 3;
9798
// Issue #205:注入位跨轮轮换。rotationTurns = 最近 N 个「不同用户查询」轮次
9899
// 注入过的记忆本轮不再优先(0 = 关闭,保持既有行为)。历史按会话维护——
@@ -270,6 +271,12 @@ export function createInjector(ctx, service, settings, config) {
270271
const rotate = recentInjectedIds(sessionId, query);
271272
// rotateWindow 随集合一起传入:候选池按 maxItems×(N+1) 扩容——窗口要的
272273
// 牌比既有 maxItems×2 池多时,轮换才有新鲜牌可换(#205 补测)。
274+
// Issue #239(第 5 项):注入条数的查询自适应(默认关)。确定性强的话题收缩
275+
// 条数、模糊话题维持上限——单向收缩,绝不越过 maxInjectedItems;判据只看查询
276+
// 本身,不做额外检索(先探针检索等于白付一次 fuseRecall)。
277+
const maxItems = config.injectUncertaintyAdaptive === true
278+
? adaptiveInjectBudget(query, baseMaxItems)
279+
: baseMaxItems;
273280
const candidates = service.injectCandidates({ query, queryVector, maxItems, threshold, scope, rotate, rotateWindow: rotationTurns });
274281
recordInjection(sessionId, query, candidates);
275282
// Hot memory (v0.5.0 1.3) leads the single memory block: the agent

‎dsh-mneme/src/search/adaptive.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,41 @@ export function adaptiveThreshold(query, candidates = []) {
2020
if (scores.length >= 5 && scores[0] - scores[4] > 0.3) return 0.5;
2121
return 0.65;
2222
}
23+
24+
// Issue #239(第 5 项):注入条数的查询自适应(去 always-on 化的一半)。
25+
// 依据:不确定性强的话题多召回、确定性的少召回(Oblivion 的不确定性驱动读路径);
26+
// 但**注入相关却带偏生成**的内容可能比不注入更糟(2607.24010 与 2604.02280 两处
27+
// 独立出处),所以这里只做**单向收缩**:模糊 → 维持用户配置的上限,确定 → 收缩,
28+
// 绝不越过上限。判据只看查询本身——不做额外检索(2607.24010:触发侧成本别忽略,
29+
// 先探针检索等于白付一次 fuseRecall)。
30+
const RECALL_CUES = [
31+
// 中文回指 / 时间线索:出现即说明用户要的是「记忆里的东西」,当场算不出来
32+
"上次", "之前", "以前", "那天", "那次", "刚才", "刚刚", "后来", "很早",
33+
"还记得", "记不记得", "我们讨论", "我们聊", "我说过", "你提过", "提到过",
34+
"最近", "上次说", "之前说",
35+
// 英文同类线索
36+
"last time", "previously", "earlier", "remember", "we discussed",
37+
"i mentioned", "you said", "that thing", "remind me"
38+
];
39+
40+
/**
41+
* 这段查询是否需要「宽召回」。命中回指/时间线索,或短到几乎匹配任何东西
42+
* (与 adaptiveThreshold 对 <5 字符的判断同源)。纯函数、零成本。
43+
* 拿不到查询时不猜——返回 true 维持现状。
44+
*/
45+
export function needsBroadRecall(query) {
46+
const q = String(query ?? "").trim().toLowerCase();
47+
if (q === "") return true;
48+
if (q.length < 5) return true;
49+
return RECALL_CUES.some((cue) => q.includes(cue));
50+
}
51+
52+
/**
53+
* 注入条数的自适应预算:模糊 → 维持配置上限;确定 → 收缩到一半(下限 1)。
54+
* `maxItems <= 1` 时原样返回(含 0 = 不注入,flag 打开也不该把 0 变成 1)。
55+
*/
56+
export function adaptiveInjectBudget(query, maxItems) {
57+
const cap = Number.isFinite(maxItems) && maxItems > 0 ? Math.floor(maxItems) : maxItems;
58+
if (!Number.isFinite(cap) || cap <= 1) return cap;
59+
return needsBroadRecall(query) ? cap : Math.max(1, Math.ceil(cap / 2));
60+
}

‎dsh-mneme/src/settings.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ const FEATURE_FLAG_BOOLEANS = [
4343
"autoInject",
4444
"autoSummarize",
4545
"hotMemoryEnabled",
46+
// Issue #239 第 5 项:注入条数的查询自适应(确定性强则收缩注入条数,默认关)。
47+
"injectUncertaintyAdaptive",
4648
"entityExtractionEnabled",
4749
// Issue #219:图召回轴——查询命中实体名时把挂联记忆并入检索融合池
4850
// (默认关;依赖实体抽取产出,lightMode 强制关闭)。

0 commit comments

Comments
 (0)