Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(llm): find the Cursor agent's JSON answer inside bracketed prose
Rudy Celeklie7ce012a7186
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fee7fa0b..6a4a9bae 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,10 @@ version in [`docs/changelog/`](docs/changelog/):## [Unreleased]### Fixed+- Parsed a Cursor agent answer whose surrounding prose contains brackets, such+ as `Here is the result for [the request]:` before the JSON or `(see [schema])`+ after it. The first-to-last bracket slice was invalid there, so the provider+ reported "cursor agent did not return JSON" despite a well-formed object.- Fixed `memory_lint` reporting findings for TTL-expired pages. The lint passnow omits expired pages before evaluating episodic staleness, rule suggestions,duplicate titles, and vector or LLM contradiction candidates, preventingdiff --git a/crates/ai-memory-llm/src/cursor_agent.rs b/crates/ai-memory-llm/src/cursor_agent.rsindex a754708a..056b16f9 100644--- a/crates/ai-memory-llm/src/cursor_agent.rs+++ b/crates/ai-memory-llm/src/cursor_agent.rs@@ -339,6 +339,14 @@ fn parse_json_answer(text: &str) -> LlmResult<serde_json::Value> {{return Ok(value);}+ // Prose around the answer can carry its own brackets (`for [the+ // request]:`, `(see [schema])`), which throws the first/last slice off.+ // Fall back to the first balanced, string-aware object.+ if let Some(value) = crate::openai_compat::first_json_object(stripped)+ .and_then(|slice| serde_json::from_str::<serde_json::Value>(slice).ok())+ {+ return Ok(value);+ }Err(LlmError::UnexpectedShape(format!("cursor agent did not return JSON: {}",truncate_chars(stripped, 200)diff --git a/crates/ai-memory-llm/tests/suite/cursor_provider.rs b/crates/ai-memory-llm/tests/suite/cursor_provider.rsindex 513e1bd7..eb6fbf56 100644--- a/crates/ai-memory-llm/tests/suite/cursor_provider.rs+++ b/crates/ai-memory-llm/tests/suite/cursor_provider.rs@@ -121,6 +121,28 @@ async fn fenced_json_is_parsed() {assert_eq!(value["answer"], 1);}+/// Ask mode often wraps the answer in prose. Brackets in that prose must not+/// hide a well-formed JSON object from the parser.+#[tokio::test]+async fn json_wrapped_in_bracketed_prose_is_parsed() {+ for stdout in [+ "Here is the result for [the request]:\n{\"answer\":1}",+ "{\"answer\":1}\n(see [schema])",+ ] {+ let dir = TempDir::new();+ let sink = dir.0.join("argv");+ let executable = fake_agent(&dir.0, &sink, stdout);+ let value = provider(executable, "default")+ .complete_structured_raw(+ ChatRequest::user_prompt("status"),+ serde_json::json!({"type": "object"}),+ )+ .await+ .unwrap_or_else(|err| panic!("{stdout:?}: {err}"));+ assert_eq!(value["answer"], 1, "{stdout:?}");+ }+}+fn script_agent(dir: &Path, body: &str) -> PathBuf {let path = dir.join("agent");std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap();