dsh-plugin-memory 0.16.1 → 0.17.1

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/reveal.js ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Reveal a memory page in the OS file manager (Finder / Explorer / xdg).
3
+ *
4
+ * The browser half cannot touch the filesystem, so the panel POSTs the page id
5
+ * to `/api/memory/reveal` and this module runs the platform command. Every path
6
+ * is resolved and confined to the memory store before anything is executed:
7
+ * a crafted id must never reveal (or hint at) a file outside the store.
8
+ *
9
+ * The command is awaited on purpose: `open -R` can fail (sandbox, missing
10
+ * binary), and a fire-and-forget call would report success while nothing
11
+ * opened. Failures carry `code: 'exec-failed'` so the route can answer 500 and
12
+ * the panel can fall back to copying the path.
13
+ *
14
+ * @module dsh-plugin-memory/reveal
15
+ */
16
+
17
+ import { execFile } from 'node:child_process'
18
+ import { existsSync, statSync } from 'node:fs'
19
+ import { dirname, resolve, sep } from 'node:path'
20
+
21
+ /**
22
+ * Resolve `relativePath` inside `memoryDir`, rejecting escapes and non-files.
23
+ *
24
+ * @param {string} memoryDir
25
+ * @param {string} relativePath
26
+ * @returns {string} absolute file path
27
+ */
28
+ export function resolveMemoryFile(memoryDir, relativePath) {
29
+ const root = resolve(String(memoryDir ?? ''))
30
+ const raw = String(relativePath ?? '')
31
+ if (raw.length === 0 || raw.includes('\0')) throw new Error('invalid id')
32
+ const candidate = resolve(root, raw)
33
+ if (candidate !== root && !candidate.startsWith(root + sep)) throw new Error('path escapes the memory store')
34
+ if (!existsSync(candidate)) throw new Error('file does not exist')
35
+ if (!statSync(candidate).isFile()) throw new Error('not a file')
36
+ return candidate
37
+ }
38
+
39
+ /** The platform command that opens the file manager with the file selected. */
40
+ function revealCommand(platform, path) {
41
+ if (platform === 'darwin') return ['open', ['-R', path]]
42
+ // explorer wants the selection switch and the path as separate argv
43
+ // entries; one combined string breaks on paths containing spaces.
44
+ if (platform === 'win32') return ['explorer', ['/select,', path]]
45
+ return ['xdg-open', [dirname(path)]]
46
+ }
47
+
48
+ /**
49
+ * Reveal one memory page in the file manager, selecting the file.
50
+ *
51
+ * @param {string} memoryDir absolute store dir
52
+ * @param {string} relativePath page id (e.g. `projects/active/x.md`)
53
+ * @param {{ execFile?: (cmd: string, args: string[], cb: (error: Error | null) => void) => void, platform?: string }} [options]
54
+ * @returns {Promise<{ ok: true, path: string }>}
55
+ */
56
+ export async function revealFile(memoryDir, relativePath, options = {}) {
57
+ const run = options.execFile ?? execFile
58
+ const platform = options.platform ?? process.platform
59
+ let path
60
+ try {
61
+ path = resolveMemoryFile(memoryDir, relativePath)
62
+ } catch (error) {
63
+ error.code = 'invalid-id'
64
+ throw error
65
+ }
66
+ const [command, args] = revealCommand(platform, path)
67
+ await new Promise((resolvePromise, reject) => {
68
+ try {
69
+ run(command, args, (error) => {
70
+ // explorer.exe exits with code 1 after opening the window; that is a
71
+ // success, not a failure.
72
+ if (error && !(platform === 'win32' && error.code === 1)) reject(error)
73
+ else resolvePromise()
74
+ })
75
+ } catch (error) {
76
+ reject(error)
77
+ }
78
+ }).catch((error) => {
79
+ if (error.code === undefined) error.code = 'exec-failed'
80
+ throw error
81
+ })
82
+ return { ok: true, path }
83
+ }
@@ -26,6 +26,7 @@
26
26
 
27
27
  import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
