Skip to content

Commit 6ed9376

Browse files
committed
fix(inject): pin 池「未展示」不再虚报、降级行同样受硬顶约束(#266 评审)
评审(Anans-Ivresse)指出两处——不影响合并,但都是真缺陷: - 「另有 N 条未展示」按 eligible - pinned 直接相减,把被 pin 预算挤下来、却仍被 general 槽选中(即真的在块内)的条目也算成未展示。改成统一按「有没有进 selected」 判:suppressed = eligible 里没进块的条数(评审实测:pref#2 已在块内仍报 2 条)。 suppressed 因此挪到 general 选完之后算,eligible 提到块外留一手。 - injectMemory 的 `_full_content` 早返回唯一效果就是跳过 maxLength(两条分支取的都是 m.content,差别只在截不截),于是 pin 的 PINNED_CONTENT_MAX 在这一路形同不存在。 删掉该分支,只留一条截断路径:超限照旧带提示,原文永远可 memory_get 取回。 语义边界(避免「逐字保真」被过度承诺):对已被 sleep 降级的行,注入的是该行当前正文 (即其摘要)——降级是 sleep 的审计决定,注入层不擅自复原 _full_content;本次修的是 「它不该因此绕过上限」。 测试:两条既有断言正好把旧行为写成了契约,一并改成新契约(旧断言即新测试的反证): inject-content-cap 的「400 字符摘要不加提示」→「照截 + 带提示」;inject-pools 的超预算 用例 →「另有 1 条未展示」并断言被挤下的那条真的在块内。 验证:注入相关三文件 32/32;全量 1229 tests / 1228 pass / 0 fail / 1 skipped; check-sync 43 文件一致;所碰文件 lint 无告警。
1 parent 608d8ef commit 6ed9376

6 files changed

Lines changed: 51 additions & 41 deletions

File tree

‎dsh-mneme/lib/inject.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ export function createInjector(ctx, service, settings, config) {
150150
// 失真,同时给「一条超长约束每轮吃满常驻段」留一道闸(超顶仍带截断提示)。
151151
const PINNED_CONTENT_MAX = Math.max(maxContent, 2000);
152152

153-
// Compressed injection (v0.5.0 2.1): a sleep-demoted row already carries its
154-
// summary in `content` with the original parked in `_full_content` — inject
155-
// the summary verbatim instead of re-truncating the (already short) text.
156-
// Regular long rows keep the hard truncate.
153+
// Compressed injection (v0.5.0 2.1): a sleep-demoted row carries its summary in
154+
// `content` with the original parked in `_full_content` — inject the summary
155+
// verbatim instead of re-truncating the (already short) text. 但「降级过」不等于
156+
// 「天然够短」:原先那条 `_full_content` 早返回的唯一效果就是**跳过 maxLength**
157+
// (两条分支取的都是 m.content),于是 pin 的硬顶在这一路上形同不存在(#266 评审)。
158+
// 现在只有一条截断路径:超限照旧带提示,原文永远可通过 memory_get 取回。
157159
function injectMemory(m, maxLength = maxContent) {
158-
if (m?._full_content) return String(m.content ?? "");
159160
const text = String(m?.content ?? "");
160161
if (text.length <= maxLength) return text;
161162
return `${text.slice(0, maxLength)}…${STR.truncatedHint[language](maxLength, text.length, m.id)}`;

‎dsh-mneme/lib/service.js‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,21 +1483,13 @@ export function createService({ store, mirror, config, onWrite, logger }) {
14831483
// 名额、也不被 document 预算截断,因此不会把当前任务需要的情景候选挤出去;
14841484
// 超预算的条数回报给调用方,在块内如实标注(绝不静默省略)。
14851485
const pinnedBudget = Math.max(0, Math.min(5, Math.floor(config?.pinnedInjectBudget ?? 0)));
1486+
// eligible 留到块外:未展示条数要等 general 槽选完才算得准(见 selected 之后)。
1487+
const eligible = pinnedBudget > 0 ? candidates.filter((m) => PINNED_MEMORY_TYPES.has(m.type)) : [];
14861488
let pinned = [];
1487-
if (pinnedBudget > 0) {
1488-
const eligible = candidates.filter((m) => PINNED_MEMORY_TYPES.has(m.type));
1489+
if (pinnedBudget > 0 && eligible.length > 0) {
14891490
pinned = eligible.slice(0, pinnedBudget);
1490-
if (pinned.length > 0) {
1491-
const pinnedIds = new Set(pinned.map((m) => m.id));
1492-
candidates = candidates.filter((m) => !pinnedIds.has(m.id));
1493-
}
1494-
if (pinnedStats) {
1495-
pinnedStats.shown = pinned.length;
1496-
pinnedStats.suppressed = eligible.length - pinned.length;
1497-
}
1498-
} else if (pinnedStats) {
1499-
pinnedStats.shown = 0;
1500-
pinnedStats.suppressed = 0;
1491+
const pinnedIds = new Set(pinned.map((m) => m.id));
1492+
candidates = candidates.filter((m) => !pinnedIds.has(m.id));
15011493
}
15021494
// Issue #205:注入位跨轮轮换。rotate = 最近 N 轮注入过的 id 集合(由注入层
15031495
// 按会话维护并传入):这些条目本轮不再优先——新鲜者前置(各自内部相对次序
@@ -1525,6 +1517,15 @@ export function createService({ store, mirror, config, onWrite, logger }) {
15251517
// #249 第一批:pin 前置到块内相关性排序之前(验收项),且不占 maxItems 名额。
15261518
// pinned 为空时 selected 就是 general 本身——关闭态与改动前逐字节一致。
15271519
const selected = pinned.length > 0 ? [...pinned, ...general] : general;
1520+
if (pinnedStats) {
1521+
// 「未展示」只数**真的没进块**的 pin 类条目:被 pin 预算挤下来的条目会回到
1522+
// 候选池,仍可能被 general 槽选中——那就是展示了。按 eligible - pinned 直接
1523+
// 相减会把它们也算成未展示,块头那一行于是虚报(评审实测:pref#2 已在块内,
1524+
// 仍报「另有 2 条未展示」)。所以统一按「有没有进 selected」判。
1525+
const shownIds = new Set(selected.map((m) => m.id));
1526+
pinnedStats.shown = pinned.length;
1527+
pinnedStats.suppressed = eligible.filter((m) => !shownIds.has(m.id)).length;
1528+
}
15281529
touchRecalled(selected);
15291530
// #217 口径(2026-09-19 拍板):注入是曝光型访问事件,与检索命中同表分账
15301531
// (mode='inject',candidates 存实际注入集)。跟随 recallRecordDefault——

‎dsh-mneme/src/inject.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ export function createInjector(ctx, service, settings, config) {
150150
// 失真,同时给「一条超长约束每轮吃满常驻段」留一道闸(超顶仍带截断提示)。
151151
const PINNED_CONTENT_MAX = Math.max(maxContent, 2000);
152152

153-
// Compressed injection (v0.5.0 2.1): a sleep-demoted row already carries its
154-
// summary in `content` with the original parked in `_full_content` — inject
155-
// the summary verbatim instead of re-truncating the (already short) text.
156-
// Regular long rows keep the hard truncate.
153+
// Compressed injection (v0.5.0 2.1): a sleep-demoted row carries its summary in
154+
// `content` with the original parked in `_full_content` — inject the summary
155+
// verbatim instead of re-truncating the (already short) text. 但「降级过」不等于
156+
// 「天然够短」:原先那条 `_full_content` 早返回的唯一效果就是**跳过 maxLength**
157+
// (两条分支取的都是 m.content),于是 pin 的硬顶在这一路上形同不存在(#266 评审)。
158+
// 现在只有一条截断路径:超限照旧带提示,原文永远可通过 memory_get 取回。
157159
function injectMemory(m, maxLength = maxContent) {
158-
if (m?._full_content) return String(m.content ?? "");
159160
const text = String(m?.content ?? "");
160161
if (text.length <= maxLength) return text;
161162
return `${text.slice(0, maxLength)}…${STR.truncatedHint[language](maxLength, text.length, m.id)}`;

‎dsh-mneme/src/service.js‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,21 +1483,13 @@ export function createService({ store, mirror, config, onWrite, logger }) {
14831483
// 名额、也不被 document 预算截断,因此不会把当前任务需要的情景候选挤出去;
14841484
// 超预算的条数回报给调用方,在块内如实标注(绝不静默省略)。
14851485
const pinnedBudget = Math.max(0, Math.min(5, Math.floor(config?.pinnedInjectBudget ?? 0)));
1486+
// eligible 留到块外:未展示条数要等 general 槽选完才算得准(见 selected 之后)。
1487+
const eligible = pinnedBudget > 0 ? candidates.filter((m) => PINNED_MEMORY_TYPES.has(m.type)) : [];
14861488
let pinned = [];
1487-
if (pinnedBudget > 0) {
1488-
const eligible = candidates.filter((m) => PINNED_MEMORY_TYPES.has(m.type));
1489+
if (pinnedBudget > 0 && eligible.length > 0) {
14891490
pinned = eligible.slice(0, pinnedBudget);
1490-
if (pinned.length > 0) {
1491-
const pinnedIds = new Set(pinned.map((m) => m.id));
1492-
candidates = candidates.filter((m) => !pinnedIds.has(m.id));
1493-
}
1494-
if (pinnedStats) {
1495-
pinnedStats.shown = pinned.length;
1496-
pinnedStats.suppressed = eligible.length - pinned.length;
1497-
}
1498-
} else if (pinnedStats) {
1499-
pinnedStats.shown = 0;
1500-
pinnedStats.suppressed = 0;
1491+
const pinnedIds = new Set(pinned.map((m) => m.id));
1492+
candidates = candidates.filter((m) => !pinnedIds.has(m.id));
15011493
}
15021494
// Issue #205:注入位跨轮轮换。rotate = 最近 N 轮注入过的 id 集合(由注入层
15031495
// 按会话维护并传入):这些条目本轮不再优先——新鲜者前置(各自内部相对次序
@@ -1525,6 +1517,15 @@ export function createService({ store, mirror, config, onWrite, logger }) {
15251517
// #249 第一批:pin 前置到块内相关性排序之前(验收项),且不占 maxItems 名额。
15261518
// pinned 为空时 selected 就是 general 本身——关闭态与改动前逐字节一致。
15271519
const selected = pinned.length > 0 ? [...pinned, ...general] : general;
1520+
if (pinnedStats) {
1521+
// 「未展示」只数**真的没进块**的 pin 类条目:被 pin 预算挤下来的条目会回到
1522+
// 候选池,仍可能被 general 槽选中——那就是展示了。按 eligible - pinned 直接
1523+
// 相减会把它们也算成未展示,块头那一行于是虚报(评审实测:pref#2 已在块内,
1524+
// 仍报「另有 2 条未展示」)。所以统一按「有没有进 selected」判。
1525+
const shownIds = new Set(selected.map((m) => m.id));
1526+
pinnedStats.shown = pinned.length;
1527+
pinnedStats.suppressed = eligible.filter((m) => !shownIds.has(m.id)).length;
1528+
}
15281529
touchRecalled(selected);
15291530
// #217 口径(2026-09-19 拍板):注入是曝光型访问事件,与检索命中同表分账
15301531
// (mode='inject',candidates 存实际注入集)。跟随 recallRecordDefault——

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,18 @@ test("block budget scales with the cap (raised cap is not defeated by the 1500 g
6767
assert.ok(text.includes("乙"), "second entry still rendered");
6868
});
6969

70-
test("_full_content (sleep-compressed) path stays verbatim without a hint", () => {
70+
test("_full_content (sleep-compressed) path obeys the cap and says so (#266 评审)", () => {
7171
const { store, contexts, service } = setup();
7272
const mem = service.saveWithDedupe({ type: "preference", title: "压缩记忆", content: "占位", importance: 5 }).memory;
7373
// 模拟 sleep 降级:content=压缩摘要(400 字符 > 默认 300 上限),原文入库 _full_content
7474
const summary = "压缩摘要。".repeat(80);
7575
store.db.prepare("UPDATE memories SET content=?, _full_content=? WHERE id=?")
7676
.run(summary, "原始长文".repeat(100), mem.id);
7777
const text = contexts[0].text({});
78-
assert.ok(text.includes(summary), "summary injected verbatim beyond the cap");
79-
assert.ok(!text.includes("已截断"), "no truncation hint on the compressed path");
78+
// 「降级过」不等于「天然够短」:原先这条早返回的唯一效果就是绕过上限,现在统一截断。
79+
assert.ok(!text.includes(summary), "oversized summary is not injected whole");
80+
assert.ok(text.includes("压缩摘要。".repeat(60)), "the part inside the cap is still there (300 chars)");
81+
assert.ok(text.includes("已截断"), "the cut is reported, never silent");
8082
});
8183

8284
test("default cap stays 300 (ellipsis regression of Bug6)", () => {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ test("#249: pinned entries lead the block and sit outside maxInjectedItems", ()
6767
assert.equal(seq.length, 4, "pinned entries do not consume maxInjectedItems slots");
6868
});
6969

70-
test("#249: over-budget pinned entries are annotated, never silently dropped", () => {
70+
test("#249: over-budget pinned entries are annotated with the truly hidden count", () => {
7171
const { service, text } = setup({ maxInjectedItems: 1, pinnedInjectBudget: 1 });
7272
for (let i = 0; i < 3; i++) {
7373
service.saveWithDedupe({ type: "preference", title: `偏好${i}`, content: `第 ${i} 条偏好内容`, importance: 5 });
7474
}
7575
const body = text();
76-
assert.ok(body.includes("另有 2 条未展示"), `annotation must carry the hidden count: ${body}`);
76+
// 3 条同类里 pref0 被 pin、pref1 进了 general 槽(真的在块内),只有 pref2 没展示。
77+
// 按 eligible - pinned 直接相减会把 pref1 也算成未展示——虚报(#266 评审实测)。
78+
assert.ok(body.includes("偏好1"), "the over-budget one that made it into a general slot is really shown");
79+
assert.ok(body.includes("另有 1 条未展示"), `annotation must carry the hidden count: ${body}`);
80+
assert.ok(!body.includes("另有 2 条"), "no double counting");
7781
});
7882

7983
test("#249: pinned entries are exempt from cross-turn rotation", () => {

0 commit comments

Comments
 (0)