session
code_context_agent.tools.lsp.session ¶
LSP session manager using singleton pattern.
This module provides a session manager that maintains LSP client connections across tool calls. Sessions are expensive to start (subprocess spawn + initialization), so we reuse them.
LspSessionManager ¶
Singleton session manager for LSP connections.
Maintains a pool of LSP client connections keyed by server kind and workspace path. This allows multiple tools to share the same LSP connection efficiently.
Session keys are formatted as "{server_kind}:{workspace_path}".
Example
manager = LspSessionManager() client = await manager.get_or_create("ts", "/path/to/project") symbols = await client.document_symbols("/path/to/file.ts") await manager.shutdown_all()
reset_instance classmethod ¶
Reset the singleton instance (for testing).
get_server_command_for_session ¶
Get the server command string that was used for a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session identifier (format: "kind:workspace"). | required |
Returns:
| Type | Description |
|---|---|
str | None | The command string that successfully started the server, or None. |
Source code in src/code_context_agent/tools/lsp/session.py
get_or_create async ¶
Get existing session or create new one with fallback chain.
Tries each configured LSP server command in order. If one fails, falls back to the next. Stores which command succeeded for later inspection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_kind | str | Server type ("ts", "py", etc.). | required |
workspace_path | str | Absolute path to workspace root. | required |
startup_timeout | float | None | Maximum seconds to wait for server initialization. If None, uses the value from settings. | None |
Returns:
| Type | Description |
|---|---|
LspClient | Connected LspClient instance. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If all server commands fail to start. |
ValueError | If server kind is not supported. |
Source code in src/code_context_agent/tools/lsp/session.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
get_session ¶
Get an existing session by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session identifier (format: "kind:workspace"). | required |
Returns:
| Type | Description |
|---|---|
LspClient | None | LspClient if session exists and is connected, None otherwise. |
Source code in src/code_context_agent/tools/lsp/session.py
list_sessions ¶
List all active session IDs.
Returns:
| Type | Description |
|---|---|
list[str] | List of session key strings. |
shutdown_session async ¶
Shutdown a specific session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Session identifier to shutdown. | required |
Returns:
| Type | Description |
|---|---|
bool | True if session was found and shutdown, False otherwise. |
Source code in src/code_context_agent/tools/lsp/session.py
shutdown_all async ¶
Shutdown all active LSP sessions.
Source code in src/code_context_agent/tools/lsp/session.py
get_session_manager ¶
Get the LSP session manager singleton.
Returns:
| Type | Description |
|---|---|
LspSessionManager | The global LspSessionManager instance. |