Skip to content

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

Rust

fix(copilot): report an incomplete /responses answer as a truncated response

Rudy Mizrahi Celeklie7a0a493dcac
diff --git a/crates/ai-memory-llm/src/copilot.rs b/crates/ai-memory-llm/src/copilot.rs
index 4c82afd4..c41335fc 100644
--- a/crates/ai-memory-llm/src/copilot.rs
+++ b/crates/ai-memory-llm/src/copilot.rs
@@ -452,6 +452,7 @@ impl LlmProvider for CopilotProvider {
&build_responses_request(&self.model, &request, None),
)
.await?,
+ &self.model,
)?),
}
}
@@ -497,6 +498,7 @@ impl LlmProvider for CopilotProvider {
&build_responses_request(&self.model, &request, Some(schema)),
)
.await?,
+ &self.model,
)?,
};
serde_json::from_str::<serde_json::Value>(&text).map_err(LlmError::from)
@@ -881,6 +883,8 @@ struct CopilotResponsesResponse {
#[serde(default)]
status: Option<String>,
#[serde(default)]
+ incomplete_details: Option<CopilotIncompleteDetails>,
+ #[serde(default)]
error: Option<CopilotResponsesError>,
}
@@ -910,6 +914,14 @@ struct CopilotResponsesUsage {
output_tokens: u32,
}
+/// Why a `/responses` answer stopped short. `error` is null on an incomplete
+/// response, so this is the only field that explains one.
+#[derive(Debug, Deserialize)]
+struct CopilotIncompleteDetails {
+ #[serde(default)]
+ reason: Option<String>,
+}
+
#[derive(Debug, Deserialize)]
struct CopilotResponsesError {
#[serde(default)]
@@ -918,12 +930,26 @@ struct CopilotResponsesError {
message: Option<String>,
}
-fn response_text(response: &CopilotResponsesResponse) -> LlmResult<String> {
+fn response_text(response: &CopilotResponsesResponse, model: &str) -> LlmResult<String> {
if response
.status
.as_deref()
.is_some_and(|status| status != "completed")
{
+ // `max_output_tokens` is the one non-completed status the operator can
+ // act on, and it is not an unexpected shape: raising `max_tokens` fixes
+ // it. The OpenAI path reports the same stop as `TruncatedResponse`.
+ if response
+ .incomplete_details
+ .as_ref()
+ .and_then(|details| details.reason.as_deref())
+ == Some("max_output_tokens")
+ {
+ return Err(LlmError::TruncatedResponse {
+ model: model.to_owned(),
+ completion_tokens: response.usage.as_ref().map(|usage| usage.output_tokens),
+ });
+ }
let detail = response
.error
.as_ref()
@@ -964,8 +990,11 @@ fn response_text(response: &CopilotResponsesResponse) -> LlmResult<String> {
))
}
-fn to_responses_chat_response(response: CopilotResponsesResponse) -> LlmResult<ChatResponse> {
- let text = response_text(&response)?;
+fn to_responses_chat_response(
+ response: CopilotResponsesResponse,
+ model: &str,
+) -> LlmResult<ChatResponse> {
+ let text = response_text(&response, model)?;
Ok(ChatResponse {
text,
usage: response.usage.map(|usage| Usage {
diff --git a/crates/ai-memory-llm/tests/suite/copilot_provider.rs b/crates/ai-memory-llm/tests/suite/copilot_provider.rs
index 13d478da..c7cca1c0 100644
--- a/crates/ai-memory-llm/tests/suite/copilot_provider.rs
+++ b/crates/ai-memory-llm/tests/suite/copilot_provider.rs
@@ -33,6 +33,90 @@ fn responses(content: &str) -> serde_json::Value {
json!({"status":"completed","model":"responses-model","output":[{"type":"message","content":[{"type":"output_text","text":content}]}],"usage":{"input_tokens":2,"output_tokens":3}})
}
+/// A `/responses` answer stopped by `max_output_tokens` is a truncation the
+/// operator can act on, not an unexpected shape. `error` is null on an
+/// incomplete response, so the old message always ended "no error detail" and
+/// told the operator nothing — and `unexpected-shape` is the wrong class for a
+/// stop that raising `max_tokens` fixes.
+#[tokio::test]
+async fn an_incomplete_responses_answer_is_a_truncated_response() {
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/models"))
+ .respond_with(
+ ResponseTemplate::new(200).set_body_json(metadata("responses", &["/responses"])),
+ )
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/responses"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "status": "incomplete",
+ "incomplete_details": { "reason": "max_output_tokens" },
+ "error": null,
+ "model": "responses-model",
+ "output": [{ "type": "reasoning", "content": [] }],
+ "usage": { "input_tokens": 2, "output_tokens": 4096 },
+ })))
+ .mount(&server)
+ .await;
+
+ let err = provider(&server, "responses")
+ .complete(ai_memory_llm::ChatRequest::user_prompt("hi"))
+ .await
+ .expect_err("a truncated answer must not be returned as text");
+
+ assert_eq!(err.class(), "truncated-response", "{err:?}");
+ assert!(
+ !err.is_transient(),
+ "raising max_tokens is the fix, not a retry"
+ );
+ match &err {
+ LlmError::TruncatedResponse {
+ model,
+ completion_tokens,
+ } => {
+ assert_eq!(model, "responses");
+ assert_eq!(*completion_tokens, Some(4096));
+ }
+ other => panic!("expected TruncatedResponse, got {other:?}"),
+ }
+}
+
+/// The control: any OTHER non-completed status keeps the existing
+/// unexpected-shape classification, so this change is scoped to the one stop
+/// reason the operator can act on.
+#[tokio::test]
+async fn another_incomplete_reason_stays_an_unexpected_shape() {
+ let server = MockServer::start().await;
+ Mock::given(method("GET"))
+ .and(path("/models"))
+ .respond_with(
+ ResponseTemplate::new(200).set_body_json(metadata("responses", &["/responses"])),
+ )
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/responses"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "status": "incomplete",
+ "incomplete_details": { "reason": "content_filter" },
+ "error": null,
+ "model": "responses-model",
+ "output": [],
+ "usage": { "input_tokens": 2, "output_tokens": 0 },
+ })))
+ .mount(&server)
+ .await;
+
+ let err = provider(&server, "responses")
+ .complete(ai_memory_llm::ChatRequest::user_prompt("hi"))
+ .await
+ .expect_err("a non-completed status must still fail");
+
+ assert_eq!(err.class(), "unexpected-shape", "{err:?}");
+}
+
#[tokio::test]
async fn metadata_selects_chat_and_preserves_chat_wire_format() {
let server = MockServer::start().await;