work-rhythm 0.0.0-stage → 0.1.1

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.
Files changed (107) hide show
  1. package/.agents/notes/AGENTS.md +86 -0
  2. package/.agents/notes/decisions/2026-11-28-long-term-goals-ride-existing-tasks.md +36 -0
  3. package/.agents/notes/decisions/2026-11-29-building-a-workdir-means-cloning-the-template.md +25 -0
  4. package/.agents/notes/decisions/2026-11-29-find-workdir-cwd-first-then-registered.md +32 -0
  5. package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md +48 -0
  6. package/.agents/notes/decisions/2026-11-30-cost-entry-writes-into-empty-cells-directly.md +25 -0
  7. package/.agents/notes/decisions/2026-11-30-dependencies-are-allowed-user-installs-missing-ones.md +30 -0
  8. package/.agents/notes/decisions/2026-11-30-markdown-is-the-record-html-is-only-a-view.md +25 -0
  9. package/.agents/notes/decisions/2026-11-30-message-channels-are-preset-in-the-skill.md +34 -0
  10. package/.agents/notes/decisions/2026-11-30-no-workdir-offers-to-create-one-by-default.md +35 -0
  11. package/.agents/notes/decisions/2026-11-30-paging-requires-coverage-only-when-enabled.md +27 -0
  12. package/.agents/notes/decisions/2026-11-30-pending-decisions-asked-in-a-chat-and-logged-briefly.md +39 -0
  13. package/.agents/notes/decisions/2026-11-30-push-via-personal-robot-webhook.md +33 -0
  14. package/.agents/notes/decisions/2026-11-30-scheduling-moves-to-a-local-service.md +37 -0
  15. package/.agents/notes/decisions/2026-11-30-skill-ships-with-its-template-in-a-zip.md +30 -0
  16. package/.agents/notes/decisions/2026-11-30-skill-stays-generic-user-provides-cli-or-mcp.md +25 -0
  17. package/.agents/notes/decisions/2026-12-01-everything-is-js-behind-work-rhythm.md +46 -0
  18. package/.agents/notes/decisions/2026-12-01-repo-is-not-a-workdir-templates-hold-workdir-files.md +29 -0
  19. package/.agents/notes/decisions/2026-12-01-repo-stays-generic-personal-data-lives-in-workdirs.md +32 -0
  20. package/.agents/notes/decisions/2026-12-01-workdir-is-not-a-git-repository.md +25 -0
  21. package/.agents/notes/pitfalls/2026-11-29-do-not-hand-roll-git-log.md +22 -0
  22. package/.agents/notes/pitfalls/2026-11-29-empty-or-partial-collector-output.md +23 -0
  23. package/.agents/notes/pitfalls/2026-11-29-skill-description-is-truncated.md +21 -0
  24. package/.agents/notes/pitfalls/2026-11-29-tool-shell-is-zsh.md +24 -0
  25. package/.agents/notes/pitfalls/2026-11-30-compare-timestamps-as-digits.md +22 -0
  26. package/.agents/notes/pitfalls/2026-11-30-message-search-lags-after-sending.md +18 -0
  27. package/.agents/notes/pitfalls/2026-11-30-rewriting-a-file-with-empty-output-after-tool-failure.md +20 -0
  28. package/.agents/notes/pitfalls/2026-11-30-secret-scan-false-positive-on-hyphenated-file-names.md +18 -0
  29. package/.agents/notes/pitfalls/2026-11-30-selftest-must-not-inherit-local-config.md +29 -0
  30. package/.agents/notes/pitfalls/2026-11-30-yzj-cli-list-limit-is-20.md +25 -0
  31. package/.agents/notes/pitfalls/2026-11-30-zsh-no-word-splitting-special-names.md +23 -0
  32. package/.agents/notes/pitfalls/2026-12-01-node-test-hooks-cannot-register-after-callbacks.md +22 -0
  33. package/.agents/notes/pitfalls/2026-12-01-spawning-node-test-inside-a-test-runs-nothing.md +18 -0
  34. package/.agents/skills/work-rhythm/SKILL.md +278 -0
  35. package/AGENTS.md +19 -0
  36. package/README.md +69 -2
  37. package/collectors/approval-yzj.js +114 -0
  38. package/collectors/calendar-yzj.js +48 -0
  39. package/collectors/file-activity.js +131 -0
  40. package/collectors/git-activity.js +105 -0
  41. package/collectors/im-yzj.js +151 -0
  42. package/collectors/linky-issues.js +88 -0
  43. package/collectors/local.env.example +52 -0
  44. package/docs/channels.md +95 -0
  45. package/docs/formats.md +285 -0
  46. package/docs/im-adapters.md +79 -0
  47. package/docs/scheduled-tasks.md +115 -0
  48. package/docs/sources.md +104 -0
  49. package/package.json +35 -4
  50. package/scripts/cli.js +101 -0
  51. package/scripts/cmd/check.js +330 -0
  52. package/scripts/cmd/collect.js +25 -0
  53. package/scripts/cmd/decisions.js +236 -0
  54. package/scripts/cmd/goals.js +145 -0
  55. package/scripts/cmd/init.js +49 -0
  56. package/scripts/cmd/listen.js +91 -0
  57. package/scripts/cmd/notify.js +42 -0
  58. package/scripts/cmd/pack.js +104 -0
  59. package/scripts/cmd/rhythm.js +102 -0
  60. package/scripts/cmd/selftest.js +23 -0
  61. package/scripts/cmd/sync.js +73 -0
  62. package/scripts/cmd/today.js +29 -0
  63. package/scripts/cmd/workdir.js +81 -0
  64. package/scripts/lib/agent.js +270 -0
  65. package/scripts/lib/bot.js +376 -0
  66. package/scripts/lib/collector.js +74 -0
  67. package/scripts/lib/config.js +250 -0
  68. package/scripts/lib/launchd.js +82 -0
  69. package/scripts/lib/log.js +22 -0
  70. package/scripts/lib/markdown.js +208 -0
  71. package/scripts/lib/notify.js +43 -0
  72. package/scripts/lib/rhythm.js +508 -0
  73. package/scripts/lib/robot.js +64 -0
  74. package/scripts/lib/run.js +151 -0
  75. package/scripts/lib/template.js +149 -0
  76. package/scripts/lib/workdir.js +98 -0
  77. package/scripts/test/check.test.js +279 -0
  78. package/scripts/test/collectors.test.js +574 -0
  79. package/scripts/test/config.test.js +118 -0
  80. package/scripts/test/decisions.test.js +129 -0
  81. package/scripts/test/fake-webhook.js +31 -0
  82. package/scripts/test/goals.test.js +109 -0
  83. package/scripts/test/harness.js +277 -0
  84. package/scripts/test/init.test.js +123 -0
  85. package/scripts/test/listen.test.js +362 -0
  86. package/scripts/test/notify.test.js +176 -0
  87. package/scripts/test/pack.test.js +141 -0
  88. package/scripts/test/rhythm.test.js +333 -0
  89. package/scripts/test/stubs.js +140 -0
  90. package/scripts/test/today.test.js +73 -0
  91. package/scripts/test/workdir.test.js +152 -0
  92. package/scripts/work-rhythm.js +18 -0
  93. package/tasks/alerts-morning.md +24 -0
  94. package/tasks/carryover.md +16 -0
  95. package/tasks/daily-plan.md +29 -0
  96. package/tasks/evening-review.md +27 -0
  97. package/tasks/linky-cost-check.md +13 -0
  98. package/tasks/linky-cost-fill.md +20 -0
  99. package/tasks/noon-checkin.md +15 -0
  100. package/tasks/weekly-rhythm.md +18 -0
  101. package/templates/00-config/profile.example.yaml +54 -0
  102. package/templates/00-config/sources.yaml +115 -0
  103. package/templates/03-carryover/CARRYOVER.md +14 -0
  104. package/templates/03-carryover/DECISIONS.md +10 -0
  105. package/templates/04-patterns/WORK_RHYTHM.md +16 -0
  106. package/templates/AGENTS.md +31 -0
  107. package/templates/README.md +73 -0
