[Feature]: Add type-aware entity canonicalization and lifecycle controls
Issue Origin
Observed or reproduced in a real environment
Problem Statement
The entities memory schema defines compact, stable cards for real-world entities, but session extraction currently produces a heterogeneous and duplicate entity namespace:
- The same project can be written under several categories and languages, such as
project/cc-connect.md,开发工具/cc-connect.md,项目/cc-connect.md, andsoftware_project/cc_connect.md. - Project entities accumulate dated deployments, PRs, commits, UI iterations, versions, and temporary paths, growing into hundreds of lines instead of a compact stable card.
- Configuration files, code commits, deployment snapshots, and one-off feature work are stored as entities even though they have different lifecycles from durable objects.
categoryandnameare immutable schema fields, so a later extraction cannot rename a duplicate into an existing canonical entity or create an alias.
In a real memory cleanup, software_project/vikingdb_fe.md had grown to 838 lines of dated implementation history, while cc-connect existed as four conflicting entity cards. This degrades retrieval: a query can return stale project history or a duplicate category card instead of the canonical object.
This is complementary to #3007 (general dedup/consolidation), #3006 (verbatim oversized memories), and #2899 (secret scrubbing). Those issues do not provide entity-type classification or canonical identity resolution at write time.
Proposed Solution
Add a type-aware entity write path:
- Canonical identity resolution before persistence, using normalized names plus aliases across category/language variants.
- Category allowlist and routing for entities. Route dated outcomes to
events; reusable procedures toexperiences; configuration snapshots to a dedicated config type or short-lived operational records. - Card-size and freshness policy for entities: retain stable facts, cap or summarize dated history, and link to events instead of appending logs.
- Conflict handling when a candidate conflicts with an immutable category/name: merge into the canonical entity, create an explicit alias, or emit a reviewable operation instead of silently creating another card.
- CLI/API audit mode to report duplicate canonical candidates, mixed categories, oversized cards, and stale dated facts before mutation.
Alternatives Considered
- Rely only on post-hoc semantic deduplication (#3007): it cannot reliably distinguish a project from a deployment, commit, or configuration snapshot after all are already indexed.
- Relax all immutable fields: this risks accidental rewrites and loses entity identity guarantees. Canonical aliases and reviewable merges are safer.
- Keep all dated history in entity cards: this preserves provenance but makes the primary entity noisy; event links preserve provenance without polluting retrieval.
Feature Area
Session Management
Use Case
Users who let OpenViking continuously extract long-running engineering sessions need project/person/tool entities to remain compact, stable, and uniquely retrievable. They should not need manual scripts or repeated human cleanup to collapse multilingual category drift and move dated operational history out of entity cards.
Additional Context
Suggested acceptance criteria:
- Repeated references to the same project across
project,software_project, and localized category names resolve to one canonical entity with aliases. - A dated commit/deployment/PR does not enlarge the project entity; it is routed or linked as an event/specialized record.
- Entity cards follow the configured compact card shape and remain bounded after repeated updates.
- Audit output explains every proposed merge, alias, routing decision, and skipped mutation.
- Existing entity data can be migrated with preview and rollback/export support.
Contribution
- [x] I am willing to contribute to implementing this feature
I reproduced a narrow write-time identity bug on current main (87d5142e0666821180ea969a1d135a93497e3b4a): the built-in entity filename template renders concept/smart.md and CONCEPT/SMART.md for case-only variants. Because URI generation preserves model casing, asynchronous commits can route the same logical entity to distinct files before the existing streaming merge/write lock can converge them.
I am preparing a small first slice for this issue: lowercase only the built-in entity category and name path segments in both normal and experimental schemas, with a regression test proving smart / SMART resolve to the same path. Display content and stored immutable fields remain unchanged.
This intentionally does not mutate existing mixed-case files. Legacy consolidation needs the reviewable merge/export/rollback semantics already requested by this issue; automatically picking or deleting a historical winner in the write path could lose independently accumulated facts or links.
Narrow implementation opened as #4070. Exact head: 304fe76363abd77313d49d14a0e6b40fde585aaf.
Scope remains write-time prevention for new case-only entity siblings. Existing mixed-case files are unchanged and remain part of the reviewable migration/audit work in this issue.
Still reproduces on v0.5.0 and current main: the entity path is still {{ category|lower }}/{{ name|lower }}.md, and #4070 only folded case. Numbers from one long-running single-user deployment (coding-agent sessions, mixed Chinese/English), 2026-10-13:
- 2,671 entity cards in 435 category directories; 187 directories hold a single card
- 196 names (after folding case and
-/_/space) exist in 452 files, e.g.openvikingunder软件项目/,software_system/,软件系统/,开源项目_agent框架/;codexundercli_tool/,系统/工具/,software_application/,coding_agent/ - it keeps growing: 349 directories / 123 duplicated names on 2026-09-21
One cause I don't see mentioned yet is that the model usually can't see the existing card. SessionExtractContextProvider prefetch runs one semantic search over all multi-file memory directories (events, entities, cases, …) with search_files(..., limit=5) and returns only URIs (eager_prefetch defaults to false). With thousands of entity and event files, the existing card is rarely among those 5, so the model picks a fresh category, which is a fresh path. When the card is visible, the category still flips with the output language (开发工具/ vs developer_tool/).
A small write-time fix we now run locally (patch, ~50 lines in extract_loop.py, no schema change):
- in
ExtractLoop.resolve_operations, when a new entity URI does not exist, globmemories/entities/**/*.mdonce per loop and look up a card whose file name matches after folding case and-/_/space; - if found, write to that URI instead. The existing
_check_unread_existing_filesrefetch then shows the card to the model, and it merges instead of creating a sibling; - lookup failures keep the original path.
Replaying 15 production archives against the same starting data (json_schema extraction): unpatched, the run created 3 cross-category duplicates (e.g. system_tool/openviking_version.md next to an existing 系统/工具/openviking_version.md); patched, it created none. Per-loop cost is one glob.
This does not cover synonyms with different names (openviking vs openviking_devbox), the existing duplicates (those still need the audit/migration from this issue), or category normalization. If this direction fits the canonical-identity step proposed here, I can open a PR with the tests.
Still reproduces on v0.5.0 and current
main: the entity path is still{{ category|lower }}/{{ name|lower }}.md, and #4070 only folded case. Numbers from one long-running single-user deployment (coding-agent sessions, mixed Chinese/English), 2026-10-13:
- 2,671 entity cards in 435 category directories; 187 directories hold a single card
- 196 names (after folding case and
-/_/space) exist in 452 files, e.g.openvikingunder软件项目/,software_system/,软件系统/,开源项目_agent框架/;codexundercli_tool/,系统/工具/,software_application/,coding_agent/- it keeps growing: 349 directories / 123 duplicated names on 2026-09-21
One cause I don't see mentioned yet is that the model usually can't see the existing card.
SessionExtractContextProviderprefetch runs one semantic search over all multi-file memory directories (events, entities, cases, …) withsearch_files(..., limit=5)and returns only URIs (eager_prefetchdefaults to false). With thousands of entity and event files, the existing card is rarely among those 5, so the model picks a fresh category, which is a fresh path. When the card is visible, the category still flips with the output language (开发工具/vsdeveloper_tool/).A small write-time fix we now run locally (patch, ~50 lines in
extract_loop.py, no schema change):
- in
ExtractLoop.resolve_operations, when a new entity URI does not exist, globmemories/entities/**/*.mdonce per loop and look up a card whose file name matches after folding case and-/_/space;- if found, write to that URI instead. The existing
_check_unread_existing_filesrefetch then shows the card to the model, and it merges instead of creating a sibling;- lookup failures keep the original path.
Replaying 15 production archives against the same starting data (json_schema extraction): unpatched, the run created 3 cross-category duplicates (e.g.
system_tool/openviking_version.mdnext to an existing系统/工具/openviking_version.md); patched, it created none. Per-loop cost is one glob.This does not cover synonyms with different names (
openvikingvsopenviking_devbox), the existing duplicates (those still need the audit/migration from this issue), or category normalization. If this direction fits the canonical-identity step proposed here, I can open a PR with the tests.
Try ov compile --skill memory --to viking://~/memories/entities --instruction xxxxx to compile your memories
Thank you for documenting the four cc-connect cards and for sharing examples from a long-running deployment. On the latest main, you can use Compile's memory skill to consolidate existing entity cards in place. To limit the cleanup to your entities, you can run:
ov compile --skill memory --to 'viking://~/memories/entities' --instruction 'Merge cards that refer to the same project across Chinese and English categories. Preserve aliases and stable facts, and summarize dated implementation details.'
We'll close this issue with that workflow for cleaning up existing cards. Your suggestion to recognize an entity during extraction addresses a separate step. If new sessions keep creating duplicates after cleanup, please feel free to follow up here with a pair of example paths. Thank you again for the report.
Thanks. I tested this against a copy of our store on main af8ae4a, with your instruction verbatim.
- Single run on the full entities root (3,041 cards in 494 directories): finished in 57 s with 0 merges, 0 deletes, and 37 rewrites. The prefetch
ls --recursivegoes throughglob(node_limit=501), and directories count toward that limit, so the model saw 403 of 3,041 files. In this run, 7 rewritten cards got the loop's temporary page id written into the body (e.g. "(page 28)"). - Batched, on our 2026-10-13 pre-cleanup snapshot (2,681 cards, 196 name-folded duplicate groups, i.e. the numbers in my earlier comment).
--tocan only batch by directory, which never puts cross-category pairs together. So I staged batches of ≤480 listing nodes, kept each name-folded group in one batch, and ran 3 full passes (24 runs):- Deletes per pass went 75 → 29 → 20, so it did not converge; duplicate groups went from 196 to 156.
- Of the 181 groups our own merge script merged that day, 39 were merged. Even among groups whose cards sat in one batch that completed, only 33 of 148 were merged.
- 11 of 24 runs failed outright. In each, a single rejected operation left the whole batch unapplied:
Memory rename target already exists…, orCase-only memory URI renames are not portableon legacy mixed-case categories such asAI基础设施/→ai基础设施/. The same operation recurred on every pass. - The merges I spot-checked were mostly correct; a few merged a model card into its paper card (Keye-VL-8B-Preview → its technical report).
Could valid operations still be applied when one is rejected, and could the listing be paginated, or compile accept an explicit candidate list? On new duplicates after cleanup: replaying 15 production archives on main created system_tool/openviking_version.md next to the existing 系统/工具/openviking_version.md. I'm happy to open a separate issue for the extraction-time step with the extract_loop.py patch and tests.