-
Notifications
You must be signed in to change notification settings - Fork 38
Add deterministic code and snippet memory identity #181
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -31,6 +31,13 @@ | |
| Operation, | ||
| OperationType, | ||
| ) | ||
| from src.schemas.code import ( | ||
| code_annotation_content_hash, | ||
| code_annotation_fields_from_storage_content, | ||
| code_annotation_identity_key, | ||
| snippet_fields_from_storage_content, | ||
| snippet_identity_hash, | ||
| ) | ||
| from src.storage.base import BaseVectorStore, SearchResult | ||
|
|
||
|
|
||
|
|
@@ -193,6 +200,10 @@ async def arun_deterministic(self, state: Dict[str, Any]) -> JudgeResult: | |
| result = await self._deterministic_profile(new_items, user_id) | ||
| elif domain == JudgeDomain.TEMPORAL: | ||
| result = await self._deterministic_temporal(new_items, user_id) | ||
| elif domain == JudgeDomain.CODE: | ||
| result = await self._deterministic_code(new_items, user_id) | ||
| elif domain == JudgeDomain.SNIPPET: | ||
| result = await self._deterministic_snippet(new_items, user_id) | ||
| else: | ||
| self.logger.warning( | ||
| "Deterministic judge unsupported for %s; falling back to LLM judge.", | ||
|
|
@@ -482,6 +493,97 @@ async def _deterministic_temporal( | |
|
|
||
| return JudgeResult(operations=operations, confidence=1.0) | ||
|
|
||
| async def _deterministic_code( | ||
| self, new_items: list, user_id: str, | ||
| ) -> JudgeResult: | ||
| unique_items: dict[str, tuple[str, dict[str, Any]]] = {} | ||
| for item in new_items: | ||
| content = str(item) | ||
| fields = code_annotation_fields_from_storage_content(content) | ||
| unique_items[code_annotation_identity_key(fields)] = (content, fields) | ||
|
|
||
| async def _process_one(content: str, fields: dict[str, Any]) -> Operation: | ||
| match = await self._lookup_metadata_match({ | ||
| "user_id": user_id, | ||
| "domain": JudgeDomain.CODE.value, | ||
| "annotation_key": code_annotation_identity_key(fields), | ||
| }) | ||
|
|
||
| if match is None: | ||
| return Operation( | ||
| type=OperationType.ADD, | ||
| content=content, | ||
| reason="No code annotation with the same repo/target/type key.", | ||
| ) | ||
|
|
||
| incoming_hash = code_annotation_content_hash(fields) | ||
| existing_hash = str((match.metadata or {}).get("annotation_hash", "")) | ||
| if incoming_hash == existing_hash: | ||
| return Operation( | ||
| type=OperationType.NOOP, | ||
| content=content, | ||
| embedding_id=match.id, | ||
| reason="Existing code annotation is unchanged.", | ||
| ) | ||
| return Operation( | ||
| type=OperationType.UPDATE, | ||
| content=content, | ||
| embedding_id=match.id, | ||
| reason="Existing code annotation target has updated content.", | ||
| ) | ||
|
|
||
| operations = await asyncio.gather(*( | ||
| _process_one(content, fields) | ||
| for content, fields in unique_items.values() | ||
| )) | ||
| return JudgeResult(operations=operations, confidence=1.0) | ||
|
|
||
| async def _deterministic_snippet( | ||
| self, new_items: list, user_id: str, | ||
| ) -> JudgeResult: | ||
| unique_items: dict[str, tuple[str, dict[str, Any]]] = {} | ||
| for item in new_items: | ||
| content = str(item) | ||
| fields = snippet_fields_from_storage_content(content) | ||
| unique_items[snippet_identity_hash(fields)] = (content, fields) | ||
|
|
||
| async def _process_one(content: str, fields: dict[str, Any]) -> Operation: | ||
| match = await self._lookup_metadata_match({ | ||
| "user_id": user_id, | ||
| "domain": JudgeDomain.SNIPPET.value, | ||
| "snippet_hash": snippet_identity_hash(fields), | ||
| }) | ||
|
|
||
| if match is None: | ||
| return Operation( | ||
| type=OperationType.ADD, | ||
| content=content, | ||
| reason="No snippet with the same normalized code/content identity.", | ||
| ) | ||
| return Operation( | ||
| type=OperationType.NOOP, | ||
| content=content, | ||
| embedding_id=match.id, | ||
| reason="Same snippet was already stored for this user.", | ||
| ) | ||
|
|
||
| operations = await asyncio.gather(*( | ||
| _process_one(content, fields) | ||
| for content, fields in unique_items.values() | ||
| )) | ||
| return JudgeResult(operations=operations, confidence=1.0) | ||
|
Comment on lines
+541
to
+574
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. Similar to async def _deterministic_snippet(
self, new_items: list, user_id: str,
) -> JudgeResult:
# Deduplicate items by snippet hash to prevent redundant operations
unique_items: dict[str, tuple[str, dict]] = {}
for item in new_items:
content = str(item)
fields = snippet_fields_from_storage_content(content)
h = snippet_identity_hash(fields)
unique_items[h] = (content, fields)
async def _process_one(content: str, fields: dict) -> Operation:
match = await self._lookup_metadata_match({
"user_id": user_id,
"domain": JudgeDomain.SNIPPET.value,
"snippet_hash": snippet_identity_hash(fields),
})
if match is None:
return Operation(
type=OperationType.ADD,
content=content,
reason="No snippet with the same normalized code/content identity.",
)
else:
return Operation(
type=OperationType.NOOP,
content=content,
embedding_id=match.id,
reason="Same snippet was already stored for this user.",
)
tasks = [_process_one(c, f) for c, f in unique_items.values()]
operations = await asyncio.gather(*tasks)
return JudgeResult(operations=list(operations), confidence=1.0) |
||
|
|
||
| async def _lookup_metadata_match( | ||
| self, filters: Dict[str, Any], | ||
| ) -> Optional[SearchResult]: | ||
| if not self.vector_store: | ||
| return None | ||
| search_fn = getattr(self.vector_store, "search_by_metadata", None) | ||
| if search_fn is None: | ||
| return None | ||
| results = await asyncio.to_thread(search_fn, filters=filters, top_k=1) | ||
| return _first_match(results or []) | ||
|
Comment on lines
+576
to
+585
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.
|
||
|
|
||
| # -- Response parsing -------------------------------------------------- | ||
|
|
||
| def _parse_response( | ||
|
|
||
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.
The
_deterministic_codemethod processes items sequentially and lacks deduplication of the incomingnew_items. If multiple identical annotations are extracted in a single turn, this will result in redundant operations and potential duplicate records in the vector store. It is recommended to deduplicate items by their identity key and useasyncio.gatherto perform metadata lookups in parallel, maintaining consistency with the profile and temporal domains.