Skip to content

feat(palace): add tag/untag/link/list_tags to MemoryProvider (mp-migration 2/8)#20

Merged
quangdang46 merged 1 commit into
mainfrom
feat/mp-pr2-tag-link
Jun 3, 2026
Merged

feat(palace): add tag/untag/link/list_tags to MemoryProvider (mp-migration 2/8)#20
quangdang46 merged 1 commit into
mainfrom
feat/mp-pr2-tag-link

Conversation

@quangdang46
Copy link
Copy Markdown
Owner

Summary

Add 4 graph-mutation methods to MemoryProvider covering jcode's tag/link surface:

Method Maps to jcode Storage
tag(&id, tag) MemoryManager::tag_memory metadata["tags"] + metadata["tag:<n>"] = true
untag(&id, tag) MemoryManager::untag_memory removes from metadata["tags"] and metadata["tag:<n>"]
link(&from, &to, w) MemoryManager::link_memories metadata["links"] = Vec<{target, weight}>
list_tags() closest: graph_stats.1 aggregates metadata["tags"] across drawers

Motivation

Required for the jcode → mempalace Mode C library migration (mp-migration 2/8). jcode's MemoryManager calls tag_memory and link_memories pervasively (memory.rs:1657, 1676) and the tool's tag and link actions (tool/memory.rs:328, 355) call them too. Without trait methods, the adapter would have to either re-implement or expose raw metadata writes.

This is PR 2/8 in the migration series. Includes a cherry-pick of PR #1 (boost/decay/reinforce/supersede/set_metadata) so this branch has all 9 mutation methods in one place. The two commits are independent in the series and can be reviewed/rebased independently.

Change

crates/core/src/palace.rs — adds 4 default methods to MemoryProvider trait (after set_metadata, before fingerprint):

async fn tag(&self, id: &DrawerId, tag: &str) -> anyhow::Result<()>
where Self: Sized;
async fn untag(&self, id: &DrawerId, tag: &str) -> anyhow::Result<()>
where Self: Sized;
async fn link(&self, from: &DrawerId, to: &DrawerId, weight: f32) -> anyhow::Result<()>
where Self: Sized;
async fn list_tags(&self) -> anyhow::Result<Vec<(String, usize)>>
where Self: Sized;

All four have the where Self: Sized bound required by the default-implementation helper.

The defaults use the metadata path so they don't depend on the KG being wired into Palace. The metadata shapes (Vec<String>, Vec<{target, weight}>) are designed to match the eventual KG triples:

  • tagkg.add_triple(subject: drawer_id, predicate: "has_tag", object: tag)
  • linkkg.add_triple(subject: from, predicate: "relates_to", object: to, confidence: Some(weight))
  • list_tagskg.query_relationship(predicate: "has_tag") then group-by

Implementations that have a wired KG should override and use the KG API directly. The metadata path stays as a fallback for the jcode adapter's get-or-default usage.

Performance

  • tag / untag / link: O(n) via the shared default_mutate_drawer helper (get_drawers + store.upsert).
  • list_tags: O(n × m) where n is drawer count and m is avg tags per drawer. The KG-backed override can be O(1).

Both are fine for the embedvec tier (≤5 k drawers). Override for larger palaces.

Test plan

  • cargo test -p mempalace-core --lib1127 passed, 0 failed (251s)
  • cargo check -p mempalace-core clean
  • cargo fmt --check -p mempalace-core clean
  • CI green on ubuntu/macos/windows

Series status

# Title Status
6 docs(palace): document Embedder reuse on search_with_embedding merged #16
7 feat(palace): add tags/trust/access_count/reinforcements/superseded_by to Drawer open #17
8 feat(palace): add MemoryProvider::graph_stats_legacy (jcode shape) open #18
1 feat(palace): add boost/decay/reinforce/supersede/set_metadata to MemoryProvider open #19
2 feat(palace): add tag/untag/link/list_tags to MemoryProvider this PR
4 feat(palace): add MemoryProvider::recent for retention-ranked recall next
3 feat(palace): add MemoryScope::All and Wing/Room variants next
5 feat(palace): add ActivityEvent sink on PalaceBuilder next

🤖 Generated with Claude Code

jcode's MemoryManager exposes 4 graph-mutation methods that today
have no equivalent on the MemoryProvider trait:

  - tag(&id, tag)        — MemoryManager::tag_memory
  - untag(&id, tag)      — MemoryManager::untag_memory
  - link(&from, &to, w)  — MemoryManager::link_memories (with weight)
  - list_tags()          — closest jcode equiv: graph_stats.1

Add them as default-implemented methods on MemoryProvider. The
defaults use the metadata path (so this PR does NOT depend on the
KG being wired into Palace), mirroring the values in shapes that
match the eventual KG triples:

  tag    → metadata["tags"] (Vec<String>) + metadata["tag:<n>"] = true
  untag  → metadata["tags"] (minus removed) + remove metadata["tag:<n>"]
  link   → metadata["links"] = Vec<{target, weight}>
  list   → aggregate metadata["tags"] across get_drawers(None, None)

Implementations that have a wired KG (mp-020 sub-task) should
override and use KnowledgeGraph::add_triple / query_relationship
directly:

  tag     → add_triple { subject: drawer_id, predicate: "has_tag",
                          object: tag, current: true }
  link    → add_triple { subject: from,     predicate: "relates_to",
                          object: to, confidence: Some(weight) }
  list    → query_relationship(predicate="has_tag") then group-by

Includes a cherry-pick of PR #1 (the boost/decay/reinforce/
supersede/set_metadata additions) so the trait has all 9 mutation
methods together. Both can be reviewed independently because the
cherry-pick commit is its own clean diff in the series.

This is PR 2/8 in the jcode → mempalace Mode C library migration
series.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@quangdang46 quangdang46 force-pushed the feat/mp-pr2-tag-link branch from 49980ff to 6af3f0b Compare June 3, 2026 15:00
@quangdang46 quangdang46 merged commit d842013 into main Jun 3, 2026
@quangdang46 quangdang46 deleted the feat/mp-pr2-tag-link branch June 3, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant