work-rhythm 0.1.7 → 0.1.8

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.
@@ -25,6 +25,7 @@
25
25
  | 验证一条推送有没有送到,或想把机器人会话加进订阅 | [刚发的消息搜不到](pitfalls/2026-11-30-message-search-lags-after-sending.md) |
26
26
  | 改模型 / 思考强度的配置、`agent models`,或想知道 zcode 为什么配不了模型 | [绑定时问模型和思考强度](decisions/2026-12-01-agent-binding-asks-for-model-and-effort.md) |
27
27
  | 改引导第 6 步、`work-rhythm agent`、预置的 agent(`scripts/lib/agent.js`),或想知道为什么不往运行时的调度器建任务 | "+NEW+" |
28
+ | 改 launchd 的安装(`scripts/lib/launchd.js`)、plist 里绑哪个 node,或从桌面 agent 里装完服务起不来 | [装常驻服务要绑真正的 node](pitfalls/2026-12-01-launchd-install-bakes-invoking-node.md) |
28
29
  | 改值守跑在哪(服务进程还是单独进程)、`listen install` / `rhythm restart`,或想知道为什么 launchd 里只有一项 | [值守并进本地服务,本机只留一个后台进程](decisions/2026-12-01-bot-lives-in-the-service-process.md) |
29
30
  | 改会话绑定(`YZJ_BOT_SESSION`、`agent session` / `sessions`),或想知道机器人为什么不接上下文 | [绑 agent 时把当前会话也绑上](decisions/2026-12-01-binding-carries-the-current-session.md) |
30
31
  | 改值守(`scripts/lib/bot.js`、`scripts/lib/agent.js`),想换一个回复的 agent,或要改值守的权限默认值 | [值守用长连接收消息](decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md) |
@@ -0,0 +1,28 @@
1
+ # 装常驻服务要绑真正的 node,不能绑 agent 运行时那个 Electron 二进制
2
+
3
+ Verified: 2026-12-01 · 在 DSH 里跑 `work-rhythm rhythm install`,再 `launchctl list | grep work-rhythm` 看到 PID 是 `-`、状态是 `5`,`01-inbox/_rhythm.launchd.log` 里是 `Unable to find helper app`
4
+ Origin: 2026-12-01 · 个人工作目录 `/Users/kingdee/workspace/my-work` 的引导第 6 步,从 DSH(DeepSeek Harness)里装的 · 实际出错(装完 `rhythm status` 立刻报「没在跑」)
5
+
6
+ ## 现象
7
+
8
+ `rhythm install` 报「已装成常驻」并给出页面地址,但紧接着 `rhythm status` 是「没在跑」。`launchctl list` 里那一行是 `- -5 work-rhythm.service.my-work`(PID 空、上次退出码 5),启动日志只有一行:
9
+
10
+ ```
11
+ [1201/121626.592752:FATAL:electron/shell/app/electron_main_delegate_mac.mm:66] Unable to find helper app
12
+ ```
13
+
14
+ plist 的 `ProgramArguments` 第一个参数是 `/Users/kingdee/.dsh-desktop/sidecar-node/<hash>/DSH Node.app/Contents/MacOS/DSH Node`。同一个二进制在终端里手动跑 `rhythm start` 是好的。
15
+
16
+ ## 原因
17
+
18
+ `scripts/lib/launchd.js` 把 `process.execPath` 原样写进 plist——谁执行 `install`,服务就绑谁那个 node。从 DSH、ZCode 这类桌面 agent 里跑时,`process.execPath` 是它们自带的 Electron 包(形如 `…/DSH Node.app/Contents/MacOS/DSH Node`)。那个二进制在终端里有父进程和 app bundle 上下文,找得到 `DSH Node Helper.app`;被 launchd 直接拉起时找不到 helper,启动即退出,退出码 5。
19
+
20
+ `rhythm doctor` 不校验 plist 里的可执行文件,`rhythm status` 只报「在跑 / 没在跑」,所以安装命令本身是「成功」的,问题要等看 launchctl 和 launchd 日志才暴露。
21
+
22
+ ## 怎么做
23
+
24
+ `scripts/lib/launchd.js` 的 `nodeForLaunchd()`:`process.execPath` 看着像 Electron(路径里有 `.app/`,或者设了 `ELECTRON_RUN_AS_NODE`)就另找一个真正的 node(PATH 上的,再 homebrew / usr/local 的常见位置)。装完 `install` 会等几秒核对 launchd 真的把它拉起来了,没有就带着 `launchd.log` 的最后几行报错;`rhythm doctor` 也会报「plist 里绑的不是真正的 node」。
25
+
26
+ 装之前被 DSH 的工具环境影响过的话,`install` 一样能装对;已经装坏的,`work-rhythm rhythm uninstall` 再 `install` 一次。装完核对:`work-rhythm rhythm status` 显示 `在跑(pid …)`,plist 的 `ProgramArguments` 第一项是 `/opt/homebrew/Cellar/node/…/bin/node` 这类真 node。
27
+
28
+ 兜底:`rhythm install` 的启动核对,`rhythm doctor` 对 plist 的检查,`scripts/test/rhythm.test.js` 的「装的是服务那一个常驻」用例。原始日志在 `01-inbox/_rhythm.launchd.log`。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "work-rhythm",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "个人工作目录:记录每天的工作事件、按订阅源采集数据、做计划和复盘;带本地服务(定时采集、定时整理、经消息渠道推送、本机网页)",
5
5
  "license": "UNLICENSED",
