The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): fence idle queue snapshots by authority epoch
David Dudok de Wit230209083fce
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/execution-authority.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/execution-authority.test.tsxindex 726b4d00eb1..8cc6facc126 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/execution-authority.test.tsx+++ b/apps/desktop/src/app/session/hooks/use-message-stream/execution-authority.test.tsx@@ -2,8 +2,25 @@ import type { GatewayEventName } from '@hermes/shared'import { act, cleanup } from '@testing-library/react'import { afterEach, expect, it } from 'vitest'+import { getQueuedPrompts } from '@/store/composer-queue'+import { renderMessageStream } from './test-harness'+it('refreshes an idle shared queue and does not let a queued cancellation end a running turn', () => {+ const stream = renderMessageStream('queue-session')+ const send = (event: Record<string, unknown>) => act(() => stream.handleEvent({ session_id: 'queue-session', ...event } as never))+ send({ type: 'message.start', authority_epoch: 2, execution_generation: 4 })+ send({ type: 'message.complete', authority_epoch: 2, execution_generation: 4, payload: { text: 'done' } })+ const snapshot = { authority_epoch: 2, execution_generation: 4, stored_session_id: 'queue-session', running: false }+ send({ type: 'session.info', payload: { ...snapshot, pending_submissions: [{ admission_id: 'queued', user: 'later', status: 'queued' }] } })+ expect(getQueuedPrompts('queue-session').map(row => row.id)).toEqual(['queued'])+ send({ type: 'session.info', payload: { ...snapshot, pending_submissions: [] } })+ expect(getQueuedPrompts('queue-session')).toEqual([])+ send({ type: 'message.start', authority_epoch: 2, execution_generation: 5 })+ send({ type: 'message.complete', authority_epoch: 2, admission_id: 'queued', payload: { admission_id: 'queued', outcome: 'cancelled' } })+ expect(stream.state().busy).toBe(true)+})+afterEach(cleanup)it('rejects stale snapshots and terminal frames across generations and owner epochs', () => {diff --git a/apps/desktop/src/lib/execution-authority.test.ts b/apps/desktop/src/lib/execution-authority.test.tsindex 08573acd99a..697ef1af2cd 100644--- a/apps/desktop/src/lib/execution-authority.test.ts+++ b/apps/desktop/src/lib/execution-authority.test.ts@@ -54,6 +54,26 @@ function fence(authorities: Map<string, unknown>, event: GatewayEvent): boolean}describe('execution authority fence over real owner frames', () => {+ it('accepts idle owner queue snapshots while rejecting retired owner snapshots', () => {+ const authorities = new Map()+ expect(acceptExecutionEvent(authorities, 's', 'message.complete', { authority_epoch: 2, execution_generation: 4 })).toBe(true)+ const snapshot = { payload: { authority_epoch: 2, execution_generation: 4, running: false, pending: [] } }+ expect(acceptExecutionEvent(authorities, 's', 'session.info', snapshot)).toBe(true)+ expect(acceptExecutionEvent(authorities, 's', 'session.info', { payload: { ...snapshot.payload, authority_epoch: 3 } })).toBe(true)+ expect(acceptExecutionEvent(authorities, 's', 'session.info', snapshot)).toBe(false)+ })++ it('ignores admission-only cancellations without retiring the current execution', () => {+ const authorities = new Map()+ acceptExecutionEvent(authorities, 's', 'message.start', { authority_epoch: 2, execution_generation: 4 })+ const before = authorities.get('s')+ expect(acceptExecutionEvent(authorities, 's', 'message.complete', {+ authority_epoch: 2, admission_id: 'queued', payload: { admission_id: 'queued', outcome: 'cancelled' }+ })).toBe(false)+ expect(authorities.get('s')).toBe(before)+ expect(before.terminal).toBe(false)+ expect(acceptExecutionEvent(authorities, 's', 'message.complete', {})).toBe(false)+ })it('installs authority from the first claimed frame and fences a retired epoch', () => {const { socket, received } = connectedClient()const authorities = new Map()diff --git a/apps/desktop/src/lib/execution-authority.ts b/apps/desktop/src/lib/execution-authority.tsindex 008c06f6abe..e2141121667 100644--- a/apps/desktop/src/lib/execution-authority.ts+++ b/apps/desktop/src/lib/execution-authority.ts@@ -72,7 +72,7 @@ export function acceptExecutionEvent(const lifecycle = LIFECYCLE_TYPES.includes(type)const previous = authorities.get(key)const payload = event?.payload as Record<string, unknown> | undefined- const stamp = executionStamp(event)+ const stamp = executionStamp(event) ?? (type === 'session.info' ? executionStamp(payload) : null)// Output may follow the current owner, but cannot establish another one.// Keep legacy unversioned output compatible; fence stamped late frames.diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex ad4f7b09432..951356c4a7e 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -204,7 +204,7 @@ class SessionAuthority:# this is the only place a viewer learns the CAS revision a later# prepared mutation must present.live.event_stream.publish(ref.session_id, {- 'stored_session_id': ref.session_id, 'pending': pending,+ 'stored_session_id': ref.session_id, 'pending': pending, 'authority_epoch': self.epoch,'desktop_protocol': CANONICAL_GATEWAY_PROTOCOL,'running': handle.execution_state == 'running','execution_generation': handle.execution_generation,