Skip to content

AgentSessionManager

Namespace AgentPrism · Assembly AgentPrism.Core.dll

Manages the lifecycle of named sessions: loads, restores, and saves them through ISessionStore.

public sealed class AgentSessionManager

objectAgentSessionManager

object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

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.

AgentSessionManager(ISessionStore, ITenantContext, TimeProvider?)

Section titled “ AgentSessionManager(ISessionStore, ITenantContext, TimeProvider?)”

Creates a new session manager.

public AgentSessionManager(ISessionStore store, ITenantContext tenantContext, TimeProvider? timeProvider = null)

store ISessionStore

The session store.

tenantContext ITenantContext

The tenant context.

timeProvider TimeProvider?

The time source. The system clock is used if not given.

ArgumentNullException

One of the required dependencies is null.

DeleteSessionAsync(string, CancellationToken)

Section titled “ DeleteSessionAsync(string, CancellationToken)”

Deletes the session.

public ValueTask<bool> DeleteSessionAsync(string sessionId, CancellationToken cancellationToken = default)

sessionId string

The session identity.

cancellationToken CancellationToken

The cancellation token.

ValueTask<bool>

true if a deletion occurred.

ArgumentException

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)

agent AIAgent

The agent the session belongs to.

sessionId string

The session identity.

cancellationToken CancellationToken

The cancellation token.

ValueTask<AgentSession>

A session ready for use.

ArgumentNullException

agent is null.

ArgumentException

sessionId is empty.

AgentPrismException

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)

query SessionQuery

The filter. The current tenant is used if none is specified.

cancellationToken CancellationToken

The cancellation token.

ValueTask<IReadOnlyList<SessionRecord>>

The sessions.

ArgumentNullException

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)

agent AIAgent

The agent the session belongs to.

session AgentSession

The session to save.

cancellationToken CancellationToken

The cancellation token.

ValueTask<string>

The identity of the saved session.

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.

ArgumentNullException

One of the parameters is null.

AgentPrismException

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.