Skip to content

Commit 4451e8f

Browse files
rogerbarretoCopilot
andcommitted
test(hosting): extend session key harness
Cover identifier boundaries, escaping, streaming, and session reuse. Fix missing test imports and document opaque hosted identifiers. Refs microsoft#8251 Co-authored-by: Copilot <[email protected]> Copilot-Session: 165465df-61e4-45eb-a147-08b425b4f5bd
1 parent 442290e commit 4451e8f

5 files changed

Lines changed: 163 additions & 38 deletions

File tree

‎dotnet/tests/Microsoft.Agents.AI.Foundry.Hosting.UnitTests/InMemoryAgentSessionStoreTests.cs‎

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,93 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using System;
4+
using System.Collections.Generic;
35
using System.Text.Json;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.Extensions.AI;
49

510
namespace Microsoft.Agents.AI.Foundry.Hosting.UnitTests;
611

712
public sealed class InMemoryAgentSessionStoreTests
813
{
9-
[Fact]
10-
public async Task SaveSessionAsync_CompositeIdentifiersPreserveBoundariesAsync()
14+
[Theory]
15+
[InlineData("cedar:u-elm", "fir", "trail", "cedar", "elm:u-fir", "trail")]
16+
[InlineData("cedar", "elm:c-fir", "trail", "cedar", "elm", "fir:c-trail")]
17+
[InlineData("cedar:c-elm", null, "trail", "cedar", null, "elm:c-trail")]
18+
[InlineData("cedar:u-elm", null, "trail", "cedar", "elm", "trail")]
19+
[InlineData("cedar", "elm", "trail", "maple", "elm", "trail")]
20+
[InlineData(null, "elm", "trail", "cedar", "elm", "trail")]
21+
[InlineData(" ", "elm", "trail", null, "elm", "trail")]
22+
[InlineData(" cedar", "elm", "trail", "cedar", "elm", "trail")]
23+
[InlineData("cedar ", "elm", "trail", "cedar", "elm", "trail")]
24+
[InlineData("Cedar", "elm", "trail", "cedar", "elm", "trail")]
25+
[InlineData("cedar", "elm", "trail", "cedar", "fir", "trail")]
26+
[InlineData("cedar", "elm", "trail", "cedar", null, "trail")]
27+
[InlineData("cedar", " elm", "trail", "cedar", "elm", "trail")]
28+
[InlineData("cedar", "elm ", "trail", "cedar", "elm", "trail")]
29+
[InlineData("cedar", "Elm", "trail", "cedar", "elm", "trail")]
30+
[InlineData("cedar", "elm", "trail", "cedar", "elm", "glade")]
31+
[InlineData("cedar", "elm", " trail", "cedar", "elm", "trail")]
32+
[InlineData("cedar", "elm", "trail ", "cedar", "elm", "trail")]
33+
[InlineData("cedar", "elm", "Trail", "cedar", "elm", "trail")]
34+
[InlineData("cedar", "elm", "", "cedar", "elm", " ")]
35+
public async Task SaveSessionAsync_CompositeIdentifiersPreserveBoundariesAsync(
36+
string? firstAgentName, string? firstUserId, string firstConversationId,
37+
string? secondAgentName, string? secondUserId, string secondConversationId)
1138
{
12-
// Arrange: these identifier tuples collapse to the same colon-delimited string key.
39+
// Arrange
1340
var store = new InMemoryAgentSessionStore();
14-
var agent = new TestAgent("concierge");
41+
var firstAgent = new TestAgent(firstAgentName);
42+
var secondAgent = new TestAgent(secondAgentName);
1543

16-
// Act
17-
await store.SaveSessionAsync(agent, "delta", new TestSession("first"), "bravo:c-charlie");
18-
await store.SaveSessionAsync(agent, "charlie:c-delta", new TestSession("second"), "bravo");
19-
var first = await store.GetSessionAsync(agent, "delta", "bravo:c-charlie");
20-
var second = await store.GetSessionAsync(agent, "charlie:c-delta", "bravo");
44+
// Act & Assert
45+
await store.SaveSessionAsync(firstAgent, firstConversationId, new TestSession("first"), firstUserId);
46+
Assert.Null(await store.GetSessionAsync(secondAgent, secondConversationId, secondUserId));
47+
48+
await store.SaveSessionAsync(secondAgent, secondConversationId, new TestSession("second"), secondUserId);
49+
var first = await store.GetSessionAsync(firstAgent, firstConversationId, firstUserId);
50+
var second = await store.GetSessionAsync(secondAgent, secondConversationId, secondUserId);
2151

22-
// Assert
2352
Assert.Equal("first", Assert.IsType<TestSession>(first).Value);
2453
Assert.Equal("second", Assert.IsType<TestSession>(second).Value);
2554
}
2655

56+
[Theory]
57+
[InlineData(null, null, null, null)]
58+
[InlineData(null, null, "", "")]
59+
[InlineData("", "", null, " \t\r\n")]
60+
[InlineData(null, "elm", "", "elm")]
61+
[InlineData("cedar", null, "cedar", "")]
62+
[InlineData("cedar", null, "cedar", " ")]
63+
[InlineData("cedar", null, "cedar", "\t\r\n")]
64+
[InlineData("cedar", "elm", "cedar", "elm")]
65+
[InlineData(" cedar ", " elm ", " cedar ", " elm ")]
66+
public async Task SaveSessionAsync_EquivalentIdentifiersReuseAndReplaceSessionAsync(
67+
string? firstAgentName, string? firstUserId, string? secondAgentName, string? secondUserId)
68+
{
69+
// Arrange
70+
var store = new InMemoryAgentSessionStore();
71+
var firstAgent = new TestAgent(firstAgentName);
72+
var secondAgent = new TestAgent(secondAgentName);
73+
Assert.NotEqual(firstAgent.Id, secondAgent.Id);
74+
75+
// Act & Assert
76+
await store.SaveSessionAsync(firstAgent, "trail", new TestSession("initial"), firstUserId);
77+
var restored = await store.GetSessionAsync(secondAgent, "trail", secondUserId);
78+
Assert.Equal("initial", Assert.IsType<TestSession>(restored).Value);
79+
80+
await store.SaveSessionAsync(secondAgent, "trail", new TestSession("updated"), secondUserId);
81+
var updated = await store.GetSessionAsync(firstAgent, "trail", firstUserId);
82+
Assert.Equal("updated", Assert.IsType<TestSession>(updated).Value);
83+
}
84+
2785
private sealed class TestSession(string value) : AgentSession
2886
{
2987
public string Value { get; } = value;
3088
}
3189

32-
private sealed class TestAgent(string name) : AIAgent
90+
private sealed class TestAgent(string? name) : AIAgent
3391
{
3492
public override string? Name => name;
3593

@@ -53,14 +111,14 @@ protected override ValueTask<AgentSession> DeserializeSessionCoreAsync(
53111
ValueTask.FromResult<AgentSession>(new TestSession(serializedState.GetString()!));
54112

55113
protected override Task<AgentResponse> RunCoreAsync(
56-
IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages,
114+
IEnumerable<ChatMessage> messages,
57115
AgentSession? session = null,
58116
AgentRunOptions? options = null,
59117
CancellationToken cancellationToken = default) =>
60118
throw new NotSupportedException();
61119

62120
protected override IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingAsync(
63-
IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages,
121+
IEnumerable<ChatMessage> messages,
64122
AgentSession? session = null,
65123
AgentRunOptions? options = null,
66124
CancellationToken cancellationToken = default) =>

‎python/packages/foundry_hosting/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ Native Responses refusal parts are stored as text carrying
8686
`additional_properties["model_output_kind"] == "refusal"` and emitted as
8787
`response.refusal.*` events when streamed back to clients.
8888

89+
`InvocationsHostServer` keeps sessions in memory. When hosted, its session key and
90+
`AgentSession.session_id` are a compact JSON array string containing the platform session ID
91+
and user ID, preserving each identifier's boundaries. Locally, the platform session ID is
92+
used unchanged. Treat the hosted identifier as opaque; sessions do not survive host restarts.
93+
8994
### Workflow checkpoints
9095

9196
`ResponsesHostServer` persists workflow checkpoints durably. By default, it uses the

‎python/packages/foundry_hosting/agent_framework_foundry_hosting/_invocations.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def __init__(
4444
def _partition_key(self) -> str:
4545
"""Get the partition key for the current request.
4646
47-
A partition key is made up of the session ID and user ID. If the request is not
48-
from a hosted environment, the partition key will be just the session ID. In the
47+
A hosted partition key is a JSON array string containing the session ID and user ID,
48+
preserving their boundaries and escaping. Locally, the key is just the session ID. In the
4949
Foundry hosted environment, the partition key is used to maintain isolation between
5050
different sessions and users, such that one user cannot access another user's sessions.
5151

‎python/packages/foundry_hosting/tests/test_invocations.py‎

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
from __future__ import annotations
1313

14+
import json
1415
from collections.abc import AsyncIterator, Iterator
1516
from contextlib import contextmanager
17+
from itertools import product
1618
from unittest.mock import AsyncMock, MagicMock
1719

1820
import pytest
@@ -169,11 +171,20 @@ def test_local_missing_session_id_raises(self) -> None:
169171
with _request_context(), pytest.raises(RuntimeError, match="missing session_id"):
170172
server._partition_key() # pyright: ignore[reportPrivateUsage]
171173

172-
def test_hosted_missing_user_id_raises(self) -> None:
174+
def test_local_ignores_user_id(self) -> None:
175+
server = InvocationsHostServer(_make_agent(response_text="hi"))
176+
with _request_context(session_id="sess-1", user_id="user-1"):
177+
assert server._partition_key() == "sess-1" # pyright: ignore[reportPrivateUsage]
178+
179+
@pytest.mark.parametrize(
180+
("session_id", "user_id"),
181+
[(None, "user-1"), ("", "user-1"), ("sess-1", None), ("sess-1", ""), (None, None)],
182+
)
183+
def test_hosted_requires_both_identifiers(self, session_id: str | None, user_id: str | None) -> None:
173184
server = InvocationsHostServer(_make_agent(response_text="hi"))
174185
server.config.is_hosted = True
175186
with (
176-
_request_context(call_id="call-1", session_id="sess-1"),
187+
_request_context(call_id="call-1", session_id=session_id, user_id=user_id),
177188
pytest.raises(RuntimeError, match="missing session_id or user_id"),
178189
):
179190
server._partition_key() # pyright: ignore[reportPrivateUsage]
@@ -184,6 +195,20 @@ def test_hosted_returns_composite_key(self) -> None:
184195
with _request_context(call_id="call-1", session_id="sess-1", user_id="user-1"):
185196
assert server._partition_key() == '["sess-1","user-1"]' # pyright: ignore[reportPrivateUsage]
186197

198+
def test_hosted_keys_preserve_identifier_values(self) -> None:
199+
server = InvocationsHostServer(_make_agent(response_text="hi"))
200+
server.config.is_hosted = True
201+
identifiers = ["part", "part:part", "part,part", "[part]", 'part"\\', "part\n\t", "\u00e9", r"\u00e9", " part "]
202+
keys: set[str] = set()
203+
204+
for session_id, user_id in product(identifiers, repeat=2):
205+
with _request_context(call_id="call-1", session_id=session_id, user_id=user_id):
206+
key = server._partition_key() # pyright: ignore[reportPrivateUsage]
207+
208+
assert json.loads(key) == [session_id, user_id]
209+
assert key not in keys
210+
keys.add(key)
211+
187212

188213
# endregion
189214

@@ -259,27 +284,64 @@ async def test_session_is_reused_across_requests(self) -> None:
259284
assert list(server._sessions) == ["sess-1"] # pyright: ignore[reportPrivateUsage]
260285
assert agent.calls[0]["session"] is agent.calls[1]["session"]
261286

262-
async def test_hosted_sessions_preserve_identifier_boundaries(self) -> None:
263-
agent = _make_agent(response_text="ok")
287+
@pytest.mark.parametrize("stream", [False, True])
288+
@pytest.mark.parametrize(
289+
("first_session_id", "first_user_id", "second_session_id", "second_user_id"),
290+
[
291+
("session:segment", "user", "session", "segment:user"),
292+
("session,segment", "user", "session", "segment,user"),
293+
("session", "first-user", "session", "second-user"),
294+
("first-session", "user", "second-session", "user"),
295+
],
296+
)
297+
async def test_hosted_sessions_preserve_identifier_boundaries(
298+
self,
299+
stream: bool,
300+
first_session_id: str,
301+
first_user_id: str,
302+
second_session_id: str,
303+
second_user_id: str,
304+
) -> None:
305+
agent = _make_agent(response_text="ok", stream_texts=["ok"])
264306
server = InvocationsHostServer(agent)
265307
server.config.is_hosted = True
266-
request = _make_request({"message": "Hi"})
267-
268-
with _request_context(session_id="sess", user_id="user:admin"):
269-
await server._handle_invoke(request) # pyright: ignore[reportPrivateUsage]
270-
first_session = agent.calls[-1]["session"]
271-
272-
with _request_context(session_id="sess:user", user_id="admin"):
273-
await server._handle_invoke(request) # pyright: ignore[reportPrivateUsage]
274-
second_session = agent.calls[-1]["session"]
275-
276-
with _request_context(session_id="sess", user_id="user:admin"):
277-
await server._handle_invoke(request) # pyright: ignore[reportPrivateUsage]
278-
repeated_session = agent.calls[-1]["session"]
279-
280-
assert first_session is not second_session
281-
assert repeated_session is first_session
308+
identifiers = [(first_session_id, first_user_id), (second_session_id, second_user_id)]
309+
sessions: list[AgentSession] = []
310+
311+
for session_id, user_id in identifiers:
312+
with _request_context(call_id="call-1", session_id=session_id, user_id=user_id):
313+
response = await server._handle_invoke( # pyright: ignore[reportPrivateUsage]
314+
_make_request({"message": "Hi", "stream": stream})
315+
)
316+
if isinstance(response, StreamingResponse):
317+
assert await _collect_stream(response) == "ok"
318+
else:
319+
assert bytes(response.body).decode() == "ok"
320+
assert response.status_code == 200
321+
322+
session = agent.calls[-1]["session"]
323+
assert isinstance(session, AgentSession)
324+
assert session.state == {}
325+
session.state["turn"] = (session_id, user_id)
326+
sessions.append(session)
327+
328+
assert sessions[0] is not sessions[1]
329+
assert sessions[0].session_id != sessions[1].session_id
282330
assert len(server._sessions) == 2 # pyright: ignore[reportPrivateUsage]
283331

332+
for (session_id, user_id), session in zip(identifiers, sessions):
333+
with _request_context(call_id="call-2", session_id=session_id, user_id=user_id):
334+
response = await server._handle_invoke( # pyright: ignore[reportPrivateUsage]
335+
_make_request({"message": "Continue", "stream": stream})
336+
)
337+
if isinstance(response, StreamingResponse):
338+
assert await _collect_stream(response) == "ok"
339+
else:
340+
assert bytes(response.body).decode() == "ok"
341+
assert response.status_code == 200
342+
343+
assert agent.calls[-1]["session"] is session
344+
assert session.state == {"turn": (session_id, user_id)}
345+
284346

285347
# endregion

‎python/samples/04-hosting/foundry-hosted-agents/invocations/break_glass/main.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
load_dotenv()
1818

1919

20-
# In-memory session store — keyed by session ID.
20+
# In-memory session store — keyed by session and user IDs when a user is present.
2121
# WARNING: This is lost on restart. Use durable storage in production.
2222
_sessions: dict[str, AgentSession] = {}
2323

@@ -43,8 +43,8 @@
4343
def get_session_partition_key() -> str:
4444
"""Get the partition key for the current request.
4545
46-
A partition key is made up of the session ID and user ID. If the request is not
47-
from a hosted environment, the partition key will be just the session ID. In the
46+
A partition key is a JSON array string containing the session ID and user ID when a user ID
47+
is present, preserving their boundaries and escaping. Otherwise, the key is just the session ID. In the
4848
Foundry hosted environment, the partition key is used to maintain isolation between
4949
different sessions and users, such that one user cannot access another user's sessions.
5050

0 commit comments

Comments
 (0)