@cocorograph/hub-agent 0.6.43 → 0.6.44
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 +1 -1
- package/src/main.mjs +23 -0
- package/src/tui-viewer-registry.mjs +185 -0
package/package.json
CHANGED
package/src/main.mjs
CHANGED
|
@@ -50,6 +50,7 @@ import {
|
|
|
50
50
|
removeWorktree as removeWorktreeDir,
|
|
51
51
|
} from "./tmux.mjs"
|
|
52
52
|
import { TuiPermissionBridge } from "./tui-permission-bridge.mjs"
|
|
53
|
+
import { TuiViewerRegistry } from "./tui-viewer-registry.mjs"
|
|
53
54
|
import {
|
|
54
55
|
contextWindowSize,
|
|
55
56
|
getSessionUsages,
|
|
@@ -430,12 +431,21 @@ export async function startDaemon({ version, ptyModule, claudeSdk } = {}) {
|
|
|
430
431
|
await tuiPermissionBridge.start()
|
|
431
432
|
ctx.tuiPermissionBridge = tuiPermissionBridge
|
|
432
433
|
|
|
434
|
+
// TUI 閲覧マーカー registry: browser の claude.tui.viewing ハートビートを受けて
|
|
435
|
+
// 「閲覧中の session」をローカルのマーカーファイルとして保持する。承認フックは
|
|
436
|
+
// その鮮度を見て「閲覧者がいる session だけブリッジ、いなければ即 ask」する
|
|
437
|
+
// (watcher ゲート, WATCHER-GATE.md)。
|
|
438
|
+
const tuiViewerRegistry = new TuiViewerRegistry({ logger })
|
|
439
|
+
await tuiViewerRegistry.start()
|
|
440
|
+
ctx.tuiViewerRegistry = tuiViewerRegistry
|
|
441
|
+
|
|
433
442
|
const shutdown = async (signal) => {
|
|
434
443
|
logger.info({ signal }, "shutting down")
|
|
435
444
|
await runHookBroadcast(plugins, "onAgentStop", ctx)
|
|
436
445
|
stateLoop.stop()
|
|
437
446
|
sessionEventLoop?.stop?.()
|
|
438
447
|
tuiPermissionBridge.stop()
|
|
448
|
+
tuiViewerRegistry.stop()
|
|
439
449
|
ptyBridge.shutdown()
|
|
440
450
|
claudeBridge?.shutdown?.()
|
|
441
451
|
client.stop()
|
|
@@ -1016,6 +1026,19 @@ async function dispatch(msg, ctx) {
|
|
|
1016
1026
|
if (!ctx.claudeBridge) return
|
|
1017
1027
|
ctx.claudeBridge.interrupt({ stream_id: msg.stream_id })
|
|
1018
1028
|
return
|
|
1029
|
+
case "claude.tui.viewing":
|
|
1030
|
+
// T04778 watcher ゲート: browser が TUI チャットでこの session を閲覧中。
|
|
1031
|
+
// 閲覧マーカーを更新し、承認フックが「閲覧者あり」と判定できるようにする。
|
|
1032
|
+
ctx.tuiViewerRegistry
|
|
1033
|
+
?.note({ session_id: msg.session_id, cwd: msg.cwd })
|
|
1034
|
+
.catch(() => {})
|
|
1035
|
+
return
|
|
1036
|
+
case "claude.tui.unviewing":
|
|
1037
|
+
// 閲覧終了。session_id マーカーを即失効させ、以降の Bash を即 ask に倒す。
|
|
1038
|
+
ctx.tuiViewerRegistry
|
|
1039
|
+
?.unview({ session_id: msg.session_id })
|
|
1040
|
+
.catch(() => {})
|
|
1041
|
+
return
|
|
1019
1042
|
case "claude.mcp.status":
|
|
1020
1043
|
case "claude.mcp.reconnect":
|
|
1021
1044
|
case "claude.mcp.authenticate":
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TUI 閲覧マーカー registry (T04778 watcher ゲート)。
|
|
3
|
+
*
|
|
4
|
+
* Cockpit の TUI チャットでブラウザが「ある session を閲覧中」であることを、
|
|
5
|
+
* ローカルのマーカーファイルとして保持する。対話 TUI (tmux/PTY) で動く claude の
|
|
6
|
+
* PreToolUse 承認フック (bridge_permission.py) は、ツール実行前にこのマーカーの
|
|
7
|
+
* 鮮度を読み、「閲覧者がいる session だけ承認を Cockpit へブリッジ、いなければ
|
|
8
|
+
* 即 ask 縮退」する。これにより、承認フックを全ユーザーの settings.json に常設
|
|
9
|
+
* しても、非閲覧の対話セッション (リーダー / cockpit-orch サブ / ターミナルモード)
|
|
10
|
+
* の Bash がブリッジ応答 TTL ぶんハングする地雷を回避する。
|
|
11
|
+
*
|
|
12
|
+
* ブラウザは閲覧中 `claude.tui.viewing {session_id, cwd}` を周期送信し、main.mjs が
|
|
13
|
+
* note() を呼んでマーカーを更新する。アンマウント時は `claude.tui.unviewing` で
|
|
14
|
+
* unview() を呼び、sid マーカーを即失効させる。鮮度切れは sweep で掃除する。
|
|
15
|
+
*
|
|
16
|
+
* ファイル IPC / atomic write (tmp+rename) / 全エラー握りつぶしは
|
|
17
|
+
* tui-permission-bridge.mjs の前例に揃える。
|
|
18
|
+
*
|
|
19
|
+
* 契約: D00000_hub.cocorograph.com:tools/cockpit-tui-chat/WATCHER-GATE.md
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { createHash } from "node:crypto"
|
|
23
|
+
import { mkdir, writeFile, rename, unlink, readdir, readFile } from "node:fs/promises"
|
|
24
|
+
import path from "node:path"
|
|
25
|
+
|
|
26
|
+
export const VIEWERS_DIR =
|
|
27
|
+
process.env.COCKPIT_VIEWERS_DIR || "/tmp/cockpit_tui_viewers"
|
|
28
|
+
|
|
29
|
+
/** マーカー鮮度 (秒)。ブラウザのハートビート間隔 (5s) の 3 倍を既定とする。 */
|
|
30
|
+
const VIEWER_TTL_SEC = Number(process.env.COCKPIT_VIEWER_TTL_SEC) || 15
|
|
31
|
+
|
|
32
|
+
/** 失効マーカーを掃除する周期 (ms)。判定はフック側が expires_at で行うので衛生のみ。 */
|
|
33
|
+
const SWEEP_INTERVAL_MS = 30 * 1000
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* session_id をフック側 (`bridge_permission.py`) と同一規則でファイル名へ正規化する。
|
|
37
|
+
* Python 側は ``c.isalnum() or c in "-_"`` を残す。UUID は ASCII 英数 + ハイフンなので
|
|
38
|
+
* 実質 no-op だが、両者が一致しないとマーカーを引けないため厳密に揃える。
|
|
39
|
+
*
|
|
40
|
+
* @param {string} sessionId
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
43
|
+
function sanitizeSessionId(sessionId) {
|
|
44
|
+
return String(sessionId).replace(/[^A-Za-z0-9_-]/g, "")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* cwd をフック側と同一規則でハッシュする (sha1 hex 先頭 16 文字)。
|
|
49
|
+
*
|
|
50
|
+
* @param {string} cwd
|
|
51
|
+
* @returns {string}
|
|
52
|
+
*/
|
|
53
|
+
function hashCwd(cwd) {
|
|
54
|
+
return createHash("sha1").update(String(cwd), "utf8").digest("hex").slice(0, 16)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* TUI 閲覧マーカーをローカル FS に保持する registry。
|
|
59
|
+
*/
|
|
60
|
+
export class TuiViewerRegistry {
|
|
61
|
+
/**
|
|
62
|
+
* @param {{dir?: string, ttlSec?: number, logger?: object}} [opts]
|
|
63
|
+
*/
|
|
64
|
+
constructor({ dir = VIEWERS_DIR, ttlSec = VIEWER_TTL_SEC, logger } = {}) {
|
|
65
|
+
this.dir = dir
|
|
66
|
+
this.ttlSec = ttlSec
|
|
67
|
+
this.logger = logger
|
|
68
|
+
this._sweepTimer = null
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** ディレクトリを作成し、起動時の残骸を掃除して周期 sweep を張る。 */
|
|
72
|
+
async start() {
|
|
73
|
+
try {
|
|
74
|
+
await mkdir(this.dir, { recursive: true })
|
|
75
|
+
} catch (err) {
|
|
76
|
+
this.logger?.warn({ err: err?.message }, "tui viewer registry: mkdir failed")
|
|
77
|
+
}
|
|
78
|
+
await this._sweep()
|
|
79
|
+
this._sweepTimer = setInterval(() => {
|
|
80
|
+
this._sweep().catch(() => {})
|
|
81
|
+
}, SWEEP_INTERVAL_MS)
|
|
82
|
+
// 他の timer 同様 unref して agent のアイドル終了を妨げない。
|
|
83
|
+
this._sweepTimer?.unref?.()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 閲覧ハートビートを受けてマーカーを更新する。session_id マーカー (厳密) と
|
|
88
|
+
* cwd マーカー (フォールバック) の両方を ``expires_at = now + ttl`` で atomic 更新する。
|
|
89
|
+
*
|
|
90
|
+
* @param {{session_id?: string|null, cwd?: string|null}} info
|
|
91
|
+
* @returns {Promise<void>}
|
|
92
|
+
*/
|
|
93
|
+
async note({ session_id, cwd } = {}) {
|
|
94
|
+
// expires_at は epoch **秒** (フック側 time.time() と比較するため)。
|
|
95
|
+
const expiresAt = Date.now() / 1000 + this.ttlSec
|
|
96
|
+
const updatedAt = Date.now() / 1000
|
|
97
|
+
const body = JSON.stringify({
|
|
98
|
+
session_id: session_id ?? null,
|
|
99
|
+
cwd: cwd ?? null,
|
|
100
|
+
expires_at: expiresAt,
|
|
101
|
+
updated_at: updatedAt,
|
|
102
|
+
})
|
|
103
|
+
const targets = []
|
|
104
|
+
if (session_id) {
|
|
105
|
+
const safe = sanitizeSessionId(session_id)
|
|
106
|
+
if (safe) targets.push(path.join(this.dir, `sid-${safe}.json`))
|
|
107
|
+
}
|
|
108
|
+
if (cwd) targets.push(path.join(this.dir, `cwd-${hashCwd(cwd)}.json`))
|
|
109
|
+
for (const fp of targets) {
|
|
110
|
+
await this._atomicWrite(fp, body)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 閲覧終了を受けて session_id マーカーを即削除する。cwd マーカーは同 cwd を
|
|
116
|
+
* 他のブラウザが閲覧している可能性があるため即削除せず、TTL 失効に委ねる。
|
|
117
|
+
*
|
|
118
|
+
* @param {{session_id?: string|null}} info
|
|
119
|
+
* @returns {Promise<void>}
|
|
120
|
+
*/
|
|
121
|
+
async unview({ session_id } = {}) {
|
|
122
|
+
if (!session_id) return
|
|
123
|
+
const safe = sanitizeSessionId(session_id)
|
|
124
|
+
if (!safe) return
|
|
125
|
+
try {
|
|
126
|
+
await unlink(path.join(this.dir, `sid-${safe}.json`))
|
|
127
|
+
} catch {
|
|
128
|
+
/* 既に無い / 失効済み。 */
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* atomic write (tmp+rename)。書けなくても agent は落とさない。
|
|
134
|
+
* @param {string} fp
|
|
135
|
+
* @param {string} body
|
|
136
|
+
*/
|
|
137
|
+
async _atomicWrite(fp, body) {
|
|
138
|
+
const tmp = `${fp}.tmp`
|
|
139
|
+
try {
|
|
140
|
+
await writeFile(tmp, body)
|
|
141
|
+
await rename(tmp, fp)
|
|
142
|
+
} catch (err) {
|
|
143
|
+
this.logger?.warn({ err: err?.message, fp }, "viewer marker write failed")
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** expires_at を過ぎたマーカーを unlink する (衛生のみ)。 */
|
|
148
|
+
async _sweep() {
|
|
149
|
+
let names = []
|
|
150
|
+
try {
|
|
151
|
+
names = await readdir(this.dir)
|
|
152
|
+
} catch {
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
const now = Date.now() / 1000
|
|
156
|
+
for (const n of names) {
|
|
157
|
+
if (!n.endsWith(".json")) continue
|
|
158
|
+
const fp = path.join(this.dir, n)
|
|
159
|
+
let body
|
|
160
|
+
try {
|
|
161
|
+
body = JSON.parse(await readFile(fp, "utf-8"))
|
|
162
|
+
} catch {
|
|
163
|
+
continue
|
|
164
|
+
}
|
|
165
|
+
const exp = body?.expires_at
|
|
166
|
+
if (typeof exp === "number" && exp <= now) {
|
|
167
|
+
try {
|
|
168
|
+
await unlink(fp)
|
|
169
|
+
} catch {
|
|
170
|
+
/* ignore */
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 周期 sweep を停止する。 */
|
|
177
|
+
stop() {
|
|
178
|
+
if (this._sweepTimer) {
|
|
179
|
+
clearInterval(this._sweepTimer)
|
|
180
|
+
this._sweepTimer = null
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export default TuiViewerRegistry
|