Lets the Bridge cockpit (scripts/bridge-cockpit.py) ask this Claude Code session: heartbeats, takes the questions the cockpit signs and addresses to this…

A Claude Code mod that lets the Bridge cockpit (scripts/bridge-cockpit.py) ask this Claude Code session. "To the control session" on the cockpit page reaches the session through it. Contract and limits: docs/bridge-cockpit.md, section 5.
What it does, and nothing else:
.bridge/cockpit/relay/heartbeat/<session>.json: alive, busy, the question in flight, and where the session runs (the cmux surface, else the tmux pane such as %3, else null), so the server can pick the control session; the heartbeat and every done file are signed with the same relay.key, and the server ignores any it cannot verify;.bridge/cockpit/relay/inbox/ every 2 seconds and submits a question only when the server addressed it to this session and signed it with relay.key (kept outside the Bridge in $BRIDGE_COCKPIT_KEY_DIR, else ~/.local/state/bridge-cockpit/);.bridge/cockpit/relay/done/<id>.json (answered, aborted, error, expired, stuck, rejected).It came out of the retired briefing-ui mod, so the cockpit kept reaching the control session when the briefing card went away.
cockpit:
enabled: true
relay: true # off unless true: without it the page works, no session takes questions
Only the top-level cockpit: block counts. The relay never runs in an agent tab (BRIDGE_TAB_SLUG set), in a claude -p or SDK run, or outside a Bridge.
From the Bridge folder:
claude plugin marketplace add ./mods --scope local
claude plugin install cockpit-relay@open-bridge-mods --scope local
npx -y -p typescript@5.6.3 -c 'node mods/cockpit-relay/tests/pure.test.mjs' # pure functions
claude plugin test mods/cockpit-relay # engine tests, local onlyhooks/register.tsx 324 lines1// SPDX-License-Identifier: MIT
2import { atom, read, update } from 'claude-code'
3import type { EngineInterface, Register } from 'claude-code'
4
5import type { RelaySettings } from '../types'
6import {
7 answerSinceLastUser, doneIdsOf, doneRecord, heartbeat, HEARTBEAT_MS, isFlightTurn, isRelayId, isRelayKey,
8 isSessionId, parseRelayConfig, pickNext, POLL_MS, reasonToStatus, RELAY_DONE, RELAY_HEARTBEAT, RELAY_INBOX,
9 relayKeyPath, rememberId, STORE_RELAY_DONE, STUCK_MS, surfaceOf, TABLES, verifyMac,
10} from './relay'
11import type { InboxRecord, RelayEntry, RelayStatus, Strings } from './relay'
12
13/**
14 * cockpit-relay: the cockpit (scripts/bridge-cockpit.py) reaches this Claude Code session.
15 *
16 * The cockpit server writes a signed question for exactly one session into .bridge/cockpit/relay/inbox;
17 * this mod heartbeats so the server can pick a session, submits the question as a prompt of its own,
18 * and writes what became of it to done/. It does nothing else: no card, no command, no terminal
19 * control. It came out of briefing-ui so the relay outlives the briefing card, and it needs no cmux:
20 * the heartbeat names the cmux surface or the tmux pane when there is one, and null otherwise.
21 *
22 * Early access: written against the function hooks API of Claude Code 2.1.291, which may change
23 * between versions.
24 */
25
26/** The relay settings: null outside a Bridge, in agent tabs, in a -p run and when the config is unreadable */
27const settings = atom({ plugin: 'cockpit-relay', key: 'settings' } as const, null as RelaySettings | null)
28/** A main turn runs in this session: the relay never submits into it */
29const busy = atom({ plugin: 'cockpit-relay', key: 'busy' } as const, false)
30/** The one cockpit question in flight in this session */
31const flight = atom({ plugin: 'cockpit-relay', key: 'flight' } as const, null)
32/** Questions marked stuck while their turn still ran: turn.complete still writes their answer */
33const late = atom({ plugin: 'cockpit-relay', key: 'late' } as const, [])
34/** The session id this session heartbeats under; null while the relay is off */
35const relaySession = atom({ plugin: 'cockpit-relay', key: 'session' } as const, null)
36
37/**
38 * Reads only what the relay needs; which block counts is decided in relay.ts (relayBlock), so the rule
39 * is tested without Python.
40 */
41const READ_CONFIG = [
42 'import json, yaml',
43 "c = yaml.safe_load(open('bridge-config.yaml', encoding='utf-8')) or {}",
44 'c = c if isinstance(c, dict) else {}',
45 "l = c.get('language')",
46 "print(json.dumps({'cockpit': c.get('cockpit'),",
47 " 'language': l.get('conversation') if isinstance(l, dict) else l}, default=str))",
48].join('\n')
49
50/** The interpreters tried in turn for the config reader */
51const PYTHONS = ['python3', 'python3.14', 'python3.13', 'python3.12', 'python3.11', '/opt/homebrew/bin/python3.14', '/usr/bin/python3']
52
53let words: Strings = TABLES.en
54
55/** Feedback as a brief toast: the mod has no surface of its own */
56function note($: EngineInterface, text: string): void {
57 $.ui.toast(text)
58}
59
60/** The relay settings of this Bridge; null outside a Bridge or when the config cannot be read */
61async function loadSettings($: EngineInterface): Promise<RelaySettings | null> {
62 const cwd = await $.session.cwd()
63 const isBridge =
64 (await $.fs.exists(`${cwd}/bridge-config.yaml`)) && (await $.fs.exists(`${cwd}/scripts/bridge-cockpit.py`))
65 if (!isBridge) return null
66 // python3 first; when it has no PyYAML (a new Python version comes without it until it is installed
67 // there), the next Python that does reads the config, so the relay does not fall silent on an update
68 for (const python of PYTHONS) {
69 try {
70 const run = await $.process.run([python, '-c', READ_CONFIG], { cwd, timeoutMs: 15000 })
71 if (run.exitCode === 0) return parseRelayConfig(JSON.parse(run.stdout))
72 } catch {
73 // not installed, or no answer: try the next
74 }
75 }
76 return null
77}
78
79// ---------------------------------------------------------------- heartbeat and poll
80
81/** A poll is running: a slow file system must not pile polls up */
82let polling = false
83/** Inbox files addressed to another session: read once, never again */
84const foreignRecords = new Set<string>()
85
86/** Where this session runs: the cmux surface, else the tmux pane, else null (relay.ts surfaceOf) */
87async function surface($: EngineInterface): Promise<string | null> {
88 return surfaceOf(await $.env.get('CMUX_SURFACE_ID'), await $.env.get('TMUX_PANE'))
89}
90
91/** The relay of one session: a heartbeat every 5 s, a look into the inbox every 2 s. */
92function startRelay($: EngineInterface, session: string): void {
93 const beat = async () => {
94 try {
95 const cur = await read($, flight)
96 // signed with relay.key, read at every beat: the server may create or renew the key at any start
97 await $.fs.write(`${RELAY_HEARTBEAT}/${session}.json`, JSON.stringify(heartbeat(session, await surface($),
98 await $.clock.now(), await read($, busy), cur?.id ?? null, await relayKey($))))
99 } catch {
100 // no heartbeat: the page says the relay is offline and offers the text to copy
101 }
102 }
103 $.clock.after(0, () => void beat())
104 $.clock.every(HEARTBEAT_MS, () => void beat())
105 $.clock.every(POLL_MS, () => void poll($, session))
106}
107
108async function poll($: EngineInterface, session: string): Promise<void> {
109 if (polling) return
110 polling = true
111 try {
112 await pollOnce($, session)
113 } catch {
114 // the next poll tries again
115 } finally {
116 polling = false
117 }
118}
119
120/** Writes done/<id>.json (the mod is its only writer) and remembers the id as handled. */
121async function relayDone($: EngineInterface, session: string, id: string, status: RelayStatus,
122 turnId: string | null, answer: string | null): Promise<void> {
123 if (!isRelayId(id)) return
124 await $.store.set(STORE_RELAY_DONE, rememberId(await $.store.get(STORE_RELAY_DONE), id))
125 try {
126 // signed with relay.key: the server shows only what this session wrote
127 await $.fs.write(`${RELAY_DONE}/${id}.json`,
128 JSON.stringify(doneRecord(id, session, status, turnId, answer, await $.clock.now(), await relayKey($))))
129 } catch {
130 // the server shows the record as queued until it expires
131 }
132}
133
134/** relay.key, where the server keeps it (outside the Bridge); null when it cannot be read */
135async function relayKey($: EngineInterface): Promise<string | null> {
136 const path = relayKeyPath(await $.env.get('BRIDGE_COCKPIT_KEY_DIR'), await $.env.get('HOME'))
137 if (!path) return null
138 try {
139 const key = (await $.fs.read(path)).trim()
140 return isRelayKey(key) ? key : null
141 } catch {
142 return null
143 }
144}
145
146async function pollOnce($: EngineInterface, session: string): Promise<void> {
147 const now = await $.clock.now()
148 const cur = await read($, flight)
149 if (cur && now - cur.at > STUCK_MS) {
150 // Free the slot. A turn that was seen and still runs (a long plan, a permission prompt answered
151 // late) keeps its claim: its turn.complete still writes the answer over `stuck`.
152 await relayDone($, session, cur.id, 'stuck', cur.turnId, null)
153 if (cur.turnId !== null) {
154 const claim = { id: cur.id, turnId: cur.turnId }
155 await update($, late, list => [...list.filter(x => x.id !== claim.id), claim].slice(-20))
156 }
157 await update($, flight, f => (f?.id === cur.id ? null : f))
158 note($, words.stuck)
159 }
160 let files: string[]
161 try {
162 files = (await $.fs.list(RELAY_INBOX)).filter(e => e.kind === 'file' && e.name.endsWith('.json')).map(e => e.name)
163 } catch {
164 return
165 }
166 if (files.length === 0) return
167 let doneNames: string[] = []
168 try {
169 doneNames = (await $.fs.list(RELAY_DONE)).map(e => e.name)
170 } catch {
171 // no done/ yet
172 }
173 const stored = await $.store.get(STORE_RELAY_DONE)
174 const handled = new Set([...doneIdsOf(doneNames), ...(Array.isArray(stored) ? stored.filter(isRelayId) : [])])
175 const entries: RelayEntry[] = []
176 let key: string | null | undefined
177 for (const name of files) {
178 if (handled.has(name.slice(0, -5)) || foreignRecords.has(name)) continue
179 let record: unknown
180 try {
181 record = JSON.parse(await $.fs.read(`${RELAY_INBOX}/${name}`))
182 } catch {
183 continue
184 }
185 if (typeof record === 'object' && record !== null && (record as { target_session?: unknown }).target_session !== session) {
186 foreignRecords.add(name)
187 continue
188 }
189 // Read the key only when a record for this session waits; a record it cannot verify is rejected.
190 if (key === undefined) key = await relayKey($)
191 entries.push({ name, record, verified: verifyMac(key, record) })
192 }
193 const inFlight = await read($, flight)
194 const pick = pickNext(entries, handled, session, now, await read($, busy), inFlight?.id ?? null)
195 for (const r of pick.expire) await relayDone($, session, r.id, 'expired', null, null)
196 for (const r of pick.reject) await relayDone($, session, r.id, 'rejected', null, null)
197 if (pick.expire.length) note($, words.expired(pick.expire.length))
198 if (pick.reject.length) note($, words.rejected(pick.reject.length))
199 if (pick.submit) await submit($, session, pick.submit)
200}
201
202/**
203 * Submits one cockpit question as a prompt of its own, framed (never asUser: the model reads that the
204 * cockpit-relay plugin sent it, and the text says it came from the cockpit). Everything that marks it
205 * as handled happens BEFORE the submit: a reload or crash in between can lose a question, never ask it twice.
206 */
207async function submit($: EngineInterface, session: string, rec: InboxRecord): Promise<void> {
208 const at = await $.clock.now()
209 await update($, flight, () => ({ id: rec.id, at, turnId: null }))
210 await relayDone($, session, rec.id, 'submitted', null, null)
211 let refused = false
212 try {
213 // A hook that refuses the prompt resolves with { drop }: the question never entered.
214 const res = await $.prompt.submit({ text: rec.text })
215 refused = typeof res === 'object' && res !== null && typeof (res as { drop?: unknown }).drop === 'string'
216 } catch {
217 refused = true
218 }
219 if (refused) {
220 await relayDone($, session, rec.id, 'error', null, null)
221 await update($, flight, f => (f?.id === rec.id ? null : f))
222 }
223}
224
225/**
226 * What the cockpit turn itself said, when turn.complete brought no answer: the last assistant text after
227 * the last user message (the cockpit prompt), never an earlier answer of the conversation.
228 */
229async function lastAnswer($: EngineInterface): Promise<string> {
230 try {
231 return answerSinceLastUser(await $.session.messages())
232 } catch {
233 return ''
234 }
235}
236
237/** The relay runs in this session (settings is null wherever it must not) */
238async function relayOn($: EngineInterface): Promise<boolean> {
239 const s = await read($, settings)
240 return s !== null && s.enabled && s.relay
241}
242
243// ---------------------------------------------------------------- Module
244
245export const register: Register = on => {
246 on('session.start', async ($, e, next) => {
247 const started = await next(e)
248 // A tab the Bridge started for an item never relays: the cockpit addresses the control session.
249 // A -p or SDK run is never addressed either: it inherits the control tab's surface and would
250 // claim its questions.
251 if (!e.isInteractive || (await $.env.get('BRIDGE_TAB_SLUG'))) return started
252 const found = await loadSettings($)
253 if (!found) return started
254 words = TABLES[found.language]
255 await update($, settings, () => found)
256 if (!found.enabled || !found.relay) return started
257 // The server addresses a question to one session by this id; one it cannot name gets none.
258 const session = await $.session.id().catch(() => '')
259 if (!isSessionId(session)) return started
260 await update($, relaySession, () => session)
261 startRelay($, session)
262 return started
263 })
264
265 // The relay follows the main loop: busy from its turn.start to its turn.complete (a subagent
266 // raises no turn.start, and its turn.complete carries agentId).
267 on('turn.start', async ($, e, next) => {
268 try {
269 if (await relayOn($)) {
270 await update($, busy, () => true)
271 const cur = await read($, flight)
272 if (cur && cur.turnId === null) {
273 // The marker, when the turn shows the text; else a turn with no text right after the submit.
274 // A turn with other text is never the cockpit turn (docs/bridge-cockpit.md, known limits).
275 const isOurs = isFlightTurn(e.text, cur.id, cur.at, await $.clock.now())
276 if (isOurs) await update($, flight, f => (f?.id === cur.id ? { ...f, turnId: e.turnId } : f))
277 }
278 }
279 } catch {
280 // bookkeeping only: the turn runs either way
281 }
282 return next(e)
283 })
284
285 on('turn.complete', async ($, e, next) => {
286 try {
287 if (e.agentId === undefined && (await relayOn($))) {
288 await update($, busy, () => false)
289 const cur = await read($, flight)
290 const held = (await read($, late)).find(x => x.turnId === e.turnId)
291 const claim = cur && cur.turnId !== null && cur.turnId === e.turnId
292 ? { id: cur.id, turnId: cur.turnId } : held ?? null
293 if (claim) {
294 // The transcript only stands in for a turn that ended with an answer; an aborted or failed
295 // turn has none of its own.
296 const answer = e.answer.trim() ? e.answer : e.reason === 'answer' ? await lastAnswer($) : ''
297 if (cur?.id === claim.id) await update($, flight, () => null)
298 if (held) await update($, late, list => list.filter(x => x.id !== held.id))
299 await relayDone($, await $.session.id(), claim.id, reasonToStatus(e.reason), claim.turnId, answer)
300 }
301 }
302 } catch {
303 // bookkeeping only
304 }
305 return next(e)
306 })
307
308 // The session ends: its heartbeat says so at once (at_ms 0), so the server stops addressing it
309 // instead of waiting 15 s for the heartbeat to go stale. The mod stays its only writer.
310 on('session.end', async ($, e, next) => {
311 try {
312 const session = await read($, relaySession)
313 if (session !== null) {
314 await update($, relaySession, () => null)
315 await $.fs.write(`${RELAY_HEARTBEAT}/${session}.json`,
316 JSON.stringify(heartbeat(session, null, 0, false, null, await relayKey($))))
317 }
318 } catch {
319 // the heartbeat goes stale after 15 s anyway
320 }
321 return next(e)
322 })
323}
324hooks/relay.ts 421 lines1// SPDX-License-Identifier: MIT
2import type { RelaySettings } from '../types'
3
4/**
5 * The relay between the cockpit (scripts/bridge-cockpit.py, scripts/lib/bridge_cockpit/relay.py) and
6 * this Claude Code session, as pure functions: no `$`, so pure.test.mjs checks them without Claude Code.
7 *
8 * Files (docs/bridge-cockpit.md section 6), each with exactly one writer:
9 * .bridge/cockpit/relay/inbox/<id>.json the server: a question, framed, for ONE session
10 * .bridge/cockpit/relay/done/<id>.json the mod of the target session: what became of it
11 * .bridge/cockpit/relay/heartbeat/<session>.json the mod of that session: alive, busy, in flight
12 *
13 * Why only the server's questions are submitted, and only this session's word counts: every file of the
14 * three carries `mac`, an HMAC-SHA256 keyed by relay.key, which the server keeps OUTSIDE the Bridge in a
15 * 0700 directory ($BRIDGE_COCKPIT_KEY_DIR, else ~/.local/state/bridge-cockpit), over a message that
16 * names its kind and puts the free text last (relayMac, heartbeatMac, doneMac below). A record this
17 * session cannot verify is rejected, and the server ignores a heartbeat or done file it cannot verify:
18 * a process that can only write inside the repo (an agent tab) can neither hand this session a prompt,
19 * nor pose as this session to get the questions, nor answer in its name.
20 *
21 * Why a question is submitted at most once: the server names one target session per record
22 * (the heartbeat whose surface is the control tab); every other session ignores the record
23 * without writing anything. The target session marks the id as handled (in flight, in its
24 * store, in done/) BEFORE it submits, never submits while a turn runs or another question is
25 * in flight, and skips every id it has a done file or a store entry for.
26 *
27 * This file came over from briefing-ui (hooks/relay.ts) when the relay became a mod of its own, so
28 * the cockpit keeps reaching the control session after the briefing card retires.
29 */
30
31export const RELAY_DIR = '.bridge/cockpit/relay'
32export const RELAY_INBOX = `${RELAY_DIR}/inbox`
33export const RELAY_DONE = `${RELAY_DIR}/done`
34export const RELAY_HEARTBEAT = `${RELAY_DIR}/heartbeat`
35/** Store key: the ids this session has handled, newest last */
36export const STORE_RELAY_DONE = 'cockpit-relay:relay-done'
37/** How many handled ids the store keeps */
38export const DONE_KEEP = 200
39/** The answer written back is cut to this many characters (relay.py ANSWER_MAX) */
40export const ANSWER_MAX = 20000
41/** A framed question longer than this is refused (the server caps the text at 4000) */
42export const TEXT_MAX = 12000
43export const HEARTBEAT_MS = 5000
44export const POLL_MS = 2000
45/** A turn that starts this soon after the submit, with no text at all, carries the question */
46export const CORRELATE_MS = 60000
47/** The key file's name in the key directory (relay.py KEY_FILE) */
48export const KEY_FILE = 'relay.key'
49/** Where the server keeps the key without $BRIDGE_COCKPIT_KEY_DIR, below $HOME (relay.py default_key_dir) */
50export const KEY_HOME_DIR = '.local/state/bridge-cockpit'
51/** In flight for longer than this: done `stuck`, the slot is free again (relay.py STUCK_MS) */
52export const STUCK_MS = 30 * 60000
53/** Must equal .claude-plugin/plugin.json version (pure.test.mjs checks it) */
54export const MOD_VERSION = '0.2.0'
55
56const ID = /^\d{8}T\d{6}-[0-9a-f]{8}$/
57const SESSION = /^[A-Za-z0-9-]{8,64}$/
58
59export type RelayStatus = 'submitted' | 'answered' | 'aborted' | 'error' | 'expired' | 'stuck' | 'rejected'
60/** Every status ends the mod's part except `submitted`, which a turn later advances; none allows a second submit */
61export const STATUSES: readonly RelayStatus[] = ['submitted', 'answered', 'aborted', 'error', 'expired', 'stuck', 'rejected']
62
63/** inbox/<id>.json as the server writes it */
64export type InboxRecord = {
65 v: number
66 id: string
67 created_ms: number
68 expires_ms: number
69 target_session: string
70 kind: string
71 text: string
72 chars: number
73}
74
75/** done/<id>.json as the mod writes it */
76export type DoneRecord = {
77 v: 1
78 id: string
79 session: string
80 status: RelayStatus
81 turn_id: string | null
82 answer: string | null
83 at_ms: number
84 /** doneMac with relay.key; null when this session cannot read the key (the server then ignores the file) */
85 mac: string | null
86}
87
88/** heartbeat/<session>.json as the mod writes it */
89export type Heartbeat = {
90 v: 1
91 session: string
92 surface: string | null
93 at_ms: number
94 busy: boolean
95 in_flight: string | null
96 mod_version: string
97 /** heartbeatMac with relay.key; null when this session cannot read the key (the server then ignores it) */
98 mac: string | null
99}
100
101/**
102 * One file of relay/inbox: its name, what JSON.parse made of it (null when unreadable), and whether
103 * its mac verified with the server's key (only true lets it be submitted)
104 */
105export type RelayEntry = { name: string; record: unknown; verified?: boolean }
106
107/** What one poll does: write `expired` / `rejected` for these, and submit at most one */
108export type RelayPick = { expire: InboxRecord[]; reject: InboxRecord[]; submit: InboxRecord | null }
109
110/**
111 * The block the relay reads: `cockpit:`, as scripts/lib/bridge_cockpit/config.py load_web_config reads it.
112 * `raw` is what the config reader printed.
113 */
114export function relayBlock(raw: unknown): unknown {
115 const r = isObject(raw) ? raw : {}
116 return r.cockpit
117}
118
119/**
120 * The relay's settings: enabled and relay each only when true (the server's rule), and the language
121 * of its notes: the block's own, else language.conversation.
122 */
123export function parseRelayConfig(raw: unknown): RelaySettings {
124 const r = isObject(raw) ? raw : {}
125 const w = relayBlock(r)
126 const b = isObject(w) ? w : {}
127 const lang = [b.language, r.language].find(v => typeof v === 'string' && v !== '')
128 return { enabled: b.enabled === true, relay: b.relay === true,
129 language: typeof lang === 'string' && lang.toLowerCase().startsWith('de') ? 'de' : 'en' }
130}
131
132function isObject(v: unknown): v is Record<string, unknown> {
133 return typeof v === 'object' && v !== null && !Array.isArray(v)
134}
135
136/** A tmux pane id as $TMUX_PANE carries it */
137const TMUX_PANE = /^%\d{1,9}$/
138
139/**
140 * The terminal place this session runs in, for the heartbeat: the cmux surface when cmux runs it, else
141 * the tmux pane ("%3"), else null. The server matches it against the control tab's id; a session it
142 * cannot place still heartbeats and is picked when it is the only fresh one.
143 */
144export function surfaceOf(cmuxSurface: string | null | undefined, tmuxPane: string | null | undefined): string | null {
145 if (typeof cmuxSurface === 'string' && cmuxSurface.trim()) return cmuxSurface.trim()
146 if (typeof tmuxPane === 'string' && TMUX_PANE.test(tmuxPane.trim())) return tmuxPane.trim()
147 return null
148}
149
150export function isRelayId(id: unknown): id is string {
151 return typeof id === 'string' && ID.test(id)
152}
153
154/** A session id the server accepts as a heartbeat file name */
155export function isSessionId(id: unknown): id is string {
156 return typeof id === 'string' && SESSION.test(id)
157}
158
159/** The text carries the marker of exactly this record up front */
160export function isFramed(text: unknown, id: string): boolean {
161 return typeof text === 'string' && text.startsWith(`[web:${id}]`)
162}
163
164/** The marker of any record in a turn's text, or null */
165export function markerIn(text: unknown): string | null {
166 if (typeof text !== 'string') return null
167 const hit = /\[web:(\d{8}T\d{6}-[0-9a-f]{8})\]/.exec(text)
168 return hit?.[1] ?? null
169}
170
171/**
172 * Sort the inbox of this session into what to write and what to submit.
173 *
174 * Ignored without a trace: files that are not `<id>.json` of a valid id, records for another
175 * session (its own mod answers them), ids already handled (`doneIds`: done files and the store).
176 * For this session, in file name order: past `expires_ms` gives `expire`; no matching marker,
177 * a version other than 1, no expiry, an oversized text or a mac that did not verify gives `reject`; the first remaining record is
178 * submitted, but only while no turn runs and nothing is in flight. The rest waits for a later poll.
179 */
180export function pickNext(entries: readonly RelayEntry[], doneIds: Iterable<string>, session: string, now: number,
181 busy: boolean, inFlight: string | null): RelayPick {
182 const handled = new Set(doneIds)
183 const out: RelayPick = { expire: [], reject: [], submit: null }
184 const sorted = [...entries].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
185 for (const { name, record, verified } of sorted) {
186 if (typeof record !== 'object' || record === null) continue
187 const r = record as Record<string, unknown>
188 if (!isRelayId(r.id) || name !== `${r.id}.json`) continue
189 if (r.target_session !== session || handled.has(r.id) || r.id === inFlight) continue
190 const rec = r as unknown as InboxRecord
191 if (typeof r.expires_ms === 'number' && now > r.expires_ms) {
192 out.expire.push(rec)
193 continue
194 }
195 if (r.v !== 1 || typeof r.expires_ms !== 'number' || !isFramed(r.text, r.id)
196 || (r.text as string).length > TEXT_MAX || verified !== true) {
197 out.reject.push(rec)
198 continue
199 }
200 if (out.submit === null && !busy && inFlight === null) out.submit = rec
201 }
202 return out
203}
204
205/** The answer as the done file carries it: cut to ANSWER_MAX characters, null when there is none.
206 * The cut never ends on a high surrogate: half a pair would sign as U+FFFD here and never verify. */
207export function clipAnswer(text: unknown): string | null {
208 if (typeof text !== 'string' || !text.trim()) return null
209 if (text.length <= ANSWER_MAX) return text
210 const last = text.charCodeAt(ANSWER_MAX - 1)
211 return text.slice(0, last >= 0xd800 && last <= 0xdbff ? ANSWER_MAX - 1 : ANSWER_MAX)
212}
213
214/** turn.complete's reason as a done status */
215export function reasonToStatus(reason: unknown): RelayStatus {
216 if (reason === 'answer') return 'answered'
217 if (reason === 'aborted') return 'aborted'
218 return 'error'
219}
220
221/** A done file, signed with `key` (whole milliseconds, so the server formats the time alike) */
222export function doneRecord(id: string, session: string, status: RelayStatus, turnId: string | null,
223 answer: unknown, now: number, key: string | null = null): DoneRecord {
224 const text = clipAnswer(answer)
225 const at = Math.floor(now)
226 return { v: 1, id, session, status, turn_id: turnId, answer: text, at_ms: at,
227 mac: isRelayKey(key) ? doneMac(key, id, status, at, text) : null }
228}
229
230/** A heartbeat, signed with `key` (whole milliseconds, so the server formats the time alike) */
231export function heartbeat(session: string, surface: string | null, now: number, busy: boolean,
232 inFlight: string | null, key: string | null = null): Heartbeat {
233 const at = Math.floor(now)
234 const place = surface || null
235 return { v: 1, session, surface: place, at_ms: at, busy, in_flight: inFlight, mod_version: MOD_VERSION,
236 mac: isRelayKey(key) ? heartbeatMac(key, session, at, busy, place) : null }
237}
238
239/** The ids of a done/ listing: `<id>.json` of valid ids only */
240export function doneIdsOf(names: readonly string[]): string[] {
241 return names.filter(n => n.endsWith('.json')).map(n => n.slice(0, -5)).filter(isRelayId)
242}
243
244/** Remember an id as handled: newest last, at most DONE_KEEP, no duplicates */
245export function rememberId(list: unknown, id: string): string[] {
246 const prev = Array.isArray(list) ? list.filter((x): x is string => typeof x === 'string' && x !== id) : []
247 return [...prev, id].slice(-DONE_KEEP)
248}
249
250// ---------------------------------------------------------------- authenticity (the server's key)
251
252/** Where the server keeps relay.key: $BRIDGE_COCKPIT_KEY_DIR, else ~/.local/state/bridge-cockpit; null without either */
253export function relayKeyPath(keyDir: string | null | undefined, home: string | null | undefined): string | null {
254 if (keyDir) return `${keyDir.replace(/\/$/, '')}/${KEY_FILE}`
255 if (home) return `${home.replace(/\/$/, '')}/${KEY_HOME_DIR}/${KEY_FILE}`
256 return null
257}
258
259/** The key as relay.key holds it: 64 lowercase hex characters */
260export function isRelayKey(key: unknown): key is string {
261 return typeof key === 'string' && /^[0-9a-f]{64}$/.test(key)
262}
263
264const BLOCK = 64
265const K = new Uint32Array([
266 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
267 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
268 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
269 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
270 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
271 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
272 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
273 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
274])
275
276/**
277 * SHA-256 (FIPS 180-4) of the concatenated parts, synchronous: crypto.subtle.digest would settle
278 * outside the session's clock, and a poll has to finish within it.
279 */
280export function sha256(...parts: Uint8Array[]): Uint8Array {
281 const len = parts.reduce((n, p) => n + p.length, 0)
282 const total = Math.ceil((len + 9) / BLOCK) * BLOCK
283 const msg = new Uint8Array(total)
284 let at = 0
285 for (const p of parts) {
286 msg.set(p, at)
287 at += p.length
288 }
289 msg[len] = 0x80
290 const bits = len * 8
291 const view = new DataView(msg.buffer)
292 view.setUint32(total - 8, Math.floor(bits / 0x100000000))
293 view.setUint32(total - 4, bits >>> 0)
294 const h = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19])
295 const w = new Uint32Array(64)
296 const rot = (x: number, n: number) => (x >>> n) | (x << (32 - n))
297 for (let off = 0; off < total; off += BLOCK) {
298 for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + i * 4)
299 for (let i = 16; i < 64; i++) {
300 const a = w[i - 15] ?? 0
301 const b = w[i - 2] ?? 0
302 const s0 = rot(a, 7) ^ rot(a, 18) ^ (a >>> 3)
303 const s1 = rot(b, 17) ^ rot(b, 19) ^ (b >>> 10)
304 w[i] = ((w[i - 16] ?? 0) + s0 + (w[i - 7] ?? 0) + s1) >>> 0
305 }
306 let [a, b, c, d, e, f, g, hh] = [h[0] ?? 0, h[1] ?? 0, h[2] ?? 0, h[3] ?? 0, h[4] ?? 0, h[5] ?? 0, h[6] ?? 0, h[7] ?? 0]
307 for (let i = 0; i < 64; i++) {
308 const t1 = (hh + (rot(e, 6) ^ rot(e, 11) ^ rot(e, 25)) + ((e & f) ^ (~e & g)) + (K[i] ?? 0) + (w[i] ?? 0)) >>> 0
309 const t2 = ((rot(a, 2) ^ rot(a, 13) ^ rot(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0
310 hh = g
311 g = f
312 f = e
313 e = (d + t1) >>> 0
314 d = c
315 c = b
316 b = a
317 a = (t1 + t2) >>> 0
318 }
319 const add = [a, b, c, d, e, f, g, hh]
320 for (let i = 0; i < 8; i++) h[i] = ((h[i] ?? 0) + (add[i] ?? 0)) >>> 0
321 }
322 const out = new Uint8Array(32)
323 const ov = new DataView(out.buffer)
324 for (let i = 0; i < 8; i++) ov.setUint32(i * 4, h[i] ?? 0)
325 return out
326}
327
328/** HMAC-SHA256 (RFC 2104, hex) over `message` with the key's ASCII bytes, as relay.py _mac computes it */
329function hmacHex(key: string, message: string): string {
330 const enc = new TextEncoder()
331 let k = enc.encode(key)
332 if (k.length > BLOCK) k = sha256(k)
333 const ipad = new Uint8Array(BLOCK)
334 const opad = new Uint8Array(BLOCK)
335 for (let i = 0; i < BLOCK; i++) {
336 const b = k[i] ?? 0
337 ipad[i] = b ^ 0x36
338 opad[i] = b ^ 0x5c
339 }
340 const inner = sha256(ipad, enc.encode(message))
341 const outer = sha256(opad, inner)
342 return [...outer].map(b => b.toString(16).padStart(2, '0')).join('')
343}
344
345/** The inbox record's mac: `inbox|id|target_session|expires_ms|text` (relay.py relay_mac) */
346export function relayMac(key: string, id: string, session: string, expiresMs: number, text: string): string {
347 return hmacHex(key, `inbox|${id}|${session}|${expiresMs}|${text}`)
348}
349
350/** The heartbeat's mac: `heartbeat|session|at_ms|busy|surface`, busy 1 or 0, no surface '' (relay.py heartbeat_mac) */
351export function heartbeatMac(key: string, session: string, atMs: number, busy: boolean, surface: string | null): string {
352 return hmacHex(key, `heartbeat|${session}|${atMs}|${busy ? 1 : 0}|${surface ?? ''}`)
353}
354
355/** The done file's mac: `done|id|status|at_ms|answer`, no answer '' (relay.py done_mac) */
356export function doneMac(key: string, id: string, status: string, atMs: number, answer: string | null): string {
357 return hmacHex(key, `done|${id}|${status}|${atMs}|${answer ?? ''}`)
358}
359
360/** The record's mac matches the server's key (compared in full, without an early exit) */
361export function verifyMac(key: string | null, record: unknown): boolean {
362 if (!isRelayKey(key) || typeof record !== 'object' || record === null) return false
363 const r = record as Record<string, unknown>
364 if (typeof r.mac !== 'string' || typeof r.id !== 'string' || typeof r.target_session !== 'string'
365 || typeof r.text !== 'string' || !Number.isSafeInteger(r.expires_ms)) return false
366 const want = relayMac(key, r.id, r.target_session, r.expires_ms as number, r.text)
367 if (want.length !== r.mac.length) return false
368 let diff = 0
369 for (let i = 0; i < want.length; i++) diff |= want.charCodeAt(i) ^ r.mac.charCodeAt(i)
370 return diff === 0
371}
372
373// ---------------------------------------------------------------- which turn carries the question
374
375/**
376 * The turn that starts now carries the question in flight: its text shows the record's marker, or it
377 * has no text at all and starts within CORRELATE_MS of the submit. A turn with any other text is never
378 * the web turn (the plugin's prompt always carries the marker): a prompt typed in that moment keeps
379 * its own answer.
380 */
381export function isFlightTurn(text: unknown, flightId: string, flightAt: number, now: number): boolean {
382 const marker = markerIn(text)
383 if (marker !== null) return marker === flightId
384 const empty = typeof text !== 'string' || text.trim() === ''
385 return empty && now - flightAt <= CORRELATE_MS
386}
387
388/** The last assistant text after the last user message: what the web turn itself said, '' when nothing */
389export function answerSinceLastUser(rows: readonly { role: string; text: string }[]): string {
390 for (let i = rows.length - 1; i >= 0; i--) {
391 const row = rows[i]
392 if (!row) continue
393 if (row.role === 'user' && row.text.trim()) return ''
394 if (row.role === 'assistant' && row.text.trim()) return row.text
395 }
396 return ''
397}
398
399// ---------------------------------------------------------------- words
400
401/** The mod's own words: one table per language, identical keys, English the default */
402export type Strings = {
403 expired: (n: number) => string
404 rejected: (n: number) => string
405 stuck: string
406}
407
408const en: Strings = {
409 expired: n => `Cockpit: ${n} question(s) expired, not asked`,
410 rejected: n => `Cockpit: ${n} question(s) without a valid signature refused`,
411 stuck: 'Cockpit: a question has had no answer for 30 min, the next one may come',
412}
413
414const de: Strings = {
415 expired: n => `Cockpit: ${n} Frage(n) abgelaufen, nicht gestellt`,
416 rejected: n => `Cockpit: ${n} Frage(n) ohne gültige Signatur abgewiesen`,
417 stuck: 'Cockpit: eine Frage hängt seit 30 Min. ohne Antwort, die nächste darf kommen',
418}
419
420export const TABLES: Record<'de' | 'en', Strings> = { de, en }
421types/index.d.ts 37 lines1/** The relay's switch as the mod reads it from `cockpit:` */
2export type RelaySettings = {
3 /** cockpit.enabled is true */
4 enabled: boolean
5 /** cockpit.relay is not false: this session heartbeats and takes cockpit questions addressed to it */
6 relay: boolean
7 /** language of the mod's notes: cockpit.language, else language.conversation */
8 language: 'de' | 'en'
9}
10
11/** The cockpit question this session submitted and has not seen answered yet */
12export type RelayFlight = {
13 /** Record id, YYYYMMDDTHHMMSS-<8 hex> */
14 id: string
15 /** When it was submitted (ms) */
16 at: number
17 /** The main turn that carries it, once turn.start named it */
18 turnId: string | null
19}
20
21declare module 'claude-code' {
22 interface PluginState {
23 'cockpit-relay': {
24 /** The relay settings; null outside a Bridge, in agent tabs, in a -p run or when the config is unreadable */
25 settings: RelaySettings | null
26 /** A main turn of this session runs (turn.start seen, its turn.complete not yet): the relay waits */
27 busy: boolean
28 /** The cockpit question in flight in this session, at most one */
29 flight: RelayFlight | null
30 /** Questions marked stuck while their known turn still ran: its turn.complete still writes the answer */
31 late: { id: string; turnId: string }[]
32 /** The session id this session heartbeats under; null while the relay is off */
33 session: string | null
34 }
35 }
36}
37