Skip to content

fix: reinitialize CopilotSession on model reload#135

Merged
rexlunae merged 2 commits intomainfrom
fix/models-copilot-auth
Mar 23, 2026
Merged

fix: reinitialize CopilotSession on model reload#135
rexlunae merged 2 commits intomainfrom
fix/models-copilot-auth

Conversation

@rexlunae
Copy link
Copy Markdown
Owner

@rexlunae rexlunae commented Mar 22, 2026

Summary

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.

Changes

  • 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

Testing

  • All 454 tests pass
  • Build compiles without errors

Fixes #134


Open with Devin

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
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View 3 additional findings in Devin Review.

Open in Devin Review

Comment on lines +1232 to +1240
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;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Suggested change
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;
}
Open in Devin Review

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.
@rexlunae rexlunae merged commit a83b8cc into main Mar 23, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/models command: model selection causes 'missing authentication header'

1 participant