work-rhythm 0.2.20 → 0.2.22
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/.agents/notes/AGENTS.md +10 -3
- package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md +1 -0
- package/.agents/notes/decisions/2026-11-30-skill-ships-with-its-template-in-a-zip.md +1 -0
- package/.agents/notes/decisions/2026-12-01-agent-binding-asks-for-model-and-effort.md +1 -0
- package/.agents/notes/decisions/2026-12-01-interjections-batch-into-one-answer-and-interrupt-reruns.md +1 -0
- package/.agents/notes/decisions/2026-12-01-question-cards-via-cli-tool-on-todays-page-answers-return-to-router.md +4 -0
- package/.agents/notes/decisions/2026-12-01-router-and-worker-pass-content-in-handoff-docs.md +1 -0
- package/.agents/notes/decisions/2026-12-01-router-runs-on-a-flash-model-with-low-effort.md +1 -0
- package/.agents/notes/decisions/2026-12-01-sync-feeds-refine-then-events-to-the-router.md +4 -0
- package/.agents/notes/decisions/2026-12-03-codex-cli-joins-as-a-preset-verified-against-its-own-binary.md +45 -0
- package/.agents/notes/decisions/2026-12-03-fewer-scheduled-tasks-silent-refine-no-question-cards.md +46 -0
- package/.agents/notes/decisions/2026-12-03-playbook-distills-handoffs-into-ws-tool-and-skill-know-how.md +43 -0
- package/.agents/notes/decisions/2026-12-03-related-work-comes-back-as-one-reply.md +49 -0
- package/.agents/notes/decisions/2026-12-03-roster-of-worker-groups-router-picks-the-group.md +33 -0
- package/.agents/notes/decisions/2026-12-03-router-model-and-effort-are-configurable.md +28 -0
- package/.agents/notes/decisions/2026-12-03-router-only-direct-mode-removed.md +27 -0
- package/.agents/notes/decisions/2026-12-03-unattended-runs-decide-on-their-own.md +32 -0
- package/.agents/notes/decisions/2026-12-03-update-upgrades-package-and-refreshes-shipped-files.md +30 -0
- package/.agents/notes/decisions/2026-12-03-workers-leave-a-status-trail-that-joins-the-receipt.md +31 -0
- package/.agents/notes/pitfalls/2026-12-01-replace-string-dollar-quote-expands.md +2 -0
- package/.agents/notes/pitfalls/2026-12-03-judging-a-symlink-by-realpath-ignores-parent-links.md +18 -0
- package/.agents/skills/work-rhythm/SKILL.md +21 -17
- package/README.md +13 -10
- package/collectors/local.env.example +31 -36
- package/docs/agents.md +61 -60
- package/docs/channels.md +15 -19
- package/docs/formats.md +28 -1
- package/docs/router-worker.md +67 -54
- package/docs/scheduled-tasks.md +14 -14
- package/docs/sources.md +5 -6
- package/package.json +1 -1
- package/scripts/cli.js +9 -5
- package/scripts/cmd/agent.js +348 -235
- package/scripts/cmd/ask.js +18 -76
- package/scripts/cmd/check.js +14 -7
- package/scripts/cmd/listen.js +33 -32
- package/scripts/cmd/pack.js +6 -6
- package/scripts/cmd/rhythm.js +13 -10
- package/scripts/cmd/trigger.js +23 -14
- package/scripts/cmd/update.js +92 -0
- package/scripts/lib/agent.js +235 -142
- package/scripts/lib/agents.js +179 -0
- package/scripts/lib/bot.js +44 -263
- package/scripts/lib/engine.js +345 -170
- package/scripts/lib/handoff.js +54 -8
- package/scripts/lib/markdown.js +19 -0
- package/scripts/lib/playbook.js +123 -0
- package/scripts/lib/rhythm.js +402 -260
- package/scripts/lib/router.js +52 -46
- package/scripts/lib/template.js +3 -3
- package/scripts/lib/update.js +279 -0
- package/scripts/lib/view.js +7 -152
- package/scripts/lib/worker.js +11 -5
- package/scripts/test/agent.test.js +410 -257
- package/scripts/test/check.test.js +17 -6
- package/scripts/test/harness.js +5 -2
- package/scripts/test/listen.test.js +190 -158
- package/scripts/test/playbook.test.js +106 -0
- package/scripts/test/rhythm.test.js +167 -109
- package/scripts/test/roster.test.js +155 -0
- package/scripts/test/router-helper.js +17 -2
- package/scripts/test/router.test.js +499 -414
- package/scripts/test/stubs.js +51 -4
- package/scripts/test/timeout.test.js +160 -59
- package/scripts/test/update.test.js +384 -0
- package/tasks/daily-persona.md +30 -13
- package/tasks/daily-plan.md +4 -3
- package/tasks/evening-review.md +1 -2
- package/tasks/noon-checkin.md +2 -1
- package/templates/00-config/profile.example.yaml +17 -13
- package/templates/04-patterns/PERSONA.md +4 -1
- package/templates/04-patterns/PLAYBOOK.md +13 -0
- package/templates/AGENTS.md +7 -3
- package/templates/README.md +10 -9
package/scripts/lib/router.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
// 路由:一个常驻的 agent 会话,收到一批触发,回一批决定(docs/router-worker.md 的「路由」)。
|
|
2
|
-
// 只读,不查数据不改文件,给用户的话也只是写出来、由 CLI 发。会话就是绑定的那个(YZJ_ROUTER_SESSION
|
|
2
|
+
// 只读,不查数据不改文件,给用户的话也只是写出来、由 CLI 发。会话就是绑定的那个(YZJ_ROUTER_SESSION,没写时看 YZJ_BOT_SESSION),
|
|
3
3
|
// 它记得桌面上聊过什么;没绑、或者绑的接不上,用它自己的专属会话(01-inbox/_bot/session-id)。
|
|
4
4
|
// 它回一段 Markdown:先列决定行(「- t-000123 dispatch」),要文字的决定各给一个小节(「## handoff t-000123」),解析在 handoff.js。
|
|
5
|
+
// 用哪个 agent、模型、强度由名册定(agents.js):模型默认是 flash 这类快模型、强度默认 low,都可以换。
|
|
5
6
|
'use strict';
|
|
6
7
|
const fs = require('node:fs');
|
|
7
8
|
const path = require('node:path');
|
|
8
|
-
const { createAgent
|
|
9
|
+
const { createAgent } = require('./agent');
|
|
9
10
|
const { fmtStamp } = require('./run');
|
|
10
11
|
const handoff = require('./handoff');
|
|
12
|
+
const playbook = require('./playbook');
|
|
11
13
|
|
|
12
14
|
const PROMPT = `${handoff.MARKER}下面是到达的触发,前面是「手头的事」。
|
|
13
15
|
|
|
@@ -16,17 +18,17 @@ const PROMPT = `${handoff.MARKER}下面是到达的触发,前面是「手头
|
|
|
16
18
|
一、每种触发默认怎么判
|
|
17
19
|
- 来源=message(用户在 IM 里刚说的话):要么 reply(一句话能答的),要么 dispatch(要动手的)。必须有回音,不能不理。
|
|
18
20
|
- 来源=manual(用户从命令行塞进来的一句话):同 message。
|
|
19
|
-
- 来源=schedule(用户自己定的定时任务):一律 dispatch,不用写任务书(CLI
|
|
21
|
+
- 来源=schedule(用户自己定的定时任务):一律 dispatch,不用写任务书(CLI 直接用指令文件),可以用 worker= 挑一组执行者。只有同一件事今天刚做过才 ignore。
|
|
20
22
|
- 来源=report(执行者做完了):读回执,写 notify 告诉用户结果;失败了说清什么没做成、为什么;想让它重做就 dispatch 并写任务书。
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
23
|
+
- 同一轮里要给用户的话有好几条(几件汇报一起回来,或者汇报和回答同时有):CLI 会把它们合成一条消息发出。每条只说这件事本身,开头不要寒暄、不要总评;想把几件汇报道成一段的,写在其中一条的 notify 里,其余几条给 ignore。
|
|
24
|
+
- 用户问某件在跑的事做到哪了(「怎么样了」「卡住了吗」):「手头的事」里在跑的那件后面带着它最近一步(执行者边做边记的)。用 reply 直接答:做到哪了、多久没动静;不要为此派人,也不要 interrupt 它。
|
|
25
|
+
- 时间:今天是 {今天}。用户说的「明早」「今晚」「下周一」「月底」这类相对时间,任务书里一律换算成具体日期(和几点,说清了的话),不要把原词照抄给执行者。
|
|
24
26
|
|
|
25
27
|
二、七种决定(每条触发只能选一种)
|
|
26
|
-
- dispatch:派人做。要写一个「## handoff
|
|
28
|
+
- dispatch:派人做。要写一个「## handoff <短号>」小节当任务书。日常事在工作目录跑;明确要在某个代码库/工程里干活、且能从「可用工作区」里挑出时,写成「- <短号> dispatch ws=<id>」。前面列了「可用执行者」时,还可以加 worker=<编号> 挑一组(按事挑:要改文件、要写整份计划这类重的派给强度高的;只是记一条、转述一句的派给快而省的;一组排满了、别组也做得了的就让开);拿不准就不写,派给第一组。如果必须确定工作区但拿不准,写成 reply,问清楚是哪个工作区(把候选的列出来),我再派人。
|
|
27
29
|
- reply:不用动手、一句话直接答。写「## reply <短号>」。只用于 message / manual。
|
|
28
30
|
- ignore:不用处理。写「## ignore <短号>」和一句原因。
|
|
29
|
-
- notify:给用户的话。写「## notify <短号>」。只用于 report
|
|
31
|
+
- notify:给用户的话。写「## notify <短号>」。只用于 report。静默的任务(提炼、quiet 的定时任务)不会有汇报。
|
|
30
32
|
- continue <短号>:这句话是接着「手头的事」里某一件做完的事继续追问,或者顺便再做一件相关事(不打断正在跑的,排在它后面单独回一次)。不用写任务书,CLI 会把原话交给原来那个执行者。
|
|
31
33
|
- interrupt <短号>:某件事还在跑,用户又补充了条件、想法或改了要求,希望合在一起只回一次。CLI 会停掉它、带着新话重跑并压掉上一轮汇报。正在跑的事收到补充时优先选 interrupt,不要选 continue。
|
|
32
34
|
- stop <短号>:用户不要某件正在跑的事了。
|
|
@@ -47,7 +49,7 @@ const PROMPT = `${handoff.MARKER}下面是到达的触发,前面是「手头
|
|
|
47
49
|
给人类在 IM 里看的回复:简洁明了,直说结论,不铺垫、不啰嗦……
|
|
48
50
|
|
|
49
51
|
## notify <短号>
|
|
50
|
-
给人类在 IM
|
|
52
|
+
给人类在 IM 里看的话:纯文本、不用表格、不放链接。务必简洁明了、说清结论——先给结论和要做的事,去掉背景铺垫、内部过程和重复解释;一条一件事,以一眼看懂为准。
|
|
51
53
|
|
|
52
54
|
五、硬规则
|
|
53
55
|
1. 每条触发都要有、且只有一个决定,写在最前面的「决定:」下面。
|
|
@@ -66,56 +68,67 @@ const readText = (file) => {
|
|
|
66
68
|
}
|
|
67
69
|
};
|
|
68
70
|
|
|
71
|
+
// 「绑定的会话接不上过」记在这个文件里(内容是那个会话 id):listen handle 是一次一个进程,光靠内存里的标记下一句又会去试
|
|
72
|
+
const lostFile = (root) => path.join(root, '01-inbox', '_bot', 'bound-session-lost');
|
|
73
|
+
|
|
74
|
+
// 换绑了会话之后调:把「接不上过」的记录清掉,下一句又从绑的那个会话开始(work-rhythm agent session、bind router 用)
|
|
75
|
+
function forgetBoundLost(root) {
|
|
76
|
+
try {
|
|
77
|
+
fs.unlinkSync(lostFile(root));
|
|
78
|
+
} catch {
|
|
79
|
+
// 本来就没有
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
69
83
|
// 一条触发在提示词里的样子:短的全带上,长的只给路径和开头
|
|
70
84
|
function triggerLine(t) {
|
|
71
85
|
const when = short(t.at);
|
|
72
86
|
if (t.kind === 'message') return `[${t.id}] 来源=message 时间=${when} 原话:${t.text}`;
|
|
73
87
|
if (t.kind === 'manual') return `[${t.id}] 来源=manual 时间=${when} 原话:${t.text}`;
|
|
74
88
|
if (t.kind === 'schedule') return `[${t.id}] 来源=schedule 时间=${when} 任务=${t.task} 指令=${t.taskFile}${t.taskTitle ? `(${t.taskTitle})` : ''}`;
|
|
75
|
-
if (t.kind === 'events') {
|
|
76
|
-
const health = (t.sources || []).map((x) => `${x.id} ${x.ok ? '成功' : `失败:${x.detail || `退出码 ${x.code}`}`}`).join('、');
|
|
77
|
-
const head = `[${t.id}] 来源=events 时间=${when} ${t.ok ? `新事件 ${(t.events || []).length} 条` : `提炼没成:${t.reason}`};这次采集:${health || '(没跑)'}`;
|
|
78
|
-
const list = (t.events || []).slice(0, 40).map((e) => ` - ${e.time} ${e.type} ${e.item}(${[e.source, e.project, e.ref].filter(Boolean).join(',')})`);
|
|
79
|
-
return [head, ...list].join('\n');
|
|
80
|
-
}
|
|
81
|
-
if (t.kind === 'answer') {
|
|
82
|
-
const a = t.ask || {};
|
|
83
|
-
const ans = a.answer || {};
|
|
84
|
-
return `[${t.id}] 来源=answer 时间=${when} 卡片=${a.id || '(老卡片)'}${t.of ? ` 原执行者=${t.of}` : ''} 问题:「${a.question || a.item || ''}」 回答:「${ans.choice || ''}」${ans.note ? ` 附言:「${ans.note}」` : ''}${a.record ? ` 记到:${a.record}` : ''}`;
|
|
85
|
-
}
|
|
86
89
|
const head = `[${t.id}] 来源=report 时间=${when} 对应=${t.of} 结果=${t.ok ? '成功' : '失败'}${t.ok ? ` 用时=${t.seconds}s` : ` 原因=${t.reason}`} 回执=${handoff.relResult(t.of)}${t.quiet ? '(不推送)' : ''}`;
|
|
87
90
|
return t.ok ? `${head}\n 开头:${t.opening}` : head;
|
|
88
91
|
}
|
|
89
92
|
|
|
90
|
-
// 一轮的提示词:固定的说明 + 可用工作区(有的话)+ 手头的事 + 这一批触发。extra
|
|
91
|
-
|
|
92
|
-
|
|
93
|
+
// 一轮的提示词:固定的说明 + 可用工作区(有的话)+ 可用的几组执行者(不止一组时才列)+ 手头的事 + 这一批触发。extra 是重试时的一句提醒。
|
|
94
|
+
// workers 是 [{ id, agent, model, effort, count, running, queued }]
|
|
95
|
+
function turnPrompt(triggers, open, extra = '', workspaces = null, now = new Date(), workers = [], playbookRows = []) {
|
|
96
|
+
const WEEK = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
|
97
|
+
const p2 = (n) => String(n).padStart(2, '0');
|
|
98
|
+
const todayText = `${now.getFullYear()}-${p2(now.getMonth() + 1)}-${p2(now.getDate())} ${WEEK[now.getDay()]},现在 ${p2(now.getHours())}:${p2(now.getMinutes())}`;
|
|
99
|
+
const lines = [PROMPT.split('{今天}').join(todayText)];
|
|
93
100
|
if (extra) lines.push('', extra);
|
|
94
101
|
const reg = (workspaces && workspaces.registered) || [];
|
|
95
102
|
const scan = (workspaces && workspaces.scanned) || [];
|
|
96
103
|
if (reg.length || scan.length) {
|
|
97
|
-
lines.push('', '可用工作区(日常事默认在工作目录跑,不写 ws=;明确要进代码库/工程时在 dispatch 行加 ws=<id
|
|
104
|
+
lines.push('', '可用工作区(日常事默认在工作目录跑,不写 ws=;明确要进代码库/工程时在 dispatch 行加 ws=<id>;拿不准是哪个就 reply 问我,把候选列出来):');
|
|
98
105
|
for (const w of reg) lines.push(`- ${w.id}(已登记,${w.path})${w.note ? `:${w.note}` : ''}`);
|
|
99
106
|
for (const w of scan) lines.push(`- ${w.id}(候选,${w.path})`);
|
|
100
107
|
}
|
|
108
|
+
if (workers.length > 1) {
|
|
109
|
+
lines.push('', '可用执行者(dispatch 时可以加 worker=<编号>,不写就派给第一组):');
|
|
110
|
+
for (const w of workers) lines.push(`- ${w.id} ${w.agent}${w.model ? ` · ${w.model}` : ''}${w.effort ? ` · ${w.effort}` : ''} · 同时 ${w.count} 个(在跑 ${w.running},排队 ${w.queued})`);
|
|
111
|
+
}
|
|
112
|
+
if (playbookRows && playbookRows.length) {
|
|
113
|
+
lines.push('', '现场手册(以前做同类活留下的经验:照它选 ws=、在任务书里写明用哪个工具或 skill):', ...playbookRows);
|
|
114
|
+
}
|
|
101
115
|
lines.push('', '手头的事(在跑的和今天做完的):');
|
|
102
116
|
if (!open.length) lines.push('- (没有)');
|
|
103
|
-
for (const o of open) lines.push(`- ${o.id} ${o.status},${short(o.at)}:${o.title || '(没写标题)'}${o.ws ? ` [ws=${o.ws}]` : ''}`);
|
|
117
|
+
for (const o of open) lines.push(`- ${o.id} ${o.status},${short(o.at)}:${o.title || '(没写标题)'}${o.ws ? ` [ws=${o.ws}]` : ''}${o.step ? `|最近一步:${o.step}` : ''}`);
|
|
104
118
|
lines.push('', '触发:');
|
|
105
119
|
for (const t of triggers) lines.push(triggerLine(t));
|
|
106
120
|
return lines.join('\n');
|
|
107
121
|
}
|
|
108
122
|
|
|
109
|
-
// role
|
|
123
|
+
// role 是名册里路由那一处加上会话:{ agent, model, effort, session }。effort 是实际用的强度(agents.routerEffort 算好的:没写是 low,agent 不认强度的是空)
|
|
110
124
|
function createRouter({ root, env, role, timeoutS = 120, preamble = '', log = () => {} }) {
|
|
111
125
|
const STATE_DIR = path.join(root, '01-inbox', '_bot');
|
|
112
126
|
const SESSION_FILE = path.join(STATE_DIR, 'session-id');
|
|
113
|
-
const LOST_FILE =
|
|
127
|
+
const LOST_FILE = lostFile(root);
|
|
114
128
|
// 绑定的会话接不上过一次就不再试,用专属会话;「接不上过」记在 bound-session-lost 里(换绑或 /new 时清掉)
|
|
115
129
|
const BOUND = role.session;
|
|
116
130
|
let boundLost = Boolean(BOUND) && readText(LOST_FILE).trim() === BOUND;
|
|
117
|
-
|
|
118
|
-
const agent = createAgent({ root, agent: role.agent, mode: 'plan', timeoutS, preamble, log, env, model: role.model, effort: ROUTER_EFFORT });
|
|
131
|
+
const agent = createAgent({ root, agent: role.agent, mode: 'plan', timeoutS, preamble, log, env, model: role.model, effort: role.effort });
|
|
119
132
|
let last = null;
|
|
120
133
|
|
|
121
134
|
const startSession = () => (BOUND && !boundLost ? BOUND : readText(SESSION_FILE).trim());
|
|
@@ -128,30 +141,24 @@ function createRouter({ root, env, role, timeoutS = 120, preamble = '', log = ()
|
|
|
128
141
|
// 记不下来就只在这次进程里有效
|
|
129
142
|
}
|
|
130
143
|
}
|
|
131
|
-
|
|
132
|
-
|
|
144
|
+
// /new:丢掉专属会话;绑了会话的,下一句又从绑的那个开始
|
|
145
|
+
function resetSession() {
|
|
133
146
|
try {
|
|
134
|
-
fs.unlinkSync(
|
|
147
|
+
fs.unlinkSync(SESSION_FILE);
|
|
135
148
|
} catch {
|
|
136
149
|
// 本来就没有
|
|
137
150
|
}
|
|
151
|
+
boundLost = false;
|
|
152
|
+
forgetBoundLost(root);
|
|
138
153
|
}
|
|
139
|
-
|
|
154
|
+
const sessionLost = (used) => {
|
|
155
|
+
if (used && used === BOUND) markBoundLost();
|
|
140
156
|
try {
|
|
141
157
|
fs.unlinkSync(SESSION_FILE);
|
|
142
158
|
} catch {
|
|
143
159
|
// 本来就没有
|
|
144
160
|
}
|
|
145
|
-
}
|
|
146
|
-
const sessionLost = (used) => {
|
|
147
|
-
if (used && used === BOUND) markBoundLost();
|
|
148
|
-
clearSession();
|
|
149
161
|
};
|
|
150
|
-
// /new:丢掉专属会话;绑了会话的,下一句又从绑的那个开始
|
|
151
|
-
function resetSession() {
|
|
152
|
-
clearSession();
|
|
153
|
-
if (BOUND) forgetBoundLost();
|
|
154
|
-
}
|
|
155
162
|
|
|
156
163
|
// 跑一轮。返回 { reply } 或 { failed, config? }
|
|
157
164
|
async function turn(prompt) {
|
|
@@ -169,13 +176,13 @@ function createRouter({ root, env, role, timeoutS = 120, preamble = '', log = ()
|
|
|
169
176
|
}
|
|
170
177
|
|
|
171
178
|
// 对一批触发出决定。解析不出来、或者漏了触发,带着一句提醒再来一轮;再不行返回 ok 为 false(missing 是漏掉的短号)
|
|
172
|
-
async function decide(triggers, open, workspaces = null) {
|
|
179
|
+
async function decide(triggers, open, workspaces = null, workers = []) {
|
|
173
180
|
const started = Date.now();
|
|
174
181
|
const ids = triggers.map((t) => t.id);
|
|
175
182
|
let extra = '';
|
|
176
183
|
let lastReply = '';
|
|
177
184
|
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
178
|
-
const r = await turn(turnPrompt(triggers, open, extra, workspaces));
|
|
185
|
+
const r = await turn(turnPrompt(triggers, open, extra, workspaces, new Date(), workers, playbook.summary(root)));
|
|
179
186
|
if (r.failed) {
|
|
180
187
|
last = { at: fmtStamp(), seconds: Math.round((Date.now() - started) / 1000), ok: false };
|
|
181
188
|
return { ok: false, failed: r.failed, config: r.config, decisions: {}, missing: ids };
|
|
@@ -202,7 +209,6 @@ function createRouter({ root, env, role, timeoutS = 120, preamble = '', log = ()
|
|
|
202
209
|
agent,
|
|
203
210
|
startSession,
|
|
204
211
|
resetSession,
|
|
205
|
-
forgetBoundLost,
|
|
206
212
|
sessionFile: SESSION_FILE,
|
|
207
213
|
doctorProblems: () => agent.doctor(),
|
|
208
214
|
notes: () => agent.notes(),
|
|
@@ -213,4 +219,4 @@ function createRouter({ root, env, role, timeoutS = 120, preamble = '', log = ()
|
|
|
213
219
|
};
|
|
214
220
|
}
|
|
215
221
|
|
|
216
|
-
module.exports = { createRouter, turnPrompt, triggerLine, PROMPT };
|
|
222
|
+
module.exports = { createRouter, turnPrompt, triggerLine, PROMPT, forgetBoundLost };
|
package/scripts/lib/template.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
// 新工作目录里的东西从哪来,以及怎么把一个用过的目录清成空白的。
|
|
2
2
|
// 这个仓库不是工作目录:工作目录里独有的文件都在 templates/ 下,分两种:
|
|
3
3
|
// 共用的(SHARED):文档、任务指令、给 agent 的笔记和 skill、local.env 的样例——仓库里就有,工作目录和包里是同一份;
|
|
4
|
-
// 模板(TEMPLATE_FILES,在 templates/ 下):profile 的空白模板、sources.yaml 的清单、空白的遗留 / 决定 /
|
|
4
|
+
// 模板(TEMPLATE_FILES,在 templates/ 下):profile 的空白模板、sources.yaml 的清单、空白的遗留 / 决定 / 规律画像 / 个人分身档案、工作目录的 AGENTS.md 和 README.md;
|
|
5
5
|
// 现生成的:profile.yaml(就是 profile.example.yaml)、sources.yaml(清单照搬,全部关着,引导时逐个确认再打开)、两个空目录 02-daily/ 和 05-goals/。
|
|
6
|
-
// 脚本和内置采集器不进工作目录:它们在包里,sources.yaml 用 id
|
|
6
|
+
// 脚本和内置采集器不进工作目录:它们在包里,sources.yaml 用 id 引用。升级用 work-rhythm update:升级这个包,再把上面这些随包带来的文件同步到新版本(lib/update.js)
|
|
7
7
|
'use strict';
|
|
8
8
|
const fs = require('node:fs');
|
|
9
9
|
const path = require('node:path');
|
|
10
10
|
const { readText, walkFiles } = require('./run');
|
|
11
11
|
|
|
12
12
|
const SHARED = ['docs', 'tasks', '.agents', 'collectors/local.env.example'];
|
|
13
|
-
const TEMPLATE_FILES = ['00-config/profile.example.yaml', '00-config/sources.yaml', '03-carryover/CARRYOVER.md', '03-carryover/DECISIONS.md', '04-patterns/WORK_RHYTHM.md', 'AGENTS.md', 'README.md'];
|
|
13
|
+
const TEMPLATE_FILES = ['00-config/profile.example.yaml', '00-config/sources.yaml', '03-carryover/CARRYOVER.md', '03-carryover/DECISIONS.md', '04-patterns/WORK_RHYTHM.md', '04-patterns/PERSONA.md', '04-patterns/PLAYBOOK.md', 'AGENTS.md', 'README.md'];
|
|
14
14
|
const BLANK = ['03-carryover/CARRYOVER.md', '03-carryover/DECISIONS.md', '04-patterns/WORK_RHYTHM.md'];
|
|
15
15
|
// 空目录:init 现建(以前靠 .gitkeep 占位;工作目录不是 git 仓库,不用了)
|
|
16
16
|
const GENERATED_DIRS = ['02-daily', '05-goals'];
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
// work-rhythm update 的核心:升级这个包,顺手把 skill 和工作目录里随包带来的文件更新到新版本。
|
|
2
|
+
// 三步:① 问 registry 有没有新版 ② 有就 npm install -g ③ 同步随包带来的文件——用户级 skills 目录里拷贝的 work-rhythm(软链的不用动),
|
|
3
|
+
// 和工作目录里 init 时复制过去的共用文件(docs、tasks、.agents、local.env.example)与 profile 的空白模板。脚本和采集器不在工作目录里,升级包就是升级它们。
|
|
4
|
+
// 这里不直接碰 npm、HOME、配置目录:怎么调 npm、目录在哪都从参数进来,自测用假的,不碰网络和全局安装。只用 node 自带的能力。
|
|
5
|
+
'use strict';
|
|
6
|
+
const fs = require('node:fs');
|
|
7
|
+
const path = require('node:path');
|
|
8
|
+
const { run, readText, which, tail } = require('./run');
|
|
9
|
+
const { SHARED, TEMPLATE_FILES } = require('./template');
|
|
10
|
+
const { configPath } = require('./workdir');
|
|
11
|
+
|
|
12
|
+
const NAME = 'work-rhythm';
|
|
13
|
+
// 包里的 skill
|
|
14
|
+
const SKILL = '.agents/skills/work-rhythm';
|
|
15
|
+
// 随包进工作目录的:init 时从包里复制过去的共用文件,加上 profile 的空白模板(其余模板是引导时填过的,不碰)
|
|
16
|
+
const SHIPPED = [...SHARED, 'templates/00-config/profile.example.yaml'];
|
|
17
|
+
// 包里的路径换成工作目录里的位置:templates/ 下的铺到根上,其余原样
|
|
18
|
+
const placed = (pkg) => (pkg.startsWith('templates/') ? pkg.slice('templates/'.length) : pkg);
|
|
19
|
+
// 各家 AI 助手读的 skills 目录(相对 HOME)。只认里面叫 work-rhythm 的那一项,别的不碰
|
|
20
|
+
const SKILL_DIRS = ['.agents/skills', '.claude/skills', '.codex/skills', '.qoder/skills', '.zcode/skills', '.kimi-code/skills', '.cursor/skills'];
|
|
21
|
+
// 运行时才有的目录(交接文档里是用户原话):不算随包带来的,更新时绕开
|
|
22
|
+
const RUNTIME = new Set(['.agents/handoff']);
|
|
23
|
+
|
|
24
|
+
const readBin = (file) => {
|
|
25
|
+
try {
|
|
26
|
+
return fs.readFileSync(file);
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// 目录下所有普通文件,路径是 base/…(/ 分隔),不跟软链;skip 里的目录(按这个路径算)跳过
|
|
33
|
+
function relFiles(dir, skip = new Set(), base = '', out = []) {
|
|
34
|
+
let entries;
|
|
35
|
+
try {
|
|
36
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
37
|
+
} catch {
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
for (const e of entries) {
|
|
41
|
+
const rel = base ? `${base}/${e.name}` : e.name;
|
|
42
|
+
if (e.isDirectory()) {
|
|
43
|
+
if (!skip.has(rel)) relFiles(path.join(dir, e.name), skip, rel, out);
|
|
44
|
+
} else if (e.isFile()) out.push(rel);
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 一个包里随包带来的所有文件(SHIPPED),加上包里的 skill:Map 包里的路径 → 内容。升级前先读进内存,升级后拿它判断本地那份改没改
|
|
50
|
+
function snapshot(packageRoot) {
|
|
51
|
+
const files = new Map();
|
|
52
|
+
for (const root of SHIPPED) {
|
|
53
|
+
const abs = path.join(packageRoot, root);
|
|
54
|
+
let stat;
|
|
55
|
+
try {
|
|
56
|
+
stat = fs.statSync(abs);
|
|
57
|
+
} catch {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (stat.isFile()) files.set(root, readBin(abs));
|
|
61
|
+
else for (const rel of relFiles(abs, RUNTIME, root)) files.set(rel, readBin(path.join(packageRoot, rel)));
|
|
62
|
+
}
|
|
63
|
+
return files;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 新包里这一片随包带来的文件(SHIPPED 各项下的),包里的路径。skill 是单独的一片(SKILL 前缀)
|
|
67
|
+
function shippedFiles(packageRoot, roots = SHIPPED) {
|
|
68
|
+
return roots.flatMap((root) => {
|
|
69
|
+
const abs = path.join(packageRoot, root);
|
|
70
|
+
let stat;
|
|
71
|
+
try {
|
|
72
|
+
stat = fs.statSync(abs);
|
|
73
|
+
} catch {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
return stat.isFile() ? [root] : relFiles(abs, RUNTIME, root);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const versionOf = (dir) => {
|
|
81
|
+
try {
|
|
82
|
+
return String(JSON.parse(readText(path.join(dir, 'package.json'))).version || '');
|
|
83
|
+
} catch {
|
|
84
|
+
return '';
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// a 比 b 新吗:按 x.y.z 逐段比数字;比不了(预发布号、非数字)就当不新
|
|
89
|
+
function newer(a, b) {
|
|
90
|
+
const pa = String(a).split('.');
|
|
91
|
+
const pb = String(b).split('.');
|
|
92
|
+
for (let i = 0; i < 3; i += 1) {
|
|
93
|
+
const x = Number(pa[i]);
|
|
94
|
+
const y = Number(pb[i]);
|
|
95
|
+
if (!Number.isInteger(x) || !Number.isInteger(y)) return false;
|
|
96
|
+
if (x !== y) return x > y;
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 把目标目录更新成新包的样子。pkgFiles 是新包里这一片的文件(包里的路径),place 把包里的路径换成目标里的路径(返回 null 的不管),
|
|
102
|
+
// old 是上一版包的快照,拿来看本地那份改没改。一个文件:
|
|
103
|
+
// 和新一样新,不动;本地没有,add;和上一版一样(没人改过),update(直接覆盖);和上一版也不一样(改过,或是更早的版本留下的),
|
|
104
|
+
// replace(先备份到 backupDir 下同样的位置,再覆盖)。上一版有、新版没了的:本地没人改过才 remove,改过的留着(kept)。
|
|
105
|
+
// 本地多出来的文件(自己写的任务、交接文档)不在 pkgFiles 里,不碰。dryRun 只算不改。返回 [{ rel, action }]
|
|
106
|
+
function syncTree({ targetDir, newDir, pkgFiles, place, old = null, backupDir = '', dryRun = false }) {
|
|
107
|
+
const done = [];
|
|
108
|
+
for (const pkg of pkgFiles) {
|
|
109
|
+
const rel = place(pkg);
|
|
110
|
+
const next = readBin(path.join(newDir, pkg));
|
|
111
|
+
if (!rel || !next) continue;
|
|
112
|
+
const file = path.join(targetDir, rel);
|
|
113
|
+
const cur = readBin(file);
|
|
114
|
+
if (cur && cur.equals(next)) continue;
|
|
115
|
+
let action = 'add';
|
|
116
|
+
if (cur) {
|
|
117
|
+
const before = old && old.get(pkg);
|
|
118
|
+
action = before && cur.equals(before) ? 'update' : 'replace';
|
|
119
|
+
if (action === 'replace' && backupDir && !dryRun) {
|
|
120
|
+
const to = path.join(backupDir, rel);
|
|
121
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
122
|
+
fs.copyFileSync(file, to);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!dryRun) {
|
|
126
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
127
|
+
fs.writeFileSync(file, next);
|
|
128
|
+
}
|
|
129
|
+
done.push({ rel, action });
|
|
130
|
+
}
|
|
131
|
+
if (!old) return done;
|
|
132
|
+
for (const [pkg, before] of old) {
|
|
133
|
+
if (pkgFiles.includes(pkg)) continue;
|
|
134
|
+
const rel = place(pkg);
|
|
135
|
+
const file = rel ? path.join(targetDir, rel) : '';
|
|
136
|
+
const cur = file ? readBin(file) : null;
|
|
137
|
+
if (!cur) continue;
|
|
138
|
+
if (cur.equals(before)) {
|
|
139
|
+
if (!dryRun) fs.rmSync(file, { force: true });
|
|
140
|
+
done.push({ rel, action: 'remove' });
|
|
141
|
+
} else done.push({ rel, action: 'kept' });
|
|
142
|
+
}
|
|
143
|
+
return done;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 用户级 skills 目录里的 work-rhythm:软链的不用动(指向包里的,包升级了它就跟着新了);拷贝的换成新包里那份。
|
|
147
|
+
// 只认 SKILL.md 里写着 name: work-rhythm 的,别的同名东西不碰。备份放 backupBase/skill/<目录名>/ 下。返回每个位置的 { where, kind, changed }
|
|
148
|
+
function syncSkills({ home, newDir, old, backupBase, dryRun = false }) {
|
|
149
|
+
const found = [];
|
|
150
|
+
if (!home) return found;
|
|
151
|
+
for (const dir of SKILL_DIRS) {
|
|
152
|
+
const entry = path.join(home, dir, NAME);
|
|
153
|
+
let st;
|
|
154
|
+
try {
|
|
155
|
+
st = fs.lstatSync(entry);
|
|
156
|
+
} catch {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const where = `${dir}/${NAME}`;
|
|
160
|
+
if (st.isSymbolicLink()) {
|
|
161
|
+
let target = '';
|
|
162
|
+
try {
|
|
163
|
+
target = fs.realpathSync(entry);
|
|
164
|
+
} catch {
|
|
165
|
+
// 坏链:下面按 broken 报
|
|
166
|
+
}
|
|
167
|
+
found.push({ where, kind: target ? 'link' : 'broken', target });
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (!st.isDirectory() || !/^name:\s*["']?work-rhythm["']?\s*$/m.test(readText(path.join(entry, 'SKILL.md')))) continue;
|
|
171
|
+
found.push({
|
|
172
|
+
where,
|
|
173
|
+
kind: 'copy',
|
|
174
|
+
changed: syncTree({
|
|
175
|
+
targetDir: entry,
|
|
176
|
+
newDir,
|
|
177
|
+
pkgFiles: shippedFiles(newDir, [SKILL]),
|
|
178
|
+
place: (pkg) => (pkg.startsWith(`${SKILL}/`) ? pkg.slice(SKILL.length + 1) : null),
|
|
179
|
+
old,
|
|
180
|
+
backupDir: path.join(backupBase, 'skill', dir.replace(/\//g, '_')),
|
|
181
|
+
dryRun,
|
|
182
|
+
}),
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return found;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 工作目录里 init 时复制过去的文件更新到新版。备份放 backupDir(工作目录的 01-inbox/_update/<时间>/ 下)。
|
|
189
|
+
// 返回 { changed, newTemplates, profileMissing }:改了什么、新版本多出来而这个工作目录还没有的模板、profile.yaml 缺的字段
|
|
190
|
+
function syncWorkdir({ workdir, newDir, old, backupDir, dryRun = false }) {
|
|
191
|
+
const changed = syncTree({ targetDir: workdir, newDir, pkgFiles: shippedFiles(newDir), place: placed, old, backupDir, dryRun });
|
|
192
|
+
// 新版本多出来的模板只报告,不放:工作目录独有的文件是引导时填出来的,直接铺一份空白的没用
|
|
193
|
+
const newTemplates = TEMPLATE_FILES.filter((rel) => !fs.existsSync(path.join(workdir, rel)));
|
|
194
|
+
// profile.yaml 不能缺 profile.example.yaml 里声明的字段(和 check 的配置自检同一个口径)
|
|
195
|
+
const keys = (file) => new Set(readText(file).split('\n').map((l) => /^\s*([A-Za-z_][A-Za-z0-9_-]*):/.exec(l.replace(/\s#.*$/, ''))).filter(Boolean).map((m) => m[1]));
|
|
196
|
+
const example = path.join(workdir, '00-config', 'profile.example.yaml');
|
|
197
|
+
const profile = path.join(workdir, '00-config', 'profile.yaml');
|
|
198
|
+
const profileMissing = fs.existsSync(profile) ? [...keys(example)].filter((k) => !keys(profile).has(k)).sort() : [];
|
|
199
|
+
return { changed, newTemplates, profileMissing };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// 跑一遍升级。packageRoot 是现在运行的这份包,home 是用户目录,workdir 是要同步文件的工作目录(没有就空串)。
|
|
203
|
+
// npm 怎么调从参数进来:npm(args) → { code, stdout, stderr };不给就真的起 npm。
|
|
204
|
+
// checkOnly 只问有没有新版;dryRun 把会改什么算出来但不写(也不升级包)。返回:
|
|
205
|
+
// { from, to, latest, installed, linked, upgraded, problem, problemCode, skills, workdir, backupDir }
|
|
206
|
+
// from 是装着的版本(全局没装就是现在运行的这份),to 是这次结束时生效的版本;
|
|
207
|
+
// problem 有值就是包没升成(problemCode:没有 npm 是 missing,全局没装是 absent,全局的是软链是 linked,其余 failed)——
|
|
208
|
+
// missing / failed 连文件都没同步就返回了;absent / linked 包不动,文件照样按现有的那份同步
|
|
209
|
+
function update({ packageRoot, home = '', workdir = '', env = process.env, npm = null, dryRun = false, checkOnly = false }) {
|
|
210
|
+
const out = { from: versionOf(packageRoot), to: '', latest: '', installed: '', linked: false, upgraded: false, problem: '', problemCode: '', skills: [], workdir: null, backupDir: '' };
|
|
211
|
+
const given = Boolean(npm);
|
|
212
|
+
if (!npm) npm = (args) => run('npm', args, { env, timeout: 900 });
|
|
213
|
+
if (!given && !which('npm', env)) return { ...out, problem: '本机没有 npm:这个命令靠 npm 升级包。装了 npm 再来,或者按 README 手动更新', problemCode: 'missing' };
|
|
214
|
+
const rootR = npm(['root', '-g']);
|
|
215
|
+
const globalRoot = rootR.code === 0 ? rootR.stdout.trim().split('\n').pop().trim() : '';
|
|
216
|
+
if (!globalRoot) return { ...out, problem: `npm root -g 没跑成:${tail(rootR.stderr || rootR.stdout) || `退出码 ${rootR.code}`}`, problemCode: 'failed' };
|
|
217
|
+
const globalDir = path.join(globalRoot, NAME);
|
|
218
|
+
const safeReal = (p) => {
|
|
219
|
+
try {
|
|
220
|
+
return fs.realpathSync(p);
|
|
221
|
+
} catch {
|
|
222
|
+
return '';
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
const real = safeReal(globalDir);
|
|
226
|
+
const installed = Boolean(real) && fs.existsSync(path.join(real, 'package.json'));
|
|
227
|
+
out.installed = installed ? globalDir : '';
|
|
228
|
+
// 是不是软链只看最后这一级(包目录本身):上级目录里的软链不算,比如 macOS 的 /var 是到 /private/var 的链
|
|
229
|
+
out.linked = installed && path.dirname(real) !== safeReal(globalRoot);
|
|
230
|
+
if (installed) out.from = versionOf(real) || out.from;
|
|
231
|
+
// 上一版的样子先读进内存:升级会把那个目录换掉
|
|
232
|
+
const old = installed ? snapshot(real) : null;
|
|
233
|
+
// ① 问 registry
|
|
234
|
+
const view = npm(['view', `${NAME}@latest`, 'version']);
|
|
235
|
+
out.latest = view.code === 0 ? view.stdout.trim().split('\n').pop().trim() : '';
|
|
236
|
+
if (!out.latest) return { ...out, problem: `问不到 registry 上最新的版本(离线?):${tail(view.stderr || view.stdout) || `退出码 ${view.code}`}`, problemCode: 'failed' };
|
|
237
|
+
// ② 升级包。newRoot 是同步文件时读的那份:全局装了就是全局那份(升级后也是它),没装就是现在运行的这份
|
|
238
|
+
let newRoot = installed ? globalDir : packageRoot;
|
|
239
|
+
out.to = out.from;
|
|
240
|
+
// 退出码 3 只在「registry 上有新版、这里却升不了」时给(problemCode:软链 linked、全局没装 absent);已经是最新就不算问题
|
|
241
|
+
if (!installed) {
|
|
242
|
+
// 全局没装(从仓库里直接跑的):包不动,文件按现在运行的这份同步
|
|
243
|
+
if (newer(out.latest, out.from)) {
|
|
244
|
+
out.problem = `全局没有用 npm 装过 ${NAME}:包没动`;
|
|
245
|
+
out.problemCode = 'absent';
|
|
246
|
+
}
|
|
247
|
+
if (checkOnly) out.to = out.latest;
|
|
248
|
+
} else if (!newer(out.latest, out.from)) {
|
|
249
|
+
// 已是最新
|
|
250
|
+
} else if (out.linked) {
|
|
251
|
+
out.problem = `全局的 ${NAME} 是软链(${globalDir} → ${real}),不是 npm 装的发布版:要升到 ${out.latest} 得更新那边的代码(git pull 之类),这里不动包`;
|
|
252
|
+
out.problemCode = 'linked';
|
|
253
|
+
newRoot = real;
|
|
254
|
+
} else if (dryRun || checkOnly) out.to = out.latest;
|
|
255
|
+
else {
|
|
256
|
+
const inst = npm(['install', '-g', `${NAME}@${out.latest}`]);
|
|
257
|
+
const after = versionOf(globalDir);
|
|
258
|
+
if (inst.code !== 0 || after !== out.latest) {
|
|
259
|
+
return { ...out, problem: `npm install -g ${NAME}@${out.latest} 没成:${tail(inst.stderr || inst.stdout) || `装上的是 ${after || '别的东西'}`}`, problemCode: 'failed' };
|
|
260
|
+
}
|
|
261
|
+
out.upgraded = true;
|
|
262
|
+
out.to = out.latest;
|
|
263
|
+
newRoot = globalDir;
|
|
264
|
+
}
|
|
265
|
+
if (checkOnly) return out;
|
|
266
|
+
const d = new Date();
|
|
267
|
+
const p = (n) => String(n).padStart(2, '0');
|
|
268
|
+
const stamp = `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`;
|
|
269
|
+
// 备份放哪:工作目录里的 01-inbox/_update/ 下;没有工作目录就放用户级配置目录旁边(skills 目录里不放,免得被当成另一个 skill)
|
|
270
|
+
const base = workdir ? path.join(workdir, '01-inbox', '_update', stamp) : path.join(path.dirname(configPath(env)), 'backups', stamp);
|
|
271
|
+
// ③ 同步随包带来的文件
|
|
272
|
+
out.skills = syncSkills({ home, newDir: newRoot, old, backupBase: base, dryRun });
|
|
273
|
+
if (workdir) out.workdir = syncWorkdir({ workdir, newDir: newRoot, old, backupDir: path.join(base, 'workdir'), dryRun });
|
|
274
|
+
// 试跑不备份: backupDir 只在真备份了的时候才有值
|
|
275
|
+
out.backupDir = !dryRun && [...out.skills, ...(out.workdir ? [out.workdir] : [])].some((r) => (r.changed || []).some((c) => c.action === 'replace')) ? base : '';
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
module.exports = { NAME, SKILL, SHIPPED, SKILL_DIRS, relFiles, snapshot, shippedFiles, newer, syncTree, syncSkills, syncWorkdir, update };
|