@modusensus/dsh-mneme 0.8.10 → 0.8.12

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 (45) hide show
  1. package/README.md +5 -5
  2. package/assets/icon.svg +3 -0
  3. package/lib/client.js +190 -58
  4. package/lib/config.js +47 -1
  5. package/lib/dream/decisions.js +16 -0
  6. package/lib/dream/sleep.js +3 -0
  7. package/lib/dream.js +57 -9
  8. package/lib/entities/extractor.js +4 -1
  9. package/lib/graph/anchoring.js +180 -0
  10. package/lib/index.js +5 -0
  11. package/lib/inject.js +15 -3
  12. package/lib/lang.js +10 -0
  13. package/lib/service.js +164 -23
  14. package/lib/settings.js +24 -2
  15. package/lib/store.js +155 -11
  16. package/lib/summarize.js +104 -7
  17. package/locale/en.json +6 -0
  18. package/locale/zh.json +6 -0
  19. package/package.json +12 -7
  20. package/scripts/sync-test-badge.mjs +9 -7
  21. package/scripts/test-count-sync.mjs +44 -0
  22. package/src/config.js +47 -1
  23. package/src/dream/decisions.js +16 -0
  24. package/src/dream/sleep.js +3 -0
  25. package/src/dream.js +57 -9
  26. package/src/entities/extractor.js +4 -1
  27. package/src/graph/anchoring.js +180 -0
  28. package/src/index.js +5 -0
  29. package/src/inject.js +15 -3
  30. package/src/lang.js +10 -0
  31. package/src/service.js +164 -23
  32. package/src/settings.js +24 -2
  33. package/src/store.js +155 -11
  34. package/src/summarize.js +104 -7
  35. package/test/api.test.js +7 -2
  36. package/test/baseline-persist.test.js +248 -0
  37. package/test/client.test.js +245 -45
  38. package/test/dream.test.js +75 -0
  39. package/test/graph-anchoring.test.js +185 -0
  40. package/test/graph-inject-budget.test.js +104 -0
  41. package/test/graph-passive-confirm.test.js +92 -0
  42. package/test/graph-weight-evolution.test.js +105 -0
  43. package/test/scope-inject-soft.test.js +106 -0
  44. package/test/summarize.test.js +177 -0
  45. package/test/test-count-sync.test.js +60 -0
package/src/store.js CHANGED
@@ -52,7 +52,13 @@ CREATE TABLE IF NOT EXISTS dream_runs (
52
52
  receipt TEXT NOT NULL,
53
53
  policy_epoch INTEGER NOT NULL DEFAULT 0, -- 裁决规则版本:规则升级后旧裁决降级为历史证据
54
54
  run_type TEXT NOT NULL DEFAULT 'auto', -- auto | sleep | organize:周期审计的类别区分
55
- skipped TEXT -- JSON: degraded 轮被跳过的逐条明细(index/action/ids/error)
55
+ skipped TEXT, -- JSON: degraded 轮被跳过的逐条明细(index/action/ids/error)
56
+ -- Issue #89(基线半边):本轮成功刷新后的活跃记忆「条数 / 正文字符数」——阈值判定的
57
+ -- 基线原本只活在调度器闭包内存里,进程重启归零 → 库里记忆多于阈值时,重启后第一条
58
+ -- 写入就绕过阈值开跑整轮(#291 只持久化了 lastRunAt 那半边)。成功轮才落这两列,
59
+ -- 失败/降级不推进基线的既有语义不变;两列同时有值或同时为 NULL。
60
+ store_count INTEGER,
61
+ store_chars INTEGER
56
62
  );
57
63
  CREATE INDEX IF NOT EXISTS idx_dream_runs_created ON dream_runs(created_at);
58
64
 
@@ -249,7 +255,12 @@ CREATE TABLE IF NOT EXISTS entity_relations (
249
255
  relation_type TEXT NOT NULL,
250
256
  memory_id TEXT,
251
257
  created_at TEXT NOT NULL,
252
- metadata TEXT
258
+ metadata TEXT,
259
+ -- issue #24 块2(权重演化):边权重 + 建边来源。初值按 source 映射
260
+ -- (manual 1.0 / confirmed 保持 / tag 0.3 / llm 0.4),演化只经
261
+ -- bumpRelationWeight(只加不减、封顶),触达侧由 touch 门控驱动。
262
+ weight REAL NOT NULL DEFAULT 1.0,
263
+ source TEXT NOT NULL DEFAULT 'manual'
253
264
  );
254
265
  CREATE INDEX IF NOT EXISTS idx_relations_from ON entity_relations(from_entity);
255
266
  CREATE INDEX IF NOT EXISTS idx_relations_to ON entity_relations(to_entity);
@@ -482,7 +493,10 @@ function toDreamRun(row) {
482
493
  policy_epoch: row.policy_epoch ?? 0,
483
494
  run_type: row.run_type ?? "auto",
484
495
  // Issue #104:degraded 轮的逐条跳过明细(JSON 列,NULL = 该轮无跳过项)。
485
- skipped: row.skipped ? JSON.parse(row.skipped) : undefined
496
+ skipped: row.skipped ? JSON.parse(row.skipped) : undefined,
497
+ // Issue #89(基线半边):成功轮落下的阈值基线(NULL = 该轮没有推进基线)。
498
+ store_count: row.store_count ?? undefined,
499
+ store_chars: row.store_chars ?? undefined
486
500
  };
487
501
  }
