@modusensus/dsh-mneme 0.8.11 → 0.8.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +5 -5
  2. package/assets/icon.svg +3 -3
  3. package/lib/api.js +2 -0
  4. package/lib/client.js +12 -1
  5. package/lib/config.js +66 -1
  6. package/lib/dream/decisions.js +16 -0
  7. package/lib/dream/sleep.js +3 -0
  8. package/lib/dream.js +57 -9
  9. package/lib/entities/extractor.js +4 -1
  10. package/lib/graph/anchoring.js +180 -0
  11. package/lib/guide.js +13 -1
  12. package/lib/index.js +18 -5
  13. package/lib/inject.js +15 -3
  14. package/lib/lang.js +10 -0
  15. package/lib/sensitive-scan.js +209 -0
  16. package/lib/service.js +203 -35
  17. package/lib/settings.js +29 -3
  18. package/lib/store.js +155 -11
  19. package/lib/summarize.js +104 -7
  20. package/locale/en.json +6 -6
  21. package/locale/zh.json +6 -6
  22. package/package.json +114 -113
  23. package/scripts/sync-test-badge.mjs +9 -7
  24. package/scripts/test-count-sync.mjs +44 -0
  25. package/src/api.js +2 -0
  26. package/src/config.js +66 -1
  27. package/src/dream/decisions.js +16 -0
  28. package/src/dream/sleep.js +3 -0
  29. package/src/dream.js +57 -9
  30. package/src/entities/extractor.js +4 -1
  31. package/src/graph/anchoring.js +180 -0
  32. package/src/guide.js +13 -1
  33. package/src/index.js +18 -5
  34. package/src/inject.js +15 -3
  35. package/src/lang.js +10 -0
  36. package/src/sensitive-scan.js +209 -0
  37. package/src/service.js +203 -35
  38. package/src/settings.js +29 -3
  39. package/src/store.js +155 -11
  40. package/src/summarize.js +104 -7
  41. package/test/api.test.js +15 -2
  42. package/test/baseline-persist.test.js +248 -0
  43. package/test/dream.test.js +75 -0
  44. package/test/graph-anchoring.test.js +185 -0
  45. package/test/graph-inject-budget.test.js +104 -0
  46. package/test/graph-passive-confirm.test.js +92 -0
  47. package/test/graph-weight-evolution.test.js +105 -0
  48. package/test/inject-pools.test.js +19 -0
  49. package/test/scope-inject-soft.test.js +106 -0
  50. package/test/scope-strict.test.js +112 -0
  51. package/test/sensitive-scan.test.js +207 -0
  52. package/test/summarize.test.js +177 -0
  53. package/test/test-count-sync.test.js +60 -0
  54. package/test/write-admission.test.js +115 -0
package/src/service.js CHANGED
@@ -11,6 +11,7 @@ import { createDocumentRegistrar } from "./document.js";
11
11
  import { createOrganizer } from "./organize.js";
12
12
  import { createBM25Index } from "./search/bm25.js";
13
13
  import { adaptiveThreshold } from "./search/adaptive.js";
14
+ import { anchorSeeds, normalizePath, cascadeDepths, HOP_QUOTA } from "./graph/anchoring.js";
14
15
 
15
16
  const INJECT_TYPES = new Set(["preference", "project", "decision", "summary", "rejected_solution", "pitfall", "constraint"]);
16
17
 
@@ -358,6 +359,22 @@ export function createService({ store, mirror, config, onWrite, logger, document
358
359
  }
359
360
  }
360
361
 
