AgentSessionManager
AgentPrism.Core.dllManages the lifecycle of named sessions: loads, restores, and saves them through ISessionStore.
public sealed class AgentSessionManagerInheritance
Section titled “Inheritance”Inherited Members
Section titled “Inherited Members”object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()
Remarks
Section titled “Remarks”This class is provider-agnostic. It works the same way with the in-memory store and the PostgreSQL store; it does not know which store is registered.
The session state is Microsoft Agent Framework’s SerializeSessionAsync
output and is treated as opaque. AgentPrism does not
interpret its content. The identity is stamped onto the session’s state
bag with AgentSessionIdentity, so that RunRecordingAgent
can write the real session identity to the run record.
Microsoft Agent Framework’s pre-release AgentSessionStore abstraction
(Microsoft.Agents.AI.Hosting) is wired to this class by delegation.
Constructors
Section titled “Constructors”AgentSessionManager(ISessionStore, ITenantContext, TimeProvider?)
Section titled “ AgentSessionManager(ISessionStore, ITenantContext, TimeProvider?)”Creates a new session manager.
public AgentSessionManager(ISessionStore store, ITenantContext tenantContext, TimeProvider? timeProvider = null)Parameters
Section titled “Parameters”store ISessionStore
The session store.
tenantContext ITenantContext
The tenant context.
timeProvider TimeProvider?
The time source. The system clock is used if not given.
Exceptions
Section titled “Exceptions”One of the required dependencies is null.
Methods
Section titled “Methods”DeleteSessionAsync(string, CancellationToken)
Section titled “ DeleteSessionAsync(string, CancellationToken)”Deletes the session.
public ValueTask<bool> DeleteSessionAsync(string sessionId, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”sessionId string
The session identity.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”true if a deletion occurred.
Exceptions
Section titled “Exceptions”sessionId is empty.
GetOrCreateSessionAsync(AIAgent, string, CancellationToken)
Section titled “ GetOrCreateSessionAsync(AIAgent, string, CancellationToken)”Restores the session with the given identity; opens a new session if no record exists. In both cases, the returned session is stamped with its own identity.
public ValueTask<AgentSession> GetOrCreateSessionAsync(AIAgent agent, string sessionId, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”agent AIAgent
The agent the session belongs to.
sessionId string
The session identity.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”ValueTask<AgentSession>
A session ready for use.
Exceptions
Section titled “Exceptions”agent is null.
sessionId is empty.
The stored state cannot be restored by this agent.
QuerySessionsAsync(SessionQuery, CancellationToken)
Section titled “ QuerySessionsAsync(SessionQuery, CancellationToken)”Lists sessions matching a filter.
public ValueTask<IReadOnlyList<SessionRecord>> QuerySessionsAsync(SessionQuery query, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”query SessionQuery
The filter. The current tenant is used if none is specified.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”ValueTask<IReadOnlyList<SessionRecord>>
The sessions.
Exceptions
Section titled “Exceptions”query is null.
SaveSessionAsync(AIAgent, AgentSession, CancellationToken)
Section titled “ SaveSessionAsync(AIAgent, AgentSession, CancellationToken)”Serializes the session and writes it to the store.
public ValueTask<string> SaveSessionAsync(AIAgent agent, AgentSession session, CancellationToken cancellationToken = default)Parameters
Section titled “Parameters”agent AIAgent
The agent the session belongs to.
session AgentSession
The session to save.
cancellationToken CancellationToken
The cancellation token.
Returns
Section titled “Returns”The identity of the saved session.
Remarks
Section titled “Remarks”If two concurrent first requests arrive for the same new session identity, both start with an empty session in AgentSessionManager.GetOrCreateSessionAsync and run their own turn — each produces its own conversation identity, which is unavoidable (the conversation identity is only known once the turn runs). The actual defect was that the second ISessionStore.SaveAsync would unconditionally overwrite the first, silently leaving the loser’s conversation unreachable. For this reason, the first save of a session (see the fresh sessions marked by AgentSessionManager.GetOrCreateSessionAsync) always attempts an atomic write with ISessionStore.TryCreateAsync; if it loses, it does not silently overwrite, and instead throws an explicit AgentPrismSessionConflictException. Subsequent saves (and every save of a session that was already found existing) continue, unchanged, to use the unconditional ISessionStore.SaveAsync.
Exceptions
Section titled “Exceptions”One of the parameters is null.
The session has no identity because it was not opened with AgentSessionManager.GetOrCreateSessionAsync.
AgentPrismSessionConflictException
This was the first save of a session that AgentSessionManager.GetOrCreateSessionAsync opened FRESH, and a concurrent request saved the same new session identity before we did.