dsh-plugin-memory 0.6.0 → 0.8.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/CHANGELOG.md +46 -0
- package/README.md +53 -12
- package/lib/boot.js +133 -8
- package/lib/checkup.js +235 -0
- package/lib/client.js +643 -2
- package/lib/digest-guard.js +43 -3
- package/lib/graph-drag.js +100 -0
- package/lib/graph-layout.js +137 -0
- package/lib/graph-view.js +98 -0
- package/lib/graph.js +395 -0
- package/lib/index-page.js +419 -0
- package/lib/index.js +130 -6
- package/lib/insights.js +275 -0
- package/lib/pages.js +238 -0
- package/lib/scaffold.js +14 -6
- package/lib/types/index.d.ts +98 -3
- package/package.json +1 -1
- package/scripts/memory.mjs +244 -7
- package/skills/memory.md +4 -4
package/lib/types/index.d.ts
CHANGED
|
@@ -8,7 +8,6 @@ import type { Context } from '@deepseek-ai/cordis'
|
|
|
8
8
|
|
|
9
9
|
/** Plugin entry name. */
|
|
10
10
|
export const name: 'memory'
|
|
11
|
-
|
|
12
11
|
/** Services required by the plugin. */
|
|
13
12
|
export const inject: ['systemPrompt', 'skills', 'agents']
|
|
14
13
|
|
|
@@ -20,8 +19,16 @@ export interface MemoryConfig {
|
|
|
20
19
|
memoryDir?: string
|
|
21
20
|
/** Files injected at session start. Default `['SOUL.md', 'MEMORY.md', 'index.md']`. */
|
|
22
21
|
bootFiles?: string[]
|
|
23
|
-
/** Total character budget of the boot block. Default `
|
|
22
|
+
/** Total character budget of the boot block. Default `12000`. */
|
|
24
23
|
bootMaxChars?: number
|
|
24
|
+
/**
|
|
25
|
+
* Explicit per-file character caps for the boot block (composition-time
|
|
26
|
+
* only). Files without a cap share what is left, with `index.md` taking its
|
|
27
|
+
* share first so the routing table is never starved.
|
|
28
|
+
*/
|
|
29
|
+
bootFileBudgets?: Record<string, number>
|
|
30
|
+
/** Stamp `last_access` on the pages a session actually loaded. Default `true`. */
|
|
31
|
+
trackPageAccess?: boolean
|
|
25
32
|
/** Inject the boot block at session start. Default `true`. */
|
|
26
33
|
autoInject?: boolean
|
|
27
34
|
/** Don't inject anything until the session's first real user message. Default `true`. */
|
|
@@ -64,6 +71,9 @@ export const Config: import('@deepseek-ai/schemastery').default<MemoryConfig>
|
|
|
64
71
|
/** HTTP route serving the user-facing config to the Settings panel. */
|
|
65
72
|
export const CONFIG_ROUTE_PATH: '/api/memory/config'
|
|
66
73
|
|
|
74
|
+
/** Read-only HTTP route serving the memory-health dashboard payload. */
|
|
75
|
+
export const INSIGHTS_ROUTE_PATH: '/api/memory/insights'
|
|
76
|
+
|
|
67
77
|
/** User-editable settings (composition config is the base layer). */
|
|
68
78
|
export interface MemorySettings {
|
|
69
79
|
enabled?: boolean
|
|
@@ -114,9 +124,55 @@ export default plugin
|
|
|
114
124
|
/** Render the boot memory block injected at session start. */
|
|
115
125
|
export function renderBootBlock(
|
|
116
126
|
memoryDir: string,
|
|
117
|
-
options?: {
|
|
127
|
+
options?: {
|
|
128
|
+
bootFiles?: string[]
|
|
129
|
+
bootMaxChars?: number
|
|
130
|
+
bootFileBudgets?: Record<string, number>
|
|
131
|
+
},
|
|
118
132
|
): string
|
|
119
133
|
|
|
134
|
+
/** Files injected at session start when the config does not override them. */
|
|
135
|
+
export const DEFAULT_BOOT_FILES: string[]
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Split the boot budget across the injected files: explicit per-file caps
|
|
139
|
+
* first, then `index.md`'s share, then an even split, each clamped to the
|
|
140
|
+
* file's real size.
|
|
141
|
+
*/
|
|
142
|
+
export function allocateBootBudget(
|
|
143
|
+
memoryDir: string,
|
|
144
|
+
options?: {
|
|
145
|
+
files?: string[]
|
|
146
|
+
total?: number
|
|
147
|
+
perFile?: Record<string, number>
|
|
148
|
+
},
|
|
149
|
+
): Map<string, number>
|
|
150
|
+
|
|
151
|
+
/** Walk a store and return every page path relative to its root. */
|
|
152
|
+
export function listPagePaths(memoryDir: string, dirs?: string[]): string[]
|
|
153
|
+
|
|
154
|
+
/** Extract the real, store-relative pages a rendered boot block refers to. */
|
|
155
|
+
export function extractReferencedPages(text: string, memoryDir: string, dirs?: string[]): string[]
|
|
156
|
+
|
|
157
|
+
/** Stamp `last_access` (`YYYY-MM-DD`, today by default) on the given pages. */
|
|
158
|
+
export function touchPages(
|
|
159
|
+
memoryDir: string,
|
|
160
|
+
paths: string[],
|
|
161
|
+
options?: { date?: string; dirs?: string[] },
|
|
162
|
+
): string[]
|
|
163
|
+
|
|
164
|
+
/** Rewrite one page's `last_access` when it differs. Returns true when written. */
|
|
165
|
+
export function touchPageFile(filePath: string, date?: string): boolean
|
|
166
|
+
|
|
167
|
+
/** Today as `YYYY-MM-DD` (local time). */
|
|
168
|
+
export function today(): string
|
|
169
|
+
|
|
170
|
+
/** Store-wide `last_access` summary used by `dsh-memory status`. */
|
|
171
|
+
export function accessSummary(
|
|
172
|
+
memoryDir: string,
|
|
173
|
+
options?: { staleDays?: number; now?: Date },
|
|
174
|
+
): { total: number; missing: number; stale: string[]; oldest?: { path: string; lastAccess: string } }
|
|
175
|
+
|
|
120
176
|
/** Create the memory-store layout if missing. Returns created paths. */
|
|
121
177
|
export function ensureMemoryScaffold(memoryDir: string): string[]
|
|
122
178
|
|
|
@@ -134,3 +190,42 @@ export class ActivityTracker {
|
|
|
134
190
|
shouldInject(id: string, config?: { deferUntilUserSpeaks?: boolean; activeSessionOnly?: boolean }): boolean
|
|
135
191
|
dispose(): void
|
|
136
192
|
}
|
|
193
|
+
|
|
194
|
+
/** Memory-health dashboard payload (read-only, computed per request). */
|
|
195
|
+
export interface MemoryInsights {
|
|
196
|
+
generatedAt: string
|
|
197
|
+
store: string
|
|
198
|
+
overview: {
|
|
199
|
+
pages: number
|
|
200
|
+
chars: number
|
|
201
|
+
bytes: number
|
|
202
|
+
indexEntries: number
|
|
203
|
+
indexBytes: number
|
|
204
|
+
logBytes: number
|
|
205
|
+
lastWrite: string | null
|
|
206
|
+
neverAccessed: number
|
|
207
|
+
}
|
|
208
|
+
byType: Array<{ type: string, label: string, count: number }>
|
|
209
|
+
bySalience: Array<{ level: 1 | 2 | 3, count: number }>
|
|
210
|
+
freshness: {
|
|
211
|
+
buckets: Array<{ key: string, label: string, count: number }>
|
|
212
|
+
never: number
|
|
213
|
+
}
|
|
214
|
+
stalePages: Array<{
|
|
215
|
+
path: string
|
|
216
|
+
title: string
|
|
217
|
+
salience?: number
|
|
218
|
+
daysSinceAccess: number | null
|
|
219
|
+
}>
|
|
220
|
+
recent: string[]
|
|
221
|
+
health: {
|
|
222
|
+
score: number
|
|
223
|
+
checks: Array<{ id: string, ok: boolean, label: string, detail: string }>
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Build the dashboard payload from the store on disk (never writes). */
|
|
228
|
+
export function collectInsights(memoryDir: string): MemoryInsights
|
|
229
|
+
|
|
230
|
+
/** Newest-first `log.md` headlines for the dashboard. */
|
|
231
|
+
export function readRecentLog(memoryDir: string, count?: number): string[]
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-memory",
|
|
3
3
|
"description": "Long-term memory plugin for DeepSeek Harness: persistent cross-session markdown memory with a soul/persona file, auto-injected at every session start, plus remember/recall/consolidate/forget workflows and portable migration tooling.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.8.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|
package/scripts/memory.mjs
CHANGED
|
@@ -4,9 +4,14 @@
|
|
|
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>
|
|
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 graph [--suggest] # relationship report / citation suggestions
|
|
10
|
+
* dsh-memory checkup [--boot=N] # one health report with prioritised actions
|
|
11
|
+
* dsh-memory index [--check|--write|--sync-frontmatter]
|
|
12
|
+
* # index routing table: drift / rewrite / import summaries
|
|
8
13
|
* dsh-memory lint # integrity check (index vs files, orphans, log format)
|
|
9
|
-
* dsh-memory status # health summary (page counts, sizes, last modified)
|
|
14
|
+
* dsh-memory status # health summary (page counts, sizes, staleness, last modified)
|
|
10
15
|
* dsh-memory pack [out.tar.gz] # export a portable archive + manifest
|
|
11
16
|
* dsh-memory unpack <archive> [--force] # restore from an archive
|
|
12
17
|
* dsh-memory --self-test # run a temp-dir round-trip test (npm test)
|
|
@@ -22,6 +27,10 @@ import { execFileSync } from 'node:child_process'
|
|
|
22
27
|
import { tmpdir, homedir } from 'node:os'
|
|
23
28
|
import { createHash } from 'node:crypto'
|
|
24
29
|
import { ensureMemoryScaffold } from '../lib/scaffold.js'
|
|
30
|
+
import { touchPages, accessSummary, today, listPagesWithMeta } from '../lib/pages.js'
|
|
31
|
+
import { planIndex, applyIndex, indexIssues, syncDeclaredSummaries, INDEX_LINE_MAX } from '../lib/index-page.js'
|
|
32
|
+
import { buildMemoryGraph, suggestLinks } from '../lib/graph.js'
|
|
33
|
+
import { runCheckup, formatCheckup, hasWork } from '../lib/checkup.js'
|
|
25
34
|
|
|
26
35
|
const META = new Set(['SOUL.md', 'MEMORY.md', 'BOOTSTRAP.md', 'index.md', 'log.md'])
|
|
27
36
|
|
|
@@ -74,17 +83,110 @@ function cmdInit(store, args) {
|
|
|
74
83
|
: `store already present: ${store} (nothing created; existing files never overwritten)`)
|
|
75
84
|
}
|
|
76
85
|
|
|
77
|
-
function cmdSearch(store, query) {
|
|
86
|
+
function cmdSearch(store, query, touch = false) {
|
|
78
87
|
if (!query) fail('search needs a query')
|
|
79
88
|
const q = query.toLowerCase()
|
|
80
89
|
const hits = []
|
|
90
|
+
const hitPages = []
|
|
81
91
|
for (const f of walk(store).filter((x) => x.endsWith('.md'))) {
|
|
82
92
|
const text = readFileSync(f, 'utf8')
|
|
83
93
|
for (const [i, line] of text.split('\n').entries()) {
|
|
84
|
-
if (line.toLowerCase().includes(q))
|
|
94
|
+
if (line.toLowerCase().includes(q)) {
|
|
95
|
+
hits.push(`${relative(store, f)}:${i + 1}: ${line.trim().slice(0, 200)}`)
|
|
96
|
+
const rel = relative(store, f)
|
|
97
|
+
if (!META.has(basename(rel)) && !hitPages.includes(rel)) hitPages.push(rel)
|
|
98
|
+
}
|
|
85
99
|
}
|
|
86
100
|
}
|
|
87
101
|
console.log(hits.length ? hits.join('\n') : `no matches for "${query}"`)
|
|
102
|
+
if (touch && hitPages.length > 0) {
|
|
103
|
+
const stamped = touchPages(store, hitPages)
|
|
104
|
+
console.log(`— last_access ${today()} stamped on ${stamped.length} page(s)`)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Stamp `last_access` on pages. With no arguments, touch every page — the
|
|
110
|
+
* "I just read through the store" case; with arguments, only the named ones.
|
|
111
|
+
*/
|
|
112
|
+
function cmdTouch(store, args) {
|
|
113
|
+
const all = pages(store)
|
|
114
|
+
const requested = args.filter((a) => !a.startsWith('-'))
|
|
115
|
+
const targets = requested.length > 0 ? requested : all
|
|
116
|
+
const unknown = requested.filter((p) => !all.includes(p))
|
|
117
|
+
const stamped = touchPages(store, targets)
|
|
118
|
+
console.log(`stamped last_access ${today()} on ${stamped.length}/${targets.length} page(s)`)
|
|
119
|
+
if (unknown.length > 0) console.log(`skipped (not a page): ${unknown.join(', ')}`)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Index compiler: report drift (default / `--check`) or rewrite the stale lines
|
|
124
|
+
* (`--write`). Only existing entry lines are rewritten — sections, order and
|
|
125
|
+
* unmentioned pages are left for the agent to decide.
|
|
126
|
+
*/
|
|
127
|
+
function cmdIndex(store, args) {
|
|
128
|
+
const plan = planIndex(store)
|
|
129
|
+
const write = args.includes('--write')
|
|
130
|
+
const check = args.includes('--check')
|
|
131
|
+
const sync = args.includes('--sync-frontmatter') || args.includes('--sync')
|
|
132
|
+
const overlong = plan.lines.filter((l) => l.current.length > INDEX_LINE_MAX).length
|
|
133
|
+
const stale = plan.lines.filter((l) => l.changed)
|
|
134
|
+
const noSummary = plan.lines.filter((l) => l.summary.length === 0)
|
|
135
|
+
|
|
136
|
+
if (sync) {
|
|
137
|
+
// One-shot migration: whatever summary already lives in the index moves into
|
|
138
|
+
// the page's frontmatter, after which the index is pure projection.
|
|
139
|
+
const result = syncDeclaredSummaries(store, plan)
|
|
140
|
+
console.log(`index: synced ${result.synced.length} summary declaration(s) into frontmatter`
|
|
141
|
+
+ (result.skipped.length > 0 ? `, skipped ${result.skipped.length}` : ''))
|
|
142
|
+
for (const path of result.skipped.slice(0, 5)) console.log(` · skipped (no index summary to import): ${path}`)
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (write) {
|
|
147
|
+
const result = applyIndex(store, plan)
|
|
148
|
+
console.log(`index: rewrote ${result.written}/${plan.lines.length} line(s) `
|
|
149
|
+
+ `(${result.bytesBefore} → ${result.bytesAfter} bytes)`)
|
|
150
|
+
if (plan.missing.length > 0) {
|
|
151
|
+
console.log(`index: ${plan.missing.length} page(s) not listed — add them by hand (section + order are editorial):`)
|
|
152
|
+
for (const path of plan.missing) console.log(` + ${path}`)
|
|
153
|
+
}
|
|
154
|
+
if (plan.removed.length > 0) {
|
|
155
|
+
console.log(`index: ${plan.removed.length} stale link(s) — decide whether to delete the line: ${plan.removed.join(', ')}`)
|
|
156
|
+
}
|
|
157
|
+
if (noSummary.length > 0) {
|
|
158
|
+
console.log(`index: ${noSummary.length} page(s) declare no summary — the line shows the title only; `
|
|
159
|
+
+ 'add `summary:` to the page frontmatter and re-run')
|
|
160
|
+
}
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (check) {
|
|
165
|
+
console.log(`index: ${stale.length} stale / ${overlong} over-cap / ${noSummary.length} undeclared-summary `
|
|
166
|
+
+ `of ${plan.lines.length} line(s)`)
|
|
167
|
+
for (const line of plan.lines) console.log(` ${line.changed ? '~' : '·'} ${line.target}`)
|
|
168
|
+
for (const path of plan.missing) console.log(` + missing from index: ${path}`)
|
|
169
|
+
for (const path of plan.removed) console.log(` - stale link: ${path}`)
|
|
170
|
+
if (stale.length > 0 || plan.missing.length > 0 || plan.removed.length > 0) process.exit(1)
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (stale.length > 0) {
|
|
175
|
+
console.log(`index: ${stale.length} of ${plan.lines.length} line(s) need a rewrite`
|
|
176
|
+
+ (overlong > 0 ? `, ${overlong} over the ${INDEX_LINE_MAX}-char cap` : ''))
|
|
177
|
+
for (const line of stale.slice(0, 10)) console.log(` ~ ${line.target}`)
|
|
178
|
+
if (stale.length > 10) console.log(` … ${stale.length - 10} more`)
|
|
179
|
+
console.log('run `dsh-memory index --write` to rewrite them (sections and order are preserved)')
|
|
180
|
+
} else if (plan.lines.length > 0) {
|
|
181
|
+
console.log(`index: ${plan.lines.length} line(s) in sync with their pages`)
|
|
182
|
+
}
|
|
183
|
+
if (noSummary.length > 0) {
|
|
184
|
+
console.log(`index: ${noSummary.length} page(s) declare no summary (line shows title only): `
|
|
185
|
+
+ noSummary.slice(0, 5).map((l) => l.target).join(', ') + (noSummary.length > 5 ? ' …' : ''))
|
|
186
|
+
console.log(' hint: `dsh-memory index --sync-frontmatter` imports the summary already written in index.md')
|
|
187
|
+
}
|
|
188
|
+
for (const path of plan.missing) console.log(` + missing from index (add by hand): ${path}`)
|
|
189
|
+
for (const path of plan.removed) console.log(` - stale link (decide by hand): ${path}`)
|
|
88
190
|
}
|
|
89
191
|
|
|
90
192
|
function cmdLint(store) {
|
|
@@ -108,13 +210,132 @@ function cmdLint(store) {
|
|
|
108
210
|
problems.push(`${p}: salience must be 1|2|3, got "${fm.salience}"`)
|
|
109
211
|
}
|
|
110
212
|
}
|
|
213
|
+
// Routing-table discipline: a line that has drifted from its page, or grown
|
|
214
|
+
// past the cap, is a lint failure — not a style preference. The index is
|
|
215
|
+
// injected whole, so an overgrown line costs every session context.
|
|
216
|
+
for (const issue of indexIssues(store, { includeNoSummary: false })) {
|
|
217
|
+
if (issue.kind === 'missing' || issue.kind === 'removed') continue // already reported above
|
|
218
|
+
problems.push(issue.detail)
|
|
219
|
+
}
|
|
111
220
|
const log = readFileSync(join(store, 'log.md'), 'utf8')
|
|
112
|
-
|
|
221
|
+
// Quoted example lines (`> ## [...]`) are documentation, not entries.
|
|
222
|
+
const badLines = log.split('\n')
|
|
223
|
+
.filter((l) => l.startsWith('## ') && !/^## \[\d{4}-\d{2}-\d{2}\] /.test(l))
|
|
113
224
|
for (const l of badLines) problems.push(`log.md malformed entry: ${l}`)
|
|
114
225
|
console.log(problems.length ? problems.join('\n') : `ok — ${ps.length} pages, no problems found`)
|
|
115
226
|
process.exit(problems.length ? 1 : 0)
|
|
116
227
|
}
|
|
117
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Relationship report: what the graph looks like, and (with `--suggest`) who
|
|
231
|
+
* each under-connected page should probably cite. Suggestion only — the write
|
|
232
|
+
* stays with the agent, per the project's "no automatic rewrites" rule.
|
|
233
|
+
*/
|
|
234
|
+
function cmdGraph(store, args) {
|
|
235
|
+
const graph = buildMemoryGraph(store, { includeSuggestions: args.includes('--suggest') })
|
|
236
|
+
const { stats } = graph
|
|
237
|
+
console.log(`graph: ${stats.pages} 页 · ${stats.links} 条页间互链 · ${stats.tagEdges} 条共享 tag · ${stats.indexLinks} 条索引路由`)
|
|
238
|
+
if (stats.isolated.length > 0) {
|
|
239
|
+
console.log(`孤立页(连索引之外没有任何关系):${stats.isolated.length} 个`)
|
|
240
|
+
for (const path of stats.isolated) console.log(` · ${path}`)
|
|
241
|
+
}
|
|
242
|
+
if (!args.includes('--suggest')) {
|
|
243
|
+
console.log('用 `dsh-memory graph --suggest` 查看"这页该连谁"的建议(只报告,不写入)')
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
const suggestions = graph.suggestions ?? []
|
|
247
|
+
if (suggestions.length === 0) {
|
|
248
|
+
console.log('没有需要补链的页面 —— 每页都已经有内容层面的关系了')
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
console.log('')
|
|
252
|
+
console.log('补链建议(只提示,请自己确认后写进页面):')
|
|
253
|
+
for (const entry of suggestions) {
|
|
254
|
+
console.log(` ${entry.page}`)
|
|
255
|
+
for (const candidate of entry.candidates) {
|
|
256
|
+
console.log(` → ${candidate.target} [${candidate.reason}]`)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
console.log('')
|
|
260
|
+
console.log('写进页面示例:在正文里加 `见 [某页](../path.md)`,图谱与探索都会用到这条边。')
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Structured search.
|
|
265
|
+
*
|
|
266
|
+
* Keyword recall alone cannot answer "every decision tagged #memory-plugin",
|
|
267
|
+
* which is what an agent usually means; this filters on frontmatter first and
|
|
268
|
+
* only then matches text, so the result set is what was actually asked for.
|
|
269
|
+
*/
|
|
270
|
+
function cmdQuery(store, args) {
|
|
271
|
+
const filters = { tags: [], type: undefined, salience: undefined, stale: false, hot: false }
|
|
272
|
+
const words = []
|
|
273
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
274
|
+
const arg = args[i]
|
|
275
|
+
if (arg === '--tag' && args[i + 1] !== undefined) filters.tags.push(String(args[i += 1]).replace(/^#/, ''))
|
|
276
|
+
else if (arg === '--type' && args[i + 1] !== undefined) filters.type = args[i += 1]
|
|
277
|
+
else if (arg === '--salience' && args[i + 1] !== undefined) filters.salience = Number(args[i += 1])
|
|
278
|
+
else if (arg === '--stale') filters.stale = true
|
|
279
|
+
else if (arg === '--hot') filters.hot = true
|
|
280
|
+
else if (!arg.startsWith('--')) words.push(arg)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const all = listPagesWithMeta(store)
|
|
284
|
+
let matched = all
|
|
285
|
+
if (filters.tags.length > 0) matched = matched.filter((p) => filters.tags.every((t) => p.tags.includes(t)))
|
|
286
|
+
if (filters.type !== undefined) matched = matched.filter((p) => p.type === filters.type)
|
|
287
|
+
if (Number.isFinite(filters.salience)) matched = matched.filter((p) => p.salience === filters.salience)
|
|
288
|
+
if (filters.hot) matched = matched.filter((p) => p.salience === 1)
|
|
289
|
+
if (filters.stale) matched = matched.filter((p) => p.daysSinceAccess === undefined || p.daysSinceAccess > 90)
|
|
290
|
+
|
|
291
|
+
const shown = []
|
|
292
|
+
if (filters.tags.length > 0) shown.push(`#${filters.tags.join(' #')}`)
|
|
293
|
+
if (filters.type !== undefined) shown.push(`type=${filters.type}`)
|
|
294
|
+
if (Number.isFinite(filters.salience)) shown.push(`salience=${filters.salience}`)
|
|
295
|
+
if (filters.hot) shown.push('hot')
|
|
296
|
+
if (filters.stale) shown.push('stale')
|
|
297
|
+
const suffix = shown.length > 0 ? ` (${shown.join(' ')})` : ''
|
|
298
|
+
|
|
299
|
+
if (words.length === 0) {
|
|
300
|
+
console.log(`query: ${matched.length}/${all.length} pages${suffix}`)
|
|
301
|
+
for (const page of matched) {
|
|
302
|
+
console.log(` [s${page.salience ?? '?'}] ${page.path} — ${page.title}`)
|
|
303
|
+
}
|
|
304
|
+
return
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const needle = words.join(' ').toLowerCase()
|
|
308
|
+
const inScope = new Set(matched.map((p) => p.path))
|
|
309
|
+
const hits = []
|
|
310
|
+
const hitPages = new Set()
|
|
311
|
+
for (const rel of pages(store)) {
|
|
312
|
+
if (!inScope.has(rel)) continue
|
|
313
|
+
const text = readFileSync(join(store, rel), 'utf8')
|
|
314
|
+
for (const [index, line] of text.split('\n').entries()) {
|
|
315
|
+
if (line.toLowerCase().includes(needle)) {
|
|
316
|
+
hits.push(`${rel}:${index + 1}: ${line.trim().slice(0, 160)}`)
|
|
317
|
+
hitPages.add(rel)
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
console.log(`query "${words.join(' ')}"${suffix}: ${hitPages.size} page(s) matched`)
|
|
322
|
+
console.log(hits.length > 0 ? hits.join('\n') : ' no matches')
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* One report that says what to do next: the four readers (lint / status /
|
|
327
|
+
* insights / graph) each answer a question, but a user still has to work out
|
|
328
|
+
* which problem matters. Exit code is non-zero when there is work, so a cron
|
|
329
|
+
* or a human can both consume it.
|
|
330
|
+
*/
|
|
331
|
+
function cmdCheckup(store, args) {
|
|
332
|
+
const bootMaxChars = Number(args.find((a) => a.startsWith('--boot='))?.slice(7)) || 12000
|
|
333
|
+
const report = runCheckup(store, { bootMaxChars })
|
|
334
|
+
const color = !args.includes('--no-color')
|
|
335
|
+
console.log(formatCheckup(report, { color }))
|
|
336
|
+
process.exit(hasWork(report) ? 1 : 0)
|
|
337
|
+
}
|
|
338
|
+
|
|
118
339
|
function cmdStatus(store) {
|
|
119
340
|
const ps = pages(store)
|
|
120
341
|
const bytes = ps.map((p) => statSync(join(store, p)).size).reduce((a, b) => a + b, 0)
|
|
@@ -132,6 +353,10 @@ function cmdStatus(store) {
|
|
|
132
353
|
console.log(`by type: ${JSON.stringify(byType)}`)
|
|
133
354
|
console.log(`last modified: ${last ? new Date(last).toISOString() : 'n/a'}`)
|
|
134
355
|
console.log(`last log write: ${logMtime ? `${new Date(logMtime).toISOString()} (${Math.round((Date.now() - logMtime) / 60000)} min ago)` : 'n/a'}`)
|
|
356
|
+
const access = accessSummary(store)
|
|
357
|
+
const oldest = access.oldest ? `${access.oldest.lastAccess} (${access.oldest.path})` : 'n/a'
|
|
358
|
+
console.log(`stale pages (>90d or unstamped): ${access.stale.length}/${access.total}${access.missing > 0 ? ` (${access.missing} missing last_access)` : ''}`)
|
|
359
|
+
console.log(`oldest last_access: ${oldest}`)
|
|
135
360
|
}
|
|
136
361
|
|
|
137
362
|
function manifestFor(storePath) {
|
|
@@ -231,7 +456,14 @@ if (args[0] === '--self-test') {
|
|
|
231
456
|
} else {
|
|
232
457
|
const help = `dsh-memory — long-term memory CLI
|
|
233
458
|
init [dir] create the store scaffold
|
|
234
|
-
search <query>
|
|
459
|
+
search <query> [--touch] full-text search (--touch stamps last_access on hits)
|
|
460
|
+
touch [pages...] stamp last_access (all pages when none given)
|
|
461
|
+
graph [--suggest] relationship report / "who should this page cite"
|
|
462
|
+
checkup [--boot=N] one health report with a prioritised action list
|
|
463
|
+
query [--tag x] [--type t] [--salience n] [--hot|--stale] [words]
|
|
464
|
+
structured search: frontmatter filters, then text
|
|
465
|
+
index [--check|--write|--sync-frontmatter]
|
|
466
|
+
index routing table: report drift / rewrite / import summaries
|
|
235
467
|
lint integrity check
|
|
236
468
|
status health summary
|
|
237
469
|
pack [out.tar.gz] export portable archive + manifest
|
|
@@ -239,7 +471,12 @@ if (args[0] === '--self-test') {
|
|
|
239
471
|
Store: $MEMORY_DIR or ./.memory or ~/.memory (current: ${store})`
|
|
240
472
|
switch (args[0]) {
|
|
241
473
|
case 'init': cmdInit(store, args.slice(1)); break
|
|
242
|
-
case 'search': cmdSearch(store, args[1]); break
|
|
474
|
+
case 'search': cmdSearch(store, args[1], args.includes('--touch')); break
|
|
475
|
+
case 'touch': cmdTouch(store, args.slice(1)); break
|
|
476
|
+
case 'graph': cmdGraph(store, args.slice(1)); break
|
|
477
|
+
case 'checkup': cmdCheckup(store, args.slice(1)); break
|
|
478
|
+
case 'query': cmdQuery(store, args.slice(1)); break
|
|
479
|
+
case 'index': cmdIndex(store, args.slice(1)); break
|
|
243
480
|
case 'lint': cmdLint(store); break
|
|
244
481
|
case 'status': cmdStatus(store); break
|
|
245
482
|
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/`)。**每个新页/改动页补 1–3 条「相关页面」互链**(相对路径,例:`见 [内存面板决策](../decisions/settings-wire-allowlist-bypass.md)`)——互链是记忆网络唯一的骨架,`index.md` 只是目录;不写互链,页面之间就永远没有关系,图谱和探索都只能靠 tag 猜。维护 `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
|
|