dsh-zdiary-kit 0.6.2 → 0.7.1

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 CHANGED
@@ -1,4 +1,4 @@
1
- # dsh-zdiary-kit 0.6.2
1
+ # dsh-zdiary-kit 0.7.1
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),
package/dist/cli.js CHANGED
@@ -23,7 +23,8 @@ const HELP = `zk-kit —— dsh-zdiary-kit 的命令行
23
23
  zk-kit daily [--entry 一段] [--date yesterday]
24
24
  zk-kit append <路径> --text 一段 [--heading 标题]
25
25
  zk-kit link <来源路径> <目标路径或标题>
26
- zk-kit lint [--stale-days 90] 体检:断链 / 孤儿 / 无标签 / 单向链接 / 陈旧 / 重名(只读)
26
+ zk-kit lint [--stale-days 90] [--links-only]
27
+ 体检:断链 / 孤儿 / 无标签 / 单向链接 / 陈旧 / 重名(只读)
27
28
  zk-kit dream [--days 7] 晋升材料:窗口新增 / daily 未完成 / 被引最多(只读)
28
29
  zk-kit query [-- <zk list 参数>] 透传 zk list(--notebook-dir / --format / -x 会被替换掉)
29
30
  zk-kit index 重建索引
@@ -146,7 +147,19 @@ function lintReport(r) {
146
147
  lines.push(row('断链', r.brokenLinks.total, r.brokenLinks.items));
147
148
  lines.push(row('孤儿', r.orphans.total, r.orphans.items));
148
149
  lines.push(row('无标签', r.tagless.total, r.tagless.items));
149
- lines.push(row('单向链接', r.oneWayLinks.total, r.oneWayLinks.items));
150
+ const oneWay = r.oneWayLinks;
151
+ const oneWayLabel = oneWay.linksOnly ? '单向链接(Links)' : '单向链接';
152
+ const bodyNote = oneWay.bodyOnly > 0
153
+ ? (oneWay.linksOnly ? `(另有正文引用 ${oneWay.bodyOnly} 条未计)` : `(正文引用 ${oneWay.bodyOnly} 条)`)
154
+ : '';
155
+ if (oneWay.total === 0)
156
+ lines.push(`✓ ${oneWayLabel}:0${bodyNote}`);
157
+ else {
158
+ const items = oneWay.items.slice(0, 5).map((i) => `${i.path} ← ${i.from.join('、')}`);
159
+ lines.push(`✗ ${oneWayLabel}:${oneWay.total} 篇 / ${oneWay.edges} 条单向边` +
160
+ bodyNote +
161
+ ` — ${items.join(';')}${oneWay.total > items.length ? ' …' : ''}`);
162
+ }
150
163
  lines.push(r.stale.total === 0
151
164
  ? '✓ 陈旧:0'
152
165
  : `✗ 陈旧:${r.stale.total} 篇 — ` + r.stale.items.slice(0, 5).map((s) => `${s.path}(${s.modified})`).join('、'));
@@ -157,7 +170,7 @@ function lintReport(r) {
157
170
  if (memLine)
158
171
  lines.push(memLine);
159
172
  if (r.truncated)
160
- lines.push('⚠️ 笔记数触到扫描上限:结果不完整');
173
+ lines.push('⚠️ 笔记数触到扫描上限:结果不完整(只会漏报,不会误报)');
161
174
  lines.push('(只报告,没有改动任何文件)');
162
175
  return lines.join('\n');
163
176
  }
@@ -345,7 +358,10 @@ async function main() {
345
358
  }
346
359
  case 'lint': {
347
360
  await ensureNotebook(cfg);
348
- const r = await lintNotebook(cfg, { staleDays: Number(str(flags['stale-days'])) || 90 });
361
+ const r = await lintNotebook(cfg, {
362
+ staleDays: Number(str(flags['stale-days'])) || 90,
363
+ linksOnly: flags['links-only'] === true || flags['links-only'] === 'true',
364
+ });
349
365
  print(json ? r : lintReport(r), json);
350
366
  return;
351
367
  }
@@ -29,6 +29,19 @@ const objSchema = (properties, required) => ({
29
29
  /** lint 输出的三个共用形状(每类最多 50 条 + 总数) */
30
30
  const LINT_ENTRY = objSchema({ title: { type: 'string' }, path: { type: 'string' } }, ['title', 'path']);
31
31
  const LINT_BUCKET = objSchema({ total: { type: 'number' }, items: { type: 'array', items: LINT_ENTRY } }, ['total', 'items']);
32
+ const LINT_ONE_WAY_ITEM = objSchema({
33
+ title: { type: 'string' },
34
+ path: { type: 'string' },
35
+ from: { type: 'array', items: { type: 'string' } },
36
+ where: { type: 'string', enum: ['links', 'body', 'mixed'] },
37
+ }, ['title', 'path', 'from', 'where']);
38
+ const LINT_ONE_WAY = objSchema({
39
+ total: { type: 'number' },
40
+ edges: { type: 'number' },
41
+ bodyOnly: { type: 'number' },
42
+ linksOnly: { type: 'boolean' },
43
+ items: { type: 'array', items: LINT_ONE_WAY_ITEM },
44
+ }, ['total', 'edges', 'bodyOnly', 'linksOnly', 'items']);
32
45
  const LINT_STALE = objSchema({
33
46
  total: { type: 'number' },
34
47
  items: {
@@ -794,12 +807,17 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
794
807
  'memoryLinks 查 MEMORY.md / USER.md 里的 [[来源链接]] 是否还指向活笔记(已废弃 / 不存在)—— ' +
795
808
  'supersede 不会重写 meta 文件里的链接,这类漂移 broken-links 看不见。' +
796
809
  '孤儿与无标签不含日记(按天写的日记天然如此,报出来只会淹没真问题)。' +
797
- 'oneWayLinks = S 链 X 但 X 没回链 S,且 S 不是日记 —— 日记天然是出边,回链它没有意义。' +
810
+ 'oneWayLinks.items 是缺回链的目标 X(不是来源),from 是没被 X 回链的来源 S;' +
811
+ 'edges 是列出的单向边数,bodyOnly 是正文引用(不在 ## Links / ## 链接 小节)的单向边数。' +
812
+ 'links_only=true 时只列 Links 小节的单向链接,正文引用只进 bodyOnly,适合把桶当可修清单。' +
813
+ 'S 或 X 是日记的边不算 —— 日记天然是出边,回链它没有意义。' +
814
+ '触到扫描上限时结果只会漏报,不会误报。' +
798
815
  '只读:不改任何笔记。拿到清单后自己判断先修哪个,再用 zk_link / zk_append / zk_supersede 动手。',
799
816
  parameters: {
800
817
  type: 'object',
801
818
  properties: {
802
819
  stale_days: S.num('多少天没修改算陈旧(1-3650,默认 90)'),
820
+ links_only: S.bool('只报 Links 小节的单向链接;正文引用只计数(默认 false)'),
803
821
  },
804
822
  additionalProperties: false,
805
823
  },
@@ -814,7 +832,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
814
832
  brokenLinks: LINT_BUCKET,
815
833
  stale: LINT_STALE,
816
834
  tagless: LINT_BUCKET,
817
- oneWayLinks: LINT_BUCKET,
835
+ oneWayLinks: LINT_ONE_WAY,
818
836
  duplicateTitles: LINT_DUPLICATE,
819
837
  memoryLinks: LINT_MEMORY,
820
838
  }, ['notebook', 'notes', 'truncated', 'staleDays', 'parseErrors', 'orphans', 'brokenLinks', 'stale', 'tagless', 'oneWayLinks', 'duplicateTitles', 'memoryLinks']),
@@ -824,6 +842,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
824
842
  await ensureNotebook(cfg, env.signal);
825
843
  return lintNotebook(cfg, {
826
844
  staleDays: args.stale_days === undefined ? undefined : Number(args.stale_days),
845
+ linksOnly: args.links_only === true,
827
846
  }, env.signal);
828
847
  },
829
848
  render(_args, value) {
@@ -843,7 +862,19 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
843
862
  lines.push(line('断链', v.brokenLinks));
844
863
  lines.push(line('孤儿', v.orphans));
845
864
  lines.push(line('无标签', v.tagless));
846
- lines.push(line('单向链接', v.oneWayLinks));
865
+ const oneWay = v.oneWayLinks;
866
+ const oneWayLabel = oneWay.linksOnly ? '单向链接(Links)' : '单向链接';
867
+ const bodyNote = oneWay.bodyOnly > 0
868
+ ? (oneWay.linksOnly ? '(另有正文引用 ' + oneWay.bodyOnly + ' 条未计)' : '(正文引用 ' + oneWay.bodyOnly + ' 条)')
869
+ : '';
870
+ if (oneWay.total === 0)
871
+ lines.push('✓ ' + oneWayLabel + ':0' + bodyNote);
872
+ else {
873
+ const items = oneWay.items.slice(0, 5).map((i) => i.path + ' ← ' + i.from.join('、'));
874
+ lines.push('✗ ' + oneWayLabel + ':' + oneWay.total + ' 篇 / ' + oneWay.edges + ' 条单向边' +
875
+ bodyNote +
876
+ ' — ' + items.join(';') + (oneWay.total > items.length ? ' …' : ''));
877
+ }
847
878
  lines.push(v.stale.total === 0
848
879
  ? '✓ 陈旧:0'
849
880
  : '✗ 陈旧:' + v.stale.total + ' 篇 — ' +
@@ -857,7 +888,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
857
888
  if (memLine)
858
889
  lines.push(memLine);
859
890
  if (v.truncated)
860
- lines.push('⚠️ 笔记数触到扫描上限:结果不完整,先归档旧笔记再跑一次');
891
+ lines.push('⚠️ 笔记数触到扫描上限:结果不完整(只会漏报,不会误报),先归档旧笔记再跑一次');
861
892
  lines.push('(只报告,没有改动任何文件)');
862
893
  return text(lines.join('\n'));
863
894
  },
package/dist/lib/lint.js CHANGED
@@ -201,16 +201,10 @@ export function formatMemoryLinks(bucket) {
201
201
  parts.push(`${bucket.unlinked} 个条目没附来源链接`);
202
202
  return `${bucket.total === 0 ? '✓' : '✗'} 记忆链接:${parts.join('|')}`;
203
203
  }
204
- /**
205
- * 单向链接的目标:存在 S→X,但 X 没有回链 S(zk `--missing-backlink` 的原义)。
206
- * 不用 zk 的过滤器,因为它没法排除「来源是日记」的边 —— 日记按天写、天然是一串出边,
207
- * 要求每篇被提过的笔记回链日记只会让这个桶常年清零不了,跟「笔记没接上」是两回事。
208
- * 返回绝对路径(与 `bucket` 的 `byAbs` 同口径);指向索引外笔记的边直接丢掉。
209
- */
210
204
  export function oneWayLinks(edges, notes, skipSource) {
211
205
  const linked = new Set(edges.map((e) => e.sourcePath + '\u0000' + e.targetPath));
212
206
  const byPath = new Map(notes.map((n) => [n.path, n]));
213
- const out = new Set();
207
+ const out = new Map();
214
208
  for (const edge of edges) {
215
209
  if (edge.sourcePath === edge.targetPath)
216
210
  continue;
@@ -218,11 +212,20 @@ export function oneWayLinks(edges, notes, skipSource) {
218
212
  continue;
219
213
  if (linked.has(edge.targetPath + '\u0000' + edge.sourcePath))
220
214
  continue;
221
- const target = byPath.get(edge.targetPath);
222
- if (target)
223
- out.add(path.resolve(target.absPath || target.path));
215
+ if (!byPath.has(edge.targetPath))
216
+ continue;
217
+ const sources = out.get(edge.targetPath) ?? new Map();
218
+ const starts = sources.get(edge.sourcePath) ?? [];
219
+ starts.push(edge.snippetStart);
220
+ sources.set(edge.sourcePath, starts);
221
+ out.set(edge.targetPath, sources);
224
222
  }
225
- return [...out];
223
+ return [...out].map(([target, sources]) => ({
224
+ target,
225
+ from: [...sources]
226
+ .map(([source, snippetStarts]) => ({ path: source, snippetStarts }))
227
+ .sort((a, b) => a.path.localeCompare(b.path)),
228
+ }));
226
229
  }
227
230
  export async function lintNotebook(cfg, opts = {}, signal) {
228
231
  const staleDays = Math.min(3650, Math.max(1, Math.trunc(opts.staleDays ?? 90)));
@@ -240,8 +243,9 @@ export async function lintNotebook(cfg, opts = {}, signal) {
240
243
  const taglessAbs = await notesWithFlag(cfg, '--tagless', signal);
241
244
  const edges = await graphEdges(cfg, signal);
242
245
  // 图里的路径是笔记本相对:先拼回绝对路径再判断是不是日记(isDaily 收的是绝对路径)
243
- const oneWayAbs = oneWayLinks(edges, notes, (p) => isDaily(path.resolve(cfg.notebook, p)));
246
+ const oneWay = oneWayLinks(edges, notes, (p) => isDaily(path.resolve(cfg.notebook, p)));
244
247
  const byAbs = new Map(notes.map((n) => [path.resolve(n.absPath || n.path), n]));
248
+ const byPath = new Map(notes.map((n) => [n.path, n]));
245
249
  const bucket = (absPaths, max = MAX_ITEMS) => {
246
250
  const items = absPaths
247
251
  .map((abs) => byAbs.get(path.resolve(abs)))
@@ -251,6 +255,94 @@ export async function lintNotebook(cfg, opts = {}, signal) {
251
255
  .map((n) => ({ title: n.title, path: n.path }));
252
256
  return { total: absPaths.length, items };
253
257
  };
258
+ // `zk graph` 的 snippetStart 是 UTF-8 字节偏移:Links 小节也按字节算,才能直接比较。
259
+ // 只读有单向边的来源文件(当前库里个位数),不按笔记逐篇读。
260
+ const sectionCache = new Map();
261
+ const linksSection = (rel) => {
262
+ if (sectionCache.has(rel))
263
+ return sectionCache.get(rel) ?? null;
264
+ let text = '';
265
+ try {
266
+ text = fs.readFileSync(path.resolve(cfg.notebook, rel), 'utf8');
267
+ }
268
+ catch {
269
+ sectionCache.set(rel, null);
270
+ return null;
271
+ }
272
+ const head = /^##[ \t]+(?:Links|链接)[ \t]*\r?$/m.exec(text);
273
+ if (!head) {
274
+ sectionCache.set(rel, null);
275
+ return null;
276
+ }
277
+ const after = head.index + head[0].length;
278
+ const next = /^#{1,2}[ \t]+\S/m.exec(text.slice(after));
279
+ const endChar = next ? after + next.index : text.length;
280
+ const section = {
281
+ start: Buffer.byteLength(text.slice(0, head.index), 'utf8'),
282
+ end: Buffer.byteLength(text.slice(0, endChar), 'utf8'),
283
+ };
284
+ sectionCache.set(rel, section);
285
+ return section;
286
+ };
287
+ const whereOf = (rel, starts) => {
288
+ const section = linksSection(rel);
289
+ if (!section)
290
+ return 'body';
291
+ return starts.some((s) => s >= section.start && s < section.end) ? 'links' : 'body';
292
+ };
293
+ const linksOnly = opts.linksOnly === true;
294
+ const oneWayItems = [];
295
+ let oneWayEdges = 0;
296
+ let bodyOnly = 0;
297
+ for (const row of oneWay) {
298
+ // 目标也不报日记:普通笔记链到日记("今天做了 X")同样是正常引用方向,
299
+ // 要求日记回链每篇被提到的笔记,跟要求笔记回链日记是同一个问题。
300
+ if (isDaily(path.resolve(cfg.notebook, row.target)))
301
+ continue;
302
+ const target = byPath.get(row.target);
303
+ if (!target)
304
+ continue;
305
+ const all = [];
306
+ const links = [];
307
+ const body = [];
308
+ for (const source of row.from) {
309
+ all.push(source.path);
310
+ if (whereOf(source.path, source.snippetStarts) === 'links')
311
+ links.push(source.path);
312
+ else
313
+ body.push(source.path);
314
+ }
315
+ // bodyOnly 始终统计全部正文边:linksOnly 时它们被隐藏,但要让调用方知道有多少
316
+ bodyOnly += body.length;
317
+ if (linksOnly && links.length === 0)
318
+ continue;
319
+ if (linksOnly) {
320
+ oneWayEdges += links.length;
321
+ oneWayItems.push({
322
+ title: target.title,
323
+ path: target.path,
324
+ from: links,
325
+ where: 'links',
326
+ });
327
+ }
328
+ else {
329
+ oneWayEdges += all.length;
330
+ oneWayItems.push({
331
+ title: target.title,
332
+ path: target.path,
333
+ from: all,
334
+ where: links.length === 0 ? 'body' : body.length === 0 ? 'links' : 'mixed',
335
+ });
336
+ }
337
+ }
338
+ oneWayItems.sort((a, b) => a.path.localeCompare(b.path));
339
+ const oneWayBucket = {
340
+ total: oneWayItems.length,
341
+ edges: oneWayEdges,
342
+ bodyOnly,
343
+ linksOnly,
344
+ items: oneWayItems.slice(0, MAX_ITEMS),
345
+ };
254
346
  const parseErrors = parseErrorWarnings(cfg, warnings);
255
347
  const staleNotes = notes
256
348
  .map((n) => ({ note: n, at: Date.parse(n.modified) }))
@@ -274,7 +366,7 @@ export async function lintNotebook(cfg, opts = {}, signal) {
274
366
  brokenLinks: bucket(brokenAbs),
275
367
  stale,
276
368
  tagless: bucket(taglessAbs.filter((abs) => !isDaily(abs))),
277
- oneWayLinks: bucket(oneWayAbs),
369
+ oneWayLinks: oneWayBucket,
278
370
  duplicateTitles: duplicateTitles(notes),
279
371
  memoryLinks: memoryLinkIssues(cfg, notes),
280
372
  };
package/dist/lib/zk.js CHANGED
@@ -339,8 +339,9 @@ export async function graphEdges(cfg, signal, opts = {}) {
339
339
  for (const link of graph.links ?? []) {
340
340
  const sourcePath = String(link.sourcePath ?? '');
341
341
  const targetPath = String(link.targetPath ?? '');
342
+ const snippetStart = Number(link.snippetStart);
342
343
  if (sourcePath && targetPath)
343
- out.push({ sourcePath, targetPath });
344
+ out.push({ sourcePath, targetPath, snippetStart: Number.isFinite(snippetStart) ? snippetStart : -1 });
344
345
  }
345
346
  return out;
346
347
  }
@@ -78,15 +78,20 @@ YAML 独有的转义(`\N`、`\_`)不是合法 JSON,退回「去掉引号
78
78
  「陈旧」在 JS 里算(`stale_days` 默认 90)—— zk 的 `stale` 不是内置过滤器,只在用户的 `[filter]` 里定义过才存在。
79
79
  「重名标题」按「压空白 + 小写」归一化后分组。孤儿与无标签**排除 daily 目录**:日记按天写,天然没入链没标签,
80
80
  全报会淹掉真问题;断链仍包含日记。
81
- 「单向链接」(`oneWayLinks`)在 JS 里按 `zk graph` 的边算:存在 S→X、X 没回链 S,且 **S 不是日记**。
81
+ 「单向链接」(`oneWayLinks`)在 JS 里按 `zk graph` 的边算:存在 S→X、X 没回链 S,且 **S、X 都不是日记**。
82
82
  不用 zk 的 `--missing-backlink`:它的原义是「至少有一条单向链接」,日记天然是一串出边,
83
83
  日记提过的每篇笔记都会常年挂在桶里 —— 那不是「没接上」,是正常的引用方向,桶就再也清零不了。
84
+ 普通笔记链到日记("今天做了 X")同理,不要求日记回链。
85
+ `oneWayLinks.items` 是缺回链的目标 X(不是来源),`from` 是没被 X 回链的来源 S;`edges` 是列出的单向边数,
86
+ `where` / `bodyOnly` 标出其中来自正文引用(不在 `## Links` / `## 链接` 小节)的部分 —— 正文引用天然常单向,
87
+ 单独计数,方便判断该不该补回链。`zk_lint(links_only=true)` / `zk-kit lint --links-only` 只列 Links 小节的单向边,
88
+ 正文引用只进 `bodyOnly`;默认 `false`,仍然全报。
84
89
  `parseErrors` 单列一类:frontmatter 写坏(典型是 `tags: [#你好世界]` —— YAML 里 `#` 不是 tag 前缀,要写 `tags: ["你好世界"]`)
