Skip to content

Commit 246867a

Browse files
committed
fix(summarize): 图片块不再静默丢,三份 textOf 收口成一份占位抽取器
会话里的图片此前从入口到落库整条断掉:三份 textOf(summarize / continuity / hot-memory)都只认 type:"text",image 块一律返回空串。后果是图-only 消息整条 不进转录(窗口里只有图时连蒸馏都不触发),图+文消息只剩文字,而且两侧都没有 任何计数,事后无法回答「图到底进来没有」。 把「块 → 文本」抽成唯一一份 src/attachment-text.js,三处调用方共用:抽一份而不是 各加一遍 image 分支,因为同一件事写成三份迟早漂移,而这次漂移的代价恰恰是 「一处记得图、另一处忘了」。 占位口径 [图: 名称 mediaType 宽x高]:有名字用名字,没名字退 mediaType, 什么都没有也落一个裸 [图]——绝不返回空串,返回空串正是要修的 bug。宽高留着, 因为它是唯一能区分同一张图不同版本的线索。 collectMessages 逐事件数 imageCount(user 的 data.content、assistant 的 data.message.content、子会话 data.inserted[].content),写进 llm_audit_logs 的 metadata,四种审计行都带;没有图时不写这个键,老档位 metadata 逐字节不变。 不做 vision / 读字节 / 落原图;描述档是下一批,需要单独一个 provider 槽。 Refs #611
1 parent 7461768 commit 246867a

14 files changed

Lines changed: 552 additions & 51 deletions

‎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-2881%20total-3E63DD?style=flat-square" alt="tests"></a>
13+
<a href="https://github.com/slow-stack/mneme"><img src="https://img.shields.io/badge/tests-2915%20total-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>
@@ -276,7 +276,7 @@ dsh-mneme-serve # 默认 ~/.dsh/memory + 127.0.0.1:8790
276276

277277
```bash
278278
cd dsh-mneme && npm install
279-
npm test # 2881 个测试
279+
npm test # 2915 个测试
280280
npm run stress # 三轴线压测
281281
npm run sync # src → lib 同步
282282
```
@@ -562,7 +562,7 @@ Community projects that wire mneme into more platforms — not part of this repo
562562

563563
```bash
564564
cd dsh-mneme && npm install
565-
npm test # 2881 tests
565+
npm test # 2915 tests
566566
npm run stress # three-axis stress test
567567
npm run sync # src → lib sync
568568
```