28
28
  import { homedir } from 'node:os'
29
+ import { expandPath } from './paths.js'
29
30
  import { dirname, join, resolve, sep } from 'node:path'
30
31
 
31
32
  /** File name of the session-override store, next to the plugin's memory.json. */
@@ -47,11 +48,7 @@ export function defaultSessionStatePath() {
47
48
  * @returns {string | undefined}
48
49
  */
49
50
  export function expandHome(value) {
50
- if (typeof value !== 'string') return undefined
51
- const trimmed = value.trim()
52
- if (trimmed === '~') return homedir()
53
- if (trimmed.startsWith('~/')) return join(homedir(), trimmed.slice(2))
54
- return trimmed
51
+ return expandPath(value)
55
52
  }
56
53
 
57
54
  /**
@@ -64,14 +61,18 @@ export function expandHome(value) {
64
61
  * @param {string[] | undefined} patterns
65
62
  * @returns {boolean}
66
63
  */
67
- export function matchesWorkspace(cwd, patterns) {
64
+ export function matchesWorkspace(cwd, patterns, options = {}) {
68
65
  if (typeof cwd !== 'string' || cwd.trim().length === 0) return false
69
- const target = resolve(cwd)
66
+ // Windows paths are case-insensitive: `C:\\Users\\X` and `c:\\users\\x` are
67
+ // the same workspace, so compare lowercased paths there.
68
+ const platform = options.platform ?? process.platform
69
+ const normalizeCase = (value) => (platform === 'win32' ? value.toLowerCase() : value)
70
+ const target = normalizeCase(resolve(cwd))
70
71
  if (!Array.isArray(patterns)) return false
71
72
  return patterns.some((pattern) => {
72
73
  const expanded = expandHome(pattern)
73
74
  if (expanded === undefined || expanded.length === 0) return false
74
- const root = resolve(expanded)
75
+ const root = normalizeCase(resolve(expanded))
75
76
  return target === root || target.startsWith(root.endsWith(sep) ? root : `${root}${sep}`)
76
77
  })
77
78
  }
package/lib/soul.js CHANGED
@@ -20,6 +20,7 @@
20
20
 
21
21
  import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
22
22
  import { join } from 'node:path'
23
+ import { normalizeNewlines } from './paths.js'
23
24
 
24
25
  /** Opening marker of the managed block. */
25
26
  export const SOUL_PROFILE_START = '<!-- dsh-memory:soul:start -->'
@@ -128,7 +129,7 @@ function readLegacySoul(text) {
128
129
  export function readSoulProfile(memoryDir) {
129
130
  let text = ''
130
131
  try {
131
- text = readFileSync(join(memoryDir, 'SOUL.md'), 'utf8')
132
+ text = normalizeNewlines(readFileSync(join(memoryDir, 'SOUL.md'), 'utf8'))
132
133
  } catch {
133
134
  text = ''
134
135
  }
@@ -216,15 +217,19 @@ export function writeSoulProfile(memoryDir, profile = {}) {
216
217
  if (summary.length > SOUL_SUMMARY_MAX) throw new Error(`summary must be at most ${SOUL_SUMMARY_MAX} characters`)
217
218
 
218
219
  const path = join(memoryDir, 'SOUL.md')
219
- let text
220
+ let raw
220
221
  try {
221
- text = readFileSync(path, 'utf8')
222
+ raw = readFileSync(path, 'utf8')
222
223
  } catch {
223
224
  throw new Error('SOUL.md not found (run the memory scaffold first)')
224
225
  }
225
- const updated = replaceOrInsert(text, renderBlock({ agentName, userName, summary }))
226
+ // Parse normalised; keep the file's own newline style on write-back so a
227
+ // Windows CRLF file is not half-converted to LF.
228
+ const crlf = raw.includes('\r\n')
229
+ const updated = replaceOrInsert(normalizeNewlines(raw), renderBlock({ agentName, userName, summary }))
230
+ const output = crlf ? updated.replace(/\n/g, '\r\n') : updated
226
231
  const tmp = `${path}.tmp`
227
- writeFileSync(tmp, updated, 'utf8')
232
+ writeFileSync(tmp, output, 'utf8')
228
233
  renameSync(tmp, path)
229
234
  return { agentName, userName, summary, managed: true }
230
235
  }
@@ -238,18 +243,21 @@ export function writeSoulProfile(memoryDir, profile = {}) {
238
243
  export function markBootstrapComplete(memoryDir) {
239
244
  const path = join(memoryDir, 'BOOTSTRAP.md')
240
245
  if (!existsSync(path)) return false
241
- let text
246
+ let raw
242
247
  try {
243
- text = readFileSync(path, 'utf8')
248
+ raw = readFileSync(path, 'utf8')
244
249
  } catch {
245
250
  return false
246
251
  }
252
+ const crlf = raw.includes('\r\n')
253
+ const text = normalizeNewlines(raw)
247
254
  if (/^status:\s*complete\s*$/m.test(text)) return false
248
255
  const updated = /^status:\s*\S+/m.test(text)
249
256
  ? text.replace(/^status:\s*\S+/m, 'status: complete')
250
257
  : `---\nstatus: complete\n---\n\n${text}`
258
+ const output = crlf ? updated.replace(/\n/g, '\r\n') : updated
251
259
  const tmp = `${path}.tmp`
252
- writeFileSync(tmp, updated, 'utf8')
260
+ writeFileSync(tmp, output, 'utf8')
253
261
  renameSync(tmp, path)
254
262
  return true
255
263
  }
@@ -110,6 +110,12 @@ export const SESSION_ROUTE_PATH: '/api/memory/session'
110
110
  /** Basic soul profile (managed SOUL.md block): read fields (GET) / save (POST). */
111
111
  export const SOUL_ROUTE_PATH: '/api/memory/soul'
112
112
 
113
+ /** Reveal a memory page in the OS file manager (POST { id }). */
114
+ export const REVEAL_ROUTE_PATH: '/api/memory/reveal'
115
+
116
+ /** Resolve a memory page to its absolute path (GET ?id=). */
117
+ export const PATH_ROUTE_PATH: '/api/memory/path'
118
+
113
119
  /** User-editable settings (composition config is the base layer). */
114
120
  export interface MemorySettings {
115
121
  enabled?: boolean
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.16.1",
4
+ "version": "0.17.1",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "main": "lib/index.js",
@@ -25,6 +25,7 @@ import {
25
25
  import { join, resolve, relative, basename, sep } from 'node:path'
26
26
  import { execFileSync } from 'node:child_process'
27
27
  import { tmpdir, homedir } from 'node:os'
28
+ import { expandPath } from '../lib/paths.js'
28
29
  import { createHash } from 'node:crypto'
29
30
  import { ensureMemoryScaffold } from '../lib/scaffold.js'
30
31
  import { touchPages, accessSummary, today, listPagesWithMeta } from '../lib/pages.js'
@@ -35,7 +36,7 @@ import { runCheckup, formatCheckup, hasWork } from '../lib/checkup.js'
35
36
  const META = new Set(['SOUL.md', 'MEMORY.md', 'BOOTSTRAP.md', 'index.md', 'log.md'])
36
37
 
37
38
  function resolveStore() {
38
- if (process.env.MEMORY_DIR) return resolve(process.env.MEMORY_DIR)
39
+ if (process.env.MEMORY_DIR) return resolve(expandPath(process.env.MEMORY_DIR) ?? process.env.MEMORY_DIR)
39
40
  const local = resolve('.memory')
40
41
  if (existsSync(local)) return local
41
42
  return join(homedir(), '.memory')
@@ -77,7 +78,7 @@ function parseFrontmatter(text) {
77
78
  function cmdInit(store, args) {
78
79
  if (args[0]) {
79
80
  // Explicit dir argument wins over store resolution.
80
- store = resolve(args[0])
81
+ store = resolve(expandPath(args[0]) ?? args[0])
81
82
  }
82
83
  const created = ensureMemoryScaffold(store)
83
84
  console.log(created.length > 0