Skip to content

fix(memory): enforce identity singularity across scopes#133

Merged
torrmal merged 6 commits intomainfrom
fix/identity-scope-singular
Apr 25, 2026
Merged

fix(memory): enforce identity singularity across scopes#133
torrmal merged 6 commits intomainfrom
fix/identity-scope-singular

Conversation

@alecantu7
Copy link
Copy Markdown
Contributor

Summary

  • Cortex.encode() routed profile engrams by declared scope, so memorize(kind=profile) with the default scope=project wrote to project/profile.md — which no read path ever loads. The facts were silently lost.
  • Force kind=profile to global_hc regardless of declared scope, matching the existing build_memory_context() comment "identity is singular".
  • One-time startup migration in Cortex.__init__ merges any orphaned project identity into global and clears the project copy, so existing users recover data already written to the wrong place.
  • Adds clear_identity() to Hippocampus (and to HippocampusProtocol) to support the migration without reaching into private paths.

Notes

Test plan

  • pytest tests/test_cortex.py tests/test_hippocampus.py — 55 passed
  • New test: kind=profile, scope=project routes to global, never writes project profile.md
  • New test: orphaned project identity is migrated into global on init, project file removed
  • New test: migration is a no-op when project identity is empty

🤖 Generated with Claude Code

ea-rus and others added 3 commits April 21, 2026 18:20
`encode()` routed profile engrams by declared scope, so `memorize(kind=profile)`
with the default `scope=project` landed in `project/profile.md` — which no
read path ever loads, silently dropping the data. Force `kind=profile` to
`global_hc` regardless of scope, and add a one-time startup migration that
merges any orphaned project identity into global then clears the project copy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@entelligence-ai-pr-reviews
Copy link
Copy Markdown

entelligence-ai-pr-reviews Bot commented Apr 24, 2026

EntelligenceAI PR Summary

Fixes identity migration in cortex.py to be non-destructive by skipping orphaned project entries whose keys already exist in the global identity store, with test coverage validating conflict resolution behavior.

  • anton/core/memory/cortex.py: Builds a set of existing global keys before migration; only migrates project-scoped facts with keys absent from global scope; guards rewrite_identity call behind a non-empty check; preserves project_hc.clear_identity() call
  • tests/test_cortex.py: New test test_migration_does_not_overwrite_fresh_global_entries verifies global-wins-on-conflict semantics, successful migration of non-conflicting keys (e.g., TZ: PST), and project profile removal after migration

Confidence Score: 4/5 - Mostly Safe

Safe to merge — the fix in cortex.py correctly addresses the identity singularity problem by building a existing_global_keys set before migration and gating the rewrite_identity call behind a non-empty check, preventing destructive overwrites of global-scoped identity entries. The logic flow — collect existing keys, filter project-scoped candidates by absence, guard the rewrite, then unconditionally clear project identity — is sound and the test coverage in tests/test_cortex.py validates the conflict resolution path. The only minor concern is that only 1 of 2 changed files received automated review coverage, meaning cortex.py itself was not fully analyzed by the static pass, though the described logic appears straightforward and low-risk.

Key Findings:

  • The pre-migration existing_global_keys set construction in cortex.py correctly prevents key collisions by filtering candidates with key not in existing_global_keys before passing to rewrite_identity, which is the core correctness requirement of this fix.
  • Guarding rewrite_identity behind a non-empty check prevents a potentially no-op or error-prone call when all project keys conflict with global keys — this is a defensive improvement over the prior behavior.
  • The project_hc.clear_identity() call is preserved unconditionally, ensuring project-scoped identity is always cleaned up regardless of how many entries were migrated — this matches the intended non-destructive but still-clearing semantics.
  • Only tests/test_cortex.py was covered by automated review; anton/core/memory/cortex.py itself did not receive automated analysis, leaving a small gap in static review confidence, though the described changes are straightforward.
Files requiring special attention
  • anton/core/memory/cortex.py
  • tests/test_cortex.py

Copy link
Copy Markdown

@entelligence-ai-pr-reviews entelligence-ai-pr-reviews Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes incorrect scoping of profile/identity engrams and adds migration logic for orphaned data, along with a method to clear stored identity.

  • cortex.py: encode() now unconditionally routes kind == 'profile' engrams to global_hc; __init__ detects and migrates orphaned project-scoped identity entries via rewrite_identity() and clears them from project_hc
  • hippocampus.py: New clear_identity() method deletes _profile_path if present
  • base.py: New abstract method clear_identity() added to enforce interface contract
  • tests/test_cortex.py: Added tests for correct global routing of profile engrams and TestOrphanedIdentityMigration class covering both migration and no-op scenarios

Comment thread anton/core/memory/cortex.py
alecantu7 and others added 2 commits April 23, 2026 20:23
The previous migration called `global_hc.rewrite_identity(orphaned)` which
treats new entries as authoritative and strips existing global entries
sharing the same key prefix. A stale `Name: Alec` in project scope could
silently overwrite a fresh `Name: Alejandro` in global, corrupting data
the user had explicitly corrected.

Global wins on key conflicts now — only orphaned keys not already present
globally are imported.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@alecantu7
Copy link
Copy Markdown
Contributor Author

@Entelligence review

@entelligence-ai-pr-reviews
Copy link
Copy Markdown

Starting fresh pr review. This may take a few minutes

@mindsdb mindsdb deleted a comment from entelligence-ai-pr-reviews Bot Apr 24, 2026
@entelligence-ai-pr-reviews
Copy link
Copy Markdown

New pr review completed! Check the pr for comments.

… fix/identity-scope-singular"

This reverts commit 3e1d119, reversing
changes made to 4cb6b2c.
@alecantu7
Copy link
Copy Markdown
Contributor Author

@Entelligence review

@entelligence-ai-pr-reviews
Copy link
Copy Markdown

Starting fresh pr review. This may take a few minutes

@entelligence-ai-pr-reviews
Copy link
Copy Markdown

New pr review completed! Check the pr for comments.

@alecantu7 alecantu7 requested a review from tino097 April 24, 2026 14:34
@torrmal torrmal added this pull request to the merge queue Apr 25, 2026
Merged via the queue into main with commit 326dfe2 Apr 25, 2026
6 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants