Skip to content

Commit 18ef8d7

Browse files
feat: 编码记忆蒸馏(codingRetrospect) + 智能调速器 + 语义保留优化
借鉴 ai-fu-cn/dsh-plugin-memos-code-retrospect 的三个优化点,为 dsh-mneme 蒸馏与召回链路增加编码记忆支持;同时解决长对话蒸馏的语义丢失与 429 限流。 新功能(codingRetrospect,opt-in 默认关): - 蒸馏上下文扩展:turn/end 蒸馏不再只看用户消息,改为整轮完整对话 (用户输入 → 助手思考/回答 → 工具调用与结果 → 代码执行),便于提炼 踩坑根因 - 编码记忆三类型:rejected_solution(被否决方案) / pitfall(踩坑) / constraint(工程约束),注册进 store TYPES 与 INJECT_TYPES - 门控加权召回:isCodingTask 命中编码关键词时编码记忆才注入(防噪声), 且 importance × codingBoostFactor 加权让编码记忆在编码场景更靠前 语义保留(借鉴 Codex「保留原始、替代压缩摘要」): - 蒸馏不再硬裁 8000 字截断语义,完整转录交给 LLM,上限 distillMaxChars 可配(默认 24000) - SUMMARY_PROMPT/CODING_SUMMARY_PROMPT 从"硬压 2-3 条"改为原子记忆风格: 每条只装一个独立事实、保留原始细节,宁可拆细不合并丢细节 智能调速器(429 保护,默认开): - 蒸馏 LLM 调用进全局串行队列,相邻请求间隔 distillRateLimitIntervalMs 分批放行(单次请求零延迟,仅批量蒸馏触发限速) - 命中 429 指数退避(baseDelay×2^n)自动重试 distillRateLimitRetries 次, 带友好日志,不把 429 错误码抛给用户 Co-Authored-By: Anans-Ivresse <[email protected]>
1 parent 1a0c65f commit 18ef8d7

13 files changed

Lines changed: 664 additions & 138 deletions

File tree

