@tyhld/conductor 0.12.142 → 0.12.143

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.
@@ -8,6 +8,8 @@
8
8
  * 【動くとき】systemd のユーザーサービスから(★sudo なし・番人を通らない)。
9
9
  * ・起動時に1回(tyhld-autoupdate-boot.service・--boot)… 常駐(conductor@)より先に動く
10
10
  * ・以後は数分ごとのタイマー(tyhld-autoupdate.timer)… 手元の目標の版を読むだけ(★管制へは聞きに行かない)
11
+ * ・Mac のお客さまの機は launchd の3つ(com.tyhld.autoupdate.boot・com.tyhld.autoupdate・com.tyhld.selfcheck)
12
+ * から同じ入口を呼ぶ(便 8a0b26eb)。違うのは常駐の起こし直しが launchctl になることと、起動時も通常の回で流すことだけ。
11
13
  *
12
14
  * 【決め(設計 3ed9e2b2 の 1/4・2/4 と管制の口の決まり 2f5eb92c)】
13
15
  * ・鍵(~/.tyhld/bin/autoupdate/enabled)が無い機では何もしない(自社PCは autoupdate-install.sh、
@@ -144,6 +146,10 @@ export function pinPath(home, env) {
144
146
  export function claudeVersionsDir(home) {
145
147
  return path.join(home, '.local', 'share', 'claude', 'versions');
146
148
  }
149
+ /** Mac(launchd)の機か(★ここ1か所で決める)。 */
150
+ function isMac(d) {
151
+ return d.platform === 'darwin';
152
+ }
147
153
  // ─────────────────────────────────────────────────────────────────────────────
148
154
  // 小さな部品
149
155
  // ─────────────────────────────────────────────────────────────────────────────
@@ -435,6 +441,14 @@ async function waitGuardSync(d, before) {
435
441
  * ★失敗しても入れ替えは止めない(記録に残すだけ)。
436
442
  */
437
443
  function alignUnits(d) {
444
+ if (isMac(d)) {
445
+ // ★Mac(launchd)は systemd の常駐の定義を持たない。部品置き場の鍵の受け渡しも Mac ではまだ用意していない
446
+ // (sales-template/setup.sh と同じ決め)。launchd の項目は ~/.tyhld/bin の動かない入口だけを指すので、
447
+ // 版が上がっても書き換える所が無い(入口そのものは refreshEntries がそろえる)。
448
+ const line = '常駐の定義と部品置き場の鍵: Mac(launchd)なので、そろえる物はありません(入口は入口の同期でそろえます)';
449
+ d.log(line);
450
+ return { units: [], reloaded: false, reverted: [], key: { outcome: 'no-source', detail: line }, needRelayRestart: false, lines: [line] };
451
+ }
438
452
  const a = alignUnitsAndKey({ home: d.home, distRoot: d.distRoot, exec: d.exec, env: d.env });
439
453
  for (const line of a.lines)
440
454
  d.log(line);
@@ -575,8 +589,35 @@ export function describeEntrySync(results) {
575
589
  .map((g) => `${label[g.k]}=${g.names.join(',')}`);
576
590
  return `入口と受け口の同期: ${parts.length > 0 ? parts.join('/') : '対象なし'}`;
577
591
  }
592
+ /**
593
+ * Mac(launchd)の常駐のラベルの頭(★sales-template/launchd/install.sh・launchd/ear-install.sh が付ける名前)。
594
+ * 現場ごと com.tyhld.conductor.<現場> と耳 com.tyhld.conductor.ear。★ほかの launchd の項目には触らない。
595
+ */
596
+ export const LAUNCHD_RELAY_PREFIX = 'com.tyhld.conductor.';
597
+ /** launchd の宛先(gui/<利用者の番号>)。 */
598
+ function launchdDomain(d) {
599
+ const uid = d.uid ?? (typeof process.getuid === 'function' ? process.getuid() : 0);
600
+ return `gui/${uid}`;
601
+ }
602
+ /** Mac の常駐(com.tyhld.conductor.*)のラベルの一覧(★launchctl list の3列目だけを読む)。 */
603
+ function launchdRelayLabels(d) {
604
+ const r = d.exec('launchctl', ['list'], { timeoutMs: 15_000 });
605
+ return r.stdout
606
+ .split('\n')
607
+ .map((l) => l.trim().split(/\s+/)[2] ?? '')
608
+ .filter((label) => label.startsWith(LAUNCHD_RELAY_PREFIX) && /^[A-Za-z0-9._-]+$/.test(label));
609
+ }
610
+ /** Mac の常駐が動いているか(★launchctl list <ラベル> の答えに PID が在れば動いている)。 */
611
+ function launchdRelayState(d, label) {
612
+ const r = d.exec('launchctl', ['list', label], { timeoutMs: 10_000 });
613
+ if (r.status !== 0)
614
+ return 'missing';
615
+ return /"PID"\s*=\s*\d+/.test(r.stdout) ? 'active' : 'inactive';
616
+ }
578
617
  /** 常駐(conductor@*・耳・司令塔)の一覧。 */
579
618
  function relayUnits(d) {
619
+ if (isMac(d))
620
+ return launchdRelayLabels(d);
580
621
  const r = d.exec('systemctl', ['--user', 'list-units', 'conductor@*.service', 'conductor-ear.service', 'conductor-build-runner.service', '--all', '--no-legend', '--plain'], { timeoutMs: 15_000 });
581
622
  return r.stdout
582
623
  .split('\n')
@@ -593,12 +634,19 @@ async function restartRelays(d, version) {
593
634
  const units = relayUnits(d);
594
635
  if (units.length === 0)
595
636
  return { ok: true, detail: '常駐がこの機にありません' };
596
- d.exec('systemctl', ['--user', 'try-restart', '--no-block', ...units], { timeoutMs: 30_000 });
637
+ if (isMac(d)) {
638
+ // ★Mac(launchd): 1本ずつ起こし直す(-k=動いていれば止めてから起こす)。名指しは自分の常駐のラベルだけ。
639
+ for (const label of units)
640
+ d.exec('launchctl', ['kickstart', '-k', `${launchdDomain(d)}/${label}`], { timeoutMs: 30_000 });
641
+ }
642
+ else {
643
+ d.exec('systemctl', ['--user', 'try-restart', '--no-block', ...units], { timeoutMs: 30_000 });
644
+ }
597
645
  const rt = path.join(d.home, '.tyhld', 'hooks', 'runtime-source.last');
598
646
  // ★待つ上限は回数で決める(時計が進まない形でも、いつまでも回らない)。
599
647
  const tries = Math.max(1, Math.ceil(RESTART_WAIT_MS / RESTART_POLL_MS));
600
648
  for (let i = 1;; i++) {
601
- const states = units.map((u) => d.exec('systemctl', ['--user', 'is-active', u], { timeoutMs: 10_000 }).stdout.trim());
649
+ const states = units.map((u) => isMac(d) ? launchdRelayState(d, u) : d.exec('systemctl', ['--user', 'is-active', u], { timeoutMs: 10_000 }).stdout.trim());
602
650
  let rtVersion = '';
603
651
  try {
604
652
  rtVersion = readFileSync(rt, 'utf8').match(/\bversion=(\S+)/)?.[1] ?? '';
@@ -1372,8 +1420,12 @@ export async function runAutoupdateCommand(argv) {
1372
1420
  runSelfcheck: selfcheckViaEntry(home, defaultExec, process.env),
1373
1421
  isAlive: isProcessAlive,
1374
1422
  log: (m) => console.log(`[autoupdate] ${m}`),
1375
- boot,
1423
+ // ★Mac(launchd)には systemd の After= に当たる順番が無く、常駐は起動時の1回と同時に起きる。
1424
+ // 「常駐はこの後に新しい版で起きる」が成り立たないので、--boot を付けられても通常の回として流す
1425
+ // (入れ替えたら常駐を起こし直し、自己診断まで行う)。launchd の項目も --boot を付けない。
1426
+ boot: boot && process.platform !== 'darwin',
1376
1427
  relayCycleMs: ((Number.isFinite(interval) && interval > 0 ? interval : 15) + 10) * 1000,
1428
+ platform: process.platform,
1377
1429
  };
1378
1430
  try {
1379
1431
  await runUpdater(d);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tyhld/conductor",
3
- "version": "0.12.142",
3
+ "version": "0.12.143",
4
4
  "description": "采配くん管制の見守りアプリ(conductor-agent)。各PCで常駐し、中央(devlog-tracker)へ定期的に生存報告(heartbeat)を送る常駐CLI。",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1464,12 +1464,12 @@ fi
1464
1464
  # ・★既に自動更新の鍵がある機(自社PC・2つ目の現場を足すとき)は何もしない=既に入っている機は1回だけ入る。
1465
1465
  # ・使用量は増やさない(上限の正本は src/usage-budget.ts。目標の版は中継の control の返事に相乗りで届く)。
1466
1466
  # ・管制がその機の自動更新を入にするまでは、何も入れ替えない(管制が autoUpdate:false を返す)。
1467
- # ・best-effort: 入らなくてもセットアップは成功扱いにする(⑦で知らせる)。Mac(launchd)はまだ用意していない。
1467
+ # ・best-effort: 入らなくてもセットアップは成功扱いにする(⑦で知らせる)。
1468
+ # ・★Mac(launchd)も同じ台本を同じ印で呼ぶ(便 8a0b26eb)。台本が launchd の3つ(ログイン時の1回・
1469
+ # 5分ごと・毎日の自己診断)を置く。Mac 用の別の更新の仕組みは作らない。
1468
1470
  AUTOUPDATE_KEY="$HOME/.tyhld/bin/autoupdate/enabled"
1469
1471
  AUTOUPDATE_INSTALL="$SCRIPT_DIR/../scripts/autoupdate-install.sh"
1470
- if [[ "$OS_KIND" == "mac" ]]; then
1471
- echo "[setup] 自動更新は、Mac ではまだ用意していません(スキップ)"
1472
- elif [[ -f "$AUTOUPDATE_KEY" ]]; then
1472
+ if [[ -f "$AUTOUPDATE_KEY" ]]; then
1473
1473
  echo "[setup] 自動更新: このPCには既に入っています(変えません)"
1474
1474
  elif [[ -f "$AUTOUPDATE_INSTALL" ]]; then
1475
1475
  if APPLY=1 AUTOUPDATE_PROFILE=customer bash "$AUTOUPDATE_INSTALL"; then
@@ -1547,13 +1547,17 @@ if [[ "$OS_KIND" != "mac" ]]; then
1547
1547
  fi
1548
1548
  fi
1549
1549
  # ★自動更新が入っているか(便 8eab821e)。★止めない・✗ に数えない(入らなくても便は今までどおり届く)。
1550
- if [[ "$OS_KIND" != "mac" ]]; then
1551
- if [[ -f "$AUTOUPDATE_KEY" && -f "$HOME/.config/systemd/user/tyhld-autoupdate.timer" ]]; then
1552
- mark ok "自動更新が入っている" ""
1553
- else
1554
- echo " ⚠ 自動更新が入っていない(止めません。版が上がったときに手で入れ直す必要があります)"
1555
- echo " 直し方: conductor setup ${SITE} をもう一度実行し、出てきた案内のとおりにしてください"
1556
- fi
1550
+ # ★Mac は launchd の5分ごとの項目で見る(便 8a0b26eb)。
1551
+ if [[ "$OS_KIND" == "mac" ]]; then
1552
+ AUTOUPDATE_TIMER="$HOME/Library/LaunchAgents/com.tyhld.autoupdate.plist"
1553
+ else
1554
+ AUTOUPDATE_TIMER="$HOME/.config/systemd/user/tyhld-autoupdate.timer"
1555
+ fi
1556
+ if [[ -f "$AUTOUPDATE_KEY" && -f "$AUTOUPDATE_TIMER" ]]; then
1557
+ mark ok "自動更新が入っている" ""
1558
+ else
1559
+ echo " ⚠ 自動更新が入っていない(止めません。版が上がったときに手で入れ直す必要があります)"
1560
+ echo " 直し方: conductor setup ${SITE} をもう一度実行し、出てきた案内のとおりにしてください"
1557
1561
  fi
1558
1562
  # ★試作の型のときだけ増える点検。「締め出しが本当に置かれたか」を字で確かめる。
1559
1563
  if [[ "$PROFILE" == "proto" ]]; then
@@ -18,6 +18,19 @@
18
18
  # ★使用量の上限は src/usage-budget.ts(管制への追加の問い合わせ0回・版が同じ日の取り寄せ0回・
19
19
  # 版が同じ日のお客さまの Claude 呼び出し0回・結果の書き込みは変化時だけ)。
20
20
  #
21
+ # 【Mac(launchd)も同じ台本で入れる(えふさん決定 2026-10-20 夜・便 8a0b26eb)】
22
+ # ★Mac 用の別の更新の仕組みは作らない(同じ鍵・同じ入口・同じ更新係・同じ自己診断・同じ control-update.json)。
23
+ # 違うのは③だけ=systemd の3つと同じ役目を launchd の3つで置く(~/Library/LaunchAgents):
24
+ # com.tyhld.autoupdate.boot … ログイン時に1回(RunAtLoad)。★launchd には After= が無く常駐と同時に起きるので
25
+ # --boot を付けない=入れ替えたら常駐を起こし直し、自己診断まで行う
26
+ # com.tyhld.autoupdate … 5分ごと(StartInterval=tyhld-autoupdate.timer の OnUnitActiveSec と同じ)
27
+ # com.tyhld.selfcheck … 毎日 12:30(StartCalendarInterval=tyhld-selfcheck.timer と同じ)
28
+ # ★plist に書くのは ~/.tyhld/bin の入口の場所と記録の置き場だけ(鍵の値は書かない。入口が ~/.conductor.env を読む)。
29
+ # ★手で書き換えた plist は入れ替えない(前にこの台本が書いた形の控え ~/.tyhld/bin/autoupdate/launchd/ と比べる)。
30
+ # 入れ替えるときは控え(~/.tyhld/bin/autoupdate/launchd-backup/)を取り、読み込みに失敗したら控えへ戻す。
31
+ # この回に置いた鍵も消す(setup.sh が次に打ったとき入れ直せる形に戻す)。
32
+ # ★名指しするのは上の3つのラベルだけ(launchd のほかの項目・常駐 com.tyhld.conductor.* には触らない)。
33
+ #
21
34
  # 【やること】
22
35
  # ⓪ 確かめ … systemd(ユーザー)が使える/入っている采配くん(公開済みの配布物)/接続設定/
23
36
  # 囲いの部品(bwrap・socat)/Claude Code の入り方
@@ -37,6 +50,7 @@
37
50
  # AUTOUPDATE_SYSTEM_BIN_DIRS … ホームの外の claude の置き場(既定 "/usr/local/bin /usr/bin")
38
51
  # CONDUCTOR_PROC_VERSION_FILE … WSL かどうかを見る見本(既定 /proc/version)
39
52
  # AUTOUPDATE_SKIP_SYSTEMD_CHECK=1 … systemd の有無の確かめを飛ばす(作り物の systemctl で流すとき)
53
+ # ★Mac かどうかは uname -s で見る(試験は作り物の uname・launchctl を PATH の先に置く)。
40
54
  set -uo pipefail
41
55
 
42
56
  APPLY="${APPLY:-0}"
@@ -57,6 +71,15 @@ SYSTEM_BIN_DIRS="${AUTOUPDATE_SYSTEM_BIN_DIRS:-/usr/local/bin /usr/bin}"
57
71
  UNITS=(tyhld-autoupdate-boot.service tyhld-autoupdate.service tyhld-autoupdate.timer tyhld-selfcheck.service tyhld-selfcheck.timer)
58
72
  ORDER_DROPIN_NAME='10-tyhld-autoupdate.conf'
59
73
  RELAY_UNITS=(conductor@.service conductor-ear.service conductor-build-runner.service)
74
+ # Mac(launchd)か。★Mac 以外は今までどおり systemd(1文字も変えない)。
75
+ OS_KIND=linux
76
+ [[ "$(uname -s 2>/dev/null)" == "Darwin" ]] && OS_KIND=mac
77
+ LA_DIR="$HOME/Library/LaunchAgents"
78
+ LA_LOG_DIR="$HOME/Library/Logs/tyhld"
79
+ LA_MINE="$AU_DIR/launchd" # この台本が書いた形の控え(★手で書き換えたかを見分ける)
80
+ LA_BAK="$AU_DIR/launchd-backup" # 入れ替える前の控え(★読み込みに失敗したら戻す)
81
+ LA_LABELS=(com.tyhld.autoupdate.boot com.tyhld.autoupdate com.tyhld.selfcheck)
82
+ LA_LEDGER_FILE="$LEDGER_DIR/installed-launchd.txt"
60
83
 
61
84
  info() { echo "[autoupdate-install] $*"; }
62
85
  warn() { echo "[autoupdate-install] 注意: $*" >&2; }
@@ -69,8 +92,14 @@ if [[ "$APPLY" == "1" ]]; then info "モード: 適用"; else info "モード:
69
92
  # ─── ⓪ 確かめ ───────────────────────────────────────────────────────────────
70
93
  info "⓪ 確かめ"
71
94
 
72
- # systemd(ユーザー)
73
- if [[ "${AUTOUPDATE_SKIP_SYSTEMD_CHECK:-0}" != "1" ]]; then
95
+ # systemd(ユーザー)/Mac は launchd
96
+ if [[ "$OS_KIND" == "mac" ]]; then
97
+ if command -v launchctl >/dev/null 2>&1; then
98
+ info " 常駐の仕組み: launchd(Mac)"
99
+ else
100
+ ng "launchctl が見つかりません。macOS が壊れている可能性があります(CC1 に知らせてください)"
101
+ fi
102
+ elif [[ "${AUTOUPDATE_SKIP_SYSTEMD_CHECK:-0}" != "1" ]]; then
74
103
  if ! command -v systemctl >/dev/null 2>&1 || ! systemctl --user show-environment >/dev/null 2>&1; then
75
104
  ng "systemd(ユーザー)が使えません。WSL なら /etc/wsl.conf に [boot] systemd=true が要ります(人の手番・CC1 に知らせてください)"
76
105
  fi
@@ -197,6 +226,8 @@ IS_WSL=0
197
226
  if [[ -r "$PROC" ]] && grep -qi microsoft "$PROC"; then IS_WSL=1; fi
198
227
  # ★お客さまの機では Windows 側の2行を出さない(setup.sh の案内だけにする)。
199
228
  [[ "$PROFILE" == "customer" ]] && IS_WSL=0
229
+ # ★Mac では出さない(WSL の話)。
230
+ [[ "$OS_KIND" == "mac" ]] && IS_WSL=0
200
231
 
201
232
  if (( ${#NG[@]} > 0 )); then
202
233
  echo
@@ -210,8 +241,12 @@ fi
210
241
  if [[ "$APPLY" != "1" ]]; then
211
242
  info "①(予定)鍵を置きます: $KEY"
212
243
  info "②(予定)入口を置きます: $BIN_DIR/tyhld-autoupdate・tyhld-selfcheck(配布物 $DIST_ROOT から)"
213
- info "③(予定)systemd: ${UNITS[*]}(起動時の1回・数分ごと・毎日の自己診断)"
214
- info " (予定)常駐(${RELAY_UNITS[*]})を起動時の1回の後に起こす drop-in"
244
+ if [[ "$OS_KIND" == "mac" ]]; then
245
+ info "③(予定)launchd: ${LA_LABELS[*]}(ログイン時の1回・5分ごと・毎日の自己診断)を $LA_DIR に置きます"
246
+ else
247
+ info "③(予定)systemd: ${UNITS[*]}(起動時の1回・数分ごと・毎日の自己診断)"
248
+ info " (予定)常駐(${RELAY_UNITS[*]})を起動時の1回の後に起こす drop-in"
249
+ fi
215
250
  if [[ "$PROFILE" == "customer" && $native_ok != 1 ]]; then
216
251
  info "④(予定)Claude Code 自身の自動更新はそのまま(公式推奨の1方式ではないので、更新係は Claude Code を入れ替えません)"
217
252
  else
@@ -231,7 +266,9 @@ ledger_add() {
231
266
 
232
267
  # ① 鍵
233
268
  mkdir -p "$AU_DIR"
269
+ KEY_NEW=0
234
270
  if [[ ! -f "$KEY" ]]; then
271
+ KEY_NEW=1
235
272
  # ★profile= は自己診断が「お客さまの機か」を見分ける控え(段の印が届く前だけ使う・src/selfcheck.ts の customerMachine)。
236
273
  printf '%s installed-by=autoupdate-install.sh version=%s profile=%s\n' "$(date '+%Y-%m-%dT%H:%M:%S%z')" "$DIST_VERSION" "$PROFILE" > "$KEY"
237
274
  fi
@@ -250,7 +287,132 @@ for pair in "scripts/tyhld-autoupdate.sh:tyhld-autoupdate" "scripts/tyhld-selfch
250
287
  done
251
288
  info "② 入口: $BIN_DIR/tyhld-autoupdate・$BIN_DIR/tyhld-selfcheck(配布物 $DIST_ROOT・版 $DIST_VERSION)"
252
289
 
253
- # ③ systemd
290
+ # ③ Mac(launchd)… systemd の3つと同じ役目を launchd の3つで置く(便 8a0b26eb)。
291
+ # ★鍵の値は1文字も書かない(入口が ~/.conductor.env を読む)。XML に入れる字は & < > を逃がす。
292
+ # ★sed で逃がす(bash 5.2 の置換は & を「当たった字」と読む。Mac の bash 3.2 とも同じ結果にそろえる)。
293
+ la_xml() { printf '%s' "$1" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g'; }
294
+ la_body() { # la_body <ラベル> … 置く plist の中身を出す
295
+ local label="$1" args sched log nice=''
296
+ local bin; bin="$(la_xml "$BIN_DIR")"
297
+ case "$label" in
298
+ com.tyhld.autoupdate.boot)
299
+ args=" <string>/bin/bash</string>
300
+ <string>$bin/tyhld-autoupdate</string>"
301
+ sched=' <key>RunAtLoad</key><true/>'
302
+ log=autoupdate ;;
303
+ com.tyhld.autoupdate)
304
+ args=" <string>/bin/bash</string>
305
+ <string>$bin/tyhld-autoupdate</string>"
306
+ sched=' <key>RunAtLoad</key><false/>
307
+ <key>StartInterval</key><integer>300</integer>'
308
+ log=autoupdate; nice=' <key>Nice</key><integer>10</integer>' ;;
309
+ com.tyhld.selfcheck)
310
+ args=" <string>/bin/bash</string>
311
+ <string>$bin/tyhld-selfcheck</string>
312
+ <string>--trigger</string>
313
+ <string>daily</string>"
314
+ sched=' <key>RunAtLoad</key><false/>
315
+ <key>StartCalendarInterval</key>
316
+ <dict>
317
+ <key>Hour</key><integer>12</integer>
318
+ <key>Minute</key><integer>30</integer>
319
+ </dict>'
320
+ log=selfcheck; nice=' <key>Nice</key><integer>10</integer>' ;;
321
+ *) return 1 ;;
322
+ esac
323
+ cat <<PLIST
324
+ <?xml version="1.0" encoding="UTF-8"?>
325
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
326
+ <!-- tyhld: 自動更新(autoupdate-install.sh が置いた・鍵の値は書かない)。★手で書き換えると、次からは入れ替えません。 -->
327
+ <plist version="1.0">
328
+ <dict>
329
+ <key>Label</key><string>$label</string>
330
+ <key>ProgramArguments</key>
331
+ <array>
332
+ $args
333
+ </array>
334
+ <key>EnvironmentVariables</key>
335
+ <dict>
336
+ <key>PATH</key><string>/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
337
+ </dict>
338
+ <key>WorkingDirectory</key><string>$(la_xml "$HOME")</string>
339
+ $sched
340
+ ${nice:+$nice
341
+ } <key>ProcessType</key><string>Background</string>
342
+ <key>StandardOutPath</key><string>$(la_xml "$LA_LOG_DIR")/$log.log</string>
343
+ <key>StandardErrorPath</key><string>$(la_xml "$LA_LOG_DIR")/$log.log</string>
344
+ </dict>
345
+ </plist>
346
+ PLIST
347
+ }
348
+ la_load() { # la_load <ラベル> … 読み込み直す(★名指しのラベルだけ)
349
+ local label="$1" plist="$LA_DIR/$1.plist" dom
350
+ dom="gui/$(id -u)"
351
+ launchctl bootout "$dom/$label" >/dev/null 2>&1 || true
352
+ launchctl bootstrap "$dom" "$plist" >/dev/null 2>&1 && return 0
353
+ # 古い macOS では bootstrap が使えないことがあるので、従来の load へ落とす(sales-template/launchd/install.sh と同じ)。
354
+ launchctl unload "$plist" >/dev/null 2>&1 || true
355
+ launchctl load -w "$plist" >/dev/null 2>&1
356
+ }
357
+ la_install_one() { # la_install_one <ラベル> … 戻り 0=置いた・同じ・手で直してあるので触らない/1=失敗(控えへ戻した)
358
+ local label="$1" dst="$LA_DIR/$1.plist" mine="$LA_MINE/$1.plist" new="$LA_MINE/$1.plist.new" bak="$LA_BAK/$1.plist"
359
+ mkdir -p "$LA_DIR" "$LA_LOG_DIR" "$LA_MINE" "$LA_BAK" || return 1
360
+ la_body "$label" > "$new" || return 1
361
+ if [[ -f "$dst" ]] && cmp -s "$new" "$dst"; then
362
+ mv -f "$new" "$mine"
363
+ # 同じ形。読み込まれていなければ読み込む(★読み込まれていれば何もしない=何度打っても同じ)。
364
+ launchctl list "$label" >/dev/null 2>&1 && { info " $label: 同じ形が読み込まれています"; return 0; }
365
+ la_load "$label" && { info " $label: 読み込みました"; return 0; }
366
+ warn "$label を読み込めませんでした"
367
+ return 1
368
+ fi
369
+ if [[ -f "$dst" ]] && ! { [[ -f "$mine" ]] && cmp -s "$mine" "$dst"; }; then
370
+ # ★前にこの台本が書いた形と違う=人が手で書き換えた物。入れ替えない(知らせるだけ)。
371
+ rm -f "$new"
372
+ warn "$dst は手で書き換えられているので、入れ替えません(人の確認。元へ戻すときはこのファイルを消してから、もう一度打ってください)"
373
+ return 0
374
+ fi
375
+ local had=0
376
+ if [[ -f "$dst" ]]; then cp -p "$dst" "$bak" || return 1; had=1; fi
377
+ cp -f "$new" "$dst.tmp" && mv -f "$dst.tmp" "$dst" || { rm -f "$dst.tmp" "$new"; return 1; }
378
+ if la_load "$label"; then
379
+ mv -f "$new" "$mine"
380
+ rm -f "$bak"
381
+ info " $label: 置いて読み込みました"
382
+ return 0
383
+ fi
384
+ # ★失敗 → 控えへ戻す(無ければ置いた物を外す)。
385
+ rm -f "$new"
386
+ if (( had == 1 )); then
387
+ cp -p "$bak" "$dst" && rm -f "$bak"
388
+ la_load "$label" >/dev/null 2>&1 || true
389
+ warn "$label を読み込めなかったので、入れ替える前の控えへ戻しました"
390
+ else
391
+ launchctl bootout "gui/$(id -u)/$label" >/dev/null 2>&1 || true
392
+ rm -f "$dst"
393
+ warn "$label を読み込めなかったので、置いた物を外しました"
394
+ fi
395
+ return 1
396
+ }
397
+ if [[ "$OS_KIND" == "mac" ]]; then
398
+ la_failed=0
399
+ for l in "${LA_LABELS[@]}"; do
400
+ la_install_one "$l" || la_failed=1
401
+ done
402
+ if (( la_failed == 1 )); then
403
+ # ★この回に置いた鍵を消す(鍵が残ると setup.sh が「既に入っています」で飛ばし、入れ直せない)。
404
+ (( KEY_NEW == 1 )) && rm -f "$KEY"
405
+ warn "launchd の項目を読み込めませんでした(★ここで止めます。もう一度打てば入れ直します)"
406
+ exit 1
407
+ fi
408
+ mkdir -p "$LEDGER_DIR"
409
+ touch "$LA_LEDGER_FILE"
410
+ for l in "${LA_LABELS[@]}"; do grep -qxF "$l" "$LA_LEDGER_FILE" || echo "$l" >> "$LA_LEDGER_FILE"; done
411
+ info "③ launchd: ${LA_LABELS[*]}(ログイン時の1回・5分ごと・毎日の自己診断・記録は $LA_LOG_DIR)"
412
+ fi
413
+
414
+ # ③ systemd(★Mac 以外)
415
+ if [[ "$OS_KIND" != "mac" ]]; then
254
416
  mkdir -p "$UNIT_DIR"
255
417
  for u in "${UNITS[@]}"; do
256
418
  cp -f "$DIST_ROOT/systemd/$u" "$UNIT_DIR/$u"
@@ -271,6 +433,7 @@ done
271
433
  for u in "${UNITS[@]}"; do ledger_add "$u"; done
272
434
  loginctl enable-linger "${USER:-$(id -un)}" >/dev/null 2>&1 || true
273
435
  info "③ systemd: ${UNITS[*]}(常駐は起動時の1回の後に起きます)"
436
+ fi
274
437
 
275
438
  # ④ Claude Code 自身の自動更新を止める(★上げるのは更新係1つだけ)
276
439
  # ★お客さまの機で Claude Code が公式推奨の1方式でないときは止めない(更新係が入れ替えないので、
@@ -98,7 +98,9 @@ dist_pkg_candidates() {
98
98
  for d in "$h/.npm-global/lib/node_modules/$CONDUCTOR_PKG" \
99
99
  "$h/.local/lib/node_modules/$CONDUCTOR_PKG" \
100
100
  "/usr/local/lib/node_modules/$CONDUCTOR_PKG" \
101
- "/usr/lib/node_modules/$CONDUCTOR_PKG"; do
101
+ "/usr/lib/node_modules/$CONDUCTOR_PKG" \
102
+ "/opt/homebrew/lib/node_modules/$CONDUCTOR_PKG"; do
103
+ # ★最後の1つは Apple Silicon の Mac の Homebrew の置き場(便 8a0b26eb)。ほかの機には無い=何も変わらない。
102
104
  dist_pkg_usable "$d" && printf '%s\n' "$d"
103
105
  done
104
106
  return 0
@@ -9,6 +9,8 @@
9
9
  # tyhld-autoupdate-boot.service … ~/.tyhld/bin/tyhld-autoupdate --boot(起動時に1回・常駐より先)
10
10
  # tyhld-autoupdate.service … ~/.tyhld/bin/tyhld-autoupdate(タイマーから数分ごと。★管制へは聞きに行かず、
11
11
  # 中継が control の相乗りで受け取った手元の目標の版を読むだけ・便 8eab821e)
12
+ # Mac(launchd・便 8a0b26eb)… com.tyhld.autoupdate.boot(ログイン時1回)・com.tyhld.autoupdate(5分ごと)が
13
+ # ~/.tyhld/bin/tyhld-autoupdate を呼ぶ(★--boot は付けない=常駐と同時に起きるため)
12
14
  # 終了コード: 0=いつも(更新しなかった・失敗した も 0。理由は journal と控えに残る)/2=使い方の誤り/
13
15
  # 78=動かす采配くんが見つからない
14
16
  #
@@ -49,7 +51,7 @@ if [[ ( -z "${CONDUCTOR_URL:-}" || -z "${CONDUCTOR_TOKEN:-}" ) && -r "$ENV_FILE"
49
51
  done < "$ENV_FILE"
50
52
  fi
51
53
 
52
- # ★同時に2つ動かさない(flock が無い機では、そのまま1つとして動かす)。
54
+ # ★同時に2つ動かさない。
53
55
  LOCK="${HOME:-}/.tyhld/bin/autoupdate/lock"
54
56
  if command -v flock >/dev/null 2>&1; then
55
57
  exec 9>"$LOCK" || exit 0
@@ -57,6 +59,22 @@ if command -v flock >/dev/null 2>&1; then
57
59
  echo "[tyhld-autoupdate] ほかの更新係が動いているので、今回は何もしません。"
58
60
  exit 0
59
61
  fi
62
+ else
63
+ # ★flock が無い機(Mac は標準で入っていない・便 8a0b26eb)は、札のフォルダを作れた1つだけが動く。
64
+ # 中の pid が生きていなければ(電源断などで残った札)取り直す。終わったら札を外す。
65
+ LOCK_DIR="$LOCK.d"
66
+ if ! mkdir "$LOCK_DIR" 2>/dev/null; then
67
+ held="$(cat "$LOCK_DIR/pid" 2>/dev/null || true)"
68
+ if [[ "$held" =~ ^[0-9]+$ ]] && kill -0 "$held" 2>/dev/null; then
69
+ echo "[tyhld-autoupdate] ほかの更新係が動いているので、今回は何もしません。"
70
+ exit 0
71
+ fi
72
+ rm -f "$LOCK_DIR/pid"
73
+ rmdir "$LOCK_DIR" 2>/dev/null || true
74
+ mkdir "$LOCK_DIR" 2>/dev/null || { echo "[tyhld-autoupdate] ほかの更新係が動いているので、今回は何もしません。"; exit 0; }
75
+ fi
76
+ echo "$$" > "$LOCK_DIR/pid"
77
+ trap 'rm -f "$LOCK_DIR/pid"; rmdir "$LOCK_DIR" 2>/dev/null' EXIT
60
78
  fi
61
79
 
62
80
  "$DIST_NODE" "$DIST_ROOT/dist/cli.js" autoupdate "$@"