‎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-2881%20total-success)](https://github.com/slow-stack/mneme)
8+
[![tests](https://img.shields.io/badge/tests-2915%20total-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)
@@ -733,7 +733,7 @@ src/
733733
├── api.js # HTTP 路由(Web 面板数据通道,含 /conflicts 冲突队列)
734734
└── index.js # 插件接线
735735
lib/ # src 的同步分发产物(npm run sync,prepack 钩子在发布时自动跑;一致性由 scripts/check-sync.js 在 CI 与 release.yml 校验——它只查不同步;唯一手写例外 lib/client.js——Web 面板 bundle,sync 不覆盖)
736-
test/ # 2881 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
736+
test/ # 2915 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
737737
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 运行时清单
738738
```
739739
@@ -742,7 +742,7 @@ scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压
742742
```bash
743743
cd dsh-mneme
744744
npm install # 安装 peer 依赖(以 devDependencies 形式,用于本地测试)
745-
npm test # 运行 2881 个测试
745+
npm test # 运行 2915 个测试
746746
npm run stress # 三轴线压测:长会话检索 / 冲突仲裁 / 多 Agent 并发(离线 mock LLM)
747747
npm run sync # 把 src/ 同步到 lib/(发布时由 prepack 钩子自动执行)
748748
```

‎dsh-mneme/docs/DISTILL.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ UNIQUE idx_memory_provenance_link(memory_id, session_id, seq_start, seq_end)
6767
|---|---|---|
6868
| 真实 `user/message`(`kind` 缺失 / `user` / `agent-message` / `subagent-settled`) | ✅ | ✅ |
6969
| 助手公开 `text` 块 | ✅ | ✅ |
70+
| 消息里的 `image` 块(#611) | ✅(只留 `[图: 名称 宽x高]` 占位) | ✅(同占位,不含字节) |
7071
| 助手 `reasoning` 块(私有推理链) | ❌ | ❌ |
7172
| 插件注入机器内容(`kind: "plugin:dsh-mneme"` / skill 正文 / AGENTS.md / 文件变更通知) | ❌ | ❌ |
7273
| 工具调用入参、工具结果、code 输出 | ✅(截断) | ❌ |
@@ -306,3 +307,43 @@ audit 行,归因会跟着悬空;而 `memory_provenance` 刻意活得比原
306307
`distill_run_id / run_type / trigger_source / run_status`(读不到整组 null),答复级另带
307308
`mints[]`(`{run_id, run_type, trigger_source, created_at}`)。面板详情抽屉有一个
308309
「蒸发归因」区块渲染这两层。
310+
311+
## 12. 图片块:占位而不是丢(#611)
312+
313+
在 #611 之前,会话里的图片是**从入口到落库整条断掉的**:三份 `textOf` 的实现都是
314+
`p?.type && p.type !== "text"` → 空串,于是
315+
316+
- 图-only 消息在 `collectMessages` 里读出一串空文本,`publicTranscriptParts` 返回 `[]`,
317+
这一条整条不进转录;窗口里如果只有图,连蒸馏都不会触发(`collected.messages.length === 0`
318+
直接 `commitCursor` 走人);
319+
- 图+文消息只剩文字,图那部分静默消失;
320+
- 两侧都没有任何计数,事后没法回答「图到底进来没有」,只能翻源码。
321+
322+
本批的修法是把「块 → 文本」抽成唯一一份 `src/attachment-text.js`
323+
(`blockToText` / `contentToText` / `countImages` / `imageLabel`),三处调用方共用:
324+
`src/summarize.js` 的 `textOf`、`src/continuity.js` 的快照抽取、`src/hot-memory.js`
325+
的 `roundsFromEvents`。抽一份而不是各加一遍 image 分支,理由和 §4 那条一样——同一件事
326+
写成三份,迟早会漂移,而这次漂移的代价恰恰是「一处记得图、另一处忘了」。
327+
328+
**占位口径**:`[图: 屏幕截图 2026-10-19 072000.png image/png 1543x983]`。有名字用名字,
329+
没名字退 `mediaType`,什么都没有也要落一个裸 `[图]`——**绝不返回空串**,返回空串正是
330+
本次要修的那个 bug。宽高留着是因为它是唯一能区分「同一张图的不同版本」的线索。
331+
332+
**不做(本批)**:不调 vision、不读字节、不落原图。宿主侧其实齐全
333+
(`attachments.readImage()` / `supportsAcpImagePrompts()` 判 `inputModalities`),
334+
但记忆库已 273MB,原图档明确不做;描述档(多模态模型出 1 段描述 + attachmentId 溯源)
335+
是下一批,需要单独一个 provider 槽,不占蒸馏那个。
336+
337+
**观测**:`collectMessages` 逐事件数 `imageCount`(user 的 `data.content`、
338+
assistant 的 `data.message.content`、子会话 `data.inserted[].content` 三处),随返回值
339+
上浮,写进 `llm_audit_logs.metadata.imageCount`——四种审计行(skip / 失败 / 成功)都带。
340+
**没有图时不写这个键**,老档位 metadata 逐字节不变。
341+
342+
**验收**:`test/attachment-text.test.js` 9 例锁口径(占位非空、三级退化、宽松字符串形状
343+
逐字节不变、reasoning 仍被挡、图+文顺序、countImages);
344+
`test/image-memory.test.js` 4 例锁端到端(图+文进转录、图-only 现在也会蒸馏、
345+
两张图数出 2、无图不写 imageCount)。变异自证:把 `blockToText` 的 image 分支改回
346+
`return ""` → 图-only 那条立刻红(`calls.length` 0 ≠ 1),恢复后 46/46 绿。
347+
`test/continuity.test.js` 那条静态 import 锁同步从「恰好一行」改成「恰好两行」——
348+
新增一行仍然转红,仍然要人看一眼它是不是纯的(`attachment-text.js` 不 import 任何东西,
349+
所以它不调模型这条约束没被削弱)。

‎dsh-mneme/lib/attachment-text.js‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// 图块抽取:会话消息里的 `type:"image"` 块到纯文本占位(Issue #611)。
2+
//
3+
// 为什么单独一个模块:在本次改动之前,`textOf` 的实现在 summarize / continuity /
4+
// hot-memory 三处各有一份副本(各写一份的理由不同,注释里都写了)。三份都是
5+
// `p?.type && p.type !== "text"` → 空串,于是图片一律被丢。给三份各加一遍 image
6+
// 分支,等于给同一件事写三份实现——而这三份本来就该同一个口径。所以为 image
7+
// 单独抽一个**只做一件事**的模块:给定一个 content 块,回答它的可读文本。
8+
//
9+
// 口径(维护者 2026-11-08 定,先只做 off 档):
10+
// - 不调 vision、不读字节、不落原图。宿主有 `attachments.readImage()`,但记忆库
11+
// 已 273MB,原图档明确不做。
12+
// - 占位形如 `[图: 屏幕截图 2026-10-19.png]`,带文件名与宽高,够人回看时定位;
13+
// 拿不到名字时退化成 `[图: image/png]`、再退化成 `[图]`,**绝不返回空串**——
14+
// 返回空串正是本次要修的那个 bug(图-only 消息整条被丢,静默)。
15+
// - 这里只回答「这一块读出什么文本」,不回答「这个模型能不能看图」。后者要看
16+
// `llm.resolveModelInfo(...).inputModalities`,属于描述档的准入判断,本批不做。
17+
18+
/** 图片块的占位前缀:审计与测试都按它计数。 */
19+
export const IMAGE_PLACEHOLDER_PREFIX = "[图";
20+
21+
/** 一个图片块读出的人类可读标签(不含方括号)。什么元数据都没有时返回空串。 */
22+
export function imageLabel(block) {
23+
const attachment = block?.attachment ?? {};
24+
const name = typeof attachment.name === "string" ? attachment.name.trim() : "";
25+
const mediaType = typeof attachment.mediaType === "string" ? attachment.mediaType : "";
26+
const width = Number.isFinite(attachment.width) ? attachment.width : undefined;
27+
const height = Number.isFinite(attachment.height) ? attachment.height : undefined;
28+
const size = width !== undefined && height !== undefined ? `${width}x${height}` : "";
29+
// 没有名字也没有 mediaType 时返回空串,由调用方退化成裸 `[图]`——宁可写「有一张图」,
30+
// 也不要写「[图: image]」这种把类型名当名字的假标签。
31+
const head = name || mediaType;
32+
if (!head) return "";
33+
const tail = [mediaType && name ? mediaType : "", size].filter(Boolean).join(" ");
34+
return tail ? `${head} ${tail}` : head;
35+
}
36+
37+
/**
38+
* 把一个 content 块读成文本。
39+
*
40+
* @param {unknown} block content 数组里的一项
41+
* @param {object} [options]
42+
* @param {boolean} [options.imagePlaceholder=true] false 时图片块返回空串(旧行为,
43+
* 留给需要严格「只要文字」的调用方,例如把同一批内容喂给不支持图的模型)。
44+
* @returns {string}
45+
*/
46+
export function blockToText(block, options = {}) {
47+
if (typeof block === "string") return block;
48+
if (!block || typeof block !== "object") return "";
49+
if (block.type === "image") {
50+
if (options.imagePlaceholder === false) return "";
51+
const label = imageLabel(block);
52+
return label ? `${IMAGE_PLACEHOLDER_PREFIX}: ${label}]` : `${IMAGE_PLACEHOLDER_PREFIX}]`;
53+
}
54+
if (block.type === undefined || block.type === "text") {
55+
return typeof block.text === "string" ? block.text : "";
56+
}
57+
// reasoning 等私有块:刻意不采集(各调用方的注释都写了理由,这里不重复)。
58+
return "";
59+
}
60+
61+
/**
62+
* 把一个 content 数组读成多行文本。字符串数组(宽松形状)与严格块数组都认。
63+
*
64+
* @param {unknown} content
65+
* @param {object} [options] 透传给 blockToText
66+
* @returns {string}
67+
*/
68+
export function contentToText(content, options = {}) {
69+
if (typeof content === "string") return content;
70+
if (!Array.isArray(content)) return "";
71+
return content
72+
.map((block) => blockToText(block, options))
73+
.filter((s) => s)
74+
.join("\n");
75+
}
76+
77+
/**
78+
* 数一段 content 里有几个图片块(审计用)。
79+
*
80+
* 为什么单独一个函数而不是在 contentToText 里顺带返回:调用方要的是「这一窗有多少
81+
* 张图」这个数,而 contentToText 的返回值是字符串,签名不想为一个计量改成对象。
82+
*
83+
* @param {unknown} content
84+
* @returns {number}
85+
*/
86+
export function countImages(content) {
87+
if (!Array.isArray(content)) return 0;
88+
let n = 0;
89+
for (const block of content) {
90+
if (block && typeof block === "object" && block.type === "image") n += 1;
91+
}
92+
return n;
93+
}

‎dsh-mneme/lib/continuity.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { createUserMessage } from "@deepseek-ai/dsh-llm";
2+
// Issue #611:图片块的文本口径(占位,不是空串),与 summarize.js 共用一份。
3+
import { contentToText } from "./attachment-text.js";
24

35
// #249 N3(压缩边缘双落点):上下文即将大幅精简前,抢救「正在做什么」。
46
//
@@ -72,13 +74,12 @@ function isCompactionEdge(event) {
7274
// 与 summarize.js 的 collectMessages 同一个口径:只取公开文本块,reasoning 私有推理
7375
// 不进快照(快照可能被宿主重建进摘要)。刻意副本而不是导出——那边是会话蒸馏的内部
7476
// 细节,两边取文本的理由不同。
77+
//
78+
// Issue #611:图片块不再返回空串,改走 attachment-text 的占位口径。快照的全部意义
79+
// 就是「压缩后还能看出刚才在干什么」,而一张没被记下的截图恰恰是最容易丢的那部分
80+
// 上下文——用户在等你看图,压缩后却只剩一句「看这个」,这种快照比没有更误导。
7581
function textOf(content) {
76-
if (typeof content === "string") return content;
77-
if (!Array.isArray(content)) return "";
78-
return content
79-
.map((block) => (typeof block === "string" ? block : (block?.type === "text" && typeof block.text === "string" ? block.text : "")))
80-
.filter((s) => s)
81-
.join("\n");
82+
return contentToText(content);
8283
}
8384

8485
// DSH 0.1.2-rc.1 起 Session 用 snapshotEvents(),兼容旧版 .events。这是**全量日志**

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { safeSlice } from "./safe-slice.js";
2+
// Issue #611:图片块改用占位文本,不再落进空串(见 roundsFromEvents 里的注释)。
3+
import { contentToText } from "./attachment-text.js";
24

35
// Session-scoped hot memory (v0.5.0 召回率优化 1.3): a short-term buffer of
46
// the latest dialogue rounds, kept strictly apart from the long-term memory
@@ -73,15 +75,12 @@ export function roundsFromEvents(events, maxRounds, visibleSeqs = null) {
7375
// reasoning 里出现 Go template / Vue / Handlebars / 正则 / 日志原文的概率远高于
7476
// 正文,把它带进 hot memory 会让 `{{...}}` 模板片段注入 prompt。旧的扁平形状
7577
// (字符串 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();
78+
//
79+
// Issue #611:图片 part 原先在这里也落进空串分支,于是「用户发了截图」这件事
80+
// 在 hot memory 里完全消失。改走 attachment-text 的占位口径:`[图: 名称 宽x高]`。
81+
// hot memory 每轮都进 prompt,占位刻意保持短——它不是描述,只是「这里有过一张
82+
// 图」的坐标。
83+
return contentToText(parts).trim();
8584
};
8685
for (const event of events) {
8786
// surface 投影:不在「模型可见面」上的事件(被 rewind 撤回、被 compact 替换)

0 commit comments

Comments
 (0)