dsh-plain-memory 1.0.1 → 1.1.2

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/index.js CHANGED
@@ -17,7 +17,7 @@ import { join, resolve } from 'node:path'
17
17
  import { fileURLToPath } from 'node:url'
18
18
  import {
19
19
  appendEntry, buildIndex, entryId, estimateTokens, findSecret, formatEntry, readRepo,
20
- recallAll, slug, splitCues, summarize, today, tokenize,
20
+ recallAll, slug, splitCues, summarize, today, tokenize, MIN_MATCHED_TERMS,
21
21
  } from './memory.js'
22
22
  import { branchOf, commit, isDirty, isRepo, pull, pushBackground } from './git.js'
23
23
  import { ensureState, readCards, writeCard } from './cards.js'
@@ -50,10 +50,14 @@ export function normalizeConfig(config = {}) {
50
50
  maxRecall: Math.min(5, positive(config.maxRecall, 3)),
51
51
  minScore: positive(config.minScore, 0.3),
52
52
  cardScore: positive(config.cardScore, 0.3),
53
- // 默认 1(一个线索即可);调成 2 会明显更安静,但实测会把跨领域召回从 5/5 压到 2/5
53
+ // 自动注入默认关闭(1.1.1 起):验收实测注入精度 7/20,开着等于每 20 次注入 13 次噪声。
54
+ // 打开 = 每轮按线索召回最多 maxRecall 条;关闭 = 每轮零注入,靠 memory_recall 主动查。
55
+ autoRecall: config.autoRecall === true,
54
56
  // 多窗口状态卡默认关闭(实验性):只写卡、不推送时,/state 里不会多出任何注入
55
57
  cards: config.cards === true,
56
- minCues: positive(config.minCues, 1),
58
+ // 默认 2:至少两个独立线索才算想起来(与 memory.js 的 MIN_MATCHED_TERMS 一致)。
59
+ // 默认 1 等于「碰上一个词就注入」,生产日志里大部分噪声从这里来。
60
+ minCues: positive(config.minCues, MIN_MATCHED_TERMS),
57
61
  }
58
62
  }
59
63
 
