sage-mem 0.9.3 → 0.9.4

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/README.md CHANGED
@@ -156,7 +156,7 @@ SAGE_MEM_DIR=/path/to/your/memory
156
156
  - **`baseline: true` 的记忆每场会话第一步无条件注入**,最多 8 条——给「你是谁」「待办清单」这类每次开场都该在场的记忆用。多条 baseline 之间的先后由 `baselinePriority` 决定(数字越大越前,缺省 0),**不按文件名排**
157
157
  - 记忆目录读不到、权限错、frontmatter 格式坏,这些都**降级为不注入并写日志**,不会静默失效
158
158
 
159
- 单文件上限 512 KB(写入时校验);标签最多 20 个、每个 40 字符(再多就不是标签,是一句话被塞进 `tags` 了)。
159
+ 单文件上限 512 KB,**读写两侧都校验**(读的时候先看体积,超了就不读进来);标签最多 20 个、每个 40 字符(再多就不是标签,是一句话被塞进 `tags` 了)。
160
160
 
161
161
  ## 自动做梦(autodream)
162
162
 
@@ -261,7 +261,7 @@ sage-mem 只做三件事,全都在你本机:
261
261
 
262
262
  | 它做什么 | 具体范围 |
263
263
  |---|---|
264
- | **读记忆** | 只读 `SAGE_MEM_DIR`(默认 `~/.sage-mem/memory`)下的顶层 `*.md`;单文件上限 512 KB 在**写入**时校验 |
264
+ | **读记忆** | 只读 `SAGE_MEM_DIR`(默认 `~/.sage-mem/memory`)下的顶层 `*.md`,单文件上限 512 KB(读写两侧都查) |
265
265
  | **写记忆** | 经 agent 的文件工具或设置页写同一个目录;`MEMORY.md`、`session-log.md` 等保留名受保护,设置页碰不到 |
266
266
  | **注入** | 把选中的记忆作为上下文交给模型(最多 10 条相关 + 8 条 `baseline`,单场会话累计上限 60 KB) |
267
267
 
@@ -238,6 +238,7 @@ export class AutodreamEngine {
238
238
  sessionsRoot: this.sessionsRoot,
239
239
  apply: !!cfg.apply,
240
240
  withSessions,
241
+ ctx: this.ctx,
241
242
  })
242
243
  const toolSchemas = autodreamToolSchemas({ apply: !!cfg.apply, withSessions })
243
244
 
@@ -23,6 +23,8 @@
23
23
 
24
24
  import { readdir, readFile } from 'node:fs/promises'
25
25
  import { join } from 'node:path'
26
+ import { inspectMemoryEntry } from './memory/scan.js'
27
+ import { warnLog } from './memory/util.js'
26
28
 
27
29
  /** 索引文件名。它本身不是一条记忆,是索引。 */
28
30
  const INDEX_FILE = 'MEMORY.md'
