From bacb935bb33bb05ec769c5039fd5691d2ef99b48 Mon Sep 17 00:00:00 2001 From: David F Glidden Date: Sun, 28 Jun 2026 06:19:32 +0200 Subject: [PATCH 1/4] fix(layers): order L1 wake-up by recency so it surfaces the latest moments (#1630) L1's generate() scored drawers by importance/emotional_weight/weight, and the docstring promised "prefer high importance, recent filing". But no ingest path (miner, convo_miner, diary, add_drawer) writes any of those fields, so the sort collapsed to insertion order (oldest first) and recency was never consulted. A scoped `wake-up --wing X` therefore surfaced the *oldest* moments: the opposite of useful. Add filed_at (present on every drawer, ISO-8601, lexically chronological) as the secondary sort key. Importance stays primary for the day a scoring pass populates it; filed_at is the effective signal today, making the "recent filing" half of the promise true with data already present. Co-authored-by: Claude Opus 4.7 (1M context) Co-authored-by: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> --- mempalace/layers.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/mempalace/layers.py b/mempalace/layers.py index 6acf852..3b78fd2 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) From 5cbc37ee9b59c1c5daf5748a555b7bc91ca103b8 Mon Sep 17 00:00:00 2001 From: ALaDingAhmad <166673823+ALaDingAhmad@users.noreply.github.com> Date: Sun, 28 Jun 2026 12:31:19 +0800 Subject: [PATCH 2/4] fix(cli): force UTF-8 when reading/writing .gitignore in init (#1648) On Windows, Path.read_text() and open(path, 'a') use locale encoding (GBK on Chinese-locale systems) before PEP 686 / Python 3.15. A valid UTF-8 .gitignore with non-ASCII comments crashes _ensure_mempalace_files_gitignored() with UnicodeDecodeError, which aborts 'mempalace init' on Windows for any user whose .gitignore contains non-ASCII text. Force encoding='utf-8' on both read and append, with errors='replace' on read as a defensive fallback for legacy mixed-encoding files. Co-authored-by: ALaDingAhmad <16530935@qq.com> Co-authored-by: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> --- mempalace/cli.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mempalace/cli.py b/mempalace/cli.py index 2fbecd5..3043628 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 From a613c921c8a0c81be98918f97ba0b2274269f162 Mon Sep 17 00:00:00 2001 From: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> Date: Tue, 14 Jul 2026 21:32:55 -0300 Subject: [PATCH 3/4] docs: include main hotfixes in 3.6.0 notes --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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 From f62ce2696bb4c2d1a0711577c726db7a0036e5cc Mon Sep 17 00:00:00 2001 From: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> Date: Tue, 14 Jul 2026 21:52:32 -0300 Subject: [PATCH 4/4] test: cover main hotfix regressions --- tests/test_init_gitignore_protection.py | 18 ++++++++++++++++++ tests/test_layers.py | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) 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()