Skip to content

Commit 9b2b2ef

Browse files
committed
fix(sleep): keep the longer copy, not the newer one, when auto-resolving
"Keep the newer one" reads as reasonable and measures wrong. In the live queue, of the 20 pairs at similarity >= 0.95, 11 (55%) have a newer side that is SHORTER than the older one, by 132 characters on average; at 0.88 it is 122 of 213 (57%). A distilled, compressed copy carries a later updated_at than the fuller original, so newer-wins archives the better record. Typical case: the 371-character original about "开不了新对话/会话异常" against its 307-character distilled rewrite - the rewrite is the newer one. This matches mem0's adjudication rule: prompts.py:217 says to keep the copy carrying the most information, not the newest. cognee does the opposite ("take the newer one"), which would cut "0:00 前必须躺下" down to a fragment. Selection is now length first, then updated_at, then id. Equal length counts as "comparable information", so the newer side wins there; the id tiebreak keeps a replay from flipping sides and archiving the copy it just saved. Tests: the longer copy survives when the shorter one is newer; equal-length copies fall back to the newer one. Mutation-checked by restoring the newer-first order, which turns the first case red. Disabling the updated_at tiebreak stays green - the fixture's ids happen to sort the same way, so that rung is a determinism guarantee rather than something the fixture exercises.
1 parent 7fbd60f commit 9b2b2ef

3 files changed

Lines changed: 101 additions & 12 deletions

File tree