362
+ /**
363
+ * v0.8.0 A3(issue #17)strictScope 硬过滤的**唯一实现**。三处共用:融合池、
364
+ * `entity:` 与 `attr:` 两条前缀路。
365
+ *
366
+ * 抽成单一实现是为了让「过滤点写在哪」不再漂移——那两条前缀路在 searchMemories
367
+ * 的入口就 return(见那里的路由块),早于融合池那道过滤,曾经整条绕过硬墙;三处
368
+ * 各写一份判断,只会让下一个新增通路再漏一次(同 #349 把阈值口径收进
369
+ * activeStoreSize 的理由:两处各写一份过滤条件,漂移只在某条路径上显形)。
370
+ *
371
+ * strictScope 关(默认)或 scope 解析不到时原样返回:行为与改动前逐字节一致。
372
+ */
373
+ function gateByScope(rows, scope) {
374
+ if (config?.strictScope !== true || !scope) return rows;
375
+ return rows.filter((m) => isVisibleInScope(m, scope));
376
+ }
377
+
361
378
  /**
362
379
  * Search for memories attached to a named entity (v0.3.0 Phase 3).
363
380
  * 合并优先级:entity_attrs.memory_id 精确关联 = 1.0 > 关键词提及 = 0.7;
@@ -365,23 +382,34 @@ export function createService({ store, mirror, config, onWrite, logger, document
365
382
  * @param {string} entityName
366
383
  * @param {object} [options]
367
384
  * @param {number} [options.topK=20]
385
+ * @param {object|null} [options.scope] 当前会话 scope,供 strictScope 闸门使用
368
386
  * @returns {any[]}
369
387
  */
