-
Notifications
You must be signed in to change notification settings - Fork 26
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
Open
hunterbastian
wants to merge
2
commits into
XortexAI:main
Choose a base branch
from
hunterbastian:codex-code-snippet-schema
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.
Open
Changes from all commits
Commits
Show all changes
2 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
| 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 []) | ||
|
|
||
| # -- Response parsing -------------------------------------------------- | ||
|
|
||
| def _parse_response( | ||
|
|
||
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
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.
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.