@@ -90,17 +94,34 @@ function agentsFile() {
90
94
  export function renderSelfBlock(repo) {
91
95
  const entries = readRepo(repo).filter((e) => e.layer === 'self')
92
96
  const lines = [SELF_BEGIN, '## 用户长期偏好(由 dsh-plain-memory 维护,按此执行)']
93
- for (const e of entries) {
94
- const text = (e.body || e.title).replace(/\s+/g, ' ').trim()
95
- const line = `- ${text}`
96
- if (estimateTokens([...lines, line, SELF_END].join('\n')) > SELF_TOKEN_BUDGET) break
97
- lines.push(line)
98
- }
97
+ for (const e of entries) lines.push(selfLine(e))
99
98
  if (lines.length === 2) return ''
100
99
  lines.push(SELF_END)
101
100
  return lines.join('\n')
102
101
  }
103
102
 
103
+ function selfLine(entry) {
104
+ return `- ${(entry.body || entry.title).replace(/\s+/g, ' ').trim()}`
105
+ }
106
+
107
+ /** 自我层的预算只在「写入时」把关:显示时一条都不丢,写满了就拒绝新条目并让人合并规则。 */
108
+ export function selfTokens(repo, extra) {
109
+ const entries = readRepo(repo).filter((e) => e.layer === 'self')
110
+ const all = extra ? [...entries, extra] : entries
111
+ return estimateTokens(all.map(selfLine).join('\n'))
112
+ }
113
+
114
+ export function assertSelfBudget(repo, entry) {
115
+ const tokens = selfTokens(repo, entry)
116
+ if (tokens <= SELF_TOKEN_BUDGET) return
117
+ const file = join(repo, 'self.md')
118
+ throw new Error(
119
+ `自我层已超预算(约 ${tokens}/${SELF_TOKEN_BUDGET} token),写入被拒。`
120
+ + `规则文件:${file}。自我层每条都必须生效、不能在显示时截断,所以这里不接收新条目——`
121
+ + '请用文件工具把相近的规则合并成一条后重试。',
122
+ )
123
+ }
124
+
104
125
  /** 只改托管块,保留 AGENTS.md 里用户自己的内容。 */
105
126
  export function syncSelfBlock(repo) {
106
127
  const file = agentsFile()
@@ -134,6 +155,26 @@ export function removeSelfBlock() {
134
155
  writeFileSync(file, next ? `${next}\n` : '', 'utf8')
135
156
  }
136
157
 
158
+ /** 只有「明确截止日期」才自动填 expires;观察日期(某天实测)不算,否则会把有效记忆整条作废。 */
159
+ const DEADLINE_WORDS = /(截止|到期|deadline|最晚|交付|交稿|提交日|提交截止)/i
160
+ const DATE_RE = /\b(20\d{2})-(\d{2})-(\d{2})\b/
161
+
162
+ export function detectExpiry(text) {
163
+ const s = String(text || '')
164
+ if (!DATE_RE.test(s) || !DEADLINE_WORDS.test(s)) return ''
165
+ DATE_RE.lastIndex = 0
166
+ const m = DATE_RE.exec(s)
167
+ DATE_RE.lastIndex = 0
168
+ return m ? m[0] : ''
169
+ }
170
+
171
+ function normalizeExpiry(value) {
172
+ const s = String(value || '').trim()
173
+ if (!s) return ''
174
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) throw new Error('expires 必须是 YYYY-MM-DD')
175
+ return s
176
+ }
177
+
137
178
  function sessionKey(agent) {
138
179
  return String(agent?.session?.id ?? '')
139
180
  }
@@ -225,7 +266,7 @@ export function apply(ctx, config = {}) {
225
266
  id, agent, cwd, line: '',
226
267
  files: new Set(), objective: '', conclusion: '',
227
268
  index: null, ready: false, pulled: false, isGit: false,
228
- writes: 0, cardAt: 0, seenCards: new Set(), seenMem: new Set(), lastGoal: '',
269
+ writes: 0, cardAt: 0, seenCards: new Set(), seenMem: new Set(), pendingUsed: [], lastGoal: '',
229
270
  preSteps: 0, injected: 0, warned: new Set(), flushing: false, pendingFlush: false,
230
271
  }
231
272
  sessions.set(id, st)
@@ -309,17 +350,45 @@ export function apply(ctx, config = {}) {
309
350
  return makeUserMessage(text, name, 'recall')
310
351
  }
311
352
 
353
+ /** 本会话历史里注入过的内容;宿主不提供事件流时返回 null(退回进程内的 seenMem)。 */
354
+ function recalledHistory(st) {
355
+ const session = st.agent?.session
356
+ if (!session || typeof session.eventAt !== 'function' || typeof session.seq !== 'number') return null
357
+ const parts = []
358
+ const from = Math.max(0, session.seq - 400)
359
+ for (let i = from; i < session.seq; i += 1) {
360
+ let event
361
+ try { event = session.eventAt(i) } catch { continue }
362
+ if (event?.type !== 'user/message') continue
363
+ const data = event.data || {}
364
+ const text = (data.content || []).map((b) => b?.text || '').join('\n')
365
+ // source.kind 在部分宿主会被归一,认内容标记更稳
366
+ if (data.source?.kind !== name && !text.includes(HEADER.trim().split('\n')[0])) continue
367
+ parts.push(text)
368
+ }
369
+ return parts.join('\n')
370
+ }
371
+
312
372
  async function recallLines(st, text) {
373
+ // 自动注入不按句切开(切句会让三个字的一段碰到一个词就拿高分),概念桥与关联跳转只留给主动查询
374
+ const hits = recallAll(st.index, text, opts.maxRecall, opts.minScore, opts.minCues, { split: false, hop: false })
375
+ // 去重不存状态:直接看标题是否已在本会话历史里出现过,进程重启也不会重复注入
376
+ const history = recalledHistory(st)
377
+ const kept = []
378
+ const titles = new Set()
313
379
  const lines = []
314
- const hits = recallAll(st.index, text, opts.maxRecall, opts.minScore, opts.minCues)
315
- .filter((h) => !st.seenMem.has(h.entry.id))
316
380
  for (const hit of hits) {
317
381
  const e = hit.entry
382
+ if (titles.has(e.title)) continue
383
+ const dup = history === null ? st.seenMem.has(e.id) : history.includes(e.title)
384
+ if (dup) continue
385
+ titles.add(e.title)
386
+ if (history === null) st.seenMem.add(e.id)
387
+ kept.push(hit)
318
388
  const label = hit.hop ? '关联' : e.layer === 'self' ? '自我' : e.layer === 'episodic' ? '情景' : '语义'
319
389
  lines.push(`[${label}] ${e.title}(${e.date || '无日期'},会话 ${shortId(e.session)}):${summarize(e, 140)}`)
320
- st.seenMem.add(e.id)
321
390
  }
322
- return { lines, hits }
391
+ return { lines, hits: kept }
323
392
  }
324
393
 
325
394
  /** 会话里最后一条真人消息;判断「用户是不是亲口说了记住」。 */
@@ -340,24 +409,78 @@ export function apply(ctx, config = {}) {
340
409
  return /记住|记下|记住这|以后都|以后不要|以后别|别再/.test(lastUserText(agent))
341
410
  }
342
411
 
343
- /** 观察日志:每次注入记一行,第 7 天据此统计误召回。relevant 留空由人补。 */
412
+ /**
413
+ * 观察日志:每次注入记一行(`hits` 是命中的词,`relevant` 留空由人补)。
414
+ * 「有没有用上」由下一轮或回合结束另起一行补 `used`:模型回答里出现该条正文的 ≥2 个特征词才算用上,
415
+ * 证据不足就记 null,不猜。两行都只记聚合事实,不记正文内容。
416
+ */
417
+ const FEATURE_MAX = 24
418
+ function featuresOf(entry) {
419
+ return [...new Set(tokenize(`${entry.title} ${entry.body}`))].filter((t) => !t.startsWith('+')).slice(0, FEATURE_MAX)
420
+ }
421
+
344
422
  function logRecall(st, text, hits, cardCount) {
345
423
  try {
424
+ const at = new Date().toISOString()
346
425
  const line = JSON.stringify({
347
- at: new Date().toISOString(),
426
+ at,
348
427
  session: shortId(st.id),
349
- q: text.replace(/\s+/g, ' ').slice(0, 120),
428
+ q: text.replace(/\s+/g, ' ').slice(0, 600),
350
429
  ids: hits.map((h) => h.entry.id),
351
430
  titles: hits.map((h) => h.entry.title),
352
431
  scores: hits.map((h) => Number(h.score.toFixed(3))),
432
+ hits: hits.map((h) => h.terms || []),
353
433
  hop: hits.filter((h) => h.hop).length,
354
434
  cards: cardCount,
355
435
  relevant: null,
356
436
  })
357
437
  appendFileSync(join(ensureState(opts.repoPath), 'recall.log'), `${line}\n`, 'utf8')
438
+ if (hits.length) {
439
+ st.pendingUsed.push({
440
+ at,
441
+ session: shortId(st.id),
442
+ ids: hits.map((h) => h.entry.id),
443
+ features: hits.map((h) => featuresOf(h.entry)),
444
+ fromSeq: Number(st.agent?.session?.seq ?? 0),
445
+ })
446
+ if (st.pendingUsed.length > 3) st.pendingUsed.shift()
447
+ }
358
448
  } catch (error) { log('recall log failed', String(error)) }
359
449
  }
360
450
 
451
+ /** 注入之后产生的模型回答文本;没有事件流就返回 null。 */
452
+ function assistantTextAfter(st, fromSeq) {
453
+ const session = st.agent?.session
454
+ if (!session || typeof session.eventAt !== 'function' || typeof session.seq !== 'number') return null
455
+ const parts = []
456
+ for (let i = Math.max(0, fromSeq); i < session.seq; i += 1) {
457
+ let event
458
+ try { event = session.eventAt(i) } catch { continue }
459
+ if (!String(event?.type || '').startsWith('assistant')) continue
460
+ for (const b of event.data?.content || []) if (b?.type === 'text') parts.push(b.text)
461
+ }
462
+ return parts.join('\n')
463
+ }
464
+
465
+ function finalizeUsed(st) {
466
+ if (!st.pendingUsed.length) return
467
+ const pending = st.pendingUsed
468
+ st.pendingUsed = []
469
+ for (const p of pending) {
470
+ const text = assistantTextAfter(st, p.fromSeq)
471
+ const used = text === null || !text.trim()
472
+ ? p.ids.map(() => null)
473
+ : p.features.map((f) => f.filter((t) => text.includes(t)).length >= 2)
474
+ try {
475
+ appendFileSync(
476
+ join(ensureState(opts.repoPath), 'recall.log'),
477
+ `${JSON.stringify({ at: p.at, session: p.session, ids: p.ids, used, via: text === null ? 'no-events' : 'events' })}\n`,
478
+ 'utf8',
479
+ )
480
+ } catch { /* 日志失败不影响主流程 */ }
481
+ }
482
+ }
483
+
361
484
  /**
362
485
  * 状态卡按「会过期的记忆」匹配:用和记忆召回同一套线索匹配(用户消息 vs 卡的目标/结论/改动文件名),
363
486
  * 过阈值才注入;同分支、同文件只是加分项。超过 24 小时没更新的卡直接失忆。
@@ -484,9 +607,10 @@ export function apply(ctx, config = {}) {
484
607
  st.preSteps += 1
485
608
  st.lastGoal = text.split('\n').find((l) => l.trim())?.trim().slice(0, 60) || ''
486
609
  auditInjection(st)
610
+ finalizeUsed(st)
487
611
  touchCard(st)
488
- const recalled = await recallLines(st, text)
489
- const cards = opts.cards ? cardLines(st, text) : []
612
+ const recalled = opts.autoRecall ? await recallLines(st, text) : { lines: [], hits: [] }
613
+ const cards = opts.autoRecall && opts.cards ? cardLines(st, text) : []
490
614
  const lines = [...recalled.lines, ...cards]
491
615
  if (lines.length === 0) return decision
492
616
  st.injected += 1
@@ -518,6 +642,7 @@ export function apply(ctx, config = {}) {
518
642
  const st = sessions.get(sessionKey(agent))
519
643
  if (!st) { trace(`turn-stopping unknown session=${shortId(sessionKey(agent))}`); return }
520
644
  trace(`turn-stopping session=${shortId(st.id)} writes=${st.writes} preSteps=${st.preSteps}`)
645
+ finalizeUsed(st)
521
646
  if (st.preSteps === 0) {
522
647
  warnOnce(st, 'hook-not-fired', '会话有回合结束,但 agent/pre-step 一次都没触发;记忆注入未生效')
523
648
  }
@@ -561,6 +686,7 @@ export function apply(ctx, config = {}) {
561
686
  body: { type: 'string', description: '记忆正文,不超过 600 字' },
562
687
  cues: { type: 'string', description: '线索词,逗号分隔,最多 12 个' },
563
688
  related: { type: 'string', description: '关联条目 id,逗号分隔' },
689
+ expires: { type: 'string', description: '可选,YYYY-MM-DD。这条记忆到期后不再被自动召回;只有带截止日期的事才填' },
564
690
  },
565
691
  },
566
692
  output: textOutput,
@@ -579,12 +705,15 @@ export function apply(ctx, config = {}) {
579
705
  const layer = args.layer
580
706
  const date = today()
581
707
  const title = args.title.trim()
708
+ const expires = normalizeExpiry(args.expires) || detectExpiry(`${title}\n${body}`)
582
709
  const entry = {
583
710
  id: entryId(layer, title, date), layer, title, date, session: st.id,
584
711
  cues: splitCues(args.cues || '').slice(0, 12),
585
712
  related: splitCues(args.related || ''),
586
713
  body,
714
+ expires,
587
715
  }
716
+ if (layer === 'self') assertSelfBudget(opts.repoPath, entry)
588
717
  const rel = appendEntry(opts.repoPath, entry)
589
718
  st.index = buildIndex(readRepo(opts.repoPath).filter((e) => e.layer !== 'self'))
590
719
  st.writes += 1
@@ -614,10 +743,12 @@ export function apply(ctx, config = {}) {
614
743
  const file = join(dir, 'pending', `${String(args?.id || '').replace(/[^\w.-]/g, '')}.json`)
615
744
  if (!existsSync(file)) throw new Error(`找不到提案 ${args?.id}`)
616
745
  const p = JSON.parse(readFileSync(file, 'utf8'))
617
- appendEntry(opts.repoPath, {
746
+ const entry = {
618
747
  id: entryId('self', p.title, p.date), layer: 'self', title: p.title, date: p.date,
619
748
  session: p.session, cues: p.cues, related: [], body: p.body,
620
- })
749
+ }
750
+ assertSelfBudget(opts.repoPath, entry)
751
+ appendEntry(opts.repoPath, entry)
621
752
  rmSync(file, { force: true })
622
753
  st.index = buildIndex(readRepo(opts.repoPath).filter((e) => e.layer !== 'self'))
623
754
  st.writes += 1
@@ -652,7 +783,7 @@ export function apply(ctx, config = {}) {
652
783
 
653
784
  ctx.tools.register({
654
785
  name: 'memory_recall',
655
- description: '按关键词读记忆全文(只读)。注入被截断时用它。',
786
+ description: '按关键词读记忆全文(只读)。用户提到以前的工作、其他窗口、「上次」「之前」时,先调用它。自动注入默认关闭,所以它是记忆的主要入口。',
656
787
  parameters: {
657
788
  type: 'object',
658
789
  additionalProperties: false,
@@ -665,7 +796,7 @@ export function apply(ctx, config = {}) {
665
796
  async execute(args, exec) {
666
797
  if (typeof args?.query !== 'string' || !args.query.trim()) throw new Error('query 不能为空')
667
798
  const st = await ensureSession(exec.agent)
668
- const hits = recallAll(st.index, args.query, 5, 0.2, 1)
799
+ const hits = recallAll(st.index, args.query, 5, 0.2, 1, { split: true, hop: true })
669
800
  if (hits.length === 0) return '记忆仓里没有匹配的条目。'
670
801
  return hits.map((h) => `[${h.entry.layer}] ${h.entry.title}(${h.entry.date || '无日期'})\n${h.entry.body}`).join('\n\n')
671
802
  },
@@ -684,3 +815,4 @@ export function apply(ctx, config = {}) {
684
815
  }
685
816
 
686
817
  export { formatEntry, slug, splitCues }
818
+
package/locale/en.json CHANGED
@@ -1,6 +1,6 @@
1
- {
2
- "meta": {
3
- "title": "Plain Memory",
4
- "description": "Markdown memory in your own Git repo, with per-window state cards and local cue-based recall."
5
- }
6
- }
1
+ {
2
+ "meta": {
3
+ "title": "Plain Memory",
4
+ "description": "Markdown memory in your own Git repo, with per-window state cards and local cue-based recall."
5
+ }
6
+ }
package/locale/zh.json CHANGED
@@ -1,6 +1,6 @@
1
- {
2
- "meta": {
3
- "title": "原味记忆",
4
- "description": "记忆是纯 Markdown,存在你自己的 Git 仓库;多窗口状态卡;线索词本地召回。"
5
- }
6
- }
1
+ {
2
+ "meta": {
3
+ "title": "原味记忆",
4
+ "description": "记忆是纯 Markdown,存在你自己的 Git 仓库;多窗口状态卡;线索词本地召回。"
5
+ }
6
+ }