dsh-plugin-memory 0.6.0 → 0.7.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.
@@ -4,9 +4,12 @@
4
4
  *
5
5
  * Usage:
6
6
  * dsh-memory init [dir] # create the store scaffold (default $MEMORY_DIR or ~/.memory)
7
- * dsh-memory search <query> # full-text search across memory pages
7
+ * dsh-memory search <query> [--touch] # full-text search; --touch stamps last_access on the hits
8
+ * dsh-memory touch [pages...] # stamp last_access (all pages when none given)
9
+ * dsh-memory index [--check|--write|--sync-frontmatter]
10
+ * # index routing table: drift / rewrite / import summaries
8
11
  * dsh-memory lint # integrity check (index vs files, orphans, log format)
9
- * dsh-memory status # health summary (page counts, sizes, last modified)
12
+ * dsh-memory status # health summary (page counts, sizes, staleness, last modified)
10
13
  * dsh-memory pack [out.tar.gz] # export a portable archive + manifest
11
14
  * dsh-memory unpack <archive> [--force] # restore from an archive
12
15
  * dsh-memory --self-test # run a temp-dir round-trip test (npm test)
@@ -22,6 +25,8 @@ import { execFileSync } from 'node:child_process'
22
25
  import { tmpdir, homedir } from 'node:os'
23
26
  import { createHash } from 'node:crypto'
24
27
  import { ensureMemoryScaffold } from '../lib/scaffold.js'
28
+ import { touchPages, accessSummary, today } from '../lib/pages.js'
29
+ import { planIndex, applyIndex, indexIssues, syncDeclaredSummaries, INDEX_LINE_MAX } from '../lib/index-page.js'
25
30
 
26
31
  const META = new Set(['SOUL.md', 'MEMORY.md', 'BOOTSTRAP.md', 'index.md', 'log.md'])
27
32
 
@@ -74,17 +79,110 @@ function cmdInit(store, args) {
74
79
  : `store already present: ${store} (nothing created; existing files never overwritten)`)
75
80
  }
76
81
 