6
6
  "type": "commonjs",
@@ -100,6 +100,8 @@ module.exports = {
100
100
  const problems = service.doctorProblems();
101
101
  if (problems.length) throw new Fail(EXIT.NOT_CONFIGURED, `值守的开关已打开,但本地服务装不成常驻,先解决:\n${problems.join('\n')}`);
102
102
  service.launchd.install();
103
+ const problem = launchd.startupProblem('service', root, path.join(root, '01-inbox', '_rhythm.launchd.log'));
104
+ if (problem) throw new Fail(EXIT.FAILED, problem);
103
105
  how = '本地服务已装成常驻(定时任务、推送、网页和值守都在这一个进程里)。';
104
106
  }
105
107
  stdout.write(`值守已打开(${path.relative(root, file)} 的 YZJ_BOT_LISTEN=1),跑在本地服务里。${how}\n日志:${path.join(root, '01-inbox', '_bot.log')}\n现在可以在云之家给机器人发一句试试。\n`);
@@ -102,7 +102,10 @@ module.exports = {
102
102
  const plist = service.launchd.install();
103
103
  // 值守现在跑在服务里;以前单独装的常驻收掉,别两个都开着
104
104
  launchd.uninstall('bot', root);
105
+ // 装完看一眼真的起来了:launchd 拉起失败时 bootstrap 也是成功的
106
+ const problem = launchd.startupProblem('service', root, `${root}/01-inbox/_rhythm.launchd.log`);
105
107
  stdout.write(`已装成常驻:${plist}\n页面:${service.baseUrl()} 日志:${root}/01-inbox/_rhythm.log\n值守跟着服务一起跑(collectors/local.env 的 YZJ_BOT_LISTEN=1 才开;work-rhythm listen install 打开)。\n`);
108
+ if (problem) throw new Fail(EXIT.FAILED, problem);
106
109
  return EXIT.OK;
107
110
  }
108
111
  case 'uninstall':
