dsh-aiyayi 1.6.1 → 1.6.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/CHANGELOG.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  > **维护约定**:新版本发布时在顶部加条目。本文件由 README 正文拆出(2026-11-09 重构):版本条目与踩坑记录原文照录,数字、时间、日期一律不改(量化保真是本插件的规矩)。版本事实以 npm registry 为准。
4
4
  >
5
- > **npm 发布一览(2026-11-13 08:5x 核对 registry `dist-tags`)**:latest = **1.5.45**(2026-11-12T16:48:34.644Z 已发布);**本次发版版为 1.6.0**(工具箱自主成长开放给所有用户,publish 与 registry 回查见本文件 1.6.0 条目、DEPLOY 台账)。下列长串是 1.5.39 时代的旧记录,原文保留、以 registry 为准。已发布:1.0.0、…、1.5.7、**1.5.14**、**1.5.15**、**1.5.16**、**1.5.17**、**1.5.20**、**1.5.21**、**1.5.22**、**1.5.23**、**1.5.24**、**1.5.25**、**1.5.26**、**1.5.27**、**1.5.28**、**1.5.29**、**1.5.30**、**1.5.31**、**1.5.32**、**1.5.33**、**1.5.34**、**1.5.35**、**1.5.36**、**1.5.37**;跳号 1.3.0、1.4.1、1.4.2、1.5.0、1.5.1、1.5.2、1.5.3、**1.5.8–1.5.13** 从未发布到 npm(本地开发线:1.5.8–1.5.11 飞书桥接、1.5.12 思考纪律约束、1.5.13 内置技能同步+feishu-setup 骨架);1.5.14 已发布(坑 #58–#67 修复);1.5.15 已发布(扫码接入改造);1.5.16 已发布(feishu-setup 技能脚本入包);1.5.17 已发布(坑 #68 重入保护修复);**1.5.18 / 1.5.19 也从未发布到 npm**(见下方 1.5.18 / 1.5.19 条目——它们的代码最终随 1.5.20 一起进 npm);**1.5.20 已发布**(飞书正文改走 interactive 卡片 + lark_md,含 1.5.19 附件收发与问答卡片、1.5.20 markdown 渲染,以及 create-bot v6 权限集中化);**1.5.21 已发布**(修飞书回复正文首尾空白行,坑 #72);**1.5.22 已发布**(多应用多长连接改造+chat 归属机制,见下方 1.5.22 条目);**1.5.23–1.5.25 已发布**(230001 边界/补 import/补 feishuCode 挖码三轮装版断言增量修复,见下方对应条目);**1.5.26 已发布**(回发多文本段覆盖丢段修复,坑 #73,见下方 1.5.26 条目);**1.5.27 已发布**(微信通道并入,默认关闭,见下方 1.5.27 条目);**1.5.28 已发布**(PowerShell 版本体检 + 微信通道收尾,见下方 1.5.28 条目);**1.5.29 已发布**(发布包排除 `test/` + 测试残留空目录清理,见下方 1.5.29 条目);**1.5.30 已发布**(create-bot 注释改用 `<记忆根>` 占位,见下方 1.5.30 条目);**1.5.31 已发布**(PowerShell 体检 `major:null` 根因修复:探测链补上 powershell.exe 一级,见下方 1.5.31 条目);**1.5.32 已发布**(行为变更:首次提示同时告知微信 ClawBot 能力 + 「对接默认落到发起对接的会话」固化为 persona 准则/文档;不含路由代码改动,见下方 1.5.32 条目);**1.5.33 已发布**(同一准则落成代码:未绑定 chat 三档复用,不再无条件新建会话,见下方 1.5.33 条目);**1.5.34 已发布**(纯测试修复:`presence-clock` 跨午夜偶发失败,`lib/` 零改动,见下方 1.5.34 条目);**1.5.35 已发布**(`feishu_status` 新增最近入站路由回看 `inboundRouting` 字段,见下方 1.5.35 条目);**1.5.36 已发布**(飞书连接状态失真修复,见下方 1.5.36 条目);**1.5.37 已发布**(压缩前沉淀机制 precompact,见下方 1.5.37 条目);**1.5.38 已发布**(工具包中间层首批 toolkit_cmd / toolkit_read / toolkit_write,见下方 1.5.38 条目);**1.5.39 为本次发版版**(toolkit 三件套注册失败修复 + feishu_bind 自动绑定,见下方 1.5.39 条目;publish 留待下一步,以 registry 为准);**1.5.40 为当前开发版**(toolkit 执行输出对齐 output.schema——宿主对输出也做 schema 校验,见下方 1.5.40 条目与坑 #78;仅本地提交,不 push 不 publish);**1.5.41 为当前发版版**(Toolbox 真异步能力 + watch_read 异步工具 + mem_sync 同步工具,见下方 1.5.41 条目与坑 #79/#80;2026-11-12T07:17:11Z 已发布到 npm,latest);**1.5.42 已发布版**(新增 `pkg_stats` 同步工具——npm 下载量 + GitHub/Gitee/GitCode 仓库热度,见下方 1.5.42 条目;2026-11-12T07:46:35Z 已发布到 npm,latest)。**1.5.43 已发布**(`mem_sync` 的 P0/P1 缺陷修复 + **破坏性变更:`watch_read`/`mem_sync`/`pkg_stats` 三个私有工具移出工具面**,实现文件保留待 1.5.44 本地清单复用,见下方 1.5.43 条目;2026-11-12T13:13:36Z 已发布到 npm,latest)。**1.5.44 已发布(latest)**(本地工具清单机制:`<记忆根>/toolbox.json` + `toolbox_list` / `toolbox_reload`,包内只留 3+2 工具、私有工具走本机清单,见下方 1.5.44 条目;2026-11-12T14:30:16Z 已发布到 npm,shasum `02a5f99e…`)。**1.5.45 已发布**(飞书引导误触发三连修 + 插件注入身份自标注,见下方 1.5.45 条目;publish 与 registry 回查见 DEPLOY 台账)。**1.6.0 已发布**(工具箱自主成长开放给所有用户,2026-11-13T01:05:35.003Z 发布,shasum `c2b25e57…`,55 个文件);**本次发版版为 1.6.1**(能力声明最小闭环 + `toolbox_remove` + precompact 沉淀过滤修复(坑 #81)+ 飞书 pending 切换提示 + persona 三条 + README 瘦身,见下方 1.6.1 条目)。
5
+ > **npm 发布一览(2026-11-13 16:4x 订正:1.6.1 发布时漏改本条,此次补正;registry `dist-tags` 实测 latest = 1.6.1)**:latest = **1.6.1**(2026-11-13T04:41:37.374Z 已发布,shasum `64c51a3f…`,见本文件 1.6.1 条目、DEPLOY 1.6.1 台账);**本次发版为 1.6.2(待发布,尚未 publish,发布后回填本处与 DEPLOY 台账)**(precompact_run 输出修复 + memory_organize 档案健康度与 forget 精确定位,见本文件 1.6.2 条目)。下列长串是 1.5.39 时代的旧记录,原文保留、以 registry 为准。已发布:1.0.0、…、1.5.7、**1.5.14**、**1.5.15**、**1.5.16**、**1.5.17**、**1.5.20**、**1.5.21**、**1.5.22**、**1.5.23**、**1.5.24**、**1.5.25**、**1.5.26**、**1.5.27**、**1.5.28**、**1.5.29**、**1.5.30**、**1.5.31**、**1.5.32**、**1.5.33**、**1.5.34**、**1.5.35**、**1.5.36**、**1.5.37**;跳号 1.3.0、1.4.1、1.4.2、1.5.0、1.5.1、1.5.2、1.5.3、**1.5.8–1.5.13** 从未发布到 npm(本地开发线:1.5.8–1.5.11 飞书桥接、1.5.12 思考纪律约束、1.5.13 内置技能同步+feishu-setup 骨架);1.5.14 已发布(坑 #58–#67 修复);1.5.15 已发布(扫码接入改造);1.5.16 已发布(feishu-setup 技能脚本入包);1.5.17 已发布(坑 #68 重入保护修复);**1.5.18 / 1.5.19 也从未发布到 npm**(见下方 1.5.18 / 1.5.19 条目——它们的代码最终随 1.5.20 一起进 npm);**1.5.20 已发布**(飞书正文改走 interactive 卡片 + lark_md,含 1.5.19 附件收发与问答卡片、1.5.20 markdown 渲染,以及 create-bot v6 权限集中化);**1.5.21 已发布**(修飞书回复正文首尾空白行,坑 #72);**1.5.22 已发布**(多应用多长连接改造+chat 归属机制,见下方 1.5.22 条目);**1.5.23–1.5.25 已发布**(230001 边界/补 import/补 feishuCode 挖码三轮装版断言增量修复,见下方对应条目);**1.5.26 已发布**(回发多文本段覆盖丢段修复,坑 #73,见下方 1.5.26 条目);**1.5.27 已发布**(微信通道并入,默认关闭,见下方 1.5.27 条目);**1.5.28 已发布**(PowerShell 版本体检 + 微信通道收尾,见下方 1.5.28 条目);**1.5.29 已发布**(发布包排除 `test/` + 测试残留空目录清理,见下方 1.5.29 条目);**1.5.30 已发布**(create-bot 注释改用 `<记忆根>` 占位,见下方 1.5.30 条目);**1.5.31 已发布**(PowerShell 体检 `major:null` 根因修复:探测链补上 powershell.exe 一级,见下方 1.5.31 条目);**1.5.32 已发布**(行为变更:首次提示同时告知微信 ClawBot 能力 + 「对接默认落到发起对接的会话」固化为 persona 准则/文档;不含路由代码改动,见下方 1.5.32 条目);**1.5.33 已发布**(同一准则落成代码:未绑定 chat 三档复用,不再无条件新建会话,见下方 1.5.33 条目);**1.5.34 已发布**(纯测试修复:`presence-clock` 跨午夜偶发失败,`lib/` 零改动,见下方 1.5.34 条目);**1.5.35 已发布**(`feishu_status` 新增最近入站路由回看 `inboundRouting` 字段,见下方 1.5.35 条目);**1.5.36 已发布**(飞书连接状态失真修复,见下方 1.5.36 条目);**1.5.37 已发布**(压缩前沉淀机制 precompact,见下方 1.5.37 条目);**1.5.38 已发布**(工具包中间层首批 toolkit_cmd / toolkit_read / toolkit_write,见下方 1.5.38 条目);**1.5.39 已发布**(toolkit 三件套注册失败修复 + feishu_bind 自动绑定,见下方 1.5.39 条目;publish 留待下一步,以 registry 为准);**1.5.40 为当前开发版**(toolkit 执行输出对齐 output.schema——宿主对输出也做 schema 校验,见下方 1.5.40 条目与坑 #78;仅本地提交,不 push 不 publish);**1.5.41 已发布**(Toolbox 真异步能力 + watch_read 异步工具 + mem_sync 同步工具,见下方 1.5.41 条目与坑 #79/#80;2026-11-12T07:17:11Z 已发布到 npm,latest);**1.5.42 已发布版**(新增 `pkg_stats` 同步工具——npm 下载量 + GitHub/Gitee/GitCode 仓库热度,见下方 1.5.42 条目;2026-11-12T07:46:35Z 已发布到 npm,latest)。**1.5.43 已发布**(`mem_sync` 的 P0/P1 缺陷修复 + **破坏性变更:`watch_read`/`mem_sync`/`pkg_stats` 三个私有工具移出工具面**,实现文件保留待 1.5.44 本地清单复用,见下方 1.5.43 条目;2026-11-12T13:13:36Z 已发布到 npm,latest)。**1.5.44 已发布(latest)**(本地工具清单机制:`<记忆根>/toolbox.json` + `toolbox_list` / `toolbox_reload`,包内只留 3+2 工具、私有工具走本机清单,见下方 1.5.44 条目;2026-11-12T14:30:16Z 已发布到 npm,shasum `02a5f99e…`)。**1.5.45 已发布**(飞书引导误触发三连修 + 插件注入身份自标注,见下方 1.5.45 条目;publish 与 registry 回查见 DEPLOY 台账)。**1.6.0 已发布**(工具箱自主成长开放给所有用户,2026-11-13T01:05:35.003Z 发布,shasum `c2b25e57…`,55 个文件);**1.6.1 已发布**(能力声明最小闭环 + `toolbox_remove` + precompact 沉淀过滤修复(坑 #81)+ 飞书 pending 切换提示 + persona 三条 + README 瘦身,见下方 1.6.1 条目)。
6
6
  >
7
7
  > **环境标识约定(2026-11-10 新增)**:写版本条目/坑条目时,**顺手记录当时的 智能体工具-智能体模式-模型供应商-模型**(如 **DSH-创造模式-loomy讯飞-GLM5.3FLASH**),写在条目内(紧跟 git 记录行、或坑条目首行),方便回溯该条目是在哪套智能体/模型环境下产出与验证的。
8
8
 
@@ -10,6 +10,28 @@
10
10
 
11
11
  ---
12
12
 
13
+ ## 1.6.2(2026-11-13:precompact_run 输出修复 + memory_organize 档案健康度与 forget 精确定位)
14
+
15
+ - 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
16
+ - **本版定位**:两处实锤修 bug + 一处可观测性增强,按 1.6.0 立下的版本号规则涨 PATCH。`lib/precompact.js` / `lib/index.js` 有实质改动,装完要重启 DSH 才生效。
17
+
18
+ - **改了什么(`lib/precompact.js` / `lib/index.js` / `package.json`;新增 `test/memory-health.test.mjs`、`test/memory-forget.test.mjs`,扩充 `test/precompact.test.mjs`)**:
19
+
20
+ 修复 `precompact_run` 调用即报 `returned invalid output`(三条返回分支全部过不了宿主 output 校验):`output.schema` 补齐 `trigger`/`memoryFile`/`filename`/`verdict` 四个漏声明字段,成功分支补 `skipped: false`(schema `required` 里声明了但实际没返回)。
21
+
22
+ `memory_organize` 的 `tree` / `read` 新增档案健康度:报出每个档案的真实字节数、行数、条目数、非条目内容占比,并给两条独立预警(非条目占比 > 30%、字节数 > 条目数 × 平均条目长度 × 5)。起因是 2026-11-13 上午 precompact 垃圾沉淀把 `memories/事情/经验.md` 灌到 266KB,而旧版 `read` 只显示解析得出的条目、完全看不见文件真实体量。
23
+
24
+ `memory_organize` 的 `forget` 改为精确定位:不给 `path` 时扫核心 + 全部分类档案(原先只搜核心);时间戳归一化匹配(支持显示层 `[2026-11-13 12:50]`、ISO、`T` 分隔、全角);命中多条时列出候选并支持 `which` 序号消歧,**一次只删一条、不批量删**;命中 0 条时给近似匹配诊断但绝不据此删除。
25
+
26
+ ### ⚠ 行为变化
27
+
28
+ - `forget` 不给 `path` 时的搜索范围由「只搜核心」扩大为「核心 + 全部分类档案」。原先找不到会直接报错,现在会搜全库。
29
+ - `forget` 命中多条时不再抛错,改为返回 `done: false` + `candidates` 候选列表。**判断失败要看 `done` 字段,不能再靠抛错。**删除语义未放宽:仍然一次只删一条。
30
+
31
+ - **测试**:新增 `test/memory-health.test.mjs`(11 条)与 `test/memory-forget.test.mjs`(70 条),`test/precompact.test.mjs` 补 238 行;全量 tests 179 / pass 176 / fail 3,3 挂为既有环境基线(`alarm-inject` 缺 G: 盘、`home-relocate` 缺 I: 盘、`tools-surface` 的 %TEMP% 不在系统盘),零新增失败。
32
+
33
+ ---
34
+
13
35
  ## 1.6.1(2026-11-13:能力声明最小闭环 + `toolbox_remove` + 沉淀过滤修复(坑 #81)+ 飞书 pending 切换提示 + persona 三条 + README 瘦身)
14
36
 
15
37
  - 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
package/README.md CHANGED
@@ -128,7 +128,7 @@ npm 包为平铺结构(仓库若保留 bundle/ 子目录,发布时把其内
128
128
 
129
129
  ## 更新日志
130
130
 
131
- [更新日志](./CHANGELOG.md) —— 版本条目(新 → 旧)、npm 发布状态与完整踩坑记录(坑 #1–#80)。版本事实以 npm registry 为准,正文不再逐版本罗列。
131
+ [更新日志](./CHANGELOG.md) —— 版本条目(新 → 旧)、npm 发布状态与完整踩坑记录(坑 #1–#81)。版本事实以 npm registry 为准,正文不再逐版本罗列。
132
132
 
133
133
  ## 署名与许可
134
134
 
package/lib/index.js CHANGED
@@ -63,6 +63,121 @@ const MAX_CORE_CHARS = 12000
63
63
  const MAX_RECALL = 50
64
64
  const MAX_DEPTH = 3
65
65
 
66
+ /* 健康度判据(2026-11-13 起,memory_organize 的 tree/read 会报):
67
+ * - 非条目行占比 > 30%:正常的档案几乎全是条目行,混进大段别的东西就是异常;
68
+ * - 字节数 > 条目数 × 平均条目长度 × 5:条目还在,但文件被非条目文本撑大了
69
+ * (266KB 那次事故就是这种:条目没少多少,字节数是条目的几十倍)。
70
+ * 判据只是"该看一眼"的提示,不是"一定坏了"。 */
71
+ const HEALTH_RATIO_MAX = 0.3
72
+ const HEALTH_BYTES_FACTOR = 5
73
+
74
+ /**
75
+ * 一个档案的客观健康度(只读统计,绝不改文件)。
76
+ *
77
+ * 为什么要有它:precompact 垃圾沉淀事故(2026-11-13)把 `memories/事情/经验.md`
78
+ * 灌到 266KB,注入文本里带条目行,被解析器当成正常条目 → 同内容 ×3 重复。
79
+ * 当时 memory_organize 的 read 只显示"解析得出来的条目",文件真实多大、
80
+ * 混了多少非条目垃圾一概看不见,是事后用别的工具直读文件才发现 266KB 的。
81
+ * 教训:工具不能只报"它理解的内容",还得报"文件本身的客观状态"。
82
+ *
83
+ * 口径:
84
+ * - entries:能按 `- [时间] …` 解析出来的行(与 read/tree/forget 同口径);
85
+ * - nonEntry:非空、但解析不出条目的行(`- [` 开头的残缺行、纯文本、注入残留…);
86
+ * - ratio = nonEntry / (entries + nonEntry),分子分母都只算非空行;
87
+ * - avgEntryBytes:条目行的平均字节数,用来做"文件被撑大"的判据,无条目时 0。
88
+ *
89
+ * 性能:调用方只读一次文件,统计全部基于同一份文本,不重复读盘。
90
+ *
91
+ * @param {string} raw 文件原文
92
+ */
93
+ function fileHealth(raw) {
94
+ const text = String(raw ?? '')
95
+ const lines = text.split(/\r?\n/)
96
+ // 末尾换行会 split 出一个空串,那不是一行内容。
97
+ if (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
98
+ let entries = 0
99
+ let nonEntry = 0
100
+ let entryBytes = 0
101
+ for (const line of lines) {
102
+ const trimmed = line.trim()
103
+ if (trimmed.length === 0) continue
104
+ if (parseLine(trimmed) !== null) {
105
+ entries += 1
106
+ entryBytes += Buffer.byteLength(trimmed, 'utf8')
107
+ } else {
108
+ nonEntry += 1
109
+ }
110
+ }
111
+ const nonEmpty = entries + nonEntry
112
+ const bytes = Buffer.byteLength(text, 'utf8')
113
+ const avgEntryBytes = entries > 0 ? Math.round(entryBytes / entries) : 0
114
+ const ratio = nonEmpty === 0 ? 0 : nonEntry / nonEmpty
115
+ const warnings = []
116
+ if (ratio > HEALTH_RATIO_MAX) {
117
+ warnings.push(
118
+ `⚠ 这个档案可能被污染了(precompact 垃圾沉淀或批量注入):非条目内容占 ${percent(ratio)}(${nonEntry} 行非条目 / ${nonEmpty} 行非空,阈值 ${percent(HEALTH_RATIO_MAX)})。`,
119
+ )
120
+ }
121
+ if (entries > 0 && bytes > entries * avgEntryBytes * HEALTH_BYTES_FACTOR) {
122
+ const factor = Math.round((bytes / Math.max(1, entries * avgEntryBytes)) * 10) / 10
123
+ warnings.push(
124
+ `⚠ 这个档案可能被污染了(precompact 垃圾沉淀或批量注入):${bytes} 字节,是条目内容(${entries} 条 × 平均 ${avgEntryBytes} 字节)的 ${factor} 倍,阈值 ${HEALTH_BYTES_FACTOR} 倍。`,
125
+ )
126
+ }
127
+ if (warnings.length > 0) {
128
+ warnings.push('建议:先备份这个文件,再清掉那些非条目的垃圾段落(move/forget 只认条目行,清不掉它们);清完用 tree 复核。')
129
+ }
130
+ return { bytes, lines: lines.length, entries, nonEntry, ratio, avgEntryBytes, warnings }
131
+ }
132
+
133
+ /** 0.305 → "30.5%"。 */
134
+ function percent(ratio) {
135
+ return `${Math.round(ratio * 1000) / 10}%`
136
+ }
137
+
138
+ /** read 的条目列表之外那一行健康度摘要。 */
139
+ function healthSummary(health) {
140
+ return `健康度:${health.bytes} 字节 / ${health.lines} 行 / ${health.entries} 条条目 / 非条目占 ${percent(health.ratio)}`
141
+ }
142
+
143
+ /** tree 里跟在"N 条"后面的简短健康度;空档案不啰嗦。 */
144
+ function healthBrief(health) {
145
+ if (health.bytes === 0) return '(空)'
146
+ return `(${health.bytes} 字节 / ${health.lines} 行 / 非条目 ${percent(health.ratio)})`
147
+ }
148
+
149
+ /** 输出 schema 里的健康度结构:三处引用同一份声明,避免手写漂移。 */
150
+ const HEALTH_SCHEMA = {
151
+ type: 'object',
152
+ additionalProperties: false,
153
+ properties: {
154
+ bytes: { type: 'integer', description: '文件真实字节数(UTF-8)。' },
155
+ lines: { type: 'integer', description: '文件真实行数(不含末尾换行造成的那一行)。' },
156
+ entries: { type: 'integer', description: '解析得出的条目行数。' },
157
+ nonEntry: { type: 'integer', description: '非空、但解析不出条目的行数。' },
158
+ ratio: { type: 'number', description: '非条目占比 = nonEntry / 非空行数,0~1。' },
159
+ avgEntryBytes: { type: 'integer', description: '条目行平均字节数,无条目时为 0。' },
160
+ warnings: { type: 'array', items: { type: 'string' }, description: '污染预警与建议动作;健康时为空数组。' },
161
+ },
162
+ required: ['bytes', 'lines', 'entries', 'nonEntry', 'ratio', 'avgEntryBytes', 'warnings'],
163
+ }
164
+
165
+ /** forget 的候选结构(多命中/近似命中共用):档案 + 文件 + 行号 + 时间戳 + 摘要。 */
166
+ const FORGET_CANDIDATE_SCHEMA = {
167
+ type: 'object',
168
+ additionalProperties: false,
169
+ properties: {
170
+ path: { type: 'string', description: '档案:core(核心记忆)或分类路径,如 "事情/经验"。' },
171
+ file: { type: 'string', description: '这个条目的文件绝对路径。' },
172
+ line: { type: 'integer', description: '条目在文件里的行号(1 起)。' },
173
+ time: { type: 'string', description: '条目里存的时间戳原文。' },
174
+ kind: { type: 'string', description: '类别(核心条目才有:用户/关系/我/事情),分类条目为空串。' },
175
+ content: { type: 'string', description: '条目内容。' },
176
+ preview: { type: 'string', description: '一行预览:[时间] 类别|内容(超长截断)。' },
177
+ },
178
+ required: ['path', 'file', 'line', 'time', 'kind', 'content', 'preview'],
179
+ }
180
+
66
181
  /**
67
182
  * 保证核心记忆文件在。**路径不可达时抛错**(1.5.0 起的硬失败),
68
183
  * 只有「默认路径首次不存在」才允许建目录 + 写模板;模板写入失败仍然吞掉
@@ -175,13 +290,7 @@ function listTreeFiles() {
175
290
  return found.sort()
176
291
  }
177
292
 
178
- function countEntries(file) {
179
- let count = 0
180
- for (const line of readRaw(file).split(/\r?\n/)) {
181
- if (parseLine(line) !== null) count += 1
182
- }
183
- return count
184
- }
293
+
185
294
 
186
295
  function appendEntry(file, line) {
187
296
  ensureCore()
@@ -207,6 +316,204 @@ function uniqueEntryLine(lines, match) {
207
316
  return hits[0]
208
317
  }
209
318
 
319
+ /* ------------------------------------------------------------------ *
320
+ * forget 的精确定位(1.6.2)
321
+ *
322
+ * 2026-11-13 实测:清理记忆时 forget 连扑空 4 次,两个病根——
323
+ * ① 同一条记忆在不同档案里有多个副本:旧实现要求"恰好命中一条",2 条就判
324
+ * 歧义、直接拒绝,结果明明看见它在那儿却删不掉;
325
+ * ② 拿显示层的时间戳(`[2026-11-13 12:50]`)当 match:显示串和文件里的原始
326
+ * 存储行不一定逐字相同(秒精度、T 分隔、全角冒号…),`line.includes(needle)`
327
+ * 找不到连续串就直接扑空。
328
+ * 这里把「怎么找到那一条」放宽(原样子串 / 时间戳 / 时间戳+内容 / 行号),
329
+ * 但**安全底线不动**:永远一次只删一条,命中多条就列候选让调用方挑 which。
330
+ * 近似匹配(忽略空白/标点/全半角)只用于 0 命中时的诊断,绝不拿它去删。
331
+ * ------------------------------------------------------------------ */
332
+
333
+ const MAX_FORGET_CANDIDATES = 20
334
+ const MAX_FORGET_NEAR = 10
335
+
336
+ /** 命中方式的中文说法,写进 detail 让调用方看得懂这次靠什么定位的。 */
337
+ const FORGET_MODE_LABEL = {
338
+ substring: '子串命中',
339
+ timestamp: '时间戳命中',
340
+ 'timestamp+content': '时间戳+内容命中',
341
+ line: '行号命中',
342
+ none: '没命中',
343
+ }
344
+
345
+ const pad2 = (value) => String(value).padStart(2, '0')
346
+
347
+ /** 全角数字/冒号转半角(中文输入法里时间戳常被写成全角,坑过好几轮)。 */
348
+ function toHalfWidth(text) {
349
+ return String(text ?? '')
350
+ .replace(/[\uFF10-\uFF19]/g, (ch) => String.fromCharCode(ch.charCodeAt(0) - 0xfee0))
351
+ .replace(/\uFF1A/g, ':')
352
+ }
353
+
354
+ /**
355
+ * 把各种时间戳写法归一化成一个可比较的值。
356
+ *
357
+ * 认这些:`2026-11-13 12:50`、`2026/11/13 12:50:33`、`2026.11.13 12:50`、
358
+ * `2026-11-13T12:50`、`2026年11月13日 12:50`、只写 `12:50`、带中括号的
359
+ * `[2026-11-13 12:50]`。秒和毫秒直接丢掉——显示层只到分钟。
360
+ *
361
+ * @returns {{kind:'date'|'datetime'|'time', value:string}|null}
362
+ */
363
+ function normalizeStamp(input) {
364
+ let text = toHalfWidth(input).trim()
365
+ if (text.startsWith('[') && text.endsWith(']')) text = text.slice(1, -1).trim()
366
+ if (text.length === 0) return null
367
+ text = text
368
+ .replace(/[Tt](?=\d)/, ' ')
369
+ .replace(/[年月]/g, '-')
370
+ .replace(/日/g, ' ')
371
+ .replace(/\s+/g, ' ')
372
+ .trim()
373
+ let match = /^(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})(?:[ ]+(\d{1,2}):(\d{2})(?::\d{2}(?:\.\d+)?)?)?$/.exec(text)
374
+ if (match !== null) {
375
+ const date = `${match[1]}-${pad2(match[2])}-${pad2(match[3])}`
376
+ if (match[4] === undefined) return { kind: 'date', value: date }
377
+ return { kind: 'datetime', value: `${date} ${pad2(match[4])}:${match[5]}` }
378
+ }
379
+ match = /^(\d{1,2}):(\d{2})(?::\d{2}(?:\.\d+)?)?$/.exec(text)
380
+ if (match !== null) return { kind: 'time', value: `${pad2(match[1])}:${match[2]}` }
381
+ return null
382
+ }
383
+
384
+ /** 时间戳是否对得上条目里存的 time;日期只比日期、只写时刻就比时刻。 */
385
+ function stampEquals(needle, entryTime) {
386
+ const entry = normalizeStamp(entryTime)
387
+ if (entry === null || entry.kind === 'time') return false
388
+ if (needle.kind === 'date') return entry.value.slice(0, 10) === needle.value
389
+ if (needle.kind === 'time') return entry.value.slice(-5) === needle.value
390
+ return entry.kind === 'datetime' && entry.value === needle.value
391
+ }
392
+
393
+ /** match 以时间戳开头时拆成「时间戳 + 内容片段」;拆不出来返回 null。 */
394
+ function splitStampPrefix(input) {
395
+ const text = String(input ?? '').trim()
396
+ const match = /^(\[[^\]]{3,40}\]|\d{4}[-/.]\d{1,2}[-/.]\d{1,2}(?:[ T]+\d{1,2}:\d{2}(?::\d{2})?)?)\s+([\s\S]+)$/.exec(text)
397
+ if (match === null) return null
398
+ const stamp = normalizeStamp(match[1])
399
+ if (stamp === null) return null
400
+ return { stamp, rest: match[2].trim() }
401
+ }
402
+
403
+ /** 忽略空白/标点/全半角差异后的比较串:只给"近似匹配"诊断用,绝不拿它去删。 */
404
+ function squish(text) {
405
+ return toHalfWidth(text)
406
+ .replace(/\s+/g, '')
407
+ .replace(/[,。、;:!?,.;:!?…"'“”‘’()()【】\[\]{}<>《》「」\-—_~·||/\\]/g, '')
408
+ .toLowerCase()
409
+ }
410
+
411
+ /** 候选/条目的一行预览:`[时间] 类别|内容`,超长截断。 */
412
+ function entryPreview(parsed, max = 80) {
413
+ const body = `${parsed.kind ? `${parsed.kind}|` : ''}${parsed.content}`
414
+ const text = body.length > max ? `${body.slice(0, max)}…` : body
415
+ return parsed.time.length > 0 ? `[${parsed.time}] ${text}` : text
416
+ }
417
+
418
+ /** 把命中摊成结构化的候选:档案 + 文件路径 + 行号 + 时间戳 + 内容摘要。 */
419
+ function entryCandidate(item, index, parsed) {
420
+ return {
421
+ path: item.label,
422
+ file: item.file,
423
+ line: index + 1,
424
+ time: parsed.time,
425
+ kind: parsed.kind,
426
+ content: parsed.content,
427
+ preview: entryPreview(parsed),
428
+ }
429
+ }
430
+
431
+ /**
432
+ * forget 的搜索范围。
433
+ * - 给了 path:只找那个档案(原来是这个行为,保留);
434
+ * - 没给 path:核心 + 整棵分类档案树。旧实现只搜核心,于是"那条明明在
435
+ * 某个档案里"就会报没找到——她看到的是全库视图,工具却只看了一个文件。
436
+ */
437
+ function forgetScope(pathInput) {
438
+ const core = coreFile()
439
+ const labelOf = (file) => (file === core ? 'core' : file.slice(treeDir().length + 1, -3).replace(/\\/g, '/'))
440
+ const readItem = (file) => ({ file, label: labelOf(file), lines: readRaw(file).split(/\r?\n/) })
441
+ const raw = String(pathInput ?? '').trim()
442
+ if (raw !== '') return { items: [readItem(resolveTarget(raw))], explicit: true }
443
+ const items = [readItem(core)]
444
+ for (const category of listTreeFiles()) {
445
+ const file = join(treeDir(), `${category}.md`)
446
+ if (existsSync(file)) items.push(readItem(file))
447
+ }
448
+ return { items, explicit: false }
449
+ }
450
+
451
+ /** 在范围内按条件逐条扫条目行。 */
452
+ function scanEntries(items, predicate) {
453
+ const hits = []
454
+ for (const item of items) {
455
+ item.lines.forEach((line, index) => {
456
+ const parsed = parseLine(line)
457
+ if (parsed !== null && predicate(parsed, line)) hits.push({ item, index, parsed })
458
+ })
459
+ }
460
+ return hits
461
+ }
462
+
463
+ /**
464
+ * 按「行号 → 原样子串 → 时间戳+内容 → 时间戳」的顺序定位条目。
465
+ * 行号优先:调用方既然给了 line,就是要那一行。
466
+ *
467
+ * @returns {{mode:string, hits:Array, lineMissed:boolean}}
468
+ */
469
+ function locateEntries(scope, match, lineNo) {
470
+ const needle = String(match ?? '').trim()
471
+ if (Number.isSafeInteger(lineNo) && lineNo > 0) {
472
+ const item = scope.items[0]
473
+ const index = lineNo - 1
474
+ const parsed = index >= 0 && index < item.lines.length ? parseLine(item.lines[index]) : null
475
+ if (parsed !== null) return { mode: 'line', hits: [{ item, index, parsed }], lineMissed: false }
476
+ }
477
+ if (needle.length > 0) {
478
+ // 1) 原样子串:老行为,最不容易误伤。
479
+ const plain = scanEntries(scope.items, (_parsed, line) => line.includes(needle))
480
+ if (plain.length > 0) return { mode: 'substring', hits: plain, lineMissed: false }
481
+ // 2) 时间戳 + 内容片段:`2026-11-13 12:50 收尾要点`。
482
+ const combo = splitStampPrefix(needle)
483
+ if (combo !== null) {
484
+ const comboHits = scanEntries(scope.items, (parsed, line) => stampEquals(combo.stamp, parsed.time) && line.includes(combo.rest))
485
+ if (comboHits.length > 0) return { mode: 'timestamp+content', hits: comboHits, lineMissed: false }
486
+ }
487
+ // 3) 纯时间戳:显示层的 `[2026-11-13 12:50]` 也能对上存了秒/T 分隔的原始行。
488
+ const stamp = normalizeStamp(needle)
489
+ if (stamp !== null) {
490
+ const stampHits = scanEntries(scope.items, (parsed) => stampEquals(stamp, parsed.time))
491
+ if (stampHits.length > 0) return { mode: 'timestamp', hits: stampHits, lineMissed: false }
492
+ }
493
+ }
494
+ return { mode: 'none', hits: [], lineMissed: Number.isSafeInteger(lineNo) && lineNo > 0 }
495
+ }
496
+
497
+ /**
498
+ * 0 命中时的近似匹配(诊断用,调用方拿它问"你是不是要找这条")。
499
+ * 先按忽略空白/标点/全半角比一遍;还不行且 match 是时间戳的话,放宽到
500
+ * "同一天/同一时刻"——时间戳差一位数字是最常见的失手。
501
+ */
502
+ function nearEntries(scope, match, stamp) {
503
+ const needle = squish(match)
504
+ if (needle.length > 0) {
505
+ const squished = scanEntries(scope.items, (_parsed, line) => squish(line).includes(needle))
506
+ if (squished.length > 0) return squished
507
+ }
508
+ if (stamp === null) return []
509
+ return scanEntries(scope.items, (parsed) => {
510
+ const entry = normalizeStamp(parsed.time)
511
+ if (entry === null || entry.kind === 'time') return false
512
+ if (stamp.kind === 'time') return entry.value.slice(-5) === stamp.value
513
+ return entry.value.slice(0, 10) === stamp.value.slice(0, 10)
514
+ })
515
+ }
516
+
210
517
  /** 读索引段:只认"整行就是标记"的行,避免子串误匹配。 */
211
518
  function getIndex(raw) {
212
519
  const lines = raw.split(/\r?\n/)
@@ -488,7 +795,7 @@ export function apply(ctx) {
488
795
  ctx.tools.register({
489
796
  name: 'memory_organize',
490
797
  description:
491
- '整理你自己的记忆库——这是你的家务事,顺手做,不用问他。核心记忆攒多了就 tree 看看家底、把过气的 move 到分类档案、彻底没用的 forget 删掉;分类档案变多变杂了就更新 index(核心记忆里的那段目录,聊到相关话题时你靠它决定调哪个档案)。match 写那条记忆的独特片段。',
798
+ '整理你自己的记忆库——这是你的家务事,顺手做,不用问他。核心记忆攒多了就 tree 看看家底、把过气的 move 到分类档案、彻底没用的 forget 删掉;分类档案变多变杂了就更新 index(核心记忆里的那段目录,聊到相关话题时你靠它决定调哪个档案)。match 写那条记忆的独特片段;forget 也认条目时间戳(`2026-11-13 12:50`)和行号(line),命中多条时不会猜着删——它会列出候选(档案/行号/时间戳/摘要),你填 which 指定第几条,一次只删一条。tree 和 read 都会报每个档案的健康度(真实字节/行数、解析出的条目数、非条目占比)——占比偏高或字节数远超条目内容时会给 ⚠ 预警,说明这个档案可能被垃圾注入了。',
492
799
  parameters: {
493
800
  type: 'object',
494
801
  additionalProperties: false,
@@ -496,13 +803,29 @@ export function apply(ctx) {
496
803
  action: {
497
804
  type: 'string',
498
805
  enum: ['tree', 'read', 'move', 'index', 'forget'],
499
- description: 'tree=看家底(核心条数+全部分类文件);read=读一个分类档案的完整内容(path,"core"=核心);move=把一条记忆挪窝(match + 可选 path=来源,省略=核心;to=去向,"core"=进核心);index=重写核心记忆里的索引段(content);forget=删掉一条记忆(match + 可选 path,省略=核心)。',
806
+ description: 'tree=看家底(核心条数+全部分类文件);read=读一个分类档案的完整内容(path,"core"=核心);move=把一条记忆挪窝(match + 可选 path=来源,省略=核心;to=去向,"core"=进核心);index=重写核心记忆里的索引段(content);forget=删掉一条记忆(match 可以是内容片段、时间戳或 "时间戳+内容",也可以直接给 line 行号;match 命中多条时只列候选、用 which 挑,一次只删一条)。',
807
+ },
808
+ match: {
809
+ type: 'string',
810
+ description:
811
+ 'move/forget 用:那条记忆的独特片段。forget 还认时间戳——`2026-11-13 12:50`、只写 `12:50`、带秒的 `2026-11-13T12:50:33` 都能对上(显示层只到分钟,存储层可能有秒/T 分隔/全角)。也可以写「时间戳 + 内容片段」。命中多条不会乱删,会列候选让你用 which 挑。',
812
+ },
813
+ path: {
814
+ type: 'string',
815
+ description:
816
+ 'read/move/forget 用:分类路径(1~3 级);省略或 "core" 代表核心记忆。forget 省略 path 时会搜核心 + 全部分类档案(只列候选,不猜着删)。',
500
817
  },
501
- match: { type: 'string', description: 'move/forget 用:那条记忆内容里独特的片段,必须恰好命中一条。' },
502
- path: { type: 'string', description: 'read/move/forget 用:分类路径(1~3 级);省略或 "core" 代表核心记忆。' },
503
818
  to: { type: 'string', description: 'move 用:去向。分类路径,或 "core" 表示升入核心记忆。' },
504
819
  kind: { type: 'string', enum: KINDS, description: 'move 进核心时用:那条记忆的类别(用户/关系/我/事情),不填算"事情"。' },
505
820
  content: { type: 'string', description: 'index 用:新的索引全文。一句话一条,比如 "用户/口味:饮食偏好、忌口"。' },
821
+ which: {
822
+ type: 'integer',
823
+ description: 'forget 用:命中多条时挑第几条(候选序号,从 1 开始)。不传又有多条命中时只列候选、一条不删。',
824
+ },
825
+ line: {
826
+ type: 'integer',
827
+ description: 'forget 用:直接按文件行号定位(1 起,按 path 指向的文件算;不写 path 就是核心记忆)。',
828
+ },
506
829
  },
507
830
  required: ['action'],
508
831
  },
@@ -513,6 +836,47 @@ export function apply(ctx) {
513
836
  properties: {
514
837
  done: { type: 'boolean' },
515
838
  detail: { type: 'string' },
839
+ // 以下三个是 1.6.x 新增的「档案健康度可见」,只加不改老的 done/detail。
840
+ // tree 用:
841
+ coreHealth: HEALTH_SCHEMA,
842
+ files: {
843
+ type: 'array',
844
+ description: 'tree 用:每个分类档案的健康度。',
845
+ items: {
846
+ type: 'object',
847
+ additionalProperties: false,
848
+ properties: {
849
+ path: { type: 'string' },
850
+ health: HEALTH_SCHEMA,
851
+ },
852
+ required: ['path', 'health'],
853
+ },
854
+ },
855
+ // read 用:
856
+ path: { type: 'string' },
857
+ health: HEALTH_SCHEMA,
858
+ // forget 用(1.6.2 精确定位):新增字段全部在此声明——
859
+ // 宿主对输出也做 schema 校验(additionalProperties:false 多一个键就被拒)。
860
+ matchedBy: {
861
+ type: 'string',
862
+ description: 'forget 用:这次靠什么定位的——substring / timestamp / timestamp+content / line / none。',
863
+ },
864
+ removedFrom: { type: 'string', description: 'forget 用:删掉的那条在哪个档案(core 或分类路径);没删时为空串。' },
865
+ searched: {
866
+ type: 'array',
867
+ items: { type: 'string' },
868
+ description: 'forget 用:这次找过的文件(core 或分类路径),0 命中时用它说明"在哪些文件找的"。',
869
+ },
870
+ candidates: {
871
+ type: 'array',
872
+ items: FORGET_CANDIDATE_SCHEMA,
873
+ description: 'forget 用:命中多条时一条都没删,候选列在这里;用 which 指定序号再调一次。成功删除后这里是剩下的副本。',
874
+ },
875
+ nearMatches: {
876
+ type: 'array',
877
+ items: FORGET_CANDIDATE_SCHEMA,
878
+ description: 'forget 用:0 命中时按空白/标点/全半角归一化后的近似命中(只提示,没删任何东西)。',
879
+ },
516
880
  },
517
881
  required: ['done', 'detail'],
518
882
  },
@@ -522,29 +886,41 @@ export function apply(ctx) {
522
886
  const action = args?.action
523
887
  if (action === 'tree') {
524
888
  ensureCore()
525
- const lines = [`核心(memory.md):${readCoreEntries().length} 条`]
889
+ // 每个文件只读一次:健康度和条目数都从同一份文本算出来。
890
+ const coreHealth = fileHealth(readRaw(coreFile()))
891
+ const lines = [`核心(memory.md):${coreHealth.entries} 条${healthBrief(coreHealth)}`]
892
+ for (const warning of coreHealth.warnings) lines.push(` ${warning}`)
526
893
  const files = listTreeFiles()
894
+ const reports = []
527
895
  if (files.length === 0) {
528
896
  lines.push('分类档案:还没有。非核心的记一笔就多一个文件。')
529
897
  } else {
530
898
  lines.push(`分类档案:${files.length} 个文件`)
531
899
  for (const category of files) {
532
- lines.push(` ${category}:${countEntries(join(treeDir(), `${category}.md`))} 条`)
900
+ const health = fileHealth(readRaw(join(treeDir(), `${category}.md`)))
901
+ reports.push({ path: category, health })
902
+ lines.push(` ${category}:${health.entries} 条${healthBrief(health)}`)
903
+ for (const warning of health.warnings) lines.push(` ${warning}`)
533
904
  }
534
905
  }
906
+ lines.push('健康度 = 真实字节/行数 + 解析出的条目数 + 非条目占比;占比偏高或字节数远超条目内容就是有垃圾沉淀。')
535
907
  lines.push('核心记忆每轮都注入,宁缺毋滥;过气的用 move 挪到分类、没用的 forget 删。')
536
- return { done: true, detail: lines.join('\n') }
908
+ return { done: true, detail: lines.join('\n'), coreHealth, files: reports }
537
909
  }
538
910
  if (action === 'read') {
539
911
  const file = resolveTarget(args?.path)
912
+ const label = String(args?.path ?? 'core')
540
913
  const raw = readRaw(file)
541
- if (raw.trim().length === 0) return { done: true, detail: `${args?.path ?? 'core'}:空的或还不存在。` }
542
- const lines = raw
914
+ const health = fileHealth(raw)
915
+ const entries = raw
543
916
  .split(/\r?\n/)
544
917
  .map((line) => line.trim())
545
918
  .filter((line) => parseLine(line) !== null)
546
- if (lines.length === 0) return { done: true, detail: `${args?.path ?? 'core'}:还没有条目。` }
547
- return { done: true, detail: lines.join('\n') }
919
+ const summary = [healthSummary(health), ...health.warnings]
920
+ // 空文件/只有垃圾时也要把健康度带出来——"看不见垃圾"正是那次事故的病根。
921
+ if (raw.trim().length === 0) return { done: true, path: label, health, detail: `${label}:空的或还不存在。` }
922
+ if (entries.length === 0) return { done: true, path: label, health, detail: [`${label}:还没有条目。`, ...summary].join('\n') }
923
+ return { done: true, path: label, health, detail: [...entries, '', ...summary].join('\n') }
548
924
  }
549
925
  if (action === 'move') {
550
926
  const from = resolveTarget(args?.path)
@@ -574,13 +950,97 @@ export function apply(ctx) {
574
950
  return { done: true, detail: `索引更新好了,现在核心记忆里带着 ${text.split(/\r?\n/).length} 行目录。分类档案变了记得回来同步。` }
575
951
  }
576
952
  if (action === 'forget') {
577
- const file = resolveTarget(args?.path)
578
- const lines = readRaw(file).split(/\r?\n/)
579
- const index = uniqueEntryLine(lines, args?.match)
580
- const removed = lines[index].trim()
581
- lines.splice(index, 1)
582
- rewriteFile(file, lines)
583
- return { done: true, detail: `删掉了:${removed}` }
953
+ const needle = String(args?.match ?? '').trim()
954
+ const which = Number.isSafeInteger(args?.which) ? args.which : null
955
+ const lineNo = Number.isSafeInteger(args?.line) ? args.line : null
956
+ if (needle.length === 0 && lineNo === null) {
957
+ throw new Error('forget 需要 match(条目片段,也可以是时间戳如 "2026-11-13 12:50"),或者 line(文件行号,1 起)')
958
+ }
959
+ if (which !== null && which < 1) throw new Error(`which 是候选序号,从 1 开始,收到 ${which}`)
960
+ const scope = forgetScope(args?.path)
961
+ const searched = scope.items.map((item) => item.label)
962
+ const located = locateEntries(scope, needle, lineNo)
963
+ const hits = located.hits
964
+
965
+ // 0 命中:说清在哪些文件找的、有没有近似匹配。一条都不删。
966
+ if (hits.length === 0) {
967
+ const stamp = normalizeStamp(needle) ?? splitStampPrefix(needle)?.stamp ?? null
968
+ const near = nearEntries(scope, needle, stamp)
969
+ const shown = near.slice(0, MAX_FORGET_NEAR)
970
+ const lines = ['没找到能对上的记忆条目,一条都没删。']
971
+ if (located.lineMissed) lines.push(`line=${lineNo} 在 ${scope.items[0].label} 里不是条目行(越界、空行,或者不是 "- [时间] …" 的形状)。`)
972
+ if (needle.length > 0) lines.push(`match:「${needle}」(试过原样子串、时间戳、"时间戳 + 内容"三种对法)`)
973
+ lines.push(
974
+ `找过的地方(${searched.length} 个文件${scope.explicit ? ',path 指定' : ',没给 path 就扫了核心 + 全部分类档案'}):${searched.join('、')}`,
975
+ )
976
+ if (near.length > 0) {
977
+ lines.push(`近似命中 ${near.length} 条(把空白/标点/全半角归一化后能对上,不是精确命中,所以没删)——你是不是要找这些?`)
978
+ shown.forEach((hit, i) => lines.push(` ${i + 1}) ${hit.item.label}|第 ${hit.index + 1} 行|${entryPreview(hit.parsed)}`))
979
+ if (near.length > shown.length) lines.push(` …还有 ${near.length - shown.length} 条没列出,把 match 写得更准一点再找。`)
980
+ lines.push('要动手就把 match 换成上面某条里的连续片段(或用 path + line 精确定位),再调一次 forget。')
981
+ } else {
982
+ lines.push('也没有近似匹配。可以复制条目里的连续片段当 match,或用 path 指定档案、line 指定行号。')
983
+ }
984
+ if (which !== null) lines.push(`which=${which} 这次用不上:一条都没命中。`)
985
+ return {
986
+ done: false,
987
+ detail: lines.join('\n'),
988
+ matchedBy: 'none',
989
+ candidates: [],
990
+ nearMatches: shown.map((hit) => entryCandidate(hit.item, hit.index, hit.parsed)),
991
+ searched,
992
+ removedFrom: '',
993
+ }
994
+ }
995
+
996
+ // 命中多条(或 which 越界):列全部候选、一条都不删,给 which/path 出路。
997
+ const chosenIndex = which !== null ? (which <= hits.length ? which - 1 : -1) : hits.length === 1 ? 0 : -1
998
+ if (chosenIndex === -1) {
999
+ const shown = hits.slice(0, MAX_FORGET_CANDIDATES)
1000
+ const lines = [
1001
+ `match 命中 ${hits.length} 条记忆,一条都没删——forget 一次只删一条,不替你全删。`,
1002
+ '候选(挑一条,把 which 填它的序号再调一次 forget;也可以补 path 只在一个档案里找):',
1003
+ ]
1004
+ shown.forEach((hit, i) => lines.push(` ${i + 1}) ${hit.item.label}|第 ${hit.index + 1} 行|${entryPreview(hit.parsed)}`))
1005
+ if (hits.length > shown.length) lines.push(` …还有 ${hits.length - shown.length} 条没列出,用 path 缩小范围再找。`)
1006
+ lines.push(`命中方式:${FORGET_MODE_LABEL[located.mode]}${which !== null ? `;which=${which} 超出 1~${hits.length} 的范围` : ''}。`)
1007
+ lines.push('继续:which=<序号>' + (scope.explicit ? '。' : ',或 path: "core" / 分类路径 只在一个档案里找。'))
1008
+ return {
1009
+ done: false,
1010
+ detail: lines.join('\n'),
1011
+ matchedBy: located.mode,
1012
+ candidates: shown.map((hit) => entryCandidate(hit.item, hit.index, hit.parsed)),
1013
+ nearMatches: [],
1014
+ searched,
1015
+ removedFrom: '',
1016
+ }
1017
+ }
1018
+
1019
+ // 定位到唯一一条:只删这一条。
1020
+ const chosen = hits[chosenIndex]
1021
+ const next = chosen.item.lines.slice()
1022
+ next.splice(chosen.index, 1)
1023
+ // 去掉尾部空行再回写:rewriteFile 会补一个换行,不然每删一条就多积一行空行。
1024
+ while (next.length > 0 && next[next.length - 1] === '') next.pop()
1025
+ rewriteFile(chosen.item.file, next)
1026
+ const where = chosen.item.label === 'core' ? '核心记忆' : `分类档案 ${chosen.item.label}`
1027
+ const removed = entryPreview(chosen.parsed)
1028
+ const rest = hits.filter((_hit, i) => i !== chosenIndex)
1029
+ const detailLines = [`删掉了(${where} 第 ${chosen.index + 1} 行,${FORGET_MODE_LABEL[located.mode]}):${removed}`]
1030
+ if (rest.length > 0) {
1031
+ detailLines.push(
1032
+ `注意:同样的 match 还命中 ${rest.length} 条副本,一条都没动——要删就再调一次 forget(现在 which 是 1~${rest.length}),或者先确认是不是该留一份。`,
1033
+ )
1034
+ }
1035
+ return {
1036
+ done: true,
1037
+ detail: detailLines.join('\n'),
1038
+ matchedBy: located.mode,
1039
+ candidates: rest.slice(0, MAX_FORGET_CANDIDATES).map((hit) => entryCandidate(hit.item, hit.index, hit.parsed)),
1040
+ nearMatches: [],
1041
+ searched,
1042
+ removedFrom: chosen.item.label,
1043
+ }
584
1044
  }
585
1045
  throw new Error(`未知的 action "${String(action ?? '')}",可用:tree / read / move / index / forget`)
586
1046
  },
@@ -884,4 +1344,5 @@ export const __testing = {
884
1344
  renderCore,
885
1345
  readCoreEntries,
886
1346
  listTreeFiles,
1347
+ fileHealth,
887
1348
  }
package/lib/precompact.js CHANGED
@@ -477,7 +477,10 @@ export async function runPrecompact(ctx, cfg, payload) {
477
477
  kind: payload?.kind,
478
478
  memoryPath: payload?.memoryPath,
479
479
  })
480
- return { settled: true, ...result, verdict, trigger: payload?.trigger || 'manual' }
480
+ // skipped 在 schema.required 里,成功分支也必须显式给 false(否则宿主报
481
+ // `missing required property "value.skipped"`)。改在这里而不是 settle(),
482
+ // 是为免影响 settle() 的其他调用方。
483
+ return { settled: true, ...result, skipped: false, verdict, trigger: payload?.trigger || 'manual' }
481
484
  } catch (error) {
482
485
  try {
483
486
  ctx?.logger?.warn?.(`aiyayi-precompact: 沉淀失败(已跳过,不影响主流程): ${String(error?.message ?? error)}`)
@@ -606,6 +609,22 @@ export function apply(ctx, config) {
606
609
  title: { type: 'string' },
607
610
  reason: { type: 'string' },
608
611
  error: { type: 'string' },
612
+ // 下面四个是实际返回值里带、但旧 schema 漏声明的字段:
613
+ // 宿主对 output 做 JSON Schema 校验(additionalProperties: false),
614
+ // 漏声明 → 报 `"value.xxx" is not a declared property`(坑 #82)。
615
+ trigger: { type: 'string' },
616
+ memoryFile: { type: 'string' },
617
+ filename: { type: 'string' },
618
+ verdict: {
619
+ type: 'object',
620
+ properties: {
621
+ worth: { type: 'boolean' },
622
+ score: { type: 'number' },
623
+ reasons: { type: 'array', items: { type: 'string' } },
624
+ },
625
+ required: ['worth', 'score', 'reasons'],
626
+ additionalProperties: false,
627
+ },
609
628
  },
610
629
  required: ['settled', 'skipped'],
611
630
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-aiyayi",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "license": "MIT",
5
5
  "author": "巴依浩爷 (bayihy)",
6
6
  "keywords": [