-
Notifications
You must be signed in to change notification settings - Fork 92
feat: write-time dedup gate (ADD/UPDATE/SUPERSEDE/NOOP) #87
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
jescalan
wants to merge
3
commits into
verygoodplugins:main
Choose a base branch
from
jescalan:feat/write-time-dedup
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.
+372
−0
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,184 @@ | ||
| """Write-time deduplication gate for AutoMem. | ||
|
|
||
| Before storing a new memory, checks for semantically similar existing memories | ||
| and uses an LLM to classify the appropriate action: | ||
|
|
||
| - ADD: Genuinely new information, store normally | ||
| - UPDATE: Refines/adds detail to an existing memory, merge into it | ||
| - SUPERSEDE: Replaces an outdated memory (delete old, store new) | ||
| - NOOP: Already known, skip entirely | ||
|
|
||
| Inspired by Helixir's decision engine approach. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| from typing import Any, Callable, Dict, List, Optional, Tuple | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Minimum vector similarity to even consider dedup (below this, always ADD) | ||
| SIMILARITY_THRESHOLD = 0.70 | ||
|
|
||
| # Maximum candidates to evaluate | ||
| MAX_CANDIDATES = 3 | ||
|
|
||
| DEDUP_PROMPT = """You are a memory deduplication system. Given a NEW memory and EXISTING memories, decide what to do. | ||
|
|
||
| NEW MEMORY: | ||
| {new_content} | ||
|
|
||
| EXISTING MEMORIES: | ||
| {existing_memories} | ||
|
|
||
| For each existing memory, decide the relationship to the new memory. Then output ONE action for the new memory: | ||
|
|
||
| - ADD: The new memory contains genuinely new information not covered by any existing memory. Store it. | ||
| - UPDATE <id>: The new memory refines, corrects, or adds meaningful detail to an existing memory. The existing memory should be updated to incorporate the new information. Output the merged content. | ||
| - SUPERSEDE <id>: The new memory replaces an outdated existing memory (e.g., a decision changed, a status updated). The old one should be deleted and the new one stored. | ||
| - NOOP: The new memory is already fully covered by existing memories. Skip it. | ||
|
|
||
| Rules: | ||
| - If the new memory has ANY meaningful new information beyond what exists, prefer ADD or UPDATE over NOOP. | ||
| - UPDATE means the existing memory's content should be expanded/corrected. Provide the merged text. | ||
| - SUPERSEDE means the old memory is wrong/outdated and should be replaced entirely. | ||
| - NOOP only if the new memory is truly redundant — same facts, same level of detail. | ||
| - When in doubt, ADD. False negatives (storing a near-dupe) are less harmful than false positives (losing information). | ||
|
|
||
| Respond with ONLY valid JSON: | ||
| {{"action": "ADD"}} | ||
| or | ||
| {{"action": "UPDATE", "target_id": "<id>", "merged_content": "<full merged text>"}} | ||
| or | ||
| {{"action": "SUPERSEDE", "target_id": "<id>"}} | ||
| or | ||
| {{"action": "NOOP", "reason": "<brief reason>"}}""" | ||
|
|
||
|
|
||
| def check_dedup( | ||
| new_content: str, | ||
| generate_embedding: Callable[[str], List[float]], | ||
| qdrant_client: Any, | ||
| collection_name: str, | ||
| openai_client: Any, | ||
| model: str = "gpt-4o-mini", | ||
| similarity_threshold: float = SIMILARITY_THRESHOLD, | ||
| ) -> Dict[str, Any]: | ||
| """Check if a new memory should be added, merged, or skipped. | ||
|
|
||
| Returns: | ||
| Dict with keys: | ||
| - action: "ADD" | "UPDATE" | "SUPERSEDE" | "NOOP" | ||
| - target_id: (for UPDATE/SUPERSEDE) the existing memory ID to modify | ||
| - merged_content: (for UPDATE) the merged text | ||
| - reason: (for NOOP) why it was skipped | ||
| - candidates: list of similar memories found (for debugging) | ||
| """ | ||
| result: Dict[str, Any] = {"action": "ADD", "candidates": []} | ||
|
|
||
| if not qdrant_client or not openai_client: | ||
| return result | ||
|
|
||
| # Step 1: Generate embedding for the new content | ||
| try: | ||
| embedding = generate_embedding(new_content) | ||
| except Exception: | ||
| logger.warning("Failed to generate embedding for dedup check, defaulting to ADD") | ||
| return result | ||
|
|
||
| if not embedding: | ||
| return result | ||
|
|
||
| # Step 2: Search for similar existing memories | ||
| try: | ||
| from qdrant_client.models import Filter # noqa: F401 | ||
|
|
||
| search_results = qdrant_client.search( | ||
| collection_name=collection_name, | ||
| query_vector=embedding, | ||
| limit=MAX_CANDIDATES, | ||
| score_threshold=similarity_threshold, | ||
| ) | ||
| except Exception: | ||
| logger.warning("Qdrant search failed during dedup check, defaulting to ADD") | ||
| return result | ||
|
jescalan marked this conversation as resolved.
|
||
|
|
||
| if not search_results: | ||
| return result | ||
|
|
||
| # Step 3: Format candidates for LLM | ||
| candidates = [] | ||
| for hit in search_results: | ||
| payload = hit.payload or {} | ||
| candidates.append( | ||
| { | ||
| "id": str(hit.id), | ||
| "content": payload.get("content", ""), | ||
| "score": round(hit.score, 3), | ||
| "type": payload.get("type", ""), | ||
| "importance": payload.get("importance", 0), | ||
| } | ||
| ) | ||
|
|
||
| result["candidates"] = candidates | ||
|
|
||
| existing_text = "\n\n".join( | ||
| f"[ID: {c['id']}] (similarity: {c['score']})\n{c['content']}" | ||
| for c in candidates | ||
| ) | ||
|
|
||
| # Step 4: Ask LLM to classify | ||
| prompt = DEDUP_PROMPT.format( | ||
| new_content=new_content, | ||
| existing_memories=existing_text, | ||
| ) | ||
|
|
||
| try: | ||
| response = openai_client.chat.completions.create( | ||
| model=model, | ||
| messages=[{"role": "user", "content": prompt}], | ||
| temperature=0, | ||
| max_tokens=1000, | ||
| response_format={"type": "json_object"}, | ||
| ) | ||
|
|
||
| raw = response.choices[0].message.content.strip() | ||
| decision = json.loads(raw) | ||
|
jescalan marked this conversation as resolved.
|
||
|
|
||
| action = decision.get("action", "ADD").upper() | ||
| if action not in ("ADD", "UPDATE", "SUPERSEDE", "NOOP"): | ||
| action = "ADD" | ||
|
|
||
| result["action"] = action | ||
|
|
||
| if action == "UPDATE": | ||
| result["target_id"] = decision.get("target_id", "") | ||
| result["merged_content"] = decision.get("merged_content", "") | ||
| if not result["target_id"] or not result["merged_content"]: | ||
| # Invalid UPDATE response, fall back to ADD | ||
| result["action"] = "ADD" | ||
| logger.warning("LLM returned UPDATE without target_id or merged_content, falling back to ADD") | ||
|
|
||
| elif action == "SUPERSEDE": | ||
| result["target_id"] = decision.get("target_id", "") | ||
| if not result["target_id"]: | ||
| result["action"] = "ADD" | ||
| logger.warning("LLM returned SUPERSEDE without target_id, falling back to ADD") | ||
|
|
||
| elif action == "NOOP": | ||
| result["reason"] = decision.get("reason", "duplicate") | ||
|
|
||
| logger.info( | ||
| "Dedup decision: %s (candidates: %d, top_score: %.3f)", | ||
| result["action"], | ||
| len(candidates), | ||
| candidates[0]["score"] if candidates else 0, | ||
| ) | ||
|
|
||
| except Exception: | ||
| logger.warning("LLM dedup classification failed, defaulting to ADD", exc_info=True) | ||
| result["action"] = "ADD" | ||
|
|
||
| return result | ||
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.