dsh-zdiary-kit 0.7.5 → 0.8.3

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.
@@ -13,11 +13,11 @@ import { config, notebookInfos, selectNotebook } from '../../config/store.js';
13
13
  import { configFor, pinSession, sessionNotebook, sessionState } from '../../config/session.js';
14
14
  import { defaultNotebook, expandHome, processWorkspace, resolveInWorkspace } from '../../config/paths.js';
15
15
  import { S, validateArgs } from '../../lib/tool.js';
16
- import { ensureNotebook } from '../../lib/notebook.js';
17
- import { searchNotes, createNote, readNote, findNote, appendToNote, ensureDailyNote, appendDaily, linkNotes, withTags, notebookStats, supersedeNote, renameNote, hasNotebook, isUnderDir, localDate as dateOnly, ZkError, } from '../../lib/zk.js';
16
+ import { ensureNotebook, newNoteDir } from '../../lib/notebook.js';
17
+ import { searchNotes, searchNotesDetailed, createNote, readNote, findNote, appendToNote, ensureDailyNote, appendDaily, linkNotes, withTags, taglessCjkWarning, notebookStats, supersedeNote, renameNote, hasNotebook, isUnderDir, localDate as dateOnly, ZkError, } from '../../lib/zk.js';
18
18
  import { buildMemoryPayload, META_FILES } from '../../lib/memory.js';
19
19
  import { META_NAMES } from '../../lib/meta.js';
20
- import { formatMemoryLinks, lintNotebook } from '../../lib/lint.js';
20
+ import { formatLintExtras, formatMemoryLinks, lintNotebook } from '../../lib/lint.js';
21
21
  import { dreamNotebook } from '../../lib/dream.js';
22
22
  const text = (value) => [{ type: 'text', text: value }];
23
23
  const objSchema = (properties, required) => ({
@@ -57,12 +57,28 @@ const LINT_DUPLICATE = objSchema({
57
57
  items: objSchema({ title: { type: 'string' }, paths: { type: 'array', items: { type: 'string' } } }, ['title', 'paths']),
58
58
  },
59
59
  }, ['total', 'items']);
60
+ /** 疑似重复标题(P2-10):只差一两个字 */
61
+ const LINT_SIMILAR = objSchema({
62
+ total: { type: 'number' },
63
+ truncated: { type: 'boolean' },
64
+ items: {
65
+ type: 'array',
66
+ items: objSchema({
67
+ titles: { type: 'array', items: { type: 'string' } },
68
+ paths: { type: 'array', items: { type: 'string' } },
69
+ score: { type: 'number' },
70
+ }, ['titles', 'paths', 'score']),
71
+ },
72
+ }, ['total', 'truncated', 'items']);
60
73
  /** 记忆层体检:MEMORY.md / USER.md 里的来源链是否还指向活笔记 */
61
74
  const LINT_MEMORY = objSchema({
62
75
  files: { type: 'number' },
63
76
  links: { type: 'number' },
64
77
  total: { type: 'number' },
65
78
  unlinked: { type: 'number' },
79
+ undated: { type: 'number' },
80
+ retired: { type: 'number' },
81
+ retiredBroken: { type: 'number' },
66
82
  items: {
67
83
  type: 'array',
68
84
  items: objSchema({
@@ -73,7 +89,68 @@ const LINT_MEMORY = objSchema({
73
89
  path: { type: 'string' },
74
90
  }, ['source', 'entry', 'link', 'reason', 'path']),
75
91
  },
76
- }, ['files', 'links', 'total', 'unlinked', 'items']);
92
+ }, ['files', 'links', 'total', 'unlinked', 'undated', 'retired', 'retiredBroken', 'items']);
93
+ /** frontmatter 缺字段(P2-13) */
94
+ const LINT_INCOMPLETE = objSchema({
95
+ total: { type: 'number' },
96
+ truncated: { type: 'boolean' },
97
+ byField: objSchema({
98
+ type: { type: 'number' },
99
+ status: { type: 'number' },
100
+ authority: { type: 'number' },
101
+ }, ['type', 'status', 'authority']),
102
+ items: {
103
+ type: 'array',
104
+ items: objSchema({
105
+ path: { type: 'string' },
106
+ missing: { type: 'array', items: { type: 'string' } },
107
+ }, ['path', 'missing']),
108
+ },
109
+ }, ['total', 'truncated', 'byField', 'items']);
110
+ /** 标签卫生(P2-14) */
111
+ const LINT_TAGS = objSchema({
112
+ total: { type: 'number' },
113
+ reported: { type: 'boolean' },
114
+ caseVariants: objSchema({
115
+ total: { type: 'number' },
116
+ items: {
117
+ type: 'array',
118
+ items: objSchema({
119
+ variants: { type: 'array', items: { type: 'string' } },
120
+ total: { type: 'number' },
121
+ }, ['variants', 'total']),
122
+ },
123
+ }, ['total', 'items']),
124
+ singletons: objSchema({
125
+ total: { type: 'number' },
126
+ items: { type: 'array', items: { type: 'string' } },
127
+ }, ['total', 'items']),
128
+ }, ['total', 'reported', 'caseVariants', 'singletons']);
129
+ /** 模板版本(P2-15) */
130
+ const LINT_TEMPLATE = objSchema({
131
+ present: { type: 'boolean' },
132
+ version: { type: 'number' },
133
+ current: { type: 'number' },
134
+ stale: { type: 'boolean' },
135
+ currentText: { type: 'string' },
136
+ }, ['present', 'version', 'current', 'stale']);
137
+ /** 晋升候选的共用形状:newNotes / alreadyPromoted / backlog 三栏一致 */
138
+ const DREAM_CANDIDATE = objSchema({
139
+ title: { type: 'string' },
140
+ path: { type: 'string' },
141
+ type: { type: 'string' },
142
+ authority: { type: 'string' },
143
+ tags: { type: 'array', items: { type: 'string' } },
144
+ created: { type: 'string' },
145
+ lead: { type: 'string' },
146
+ cited: { type: 'number' },
147
+ citedBy: { type: 'array', items: { type: 'string' } },
148
+ promoted: { type: 'boolean' },
149
+ promotedBy: { type: 'string', enum: ['link', 'title', ''] },
150
+ dailyMentions: { type: 'number' },
151
+ daysMentioned: { type: 'number' },
152
+ lastMentioned: { type: 'string' },
153
+ }, ['title', 'path', 'type', 'authority', 'tags', 'created', 'lead', 'cited', 'citedBy', 'promoted', 'promotedBy', 'dailyMentions', 'daysMentioned', 'lastMentioned']);
77
154
  /** frontmatter 解析失败:这些笔记不在索引里,所以单列一桶(path 相对笔记本,不带 title) */
78
155
  const LINT_PARSE = objSchema({
79
156
  total: { type: 'number' },
@@ -159,11 +236,12 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
159
236
  t({
160
237
  name: 'zk_search',
161
238
  description: '在 zk 知识库里搜索笔记(全文 / 标签 / 日期 / 断链 / 孤立笔记),返回标题、相对路径、标签、创建日与摘要。' +
162
- '找到后用 zk_read 读全文。写笔记前先用它查重。',
239
+ '找到后用 zk_read 读全文。写笔记前先用它查重。' +
240
+ '中文查询会自动并用子串匹配(FTS 不切中文):matchStrategy 说明这次用了 fts / regex / mixed,mixed 表示补到了全文索引漏掉的笔记。',
163
241
  parameters: {
164
242
  type: 'object',
165
243
  properties: {
166
- query: S.str('全文检索词(FTS,支持 "短语" 与 AND / OR)'),
244
+ query: S.str('全文检索词(FTS,支持 "短语" 与 AND / OR;含中文时自动并用子串匹配补漏)'),
167
245
  tags: S.arr('按标签过滤(AND:必须同时带这些标签)', S.str('标签名,不带 #')),
168
246
  since: S.str('只看这个日期之后创建的笔记,自然语言或 ISO 日期,如 2026-01-01 / "last week"'),
169
247
  until: S.str('只看这个日期之前创建的笔记'),
@@ -178,6 +256,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
178
256
  schema: objSchema({
179
257
  notebook: { type: 'string' },
180
258
  count: { type: 'number' },
259
+ matchStrategy: { type: 'string', enum: ['fts', 'regex', 'mixed'] },
181
260
  notes: {
182
261
  type: 'array',
183
262
  items: objSchema({
@@ -188,12 +267,12 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
188
267
  lead: { type: 'string' },
189
268
  }, ['title', 'path', 'tags', 'created', 'lead']),
190
269
  },
191
- }, ['notebook', 'count', 'notes']),
270
+ }, ['notebook', 'count', 'matchStrategy', 'notes']),
192
271
  },
193
272
  async run(args, env) {
194
273
  const cfg = configFor(env.workspace, env.sessionId);
195
274
  await ensureNotebook(cfg);
196
- const notes = await searchNotes(cfg, {
275
+ const result = await searchNotesDetailed(cfg, {
197
276
  query: args.query ? String(args.query) : undefined,
198
277
  tags: Array.isArray(args.tags) ? args.tags.map(String) : undefined,
199
278
  since: args.since ? String(args.since) : undefined,
@@ -205,15 +284,19 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
205
284
  });
206
285
  return {
207
286
  notebook: cfg.notebook,
208
- count: notes.length,
209
- notes: notes.map((n) => ({ title: n.title, path: n.path, tags: n.tags, created: dateOnly(n.created), lead: n.lead })),
287
+ count: result.notes.length,
288
+ matchStrategy: result.matchStrategy,
289
+ notes: result.notes.map((n) => ({ title: n.title, path: n.path, tags: n.tags, created: dateOnly(n.created), lead: n.lead })),
210
290
  };
211
291
  },
212
292
  render(_args, value) {
213
293
  const v = value;
214
294
  if (!v.count)
215
295
  return text(`没有匹配的笔记(笔记本 ${v.notebook})`);
216
- return text(`${v.count} 条(笔记本 ${v.notebook})\n` + v.notes.map(noteLine).join('\n'));
296
+ const how = v.matchStrategy === 'mixed'
297
+ ? '(中文:全文 + 子串合并,已补上全文索引漏掉的)'
298
+ : v.matchStrategy === 'regex' ? '(全文无结果,已退回子串匹配)' : '';
299
+ return text(`${v.count} 条(笔记本 ${v.notebook})${how}\n` + v.notes.map(noteLine).join('\n'));
217
300
  },
218
301
  }),
219
302
  t({
@@ -376,10 +459,13 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
376
459
  description: '新建一篇笔记(zk 负责生成文件名、套用模板、写入索引)。适合记下一个概念、决定、来源或想法;' +
377
460
  '写前先 zk_search 查重。标题重复会直接报错(flat 按标题查重,layered 同目录同名 slug 也会被 zk 拒绝)。' +
378
461
  'layered 的文件名默认来自标题({{slug title}}):中文标题会生成很长的拼音文件名。' +
462
+ 'layered 不给 dir 就按 type 落到 notes/sources 之类;两者都没有、或给的 dir 不在任何 group 里都会报错,' +
463
+ '不会默默建到 notes/ 根(那里不套模板,没有 frontmatter,文件名是随机 id)。' +
379
464
  '要短文件名就传 name(8–14 字中文短语 / 英文小写短横线名):只换文件名,标题和正文一个字不动。' +
380
465
  '归档里有同名旧结论不会拦,但会放进 archived(带 supersede-reason):先读它,别把同一个错误结论重新提出来。' +
381
466
  'type / authority 只有 layered 模板会写进 frontmatter。tags 会写成 #hashtag;links 里的目标会被追加成 wiki 双链。' +
382
- '根目录 meta 文件(AGENTS / USER / MEMORY 等)与 raw/ 不能写;写进去一个会被当成人写的工作面、另一个会被排除出索引。',
467
+ '中文笔记如果没有任何标签(入参 / 默认值 / 正文 hashtag 都没有),会在 tagWarning 里提示 —— 只提示,不阻断。' +
468
+ '根目录 meta 文件(AGENTS / USER / MEMORY 等)与 raw/ 、inbox/ 不能写:前者会被当成人写的工作面,后两者会被排除出索引。',
383
469
  parameters: {
384
470
  type: 'object',
385
471
  properties: {
@@ -387,9 +473,9 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
387
473
  name: S.str('文件名(不含扩展名,也不要带目录);不给就由 zk 按标题生成。中文标题建议给一个短名,否则是长拼音'),
388
474
  body: S.str('正文 Markdown(可空)。会插在模板的 {{content}} 位置(layered 下在 ## Content 之下、## Links 之前):不要自己再写 ## Content / ## Summary 这类模板小节,会多出重复标题'),
389
475
  summary: S.str('一句话摘要(填进模板的 ## Summary;正文里已经写了这一节就不动)'),
390
- tags: S.arr('标签(不写 #)。与设置里的默认标签合并', S.str('标签名')),
391
- dir: S.str('放到哪个子目录(相对笔记本),默认用设置里的 notesDir'),
392
- type: S.enum('笔记类型(写进 frontmatter;layered 结构下决定模板的分节)', ['source', 'concept', 'entity', 'pattern']),
476
+ tags: S.arr('标签(不写 #)。与设置里的默认标签合并;正文里已经写过的标签不会再追加一行', S.str('标签名')),
477
+ dir: S.str('放到哪个子目录(相对笔记本)。flat 默认 notesDir;layered 必须落在某个 group(notes/sources 等,子目录也行),不给就按 type 落位;两者都没有、或不在 group 里都会报错'),
478
+ type: S.enum('笔记类型(写进 frontmatter;layered 结构下决定模板的分节,不给 dir 时也决定目录)', ['source', 'concept', 'entity', 'pattern']),
393
479
  authority: S.enum('可信度(写进 frontmatter,给未来的自己判断该不该引用)', ['official', 'expert', 'informal']),
394
480
  group: S.str('zk 的 group 名(一般不用给:目录会自动匹配 group)'),
395
481
  links: S.arr('要链接到的笔记(相对路径或标题)', S.str('目标笔记')),
@@ -406,6 +492,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
406
492
  links: { type: 'array', items: { type: 'string' } },
407
493
  summaryApplied: { type: 'boolean' },
408
494
  summaryWarning: { type: 'string' },
495
+ tagWarning: { type: 'string' },
409
496
  archived: {
410
497
  type: 'array',
411
498
  items: objSchema({
@@ -421,14 +508,18 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
421
508
  const cfg = configFor(env.workspace, env.sessionId);
422
509
  await ensureNotebook(cfg, env.signal);
423
510
  const tags = [...new Set([...(cfg.defaultTags ?? []), ...(Array.isArray(args.tags) ? args.tags.map(String) : [])])];
511
+ const type = args.type ? String(args.type) : undefined;
512
+ const title = String(args.title);
513
+ const body = args.body ? String(args.body) : '';
514
+ const content = withTags(body, tags);
424
515
  const created = await createNote(cfg, {
425
- title: String(args.title),
516
+ title,
426
517
  name: args.name ? String(args.name) : undefined,
427
518
  summary: args.summary ? String(args.summary) : undefined,
428
- content: withTags(args.body ? String(args.body) : '', tags),
429
- dir: args.dir ? String(args.dir) : cfg.notesDir,
519
+ content,
520
+ dir: newNoteDir(cfg, { dir: args.dir ? String(args.dir) : undefined, type }),
430
521
  group: args.group ? String(args.group) : undefined,
431
- type: args.type ? String(args.type) : undefined,
522
+ type,
432
523
  authority: args.authority ? String(args.authority) : undefined,
433
524
  }, env.signal);
434
525
  const links = [];
@@ -443,12 +534,18 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
443
534
  warnings.push(`${target}:${err instanceof Error ? err.message : String(err)}`);
444
535
  }
445
536
  }
537
+ // 中文没标签:写路径是唯一便宜的修复点(P2-9)。只提示,不阻断。
538
+ const tagWarning = taglessCjkWarning(title, content, tags);
446
539
  // 归档同名只是提醒:zk_new 的查重本来就不含 archive,允许复用标题 —— 但要把废弃原因一起交出来
447
540
  // (createNote 建之前那次全库扫描已经算好了,这里不再单独跑一次 zk)
448
541
  const archived = created.archived;
542
+ // 没有告警就不要写 summaryWarning: undefined —— 宿主按无损 JSON 快照校验输出,
543
+ // 值为 undefined 的字段会让整次调用报 INVALID_TOOL_OUTPUT(文件其实已经建好)。
449
544
  return {
450
545
  notebook: cfg.notebook, path: created.path, title: created.title, tags, links,
451
- summaryApplied: created.summaryApplied, summaryWarning: created.summaryWarning, archived, warnings,
546
+ summaryApplied: created.summaryApplied, archived, warnings,
547
+ ...(created.summaryWarning ? { summaryWarning: created.summaryWarning } : {}),
548
+ ...(tagWarning ? { tagWarning } : {}),
452
549
  };
453
550
  },
454
551
  render(_args, value) {
@@ -460,6 +557,8 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
460
557
  lines.push('双链:' + v.links.join(' '));
461
558
  if (v.summaryWarning)
462
559
  lines.push('⚠️ ' + v.summaryWarning);
560
+ if (v.tagWarning)
561
+ lines.push('⚠️ ' + v.tagWarning);
463
562
  if (v.archived.length) {
464
563
  lines.push('⚠️ 归档里有同名旧结论:' + v.archived.map((h) => h.path + (h.reason ? `(废弃原因:${h.reason})` : '(没有记录废弃原因)')).join(';') +
465
564
  ' —— 先读它,确认不是同一个结论再复用标题');
@@ -755,6 +854,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
755
854
  text: { type: 'string' },
756
855
  words: { type: 'number' },
757
856
  budget: { type: 'number' },
857
+ ratio: { type: 'number' },
758
858
  truncatedWords: { type: 'number' },
759
859
  tocWords: { type: 'number' },
760
860
  notebook: { type: 'string' },
@@ -778,7 +878,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
778
878
  originalWords: { type: 'number' },
779
879
  }, ['name', 'present', 'words', 'originalWords']),
780
880
  },
781
- }, ['text', 'words', 'budget', 'truncatedWords', 'tocWords', 'notebook', 'defaultNotebook', 'pinned', 'notebooks', 'files']),
881
+ }, ['text', 'words', 'budget', 'ratio', 'truncatedWords', 'tocWords', 'notebook', 'defaultNotebook', 'pinned', 'notebooks', 'files']),
782
882
  },
783
883
  async run(_args, env) {
784
884
  // 不 ensureNotebook:读记忆不该依赖 zk,笔记本不存在时就是空载荷
@@ -815,7 +915,12 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
815
915
  description: '给知识库做体检:断链 / 孤儿 / 无标签 / 单向链接 / 陈旧(N 天没改)/ 重名标题,每类最多 50 条并给总数。' +
816
916
  'frontmatter 解析失败的笔记单列一类:它们进不了索引,别的项统计不到,先修它们再看其余。' +
817
917
  'memoryLinks 查 MEMORY.md / USER.md 里的 [[来源链接]] 是否还指向活笔记(已废弃 / 不存在)—— ' +
818
- 'supersede 不会重写 meta 文件里的链接,这类漂移 broken-links 看不见。' +
918
+ 'supersede 不会重写 meta 文件里的链接,这类漂移 broken-links 看不见;另数没附链接(unlinked)、' +
919
+ '没写日期(undated)、已退役(retired)与退役后新结论失效(retiredBroken)的条目。' +
920
+ 'incomplete 是 frontmatter 缺 type / status / authority 的笔记:缺失被索引落成空串,机械门会静默失效。' +
921
+ 'tags 查标签卫生(大小写变体 / 单例,标签总数 ≥ 30 才报);template 说根目录 AGENTS.md 是不是当前版本。' +
922
+ 'similarTitles 是「改一两个字」的疑似重复(同目录、二元组相似度 ≥ 0.6):与全等重名分开,只报告。' +
923
+ 'indexHint=true 表示笔记(不含日记)已到 150 篇而根目录还没有 INDEX.md(只是提示,不生成静态索引)。' +
819
924
  '孤儿与无标签不含日记(按天写的日记天然如此,报出来只会淹没真问题)。' +
820
925
  'oneWayLinks.items 是缺回链的目标 X(不是来源),from 是没被 X 回链的来源 S;' +
821
926
  'edges 是全部单向边数(items 截到 50 条时仍计全量),bodyOnly 是正文引用(不在 ## Links / ## 链接 小节)的单向边数。' +
@@ -845,8 +950,13 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
845
950
  tagless: LINT_BUCKET,
846
951
  oneWayLinks: LINT_ONE_WAY,
847
952
  duplicateTitles: LINT_DUPLICATE,
953
+ similarTitles: LINT_SIMILAR,
848
954
  memoryLinks: LINT_MEMORY,
849
- }, ['notebook', 'notes', 'truncated', 'staleDays', 'parseErrors', 'orphans', 'brokenLinks', 'stale', 'tagless', 'oneWayLinks', 'duplicateTitles', 'memoryLinks']),
955
+ incomplete: LINT_INCOMPLETE,
956
+ tags: LINT_TAGS,
957
+ template: LINT_TEMPLATE,
958
+ indexHint: { type: 'boolean' },
959
+ }, ['notebook', 'notes', 'truncated', 'staleDays', 'parseErrors', 'orphans', 'brokenLinks', 'stale', 'tagless', 'oneWayLinks', 'duplicateTitles', 'similarTitles', 'memoryLinks', 'incomplete', 'tags', 'template', 'indexHint']),
850
960
  },
