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>
This commit is contained in:
committed by
GitHub
parent
4631d6a7db
commit
1b1854e5ae
+1
-1
@@ -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}")
|
||||
|
||||
|
||||
+21
-9
@@ -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")
|
||||
|
||||
@@ -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 ───────────────────────────────────────────────────
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user