@@ -8,28 +8,28 @@ import zio.raft.protocol.RequestId.RequestIdSyntax
88import zio .stream .Stream
99import java .time .Instant
1010
11- /** Abstract base class for session-aware state machines using the template pattern.
11+ /** Trait for session-aware state machines using the template pattern.
1212 *
13- * This class implements Chapter 6.3 of the Raft dissertation (Implementing linearizable semantics) by providing
13+ * This trait implements Chapter 6.3 of the Raft dissertation (Implementing linearizable semantics) by providing
1414 * automatic session management, idempotency checking, and response caching.
1515 *
1616 * ## Template Pattern
1717 *
18- * Users extend this class and implement 3 protected abstract methods:
18+ * Users extend this trait and implement 3 protected abstract methods:
1919 * - `applyCommand`: Business logic for processing user commands (returns StateWriter)
2020 * - `handleSessionCreated`: Session initialization logic (returns StateWriter)
2121 * - `handleSessionExpired`: Session cleanup logic (returns StateWriter)
2222 *
2323 * The StateWriter monad combines State for state transitions with Writer for accumulating server-initiated requests.
2424 * Users call `.log(serverRequest)` to emit server requests instead of manually collecting them in tuples.
2525 *
26- * The base class provides the final `apply` template method that orchestrates:
26+ * This trait provides the final `apply` template method that orchestrates:
2727 * - Idempotency checking via (sessionId, requestId) pairs
2828 * - Response caching for duplicate requests
2929 * - Server-initiated request management with cumulative acknowledgment
3030 * - Session lifecycle coordination
31- * - State narrowing (users see only HMap[UserSchema] )
32- * - State merging (user changes merged back to combined state)
31+ * - Cache cleanup driven by client-provided lowestRequestId (inclusive: removes requestId <= lowestRequestId )
32+ * - Evicted response detection: if requestId <= highestLowestRequestIdSeen and not in cache, return RequestError.ResponseEvicted
3333 *
3434 * ## Type Parameters
3535 *
@@ -86,7 +86,7 @@ trait SessionStateMachine[UC <: Command, R, SR, UserSchema <: Tuple]
8686 /** Apply a user command to the state.
8787 *
8888 * This method receives ONLY the user schema state (UserSchema), not session management state. Session management
89- * prefixes are handled by the base class automatically.
89+ * prefixes are handled by this trait automatically.
9090 *
9191 * Use `.log(serverRequest)` to emit server-initiated requests. Server requests MUST be wrapped in
9292 * ServerRequestForSession to specify target sessionId. This allows sending requests to ANY session, not just the
@@ -175,7 +175,7 @@ trait SessionStateMachine[UC <: Command, R, SR, UserSchema <: Tuple]
175175 * @note
176176 * Receives complete schema state
177177 * @note
178- * Session metadata and cache are automatically removed by base class
178+ * Session metadata and cache are automatically removed by this trait
179179 * @note
180180 * Must be pure and deterministic
181181 * @note
@@ -188,7 +188,7 @@ trait SessionStateMachine[UC <: Command, R, SR, UserSchema <: Tuple]
188188 ): StateWriter [HMap [Schema ], ServerRequestForSession [SR ], Unit ]
189189
190190 // ====================================================================================
191- // StateMachine INTERFACE - Implemented by base class
191+ // StateMachine INTERFACE - Implemented by this trait
192192 // ====================================================================================
193193
194194 /** Empty state with no sessions or data.
@@ -429,7 +429,7 @@ trait SessionStateMachine[UC <: Command, R, SR, UserSchema <: Tuple]
429429 /** Add server requests to state and assign monotonically increasing IDs.
430430 *
431431 * Each request is stored with a composite key (SessionId, RequestId) for efficiency. sessionId comes from
432- * ServerRequestForSession, not as a parameter!
432+ * ServerRequestForSession.
433433 *
434434 * @param createdAt
435435 * Timestamp when the command was created (for lastSentAt)
0 commit comments