851
961
  async run(args, env) {
852
962
  const cfg = configFor(env.workspace, env.sessionId);
@@ -898,6 +1008,9 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
898
1008
  const memLine = formatMemoryLinks(v.memoryLinks);
899
1009
  if (memLine)
900
1010
  lines.push(memLine);
1011
+ lines.push(...formatLintExtras(v));
1012
+ if (v.similarTitles.truncated)
1013
+ lines.push('⚠️ 疑似重复只比较了部分笔记(同目录超过 300 篇或扫描触顶):可能漏报');
901
1014
  if (v.truncated)
902
1015
  lines.push('⚠️ 笔记数触到扫描上限:结果不完整(只会漏报,不会误报),先归档旧笔记再跑一次');
903
1016
  if (oneWay.edgesTruncated)
@@ -910,11 +1023,15 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
910
1023
  name: 'zk_dream',
911
1024
  description: '整理晋升材料:最近 N 天新增的笔记(机械筛掉 authority=informal 与无标签的,其余作为候选)、' +
912
1025
  'daily 里没划掉的 Loose ends、被引用最多的笔记。候选带正文摘要(lead)与入链来源(citedBy),方便重放上下文。' +
1026
+ 'topCited 先按「被日记提到几天」排(使用信号),入链数只作并列次序;没有日记时退化为原排序。' +
1027
+ '会读 MEMORY.md:已经晋升过的(`[[链接]]` 命中或条目里写了标题)移出候选、进 alreadyPromoted —— 不会每周原样重复。' +
1028
+ 'backlog 是窗口外仍未晋升的笔记(按被引数),漏做一次不会永久掉队。' +
1029
+ 'days 省略时窗口 = 距 DREAMS.md「上次做梦」的天数(没有则 7 天)。' +
913
1030
  '只返回材料:写 MEMORY.md / DREAMS.md 由你按 AGENTS.md 决定,这个工具不写任何文件。',
914
1031
  parameters: {
915
1032
  type: 'object',
916
1033
  properties: {
917
- days: S.num('看最近多少天(1-365,默认 7)'),
1034
+ days: S.num('看最近多少天(1-365);省略时 = 距 DREAMS.md「上次做梦」的天数,没有这行才退回 7'),
918
1035
  },
919
1036
  additionalProperties: false,
920
1037
  },
@@ -930,28 +1047,26 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
930
1047
  candidates: { type: 'number' },
931
1048
  rejected: { type: 'number' },
932
1049
  looseEnds: { type: 'number' },
933
- }, ['window', 'daily', 'candidates', 'rejected', 'looseEnds']),
934
- newNotes: {
935
- type: 'array',
936
- items: objSchema({
937
- title: { type: 'string' },
938
- path: { type: 'string' },
939
- type: { type: 'string' },
940
- authority: { type: 'string' },
941
- tags: { type: 'array', items: { type: 'string' } },
942
- created: { type: 'string' },
943
- lead: { type: 'string' },
944
- cited: { type: 'number' },
945
- citedBy: { type: 'array', items: { type: 'string' } },
946
- }, ['title', 'path', 'type', 'authority', 'tags', 'created', 'lead', 'cited', 'citedBy']),
947
- },
1050
+ promoted: { type: 'number' },
1051
+ backlog: { type: 'number' },
1052
+ }, ['window', 'daily', 'candidates', 'rejected', 'looseEnds', 'promoted', 'backlog']),
1053
+ newNotes: { type: 'array', items: DREAM_CANDIDATE },
1054
+ alreadyPromoted: { type: 'array', items: DREAM_CANDIDATE },
1055
+ backlog: { type: 'array', items: DREAM_CANDIDATE },
948
1056
  rejected: {
949
1057
  type: 'array',
950
1058
  items: objSchema({ title: { type: 'string' }, path: { type: 'string' }, reason: { type: 'string' } }, ['title', 'path', 'reason']),
951
1059
  },
