Skip to content

Commit a18fc67

Browse files
committed
Merge remote-tracking branch 'origin/main' into feat/graph-anchoring-v08
# Conflicts: # dsh-mneme/CHANGELOG.md
2 parents 56e8a6b + e066659 commit a18fc67

16 files changed

Lines changed: 995 additions & 183 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-1437%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-1450%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 # 1431 个测试
186+
npm test # 1450 个测试
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 # 1431 tests
369+
npm test # 1450 tests
370370
npm run stress # three-axis stress test
371371
npm run sync # src → lib sync
372372
```

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
## [Unreleased]
44

5+
## 🐛 修复
6+
7+
- **巩固阈值基线跨重启持久化(issue #89 基线半边)**:#291 只把时间闸门 `lastRunAt` 从 `dream_runs` 恢复了回来,阈值基线 `baseline{count,chars}` 仍是调度器闭包里的内存变量,进程重启归零。库里记忆远多于阈值(默认 10 条 / 5000 字符)时,重启后第一条写入(`notifyWrite` → `dreamHook` → `maybeSchedule`,更新类写入同样计入)就让 `overBase` 成立——**阈值被整个绕过**,直接开跑一整轮巩固。0.8.11 实测一天四次重启,其后第一轮巩固发生时窗口内只新增 6 / 7 / 0 / 8 条记忆(1427 / 1488 / 0 / 1509 字符),每轮约 16 万 tokens(consolidate ≈7.5 万 + 总览 ≈8.8 万,`llm_audit_logs` 实测)。现在成功轮把「本轮结束后的活跃库规模」写进 `dream_runs.store_count` / `store_chars`(新增两列,幂等迁移,两列同时有值或同时为 NULL),调度器构造时按 `run_type='auto'` 播种;认不出行(新库、升级后还没跑过成功轮、老行两列为 NULL)就退回零基线——**不假装知道基线**,行为与升级前逐字节一致。阈值口径抽成 `activeStoreSize()` 单一实现,`shouldTrigger` 与落库共用:两处各写一份过滤条件的话,重启播种读到的基线会与运行期判定漂移(一个把 document 算进去、一个不算),而这种漂移只在重启后才显形。run 自报的基线与审计行是同一份数字,进程内刷新改用它,两条路径不再各算一次。
8+
59
## 🆕 新增
610

711
- **图谱锚定层 Activation Anchoring(issue #24 块1)**:在 `entityRecallEnabled` 的「查询命中实体 → 挂联记忆进融合池」之上再加一层级联——命中实体作锚定种子(`graphSeedCap` 封顶,默认 12),沿 `entity_relations` 按 `graphCascadeDepth`(默认 2)向邻居扩散,邻居挂联的记忆以跳数配额权重(1:2:3-hop = 0.5/0.3/0.2)补位进池,恒低于种子直达分(attr 1.0 / relation 0.9)。级联 BFS 逐层懒取邻居(>1 跳真正可达),邻接查询双向绑定(方向写反的关系行不丢)。默认关=检索行为与 #219 单跳轴逐字节一致(关档连种子裁剪都不生效)。新增三键 `graphAnchoringEnabled`/`graphSeedCap`/`graphCascadeDepth` 注册进 feature_flags 白名单,面板可启停;lightMode 强制关。
812

13+
## 🧹 工程
14+
15+
- **基线持久化回归集 9 条,全量测试 1441 → 1450**:无种子对照组与播种组同库同写入(一个仍触发、一个被拦下——锁的正是这个差异,否则播种被忽略时两条断言同真同假)、阈值照常累积(+10 条仍触发)、run 自报基线优先于现场计算(运行后再长 15 条仍不触发)、成功轮落库 / 失败轮不落、sleep 行不进 auto 基线、半个基线被拒、老库补列幂等、非法种子(半个 / 负数 / 非整数 / 非对象)一律按「没有基线」处理、收尾取证失败既不反噬审计行也不写假基线、`index.js` 接线源码锁。**变异检验**:把播种改成忽略种子(`toBaseline(baselineSeed)` 恒为空)→ 2 条变红;把「成功轮落基线」分支短路 → 1 条变红。
16+
917
## [0.8.11] - 2026-09-30
1018

1119
## 🐛 修复

‎dsh-mneme/README.md‎

Lines changed: 4 additions & 4 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-1437%20passed-success)](https://github.com/slow-stack/mneme)
8+
[![tests](https://img.shields.io/badge/tests-1450%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)
@@ -253,7 +253,7 @@ v0.3.0 起新增**记忆基因**层:从记忆里抽取**命名实体**、**带
253253

254254
### 前置条件
255255

256-
- [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness)(DSH)— 兼容 DSH 0.1.x,已验证 0.1.2-rc.1(`Session.events` → `snapshotEvents()` 变更已由插件垫片兼容,新旧版本通吃;v0.7.9 确保该垫片真正进入发布产物 lib/)
256+
- [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness)(DSH)— 兼容 DSH 0.1.x 与 0.2.x(三段式 peer 范围见 `package.json`);开发与 CI 已对齐 0.2 线,`0.2.0-rc.1` / `0.2.0-rc.2` 实测全绿。旧版兼容垫片(`Session.events` → `snapshotEvents()`,v0.7.9 起确保进入发布产物 lib/)保留
257257
- Node 24+(`node:sqlite`)
258258

259259
### 安装步骤
@@ -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/ # 1431 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
587+
test/ # 1450 个 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 # 运行 1431 个测试
596+
npm test # 运行 1450 个测试
597597
npm run stress # 三轴线压测:长会话检索 / 冲突仲裁 / 多 Agent 并发(离线 mock LLM)
598598
npm run sync # 把 src/ 同步到 lib/(发布时由 prepack 钩子自动执行)
599599
```

