XMem icon indicating copy to clipboard operation
XMem copied to clipboard

Add deterministic code and snippet memory identity

Open hunterbastian opened this issue 6 months ago • 5 comments

Summary

Implements deterministic identity metadata for code annotations and personal snippets so XMem can avoid re-judging exact code/snippet memories with an LLM.

Changes:

  • add stable Pinecone metadata helpers for snippet identity, snippet search text, code annotation identity keys, and code annotation content hashes
  • route code and snippet memory through deterministic judge paths using metadata lookups
  • store snippet_hash, annotation_key, and annotation_hash in Pinecone metadata
  • keep snippet code exact in metadata while embedding only the searchable description/language/tags text
  • add regression coverage for repeated snippets across sessions and same-target code annotation updates

This addresses the edge case discussed in #141 where a user sends a snippet, then asks for the same snippet in another session. The normalized snippet_hash lets the judge no-op the duplicate without another model call.

Verification

  • python3 -m compileall src/schemas/code.py src/agents/judge.py src/pipelines/ingest.py src/pipelines/weaver.py tests/unit/test_schemas.py tests/test_deterministic_memory_layer.py
  • uv run --extra dev pytest tests/unit/test_schemas.py tests/test_deterministic_memory_layer.py -> 12 passed
  • uv run --extra dev pytest -> 44 passed
  • uv run ruff check --select F401 src/schemas/code.py src/agents/judge.py src/pipelines/ingest.py src/pipelines/weaver.py tests/unit/test_schemas.py tests/test_deterministic_memory_layer.py
  • git diff --check

/claim #141

hunterbastian avatar May 15 '26 01:05 hunterbastian

Pushed a follow-up in 0345c53 addressing the Gemini review notes.\n\nWhat changed:\n- deduplicates incoming code annotations by deterministic annotation key before lookup;\n- deduplicates incoming snippets by normalized snippet hash before lookup;\n- runs the resulting metadata lookups concurrently with asyncio.gather;\n- added regression tests for duplicate code/snippet extractions in one deterministic judge batch.\n\nVerification rerun locally:\n- python3 -m compileall src/agents/judge.py tests/test_deterministic_memory_layer.py\n- git diff --check\n- uv run --extra dev pytest tests/test_deterministic_memory_layer.py tests/unit/test_schemas.py -> 14 passed\n- uv run --extra dev pytest -> 46 passed\n- uv run ruff check --select F401 src/agents/judge.py tests/test_deterministic_memory_layer.py

hunterbastian avatar May 15 '26 01:05 hunterbastian

Pushed follow-up in f51930f for the requested identity fixes.\n\nWhat changed:\n- code_annotation_identity_key() now includes both target_file and target_symbol, so same-symbol annotations in different files no longer collide.\n- snippet_identity_hash() now hashes normalized code text without lowercasing or collapsing internal whitespace when a code snippet is present.\n- Added regression coverage for both collision cases and updated the deterministic weaver expectation to the new annotation key shape.\n\nVerification rerun locally:\n- .venv/bin/python -m pytest tests/unit/test_schemas.py -q -> 6 passed\n- .venv/bin/python -m pytest tests/test_deterministic_memory_layer.py -q -> 10 passed\n- .venv/bin/python -m pytest -q -> 48 passed\n- git diff --check -> passed\n\nThe PR Labeler check is also green on the new head.

hunterbastian avatar May 16 '26 18:05 hunterbastian

@hunterbastian Please have a discussion on the issue #141 so that we can discuss the approach first and then you can implement in the PR.

ishaanxgupta avatar May 19 '26 05:05 ishaanxgupta

Greptile Summary

This PR introduces deterministic identity metadata for code annotations and personal snippets, letting XMem skip LLM judgment calls when re-encountering the same code or snippet across sessions. Parsing, hashing, and Pinecone metadata construction are centralized in src/schemas/code.py, the judge routes CODE and SNIPPET domains through new deterministic paths, and the weaver uses the shared helpers instead of inline dicts.

  • snippet_identity_hash uses the normalized code text (case-preserved, trailing-whitespace stripped) as the primary identity signal, falling back to a case-insensitive description+language hash when no code is present — correctly handling cross-session duplicates like the \ -vs-\\\ encoding difference between storage and ingestion.
  • code_annotation_content_hash identifies annotation changes by hashing identity key + severity + content; currently uses the case-insensitive stable_hash, which would suppress an UPDATE when only the annotation body casing changes.
  • ingest.py creates ephemeral JudgeAgent instances scoped to the right vector store for each domain, and correctly binds weaver.snippet_vector_store before both the judge and the weaver execute.