@@ -23,6 +23,26 @@ function bootout(kind, root) {
23
23
 
24
24
  const escapeXml = (s) => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
25
25
 
26
+ // 看着像 Electron 包的可执行文件(…/DSH Node.app/Contents/MacOS/DSH Node):靠 ELECTRON_RUN_AS_NODE 当 node 用,
27
+ // 被 launchd 直接拉起时找不到 helper app,启动即退
28
+ const looksLikeElectron = (file, env) => /\.app\//.test(file) || env.ELECTRON_RUN_AS_NODE === '1' && !/(^|\/)node$/.test(file);
29
+
30
+ // 写进 plist 的 node:要真正的 node。从桌面 agent(DSH、ZCode)的工具里跑 install 时 process.execPath 是它们自带的 Electron 包,
31
+ // 所以 execPath 看着像 Electron 就另找一个:先 PATH 上的 node,再 homebrew / usr/local 的常见位置。都找不到才用它,并让 doctor 报出来
32
+ function nodeForLaunchd(env = process.env) {
33
+ const current = process.execPath;
34
+ if (!looksLikeElectron(current, env)) return current;
35
+ const onPath = spawnSync('/usr/bin/which', ['node'], { encoding: 'utf8', env });
36
+ const candidates = [onPath.status === 0 ? onPath.stdout.trim() : '', '/opt/homebrew/bin/node', '/usr/local/bin/node'];
37
+ return candidates.find((c) => c && fs.existsSync(c) && !looksLikeElectron(c, env)) || current;
38
+ }
39
+
40
+ // 已经装好的 plist 里绑的是什么 node:读 ProgramArguments 的第一项
41
+ function plistNode(plist) {
42
+ const m = /<key>ProgramArguments<\/key>\s*<array>\s*<string>([^<]+)<\/string>/.exec(fs.readFileSync(plist, 'utf8'));
43
+ return m ? m[1] : '';
44
+ }
45
+
26
46
  // 写 plist 并装上。args 是子命令和它的参数(跟在入口后面)。日志写进 logFile
27
47
  function install({ kind, root, entry, args, logFile }) {
28
48
  needLaunchd();
@@ -34,14 +54,14 @@ function install({ kind, root, entry, args, logFile }) {
34
54
  <key>Label</key><string>${escapeXml(label(kind, root))}</string>
35
55
  <key>ProgramArguments</key>
36
56
  <array>
37
- <string>${escapeXml(process.execPath)}</string>
57
+ <string>${escapeXml(nodeForLaunchd())}</string>
38
58
  <string>${escapeXml(entry)}</string>
39
59
  ${args.map((a) => ` <string>${escapeXml(a)}</string>`).join('\n')}
40
60
  </array>
41
61
  <key>WorkingDirectory</key><string>${escapeXml(root)}</string>
42
62
  <key>EnvironmentVariables</key>
43
63
  <dict>
44
- <key>PATH</key><string>${escapeXml(path.dirname(process.execPath))}:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
64
+ <key>PATH</key><string>${escapeXml(path.dirname(nodeForLaunchd()))}:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
45
65
  <key>HOME</key><string>${escapeXml(os.homedir())}</string>
46
66
  <key>LANG</key><string>zh_CN.UTF-8</string>
47
67
  </dict>
@@ -61,6 +81,32 @@ ${args.map((a) => ` <string>${escapeXml(a)}</string>`).join('\n')}
61
81
  return plist;
62
82
  }
63
83
 
84
+ // 装完核对它真的起来了:launchd 拉起失败(绑错了 node、端口被占)时 install 也会报「已装成常驻」,要等几秒看一眼。
85
+ // 返回问题(空串就是起来了)
86
+ function startupProblem(kind, root, logFile, { waitMs = 4000 } = {}) {
87
+ const until = Date.now() + waitMs;
88
+ const syncWait = (ms) => spawnSync(process.execPath, ['-e', `setTimeout(() => {}, ${ms})`]);
89
+ let last = '';
90
+ for (;;) {
91
+ const s = status(kind, root);
92
+ if (s.running) return '';
93
+ last = tail(readText(logFile), 300);
94
+ if (Date.now() >= until) break;
95
+ syncWait(500);
96
+ }
97
+ const node = plistNode(plistPath(kind, root));
98
+ const why = looksLikeElectron(node, {}) ? `plist 里绑的不是真正的 node(${node})` : '';
99
+ return `装是装上了,但 launchd 没能把它拉起来${why ? `:${why}` : ''}。先看 ${logFile}${last ? `,最后几行是:${last}` : ''}`;
100
+ }
101
+
102
+ const readText = (file) => {
103
+ try {
104
+ return fs.readFileSync(file, 'utf8');
105
+ } catch {
106
+ return '';
107
+ }
108
+ };
109
+
64
110
  function uninstall(kind, root) {
65
111
  needLaunchd();
66
112
  bootout(kind, root);
@@ -86,4 +132,4 @@ function status(kind, root) {
86
132
  return { running: r.status === 0, pid: pid ? Number(pid[1]) : 0 };
87
133
  }
88
134
 
89
- module.exports = { label, plistPath, install, uninstall, kickstart, status };
135
+ module.exports = { label, plistPath, looksLikeElectron, nodeForLaunchd, plistNode, install, uninstall, kickstart, startupProblem, status };
@@ -374,10 +374,16 @@ function createRhythm({ root, env, packageRoot }) {
374
374
  return md.page(path.basename(full), md.render(fs.readFileSync(full, 'utf8')), navHtml());
375
375
  }
376
376
 
377
- // 配置和依赖的问题,一条一个字符串。strict 为 false 时不把「缺指令文件的任务」算进去:起服务时到点跳过就是了
378
- // strict 为 false 时不把「缺指令文件的任务」算进去;bot 为 false 时不把值守的配置算进去(起服务时值守配不齐只记一笔,不拦着)
377
+ // 配置和依赖的问题,一条一个字符串。strict 为 false 时不把「缺指令文件的任务」算进去;bot 为 false 时不把值守的配置算进去(起服务时值守配不齐只记一笔,不拦着)
379
378
  function doctorProblems({ strict = true, bot = true } = {}) {
380
379
  const problems = [];
380
+ // 已经装成常驻的:plist 里绑的得是真正的 node。从 DSH 这类桌面 agent 里装过的话,绑的是它们的 Electron 包,launchd 拉不起来
381
+ const plist = launchd.plistPath('service', root);
382
+ if (fs.existsSync(plist)) {
383
+ const node = launchd.plistNode(plist);
384
+ if (!node || launchd.looksLikeElectron(node, {})) problems.push(`常驻的 plist 里绑的不是真正的 node(${node || '没读到'}):work-rhythm rhythm uninstall 再 install 一次`);
385
+ else if (!fs.existsSync(node)) problems.push(`常驻的 plist 里绑的 node 不在了(${node}):work-rhythm rhythm uninstall 再 install 一次`);
386
+ }
381
387
  // 值守开着的话,它的配置(agent、钥匙串、我的 openId、渠道)也要齐
382
388
  if (bot && botWanted()) problems.push(...createBot({ root, env }).doctorProblems());
383
389
  for (const [id, specText] of Object.entries(cfg.schedule)) {
@@ -416,7 +416,7 @@ test('listen install / uninstall / status:值守跑在本地服务那一个进
416
416
  write(`${s.box.work}/collectors/local.env`, 'YZJ_MY_OPEN_ID="me-0001"\nYZJ_BOT_AGENT="dsh"\n');
417
417
  const home = path.join(s.box.root, 'home');
418
418
  fs.mkdirSync(path.join(home, 'Library', 'LaunchAgents'), { recursive: true });
419
- const env = { ...s.stubs, HOME: home, STUB_KEYCHAIN: 'ok', STUB_ROBOT_URL: 'http://127.0.0.1:1/send?yzjtoken=stubtoken0123456789abcd' };
419
+ const env = { ...s.stubs, HOME: home, STUB_KEYCHAIN: 'ok', STUB_LAUNCHCTL_RUNNING: '1', STUB_ROBOT_URL: 'http://127.0.0.1:1/send?yzjtoken=stubtoken0123456789abcd' };
420
420
  const agents = path.join(home, 'Library', 'LaunchAgents');
421
421
  // 老的单独常驻还在:install 要把它收掉
422
422
  write(path.join(agents, 'work-rhythm.bot.work.plist'), '<plist/>');
@@ -405,12 +405,24 @@ test('rhythm install:装的是服务那一个常驻,老的单独值守常驻
405
405
  const agents = path.join(home, 'Library', 'LaunchAgents');
406
406
  fs.mkdirSync(agents, { recursive: true });
407
407
  write(path.join(agents, 'work-rhythm.bot.work.plist'), '<plist/>');
408
- const env = { ...s.env, HOME: home };
408
+ const env = { ...s.env, HOME: home, STUB_LAUNCHCTL_RUNNING: '1' };
409
409
  assert.equal(s.rhythm(['install'], env).code, 0, s.rhythm(['install'], env).out);
410
+ // 装完核对过真的起来了(假的 launchctl 说在跑);plist 里绑的是真正的 node
411
+ assert.equal(require('../lib/launchd').plistNode(path.join(agents, 'work-rhythm.service.work.plist')), process.execPath);
412
+ assert.equal(s.rhythm(['doctor'], env).code, 0, s.rhythm(['doctor'], env).out);
413
+ // launchd 说没在跑:install 不报成功,带着日志说清
414
+ const notUp = s.rhythm(['install'], { ...s.env, HOME: home });
415
+ assert.equal(notUp.code, 5);
416
+ assert.match(notUp.stderr, /装是装上了,但 launchd 没能把它拉起来。先看 .*_rhythm\.launchd\.log/);
417
+ // 装坏的 plist(绑的是 DSH 的 Electron 包):doctor 报出来
418
+ write(path.join(agents, 'work-rhythm.service.work.plist'), '<key>ProgramArguments</key><array><string>/Users/x/.dsh-desktop/sidecar-node/1/DSH Node.app/Contents/MacOS/DSH Node</string></array>');
419
+ const bad = s.rhythm(['doctor'], env);
420
+ assert.equal(bad.code, 1);
421
+ assert.match(bad.stdout, /常驻的 plist 里绑的不是真正的 node(.*DSH Node\.app\/Contents\/MacOS\/DSH Node):work-rhythm rhythm uninstall 再 install 一次/);
410
422
  assert.ok(exists(path.join(agents, 'work-rhythm.service.work.plist')));
411
423
  assert.equal(exists(path.join(agents, 'work-rhythm.bot.work.plist')), false, '老的单独值守常驻收掉');
412
424
  assert.match(read(s.env.STUB_LAUNCHCTL), /bootout gui\/\d+\/work-rhythm\.bot\.work\n/);
413
- const restart = s.rhythm(['restart'], env);
425
+ const restart = s.rhythm(['restart'], { ...s.env, HOME: home });
414
426
  assert.equal(restart.code, 3, 'launchd 说没在跑就不重启');
415
427
  assert.match(restart.stderr, /服务不是 launchd 管的常驻/);
416
428
  const again = s.rhythm(['restart'], { ...env, STUB_LAUNCHCTL_RUNNING: '1' });