@modusensus/dsh-mneme 0.5.0 → 0.5.2

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/store.js CHANGED
@@ -12,6 +12,7 @@ CREATE TABLE IF NOT EXISTS memories (
12
12
  forgotten INTEGER NOT NULL DEFAULT 0,
13
13
  archived INTEGER NOT NULL DEFAULT 0,
14
14
  source TEXT,
15
+ session_id TEXT,
15
16
  content_history TEXT,
16
17
  embedding TEXT,
17
18
  epistemic_status TEXT NOT NULL DEFAULT 'subjective',
@@ -321,6 +322,7 @@ function toRow(row) {
321
322
  forgotten: row.forgotten === 1,
322
323
  archived: row.archived === 1,
323
324
  source: row.source ?? undefined,
325
+ session_id: row.session_id ?? undefined,
324
326
  content_history: parseJsonArray(row.content_history),
325
327
  quality_score: row.quality_score !== null && row.quality_score !== undefined ? Number(row.quality_score) : undefined,
326
328
  epistemic_status: row.epistemic_status ?? "subjective",
@@ -571,6 +573,7 @@ export function createStore(path) {
571
573
  addColumn("memories", "epistemic_status", "ALTER TABLE memories ADD COLUMN epistemic_status TEXT NOT NULL DEFAULT 'subjective'");
572
574
  addColumn("memories", "content_history", "ALTER TABLE memories ADD COLUMN content_history TEXT");
573
575
  addColumn("memories", "quality_score", "ALTER TABLE memories ADD COLUMN quality_score REAL");
576
+ addColumn("memories", "session_id", "ALTER TABLE memories ADD COLUMN session_id TEXT");
574
577
 
575
578
  // Legacy dream_runs without policy_epoch → backfill with the default epoch.
576
579
  addColumn("dream_runs", "policy_epoch", "ALTER TABLE dream_runs ADD COLUMN policy_epoch INTEGER NOT NULL DEFAULT 0");
@@ -657,8 +660,8 @@ export function createStore(path) {
657
660
  : inferEpistemicStatus(memory);
658
661
  runAtomically(() => {
659
662
  db.prepare(
660
- `INSERT INTO memories (id, type, title, content, tags, importance, forgotten, archived, source, content_history, quality_score, embedding, epistemic_status, created_at, updated_at)
661
- VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?)`
663
+ `INSERT INTO memories (id, type, title, content, tags, importance, forgotten, archived, source, session_id, content_history, quality_score, embedding, epistemic_status, created_at, updated_at)
664
+ VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
662
665
  ).run(
663
666
  id,
664
667
  type,
@@ -668,6 +671,7 @@ export function createStore(path) {
668
671
  importance,
669
672
  memory.archived ? 1 : 0,
670
673
  memory.source ?? null,
674
+ memory.session_id ?? null,
671
675
  JSON.stringify(memory.content_history ?? []),
672
676
  Number.isFinite(memory.quality_score) ? memory.quality_score : null,
673
677
  embedding,
package/lib/summarize.js CHANGED
@@ -182,7 +182,11 @@ export function createSummarizer(ctx, service, config) {
182
182
  .join("");
183
183
  const entries = parseSummaryJson(text || assembledText);
184
184
  for (const entry of entries) {
185
- service.saveWithDedupe({ ...entry, source: `session:${session.id}` });
185
+ // Provenance: the summarizer runs on a real session (turn/end hook), so
186
+ // session.id is always available here — it rides both the human-readable
187
+ // source label and the structured session_id column (v0.5.x memory
188
+ // provenance, the raw material for v0.6.0 reasoning-path / drift analysis).
189
+ service.saveWithDedupe({ ...entry, source: `session:${session.id}`, session_id: session.id });
186
190
  }
187
191
  } finally {
188
192
  if (audit) {
package/lib/tools.js CHANGED
@@ -16,6 +16,7 @@ const MEMORY_ITEM_SCHEMA = {
16
16
  tags: { type: "array", items: { type: "string" } },
17
17
  importance: { type: "integer", required: true },
18
18
  source: { type: "string" },
19
+ session_id: { type: "string" },
19
20
  created_at: { type: "string", required: true },
20
21
  updated_at: { type: "string", required: true }
21
22
  }
@@ -48,14 +49,18 @@ export function createTools(ctx, service, config, embedder) {
48
49
  },
49
50
  render: (_args, value) => TEXT_OUTPUT(`memory ${value.action}: ${value.id}`)
50
51
  },
51
- async execute(args) {
52
+ async execute(args, exec) {
52
53
  const { action, memory } = service.saveWithDedupe({
53
54
  type: args.type,
54
55
  title: args.title,
55
56
  content: args.content,
56
57
  tags: args.tags ?? [],
57
58
  importance: args.importance ?? 3,
58
- source: args.source ?? "tool"
59
+ source: args.source ?? "tool",
60
+ // Provenance: the session that issued the tool call. exec.agent is
61
+ // set by the agent loop (undefined in unit tests / direct calls) —
62
+ // absent a session, session_id stays null rather than fabricating one.
63
+ session_id: exec?.agent?.session?.id ?? undefined
59
64
  });
60
65
  return { action, id: memory.id };
61
66
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@modusensus/dsh-mneme",
3
3
  "description": "Cross-session memory plugin for DeepSeek Harness with autoDream consolidation: SQLite store, Markdown mirrors, 7 model tools, automatic injection, session summarization, user profile/rules, custom slash commands, vector (semantic) search, and a Web GUI panel",
4
- "version": "0.5.0",
4
+ "version": "0.5.2",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -5,7 +5,12 @@
5
5
  //
6
6
  // Usage: npm run sync (also run automatically by `npm pack`/`npm publish`
7
7
  // via the prepack hook, so a published tarball always ships a fresh lib/).
8
- import { cpSync, existsSync, mkdirSync, readdirSync } from "node:fs";
8
+ //
9
+ // Note: copyFileSync (not cpSync) is used on purpose — cpSync removes the
10
+ // destination first, which fails with EPERM/unlink on Windows when the path
11
+ // is long enough to trigger the \\?\ extended-prefix (observed on publish).
12
+ // copyFileSync truncates and rewrites in place, so it survives long paths.
13
+ import { copyFileSync, existsSync, mkdirSync, readdirSync } from "node:fs";
9
14
  import { join, relative } from "node:path";
10
15
  import { fileURLToPath } from "node:url";
11
16
 
@@ -31,7 +36,7 @@ for (const file of walk(srcDir)) {
31
36
  const rel = relative(srcDir, file);
32
37
  const dest = join(libDir, rel);
33
38
  mkdirSync(join(dest, ".."), { recursive: true });
34
- cpSync(file, dest);
39
+ copyFileSync(file, dest);
35
40
  copied++;
36
41
  console.log(`synced ${rel}`);
37
42
  }
package/src/hot-memory.js CHANGED
@@ -1,46 +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
- const buffer = [];
25
-
26
- function totalTokens() {
27
- return buffer.reduce(
28
- (sum, r) => sum + estimateTokens(`Q: ${r.query}\nA: ${r.response ?? ""}`),
29
- 0
30
- );
31
- }
32
-
33
- return {
34
- add(round) {
35
- if (!round?.query) return;
36
- buffer.push({ query: String(round.query), response: String(round.response ?? "") });
37
- while (buffer.length > maxRounds) buffer.shift();
38
- while (buffer.length > 1 && totalTokens() > maxTokens) buffer.shift();
39
- },
40
- getContext() {
41
- return buffer.map((r) => `Q: ${r.query}\nA: ${r.response ?? ""}`).join("\n\n");
42
- },
43
- rounds: () => [...buffer],
44
- clear() { buffer.length = 0; }
45
- };
46
- }
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
+ }