feat: Add Session Lifecycle Events and Retry Policy (Phase 3 + 4)

Implements Phase 3 (Session Lifecycle Events - REQ-4) and Phase 4 (Retry Policy - REQ-7)
for v2.19.0 session persistence feature.

Phase 3 - Session Lifecycle Events (REQ-4):
- Added 5 lifecycle event callbacks: onSessionCreated, onSessionRestored,
  onSessionAccessed, onSessionExpired, onSessionDeleted
- Fire-and-forget pattern: non-blocking, errors don't affect operations
- Supports both sync and async handlers
- Events emitted at 5 key lifecycle points

Phase 4 - Retry Policy (REQ-7):
- Configurable retry logic with sessionRestorationRetries and sessionRestorationRetryDelay
- Overall timeout applies to ALL retry attempts combined
- Timeout errors are never retried (already took too long)
- Smart error handling with comprehensive logging

Features:
- Backward compatible: all new options are optional with sensible defaults
- Type-safe interfaces with comprehensive JSDoc documentation
- Security: session ID validation before restoration attempts
- Performance: non-blocking events, efficient retry logic
- Observability: structured logging at all critical points

Files modified:
- src/types/session-restoration.ts: Added SessionLifecycleEvents interface and retry options
- src/http-server-single-session.ts: Added emitEvent() and restoreSessionWithRetry() methods
- src/mcp-engine.ts: Added sessionEvents and retry options to EngineOptions
- CHANGELOG.md: Comprehensive v2.19.0 release documentation

Tests:
- 34 unit tests passing (14 lifecycle events + 20 retry policy)
- Integration tests created for combined behavior
- Code reviewed and approved (9.3/10 rating)
- MCP server tested and verified working

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-10-12 18:31:39 +02:00
parent b6bc3b732e
commit 085f6db7a2
7 changed files with 1946 additions and 6 deletions

View File

@@ -46,6 +46,51 @@ export interface EngineOptions {
* @since 2.19.0
*/
sessionRestorationTimeout?: number;
/**
* Session lifecycle event handlers (Phase 3 - REQ-4)
*
* Optional callbacks for session lifecycle events:
* - onSessionCreated: Called when a new session is created
* - onSessionRestored: Called when a session is restored from storage
* - onSessionAccessed: Called on EVERY request (consider throttling!)
* - onSessionExpired: Called when a session expires
* - onSessionDeleted: Called when a session is manually deleted
*
* All handlers are fire-and-forget (non-blocking).
* Errors are logged but don't affect session operations.
*
* @since 2.19.0
*/
sessionEvents?: {
onSessionCreated?: (sessionId: string, instanceContext: InstanceContext) => void | Promise<void>;
onSessionRestored?: (sessionId: string, instanceContext: InstanceContext) => void | Promise<void>;
onSessionAccessed?: (sessionId: string) => void | Promise<void>;
onSessionExpired?: (sessionId: string) => void | Promise<void>;
onSessionDeleted?: (sessionId: string) => void | Promise<void>;
};
/**
* Number of retry attempts for failed session restoration (Phase 4 - REQ-7)
*
* When the restoration hook throws an error, the system will retry
* up to this many times with a delay between attempts.
*
* Timeout errors are NOT retried (already took too long).
* The overall timeout applies to ALL retry attempts combined.
*
* @default 0 (no retries, opt-in)
* @since 2.19.0
*/
sessionRestorationRetries?: number;
/**
* Delay between retry attempts in milliseconds (Phase 4 - REQ-7)
*
* @default 100 (100 milliseconds)
* @since 2.19.0
*/
sessionRestorationRetryDelay?: number;
}
export class N8NMCPEngine {