Skip to content

fix(kg): port source_drawer_id provenance with schema migration (#1314)#6

Merged
quangdang46 merged 1 commit into
mainfrom
devin/1778514268-kg-source-drawer-id
May 12, 2026
Merged

fix(kg): port source_drawer_id provenance with schema migration (#1314)#6
quangdang46 merged 1 commit into
mainfrom
devin/1778514268-kg-source-drawer-id

Conversation

@quangdang46
Copy link
Copy Markdown
Owner

Summary

Follow-up to #5. Ports the schema-changing part of upstream e4e25ed (milla-jovovich/mempalace#1314, RFC 002 §5.5) that the previous PR explicitly deferred because it requires an ALTER TABLE on the triples table. With this PR, the gap entry for source_drawer_id in port.txt is now resolved.

What changed

  • Schema: triples now declares source_drawer_id TEXT and adapter_name TEXT in the canonical CREATE TABLE. For palaces created before these columns existed, a new KnowledgeGraph::migrate_schema introspects PRAGMA table_info(triples) and only issues ALTER TABLE ADD COLUMN when the column is missing — mirroring the upstream _migrate_schema no-op-on-new-installs contract. Fresh palaces pay no cost; legacy palaces are migrated in-place on open().
  • API: KnowledgeGraph::add_triple gains two trailing Option<&str> kwargs (source_drawer_id, adapter_name), defaulting to None so all existing call sites stay source-compatible. Triple and EntityQueryResult expose source_drawer_id; Triple also exposes adapter_name. New fields use #[serde(default, skip_serializing_if = "Option::is_none")] so existing JSON consumers stay backward compatible.
  • MCP: tool_kg_add accepts a new source_drawer_id input and forwards it to the KG layer; the tool input schema advertises the property with a description that references RFC 002 §5.5. adapter_name is intentionally not exposed at the MCP boundary — matching upstream, where only adapters set it.
  • Tests: 4 new regression tests (3 unit + 1 MCP integration):
    • knowledge_graph::tests::test_add_triple_persists_source_drawer_and_adapter — round-trip persistence of both new columns.
    • knowledge_graph::tests::test_query_entity_exposes_source_drawer_idquery_entity and timeline surface the drawer id.
    • knowledge_graph::tests::test_migrate_schema_adds_missing_provenance_columns — creates a legacy triples table without the new columns and verifies KnowledgeGraph::open migrates it and that subsequent writes use the migrated columns.
    • mcp_server::tests::test_kg_add_forwards_source_drawer_id — mirrors upstream test_kg_add_forwards_source_provenance; reads the raw row to confirm the drawer id reaches SQLite via the MCP boundary.
  • port.txt: removes the source_drawer_id entry from "Remaining gaps" and appends a follow-up sync entry documenting what was ported.

Review & Testing Checklist for Human

  • Verify the in-place migration on an existing palace. Open a palace created with this repo before this PR (or with the upstream Python version) and confirm that PRAGMA table_info(triples) shows the new source_drawer_id and adapter_name columns after KnowledgeGraph::open runs. The migration is idempotent — running open() twice is a no-op on the second call.
  • Confirm backward compatibility of the JSON shape. Existing consumers of Triple / EntityQueryResult JSON should still see no breaking change because the new fields are Option<String> with skip_serializing_if = "Option::is_none". Spot-check mempalace_kg_query output for facts written without the new fields.
  • Check the MCP mempalace_kg_add schemasource_drawer_id should be visible as an optional string property in tools/list output.

Test plan to verify end-to-end:

  1. cargo build --workspace
  2. cargo test --workspace — expect 408 passed; 0 failed (4 new regression tests in mempalace-core).
  3. cargo clippy --workspace --all-targets -- -D warnings — expect clean.
  4. cargo fmt --all -- --check — expect clean.
  5. Smoke-test the MCP boundary:
    • Call mempalace_kg_add with source_drawer_id="drawer_xyz", then query the triples row directly via SQLite and verify the column was persisted.
    • Query the same fact via mempalace_kg_query and confirm source_drawer_id appears in the response.

Notes

  • adapter_name is not exposed at the MCP boundary in this PR. Upstream only sets adapter_name from inside adapter code that advertises supports_kg_triples; exposing it at the MCP boundary would let arbitrary callers spoof adapter identity. The column exists at the storage layer and is plumbed through Triple, but the MCP tool only forwards source_drawer_id.
  • The internal add_triple test call sites were padded mechanically (all existing calls used 8 args; the two new kwargs default to None). No test semantics changed.
  • Diff size: 3 files, +331/-16 lines. No new dependencies.

Follow-up to PR #5. Ports the schema-changing part of upstream e4e25ed
(MemPalace/mempalace#1314, RFC 002 §5.5) that was explicitly deferred
because it requires an ALTER TABLE on the triples table.

- Add source_drawer_id TEXT and adapter_name TEXT columns to the canonical
  triples CREATE TABLE.
- Add KnowledgeGraph::migrate_schema: introspect PRAGMA table_info(triples)
  and ALTER TABLE ADD COLUMN only when the columns are missing, so legacy
  palaces are migrated in-place on open() and fresh installs stay no-op.
- Extend KnowledgeGraph::add_triple with source_drawer_id and adapter_name
  Option<&str> kwargs; existing test call sites pass None.
- Surface source_drawer_id on Triple and EntityQueryResult; surface
  adapter_name on Triple so callers can navigate back to the drawer that
  produced the fact.
- Wire tool_kg_add at the MCP boundary to accept source_drawer_id and
  forward it; advertise it in the tool input schema. adapter_name remains
  internal, matching upstream.
- Regression tests: unit tests for round-trip persistence, query exposure,
  and the legacy-schema migration path, plus an MCP-level test that the
  drawer id reaches SQLite via tool_kg_add.
- port.txt: drop the now-resolved 'source_drawer_id' gap and append a new
  sync update entry.
@quangdang46 quangdang46 merged commit d738d18 into main May 12, 2026
4 checks passed
@quangdang46 quangdang46 deleted the devin/1778514268-kg-source-drawer-id branch May 12, 2026 05:26
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