@@ -98,7 +100,23 @@ async function readDirSnapshot(dir) {
98
100
  const all = entries
99
101
  .filter((e) => e.isFile() && e.name.toLowerCase().endsWith('.md'))
100
102
  .map((e) => e.name)
101
- const files = all.filter((f) => !AUDIT_SKIP_FILES.has(f.toLowerCase()))
103
+ const files = []
104
+ for (const f of all) {
105
+ if (AUDIT_SKIP_FILES.has(f.toLowerCase())) continue
106
+ /**
107
+ * 与「列表 / 注入 / 模型工具」同一条闸门。
108
+ *
109
+ * 审计原先自己扫目录、不过闸门,于是同一个目录会给出互相矛盾的两个数:
110
+ * 列表说 1 条(链接 / 超大的被跳过),体检说 3 条 —— 用户根本不知道该信哪个。
111
+ * 被跳过的条目在这里同样不计入 fileCount,原因落到日志。
112
+ */
113
+ const gate = await inspectMemoryEntry(join(dir, f))
114
+ if (!gate.ok) {
115
+ warnLog(null, `audit: skip ${f}: ${gate.error}`)
116
+ continue
117
+ }
118
+ files.push(f)
119
+ }
102
120
  let indexText = ''
103
121
  try {
104
122
  indexText = await readFile(join(dir, INDEX_FILE), 'utf8')
@@ -29,7 +29,8 @@ import { join, basename } from 'node:path'
29
29
  import { createHash } from 'node:crypto'
30
30
  import { auditMemoryDir, formatAudit, parseFrontmatterLite } from './autodream-audit.js'
31
31
  import { archiveMemory } from './memory/archive.js'
32
- import { writeFileAtomic } from './memory/util.js'
32
+ import { writeFileAtomic, warnLog } from './memory/util.js'
33
+ import { inspectMemoryEntry, reservedIdentitySet } from './memory/scan.js'
33
34
 
34
35
  /** 单次 read_memory 返回的正文上限(字符)。超过就截断并明确告诉模型。 */
35
36
  const READ_LIMIT = 20000
@@ -103,7 +104,7 @@ function ageDays(ms) {
103
104
  * @param {string} memoryDir
104
105
  * @returns {Promise<Array<object>>}
105
106
  */
106
- async function scanEntries(memoryDir) {
107
+ async function scanEntries(memoryDir, ctx) {
107
108
  let entries
108
109
  try {
109
110
  entries = await readdir(memoryDir, { withFileTypes: true })
@@ -111,14 +112,25 @@ async function scanEntries(memoryDir) {
111
112
  if (err?.code === 'ENOENT') return []
112
113
  throw err
113
114
  }
115
+ const reservedIds = await reservedIdentitySet(memoryDir)
114
116
  const names = entries
115
117
  .filter((e) => e.isFile() && e.name.toLowerCase().endsWith('.md') && !NON_ENTRY_FILES.has(e.name.toLowerCase()))
116
118
  .map((e) => e.name)
117
119
  const out = []
118
120
  for (const name of names) {
119
121
  const full = join(memoryDir, name)
122
+ // 与注入 / 面板同一条闸门:模型看到的清单不该包含「住在别处」的文件。
123
+ const gate = await inspectMemoryEntry(full)
124
+ if (!gate.ok) {
125
+ warnLog(ctx, `autodream: skip ${name}: ${gate.error}`)
126
+ continue
127
+ }
128
+ if (gate.id && reservedIds.has(gate.id)) {
129
+ warnLog(ctx, `autodream: skip ${name}: 这是某个保留名的另一个名字`)
130
+ continue
131
+ }
120
132
  try {
121
- const [content, info] = await Promise.all([readFile(full, 'utf8'), stat(full)])
133
+ const content = await readFile(full, 'utf8')
122
134
  const meta = parseFrontmatterLite(content)
123
135
  const body = content.replace(/^---\r?\n[\s\S]*?\r?\n---/, '')
124
136
  const h1 = body.match(/^#\s+(.+)$/m)
@@ -126,8 +138,8 @@ async function scanEntries(memoryDir) {
126
138
  file: name,
127
139
  // 索引不是一条记忆:标出来,否则模型会看到「MEMORY.md · reference」而困惑。
128
140
  type: name.toLowerCase() === INDEX_FILE ? '索引' : meta.type || 'reference',
129
- bytes: info.size,
130
- mtimeMs: info.mtimeMs,
141
+ bytes: gate.size,
142
+ mtimeMs: gate.mtimeMs,
131
143
  title: (h1 ? h1[1] : meta.name || '').trim(),
132
144
  description: meta.description || '',
133
145
  })
@@ -327,7 +339,7 @@ const MIN_REASON_CHARS = 4
327
339
  * @returns {{run:(name:string, args:object)=>Promise<string>, touchedFiles:()=>string[], changes:()=>object[], warnings:()=>string[]}}
328
340
  */
329
341
  export function createToolRunner(opts) {
330
- const { memoryDir, sessionsRoot, apply, withSessions } = opts
342
+ const { memoryDir, sessionsRoot, apply, withSessions, ctx } = opts
331
343
  let listCache = null
332
344
  let auditCache = null
333
345
  const touched = []
@@ -396,7 +408,7 @@ export function createToolRunner(opts) {
396
408
  try {
397
409
  switch (name) {
398
410
  case 'list_memory': {
399
- if (!listCache) listCache = await scanEntries(memoryDir)
411
+ if (!listCache) listCache = await scanEntries(memoryDir, ctx)
400
412
  const lines = listCache.map(
401
413
  (e) =>
402
414
  `- ${e.file} · ${e.type} · ${Math.round(e.bytes / 1024)}KB · ${ageDays(e.mtimeMs)}天前 · ${localStamp(e.mtimeMs)}\n ${(e.title || '(无标题)').slice(0, 60)}${e.description ? ' — ' + e.description.slice(0, LIST_DESC_CHARS) : ''}`,
@@ -408,6 +420,17 @@ export function createToolRunner(opts) {
408
420
  if (!validated) return `错误:文件名不合法(只能是 memory 目录顶层的 .md 文件名):${String(a.file)}`
409
421
  // 统一落到磁盘上的真实名字:NTFS 大小写不敏感,`A.md` 与 `a.md` 是同一个文件。
410
422
  const full = join(memoryDir, await resolveRealName(memoryDir, basename(validated)))
423
+ /**
424
+ * 过闸门再读:**名字在记忆目录里不等于内容住在记忆目录里**。
425
+ * 注入与面板两条腿都挡住了,这条腿(模型工具)也必须挡 ——
426
+ * 否则外部文件的内容照样会被读进来、送进模型上下文。
427
+ */
428
+ const gate = await inspectMemoryEntry(full)
429
+ if (!gate.ok) {
430
+ return gate.code === 'missing'
431
+ ? `错误:没有这个文件:${basename(full)}`
432
+ : `错误:拒绝读取 ${basename(full)}:${gate.error}`
433
+ }
411
434
  const content = await readFile(full, 'utf8').catch((err) => {
412
435
  if (err?.code === 'ENOENT') return null
413
436
  throw err
package/lib/index.js CHANGED
@@ -22,12 +22,12 @@ import { MAX_BASELINE, MAX_CHARS_PER_FILE, MAX_FILE_BYTES, MAX_RESULTS, MAX_SESS
22
22
  import { parseAliases, parseBaseline, parseBaselinePriority, parseFrontmatter, parseTags } from './memory/frontmatter.js'
23
23
  import { isReserved, nameKey, safeName } from './memory/naming.js'
24
24
  import { ageDays, bigrams, extractText, score, selectBaseline, selectRelevant } from './memory/retrieval.js'
25
- import { MEMORY_ARCHIVE_DIR, addArchiveMeta, extractTitle, readArchivedMeta, scanArchived, scanMemoryFiles, scanStars } from './memory/scan.js'
25
+ import { MEMORY_ARCHIVE_DIR, addArchiveMeta, extractTitle, inspectDir, inspectMemoryEntry, readArchivedMeta, scanArchived, scanMemoryFiles, scanStars, shadowsReserved } from './memory/scan.js'
26
26
  import { archiveMemory, restoreMemory } from './memory/archive.js'
27
27
  import { flushAccess, pickArchiveCandidates, readAccessLedger, recordAccess } from './memory/access.js'
28
28
  import { warnLog, writeFileAtomic } from './memory/util.js'
29
29
  import { Remote, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol'
30
- import { mkdir, readFile, stat, unlink, writeFile } from 'node:fs/promises'
30
+ import { mkdir, readFile, unlink, writeFile } from 'node:fs/promises'
31
31
  import { basename, join } from 'node:path'
32
32
 
33
33
  /**
@@ -121,6 +121,9 @@ async function readReservedFile() {
121
121
  function rawName(raw) {
122
122
  const s = typeof raw === 'string' ? raw : ''
123
123
  if (!s || basename(s) !== s) return null
124
+ // `:` 是 NTFS 交替数据流的分隔符:`MEMORY.md:evil.md` 能过扩展名判据,落盘却是写到
125
+ // 索引的隐藏流上(`readdir` 看不见)。与 `safeName` 同一条理由,这里也要拒。
126
+ if (s.includes(':')) return null
124
127
  return /\.md$/i.test(s) ? s : null
125
128
  }
126
129
 
@@ -389,11 +392,24 @@ export class MemoryGateway extends TypertRemoteService {
389
392
  })
390
393
  }
391
394
 
392
- /** 读单个记忆文件全文。 */
395
+ /**
396
+ * 读单个记忆文件全文。
397
+ *
398
+ * 过闸门再读:名字在记忆目录里**不等于**内容住在记忆目录里 —— 符号链接 / 目录联接 /
399
+ * 硬链接都能让一次「读自己的记忆」变成读别人的文件,而读到的内容会被注入模型上下文。
400
+ */
393
401
  async readFile(name) {
394
402
  const safe = safeName(name)
395
403
  if (!safe) throw new Error('sage-mem: invalid file name')
396
- const content = await readFile(join(MEMORY_DIR, safe), 'utf8')
404
+ const full = join(MEMORY_DIR, safe)
405
+ // 名字不是保留名 ≠ 文件不是保留名:8.3 短名(`MEMORY~1.MD`)文本上与索引毫无关系,
406
+ // 却是同一个文件。别把索引当普通记忆交出去。
407
+ if (await shadowsReserved(full)) {
408
+ throw new Error(`sage-mem: refusing to read ${safe}: 这是某个保留名(索引 / 流水)的另一个名字`)
409
+ }
410
+ const gate = await inspectMemoryEntry(full)
411
+ if (!gate.ok) throw new Error(`sage-mem: refusing to read ${safe}: ${gate.error}`)
412
+ const content = await readFile(full, 'utf8')
397
413
  return { name: safe, content }
398
414
  }
399
415
 
@@ -412,6 +428,12 @@ export class MemoryGateway extends TypertRemoteService {
412
428
  }
413
429
  const safe = safeName(name)
414
430
  if (!safe) throw new Error('sage-mem: invalid file name')
431
+ // 名字文本不是保留名 **不等于** 文件不是保留名:NTFS 的 8.3 短名(`MEMORY~1.MD`)
432
+ // 在文本上跟索引毫无关系,inode 却是同一个 —— 实测它能改写索引。
433
+ // 保留名保护必须落在**文件身份**上,否则「保留名不可写」这句承诺是假的。
434
+ if (await shadowsReserved(join(MEMORY_DIR, safe))) {
435
+ return { ok: false, file: safe, error: `sage-mem: reserved file (它的另一个名字是 ${safe}),请用文件工具改` }
436
+ }
415
437
  // 不加这一层,传个对象进来会被 String() 写成 "[object Object]" 还回 { ok: true },
416
438
  // 原文件就此被一行垃圾顶掉,且没有任何错误信号。
417
439
  if (typeof content !== 'string') {
@@ -421,7 +443,19 @@ export class MemoryGateway extends TypertRemoteService {
421
443
  if (bytes > MAX_FILE_BYTES) {
422
444
  return { ok: false, file: safe, error: `sage-mem: content too large (${bytes} bytes > ${MAX_FILE_BYTES})` }
423
445
  }
424
- await writeFileAtomic(join(MEMORY_DIR, safe), content)
446
+ /**
447
+ * 目标已存在时也要过闸门:链接 / 硬链接一律拒绝。
448
+ *
449
+ * 原子写本身不会写穿链接(rename 换掉的是链接本身),但那样「改这条记忆」就变成了
450
+ * 「把这个名字指向别处」—— 用户以为改的是记忆,实际磁盘上多了一个指向外部的名字。
451
+ * `limitBytes = 0` 表示不在这里判体积(上面刚按字符数查过)。
452
+ */
453
+ const target = join(MEMORY_DIR, safe)
454
+ const existing = await inspectMemoryEntry(target, { limitBytes: null })
455
+ if (!existing.ok && existing.code !== 'missing') {
456
+ return { ok: false, file: safe, error: `sage-mem: refusing to write ${safe}: ${existing.error}` }
457
+ }
458
+ await writeFileAtomic(target, content)
425
459
  return { ok: true, file: safe }
426
460
  }
427
461
 
@@ -432,6 +466,10 @@ export class MemoryGateway extends TypertRemoteService {
432
466
  }
433
467
  const safe = safeName(name)
434
468
  if (!safe) throw new Error('sage-mem: invalid file name')
469
+ // 同 writeFile:8.3 短名能绕过名字文本判据,删除保护也必须看文件身份。
470
+ if (await shadowsReserved(join(MEMORY_DIR, safe))) {
471
+ return { ok: false, error: `sage-mem: reserved file (它的另一个名字是 ${safe}),拒绝删除` }
472
+ }
435
473
  await unlink(join(MEMORY_DIR, safe))
436
474
  return { ok: true }
437
475
  }
@@ -497,11 +535,19 @@ export class MemoryGateway extends TypertRemoteService {
497
535
  async readArchived(name) {
498
536
  const safe = safeName(name)
499
537
  if (!safe) throw new Error('sage-mem: invalid file name')
538
+ // 归档区本身先过目录闸门:它若是指向别处的联接,读归档就等于读外部的文件。
539
+ const dirGate = await inspectDir(MEMORY_ARCHIVE_DIR)
540
+ if (!dirGate.ok) throw new Error(`sage-mem: archive dir unavailable: ${dirGate.error}`)
500
541
  const full = join(MEMORY_ARCHIVE_DIR, safe)
501
- const info = await stat(full).catch(() => null)
542
+ const gate = await inspectMemoryEntry(full)
502
543
  // 找不到就抛:与 readFile 一致。静默返回空串会让面板显示一个「空的记忆」,
503
544
  // 而用户改完一存 —— 就把一条真实内容顶掉了。
504
- if (!info?.isFile()) throw new Error(`sage-mem: archived file not found: ${safe}`)
545
+ if (!gate.ok) {
546
+ // ⚠️「形状不对」与「不存在」是两回事:把联接 / 超限都报成「文件不存在」,
547
+ // 会让用户去找一个明明躺在盘上的文件。
548
+ if (gate.code === 'missing') throw new Error(`sage-mem: archived file not found: ${safe}`)
549
+ throw new Error(`sage-mem: refusing to read archived ${safe}: ${gate.error}`)
550
+ }
505
551
  const content = await readFile(full, 'utf8')
506
552
  return { name: safe, content }
507
553
  }
@@ -520,10 +566,21 @@ export class MemoryGateway extends TypertRemoteService {
520
566
  }
521
567
  const safe = safeName(name)
522
568
  if (!safe) throw new Error('sage-mem: invalid file name')
569
+ const dirGate = await inspectDir(MEMORY_ARCHIVE_DIR)
570
+ if (!dirGate.ok) return { ok: false, file: safe, error: `sage-mem: archive dir unavailable: ${dirGate.error}` }
523
571
  const full = join(MEMORY_ARCHIVE_DIR, safe)
524
- const info = await stat(full).catch(() => null)
572
+ const gate = await inspectMemoryEntry(full, { limitBytes: null })
525
573
  // 只改归档区里**已经存在**的文件:这条 remote 不是「把任意文件写进归档区」的后门。
526
- if (!info?.isFile()) return { ok: false, file: safe, error: `sage-mem: archived file not found: ${safe}` }
574
+ if (!gate.ok) {
575
+ return {
576
+ ok: false,
577
+ file: safe,
578
+ error:
579
+ gate.code === 'missing'
580
+ ? `sage-mem: archived file not found: ${safe}`
581
+ : `sage-mem: refusing to write archived ${safe}: ${gate.error}`,
582
+ }
583
+ }
527
584
  if (typeof content !== 'string') {
528
585
  return { ok: false, file: safe, error: `sage-mem: content must be a string, got ${typeof content}` }
529
586
  }
@@ -669,9 +726,15 @@ export class MemoryGateway extends TypertRemoteService {
669
726
  }
670
727
  const safe = rawName(name)
671
728
  if (!safe) return { ok: false, error: `sage-mem: invalid file name: ${String(name)}` }
729
+ // 保留名同样过闸门:链接 / 硬链接一样能把「读索引 / 读流水」变成读外部文件。
730
+ // ⚠️ 体积**不**在这里判(`limitBytes = 0`):流水天然比单条记忆大得多,
731
+ // 它有自己的上限(`MAX_RAW_BYTES`,见上面的注释)。
732
+ const full = join(MEMORY_DIR, safe)
733
+ const gate = await inspectMemoryEntry(full, { limitBytes: null })
734
+ if (!gate.ok) throw new Error(`sage-mem: refusing to read ${safe}: ${gate.error}`)
672
735
  // 读不到就抛(与 readFile 一致):静默返回空串会让面板显示一份「空的索引」,
673
736
  // 用户一存就把真索引顶掉。
674
- const content = await readFile(join(MEMORY_DIR, safe), 'utf8')
737
+ const content = await readFile(full, 'utf8')
675
738
  return { name: safe, content }
676
739
  }
677
740
 
@@ -699,7 +762,14 @@ export class MemoryGateway extends TypertRemoteService {
699
762
  error: `sage-mem: content too large (${bytes} bytes > ${MAX_RAW_BYTES / (1024 * 1024)} MB raw limit)`,
700
763
  }
701
764
  }
702
- await writeFileAtomic(join(MEMORY_DIR, safe), content)
765
+ const target = join(MEMORY_DIR, safe)
766
+ // 与 `writeFile` 同一条理由:目标已存在时若是链接 / 硬链接,一律拒绝 ——
767
+ // 「改这份索引」不该变成「把这个名字指向别处」。体积由上面那道 `MAX_RAW_BYTES` 管。
768
+ const existing = await inspectMemoryEntry(target, { limitBytes: null })
769
+ if (!existing.ok && existing.code !== 'missing') {
770
+ return { ok: false, file: safe, error: `sage-mem: refusing to write ${safe}: ${existing.error}` }
771
+ }
772
+ await writeFileAtomic(target, content)
703
773
  return { ok: true, file: safe }
704
774
  }
705
775
  }
@@ -15,7 +15,7 @@
15
15
  * 就是临时目录),拿模块级常量会写错地方 —— 那是会碰到真实记忆的危险错误。
16
16
  */
17
17
 
18
- import { ARCHIVE_DIR_NAME, addArchiveMeta, localStamp, stripArchiveMeta } from './scan.js'
18
+ import { ARCHIVE_DIR_NAME, addArchiveMeta, inspectDir, inspectMemoryEntry, localStamp, stripArchiveMeta } from './scan.js'
19
19
  import { isReserved, nameKey, safeName } from './naming.js'
20
20
  import { writeFileAtomic } from './util.js'
21
21
  import { mkdir, readFile, stat, unlink } from 'node:fs/promises'
@@ -49,9 +49,26 @@ export async function archiveMemory(memoryDir, name, reason, at = localStamp())
49
49
  const safe = gate.safe
50
50
  const src = join(memoryDir, safe)
51
51
  const dst = join(archiveDirOf(memoryDir), safe)
52
- const info = await stat(src).catch(() => null)
52
+ /**
53
+ * 两道闸门,缺一不可:
54
+ * - 源文件:链接 / 硬链接指向的可能是记忆目录**之外**的文件,归档它 = 把它从外部搬走
55
+ * - 归档区:它若被换成指向外部的目录联接,归档 = 把记忆搬出记忆目录
56
+ * (体积不在这里判:归档是搬移,不是写入。)
57
+ */
58
+ const srcGate = await inspectMemoryEntry(src, { limitBytes: null })
53
59
  // 根目录里没有 → 文件名写错,或者它已经是归档态(归档区那份不该再归档一次)。
54
- if (!info?.isFile()) return { ok: false, code: 'not-found', error: `sage-mem: file not found: ${safe}` }
60
+ if (!srcGate.ok) {
61
+ return {
62
+ ok: false,
63
+ code: srcGate.code === 'missing' ? 'not-found' : srcGate.code,
64
+ error: `sage-mem: file not found: ${safe}(${srcGate.error})`,
65
+ }
66
+ }
67
+ const dirGate = await inspectDir(archiveDirOf(memoryDir))
68
+ // 归档区还不存在是正常状态(下面 mkdir 会建)。
69
+ if (!dirGate.ok && dirGate.code !== 'missing') {
70
+ return { ok: false, code: dirGate.code, error: `sage-mem: archive dir unavailable: ${dirGate.error}` }
71
+ }
55
72
  if (await stat(dst).catch(() => null)) {
56
73
  // 归档区已有同名:不覆盖 —— 那份旧归档可能是用户想留的另一版。
57
74
  return { ok: false, code: 'exists', error: `sage-mem: already archived: ${safe}` }
@@ -79,11 +96,25 @@ export async function restoreMemory(memoryDir, name) {
79
96
  const safe = gate.safe
80
97
  const dst = join(memoryDir, safe)
81
98
  const src = join(archiveDirOf(memoryDir), safe)
82
- const info = await stat(src).catch(() => null)
83
- if (!info?.isFile()) return { ok: false, code: 'not-archived', error: `sage-mem: not archived: ${safe}` }
84
- if (await stat(dst).catch(() => null)) {
99
+ // 归档区过目录闸门:联接指向外部时,「恢复」会把外部的文件搬进记忆目录。
100
+ const dirGate = await inspectDir(archiveDirOf(memoryDir))
101
+ if (!dirGate.ok) {
102
+ return {
103
+ ok: false,
104
+ code: dirGate.code === 'missing' ? 'not-archived' : dirGate.code,
105
+ error: `sage-mem: not archived: ${safe}(${dirGate.error})`,
106
+ }
107
+ }
108
+ const srcGate = await inspectMemoryEntry(src, { limitBytes: null })
109
+ if (!srcGate.ok) return { ok: false, code: 'not-archived', error: `sage-mem: not archived: ${safe}(${srcGate.error})` }
110
+ // 顶层已有同名(且不是链接)时拒绝:根目录那份是正在用的记忆,归档这份只是退役版。
111
+ const dstGate = await inspectMemoryEntry(dst, { limitBytes: null })
112
+ if (dstGate.ok) {
85
113
  return { ok: false, code: 'target-exists', error: `sage-mem: already exists, refusing to overwrite: ${safe}` }
86
114
  }
115
+ if (dstGate.code !== 'missing') {
116
+ return { ok: false, code: dstGate.code, error: `sage-mem: refusing to restore over ${safe}: ${dstGate.error}` }
117
+ }
87
118
  const content = await readFile(src, 'utf8')
88
119
  await writeFileAtomic(dst, stripArchiveMeta(content))
89
120
  await unlink(src)
@@ -48,6 +48,11 @@ export function safeName(raw) {
48
48
  // basename 与原文不一致=含目录成分。旧版是「悄悄取 basename 继续」,
49
49
  // 与 starmap 那套(要求 safe === raw)行为不一致;现在统一为拒绝。
50
50
  if (basename(raw) !== raw) return null
51
+ // `:` 在 NTFS 上是**交替数据流**的分隔符:`MEMORY.md:evil.md` 以 `.md` 结尾、能过下面
52
+ // 所有判据,落盘时却会写到索引的隐藏数据流上(`readdir` 看不见,只写流不报错)。
53
+ // 实测更糟:`writeFile('MEMORY.md:evil.md')` 会在 `rename` 处抛**未捕获**的 EINVAL ——
54
+ // 违背「拒绝一律返回 {ok:false}、不抛」这条契约。正常文件名不允许 `:`,直接拒。
55
+ if (raw.includes(':')) return null
51
56
  if (!/\.md$/i.test(raw)) return null
52
57
  if (isNonEntry(raw)) return null
53
58
  return raw
@@ -10,11 +10,11 @@
10
10
  * 文件可能被手工搬来搬去,随文件走的元数据才不会与文件脱节。
11
11
  */
12
12
 
13
- import { MEMORY_DIR } from './config.js'
13
+ import { MAX_FILE_BYTES, MEMORY_DIR, RESERVED_FILES } from './config.js'
14
14
  import { parseBaseline, parseFrontmatter } from './frontmatter.js'
15
15
  import { isMemoryEntry } from './naming.js'
16
16
  import { warnLog } from './util.js'
17
- import { readFile, readdir, stat } from 'node:fs/promises'
17
+ import { lstat, readFile, readdir } from 'node:fs/promises'
18
18
  import { join } from 'node:path'
19
19
 
20
20
  /** 归档区目录名。与 autodream 工具里的 ARCHIVE_DIR 同名同义(同一个目录)。 */
@@ -112,18 +112,160 @@ export function addArchiveMeta(content, reason, at) {
112
112
  return stripped.slice(0, m.index) + block.join(eol) + stripped.slice(m.index + m[0].length)
113
113
  }
114
114
 
115
+ /**
116
+ * 记忆文件形状闸门:只认「普通文件、体积在给定上限内」的 `.md`。
117
+ *
118
+ * 为什么要闸门:记忆目录里的一条 `x.md` **不一定真的住在记忆目录里** ——
119
+ * **符号链接 / 目录联接**把名字留在记忆目录、把内容指到任意路径:读它就是读别人的文件,
120
+ * 写它就是改别人的文件,而这些内容会被注入模型上下文。
121
+ * 用 `lstat` 而不是 `stat`:`stat` 会跟随链接,永远看不出这一层。
122
+ *
123
+ * ⚠️ 本机实测:Windows 上 `symlink()`(文件或目录)要特权、默认 EPERM,但
124
+ * `symlink(dir, 'junction')` **能建** —— 所以这不是理论风险。
125
+ *
126
+ * **硬链接(`nlink > 1`)不拦,只提示**(返回值里的 `links`,由调用方负责出声)。
127
+ * 取舍理由:硬链接**不提供**「指向任意路径」的能力(不能跨卷,只能由已在同卷上有权限的人建),
128
+ * 它带来的是「内容可能被记忆目录之外改到」—— 这是完整性提示,不是越界读取。
129
+ * 而拦它的代价是**真实误伤**:`cp -al` / `rsync --link-dest` 这类快照备份会让源文件的
130
+ * `nlink` 变成 2,于是整条记忆从注入与面板里静默消失(实测复现过)。
131
+ * 宁可提示、不可丢记忆 —— 与「大小写判定宁可多留」同一条原则。
132
+ *
133
+ * 体积上限也在这里:它原先只写在**写入**路径上,读路径谁来都全量读进内存,
134
+ * 等于上限只在「自己人写」的前提下成立(一条被手工粘进来的日志就能把整轮注入吃光)。
135
+ *
136
+ * @param {string} full — 绝对路径
137
+ * @param {{limitBytes?: number|null}} [opts] — `limitBytes: null` = **不判体积**(归档区用);
138
+ * 不传则用 `MAX_FILE_BYTES`。⚠️ 显式 `null` 与「随便传个 0 / NaN」是两回事:
139
+ * 非有限数一律按「超过上限」处理,不会静默把闸门关掉。
140
+ * @returns {Promise<{ok:true,size:number,mtimeMs:number,links:number}|{ok:false,code:string,error:string}>}
141
+ */
142
+ export async function inspectMemoryEntry(full, opts = {}) {
143
+ const limitBytes = opts?.limitBytes === undefined ? MAX_FILE_BYTES : opts.limitBytes
144
+ let info
145
+ try {
146
+ info = await lstat(full)
147
+ } catch (err) {
148
+ return {
149
+ ok: false,
150
+ code: err?.code === 'ENOENT' ? 'missing' : 'stat-failed',
151
+ error: `读不到文件属性(${err?.code ?? err})`,
152
+ }
153
+ }
154
+ if (info.isSymbolicLink()) return { ok: false, code: 'symlink', error: '是指向别处的符号链接 / 目录联接' }
155
+ if (!info.isFile()) return { ok: false, code: 'not-file', error: '不是普通文件' }
156
+ // null = 明确「不判体积」;其余一律按数值上限处理(0 / NaN / 字符串都会被当成超限)。
157
+ if (limitBytes !== null && (!Number.isFinite(limitBytes) || info.size > limitBytes)) {
158
+ return {
159
+ ok: false,
160
+ code: 'too-big',
161
+ error: Number.isFinite(limitBytes)
162
+ ? `体积 ${info.size} 字节,超过单文件上限 ${limitBytes} 字节`
163
+ : `体积 ${info.size} 字节,上限参数不合法(${String(limitBytes)})`,
164
+ }
165
+ }
166
+ return { ok: true, size: info.size, mtimeMs: info.mtimeMs, links: info.nlink, id: identityOf(info) }
167
+ }
168
+
169
+ /** 文件身份键(`dev:ino`)。拿不到 inode 的平台返回 null(那就退回名字文本判据)。 */
170
+ export function identityOf(info) {
171
+ return info && info.ino ? `${info.dev}:${info.ino}` : null
172
+ }
173
+
174
+ /**
175
+ * 保留名在磁盘上的**文件身份**集合(`dev:ino`)。
176
+ *
177
+ * 为什么需要它:保留名保护原先只比**名字文本**(`nameKey` 归一 + 小写)。但 NTFS 上一个
178
+ * 文件可以有第二个 8.3 短名 —— `MEMORY~1.MD` 与 `MEMORY.md` 文本上毫无关系,判据却必须
179
+ * 认出它们是**同一个文件**:实测在能建短名的卷上,`writeFile('MEMORY~1.MD')` 能改写索引、
180
+ * `deleteFile('MEMORY~2.MD')` 能把它删掉,而 `isReserved()` 一路放行。
181
+ *
182
+ * 返回集合在每次调用时现取(保留名就那两三个,一次目录写入的开销可忽略)。
183
+ */
184
+ export async function reservedIdentitySet(dir = MEMORY_DIR) {
185
+ const ids = new Set()
186
+ for (const name of RESERVED_FILES) {
187
+ const info = await lstat(join(dir, name)).catch(() => null)
188
+ const id = identityOf(info)
189
+ if (id) ids.add(id)
190
+ }
191
+ return ids
192
+ }
193
+
194
+ /** 这个名字在磁盘上是不是某个保留名的另一个名字(8.3 短名 / 硬链接)。 */
195
+ export async function shadowsReserved(full, dir = MEMORY_DIR) {
196
+ const ids = await reservedIdentitySet(dir)
197
+ if (!ids.size) return false
198
+ const info = await lstat(full).catch(() => null)
199
+ const id = identityOf(info)
200
+ return id !== null && ids.has(id)
201
+ }
202
+
203
+ /**
204
+ * 子目录闸门:`archive/` 这类**插件自己管理的目录**必须是真目录,不能是指向别处的联接。
205
+ *
206
+ * ⚠️ 只对 `archive/` 这类子目录用,**不**对记忆目录本身用:把 `SAGE_MEM_DIR` 指向一个
207
+ * 链接目录(例如网盘同步目录)是正当用法,拦掉它属于误伤。而 `archive/` 是归档动作的
208
+ * 落地处 —— 它若是联接,归档就等于把记忆搬出记忆目录,恢复则可能把外部文件搬进来。
209
+ *
210
+ * @returns {Promise<{ok:true}|{ok:false,code:string,error:string}>}
211
+ */
212
+ export async function inspectDir(dir) {
213
+ let info
214
+ try {
215
+ info = await lstat(dir)
216
+ } catch (err) {
217
+ return {
218
+ ok: false,
219
+ code: err?.code === 'ENOENT' ? 'missing' : 'stat-failed',
220
+ error: `读不到目录属性(${err?.code ?? err})`,
221
+ }
222
+ }
223
+ if (info.isSymbolicLink()) return { ok: false, code: 'symlink', error: '是指向别处的符号链接 / 目录联接' }
224
+ if (!info.isDirectory()) return { ok: false, code: 'not-dir', error: '不是目录' }
225
+ return { ok: true }
226
+ }
227
+
115
228
  /** 扫 memory 目录,读回全部记忆文件(跳过索引与子目录),并解析 frontmatter 摘要。 */
116
229
  export async function scanMemoryFiles(ctx, sessionId) {
117
230
  try {
118
231
  const entries = await readdir(MEMORY_DIR, { withFileTypes: true })
232
+ /** 保留名的文件身份:8.3 短名与硬链接都得靠它才认得出(名字文本上它们毫无关系)。 */
233
+ const reservedIds = await reservedIdentitySet()
234
+ /**
235
+ * 链接条目在 `isFile()` 这一步就被滤掉了(`readdir` 的 Dirent 报的是链接本身),
236
+ * 也就是说**闸门根本没机会看到它们** —— 不在这里出声,用户只会看到「这条记忆不见了」。
237
+ */
238
+ for (const e of entries) {
239
+ if (e.isSymbolicLink() && /\.md$/i.test(e.name)) {
240
+ warnLog(ctx, `skip ${e.name}: 是指向别处的符号链接 / 目录联接`, sessionId)
241
+ }
242
+ }
119
243
  const names = entries
120
244
  .filter(e => e.isFile() && isMemoryEntry(e.name))
121
245
  .map(e => e.name)
122
246
  const loaded = await Promise.all(names.map(async (name) => {
247
+ const full = join(MEMORY_DIR, name)
248
+ // 先过闸门(lstat + 体积),再读:链接与超大文件都在**全量读之前**被挡下,
249
+ // 而不是读进来之后才发现不该读。mtime 由闸门一并给出,比原来少一跳 stat。
250
+ const gate = await inspectMemoryEntry(full)
251
+ /**
252
+ * 8.3 短名 / 硬链接都可能是「保留名的另一个名字」:`MEMORY~1.MD` 与 `MEMORY.md`
253
+ * 文本上毫无关系,inode 却是同一个。不按身份拦,索引就会以普通记忆的身份被读进上下文。
254
+ */
255
+ if (gate.ok && gate.id && reservedIds.has(gate.id)) {
256
+ warnLog(ctx, `skip ${name}: 这是某个保留名(索引 / 流水)的另一个名字,不当记忆读`, sessionId)
257
+ return null
258
+ }
259
+ // 硬链接**不拦**(见 inspectMemoryEntry 的取舍),但要让用户知道内容可能被外部改到。
260
+ if (gate.ok && gate.links > 1) {
261
+ warnLog(ctx, `note ${name}: 有 ${gate.links} 个硬链接名,内容可能被记忆目录之外改到`, sessionId)
262
+ }
263
+ if (!gate.ok) {
264
+ warnLog(ctx, `skip ${name}: ${gate.error}`, sessionId)
265
+ return null
266
+ }
123
267
  try {
124
- const full = join(MEMORY_DIR, name)
125
- // stat 与 readFile 并行 —— 多要一个 mtime 不该变成串行两跳。
126
- const [content, info] = await Promise.all([readFile(full, 'utf8'), stat(full)])
268
+ const content = await readFile(full, 'utf8')
127
269
  const meta = parseFrontmatter(content)
128
270
  return {
129
271
  file: name,
@@ -134,7 +276,7 @@ export async function scanMemoryFiles(ctx, sessionId) {
134
276
  baselinePriority: meta.baselinePriority || 0,
135
277
  // 新鲜度的两个来源:frontmatter 的 updated(权威)与文件 mtime(退路)。
136
278
  updated: meta.updated || '',
137
- mtimeMs: info.mtimeMs,
279
+ mtimeMs: gate.mtimeMs,
138
280
  }
139
281
  } catch (err) {
140
282
  // 只有 ENOENT 是正常状态(扫到读之间被删)。其余——权限错、编码坏、盘掉线——
@@ -159,7 +301,7 @@ export function extractTitle(content) {
159
301
  }
160
302
 
161
303
  /** 扫一个目录,读回星图要的轻量元数据(正文 + 体积 + 修改时间)。 */
162
- async function readStarDir(dir, ctx) {
304
+ async function readStarDir(dir, ctx, opts = {}) {
163
305
  let entries
164
306
  try {
165
307
  entries = await readdir(dir, { withFileTypes: true })
@@ -170,11 +312,22 @@ async function readStarDir(dir, ctx) {
170
312
  const names = entries
171
313
  .filter(e => e.isFile() && isMemoryEntry(e.name))
172
314
  .map(e => e.name)
315
+ const reservedIds = await reservedIdentitySet()
173
316
  const loaded = await Promise.all(names.map(async (name) => {
317
+ const full = join(dir, name)
318
+ const gate = await inspectMemoryEntry(full, opts)
319
+ if (!gate.ok) {
320
+ warnLog(ctx, `skip ${full}: ${gate.error}`)
321
+ return null
322
+ }
323
+ // 星图同样不显示「保留名的另一个名字」(8.3 短名 / 硬链接)。
324
+ if (gate.id && reservedIds.has(gate.id)) {
325
+ warnLog(ctx, `skip ${full}: 这是某个保留名(索引 / 流水)的另一个名字`)
326
+ return null
327
+ }
174
328
  try {
175
- const full = join(dir, name)
176
- const [content, info] = await Promise.all([readFile(full, 'utf8'), stat(full)])
177
- return { name, content, bytes: info.size, mtimeMs: info.mtimeMs }
329
+ const content = await readFile(full, 'utf8')
330
+ return { name, content, bytes: gate.size, mtimeMs: gate.mtimeMs }
178
331
  } catch (err) {
179
332
  if (err?.code !== 'ENOENT') warnLog(ctx, `cannot read ${full}: ${err?.message ?? err}`)
180
333
  return null
@@ -188,7 +341,21 @@ async function readStarDir(dir, ctx) {
188
341
  * 归档区不存在是正常状态(没人归档过)→ 空数组,不是错误。
189
342
  */
190
343
  export async function scanArchived(ctx) {
191
- const rows = await readStarDir(MEMORY_ARCHIVE_DIR, ctx)
344
+ // 归档区是**插件自己建的子目录**,必须过目录闸门:它若被换成指向外部的联接,
345
+ // 「归档 = 搬进 archive/」就变成「把记忆搬出记忆目录」。
346
+ const gate = await inspectDir(MEMORY_ARCHIVE_DIR)
347
+ if (!gate.ok) {
348
+ // ENOENT 是正常状态(还没人归档过)→ 空数组;其余一律出声,绝不静默当成「没有归档」。
349
+ if (gate.code !== 'missing') warnLog(ctx, `归档区不可用,跳过:${gate.error}`)
350
+ return []
351
+ }
352
+ /**
353
+ * ⚠️ 归档区**不判体积**(`limitBytes: null`):归档是「把这条记忆收起来」,
354
+ * 不是「新写一条记忆」—— 拿活动记忆的上限去卡归档区,会造出「能归档、列不出、
355
+ * 读不回」的半死状态(实测:600 KB 的记忆归档成功,却从馆里消失、又还能被覆盖)。
356
+ * 形状闸门(symlink)照旧生效。
357
+ */
358
+ const rows = await readStarDir(MEMORY_ARCHIVE_DIR, ctx, { limitBytes: null })
192
359
  return rows.map(f => ({ ...f, archived: true }))
193
360
  }
194
361
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "sage-mem",
3
3
  "description": "File-based memory plugin for DeepSeek Harness (DSH) — cross-session memory in plain markdown files, with a memory star-map view, type/tag filtering, retrieval aliases, and an optional autodream pass that consolidates memories on demand or on a schedule — with per-run rollback points, a per-file change declaration (what changed and why), and a separately selectable model for the consolidation pass",
4
- "version": "0.9.3",
4
+ "version": "0.9.4",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "exports": {