dsh-zdiary-kit 2.0.0 → 2.2.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 +29 -8
- package/dist/app/plugin.js +60 -4
- package/dist/cli.js +156 -1
- package/dist/config/store.js +4 -0
- package/dist/features/readonly/feature.js +36 -5
- package/dist/features/settings/client.js +29 -3
- package/dist/features/settings/feature.js +41 -0
- package/dist/features/zk-tools/feature.js +208 -10
- package/dist/lib/agents-template.js +11 -2
- package/dist/lib/lint.js +5 -3
- package/dist/lib/memory.js +7 -17
- package/dist/lib/scene.js +250 -0
- package/dist/lib/state-contract.js +130 -0
- package/dist/lib/state-log.js +395 -0
- package/dist/lib/state-rules.js +662 -0
- package/dist/lib/zk.js +7 -1
- package/docs/features/memory.md +25 -10
- package/docs/features/provisioning.md +6 -2
- package/docs/features/readonly.md +5 -2
- package/docs/features/settings.md +6 -3
- package/docs/features/zk-tools.md +29 -6
- package/package.json +1 -1
|
@@ -16,6 +16,7 @@ import { CFG_FILE, processWorkspace } from '../../config/paths.js';
|
|
|
16
16
|
import { ArchiveDirChangeError, config, configStamp, ConfigConflictError, writeConfig } from '../../config/store.js';
|
|
17
17
|
import { ensureNotebook, structureStatus, writeAgentsTemplate, writeContractTemplates } from '../../lib/notebook.js';
|
|
18
18
|
import { buildMemoryPayload, formatFileShares } from '../../lib/memory.js';
|
|
19
|
+
import { buildScene } from '../../lib/scene.js';
|
|
19
20
|
import { contractStatus } from '../../lib/lint.js';
|
|
20
21
|
import { installZk, sourceLabel } from '../../lib/install.js';
|
|
21
22
|
import { hasNotebook, indexNotebook, notebookStats, resolveZk } from '../../lib/zk.js';
|
|
@@ -52,6 +53,7 @@ async function snapshot(cfg) {
|
|
|
52
53
|
memory = {
|
|
53
54
|
words: payload.words,
|
|
54
55
|
budget: payload.budget,
|
|
56
|
+
totalWords: payload.totalWords,
|
|
55
57
|
truncatedWords: payload.truncatedWords,
|
|
56
58
|
retiredWords: payload.retiredWords,
|
|
57
59
|
tocWords: payload.tocWords,
|
|
@@ -62,6 +64,24 @@ async function snapshot(cfg) {
|
|
|
62
64
|
catch {
|
|
63
65
|
memory = null;
|
|
64
66
|
}
|
|
67
|
+
// 状态层只读回显:没有日志时 present=false(不是错误)
|
|
68
|
+
let scene = null;
|
|
69
|
+
try {
|
|
70
|
+
const s = buildScene(cfg);
|
|
71
|
+
scene = {
|
|
72
|
+
present: s.present,
|
|
73
|
+
presence: s.presence,
|
|
74
|
+
events: s.events,
|
|
75
|
+
rulesVersion: s.rulesVersion,
|
|
76
|
+
words: s.words,
|
|
77
|
+
budget: cfg.sceneBudgetWords,
|
|
78
|
+
emptyReason: s.emptyReason,
|
|
79
|
+
...(s.rulesError ? { rulesError: s.rulesError } : {}),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
scene = null;
|
|
84
|
+
}
|
|
65
85
|
return {
|
|
66
86
|
zk,
|
|
67
87
|
// 本页解析空 / 相对路径用的是插件进程工作区;agent 工具按各自会话工作区,可能不是同一个目录
|
|
@@ -69,6 +89,7 @@ async function snapshot(cfg) {
|
|
|
69
89
|
notebook: { path: cfg.notebook, exists },
|
|
70
90
|
structure,
|
|
71
91
|
memory,
|
|
92
|
+
scene,
|
|
72
93
|
dailyTemplate: fs.existsSync(path.join(cfg.notebook, '.zk', 'templates', 'daily.md')),
|
|
73
94
|
counts,
|
|
74
95
|
countsError,
|
|
@@ -351,6 +372,26 @@ export function registerSettings({ route, isTrusted }) {
|
|
|
351
372
|
: `${st.dirs.length} 个目录齐全` + drift
|
|
352
373
|
: `缺目录:${st.missing.join('、')} —— 点「初始化 / 补建」` + drift,
|
|
353
374
|
});
|
|
375
|
+
// 状态层:空日志 = 未启用(不算错);规则回退 / 读不了才是问题 ——
|
|
376
|
+
// 那意味着模型这一轮看到的场景是错的;自检在这项上不能比 doctor 宽
|
|
377
|
+
try {
|
|
378
|
+
const s = buildScene(cfg);
|
|
379
|
+
const presence = s.presence === 'present' ? '在场' : s.presence === 'absent' ? '不在' : '未知';
|
|
380
|
+
const detail = s.present
|
|
381
|
+
? `场景 ${s.words}/${cfg.sceneBudgetWords} 词 · 规则 ${s.rulesVersion} · 窗口内 ${s.events} 条事件 · 用户${presence}` +
|
|
382
|
+
(s.rulesError ? `;⚠️ 规则回退默认:${s.rulesError}` : '')
|
|
383
|
+
: s.emptyReason === 'budget'
|
|
384
|
+
? `有事件,但场景预算(${cfg.sceneBudgetWords} 词)一句都放不下 —— 调大场景预算`
|
|
385
|
+
: s.emptyReason === 'rules'
|
|
386
|
+
? '有事件,但规则没算出可展示的值(energy / mood / satiety / 地点 / 目标 都是 null)'
|
|
387
|
+
: s.emptyReason === 'error'
|
|
388
|
+
? `读取失败:${s.rulesError ?? '看 doctor 的「状态」一行'}`
|
|
389
|
+
: '未启用:还没有 .state/events.log(agent 用 zk_state append 记第一条,缺文件不算错)';
|
|
390
|
+
items.push({ name: '状态', ok: !s.rulesError, detail });
|
|
391
|
+
}
|
|
392
|
+
catch (err) {
|
|
393
|
+
items.push({ name: '状态', ok: false, detail: err instanceof Error ? err.message : String(err) });
|
|
394
|
+
}
|
|
354
395
|
try {
|
|
355
396
|
const payload = buildMemoryPayload(cfg);
|
|
356
397
|
const present = payload.files.filter((f) => f.present);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* zk 工具(feature 的公共入口):
|
|
2
|
+
* zk 工具(feature 的公共入口):15 个内置工具 + `[alias]` 别名工具,全部经 lib/zk.ts。
|
|
3
3
|
*
|
|
4
4
|
* 设计要点:
|
|
5
5
|
* · 除 zk_context(只读 meta 文件,不依赖 zk)外,每个工具进来先 ensureNotebook —— 第一次调用就自动建好笔记本,用户不用先点设置页;
|
|
@@ -19,9 +19,14 @@ import { searchNotes, searchNotesDetailed, createNote, readNote, findNote, appen
|
|
|
19
19
|
import { configuredAliases, readAliases } from '../../lib/alias.js';
|
|
20
20
|
import { buildMemoryPayload, META_FILES } from '../../lib/memory.js';
|
|
21
21
|
import { META_NAMES } from '../../lib/meta.js';
|
|
22
|
+
import { buildScene, combinedWords } from '../../lib/scene.js';
|
|
23
|
+
import { buildStateContract } from '../../lib/state-contract.js';
|
|
24
|
+
import { appendEvent, checkAppendInput, pruneStateLog, readStateLog, StateError } from '../../lib/state-log.js';
|
|
22
25
|
import { formatLintExtras, formatMemoryLinks, lintNotebook } from '../../lib/lint.js';
|
|
23
26
|
import { dreamNotebook, STALE_DAYS } from '../../lib/dream.js';
|
|
24
27
|
const text = (value) => [{ type: 'text', text: value }];
|
|
28
|
+
/** 事件参数回显成一行 `key=value`;含空白 / 引号的值用 JSON 引号包一层 */
|
|
29
|
+
const paramsText = (params) => Object.entries(params).map(([key, value]) => `${key}=${/[\s"]/.test(value) ? JSON.stringify(value) : value}`).join(' ');
|
|
25
30
|
const objSchema = (properties, required) => ({
|
|
26
31
|
type: 'object',
|
|
27
32
|
properties,
|
|
@@ -188,7 +193,8 @@ const LINT_PARSE = objSchema({
|
|
|
188
193
|
},
|
|
189
194
|
}, ['total', 'items']);
|
|
190
195
|
function fail(err) {
|
|
191
|
-
|
|
196
|
+
// StateError 与 ZkError 同为「人话 + 下一步」:hint 必须一起交给模型,否则它只知道写错了、不知道怎么写
|
|
197
|
+
if (err instanceof ZkError || err instanceof StateError) {
|
|
192
198
|
throw new Error(err.hint ? `${err.message}(${err.hint})` : err.message);
|
|
193
199
|
}
|
|
194
200
|
throw err instanceof Error ? err : new Error(String(err));
|
|
@@ -941,7 +947,7 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
941
947
|
name: 'zk_context',
|
|
942
948
|
description: '读知识库根目录的长期约定与记忆(AGENTS.md / USER.md / MEMORY.md),按注入预算截断后返回;' +
|
|
943
949
|
'角色契约(PERSONA.md)单独成段、在记忆段之前、不参与截断。' +
|
|
944
|
-
'同时列出已配置的笔记本(名字 / 路径 / 结构 /
|
|
950
|
+
'同时列出已配置的笔记本(名字 / 路径 / 结构 / 当前),并回带场景投影与当前规则的事件词汇(stateEvents / stateRulesVersion)。' +
|
|
945
951
|
'宿主支持注入时这些内容每轮已自动进入系统提示;宿主不支持、或想核对「被截断了多少」时用它。',
|
|
946
952
|
parameters: { type: 'object', properties: {}, additionalProperties: false },
|
|
947
953
|
output: {
|
|
@@ -974,9 +980,16 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
974
980
|
}, ['present', 'contentWords', 'words', 'noticeWords', 'budget', 'overBudget', 'overWords', 'files']),
|
|
975
981
|
scene: objSchema({
|
|
976
982
|
present: { type: 'boolean' },
|
|
977
|
-
|
|
983
|
+
presence: { type: 'string' },
|
|
984
|
+
events: { type: 'number' },
|
|
985
|
+
rulesVersion: { type: 'string' },
|
|
978
986
|
words: { type: 'number' },
|
|
979
|
-
|
|
987
|
+
emptyReason: { type: 'string' },
|
|
988
|
+
}, ['present', 'presence', 'events', 'rulesVersion', 'words', 'emptyReason']),
|
|
989
|
+
combinedWords: { type: 'number' },
|
|
990
|
+
combinedBudget: { type: 'number' },
|
|
991
|
+
stateEvents: { type: 'string' },
|
|
992
|
+
stateRulesVersion: { type: 'string' },
|
|
980
993
|
notebook: { type: 'string' },
|
|
981
994
|
defaultNotebook: { type: 'string' },
|
|
982
995
|
pinned: { type: 'string' },
|
|
@@ -999,14 +1012,31 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
999
1012
|
retiredWords: { type: 'number' },
|
|
1000
1013
|
}, ['name', 'present', 'words', 'originalWords', 'retiredWords']),
|
|
1001
1014
|
},
|
|
1002
|
-
}, ['text', 'words', 'budget', 'ratio', 'truncatedWords', 'retiredWords', 'tocWords', 'totalWords', 'overheadWords', 'contract', 'scene', 'notebook', 'defaultNotebook', 'pinned', 'notebooks', 'files']),
|
|
1015
|
+
}, ['text', 'words', 'budget', 'ratio', 'truncatedWords', 'retiredWords', 'tocWords', 'totalWords', 'overheadWords', 'contract', 'scene', 'combinedWords', 'combinedBudget', 'stateEvents', 'stateRulesVersion', 'notebook', 'defaultNotebook', 'pinned', 'notebooks', 'files']),
|
|
1003
1016
|
},
|
|
1004
1017
|
async run(_args, env) {
|
|
1005
1018
|
// 不 ensureNotebook:读记忆不该依赖 zk,笔记本不存在时就是空载荷
|
|
1006
1019
|
const cfg = configFor(env.workspace, env.sessionId);
|
|
1007
1020
|
const state = sessionState(env.workspace, env.sessionId);
|
|
1021
|
+
const payload = buildMemoryPayload(cfg);
|
|
1022
|
+
const scene = buildScene(cfg);
|
|
1023
|
+
const stateContract = buildStateContract(cfg);
|
|
1008
1024
|
return {
|
|
1009
|
-
...
|
|
1025
|
+
...payload,
|
|
1026
|
+
// 场景单独投影、单独预算(D7):不进 memory text,也不占记忆预算
|
|
1027
|
+
scene: {
|
|
1028
|
+
present: scene.present,
|
|
1029
|
+
presence: scene.presence,
|
|
1030
|
+
events: scene.events,
|
|
1031
|
+
rulesVersion: scene.rulesVersion,
|
|
1032
|
+
words: scene.words,
|
|
1033
|
+
emptyReason: scene.emptyReason,
|
|
1034
|
+
},
|
|
1035
|
+
combinedWords: combinedWords(payload.totalWords, scene),
|
|
1036
|
+
combinedBudget: cfg.totalPromptBudgetWords,
|
|
1037
|
+
// 宿主没有 systemPrompt 时的后备:把当前规则的事件词汇带在 context 里
|
|
1038
|
+
stateEvents: stateContract?.text ?? '',
|
|
1039
|
+
stateRulesVersion: stateContract?.rulesVersion ?? '',
|
|
1010
1040
|
notebook: state.active,
|
|
1011
1041
|
defaultNotebook: state.defaultName,
|
|
1012
1042
|
pinned: state.diverged ? state.pinned : '',
|
|
@@ -1026,14 +1056,182 @@ export function buildTools(workspaceOf = () => processWorkspace()) {
|
|
|
1026
1056
|
? `笔记本「${current.name}」(${current.structure})→ ${current.path}` +
|
|
1027
1057
|
(others.length ? `\n另有:${others.map((n) => `${n.name}(${n.structure})`).join('、')} —— zk_use 切换本会话` : '')
|
|
1028
1058
|
: `笔记本「${v.notebook}」`;
|
|
1029
|
-
|
|
1030
|
-
|
|
1059
|
+
// 宿主没有 systemPrompt、或想核对当前规则时,词汇跟 context 一起回
|
|
1060
|
+
const stateLine = v.stateEvents
|
|
1061
|
+
? `\n\n${v.stateEvents}${v.stateRulesVersion ? ` · 规则 ${v.stateRulesVersion}` : ''}`
|
|
1062
|
+
: '';
|
|
1063
|
+
if (!v.text && !v.scene.present) {
|
|
1064
|
+
const why = v.scene.emptyReason === 'budget'
|
|
1065
|
+
? '场景有事件,但一行都放不下(调大 sceneBudgetWords)'
|
|
1066
|
+
: v.scene.emptyReason === 'rules'
|
|
1067
|
+
? '场景规则没算出可展示的值'
|
|
1068
|
+
: v.scene.emptyReason === 'error'
|
|
1069
|
+
? '场景读取失败(看 doctor / 设置页)'
|
|
1070
|
+
: '没有状态事件';
|
|
1071
|
+
return text(drift + head + stateLine + `\n\n知识库根目录没有 ${META_FILES.join(' / ')}(或都是空的),也没有 PERSONA.md 契约。\n${why}。`);
|
|
1072
|
+
}
|
|
1031
1073
|
const contract = v.contract?.present
|
|
1032
1074
|
? `\n\n契约:正文(含标题)${v.contract.contentWords}/${v.contract.budget} 词` +
|
|
1033
1075
|
`(${v.contract.files.filter((f) => f.present).map((f) => `${f.name} ${f.words} 词`).join(' · ')})` +
|
|
1034
1076
|
(v.contract.overBudget ? `,超 ${v.contract.overWords} 词(契约不截断)` : '')
|
|
1035
1077
|
: '\n\n契约:未启用(根目录没有 PERSONA.md)';
|
|
1036
|
-
|
|
1078
|
+
const presence = v.scene.presence === 'present' ? '在场' : v.scene.presence === 'absent' ? '不在' : '未知';
|
|
1079
|
+
const scene = v.scene.present
|
|
1080
|
+
? `\n\n场景:${v.scene.words} 词(规则 ${v.scene.rulesVersion} · 窗口内 ${v.scene.events} 条事件 · 用户${presence})`
|
|
1081
|
+
: v.scene.emptyReason === 'budget'
|
|
1082
|
+
? '\n\n场景:有事件,但一句都放不下(调大 sceneBudgetWords)'
|
|
1083
|
+
: v.scene.emptyReason === 'rules'
|
|
1084
|
+
? '\n\n场景:规则没算出可展示的值(energy / mood / satiety / 地点 / 目标 都是 null)'
|
|
1085
|
+
: v.scene.emptyReason === 'error'
|
|
1086
|
+
? '\n\n场景:读取失败(看 doctor / 设置页)'
|
|
1087
|
+
: '\n\n场景:未启用(.state/events.log 不存在或没有事件)';
|
|
1088
|
+
const over = v.combinedBudget > 0 && v.combinedWords > v.combinedBudget
|
|
1089
|
+
? `,超 ${v.combinedWords - v.combinedBudget} 词(合并预算 ${v.combinedBudget})`
|
|
1090
|
+
: `(合并预算 ${v.combinedBudget})`;
|
|
1091
|
+
const totals = `\n\n用量:记忆载荷 ${v.totalWords} 词(预算 ${v.budget})+ 场景 ${v.scene.words} 词(预算单独算)= 合计 ${v.combinedWords} 词` + over;
|
|
1092
|
+
return text(drift + head + contract + stateLine + scene + totals + (v.text ? '\n\n' + v.text : ''));
|
|
1093
|
+
},
|
|
1094
|
+
}),
|
|
1095
|
+
t({
|
|
1096
|
+
name: 'zk_state',
|
|
1097
|
+
description: '运行时状态:一行一条事件,场景是日志的投影(不存快照、不跑定时器)。' +
|
|
1098
|
+
'append:记一件已经发生的事(不写计划)。内置事件示例:slept(入睡)/ woke hours=7.5(醒来,时长为睡眠小时数)/ ate(吃过)/ at place=书房(所在位置)/ set_goal text="写完 D7"(当前目标)/ user_left(用户离开)/ user_returned(用户回来);参数写成 key=value,值含空格要加引号。' +
|
|
1099
|
+
'有 `.state/rules` 时,系统提示里的「状态事件」是当前规则认可的词汇,以它为准;事件是你自己的叙述,插件只校验格式与时间,不校验真假,也不校验事件名是否属于内置集;别把 token 余额之类宿主观测写进来。' +
|
|
1100
|
+
'show:读当前投影(energy / mood / satiety / 地点 / 目标 / 在场)+ 窗口内最近 20 条事件,不回全量日志。' +
|
|
1101
|
+
'prune:压实日志(保留窗口内全部 + 每个事件名在窗口外的最后一条),返回 dropped / kept;不自动 commit。' +
|
|
1102
|
+
'它不是日常操作:只有人明确要求、或 doctor 提示日志偏大时才用;它会删掉旧事件,默认别碰。' +
|
|
1103
|
+
'默认不翻历史:只有用户明确问「之前 / 过去」时才查;平时不要主动读 .state。',
|
|
1104
|
+
parameters: {
|
|
1105
|
+
type: 'object',
|
|
1106
|
+
properties: {
|
|
1107
|
+
action: S.enum('append 追加事件 / show 读投影 / prune 压实', ['append', 'show', 'prune']),
|
|
1108
|
+
event: S.str('append 用:事件名(内置 slept / woke / ate / at / set_goal / user_left / user_returned;自定义规则以系统提示的状态事件为准;小写字母开头,只含 [a-z0-9_],最长 32)'),
|
|
1109
|
+
params: S.arr('append 用:参数列表,形如 key=value(如 woke 的 hours=7.5;最多 8 个;字符串含空格要加引号)', S.str('key=value')),
|
|
1110
|
+
at: S.str('append 用:回填时间(ISO,必须带时区,如 2026-11-04T07:10:00+08:00);缺省 = 现在'),
|
|
1111
|
+
days: S.num('prune 用:窗口外的截止天数(必须 ≥ 投影窗口)'),
|
|
1112
|
+
force: S.bool('prune 用:截止天数小于投影窗口时强行压实'),
|
|
1113
|
+
},
|
|
1114
|
+
required: ['action'],
|
|
1115
|
+
additionalProperties: false,
|
|
1116
|
+
},
|
|
1117
|
+
output: {
|
|
1118
|
+
schema: objSchema({
|
|
1119
|
+
action: { type: 'string' },
|
|
1120
|
+
present: { type: 'boolean' },
|
|
1121
|
+
presence: { type: 'string' },
|
|
1122
|
+
rulesVersion: { type: 'string' },
|
|
1123
|
+
words: { type: 'number' },
|
|
1124
|
+
text: { type: 'string' },
|
|
1125
|
+
emptyReason: { type: 'string' },
|
|
1126
|
+
total: { type: 'number' },
|
|
1127
|
+
windowEvents: { type: 'number' },
|
|
1128
|
+
badLines: { type: 'number' },
|
|
1129
|
+
events: {
|
|
1130
|
+
type: 'array',
|
|
1131
|
+
items: objSchema({
|
|
1132
|
+
at: { type: 'string' },
|
|
1133
|
+
event: { type: 'string' },
|
|
1134
|
+
params: { type: 'string' },
|
|
1135
|
+
}, ['at', 'event', 'params']),
|
|
1136
|
+
},
|
|
1137
|
+
dropped: { type: 'number' },
|
|
1138
|
+
kept: { type: 'number' },
|
|
1139
|
+
windowDays: { type: 'number' },
|
|
1140
|
+
at: { type: 'string' },
|
|
1141
|
+
name: { type: 'string' },
|
|
1142
|
+
line: { type: 'string' },
|
|
1143
|
+
}, ['action', 'present', 'presence', 'rulesVersion', 'words', 'text', 'emptyReason', 'total', 'windowEvents', 'badLines', 'events', 'dropped', 'kept', 'windowDays', 'at', 'name', 'line']),
|
|
1144
|
+
},
|
|
1145
|
+
async run(args, env) {
|
|
1146
|
+
const cfg = configFor(env.workspace, env.sessionId);
|
|
1147
|
+
const action = String(args.action);
|
|
1148
|
+
const empty = {
|
|
1149
|
+
action, present: false, presence: 'unknown', rulesVersion: '', words: 0, text: '', emptyReason: 'no-log',
|
|
1150
|
+
total: 0, windowEvents: 0, badLines: 0, events: [],
|
|
1151
|
+
dropped: 0, kept: 0, windowDays: cfg.sceneWindowDays, at: '', name: '', line: '',
|
|
1152
|
+
};
|
|
1153
|
+
if (action === 'append') {
|
|
1154
|
+
const name = String(args.event ?? '').trim();
|
|
1155
|
+
if (!name)
|
|
1156
|
+
throw new StateError('append 需要 event(事件名)');
|
|
1157
|
+
const params = {};
|
|
1158
|
+
for (const item of Array.isArray(args.params) ? args.params : []) {
|
|
1159
|
+
const raw = String(item ?? '');
|
|
1160
|
+
const eq = raw.indexOf('=');
|
|
1161
|
+
if (eq <= 0)
|
|
1162
|
+
throw new StateError(`参数写法不对:${raw}`, '形如 place=书房;值里有空格就写 place="书房 阳台"');
|
|
1163
|
+
const key = raw.slice(0, eq).trim();
|
|
1164
|
+
let value = raw.slice(eq + 1).trim();
|
|
1165
|
+
if (value.startsWith('"') && value.endsWith('"') && value.length >= 2)
|
|
1166
|
+
value = value.slice(1, -1);
|
|
1167
|
+
if (key in params)
|
|
1168
|
+
throw new StateError(`参数重复:${key}`);
|
|
1169
|
+
params[key] = value;
|
|
1170
|
+
}
|
|
1171
|
+
const input = { name, params, at: args.at ? String(args.at) : undefined };
|
|
1172
|
+
// 先校验再建库:写错的事件不该在磁盘上留下一个刚初始化好的笔记本
|
|
1173
|
+
checkAppendInput(input);
|
|
1174
|
+
// 与其它写工具一致:第一个事件顺手把笔记本建好(否则只多一个没有 .zk 的 .state/)
|
|
1175
|
+
await ensureNotebook(cfg, env.signal);
|
|
1176
|
+
const written = appendEvent(cfg, input);
|
|
1177
|
+
return {
|
|
1178
|
+
...empty,
|
|
1179
|
+
at: written.at,
|
|
1180
|
+
name: written.name,
|
|
1181
|
+
line: written.line,
|
|
1182
|
+
events: [{ at: written.at, event: written.name, params: paramsText(written.params) }],
|
|
1183
|
+
};
|
|
1184
|
+
}
|
|
1185
|
+
if (action === 'show') {
|
|
1186
|
+
const log = readStateLog(cfg, { windowDays: cfg.sceneWindowDays });
|
|
1187
|
+
const scene = buildScene(cfg);
|
|
1188
|
+
return {
|
|
1189
|
+
...empty,
|
|
1190
|
+
present: scene.present,
|
|
1191
|
+
presence: scene.presence,
|
|
1192
|
+
rulesVersion: scene.rulesVersion,
|
|
1193
|
+
words: scene.words,
|
|
1194
|
+
text: scene.text,
|
|
1195
|
+
emptyReason: log.readError ? 'error' : scene.emptyReason,
|
|
1196
|
+
total: log.total,
|
|
1197
|
+
windowEvents: log.events.length,
|
|
1198
|
+
badLines: log.badLines.length,
|
|
1199
|
+
events: log.events.slice(-20).reverse().map((e) => ({ at: e.at, event: e.name, params: paramsText(e.params) })),
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
if (action === 'prune') {
|
|
1203
|
+
const days = Number(args.days ?? cfg.sceneWindowDays);
|
|
1204
|
+
const r = pruneStateLog(cfg, { days, force: args.force === true, windowDays: cfg.sceneWindowDays });
|
|
1205
|
+
return { ...empty, dropped: r.dropped, kept: r.kept, windowDays: r.windowDays };
|
|
1206
|
+
}
|
|
1207
|
+
throw new StateError(`不认识的 action:${action}`, '只能是 append / show / prune');
|
|
1208
|
+
},
|
|
1209
|
+
render(_args, value) {
|
|
1210
|
+
const v = value;
|
|
1211
|
+
if (v.action === 'append')
|
|
1212
|
+
return text(`已记录:${v.line}\n(.state/events.log;场景下一轮装配时重算)`);
|
|
1213
|
+
if (v.action === 'prune') {
|
|
1214
|
+
return text(`已压实 .state/events.log:删除 ${v.dropped} 条,保留 ${v.kept} 条(窗口 ${v.windowDays} 天)。\n没有自动 commit —— 删除证据在 git diff 里,由人决定何时提交。`);
|
|
1215
|
+
}
|
|
1216
|
+
const presence = v.presence === 'present' ? '在场' : v.presence === 'absent' ? '不在' : '未知';
|
|
1217
|
+
const emptyText = v.emptyReason === 'budget'
|
|
1218
|
+
? '(有事件,但场景预算一句都放不下:调大 sceneBudgetWords)'
|
|
1219
|
+
: v.emptyReason === 'rules'
|
|
1220
|
+
? '(规则没算出可展示的值:energy / mood / satiety / 地点 / 目标 都是 null)'
|
|
1221
|
+
: v.emptyReason === 'error'
|
|
1222
|
+
? '(场景读取失败:看 doctor / 设置页)'
|
|
1223
|
+
: '(没有可投影的场景:日志为空或窗口内没有事件)';
|
|
1224
|
+
const lines = [
|
|
1225
|
+
v.text || emptyText,
|
|
1226
|
+
`规则 ${v.rulesVersion || 'default'} · 窗口内 ${v.windowEvents} 条 · 历史共 ${v.total} 条` +
|
|
1227
|
+
(v.badLines > 0 ? ` · 坏行 ${v.badLines}` : '') + ` · 用户${presence}`,
|
|
1228
|
+
];
|
|
1229
|
+
if (v.events.length) {
|
|
1230
|
+
lines.push('最近事件(新 → 旧,最多 20 条):');
|
|
1231
|
+
for (const e of v.events)
|
|
1232
|
+
lines.push(` ${e.at} ${e.event}${e.params ? ' ' + e.params : ''}`);
|
|
1233
|
+
}
|
|
1234
|
+
return text(lines.join('\n'));
|
|
1037
1235
|
},
|
|
1038
1236
|
}),
|
|
1039
1237
|
t({
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
* 首行 `<!-- zdiary-template: vN -->` 是版本标记:lint 据此报告「老库的 AGENTS.md 是旧版」,
|
|
18
18
|
* 只报告不覆盖(模板允许用户改)。
|
|
19
19
|
*/
|
|
20
|
-
/** 模板版本:改模板就 +1;lint 用它判断老库是否还在用旧契约(v2:去掉 inbox/;v3:截断改按日期;v4:去掉「宿主不会自动加载」一句;v5:MEMORY 条目补「先 zk_read
|
|
21
|
-
export const TEMPLATE_VERSION =
|
|
20
|
+
/** 模板版本:改模板就 +1;lint 用它判断老库是否还在用旧契约(v2:去掉 inbox/;v3:截断改按日期;v4:去掉「宿主不会自动加载」一句;v5:MEMORY 条目补「先 zk_read 核对来源」;v6:补状态层一句) */
|
|
21
|
+
export const TEMPLATE_VERSION = 6;
|
|
22
22
|
/** 首行版本标记(实测 4 词);不许带日期 —— 带日期的写法实测 6 词,白花 2 词 */
|
|
23
23
|
export const TEMPLATE_MARK = `<!-- zdiary-template: v${TEMPLATE_VERSION} -->`;
|
|
24
24
|
/**
|
|
@@ -32,6 +32,7 @@ export const TEMPLATE_MARK = `<!-- zdiary-template: v${TEMPLATE_VERSION} -->`;
|
|
|
32
32
|
export const TEMPLATE_SENTINELS = {
|
|
33
33
|
4: { forbidden: ['DSH 不会自动加载', '从底部往前丢'] },
|
|
34
34
|
5: { required: ['先 zk_read 核对来源'], forbidden: ['DSH 不会自动加载', '从底部往前丢'] },
|
|
35
|
+
6: { required: ['先 zk_read 核对来源', 'zk_state'], forbidden: ['DSH 不会自动加载', '从底部往前丢'] },
|
|
35
36
|
};
|
|
36
37
|
export const AGENTS_TEMPLATE = `${TEMPLATE_MARK}
|
|
37
38
|
|
|
@@ -59,6 +60,10 @@ notes/{concepts,entities,patterns}/ 原子笔记 · archive/ 废弃(不删)
|
|
|
59
60
|
每周:DREAMS.md 写「上次做梦:YYYY-MM-DD」;到期 zk_lint → zk_dream → 人裁决改 MEMORY。
|
|
60
61
|
MEMORY.md 每条:结论(YYYY-MM-DD)[[来源路径]],先 zk_read 核对来源;退役写 - [superseded YYYY-MM-DD] 旧结论 → [[新结论]],行不删。
|
|
61
62
|
新条目写顶部;超预算按日期丢最旧的一条。
|
|
63
|
+
|
|
64
|
+
## 状态
|
|
65
|
+
|
|
66
|
+
用 zk_state 记事件,不翻历史。
|
|
62
67
|
`;
|
|
63
68
|
/**
|
|
64
69
|
* flat 结构用的模板:不出现 raw / inbox / archive / WORKFLOW.md 这些 flat 没有的路径。
|
|
@@ -78,6 +83,10 @@ export const AGENTS_TEMPLATE_FLAT = `${TEMPLATE_MARK}
|
|
|
78
83
|
|
|
79
84
|
DREAMS.md 写「上次做梦:YYYY-MM-DD」;到期 zk_lint → zk_dream → 人裁决改 MEMORY。
|
|
80
85
|
MEMORY.md 每条:结论(YYYY-MM-DD)[[来源路径]],先 zk_read 核对来源;退役写 - [superseded] 旧结论 → [[新结论]]。
|
|
86
|
+
|
|
87
|
+
## 状态
|
|
88
|
+
|
|
89
|
+
用 zk_state 记事件,不翻历史。
|
|
81
90
|
`;
|
|
82
91
|
/**
|
|
83
92
|
* 根目录 WORKFLOW.md:只在「要动已有笔记」时才需要读,所以不注入、不计预算。
|
package/dist/lib/lint.js
CHANGED
|
@@ -487,13 +487,15 @@ function sentinelIssue(body, rule) {
|
|
|
487
487
|
if (!rule)
|
|
488
488
|
return null;
|
|
489
489
|
const quote = (texts) => texts.map((text) => `「${showSentinel(text)}」`).join('、');
|
|
490
|
+
// 两件事一起报:只报「缺了什么」会让「还留着旧句」被藏起来,人改完一处又撞一次
|
|
491
|
+
const parts = [];
|
|
490
492
|
const missing = (rule.required ?? []).filter((text) => !body.includes(text));
|
|
491
493
|
if (missing.length > 0)
|
|
492
|
-
|
|
494
|
+
parts.push(`缺少当前版本才有的内容:${quote(missing)}`);
|
|
493
495
|
const leftover = (rule.forbidden ?? []).filter((text) => body.includes(text));
|
|
494
496
|
if (leftover.length > 0)
|
|
495
|
-
|
|
496
|
-
return null;
|
|
497
|
+
parts.push(`仍含旧版已移除的内容:${quote(leftover)}`);
|
|
498
|
+
return parts.length > 0 ? parts.join(';') : null;
|
|
497
499
|
}
|
|
498
500
|
/**
|
|
499
501
|
* 契约体检(M1):缺文件 = 未启用(不算错,但要让 zk_lint 可见);
|
package/dist/lib/memory.js
CHANGED
|
@@ -251,7 +251,7 @@ function escapeFrame(text) {
|
|
|
251
251
|
// 文件内容不许提前闭合我们自己的 <system-reminder> 框
|
|
252
252
|
return text.replaceAll('</system-reminder>', '<\\/system-reminder>');
|
|
253
253
|
}
|
|
254
|
-
/**
|
|
254
|
+
/** 载荷框:定义成常量,最终拼装与各处词数统计才能共用同一份文字(否则数出来的词对不上) */
|
|
255
255
|
const FRAME_HEAD = '<system-reminder>\n' +
|
|
256
256
|
'以下内容由 zdiary 从知识库根目录读出,是长期约定与记忆;工作约定放在 AGENTS.md,不要写进 MEMORY.md。\n\n';
|
|
257
257
|
const FRAME_TAIL = '\n</system-reminder>';
|
|
@@ -296,8 +296,9 @@ function payloadKeyOf(cfg) {
|
|
|
296
296
|
// 做梦提醒按本地日历天变化(「3 天前」→「4 天前」):文件没动也得跟着日期失效
|
|
297
297
|
const now = new Date();
|
|
298
298
|
const day = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
|
|
299
|
-
//
|
|
300
|
-
|
|
299
|
+
// 影响这个载荷的预算都要进键:改配置不重新写入任何文件,漏一个就会拿到旧预算的载荷。
|
|
300
|
+
// totalPromptBudgetWords 不进:它是 memory + scene 的合并总账,改它不改变 payload。
|
|
301
|
+
return `${cfg.notebook}|${cfg.memoryBudgetWords}|${cfg.contractBudgetWords}|${day}|${stamps}`;
|
|
301
302
|
}
|
|
302
303
|
/**
|
|
303
304
|
* 生成注入载荷。纯读文件,不调 zk、不建目录:笔记本不存在时返回空载荷。
|
|
@@ -455,7 +456,8 @@ function computePayload(cfg) {
|
|
|
455
456
|
notice;
|
|
456
457
|
}
|
|
457
458
|
// ── 契约段(角色):永远注入、在记忆段之前、不参与截断 ────
|
|
458
|
-
//
|
|
459
|
+
// 超预算只告警不静默丢。合并预算(记忆载荷 + 场景)不在这里量:
|
|
460
|
+
// memory 看不到场景,硬算会得到一个少了一截的「总 prompt」—— 那是错的。
|
|
459
461
|
// 没有契约文件 = 未启用:不多一个词,0.9 的老库逐字节一致。
|
|
460
462
|
const contractBudget = cfg.contractBudgetWords;
|
|
461
463
|
const contracts = readContracts(cfg);
|
|
@@ -468,16 +470,6 @@ function computePayload(cfg) {
|
|
|
468
470
|
if (contractOver) {
|
|
469
471
|
contractNotes.push(`契约预算 ${contractBudget} 词:已用 ${contractContentWords},超出 ${contractContentWords - contractBudget} 词(契约不截断)`);
|
|
470
472
|
}
|
|
471
|
-
if (contractBodies.length > 0) {
|
|
472
|
-
// 总 prompt 用「还没加契约告警」的完整载荷来量:框 / 做梦提醒 / 记忆 notice 都要算,
|
|
473
|
-
// 但本条告警自己不能算进自己报的数里(否则又是一个自指的口径)。
|
|
474
|
-
const provisional = frameText((contractBodyText ? contractBodyText + '\n\n' : '') + memoryInner);
|
|
475
|
-
const promptTotal = countWords(provisional);
|
|
476
|
-
if (promptTotal > cfg.totalPromptBudgetWords) {
|
|
477
|
-
const overhead = Math.max(0, promptTotal - contractContentWords - finalWords);
|
|
478
|
-
contractNotes.push(`总 prompt 预算 ${cfg.totalPromptBudgetWords} 词:契约 ${contractContentWords} + 记忆 ${finalWords} + 框与提示 ${overhead} = ${promptTotal},超出 ${promptTotal - cfg.totalPromptBudgetWords}`);
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
473
|
const contractText = contractBodyText + (contractNotes.length > 0 ? `\n\n(${contractNotes.join(';')})` : '');
|
|
482
474
|
const contractWords = countWords(contractText);
|
|
483
475
|
let text = '';
|
|
@@ -486,7 +478,6 @@ function computePayload(cfg) {
|
|
|
486
478
|
}
|
|
487
479
|
// 模型真正收到的总量与开销:口径与 memoryUsage / D9 的载荷预览同源
|
|
488
480
|
const totalWords = countWords(text);
|
|
489
|
-
const sceneWords = 0; // M1 只落形状:M3 起由 STATE 填
|
|
490
481
|
return {
|
|
491
482
|
text,
|
|
492
483
|
words: finalWords,
|
|
@@ -513,9 +504,8 @@ function computePayload(cfg) {
|
|
|
513
504
|
overWords: Math.max(0, contractContentWords - contractBudget),
|
|
514
505
|
files: contracts.map((f) => ({ name: f.name, present: f.body.length > 0, words: countWords(f.body) })),
|
|
515
506
|
},
|
|
516
|
-
scene: { present: false, expired: false, words: sceneWords },
|
|
517
507
|
totalWords,
|
|
518
|
-
overheadWords: Math.max(0, totalWords - contractContentWords - finalWords
|
|
508
|
+
overheadWords: Math.max(0, totalWords - contractContentWords - finalWords),
|
|
519
509
|
};
|
|
520
510
|
}
|
|
521
511
|
/**
|