From 3ede28c6beb6d77e8377dc6c1e981af65b3077b8 Mon Sep 17 00:00:00 2001 From: Adam Weidman Date: Tue, 12 May 2026 17:50:36 -0400 Subject: [PATCH] test(core): add tests for composite session key in RemoteSessionInvocation --- .../agents/remote-session-invocation.test.ts | 89 +++++++++++++++++-- 1 file changed, 82 insertions(+), 7 deletions(-) diff --git a/packages/core/src/agents/remote-session-invocation.test.ts b/packages/core/src/agents/remote-session-invocation.test.ts index f096d72a89..1777e5e62f 100644 --- a/packages/core/src/agents/remote-session-invocation.test.ts +++ b/packages/core/src/agents/remote-session-invocation.test.ts @@ -216,7 +216,7 @@ describe('RemoteSessionInvocation', () => { RemoteSessionInvocation as unknown as { sessionState: Map; } - ).sessionState.set('test-agent', priorState); + ).sessionState.set('test-agent::http://test-agent/card', priorState); setupMockSession(); @@ -258,7 +258,7 @@ describe('RemoteSessionInvocation', () => { RemoteSessionInvocation as unknown as { sessionState: Map; } - ).sessionState.get('test-agent'); + ).sessionState.get('test-agent::http://test-agent/card'); expect(storedState).toEqual(newState); }); @@ -496,11 +496,12 @@ describe('RemoteSessionInvocation', () => { // --------------------------------------------------------------------------- describe('SessionState Management', () => { - it('should use definition.name as session state key', async () => { + it('should use composite name::url as session state key', async () => { const secondDefinition: RemoteAgentDefinition = { ...mockDefinition, name: 'other-agent', displayName: 'Other Agent', + agentCardUrl: 'http://other-agent/card', }; // First agent @@ -533,9 +534,81 @@ describe('RemoteSessionInvocation', () => { } ).sessionState; - // Each agent should have its own entry - expect(stateMap.get('test-agent')).toEqual({ contextId: 'ctx-a' }); - expect(stateMap.get('other-agent')).toEqual({ contextId: 'ctx-b' }); + // Each agent should have its own entry keyed by name::url + expect(stateMap.get('test-agent::http://test-agent/card')).toEqual({ + contextId: 'ctx-a', + }); + expect(stateMap.get('other-agent::http://other-agent/card')).toEqual({ + contextId: 'ctx-b', + }); + }); + + it('should isolate same-name agents with different URLs', async () => { + const defA: RemoteAgentDefinition = { + ...mockDefinition, + agentCardUrl: 'http://host-a/card', + }; + const defB: RemoteAgentDefinition = { + ...mockDefinition, + agentCardUrl: 'http://host-b/card', + }; + + // Agent A + setupMockSession({ sessionState: { contextId: 'ctx-a' } }); + const invA = new RemoteSessionInvocation( + defA, + mockContext, + { query: 'hi' }, + mockMessageBus, + ); + await invA.execute({ abortSignal: new AbortController().signal }); + + // Agent B (same name, different URL) + setupMockSession({ sessionState: { contextId: 'ctx-b' } }); + const invB = new RemoteSessionInvocation( + defB, + mockContext, + { query: 'hi' }, + mockMessageBus, + ); + await invB.execute({ abortSignal: new AbortController().signal }); + + const stateMap = ( + RemoteSessionInvocation as unknown as { + sessionState: Map; + } + ).sessionState; + + expect(stateMap.get('test-agent::http://host-a/card')).toEqual({ + contextId: 'ctx-a', + }); + expect(stateMap.get('test-agent::http://host-b/card')).toEqual({ + contextId: 'ctx-b', + }); + }); + + it('should fall back to name-only key when URL is unavailable', async () => { + const noUrlDef: RemoteAgentDefinition = { + ...mockDefinition, + agentCardUrl: undefined, + }; + + setupMockSession({ sessionState: { contextId: 'ctx-no-url' } }); + const inv = new RemoteSessionInvocation( + noUrlDef, + mockContext, + { query: 'hi' }, + mockMessageBus, + ); + await inv.execute({ abortSignal: new AbortController().signal }); + + const stateMap = ( + RemoteSessionInvocation as unknown as { + sessionState: Map; + } + ).sessionState; + + expect(stateMap.get('test-agent')).toEqual({ contextId: 'ctx-no-url' }); }); it('should persist state even on error', async () => { @@ -562,7 +635,9 @@ describe('RemoteSessionInvocation', () => { } ).sessionState; - expect(stateMap.get('test-agent')).toEqual(stateOnError); + expect(stateMap.get('test-agent::http://test-agent/card')).toEqual( + stateOnError, + ); }); }); });