@modusensus/dsh-mneme 0.5.2 → 0.6.0

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.
package/lib/config.js CHANGED
@@ -4,6 +4,11 @@ export const Config = z.object({
4
4
  memoryDir: z.string().default("~/.dsh/memory"),
5
5
  autoInject: z.boolean().default(true),
6
6
  autoSummarize: z.boolean().default(true),
7
+ // Session lifecycle (v0.6.0): when enabled, deleting/disposing a session also
8
+ // archives every memory that was born in it (treating the session as a save
9
+ // point — entries stay recoverable via memory_archive/restoreBySession).
10
+ // Default OFF: legacy behavior, a disposed session leaves its memories active.
11
+ sessionLifecycleEnabled: z.boolean().default(false),
7
12
  // Optional model override for summarization. When both are non-empty, they
8
13
  // take priority over the session's current model. Empty = use the session's
9
14
  // active provider/model (same as before).
@@ -19,11 +24,13 @@ export const Config = z.object({
19
24
  dreamModel: z.string(),
20
25
  dreamMaxTokens: z.natural().min(256).max(131072).default(8192),
21
26
  // Pass-through reasoning effort for dream's LLM calls. 'none' (default)
22
- // omits the field so the provider's own default applies; low/medium/high
23
- // are forwarded verbatim. Useful to cap reasoning spend on thinking-type
24
- // models that would otherwise drain the whole token budget and return an
25
- // empty body ("no json array in llm output").
27
+ // omits the field so the provider's own default applies; 'off' explicitly
28
+ // disables thinking REQUIRED for thinking-type models (deepseek-v4-flash
29
+ // etc.) that would otherwise drain the whole token budget into reasoning and
30
+ // return an empty body ("no json array in llm output"); low/medium/high are
31
+ // forwarded verbatim to cap reasoning spend.
26
32
  dreamReasoningEffort: z.union([
33
+ z.const("off"),
27
34
  z.const("low"),
28
35
  z.const("medium"),
29
36
  z.const("high"),
@@ -197,9 +204,11 @@ export const Config = z.object({
197
204
  sleepProvider: z.string().default(""),
198
205
  sleepModel: z.string().default(""),
199
206
  // Pass-through reasoning effort for sleep's LLM passes, same semantics as
200
- // dreamReasoningEffort: 'none' (default) omits the field; low/medium/high
201
- // are forwarded verbatim.
207
+ // dreamReasoningEffort: 'none' (default) omits the field; 'off' explicitly
208
+ // disables thinking (thinking-type models would burn the whole budget on
209
+ // reasoning); low/medium/high are forwarded verbatim.
202
210
  sleepReasoningEffort: z.union([
211
+ z.const("off"),
203
212
  z.const("low"),
204
213
  z.const("medium"),
205
214
  z.const("high"),
@@ -108,7 +108,7 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
108
108
  }
109
109
  const strictness = config.sleepConflictStrictness ?? "normal";
110
110
  const threshold = CONFLICT_THRESHOLDS[strictness] ?? CONFLICT_THRESHOLDS.normal;
111
- const memories = service.all().filter((m) => !m.archived && !m.forgotten && m.type !== "summary");
111
+ const memories = service.all().filter((m) => !m.archived && !m.session_disposed_at && !m.forgotten && m.type !== "summary");
112
112
  if (memories.length < 2) return { status: "skipped", reason: "too few memories" };
113
113
  if (signal?.aborted) return { status: "aborted", reason: "user activity" };
114
114
 
@@ -239,7 +239,7 @@ function phaseDemotion(service, config, logger, runId, signal = null) {
239
239
  const archived = [];
240
240
  for (const m of service.all()) {
241
241
  if (signal?.aborted) break;
242
- if (m.archived || m.forgotten) continue;
242
+ if (m.archived || m.forgotten || m.session_disposed_at) continue;
243
243
  const ref = m.last_accessed_at ?? m.updated_at ?? m.created_at;
244
244
  if (!ref) continue;
245
245
  const t = new Date(ref).getTime();
@@ -273,7 +273,7 @@ async function phasePatterns(ctx, service, config, logger, runId, signal = null)
273
273
  const limit = config.sleepPatternMinMemories ?? 100;
274
274
  const memories = service
275
275
  .list({ limit: 200, includeForgotten: false })
276
- .filter((m) => !m.archived && m.type !== "summary" && m.type !== "pattern")
276
+ .filter((m) => !m.archived && !m.session_disposed_at && m.type !== "summary" && m.type !== "pattern")
277
277
  .sort((a, b) => (a.updated_at < b.updated_at ? 1 : -1))
278
278
  .slice(0, limit);
279
279
  if (memories.length === 0) return { status: "skipped", reason: "no memories to scan" };
@@ -342,7 +342,7 @@ function phaseRelations(service, config, logger, runId, signal = null) {
342
342
  if (entities.length < 2) return { status: "skipped", reason: "too few entities" };
343
343
  const orphans = entities.filter((e) => (service.getRelations(e.id) ?? []).length === 0);
344
344
  if (orphans.length === 0) return { status: "skipped", reason: "no orphan entities" };
345
- const memories = service.all().filter((m) => !m.archived && !m.forgotten);
345
+ const memories = service.all().filter((m) => !m.archived && !m.session_disposed_at && !m.forgotten);
346
346
  const seen = new Set();
347
347
  const related = [];
348
348
  const MAX_RELATIONS_PER_ORPHAN = 3;
package/lib/dream.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { validateDecisions, applyDecisions } from "./dream/decisions.js";
2
2
  import { clusterMemories, findPotentialConflicts } from "./dream/clustering.js";
3
3
  import { createHash, randomUUID } from "node:crypto";
4
- export { validateDecisions, applyDecisions };
4
+ export { validateDecisions, applyDecisions, normalizeDecisions };
5
5
 
6
6
 
7
7
  // Extract the first JSON array from LLM output, tolerating markdown fences,
@@ -40,6 +40,95 @@ function extractJsonArray(text) {
40
40
  }
41
41
  return null;
42
42
  }
43
+
44
+ /**
45
+ * Field-name drift guard (v0.5.3): thinking-type models (deepseek-v4-flash
46
+ * etc.) occasionally ignore the prompt's exact decision schema and emit alias
47
+ * keys —实测方案 A 输出 "consolidation"/"target_ids"、方案 B 输出
48
+ * "action"/"targetIds",都不是插件要求的 "action"/"ids"。normalizeDecisions
49
+ * 在 validateDecisions 之前把常见变体重写回规范字段名,让"字段名不听话但
50
+ * 语义正确"的输出仍可被应用,而不是整单被拒。覆盖三类漂移:
51
+ * 1. 整个 body 是包在对象里的数组({consolidation:[...]} /
52
+ * {decisions:[...]} / {actions:[...]});
53
+ * 2. 决策对象用了别名键(target_ids→ids、keep_source→keepSource、
54
+ * winner_id→winner、targetIds→ids 等);
55
+ * 3. action 值用了同义词(archived→archive、consolidation→merge 等)。
56
+ * 无归一化必要时原样返回(null→null,调用方"no json array"分支不受影响)。
57
+ */
58
+ const FIELD_ALIASES = {
59
+ // "consolidation" 也可作 action 键(实测 deepseek-v4-flash 这么写过);
60
+ // 但 normalizeOne 对 action 键做字符串过滤,{consolidation:[...]} 这种
61
+ // wrapper 数组不会被误当成 action(顶层 WRAPPER_KEYS 负责解包)。
62
+ action: ["action", "decision", "operation", "op", "action_type", "actionType", "mode", "consolidation"],
63
+ // 注意:action 别名不含 "type"——create 决策的 type 是合法的记忆类型字段,
64
+ // 不能误当 action。
65
+ ids: ["ids", "target_ids", "targetIds", "targets", "memory_ids", "memoryIds", "id_list", "idList", "memories"],
66
+ reason: ["reason", "rationale", "why", "comment", "note", "explanation"],
67
+ importance: ["importance", "priority", "weight", "level", "score"],
68
+ keepSource: ["keepSource", "keep_source", "source_id", "sourceId", "keeper", "keep_id", "keepId"],
69
+ winner: ["winner", "winner_id", "winnerId", "win_id", "winId", "preferred", "primary"],
70
+ loser: ["loser", "loser_id", "loserId", "lose_id", "loseId", "drop_id", "dropId", "archive_id", "archiveId"],
71
+ title: ["title", "new_title", "newTitle", "merged_title", "mergedTitle", "merge_title", "mergeTitle"],
72
+ content: ["content", "new_content", "newContent", "merged_content", "mergedContent", "merge_content", "mergeContent"],
73
+ evidence: ["evidence", "evidence_ids", "evidenceIds"]
74
+ };
75
+
76
+ // 保守的 action 值同义词:只收语义无歧义、不可能被误认为记忆类型/其他 action 的映射。
77
+ const ACTION_SYNONYMS = {
78
+ archived: "archive",
79
+ remove: "archive",
80
+ delete: "archive",
81
+ combine: "merge",
82
+ consolidation: "merge",
83
+ consolidate: "merge",
84
+ modify: "update",
85
+ edit: "update"
86
+ };
87
+
88
+ const WRAPPER_KEYS = ["consolidation", "decisions", "actions", "results", "updates", "list", "data"];
89
+
90
+ /** 把单个决策对象重写到规范字段名;非对象原样返回。 */
91
+ function normalizeOne(raw) {
92
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return raw;
93
+ const d = {};
94
+ const consumed = new Set();
95
+ for (const [canon, aliases] of Object.entries(FIELD_ALIASES)) {
96
+ for (const key of aliases) {
97
+ if (key in raw && raw[key] !== undefined && raw[key] !== null) {
98
+ // action 必须是字符串——{consolidation:[...]} 的 wrapper 数组跳过。
99
+ if (canon === "action" && Array.isArray(raw[key])) continue;
100
+ d[canon] = raw[key];
101
+ consumed.add(key);
102
+ break;
103
+ }
104
+ }
105
+ }
106
+ // 保留未被别名消费的原始键(create 的 type、未来字段)原样透传。
107
+ for (const key of Object.keys(raw)) {
108
+ if (!consumed.has(key)) d[key] = raw[key];
109
+ }
110
+ if (typeof d.action === "string") {
111
+ const low = d.action.toLowerCase();
112
+ if (ACTION_SYNONYMS[low] !== undefined) d.action = ACTION_SYNONYMS[low];
113
+ }
114
+ // ids 若被写成单个字符串则包成数组(validateDecisions 要求数组)。
115
+ if (d.ids !== undefined && typeof d.ids === "string") d.ids = [d.ids];
116
+ return d;
117
+ }
118
+
119
+ /** 归一化 LLM 决策 body(数组 / 包裹对象 / 单个决策对象),null 原样返回。 */
120
+ function normalizeDecisions(raw) {
121
+ if (!raw) return raw;
122
+ if (Array.isArray(raw)) return raw.map(normalizeOne).filter((d) => d && typeof d === "object");
123
+ if (typeof raw === "object") {
124
+ for (const key of WRAPPER_KEYS) {
125
+ if (Array.isArray(raw[key])) return normalizeDecisions(raw[key]);
126
+ }
127
+ // 单个决策对象 → 包成单元素数组(validateDecisions 要求数组)。
128
+ return [normalizeOne(raw)];
129
+ }
130
+ return raw;
131
+ }
43
132
  const SUMMARY_PROMPT = `你是记忆库摘要助手。根据整理后的记忆,生成一段 150-200 字的记忆库总览,覆盖:用户偏好、活跃项目、关键决策。之后作为会话上下文注入。只输出摘要文本,不要其他内容。`;
44
133
 
45
134
  const CONSOLIDATION_PROMPT = `你是记忆库整理助手。下面是全部记忆条目(id、类型、标题、内容、重要性、更新时间)。
@@ -406,7 +495,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
406
495
  let inFlight = null;
407
496
 
408
497
  function shouldTrigger(service) {
409
- const memories = service.all().filter((m) => !m.archived && m.type !== "summary");
498
+ const memories = service.all().filter((m) => !m.archived && !m.session_disposed_at && m.type !== "summary");
410
499
  const count = memories.length;
411
500
  const chars = totalChars(memories);
412
501
  const overBase = count >= baseline.count + thresholdCount || chars >= baseline.chars + thresholdChars;
@@ -616,7 +705,10 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
616
705
  return finish({ ok: false, error: "llm failed", summary: false });
617
706
  }
618
707
 
619
- const decisions = extractJsonArray(decisionText);
708
+ // v0.5.3 字段名归一化兜底:thinking 模型可能输出别名键/wrapper 对象,
709
+ // 在 validateDecisions 之前重写到规范字段名,语义正确但 schema 不听话的
710
+ // 输出不再整单被拒。
711
+ const decisions = normalizeDecisions(extractJsonArray(decisionText));
620
712
  if (!Array.isArray(decisions)) {
621
713
  logger?.warn?.(`dsh-mneme dream: no json array in llm output (raw length ${decisionText?.length ?? 0})`);
622
714
  return finish({ ok: false, error: "no json array in llm output", summary: false });
@@ -752,7 +844,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
752
844
  : {}),
753
845
  messages: [
754
846
  { role: "system", content: [{ type: "text", text: SUMMARY_PROMPT }] },
755
- { role: "user", content: [{ type: "text", text: service.all().filter((m) => !m.archived && m.type !== "summary").map((m) => `- ${m.title}: ${m.content}`).join("\n") }] }
847
+ { role: "user", content: [{ type: "text", text: service.all().filter((m) => !m.archived && !m.session_disposed_at && m.type !== "summary").map((m) => `- ${m.title}: ${m.content}`).join("\n") }] }
756
848
  ]
757
849
  }, reportUsage));
758
850
  } catch (error) {
package/lib/hot-memory.js CHANGED
@@ -1,53 +1,53 @@
1
- // Session-scoped hot memory (v0.5.0 召回率优化 1.3): a short-term buffer of
2
- // the latest dialogue rounds, kept strictly apart from the long-term memory
3
- // store. The injector renders it ahead of the long-term recall block so the
4
- // agent sees "what we were just talking about" without those rounds ever
5
- // being persisted as memories. Bounded two ways: maxRounds (count) and
6
- // maxTokens (budget) — whichever evicts first.
7
-
8
- // CJK-aware token estimate: one Chinese character ≈ 0.6 tokens (clustering
9
- // behavior of mainstream tokenizers), one ASCII char ≈ 0.25.
10
- export function estimateTokens(text) {
11
- const s = String(text ?? "");
12
- let cjk = 0;
13
- for (const ch of s) if (ch >= "\u4e00" && ch <= "\u9fff") cjk++;
14
- return Math.ceil(cjk * 0.6 + (s.length - cjk) * 0.25);
15
- }
16
-
17
- /**
18
- * @param {{maxRounds?: number, maxTokens?: number}} opts
19
- * @returns {{add(round: {query: string, response?: string}): void,
20
- * getContext(): string,
21
- * rounds(): Array, clear(): void}}
22
- */
23
- export function createHotMemory({ maxRounds = 5, maxTokens = 2000 } = {}) {
24
- // Entry defense: a non-positive or non-integer maxRounds (0, -1, 1.5, NaN,
25
- // null, "2") would make the eviction while-loop unbounded — the buffer can
26
- // never shrink below `buffer.length > maxRounds`, so `add` would spin forever.
27
- // Fall back to the defaults so a hostile/buggy caller can never wedge the
28
- // hot-memory buffer in an infinite loop.
29
- maxRounds = (Number.isInteger(maxRounds) && maxRounds > 0) ? maxRounds : 5;
30
- maxTokens = (Number.isFinite(maxTokens) && maxTokens > 0) ? maxTokens : 2000;
31
- const buffer = [];
32
-
33
- function totalTokens() {
34
- return buffer.reduce(
35
- (sum, r) => sum + estimateTokens(`Q: ${r.query}\nA: ${r.response ?? ""}`),
36
- 0
37
- );
38
- }
39
-
40
- return {
41
- add(round) {
42
- if (!round?.query) return;
43
- buffer.push({ query: String(round.query), response: String(round.response ?? "") });
44
- while (buffer.length > maxRounds) buffer.shift();
45
- while (buffer.length > 1 && totalTokens() > maxTokens) buffer.shift();
46
- },
47
- getContext() {
48
- return buffer.map((r) => `Q: ${r.query}\nA: ${r.response ?? ""}`).join("\n\n");
49
- },
50
- rounds: () => [...buffer],
51
- clear() { buffer.length = 0; }
52
- };
53
- }
1
+ // Session-scoped hot memory (v0.5.0 召回率优化 1.3): a short-term buffer of
2
+ // the latest dialogue rounds, kept strictly apart from the long-term memory
3
+ // store. The injector renders it ahead of the long-term recall block so the
4
+ // agent sees "what we were just talking about" without those rounds ever
5
+ // being persisted as memories. Bounded two ways: maxRounds (count) and
6
+ // maxTokens (budget) — whichever evicts first.
7
+
8
+ // CJK-aware token estimate: one Chinese character ≈ 0.6 tokens (clustering
9
+ // behavior of mainstream tokenizers), one ASCII char ≈ 0.25.
10
+ export function estimateTokens(text) {
11
+ const s = String(text ?? "");
12
+ let cjk = 0;
13
+ for (const ch of s) if (ch >= "\u4e00" && ch <= "\u9fff") cjk++;
14
+ return Math.ceil(cjk * 0.6 + (s.length - cjk) * 0.25);
15
+ }
16
+
17
+ /**
18
+ * @param {{maxRounds?: number, maxTokens?: number}} opts
19
+ * @returns {{add(round: {query: string, response?: string}): void,
20
+ * getContext(): string,
21
+ * rounds(): Array, clear(): void}}
22
+ */
23
+ export function createHotMemory({ maxRounds = 5, maxTokens = 2000 } = {}) {
24
+ // Entry defense: a non-positive or non-integer maxRounds (0, -1, 1.5, NaN,
25
+ // null, "2") would make the eviction while-loop unbounded — the buffer can
26
+ // never shrink below `buffer.length > maxRounds`, so `add` would spin forever.
27
+ // Fall back to the defaults so a hostile/buggy caller can never wedge the
28
+ // hot-memory buffer in an infinite loop.
29
+ maxRounds = (Number.isInteger(maxRounds) && maxRounds > 0) ? maxRounds : 5;
30
+ maxTokens = (Number.isFinite(maxTokens) && maxTokens > 0) ? maxTokens : 2000;
31
+ const buffer = [];
32
+
33
+ function totalTokens() {
34
+ return buffer.reduce(
35
+ (sum, r) => sum + estimateTokens(`Q: ${r.query}\nA: ${r.response ?? ""}`),
36
+ 0
37
+ );
38
+ }
39
+
40
+ return {
41
+ add(round) {
42
+ if (!round?.query) return;
43
+ buffer.push({ query: String(round.query), response: String(round.response ?? "") });
44
+ while (buffer.length > maxRounds) buffer.shift();
45
+ while (buffer.length > 1 && totalTokens() > maxTokens) buffer.shift();
46
+ },
47
+ getContext() {
48
+ return buffer.map((r) => `Q: ${r.query}\nA: ${r.response ?? ""}`).join("\n\n");
49
+ },
50
+ rounds: () => [...buffer],
51
+ clear() { buffer.length = 0; }
52
+ };
53
+ }
package/lib/index.js CHANGED
@@ -317,6 +317,26 @@ export const apply = (ctx, config) => {
317
317
  const summarizer = createSummarizer(ctx, service, cfg);
318
318
  disposers.push(summarizer.dispose);
319
319
 
320
+ // Session lifecycle (v0.6.0): when a session leaves the store and the toggle
321
+ // is enabled, mark every memory born in it as session-disposed (hidden from
322
+ // injection/search/dream but never destroyed — recoverable via
323
+ // restoreBySession). Default off, so a disposed session leaves its memories
324
+ // active (legacy behavior). Every path is guarded: a failure inside the
325
+ // callback must never propagate into DSH's session teardown (that would crash
326
+ // the plugin on the very delete action it serves).
327
+ if (cfg.sessionLifecycleEnabled) {
328
+ disposers.push(ctx.on("session/disposed", (session) => {
329
+ const sessionId = session?.id;
330
+ if (!sessionId) return;
331
+ try {
332
+ const { disposed } = service.disposeBySession(sessionId);
333
+ ctx.logger?.info?.(`[dsh-mneme] session disposed, hid ${disposed} memory(s) for ${sessionId}`);
334
+ } catch (error) {
335
+ ctx.logger?.warn?.(`[dsh-mneme] session dispose failed for ${sessionId}: ${String(error)}`);
336
+ }
337
+ }));
338
+ }
339
+
320
340
  if (ctx.webServer) {
321
341
  const api = createApi(ctx, service, settings, commands ?? {
322
342
  add: () => { throw new Error("commands unavailable"); },