Class SessionResolver
Session from an AgentInit at the start of an agent invocation.
Enforces the server-vs-client state-management rules:
- API misuse (wrong init for the state-management mode, ownership mismatch) throws
GenkitExceptionwithFAILED_PRECONDITIONorINVALID_ARGUMENT. - Other pre-turn failures are returned as a
SessionResolver.Resolution.failure(com.google.genkit.ai.agent.RuntimeError)so the agent can emit anAgentOutputwithfinishReason=FAILEDrather than surfacing as an unhandled exception.
Pending-leaf decision
When resolving by sessionId and the latest leaf snapshot has a non-COMPLETED status (e.g.
PENDING, FAILED, ABORTED), this implementation throws GenkitException with
FAILED_PRECONDITION. This matches Go's resumeSessionFrom behavior and is simpler than
the JS approach of walking back to the last completed snapshot. The error is conformance-tested;
callers should clear or complete any in-progress snapshot before starting a new turn.
Unknown-snapshot decision
When a specific snapshotId is provided but not found in the store, this implementation
throws GenkitException with INVALID_ARGUMENT. An unknown snapshotId is treated as
API misuse (the client provided a bad reference), not as a recoverable pre-turn error. This
matches the design spec ยง6.4 note: "unknown snapshot โ throw/404".
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classResult of session resolution: either a readySession, or a graceful pre-turn failure. -
Method Summary
Modifier and TypeMethodDescriptionstatic <S> SessionResolver.Resolution<S> resolve(SessionStore<S> store, boolean serverManaged, AgentInit<S> init, SessionStoreOptions opts)
-
Method Details
-
resolve
public static <S> SessionResolver.Resolution<S> resolve(SessionStore<S> store, boolean serverManaged, AgentInit<S> init, SessionStoreOptions opts) Resolves aSessionfrom the givenAgentInit.Rules (see class-level Javadoc for details):
- State-management mismatch โ throws
GenkitException. - Client-managed: hydrate from
init.stateor mint a fresh session. - Server-managed: resolve via snapshotId, sessionId, or mint fresh.
- Type Parameters:
S- the type of custom session state- Parameters:
store- the agent's session store, ornullfor client-managed agentsserverManaged-trueiff the agent has a store (server-managed)init- the agent init (may benullโ treated as all fields null)opts- options forwarded to store operations (may benull)- Returns:
- a
SessionResolver.Resolutionwrapping either the resolved session or a graceful failure - Throws:
GenkitException- withFAILED_PRECONDITIONorINVALID_ARGUMENTfor API misuse (wrong init for the state-management mode, or ownership mismatch)
- State-management mismatch โ throws
-