@cocorograph/hub-agent 0.5.16 → 0.5.17

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/main.mjs +42 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocorograph/hub-agent",
3
- "version": "0.5.16",
3
+ "version": "0.5.17",
4
4
  "description": "Hub Hosted Cockpit のローカル常駐 agent。Hub と outbound WSS で接続し、ローカルの tmux/pty を中継する。",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
package/src/main.mjs CHANGED
@@ -17,7 +17,7 @@ import path from "node:path"
17
17
 
18
18
  import pino from "pino"
19
19
 
20
- import { readConfig } from "./config.mjs"
20
+ import { readConfig, writeConfig } from "./config.mjs"
21
21
  import { loadPlugins, runHookBroadcast, runHookChain } from "./plugin-loader.mjs"
22
22
  import { WsClient } from "./ws-client.mjs"
23
23
  import { PtyBridge } from "./pty-bridge.mjs"
@@ -302,15 +302,56 @@ function startStateLoop({ client, plugins, logger, intervalMs }) {
302
302
  * `pty.*` は PtyBridge にそのまま流す。`tmux.exec` は Sprint E で tmux 抽象に
303
303
  * 渡す予定なので、現状は plugin hook chain だけ走らせて log に残す。
304
304
  */
305
+ /**
306
+ * Hub から `ready` / `ack` で配信された Claude CLI 設定を agent.json と
307
+ * in-memory config に反映する (Phase 3 live-sync)。
308
+ *
309
+ * 旧設計 (Phase 2) では enrollment 時のみ取得していたため、cockpit 管理画面で
310
+ * 設定を変更しても既存 agent には再 enroll しない限り反映されなかった。Phase 3 で
311
+ * 全 WS 接続/heartbeat 毎に最新値が同期されるようにした。
312
+ *
313
+ * 値が変化していない場合は writeConfig を呼ばない (書込負荷軽減)。
314
+ * 失敗しても agent は止めず log だけ出して継続。
315
+ */
316
+ export async function maybeSyncClaudeSettings(msg, ctx) {
317
+ if (typeof msg?.claude_model !== "string" && typeof msg?.claude_permission_mode !== "string") {
318
+ return
319
+ }
320
+ const nextModel = typeof msg.claude_model === "string" ? msg.claude_model : ctx.config.claude_model || ""
321
+ const nextMode = typeof msg.claude_permission_mode === "string" ? msg.claude_permission_mode : ctx.config.claude_permission_mode || ""
322
+ const prevModel = ctx.config.claude_model || ""
323
+ const prevMode = ctx.config.claude_permission_mode || ""
324
+ if (nextModel === prevModel && nextMode === prevMode) return
325
+ ctx.config.claude_model = nextModel
326
+ ctx.config.claude_permission_mode = nextMode
327
+ try {
328
+ await writeConfig({
329
+ agent_id: ctx.config.agent_id,
330
+ agent_token: ctx.config.agent_token,
331
+ hub_url: ctx.config.hub_url,
332
+ claude_model: nextModel,
333
+ claude_permission_mode: nextMode,
334
+ })
335
+ ctx.logger.info(
336
+ { claude_model: nextModel, claude_permission_mode: nextMode },
337
+ "claude settings synced from hub",
338
+ )
339
+ } catch (err) {
340
+ ctx.logger.warn({ err: err.message }, "failed to persist claude settings to agent.json")
341
+ }
342
+ }
343
+
305
344
  async function dispatch(msg, ctx) {
306
345
  const t = msg?.type || ""
307
346
  try {
308
347
  switch (t) {
309
348
  case "ready":
310
349
  ctx.logger.info({ msg }, "hub ready")
350
+ await maybeSyncClaudeSettings(msg, ctx)
311
351
  return
312
352
  case "ack":
313
353
  ctx.logger.debug({ echo: msg.echo }, "hub ack")
354
+ await maybeSyncClaudeSettings(msg, ctx)
314
355
  return
315
356
  case "error":
316
357
  ctx.logger.warn({ msg }, "hub error")