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 `