Skip to content

Commit bfef38d

Browse files
authored
Merge pull request #403 from slow-stack/fix/hot-memory-surface-projection
fix(inject): route hot memory and semantic query through the surface projection (#402)
2 parents 9517b95 + 2099858 commit bfef38d

10 files changed

Lines changed: 310 additions & 133 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-3E63DD?style=flat-square" alt="license"></a>
1111
<a href="https://github.com/slow-stack/mneme/actions"><img src="https://img.shields.io/github/actions/workflow/status/slow-stack/mneme/ci.yml?style=flat-square&label=CI" alt="CI"></a>
1212
<a href="https://nodejs.org"><img src="https://img.shields.io/badge/node-22%2B-3E63DD?style=flat-square&logo=nodedotjs&logoColor=white" alt="node"></a>
13-
<a href="https://github.com/slow-stack/mneme"><img src="https://img.shields.io/badge/tests-1602%20passed-3E63DD?style=flat-square" alt="tests"></a>
13+
<a href="https://github.com/slow-stack/mneme"><img src="https://img.shields.io/badge/tests-1609%20passed-3E63DD?style=flat-square" alt="tests"></a>
1414
<a href="https://codecov.io/gh/slow-stack/mneme"><img src="https://img.shields.io/codecov/c/github/slow-stack/mneme/main?style=flat-square" alt="coverage"></a>
1515
<a href="https://github.com/awesome-dsh-plugin/awesome-dsh-plugin"><img src="https://awesome-dsh-plugin.com/badge.svg" alt="Awesome"></a>
1616
</p>
@@ -207,7 +207,7 @@ dsh-mneme-serve # 默认 ~/.dsh/memory + 127.0.0.1:8790
207207

208208
```bash
209209
cd dsh-mneme && npm install
210-
npm test # 1602 个测试
210+
npm test # 1609 个测试
211211
npm run stress # 三轴线压测
212212
npm run sync # src → lib 同步
213213
```
@@ -414,7 +414,7 @@ Community projects that wire mneme into more platforms — not part of this repo
414414

415415
```bash
416416
cd dsh-mneme && npm install
417-
npm test # 1602 tests
417+
npm test # 1609 tests
418418
npm run stress # three-axis stress test
419419
npm run sync # src → lib sync
420420
```

‎dsh-mneme/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![npm version](https://img.shields.io/npm/v/@modusensus/dsh-mneme?color=blue&label=npm)](https://www.npmjs.com/package/@modusensus/dsh-mneme)
66
[![license](https://img.shields.io/badge/license-MIT-green)](LICENSE)
77
[![Awesome](https://awesome-dsh-plugin.com/badge.svg)](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin)
8-
[![tests](https://img.shields.io/badge/tests-1602%20passed-success)](https://github.com/slow-stack/mneme)
8+
[![tests](https://img.shields.io/badge/tests-1609%20passed-success)](https://github.com/slow-stack/mneme)
99
[![CI](https://img.shields.io/github/actions/workflow/status/slow-stack/mneme/ci.yml)](https://github.com/slow-stack/mneme/actions)
1010
[![node](https://img.shields.io/badge/node-22%2B-blue)](https://nodejs.org)
1111
[![npm downloads](https://img.shields.io/npm/d18m/@modusensus/dsh-mneme.svg?color=blue&label=downloads)](https://www.npmjs.com/package/@modusensus/dsh-mneme)
@@ -613,7 +613,7 @@ src/
613613
├── api.js # HTTP 路由(Web 面板数据通道,含 /conflicts 冲突队列)
614614
└── index.js # 插件接线
615615
lib/ # src 的同步分发产物(npm run sync;发布前由 root prepack 的 check-sync.js 校验一致性;唯一手写例外 lib/client.js——Web 面板 bundle,sync 不覆盖)
616-
test/ # 1602 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
616+
test/ # 1609 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
617617
scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压测 · sync-lib.js 同步 · check-sync.js 发布闸门 · benchmark-recall.js / benchmark-embed.js / benchmark-rerank.js 基准 · sync-test-badge.mjs 测试徽章 · build-runtime-manifest.mjs 运行时清单
618618
```
619619
@@ -622,7 +622,7 @@ scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压
622622
```bash
623623
cd dsh-mneme
624624
npm install # 安装 peer 依赖(以 devDependencies 形式,用于本地测试)
625-
npm test # 运行 1602 个测试
625+
npm test # 运行 1609 个测试
626626
npm run stress # 三轴线压测:长会话检索 / 冲突仲裁 / 多 Agent 并发(离线 mock LLM)
627627
npm run sync # 把 src/ 同步到 lib/(发布时由 prepack 钩子自动执行)
628628
```

‎dsh-mneme/lib/hot-memory.js‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { safeSlice } from "./safe-slice.js";
2+
13
// Session-scoped hot memory (v0.5.0 召回率优化 1.3): a short-term buffer of
24
// the latest dialogue rounds, kept strictly apart from the long-term memory
35
// store. The injector renders it ahead of the long-term recall block so the
@@ -44,3 +46,63 @@ export function createHotMemory({ maxRounds = 5, maxTokens = 2000 } = {}) {
4446
clear() { buffer.length = 0; }
4547
};
4648
}
49+
50+
// Issue:rewind(surfaceOp replace)/compact 遮蔽过的事件仍留在 append-only 事件
51+
// 流里——热记忆若直接遍历原始流,被撤回的轮次会回流进 prompt(用户实测:一次
52+
// 撤回 3+ 条后模型仍能复述大部分内容)。宿主在 session.surface.nodes 维护
53+
// 「模型可见事件 seq」列表(dsh-rewind 插件即据此计算替换区间);调用方把可见
54+
// 集合传入本函数,只统计可见事件,热记忆与模型看到的对话因此同源。visibleSeqs
55+
// 传 null 表示宿主未提供 surface(老版本 DSH),保持旧行为——宁可持续旧偏差,
56+
// 绝不误删仍可见的轮次。
57+
//
58+
// 配对规则与 inject.js 原实现一致:user/message(source.kind 缺省或 "user")开启
59+
// 新一轮;下一条 assistant 起源的事件关闭该轮。tool/system 等携带正文的事件不得
60+
// 冒充模型回复;rewind 标记(source.kind = "dsh-rewind")不是用户提问。
61+
export function roundsFromEvents(events, maxRounds, visibleSeqs = null) {
62+
const rounds = [];
63+
let pendingQuery = null;
64+
const textOf = (event) => {
65+
// user/message 的正文就在 data 上,而 assistant/message(以及 tool/result)的消息体
66+
// 嵌在 data.message 下 —— DSH 侧由 @deepseek-ai/dsh-session 的
67+
// assertMessageEventShape 一行写死:const message = type === "user/message" ? record : record?.["message"]。
68+
// 只读 data.content 会让每一轮的 response 恒为空(Issue #129)。
69+
// 这里用 ?? 兜底而不是直接改成 data.message.content:user 消息与旧的扁平形状都照旧可用。
70+
const parts = event?.data?.content ?? event?.data?.message?.content;
71+
if (!Array.isArray(parts)) return "";
72+
// 只取正文 part(type === "text" 或字符串),跳过 reasoning(issue #162):
73+
// reasoning 里出现 Go template / Vue / Handlebars / 正则 / 日志原文的概率远高于
74+
// 正文,把它带进 hot memory 会让 `{{...}}` 模板片段注入 prompt。旧的扁平形状
75+
// (字符串 part)照旧取用,无 type 字段的兜底视为正文。
76+
return parts
77+
.map((p) => {
78+
if (typeof p === "string") return p;
79+
if (p?.type && p.type !== "text") return "";
80+
return p?.text ?? "";
81+
})
82+
.filter(Boolean)
83+
.join("\n")
84+
.trim();
85+
};
86+
for (const event of events) {
87+
// surface 投影:不在「模型可见面」上的事件(被 rewind 撤回、被 compact 替换)
88+
// 不参与配对。
89+
if (visibleSeqs && !visibleSeqs.has(event?.seq)) continue;
90+
const kind = event?.data?.source?.kind;
91+
const isUser = event?.type === "user/message" && (kind === undefined || kind === "user");
92+
if (isUser) {
93+
if (pendingQuery) rounds.push({ query: pendingQuery, response: "" });
94+
pendingQuery = safeSlice(textOf(event), 500);
95+
continue;
96+
}
97+
// Only assistant-originated events close a round; tool/system events
98+
// carrying text must not be mistaken for the model's reply.
99+
const isAssistant = typeof event?.type === "string" && event.type.includes("assistant")
100+
|| kind === "assistant";
101+
const body = isAssistant ? textOf(event) : "";
102+
if (!body || !pendingQuery) continue;
103+
rounds.push({ query: pendingQuery, response: safeSlice(body, 800) });
104+
pendingQuery = null;
105+
}
106+
if (pendingQuery) rounds.push({ query: pendingQuery, response: "" });
107+
return rounds.slice(-maxRounds);
108+
}

‎dsh-mneme/lib/inject.js‎

Lines changed: 27 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { createScopeResolver } from "./scope.js";
2-
import { createHotMemory } from "./hot-memory.js";
2+
import { createHotMemory, roundsFromEvents } from "./hot-memory.js";
33
import { STR, langOf } from "./lang.js";
44
import { MEMORY_GUIDE_SECTION } from "./guide.js";
55
import { adaptiveInjectBudget } from "./search/adaptive.js";
66
import { injectChildEnabled } from "./config.js";
7+
import { safeSlice } from "./safe-slice.js";
78

89
// Issue #179:注入预览的数据底座。systemPrompt 渲染是同步回调,面板只能事后
910
// 拉取,所以在这里旁路缓存「最近一帧组装」——快照就是本次渲染用过的同一份
@@ -16,21 +17,19 @@ export function getInjectionSnapshot() {
1617
return injectionSnapshot;
1718
}
1819

19-
// Issue #334:UTF-16 截断切开 emoji 代理对会留下孤立代理项(如 🔴 = D83D DD34
20-
// 在 800 处切,D83D 留下、DD34 被切走)。DSH 把它序列化成非法 UTF-8,DeepSeek
21-
// API 对每个请求回 400 INVALID_REQUEST——而且畸形文本随注入消息被永久写进会话
22-
// 历史,此后**每一轮**都带着它,表现为会话从某一轮起连续 400、换话题/重启都不
23-
// 恢复(会话级不可逆损坏)。防法是「少一个字符都行,绝不留半个」:尾字符是
24-
// 高位代理(0xD800–0xDBFF)说明它的低位搭档被切走了,把这个高位代理整个丢掉。
25-
// (反过来尾字符是低位代理不可能孤立——它的搭档必然紧邻在前、同在切点之内。)
26-
// ASCII-only 的文本零开销(length 相等直接短路)。
27-
function safeSlice(text, limit) {
28-
const s = String(text);
29-
if (s.length <= limit) return s;
30-
let cut = s.slice(0, limit);
31-
const tail = cut.charCodeAt(cut.length - 1);
32-
if (tail >= 0xd800 && tail <= 0xdbff) cut = cut.slice(0, -1);
33-
return cut;
20+
// 「模型可见事件 seq」集合:宿主在 session.surface.nodes 维护可见面——rewind 的
21+
// surfaceOp replace 与 compact 的隐藏替换都作用在这层(dsh-rewind 插件即据此计算
22+
// 替换区间)。拿不到(老版本宿主)返回 null,调用方退回「全量事件」的旧行为。
23+
// 读取必须在 try 内:`surface.nodes` 是宿主 getter,其 _assertProjections 在消息投影
24+
// 所属插件被移除时会 throw(dsh-session lib/types/surface.js)。抛出等同于「没有
25+
// 可见面」,绝不能让整块热记忆消失——那是比旧偏差更糟的退化。
26+
function visibleSeqsOf(session) {
27+
try {
28+
const nodes = session?.surface?.nodes;
29+
return Array.isArray(nodes) ? new Set(nodes) : null;
30+
} catch {
31+
return null;
32+
}
3433
}
3534

3635
// Best-effort extraction of the current user's latest message text from the
@@ -46,8 +45,11 @@ function lastUserQuery(ctx) {
4645
// still have the .events property, so fall back to it.
4746
const events = session?.snapshotEvents?.() ?? session?.events;
4847
if (!Array.isArray(events) || events.length === 0) return "";
48+
const visibleSeqs = visibleSeqsOf(session);
4949
for (let i = events.length - 1; i >= 0; i--) {
5050
const event = events[i];
51+
// 被撤回/被压缩遮蔽的事件不算「最近一条用户消息」(与 extractRounds 同口径)。
52+
if (visibleSeqs && !visibleSeqs.has(event?.seq)) continue;
5153
if (event?.type !== "user/message") continue;
5254
const kind = event.data?.source?.kind;
5355
if (kind !== undefined && kind !== "user") continue;
@@ -66,58 +68,20 @@ function lastUserQuery(ctx) {
6668
}
6769

6870
// Hot-memory round extraction (v0.5.0 1.3): pairs each user/message with the
69-
// next assistant reply from the materialized session log. Tolerates shapes
70-
// where assistant events carry a different type tag — anything whose payload
71-
// has content parts and is not a user message counts as a reply. Best-effort:
72-
// returns [] on any failure, and the hot block simply does not render.
71+
// next assistant reply. The pairing lives in hot-memory.js (roundsFromEvents)
72+
// so it can be unit-tested directly; this adapter only resolves the session's
73+
// raw event log and the model-visible seq set (session.surface.nodes — see
74+
// visibleSeqsOf). Rewind-shadowed / compacted events are thereby excluded:
75+
// hot memory and the model see the same conversation. Best-effort: returns []
76+
// on any failure, and the hot block simply does not render.
7377
function extractRounds(ctx, maxRounds) {
7478
try {
7579
const session = ctx?.agent?.session;
80+
// DSH ≥0.1.2-rc only exposes events via snapshotEvents(); older builds
81+
// still have the .events property, so fall back to it.
7682
const events = session?.snapshotEvents?.() ?? session?.events;
7783
if (!Array.isArray(events) || events.length === 0) return [];
78-
const rounds = [];
79-
let pendingQuery = null;
80-
const textOf = (event) => {
81-
// user/message 的正文就在 data 上,而 assistant/message(以及 tool/result)的消息体
82-
// 嵌在 data.message 下 —— DSH 侧由 @deepseek-ai/dsh-session 的
83-
// assertMessageEventShape 一行写死:const message = type === "user/message" ? record : record?.["message"]。
84-
// 只读 data.content 会让每一轮的 response 恒为空(Issue #129)。
85-
// 这里用 ?? 兜底而不是直接改成 data.message.content:user 消息与旧的扁平形状都照旧可用。
86-
const parts = event?.data?.content ?? event?.data?.message?.content;
87-
if (!Array.isArray(parts)) return "";
88-
// 只取正文 part(type === "text" 或字符串),跳过 reasoning(issue #162):
89-
// reasoning 里出现 Go template / Vue / Handlebars / 正则 / 日志原文的概率远高于
90-
// 正文,把它带进 hot memory 会让 `{{...}}` 模板片段注入 prompt。旧的扁平形状
91-
// (字符串 part)照旧取用,无 type 字段的兜底视为正文。
92-
return parts
93-
.map((p) => {
94-
if (typeof p === "string") return p;
95-
if (p?.type && p.type !== "text") return "";
96-
return p?.text ?? "";
97-
})
98-
.filter(Boolean)
99-
.join("\n")
100-
.trim();
101-
};
102-
for (const event of events) {
103-
const kind = event?.data?.source?.kind;
104-
const isUser = event?.type === "user/message" && (kind === undefined || kind === "user");
105-
if (isUser) {
106-
if (pendingQuery) rounds.push({ query: pendingQuery, response: "" });
107-
pendingQuery = safeSlice(textOf(event), 500);
108-
continue;
109-
}
110-
// Only assistant-originated events close a round; tool/system events
111-
// carrying text must not be mistaken for the model's reply.
112-
const isAssistant = typeof event?.type === "string" && event.type.includes("assistant")
113-
|| kind === "assistant";
114-
const body = isAssistant ? textOf(event) : "";
115-
if (!body || !pendingQuery) continue;
116-
rounds.push({ query: pendingQuery, response: safeSlice(body, 800) });
117-
pendingQuery = null;
118-
}
119-
if (pendingQuery) rounds.push({ query: pendingQuery, response: "" });
120-
return rounds.slice(-maxRounds);
84+
return roundsFromEvents(events, maxRounds, visibleSeqsOf(session));
12185
} catch {
12286
return [];
12387
}

‎dsh-mneme/lib/safe-slice.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Issue #334:UTF-16 截断切开 emoji 代理对会留下孤立代理项(如 🔴 = D83D DD34
2+
// 在 800 处切,D83D 留下、DD34 被切走)。DSH 把它序列化成非法 UTF-8,DeepSeek
3+
// API 对每个请求回 400 INVALID_REQUEST——而且畸形文本随注入消息被永久写进会话
4+
// 历史,此后**每一轮**都带着它,表现为会话从某一轮起连续 400、换话题/重启都不
5+
// 恢复(会话级不可逆损坏)。防法是「少一个字符都行,绝不留半个」:尾字符是
6+
// 高位代理(0xD800–0xDBFF)说明它的低位搭档被切走了,把这个高位代理整个丢掉。
7+
// (反过来尾字符是低位代理不可能孤立——它的搭档必然紧邻在前、同在切点之内。)
8+
// ASCII-only 的文本零开销(length 相等直接短路)。
9+
// (原为 inject.js 模块私有;热记忆的轮次抽取同样要做正文截断,收拢成共享模块
10+
// 以免两份实现漂移。)
11+
export function safeSlice(text, limit) {
12+
const s = String(text);
13+
if (s.length <= limit) return s;
14+
let cut = s.slice(0, limit);
15+
const tail = cut.charCodeAt(cut.length - 1);
16+
if (tail >= 0xd800 && tail <= 0xdbff) cut = cut.slice(0, -1);
17+
return cut;
18+
}

‎dsh-mneme/src/hot-memory.js‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { safeSlice } from "./safe-slice.js";
2+
13
// Session-scoped hot memory (v0.5.0 召回率优化 1.3): a short-term buffer of
24
// the latest dialogue rounds, kept strictly apart from the long-term memory
35
// store. The injector renders it ahead of the long-term recall block so the
@@ -44,3 +46,63 @@ export function createHotMemory({ maxRounds = 5, maxTokens = 2000 } = {}) {
4446
clear() { buffer.length = 0; }
4547
};
4648
}
49+
50+
// Issue:rewind(surfaceOp replace)/compact 遮蔽过的事件仍留在 append-only 事件
51+
// 流里——热记忆若直接遍历原始流,被撤回的轮次会回流进 prompt(用户实测:一次
52+
// 撤回 3+ 条后模型仍能复述大部分内容)。宿主在 session.surface.nodes 维护
53+
// 「模型可见事件 seq」列表(dsh-rewind 插件即据此计算替换区间);调用方把可见
54+
// 集合传入本函数,只统计可见事件,热记忆与模型看到的对话因此同源。visibleSeqs
55+
// 传 null 表示宿主未提供 surface(老版本 DSH),保持旧行为——宁可持续旧偏差,
56+
// 绝不误删仍可见的轮次。
57+
//
58+
// 配对规则与 inject.js 原实现一致:user/message(source.kind 缺省或 "user")开启
59+
// 新一轮;下一条 assistant 起源的事件关闭该轮。tool/system 等携带正文的事件不得
60+
// 冒充模型回复;rewind 标记(source.kind = "dsh-rewind")不是用户提问。
61+
export function roundsFromEvents(events, maxRounds, visibleSeqs = null) {
62+
const rounds = [];
63+
let pendingQuery = null;
64+
const textOf = (event) => {
65+
// user/message 的正文就在 data 上,而 assistant/message(以及 tool/result)的消息体
66+
// 嵌在 data.message 下 —— DSH 侧由 @deepseek-ai/dsh-session 的
67+
// assertMessageEventShape 一行写死:const message = type === "user/message" ? record : record?.["message"]。
68+
// 只读 data.content 会让每一轮的 response 恒为空(Issue #129)。
69+
// 这里用 ?? 兜底而不是直接改成 data.message.content:user 消息与旧的扁平形状都照旧可用。
70+
const parts = event?.data?.content ?? event?.data?.message?.content;
71+
if (!Array.isArray(parts)) return "";
72+
// 只取正文 part(type === "text" 或字符串),跳过 reasoning(issue #162):
73+
// reasoning 里出现 Go template / Vue / Handlebars / 正则 / 日志原文的概率远高于
74+
// 正文,把它带进 hot memory 会让 `{{...}}` 模板片段注入 prompt。旧的扁平形状
75+
// (字符串 part)照旧取用,无 type 字段的兜底视为正文。
76+
return parts
77+
.map((p) => {
78+
if (typeof p === "string") return p;
79+
if (p?.type && p.type !== "text") return "";
80+
return p?.text ?? "";
81+
})
82+
.filter(Boolean)
83+
.join("\n")
84+
.trim();
85+
};
86+
for (const event of events) {
87+
// surface 投影:不在「模型可见面」上的事件(被 rewind 撤回、被 compact 替换)
88+
// 不参与配对。
89+
if (visibleSeqs && !visibleSeqs.has(event?.seq)) continue;
90+
const kind = event?.data?.source?.kind;
91+
const isUser = event?.type === "user/message" && (kind === undefined || kind === "user");
92+
if (isUser) {
93+
if (pendingQuery) rounds.push({ query: pendingQuery, response: "" });
94+
pendingQuery = safeSlice(textOf(event), 500);
95+
continue;
96+
}
97+
// Only assistant-originated events close a round; tool/system events
98+
// carrying text must not be mistaken for the model's reply.
99+
const isAssistant = typeof event?.type === "string" && event.type.includes("assistant")
100+
|| kind === "assistant";
101+
const body = isAssistant ? textOf(event) : "";
102+
if (!body || !pendingQuery) continue;
103+
rounds.push({ query: pendingQuery, response: safeSlice(body, 800) });
104+
pendingQuery = null;
105+
}
106+
if (pendingQuery) rounds.push({ query: pendingQuery, response: "" });
107+
return rounds.slice(-maxRounds);
108+
}

0 commit comments

Comments
 (0)