488
502
 
@@ -549,6 +563,15 @@ function toRecallRun(row) {
549
563
  };
550
564
  }
551
565
 
566
+ // issue #24 块2:建边来源 → 初值权重映射。tag 延续 LLM 偏差取低起步,
567
+ // llm 抽取自动建边取中低,manual 全权(1.0 是满权,不含演化余量的上限)。
568
+ export const RELATION_SOURCE_DEFAULTS = Object.freeze({
569
+ manual: 1.0,
570
+ confirmed: 1.0, // manual_confirmed:已人工确认,维持满权
571
+ tag: 0.3,
572
+ llm: 0.4
573
+ });
574
+
552
575
  function toRecallEval(row) {
553
576
  if (!row) return undefined;
554
577
  let metrics;
@@ -612,6 +635,8 @@ function toRelation(row) {
612
635
  relation_type: row.relation_type,
613
636
  memory_id: row.memory_id ?? undefined,
614
637
  created_at: row.created_at,
638
+ weight: row.weight ?? 1.0,
639
+ source: row.source ?? "manual",
615
640
  metadata
616
641
  };
617
642
  }
@@ -751,6 +776,10 @@ export function createStore(path) {
751
776
  // 老库打开时 SCHEMA 的 CREATE TABLE 对既有表不生效,列还不存在,把索引写进 SCHEMA
752
777
  // 会直接报 no such column(与下方 llm_audit_logs.session_key 同理)。
753
778
  addColumn("memories", "content_hash", "ALTER TABLE memories ADD COLUMN content_hash TEXT");
779
+ // issue #24 块2(权重演化):entity_relations 加 weight + source。存量关系
780
+ // 行缺列 → 默认 manual 1.0(最保守:既有语义不因迁移而变弱)。
781
+ addColumn("entity_relations", "weight", "ALTER TABLE entity_relations ADD COLUMN weight REAL NOT NULL DEFAULT 1.0");
782
+ addColumn("entity_relations", "source", "ALTER TABLE entity_relations ADD COLUMN source TEXT NOT NULL DEFAULT 'manual'");
754
783
  // 列序是 content_hash 打头:哈希几乎唯一,等值 seek 就已收窄到候选行,type 只当同
755
784
  // 一次 seek 里的第二列过滤;反过来(type 打头)下面的存量回填就得全表扫——每次打开
756
785
  // 都要把全部正文读一遍。
@@ -780,6 +809,11 @@ export function createStore(path) {
780
809
  // Issue #104:degraded(合法子集已应用)轮被跳过的决策明细。此前只进 logger.warn,
781
810
  // 离线回放 dream_runs 无法定位 degraded 成因(跨类型 merge / update 保护期 / unknown id)。
782
811
  addColumn("dream_runs", "skipped", "ALTER TABLE dream_runs ADD COLUMN skipped TEXT");
812
+ // Issue #89(基线半边):阈值基线跨重启持久化。存量行两列皆 NULL——历史行反推不出
813
+ // 「那轮结束时库有多大」(input_count 是窗口条数,不是库规模),所以升级后第一次
814
+ // 成功轮之前播种端拿不到基线,行为与升级前一致;跑过一轮之后闸门即跨重启生效。
815
+ addColumn("dream_runs", "store_count", "ALTER TABLE dream_runs ADD COLUMN store_count INTEGER");
816
+ addColumn("dream_runs", "store_chars", "ALTER TABLE dream_runs ADD COLUMN store_chars INTEGER");
783
817
 
784
818
  // Legacy mirror_state without v0.3.6 generation columns → add each missing
785
819
  // column idempotently (old DBs open cleanly, no data loss).
@@ -1605,17 +1639,26 @@ export function createStore(path) {
1605
1639
  // Issue #104:degraded 轮的跳过明细(JSON)。与 decisions/outcome 同为可选
1606
1640
  // 载荷,未提供时落 NULL(而不是 "[]"),审计行只记真实发生过的跳过。
1607
1641
  const skipped = run.skipped !== undefined ? JSON.stringify(run.skipped) : null;
1642
+ // Issue #89(基线半边):只有成功刷新过基线的 run 才带这两个值。非法/缺失一律落
1643
+ // NULL——播种端以「两列同时有值」为唯一认账条件,半个基线(只有条数没有字符数)
1644
+ // 会让阈值按 0 字符起算,比不认还糟。
1645
+ const baselineKnown =
1646
+ Number.isInteger(run.store_count) && run.store_count >= 0 &&
1647
+ Number.isInteger(run.store_chars) && run.store_chars >= 0;
1648
+ const storeCount = baselineKnown ? run.store_count : null;
1649
+ const storeChars = baselineKnown ? run.store_chars : null;
1608
1650
  db.prepare(
1609
1651
  `INSERT INTO dream_runs (id, created_at, status, error, provider, model, snapshot_hash,
1610
- input_count, input, decisions, outcome, applied, summary_stored, receipt, policy_epoch, run_type, skipped)
1611
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1652
+ input_count, input, decisions, outcome, applied, summary_stored, receipt, policy_epoch, run_type, skipped,
1653
+ store_count, store_chars)
1654
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1612
1655
  ON CONFLICT(id) DO UPDATE SET
1613
1656
  created_at=excluded.created_at, status=excluded.status, error=excluded.error,
1614
1657
  provider=excluded.provider, model=excluded.model, snapshot_hash=excluded.snapshot_hash,
1615
1658
  input_count=excluded.input_count, input=excluded.input, decisions=excluded.decisions,
1616
1659
  outcome=excluded.outcome, applied=excluded.applied, summary_stored=excluded.summary_stored,
1617
1660
  receipt=excluded.receipt, policy_epoch=excluded.policy_epoch, run_type=excluded.run_type,
1618
- skipped=excluded.skipped`
1661
+ skipped=excluded.skipped, store_count=excluded.store_count, store_chars=excluded.store_chars`
1619
1662
  ).run(
1620
1663
  id,
1621
1664
  run.created_at ?? now,
@@ -1633,7 +1676,9 @@ export function createStore(path) {
1633
1676
  run.receipt,
1634
1677
  policyEpoch,
1635
1678
  runType,
1636
- skipped
1679
+ skipped,
1680
+ storeCount,
1681
+ storeChars
1637
1682
  );
1638
1683
  return getDreamRun(id);
1639
1684
  }
@@ -1667,6 +1712,27 @@ export function createStore(path) {
1667
1712
  return Number.isFinite(ms) ? ms : 0;
1668
1713
  }
1669
1714
 
1715
+ // Issue #89(基线半边):上次成功刷新后的阈值基线(活跃条数 / 正文字符数)。与
1716
+ // lastDreamRunAt 同源——审计表就是事实源,不另开一份状态;差别只在只认「推进过
1717
+ // 基线」的行(两列同时非 NULL = 那一轮是 ok)。按 run_type 隔离:sleep/organize
1718
+ // 的轮次不参与 auto 的阈值判定,混用会让其余模块的库规模假扮成巩固基线。
1719
+ // 认不出行时返回 null,调用方退回零基线(= 升级前行为),绝不假装知道基线。
1720
+ function lastDreamBaseline(runType = null) {
1721
+ const where = "store_count IS NOT NULL AND store_chars IS NOT NULL";
1722
+ const row = runType
1723
+ ? db.prepare(
1724
+ `SELECT store_count, store_chars FROM dream_runs
1725
+ WHERE run_type = ? AND ${where}
1726
+ ORDER BY created_at DESC, id LIMIT 1`
1727
+ ).get(runType)
1728
+ : db.prepare(
1729
+ `SELECT store_count, store_chars FROM dream_runs
1730
+ WHERE ${where} ORDER BY created_at DESC, id LIMIT 1`
1731
+ ).get();
1732
+ if (!row) return null;
1733
+ return { count: row.store_count, chars: row.store_chars };
1734
+ }
1735
+
1670
1736
  /**
1671
1737
  * Latest ruling-rule version seen on the audit trail. policy_epoch is a config
1672
1738
  * value stamped onto each run by the caller; reading the newest row's epoch
@@ -2402,8 +2468,13 @@ export function createStore(path) {
2402
2468
  /**
2403
2469
  * Record a typed relation between two entities. metadata (optional) is a
2404
2470
  * free-form JSON blob describing the relation. Relations are append-only.
2471
+ *
2472
+ * issue #24 块2(权重演化):source 决定边初值——manual 1.0 / confirmed
2473
+ * 保持 / tag 0.3 / llm 0.4;显式传入 weight 时优先于 source 映射(manual
2474
+ * 建边可自带权重)。哲学「关系是用出来的,不是建出来的」:初值只反映来源
2475
+ * 偏见,真正的抬升留给 bumpRelationWeight(touch 门控驱动)。
2405
2476
  */
2406
- function saveRelation({ from_entity, to_entity, relation_type, memory_id, metadata }) {
2477
+ function saveRelation({ from_entity, to_entity, relation_type, memory_id, metadata, weight, source }) {
2407
2478
  const id = randomUUID();
2408
2479
  const now = nowIso();
2409
2480
  const metaStr = metadata === undefined
@@ -2411,13 +2482,36 @@ export function createStore(path) {
2411
2482
  : typeof metadata === "string"
2412
2483
  ? metadata
2413
2484
  : JSON.stringify(metadata);
2485
+ const src = source ?? "manual";
2486
+ // 只认映射表的自有键:src="constructor"/"toString" 会命中原型链上的函数,直接查表
2487
+ // 拿到的是函数而非权重(绑进 SQL 就是整条写入抛错),必须回退默认 1.0。
2488
+ const w = weight ?? (Object.hasOwn(RELATION_SOURCE_DEFAULTS, src) ? RELATION_SOURCE_DEFAULTS[src] : 1.0);
2414
2489
  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);
2490
+ `INSERT INTO entity_relations (id, from_entity, to_entity, relation_type, memory_id, created_at, metadata, weight, source)
2491
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
2492
+ ).run(id, from_entity, to_entity, relation_type, memory_id ?? null, now, metaStr, w, src);
2418
2493
  return toRelation(db.prepare("SELECT * FROM entity_relations WHERE id = ?").get(id));
2419
2494
  }
2420
2495
 
2496
+ /**
2497
+ * 边权重演化(issue #24 块2)。只加不减、封顶 1.0——权重只能靠「被用」抬升、
2498
+ * 不能靠被人为敲低,封顶防触达过频把边吹到无意义大。
2499
+ * 下界守在这里而不是调用方:增量为负/非有限数一律拒绝(返回 false)。限幅只留
2500
+ * 上界、把「只加不减」托付给配置范围,等于把存储接口的契约挂在调用方自觉上。
2501
+ * 读改写合并成单条 UPDATE(MIN(1.0, weight + ?)):memoryDir 可被多进程共用,
2502
+ * 先 SELECT 再 UPDATE 会丢并发增量,还会拿旧值把别人已抬升的权重压回去。
2503
+ * 幂等(缺行 changes=0 → false);delta 默认 0.1,由调用方(touch 门控)决定
2504
+ * 何时算一次有效触达。**作用在关系行**,与 memories 的 last_accessed_at 是两码
2505
+ * 事:这条是针对「这条边被激活了」的演化。
2506
+ */
2507
+ function bumpRelationWeight(id, delta = 0.1) {
2508
+ if (!Number.isFinite(delta) || delta < 0) return false;
2509
+ const result = db.prepare(
2510
+ "UPDATE entity_relations SET weight = MIN(1.0, weight + ?) WHERE id = ?"
2511
+ ).run(delta, id);
2512
+ return result.changes > 0;
2513
+ }
2514
+
2421
2515
  /**
2422
2516
  * Re-point every attr row whose memory_id is fromMemoryId to toMemoryId
2423
2517
  * (autoDream merge migration, v0.3.0 Phase 4 / 4.3.2). When the keeper
@@ -2459,6 +2553,52 @@ export function createStore(path) {
2459
2553
  ).all(entityId, entityId).map(toRelation);
2460
2554
  }
2461
2555
 
2556
+ /**
2557
+ /**
2558
+ * 批量邻接(issue #24 · 块1 锚定层级联):给定多条实体 id,一并取全部关系行
2559
+ * 并归并成「每个实体 → 相邻实体 id 去重列表」的邻接表。供 anchoring.js 的
2560
+ * cascadeDepths 使用——多实体一族只需要一次 SQL(IN 查询),比逐实体调
2561
+ * getRelations 的 N 次往返省。邻接是无向的(from/to 双向),往返不重复。
2562
+ * 返回 Map<entityId, Array<entityId>>;未命中的实体缺省为 absent(级联视为
2563
+ * 无邻居,Map 里不放键同行,null 同等对待)。
2564
+ */
2565
+ function getEntityNeighbors(entityIds) {
2566
+ const out = new Map();
2567
+ const ids = Array.isArray(entityIds) ? entityIds.filter(Boolean) : [];
2568
+ if (!ids.length) return out;
2569
+ const placeholders = ids.map(() => "?").join(",");
2570
+ const rows = db.prepare(
2571
+ `SELECT from_entity, to_entity FROM entity_relations
2572
+ WHERE from_entity IN (${placeholders}) OR to_entity IN (${placeholders})`
2573
+ ).all(...ids, ...ids);
2574
+ for (const r of rows) {
2575
+ addNeighbor(out, r.from_entity, r.to_entity, ids);
2576
+ addNeighbor(out, r.to_entity, r.from_entity, ids);
2577
+ }
2578
+ return out;
2579
+ }
2580
+
2581
+ function addNeighbor(out, anchor, neighbor, queryIds) {
2582
+ if (!queryIds.includes(anchor)) return; // 只以查询过的实体为锚
2583
+ if (neighbor === anchor) return; // 自环不构成扩散边
2584
+ if (!out.has(anchor)) out.set(anchor, []);
2585
+ const list = out.get(anchor);
2586
+ if (!list.includes(neighbor)) list.push(neighbor);
2587
+ }
2588
+
2589
+ /**
2590
+ * 按记忆反查关联边(issue #24 块4 被动确认):一条记忆被召回/注入(触达)
2591
+ * 时,挂在同一记忆上的关系行即「这条关系被用过了」的证据——正常触达视为
2592
+ * 被动确认,调用方据此 bumpRelationWeight。返回 toRelation 行(含 weight/
2593
+ * source)。索引 idx_relations_memory 已建(#219)。
2594
+ */
2595
+ function getRelationsByMemory(memoryId) {
2596
+ if (!memoryId) return [];
2597
+ return db.prepare(
2598
+ "SELECT * FROM entity_relations WHERE memory_id = ?"
2599
+ ).all(memoryId).map(toRelation);
2600
+ }
2601
+
2462
2602
  /** All entities (optionally name-filtered, newest first). Used by sleep phase 4
2463
2603
  * orphan detection: an entity with zero relations is a candidate for relation
2464
2604
  * completion. */
@@ -2788,6 +2928,7 @@ export function createStore(path) {
2788
2928
  searchVector,
2789
2929
  saveDreamRun,
2790
2930
  lastDreamRunAt,
2931
+ lastDreamBaseline,
2791
2932
  getDreamRun,
2792
2933
  listDreamRuns,
2793
2934
  getLatestPolicyEpoch,
@@ -2840,8 +2981,11 @@ export function createStore(path) {
2840
2981
  findEntitiesMentionedIn,
2841
2982
  getLinkedMemoryIds,
2842
2983
  saveRelation,
2984
+ bumpRelationWeight,
2843
2985
  migrateAttrsToMemory,
2844
2986
  getRelations,
2987
+ getEntityNeighbors,
2988
+ getRelationsByMemory,
2845
2989
  setMirrorState,
2846
2990
  getMirrorState,
2847
2991
  markMirrorDirty,
package/src/summarize.js CHANGED
@@ -15,10 +15,17 @@ function parseSummaryJsonResult(raw) {
15
15
  const end = text.lastIndexOf("]");
16
16
  if (start === -1 || end === -1 || end <= start) return { ok: false, entries: [] };
17
17
  let arr;
18
+ let salvaged = false;
18
19
  try {
19
20
  arr = JSON.parse(text.slice(start, end + 1));
20
21
  } catch {
21
- return { ok: false, entries: [] };
22
+ // issue #339 / E8 实测:10-20% 的窗口输出了含记忆条目的完整对象,只因中段
23
+ // 一处语法错误被整窗 JSON.parse 拒收;而失败路径的生产行为是游标不推进 +
24
+ // 温度 0 重试同文同错 = 该窗口记忆静默丢失。这里做失败路径修复:括号配对
25
+ // 扫描截出完整的顶层对象逐个 parse,救活多少算多少;截出 0 个不视为「模型
26
+ // 显式说无内容」(那是 ok:true 专属于真实空数组的语义)。
27
+ arr = salvageArrayItems(text.slice(start, end + 1));
28
+ salvaged = true;
22
29
  }
23
30
  if (!Array.isArray(arr)) return { ok: false, entries: [] };
24
31
  const VALID = new Set(["preference", "project", "decision", "history", "rejected_solution", "pitfall", "constraint"]);
@@ -38,8 +45,92 @@ function parseSummaryJsonResult(raw) {
38
45
  importance: Number.isInteger(item.importance) ? Math.min(5, Math.max(1, item.importance)) : 3
39
46
  }));
40
47
  // 空数组表示模型明确判断本轮没有可沉淀内容;非空数组若全部无效,
41
- // 则不能消费窗口,否则无效输出会永久推进 seq 游标。
42
- return { ok: arr.length === 0 || entries.length > 0, entries };
48
+ // 则不能消费窗口,否则无效输出会永久推进 seq 游标。salvage 路径
49
+ // 没有「显式空数组」可言——ok 只看是否救回了条目。
50
+ return {
51
+ ok: salvaged ? entries.length > 0 : (arr.length === 0 || entries.length > 0),
52
+ entries,
53
+ salvaged
54
+ };
55
+ }
56
+
57
+ /**
58
+ * Salvage scanner for malformed JSON arrays (issue #339): extracts every
59
+ * complete top-level {...} span, parsing each independently. Two passes,
60
+ * merged with de-duplication:
61
+ * - pass 1 is string-aware (handles valid objects whose string values
62
+ * legitimately contain braces);
63
+ * - pass 2 ignores string state entirely — E8 现场的坏对象带奇数个引号
64
+ * (stray quote),pass 1 的奇偶失配会把后续对象的收尾 } 吞进字符串里,
65
+ * 盲扫按括号深度截取反而能救回它们。坏对象两种扫法都 parse 失败,自然
66
+ * 被丢弃;两遍的去重靠序列化键。
67
+ */
68
+ function salvageArrayItems(chunk) {
69
+ const out = [];
70
+ const seen = new Set();
71
+ const push = (obj) => {
72
+ if (!obj || typeof obj !== "object" || Array.isArray(obj)) return;
73
+ const key = JSON.stringify(obj);
74
+ if (!seen.has(key)) { seen.add(key); out.push(obj); }
75
+ };
76
+ for (const obj of scanBraceSpans(chunk, true)) push(obj);
77
+ for (const obj of scanBraceSpans(chunk, false)) push(obj);
78
+ return out;
79
+ }
80
+
81
+ function scanBraceSpans(chunk, respectStrings) {
82
+ // 修订(CodeRabbit review on #350):只接受「最外层数组的直接子对象」——
83
+ // 候选 { 必须满足 arrayDepth===1 && objDepth===0 且前一非空白 token 是 [ 或
84
+ // ,。否则 (a) 嵌套子数组里的对象会被误捞成顶层记忆;(b) 盲扫遇到字符串值
85
+ // 里的 } 会提前断 span,把属性值对象当独立记忆落库。误拦的代价只是少救回
86
+ // (安全侧),误捞的代价是写进假记忆。
87
+ const items = [];
88
+ let arrayDepth = 0;
89
+ let objDepth = 0;
90
+ let inString = false;
91
+ let escape = false;
92
+ let objStart = -1;
93
+ let prev = "";
94
+ for (let i = 0; i < chunk.length; i++) {
95
+ const ch = chunk[i];
96
+ if (respectStrings && inString) {
97
+ if (escape) escape = false;
98
+ else if (ch === "\\") escape = true;
99
+ else if (ch === '"') inString = false;
100
+ continue;
101
+ }
102
+ if (respectStrings && ch === '"') { inString = true; continue; }
103
+ if (ch === "[") {
104
+ arrayDepth++;
105
+ prev = "[";
106
+ continue;
107
+ }
108
+ if (ch === "]") {
109
+ arrayDepth = Math.max(0, arrayDepth - 1);
110
+ prev = "]";
111
+ continue;
112
+ }
113
+ if (ch === "{") {
114
+ if (arrayDepth === 1 && objDepth === 0 && (prev === "[" || prev === ",")) {
115
+ objStart = i;
116
+ }
117
+ objDepth++;
118
+ prev = "{";
119
+ continue;
120
+ }
121
+ if (ch === "}") {
122
+ objDepth = Math.max(0, objDepth - 1);
123
+ if (objDepth === 0 && objStart !== -1) {
124
+ try { items.push(JSON.parse(chunk.slice(objStart, i + 1))); } catch { /* 坏对象跳过 */ }
125
+ objStart = -1;
126
+ }
127
+ prev = "}";
128
+ continue;
129
+ }
130
+ if (ch === ",") { prev = ","; continue; }
131
+ if (!/\s/.test(ch)) prev = ch;
132
+ }
133
+ return items;
43
134
  }
44
135
 
45
136
  /** Extract a JSON array from LLM output that may contain prose around it. */
@@ -195,9 +286,14 @@ function collectMessages(session, maxChars = 8000, language = "zh", afterSeq, th
195
286
  break;
196
287
  }
197
288
  case "tool/result": {
198
- const result = Array.isArray(data?.message?.content)
199
- ? data.message.content.find((block) => block?.type === "tool-result")
200
- : undefined;
289
+ // 工具结果有两种形状:旧形状把载荷与 isError 装在 content 内的
290
+ // `type:"tool-result"` 块上;DSH 0.2.x 起内核改为直接投递 `role:"tool"`
291
+ // 消息本身,content 里只有 `type:"text"` 块、isError 挂在消息上。只认
292
+ // 旧块形状时新形状落进 undefined 分支,工具输出与工具报错被静默丢弃。
293
+ const message = data?.message;
294
+ const blocks = Array.isArray(message?.content) ? message.content : [];
295
+ const legacy = blocks.find((block) => block?.type === "tool-result");
296
+ const result = legacy ?? (message?.role === "tool" ? message : undefined);
201
297
  const out = textOf(result?.content);
202
298
  const status = result?.isError === true ? STR.statusFail[language] : STR.statusOk[language];
203
299
  lines.push(STR.transcriptToolResult[language](status, trim(out, 500)));
@@ -678,9 +774,10 @@ export function createSummarizer(ctx, service, config, deps = {}) {
678
774
  // 空数组是合法成功:没有记忆写入,但本次事件窗口仍然应被持久消费。
679
775
  persistCursor(session.id, nextSeq);
680
776
  }
681
- if (audit && (capped > 0 || deduped > 0)) {
777
+ if (audit && (capped > 0 || deduped > 0 || parsedResult.salvaged)) {
682
778
  audit.metadata = {
683
779
  parsed: parsed.length,
780
+ ...(parsedResult.salvaged ? { json_salvaged: true } : {}),
684
781
  ...(capped > 0 ? { capped } : {}),
685
782
  ...(deduped > 0 ? { deduped, mode: dedupeMode, maxSim: Number(dedupeMaxSim.toFixed(4)) } : {})
686
783
  };
package/test/api.test.js CHANGED
@@ -669,10 +669,15 @@ test("GET /api/dsh-mneme/features returns empty overrides and effective config d
669
669
  // issue #239 第 4 项镜像到巩固新增 dreamPeakHours/dreamPeakMaxDeferMinutes,
670
670
  // issue #292 新增 autoDreamFailureBackoff、
671
671
  // issue #254 新增 writeAdmission.enabled/writeAdmission.enforce,
672
- // issue #34 恢复(#333)新增 injectTimePrefix)
673
- assert.equal(Object.keys(data.effective).length, 59 + 3 + 2 + 1 + 2 + 2 + 2 + 1 + 1 + 1 + 2 + 1 + 2 + 1);
672
+ // issue #34 恢复(#333)新增 injectTimePrefix、
673
+ // issue #24 块1 新增 graphAnchoringEnabled/graphSeedCap/graphCascadeDepth、
674
+ // 块2 新增 graphWeightEnabled/graphWeightDelta、块3 新增 graphInjectHint/graphInjectBudget、
675
+ // 块4 新增 graphPassiveConfirm、
676
+ // issue #339 新增 dreamMergeGuard)
677
+ assert.equal(Object.keys(data.effective).length, 59 + 3 + 2 + 1 + 2 + 2 + 2 + 1 + 1 + 1 + 2 + 1 + 2 + 1 + 3 + 2 + 2 + 1 + 1);
674
678
  assert.equal(data.effective.dreamSkipInvalid, true);
675
679
  assert.equal(data.effective.allowCrossTypeMerge, false);
680
+ assert.equal(data.effective.dreamMergeGuard, false);
676
681
  assert.equal(data.effective.dreamMinIntervalMinutes, 0);
677
682
  assert.equal(data.effective.autoDreamFailureBackoff, false);
678
683
  assert.equal(data.effective.dreamMaxTokens, 131072);