Skip to content

Commit 769f567

Browse files
committed
fix(inject): 回应 #335 review 两条 Minor
1. lastUserQuery 移除预截断:先 slice(0,500) 再 safeSlice 时,恰好 500 长的串会绕过孤立代理项检查(length<=limit 早退),畸形 query 流向 embedQuery/轮换记录/injectionSnapshot。改为全文直接 safeSlice; 补快照 query 干净性断言。 2. 时间前缀闩锁改按会话集合:单值闩锁在 A→B→A 交替渲染下会让 A 重复注入。改 Set(上限 500 逐出最早,防长驻进程无界增长);补 A-B-A 用例,变异阴性对照红。 全量 1431/1430/0。
1 parent 284f0a7 commit 769f567

5 files changed

Lines changed: 52 additions & 22 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-1429%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-1431%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>
@@ -183,7 +183,7 @@ dsh web
183183

184184
```bash
185185
cd dsh-mneme && npm install
186-
npm test # 1429 个测试
186+
npm test # 1431 个测试
187187
npm run stress # 三轴线压测
188188
npm run sync # src → lib 同步
189189
```
@@ -366,7 +366,7 @@ The plugin ships a zero-dependency stdio MCP server (standalone npm package **`m
366366

367367
```bash
368368
cd dsh-mneme && npm install
369-
npm test # 1429 tests
369+
npm test # 1431 tests
370370
npm run stress # three-axis stress test
371371
npm run sync # src → lib sync
372372
```

‎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-1429%20passed-success)](https://github.com/slow-stack/mneme)
8+
[![tests](https://img.shields.io/badge/tests-1431%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)
@@ -584,7 +584,7 @@ src/
584584
├── api.js # HTTP 路由(Web 面板数据通道,含 /conflicts 冲突队列)
585585
└── index.js # 插件接线
586586
lib/ # src 的同步分发产物(npm run sync;发布前由 root prepack 的 check-sync.js 校验一致性;唯一手写例外 lib/client.js——Web 面板 bundle,sync 不覆盖)
587-
test/ # 1429 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
587+
test/ # 1431 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
588588
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 运行时清单
589589
```
590590
@@ -593,7 +593,7 @@ scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压
593593
```bash
594594
cd dsh-mneme
595595
npm install # 安装 peer 依赖(以 devDependencies 形式,用于本地测试)
596-
npm test # 运行 1429 个测试
596+
npm test # 运行 1431 个测试
597597
npm run stress # 三轴线压测:长会话检索 / 冲突仲裁 / 多 Agent 并发(离线 mock LLM)
598598
npm run sync # 把 src/ 同步到 lib/(发布时由 prepack 钩子自动执行)
599599
```

‎dsh-mneme/lib/inject.js‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ function lastUserQuery(ctx) {
5757
parts
5858
.map((p) => (typeof p === "string" ? p : p?.text ?? ""))
5959
.filter(Boolean)
60-
.join("\n")
61-
.slice(0, 500),
60+
.join("\n"),
6261
500
6362
);
6463
}
@@ -223,19 +222,23 @@ export function createInjector(ctx, service, settings, config) {
223222
// CHANGELOG 开着开关,实际什么都不会发生。本次按 v0.7.2 语义原样恢复:键名、
224223
// 默认值(false)、格式 `[当前时间: YYYY-MM-DD 周X HH:MM]` 全部不变,存量
225224
// feature_flags 里的配置恢复即生效。
226-
const WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
227-
let timePrefixSession = null;
225+
// 已注入过时间前缀的会话集合(#335 review:单值闩锁在 A→B→A 交替渲染下会
226+
// 重复注入)。上限 500 防长驻进程无界增长,超限逐出最早记录。
227+
const timePrefixDone = new Set();
228+
let timePrefixBare = false;
228229

229230
function renderTimePrefix(ctx) {
230231
if (config.injectTimePrefix !== true) return "";
231232
const sessionId = ctx?.agent?.session?.id;
232233
if (sessionId === undefined) {
233-
if (timePrefixSession !== null) return "";
234-
timePrefixSession = true;
234+
if (timePrefixBare) return "";
235+
timePrefixBare = true;
235236
} else {
236-
if (sessionId === timePrefixSession) return "";
237-
timePrefixSession = sessionId;
237+
if (timePrefixDone.has(sessionId)) return "";
238+
if (timePrefixDone.size >= 500) timePrefixDone.delete(timePrefixDone.keys().next().value);
239+
timePrefixDone.add(sessionId);
238240
}
241+
const WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
239242
const now = new Date();
240243
const pad = (n) => String(n).padStart(2, "0");
241244
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;

‎dsh-mneme/src/inject.js‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ function lastUserQuery(ctx) {
5757
parts
5858
.map((p) => (typeof p === "string" ? p : p?.text ?? ""))
5959
.filter(Boolean)
60-
.join("\n")
61-
.slice(0, 500),
60+
.join("\n"),
6261
500
6362
);
6463
}
@@ -223,19 +222,23 @@ export function createInjector(ctx, service, settings, config) {
223222
// CHANGELOG 开着开关,实际什么都不会发生。本次按 v0.7.2 语义原样恢复:键名、
224223
// 默认值(false)、格式 `[当前时间: YYYY-MM-DD 周X HH:MM]` 全部不变,存量
225224
// feature_flags 里的配置恢复即生效。
226-
const WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
227-
let timePrefixSession = null;
225+
// 已注入过时间前缀的会话集合(#335 review:单值闩锁在 A→B→A 交替渲染下会
226+
// 重复注入)。上限 500 防长驻进程无界增长,超限逐出最早记录。
227+
const timePrefixDone = new Set();
228+
let timePrefixBare = false;
228229

229230
function renderTimePrefix(ctx) {
230231
if (config.injectTimePrefix !== true) return "";
231232
const sessionId = ctx?.agent?.session?.id;
232233
if (sessionId === undefined) {
233-
if (timePrefixSession !== null) return "";
234-
timePrefixSession = true;
234+
if (timePrefixBare) return "";
235+
timePrefixBare = true;
235236
} else {
236-
if (sessionId === timePrefixSession) return "";
237-
timePrefixSession = sessionId;
237+
if (timePrefixDone.has(sessionId)) return "";
238+
if (timePrefixDone.size >= 500) timePrefixDone.delete(timePrefixDone.keys().next().value);
239+
timePrefixDone.add(sessionId);
238240
}
241+
const WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
239242
const now = new Date();
240243
const pad = (n) => String(n).padStart(2, "0");
241244
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,27 @@ test("issue #34/#333: time prefix coexists with hot context (time first, then ho
394394
const hi = text.indexOf("[短期上下文]");
395395
assert.ok(ti >= 0 && hi > ti, "time prefix precedes the hot block");
396396
});
397+
398+
test("issue #34/#333: latch survives interleaved sessions (A-B-A never re-injects A)", () => {
399+
const { contexts, service } = setup({ injectTimePrefix: true });
400+
service.saveWithDedupe({ type: "preference", title: "语言", content: "用户用中文交流", importance: 5 });
401+
contexts[0].text({ agent: { session: { id: "sA" } } });
402+
contexts[0].text({ agent: { session: { id: "sB" } } });
403+
// 回到 sA:单值闩锁会在这里重复注入(#335 review);集合闩锁不会。
404+
const back = contexts[0].text({ agent: { session: { id: "sA" } } });
405+
assert.ok(!back.includes("当前时间"), "interleaved return to session A must not re-inject");
406+
});
407+
408+
test("issue #334: snapshot query carries no lone surrogate (query path cut at exact limit)", () => {
409+
// #335 review:先 slice 再 safeSlice 时,恰为上限的串会绕过检查。现在
410+
// lastUserQuery 直接 safeSlice 全文,快照里的 query 必须干净。
411+
const { contexts } = setup();
412+
const events = [
413+
{ type: "user/message", data: { source: { kind: "user" }, content: [{ type: "text", text: "q".repeat(499) + "\u{1F534}" }] } },
414+
{ type: "assistant/message", data: { message: { content: [{ type: "text", text: "ans" }] } } }
415+
];
416+
contexts[0].text({ agent: { session: { id: "s-q2", snapshotEvents: () => events } } });
417+
const snap = getInjectionSnapshot();
418+
assert.ok(snap.query.length > 0, "query recorded");
419+
assert.ok(!hasLoneSurrogate(snap.query), "snapshot query is surrogate-clean");
420+
});

0 commit comments

Comments
 (0)