77
- function cmdSearch(store, query) {
82
+ function cmdSearch(store, query, touch = false) {
78
83
  if (!query) fail('search needs a query')
79
84
  const q = query.toLowerCase()
80
85
  const hits = []
86
+ const hitPages = []
81
87
  for (const f of walk(store).filter((x) => x.endsWith('.md'))) {
82
88
  const text = readFileSync(f, 'utf8')
83
89
  for (const [i, line] of text.split('\n').entries()) {
84
- if (line.toLowerCase().includes(q)) hits.push(`${relative(store, f)}:${i + 1}: ${line.trim().slice(0, 200)}`)
90
+ if (line.toLowerCase().includes(q)) {
91
+ hits.push(`${relative(store, f)}:${i + 1}: ${line.trim().slice(0, 200)}`)
92
+ const rel = relative(store, f)
93
+ if (!META.has(basename(rel)) && !hitPages.includes(rel)) hitPages.push(rel)
94
+ }
85
95
  }
86
96
  }
87
97
  console.log(hits.length ? hits.join('\n') : `no matches for "${query}"`)
98
+ if (touch && hitPages.length > 0) {
99
+ const stamped = touchPages(store, hitPages)
100
+ console.log(`— last_access ${today()} stamped on ${stamped.length} page(s)`)
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Stamp `last_access` on pages. With no arguments, touch every page — the
106
+ * "I just read through the store" case; with arguments, only the named ones.
107
+ */
108
+ function cmdTouch(store, args) {
109
+ const all = pages(store)
110
+ const requested = args.filter((a) => !a.startsWith('-'))
111
+ const targets = requested.length > 0 ? requested : all
112
+ const unknown = requested.filter((p) => !all.includes(p))
113
+ const stamped = touchPages(store, targets)
114
+ console.log(`stamped last_access ${today()} on ${stamped.length}/${targets.length} page(s)`)
115
+ if (unknown.length > 0) console.log(`skipped (not a page): ${unknown.join(', ')}`)
116
+ }
117
+
118
+ /**
119
+ * Index compiler: report drift (default / `--check`) or rewrite the stale lines
120
+ * (`--write`). Only existing entry lines are rewritten — sections, order and
121
+ * unmentioned pages are left for the agent to decide.
122
+ */
123
+ function cmdIndex(store, args) {
124
+ const plan = planIndex(store)
125
+ const write = args.includes('--write')
126
+ const check = args.includes('--check')
127
+ const sync = args.includes('--sync-frontmatter') || args.includes('--sync')
128
+ const overlong = plan.lines.filter((l) => l.current.length > INDEX_LINE_MAX).length
129
+ const stale = plan.lines.filter((l) => l.changed)
130
+ const noSummary = plan.lines.filter((l) => l.summary.length === 0)
131
+
132
+ if (sync) {
133
+ // One-shot migration: whatever summary already lives in the index moves into
134
+ // the page's frontmatter, after which the index is pure projection.
135
+ const result = syncDeclaredSummaries(store, plan)
136
+ console.log(`index: synced ${result.synced.length} summary declaration(s) into frontmatter`
137
+ + (result.skipped.length > 0 ? `, skipped ${result.skipped.length}` : ''))
138
+ for (const path of result.skipped.slice(0, 5)) console.log(` · skipped (no index summary to import): ${path}`)
139
+ return
140
+ }
141
+
142
+ if (write) {
143
+ const result = applyIndex(store, plan)
144
+ console.log(`index: rewrote ${result.written}/${plan.lines.length} line(s) `
145
+ + `(${result.bytesBefore} → ${result.bytesAfter} bytes)`)
146
+ if (plan.missing.length > 0) {
147
+ console.log(`index: ${plan.missing.length} page(s) not listed — add them by hand (section + order are editorial):`)
148
+ for (const path of plan.missing) console.log(` + ${path}`)
149
+ }
150
+ if (plan.removed.length > 0) {
151
+ console.log(`index: ${plan.removed.length} stale link(s) — decide whether to delete the line: ${plan.removed.join(', ')}`)
152
+ }
153
+ if (noSummary.length > 0) {
154
+ console.log(`index: ${noSummary.length} page(s) declare no summary — the line shows the title only; `
155
+ + 'add `summary:` to the page frontmatter and re-run')
156
+ }
157
+ return
158
+ }
159
+
160
+ if (check) {
161
+ console.log(`index: ${stale.length} stale / ${overlong} over-cap / ${noSummary.length} undeclared-summary `
162
+ + `of ${plan.lines.length} line(s)`)
163
+ for (const line of plan.lines) console.log(` ${line.changed ? '~' : '·'} ${line.target}`)
164
+ for (const path of plan.missing) console.log(` + missing from index: ${path}`)
165
+ for (const path of plan.removed) console.log(` - stale link: ${path}`)
166
+ if (stale.length > 0 || plan.missing.length > 0 || plan.removed.length > 0) process.exit(1)
167
+ return
168
+ }
169
+
170
+ if (stale.length > 0) {
171
+ console.log(`index: ${stale.length} of ${plan.lines.length} line(s) need a rewrite`
172
+ + (overlong > 0 ? `, ${overlong} over the ${INDEX_LINE_MAX}-char cap` : ''))
173
+ for (const line of stale.slice(0, 10)) console.log(` ~ ${line.target}`)
174
+ if (stale.length > 10) console.log(` … ${stale.length - 10} more`)
175
+ console.log('run `dsh-memory index --write` to rewrite them (sections and order are preserved)')
176
+ } else if (plan.lines.length > 0) {
177
+ console.log(`index: ${plan.lines.length} line(s) in sync with their pages`)
178
+ }
179
+ if (noSummary.length > 0) {
180
+ console.log(`index: ${noSummary.length} page(s) declare no summary (line shows title only): `
181
+ + noSummary.slice(0, 5).map((l) => l.target).join(', ') + (noSummary.length > 5 ? ' …' : ''))
182
+ console.log(' hint: `dsh-memory index --sync-frontmatter` imports the summary already written in index.md')
183
+ }
184
+ for (const path of plan.missing) console.log(` + missing from index (add by hand): ${path}`)
185
+ for (const path of plan.removed) console.log(` - stale link (decide by hand): ${path}`)
88
186
  }
89
187
 
90
188
  function cmdLint(store) {
@@ -108,8 +206,17 @@ function cmdLint(store) {
108
206
  problems.push(`${p}: salience must be 1|2|3, got "${fm.salience}"`)
109
207
  }
110
208
  }
209
+ // Routing-table discipline: a line that has drifted from its page, or grown
210
+ // past the cap, is a lint failure — not a style preference. The index is
211
+ // injected whole, so an overgrown line costs every session context.
212
+ for (const issue of indexIssues(store, { includeNoSummary: false })) {
213
+ if (issue.kind === 'missing' || issue.kind === 'removed') continue // already reported above
214
+ problems.push(issue.detail)
215
+ }
111
216
  const log = readFileSync(join(store, 'log.md'), 'utf8')
112
- const badLines = log.split('\n').filter((l) => l.startsWith('## ') && !/^## \[\d{4}-\d{2}-\d{2}\] /.test(l))
217
+ // Quoted example lines (`> ## [...]`) are documentation, not entries.
218
+ const badLines = log.split('\n')
219
+ .filter((l) => l.startsWith('## ') && !/^## \[\d{4}-\d{2}-\d{2}\] /.test(l))
113
220
  for (const l of badLines) problems.push(`log.md malformed entry: ${l}`)
114
221
  console.log(problems.length ? problems.join('\n') : `ok — ${ps.length} pages, no problems found`)
115
222
  process.exit(problems.length ? 1 : 0)
@@ -132,6 +239,10 @@ function cmdStatus(store) {
132
239
  console.log(`by type: ${JSON.stringify(byType)}`)
133
240
  console.log(`last modified: ${last ? new Date(last).toISOString() : 'n/a'}`)
134
241
  console.log(`last log write: ${logMtime ? `${new Date(logMtime).toISOString()} (${Math.round((Date.now() - logMtime) / 60000)} min ago)` : 'n/a'}`)
242
+ const access = accessSummary(store)
243
+ const oldest = access.oldest ? `${access.oldest.lastAccess} (${access.oldest.path})` : 'n/a'
244
+ console.log(`stale pages (>90d or unstamped): ${access.stale.length}/${access.total}${access.missing > 0 ? ` (${access.missing} missing last_access)` : ''}`)
245
+ console.log(`oldest last_access: ${oldest}`)
135
246
  }
136
247
 
137
248
  function manifestFor(storePath) {
@@ -231,7 +342,10 @@ if (args[0] === '--self-test') {
231
342
  } else {
232
343
  const help = `dsh-memory — long-term memory CLI
233
344
  init [dir] create the store scaffold
234
- search <query> full-text search
345
+ search <query> [--touch] full-text search (--touch stamps last_access on hits)
346
+ touch [pages...] stamp last_access (all pages when none given)
347
+ index [--check|--write|--sync-frontmatter]
348
+ index routing table: report drift / rewrite / import summaries
235
349
  lint integrity check
236
350
  status health summary
237
351
  pack [out.tar.gz] export portable archive + manifest
@@ -239,7 +353,9 @@ if (args[0] === '--self-test') {
239
353
  Store: $MEMORY_DIR or ./.memory or ~/.memory (current: ${store})`
240
354
  switch (args[0]) {
241
355
  case 'init': cmdInit(store, args.slice(1)); break
242
- case 'search': cmdSearch(store, args[1]); break
356
+ case 'search': cmdSearch(store, args[1], args.includes('--touch')); break
357
+ case 'touch': cmdTouch(store, args.slice(1)); break
358
+ case 'index': cmdIndex(store, args.slice(1)); break
243
359
  case 'lint': cmdLint(store); break
244
360
  case 'status': cmdStatus(store); break
245
361
  case 'pack': cmdPack(store, args[1]); break
package/skills/memory.md CHANGED
@@ -15,10 +15,10 @@
15
15
 
16
16
  ## 四个操作
17
17
 
18
- - **remember(记)**:会话中或收尾时,把值得持久化的内容蒸馏成/更新为页面,并同步更新 `index.md`、追加 `log.md`。一条事实可能 touch 多个页(例:一次决策同时更新 `decisions/`、`user/`、`projects/`)。
19
- - **recall(忆)**:先查 `index.md`,再钻具体页;必要时用 `dsh-memory search`。
20
- - **consolidate(整理)**:定期 `dsh-memory lint`——查矛盾、过时声明、孤儿页、缺交叉引用、该归档的冷页;合并重复页。
21
- - **forget(忘)**:显式遗忘(用户要求删除/修正)+ 自动衰减(按 salience 与 last_access,冷页归档或删除,见 `MEMORY.md`)。
18
+ - **remember(记)**:会话中或收尾时,把值得持久化的内容蒸馏成/更新为页面,并同步更新 `index.md`、追加 `log.md`。一条事实可能 touch 多个页(例:一次决策同时更新 `decisions/`、`user/`、`projects/`)。维护 `index.md` 时守**路由表纪律**:一行一页、链接 + 一句话摘要(≤ 80 字)+ `salience`,不写状态流水——index 会被完整注入会话,它膨胀的代价是挤掉别的记忆;一行写不下就说明该拆页或该把细节移进正文。
19
+ - **recall(忆)**:先查 `index.md`,再钻具体页;必要时用 `dsh-memory search <关键词>`;读完一批页可用 `dsh-memory touch <pages...>` 补记访问。
20
+ - **consolidate(整理)**:定期 `dsh-memory lint`——查矛盾、过时声明、孤儿页、缺交叉引用、该归档的冷页;合并重复页;`dsh-memory status` 看陈旧页(>90 天未访问)分布。\n- **index 同步**:index 行**由页面的 frontmatter `summary:` 派生**,不要手写 index 散文。写完页面后:先 `dsh-memory index` 看漂移,再 `dsh-memory index --write` 重写(只改行内容,分节与顺序不动);`--check` 供机器判定(有漂移则退出码非 0);老库可用 `--sync-frontmatter` 把已经写在 index 里的摘要回填进页面。缺 `summary:` 的页面只显示标题——补上它。
21
+ - **forget(忘)**:显式遗忘(用户要求删除/修正)+ 自动衰减(按 salience 与 last_access,冷页归档或删除,见 `MEMORY.md`)。`last_access` 由插件自动维护(boot 把实际注入的页面戳为当天),你不需要逐日手改,但写页时要保留该字段。
22
22
 
23
23
  ## 何时写、写什么
24
24