952
1060
  topCited: {
953
1061
  type: 'array',
954
- items: objSchema({ title: { type: 'string' }, path: { type: 'string' }, cited: { type: 'number' } }, ['title', 'path', 'cited']),
1062
+ items: objSchema({
1063
+ title: { type: 'string' },
1064
+ path: { type: 'string' },
1065
+ cited: { type: 'number' },
1066
+ dailyMentions: { type: 'number' },
1067
+ daysMentioned: { type: 'number' },
1068
+ lastMentioned: { type: 'string' },
1069
+ }, ['title', 'path', 'cited', 'dailyMentions', 'daysMentioned', 'lastMentioned']),
955
1070
  },
956
1071
  dailyLooseEnds: {
957
1072
  type: 'array',
@@ -961,7 +1076,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
961
1076
  items: { type: 'array', items: { type: 'string' } },
962
1077
  }, ['path', 'date', 'items']),
963
1078
  },
964
- }, ['notebook', 'windowDays', 'since', 'truncated', 'counts', 'newNotes', 'rejected', 'topCited', 'dailyLooseEnds']),
1079
+ }, ['notebook', 'windowDays', 'since', 'truncated', 'counts', 'newNotes', 'alreadyPromoted', 'backlog', 'rejected', 'topCited', 'dailyLooseEnds']),
965
1080
  },
