Skip to content

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

Rust

fix(auto-improve): sanitize Windows-illegal LLM page paths

kevin9327aeb5ce5376de
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 188df552..a4f2f934 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
+- Auto-improve review no longer stages a proposal whose LLM-produced page
+ path contains a Windows-illegal character (e.g. a `:` copied from a
+ conventional-commit subject). That path passed the deliberately tolerant
+ `PagePath::new` and only failed later at `ensure_portable` when the
+ proposal was approved, so the learning loop queued work that could not
+ be applied. Paths are now sanitized the same way bootstrap (#847) and
+ per-session consolidation (#848) already sanitize theirs, before
+ validation; a path that is still unportable after sanitizing is rejected
+ instead of staged.
- `companions/ai-memory-macos/build.sh` no longer fails on machines whose
active developer directory is Command Line Tools only: SwiftUI `@State`
needs the `SwiftUIMacros` plugin shipped with full Xcode, so the script
diff --git a/crates/ai-memory-consolidate/src/auto_improve.rs b/crates/ai-memory-consolidate/src/auto_improve.rs
index 6136bc66..89fa3c08 100644
--- a/crates/ai-memory-consolidate/src/auto_improve.rs
+++ b/crates/ai-memory-consolidate/src/auto_improve.rs
@@ -21,6 +21,7 @@ use serde::{Deserialize, Serialize, de};
use sha2::{Digest, Sha256};
use thiserror::Error;
+use crate::path_sanitize::slugify_page_path;
use crate::projection::{ObservationProjectionConfig, cap_text_with_marker, project_observations};
const CHARS_PER_TOKEN: usize = 4;
@@ -1590,7 +1591,18 @@ fn validate_proposal(
{
return Err("missing_evidence".into());
}
- let path = PagePath::new(proposal.path.clone()).map_err(|_| "invalid_path".to_string())?;
+ // Same class as bootstrap #847 / consolidation #848: a model-produced
+ // path with a Windows-illegal character (e.g. `:`) passes `PagePath::new`
+ // and would be staged, then fail `ensure_portable` at approve time.
+ // Sanitize before constructing the path so the staged proposal is
+ // applyable; a path that is still unportable after slugify is rejected
+ // rather than queued.
+ let cleaned = slugify_page_path(&proposal.path);
+ let path = PagePath::new(&cleaned).map_err(|_| "invalid_path".to_string())?;
+ if path.ensure_portable().is_err() {
+ return Err("invalid_path".into());
+ }
+ proposal.path = path.as_str().to_string();
match proposal.edit_mode.as_str() {
"" | "full_page" => {
validate_full_page_proposal(proposal, cfg, existing_index, path.as_str())
@@ -2604,6 +2616,53 @@ mod tests {
assert!(warnings.is_empty());
}
+ /// Same class as bootstrap #847 / consolidation #848: the reviewer LLM
+ /// echoes a conventional-commit subject into the page path. That passes
+ /// `PagePath::new` (deliberately tolerant) but fails `ensure_portable`,
+ /// so the proposal is staged and then cannot be applied.
+ #[test]
+ fn windows_illegal_model_path_is_sanitized_before_staging() {
+ let raw_path = "concepts/build(sandbox): orchestrate.md";
+ assert!(
+ PagePath::new(raw_path).is_ok(),
+ "precondition: PagePath::new is deliberately tolerant of ':'"
+ );
+ assert!(
+ PagePath::new(raw_path).unwrap().ensure_portable().is_err(),
+ "precondition: the raw model path is not portable"
+ );
+
+ let raw: AutoImproveLlmResponse = serde_json::from_value(serde_json::json!({
+ "summary": "ok",
+ "proposals": [{
+ "path": raw_path,
+ "title": "Orchestrate the run",
+ "kind": "concept",
+ "confidence": 0.91,
+ "rationale": "The session recorded a durable conventional-commit workflow.",
+ "evidence": [{"quote": "build(sandbox): orchestrate the run"}],
+ "body_markdown": "# Orchestrate the run\n\nKeep the sandbox build orchestrated."
+ }],
+ "rejected_candidates": []
+ }))
+ .unwrap();
+
+ let (accepted, rejected, warnings) =
+ validate_response(raw, &cfg(), &ExistingPageIndex::default());
+ assert!(
+ rejected.is_empty(),
+ "sanitized path should be accepted: {rejected:?}"
+ );
+ assert!(warnings.is_empty());
+ assert_eq!(accepted.len(), 1);
+ assert_eq!(accepted[0].path, "concepts/build(sandbox)- orchestrate.md");
+ let path = PagePath::new(&accepted[0].path).unwrap();
+ assert!(
+ path.ensure_portable().is_ok(),
+ "staged path must be applyable"
+ );
+ }
+
#[test]
fn proposal_body_accepts_common_markdown_aliases() {
let raw: AutoImproveLlmResponse = serde_json::from_value(serde_json::json!({
diff --git a/crates/ai-memory-consolidate/src/path_sanitize.rs b/crates/ai-memory-consolidate/src/path_sanitize.rs
index d3c86620..a994e386 100644
--- a/crates/ai-memory-consolidate/src/path_sanitize.rs
+++ b/crates/ai-memory-consolidate/src/path_sanitize.rs
@@ -1,12 +1,14 @@
//! Shared model-path sanitization for LLM-produced wiki paths.
//!
-//! Both bootstrap (#847) and per-session consolidation (#848) accept a
-//! page path straight from LLM structured output and hand it to
-//! `Wiki::apply_batch`, which is atomic: a single path that fails
-//! `PagePath::ensure_portable` at write time aborts every page in that
-//! batch, not just its own. `PagePath::new` is deliberately tolerant (see
-//! its doc comment) and does not catch this, so callers must sanitize the
-//! raw model path themselves before constructing a `PagePath`.
+//! Bootstrap (#847), per-session consolidation (#848), and auto-improve
+//! review all accept a page path straight from LLM structured output.
+//! Bootstrap and consolidation hand it to `Wiki::apply_batch`, which is
+//! atomic: a single path that fails `PagePath::ensure_portable` at write
+//! time aborts every page in that batch, not just its own. Auto-improve
+//! stages the path and only hits `ensure_portable` on approve, so a bad
+//! path becomes an unapplyable proposal. `PagePath::new` is deliberately
+//! tolerant (see its doc comment) and does not catch this, so callers must
+//! sanitize the raw model path themselves before constructing a `PagePath`.
/// Filename characters Windows refuses, mirroring
/// `ai_memory_core::ids`'s reserved-char set, plus `\` — `PagePath::new`
@@ -23,6 +25,7 @@ pub(crate) const PATH_ILLEGAL_CHARS: &[char] = &['<', '>', ':', '"', '|', '?', '
/// `PagePath::new` (deliberately tolerant; see its doc comment) but fails
/// `ensure_portable`, which `Wiki::apply_batch` enforces atomically: one bad
/// path there aborts every page in the batch, not just its own (#847, #848).
+/// Auto-improve review stages the path and hits the same check on approve.
/// Replace every Windows-illegal character and ASCII control byte in each
/// `/`-separated component with `-`, keeping the `dir/subdir/name.md` shape
/// intact so the model's intended layout survives.