‎dsh-mneme/lib/dream/sleep.js‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
443443
// 报成 backlog 会让调度器空转(catchup 上限 24 轮)。
444444
const perRunSkipped = withinRoom.length - pool.length;
445445
// Issue #603 第 4 层(自动裁决档,默认关):相似度极高的**同 scope** 对不再
446-
// 进人工队列,直接保留新条、归档旧条。人只看模糊地带。
446+
// 进人工队列,直接保留信息量更大的那条、归档另一条。人只看模糊地带。
447447
//
448448
// 为什么阈值留在配置里而不是写死 0.95:live 库 345 条有相似度的历史行里,
449449
// >=0.95 只有 20 条(5.8%)、>=0.94 是 28 条(8.1%)。这一档减的是峰值,
@@ -474,22 +474,39 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
474474
// 这一档不走 LLM,没有批次时间戳可借(judgedAt 在下面那条 LLM 通路里才定义),
475475
// 自己取一个:台账要能回答「什么时候判的」,不能留空。
476476
const autoJudgedAt = new Date().toISOString();
477-
// 保留较新的那条:updated_at 相等时用 id 兜底,保证同一对每次判出同一个
478-
// 赢家(否则重放会来回翻,归档的可能是刚刚保住的那条)。
479-
const newerFirst = (p) => {
477+
// 保留**信息量更大**的那条,不是更新的那条。
478+
//
479+
// 「保留新的」看着合理,实测是错的:live 库 >=0.95 的 20 对里,11 对(55%)
480+
// 新的那条比旧的短,平均短 132 字;降到 0.88 的 213 对里 122 对(57%)如此。
481+
// 典型例子:「用户同时开 web profile 和 desktop profile 时出现开不了新对话/
482+
// 会话异常,用户自己…」(371 字,旧)被压成「用户同时开 web profile 和
483+
// desktop profile 的 dsh-mneme 时开不了新对话」(307 字,新)——蒸馏时压缩过
484+
// 的一版反而更新。留新条等于把更完整的那条归档掉。
485+
//
486+
// 这与 mem0 的裁决规则一致:`configs/prompts.py:217` 明写「两句话说的是同一
487+
// 件事时,留信息量最大的那条,不是留最新的」。cognee 反过来(「取更新的」),
488+
// 那是反例——照抄会把「0:00 前必须躺下」削成残缺版。
489+
//
490+
// 长度相同才算「信息量相当」,此时退回更新时间,再退回 id:三级兜底保证同一
491+
// 对每次判出同一个赢家(否则重放会来回翻,归档的可能是刚保住的那条)。
492+
const infoFirst = (p) => {
493+
const an = p.a.content?.length ?? 0;
494+
const bn = p.b.content?.length ?? 0;
495+
if (an !== bn) return an > bn ? [p.a, p.b] : [p.b, p.a];
480496
const au = p.a.updated_at ?? "";
481497
const bu = p.b.updated_at ?? "";
482498
if (au !== bu) return au > bu ? [p.a, p.b] : [p.b, p.a];
483499
return p.a.id > p.b.id ? [p.a, p.b] : [p.b, p.a];
484500
};
485501
for (const p of autoPool) {
486-
const [winner, loser] = newerFirst(p);
502+
const [winner, loser] = infoFirst(p);
487503
const snapshot = new Map();
488504
for (const m of [winner, loser]) {
489505
snapshot.set(m.id, { updated_at: m.updated_at, content: m.content, title: m.title });
490506
}
491507
// trustEpistemicWeighting 显式关掉:它会在 applySupersede 里按 epistemic_status
492-
// 交换赢家,那会让「保留新条」这条契约随数据漂移。自动档的承诺必须是确定的。
508+
// 交换赢家,那会让「留信息量更大的那条」这条契约随数据漂移。自动档的承诺
509+
// 必须是确定的。
493510
const { applied, conflicts } = applyDecisions(
494511
[{ action: "supersede", winner: winner.id, loser: loser.id }],
495512
service, logger, snapshot, { ...config, trustEpistemicWeighting: false }

‎dsh-mneme/src/dream/sleep.js‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
443443
// 报成 backlog 会让调度器空转(catchup 上限 24 轮)。
444444
const perRunSkipped = withinRoom.length - pool.length;
445445
// Issue #603 第 4 层(自动裁决档,默认关):相似度极高的**同 scope** 对不再
446-
// 进人工队列,直接保留新条、归档旧条。人只看模糊地带。
446+
// 进人工队列,直接保留信息量更大的那条、归档另一条。人只看模糊地带。
447447
//
448448
// 为什么阈值留在配置里而不是写死 0.95:live 库 345 条有相似度的历史行里,
449449
// >=0.95 只有 20 条(5.8%)、>=0.94 是 28 条(8.1%)。这一档减的是峰值,
@@ -474,22 +474,39 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
474474
// 这一档不走 LLM,没有批次时间戳可借(judgedAt 在下面那条 LLM 通路里才定义),
475475
// 自己取一个:台账要能回答「什么时候判的」,不能留空。
476476
const autoJudgedAt = new Date().toISOString();
477-
// 保留较新的那条:updated_at 相等时用 id 兜底,保证同一对每次判出同一个
478-
// 赢家(否则重放会来回翻,归档的可能是刚刚保住的那条)。
479-
const newerFirst = (p) => {
477+
// 保留**信息量更大**的那条,不是更新的那条。
478+
//
479+
// 「保留新的」看着合理,实测是错的:live 库 >=0.95 的 20 对里,11 对(55%)
480+
// 新的那条比旧的短,平均短 132 字;降到 0.88 的 213 对里 122 对(57%)如此。
481+
// 典型例子:「用户同时开 web profile 和 desktop profile 时出现开不了新对话/
482+
// 会话异常,用户自己…」(371 字,旧)被压成「用户同时开 web profile 和
483+
// desktop profile 的 dsh-mneme 时开不了新对话」(307 字,新)——蒸馏时压缩过
484+
// 的一版反而更新。留新条等于把更完整的那条归档掉。
485+
//
486+
// 这与 mem0 的裁决规则一致:`configs/prompts.py:217` 明写「两句话说的是同一
487+
// 件事时,留信息量最大的那条,不是留最新的」。cognee 反过来(「取更新的」),
488+
// 那是反例——照抄会把「0:00 前必须躺下」削成残缺版。
489+
//
490+
// 长度相同才算「信息量相当」,此时退回更新时间,再退回 id:三级兜底保证同一
491+
// 对每次判出同一个赢家(否则重放会来回翻,归档的可能是刚保住的那条)。
492+
const infoFirst = (p) => {
493+
const an = p.a.content?.length ?? 0;
494+
const bn = p.b.content?.length ?? 0;
495+
if (an !== bn) return an > bn ? [p.a, p.b] : [p.b, p.a];
480496
const au = p.a.updated_at ?? "";
481497
const bu = p.b.updated_at ?? "";
482498
if (au !== bu) return au > bu ? [p.a, p.b] : [p.b, p.a];
483499
return p.a.id > p.b.id ? [p.a, p.b] : [p.b, p.a];
484500
};
485501
for (const p of autoPool) {
486-
const [winner, loser] = newerFirst(p);
502+
const [winner, loser] = infoFirst(p);
487503
const snapshot = new Map();
488504
for (const m of [winner, loser]) {
489505
snapshot.set(m.id, { updated_at: m.updated_at, content: m.content, title: m.title });
490506
}
491507
// trustEpistemicWeighting 显式关掉:它会在 applySupersede 里按 epistemic_status
492-
// 交换赢家,那会让「保留新条」这条契约随数据漂移。自动档的承诺必须是确定的。
508+
// 交换赢家,那会让「留信息量更大的那条」这条契约随数据漂移。自动档的承诺
509+
// 必须是确定的。
493510
const { applied, conflicts } = applyDecisions(
494511
[{ action: "supersede", winner: winner.id, loser: loser.id }],
495512
service, logger, snapshot, { ...config, trustEpistemicWeighting: false }

‎dsh-mneme/test/scope-candidates.test.js‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,61 @@ test("#603 auto-resolve: with the switch off nothing is auto-adjudicated", async
367367
store.close();
368368
});
369369

370+
// 自动裁决保留的是**信息量更大**的那条,不是更新的那条。
371+
// 反例来自 live 库:>=0.95 的 20 对里 11 对(55%)新条比旧条短,平均短 132 字——
372+
// 蒸馏压缩过的一版反而更新时间更晚,按「留新的」会把更完整的那条归档掉。
373+
test("#603 auto-resolve: the longer copy survives even when the shorter one is newer", async () => {
374+
const { store, service, vectorIndex } = setup();
375+
// 先存长的那条(更旧),再存短的那条(更新):新条更短,正是实测里的多数情况。
376+
const longBody = "缓存策略要按写入频率分三档:热数据走内存、温数据走 SSD、冷数据归档到对象存储。"
377+
+ "切换阈值由每小时访问次数决定,低于一次就降档,连续三天高于十次再升档。";
378+
const shortBody = "缓存策略按写入频率分档。";
379+
const older = service.saveWithDedupe({
380+
type: "project", title: "缓存分层", content: longBody
381+
}).memory;
382+
const newer = service.saveWithDedupe({
383+
type: "project", title: "缓存分层摘要", content: shortBody
384+
}).memory;
385+
vectorIndex.saveEmbedding(older.id, [1, 0, 0]);
386+
vectorIndex.saveEmbedding(newer.id, [1, 0, 0]);
387+
assert.ok(older.content.length > newer.content.length, "fixture: the older copy must be the longer one");
388+
389+
const result = await runSleep(
390+
llmCtx(), service,
391+
baseConfig({ conflictFreezeEnabled: true, conflictAutoResolveEnabled: true, conflictAutoResolveMinSimilarity: 0.95 }),
392+
{ warn: () => {}, info: () => {} }, { embedder, vectorIndex }, null
393+
);
394+
assert.equal(result.status, "ok");
395+
assert.equal(result.phases.conflicts.autoResolved, 1, "the pair is auto-adjudicated");
396+
397+
// 幸存者必须是长的那条:留新的会把三档策略削成一句话。
398+
assert.equal(store.getById(older.id).archived, false, "the longer, older copy must survive");
399+
assert.equal(store.getById(newer.id).archived, true, "the shorter, newer copy is the one archived");
400+
store.close();
401+
});
402+
403+
// 长度相同才是「信息量相当」,此时退回更新时间:新条胜出,且结论确定可重放。
404+
test("#603 auto-resolve: equal-length copies fall back to the newer one, deterministically", async () => {
405+
const { store, service, vectorIndex } = setup();
406+
const body = "部署脚本先跑 migrate 再跑 smoke,任一步失败就回滚到上一个 tag。";
407+
const older = service.saveWithDedupe({ type: "project", title: "部署甲", content: body }).memory;
408+
const newer = service.saveWithDedupe({ type: "project", title: "部署乙", content: body }).memory;
409+
vectorIndex.saveEmbedding(older.id, [1, 0, 0]);
410+
vectorIndex.saveEmbedding(newer.id, [1, 0, 0]);
411+
412+
const result = await runSleep(
413+
llmCtx(), service,
414+
baseConfig({ conflictFreezeEnabled: true, conflictAutoResolveEnabled: true, conflictAutoResolveMinSimilarity: 0.95 }),
415+
{ warn: () => {}, info: () => {} }, { embedder, vectorIndex }, null
416+
);
417+
assert.equal(result.status, "ok");
418+
assert.equal(result.phases.conflicts.autoResolved, 1);
419+
// 等长 → 比 updated_at:后存的赢。长度相等时不至于两边都"更完整",留新的是安全的。
420+
assert.equal(store.getById(newer.id).archived, false, "equal length: the newer copy survives");
421+
assert.equal(store.getById(older.id).archived, true);
422+
store.close();
423+
});
424+
370425
test("#603 auto-resolve: a failed adjudication falls back to the manual queue instead of vanishing", async () => {
371426
const { store, service, vectorIndex } = setup();
372427
savePair(service, vectorIndex, "回退A", "回退B", { agent: "coder" }, { agent: "coder" });

0 commit comments

Comments
 (0)