-
Notifications
You must be signed in to change notification settings - Fork 38
Add SQLite memory lifecycle hashing, versioning, and forget support #199
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
Open
RYB-404
wants to merge
3
commits into
XortexAI:main
Choose a base branch
from
RYB-404:ryb-xmem-memory-lifecycle-166
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+495
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Memory lifecycle metadata helpers. | ||
|
|
||
| These helpers keep duplicate detection, version lineage, and soft-forget | ||
| metadata consistent across vector-store implementations. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
| import re | ||
| from datetime import datetime, timezone | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| CONTENT_HASH_KEY = "content_hash" | ||
| PARENT_MEMORY_ID_KEY = "parent_memory_id" | ||
| VERSION_KEY = "version" | ||
| IS_CURRENT_KEY = "is_current" | ||
| FORGOTTEN_AT_KEY = "forgotten_at" | ||
| FORGET_REASON_KEY = "forget_reason" | ||
|
|
||
|
|
||
| def normalize_memory_content(content: str) -> str: | ||
| """Normalize memory text before hashing to catch whitespace-only duplicates.""" | ||
|
|
||
| return re.sub(r"\s+", " ", content.strip()).casefold() | ||
|
|
||
|
|
||
| def compute_memory_hash(content: str) -> str: | ||
| """Return a stable SHA-256 digest for normalized memory content.""" | ||
|
|
||
| normalized = normalize_memory_content(content) | ||
| return hashlib.sha256(normalized.encode("utf-8")).hexdigest() | ||
|
|
||
|
|
||
| def utc_now_iso() -> str: | ||
| return datetime.now(timezone.utc).isoformat() | ||
|
|
||
|
|
||
| def build_lifecycle_metadata( | ||
| content: str, | ||
| metadata: Optional[Dict[str, Any]] = None, | ||
| *, | ||
| parent_memory_id: Optional[str] = None, | ||
| version: int = 1, | ||
| is_current: bool = True, | ||
| ) -> Dict[str, Any]: | ||
| """Merge caller metadata with lifecycle fields without losing custom keys.""" | ||
|
|
||
| merged = dict(metadata or {}) | ||
| merged[CONTENT_HASH_KEY] = compute_memory_hash(content) | ||
| merged[PARENT_MEMORY_ID_KEY] = parent_memory_id | ||
| merged[VERSION_KEY] = version | ||
| merged[IS_CURRENT_KEY] = is_current | ||
| merged[FORGOTTEN_AT_KEY] = None | ||
| merged[FORGET_REASON_KEY] = None | ||
| return merged | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def is_retrievable_memory(metadata: Optional[Dict[str, Any]]) -> bool: | ||
| """Return False for superseded or soft-forgotten memory records.""" | ||
|
|
||
| meta = metadata or {} | ||
| return meta.get(IS_CURRENT_KEY, True) is not False and not meta.get(FORGOTTEN_AT_KEY) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.