Overview
SessionRuntime is a thin application service for persisted agent sessions.
Its contract is intentionally small:
- load session
- apply action (
executeOn(AgentSession): AgentSession) - save session
Core Types
AgentSession- immutable aggregate: session info +AgentDefinition+AgentStateSessionId- typed identifierSessionRepository- repository over a session storeCanStoreSessions- persistence contractCanExecuteSessionAction- action contract (executeOn(AgentSession): AgentSession)CanRunSessionRuntime- runtime contract
Runtime Contract
getSession, getSessionInfo, listSessions) do not mutate persisted version.
Store Contract and Versioning
Stores use optimistic version checks.create(AgentSession): AgentSessionstores version1save(AgentSession): AgentSessionrequires incoming version to match stored version- successful
save()returns a reconstituted session with incremented version
SessionNotFoundExceptionSessionConflictExceptionInvalidSessionFileException(file store)
Minimal Setup
Typical Flow
Built-in Actions
Lifecycle:ResumeSessionSuspendSessionClearSession
ChangeModelChangeBudgetChangeSystemPromptWriteMetadataUpdateTask
SendMessageForkSession
AgentSession.
Session Lifecycle vs Execution Lifecycle
Session lifecycle is cross-run (active, suspended, completed, failed, deleted).
Execution lifecycle is per-run and resettable (AgentState::forNextExecution()).
AgentSession::withState() does not derive session status from execution status. Session lifecycle transitions are explicit session-level decisions.
Events
SessionRuntime emits events for observability:
SessionLoadedSessionActionExecutedSessionSavedSessionLoadFailedSessionSaveFailed
wiretap() on the injected event handler to record session runtime behavior.
Concurrency and Ordering
SessionRuntime provides deterministic conflict signaling, not conflict resolution.
- If two workers update the same session concurrently, one save may throw
SessionConflictException. - Runtime does not retry automatically.
- Caller/adapter owns retry policy and ordering strategy (for example, per-session queueing in external infrastructure).