fix: reinitialize CopilotSession on model reload#135
Conversation
When switching models via /models or /reload, the gateway now properly reinitializes the CopilotSession if the new model requires it. Previously, the session was only created at startup, causing 'missing authentication header' errors when changing models at runtime. - Add SharedCopilotSession type for mutable shared state - Extract init_copilot_session() helper function - Update Reload handler to reinitialize session - Read session from shared state before API calls Fixes #134
| if let Some(ref ctx) = new_model_ctx { | ||
| let new_session = init_copilot_session( | ||
| &ctx.provider, | ||
| ctx.api_key.as_deref(), | ||
| &vault, | ||
| ).await; | ||
| let mut session = shared_copilot_session.write().await; | ||
| *session = new_session; | ||
| } |
There was a problem hiding this comment.
🟡 Reload does not clear stale Copilot session when model context becomes None
When the user reloads configuration and the new config results in new_model_ctx being None (e.g., model removed or ModelContext::resolve fails), the shared_copilot_session is never updated (line 1232 only enters the block when new_model_ctx is Some). The stale copilot session from the previous provider remains in shared state. While subsequent chat requests will likely fail for other reasons (no model context), the model context is cleared at line 1247-1248, yet the copilot session persists, creating an inconsistency between the two shared states. If a client later sends a ChatRequest with inline provider info, dispatch_text_message would receive a stale copilot_session from the old provider.
| if let Some(ref ctx) = new_model_ctx { | |
| let new_session = init_copilot_session( | |
| &ctx.provider, | |
| ctx.api_key.as_deref(), | |
| &vault, | |
| ).await; | |
| let mut session = shared_copilot_session.write().await; | |
| *session = new_session; | |
| } | |
| // Reinitialize Copilot session if the new model needs it | |
| { | |
| let new_session = if let Some(ref ctx) = new_model_ctx { | |
| init_copilot_session( | |
| &ctx.provider, | |
| ctx.api_key.as_deref(), | |
| &vault, | |
| ).await | |
| } else { | |
| None | |
| }; | |
| let mut session = shared_copilot_session.write().await; | |
| *session = new_session; | |
| } |
Was this helpful? React with 👍 or 👎 to provide feedback.
PR #131 added is_direct to the Message struct but only updated matrix_cli.rs. This fixes the other messengers so the 'full' feature set compiles.
Summary
When switching models via
/modelsor/reload, the gateway now properly reinitializes theCopilotSessionif the new model requires it. Previously, the session was only created at startup, causing "missing authentication header" errors when changing models at runtime.Changes
SharedCopilotSessiontype for mutable shared stateinit_copilot_session()helper functionTesting
Fixes #134