@cocorograph/hub-agent 0.8.87 → 0.8.89

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocorograph/hub-agent",
3
- "version": "0.8.87",
3
+ "version": "0.8.89",
4
4
  "description": "Hub Hosted Cockpit のローカル常駐 agent。Hub と outbound WSS で接続し、ローカルの Claude / Codex チャットと tmux セッションを中継する。",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -50,6 +50,29 @@ function idleRank(item) {
50
50
  return typeof item?.idle_ms === "number" && item.idle_ms >= 0 ? item.idle_ms : Infinity
51
51
  }
52
52
 
53
+ /**
54
+ * この記述子が、その cwd の表示信号の持ち主 (= 代表会話) か。
55
+ *
56
+ * ⚠️ 畳み込みで信号の持ち主を落とすと、frontend (chatSessionsAdapter.ts の
57
+ * signalBelongsTo) が「別の会話の信号」として捨て、生成中でも turn_active=false の赤に
58
+ * なる。idle_ms は attach 中の端末の活性からしか作られず (claude-stream-bridge.mjs
59
+ * `_describeCandidate`)、ワークスペースを離れると -1 になるので、idle_ms だけで選ぶと
60
+ * 離れた後の生成中の会話がほぼ必ず負ける (2026-09-22 に実測)。
61
+ *
62
+ * 会話 ID が片方でも未確定なら持ち主とは言えないので false (従来の idle_ms 規則に委ねる)。
63
+ * 信号の取得失敗も false。失敗の記録は attachChatSignal が行うので、ここでは黙る。
64
+ */
65
+ function ownsChatSignal(item, cwd, getChatSignal) {
66
+ const id = conversationIdOf(item)
67
+ if (!id || !cwd) return false
68
+ try {
69
+ const sig = getChatSignal(cwd, item.kind)
70
+ return typeof sig?.sessionId === "string" && sig.sessionId === id
71
+ } catch {
72
+ return false
73
+ }
74
+ }
75
+
53
76
  /**
54
77
  * 記述子の会話 ID。claude は `session_id`、codex は `thread_id` に入る。
55
78
  * まだ確定していない会話 (初回入力前の attach 等) は両方とも欠けるので null を返す。
@@ -107,6 +130,23 @@ export async function buildChatSessionsSnapshot({
107
130
  const descriptors = describeLiveSessionsUnion(bridges)
108
131
  const { targets, errors } = await listChatTargets({ logger })
109
132
  const targetByCwd = chatTargetsByCwd(targets)
133
+ // 畳み込みでは同じ (cwd, kind) の信号を何度も引く (所有者判定 ×2 + 付与)。readRowChatState は
134
+ // ライブ会話の走査を伴うので、この同期区間の中だけで結果 (例外も含む) を共有する。
135
+ // ⚠️ await をまたいで使わない。送信直前の付け直しは refreshChatSessionsSignals が行う。
136
+ const signalMemo = new Map()
137
+ const signalFor = (cwd, kind) => {
138
+ const key = `${kind}\u0000${cwd}`
139
+ if (!signalMemo.has(key)) {
140
+ try {
141
+ signalMemo.set(key, { value: getChatSignal(cwd, kind) })
142
+ } catch (error) {
143
+ signalMemo.set(key, { error })
144
+ }
145
+ }
146
+ const hit = signalMemo.get(key)
147
+ if (hit.error) throw hit.error
148
+ return hit.value
149
+ }
110
150
  const named = descriptors.map((descriptor) => {
111
151
  const item = { ...descriptor }
112
152
  const cwd = typeof item.cwd === "string" ? item.cwd : ""
@@ -122,10 +162,12 @@ export async function buildChatSessionsSnapshot({
122
162
  // 成立していた (同名 worktree は片方しか起動できなかった)。会話を tmux から切り離すと
123
163
  // 両方ライブになれるため、agent 側で畳んで不変条件を維持する。
124
164
  //
125
- // 残す 1 本は「直近に動いた方」。frontend の adaptChatSessions が同名を畳むときと
126
- // 同じ規則 (idle_ms が小さい方、同値なら先勝ち) にして、どちらで畳んでも同じ行が残る。
127
- // ⚠️ 表示シグナルは cwd キーなので、畳んで残った会話のものが正しく付く (畳み込みは
128
- // 行の一意性のためで、帰属のためではない)。
165
+ // 残す 1 本は、まず「その cwd の表示信号の持ち主 (代表会話)」、次に「直近に動いた方」
166
+ // (idle_ms が小さい方、同値なら先勝ち)。
167
+ // ⚠️ 表示シグナルは cwd キーで代表会話の値なので、代表でない会話を残すと frontend が
168
+ // 帰属不一致で信号を捨てて赤に落ちる (ownsChatSignal 参照)。同じ cwd に会話が複数
169
+ // 生きるのは通常運用 (warm 境界は cwd あたり K=3) なので、ここが実際の分岐になる。
170
+ // frontend の adaptChatSessions も同名を畳むが、それは畳まない旧 agent 向けの互換経路。
129
171
  const live = []
130
172
  const indexByName = new Map()
131
173
  for (const { item, cwd, sessionName } of named) {
@@ -137,11 +179,13 @@ export async function buildChatSessionsSnapshot({
137
179
  const prior = indexByName.get(sessionName)
138
180
  if (prior === undefined) {
139
181
  indexByName.set(sessionName, live.length)
140
- live.push(attachChatSignal(item, cwd, getChatSignal, logger))
182
+ live.push(attachChatSignal(item, cwd, signalFor, logger))
141
183
  continue
142
184
  }
143
185
  const kept = live[prior]
144
- const replace = idleRank(item) < idleRank(kept)
186
+ const itemOwns = ownsChatSignal(item, cwd, signalFor)
187
+ const keptOwns = ownsChatSignal(kept, kept.cwd, signalFor)
188
+ const replace = itemOwns !== keptOwns ? itemOwns : idleRank(item) < idleRank(kept)
145
189
  const winner = replace ? item : kept
146
190
  const loser = replace ? kept : item
147
191
  // ⚠️ cwd だけでは衝突の正体が分からない。想定していたのは「別 workspace 配下の同名
@@ -163,7 +207,7 @@ export async function buildChatSessionsSnapshot({
163
207
  },
164
208
  "chat.sessions: session name collision (folded)",
165
209
  )
166
- if (replace) live[prior] = attachChatSignal(item, cwd, getChatSignal, logger)
210
+ if (replace) live[prior] = attachChatSignal(item, cwd, signalFor, logger)
167
211
  }
168
212
  // chat-targets.mjs の fs.readdir(hubProjectsBase()) と同じ絶対パスに揃える(~ は展開しない)。
169
213
  const projects_base = path.resolve(hubProjectsBase())
package/src/main.mjs CHANGED
@@ -1572,7 +1572,10 @@ async function startSessionEventWatcher({ client, logger, readinessTracker }) {
1572
1572
  const data = JSON.parse(text)
1573
1573
  if (!data || typeof data.event !== "string") return
1574
1574
  const at = typeof data.at === "number" ? data.at : Date.now()
1575
- await publishSessionEvent({ client, readinessTracker, logger }, sessionName, data.event, at)
1575
+ // チャットの行は state loop が返答付きの stop を送る。ここでも送ると Hub が完了通知を
1576
+ // 2 通 (定型文だけ + 返答付き) 出すため、送信だけ省く (並び順・readiness は取り込む)。
1577
+ const send = !(data.event === "stop" && chatStopNotifyingSessionNames.has(sessionName))
1578
+ await publishSessionEvent({ client, readinessTracker, logger }, sessionName, data.event, at, { send })
1576
1579
  } catch {
1577
1580
  // 一時欠如 / parse 失敗等は無視 (次の変更で読み直す)
1578
1581
  }
@@ -1613,13 +1616,20 @@ async function startSessionEventWatcher({ client, logger, readinessTracker }) {
1613
1616
  }
1614
1617
  }
1615
1618
 
1619
+ /**
1620
+ * state loop が直近の tick で観測したチャット行の session 名。これらの行の stop は
1621
+ * state loop が返答付きで送るので、bundle hook 由来の stop は WS へ送らない。
1622
+ * 行を観測する前 (起動直後の最初の tick まで) は空なので、hook の stop がそのまま送られる。
1623
+ */
1624
+ export const chatStopNotifyingSessionNames = new Set()
1625
+
1616
1626
  /** session-event をプロセス内で取り込み、並び順を更新して WS push する。 */
