dsh-plugin-memory 0.9.3 → 0.10.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/CHANGELOG.md CHANGED
@@ -2,6 +2,45 @@
2
2
 
3
3
  所有记录跟随 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/) 风格;版本号与 `package.json` 保持一致。
4
4
 
5
+ ## [0.10.0] - 2026-10-16
6
+
7
+ ### Added
8
+
9
+ - **`dsh-memory doctor` — 环境体检,一条命令定位"感觉哪里不对"**。检查的是**实际会坏的那些东西**,每条结论后面直接跟修复命令:
10
+ | 检查项 | 说明 |
11
+ |---|---|
12
+ | 目录可写 | 用**真实写入探针**判断(不看 mode 位——ACL 下会骗人) |
13
+ | 脚手架 | `SOUL.md` / `MEMORY.md` / `index.md` / `log.md` / `BOOTSTRAP.md` 是否齐全 |
14
+ | 页面 / 孤儿页 | 存在但没进 `index.md` 的页面,附 `index --write` |
15
+ | index 死链 | `index.md` 指向但不存在的页(**硬失败**) |
16
+ | git 状态 | 是否仓库 / 未提交数 / **领先远端多少提交未 push** |
17
+ | boot 预算 | `SOUL+MEMORY+index` 实际字符数 vs 默认 12000(标注"实际以插件配置为准") |
18
+ | 陈旧度 | 复用 `accessSummary`,与 `status` 同源 |
19
+
20
+ **退出码**:有硬失败 → 1(可当 CI 闸门);只有提醒 → 0。
21
+ - **实测立刻见效**:在真实记忆库上首跑就报出「**领先远端 57 个提交**」——正是下面那条建议所指的问题;同时报出 boot 预算已用到 **10729/12000(89%)**。
22
+
23
+ - **README 增加「快速上手 / Quickstart」**(中英双语),放在 Features 之前:三步(装 → 重启说句话看铸魂 → `doctor` 或设置页确认),不必先啃完 20 条特性。两个 README 的 CLI 章节也补上了 `doctor`。
24
+
25
+ ### Tests
26
+
27
+ - `test/cli.test.mjs` 新增 4 条(先 RED 后实现):报告脚手架/页面数/git 状态;缺文件时报 `✗` 且不崩;有孤儿页时指向修复命令;`doctor` 出现在 help 里。
28
+ - 全量 **181/181 通过**(0.9.4 时 177);`npm test` self-test 通过。
29
+ - 过程中一条测试写错并已修正:`execFileSync` 在非零退出码时抛异常,断言要从 `error.stdout` 取输出——**是测试写法问题,不是 doctor 的缺陷**。
30
+
31
+ ## [0.9.4] - 2026-10-15
32
+
33
+ ### Added(中英双语 README)
34
+
35
+ - **`README.md` 改为英文**,**新增 `README.zh.md` 中文版**,两边标题下互挂语言切换(`**English** | [中文](README.zh.md)` / `**中文** | [English](README.md)`)。
36
+ - 取舍说明:npm 与 GitHub 默认渲染 `README.md`,而 npm 是国际渠道,所以**英文放主位**;中文完整版放 `README.zh.md`,内容与结构逐节对齐(Features / Architecture / Installation / Configuration / First run / Operations / CLI / Migration / FAQ / Development / Roadmap / License),不是摘要。
37
+ - `package.json` 的 `files` 白名单加入 `README.zh.md`,因此 **npm 包内同时带中英两份**(打包预演:`README.md` 24.7 kB + `README.zh.md` 22.4 kB,共 30 files)。
38
+ - 原有的 "English TL;DR" 段落被完整英文版取代。
39
+
40
+ ### Tests
41
+
42
+ - 全量 **177/177 通过**;`npm test`(self-test: init / pack / unpack)通过。本次为纯文档改动,无代码变更。
43
+
5
44
  ## [0.9.3] - 2026-10-08
6
45
 
7
46
  ### Fixed(主动追忆「显示已完成但没有任何内容」)
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # dsh-plugin-memory
2
2
 
3
+ **English** | [中文](README.zh.md)
4
+
3
5
  <p align="center">
4
6
  <a href="https://github.com/LittleBlackTong/dsh-plugin-memory"><img alt="GitHub" src="https://img.shields.io/badge/GitHub-LittleBlackTong%2Fdsh--plugin--memory-blue?logo=github"></a>
5
7
  <a href="https://www.npmjs.com/package/dsh-plugin-memory"><img alt="npm" src="https://img.shields.io/npm/v/dsh-plugin-memory?logo=npm"></a>
@@ -7,145 +9,152 @@
7
9
  <img alt="node" src="https://img.shields.io/badge/node-%3E%3D18-green">
8
10
  </p>
9
11
 