966
1081
  async run(args, env) {
967
1082
  const cfg = configFor(env.workspace, env.sessionId);
@@ -973,7 +1088,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
973
1088
  const lines = [
974
1089
  '做梦:近 ' + v.windowDays + ' 天(自 ' + v.since + ')— 窗口 ' + v.counts.window +
975
1090
  ' 篇(日记 ' + v.counts.daily + ')· 候选 ' + v.counts.candidates + ' · 拒绝 ' + v.counts.rejected +
976
- ' · 未完成 ' + v.counts.looseEnds,
1091
+ ' · 未完成 ' + v.counts.looseEnds + ' · 已晋升 ' + v.counts.promoted + ' · 积压 ' + v.counts.backlog,
977
1092
  ];
978
1093
  if (v.newNotes.length) {
979
1094
  lines.push('晋升候选:');
@@ -981,13 +1096,29 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
981
1096
  const badges = [n.type || '—', n.authority || '—'].join(' · ');
982
1097
  const lead = n.lead ? '\n ' + n.lead : '';
983
1098
  const citedBy = n.citedBy.length ? '\n 被引:' + n.citedBy.join('、') : '';
1099
+ const used = n.daysMentioned > 0 ? ' · 日记 ' + n.daysMentioned + ' 天' : '';
984
1100
  lines.push(' ' + (i + 1) + '. ' + (n.title || n.path) + ' [' + badges + '] ' + n.path +
985
1101
  (n.tags.length ? ' · ' + n.tags.map((t) => '#' + t).join(' ') : '') +
986
- ' · 被引 ' + n.cited + lead + citedBy);
1102
+ ' · 被引 ' + n.cited + used + lead + citedBy);
987
1103
  }