1617
1627
  export async function publishSessionEvent(
1618
1628
  { client, readinessTracker, logger },
1619
1629
  sessionName,
1620
1630
  event,
1621
1631
  at,
1622
- { fresh = false, arm = !fresh, text = "" } = {},
1632
+ { fresh = false, arm = !fresh, text = "", send = true } = {},
1623
1633
  ) {
1624
1634
  // readiness (proc 内省の ready 判定) を動かすのはファイル監視経路だけ。直接 publish (fresh) は
1625
1635
  // 「人間が送った」事実を並び順へ流すだけで、ターンの開始を意味しない (bridge が拒否すれば
@@ -1651,7 +1661,7 @@ export async function publishSessionEvent(
1651
1661
  }
1652
1662
  // 通知本文に添える最後の返答。空なら項目ごと送らない (Hub は固定文だけにする)。
1653
1663
  if (typeof text === "string" && text) message.text = text
1654
- client.send(message)
1664
+ if (send) client.send(message)
1655
1665
  return { sortAffecting }
1656
1666
  }
1657
1667
 
@@ -2031,6 +2041,7 @@ export function startStateLoop({ client, logger, intervalMs, claudeBridge, codex
2031
2041
  // 直列実行する (tmux.mjs)。無関係な worktree の Git 処理待ちで全会話の通知観測が止まる。
2032
2042
  // 現在の target 名は workspace / worktree とも sanitizeTmuxName(basename(cwd)) と同値
2033
2043
  // なので、cwd から直接導出する。この同値は test/chat-targets-naming.test.mjs で固定する。
2044
+ const observedChatNames = new Set()
2034
2045
  for (const kind of ["claude", "codex"]) {
2035
2046
  for (const cwd of representativeRows(kind)) {
2036
2047
  // 観測キーは行名でなく cwd+kind。同名 worktree は 1 行へ畳まれるため、
@@ -2042,6 +2053,7 @@ export function startStateLoop({ client, logger, intervalMs, claudeBridge, codex
2042
2053
  previousChatEvents.delete(key)
2043
2054
  continue
2044
2055
  }
2056
+ observedChatNames.add(sessionName)
2045
2057
  const directAt = getPromptAcceptedAt(cwd, kind)
2046
2058
  const previous = previousChatEvents.get(key)
2047
2059
  // 起動時の過去状態は通知せず、次の変化を比較する基準だけを保存する。
@@ -2084,6 +2096,9 @@ export function startStateLoop({ client, logger, intervalMs, claudeBridge, codex
2084
2096
  .catch(err => logger?.warn({ err: err?.message }, "chat session.event publish failed"))
2085
2097
  }
2086
2098
  }
2099
+ // 走査を完走した tick の観測だけで置き換える (途中で例外なら前回の集合を保つ)。
2100
+ chatStopNotifyingSessionNames.clear()
2101
+ for (const name of observedChatNames) chatStopNotifyingSessionNames.add(name)
2087
2102
  } catch (err) {
2088
2103
  logger?.warn({ err: err?.message }, "chat session.event tick failed")
2089
2104
  }