Skip to content

The agent that grows with you

test(desktop): fold the canonical owner/deadline probes into three files

Teknium0f214911429e
diff --git a/apps/desktop/src/api/adversarial-attach-deadline.test.ts b/apps/desktop/src/api/adversarial-attach-deadline.test.ts
deleted file mode 100644
index 3d58aa47961..00000000000
--- a/apps/desktop/src/api/adversarial-attach-deadline.test.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { JsonRpcGatewayClient } from '@hermes/shared'
-import { afterEach, expect, test, vi } from 'vitest'
-
-import { HermesGateway } from './client'
-
-afterEach(() => { vi.restoreAllMocks() })
-
-test('the prerequisite attachment shares the caller deadline and cancellation', async () => {
- let now = 1_000
- vi.spyOn(Date, 'now').mockImplementation(() => now)
- vi.spyOn(JsonRpcGatewayClient.prototype, 'connect').mockResolvedValue()
- const calls: Array<{ method: string; timeout?: number; signal?: AbortSignal }> = []
- vi.spyOn(JsonRpcGatewayClient.prototype, 'request').mockImplementation(async (method, _params, timeout, signal) => {
- calls.push({ method, timeout, signal })
-
- if (method === 'session.resume') {
- now += 60
-
- return { session_id: 's', revision: 1, execution_generation: 2, messages: [], info: {} } as never
- }
-
- return { ref: { session_id: 's' }, admission_id: 'a', status: 'queued' } as never
- })
- const client = new HermesGateway()
- await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=fixture')
- const controller = new AbortController()
-
- try {
- await client.request('prompt.submit', { session_id: 's', submission_id: 'one', text: 'hello' }, 100, controller.signal)
- expect(calls).toEqual([
- { method: 'session.resume', timeout: 100, signal: controller.signal },
- { method: 'prompt.submit', timeout: 40, signal: controller.signal }
- ])
- } finally { client.close() }
-})
-
-
-
-test('a prerequisite attachment resolves the launch profile alias before preparing a mutation', async () => {
- vi.spyOn(JsonRpcGatewayClient.prototype, 'connect').mockResolvedValue()
- const calls: Array<{ method: string; params: unknown }> = []
- vi.spyOn(JsonRpcGatewayClient.prototype, 'request').mockImplementation(async (method, params) => {
- calls.push({ method, params })
-
- if (method === 'session.resume') {
- return { session_id: 's', revision: 3, execution_generation: 2, replay_epoch: 'named-owner',
- messages: [], info: { profile_name: 'work', profile_id: '/profiles/work' } } as never
- }
-
- return { session_id: 's', revision: 4, operation: 'branch', branched_session_id: 'child', copied_messages: 0 } as never
- })
- const client = new HermesGateway()
- await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=fixture')
-
- try {
- await expect(client.request('session.branch_stored', { parent_session_id: 's' })).resolves.toMatchObject({ session_id: 'child' })
- expect(calls[1]).toMatchObject({ method: 'session.mutate', params: { profile: 'work', expected_revision: 3 } })
- } finally { client.close() }
-})
diff --git a/apps/desktop/src/api/adversarial-prompt-replay.test.ts b/apps/desktop/src/api/adversarial-prompt-replay.test.ts
deleted file mode 100644
index ced1c713ec2..00000000000
--- a/apps/desktop/src/api/adversarial-prompt-replay.test.ts
+++ /dev/null
@@ -1,63 +0,0 @@
-import { JsonRpcGatewayClient } from '@hermes/shared'
-import { afterEach, expect, test, vi } from 'vitest'
-
-import { HermesGateway } from './client'
-
-afterEach(() => vi.restoreAllMocks())
-
-test('an unanswered prompt is replayed after its response was lost and the socket reconnects', async () => {
- vi.spyOn(JsonRpcGatewayClient.prototype, 'connect').mockResolvedValue()
-
- const pending = { kind: 'approval', prompt_id: 'same-prompt', execution_generation: 2,
- command: 'build', choices: ['once', 'deny'] }
-
- vi.spyOn(JsonRpcGatewayClient.prototype, 'request').mockImplementation(async method => {
- if (method === 'approval.respond') {throw new Error('Gateway connection closed')}
-
- return { session_id: 's', revision: 1, execution_generation: 2, prompts: [pending] } as never
- })
- const client = new HermesGateway()
- const shown: any[] = []
- client.onRequest(request => { shown.push(request) })
- await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=first')
- await client.request('session.resume', { session_id: 's' })
- shown[0].respond({ choice: 'once' })
- await Promise.resolve()
- await Promise.resolve()
- await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=second')
- await client.request('session.resume', { session_id: 's' })
- expect(shown).toHaveLength(2)
- expect(shown[1].replayed).toBe(true)
- client.close()
-})
-
-test('failed answers recover an authoritative pending card once and callbacks cannot cross socket generations', async () => {
- vi.spyOn(JsonRpcGatewayClient.prototype, 'connect').mockResolvedValue()
- const prompt = { kind: 'approval', prompt_id: 'pending', execution_generation: 2, command: 'build', choices: ['once'] }
-
- const wire = vi.spyOn(JsonRpcGatewayClient.prototype, 'request').mockImplementation(async method => {
- if (method === 'approval.respond') { throw new Error('temporary transport failure') }
-
- return { session_id: 's', revision: 1, execution_generation: 2, prompts: [prompt] } as never
- })
-
- const client = new HermesGateway()
- const shown: any[] = []
- client.onRequest(request => { shown.push(request) })
- await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=first')
- await client.request('session.resume', { session_id: 's', profile: 'sibling' })
- shown[0].respond({ choice: 'once' })
- await vi.waitFor(() => expect(shown).toHaveLength(2))
- await client.request('session.resume', { session_id: 's', profile: 'sibling' })
- expect(shown).toHaveLength(2)
- expect(wire.mock.calls.filter(([method]) => method === 'approval.respond')).toHaveLength(1)
- const old = shown[1]
- await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=second')
- await client.request('session.resume', { session_id: 's', profile: 'sibling' })
- const before = wire.mock.calls.length
- old.respond({ choice: 'once' })
- await Promise.resolve()
- expect(wire).toHaveBeenCalledTimes(before)
- expect(shown).toHaveLength(3)
- client.close()
-})
diff --git a/apps/desktop/src/api/canonical-deadline.test.ts b/apps/desktop/src/api/canonical-deadline.test.ts
index 5ffa7300164..663b8f5f1b3 100644
--- a/apps/desktop/src/api/canonical-deadline.test.ts
+++ b/apps/desktop/src/api/canonical-deadline.test.ts
@@ -42,3 +42,54 @@ test.each([60, 120])('compress follow-up spends only the remaining caller budget
client.close()
})
+
+test('the prerequisite attachment shares the caller deadline and cancellation', async () => {
+ let now = 1_000
+ vi.spyOn(Date, 'now').mockImplementation(() => now)
+ vi.spyOn(JsonRpcGatewayClient.prototype, 'connect').mockResolvedValue()
+ const calls: Array<{ method: string; timeout?: number; signal?: AbortSignal }> = []
+ vi.spyOn(JsonRpcGatewayClient.prototype, 'request').mockImplementation(async (method, _params, timeout, signal) => {
+ calls.push({ method, timeout, signal })
+
+ if (method === 'session.resume') {
+ now += 60
+
+ return { session_id: 's', revision: 1, execution_generation: 2, messages: [], info: {} } as never
+ }
+
+ return { ref: { session_id: 's' }, admission_id: 'a', status: 'queued' } as never
+ })
+ const client = new HermesGateway()
+ await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=fixture')
+ const controller = new AbortController()
+
+ try {
+ await client.request('prompt.submit', { session_id: 's', submission_id: 'one', text: 'hello' }, 100, controller.signal)
+ expect(calls).toEqual([
+ { method: 'session.resume', timeout: 100, signal: controller.signal },
+ { method: 'prompt.submit', timeout: 40, signal: controller.signal }
+ ])
+ } finally { client.close() }
+})
+
+test('a prerequisite attachment resolves the launch profile alias before preparing a mutation', async () => {
+ vi.spyOn(JsonRpcGatewayClient.prototype, 'connect').mockResolvedValue()
+ const calls: Array<{ method: string; params: unknown }> = []
+ vi.spyOn(JsonRpcGatewayClient.prototype, 'request').mockImplementation(async (method, params) => {
+ calls.push({ method, params })
+
+ if (method === 'session.resume') {
+ return { session_id: 's', revision: 3, execution_generation: 2, replay_epoch: 'named-owner',
+ messages: [], info: { profile_name: 'work', profile_id: '/profiles/work' } } as never
+ }
+
+ return { session_id: 's', revision: 4, operation: 'branch', branched_session_id: 'child', copied_messages: 0 } as never
+ })
+ const client = new HermesGateway()
+ await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=fixture')
+
+ try {
+ await expect(client.request('session.branch_stored', { parent_session_id: 's' })).resolves.toMatchObject({ session_id: 'child' })
+ expect(calls[1]).toMatchObject({ method: 'session.mutate', params: { profile: 'work', expected_revision: 3 } })
+ } finally { client.close() }
+})
diff --git a/apps/desktop/src/api/canonical-profile-events.test.ts b/apps/desktop/src/api/canonical-profile-events.test.ts
deleted file mode 100644
index 8330e70340d..00000000000
--- a/apps/desktop/src/api/canonical-profile-events.test.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-// @vitest-environment node
-import { expect, test, vi } from 'vitest'
-
-import { HermesGateway } from './client'
-
-test('shared-socket equal-ID streams use replay owners and unknown epochs reconcile without changing controls', async () => {
- 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 calls: any[] = []
- let socket: any
-
- const snapshots: Record<string, any> = {
- alpha: { session_id: 'same', revision: 4, execution_generation: 2, replay_epoch: 'epoch-alpha', prompts: [] },
- beta: { session_id: 'same', revision: 20, execution_generation: 9, replay_epoch: 'epoch-beta', prompts: [] }
- }
-
- server.on('connection', (connected: any) => {
- socket = connected
- socket.on('message', (bytes: Buffer) => {
- const frame = JSON.parse(bytes.toString())
- calls.push(frame)
- const snapshot = snapshots[frame.params.profile]
-
- const result = frame.method === 'session.resume' ? snapshot : frame.method === 'prompt.submit'
- ? { admission_id: 'accepted-beta', ref: { profile_id: 'beta', session_id: 'same' }, status: 'queued' }
- : { ...snapshot, operation: frame.params.operation }
-
- socket.send(JSON.stringify({ jsonrpc: '2.0', id: frame.id, result }))
- })
- })
- const client = new HermesGateway()
- const named: any[] = []
- const wildcard: any[] = []
- client.on('session.info', event => named.push(event))
- client.onAny(event => { if (event.type === 'session.info') {wildcard.push(event)} })
-
- 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`)
- await client.request('session.resume', { session_id: 'same', profile: 'alpha' })
- await client.request('prompt.submit', { session_id: 'same', profile: 'beta', submission_id: 'beta-input', text: 'beta' })
- expect(calls.filter(frame => frame.method === 'session.resume').map(frame => frame.params.profile)).toEqual(['alpha', 'beta'])
-
- const send = (epoch: string, generation: number, revision: number) => socket.send(JSON.stringify({
- jsonrpc: '2.0', method: 'event', params: { type: 'session.info', session_id: 'same',
- replay_epoch: epoch, execution_generation: generation, payload: { revision, execution_generation: generation } }
- }))
-
- snapshots.alpha = { ...snapshots.alpha, revision: 5, execution_generation: 3 }
- snapshots.beta = { ...snapshots.beta, revision: 21, execution_generation: 10 }
- send('epoch-beta', 10, 21)
- send('epoch-alpha', 3, 5)
- await vi.waitFor(() => expect(named).toHaveLength(2))
- expect(named.map(event => event.profile)).toEqual(['beta', 'alpha'])
- expect(wildcard.map(event => event.profile)).toEqual(['beta', 'alpha'])
- await client.request('session.title', { session_id: 'same', profile: 'alpha', title: 'alpha title' })
- expect(calls.find(frame => frame.method === 'session.mutate').params.expected_revision).toBe(5)
- send('unrecognized-epoch', 999, 999)
- await vi.waitFor(() => expect(calls.filter(frame => frame.method === 'session.resume')).toHaveLength(4))
- expect(named).toHaveLength(2)
- await client.request('session.interrupt', { session_id: 'same', profile: 'alpha' })
- expect(calls.find(frame => frame.method === 'session.interrupt').params.execution_generation).toBe(3)
- await expect(client.request('session.interrupt', { session_id: 'same' })).rejects.toThrow('several profiles')
- } finally {
- client.close()
-
- for (const peer of server.clients) {peer.terminate()}
- await new Promise<void>(resolve => server.close(() => resolve()))
- }
-})
diff --git a/apps/desktop/src/api/adversarial-prompt-profile-collision.test.ts b/apps/desktop/src/api/canonical-prompt-owner.test.ts
similarity index 67%
rename from apps/desktop/src/api/adversarial-prompt-profile-collision.test.ts
rename to apps/desktop/src/api/canonical-prompt-owner.test.ts
index 3849d2fe5eb..5573bb77c77 100644
--- a/apps/desktop/src/api/adversarial-prompt-profile-collision.test.ts
+++ b/apps/desktop/src/api/canonical-prompt-owner.test.ts
@@ -98,3 +98,60 @@ test('implicit named-profile creation and explicit follow-ups share one owner an
expect(second.expected_revision).toBe(4)
client.close()
})
+
+test('an unanswered prompt is replayed after its response was lost and the socket reconnects', async () => {
+ vi.spyOn(JsonRpcGatewayClient.prototype, 'connect').mockResolvedValue()
+
+ const pending = { kind: 'approval', prompt_id: 'same-prompt', execution_generation: 2,
+ command: 'build', choices: ['once', 'deny'] }
+
+ vi.spyOn(JsonRpcGatewayClient.prototype, 'request').mockImplementation(async method => {
+ if (method === 'approval.respond') {throw new Error('Gateway connection closed')}
+
+ return { session_id: 's', revision: 1, execution_generation: 2, prompts: [pending] } as never
+ })
+ const client = new HermesGateway()
+ const shown: any[] = []
+ client.onRequest(request => { shown.push(request) })
+ await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=first')
+ await client.request('session.resume', { session_id: 's' })
+ shown[0].respond({ choice: 'once' })
+ await Promise.resolve()
+ await Promise.resolve()
+ await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=second')
+ await client.request('session.resume', { session_id: 's' })
+ expect(shown).toHaveLength(2)
+ expect(shown[1].replayed).toBe(true)
+ client.close()
+})
+
+test('failed answers recover an authoritative pending card once and callbacks cannot cross socket generations', async () => {
+ vi.spyOn(JsonRpcGatewayClient.prototype, 'connect').mockResolvedValue()
+ const prompt = { kind: 'approval', prompt_id: 'pending', execution_generation: 2, command: 'build', choices: ['once'] }
+
+ const wire = vi.spyOn(JsonRpcGatewayClient.prototype, 'request').mockImplementation(async method => {
+ if (method === 'approval.respond') { throw new Error('temporary transport failure') }
+
+ return { session_id: 's', revision: 1, execution_generation: 2, prompts: [prompt] } as never
+ })
+
+ const client = new HermesGateway()
+ const shown: any[] = []
+ client.onRequest(request => { shown.push(request) })
+ await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=first')
+ await client.request('session.resume', { session_id: 's', profile: 'sibling' })
+ shown[0].respond({ choice: 'once' })
+ await vi.waitFor(() => expect(shown).toHaveLength(2))
+ await client.request('session.resume', { session_id: 's', profile: 'sibling' })
+ expect(shown).toHaveLength(2)
+ expect(wire.mock.calls.filter(([method]) => method === 'approval.respond')).toHaveLength(1)
+ const old = shown[1]
+ await client.connect('ws://localhost/api/ws?native_dial=fixture&ticket=second')
+ await client.request('session.resume', { session_id: 's', profile: 'sibling' })
+ const before = wire.mock.calls.length
+ old.respond({ choice: 'once' })
+ await Promise.resolve()
+ expect(wire).toHaveBeenCalledTimes(before)
+ expect(shown).toHaveLength(3)
+ client.close()
+})
diff --git a/apps/desktop/src/api/canonical-protocol-wire.test.ts b/apps/desktop/src/api/canonical-protocol-wire.test.ts
index 14c2adf85b7..c35b420d907 100644
--- a/apps/desktop/src/api/canonical-protocol-wire.test.ts
+++ b/apps/desktop/src/api/canonical-protocol-wire.test.ts
@@ -105,3 +105,71 @@ test('a compress settle resumes under the caller\'s original timeout and abort s
spy.mockRestore()
}
})
+
+test('shared-socket equal-ID streams use replay owners and unknown epochs reconcile without changing controls', async () => {
+ 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 calls: any[] = []
+ let socket: any
+
+ const snapshots: Record<string, any> = {
+ alpha: { session_id: 'same', revision: 4, execution_generation: 2, replay_epoch: 'epoch-alpha', prompts: [] },
+ beta: { session_id: 'same', revision: 20, execution_generation: 9, replay_epoch: 'epoch-beta', prompts: [] }
+ }
+
+ server.on('connection', (connected: any) => {
+ socket = connected
+ socket.on('message', (bytes: Buffer) => {
+ const frame = JSON.parse(bytes.toString())
+ calls.push(frame)
+ const snapshot = snapshots[frame.params.profile]
+
+ const result = frame.method === 'session.resume' ? snapshot : frame.method === 'prompt.submit'
+ ? { admission_id: 'accepted-beta', ref: { profile_id: 'beta', session_id: 'same' }, status: 'queued' }
+ : { ...snapshot, operation: frame.params.operation }
+
+ socket.send(JSON.stringify({ jsonrpc: '2.0', id: frame.id, result }))
+ })
+ })
+ const client = new HermesGateway()
+ const named: any[] = []
+ const wildcard: any[] = []
+ client.on('session.info', event => named.push(event))
+ client.onAny(event => { if (event.type === 'session.info') {wildcard.push(event)} })
+
+ 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`)
+ await client.request('session.resume', { session_id: 'same', profile: 'alpha' })
+ await client.request('prompt.submit', { session_id: 'same', profile: 'beta', submission_id: 'beta-input', text: 'beta' })
+ expect(calls.filter(frame => frame.method === 'session.resume').map(frame => frame.params.profile)).toEqual(['alpha', 'beta'])
+
+ const send = (epoch: string, generation: number, revision: number) => socket.send(JSON.stringify({
+ jsonrpc: '2.0', method: 'event', params: { type: 'session.info', session_id: 'same',
+ replay_epoch: epoch, execution_generation: generation, payload: { revision, execution_generation: generation } }
+ }))
+
+ snapshots.alpha = { ...snapshots.alpha, revision: 5, execution_generation: 3 }
+ snapshots.beta = { ...snapshots.beta, revision: 21, execution_generation: 10 }
+ send('epoch-beta', 10, 21)
+ send('epoch-alpha', 3, 5)
+ await vi.waitFor(() => expect(named).toHaveLength(2))
+ expect(named.map(event => event.profile)).toEqual(['beta', 'alpha'])
+ expect(wildcard.map(event => event.profile)).toEqual(['beta', 'alpha'])
+ await client.request('session.title', { session_id: 'same', profile: 'alpha', title: 'alpha title' })
+ expect(calls.find(frame => frame.method === 'session.mutate').params.expected_revision).toBe(5)
+ send('unrecognized-epoch', 999, 999)
+ await vi.waitFor(() => expect(calls.filter(frame => frame.method === 'session.resume')).toHaveLength(4))
+ expect(named).toHaveLength(2)
+ await client.request('session.interrupt', { session_id: 'same', profile: 'alpha' })
+ expect(calls.find(frame => frame.method === 'session.interrupt').params.execution_generation).toBe(3)
+ await expect(client.request('session.interrupt', { session_id: 'same' })).rejects.toThrow('several profiles')
+ } finally {
+ client.close()
+
+ for (const peer of server.clients) {peer.terminate()}
+ await new Promise<void>(resolve => server.close(() => resolve()))
+ }
+})