Skip to content

The agent that grows with you

fix(tui): validate a remote gateway's receipts against the owner identity

Teknium9d245c9c0a90
diff --git a/ui-tui/src/__tests__/inkRemoteReceiptHome.test.tsx b/ui-tui/src/__tests__/inkRemoteReceiptHome.test.tsx
new file mode 100644
index 00000000000..bc042c6f16e
--- /dev/null
+++ b/ui-tui/src/__tests__/inkRemoteReceiptHome.test.tsx
@@ -0,0 +1,118 @@
+import { mkdtempSync, realpathSync, rmSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { PassThrough } from 'node:stream'
+
+import { renderSync, Text } from '@hermes/ink'
+import { useStore } from '@nanostores/react'
+import React from 'react'
+import { expect, it, vi } from 'vitest'
+
+import { createGatewayEventHandler } from '../app/createGatewayEventHandler.js'
+import { captureDestination } from '../app/submissionDestination.js'
+import { turnController } from '../app/turnController.js'
+import { $uiState, patchUiState, resetUiState } from '../app/uiStore.js'
+import { useSubmission } from '../app/useSubmission.js'
+import { canonicalEvent, canonicalResult } from '../canonicalGateway.js'
+import { useQueue } from '../hooks/useQueue.js'
+import { t } from '../i18n/runtime.js'
+
+// C22: an explicit HERMES_TUI_GATEWAY_URL owner on another home/machine answers receipts naming ITS
+// profile_id; Ink validates them against the owner identity from the snapshot, not its local
+// HERMES_HOME, and still rejects a wrong-session receipt.
+const row = (admission_id: string, input_id: string, text: string, status = 'queued') => ({
+ admission_id, input_id, text, status, sequence: 1, outcome: null, authority_epoch: 1, execution_generation: null,
+ ref: { profile_id: '/tmp/profile', session_id: 'stored-owner' }
+})
+
+function mount(receiptHome: string, receiptSession = 'stored-owner', busyInputMode: 'queue' | 'interrupt' | 'steer' = 'queue', cancel: () => Promise<unknown> = () => Promise.resolve({ status: 'terminal', outcome: 'cancelled' })) {
+ // macOS: tmpdir() is /var/..., a symlink to /private/var; captureDestination() realpaths
+ // HERMES_HOME, so receipts must carry the canonical home or target_profile_home never matches.
+ const home = realpathSync(mkdtempSync(join(tmpdir(), 'ink-busy-admit-')))
+ vi.stubEnv('HERMES_HOME', home)
+ resetUiState()
+
+ vi.stubEnv('HERMES_TUI_GATEWAY_URL', 'ws://remote.test/api/ws')
+ const info = { model: 'test', skills: {}, tools: {}, profile_name: 'default', profile_id: '/srv/remote-owner/.hermes',
+ stored_session_id: 'stored-owner', execution_epoch: '1', execution_generation: 1, running: true }
+
+ patchUiState({ sid: 'owner', info, busy: true, status: 'running…', busyInputMode })
+ const calls: Array<{ method: string; params: any }> = []
+
+ const request = vi.fn((method: string, params: any) => {
+ calls.push({ method, params })
+
+ if (method === 'input.detect_drop') { return Promise.resolve({ matched: false }) }
+
+ if (method === 'prompt.submit') {
+ return Promise.resolve({ admission_id: `adm-${params.submission_id}`, input_id: params.submission_id,
+ target_session_id: receiptSession, target_profile_home: receiptHome === 'local' ? home : receiptHome, status: 'queued' })
+ }
+
+ if (method === 'prompt.cancel') { return cancel() }
+ throw new Error(`unexpected RPC: ${method}`)
+ })
+
+ const gw = { request, isCanonical: true } as any
+ let queue!: ReturnType<typeof useQueue>
+ let submission!: ReturnType<typeof useSubmission>
+
+ const noop = () => {}
+
+ const onEvent = createGatewayEventHandler({
+ composer: { dequeue: () => undefined, queueEditRef: { current: null }, sendQueued: noop, setInput: noop },
+ gateway: { gw, rpc: async () => null },
+ session: { STARTUP_RESUME_ID: '', colsRef: { current: 80 }, newSession: noop, resetSession: noop, resumeById: noop, setCatalog: noop },
+ submission: { submitRef: { current: noop } }, system: { bellOnComplete: false, sys: noop },
+ transcript: { appendMessage: noop, panel: noop, setHistoryItems: noop },
+ voice: { setProcessing: noop, setRecording: noop, setVoiceEnabled: noop }
+ } as any)
+
+ // The wire shape the gateway fans out; the client translates it exactly once.
+ const fanout = (pending: any[]) => onEvent(canonicalEvent({ type: 'session.info', session_id: 'owner',
+ payload: { stored_session_id: 'stored-owner', pending, running: true, execution_generation: 1, revision: 3,
+ execution_epoch: '1' } } as any) as any)
+
+ function Harness() {
+ const ui = useStore($uiState)
+ queue = useQueue(gw)
+ submission = useSubmission({
+ appendMessage: noop,
+ composerActions: { ...queue, prependQueue: queue.prependQ, takeQueue: queue.takeQ, removeQueue: queue.removeQ,
+ pushHistory: noop, clearIn: noop } as any,
+ composerRefs: { queueRef: queue.queueRef, queueEditRef: queue.queueEditRef, tokensRef: { current: [] } } as any,
+ composerState: { input: '', inputBuf: [], completions: [] } as any,
+ gw, setLastUserMsg: noop, slashRef: { current: () => true },
+ submitRef: { current: noop }, sys: noop
+ })
+
+ return <Text>{ui.status} {queue.queuedDisplay.join('|')}</Text>
+ }
+
+ const instance = renderSync(<Harness />, {
+ stdin: new PassThrough() as any,
+ stdout: Object.assign(new PassThrough(), { columns: 80, rows: 20, isTTY: false }) as any,
+ stderr: new PassThrough() as any, patchConsole: false
+ })
+
+ return {
+ calls, request, fanout,
+ get queue() { return queue }, get submission() { return submission },
+ cleanup() { instance.unmount(); turnController.fullReset(); resetUiState(); vi.unstubAllEnvs(); rmSync(home, { recursive: true, force: true }) }
+ }
+}
+
+it.each([
+ ['the remote owner home', '/srv/remote-owner/.hermes', 'stored-owner', false],
+ ['a wrong session', '/srv/remote-owner/.hermes', 'someone-else', true]
+] as const)('a remote gateway receipt naming %s is confirmed only for the owner destination', async (_label, receiptHome, receiptSession, refused) => {
+ const h = mount(receiptHome, receiptSession)
+
+ try {
+ h.submission.dispatchSubmission('follower while busy')
+ await expect.poll(() => h.calls.filter(c => c.method === 'prompt.submit').length).toBe(1)
+ // A confirmed receipt hands the row to the durable FIFO; a refused one keeps it local.
+ await expect.poll(() => h.queue.queueRef.current.length).toBe(refused ? 1 : 0)
+ expect($uiState.get().status === t('canonical.submit.statusAdmissionUnconfirmed')).toBe(refused)
+ } finally { h.cleanup() }
+})
diff --git a/ui-tui/src/app/submissionCore.ts b/ui-tui/src/app/submissionCore.ts
index 4712feedddc..6692ec3edb7 100644
--- a/ui-tui/src/app/submissionCore.ts
+++ b/ui-tui/src/app/submissionCore.ts
@@ -168,7 +168,7 @@ function settleAdmissionReply(item: QueueItem, r: PromptSubmitResponse, ctx: Adm
(r?.input_id ?? r?.admission_id) === item.submissionId &&
Boolean(destination.storedSid) &&
r?.target_session_id === destination.storedSid &&
- r?.target_profile_home === destination.profileHome &&
+ r?.target_profile_home === (destination.ownerHome ?? destination.profileHome) &&
['queued', 'started', 'terminal', 'unknown'].includes(r?.status ?? '')
item.settle?.(accepted)
diff --git a/ui-tui/src/app/submissionDestination.ts b/ui-tui/src/app/submissionDestination.ts
index 904beea62f6..1073c1577ab 100644
--- a/ui-tui/src/app/submissionDestination.ts
+++ b/ui-tui/src/app/submissionDestination.ts
@@ -9,6 +9,8 @@ export interface SubmissionDestination {
readonly storedSid?: string | null
readonly profile: string
readonly profileHome: string
+ /** The owner's profile identity its receipts name (`target_profile_home`). */
+ readonly ownerHome?: string
}
// Resolve existing ancestors too: a new profile may not have been created yet.
@@ -28,12 +30,18 @@ function canonicalHome(path: string): string {
export function captureDestination(): SubmissionDestination {
const { sid, info } = getUiState()
+ const profileHome = canonicalHome(resolve(process.env.HERMES_HOME ?? join(homedir(), '.hermes')))
+ // An explicit HERMES_TUI_GATEWAY_URL may serve another home or machine: its receipts name the
+ // owner's profile_id (from the session snapshot), not this client's HERMES_HOME (C22). The local
+ // home still keys the offline journal and image cache.
+ const owner = process.env.HERMES_TUI_GATEWAY_URL?.trim() ? (info as { profile_id?: unknown } | null)?.profile_id : undefined
return Object.freeze({
sid,
storedSid: info?.stored_session_id || null,
profile: info?.profile_name || 'default',
- profileHome: canonicalHome(resolve(process.env.HERMES_HOME ?? join(homedir(), '.hermes')))
+ profileHome,
+ ownerHome: typeof owner === 'string' && owner ? owner : profileHome
})
}
diff --git a/ui-tui/src/hooks/useQueue.ts b/ui-tui/src/hooks/useQueue.ts
index 50f85d9ac1c..d9db4b847c7 100644
--- a/ui-tui/src/hooks/useQueue.ts
+++ b/ui-tui/src/hooks/useQueue.ts
@@ -355,7 +355,7 @@ export function useQueue(gw?: { request: (method: string, params: Record<string,
item.submissionId === (receipt.input_id ?? receipt.admission_id) &&
Boolean(item.destination?.storedSid) &&
item.destination?.storedSid === receipt.target_session_id &&
- item.destination?.profileHome === receipt.target_profile_home
+ (item.destination?.ownerHome ?? item.destination?.profileHome) === receipt.target_profile_home
)
if (!item) {