dsh-zdiary-kit 3.7.0 → 3.9.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 +34 -12
- package/dist/cli.js +53 -4
- package/dist/features/zk-tools/feature.js +237 -16
- package/dist/lib/agents-template.js +6 -5
- package/dist/lib/archive-probe.js +159 -0
- package/dist/lib/dream.js +16 -10
- package/dist/lib/edge-context.js +95 -0
- package/dist/lib/entry-points.js +204 -0
- package/dist/lib/lint.js +5 -0
- package/dist/lib/recall.js +30 -1
- package/dist/lib/usage.js +60 -0
- package/dist/lib/wikilink.js +18 -1
- package/dist/lib/zk.js +174 -7
- package/docs/features/memory.md +25 -4
- package/docs/features/zk-tools.md +76 -3
- package/package.json +1 -1
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';
|
|
@@ -16,7 +17,8 @@ import { withFileLockSync } from './lock.js';
|
|
|
16
17
|
import { parseFrontmatter, setFrontmatter } from './frontmatter.js';
|
|
17
18
|
import { isMetaPath, META_NAMES } from './meta.js';
|
|
18
19
|
import { similarCandidates } from './similar.js';
|
|
19
|
-
import {
|
|
20
|
+
import { linkContextOf } from './edge-context.js';
|
|
21
|
+
import { normalizeTitle, noteKey } from './wikilink.js';
|
|
20
22
|
export class ZkError extends Error {
|
|
21
23
|
hint;
|
|
22
24
|
/**
|
|
@@ -759,6 +761,7 @@ function toRecord(raw, cfg, terms = []) {
|
|
|
759
761
|
snippet: buildSnippet(raw, terms),
|
|
760
762
|
words: Number(raw.wordCount ?? 0),
|
|
761
763
|
metadata,
|
|
764
|
+
id: meta('id'),
|
|
762
765
|
status: meta('status'),
|
|
763
766
|
type: meta('type'),
|
|
764
767
|
authority: meta('authority'),
|
|
@@ -859,6 +862,7 @@ const LEAN_FIELDS = [
|
|
|
859
862
|
'{{metadata.authority}}',
|
|
860
863
|
'{{metadata.date}}',
|
|
861
864
|
'{{metadata.supersede-reason}}',
|
|
865
|
+
'{{metadata.id}}',
|
|
862
866
|
];
|
|
863
867
|
const LEAN_TEMPLATE = [...LEAN_FIELDS, '{{lead}}'].join(LEAN_FIELD);
|
|
864
868
|
/**
|
|
@@ -882,8 +886,8 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
882
886
|
return { notes: [], malformed: false };
|
|
883
887
|
const notes = [];
|
|
884
888
|
let malformed = false;
|
|
885
|
-
//
|
|
886
|
-
const leadAt = withSnippet ?
|
|
889
|
+
// 12 个定长字段(含 id)+(可选)snippet + lead;lead 放最后,多余的段拼回去
|
|
890
|
+
const leadAt = withSnippet ? 13 : 12;
|
|
887
891
|
for (const chunk of text.split(LEAN_RECORD)) {
|
|
888
892
|
const record = chunk.replace(/^[\r\n]+|[\r\n]+$/g, '');
|
|
889
893
|
if (!record.trim())
|
|
@@ -893,8 +897,8 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
893
897
|
malformed = true;
|
|
894
898
|
continue;
|
|
895
899
|
}
|
|
896
|
-
const [absPath, title, words, created, modified, tags, status, type, authority, date, reason] = parts;
|
|
897
|
-
const snippet = withSnippet ? clipSnippet(parts[
|
|
900
|
+
const [absPath, title, words, created, modified, tags, status, type, authority, date, reason, id] = parts;
|
|
901
|
+
const snippet = withSnippet ? clipSnippet(parts[12]) : '';
|
|
898
902
|
const lead = parts.slice(leadAt).join(LEAN_FIELD).replace(/\s+/g, ' ').trim().slice(0, 200);
|
|
899
903
|
const abs = absPath ?? '';
|
|
900
904
|
if (!abs) {
|
|
@@ -913,6 +917,8 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
913
917
|
metadata.date = date;
|
|
914
918
|
if (reason)
|
|
915
919
|
metadata['supersede-reason'] = reason;
|
|
920
|
+
if (id)
|
|
921
|
+
metadata.id = id;
|
|
916
922
|
notes.push({
|
|
917
923
|
title: String(title ?? ''),
|
|
918
924
|
path: rel,
|
|
@@ -925,6 +931,7 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
925
931
|
snippet,
|
|
926
932
|
words: Number(words ?? 0),
|
|
927
933
|
metadata,
|
|
934
|
+
id: String(id ?? ''),
|
|
928
935
|
status: String(status ?? ''),
|
|
929
936
|
type: String(type ?? ''),
|
|
930
937
|
authority: String(authority ?? ''),
|
|
@@ -1277,9 +1284,18 @@ async function linkedNotes(cfg, flag, rel, opts = {}, signal) {
|
|
|
1277
1284
|
const args = ['list', '--quiet', '--no-pager', '--format', 'json', '--limit', String(limit + 1), flag, abs];
|
|
1278
1285
|
for (const name of excludedNames(cfg, { includeArchived: opts.includeArchived }))
|
|
1279
1286
|
args.push('-x', name);
|
|
1280
|
-
const
|
|
1287
|
+
const raws = parseList(await zk(cfg, args, { signal }));
|
|
1288
|
+
const notes = raws.map((raw) => toRecord(raw, cfg));
|
|
1281
1289
|
const truncated = notes.length > limit;
|
|
1282
|
-
|
|
1290
|
+
const result = { notes: notes.slice(0, limit), truncated, completeness: truncated ? 'partial' : 'complete' };
|
|
1291
|
+
if (opts.keepContent) {
|
|
1292
|
+
const contents = new Map();
|
|
1293
|
+
for (let i = 0; i < notes.length; i += 1) {
|
|
1294
|
+
contents.set(notes[i].path, String(raws[i]?.rawContent ?? raws[i]?.body ?? ''));
|
|
1295
|
+
}
|
|
1296
|
+
result.contents = contents;
|
|
1297
|
+
}
|
|
1298
|
+
return result;
|
|
1283
1299
|
}
|
|
1284
1300
|
/** 谁链到这篇(入链 / 反链)。路径是笔记本相对的;目标不存在时返回空,不返回全库 */
|
|
1285
1301
|
export async function backlinksOf(cfg, rel, opts = {}, signal) {
|
|
@@ -1291,6 +1307,138 @@ export async function outlinksOf(cfg, rel, opts = {}, signal) {
|
|
|
1291
1307
|
const found = await linkedNotes(cfg, '--linked-by', rel, opts, signal);
|
|
1292
1308
|
return { ...found, notes: found.notes.filter((n) => n.path !== rel) };
|
|
1293
1309
|
}
|
|
1310
|
+
/** `[[目标|别名]]` / `[[目标#小节]]` / `./notes/x.md` → `notes/x` 形态的裸目标 */
|
|
1311
|
+
function walkSeedTarget(raw) {
|
|
1312
|
+
let target = String(raw ?? '').trim();
|
|
1313
|
+
const wrapped = /^\[\[([^\]]*)\]\]$/.exec(target);
|
|
1314
|
+
if (wrapped)
|
|
1315
|
+
target = String(wrapped[1] ?? '');
|
|
1316
|
+
return target.split('|')[0].split('#')[0].trim().replace(/\\/g, '/').replace(/^\.\//, '');
|
|
1317
|
+
}
|
|
1318
|
+
/** 看起来是路径(含目录分隔符或 .md 后缀)—— 路径种子不查索引,直接看磁盘 */
|
|
1319
|
+
function looksLikePath(target) {
|
|
1320
|
+
return target.includes('/') || /\.(md|markdown)$/i.test(target);
|
|
1321
|
+
}
|
|
1322
|
+
function seedAt(cfg, rel, opts = {}) {
|
|
1323
|
+
const root = path.resolve(cfg.notebook);
|
|
1324
|
+
const abs = path.resolve(cfg.notebook, rel);
|
|
1325
|
+
if (abs !== root && !abs.startsWith(root + path.sep))
|
|
1326
|
+
return null;
|
|
1327
|
+
// 种子也是「进入网络的入口」:meta / raw / inbox 不是可导航的笔记,归档要先显式允许。
|
|
1328
|
+
// 与邻居侧的 `excludedNames` 同口径 —— 不能只有邻居过滤、种子随便进(plan §14.5)。
|
|
1329
|
+
const norm = rel.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
1330
|
+
if (isMetaPath(norm))
|
|
1331
|
+
return null;
|
|
1332
|
+
if (norm === 'raw' || norm.startsWith('raw/') || norm === 'inbox' || norm.startsWith('inbox/'))
|
|
1333
|
+
return null;
|
|
1334
|
+
if (!opts.includeArchived && inArchive(cfg, norm))
|
|
1335
|
+
return null;
|
|
1336
|
+
try {
|
|
1337
|
+
if (!fs.statSync(abs).isFile())
|
|
1338
|
+
return null;
|
|
1339
|
+
}
|
|
1340
|
+
catch {
|
|
1341
|
+
return null;
|
|
1342
|
+
}
|
|
1343
|
+
let title = '';
|
|
1344
|
+
try {
|
|
1345
|
+
title = String(parseFrontmatter(fs.readFileSync(abs, 'utf8')).fields.title ?? '');
|
|
1346
|
+
}
|
|
1347
|
+
catch { /* 标题只作回显 */ }
|
|
1348
|
+
return { title: title || path.basename(rel, path.extname(rel)), path: rel, absPath: abs };
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* 种子解析:路径 / `[[路径]]` 只看磁盘(0 个子进程);标题 / `[[id]]` 走一次 `zk list -M exact`(1 个子进程)。
|
|
1352
|
+
*
|
|
1353
|
+
* 为什么不用「全库扫描 + resolveWikiTarget」:10k 上那是一整趟 `zk list`(约 2.6 s / 2 MB),
|
|
1354
|
+
* 会把单种子 1 跳从 2 个进程抬到 3 个,与 R8 的成本口径(§11.3)不符。
|
|
1355
|
+
* `-M exact` 实测能命中 frontmatter 的 id 与标题,但它是「逐词精确」而不是「整串相等」
|
|
1356
|
+
* (`记忆预算` 会同时命中 `记忆预算的另一篇`),所以拿到候选后还要在 JS 里比对一次
|
|
1357
|
+
* id / 标题 / 文件名,对不上就当没找到 —— 不拿第一个命中冒充种子。
|
|
1358
|
+
*/
|
|
1359
|
+
async function resolveWalkSeed(cfg, raw, opts = {}, signal) {
|
|
1360
|
+
const target = walkSeedTarget(raw);
|
|
1361
|
+
if (!target)
|
|
1362
|
+
return null;
|
|
1363
|
+
if (looksLikePath(target)) {
|
|
1364
|
+
const candidates = /\.(md|markdown)$/i.test(target) ? [target] : [target, `${target}.md`];
|
|
1365
|
+
for (const candidate of candidates) {
|
|
1366
|
+
const hit = seedAt(cfg, candidate, opts);
|
|
1367
|
+
if (hit)
|
|
1368
|
+
return hit;
|
|
1369
|
+
}
|
|
1370
|
+
return null;
|
|
1371
|
+
}
|
|
1372
|
+
const args = [
|
|
1373
|
+
'list', '--quiet', '--no-pager', '--format', LEAN_TEMPLATE, '-d', LEAN_RECORD,
|
|
1374
|
+
'--limit', '5', '-M', 'exact', '-m', target,
|
|
1375
|
+
];
|
|
1376
|
+
// 与邻居的排除口径一致:include_archived 时种子也可以是一篇归档笔记
|
|
1377
|
+
for (const name of excludedNames(cfg, { includeArchived: opts.includeArchived }))
|
|
1378
|
+
args.push('-x', name);
|
|
1379
|
+
const notes = parseLeanList(await zk(cfg, args, { signal }), cfg).notes;
|
|
1380
|
+
const wanted = normalizeTitle(target);
|
|
1381
|
+
const hit = notes.find((n) => n.id && n.id === target)
|
|
1382
|
+
?? notes.find((n) => normalizeTitle(n.title) === wanted)
|
|
1383
|
+
?? notes.find((n) => normalizeTitle(path.basename(n.path, path.extname(n.path))) === wanted);
|
|
1384
|
+
return hit ? { title: hit.title || path.basename(hit.path, path.extname(hit.path)), path: hit.path, absPath: hit.absPath } : null;
|
|
1385
|
+
}
|
|
1386
|
+
/**
|
|
1387
|
+
* R8:从一篇种子出发走一跳(按篇反查,不建全图)。
|
|
1388
|
+
*
|
|
1389
|
+
* 成本:路径种子 = `both` 2 个子进程(backlinksOf + outlinksOf,10k 实测见 docs/bench-baseline.md);
|
|
1390
|
+
* 标题 / id 种子再加一次 `-M exact` 解析(共 3 个)。`limit` 硬钳 `MAX_LINK_NOTES` = 200,
|
|
1391
|
+
* 不用 `MAX_SCAN` —— 后者配合 `--format json` 会吐 17 MB,等于把全图成本放回来。
|
|
1392
|
+
* 默认排除 archive / raw / meta(与 `excludedNames` 同口径):邻居与**种子**两侧都过滤,
|
|
1393
|
+
* 不能只过滤一侧(plan §14.5)。
|
|
1394
|
+
*
|
|
1395
|
+
* 每条邻居多带 `section` / `linkLine`(那条边所在的小节与那一行):
|
|
1396
|
+
* 入链用邻居 JSON 里已经在传输的 `rawContent`,出链读一次种子文件 —— 两边都是 **0 个额外子进程**。
|
|
1397
|
+
* 没有这两个字段,`lead` 对同一篇种子的所有出链是同一段文字,等于只给了 8 个标题(plan §14.1)。
|
|
1398
|
+
*/
|
|
1399
|
+
export async function walkFrom(cfg, opts, signal) {
|
|
1400
|
+
const direction = opts.direction ?? 'both';
|
|
1401
|
+
const limit = Math.min(MAX_LINK_NOTES, Math.max(1, Math.trunc(opts.limit ?? 10)));
|
|
1402
|
+
const seed = await resolveWalkSeed(cfg, opts.seed, { includeArchived: opts.includeArchived }, signal);
|
|
1403
|
+
if (!seed)
|
|
1404
|
+
return { seed: null, notes: [], truncated: false, completeness: 'complete' };
|
|
1405
|
+
const directions = direction === 'both' ? ['in', 'out'] : [direction];
|
|
1406
|
+
const notes = [];
|
|
1407
|
+
const seen = new Set();
|
|
1408
|
+
let truncated = false;
|
|
1409
|
+
// 出链的「那一行」写在种子里;入链的写在邻居里(JSON 里已经带了 rawContent)
|
|
1410
|
+
let seedContent = '';
|
|
1411
|
+
if (direction !== 'in') {
|
|
1412
|
+
try {
|
|
1413
|
+
seedContent = fs.readFileSync(seed.absPath, 'utf8');
|
|
1414
|
+
}
|
|
1415
|
+
catch {
|
|
1416
|
+
seedContent = '';
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
for (const relation of directions) {
|
|
1420
|
+
const found = relation === 'in'
|
|
1421
|
+
? await backlinksOf(cfg, seed.path, { limit, includeArchived: opts.includeArchived, keepContent: true }, signal)
|
|
1422
|
+
: await outlinksOf(cfg, seed.path, { limit, includeArchived: opts.includeArchived }, signal);
|
|
1423
|
+
if (found.truncated)
|
|
1424
|
+
truncated = true;
|
|
1425
|
+
for (const note of found.notes) {
|
|
1426
|
+
// 按身份键去重:互链的笔记两个方向都会出现,只报一次(先 in 后 out)
|
|
1427
|
+
const key = noteKey(note);
|
|
1428
|
+
if (seen.has(key))
|
|
1429
|
+
continue;
|
|
1430
|
+
seen.add(key);
|
|
1431
|
+
const ctx = relation === 'out'
|
|
1432
|
+
? linkContextOf(seedContent, note, seed.path)
|
|
1433
|
+
: linkContextOf(found.contents?.get(note.path) ?? '', seed, note.path);
|
|
1434
|
+
notes.push({
|
|
1435
|
+
title: note.title, path: note.path, absPath: note.absPath, relation, lead: note.lead,
|
|
1436
|
+
...(ctx ? { section: ctx.section, linkLine: ctx.line } : {}),
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
return { seed, notes, truncated, completeness: truncated ? 'partial' : 'complete' };
|
|
1441
|
+
}
|
|
1294
1442
|
/**
|
|
1295
1443
|
* `zk-kit query` 会被**丢掉**的参数:它们要么绕开「统一排除」,要么弄坏 JSON 解析。
|
|
1296
1444
|
* 长名与短名都列出来(`--exclude` / `-x` 是同一个东西的两个写法)。
|
|
@@ -1430,6 +1578,7 @@ function noteFromDisk(cfg, abs) {
|
|
|
1430
1578
|
snippet: '',
|
|
1431
1579
|
words: content.split(/\s+/).filter(Boolean).length,
|
|
1432
1580
|
metadata: { ...fields },
|
|
1581
|
+
id: fields.id ?? '',
|
|
1433
1582
|
status: fields.status ?? '',
|
|
1434
1583
|
type: fields.type ?? '',
|
|
1435
1584
|
authority: fields.authority ?? '',
|
|
@@ -1495,6 +1644,13 @@ function normalizeNoteName(raw) {
|
|
|
1495
1644
|
}
|
|
1496
1645
|
return name;
|
|
1497
1646
|
}
|
|
1647
|
+
/**
|
|
1648
|
+
* 稳定 ID(§9.2):10 位十六进制短随机串(40 bit,10⁴ 篇库的碰撞概率可忽略)。
|
|
1649
|
+
* 纯小写 hex,不会与路径 / `.md` 后缀 / 中文标题撞形,`[[id]]` 才认得出来。
|
|
1650
|
+
*/
|
|
1651
|
+
export function newNoteId() {
|
|
1652
|
+
return randomBytes(5).toString('hex');
|
|
1653
|
+
}
|
|
1498
1654
|
/**
|
|
1499
1655
|
* 用 zk new 建一篇(正文走 stdin → 模板里的 {{content}})。
|
|
1500
1656
|
*
|
|
@@ -1635,6 +1791,17 @@ export async function createNote(cfg, input, signal, scan = (c, s) => allNotes(c
|
|
|
1635
1791
|
summaryWarning = `summary 没写进去:${err instanceof Error ? err.message : String(err)}(文件已建好,可以直接编辑那一节)`;
|
|
1636
1792
|
}
|
|
1637
1793
|
}
|
|
1794
|
+
// 稳定 ID(§9.2):只给 zk_new 新建的笔记写 id,老笔记 / 日记不回填。
|
|
1795
|
+
// 建完才写:zk new -i 索引的是模板产物,改了文件就得标脏,让下一次读补索引(R6)。
|
|
1796
|
+
// 收尾失败一律吞掉(同 markIndexDirty):笔记已经建好,不能让一次 id 写入看起来像整次创建失败。
|
|
1797
|
+
try {
|
|
1798
|
+
const text = fs.readFileSync(finalAbs, 'utf8');
|
|
1799
|
+
if (!parseFrontmatter(text).fields.id) {
|
|
1800
|
+
fs.writeFileSync(finalAbs, setFrontmatter(text, { id: newNoteId() }), 'utf8');
|
|
1801
|
+
markIndexDirty(cfg);
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
catch { /* 收尾失败:文件已落盘,不回滚、不误导 */ }
|
|
1638
1805
|
return { path: relPathOf(cfg, finalAbs), absPath: finalAbs, title, archived, similar, summaryApplied, summaryWarning };
|
|
1639
1806
|
}
|
|
1640
1807
|
/** 笔记本相对路径是否在归档目录下(supersede 会把文件搬进去,路径保持不变) */
|
package/docs/features/memory.md
CHANGED
|
@@ -12,12 +12,14 @@
|
|
|
12
12
|
- `lib/memory-format.ts`:条目的唯一切分口径 —— `splitMemoryEntries` / `isRetiredEntry` / `entryDate` /
|
|
13
13
|
`stripRetiredEntries` / `daysSinceDate` / `memoryFormatDebt`(日期与来源缺口的机械计数);
|
|
14
14
|
memory、dream、lint 共用,禁止各写一套
|
|
15
|
+
- `lib/entry-points.ts`:MEMORY 当检索入口(R9)—— `liveMemoryEntries` / `entryPointsOf`(`via: query` 的来源可以不在结果集里),
|
|
16
|
+
把「命中笔记 ← 已在 MEMORY 的哪条结论」接出来;索引只用本次结果笔记建、路径式来源直接读盘,零子进程(见下)
|
|
15
17
|
- `lib/dream.ts`:`lastDreamDate` / `daysSinceDream`(只认整齐的一行,按本地日历天算);`dreamNotebook` 用它算默认窗口
|
|
16
18
|
- `lib/wikilink.ts`:`[[链接]]` 的解析口径(路径 → `.md` → `archive/` → 活笔记标题),lint 与 dream 共用一份
|
|
17
19
|
- `lib/persona-template.ts`:`PERSONA_TEMPLATE` 与独立版本标记(`<!-- zdiary-persona: vN -->`,
|
|
18
20
|
不复用 `zdiary-template` —— AGENTS 改版不该让人格文件误报旧版);只走 opt-in 写入,已存在不覆盖
|
|
19
|
-
- `lib/agents-template.ts`:`AGENTS.md` 模板两份 —— layered 用完整版(当前
|
|
20
|
-
(
|
|
21
|
+
- `lib/agents-template.ts`:`AGENTS.md` 模板两份 —— layered 用完整版(当前 190 词),flat 用精简版
|
|
22
|
+
(88 词,不出现 `raw` / `inbox` / `archive` / `WORKFLOW.md`);两份首行都是 `<!-- zdiary-template: vN -->`。
|
|
21
23
|
layered 初始化自动写,flat 只有 `ensureNotebook(cfg, signal, { withAgents: true })` / `zk-kit init --with-agents` /
|
|
22
24
|
设置页「写入 AGENTS.md 模板」才写;已存在一律不覆盖。
|
|
23
25
|
`WORKFLOW.md` 模板(改已有笔记的清单确认 + 三分类;不注入、不计预算,AGENTS 只留一行指针)
|
|
@@ -58,7 +60,8 @@
|
|
|
58
60
|
需要「别自动发布 / 别花钱」这类边界时,写进 `AGENTS.md`(工作约定)或 `PERSONA.md`(角色边界)。
|
|
59
61
|
|
|
60
62
|
**记忆层与事实层的一致性。** `MEMORY.md` 每条结论后附 `[[来源路径]]`(写进 AGENTS 模板);
|
|
61
|
-
`zk_lint` 的 `memoryLinks`
|
|
63
|
+
`zk_lint` 的 `memoryLinks` 只做机械核对:先按稳定 id 查(`[[<id>]]` → 该笔记当前路径,§10.4 第 7 项),
|
|
64
|
+
未命中再走原路径 → 加 `.md` → `archive/` 下同名 → 活笔记标题 / 文件名,
|
|
62
65
|
旧路径在 `archive/` 下找到同名(或活文件带 `status: superseded`)报 `superseded`,都找不到报 `missing`;
|
|
63
66
|
显式写 `archive/` 的链接是有意引用,不算漂移;另数 MEMORY.md 里有多少活条目没附链接(USER 不计;
|
|
64
67
|
`[[ ]]` 这种空目标不算来源)。`unlinked` / `undated` 与注入提示、记忆页共用 `memoryFormatDebt()`,
|
|
@@ -66,8 +69,25 @@
|
|
|
66
69
|
P1-3 再加三项,都只报告:`undated`(条目首行没有 `YYYY-MM-DD`,退役行不计)、`retired`(`- [superseded …]` 条目数)、
|
|
67
70
|
`retiredBroken`(退役行的 `→ [[新结论]]` 解析不到活笔记)。0.9 起注入会省略退役条目(notice 说明省略了几条),
|
|
68
71
|
但文件不删、不改 —— 「注入 = 文件原文」收窄为「注入 = 原文去掉退役条目」(D3)。
|
|
72
|
+
|
|
73
|
+
**MEMORY 不只当注入载荷(R9,§10.4 第 9 项 / §14.2)。** 宿主支持注入时 MEMORY 正文每轮都在系统提示里,
|
|
74
|
+
缺的不是「可见」,是「可导航」。
|
|
75
|
+
`zk_search` / `zk_query` / `zk_recall` 因此多一个可选 `entryPoints`,两种来源分开标 `via`:
|
|
76
|
+
- `via: 'result'`:条目指向**本次命中**的笔记 → 那条结论首行 + 路径(3.9.0 原语义,是注解);
|
|
77
|
+
- `via: 'query'`:**条目本身命中检索词** → 来源不必在结果集里。这是索引该有的方向:关键词 → 卡号,
|
|
78
|
+
而那张卡可以不含这个词。路径式 `[[notes/x]]` 直接读盘落地(`resolved: true`,0 子进程);
|
|
79
|
+
标题 / id 式且不在本批结果里时 `resolved: false`,只给入口名,渲染成「用 `zk_walk` 走进去」——
|
|
80
|
+
不为补一次路径再起一个 zk 进程。
|
|
81
|
+
口径集中在 `lib/entry-points.ts`:条目切分 / 退役判断复用 `splitMemoryEntries` / `isRetiredEntry`,
|
|
82
|
+
`[[链接]]` 解析复用 `WIKI_LINK` / `resolveWikiTarget`(与 lint / dream 同一份);退役行不计、显式 `archive/` 链接
|
|
83
|
+
不算活入口(旧结论归死胡同探针)。索引只用本次结果笔记建,所以**不扫库**;没有 MEMORY.md 或解析失败时整个键不出现,
|
|
84
|
+
老库输出与改动前一致。查询比对是机械的(空格分词 + CJK 二元组),最多 5 条,只做字符串比较 ——
|
|
85
|
+
「哪条更相关」仍是 agent 的活。
|
|
69
86
|
为什么要有:`zk_supersede` 只重写 `zk graph` 里的入链,meta 文件被 `excludedNames` 排除在图外 ——
|
|
70
87
|
链接不会跟着搬,`--broken-links` 也看不见。语义重复仍留给 agent,插件不碰。
|
|
88
|
+
稳定 ID 让来源链接可以选择不绑路径:写成 `[[<id>]]`(从笔记 frontmatter 读)时,改名后仍落到同一篇;
|
|
89
|
+
老笔记没有 id、链接也没写 id 时解析顺序与以前逐字一致。插件不把已有的 `[[来源路径]]` 自动改写成 id ——
|
|
90
|
+
回填属于写路径,单独一轮(§9.2)。
|
|
71
91
|
|
|
72
92
|
**Depends on.** `config/store`(`memoryBudgetWords` / `personaBudgetWords`,每次现读 —— 设置页保存后下一次工具调用就生效)、
|
|
73
93
|
`lib/meta`、`lib/persona-template`、`node:fs`;宿主的 `systemPrompt` 服务(可选,没有就只留 `zk_context`)。
|
|
@@ -93,7 +113,8 @@ P1-3 的日期 / 退役格式 + P2-15 的版本标记后为 189 词;P2-8 去
|
|
|
93
113
|
v4 去掉「宿主不会自动加载本文件」一句后为 175 词(单测锁 `≤190`;flat 精简版锁 `≤90`,v4 起实测 76);
|
|
94
114
|
v5 在 MEMORY 条目约定后补「先 zk_read 核对来源」后为 180 词(flat 80);v7 精简模板后仍为 180 词(flat 80);
|
|
95
115
|
v8 把 `zk_read` 改成原生 `read` 后仍为 180 词(flat 80);
|
|
96
|
-
v9 两份模板各补一句「改 MEMORY 前看 zk_context 余量」、WORKFLOW.md 补做法后为 188 词(flat 86
|
|
116
|
+
v9 两份模板各补一句「改 MEMORY 前看 zk_context 余量」、WORKFLOW.md 补做法后为 188 词(flat 86);
|
|
117
|
+
v10 两份模板把 `zk_walk 一跳` 并进查重那一步后为 190 词(flat 88,各 +2 词,不提高预算)。
|
|
97
118
|
`AGENTS.md` 是硬规则,单独超预算也不截断,只回显告警;文件内容里的 `</system-reminder>` 会被转义,
|
|
98
119
|
截断目录里的段落标题同样转义 —— 不许提前闭合插件自己的框;任何一份文件读不到都当作不存在,笔记本没建好时返回空载荷而不是报错。
|
|
99
120
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**Purpose.** 让模型用 zk 思维工作:先查重、再写原子笔记、用双链连起来、每天记一段。
|
|
4
4
|
|
|
5
|
-
**Owns.**
|
|
5
|
+
**Owns.** 15 个内置工具定义(`zk_search` / `zk_query` / `zk_recall` / `zk_walk` / `zk_new` / `zk_append` / `zk_daily` / `zk_link` /
|
|
6
6
|
`zk_supersede` / `zk_rename` / `zk_context` / `zk_lint` / `zk_dream` / `zk_stats` / `zk_use`)以及笔记本
|
|
7
7
|
`.zk/config.toml` 的 `[alias]` 派生的 `zk_<别名>` 工具:
|
|
8
8
|
参数 schema、输出 schema、render、每个工具自己的 `ensureNotebook` 与参数校验。
|
|
@@ -22,8 +22,21 @@
|
|
|
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
|
+
内置工具固定 15 个。别名是 `$SHELL -c` 的任意命令,注册成工具等于把它交到模型手里,所以要人显式打开
|
|
27
40
|
(设置页「工具」组的复选框,或 `zk-kit config set aliasTools=true`,只认 `true`;拼错的值归一化成 false 并在 `config set` 时回显)。
|
|
28
41
|
打开后,插件加载时读所有已配置笔记本的 `.zk/config.toml`,把 `[alias]` 表里未注释的键注册成工具;
|
|
29
42
|
同名只取第一本。工具名用下划线而不是点号:DSH 的 function-name 契约是 `[A-Za-z0-9_-]`(`zk.lucky` 会被 provider 拒绝)。
|
|
@@ -287,7 +300,67 @@ v2 去掉了 `inbox/`(P2-8),老库会在下次 `zk_lint` 看到「旧版
|
|
|
287
300
|
- 分组轮转:按 `type` → 首个标签 → 「未分类」分组,各组轮流取一条,组内保持检索顺序;预算不会被排在最前的那一类全吃掉。
|
|
288
301
|
- 每条取 `title` / `path` / `absPath` / `words` 与 `snippet`(比 `lead` 多信息时用它),`budget` / `used` 走 `countWords` 口径(中文 2 字 1 词)。放不下就**整条丢弃、不切半句**,`notes[].reason` 明写 `included` / `duplicate` / `empty` / `over-budget`;`used` 是对最终整段文本算的(分隔符也计入),所以一定 `<= budget`。
|
|
289
302
|
- 0 命中返回空 `text` / 空 `notes` / `used = 0`,不编造;`completeness` / `hasMore` 沿用检索三态 —— 装配过的只是本次候选,不代表库里没有别的。需要全文仍由宿主原生 `read` 按 `absPath` 取。
|
|
290
|
-
- 命令行 `zk-kit recall [词] [--budget 500] [--limit 30] [--json]`,与工具走同一份 `assembleRecall
|
|
303
|
+
- 命令行 `zk-kit recall [词] [--budget 500] [--limit 30] [--json]`,与工具走同一份 `assembleRecall`;
|
|
304
|
+
入口块的落位也同口径(见下)。
|
|
305
|
+
- 入口块(`via=query`)排在装配正文之前、占同一份预算。它进没进 `text` 是**结构化状态** `entryPointsInText`,
|
|
306
|
+
render 读这个字段(`lib/entry-points.ts` 的 `entryPointLines` 与 `zk-kit search` / `zk-kit recall` 共用一份措辞):
|
|
307
|
+
`false` 时在正文外补列 —— 不从 `text` 里嗅字符串,笔记正文里也可能出现同样的字。
|
|
308
|
+
命中却全被预算挡下时,CLI 明说「放不进 N 词预算」,不再打印「没有匹配的笔记」。
|
|
309
|
+
|
|
310
|
+
**一跳入口(R8,§10.4 第 8 项).** 新增只读 `zk_walk`(`lib/zk.ts` 的 `walkFrom`):从一篇种子出发走一跳,补上「沿链接走」的入口。
|
|
311
|
+
- **种子解析**:路径(相对 / 绝对 / `[[路径]]`)只看磁盘,**0 个子进程**;标题 / `[[标题]]` / `[[id]]` 走一次
|
|
312
|
+
`zk list -M exact --limit 5`,再在 JS 里按 id / 标题 / 文件名精确比对 —— `-M exact` 是「逐词精确」而不是整串相等
|
|
313
|
+
(实测 `记忆预算` 会同时命中 `记忆预算的另一篇`),所以对不上就当没找到,**不拿第一个命中冒充种子,也不退回全库**。
|
|
314
|
+
- **走法**:`backlinksOf` / `outlinksOf`(各一次 `zk list --link-to` / `--linked-by`),默认 `both` = 2 个子进程;
|
|
315
|
+
标题 / id 种子再加一次解析(3 个)。互链的邻居按身份键去重,`in` 先 `out` 后。**不建全图、不递归、不 depth 2**。
|
|
316
|
+
- **边界**:`limit` 硬钳 `MAX_LINK_NOTES` = 200(不是 `MAX_SCAN` 10 000:后者配 `--format json` 会吐 17 MB,等于把全图成本放回来);
|
|
317
|
+
默认排除 archive / raw / meta(`excludedNames` 同口径),`include_archived: true` 才连归档一起看;
|
|
318
|
+
`complete / partial` 二态(按篇反查没有产生 `unknown` 的路径,**不谎称三态**),触顶明说「邻居没列全,不代表没有」。
|
|
319
|
+
每条邻居还带 `section` / `linkLine`(§14.1,`lib/edge-context.ts` 的 `linkContextOf`):那条边所在的 `##` / `###` 小节与**那一行**。
|
|
320
|
+
没有它,`lead` 对同一篇种子的所有出链是同一段文字(邻居首段),等于只给了 8 个标题 ——
|
|
321
|
+
而 Sammelverweise 的价值在于「关于 X 见这几处」。字节是白捡的:入链用邻居 JSON 里已经在传输的 `rawContent`,
|
|
322
|
+
出链读一次种子文件,**0 个额外子进程**;只认 wiki 双链与 Markdown 行内链接,代码块里的 `[[…]]` 不算,
|
|
323
|
+
找不到就整个键不出现(不拿「提到了标题」冒充链接)。
|
|
324
|
+
只回标题 / 路径 / `absPath` / 摘要 / 那一行,全文仍由宿主原生 `read` 取;不改标题、不自动 link、不写任何文件。
|
|
325
|
+
- 命令行 `zk-kit walk <种子> [--direction both|in|out] [--limit 10] [--json]`,与工具共用 `walkFrom`。
|
|
326
|
+
- 成本(`docs/bench-baseline.md` 的三档实测):路径种子 `both` 恒 2 子进程,10k 上 p95 1.3 / 1.1 / 1.8 s,
|
|
327
|
+
传输 7.2 / 15.1 / 32.3 KB(0.2 / 1 / 3 链每篇);全图同库 26–31 s / 17 MB。
|
|
328
|
+
|
|
329
|
+
**MEMORY 当检索入口(R9,§10.4 第 9 项 / §14.2).** `zk_search` / `zk_query` / `zk_recall` 的结果多一个可选 `entryPoints`。
|
|
330
|
+
两条来源,`via` 分开写:
|
|
331
|
+
- `via: 'result'` —— 条目指向**本次命中**的笔记:把「这条已晋升结论」贴回结果(3.9.0 原语义);
|
|
332
|
+
- `via: 'query'` —— **条目本身命中检索词**:来源不必在结果集里。这才是索引的方向(关键词 → 卡号 → 那张卡
|
|
333
|
+
可以不含这个词)。原来用 `byPath` 卡死,只做成了注解。
|
|
334
|
+
- 落地(`resolved`):路径式 `[[notes/x]]` 直接读盘、**0 子进程**;标题 / id 式且不在结果集里时
|
|
335
|
+
`resolved: false`,只给入口名(`title` 就是 `[[…]]` 里那一串,`path` / `absPath` 是空串 —— 不编),
|
|
336
|
+
渲染成「用 zk_walk 走进去」。不为了补一次路径再起一个 zk 进程(10k 上 `zk list -M re` 实测 0.7–1.0 s)。
|
|
337
|
+
- 口径在 `lib/entry-points.ts`:条目切分 / 退役判断复用 `splitMemoryEntries` / `isRetiredEntry`,
|
|
338
|
+
链接解析复用 `WIKI_LINK` / `resolveWikiTarget`;退役行不计(D3),归档目标归死胡同探针、不当活入口,
|
|
339
|
+
`raw/` / `inbox/` / 根目录 meta 一律不算入口。
|
|
340
|
+
- 查询比对是**机械的**:ASCII 按空格分词、CJK 长串补二元组,`entry` 文本包含任一比对项即命中;
|
|
341
|
+
不排序(按 MEMORY 文件顺序)、不做语义判断(「哪条更相关」仍是 agent 的活);最多 5 条(`MAX_ENTRY_POINTS`),
|
|
342
|
+
超出置 `entryPointsTruncated`。
|
|
343
|
+
- 解析失败或没有 MEMORY.md 时整个键不出现(老库输出与改动前一致),不谎称完整。
|
|
344
|
+
|
|
345
|
+
**死胡同可见(§14.3).** `lib/archive-probe.ts`:检索命中一篇活笔记时,顺手看 `archive/` 里有没有**同名旧结论**,
|
|
346
|
+
带出 `supersede-reason` 与 `superseded-by`,放进 `counterEntries`(最多 3 条)。
|
|
347
|
+
- 为什么:`zk_supersede` 搬进归档的旧结论被 `excludedNames` 排除出检索,只有 `zk_new` 写同名时才提示一次 ——
|
|
348
|
+
于是 agent 在读 / 想的阶段看不到「这条路试过、为什么废」,只能重新踩一遍。这不是「不查全」的谦逊,
|
|
349
|
+
是主动隐藏反例。
|
|
350
|
+
- 成本:**零子进程、零 zk 调用**。只读 `archive/` 下的文件(10k 夹具 800 篇 → 10 条命中比对 p50 50 ms),
|
|
351
|
+
标题相似度复用 `lib/similar.ts` 的同一份口径;扫描上限 `MAX_ARCHIVE_SCAN` = 2000,触顶置 `counterEntriesTruncated`
|
|
352
|
+
(没看全 ≠ 没有);对照项最多 `MAX_COUNTER_PROBES` = 50 条命中。
|
|
353
|
+
- 0 命中且给了 query 时改拿查询词当标题比 —— 「库里没有」不等于「从来没出现过」;渲染文案不再是「没有匹配」,
|
|
354
|
+
而是「没有活笔记命中,但归档里有同名旧结论」。
|
|
355
|
+
- 只提示:不改默认结果集、不改 `completeness` 口径、不判断「是不是同一个错误」;`include_archived: true` 时
|
|
356
|
+
归档已经在结果里,不再重复报。
|
|
357
|
+
|
|
358
|
+
**本地聚合计数(§14.4).** `lib/usage.ts` 在 `.zk/dsh-usage.json` 记「哪个工具被调了几次」
|
|
359
|
+
(`search` / `query` / `recall` / `walk` / `entryPoints` / `counterEntries`)+首次计数时间,`zk_stats.usage` 读出来。
|
|
360
|
+
- 只存键与数字:不记查询词、不记路径、不上报。写入失败 / 文件损坏一律静默降级,不影响工具调用。
|
|
361
|
+
- 目的:到 3.9.0 为止入口 / 一跳的验收全是机械口径(子进程数、字节、schema、与 `graph` 对账),
|
|
362
|
+
没有一条问「这条路有没有被真的用上」。`zk_stats` 的 `usage` 是唯一可观测的代理 ——
|
|
363
|
+
发布后过一段时间看 `walk` / `entryPoints` / `counterEntries` 有没有涨。
|
|
291
364
|
|
|
292
365
|
**写 MEMORY 前的容量(G1 / G4,§10.4 第 6 项).** 复用 `zk_context`,不新增 `zk_memory_check`:
|
|
293
366
|
入参加 `proposed_entry` / `extra_words`,输出增量 `headroom` / `extraWords` / `wouldTruncate` /
|