package/scripts/cli.js ADDED
@@ -0,0 +1,101 @@
1
+ // work-rhythm 的子命令分发。每个子命令在 scripts/cmd/<名字>.js 里,导出 run(args, ctx) 和 usage。
2
+ // ctx = { root, cwd, stdin, stdout, stderr, env, packageRoot };root 是工作目录(找不到时为空串)。
3
+ // 退出码统一:0 成功 · 2 用法不对 · 3 没配置好 · 4 缺依赖 · 5 运行失败
4
+ 'use strict';
5
+ const fs = require('node:fs');
6
+ const path = require('node:path');
7
+ const { EXIT, Fail } = require('./lib/run');
8
+ const { resolveRoot } = require('./lib/workdir');
9
+
10
+ const PACKAGE_ROOT = path.resolve(__dirname, '..');
11
+ const MANIFEST = JSON.parse(fs.readFileSync(path.join(PACKAGE_ROOT, 'package.json'), 'utf8'));
12
+ const NAME = MANIFEST.name;
13
+ const VERSION = MANIFEST.version;
14
+
15
+ // 需要工作目录的子命令,和它们的用法。不需要的(workdir、pack、init、help、version)另算
16
+ const COMMANDS = {
17
+ today: 'today [YYYY-MM-DD] 今天在工作目录里的约定:日期、星期、每日文件路径、是否工作日',
18
+ goals: 'goals list | due [日期] | touch <id> [日期] | progress <id> [日期] 长期目标的小工具',
19
+ decisions: 'decisions add | answer | done | list | archive … 待决与决定的记录',
20
+ sync: 'sync [起始日期] [源 id …] 依次运行已启用的源,原始数据落 01-inbox/<源>/<今天>',
21
+ collect: 'collect <源id> <输出目录> [起始日期] 只跑一个采集器',
22
+ notify: 'notify "<文字>" | - 把一段文字经消息渠道推给我本人,或从标准输入读',
23
+ check: 'check 改完脚本、配置或文档后跑一遍:语法、内链、预算、配置、密钥、笔记',
24
+ listen: 'listen probe|run|handle|handle-frame|reset|doctor|install|uninstall|status 值守:收我在 IM 里说的话,交给本地 agent 回复',
25
+ rhythm: 'rhythm start|stop|status|due|run|serve|render|doctor|install|uninstall 本地服务:定时采集、定时整理、推送、网页',
26
+ workdir: 'workdir [register|find|config|blank] 工作目录在哪:找、登记、扫描本机;不带子命令是找',
27
+ init: 'init <目录> 新建一个工作目录(空白模板、登记)',
28
+ pack: 'pack [--out <目录>] 把 skill 和这个包打成可分发的目录',
29
+ selftest: 'selftest [--test-name-pattern=…] 自测:跑一遍 scripts/test/ 下的用例',
30
+ };
31
+
32
+ // 不需要站在工作目录里的子命令(它们自己找,或者和工作目录无关)
33
+ const NO_ROOT = new Set(['workdir', 'init', 'pack', 'selftest']);
34
+ const NEED_ROOT = new Set(Object.keys(COMMANDS).filter((k) => !NO_ROOT.has(k)));
35
+
36
+ const usage = (name) => {
37
+ if (name && COMMANDS[name]) return require(`./cmd/${name}`).usage;
38
+ const lines = Object.entries(COMMANDS).map(([key, text]) => ` ${text}`);
39
+ return `work-rhythm ${VERSION} —— 个人工作目录的日常运转
40
+ 用法: work-rhythm <子命令> [参数] [--workdir <目录>]
41
+ 在某个工作目录里运行,就作用在那个目录上;不在时往上找带 00-config/profile.yaml 的目录,也可以用 --workdir 指定。
42
+
43
+ ${lines.join('\n')}
44
+ --help [子命令] | --version
45
+ 消息渠道见 docs/channels.md,定时任务见 docs/scheduled-tasks.md。`;
46
+ };
47
+
48
+ async function main(argv) {
49
+ const stdout = process.stdout;
50
+ const stderr = process.stderr;
51
+ if (Number(process.versions.node.split('.')[0]) < 22) {
52
+ stderr.write(`work-rhythm: 需要 node 22 以上,本机是 ${process.version}\n`);
53
+ return EXIT.MISSING_DEP;
54
+ }
55
+ // 全局选项 --workdir 可以写在任何位置
56
+ const args = [];
57
+ let rootOption = '';
58
+ for (let i = 0; i < argv.length; i += 1) {
59
+ if (argv[i] === '--workdir' && i + 1 < argv.length) rootOption = argv[++i];
60
+ else args.push(argv[i]);
61
+ }
62
+ const [name, ...rest] = args;
63
+ if (name === '--version' || name === 'version') {
64
+ stdout.write(`${VERSION}\n`);
65
+ return EXIT.OK;
66
+ }
67
+ if (name === '--help' || name === 'help' || name === '-h') {
68
+ stdout.write(`${usage(rest[0])}\n`);
69
+ return EXIT.OK;
70
+ }
71
+ if (!name) {
72
+ stderr.write(`${usage()}\n`);
73
+ return EXIT.USAGE;
74
+ }
75
+ if (!COMMANDS[name]) {
76
+ stderr.write(`work-rhythm: 不认识的子命令「${name}」\n\n${usage()}\n`);
77
+ return EXIT.USAGE;
78
+ }
79
+ // 作用在哪个目录:--workdir 指定的,或从当前目录往上找;站在这个包自己的仓库里就是仓库(只有 check 查它);最后看用户级的登记
80
+ const found = resolveRoot({ cwd: process.cwd(), option: rootOption, env: process.env, packageRoot: PACKAGE_ROOT });
81
+ const ctx = { root: found.root, rootSource: found.source, cwd: process.cwd(), stdin: process.stdin, stdout, stderr, env: process.env, packageRoot: PACKAGE_ROOT };
82
+ if (NEED_ROOT.has(name) && (!found.root || (found.source === 'package' && name !== 'check'))) {
83
+ const why = found.source === 'package'
84
+ ? '这里是 work-rhythm 自己的仓库,不是工作目录'
85
+ : `往上找不到 00-config/profile.yaml${found.problem ? `;${found.problem}` : ',也没有登记'}`;
86
+ stderr.write(`work-rhythm ${name}: 当前目录不是工作目录(${why});用 --workdir 指定,或先用 work-rhythm init 新建\n`);
87
+ return EXIT.NOT_CONFIGURED;
88
+ }
89
+ try {
90
+ return await require(`./cmd/${name}`).run(rest, ctx);
91
+ } catch (error) {
92
+ // 用法不对、没配置好、缺依赖、没跑成:消息打给用户,按它带的退出码结束
93
+ if (error instanceof Fail) {
94
+ stderr.write(`work-rhythm ${name}: ${error.message}\n`);
95
+ return error.code;
96
+ }
97
+ throw error;
98
+ }
99
+ }
100
+
101
+ module.exports = { main, usage, COMMANDS, NAME, VERSION, PACKAGE_ROOT };
@@ -0,0 +1,330 @@
1
+ // 改完脚本、配置或文档后跑一遍:脚本语法、文档内链、预算、配置自检、疑似密钥、笔记格式。
2
+ // 全部通过时退出码为 0;有未通过的项退出码为 1,每项都指出来。只读,不修改任何文件。
3
+ // 用法: work-rhythm check
4
+ // 站在工作目录里是 6 项;站在这个包自己的仓库里(它不是工作目录)换成另一套:没有「配置自检」,多「模板」和「仓库保持通用」。
5
+ 'use strict';
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+ const vm = require('node:vm');
9
+ const { EXIT, UsageError, run: spawn, which, walkFiles, readText, tail } = require('../lib/run');
10
+ const { loadSources, settings, lintLocalEnv } = require('../lib/config');
11
+ const { notifyChannel } = require('../lib/notify');
12
+ const { hasWebhook, serviceName } = require('../lib/robot');
13
+ const { resolveCollector } = require('./sync');
14
+ const { createBot } = require('../lib/bot');
15
+ const { GENERATED_DIRS, templateProblems } = require('../lib/template');
16
+
17
+ const SKILL_KEYS = ['name', 'description', 'when_to_use', 'license', 'metadata'];
18
+ const SKIP_DIRS = new Set(['.git', '01-inbox', 'node_modules', '.zcode']);
19
+ // 内置采集器各要什么:tools 是 PATH 上要有的命令,var 是 local.env 里一定要有的变量(源 id 不是采集器自己的名字时加 _<ID> 后缀)
20
+ const NEEDS = {
21
+ 'im-yzj': { tools: [], var: 'YZJ_GROUP_IDS' },
22
+ 'linky-issues': { tools: ['linky'] },
23
+ 'calendar-yzj': { tools: ['yzj-cli'] },
24
+ 'approval-yzj': { tools: ['yzj-cli'] },
25
+ };
26
+
27
+ const filledLines = (file) => readText(file).split('\n').filter((l) => l.length).length;
28
+
29
+ function listDir(dir, files = false) {
30
+ try {
31
+ return fs.readdirSync(dir, { withFileTypes: true }).filter((d) => (files ? d.isFile() : d.isDirectory())).map((d) => d.name);
32
+ } catch {
33
+ return [];
34
+ }
35
+ }
36
+
37
+ // 把一个 JS 文件编译一遍但不运行:有语法错就返回错误信息,没有返回空串。开头的 #! 要先去掉
38
+ function syntaxError(file) {
39
+ try {
40
+ new vm.Script(readText(file).replace(/^#![^\n]*/, ''), { filename: file });
41
+ return '';
42
+ } catch (error) {
43
+ return tail(String(error && error.message ? error.message : error), 200);
44
+ }
45
+ }
46
+
47
+ // 1 脚本语法。查的是 work-rhythm 自己(包里的 scripts/、collectors/)和工作目录里自己加的脚本:
48
+ // 采集器、skill 里带的脚本,用什么语言写都行。返回没通过的个数
49
+ function checkSyntax({ root, packageRoot, say }) {
50
+ const files = new Set();
51
+ const add = (file) => {
52
+ if (/\.(js|mjs|cjs|sh|py)$/.test(file)) files.add(file);
53
+ };
54
+ for (const dir of ['scripts', 'collectors']) walkFiles(path.join(packageRoot, dir), SKIP_DIRS, 0).forEach(add);
55
+ // 工作目录里的 collectors/ 只看自己加的那几个(local.env 之类不是脚本)
56
+ for (const name of listDir(path.join(root, 'collectors'), true)) add(path.join(root, 'collectors', name));
57
+ for (const skill of listDir(path.join(root, '.agents', 'skills'))) walkFiles(path.join(root, '.agents', 'skills', skill, 'scripts'), SKIP_DIRS, 0).forEach(add);
58
+ let bad = 0;
59
+ const sorted = [...files].sort();
60
+ for (const file of sorted.filter((f) => /\.(js|mjs|cjs)$/.test(f))) {
61
+ const problem = syntaxError(file);
62
+ if (!problem) continue;
63
+ say(problem);
64
+ say(`语法错误 ${path.relative(root, file)}`);
65
+ bad += 1;
66
+ }
67
+ // 用 shell、Python 写的:有对应的解释器就查一遍,没有就报缺依赖
68
+ const others = [
69
+ ['sh', 'bash', (f) => spawn('bash', ['-n', f]).code === 0],
70
+ ['py', 'python3', (f) => spawn('python3', ['-c', 'import ast, sys; ast.parse(open(sys.argv[1], encoding="utf-8").read(), sys.argv[1])', f]).code === 0],
71
+ ];
72
+ for (const [ext, tool, passes] of others) {
73
+ for (const file of sorted.filter((f) => f.endsWith(`.${ext}`))) {
74
+ if (!which(tool)) say(`${path.relative(root, file)} 需要 ${tool},本机没有`);
75
+ else if (passes(file)) continue;
76
+ else say(`语法错误 ${path.relative(root, file)}`);
77
+ bad += 1;
78
+ }
79
+ }
80
+ return bad;
81
+ }
82
+
83
+ // 2 文档内链:相对链接指向的文件在不在。代码块里的内容是示例,不检查;别的协议(http、mailto…)不管。
84
+ // 链接都是按工作目录里的位置写的。站在工作目录里查,找不到的再到包里找(脚本不在工作目录里);
85
+ // 站在仓库里查,按工作目录里的位置到包里找:共用的文件在包根下,工作目录独有的在 templates/ 下。
86
+ // templates/ 下的文件(AGENTS.md、README.md、03-carryover/…)铺进工作目录之后才在根上,所以它们的位置先换算成工作目录里的位置
87
+ function checkLinks({ root, packageRoot, say }) {
88
+ const broken = [];
89
+ const dev = path.resolve(root) === path.resolve(packageRoot);
90
+ const templates = path.join(packageRoot, 'templates');
91
+ for (const file of walkFiles(root, SKIP_DIRS, 0)) {
92
+ if (!file.endsWith('.md')) continue;
93
+ let fenced = false;
94
+ for (const line of readText(file).split('\n')) {
95
+ if (/^\s*```/.test(line)) {
96
+ fenced = !fenced;
97
+ continue;
98
+ }
99
+ if (fenced) continue;
100
+ for (const m of line.matchAll(/\]\(([^)]+)\)/g)) {
101
+ const link = m[1].trim().replace(/\s+"[^"]*"$/, '').replace(/#.*$/, '');
102
+ if (!link || /^[a-z][a-z0-9+.-]*:/i.test(link)) continue;
103
+ // 文件在工作目录里的位置:templates/ 下的按铺开之后算
104
+ const placed = file.startsWith(`${templates}${path.sep}`) ? path.join(packageRoot, path.relative(templates, file)) : file;
105
+ const target = path.resolve(path.dirname(placed), decodeURIComponent(link));
106
+ const rel = path.relative(dev ? packageRoot : root, target);
107
+ if (rel.startsWith('..')) {
108
+ broken.push(`断链 ./${path.relative(root, file)} -> ${link}`);
109
+ continue;
110
+ }
111
+ const candidates = [target, path.resolve(packageRoot, rel)];
112
+ if (dev) {
113
+ candidates.push(path.resolve(templates, rel));
114
+ // profile.yaml、02-daily/、05-goals/ 是 init 现生成的(见 lib/template.js),仓库里只有模板那一份或者没有
115
+ if (rel === '00-config/profile.yaml') candidates.push(path.resolve(templates, '00-config', 'profile.example.yaml'));
116
+ if (GENERATED_DIRS.includes(rel)) continue;
117
+ }
118
+ if (!candidates.some((c) => fs.existsSync(c))) broken.push(`断链 ./${path.relative(root, file)} -> ${link}`);
119
+ }
120
+ }
121
+ }
122
+ broken.forEach(say);
123
+ return broken.length;
124
+ }
125
+
126
+ // 3 预算:SKILL.md 不超过 500 个非空行;工作目录的 AGENTS.md 不超过 60 行、standing orders 不超过 10 条。
127
+ // skill 的头部:ZCode 只认 name、description、when_to_use、license、metadata 五个键,而且只把 description 的前约 250 个字符交给模型——
128
+ // skill 装上了不等于会被调用
129
+ function checkBudgets({ root, packageRoot, say }) {
130
+ const bad = [];
131
+ // skill 和工作目录的 AGENTS.md:在工作目录里就是它自己那份,在仓库里 skill 在包根下、AGENTS.md 在 templates/ 下
132
+ const dev = path.resolve(root) === path.resolve(packageRoot);
133
+ const skill = path.join(dev ? packageRoot : root, '.agents', 'skills', 'work-rhythm', 'SKILL.md');
134
+ if (fs.existsSync(skill)) {
135
+ if (filledLines(skill) >= 500) bad.push('SKILL.md 超过 500 个非空行');
136
+ const lines = readText(skill).split('\n');
137
+ const end = lines.indexOf('---', 1);
138
+ const front = lines[0] === '---' && end > 0 ? lines.slice(1, end) : [];
139
+ const isKey = (l) => /^[A-Za-z_][A-Za-z0-9_]*:/.test(l);
140
+ const unknown = [...new Set(front.filter((l) => isKey(l)).map((l) => /^([A-Za-z_][A-Za-z0-9_]*):/.exec(l)[1]))].filter((k) => !SKILL_KEYS.includes(k));
141
+ if (unknown.length) bad.push(`SKILL.md 的头部有 ZCode 不认识的键:${unknown.join(' ')}(会被忽略;其他信息放进 metadata)`);
142
+ // 描述可能是一行带引号的字符串,也可能是 > 或 | 起头的多行;到下一个键为止,拼成一行再数字符
143
+ const at = front.findIndex((l) => /^description:/.test(l));
144
+ const parts = [];
145
+ if (at >= 0) {
146
+ parts.push(front[at].replace(/^description:\s*/, ''));
147
+ for (const l of front.slice(at + 1)) {
148
+ if (isKey(l)) break;
149
+ parts.push(l.replace(/^\s*[|>][-+]?\s*/, ' ').replace(/^\s+/, ' '));
150
+ }
151
+ }
152
+ const desc = parts.join('').trim().replace(/^"/, '').replace(/"$/, '').trim();
153
+ const len = Array.from(desc).length;
154
+ if (len > 250) bad.push(`SKILL.md 的 description 超过 250 个字符(现在 ${len}),超出的部分模型看不到:把触发场景放到最前面`);
155
+ }
156
+ // 工作目录的 AGENTS.md:在工作目录里就是根目录那份,在仓库里是 templates/ 下那份
157
+ const agents = dev ? path.join(packageRoot, 'templates', 'AGENTS.md') : path.join(root, 'AGENTS.md');
158
+ if (fs.existsSync(agents)) {
159
+ if (filledLines(agents) > 60) bad.push('AGENTS.md 超过 60 个非空行');
160
+ const lines = readText(agents).split('\n');
161
+ const from = lines.indexOf('## Standing orders');
162
+ let orders = 0;
163
+ for (const l of from < 0 ? [] : lines.slice(from + 1)) {
164
+ if (/^## /.test(l)) break;
165
+ if (/^- ./.test(l)) orders += 1;
166
+ }
167
+ if (orders > 10) bad.push('AGENTS.md 的 standing orders 超过 10 条');
168
+ }
169
+ bad.forEach(say);
170
+ return bad.length;
171
+ }
172
+
173
+ // 4 配置自检(只在工作目录里):profile 没缺字段、local.env 写得对、启用的源有采集器、变量和依赖、消息渠道和值守配好了。
174
+ // 定时任务是无人值守的,问题要在这里先暴露。仓库自己没有这些配置
175
+ function checkConfig({ root, packageRoot, env, say }) {
176
+ if (path.resolve(root) === path.resolve(packageRoot)) return 0;
177
+ const bad = [];
178
+ // profile.yaml 不能缺 profile.example.yaml(空白模板)里声明的字段:模板加了新字段,填好的那份要跟上。多出来的字段是个人加的,不管
179
+ const example = path.join(root, '00-config', 'profile.example.yaml');
180
+ const profile = path.join(root, '00-config', 'profile.yaml');
181
+ if (fs.existsSync(example) && fs.existsSync(profile)) {
182
+ 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]));
183
+ const missing = [...keys(example)].filter((k) => !keys(profile).has(k));
184
+ if (missing.length) bad.push(`00-config/profile.yaml 缺了这些字段(对照 profile.example.yaml 补上): ${missing.sort().join(' ')}`);
185
+ }
186
+ // local.env 写得对不对:引用了未定义的变量、不是 KEY=VALUE 的行。有问题时采集脚本读到的配置就不是你以为的那份
187
+ const hasLocalEnv = fs.existsSync(path.join(root, 'collectors', 'local.env'));
188
+ const envProblems = hasLocalEnv ? lintLocalEnv(readText(path.join(root, 'collectors', 'local.env')), env) : [];
189
+ envProblems.forEach((p) => bad.push(`collectors/local.env ${p}`));
190
+ const values = envProblems.length ? {} : settings(root, env);
191
+ // 模板约定:每个源里 enabled 写在 collector 之前。只看已启用的源
192
+ for (const src of loadSources(root)) {
193
+ if (!src.enabled || src.collector === 'none') continue;
194
+ if (!resolveCollector(root, packageRoot, src.collector)) {
195
+ bad.push(`已启用的源 ${src.id} 缺采集脚本: ${src.collector}(关掉它,或把脚本补上)`);
196
+ continue;
197
+ }
198
+ const needs = NEEDS[src.collector];
199
+ if (!needs) continue;
200
+ for (const tool of needs.tools) {
201
+ if (!which(tool, env)) bad.push(`已启用的源 ${src.id} 需要 ${tool},本机没有`);
202
+ }
203
+ if (needs.var) {
204
+ // 源 id 是 im-yzj 时读 YZJ_GROUP_IDS,别的 id 读 YZJ_GROUP_IDS_<ID>
205
+ const name = src.id === 'im-yzj' ? needs.var : `${needs.var}_${src.id.toUpperCase().replace(/-/g, '_')}`;
206
+ if (!hasLocalEnv) bad.push(`已启用的源 ${src.id} 需要 collectors/local.env(从 local.env.example 复制后填写 ${name})`);
207
+ else if (!envProblems.length && !(values[name] || '').trim()) bad.push(`已启用的源 ${src.id} 没有配置 ${name}(collectors/local.env)`);
208
+ }
209
+ }
210
+ // 消息渠道(docs/channels.md):preferences.notify 写了内置渠道时,钥匙串里要有地址、依赖要装好。
211
+ // 只查钥匙串里有没有这一项,不把地址读出来,更不打印。别的值这里不管(notify 会报「不认识的渠道」)
212
+ const channel = notifyChannel(root);
213
+ if (channel === 'yzj-robot') {
214
+ if (!which('security', env)) bad.push(`消息渠道 ${channel} 需要 security,本机没有`);
215
+ else if (!hasWebhook(env)) bad.push(`消息渠道 ${channel}:钥匙串里没有 ${serviceName(env)}(开通步骤见 docs/channels.md「云之家」)`);
216
+ }
217
+ // 值守(work-rhythm listen,渠道的收消息一半):配了 YZJ_BOT_AGENT 才检查。查什么由它自己的 doctor 说了算,这里不重复一遍
218
+ if (!envProblems.length && (values.YZJ_BOT_AGENT || '').trim()) bad.push(...createBot({ root, env: values }).doctorProblems());
219
+ bad.forEach(say);
220
+ return bad.length;
221
+ }
222
+
223
+ // 5 疑似密钥:在工作目录里查会入库的六个目录(01-inbox/ 只是原料,不算);在仓库里查整个仓库。只报位置,不打印内容。
224
+ // 在工作目录里扫会入库的六个目录;在仓库里扫整个仓库(它全部都会入库,scripts/test/ 里故意的假样例除外)
225
+ // yzjtoken 是云之家机器人 Webhook 地址里的凭据;机器人会话的第一条消息里就有完整地址,容易被人顺手抄进备注或笔记
226
+ const SECRET = /sk-[A-Za-z0-9_-]{16,}|appSecret|accessToken|password\s*[:=]|BEGIN [A-Z ]*PRIVATE KEY|yzjtoken=[0-9A-Za-z]{8,}/;
227
+
228
+ function checkSecrets({ root, packageRoot, say }) {
229
+ const hits = [];
230
+ const dev = path.resolve(root) === path.resolve(packageRoot);
231
+ const scan = (file) => {
232
+ const text = readText(file);
233
+ if (text.includes('\0')) return; // 不是文本的不管
234
+ text.split('\n').forEach((line, n) => {
235
+ if (SECRET.test(line)) hits.push(`${path.relative(root, file)}:${n + 1}`);
236
+ });
237
+ };
238
+ if (dev) {
239
+ // 自测里的假 token 是故意写的样例,不扫;这条正则本身(写在这个文件里)也不扫
240
+ const tests = path.join(root, 'scripts', 'test');
241
+ for (const file of walkFiles(root, SKIP_DIRS, 0)) {
242
+ if (file.startsWith(`${tests}${path.sep}`) || file === __filename) continue;
243
+ scan(file);
244
+ }
245
+ } else {
246
+ for (const dir of ['00-config', '02-daily', '03-carryover', '04-patterns', '05-goals', '.agents']) {
247
+ for (const file of walkFiles(path.join(root, dir), SKIP_DIRS, 0)) scan(file);
248
+ }
249
+ }
250
+ if (hits.length) {
251
+ say('疑似密钥,先删掉再提交(下面只是位置):');
252
+ hits.forEach(say);
253
+ }
254
+ return hits.length;
255
+ }
256
+
257
+ // 6 笔记格式。.agents/notes/ 下的两类笔记。路径是 <类别>/yyyy-mm-dd-<英文短横线标题>.md;
258
+ // 踩坑笔记带 Verified(怎么重新确认坑还在)、Origin(在哪、怎么发现的)和固定的三节,决策笔记带 Decided 和四节。规则见 .agents/notes/AGENTS.md。
259
+ function checkNotes({ root, say }) {
260
+ const bad = [];
261
+ const need = (file, what) => bad.push(`笔记 ${file}:${what}`);
262
+ for (const [kind, dateKey, sections] of [
263
+ ['pitfalls', null, ['现象', '原因', '怎么做']],
264
+ ['decisions', 'Decided', ['Problem', 'Decision', 'Alternatives considered', 'Consequences']],
265
+ ]) {
266
+ const dir = path.join(root, '.agents', 'notes', kind);
267
+ for (const name of listDir(dir, true).sort()) {
268
+ const rel = `.agents/notes/${kind}/${name}`;
269
+ if (!/^[0-9]{4}-[0-9]{2}-[0-9]{2}-[a-z0-9][a-z0-9-]*\.md$/.test(name)) {
270
+ need(rel, '文件名应为 yyyy-mm-dd-<英文短横线标题>.md');
271
+ continue;
272
+ }
273
+ const lines = readText(path.join(dir, name)).split('\n');
274
+ const head = lines.slice(0, 6);
275
+ if (kind === 'pitfalls') {
276
+ if (!head.some((l) => /^Verified: [0-9]{4}-[0-9]{2}-[0-9]{2}/.test(l))) need(rel, '前 6 行里没有「Verified: yyyy-mm-dd · <重新确认这个坑还在的方法>」');
277
+ if (!head.some((l) => /^Origin: [0-9]{4}-[0-9]{2}-[0-9]{2}/.test(l))) need(rel, '前 6 行里没有「Origin: yyyy-mm-dd · <在哪踩的> · <怎么发现的>」');
278
+ } else if (!head.some((l) => new RegExp(`^${dateKey}: [0-9]{4}-[0-9]{2}-[0-9]{2}`).test(l))) {
279
+ need(rel, '前 6 行里没有「Decided: yyyy-mm-dd」');
280
+ }
281
+ for (const s of sections) {
282
+ if (!lines.includes(`## ${s}`)) need(rel, `缺少小节「## ${s}」`);
283
+ }
284
+ }
285
+ }
286
+ bad.forEach(say);
287
+ return bad.length;
288
+ }
289
+
290
+ // 模板(只在仓库里查):templates/ 下每份都在、profile 是空白状态、数据源全关、记录里没有行
291
+ function checkTemplates({ packageRoot, say }) {
292
+ const bad = templateProblems(packageRoot);
293
+ bad.forEach(say);
294
+ return bad.length;
295
+ }
296
+
297
+ // 仓库保持通用(只在仓库里查):这个仓库只放通用的 skill 和 CLI,不是工作目录——根目录不该出现工作目录才有的东西。
298
+ // 个人数据(身份、群、作息、渠道)只放进用户自己的工作目录;工作目录独有的文件只在 templates/ 下有
299
+ function genericProblems({ root, say }) {
300
+ const bad = [];
301
+ for (const rel of ['00-config', '02-daily', '03-carryover', '04-patterns', '05-goals', 'CLAUDE.md']) {
302
+ if (fs.existsSync(path.join(root, rel))) bad.push(`仓库根目录里出现了 ${rel}:这个仓库只放 skill 和 CLI,不是工作目录。工作目录独有的文件在 templates/ 下;试跑请先用 work-rhythm init 建一个临时工作目录`);
303
+ }
304
+ if (fs.existsSync(path.join(root, 'collectors', 'local.env'))) bad.push('仓库里出现了个人配置 collectors/local.env:个人的 profile 和 local.env 只放在自己的工作目录里');
305
+ bad.forEach(say);
306
+ return bad.length;
307
+ }
308
+
309
+ // 顺序:前三项两边都有;工作目录里接着查配置,仓库里接着查模板;最后两项两边都有,仓库里再多一项
310
+ const COMMON_HEAD = [['脚本语法', checkSyntax], ['文档内链', checkLinks], ['预算', checkBudgets]];
311
+ const COMMON_TAIL = [['疑似密钥', checkSecrets], ['笔记格式', checkNotes]];
312
+
313
+ async function run(args, ctx) {
314
+ if (args.length) throw new UsageError('用法: work-rhythm check');
315
+ const { root, packageRoot, stdout, env } = ctx;
316
+ const say = (text = '') => stdout.write(`${text}\n`);
317
+ const dev = path.resolve(root) === path.resolve(packageRoot);
318
+ const steps = dev
319
+ ? [...COMMON_HEAD, ['模板', checkTemplates], ...COMMON_TAIL, ['仓库保持通用', genericProblems]]
320
+ : [...COMMON_HEAD, ['配置自检', checkConfig], ...COMMON_TAIL];
321
+ let bad = 0;
322
+ for (const [i, [title, check]] of steps.entries()) {
323
+ say(`== ${i + 1}/${steps.length} ${title} ==`);
324
+ bad += check({ root, packageRoot, env: settings(root, env), say });
325
+ }
326
+ say(bad ? `${bad} 项未通过` : '全部通过');
327
+ return bad ? 1 : EXIT.OK;
328
+ }
329
+
330
+ module.exports = { usage: '用法: work-rhythm check 改完脚本、配置或文档后跑一遍,只读', run, __test: { genericProblems, checkTemplates } };
@@ -0,0 +1,25 @@
1
+ // 直接跑一个采集器:任务指令里「取今日日程」「取告警」这类只要某一个源的数据时用
2
+ // 用法: work-rhythm collect <源id> <输出目录> [采集器自己的参数…,一般是起始日期 YYYY-MM-DD]
3
+ // 输出目录写成 01-inbox/<源id>/<日期>:和 im-yzj 共用脚本的源(如 ops-yzj)靠它认出自己该读哪个变量。
4
+ // sources.yaml 里有的源按它登记的采集器跑,没有的把 <源id> 当成内置采集器的 id
5
+ 'use strict';
6
+ const path = require('node:path');
7
+ const { EXIT, UsageError } = require('../lib/run');
8
+ const { loadSources } = require('../lib/config');
9
+ const { resolveCollector, spawnCollector } = require('./sync');
10
+
11
+ module.exports = {
12
+ usage: '用法: work-rhythm collect <源id> <输出目录> [起始日期]',
13
+ async run(args, ctx) {
14
+ const { root, env, packageRoot } = ctx;
15
+ const [id, out] = args;
16
+ if (!id || !out) throw new UsageError(module.exports.usage);
17
+ const source = loadSources(root).find((s) => s.id === id);
18
+ const collector = resolveCollector(root, packageRoot, source ? source.collector : id);
19
+ if (!collector) throw new UsageError(`没有 ${id} 这个采集器,sources.yaml 里也没有这个源`);
20
+ const r = spawnCollector(collector, [path.resolve(out), ...args.slice(2)], { cwd: root, env: { ...env, WORK_RHYTHM_ROOT: root } });
21
+ if (r.stdout) ctx.stdout.write(r.stdout);
22
+ if (r.stderr) ctx.stderr.write(r.stderr);
23
+ return r.code === 0 ? EXIT.OK : r.code > 0 && r.code < 126 ? r.code : EXIT.FAILED;
24
+ },
25
+ };