370
- function searchByEntity(entityName, { topK = 20 } = {}) {
388
+ function searchByEntity(entityName, { topK = 20, scope = null } = {}) {
371
389
  const entity = store.findEntityByName(entityName);
372
390
  if (!entity) return [];
373
391
  const attrs = store.getCurrentAttrs(entity.id);
374
392
  const memoryIds = [...new Set(attrs.map((a) => a.memory_id).filter(Boolean))];
375
393
  const attrHits = memoryIds.map((id) => store.getById(id)).filter(Boolean);
376
- const keywordHits = store.search(entityName, { limit: topK });
394
+ // 关键词这一路的窗口取两倍:闸门在截断之前生效,出局的候选会让位,窗口按最终条数
395
+ // 取就会不够(与融合路给向量检索取 lim * 2 同一个理由)。没有候选被闸掉时结果与
396
+ // 取 topK 逐字节一致——多出来的是排在后面的低分候选,进不了 topK。
397
+ const keywordHits = store.search(entityName, { limit: topK * 2 });
377
398
  const merged = new Map();
378
399
  for (const mem of attrHits) merged.set(mem.id, { ...mem, _source: "entity_attr", _score: 1.0 });
379
400
  for (const mem of keywordHits) {
380
401
  if (!merged.has(mem.id)) merged.set(mem.id, { ...mem, _source: "keyword", _score: 0.7 });
381
402
  }
382
- const hits = Array.from(merged.values()).sort((a, b) => b._score - a._score).slice(0, topK);
383
- touchRecalled(hits);
384
- return hits;
403
+ // 先过闸、再截 topK:反过来做的话,出局的候选会占掉名额、可见的补不进来——topK=1
404
+ // 而首位出局时直接返回空数组。排序本身不动,只是把闸门插在截断之前,与融合池那条
405
+ // 路同序(那边也是先 filter 后 slice)。
406
+ const ranked = Array.from(merged.values()).sort((a, b) => b._score - a._score);
407
+ const visible = gateByScope(ranked, scope).slice(0, topK);
408
+ // 闸门还必须在 touchRecalled **之前**:放在后面的话,一次越权检索照样会给出局的
409
+ // 行刷回温时钟,并在被动确认开启时 bump 它们的关联边——命中反馈落到了调用方
410
+ // 本不该看见的行上。
411
+ touchRecalled(visible);
412
+ return visible;
385
413
  }
386
414
 
387
415
  /**
@@ -391,17 +419,19 @@ export function createService({ store, mirror, config, onWrite, logger, document
391
419
  * @param {string | undefined} value
392
420
  * @param {object} [options]
393
421
  * @param {number} [options.topK=20]
422
+ * @param {object|null} [options.scope] 当前会话 scope,供 strictScope 闸门使用
394
423
  * @returns {any[]}
395
424
  */
396
- function searchByAttr(key, value, { topK = 20 } = {}) {
425
+ function searchByAttr(key, value, { topK = 20, scope = null } = {}) {
397
426
  if (!key) return [];
398
427
  // value 可能为 undefined(attr:key 无 = 值):归一为空串后交给
399
428
  // store.findMemoriesByAttr —— 空 value 契约 = 返回该 attr_key 的全部
400
429
  // 当前有效记忆(v0.3.0,store.js 已实现)。
401
430
  const rows = store.findMemoriesByAttr(key, value ?? "");
402
- const hits = rows.slice(0, topK);
403
- touchRecalled(hits);
404
- return hits;
431
+ // 同 searchByEntity:先过闸再截 topK,且闸门在 touchRecalled 之前。
432
+ const visible = gateByScope(rows, scope).slice(0, topK);
433
+ touchRecalled(visible);
434
+ return visible;
405
435
  }
406
436
 
407
437
  /**
@@ -448,30 +478,92 @@ export function createService({ store, mirror, config, onWrite, logger, document
448
478
  }
449
479
 
450
480
  /**
451
- * Graph fourth recall path (issue #219). When the query text mentions a
452
- * known entity name, memories linked to that entity (attrs + relations)
453
- * join the fusion pool as a confirm/backfill signal — same standing as
454
- * BM25, never dominating the semantic ranking. Tier scores: attr-linked
455
- * 1.0 (direct evidence, same source searchByEntity trusts) > relation
456
- * edge 0.9. Archived/forgotten rows never participate. Failures degrade
457
- * to [] — a recall booster, never a correctness gate (same contract as
458
- * bm25Recall).
481
+ * Graph fourth recall path (issue #219 → #24 块1 锚定层). When the query text
482
+ * mentions a known entity name, memories linked to that entity (attrs +
483
+ * relations) join the fusion pool as a confirm/backfill signal — same standing
484
+ * as BM25, never dominating the semantic ranking. Tier scores: attr-linked
485
+ * 1.0 (direct evidence, same source searchByEntity trusts) > relation edge
486
+ * 0.9. Archived/forgotten rows never participate. Failures degrade to [] —
487
+ * a recall booster, never a correctness gate (same contract as bm25Recall).
488
+ *
489
+ * #24 升级(graphAnchoringEnabled):命中实体作为锚定种子,沿 entity_relations
490
+ * 级联扩散邻居(默认深度 2),邻居挂联的记忆以 hop 配额权重参与——把「关系是
491
+ * 用出来的」延伸成检索信号:查询命中的不只有直接挂联,还有跳跃可达的活跃子图。
492
+ * 邻居权重 = HOP_QUOTA[hop] × linked tier(attr 1.0 / relation 0.9),仍低于
493
+ * 直达种子的满权,绝不喧宾夺主。
459
494
  */
460
495
  function entityRecall(q, limit) {
461
496
  if (config?.entityRecallEnabled !== true) return [];
462
497
  try {
463
498
  const entities = store.findEntitiesMentionedIn(q);
464
499
  if (!entities.length) return [];
465
- const linked = store.getLinkedMemoryIds(entities.map((e) => e.id));
466
- if (!linked.size) return [];
467
- const hits = [];
468
- for (const [id, tier] of linked) {
469
- const row = store.getById(id);
470
- if (!row || row.archived || row.forgotten) continue;
471
- hits.push({ ...row, score: tier === "attr" ? 1.0 : 0.9, source: "entity" });
472
- if (hits.length >= limit * 2) break;
500
+ // 关闭锚定级联=逐字节复用 #219 单跳轴:种子裁剪/重排/截断全是 #24 新增的
501
+ // 行为,默认关时一条都不许漏进旧路径(graphSeedCap 也只在开启后生效——
502
+ // 无论 cap 调多小,关档时命中实体仍全量挂联)。
503
+ if (config?.graphAnchoringEnabled !== true) {
504
+ const linked = store.getLinkedMemoryIds(entities.map((e) => e.id));
505
+ if (!linked.size) return [];
506
+ const hits = [];
507
+ for (const [id, tier] of linked) {
508
+ const row = store.getById(id);
509
+ if (!row || row.archived || row.forgotten) continue;
510
+ hits.push({ ...row, score: tier === "attr" ? 1.0 : 0.9, source: "entity" });
511
+ if (hits.length >= limit * 2) break;
512
+ }
513
+ return hits;
473
514
  }
474
- return hits;
515
+ // 锚定种子(精确名布尔通路——命名实体逻辑上是离散匹配,不参与 min-max)。
516
+ const exact = normalizePath(entities.map((e) => e.id), { path: "exact" });
517
+ const seeds = anchorSeeds({ paths: [exact], cap: config?.graphSeedCap ?? 12 });
518
+ if (!seeds.length) return [];
519
+ // 0-hop 直达:挂联记忆按 tier 定分(attr 1.0 / relation 0.9)——与 #219
520
+ // 单跳轴同分。MAX 语义:同一条记忆被多实体挂联时保留最高分。
521
+ const scored = new Map();
522
+ const push = (memId, score) => {
523
+ if (scored.has(memId) && scored.get(memId) >= score) return;
524
+ const row = store.getById(memId);
525
+ if (!row || row.archived || row.forgotten) return;
526
+ scored.set(memId, score);
527
+ };
528
+ const seedLinked = store.getLinkedMemoryIds(seeds.map((s) => s.id));
529
+ for (const [memId, tier] of seedLinked) {
530
+ push(memId, tier === "attr" ? 1.0 : 0.9);
531
+ }
532
+ // 级联:沿 entity_relations 走 BFS,邻居挂联的记忆以 hop 配额权重补位——
533
+ // 不覆盖种子直达分(MAX 语义保证),只填「种子里没有但邻居可达」的槽。
534
+ const seedIds = seeds.map((s) => s.id);
535
+ const adjacency = store.getEntityNeighbors(seedIds);
536
+ const depths = cascadeDepths({
537
+ seeds,
538
+ // 逐节点懒取邻居:初次查询只带种子那一层的邻接,BFS 走到 1-hop 节点时
539
+ // 必须按需再拉一层,否则 >1 跳永远拿到空邻居、graphCascadeDepth≥2 形同
540
+ // 虚设。节点数随图度数增长,热路径上以 graphCascadeDepth≤3 为天然上界,
541
+ // 真遇到大图再考虑查询配额。
542
+ adjacencyOf: (id) => {
543
+ if (!adjacency.has(id)) adjacency.set(id, store.getEntityNeighbors([id]).get(id) ?? []);
544
+ return adjacency.get(id);
545
+ },
546
+ maxDepth: config?.graphCascadeDepth ?? 2
547
+ });
548
+ let neighborsSeen = 0;
549
+ const neighborCap = (config?.graphSeedCap ?? 12) * HOP_QUOTA[1]; // 1-hop 配额即为邻居总量上界
550
+ for (const [nid, hop] of depths) {
551
+ if (hop < 1) continue;
552
+ if (neighborsSeen >= Math.max(1, Math.floor(neighborCap))) break;
553
+ neighborsSeen++;
554
+ const neighborLinked = store.getLinkedMemoryIds([nid]);
555
+ for (const [memId, tier] of neighborLinked) {
556
+ const tierW = tier === "attr" ? 1.0 : 0.9;
557
+ push(memId, HOP_QUOTA[Math.min(hop, 3)] * tierW);
558
+ }
559
+ }
560
+ const hits = [...scored.entries()]
561
+ .map(([id, score]) => {
562
+ const row = store.getById(id);
563
+ return { ...row, score, source: "entity" };
564
+ })
565
+ .sort((a, b) => b.score - a.score || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
566
+ return hits.slice(0, limit * 2);
475
567
  } catch {
476
568
  return [];
477
569
  }
@@ -530,11 +622,28 @@ export function createService({ store, mirror, config, onWrite, logger, document
530
622
  * on the hot recall path). A touch failure must never break search/inject.
531
623
  */
532
624
  function touchRecalled(memories) {
533
- if (config?.heatEnabled === false || !Array.isArray(memories) || memories.length === 0) return;
625
+ if (!Array.isArray(memories) || memories.length === 0) return;
626
+ // heatEnabled=false 只关 touchLastAccess 的热度消费(既有语义),但 #24
627
+ // 块4 被动确认独立于 heat——graphPassiveConfirm 开时照常 bump 关联边。
628
+ // graphWeightEnabled 是块2 立的演化总闸:它关着时任何触达都不该改边权,被动
629
+ // 确认只是它底下的一个通道(后续块还会接别的确认通道),两键同开才 bump,
630
+ // 否则单开通道键就等于绕过「演化默认关」的承诺。
631
+ // 两条路径都 best-effort,失败绝不阻断检索/注入。
632
+ const heatOn = config?.heatEnabled !== false;
633
+ const passive = config?.graphPassiveConfirm === true && config?.graphWeightEnabled === true;
634
+ const delta = config?.graphWeightDelta ?? 0.1;
534
635
  for (const m of memories) {
535
636
  if (!m?.id) continue;
536
637
  try {
537
- store.touchLastAccess(m.id);
638
+ if (heatOn) store.touchLastAccess(m.id);
639
+ if (passive) {
640
+ // 正常触达=对该记忆挂联关系边的被动确认;bump 只加不减封顶 1.0,
641
+ // 自激回路由封顶天然遏制。「仅异常路径暴露给用户复核」的确认记录
642
+ // 侧在此落地,复核 UI 留后续块。
643
+ for (const rel of store.getRelationsByMemory(m.id)) {
644
+ store.bumpRelationWeight(rel.id, delta);
645
+ }
646
+ }
538
647
  } catch { /* touch is best effort */ }
539
648
  }
540
649
  }
@@ -816,9 +925,7 @@ export function createService({ store, mirror, config, onWrite, logger, document
816
925
  // (区别于 A2 的降权保留可见);未标注行与命中行保留。strict 与 A2 加权
817
926
  // 叠加:过滤后剩下的命中行仍吃加成。scope 未传(flag 关)或完全解析不到
818
927
  // 时跳过——identity 为空的对象({null,null})按 fail-closed 过滤。
819
- if (config.strictScope === true && scope) {
820
- merged = merged.filter((m) => isVisibleInScope(m, scope));
821
- }
928
+ merged = gateByScope(merged, scope);
822
929
  // v0.8.0 A2:occurred_at 时间过滤——在融合池上先滤再 dedup/slice,rerank
823
930
  // 只看窗内候选,topK 槽位不被窗外行占用。
824
931
  const occurredBounds = updatedAtBounds(occurredFrom, occurredTo);
@@ -1427,6 +1534,14 @@ export function createService({ store, mirror, config, onWrite, logger, document
1427
1534
  ? new Map(filtered.map((m) => [m.id, computeHeat(m, Date.now(), config)]))
1428
1535
  : null;
1429
1536
  const heatOf = (m) => (heatMap ? heatMap.get(m.id) ?? 1 : 1);
1537
+ // v0.8.x(issue #339 / E7 实测):A2 软加权补齐到注入通道。此前 ×0.5/×1.25
1538
+ // 只作用于 searchMemories——E7 考卷里「explicit 标注 + 软档」的注入集与无标注
1539
+ // 逐条相同(80/80),主泄露面上软档形同虚设。门控与检索侧同款:scopeEnabled
1540
+ // 且当前会话至少一维可解析;strictScope 硬过滤在下方先行,硬墙开启时被滤行
1541
+ // 不会到这里被二次降权。未激活时乘 1,排序与改动前逐字节一致。
1542
+ const softScopeActive = config?.scopeEnabled === true && scope != null &&
1543
+ Boolean(scope.agent_scope || scope.workspace_scope);
1544
+ const scopeMultOf = (m) => (softScopeActive ? scopeMultiplier(m, scope) : 1);
1430
1545
  const items = filtered.sort((a, b) => {
1431
1546
  // 编码记忆在编码任务时优先于普通 decision(与 preference 同级),
1432
1547
  // importance 乘 codingBoostFactor 加权(封顶 5,保持 importance 语义)。
@@ -1447,8 +1562,11 @@ export function createService({ store, mirror, config, onWrite, logger, document
1447
1562
  : m.importance;
1448
1563
  const pa = priority(a);
1449
1564
  const pb = priority(b);
1565
+ // 软加权乘在层内数值积上(priority 档位不动):foreign ×0.5 后压不过
1566
+ // 同档自己行——E7 的同 importance 档设计正是这个场景。
1450
1567
  return pa - pb ||
1451
- (effImportance(b) * qualityWeight(b) * heatOf(b)) - (effImportance(a) * qualityWeight(a) * heatOf(a));
1568
+ (effImportance(b) * qualityWeight(b) * heatOf(b) * scopeMultOf(b)) -
1569
+ (effImportance(a) * qualityWeight(a) * heatOf(a) * scopeMultOf(a));
1452
1570
  });
1453
1571
  let candidates = items;
1454
1572
  if (config.hybridInject !== false && q) {
@@ -1493,6 +1611,13 @@ export function createService({ store, mirror, config, onWrite, logger, document
1493
1611
  semanticItems.push(hit);
1494
1612
  }
1495
1613
  }
1614
+ // issue #339(CodeRabbit review on #350):首轮 BM25 兜底领跑合并池,
1615
+ // 而 selectiveInject 相似度重排要等查询向量就绪才执行——软加权必须
1616
+ // 在这里就位,否则 foreign 命中绕过降权直接占注入位。
1617
+ if (softScopeActive) {
1618
+ semanticItems.sort((a, b) =>
1619
+ (b.score ?? 0) * scopeMultOf(b) - (a.score ?? 0) * scopeMultOf(a));
1620
+ }
1496
1621
  }
1497
1622
  if (semanticItems.length) {
1498
1623
  const seen = new Set();
@@ -1523,7 +1648,13 @@ export function createService({ store, mirror, config, onWrite, logger, document
1523
1648
  const hits = vectorIndex.search(queryVector, { limit: 200, threshold: 0 });
1524
1649
  const sim = new Map(hits.map((m) => [m.id, m.score ?? 0]));
1525
1650
  if (sim.size) {
1526
- candidates = [...candidates].sort((a, b) => (sim.get(b.id) ?? -1) - (sim.get(a.id) ?? -1));
1651
+ // 软加权作用在真实 sim 上;未命中的规则候选保持 -1 沉底(缺失项
1652
+ // 乘乘数会让 foreign 缺失项反而排到自己的缺失项之上,故不乘)。
1653
+ const scopedSim = (m) => {
1654
+ const s = sim.get(m.id);
1655
+ return s === undefined ? -1 : s * scopeMultOf(m);
1656
+ };
1657
+ candidates = [...candidates].sort((a, b) => scopedSim(b) - scopedSim(a));
1527
1658
  }
1528
1659
  } catch { /* topic re-rank unavailable: keep rule-based order */ }
1529
1660
  }
@@ -1532,6 +1663,26 @@ export function createService({ store, mirror, config, onWrite, logger, document
1532
1663
  if (config?.strictScope === true && scope) {
1533
1664
  candidates = candidates.filter((m) => isVisibleInScope(m, scope));
1534
1665
  }
1666
+ // #24 块3:图召回候选打标。entityRecall 只在 entityRecallEnabled 开启时
1667
+ // 有产出,这里为命中者贴 graphHint 标签——注入侧据此(a)graphInjectHint
1668
+ // 开=标成 [检索线索] 前缀的线索行(独立预算),(b)关=线索行只参与排序
1669
+ // 不改变注入块(保守档)。与检索侧共用同一图闸:块1 #341 把 entityRecall
1670
+ // 升级成级联后,注入侧自动吃到同样的扩散信号,无需再改。只在语义/规则候选
1671
+ // 为空时兜底检索,避免注入路径双倍候选成本。
1672
+ if (config?.entityRecallEnabled === true && q) {
1673
+ try {
1674
+ const graphHits = entityRecall(q, maxItems * 2);
1675
+ if (graphHits.length) {
1676
+ const graphIds = new Set(graphHits.map((m) => m.id));
1677
+ candidates = [
1678
+ ...candidates.map((m) => graphIds.has(m.id) ? { ...m, graphHint: true } : m),
1679
+ ...graphHits
1680
+ .filter((m) => !candidates.some((c) => c.id === m.id))
1681
+ .map((m) => ({ ...m, graphHint: true }))
1682
+ ];
1683
+ }
1684
+ } catch { /* graph hint is best-effort */ }
1685
+ }
1535
1686
  // #249 第一批:B1 pin 池。取在相关性排序之后、轮换之前——取谁按此刻的候选
1536
1687
  // 次序(即相关性次序),取到后从候选中摘除,于是下面的轮换重排碰不到它们
1537
1688
  // (验收:pin 不参与跨轮轮换)。独立预算的两层意义:pin 既不占 maxItems
@@ -1539,7 +1690,10 @@ export function createService({ store, mirror, config, onWrite, logger, document
1539
1690
  // 超预算的条数回报给调用方,在块内如实标注(绝不静默省略)。
1540
1691
  const pinnedBudget = Math.max(0, Math.min(5, Math.floor(config?.pinnedInjectBudget ?? 0)));
1541
1692
  // eligible 留到块外:未展示条数要等 general 槽选完才算得准(见 selected 之后)。
1542
- const eligible = pinnedBudget > 0 ? candidates.filter((m) => PINNED_MEMORY_TYPES.has(m.type)) : [];
1693
+ // graphHint 行不进 pin 池:pin 是「每轮必进的约束/偏好」,而线索行按定义是
1694
+ // 「链路信息、非事实断言」,且 pin 长在候选池遍历之前——不排除的话,保守档
1695
+ // (graphInjectHint 关)也能从 pin 侧把它放回注入块,线索开关就被绕过去了。
1696
+ const eligible = pinnedBudget > 0 ? candidates.filter((m) => PINNED_MEMORY_TYPES.has(m.type) && m.graphHint !== true) : [];
1543
1697
  let pinned = [];
1544
1698
  if (pinnedBudget > 0 && eligible.length > 0) {
1545
1699
  pinned = eligible.slice(0, pinnedBudget);
@@ -1560,9 +1714,23 @@ export function createService({ store, mirror, config, onWrite, logger, document
1560
1714
  // 注入块变成文档目录(#164 失败判据:批量把历史塞进上下文)。
1561
1715
  const documentBudget = config?.documentInjectBudget ?? 2;
1562
1716
  let documentSeen = 0;
1717
+ // #24 块3:图线索行(graphHint)按独立预算 graphInjectBudget 进块;所谓
1718
+ // 独立=既不受 document 预算约束、也不挤占 maxItems 槽位。graphInjectHint
1719
+ // 关(默认保守档)时线索行**不进注入块**——它们只参与候选池排序(对融合无
1720
+ // 影响,因为候选池生成的唯一用途就是注入),桥接信号不污染常驻文本;开时
1721
+ // 才作为 [检索线索] 前缀行进入。与 document/pin 同构:预算只约束注入。
1722
+ const hintOpen = config?.graphInjectHint === true;
1723
+ let graphSeen = 0;
1724
+ const graphBudget = hintOpen ? Math.max(0, Math.floor(config?.graphInjectBudget ?? 1)) : 0;
1563
1725
  const general = [];
1564
1726
  for (const m of candidates) {
1565
- if (general.length >= maxItems) break;
1727
+ if (m.graphHint === true) {
1728
+ if (!hintOpen) continue; // 保守档:线索只排序不进块
1729
+ if (graphSeen >= graphBudget) continue; // 独立预算内放行
1730
+ graphSeen++;
1731
+ } else if (general.length >= maxItems) {
1732
+ break; // 普通候选受 maxItems 槽位上界
1733
+ }
1566
1734
  if (m.type === "document") {
1567
1735
  if (documentSeen >= documentBudget) continue;
1568
1736
  documentSeen++;
package/src/settings.js CHANGED
@@ -58,6 +58,18 @@ const FEATURE_FLAG_BOOLEANS = [
58
58
  // Issue #219:图召回轴——查询命中实体名时把挂联记忆并入检索融合池
59
59
  // (默认关;依赖实体抽取产出,lightMode 强制关闭)。
60
60
  "entityRecallEnabled",
61
+ // Issue #24 块1:图谱锚定层级联——命中实体作种子、沿关系表向邻居扩散
62
+ // (默认关;依赖实体抽取产出,lightMode 强制关闭)。
63
+ "graphAnchoringEnabled",
64
+ // Issue #24 块2:边权重演化总闸——关着时任何触达都不改边权(默认关;
65
+ // 依赖实体抽取产出,lightMode 强制关闭)。块4 的被动确认是它底下的一个通道。
66
+ "graphWeightEnabled",
67
+ // Issue #24 块3:关联提示防幻觉——图召回线索行以「[检索线索]」标注进注入
68
+ // (默认关=线索只参与排序不进块;依赖实体抽取产出,lightMode 强制关闭)。
69
+ "graphInjectHint",
70
+ // Issue #24 块4:被动确认通道——正常触达即对该记忆挂联的边 bump 一格
71
+ // (默认关;需总闸 graphWeightEnabled 同开)。
72
+ "graphPassiveConfirm",
61
73
  // Issue #164:叙述条——dream 期间按 tag 主题簇合成叙述落库(source=
62
74
  // narrative,evidence 回链簇内记忆;按需检索不常驻注入;默认关)。
63
75
  "dreamNarrativeEnabled",
@@ -88,6 +100,9 @@ const FEATURE_FLAG_BOOLEANS = [
88
100
  // Issue #89:宽容校验回归(默认开)+ 跨类型合并显式放宽(默认关)。
89
101
  "dreamSkipInvalid",
90
102
  "allowCrossTypeMerge",
103
+ // Issue #339 / E8:guarded 类型 merge 护栏(默认关)——合并对象命中
104
+ // 长保留类型的 merge 决策整条跳过。
105
+ "dreamMergeGuard",
91
106
  // Issue #17(v0.8.0 A1):scope 隔离存储层总开关。开启后写入标注
92
107
  // agent_scope/workspace_scope、去重键扩展(含 sensitivity);检索侧过滤在
93
108
  // A2/A3。默认关=行为与 A1 前逐字节一致。
@@ -108,7 +123,11 @@ const FEATURE_FLAG_BOOLEANS = [
108
123
  // enforce 真拦;都默认关。同上,点号键平铺存、合并时展开回
109
124
  // writeAdmission 对象。
110
125
  "writeAdmission.enabled",
111
- "writeAdmission.enforce"
126
+ "writeAdmission.enforce",
127
+ // Issue #164 A2:写入边界的密钥 / PII 判据(src/sensitive-scan.js)。与上面两个
128
+ // 键分层——本键决定「这类判据参不参与」(默认关),命中之后是仅告警还是真拦仍由
129
+ // writeAdmission.enforce 决定(#164 口径:默认仅告警、拦截 opt-in)。
130
+ "sensitiveScanEnabled"
112
131
  ];
113
132
  // 整数开关的闭区间,与 config.js 里 z.natural().min().max() 对齐。
114
133
  const FEATURE_FLAG_INT_RANGES = {
@@ -143,7 +162,12 @@ const FEATURE_FLAG_INT_RANGES = {
143
162
  // Issue #257:sleep 冲突/模式阶段的 LLM 输出预算(原硬编码 2048,实测不足)。
144
163
  sleepMaxTokens: [256, 131072],
145
164
  // Issue #258:总览(dream_summarize)输入条数硬上限(0 = 不设上限)。
146
- dreamSummaryMaxInputs: [0, 100000]
165
+ dreamSummaryMaxInputs: [0, 100000],
166
+ // Issue #24 块1:锚定种子上限与级联深度(闭区间与 config.js 的 z.natural() 对齐)。
167
+ graphSeedCap: [1, 30],
168
+ graphCascadeDepth: [1, 3],
169
+ // Issue #24 块3:图线索行的独立注入预算(0 = 线索不进块)。
170
+ graphInjectBudget: [0, 5]
147
171
  };
148
172
  // 浮点开关的闭区间(与 config.js 的 z.number().min().max() 对齐)。与整数开关
149
173
  // 分开:面板的整数控件要求 Number.isInteger,而余弦相似度阈值必须允许小数。
@@ -151,7 +175,9 @@ const FEATURE_FLAG_NUMBER_RANGES = {
151
175
  // Issue #127:vector 去重档的并入阈值。
152
176
  summarizeDedupeMinSim: [0.5, 0.99],
153
177
  // Issue #125:hybrid 判"高相似"的阈值。
154
- dreamCandidateMinSim: [0.5, 0.99]
178
+ dreamCandidateMinSim: [0.5, 0.99],
179
+ // Issue #24 块2:单次有效触达的边权抬升幅度(与 config.js 的闭区间对齐)。
180
+ graphWeightDelta: [0, 1]
155
181
  };
156
182
  // 自由字符串开关(与 config.js 的 z.string() 同名同型):trim 后 ≤200 字符,
157
183
  // 空串合法(= 跟随主对话模型/默认路径,面板显示 placeholder)。