Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(llm): strip reasoning that ends in an unopened closing tag
Rudy Celeklib28451843142
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fee7fa0b..e76ef855 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,11 @@ version in [`docs/changelog/`](docs/changelog/):## [Unreleased]### Fixed+- Stripped a reasoning section that ends in a bare `</think>` (or+ `</thinking>`, `</analysis>`, `</reasoning>`) from openai-compat structured+ output. Chat templates that open the reasoning block themselves (DeepSeek-R1,+ QwQ, Qwen3 thinking models) return no opening tag, so the reasoning stayed in+ front of the answer and the JSON extractor could pick a draft object out of it.- 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/openai_compat.rs b/crates/ai-memory-llm/src/openai_compat.rsindex 0e9538c7..8b600262 100644--- a/crates/ai-memory-llm/src/openai_compat.rs+++ b/crates/ai-memory-llm/src/openai_compat.rs@@ -36,6 +36,12 @@ static REASONING_BLOCK_RE: LazyLock<Regex> = LazyLock::new(|| {.unwrap()});+// The first closing reasoning tag. Chat templates that open the reasoning+// block themselves (DeepSeek-R1, QwQ, Qwen3 thinking models) return the+// reasoning followed by a bare `</think>`, with no opening tag to pair it.+static REASONING_CLOSE_RE: LazyLock<Regex> =+ LazyLock::new(|| Regex::new(r"(?i)</(?:think|thinking|analysis|reasoning)>").unwrap());+// Matches an outermost ```json ... ``` or ``` ... ``` markdown fence.static CODE_FENCE_RE: LazyLock<Regex> =LazyLock::new(|| Regex::new(r"(?s)\A\s*```(?:json)?\s*\n(.*?)\n\s*```\s*\z").unwrap());@@ -46,6 +52,20 @@ static CODE_FENCE_RE: LazyLock<Regex> =/// between the cleaned and the raw form.pub(crate) fn strip_reasoning_blocks(s: &str) -> String {let stripped = REASONING_BLOCK_RE.replace_all(s, "");+ // A closing tag left after the paired blocks are gone ends a reasoning+ // section whose opening tag the chat template supplied. Drop that+ // section too, unless the text before it already looks like the JSON+ // answer (a literal closing tag inside a JSON string must survive).+ let stripped = match REASONING_CLOSE_RE.find(stripped.as_ref()) {+ Some(close)+ if !stripped[..close.start()]+ .trim_start()+ .starts_with(['{', '[', '`']) =>+ {+ std::borrow::Cow::Owned(stripped[close.end()..].to_string())+ }+ _ => stripped,+ };// If the entire remaining payload is wrapped in a code fence, unwrap it// so `serde_json::from_str` can see the bare JSON directly.if let Some(caps) = CODE_FENCE_RE.captures(stripped.as_ref()) {@@ -700,6 +720,35 @@ mod tests {assert_eq!(v["findings"], serde_json::json!([]));}+ /// A chat template that opens the reasoning block itself leaves the+ /// completion with reasoning text and a bare `</think>`. Without the+ /// opening tag the paired-block regex never matched, and the brace+ /// extractor took a draft object out of the reasoning instead of the+ /// answer.+ #[test]+ fn strip_reasoning_without_its_opening_tag() {+ let input = "Okay, the schema wants {\"findings\":[\"draft\"]} roughly.\n</think>\n\n{\"findings\":[]}";+ let cleaned = strip_reasoning_blocks(input);+ let v: serde_json::Value = serde_json::from_str(&cleaned).expect("the real answer");+ assert_eq!(v["findings"], serde_json::json!([]));++ let upper = strip_reasoning_blocks("Let me think.</THINKING>```json\n{\"a\":1}\n```");+ assert_eq!(upper, "{\"a\":1}");+ }++ /// A JSON answer that merely mentions a closing tag in a string value is+ /// not reasoning and must come through intact.+ #[test]+ fn closing_tag_inside_the_json_answer_is_kept() {+ let input = "{\"body\":\"models end reasoning with </think>\"}";+ assert_eq!(strip_reasoning_blocks(input), input);+ let fenced = "```json\n{\"body\":\"a </think> tag\"}\n```";+ assert_eq!(+ strip_reasoning_blocks(fenced),+ "{\"body\":\"a </think> tag\"}"+ );+ }+/// `<analysis>` prefix is also stripped.#[test]fn strip_analysis_block() {