From 1b1854e5ae0b0a120db02b4cf44479a580e04f82 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 24 Apr 2026 05:25:34 +0000 Subject: [PATCH] fix(init): address registry review feedback Agent-Logs-Url: https://github.com/MemPalace/mempalace/sessions/76794fde-2383-4674-ab36-f89ad803eeb2 Co-authored-by: igorls <4753812+igorls@users.noreply.github.com> --- mempalace/cli.py | 2 +- mempalace/miner.py | 30 +++++++++++++++++++-------- tests/test_known_entities_registry.py | 7 +++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/mempalace/cli.py b/mempalace/cli.py index ec185c3..714c64c 100644 --- a/mempalace/cli.py +++ b/mempalace/cli.py @@ -125,7 +125,7 @@ def cmd_init(args): # global registry the miner reads at mine time. if confirmed["people"] or confirmed["projects"]: entities_path = Path(args.dir).expanduser().resolve() / "entities.json" - with open(entities_path, "w") as f: + with open(entities_path, "w", encoding="utf-8") as f: json.dump(confirmed, f, indent=2, ensure_ascii=False) print(f" Entities saved: {entities_path}") diff --git a/mempalace/miner.py b/mempalace/miner.py index 61b95f1..9e8ff5e 100644 --- a/mempalace/miner.py +++ b/mempalace/miner.py @@ -507,6 +507,12 @@ def add_to_known_entities(entities_by_category: dict) -> str: except (_json.JSONDecodeError, OSError): existing = {} + def _coerce_name(value): + if not value: + return None + name = str(value) + return name if name else None + for category, names in entities_by_category.items(): if not isinstance(names, list) or not names: continue @@ -514,27 +520,33 @@ def add_to_known_entities(entities_by_category: dict) -> str: if isinstance(current, list): seen_lower = {str(n).lower() for n in current} for n in names: - if not n: + name = _coerce_name(n) + if not name: continue - if str(n).lower() not in seen_lower: - current.append(n) - seen_lower.add(str(n).lower()) + if name.lower() not in seen_lower: + current.append(name) + seen_lower.add(name.lower()) elif isinstance(current, dict): + seen_lower = {str(name).lower() for name in current} for n in names: - if n and n not in current: - current[n] = None + name = _coerce_name(n) + if not name or name.lower() in seen_lower: + continue + current[name] = None + seen_lower.add(name.lower()) else: # Missing or unrecognized shape — seed as a fresh list, deduped seen: set = set() ordered: list = [] for n in names: - if not n: + name = _coerce_name(n) + if not name: continue - key = str(n).lower() + key = name.lower() if key in seen: continue seen.add(key) - ordered.append(n) + ordered.append(name) existing[category] = ordered registry_path.write_text(_json.dumps(existing, indent=2, ensure_ascii=False), encoding="utf-8") diff --git a/tests/test_known_entities_registry.py b/tests/test_known_entities_registry.py index cd558e3..300cfb6 100644 --- a/tests/test_known_entities_registry.py +++ b/tests/test_known_entities_registry.py @@ -114,6 +114,13 @@ def test_dict_format_existing_category_gets_new_keys(temp_registry): assert data["people"]["Carol"] is None +def test_dict_format_dedupes_case_insensitively_and_stringifies_new_names(temp_registry): + temp_registry.write_text(json.dumps({"people": {"Alice": "ALC"}})) + miner.add_to_known_entities({"people": ["alice", 123]}) + data = json.loads(temp_registry.read_text()) + assert data["people"] == {"Alice": "ALC", "123": None} + + # ── error tolerance ───────────────────────────────────────────────────