988
1104
  if (v.newNotes.length > 10)
989
1105
  lines.push(' …还有 ' + (v.newNotes.length - 10) + ' 篇');
990
1106
  }
1107
+ if (v.alreadyPromoted.length) {
1108
+ lines.push('已在 MEMORY(不再重复提):');
1109
+ for (const n of v.alreadyPromoted.slice(0, 10)) {
1110
+ lines.push(' · ' + (n.title || n.path) + '(' + n.path + ')· ' + (n.promotedBy === 'link' ? '按链接命中' : '标题出现在条目里'));
1111
+ }
1112
+ if (v.alreadyPromoted.length > 10)
1113
+ lines.push(' …还有 ' + (v.alreadyPromoted.length - 10) + ' 篇');
1114
+ }
1115
+ if (v.backlog.length) {
1116
+ lines.push('窗口外仍未晋升(按被引数):');
1117
+ for (const n of v.backlog.slice(0, 10))
1118
+ lines.push(' · ' + (n.title || n.path) + '(' + n.path + ')· 被引 ' + n.cited);
1119
+ if (v.counts.backlog > v.backlog.length)
1120
+ lines.push(' …还有 ' + (v.counts.backlog - v.backlog.length) + ' 篇');
1121
+ }
991
1122
  if (v.rejected.length) {
992
1123
  lines.push('机械拒绝(不进候选):');
993
1124
  for (const r of v.rejected.slice(0, 10))
@@ -1002,8 +1133,10 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
1002
1133
  }
1003
1134
  if (v.topCited.length) {
1004
1135
  lines.push('被引用最多:');
1005
- for (const [i, c] of v.topCited.slice(0, 5).entries())
1006
- lines.push(' ' + (i + 1) + '. ' + (c.title || c.path) + '(' + c.path + ')· ' + c.cited + ' 次');
1136
+ for (const [i, c] of v.topCited.slice(0, 5).entries()) {
1137
+ lines.push(' ' + (i + 1) + '. ' + (c.title || c.path) + '(' + c.path + ')· 被引 ' + c.cited + ' 次' +
1138
+ (c.daysMentioned > 0 ? ' · 日记 ' + c.daysMentioned + ' 天(最近 ' + c.lastMentioned + ')' : ''));
1139
+ }
1007
1140
  }
1008
1141
  if (!v.newNotes.length && !v.dailyLooseEnds.length && !v.topCited.length)
1009
1142
  lines.push('这个窗口里没有新材料。');
@@ -1015,7 +1148,8 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
1015
1148
  }),
