Skip to content

Commit b87abb0

Browse files
committed
feat(graph): 边权重演化 Weight Evolution(issue #24 块2)
entity_relations 加 weight + source 列(idempotent migration,存量行默认 manual 1.0),按建边来源映射初值:manual/confirmed 1.0、tag 0.3(LLM 打的延续偏见,靠「用出来」抬升)、llm 0.4。extractor 自动建边落 source=llm。 bumpRelationWeight 演化接口:只加不减、封顶 1.0,留给 touch 门控驱动。 哲学「关系是用出来的,不是建出来的」:初值只反映来源,抬升靠触达。 - store:pgsm 两列 + saveRelation source→weight 映射 + bumpRelationWeight - extractor:LLM 抽取建边标记 source=llm(初始 0.4) - config:graphWeightEnabled(默认关=行为不变)+ graphWeightDelta,全走 feature_flags 面板可启停、lightMode 强制关闭 - 设计呼应 heptaspirit serendipity-engine 结论:touch 只记录不演化、 tag 低起步共享同一自激回路顾虑(#234 同源);本块先把「演化接口 + 存储」 立起来,真正的自激风险留给数据印证后由调用方量化决定 测试:6 组 graph-weight-evolution 新增全过;entities 全组回归绿
1 parent 8519f57 commit b87abb0

7 files changed

Lines changed: 194 additions & 12 deletions

File tree

‎dsh-mneme/lib/config.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@ export const Config = z.object({
463463
// 检索融合池(与 BM25 同级的确认/回填信号)。默认关=检索行为与 #219 前
464464
// 逐字节一致;走 feature_flags 面板可启停,lightMode 强制关闭。
465465
entityRecallEnabled: z.boolean().default(false),
466+
// 边权重演化(issue #24 块2):touch 门控是否把被寻回路径的边权重抬升。
467+
// 默认关=边初值纯由建边来源决定(store.RELATION_SOURCE_DEFAULTS),演化
468+
// 不生效;开=被激活的边经 bumpRelationWeight 上涨(只加不减、封顶 1.0)。
469+
graphWeightEnabled: z.boolean().default(false),
470+
// 单次有效触达的抬升幅度。
471+
graphWeightDelta: z.number().min(0).max(1).default(0.1),
466472

467473
// --- sleep mode: idle-triggered deep maintenance (v0.4.0) ---------------
468474
// Opt-in, off by default. Unlike autoDream (threshold-triggered, lightweight)
@@ -712,6 +718,8 @@ const LIGHT_MODE_OFF = [
712718
"bm25SearchEnabled",
713719
// 轻量模式不开图召回轴(#219,依赖实体抽取产出;抽取本身已被关掉)。
714720
"entityRecallEnabled",
721+
// 轻量模式不开边权重演化(#24 块2,依赖实体抽取产出;抽取关则无 v08 边可抬升)。
722+
"graphWeightEnabled",
715723
// 轻量模式不开叙述条(额外 LLM 调用;#164 对齐,opt-in)。
716724
"dreamNarrativeEnabled",
717725
// 轻量模式不开 document 指针行(#230,opt-in:注册/注入/检索增强全随闸)。

‎dsh-mneme/lib/entities/extractor.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ export async function extractEntities(memory, { store, config, callLLM, logger }
226226
to_entity: toId,
227227
relation_type: rel.type,
228228
memory_id: memory.id,
229-
metadata: { model: options.model || "default" }
229+
metadata: { model: options.model || "default" },
230+
// issue #24 块2:LLM 抽取自动建边 → llm 来源(初值 0.4),区别于
231+
// 人工/确认的满权边。来源进 metadata 之上的独立列,演化与审计可辨。
232+
source: "llm"
230233
});
231234
savedRelations.push(saved);
232235
} catch (err) {

‎dsh-mneme/lib/store.js‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ CREATE TABLE IF NOT EXISTS entity_relations (
249249
relation_type TEXT NOT NULL,
250250
memory_id TEXT,
251251
created_at TEXT NOT NULL,
252-
metadata TEXT
252+
metadata TEXT,
253+
-- issue #24 块2(权重演化):边权重 + 建边来源。初值按 source 映射
254+
-- (manual 1.0 / confirmed 保持 / tag 0.3 / llm 0.4),演化只经
255+
-- bumpRelationWeight(只加不减、封顶),触达侧由 touch 门控驱动。
256+
weight REAL NOT NULL DEFAULT 1.0,
257+
source TEXT NOT NULL DEFAULT 'manual'
253258
);
254259
CREATE INDEX IF NOT EXISTS idx_relations_from ON entity_relations(from_entity);
255260
CREATE INDEX IF NOT EXISTS idx_relations_to ON entity_relations(to_entity);
@@ -549,6 +554,15 @@ function toRecallRun(row) {
549554
};
550555
}
551556

557+
// issue #24 块2:建边来源 → 初值权重映射。tag 延续 LLM 偏差取低起步,
558+
// llm 抽取自动建边取中低,manual 全权(1.0 是满权,不含演化余量的上限)。
559+
export const RELATION_SOURCE_DEFAULTS = Object.freeze({
560+
manual: 1.0,
561+
confirmed: 1.0, // manual_confirmed:已人工确认,维持满权
562+
tag: 0.3,
563+
llm: 0.4
564+
});
565+
552566
function toRecallEval(row) {
553567
if (!row) return undefined;
554568
let metrics;
@@ -612,6 +626,8 @@ function toRelation(row) {
612626
relation_type: row.relation_type,
613627
memory_id: row.memory_id ?? undefined,
614628
created_at: row.created_at,
629+
weight: row.weight ?? 1.0,
630+
source: row.source ?? "manual",
615631
metadata
616632
};
617633
}
@@ -751,6 +767,10 @@ export function createStore(path) {
751767
// 老库打开时 SCHEMA 的 CREATE TABLE 对既有表不生效,列还不存在,把索引写进 SCHEMA
752768
// 会直接报 no such column(与下方 llm_audit_logs.session_key 同理)。
753769
addColumn("memories", "content_hash", "ALTER TABLE memories ADD COLUMN content_hash TEXT");
770+
// issue #24 块2(权重演化):entity_relations 加 weight + source。存量关系
771+
// 行缺列 → 默认 manual 1.0(最保守:既有语义不因迁移而变弱)。
772+
addColumn("entity_relations", "weight", "ALTER TABLE entity_relations ADD COLUMN weight REAL NOT NULL DEFAULT 1.0");
773+
addColumn("entity_relations", "source", "ALTER TABLE entity_relations ADD COLUMN source TEXT NOT NULL DEFAULT 'manual'");
754774
// 列序是 content_hash 打头:哈希几乎唯一,等值 seek 就已收窄到候选行,type 只当同
755775
// 一次 seek 里的第二列过滤;反过来(type 打头)下面的存量回填就得全表扫——每次打开
756776
// 都要把全部正文读一遍。
@@ -2402,22 +2422,43 @@ export function createStore(path) {
24022422
/**
24032423
* Record a typed relation between two entities. metadata (optional) is a
24042424
* free-form JSON blob describing the relation. Relations are append-only.
2425+
*
2426+
* issue #24 块2(权重演化):source 决定边初值——manual 1.0 / confirmed
2427+
* 保持 / tag 0.3 / llm 0.4;显式传入 weight 时优先于 source 映射(manual
2428+
* 建边可自带权重)。哲学「关系是用出来的,不是建出来的」:初值只反映来源
2429+
* 偏见,真正的抬升留给 bumpRelationWeight(touch 门控驱动)。
24052430
*/
2406-
function saveRelation({ from_entity, to_entity, relation_type, memory_id, metadata }) {
2431+
function saveRelation({ from_entity, to_entity, relation_type, memory_id, metadata, weight, source }) {
24072432
const id = randomUUID();
24082433
const now = nowIso();
24092434
const metaStr = metadata === undefined
24102435
? null
24112436
: typeof metadata === "string"
24122437
? metadata
24132438
: JSON.stringify(metadata);
2439+
const src = source ?? "manual";
2440+
const w = weight ?? RELATION_SOURCE_DEFAULTS[src] ?? 1.0;
24142441
db.prepare(
2415-
`INSERT INTO entity_relations (id, from_entity, to_entity, relation_type, memory_id, created_at, metadata)
2416-
VALUES (?, ?, ?, ?, ?, ?, ?)`
2417-
).run(id, from_entity, to_entity, relation_type, memory_id ?? null, now, metaStr);
2442+
`INSERT INTO entity_relations (id, from_entity, to_entity, relation_type, memory_id, created_at, metadata, weight, source)
2443+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
2444+
).run(id, from_entity, to_entity, relation_type, memory_id ?? null, now, metaStr, w, src);
24182445
return toRelation(db.prepare("SELECT * FROM entity_relations WHERE id = ?").get(id));
24192446
}
24202447

2448+
/**
2449+
* 边权重演化(issue #24 块2)。只加不减、封顶 1.0——权重只能靠「被用」抬升、
2450+
* 不能靠被人为敲低,封顶防触达过频把边吹到无意义大。幂等(缺行直接返回 false)。
2451+
* delta 默认 0.1;调用方(touch 门控)决定何时算一次有效触达。**作用在关系行**,
2452+
* 与 memories 的 last_accessed_at 是两码事:这条是针对「这条边被激活了」的演化。
2453+
*/
2454+
function bumpRelationWeight(id, delta = 0.1) {
2455+
const row = db.prepare("SELECT weight FROM entity_relations WHERE id = ?").get(id);
2456+
if (!row) return false;
2457+
const next = Math.min(1.0, (row.weight ?? 1.0) + (delta ?? 0.1));
2458+
db.prepare("UPDATE entity_relations SET weight = ? WHERE id = ?").run(next, id);
2459+
return true;
2460+
}
2461+
24212462
/**
24222463
* Re-point every attr row whose memory_id is fromMemoryId to toMemoryId
24232464
* (autoDream merge migration, v0.3.0 Phase 4 / 4.3.2). When the keeper
@@ -2840,6 +2881,7 @@ export function createStore(path) {
28402881
findEntitiesMentionedIn,
28412882
getLinkedMemoryIds,
28422883
saveRelation,
2884+
bumpRelationWeight,
28432885
migrateAttrsToMemory,
28442886
getRelations,
28452887
setMirrorState,

‎dsh-mneme/src/config.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@ export const Config = z.object({
463463
// 检索融合池(与 BM25 同级的确认/回填信号)。默认关=检索行为与 #219 前
464464
// 逐字节一致;走 feature_flags 面板可启停,lightMode 强制关闭。
465465
entityRecallEnabled: z.boolean().default(false),
466+
// 边权重演化(issue #24 块2):touch 门控是否把被寻回路径的边权重抬升。
467+
// 默认关=边初值纯由建边来源决定(store.RELATION_SOURCE_DEFAULTS),演化
468+
// 不生效;开=被激活的边经 bumpRelationWeight 上涨(只加不减、封顶 1.0)。
469+
graphWeightEnabled: z.boolean().default(false),
470+
// 单次有效触达的抬升幅度。
471+
graphWeightDelta: z.number().min(0).max(1).default(0.1),
466472

467473
// --- sleep mode: idle-triggered deep maintenance (v0.4.0) ---------------
468474
// Opt-in, off by default. Unlike autoDream (threshold-triggered, lightweight)
@@ -712,6 +718,8 @@ const LIGHT_MODE_OFF = [
712718
"bm25SearchEnabled",
713719
// 轻量模式不开图召回轴(#219,依赖实体抽取产出;抽取本身已被关掉)。
714720
"entityRecallEnabled",
721+
// 轻量模式不开边权重演化(#24 块2,依赖实体抽取产出;抽取关则无 v08 边可抬升)。
722+
"graphWeightEnabled",
715723
// 轻量模式不开叙述条(额外 LLM 调用;#164 对齐,opt-in)。
716724
"dreamNarrativeEnabled",
717725
// 轻量模式不开 document 指针行(#230,opt-in:注册/注入/检索增强全随闸)。

‎dsh-mneme/src/entities/extractor.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ export async function extractEntities(memory, { store, config, callLLM, logger }
226226
to_entity: toId,
227227
relation_type: rel.type,
228228
memory_id: memory.id,
229-
metadata: { model: options.model || "default" }
229+
metadata: { model: options.model || "default" },
230+
// issue #24 块2:LLM 抽取自动建边 → llm 来源(初值 0.4),区别于
231+
// 人工/确认的满权边。来源进 metadata 之上的独立列,演化与审计可辨。
232+
source: "llm"
230233
});
231234
savedRelations.push(saved);
232235
} catch (err) {

‎dsh-mneme/src/store.js‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ CREATE TABLE IF NOT EXISTS entity_relations (
249249
relation_type TEXT NOT NULL,
250250
memory_id TEXT,
251251
created_at TEXT NOT NULL,
252-
metadata TEXT
252+
metadata TEXT,
253+
-- issue #24 块2(权重演化):边权重 + 建边来源。初值按 source 映射
254+
-- (manual 1.0 / confirmed 保持 / tag 0.3 / llm 0.4),演化只经
255+
-- bumpRelationWeight(只加不减、封顶),触达侧由 touch 门控驱动。
256+
weight REAL NOT NULL DEFAULT 1.0,
257+
source TEXT NOT NULL DEFAULT 'manual'
253258
);
254259
CREATE INDEX IF NOT EXISTS idx_relations_from ON entity_relations(from_entity);
255260
CREATE INDEX IF NOT EXISTS idx_relations_to ON entity_relations(to_entity);
@@ -549,6 +554,15 @@ function toRecallRun(row) {
549554
};
550555
}
551556

557+
// issue #24 块2:建边来源 → 初值权重映射。tag 延续 LLM 偏差取低起步,
558+
// llm 抽取自动建边取中低,manual 全权(1.0 是满权,不含演化余量的上限)。
559+
export const RELATION_SOURCE_DEFAULTS = Object.freeze({
560+
manual: 1.0,
561+
confirmed: 1.0, // manual_confirmed:已人工确认,维持满权
562+
tag: 0.3,
563+
llm: 0.4
564+
});
565+
552566
function toRecallEval(row) {
553567
if (!row) return undefined;
554568
let metrics;
@@ -612,6 +626,8 @@ function toRelation(row) {
612626
relation_type: row.relation_type,
613627
memory_id: row.memory_id ?? undefined,
614628
created_at: row.created_at,
629+
weight: row.weight ?? 1.0,
630+
source: row.source ?? "manual",
615631
metadata
616632
};
617633
}
@@ -751,6 +767,10 @@ export function createStore(path) {
751767
// 老库打开时 SCHEMA 的 CREATE TABLE 对既有表不生效,列还不存在,把索引写进 SCHEMA
752768
// 会直接报 no such column(与下方 llm_audit_logs.session_key 同理)。
753769
addColumn("memories", "content_hash", "ALTER TABLE memories ADD COLUMN content_hash TEXT");
770+
// issue #24 块2(权重演化):entity_relations 加 weight + source。存量关系
771+
// 行缺列 → 默认 manual 1.0(最保守:既有语义不因迁移而变弱)。
772+
addColumn("entity_relations", "weight", "ALTER TABLE entity_relations ADD COLUMN weight REAL NOT NULL DEFAULT 1.0");
773+
addColumn("entity_relations", "source", "ALTER TABLE entity_relations ADD COLUMN source TEXT NOT NULL DEFAULT 'manual'");
754774
// 列序是 content_hash 打头:哈希几乎唯一,等值 seek 就已收窄到候选行,type 只当同
755775
// 一次 seek 里的第二列过滤;反过来(type 打头)下面的存量回填就得全表扫——每次打开
756776
// 都要把全部正文读一遍。
@@ -2402,22 +2422,43 @@ export function createStore(path) {
24022422
/**
24032423
* Record a typed relation between two entities. metadata (optional) is a
24042424
* free-form JSON blob describing the relation. Relations are append-only.
2425+
*
2426+
* issue #24 块2(权重演化):source 决定边初值——manual 1.0 / confirmed
2427+
* 保持 / tag 0.3 / llm 0.4;显式传入 weight 时优先于 source 映射(manual
2428+
* 建边可自带权重)。哲学「关系是用出来的,不是建出来的」:初值只反映来源
2429+
* 偏见,真正的抬升留给 bumpRelationWeight(touch 门控驱动)。
24052430
*/
2406-
function saveRelation({ from_entity, to_entity, relation_type, memory_id, metadata }) {
2431+
function saveRelation({ from_entity, to_entity, relation_type, memory_id, metadata, weight, source }) {
24072432
const id = randomUUID();
24082433
const now = nowIso();
24092434
const metaStr = metadata === undefined
24102435
? null
24112436
: typeof metadata === "string"
24122437
? metadata
24132438
: JSON.stringify(metadata);
2439+
const src = source ?? "manual";
2440+
const w = weight ?? RELATION_SOURCE_DEFAULTS[src] ?? 1.0;
24142441
db.prepare(
2415-
`INSERT INTO entity_relations (id, from_entity, to_entity, relation_type, memory_id, created_at, metadata)
2416-
VALUES (?, ?, ?, ?, ?, ?, ?)`
2417-
).run(id, from_entity, to_entity, relation_type, memory_id ?? null, now, metaStr);
2442+
`INSERT INTO entity_relations (id, from_entity, to_entity, relation_type, memory_id, created_at, metadata, weight, source)
2443+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
2444+
).run(id, from_entity, to_entity, relation_type, memory_id ?? null, now, metaStr, w, src);
24182445
return toRelation(db.prepare("SELECT * FROM entity_relations WHERE id = ?").get(id));
24192446
}
24202447

2448+
/**
2449+
* 边权重演化(issue #24 块2)。只加不减、封顶 1.0——权重只能靠「被用」抬升、
2450+
* 不能靠被人为敲低,封顶防触达过频把边吹到无意义大。幂等(缺行直接返回 false)。
2451+
* delta 默认 0.1;调用方(touch 门控)决定何时算一次有效触达。**作用在关系行**,
2452+
* 与 memories 的 last_accessed_at 是两码事:这条是针对「这条边被激活了」的演化。
2453+
*/
2454+
function bumpRelationWeight(id, delta = 0.1) {
2455+
const row = db.prepare("SELECT weight FROM entity_relations WHERE id = ?").get(id);
2456+
if (!row) return false;
2457+
const next = Math.min(1.0, (row.weight ?? 1.0) + (delta ?? 0.1));
2458+
db.prepare("UPDATE entity_relations SET weight = ? WHERE id = ?").run(next, id);
2459+
return true;
2460+
}
2461+
24212462
/**
24222463
* Re-point every attr row whose memory_id is fromMemoryId to toMemoryId
24232464
* (autoDream merge migration, v0.3.0 Phase 4 / 4.3.2). When the keeper
@@ -2840,6 +2881,7 @@ export function createStore(path) {
28402881
findEntitiesMentionedIn,
28412882
getLinkedMemoryIds,
28422883
saveRelation,
2884+
bumpRelationWeight,
28432885
migrateAttrsToMemory,
28442886
getRelations,
28452887
setMirrorState,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// issue #24 块2 权重演化(entity_relations weight + source + bump)测试。
2+
// 覆盖:来源→初值映射(manual/tag/llm)/ weight 显式优先 / toRelation 回读 /
3+
// bumpRelationWeight 只加不减封顶 / extractor 建 llm 边 / 四来源全映射落表。
4+
import test from "node:test";
5+
import assert from "node:assert/strict";
6+
import { createStore, RELATION_SOURCE_DEFAULTS } from "../src/store.js";
7+
8+
function makeStore() {
9+
return createStore(":memory:");
10+
}
11+
12+
test("RELATION_SOURCE_DEFAULTS matches issue #24 mapping", () => {
13+
assert.equal(RELATION_SOURCE_DEFAULTS.manual, 1.0);
14+
assert.equal(RELATION_SOURCE_DEFAULTS.confirmed, 1.0);
15+
assert.equal(RELATION_SOURCE_DEFAULTS.tag, 0.3);
16+
assert.equal(RELATION_SOURCE_DEFAULTS.llm, 0.4);
17+
});
18+
19+
test("saveRelation maps source to initial weight", () => {
20+
const store = makeStore();
21+
const a = store.createEntity({ name: "A", type: "technology" });
22+
const b = store.createEntity({ name: "B", type: "technology" });
23+
const manual = store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "x", source: "manual" });
24+
const tag = store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "y", source: "tag" });
25+
const llm = store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "z", source: "llm" });
26+
assert.equal(manual.weight, 1.0);
27+
assert.equal(manual.source, "manual");
28+
assert.equal(tag.weight, 0.3);
29+
assert.equal(tag.source, "tag");
30+
assert.equal(llm.weight, 0.4);
31+
assert.equal(llm.source, "llm");
32+
});
33+
34+
test("explicit weight overrides source mapping", () => {
35+
const store = makeStore();
36+
const a = store.createEntity({ name: "A", type: "technology" });
37+
const b = store.createEntity({ name: "B", type: "technology" });
38+
const r = store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "x", source: "llm", weight: 0.7 });
39+
assert.equal(r.weight, 0.7);
40+
});
41+
42+
test("toRelation round-trips weight and source via getRelations", () => {
43+
const store = makeStore();
44+
const a = store.createEntity({ name: "A", type: "technology" });
45+
const b = store.createEntity({ name: "B", type: "technology" });
46+
store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "x", source: "tag" });
47+
const rels = store.getRelations(a.id);
48+
assert.equal(rels.length, 1);
49+
assert.equal(rels[0].weight, 0.3);
50+
assert.equal(rels[0].source, "tag");
51+
});
52+
53+
test("bumpRelationWeight raises weight, caps at 1.0, never lowers", () => {
54+
const store = makeStore();
55+
const a = store.createEntity({ name: "A", type: "technology" });
56+
const b = store.createEntity({ name: "B", type: "technology" });
57+
const r = store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "x", source: "llm" });
58+
assert.equal(store.bumpRelationWeight(r.id, 0.2), true);
59+
assert.equal(store.bumpRelationWeight(r.id, 0.2), true);
60+
assert.equal(store.bumpRelationWeight(r.id, 0.2), true); // 0.4→0.6→0.8→1.0
61+
assert.equal(store.bumpRelationWeight(r.id, 0.2), true);
62+
assert.equal(store.getRelations(a.id)[0].weight, 1.0, "capped");
63+
// 不存在的关系:幂等 false
64+
assert.equal(store.bumpRelationWeight("no-such-id"), false);
65+
});
66+
67+
test("legacy relation rows migrate with manual weight default (no crash)", () => {
68+
const store = makeStore();
69+
const a = store.createEntity({ name: "A", type: "technology" });
70+
const b = store.createEntity({ name: "B", type: "technology" });
71+
// 直接走底层 SQL 模拟旧库行(不带 weight/source 列 —— 但新 schema 已含列,
72+
// 所以这条主要验证 migration 幂等路径不崩 + 默认映射成立)。
73+
const r = store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "x" });
74+
assert.equal(r.weight, 1.0, "default source manual → weight 1.0");
75+
assert.equal(r.source, "manual");
76+
});

0 commit comments

Comments
 (0)