‎dsh-mneme/lib/config.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,39 @@ export const Config = z.object({
99
// active provider/model (same as before).
1010
summarizeProvider: z.string().default(""),
1111
summarizeModel: z.string().default(""),
12+
// 蒸馏转录上限(字符)。借鉴 Codex「保留原始、替代压缩摘要」的思路:
13+
// 蒸馏把完整对话上下文交给 LLM 提炼,不硬裁到 8000 字就截断语义;默认
14+
// 24000 字符(约覆盖一整轮中等对话),需要更完整可调大。
15+
distillMaxChars: z.natural().min(1000).max(200000).default(24000),
16+
// 智能调速器(429 保护,默认开):蒸馏 LLM 调用全局串行排队,相邻请求
17+
// 间隔 distillRateLimitIntervalMs(默认 1s 一次);命中 429 限流时按
18+
// distillRateLimitBaseDelayMs 指数退避(1s→2s→4s…)自动重试
19+
// distillRateLimitRetries 次,全程对用户透明,不把 429 错误码抛给用户。
20+
distillRateLimitIntervalMs: z.natural().min(0).max(60000).default(1000),
21+
distillRateLimitRetries: z.natural().min(0).max(10).default(3),
22+
distillRateLimitBaseDelayMs: z.natural().min(100).max(60000).default(1000),
1223
maxInjectedItems: z.natural().min(1).max(20).default(5),
1324
importanceThreshold: z.natural().min(1).max(5).default(3),
25+
// 编码记忆蒸馏(codingRetrospect,opt-in,默认关)。开启时,turn/end 蒸馏
26+
// 额外提取三类编码专属记忆:rejected_solution(被否决方案)/ pitfall(踩坑)/
27+
// constraint(工程约束)。蒸馏上下文为整轮完整对话(用户输入 → 助手思考/回答
28+
// → 工具调用与结果 → 代码执行),不再只看用户消息,便于提炼踩坑根因。
29+
// 关闭时行为与之前完全一致。
30+
codingRetrospect: z.boolean().default(false),
31+
// 编码任务识别词表(读取侧门控用):命中即视为编码类任务,编码记忆才注入。
32+
codingKeywords: z.array(z.string()).default([
33+
"代码", "编码", "写一个", "写个", "实现", "函数", "方法", "类",
34+
"接口", "bug", "调试", "报错", "错误", "异常", "堆栈", "脚本",
35+
"python", "javascript", "typescript", "node", "js", "ts",
36+
"sql", "sqlite", "数据库", "算法", "重构", "优化", "性能",
37+
"测试", "单测", "修复", "补丁", "依赖", "npm", "pip",
38+
"命令行", "shell", "配置", "配置文件", "yaml", "json",
39+
"插件", "开发", "编译", "构建", "部署", "git", "commit",
40+
"review", "前端", "后端", "页面", "组件", "dsh", "memos"
41+
]),
42+
// 编码记忆注入加权系数:编码任务时对 rejected_solution/pitfall/constraint
43+
// 记忆的 importance 乘以该系数排序,让编码记忆在编码场景更靠前。
44+
codingBoostFactor: z.number().min(1).max(5).default(2),
1445
autoDream: z.boolean().default(true),
1546
dreamThresholdCount: z.natural().min(1).max(1000).default(10),
1647
dreamThresholdChars: z.natural().min(100).max(100000).default(5000),

‎dsh-mneme/lib/quality-filter.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ const TYPE_LABELS = {
3434
decision: ["decision", "决策", "决定"],
3535
history: ["history", "历史", "事件"],
3636
summary: ["summary", "总结", "摘要", "总览"],
37-
pattern: ["pattern", "模式", "规律"]
37+
pattern: ["pattern", "模式", "规律"],
38+
rejected_solution: ["rejected_solution", "被否决", "废弃方案"],
39+
pitfall: ["pitfall", "踩坑"],
40+
constraint: ["constraint", "约束"]
3841
};
3942

4043
/** Normalized bigram-overlap similarity in [0,1]; 0 for tiny/empty inputs. */

‎dsh-mneme/lib/service.js‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ import { evaluateMemoryQuality } from "./quality-filter.js";
44
import { createBM25Index } from "./search/bm25.js";
55
import { adaptiveThreshold } from "./search/adaptive.js";
66

7-
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
7+
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary", "rejected_solution", "pitfall", "constraint"]);
8+
9+
// 编码记忆类型(codingRetrospect):rejected_solution / pitfall / constraint
10+
// 只在编码任务时注入(防噪声污染其他业务),且编码场景下按 codingBoostFactor
11+
// 加权排序提前。
12+
const CODING_MEMORY_TYPES = new Set(["rejected_solution", "pitfall", "constraint"]);
13+
14+
/**
15+
* 判断一段文本是否编码类任务(关键词匹配,codingRetrospect 读取侧门控)。
16+
* 纯函数,无副作用,便于单测。
17+
*/
18+
export function isCodingTask(text, keywords = []) {
19+
const t = String(text ?? "").toLowerCase();
20+
return keywords.some((kw) => t.includes(String(kw).toLowerCase()));
21+
}
822

923
// Epistemic trust weights (v0.4.5): when config.trustEpistemicWeighting is on,
1024
// each recall candidate's existing score is multiplied by the weight of its
@@ -863,17 +877,35 @@ export function createService({ store, mirror, config, onWrite, logger }) {
863877
*/
864878
function injectCandidates({ query = "", maxItems = 5, threshold = 3, queryVector } = {}) {
865879
const q = String(query ?? "").trim();
880+
// codingRetrospect 读取侧门控:编码记忆(rejected_solution / pitfall /
881+
// constraint)只在编码任务时注入,防噪声污染其他业务;编码任务时按
882+
// codingBoostFactor 加权,让编码记忆在编码场景更靠前。
883+
const isCoding = isCodingTask(q, config.codingKeywords ?? []);
884+
const codingGate = (m) => isCoding || !CODING_MEMORY_TYPES.has(m.type);
866885
// Bug7: quality-weighted importance in the rule-based tier. Unassessed rows
867886
// (quality_score null) count as 100 (weight 1), so legacy stores keep their
868887
// exact summary>preference>importance ordering.
869888
const qualityWeight = (m) => (m.quality_score != null ? m.quality_score / 100 : 1);
870889
const items = store.list({ limit: 200, includeForgotten: false })
871890
.filter((m) => !m.archived && INJECT_TYPES.has(m.type) && !m.forgotten &&
891+
codingGate(m) &&
872892
(m.type === "summary" || m.type === "preference" || m.importance >= threshold))
873893
.sort((a, b) => {
874-
const pa = a.type === "summary" ? 0 : a.type === "preference" ? 1 : 2;
875-
const pb = b.type === "summary" ? 0 : b.type === "preference" ? 1 : 2;
876-
return pa - pb || (b.importance * qualityWeight(b)) - (a.importance * qualityWeight(a));
894+
// 编码记忆在编码任务时优先于普通 decision(与 preference 同级),
895+
// importance 乘 codingBoostFactor 加权(封顶 5,保持 importance 语义)。
896+
const priority = (m) => {
897+
if (m.type === "summary") return 0;
898+
if (m.type === "preference") return 1;
899+
if (isCoding && CODING_MEMORY_TYPES.has(m.type)) return 1;
900+
return 2;
901+
};
902+
const effImportance = (m) =>
903+
(isCoding && CODING_MEMORY_TYPES.has(m.type))
904+
? Math.min(5, m.importance * (config.codingBoostFactor ?? 2))
905+
: m.importance;
906+
const pa = priority(a);
907+
const pb = priority(b);
908+
return pa - pb || (effImportance(b) * qualityWeight(b)) - (effImportance(a) * qualityWeight(a));
877909
});
878910
let candidates = items;
879911
if (config.hybridInject !== false && q) {
@@ -888,6 +920,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
888920
const hits = vectorIndex.search(queryVector, { limit: maxItems * 2, threshold: 0 });
889921
for (const m of hits) {
890922
if (m && !m.archived && INJECT_TYPES.has(m.type) && !m.forgotten &&
923+
codingGate(m) &&
891924
(m.type === "summary" || m.type === "preference" || m.importance >= threshold)) {
892925
semanticItems.push(m);
893926
}
@@ -896,7 +929,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
896929
}
897930
if (!semanticItems.length && lastSemanticRecall?.query === q && lastSemanticRecall.items?.length) {
898931
for (const m of lastSemanticRecall.items) {
899-
if (m && !m.archived && INJECT_TYPES.has(m.type) && !m.forgotten) semanticItems.push(m);
932+
if (m && !m.archived && INJECT_TYPES.has(m.type) && !m.forgotten && codingGate(m)) semanticItems.push(m);
900933
}
901934
}
902935
if (semanticItems.length) {

‎dsh-mneme/lib/store.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ CREATE TABLE IF NOT EXISTS mirror_state (
242242
// Exported for API-layer type validation (standalone API POST /memories and
243243
// the /status byType breakdown); the set itself stays the single source of
244244
// truth for what store.save accepts.
245-
export const TYPES = new Set(["preference", "project", "decision", "history", "summary", "pattern"]);
245+
export const TYPES = new Set(["preference", "project", "decision", "history", "summary", "pattern", "rejected_solution", "pitfall", "constraint"]);
246246

247247
// Epistemic status: what kind of evidence a memory rests on. Defaults to
248248
// 'subjective' so legacy rows (and rows without any signal) stay compatible.

0 commit comments

Comments
 (0)