Skip to content

Commit 536638c

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 cb5ec63 commit 536638c

13 files changed

Lines changed: 660 additions & 124 deletions

File tree

‎dsh-mneme/lib/client.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,10 @@ window.__ModuleLoader__.load({
601601
summary: "#a855f7",
602602
user: "#f59e0b",
603603
fact: "#22c55e",
604-
history: "#94a3b8"
604+
history: "#94a3b8",
605+
rejected_solution: "#ef4444",
606+
pitfall: "#f97316",
607+
constraint: "#06b6d4"
605608
};
606609
function memoryTypeColor(type) {
607610
return MEMORY_TYPE_COLORS[type] || "#94a3b8";
@@ -1610,7 +1613,7 @@ window.__ModuleLoader__.load({
16101613
// bar chart and a 7-day creation trend. Data comes from /stats; each tile
16111614
// probes /list?type=X&limit=1 for a representative latest title (the list
16121615
// API sorts importance-first, so limit=1 is the type's most prominent row).
1613-
const OVERVIEW_TYPE_ORDER = ["preference", "project", "decision", "history", "user", "fact", "summary", "pattern"];
1616+
const OVERVIEW_TYPE_ORDER = ["preference", "project", "decision", "history", "user", "fact", "summary", "pattern", "rejected_solution", "pitfall", "constraint"];
16141617
// Injection-priority tiers from service.js (summary > preference/user >
16151618
// importance): these layers ride into nearly every prompt.
16161619
const OVERVIEW_INJECT_TYPES = ["summary", "user", "preference"];
@@ -1733,7 +1736,7 @@ window.__ModuleLoader__.load({
17331736
);
17341737
}
17351738

1736-
const EXPLORER_TYPES = ["preference", "project", "decision", "summary", "history", "user", "fact"];
1739+
const EXPLORER_TYPES = ["preference", "project", "decision", "summary", "history", "user", "fact", "rejected_solution", "pitfall", "constraint"];
17371740

17381741
function MemoryExplorer({ t }) {
17391742
const [view, setView] = useState("memory"); // memory | overview | directory | graph | settings

‎dsh-mneme/lib/config.js‎

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ const TYPE_LABELS = {
3636
summary: ["summary", "总结", "摘要", "总览"],
3737
pattern: ["pattern", "模式", "规律"],
3838
user: ["user", "用户"],
39-
fact: ["fact", "事实", "原子事实"]
39+
fact: ["fact", "事实", "原子事实"],
40+
rejected_solution: ["rejected_solution", "被否决", "废弃方案"],
41+
pitfall: ["pitfall", "踩坑"],
42+
constraint: ["constraint", "约束"]
4043
};
4144

4245
/** 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
@@ -7,7 +7,21 @@ import { parseWikiLinks } from "./parser/wiki-link.js";
77
import { extractQueryTags, applyTagBoost } from "./search/tag-boost.js";
88
import { computeHeat } from "./heat.js";
99

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

1226
// Epistemic trust weights (v0.4.5): when config.trustEpistemicWeighting is on,
1327
// each recall candidate's existing score is multiplied by the weight of its
@@ -1043,6 +1057,11 @@ export function createService({ store, mirror, config, onWrite, logger, settings
10431057
*/
10441058
function injectCandidates({ query = "", maxItems = 5, threshold = 3, queryVector } = {}) {
10451059
const q = String(query ?? "").trim();
1060+
// v0.7.11 codingRetrospect 读取侧门控:编码记忆(rejected_solution / pitfall /
1061+
// constraint)只在编码任务时注入,防噪声污染其他业务;编码任务时按
1062+
// codingBoostFactor 加权,让编码记忆在编码场景更靠前。
1063+
const isCoding = isCodingTask(q, config.codingKeywords ?? []);
1064+
const codingGate = (m) => isCoding || !CODING_MEMORY_TYPES.has(m.type);
10461065
// Bug7: quality-weighted importance in the rule-based tier. Unassessed rows
10471066
// (quality_score null) count as 100 (weight 1), so legacy stores keep their
10481067
// exact summary>preference>importance ordering.
@@ -1052,11 +1071,24 @@ export function createService({ store, mirror, config, onWrite, logger, settings
10521071
// importance >= threshold — the frontend "常注入" badge maps to this split.
10531072
const items = store.list({ limit: 200, includeForgotten: false })
10541073
.filter((m) => !m.archived && INJECT_TYPES.has(m.type) && !m.forgotten &&
1074+
codingGate(m) &&
10551075
(m.type === "summary" || m.type === "preference" || m.type === "user" || m.importance >= threshold))
10561076
.sort((a, b) => {
1057-
const pa = a.type === "summary" ? 0 : (a.type === "preference" || a.type === "user") ? 1 : 2;
1058-
const pb = b.type === "summary" ? 0 : (b.type === "preference" || b.type === "user") ? 1 : 2;
1059-
return pa - pb || (b.importance * qualityWeight(b)) - (a.importance * qualityWeight(a));
1077+
// 编码记忆在编码任务时优先于普通 decision/fact(与 preference/user 同级),
1078+
// importance 乘 codingBoostFactor 加权(封顶 5,保持 importance 语义)。
1079+
const priority = (m) => {
1080+
if (m.type === "summary") return 0;
1081+
if (m.type === "preference" || m.type === "user") return 1;
1082+
if (isCoding && CODING_MEMORY_TYPES.has(m.type)) return 1;
1083+
return 2;
1084+
};
1085+
const effImportance = (m) =>
1086+
(isCoding && CODING_MEMORY_TYPES.has(m.type))
1087+
? Math.min(5, m.importance * (config.codingBoostFactor ?? 2))
1088+
: m.importance;
1089+
const pa = priority(a);
1090+
const pb = priority(b);
1091+
return pa - pb || (effImportance(b) * qualityWeight(b)) - (effImportance(a) * qualityWeight(a));
10601092
});
10611093
let candidates = items;
10621094
if (config.hybridInject !== false && q) {
@@ -1071,6 +1103,7 @@ export function createService({ store, mirror, config, onWrite, logger, settings
10711103
const hits = vectorIndex.search(queryVector, { limit: maxItems * 2, threshold: 0 });
10721104
for (const m of hits) {
10731105
if (m && !m.archived && INJECT_TYPES.has(m.type) && !m.forgotten &&
1106+
codingGate(m) &&
10741107
(m.type === "summary" || m.type === "preference" || m.type === "user" || m.importance >= threshold)) {
10751108
semanticItems.push(m);
10761109
}
@@ -1079,7 +1112,7 @@ export function createService({ store, mirror, config, onWrite, logger, settings
10791112
}
10801113
if (!semanticItems.length && lastSemanticRecall?.query === q && lastSemanticRecall.items?.length) {
10811114
for (const m of lastSemanticRecall.items) {
1082-
if (m && !m.archived && INJECT_TYPES.has(m.type) && !m.forgotten) semanticItems.push(m);
1115+
if (m && !m.archived && INJECT_TYPES.has(m.type) && !m.forgotten && codingGate(m)) semanticItems.push(m);
10831116
}
10841117
}
10851118
if (semanticItems.length) {

‎dsh-mneme/lib/store.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ CREATE TABLE IF NOT EXISTS mirror_state (
243243
);
244244
`;
245245

246-
const TYPES = new Set(["preference", "project", "decision", "history", "summary", "pattern", "user", "fact"]);
246+
const TYPES = new Set(["preference", "project", "decision", "history", "summary", "pattern", "user", "fact", "rejected_solution", "pitfall", "constraint"]);
247247

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

0 commit comments

Comments
 (0)