‎dsh-mneme/lib/dream.js‎

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ function totalChars(memories) {
5252
return memories.reduce((sum, m) => sum + (m.title?.length ?? 0) + (m.content?.length ?? 0), 0);
5353
}
5454

55+
// 阈值口径的唯一实现:活跃记忆(未归档、非 summary/document)的条数与正文字符数。
56+
// 触发判定(shouldTrigger)与「成功轮把基线落库」(runDream 的 finish)都走这里——
57+
// 两处若各写一份过滤条件,重启播种读到的基线就会和运行期判定口径漂移(一个把
58+
// document 算进去、一个不算),阈值随之失真,且这种漂移只会在重启后才显形。
59+
function activeStoreSize(service) {
60+
const memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
61+
return { count: memories.length, chars: totalChars(memories) };
62+
}
63+
64+
// 基线的唯一合法形状:两个非负整数;其余(缺字段、负数、非整数、整个对象缺失)
65+
// 一律视为「没有基线」→ 调用方退回 {0,0}。播种值来自持久化列,可能是老库的 NULL
66+
// 或手工改坏的库,判定必须比调用点更严格,而不是相信数据库里存的一定合法。
67+
function toBaseline(raw) {
68+
if (!raw || typeof raw !== "object") return null;
69+
const count = Number.isInteger(raw.count) && raw.count >= 0 ? raw.count : null;
70+
const chars = Number.isInteger(raw.chars) && raw.chars >= 0 ? raw.chars : null;
71+
if (count === null || chars === null) return null;
72+
return { count, chars };
73+
}
74+
5575
// ---------------------------------------------------------------- audit
5676

