sage-mem 2.17.0 → 2.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.
- package/README.en.md +20 -2
- package/README.md +20 -2
- package/lib/autodream/llm-loop.js +19 -0
- package/lib/autodream/prompt.js +32 -2
- package/lib/autodream/reconcile.js +145 -0
- package/lib/client.js +2 -2
- package/lib/index.js +11 -132
- package/lib/settings.js +175 -4
- package/lib/typert/schemas.js +670 -0
- package/lib/typert.host.js +15 -652
- package/package.json +1 -20
package/README.en.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
**Cross-session memory for DeepSeek Harness — plain markdown files, no database, no worker.**
|
|
14
14
|
|
|
15
|
-
**Current release: `2.
|
|
15
|
+
**Current release: `2.18.0`** — the npm badge above is the dynamic source of truth. **Contract baseline: `1.0.0`** — the first stable release, whose public commitments, the capability boundaries and the list of things it explicitly cannot do are frozen in the
|
|
16
16
|
[frozen contract](https://github.com/gezi-wen/sage-mem/blob/main/docs/1.0.0-contract.md).
|
|
17
17
|
|
|
18
18
|
If it is useful to you, a Star is the most direct feedback.
|
|
@@ -409,6 +409,24 @@ The optional memory-consolidation feature (identifier `autodream`): it reads thr
|
|
|
409
409
|
|
|
410
410
|
Automatic triggering runs on the plugin's own timer (checks the gates every 30 minutes by default). Consolidation **deliberately does not run through your session** — the tool set is given by the plugin, the step count is capped, and it is not written into your session records.
|
|
411
411
|
|
|
412
|
+
### Reconciliation: checking old memories against sessions (2.18.0)
|
|
413
|
+
|
|
414
|
+
When the input source is "memory + session records", consolidation first picks at most 5 memories **most likely to disagree with the present**. Two criteria must both hold:
|
|
415
|
+
|
|
416
|
+
- **Still being injected recently** (it appeared in your context within the last 7 days) — so it is in service, not a dead file;
|
|
417
|
+
- **But the content has not changed for a long time** (the file has not been written for 14+ days) — so the wording may be stale.
|
|
418
|
+
|
|
419
|
+
"Still in use + long unchanged" is the dangerous combination: new facts cannot get in, while the old statement keeps showing up in every injection. For those 5, the model uses session records to check whether what they say still lines up:
|
|
420
|
+
|
|
421
|
+
- **It lines up** → do nothing. **All five passing with not a single edit is a normal outcome of that run.**
|
|
422
|
+
- **It does not line up** → fix the old wording at its source (not by appending "actually it is not like that")
|
|
423
|
+
- **The whole entry is obsolete** → archiving is an option, but a reason is required
|
|
424
|
+
- **It is merely missing new progress** → merge into the existing file, do not open a new one
|
|
425
|
+
|
|
426
|
+
So it may occasionally touch an entry you never asked it to change — **that is reconciliation, not random editing**. This is also the first time the feature can notice "this was recorded wrong / has gone stale"; before this it could only notice "something is missing".
|
|
427
|
+
|
|
428
|
+
**With "memory directory only" this whole stage disappears** (there are no sessions to check against, and a list without sessions would only make it guess). The list itself carries only file names and day counts, never the body text.
|
|
429
|
+
|
|
412
430
|
### The Consolidation Model Can Be Specified Separately
|
|
413
431
|
|
|
414
432
|
Consolidation is a background batch job and need not be the same model as the one used for chatting. The settings page under 「高级设置 → 模型」(Advanced settings → Model) lets you pick one from **the model routes currently configured** (the options come from the same catalog as in 「模型选择」/ model selection).
|
|
@@ -623,7 +641,7 @@ sage-mem is a pure DSH plugin and declares its host requirement through **`engin
|
|
|
623
641
|
|
|
624
642
|
**Verified on DSH Go (the Android client).** After installing from the in-app plugin market, the settings section and sidebar panel render normally and the `sidebar.panellist` island opens; the memory directory keeps sage-mem's default `~/.sage-mem/memory`, no extra adaptation needed. 2.1.1 fixes the invisible primary-button label on the phone host (foreground token fallback).
|
|
625
643
|
|
|
626
|
-
Version-by-version compatibility record:
|
|
644
|
+
Version-by-version compatibility record (**historical list, not a declaration** — since 2.17.1 `package.json` no longer carries `dsh.compatibility`: hosts after 0.2.1-alpha.4 only read `engines.dsh`, so a row in this table cannot make the plugin compatible; adding one only makes people believe "one row = one verified run"):
|
|
627
645
|
|
|
628
646
|
| DSH version | Status |
|
|
629
647
|
| --- | --- |
|
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
> File-based cross-session memory for DeepSeek Harness — every memory is a plain markdown file you can open, edit and delete. No database, no worker, no port.
|
|
16
16
|
|
|
17
|
-
**当前发布版本:`2.
|
|
17
|
+
**当前发布版本:`2.18.0`** —— 顶部 npm 徽章是动态权威。**契约基线:`1.0.0`** —— 第一个稳定版,公开承诺、能力边界与「明确做不到」的清单冻结在
|
|
18
18
|
[契约冻结](https://github.com/gezi-wen/sage-mem/blob/main/docs/1.0.0-contract.md)。
|
|
19
19
|
|
|
20
20
|
如果它对你有用,给个 Star 是最直接的反馈。
|
|
@@ -411,6 +411,24 @@ sage-mem 给记忆目录的写入加了**版本守卫**:写入前记下当前
|
|
|
411
411
|
|
|
412
412
|
自动触发走插件自己的定时器(默认 30 分钟查一次门控)。整理**刻意不走会话** —— 工具集由插件给、步数有上限、不写进你的会话记录。
|
|
413
413
|
|
|
414
|
+
### 对账:拿会话核对旧记忆(2.18.0)
|
|
415
|
+
|
|
416
|
+
输入源选了「记忆 + 会话记录」时,整理前会先挑出最多 5 条**最可能与现状不符**的记忆。判据是两条同时成立:
|
|
417
|
+
|
|
418
|
+
- **最近还被注入过**(最近 7 天里出现过在你的上下文里)—— 说明它仍在服役,不是死文件;
|
|
419
|
+
- **但内容很久没动**(文件 14 天以上没写过)—— 说明这段话可能就是旧的。
|
|
420
|
+
|
|
421
|
+
「还在用 + 很久没改」才是最危险的组合:新事实进不来,旧说法还在每次注入里出现。对这 5 条,模型拿会话记录去核对「它写的还对不对得上」:
|
|
422
|
+
|
|
423
|
+
- **对得上** → 什么都不做。**全部通过、一个字都不改,是这一趟的正常结果。**
|
|
424
|
+
- **对不上** → 在源头改掉旧说法(不是后面追加一句「其实不是这样」)
|
|
425
|
+
- **整条过时** → 可以考虑归档,必须带缘由
|
|
426
|
+
- **只是缺了新进展** → 合并进已有文件,不新开一条
|
|
427
|
+
|
|
428
|
+
所以它偶尔会动到你没明说要改的条目 —— **那是对账,不是乱动**。这也是本功能第一次具备「发现记错了 / 过时了」的能力;在此之前它只能发现「漏了什么」。
|
|
429
|
+
|
|
430
|
+
**选「仅记忆目录」时这一段完全不出现**(没有会话可核对,名单给出来只会让它硬猜)。名单本身只有文件名与天数,不含正文。
|
|
431
|
+
|
|
414
432
|
### 整理用的模型可以单独指定
|
|
415
433
|
|
|
416
434
|
整理是后台批量任务,跟聊天用的不一定是同一个模型。设置页「高级设置 → 模型」可以从**当前已配置的模型路线**里挑一条(选项与「模型选择」里的目录同源)。
|
|
@@ -627,7 +645,7 @@ sage-mem 是纯 DSH 插件,用 `package.json` 的 **`engines.dsh`** 声明宿
|
|
|
627
645
|
|
|
628
646
|
**已在 DSH Go(Android 客户端)上验证可用**:从 App 内插件市场安装后,设置分区与侧栏面板正常、`sidebar.panellist` 岛屿可打开;记忆目录用 `SAGE_MEM_DIR` 默认的 `~/.sage-mem/memory` 即可,不需要额外适配。2.1.1 修掉了手机宿主上主按钮文字不可见的问题(前景令牌兜底)。
|
|
629
647
|
|
|
630
|
-
|
|
648
|
+
逐版本兼容记录(**历史清单,不是声明** —— 2.17.1 起 `package.json` 不再带 `dsh.compatibility`:0.2.1-alpha.4 之后的宿主只看 `engines.dsh`,那张表写了也不会让插件真的 compatible,补进去只会让人以为「补一行 = 验过一次」):
|
|
631
649
|
|
|
632
650
|
| DSH 版本 | 状态 |
|
|
633
651
|
| --- | --- |
|
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { buildAutodreamSystemPrompt, buildAutodreamUserPrompt } from './prompt.js'
|
|
12
|
+
import { buildReconcileList } from './reconcile.js'
|
|
13
|
+
import { readAccessLedger, ledgerFileFor } from '../memory/access.js'
|
|
14
|
+
import { scanMemoryDir } from '../memory/scan.js'
|
|
12
15
|
import { randomUUID } from 'node:crypto'
|
|
13
16
|
|
|
14
17
|
export class BlockAssembler {
|
|
@@ -214,12 +217,28 @@ export async function agentLoop(cfg, runOpts) {
|
|
|
214
217
|
withSessions: cfg.source === 'memory+sessions',
|
|
215
218
|
reservedFiles: this.reservedFiles,
|
|
216
219
|
})
|
|
220
|
+
/**
|
|
221
|
+
* 2.18.0「对账」:只在**会话记录也在输入源里**时,挑几条「还在被注入、内容却很久没动」
|
|
222
|
+
* 的记忆,让模型拿会话去核对它们。这是「查字典」之外的新切口 —— 字典补漏,对账纠错。
|
|
223
|
+
*
|
|
224
|
+
* 「什么时候算、算不出来怎么办」都在 `buildReconcileList` 里(含静默降级),
|
|
225
|
+
* 这里只负责把真实 IO 递进去:扫目录拿 mtime,读 access 台账拿「最近被注入过没有」。
|
|
226
|
+
*/
|
|
227
|
+
const reconcileList = await buildReconcileList({
|
|
228
|
+
source: cfg.source,
|
|
229
|
+
// 粗细刚好:这里只需要 mtime 做粗筛,正文一个字都不读(扫描层有目录缓存)。
|
|
230
|
+
listEntries: async () =>
|
|
231
|
+
(await scanMemoryDir(this.ctx, '', this.memoryDir)).map((f) => ({ file: f.file, mtime: f.mtimeMs })),
|
|
232
|
+
readLedger: readAccessLedger,
|
|
233
|
+
ledgerFile: ledgerFileFor(this.memoryDir),
|
|
234
|
+
})
|
|
217
235
|
const userText = buildAutodreamUserPrompt({
|
|
218
236
|
memoryDir: this.memoryDir,
|
|
219
237
|
apply: cfg.apply,
|
|
220
238
|
hoursSince: runOpts.hoursSince ?? 0,
|
|
221
239
|
sessionCount: runOpts.sessionCount ?? 0,
|
|
222
240
|
now: Date.now(),
|
|
241
|
+
reconcileList,
|
|
223
242
|
})
|
|
224
243
|
const messages = [userMessage(userText)]
|
|
225
244
|
|
package/lib/autodream/prompt.js
CHANGED
|
@@ -79,7 +79,37 @@ ${reservedNote}
|
|
|
79
79
|
* @returns {string}
|
|
80
80
|
*/
|
|
81
81
|
export function buildAutodreamUserPrompt(opts) {
|
|
82
|
-
const { memoryDir, apply, hoursSince, sessionCount, now } = opts
|
|
82
|
+
const { memoryDir, apply, hoursSince, sessionCount, now, reconcileList } = opts
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 阶段 2.5(2.18.0「对账」)。只在**有会话输入源且确实挑出了候选**时出现。
|
|
86
|
+
*
|
|
87
|
+
* 为什么单开一段而不是塞进阶段 2:阶段 2 说的是「收集新东西」,这一段说的是
|
|
88
|
+
* 「核对旧东西还对不对」。两者目标相反 —— 前者要多记,后者要少错。混在一起,
|
|
89
|
+
* 模型会把它读成「又一轮收集」,于是对着每条候选硬凑一句改动。
|
|
90
|
+
*
|
|
91
|
+
* ⚠️ 末句「一条都没问题也可以」是必需的:不给退路,模型会为了有产出而去改
|
|
92
|
+
* 本来没错的记忆,那就是把「对账」做成了「制造 Git 噪音」。
|
|
93
|
+
*/
|
|
94
|
+
const stage25 = reconcileList
|
|
95
|
+
? `
|
|
96
|
+
## 阶段 2.5 — 对账(只核对下面这几条)
|
|
97
|
+
|
|
98
|
+
下面这些条目**最近还在被注入,但内容很久没动过**。它们不代表写错了,只是最可能与现状不符:
|
|
99
|
+
|
|
100
|
+
${reconcileList}
|
|
101
|
+
|
|
102
|
+
对每一条,用 \`search_sessions\` 搜一两个来自它的关键词,看最近实际发生的事跟它写的**还对不对得上**:
|
|
103
|
+
|
|
104
|
+
- **对得上** → 什么都不要做(通过,别为了「有产出」去改它)
|
|
105
|
+
- **对不上** → 在**源头**改掉旧说法(阶段 3 的规则同样适用:改在源头,不在后面追加一句「其实不是这样」)
|
|
106
|
+
- **整条过时** → 可以考虑归档,但必须带 \`reason\`
|
|
107
|
+
- **只是缺了新进展** → 合并进已有文件,不要新开一条
|
|
108
|
+
|
|
109
|
+
⚠️ 这一段的目标是**减少错记忆**,不是增加记忆条数。全部核对通过、一个字都不改,也是这一趟的正常结果。
|
|
110
|
+
`
|
|
111
|
+
: ''
|
|
112
|
+
|
|
83
113
|
const stage4 = apply
|
|
84
114
|
? `- 更新 \`MEMORY.md\` 索引,让它保持是索引而不是内容堆:
|
|
85
115
|
- 每条一行 \`- [标题](文件.md) — 一句话\`
|
|
@@ -125,7 +155,7 @@ export function buildAutodreamUserPrompt(opts) {
|
|
|
125
155
|
1. **结构性毛病** —— 直接跑 \`audit_memory\`,它会把索引悬空、漏索引、type 与前缀不一致、双链断链、YAML 陷阱都列出来。这些是确定要做的事。
|
|
126
156
|
2. **漂移的旧记忆** —— 描述与内容已经对不上的、日期还是相对写法的、被后续进展推翻的。
|
|
127
157
|
3. **(仅在需要具体细节时)** 用 \`search_sessions\` 定向搜一个具体词。不要通读会话记录。
|
|
128
|
-
|
|
158
|
+
${stage25}
|
|
129
159
|
## 阶段 3 — 整合
|
|
130
160
|
|
|
131
161
|
对每件值得记住的事,改写或新建一条记忆。**合并新信息进已有文件**,不要为一点新东西新开一个文件。被推翻的事实改在源头,而不是在后面追加一句「其实不是这样」。
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「对账候选」的挑选(2.18.0)。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么要有它
|
|
5
|
+
*
|
|
6
|
+
* 自动做梦原先拿会话记录的方式是**查字典**(`prompt.js` 里那句「用 `search_sessions`
|
|
7
|
+
* 定向搜一个具体词,不要通读会话记录」)。查字典能补漏,但发现不了「记错了 / 过时了」——
|
|
8
|
+
* 而那恰恰是 Anthropic 的 Dreams 在做的事(把 session transcripts 当输入,跟记忆逐条对)。
|
|
9
|
+
*
|
|
10
|
+
* 我们不通读会话是有意的取舍(token 与隐私)。所以这里换个更省的切口:
|
|
11
|
+
* **不让模型通读,而是先给它一张「最可能与现状不符」的短名单**,让它只对这几条去核对。
|
|
12
|
+
*
|
|
13
|
+
* ## 候选判据(两个条件同时成立)
|
|
14
|
+
*
|
|
15
|
+
* 1. **最近还被注入过**(`injected` 在 N 天内)—— 说明它仍在服役,不是死文件;
|
|
16
|
+
* 2. **但内容很久没动过**(文件 mtime 早于 M 天)—— 说明这段话很可能是旧的。
|
|
17
|
+
*
|
|
18
|
+
* 「还在用 + 很久没改」= 最危险的组合:新的事实进不来,旧的说法还在每次注入里出现。
|
|
19
|
+
*
|
|
20
|
+
* 为什么用 mtime 而不读 frontmatter 的 `updated`:这里只需要粗筛,读 129 个文件的
|
|
21
|
+
* frontmatter 要解析 YAML,而 mtime 一次 stat 就够。**粗筛宁宽勿严** —— 名单是给模型
|
|
22
|
+
* 参考的,它自己会判断要不要真去改。
|
|
23
|
+
*
|
|
24
|
+
* ## 为什么不用语义
|
|
25
|
+
*
|
|
26
|
+
* 与整个插件一致:这一层只做「哪几条值得看一眼」的排序,不做「哪条错了」的判断。
|
|
27
|
+
* 判断留给模型(它在做梦时本来就有 tool 和上下文)。
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** 候选条数的默认上限。给多了模型读不完,给少了覆盖不到。 */
|
|
31
|
+
export const DEFAULT_RECONCILE_LIMIT = 5
|
|
32
|
+
|
|
33
|
+
/** 「最近还被注入过」的窗口(天)。 */
|
|
34
|
+
export const DEFAULT_FRESH_DAYS = 7
|
|
35
|
+
|
|
36
|
+
/** 「内容很久没动」的门槛(天)。 */
|
|
37
|
+
export const DEFAULT_STALE_DAYS = 14
|
|
38
|
+
|
|
39
|
+
const day = 86400000
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 纯函数:从「文件 × 台账」里挑对账候选。
|
|
43
|
+
*
|
|
44
|
+
* @param {object} input
|
|
45
|
+
* @param {Array<{file: string, mtime: number, bytes?: number}>} input.entries — 记忆目录里的条目
|
|
46
|
+
* @param {Record<string, {injected?: number, read?: number}>} input.ledger — access 台账
|
|
47
|
+
* @param {number} [input.limit]
|
|
48
|
+
* @param {number} [input.freshDays]
|
|
49
|
+
* @param {number} [input.staleDays]
|
|
50
|
+
* @param {number} [input.now]
|
|
51
|
+
* @returns {Array<{file: string, idleDays: number, injectedDays: number}>} 按「最久没改」优先
|
|
52
|
+
*/
|
|
53
|
+
export function pickReconcileCandidates({
|
|
54
|
+
entries,
|
|
55
|
+
ledger,
|
|
56
|
+
limit = DEFAULT_RECONCILE_LIMIT,
|
|
57
|
+
freshDays = DEFAULT_FRESH_DAYS,
|
|
58
|
+
staleDays = DEFAULT_STALE_DAYS,
|
|
59
|
+
now = Date.now(),
|
|
60
|
+
}) {
|
|
61
|
+
if (!Array.isArray(entries) || entries.length === 0) return []
|
|
62
|
+
const rows = []
|
|
63
|
+
for (const e of entries) {
|
|
64
|
+
if (!e || typeof e.file !== 'string') continue
|
|
65
|
+
const rec = ledger?.[e.file] ?? ledger?.[e.file.toLowerCase()]
|
|
66
|
+
const injected = typeof rec?.injected === 'number' ? rec.injected : 0
|
|
67
|
+
// 条件一:最近还被注入过(0 = 从没注入过,不算候选 —— 那种不叫「过时」,叫「没用上」)
|
|
68
|
+
if (injected <= 0) continue
|
|
69
|
+
const injectedDays = (now - injected) / day
|
|
70
|
+
if (injectedDays > freshDays) continue
|
|
71
|
+
// 条件二:内容很久没动
|
|
72
|
+
const mtime = typeof e.mtime === 'number' ? e.mtime : 0
|
|
73
|
+
if (mtime <= 0) continue
|
|
74
|
+
const idleDays = (now - mtime) / day
|
|
75
|
+
if (idleDays < staleDays) continue
|
|
76
|
+
rows.push({ file: e.file, idleDays: Math.floor(idleDays), injectedDays: Math.floor(injectedDays) })
|
|
77
|
+
}
|
|
78
|
+
// 「还在服役」这一点越强(注入越近)、「内容越旧」,越该先对账。
|
|
79
|
+
rows.sort((a, b) => (a.injectedDays - b.injectedDays) || (b.idleDays - a.idleDays))
|
|
80
|
+
return rows.slice(0, Math.max(0, limit))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 带上 IO 的那层:读目录 + 读台账,交给纯函数挑。
|
|
85
|
+
*
|
|
86
|
+
* @param {object} input
|
|
87
|
+
* @param {string} input.memoryDir
|
|
88
|
+
* @param {(dir: string) => Promise<Array<{file: string, mtime: number, bytes?: number}>>} input.listEntries — 由调用方注入,避免本文件依赖 scan
|
|
89
|
+
* @param {(ledgerFile: string) => Promise<object>} input.readLedger
|
|
90
|
+
* @param {string} input.ledgerFile
|
|
91
|
+
* @param {number} [input.now]
|
|
92
|
+
* @param {number} [input.limit]
|
|
93
|
+
* @returns {Promise<Array<{file: string, idleDays: number, injectedDays: number}>>}
|
|
94
|
+
*/
|
|
95
|
+
export async function collectReconcileCandidates({ listEntries, readLedger, ledgerFile, now = Date.now(), limit }) {
|
|
96
|
+
const [entries, ledger] = await Promise.all([listEntries(), readLedger(ledgerFile)])
|
|
97
|
+
return pickReconcileCandidates({ entries, ledger, limit, now })
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 把候选渲染成 prompt 里那一段。
|
|
102
|
+
*
|
|
103
|
+
* 为什么渲染在 prompt 侧而不是这里拼好整段:这一段要跟「阶段 2.5」的标题与指令一起出现,
|
|
104
|
+
* 分开写会让两处措辞漂移。这里只负责**列表本身**,措辞归 `prompt.js`。
|
|
105
|
+
*
|
|
106
|
+
* @param {Array<{file: string, idleDays: number, injectedDays: number}>} rows
|
|
107
|
+
* @returns {string} 每行一条;空数组返回空串(调用方据此整段不出现)
|
|
108
|
+
*/
|
|
109
|
+
export function renderReconcileList(rows) {
|
|
110
|
+
if (!Array.isArray(rows) || rows.length === 0) return ''
|
|
111
|
+
return rows
|
|
112
|
+
.map((r) => `- \`${r.file}\` —— 最近 ${r.injectedDays} 天内还在被注入,但内容已经 ${r.idleDays} 天没动过`)
|
|
113
|
+
.join('\n')
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** 只有会话记录也在输入源里时才有「拿会话去核对」这回事。 */
|
|
117
|
+
export const RECONCILE_SOURCE = 'memory+sessions'
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 引擎实际调用的那一层:**按输入源开关 + 失败降级**都收在这里,好被测试直接钉住。
|
|
121
|
+
*
|
|
122
|
+
* 为什么把「什么时候算、算不出来怎么办」也放本文件:这两条是这一版唯一有行为风险的
|
|
123
|
+
* 决定(错了要么拖垮整趟做梦,要么在「仅记忆目录」时凭空多出一段)。留在引擎的方法体里
|
|
124
|
+
* 就只能靠读源码断言,放到这里可以直接喂假 IO 跑。
|
|
125
|
+
*
|
|
126
|
+
* ⚠️ 降级是**静默**的:对账是附加价值,它挂了不该让整趟做梦失败,也不该往报告里塞错误。
|
|
127
|
+
*
|
|
128
|
+
* @param {object} input
|
|
129
|
+
* @param {string} input.source — 本次输入源
|
|
130
|
+
* @param {() => Promise<Array<{file: string, mtime: number}>>} input.listEntries
|
|
131
|
+
* @param {(file: string) => Promise<object>} input.readLedger
|
|
132
|
+
* @param {string} input.ledgerFile
|
|
133
|
+
* @param {number} [input.limit]
|
|
134
|
+
* @param {number} [input.now]
|
|
135
|
+
* @returns {Promise<string>} 列表文本;不该有或算不出来时是空串
|
|
136
|
+
*/
|
|
137
|
+
export async function buildReconcileList({ source, listEntries, readLedger, ledgerFile, limit, now = Date.now() }) {
|
|
138
|
+
if (source !== RECONCILE_SOURCE) return ''
|
|
139
|
+
try {
|
|
140
|
+
const rows = await collectReconcileCandidates({ listEntries, readLedger, ledgerFile, limit, now })
|
|
141
|
+
return renderReconcileList(rows)
|
|
142
|
+
} catch {
|
|
143
|
+
return ''
|
|
144
|
+
}
|
|
145
|
+
}
|
package/lib/client.js
CHANGED
|
@@ -4575,7 +4575,7 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
4575
4575
|
"ad.row.source": "输入源",
|
|
4576
4576
|
"ad.source.memory": "仅记忆目录",
|
|
4577
4577
|
"ad.source.sessions": "记忆 + 会话记录",
|
|
4578
|
-
"ad.note.sourceSessions": "会解压会话记录做定向搜索 —— 私密对话会进入模型上下文,token
|
|
4578
|
+
"ad.note.sourceSessions": "会解压会话记录做定向搜索 —— 私密对话会进入模型上下文,token 成本也更高。整理前还会挑出几条「还在被注入、但很久没改」的记忆去核对(最多 5 条):所以它偶尔会动到你没明说要改的条目,那是对账,不是乱动。",
|
|
4579
4579
|
"ad.note.sourceMemory": "只看记忆文件本身,不碰会话记录。",
|
|
4580
4580
|
"ad.note.applySnapshot": "改写前会自动把整个记忆目录快照一份(memory 上一层 .sage-mem/autodream/snapshots/),改坏可回退。",
|
|
4581
4581
|
"ad.note.reportOnly": "只读模式:这一趟连写工具都不会挂给模型,所以它不可能改到任何文件。",
|
|
@@ -5242,7 +5242,7 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
5242
5242
|
"ad.row.source": "Input",
|
|
5243
5243
|
"ad.source.memory": "Memory directory only",
|
|
5244
5244
|
"ad.source.sessions": "Memory + session logs",
|
|
5245
|
-
"ad.note.sourceSessions": "Session logs are unpacked for a targeted search — private conversations enter the model context, and token cost is higher.",
|
|
5245
|
+
"ad.note.sourceSessions": "Session logs are unpacked for a targeted search — private conversations enter the model context, and token cost is higher. Before consolidating it also picks a few memories that are still being injected but haven't changed for a long time and checks them against recent sessions (at most 5) — so it may occasionally touch an entry you never asked to change. That is reconciliation, not random editing.",
|
|
5246
5246
|
"ad.note.sourceMemory": "Only the memory files themselves; session logs are never touched.",
|
|
5247
5247
|
"ad.note.applySnapshot": "Before rewriting, the whole memory directory is snapshotted (under .sage-mem/autodream/snapshots/, one level above memory), so a bad run can be rolled back.",
|
|
5248
5248
|
"ad.note.reportOnly": "Read-only mode: this run does not even give the model write tools, so it cannot change any file.",
|
package/lib/index.js
CHANGED
|
@@ -26,7 +26,7 @@ import { Remote, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol'
|
|
|
26
26
|
import { mkdir, unlink, writeFile } from 'node:fs/promises'
|
|
27
27
|
import { join } from 'node:path'
|
|
28
28
|
import { checkUpdate as checkUpdateImpl } from './update.js'
|
|
29
|
-
import { autodreamConfigSnapshot, clampInt, MAX_RAW_BYTES, parseExperimentalPatch, parseUiPatch, pendingExperimental, pendingUi, rawName, readReservedFile, readSettingsFileDetailed, RESERVED_FILE, SETTINGS_FILE } from './settings.js'
|
|
29
|
+
import { applySettingsPatch, autodreamConfigSnapshot, clampInt, MAX_RAW_BYTES, parseExperimentalPatch, parseUiPatch, pendingExperimental, pendingUi, rawName, readReservedFile, readSettingsFileDetailed, readSettingsSnapshot, RESERVED_FILE, SETTINGS_FILE } from './settings.js'
|
|
30
30
|
import { createRecallInjector, createSessionInjectState, readRecallDebug } from './recall/index.js'
|
|
31
31
|
import { mountAutodream, mountMemoryTools, mountObserve, mountPreCompact, mountWriteGate } from './assembly/index.js'
|
|
32
32
|
|
|
@@ -415,144 +415,23 @@ export class MemoryGateway extends TypertRemoteService {
|
|
|
415
415
|
}
|
|
416
416
|
|
|
417
417
|
/**
|
|
418
|
-
* 面板上的注入设置(5 项上限 +
|
|
419
|
-
*
|
|
420
|
-
*
|
|
418
|
+
* 面板上的注入设置(5 项上限 + 保护名 + 实验性开关)。
|
|
419
|
+
*
|
|
420
|
+
* ⚠️ 那 5 个上限常量是模块加载时读的:改完要重启 DSH 才生效。快照里同时给出
|
|
421
|
+
* 「已生效 / 待生效 / 要不要重启」,界面才能诚实地说这件事。
|
|
422
|
+
*
|
|
423
|
+
* 2.17.1:实现搬到 `lib/settings.js`(组合根撞 1000 行上限)。搬的理由不只是行数 ——
|
|
424
|
+
* 设置的读与写、合并与校验本来就该住在同一个文件里。
|
|
421
425
|
*/
|
|
422
426
|
async getSettings() {
|
|
423
|
-
|
|
424
|
-
const limits = {
|
|
425
|
-
maxResults: MAX_RESULTS,
|
|
426
|
-
maxChars: MAX_CHARS_PER_FILE,
|
|
427
|
-
maxBaseline: MAX_BASELINE,
|
|
428
|
-
maxSessionBytes: MAX_SESSION_BYTES,
|
|
429
|
-
staleDays: STALE_DAYS,
|
|
430
|
-
}
|
|
431
|
-
// Q04:pending 与**启动解析**共用 `resolveLimits` 这一份判据(持久设置 > 环境变量 > 默认)。
|
|
432
|
-
const pending = resolveLimits(fileRead.data?.limits)
|
|
433
|
-
// 2.17.0:从 `EXPERIMENTAL_SPECS` 派生(原先手写 mmr/todoBoard 两份,加开关就得来改第三次)。
|
|
434
|
-
const experimental = Object.fromEntries(Object.keys(EXPERIMENTAL_SPECS).map((k) => [k, liveExperimental(k)]))
|
|
435
|
-
const pendingLab = pendingExperimental(fileRead.data)
|
|
436
|
-
// 坏设置给明确诊断、不静默回默认:启动期那份 + 这次读文件的问题 + 逐项非法/越界说明,去重后一起给。
|
|
437
|
-
const limitDiagnostics = [...new Set([
|
|
438
|
-
...STARTUP_LIMIT_DIAGNOSTICS,
|
|
439
|
-
...STARTUP_EXPERIMENTAL_DIAGNOSTICS,
|
|
440
|
-
...(fileRead.error ? [fileRead.error] : []),
|
|
441
|
-
...pending.diagnostics,
|
|
442
|
-
])]
|
|
443
|
-
if (limitDiagnostics.length > 0) {
|
|
444
|
-
warnLog(this.ctx, `上限设置有问题(本次仍按环境变量/默认值生效):${limitDiagnostics.join(';')}`)
|
|
445
|
-
}
|
|
446
|
-
const reservedExtra = await readReservedFile()
|
|
447
|
-
// 模块加载时那份「用户声明的保护名」(RESERVED_FILES 里抠掉两个内置名)
|
|
448
|
-
const activeExtra = [...RESERVED_FILES].filter((n) => n !== INDEX_FILE_KEY && n !== SESSION_LOG_KEY)
|
|
449
|
-
const norm = (list) => JSON.stringify([...list].map((s) => String(s).toLowerCase()).sort())
|
|
450
|
-
const restartRequired =
|
|
451
|
-
JSON.stringify(limits) !== JSON.stringify(pending.limits) ||
|
|
452
|
-
// 2.11.0:实验性开关里带 `live: true` 的(如 todoBoard)由 client 半当场应用,
|
|
453
|
-
// 不参与 restartRequired —— 说「要重启」而实际已经生效,比不说更糟。
|
|
454
|
-
Object.keys(pendingLab).some((k) => !EXPERIMENTAL_SPECS[k]?.live && experimental[k] !== pendingLab[k]) ||
|
|
455
|
-
norm(activeExtra) !== norm(reservedExtra)
|
|
456
|
-
// 2.7.0:entry 是 canonical;sidebarEntry 只为 2.6.0 的客户端保留(派生值,不落盘)。
|
|
457
|
-
const ui = pendingUi(fileRead.data)
|
|
458
|
-
return {
|
|
459
|
-
limits,
|
|
460
|
-
pendingLimits: pending.limits,
|
|
461
|
-
// 2.9.0 实验性:`experimental` = 已生效值(模块加载时读的),`pendingExperimental` = 待生效值。
|
|
462
|
-
experimental,
|
|
463
|
-
pendingExperimental: pendingLab,
|
|
464
|
-
restartRequired,
|
|
465
|
-
// Q04 的诊断出口:面板据此说明「你保存的值没生效,原因是 X」;宿主侧先如实给出,别让问题只能靠猜。
|
|
466
|
-
limitDiagnostics,
|
|
467
|
-
// 1.8.0 / 2.7.0:客户端行为(界面入口三选一)。**不参与** restartRequired —— 改完立刻生效。
|
|
468
|
-
ui: { ...ui, sidebarEntry: ui.entry !== 'settings' },
|
|
469
|
-
// 生效的保护名单(含两个内置名)+ 用户声明的那部分原文(面板编辑的是后者)
|
|
470
|
-
reserved: [...new Set([INDEX_FILE_KEY, SESSION_LOG_KEY, ...reservedExtra.map((s) => s.toLowerCase())])],
|
|
471
|
-
reservedExtra,
|
|
472
|
-
}
|
|
427
|
+
return readSettingsSnapshot(this.ctx)
|
|
473
428
|
}
|
|
474
429
|
|
|
475
430
|
/**
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
* 校验**先全部过一遍再写盘**:任何一个值非法就整体 `{ ok:false }`,绝不写半份。
|
|
479
|
-
* 越界不是「夹到边界」(那是环境变量的宽容语义)—— 面板上手输的数字越界是笔误,
|
|
480
|
-
* 静默改成别的值比报错更让人困惑。
|
|
431
|
+
* 改设置:先全验、后一起写,任何一个非法就整份不落盘。实现在 `lib/settings.js`。
|
|
481
432
|
*/
|
|
482
433
|
async setSettings(patch) {
|
|
483
|
-
|
|
484
|
-
const limitsPatch = p.limits && typeof p.limits === 'object' && !Array.isArray(p.limits) ? p.limits : null
|
|
485
|
-
const nextLimits = {}
|
|
486
|
-
if (limitsPatch) {
|
|
487
|
-
for (const [key, value] of Object.entries(limitsPatch)) {
|
|
488
|
-
const spec = LIMIT_SPECS[key]
|
|
489
|
-
if (!spec) return { ok: false, error: `sage-mem: unknown setting: ${key}` }
|
|
490
|
-
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
491
|
-
return { ok: false, error: `sage-mem: ${key} must be a number, got ${typeof value}` }
|
|
492
|
-
}
|
|
493
|
-
const rounded = Math.round(value)
|
|
494
|
-
if (rounded < spec.lo || rounded > spec.hi) {
|
|
495
|
-
return { ok: false, error: `sage-mem: ${key} must be an integer in [${spec.lo}, ${spec.hi}], got ${value}` }
|
|
496
|
-
}
|
|
497
|
-
nextLimits[key] = rounded
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
let nextReserved = null
|
|
501
|
-
if (p.reserved !== undefined) {
|
|
502
|
-
if (!Array.isArray(p.reserved)) return { ok: false, error: 'sage-mem: reserved must be an array of file names' }
|
|
503
|
-
const cleaned = []
|
|
504
|
-
for (const item of p.reserved) {
|
|
505
|
-
const s = String(item ?? '').trim()
|
|
506
|
-
if (!isPlainMdName(s) || s.startsWith('..')) {
|
|
507
|
-
return { ok: false, error: `sage-mem: invalid reserved file name: ${String(item)}` }
|
|
508
|
-
}
|
|
509
|
-
if (!cleaned.includes(s)) cleaned.push(s)
|
|
510
|
-
}
|
|
511
|
-
nextReserved = cleaned
|
|
512
|
-
}
|
|
513
|
-
// 1.8.0:ui 与 limits / reserved 同一套「先全验、后一起写」—— 任何一个非法,整份 patch 不落盘。
|
|
514
|
-
const uiCheck = parseUiPatch(p.ui)
|
|
515
|
-
if (!uiCheck.ok) return { ok: false, error: uiCheck.error }
|
|
516
|
-
const uiPatch = uiCheck.patch
|
|
517
|
-
// 2.9.0:实验性开关与 limits / ui 同一套「先全验、后一起写」。
|
|
518
|
-
const labCheck = parseExperimentalPatch(p.experimental)
|
|
519
|
-
if (!labCheck.ok) return { ok: false, error: labCheck.error }
|
|
520
|
-
const labPatch = labCheck.patch
|
|
521
|
-
if (!limitsPatch && nextReserved === null && uiPatch === null && labPatch === null) return { ok: false, error: 'sage-mem: empty patch' }
|
|
522
|
-
|
|
523
|
-
await mkdir(STATE_ROOT, { recursive: true })
|
|
524
|
-
if (limitsPatch || uiPatch !== null || labPatch !== null) {
|
|
525
|
-
const currentRead = await readSettingsFileDetailed()
|
|
526
|
-
// 坏文件上不写:没有可合并的旧值,整份重写会把用户手改的内容静默吞掉。
|
|
527
|
-
// 明确拒绝、让用户先修文件,比「保存成功但别的设置都没了」诚实。
|
|
528
|
-
if (currentRead.error) {
|
|
529
|
-
return { ok: false, error: `sage-mem: ${currentRead.error};请先修复或删除它再保存(不会静默覆盖)` }
|
|
530
|
-
}
|
|
531
|
-
const current = currentRead.data
|
|
532
|
-
const mergedLimits = { ...(current?.limits ?? {}), ...nextLimits }
|
|
533
|
-
// 实验性:只覆盖这次给出的键,没给就沿用文件里的有效值(缺失 = 全关)。
|
|
534
|
-
const mergedLab = { ...pendingExperimental(current), ...(labPatch ?? {}) }
|
|
535
|
-
// ui 只覆盖这次给出的键;没给就沿用文件里的有效值(坏值 / 缺失一律按默认「两个都显示」)。
|
|
536
|
-
// 2.7.0:落盘只写 canonical 的 entry,旧 sidebarEntry 由 pendingUi 迁移进来、不再写出。
|
|
537
|
-
const mergedUi = { ...pendingUi(current), ...(uiPatch ?? {}) }
|
|
538
|
-
await writeFileAtomic(SETTINGS_FILE, JSON.stringify({ limits: mergedLimits, experimental: mergedLab, ui: mergedUi, updatedAt: Date.now() }, null, 2))
|
|
539
|
-
}
|
|
540
|
-
if (nextReserved !== null) {
|
|
541
|
-
await writeFileAtomic(RESERVED_FILE, `${JSON.stringify(nextReserved, null, 2)}\n`)
|
|
542
|
-
}
|
|
543
|
-
// 2.17.0:live 开关写盘后立刻生效;必须在下面 getSettings 之前(否则返回的已生效值是旧的)。
|
|
544
|
-
if (labPatch !== null) applyRuntimeExperimental(labPatch)
|
|
545
|
-
// 上限 / 保护名单在模块加载时就固化了,写盘即提示重启;ui 是**客户端行为**,改完立刻生效,
|
|
546
|
-
// 所以只有前两者会强制 true,纯 ui 补丁照实回传(通常就是 false)。
|
|
547
|
-
const settings = await this.getSettings()
|
|
548
|
-
// 2.11.0:实验性补丁只在**碰到要重启的键**时才强制 true —— 带 `live: true` 的(todoBoard)
|
|
549
|
-
// 由 client 半当场应用,写盘即生效,不该再吓用户一次。
|
|
550
|
-
const labNeedsRestart = labPatch !== null && Object.keys(labPatch).some((k) => !EXPERIMENTAL_SPECS[k]?.live)
|
|
551
|
-
return {
|
|
552
|
-
ok: true,
|
|
553
|
-
settings,
|
|
554
|
-
restartRequired: limitsPatch || nextReserved !== null || labNeedsRestart ? true : settings.restartRequired,
|
|
555
|
-
}
|
|
434
|
+
return applySettingsPatch(this.ctx, patch)
|
|
556
435
|
}
|
|
557
436
|
|
|
558
437
|
/**
|