The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): Bot Relay sends the canonical bot_relay.deliver shape
Teknium6b949c27f437
diff --git a/apps/desktop/src/api/canonical-bot-relay-deliver.test.ts b/apps/desktop/src/api/canonical-bot-relay-deliver.test.tsnew file mode 100644index 00000000000..b9eecf96ecb--- /dev/null+++ b/apps/desktop/src/api/canonical-bot-relay-deliver.test.ts@@ -0,0 +1,82 @@+// @vitest-environment node+import { readFileSync } from 'node:fs'+import { join } from 'node:path'++import { expect, test } from 'vitest'++import { HermesGateway } from './client'++// The canonical dispatcher refuses any key outside CanonicalBotDeliverParams (4001+// invalid_params, handler never called). Read that closed set from the Python contract so the+// fixture owner refuses exactly what the real one does.+function canonicalDeliverKeys(): Set<string> {+ const source = readFileSync(join(process.cwd(), '..', '..', 'tui_gateway/contracts/canonical_projections.py'), 'utf8')+ const body = source.match(/class CanonicalBotDeliverParams\(Params\):\n((?: {4}\w+:.*\n)+)/)++ expect(body, 'CanonicalBotDeliverParams must exist in canonical_projections.py').toBeTruthy()++ return new Set([...body![1].matchAll(/^ {4}(\w+):/gm)].map(match => match[1]))+}++test('a Bot Relay delivery on a canonical dial carries only keys the owner admits, sender as a bot author', async () => {+ const allowed = canonicalDeliverKeys()+ const wsPackage = 'ws'+ const { WebSocketServer } = await import(wsPackage)+ const server = new WebSocketServer({ host: '127.0.0.1', port: 0 })+ await new Promise<void>(resolve => server.once('listening', resolve))+ const sent: any[] = []+ server.on('connection', (socket: any) => {+ socket.on('message', (bytes: Buffer) => {+ const frame = JSON.parse(bytes.toString())+ sent.push(frame)+ const fields = Object.keys(frame.params).filter(key => !allowed.has(key))++ socket.send(+ JSON.stringify(+ fields.length+ ? {+ jsonrpc: '2.0',+ id: frame.id,+ error: { code: 4001, message: 'invalid_params', data: { reason: 'invalid_params', fields } }+ }+ : {+ jsonrpc: '2.0',+ id: frame.id,+ result: { status: 'settled', delivery_id: frame.params.id, admission_id: 'a1', reply: 'ok' }+ }+ )+ )+ })+ })+ const client = new HermesGateway()++ try {+ const address = server.address() as { port: number }+ await client.connect(`ws://127.0.0.1:${address.port}/api/ws?native_dial=fixture&ticket=one-use`)+ // Exactly what hermes-bots/relay.ts::relayDeliverParams sends.+ await expect(+ client.request('bot_relay.deliver', {+ id: 'env-1',+ profile: 'ops',+ message: 'Message from π€ Scout (@scout): status?',+ from_profile: 'scout',+ from_handle: 'scout',+ from_connection: 'laptop'+ })+ ).resolves.toMatchObject({ status: 'settled', delivery_id: 'env-1' })+ expect(sent.at(-1).params).toEqual({+ id: 'env-1',+ profile: 'ops',+ message: 'Message from π€ Scout (@scout@laptop): status?',+ author: { id: 'bot:laptop/scout', name: 'scout', is_bot: true }+ })+ } finally {+ client.close()++ for (const socket of server.clients) {+ socket.terminate()+ }++ await new Promise<void>(resolve => server.close(() => resolve()))+ }+})diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.tsindex 47565cba600..8d62eedc750 100644--- a/apps/desktop/src/api/canonical-protocol.ts+++ b/apps/desktop/src/api/canonical-protocol.ts@@ -88,6 +88,28 @@ function branchBoundary(params: Record<string, unknown>): Record<string, unknownreturn {}}+const RELAY_HANDLE = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/+const RELAY_SENDER_STAMP = /^(Message from π€ [\s\S]+? \(@)[A-Za-z0-9_-]+(\): )/u++// The canonical `bot_relay.deliver` is a closed key set without the legacy `from_*` sender+// fields. Translate them as the legacy bridge did (tui_gateway/methods_bot_relay.py): a bot+// author keyed by the sender's connection (`delivery_turn_author`), and the message's+// `(@handle)` stamp qualified with that connection so a reply never lands on the recipient's+// own same-named bot (#103731; `<handle>@<connection>` always resolves there).+function canonicalBotDelivery(params: Record<string, unknown>): Record<string, unknown> {+ const { from_profile: rawProfile, from_handle: rawHandle, from_connection: rawConnection, ...rest } = params+ const [profile, connection] = [String(rawProfile ?? '').trim(), String(rawConnection ?? '').trim()]+ const handle = String(rawHandle ?? '').trim().replace(/^@/, '')++ if (!profile) { return rest }++ const author = { id: connection ? `bot:${connection}/${profile}` : `bot:${profile}`, name: handle || profile, is_bot: true }+ const qualify = typeof rest.message === 'string' && RELAY_HANDLE.test(handle) && RELAY_HANDLE.test(connection)+ const message = qualify ? String(rest.message).replace(RELAY_SENDER_STAMP, `$1${handle}@${connection}$2`) : rest.message++ return { ...rest, message, author }+}+const BRANCH_METHODS = new Set(['session.branch', 'session.branch_stored', 'session.branch_whole'])const MUTATION_METHODS = new Set(['session.title', 'session.archive', 'session.compress', ...BRANCH_METHODS])@@ -197,7 +219,7 @@ export class CanonicalDesktopProtocol {if (method === 'approval.respond' || method === 'clarify.respond') { return this.preparePromptResponse(method, params) }- return params+ return method === 'bot_relay.deliver' ? canonicalBotDelivery(params) : params}// Metadata, branch and typed slash directives that travel as canonical `session.mutate`; null otherwise.