Coverage for /home/admin/Documents/AI/applications/lexigram-dev/lexigram/experimental/ai/lexigram-ai-session/src/lexigram/ai/session/config.py: 84%

32 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 07:19 +0800

1"""Configuration for the lexigram-ai-session package.""" 

2 

3from __future__ import annotations 

4 

5from typing import ClassVar, cast 

6 

7from lexigram.ai.session import constants as const 

8from lexigram.config import ( 

9 BaseConfig, 

10) 

11from lexigram.contracts.core.config import ConfigIssue, Environment 

12from lexigram.validation import ConfigDict, Field 

13 

14 

15class SessionConfig(BaseConfig): 

16 """Configuration for the session management subsystem. 

17 

18 Controls lifecycle timeouts, checkpointing thresholds, multi-agent 

19 settings, backend selection, and web middleware behaviour. 

20 """ 

21 

22 config_section: ClassVar[str] = "ai_session" 

23 

24 model_config: ClassVar[ConfigDict] = cast( 

25 "ConfigDict", 

26 { 

27 "env_prefix": const.ENV_PREFIX, 

28 "env_nested_delimiter": const.ENV_NESTED_DELIMITER, 

29 "extra": "ignore", 

30 }, 

31 ) 

32 

33 enabled: bool = Field(default=True, description="Enable the AI session subsystem") 

34 

35 name: str = Field( 

36 default="ai-session", description="Logical name used for DI registration keys" 

37 ) 

38 

39 # Lifecycle 

40 default_system_prompt: str | None = Field( 

41 default=None, 

42 description="System prompt injected into every new session", 

43 ) 

44 session_ttl: int = Field( 

45 default=const.DEFAULT_SESSION_TTL_S, 

46 ge=0, 

47 description="Maximum age of a session in seconds (0 to disable)", 

48 ) 

49 cleanup_interval_s: int = Field( 

50 default=const.DEFAULT_CLEANUP_INTERVAL_S, 

51 ge=1, 

52 description="How often the cleanup scheduler sweeps for expired sessions", 

53 ) 

54 max_turns_per_session: int = Field( 

55 default=const.DEFAULT_MAX_TURNS, 

56 ge=1, 

57 description="Hard cap on turns before the session is closed", 

58 ) 

59 max_sessions_per_user: int = Field( 

60 default=const.DEFAULT_MAX_SESSIONS_PER_USER, 

61 ge=1, 

62 description="Maximum concurrent sessions per user", 

63 ) 

64 

65 # Checkpointing 

66 auto_checkpoint_interval: int | None = Field( 

67 default=const.DEFAULT_AUTO_CHECKPOINT_INTERVAL, 

68 description="Checkpoint every N turns; None to disable", 

69 ) 

70 max_checkpoints_per_session: int = Field( 

71 default=const.DEFAULT_MAX_CHECKPOINTS, 

72 ge=1, 

73 description="Maximum retained checkpoints per session", 

74 ) 

75 

76 # Branching 

77 max_branches_per_session: int = Field( 

78 default=const.DEFAULT_MAX_BRANCHES, 

79 ge=1, 

80 description="Maximum forked branches per session", 

81 ) 

82 

83 # Multi-agent 

84 max_agents_per_group: int = Field( 

85 default=const.DEFAULT_MAX_AGENTS, 

86 ge=1, 

87 description="Maximum agents in a multi-agent group session", 

88 ) 

89 default_turn_strategy: str = Field( 

90 default=const.DEFAULT_TURN_STRATEGY, 

91 description="Default turn-selection strategy (round_robin, priority, llm_directed)", 

92 ) 

93 

94 # Persistence 

95 backend: str = Field( 

96 default=const.DEFAULT_BACKEND, 

97 description="Persistence backend (in_memory, cache, database)", 

98 ) 

99 

100 # Web middleware 

101 cookie_name: str | None = Field( 

102 default=const.DEFAULT_COOKIE_NAME, 

103 description="Cookie name for web session ID; None disables cookies", 

104 ) 

105 header_name: str = Field( 

106 default=const.DEFAULT_HEADER_NAME, 

107 description="HTTP header name for session ID pass-through", 

108 ) 

109 

110 # Integration 

111 consolidate_on_close: bool = Field( 

112 default=const.DEFAULT_CONSOLIDATE_ON_CLOSE, 

113 description="Whether to trigger memory consolidation on session close", 

114 ) 

115 

116 def validate_for_environment( 

117 self, env: Environment | None = None 

118 ) -> list[ConfigIssue]: 

119 """Check config is safe for the target environment.""" 

120 issues: list[ConfigIssue] = [] 

121 if env == Environment.PRODUCTION: 

122 if self.backend == "in_memory": 

123 issues.append( 

124 ConfigIssue( 

125 severity="error", 

126 field="backend", 

127 message="In-memory session backend used in production", 

128 suggestion="Use 'cache' or 'database' for persistent sessions", 

129 ) 

130 ) 

131 return issues 

132 

133 

134__all__ = ["SessionConfig"]