dsh-zdiary-kit 3.7.0 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -4
- package/dist/cli.js +2 -0
- package/dist/features/zk-tools/feature.js +8 -2
- package/dist/lib/dream.js +16 -10
- package/dist/lib/lint.js +5 -0
- package/dist/lib/wikilink.js +18 -1
- package/dist/lib/zk.js +29 -4
- package/docs/features/memory.md +5 -1
- package/docs/features/zk-tools.md +13 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# dsh-zdiary-kit 3.
|
|
1
|
+
# dsh-zdiary-kit 3.8.0
|
|
2
2
|
|
|
3
3
|
> 给 [DSH](https://github.com/deepseek-ai/deepseek-harness) 装一个**内置 [zk](https://github.com/zk-org/zk) 的知识库 / 日记本**:
|
|
4
4
|
> 插件提供一键安装 zk(官方 release;设置页按钮或 `zk-kit install`)、自动建笔记本(zk init + 日记模板 + 日记 group),
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
| `zk_supersede` | 废弃过时结论:标 `superseded` + 写 `reason` + 搬进 `archive/` + 重写能精确匹配的 `[[旧路径]]` 入链(其它写法列进 `missingLinks`);默认只预演;meta 文件不能废弃 |
|
|
21
21
|
| `zk_rename` | 改名 / 换目录并修断链:入链(`[[…]]` 与 Markdown 链接,保留 `#锚点` 与 `\|别名`)按实际解析结果改到新路径,根目录 meta 里的 `[[来源]]` 一起改,被搬走那篇自己的相对 Markdown 链接按新位置重算;默认只预演 |
|
|
22
22
|
| `zk_context` | 读知识库根目录的 `AGENTS.md` / `USER.md` / `MEMORY.md`(按注入预算截断)与角色人格 `PERSONA.md`(单独成段、不截断),并列出已配置的笔记本(名字 / 路径 / 结构 / 当前);宿主支持注入时这些内容每轮已自动带进系统提示。改 MEMORY 前用它看余量:`headroom` / `wouldTruncate` / `dropCandidates` 说明「再加一条会丢谁」,给 `proposed_entry` 或 `extra_words` 就能预估;同日条目按位置决胜(`sameDateCount`),不假装有使用频率 |
|
|
23
|
-
| `zk_lint` | 体检:解析失败 / 断链 / 孤儿 / 无标签 / 单向链接 / 陈旧 / 重名标题 / 疑似重复标题 / 记忆链接失效 / frontmatter 缺字段 / 标签卫生 / 模板版本 / 人格(缺失·旧版·比插件新·标记与内容不符·未填占位符)/ 日记时间轴(最近 90 天的空块·倒序·格式·位置),每类最多 50 条;非日记笔记到 150 篇且没有 `INDEX.md`
|
|
24
|
-
| `zk_dream` | 晋升材料:窗口新增(机械筛掉 `informal` 与无标签,带正文摘要与入链来源)、已在 MEMORY 的、窗口外积压、daily
|
|
23
|
+
| `zk_lint` | 体检:解析失败 / 断链 / 孤儿 / 无标签 / 单向链接 / 陈旧 / 重名标题 / 疑似重复标题 / 记忆链接失效 / frontmatter 缺字段 / 标签卫生 / 模板版本 / 人格(缺失·旧版·比插件新·标记与内容不符·未填占位符)/ 日记时间轴(最近 90 天的空块·倒序·格式·位置),每类最多 50 条;非日记笔记到 150 篇且没有 `INDEX.md` 时提示一句;另给 `missingId`(缺稳定 id 的非日记笔记数,只提示不回填);只报告不改写 |
|
|
24
|
+
| `zk_dream` | 晋升材料:窗口新增(机械筛掉 `informal` 与无标签,带正文摘要与入链来源)、已在 MEMORY 的、窗口外积压、daily 未完成项、被引最多(先按「被日记提到几天」排);带 `missingId`(缺稳定 id 的非日记笔记数);只报告 |
|
|
25
25
|
| `zk_stats` | 笔记数 / 标签数 / 断链 / 孤立 / 归档(superseded),以及顶层目录与前 10 个标签的实时分布 |
|
|
26
26
|
| `zk_use` | 切换**本会话**用的笔记本(只影响这个会话,不改全局默认);不带参数就列出现有的与当前固定的 |
|
|
27
27
|
| `zk_<别名>` | 笔记本 `.zk/config.toml` 的 `[alias]` 里定义的自定义命令(例如 `lucky = "zk list --quiet --format full --sort random --limit 1"`)注册成工具:调用等于 `zk <别名> [args]`,`args` 追加到命令里的 `$@`;输出按原样返回。紧邻别名上方的注释(或行尾注释)作为工具说明;改完 config.toml 要重载插件才会出现新工具。**默认关闭**,见下 |
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
写笔记前 agent 会先 `zk_search` 查重;flat 笔记本的 `zk_new` 还会按标题再查一次,重复直接报错。
|
|
40
40
|
layered 笔记本的文件名是标题的 slug:英文标题自然得到英文文件名,短不短看标题本身;中文标题会变成很长的拼音名,要短名就在 `zk_new` 时传 `name`;老笔记不自动改名,**手工 `mv` 会断入链**,要改名用 `zk_rename`(默认预演,会重写引用)。
|
|
41
41
|
layered 下每篇笔记都要落在 group 里:`zk_new` 不给 `dir` 就按 `type` 选目录(source → notes/sources),没给 `type` 会直接报错;显式给的 `dir` 也要在某个 group 下(`notes/sources` 及其子目录,写 `notes/concept` 这种错字一样拦)—— `notes/` 根不属于任何 group,建在那里的笔记不套模板(没有 frontmatter,文件名是随机 id),所以这条路径一律不放过。
|
|
42
|
+
**稳定 ID**:`zk_new` 新建的笔记在 frontmatter 里写一个 10 位十六进制 `id`(主键不绑检索地址;flat 的笔记原本没有 frontmatter,会补一个只含 `id` 的最小块,正文形状不变)。老笔记、日记一律不回填;日记按日期命名、路径天然稳定,没有 id 也不计入缺口。身份判断先看 `id`、缺失才退回 `path`,所以 `MEMORY.md` / `USER.md` 的来源写 `[[<id>]]` 时,笔记改名后仍指向同一篇。`zk_lint` / `zk_dream` 报 `missingId`(缺 id 的非日记笔记数),只提示不改写;批量回填属于写路径,另开一轮。相似标题仍只是 warning,不升级为去重方案。
|
|
42
43
|
双链用相对路径(zk 的 `link-format = "wiki"` 认这个),所以 `--broken-links` 不会因为「按标题链接」而误报。
|
|
43
44
|
|
|
44
45
|
**多会话**:每个会话第一次调工具时,会把当时配置里的默认笔记本**固定**下来(落在 `~/.dsh/zdiary/sessions/`,
|
|
@@ -95,6 +96,7 @@ DSH 自带的 AGENTS.md 加载只看「项目根 → 当前目录」链,**不
|
|
|
95
96
|
被引数与 `topCited` 为空 —— 那是「没算」,不是「没人引」。
|
|
96
97
|
`zk_lint` 会核对 `MEMORY.md` / `USER.md` 里的 `[[来源路径]]` 是否还指向活笔记 ——
|
|
97
98
|
`zk_supersede` 只重写笔记之间的入链,meta 文件不在图里,链接不会跟着搬,`--broken-links` 也看不见。
|
|
99
|
+
来源也可以写 `[[<稳定 id>]]`(见上):链接解析先按 id 落到那篇笔记的当前路径,改名后不会漂。
|
|
98
100
|
改名同理:`zk_rename` 会把入链(按篇反查 `--link-to`,不再建全图)和根目录 meta 的 `[[来源]]` 一起改到新路径,被搬走那篇自己的相对 Markdown 链接也按新位置重算 —— 手工 `mv` 会一次断掉这三处。
|
|
99
101
|
插件不跑定时任务:`DREAMS.md` 开头写一行 `上次做梦:YYYY-MM-DD`,注入里就会按 7 天节奏提醒该整理
|
|
100
102
|
(没做过就提示先跑一次;没有 `AGENTS.md` 的库不提醒)。
|
|
@@ -424,6 +426,7 @@ zk-kit doctor # 自检:zk / 笔记本 / 模板 / group
|
|
|
424
426
|
| 体检提示「笔记已到 150 篇(不含日记)」 | 检索开始变糊的拐点,但插件不生成 `INDEX.md`(静态索引会过期)。用 `zk_stats` 看目录 / 标签分布决定先读哪里;真要总索引就自己写,提示会消失 |
|
|
425
427
|
| 中文搜索只回来几条 | `zk_search` / `zk_query` 对含中文的查询都会 FTS + 子串各跑一次并合并(`matchStrategy: mixed`):FTS5 不切中文,整串查询只匹配字面量。多词查询(如「记忆 召回」)会自动切成词做 alternation、按覆盖度排序,命中句进 `snippet`。0 命中时会提示换更短的词或按 tags 查 |
|
|
426
428
|
| 体检报「frontmatter 缺字段」(layered) | 缺 `type` / `status` / `authority`:索引把缺失落成空串,`authority=informal` 这类机械门对它们静默失效。按模板补上即可;flat 不查这一项 |
|
|
429
|
+
| 体检 / 做梦报「稳定 ID:N 篇没有 id」 | 老笔记没有稳定 id 属预期:`zk_new` 新建的笔记会自动写,插件**不批量回填**(回填是写路径,要单独一轮)。想让 `MEMORY.md` 的来源链接在改名后不断,把来源写成 `[[<id>]]`(从笔记 frontmatter 读)。日记按日期命名,不计入 |
|
|
427
430
|
| `doctor` 提示「记忆在跑,但缺 AGENTS.md」 | `flat` 默认不写工作约定:跑 `zk-kit init --with-agents`,或设置页点「写入 AGENTS.md 模板」(已存在不覆盖) |
|
|
428
431
|
| 提示「盘上有分层目录,但配置是 flat」 | 以前是 layered、后来改成 flat(或手工搬过目录):新建笔记会落在 `notes/` 根、不套模板。要么把配置改回 layered,要么把这些目录移走;插件只提示,不动文件 |
|
|
429
432
|
| `read` / `edit` 笔记本报 `file access denied under workspace-write mode` | 笔记本被指到了工作区外。读也只能走原生 `read`(`zk_read` 已移除):把笔记本放回工作区(默认 `<工作区>/zdiary` 就是为此选的),或把目录加进 `additionalWritableRoots`,或开会话到 `danger-full-access` |
|
|
@@ -445,7 +448,7 @@ zk-kit doctor # 自检:zk / 笔记本 / 模板 / group
|
|
|
445
448
|
`--links-per-note` 决定链接密度 —— 建索引成本主要花在链接上,产出目录自带 `.zk/config.toml`,进去 `zk index` 即可)·
|
|
446
449
|
`bun run bench`(成本表:逐用例真起 zk,记子进程数 / 回传字节 / 峰值 RSS / p50 / p95 与库的篇数 / 链接数;默认 1k、3 轮;
|
|
447
450
|
全量 10k 实测与复跑方法见 `docs/bench-baseline.md`)
|
|
448
|
-
- **耗时差两个数量级**:`bun run test` 约 100 s(
|
|
451
|
+
- **耗时差两个数量级**:`bun run test` 约 100 s(236 个用例,真起 zk;机器快慢能差一倍);`fixture` 百毫秒级;`bench` 默认 1k 约十几秒,
|
|
449
452
|
10k 上光一条 `graph.full` 就是 30 s、冷建索引几分钟。所以 `bench` **不在** `check` / CI 里,跟测试完全分开,不会因为你跑测试而变慢。
|
|
450
453
|
- `bun run test` 会先按插件自己的顺序找 zk(配置 → `ZK_BIN` → PATH → `~/.dsh/zdiary/bin`),跑完要求 **skip = 0**:
|
|
451
454
|
集成测试跳过一律按失败处理,否则「全绿」可能是「什么都没跑」。要换 zk 就 `ZK_BIN=/path/to/zk bun run check`。
|
package/dist/cli.js
CHANGED
|
@@ -202,6 +202,8 @@ function dreamReport(r) {
|
|
|
202
202
|
const promptWords = Math.max(0, memoryTotal - r.memory.words);
|
|
203
203
|
lines.push(`记忆:内容 ${r.memory.words}/${r.memory.budget} 词 + 提示 ${promptWords} = ${memoryTotal} 词(余 ${r.memory.headroom})` +
|
|
204
204
|
(r.memory.personaWords > 0 ? ` · 人格 ${r.memory.personaWords} 词(单独注入,不计入记忆预算)` : ''));
|
|
205
|
+
if (r.missingId > 0)
|
|
206
|
+
lines.push(`· 稳定 ID:${r.missingId} 篇没有 id — 新笔记自动写入,老笔记只提示、不回填(§9.2)`);
|
|
205
207
|
if (r.retireCandidates.length) {
|
|
206
208
|
lines.push('可考虑退役:');
|
|
207
209
|
for (const c of r.retireCandidates.slice(0, 10)) {
|
|
@@ -1227,6 +1227,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1227
1227
|
'incomplete 是 frontmatter 缺 type / status / authority 的笔记:缺失被索引落成空串,机械门会静默失效。' +
|
|
1228
1228
|
'tags 查标签卫生(大小写变体 / 单例,标签总数 ≥ 30 才报);template 说根目录 AGENTS.md 是不是当前版本。' +
|
|
1229
1229
|
'similarTitles 是「改一两个字」的疑似重复(同目录、二元组相似度 ≥ 0.6):与全等重名分开,只报告。' +
|
|
1230
|
+
'missingId 是缺稳定 id 的非日记笔记数:zk_new 新建的笔记自动写 id,老笔记只提示、不回填。' +
|
|
1230
1231
|
'timeline 是日记时间轴:最近 90 天的空块 / 倒序 / 格式(11:5x)/ 位置(layered 用 ##、或在 ## Events 外)问题,只报告不改文件。' +
|
|
1231
1232
|
'total / counts 按全量算,items 里每篇最多 50 条明细,整个桶最多 50 篇;skipped 是过大或读不到而没检查的篇数。' +
|
|
1232
1233
|
'indexHint=true 表示笔记(不含日记)已到 150 篇而根目录还没有 INDEX.md(只是提示,不生成静态索引)。' +
|
|
@@ -1251,6 +1252,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1251
1252
|
schema: objSchema({
|
|
1252
1253
|
notebook: { type: 'string' },
|
|
1253
1254
|
notes: { type: 'number' },
|
|
1255
|
+
missingId: { type: 'number' },
|
|
1254
1256
|
truncated: { type: 'boolean' },
|
|
1255
1257
|
completeness: { type: 'string', enum: ['complete', 'partial', 'unknown'] },
|
|
1256
1258
|
scanLimit: { type: 'number' },
|
|
@@ -1271,7 +1273,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1271
1273
|
timeline: LINT_TIMELINE,
|
|
1272
1274
|
indexHint: { type: 'boolean' },
|
|
1273
1275
|
indexLag: { type: 'string' },
|
|
1274
|
-
}, ['notebook', 'notes', 'truncated', 'completeness', 'scanLimit', 'staleDays', 'parseErrors', 'orphans', 'brokenLinks', 'stale', 'tagless', 'oneWayLinks', 'duplicateTitles', 'similarTitles', 'memoryLinks', 'incomplete', 'tags', 'template', 'persona', 'timeline', 'indexHint', 'indexLag']),
|
|
1276
|
+
}, ['notebook', 'notes', 'missingId', 'truncated', 'completeness', 'scanLimit', 'staleDays', 'parseErrors', 'orphans', 'brokenLinks', 'stale', 'tagless', 'oneWayLinks', 'duplicateTitles', 'similarTitles', 'memoryLinks', 'incomplete', 'tags', 'template', 'persona', 'timeline', 'indexHint', 'indexLag']),
|
|
1275
1277
|
},
|
|
1276
1278
|
async run(args, env) {
|
|
1277
1279
|
const cfg = configFor(env.workspace, env.sessionId);
|
|
@@ -1355,6 +1357,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1355
1357
|
'backlog 是窗口外仍未晋升的笔记(按被引数),漏做一次不会永久掉队。' +
|
|
1356
1358
|
'库超过全图统计上限时不再建全图统计入链(默认 2000 篇,设置页可调;graphBudget 给出生效值):' +
|
|
1357
1359
|
'graphSkipped=true,cited / citedBy / topCited 为空 —— 那是「没算」,不是「没人引」。' +
|
|
1360
|
+
'missingId 是缺稳定 id 的非日记笔记数(只提示、不回填);MEMORY 里可以用 [[<id>]] 当来源,改名后仍指向同一篇。' +
|
|
1358
1361
|
'memory 给记忆余量(内容词 + 提示 = 总词,余量按总词算,可以为负);retireCandidates 给「可考虑退役」的条目' +
|
|
1359
1362
|
'(来源归档 / 不存在、超过 ' + STALE_DAYS + ' 天未复核;显式写 archive/ 的有意引用不算)。' +
|
|
1360
1363
|
'days 省略时窗口 = 距 DREAMS.md「上次做梦」的天数(没有则 7 天)。' +
|
|
@@ -1376,6 +1379,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1376
1379
|
scanLimit: { type: 'number' },
|
|
1377
1380
|
graphSkipped: { type: 'boolean' },
|
|
1378
1381
|
graphBudget: { type: 'number' },
|
|
1382
|
+
missingId: { type: 'number' },
|
|
1379
1383
|
memory: objSchema({
|
|
1380
1384
|
words: { type: 'number' },
|
|
1381
1385
|
personaWords: { type: 'number' },
|
|
@@ -1441,7 +1445,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1441
1445
|
reason: { type: 'string', enum: ['stale', 'source-missing', 'source-superseded'] },
|
|
1442
1446
|
}, ['entry', 'date', 'age', 'reason']),
|
|
1443
1447
|
},
|
|
1444
|
-
}, ['notebook', 'windowDays', 'since', 'truncated', 'completeness', 'scanLimit', 'graphSkipped', 'graphBudget', 'memory', 'counts', 'newNotes', 'alreadyPromoted', 'backlog', 'rejected', 'topCited', 'dailyLooseEnds', 'retireCandidates']),
|
|
1448
|
+
}, ['notebook', 'windowDays', 'since', 'truncated', 'completeness', 'scanLimit', 'graphSkipped', 'graphBudget', 'missingId', 'memory', 'counts', 'newNotes', 'alreadyPromoted', 'backlog', 'rejected', 'topCited', 'dailyLooseEnds', 'retireCandidates']),
|
|
1445
1449
|
},
|
|
1446
1450
|
async run(args, env) {
|
|
1447
1451
|
const cfg = configFor(env.workspace, env.sessionId);
|
|
@@ -1462,6 +1466,8 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1462
1466
|
lines.push('记忆:内容 ' + v.memory.words + '/' + v.memory.budget + ' 词 + 提示 ' + promptWords +
|
|
1463
1467
|
' = ' + memoryTotal + ' 词(余 ' + v.memory.headroom + ')' +
|
|
1464
1468
|
(v.memory.personaWords > 0 ? ' · 人格 ' + v.memory.personaWords + ' 词(单独注入,不计入记忆预算)' : ''));
|
|
1469
|
+
if (v.missingId > 0)
|
|
1470
|
+
lines.push('· 稳定 ID:' + v.missingId + ' 篇没有 id — 新笔记自动写入,老笔记只提示、不回填(§9.2)');
|
|
1465
1471
|
if (v.retireCandidates.length) {
|
|
1466
1472
|
lines.push('可考虑退役:');
|
|
1467
1473
|
for (const c of v.retireCandidates.slice(0, 10)) {
|
package/dist/lib/dream.js
CHANGED
|
@@ -20,7 +20,7 @@ import { parseFrontmatter } from './frontmatter.js';
|
|
|
20
20
|
import { memoryUsage } from './memory.js';
|
|
21
21
|
import { daysSinceDate, entryDate, isRetiredEntry, splitMemoryEntries, stripRetiredEntries } from './memory-format.js';
|
|
22
22
|
import { isDayString } from './dates.js';
|
|
23
|
-
import { normalizeTitle, resolveWikiTarget, WIKI_LINK, wikiIndex } from './wikilink.js';
|
|
23
|
+
import { normalizeTitle, noteKey, resolveWikiTarget, WIKI_LINK, wikiIndex } from './wikilink.js';
|
|
24
24
|
/** 每栏最多回多少条(计划 §4 M3 的 50 条上限) */
|
|
25
25
|
export const MAX_ITEMS = 50;
|
|
26
26
|
/** topCited 只取前几名:给 agent 一个「本周被反复引用」的提示,不是排行榜 */
|
|
@@ -211,20 +211,24 @@ function promotionsOf(cfg, notes) {
|
|
|
211
211
|
if (!body.trim())
|
|
212
212
|
return out;
|
|
213
213
|
const index = wikiIndex(notes);
|
|
214
|
+
const byPath = new Map(notes.map((n) => [n.path, n]));
|
|
214
215
|
for (const match of body.matchAll(WIKI_LINK)) {
|
|
215
216
|
const hit = resolveWikiTarget(cfg, String(match[1] ?? '').trim(), index);
|
|
216
|
-
|
|
217
|
-
|
|
217
|
+
// 解析结果是路径,按身份键记账(§9.2):有 id 的笔记改名后仍是同一篇,不会重复晋升。
|
|
218
|
+
const note = byPath.get(hit.path);
|
|
219
|
+
if (hit.status === 'live' && note)
|
|
220
|
+
out.set(noteKey(note), 'link');
|
|
218
221
|
}
|
|
219
222
|
const lines = stripRetiredEntries(body).body.split('\n').map((line) => normalizeTitle(line)).filter(Boolean);
|
|
220
223
|
for (const note of notes) {
|
|
221
|
-
|
|
224
|
+
const key = noteKey(note);
|
|
225
|
+
if (out.has(key))
|
|
222
226
|
continue;
|
|
223
|
-
const
|
|
224
|
-
if ([...
|
|
227
|
+
const title = normalizeTitle(note.title);
|
|
228
|
+
if ([...title].length < MIN_TITLE_MATCH)
|
|
225
229
|
continue;
|
|
226
|
-
if (lines.some((line) => line.includes(
|
|
227
|
-
out.set(
|
|
230
|
+
if (lines.some((line) => line.includes(title)))
|
|
231
|
+
out.set(key, 'title');
|
|
228
232
|
}
|
|
229
233
|
return out;
|
|
230
234
|
}
|
|
@@ -360,7 +364,7 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
360
364
|
if (isUnder(note.path, cfg.dailyDir))
|
|
361
365
|
continue;
|
|
362
366
|
// 已经进 MEMORY 的先说:否则每周原样重复,仪式几次之后就没人看了
|
|
363
|
-
const by = promoted.get(note
|
|
367
|
+
const by = promoted.get(noteKey(note));
|
|
364
368
|
if (by) {
|
|
365
369
|
alreadyPromoted.push(candidateOf(note, by));
|
|
366
370
|
continue;
|
|
@@ -380,7 +384,7 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
380
384
|
const at = dayMs(noteDate(cfg, n));
|
|
381
385
|
if (Number.isFinite(at) && at >= sinceMs)
|
|
382
386
|
return false;
|
|
383
|
-
if (promoted.has(n
|
|
387
|
+
if (promoted.has(noteKey(n)))
|
|
384
388
|
return false;
|
|
385
389
|
return rejectionReason(n) === null;
|
|
386
390
|
})
|
|
@@ -425,6 +429,8 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
425
429
|
scanLimit: scan.limit,
|
|
426
430
|
graphSkipped: !graphReady,
|
|
427
431
|
graphBudget,
|
|
432
|
+
// 只读计数:老笔记 / 日记没有 id 属预期,日记按日期命名、路径天然稳定,不计入
|
|
433
|
+
missingId: notes.filter((n) => !n.id && !isUnder(n.path, cfg.dailyDir)).length,
|
|
428
434
|
counts: {
|
|
429
435
|
window: windowNotes.length,
|
|
430
436
|
daily: windowNotes.filter((n) => isUnder(n.path, cfg.dailyDir)).length,
|
package/dist/lib/lint.js
CHANGED
|
@@ -287,6 +287,9 @@ export function formatLintExtras(r) {
|
|
|
287
287
|
const timeline = formatTimeline(r.timeline);
|
|
288
288
|
if (timeline)
|
|
289
289
|
lines.push(timeline);
|
|
290
|
+
if (r.missingId > 0) {
|
|
291
|
+
lines.push(`· 稳定 ID:${r.missingId} 篇没有 id — 新建的笔记已自动写入,老笔记只提示、不回填(§9.2)`);
|
|
292
|
+
}
|
|
290
293
|
// 缺文件 = 人格未启用(默认状态):不报错、不占行。
|
|
291
294
|
// 有旧版 / 占位符 / 标记不符时才提醒 —— 否则每个老库每次 lint 都多一条永久 ⚠️,反而训练人忽略告警。
|
|
292
295
|
const persona = r.persona.items;
|
|
@@ -925,6 +928,8 @@ export async function lintNotebook(cfg, opts = {}, signal) {
|
|
|
925
928
|
return {
|
|
926
929
|
notebook: cfg.notebook,
|
|
927
930
|
notes: notes.length,
|
|
931
|
+
// 只读计数:日记按日期命名、路径天然稳定,不计入;老笔记缺 id 属预期,不回填
|
|
932
|
+
missingId: nonDaily.filter((n) => !n.id).length,
|
|
928
933
|
truncated: scanTruncated,
|
|
929
934
|
completeness,
|
|
930
935
|
scanLimit: scan.limit,
|
package/dist/lib/wikilink.js
CHANGED
|
@@ -17,15 +17,27 @@ export const WIKI_LINK = /\[\[([^\]|#]+)(?:#[^\]|]*)?(?:\|[^\]]*)?\]\]/g;
|
|
|
17
17
|
export function normalizeTitle(title) {
|
|
18
18
|
return String(title ?? '').replace(/\s+/g, ' ').trim().toLowerCase();
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* 「同一篇」的身份键(§9.2):有稳定 id 就只看 id,老笔记 / 日记退回 path。
|
|
22
|
+
* 改名 / 移动 / 重建索引不会改 frontmatter 里的 id,所以这个键不随检索地址漂移。
|
|
23
|
+
*/
|
|
24
|
+
export function noteKey(note) {
|
|
25
|
+
const id = String(note.id ?? '').trim();
|
|
26
|
+
return id ? `id:${id}` : `path:${note.path}`;
|
|
27
|
+
}
|
|
20
28
|
export function wikiIndex(notes) {
|
|
21
29
|
const byTitle = new Map();
|
|
30
|
+
const byId = new Map();
|
|
22
31
|
for (const note of notes) {
|
|
23
32
|
for (const key of [normalizeTitle(note.title), normalizeTitle(path.basename(note.path, path.extname(note.path)))]) {
|
|
24
33
|
if (key && !byTitle.has(key))
|
|
25
34
|
byTitle.set(key, note.path);
|
|
26
35
|
}
|
|
36
|
+
const id = String(note.id ?? '').trim();
|
|
37
|
+
if (id && !byId.has(id))
|
|
38
|
+
byId.set(id, note.path);
|
|
27
39
|
}
|
|
28
|
-
return { byTitle };
|
|
40
|
+
return { byTitle, byId };
|
|
29
41
|
}
|
|
30
42
|
function insideNotebook(cfg, abs) {
|
|
31
43
|
const root = path.resolve(cfg.notebook);
|
|
@@ -48,6 +60,11 @@ export function resolveWikiTarget(cfg, target, index) {
|
|
|
48
60
|
const rel = String(target ?? '').replace(/\\/g, '/').replace(/^\.\//, '').trim();
|
|
49
61
|
if (!rel)
|
|
50
62
|
return { status: 'missing', path: '', explicit: false };
|
|
63
|
+
// 稳定 ID 优先(§9.2):`[[<id>]]` 直接落到那篇笔记的当前路径,改名之后也不断链。
|
|
64
|
+
// 老库没有 id,byId 为空,下面的路径 / 标题解析顺序逐字不变。
|
|
65
|
+
const byId = index.byId.get(rel);
|
|
66
|
+
if (byId)
|
|
67
|
+
return { status: 'live', path: byId, explicit: false };
|
|
51
68
|
const candidates = /\.md$/i.test(rel) ? [rel] : [rel, `${rel}.md`];
|
|
52
69
|
for (const candidate of candidates) {
|
|
53
70
|
const abs = path.resolve(cfg.notebook, candidate);
|
package/dist/lib/zk.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import fs from 'node:fs';
|
|
11
11
|
import path from 'node:path';
|
|
12
|
+
import { randomBytes } from 'node:crypto';
|
|
12
13
|
import { BIN_DIR } from '../config/paths.js';
|
|
13
14
|
import { ExecError, findOnPath, run } from './exec.js';
|
|
14
15
|
import { readJson, writeJson } from './json-file.js';
|
|
@@ -759,6 +760,7 @@ function toRecord(raw, cfg, terms = []) {
|
|
|
759
760
|
snippet: buildSnippet(raw, terms),
|
|
760
761
|
words: Number(raw.wordCount ?? 0),
|
|
761
762
|
metadata,
|
|
763
|
+
id: meta('id'),
|
|
762
764
|
status: meta('status'),
|
|
763
765
|
type: meta('type'),
|
|
764
766
|
authority: meta('authority'),
|
|
@@ -859,6 +861,7 @@ const LEAN_FIELDS = [
|
|
|
859
861
|
'{{metadata.authority}}',
|
|
860
862
|
'{{metadata.date}}',
|
|
861
863
|
'{{metadata.supersede-reason}}',
|
|
864
|
+
'{{metadata.id}}',
|
|
862
865
|
];
|
|
863
866
|
const LEAN_TEMPLATE = [...LEAN_FIELDS, '{{lead}}'].join(LEAN_FIELD);
|
|
864
867
|
/**
|
|
@@ -882,8 +885,8 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
882
885
|
return { notes: [], malformed: false };
|
|
883
886
|
const notes = [];
|
|
884
887
|
let malformed = false;
|
|
885
|
-
//
|
|
886
|
-
const leadAt = withSnippet ?
|
|
888
|
+
// 12 个定长字段(含 id)+(可选)snippet + lead;lead 放最后,多余的段拼回去
|
|
889
|
+
const leadAt = withSnippet ? 13 : 12;
|
|
887
890
|
for (const chunk of text.split(LEAN_RECORD)) {
|
|
888
891
|
const record = chunk.replace(/^[\r\n]+|[\r\n]+$/g, '');
|
|
889
892
|
if (!record.trim())
|
|
@@ -893,8 +896,8 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
893
896
|
malformed = true;
|
|
894
897
|
continue;
|
|
895
898
|
}
|
|
896
|
-
const [absPath, title, words, created, modified, tags, status, type, authority, date, reason] = parts;
|
|
897
|
-
const snippet = withSnippet ? clipSnippet(parts[
|
|
899
|
+
const [absPath, title, words, created, modified, tags, status, type, authority, date, reason, id] = parts;
|
|
900
|
+
const snippet = withSnippet ? clipSnippet(parts[12]) : '';
|
|
898
901
|
const lead = parts.slice(leadAt).join(LEAN_FIELD).replace(/\s+/g, ' ').trim().slice(0, 200);
|
|
899
902
|
const abs = absPath ?? '';
|
|
900
903
|
if (!abs) {
|
|
@@ -913,6 +916,8 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
913
916
|
metadata.date = date;
|
|
914
917
|
if (reason)
|
|
915
918
|
metadata['supersede-reason'] = reason;
|
|
919
|
+
if (id)
|
|
920
|
+
metadata.id = id;
|
|
916
921
|
notes.push({
|
|
917
922
|
title: String(title ?? ''),
|
|
918
923
|
path: rel,
|
|
@@ -925,6 +930,7 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
925
930
|
snippet,
|
|
926
931
|
words: Number(words ?? 0),
|
|
927
932
|
metadata,
|
|
933
|
+
id: String(id ?? ''),
|
|
928
934
|
status: String(status ?? ''),
|
|
929
935
|
type: String(type ?? ''),
|
|
930
936
|
authority: String(authority ?? ''),
|
|
@@ -1430,6 +1436,7 @@ function noteFromDisk(cfg, abs) {
|
|
|
1430
1436
|
snippet: '',
|
|
1431
1437
|
words: content.split(/\s+/).filter(Boolean).length,
|
|
1432
1438
|
metadata: { ...fields },
|
|
1439
|
+
id: fields.id ?? '',
|
|
1433
1440
|
status: fields.status ?? '',
|
|
1434
1441
|
type: fields.type ?? '',
|
|
1435
1442
|
authority: fields.authority ?? '',
|
|
@@ -1495,6 +1502,13 @@ function normalizeNoteName(raw) {
|
|
|
1495
1502
|
}
|
|
1496
1503
|
return name;
|
|
1497
1504
|
}
|
|
1505
|
+
/**
|
|
1506
|
+
* 稳定 ID(§9.2):10 位十六进制短随机串(40 bit,10⁴ 篇库的碰撞概率可忽略)。
|
|
1507
|
+
* 纯小写 hex,不会与路径 / `.md` 后缀 / 中文标题撞形,`[[id]]` 才认得出来。
|
|
1508
|
+
*/
|
|
1509
|
+
export function newNoteId() {
|
|
1510
|
+
return randomBytes(5).toString('hex');
|
|
1511
|
+
}
|
|
1498
1512
|
/**
|
|
1499
1513
|
* 用 zk new 建一篇(正文走 stdin → 模板里的 {{content}})。
|
|
1500
1514
|
*
|
|
@@ -1635,6 +1649,17 @@ export async function createNote(cfg, input, signal, scan = (c, s) => allNotes(c
|
|
|
1635
1649
|
summaryWarning = `summary 没写进去:${err instanceof Error ? err.message : String(err)}(文件已建好,可以直接编辑那一节)`;
|
|
1636
1650
|
}
|
|
1637
1651
|
}
|
|
1652
|
+
// 稳定 ID(§9.2):只给 zk_new 新建的笔记写 id,老笔记 / 日记不回填。
|
|
1653
|
+
// 建完才写:zk new -i 索引的是模板产物,改了文件就得标脏,让下一次读补索引(R6)。
|
|
1654
|
+
// 收尾失败一律吞掉(同 markIndexDirty):笔记已经建好,不能让一次 id 写入看起来像整次创建失败。
|
|
1655
|
+
try {
|
|
1656
|
+
const text = fs.readFileSync(finalAbs, 'utf8');
|
|
1657
|
+
if (!parseFrontmatter(text).fields.id) {
|
|
1658
|
+
fs.writeFileSync(finalAbs, setFrontmatter(text, { id: newNoteId() }), 'utf8');
|
|
1659
|
+
markIndexDirty(cfg);
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
catch { /* 收尾失败:文件已落盘,不回滚、不误导 */ }
|
|
1638
1663
|
return { path: relPathOf(cfg, finalAbs), absPath: finalAbs, title, archived, similar, summaryApplied, summaryWarning };
|
|
1639
1664
|
}
|
|
1640
1665
|
/** 笔记本相对路径是否在归档目录下(supersede 会把文件搬进去,路径保持不变) */
|
package/docs/features/memory.md
CHANGED
|
@@ -58,7 +58,8 @@
|
|
|
58
58
|
需要「别自动发布 / 别花钱」这类边界时,写进 `AGENTS.md`(工作约定)或 `PERSONA.md`(角色边界)。
|
|
59
59
|
|
|
60
60
|
**记忆层与事实层的一致性。** `MEMORY.md` 每条结论后附 `[[来源路径]]`(写进 AGENTS 模板);
|
|
61
|
-
`zk_lint` 的 `memoryLinks`
|
|
61
|
+
`zk_lint` 的 `memoryLinks` 只做机械核对:先按稳定 id 查(`[[<id>]]` → 该笔记当前路径,§10.4 第 7 项),
|
|
62
|
+
未命中再走原路径 → 加 `.md` → `archive/` 下同名 → 活笔记标题 / 文件名,
|
|
62
63
|
旧路径在 `archive/` 下找到同名(或活文件带 `status: superseded`)报 `superseded`,都找不到报 `missing`;
|
|
63
64
|
显式写 `archive/` 的链接是有意引用,不算漂移;另数 MEMORY.md 里有多少活条目没附链接(USER 不计;
|
|
64
65
|
`[[ ]]` 这种空目标不算来源)。`unlinked` / `undated` 与注入提示、记忆页共用 `memoryFormatDebt()`,
|
|
@@ -68,6 +69,9 @@ P1-3 再加三项,都只报告:`undated`(条目首行没有 `YYYY-MM-DD`
|
|
|
68
69
|
但文件不删、不改 —— 「注入 = 文件原文」收窄为「注入 = 原文去掉退役条目」(D3)。
|
|
69
70
|
为什么要有:`zk_supersede` 只重写 `zk graph` 里的入链,meta 文件被 `excludedNames` 排除在图外 ——
|
|
70
71
|
链接不会跟着搬,`--broken-links` 也看不见。语义重复仍留给 agent,插件不碰。
|
|
72
|
+
稳定 ID 让来源链接可以选择不绑路径:写成 `[[<id>]]`(从笔记 frontmatter 读)时,改名后仍落到同一篇;
|
|
73
|
+
老笔记没有 id、链接也没写 id 时解析顺序与以前逐字一致。插件不把已有的 `[[来源路径]]` 自动改写成 id ——
|
|
74
|
+
回填属于写路径,单独一轮(§9.2)。
|
|
71
75
|
|
|
72
76
|
**Depends on.** `config/store`(`memoryBudgetWords` / `personaBudgetWords`,每次现读 —— 设置页保存后下一次工具调用就生效)、
|
|
73
77
|
`lib/meta`、`lib/persona-template`、`node:fs`;宿主的 `systemPrompt` 服务(可选,没有就只留 `zk_context`)。
|
|
@@ -22,6 +22,19 @@
|
|
|
22
22
|
|
|
23
23
|
**Public surface.** `buildTools()`(按默认工作区的笔记本配置构建,测试用)与 `registerZkTools(tools)`(返回撤销函数)。
|
|
24
24
|
|
|
25
|
+
**稳定 ID(§9.2 / §10.4 第 7 项).**
|
|
26
|
+
`zk_new` 建完笔记后在 frontmatter 写一个 10 位十六进制 `id`(`newNoteId()`,`lib/zk.ts`):
|
|
27
|
+
没有 frontmatter 的 flat 笔记补一个只含 `id` 的最小块、正文形状不变;老笔记 / 日记不回填(日记按日期命名,路径天然稳定)。
|
|
28
|
+
`NoteRecord.id` 取 `metadata.id`,三条构造路径(`toRecord` / `parseLeanList` / `noteFromDisk`)都给;
|
|
29
|
+
LEAN 模板加一列 `{{metadata.id}}`,`parseLeanList` 的定长字段从 11 变 12(`leadAt` 无 snippet 12 / 有 snippet 13)。
|
|
30
|
+
身份判断收在一处:`lib/wikilink.ts` 的 `noteKey()` 有 id 用 `id:xxx`、缺了退回 `path:xxx`;
|
|
31
|
+
`wikiIndex()` 建 `byId`(id → 当前路径),`resolveWikiTarget()` 先查 byId,所以 `[[<id>]]` 改名后仍落到同一篇;
|
|
32
|
+
`promotionsOf` 用 `noteKey()` 记账,改名后不会把同一篇再报一遍候选。
|
|
33
|
+
`zk_lint` / `zk_dream` 的 `missingId` 是只读计数(非日记、无 id),render 与 CLI report 都报一行「只提示不回填」;
|
|
34
|
+
输出 schema 里 `missingId` 是必填项 —— 所有 schema 都是 `additionalProperties: false`,多一个 key 会让整次调用 invalid output。
|
|
35
|
+
不做:批量回填、把 `id` 加进 `zk_new` 返回、把 dedup / supersede / rename 的 path+title 键改造成 id 键(那是迁移,单独一轮);
|
|
36
|
+
相似标题继续只作 warning,不升级为去重方案。
|
|
37
|
+
|
|
25
38
|
**Alias tools(`zk_<别名>`).** 默认**不注册**:`aliasTools` 为 false 时 `buildTools` 连 `.zk/config.toml` 都不读,
|
|
26
39
|
内置工具固定 14 个。别名是 `$SHELL -c` 的任意命令,注册成工具等于把它交到模型手里,所以要人显式打开
|
|
27
40
|
(设置页「工具」组的复选框,或 `zk-kit config set aliasTools=true`,只认 `true`;拼错的值归一化成 false 并在 `config set` 时回显)。
|