Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(llm): include compat schema in tolerant prompts
willyamc3edf34cee0a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 12202b0f..f7775a2d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,10 @@ version in [`docs/changelog/`](docs/changelog/):### Fixed+- Appended the target JSON Schema to the system prompt during `openai-compat`+ tolerant structured completion fallback and direct tolerant mode, preventing+ models from omitting top-level object wrappers when `response_format` is+ unsupported or rejected (#1909).- Project search links now use the project's current stored name afterresolving a canonical or legacy key. Results, breadcrumbs, and the scopeselector stay on that project without renaming it (#1875).diff --git a/crates/ai-memory-llm/src/openai_compat.rs b/crates/ai-memory-llm/src/openai_compat.rsindex 19433b81..564bd20a 100644--- a/crates/ai-memory-llm/src/openai_compat.rs+++ b/crates/ai-memory-llm/src/openai_compat.rs@@ -343,8 +343,9 @@ impl OpenAiCompatProvider {}}// Default (and strict fallback): most older local engines don't- // honour `response_format`. Ask for JSON and extract the first- // balanced `{…}` object from the text.+ // honour `response_format`. Put the schema in the prompt before+ // extracting the first balanced `{…}` object from the response.+ let request = with_tolerant_schema_prompt(request, &schema)?;let (res, finish_reason) = self.inner.complete_with_operation_id_and_finish_reason(request, operation_id)@@ -397,6 +398,20 @@ impl OpenAiCompatProvider {}}+fn with_tolerant_schema_prompt(+ mut request: ChatRequest,+ schema: &serde_json::Value,+) -> LlmResult<ChatRequest> {+ let schema = serde_json::to_string(schema)?;+ let instruction =+ format!("Return ONE JSON object matching this JSON Schema exactly:\n\n{schema}");+ request.system = Some(match request.system.take() {+ Some(system) if !system.trim().is_empty() => format!("{system}\n\n{instruction}"),+ _ => instruction,+ });+ Ok(request)+}+/// True for errors where a *retry without structured-output coercion*/// has a real chance of succeeding — i.e. the upstream returned a/// response, but it wasn't in the shape we wanted. Transport / HTTP /@@ -492,6 +507,16 @@ mod tests {.map(str::to_owned)}+ fn request_body(request: &wiremock::Request) -> serde_json::Value {+ serde_json::from_slice(&request.body).expect("request body is JSON")+ }++ fn system_prompt(body: &serde_json::Value) -> &str {+ body["messages"][0]["content"]+ .as_str()+ .expect("system message content")+ }+/// Acceptance (a): the header is present on the wire in the accepted/// format — a 36-character `[A-Za-z0-9._:-]` value (UUID v7 display/// string) well inside the 64-character header contract — and it carries@@ -568,14 +593,15 @@ mod tests {.with_strict(true).with_request_id_header();let operation_id = LlmOperationId::new();+ let schema = json!({+ "type": "object",+ "properties": { "ok": { "type": "boolean" } },+ "required": ["ok"],+ });let value = provider.complete_structured_raw_with_operation_id(ChatRequest::user_prompt("emit JSON"),- json!({- "type": "object",- "properties": { "ok": { "type": "boolean" } },- "required": ["ok"],- }),+ schema.clone(),operation_id,).await@@ -584,6 +610,14 @@ mod tests {assert_eq!(value, json!({ "ok": true }));let requests = server.received_requests().await.expect("requests");assert_eq!(requests.len(), 2, "strict attempt + tolerant fallback");+ let strict_body = request_body(&requests[0]);+ let tolerant_body = request_body(&requests[1]);+ assert!(strict_body.get("response_format").is_some());+ assert!(tolerant_body.get("response_format").is_none());+ assert!(+ system_prompt(&tolerant_body)+ .contains(&serde_json::to_string(&schema).expect("schema serializes"))+ );let expected = operation_id.to_string();for request in &requests {assert_eq!(@@ -602,6 +636,52 @@ mod tests {}}+ #[tokio::test]+ async fn tolerant_structured_request_includes_schema_in_system_prompt() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path("/v1/chat/completions"))+ .respond_with(+ ResponseTemplate::new(200)+ .set_body_json(chat_response_with_content(r#"{"proposals":[]}"#)),+ )+ .mount(&server)+ .await;++ let provider =+ OpenAiCompatProvider::new(server.uri(), None, "model-x").expect("provider builds");+ let schema = json!({+ "type": "object",+ "properties": {+ "proposals": {+ "type": "array",+ "items": {+ "type": "object",+ "properties": {+ "operation": { "type": "string" },+ "kind": { "type": "string" },+ "evidence": { "type": "array" },+ "confidence": { "type": "number" },+ },+ },+ },+ },+ });+ let mut request = ChatRequest::user_prompt("review these observations");+ request.system = Some("Keep the existing review rules.".into());+ provider+ .complete_structured_raw(request, schema.clone())+ .await+ .expect("tolerant structured completion succeeds");++ let requests = server.received_requests().await.expect("requests");+ let body = request_body(&requests[0]);+ assert!(body.get("response_format").is_none());+ let system = system_prompt(&body);+ assert!(system.starts_with("Keep the existing review rules."));+ assert!(system.contains(&serde_json::to_string(&schema).expect("schema serializes")));+ }+/// Acceptance (e): `opencode` routes through this wrapper internally but/// keeps its own `x-opencode-session` contract — it must NOT gain the/// `x-request-id` header.diff --git a/crates/ai-memory-llm/src/opencode.rs b/crates/ai-memory-llm/src/opencode.rsindex 71c6e3a5..c2be4d14 100644--- a/crates/ai-memory-llm/src/opencode.rs+++ b/crates/ai-memory-llm/src/opencode.rs@@ -845,6 +845,10 @@ mod tests {);assert!(header_value(request, "x-api-key").is_none());assert_eq!(body["messages"][0]["content"], "hello");+ if index == 1 {+ assert_eq!(body["response_format"]["type"], "json_schema");+ assert_eq!(body["response_format"]["json_schema"]["strict"], true);+ }assert_eq!(body["reasoning_effort"], "high");assert!(body.get("input").is_none());assert!(body.get("tools").is_none());@@ -1358,14 +1362,16 @@ mod tests {.unwrap().with_strict(true);let operation_id = LlmOperationId::new();+ let schema = json!({+ "type": "object",+ "properties": { "ok": { "type": "boolean" } },+ "required": ["ok"],+ });+ let schema_json = serde_json::to_string(&schema).unwrap();let value = provider.complete_structured_raw_with_operation_id(ChatRequest::user_prompt("emit JSON"),- json!({- "type": "object",- "properties": { "ok": { "type": "boolean" } },- "required": ["ok"],- }),+ schema,operation_id,).await@@ -1374,6 +1380,15 @@ mod tests {assert_eq!(value, json!({"ok": true}));let requests = server.received_requests().await.unwrap();assert_eq!(requests.len(), 2);+ let fallback_body: serde_json::Value =+ serde_json::from_slice(&requests[1].body).expect("request body is JSON");+ assert_eq!(fallback_body["messages"][0]["role"], "system");+ assert!(+ fallback_body["messages"][0]["content"]+ .as_str()+ .unwrap()+ .contains(&schema_json)+ );let expected_id = operation_id.to_string();assert_eq!(header_value(&requests[0], "x-opencode-session"),