@@ -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);
254259CREATE INDEX IF NOT EXISTS idx_relations_from ON entity_relations(from_entity);
255260CREATE 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+
552566function 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,
0 commit comments