diff --git a/CHANGELOG.md b/CHANGELOG.md index c6e0962..cf7d041 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - **Mining and configuration correctness.** Oversized files produce a visible stderr warning, `~` in configured palace paths expands consistently, wing slugs handle special characters, and Windows background daemon / synchronous hook mines use `CREATE_NO_WINDOW`. (#923, #1852, #1857, #1863, #1865) +- **`mempalace init` handles non-ASCII `.gitignore` files on Windows.** The project-file ignore guard now reads and appends UTF-8 explicitly instead of relying on locale defaults such as GBK. (#1648) + +- **L1 wake-up surfaces the latest moments.** Drawers with equal importance are now ordered by `filed_at` recency rather than insertion order, so startup context prefers recent memories instead of the oldest ones. (#1630) + - **Backend detection requires a SQLite magic header** before classifying a target as Chroma or `sqlite_exact`, preventing unrelated files from being mistaken for a palace. (#1893, #1896) ### Documentation diff --git a/mempalace/cli.py b/mempalace/cli.py index a9ef289..f7d3b95 100644 --- a/mempalace/cli.py +++ b/mempalace/cli.py @@ -259,14 +259,16 @@ def _ensure_mempalace_files_gitignored(project_dir) -> bool: if not (project_path / ".git").exists(): return False gitignore = project_path / ".gitignore" - existing = gitignore.read_text() if gitignore.exists() else "" + # Force UTF-8: Windows defaults to GBK and chokes on non-ASCII .gitignore + # comments, killing auto-init even though the file is valid UTF-8. + existing = gitignore.read_text(encoding="utf-8", errors="replace") if gitignore.exists() else "" existing_lines = {line.strip() for line in existing.splitlines()} missing = [p for p in _MEMPALACE_PROJECT_FILES if p not in existing_lines] if not missing: return False prefix = "" if not existing or existing.endswith("\n") else "\n" block = prefix + "\n# MemPalace per-project files (issue #185)\n" + "\n".join(missing) + "\n" - with open(gitignore, "a") as f: + with open(gitignore, "a", encoding="utf-8") as f: f.write(block) print(f" Added {', '.join(missing)} to {gitignore.name}") return True diff --git a/mempalace/layers.py b/mempalace/layers.py index 1d6ceeb..b6135a8 100644 --- a/mempalace/layers.py +++ b/mempalace/layers.py @@ -126,12 +126,21 @@ class Layer1: if not docs: return "## L1 — No memories yet." - # Score each drawer: prefer high importance, recent filing + # Score each drawer: prefer high importance, then most-recent filing. + # NOTE: the ingest pipeline (miner, convo_miner, diary, add_drawer) + # records provenance metadata — wing/room/source/chunk/filed_at — but + # never an evaluative importance/weight field. So `importance` is + # absent on virtually every drawer and ties at the default, which used + # to collapse the sort to insertion order (oldest first). `filed_at` + # is present on every drawer, so it is the *effective* ordering signal: + # newest first. This keeps importance as the primary key for the day a + # scoring pass populates it, while making the "recent filing" half of + # the promise true today with data we already have. scored = [] for doc, meta in zip(docs, metas): meta = meta or {} doc = doc or "" - importance = 3 + importance = 3.0 # Try multiple metadata keys that might carry weight info for key in ("importance", "emotional_weight", "weight"): val = meta.get(key) @@ -141,11 +150,15 @@ class Layer1: except (ValueError, TypeError): pass break - scored.append((importance, meta, doc)) + # filed_at is an ISO-8601 string; ISO strings sort lexicographically + # in chronological order. Coerce to str so a missing/odd value sorts + # oldest rather than raising during the comparison. + recency = str(meta.get("filed_at") or "") + scored.append((importance, recency, meta, doc)) - # Sort by importance descending, take top N - scored.sort(key=lambda x: x[0], reverse=True) - top = scored[: self.MAX_DRAWERS] + # Sort by importance desc, then recency (filed_at) desc; take top N. + scored.sort(key=lambda x: (x[0], x[1]), reverse=True) + top = [(imp, meta, doc) for imp, _recency, meta, doc in scored[: self.MAX_DRAWERS]] # Group by room for readability by_room = defaultdict(list) diff --git a/tests/test_init_gitignore_protection.py b/tests/test_init_gitignore_protection.py index ab22ea3..6fb7499 100644 --- a/tests/test_init_gitignore_protection.py +++ b/tests/test_init_gitignore_protection.py @@ -7,6 +7,7 @@ the two filenames to `.gitignore` when `` is a git repository. """ from pathlib import Path +from unittest.mock import mock_open, patch from mempalace.cli import _ensure_mempalace_files_gitignored @@ -60,3 +61,20 @@ def test_handles_gitignore_without_trailing_newline(tmp_path): assert "dist\n" in contents assert "mempalace.yaml" in contents assert "entities.json" in contents + + +def test_gitignore_io_pins_utf8_and_defensive_decode(tmp_path): + """Regression for #1648: never fall back to the Windows locale codec.""" + _git_init(tmp_path) + gitignore = tmp_path / ".gitignore" + gitignore.write_text("# café\n", encoding="utf-8") + append_handle = mock_open() + + with ( + patch.object(Path, "read_text", return_value="# café\n") as read_text, + patch("builtins.open", append_handle), + ): + assert _ensure_mempalace_files_gitignored(tmp_path) is True + + read_text.assert_called_once_with(encoding="utf-8", errors="replace") + append_handle.assert_called_once_with(gitignore, "a", encoding="utf-8") diff --git a/tests/test_layers.py b/tests/test_layers.py index d4c54ce..56263a3 100644 --- a/tests/test_layers.py +++ b/tests/test_layers.py @@ -201,6 +201,27 @@ def test_layer1_importance_from_various_keys(): assert "ESSENTIAL STORY" in result +def test_layer1_breaks_importance_ties_by_filed_at_recency(): + """Equal-importance drawers surface newest-first instead of insertion order.""" + docs = ["oldest memory", "newest memory", "middle memory"] + metas = [ + {"room": "moments", "importance": 3, "filed_at": "2026-01-01T00:00:00Z"}, + {"room": "moments", "importance": 3, "filed_at": "2026-03-01T00:00:00Z"}, + {"room": "moments", "importance": 3, "filed_at": "2026-02-01T00:00:00Z"}, + ] + mock_col = _mock_chromadb_for_layer(docs, metas) + + with ( + patch("mempalace.layers.MempalaceConfig") as mock_cfg, + patch("mempalace.layers._get_collection", return_value=mock_col), + ): + mock_cfg.return_value.palace_path = "/fake" + result = Layer1(palace_path="/fake").generate() + + assert result.index("newest memory") < result.index("middle memory") + assert result.index("middle memory") < result.index("oldest memory") + + def test_layer1_batch_exception_breaks(): """If col.get raises on a batch, loop breaks gracefully.""" mock_col = MagicMock()