Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(copilot): fall back to chat/completions when a model is absent from /models

AkitaOnRailsbb557bd66c10
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8e08fcff..71448d31 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
endpoint from `/models`: existing Chat Completions remains preferred when
available, while Responses-only models use `/responses`. Responses requests
preserve strict JSON Schema structured-output constraints and report empty,
- refused, or rejected output without silently downgrading the contract. (#761)
+ refused, or rejected output without silently downgrading the contract. A model
+ the `/models` catalogue does not enumerate (enterprise/custom deployments,
+ aliases, a model newer than the list) falls back to Chat Completions with a
+ warning instead of erroring, matching the graceful fallback already used when
+ `/models` is unavailable. As a related behavior change, a Copilot chat
+ completion that returns empty content now reports `UnexpectedShape` rather
+ than yielding an empty string. (#761)
- The generated OpenCode and OpenCode 2 plugins now forward a subagent session's
`parentID` as the `agent_id` marker, so `[capture] drop_subagent_captures` can
recognize and drop OpenCode subagent sessions. Previously both plugins emitted
diff --git a/crates/ai-memory-llm/src/copilot.rs b/crates/ai-memory-llm/src/copilot.rs
index b5e939d2..fee48854 100644
--- a/crates/ai-memory-llm/src/copilot.rs
+++ b/crates/ai-memory-llm/src/copilot.rs
@@ -12,7 +12,7 @@ use async_trait::async_trait;
use secrecy::{ExposeSecret as _, SecretString};
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
-use tracing::{debug, info};
+use tracing::{debug, info, warn};
use crate::auth::CopilotAuth;
use crate::auth_file::{load_entry, now_ms, save_entry};
@@ -339,16 +339,24 @@ impl CopilotProvider {
return Ok(CopilotEndpoint::ChatCompletions);
}
let metadata = response_json_limited::<CopilotModelsResponse>(response).await?;
- let model = metadata
+ let Some(model) = metadata
.data
.into_iter()
.find(|entry| entry.id == self.model)
- .ok_or_else(|| {
- LlmError::UnexpectedShape(format!(
- "copilot model metadata did not contain configured model {:?}",
- self.model
- ))
- })?;
+ else {
+ // A configured model that the catalogue does not enumerate
+ // (enterprise/custom deployments, aliases, casing, a model newer
+ // than this list) reached Chat Completions directly before this
+ // metadata check existed. Keep that backward-compatible path rather
+ // than hard-erroring; only models the catalogue lists as
+ // Responses-only route through `/responses`.
+ warn!(
+ model = %self.model,
+ "copilot model not found in /models catalogue; using chat completions"
+ );
+ *cached = Some(CopilotEndpoint::ChatCompletions);
+ return Ok(CopilotEndpoint::ChatCompletions);
+ };
let endpoint = CopilotEndpoint::from_supported(&model.supported_endpoints).ok_or_else(|| {
LlmError::UnexpectedShape(format!(
"copilot model {:?} has no supported completion endpoint; advertised endpoints: {:?}",
diff --git a/crates/ai-memory-llm/tests/suite/copilot_provider.rs b/crates/ai-memory-llm/tests/suite/copilot_provider.rs
index b50ffbc2..13d478da 100644
--- a/crates/ai-memory-llm/tests/suite/copilot_provider.rs
+++ b/crates/ai-memory-llm/tests/suite/copilot_provider.rs
@@ -130,6 +130,43 @@ async fn metadata_with_both_endpoints_deterministically_prefers_chat() {
);
}
+#[tokio::test]
+async fn model_absent_from_catalogue_falls_back_to_chat_completions() {
+ // A 200 `/models` response that does not enumerate the configured model
+ // (enterprise/custom deployments, aliases, a model newer than the list)
+ // must keep the pre-metadata-check Chat Completions path, not hard-error.
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/models"))
+ .respond_with(
+ ResponseTemplate::new(200)
+ .set_body_json(metadata("some-other-model", &["/chat/completions"])),
+ )
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/chat/completions"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(chat("fallback")))
+ .expect(1)
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/responses"))
+ .respond_with(ResponseTemplate::new(500))
+ .expect(0)
+ .mount(&server)
+ .await;
+
+ assert_eq!(
+ provider(&server, "enterprise-custom-deployment")
+ .complete(ai_memory_llm::ChatRequest::user_prompt("hi"))
+ .await
+ .unwrap()
+ .text,
+ "fallback"
+ );
+}
+
#[tokio::test]
async fn metadata_without_a_compatible_endpoint_fails_with_model_and_capabilities() {
let server = MockServer::start().await;