@modusensus/dsh-mneme 0.6.1 → 0.6.5

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/dream.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { validateDecisions, applyDecisions } from "./dream/decisions.js";
2
2
  import { clusterMemories, findPotentialConflicts } from "./dream/clustering.js";
3
+ import { runAutoTag } from "./dream/tag-extractor.js";
3
4
  import { createHash, randomUUID } from "node:crypto";
4
5
  export { validateDecisions, applyDecisions, normalizeDecisions };
5
6
 
@@ -815,6 +816,25 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
815
816
  // that only froze conflicts is not a noop.
816
817
  const noChange = frozenCount === 0 && applied === 0 && committed.every((c) => c.action === "keep");
817
818
 
819
+ // v0.6.2 auto-tag: a light LLM pass over the retained (post-consolidation)
820
+ // memories. Opt-in via config.autoTagEnabled, bounded by autoTagMaxPerRun,
821
+ // and always fail-safe — a tag failure must never change the consolidation
822
+ // outcome reported below (it is logged and counted, nothing more).
823
+ let autoTagged = 0;
824
+ if (config.autoTagEnabled === true) {
825
+ try {
826
+ const tagResult = await runAutoTag({ ctx, service, config, route });
827
+ autoTagged = tagResult?.tagged ?? 0;
828
+ if (tagResult?.ok === false && tagResult?.skippedBy && tagResult.skippedBy !== "empty") {
829
+ logger?.warn?.(`dsh-mneme dream: auto-tag skipped (${tagResult.skippedBy})`);
830
+ } else if (autoTagged > 0) {
831
+ logger?.info?.(`[dsh-mneme] auto-tag: ${autoTagged} memory(ies) tagged`);
832
+ }
833
+ } catch (error) {
834
+ logger?.warn?.(`dsh-mneme dream: auto-tag failed: ${String(error)}`);
835
+ }
836
+ }
837
+
818
838
  // Keep the vector index consistent with the post-dream store state.