1016
1149
  t({
1017
1150
  name: 'zk_stats',
1018
- description: '看一眼知识库现状:笔记数、标签数、断链数、孤立笔记数、已归档(superseded)数。适合开始工作前了解上下文规模。',
1151
+ description: '看一眼知识库现状:笔记数、标签数、断链数、孤立笔记数、已归档(superseded)数,以及顶层目录 / 前 10 个标签的分布(byDir / byTag)。' +
1152
+ '适合开始工作前了解上下文规模;笔记多了以后用它判断该先看哪里(不是排行榜)。',
1019
1153
  parameters: { type: 'object', properties: {}, additionalProperties: false },
1020
1154
  output: {
1021
1155
  schema: objSchema({
@@ -1028,7 +1162,15 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
1028
1162
  orphans: { type: 'number' },
1029
1163
  archived: { type: 'number' },
1030
1164
  superseded: { type: 'number' },
1031
- }, ['notebook', 'defaultNotebook', 'pinned', 'notes', 'tags', 'brokenLinks', 'orphans', 'archived', 'superseded']),
1165
+ byDir: {
1166
+ type: 'array',
1167
+ items: objSchema({ dir: { type: 'string' }, count: { type: 'number' } }, ['dir', 'count']),
1168
+ },
1169
+ byTag: {
1170
+ type: 'array',
1171
+ items: objSchema({ tag: { type: 'string' }, count: { type: 'number' } }, ['tag', 'count']),
1172
+ },
1173
+ }, ['notebook', 'defaultNotebook', 'pinned', 'notes', 'tags', 'brokenLinks', 'orphans', 'archived', 'superseded', 'byDir', 'byTag']),
1032
1174
  },
1033
1175
  async run(_args, env) {
1034
1176
  const cfg = configFor(env.workspace, env.sessionId);
@@ -1047,8 +1189,15 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
1047
1189
  const drift = v.pinned && v.pinned !== v.defaultNotebook
1048
1190
  ? `\n本会话固定在「${v.pinned}」;全局默认是「${v.defaultNotebook}」(要跟随用 zk_use ${v.defaultNotebook})`
1049
1191
  : '';
1192
+ const dirs = v.byDir.length
1193
+ ? '\n目录:' + v.byDir.map((d) => `${d.dir === '.' ? '(根目录)' : d.dir} ${d.count}`).join(' · ')
1194
+ : '';
1195
+ const tags = v.byTag.length
1196
+ ? '\n标签:' + v.byTag.map((t) => `#${t.tag} ${t.count}`).join(' · ')
1197
+ : '';
1050
1198
  return text(`笔记本 ${v.notebook}${drift}\n` +
1051
- `笔记 ${v.notes} 篇 · 标签 ${v.tags} 个 · 断链 ${v.brokenLinks} 篇 · 孤立 ${v.orphans} 篇 · 归档 ${v.archived} 篇(superseded ${v.superseded})`);
1199
+ `笔记 ${v.notes} 篇 · 标签 ${v.tags} 个 · 断链 ${v.brokenLinks} 篇 · 孤立 ${v.orphans} 篇 · 归档 ${v.archived} 篇(superseded ${v.superseded})` +
1200
+ dirs + tags);
1052
1201
  },
1053
1202
  }),
