-
Notifications
You must be signed in to change notification settings - Fork 20
feat: improve cross-session memory quality #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import json | ||
| import re | ||
| import shutil | ||
| import unicodedata | ||
| from pathlib import Path | ||
|
|
||
| import yaml | ||
|
|
@@ -40,10 +41,23 @@ def remember(self, proposal): | |
| source_hash = proposal.get("source_hash") | ||
| if source_hash: | ||
| row = self.store.connection.execute( | ||
| "SELECT memory_id FROM memory_index WHERE source_hash=?", (source_hash,) | ||
| "SELECT memory_id FROM memory_index " | ||
| "WHERE source_hash=? AND status='active' AND source_kind='markdown'", | ||
| (source_hash,), | ||
| ).fetchone() | ||
| if row: | ||
| return {"created": False, "memory_id": row[0], "superseded": []} | ||
| return { | ||
| "created": False, "deduplicated": True, | ||
| "memory_id": row[0], "superseded": [], | ||
| } | ||
| normalized_summary = self._normalize_summary(summary) | ||
| duplicate = self._find_active_duplicate(mem_type, normalized_summary) | ||
| if duplicate: | ||
|
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a caller supplies Useful? React with 👍 / 👎. |
||
| self._merge_duplicate_provenance(duplicate, proposal) | ||
| return { | ||
| "created": False, "deduplicated": True, | ||
| "memory_id": duplicate["memory_id"], "superseded": [], | ||
| } | ||
| supersedes = self._normalize_supersedes(proposal.get("supersedes")) | ||
| root = self.config.knowledge_root / self.config.managed_memory_dir | ||
| targets = self._load_active_targets(supersedes, root) | ||
|
|
@@ -80,6 +94,55 @@ def remember(self, proposal): | |
| "superseded": supersedes, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def _normalize_summary(summary): | ||
| """Normalize only exact textual duplicates; never use fuzzy similarity.""" | ||
| normalized = unicodedata.normalize("NFKC", str(summary or "")) | ||
| return re.sub(r"\s+", " ", normalized).strip().casefold() | ||
|
|
||
| def _find_active_duplicate(self, mem_type, normalized_summary): | ||
| rows = self.store.connection.execute( | ||
| "SELECT memory_id, markdown_path, summary FROM memory_index " | ||
| "WHERE source_kind='markdown' AND status='active' AND type=?", | ||
| (mem_type,), | ||
| ).fetchall() | ||
| for row in rows: | ||
| if self._normalize_summary(row["summary"]) == normalized_summary: | ||
| return row | ||
| return None | ||
|
|
||
| @staticmethod | ||
| def _merge_unique(existing, incoming): | ||
| values = [] | ||
| for collection in (existing, incoming): | ||
| if not isinstance(collection, list): | ||
| continue | ||
| for value in collection: | ||
| if value not in values: | ||
| values.append(value) | ||
| return values | ||
|
|
||
| def _merge_duplicate_provenance(self, duplicate, proposal): | ||
| root = self.config.knowledge_root / self.config.managed_memory_dir | ||
| targets = self._load_active_targets([duplicate["memory_id"]], root) | ||
| target = targets[0] | ||
| meta = dict(target["meta"]) | ||
| meta["source_refs"] = self._merge_unique( | ||
| meta.get("source_refs", []), proposal.get("source_refs", []) | ||
| ) | ||
| meta["clients"] = self._merge_unique( | ||
| meta.get("clients", []), proposal.get("clients", []) | ||
| ) | ||
| now = time.strftime("%Y-%m-%dT%H:%M:%S%z") | ||
| meta["updated"] = now | ||
| if proposal.get("project_path") and not meta.get("project_path"): | ||
| meta["project_path"] = proposal["project_path"] | ||
| if proposal.get("session_id") and not meta.get("session_id"): | ||
| meta["session_id"] = proposal["session_id"] | ||
| self._install_memory_files({ | ||
| target["path"]: self._render_memory(meta, target["body"]), | ||
| }) | ||
|
|
||
| @staticmethod | ||
| def _normalize_supersedes(value): | ||
| if value is None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filtering the source-hash idempotency lookup to
status='active'means retrying or reimporting the same source after its memory was intentionally superseded no longer no-ops; if the old summary is no longer active, the method proceeds to create a fresh active Markdown page from that superseded source. Sincesource_hashidentifies already-processed source material, this can resurrect obsolete facts during replays; the lookup should still detect existing Markdown records with the same source hash rather than only active ones.Useful? React with 👍 / 👎.