5777
/**
@@ -760,15 +780,21 @@ export async function maintainIndexAfterDream(decisions, service, semantic) {
760780
// 用户配得比这更大的 dreamMinIntervalMinutes 基数压小(见 effectiveMinIntervalMs)。
761781
const FAILURE_BACKOFF_CAP_MS = 30 * 60 * 1000;
762782

763-
export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChars = 5000, delayMs = 2000, minIntervalMs = 0, failureBackoff = false, logger, semantic = null, lastRunAtSeed = 0, peakHours = "", peakMaxDeferMinutes = 120, auditPeakSkip = null, now = () => Date.now(), setTimeoutFn = setTimeout, clearTimeoutFn = clearTimeout }) {
783+
export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChars = 5000, delayMs = 2000, minIntervalMs = 0, failureBackoff = false, logger, semantic = null, lastRunAtSeed = 0, baselineSeed = null, peakHours = "", peakMaxDeferMinutes = 120, auditPeakSkip = null, now = () => Date.now(), setTimeoutFn = setTimeout, clearTimeoutFn = clearTimeout }) {
764784
let pendingTimer = null;
765785
// Issue #239(第 4 项)镜像到巩固:高峰顺延定时器。与 pendingTimer 分开——两者
766786
// 语义不同(一个是「马上要跑」,一个是「等出高峰再跑」),合成一个变量会让
767787
// maybeSchedule 的守卫在顺延期间把新的写入触发误当成「已有待跑」而吞掉。
768788
let deferTimer = null;
769789
let running = false;
770790
let disposed = false;
771-
let baseline = { count: 0, chars: 0 };
791+
// Issue #89(基线半边):阈值基线跨重启持久化。baselineSeed 由调用方从
792+
// dream_runs 读上次成功轮结束时的库规模(store.lastDreamBaseline)——它只活在
793+
// 闭包内存里时进程重启即归零,而库里记忆远多于阈值,重启后第一条写入就满足
794+
// overBase、绕过阈值开跑整轮(#291 只补了 lastRunAt 那半边)。缺种子(新库、
795+
// 升级后还没跑过成功轮)或种子非法时退回 {0,0}:不假装知道基线,行为与升级前
796+
// 逐字节一致——修的是「知道却不认」,不是凭空造一个基线。
797+
let baseline = toBaseline(baselineSeed) ?? { count: 0, chars: 0 };
772798
let inFlight = null;
773799
// Issue #89(请求 2):上一次实际开跑时刻。失败/degraded 的 run 也占用
774800
// 最小间隔——节流的目的正是防止失败调用连发;间隔内的触发静默跳过。
@@ -792,9 +818,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
792818
}
793819

794820
function shouldTrigger(service) {
795-
const memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
796-
const count = memories.length;
797-
const chars = totalChars(memories);
821+
const { count, chars } = activeStoreSize(service);
798822
const overBase = count >= baseline.count + thresholdCount || chars >= baseline.chars + thresholdChars;
799823
const overAbs = count >= thresholdCount || chars >= thresholdChars;
800824
return { trigger: overAbs && overBase, count, chars };
@@ -881,7 +905,10 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
881905
if (result && result.ok) {
882906
consecutiveFailures = 0; // Issue #292:成功清零,下次触发回到基数间隔
883907
try {
884-
baseline = shouldTrigger(service);
908+
// Issue #89(基线半边):run 自报的基线(与审计行同一份数字)优先——重启
909+
// 播种读的就是它,两条路径共用唯一口径;no-op 桩不回这个字段时退回现场
910+
// 计算,最小测试替身的行为逐字节不变。
911+
baseline = toBaseline(result.baseline) ?? shouldTrigger(service);
885912
} catch (error) {
886913
// Store closed mid-flight: keep the last known baseline.
887914
logger?.warn?.(`dsh-mneme dream: baseline refresh failed: ${String(error)}`);
@@ -986,6 +1013,19 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
9861013
const applied = result.applied ?? 0;
9871014
const summaryStored = result.summary ?? false;
9881015
const receipt = buildReceipt({ runId, status, snapshotHash, inputCount: snapshot.size, applied, summaryStored });
1016+
// Issue #89(基线半边):成功轮把「本轮结束后的库规模」落进审计行 = 阈值基线的
1017+
// 持久化副本,重启后由 store.lastDreamBaseline 播种。判定条件必须与调度器刷新
1018+
// 基线的条件严格一致(result.ok)——非 ok 轮不推进基线是既有语义,若这里也落值,
1019+
// 重启后播种就会拿到一个「其实没推进过」的假基线,阈值反而被抬高。
1020+
// 取不到(库已关闭等)就落 NULL:宁可不持久化,也不写一个错的基线。
1021+
let baselineSnapshot = null;
1022+
if (result.ok) {
1023+
try {
1024+
baselineSnapshot = activeStoreSize(service);
1025+
} catch {
1026+
baselineSnapshot = null;
1027+
}
1028+
}
9891029
try {
9901030
service.saveDreamRun({
9911031
id: runId,
@@ -1023,12 +1063,18 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
10231063
// 而非内联进 decisions——degraded 的 decisions 是合法子集,下游按「决策
10241064
// 数组」消费,内联标记会污染其它读者(失败轮的 _validationFailed 是整单
10251065
// 拒绝哨兵,语义与「部分应用」不同,也不复用)。
1026-
skipped: result.skipped
1066+
skipped: result.skipped,
1067+
// Issue #89(基线半边):成功的 run 才带这两个值(见上方 baselineSnapshot
1068+
// 的注释)。两列要么同时有值、要么同时为 NULL,读侧以「同时有值」认账。
1069+
store_count: baselineSnapshot?.count,
1070+
store_chars: baselineSnapshot?.chars
10271071
});
10281072
} catch (error) {
10291073
logger?.warn?.(`dsh-mneme dream: failed to record audit run: ${String(error)}`);
10301074
}
1031-
return { ...result, runId, receipt, snapshotHash };
1075+
// Issue #89(基线半边):把「与审计行同一份数字」的基线回给调度器,两条路径
1076+
// (进程内刷新 / 跨重启播种)因此共享唯一口径;调度器拿不到时会退出现场计算。
1077+
return { ...result, runId, receipt, snapshotHash, baseline: baselineSnapshot ?? undefined };
10321078
};
10331079
if (!route) {
10341080
logger?.warn?.("dsh-mneme dream: no llm route available");

‎dsh-mneme/lib/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ export const apply = (ctx, config) => {
502502
logger: ctx.logger,
503503
semantic: { embedder, vectorIndex },
504504
lastRunAtSeed: store.lastDreamRunAt("auto"),
505+
// Issue #89(基线半边):阈值基线同样跨重启恢复——只恢复 lastRunAt 的话,
506+
// 重启后的第一条写入仍会绕过阈值(库里记忆多于阈值时 overBase 恒成立)开跑
507+
// 整轮。种子取上次成功轮的库规模,口径与运行期刷新一致;读不到时调度器退回
508+
// 零基线(= 升级前行为)。
509+
baselineSeed: store.lastDreamBaseline("auto"),
505510
// Issue #239(第 4 项)镜像到巩固:高峰期不做梦,顺延到最近的高峰结束时刻。
506511
peakHours: cfg.dreamPeakHours ?? "",
507512
peakMaxDeferMinutes: cfg.dreamPeakMaxDeferMinutes ?? 120,

0 commit comments

Comments
 (0)