85
90
  时 zk 以 exit 0 跳过那篇、只往 stderr 打一行 `error parsing note content`,于是它不在索引里 ——
86
91
  搜不到,也不会出现在上面任何一个桶里(表现成邻居突然孤儿 / 断链)。`zk()` 收 exit 0 的 stderr warning,
87
92
  按 `excludedNames` 同一套口径过滤(归档 / raw / inbox / meta 的坏文件本来就不参与检索,报了只会淹掉真问题),
88
93
  `zk_lint` 与 `zk-kit doctor` 各回一行;`doctor` 用 ⚠️ 标,不影响它的退出码。
89
- 一次最多扫 10000 篇,到顶时两个工具都返回 `truncated: true` 并在文本里告警(不静默截断)。
94
+ 一次最多扫 10000 篇,到顶时两个工具都返回 `truncated: true` 并在文本里告警:结果只会漏报,不会误报。
90
95
  `memoryLinks` 单独查**记忆层**:`MEMORY.md` / `USER.md` 里的 `[[来源]]` 是否还指向活笔记。
91
96
  解析顺序固定:原路径 → 加 `.md` → `archive/` 下同名 → 活笔记标题 / 文件名;旧路径在 `archive/` 下找到同名,
92
97
  或活文件带 `status: superseded`,报 `superseded`;都找不到报 `missing`。**显式写 `archive/` 路径的链接是有意引用**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-zdiary-kit",
3
- "version": "0.6.2",
3
+ "version": "0.7.1",
4
4
  "description": "内置 zk 的 DSH 知识库 / 日记插件:装好即用 —— 自动装 zk、建笔记本,给 agent 一组 zk_* 工具(搜索、读写、日记、双链),设置页里管路径、索引与自检。",
5
5
  "icon": "./icon.svg",
6
6
  "repository": {