Skip to content

Commit e1e9a94

Browse files
committed
fix(summarize): 缺 setDistillCursor 时降级内存游标而非抛错 (#274)
Anans 复审留下的非阻塞建议第 1 条:第三方宿主用旧版 service 构造时, persistCursor 在方法缺失分支硬抛错,会把 #274 之前能正常工作的宿主 变成每轮蒸馏即崩。降级为 warn + 内存游标(#274 前语义,本进程不重蒸, 跨重启重放由 saveWithDedupe 三元组兜底);方法存在但抛错仍向上传播, 恰一次回滚语义不动(对应回滚测试用例保持原样)。 补回归测试:缺方法时蒸馏照跑、记忆落库、不持久化、进程内不重蒸。 Anans 建议第 2 条(内存优先读)经评估不改:memoryDir 允许双宿主共用, DB 优先是跨进程游标一致的依据,内存优先会复活跨进程重复蒸馏。
1 parent 360f7c8 commit e1e9a94

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎dsh-mneme/lib/summarize.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,12 @@ export function createSummarizer(ctx, service, config, deps = {}) {
408408
function persistCursor(sessionId, nextSeq) {
409409
if (!Number.isFinite(nextSeq)) return;
410410
if (typeof service.setDistillCursor !== "function") {
411-
throw new Error("dsh-mneme: persistent summarization cursors are unavailable");
411+
// 第三方宿主拿旧版 service 构造时没有持久化游标能力:降级为内存游标
412+
// (#274 之前的行为),本进程内不重复蒸馏,重启后窗口重放由
413+
// saveWithDedupe 三元组兜底。方法存在但抛错仍向上传播——那是
414+
// 「写失败须回滚」的恰一次语义,不能吞(见 summarize.test.js 回滚用例)。
415+
ctx.logger?.warn?.("dsh-mneme: service.setDistillCursor unavailable, distill cursor falls back to in-memory");
416+
return;
412417
}
413418
service.setDistillCursor(sessionId, nextSeq);
414419
}

‎dsh-mneme/src/summarize.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,12 @@ export function createSummarizer(ctx, service, config, deps = {}) {
408408
function persistCursor(sessionId, nextSeq) {
409409
if (!Number.isFinite(nextSeq)) return;
410410
if (typeof service.setDistillCursor !== "function") {
411-
throw new Error("dsh-mneme: persistent summarization cursors are unavailable");
411+
// 第三方宿主拿旧版 service 构造时没有持久化游标能力:降级为内存游标
412+
// (#274 之前的行为),本进程内不重复蒸馏,重启后窗口重放由
413+
// saveWithDedupe 三元组兜底。方法存在但抛错仍向上传播——那是
414+
// 「写失败须回滚」的恰一次语义,不能吞(见 summarize.test.js 回滚用例)。
415+
ctx.logger?.warn?.("dsh-mneme: service.setDistillCursor unavailable, distill cursor falls back to in-memory");
416+
return;
412417
}
413418
service.setDistillCursor(sessionId, nextSeq);
414419
}

‎dsh-mneme/test/summarize.test.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,30 @@ test("rolls back memory writes when persisting the cursor fails", async () => {
794794
assert.equal(store.getDistillCursor(session.id).last_seq, 2);
795795
});
796796

797+
test("falls back to the in-memory cursor when service lacks setDistillCursor", async () => {
798+
// 旧版 service(#274 之前)没有持久化游标:缺方法必须降级为内存游标照常蒸馏,
799+
// 而不是抛错打断每轮蒸馏。方法存在但抛错走的是上一条回滚用例,两者不可混淆。
800+
const { events, store, service, calls } = setup(
801+
{ distillRateLimitIntervalMs: 0 },
802+
{ stream: streamOf([{ type: "history", title: "降级游标", content: "缺方法也能蒸馏", importance: 3 }]) }
803+
);
804+
delete service.setDistillCursor; // 模拟旧版 service:实例属性,直接删掉
805+
const handler = events.find((e) => e.name === "session/event").fn;
806+
const session = {
807+
id: "s-no-cursor-api",
808+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
809+
events: [userMessage("旧宿主没有游标接口", 1), { seq: 2, type: "turn/end" }]
810+
};
811+
812+
await handler(session, { seq: 2, type: "turn/end" });
813+
assert.equal(calls.length, 1, "distillation must still run without a persisted cursor");
814+
assert.equal(store.count(), 1, "the memory itself must be saved");
815+
assert.equal(store.getDistillCursor(session.id), undefined, "nothing may be persisted without the API");
816+
817+
await handler(session, { seq: 2, type: "turn/end" });
818+
assert.equal(calls.length, 1, "the in-memory cursor must prevent re-distillation in this process");
819+
});
820+
797821
test("uses summarizeProvider/summarizeModel config override when set", async () => {
798822
const { events, calls } = setup({
799823
summarizeProvider: "aliyun",

0 commit comments

Comments
 (0)