819
839
  if (semantic?.embedder && semantic?.vectorIndex) {
820
840
  try {
package/lib/mirror.js CHANGED
@@ -31,6 +31,15 @@ function renderMemory(m) {
31
31
  .digest("hex");
32
32
  const lines = [];
33
33
  lines.push(`## ${esc(m.title)}`);
34
+ // v0.6.2 tag line: entity_attrs-backed tags (attached by the service as
35
+ // `entityTags`) rendered as `#tag` space-separated under the title. No tags
36
+ // → no line. Legacy `- **标签**:` metadata below keeps the memories.tags
37
+ // column (still written by save/update) readable.
38
+ const entityTags = Array.isArray(m.entityTags) ? m.entityTags : [];
39
+ if (entityTags.length) {
40
+ lines.push("");
41
+ lines.push(entityTags.map((t) => `#${esc(t)}`).join(" "));
42
+ }
34
43
  lines.push("");
35
44
  lines.push(`- **ID**: \`${m.id}\``);
36
45
  lines.push(`- **类型**: ${m.type}`);
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Tag parser (v0.6.2). Given a memory's content, extract explicit hashtags of
3
+ * the form `#标签`. The allowed character set is `[a-zA-Z0-9_一-龥-]+` (ASCII
4
+ * letters/digits/underscore, CJK characters, and hyphen), so `#linux`,
5
+ * `#deepseek` and `#考研` are all valid tags.
6
+ *
7
+ * Rules:
8
+ * - multiple tags on one line are all extracted, in source order;
9
+ * - duplicates collapse to the first occurrence;
10
+ * - tags longer than MAX_TAG_LENGTH (20) characters are dropped as illegal;
11
+ * - edge hyphens (leading/trailing, e.g. a sentence-terminating `#tag-`) are
12
+ * stripped; a tag that collapses to empty is dropped;
13
+ * - markdown `## heading` never matches (the second `#`/space is not in the
14
+ * allowed set).
15
+ *
16
+ * Pure module: no store, no side effects — persistence (store.setMemoryTags)
17
+ * is a separate step. sanitizeTags shares the same validation so LLM-extracted
18
+ * tag arrays (autoDream tag-extractor) are filtered identically.
19
+ */
20
+
21
+ export const MAX_TAG_LENGTH = 20;
22
+
23
+ // `一` (U+4E00) .. `龥` (U+9FA5) is the CJK Unified Ideographs block (the
24
+ // rule's `一-龥`). Matching requires a tag character right after `#` so a bare
25
+ // `#` and markdown `## heading` are naturally excluded (#/space not in set).
26
+ const TAG_RE = /#[a-zA-Z0-9_一-龥-]+/g;
27
+ const EDGE_HYPHEN = /^-+|-+$/g;
28
+ const VALID_TAG = /^[a-zA-Z0-9_一-龥-]+$/;
29
+
30
+ /** Validate + dedupe an array of tag strings. Non-strings, blanks, over-long
31
+ * and illegal-character tags are dropped (fail-safe). A leading `#` is
32
+ * tolerated so raw LLM output can be passed through directly. */
33
+ export function sanitizeTags(tags) {
34
+ if (!Array.isArray(tags)) return [];
35
+ const seen = new Set();
36
+ const out = [];
37
+ for (const t of tags) {
38
+ if (typeof t !== "string") continue;
39
+ let tag = t.trim();
40
+ if (tag.startsWith("#")) tag = tag.slice(1);
41
+ tag = tag.replace(EDGE_HYPHEN, "");
42
+ if (!tag || tag.length > MAX_TAG_LENGTH) continue;
43
+ if (!VALID_TAG.test(tag)) continue;
44
+ if (seen.has(tag)) continue; // 去重(保留首次出现顺序)
45
+ seen.add(tag);
46
+ out.push(tag);
47
+ }
48
+ return out;
49
+ }
50
+
51
+ /** @returns {string[]} validated tags in source order, deduplicated. */
52
+ export function parseTags(content) {
53
+ if (typeof content !== "string" || content.length === 0) return [];
54
+ const raw = [];
55
+ TAG_RE.lastIndex = 0;
56
+ let m;
57
+ while ((m = TAG_RE.exec(content)) !== null) raw.push(m[0]);
58
+ return sanitizeTags(raw);
59
+ }
@@ -0,0 +1,61 @@
1
+ // v0.6.4 Tag-weighted re-rank helpers.
2
+ // Query tags come from `#hashtags` in the query plus any known tags the query
3
+ // mentions; candidates whose tags overlap the query tags or the session's
4
+ // hot-memory tags get their score boosted before the final top-K cut.
5
+
6
+ /**
7
+ * Extract tags from a query: explicit `#hashtags` (CJK allowed) plus known
8
+ * tags whose lowercase form appears in the query. Deduped, order-preserving.
9
+ */
10
+ export function extractQueryTags(query, knownTags = []) {
11
+ if (typeof query !== "string" || !query) return [];
12
+ const set = new Set();
13
+ const re = /#([a-zA-Z0-9_一-龥-]+)/g;
14
+ let m;
15
+ while ((m = re.exec(query)) !== null) set.add(m[1]);
16
+ const q = query.toLowerCase();
17
+ for (const t of knownTags) {
18
+ if (typeof t === "string" && q.includes(t.toLowerCase())) set.add(t);
19
+ }
20
+ return Array.from(set);
21
+ }
22
+
23
+ function hasOverlap(a, b) {
24
+ if (!Array.isArray(a) || !Array.isArray(b)) return false;
25
+ const setB = new Set(b);
26
+ return a.some((x) => setB.has(x));
27
+ }
28
+
29
+ /**
30
+ * Boost candidate scores by tag overlap with the query and/or session tags.
31
+ * Each candidate is expected to carry a `tags` array (enriched by the caller).
32
+ * Returns a new array sorted by boosted score descending; candidates that were
33
+ * boosted are marked `tagBoost: true`. Unchanged candidates keep their
34
+ * relative order (stable).
35
+ */
36
+ export function applyTagBoost(
37
+ candidates,
38
+ { queryTags = [], sessionTags = [], factor = 1.15, sessionFactor = 1.08 }
39
+ ) {
40
+ const boosted = candidates.map((c) => {
41
+ const tags = c.tags ?? [];
42
+ let score = typeof c.score === "number" ? c.score : 0;
43
+ let didBoost = false;
44
+
45
+ if (queryTags.length && hasOverlap(tags, queryTags)) {
46
+ score = Math.min(1, score * factor);
47
+ didBoost = true;
48
+ }
49
+ if (sessionTags.length && hasOverlap(tags, sessionTags)) {
50
+ score = Math.min(1, score * sessionFactor);
51
+ didBoost = true;
52
+ }
53
+
54
+ const out = { ...c, score };
55
+ if (didBoost) out.tagBoost = true;
56
+ return out;
57
+ });
58
+
59
+ boosted.sort((a, b) => (b.score ?? 0) - (a.score ?? 0));
60
+ return boosted;
61
+ }
package/lib/service.js CHANGED
@@ -4,6 +4,7 @@ import { evaluateMemoryQuality } from "./quality-filter.js";
4
4
  import { createBM25Index } from "./search/bm25.js";
5
5
  import { adaptiveThreshold } from "./search/adaptive.js";
6
6
  import { parseWikiLinks } from "./parser/wiki-link.js";
7
+ import { extractQueryTags, applyTagBoost } from "./search/tag-boost.js";
7
8
 
8
9
  const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
9
10
 
@@ -327,6 +328,55 @@ export function createService({ store, mirror, config, onWrite, logger }) {
327
328
  return hits;
328
329
  }
329
330
 
331
+ /**
332
+ * Search for memories carrying a given tag set (v0.6.2). Multiple tag:
333
+ * tokens in one query use AND semantics — a memory must carry every tag.
334
+ * Combinable with the leftover query: an entity:/attr: prefix is intersected
335
+ * with the tag-matched set, and plain keyword text ranks it (only rows whose
336
+ * keyword score > 0 survive). Tags are first-class recall, not entity-gated:
337
+ * the tag match itself does not depend on config.entitySearchEnabled.
338
+ * @param {string[]} tagTokens
339
+ * @param {string} q — leftover query after tag: tokens were stripped
340
+ * @param {object} [options]
341
+ * @param {number} [options.topK=20]
342
+ * @returns {any[]}
343
+ */
344
+ function searchByTags(tagTokens, q, options) {
345
+ const { topK = 20 } = options;
346
+ const tags = (Array.isArray(tagTokens) ? tagTokens : [])
347
+ .map((t) => String(t).trim()).filter(Boolean);
348
+ if (!tags.length) return [];
349
+ let hits = store.findMemoriesByTags(tags);
350
+ if (!hits.length) return [];
351
+ // Intersect with entity:/attr: prefixes present in the leftover query.
352
+ if (config?.entitySearchEnabled) {
353
+ if (q.startsWith("entity:")) {
354
+ const ids = new Set(searchByEntity(q.slice(7).trim(), { topK: 10000 }).map((m) => m.id));
355
+ hits = hits.filter((m) => ids.has(m.id));
356
+ }
357
+ if (q.startsWith("attr:")) {
358
+ const [key, value] = q.slice(5).split("=");
359
+ const ids = new Set(searchByAttr(key, value, { topK: 10000 }).map((m) => m.id));
360
+ hits = hits.filter((m) => ids.has(m.id));
361
+ }
362
+ }
363
+ // Leftover plain text ranks the tag-matched set; only rows whose title or
364
+ // content actually mentions the keyword survive (scoreKeyword's 0.3 base is
365
+ // a non-match, so the containment check is the real gate); otherwise
366
+ // preserve insertion order.
367
+ const keywordOnly = q && !q.startsWith("entity:") && !q.startsWith("attr:");
368
+ hits = keywordOnly
369
+ ? hits
370
+ .map((m) => ({ ...m, score: scoreKeyword(m, q), source: "tag" }))
371
+ .filter((m) => (m.title ?? "").toLowerCase().includes(q.toLowerCase())
372
+ || (m.content ?? "").toLowerCase().includes(q.toLowerCase()))
373
+ .sort((a, b) => b.score - a.score)
374
+ : hits.map((m) => ({ ...m, source: "tag" }));
375
+ const result = hits.slice(0, topK);
376
+ touchRecalled(result);
377
+ return result;
378
+ }
379
+
330
380
  /**
331
381
  * Semantic-aware memory search: keyword recall (store.search) plus optional
332
382
  * vector recall + rerank. mode:
@@ -433,8 +483,18 @@ export function createService({ store, mirror, config, onWrite, logger }) {
433
483
 
434
484
  async function searchMemories(query, options = {}) {
435
485
  const { mode = "auto", topK = 20, threshold, useRerank = true, recordRecall = false } = options;
436
- const q = String(query ?? "").trim();
437
- if (!q) return [];
486
+ const raw = String(query ?? "").trim();
487
+ if (!raw) return [];
488
+
489
+ // tag: 前缀(v0.6.2)。先把所有 tag: 令牌从 query 里剥出来,剩余的 q 仍可带
490
+ // entity:/attr: 前缀或普通关键词 —— searchByTags 负责交集/排序。没有 tag:
491
+ // 令牌则走下面的 entity:/attr:/文本原逻辑(完全向后兼容)。
492
+ const tagTokens = [];
493
+ const q = raw.replace(/\btag:([^\s]+)/g, (_, tok) => {
494
+ if (tok) tagTokens.push(tok);
495
+ return "";
496
+ }).trim();
497
+ if (tagTokens.length) return searchByTags(tagTokens, q, options);
438
498
 
439
499
  // entity:/attr: 前缀路由(v0.3.0 Phase 3)。entitySearchEnabled 关闭时走原逻辑。
440
500
  if (config?.entitySearchEnabled) {
@@ -539,7 +599,10 @@ export function createService({ store, mirror, config, onWrite, logger }) {
539
599
  const ranked = [...byId.values()]
540
600
  .sort((a, b) => (b.score ?? 0) - (a.score ?? 0))
541
601
  .map((r) => ({ ...r, score: Math.max(0, Math.min(1, r.score ?? 0)) }));
542
- merged = ranked.slice(0, lim);
602
+ // Tag boost (v0.6.4) needs the full ranked pool, not just the top-lim
603
+ // slice, so a tagged candidate just below the line can be re-admitted
604
+ // after the boost. Without boost this is the legacy lim truncation.
605
+ merged = config.tagBoostEnabled === true ? ranked : ranked.slice(0, lim);
543
606
  if (merged.length < lim && !merged.length) {
544
607
  // Vector unavailable entirely: fall back to plain keyword.
545
608
  merged = keyword.slice(0, lim);
@@ -565,7 +628,31 @@ export function createService({ store, mirror, config, onWrite, logger }) {
565
628
  // it is the documented text-only path and must not be altered by
566
629
  // embedding state.
567
630
  merged = mode === "keyword" ? merged : semanticDeduplicate(merged);
631
+
632
+ // Tag-weighted re-rank (v0.6.4): boost candidates whose tags overlap the
633
+ // query tags or the current session's hot-memory tags — applied BEFORE the
634
+ // final top-K cut so tagged candidates just below the line can be
635
+ // re-admitted. Opt-in, and skipped entirely on the keyword-only path. When
636
+ // a reranker is configured (opt-in), it remains the final authority on
637
+ // order; the boost still shapes which candidates reach it.
638
+ if (config.tagBoostEnabled === true && mode !== "keyword" && merged.length) {
639
+ const ids = merged.map((m) => m.id);
640
+ const tagsMap = store.getMemoryTagsMap(ids);
641
+ const enriched = merged.map((m) => ({ ...m, tags: tagsMap.get(m.id) ?? [] }));
642
+ const knownTags = [...tagsMap.values()].flat();
643
+ const queryTags = extractQueryTags(q, knownTags);
644
+ const sessionTags = Array.isArray(options?.sessionTags) ? options.sessionTags : [];
645
+ if (queryTags.length || sessionTags.length) {
646
+ merged = applyTagBoost(enriched, {
647
+ queryTags,
648
+ sessionTags,
649
+ factor: config.tagBoostFactor,
650
+ sessionFactor: config.sessionTagBoostFactor,
651
+ }).map(({ tags, ...rest }) => rest);
652
+ }
653
+ }
568
654
  merged = merged.slice(0, lim);
655
+
569
656
  let result = useRerank && reranker && merged.length
570
657
  ? await rerankCandidates(q, merged, lim)
571
658
  : merged;
@@ -1043,6 +1130,21 @@ export function createService({ store, mirror, config, onWrite, logger }) {
1043
1130
  }));
1044
1131
  }
1045
1132
 
1133
+ /**
1134
+ * Directory view (v0.6.3): group live memories by tag. Delegates to
1135
+ * store.getDirectory (tag-sorted groups, importance/updated DESC members,
1136
+ * live-only filtering) and maps every memory to the wire DTO so the result
1137
+ * is JSON-safe for the API endpoint.
1138
+ * @returns {{groups: {tag: string, memories: object[]}[], untagged: object[]}}
1139
+ */
1140
+ function getDirectory() {
1141
+ const { groups, untagged } = store.getDirectory();
1142
+ return {
1143
+ groups: groups.map((g) => ({ tag: g.tag, memories: toApiList(g.memories) })),
1144
+ untagged: toApiList(untagged)
1145
+ };
1146
+ }
1147
+
1046
1148
  /**
1047
1149
  * Three-way merge of in-flight human mirror edits before a re-render.
1048
1150
  * Runs on every syncMirror, so a human edit made between two store writes is
@@ -1150,8 +1252,14 @@ export function createService({ store, mirror, config, onWrite, logger }) {
1150
1252
  // leaving committed files mislabeled as failed and masking partial state.
1151
1253
  // Absent entries (a type with no memories) count as success: sync prunes
1152
1254
  // the stale file, which is itself a completed physical state.
1255
+ // v0.6.2: attach entity_attrs-backed tags (entityTags) after the human-edit
1256
+ // merge so renderMemory can draw the `#tag` line under each title. The
1257
+ // bulk map is a single query, and a missing row simply renders no line.
1258
+ const reconciled = reconcileHumanEdits(list);
1259
+ const tagsMap = store.getMemoryTagsMap(reconciled.map((m) => m.id));
1260
+ const tagged = reconciled.map((m) => ({ ...m, entityTags: tagsMap.get(m.id) ?? [] }));
1153
1261
  let allOk = true;
1154
- const results = mirror.sync(reconcileHumanEdits(list)) ?? {};
1262
+ const results = mirror.sync(tagged) ?? {};
1155
1263
  for (const type of Object.keys(TYPE_FILE)) {
1156
1264
  const r = results[type];
1157
1265
  const ok = !r || r.ok === true;
@@ -1371,6 +1479,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
1371
1479
  injectCandidates,
1372
1480
  mergeHumanEdits,
1373
1481
  toApiList,
1482
+ getDirectory,
1374
1483
  transaction,
1375
1484
  enqueue,
1376
1485
  setDreamHook(fn) { dreamHook = fn; },
@@ -1585,6 +1694,25 @@ export function createService({ store, mirror, config, onWrite, logger }) {
1585
1694
  saveWikiLinks: (r) => store.saveWikiLinks(r),
1586
1695
  listEntities: (o) => store.listEntities(o),
1587
1696
  getRelations: (id) => store.getRelations(id),
1697
+ // Tag system (v0.6.2). setMemoryTags is the manual/user path: gated by
1698
+ // config.manualTagEnabled (default true), re-renders the mirror and fires
1699
+ // the write hook. applyMemoryTags is the raw write used by the autoDream
1700
+ // tag pass (gated by autoTagEnabled, wrapped in a transaction by the
1701
+ // extractor so the mirror re-renders exactly once).
1702
+ setMemoryTags: (memoryId, tags) => {
1703
+ if (config.manualTagEnabled === false) {
1704
+ return { ok: false, error: "manualTagEnabled is off" };
1705
+ }
1706
+ const stored = store.setMemoryTags(memoryId, tags);
1707
+ afterSync("write");
1708
+ notifyWrite();
1709
+ return { ok: true, tags: stored };
1710
+ },
1711
+ getMemoryTags: (memoryId) => store.getMemoryTags(memoryId),
1712
+ // Read-only gate flag so the Web panel can hide tag editing when the
1713
+ // manual path is disabled (default: manual tagging is on).
1714
+ manualTagEnabled: () => config.manualTagEnabled !== false,
1715
+ applyMemoryTags: (memoryId, tags) => store.setMemoryTags(memoryId, tags),
1588
1716
  saveAttr: (r) => store.saveAttr(r),
1589
1717
  createEntity: (r) => store.createEntity(r),
1590
1718
  findEntityByName: (n) => store.findEntityByName(n),
package/lib/store.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { DatabaseSync } from "node:sqlite";
2
2
  import { randomUUID } from "node:crypto";
3
+ import { sanitizeTags } from "./parser/tag.js";
3
4
 
4
5
  const SCHEMA = `
5
6
  CREATE TABLE IF NOT EXISTS memories (
@@ -1684,6 +1685,162 @@ export function createStore(path) {
1684
1685
  return memories;
1685
1686
  }
1686
1687
 
1688
+ // --- tag storage (v0.6.2) ------------------------------------------------
1689
+ // Tags ride the snapshot-style entity_attrs table (attr_key='tags'), so one
1690
+ // memory has exactly one live tags row; setMemoryTags invalidates any prior
1691
+ // live row and inserts a fresh one (idempotent overwrite). entity_id is the
1692
+ // memory id itself (the memory is its own tag entity), memory_id is kept so
1693
+ // the existing memory-scoped attr queries (getAttrsByMemory / findMemoriesByAttr)
1694
+ // and the bulk tag map all work without a special path.
1695
+
1696
+ /** Normalize an arbitrary tags input to a deduplicated string array.
1697
+ * Delegates to parser/tag.js sanitizeTags (shared validation with parseTags
1698
+ * and the autoDream tag-extractor): strips a leading `#`, trims, drops
1699
+ * non-strings/blanks/over-long/illegal-char tags. Kept as a thin alias so
1700
+ * the tag write path validates identically to the parser path. */
1701
+ function normalizeTags(tags) {
1702
+ return sanitizeTags(tags);
1703
+ }
1704
+
1705
+ /**
1706
+ * Set (overwrite) the live tag set for a memory. Exactly one tags row stays
1707
+ * live per memory: any prior live row is invalidated first, then one fresh
1708
+ * row is written (no-op when tags is empty — the invalidated row is removed
1709
+ * so "clear tags" = no live row). Returns the stored tag array.
1710
+ */
1711
+ function setMemoryTags(memoryId, tags) {
1712
+ const arr = normalizeTags(tags);
1713
+ const now = nowIso();
1714
+ // Atomic: the invalidation and the fresh row must land together, so a
1715
+ // mid-write crash never leaves the old live row gone without a replacement.
1716
+ // SAVEPOINT (not BEGIN) so this nests safely inside service.transaction().
1717
+ db.exec("SAVEPOINT set_memory_tags");
1718
+ try {
1719
+ db.prepare(
1720
+ `UPDATE entity_attrs SET valid_until = ?
1721
+ WHERE attr_key = 'tags' AND memory_id = ? AND valid_until IS NULL`
1722
+ ).run(now, memoryId);
1723
+ if (arr.length) {
1724
+ const id = randomUUID();
1725
+ db.prepare(
1726
+ `INSERT INTO entity_attrs (id, entity_id, attr_key, attr_value, memory_id, valid_from, valid_until, confidence, source)
1727
+ VALUES (?, ?, 'tags', ?, ?, ?, NULL, 1.0, 'manual')`
1728
+ ).run(id, memoryId, JSON.stringify(arr), memoryId, now);
1729
+ }
1730
+ db.exec("RELEASE set_memory_tags");
1731
+ } catch (e) {
1732
+ db.exec("ROLLBACK TO set_memory_tags");
1733
+ db.exec("RELEASE set_memory_tags");
1734
+ throw e;
1735
+ }
1736
+ return arr;
1737
+ }
1738
+
1739
+ /** Live tags for a memory ([] when none / unknown). */
1740
+ function getMemoryTags(memoryId) {
1741
+ const row = db.prepare(
1742
+ `SELECT attr_value FROM entity_attrs
1743
+ WHERE attr_key = 'tags' AND memory_id = ? AND valid_until IS NULL
1744
+ ORDER BY valid_from DESC LIMIT 1`
1745
+ ).get(memoryId);
1746
+ if (!row) return [];
1747
+ try {
1748
+ const arr = JSON.parse(row.attr_value);
1749
+ return Array.isArray(arr) ? arr : [];
1750
+ } catch {
1751
+ return [];
1752
+ }
1753
+ }
1754
+
1755
+ /** Bulk live-tags lookup for mirror rendering. Returns Map<memoryId, string[]>. */
1756
+ function getMemoryTagsMap(ids) {
1757
+ const out = new Map();
1758
+ const list = (Array.isArray(ids) ? ids : []).filter(Boolean);
1759
+ for (let i = 0; i < list.length; i += 100) {
1760
+ const chunk = list.slice(i, i + 100);
1761
+ const rows = db.prepare(
1762
+ `SELECT memory_id, attr_value FROM entity_attrs
1763
+ WHERE attr_key = 'tags' AND valid_until IS NULL
1764
+ AND memory_id IN (${chunk.map(() => "?").join(",")})`
1765
+ ).all(...chunk);
1766
+ for (const row of rows) {
1767
+ try {
1768
+ const arr = JSON.parse(row.attr_value);
1769
+ if (Array.isArray(arr) && arr.length) out.set(row.memory_id, arr);
1770
+ } catch { /* corrupt row: skip */ }
1771
+ }
1772
+ }
1773
+ return out;
1774
+ }
1775
+
1776
+ /**
1777
+ * Memories carrying a live tags row that contains EVERY requested tag
1778
+ * (AND semantics for a multi-tag query). attr_value is a JSON array, so the
1779
+ * match uses quoted `"tag"` substrings — `tag:lin` never collides with
1780
+ * `linux` because JSON array elements are quote-delimited. Only live rows
1781
+ * (valid_until IS NULL) with a memory reference participate; each memory
1782
+ * appears once.
1783
+ */
1784
+ function findMemoriesByTags(tags) {
1785
+ const list = normalizeTags(tags);
1786
+ if (!list.length) return [];
1787
+ const where = list.map(() => `attr_value LIKE ? ESCAPE '\\'`).join(" AND ");
1788
+ const params = list.map((t) => `%"${escapeLike(t)}"%`);
1789
+ const rows = db.prepare(
1790
+ `SELECT DISTINCT memory_id FROM entity_attrs
1791
+ WHERE attr_key = 'tags' AND valid_until IS NULL
1792
+ AND memory_id IS NOT NULL AND memory_id != ''
1793
+ AND (${where})`
1794
+ ).all(...params);
1795
+ // Same live-memory filter as getDirectory/store.search: forgotten/archived/
1796
+ // session-disposed memories are invisible to `tag:` recall.
1797
+ const stmt = db.prepare(
1798
+ "SELECT * FROM memories WHERE id = ? AND forgotten = 0 AND archived = 0 AND session_disposed_at IS NULL"
1799
+ );
1800
+ const memories = [];
1801
+ for (const { memory_id } of rows) {
1802
+ const row = stmt.get(memory_id);
1803
+ if (row) memories.push(toRow(row));
1804
+ }
1805
+ return memories;
1806
+ }
1807
+
1808
+ /**
1809
+ * Directory view (v0.6.3): group live memories by their entity_attrs-backed
1810
+ * tag set. A memory carrying N tags appears under all N tag folders; a memory
1811
+ * with no live tags lands in `untagged`. Only live rows participate —
1812
+ * forgotten, archived and session-disposed memories are excluded. Groups are
1813
+ * ordered by tag (locale-aware), group members and untagged follow the
1814
+ * canonical memory order (importance DESC, updated_at DESC, id).
1815
+ * @returns {{groups: {tag: string, memories: object[]}[], untagged: object[]}}
1816
+ */
1817
+ function getDirectory() {
1818
+ const rows = db.prepare(
1819
+ `SELECT * FROM memories
1820
+ WHERE forgotten = 0 AND archived = 0 AND session_disposed_at IS NULL
1821
+ ORDER BY importance DESC, updated_at DESC, id`
1822
+ ).all();
1823
+ const memories = rows.map(toRow);
1824
+ const tagMap = getMemoryTagsMap(memories.map((m) => m.id));
1825
+ const byTag = new Map(); // tag -> memory[]
1826
+ const untagged = [];
1827
+ for (const m of memories) {
1828
+ const tags = tagMap.get(m.id);
1829
+ if (!tags || tags.length === 0) {
1830
+ untagged.push(m);
1831
+ continue;
1832
+ }
1833
+ for (const tag of tags) {
1834
+ if (!byTag.has(tag)) byTag.set(tag, []);
1835
+ byTag.get(tag).push(m);
1836
+ }
1837
+ }
1838
+ const groups = [...byTag.entries()]
1839
+ .sort((a, b) => a[0].localeCompare(b[0]))
1840
+ .map(([tag, ms]) => ({ tag, memories: ms }));
1841
+ return { groups, untagged };
1842
+ }
1843
+
1687
1844
  /**
1688
1845
  * Record a typed relation between two entities. metadata (optional) is a
1689
1846
  * free-form JSON blob describing the relation. Relations are append-only —
@@ -2055,6 +2212,11 @@ export function createStore(path) {
2055
2212
  getAttrHistory,
2056
2213
  getAttrsByMemory,
2057
2214
  findMemoriesByAttr,
2215
+ setMemoryTags,
2216
+ getMemoryTags,
2217
+ getMemoryTagsMap,
2218
+ findMemoriesByTags,
2219
+ getDirectory,
2058
2220
  saveRelation,
2059
2221
  saveWikiLinks,
2060
2222
  findByTitle,
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.6.1",
4
+ "version": "0.6.5",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
package/src/api.js CHANGED
@@ -608,8 +608,84 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
608
608
  }
609
609
  });
610
610
 
611
+ // --- memory tags (v0.6.2) ------------------------------------------------
612
+ // GET /api/dsh-mneme/memory/tags?id=<memoryId> → live entity_attrs-backed
613
+ // tag set for one memory plus the manualTagEnabled gate (so the panel
614
+ // can hide tag editing when the manual path is off). Read-only, stays
615
+ // open when apiToken is set (like list/search/semantic).
616
+ // POST /api/dsh-mneme/memory/tags { id, tags } → overwrite the live tag set
617
+ // via service.setMemoryTags (manualTagEnabled gate); 409 when the gate
618
+ // is closed. Auth-gated like the other write endpoints.
619
+ register({
620
+ kind: "exact",
621
+ path: "/api/dsh-mneme/memory/tags",
622
+ handler(req, res) {
623
+ try {
624
+ if (req.method === "POST" || req.method === "PUT") {
625
+ if (!requireAuth(req, res, apiToken)) return;
626
+ return readBody(req).then((text) => {
627
+ const body = parseBody(text);
628
+ const id = typeof body.id === "string" ? body.id.trim() : "";
629
+ if (!id) {
630
+ sendJson(res, 400, { error: "missing-id" });
631
+ return;
632
+ }
633
+ const memory = service.getById?.(id) ?? null;
634
+ if (!memory) {
635
+ sendJson(res, 404, { error: "memory-not-found" });
636
+ return;
637
+ }
638
+ const result = service.setMemoryTags(id, Array.isArray(body.tags) ? body.tags : []);
639
+ if (result?.ok === false) {
640
+ sendJson(res, 409, { error: result.error || "tags-disabled" });
641
+ return;
642
+ }
643
+ sendJson(res, 200, { ok: true, memoryId: id, tags: result?.tags ?? [] });
644
+ });
645
+ }
646
+ const url = new URL(req.url, "http://localhost");
647
+ const id = (url.searchParams.get("id") ?? "").trim();
648
+ if (!id) {
649
+ sendJson(res, 400, { error: "missing-id" });
650
+ return;
651
+ }
652
+ const memory = service.getById?.(id) ?? null;
653
+ if (!memory) {
654
+ sendJson(res, 404, { error: "memory-not-found" });
655
+ return;
656
+ }
657
+ const tags = service.getMemoryTags?.(id) ?? [];
658
+ sendJson(res, 200, {
659
+ memoryId: id,
660
+ tags: Array.isArray(tags) ? tags : [],
661
+ manualTagEnabled: service.manualTagEnabled?.() ?? true
662
+ });
663
+ } catch {
664
+ sendJson(res, 500, { error: "internal" });
665
+ }
666
+ }
667
+ });
668
+
669
+ // --- directory view (v0.6.3) ---------------------------------------------
670
+ // GET /api/dsh-mneme/directory → memories grouped by tag as
671
+ // { groups: [{ tag, memories: [...] }], untagged: [...] }. Live-only
672
+ // (forgotten/archived/session-disposed excluded), groups tag-sorted, members
673
+ // importance+updated DESC. Read-only, stays open when apiToken is set.
674
+ register({
675
+ kind: "exact",
676
+ path: "/api/dsh-mneme/directory",
677
+ handler(req, res) {
678
+ try {
679
+ const dir = service.getDirectory?.() ?? { groups: [], untagged: [] };
680
+ sendJson(res, 200, dir);
681
+ } catch {
682
+ sendJson(res, 500, { error: "internal" });
683
+ }
684
+ }
685
+ });
686
+
611
687
  return {
612
- routes: 11,
688
+ routes: 19,
613
689
  dispose: () => {
614
690
  for (const dispose of disposers) dispose();
615
691
  }
package/src/config.js CHANGED
@@ -175,6 +175,24 @@ export const Config = z.object({
175
175
  // regardless of this flag.
176
176
  wikiLinkEnabled: z.boolean().default(false),
177
177
 
178
+ // --- tag system (v0.6.2) ---------------------------------------------------
179
+ // Opt-in: when autoTagEnabled is true, a light LLM pass runs after each
180
+ // autoDream consolidation and extracts 1-3 tags per retained memory
181
+ // (autoTagMaxPerRun bounds how many memories are tagged per run). The tag
182
+ // storage layer (store.setMemoryTags/getMemoryTags + tag: search + mirror
183
+ // `#tag` line) is always available regardless of this flag.
184
+ autoTagEnabled: z.boolean().default(false),
185
+ autoTagMaxPerRun: z.natural().min(1).max(100).default(10),
186
+ // Manual tagging (service.setMemoryTags / memory tools) is on by default;
187
+ // set false to disable the manual write path too.
188
+ manualTagEnabled: z.boolean().default(true),
189
+
190
+ // --- tag-weighted re-rank (v0.6.4) -------------------------------------
191
+ // Opt-in: boost candidates whose tags overlap the query/session tags.
192
+ tagBoostEnabled: z.boolean().default(false),
193
+ tagBoostFactor: z.number().min(1).max(2).default(1.15),
194
+ sessionTagBoostFactor: z.number().min(1).max(2).default(1.08),
195
+
178
196
  // --- sleep mode: idle-triggered deep maintenance (v0.4.0) ---------------
179
197
  // Opt-in, off by default. Unlike autoDream (threshold-triggered, lightweight)
180
198
  // sleep fires when the store has been quiet for sleepIdleMinutes and deep-