10
- > DeepSeek Harness 长期记忆插件:跨会话、可迁移、带「灵魂」的 markdown 记忆库。
12
+ > A long-term memory plugin for DeepSeek Harness: a cross-session, portable, markdown memory store that comes with a soul.
13
+
14
+ A Cordis plugin for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) that gives agents persistent, cross-session, migratable long-term memory: a markdown + git store (inspired by Karpathy's *LLM Wiki* pattern) with a `SOUL.md` persona file, **auto-injected at every session start** through the system-prompt runtime context, plus remember / recall / consolidate / forget workflows and portable CLI tooling.
15
+
16
+ ## Quickstart
17
+
18
+ Three steps, about five minutes:
19
+
20
+ ```sh
21
+ # 1. install
22
+ dsh plugin --profile <profile> add dsh-plugin-memory
23
+ ```
24
+
25
+ 2. **Restart the app**, open a session and say anything. The boot block loads automatically — and because the store has no soul yet, the agent **starts the soul-forging conversation by itself**: what its name is, how to address you, what you are to each other, what personality it should have. Answer as much or as little as you like; each answer is written back.
26
+
27
+ 3. **Confirm it took.** `dsh-memory doctor` gives an environment check-up (scaffold, orphan pages, git state, boot budget) and ends with the command that fixes each finding. Or open **Settings → Memory** for the health dashboard and hot-editable options.
11
28
 
12
- **English TL;DR** — A Cordis plugin for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) that gives agents a persistent, cross-session, migratable long-term memory: a markdown + git store (inspired by Karpathy's *LLM Wiki* pattern) with a `SOUL.md` persona file, **auto-injected at every session start** via the system-prompt runtime context, plus remember / recall / consolidate / forget workflows and portable CLI tooling.
29
+ After that the loop is simple: the agent writes memories back as you work, and you can steer the store by hand whenever you like — it is plain markdown plus git, nothing locked in.
13
30
 
14
- ## 特性
31
+ ## Features
15
32
 
16
- - **开机强制注入(带路由表保护)**:插件通过 `ctx.systemPrompt.context()` 把记忆 boot 块(`SOUL.md` 人格 + `MEMORY.md` 协议 + `index.md` 目录 + 最近动态)注入会话上下文。宿主按投影去重:记忆不变就不重复注入,变化时新快照自动取代旧的——这是"新会话必先加载记忆"的**硬保障**,不需要模型碰运气调技能。总预算是 `bootMaxChars`,**按文件分配**:每个文件先拿一份均分(受实际大小封顶),剩余额度优先给 `index.md`、再给尚有内容未注入的文件——所以记忆库长大时,目录的尾部不会先被截掉(详见「boot 预算分配」)。默认还带**两道礼貌闸门**:`deferUntilUserSpeaks`(用户开口后才注入)与 `activeSessionOnly`(只注入当前激活会话),见「配置」。
17
- - **last_access 自动戳记**:`MEMORY.md` 的 salience 衰减规则依赖 `last_access`,而它以前纯粹靠自觉——模板里有字段、规则里提到它,但没有任何代码去写它,于是每个页面都原地变老、衰减表形同虚设。现在会话收到第一条真实用户消息时,插件会把 boot 块**实际注入过的页面**(`SOUL.md` / `MEMORY.md` / `index.md` + 目录里被引用到的页)戳成当天,每会话一次、按天幂等,只改 `last_access` 一行;`dsh-memory touch <pages...>` 可手动补记,`trackPageAccess: false` 可关闭。
18
- - **SOUL.md 铸魂**:安装后首要任务是和用户对话定义灵魂(名字、性格、价值观、语气、边界)、确认身份与关系(`BOOTSTRAP.md` 清单驱动,complete 前优先于常规任务)。
19
- - **铸魂自动引导**:记忆库还没有灵魂(`BOOTSTRAP.md` 非 complete,或 `SOUL.md` 仍是占位模板)时,boot 块会自动前置一段第一人称引导词——「我的首要任务是确认我是谁,还有你是谁:我叫什么名字、怎么称呼你、你我是什么关系、我该是什么样的性格」——像 OpenClaw 初始化那样,**由 agent 在对话里主动发起铸魂**,逐项问、逐项写回,而不是等用户来喂。铸魂完成后引导词自动消失,零开销。
20
- - **复利记忆**:遵循 Karpathy 的 *LLM Wiki* 约定——记忆是"一次编译、持续保鲜"的持久产物,不是每次查询重新 RAG。remember / recall / consolidate / forget 四操作 + salience 三级衰减。
21
- - **可迁移**:记忆本体是纯 markdown + git + 自描述 schema,任何能读 markdown 的 agent 都能接手。`dsh-memory pack/unpack` 打包迁移。
22
- - **内嵌技能**:通过 `ctx.skills.register()` 注册 `memory` 技能(操作协议随插件分发);项目级 `.dsh/skills/memory` 文件技能仍可覆盖它。
23
- - **防懒 digest 唤醒**:每轮结束后,若 agent 空闲且记忆库超过 `digestNudgeAfterMinutes` 未写入,插件注入一条 digest 提醒(合成消息,走 `agent.followup`),把"会话收尾沉淀"从靠自觉变成有机制兜底;带冷却与每会话限次,不骚扰。**独立于 dsh-plugin-heartbeat**,两插件各自可装、互不依赖。
24
- - **主动追忆(拟人化)**:对话空下来时,插件会以第一人称主动提起一件**真实记得**的、关于用户或你们之间的事(偏好、往事、未了的决定、最近的进展),把记忆从"只写回"变成"也用起来"——像老友自然想起那样,而非报状态。间隔在最短/最长之间**随机取值**(不固定节奏),配合每会话限次,不骚扰、不编造、不硬聊;纯对话行为,不写记忆库。**同样独立于 heartbeat**。
25
- - **git 自动提交**:记忆库变更静默 `autoCommitQuietSeconds` 后自动 `git add -A && git commit`(无 `.git` 则跳过)——历史可回滚不再依赖 agent 记得 commit。
26
- - **设置面板**:在 DSH 设置页提供「记忆 Memory」区块——总开关、记忆目录、开机注入、技能注册、主动追忆(开关 + 随机间隔范围 + 每会话次数)均可热改,立即生效,无需重启。
27
- - **index 自动整理(声明式编译)**:索引行不再手写。页面在 frontmatter 里声明 `summary:`,`dsh-memory index --write` 把行重写为它的投影(标签 ≤32 / 摘要 ≤48 / 整行 ≤132 字符),**只改行内容,分节与顺序逐字节保留**,且幂等。`index --check` 给机器判定(有漂移退出码非 0),`index --sync-frontmatter` 把已写在 index 里的摘要回填进页面(老库一次性迁移)。digest 提醒会在索引漂移时附一句提示,agent 顺手就能修。
28
- - **互链成为默认动作**:`index.md` 是目录(谁存在),页间互链才是**关系**(谁和谁有关)——实测一个真实记忆库里 29 页只有 1 条真互链,而图谱与跨页综合全都建立在这层关系上。现在三处一起推:`skills/memory.md` 的 remember 流程、`MEMORY.md` 模板的工作流、以及 digest 提醒的收尾指令,都明确要求"给这次碰过的页面各补 1–3 条相关页链接"。
29
- - **补链建议**:`dsh-memory graph --suggest` 列出「只有索引入口、没有任何内容关系」的页面该引用谁——按共享主题 tag、标题词、同类型打分,并说明理由(`共享 #dsh` / `标题词 dsh` / `同类型`)。只报告不写入:是否连、怎么连由 agent 决定。`GET /api/memory/graph?suggest=1` 同源。
30
- - **记忆图谱**:设置页里一张只读关系图 —— 不只是 `index.md` 那种"索引连着所有页"的星形,而是把页面**已经编码但没人画出来**的关系画出来:**索引路由**(`index.md` 指向每一页——这是全库最大的边集,43 条;先前版本跳过了元文件,导致 index 在图上孤立无援)、页间显式 markdown 链接(含相对路径解析)、共享 frontmatter tag(通用容器 tag 如 `project`/`skill` 会被忽略,稠密 tag 走锚点链而非全连接)。坐标由服务端 `lib/graph-layout.js` 一次性算好(确定性、无随机、无依赖),客户端只负责画 SVG。**可交互**:滚轮缩放(以光标为中心,缩放范围 0.4×–2.5×,非 passive 监听所以不会连带滚动设置页)、拖空白处平移、`重置视图` 回到全图;按住节点即可拖走,直接邻居按距离轻微跟随,松手后带缓动滑回原布局——拖动期间暂停过渡做到 1:1 跟手,用 O(邻居数) 的局部松弛而不是逐帧全量力导向(后者在几百页时会卡)。悬停高亮邻里,首次渲染从中心绽开(尊重 `prefers-reduced-motion`)。图谱**每 25 秒自动刷新**(面板开着时记忆变更会自己出现,副标题显示「更新于 HH:MM:SS」);取数失败保留上一张图而不是清空。视图数学(`zoomAt`/`panBy`/`toGraphPoint`)在 `lib/graph-view.js` 里是纯函数并有测试——缩放中心不漂移这件事必须被钉住。`GET /api/memory/graph`(只读、实时,`?types=1` 追加同类型弱边)。三类边在图上可区分:页间互链=实线蓝、索引路由=灰色虚线、共享 tag=细线。
31
- - **`dsh-memory checkup`**:把四个读者(lint 的完整性、status 的计数、看板的体量与新鲜度、graph 的关系)合成**一份带优先级的报告** —— 体量、新鲜度条形图、连接度、一致性检查,最后给出"最该做的 N 件事"。四个数字是诊断,"做这三件事"才是产品。有活干时退出码非 0,可被脚本消费。`--boot=N` 用于按你的 boot 预算计算索引占比。
32
- - **记忆健康看板**:同一区块下方是一张**只读**看板——记忆页数 / index 路由数 / 记忆字数、访问新鲜度条形图(今天 / ≤7 / ≤30 / ≤90 / >90 天)、四项体检结论(index 链接、孤儿页、frontmatter、新鲜度)与健康分、陈旧页候选、最近 5 条动态。数据来自新增的 `GET /api/memory/insights`(每次请求实时统计,不缓存),体检口径与 `dsh-memory lint` 同源,所以看板与 CLI 不会互相打脸。
33
- - **零构建**:纯 ESM JavaScript,无编译步骤,`pnpm add` 即用。
33
+ - **Mandatory boot injection (with routing-table protection)**: the plugin injects a memory boot block (`SOUL.md` persona + `MEMORY.md` protocol + `index.md` directory + recent activity) into the session context via `ctx.systemPrompt.context()`. The host deduplicates by projection: an unchanged memory is injected once, and a changed memory replaces the previous snapshot — this is the **hard guarantee** that a new session always loads memory first, rather than hoping the model calls a skill. The total budget is `bootMaxChars`, **allocated per file**: every file first gets an equal share (capped by its real size), and the remaining budget goes to `index.md` first, then to files that still have content left — so as the store grows, the tail of the directory is not the first thing to be truncated (see "boot budget allocation"). Two polite gates ship enabled by default: `deferUntilUserSpeaks` (inject only after the user speaks) and `activeSessionOnly` (inject only into the currently active session) — see "Configuration".
34
+ - **Automatic `last_access` stamping**: the salience decay rules in `MEMORY.md` depend on `last_access`, which used to be pure self-discipline — the field existed in the template and the rules mentioned it, but no code ever wrote it, so every page silently aged and the decay table was decorative. Now, on the first real user message of a session, the plugin stamps today's date onto the pages the boot block **actually injected** (`SOUL.md` / `MEMORY.md` / `index.md` plus the pages the directory references) — once per session, idempotent per day, touching only the `last_access` line. `dsh-memory touch <pages...>` backfills manually; `trackPageAccess: false` disables it.
35
+ - **SOUL.md soul-forging**: right after installation, the first task is a conversation with the user to define a soul (name, personality, values, tone, boundaries) and to confirm identity and relationship, driven by the `BOOTSTRAP.md` checklist, which takes priority over ordinary tasks until complete.
36
+ - **Automatic soul-forging guidance**: while the store has no soul yet (`BOOTSTRAP.md` not complete, or `SOUL.md` still the placeholder template), the boot block prepends a first-person prompt — "my first job is to establish who I am and who you are: what my name is, what to call you, what we are to each other, and what personality I should have" — so the agent **starts the forging conversation itself**, item by item, writing each answer back, OpenClaw-style, instead of waiting to be fed. The prompt disappears once forging completes, at zero cost.
37
+ - **Compounding memory**: follows Karpathy's *LLM Wiki* convention — memory is a durable artifact that is compiled once and kept fresh, not re-RAG'd per query. Four operations (remember / recall / consolidate / forget) plus three-level salience decay.
38
+ - **Portable**: the store is plain markdown + git + a self-describing schema; any agent that reads markdown can take it over. `dsh-memory pack/unpack` moves it between machines.
39
+ - **Embedded skill**: registers the `memory` skill through `ctx.skills.register()` (the operating protocol ships with the plugin); a project-level `.dsh/skills/memory` file skill can still override it.
40
+ - **Anti-laziness digest nudge**: after a turn ends, if the agent sits idle and the store has not been written for `digestNudgeAfterMinutes`, the plugin injects a digest reminder (a synthetic message through `agent.followup`), turning "settle the session at the end" from self-discipline into a mechanism, with cooldown and a per-session cap so it never nags. **Independent of dsh-plugin-heartbeat** — either plugin can be installed alone.
41
+ - **Proactive recall (personification)**: when the conversation goes quiet, the plugin brings up, in the first person, one thing it **genuinely remembers** about you or the two of you (a preference, a past event, an open decision, recent progress) — turning memory from "write-only" into something actually used, the way an old friend naturally recalls something rather than reporting status. The interval is **randomised** between a minimum and a maximum (no fixed rhythm), paired with a per-session cap: no nagging, no fabrication, no forcing conversation. Purely conversational; it never writes the store. **Also independent of heartbeat**.
42
+ - **Automatic git commits**: after `autoCommitQuietSeconds` of quiet, store changes are silently `git add -A && git commit` (skipped when there is no `.git`) — history becomes rollback-able without depending on the agent remembering to commit.
43
+ - **Settings panel**: a "Memory" section in the DSH settings page — master switch, store directory, boot injection, skill registration, and proactive recall (switch + random interval range + per-session cap) are all hot-editable and take effect immediately, no restart.
44
+ - **Automatic index upkeep (declarative compilation)**: index lines are no longer hand-written. A page declares `summary:` in its frontmatter, and `dsh-memory index --write` rewrites the line as a projection of it (tag ≤32 / summary ≤48 / whole line ≤132 characters), **changing only line content while preserving sections and order byte for byte**, and idempotently. `index --check` gives a machine verdict (non-zero exit when there is drift), and `index --sync-frontmatter` backfills summaries already written in the index into the pages (a one-off migration for older stores). The digest reminder appends a note when the index has drifted, so the agent can fix it in passing.
45
+ - **Cross-linking as a default action**: `index.md` is the directory (who exists); page-to-page links are the **relationships** (who relates to whom). In a real store measured here, 29 pages had only 1 genuine cross-link, yet the graph and all cross-page synthesis rest on that layer. Three places now push it together: the remember flow in `skills/memory.md`, the workflow in the `MEMORY.md` template, and the closing instruction of the digest reminder all require "add 1–3 related-page links for every page you touched this time".
46
+ - **Link suggestions**: `dsh-memory graph --suggest` lists, for pages that have only their index entry and no content relationship, who they ought to reference — scored by shared topic tags, title words, and same type, with the reason stated (`shared #dsh` / `title word dsh` / `same type`). Report-only: whether and how to link stays the agent's call. Also available as `GET /api/memory/graph?suggest=1`.
47
+ - **Memory graph**: a read-only relationship graph in the settings page — not just the `index.md` star (the index is connected to everything), but the relationships pages **already encode and nobody ever drew**: index routing (`index.md` points at every page — the largest edge set in the store, 43 edges; earlier versions skipped meta files, which left the index stranded on the graph), explicit markdown links between pages (with relative path resolution), and shared frontmatter tags (generic container tags like `project`/`skill` are ignored; dense tags go through an anchor chain rather than all-to-all). Coordinates are computed once on the server by `lib/graph-layout.js` (deterministic, no randomness, no dependencies); the client only draws SVG. **Interactive**: wheel zoom (cursor-centred, 0.4×–2.5×, non-passive listener so the settings page does not scroll along), drag empty space to pan, `Reset view` to return to the whole graph; hold a node to drag it, direct neighbours follow slightly by distance, and it eases back to the layout on release — transitions pause during a drag for 1:1 tracking, using an O(neighbours) local relaxation instead of a per-frame full force simulation (which stalls at a few hundred pages). Hover highlights the neighbourhood; the first render blooms out from the centre (respecting `prefers-reduced-motion`). The graph **auto-refreshes every 25 seconds** (changes appear while the panel is open, with "updated at HH:MM:SS" in the subtitle); a failed fetch keeps the previous graph instead of clearing it. The view maths (`zoomAt`/`panBy`/`toGraphPoint`) are pure functions in `lib/graph-view.js` with tests — "the zoom centre must not drift" is pinned down. `GET /api/memory/graph` (read-only, live; `?types=1` adds weak same-type edges). Three edge kinds are distinguishable: page links = solid blue, index routing = grey dashed, shared tags = thin lines.
48
+ - **`dsh-memory checkup`**: folds the four readers (lint's integrity, status's counts, the dashboard's size and freshness, graph's relationships) into **one prioritised report** — size, freshness bars, connectivity, consistency checks, ending with "the N things most worth doing". The four numbers are diagnosis; "do these three things" is the product. Exits non-zero when there is work to do, so scripts can consume it. `--boot=N` computes the index share against your boot budget.
49
+ - **Memory health dashboard**: below the same section is a **read-only** dashboard — memory pages / index routes / word count, an access-freshness bar chart (today / ≤7 / ≤30 / ≤90 / >90 days), four health verdicts (index links, orphan pages, frontmatter, freshness) with a health score, stale-page candidates, and the 5 most recent log entries. Data comes from the new `GET /api/memory/insights` (computed live per request, never cached), and its criteria share a source with `dsh-memory lint`, so the dashboard and the CLI cannot contradict each other.
50
+ - **Zero build**: plain ESM JavaScript, no compilation step, usable straight after `pnpm add`.
34
51
 
35
- ## 架构
52
+ ## Architecture
36
53
 
37
- 插件只拥有**工作流**,不拥有**数据格式**:
54
+ The plugin owns the **workflow**, never the **data format**:
38
55
 
39
56
  ```
40
- dsh-plugin-memory(本插件)
41
- ├── lib/index.js # Cordis 入口:boot 注入 + 运行时技能注册 + settings 热改
42
- ├── lib/boot.js # boot 块渲染(SOUL/MEMORY/index + 最近 log,按文件分配预算)
43
- ├── lib/pages.js # 页面访问记账:extractReferencedPages / touchPages(last_access)
44
- ├── lib/insights.js # 记忆健康看板的数据层(页数/分布/新鲜度/体检,只读)
45
- ├── lib/activity-tracker.js # 两道礼貌闸门:用户是否开口 + 当前激活会话
46
- ├── lib/digest-guard.js # 防懒 digest 唤醒(空闲 + 记忆库久未写 → followup 提醒)
47
- ├── lib/recall-nudge.js # 主动追忆(空闲 → 第一人称提起一件真实往事,纯对话不写库)
48
- ├── lib/scaffold.js # 记忆库脚手架(模板只建不覆盖)
49
- ├── lib/client.js # 客户端半:设置面板「记忆 Memory」区块 + 记忆健康看板
50
- ├── skills/memory.md # 内嵌技能的操作协议正文
51
- └── scripts/memory.mjs # CLI:init/search/touch/lint/status/pack/unpack
52
-
53
- 记忆库(用户数据,默认 ~/.memory)
54
- ├── SOUL.md # 人格与灵魂(用户主导)
55
- ├── BOOTSTRAP.md # 铸魂清单(complete 前优先)
56
- ├── MEMORY.md # schema 与维护协议(自描述)
57
- ├── index.md # 页面目录 log.md # 时间线(append-only)
57
+ dsh-plugin-memory (this plugin)
58
+ ├── lib/index.js # Cordis entry: boot injection + runtime skill registration + settings hot-edits
59
+ ├── lib/boot.js # boot block rendering (SOUL/MEMORY/index + recent log, budgeted per file)
60
+ ├── lib/pages.js # page access accounting: extractReferencedPages / touchPages (last_access)
61
+ ├── lib/insights.js # data layer for the health dashboard (pages/distribution/freshness/checks, read-only)
62
+ ├── lib/activity-tracker.js # the two polite gates: has the user spoken + is this the active session
63
+ ├── lib/digest-guard.js # anti-laziness digest nudge (idle + store unwritten -> followup reminder)
64
+ ├── lib/recall-nudge.js # proactive recall (idle -> one true recollection in the first person, never writes)
65
+ ├── lib/scaffold.js # store scaffolding (templates are created, never overwritten)
66
+ ├── lib/client.js # client half: the "Memory" settings section + health dashboard
67
+ ├── skills/memory.md # the embedded skill's operating protocol
68
+ └── scripts/memory.mjs # CLI: init/search/touch/lint/status/pack/unpack
69
+
70
+ The store (your data, default ~/.memory)
71
+ ├── SOUL.md # persona and soul (user-owned)
72
+ ├── BOOTSTRAP.md # soul-forging checklist (takes priority until complete)
73
+ ├── MEMORY.md # schema and maintenance protocol (self-describing)
74
+ ├── index.md # page directory log.md # timeline (append-only)
58
75
  ├── identity/ user/ skills/ decisions/ projects/{active,archive}/ concepts/
59
- └── raw/ # 不可变源材料
76
+ └── raw/ # immutable source material
60
77
  ```
61
78
 
62
- ## 安装
79
+ ## Installation
63
80
 
64
81
  ```sh
65
82
  dsh plugin --profile <profile> add dsh-plugin-memory
66
83
  ```
67
84
 
68
- (包内置 `dsh.bundle` manifest,`dsh plugin add` 会把它自动挂进 profile 的 bundles 层;dsh-market 里的一键安装同此通道。)
85
+ (The package ships a `dsh.bundle` manifest, so `dsh plugin add` mounts it into the profile's bundles layer automatically; one-click install from dsh-market uses the same channel.)
69
86
 
70
- 重启 profile(DSH Desktop 重启应用)后生效。
87
+ Restart the profile (restart the DSH Desktop app) to take effect.
71
88
 
72
- > 📌 **版本要求:DSH session format v4(官方 DeepSeek Harness 0.1.7+)**
89
+ > 📌 **Version requirement: DSH session format v4 (official DeepSeek Harness 0.1.7+)**
73
90
  >
74
- > 注入消息的 source 使用 producer-owned kind `plugin:memory`。v4 的原生准入明确拒绝 v3 时代的
75
- > `{ kind: 'plugin', plugin: 'memory' }` wrapper,报
76
- > `format v4 message requires a producer-owned source kind`(该错误被包成 `code: "UNKNOWN"`,
77
- > 所以在界面上会显示成 `... source kind UNKNOWN`——`UNKNOWN` 是错误码,不是 kind 值)。
91
+ > Injected messages use the producer-owned source kind `plugin:memory`. v4's native admission explicitly rejects the v3-era `{ kind: 'plugin', plugin: 'memory' }` wrapper with
92
+ > `format v4 message requires a producer-owned source kind` (the error is wrapped as `code: "UNKNOWN"`, so it surfaces in the UI as `... source kind UNKNOWN` — `UNKNOWN` is the error code, not a kind value).
78
93
  >
79
- > v4 与 v3 的白名单**互斥**(v3 只认 `plugin`),因此无法同时兼容两版。
80
- > **0.8.0 及更早版本在 v4 上会触发该错误**;旧版 DSH 请停留在 0.8.0。
94
+ > The v4 and v3 whitelists are **mutually exclusive** (v3 only recognises `plugin`), so one build cannot support both. **0.8.0 and earlier trigger this error on v4**; older DSH should stay on 0.8.0.
81
95
 
82
- > ⚠️ **不要**再往 profile 的 `cordis.patch.yml` 里手写 `- insert: {id: dsh-memory, ...}`:
83
- > 那会与 bundle manifest 的自动挂载产生两条同名 entry,整个 profile 会以
84
- > `duplicate loader entry id "dsh-memory"` 启动失败(2026-08-18 实机事故)。
85
- > 运行期配置(enabled / memoryDir / autoInject / registerSkill / recallEnabled)改走
86
- > `<dshHome>/memory.json`(设置面板热改);composition 配置见下表。
87
- > 如需覆盖某个 composition 键,用**不带 insert 的 id 覆盖条目**(见配置一节)。
96
+ > ⚠️ Do **not** hand-write `- insert: {id: dsh-memory, ...}` into the profile's `cordis.patch.yml`: that produces two entries with the same name alongside the bundle manifest's automatic mount, and the whole profile fails to start with `duplicate loader entry id "dsh-memory"` (a real incident on 2026-08-18). Runtime settings (enabled / memoryDir / autoInject / registerSkill / recallEnabled) go through `<dshHome>/memory.json` (hot-edited from the settings panel); composition settings are in the table below. To override a composition key, use an **id override entry without `insert`** (see Configuration).
88
97
 
89
- ## 配置
98
+ ## Configuration
90
99
 
91
- | 键 | 默认值 | 含义 |
100
+ | Key | Default | Meaning |
92
101
  |---|---|---|
93
- | `enabled` | `true` | 总开关:关闭后不注入 boot 块、不注册 `memory` 技能 |
94
- | `memoryDir` | `~/.memory` | 记忆库绝对路径(`~` 自动展开) |
95
- | `bootFiles` | `[SOUL.md, MEMORY.md, index.md]` | 开机注入的文件 |
96
- | `bootMaxChars` | `12000` | boot 块总字符预算(防止占用过多上下文);按文件分配见下文 |
97
- | `bootFileBudgets` | — | 逐文件字符上限,例如 `{ index.md: 3500 }`;未列出的文件分剩余额度(composition 层,改完需重启) |
98
- | `trackPageAccess` | `true` | 会话首条用户消息时,把 boot 块实际注入的页面戳 `last_access`(关闭后衰减表需手动维护) |
99
- | `autoInject` | `true` | 会话开始时注入 boot 块 |
100
- | `deferUntilUserSpeaks` | `true` | 用户发出第一条真实消息后才注入(boot 块 / 追忆 / digest 提醒都遵守);面板可热改 |
101
- | `activeSessionOnly` | `true` | 只对「当前激活会话」(最近收到用户消息的会话)注入,后台会话不打扰;面板可热改 |
102
- | `registerSkill` | `true` | 注册内嵌 `memory` 技能 |
103
- | `scaffold` | `true` | 记忆库缺失时自动创建模板(只建不覆盖) |
104
- | `configFile` | `<dshHome>/memory.json` | 用户可改配置的 JSON 文件路径(设置面板读写它) |
105
- | `digestNudgeEnabled` | `true` | 防懒 digest 提醒总开关(composition) |
106
- | `digestNudgeAfterMinutes` | `120` | 记忆库超过多久未写入就提醒 |
107
- | `digestNudgeCooldownMinutes` | `180` | 两次提醒的最小间隔 |
108
- | `digestNudgeMaxPerSession` | `2` | 每个会话最多提醒次数 |
109
- | `recallEnabled` | `true` | 主动追忆总开关(面板可热改) |
110
- | `recallIntervalMinMinutes` | `30` | 随机间隔下限(分钟,面板可热改) |
111
- | `recallIntervalMaxMinutes` | `240` | 随机间隔上限(分钟,面板可热改) |
112
- | `recallMaxPerSession` | `3` | 每个会话最多追忆次数(面板可热改) |
113
- | `autoCommit` | `true` | 记忆库 git 自动提交开关(composition) |
114
- | `autoCommitQuietSeconds` | `60` | 变更静默多久后提交(防抖) |
115
- | `autoCommitIntervalSeconds` | `60` | 变更轮询间隔 |
116
-
117
- ### 设置面板(热改)
118
-
119
- `enabled` / `memoryDir` / `autoInject` / `deferUntilUserSpeaks` / `activeSessionOnly` / `registerSkill` / `recallEnabled` / `recallIntervalMinMinutes` / `recallIntervalMaxMinutes` / `recallMaxPerSession` 十项在 DSH 设置页的「记忆 Memory」区块中可改,**即时生效**:boot 注入、两道礼貌闸门、技能注册、主动追忆(含随机间隔与次数)随修改立即生效;记忆目录切换时自动为新目录初始化脚手架(`scaffold: true` 时)。其余键(`bootFiles` / `bootMaxChars` / `bootFileBudgets` / `trackPageAccess` / `scaffold` / `configFile` / `digestNudge*` / `autoCommit*`)只在 composition 配置层生效,改完需重启。
120
-
121
- ### boot 预算分配(为什么 index 不会先被截掉)
122
-
123
- `bootMaxChars` 是总预算,按文件分配而不是简单平摊:
124
-
125
- 1. 有 `bootFileBudgets` 条目的文件先拿自己的额度;
126
- 2. 其余文件各拿一份均分额度,**以文件实际大小封顶**——短文件把用不完的额度退回池子;
127
- 3. 池子再补给「还有内容没注入」的文件(每个最多补到均分额度的两倍):**`index.md` 优先**,其余按体积降序——所以截断落在哪个文件上由体积决定,而不是由 `bootFiles` 的书写顺序决定。
128
-
129
- 当每个文件都小于均分额度时,结果与旧的平摊规则逐字节相同;只有记忆库长大后才会不同。这也意味着:**`index.md` 是路由表,别把它写成摘要表**——它会被完整注入,膨胀的代价是挤掉人格与其他记忆。
130
-
131
- > **「当前激活会话」怎么判?** DSH 宿主侧没有「浏览器当前聚焦的会话」信号(激活会话是前端概念)。插件用**最近一次收到真实用户消息的 live root agent**作为激活会话的代理:你在哪个会话里说话,哪个会话就激活;切到别处但不发消息时,宿主感知不到「切换」这个动作(这是代理的已知边界)。若日后需要精确到「展开/聚焦」级别,需补一小段客户端 focus 上报。
132
-
133
- ### 记忆健康看板
134
-
135
- 设置页「记忆 Memory」区块下方是一张**只读**看板,数据来自 `GET /api/memory/insights`:
136
-
137
- | 展示 | 含义 |
102
+ | `enabled` | `true` | Master switch: when off, no boot injection and no `memory` skill registration |
103
+ | `memoryDir` | `~/.memory` | Absolute path to the store (`~` is expanded) |
104
+ | `bootFiles` | `[SOUL.md, MEMORY.md, index.md]` | Files injected at session start |
105
+ | `bootMaxChars` | `12000` | Total character budget for the boot block (keeps context usage bounded); per-file allocation below |
106
+ | `bootFileBudgets` | — | Per-file character caps, e.g. `{ index.md: 3500 }`; unlisted files share the remainder (composition layer, restart required) |
107
+ | `trackPageAccess` | `true` | On the session's first user message, stamp `last_access` on the pages the boot block actually injected (when off, the decay table needs manual upkeep) |
108
+ | `autoInject` | `true` | Inject the boot block at session start |
109
+ | `deferUntilUserSpeaks` | `true` | Inject only after the user's first real message (the boot block, recall and digest reminders all obey it); hot-editable |
110
+ | `activeSessionOnly` | `true` | Inject only into the "currently active session" (the one that most recently received a user message); background sessions are left alone; hot-editable |
111
+ | `registerSkill` | `true` | Register the embedded `memory` skill |
112
+ | `scaffold` | `true` | Create the store templates when missing (create only, never overwrite) |
113
+ | `configFile` | `<dshHome>/memory.json` | Path to the user-editable JSON config (read/written by the settings panel) |
114
+ | `digestNudgeEnabled` | `true` | Master switch for the anti-laziness digest reminder (composition) |
115
+ | `digestNudgeAfterMinutes` | `120` | How long the store may go unwritten before reminding |
116
+ | `digestNudgeCooldownMinutes` | `180` | Minimum gap between two reminders |
117
+ | `digestNudgeMaxPerSession` | `2` | Maximum reminders per session |
118
+ | `recallEnabled` | `true` | Master switch for proactive recall (hot-editable) |
119
+ | `recallIntervalMinMinutes` | `30` | Lower bound of the random interval (minutes, hot-editable) |
120
+ | `recallIntervalMaxMinutes` | `240` | Upper bound of the random interval (minutes, hot-editable) |
121
+ | `recallMaxPerSession` | `3` | Maximum recalls per session (hot-editable) |
122
+ | `autoCommit` | `true` | Git auto-commit switch for the store (composition) |
123
+ | `autoCommitQuietSeconds` | `60` | How long changes must be quiet before committing (debounce) |
124
+ | `autoCommitIntervalSeconds` | `60` | Change polling interval |
125
+
126
+ ### Settings panel (hot edits)
127
+
128
+ Ten settings are editable in the "Memory" section of the DSH settings page and take effect **immediately**: `enabled` / `memoryDir` / `autoInject` / `deferUntilUserSpeaks` / `activeSessionOnly` / `registerSkill` / `recallEnabled` / `recallIntervalMinMinutes` / `recallIntervalMaxMinutes` / `recallMaxPerSession` — boot injection, the two polite gates, skill registration, and proactive recall (including its random interval and cap) all follow the change at once; switching the store directory scaffolds the new one automatically (when `scaffold: true`). The remaining keys (`bootFiles` / `bootMaxChars` / `bootFileBudgets` / `trackPageAccess` / `scaffold` / `configFile` / `digestNudge*` / `autoCommit*`) only apply at the composition layer and need a restart.
129
+
130
+ ### Boot budget allocation (why the index is not truncated first)
131
+
132
+ `bootMaxChars` is a total budget, allocated per file rather than split evenly:
133
+
134
+ 1. Files with a `bootFileBudgets` entry take their own allowance first;
135
+ 2. the remaining files each get an equal share, **capped by their real size** — a short file returns its unused allowance to the pool;
136
+ 3. the pool then tops up files that still have content left to inject (each up to twice the equal share): **`index.md` first**, then the rest by descending size — so which file gets truncated is decided by size, not by the order in `bootFiles`.
137
+
138
+ When every file is under the equal share, the result is byte-for-byte identical to the old even-split rule; it only diverges once the store grows. This also means: **`index.md` is a routing table, not a summary table** — it is injected in full, and bloat costs you the persona and other memories.
139
+
140
+ > **How is the "currently active session" decided?** The DSH host has no "currently focused session" signal (active session is a front-end concept). The plugin uses **the live root agent that most recently received a real user message** as a proxy: whichever session you speak in is the active one; switching away without sending a message is invisible to the host (a known limit of the proxy). Exact "expanded/focused" precision would need a small client-side focus report.
141
+
142
+ ### Memory health dashboard
143
+
144
+ Below the "Memory" section is a **read-only** dashboard fed by `GET /api/memory/insights`:
145
+
146
+ | Display | Meaning |
138
147
  |---|---|
139
- | 记忆页 / index 路由 / 记忆字数 | 记忆库规模(字数按字符计,中文不会按字节虚高 3 倍) |
140
- | 健康分(0–100) | 由孤儿页、缺 `salience`/`type`、陈旧页、失效 index 链接加权得出 |
141
- | 访问新鲜度条形图 | 今天 / ≤7 / ≤30 / ≤90 / >90 天 / 从未戳记(驱动自 `last_access` 自动戳记) |
142
- | 四项体检 | index 链接完整、无孤儿页、frontmatter 完整、访问新鲜度——口径与 `dsh-memory lint` 同源 |
143
- | 陈旧页候选 | 超过 90 天或从未戳记的页面,按陈旧度排序(前 8) |
144
- | 最近动态 | `log.md` 最新 5 条标题 |
148
+ | pages / index routes / word count | Store size (counted in characters, so Chinese is not inflated 3× by bytes) |
149
+ | Health score (0–100) | Weighted from orphan pages, missing `salience`/`type`, stale pages, and broken index links |
150
+ | Access freshness bars | today / ≤7 / ≤30 / ≤90 / >90 days / never stamped (driven by automatic `last_access` stamping) |
151
+ | Four checks | Index links complete, no orphan pages, frontmatter complete, access freshness — same source as `dsh-memory lint` |
152
+ | Stale candidates | Pages older than 90 days or never stamped, sorted by staleness (top 8) |
153
+ | Recent activity | The 5 newest `log.md` headlines |
145
154
 
146
- 看板是**实时**的:每次打开设置页(或改完任一配置项后)重新拉取,不落盘、不缓存、不写记忆库。头less 部署没有 `webServer` 时该路由与看板都不存在,其余功能不受影响。
155
+ The dashboard is **live**: refetched each time the settings page opens (or after any config change), never persisted, cached, or written back to the store. On a headless deployment without `webServer`, that route and the dashboard simply do not exist; everything else is unaffected.
147
156
 
148
- 覆盖 composition 键(例如把 boot 块预算调大),在 profile 的 `cordis.patch.yml` 里写**不带 `insert` 的 id 覆盖条目**:
157
+ To override a composition key (for example raising the boot budget), write an **id override entry without `insert`** in the profile's `cordis.patch.yml`:
149
158
 
150
159
  ```yaml
151
160
  - id: dsh-memory
@@ -153,74 +162,75 @@ dsh plugin --profile <profile> add dsh-plugin-memory
153
162
  bootMaxChars: 12000
154
163
  ```
155
164
 
156
- > 实现说明:DSH 的 settings wire 只服务硬编码的命名空间白名单,插件命名空间写不进去,因此本插件走自建通道——配置存 `<dshHome>/memory.json`(schema 校验 + 原子落盘),由插件自注册的 `GET/POST /api/memory/config` 路由服务,客户端区块 fetch 直连。
165
+ > Implementation note: DSH's settings wire only serves a hard-coded namespace allowlist, so a plugin namespace cannot be written through it. This plugin therefore uses its own channel: config lives in `<dshHome>/memory.json` (schema-validated, atomically written), served by the plugin's self-registered `GET/POST /api/memory/config` routes, which the client section fetches directly.
157
166
 
158
- ## 首次使用:铸魂
167
+ ## First run: soul-forging
159
168
 
160
- 插件安装后,第一次会话里 agent 的首要任务**不是干活**,而是与你对话定义它的灵魂:名字、性格、价值观、语气、边界,以及你的身份与你们的关系。逐项确认并写回 `SOUL.md` / `user/profile.md`,直到 `BOOTSTRAP.md` 的 `status` 变为 `complete`。你可以随时跳过或暂缓。
169
+ After installation, the agent's first task in the first session is **not** to work, but to talk with you about its soul: name, personality, values, tone, boundaries, plus who you are and what you are to each other. Each item is confirmed and written back to `SOUL.md` / `user/profile.md` until `BOOTSTRAP.md`'s `status` becomes `complete`. You can skip or defer at any time.
161
170
 
162
- ## 四个操作
171
+ ## The four operations
163
172
 
164
- - **remember(记)**:把值得持久化的内容蒸馏成页面,同步更新 `index.md`、追加 `log.md`。维护 index 时守路由表纪律:一行一页、一句话摘要(≤ 80 字)+ `salience`。
165
- - **recall(忆)**:会话开始读 boot 块;查询时先查 `index.md` 再钻页;必要时 `dsh-memory search <关键词>`。
166
- - **consolidate(整理)**:`dsh-memory lint` 查矛盾、孤儿页、该归档的冷页;`dsh-memory status` 看陈旧页分布。
167
- - **forget(忘)**:显式遗忘立即执行;自动衰减按 salience + last_access(冷页优先归档)。`last_access` 由插件自动维护,无需手改。
173
+ - **remember**: distil what is worth persisting into pages, updating `index.md` and appending to `log.md` in the same pass. Keep routing-table discipline when maintaining the index: one line per page, a one-sentence summary (≤ 80 characters) plus `salience`.
174
+ - **recall**: read the boot block at session start; on a query, consult `index.md` first and then drill into pages; use `dsh-memory search <keywords>` when needed.
175
+ - **consolidate**: `dsh-memory lint` finds contradictions, orphans, and cold pages due for archiving; `dsh-memory status` shows the stale-page distribution.
176
+ - **forget**: explicit forgetting happens immediately; automatic decay follows salience + last_access (cold pages are archived first). `last_access` is maintained by the plugin — no manual edits.
168
177
 
169
178
  ## CLI
170
179
 
171
180
  ```sh
172
- dsh-memory init [dir] # 创建记忆库脚手架
173
- dsh-memory search <query> [--touch] # 全文检索(--touch 给命中页戳 last_access)
174
- dsh-memory touch [pages...] # 手动戳 last_access(不带参数 = 全部页面)
175
- dsh-memory graph --suggest # 关系报告 + 「这页该连谁」建议(只报告)
176
- dsh-memory query --type decision --tag dsh --hot --stale [关键词]
177
- # 结构化检索:先按 frontmatter 过滤,再匹配正文
178
- dsh-memory checkup [--boot=N] # 一份报告:体量/新鲜度/连接度/一致性 + 优先级行动清单
179
- dsh-memory lint # 完整性体检
180
- dsh-memory status # 健康概览(含陈旧页统计)
181
- dsh-memory pack [out.tar.gz] # 打包导出(含 manifest)
182
- dsh-memory unpack <archive> [--force] # 从归档恢复
181
+ dsh-memory init [dir] # create the store scaffold
182
+ dsh-memory search <query> [--touch] # full-text search (--touch stamps hits with last_access)
183
+ dsh-memory touch [pages...] # stamp last_access manually (no args = every page)
184
+ dsh-memory graph --suggest # relationship report + "who should this page link to" (report only)
185
+ dsh-memory query --type decision --tag dsh --hot --stale [keywords]
186
+ # structured query: filter by frontmatter, then match the body
187
+ dsh-memory checkup [--boot=N] # one report: size/freshness/connectivity/consistency + prioritised actions
188
+ dsh-memory lint # integrity check
189
+ dsh-memory doctor # environment check-up + the command that fixes each finding
190
+ dsh-memory status # health overview (including stale-page counts)
191
+ dsh-memory pack [out.tar.gz] # export an archive (with manifest)
192
+ dsh-memory unpack <archive> [--force] # restore from an archive
183
193
  ```
184
194
 
185
- 存储定位顺序:`$MEMORY_DIR` → `./.memory`(存在时)→ `~/.memory`。
195
+ Store resolution order: `$MEMORY_DIR` → `./.memory` (when present) → `~/.memory`.
186
196
 
187
- ## 迁移
197
+ ## Migration
188
198
 
189
- 记忆库是纯 markdown + git:拷贝即迁移。跨机器 / 跨 agent / 能力降级档位见 [docs/MIGRATION.md](docs/MIGRATION.md)。
199
+ The store is plain markdown + git: copying it *is* the migration. Cross-machine, cross-agent, and capability-degradation tiers are covered in [docs/MIGRATION.md](docs/MIGRATION.md).
190
200
 
191
- ## 常见问题
201
+ ## FAQ
192
202
 
193
- **Q:和手写的 `.dsh/skills/memory` 文件技能(skill 版)什么关系?**
194
- skill 版是"软保障"(技能目录只注入简介,正文靠模型主动加载);本插件是"硬保障"(boot 块随系统提示词运行时上下文自动注入)。两者可共存:文件技能(rank 100)会覆盖插件内嵌技能(rank 250)的协议。如果你之前为了软保障改过系统提示词 persona(如 profile 补丁里的开机指令),装上本插件后建议**移除那段 persona**,避免双份注入。
203
+ **Q: How does this relate to a hand-written `.dsh/skills/memory` file skill?**
204
+ The file skill is a *soft* guarantee (only the skill's blurb is injected; the body depends on the model choosing to load it). This plugin is a *hard* guarantee (the boot block rides the system prompt's runtime context automatically). They can coexist: the file skill (rank 100) overrides the plugin's embedded skill (rank 250). If you previously edited the system prompt persona (say a boot instruction in a profile patch) to get the soft guarantee, **remove that persona** after installing this plugin to avoid double injection.
195
205
 
196
- **Q:boot 块会不会每次请求都重复注入、烧 token?**
197
- 不会。运行时上下文按投影去重:内容不变只注入一次;记忆更新后新快照取代旧快照。
206
+ **Q: Does the boot block get re-injected on every request and burn tokens?**
207
+ No. Runtime context is deduplicated by projection: an unchanged payload is injected once, and an updated memory replaces the old snapshot.
198
208
 
199
- **Q:记忆库放在哪里最合适?**
200
- 默认 `~/.memory`(全局、跨项目)。需要按项目隔离时,把 `memoryDir` 配到项目内,或让 agent 在项目里维护 `.memory/`。
209
+ **Q: Where should the store live?**
210
+ `~/.memory` by default (global, cross-project). For per-project isolation, point `memoryDir` inside the project, or let the agent maintain `.memory/` there.
201
211
 
202
- **Q:可以加密吗?**
203
- 记忆含敏感内容时,可把 `memoryDir` 放进加密卷 / 私有仓库。格式不变,插件无感知。
212
+ **Q: Can it be encrypted?**
213
+ If the memory holds sensitive content, put `memoryDir` on an encrypted volume or in a private repository. The format is unchanged and the plugin is unaware.
204
214
 
205
- ## 开发
215
+ ## Development
206
216
 
207
217
  ```sh
208
218
  git clone https://github.com/LittleBlackTong/dsh-plugin-memory.git
209
219
  cd dsh-plugin-memory
210
- node scripts/memory.mjs --self-test # 冒烟测试(无需安装依赖)
220
+ node scripts/memory.mjs --self-test # smoke test (no dependencies to install)
211
221
  ```
212
222
 
213
- 零构建:`lib/` 直接是运行时代码,`lib/types/index.d.ts` 供 TS 消费方使用。`boot.js` / `scaffold.js` 只依赖 `node:*` 内置模块,可独立复用。
223
+ Zero build: `lib/` is runtime code as-is, and `lib/types/index.d.ts` serves TypeScript consumers. `boot.js` / `scaffold.js` depend only on `node:*` built-ins and can be reused standalone.
214
224
 
215
- ## 路线图
225
+ ## Roadmap
216
226
 
217
- - [ ] TypeScript 重写(带完整类型与构建步骤)
218
- - [ ] embedding/BM25 检索(规模超过几百页后替代 index 先行)
219
- - [ ] MCP server(让非 DSH 的 agent 也能用同一套记忆库)
220
- - [ ] GitHub Actions CI(跑 `--self-test` 与 lint)
221
- - [ ] 记忆加密存储选项
227
+ - [ ] TypeScript rewrite (full types and a build step)
228
+ - [ ] Embedding/BM25 retrieval (replacing index-first lookup past a few hundred pages)
229
+ - [ ] MCP server (so non-DSH agents can use the same store)
230
+ - [ ] GitHub Actions CI (running `--self-test` and lint)
231
+ - [ ] Encrypted memory storage option
222
232
 
223
- 欢迎在 [Issues](https://github.com/LittleBlackTong/dsh-plugin-memory/issues) 里提需求、报 bug、交 PR。
233
+ Requests, bug reports, and pull requests are welcome in [Issues](https://github.com/LittleBlackTong/dsh-plugin-memory/issues).
224
234
 
225
235
  ## License
226
236
 
package/README.zh.md ADDED
@@ -0,0 +1,245 @@
1
+ # dsh-plugin-memory
2
+
3
+ **中文** | [English](README.md)
4
+
5
+ <p align="center">
6
+ <a href="https://github.com/LittleBlackTong/dsh-plugin-memory"><img alt="GitHub" src="https://img.shields.io/badge/GitHub-LittleBlackTong%2Fdsh--plugin--memory-blue?logo=github"></a>
7
+ <a href="https://www.npmjs.com/package/dsh-plugin-memory"><img alt="npm" src="https://img.shields.io/npm/v/dsh-plugin-memory?logo=npm"></a>
8
+ <img alt="license" src="https://img.shields.io/badge/license-MIT-blue">
9
+ <img alt="node" src="https://img.shields.io/badge/node-%3E%3D18-green">
10
+ </p>
11
+
12
+ > DeepSeek Harness 长期记忆插件:跨会话、可迁移、带「灵魂」的 markdown 记忆库。
13
+
14
+ **English TL;DR** — A Cordis plugin for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) that gives agents a persistent, cross-session, migratable long-term memory: a markdown + git store (inspired by Karpathy's *LLM Wiki* pattern) with a `SOUL.md` persona file, **auto-injected at every session start** via the system-prompt runtime context, plus remember / recall / consolidate / forget workflows and portable CLI tooling.
15
+
16
+ ## 快速上手
17
+
18
+ 三步,大约五分钟:
19
+
20
+ ```sh
21
+ # 1. 安装
22
+ dsh plugin --profile <profile> add dsh-plugin-memory
23
+ ```
24
+
25
+ 2. **重启应用**,开个会话随便说句话。boot 块会自动加载——因为记忆库还没有灵魂,agent 会**主动开始「铸魂」对话**:它叫什么名字、怎么称呼你、你们是什么关系、该是什么性格。答多答少都行,它会逐项写回去。
26
+
27
+ 3. **确认生效**。`dsh-memory doctor` 做一次环境体检(脚手架 / 孤儿页 / git 状态 / boot 预算),最后给出修复每一条的命令。或者打开**设置 → 记忆**看健康看板,选项都能热改。
28
+
29
+ 之后就很简单了:你干活时 agent 顺手把记忆写回去;你随时能手动整理记忆库——纯 markdown + git,没有任何锁死的东西。
30
+
31
+ ## 特性
32
+
33
+ - **开机强制注入(带路由表保护)**:插件通过 `ctx.systemPrompt.context()` 把记忆 boot 块(`SOUL.md` 人格 + `MEMORY.md` 协议 + `index.md` 目录 + 最近动态)注入会话上下文。宿主按投影去重:记忆不变就不重复注入,变化时新快照自动取代旧的——这是"新会话必先加载记忆"的**硬保障**,不需要模型碰运气调技能。总预算是 `bootMaxChars`,**按文件分配**:每个文件先拿一份均分(受实际大小封顶),剩余额度优先给 `index.md`、再给尚有内容未注入的文件——所以记忆库长大时,目录的尾部不会先被截掉(详见「boot 预算分配」)。默认还带**两道礼貌闸门**:`deferUntilUserSpeaks`(用户开口后才注入)与 `activeSessionOnly`(只注入当前激活会话),见「配置」。
34
+ - **last_access 自动戳记**:`MEMORY.md` 的 salience 衰减规则依赖 `last_access`,而它以前纯粹靠自觉——模板里有字段、规则里提到它,但没有任何代码去写它,于是每个页面都原地变老、衰减表形同虚设。现在会话收到第一条真实用户消息时,插件会把 boot 块**实际注入过的页面**(`SOUL.md` / `MEMORY.md` / `index.md` + 目录里被引用到的页)戳成当天,每会话一次、按天幂等,只改 `last_access` 一行;`dsh-memory touch <pages...>` 可手动补记,`trackPageAccess: false` 可关闭。
35
+ - **SOUL.md 铸魂**:安装后首要任务是和用户对话定义灵魂(名字、性格、价值观、语气、边界)、确认身份与关系(`BOOTSTRAP.md` 清单驱动,complete 前优先于常规任务)。
36
+ - **铸魂自动引导**:记忆库还没有灵魂(`BOOTSTRAP.md` 非 complete,或 `SOUL.md` 仍是占位模板)时,boot 块会自动前置一段第一人称引导词——「我的首要任务是确认我是谁,还有你是谁:我叫什么名字、怎么称呼你、你我是什么关系、我该是什么样的性格」——像 OpenClaw 初始化那样,**由 agent 在对话里主动发起铸魂**,逐项问、逐项写回,而不是等用户来喂。铸魂完成后引导词自动消失,零开销。
37
+ - **复利记忆**:遵循 Karpathy 的 *LLM Wiki* 约定——记忆是"一次编译、持续保鲜"的持久产物,不是每次查询重新 RAG。remember / recall / consolidate / forget 四操作 + salience 三级衰减。
38
+ - **可迁移**:记忆本体是纯 markdown + git + 自描述 schema,任何能读 markdown 的 agent 都能接手。`dsh-memory pack/unpack` 打包迁移。
39
+ - **内嵌技能**:通过 `ctx.skills.register()` 注册 `memory` 技能(操作协议随插件分发);项目级 `.dsh/skills/memory` 文件技能仍可覆盖它。
40
+ - **防懒 digest 唤醒**:每轮结束后,若 agent 空闲且记忆库超过 `digestNudgeAfterMinutes` 未写入,插件注入一条 digest 提醒(合成消息,走 `agent.followup`),把"会话收尾沉淀"从靠自觉变成有机制兜底;带冷却与每会话限次,不骚扰。**独立于 dsh-plugin-heartbeat**,两插件各自可装、互不依赖。
41
+ - **主动追忆(拟人化)**:对话空下来时,插件会以第一人称主动提起一件**真实记得**的、关于用户或你们之间的事(偏好、往事、未了的决定、最近的进展),把记忆从"只写回"变成"也用起来"——像老友自然想起那样,而非报状态。间隔在最短/最长之间**随机取值**(不固定节奏),配合每会话限次,不骚扰、不编造、不硬聊;纯对话行为,不写记忆库。**同样独立于 heartbeat**。
42
+ - **git 自动提交**:记忆库变更静默 `autoCommitQuietSeconds` 后自动 `git add -A && git commit`(无 `.git` 则跳过)——历史可回滚不再依赖 agent 记得 commit。
43
+ - **设置面板**:在 DSH 设置页提供「记忆 Memory」区块——总开关、记忆目录、开机注入、技能注册、主动追忆(开关 + 随机间隔范围 + 每会话次数)均可热改,立即生效,无需重启。
44
+ - **index 自动整理(声明式编译)**:索引行不再手写。页面在 frontmatter 里声明 `summary:`,`dsh-memory index --write` 把行重写为它的投影(标签 ≤32 / 摘要 ≤48 / 整行 ≤132 字符),**只改行内容,分节与顺序逐字节保留**,且幂等。`index --check` 给机器判定(有漂移退出码非 0),`index --sync-frontmatter` 把已写在 index 里的摘要回填进页面(老库一次性迁移)。digest 提醒会在索引漂移时附一句提示,agent 顺手就能修。
45
+ - **互链成为默认动作**:`index.md` 是目录(谁存在),页间互链才是**关系**(谁和谁有关)——实测一个真实记忆库里 29 页只有 1 条真互链,而图谱与跨页综合全都建立在这层关系上。现在三处一起推:`skills/memory.md` 的 remember 流程、`MEMORY.md` 模板的工作流、以及 digest 提醒的收尾指令,都明确要求"给这次碰过的页面各补 1–3 条相关页链接"。
46
+ - **补链建议**:`dsh-memory graph --suggest` 列出「只有索引入口、没有任何内容关系」的页面该引用谁——按共享主题 tag、标题词、同类型打分,并说明理由(`共享 #dsh` / `标题词 dsh` / `同类型`)。只报告不写入:是否连、怎么连由 agent 决定。`GET /api/memory/graph?suggest=1` 同源。
47
+ - **记忆图谱**:设置页里一张只读关系图 —— 不只是 `index.md` 那种"索引连着所有页"的星形,而是把页面**已经编码但没人画出来**的关系画出来:**索引路由**(`index.md` 指向每一页——这是全库最大的边集,43 条;先前版本跳过了元文件,导致 index 在图上孤立无援)、页间显式 markdown 链接(含相对路径解析)、共享 frontmatter tag(通用容器 tag 如 `project`/`skill` 会被忽略,稠密 tag 走锚点链而非全连接)。坐标由服务端 `lib/graph-layout.js` 一次性算好(确定性、无随机、无依赖),客户端只负责画 SVG。**可交互**:滚轮缩放(以光标为中心,缩放范围 0.4×–2.5×,非 passive 监听所以不会连带滚动设置页)、拖空白处平移、`重置视图` 回到全图;按住节点即可拖走,直接邻居按距离轻微跟随,松手后带缓动滑回原布局——拖动期间暂停过渡做到 1:1 跟手,用 O(邻居数) 的局部松弛而不是逐帧全量力导向(后者在几百页时会卡)。悬停高亮邻里,首次渲染从中心绽开(尊重 `prefers-reduced-motion`)。图谱**每 25 秒自动刷新**(面板开着时记忆变更会自己出现,副标题显示「更新于 HH:MM:SS」);取数失败保留上一张图而不是清空。视图数学(`zoomAt`/`panBy`/`toGraphPoint`)在 `lib/graph-view.js` 里是纯函数并有测试——缩放中心不漂移这件事必须被钉住。`GET /api/memory/graph`(只读、实时,`?types=1` 追加同类型弱边)。三类边在图上可区分:页间互链=实线蓝、索引路由=灰色虚线、共享 tag=细线。
48
+ - **`dsh-memory checkup`**:把四个读者(lint 的完整性、status 的计数、看板的体量与新鲜度、graph 的关系)合成**一份带优先级的报告** —— 体量、新鲜度条形图、连接度、一致性检查,最后给出"最该做的 N 件事"。四个数字是诊断,"做这三件事"才是产品。有活干时退出码非 0,可被脚本消费。`--boot=N` 用于按你的 boot 预算计算索引占比。
49
+ - **记忆健康看板**:同一区块下方是一张**只读**看板——记忆页数 / index 路由数 / 记忆字数、访问新鲜度条形图(今天 / ≤7 / ≤30 / ≤90 / >90 天)、四项体检结论(index 链接、孤儿页、frontmatter、新鲜度)与健康分、陈旧页候选、最近 5 条动态。数据来自新增的 `GET /api/memory/insights`(每次请求实时统计,不缓存),体检口径与 `dsh-memory lint` 同源,所以看板与 CLI 不会互相打脸。
50
+ - **零构建**:纯 ESM JavaScript,无编译步骤,`pnpm add` 即用。
51
+
52
+ ## 架构
53
+
54
+ 插件只拥有**工作流**,不拥有**数据格式**:
55
+
56
+ ```
57
+ dsh-plugin-memory(本插件)
58
+ ├── lib/index.js # Cordis 入口:boot 注入 + 运行时技能注册 + settings 热改
59
+ ├── lib/boot.js # boot 块渲染(SOUL/MEMORY/index + 最近 log,按文件分配预算)
60
+ ├── lib/pages.js # 页面访问记账:extractReferencedPages / touchPages(last_access)
61
+ ├── lib/insights.js # 记忆健康看板的数据层(页数/分布/新鲜度/体检,只读)
62
+ ├── lib/activity-tracker.js # 两道礼貌闸门:用户是否开口 + 当前激活会话
63
+ ├── lib/digest-guard.js # 防懒 digest 唤醒(空闲 + 记忆库久未写 → followup 提醒)
64
+ ├── lib/recall-nudge.js # 主动追忆(空闲 → 第一人称提起一件真实往事,纯对话不写库)
65
+ ├── lib/scaffold.js # 记忆库脚手架(模板只建不覆盖)
66
+ ├── lib/client.js # 客户端半:设置面板「记忆 Memory」区块 + 记忆健康看板
67
+ ├── skills/memory.md # 内嵌技能的操作协议正文
68
+ └── scripts/memory.mjs # CLI:init/search/touch/lint/status/pack/unpack
69
+
70
+ 记忆库(用户数据,默认 ~/.memory)
71
+ ├── SOUL.md # 人格与灵魂(用户主导)
72
+ ├── BOOTSTRAP.md # 铸魂清单(complete 前优先)
73
+ ├── MEMORY.md # schema 与维护协议(自描述)
74
+ ├── index.md # 页面目录 log.md # 时间线(append-only)
75
+ ├── identity/ user/ skills/ decisions/ projects/{active,archive}/ concepts/
76
+ └── raw/ # 不可变源材料
77
+ ```
78
+
79
+ ## 安装
80
+
81
+ ```sh
82
+ dsh plugin --profile <profile> add dsh-plugin-memory
83
+ ```
84
+
85
+ (包内置 `dsh.bundle` manifest,`dsh plugin add` 会把它自动挂进 profile 的 bundles 层;dsh-market 里的一键安装同此通道。)
86
+
87
+ 重启 profile(DSH Desktop 重启应用)后生效。
88
+
89
+ > 📌 **版本要求:DSH session format v4(官方 DeepSeek Harness 0.1.7+)**
90
+ >
91
+ > 注入消息的 source 使用 producer-owned kind `plugin:memory`。v4 的原生准入明确拒绝 v3 时代的
92
+ > `{ kind: 'plugin', plugin: 'memory' }` wrapper,报
93
+ > `format v4 message requires a producer-owned source kind`(该错误被包成 `code: "UNKNOWN"`,
94
+ > 所以在界面上会显示成 `... source kind UNKNOWN`——`UNKNOWN` 是错误码,不是 kind 值)。
95
+ >
96
+ > v4 与 v3 的白名单**互斥**(v3 只认 `plugin`),因此无法同时兼容两版。
97
+ > **0.8.0 及更早版本在 v4 上会触发该错误**;旧版 DSH 请停留在 0.8.0。
98
+
99
+ > ⚠️ **不要**再往 profile 的 `cordis.patch.yml` 里手写 `- insert: {id: dsh-memory, ...}`:
100
+ > 那会与 bundle manifest 的自动挂载产生两条同名 entry,整个 profile 会以
101
+ > `duplicate loader entry id "dsh-memory"` 启动失败(2026-08-18 实机事故)。
102
+ > 运行期配置(enabled / memoryDir / autoInject / registerSkill / recallEnabled)改走
103
+ > `<dshHome>/memory.json`(设置面板热改);composition 配置见下表。
104
+ > 如需覆盖某个 composition 键,用**不带 insert 的 id 覆盖条目**(见配置一节)。
105
+
106
+ ## 配置
107
+
108
+ | 键 | 默认值 | 含义 |
109
+ |---|---|---|
110
+ | `enabled` | `true` | 总开关:关闭后不注入 boot 块、不注册 `memory` 技能 |
111
+ | `memoryDir` | `~/.memory` | 记忆库绝对路径(`~` 自动展开) |
112
+ | `bootFiles` | `[SOUL.md, MEMORY.md, index.md]` | 开机注入的文件 |
113
+ | `bootMaxChars` | `12000` | boot 块总字符预算(防止占用过多上下文);按文件分配见下文 |
114
+ | `bootFileBudgets` | — | 逐文件字符上限,例如 `{ index.md: 3500 }`;未列出的文件分剩余额度(composition 层,改完需重启) |
115
+ | `trackPageAccess` | `true` | 会话首条用户消息时,把 boot 块实际注入的页面戳 `last_access`(关闭后衰减表需手动维护) |
116
+ | `autoInject` | `true` | 会话开始时注入 boot 块 |
117
+ | `deferUntilUserSpeaks` | `true` | 用户发出第一条真实消息后才注入(boot 块 / 追忆 / digest 提醒都遵守);面板可热改 |
118
+ | `activeSessionOnly` | `true` | 只对「当前激活会话」(最近收到用户消息的会话)注入,后台会话不打扰;面板可热改 |
119
+ | `registerSkill` | `true` | 注册内嵌 `memory` 技能 |
120
+ | `scaffold` | `true` | 记忆库缺失时自动创建模板(只建不覆盖) |
121
+ | `configFile` | `<dshHome>/memory.json` | 用户可改配置的 JSON 文件路径(设置面板读写它) |
122
+ | `digestNudgeEnabled` | `true` | 防懒 digest 提醒总开关(composition) |
123
+ | `digestNudgeAfterMinutes` | `120` | 记忆库超过多久未写入就提醒 |
124
+ | `digestNudgeCooldownMinutes` | `180` | 两次提醒的最小间隔 |
125
+ | `digestNudgeMaxPerSession` | `2` | 每个会话最多提醒次数 |
126
+ | `recallEnabled` | `true` | 主动追忆总开关(面板可热改) |
127
+ | `recallIntervalMinMinutes` | `30` | 随机间隔下限(分钟,面板可热改) |
128
+ | `recallIntervalMaxMinutes` | `240` | 随机间隔上限(分钟,面板可热改) |
129
+ | `recallMaxPerSession` | `3` | 每个会话最多追忆次数(面板可热改) |
130
+ | `autoCommit` | `true` | 记忆库 git 自动提交开关(composition) |
131
+ | `autoCommitQuietSeconds` | `60` | 变更静默多久后提交(防抖) |
132
+ | `autoCommitIntervalSeconds` | `60` | 变更轮询间隔 |
133
+
134
+ ### 设置面板(热改)
135
+
136
+ `enabled` / `memoryDir` / `autoInject` / `deferUntilUserSpeaks` / `activeSessionOnly` / `registerSkill` / `recallEnabled` / `recallIntervalMinMinutes` / `recallIntervalMaxMinutes` / `recallMaxPerSession` 十项在 DSH 设置页的「记忆 Memory」区块中可改,**即时生效**:boot 注入、两道礼貌闸门、技能注册、主动追忆(含随机间隔与次数)随修改立即生效;记忆目录切换时自动为新目录初始化脚手架(`scaffold: true` 时)。其余键(`bootFiles` / `bootMaxChars` / `bootFileBudgets` / `trackPageAccess` / `scaffold` / `configFile` / `digestNudge*` / `autoCommit*`)只在 composition 配置层生效,改完需重启。
137
+
138
+ ### boot 预算分配(为什么 index 不会先被截掉)
139
+
140
+ `bootMaxChars` 是总预算,按文件分配而不是简单平摊:
141
+
142
+ 1. 有 `bootFileBudgets` 条目的文件先拿自己的额度;
143
+ 2. 其余文件各拿一份均分额度,**以文件实际大小封顶**——短文件把用不完的额度退回池子;
144
+ 3. 池子再补给「还有内容没注入」的文件(每个最多补到均分额度的两倍):**`index.md` 优先**,其余按体积降序——所以截断落在哪个文件上由体积决定,而不是由 `bootFiles` 的书写顺序决定。
145
+
146
+ 当每个文件都小于均分额度时,结果与旧的平摊规则逐字节相同;只有记忆库长大后才会不同。这也意味着:**`index.md` 是路由表,别把它写成摘要表**——它会被完整注入,膨胀的代价是挤掉人格与其他记忆。
147
+
148
+ > **「当前激活会话」怎么判?** DSH 宿主侧没有「浏览器当前聚焦的会话」信号(激活会话是前端概念)。插件用**最近一次收到真实用户消息的 live root agent**作为激活会话的代理:你在哪个会话里说话,哪个会话就激活;切到别处但不发消息时,宿主感知不到「切换」这个动作(这是代理的已知边界)。若日后需要精确到「展开/聚焦」级别,需补一小段客户端 focus 上报。
149
+
150
+ ### 记忆健康看板
151
+
152
+ 设置页「记忆 Memory」区块下方是一张**只读**看板,数据来自 `GET /api/memory/insights`:
153
+
154
+ | 展示 | 含义 |
155
+ |---|---|
156
+ | 记忆页 / index 路由 / 记忆字数 | 记忆库规模(字数按字符计,中文不会按字节虚高 3 倍) |
157
+ | 健康分(0–100) | 由孤儿页、缺 `salience`/`type`、陈旧页、失效 index 链接加权得出 |
158
+ | 访问新鲜度条形图 | 今天 / ≤7 / ≤30 / ≤90 / >90 天 / 从未戳记(驱动自 `last_access` 自动戳记) |
159
+ | 四项体检 | index 链接完整、无孤儿页、frontmatter 完整、访问新鲜度——口径与 `dsh-memory lint` 同源 |
160
+ | 陈旧页候选 | 超过 90 天或从未戳记的页面,按陈旧度排序(前 8) |
161
+ | 最近动态 | `log.md` 最新 5 条标题 |
162
+
163
+ 看板是**实时**的:每次打开设置页(或改完任一配置项后)重新拉取,不落盘、不缓存、不写记忆库。头less 部署没有 `webServer` 时该路由与看板都不存在,其余功能不受影响。
164
+
165
+ 覆盖 composition 键(例如把 boot 块预算调大),在 profile 的 `cordis.patch.yml` 里写**不带 `insert` 的 id 覆盖条目**:
166
+
167
+ ```yaml
168
+ - id: dsh-memory
169
+ config:
170
+ bootMaxChars: 12000
171
+ ```
172
+
173
+ > 实现说明:DSH 的 settings wire 只服务硬编码的命名空间白名单,插件命名空间写不进去,因此本插件走自建通道——配置存 `<dshHome>/memory.json`(schema 校验 + 原子落盘),由插件自注册的 `GET/POST /api/memory/config` 路由服务,客户端区块 fetch 直连。
174
+
175
+ ## 首次使用:铸魂
176
+
177
+ 插件安装后,第一次会话里 agent 的首要任务**不是干活**,而是与你对话定义它的灵魂:名字、性格、价值观、语气、边界,以及你的身份与你们的关系。逐项确认并写回 `SOUL.md` / `user/profile.md`,直到 `BOOTSTRAP.md` 的 `status` 变为 `complete`。你可以随时跳过或暂缓。
178
+
179
+ ## 四个操作
180
+
181
+ - **remember(记)**:把值得持久化的内容蒸馏成页面,同步更新 `index.md`、追加 `log.md`。维护 index 时守路由表纪律:一行一页、一句话摘要(≤ 80 字)+ `salience`。
182
+ - **recall(忆)**:会话开始读 boot 块;查询时先查 `index.md` 再钻页;必要时 `dsh-memory search <关键词>`。
183
+ - **consolidate(整理)**:`dsh-memory lint` 查矛盾、孤儿页、该归档的冷页;`dsh-memory status` 看陈旧页分布。
184
+ - **forget(忘)**:显式遗忘立即执行;自动衰减按 salience + last_access(冷页优先归档)。`last_access` 由插件自动维护,无需手改。
185
+
186
+ ## CLI
187
+
188
+ ```sh
189
+ dsh-memory init [dir] # 创建记忆库脚手架
190
+ dsh-memory search <query> [--touch] # 全文检索(--touch 给命中页戳 last_access)
191
+ dsh-memory touch [pages...] # 手动戳 last_access(不带参数 = 全部页面)
192
+ dsh-memory graph --suggest # 关系报告 + 「这页该连谁」建议(只报告)
193
+ dsh-memory query --type decision --tag dsh --hot --stale [关键词]
194
+ # 结构化检索:先按 frontmatter 过滤,再匹配正文
195
+ dsh-memory checkup [--boot=N] # 一份报告:体量/新鲜度/连接度/一致性 + 优先级行动清单
196
+ dsh-memory lint # 完整性体检
197
+ dsh-memory doctor # 环境体检:脚手架 / 孤儿页 / git / boot 预算,并给出修复命令
198
+ dsh-memory status # 健康概览(含陈旧页统计)
199
+ dsh-memory pack [out.tar.gz] # 打包导出(含 manifest)
200
+ dsh-memory unpack <archive> [--force] # 从归档恢复
201
+ ```
202
+
203
+ 存储定位顺序:`$MEMORY_DIR` → `./.memory`(存在时)→ `~/.memory`。
204
+
205
+ ## 迁移
206
+
207
+ 记忆库是纯 markdown + git:拷贝即迁移。跨机器 / 跨 agent / 能力降级档位见 [docs/MIGRATION.md](docs/MIGRATION.md)。
208
+
209
+ ## 常见问题
210
+
211
+ **Q:和手写的 `.dsh/skills/memory` 文件技能(skill 版)什么关系?**
212
+ skill 版是"软保障"(技能目录只注入简介,正文靠模型主动加载);本插件是"硬保障"(boot 块随系统提示词运行时上下文自动注入)。两者可共存:文件技能(rank 100)会覆盖插件内嵌技能(rank 250)的协议。如果你之前为了软保障改过系统提示词 persona(如 profile 补丁里的开机指令),装上本插件后建议**移除那段 persona**,避免双份注入。
213
+
214
+ **Q:boot 块会不会每次请求都重复注入、烧 token?**
215
+ 不会。运行时上下文按投影去重:内容不变只注入一次;记忆更新后新快照取代旧快照。
216
+
217
+ **Q:记忆库放在哪里最合适?**
218
+ 默认 `~/.memory`(全局、跨项目)。需要按项目隔离时,把 `memoryDir` 配到项目内,或让 agent 在项目里维护 `.memory/`。
219
+
220
+ **Q:可以加密吗?**
221
+ 记忆含敏感内容时,可把 `memoryDir` 放进加密卷 / 私有仓库。格式不变,插件无感知。
222
+
223
+ ## 开发
224
+
225
+ ```sh
226
+ git clone https://github.com/LittleBlackTong/dsh-plugin-memory.git
227
+ cd dsh-plugin-memory
228
+ node scripts/memory.mjs --self-test # 冒烟测试(无需安装依赖)
229
+ ```
230
+
231
+ 零构建:`lib/` 直接是运行时代码,`lib/types/index.d.ts` 供 TS 消费方使用。`boot.js` / `scaffold.js` 只依赖 `node:*` 内置模块,可独立复用。
232
+
233
+ ## 路线图
234
+
235
+ - [ ] TypeScript 重写(带完整类型与构建步骤)
236
+ - [ ] embedding/BM25 检索(规模超过几百页后替代 index 先行)
237
+ - [ ] MCP server(让非 DSH 的 agent 也能用同一套记忆库)
238
+ - [ ] GitHub Actions CI(跑 `--self-test` 与 lint)
239
+ - [ ] 记忆加密存储选项
240
+
241
+ 欢迎在 [Issues](https://github.com/LittleBlackTong/dsh-plugin-memory/issues) 里提需求、报 bug、交 PR。
242
+
243
+ ## License
244
+
245
+ [MIT](LICENSE)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-plugin-memory",
3
3
  "description": "Long-term memory plugin for DeepSeek Harness: persistent cross-session markdown memory with a soul/persona file, auto-injected at every session start, plus remember/recall/consolidate/forget workflows and portable migration tooling.",
4
- "version": "0.9.3",
4
+ "version": "0.10.0",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "main": "lib/index.js",
@@ -33,6 +33,7 @@
33
33
  "cordis.patch.yml",
34
34
  "CHANGELOG.md",
35
35
  "README.md",
36
+ "README.zh.md",
36
37
  "LICENSE"
37
38
  ],
38
39
  "scripts": {
@@ -361,6 +361,151 @@ function cmdStatus(store) {
361
361
  console.log(`oldest last_access: ${oldest}`)
362
362
  }
363
363
 
364
+ /**
365
+ * `doctor` — the command to run when something feels off.
366
+ *
367
+ * It checks what actually breaks in practice — the scaffold, orphan pages,
368
+ * dead index links, git state, boot budget, staleness — and ends with the
369
+ * command that repairs each finding. Any hard failure exits 1; warnings
370
+ * alone keep the exit at 0, so `doctor` can gate a CI step.
371
+ */
372
+ function cmdDoctor(store) {
373
+ const failures = []
374
+ const warnings = []
375
+ const fixes = []
376
+ const row = (mark, text) => console.log(` ${mark} ${text}`)
377
+
378
+ console.log('dsh-memory doctor')
379
+ console.log(`记忆库:${store}`)
380
+ console.log('')
381
+
382
+ if (!existsSync(store)) {
383
+ row('✗', '目录不存在')
384
+ console.log('\n建议:')
385
+ console.log(` dsh-memory init ${store}`)
386
+ process.exit(1)
387
+ }
388
+
389
+ // 1. writability — probe with a real write, because mode bits lie under ACLs
390
+ let writable = true
391
+ const probe = join(store, '.doctor-probe')
392
+ try {
393
+ writeFileSync(probe, '')
394
+ rmSync(probe)
395
+ } catch {
396
+ writable = false
397
+ }
398
+ row(writable ? '✓' : '✗', `目录可写${writable ? '' : '(写不进去,插件无法维护记忆库)'}`)
399
+ if (!writable) {
400
+ failures.push('目录不可写')
401
+ fixes.push(`chmod u+w "${store}"`)
402
+ }
403
+
404
+ // 2. scaffold
405
+ const REQUIRED = ['SOUL.md', 'MEMORY.md', 'index.md', 'log.md', 'BOOTSTRAP.md']
406
+ const missing = REQUIRED.filter((f) => !existsSync(join(store, f)))
407
+ if (missing.length === 0) {
408
+ row('✓', '脚手架完整(SOUL / MEMORY / index / log / BOOTSTRAP)')
409
+ } else {
410
+ row('✗', `脚手架缺文件:${missing.join(' / ')}`)
411
+ failures.push('脚手架不完整')
412
+ fixes.push('dsh-memory init — 只补缺失的模板,不覆盖已有内容')
413
+ }
414
+
415
+ // 3. pages, orphans and dead links
416
+ const all = pages(store)
417
+ const indexPath = join(store, 'index.md')
418
+ const indexText = existsSync(indexPath) ? readFileSync(indexPath, 'utf8') : ''
419
+ const linked = [...indexText.matchAll(/\]\(([^)]+\.md)\)/g)]
420
+ .map((m) => m[1])
421
+ .filter((p) => !p.startsWith('http'))
422
+ .map((p) => p.replace(/^\.\//, ''))
423
+ const inIndex = new Set(linked)
424
+ const orphans = all.filter((p) => !inIndex.has(p))
425
+ row('✓', `页面 ${all.length} 个,index 覆盖 ${linked.length} 条`)
426
+ if (orphans.length > 0) {
427
+ row('⚠', `孤儿页 ${orphans.length} 个(存在于记忆库但不在 index.md 里)`)
428
+ warnings.push(`${orphans.length} 个孤儿页`)
429
+ fixes.push('dsh-memory index --write — 把声明了 summary: 的页面写回索引行')
430
+ }
431
+ const dead = linked.filter((p) => !existsSync(join(store, p)))
432
+ if (dead.length > 0) {
433
+ row('✗', `index 里 ${dead.length} 条死链:${dead.slice(0, 3).join(', ')}${dead.length > 3 ? ' …' : ''}`)
434
+ failures.push('index 有死链')
435
+ fixes.push('dsh-memory lint — 列出全部死链后手工修')
436
+ }
437
+
438
+ // 4. git — the store is worth versioning, and autoCommit silently no-ops without a repo
439
+ if (!existsSync(join(store, '.git'))) {
440
+ row('⚠', '不是 git 仓库(没有版本历史,插件的 autoCommit 会跳过)')
441
+ warnings.push('无 git 仓库')
442
+ fixes.push(`git -C "${store}" init && git -C "${store}" add -A && git -C "${store}" commit -m "init"`)
443
+ } else {
444
+ const git = (...a) => execFileSync('git', a, {
445
+ cwd: store, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'],
446
+ }).trim()
447
+ let branch = ''
448
+ let dirty = 0
449
+ let ahead = 0
450
+ let hasRemote = false
451
+ try {
452
+ branch = git('rev-parse', '--abbrev-ref', 'HEAD')
453
+ dirty = git('status', '--porcelain').split('\n').filter(Boolean).length
454
+ hasRemote = git('remote').length > 0
455
+ if (hasRemote) {
456
+ const counts = git('rev-list', '--count', '--left-right', '@{upstream}...HEAD').split(/\s+/)
457
+ ahead = Number(counts[1] ?? 0)
458
+ }
459
+ } catch {
460
+ // an empty repo or a missing upstream lands here; the rows below cover both
461
+ }
462
+ row('✓', `git 仓库(分支 ${branch || '(尚无提交)'}${hasRemote ? '' : ',未配远端'})`)
463
+ if (dirty > 0) {
464
+ row('⚠', `${dirty} 个文件未提交(autoCommit 静默后会自行提交)`)
465
+ warnings.push(`${dirty} 个未提交文件`)
466
+ }
467
+ if (hasRemote && ahead > 0) {
468
+ row('⚠', `领先远端 ${ahead} 个提交(本地有历史没推上去)`)
469
+ warnings.push(`${ahead} 个提交未推送`)
470
+ fixes.push(`git -C "${store}" push — 记忆插件只 auto-commit,从不 auto-push`)
471
+ }
472
+ }
473
+
474
+ // 5. boot budget — best effort: the CLI cannot see the plugin's composition config
475
+ const BOOT_FILES = ['SOUL.md', 'MEMORY.md', 'index.md']
476
+ const DEFAULT_BUDGET = 12000
477
+ const bootChars = BOOT_FILES.reduce((n, f) => {
478
+ const p = join(store, f)
479
+ return n + (existsSync(p) ? readFileSync(p, 'utf8').length : 0)
480
+ }, 0)
481
+ const pct = Math.round((bootChars / DEFAULT_BUDGET) * 100)
482
+ row(pct > 100 ? '⚠' : '✓', `boot 预算 ${bootChars} / ${DEFAULT_BUDGET} 字符(${pct}%;按默认值算,实际以插件配置为准)`)
483
+ if (pct > 100) {
484
+ warnings.push('boot 预算超出默认值')
485
+ fixes.push('dsh-memory checkup --boot=12000 — 看该裁哪个文件')
486
+ }
487
+
488
+ // 6. staleness, reusing the same source as `status`
489
+ const access = accessSummary(store)
490
+ if (access.total > 0 && access.stale.length > 0) {
491
+ row('⚠', `陈旧页 ${access.stale.length}/${access.total}(>90 天或从未戳记)`)
492
+ warnings.push(`${access.stale.length} 个陈旧页`)
493
+ }
494
+
495
+ console.log('')
496
+ if (failures.length === 0 && warnings.length === 0) {
497
+ console.log('一切正常 ✅')
498
+ process.exit(0)
499
+ }
500
+ if (fixes.length > 0) {
501
+ console.log('建议:')
502
+ for (const f of fixes) console.log(` - ${f}`)
503
+ console.log('')
504
+ }
505
+ console.log(`结论:${failures.length} 项失败,${warnings.length} 项提醒`)
506
+ process.exit(failures.length > 0 ? 1 : 0)
507
+ }
508
+
364
509
  function manifestFor(storePath) {
365
510
  const files = walk(storePath).sort().map((f) => ({
366
511
  path: relative(storePath, f),
@@ -467,6 +612,7 @@ if (args[0] === '--self-test') {
467
612
  index [--check|--write|--sync-frontmatter]
468
613
  index routing table: report drift / rewrite / import summaries
469
614
  lint integrity check
615
+ doctor environment check-up: scaffold, orphans, git, boot budget + fixes
470
616
  status health summary
471
617
  pack [out.tar.gz] export portable archive + manifest
472
618
  unpack <archive> [--force] restore archive
@@ -480,6 +626,7 @@ Store: $MEMORY_DIR or ./.memory or ~/.memory (current: ${store})`
480
626
  case 'query': cmdQuery(store, args.slice(1)); break
481
627
  case 'index': cmdIndex(store, args.slice(1)); break
482
628
  case 'lint': cmdLint(store); break
629
+ case 'doctor': cmdDoctor(store); break
483
630
  case 'status': cmdStatus(store); break
484
631
  case 'pack': cmdPack(store, args[1]); break
485
632
  case 'unpack': cmdUnpack(store, args[1], args.includes('--force')); break