Skip to content

The agent that grows with you

fix(desktop): a session routed onto a new socket re-attaches before it submits

teknium1307dac202f0f
diff --git a/apps/desktop/electron/local-gateway.ts b/apps/desktop/electron/local-gateway.ts
index f3e0e40cc68..2f93c65fa68 100644
--- a/apps/desktop/electron/local-gateway.ts
+++ b/apps/desktop/electron/local-gateway.ts
@@ -162,8 +162,8 @@ async function privateNode(file: string, kind: 'directory' | 'socket' | 'file')
/** The endpoint a `?profile=<name>` request is scoped to on a shared host descriptor: the same
* daemon, ticket minted for the sibling profile's home (`profiles/<name>` under the launch root).
* The gateway refuses a ticket whose profile it does not serve, so a bad name fails closed. */
-export function routedGatewayEndpoint(endpoint: GatewayEndpoint, url: string, launchHome: string): GatewayEndpoint {
- const profile = new URL(url).searchParams.get('profile')?.trim()
+export function routedGatewayEndpoint(endpoint: GatewayEndpoint, urlOrProfile: string, launchHome: string): GatewayEndpoint {
+ const profile = (/^[a-z]+:\/\//.test(urlOrProfile) ? new URL(urlOrProfile).searchParams.get('profile') : urlOrProfile)?.trim()
if (!profile || profile === 'current') {return endpoint}
const own = path.basename(endpoint.profile_id)
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index db9a88ec6f5..ddab5bfc656 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -1,7 +1,7 @@
import { execFileSync, spawn } from 'node:child_process'
import type { GatewayEndpoint } from './local-gateway'
-import { configureWindowsGatewayTicketClient, createLocalGatewayDials, ensureLocalGateway, mintLocalGatewayTicket, nativeGatewayHttpHeaders, redialLocalGateway, runGatewayEnsure } from './local-gateway'
+import { configureWindowsGatewayTicketClient, createLocalGatewayDials, ensureLocalGateway, mintLocalGatewayTicket, nativeGatewayHttpHeaders, redialLocalGateway, routedGatewayEndpoint, runGatewayEnsure } from './local-gateway'
import { mintGatewayTicketWithPython } from './local-gateway-python'
const localGatewayDials = createLocalGatewayDials()
configureWindowsGatewayTicketClient(async (endpoint, purpose) => {
@@ -8361,7 +8361,9 @@ async function freshGatewayWsUrl(profile, webContentsId) {
ensure: () => ensureBackend(profile),
forget: () => forgetLocalGatewayDescriptor(profile),
use: async current => {
- const ticket = await mintLocalGatewayTicket(current.gatewayEndpoint)
+ // A shared-primary descriptor answers for a sibling profile too: the socket's
+ // ticket must name THAT profile's home or its sessions are another owner's.
+ const ticket = await mintLocalGatewayTicket(routedGatewayEndpoint(current.gatewayEndpoint, String(profile ?? ''), HERMES_HOME))
return localGatewayDials.prepare(current.baseUrl, ticket, webContentsId)
}
@@ -15154,7 +15156,7 @@ ipcMain.handle('hermes:gateway:ws-url-for', async (_event, payload) => {
ensure: () => ensureRegistryBackend(payload?.connectionId, payload?.profile),
forget: () => forgetRegistryLocalGatewayDescriptor(payload?.connectionId, payload?.profile),
use: async (current: typeof connection) => {
- const ticket = await mintLocalGatewayTicket(current.gatewayEndpoint)
+ const ticket = await mintLocalGatewayTicket(routedGatewayEndpoint(current.gatewayEndpoint, String(payload?.profile ?? ''), HERMES_HOME))
return localGatewayDials.prepare(current.baseUrl, ticket, _event.sender.id)
}
diff --git a/apps/desktop/src/api/client.ts b/apps/desktop/src/api/client.ts
index eb72cc5c660..e31fcfe6a6d 100644
--- a/apps/desktop/src/api/client.ts
+++ b/apps/desktop/src/api/client.ts
@@ -36,6 +36,8 @@ export const GATEWAY_NOT_CONNECTED_MESSAGE = 'Hermes gateway is not connected'
// the same `onRequest` registry as a request whose `respond` issues the RPC.
const CANONICAL_PROMPT_EVENTS: Record<string, 'approval' | 'clarify'> = { 'approval.request': 'approval', 'clarify.request': 'clarify' }
+const ATTACH_REQUIRED = new Set(['prompt.submit', 'approval.respond', 'clarify.respond', 'session.interrupt', 'prompt.cancel'])
+
export class HermesGateway extends JsonRpcGatewayClient {
private canonical = false
private readonly protocol = new CanonicalDesktopProtocol()
@@ -101,12 +103,23 @@ export class HermesGateway extends JsonRpcGatewayClient {
override async connect(wsUrl: string): Promise<void> {
this.canonical = new URL(wsUrl).searchParams.has('native_dial')
+ this.attached.clear()
return super.connect(wsUrl)
}
+ // Sessions attached over THIS socket. An authority subscription is per
+ // transport: when routing moves a session to another socket (a profile split,
+ // a redial) the new socket must resume before it may submit or respond.
+ private attached = new Set<string>()
+
override async request<T>(method: string, params: Record<string, unknown> = {}, timeoutMs?: number, signal?: AbortSignal): Promise<T> {
if (!this.canonical) { return super.request<T>(method, params, timeoutMs, signal) }
+ const sid = typeof params.session_id === 'string' ? params.session_id : null
+
+ if (sid && ATTACH_REQUIRED.has(method) && !this.attached.has(sid)) {
+ await this.request('session.resume', { session_id: sid, defer_history: true, omit_messages: true, ...(params.profile ? { profile: params.profile } : {}) })
+ }
const prepared = this.protocol.prepare(method, params)
const wireMethod = this.protocol.wire(method, prepared)
@@ -119,6 +132,8 @@ export class HermesGateway extends JsonRpcGatewayClient {
// Prompts still open on the authority re-deliver like `open_requests` after a reconnect.
const sid = (settled as { session_id?: string }).session_id
+ if (sid) { this.attached.add(sid) }
+
for (const prompt of ((settled as { prompts?: Array<Record<string, unknown>> }).prompts ?? [])) {
this.deliverCanonicalPrompt({ type: `${prompt.kind}.request`, session_id: sid, payload: prompt }, true)
}