From 248ecd98f14a2af3c91e31e06d63014945cc3a11 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 12 Apr 2026 22:24:41 +0000 Subject: [PATCH] fix: polish sanitizer and repair messaging Agent-Logs-Url: https://github.com/MemPalace/mempalace/sessions/775f2fc4-3051-462e-8586-6d694b55da0d Co-authored-by: igorls <4753812+igorls@users.noreply.github.com> --- mempalace/cli.py | 3 ++- mempalace/query_sanitizer.py | 12 ++++++++++-- tests/test_migrate.py | 6 ++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/mempalace/cli.py b/mempalace/cli.py index 6acfcb4..4483b4f 100644 --- a/mempalace/cli.py +++ b/mempalace/cli.py @@ -228,7 +228,8 @@ def cmd_repair(args): if os.path.exists(backup_path): if not contains_palace_database(backup_path): print( - " Cannot proceed: backup path exists but is not a valid palace database. " + " Cannot proceed: backup path exists but does not contain a valid palace backup " + "(expected chroma.sqlite3). " f"Please remove or rename: {backup_path}" ) return diff --git a/mempalace/query_sanitizer.py b/mempalace/query_sanitizer.py index cb783f7..9741312 100644 --- a/mempalace/query_sanitizer.py +++ b/mempalace/query_sanitizer.py @@ -67,13 +67,21 @@ def sanitize_query(raw_query: str) -> dict: raw_query = raw_query.strip() original_length = len(raw_query) + def _strip_wrapping_quotes(candidate: str) -> str: + candidate = candidate.strip() + while candidate[:1] in {"'", '"'} or candidate[-1:] in {"'", '"'}: + candidate = candidate.strip("\"'") + if not candidate: + return "" + return candidate + def _trim_candidate(candidate: str) -> str: - candidate = candidate.strip().strip("\"'") + candidate = _strip_wrapping_quotes(candidate) if len(candidate) <= MAX_QUERY_LENGTH: return candidate nested_fragments = [ - frag.strip().strip("\"'") for frag in _SENTENCE_SPLIT.split(candidate) if frag.strip() + _strip_wrapping_quotes(frag) for frag in _SENTENCE_SPLIT.split(candidate) if frag.strip() ] for frag in reversed(nested_fragments): if MIN_QUERY_LENGTH <= len(frag) <= MAX_QUERY_LENGTH: diff --git a/tests/test_migrate.py b/tests/test_migrate.py index 1a02462..33c9191 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -22,8 +22,10 @@ def test_migrate_aborts_without_confirmation(tmp_path, capsys): palace_dir.mkdir() (palace_dir / "chroma.sqlite3").write_text("db") - mock_chromadb = SimpleNamespace(__version__="0.6.0", PersistentClient=MagicMock()) - mock_chromadb.PersistentClient.side_effect = Exception("unreadable") + mock_chromadb = SimpleNamespace( + __version__="0.6.0", + PersistentClient=MagicMock(side_effect=Exception("unreadable")), + ) with ( patch.dict("sys.modules", {"chromadb": mock_chromadb}),