dsh-zdiary-kit 3.9.0 → 3.10.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 +8 -6
- package/dist/config/store.js +3 -0
- package/dist/features/readonly/feature.js +12 -10
- package/dist/features/readonly/html.js +10 -5
- package/dist/features/settings/client.js +1 -0
- package/dist/features/zk-tools/feature.js +146 -7
- package/dist/lib/agents-template.js +14 -8
- package/dist/lib/dream.js +36 -7
- package/dist/lib/edge-context.js +6 -7
- package/dist/lib/entry-points.js +3 -1
- package/dist/lib/fence.js +45 -0
- package/dist/lib/lint.js +8 -1
- package/dist/lib/memory-format.js +23 -4
- package/dist/lib/memory-write.js +187 -0
- package/dist/lib/memory.js +26 -19
- package/dist/lib/recall-signal.js +166 -0
- package/dist/lib/recall.js +5 -3
- package/dist/lib/timeline.js +18 -10
- package/dist/lib/zk.js +62 -25
- package/docs/features/memory.md +2 -0
- package/docs/features/zk-tools.md +25 -3
- package/package.json +1 -1
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 15.2 召回信号 sidecar:**可关的本地聚合计数**,不是查询日志。
|
|
3
|
+
*
|
|
4
|
+
* `zk_search` / `zk_query` / `zk_recall` 命中时按 path 记一笔「哪篇、哪天、被哪类检索命中」,
|
|
5
|
+
* 给 MEMORY 的晋升(topCited)、退役保护(retireCandidates)、同日淘汰、周审审计流
|
|
6
|
+
* 提供「其实被用过」的证据;否则没被召回过的笔记永远拿不到这类信号。
|
|
7
|
+
*
|
|
8
|
+
* 硬边界(§7.4 / §9.4 / §15.12):
|
|
9
|
+
* · 不记查询词、不记正文、不上报;`title` 只是命中笔记的标题,用来关联 MEMORY 的标题式 `[[来源]]`;
|
|
10
|
+
* · 默认关(`cfg.recallSignal`),关掉时不读不写,输出与改动前逐字节一致;
|
|
11
|
+
* · 原子写(tmp + rename)+ 失败静默,计数不能把工具调用弄失败;
|
|
12
|
+
* · 插件不跑定时器,只在工具调用内触发。
|
|
13
|
+
*/
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { WIKI_LINK } from './wikilink.js';
|
|
17
|
+
export const RECALL_SIGNAL_FILE = path.join('.zk', 'dsh-recall.json');
|
|
18
|
+
/** 每篇最多保留多少个不同召回日期,防止 sidecar 无限膨胀 */
|
|
19
|
+
export const MAX_RECALL_DAYS = 30;
|
|
20
|
+
/** 周审审计流的固定抽样比例(与分数无关、确定性) */
|
|
21
|
+
export const AUDIT_RATE = 0.1;
|
|
22
|
+
/** 最近这么多天内被召回过的 MEMORY 条目,不算退役候选 */
|
|
23
|
+
export const RECALL_PROTECT_DAYS = 30;
|
|
24
|
+
const empty = () => ({ version: 1, since: '', byPath: {} });
|
|
25
|
+
/** 「这份信号非空」按 signal 对象缓存:10k 篇时 for...in / Object.keys 每次都要 O(n) 枚举,
|
|
26
|
+
* 而排序比较器一条路径就会调上百次。readRecallSignal 构建时算一次,之后全是 O(1)。 */
|
|
27
|
+
const activeCache = new WeakMap();
|
|
28
|
+
/** 开关关闭 / 没有笔记本 / 文件坏了:一律空信号,调用方行为与改动前完全一致 */
|
|
29
|
+
export function readRecallSignal(cfg) {
|
|
30
|
+
if (cfg.recallSignal !== true || !cfg.notebook)
|
|
31
|
+
return empty();
|
|
32
|
+
try {
|
|
33
|
+
const raw = JSON.parse(fs.readFileSync(path.join(cfg.notebook, RECALL_SIGNAL_FILE), 'utf8'));
|
|
34
|
+
const out = empty();
|
|
35
|
+
if (typeof raw?.since === 'string')
|
|
36
|
+
out.since = raw.since;
|
|
37
|
+
let count = 0;
|
|
38
|
+
if (raw && typeof raw.byPath === 'object' && raw.byPath) {
|
|
39
|
+
for (const [p, value] of Object.entries(raw.byPath)) {
|
|
40
|
+
const e = value;
|
|
41
|
+
if (!e || typeof e !== 'object')
|
|
42
|
+
continue;
|
|
43
|
+
count += 1;
|
|
44
|
+
out.byPath[p] = {
|
|
45
|
+
count: Number.isFinite(Number(e.count)) ? Math.max(0, Math.trunc(Number(e.count))) : 0,
|
|
46
|
+
last: typeof e.last === 'string' ? e.last : '',
|
|
47
|
+
days: Array.isArray(e.days)
|
|
48
|
+
? e.days.filter((d) => typeof d === 'string').slice(-MAX_RECALL_DAYS)
|
|
49
|
+
: [],
|
|
50
|
+
queryKinds: Array.isArray(e.queryKinds)
|
|
51
|
+
? e.queryKinds.filter((k) => k === 'search' || k === 'query' || k === 'recall')
|
|
52
|
+
: [],
|
|
53
|
+
title: typeof e.title === 'string' ? e.title : '',
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
activeCache.set(out, count > 0);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return empty();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** 有实际召回记录才算「信号生效」;开关打开但空文件仍按老口径走。
|
|
65
|
+
* readRecallSignal 已按对象算好;未经它构造的手工对象(测试 / 外部)才 fallback 一次短路判断。 */
|
|
66
|
+
export function hasRecallSignal(signal) {
|
|
67
|
+
const known = activeCache.get(signal);
|
|
68
|
+
if (known !== undefined)
|
|
69
|
+
return known;
|
|
70
|
+
let active = false;
|
|
71
|
+
for (const key in signal.byPath) {
|
|
72
|
+
if (key) {
|
|
73
|
+
active = true;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
activeCache.set(signal, active);
|
|
78
|
+
return active;
|
|
79
|
+
}
|
|
80
|
+
/** 命中时记一笔:同一批里的同 path 只记一次;失败静默(计数器,不是账本) */
|
|
81
|
+
export function bumpRecall(cfg, hits, kind) {
|
|
82
|
+
if (cfg.recallSignal !== true || !hits.length || !cfg.notebook)
|
|
83
|
+
return;
|
|
84
|
+
const file = path.join(cfg.notebook, RECALL_SIGNAL_FILE);
|
|
85
|
+
const signal = readRecallSignal(cfg);
|
|
86
|
+
const now = new Date();
|
|
87
|
+
const today = now.toISOString().slice(0, 10);
|
|
88
|
+
if (!signal.since)
|
|
89
|
+
signal.since = now.toISOString();
|
|
90
|
+
const seen = new Set();
|
|
91
|
+
for (const hit of hits) {
|
|
92
|
+
if (!hit.path || seen.has(hit.path))
|
|
93
|
+
continue;
|
|
94
|
+
seen.add(hit.path);
|
|
95
|
+
const prev = signal.byPath[hit.path] ?? { count: 0, last: '', days: [], queryKinds: [], title: hit.title ?? '' };
|
|
96
|
+
signal.byPath[hit.path] = {
|
|
97
|
+
count: prev.count + 1,
|
|
98
|
+
last: today,
|
|
99
|
+
days: prev.days.includes(today) ? prev.days : [...prev.days, today].slice(-MAX_RECALL_DAYS),
|
|
100
|
+
queryKinds: prev.queryKinds.includes(kind) ? prev.queryKinds : [...prev.queryKinds, kind],
|
|
101
|
+
title: hit.title || prev.title,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
105
|
+
try {
|
|
106
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
107
|
+
fs.writeFileSync(tmp, JSON.stringify(signal), 'utf8');
|
|
108
|
+
fs.renameSync(tmp, file);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
try {
|
|
112
|
+
fs.rmSync(tmp, { force: true });
|
|
113
|
+
}
|
|
114
|
+
catch { /* 连清理都失败就只能算了 */ }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 一次工具调用内复用同一份索引:`memoryCapacity` 的排序比较器会反复调 `lastRecallOf`,
|
|
119
|
+
* 每次重建全量 lookup 会让 10k sidecar 退化到秒级。signal 只读不写(写只在 bumpRecall 的独立副本里),
|
|
120
|
+
* 所以用 WeakMap 按对象缓存,readRecallSignal 每次返回新对象,不会跨调用拿到过期索引。
|
|
121
|
+
*/
|
|
122
|
+
const lookupCache = new WeakMap();
|
|
123
|
+
/** 同一次调用里排序比较器会把同一条目反复比较,结果按 (signal, entry) 缓存 */
|
|
124
|
+
const resultCache = new WeakMap();
|
|
125
|
+
function recallLookup(signal) {
|
|
126
|
+
const cached = lookupCache.get(signal);
|
|
127
|
+
if (cached)
|
|
128
|
+
return cached;
|
|
129
|
+
const lookup = new Map();
|
|
130
|
+
for (const [p, e] of Object.entries(signal.byPath)) {
|
|
131
|
+
const stem = p.replace(/\.[^./]+$/, '');
|
|
132
|
+
for (const key of [p, stem, e.title]) {
|
|
133
|
+
if (key && e.last > (lookup.get(key) ?? ''))
|
|
134
|
+
lookup.set(key, e.last);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
lookupCache.set(signal, lookup);
|
|
138
|
+
return lookup;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* MEMORY 条目 → 最近召回日期。sidecar 的 path / stem / title 都建成键,
|
|
142
|
+
* 所以标题式 `[[标题]]` 与路径式 `[[notes/x]]` 都能关联上(纯 JS、不跑 zk)。
|
|
143
|
+
* signal 只读、entry 是字符串,结果确定 —— 一次调用内按 entry 缓存,避免排序比较器重复解析。
|
|
144
|
+
*/
|
|
145
|
+
export function lastRecallOf(signal, entry) {
|
|
146
|
+
if (!hasRecallSignal(signal))
|
|
147
|
+
return '';
|
|
148
|
+
let results = resultCache.get(signal);
|
|
149
|
+
if (!results) {
|
|
150
|
+
results = new Map();
|
|
151
|
+
resultCache.set(signal, results);
|
|
152
|
+
}
|
|
153
|
+
const cached = results.get(entry);
|
|
154
|
+
if (cached !== undefined)
|
|
155
|
+
return cached;
|
|
156
|
+
const lookup = recallLookup(signal);
|
|
157
|
+
let best = '';
|
|
158
|
+
for (const m of String(entry).matchAll(WIKI_LINK)) {
|
|
159
|
+
const target = String(m[1] ?? '').trim();
|
|
160
|
+
const hit = lookup.get(target) ?? lookup.get(target.replace(/\.[^./]+$/, ''));
|
|
161
|
+
if (hit && hit > best)
|
|
162
|
+
best = hit;
|
|
163
|
+
}
|
|
164
|
+
results.set(entry, best);
|
|
165
|
+
return best;
|
|
166
|
+
}
|
package/dist/lib/recall.js
CHANGED
|
@@ -2,6 +2,7 @@ import { countWords } from './memory.js';
|
|
|
2
2
|
import { entryPointsOf } from './entry-points.js';
|
|
3
3
|
import { archivedCounterparts } from './archive-probe.js';
|
|
4
4
|
import { bumpUsage } from './usage.js';
|
|
5
|
+
import { bumpRecall } from './recall-signal.js';
|
|
5
6
|
import { searchNotesDetailed } from './zk.js';
|
|
6
7
|
export const DEFAULT_RECALL_BUDGET = 500;
|
|
7
8
|
export const MAX_RECALL_BUDGET = 5_000;
|
|
@@ -39,9 +40,9 @@ function roundRobin(notes) {
|
|
|
39
40
|
}
|
|
40
41
|
return out;
|
|
41
42
|
}
|
|
42
|
-
/** 与 feature.ts 的 detailOf 同口径:snippet
|
|
43
|
+
/** 与 feature.ts 的 detailOf 同口径:15.6 先用 summary,空串再退回 snippet / lead(lead 实测就是 H1) */
|
|
43
44
|
function detailOf(n) {
|
|
44
|
-
return n.snippet && n.snippet !== n.lead ? n.snippet : (n.lead ?? '');
|
|
45
|
+
return n.summary || (n.snippet && n.snippet !== n.lead ? n.snippet : (n.lead ?? ''));
|
|
45
46
|
}
|
|
46
47
|
function entryText(n, index) {
|
|
47
48
|
const badges = [n.type, ...n.tags.map((t) => '#' + t)].filter(Boolean);
|
|
@@ -81,7 +82,7 @@ export async function assembleRecall(cfg, opts = {}, signal) {
|
|
|
81
82
|
let index = 0;
|
|
82
83
|
for (const n of roundRobin(result.notes)) {
|
|
83
84
|
const note = {
|
|
84
|
-
title: n.title, path: n.path, absPath: n.absPath, included: false, reason: 'over-budget',
|
|
85
|
+
title: n.title, path: n.path, absPath: n.absPath, summary: n.summary, included: false, reason: 'over-budget',
|
|
85
86
|
};
|
|
86
87
|
if (seen.has(n.path)) {
|
|
87
88
|
note.reason = 'duplicate';
|
|
@@ -119,6 +120,7 @@ export async function assembleRecall(cfg, opts = {}, signal) {
|
|
|
119
120
|
...(entryPoints.points.length ? ['entryPoints'] : []),
|
|
120
121
|
...(counters.entries.length ? ['counterEntries'] : []),
|
|
121
122
|
]);
|
|
123
|
+
bumpRecall(cfg, result.notes.map((n) => ({ path: n.path, title: n.title })), 'recall');
|
|
122
124
|
return {
|
|
123
125
|
text, used, budget,
|
|
124
126
|
completeness: result.completeness,
|
package/dist/lib/timeline.js
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日记时间轴校验(v3.1 M0):只报告,不改文件(计划 §2 D1 / D2 / D5)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独一层:日记的 `## HH:MM` / `### HH:MM` 是人的工作面,空块、倒序、层级混用
|
|
5
|
+
* 都不会让 zk 报错,也不会影响检索 —— 只会让时间轴慢慢读不出顺序。自动重排会改正文顺序,
|
|
6
|
+
* diff 不可审,所以这里只产出问题清单,修不修由人或 agent 决定。
|
|
7
|
+
*
|
|
8
|
+
* 两种结构分开判:
|
|
9
|
+
* · layered(有 `## Events`,大小写不敏感):时间条目必须是 `### HH:MM`,且落在 Events 与下一个 `##` 之间;
|
|
10
|
+
* · flat(没有 `## Events`):时间条目必须是 `## HH:MM`,按文件顺序追加。
|
|
11
|
+
* 统一要求 `## Events` 会让所有 flat 日记一次性全红,所以用第一个 `## Events` 决定结构。
|
|
12
|
+
* 大小写不敏感是为了跟 `appendDaily` 对齐:`sectionRange` 用 `/^##\s+Events\s*$/i` 找小节,
|
|
13
|
+
* 这里若区分大小写,`## EVENTS` 会被判成 flat、然后 `### HH:MM` 全报位置错。
|
|
14
|
+
*/
|
|
15
|
+
import { fenceMask } from './fence.js';
|
|
1
16
|
/** 合法时间标题:00:00–23:59,可跟全角 / 半角括号标题 */
|
|
2
17
|
const TIME_TITLE = /^(?:[01]\d|2[0-3]):[0-5]\d(?:\s*[((].*[))])?$/;
|
|
3
18
|
/** 看起来像时间(用于抓 11:5x / 9:5 / 24:00 / 12:60 / 12:45 标题):格式错也要报 */
|
|
@@ -6,7 +21,6 @@ const TIME_LIKE = /^\d{1,2}:\d{1,2}/;
|
|
|
6
21
|
const HEADING = /^(#{1,6})[ \t]+(.*?)[ \t]*$/;
|
|
7
22
|
/** ATX 收尾井号是装饰(`### 12:00 ##` 的标题仍是 `12:00`),与 zk 的 atxHeading 同口径 */
|
|
8
23
|
const CLOSING_HASHES = /[ \t]+#+$/;
|
|
9
|
-
const FENCE = /^\s{0,3}(?:```|~~~)/;
|
|
10
24
|
/**
|
|
11
25
|
* 按行遮掉 frontmatter 与围栏代码:内容换成空行,行号保持不变。
|
|
12
26
|
* 不这样做,围栏里的示例 `## 12:00` 会被当成真的时间条目。
|
|
@@ -24,16 +38,10 @@ function maskLines(body) {
|
|
|
24
38
|
}
|
|
25
39
|
}
|
|
26
40
|
}
|
|
27
|
-
|
|
28
|
-
for (let i = 0; i < masked.length; i++)
|
|
29
|
-
if (
|
|
41
|
+
const fenced = fenceMask(masked);
|
|
42
|
+
for (let i = 0; i < masked.length; i++)
|
|
43
|
+
if (fenced[i])
|
|
30
44
|
masked[i] = '';
|
|
31
|
-
inFence = !inFence;
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
if (inFence)
|
|
35
|
-
masked[i] = '';
|
|
36
|
-
}
|
|
37
45
|
return masked;
|
|
38
46
|
}
|
|
39
47
|
function parseHeadings(lines) {
|
package/dist/lib/zk.js
CHANGED
|
@@ -18,6 +18,7 @@ import { parseFrontmatter, setFrontmatter } from './frontmatter.js';
|
|
|
18
18
|
import { isMetaPath, META_NAMES } from './meta.js';
|
|
19
19
|
import { similarCandidates } from './similar.js';
|
|
20
20
|
import { linkContextOf } from './edge-context.js';
|
|
21
|
+
import { fenceMask } from './fence.js';
|
|
21
22
|
import { normalizeTitle, noteKey } from './wikilink.js';
|
|
22
23
|
export class ZkError extends Error {
|
|
23
24
|
hint;
|
|
@@ -759,6 +760,7 @@ function toRecord(raw, cfg, terms = []) {
|
|
|
759
760
|
modified: String(raw.modified ?? ''),
|
|
760
761
|
lead: String(raw.lead ?? '').replace(/\s+/g, ' ').trim().slice(0, 200),
|
|
761
762
|
snippet: buildSnippet(raw, terms),
|
|
763
|
+
summary: '',
|
|
762
764
|
words: Number(raw.wordCount ?? 0),
|
|
763
765
|
metadata,
|
|
764
766
|
id: meta('id'),
|
|
@@ -929,6 +931,7 @@ function parseLeanList(stdout, cfg, withSnippet = false) {
|
|
|
929
931
|
modified: leanIso(modified ?? ''),
|
|
930
932
|
lead,
|
|
931
933
|
snippet,
|
|
934
|
+
summary: '',
|
|
932
935
|
words: Number(words ?? 0),
|
|
933
936
|
metadata,
|
|
934
937
|
id: String(id ?? ''),
|
|
@@ -1053,7 +1056,8 @@ export async function searchNotesDetailed(cfg, opts = {}, signal) {
|
|
|
1053
1056
|
: extra.malformed || extra.strategyFailed ? 'unknown' : 'complete';
|
|
1054
1057
|
// R2:只有 complete 时 notes.length 才是精确总数;partial / unknown 不给 total。
|
|
1055
1058
|
const total = completeness === 'complete' ? { total: notes.length } : {};
|
|
1056
|
-
|
|
1059
|
+
// 15.6:只对可见命中读盘补 summary(O(limit) 本地 IO,零子进程);scanNotes 全库路径不经过这里
|
|
1060
|
+
return { notes: attachSummaries(cfg, notes.slice(0, limit)), hasMore: more, completeness, scanned: examined, ...total, matchStrategy: 'fts' };
|
|
1057
1061
|
};
|
|
1058
1062
|
/**
|
|
1059
1063
|
* mixed 的判据必须看「截断后的可见结果」:FTS 已占满 limit 时,正则补的那条
|
|
@@ -1576,6 +1580,7 @@ function noteFromDisk(cfg, abs) {
|
|
|
1576
1580
|
modified: stat.mtime.toISOString(),
|
|
1577
1581
|
lead: '',
|
|
1578
1582
|
snippet: '',
|
|
1583
|
+
summary: summarizeContent(content),
|
|
1579
1584
|
words: content.split(/\s+/).filter(Boolean).length,
|
|
1580
1585
|
metadata: { ...fields },
|
|
1581
1586
|
id: fields.id ?? '',
|
|
@@ -1856,24 +1861,55 @@ function atxHeading(line) {
|
|
|
1856
1861
|
const title = m[2].replace(/[ \t]+#+$/, '').trim();
|
|
1857
1862
|
return title ? { level: m[1].length, title } : null;
|
|
1858
1863
|
}
|
|
1859
|
-
/**
|
|
1860
|
-
function
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1864
|
+
/** `## Summary` / `## 摘要` 都算摘要节:写入(fillSummary)与读回(summarizeContent)共用同一口径 */
|
|
1865
|
+
function isSummaryTitle(title) {
|
|
1866
|
+
return /^(summary|摘要)$/i.test(title.trim());
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* 15.6:从正文取一句话摘要,只读不改。
|
|
1870
|
+
* frontmatter 的 `summary` 标量优先;否则读 `## Summary` 一节,取到下一个同级 / 更高级标题之前,
|
|
1871
|
+
* 首段压空白、截到 240 字。没有这一节 / 读不到就返回空串(老笔记不假装有摘要)。
|
|
1872
|
+
*/
|
|
1873
|
+
export function summarizeContent(content) {
|
|
1874
|
+
const fromMeta = String(parseFrontmatter(content).fields.summary ?? '').replace(/\s+/g, ' ').trim();
|
|
1875
|
+
if (fromMeta)
|
|
1876
|
+
return fromMeta.slice(0, 240);
|
|
1877
|
+
const lines = String(content ?? '').split('\n');
|
|
1878
|
+
const fenced = fenceMask(lines);
|
|
1879
|
+
const heads = lines.map((line, i) => (fenced[i] ? null : atxHeading(line)));
|
|
1880
|
+
const target = heads.findIndex((h) => h !== null && isSummaryTitle(h.title));
|
|
1881
|
+
if (target < 0)
|
|
1882
|
+
return '';
|
|
1883
|
+
const head = heads[target];
|
|
1884
|
+
let end = lines.length;
|
|
1885
|
+
for (let i = target + 1; i < lines.length; i += 1) {
|
|
1886
|
+
const next = heads[i];
|
|
1887
|
+
if (next && next.level <= head.level) {
|
|
1888
|
+
end = i;
|
|
1889
|
+
break;
|
|
1874
1890
|
}
|
|
1875
1891
|
}
|
|
1876
|
-
return
|
|
1892
|
+
return lines.slice(target + 1, end).join('\n').replace(/\s+/g, ' ').trim().slice(0, 240);
|
|
1893
|
+
}
|
|
1894
|
+
/**
|
|
1895
|
+
* 15.6:给一批**已可见**的命中补 `summary`。逐条 `readFileSync` 是本地文件 IO、不是子进程,
|
|
1896
|
+
* 不改 LEAN 模板、不落索引、不给全库扫描用 —— 只挂在检索结果的出口(O(limit))。
|
|
1897
|
+
* 已有非空 summary 的行直接跳过,避免只读视图对同一批结果重复读盘。
|
|
1898
|
+
*/
|
|
1899
|
+
export function attachSummaries(cfg, notes) {
|
|
1900
|
+
return notes.map((n) => {
|
|
1901
|
+
if (n.summary)
|
|
1902
|
+
return n;
|
|
1903
|
+
const abs = n.absPath || (n.path ? path.resolve(cfg.notebook, n.path) : '');
|
|
1904
|
+
if (!abs)
|
|
1905
|
+
return n;
|
|
1906
|
+
try {
|
|
1907
|
+
return { ...n, summary: summarizeContent(fs.readFileSync(abs, 'utf8')) };
|
|
1908
|
+
}
|
|
1909
|
+
catch {
|
|
1910
|
+
return n;
|
|
1911
|
+
}
|
|
1912
|
+
});
|
|
1877
1913
|
}
|
|
1878
1914
|
/**
|
|
1879
1915
|
* 把一句话摘要填进 `## Summary`。
|
|
@@ -1892,10 +1928,9 @@ export function fillSummary(content, summary) {
|
|
|
1892
1928
|
if (!body)
|
|
1893
1929
|
return { text: content, applied: false };
|
|
1894
1930
|
const lines = String(content ?? '').split('\n');
|
|
1895
|
-
const fenced =
|
|
1931
|
+
const fenced = fenceMask(lines);
|
|
1896
1932
|
const heads = lines.map((line, i) => (fenced[i] ? null : atxHeading(line)));
|
|
1897
|
-
const
|
|
1898
|
-
const target = heads.findIndex((h) => h !== null && isSummary(h.title));
|
|
1933
|
+
const target = heads.findIndex((h) => h !== null && isSummaryTitle(h.title));
|
|
1899
1934
|
if (target >= 0) {
|
|
1900
1935
|
const head = heads[target];
|
|
1901
1936
|
let end = lines.length;
|
|
@@ -1909,7 +1944,7 @@ export function fillSummary(content, summary) {
|
|
|
1909
1944
|
if (lines.slice(target + 1, end).some((line) => line.trim() !== ''))
|
|
1910
1945
|
return { text: content, applied: false };
|
|
1911
1946
|
// 正文自己又写了一份 Summary(模板那一节只是空壳):那一节已经有主,不覆盖、也不在旁边再长一节
|
|
1912
|
-
const writtenByContent = heads.some((h, k) => k >= end && h !== null && h.level === head.level &&
|
|
1947
|
+
const writtenByContent = heads.some((h, k) => k >= end && h !== null && h.level === head.level && isSummaryTitle(h.title));
|
|
1913
1948
|
if (writtenByContent)
|
|
1914
1949
|
return { text: content, applied: false };
|
|
1915
1950
|
const out = [...lines.slice(0, target + 1), '', body, '', ...lines.slice(end)];
|
|
@@ -2317,10 +2352,12 @@ function relativeLinkPath(outDir, targetAbs, keepExt) {
|
|
|
2317
2352
|
rel = path.basename(targetAbs);
|
|
2318
2353
|
return keepExt ? rel : rel.replace(NOTE_EXT, '');
|
|
2319
2354
|
}
|
|
2320
|
-
/** 按计划重写一段 Markdown
|
|
2355
|
+
/** 按计划重写一段 Markdown 里的链接;返回新文本与被改条数。围栏里的示例链接不动 */
|
|
2321
2356
|
function relinkText(cfg, text, plan) {
|
|
2322
2357
|
let changed = 0;
|
|
2323
|
-
const
|
|
2358
|
+
const lines = String(text).split('\n');
|
|
2359
|
+
const masked = fenceMask(lines);
|
|
2360
|
+
const wikiOf = (line) => line.replace(WIKI_LINK, (whole, inner) => {
|
|
2324
2361
|
const bar = inner.indexOf('|');
|
|
2325
2362
|
const head = bar >= 0 ? inner.slice(0, bar) : inner;
|
|
2326
2363
|
const tail = bar >= 0 ? inner.slice(bar) : '';
|
|
@@ -2343,7 +2380,7 @@ function relinkText(cfg, text, plan) {
|
|
|
2343
2380
|
changed++;
|
|
2344
2381
|
return rebuilt;
|
|
2345
2382
|
});
|
|
2346
|
-
const
|
|
2383
|
+
const mdOf = (line) => line.replace(MD_LINK, (whole, prefix, dest, tail) => {
|
|
2347
2384
|
const angled = dest.startsWith('<') && dest.endsWith('>');
|
|
2348
2385
|
const raw = angled ? dest.slice(1, -1) : dest;
|
|
2349
2386
|
if (!raw || raw.startsWith('#'))
|
|
@@ -2368,7 +2405,7 @@ function relinkText(cfg, text, plan) {
|
|
|
2368
2405
|
changed++;
|
|
2369
2406
|
return rebuilt;
|
|
2370
2407
|
});
|
|
2371
|
-
return { text:
|
|
2408
|
+
return { text: lines.map((line, i) => (masked[i] ? line : mdOf(wikiOf(line)))).join('\n'), changed };
|
|
2372
2409
|
}
|
|
2373
2410
|
/** 新路径归一化:可只给文件名、可给目录(末尾 /),扩展名不变;跑出笔记本由 absPathOf 兜 */
|
|
2374
2411
|
function renameTargetRel(cfg, oldAbs, to) {
|
package/docs/features/memory.md
CHANGED
|
@@ -35,6 +35,8 @@
|
|
|
35
35
|
**Does not own.** MEMORY.md 写什么(agent 按 AGENTS.md 直接改文件,git diff 可审);
|
|
36
36
|
哪些记忆该晋升(语义判断是 agent 的活,插件只做机械截断);`MEMORY.md` 的写入路径 ——
|
|
37
37
|
它由人和 agent 直接编辑,`zk_append` / `zk_link` / `zk_supersede` 都会拒绝(追加会把记忆变成流水账,也丢掉「谁改的」这条审计线;后者还会把文件搬进 archive/)。
|
|
38
|
+
唯一的例外是 `zk_memory_write`(§15.1):它按机械门只往末尾追加一条调用方给的结论,并往 `DREAMS.md` 追加改前摘要与 diff;
|
|
39
|
+
不替换 / 合并 / 改写已有条目,也不自动判断该写什么,语义判断仍在调用方。
|
|
38
40
|
|
|
39
41
|
**人格层(0.10 M1)。** `PERSONA.md` 与 `MEMORY.md` 同级:根目录 meta,人类所有,
|
|
40
42
|
`zk_append` / `zk_new` / `zk_link` / `zk_supersede` 一律拒绝写入或搬移。
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
**Purpose.** 让模型用 zk 思维工作:先查重、再写原子笔记、用双链连起来、每天记一段。
|
|
4
4
|
|
|
5
|
-
**Owns.**
|
|
6
|
-
`zk_supersede` / `zk_rename` / `zk_context` / `zk_lint` / `zk_dream` / `zk_stats` / `zk_use`)以及笔记本
|
|
5
|
+
**Owns.** 16 个内置工具定义(`zk_search` / `zk_query` / `zk_recall` / `zk_walk` / `zk_new` / `zk_append` / `zk_daily` / `zk_link` /
|
|
6
|
+
`zk_supersede` / `zk_rename` / `zk_context` / `zk_memory_write` / `zk_lint` / `zk_dream` / `zk_stats` / `zk_use`)以及笔记本
|
|
7
7
|
`.zk/config.toml` 的 `[alias]` 派生的 `zk_<别名>` 工具:
|
|
8
8
|
参数 schema、输出 schema、render、每个工具自己的 `ensureNotebook` 与参数校验。
|
|
9
9
|
`zk_context` 例外:它只读根目录三份 meta 文件与角色人格(`PERSONA.md`),并列出已配置笔记本(另读 `DREAMS.md` 的「上次做梦」一行用于周节奏提醒),**不** `ensureNotebook`(读记忆不该依赖 zk)。
|
|
@@ -36,7 +36,7 @@ LEAN 模板加一列 `{{metadata.id}}`,`parseLeanList` 的定长字段从 11
|
|
|
36
36
|
相似标题继续只作 warning,不升级为去重方案。
|
|
37
37
|
|
|
38
38
|
**Alias tools(`zk_<别名>`).** 默认**不注册**:`aliasTools` 为 false 时 `buildTools` 连 `.zk/config.toml` 都不读,
|
|
39
|
-
内置工具固定
|
|
39
|
+
内置工具固定 16 个。别名是 `$SHELL -c` 的任意命令,注册成工具等于把它交到模型手里,所以要人显式打开
|
|
40
40
|
(设置页「工具」组的复选框,或 `zk-kit config set aliasTools=true`,只认 `true`;拼错的值归一化成 false 并在 `config set` 时回显)。
|
|
41
41
|
打开后,插件加载时读所有已配置笔记本的 `.zk/config.toml`,把 `[alias]` 表里未注释的键注册成工具;
|
|
42
42
|
同名只取第一本。工具名用下划线而不是点号:DSH 的 function-name 契约是 `[A-Za-z0-9_-]`(`zk.lucky` 会被 provider 拒绝)。
|
|
@@ -382,3 +382,25 @@ dirty 留到下次读。**回退只允许落在完整 db 上**:db 不完整(
|
|
|
382
382
|
`zk_new` 本身不需要标脏:`zk new` 自己会把新文件写进索引;只有摘要回填(`fs.writeFileSync` 绕过了 zk)才补标。
|
|
383
383
|
外部编辑不被感知:`indexLagHint`(doctor / `zk_lint` 的 `indexLag`)只比 `notebook.db` 与笔记本**顶层**条目的 mtime,
|
|
384
384
|
约十次 stat 而不是 10⁴ 次;目录 mtime 只在新增 / 删除 / 改名时变,**改已有笔记的正文一定漏报**(不是「可能漏报」)。
|
|
385
|
+
|
|
386
|
+
**记忆写路径(§15.1 / §10.4 第 16 项).** `zk_memory_write` 是第一个写根目录 meta 的工具;
|
|
387
|
+
所有 `zk_append` / `zk_new` / `zk_link` / `zk_supersede` 仍一律拒写 meta(`isMetaPath`)。
|
|
388
|
+
入参 `target: MEMORY|USER`、`entry`(调用方给全文)、`action: add|retire`、`dry_run`(默认 true)、
|
|
389
|
+
`expect_stamp`(可选,预演返回的 `fileStamp`)。机械门全在 `lib/memory-write.ts`:
|
|
390
|
+
|
|
391
|
+
1. 形状:`splitMemoryEntries(entry)` 必须恰好一条顶层列表项,不带额外段落;
|
|
392
|
+
2. 日期:`entryDate()` 要能解析出合法 `YYYY-MM-DD`(与 `memoryFormatDebt` 同一份口径);
|
|
393
|
+
3. 来源:`WIKI_LINK` 取第一个 `[[…]]`,`resolveWikiTarget(cfg, target, wikiIndex(scanNotes))` 必须是 `live`;
|
|
394
|
+
索引没扫全(`completeness !== 'complete'`)或 zk 起不来时,只放**路径式**来源过(`index-incomplete` 拒绝码),
|
|
395
|
+
不拿前 10k 篇里碰巧的标题命中冒充核实过;
|
|
396
|
+
4. 容量:`memoryCapacity({ proposedEntry })`,`add` 且 `wouldTruncate` 时返回 `over-budget` +
|
|
397
|
+
`needToFree` + `dropCandidates`,绝不静默截断;`retire` 不做容量门(退役条目在注入里被 strip);
|
|
398
|
+
5. 并发:`fileStamp` 在预演与落盘前各核一次,`expect_stamp` 不符返回 `stale-stamp`(与设置页 409 同语义);
|
|
399
|
+
6. 落盘:`tmp + rename` 原子写,只往文件末尾追加;每次真写把 pre-image 摘要(前 40 行 / 4000 字)与一行 diff
|
|
400
|
+
追加到 `DREAMS.md`。
|
|
401
|
+
|
|
402
|
+
**边界**:不替换 / 合并现有条目、不自动补日期 / 来源、不提议退役哪条;`retire` 追加的是调用方给的
|
|
403
|
+
`- [superseded …]` 标记,真正让某条现行结论退役仍需原生 edit 改写那一行(语义判断)。
|
|
404
|
+
`WORKFLOW_TEMPLATE` 的「改 MEMORY.md 之前」补一行「先 `zk_memory_write` 预演」;`zk_context` 保持只读预览。
|
|
405
|
+
**成本**:格式门要核实标题式来源,走一次 `scanNotes`(**1 个 zk 子进程**,与 `zk_lint` 同口径);
|
|
406
|
+
容量 / 日期 / 并发门都是纯 JS。详见 `docs/plan.md` §16 的实施记录。
|