memory-eternal 0.17.0 → 0.18.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.
@@ -0,0 +1,45 @@
1
+ // 核心记忆(前置强制记忆):被人工批准并标记为 core 的卡片,
2
+ // 每次会话都注入 systemPrompt,不依赖模型主动调用 memory_recall。
3
+ //
4
+ // 注意:只读取主库(cards 表)里 kind='core' 的已批准卡片;
5
+ // 待审/驳回卡永远不会进入这里。
6
+
7
+ import { listCards } from './vault.js'
8
+
9
+ export const CORE_KIND = 'core'
10
+ export const CORE_HEADER = '【核心记忆 · 必须遵守】\n以下内容已被用户批准为核心记忆,每次会话都必须优先遵守;与其他记忆或默认习惯冲突时,以此处为准。'
11
+
12
+ export function normalizeCoreOptions({ enabled = true, limit = 6, maxChars = 2000 } = {}) {
13
+ return {
14
+ enabled: enabled !== false,
15
+ limit: Math.min(Math.max(Math.floor(Number(limit) || 6), 1), 20),
16
+ maxChars: Math.min(Math.max(Math.floor(Number(maxChars) || 2000), 200), 10000),
17
+ }
18
+ }
19
+
20
+ /** 生成要注入 systemPrompt 的核心记忆文本;没有核心卡时返回空文本。 */
21
+ export async function coreMemoryPrompt(root, opts = {}) {
22
+ const { enabled, limit, maxChars } = normalizeCoreOptions(opts)
23
+ if (!enabled) return { ok: true, count: 0, text: '', chars: 0 }
24
+ let cards = []
25
+ try {
26
+ cards = await listCards(root, { kind: CORE_KIND, limit })
27
+ } catch {
28
+ return { ok: false, count: 0, text: '', chars: 0 }
29
+ }
30
+ const lines = []
31
+ let chars = CORE_HEADER.length
32
+ for (const card of cards) {
33
+ const title = String(card.title || '').replace(/\s+/g, ' ').trim()
34
+ const summary = String(card.summary || '').replace(/\s+/g, ' ').trim()
35
+ if (!title && !summary) continue
36
+ let line = '- ' + (title || '核心记忆') + (summary ? ':' + summary : '')
37
+ if (line.length > maxChars) line = line.slice(0, maxChars)
38
+ if (chars + 1 + line.length > maxChars && lines.length) break
39
+ lines.push(line)
40
+ chars += 1 + line.length
41
+ }
42
+ if (!lines.length) return { ok: true, count: 0, text: '', chars: 0 }
43
+ const text = CORE_HEADER + '\n' + lines.join('\n')
44
+ return { ok: true, count: lines.length, text: text.slice(0, maxChars), chars: Math.min(chars, maxChars) }
45
+ }
package/lib/db.js CHANGED
@@ -373,6 +373,8 @@ export function checkMainStoreInvariant(root) {
373
373
  * @returns {'pending' | 'approved'}
374
374
  */
375
375
  export function enforceAudit(root, kind, submittedBy, fallback = 'pending') {
376
+ // 核心记忆必须人工审核:无论 auditMode / 免审名单如何配置,写入时一律待审。
377
+ if (String(kind) === 'core') return 'pending'
376
378
  const db = getDb(root)
377
379
  const get = (key) => { try { return db.prepare('SELECT value FROM config WHERE key = ?').get(key)?.value } catch { return null } }
378
380
  const mode = get('auditMode')
package/lib/setup.js CHANGED
@@ -13,6 +13,7 @@ import { promises as fs } from 'node:fs'
13
13
  import path from 'node:path'
14
14
  import os from 'node:os'
15
15
  import { fileURLToPath } from 'node:url'
16
+ import { nodeBinary, usesElectronBinary } from './node-bin.js'
16
17
 
17
18
  const __dirname = path.dirname(fileURLToPath(import.meta.url))
18
19
  export const PACKAGE_ROOT = path.join(__dirname, '..')
@@ -63,8 +64,40 @@ async function backupOnce(file) {
63
64
  return bak
64
65
  }
65
66
 
66
- export function mcpCommand(nodePath = process.execPath) {
67
- return { command: nodePath, args: [BIN_PATH, 'mcp'] }
67
+ /** 这个 node 路径是否其实是 Electron 宿主二进制(直接运行会拉起 GUI)。 */
68
+ export function needsElectronEnv(nodePath = nodeBinary()) {
69
+ const p = String(nodePath || '')
70
+ if (!p) return false
71
+ if (usesElectronBinary()) return true
72
+ if (!process.versions.electron) return false
73
+ try { return path.resolve(p) === path.resolve(process.execPath) } catch { return false }
74
+ }
75
+
76
+ /** 静态 hook 命令:真实 node 直接用;只能退回 Electron 二进制时补 ELECTRON_RUN_AS_NODE。 */
77
+ export function hookCommand(nodePath = nodeBinary(), script, suffix = '') {
78
+ const base = `${JSON.stringify(nodePath)} ${JSON.stringify(script)}${suffix ? ' ' + suffix : ''}`
79
+ if (!needsElectronEnv(nodePath)) return base
80
+ if (process.platform === 'win32') {
81
+ // cmd.exe 不支持 POSIX 的 VAR=value 前缀;用 set 后再执行
82
+ return `cmd /d /s /c "set ELECTRON_RUN_AS_NODE=1&& ${base.replace(/"/g, '\\"')}"`
83
+ }
84
+ return `ELECTRON_RUN_AS_NODE=1 ${base}`
85
+ }
86
+
87
+ /** 历史配置自检:命令指向 Electron 二进制、却没有 ELECTRON_RUN_AS_NODE。 */
88
+ export function isLegacyElectronCommand(command) {
89
+ const s = String(command || '')
90
+ if (!process.versions.electron) return false // 普通 node 进程不能用 execPath 名字判断
91
+ if (!s || /ELECTRON_RUN_AS_NODE/i.test(s)) return false
92
+ const exe = path.basename(process.execPath || '').toLowerCase()
93
+ if (!exe) return false
94
+ return s.toLowerCase().includes(exe)
95
+ }
96
+
97
+ export function mcpCommand(nodePath = nodeBinary()) {
98
+ const out = { command: nodePath, args: [BIN_PATH, 'mcp'] }
99
+ if (needsElectronEnv(nodePath)) out.env = { ELECTRON_RUN_AS_NODE: '1' }
100
+ return out
68
101
  }
69
102
 
70
103
  function sameMcpEntry(existing, want) {
@@ -72,12 +105,28 @@ function sameMcpEntry(existing, want) {
72
105
  if (existing.type === 'stdio' || existing.type === undefined) {
73
106
  return existing.command === want.command
74
107
  && JSON.stringify(existing.args ?? []) === JSON.stringify(want.args)
108
+ && (!want.env || JSON.stringify(existing.env ?? {}) === JSON.stringify(want.env))
75
109
  }
76
110
  return false
77
111
  }
78
112
 
79
113
  // 是否已写入我们(capture.mjs)的会话结束 hook。
80
114
  // marker 用于区分宿主:codex 走 hooks.json 的 Stop;cursor 走 hooks.json 的 stop/sessionEnd。
115
+ /** hooks 文件里是否有「指向 Electron 宿主 exe、却没带 ELECTRON_RUN_AS_NODE」的历史命令。 */
116
+ async function hooksNeedRepair(hooksJson, markers) {
117
+ try {
118
+ const cfg = JSON.parse(await fs.readFile(hooksJson, 'utf8'))
119
+ const walk = (v) => {
120
+ if (!v) return false
121
+ if (typeof v === 'string') return markers.some((m) => v.includes(m)) && isLegacyElectronCommand(v)
122
+ if (Array.isArray(v)) return v.some(walk)
123
+ if (typeof v === 'object') return Object.values(v).some(walk)
124
+ return false
125
+ }
126
+ return walk(cfg)
127
+ } catch { return false }
128
+ }
129
+
81
130
  async function hookState(hooksJson, agent, cursorStyle = false) {
82
131
  try {
83
132
  const cfg = JSON.parse(await fs.readFile(hooksJson, 'utf8'))
@@ -148,7 +197,7 @@ async function setupClaudeHooks({ nodePath, dryRun, log }) {
148
197
  // ① SessionEnd:会话结束自动沉淀
149
198
  const want = {
150
199
  matcher: '',
151
- hooks: [{ type: 'command', command: `${JSON.stringify(nodePath)} ${JSON.stringify(hookScript)} claude` }],
200
+ hooks: [{ type: 'command', command: hookCommand(nodePath, hookScript, 'claude') }],
152
201
  }
153
202
  const hookKey = 'SessionEnd'
154
203
  cfg.hooks[hookKey] = Array.isArray(cfg.hooks[hookKey]) ? cfg.hooks[hookKey] : []
@@ -158,7 +207,7 @@ async function setupClaudeHooks({ nodePath, dryRun, log }) {
158
207
  // 或 npm 版本装到别处),旧实现只判「有没有 capture.mjs」就跳过,于是 hook 永远
159
208
  // 指着那个旧副本(本机实测:Claude Code 一直在跑 web profile 的 0.9.23)。
160
209
  // 与 Codex 分支同样的口径:只重写自己那条命令,其它 hook 一律不动。
161
- const wanted = `${JSON.stringify(nodePath)} ${JSON.stringify(hookScript)} claude`
210
+ const wanted = hookCommand(nodePath, hookScript, 'claude')
162
211
  let rewrote = false
163
212
  for (const c of existingCapture.hooks ?? []) {
164
213
  if (typeof c?.command === 'string' && c.command.includes('capture.mjs') && c.command !== wanted) {
@@ -184,13 +233,25 @@ async function setupClaudeHooks({ nodePath, dryRun, log }) {
184
233
  if (await exists(guardScript)) {
185
234
  const guardKey = 'PreToolUse'
186
235
  cfg.hooks[guardKey] = Array.isArray(cfg.hooks[guardKey]) ? cfg.hooks[guardKey] : []
187
- if (cfg.hooks[guardKey].some((h) => h?.hooks?.some((c) => c?.command?.includes('secret-guard.mjs')))) {
188
- log('✓ Claude Code PreToolUse 凭据守卫已存在,跳过')
236
+ const wantedGuard = hookCommand(nodePath, guardScript)
237
+ const existingGuard = cfg.hooks[guardKey].find((h) => h?.hooks?.some((c) => c?.command?.includes('secret-guard.mjs')))
238
+ if (existingGuard) {
239
+ // 已存在也要修命令:旧版本把 Electron 宿主 exe 写进了 hook(issue #53),
240
+ // 只判「有没有 secret-guard」会永远跳过修复。
241
+ let rewrote = false
242
+ for (const c of existingGuard.hooks ?? []) {
243
+ if (typeof c?.command === 'string' && c.command.includes('secret-guard.mjs') && c.command !== wantedGuard) {
244
+ c.command = wantedGuard
245
+ rewrote = true
246
+ }
247
+ }
248
+ if (rewrote) { written.push(guardKey + '(path)'); changed = true; log('↻ Claude Code PreToolUse hook 路径已更新') }
249
+ else log('✓ Claude Code PreToolUse 凭据守卫已存在,跳过')
189
250
  } else {
190
251
  // 只追加一个独立条目,保留用户已有的 PreToolUse 配置
191
252
  cfg.hooks[guardKey].push({
192
253
  matcher: 'Read|Bash|Grep|Glob',
193
- hooks: [{ type: 'command', command: `${JSON.stringify(nodePath)} ${JSON.stringify(guardScript)}` }],
254
+ hooks: [{ type: 'command', command: wantedGuard }],
194
255
  })
195
256
  written.push(guardKey)
196
257
  changed = true
@@ -245,18 +306,40 @@ export function parseCodexMemoryEntry(text) {
245
306
  * 纯函数:把 `[mcp_servers.memory]` 段对齐到 want(只动这一段的两行)。
246
307
  * @returns {{text:string, changed:boolean}}
247
308
  */
309
+ export function codexEnvLine(env) {
310
+ if (!env || typeof env !== 'object') return ''
311
+ const keys = Object.keys(env)
312
+ if (!keys.length) return ''
313
+ return 'env = { ' + keys.map((k) => `${k} = ${JSON.stringify(String(env[k]))}`).join(', ') + ' }'
314
+ }
315
+
248
316
  export function alignCodexMemorySection(text, want) {
249
317
  const lines = String(text || '').split(/\r?\n/)
250
318
  let inSec = false
251
319
  let changed = false
320
+ let lastRelevant = -1
321
+ let envAt = -1
322
+ const wantCmd = 'command = ' + JSON.stringify(want.command)
323
+ const wantArgs = 'args = ' + JSON.stringify(want.args)
324
+ const wantEnv = codexEnvLine(want.env)
252
325
  for (let i = 0; i < lines.length; i++) {
253
326
  if (/^\s*\[/.test(lines[i])) { inSec = /^\s*\[mcp_servers\.memory\]\s*$/.test(lines[i].trim()); continue }
254
327
  if (!inSec) continue
255
- // 只在**真的不同**时才算改动(幂等:已经是目标形态就不该报 changed,否则每次 setup 都像改过)
256
- const wantCmd = 'command = ' + JSON.stringify(want.command)
257
- const wantArgs = 'args = ' + JSON.stringify(want.args)
258
- if (/^\s*command\s*=/.test(lines[i])) { if (lines[i] !== wantCmd) { lines[i] = wantCmd; changed = true } }
259
- else if (/^\s*args\s*=/.test(lines[i])) { if (lines[i] !== wantArgs) { lines[i] = wantArgs; changed = true } }
328
+ if (/^\s*command\s*=/.test(lines[i])) {
329
+ if (lines[i] !== wantCmd) { lines[i] = wantCmd; changed = true }
330
+ lastRelevant = i
331
+ } else if (/^\s*args\s*=/.test(lines[i])) {
332
+ if (lines[i] !== wantArgs) { lines[i] = wantArgs; changed = true }
333
+ lastRelevant = i
334
+ } else if (/^\s*env\s*=/.test(lines[i])) {
335
+ envAt = i
336
+ if (wantEnv && lines[i] !== wantEnv) { lines[i] = wantEnv; changed = true }
337
+ }
338
+ }
339
+ if (wantEnv && envAt < 0) {
340
+ const at = lastRelevant >= 0 ? lastRelevant + 1 : 0
341
+ lines.splice(at, 0, wantEnv)
342
+ changed = true
260
343
  }
261
344
  return { text: changed ? lines.join('\n') : String(text || ''), changed }
262
345
  }
@@ -275,10 +358,12 @@ export async function setupCodex({ nodePath, dryRun = false, log = () => {} } =
275
358
  // issue #44 现场的教训:TOML 里可能是**单引号**(`command = 'C:\…'`),老代码只认双引号 →
276
359
  // 既读不到(status 报「配置里是 (空)」)也改不掉(`replace` 不命中),**却照样打印「已更新」**。
277
360
  // 所以:① 读写都认两种引号;② 改完必须**校验文本真的变了**,没变就如实报失败。
278
- const curCmd = /command\s*=\s*["']([^"']*)["']/.exec(text)?.[1] || ''
279
- const curArgs = /^args\s*=\s*\[([^\]]*)\]\s*$/m.exec(text)?.[1] || ''
280
- const argsOk = want.args.every((a) => curArgs.includes(JSON.stringify(a)) || curArgs.includes("'" + a + "'"))
281
- if (curCmd === want.command && argsOk) {
361
+ // 必须按 [mcp_servers.memory] 段读取;直接在全文抓 command 会抓到别的 server(issue #44)。
362
+ const entry = parseCodexMemoryEntry(text)
363
+ const curCmd = entry.command
364
+ const argsOk = entry.args.length === want.args.length && want.args.every((a, i) => entry.args[i] === a)
365
+ const envOk = !want.env || /ELECTRON_RUN_AS_NODE\s*=\s*["']1["']/.test(text)
366
+ if (curCmd === want.command && argsOk && envOk) {
282
367
  log('✓ Codex MCP 已是最新,跳过')
283
368
  return [{ agent: 'codex', ok: true, reason: 'section exists' }]
284
369
  }
@@ -303,6 +388,7 @@ export async function setupCodex({ nodePath, dryRun = false, log = () => {} } =
303
388
  '[mcp_servers.memory]',
304
389
  `command = ${JSON.stringify(want.command)}`,
305
390
  `args = ${JSON.stringify(want.args)}`,
391
+ ...(want.env ? [codexEnvLine(want.env)] : []),
306
392
  '',
307
393
  ].join('\n')
308
394
  if (!dryRun) {
@@ -367,9 +453,10 @@ export async function runSetup(opts = {}) {
367
453
  }
368
454
  const all = []
369
455
  const want = (name) => only.length === 0 || only.includes(name)
370
- if (want('claude')) all.push(...await setupClaude({ nodePath: process.execPath, withHooks, dryRun, log }))
371
- if (want('codex')) all.push(...await setupCodex({ nodePath: process.execPath, dryRun, log }))
372
- if (want('cursor')) all.push(...await setupCursor({ nodePath: process.execPath, dryRun, log }))
456
+ const nodePath = nodeBinary()
457
+ if (want('claude')) all.push(...await setupClaude({ nodePath, withHooks, dryRun, log }))
458
+ if (want('codex')) all.push(...await setupCodex({ nodePath, dryRun, log }))
459
+ if (want('cursor')) all.push(...await setupCursor({ nodePath, dryRun, log }))
373
460
  return { ok: true, results: all }
374
461
  }
375
462
 
@@ -377,7 +464,7 @@ export async function runSetup(opts = {}) {
377
464
  * 只读查询各 agent 的 MCP 配置状态。不修改任何文件。
378
465
  * @returns {Promise<{ok:true, agents: Array, lastCheckedAt: number}>}
379
466
  */
380
- export async function getSetupStatus({ nodePath = process.execPath } = {}) {
467
+ export async function getSetupStatus({ nodePath = nodeBinary() } = {}) {
381
468
  const agents = []
382
469
  const want = mcpCommand(nodePath)
383
470
  // claude-code
@@ -413,6 +500,10 @@ export async function getSetupStatus({ nodePath = process.execPath } = {}) {
413
500
  } else {
414
501
  agents[agents.length - 1].hook = 'missing'
415
502
  }
503
+ const last = agents[agents.length - 1]
504
+ last.mcpNeedsRepair = mcpExists && isLegacyElectronCommand(entry?.command)
505
+ last.hookNeedsRepair = await hooksNeedRepair(path.join(home, '.claude', 'settings.json'), ['capture.mjs', 'secret-guard.mjs'])
506
+ last.needsRepair = Boolean(last.mcpNeedsRepair || last.hookNeedsRepair)
416
507
  }
417
508
  // codex
418
509
  const codexToml = path.join(home, '.codex', 'config.toml')
@@ -433,12 +524,15 @@ export async function getSetupStatus({ nodePath = process.execPath } = {}) {
433
524
  servers: listCodexMcpServers(text),
434
525
  installed: true,
435
526
  mcpConfigured: has,
436
- mcpMatchesCurrentNode: has && configuredNodePath === nodePath,
527
+ mcpMatchesCurrentNode: has && configuredNodePath === nodePath && (!want.env || /ELECTRON_RUN_AS_NODE\s*=\s*["']1["']/.test(text)),
437
528
  currentNodePath: nodePath,
438
529
  configuredNodePath,
439
530
  mcpPath: codexToml,
440
531
  hook: await hookState(path.join(home, '.codex', 'hooks.json'), 'codex'),
532
+ mcpNeedsRepair: has && isLegacyElectronCommand(configuredNodePath),
533
+ hookNeedsRepair: await hooksNeedRepair(path.join(home, '.codex', 'hooks.json'), ['capture.mjs']),
441
534
  })
535
+ agents[agents.length - 1].needsRepair = Boolean(agents[agents.length - 1].mcpNeedsRepair || agents[agents.length - 1].hookNeedsRepair)
442
536
  }
443
537
  // cursor
444
538
  const cursorJson = path.join(home, '.cursor', 'mcp.json')
@@ -464,7 +558,10 @@ export async function getSetupStatus({ nodePath = process.execPath } = {}) {
464
558
  configuredNodePath: entry?.command || '',
465
559
  mcpPath: cursorJson,
466
560
  hook: await hookState(path.join(home, '.cursor', 'hooks.json'), 'cursor', true),
561
+ mcpNeedsRepair: mcpExists && isLegacyElectronCommand(entry?.command),
562
+ hookNeedsRepair: await hooksNeedRepair(path.join(home, '.cursor', 'hooks.json'), ['capture.mjs']),
467
563
  })
564
+ agents[agents.length - 1].needsRepair = Boolean(agents[agents.length - 1].mcpNeedsRepair || agents[agents.length - 1].hookNeedsRepair)
468
565
  } catch { agents.push({ name: 'cursor', installed: true, mcpConfigured: false, mcpPath: cursorJson, reason: 'JSON 解析失败' }) }
469
566
  }
470
567
  // 兼容性探测:只列出「本机确实存在配置文件」的其它 MCP 客户端(只读,不提供挂载按钮)
@@ -484,10 +581,11 @@ export async function getSetupStatus({ nodePath = process.execPath } = {}) {
484
581
  const summary = {
485
582
  detectedAgents: agents.filter((a) => a.installed).length,
486
583
  mountedAgents: agents.filter((a) => a.mcpConfigured).length,
584
+ needsRepair: agents.filter((a) => a.needsRepair).length,
487
585
  totalServers: allServers.length,
488
586
  allServers,
489
587
  }
490
- return { ok: true, agents, summary, lastCheckedAt: Date.now(), want: { command: nodePath, args: want.args } }
588
+ return { ok: true, agents, summary, lastCheckedAt: Date.now(), want: { command: nodePath, args: want.args, ...(want.env ? { env: want.env } : {}) } }
491
589
  }
492
590
 
493
591
  // -- 会话结束自动沉淀 hook(connect <agent>,agentmemory 的 `connect --with-hooks` 兜底路径)--
@@ -496,9 +594,9 @@ export async function getSetupStatus({ nodePath = process.execPath } = {}) {
496
594
 
497
595
  /** capture.mjs 的完整调用(绝对路径,避免升级后路径内嵌版本号而失效)。 */
498
596
  function captureCmd(agent) {
499
- const node = process.execPath
597
+ const node = nodeBinary()
500
598
  const script = path.join(PACKAGE_ROOT, 'hooks', 'capture.mjs')
501
- return `${JSON.stringify(node)} ${JSON.stringify(script)} ${agent}`
599
+ return hookCommand(node, script, agent)
502
600
  }
503
601
 
504
602
  /** 写一个幂等 hooks.json:events → [{command}](cursor 风格)或 {hooks:[{type,command}]}(codex 风格)。
@@ -550,7 +648,7 @@ export async function connectCursorHooks(opts = {}) {
550
648
  /** 统一入口:claude 复用 setupClaude 的 SessionEnd(capture.mjs);codex/cursor 写 capture.mjs hooks。 */
551
649
  export async function connectAgentHooks(agent, opts = {}) {
552
650
  if (agent === 'claude' || agent === 'claude-code') {
553
- const results = await setupClaude({ nodePath: process.execPath, withHooks: true, log: opts.log })
651
+ const results = await setupClaude({ nodePath: nodeBinary(), withHooks: true, log: opts.log })
554
652
  return { ok: results.every((r) => r.ok), results }
555
653
  }
556
654
  if (agent === 'codex') return connectCodexHooks(opts)
@@ -578,13 +676,14 @@ export async function removeClaudeMcp({ dryRun = false, log = () => {} } = {}) {
578
676
  if (await exists(settingsJson)) {
579
677
  try {
580
678
  const scfg = JSON.parse(await fs.readFile(settingsJson, 'utf8'))
581
- if (scfg.hooks?.SessionEnd) {
582
- const kept = scfg.hooks.SessionEnd.filter((h) => !(h.hooks || []).some((c) => c?.command?.includes('capture.mjs')))
583
- if (kept.length !== scfg.hooks.SessionEnd.length) {
584
- if (!dryRun) { await backupOnce(settingsJson); scfg.hooks.SessionEnd = kept; await fs.writeFile(settingsJson, JSON.stringify(scfg, null, 2), 'utf8') }
585
- removed = true
586
- }
679
+ let dirty = false
680
+ for (const key of ['SessionEnd', 'PreToolUse']) {
681
+ const list = scfg.hooks?.[key]
682
+ if (!Array.isArray(list)) continue
683
+ const kept = list.filter((h) => !(h.hooks || []).some((c) => c?.command?.includes('capture.mjs') || c?.command?.includes('secret-guard.mjs')))
684
+ if (kept.length !== list.length) { scfg.hooks[key] = kept; dirty = true; removed = true }
587
685
  }
686
+ if (dirty && !dryRun) { await backupOnce(settingsJson); await fs.writeFile(settingsJson, JSON.stringify(scfg, null, 2), 'utf8') }
588
687
  } catch {}
589
688
  }
590
689
  log(removed ? (dryRun ? '[dry-run] 将卸载 Claude Code MCP' : '✓ 已卸载 Claude Code MCP') : '✓ Claude Code MCP 未配置')
@@ -599,7 +698,7 @@ export async function removeCodexMcp({ dryRun = false, log = () => {} } = {}) {
599
698
  const sectionRe = /^\s*\[mcp_servers\.memory\]\s*$/m
600
699
  if (!sectionRe.test(text)) { log('✓ Codex MCP 未配置'); return { agent: 'codex', ok: true, removed: false } }
601
700
  if (!dryRun) {
602
- const newText = text.replace(/^\s*# memory-eternal.*\n?/m, '').replace(/^\s*\[mcp_servers\.memory\]\s*\n(?:command\s*=.*\n|args\s*=.*\n)+\s*/m, '')
701
+ const newText = text.replace(/^\s*# memory-eternal.*\n?/m, '').replace(/^\s*\[mcp_servers\.memory\]\s*\n(?:command\s*=.*\n|args\s*=.*\n|env\s*=.*\n)+\s*/m, '')
603
702
  await backupOnce(codexToml)
604
703
  await fs.writeFile(codexToml, newText.replace(/\n{3,}/g, '\n\n'), 'utf8')
605
704
  }
@@ -626,7 +725,7 @@ export async function removeCursorMcp({ dryRun = false, log = () => {} } = {}) {
626
725
  /** 对单个智能体执行安装/卸载 MCP。action: 'install' | 'uninstall'。agent: 'claude-code'|'codex'|'cursor' */
627
726
  export async function mcpAgentAction(agent, action, { dryRun = false, log = () => {} } = {}) {
628
727
  if (action === 'install') {
629
- const nodePath = process.execPath
728
+ const nodePath = nodeBinary()
630
729
  if (agent === 'claude-code') return { ok: true, results: await setupClaude({ nodePath, dryRun, log }) }
631
730
  if (agent === 'codex') return { ok: true, results: await setupCodex({ nodePath, dryRun, log }) }
632
731
  if (agent === 'cursor') return { ok: true, results: await setupCursor({ nodePath, dryRun, log }) }
package/lib/vault.js CHANGED
@@ -10,6 +10,7 @@ import { minhashSignature, lshCandidates } from './minhash.js'
10
10
 
11
11
  /** 主题目录根。 */
12
12
  export const KIND_ROOTS = {
13
+ core: '09-Core',
13
14
  project: '02-Projects',
14
15
  knowledge: '03-Knowledge',
15
16
  content: '04-Content',
@@ -19,6 +20,9 @@ export const KIND_ROOTS = {
19
20
  mistake: '08-Mistakes',
20
21
  }
21
22
 
23
+ /** 核心记忆:人工批准后,每次会话注入 systemPrompt 的卡片类型。 */
24
+ export const CORE_KIND = 'core'
25
+
22
26
  export const CAPTURE_KINDS = ['project', 'knowledge', 'content', 'prompt', 'business', 'tool', 'mistake']
23
27
 
24
28
  /** 从 Markdown 文本提取 frontmatter 与正文。 */
@@ -593,6 +597,22 @@ export function normalizeTags(tags) {
593
597
  .slice(0, 20)
594
598
  }
595
599
 
600
+ /**
601
+ * 把一张已批准的卡片设为核心记忆(kind=core),或恢复成普通知识卡。
602
+ * 只有主库(approved)卡片可以设为核心;审核中的卡必须先批准。
603
+ */
604
+ export async function setCardKind(root, rel, kind) {
605
+ const db = getDb(root)
606
+ const target = String(kind || '').trim()
607
+ const existing = findCardRow(db, rel)
608
+ if (!existing) throw new Error('卡片不存在: ' + rel)
609
+ if (existing.table !== 'cards') throw new Error('只有已批准卡片才能设为核心记忆,请先在审核中心批准')
610
+ if (target !== CORE_KIND && !CAPTURE_KINDS.includes(target)) throw new Error('不支持的卡片类型: ' + target)
611
+ const now = new Date().toISOString()
612
+ db.prepare('UPDATE cards SET kind = ?, updated_at = ? WHERE path = ?').run(target, now, rel)
613
+ return { ok: true, path: rel, kind: target, updatedAt: now }
614
+ }
615
+
596
616
  export async function writeCard(root, { kind, title, tags = [], body, source = '', status = 'pending', submittedBy = '', severity = 'info', reason = '', deletedAt = '' }, { threshold = 0.62, dedup = true, dedupAgainst = null } = {}) {
597
617
  const db = getDb(root)
598
618
  const kindRoot = KIND_ROOTS[kind] || KIND_ROOTS.knowledge
@@ -1,36 +1,36 @@
1
- {
2
- "manifestVersion": 1,
3
- "id": "memory-eternal",
4
- "publisher": "eternalnight996",
5
- "name": "memory-eternal",
6
- "version": "0.17.0",
7
- "description": "Memory Atlas for Orca: open the local memory vault, pending review and knowledge graph from a sandboxed Orca panel.",
8
- "author": {
9
- "name": "EternalNight996",
10
- "url": "https://github.com/EternalNight996"
11
- },
12
- "repository": "https://github.com/EternalNight996/memory-eternal",
13
- "icon": "assets/icon.png",
14
- "engines": {
15
- "orca": ">=1.4.0"
16
- },
17
- "pluginApi": 1,
18
- "contributes": {
19
- "panels": [
20
- {
21
- "id": "memory",
22
- "title": "记忆",
23
- "icon": "brain",
24
- "entry": "panel.html"
25
- }
26
- ]
27
- },
28
- "capabilities": [
29
- {
30
- "kind": "workspace:read"
31
- },
32
- {
33
- "kind": "terminal:send"
34
- }
35
- ]
36
- }
1
+ {
2
+ "manifestVersion": 1,
3
+ "id": "memory-eternal",
4
+ "publisher": "eternalnight996",
5
+ "name": "memory-eternal",
6
+ "version": "0.18.0",
7
+ "description": "Memory Atlas for Orca: open the local memory vault, pending review and knowledge graph from a sandboxed Orca panel.",
8
+ "author": {
9
+ "name": "EternalNight996",
10
+ "url": "https://github.com/EternalNight996"
11
+ },
12
+ "repository": "https://github.com/EternalNight996/memory-eternal",
13
+ "icon": "assets/icon.png",
14
+ "engines": {
15
+ "orca": ">=1.4.0"
16
+ },
17
+ "pluginApi": 1,
18
+ "contributes": {
19
+ "panels": [
20
+ {
21
+ "id": "memory",
22
+ "title": "记忆",
23
+ "icon": "brain",
24
+ "entry": "panel.html"
25
+ }
26
+ ]
27
+ },
28
+ "capabilities": [
29
+ {
30
+ "kind": "workspace:read"
31
+ },
32
+ {
33
+ "kind": "terminal:send"
34
+ }
35
+ ]
36
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "memory-eternal",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "记忆核心(Memory Eternal):自研的 DeepSeek Harness 记忆插件,不移植任何既有记忆框架——对话结束后自动沉淀知识卡到本地 Markdown Vault(自研去重、自研 CJK 检索、可 git 管理),设置页提供图形化知识库(统计 / 搜索 / 知识图谱 + 侧边栏一键弹窗),Agent 通过 memory_recall 工具按需召回历史上下文。零人工干预。",
5
5
  "icon": "assets/icon.png",
6
6
  "license": "MIT",
@@ -70,7 +70,7 @@
70
70
  "release": "node scripts/release.mjs",
71
71
  "release:dry": "node scripts/release.mjs --dry-run",
72
72
  "panel-check": "node scripts/panel-check.mjs",
73
- "test": "node build.mjs && node tests/vault.test.mjs && node tests/capture.test.mjs && node tests/capture-route.test.mjs && node tests/stall.test.mjs && node tests/vault-path.test.mjs && node tests/vault-resolve.test.mjs && node tests/minhash.test.mjs && node tests/settings-save.test.mjs && node tests/config-coverage.test.mjs && node tests/config-sync.test.mjs && node tests/config-merge.test.mjs && node tests/sse.test.mjs && node tests/api.test.mjs && node tests/import-roundtrip.test.mjs && node tests/graph-perf.test.mjs && node tests/settings-compat.test.mjs && node tests/node-bin.test.mjs && node tests/watchdog-lock.test.mjs && node tests/markdown.test.mjs && node tests/feedback.test.mjs && node tests/graph-lod.test.mjs && node tests/graph-warmup.test.mjs && node tests/loading-hints.test.mjs && node tests/web-entry.test.mjs && node tests/tui-scroll.test.mjs && node tests/tui-audit-scene.test.mjs && node tests/tui-plugin-boot.test.mjs && node tests/capture-session-id.test.mjs && node tests/ssr.test.mjs && node tests/window-controls.test.mjs && node tests/web-assets.test.mjs && node tests/manifests.test.mjs && node tests/two-store.test.mjs && node tests/noise-gate.test.mjs && node tests/e2e-recall.test.mjs && node tests/issue-fixes.test.mjs && node tests/issue-fixes-2.test.mjs && node tests/issue-fixes-3.test.mjs && node tests/watchdog-orphans.test.mjs && node tests/watchdog-restart.test.mjs && node tests/readme-i18n.test.mjs && node tests/issue-fixes-4.test.mjs && node tests/host-drain.test.mjs && node tests/e2e-host-drain.mjs && node tests/issue-fixes-5.test.mjs && node tests/issue-fixes-6.test.mjs && node tests/capture-json-repair.test.mjs && node tests/csrf-origin.test.mjs && node tests/watchdog-orphan-web.test.mjs && node tests/config-validate.test.mjs && node tests/issue-fixes-8.test.mjs && node tests/issue-fixes-9.test.mjs && node tests/release-verify.test.mjs && node tests/refine.test.mjs && node tests/db-restore.test.mjs && node tests/align.test.mjs && node tests/tags-normalize.test.mjs && node tests/resident-state.test.mjs && node tests/llm-route.test.mjs && node tests/host-managed.test.mjs && node tests/replace-claim.test.mjs && node tests/version-check-self.test.mjs && node tests/setup-codex.test.mjs && node tests/listener-netstat.test.mjs && node tests/issue-46-overlay.test.mjs && node tests/graph-initial-zoom.test.mjs && node tests/card-head-actions.test.mjs && node tests/card-reader-ux.test.mjs && node tests/tag-filter.test.mjs && node tests/ui-overflow.test.mjs && node tests/version-card-state.test.mjs && node tests/llm-routes-proxy.test.mjs && node tests/model-strikes.test.mjs && node tests/support-bundle.test.mjs && node tests/health-panel.test.mjs && node tests/issue-48-timeout.test.mjs && node tests/ledger.test.mjs"
73
+ "test": "node build.mjs && node tests/vault.test.mjs && node tests/capture.test.mjs && node tests/capture-route.test.mjs && node tests/stall.test.mjs && node tests/vault-path.test.mjs && node tests/vault-resolve.test.mjs && node tests/minhash.test.mjs && node tests/settings-save.test.mjs && node tests/config-coverage.test.mjs && node tests/config-sync.test.mjs && node tests/config-merge.test.mjs && node tests/sse.test.mjs && node tests/api.test.mjs && node tests/import-roundtrip.test.mjs && node tests/graph-perf.test.mjs && node tests/settings-compat.test.mjs && node tests/node-bin.test.mjs && node tests/watchdog-lock.test.mjs && node tests/markdown.test.mjs && node tests/feedback.test.mjs && node tests/graph-lod.test.mjs && node tests/graph-warmup.test.mjs && node tests/loading-hints.test.mjs && node tests/web-entry.test.mjs && node tests/tui-scroll.test.mjs && node tests/tui-audit-scene.test.mjs && node tests/tui-plugin-boot.test.mjs && node tests/capture-session-id.test.mjs && node tests/ssr.test.mjs && node tests/window-controls.test.mjs && node tests/web-assets.test.mjs && node tests/manifests.test.mjs && node tests/two-store.test.mjs && node tests/noise-gate.test.mjs && node tests/e2e-recall.test.mjs && node tests/issue-fixes.test.mjs && node tests/issue-fixes-2.test.mjs && node tests/issue-fixes-3.test.mjs && node tests/watchdog-orphans.test.mjs && node tests/watchdog-restart.test.mjs && node tests/readme-i18n.test.mjs && node tests/issue-fixes-4.test.mjs && node tests/host-drain.test.mjs && node tests/e2e-host-drain.mjs && node tests/issue-fixes-5.test.mjs && node tests/issue-fixes-6.test.mjs && node tests/capture-json-repair.test.mjs && node tests/csrf-origin.test.mjs && node tests/watchdog-orphan-web.test.mjs && node tests/config-validate.test.mjs && node tests/issue-fixes-8.test.mjs && node tests/issue-fixes-9.test.mjs && node tests/release-verify.test.mjs && node tests/refine.test.mjs && node tests/db-restore.test.mjs && node tests/align.test.mjs && node tests/tags-normalize.test.mjs && node tests/resident-state.test.mjs && node tests/llm-route.test.mjs && node tests/host-managed.test.mjs && node tests/replace-claim.test.mjs && node tests/version-check-self.test.mjs && node tests/setup-codex.test.mjs && node tests/listener-netstat.test.mjs && node tests/issue-46-overlay.test.mjs && node tests/graph-initial-zoom.test.mjs && node tests/card-head-actions.test.mjs && node tests/card-reader-ux.test.mjs && node tests/tag-filter.test.mjs && node tests/ui-overflow.test.mjs && node tests/version-card-state.test.mjs && node tests/llm-routes-proxy.test.mjs && node tests/model-strikes.test.mjs && node tests/support-bundle.test.mjs && node tests/health-panel.test.mjs && node tests/issue-48-timeout.test.mjs && node tests/setup-node-path.test.mjs && node tests/capture-kind.test.mjs && node tests/core-memory.test.mjs && node tests/ledger.test.mjs"
74
74
  },
75
75
  "dependencies": {
76
76
  "@deepseek-ai/schemastery": "^3.18.1"