1054
1203
  t({
@@ -1,45 +1,83 @@
1
1
  /**
2
- * 根目录 AGENTS.md 模板:layered 初始化时写进 vault,已存在就不碰。
2
+ * 根目录 AGENTS.md 模板:初始化时写进 vault,已存在就不碰。
3
+ *
4
+ * 两份模板:layered 用 AGENTS_TEMPLATE(目录 / group / WORKFLOW 那些路径都存在),
5
+ * flat 用 AGENTS_TEMPLATE_FLAT(只有 notes/ + 日记,不出现 raw / inbox / archive / WORKFLOW)。
6
+ * flat 默认不写(0.1.0 的 vault 升级过来必须一个文件都不多),要的人显式 `zk-kit init --with-agents`
7
+ * 或在设置页点「写入 AGENTS.md 模板」—— 否则默认安装既没有约定、也没有周节奏提醒。
3
8
  *
4
9
  * 为什么单独一个文件:它是「给人读的文档」,不是逻辑;放在这里也方便直接对照
5
10
  * docs/features/memory.md 的三条口径(检索词归宿 / 中文必打 tags / 宿主不会自动加载)。
11
+ * 注意别让运行期消息只指向 docs/:插件发布后文档在 profile 的 node_modules 里,agent 够不着;
12
+ * lint 的旧版提示直接带模板全文(见 lib/lint.ts 的 LintTemplateBucket.currentText)。
6
13
  *
7
14
  * 长度是设计约束:AGENTS.md 属于注入预算(默认 500 词,中文 2 字 = 1 词)且**永不截断**,
8
15
  * 模板自己吃掉过半预算,USER / MEMORY 就没位置了 —— 所以这里只留规则,不写教程。
9
- * 目标 ~175 词(195 → 160 → 175):每多 10 词,MEMORY 就少 10 词的正文,规则能紧就紧。
16
+ * 目标 ≤190 词(AGENTS_TEMPLATE 实测 186、AGENTS_TEMPLATE_FLAT 实测 88),单测里锁住。
17
+ * 首行 `<!-- zdiary-template: vN -->` 是版本标记:lint 据此报告「老库的 AGENTS.md 是旧版」,
18
+ * 只报告不覆盖(模板允许用户改)。
10
19
  */
11
- export const AGENTS_TEMPLATE = `# 知识库工作约定
20
+ /** 模板版本:改模板就 +1;lint 用它判断老库是否还在用旧契约(v2:去掉 inbox/) */
21
+ export const TEMPLATE_VERSION = 2;
22
+ /** 首行版本标记(实测 4 词);不许带日期 —— 带日期的写法实测 6 词,白花 2 词 */
23
+ export const TEMPLATE_MARK = `<!-- zdiary-template: v${TEMPLATE_VERSION} -->`;
24
+ export const AGENTS_TEMPLATE = `${TEMPLATE_MARK}
25
+
26
+ # 知识库工作约定
12
27
 
13
- > zdiary 生成;DSH 不会自动加载,由插件每轮注入。
28
+ > zdiary 生成;DSH 不会自动加载,插件每轮注入。
14
29
 
15
30
  ## 目录
16
31
 
17
- raw/ 只读材料 · inbox/ 待归类 · notes/sources/ 来源
32
+ raw/ 只读材料 · notes/sources/ 来源
18
33
  notes/{concepts,entities,patterns}/ 原子笔记 · archive/ 废弃(不删)
19
34
 
20
35
  ## 检索词
21
36
 
22
37
  值得检索 → tags 或正文;不值得 → 不写。
23
- 中文必打 tags:全文索引不切中文,只查正文会静默查空,进而建重复笔记。
38
+ 中文必打 tags:全文索引不切中文,只查正文会静默查空、建重复笔记。
24
39
 
25
40
  ## 写之前
26
41
 
27
42
  1. zk_query / zk_search 查重(中文查 tags)。
28
- 2. 一条笔记一件事,标题即结论;文件名用 zk_new 的 name 给 8–14 字短名(不给就是按标题生成的拼音长名);来源进 sources/,结论进 concepts/,zk_link 相连。
43
+ 2. 一条笔记一件事,标题即结论;文件名用 zk_new 的 name(8–14 字);来源给 type=source、结论给 type=concept(目录跟 type 走,别落 notes/ 根),zk_link 相连。
29
44
  3. 结论过时用 zk_supersede(默认预演,带 reason):不删文件、不改旧结论。
30
45
  4. 改已有笔记前读 WORKFLOW.md:先列清单等确认,再按三分类动手。
31
46
 
32
47
  ## 每周
33
48
 
34
- 把「上次做梦:YYYY-MM-DD」写进 DREAMS.md;到期或缺这行时:
35
- zk_lint → zk_dream → 写 DREAMS.md → 人裁决后改 MEMORY.md。
36
- MEMORY.md 每条结论附 [[来源路径]],zk_lint 核对。
49
+ 每周:DREAMS.md 写「上次做梦:YYYY-MM-DD」;到期 zk_lint → zk_dream → 人裁决改 MEMORY。
50
+ MEMORY.md 每条:结论(YYYY-MM-DD)[[来源路径]];退役写 - [superseded YYYY-MM-DD] 旧结论 → [[新结论]],行不删。
51
+ 新条目写顶部;超预算从底部往前丢。
52
+ `;
53
+ /**
54
+ * flat 结构用的模板:不出现 raw / inbox / archive / WORKFLOW.md 这些 flat 没有的路径。
55
+ * 目标 ≤90 词 —— 它和服务端那份共用同一笔预算,而 flat 用户往往只是想要「有一个约定」。
56
+ */
57
+ export const AGENTS_TEMPLATE_FLAT = `${TEMPLATE_MARK}
58
+
59
+ # 知识库工作约定
60
+
61
+ > zdiary 生成;DSH 不会自动加载,插件每轮注入。
62
+
63
+ ## 写之前
64
+
65
+ 1. zk_search 查重;中文必打 tags(FTS 不切中文)。
66
+ 2. 一笔记一件事,标题即结论;文件名 8–14 字。
67
+ 3. 结论过时用 zk_supersede(默认预演,带 reason)。
68
+
69
+ ## 每周
70
+
71
+ DREAMS.md 写「上次做梦:YYYY-MM-DD」;到期 zk_lint → zk_dream → 人裁决改 MEMORY。
72
+ MEMORY.md 每条:结论(YYYY-MM-DD)[[来源路径]];退役写 - [superseded] 旧结论 → [[新结论]]。
37
73
  `;
38
74
  /**
39
75
  * 根目录 WORKFLOW.md:只在「要动已有笔记」时才需要读,所以不注入、不计预算。
40
76
  * 放进注入的话,AGENTS.md 一膨胀就会挤掉 MEMORY —— 这些规则低频但重要,适合按需读。
41
77
  */
42
- export const WORKFLOW_TEMPLATE = `# 改笔记的工作流(按需读)
78
+ export const WORKFLOW_TEMPLATE = `${TEMPLATE_MARK}
79
+
80
+ # 改笔记的工作流(按需读)
43
81
 
44
82
  > 由 dsh-zdiary-kit 生成。AGENTS.md 只放每轮都要用的规则;这一页在要动已有笔记时才读。
45
83
  > 它不进检索,也不要往里追加内容(整篇重写,git diff 可审)。