Confidence Score: 3/5

The new deterministic paths work correctly for the happy path, but a case-only change to a code annotation's body text would be silently swallowed as a no-op rather than written as an update.

The code_annotation_content_hash function hashes the annotation content through stable_hash, which collapses casing. Any annotation whose body changes purely in letter-case (e.g. correcting identifier casing in free-text, changing 'NULL' to 'null') will produce the identical hash before and after the change. The deterministic judge will classify the incoming item as NOOP and the stored annotation will remain stale. All other logic — the snippet hash path, the ingest wiring, the weaver metadata helpers, and the test coverage — looks correct.

src/schemas/code.py — specifically code_annotation_content_hash and its use of stable_hash for the content field.

Important Files Changed

Filename Overview
src/schemas/code.py Adds deterministic identity helpers (parse, hash, metadata builders) for code annotations and snippets; code_annotation_content_hash uses the case-insensitive stable_hash, which can silently suppress updates when only annotation body casing changes.
src/agents/judge.py Routes CODE and SNIPPET domains through new deterministic paths; _lookup_metadata_match correctly uses asyncio.to_thread matching the existing profile pattern, but has no fallback when search_by_metadata is unavailable.
src/pipelines/ingest.py Replaces LLM judge calls with ephemeral deterministic JudgeAgent instances for code and snippet domains; correctly binds snippet_vector_store before both the judge and the weaver now.
src/pipelines/weaver.py Replaces inline metadata dicts and local parser functions with schema helpers; embedding text for snippets is now richer (description + language + tags) instead of bare description.
tests/test_deterministic_memory_layer.py Adds four integration tests covering cross-session snippet dedup, batch dedup, metadata persistence, and code annotation update detection; FakeVectorStore correctly models metadata equality search.
tests/unit/test_schemas.py Adds unit tests for all new schema helpers; verifies hash stability, identity key format, and metadata field values.

Sequence Diagram

sequenceDiagram
    participant Ingest as IngestPipeline
    participant Judge as JudgeAgent (ephemeral)
    participant Schema as schemas/code.py
    participant Store as VectorStore (Pinecone)
    participant Weaver as Weaver

    Ingest->>Judge: "arun_deterministic({domain: CODE/SNIPPET, new_items, user_id})"
    Judge->>Schema: code_annotation_fields_from_storage_content(content)
    Schema-->>Judge: fields dict
    Judge->>Schema: code_annotation_identity_key(fields) / snippet_identity_hash(fields)
    Schema-->>Judge: identity key / hash
    Judge->>Store: "search_by_metadata({user_id, domain, annotation_key/snippet_hash})"
    Store-->>Judge: SearchResult or None
    alt No match found
        Judge-->>Ingest: Operation(ADD)
    else Match found, hash unchanged
        Judge-->>Ingest: Operation(NOOP)
    else Match found, hash changed (code only)
        Judge-->>Ingest: Operation(UPDATE)
    end
    Ingest->>Weaver: execute(judge_result, domain, user_id)
    Weaver->>Schema: code_annotation_pinecone_metadata / snippet_pinecone_metadata
    Schema-->>Weaver: metadata dict (with annotation_key/annotation_hash or snippet_hash)
    Weaver->>Store: add / update with enriched metadata

Fix All in Cursor Fix All in Codex Fix All in Claude Code

Reviews (1): Last reviewed commit: "Fix deterministic code identity collisio..." | Re-trigger Greptile

greptile-apps avatar May 23 '26 09:05 greptile-apps

hi @hunterbastian please have a look on greptile suggestions and merge conflicts

ishaanxgupta avatar May 27 '26 05:05 ishaanxgupta