dsh-zdiary-kit 0.6.2 → 0.7.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 CHANGED
@@ -1,4 +1,4 @@
1
- # dsh-zdiary-kit 0.6.2
1
+ # dsh-zdiary-kit 0.7.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),
package/dist/cli.js CHANGED
@@ -146,7 +146,15 @@ function lintReport(r) {
146
146
  lines.push(row('断链', r.brokenLinks.total, r.brokenLinks.items));
147
147
  lines.push(row('孤儿', r.orphans.total, r.orphans.items));
148
148
  lines.push(row('无标签', r.tagless.total, r.tagless.items));
149
- lines.push(row('单向链接', r.oneWayLinks.total, r.oneWayLinks.items));
149
+ const oneWay = r.oneWayLinks;
150
+ if (oneWay.total === 0)
151
+ lines.push('✓ 单向链接:0');
152
+ else {
153
+ const items = oneWay.items.slice(0, 5).map((i) => `${i.path} ← ${i.from.join('、')}`);
154
+ lines.push(`✗ 单向链接:${oneWay.total} 篇 / ${oneWay.edges} 条单向边` +
155
+ (oneWay.bodyOnly > 0 ? `(正文引用 ${oneWay.bodyOnly} 条)` : '') +
156
+ ` — ${items.join(';')}${oneWay.total > items.length ? ' …' : ''}`);
157
+ }
150
158
  lines.push(r.stale.total === 0
151
159
  ? '✓ 陈旧:0'
152
160
  : `✗ 陈旧:${r.stale.total} 篇 — ` + r.stale.items.slice(0, 5).map((s) => `${s.path}(${s.modified})`).join('、'));
@@ -157,7 +165,7 @@ function lintReport(r) {
157
165
  if (memLine)
158
166
  lines.push(memLine);
159
167
  if (r.truncated)
160
- lines.push('⚠️ 笔记数触到扫描上限:结果不完整');
168
+ lines.push('⚠️ 笔记数触到扫描上限:结果不完整(只会漏报,不会误报)');
161
169
  lines.push('(只报告,没有改动任何文件)');
162
170
  return lines.join('\n');
163
171
  }
@@ -29,6 +29,18 @@ 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
+ items: { type: 'array', items: LINT_ONE_WAY_ITEM },
43
+ }, ['total', 'edges', 'bodyOnly', 'items']);
32
44
  const LINT_STALE = objSchema({
33
45
  total: { type: 'number' },
34
46
  items: {
@@ -794,7 +806,10 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
794
806
  'memoryLinks 查 MEMORY.md / USER.md 里的 [[来源链接]] 是否还指向活笔记(已废弃 / 不存在)—— ' +
795
807
  'supersede 不会重写 meta 文件里的链接,这类漂移 broken-links 看不见。' +
796
808
  '孤儿与无标签不含日记(按天写的日记天然如此,报出来只会淹没真问题)。' +
797
- 'oneWayLinks = S 链 X 但 X 没回链 S,且 S 不是日记 —— 日记天然是出边,回链它没有意义。' +
809
+ 'oneWayLinks.items 是缺回链的目标 X(不是来源),from 是没被 X 回链的来源 S;' +
810
+ 'edges 是单向边数,bodyOnly 是其中来自正文引用(不在 ## Links / ## 链接 小节)的条数。' +
811
+ 'S 或 X 是日记的边不算 —— 日记天然是出边,回链它没有意义。' +
812
+ '触到扫描上限时结果只会漏报,不会误报。' +
798
813
  '只读:不改任何笔记。拿到清单后自己判断先修哪个,再用 zk_link / zk_append / zk_supersede 动手。',
799
814
  parameters: {
800
815
  type: 'object',
@@ -814,7 +829,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
814
829
  brokenLinks: LINT_BUCKET,
815
830
  stale: LINT_STALE,
816
831
  tagless: LINT_BUCKET,
817
- oneWayLinks: LINT_BUCKET,
832
+ oneWayLinks: LINT_ONE_WAY,
818
833
  duplicateTitles: LINT_DUPLICATE,
819
834
  memoryLinks: LINT_MEMORY,
820
835
  }, ['notebook', 'notes', 'truncated', 'staleDays', 'parseErrors', 'orphans', 'brokenLinks', 'stale', 'tagless', 'oneWayLinks', 'duplicateTitles', 'memoryLinks']),
@@ -843,7 +858,15 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
843
858
  lines.push(line('断链', v.brokenLinks));
844
859
  lines.push(line('孤儿', v.orphans));
845
860
  lines.push(line('无标签', v.tagless));
846
- lines.push(line('单向链接', v.oneWayLinks));
861
+ const oneWay = v.oneWayLinks;
862
+ if (oneWay.total === 0)
863
+ lines.push('✓ 单向链接:0');
864
+ else {
865
+ const items = oneWay.items.slice(0, 5).map((i) => i.path + ' ← ' + i.from.join('、'));
866
+ lines.push('✗ 单向链接:' + oneWay.total + ' 篇 / ' + oneWay.edges + ' 条单向边' +
867
+ (oneWay.bodyOnly > 0 ? '(正文引用 ' + oneWay.bodyOnly + ' 条)' : '') +
868
+ ' — ' + items.join(';') + (oneWay.total > items.length ? ' …' : ''));
869
+ }
847
870
  lines.push(v.stale.total === 0
848
871
  ? '✓ 陈旧:0'
849
872
  : '✗ 陈旧:' + v.stale.total + ' 篇 — ' +
@@ -857,7 +880,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
857
880
  if (memLine)
858
881
  lines.push(memLine);
859
882
  if (v.truncated)
860
- lines.push('⚠️ 笔记数触到扫描上限:结果不完整,先归档旧笔记再跑一次');
883
+ lines.push('⚠️ 笔记数触到扫描上限:结果不完整(只会漏报,不会误报),先归档旧笔记再跑一次');
861
884
  lines.push('(只报告,没有改动任何文件)');
862
885
  return text(lines.join('\n'));
863
886
  },
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,77 @@ 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 oneWayItems = [];
294
+ let oneWayEdges = 0;
295
+ let bodyOnly = 0;
296
+ for (const row of oneWay) {
297
+ // 目标也不报日记:普通笔记链到日记("今天做了 X")同样是正常引用方向,
298
+ // 要求日记回链每篇被提到的笔记,跟要求笔记回链日记是同一个问题。
299
+ if (isDaily(path.resolve(cfg.notebook, row.target)))
300
+ continue;
301
+ const target = byPath.get(row.target);
302
+ if (!target)
303
+ continue;
304
+ let links = 0;
305
+ let body = 0;
306
+ for (const source of row.from) {
307
+ oneWayEdges += 1;
308
+ if (whereOf(source.path, source.snippetStarts) === 'links')
309
+ links += 1;
310
+ else {
311
+ body += 1;
312
+ bodyOnly += 1;
313
+ }
314
+ }
315
+ oneWayItems.push({
316
+ title: target.title,
317
+ path: target.path,
318
+ from: row.from.map((s) => s.path),
319
+ where: links === 0 ? 'body' : body === 0 ? 'links' : 'mixed',
320
+ });
321
+ }
322
+ oneWayItems.sort((a, b) => a.path.localeCompare(b.path));
323
+ const oneWayBucket = {
324
+ total: oneWayItems.length,
325
+ edges: oneWayEdges,
326
+ bodyOnly,
327
+ items: oneWayItems.slice(0, MAX_ITEMS),
328
+ };
254
329
  const parseErrors = parseErrorWarnings(cfg, warnings);
255
330
  const staleNotes = notes
256
331
  .map((n) => ({ note: n, at: Date.parse(n.modified) }))
@@ -274,7 +349,7 @@ export async function lintNotebook(cfg, opts = {}, signal) {
274
349
  brokenLinks: bucket(brokenAbs),
275
350
  stale,
276
351
  tagless: bucket(taglessAbs.filter((abs) => !isDaily(abs))),
277
- oneWayLinks: bucket(oneWayAbs),
352
+ oneWayLinks: oneWayBucket,
278
353
  duplicateTitles: duplicateTitles(notes),
279
354
  memoryLinks: memoryLinkIssues(cfg, notes),
280
355
  };
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,19 @@ 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
+ 单独计数,方便判断该不该补回链。
84
88
  `parseErrors` 单列一类:frontmatter 写坏(典型是 `tags: [#你好世界]` —— YAML 里 `#` 不是 tag 前缀,要写 `tags: ["你好世界"]`)
85
89
  时 zk 以 exit 0 跳过那篇、只往 stderr 打一行 `error parsing note content`,于是它不在索引里 ——
86
90
  搜不到,也不会出现在上面任何一个桶里(表现成邻居突然孤儿 / 断链)。`zk()` 收 exit 0 的 stderr warning,
87
91
  按 `excludedNames` 同一套口径过滤(归档 / raw / inbox / meta 的坏文件本来就不参与检索,报了只会淹掉真问题),
88
92
  `zk_lint` 与 `zk-kit doctor` 各回一行;`doctor` 用 ⚠️ 标,不影响它的退出码。
89
- 一次最多扫 10000 篇,到顶时两个工具都返回 `truncated: true` 并在文本里告警(不静默截断)。
93
+ 一次最多扫 10000 篇,到顶时两个工具都返回 `truncated: true` 并在文本里告警:结果只会漏报,不会误报。
90
94
  `memoryLinks` 单独查**记忆层**:`MEMORY.md` / `USER.md` 里的 `[[来源]]` 是否还指向活笔记。
91
95
  解析顺序固定:原路径 → 加 `.md` → `archive/` 下同名 → 活笔记标题 / 文件名;旧路径在 `archive/` 下找到同名,
92
96
  或活文件带 `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.0",
4
4
  "description": "内置 zk 的 DSH 知识库 / 日记插件:装好即用 —— 自动装 zk、建笔记本,给 agent 一组 zk_* 工具(搜索、读写、日记、双链),设置页里管路径、索引与自检。",
5
5
  "icon": "./icon.svg",
6
6
  "repository": {